util.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  6. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. * Copyright (C) 2015-2017 Intel Deutschland GmbH
  8. * Copyright (C) 2018 Intel Corporation
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * utilities for mac80211
  15. */
  16. #include <net/mac80211.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/export.h>
  19. #include <linux/types.h>
  20. #include <linux/slab.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/if_arp.h>
  24. #include <linux/bitmap.h>
  25. #include <linux/crc32.h>
  26. #include <net/net_namespace.h>
  27. #include <net/cfg80211.h>
  28. #include <net/rtnetlink.h>
  29. #include "ieee80211_i.h"
  30. #include "driver-ops.h"
  31. #include "rate.h"
  32. #include "mesh.h"
  33. #include "wme.h"
  34. #include "led.h"
  35. #include "wep.h"
  36. /* privid for wiphys to determine whether they belong to us or not */
  37. const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
  38. struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
  39. {
  40. struct ieee80211_local *local;
  41. BUG_ON(!wiphy);
  42. local = wiphy_priv(wiphy);
  43. return &local->hw;
  44. }
  45. EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
  46. void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
  47. {
  48. struct sk_buff *skb;
  49. struct ieee80211_hdr *hdr;
  50. skb_queue_walk(&tx->skbs, skb) {
  51. hdr = (struct ieee80211_hdr *) skb->data;
  52. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  53. }
  54. }
  55. int ieee80211_frame_duration(enum nl80211_band band, size_t len,
  56. int rate, int erp, int short_preamble,
  57. int shift)
  58. {
  59. int dur;
  60. /* calculate duration (in microseconds, rounded up to next higher
  61. * integer if it includes a fractional microsecond) to send frame of
  62. * len bytes (does not include FCS) at the given rate. Duration will
  63. * also include SIFS.
  64. *
  65. * rate is in 100 kbps, so divident is multiplied by 10 in the
  66. * DIV_ROUND_UP() operations.
  67. *
  68. * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
  69. * is assumed to be 0 otherwise.
  70. */
  71. if (band == NL80211_BAND_5GHZ || erp) {
  72. /*
  73. * OFDM:
  74. *
  75. * N_DBPS = DATARATE x 4
  76. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  77. * (16 = SIGNAL time, 6 = tail bits)
  78. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  79. *
  80. * T_SYM = 4 usec
  81. * 802.11a - 18.5.2: aSIFSTime = 16 usec
  82. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  83. * signal ext = 6 usec
  84. */
  85. dur = 16; /* SIFS + signal ext */
  86. dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
  87. dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
  88. /* IEEE 802.11-2012 18.3.2.4: all values above are:
  89. * * times 4 for 5 MHz
  90. * * times 2 for 10 MHz
  91. */
  92. dur *= 1 << shift;
  93. /* rates should already consider the channel bandwidth,
  94. * don't apply divisor again.
  95. */
  96. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  97. 4 * rate); /* T_SYM x N_SYM */
  98. } else {
  99. /*
  100. * 802.11b or 802.11g with 802.11b compatibility:
  101. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  102. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  103. *
  104. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  105. * aSIFSTime = 10 usec
  106. * aPreambleLength = 144 usec or 72 usec with short preamble
  107. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  108. */
  109. dur = 10; /* aSIFSTime = 10 usec */
  110. dur += short_preamble ? (72 + 24) : (144 + 48);
  111. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  112. }
  113. return dur;
  114. }
  115. /* Exported duration function for driver use */
  116. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  117. struct ieee80211_vif *vif,
  118. enum nl80211_band band,
  119. size_t frame_len,
  120. struct ieee80211_rate *rate)
  121. {
  122. struct ieee80211_sub_if_data *sdata;
  123. u16 dur;
  124. int erp, shift = 0;
  125. bool short_preamble = false;
  126. erp = 0;
  127. if (vif) {
  128. sdata = vif_to_sdata(vif);
  129. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  130. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  131. erp = rate->flags & IEEE80211_RATE_ERP_G;
  132. shift = ieee80211_vif_get_shift(vif);
  133. }
  134. dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
  135. short_preamble, shift);
  136. return cpu_to_le16(dur);
  137. }
  138. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  139. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  140. struct ieee80211_vif *vif, size_t frame_len,
  141. const struct ieee80211_tx_info *frame_txctl)
  142. {
  143. struct ieee80211_local *local = hw_to_local(hw);
  144. struct ieee80211_rate *rate;
  145. struct ieee80211_sub_if_data *sdata;
  146. bool short_preamble;
  147. int erp, shift = 0, bitrate;
  148. u16 dur;
  149. struct ieee80211_supported_band *sband;
  150. sband = local->hw.wiphy->bands[frame_txctl->band];
  151. short_preamble = false;
  152. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  153. erp = 0;
  154. if (vif) {
  155. sdata = vif_to_sdata(vif);
  156. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  157. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  158. erp = rate->flags & IEEE80211_RATE_ERP_G;
  159. shift = ieee80211_vif_get_shift(vif);
  160. }
  161. bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
  162. /* CTS duration */
  163. dur = ieee80211_frame_duration(sband->band, 10, bitrate,
  164. erp, short_preamble, shift);
  165. /* Data frame duration */
  166. dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
  167. erp, short_preamble, shift);
  168. /* ACK duration */
  169. dur += ieee80211_frame_duration(sband->band, 10, bitrate,
  170. erp, short_preamble, shift);
  171. return cpu_to_le16(dur);
  172. }
  173. EXPORT_SYMBOL(ieee80211_rts_duration);
  174. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  175. struct ieee80211_vif *vif,
  176. size_t frame_len,
  177. const struct ieee80211_tx_info *frame_txctl)
  178. {
  179. struct ieee80211_local *local = hw_to_local(hw);
  180. struct ieee80211_rate *rate;
  181. struct ieee80211_sub_if_data *sdata;
  182. bool short_preamble;
  183. int erp, shift = 0, bitrate;
  184. u16 dur;
  185. struct ieee80211_supported_band *sband;
  186. sband = local->hw.wiphy->bands[frame_txctl->band];
  187. short_preamble = false;
  188. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  189. erp = 0;
  190. if (vif) {
  191. sdata = vif_to_sdata(vif);
  192. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  193. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  194. erp = rate->flags & IEEE80211_RATE_ERP_G;
  195. shift = ieee80211_vif_get_shift(vif);
  196. }
  197. bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
  198. /* Data frame duration */
  199. dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
  200. erp, short_preamble, shift);
  201. if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
  202. /* ACK duration */
  203. dur += ieee80211_frame_duration(sband->band, 10, bitrate,
  204. erp, short_preamble, shift);
  205. }
  206. return cpu_to_le16(dur);
  207. }
  208. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  209. void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
  210. {
  211. struct ieee80211_sub_if_data *sdata;
  212. int n_acs = IEEE80211_NUM_ACS;
  213. if (local->ops->wake_tx_queue)
  214. return;
  215. if (local->hw.queues < IEEE80211_NUM_ACS)
  216. n_acs = 1;
  217. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  218. int ac;
  219. if (!sdata->dev)
  220. continue;
  221. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
  222. local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
  223. continue;
  224. for (ac = 0; ac < n_acs; ac++) {
  225. int ac_queue = sdata->vif.hw_queue[ac];
  226. if (ac_queue == queue ||
  227. (sdata->vif.cab_queue == queue &&
  228. local->queue_stop_reasons[ac_queue] == 0 &&
  229. skb_queue_empty(&local->pending[ac_queue])))
  230. netif_wake_subqueue(sdata->dev, ac);
  231. }
  232. }
  233. }
  234. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  235. enum queue_stop_reason reason,
  236. bool refcounted)
  237. {
  238. struct ieee80211_local *local = hw_to_local(hw);
  239. trace_wake_queue(local, queue, reason);
  240. if (WARN_ON(queue >= hw->queues))
  241. return;
  242. if (!test_bit(reason, &local->queue_stop_reasons[queue]))
  243. return;
  244. if (!refcounted) {
  245. local->q_stop_reasons[queue][reason] = 0;
  246. } else {
  247. local->q_stop_reasons[queue][reason]--;
  248. if (WARN_ON(local->q_stop_reasons[queue][reason] < 0))
  249. local->q_stop_reasons[queue][reason] = 0;
  250. }
  251. if (local->q_stop_reasons[queue][reason] == 0)
  252. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  253. if (local->queue_stop_reasons[queue] != 0)
  254. /* someone still has this queue stopped */
  255. return;
  256. if (skb_queue_empty(&local->pending[queue])) {
  257. rcu_read_lock();
  258. ieee80211_propagate_queue_wake(local, queue);
  259. rcu_read_unlock();
  260. } else
  261. tasklet_schedule(&local->tx_pending_tasklet);
  262. }
  263. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  264. enum queue_stop_reason reason,
  265. bool refcounted)
  266. {
  267. struct ieee80211_local *local = hw_to_local(hw);
  268. unsigned long flags;
  269. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  270. __ieee80211_wake_queue(hw, queue, reason, refcounted);
  271. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  272. }
  273. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  274. {
  275. ieee80211_wake_queue_by_reason(hw, queue,
  276. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  277. false);
  278. }
  279. EXPORT_SYMBOL(ieee80211_wake_queue);
  280. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  281. enum queue_stop_reason reason,
  282. bool refcounted)
  283. {
  284. struct ieee80211_local *local = hw_to_local(hw);
  285. struct ieee80211_sub_if_data *sdata;
  286. int n_acs = IEEE80211_NUM_ACS;
  287. trace_stop_queue(local, queue, reason);
  288. if (WARN_ON(queue >= hw->queues))
  289. return;
  290. if (!refcounted)
  291. local->q_stop_reasons[queue][reason] = 1;
  292. else
  293. local->q_stop_reasons[queue][reason]++;
  294. if (__test_and_set_bit(reason, &local->queue_stop_reasons[queue]))
  295. return;
  296. if (local->ops->wake_tx_queue)
  297. return;
  298. if (local->hw.queues < IEEE80211_NUM_ACS)
  299. n_acs = 1;
  300. rcu_read_lock();
  301. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  302. int ac;
  303. if (!sdata->dev)
  304. continue;
  305. for (ac = 0; ac < n_acs; ac++) {
  306. if (sdata->vif.hw_queue[ac] == queue ||
  307. sdata->vif.cab_queue == queue)
  308. netif_stop_subqueue(sdata->dev, ac);
  309. }
  310. }
  311. rcu_read_unlock();
  312. }
  313. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  314. enum queue_stop_reason reason,
  315. bool refcounted)
  316. {
  317. struct ieee80211_local *local = hw_to_local(hw);
  318. unsigned long flags;
  319. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  320. __ieee80211_stop_queue(hw, queue, reason, refcounted);
  321. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  322. }
  323. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  324. {
  325. ieee80211_stop_queue_by_reason(hw, queue,
  326. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  327. false);
  328. }
  329. EXPORT_SYMBOL(ieee80211_stop_queue);
  330. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  331. struct sk_buff *skb)
  332. {
  333. struct ieee80211_hw *hw = &local->hw;
  334. unsigned long flags;
  335. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  336. int queue = info->hw_queue;
  337. if (WARN_ON(!info->control.vif)) {
  338. ieee80211_free_txskb(&local->hw, skb);
  339. return;
  340. }
  341. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  342. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  343. false);
  344. __skb_queue_tail(&local->pending[queue], skb);
  345. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  346. false);
  347. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  348. }
  349. void ieee80211_add_pending_skbs(struct ieee80211_local *local,
  350. struct sk_buff_head *skbs)
  351. {
  352. struct ieee80211_hw *hw = &local->hw;
  353. struct sk_buff *skb;
  354. unsigned long flags;
  355. int queue, i;
  356. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  357. while ((skb = skb_dequeue(skbs))) {
  358. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  359. if (WARN_ON(!info->control.vif)) {
  360. ieee80211_free_txskb(&local->hw, skb);
  361. continue;
  362. }
  363. queue = info->hw_queue;
  364. __ieee80211_stop_queue(hw, queue,
  365. IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  366. false);
  367. __skb_queue_tail(&local->pending[queue], skb);
  368. }
  369. for (i = 0; i < hw->queues; i++)
  370. __ieee80211_wake_queue(hw, i,
  371. IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  372. false);
  373. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  374. }
  375. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  376. unsigned long queues,
  377. enum queue_stop_reason reason,
  378. bool refcounted)
  379. {
  380. struct ieee80211_local *local = hw_to_local(hw);
  381. unsigned long flags;
  382. int i;
  383. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  384. for_each_set_bit(i, &queues, hw->queues)
  385. __ieee80211_stop_queue(hw, i, reason, refcounted);
  386. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  387. }
  388. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  389. {
  390. ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  391. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  392. false);
  393. }
  394. EXPORT_SYMBOL(ieee80211_stop_queues);
  395. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  396. {
  397. struct ieee80211_local *local = hw_to_local(hw);
  398. unsigned long flags;
  399. int ret;
  400. if (WARN_ON(queue >= hw->queues))
  401. return true;
  402. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  403. ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
  404. &local->queue_stop_reasons[queue]);
  405. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  406. return ret;
  407. }
  408. EXPORT_SYMBOL(ieee80211_queue_stopped);
  409. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  410. unsigned long queues,
  411. enum queue_stop_reason reason,
  412. bool refcounted)
  413. {
  414. struct ieee80211_local *local = hw_to_local(hw);
  415. unsigned long flags;
  416. int i;
  417. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  418. for_each_set_bit(i, &queues, hw->queues)
  419. __ieee80211_wake_queue(hw, i, reason, refcounted);
  420. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  421. }
  422. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  423. {
  424. ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  425. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  426. false);
  427. }
  428. EXPORT_SYMBOL(ieee80211_wake_queues);
  429. static unsigned int
  430. ieee80211_get_vif_queues(struct ieee80211_local *local,
  431. struct ieee80211_sub_if_data *sdata)
  432. {
  433. unsigned int queues;
  434. if (sdata && ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
  435. int ac;
  436. queues = 0;
  437. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  438. queues |= BIT(sdata->vif.hw_queue[ac]);
  439. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
  440. queues |= BIT(sdata->vif.cab_queue);
  441. } else {
  442. /* all queues */
  443. queues = BIT(local->hw.queues) - 1;
  444. }
  445. return queues;
  446. }
  447. void __ieee80211_flush_queues(struct ieee80211_local *local,
  448. struct ieee80211_sub_if_data *sdata,
  449. unsigned int queues, bool drop)
  450. {
  451. if (!local->ops->flush)
  452. return;
  453. /*
  454. * If no queue was set, or if the HW doesn't support
  455. * IEEE80211_HW_QUEUE_CONTROL - flush all queues
  456. */
  457. if (!queues || !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
  458. queues = ieee80211_get_vif_queues(local, sdata);
  459. ieee80211_stop_queues_by_reason(&local->hw, queues,
  460. IEEE80211_QUEUE_STOP_REASON_FLUSH,
  461. false);
  462. drv_flush(local, sdata, queues, drop);
  463. ieee80211_wake_queues_by_reason(&local->hw, queues,
  464. IEEE80211_QUEUE_STOP_REASON_FLUSH,
  465. false);
  466. }
  467. void ieee80211_flush_queues(struct ieee80211_local *local,
  468. struct ieee80211_sub_if_data *sdata, bool drop)
  469. {
  470. __ieee80211_flush_queues(local, sdata, 0, drop);
  471. }
  472. void ieee80211_stop_vif_queues(struct ieee80211_local *local,
  473. struct ieee80211_sub_if_data *sdata,
  474. enum queue_stop_reason reason)
  475. {
  476. ieee80211_stop_queues_by_reason(&local->hw,
  477. ieee80211_get_vif_queues(local, sdata),
  478. reason, true);
  479. }
  480. void ieee80211_wake_vif_queues(struct ieee80211_local *local,
  481. struct ieee80211_sub_if_data *sdata,
  482. enum queue_stop_reason reason)
  483. {
  484. ieee80211_wake_queues_by_reason(&local->hw,
  485. ieee80211_get_vif_queues(local, sdata),
  486. reason, true);
  487. }
  488. static void __iterate_interfaces(struct ieee80211_local *local,
  489. u32 iter_flags,
  490. void (*iterator)(void *data, u8 *mac,
  491. struct ieee80211_vif *vif),
  492. void *data)
  493. {
  494. struct ieee80211_sub_if_data *sdata;
  495. bool active_only = iter_flags & IEEE80211_IFACE_ITER_ACTIVE;
  496. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  497. switch (sdata->vif.type) {
  498. case NL80211_IFTYPE_MONITOR:
  499. if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
  500. continue;
  501. break;
  502. case NL80211_IFTYPE_AP_VLAN:
  503. continue;
  504. default:
  505. break;
  506. }
  507. if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
  508. active_only && !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  509. continue;
  510. if (ieee80211_sdata_running(sdata) || !active_only)
  511. iterator(data, sdata->vif.addr,
  512. &sdata->vif);
  513. }
  514. sdata = rcu_dereference_check(local->monitor_sdata,
  515. lockdep_is_held(&local->iflist_mtx) ||
  516. lockdep_rtnl_is_held());
  517. if (sdata &&
  518. (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || !active_only ||
  519. sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  520. iterator(data, sdata->vif.addr, &sdata->vif);
  521. }
  522. void ieee80211_iterate_interfaces(
  523. struct ieee80211_hw *hw, u32 iter_flags,
  524. void (*iterator)(void *data, u8 *mac,
  525. struct ieee80211_vif *vif),
  526. void *data)
  527. {
  528. struct ieee80211_local *local = hw_to_local(hw);
  529. mutex_lock(&local->iflist_mtx);
  530. __iterate_interfaces(local, iter_flags, iterator, data);
  531. mutex_unlock(&local->iflist_mtx);
  532. }
  533. EXPORT_SYMBOL_GPL(ieee80211_iterate_interfaces);
  534. void ieee80211_iterate_active_interfaces_atomic(
  535. struct ieee80211_hw *hw, u32 iter_flags,
  536. void (*iterator)(void *data, u8 *mac,
  537. struct ieee80211_vif *vif),
  538. void *data)
  539. {
  540. struct ieee80211_local *local = hw_to_local(hw);
  541. rcu_read_lock();
  542. __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
  543. iterator, data);
  544. rcu_read_unlock();
  545. }
  546. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  547. void ieee80211_iterate_active_interfaces_rtnl(
  548. struct ieee80211_hw *hw, u32 iter_flags,
  549. void (*iterator)(void *data, u8 *mac,
  550. struct ieee80211_vif *vif),
  551. void *data)
  552. {
  553. struct ieee80211_local *local = hw_to_local(hw);
  554. ASSERT_RTNL();
  555. __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
  556. iterator, data);
  557. }
  558. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_rtnl);
  559. static void __iterate_stations(struct ieee80211_local *local,
  560. void (*iterator)(void *data,
  561. struct ieee80211_sta *sta),
  562. void *data)
  563. {
  564. struct sta_info *sta;
  565. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  566. if (!sta->uploaded)
  567. continue;
  568. iterator(data, &sta->sta);
  569. }
  570. }
  571. void ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
  572. void (*iterator)(void *data,
  573. struct ieee80211_sta *sta),
  574. void *data)
  575. {
  576. struct ieee80211_local *local = hw_to_local(hw);
  577. rcu_read_lock();
  578. __iterate_stations(local, iterator, data);
  579. rcu_read_unlock();
  580. }
  581. EXPORT_SYMBOL_GPL(ieee80211_iterate_stations_atomic);
  582. struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
  583. {
  584. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  585. if (!ieee80211_sdata_running(sdata) ||
  586. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  587. return NULL;
  588. return &sdata->vif;
  589. }
  590. EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
  591. struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
  592. {
  593. struct ieee80211_sub_if_data *sdata;
  594. if (!vif)
  595. return NULL;
  596. sdata = vif_to_sdata(vif);
  597. if (!ieee80211_sdata_running(sdata) ||
  598. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  599. return NULL;
  600. return &sdata->wdev;
  601. }
  602. EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev);
  603. /*
  604. * Nothing should have been stuffed into the workqueue during
  605. * the suspend->resume cycle. Since we can't check each caller
  606. * of this function if we are already quiescing / suspended,
  607. * check here and don't WARN since this can actually happen when
  608. * the rx path (for example) is racing against __ieee80211_suspend
  609. * and suspending / quiescing was set after the rx path checked
  610. * them.
  611. */
  612. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  613. {
  614. if (local->quiescing || (local->suspended && !local->resuming)) {
  615. pr_warn("queueing ieee80211 work while going to suspend\n");
  616. return false;
  617. }
  618. return true;
  619. }
  620. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  621. {
  622. struct ieee80211_local *local = hw_to_local(hw);
  623. if (!ieee80211_can_queue_work(local))
  624. return;
  625. queue_work(local->workqueue, work);
  626. }
  627. EXPORT_SYMBOL(ieee80211_queue_work);
  628. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  629. struct delayed_work *dwork,
  630. unsigned long delay)
  631. {
  632. struct ieee80211_local *local = hw_to_local(hw);
  633. if (!ieee80211_can_queue_work(local))
  634. return;
  635. queue_delayed_work(local->workqueue, dwork, delay);
  636. }
  637. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  638. u32 ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
  639. struct ieee802_11_elems *elems,
  640. u64 filter, u32 crc)
  641. {
  642. size_t left = len;
  643. const u8 *pos = start;
  644. bool calc_crc = filter != 0;
  645. DECLARE_BITMAP(seen_elems, 256);
  646. const u8 *ie;
  647. bitmap_zero(seen_elems, 256);
  648. memset(elems, 0, sizeof(*elems));
  649. elems->ie_start = start;
  650. elems->total_len = len;
  651. while (left >= 2) {
  652. u8 id, elen;
  653. bool elem_parse_failed;
  654. id = *pos++;
  655. elen = *pos++;
  656. left -= 2;
  657. if (elen > left) {
  658. elems->parse_error = true;
  659. break;
  660. }
  661. switch (id) {
  662. case WLAN_EID_SSID:
  663. case WLAN_EID_SUPP_RATES:
  664. case WLAN_EID_FH_PARAMS:
  665. case WLAN_EID_DS_PARAMS:
  666. case WLAN_EID_CF_PARAMS:
  667. case WLAN_EID_TIM:
  668. case WLAN_EID_IBSS_PARAMS:
  669. case WLAN_EID_CHALLENGE:
  670. case WLAN_EID_RSN:
  671. case WLAN_EID_ERP_INFO:
  672. case WLAN_EID_EXT_SUPP_RATES:
  673. case WLAN_EID_HT_CAPABILITY:
  674. case WLAN_EID_HT_OPERATION:
  675. case WLAN_EID_VHT_CAPABILITY:
  676. case WLAN_EID_VHT_OPERATION:
  677. case WLAN_EID_MESH_ID:
  678. case WLAN_EID_MESH_CONFIG:
  679. case WLAN_EID_PEER_MGMT:
  680. case WLAN_EID_PREQ:
  681. case WLAN_EID_PREP:
  682. case WLAN_EID_PERR:
  683. case WLAN_EID_RANN:
  684. case WLAN_EID_CHANNEL_SWITCH:
  685. case WLAN_EID_EXT_CHANSWITCH_ANN:
  686. case WLAN_EID_COUNTRY:
  687. case WLAN_EID_PWR_CONSTRAINT:
  688. case WLAN_EID_TIMEOUT_INTERVAL:
  689. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  690. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  691. case WLAN_EID_CHAN_SWITCH_PARAM:
  692. case WLAN_EID_EXT_CAPABILITY:
  693. case WLAN_EID_CHAN_SWITCH_TIMING:
  694. case WLAN_EID_LINK_ID:
  695. case WLAN_EID_BSS_MAX_IDLE_PERIOD:
  696. /*
  697. * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
  698. * that if the content gets bigger it might be needed more than once
  699. */
  700. if (test_bit(id, seen_elems)) {
  701. elems->parse_error = true;
  702. left -= elen;
  703. pos += elen;
  704. continue;
  705. }
  706. break;
  707. }
  708. if (calc_crc && id < 64 && (filter & (1ULL << id)))
  709. crc = crc32_be(crc, pos - 2, elen + 2);
  710. elem_parse_failed = false;
  711. switch (id) {
  712. case WLAN_EID_LINK_ID:
  713. if (elen + 2 != sizeof(struct ieee80211_tdls_lnkie)) {
  714. elem_parse_failed = true;
  715. break;
  716. }
  717. elems->lnk_id = (void *)(pos - 2);
  718. break;
  719. case WLAN_EID_CHAN_SWITCH_TIMING:
  720. if (elen != sizeof(struct ieee80211_ch_switch_timing)) {
  721. elem_parse_failed = true;
  722. break;
  723. }
  724. elems->ch_sw_timing = (void *)pos;
  725. break;
  726. case WLAN_EID_EXT_CAPABILITY:
  727. elems->ext_capab = pos;
  728. elems->ext_capab_len = elen;
  729. break;
  730. case WLAN_EID_SSID:
  731. elems->ssid = pos;
  732. elems->ssid_len = elen;
  733. break;
  734. case WLAN_EID_SUPP_RATES:
  735. elems->supp_rates = pos;
  736. elems->supp_rates_len = elen;
  737. break;
  738. case WLAN_EID_DS_PARAMS:
  739. if (elen >= 1)
  740. elems->ds_params = pos;
  741. else
  742. elem_parse_failed = true;
  743. break;
  744. case WLAN_EID_TIM:
  745. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  746. elems->tim = (void *)pos;
  747. elems->tim_len = elen;
  748. } else
  749. elem_parse_failed = true;
  750. break;
  751. case WLAN_EID_CHALLENGE:
  752. elems->challenge = pos;
  753. elems->challenge_len = elen;
  754. break;
  755. case WLAN_EID_VENDOR_SPECIFIC:
  756. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  757. pos[2] == 0xf2) {
  758. /* Microsoft OUI (00:50:F2) */
  759. if (calc_crc)
  760. crc = crc32_be(crc, pos - 2, elen + 2);
  761. if (elen >= 5 && pos[3] == 2) {
  762. /* OUI Type 2 - WMM IE */
  763. if (pos[4] == 0) {
  764. elems->wmm_info = pos;
  765. elems->wmm_info_len = elen;
  766. } else if (pos[4] == 1) {
  767. elems->wmm_param = pos;
  768. elems->wmm_param_len = elen;
  769. }
  770. }
  771. }
  772. break;
  773. case WLAN_EID_RSN:
  774. elems->rsn = pos;
  775. elems->rsn_len = elen;
  776. break;
  777. case WLAN_EID_ERP_INFO:
  778. if (elen >= 1)
  779. elems->erp_info = pos;
  780. else
  781. elem_parse_failed = true;
  782. break;
  783. case WLAN_EID_EXT_SUPP_RATES:
  784. elems->ext_supp_rates = pos;
  785. elems->ext_supp_rates_len = elen;
  786. break;
  787. case WLAN_EID_HT_CAPABILITY:
  788. if (elen >= sizeof(struct ieee80211_ht_cap))
  789. elems->ht_cap_elem = (void *)pos;
  790. else
  791. elem_parse_failed = true;
  792. break;
  793. case WLAN_EID_HT_OPERATION:
  794. if (elen >= sizeof(struct ieee80211_ht_operation))
  795. elems->ht_operation = (void *)pos;
  796. else
  797. elem_parse_failed = true;
  798. break;
  799. case WLAN_EID_VHT_CAPABILITY:
  800. if (elen >= sizeof(struct ieee80211_vht_cap))
  801. elems->vht_cap_elem = (void *)pos;
  802. else
  803. elem_parse_failed = true;
  804. break;
  805. case WLAN_EID_VHT_OPERATION:
  806. if (elen >= sizeof(struct ieee80211_vht_operation))
  807. elems->vht_operation = (void *)pos;
  808. else
  809. elem_parse_failed = true;
  810. break;
  811. case WLAN_EID_OPMODE_NOTIF:
  812. if (elen > 0)
  813. elems->opmode_notif = pos;
  814. else
  815. elem_parse_failed = true;
  816. break;
  817. case WLAN_EID_MESH_ID:
  818. elems->mesh_id = pos;
  819. elems->mesh_id_len = elen;
  820. break;
  821. case WLAN_EID_MESH_CONFIG:
  822. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  823. elems->mesh_config = (void *)pos;
  824. else
  825. elem_parse_failed = true;
  826. break;
  827. case WLAN_EID_PEER_MGMT:
  828. elems->peering = pos;
  829. elems->peering_len = elen;
  830. break;
  831. case WLAN_EID_MESH_AWAKE_WINDOW:
  832. if (elen >= 2)
  833. elems->awake_window = (void *)pos;
  834. break;
  835. case WLAN_EID_PREQ:
  836. elems->preq = pos;
  837. elems->preq_len = elen;
  838. break;
  839. case WLAN_EID_PREP:
  840. elems->prep = pos;
  841. elems->prep_len = elen;
  842. break;
  843. case WLAN_EID_PERR:
  844. elems->perr = pos;
  845. elems->perr_len = elen;
  846. break;
  847. case WLAN_EID_RANN:
  848. if (elen >= sizeof(struct ieee80211_rann_ie))
  849. elems->rann = (void *)pos;
  850. else
  851. elem_parse_failed = true;
  852. break;
  853. case WLAN_EID_CHANNEL_SWITCH:
  854. if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
  855. elem_parse_failed = true;
  856. break;
  857. }
  858. elems->ch_switch_ie = (void *)pos;
  859. break;
  860. case WLAN_EID_EXT_CHANSWITCH_ANN:
  861. if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
  862. elem_parse_failed = true;
  863. break;
  864. }
  865. elems->ext_chansw_ie = (void *)pos;
  866. break;
  867. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  868. if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
  869. elem_parse_failed = true;
  870. break;
  871. }
  872. elems->sec_chan_offs = (void *)pos;
  873. break;
  874. case WLAN_EID_CHAN_SWITCH_PARAM:
  875. if (elen !=
  876. sizeof(*elems->mesh_chansw_params_ie)) {
  877. elem_parse_failed = true;
  878. break;
  879. }
  880. elems->mesh_chansw_params_ie = (void *)pos;
  881. break;
  882. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  883. if (!action ||
  884. elen != sizeof(*elems->wide_bw_chansw_ie)) {
  885. elem_parse_failed = true;
  886. break;
  887. }
  888. elems->wide_bw_chansw_ie = (void *)pos;
  889. break;
  890. case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
  891. if (action) {
  892. elem_parse_failed = true;
  893. break;
  894. }
  895. /*
  896. * This is a bit tricky, but as we only care about
  897. * the wide bandwidth channel switch element, so
  898. * just parse it out manually.
  899. */
  900. ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
  901. pos, elen);
  902. if (ie) {
  903. if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
  904. elems->wide_bw_chansw_ie =
  905. (void *)(ie + 2);
  906. else
  907. elem_parse_failed = true;
  908. }
  909. break;
  910. case WLAN_EID_COUNTRY:
  911. elems->country_elem = pos;
  912. elems->country_elem_len = elen;
  913. break;
  914. case WLAN_EID_PWR_CONSTRAINT:
  915. if (elen != 1) {
  916. elem_parse_failed = true;
  917. break;
  918. }
  919. elems->pwr_constr_elem = pos;
  920. break;
  921. case WLAN_EID_CISCO_VENDOR_SPECIFIC:
  922. /* Lots of different options exist, but we only care
  923. * about the Dynamic Transmit Power Control element.
  924. * First check for the Cisco OUI, then for the DTPC
  925. * tag (0x00).
  926. */
  927. if (elen < 4) {
  928. elem_parse_failed = true;
  929. break;
  930. }
  931. if (pos[0] != 0x00 || pos[1] != 0x40 ||
  932. pos[2] != 0x96 || pos[3] != 0x00)
  933. break;
  934. if (elen != 6) {
  935. elem_parse_failed = true;
  936. break;
  937. }
  938. if (calc_crc)
  939. crc = crc32_be(crc, pos - 2, elen + 2);
  940. elems->cisco_dtpc_elem = pos;
  941. break;
  942. case WLAN_EID_TIMEOUT_INTERVAL:
  943. if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
  944. elems->timeout_int = (void *)pos;
  945. else
  946. elem_parse_failed = true;
  947. break;
  948. case WLAN_EID_BSS_MAX_IDLE_PERIOD:
  949. if (elen >= sizeof(*elems->max_idle_period_ie))
  950. elems->max_idle_period_ie = (void *)pos;
  951. break;
  952. case WLAN_EID_EXTENSION:
  953. if (pos[0] == WLAN_EID_EXT_HE_MU_EDCA &&
  954. elen >= (sizeof(*elems->mu_edca_param_set) + 1)) {
  955. elems->mu_edca_param_set = (void *)&pos[1];
  956. } else if (pos[0] == WLAN_EID_EXT_HE_CAPABILITY) {
  957. elems->he_cap = (void *)&pos[1];
  958. elems->he_cap_len = elen - 1;
  959. } else if (pos[0] == WLAN_EID_EXT_HE_OPERATION &&
  960. elen >= sizeof(*elems->he_operation) &&
  961. elen >= ieee80211_he_oper_size(&pos[1])) {
  962. elems->he_operation = (void *)&pos[1];
  963. } else if (pos[0] == WLAN_EID_EXT_UORA && elen >= 1) {
  964. elems->uora_element = (void *)&pos[1];
  965. }
  966. break;
  967. default:
  968. break;
  969. }
  970. if (elem_parse_failed)
  971. elems->parse_error = true;
  972. else
  973. __set_bit(id, seen_elems);
  974. left -= elen;
  975. pos += elen;
  976. }
  977. if (left != 0)
  978. elems->parse_error = true;
  979. return crc;
  980. }
  981. void ieee80211_regulatory_limit_wmm_params(struct ieee80211_sub_if_data *sdata,
  982. struct ieee80211_tx_queue_params
  983. *qparam, int ac)
  984. {
  985. struct ieee80211_chanctx_conf *chanctx_conf;
  986. const struct ieee80211_reg_rule *rrule;
  987. struct ieee80211_wmm_ac *wmm_ac;
  988. u16 center_freq = 0;
  989. if (sdata->vif.type != NL80211_IFTYPE_AP &&
  990. sdata->vif.type != NL80211_IFTYPE_STATION)
  991. return;
  992. rcu_read_lock();
  993. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  994. if (chanctx_conf)
  995. center_freq = chanctx_conf->def.chan->center_freq;
  996. if (!center_freq) {
  997. rcu_read_unlock();
  998. return;
  999. }
  1000. rrule = freq_reg_info(sdata->wdev.wiphy, MHZ_TO_KHZ(center_freq));
  1001. if (IS_ERR_OR_NULL(rrule) || !rrule->wmm_rule) {
  1002. rcu_read_unlock();
  1003. return;
  1004. }
  1005. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1006. wmm_ac = &rrule->wmm_rule->ap[ac];
  1007. else
  1008. wmm_ac = &rrule->wmm_rule->client[ac];
  1009. qparam->cw_min = max_t(u16, qparam->cw_min, wmm_ac->cw_min);
  1010. qparam->cw_max = max_t(u16, qparam->cw_max, wmm_ac->cw_max);
  1011. qparam->aifs = max_t(u8, qparam->aifs, wmm_ac->aifsn);
  1012. qparam->txop = !qparam->txop ? wmm_ac->cot / 32 :
  1013. min_t(u16, qparam->txop, wmm_ac->cot / 32);
  1014. rcu_read_unlock();
  1015. }
  1016. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
  1017. bool bss_notify, bool enable_qos)
  1018. {
  1019. struct ieee80211_local *local = sdata->local;
  1020. struct ieee80211_tx_queue_params qparam;
  1021. struct ieee80211_chanctx_conf *chanctx_conf;
  1022. int ac;
  1023. bool use_11b;
  1024. bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
  1025. int aCWmin, aCWmax;
  1026. if (!local->ops->conf_tx)
  1027. return;
  1028. if (local->hw.queues < IEEE80211_NUM_ACS)
  1029. return;
  1030. memset(&qparam, 0, sizeof(qparam));
  1031. rcu_read_lock();
  1032. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  1033. use_11b = (chanctx_conf &&
  1034. chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) &&
  1035. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  1036. rcu_read_unlock();
  1037. is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
  1038. /* Set defaults according to 802.11-2007 Table 7-37 */
  1039. aCWmax = 1023;
  1040. if (use_11b)
  1041. aCWmin = 31;
  1042. else
  1043. aCWmin = 15;
  1044. /* Confiure old 802.11b/g medium access rules. */
  1045. qparam.cw_max = aCWmax;
  1046. qparam.cw_min = aCWmin;
  1047. qparam.txop = 0;
  1048. qparam.aifs = 2;
  1049. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1050. /* Update if QoS is enabled. */
  1051. if (enable_qos) {
  1052. switch (ac) {
  1053. case IEEE80211_AC_BK:
  1054. qparam.cw_max = aCWmax;
  1055. qparam.cw_min = aCWmin;
  1056. qparam.txop = 0;
  1057. if (is_ocb)
  1058. qparam.aifs = 9;
  1059. else
  1060. qparam.aifs = 7;
  1061. break;
  1062. /* never happens but let's not leave undefined */
  1063. default:
  1064. case IEEE80211_AC_BE:
  1065. qparam.cw_max = aCWmax;
  1066. qparam.cw_min = aCWmin;
  1067. qparam.txop = 0;
  1068. if (is_ocb)
  1069. qparam.aifs = 6;
  1070. else
  1071. qparam.aifs = 3;
  1072. break;
  1073. case IEEE80211_AC_VI:
  1074. qparam.cw_max = aCWmin;
  1075. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  1076. if (is_ocb)
  1077. qparam.txop = 0;
  1078. else if (use_11b)
  1079. qparam.txop = 6016/32;
  1080. else
  1081. qparam.txop = 3008/32;
  1082. if (is_ocb)
  1083. qparam.aifs = 3;
  1084. else
  1085. qparam.aifs = 2;
  1086. break;
  1087. case IEEE80211_AC_VO:
  1088. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  1089. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  1090. if (is_ocb)
  1091. qparam.txop = 0;
  1092. else if (use_11b)
  1093. qparam.txop = 3264/32;
  1094. else
  1095. qparam.txop = 1504/32;
  1096. qparam.aifs = 2;
  1097. break;
  1098. }
  1099. }
  1100. ieee80211_regulatory_limit_wmm_params(sdata, &qparam, ac);
  1101. qparam.uapsd = false;
  1102. sdata->tx_conf[ac] = qparam;
  1103. drv_conf_tx(local, sdata, ac, &qparam);
  1104. }
  1105. if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1106. sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
  1107. sdata->vif.type != NL80211_IFTYPE_NAN) {
  1108. sdata->vif.bss_conf.qos = enable_qos;
  1109. if (bss_notify)
  1110. ieee80211_bss_info_change_notify(sdata,
  1111. BSS_CHANGED_QOS);
  1112. }
  1113. }
  1114. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  1115. u16 transaction, u16 auth_alg, u16 status,
  1116. const u8 *extra, size_t extra_len, const u8 *da,
  1117. const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
  1118. u32 tx_flags)
  1119. {
  1120. struct ieee80211_local *local = sdata->local;
  1121. struct sk_buff *skb;
  1122. struct ieee80211_mgmt *mgmt;
  1123. int err;
  1124. /* 24 + 6 = header + auth_algo + auth_transaction + status_code */
  1125. skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
  1126. 24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN);
  1127. if (!skb)
  1128. return;
  1129. skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
  1130. mgmt = skb_put_zero(skb, 24 + 6);
  1131. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1132. IEEE80211_STYPE_AUTH);
  1133. memcpy(mgmt->da, da, ETH_ALEN);
  1134. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  1135. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  1136. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  1137. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  1138. mgmt->u.auth.status_code = cpu_to_le16(status);
  1139. if (extra)
  1140. skb_put_data(skb, extra, extra_len);
  1141. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  1142. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  1143. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  1144. WARN_ON(err);
  1145. }
  1146. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  1147. tx_flags;
  1148. ieee80211_tx_skb(sdata, skb);
  1149. }
  1150. void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  1151. const u8 *bssid, u16 stype, u16 reason,
  1152. bool send_frame, u8 *frame_buf)
  1153. {
  1154. struct ieee80211_local *local = sdata->local;
  1155. struct sk_buff *skb;
  1156. struct ieee80211_mgmt *mgmt = (void *)frame_buf;
  1157. /* build frame */
  1158. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  1159. mgmt->duration = 0; /* initialize only */
  1160. mgmt->seq_ctrl = 0; /* initialize only */
  1161. memcpy(mgmt->da, bssid, ETH_ALEN);
  1162. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  1163. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  1164. /* u.deauth.reason_code == u.disassoc.reason_code */
  1165. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  1166. if (send_frame) {
  1167. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  1168. IEEE80211_DEAUTH_FRAME_LEN);
  1169. if (!skb)
  1170. return;
  1171. skb_reserve(skb, local->hw.extra_tx_headroom);
  1172. /* copy in frame */
  1173. skb_put_data(skb, mgmt, IEEE80211_DEAUTH_FRAME_LEN);
  1174. if (sdata->vif.type != NL80211_IFTYPE_STATION ||
  1175. !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
  1176. IEEE80211_SKB_CB(skb)->flags |=
  1177. IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1178. ieee80211_tx_skb(sdata, skb);
  1179. }
  1180. }
  1181. static int ieee80211_build_preq_ies_band(struct ieee80211_local *local,
  1182. u8 *buffer, size_t buffer_len,
  1183. const u8 *ie, size_t ie_len,
  1184. enum nl80211_band band,
  1185. u32 rate_mask,
  1186. struct cfg80211_chan_def *chandef,
  1187. size_t *offset, u32 flags)
  1188. {
  1189. struct ieee80211_supported_band *sband;
  1190. const struct ieee80211_sta_he_cap *he_cap;
  1191. u8 *pos = buffer, *end = buffer + buffer_len;
  1192. size_t noffset;
  1193. int supp_rates_len, i;
  1194. u8 rates[32];
  1195. int num_rates;
  1196. int ext_rates_len;
  1197. int shift;
  1198. u32 rate_flags;
  1199. bool have_80mhz = false;
  1200. *offset = 0;
  1201. sband = local->hw.wiphy->bands[band];
  1202. if (WARN_ON_ONCE(!sband))
  1203. return 0;
  1204. rate_flags = ieee80211_chandef_rate_flags(chandef);
  1205. shift = ieee80211_chandef_get_shift(chandef);
  1206. num_rates = 0;
  1207. for (i = 0; i < sband->n_bitrates; i++) {
  1208. if ((BIT(i) & rate_mask) == 0)
  1209. continue; /* skip rate */
  1210. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1211. continue;
  1212. rates[num_rates++] =
  1213. (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
  1214. (1 << shift) * 5);
  1215. }
  1216. supp_rates_len = min_t(int, num_rates, 8);
  1217. if (end - pos < 2 + supp_rates_len)
  1218. goto out_err;
  1219. *pos++ = WLAN_EID_SUPP_RATES;
  1220. *pos++ = supp_rates_len;
  1221. memcpy(pos, rates, supp_rates_len);
  1222. pos += supp_rates_len;
  1223. /* insert "request information" if in custom IEs */
  1224. if (ie && ie_len) {
  1225. static const u8 before_extrates[] = {
  1226. WLAN_EID_SSID,
  1227. WLAN_EID_SUPP_RATES,
  1228. WLAN_EID_REQUEST,
  1229. };
  1230. noffset = ieee80211_ie_split(ie, ie_len,
  1231. before_extrates,
  1232. ARRAY_SIZE(before_extrates),
  1233. *offset);
  1234. if (end - pos < noffset - *offset)
  1235. goto out_err;
  1236. memcpy(pos, ie + *offset, noffset - *offset);
  1237. pos += noffset - *offset;
  1238. *offset = noffset;
  1239. }
  1240. ext_rates_len = num_rates - supp_rates_len;
  1241. if (ext_rates_len > 0) {
  1242. if (end - pos < 2 + ext_rates_len)
  1243. goto out_err;
  1244. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1245. *pos++ = ext_rates_len;
  1246. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  1247. pos += ext_rates_len;
  1248. }
  1249. if (chandef->chan && sband->band == NL80211_BAND_2GHZ) {
  1250. if (end - pos < 3)
  1251. goto out_err;
  1252. *pos++ = WLAN_EID_DS_PARAMS;
  1253. *pos++ = 1;
  1254. *pos++ = ieee80211_frequency_to_channel(
  1255. chandef->chan->center_freq);
  1256. }
  1257. if (flags & IEEE80211_PROBE_FLAG_MIN_CONTENT)
  1258. goto done;
  1259. /* insert custom IEs that go before HT */
  1260. if (ie && ie_len) {
  1261. static const u8 before_ht[] = {
  1262. /*
  1263. * no need to list the ones split off already
  1264. * (or generated here)
  1265. */
  1266. WLAN_EID_DS_PARAMS,
  1267. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  1268. };
  1269. noffset = ieee80211_ie_split(ie, ie_len,
  1270. before_ht, ARRAY_SIZE(before_ht),
  1271. *offset);
  1272. if (end - pos < noffset - *offset)
  1273. goto out_err;
  1274. memcpy(pos, ie + *offset, noffset - *offset);
  1275. pos += noffset - *offset;
  1276. *offset = noffset;
  1277. }
  1278. if (sband->ht_cap.ht_supported) {
  1279. if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
  1280. goto out_err;
  1281. pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
  1282. sband->ht_cap.cap);
  1283. }
  1284. /* insert custom IEs that go before VHT */
  1285. if (ie && ie_len) {
  1286. static const u8 before_vht[] = {
  1287. /*
  1288. * no need to list the ones split off already
  1289. * (or generated here)
  1290. */
  1291. WLAN_EID_BSS_COEX_2040,
  1292. WLAN_EID_EXT_CAPABILITY,
  1293. WLAN_EID_SSID_LIST,
  1294. WLAN_EID_CHANNEL_USAGE,
  1295. WLAN_EID_INTERWORKING,
  1296. WLAN_EID_MESH_ID,
  1297. /* 60 GHz (Multi-band, DMG, MMS) can't happen */
  1298. };
  1299. noffset = ieee80211_ie_split(ie, ie_len,
  1300. before_vht, ARRAY_SIZE(before_vht),
  1301. *offset);
  1302. if (end - pos < noffset - *offset)
  1303. goto out_err;
  1304. memcpy(pos, ie + *offset, noffset - *offset);
  1305. pos += noffset - *offset;
  1306. *offset = noffset;
  1307. }
  1308. /* Check if any channel in this sband supports at least 80 MHz */
  1309. for (i = 0; i < sband->n_channels; i++) {
  1310. if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
  1311. IEEE80211_CHAN_NO_80MHZ))
  1312. continue;
  1313. have_80mhz = true;
  1314. break;
  1315. }
  1316. if (sband->vht_cap.vht_supported && have_80mhz) {
  1317. if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
  1318. goto out_err;
  1319. pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
  1320. sband->vht_cap.cap);
  1321. }
  1322. /* insert custom IEs that go before HE */
  1323. if (ie && ie_len) {
  1324. static const u8 before_he[] = {
  1325. /*
  1326. * no need to list the ones split off before VHT
  1327. * or generated here
  1328. */
  1329. WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_REQ_PARAMS,
  1330. WLAN_EID_AP_CSN,
  1331. /* TODO: add 11ah/11aj/11ak elements */
  1332. };
  1333. noffset = ieee80211_ie_split(ie, ie_len,
  1334. before_he, ARRAY_SIZE(before_he),
  1335. *offset);
  1336. if (end - pos < noffset - *offset)
  1337. goto out_err;
  1338. memcpy(pos, ie + *offset, noffset - *offset);
  1339. pos += noffset - *offset;
  1340. *offset = noffset;
  1341. }
  1342. he_cap = ieee80211_get_he_sta_cap(sband);
  1343. if (he_cap) {
  1344. pos = ieee80211_ie_build_he_cap(pos, he_cap, end);
  1345. if (!pos)
  1346. goto out_err;
  1347. }
  1348. /*
  1349. * If adding more here, adjust code in main.c
  1350. * that calculates local->scan_ies_len.
  1351. */
  1352. return pos - buffer;
  1353. out_err:
  1354. WARN_ONCE(1, "not enough space for preq IEs\n");
  1355. done:
  1356. return pos - buffer;
  1357. }
  1358. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  1359. size_t buffer_len,
  1360. struct ieee80211_scan_ies *ie_desc,
  1361. const u8 *ie, size_t ie_len,
  1362. u8 bands_used, u32 *rate_masks,
  1363. struct cfg80211_chan_def *chandef,
  1364. u32 flags)
  1365. {
  1366. size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
  1367. int i;
  1368. memset(ie_desc, 0, sizeof(*ie_desc));
  1369. for (i = 0; i < NUM_NL80211_BANDS; i++) {
  1370. if (bands_used & BIT(i)) {
  1371. pos += ieee80211_build_preq_ies_band(local,
  1372. buffer + pos,
  1373. buffer_len - pos,
  1374. ie, ie_len, i,
  1375. rate_masks[i],
  1376. chandef,
  1377. &custom_ie_offset,
  1378. flags);
  1379. ie_desc->ies[i] = buffer + old_pos;
  1380. ie_desc->len[i] = pos - old_pos;
  1381. old_pos = pos;
  1382. }
  1383. }
  1384. /* add any remaining custom IEs */
  1385. if (ie && ie_len) {
  1386. if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
  1387. "not enough space for preq custom IEs\n"))
  1388. return pos;
  1389. memcpy(buffer + pos, ie + custom_ie_offset,
  1390. ie_len - custom_ie_offset);
  1391. ie_desc->common_ies = buffer + pos;
  1392. ie_desc->common_ie_len = ie_len - custom_ie_offset;
  1393. pos += ie_len - custom_ie_offset;
  1394. }
  1395. return pos;
  1396. };
  1397. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  1398. const u8 *src, const u8 *dst,
  1399. u32 ratemask,
  1400. struct ieee80211_channel *chan,
  1401. const u8 *ssid, size_t ssid_len,
  1402. const u8 *ie, size_t ie_len,
  1403. u32 flags)
  1404. {
  1405. struct ieee80211_local *local = sdata->local;
  1406. struct cfg80211_chan_def chandef;
  1407. struct sk_buff *skb;
  1408. struct ieee80211_mgmt *mgmt;
  1409. int ies_len;
  1410. u32 rate_masks[NUM_NL80211_BANDS] = {};
  1411. struct ieee80211_scan_ies dummy_ie_desc;
  1412. /*
  1413. * Do not send DS Channel parameter for directed probe requests
  1414. * in order to maximize the chance that we get a response. Some
  1415. * badly-behaved APs don't respond when this parameter is included.
  1416. */
  1417. chandef.width = sdata->vif.bss_conf.chandef.width;
  1418. if (flags & IEEE80211_PROBE_FLAG_DIRECTED)
  1419. chandef.chan = NULL;
  1420. else
  1421. chandef.chan = chan;
  1422. skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
  1423. 100 + ie_len);
  1424. if (!skb)
  1425. return NULL;
  1426. rate_masks[chan->band] = ratemask;
  1427. ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
  1428. skb_tailroom(skb), &dummy_ie_desc,
  1429. ie, ie_len, BIT(chan->band),
  1430. rate_masks, &chandef, flags);
  1431. skb_put(skb, ies_len);
  1432. if (dst) {
  1433. mgmt = (struct ieee80211_mgmt *) skb->data;
  1434. memcpy(mgmt->da, dst, ETH_ALEN);
  1435. memcpy(mgmt->bssid, dst, ETH_ALEN);
  1436. }
  1437. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1438. return skb;
  1439. }
  1440. u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
  1441. struct ieee802_11_elems *elems,
  1442. enum nl80211_band band, u32 *basic_rates)
  1443. {
  1444. struct ieee80211_supported_band *sband;
  1445. size_t num_rates;
  1446. u32 supp_rates, rate_flags;
  1447. int i, j, shift;
  1448. sband = sdata->local->hw.wiphy->bands[band];
  1449. if (WARN_ON(!sband))
  1450. return 1;
  1451. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  1452. shift = ieee80211_vif_get_shift(&sdata->vif);
  1453. num_rates = sband->n_bitrates;
  1454. supp_rates = 0;
  1455. for (i = 0; i < elems->supp_rates_len +
  1456. elems->ext_supp_rates_len; i++) {
  1457. u8 rate = 0;
  1458. int own_rate;
  1459. bool is_basic;
  1460. if (i < elems->supp_rates_len)
  1461. rate = elems->supp_rates[i];
  1462. else if (elems->ext_supp_rates)
  1463. rate = elems->ext_supp_rates
  1464. [i - elems->supp_rates_len];
  1465. own_rate = 5 * (rate & 0x7f);
  1466. is_basic = !!(rate & 0x80);
  1467. if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1468. continue;
  1469. for (j = 0; j < num_rates; j++) {
  1470. int brate;
  1471. if ((rate_flags & sband->bitrates[j].flags)
  1472. != rate_flags)
  1473. continue;
  1474. brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
  1475. 1 << shift);
  1476. if (brate == own_rate) {
  1477. supp_rates |= BIT(j);
  1478. if (basic_rates && is_basic)
  1479. *basic_rates |= BIT(j);
  1480. }
  1481. }
  1482. }
  1483. return supp_rates;
  1484. }
  1485. void ieee80211_stop_device(struct ieee80211_local *local)
  1486. {
  1487. ieee80211_led_radio(local, false);
  1488. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  1489. cancel_work_sync(&local->reconfig_filter);
  1490. flush_workqueue(local->workqueue);
  1491. drv_stop(local);
  1492. }
  1493. static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
  1494. bool aborted)
  1495. {
  1496. /* It's possible that we don't handle the scan completion in
  1497. * time during suspend, so if it's still marked as completed
  1498. * here, queue the work and flush it to clean things up.
  1499. * Instead of calling the worker function directly here, we
  1500. * really queue it to avoid potential races with other flows
  1501. * scheduling the same work.
  1502. */
  1503. if (test_bit(SCAN_COMPLETED, &local->scanning)) {
  1504. /* If coming from reconfiguration failure, abort the scan so
  1505. * we don't attempt to continue a partial HW scan - which is
  1506. * possible otherwise if (e.g.) the 2.4 GHz portion was the
  1507. * completed scan, and a 5 GHz portion is still pending.
  1508. */
  1509. if (aborted)
  1510. set_bit(SCAN_ABORTED, &local->scanning);
  1511. ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
  1512. flush_delayed_work(&local->scan_work);
  1513. }
  1514. }
  1515. static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
  1516. {
  1517. struct ieee80211_sub_if_data *sdata;
  1518. struct ieee80211_chanctx *ctx;
  1519. /*
  1520. * We get here if during resume the device can't be restarted properly.
  1521. * We might also get here if this happens during HW reset, which is a
  1522. * slightly different situation and we need to drop all connections in
  1523. * the latter case.
  1524. *
  1525. * Ask cfg80211 to turn off all interfaces, this will result in more
  1526. * warnings but at least we'll then get into a clean stopped state.
  1527. */
  1528. local->resuming = false;
  1529. local->suspended = false;
  1530. local->in_reconfig = false;
  1531. ieee80211_flush_completed_scan(local, true);
  1532. /* scheduled scan clearly can't be running any more, but tell
  1533. * cfg80211 and clear local state
  1534. */
  1535. ieee80211_sched_scan_end(local);
  1536. list_for_each_entry(sdata, &local->interfaces, list)
  1537. sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
  1538. /* Mark channel contexts as not being in the driver any more to avoid
  1539. * removing them from the driver during the shutdown process...
  1540. */
  1541. mutex_lock(&local->chanctx_mtx);
  1542. list_for_each_entry(ctx, &local->chanctx_list, list)
  1543. ctx->driver_present = false;
  1544. mutex_unlock(&local->chanctx_mtx);
  1545. cfg80211_shutdown_all_interfaces(local->hw.wiphy);
  1546. }
  1547. static void ieee80211_assign_chanctx(struct ieee80211_local *local,
  1548. struct ieee80211_sub_if_data *sdata)
  1549. {
  1550. struct ieee80211_chanctx_conf *conf;
  1551. struct ieee80211_chanctx *ctx;
  1552. if (!local->use_chanctx)
  1553. return;
  1554. mutex_lock(&local->chanctx_mtx);
  1555. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1556. lockdep_is_held(&local->chanctx_mtx));
  1557. if (conf) {
  1558. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1559. drv_assign_vif_chanctx(local, sdata, ctx);
  1560. }
  1561. mutex_unlock(&local->chanctx_mtx);
  1562. }
  1563. static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
  1564. {
  1565. struct ieee80211_local *local = sdata->local;
  1566. struct sta_info *sta;
  1567. /* add STAs back */
  1568. mutex_lock(&local->sta_mtx);
  1569. list_for_each_entry(sta, &local->sta_list, list) {
  1570. enum ieee80211_sta_state state;
  1571. if (!sta->uploaded || sta->sdata != sdata)
  1572. continue;
  1573. for (state = IEEE80211_STA_NOTEXIST;
  1574. state < sta->sta_state; state++)
  1575. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1576. state + 1));
  1577. }
  1578. mutex_unlock(&local->sta_mtx);
  1579. }
  1580. static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata)
  1581. {
  1582. struct cfg80211_nan_func *func, **funcs;
  1583. int res, id, i = 0;
  1584. res = drv_start_nan(sdata->local, sdata,
  1585. &sdata->u.nan.conf);
  1586. if (WARN_ON(res))
  1587. return res;
  1588. funcs = kcalloc(sdata->local->hw.max_nan_de_entries + 1,
  1589. sizeof(*funcs),
  1590. GFP_KERNEL);
  1591. if (!funcs)
  1592. return -ENOMEM;
  1593. /* Add all the functions:
  1594. * This is a little bit ugly. We need to call a potentially sleeping
  1595. * callback for each NAN function, so we can't hold the spinlock.
  1596. */
  1597. spin_lock_bh(&sdata->u.nan.func_lock);
  1598. idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id)
  1599. funcs[i++] = func;
  1600. spin_unlock_bh(&sdata->u.nan.func_lock);
  1601. for (i = 0; funcs[i]; i++) {
  1602. res = drv_add_nan_func(sdata->local, sdata, funcs[i]);
  1603. if (WARN_ON(res))
  1604. ieee80211_nan_func_terminated(&sdata->vif,
  1605. funcs[i]->instance_id,
  1606. NL80211_NAN_FUNC_TERM_REASON_ERROR,
  1607. GFP_KERNEL);
  1608. }
  1609. kfree(funcs);
  1610. return 0;
  1611. }
  1612. int ieee80211_reconfig(struct ieee80211_local *local)
  1613. {
  1614. struct ieee80211_hw *hw = &local->hw;
  1615. struct ieee80211_sub_if_data *sdata;
  1616. struct ieee80211_chanctx *ctx;
  1617. struct sta_info *sta;
  1618. int res, i;
  1619. bool reconfig_due_to_wowlan = false;
  1620. struct ieee80211_sub_if_data *sched_scan_sdata;
  1621. struct cfg80211_sched_scan_request *sched_scan_req;
  1622. bool sched_scan_stopped = false;
  1623. bool suspended = local->suspended;
  1624. /* nothing to do if HW shouldn't run */
  1625. if (!local->open_count)
  1626. goto wake_up;
  1627. #ifdef CONFIG_PM
  1628. if (suspended)
  1629. local->resuming = true;
  1630. if (local->wowlan) {
  1631. /*
  1632. * In the wowlan case, both mac80211 and the device
  1633. * are functional when the resume op is called, so
  1634. * clear local->suspended so the device could operate
  1635. * normally (e.g. pass rx frames).
  1636. */
  1637. local->suspended = false;
  1638. res = drv_resume(local);
  1639. local->wowlan = false;
  1640. if (res < 0) {
  1641. local->resuming = false;
  1642. return res;
  1643. }
  1644. if (res == 0)
  1645. goto wake_up;
  1646. WARN_ON(res > 1);
  1647. /*
  1648. * res is 1, which means the driver requested
  1649. * to go through a regular reset on wakeup.
  1650. * restore local->suspended in this case.
  1651. */
  1652. reconfig_due_to_wowlan = true;
  1653. local->suspended = true;
  1654. }
  1655. #endif
  1656. /*
  1657. * In case of hw_restart during suspend (without wowlan),
  1658. * cancel restart work, as we are reconfiguring the device
  1659. * anyway.
  1660. * Note that restart_work is scheduled on a frozen workqueue,
  1661. * so we can't deadlock in this case.
  1662. */
  1663. if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
  1664. cancel_work_sync(&local->restart_work);
  1665. local->started = false;
  1666. /*
  1667. * Upon resume hardware can sometimes be goofy due to
  1668. * various platform / driver / bus issues, so restarting
  1669. * the device may at times not work immediately. Propagate
  1670. * the error.
  1671. */
  1672. res = drv_start(local);
  1673. if (res) {
  1674. if (suspended)
  1675. WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
  1676. else
  1677. WARN(1, "Hardware became unavailable during restart.\n");
  1678. ieee80211_handle_reconfig_failure(local);
  1679. return res;
  1680. }
  1681. /* setup fragmentation threshold */
  1682. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  1683. /* setup RTS threshold */
  1684. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  1685. /* reset coverage class */
  1686. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  1687. ieee80211_led_radio(local, true);
  1688. ieee80211_mod_tpt_led_trig(local,
  1689. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  1690. /* add interfaces */
  1691. sdata = rtnl_dereference(local->monitor_sdata);
  1692. if (sdata) {
  1693. /* in HW restart it exists already */
  1694. WARN_ON(local->resuming);
  1695. res = drv_add_interface(local, sdata);
  1696. if (WARN_ON(res)) {
  1697. RCU_INIT_POINTER(local->monitor_sdata, NULL);
  1698. synchronize_net();
  1699. kfree(sdata);
  1700. }
  1701. }
  1702. list_for_each_entry(sdata, &local->interfaces, list) {
  1703. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1704. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1705. ieee80211_sdata_running(sdata)) {
  1706. res = drv_add_interface(local, sdata);
  1707. if (WARN_ON(res))
  1708. break;
  1709. }
  1710. }
  1711. /* If adding any of the interfaces failed above, roll back and
  1712. * report failure.
  1713. */
  1714. if (res) {
  1715. list_for_each_entry_continue_reverse(sdata, &local->interfaces,
  1716. list)
  1717. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1718. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1719. ieee80211_sdata_running(sdata))
  1720. drv_remove_interface(local, sdata);
  1721. ieee80211_handle_reconfig_failure(local);
  1722. return res;
  1723. }
  1724. /* add channel contexts */
  1725. if (local->use_chanctx) {
  1726. mutex_lock(&local->chanctx_mtx);
  1727. list_for_each_entry(ctx, &local->chanctx_list, list)
  1728. if (ctx->replace_state !=
  1729. IEEE80211_CHANCTX_REPLACES_OTHER)
  1730. WARN_ON(drv_add_chanctx(local, ctx));
  1731. mutex_unlock(&local->chanctx_mtx);
  1732. sdata = rtnl_dereference(local->monitor_sdata);
  1733. if (sdata && ieee80211_sdata_running(sdata))
  1734. ieee80211_assign_chanctx(local, sdata);
  1735. }
  1736. /* reconfigure hardware */
  1737. ieee80211_hw_config(local, ~0);
  1738. ieee80211_configure_filter(local);
  1739. /* Finally also reconfigure all the BSS information */
  1740. list_for_each_entry(sdata, &local->interfaces, list) {
  1741. u32 changed;
  1742. if (!ieee80211_sdata_running(sdata))
  1743. continue;
  1744. ieee80211_assign_chanctx(local, sdata);
  1745. switch (sdata->vif.type) {
  1746. case NL80211_IFTYPE_AP_VLAN:
  1747. case NL80211_IFTYPE_MONITOR:
  1748. break;
  1749. default:
  1750. ieee80211_reconfig_stations(sdata);
  1751. /* fall through */
  1752. case NL80211_IFTYPE_AP: /* AP stations are handled later */
  1753. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1754. drv_conf_tx(local, sdata, i,
  1755. &sdata->tx_conf[i]);
  1756. break;
  1757. }
  1758. /* common change flags for all interface types */
  1759. changed = BSS_CHANGED_ERP_CTS_PROT |
  1760. BSS_CHANGED_ERP_PREAMBLE |
  1761. BSS_CHANGED_ERP_SLOT |
  1762. BSS_CHANGED_HT |
  1763. BSS_CHANGED_BASIC_RATES |
  1764. BSS_CHANGED_BEACON_INT |
  1765. BSS_CHANGED_BSSID |
  1766. BSS_CHANGED_CQM |
  1767. BSS_CHANGED_QOS |
  1768. BSS_CHANGED_IDLE |
  1769. BSS_CHANGED_TXPOWER |
  1770. BSS_CHANGED_MCAST_RATE;
  1771. if (sdata->vif.mu_mimo_owner)
  1772. changed |= BSS_CHANGED_MU_GROUPS;
  1773. switch (sdata->vif.type) {
  1774. case NL80211_IFTYPE_STATION:
  1775. changed |= BSS_CHANGED_ASSOC |
  1776. BSS_CHANGED_ARP_FILTER |
  1777. BSS_CHANGED_PS;
  1778. /* Re-send beacon info report to the driver */
  1779. if (sdata->u.mgd.have_beacon)
  1780. changed |= BSS_CHANGED_BEACON_INFO;
  1781. if (sdata->vif.bss_conf.max_idle_period ||
  1782. sdata->vif.bss_conf.protected_keep_alive)
  1783. changed |= BSS_CHANGED_KEEP_ALIVE;
  1784. sdata_lock(sdata);
  1785. ieee80211_bss_info_change_notify(sdata, changed);
  1786. sdata_unlock(sdata);
  1787. break;
  1788. case NL80211_IFTYPE_OCB:
  1789. changed |= BSS_CHANGED_OCB;
  1790. ieee80211_bss_info_change_notify(sdata, changed);
  1791. break;
  1792. case NL80211_IFTYPE_ADHOC:
  1793. changed |= BSS_CHANGED_IBSS;
  1794. /* fall through */
  1795. case NL80211_IFTYPE_AP:
  1796. changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
  1797. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1798. changed |= BSS_CHANGED_AP_PROBE_RESP;
  1799. if (rcu_access_pointer(sdata->u.ap.beacon))
  1800. drv_start_ap(local, sdata);
  1801. }
  1802. /* fall through */
  1803. case NL80211_IFTYPE_MESH_POINT:
  1804. if (sdata->vif.bss_conf.enable_beacon) {
  1805. changed |= BSS_CHANGED_BEACON |
  1806. BSS_CHANGED_BEACON_ENABLED;
  1807. ieee80211_bss_info_change_notify(sdata, changed);
  1808. }
  1809. break;
  1810. case NL80211_IFTYPE_NAN:
  1811. res = ieee80211_reconfig_nan(sdata);
  1812. if (res < 0) {
  1813. ieee80211_handle_reconfig_failure(local);
  1814. return res;
  1815. }
  1816. break;
  1817. case NL80211_IFTYPE_WDS:
  1818. case NL80211_IFTYPE_AP_VLAN:
  1819. case NL80211_IFTYPE_MONITOR:
  1820. case NL80211_IFTYPE_P2P_DEVICE:
  1821. /* nothing to do */
  1822. break;
  1823. case NL80211_IFTYPE_UNSPECIFIED:
  1824. case NUM_NL80211_IFTYPES:
  1825. case NL80211_IFTYPE_P2P_CLIENT:
  1826. case NL80211_IFTYPE_P2P_GO:
  1827. WARN_ON(1);
  1828. break;
  1829. }
  1830. }
  1831. ieee80211_recalc_ps(local);
  1832. /*
  1833. * The sta might be in psm against the ap (e.g. because
  1834. * this was the state before a hw restart), so we
  1835. * explicitly send a null packet in order to make sure
  1836. * it'll sync against the ap (and get out of psm).
  1837. */
  1838. if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
  1839. list_for_each_entry(sdata, &local->interfaces, list) {
  1840. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1841. continue;
  1842. if (!sdata->u.mgd.associated)
  1843. continue;
  1844. ieee80211_send_nullfunc(local, sdata, false);
  1845. }
  1846. }
  1847. /* APs are now beaconing, add back stations */
  1848. mutex_lock(&local->sta_mtx);
  1849. list_for_each_entry(sta, &local->sta_list, list) {
  1850. enum ieee80211_sta_state state;
  1851. if (!sta->uploaded)
  1852. continue;
  1853. if (sta->sdata->vif.type != NL80211_IFTYPE_AP &&
  1854. sta->sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
  1855. continue;
  1856. for (state = IEEE80211_STA_NOTEXIST;
  1857. state < sta->sta_state; state++)
  1858. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1859. state + 1));
  1860. }
  1861. mutex_unlock(&local->sta_mtx);
  1862. /* add back keys */
  1863. list_for_each_entry(sdata, &local->interfaces, list)
  1864. ieee80211_reset_crypto_tx_tailroom(sdata);
  1865. list_for_each_entry(sdata, &local->interfaces, list)
  1866. if (ieee80211_sdata_running(sdata))
  1867. ieee80211_enable_keys(sdata);
  1868. /* Reconfigure sched scan if it was interrupted by FW restart */
  1869. mutex_lock(&local->mtx);
  1870. sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
  1871. lockdep_is_held(&local->mtx));
  1872. sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
  1873. lockdep_is_held(&local->mtx));
  1874. if (sched_scan_sdata && sched_scan_req)
  1875. /*
  1876. * Sched scan stopped, but we don't want to report it. Instead,
  1877. * we're trying to reschedule. However, if more than one scan
  1878. * plan was set, we cannot reschedule since we don't know which
  1879. * scan plan was currently running (and some scan plans may have
  1880. * already finished).
  1881. */
  1882. if (sched_scan_req->n_scan_plans > 1 ||
  1883. __ieee80211_request_sched_scan_start(sched_scan_sdata,
  1884. sched_scan_req)) {
  1885. RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
  1886. RCU_INIT_POINTER(local->sched_scan_req, NULL);
  1887. sched_scan_stopped = true;
  1888. }
  1889. mutex_unlock(&local->mtx);
  1890. if (sched_scan_stopped)
  1891. cfg80211_sched_scan_stopped_rtnl(local->hw.wiphy, 0);
  1892. wake_up:
  1893. if (local->monitors == local->open_count && local->monitors > 0)
  1894. ieee80211_add_virtual_monitor(local);
  1895. /*
  1896. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  1897. * sessions can be established after a resume.
  1898. *
  1899. * Also tear down aggregation sessions since reconfiguring
  1900. * them in a hardware restart scenario is not easily done
  1901. * right now, and the hardware will have lost information
  1902. * about the sessions, but we and the AP still think they
  1903. * are active. This is really a workaround though.
  1904. */
  1905. if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
  1906. mutex_lock(&local->sta_mtx);
  1907. list_for_each_entry(sta, &local->sta_list, list) {
  1908. if (!local->resuming)
  1909. ieee80211_sta_tear_down_BA_sessions(
  1910. sta, AGG_STOP_LOCAL_REQUEST);
  1911. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1912. }
  1913. mutex_unlock(&local->sta_mtx);
  1914. }
  1915. if (local->in_reconfig) {
  1916. local->in_reconfig = false;
  1917. barrier();
  1918. /* Restart deferred ROCs */
  1919. mutex_lock(&local->mtx);
  1920. ieee80211_start_next_roc(local);
  1921. mutex_unlock(&local->mtx);
  1922. }
  1923. ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  1924. IEEE80211_QUEUE_STOP_REASON_SUSPEND,
  1925. false);
  1926. /*
  1927. * If this is for hw restart things are still running.
  1928. * We may want to change that later, however.
  1929. */
  1930. if (local->open_count && (!suspended || reconfig_due_to_wowlan))
  1931. drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
  1932. if (!suspended)
  1933. return 0;
  1934. #ifdef CONFIG_PM
  1935. /* first set suspended false, then resuming */
  1936. local->suspended = false;
  1937. mb();
  1938. local->resuming = false;
  1939. ieee80211_flush_completed_scan(local, false);
  1940. if (local->open_count && !reconfig_due_to_wowlan)
  1941. drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
  1942. list_for_each_entry(sdata, &local->interfaces, list) {
  1943. if (!ieee80211_sdata_running(sdata))
  1944. continue;
  1945. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  1946. ieee80211_sta_restart(sdata);
  1947. }
  1948. mod_timer(&local->sta_cleanup, jiffies + 1);
  1949. #else
  1950. WARN_ON(1);
  1951. #endif
  1952. return 0;
  1953. }
  1954. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  1955. {
  1956. struct ieee80211_sub_if_data *sdata;
  1957. struct ieee80211_local *local;
  1958. struct ieee80211_key *key;
  1959. if (WARN_ON(!vif))
  1960. return;
  1961. sdata = vif_to_sdata(vif);
  1962. local = sdata->local;
  1963. if (WARN_ON(!local->resuming))
  1964. return;
  1965. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1966. return;
  1967. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1968. mutex_lock(&local->key_mtx);
  1969. list_for_each_entry(key, &sdata->key_list, list)
  1970. key->flags |= KEY_FLAG_TAINTED;
  1971. mutex_unlock(&local->key_mtx);
  1972. }
  1973. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1974. void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
  1975. {
  1976. struct ieee80211_local *local = sdata->local;
  1977. struct ieee80211_chanctx_conf *chanctx_conf;
  1978. struct ieee80211_chanctx *chanctx;
  1979. mutex_lock(&local->chanctx_mtx);
  1980. chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1981. lockdep_is_held(&local->chanctx_mtx));
  1982. /*
  1983. * This function can be called from a work, thus it may be possible
  1984. * that the chanctx_conf is removed (due to a disconnection, for
  1985. * example).
  1986. * So nothing should be done in such case.
  1987. */
  1988. if (!chanctx_conf)
  1989. goto unlock;
  1990. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
  1991. ieee80211_recalc_smps_chanctx(local, chanctx);
  1992. unlock:
  1993. mutex_unlock(&local->chanctx_mtx);
  1994. }
  1995. void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata)
  1996. {
  1997. struct ieee80211_local *local = sdata->local;
  1998. struct ieee80211_chanctx_conf *chanctx_conf;
  1999. struct ieee80211_chanctx *chanctx;
  2000. mutex_lock(&local->chanctx_mtx);
  2001. chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  2002. lockdep_is_held(&local->chanctx_mtx));
  2003. if (WARN_ON_ONCE(!chanctx_conf))
  2004. goto unlock;
  2005. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
  2006. ieee80211_recalc_chanctx_min_def(local, chanctx);
  2007. unlock:
  2008. mutex_unlock(&local->chanctx_mtx);
  2009. }
  2010. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  2011. {
  2012. size_t pos = offset;
  2013. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  2014. pos += 2 + ies[pos + 1];
  2015. return pos;
  2016. }
  2017. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  2018. int rssi_min_thold,
  2019. int rssi_max_thold)
  2020. {
  2021. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  2022. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  2023. return;
  2024. /*
  2025. * Scale up threshold values before storing it, as the RSSI averaging
  2026. * algorithm uses a scaled up value as well. Change this scaling
  2027. * factor if the RSSI averaging algorithm changes.
  2028. */
  2029. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  2030. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  2031. }
  2032. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  2033. int rssi_min_thold,
  2034. int rssi_max_thold)
  2035. {
  2036. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2037. WARN_ON(rssi_min_thold == rssi_max_thold ||
  2038. rssi_min_thold > rssi_max_thold);
  2039. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  2040. rssi_max_thold);
  2041. }
  2042. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  2043. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  2044. {
  2045. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2046. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  2047. }
  2048. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  2049. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  2050. u16 cap)
  2051. {
  2052. __le16 tmp;
  2053. *pos++ = WLAN_EID_HT_CAPABILITY;
  2054. *pos++ = sizeof(struct ieee80211_ht_cap);
  2055. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  2056. /* capability flags */
  2057. tmp = cpu_to_le16(cap);
  2058. memcpy(pos, &tmp, sizeof(u16));
  2059. pos += sizeof(u16);
  2060. /* AMPDU parameters */
  2061. *pos++ = ht_cap->ampdu_factor |
  2062. (ht_cap->ampdu_density <<
  2063. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  2064. /* MCS set */
  2065. memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
  2066. pos += sizeof(ht_cap->mcs);
  2067. /* extended capabilities */
  2068. pos += sizeof(__le16);
  2069. /* BF capabilities */
  2070. pos += sizeof(__le32);
  2071. /* antenna selection */
  2072. pos += sizeof(u8);
  2073. return pos;
  2074. }
  2075. u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  2076. u32 cap)
  2077. {
  2078. __le32 tmp;
  2079. *pos++ = WLAN_EID_VHT_CAPABILITY;
  2080. *pos++ = sizeof(struct ieee80211_vht_cap);
  2081. memset(pos, 0, sizeof(struct ieee80211_vht_cap));
  2082. /* capability flags */
  2083. tmp = cpu_to_le32(cap);
  2084. memcpy(pos, &tmp, sizeof(u32));
  2085. pos += sizeof(u32);
  2086. /* VHT MCS set */
  2087. memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
  2088. pos += sizeof(vht_cap->vht_mcs);
  2089. return pos;
  2090. }
  2091. u8 *ieee80211_ie_build_he_cap(u8 *pos,
  2092. const struct ieee80211_sta_he_cap *he_cap,
  2093. u8 *end)
  2094. {
  2095. u8 n;
  2096. u8 ie_len;
  2097. u8 *orig_pos = pos;
  2098. /* Make sure we have place for the IE */
  2099. /*
  2100. * TODO: the 1 added is because this temporarily is under the EXTENSION
  2101. * IE. Get rid of it when it moves.
  2102. */
  2103. if (!he_cap)
  2104. return orig_pos;
  2105. n = ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem);
  2106. ie_len = 2 + 1 +
  2107. sizeof(he_cap->he_cap_elem) + n +
  2108. ieee80211_he_ppe_size(he_cap->ppe_thres[0],
  2109. he_cap->he_cap_elem.phy_cap_info);
  2110. if ((end - pos) < ie_len)
  2111. return orig_pos;
  2112. *pos++ = WLAN_EID_EXTENSION;
  2113. pos++; /* We'll set the size later below */
  2114. *pos++ = WLAN_EID_EXT_HE_CAPABILITY;
  2115. /* Fixed data */
  2116. memcpy(pos, &he_cap->he_cap_elem, sizeof(he_cap->he_cap_elem));
  2117. pos += sizeof(he_cap->he_cap_elem);
  2118. memcpy(pos, &he_cap->he_mcs_nss_supp, n);
  2119. pos += n;
  2120. /* Check if PPE Threshold should be present */
  2121. if ((he_cap->he_cap_elem.phy_cap_info[6] &
  2122. IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) == 0)
  2123. goto end;
  2124. /*
  2125. * Calculate how many PPET16/PPET8 pairs are to come. Algorithm:
  2126. * (NSS_M1 + 1) x (num of 1 bits in RU_INDEX_BITMASK)
  2127. */
  2128. n = hweight8(he_cap->ppe_thres[0] &
  2129. IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
  2130. n *= (1 + ((he_cap->ppe_thres[0] & IEEE80211_PPE_THRES_NSS_MASK) >>
  2131. IEEE80211_PPE_THRES_NSS_POS));
  2132. /*
  2133. * Each pair is 6 bits, and we need to add the 7 "header" bits to the
  2134. * total size.
  2135. */
  2136. n = (n * IEEE80211_PPE_THRES_INFO_PPET_SIZE * 2) + 7;
  2137. n = DIV_ROUND_UP(n, 8);
  2138. /* Copy PPE Thresholds */
  2139. memcpy(pos, &he_cap->ppe_thres, n);
  2140. pos += n;
  2141. end:
  2142. orig_pos[1] = (pos - orig_pos) - 2;
  2143. return pos;
  2144. }
  2145. u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  2146. const struct cfg80211_chan_def *chandef,
  2147. u16 prot_mode, bool rifs_mode)
  2148. {
  2149. struct ieee80211_ht_operation *ht_oper;
  2150. /* Build HT Information */
  2151. *pos++ = WLAN_EID_HT_OPERATION;
  2152. *pos++ = sizeof(struct ieee80211_ht_operation);
  2153. ht_oper = (struct ieee80211_ht_operation *)pos;
  2154. ht_oper->primary_chan = ieee80211_frequency_to_channel(
  2155. chandef->chan->center_freq);
  2156. switch (chandef->width) {
  2157. case NL80211_CHAN_WIDTH_160:
  2158. case NL80211_CHAN_WIDTH_80P80:
  2159. case NL80211_CHAN_WIDTH_80:
  2160. case NL80211_CHAN_WIDTH_40:
  2161. if (chandef->center_freq1 > chandef->chan->center_freq)
  2162. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  2163. else
  2164. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  2165. break;
  2166. default:
  2167. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  2168. break;
  2169. }
  2170. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
  2171. chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
  2172. chandef->width != NL80211_CHAN_WIDTH_20)
  2173. ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  2174. if (rifs_mode)
  2175. ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
  2176. ht_oper->operation_mode = cpu_to_le16(prot_mode);
  2177. ht_oper->stbc_param = 0x0000;
  2178. /* It seems that Basic MCS set and Supported MCS set
  2179. are identical for the first 10 bytes */
  2180. memset(&ht_oper->basic_set, 0, 16);
  2181. memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
  2182. return pos + sizeof(struct ieee80211_ht_operation);
  2183. }
  2184. void ieee80211_ie_build_wide_bw_cs(u8 *pos,
  2185. const struct cfg80211_chan_def *chandef)
  2186. {
  2187. *pos++ = WLAN_EID_WIDE_BW_CHANNEL_SWITCH; /* EID */
  2188. *pos++ = 3; /* IE length */
  2189. /* New channel width */
  2190. switch (chandef->width) {
  2191. case NL80211_CHAN_WIDTH_80:
  2192. *pos++ = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2193. break;
  2194. case NL80211_CHAN_WIDTH_160:
  2195. *pos++ = IEEE80211_VHT_CHANWIDTH_160MHZ;
  2196. break;
  2197. case NL80211_CHAN_WIDTH_80P80:
  2198. *pos++ = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
  2199. break;
  2200. default:
  2201. *pos++ = IEEE80211_VHT_CHANWIDTH_USE_HT;
  2202. }
  2203. /* new center frequency segment 0 */
  2204. *pos++ = ieee80211_frequency_to_channel(chandef->center_freq1);
  2205. /* new center frequency segment 1 */
  2206. if (chandef->center_freq2)
  2207. *pos++ = ieee80211_frequency_to_channel(chandef->center_freq2);
  2208. else
  2209. *pos++ = 0;
  2210. }
  2211. u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  2212. const struct cfg80211_chan_def *chandef)
  2213. {
  2214. struct ieee80211_vht_operation *vht_oper;
  2215. *pos++ = WLAN_EID_VHT_OPERATION;
  2216. *pos++ = sizeof(struct ieee80211_vht_operation);
  2217. vht_oper = (struct ieee80211_vht_operation *)pos;
  2218. vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
  2219. chandef->center_freq1);
  2220. if (chandef->center_freq2)
  2221. vht_oper->center_freq_seg1_idx =
  2222. ieee80211_frequency_to_channel(chandef->center_freq2);
  2223. else
  2224. vht_oper->center_freq_seg1_idx = 0x00;
  2225. switch (chandef->width) {
  2226. case NL80211_CHAN_WIDTH_160:
  2227. /*
  2228. * Convert 160 MHz channel width to new style as interop
  2229. * workaround.
  2230. */
  2231. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2232. vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
  2233. if (chandef->chan->center_freq < chandef->center_freq1)
  2234. vht_oper->center_freq_seg0_idx -= 8;
  2235. else
  2236. vht_oper->center_freq_seg0_idx += 8;
  2237. break;
  2238. case NL80211_CHAN_WIDTH_80P80:
  2239. /*
  2240. * Convert 80+80 MHz channel width to new style as interop
  2241. * workaround.
  2242. */
  2243. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2244. break;
  2245. case NL80211_CHAN_WIDTH_80:
  2246. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2247. break;
  2248. default:
  2249. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
  2250. break;
  2251. }
  2252. /* don't require special VHT peer rates */
  2253. vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
  2254. return pos + sizeof(struct ieee80211_vht_operation);
  2255. }
  2256. bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
  2257. struct cfg80211_chan_def *chandef)
  2258. {
  2259. enum nl80211_channel_type channel_type;
  2260. if (!ht_oper)
  2261. return false;
  2262. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  2263. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  2264. channel_type = NL80211_CHAN_HT20;
  2265. break;
  2266. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  2267. channel_type = NL80211_CHAN_HT40PLUS;
  2268. break;
  2269. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  2270. channel_type = NL80211_CHAN_HT40MINUS;
  2271. break;
  2272. default:
  2273. channel_type = NL80211_CHAN_NO_HT;
  2274. return false;
  2275. }
  2276. cfg80211_chandef_create(chandef, chandef->chan, channel_type);
  2277. return true;
  2278. }
  2279. bool ieee80211_chandef_vht_oper(const struct ieee80211_vht_operation *oper,
  2280. struct cfg80211_chan_def *chandef)
  2281. {
  2282. struct cfg80211_chan_def new = *chandef;
  2283. int cf1, cf2;
  2284. if (!oper)
  2285. return false;
  2286. cf1 = ieee80211_channel_to_frequency(oper->center_freq_seg0_idx,
  2287. chandef->chan->band);
  2288. cf2 = ieee80211_channel_to_frequency(oper->center_freq_seg1_idx,
  2289. chandef->chan->band);
  2290. switch (oper->chan_width) {
  2291. case IEEE80211_VHT_CHANWIDTH_USE_HT:
  2292. break;
  2293. case IEEE80211_VHT_CHANWIDTH_80MHZ:
  2294. new.width = NL80211_CHAN_WIDTH_80;
  2295. new.center_freq1 = cf1;
  2296. /* If needed, adjust based on the newer interop workaround. */
  2297. if (oper->center_freq_seg1_idx) {
  2298. unsigned int diff;
  2299. diff = abs(oper->center_freq_seg1_idx -
  2300. oper->center_freq_seg0_idx);
  2301. if (diff == 8) {
  2302. new.width = NL80211_CHAN_WIDTH_160;
  2303. new.center_freq1 = cf2;
  2304. } else if (diff > 8) {
  2305. new.width = NL80211_CHAN_WIDTH_80P80;
  2306. new.center_freq2 = cf2;
  2307. }
  2308. }
  2309. break;
  2310. case IEEE80211_VHT_CHANWIDTH_160MHZ:
  2311. new.width = NL80211_CHAN_WIDTH_160;
  2312. new.center_freq1 = cf1;
  2313. break;
  2314. case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
  2315. new.width = NL80211_CHAN_WIDTH_80P80;
  2316. new.center_freq1 = cf1;
  2317. new.center_freq2 = cf2;
  2318. break;
  2319. default:
  2320. return false;
  2321. }
  2322. if (!cfg80211_chandef_valid(&new))
  2323. return false;
  2324. *chandef = new;
  2325. return true;
  2326. }
  2327. int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
  2328. const struct ieee80211_supported_band *sband,
  2329. const u8 *srates, int srates_len, u32 *rates)
  2330. {
  2331. u32 rate_flags = ieee80211_chandef_rate_flags(chandef);
  2332. int shift = ieee80211_chandef_get_shift(chandef);
  2333. struct ieee80211_rate *br;
  2334. int brate, rate, i, j, count = 0;
  2335. *rates = 0;
  2336. for (i = 0; i < srates_len; i++) {
  2337. rate = srates[i] & 0x7f;
  2338. for (j = 0; j < sband->n_bitrates; j++) {
  2339. br = &sband->bitrates[j];
  2340. if ((rate_flags & br->flags) != rate_flags)
  2341. continue;
  2342. brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
  2343. if (brate == rate) {
  2344. *rates |= BIT(j);
  2345. count++;
  2346. break;
  2347. }
  2348. }
  2349. }
  2350. return count;
  2351. }
  2352. int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
  2353. struct sk_buff *skb, bool need_basic,
  2354. enum nl80211_band band)
  2355. {
  2356. struct ieee80211_local *local = sdata->local;
  2357. struct ieee80211_supported_band *sband;
  2358. int rate, shift;
  2359. u8 i, rates, *pos;
  2360. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  2361. u32 rate_flags;
  2362. shift = ieee80211_vif_get_shift(&sdata->vif);
  2363. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  2364. sband = local->hw.wiphy->bands[band];
  2365. rates = 0;
  2366. for (i = 0; i < sband->n_bitrates; i++) {
  2367. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  2368. continue;
  2369. rates++;
  2370. }
  2371. if (rates > 8)
  2372. rates = 8;
  2373. if (skb_tailroom(skb) < rates + 2)
  2374. return -ENOMEM;
  2375. pos = skb_put(skb, rates + 2);
  2376. *pos++ = WLAN_EID_SUPP_RATES;
  2377. *pos++ = rates;
  2378. for (i = 0; i < rates; i++) {
  2379. u8 basic = 0;
  2380. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  2381. continue;
  2382. if (need_basic && basic_rates & BIT(i))
  2383. basic = 0x80;
  2384. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  2385. 5 * (1 << shift));
  2386. *pos++ = basic | (u8) rate;
  2387. }
  2388. return 0;
  2389. }
  2390. int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
  2391. struct sk_buff *skb, bool need_basic,
  2392. enum nl80211_band band)
  2393. {
  2394. struct ieee80211_local *local = sdata->local;
  2395. struct ieee80211_supported_band *sband;
  2396. int rate, shift;
  2397. u8 i, exrates, *pos;
  2398. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  2399. u32 rate_flags;
  2400. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  2401. shift = ieee80211_vif_get_shift(&sdata->vif);
  2402. sband = local->hw.wiphy->bands[band];
  2403. exrates = 0;
  2404. for (i = 0; i < sband->n_bitrates; i++) {
  2405. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  2406. continue;
  2407. exrates++;
  2408. }
  2409. if (exrates > 8)
  2410. exrates -= 8;
  2411. else
  2412. exrates = 0;
  2413. if (skb_tailroom(skb) < exrates + 2)
  2414. return -ENOMEM;
  2415. if (exrates) {
  2416. pos = skb_put(skb, exrates + 2);
  2417. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  2418. *pos++ = exrates;
  2419. for (i = 8; i < sband->n_bitrates; i++) {
  2420. u8 basic = 0;
  2421. if ((rate_flags & sband->bitrates[i].flags)
  2422. != rate_flags)
  2423. continue;
  2424. if (need_basic && basic_rates & BIT(i))
  2425. basic = 0x80;
  2426. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  2427. 5 * (1 << shift));
  2428. *pos++ = basic | (u8) rate;
  2429. }
  2430. }
  2431. return 0;
  2432. }
  2433. int ieee80211_ave_rssi(struct ieee80211_vif *vif)
  2434. {
  2435. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2436. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2437. if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
  2438. /* non-managed type inferfaces */
  2439. return 0;
  2440. }
  2441. return -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
  2442. }
  2443. EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
  2444. u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
  2445. {
  2446. if (!mcs)
  2447. return 1;
  2448. /* TODO: consider rx_highest */
  2449. if (mcs->rx_mask[3])
  2450. return 4;
  2451. if (mcs->rx_mask[2])
  2452. return 3;
  2453. if (mcs->rx_mask[1])
  2454. return 2;
  2455. return 1;
  2456. }
  2457. /**
  2458. * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
  2459. * @local: mac80211 hw info struct
  2460. * @status: RX status
  2461. * @mpdu_len: total MPDU length (including FCS)
  2462. * @mpdu_offset: offset into MPDU to calculate timestamp at
  2463. *
  2464. * This function calculates the RX timestamp at the given MPDU offset, taking
  2465. * into account what the RX timestamp was. An offset of 0 will just normalize
  2466. * the timestamp to TSF at beginning of MPDU reception.
  2467. */
  2468. u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
  2469. struct ieee80211_rx_status *status,
  2470. unsigned int mpdu_len,
  2471. unsigned int mpdu_offset)
  2472. {
  2473. u64 ts = status->mactime;
  2474. struct rate_info ri;
  2475. u16 rate;
  2476. if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
  2477. return 0;
  2478. memset(&ri, 0, sizeof(ri));
  2479. ri.bw = status->bw;
  2480. /* Fill cfg80211 rate info */
  2481. switch (status->encoding) {
  2482. case RX_ENC_HT:
  2483. ri.mcs = status->rate_idx;
  2484. ri.flags |= RATE_INFO_FLAGS_MCS;
  2485. if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
  2486. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  2487. break;
  2488. case RX_ENC_VHT:
  2489. ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
  2490. ri.mcs = status->rate_idx;
  2491. ri.nss = status->nss;
  2492. if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
  2493. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  2494. break;
  2495. default:
  2496. WARN_ON(1);
  2497. /* fall through */
  2498. case RX_ENC_LEGACY: {
  2499. struct ieee80211_supported_band *sband;
  2500. int shift = 0;
  2501. int bitrate;
  2502. switch (status->bw) {
  2503. case RATE_INFO_BW_10:
  2504. shift = 1;
  2505. break;
  2506. case RATE_INFO_BW_5:
  2507. shift = 2;
  2508. break;
  2509. }
  2510. sband = local->hw.wiphy->bands[status->band];
  2511. bitrate = sband->bitrates[status->rate_idx].bitrate;
  2512. ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
  2513. if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
  2514. /* TODO: handle HT/VHT preambles */
  2515. if (status->band == NL80211_BAND_5GHZ) {
  2516. ts += 20 << shift;
  2517. mpdu_offset += 2;
  2518. } else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) {
  2519. ts += 96;
  2520. } else {
  2521. ts += 192;
  2522. }
  2523. }
  2524. break;
  2525. }
  2526. }
  2527. rate = cfg80211_calculate_bitrate(&ri);
  2528. if (WARN_ONCE(!rate,
  2529. "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
  2530. (unsigned long long)status->flag, status->rate_idx,
  2531. status->nss))
  2532. return 0;
  2533. /* rewind from end of MPDU */
  2534. if (status->flag & RX_FLAG_MACTIME_END)
  2535. ts -= mpdu_len * 8 * 10 / rate;
  2536. ts += mpdu_offset * 8 * 10 / rate;
  2537. return ts;
  2538. }
  2539. void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
  2540. {
  2541. struct ieee80211_sub_if_data *sdata;
  2542. struct cfg80211_chan_def chandef;
  2543. /* for interface list, to avoid linking iflist_mtx and chanctx_mtx */
  2544. ASSERT_RTNL();
  2545. mutex_lock(&local->mtx);
  2546. list_for_each_entry(sdata, &local->interfaces, list) {
  2547. /* it might be waiting for the local->mtx, but then
  2548. * by the time it gets it, sdata->wdev.cac_started
  2549. * will no longer be true
  2550. */
  2551. cancel_delayed_work(&sdata->dfs_cac_timer_work);
  2552. if (sdata->wdev.cac_started) {
  2553. chandef = sdata->vif.bss_conf.chandef;
  2554. ieee80211_vif_release_channel(sdata);
  2555. cfg80211_cac_event(sdata->dev,
  2556. &chandef,
  2557. NL80211_RADAR_CAC_ABORTED,
  2558. GFP_KERNEL);
  2559. }
  2560. }
  2561. mutex_unlock(&local->mtx);
  2562. }
  2563. void ieee80211_dfs_radar_detected_work(struct work_struct *work)
  2564. {
  2565. struct ieee80211_local *local =
  2566. container_of(work, struct ieee80211_local, radar_detected_work);
  2567. struct cfg80211_chan_def chandef = local->hw.conf.chandef;
  2568. struct ieee80211_chanctx *ctx;
  2569. int num_chanctx = 0;
  2570. mutex_lock(&local->chanctx_mtx);
  2571. list_for_each_entry(ctx, &local->chanctx_list, list) {
  2572. if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
  2573. continue;
  2574. num_chanctx++;
  2575. chandef = ctx->conf.def;
  2576. }
  2577. mutex_unlock(&local->chanctx_mtx);
  2578. rtnl_lock();
  2579. ieee80211_dfs_cac_cancel(local);
  2580. rtnl_unlock();
  2581. if (num_chanctx > 1)
  2582. /* XXX: multi-channel is not supported yet */
  2583. WARN_ON(1);
  2584. else
  2585. cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
  2586. }
  2587. void ieee80211_radar_detected(struct ieee80211_hw *hw)
  2588. {
  2589. struct ieee80211_local *local = hw_to_local(hw);
  2590. trace_api_radar_detected(local);
  2591. schedule_work(&local->radar_detected_work);
  2592. }
  2593. EXPORT_SYMBOL(ieee80211_radar_detected);
  2594. u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
  2595. {
  2596. u32 ret;
  2597. int tmp;
  2598. switch (c->width) {
  2599. case NL80211_CHAN_WIDTH_20:
  2600. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  2601. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2602. break;
  2603. case NL80211_CHAN_WIDTH_40:
  2604. c->width = NL80211_CHAN_WIDTH_20;
  2605. c->center_freq1 = c->chan->center_freq;
  2606. ret = IEEE80211_STA_DISABLE_40MHZ |
  2607. IEEE80211_STA_DISABLE_VHT;
  2608. break;
  2609. case NL80211_CHAN_WIDTH_80:
  2610. tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
  2611. /* n_P40 */
  2612. tmp /= 2;
  2613. /* freq_P40 */
  2614. c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
  2615. c->width = NL80211_CHAN_WIDTH_40;
  2616. ret = IEEE80211_STA_DISABLE_VHT;
  2617. break;
  2618. case NL80211_CHAN_WIDTH_80P80:
  2619. c->center_freq2 = 0;
  2620. c->width = NL80211_CHAN_WIDTH_80;
  2621. ret = IEEE80211_STA_DISABLE_80P80MHZ |
  2622. IEEE80211_STA_DISABLE_160MHZ;
  2623. break;
  2624. case NL80211_CHAN_WIDTH_160:
  2625. /* n_P20 */
  2626. tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
  2627. /* n_P80 */
  2628. tmp /= 4;
  2629. c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
  2630. c->width = NL80211_CHAN_WIDTH_80;
  2631. ret = IEEE80211_STA_DISABLE_80P80MHZ |
  2632. IEEE80211_STA_DISABLE_160MHZ;
  2633. break;
  2634. default:
  2635. case NL80211_CHAN_WIDTH_20_NOHT:
  2636. WARN_ON_ONCE(1);
  2637. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  2638. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2639. break;
  2640. case NL80211_CHAN_WIDTH_5:
  2641. case NL80211_CHAN_WIDTH_10:
  2642. WARN_ON_ONCE(1);
  2643. /* keep c->width */
  2644. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2645. break;
  2646. }
  2647. WARN_ON_ONCE(!cfg80211_chandef_valid(c));
  2648. return ret;
  2649. }
  2650. /*
  2651. * Returns true if smps_mode_new is strictly more restrictive than
  2652. * smps_mode_old.
  2653. */
  2654. bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
  2655. enum ieee80211_smps_mode smps_mode_new)
  2656. {
  2657. if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
  2658. smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
  2659. return false;
  2660. switch (smps_mode_old) {
  2661. case IEEE80211_SMPS_STATIC:
  2662. return false;
  2663. case IEEE80211_SMPS_DYNAMIC:
  2664. return smps_mode_new == IEEE80211_SMPS_STATIC;
  2665. case IEEE80211_SMPS_OFF:
  2666. return smps_mode_new != IEEE80211_SMPS_OFF;
  2667. default:
  2668. WARN_ON(1);
  2669. }
  2670. return false;
  2671. }
  2672. int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
  2673. struct cfg80211_csa_settings *csa_settings)
  2674. {
  2675. struct sk_buff *skb;
  2676. struct ieee80211_mgmt *mgmt;
  2677. struct ieee80211_local *local = sdata->local;
  2678. int freq;
  2679. int hdr_len = offsetofend(struct ieee80211_mgmt,
  2680. u.action.u.chan_switch);
  2681. u8 *pos;
  2682. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2683. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  2684. return -EOPNOTSUPP;
  2685. skb = dev_alloc_skb(local->tx_headroom + hdr_len +
  2686. 5 + /* channel switch announcement element */
  2687. 3 + /* secondary channel offset element */
  2688. 5 + /* wide bandwidth channel switch announcement */
  2689. 8); /* mesh channel switch parameters element */
  2690. if (!skb)
  2691. return -ENOMEM;
  2692. skb_reserve(skb, local->tx_headroom);
  2693. mgmt = skb_put_zero(skb, hdr_len);
  2694. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  2695. IEEE80211_STYPE_ACTION);
  2696. eth_broadcast_addr(mgmt->da);
  2697. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  2698. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2699. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  2700. } else {
  2701. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  2702. memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
  2703. }
  2704. mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
  2705. mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
  2706. pos = skb_put(skb, 5);
  2707. *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
  2708. *pos++ = 3; /* IE length */
  2709. *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
  2710. freq = csa_settings->chandef.chan->center_freq;
  2711. *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
  2712. *pos++ = csa_settings->count; /* count */
  2713. if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
  2714. enum nl80211_channel_type ch_type;
  2715. skb_put(skb, 3);
  2716. *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
  2717. *pos++ = 1; /* IE length */
  2718. ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
  2719. if (ch_type == NL80211_CHAN_HT40PLUS)
  2720. *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  2721. else
  2722. *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  2723. }
  2724. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2725. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  2726. skb_put(skb, 8);
  2727. *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
  2728. *pos++ = 6; /* IE length */
  2729. *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
  2730. *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
  2731. *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
  2732. *pos++ |= csa_settings->block_tx ?
  2733. WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
  2734. put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
  2735. pos += 2;
  2736. put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
  2737. pos += 2;
  2738. }
  2739. if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_80 ||
  2740. csa_settings->chandef.width == NL80211_CHAN_WIDTH_80P80 ||
  2741. csa_settings->chandef.width == NL80211_CHAN_WIDTH_160) {
  2742. skb_put(skb, 5);
  2743. ieee80211_ie_build_wide_bw_cs(pos, &csa_settings->chandef);
  2744. }
  2745. ieee80211_tx_skb(sdata, skb);
  2746. return 0;
  2747. }
  2748. bool ieee80211_cs_valid(const struct ieee80211_cipher_scheme *cs)
  2749. {
  2750. return !(cs == NULL || cs->cipher == 0 ||
  2751. cs->hdr_len < cs->pn_len + cs->pn_off ||
  2752. cs->hdr_len <= cs->key_idx_off ||
  2753. cs->key_idx_shift > 7 ||
  2754. cs->key_idx_mask == 0);
  2755. }
  2756. bool ieee80211_cs_list_valid(const struct ieee80211_cipher_scheme *cs, int n)
  2757. {
  2758. int i;
  2759. /* Ensure we have enough iftype bitmap space for all iftype values */
  2760. WARN_ON((NUM_NL80211_IFTYPES / 8 + 1) > sizeof(cs[0].iftype));
  2761. for (i = 0; i < n; i++)
  2762. if (!ieee80211_cs_valid(&cs[i]))
  2763. return false;
  2764. return true;
  2765. }
  2766. const struct ieee80211_cipher_scheme *
  2767. ieee80211_cs_get(struct ieee80211_local *local, u32 cipher,
  2768. enum nl80211_iftype iftype)
  2769. {
  2770. const struct ieee80211_cipher_scheme *l = local->hw.cipher_schemes;
  2771. int n = local->hw.n_cipher_schemes;
  2772. int i;
  2773. const struct ieee80211_cipher_scheme *cs = NULL;
  2774. for (i = 0; i < n; i++) {
  2775. if (l[i].cipher == cipher) {
  2776. cs = &l[i];
  2777. break;
  2778. }
  2779. }
  2780. if (!cs || !(cs->iftype & BIT(iftype)))
  2781. return NULL;
  2782. return cs;
  2783. }
  2784. int ieee80211_cs_headroom(struct ieee80211_local *local,
  2785. struct cfg80211_crypto_settings *crypto,
  2786. enum nl80211_iftype iftype)
  2787. {
  2788. const struct ieee80211_cipher_scheme *cs;
  2789. int headroom = IEEE80211_ENCRYPT_HEADROOM;
  2790. int i;
  2791. for (i = 0; i < crypto->n_ciphers_pairwise; i++) {
  2792. cs = ieee80211_cs_get(local, crypto->ciphers_pairwise[i],
  2793. iftype);
  2794. if (cs && headroom < cs->hdr_len)
  2795. headroom = cs->hdr_len;
  2796. }
  2797. cs = ieee80211_cs_get(local, crypto->cipher_group, iftype);
  2798. if (cs && headroom < cs->hdr_len)
  2799. headroom = cs->hdr_len;
  2800. return headroom;
  2801. }
  2802. static bool
  2803. ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
  2804. {
  2805. s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
  2806. int skip;
  2807. if (end > 0)
  2808. return false;
  2809. /* One shot NOA */
  2810. if (data->count[i] == 1)
  2811. return false;
  2812. if (data->desc[i].interval == 0)
  2813. return false;
  2814. /* End time is in the past, check for repetitions */
  2815. skip = DIV_ROUND_UP(-end, data->desc[i].interval);
  2816. if (data->count[i] < 255) {
  2817. if (data->count[i] <= skip) {
  2818. data->count[i] = 0;
  2819. return false;
  2820. }
  2821. data->count[i] -= skip;
  2822. }
  2823. data->desc[i].start += skip * data->desc[i].interval;
  2824. return true;
  2825. }
  2826. static bool
  2827. ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
  2828. s32 *offset)
  2829. {
  2830. bool ret = false;
  2831. int i;
  2832. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  2833. s32 cur;
  2834. if (!data->count[i])
  2835. continue;
  2836. if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
  2837. ret = true;
  2838. cur = data->desc[i].start - tsf;
  2839. if (cur > *offset)
  2840. continue;
  2841. cur = data->desc[i].start + data->desc[i].duration - tsf;
  2842. if (cur > *offset)
  2843. *offset = cur;
  2844. }
  2845. return ret;
  2846. }
  2847. static u32
  2848. ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
  2849. {
  2850. s32 offset = 0;
  2851. int tries = 0;
  2852. /*
  2853. * arbitrary limit, used to avoid infinite loops when combined NoA
  2854. * descriptors cover the full time period.
  2855. */
  2856. int max_tries = 5;
  2857. ieee80211_extend_absent_time(data, tsf, &offset);
  2858. do {
  2859. if (!ieee80211_extend_absent_time(data, tsf, &offset))
  2860. break;
  2861. tries++;
  2862. } while (tries < max_tries);
  2863. return offset;
  2864. }
  2865. void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
  2866. {
  2867. u32 next_offset = BIT(31) - 1;
  2868. int i;
  2869. data->absent = 0;
  2870. data->has_next_tsf = false;
  2871. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  2872. s32 start;
  2873. if (!data->count[i])
  2874. continue;
  2875. ieee80211_extend_noa_desc(data, tsf, i);
  2876. start = data->desc[i].start - tsf;
  2877. if (start <= 0)
  2878. data->absent |= BIT(i);
  2879. if (next_offset > start)
  2880. next_offset = start;
  2881. data->has_next_tsf = true;
  2882. }
  2883. if (data->absent)
  2884. next_offset = ieee80211_get_noa_absent_time(data, tsf);
  2885. data->next_tsf = tsf + next_offset;
  2886. }
  2887. EXPORT_SYMBOL(ieee80211_update_p2p_noa);
  2888. int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
  2889. struct ieee80211_noa_data *data, u32 tsf)
  2890. {
  2891. int ret = 0;
  2892. int i;
  2893. memset(data, 0, sizeof(*data));
  2894. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  2895. const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
  2896. if (!desc->count || !desc->duration)
  2897. continue;
  2898. data->count[i] = desc->count;
  2899. data->desc[i].start = le32_to_cpu(desc->start_time);
  2900. data->desc[i].duration = le32_to_cpu(desc->duration);
  2901. data->desc[i].interval = le32_to_cpu(desc->interval);
  2902. if (data->count[i] > 1 &&
  2903. data->desc[i].interval < data->desc[i].duration)
  2904. continue;
  2905. ieee80211_extend_noa_desc(data, tsf, i);
  2906. ret++;
  2907. }
  2908. if (ret)
  2909. ieee80211_update_p2p_noa(data, tsf);
  2910. return ret;
  2911. }
  2912. EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
  2913. void ieee80211_recalc_dtim(struct ieee80211_local *local,
  2914. struct ieee80211_sub_if_data *sdata)
  2915. {
  2916. u64 tsf = drv_get_tsf(local, sdata);
  2917. u64 dtim_count = 0;
  2918. u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
  2919. u8 dtim_period = sdata->vif.bss_conf.dtim_period;
  2920. struct ps_data *ps;
  2921. u8 bcns_from_dtim;
  2922. if (tsf == -1ULL || !beacon_int || !dtim_period)
  2923. return;
  2924. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  2925. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  2926. if (!sdata->bss)
  2927. return;
  2928. ps = &sdata->bss->ps;
  2929. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2930. ps = &sdata->u.mesh.ps;
  2931. } else {
  2932. return;
  2933. }
  2934. /*
  2935. * actually finds last dtim_count, mac80211 will update in
  2936. * __beacon_add_tim().
  2937. * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
  2938. */
  2939. do_div(tsf, beacon_int);
  2940. bcns_from_dtim = do_div(tsf, dtim_period);
  2941. /* just had a DTIM */
  2942. if (!bcns_from_dtim)
  2943. dtim_count = 0;
  2944. else
  2945. dtim_count = dtim_period - bcns_from_dtim;
  2946. ps->dtim_count = dtim_count;
  2947. }
  2948. static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
  2949. struct ieee80211_chanctx *ctx)
  2950. {
  2951. struct ieee80211_sub_if_data *sdata;
  2952. u8 radar_detect = 0;
  2953. lockdep_assert_held(&local->chanctx_mtx);
  2954. if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
  2955. return 0;
  2956. list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
  2957. if (sdata->reserved_radar_required)
  2958. radar_detect |= BIT(sdata->reserved_chandef.width);
  2959. /*
  2960. * An in-place reservation context should not have any assigned vifs
  2961. * until it replaces the other context.
  2962. */
  2963. WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
  2964. !list_empty(&ctx->assigned_vifs));
  2965. list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
  2966. if (sdata->radar_required)
  2967. radar_detect |= BIT(sdata->vif.bss_conf.chandef.width);
  2968. return radar_detect;
  2969. }
  2970. int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
  2971. const struct cfg80211_chan_def *chandef,
  2972. enum ieee80211_chanctx_mode chanmode,
  2973. u8 radar_detect)
  2974. {
  2975. struct ieee80211_local *local = sdata->local;
  2976. struct ieee80211_sub_if_data *sdata_iter;
  2977. enum nl80211_iftype iftype = sdata->wdev.iftype;
  2978. struct ieee80211_chanctx *ctx;
  2979. int total = 1;
  2980. struct iface_combination_params params = {
  2981. .radar_detect = radar_detect,
  2982. };
  2983. lockdep_assert_held(&local->chanctx_mtx);
  2984. if (WARN_ON(hweight32(radar_detect) > 1))
  2985. return -EINVAL;
  2986. if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
  2987. !chandef->chan))
  2988. return -EINVAL;
  2989. if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
  2990. return -EINVAL;
  2991. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  2992. sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
  2993. /*
  2994. * always passing this is harmless, since it'll be the
  2995. * same value that cfg80211 finds if it finds the same
  2996. * interface ... and that's always allowed
  2997. */
  2998. params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
  2999. }
  3000. /* Always allow software iftypes */
  3001. if (local->hw.wiphy->software_iftypes & BIT(iftype)) {
  3002. if (radar_detect)
  3003. return -EINVAL;
  3004. return 0;
  3005. }
  3006. if (chandef)
  3007. params.num_different_channels = 1;
  3008. if (iftype != NL80211_IFTYPE_UNSPECIFIED)
  3009. params.iftype_num[iftype] = 1;
  3010. list_for_each_entry(ctx, &local->chanctx_list, list) {
  3011. if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
  3012. continue;
  3013. params.radar_detect |=
  3014. ieee80211_chanctx_radar_detect(local, ctx);
  3015. if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
  3016. params.num_different_channels++;
  3017. continue;
  3018. }
  3019. if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
  3020. cfg80211_chandef_compatible(chandef,
  3021. &ctx->conf.def))
  3022. continue;
  3023. params.num_different_channels++;
  3024. }
  3025. list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
  3026. struct wireless_dev *wdev_iter;
  3027. wdev_iter = &sdata_iter->wdev;
  3028. if (sdata_iter == sdata ||
  3029. !ieee80211_sdata_running(sdata_iter) ||
  3030. local->hw.wiphy->software_iftypes & BIT(wdev_iter->iftype))
  3031. continue;
  3032. params.iftype_num[wdev_iter->iftype]++;
  3033. total++;
  3034. }
  3035. if (total == 1 && !params.radar_detect)
  3036. return 0;
  3037. return cfg80211_check_combinations(local->hw.wiphy, &params);
  3038. }
  3039. static void
  3040. ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
  3041. void *data)
  3042. {
  3043. u32 *max_num_different_channels = data;
  3044. *max_num_different_channels = max(*max_num_different_channels,
  3045. c->num_different_channels);
  3046. }
  3047. int ieee80211_max_num_channels(struct ieee80211_local *local)
  3048. {
  3049. struct ieee80211_sub_if_data *sdata;
  3050. struct ieee80211_chanctx *ctx;
  3051. u32 max_num_different_channels = 1;
  3052. int err;
  3053. struct iface_combination_params params = {0};
  3054. lockdep_assert_held(&local->chanctx_mtx);
  3055. list_for_each_entry(ctx, &local->chanctx_list, list) {
  3056. if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
  3057. continue;
  3058. params.num_different_channels++;
  3059. params.radar_detect |=
  3060. ieee80211_chanctx_radar_detect(local, ctx);
  3061. }
  3062. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  3063. params.iftype_num[sdata->wdev.iftype]++;
  3064. err = cfg80211_iter_combinations(local->hw.wiphy, &params,
  3065. ieee80211_iter_max_chans,
  3066. &max_num_different_channels);
  3067. if (err < 0)
  3068. return err;
  3069. return max_num_different_channels;
  3070. }
  3071. u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
  3072. {
  3073. *buf++ = WLAN_EID_VENDOR_SPECIFIC;
  3074. *buf++ = 7; /* len */
  3075. *buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
  3076. *buf++ = 0x50;
  3077. *buf++ = 0xf2;
  3078. *buf++ = 2; /* WME */
  3079. *buf++ = 0; /* WME info */
  3080. *buf++ = 1; /* WME ver */
  3081. *buf++ = qosinfo; /* U-APSD no in use */
  3082. return buf;
  3083. }
  3084. void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
  3085. unsigned long *frame_cnt,
  3086. unsigned long *byte_cnt)
  3087. {
  3088. struct txq_info *txqi = to_txq_info(txq);
  3089. u32 frag_cnt = 0, frag_bytes = 0;
  3090. struct sk_buff *skb;
  3091. skb_queue_walk(&txqi->frags, skb) {
  3092. frag_cnt++;
  3093. frag_bytes += skb->len;
  3094. }
  3095. if (frame_cnt)
  3096. *frame_cnt = txqi->tin.backlog_packets + frag_cnt;
  3097. if (byte_cnt)
  3098. *byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
  3099. }
  3100. EXPORT_SYMBOL(ieee80211_txq_get_depth);
  3101. const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
  3102. IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
  3103. IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
  3104. IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
  3105. IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
  3106. };