mlme.c 145 KB

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