mlme.c 143 KB

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