mlme.c 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281
  1. /*
  2. * BSS client mode implementation
  3. * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright 2004, Instant802 Networks, Inc.
  5. * Copyright 2005, Devicescape Software, Inc.
  6. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  7. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/if_ether.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/pm_qos.h>
  21. #include <linux/crc32.h>
  22. #include <linux/slab.h>
  23. #include <linux/export.h>
  24. #include <net/mac80211.h>
  25. #include <asm/unaligned.h>
  26. #include "ieee80211_i.h"
  27. #include "driver-ops.h"
  28. #include "rate.h"
  29. #include "led.h"
  30. #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
  31. #define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
  32. #define IEEE80211_AUTH_MAX_TRIES 3
  33. #define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
  34. #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
  35. #define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
  36. #define IEEE80211_ASSOC_MAX_TRIES 3
  37. static int max_nullfunc_tries = 2;
  38. module_param(max_nullfunc_tries, int, 0644);
  39. MODULE_PARM_DESC(max_nullfunc_tries,
  40. "Maximum nullfunc tx tries before disconnecting (reason 4).");
  41. static int max_probe_tries = 5;
  42. module_param(max_probe_tries, int, 0644);
  43. MODULE_PARM_DESC(max_probe_tries,
  44. "Maximum probe tries before disconnecting (reason 4).");
  45. /*
  46. * Beacon loss timeout is calculated as N frames times the
  47. * advertised beacon interval. This may need to be somewhat
  48. * higher than what hardware might detect to account for
  49. * delays in the host processing frames. But since we also
  50. * probe on beacon miss before declaring the connection lost
  51. * default to what we want.
  52. */
  53. #define IEEE80211_BEACON_LOSS_COUNT 7
  54. /*
  55. * Time the connection can be idle before we probe
  56. * it to see if we can still talk to the AP.
  57. */
  58. #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
  59. /*
  60. * Time we wait for a probe response after sending
  61. * a probe request because of beacon loss or for
  62. * checking the connection still works.
  63. */
  64. static int probe_wait_ms = 500;
  65. module_param(probe_wait_ms, int, 0644);
  66. MODULE_PARM_DESC(probe_wait_ms,
  67. "Maximum time(ms) to wait for probe response"
  68. " before disconnecting (reason 4).");
  69. /*
  70. * Weight given to the latest Beacon frame when calculating average signal
  71. * strength for Beacon frames received in the current BSS. This must be
  72. * between 1 and 15.
  73. */
  74. #define IEEE80211_SIGNAL_AVE_WEIGHT 3
  75. /*
  76. * How many Beacon frames need to have been used in average signal strength
  77. * before starting to indicate signal change events.
  78. */
  79. #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
  80. /*
  81. * All cfg80211 functions have to be called outside a locked
  82. * section so that they can acquire a lock themselves... This
  83. * is much simpler than queuing up things in cfg80211, but we
  84. * do need some indirection for that here.
  85. */
  86. enum rx_mgmt_action {
  87. /* no action required */
  88. RX_MGMT_NONE,
  89. /* caller must call cfg80211_send_deauth() */
  90. RX_MGMT_CFG80211_DEAUTH,
  91. /* caller must call cfg80211_send_disassoc() */
  92. RX_MGMT_CFG80211_DISASSOC,
  93. /* caller must call cfg80211_send_rx_auth() */
  94. RX_MGMT_CFG80211_RX_AUTH,
  95. /* caller must call cfg80211_send_rx_assoc() */
  96. RX_MGMT_CFG80211_RX_ASSOC,
  97. /* caller must call cfg80211_send_assoc_timeout() */
  98. RX_MGMT_CFG80211_ASSOC_TIMEOUT,
  99. /* used when a processed beacon causes a deauth */
  100. RX_MGMT_CFG80211_TX_DEAUTH,
  101. };
  102. /* utils */
  103. static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
  104. {
  105. lockdep_assert_held(&ifmgd->mtx);
  106. }
  107. /*
  108. * We can have multiple work items (and connection probing)
  109. * scheduling this timer, but we need to take care to only
  110. * reschedule it when it should fire _earlier_ than it was
  111. * asked for before, or if it's not pending right now. This
  112. * function ensures that. Note that it then is required to
  113. * run this function for all timeouts after the first one
  114. * has happened -- the work that runs from this timer will
  115. * do that.
  116. */
  117. static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
  118. {
  119. ASSERT_MGD_MTX(ifmgd);
  120. if (!timer_pending(&ifmgd->timer) ||
  121. time_before(timeout, ifmgd->timer.expires))
  122. mod_timer(&ifmgd->timer, timeout);
  123. }
  124. void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
  125. {
  126. if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
  127. return;
  128. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  129. return;
  130. mod_timer(&sdata->u.mgd.bcn_mon_timer,
  131. round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
  132. }
  133. void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
  134. {
  135. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  136. if (unlikely(!sdata->u.mgd.associated))
  137. return;
  138. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  139. return;
  140. mod_timer(&sdata->u.mgd.conn_mon_timer,
  141. round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
  142. ifmgd->probe_send_count = 0;
  143. }
  144. static int ecw2cw(int ecw)
  145. {
  146. return (1 << ecw) - 1;
  147. }
  148. static u32 chandef_downgrade(struct cfg80211_chan_def *c)
  149. {
  150. u32 ret;
  151. int tmp;
  152. switch (c->width) {
  153. case NL80211_CHAN_WIDTH_20:
  154. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  155. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  156. break;
  157. case NL80211_CHAN_WIDTH_40:
  158. c->width = NL80211_CHAN_WIDTH_20;
  159. c->center_freq1 = c->chan->center_freq;
  160. ret = IEEE80211_STA_DISABLE_40MHZ |
  161. IEEE80211_STA_DISABLE_VHT;
  162. break;
  163. case NL80211_CHAN_WIDTH_80:
  164. tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
  165. /* n_P40 */
  166. tmp /= 2;
  167. /* freq_P40 */
  168. c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
  169. c->width = NL80211_CHAN_WIDTH_40;
  170. ret = IEEE80211_STA_DISABLE_VHT;
  171. break;
  172. case NL80211_CHAN_WIDTH_80P80:
  173. c->center_freq2 = 0;
  174. c->width = NL80211_CHAN_WIDTH_80;
  175. ret = IEEE80211_STA_DISABLE_80P80MHZ |
  176. IEEE80211_STA_DISABLE_160MHZ;
  177. break;
  178. case NL80211_CHAN_WIDTH_160:
  179. /* n_P20 */
  180. tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
  181. /* n_P80 */
  182. tmp /= 4;
  183. c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
  184. c->width = NL80211_CHAN_WIDTH_80;
  185. ret = IEEE80211_STA_DISABLE_80P80MHZ |
  186. IEEE80211_STA_DISABLE_160MHZ;
  187. break;
  188. default:
  189. case NL80211_CHAN_WIDTH_20_NOHT:
  190. WARN_ON_ONCE(1);
  191. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  192. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  193. break;
  194. }
  195. WARN_ON_ONCE(!cfg80211_chandef_valid(c));
  196. return ret;
  197. }
  198. static u32
  199. ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
  200. struct ieee80211_supported_band *sband,
  201. struct ieee80211_channel *channel,
  202. const struct ieee80211_ht_operation *ht_oper,
  203. const struct ieee80211_vht_operation *vht_oper,
  204. struct cfg80211_chan_def *chandef, bool verbose)
  205. {
  206. struct cfg80211_chan_def vht_chandef;
  207. u32 ht_cfreq, ret;
  208. chandef->chan = channel;
  209. chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  210. chandef->center_freq1 = channel->center_freq;
  211. chandef->center_freq2 = 0;
  212. if (!ht_oper || !sband->ht_cap.ht_supported) {
  213. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  214. goto out;
  215. }
  216. chandef->width = NL80211_CHAN_WIDTH_20;
  217. ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
  218. channel->band);
  219. /* check that channel matches the right operating channel */
  220. if (channel->center_freq != ht_cfreq) {
  221. /*
  222. * It's possible that some APs are confused here;
  223. * Netgear WNDR3700 sometimes reports 4 higher than
  224. * the actual channel in association responses, but
  225. * since we look at probe response/beacon data here
  226. * it should be OK.
  227. */
  228. if (verbose)
  229. sdata_info(sdata,
  230. "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
  231. channel->center_freq, ht_cfreq,
  232. ht_oper->primary_chan, channel->band);
  233. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  234. goto out;
  235. }
  236. /* check 40 MHz support, if we have it */
  237. if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
  238. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  239. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  240. chandef->width = NL80211_CHAN_WIDTH_40;
  241. chandef->center_freq1 += 10;
  242. break;
  243. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  244. chandef->width = NL80211_CHAN_WIDTH_40;
  245. chandef->center_freq1 -= 10;
  246. break;
  247. }
  248. } else {
  249. /* 40 MHz (and 80 MHz) must be supported for VHT */
  250. ret = IEEE80211_STA_DISABLE_VHT;
  251. goto out;
  252. }
  253. if (!vht_oper || !sband->vht_cap.vht_supported) {
  254. ret = IEEE80211_STA_DISABLE_VHT;
  255. goto out;
  256. }
  257. vht_chandef.chan = channel;
  258. vht_chandef.center_freq1 =
  259. ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
  260. channel->band);
  261. vht_chandef.center_freq2 = 0;
  262. if (vht_oper->center_freq_seg2_idx)
  263. vht_chandef.center_freq2 =
  264. ieee80211_channel_to_frequency(
  265. vht_oper->center_freq_seg2_idx,
  266. channel->band);
  267. switch (vht_oper->chan_width) {
  268. case IEEE80211_VHT_CHANWIDTH_USE_HT:
  269. vht_chandef.width = chandef->width;
  270. break;
  271. case IEEE80211_VHT_CHANWIDTH_80MHZ:
  272. vht_chandef.width = NL80211_CHAN_WIDTH_80;
  273. break;
  274. case IEEE80211_VHT_CHANWIDTH_160MHZ:
  275. vht_chandef.width = NL80211_CHAN_WIDTH_160;
  276. break;
  277. case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
  278. vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
  279. break;
  280. default:
  281. if (verbose)
  282. sdata_info(sdata,
  283. "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
  284. vht_oper->chan_width);
  285. ret = IEEE80211_STA_DISABLE_VHT;
  286. goto out;
  287. }
  288. if (!cfg80211_chandef_valid(&vht_chandef)) {
  289. if (verbose)
  290. sdata_info(sdata,
  291. "AP VHT information is invalid, disable VHT\n");
  292. ret = IEEE80211_STA_DISABLE_VHT;
  293. goto out;
  294. }
  295. if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
  296. ret = 0;
  297. goto out;
  298. }
  299. if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
  300. if (verbose)
  301. sdata_info(sdata,
  302. "AP VHT information doesn't match HT, disable VHT\n");
  303. ret = IEEE80211_STA_DISABLE_VHT;
  304. goto out;
  305. }
  306. *chandef = vht_chandef;
  307. ret = 0;
  308. out:
  309. /* don't print the message below for VHT mismatch if VHT is disabled */
  310. if (ret & IEEE80211_STA_DISABLE_VHT)
  311. vht_chandef = *chandef;
  312. while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  313. IEEE80211_CHAN_DISABLED)) {
  314. if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
  315. ret = IEEE80211_STA_DISABLE_HT |
  316. IEEE80211_STA_DISABLE_VHT;
  317. goto out;
  318. }
  319. ret |= chandef_downgrade(chandef);
  320. }
  321. if (chandef->width != vht_chandef.width && verbose)
  322. sdata_info(sdata,
  323. "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
  324. WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
  325. return ret;
  326. }
  327. static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
  328. struct sta_info *sta,
  329. const struct ieee80211_ht_operation *ht_oper,
  330. const struct ieee80211_vht_operation *vht_oper,
  331. const u8 *bssid, u32 *changed)
  332. {
  333. struct ieee80211_local *local = sdata->local;
  334. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  335. struct ieee80211_supported_band *sband;
  336. struct ieee80211_channel *chan;
  337. struct cfg80211_chan_def chandef;
  338. u16 ht_opmode;
  339. u32 flags;
  340. enum ieee80211_sta_rx_bandwidth new_sta_bw;
  341. int ret;
  342. /* if HT was/is disabled, don't track any bandwidth changes */
  343. if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
  344. return 0;
  345. /* don't check VHT if we associated as non-VHT station */
  346. if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
  347. vht_oper = NULL;
  348. if (WARN_ON_ONCE(!sta))
  349. return -EINVAL;
  350. chan = sdata->vif.bss_conf.chandef.chan;
  351. sband = local->hw.wiphy->bands[chan->band];
  352. /* calculate new channel (type) based on HT/VHT operation IEs */
  353. flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
  354. vht_oper, &chandef, false);
  355. /*
  356. * Downgrade the new channel if we associated with restricted
  357. * capabilities. For example, if we associated as a 20 MHz STA
  358. * to a 40 MHz AP (due to regulatory, capabilities or config
  359. * reasons) then switching to a 40 MHz channel now won't do us
  360. * any good -- we couldn't use it with the AP.
  361. */
  362. if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
  363. chandef.width == NL80211_CHAN_WIDTH_80P80)
  364. flags |= chandef_downgrade(&chandef);
  365. if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
  366. chandef.width == NL80211_CHAN_WIDTH_160)
  367. flags |= chandef_downgrade(&chandef);
  368. if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
  369. chandef.width > NL80211_CHAN_WIDTH_20)
  370. flags |= chandef_downgrade(&chandef);
  371. if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
  372. return 0;
  373. sdata_info(sdata,
  374. "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
  375. ifmgd->bssid, chandef.chan->center_freq, chandef.width,
  376. chandef.center_freq1, chandef.center_freq2);
  377. if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
  378. IEEE80211_STA_DISABLE_VHT |
  379. IEEE80211_STA_DISABLE_40MHZ |
  380. IEEE80211_STA_DISABLE_80P80MHZ |
  381. IEEE80211_STA_DISABLE_160MHZ)) ||
  382. !cfg80211_chandef_valid(&chandef)) {
  383. sdata_info(sdata,
  384. "AP %pM changed bandwidth in a way we can't support - disconnect\n",
  385. ifmgd->bssid);
  386. return -EINVAL;
  387. }
  388. switch (chandef.width) {
  389. case NL80211_CHAN_WIDTH_20_NOHT:
  390. case NL80211_CHAN_WIDTH_20:
  391. new_sta_bw = IEEE80211_STA_RX_BW_20;
  392. break;
  393. case NL80211_CHAN_WIDTH_40:
  394. new_sta_bw = IEEE80211_STA_RX_BW_40;
  395. break;
  396. case NL80211_CHAN_WIDTH_80:
  397. new_sta_bw = IEEE80211_STA_RX_BW_80;
  398. break;
  399. case NL80211_CHAN_WIDTH_80P80:
  400. case NL80211_CHAN_WIDTH_160:
  401. new_sta_bw = IEEE80211_STA_RX_BW_160;
  402. break;
  403. default:
  404. return -EINVAL;
  405. }
  406. if (new_sta_bw > sta->cur_max_bandwidth)
  407. new_sta_bw = sta->cur_max_bandwidth;
  408. if (new_sta_bw < sta->sta.bandwidth) {
  409. sta->sta.bandwidth = new_sta_bw;
  410. rate_control_rate_update(local, sband, sta,
  411. IEEE80211_RC_BW_CHANGED);
  412. }
  413. ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
  414. if (ret) {
  415. sdata_info(sdata,
  416. "AP %pM changed bandwidth to incompatible one - disconnect\n",
  417. ifmgd->bssid);
  418. return ret;
  419. }
  420. if (new_sta_bw > sta->sta.bandwidth) {
  421. sta->sta.bandwidth = new_sta_bw;
  422. rate_control_rate_update(local, sband, sta,
  423. IEEE80211_RC_BW_CHANGED);
  424. }
  425. ht_opmode = le16_to_cpu(ht_oper->operation_mode);
  426. /* if bss configuration changed store the new one */
  427. if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
  428. *changed |= BSS_CHANGED_HT;
  429. sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
  430. }
  431. return 0;
  432. }
  433. /* frame sending functions */
  434. static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
  435. struct ieee80211_supported_band *sband,
  436. u32 *rates)
  437. {
  438. int i, j, count;
  439. *rates = 0;
  440. count = 0;
  441. for (i = 0; i < supp_rates_len; i++) {
  442. int rate = (supp_rates[i] & 0x7F) * 5;
  443. for (j = 0; j < sband->n_bitrates; j++)
  444. if (sband->bitrates[j].bitrate == rate) {
  445. *rates |= BIT(j);
  446. count++;
  447. break;
  448. }
  449. }
  450. return count;
  451. }
  452. static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
  453. struct sk_buff *skb, u8 ap_ht_param,
  454. struct ieee80211_supported_band *sband,
  455. struct ieee80211_channel *channel,
  456. enum ieee80211_smps_mode smps)
  457. {
  458. u8 *pos;
  459. u32 flags = channel->flags;
  460. u16 cap;
  461. struct ieee80211_sta_ht_cap ht_cap;
  462. BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
  463. memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
  464. ieee80211_apply_htcap_overrides(sdata, &ht_cap);
  465. /* determine capability flags */
  466. cap = ht_cap.cap;
  467. switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  468. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  469. if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
  470. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  471. cap &= ~IEEE80211_HT_CAP_SGI_40;
  472. }
  473. break;
  474. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  475. if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
  476. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  477. cap &= ~IEEE80211_HT_CAP_SGI_40;
  478. }
  479. break;
  480. }
  481. /*
  482. * If 40 MHz was disabled associate as though we weren't
  483. * capable of 40 MHz -- some broken APs will never fall
  484. * back to trying to transmit in 20 MHz.
  485. */
  486. if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
  487. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  488. cap &= ~IEEE80211_HT_CAP_SGI_40;
  489. }
  490. /* set SM PS mode properly */
  491. cap &= ~IEEE80211_HT_CAP_SM_PS;
  492. switch (smps) {
  493. case IEEE80211_SMPS_AUTOMATIC:
  494. case IEEE80211_SMPS_NUM_MODES:
  495. WARN_ON(1);
  496. case IEEE80211_SMPS_OFF:
  497. cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
  498. IEEE80211_HT_CAP_SM_PS_SHIFT;
  499. break;
  500. case IEEE80211_SMPS_STATIC:
  501. cap |= WLAN_HT_CAP_SM_PS_STATIC <<
  502. IEEE80211_HT_CAP_SM_PS_SHIFT;
  503. break;
  504. case IEEE80211_SMPS_DYNAMIC:
  505. cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
  506. IEEE80211_HT_CAP_SM_PS_SHIFT;
  507. break;
  508. }
  509. /* reserve and fill IE */
  510. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
  511. ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
  512. }
  513. static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
  514. struct sk_buff *skb,
  515. struct ieee80211_supported_band *sband,
  516. struct ieee80211_vht_cap *ap_vht_cap)
  517. {
  518. u8 *pos;
  519. u32 cap;
  520. struct ieee80211_sta_vht_cap vht_cap;
  521. int i;
  522. BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
  523. memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
  524. ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
  525. /* determine capability flags */
  526. cap = vht_cap.cap;
  527. if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
  528. cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
  529. cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
  530. }
  531. if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
  532. cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
  533. cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
  534. }
  535. /*
  536. * Some APs apparently get confused if our capabilities are better
  537. * than theirs, so restrict what we advertise in the assoc request.
  538. */
  539. if (!(ap_vht_cap->vht_cap_info &
  540. cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
  541. cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
  542. if (!(ap_vht_cap->vht_cap_info &
  543. cpu_to_le32(IEEE80211_VHT_CAP_TXSTBC)))
  544. cap &= ~(IEEE80211_VHT_CAP_RXSTBC_1 |
  545. IEEE80211_VHT_CAP_RXSTBC_3 |
  546. IEEE80211_VHT_CAP_RXSTBC_4);
  547. for (i = 0; i < 8; i++) {
  548. int shift = i * 2;
  549. u16 mask = IEEE80211_VHT_MCS_NOT_SUPPORTED << shift;
  550. u16 ap_mcs, our_mcs;
  551. ap_mcs = (le16_to_cpu(ap_vht_cap->supp_mcs.tx_mcs_map) &
  552. mask) >> shift;
  553. our_mcs = (le16_to_cpu(vht_cap.vht_mcs.rx_mcs_map) &
  554. mask) >> shift;
  555. if (our_mcs == IEEE80211_VHT_MCS_NOT_SUPPORTED)
  556. continue;
  557. switch (ap_mcs) {
  558. default:
  559. if (our_mcs <= ap_mcs)
  560. break;
  561. /* fall through */
  562. case IEEE80211_VHT_MCS_NOT_SUPPORTED:
  563. vht_cap.vht_mcs.rx_mcs_map &= cpu_to_le16(~mask);
  564. vht_cap.vht_mcs.rx_mcs_map |=
  565. cpu_to_le16(ap_mcs << shift);
  566. }
  567. }
  568. /* reserve and fill IE */
  569. pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
  570. ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
  571. }
  572. static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
  573. {
  574. struct ieee80211_local *local = sdata->local;
  575. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  576. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  577. struct sk_buff *skb;
  578. struct ieee80211_mgmt *mgmt;
  579. u8 *pos, qos_info;
  580. size_t offset = 0, noffset;
  581. int i, count, rates_len, supp_rates_len;
  582. u16 capab;
  583. struct ieee80211_supported_band *sband;
  584. struct ieee80211_chanctx_conf *chanctx_conf;
  585. struct ieee80211_channel *chan;
  586. u32 rates = 0;
  587. lockdep_assert_held(&ifmgd->mtx);
  588. rcu_read_lock();
  589. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  590. if (WARN_ON(!chanctx_conf)) {
  591. rcu_read_unlock();
  592. return;
  593. }
  594. chan = chanctx_conf->def.chan;
  595. rcu_read_unlock();
  596. sband = local->hw.wiphy->bands[chan->band];
  597. if (assoc_data->supp_rates_len) {
  598. /*
  599. * Get all rates supported by the device and the AP as
  600. * some APs don't like getting a superset of their rates
  601. * in the association request (e.g. D-Link DAP 1353 in
  602. * b-only mode)...
  603. */
  604. rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
  605. assoc_data->supp_rates_len,
  606. sband, &rates);
  607. } else {
  608. /*
  609. * In case AP not provide any supported rates information
  610. * before association, we send information element(s) with
  611. * all rates that we support.
  612. */
  613. rates = ~0;
  614. rates_len = sband->n_bitrates;
  615. }
  616. skb = alloc_skb(local->hw.extra_tx_headroom +
  617. sizeof(*mgmt) + /* bit too much but doesn't matter */
  618. 2 + assoc_data->ssid_len + /* SSID */
  619. 4 + rates_len + /* (extended) rates */
  620. 4 + /* power capability */
  621. 2 + 2 * sband->n_channels + /* supported channels */
  622. 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
  623. 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
  624. assoc_data->ie_len + /* extra IEs */
  625. 9, /* WMM */
  626. GFP_KERNEL);
  627. if (!skb)
  628. return;
  629. skb_reserve(skb, local->hw.extra_tx_headroom);
  630. capab = WLAN_CAPABILITY_ESS;
  631. if (sband->band == IEEE80211_BAND_2GHZ) {
  632. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  633. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  634. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
  635. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  636. }
  637. if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
  638. capab |= WLAN_CAPABILITY_PRIVACY;
  639. if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
  640. (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
  641. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  642. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  643. memset(mgmt, 0, 24);
  644. memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
  645. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  646. memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
  647. if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
  648. skb_put(skb, 10);
  649. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  650. IEEE80211_STYPE_REASSOC_REQ);
  651. mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
  652. mgmt->u.reassoc_req.listen_interval =
  653. cpu_to_le16(local->hw.conf.listen_interval);
  654. memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
  655. ETH_ALEN);
  656. } else {
  657. skb_put(skb, 4);
  658. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  659. IEEE80211_STYPE_ASSOC_REQ);
  660. mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
  661. mgmt->u.assoc_req.listen_interval =
  662. cpu_to_le16(local->hw.conf.listen_interval);
  663. }
  664. /* SSID */
  665. pos = skb_put(skb, 2 + assoc_data->ssid_len);
  666. *pos++ = WLAN_EID_SSID;
  667. *pos++ = assoc_data->ssid_len;
  668. memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
  669. /* add all rates which were marked to be used above */
  670. supp_rates_len = rates_len;
  671. if (supp_rates_len > 8)
  672. supp_rates_len = 8;
  673. pos = skb_put(skb, supp_rates_len + 2);
  674. *pos++ = WLAN_EID_SUPP_RATES;
  675. *pos++ = supp_rates_len;
  676. count = 0;
  677. for (i = 0; i < sband->n_bitrates; i++) {
  678. if (BIT(i) & rates) {
  679. int rate = sband->bitrates[i].bitrate;
  680. *pos++ = (u8) (rate / 5);
  681. if (++count == 8)
  682. break;
  683. }
  684. }
  685. if (rates_len > count) {
  686. pos = skb_put(skb, rates_len - count + 2);
  687. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  688. *pos++ = rates_len - count;
  689. for (i++; i < sband->n_bitrates; i++) {
  690. if (BIT(i) & rates) {
  691. int rate = sband->bitrates[i].bitrate;
  692. *pos++ = (u8) (rate / 5);
  693. }
  694. }
  695. }
  696. if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
  697. /* 1. power capabilities */
  698. pos = skb_put(skb, 4);
  699. *pos++ = WLAN_EID_PWR_CAPABILITY;
  700. *pos++ = 2;
  701. *pos++ = 0; /* min tx power */
  702. *pos++ = chan->max_power; /* max tx power */
  703. /* 2. supported channels */
  704. /* TODO: get this in reg domain format */
  705. pos = skb_put(skb, 2 * sband->n_channels + 2);
  706. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  707. *pos++ = 2 * sband->n_channels;
  708. for (i = 0; i < sband->n_channels; i++) {
  709. *pos++ = ieee80211_frequency_to_channel(
  710. sband->channels[i].center_freq);
  711. *pos++ = 1; /* one channel in the subband*/
  712. }
  713. }
  714. /* if present, add any custom IEs that go before HT */
  715. if (assoc_data->ie_len && assoc_data->ie) {
  716. static const u8 before_ht[] = {
  717. WLAN_EID_SSID,
  718. WLAN_EID_SUPP_RATES,
  719. WLAN_EID_EXT_SUPP_RATES,
  720. WLAN_EID_PWR_CAPABILITY,
  721. WLAN_EID_SUPPORTED_CHANNELS,
  722. WLAN_EID_RSN,
  723. WLAN_EID_QOS_CAPA,
  724. WLAN_EID_RRM_ENABLED_CAPABILITIES,
  725. WLAN_EID_MOBILITY_DOMAIN,
  726. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  727. };
  728. noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
  729. before_ht, ARRAY_SIZE(before_ht),
  730. offset);
  731. pos = skb_put(skb, noffset - offset);
  732. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  733. offset = noffset;
  734. }
  735. if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  736. !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
  737. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  738. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
  739. ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
  740. sband, chan, sdata->smps_mode);
  741. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  742. ieee80211_add_vht_ie(sdata, skb, sband,
  743. &assoc_data->ap_vht_cap);
  744. /* if present, add any custom non-vendor IEs that go after HT */
  745. if (assoc_data->ie_len && assoc_data->ie) {
  746. noffset = ieee80211_ie_split_vendor(assoc_data->ie,
  747. assoc_data->ie_len,
  748. offset);
  749. pos = skb_put(skb, noffset - offset);
  750. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  751. offset = noffset;
  752. }
  753. if (assoc_data->wmm) {
  754. if (assoc_data->uapsd) {
  755. qos_info = ifmgd->uapsd_queues;
  756. qos_info |= (ifmgd->uapsd_max_sp_len <<
  757. IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
  758. } else {
  759. qos_info = 0;
  760. }
  761. pos = skb_put(skb, 9);
  762. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  763. *pos++ = 7; /* len */
  764. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  765. *pos++ = 0x50;
  766. *pos++ = 0xf2;
  767. *pos++ = 2; /* WME */
  768. *pos++ = 0; /* WME info */
  769. *pos++ = 1; /* WME ver */
  770. *pos++ = qos_info;
  771. }
  772. /* add any remaining custom (i.e. vendor specific here) IEs */
  773. if (assoc_data->ie_len && assoc_data->ie) {
  774. noffset = assoc_data->ie_len;
  775. pos = skb_put(skb, noffset - offset);
  776. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  777. }
  778. drv_mgd_prepare_tx(local, sdata);
  779. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  780. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  781. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
  782. IEEE80211_TX_INTFL_MLME_CONN_TX;
  783. ieee80211_tx_skb(sdata, skb);
  784. }
  785. void ieee80211_send_pspoll(struct ieee80211_local *local,
  786. struct ieee80211_sub_if_data *sdata)
  787. {
  788. struct ieee80211_pspoll *pspoll;
  789. struct sk_buff *skb;
  790. skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
  791. if (!skb)
  792. return;
  793. pspoll = (struct ieee80211_pspoll *) skb->data;
  794. pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  795. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  796. ieee80211_tx_skb(sdata, skb);
  797. }
  798. void ieee80211_send_nullfunc(struct ieee80211_local *local,
  799. struct ieee80211_sub_if_data *sdata,
  800. int powersave)
  801. {
  802. struct sk_buff *skb;
  803. struct ieee80211_hdr_3addr *nullfunc;
  804. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  805. skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
  806. if (!skb)
  807. return;
  808. nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
  809. if (powersave)
  810. nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  811. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  812. IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
  813. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  814. IEEE80211_STA_CONNECTION_POLL))
  815. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
  816. ieee80211_tx_skb(sdata, skb);
  817. }
  818. static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
  819. struct ieee80211_sub_if_data *sdata)
  820. {
  821. struct sk_buff *skb;
  822. struct ieee80211_hdr *nullfunc;
  823. __le16 fc;
  824. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  825. return;
  826. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
  827. if (!skb)
  828. return;
  829. skb_reserve(skb, local->hw.extra_tx_headroom);
  830. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
  831. memset(nullfunc, 0, 30);
  832. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  833. IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  834. nullfunc->frame_control = fc;
  835. memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
  836. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  837. memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
  838. memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
  839. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  840. ieee80211_tx_skb(sdata, skb);
  841. }
  842. /* spectrum management related things */
  843. static void ieee80211_chswitch_work(struct work_struct *work)
  844. {
  845. struct ieee80211_sub_if_data *sdata =
  846. container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
  847. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  848. if (!ieee80211_sdata_running(sdata))
  849. return;
  850. mutex_lock(&ifmgd->mtx);
  851. if (!ifmgd->associated)
  852. goto out;
  853. sdata->local->_oper_channel = sdata->local->csa_channel;
  854. if (!sdata->local->ops->channel_switch) {
  855. /* call "hw_config" only if doing sw channel switch */
  856. ieee80211_hw_config(sdata->local,
  857. IEEE80211_CONF_CHANGE_CHANNEL);
  858. } else {
  859. /* update the device channel directly */
  860. sdata->local->hw.conf.channel = sdata->local->_oper_channel;
  861. }
  862. /* XXX: shouldn't really modify cfg80211-owned data! */
  863. ifmgd->associated->channel = sdata->local->_oper_channel;
  864. /* XXX: wait for a beacon first? */
  865. ieee80211_wake_queues_by_reason(&sdata->local->hw,
  866. IEEE80211_QUEUE_STOP_REASON_CSA);
  867. out:
  868. ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
  869. mutex_unlock(&ifmgd->mtx);
  870. }
  871. void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
  872. {
  873. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  874. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  875. trace_api_chswitch_done(sdata, success);
  876. if (!success) {
  877. sdata_info(sdata,
  878. "driver channel switch failed, disconnecting\n");
  879. ieee80211_queue_work(&sdata->local->hw,
  880. &ifmgd->csa_connection_drop_work);
  881. } else {
  882. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  883. }
  884. }
  885. EXPORT_SYMBOL(ieee80211_chswitch_done);
  886. static void ieee80211_chswitch_timer(unsigned long data)
  887. {
  888. struct ieee80211_sub_if_data *sdata =
  889. (struct ieee80211_sub_if_data *) data;
  890. ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
  891. }
  892. void
  893. ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
  894. const struct ieee80211_channel_sw_ie *sw_elem,
  895. struct ieee80211_bss *bss, u64 timestamp)
  896. {
  897. struct cfg80211_bss *cbss =
  898. container_of((void *)bss, struct cfg80211_bss, priv);
  899. struct ieee80211_channel *new_ch;
  900. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  901. int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
  902. cbss->channel->band);
  903. struct ieee80211_chanctx *chanctx;
  904. ASSERT_MGD_MTX(ifmgd);
  905. if (!ifmgd->associated)
  906. return;
  907. if (sdata->local->scanning)
  908. return;
  909. /* Disregard subsequent beacons if we are already running a timer
  910. processing a CSA */
  911. if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
  912. return;
  913. new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
  914. if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
  915. sdata_info(sdata,
  916. "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
  917. ifmgd->associated->bssid, new_freq);
  918. ieee80211_queue_work(&sdata->local->hw,
  919. &ifmgd->csa_connection_drop_work);
  920. return;
  921. }
  922. ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
  923. if (sdata->local->use_chanctx) {
  924. sdata_info(sdata,
  925. "not handling channel switch with channel contexts\n");
  926. ieee80211_queue_work(&sdata->local->hw,
  927. &ifmgd->csa_connection_drop_work);
  928. return;
  929. }
  930. mutex_lock(&sdata->local->chanctx_mtx);
  931. if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
  932. mutex_unlock(&sdata->local->chanctx_mtx);
  933. return;
  934. }
  935. chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
  936. struct ieee80211_chanctx, conf);
  937. if (chanctx->refcount > 1) {
  938. sdata_info(sdata,
  939. "channel switch with multiple interfaces on the same channel, disconnecting\n");
  940. ieee80211_queue_work(&sdata->local->hw,
  941. &ifmgd->csa_connection_drop_work);
  942. mutex_unlock(&sdata->local->chanctx_mtx);
  943. return;
  944. }
  945. mutex_unlock(&sdata->local->chanctx_mtx);
  946. sdata->local->csa_channel = new_ch;
  947. if (sw_elem->mode)
  948. ieee80211_stop_queues_by_reason(&sdata->local->hw,
  949. IEEE80211_QUEUE_STOP_REASON_CSA);
  950. if (sdata->local->ops->channel_switch) {
  951. /* use driver's channel switch callback */
  952. struct ieee80211_channel_switch ch_switch = {
  953. .timestamp = timestamp,
  954. .block_tx = sw_elem->mode,
  955. .channel = new_ch,
  956. .count = sw_elem->count,
  957. };
  958. drv_channel_switch(sdata->local, &ch_switch);
  959. return;
  960. }
  961. /* channel switch handled in software */
  962. if (sw_elem->count <= 1)
  963. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  964. else
  965. mod_timer(&ifmgd->chswitch_timer,
  966. TU_TO_EXP_TIME(sw_elem->count *
  967. cbss->beacon_interval));
  968. }
  969. static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
  970. struct ieee80211_channel *channel,
  971. const u8 *country_ie, u8 country_ie_len,
  972. const u8 *pwr_constr_elem)
  973. {
  974. struct ieee80211_country_ie_triplet *triplet;
  975. int chan = ieee80211_frequency_to_channel(channel->center_freq);
  976. int i, chan_pwr, chan_increment, new_ap_level;
  977. bool have_chan_pwr = false;
  978. /* Invalid IE */
  979. if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  980. return 0;
  981. triplet = (void *)(country_ie + 3);
  982. country_ie_len -= 3;
  983. switch (channel->band) {
  984. default:
  985. WARN_ON_ONCE(1);
  986. /* fall through */
  987. case IEEE80211_BAND_2GHZ:
  988. case IEEE80211_BAND_60GHZ:
  989. chan_increment = 1;
  990. break;
  991. case IEEE80211_BAND_5GHZ:
  992. chan_increment = 4;
  993. break;
  994. }
  995. /* find channel */
  996. while (country_ie_len >= 3) {
  997. u8 first_channel = triplet->chans.first_channel;
  998. if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
  999. goto next;
  1000. for (i = 0; i < triplet->chans.num_channels; i++) {
  1001. if (first_channel + i * chan_increment == chan) {
  1002. have_chan_pwr = true;
  1003. chan_pwr = triplet->chans.max_power;
  1004. break;
  1005. }
  1006. }
  1007. if (have_chan_pwr)
  1008. break;
  1009. next:
  1010. triplet++;
  1011. country_ie_len -= 3;
  1012. }
  1013. if (!have_chan_pwr)
  1014. return 0;
  1015. new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
  1016. if (sdata->ap_power_level == new_ap_level)
  1017. return 0;
  1018. sdata_info(sdata,
  1019. "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
  1020. new_ap_level, chan_pwr, *pwr_constr_elem,
  1021. sdata->u.mgd.bssid);
  1022. sdata->ap_power_level = new_ap_level;
  1023. if (__ieee80211_recalc_txpower(sdata))
  1024. return BSS_CHANGED_TXPOWER;
  1025. return 0;
  1026. }
  1027. /* powersave */
  1028. static void ieee80211_enable_ps(struct ieee80211_local *local,
  1029. struct ieee80211_sub_if_data *sdata)
  1030. {
  1031. struct ieee80211_conf *conf = &local->hw.conf;
  1032. /*
  1033. * If we are scanning right now then the parameters will
  1034. * take effect when scan finishes.
  1035. */
  1036. if (local->scanning)
  1037. return;
  1038. if (conf->dynamic_ps_timeout > 0 &&
  1039. !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
  1040. mod_timer(&local->dynamic_ps_timer, jiffies +
  1041. msecs_to_jiffies(conf->dynamic_ps_timeout));
  1042. } else {
  1043. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  1044. ieee80211_send_nullfunc(local, sdata, 1);
  1045. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  1046. (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
  1047. return;
  1048. conf->flags |= IEEE80211_CONF_PS;
  1049. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1050. }
  1051. }
  1052. static void ieee80211_change_ps(struct ieee80211_local *local)
  1053. {
  1054. struct ieee80211_conf *conf = &local->hw.conf;
  1055. if (local->ps_sdata) {
  1056. ieee80211_enable_ps(local, local->ps_sdata);
  1057. } else if (conf->flags & IEEE80211_CONF_PS) {
  1058. conf->flags &= ~IEEE80211_CONF_PS;
  1059. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1060. del_timer_sync(&local->dynamic_ps_timer);
  1061. cancel_work_sync(&local->dynamic_ps_enable_work);
  1062. }
  1063. }
  1064. static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
  1065. {
  1066. struct ieee80211_if_managed *mgd = &sdata->u.mgd;
  1067. struct sta_info *sta = NULL;
  1068. bool authorized = false;
  1069. if (!mgd->powersave)
  1070. return false;
  1071. if (mgd->broken_ap)
  1072. return false;
  1073. if (!mgd->associated)
  1074. return false;
  1075. if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
  1076. IEEE80211_STA_CONNECTION_POLL))
  1077. return false;
  1078. rcu_read_lock();
  1079. sta = sta_info_get(sdata, mgd->bssid);
  1080. if (sta)
  1081. authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
  1082. rcu_read_unlock();
  1083. return authorized;
  1084. }
  1085. /* need to hold RTNL or interface lock */
  1086. void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
  1087. {
  1088. struct ieee80211_sub_if_data *sdata, *found = NULL;
  1089. int count = 0;
  1090. int timeout;
  1091. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
  1092. local->ps_sdata = NULL;
  1093. return;
  1094. }
  1095. list_for_each_entry(sdata, &local->interfaces, list) {
  1096. if (!ieee80211_sdata_running(sdata))
  1097. continue;
  1098. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1099. /* If an AP vif is found, then disable PS
  1100. * by setting the count to zero thereby setting
  1101. * ps_sdata to NULL.
  1102. */
  1103. count = 0;
  1104. break;
  1105. }
  1106. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1107. continue;
  1108. found = sdata;
  1109. count++;
  1110. }
  1111. if (count == 1 && ieee80211_powersave_allowed(found)) {
  1112. s32 beaconint_us;
  1113. if (latency < 0)
  1114. latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
  1115. beaconint_us = ieee80211_tu_to_usec(
  1116. found->vif.bss_conf.beacon_int);
  1117. timeout = local->dynamic_ps_forced_timeout;
  1118. if (timeout < 0) {
  1119. /*
  1120. * Go to full PSM if the user configures a very low
  1121. * latency requirement.
  1122. * The 2000 second value is there for compatibility
  1123. * until the PM_QOS_NETWORK_LATENCY is configured
  1124. * with real values.
  1125. */
  1126. if (latency > (1900 * USEC_PER_MSEC) &&
  1127. latency != (2000 * USEC_PER_SEC))
  1128. timeout = 0;
  1129. else
  1130. timeout = 100;
  1131. }
  1132. local->hw.conf.dynamic_ps_timeout = timeout;
  1133. if (beaconint_us > latency) {
  1134. local->ps_sdata = NULL;
  1135. } else {
  1136. int maxslp = 1;
  1137. u8 dtimper = found->u.mgd.dtim_period;
  1138. /* If the TIM IE is invalid, pretend the value is 1 */
  1139. if (!dtimper)
  1140. dtimper = 1;
  1141. else if (dtimper > 1)
  1142. maxslp = min_t(int, dtimper,
  1143. latency / beaconint_us);
  1144. local->hw.conf.max_sleep_period = maxslp;
  1145. local->hw.conf.ps_dtim_period = dtimper;
  1146. local->ps_sdata = found;
  1147. }
  1148. } else {
  1149. local->ps_sdata = NULL;
  1150. }
  1151. ieee80211_change_ps(local);
  1152. }
  1153. void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
  1154. {
  1155. bool ps_allowed = ieee80211_powersave_allowed(sdata);
  1156. if (sdata->vif.bss_conf.ps != ps_allowed) {
  1157. sdata->vif.bss_conf.ps = ps_allowed;
  1158. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
  1159. }
  1160. }
  1161. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  1162. {
  1163. struct ieee80211_local *local =
  1164. container_of(work, struct ieee80211_local,
  1165. dynamic_ps_disable_work);
  1166. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1167. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1168. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1169. }
  1170. ieee80211_wake_queues_by_reason(&local->hw,
  1171. IEEE80211_QUEUE_STOP_REASON_PS);
  1172. }
  1173. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  1174. {
  1175. struct ieee80211_local *local =
  1176. container_of(work, struct ieee80211_local,
  1177. dynamic_ps_enable_work);
  1178. struct ieee80211_sub_if_data *sdata = local->ps_sdata;
  1179. struct ieee80211_if_managed *ifmgd;
  1180. unsigned long flags;
  1181. int q;
  1182. /* can only happen when PS was just disabled anyway */
  1183. if (!sdata)
  1184. return;
  1185. ifmgd = &sdata->u.mgd;
  1186. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  1187. return;
  1188. if (local->hw.conf.dynamic_ps_timeout > 0) {
  1189. /* don't enter PS if TX frames are pending */
  1190. if (drv_tx_frames_pending(local)) {
  1191. mod_timer(&local->dynamic_ps_timer, jiffies +
  1192. msecs_to_jiffies(
  1193. local->hw.conf.dynamic_ps_timeout));
  1194. return;
  1195. }
  1196. /*
  1197. * transmission can be stopped by others which leads to
  1198. * dynamic_ps_timer expiry. Postpone the ps timer if it
  1199. * is not the actual idle state.
  1200. */
  1201. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  1202. for (q = 0; q < local->hw.queues; q++) {
  1203. if (local->queue_stop_reasons[q]) {
  1204. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  1205. flags);
  1206. mod_timer(&local->dynamic_ps_timer, jiffies +
  1207. msecs_to_jiffies(
  1208. local->hw.conf.dynamic_ps_timeout));
  1209. return;
  1210. }
  1211. }
  1212. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  1213. }
  1214. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  1215. !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  1216. netif_tx_stop_all_queues(sdata->dev);
  1217. if (drv_tx_frames_pending(local))
  1218. mod_timer(&local->dynamic_ps_timer, jiffies +
  1219. msecs_to_jiffies(
  1220. local->hw.conf.dynamic_ps_timeout));
  1221. else {
  1222. ieee80211_send_nullfunc(local, sdata, 1);
  1223. /* Flush to get the tx status of nullfunc frame */
  1224. drv_flush(local, false);
  1225. }
  1226. }
  1227. if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
  1228. (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
  1229. (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  1230. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  1231. local->hw.conf.flags |= IEEE80211_CONF_PS;
  1232. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1233. }
  1234. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  1235. netif_tx_wake_all_queues(sdata->dev);
  1236. }
  1237. void ieee80211_dynamic_ps_timer(unsigned long data)
  1238. {
  1239. struct ieee80211_local *local = (void *) data;
  1240. if (local->quiescing || local->suspended)
  1241. return;
  1242. ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
  1243. }
  1244. void ieee80211_dfs_cac_timer_work(struct work_struct *work)
  1245. {
  1246. struct delayed_work *delayed_work =
  1247. container_of(work, struct delayed_work, work);
  1248. struct ieee80211_sub_if_data *sdata =
  1249. container_of(delayed_work, struct ieee80211_sub_if_data,
  1250. dfs_cac_timer_work);
  1251. ieee80211_vif_release_channel(sdata);
  1252. cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
  1253. }
  1254. /* MLME */
  1255. static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
  1256. struct ieee80211_sub_if_data *sdata,
  1257. const u8 *wmm_param, size_t wmm_param_len)
  1258. {
  1259. struct ieee80211_tx_queue_params params;
  1260. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1261. size_t left;
  1262. int count;
  1263. const u8 *pos;
  1264. u8 uapsd_queues = 0;
  1265. if (!local->ops->conf_tx)
  1266. return false;
  1267. if (local->hw.queues < IEEE80211_NUM_ACS)
  1268. return false;
  1269. if (!wmm_param)
  1270. return false;
  1271. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  1272. return false;
  1273. if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
  1274. uapsd_queues = ifmgd->uapsd_queues;
  1275. count = wmm_param[6] & 0x0f;
  1276. if (count == ifmgd->wmm_last_param_set)
  1277. return false;
  1278. ifmgd->wmm_last_param_set = count;
  1279. pos = wmm_param + 8;
  1280. left = wmm_param_len - 8;
  1281. memset(&params, 0, sizeof(params));
  1282. sdata->wmm_acm = 0;
  1283. for (; left >= 4; left -= 4, pos += 4) {
  1284. int aci = (pos[0] >> 5) & 0x03;
  1285. int acm = (pos[0] >> 4) & 0x01;
  1286. bool uapsd = false;
  1287. int queue;
  1288. switch (aci) {
  1289. case 1: /* AC_BK */
  1290. queue = 3;
  1291. if (acm)
  1292. sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
  1293. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
  1294. uapsd = true;
  1295. break;
  1296. case 2: /* AC_VI */
  1297. queue = 1;
  1298. if (acm)
  1299. sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
  1300. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
  1301. uapsd = true;
  1302. break;
  1303. case 3: /* AC_VO */
  1304. queue = 0;
  1305. if (acm)
  1306. sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
  1307. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  1308. uapsd = true;
  1309. break;
  1310. case 0: /* AC_BE */
  1311. default:
  1312. queue = 2;
  1313. if (acm)
  1314. sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
  1315. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
  1316. uapsd = true;
  1317. break;
  1318. }
  1319. params.aifs = pos[0] & 0x0f;
  1320. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  1321. params.cw_min = ecw2cw(pos[1] & 0x0f);
  1322. params.txop = get_unaligned_le16(pos + 2);
  1323. params.uapsd = uapsd;
  1324. mlme_dbg(sdata,
  1325. "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
  1326. queue, aci, acm,
  1327. params.aifs, params.cw_min, params.cw_max,
  1328. params.txop, params.uapsd);
  1329. sdata->tx_conf[queue] = params;
  1330. if (drv_conf_tx(local, sdata, queue, &params))
  1331. sdata_err(sdata,
  1332. "failed to set TX queue parameters for queue %d\n",
  1333. queue);
  1334. }
  1335. /* enable WMM or activate new settings */
  1336. sdata->vif.bss_conf.qos = true;
  1337. return true;
  1338. }
  1339. static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
  1340. {
  1341. lockdep_assert_held(&sdata->local->mtx);
  1342. sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  1343. IEEE80211_STA_BEACON_POLL);
  1344. ieee80211_run_deferred_scan(sdata->local);
  1345. }
  1346. static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
  1347. {
  1348. mutex_lock(&sdata->local->mtx);
  1349. __ieee80211_stop_poll(sdata);
  1350. mutex_unlock(&sdata->local->mtx);
  1351. }
  1352. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  1353. u16 capab, bool erp_valid, u8 erp)
  1354. {
  1355. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1356. u32 changed = 0;
  1357. bool use_protection;
  1358. bool use_short_preamble;
  1359. bool use_short_slot;
  1360. if (erp_valid) {
  1361. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  1362. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  1363. } else {
  1364. use_protection = false;
  1365. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  1366. }
  1367. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  1368. if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
  1369. use_short_slot = true;
  1370. if (use_protection != bss_conf->use_cts_prot) {
  1371. bss_conf->use_cts_prot = use_protection;
  1372. changed |= BSS_CHANGED_ERP_CTS_PROT;
  1373. }
  1374. if (use_short_preamble != bss_conf->use_short_preamble) {
  1375. bss_conf->use_short_preamble = use_short_preamble;
  1376. changed |= BSS_CHANGED_ERP_PREAMBLE;
  1377. }
  1378. if (use_short_slot != bss_conf->use_short_slot) {
  1379. bss_conf->use_short_slot = use_short_slot;
  1380. changed |= BSS_CHANGED_ERP_SLOT;
  1381. }
  1382. return changed;
  1383. }
  1384. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  1385. struct cfg80211_bss *cbss,
  1386. u32 bss_info_changed)
  1387. {
  1388. struct ieee80211_bss *bss = (void *)cbss->priv;
  1389. struct ieee80211_local *local = sdata->local;
  1390. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1391. bss_info_changed |= BSS_CHANGED_ASSOC;
  1392. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  1393. bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
  1394. sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
  1395. IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
  1396. sdata->u.mgd.associated = cbss;
  1397. memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
  1398. sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
  1399. if (sdata->vif.p2p) {
  1400. const struct cfg80211_bss_ies *ies;
  1401. rcu_read_lock();
  1402. ies = rcu_dereference(cbss->ies);
  1403. if (ies) {
  1404. u8 noa[2];
  1405. int ret;
  1406. ret = cfg80211_get_p2p_attr(
  1407. ies->data, ies->len,
  1408. IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
  1409. noa, sizeof(noa));
  1410. if (ret >= 2) {
  1411. bss_conf->p2p_oppps = noa[1] & 0x80;
  1412. bss_conf->p2p_ctwindow = noa[1] & 0x7f;
  1413. bss_info_changed |= BSS_CHANGED_P2P_PS;
  1414. sdata->u.mgd.p2p_noa_index = noa[0];
  1415. }
  1416. }
  1417. rcu_read_unlock();
  1418. }
  1419. /* just to be sure */
  1420. ieee80211_stop_poll(sdata);
  1421. ieee80211_led_assoc(local, 1);
  1422. if (sdata->u.mgd.assoc_data->have_beacon) {
  1423. /*
  1424. * If the AP is buggy we may get here with no DTIM period
  1425. * known, so assume it's 1 which is the only safe assumption
  1426. * in that case, although if the TIM IE is broken powersave
  1427. * probably just won't work at all.
  1428. */
  1429. bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
  1430. bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
  1431. } else {
  1432. bss_conf->dtim_period = 0;
  1433. }
  1434. bss_conf->assoc = 1;
  1435. /* Tell the driver to monitor connection quality (if supported) */
  1436. if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
  1437. bss_conf->cqm_rssi_thold)
  1438. bss_info_changed |= BSS_CHANGED_CQM;
  1439. /* Enable ARP filtering */
  1440. if (bss_conf->arp_addr_cnt)
  1441. bss_info_changed |= BSS_CHANGED_ARP_FILTER;
  1442. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  1443. mutex_lock(&local->iflist_mtx);
  1444. ieee80211_recalc_ps(local, -1);
  1445. mutex_unlock(&local->iflist_mtx);
  1446. ieee80211_recalc_smps(sdata);
  1447. ieee80211_recalc_ps_vif(sdata);
  1448. netif_tx_start_all_queues(sdata->dev);
  1449. netif_carrier_on(sdata->dev);
  1450. }
  1451. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  1452. u16 stype, u16 reason, bool tx,
  1453. u8 *frame_buf)
  1454. {
  1455. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1456. struct ieee80211_local *local = sdata->local;
  1457. u32 changed = 0;
  1458. ASSERT_MGD_MTX(ifmgd);
  1459. if (WARN_ON_ONCE(tx && !frame_buf))
  1460. return;
  1461. if (WARN_ON(!ifmgd->associated))
  1462. return;
  1463. ieee80211_stop_poll(sdata);
  1464. ifmgd->associated = NULL;
  1465. /*
  1466. * we need to commit the associated = NULL change because the
  1467. * scan code uses that to determine whether this iface should
  1468. * go to/wake up from powersave or not -- and could otherwise
  1469. * wake the queues erroneously.
  1470. */
  1471. smp_mb();
  1472. /*
  1473. * Thus, we can only afterwards stop the queues -- to account
  1474. * for the case where another CPU is finishing a scan at this
  1475. * time -- we don't want the scan code to enable queues.
  1476. */
  1477. netif_tx_stop_all_queues(sdata->dev);
  1478. netif_carrier_off(sdata->dev);
  1479. /*
  1480. * if we want to get out of ps before disassoc (why?) we have
  1481. * to do it before sending disassoc, as otherwise the null-packet
  1482. * won't be valid.
  1483. */
  1484. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1485. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1486. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1487. }
  1488. local->ps_sdata = NULL;
  1489. /* disable per-vif ps */
  1490. ieee80211_recalc_ps_vif(sdata);
  1491. /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
  1492. if (tx)
  1493. drv_flush(local, false);
  1494. /* deauthenticate/disassociate now */
  1495. if (tx || frame_buf)
  1496. ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
  1497. reason, tx, frame_buf);
  1498. /* flush out frame */
  1499. if (tx)
  1500. drv_flush(local, false);
  1501. /* clear bssid only after building the needed mgmt frames */
  1502. memset(ifmgd->bssid, 0, ETH_ALEN);
  1503. /* remove AP and TDLS peers */
  1504. sta_info_flush_defer(sdata);
  1505. /* finally reset all BSS / config parameters */
  1506. changed |= ieee80211_reset_erp_info(sdata);
  1507. ieee80211_led_assoc(local, 0);
  1508. changed |= BSS_CHANGED_ASSOC;
  1509. sdata->vif.bss_conf.assoc = false;
  1510. sdata->vif.bss_conf.p2p_ctwindow = 0;
  1511. sdata->vif.bss_conf.p2p_oppps = false;
  1512. /* on the next assoc, re-program HT/VHT parameters */
  1513. memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
  1514. memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
  1515. memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
  1516. memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
  1517. sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
  1518. del_timer_sync(&local->dynamic_ps_timer);
  1519. cancel_work_sync(&local->dynamic_ps_enable_work);
  1520. /* Disable ARP filtering */
  1521. if (sdata->vif.bss_conf.arp_addr_cnt)
  1522. changed |= BSS_CHANGED_ARP_FILTER;
  1523. sdata->vif.bss_conf.qos = false;
  1524. changed |= BSS_CHANGED_QOS;
  1525. /* The BSSID (not really interesting) and HT changed */
  1526. changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
  1527. ieee80211_bss_info_change_notify(sdata, changed);
  1528. /* disassociated - set to defaults now */
  1529. ieee80211_set_wmm_default(sdata, false);
  1530. del_timer_sync(&sdata->u.mgd.conn_mon_timer);
  1531. del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
  1532. del_timer_sync(&sdata->u.mgd.timer);
  1533. del_timer_sync(&sdata->u.mgd.chswitch_timer);
  1534. sdata->vif.bss_conf.dtim_period = 0;
  1535. ifmgd->flags = 0;
  1536. ieee80211_vif_release_channel(sdata);
  1537. }
  1538. void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
  1539. struct ieee80211_hdr *hdr)
  1540. {
  1541. /*
  1542. * We can postpone the mgd.timer whenever receiving unicast frames
  1543. * from AP because we know that the connection is working both ways
  1544. * at that time. But multicast frames (and hence also beacons) must
  1545. * be ignored here, because we need to trigger the timer during
  1546. * data idle periods for sending the periodic probe request to the
  1547. * AP we're connected to.
  1548. */
  1549. if (is_multicast_ether_addr(hdr->addr1))
  1550. return;
  1551. ieee80211_sta_reset_conn_monitor(sdata);
  1552. }
  1553. static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
  1554. {
  1555. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1556. struct ieee80211_local *local = sdata->local;
  1557. mutex_lock(&local->mtx);
  1558. if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  1559. IEEE80211_STA_CONNECTION_POLL))) {
  1560. mutex_unlock(&local->mtx);
  1561. return;
  1562. }
  1563. __ieee80211_stop_poll(sdata);
  1564. mutex_lock(&local->iflist_mtx);
  1565. ieee80211_recalc_ps(local, -1);
  1566. mutex_unlock(&local->iflist_mtx);
  1567. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  1568. goto out;
  1569. /*
  1570. * We've received a probe response, but are not sure whether
  1571. * we have or will be receiving any beacons or data, so let's
  1572. * schedule the timers again, just in case.
  1573. */
  1574. ieee80211_sta_reset_beacon_monitor(sdata);
  1575. mod_timer(&ifmgd->conn_mon_timer,
  1576. round_jiffies_up(jiffies +
  1577. IEEE80211_CONNECTION_IDLE_TIME));
  1578. out:
  1579. mutex_unlock(&local->mtx);
  1580. }
  1581. void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
  1582. struct ieee80211_hdr *hdr, bool ack)
  1583. {
  1584. if (!ieee80211_is_data(hdr->frame_control))
  1585. return;
  1586. if (ieee80211_is_nullfunc(hdr->frame_control) &&
  1587. sdata->u.mgd.probe_send_count > 0) {
  1588. if (ack)
  1589. ieee80211_sta_reset_conn_monitor(sdata);
  1590. else
  1591. sdata->u.mgd.nullfunc_failed = true;
  1592. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  1593. return;
  1594. }
  1595. if (ack)
  1596. ieee80211_sta_reset_conn_monitor(sdata);
  1597. }
  1598. static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
  1599. {
  1600. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1601. const u8 *ssid;
  1602. u8 *dst = ifmgd->associated->bssid;
  1603. u8 unicast_limit = max(1, max_probe_tries - 3);
  1604. /*
  1605. * Try sending broadcast probe requests for the last three
  1606. * probe requests after the first ones failed since some
  1607. * buggy APs only support broadcast probe requests.
  1608. */
  1609. if (ifmgd->probe_send_count >= unicast_limit)
  1610. dst = NULL;
  1611. /*
  1612. * When the hardware reports an accurate Tx ACK status, it's
  1613. * better to send a nullfunc frame instead of a probe request,
  1614. * as it will kick us off the AP quickly if we aren't associated
  1615. * anymore. The timeout will be reset if the frame is ACKed by
  1616. * the AP.
  1617. */
  1618. ifmgd->probe_send_count++;
  1619. if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  1620. ifmgd->nullfunc_failed = false;
  1621. ieee80211_send_nullfunc(sdata->local, sdata, 0);
  1622. } else {
  1623. int ssid_len;
  1624. rcu_read_lock();
  1625. ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
  1626. if (WARN_ON_ONCE(ssid == NULL))
  1627. ssid_len = 0;
  1628. else
  1629. ssid_len = ssid[1];
  1630. ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
  1631. 0, (u32) -1, true, 0,
  1632. ifmgd->associated->channel, false);
  1633. rcu_read_unlock();
  1634. }
  1635. ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
  1636. run_again(ifmgd, ifmgd->probe_timeout);
  1637. if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  1638. drv_flush(sdata->local, false);
  1639. }
  1640. static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
  1641. bool beacon)
  1642. {
  1643. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1644. bool already = false;
  1645. if (!ieee80211_sdata_running(sdata))
  1646. return;
  1647. mutex_lock(&ifmgd->mtx);
  1648. if (!ifmgd->associated)
  1649. goto out;
  1650. mutex_lock(&sdata->local->mtx);
  1651. if (sdata->local->tmp_channel || sdata->local->scanning) {
  1652. mutex_unlock(&sdata->local->mtx);
  1653. goto out;
  1654. }
  1655. if (beacon)
  1656. mlme_dbg_ratelimited(sdata,
  1657. "detected beacon loss from AP - probing\n");
  1658. ieee80211_cqm_rssi_notify(&sdata->vif,
  1659. NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
  1660. /*
  1661. * The driver/our work has already reported this event or the
  1662. * connection monitoring has kicked in and we have already sent
  1663. * a probe request. Or maybe the AP died and the driver keeps
  1664. * reporting until we disassociate...
  1665. *
  1666. * In either case we have to ignore the current call to this
  1667. * function (except for setting the correct probe reason bit)
  1668. * because otherwise we would reset the timer every time and
  1669. * never check whether we received a probe response!
  1670. */
  1671. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  1672. IEEE80211_STA_CONNECTION_POLL))
  1673. already = true;
  1674. if (beacon)
  1675. ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
  1676. else
  1677. ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
  1678. mutex_unlock(&sdata->local->mtx);
  1679. if (already)
  1680. goto out;
  1681. mutex_lock(&sdata->local->iflist_mtx);
  1682. ieee80211_recalc_ps(sdata->local, -1);
  1683. mutex_unlock(&sdata->local->iflist_mtx);
  1684. ifmgd->probe_send_count = 0;
  1685. ieee80211_mgd_probe_ap_send(sdata);
  1686. out:
  1687. mutex_unlock(&ifmgd->mtx);
  1688. }
  1689. struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
  1690. struct ieee80211_vif *vif)
  1691. {
  1692. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1693. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1694. struct cfg80211_bss *cbss;
  1695. struct sk_buff *skb;
  1696. const u8 *ssid;
  1697. int ssid_len;
  1698. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1699. return NULL;
  1700. ASSERT_MGD_MTX(ifmgd);
  1701. if (ifmgd->associated)
  1702. cbss = ifmgd->associated;
  1703. else if (ifmgd->auth_data)
  1704. cbss = ifmgd->auth_data->bss;
  1705. else if (ifmgd->assoc_data)
  1706. cbss = ifmgd->assoc_data->bss;
  1707. else
  1708. return NULL;
  1709. rcu_read_lock();
  1710. ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
  1711. if (WARN_ON_ONCE(ssid == NULL))
  1712. ssid_len = 0;
  1713. else
  1714. ssid_len = ssid[1];
  1715. skb = ieee80211_build_probe_req(sdata, cbss->bssid,
  1716. (u32) -1, cbss->channel,
  1717. ssid + 2, ssid_len,
  1718. NULL, 0, true);
  1719. rcu_read_unlock();
  1720. return skb;
  1721. }
  1722. EXPORT_SYMBOL(ieee80211_ap_probereq_get);
  1723. static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
  1724. {
  1725. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1726. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  1727. mutex_lock(&ifmgd->mtx);
  1728. if (!ifmgd->associated) {
  1729. mutex_unlock(&ifmgd->mtx);
  1730. return;
  1731. }
  1732. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  1733. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  1734. true, frame_buf);
  1735. ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
  1736. ieee80211_wake_queues_by_reason(&sdata->local->hw,
  1737. IEEE80211_QUEUE_STOP_REASON_CSA);
  1738. mutex_unlock(&ifmgd->mtx);
  1739. /*
  1740. * must be outside lock due to cfg80211,
  1741. * but that's not a problem.
  1742. */
  1743. cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
  1744. }
  1745. static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
  1746. {
  1747. struct ieee80211_sub_if_data *sdata =
  1748. container_of(work, struct ieee80211_sub_if_data,
  1749. u.mgd.beacon_connection_loss_work);
  1750. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1751. struct sta_info *sta;
  1752. if (ifmgd->associated) {
  1753. rcu_read_lock();
  1754. sta = sta_info_get(sdata, ifmgd->bssid);
  1755. if (sta)
  1756. sta->beacon_loss_count++;
  1757. rcu_read_unlock();
  1758. }
  1759. if (ifmgd->connection_loss) {
  1760. sdata_info(sdata, "Connection to AP %pM lost\n",
  1761. ifmgd->bssid);
  1762. __ieee80211_disconnect(sdata);
  1763. } else {
  1764. ieee80211_mgd_probe_ap(sdata, true);
  1765. }
  1766. }
  1767. static void ieee80211_csa_connection_drop_work(struct work_struct *work)
  1768. {
  1769. struct ieee80211_sub_if_data *sdata =
  1770. container_of(work, struct ieee80211_sub_if_data,
  1771. u.mgd.csa_connection_drop_work);
  1772. __ieee80211_disconnect(sdata);
  1773. }
  1774. void ieee80211_beacon_loss(struct ieee80211_vif *vif)
  1775. {
  1776. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1777. struct ieee80211_hw *hw = &sdata->local->hw;
  1778. trace_api_beacon_loss(sdata);
  1779. WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
  1780. sdata->u.mgd.connection_loss = false;
  1781. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1782. }
  1783. EXPORT_SYMBOL(ieee80211_beacon_loss);
  1784. void ieee80211_connection_loss(struct ieee80211_vif *vif)
  1785. {
  1786. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1787. struct ieee80211_hw *hw = &sdata->local->hw;
  1788. trace_api_connection_loss(sdata);
  1789. sdata->u.mgd.connection_loss = true;
  1790. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1791. }
  1792. EXPORT_SYMBOL(ieee80211_connection_loss);
  1793. static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
  1794. bool assoc)
  1795. {
  1796. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  1797. lockdep_assert_held(&sdata->u.mgd.mtx);
  1798. if (!assoc) {
  1799. sta_info_destroy_addr(sdata, auth_data->bss->bssid);
  1800. memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
  1801. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  1802. sdata->u.mgd.flags = 0;
  1803. ieee80211_vif_release_channel(sdata);
  1804. }
  1805. cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
  1806. kfree(auth_data);
  1807. sdata->u.mgd.auth_data = NULL;
  1808. }
  1809. static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
  1810. struct ieee80211_mgmt *mgmt, size_t len)
  1811. {
  1812. struct ieee80211_local *local = sdata->local;
  1813. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  1814. u8 *pos;
  1815. struct ieee802_11_elems elems;
  1816. u32 tx_flags = 0;
  1817. pos = mgmt->u.auth.variable;
  1818. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1819. if (!elems.challenge)
  1820. return;
  1821. auth_data->expected_transaction = 4;
  1822. drv_mgd_prepare_tx(sdata->local, sdata);
  1823. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  1824. tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
  1825. IEEE80211_TX_INTFL_MLME_CONN_TX;
  1826. ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
  1827. elems.challenge - 2, elems.challenge_len + 2,
  1828. auth_data->bss->bssid, auth_data->bss->bssid,
  1829. auth_data->key, auth_data->key_len,
  1830. auth_data->key_idx, tx_flags);
  1831. }
  1832. static enum rx_mgmt_action __must_check
  1833. ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
  1834. struct ieee80211_mgmt *mgmt, size_t len)
  1835. {
  1836. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1837. u8 bssid[ETH_ALEN];
  1838. u16 auth_alg, auth_transaction, status_code;
  1839. struct sta_info *sta;
  1840. lockdep_assert_held(&ifmgd->mtx);
  1841. if (len < 24 + 6)
  1842. return RX_MGMT_NONE;
  1843. if (!ifmgd->auth_data || ifmgd->auth_data->done)
  1844. return RX_MGMT_NONE;
  1845. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  1846. if (!ether_addr_equal(bssid, mgmt->bssid))
  1847. return RX_MGMT_NONE;
  1848. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  1849. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  1850. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  1851. if (auth_alg != ifmgd->auth_data->algorithm ||
  1852. auth_transaction != ifmgd->auth_data->expected_transaction) {
  1853. sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
  1854. mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
  1855. auth_transaction,
  1856. ifmgd->auth_data->expected_transaction);
  1857. return RX_MGMT_NONE;
  1858. }
  1859. if (status_code != WLAN_STATUS_SUCCESS) {
  1860. sdata_info(sdata, "%pM denied authentication (status %d)\n",
  1861. mgmt->sa, status_code);
  1862. ieee80211_destroy_auth_data(sdata, false);
  1863. return RX_MGMT_CFG80211_RX_AUTH;
  1864. }
  1865. switch (ifmgd->auth_data->algorithm) {
  1866. case WLAN_AUTH_OPEN:
  1867. case WLAN_AUTH_LEAP:
  1868. case WLAN_AUTH_FT:
  1869. case WLAN_AUTH_SAE:
  1870. break;
  1871. case WLAN_AUTH_SHARED_KEY:
  1872. if (ifmgd->auth_data->expected_transaction != 4) {
  1873. ieee80211_auth_challenge(sdata, mgmt, len);
  1874. /* need another frame */
  1875. return RX_MGMT_NONE;
  1876. }
  1877. break;
  1878. default:
  1879. WARN_ONCE(1, "invalid auth alg %d",
  1880. ifmgd->auth_data->algorithm);
  1881. return RX_MGMT_NONE;
  1882. }
  1883. sdata_info(sdata, "authenticated\n");
  1884. ifmgd->auth_data->done = true;
  1885. ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
  1886. ifmgd->auth_data->timeout_started = true;
  1887. run_again(ifmgd, ifmgd->auth_data->timeout);
  1888. if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
  1889. ifmgd->auth_data->expected_transaction != 2) {
  1890. /*
  1891. * Report auth frame to user space for processing since another
  1892. * round of Authentication frames is still needed.
  1893. */
  1894. return RX_MGMT_CFG80211_RX_AUTH;
  1895. }
  1896. /* move station state to auth */
  1897. mutex_lock(&sdata->local->sta_mtx);
  1898. sta = sta_info_get(sdata, bssid);
  1899. if (!sta) {
  1900. WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
  1901. goto out_err;
  1902. }
  1903. if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
  1904. sdata_info(sdata, "failed moving %pM to auth\n", bssid);
  1905. goto out_err;
  1906. }
  1907. mutex_unlock(&sdata->local->sta_mtx);
  1908. return RX_MGMT_CFG80211_RX_AUTH;
  1909. out_err:
  1910. mutex_unlock(&sdata->local->sta_mtx);
  1911. /* ignore frame -- wait for timeout */
  1912. return RX_MGMT_NONE;
  1913. }
  1914. static enum rx_mgmt_action __must_check
  1915. ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  1916. struct ieee80211_mgmt *mgmt, size_t len)
  1917. {
  1918. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1919. const u8 *bssid = NULL;
  1920. u16 reason_code;
  1921. lockdep_assert_held(&ifmgd->mtx);
  1922. if (len < 24 + 2)
  1923. return RX_MGMT_NONE;
  1924. if (!ifmgd->associated ||
  1925. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  1926. return RX_MGMT_NONE;
  1927. bssid = ifmgd->associated->bssid;
  1928. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  1929. sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
  1930. bssid, reason_code);
  1931. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  1932. return RX_MGMT_CFG80211_DEAUTH;
  1933. }
  1934. static enum rx_mgmt_action __must_check
  1935. ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  1936. struct ieee80211_mgmt *mgmt, size_t len)
  1937. {
  1938. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1939. u16 reason_code;
  1940. lockdep_assert_held(&ifmgd->mtx);
  1941. if (len < 24 + 2)
  1942. return RX_MGMT_NONE;
  1943. if (!ifmgd->associated ||
  1944. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  1945. return RX_MGMT_NONE;
  1946. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  1947. sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
  1948. mgmt->sa, reason_code);
  1949. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  1950. return RX_MGMT_CFG80211_DISASSOC;
  1951. }
  1952. static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
  1953. u8 *supp_rates, unsigned int supp_rates_len,
  1954. u32 *rates, u32 *basic_rates,
  1955. bool *have_higher_than_11mbit,
  1956. int *min_rate, int *min_rate_index)
  1957. {
  1958. int i, j;
  1959. for (i = 0; i < supp_rates_len; i++) {
  1960. int rate = (supp_rates[i] & 0x7f) * 5;
  1961. bool is_basic = !!(supp_rates[i] & 0x80);
  1962. if (rate > 110)
  1963. *have_higher_than_11mbit = true;
  1964. /*
  1965. * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
  1966. * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
  1967. *
  1968. * Note: Even through the membership selector and the basic
  1969. * rate flag share the same bit, they are not exactly
  1970. * the same.
  1971. */
  1972. if (!!(supp_rates[i] & 0x80) &&
  1973. (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1974. continue;
  1975. for (j = 0; j < sband->n_bitrates; j++) {
  1976. if (sband->bitrates[j].bitrate == rate) {
  1977. *rates |= BIT(j);
  1978. if (is_basic)
  1979. *basic_rates |= BIT(j);
  1980. if (rate < *min_rate) {
  1981. *min_rate = rate;
  1982. *min_rate_index = j;
  1983. }
  1984. break;
  1985. }
  1986. }
  1987. }
  1988. }
  1989. static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
  1990. bool assoc)
  1991. {
  1992. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  1993. lockdep_assert_held(&sdata->u.mgd.mtx);
  1994. if (!assoc) {
  1995. sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
  1996. memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
  1997. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  1998. sdata->u.mgd.flags = 0;
  1999. ieee80211_vif_release_channel(sdata);
  2000. }
  2001. kfree(assoc_data);
  2002. sdata->u.mgd.assoc_data = NULL;
  2003. }
  2004. static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
  2005. struct cfg80211_bss *cbss,
  2006. struct ieee80211_mgmt *mgmt, size_t len)
  2007. {
  2008. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2009. struct ieee80211_local *local = sdata->local;
  2010. struct ieee80211_supported_band *sband;
  2011. struct sta_info *sta;
  2012. u8 *pos;
  2013. u16 capab_info, aid;
  2014. struct ieee802_11_elems elems;
  2015. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  2016. u32 changed = 0;
  2017. int err;
  2018. /* AssocResp and ReassocResp have identical structure */
  2019. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  2020. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  2021. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  2022. sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
  2023. aid);
  2024. aid &= ~(BIT(15) | BIT(14));
  2025. ifmgd->broken_ap = false;
  2026. if (aid == 0 || aid > IEEE80211_MAX_AID) {
  2027. sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
  2028. aid);
  2029. aid = 0;
  2030. ifmgd->broken_ap = true;
  2031. }
  2032. pos = mgmt->u.assoc_resp.variable;
  2033. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  2034. if (!elems.supp_rates) {
  2035. sdata_info(sdata, "no SuppRates element in AssocResp\n");
  2036. return false;
  2037. }
  2038. ifmgd->aid = aid;
  2039. /*
  2040. * We previously checked these in the beacon/probe response, so
  2041. * they should be present here. This is just a safety net.
  2042. */
  2043. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  2044. (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
  2045. sdata_info(sdata,
  2046. "HT AP is missing WMM params or HT capability/operation in AssocResp\n");
  2047. return false;
  2048. }
  2049. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  2050. (!elems.vht_cap_elem || !elems.vht_operation)) {
  2051. sdata_info(sdata,
  2052. "VHT AP is missing VHT capability/operation in AssocResp\n");
  2053. return false;
  2054. }
  2055. mutex_lock(&sdata->local->sta_mtx);
  2056. /*
  2057. * station info was already allocated and inserted before
  2058. * the association and should be available to us
  2059. */
  2060. sta = sta_info_get(sdata, cbss->bssid);
  2061. if (WARN_ON(!sta)) {
  2062. mutex_unlock(&sdata->local->sta_mtx);
  2063. return false;
  2064. }
  2065. sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
  2066. /* Set up internal HT/VHT capabilities */
  2067. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
  2068. ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  2069. elems.ht_cap_elem, sta);
  2070. if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  2071. ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
  2072. elems.vht_cap_elem, sta);
  2073. /*
  2074. * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
  2075. * in their association response, so ignore that data for our own
  2076. * configuration. If it changed since the last beacon, we'll get the
  2077. * next beacon and update then.
  2078. */
  2079. /*
  2080. * If an operating mode notification IE is present, override the
  2081. * NSS calculation (that would be done in rate_control_rate_init())
  2082. * and use the # of streams from that element.
  2083. */
  2084. if (elems.opmode_notif &&
  2085. !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
  2086. u8 nss;
  2087. nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
  2088. nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
  2089. nss += 1;
  2090. sta->sta.rx_nss = nss;
  2091. }
  2092. rate_control_rate_init(sta);
  2093. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
  2094. set_sta_flag(sta, WLAN_STA_MFP);
  2095. if (elems.wmm_param)
  2096. set_sta_flag(sta, WLAN_STA_WME);
  2097. err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
  2098. if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
  2099. err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
  2100. if (err) {
  2101. sdata_info(sdata,
  2102. "failed to move station %pM to desired state\n",
  2103. sta->sta.addr);
  2104. WARN_ON(__sta_info_destroy(sta));
  2105. mutex_unlock(&sdata->local->sta_mtx);
  2106. return false;
  2107. }
  2108. mutex_unlock(&sdata->local->sta_mtx);
  2109. /*
  2110. * Always handle WMM once after association regardless
  2111. * of the first value the AP uses. Setting -1 here has
  2112. * that effect because the AP values is an unsigned
  2113. * 4-bit value.
  2114. */
  2115. ifmgd->wmm_last_param_set = -1;
  2116. if (elems.wmm_param)
  2117. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  2118. elems.wmm_param_len);
  2119. else
  2120. ieee80211_set_wmm_default(sdata, false);
  2121. changed |= BSS_CHANGED_QOS;
  2122. /* set AID and assoc capability,
  2123. * ieee80211_set_associated() will tell the driver */
  2124. bss_conf->aid = aid;
  2125. bss_conf->assoc_capability = capab_info;
  2126. ieee80211_set_associated(sdata, cbss, changed);
  2127. /*
  2128. * If we're using 4-addr mode, let the AP know that we're
  2129. * doing so, so that it can create the STA VLAN on its side
  2130. */
  2131. if (ifmgd->use_4addr)
  2132. ieee80211_send_4addr_nullfunc(local, sdata);
  2133. /*
  2134. * Start timer to probe the connection to the AP now.
  2135. * Also start the timer that will detect beacon loss.
  2136. */
  2137. ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
  2138. ieee80211_sta_reset_beacon_monitor(sdata);
  2139. return true;
  2140. }
  2141. static enum rx_mgmt_action __must_check
  2142. ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  2143. struct ieee80211_mgmt *mgmt, size_t len,
  2144. struct cfg80211_bss **bss)
  2145. {
  2146. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2147. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  2148. u16 capab_info, status_code, aid;
  2149. struct ieee802_11_elems elems;
  2150. u8 *pos;
  2151. bool reassoc;
  2152. lockdep_assert_held(&ifmgd->mtx);
  2153. if (!assoc_data)
  2154. return RX_MGMT_NONE;
  2155. if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
  2156. return RX_MGMT_NONE;
  2157. /*
  2158. * AssocResp and ReassocResp have identical structure, so process both
  2159. * of them in this function.
  2160. */
  2161. if (len < 24 + 6)
  2162. return RX_MGMT_NONE;
  2163. reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
  2164. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  2165. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  2166. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  2167. sdata_info(sdata,
  2168. "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
  2169. reassoc ? "Rea" : "A", mgmt->sa,
  2170. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  2171. pos = mgmt->u.assoc_resp.variable;
  2172. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  2173. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  2174. elems.timeout_int && elems.timeout_int_len == 5 &&
  2175. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  2176. u32 tu, ms;
  2177. tu = get_unaligned_le32(elems.timeout_int + 1);
  2178. ms = tu * 1024 / 1000;
  2179. sdata_info(sdata,
  2180. "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
  2181. mgmt->sa, tu, ms);
  2182. assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
  2183. assoc_data->timeout_started = true;
  2184. if (ms > IEEE80211_ASSOC_TIMEOUT)
  2185. run_again(ifmgd, assoc_data->timeout);
  2186. return RX_MGMT_NONE;
  2187. }
  2188. *bss = assoc_data->bss;
  2189. if (status_code != WLAN_STATUS_SUCCESS) {
  2190. sdata_info(sdata, "%pM denied association (code=%d)\n",
  2191. mgmt->sa, status_code);
  2192. ieee80211_destroy_assoc_data(sdata, false);
  2193. } else {
  2194. if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
  2195. /* oops -- internal error -- send timeout for now */
  2196. ieee80211_destroy_assoc_data(sdata, false);
  2197. cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
  2198. return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
  2199. }
  2200. sdata_info(sdata, "associated\n");
  2201. /*
  2202. * destroy assoc_data afterwards, as otherwise an idle
  2203. * recalc after assoc_data is NULL but before associated
  2204. * is set can cause the interface to go idle
  2205. */
  2206. ieee80211_destroy_assoc_data(sdata, true);
  2207. }
  2208. return RX_MGMT_CFG80211_RX_ASSOC;
  2209. }
  2210. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  2211. struct ieee80211_mgmt *mgmt, size_t len,
  2212. struct ieee80211_rx_status *rx_status,
  2213. struct ieee802_11_elems *elems)
  2214. {
  2215. struct ieee80211_local *local = sdata->local;
  2216. int freq;
  2217. struct ieee80211_bss *bss;
  2218. struct ieee80211_channel *channel;
  2219. bool need_ps = false;
  2220. if ((sdata->u.mgd.associated &&
  2221. ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
  2222. (sdata->u.mgd.assoc_data &&
  2223. ether_addr_equal(mgmt->bssid,
  2224. sdata->u.mgd.assoc_data->bss->bssid))) {
  2225. /* not previously set so we may need to recalc */
  2226. need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
  2227. if (elems->tim && !elems->parse_error) {
  2228. const struct ieee80211_tim_ie *tim_ie = elems->tim;
  2229. sdata->u.mgd.dtim_period = tim_ie->dtim_period;
  2230. }
  2231. }
  2232. if (elems->ds_params && elems->ds_params_len == 1)
  2233. freq = ieee80211_channel_to_frequency(elems->ds_params[0],
  2234. rx_status->band);
  2235. else
  2236. freq = rx_status->freq;
  2237. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  2238. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  2239. return;
  2240. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  2241. channel);
  2242. if (bss)
  2243. ieee80211_rx_bss_put(local, bss);
  2244. if (!sdata->u.mgd.associated)
  2245. return;
  2246. if (need_ps) {
  2247. mutex_lock(&local->iflist_mtx);
  2248. ieee80211_recalc_ps(local, -1);
  2249. mutex_unlock(&local->iflist_mtx);
  2250. }
  2251. if (elems->ch_switch_ie &&
  2252. memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
  2253. ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
  2254. bss, rx_status->mactime);
  2255. }
  2256. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  2257. struct sk_buff *skb)
  2258. {
  2259. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  2260. struct ieee80211_if_managed *ifmgd;
  2261. struct ieee80211_rx_status *rx_status = (void *) skb->cb;
  2262. size_t baselen, len = skb->len;
  2263. struct ieee802_11_elems elems;
  2264. ifmgd = &sdata->u.mgd;
  2265. ASSERT_MGD_MTX(ifmgd);
  2266. if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
  2267. return; /* ignore ProbeResp to foreign address */
  2268. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  2269. if (baselen > len)
  2270. return;
  2271. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  2272. &elems);
  2273. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2274. if (ifmgd->associated &&
  2275. ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2276. ieee80211_reset_ap_probe(sdata);
  2277. if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
  2278. ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
  2279. /* got probe response, continue with auth */
  2280. sdata_info(sdata, "direct probe responded\n");
  2281. ifmgd->auth_data->tries = 0;
  2282. ifmgd->auth_data->timeout = jiffies;
  2283. ifmgd->auth_data->timeout_started = true;
  2284. run_again(ifmgd, ifmgd->auth_data->timeout);
  2285. }
  2286. }
  2287. /*
  2288. * This is the canonical list of information elements we care about,
  2289. * the filter code also gives us all changes to the Microsoft OUI
  2290. * (00:50:F2) vendor IE which is used for WMM which we need to track.
  2291. *
  2292. * We implement beacon filtering in software since that means we can
  2293. * avoid processing the frame here and in cfg80211, and userspace
  2294. * will not be able to tell whether the hardware supports it or not.
  2295. *
  2296. * XXX: This list needs to be dynamic -- userspace needs to be able to
  2297. * add items it requires. It also needs to be able to tell us to
  2298. * look out for other vendor IEs.
  2299. */
  2300. static const u64 care_about_ies =
  2301. (1ULL << WLAN_EID_COUNTRY) |
  2302. (1ULL << WLAN_EID_ERP_INFO) |
  2303. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  2304. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  2305. (1ULL << WLAN_EID_HT_CAPABILITY) |
  2306. (1ULL << WLAN_EID_HT_OPERATION);
  2307. static enum rx_mgmt_action
  2308. ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  2309. struct ieee80211_mgmt *mgmt, size_t len,
  2310. u8 *deauth_buf, struct ieee80211_rx_status *rx_status)
  2311. {
  2312. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2313. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  2314. size_t baselen;
  2315. struct ieee802_11_elems elems;
  2316. struct ieee80211_local *local = sdata->local;
  2317. struct ieee80211_chanctx_conf *chanctx_conf;
  2318. struct ieee80211_channel *chan;
  2319. struct sta_info *sta;
  2320. u32 changed = 0;
  2321. bool erp_valid;
  2322. u8 erp_value = 0;
  2323. u32 ncrc;
  2324. u8 *bssid;
  2325. lockdep_assert_held(&ifmgd->mtx);
  2326. /* Process beacon from the current BSS */
  2327. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  2328. if (baselen > len)
  2329. return RX_MGMT_NONE;
  2330. rcu_read_lock();
  2331. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  2332. if (!chanctx_conf) {
  2333. rcu_read_unlock();
  2334. return RX_MGMT_NONE;
  2335. }
  2336. if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
  2337. rcu_read_unlock();
  2338. return RX_MGMT_NONE;
  2339. }
  2340. chan = chanctx_conf->def.chan;
  2341. rcu_read_unlock();
  2342. if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
  2343. ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
  2344. ieee802_11_parse_elems(mgmt->u.beacon.variable,
  2345. len - baselen, &elems);
  2346. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2347. ifmgd->assoc_data->have_beacon = true;
  2348. ifmgd->assoc_data->need_beacon = false;
  2349. if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
  2350. sdata->vif.bss_conf.sync_tsf =
  2351. le64_to_cpu(mgmt->u.beacon.timestamp);
  2352. sdata->vif.bss_conf.sync_device_ts =
  2353. rx_status->device_timestamp;
  2354. if (elems.tim)
  2355. sdata->vif.bss_conf.sync_dtim_count =
  2356. elems.tim->dtim_count;
  2357. else
  2358. sdata->vif.bss_conf.sync_dtim_count = 0;
  2359. }
  2360. /* continue assoc process */
  2361. ifmgd->assoc_data->timeout = jiffies;
  2362. ifmgd->assoc_data->timeout_started = true;
  2363. run_again(ifmgd, ifmgd->assoc_data->timeout);
  2364. return RX_MGMT_NONE;
  2365. }
  2366. if (!ifmgd->associated ||
  2367. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2368. return RX_MGMT_NONE;
  2369. bssid = ifmgd->associated->bssid;
  2370. /* Track average RSSI from the Beacon frames of the current AP */
  2371. ifmgd->last_beacon_signal = rx_status->signal;
  2372. if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
  2373. ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
  2374. ifmgd->ave_beacon_signal = rx_status->signal * 16;
  2375. ifmgd->last_cqm_event_signal = 0;
  2376. ifmgd->count_beacon_signal = 1;
  2377. ifmgd->last_ave_beacon_signal = 0;
  2378. } else {
  2379. ifmgd->ave_beacon_signal =
  2380. (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
  2381. (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
  2382. ifmgd->ave_beacon_signal) / 16;
  2383. ifmgd->count_beacon_signal++;
  2384. }
  2385. if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
  2386. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
  2387. int sig = ifmgd->ave_beacon_signal;
  2388. int last_sig = ifmgd->last_ave_beacon_signal;
  2389. /*
  2390. * if signal crosses either of the boundaries, invoke callback
  2391. * with appropriate parameters
  2392. */
  2393. if (sig > ifmgd->rssi_max_thold &&
  2394. (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
  2395. ifmgd->last_ave_beacon_signal = sig;
  2396. drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
  2397. } else if (sig < ifmgd->rssi_min_thold &&
  2398. (last_sig >= ifmgd->rssi_max_thold ||
  2399. last_sig == 0)) {
  2400. ifmgd->last_ave_beacon_signal = sig;
  2401. drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
  2402. }
  2403. }
  2404. if (bss_conf->cqm_rssi_thold &&
  2405. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
  2406. !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
  2407. int sig = ifmgd->ave_beacon_signal / 16;
  2408. int last_event = ifmgd->last_cqm_event_signal;
  2409. int thold = bss_conf->cqm_rssi_thold;
  2410. int hyst = bss_conf->cqm_rssi_hyst;
  2411. if (sig < thold &&
  2412. (last_event == 0 || sig < last_event - hyst)) {
  2413. ifmgd->last_cqm_event_signal = sig;
  2414. ieee80211_cqm_rssi_notify(
  2415. &sdata->vif,
  2416. NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
  2417. GFP_KERNEL);
  2418. } else if (sig > thold &&
  2419. (last_event == 0 || sig > last_event + hyst)) {
  2420. ifmgd->last_cqm_event_signal = sig;
  2421. ieee80211_cqm_rssi_notify(
  2422. &sdata->vif,
  2423. NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
  2424. GFP_KERNEL);
  2425. }
  2426. }
  2427. if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
  2428. mlme_dbg_ratelimited(sdata,
  2429. "cancelling AP probe due to a received beacon\n");
  2430. mutex_lock(&local->mtx);
  2431. ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
  2432. ieee80211_run_deferred_scan(local);
  2433. mutex_unlock(&local->mtx);
  2434. mutex_lock(&local->iflist_mtx);
  2435. ieee80211_recalc_ps(local, -1);
  2436. mutex_unlock(&local->iflist_mtx);
  2437. }
  2438. /*
  2439. * Push the beacon loss detection into the future since
  2440. * we are processing a beacon from the AP just now.
  2441. */
  2442. ieee80211_sta_reset_beacon_monitor(sdata);
  2443. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  2444. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  2445. len - baselen, &elems,
  2446. care_about_ies, ncrc);
  2447. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
  2448. bool directed_tim = ieee80211_check_tim(elems.tim,
  2449. elems.tim_len,
  2450. ifmgd->aid);
  2451. if (directed_tim) {
  2452. if (local->hw.conf.dynamic_ps_timeout > 0) {
  2453. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  2454. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  2455. ieee80211_hw_config(local,
  2456. IEEE80211_CONF_CHANGE_PS);
  2457. }
  2458. ieee80211_send_nullfunc(local, sdata, 0);
  2459. } else if (!local->pspolling && sdata->u.mgd.powersave) {
  2460. local->pspolling = true;
  2461. /*
  2462. * Here is assumed that the driver will be
  2463. * able to send ps-poll frame and receive a
  2464. * response even though power save mode is
  2465. * enabled, but some drivers might require
  2466. * to disable power save here. This needs
  2467. * to be investigated.
  2468. */
  2469. ieee80211_send_pspoll(local, sdata);
  2470. }
  2471. }
  2472. }
  2473. if (sdata->vif.p2p) {
  2474. u8 noa[2];
  2475. int ret;
  2476. ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
  2477. len - baselen,
  2478. IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
  2479. noa, sizeof(noa));
  2480. if (ret >= 2 && sdata->u.mgd.p2p_noa_index != noa[0]) {
  2481. bss_conf->p2p_oppps = noa[1] & 0x80;
  2482. bss_conf->p2p_ctwindow = noa[1] & 0x7f;
  2483. changed |= BSS_CHANGED_P2P_PS;
  2484. sdata->u.mgd.p2p_noa_index = noa[0];
  2485. /*
  2486. * make sure we update all information, the CRC
  2487. * mechanism doesn't look at P2P attributes.
  2488. */
  2489. ifmgd->beacon_crc_valid = false;
  2490. }
  2491. }
  2492. if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
  2493. return RX_MGMT_NONE;
  2494. ifmgd->beacon_crc = ncrc;
  2495. ifmgd->beacon_crc_valid = true;
  2496. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2497. if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  2498. elems.wmm_param_len))
  2499. changed |= BSS_CHANGED_QOS;
  2500. /*
  2501. * If we haven't had a beacon before, tell the driver about the
  2502. * DTIM period (and beacon timing if desired) now.
  2503. */
  2504. if (!bss_conf->dtim_period) {
  2505. /* a few bogus AP send dtim_period = 0 or no TIM IE */
  2506. if (elems.tim)
  2507. bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
  2508. else
  2509. bss_conf->dtim_period = 1;
  2510. if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
  2511. sdata->vif.bss_conf.sync_tsf =
  2512. le64_to_cpu(mgmt->u.beacon.timestamp);
  2513. sdata->vif.bss_conf.sync_device_ts =
  2514. rx_status->device_timestamp;
  2515. if (elems.tim)
  2516. sdata->vif.bss_conf.sync_dtim_count =
  2517. elems.tim->dtim_count;
  2518. else
  2519. sdata->vif.bss_conf.sync_dtim_count = 0;
  2520. }
  2521. changed |= BSS_CHANGED_DTIM_PERIOD;
  2522. }
  2523. if (elems.erp_info && elems.erp_info_len >= 1) {
  2524. erp_valid = true;
  2525. erp_value = elems.erp_info[0];
  2526. } else {
  2527. erp_valid = false;
  2528. }
  2529. changed |= ieee80211_handle_bss_capability(sdata,
  2530. le16_to_cpu(mgmt->u.beacon.capab_info),
  2531. erp_valid, erp_value);
  2532. mutex_lock(&local->sta_mtx);
  2533. sta = sta_info_get(sdata, bssid);
  2534. if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
  2535. elems.vht_operation, bssid, &changed)) {
  2536. mutex_unlock(&local->sta_mtx);
  2537. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  2538. WLAN_REASON_DEAUTH_LEAVING,
  2539. true, deauth_buf);
  2540. return RX_MGMT_CFG80211_TX_DEAUTH;
  2541. }
  2542. if (sta && elems.opmode_notif)
  2543. ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
  2544. rx_status->band, true);
  2545. mutex_unlock(&local->sta_mtx);
  2546. if (elems.country_elem && elems.pwr_constr_elem &&
  2547. mgmt->u.probe_resp.capab_info &
  2548. cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
  2549. changed |= ieee80211_handle_pwr_constr(sdata, chan,
  2550. elems.country_elem,
  2551. elems.country_elem_len,
  2552. elems.pwr_constr_elem);
  2553. ieee80211_bss_info_change_notify(sdata, changed);
  2554. return RX_MGMT_NONE;
  2555. }
  2556. void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  2557. struct sk_buff *skb)
  2558. {
  2559. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2560. struct ieee80211_rx_status *rx_status;
  2561. struct ieee80211_mgmt *mgmt;
  2562. struct cfg80211_bss *bss = NULL;
  2563. enum rx_mgmt_action rma = RX_MGMT_NONE;
  2564. u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
  2565. u16 fc;
  2566. rx_status = (struct ieee80211_rx_status *) skb->cb;
  2567. mgmt = (struct ieee80211_mgmt *) skb->data;
  2568. fc = le16_to_cpu(mgmt->frame_control);
  2569. mutex_lock(&ifmgd->mtx);
  2570. switch (fc & IEEE80211_FCTL_STYPE) {
  2571. case IEEE80211_STYPE_BEACON:
  2572. rma = ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
  2573. deauth_buf, rx_status);
  2574. break;
  2575. case IEEE80211_STYPE_PROBE_RESP:
  2576. ieee80211_rx_mgmt_probe_resp(sdata, skb);
  2577. break;
  2578. case IEEE80211_STYPE_AUTH:
  2579. rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
  2580. break;
  2581. case IEEE80211_STYPE_DEAUTH:
  2582. rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  2583. break;
  2584. case IEEE80211_STYPE_DISASSOC:
  2585. rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  2586. break;
  2587. case IEEE80211_STYPE_ASSOC_RESP:
  2588. case IEEE80211_STYPE_REASSOC_RESP:
  2589. rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
  2590. break;
  2591. case IEEE80211_STYPE_ACTION:
  2592. switch (mgmt->u.action.category) {
  2593. case WLAN_CATEGORY_SPECTRUM_MGMT:
  2594. ieee80211_sta_process_chanswitch(sdata,
  2595. &mgmt->u.action.u.chan_switch.sw_elem,
  2596. (void *)ifmgd->associated->priv,
  2597. rx_status->mactime);
  2598. break;
  2599. }
  2600. }
  2601. mutex_unlock(&ifmgd->mtx);
  2602. switch (rma) {
  2603. case RX_MGMT_NONE:
  2604. /* no action */
  2605. break;
  2606. case RX_MGMT_CFG80211_DEAUTH:
  2607. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  2608. break;
  2609. case RX_MGMT_CFG80211_DISASSOC:
  2610. cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  2611. break;
  2612. case RX_MGMT_CFG80211_RX_AUTH:
  2613. cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
  2614. break;
  2615. case RX_MGMT_CFG80211_RX_ASSOC:
  2616. cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
  2617. break;
  2618. case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
  2619. cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
  2620. break;
  2621. case RX_MGMT_CFG80211_TX_DEAUTH:
  2622. cfg80211_send_deauth(sdata->dev, deauth_buf,
  2623. sizeof(deauth_buf));
  2624. break;
  2625. default:
  2626. WARN(1, "unexpected: %d", rma);
  2627. }
  2628. }
  2629. static void ieee80211_sta_timer(unsigned long data)
  2630. {
  2631. struct ieee80211_sub_if_data *sdata =
  2632. (struct ieee80211_sub_if_data *) data;
  2633. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  2634. }
  2635. static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
  2636. u8 *bssid, u8 reason, bool tx)
  2637. {
  2638. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2639. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  2640. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
  2641. tx, frame_buf);
  2642. mutex_unlock(&ifmgd->mtx);
  2643. /*
  2644. * must be outside lock due to cfg80211,
  2645. * but that's not a problem.
  2646. */
  2647. cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
  2648. mutex_lock(&ifmgd->mtx);
  2649. }
  2650. static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
  2651. {
  2652. struct ieee80211_local *local = sdata->local;
  2653. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2654. struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
  2655. u32 tx_flags = 0;
  2656. lockdep_assert_held(&ifmgd->mtx);
  2657. if (WARN_ON_ONCE(!auth_data))
  2658. return -EINVAL;
  2659. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  2660. tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
  2661. IEEE80211_TX_INTFL_MLME_CONN_TX;
  2662. auth_data->tries++;
  2663. if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
  2664. sdata_info(sdata, "authentication with %pM timed out\n",
  2665. auth_data->bss->bssid);
  2666. /*
  2667. * Most likely AP is not in the range so remove the
  2668. * bss struct for that AP.
  2669. */
  2670. cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
  2671. return -ETIMEDOUT;
  2672. }
  2673. drv_mgd_prepare_tx(local, sdata);
  2674. if (auth_data->bss->proberesp_ies) {
  2675. u16 trans = 1;
  2676. u16 status = 0;
  2677. sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
  2678. auth_data->bss->bssid, auth_data->tries,
  2679. IEEE80211_AUTH_MAX_TRIES);
  2680. auth_data->expected_transaction = 2;
  2681. if (auth_data->algorithm == WLAN_AUTH_SAE) {
  2682. trans = auth_data->sae_trans;
  2683. status = auth_data->sae_status;
  2684. auth_data->expected_transaction = trans;
  2685. }
  2686. ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
  2687. auth_data->data, auth_data->data_len,
  2688. auth_data->bss->bssid,
  2689. auth_data->bss->bssid, NULL, 0, 0,
  2690. tx_flags);
  2691. } else {
  2692. const u8 *ssidie;
  2693. sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
  2694. auth_data->bss->bssid, auth_data->tries,
  2695. IEEE80211_AUTH_MAX_TRIES);
  2696. rcu_read_lock();
  2697. ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
  2698. if (!ssidie) {
  2699. rcu_read_unlock();
  2700. return -EINVAL;
  2701. }
  2702. /*
  2703. * Direct probe is sent to broadcast address as some APs
  2704. * will not answer to direct packet in unassociated state.
  2705. */
  2706. ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
  2707. NULL, 0, (u32) -1, true, tx_flags,
  2708. auth_data->bss->channel, false);
  2709. rcu_read_unlock();
  2710. }
  2711. if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
  2712. auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  2713. ifmgd->auth_data->timeout_started = true;
  2714. run_again(ifmgd, auth_data->timeout);
  2715. } else {
  2716. auth_data->timeout_started = false;
  2717. }
  2718. return 0;
  2719. }
  2720. static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
  2721. {
  2722. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  2723. struct ieee80211_local *local = sdata->local;
  2724. lockdep_assert_held(&sdata->u.mgd.mtx);
  2725. assoc_data->tries++;
  2726. if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
  2727. sdata_info(sdata, "association with %pM timed out\n",
  2728. assoc_data->bss->bssid);
  2729. /*
  2730. * Most likely AP is not in the range so remove the
  2731. * bss struct for that AP.
  2732. */
  2733. cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
  2734. return -ETIMEDOUT;
  2735. }
  2736. sdata_info(sdata, "associate with %pM (try %d/%d)\n",
  2737. assoc_data->bss->bssid, assoc_data->tries,
  2738. IEEE80211_ASSOC_MAX_TRIES);
  2739. ieee80211_send_assoc(sdata);
  2740. if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
  2741. assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
  2742. assoc_data->timeout_started = true;
  2743. run_again(&sdata->u.mgd, assoc_data->timeout);
  2744. } else {
  2745. assoc_data->timeout_started = false;
  2746. }
  2747. return 0;
  2748. }
  2749. void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
  2750. __le16 fc, bool acked)
  2751. {
  2752. struct ieee80211_local *local = sdata->local;
  2753. sdata->u.mgd.status_fc = fc;
  2754. sdata->u.mgd.status_acked = acked;
  2755. sdata->u.mgd.status_received = true;
  2756. ieee80211_queue_work(&local->hw, &sdata->work);
  2757. }
  2758. void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
  2759. {
  2760. struct ieee80211_local *local = sdata->local;
  2761. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2762. mutex_lock(&ifmgd->mtx);
  2763. if (ifmgd->status_received) {
  2764. __le16 fc = ifmgd->status_fc;
  2765. bool status_acked = ifmgd->status_acked;
  2766. ifmgd->status_received = false;
  2767. if (ifmgd->auth_data &&
  2768. (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
  2769. if (status_acked) {
  2770. ifmgd->auth_data->timeout =
  2771. jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
  2772. run_again(ifmgd, ifmgd->auth_data->timeout);
  2773. } else {
  2774. ifmgd->auth_data->timeout = jiffies - 1;
  2775. }
  2776. ifmgd->auth_data->timeout_started = true;
  2777. } else if (ifmgd->assoc_data &&
  2778. (ieee80211_is_assoc_req(fc) ||
  2779. ieee80211_is_reassoc_req(fc))) {
  2780. if (status_acked) {
  2781. ifmgd->assoc_data->timeout =
  2782. jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
  2783. run_again(ifmgd, ifmgd->assoc_data->timeout);
  2784. } else {
  2785. ifmgd->assoc_data->timeout = jiffies - 1;
  2786. }
  2787. ifmgd->assoc_data->timeout_started = true;
  2788. }
  2789. }
  2790. if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
  2791. time_after(jiffies, ifmgd->auth_data->timeout)) {
  2792. if (ifmgd->auth_data->done) {
  2793. /*
  2794. * ok ... we waited for assoc but userspace didn't,
  2795. * so let's just kill the auth data
  2796. */
  2797. ieee80211_destroy_auth_data(sdata, false);
  2798. } else if (ieee80211_probe_auth(sdata)) {
  2799. u8 bssid[ETH_ALEN];
  2800. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  2801. ieee80211_destroy_auth_data(sdata, false);
  2802. mutex_unlock(&ifmgd->mtx);
  2803. cfg80211_send_auth_timeout(sdata->dev, bssid);
  2804. mutex_lock(&ifmgd->mtx);
  2805. }
  2806. } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
  2807. run_again(ifmgd, ifmgd->auth_data->timeout);
  2808. if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
  2809. time_after(jiffies, ifmgd->assoc_data->timeout)) {
  2810. if ((ifmgd->assoc_data->need_beacon &&
  2811. !ifmgd->assoc_data->have_beacon) ||
  2812. ieee80211_do_assoc(sdata)) {
  2813. u8 bssid[ETH_ALEN];
  2814. memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
  2815. ieee80211_destroy_assoc_data(sdata, false);
  2816. mutex_unlock(&ifmgd->mtx);
  2817. cfg80211_send_assoc_timeout(sdata->dev, bssid);
  2818. mutex_lock(&ifmgd->mtx);
  2819. }
  2820. } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
  2821. run_again(ifmgd, ifmgd->assoc_data->timeout);
  2822. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  2823. IEEE80211_STA_CONNECTION_POLL) &&
  2824. ifmgd->associated) {
  2825. u8 bssid[ETH_ALEN];
  2826. int max_tries;
  2827. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  2828. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  2829. max_tries = max_nullfunc_tries;
  2830. else
  2831. max_tries = max_probe_tries;
  2832. /* ACK received for nullfunc probing frame */
  2833. if (!ifmgd->probe_send_count)
  2834. ieee80211_reset_ap_probe(sdata);
  2835. else if (ifmgd->nullfunc_failed) {
  2836. if (ifmgd->probe_send_count < max_tries) {
  2837. mlme_dbg(sdata,
  2838. "No ack for nullfunc frame to AP %pM, try %d/%i\n",
  2839. bssid, ifmgd->probe_send_count,
  2840. max_tries);
  2841. ieee80211_mgd_probe_ap_send(sdata);
  2842. } else {
  2843. mlme_dbg(sdata,
  2844. "No ack for nullfunc frame to AP %pM, disconnecting.\n",
  2845. bssid);
  2846. ieee80211_sta_connection_lost(sdata, bssid,
  2847. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  2848. false);
  2849. }
  2850. } else if (time_is_after_jiffies(ifmgd->probe_timeout))
  2851. run_again(ifmgd, ifmgd->probe_timeout);
  2852. else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  2853. mlme_dbg(sdata,
  2854. "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
  2855. bssid, probe_wait_ms);
  2856. ieee80211_sta_connection_lost(sdata, bssid,
  2857. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
  2858. } else if (ifmgd->probe_send_count < max_tries) {
  2859. mlme_dbg(sdata,
  2860. "No probe response from AP %pM after %dms, try %d/%i\n",
  2861. bssid, probe_wait_ms,
  2862. ifmgd->probe_send_count, max_tries);
  2863. ieee80211_mgd_probe_ap_send(sdata);
  2864. } else {
  2865. /*
  2866. * We actually lost the connection ... or did we?
  2867. * Let's make sure!
  2868. */
  2869. wiphy_debug(local->hw.wiphy,
  2870. "%s: No probe response from AP %pM"
  2871. " after %dms, disconnecting.\n",
  2872. sdata->name,
  2873. bssid, probe_wait_ms);
  2874. ieee80211_sta_connection_lost(sdata, bssid,
  2875. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
  2876. }
  2877. }
  2878. mutex_unlock(&ifmgd->mtx);
  2879. }
  2880. static void ieee80211_sta_bcn_mon_timer(unsigned long data)
  2881. {
  2882. struct ieee80211_sub_if_data *sdata =
  2883. (struct ieee80211_sub_if_data *) data;
  2884. struct ieee80211_local *local = sdata->local;
  2885. if (local->quiescing)
  2886. return;
  2887. sdata->u.mgd.connection_loss = false;
  2888. ieee80211_queue_work(&sdata->local->hw,
  2889. &sdata->u.mgd.beacon_connection_loss_work);
  2890. }
  2891. static void ieee80211_sta_conn_mon_timer(unsigned long data)
  2892. {
  2893. struct ieee80211_sub_if_data *sdata =
  2894. (struct ieee80211_sub_if_data *) data;
  2895. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2896. struct ieee80211_local *local = sdata->local;
  2897. if (local->quiescing)
  2898. return;
  2899. ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
  2900. }
  2901. static void ieee80211_sta_monitor_work(struct work_struct *work)
  2902. {
  2903. struct ieee80211_sub_if_data *sdata =
  2904. container_of(work, struct ieee80211_sub_if_data,
  2905. u.mgd.monitor_work);
  2906. ieee80211_mgd_probe_ap(sdata, false);
  2907. }
  2908. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  2909. {
  2910. u32 flags;
  2911. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  2912. __ieee80211_stop_poll(sdata);
  2913. /* let's probe the connection once */
  2914. flags = sdata->local->hw.flags;
  2915. if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
  2916. ieee80211_queue_work(&sdata->local->hw,
  2917. &sdata->u.mgd.monitor_work);
  2918. /* and do all the other regular work too */
  2919. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  2920. }
  2921. }
  2922. /* interface setup */
  2923. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  2924. {
  2925. struct ieee80211_if_managed *ifmgd;
  2926. ifmgd = &sdata->u.mgd;
  2927. INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
  2928. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  2929. INIT_WORK(&ifmgd->beacon_connection_loss_work,
  2930. ieee80211_beacon_connection_loss_work);
  2931. INIT_WORK(&ifmgd->csa_connection_drop_work,
  2932. ieee80211_csa_connection_drop_work);
  2933. INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
  2934. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  2935. (unsigned long) sdata);
  2936. setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
  2937. (unsigned long) sdata);
  2938. setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
  2939. (unsigned long) sdata);
  2940. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  2941. (unsigned long) sdata);
  2942. ifmgd->flags = 0;
  2943. ifmgd->powersave = sdata->wdev.ps;
  2944. ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
  2945. ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
  2946. mutex_init(&ifmgd->mtx);
  2947. if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
  2948. ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
  2949. else
  2950. ifmgd->req_smps = IEEE80211_SMPS_OFF;
  2951. }
  2952. /* scan finished notification */
  2953. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  2954. {
  2955. struct ieee80211_sub_if_data *sdata;
  2956. /* Restart STA timers */
  2957. rcu_read_lock();
  2958. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  2959. ieee80211_restart_sta_timer(sdata);
  2960. rcu_read_unlock();
  2961. }
  2962. int ieee80211_max_network_latency(struct notifier_block *nb,
  2963. unsigned long data, void *dummy)
  2964. {
  2965. s32 latency_usec = (s32) data;
  2966. struct ieee80211_local *local =
  2967. container_of(nb, struct ieee80211_local,
  2968. network_latency_notifier);
  2969. mutex_lock(&local->iflist_mtx);
  2970. ieee80211_recalc_ps(local, latency_usec);
  2971. mutex_unlock(&local->iflist_mtx);
  2972. return 0;
  2973. }
  2974. static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
  2975. struct cfg80211_bss *cbss)
  2976. {
  2977. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2978. const u8 *ht_cap_ie, *vht_cap_ie;
  2979. const struct ieee80211_ht_cap *ht_cap;
  2980. const struct ieee80211_vht_cap *vht_cap;
  2981. u8 chains = 1;
  2982. if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
  2983. return chains;
  2984. ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
  2985. if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
  2986. ht_cap = (void *)(ht_cap_ie + 2);
  2987. chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
  2988. /*
  2989. * TODO: use "Tx Maximum Number Spatial Streams Supported" and
  2990. * "Tx Unequal Modulation Supported" fields.
  2991. */
  2992. }
  2993. if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
  2994. return chains;
  2995. vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
  2996. if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
  2997. u8 nss;
  2998. u16 tx_mcs_map;
  2999. vht_cap = (void *)(vht_cap_ie + 2);
  3000. tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
  3001. for (nss = 8; nss > 0; nss--) {
  3002. if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
  3003. IEEE80211_VHT_MCS_NOT_SUPPORTED)
  3004. break;
  3005. }
  3006. /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
  3007. chains = max(chains, nss);
  3008. }
  3009. return chains;
  3010. }
  3011. static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
  3012. struct cfg80211_bss *cbss)
  3013. {
  3014. struct ieee80211_local *local = sdata->local;
  3015. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3016. const struct ieee80211_ht_operation *ht_oper = NULL;
  3017. const struct ieee80211_vht_operation *vht_oper = NULL;
  3018. struct ieee80211_supported_band *sband;
  3019. struct cfg80211_chan_def chandef;
  3020. int ret;
  3021. sband = local->hw.wiphy->bands[cbss->channel->band];
  3022. ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
  3023. IEEE80211_STA_DISABLE_80P80MHZ |
  3024. IEEE80211_STA_DISABLE_160MHZ);
  3025. rcu_read_lock();
  3026. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  3027. sband->ht_cap.ht_supported) {
  3028. const u8 *ht_oper_ie, *ht_cap;
  3029. ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
  3030. if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
  3031. ht_oper = (void *)(ht_oper_ie + 2);
  3032. ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
  3033. if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
  3034. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3035. ht_oper = NULL;
  3036. }
  3037. }
  3038. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  3039. sband->vht_cap.vht_supported) {
  3040. const u8 *vht_oper_ie, *vht_cap;
  3041. vht_oper_ie = ieee80211_bss_get_ie(cbss,
  3042. WLAN_EID_VHT_OPERATION);
  3043. if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
  3044. vht_oper = (void *)(vht_oper_ie + 2);
  3045. if (vht_oper && !ht_oper) {
  3046. vht_oper = NULL;
  3047. sdata_info(sdata,
  3048. "AP advertised VHT without HT, disabling both\n");
  3049. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3050. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3051. }
  3052. vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
  3053. if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
  3054. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3055. vht_oper = NULL;
  3056. }
  3057. }
  3058. ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
  3059. cbss->channel,
  3060. ht_oper, vht_oper,
  3061. &chandef, true);
  3062. sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
  3063. local->rx_chains);
  3064. rcu_read_unlock();
  3065. /* will change later if needed */
  3066. sdata->smps_mode = IEEE80211_SMPS_OFF;
  3067. /*
  3068. * If this fails (possibly due to channel context sharing
  3069. * on incompatible channels, e.g. 80+80 and 160 sharing the
  3070. * same control channel) try to use a smaller bandwidth.
  3071. */
  3072. ret = ieee80211_vif_use_channel(sdata, &chandef,
  3073. IEEE80211_CHANCTX_SHARED);
  3074. while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
  3075. ifmgd->flags |= chandef_downgrade(&chandef);
  3076. ret = ieee80211_vif_use_channel(sdata, &chandef,
  3077. IEEE80211_CHANCTX_SHARED);
  3078. }
  3079. return ret;
  3080. }
  3081. static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
  3082. struct cfg80211_bss *cbss, bool assoc)
  3083. {
  3084. struct ieee80211_local *local = sdata->local;
  3085. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3086. struct ieee80211_bss *bss = (void *)cbss->priv;
  3087. struct sta_info *new_sta = NULL;
  3088. bool have_sta = false;
  3089. int err;
  3090. if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
  3091. return -EINVAL;
  3092. if (assoc) {
  3093. rcu_read_lock();
  3094. have_sta = sta_info_get(sdata, cbss->bssid);
  3095. rcu_read_unlock();
  3096. }
  3097. if (!have_sta) {
  3098. new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
  3099. if (!new_sta)
  3100. return -ENOMEM;
  3101. }
  3102. if (new_sta) {
  3103. u32 rates = 0, basic_rates = 0;
  3104. bool have_higher_than_11mbit;
  3105. int min_rate = INT_MAX, min_rate_index = -1;
  3106. struct ieee80211_supported_band *sband;
  3107. const struct cfg80211_bss_ies *ies;
  3108. sband = local->hw.wiphy->bands[cbss->channel->band];
  3109. err = ieee80211_prep_channel(sdata, cbss);
  3110. if (err) {
  3111. sta_info_free(local, new_sta);
  3112. return err;
  3113. }
  3114. ieee80211_get_rates(sband, bss->supp_rates,
  3115. bss->supp_rates_len,
  3116. &rates, &basic_rates,
  3117. &have_higher_than_11mbit,
  3118. &min_rate, &min_rate_index);
  3119. /*
  3120. * This used to be a workaround for basic rates missing
  3121. * in the association response frame. Now that we no
  3122. * longer use the basic rates from there, it probably
  3123. * doesn't happen any more, but keep the workaround so
  3124. * in case some *other* APs are buggy in different ways
  3125. * we can connect -- with a warning.
  3126. */
  3127. if (!basic_rates && min_rate_index >= 0) {
  3128. sdata_info(sdata,
  3129. "No basic rates, using min rate instead\n");
  3130. basic_rates = BIT(min_rate_index);
  3131. }
  3132. new_sta->sta.supp_rates[cbss->channel->band] = rates;
  3133. sdata->vif.bss_conf.basic_rates = basic_rates;
  3134. /* cf. IEEE 802.11 9.2.12 */
  3135. if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
  3136. have_higher_than_11mbit)
  3137. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  3138. else
  3139. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  3140. memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
  3141. /* set timing information */
  3142. sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
  3143. rcu_read_lock();
  3144. ies = rcu_dereference(cbss->beacon_ies);
  3145. if (ies) {
  3146. const u8 *tim_ie;
  3147. sdata->vif.bss_conf.sync_tsf = ies->tsf;
  3148. sdata->vif.bss_conf.sync_device_ts =
  3149. bss->device_ts_beacon;
  3150. tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
  3151. ies->data, ies->len);
  3152. if (tim_ie && tim_ie[1] >= 2)
  3153. sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
  3154. else
  3155. sdata->vif.bss_conf.sync_dtim_count = 0;
  3156. } else if (!(local->hw.flags &
  3157. IEEE80211_HW_TIMING_BEACON_ONLY)) {
  3158. ies = rcu_dereference(cbss->proberesp_ies);
  3159. /* must be non-NULL since beacon IEs were NULL */
  3160. sdata->vif.bss_conf.sync_tsf = ies->tsf;
  3161. sdata->vif.bss_conf.sync_device_ts =
  3162. bss->device_ts_presp;
  3163. sdata->vif.bss_conf.sync_dtim_count = 0;
  3164. } else {
  3165. sdata->vif.bss_conf.sync_tsf = 0;
  3166. sdata->vif.bss_conf.sync_device_ts = 0;
  3167. sdata->vif.bss_conf.sync_dtim_count = 0;
  3168. }
  3169. rcu_read_unlock();
  3170. /* tell driver about BSSID, basic rates and timing */
  3171. ieee80211_bss_info_change_notify(sdata,
  3172. BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
  3173. BSS_CHANGED_BEACON_INT);
  3174. if (assoc)
  3175. sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
  3176. err = sta_info_insert(new_sta);
  3177. new_sta = NULL;
  3178. if (err) {
  3179. sdata_info(sdata,
  3180. "failed to insert STA entry for the AP (error %d)\n",
  3181. err);
  3182. return err;
  3183. }
  3184. } else
  3185. WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
  3186. return 0;
  3187. }
  3188. /* config hooks */
  3189. int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
  3190. struct cfg80211_auth_request *req)
  3191. {
  3192. struct ieee80211_local *local = sdata->local;
  3193. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3194. struct ieee80211_mgd_auth_data *auth_data;
  3195. u16 auth_alg;
  3196. int err;
  3197. /* prepare auth data structure */
  3198. switch (req->auth_type) {
  3199. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  3200. auth_alg = WLAN_AUTH_OPEN;
  3201. break;
  3202. case NL80211_AUTHTYPE_SHARED_KEY:
  3203. if (IS_ERR(local->wep_tx_tfm))
  3204. return -EOPNOTSUPP;
  3205. auth_alg = WLAN_AUTH_SHARED_KEY;
  3206. break;
  3207. case NL80211_AUTHTYPE_FT:
  3208. auth_alg = WLAN_AUTH_FT;
  3209. break;
  3210. case NL80211_AUTHTYPE_NETWORK_EAP:
  3211. auth_alg = WLAN_AUTH_LEAP;
  3212. break;
  3213. case NL80211_AUTHTYPE_SAE:
  3214. auth_alg = WLAN_AUTH_SAE;
  3215. break;
  3216. default:
  3217. return -EOPNOTSUPP;
  3218. }
  3219. auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
  3220. req->ie_len, GFP_KERNEL);
  3221. if (!auth_data)
  3222. return -ENOMEM;
  3223. auth_data->bss = req->bss;
  3224. if (req->sae_data_len >= 4) {
  3225. __le16 *pos = (__le16 *) req->sae_data;
  3226. auth_data->sae_trans = le16_to_cpu(pos[0]);
  3227. auth_data->sae_status = le16_to_cpu(pos[1]);
  3228. memcpy(auth_data->data, req->sae_data + 4,
  3229. req->sae_data_len - 4);
  3230. auth_data->data_len += req->sae_data_len - 4;
  3231. }
  3232. if (req->ie && req->ie_len) {
  3233. memcpy(&auth_data->data[auth_data->data_len],
  3234. req->ie, req->ie_len);
  3235. auth_data->data_len += req->ie_len;
  3236. }
  3237. if (req->key && req->key_len) {
  3238. auth_data->key_len = req->key_len;
  3239. auth_data->key_idx = req->key_idx;
  3240. memcpy(auth_data->key, req->key, req->key_len);
  3241. }
  3242. auth_data->algorithm = auth_alg;
  3243. /* try to authenticate/probe */
  3244. mutex_lock(&ifmgd->mtx);
  3245. if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
  3246. ifmgd->assoc_data) {
  3247. err = -EBUSY;
  3248. goto err_free;
  3249. }
  3250. if (ifmgd->auth_data)
  3251. ieee80211_destroy_auth_data(sdata, false);
  3252. /* prep auth_data so we don't go into idle on disassoc */
  3253. ifmgd->auth_data = auth_data;
  3254. if (ifmgd->associated)
  3255. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  3256. sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
  3257. err = ieee80211_prep_connection(sdata, req->bss, false);
  3258. if (err)
  3259. goto err_clear;
  3260. err = ieee80211_probe_auth(sdata);
  3261. if (err) {
  3262. sta_info_destroy_addr(sdata, req->bss->bssid);
  3263. goto err_clear;
  3264. }
  3265. /* hold our own reference */
  3266. cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
  3267. err = 0;
  3268. goto out_unlock;
  3269. err_clear:
  3270. memset(ifmgd->bssid, 0, ETH_ALEN);
  3271. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  3272. ifmgd->auth_data = NULL;
  3273. err_free:
  3274. kfree(auth_data);
  3275. out_unlock:
  3276. mutex_unlock(&ifmgd->mtx);
  3277. return err;
  3278. }
  3279. int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
  3280. struct cfg80211_assoc_request *req)
  3281. {
  3282. struct ieee80211_local *local = sdata->local;
  3283. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3284. struct ieee80211_bss *bss = (void *)req->bss->priv;
  3285. struct ieee80211_mgd_assoc_data *assoc_data;
  3286. const struct cfg80211_bss_ies *beacon_ies;
  3287. struct ieee80211_supported_band *sband;
  3288. const u8 *ssidie, *ht_ie, *vht_ie;
  3289. int i, err;
  3290. assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
  3291. if (!assoc_data)
  3292. return -ENOMEM;
  3293. rcu_read_lock();
  3294. ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  3295. if (!ssidie) {
  3296. rcu_read_unlock();
  3297. kfree(assoc_data);
  3298. return -EINVAL;
  3299. }
  3300. memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
  3301. assoc_data->ssid_len = ssidie[1];
  3302. rcu_read_unlock();
  3303. mutex_lock(&ifmgd->mtx);
  3304. if (ifmgd->associated)
  3305. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  3306. if (ifmgd->auth_data && !ifmgd->auth_data->done) {
  3307. err = -EBUSY;
  3308. goto err_free;
  3309. }
  3310. if (ifmgd->assoc_data) {
  3311. err = -EBUSY;
  3312. goto err_free;
  3313. }
  3314. if (ifmgd->auth_data) {
  3315. bool match;
  3316. /* keep sta info, bssid if matching */
  3317. match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
  3318. ieee80211_destroy_auth_data(sdata, match);
  3319. }
  3320. /* prepare assoc data */
  3321. ifmgd->beacon_crc_valid = false;
  3322. /*
  3323. * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
  3324. * We still associate in non-HT mode (11a/b/g) if any one of these
  3325. * ciphers is configured as pairwise.
  3326. * We can set this to true for non-11n hardware, that'll be checked
  3327. * separately along with the peer capabilities.
  3328. */
  3329. for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
  3330. if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
  3331. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
  3332. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
  3333. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3334. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3335. netdev_info(sdata->dev,
  3336. "disabling HT/VHT due to WEP/TKIP use\n");
  3337. }
  3338. }
  3339. if (req->flags & ASSOC_REQ_DISABLE_HT) {
  3340. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3341. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3342. }
  3343. if (req->flags & ASSOC_REQ_DISABLE_VHT)
  3344. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3345. /* Also disable HT if we don't support it or the AP doesn't use WMM */
  3346. sband = local->hw.wiphy->bands[req->bss->channel->band];
  3347. if (!sband->ht_cap.ht_supported ||
  3348. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
  3349. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3350. if (!bss->wmm_used)
  3351. netdev_info(sdata->dev,
  3352. "disabling HT as WMM/QoS is not supported by the AP\n");
  3353. }
  3354. /* disable VHT if we don't support it or the AP doesn't use WMM */
  3355. if (!sband->vht_cap.vht_supported ||
  3356. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
  3357. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3358. if (!bss->wmm_used)
  3359. netdev_info(sdata->dev,
  3360. "disabling VHT as WMM/QoS is not supported by the AP\n");
  3361. }
  3362. memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
  3363. memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
  3364. sizeof(ifmgd->ht_capa_mask));
  3365. memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
  3366. memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
  3367. sizeof(ifmgd->vht_capa_mask));
  3368. if (req->ie && req->ie_len) {
  3369. memcpy(assoc_data->ie, req->ie, req->ie_len);
  3370. assoc_data->ie_len = req->ie_len;
  3371. }
  3372. assoc_data->bss = req->bss;
  3373. if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
  3374. if (ifmgd->powersave)
  3375. sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
  3376. else
  3377. sdata->smps_mode = IEEE80211_SMPS_OFF;
  3378. } else
  3379. sdata->smps_mode = ifmgd->req_smps;
  3380. assoc_data->capability = req->bss->capability;
  3381. assoc_data->wmm = bss->wmm_used &&
  3382. (local->hw.queues >= IEEE80211_NUM_ACS);
  3383. assoc_data->supp_rates = bss->supp_rates;
  3384. assoc_data->supp_rates_len = bss->supp_rates_len;
  3385. rcu_read_lock();
  3386. ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
  3387. if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
  3388. assoc_data->ap_ht_param =
  3389. ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
  3390. else
  3391. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3392. vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
  3393. if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
  3394. memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
  3395. sizeof(struct ieee80211_vht_cap));
  3396. else
  3397. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3398. rcu_read_unlock();
  3399. if (bss->wmm_used && bss->uapsd_supported &&
  3400. (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
  3401. assoc_data->uapsd = true;
  3402. ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
  3403. } else {
  3404. assoc_data->uapsd = false;
  3405. ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
  3406. }
  3407. if (req->prev_bssid)
  3408. memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
  3409. if (req->use_mfp) {
  3410. ifmgd->mfp = IEEE80211_MFP_REQUIRED;
  3411. ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
  3412. } else {
  3413. ifmgd->mfp = IEEE80211_MFP_DISABLED;
  3414. ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
  3415. }
  3416. if (req->crypto.control_port)
  3417. ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
  3418. else
  3419. ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
  3420. sdata->control_port_protocol = req->crypto.control_port_ethertype;
  3421. sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
  3422. /* kick off associate process */
  3423. ifmgd->assoc_data = assoc_data;
  3424. ifmgd->dtim_period = 0;
  3425. err = ieee80211_prep_connection(sdata, req->bss, true);
  3426. if (err)
  3427. goto err_clear;
  3428. rcu_read_lock();
  3429. beacon_ies = rcu_dereference(req->bss->beacon_ies);
  3430. if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
  3431. !beacon_ies) {
  3432. /*
  3433. * Wait up to one beacon interval ...
  3434. * should this be more if we miss one?
  3435. */
  3436. sdata_info(sdata, "waiting for beacon from %pM\n",
  3437. ifmgd->bssid);
  3438. assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
  3439. assoc_data->timeout_started = true;
  3440. assoc_data->need_beacon = true;
  3441. } else if (beacon_ies) {
  3442. const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
  3443. beacon_ies->data,
  3444. beacon_ies->len);
  3445. u8 dtim_count = 0;
  3446. if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
  3447. const struct ieee80211_tim_ie *tim;
  3448. tim = (void *)(tim_ie + 2);
  3449. ifmgd->dtim_period = tim->dtim_period;
  3450. dtim_count = tim->dtim_count;
  3451. }
  3452. assoc_data->have_beacon = true;
  3453. assoc_data->timeout = jiffies;
  3454. assoc_data->timeout_started = true;
  3455. if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
  3456. sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
  3457. sdata->vif.bss_conf.sync_device_ts =
  3458. bss->device_ts_beacon;
  3459. sdata->vif.bss_conf.sync_dtim_count = dtim_count;
  3460. }
  3461. } else {
  3462. assoc_data->timeout = jiffies;
  3463. assoc_data->timeout_started = true;
  3464. }
  3465. rcu_read_unlock();
  3466. run_again(ifmgd, assoc_data->timeout);
  3467. if (bss->corrupt_data) {
  3468. char *corrupt_type = "data";
  3469. if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
  3470. if (bss->corrupt_data &
  3471. IEEE80211_BSS_CORRUPT_PROBE_RESP)
  3472. corrupt_type = "beacon and probe response";
  3473. else
  3474. corrupt_type = "beacon";
  3475. } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
  3476. corrupt_type = "probe response";
  3477. sdata_info(sdata, "associating with AP with corrupt %s\n",
  3478. corrupt_type);
  3479. }
  3480. err = 0;
  3481. goto out;
  3482. err_clear:
  3483. memset(ifmgd->bssid, 0, ETH_ALEN);
  3484. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  3485. ifmgd->assoc_data = NULL;
  3486. err_free:
  3487. kfree(assoc_data);
  3488. out:
  3489. mutex_unlock(&ifmgd->mtx);
  3490. return err;
  3491. }
  3492. int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
  3493. struct cfg80211_deauth_request *req)
  3494. {
  3495. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3496. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3497. bool tx = !req->local_state_change;
  3498. bool sent_frame = false;
  3499. mutex_lock(&ifmgd->mtx);
  3500. sdata_info(sdata,
  3501. "deauthenticating from %pM by local choice (reason=%d)\n",
  3502. req->bssid, req->reason_code);
  3503. if (ifmgd->auth_data) {
  3504. drv_mgd_prepare_tx(sdata->local, sdata);
  3505. ieee80211_send_deauth_disassoc(sdata, req->bssid,
  3506. IEEE80211_STYPE_DEAUTH,
  3507. req->reason_code, tx,
  3508. frame_buf);
  3509. ieee80211_destroy_auth_data(sdata, false);
  3510. mutex_unlock(&ifmgd->mtx);
  3511. sent_frame = tx;
  3512. goto out;
  3513. }
  3514. if (ifmgd->associated &&
  3515. ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
  3516. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  3517. req->reason_code, tx, frame_buf);
  3518. sent_frame = tx;
  3519. }
  3520. mutex_unlock(&ifmgd->mtx);
  3521. out:
  3522. if (sent_frame)
  3523. __cfg80211_send_deauth(sdata->dev, frame_buf,
  3524. IEEE80211_DEAUTH_FRAME_LEN);
  3525. return 0;
  3526. }
  3527. int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
  3528. struct cfg80211_disassoc_request *req)
  3529. {
  3530. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3531. u8 bssid[ETH_ALEN];
  3532. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3533. mutex_lock(&ifmgd->mtx);
  3534. /*
  3535. * cfg80211 should catch this ... but it's racy since
  3536. * we can receive a disassoc frame, process it, hand it
  3537. * to cfg80211 while that's in a locked section already
  3538. * trying to tell us that the user wants to disconnect.
  3539. */
  3540. if (ifmgd->associated != req->bss) {
  3541. mutex_unlock(&ifmgd->mtx);
  3542. return -ENOLINK;
  3543. }
  3544. sdata_info(sdata,
  3545. "disassociating from %pM by local choice (reason=%d)\n",
  3546. req->bss->bssid, req->reason_code);
  3547. memcpy(bssid, req->bss->bssid, ETH_ALEN);
  3548. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
  3549. req->reason_code, !req->local_state_change,
  3550. frame_buf);
  3551. mutex_unlock(&ifmgd->mtx);
  3552. __cfg80211_send_disassoc(sdata->dev, frame_buf,
  3553. IEEE80211_DEAUTH_FRAME_LEN);
  3554. return 0;
  3555. }
  3556. void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
  3557. {
  3558. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3559. /*
  3560. * Make sure some work items will not run after this,
  3561. * they will not do anything but might not have been
  3562. * cancelled when disconnecting.
  3563. */
  3564. cancel_work_sync(&ifmgd->monitor_work);
  3565. cancel_work_sync(&ifmgd->beacon_connection_loss_work);
  3566. cancel_work_sync(&ifmgd->request_smps_work);
  3567. cancel_work_sync(&ifmgd->csa_connection_drop_work);
  3568. cancel_work_sync(&ifmgd->chswitch_work);
  3569. mutex_lock(&ifmgd->mtx);
  3570. if (ifmgd->assoc_data)
  3571. ieee80211_destroy_assoc_data(sdata, false);
  3572. if (ifmgd->auth_data)
  3573. ieee80211_destroy_auth_data(sdata, false);
  3574. del_timer_sync(&ifmgd->timer);
  3575. mutex_unlock(&ifmgd->mtx);
  3576. }
  3577. void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
  3578. enum nl80211_cqm_rssi_threshold_event rssi_event,
  3579. gfp_t gfp)
  3580. {
  3581. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  3582. trace_api_cqm_rssi_notify(sdata, rssi_event);
  3583. cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
  3584. }
  3585. EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);