mlme.c 149 KB

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