util.c 87 KB

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