util.c 76 KB

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