util.c 80 KB

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