mlme.c 126 KB

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