util.c 97 KB

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