mlme.c 125 KB

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