mlme.c 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578
  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. /* keep width for 5/10 MHz channels */
  968. switch (sdata->vif.bss_conf.chandef.width) {
  969. case NL80211_CHAN_WIDTH_5:
  970. case NL80211_CHAN_WIDTH_10:
  971. new_chandef.width = sdata->vif.bss_conf.chandef.width;
  972. break;
  973. default:
  974. break;
  975. }
  976. break;
  977. }
  978. if (wide_bw_chansw_ie) {
  979. new_vht_chandef.chan = new_chan;
  980. new_vht_chandef.center_freq1 =
  981. ieee80211_channel_to_frequency(
  982. wide_bw_chansw_ie->new_center_freq_seg0,
  983. new_band);
  984. switch (wide_bw_chansw_ie->new_channel_width) {
  985. default:
  986. /* hmmm, ignore VHT and use HT if present */
  987. case IEEE80211_VHT_CHANWIDTH_USE_HT:
  988. new_vht_chandef.chan = NULL;
  989. break;
  990. case IEEE80211_VHT_CHANWIDTH_80MHZ:
  991. new_vht_chandef.width = NL80211_CHAN_WIDTH_80;
  992. break;
  993. case IEEE80211_VHT_CHANWIDTH_160MHZ:
  994. new_vht_chandef.width = NL80211_CHAN_WIDTH_160;
  995. break;
  996. case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
  997. /* field is otherwise reserved */
  998. new_vht_chandef.center_freq2 =
  999. ieee80211_channel_to_frequency(
  1000. wide_bw_chansw_ie->new_center_freq_seg1,
  1001. new_band);
  1002. new_vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
  1003. break;
  1004. }
  1005. if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
  1006. new_vht_chandef.width == NL80211_CHAN_WIDTH_80P80)
  1007. chandef_downgrade(&new_vht_chandef);
  1008. if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
  1009. new_vht_chandef.width == NL80211_CHAN_WIDTH_160)
  1010. chandef_downgrade(&new_vht_chandef);
  1011. if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
  1012. new_vht_chandef.width > NL80211_CHAN_WIDTH_20)
  1013. chandef_downgrade(&new_vht_chandef);
  1014. }
  1015. /* if VHT data is there validate & use it */
  1016. if (new_vht_chandef.chan) {
  1017. if (!cfg80211_chandef_compatible(&new_vht_chandef,
  1018. &new_chandef)) {
  1019. sdata_info(sdata,
  1020. "AP %pM CSA has inconsistent channel data, disconnecting\n",
  1021. ifmgd->associated->bssid);
  1022. ieee80211_queue_work(&local->hw,
  1023. &ifmgd->csa_connection_drop_work);
  1024. return;
  1025. }
  1026. new_chandef = new_vht_chandef;
  1027. }
  1028. if (!cfg80211_chandef_usable(local->hw.wiphy, &new_chandef,
  1029. IEEE80211_CHAN_DISABLED)) {
  1030. sdata_info(sdata,
  1031. "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
  1032. ifmgd->associated->bssid, new_freq,
  1033. new_chandef.width, new_chandef.center_freq1,
  1034. new_chandef.center_freq2);
  1035. ieee80211_queue_work(&local->hw,
  1036. &ifmgd->csa_connection_drop_work);
  1037. return;
  1038. }
  1039. ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
  1040. if (local->use_chanctx) {
  1041. sdata_info(sdata,
  1042. "not handling channel switch with channel contexts\n");
  1043. ieee80211_queue_work(&local->hw,
  1044. &ifmgd->csa_connection_drop_work);
  1045. return;
  1046. }
  1047. mutex_lock(&local->chanctx_mtx);
  1048. if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
  1049. mutex_unlock(&local->chanctx_mtx);
  1050. return;
  1051. }
  1052. chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
  1053. struct ieee80211_chanctx, conf);
  1054. if (chanctx->refcount > 1) {
  1055. sdata_info(sdata,
  1056. "channel switch with multiple interfaces on the same channel, disconnecting\n");
  1057. ieee80211_queue_work(&local->hw,
  1058. &ifmgd->csa_connection_drop_work);
  1059. mutex_unlock(&local->chanctx_mtx);
  1060. return;
  1061. }
  1062. mutex_unlock(&local->chanctx_mtx);
  1063. local->csa_chandef = new_chandef;
  1064. if (mode)
  1065. ieee80211_stop_queues_by_reason(&local->hw,
  1066. IEEE80211_MAX_QUEUE_MAP,
  1067. IEEE80211_QUEUE_STOP_REASON_CSA);
  1068. if (local->ops->channel_switch) {
  1069. /* use driver's channel switch callback */
  1070. struct ieee80211_channel_switch ch_switch = {
  1071. .timestamp = timestamp,
  1072. .block_tx = mode,
  1073. .chandef = new_chandef,
  1074. .count = count,
  1075. };
  1076. drv_channel_switch(local, &ch_switch);
  1077. return;
  1078. }
  1079. /* channel switch handled in software */
  1080. if (count <= 1)
  1081. ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
  1082. else
  1083. mod_timer(&ifmgd->chswitch_timer,
  1084. TU_TO_EXP_TIME(count * cbss->beacon_interval));
  1085. }
  1086. static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
  1087. struct ieee80211_channel *channel,
  1088. const u8 *country_ie, u8 country_ie_len,
  1089. const u8 *pwr_constr_elem)
  1090. {
  1091. struct ieee80211_country_ie_triplet *triplet;
  1092. int chan = ieee80211_frequency_to_channel(channel->center_freq);
  1093. int i, chan_pwr, chan_increment, new_ap_level;
  1094. bool have_chan_pwr = false;
  1095. /* Invalid IE */
  1096. if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  1097. return 0;
  1098. triplet = (void *)(country_ie + 3);
  1099. country_ie_len -= 3;
  1100. switch (channel->band) {
  1101. default:
  1102. WARN_ON_ONCE(1);
  1103. /* fall through */
  1104. case IEEE80211_BAND_2GHZ:
  1105. case IEEE80211_BAND_60GHZ:
  1106. chan_increment = 1;
  1107. break;
  1108. case IEEE80211_BAND_5GHZ:
  1109. chan_increment = 4;
  1110. break;
  1111. }
  1112. /* find channel */
  1113. while (country_ie_len >= 3) {
  1114. u8 first_channel = triplet->chans.first_channel;
  1115. if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
  1116. goto next;
  1117. for (i = 0; i < triplet->chans.num_channels; i++) {
  1118. if (first_channel + i * chan_increment == chan) {
  1119. have_chan_pwr = true;
  1120. chan_pwr = triplet->chans.max_power;
  1121. break;
  1122. }
  1123. }
  1124. if (have_chan_pwr)
  1125. break;
  1126. next:
  1127. triplet++;
  1128. country_ie_len -= 3;
  1129. }
  1130. if (!have_chan_pwr)
  1131. return 0;
  1132. new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
  1133. if (sdata->ap_power_level == new_ap_level)
  1134. return 0;
  1135. sdata_info(sdata,
  1136. "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
  1137. new_ap_level, chan_pwr, *pwr_constr_elem,
  1138. sdata->u.mgd.bssid);
  1139. sdata->ap_power_level = new_ap_level;
  1140. if (__ieee80211_recalc_txpower(sdata))
  1141. return BSS_CHANGED_TXPOWER;
  1142. return 0;
  1143. }
  1144. /* powersave */
  1145. static void ieee80211_enable_ps(struct ieee80211_local *local,
  1146. struct ieee80211_sub_if_data *sdata)
  1147. {
  1148. struct ieee80211_conf *conf = &local->hw.conf;
  1149. /*
  1150. * If we are scanning right now then the parameters will
  1151. * take effect when scan finishes.
  1152. */
  1153. if (local->scanning)
  1154. return;
  1155. if (conf->dynamic_ps_timeout > 0 &&
  1156. !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
  1157. mod_timer(&local->dynamic_ps_timer, jiffies +
  1158. msecs_to_jiffies(conf->dynamic_ps_timeout));
  1159. } else {
  1160. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  1161. ieee80211_send_nullfunc(local, sdata, 1);
  1162. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  1163. (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
  1164. return;
  1165. conf->flags |= IEEE80211_CONF_PS;
  1166. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1167. }
  1168. }
  1169. static void ieee80211_change_ps(struct ieee80211_local *local)
  1170. {
  1171. struct ieee80211_conf *conf = &local->hw.conf;
  1172. if (local->ps_sdata) {
  1173. ieee80211_enable_ps(local, local->ps_sdata);
  1174. } else if (conf->flags & IEEE80211_CONF_PS) {
  1175. conf->flags &= ~IEEE80211_CONF_PS;
  1176. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1177. del_timer_sync(&local->dynamic_ps_timer);
  1178. cancel_work_sync(&local->dynamic_ps_enable_work);
  1179. }
  1180. }
  1181. static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
  1182. {
  1183. struct ieee80211_if_managed *mgd = &sdata->u.mgd;
  1184. struct sta_info *sta = NULL;
  1185. bool authorized = false;
  1186. if (!mgd->powersave)
  1187. return false;
  1188. if (mgd->broken_ap)
  1189. return false;
  1190. if (!mgd->associated)
  1191. return false;
  1192. if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
  1193. IEEE80211_STA_CONNECTION_POLL))
  1194. return false;
  1195. if (!mgd->have_beacon)
  1196. return false;
  1197. rcu_read_lock();
  1198. sta = sta_info_get(sdata, mgd->bssid);
  1199. if (sta)
  1200. authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
  1201. rcu_read_unlock();
  1202. return authorized;
  1203. }
  1204. /* need to hold RTNL or interface lock */
  1205. void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
  1206. {
  1207. struct ieee80211_sub_if_data *sdata, *found = NULL;
  1208. int count = 0;
  1209. int timeout;
  1210. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
  1211. local->ps_sdata = NULL;
  1212. return;
  1213. }
  1214. list_for_each_entry(sdata, &local->interfaces, list) {
  1215. if (!ieee80211_sdata_running(sdata))
  1216. continue;
  1217. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1218. /* If an AP vif is found, then disable PS
  1219. * by setting the count to zero thereby setting
  1220. * ps_sdata to NULL.
  1221. */
  1222. count = 0;
  1223. break;
  1224. }
  1225. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1226. continue;
  1227. found = sdata;
  1228. count++;
  1229. }
  1230. if (count == 1 && ieee80211_powersave_allowed(found)) {
  1231. s32 beaconint_us;
  1232. if (latency < 0)
  1233. latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
  1234. beaconint_us = ieee80211_tu_to_usec(
  1235. found->vif.bss_conf.beacon_int);
  1236. timeout = local->dynamic_ps_forced_timeout;
  1237. if (timeout < 0) {
  1238. /*
  1239. * Go to full PSM if the user configures a very low
  1240. * latency requirement.
  1241. * The 2000 second value is there for compatibility
  1242. * until the PM_QOS_NETWORK_LATENCY is configured
  1243. * with real values.
  1244. */
  1245. if (latency > (1900 * USEC_PER_MSEC) &&
  1246. latency != (2000 * USEC_PER_SEC))
  1247. timeout = 0;
  1248. else
  1249. timeout = 100;
  1250. }
  1251. local->hw.conf.dynamic_ps_timeout = timeout;
  1252. if (beaconint_us > latency) {
  1253. local->ps_sdata = NULL;
  1254. } else {
  1255. int maxslp = 1;
  1256. u8 dtimper = found->u.mgd.dtim_period;
  1257. /* If the TIM IE is invalid, pretend the value is 1 */
  1258. if (!dtimper)
  1259. dtimper = 1;
  1260. else if (dtimper > 1)
  1261. maxslp = min_t(int, dtimper,
  1262. latency / beaconint_us);
  1263. local->hw.conf.max_sleep_period = maxslp;
  1264. local->hw.conf.ps_dtim_period = dtimper;
  1265. local->ps_sdata = found;
  1266. }
  1267. } else {
  1268. local->ps_sdata = NULL;
  1269. }
  1270. ieee80211_change_ps(local);
  1271. }
  1272. void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
  1273. {
  1274. bool ps_allowed = ieee80211_powersave_allowed(sdata);
  1275. if (sdata->vif.bss_conf.ps != ps_allowed) {
  1276. sdata->vif.bss_conf.ps = ps_allowed;
  1277. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
  1278. }
  1279. }
  1280. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  1281. {
  1282. struct ieee80211_local *local =
  1283. container_of(work, struct ieee80211_local,
  1284. dynamic_ps_disable_work);
  1285. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1286. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1287. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1288. }
  1289. ieee80211_wake_queues_by_reason(&local->hw,
  1290. IEEE80211_MAX_QUEUE_MAP,
  1291. IEEE80211_QUEUE_STOP_REASON_PS);
  1292. }
  1293. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  1294. {
  1295. struct ieee80211_local *local =
  1296. container_of(work, struct ieee80211_local,
  1297. dynamic_ps_enable_work);
  1298. struct ieee80211_sub_if_data *sdata = local->ps_sdata;
  1299. struct ieee80211_if_managed *ifmgd;
  1300. unsigned long flags;
  1301. int q;
  1302. /* can only happen when PS was just disabled anyway */
  1303. if (!sdata)
  1304. return;
  1305. ifmgd = &sdata->u.mgd;
  1306. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  1307. return;
  1308. if (local->hw.conf.dynamic_ps_timeout > 0) {
  1309. /* don't enter PS if TX frames are pending */
  1310. if (drv_tx_frames_pending(local)) {
  1311. mod_timer(&local->dynamic_ps_timer, jiffies +
  1312. msecs_to_jiffies(
  1313. local->hw.conf.dynamic_ps_timeout));
  1314. return;
  1315. }
  1316. /*
  1317. * transmission can be stopped by others which leads to
  1318. * dynamic_ps_timer expiry. Postpone the ps timer if it
  1319. * is not the actual idle state.
  1320. */
  1321. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  1322. for (q = 0; q < local->hw.queues; q++) {
  1323. if (local->queue_stop_reasons[q]) {
  1324. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  1325. flags);
  1326. mod_timer(&local->dynamic_ps_timer, jiffies +
  1327. msecs_to_jiffies(
  1328. local->hw.conf.dynamic_ps_timeout));
  1329. return;
  1330. }
  1331. }
  1332. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  1333. }
  1334. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  1335. !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  1336. if (drv_tx_frames_pending(local)) {
  1337. mod_timer(&local->dynamic_ps_timer, jiffies +
  1338. msecs_to_jiffies(
  1339. local->hw.conf.dynamic_ps_timeout));
  1340. } else {
  1341. ieee80211_send_nullfunc(local, sdata, 1);
  1342. /* Flush to get the tx status of nullfunc frame */
  1343. ieee80211_flush_queues(local, sdata);
  1344. }
  1345. }
  1346. if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
  1347. (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
  1348. (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  1349. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  1350. local->hw.conf.flags |= IEEE80211_CONF_PS;
  1351. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1352. }
  1353. }
  1354. void ieee80211_dynamic_ps_timer(unsigned long data)
  1355. {
  1356. struct ieee80211_local *local = (void *) data;
  1357. if (local->quiescing || local->suspended)
  1358. return;
  1359. ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
  1360. }
  1361. void ieee80211_dfs_cac_timer_work(struct work_struct *work)
  1362. {
  1363. struct delayed_work *delayed_work =
  1364. container_of(work, struct delayed_work, work);
  1365. struct ieee80211_sub_if_data *sdata =
  1366. container_of(delayed_work, struct ieee80211_sub_if_data,
  1367. dfs_cac_timer_work);
  1368. ieee80211_vif_release_channel(sdata);
  1369. cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
  1370. }
  1371. /* MLME */
  1372. static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
  1373. struct ieee80211_sub_if_data *sdata,
  1374. const u8 *wmm_param, size_t wmm_param_len)
  1375. {
  1376. struct ieee80211_tx_queue_params params;
  1377. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1378. size_t left;
  1379. int count;
  1380. const u8 *pos;
  1381. u8 uapsd_queues = 0;
  1382. if (!local->ops->conf_tx)
  1383. return false;
  1384. if (local->hw.queues < IEEE80211_NUM_ACS)
  1385. return false;
  1386. if (!wmm_param)
  1387. return false;
  1388. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  1389. return false;
  1390. if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
  1391. uapsd_queues = ifmgd->uapsd_queues;
  1392. count = wmm_param[6] & 0x0f;
  1393. if (count == ifmgd->wmm_last_param_set)
  1394. return false;
  1395. ifmgd->wmm_last_param_set = count;
  1396. pos = wmm_param + 8;
  1397. left = wmm_param_len - 8;
  1398. memset(&params, 0, sizeof(params));
  1399. sdata->wmm_acm = 0;
  1400. for (; left >= 4; left -= 4, pos += 4) {
  1401. int aci = (pos[0] >> 5) & 0x03;
  1402. int acm = (pos[0] >> 4) & 0x01;
  1403. bool uapsd = false;
  1404. int queue;
  1405. switch (aci) {
  1406. case 1: /* AC_BK */
  1407. queue = 3;
  1408. if (acm)
  1409. sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
  1410. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
  1411. uapsd = true;
  1412. break;
  1413. case 2: /* AC_VI */
  1414. queue = 1;
  1415. if (acm)
  1416. sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
  1417. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
  1418. uapsd = true;
  1419. break;
  1420. case 3: /* AC_VO */
  1421. queue = 0;
  1422. if (acm)
  1423. sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
  1424. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  1425. uapsd = true;
  1426. break;
  1427. case 0: /* AC_BE */
  1428. default:
  1429. queue = 2;
  1430. if (acm)
  1431. sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
  1432. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
  1433. uapsd = true;
  1434. break;
  1435. }
  1436. params.aifs = pos[0] & 0x0f;
  1437. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  1438. params.cw_min = ecw2cw(pos[1] & 0x0f);
  1439. params.txop = get_unaligned_le16(pos + 2);
  1440. params.acm = acm;
  1441. params.uapsd = uapsd;
  1442. mlme_dbg(sdata,
  1443. "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
  1444. queue, aci, acm,
  1445. params.aifs, params.cw_min, params.cw_max,
  1446. params.txop, params.uapsd);
  1447. sdata->tx_conf[queue] = params;
  1448. if (drv_conf_tx(local, sdata, queue, &params))
  1449. sdata_err(sdata,
  1450. "failed to set TX queue parameters for queue %d\n",
  1451. queue);
  1452. }
  1453. /* enable WMM or activate new settings */
  1454. sdata->vif.bss_conf.qos = true;
  1455. return true;
  1456. }
  1457. static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
  1458. {
  1459. lockdep_assert_held(&sdata->local->mtx);
  1460. sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  1461. IEEE80211_STA_BEACON_POLL);
  1462. ieee80211_run_deferred_scan(sdata->local);
  1463. }
  1464. static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
  1465. {
  1466. mutex_lock(&sdata->local->mtx);
  1467. __ieee80211_stop_poll(sdata);
  1468. mutex_unlock(&sdata->local->mtx);
  1469. }
  1470. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  1471. u16 capab, bool erp_valid, u8 erp)
  1472. {
  1473. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1474. u32 changed = 0;
  1475. bool use_protection;
  1476. bool use_short_preamble;
  1477. bool use_short_slot;
  1478. if (erp_valid) {
  1479. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  1480. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  1481. } else {
  1482. use_protection = false;
  1483. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  1484. }
  1485. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  1486. if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
  1487. use_short_slot = true;
  1488. if (use_protection != bss_conf->use_cts_prot) {
  1489. bss_conf->use_cts_prot = use_protection;
  1490. changed |= BSS_CHANGED_ERP_CTS_PROT;
  1491. }
  1492. if (use_short_preamble != bss_conf->use_short_preamble) {
  1493. bss_conf->use_short_preamble = use_short_preamble;
  1494. changed |= BSS_CHANGED_ERP_PREAMBLE;
  1495. }
  1496. if (use_short_slot != bss_conf->use_short_slot) {
  1497. bss_conf->use_short_slot = use_short_slot;
  1498. changed |= BSS_CHANGED_ERP_SLOT;
  1499. }
  1500. return changed;
  1501. }
  1502. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  1503. struct cfg80211_bss *cbss,
  1504. u32 bss_info_changed)
  1505. {
  1506. struct ieee80211_bss *bss = (void *)cbss->priv;
  1507. struct ieee80211_local *local = sdata->local;
  1508. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1509. bss_info_changed |= BSS_CHANGED_ASSOC;
  1510. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  1511. bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
  1512. sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
  1513. beacon_loss_count * bss_conf->beacon_int));
  1514. sdata->u.mgd.associated = cbss;
  1515. memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
  1516. sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
  1517. if (sdata->vif.p2p) {
  1518. const struct cfg80211_bss_ies *ies;
  1519. rcu_read_lock();
  1520. ies = rcu_dereference(cbss->ies);
  1521. if (ies) {
  1522. int ret;
  1523. ret = cfg80211_get_p2p_attr(
  1524. ies->data, ies->len,
  1525. IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
  1526. (u8 *) &bss_conf->p2p_noa_attr,
  1527. sizeof(bss_conf->p2p_noa_attr));
  1528. if (ret >= 2) {
  1529. sdata->u.mgd.p2p_noa_index =
  1530. bss_conf->p2p_noa_attr.index;
  1531. bss_info_changed |= BSS_CHANGED_P2P_PS;
  1532. }
  1533. }
  1534. rcu_read_unlock();
  1535. }
  1536. /* just to be sure */
  1537. ieee80211_stop_poll(sdata);
  1538. ieee80211_led_assoc(local, 1);
  1539. if (sdata->u.mgd.have_beacon) {
  1540. /*
  1541. * If the AP is buggy we may get here with no DTIM period
  1542. * known, so assume it's 1 which is the only safe assumption
  1543. * in that case, although if the TIM IE is broken powersave
  1544. * probably just won't work at all.
  1545. */
  1546. bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
  1547. bss_conf->beacon_rate = bss->beacon_rate;
  1548. bss_info_changed |= BSS_CHANGED_BEACON_INFO;
  1549. } else {
  1550. bss_conf->beacon_rate = NULL;
  1551. bss_conf->dtim_period = 0;
  1552. }
  1553. bss_conf->assoc = 1;
  1554. /* Tell the driver to monitor connection quality (if supported) */
  1555. if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
  1556. bss_conf->cqm_rssi_thold)
  1557. bss_info_changed |= BSS_CHANGED_CQM;
  1558. /* Enable ARP filtering */
  1559. if (bss_conf->arp_addr_cnt)
  1560. bss_info_changed |= BSS_CHANGED_ARP_FILTER;
  1561. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  1562. mutex_lock(&local->iflist_mtx);
  1563. ieee80211_recalc_ps(local, -1);
  1564. mutex_unlock(&local->iflist_mtx);
  1565. ieee80211_recalc_smps(sdata);
  1566. ieee80211_recalc_ps_vif(sdata);
  1567. netif_carrier_on(sdata->dev);
  1568. }
  1569. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  1570. u16 stype, u16 reason, bool tx,
  1571. u8 *frame_buf)
  1572. {
  1573. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1574. struct ieee80211_local *local = sdata->local;
  1575. u32 changed = 0;
  1576. sdata_assert_lock(sdata);
  1577. if (WARN_ON_ONCE(tx && !frame_buf))
  1578. return;
  1579. if (WARN_ON(!ifmgd->associated))
  1580. return;
  1581. ieee80211_stop_poll(sdata);
  1582. ifmgd->associated = NULL;
  1583. netif_carrier_off(sdata->dev);
  1584. /*
  1585. * if we want to get out of ps before disassoc (why?) we have
  1586. * to do it before sending disassoc, as otherwise the null-packet
  1587. * won't be valid.
  1588. */
  1589. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1590. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1591. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1592. }
  1593. local->ps_sdata = NULL;
  1594. /* disable per-vif ps */
  1595. ieee80211_recalc_ps_vif(sdata);
  1596. /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
  1597. if (tx)
  1598. ieee80211_flush_queues(local, sdata);
  1599. /* deauthenticate/disassociate now */
  1600. if (tx || frame_buf)
  1601. ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
  1602. reason, tx, frame_buf);
  1603. /* flush out frame */
  1604. if (tx)
  1605. ieee80211_flush_queues(local, sdata);
  1606. /* clear bssid only after building the needed mgmt frames */
  1607. memset(ifmgd->bssid, 0, ETH_ALEN);
  1608. /* remove AP and TDLS peers */
  1609. sta_info_flush_defer(sdata);
  1610. /* finally reset all BSS / config parameters */
  1611. changed |= ieee80211_reset_erp_info(sdata);
  1612. ieee80211_led_assoc(local, 0);
  1613. changed |= BSS_CHANGED_ASSOC;
  1614. sdata->vif.bss_conf.assoc = false;
  1615. ifmgd->p2p_noa_index = -1;
  1616. memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
  1617. sizeof(sdata->vif.bss_conf.p2p_noa_attr));
  1618. /* on the next assoc, re-program HT/VHT parameters */
  1619. memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
  1620. memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
  1621. memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
  1622. memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
  1623. sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
  1624. del_timer_sync(&local->dynamic_ps_timer);
  1625. cancel_work_sync(&local->dynamic_ps_enable_work);
  1626. /* Disable ARP filtering */
  1627. if (sdata->vif.bss_conf.arp_addr_cnt)
  1628. changed |= BSS_CHANGED_ARP_FILTER;
  1629. sdata->vif.bss_conf.qos = false;
  1630. changed |= BSS_CHANGED_QOS;
  1631. /* The BSSID (not really interesting) and HT changed */
  1632. changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
  1633. ieee80211_bss_info_change_notify(sdata, changed);
  1634. /* disassociated - set to defaults now */
  1635. ieee80211_set_wmm_default(sdata, false);
  1636. del_timer_sync(&sdata->u.mgd.conn_mon_timer);
  1637. del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
  1638. del_timer_sync(&sdata->u.mgd.timer);
  1639. del_timer_sync(&sdata->u.mgd.chswitch_timer);
  1640. sdata->vif.bss_conf.dtim_period = 0;
  1641. sdata->vif.bss_conf.beacon_rate = NULL;
  1642. ifmgd->have_beacon = false;
  1643. ifmgd->flags = 0;
  1644. ieee80211_vif_release_channel(sdata);
  1645. }
  1646. void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
  1647. struct ieee80211_hdr *hdr)
  1648. {
  1649. /*
  1650. * We can postpone the mgd.timer whenever receiving unicast frames
  1651. * from AP because we know that the connection is working both ways
  1652. * at that time. But multicast frames (and hence also beacons) must
  1653. * be ignored here, because we need to trigger the timer during
  1654. * data idle periods for sending the periodic probe request to the
  1655. * AP we're connected to.
  1656. */
  1657. if (is_multicast_ether_addr(hdr->addr1))
  1658. return;
  1659. ieee80211_sta_reset_conn_monitor(sdata);
  1660. }
  1661. static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
  1662. {
  1663. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1664. struct ieee80211_local *local = sdata->local;
  1665. mutex_lock(&local->mtx);
  1666. if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  1667. IEEE80211_STA_CONNECTION_POLL))) {
  1668. mutex_unlock(&local->mtx);
  1669. return;
  1670. }
  1671. __ieee80211_stop_poll(sdata);
  1672. mutex_lock(&local->iflist_mtx);
  1673. ieee80211_recalc_ps(local, -1);
  1674. mutex_unlock(&local->iflist_mtx);
  1675. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  1676. goto out;
  1677. /*
  1678. * We've received a probe response, but are not sure whether
  1679. * we have or will be receiving any beacons or data, so let's
  1680. * schedule the timers again, just in case.
  1681. */
  1682. ieee80211_sta_reset_beacon_monitor(sdata);
  1683. mod_timer(&ifmgd->conn_mon_timer,
  1684. round_jiffies_up(jiffies +
  1685. IEEE80211_CONNECTION_IDLE_TIME));
  1686. out:
  1687. mutex_unlock(&local->mtx);
  1688. }
  1689. void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
  1690. struct ieee80211_hdr *hdr, bool ack)
  1691. {
  1692. if (!ieee80211_is_data(hdr->frame_control))
  1693. return;
  1694. if (ieee80211_is_nullfunc(hdr->frame_control) &&
  1695. sdata->u.mgd.probe_send_count > 0) {
  1696. if (ack)
  1697. ieee80211_sta_reset_conn_monitor(sdata);
  1698. else
  1699. sdata->u.mgd.nullfunc_failed = true;
  1700. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  1701. return;
  1702. }
  1703. if (ack)
  1704. ieee80211_sta_reset_conn_monitor(sdata);
  1705. }
  1706. static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
  1707. {
  1708. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1709. const u8 *ssid;
  1710. u8 *dst = ifmgd->associated->bssid;
  1711. u8 unicast_limit = max(1, max_probe_tries - 3);
  1712. /*
  1713. * Try sending broadcast probe requests for the last three
  1714. * probe requests after the first ones failed since some
  1715. * buggy APs only support broadcast probe requests.
  1716. */
  1717. if (ifmgd->probe_send_count >= unicast_limit)
  1718. dst = NULL;
  1719. /*
  1720. * When the hardware reports an accurate Tx ACK status, it's
  1721. * better to send a nullfunc frame instead of a probe request,
  1722. * as it will kick us off the AP quickly if we aren't associated
  1723. * anymore. The timeout will be reset if the frame is ACKed by
  1724. * the AP.
  1725. */
  1726. ifmgd->probe_send_count++;
  1727. if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  1728. ifmgd->nullfunc_failed = false;
  1729. ieee80211_send_nullfunc(sdata->local, sdata, 0);
  1730. } else {
  1731. int ssid_len;
  1732. rcu_read_lock();
  1733. ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
  1734. if (WARN_ON_ONCE(ssid == NULL))
  1735. ssid_len = 0;
  1736. else
  1737. ssid_len = ssid[1];
  1738. ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
  1739. 0, (u32) -1, true, 0,
  1740. ifmgd->associated->channel, false);
  1741. rcu_read_unlock();
  1742. }
  1743. ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
  1744. run_again(sdata, ifmgd->probe_timeout);
  1745. if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  1746. ieee80211_flush_queues(sdata->local, sdata);
  1747. }
  1748. static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
  1749. bool beacon)
  1750. {
  1751. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1752. bool already = false;
  1753. if (!ieee80211_sdata_running(sdata))
  1754. return;
  1755. sdata_lock(sdata);
  1756. if (!ifmgd->associated)
  1757. goto out;
  1758. mutex_lock(&sdata->local->mtx);
  1759. if (sdata->local->tmp_channel || sdata->local->scanning) {
  1760. mutex_unlock(&sdata->local->mtx);
  1761. goto out;
  1762. }
  1763. if (beacon) {
  1764. mlme_dbg_ratelimited(sdata,
  1765. "detected beacon loss from AP (missed %d beacons) - probing\n",
  1766. beacon_loss_count);
  1767. ieee80211_cqm_rssi_notify(&sdata->vif,
  1768. NL80211_CQM_RSSI_BEACON_LOSS_EVENT,
  1769. GFP_KERNEL);
  1770. }
  1771. /*
  1772. * The driver/our work has already reported this event or the
  1773. * connection monitoring has kicked in and we have already sent
  1774. * a probe request. Or maybe the AP died and the driver keeps
  1775. * reporting until we disassociate...
  1776. *
  1777. * In either case we have to ignore the current call to this
  1778. * function (except for setting the correct probe reason bit)
  1779. * because otherwise we would reset the timer every time and
  1780. * never check whether we received a probe response!
  1781. */
  1782. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  1783. IEEE80211_STA_CONNECTION_POLL))
  1784. already = true;
  1785. if (beacon)
  1786. ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
  1787. else
  1788. ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
  1789. mutex_unlock(&sdata->local->mtx);
  1790. if (already)
  1791. goto out;
  1792. mutex_lock(&sdata->local->iflist_mtx);
  1793. ieee80211_recalc_ps(sdata->local, -1);
  1794. mutex_unlock(&sdata->local->iflist_mtx);
  1795. ifmgd->probe_send_count = 0;
  1796. ieee80211_mgd_probe_ap_send(sdata);
  1797. out:
  1798. sdata_unlock(sdata);
  1799. }
  1800. struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
  1801. struct ieee80211_vif *vif)
  1802. {
  1803. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1804. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1805. struct cfg80211_bss *cbss;
  1806. struct sk_buff *skb;
  1807. const u8 *ssid;
  1808. int ssid_len;
  1809. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1810. return NULL;
  1811. sdata_assert_lock(sdata);
  1812. if (ifmgd->associated)
  1813. cbss = ifmgd->associated;
  1814. else if (ifmgd->auth_data)
  1815. cbss = ifmgd->auth_data->bss;
  1816. else if (ifmgd->assoc_data)
  1817. cbss = ifmgd->assoc_data->bss;
  1818. else
  1819. return NULL;
  1820. rcu_read_lock();
  1821. ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
  1822. if (WARN_ON_ONCE(ssid == NULL))
  1823. ssid_len = 0;
  1824. else
  1825. ssid_len = ssid[1];
  1826. skb = ieee80211_build_probe_req(sdata, cbss->bssid,
  1827. (u32) -1, cbss->channel,
  1828. ssid + 2, ssid_len,
  1829. NULL, 0, true);
  1830. rcu_read_unlock();
  1831. return skb;
  1832. }
  1833. EXPORT_SYMBOL(ieee80211_ap_probereq_get);
  1834. static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
  1835. {
  1836. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1837. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  1838. sdata_lock(sdata);
  1839. if (!ifmgd->associated) {
  1840. sdata_unlock(sdata);
  1841. return;
  1842. }
  1843. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  1844. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  1845. true, frame_buf);
  1846. ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
  1847. ieee80211_wake_queues_by_reason(&sdata->local->hw,
  1848. IEEE80211_MAX_QUEUE_MAP,
  1849. IEEE80211_QUEUE_STOP_REASON_CSA);
  1850. cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
  1851. IEEE80211_DEAUTH_FRAME_LEN);
  1852. sdata_unlock(sdata);
  1853. }
  1854. static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
  1855. {
  1856. struct ieee80211_sub_if_data *sdata =
  1857. container_of(work, struct ieee80211_sub_if_data,
  1858. u.mgd.beacon_connection_loss_work);
  1859. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1860. struct sta_info *sta;
  1861. if (ifmgd->associated) {
  1862. rcu_read_lock();
  1863. sta = sta_info_get(sdata, ifmgd->bssid);
  1864. if (sta)
  1865. sta->beacon_loss_count++;
  1866. rcu_read_unlock();
  1867. }
  1868. if (ifmgd->connection_loss) {
  1869. sdata_info(sdata, "Connection to AP %pM lost\n",
  1870. ifmgd->bssid);
  1871. __ieee80211_disconnect(sdata);
  1872. } else {
  1873. ieee80211_mgd_probe_ap(sdata, true);
  1874. }
  1875. }
  1876. static void ieee80211_csa_connection_drop_work(struct work_struct *work)
  1877. {
  1878. struct ieee80211_sub_if_data *sdata =
  1879. container_of(work, struct ieee80211_sub_if_data,
  1880. u.mgd.csa_connection_drop_work);
  1881. __ieee80211_disconnect(sdata);
  1882. }
  1883. void ieee80211_beacon_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_beacon_loss(sdata);
  1888. sdata->u.mgd.connection_loss = false;
  1889. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1890. }
  1891. EXPORT_SYMBOL(ieee80211_beacon_loss);
  1892. void ieee80211_connection_loss(struct ieee80211_vif *vif)
  1893. {
  1894. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1895. struct ieee80211_hw *hw = &sdata->local->hw;
  1896. trace_api_connection_loss(sdata);
  1897. sdata->u.mgd.connection_loss = true;
  1898. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1899. }
  1900. EXPORT_SYMBOL(ieee80211_connection_loss);
  1901. static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
  1902. bool assoc)
  1903. {
  1904. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  1905. sdata_assert_lock(sdata);
  1906. if (!assoc) {
  1907. sta_info_destroy_addr(sdata, auth_data->bss->bssid);
  1908. memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
  1909. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  1910. sdata->u.mgd.flags = 0;
  1911. ieee80211_vif_release_channel(sdata);
  1912. }
  1913. cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
  1914. kfree(auth_data);
  1915. sdata->u.mgd.auth_data = NULL;
  1916. }
  1917. static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
  1918. struct ieee80211_mgmt *mgmt, size_t len)
  1919. {
  1920. struct ieee80211_local *local = sdata->local;
  1921. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  1922. u8 *pos;
  1923. struct ieee802_11_elems elems;
  1924. u32 tx_flags = 0;
  1925. pos = mgmt->u.auth.variable;
  1926. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
  1927. if (!elems.challenge)
  1928. return;
  1929. auth_data->expected_transaction = 4;
  1930. drv_mgd_prepare_tx(sdata->local, sdata);
  1931. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  1932. tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
  1933. IEEE80211_TX_INTFL_MLME_CONN_TX;
  1934. ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
  1935. elems.challenge - 2, elems.challenge_len + 2,
  1936. auth_data->bss->bssid, auth_data->bss->bssid,
  1937. auth_data->key, auth_data->key_len,
  1938. auth_data->key_idx, tx_flags);
  1939. }
  1940. static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
  1941. struct ieee80211_mgmt *mgmt, size_t len)
  1942. {
  1943. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1944. u8 bssid[ETH_ALEN];
  1945. u16 auth_alg, auth_transaction, status_code;
  1946. struct sta_info *sta;
  1947. sdata_assert_lock(sdata);
  1948. if (len < 24 + 6)
  1949. return;
  1950. if (!ifmgd->auth_data || ifmgd->auth_data->done)
  1951. return;
  1952. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  1953. if (!ether_addr_equal(bssid, mgmt->bssid))
  1954. return;
  1955. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  1956. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  1957. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  1958. if (auth_alg != ifmgd->auth_data->algorithm ||
  1959. auth_transaction != ifmgd->auth_data->expected_transaction) {
  1960. sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
  1961. mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
  1962. auth_transaction,
  1963. ifmgd->auth_data->expected_transaction);
  1964. return;
  1965. }
  1966. if (status_code != WLAN_STATUS_SUCCESS) {
  1967. sdata_info(sdata, "%pM denied authentication (status %d)\n",
  1968. mgmt->sa, status_code);
  1969. ieee80211_destroy_auth_data(sdata, false);
  1970. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  1971. return;
  1972. }
  1973. switch (ifmgd->auth_data->algorithm) {
  1974. case WLAN_AUTH_OPEN:
  1975. case WLAN_AUTH_LEAP:
  1976. case WLAN_AUTH_FT:
  1977. case WLAN_AUTH_SAE:
  1978. break;
  1979. case WLAN_AUTH_SHARED_KEY:
  1980. if (ifmgd->auth_data->expected_transaction != 4) {
  1981. ieee80211_auth_challenge(sdata, mgmt, len);
  1982. /* need another frame */
  1983. return;
  1984. }
  1985. break;
  1986. default:
  1987. WARN_ONCE(1, "invalid auth alg %d",
  1988. ifmgd->auth_data->algorithm);
  1989. return;
  1990. }
  1991. sdata_info(sdata, "authenticated\n");
  1992. ifmgd->auth_data->done = true;
  1993. ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
  1994. ifmgd->auth_data->timeout_started = true;
  1995. run_again(sdata, ifmgd->auth_data->timeout);
  1996. if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
  1997. ifmgd->auth_data->expected_transaction != 2) {
  1998. /*
  1999. * Report auth frame to user space for processing since another
  2000. * round of Authentication frames is still needed.
  2001. */
  2002. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  2003. return;
  2004. }
  2005. /* move station state to auth */
  2006. mutex_lock(&sdata->local->sta_mtx);
  2007. sta = sta_info_get(sdata, bssid);
  2008. if (!sta) {
  2009. WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
  2010. goto out_err;
  2011. }
  2012. if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
  2013. sdata_info(sdata, "failed moving %pM to auth\n", bssid);
  2014. goto out_err;
  2015. }
  2016. mutex_unlock(&sdata->local->sta_mtx);
  2017. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  2018. return;
  2019. out_err:
  2020. mutex_unlock(&sdata->local->sta_mtx);
  2021. /* ignore frame -- wait for timeout */
  2022. }
  2023. static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  2024. struct ieee80211_mgmt *mgmt, size_t len)
  2025. {
  2026. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2027. const u8 *bssid = NULL;
  2028. u16 reason_code;
  2029. sdata_assert_lock(sdata);
  2030. if (len < 24 + 2)
  2031. return;
  2032. if (!ifmgd->associated ||
  2033. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2034. return;
  2035. bssid = ifmgd->associated->bssid;
  2036. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  2037. sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
  2038. bssid, reason_code);
  2039. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  2040. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  2041. }
  2042. static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  2043. struct ieee80211_mgmt *mgmt, size_t len)
  2044. {
  2045. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2046. u16 reason_code;
  2047. sdata_assert_lock(sdata);
  2048. if (len < 24 + 2)
  2049. return;
  2050. if (!ifmgd->associated ||
  2051. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2052. return;
  2053. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  2054. sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
  2055. mgmt->sa, reason_code);
  2056. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  2057. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  2058. }
  2059. static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
  2060. u8 *supp_rates, unsigned int supp_rates_len,
  2061. u32 *rates, u32 *basic_rates,
  2062. bool *have_higher_than_11mbit,
  2063. int *min_rate, int *min_rate_index,
  2064. int shift, u32 rate_flags)
  2065. {
  2066. int i, j;
  2067. for (i = 0; i < supp_rates_len; i++) {
  2068. int rate = supp_rates[i] & 0x7f;
  2069. bool is_basic = !!(supp_rates[i] & 0x80);
  2070. if ((rate * 5 * (1 << shift)) > 110)
  2071. *have_higher_than_11mbit = true;
  2072. /*
  2073. * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
  2074. * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
  2075. *
  2076. * Note: Even through the membership selector and the basic
  2077. * rate flag share the same bit, they are not exactly
  2078. * the same.
  2079. */
  2080. if (!!(supp_rates[i] & 0x80) &&
  2081. (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  2082. continue;
  2083. for (j = 0; j < sband->n_bitrates; j++) {
  2084. struct ieee80211_rate *br;
  2085. int brate;
  2086. br = &sband->bitrates[j];
  2087. if ((rate_flags & br->flags) != rate_flags)
  2088. continue;
  2089. brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
  2090. if (brate == rate) {
  2091. *rates |= BIT(j);
  2092. if (is_basic)
  2093. *basic_rates |= BIT(j);
  2094. if ((rate * 5) < *min_rate) {
  2095. *min_rate = rate * 5;
  2096. *min_rate_index = j;
  2097. }
  2098. break;
  2099. }
  2100. }
  2101. }
  2102. }
  2103. static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
  2104. bool assoc)
  2105. {
  2106. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  2107. sdata_assert_lock(sdata);
  2108. if (!assoc) {
  2109. sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
  2110. memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
  2111. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  2112. sdata->u.mgd.flags = 0;
  2113. ieee80211_vif_release_channel(sdata);
  2114. }
  2115. kfree(assoc_data);
  2116. sdata->u.mgd.assoc_data = NULL;
  2117. }
  2118. static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
  2119. struct cfg80211_bss *cbss,
  2120. struct ieee80211_mgmt *mgmt, size_t len)
  2121. {
  2122. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2123. struct ieee80211_local *local = sdata->local;
  2124. struct ieee80211_supported_band *sband;
  2125. struct sta_info *sta;
  2126. u8 *pos;
  2127. u16 capab_info, aid;
  2128. struct ieee802_11_elems elems;
  2129. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  2130. const struct cfg80211_bss_ies *bss_ies = NULL;
  2131. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  2132. u32 changed = 0;
  2133. int err;
  2134. bool ret;
  2135. /* AssocResp and ReassocResp have identical structure */
  2136. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  2137. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  2138. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  2139. sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
  2140. aid);
  2141. aid &= ~(BIT(15) | BIT(14));
  2142. ifmgd->broken_ap = false;
  2143. if (aid == 0 || aid > IEEE80211_MAX_AID) {
  2144. sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
  2145. aid);
  2146. aid = 0;
  2147. ifmgd->broken_ap = true;
  2148. }
  2149. pos = mgmt->u.assoc_resp.variable;
  2150. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
  2151. if (!elems.supp_rates) {
  2152. sdata_info(sdata, "no SuppRates element in AssocResp\n");
  2153. return false;
  2154. }
  2155. ifmgd->aid = aid;
  2156. /*
  2157. * Some APs are erroneously not including some information in their
  2158. * (re)association response frames. Try to recover by using the data
  2159. * from the beacon or probe response. This seems to afflict mobile
  2160. * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
  2161. * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
  2162. */
  2163. if ((assoc_data->wmm && !elems.wmm_param) ||
  2164. (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  2165. (!elems.ht_cap_elem || !elems.ht_operation)) ||
  2166. (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  2167. (!elems.vht_cap_elem || !elems.vht_operation))) {
  2168. const struct cfg80211_bss_ies *ies;
  2169. struct ieee802_11_elems bss_elems;
  2170. rcu_read_lock();
  2171. ies = rcu_dereference(cbss->ies);
  2172. if (ies)
  2173. bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
  2174. GFP_ATOMIC);
  2175. rcu_read_unlock();
  2176. if (!bss_ies)
  2177. return false;
  2178. ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
  2179. false, &bss_elems);
  2180. if (assoc_data->wmm &&
  2181. !elems.wmm_param && bss_elems.wmm_param) {
  2182. elems.wmm_param = bss_elems.wmm_param;
  2183. sdata_info(sdata,
  2184. "AP bug: WMM param missing from AssocResp\n");
  2185. }
  2186. /*
  2187. * Also check if we requested HT/VHT, otherwise the AP doesn't
  2188. * have to include the IEs in the (re)association response.
  2189. */
  2190. if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
  2191. !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
  2192. elems.ht_cap_elem = bss_elems.ht_cap_elem;
  2193. sdata_info(sdata,
  2194. "AP bug: HT capability missing from AssocResp\n");
  2195. }
  2196. if (!elems.ht_operation && bss_elems.ht_operation &&
  2197. !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
  2198. elems.ht_operation = bss_elems.ht_operation;
  2199. sdata_info(sdata,
  2200. "AP bug: HT operation missing from AssocResp\n");
  2201. }
  2202. if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
  2203. !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
  2204. elems.vht_cap_elem = bss_elems.vht_cap_elem;
  2205. sdata_info(sdata,
  2206. "AP bug: VHT capa missing from AssocResp\n");
  2207. }
  2208. if (!elems.vht_operation && bss_elems.vht_operation &&
  2209. !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
  2210. elems.vht_operation = bss_elems.vht_operation;
  2211. sdata_info(sdata,
  2212. "AP bug: VHT operation missing from AssocResp\n");
  2213. }
  2214. }
  2215. /*
  2216. * We previously checked these in the beacon/probe response, so
  2217. * they should be present here. This is just a safety net.
  2218. */
  2219. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  2220. (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
  2221. sdata_info(sdata,
  2222. "HT AP is missing WMM params or HT capability/operation\n");
  2223. ret = false;
  2224. goto out;
  2225. }
  2226. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  2227. (!elems.vht_cap_elem || !elems.vht_operation)) {
  2228. sdata_info(sdata,
  2229. "VHT AP is missing VHT capability/operation\n");
  2230. ret = false;
  2231. goto out;
  2232. }
  2233. mutex_lock(&sdata->local->sta_mtx);
  2234. /*
  2235. * station info was already allocated and inserted before
  2236. * the association and should be available to us
  2237. */
  2238. sta = sta_info_get(sdata, cbss->bssid);
  2239. if (WARN_ON(!sta)) {
  2240. mutex_unlock(&sdata->local->sta_mtx);
  2241. ret = false;
  2242. goto out;
  2243. }
  2244. sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
  2245. /* Set up internal HT/VHT capabilities */
  2246. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
  2247. ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  2248. elems.ht_cap_elem, sta);
  2249. if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  2250. ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
  2251. elems.vht_cap_elem, sta);
  2252. /*
  2253. * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
  2254. * in their association response, so ignore that data for our own
  2255. * configuration. If it changed since the last beacon, we'll get the
  2256. * next beacon and update then.
  2257. */
  2258. /*
  2259. * If an operating mode notification IE is present, override the
  2260. * NSS calculation (that would be done in rate_control_rate_init())
  2261. * and use the # of streams from that element.
  2262. */
  2263. if (elems.opmode_notif &&
  2264. !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
  2265. u8 nss;
  2266. nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
  2267. nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
  2268. nss += 1;
  2269. sta->sta.rx_nss = nss;
  2270. }
  2271. rate_control_rate_init(sta);
  2272. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
  2273. set_sta_flag(sta, WLAN_STA_MFP);
  2274. if (elems.wmm_param)
  2275. set_sta_flag(sta, WLAN_STA_WME);
  2276. err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
  2277. if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
  2278. err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
  2279. if (err) {
  2280. sdata_info(sdata,
  2281. "failed to move station %pM to desired state\n",
  2282. sta->sta.addr);
  2283. WARN_ON(__sta_info_destroy(sta));
  2284. mutex_unlock(&sdata->local->sta_mtx);
  2285. ret = false;
  2286. goto out;
  2287. }
  2288. mutex_unlock(&sdata->local->sta_mtx);
  2289. /*
  2290. * Always handle WMM once after association regardless
  2291. * of the first value the AP uses. Setting -1 here has
  2292. * that effect because the AP values is an unsigned
  2293. * 4-bit value.
  2294. */
  2295. ifmgd->wmm_last_param_set = -1;
  2296. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) && elems.wmm_param)
  2297. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  2298. elems.wmm_param_len);
  2299. else
  2300. ieee80211_set_wmm_default(sdata, false);
  2301. changed |= BSS_CHANGED_QOS;
  2302. /* set AID and assoc capability,
  2303. * ieee80211_set_associated() will tell the driver */
  2304. bss_conf->aid = aid;
  2305. bss_conf->assoc_capability = capab_info;
  2306. ieee80211_set_associated(sdata, cbss, changed);
  2307. /*
  2308. * If we're using 4-addr mode, let the AP know that we're
  2309. * doing so, so that it can create the STA VLAN on its side
  2310. */
  2311. if (ifmgd->use_4addr)
  2312. ieee80211_send_4addr_nullfunc(local, sdata);
  2313. /*
  2314. * Start timer to probe the connection to the AP now.
  2315. * Also start the timer that will detect beacon loss.
  2316. */
  2317. ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
  2318. ieee80211_sta_reset_beacon_monitor(sdata);
  2319. ret = true;
  2320. out:
  2321. kfree(bss_ies);
  2322. return ret;
  2323. }
  2324. static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  2325. struct ieee80211_mgmt *mgmt,
  2326. size_t len)
  2327. {
  2328. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2329. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  2330. u16 capab_info, status_code, aid;
  2331. struct ieee802_11_elems elems;
  2332. u8 *pos;
  2333. bool reassoc;
  2334. struct cfg80211_bss *bss;
  2335. sdata_assert_lock(sdata);
  2336. if (!assoc_data)
  2337. return;
  2338. if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
  2339. return;
  2340. /*
  2341. * AssocResp and ReassocResp have identical structure, so process both
  2342. * of them in this function.
  2343. */
  2344. if (len < 24 + 6)
  2345. return;
  2346. reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
  2347. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  2348. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  2349. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  2350. sdata_info(sdata,
  2351. "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
  2352. reassoc ? "Rea" : "A", mgmt->sa,
  2353. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  2354. pos = mgmt->u.assoc_resp.variable;
  2355. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
  2356. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  2357. elems.timeout_int &&
  2358. elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  2359. u32 tu, ms;
  2360. tu = le32_to_cpu(elems.timeout_int->value);
  2361. ms = tu * 1024 / 1000;
  2362. sdata_info(sdata,
  2363. "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
  2364. mgmt->sa, tu, ms);
  2365. assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
  2366. assoc_data->timeout_started = true;
  2367. if (ms > IEEE80211_ASSOC_TIMEOUT)
  2368. run_again(sdata, assoc_data->timeout);
  2369. return;
  2370. }
  2371. bss = assoc_data->bss;
  2372. if (status_code != WLAN_STATUS_SUCCESS) {
  2373. sdata_info(sdata, "%pM denied association (code=%d)\n",
  2374. mgmt->sa, status_code);
  2375. ieee80211_destroy_assoc_data(sdata, false);
  2376. } else {
  2377. if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) {
  2378. /* oops -- internal error -- send timeout for now */
  2379. ieee80211_destroy_assoc_data(sdata, false);
  2380. cfg80211_assoc_timeout(sdata->dev, bss);
  2381. return;
  2382. }
  2383. sdata_info(sdata, "associated\n");
  2384. /*
  2385. * destroy assoc_data afterwards, as otherwise an idle
  2386. * recalc after assoc_data is NULL but before associated
  2387. * is set can cause the interface to go idle
  2388. */
  2389. ieee80211_destroy_assoc_data(sdata, true);
  2390. }
  2391. cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len);
  2392. }
  2393. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  2394. struct ieee80211_mgmt *mgmt, size_t len,
  2395. struct ieee80211_rx_status *rx_status,
  2396. struct ieee802_11_elems *elems)
  2397. {
  2398. struct ieee80211_local *local = sdata->local;
  2399. int freq;
  2400. struct ieee80211_bss *bss;
  2401. struct ieee80211_channel *channel;
  2402. sdata_assert_lock(sdata);
  2403. if (elems->ds_params)
  2404. freq = ieee80211_channel_to_frequency(elems->ds_params[0],
  2405. rx_status->band);
  2406. else
  2407. freq = rx_status->freq;
  2408. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  2409. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  2410. return;
  2411. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  2412. channel);
  2413. if (bss) {
  2414. ieee80211_rx_bss_put(local, bss);
  2415. sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
  2416. }
  2417. }
  2418. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  2419. struct sk_buff *skb)
  2420. {
  2421. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  2422. struct ieee80211_if_managed *ifmgd;
  2423. struct ieee80211_rx_status *rx_status = (void *) skb->cb;
  2424. size_t baselen, len = skb->len;
  2425. struct ieee802_11_elems elems;
  2426. ifmgd = &sdata->u.mgd;
  2427. sdata_assert_lock(sdata);
  2428. if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
  2429. return; /* ignore ProbeResp to foreign address */
  2430. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  2431. if (baselen > len)
  2432. return;
  2433. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  2434. false, &elems);
  2435. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2436. if (ifmgd->associated &&
  2437. ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2438. ieee80211_reset_ap_probe(sdata);
  2439. if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
  2440. ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
  2441. /* got probe response, continue with auth */
  2442. sdata_info(sdata, "direct probe responded\n");
  2443. ifmgd->auth_data->tries = 0;
  2444. ifmgd->auth_data->timeout = jiffies;
  2445. ifmgd->auth_data->timeout_started = true;
  2446. run_again(sdata, ifmgd->auth_data->timeout);
  2447. }
  2448. }
  2449. /*
  2450. * This is the canonical list of information elements we care about,
  2451. * the filter code also gives us all changes to the Microsoft OUI
  2452. * (00:50:F2) vendor IE which is used for WMM which we need to track.
  2453. *
  2454. * We implement beacon filtering in software since that means we can
  2455. * avoid processing the frame here and in cfg80211, and userspace
  2456. * will not be able to tell whether the hardware supports it or not.
  2457. *
  2458. * XXX: This list needs to be dynamic -- userspace needs to be able to
  2459. * add items it requires. It also needs to be able to tell us to
  2460. * look out for other vendor IEs.
  2461. */
  2462. static const u64 care_about_ies =
  2463. (1ULL << WLAN_EID_COUNTRY) |
  2464. (1ULL << WLAN_EID_ERP_INFO) |
  2465. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  2466. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  2467. (1ULL << WLAN_EID_HT_CAPABILITY) |
  2468. (1ULL << WLAN_EID_HT_OPERATION);
  2469. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  2470. struct ieee80211_mgmt *mgmt, size_t len,
  2471. struct ieee80211_rx_status *rx_status)
  2472. {
  2473. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2474. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  2475. size_t baselen;
  2476. struct ieee802_11_elems elems;
  2477. struct ieee80211_local *local = sdata->local;
  2478. struct ieee80211_chanctx_conf *chanctx_conf;
  2479. struct ieee80211_channel *chan;
  2480. struct sta_info *sta;
  2481. u32 changed = 0;
  2482. bool erp_valid;
  2483. u8 erp_value = 0;
  2484. u32 ncrc;
  2485. u8 *bssid;
  2486. u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
  2487. sdata_assert_lock(sdata);
  2488. /* Process beacon from the current BSS */
  2489. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  2490. if (baselen > len)
  2491. return;
  2492. rcu_read_lock();
  2493. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  2494. if (!chanctx_conf) {
  2495. rcu_read_unlock();
  2496. return;
  2497. }
  2498. if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
  2499. rcu_read_unlock();
  2500. return;
  2501. }
  2502. chan = chanctx_conf->def.chan;
  2503. rcu_read_unlock();
  2504. if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
  2505. ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
  2506. ieee802_11_parse_elems(mgmt->u.beacon.variable,
  2507. len - baselen, false, &elems);
  2508. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2509. if (elems.tim && !elems.parse_error) {
  2510. const struct ieee80211_tim_ie *tim_ie = elems.tim;
  2511. ifmgd->dtim_period = tim_ie->dtim_period;
  2512. }
  2513. ifmgd->have_beacon = true;
  2514. ifmgd->assoc_data->need_beacon = false;
  2515. if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
  2516. sdata->vif.bss_conf.sync_tsf =
  2517. le64_to_cpu(mgmt->u.beacon.timestamp);
  2518. sdata->vif.bss_conf.sync_device_ts =
  2519. rx_status->device_timestamp;
  2520. if (elems.tim)
  2521. sdata->vif.bss_conf.sync_dtim_count =
  2522. elems.tim->dtim_count;
  2523. else
  2524. sdata->vif.bss_conf.sync_dtim_count = 0;
  2525. }
  2526. /* continue assoc process */
  2527. ifmgd->assoc_data->timeout = jiffies;
  2528. ifmgd->assoc_data->timeout_started = true;
  2529. run_again(sdata, ifmgd->assoc_data->timeout);
  2530. return;
  2531. }
  2532. if (!ifmgd->associated ||
  2533. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2534. return;
  2535. bssid = ifmgd->associated->bssid;
  2536. /* Track average RSSI from the Beacon frames of the current AP */
  2537. ifmgd->last_beacon_signal = rx_status->signal;
  2538. if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
  2539. ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
  2540. ifmgd->ave_beacon_signal = rx_status->signal * 16;
  2541. ifmgd->last_cqm_event_signal = 0;
  2542. ifmgd->count_beacon_signal = 1;
  2543. ifmgd->last_ave_beacon_signal = 0;
  2544. } else {
  2545. ifmgd->ave_beacon_signal =
  2546. (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
  2547. (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
  2548. ifmgd->ave_beacon_signal) / 16;
  2549. ifmgd->count_beacon_signal++;
  2550. }
  2551. if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
  2552. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
  2553. int sig = ifmgd->ave_beacon_signal;
  2554. int last_sig = ifmgd->last_ave_beacon_signal;
  2555. /*
  2556. * if signal crosses either of the boundaries, invoke callback
  2557. * with appropriate parameters
  2558. */
  2559. if (sig > ifmgd->rssi_max_thold &&
  2560. (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
  2561. ifmgd->last_ave_beacon_signal = sig;
  2562. drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
  2563. } else if (sig < ifmgd->rssi_min_thold &&
  2564. (last_sig >= ifmgd->rssi_max_thold ||
  2565. last_sig == 0)) {
  2566. ifmgd->last_ave_beacon_signal = sig;
  2567. drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
  2568. }
  2569. }
  2570. if (bss_conf->cqm_rssi_thold &&
  2571. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
  2572. !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
  2573. int sig = ifmgd->ave_beacon_signal / 16;
  2574. int last_event = ifmgd->last_cqm_event_signal;
  2575. int thold = bss_conf->cqm_rssi_thold;
  2576. int hyst = bss_conf->cqm_rssi_hyst;
  2577. if (sig < thold &&
  2578. (last_event == 0 || sig < last_event - hyst)) {
  2579. ifmgd->last_cqm_event_signal = sig;
  2580. ieee80211_cqm_rssi_notify(
  2581. &sdata->vif,
  2582. NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
  2583. GFP_KERNEL);
  2584. } else if (sig > thold &&
  2585. (last_event == 0 || sig > last_event + hyst)) {
  2586. ifmgd->last_cqm_event_signal = sig;
  2587. ieee80211_cqm_rssi_notify(
  2588. &sdata->vif,
  2589. NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
  2590. GFP_KERNEL);
  2591. }
  2592. }
  2593. if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
  2594. mlme_dbg_ratelimited(sdata,
  2595. "cancelling AP probe due to a received beacon\n");
  2596. mutex_lock(&local->mtx);
  2597. ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
  2598. ieee80211_run_deferred_scan(local);
  2599. mutex_unlock(&local->mtx);
  2600. mutex_lock(&local->iflist_mtx);
  2601. ieee80211_recalc_ps(local, -1);
  2602. mutex_unlock(&local->iflist_mtx);
  2603. }
  2604. /*
  2605. * Push the beacon loss detection into the future since
  2606. * we are processing a beacon from the AP just now.
  2607. */
  2608. ieee80211_sta_reset_beacon_monitor(sdata);
  2609. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  2610. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  2611. len - baselen, false, &elems,
  2612. care_about_ies, ncrc);
  2613. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
  2614. bool directed_tim = ieee80211_check_tim(elems.tim,
  2615. elems.tim_len,
  2616. ifmgd->aid);
  2617. if (directed_tim) {
  2618. if (local->hw.conf.dynamic_ps_timeout > 0) {
  2619. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  2620. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  2621. ieee80211_hw_config(local,
  2622. IEEE80211_CONF_CHANGE_PS);
  2623. }
  2624. ieee80211_send_nullfunc(local, sdata, 0);
  2625. } else if (!local->pspolling && sdata->u.mgd.powersave) {
  2626. local->pspolling = true;
  2627. /*
  2628. * Here is assumed that the driver will be
  2629. * able to send ps-poll frame and receive a
  2630. * response even though power save mode is
  2631. * enabled, but some drivers might require
  2632. * to disable power save here. This needs
  2633. * to be investigated.
  2634. */
  2635. ieee80211_send_pspoll(local, sdata);
  2636. }
  2637. }
  2638. }
  2639. if (sdata->vif.p2p) {
  2640. struct ieee80211_p2p_noa_attr noa = {};
  2641. int ret;
  2642. ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
  2643. len - baselen,
  2644. IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
  2645. (u8 *) &noa, sizeof(noa));
  2646. if (ret >= 2) {
  2647. if (sdata->u.mgd.p2p_noa_index != noa.index) {
  2648. /* valid noa_attr and index changed */
  2649. sdata->u.mgd.p2p_noa_index = noa.index;
  2650. memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
  2651. changed |= BSS_CHANGED_P2P_PS;
  2652. /*
  2653. * make sure we update all information, the CRC
  2654. * mechanism doesn't look at P2P attributes.
  2655. */
  2656. ifmgd->beacon_crc_valid = false;
  2657. }
  2658. } else if (sdata->u.mgd.p2p_noa_index != -1) {
  2659. /* noa_attr not found and we had valid noa_attr before */
  2660. sdata->u.mgd.p2p_noa_index = -1;
  2661. memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
  2662. changed |= BSS_CHANGED_P2P_PS;
  2663. ifmgd->beacon_crc_valid = false;
  2664. }
  2665. }
  2666. if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
  2667. return;
  2668. ifmgd->beacon_crc = ncrc;
  2669. ifmgd->beacon_crc_valid = true;
  2670. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2671. ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
  2672. &elems, true);
  2673. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
  2674. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  2675. elems.wmm_param_len))
  2676. changed |= BSS_CHANGED_QOS;
  2677. /*
  2678. * If we haven't had a beacon before, tell the driver about the
  2679. * DTIM period (and beacon timing if desired) now.
  2680. */
  2681. if (!ifmgd->have_beacon) {
  2682. /* a few bogus AP send dtim_period = 0 or no TIM IE */
  2683. if (elems.tim)
  2684. bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
  2685. else
  2686. bss_conf->dtim_period = 1;
  2687. if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
  2688. sdata->vif.bss_conf.sync_tsf =
  2689. le64_to_cpu(mgmt->u.beacon.timestamp);
  2690. sdata->vif.bss_conf.sync_device_ts =
  2691. rx_status->device_timestamp;
  2692. if (elems.tim)
  2693. sdata->vif.bss_conf.sync_dtim_count =
  2694. elems.tim->dtim_count;
  2695. else
  2696. sdata->vif.bss_conf.sync_dtim_count = 0;
  2697. }
  2698. changed |= BSS_CHANGED_BEACON_INFO;
  2699. ifmgd->have_beacon = true;
  2700. mutex_lock(&local->iflist_mtx);
  2701. ieee80211_recalc_ps(local, -1);
  2702. mutex_unlock(&local->iflist_mtx);
  2703. ieee80211_recalc_ps_vif(sdata);
  2704. }
  2705. if (elems.erp_info) {
  2706. erp_valid = true;
  2707. erp_value = elems.erp_info[0];
  2708. } else {
  2709. erp_valid = false;
  2710. }
  2711. changed |= ieee80211_handle_bss_capability(sdata,
  2712. le16_to_cpu(mgmt->u.beacon.capab_info),
  2713. erp_valid, erp_value);
  2714. mutex_lock(&local->sta_mtx);
  2715. sta = sta_info_get(sdata, bssid);
  2716. if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
  2717. elems.vht_operation, bssid, &changed)) {
  2718. mutex_unlock(&local->sta_mtx);
  2719. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  2720. WLAN_REASON_DEAUTH_LEAVING,
  2721. true, deauth_buf);
  2722. cfg80211_tx_mlme_mgmt(sdata->dev, deauth_buf,
  2723. sizeof(deauth_buf));
  2724. return;
  2725. }
  2726. if (sta && elems.opmode_notif)
  2727. ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
  2728. rx_status->band, true);
  2729. mutex_unlock(&local->sta_mtx);
  2730. if (elems.country_elem && elems.pwr_constr_elem &&
  2731. mgmt->u.probe_resp.capab_info &
  2732. cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
  2733. changed |= ieee80211_handle_pwr_constr(sdata, chan,
  2734. elems.country_elem,
  2735. elems.country_elem_len,
  2736. elems.pwr_constr_elem);
  2737. ieee80211_bss_info_change_notify(sdata, changed);
  2738. }
  2739. void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  2740. struct sk_buff *skb)
  2741. {
  2742. struct ieee80211_rx_status *rx_status;
  2743. struct ieee80211_mgmt *mgmt;
  2744. u16 fc;
  2745. struct ieee802_11_elems elems;
  2746. int ies_len;
  2747. rx_status = (struct ieee80211_rx_status *) skb->cb;
  2748. mgmt = (struct ieee80211_mgmt *) skb->data;
  2749. fc = le16_to_cpu(mgmt->frame_control);
  2750. sdata_lock(sdata);
  2751. switch (fc & IEEE80211_FCTL_STYPE) {
  2752. case IEEE80211_STYPE_BEACON:
  2753. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
  2754. break;
  2755. case IEEE80211_STYPE_PROBE_RESP:
  2756. ieee80211_rx_mgmt_probe_resp(sdata, skb);
  2757. break;
  2758. case IEEE80211_STYPE_AUTH:
  2759. ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
  2760. break;
  2761. case IEEE80211_STYPE_DEAUTH:
  2762. ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  2763. break;
  2764. case IEEE80211_STYPE_DISASSOC:
  2765. ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  2766. break;
  2767. case IEEE80211_STYPE_ASSOC_RESP:
  2768. case IEEE80211_STYPE_REASSOC_RESP:
  2769. ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
  2770. break;
  2771. case IEEE80211_STYPE_ACTION:
  2772. if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
  2773. ies_len = skb->len -
  2774. offsetof(struct ieee80211_mgmt,
  2775. u.action.u.chan_switch.variable);
  2776. if (ies_len < 0)
  2777. break;
  2778. ieee802_11_parse_elems(
  2779. mgmt->u.action.u.chan_switch.variable,
  2780. ies_len, true, &elems);
  2781. if (elems.parse_error)
  2782. break;
  2783. ieee80211_sta_process_chanswitch(sdata,
  2784. rx_status->mactime,
  2785. &elems, false);
  2786. } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
  2787. ies_len = skb->len -
  2788. offsetof(struct ieee80211_mgmt,
  2789. u.action.u.ext_chan_switch.variable);
  2790. if (ies_len < 0)
  2791. break;
  2792. ieee802_11_parse_elems(
  2793. mgmt->u.action.u.ext_chan_switch.variable,
  2794. ies_len, true, &elems);
  2795. if (elems.parse_error)
  2796. break;
  2797. /* for the handling code pretend this was also an IE */
  2798. elems.ext_chansw_ie =
  2799. &mgmt->u.action.u.ext_chan_switch.data;
  2800. ieee80211_sta_process_chanswitch(sdata,
  2801. rx_status->mactime,
  2802. &elems, false);
  2803. }
  2804. break;
  2805. }
  2806. sdata_unlock(sdata);
  2807. }
  2808. static void ieee80211_sta_timer(unsigned long data)
  2809. {
  2810. struct ieee80211_sub_if_data *sdata =
  2811. (struct ieee80211_sub_if_data *) data;
  2812. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  2813. }
  2814. static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
  2815. u8 *bssid, u8 reason, bool tx)
  2816. {
  2817. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  2818. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
  2819. tx, frame_buf);
  2820. cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
  2821. IEEE80211_DEAUTH_FRAME_LEN);
  2822. }
  2823. static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
  2824. {
  2825. struct ieee80211_local *local = sdata->local;
  2826. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2827. struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
  2828. u32 tx_flags = 0;
  2829. sdata_assert_lock(sdata);
  2830. if (WARN_ON_ONCE(!auth_data))
  2831. return -EINVAL;
  2832. auth_data->tries++;
  2833. if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
  2834. sdata_info(sdata, "authentication with %pM timed out\n",
  2835. auth_data->bss->bssid);
  2836. /*
  2837. * Most likely AP is not in the range so remove the
  2838. * bss struct for that AP.
  2839. */
  2840. cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
  2841. return -ETIMEDOUT;
  2842. }
  2843. drv_mgd_prepare_tx(local, sdata);
  2844. if (auth_data->bss->proberesp_ies) {
  2845. u16 trans = 1;
  2846. u16 status = 0;
  2847. sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
  2848. auth_data->bss->bssid, auth_data->tries,
  2849. IEEE80211_AUTH_MAX_TRIES);
  2850. auth_data->expected_transaction = 2;
  2851. if (auth_data->algorithm == WLAN_AUTH_SAE) {
  2852. trans = auth_data->sae_trans;
  2853. status = auth_data->sae_status;
  2854. auth_data->expected_transaction = trans;
  2855. }
  2856. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  2857. tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
  2858. IEEE80211_TX_INTFL_MLME_CONN_TX;
  2859. ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
  2860. auth_data->data, auth_data->data_len,
  2861. auth_data->bss->bssid,
  2862. auth_data->bss->bssid, NULL, 0, 0,
  2863. tx_flags);
  2864. } else {
  2865. const u8 *ssidie;
  2866. sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
  2867. auth_data->bss->bssid, auth_data->tries,
  2868. IEEE80211_AUTH_MAX_TRIES);
  2869. rcu_read_lock();
  2870. ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
  2871. if (!ssidie) {
  2872. rcu_read_unlock();
  2873. return -EINVAL;
  2874. }
  2875. /*
  2876. * Direct probe is sent to broadcast address as some APs
  2877. * will not answer to direct packet in unassociated state.
  2878. */
  2879. ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
  2880. NULL, 0, (u32) -1, true, 0,
  2881. auth_data->bss->channel, false);
  2882. rcu_read_unlock();
  2883. }
  2884. if (tx_flags == 0) {
  2885. auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  2886. auth_data->timeout_started = true;
  2887. run_again(sdata, auth_data->timeout);
  2888. } else {
  2889. auth_data->timeout =
  2890. round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
  2891. auth_data->timeout_started = true;
  2892. run_again(sdata, auth_data->timeout);
  2893. }
  2894. return 0;
  2895. }
  2896. static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
  2897. {
  2898. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  2899. struct ieee80211_local *local = sdata->local;
  2900. sdata_assert_lock(sdata);
  2901. assoc_data->tries++;
  2902. if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
  2903. sdata_info(sdata, "association with %pM timed out\n",
  2904. assoc_data->bss->bssid);
  2905. /*
  2906. * Most likely AP is not in the range so remove the
  2907. * bss struct for that AP.
  2908. */
  2909. cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
  2910. return -ETIMEDOUT;
  2911. }
  2912. sdata_info(sdata, "associate with %pM (try %d/%d)\n",
  2913. assoc_data->bss->bssid, assoc_data->tries,
  2914. IEEE80211_ASSOC_MAX_TRIES);
  2915. ieee80211_send_assoc(sdata);
  2916. if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
  2917. assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
  2918. assoc_data->timeout_started = true;
  2919. run_again(sdata, assoc_data->timeout);
  2920. } else {
  2921. assoc_data->timeout =
  2922. round_jiffies_up(jiffies +
  2923. IEEE80211_ASSOC_TIMEOUT_LONG);
  2924. assoc_data->timeout_started = true;
  2925. run_again(sdata, assoc_data->timeout);
  2926. }
  2927. return 0;
  2928. }
  2929. void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
  2930. __le16 fc, bool acked)
  2931. {
  2932. struct ieee80211_local *local = sdata->local;
  2933. sdata->u.mgd.status_fc = fc;
  2934. sdata->u.mgd.status_acked = acked;
  2935. sdata->u.mgd.status_received = true;
  2936. ieee80211_queue_work(&local->hw, &sdata->work);
  2937. }
  2938. void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
  2939. {
  2940. struct ieee80211_local *local = sdata->local;
  2941. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2942. sdata_lock(sdata);
  2943. if (ifmgd->status_received) {
  2944. __le16 fc = ifmgd->status_fc;
  2945. bool status_acked = ifmgd->status_acked;
  2946. ifmgd->status_received = false;
  2947. if (ifmgd->auth_data &&
  2948. (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
  2949. if (status_acked) {
  2950. ifmgd->auth_data->timeout =
  2951. jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
  2952. run_again(sdata, ifmgd->auth_data->timeout);
  2953. } else {
  2954. ifmgd->auth_data->timeout = jiffies - 1;
  2955. }
  2956. ifmgd->auth_data->timeout_started = true;
  2957. } else if (ifmgd->assoc_data &&
  2958. (ieee80211_is_assoc_req(fc) ||
  2959. ieee80211_is_reassoc_req(fc))) {
  2960. if (status_acked) {
  2961. ifmgd->assoc_data->timeout =
  2962. jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
  2963. run_again(sdata, ifmgd->assoc_data->timeout);
  2964. } else {
  2965. ifmgd->assoc_data->timeout = jiffies - 1;
  2966. }
  2967. ifmgd->assoc_data->timeout_started = true;
  2968. }
  2969. }
  2970. if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
  2971. time_after(jiffies, ifmgd->auth_data->timeout)) {
  2972. if (ifmgd->auth_data->done) {
  2973. /*
  2974. * ok ... we waited for assoc but userspace didn't,
  2975. * so let's just kill the auth data
  2976. */
  2977. ieee80211_destroy_auth_data(sdata, false);
  2978. } else if (ieee80211_probe_auth(sdata)) {
  2979. u8 bssid[ETH_ALEN];
  2980. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  2981. ieee80211_destroy_auth_data(sdata, false);
  2982. cfg80211_auth_timeout(sdata->dev, bssid);
  2983. }
  2984. } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
  2985. run_again(sdata, ifmgd->auth_data->timeout);
  2986. if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
  2987. time_after(jiffies, ifmgd->assoc_data->timeout)) {
  2988. if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
  2989. ieee80211_do_assoc(sdata)) {
  2990. struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
  2991. ieee80211_destroy_assoc_data(sdata, false);
  2992. cfg80211_assoc_timeout(sdata->dev, bss);
  2993. }
  2994. } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
  2995. run_again(sdata, ifmgd->assoc_data->timeout);
  2996. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  2997. IEEE80211_STA_CONNECTION_POLL) &&
  2998. ifmgd->associated) {
  2999. u8 bssid[ETH_ALEN];
  3000. int max_tries;
  3001. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  3002. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  3003. max_tries = max_nullfunc_tries;
  3004. else
  3005. max_tries = max_probe_tries;
  3006. /* ACK received for nullfunc probing frame */
  3007. if (!ifmgd->probe_send_count)
  3008. ieee80211_reset_ap_probe(sdata);
  3009. else if (ifmgd->nullfunc_failed) {
  3010. if (ifmgd->probe_send_count < max_tries) {
  3011. mlme_dbg(sdata,
  3012. "No ack for nullfunc frame to AP %pM, try %d/%i\n",
  3013. bssid, ifmgd->probe_send_count,
  3014. max_tries);
  3015. ieee80211_mgd_probe_ap_send(sdata);
  3016. } else {
  3017. mlme_dbg(sdata,
  3018. "No ack for nullfunc frame to AP %pM, disconnecting.\n",
  3019. bssid);
  3020. ieee80211_sta_connection_lost(sdata, bssid,
  3021. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  3022. false);
  3023. }
  3024. } else if (time_is_after_jiffies(ifmgd->probe_timeout))
  3025. run_again(sdata, ifmgd->probe_timeout);
  3026. else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  3027. mlme_dbg(sdata,
  3028. "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
  3029. bssid, probe_wait_ms);
  3030. ieee80211_sta_connection_lost(sdata, bssid,
  3031. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
  3032. } else if (ifmgd->probe_send_count < max_tries) {
  3033. mlme_dbg(sdata,
  3034. "No probe response from AP %pM after %dms, try %d/%i\n",
  3035. bssid, probe_wait_ms,
  3036. ifmgd->probe_send_count, max_tries);
  3037. ieee80211_mgd_probe_ap_send(sdata);
  3038. } else {
  3039. /*
  3040. * We actually lost the connection ... or did we?
  3041. * Let's make sure!
  3042. */
  3043. wiphy_debug(local->hw.wiphy,
  3044. "%s: No probe response from AP %pM"
  3045. " after %dms, disconnecting.\n",
  3046. sdata->name,
  3047. bssid, probe_wait_ms);
  3048. ieee80211_sta_connection_lost(sdata, bssid,
  3049. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
  3050. }
  3051. }
  3052. sdata_unlock(sdata);
  3053. }
  3054. static void ieee80211_sta_bcn_mon_timer(unsigned long data)
  3055. {
  3056. struct ieee80211_sub_if_data *sdata =
  3057. (struct ieee80211_sub_if_data *) data;
  3058. struct ieee80211_local *local = sdata->local;
  3059. if (local->quiescing)
  3060. return;
  3061. sdata->u.mgd.connection_loss = false;
  3062. ieee80211_queue_work(&sdata->local->hw,
  3063. &sdata->u.mgd.beacon_connection_loss_work);
  3064. }
  3065. static void ieee80211_sta_conn_mon_timer(unsigned long data)
  3066. {
  3067. struct ieee80211_sub_if_data *sdata =
  3068. (struct ieee80211_sub_if_data *) data;
  3069. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3070. struct ieee80211_local *local = sdata->local;
  3071. if (local->quiescing)
  3072. return;
  3073. ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
  3074. }
  3075. static void ieee80211_sta_monitor_work(struct work_struct *work)
  3076. {
  3077. struct ieee80211_sub_if_data *sdata =
  3078. container_of(work, struct ieee80211_sub_if_data,
  3079. u.mgd.monitor_work);
  3080. ieee80211_mgd_probe_ap(sdata, false);
  3081. }
  3082. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  3083. {
  3084. u32 flags;
  3085. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  3086. __ieee80211_stop_poll(sdata);
  3087. /* let's probe the connection once */
  3088. flags = sdata->local->hw.flags;
  3089. if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
  3090. ieee80211_queue_work(&sdata->local->hw,
  3091. &sdata->u.mgd.monitor_work);
  3092. /* and do all the other regular work too */
  3093. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  3094. }
  3095. }
  3096. #ifdef CONFIG_PM
  3097. void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
  3098. {
  3099. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3100. sdata_lock(sdata);
  3101. if (!ifmgd->associated) {
  3102. sdata_unlock(sdata);
  3103. return;
  3104. }
  3105. if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
  3106. sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
  3107. mlme_dbg(sdata, "driver requested disconnect after resume\n");
  3108. ieee80211_sta_connection_lost(sdata,
  3109. ifmgd->associated->bssid,
  3110. WLAN_REASON_UNSPECIFIED,
  3111. true);
  3112. sdata_unlock(sdata);
  3113. return;
  3114. }
  3115. sdata_unlock(sdata);
  3116. }
  3117. #endif
  3118. /* interface setup */
  3119. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  3120. {
  3121. struct ieee80211_if_managed *ifmgd;
  3122. ifmgd = &sdata->u.mgd;
  3123. INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
  3124. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  3125. INIT_WORK(&ifmgd->beacon_connection_loss_work,
  3126. ieee80211_beacon_connection_loss_work);
  3127. INIT_WORK(&ifmgd->csa_connection_drop_work,
  3128. ieee80211_csa_connection_drop_work);
  3129. INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
  3130. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  3131. (unsigned long) sdata);
  3132. setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
  3133. (unsigned long) sdata);
  3134. setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
  3135. (unsigned long) sdata);
  3136. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  3137. (unsigned long) sdata);
  3138. ifmgd->flags = 0;
  3139. ifmgd->powersave = sdata->wdev.ps;
  3140. ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
  3141. ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
  3142. ifmgd->p2p_noa_index = -1;
  3143. if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
  3144. ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
  3145. else
  3146. ifmgd->req_smps = IEEE80211_SMPS_OFF;
  3147. }
  3148. /* scan finished notification */
  3149. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  3150. {
  3151. struct ieee80211_sub_if_data *sdata;
  3152. /* Restart STA timers */
  3153. rcu_read_lock();
  3154. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  3155. if (ieee80211_sdata_running(sdata))
  3156. ieee80211_restart_sta_timer(sdata);
  3157. }
  3158. rcu_read_unlock();
  3159. }
  3160. int ieee80211_max_network_latency(struct notifier_block *nb,
  3161. unsigned long data, void *dummy)
  3162. {
  3163. s32 latency_usec = (s32) data;
  3164. struct ieee80211_local *local =
  3165. container_of(nb, struct ieee80211_local,
  3166. network_latency_notifier);
  3167. mutex_lock(&local->iflist_mtx);
  3168. ieee80211_recalc_ps(local, latency_usec);
  3169. mutex_unlock(&local->iflist_mtx);
  3170. return 0;
  3171. }
  3172. static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
  3173. struct cfg80211_bss *cbss)
  3174. {
  3175. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3176. const u8 *ht_cap_ie, *vht_cap_ie;
  3177. const struct ieee80211_ht_cap *ht_cap;
  3178. const struct ieee80211_vht_cap *vht_cap;
  3179. u8 chains = 1;
  3180. if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
  3181. return chains;
  3182. ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
  3183. if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
  3184. ht_cap = (void *)(ht_cap_ie + 2);
  3185. chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
  3186. /*
  3187. * TODO: use "Tx Maximum Number Spatial Streams Supported" and
  3188. * "Tx Unequal Modulation Supported" fields.
  3189. */
  3190. }
  3191. if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
  3192. return chains;
  3193. vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
  3194. if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
  3195. u8 nss;
  3196. u16 tx_mcs_map;
  3197. vht_cap = (void *)(vht_cap_ie + 2);
  3198. tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
  3199. for (nss = 8; nss > 0; nss--) {
  3200. if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
  3201. IEEE80211_VHT_MCS_NOT_SUPPORTED)
  3202. break;
  3203. }
  3204. /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
  3205. chains = max(chains, nss);
  3206. }
  3207. return chains;
  3208. }
  3209. static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
  3210. struct cfg80211_bss *cbss)
  3211. {
  3212. struct ieee80211_local *local = sdata->local;
  3213. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3214. const struct ieee80211_ht_operation *ht_oper = NULL;
  3215. const struct ieee80211_vht_operation *vht_oper = NULL;
  3216. struct ieee80211_supported_band *sband;
  3217. struct cfg80211_chan_def chandef;
  3218. int ret;
  3219. sband = local->hw.wiphy->bands[cbss->channel->band];
  3220. ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
  3221. IEEE80211_STA_DISABLE_80P80MHZ |
  3222. IEEE80211_STA_DISABLE_160MHZ);
  3223. rcu_read_lock();
  3224. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  3225. sband->ht_cap.ht_supported) {
  3226. const u8 *ht_oper_ie, *ht_cap;
  3227. ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
  3228. if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
  3229. ht_oper = (void *)(ht_oper_ie + 2);
  3230. ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
  3231. if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
  3232. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3233. ht_oper = NULL;
  3234. }
  3235. }
  3236. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  3237. sband->vht_cap.vht_supported) {
  3238. const u8 *vht_oper_ie, *vht_cap;
  3239. vht_oper_ie = ieee80211_bss_get_ie(cbss,
  3240. WLAN_EID_VHT_OPERATION);
  3241. if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
  3242. vht_oper = (void *)(vht_oper_ie + 2);
  3243. if (vht_oper && !ht_oper) {
  3244. vht_oper = NULL;
  3245. sdata_info(sdata,
  3246. "AP advertised VHT without HT, disabling both\n");
  3247. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3248. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3249. }
  3250. vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
  3251. if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
  3252. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3253. vht_oper = NULL;
  3254. }
  3255. }
  3256. ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
  3257. cbss->channel,
  3258. ht_oper, vht_oper,
  3259. &chandef, false);
  3260. sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
  3261. local->rx_chains);
  3262. rcu_read_unlock();
  3263. /* will change later if needed */
  3264. sdata->smps_mode = IEEE80211_SMPS_OFF;
  3265. /*
  3266. * If this fails (possibly due to channel context sharing
  3267. * on incompatible channels, e.g. 80+80 and 160 sharing the
  3268. * same control channel) try to use a smaller bandwidth.
  3269. */
  3270. ret = ieee80211_vif_use_channel(sdata, &chandef,
  3271. IEEE80211_CHANCTX_SHARED);
  3272. /* don't downgrade for 5 and 10 MHz channels, though. */
  3273. if (chandef.width == NL80211_CHAN_WIDTH_5 ||
  3274. chandef.width == NL80211_CHAN_WIDTH_10)
  3275. return ret;
  3276. while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
  3277. ifmgd->flags |= chandef_downgrade(&chandef);
  3278. ret = ieee80211_vif_use_channel(sdata, &chandef,
  3279. IEEE80211_CHANCTX_SHARED);
  3280. }
  3281. return ret;
  3282. }
  3283. static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
  3284. struct cfg80211_bss *cbss, bool assoc)
  3285. {
  3286. struct ieee80211_local *local = sdata->local;
  3287. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3288. struct ieee80211_bss *bss = (void *)cbss->priv;
  3289. struct sta_info *new_sta = NULL;
  3290. bool have_sta = false;
  3291. int err;
  3292. if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
  3293. return -EINVAL;
  3294. if (assoc) {
  3295. rcu_read_lock();
  3296. have_sta = sta_info_get(sdata, cbss->bssid);
  3297. rcu_read_unlock();
  3298. }
  3299. if (!have_sta) {
  3300. new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
  3301. if (!new_sta)
  3302. return -ENOMEM;
  3303. }
  3304. if (new_sta) {
  3305. u32 rates = 0, basic_rates = 0;
  3306. bool have_higher_than_11mbit;
  3307. int min_rate = INT_MAX, min_rate_index = -1;
  3308. struct ieee80211_chanctx_conf *chanctx_conf;
  3309. struct ieee80211_supported_band *sband;
  3310. const struct cfg80211_bss_ies *ies;
  3311. int shift;
  3312. u32 rate_flags;
  3313. sband = local->hw.wiphy->bands[cbss->channel->band];
  3314. err = ieee80211_prep_channel(sdata, cbss);
  3315. if (err) {
  3316. sta_info_free(local, new_sta);
  3317. return -EINVAL;
  3318. }
  3319. shift = ieee80211_vif_get_shift(&sdata->vif);
  3320. rcu_read_lock();
  3321. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  3322. if (WARN_ON(!chanctx_conf)) {
  3323. rcu_read_unlock();
  3324. return -EINVAL;
  3325. }
  3326. rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
  3327. rcu_read_unlock();
  3328. ieee80211_get_rates(sband, bss->supp_rates,
  3329. bss->supp_rates_len,
  3330. &rates, &basic_rates,
  3331. &have_higher_than_11mbit,
  3332. &min_rate, &min_rate_index,
  3333. shift, rate_flags);
  3334. /*
  3335. * This used to be a workaround for basic rates missing
  3336. * in the association response frame. Now that we no
  3337. * longer use the basic rates from there, it probably
  3338. * doesn't happen any more, but keep the workaround so
  3339. * in case some *other* APs are buggy in different ways
  3340. * we can connect -- with a warning.
  3341. */
  3342. if (!basic_rates && min_rate_index >= 0) {
  3343. sdata_info(sdata,
  3344. "No basic rates, using min rate instead\n");
  3345. basic_rates = BIT(min_rate_index);
  3346. }
  3347. new_sta->sta.supp_rates[cbss->channel->band] = rates;
  3348. sdata->vif.bss_conf.basic_rates = basic_rates;
  3349. /* cf. IEEE 802.11 9.2.12 */
  3350. if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
  3351. have_higher_than_11mbit)
  3352. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  3353. else
  3354. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  3355. memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
  3356. /* set timing information */
  3357. sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
  3358. rcu_read_lock();
  3359. ies = rcu_dereference(cbss->beacon_ies);
  3360. if (ies) {
  3361. const u8 *tim_ie;
  3362. sdata->vif.bss_conf.sync_tsf = ies->tsf;
  3363. sdata->vif.bss_conf.sync_device_ts =
  3364. bss->device_ts_beacon;
  3365. tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
  3366. ies->data, ies->len);
  3367. if (tim_ie && tim_ie[1] >= 2)
  3368. sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
  3369. else
  3370. sdata->vif.bss_conf.sync_dtim_count = 0;
  3371. } else if (!(local->hw.flags &
  3372. IEEE80211_HW_TIMING_BEACON_ONLY)) {
  3373. ies = rcu_dereference(cbss->proberesp_ies);
  3374. /* must be non-NULL since beacon IEs were NULL */
  3375. sdata->vif.bss_conf.sync_tsf = ies->tsf;
  3376. sdata->vif.bss_conf.sync_device_ts =
  3377. bss->device_ts_presp;
  3378. sdata->vif.bss_conf.sync_dtim_count = 0;
  3379. } else {
  3380. sdata->vif.bss_conf.sync_tsf = 0;
  3381. sdata->vif.bss_conf.sync_device_ts = 0;
  3382. sdata->vif.bss_conf.sync_dtim_count = 0;
  3383. }
  3384. rcu_read_unlock();
  3385. /* tell driver about BSSID, basic rates and timing */
  3386. ieee80211_bss_info_change_notify(sdata,
  3387. BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
  3388. BSS_CHANGED_BEACON_INT);
  3389. if (assoc)
  3390. sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
  3391. err = sta_info_insert(new_sta);
  3392. new_sta = NULL;
  3393. if (err) {
  3394. sdata_info(sdata,
  3395. "failed to insert STA entry for the AP (error %d)\n",
  3396. err);
  3397. return err;
  3398. }
  3399. } else
  3400. WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
  3401. return 0;
  3402. }
  3403. /* config hooks */
  3404. int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
  3405. struct cfg80211_auth_request *req)
  3406. {
  3407. struct ieee80211_local *local = sdata->local;
  3408. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3409. struct ieee80211_mgd_auth_data *auth_data;
  3410. u16 auth_alg;
  3411. int err;
  3412. /* prepare auth data structure */
  3413. switch (req->auth_type) {
  3414. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  3415. auth_alg = WLAN_AUTH_OPEN;
  3416. break;
  3417. case NL80211_AUTHTYPE_SHARED_KEY:
  3418. if (IS_ERR(local->wep_tx_tfm))
  3419. return -EOPNOTSUPP;
  3420. auth_alg = WLAN_AUTH_SHARED_KEY;
  3421. break;
  3422. case NL80211_AUTHTYPE_FT:
  3423. auth_alg = WLAN_AUTH_FT;
  3424. break;
  3425. case NL80211_AUTHTYPE_NETWORK_EAP:
  3426. auth_alg = WLAN_AUTH_LEAP;
  3427. break;
  3428. case NL80211_AUTHTYPE_SAE:
  3429. auth_alg = WLAN_AUTH_SAE;
  3430. break;
  3431. default:
  3432. return -EOPNOTSUPP;
  3433. }
  3434. auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
  3435. req->ie_len, GFP_KERNEL);
  3436. if (!auth_data)
  3437. return -ENOMEM;
  3438. auth_data->bss = req->bss;
  3439. if (req->sae_data_len >= 4) {
  3440. __le16 *pos = (__le16 *) req->sae_data;
  3441. auth_data->sae_trans = le16_to_cpu(pos[0]);
  3442. auth_data->sae_status = le16_to_cpu(pos[1]);
  3443. memcpy(auth_data->data, req->sae_data + 4,
  3444. req->sae_data_len - 4);
  3445. auth_data->data_len += req->sae_data_len - 4;
  3446. }
  3447. if (req->ie && req->ie_len) {
  3448. memcpy(&auth_data->data[auth_data->data_len],
  3449. req->ie, req->ie_len);
  3450. auth_data->data_len += req->ie_len;
  3451. }
  3452. if (req->key && req->key_len) {
  3453. auth_data->key_len = req->key_len;
  3454. auth_data->key_idx = req->key_idx;
  3455. memcpy(auth_data->key, req->key, req->key_len);
  3456. }
  3457. auth_data->algorithm = auth_alg;
  3458. /* try to authenticate/probe */
  3459. if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
  3460. ifmgd->assoc_data) {
  3461. err = -EBUSY;
  3462. goto err_free;
  3463. }
  3464. if (ifmgd->auth_data)
  3465. ieee80211_destroy_auth_data(sdata, false);
  3466. /* prep auth_data so we don't go into idle on disassoc */
  3467. ifmgd->auth_data = auth_data;
  3468. if (ifmgd->associated) {
  3469. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3470. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  3471. WLAN_REASON_UNSPECIFIED,
  3472. false, frame_buf);
  3473. cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
  3474. sizeof(frame_buf));
  3475. }
  3476. sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
  3477. err = ieee80211_prep_connection(sdata, req->bss, false);
  3478. if (err)
  3479. goto err_clear;
  3480. err = ieee80211_probe_auth(sdata);
  3481. if (err) {
  3482. sta_info_destroy_addr(sdata, req->bss->bssid);
  3483. goto err_clear;
  3484. }
  3485. /* hold our own reference */
  3486. cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
  3487. return 0;
  3488. err_clear:
  3489. memset(ifmgd->bssid, 0, ETH_ALEN);
  3490. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  3491. ifmgd->auth_data = NULL;
  3492. err_free:
  3493. kfree(auth_data);
  3494. return err;
  3495. }
  3496. static bool ieee80211_usable_wmm_params(struct ieee80211_sub_if_data *sdata,
  3497. const u8 *wmm_param, int len)
  3498. {
  3499. const u8 *pos;
  3500. size_t left;
  3501. if (len < 8)
  3502. return false;
  3503. if (wmm_param[5] != 1 /* version */)
  3504. return false;
  3505. pos = wmm_param + 8;
  3506. left = len - 8;
  3507. for (; left >= 4; left -= 4, pos += 4) {
  3508. u8 aifsn = pos[0] & 0x0f;
  3509. u8 ecwmin = pos[1] & 0x0f;
  3510. u8 ecwmax = (pos[1] & 0xf0) >> 4;
  3511. int aci = (pos[0] >> 5) & 0x03;
  3512. if (aifsn < 2) {
  3513. sdata_info(sdata,
  3514. "AP has invalid WMM params (AIFSN=%d for ACI %d), disabling WMM\n",
  3515. aifsn, aci);
  3516. return false;
  3517. }
  3518. if (ecwmin > ecwmax) {
  3519. sdata_info(sdata,
  3520. "AP has invalid WMM params (ECWmin/max=%d/%d for ACI %d), disabling WMM\n",
  3521. ecwmin, ecwmax, aci);
  3522. return false;
  3523. }
  3524. }
  3525. return true;
  3526. }
  3527. int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
  3528. struct cfg80211_assoc_request *req)
  3529. {
  3530. struct ieee80211_local *local = sdata->local;
  3531. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3532. struct ieee80211_bss *bss = (void *)req->bss->priv;
  3533. struct ieee80211_mgd_assoc_data *assoc_data;
  3534. const struct cfg80211_bss_ies *beacon_ies;
  3535. struct ieee80211_supported_band *sband;
  3536. const u8 *ssidie, *ht_ie, *vht_ie;
  3537. int i, err;
  3538. assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
  3539. if (!assoc_data)
  3540. return -ENOMEM;
  3541. rcu_read_lock();
  3542. ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  3543. if (!ssidie) {
  3544. rcu_read_unlock();
  3545. kfree(assoc_data);
  3546. return -EINVAL;
  3547. }
  3548. memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
  3549. assoc_data->ssid_len = ssidie[1];
  3550. rcu_read_unlock();
  3551. if (ifmgd->associated) {
  3552. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3553. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  3554. WLAN_REASON_UNSPECIFIED,
  3555. false, frame_buf);
  3556. cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
  3557. sizeof(frame_buf));
  3558. }
  3559. if (ifmgd->auth_data && !ifmgd->auth_data->done) {
  3560. err = -EBUSY;
  3561. goto err_free;
  3562. }
  3563. if (ifmgd->assoc_data) {
  3564. err = -EBUSY;
  3565. goto err_free;
  3566. }
  3567. if (ifmgd->auth_data) {
  3568. bool match;
  3569. /* keep sta info, bssid if matching */
  3570. match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
  3571. ieee80211_destroy_auth_data(sdata, match);
  3572. }
  3573. /* prepare assoc data */
  3574. ifmgd->beacon_crc_valid = false;
  3575. assoc_data->wmm = bss->wmm_used &&
  3576. (local->hw.queues >= IEEE80211_NUM_ACS);
  3577. if (assoc_data->wmm) {
  3578. /* try to check validity of WMM params IE */
  3579. const struct cfg80211_bss_ies *ies;
  3580. const u8 *wp, *start, *end;
  3581. rcu_read_lock();
  3582. ies = rcu_dereference(req->bss->ies);
  3583. start = ies->data;
  3584. end = start + ies->len;
  3585. while (true) {
  3586. wp = cfg80211_find_vendor_ie(
  3587. WLAN_OUI_MICROSOFT,
  3588. WLAN_OUI_TYPE_MICROSOFT_WMM,
  3589. start, end - start);
  3590. if (!wp)
  3591. break;
  3592. start = wp + wp[1] + 2;
  3593. /* if this IE is too short, try the next */
  3594. if (wp[1] <= 4)
  3595. continue;
  3596. /* if this IE is WMM params, we found what we wanted */
  3597. if (wp[6] == 1)
  3598. break;
  3599. }
  3600. if (!wp || !ieee80211_usable_wmm_params(sdata, wp + 2,
  3601. wp[1] - 2)) {
  3602. assoc_data->wmm = false;
  3603. ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
  3604. }
  3605. rcu_read_unlock();
  3606. }
  3607. /*
  3608. * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
  3609. * We still associate in non-HT mode (11a/b/g) if any one of these
  3610. * ciphers is configured as pairwise.
  3611. * We can set this to true for non-11n hardware, that'll be checked
  3612. * separately along with the peer capabilities.
  3613. */
  3614. for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
  3615. if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
  3616. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
  3617. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
  3618. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3619. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3620. netdev_info(sdata->dev,
  3621. "disabling HT/VHT due to WEP/TKIP use\n");
  3622. }
  3623. }
  3624. if (req->flags & ASSOC_REQ_DISABLE_HT) {
  3625. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3626. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3627. }
  3628. if (req->flags & ASSOC_REQ_DISABLE_VHT)
  3629. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3630. /* Also disable HT if we don't support it or the AP doesn't use WMM */
  3631. sband = local->hw.wiphy->bands[req->bss->channel->band];
  3632. if (!sband->ht_cap.ht_supported ||
  3633. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
  3634. ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
  3635. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3636. if (!bss->wmm_used &&
  3637. !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
  3638. netdev_info(sdata->dev,
  3639. "disabling HT as WMM/QoS is not supported by the AP\n");
  3640. }
  3641. /* disable VHT if we don't support it or the AP doesn't use WMM */
  3642. if (!sband->vht_cap.vht_supported ||
  3643. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
  3644. ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
  3645. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3646. if (!bss->wmm_used &&
  3647. !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
  3648. netdev_info(sdata->dev,
  3649. "disabling VHT as WMM/QoS is not supported by the AP\n");
  3650. }
  3651. memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
  3652. memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
  3653. sizeof(ifmgd->ht_capa_mask));
  3654. memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
  3655. memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
  3656. sizeof(ifmgd->vht_capa_mask));
  3657. if (req->ie && req->ie_len) {
  3658. memcpy(assoc_data->ie, req->ie, req->ie_len);
  3659. assoc_data->ie_len = req->ie_len;
  3660. }
  3661. assoc_data->bss = req->bss;
  3662. if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
  3663. if (ifmgd->powersave)
  3664. sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
  3665. else
  3666. sdata->smps_mode = IEEE80211_SMPS_OFF;
  3667. } else
  3668. sdata->smps_mode = ifmgd->req_smps;
  3669. assoc_data->capability = req->bss->capability;
  3670. assoc_data->supp_rates = bss->supp_rates;
  3671. assoc_data->supp_rates_len = bss->supp_rates_len;
  3672. rcu_read_lock();
  3673. ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
  3674. if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
  3675. assoc_data->ap_ht_param =
  3676. ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
  3677. else
  3678. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3679. vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
  3680. if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
  3681. memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
  3682. sizeof(struct ieee80211_vht_cap));
  3683. else
  3684. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3685. rcu_read_unlock();
  3686. if (bss->wmm_used && bss->uapsd_supported &&
  3687. (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD) &&
  3688. sdata->wmm_acm != 0xff) {
  3689. assoc_data->uapsd = true;
  3690. ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
  3691. } else {
  3692. assoc_data->uapsd = false;
  3693. ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
  3694. }
  3695. if (req->prev_bssid)
  3696. memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
  3697. if (req->use_mfp) {
  3698. ifmgd->mfp = IEEE80211_MFP_REQUIRED;
  3699. ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
  3700. } else {
  3701. ifmgd->mfp = IEEE80211_MFP_DISABLED;
  3702. ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
  3703. }
  3704. if (req->crypto.control_port)
  3705. ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
  3706. else
  3707. ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
  3708. sdata->control_port_protocol = req->crypto.control_port_ethertype;
  3709. sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
  3710. /* kick off associate process */
  3711. ifmgd->assoc_data = assoc_data;
  3712. ifmgd->dtim_period = 0;
  3713. ifmgd->have_beacon = false;
  3714. err = ieee80211_prep_connection(sdata, req->bss, true);
  3715. if (err)
  3716. goto err_clear;
  3717. rcu_read_lock();
  3718. beacon_ies = rcu_dereference(req->bss->beacon_ies);
  3719. if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
  3720. !beacon_ies) {
  3721. /*
  3722. * Wait up to one beacon interval ...
  3723. * should this be more if we miss one?
  3724. */
  3725. sdata_info(sdata, "waiting for beacon from %pM\n",
  3726. ifmgd->bssid);
  3727. assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
  3728. assoc_data->timeout_started = true;
  3729. assoc_data->need_beacon = true;
  3730. } else if (beacon_ies) {
  3731. const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
  3732. beacon_ies->data,
  3733. beacon_ies->len);
  3734. u8 dtim_count = 0;
  3735. if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
  3736. const struct ieee80211_tim_ie *tim;
  3737. tim = (void *)(tim_ie + 2);
  3738. ifmgd->dtim_period = tim->dtim_period;
  3739. dtim_count = tim->dtim_count;
  3740. }
  3741. ifmgd->have_beacon = true;
  3742. assoc_data->timeout = jiffies;
  3743. assoc_data->timeout_started = true;
  3744. if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
  3745. sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
  3746. sdata->vif.bss_conf.sync_device_ts =
  3747. bss->device_ts_beacon;
  3748. sdata->vif.bss_conf.sync_dtim_count = dtim_count;
  3749. }
  3750. } else {
  3751. assoc_data->timeout = jiffies;
  3752. assoc_data->timeout_started = true;
  3753. }
  3754. rcu_read_unlock();
  3755. run_again(sdata, assoc_data->timeout);
  3756. if (bss->corrupt_data) {
  3757. char *corrupt_type = "data";
  3758. if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
  3759. if (bss->corrupt_data &
  3760. IEEE80211_BSS_CORRUPT_PROBE_RESP)
  3761. corrupt_type = "beacon and probe response";
  3762. else
  3763. corrupt_type = "beacon";
  3764. } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
  3765. corrupt_type = "probe response";
  3766. sdata_info(sdata, "associating with AP with corrupt %s\n",
  3767. corrupt_type);
  3768. }
  3769. return 0;
  3770. err_clear:
  3771. memset(ifmgd->bssid, 0, ETH_ALEN);
  3772. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  3773. ifmgd->assoc_data = NULL;
  3774. err_free:
  3775. kfree(assoc_data);
  3776. return err;
  3777. }
  3778. int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
  3779. struct cfg80211_deauth_request *req)
  3780. {
  3781. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3782. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3783. bool tx = !req->local_state_change;
  3784. bool report_frame = false;
  3785. sdata_info(sdata,
  3786. "deauthenticating from %pM by local choice (reason=%d)\n",
  3787. req->bssid, req->reason_code);
  3788. if (ifmgd->auth_data) {
  3789. drv_mgd_prepare_tx(sdata->local, sdata);
  3790. ieee80211_send_deauth_disassoc(sdata, req->bssid,
  3791. IEEE80211_STYPE_DEAUTH,
  3792. req->reason_code, tx,
  3793. frame_buf);
  3794. ieee80211_destroy_auth_data(sdata, false);
  3795. report_frame = true;
  3796. goto out;
  3797. }
  3798. if (ifmgd->associated &&
  3799. ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
  3800. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  3801. req->reason_code, tx, frame_buf);
  3802. report_frame = true;
  3803. }
  3804. out:
  3805. if (report_frame)
  3806. cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
  3807. IEEE80211_DEAUTH_FRAME_LEN);
  3808. return 0;
  3809. }
  3810. int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
  3811. struct cfg80211_disassoc_request *req)
  3812. {
  3813. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3814. u8 bssid[ETH_ALEN];
  3815. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3816. /*
  3817. * cfg80211 should catch this ... but it's racy since
  3818. * we can receive a disassoc frame, process it, hand it
  3819. * to cfg80211 while that's in a locked section already
  3820. * trying to tell us that the user wants to disconnect.
  3821. */
  3822. if (ifmgd->associated != req->bss)
  3823. return -ENOLINK;
  3824. sdata_info(sdata,
  3825. "disassociating from %pM by local choice (reason=%d)\n",
  3826. req->bss->bssid, req->reason_code);
  3827. memcpy(bssid, req->bss->bssid, ETH_ALEN);
  3828. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
  3829. req->reason_code, !req->local_state_change,
  3830. frame_buf);
  3831. cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
  3832. IEEE80211_DEAUTH_FRAME_LEN);
  3833. return 0;
  3834. }
  3835. void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
  3836. {
  3837. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3838. /*
  3839. * Make sure some work items will not run after this,
  3840. * they will not do anything but might not have been
  3841. * cancelled when disconnecting.
  3842. */
  3843. cancel_work_sync(&ifmgd->monitor_work);
  3844. cancel_work_sync(&ifmgd->beacon_connection_loss_work);
  3845. cancel_work_sync(&ifmgd->request_smps_work);
  3846. cancel_work_sync(&ifmgd->csa_connection_drop_work);
  3847. cancel_work_sync(&ifmgd->chswitch_work);
  3848. sdata_lock(sdata);
  3849. if (ifmgd->assoc_data) {
  3850. struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
  3851. ieee80211_destroy_assoc_data(sdata, false);
  3852. cfg80211_assoc_timeout(sdata->dev, bss);
  3853. }
  3854. if (ifmgd->auth_data)
  3855. ieee80211_destroy_auth_data(sdata, false);
  3856. del_timer_sync(&ifmgd->timer);
  3857. sdata_unlock(sdata);
  3858. }
  3859. void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
  3860. enum nl80211_cqm_rssi_threshold_event rssi_event,
  3861. gfp_t gfp)
  3862. {
  3863. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  3864. trace_api_cqm_rssi_notify(sdata, rssi_event);
  3865. cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
  3866. }
  3867. EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);