mlme.c 144 KB

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