util.c 95 KB

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