util.c 91 KB

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