mlme.c 120 KB

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