mlme.c 126 KB

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