mlme.c 146 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090
  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. timeout = local->dynamic_ps_forced_timeout;
  1273. if (timeout < 0)
  1274. timeout = 100;
  1275. local->hw.conf.dynamic_ps_timeout = timeout;
  1276. /* If the TIM IE is invalid, pretend the value is 1 */
  1277. if (!dtimper)
  1278. dtimper = 1;
  1279. local->hw.conf.ps_dtim_period = dtimper;
  1280. local->ps_sdata = found;
  1281. } else {
  1282. local->ps_sdata = NULL;
  1283. }
  1284. ieee80211_change_ps(local);
  1285. }
  1286. void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
  1287. {
  1288. bool ps_allowed = ieee80211_powersave_allowed(sdata);
  1289. if (sdata->vif.bss_conf.ps != ps_allowed) {
  1290. sdata->vif.bss_conf.ps = ps_allowed;
  1291. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
  1292. }
  1293. }
  1294. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  1295. {
  1296. struct ieee80211_local *local =
  1297. container_of(work, struct ieee80211_local,
  1298. dynamic_ps_disable_work);
  1299. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1300. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1301. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1302. }
  1303. ieee80211_wake_queues_by_reason(&local->hw,
  1304. IEEE80211_MAX_QUEUE_MAP,
  1305. IEEE80211_QUEUE_STOP_REASON_PS,
  1306. false);
  1307. }
  1308. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  1309. {
  1310. struct ieee80211_local *local =
  1311. container_of(work, struct ieee80211_local,
  1312. dynamic_ps_enable_work);
  1313. struct ieee80211_sub_if_data *sdata = local->ps_sdata;
  1314. struct ieee80211_if_managed *ifmgd;
  1315. unsigned long flags;
  1316. int q;
  1317. /* can only happen when PS was just disabled anyway */
  1318. if (!sdata)
  1319. return;
  1320. ifmgd = &sdata->u.mgd;
  1321. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  1322. return;
  1323. if (local->hw.conf.dynamic_ps_timeout > 0) {
  1324. /* don't enter PS if TX frames are pending */
  1325. if (drv_tx_frames_pending(local)) {
  1326. mod_timer(&local->dynamic_ps_timer, jiffies +
  1327. msecs_to_jiffies(
  1328. local->hw.conf.dynamic_ps_timeout));
  1329. return;
  1330. }
  1331. /*
  1332. * transmission can be stopped by others which leads to
  1333. * dynamic_ps_timer expiry. Postpone the ps timer if it
  1334. * is not the actual idle state.
  1335. */
  1336. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  1337. for (q = 0; q < local->hw.queues; q++) {
  1338. if (local->queue_stop_reasons[q]) {
  1339. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  1340. flags);
  1341. mod_timer(&local->dynamic_ps_timer, jiffies +
  1342. msecs_to_jiffies(
  1343. local->hw.conf.dynamic_ps_timeout));
  1344. return;
  1345. }
  1346. }
  1347. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  1348. }
  1349. if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
  1350. !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  1351. if (drv_tx_frames_pending(local)) {
  1352. mod_timer(&local->dynamic_ps_timer, jiffies +
  1353. msecs_to_jiffies(
  1354. local->hw.conf.dynamic_ps_timeout));
  1355. } else {
  1356. ieee80211_send_nullfunc(local, sdata, true);
  1357. /* Flush to get the tx status of nullfunc frame */
  1358. ieee80211_flush_queues(local, sdata, false);
  1359. }
  1360. }
  1361. if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) &&
  1362. ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) ||
  1363. (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  1364. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  1365. local->hw.conf.flags |= IEEE80211_CONF_PS;
  1366. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1367. }
  1368. }
  1369. void ieee80211_dynamic_ps_timer(unsigned long data)
  1370. {
  1371. struct ieee80211_local *local = (void *) data;
  1372. ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
  1373. }
  1374. void ieee80211_dfs_cac_timer_work(struct work_struct *work)
  1375. {
  1376. struct delayed_work *delayed_work = to_delayed_work(work);
  1377. struct ieee80211_sub_if_data *sdata =
  1378. container_of(delayed_work, struct ieee80211_sub_if_data,
  1379. dfs_cac_timer_work);
  1380. struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
  1381. mutex_lock(&sdata->local->mtx);
  1382. if (sdata->wdev.cac_started) {
  1383. ieee80211_vif_release_channel(sdata);
  1384. cfg80211_cac_event(sdata->dev, &chandef,
  1385. NL80211_RADAR_CAC_FINISHED,
  1386. GFP_KERNEL);
  1387. }
  1388. mutex_unlock(&sdata->local->mtx);
  1389. }
  1390. static bool
  1391. __ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
  1392. {
  1393. struct ieee80211_local *local = sdata->local;
  1394. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1395. bool ret = false;
  1396. int ac;
  1397. if (local->hw.queues < IEEE80211_NUM_ACS)
  1398. return false;
  1399. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1400. struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
  1401. int non_acm_ac;
  1402. unsigned long now = jiffies;
  1403. if (tx_tspec->action == TX_TSPEC_ACTION_NONE &&
  1404. tx_tspec->admitted_time &&
  1405. time_after(now, tx_tspec->time_slice_start + HZ)) {
  1406. tx_tspec->consumed_tx_time = 0;
  1407. tx_tspec->time_slice_start = now;
  1408. if (tx_tspec->downgraded)
  1409. tx_tspec->action =
  1410. TX_TSPEC_ACTION_STOP_DOWNGRADE;
  1411. }
  1412. switch (tx_tspec->action) {
  1413. case TX_TSPEC_ACTION_STOP_DOWNGRADE:
  1414. /* take the original parameters */
  1415. if (drv_conf_tx(local, sdata, ac, &sdata->tx_conf[ac]))
  1416. sdata_err(sdata,
  1417. "failed to set TX queue parameters for queue %d\n",
  1418. ac);
  1419. tx_tspec->action = TX_TSPEC_ACTION_NONE;
  1420. tx_tspec->downgraded = false;
  1421. ret = true;
  1422. break;
  1423. case TX_TSPEC_ACTION_DOWNGRADE:
  1424. if (time_after(now, tx_tspec->time_slice_start + HZ)) {
  1425. tx_tspec->action = TX_TSPEC_ACTION_NONE;
  1426. ret = true;
  1427. break;
  1428. }
  1429. /* downgrade next lower non-ACM AC */
  1430. for (non_acm_ac = ac + 1;
  1431. non_acm_ac < IEEE80211_NUM_ACS;
  1432. non_acm_ac++)
  1433. if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac)))
  1434. break;
  1435. /* Usually the loop will result in using BK even if it
  1436. * requires admission control, but such a configuration
  1437. * makes no sense and we have to transmit somehow - the
  1438. * AC selection does the same thing.
  1439. * If we started out trying to downgrade from BK, then
  1440. * the extra condition here might be needed.
  1441. */
  1442. if (non_acm_ac >= IEEE80211_NUM_ACS)
  1443. non_acm_ac = IEEE80211_AC_BK;
  1444. if (drv_conf_tx(local, sdata, ac,
  1445. &sdata->tx_conf[non_acm_ac]))
  1446. sdata_err(sdata,
  1447. "failed to set TX queue parameters for queue %d\n",
  1448. ac);
  1449. tx_tspec->action = TX_TSPEC_ACTION_NONE;
  1450. ret = true;
  1451. schedule_delayed_work(&ifmgd->tx_tspec_wk,
  1452. tx_tspec->time_slice_start + HZ - now + 1);
  1453. break;
  1454. case TX_TSPEC_ACTION_NONE:
  1455. /* nothing now */
  1456. break;
  1457. }
  1458. }
  1459. return ret;
  1460. }
  1461. void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata)
  1462. {
  1463. if (__ieee80211_sta_handle_tspec_ac_params(sdata))
  1464. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
  1465. }
  1466. static void ieee80211_sta_handle_tspec_ac_params_wk(struct work_struct *work)
  1467. {
  1468. struct ieee80211_sub_if_data *sdata;
  1469. sdata = container_of(work, struct ieee80211_sub_if_data,
  1470. u.mgd.tx_tspec_wk.work);
  1471. ieee80211_sta_handle_tspec_ac_params(sdata);
  1472. }
  1473. /* MLME */
  1474. static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
  1475. struct ieee80211_sub_if_data *sdata,
  1476. const u8 *wmm_param, size_t wmm_param_len)
  1477. {
  1478. struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS];
  1479. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1480. size_t left;
  1481. int count, ac;
  1482. const u8 *pos;
  1483. u8 uapsd_queues = 0;
  1484. if (!local->ops->conf_tx)
  1485. return false;
  1486. if (local->hw.queues < IEEE80211_NUM_ACS)
  1487. return false;
  1488. if (!wmm_param)
  1489. return false;
  1490. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  1491. return false;
  1492. if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
  1493. uapsd_queues = ifmgd->uapsd_queues;
  1494. count = wmm_param[6] & 0x0f;
  1495. if (count == ifmgd->wmm_last_param_set)
  1496. return false;
  1497. ifmgd->wmm_last_param_set = count;
  1498. pos = wmm_param + 8;
  1499. left = wmm_param_len - 8;
  1500. memset(&params, 0, sizeof(params));
  1501. sdata->wmm_acm = 0;
  1502. for (; left >= 4; left -= 4, pos += 4) {
  1503. int aci = (pos[0] >> 5) & 0x03;
  1504. int acm = (pos[0] >> 4) & 0x01;
  1505. bool uapsd = false;
  1506. switch (aci) {
  1507. case 1: /* AC_BK */
  1508. ac = IEEE80211_AC_BK;
  1509. if (acm)
  1510. sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
  1511. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
  1512. uapsd = true;
  1513. break;
  1514. case 2: /* AC_VI */
  1515. ac = IEEE80211_AC_VI;
  1516. if (acm)
  1517. sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
  1518. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
  1519. uapsd = true;
  1520. break;
  1521. case 3: /* AC_VO */
  1522. ac = IEEE80211_AC_VO;
  1523. if (acm)
  1524. sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
  1525. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  1526. uapsd = true;
  1527. break;
  1528. case 0: /* AC_BE */
  1529. default:
  1530. ac = IEEE80211_AC_BE;
  1531. if (acm)
  1532. sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
  1533. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
  1534. uapsd = true;
  1535. break;
  1536. }
  1537. params[ac].aifs = pos[0] & 0x0f;
  1538. if (params[ac].aifs < 2) {
  1539. sdata_info(sdata,
  1540. "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n",
  1541. params[ac].aifs, aci);
  1542. params[ac].aifs = 2;
  1543. }
  1544. params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  1545. params[ac].cw_min = ecw2cw(pos[1] & 0x0f);
  1546. params[ac].txop = get_unaligned_le16(pos + 2);
  1547. params[ac].acm = acm;
  1548. params[ac].uapsd = uapsd;
  1549. if (params[ac].cw_min > params[ac].cw_max) {
  1550. sdata_info(sdata,
  1551. "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n",
  1552. params[ac].cw_min, params[ac].cw_max, aci);
  1553. return false;
  1554. }
  1555. }
  1556. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1557. mlme_dbg(sdata,
  1558. "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n",
  1559. ac, params[ac].acm,
  1560. params[ac].aifs, params[ac].cw_min, params[ac].cw_max,
  1561. params[ac].txop, params[ac].uapsd,
  1562. ifmgd->tx_tspec[ac].downgraded);
  1563. sdata->tx_conf[ac] = params[ac];
  1564. if (!ifmgd->tx_tspec[ac].downgraded &&
  1565. drv_conf_tx(local, sdata, ac, &params[ac]))
  1566. sdata_err(sdata,
  1567. "failed to set TX queue parameters for AC %d\n",
  1568. ac);
  1569. }
  1570. /* enable WMM or activate new settings */
  1571. sdata->vif.bss_conf.qos = true;
  1572. return true;
  1573. }
  1574. static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
  1575. {
  1576. lockdep_assert_held(&sdata->local->mtx);
  1577. sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
  1578. ieee80211_run_deferred_scan(sdata->local);
  1579. }
  1580. static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
  1581. {
  1582. mutex_lock(&sdata->local->mtx);
  1583. __ieee80211_stop_poll(sdata);
  1584. mutex_unlock(&sdata->local->mtx);
  1585. }
  1586. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  1587. u16 capab, bool erp_valid, u8 erp)
  1588. {
  1589. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1590. u32 changed = 0;
  1591. bool use_protection;
  1592. bool use_short_preamble;
  1593. bool use_short_slot;
  1594. if (erp_valid) {
  1595. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  1596. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  1597. } else {
  1598. use_protection = false;
  1599. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  1600. }
  1601. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  1602. if (ieee80211_get_sdata_band(sdata) == NL80211_BAND_5GHZ)
  1603. use_short_slot = true;
  1604. if (use_protection != bss_conf->use_cts_prot) {
  1605. bss_conf->use_cts_prot = use_protection;
  1606. changed |= BSS_CHANGED_ERP_CTS_PROT;
  1607. }
  1608. if (use_short_preamble != bss_conf->use_short_preamble) {
  1609. bss_conf->use_short_preamble = use_short_preamble;
  1610. changed |= BSS_CHANGED_ERP_PREAMBLE;
  1611. }
  1612. if (use_short_slot != bss_conf->use_short_slot) {
  1613. bss_conf->use_short_slot = use_short_slot;
  1614. changed |= BSS_CHANGED_ERP_SLOT;
  1615. }
  1616. return changed;
  1617. }
  1618. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  1619. struct cfg80211_bss *cbss,
  1620. u32 bss_info_changed)
  1621. {
  1622. struct ieee80211_bss *bss = (void *)cbss->priv;
  1623. struct ieee80211_local *local = sdata->local;
  1624. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1625. bss_info_changed |= BSS_CHANGED_ASSOC;
  1626. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  1627. bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
  1628. sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
  1629. beacon_loss_count * bss_conf->beacon_int));
  1630. sdata->u.mgd.associated = cbss;
  1631. memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
  1632. ieee80211_check_rate_mask(sdata);
  1633. sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
  1634. if (sdata->vif.p2p ||
  1635. sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
  1636. const struct cfg80211_bss_ies *ies;
  1637. rcu_read_lock();
  1638. ies = rcu_dereference(cbss->ies);
  1639. if (ies) {
  1640. int ret;
  1641. ret = cfg80211_get_p2p_attr(
  1642. ies->data, ies->len,
  1643. IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
  1644. (u8 *) &bss_conf->p2p_noa_attr,
  1645. sizeof(bss_conf->p2p_noa_attr));
  1646. if (ret >= 2) {
  1647. sdata->u.mgd.p2p_noa_index =
  1648. bss_conf->p2p_noa_attr.index;
  1649. bss_info_changed |= BSS_CHANGED_P2P_PS;
  1650. }
  1651. }
  1652. rcu_read_unlock();
  1653. }
  1654. /* just to be sure */
  1655. ieee80211_stop_poll(sdata);
  1656. ieee80211_led_assoc(local, 1);
  1657. if (sdata->u.mgd.have_beacon) {
  1658. /*
  1659. * If the AP is buggy we may get here with no DTIM period
  1660. * known, so assume it's 1 which is the only safe assumption
  1661. * in that case, although if the TIM IE is broken powersave
  1662. * probably just won't work at all.
  1663. */
  1664. bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
  1665. bss_conf->beacon_rate = bss->beacon_rate;
  1666. bss_info_changed |= BSS_CHANGED_BEACON_INFO;
  1667. } else {
  1668. bss_conf->beacon_rate = NULL;
  1669. bss_conf->dtim_period = 0;
  1670. }
  1671. bss_conf->assoc = 1;
  1672. /* Tell the driver to monitor connection quality (if supported) */
  1673. if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
  1674. bss_conf->cqm_rssi_thold)
  1675. bss_info_changed |= BSS_CHANGED_CQM;
  1676. /* Enable ARP filtering */
  1677. if (bss_conf->arp_addr_cnt)
  1678. bss_info_changed |= BSS_CHANGED_ARP_FILTER;
  1679. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  1680. mutex_lock(&local->iflist_mtx);
  1681. ieee80211_recalc_ps(local);
  1682. mutex_unlock(&local->iflist_mtx);
  1683. ieee80211_recalc_smps(sdata);
  1684. ieee80211_recalc_ps_vif(sdata);
  1685. netif_carrier_on(sdata->dev);
  1686. }
  1687. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  1688. u16 stype, u16 reason, bool tx,
  1689. u8 *frame_buf)
  1690. {
  1691. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1692. struct ieee80211_local *local = sdata->local;
  1693. u32 changed = 0;
  1694. sdata_assert_lock(sdata);
  1695. if (WARN_ON_ONCE(tx && !frame_buf))
  1696. return;
  1697. if (WARN_ON(!ifmgd->associated))
  1698. return;
  1699. ieee80211_stop_poll(sdata);
  1700. ifmgd->associated = NULL;
  1701. netif_carrier_off(sdata->dev);
  1702. /*
  1703. * if we want to get out of ps before disassoc (why?) we have
  1704. * to do it before sending disassoc, as otherwise the null-packet
  1705. * won't be valid.
  1706. */
  1707. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1708. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1709. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1710. }
  1711. local->ps_sdata = NULL;
  1712. /* disable per-vif ps */
  1713. ieee80211_recalc_ps_vif(sdata);
  1714. /* make sure ongoing transmission finishes */
  1715. synchronize_net();
  1716. /*
  1717. * drop any frame before deauth/disassoc, this can be data or
  1718. * management frame. Since we are disconnecting, we should not
  1719. * insist sending these frames which can take time and delay
  1720. * the disconnection and possible the roaming.
  1721. */
  1722. if (tx)
  1723. ieee80211_flush_queues(local, sdata, true);
  1724. /* deauthenticate/disassociate now */
  1725. if (tx || frame_buf)
  1726. ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
  1727. reason, tx, frame_buf);
  1728. /* flush out frame - make sure the deauth was actually sent */
  1729. if (tx)
  1730. ieee80211_flush_queues(local, sdata, false);
  1731. /* clear bssid only after building the needed mgmt frames */
  1732. eth_zero_addr(ifmgd->bssid);
  1733. /* remove AP and TDLS peers */
  1734. sta_info_flush(sdata);
  1735. /* finally reset all BSS / config parameters */
  1736. changed |= ieee80211_reset_erp_info(sdata);
  1737. ieee80211_led_assoc(local, 0);
  1738. changed |= BSS_CHANGED_ASSOC;
  1739. sdata->vif.bss_conf.assoc = false;
  1740. ifmgd->p2p_noa_index = -1;
  1741. memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
  1742. sizeof(sdata->vif.bss_conf.p2p_noa_attr));
  1743. /* on the next assoc, re-program HT/VHT parameters */
  1744. memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
  1745. memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
  1746. memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
  1747. memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
  1748. /* reset MU-MIMO ownership and group data */
  1749. memset(sdata->vif.bss_conf.mu_group.membership, 0,
  1750. sizeof(sdata->vif.bss_conf.mu_group.membership));
  1751. memset(sdata->vif.bss_conf.mu_group.position, 0,
  1752. sizeof(sdata->vif.bss_conf.mu_group.position));
  1753. changed |= BSS_CHANGED_MU_GROUPS;
  1754. sdata->vif.mu_mimo_owner = false;
  1755. sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
  1756. del_timer_sync(&local->dynamic_ps_timer);
  1757. cancel_work_sync(&local->dynamic_ps_enable_work);
  1758. /* Disable ARP filtering */
  1759. if (sdata->vif.bss_conf.arp_addr_cnt)
  1760. changed |= BSS_CHANGED_ARP_FILTER;
  1761. sdata->vif.bss_conf.qos = false;
  1762. changed |= BSS_CHANGED_QOS;
  1763. /* The BSSID (not really interesting) and HT changed */
  1764. changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
  1765. ieee80211_bss_info_change_notify(sdata, changed);
  1766. /* disassociated - set to defaults now */
  1767. ieee80211_set_wmm_default(sdata, false, false);
  1768. del_timer_sync(&sdata->u.mgd.conn_mon_timer);
  1769. del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
  1770. del_timer_sync(&sdata->u.mgd.timer);
  1771. del_timer_sync(&sdata->u.mgd.chswitch_timer);
  1772. sdata->vif.bss_conf.dtim_period = 0;
  1773. sdata->vif.bss_conf.beacon_rate = NULL;
  1774. ifmgd->have_beacon = false;
  1775. ifmgd->flags = 0;
  1776. mutex_lock(&local->mtx);
  1777. ieee80211_vif_release_channel(sdata);
  1778. sdata->vif.csa_active = false;
  1779. ifmgd->csa_waiting_bcn = false;
  1780. ifmgd->csa_ignored_same_chan = false;
  1781. if (sdata->csa_block_tx) {
  1782. ieee80211_wake_vif_queues(local, sdata,
  1783. IEEE80211_QUEUE_STOP_REASON_CSA);
  1784. sdata->csa_block_tx = false;
  1785. }
  1786. mutex_unlock(&local->mtx);
  1787. /* existing TX TSPEC sessions no longer exist */
  1788. memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec));
  1789. cancel_delayed_work_sync(&ifmgd->tx_tspec_wk);
  1790. sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
  1791. }
  1792. void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
  1793. struct ieee80211_hdr *hdr)
  1794. {
  1795. /*
  1796. * We can postpone the mgd.timer whenever receiving unicast frames
  1797. * from AP because we know that the connection is working both ways
  1798. * at that time. But multicast frames (and hence also beacons) must
  1799. * be ignored here, because we need to trigger the timer during
  1800. * data idle periods for sending the periodic probe request to the
  1801. * AP we're connected to.
  1802. */
  1803. if (is_multicast_ether_addr(hdr->addr1))
  1804. return;
  1805. ieee80211_sta_reset_conn_monitor(sdata);
  1806. }
  1807. static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
  1808. {
  1809. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1810. struct ieee80211_local *local = sdata->local;
  1811. mutex_lock(&local->mtx);
  1812. if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
  1813. goto out;
  1814. __ieee80211_stop_poll(sdata);
  1815. mutex_lock(&local->iflist_mtx);
  1816. ieee80211_recalc_ps(local);
  1817. mutex_unlock(&local->iflist_mtx);
  1818. if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
  1819. goto out;
  1820. /*
  1821. * We've received a probe response, but are not sure whether
  1822. * we have or will be receiving any beacons or data, so let's
  1823. * schedule the timers again, just in case.
  1824. */
  1825. ieee80211_sta_reset_beacon_monitor(sdata);
  1826. mod_timer(&ifmgd->conn_mon_timer,
  1827. round_jiffies_up(jiffies +
  1828. IEEE80211_CONNECTION_IDLE_TIME));
  1829. out:
  1830. mutex_unlock(&local->mtx);
  1831. }
  1832. static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata,
  1833. struct ieee80211_hdr *hdr,
  1834. u16 tx_time)
  1835. {
  1836. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1837. u16 tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  1838. int ac = ieee80211_ac_from_tid(tid);
  1839. struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac];
  1840. unsigned long now = jiffies;
  1841. if (likely(!tx_tspec->admitted_time))
  1842. return;
  1843. if (time_after(now, tx_tspec->time_slice_start + HZ)) {
  1844. tx_tspec->consumed_tx_time = 0;
  1845. tx_tspec->time_slice_start = now;
  1846. if (tx_tspec->downgraded) {
  1847. tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE;
  1848. schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
  1849. }
  1850. }
  1851. if (tx_tspec->downgraded)
  1852. return;
  1853. tx_tspec->consumed_tx_time += tx_time;
  1854. if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) {
  1855. tx_tspec->downgraded = true;
  1856. tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE;
  1857. schedule_delayed_work(&ifmgd->tx_tspec_wk, 0);
  1858. }
  1859. }
  1860. void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
  1861. struct ieee80211_hdr *hdr, bool ack, u16 tx_time)
  1862. {
  1863. ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time);
  1864. if (!ieee80211_is_data(hdr->frame_control))
  1865. return;
  1866. if (ieee80211_is_nullfunc(hdr->frame_control) &&
  1867. sdata->u.mgd.probe_send_count > 0) {
  1868. if (ack)
  1869. ieee80211_sta_reset_conn_monitor(sdata);
  1870. else
  1871. sdata->u.mgd.nullfunc_failed = true;
  1872. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  1873. return;
  1874. }
  1875. if (ack)
  1876. ieee80211_sta_reset_conn_monitor(sdata);
  1877. }
  1878. static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
  1879. {
  1880. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1881. const u8 *ssid;
  1882. u8 *dst = ifmgd->associated->bssid;
  1883. u8 unicast_limit = max(1, max_probe_tries - 3);
  1884. struct sta_info *sta;
  1885. /*
  1886. * Try sending broadcast probe requests for the last three
  1887. * probe requests after the first ones failed since some
  1888. * buggy APs only support broadcast probe requests.
  1889. */
  1890. if (ifmgd->probe_send_count >= unicast_limit)
  1891. dst = NULL;
  1892. /*
  1893. * When the hardware reports an accurate Tx ACK status, it's
  1894. * better to send a nullfunc frame instead of a probe request,
  1895. * as it will kick us off the AP quickly if we aren't associated
  1896. * anymore. The timeout will be reset if the frame is ACKed by
  1897. * the AP.
  1898. */
  1899. ifmgd->probe_send_count++;
  1900. if (dst) {
  1901. mutex_lock(&sdata->local->sta_mtx);
  1902. sta = sta_info_get(sdata, dst);
  1903. if (!WARN_ON(!sta))
  1904. ieee80211_check_fast_rx(sta);
  1905. mutex_unlock(&sdata->local->sta_mtx);
  1906. }
  1907. if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) {
  1908. ifmgd->nullfunc_failed = false;
  1909. ieee80211_send_nullfunc(sdata->local, sdata, false);
  1910. } else {
  1911. int ssid_len;
  1912. rcu_read_lock();
  1913. ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
  1914. if (WARN_ON_ONCE(ssid == NULL))
  1915. ssid_len = 0;
  1916. else
  1917. ssid_len = ssid[1];
  1918. ieee80211_send_probe_req(sdata, sdata->vif.addr, dst,
  1919. ssid + 2, ssid_len, NULL,
  1920. 0, (u32) -1, true, 0,
  1921. ifmgd->associated->channel, false);
  1922. rcu_read_unlock();
  1923. }
  1924. ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
  1925. run_again(sdata, ifmgd->probe_timeout);
  1926. }
  1927. static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
  1928. bool beacon)
  1929. {
  1930. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1931. bool already = false;
  1932. if (!ieee80211_sdata_running(sdata))
  1933. return;
  1934. sdata_lock(sdata);
  1935. if (!ifmgd->associated)
  1936. goto out;
  1937. mutex_lock(&sdata->local->mtx);
  1938. if (sdata->local->tmp_channel || sdata->local->scanning) {
  1939. mutex_unlock(&sdata->local->mtx);
  1940. goto out;
  1941. }
  1942. if (beacon) {
  1943. mlme_dbg_ratelimited(sdata,
  1944. "detected beacon loss from AP (missed %d beacons) - probing\n",
  1945. beacon_loss_count);
  1946. ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL);
  1947. }
  1948. /*
  1949. * The driver/our work has already reported this event or the
  1950. * connection monitoring has kicked in and we have already sent
  1951. * a probe request. Or maybe the AP died and the driver keeps
  1952. * reporting until we disassociate...
  1953. *
  1954. * In either case we have to ignore the current call to this
  1955. * function (except for setting the correct probe reason bit)
  1956. * because otherwise we would reset the timer every time and
  1957. * never check whether we received a probe response!
  1958. */
  1959. if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
  1960. already = true;
  1961. ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
  1962. mutex_unlock(&sdata->local->mtx);
  1963. if (already)
  1964. goto out;
  1965. mutex_lock(&sdata->local->iflist_mtx);
  1966. ieee80211_recalc_ps(sdata->local);
  1967. mutex_unlock(&sdata->local->iflist_mtx);
  1968. ifmgd->probe_send_count = 0;
  1969. ieee80211_mgd_probe_ap_send(sdata);
  1970. out:
  1971. sdata_unlock(sdata);
  1972. }
  1973. struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
  1974. struct ieee80211_vif *vif)
  1975. {
  1976. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1977. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1978. struct cfg80211_bss *cbss;
  1979. struct sk_buff *skb;
  1980. const u8 *ssid;
  1981. int ssid_len;
  1982. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1983. return NULL;
  1984. sdata_assert_lock(sdata);
  1985. if (ifmgd->associated)
  1986. cbss = ifmgd->associated;
  1987. else if (ifmgd->auth_data)
  1988. cbss = ifmgd->auth_data->bss;
  1989. else if (ifmgd->assoc_data)
  1990. cbss = ifmgd->assoc_data->bss;
  1991. else
  1992. return NULL;
  1993. rcu_read_lock();
  1994. ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
  1995. if (WARN_ON_ONCE(ssid == NULL))
  1996. ssid_len = 0;
  1997. else
  1998. ssid_len = ssid[1];
  1999. skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid,
  2000. (u32) -1, cbss->channel,
  2001. ssid + 2, ssid_len,
  2002. NULL, 0, true);
  2003. rcu_read_unlock();
  2004. return skb;
  2005. }
  2006. EXPORT_SYMBOL(ieee80211_ap_probereq_get);
  2007. static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
  2008. const u8 *buf, size_t len, bool tx,
  2009. u16 reason)
  2010. {
  2011. struct ieee80211_event event = {
  2012. .type = MLME_EVENT,
  2013. .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
  2014. .u.mlme.reason = reason,
  2015. };
  2016. if (tx)
  2017. cfg80211_tx_mlme_mgmt(sdata->dev, buf, len);
  2018. else
  2019. cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
  2020. drv_event_callback(sdata->local, sdata, &event);
  2021. }
  2022. static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
  2023. {
  2024. struct ieee80211_local *local = sdata->local;
  2025. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2026. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  2027. sdata_lock(sdata);
  2028. if (!ifmgd->associated) {
  2029. sdata_unlock(sdata);
  2030. return;
  2031. }
  2032. /* AP is probably out of range (or not reachable for another reason) so
  2033. * remove the bss struct for that AP.
  2034. */
  2035. cfg80211_unlink_bss(local->hw.wiphy, ifmgd->associated);
  2036. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  2037. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  2038. true, frame_buf);
  2039. mutex_lock(&local->mtx);
  2040. sdata->vif.csa_active = false;
  2041. ifmgd->csa_waiting_bcn = false;
  2042. if (sdata->csa_block_tx) {
  2043. ieee80211_wake_vif_queues(local, sdata,
  2044. IEEE80211_QUEUE_STOP_REASON_CSA);
  2045. sdata->csa_block_tx = false;
  2046. }
  2047. mutex_unlock(&local->mtx);
  2048. ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
  2049. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  2050. sdata_unlock(sdata);
  2051. }
  2052. static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
  2053. {
  2054. struct ieee80211_sub_if_data *sdata =
  2055. container_of(work, struct ieee80211_sub_if_data,
  2056. u.mgd.beacon_connection_loss_work);
  2057. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2058. if (ifmgd->associated)
  2059. ifmgd->beacon_loss_count++;
  2060. if (ifmgd->connection_loss) {
  2061. sdata_info(sdata, "Connection to AP %pM lost\n",
  2062. ifmgd->bssid);
  2063. __ieee80211_disconnect(sdata);
  2064. } else {
  2065. ieee80211_mgd_probe_ap(sdata, true);
  2066. }
  2067. }
  2068. static void ieee80211_csa_connection_drop_work(struct work_struct *work)
  2069. {
  2070. struct ieee80211_sub_if_data *sdata =
  2071. container_of(work, struct ieee80211_sub_if_data,
  2072. u.mgd.csa_connection_drop_work);
  2073. __ieee80211_disconnect(sdata);
  2074. }
  2075. void ieee80211_beacon_loss(struct ieee80211_vif *vif)
  2076. {
  2077. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2078. struct ieee80211_hw *hw = &sdata->local->hw;
  2079. trace_api_beacon_loss(sdata);
  2080. sdata->u.mgd.connection_loss = false;
  2081. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  2082. }
  2083. EXPORT_SYMBOL(ieee80211_beacon_loss);
  2084. void ieee80211_connection_loss(struct ieee80211_vif *vif)
  2085. {
  2086. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2087. struct ieee80211_hw *hw = &sdata->local->hw;
  2088. trace_api_connection_loss(sdata);
  2089. sdata->u.mgd.connection_loss = true;
  2090. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  2091. }
  2092. EXPORT_SYMBOL(ieee80211_connection_loss);
  2093. static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
  2094. bool assoc)
  2095. {
  2096. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  2097. sdata_assert_lock(sdata);
  2098. if (!assoc) {
  2099. /*
  2100. * we are not authenticated yet, the only timer that could be
  2101. * running is the timeout for the authentication response which
  2102. * which is not relevant anymore.
  2103. */
  2104. del_timer_sync(&sdata->u.mgd.timer);
  2105. sta_info_destroy_addr(sdata, auth_data->bss->bssid);
  2106. eth_zero_addr(sdata->u.mgd.bssid);
  2107. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  2108. sdata->u.mgd.flags = 0;
  2109. mutex_lock(&sdata->local->mtx);
  2110. ieee80211_vif_release_channel(sdata);
  2111. mutex_unlock(&sdata->local->mtx);
  2112. }
  2113. cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
  2114. kfree(auth_data);
  2115. sdata->u.mgd.auth_data = NULL;
  2116. }
  2117. static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
  2118. bool assoc, bool abandon)
  2119. {
  2120. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  2121. sdata_assert_lock(sdata);
  2122. if (!assoc) {
  2123. /*
  2124. * we are not associated yet, the only timer that could be
  2125. * running is the timeout for the association response which
  2126. * which is not relevant anymore.
  2127. */
  2128. del_timer_sync(&sdata->u.mgd.timer);
  2129. sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
  2130. eth_zero_addr(sdata->u.mgd.bssid);
  2131. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  2132. sdata->u.mgd.flags = 0;
  2133. sdata->vif.mu_mimo_owner = false;
  2134. mutex_lock(&sdata->local->mtx);
  2135. ieee80211_vif_release_channel(sdata);
  2136. mutex_unlock(&sdata->local->mtx);
  2137. if (abandon)
  2138. cfg80211_abandon_assoc(sdata->dev, assoc_data->bss);
  2139. }
  2140. kfree(assoc_data);
  2141. sdata->u.mgd.assoc_data = NULL;
  2142. }
  2143. static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
  2144. struct ieee80211_mgmt *mgmt, size_t len)
  2145. {
  2146. struct ieee80211_local *local = sdata->local;
  2147. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  2148. u8 *pos;
  2149. struct ieee802_11_elems elems;
  2150. u32 tx_flags = 0;
  2151. pos = mgmt->u.auth.variable;
  2152. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
  2153. if (!elems.challenge)
  2154. return;
  2155. auth_data->expected_transaction = 4;
  2156. drv_mgd_prepare_tx(sdata->local, sdata);
  2157. if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
  2158. tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
  2159. IEEE80211_TX_INTFL_MLME_CONN_TX;
  2160. ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
  2161. elems.challenge - 2, elems.challenge_len + 2,
  2162. auth_data->bss->bssid, auth_data->bss->bssid,
  2163. auth_data->key, auth_data->key_len,
  2164. auth_data->key_idx, tx_flags);
  2165. }
  2166. static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
  2167. struct ieee80211_mgmt *mgmt, size_t len)
  2168. {
  2169. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2170. u8 bssid[ETH_ALEN];
  2171. u16 auth_alg, auth_transaction, status_code;
  2172. struct sta_info *sta;
  2173. struct ieee80211_event event = {
  2174. .type = MLME_EVENT,
  2175. .u.mlme.data = AUTH_EVENT,
  2176. };
  2177. sdata_assert_lock(sdata);
  2178. if (len < 24 + 6)
  2179. return;
  2180. if (!ifmgd->auth_data || ifmgd->auth_data->done)
  2181. return;
  2182. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  2183. if (!ether_addr_equal(bssid, mgmt->bssid))
  2184. return;
  2185. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  2186. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  2187. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  2188. if (auth_alg != ifmgd->auth_data->algorithm ||
  2189. auth_transaction != ifmgd->auth_data->expected_transaction) {
  2190. sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
  2191. mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
  2192. auth_transaction,
  2193. ifmgd->auth_data->expected_transaction);
  2194. return;
  2195. }
  2196. if (status_code != WLAN_STATUS_SUCCESS) {
  2197. sdata_info(sdata, "%pM denied authentication (status %d)\n",
  2198. mgmt->sa, status_code);
  2199. ieee80211_destroy_auth_data(sdata, false);
  2200. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  2201. event.u.mlme.status = MLME_DENIED;
  2202. event.u.mlme.reason = status_code;
  2203. drv_event_callback(sdata->local, sdata, &event);
  2204. return;
  2205. }
  2206. switch (ifmgd->auth_data->algorithm) {
  2207. case WLAN_AUTH_OPEN:
  2208. case WLAN_AUTH_LEAP:
  2209. case WLAN_AUTH_FT:
  2210. case WLAN_AUTH_SAE:
  2211. case WLAN_AUTH_FILS_SK:
  2212. case WLAN_AUTH_FILS_SK_PFS:
  2213. case WLAN_AUTH_FILS_PK:
  2214. break;
  2215. case WLAN_AUTH_SHARED_KEY:
  2216. if (ifmgd->auth_data->expected_transaction != 4) {
  2217. ieee80211_auth_challenge(sdata, mgmt, len);
  2218. /* need another frame */
  2219. return;
  2220. }
  2221. break;
  2222. default:
  2223. WARN_ONCE(1, "invalid auth alg %d",
  2224. ifmgd->auth_data->algorithm);
  2225. return;
  2226. }
  2227. event.u.mlme.status = MLME_SUCCESS;
  2228. drv_event_callback(sdata->local, sdata, &event);
  2229. sdata_info(sdata, "authenticated\n");
  2230. ifmgd->auth_data->done = true;
  2231. ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
  2232. ifmgd->auth_data->timeout_started = true;
  2233. run_again(sdata, ifmgd->auth_data->timeout);
  2234. if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
  2235. ifmgd->auth_data->expected_transaction != 2) {
  2236. /*
  2237. * Report auth frame to user space for processing since another
  2238. * round of Authentication frames is still needed.
  2239. */
  2240. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  2241. return;
  2242. }
  2243. /* move station state to auth */
  2244. mutex_lock(&sdata->local->sta_mtx);
  2245. sta = sta_info_get(sdata, bssid);
  2246. if (!sta) {
  2247. WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
  2248. goto out_err;
  2249. }
  2250. if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
  2251. sdata_info(sdata, "failed moving %pM to auth\n", bssid);
  2252. goto out_err;
  2253. }
  2254. mutex_unlock(&sdata->local->sta_mtx);
  2255. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  2256. return;
  2257. out_err:
  2258. mutex_unlock(&sdata->local->sta_mtx);
  2259. /* ignore frame -- wait for timeout */
  2260. }
  2261. #define case_WLAN(type) \
  2262. case WLAN_REASON_##type: return #type
  2263. static const char *ieee80211_get_reason_code_string(u16 reason_code)
  2264. {
  2265. switch (reason_code) {
  2266. case_WLAN(UNSPECIFIED);
  2267. case_WLAN(PREV_AUTH_NOT_VALID);
  2268. case_WLAN(DEAUTH_LEAVING);
  2269. case_WLAN(DISASSOC_DUE_TO_INACTIVITY);
  2270. case_WLAN(DISASSOC_AP_BUSY);
  2271. case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA);
  2272. case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA);
  2273. case_WLAN(DISASSOC_STA_HAS_LEFT);
  2274. case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH);
  2275. case_WLAN(DISASSOC_BAD_POWER);
  2276. case_WLAN(DISASSOC_BAD_SUPP_CHAN);
  2277. case_WLAN(INVALID_IE);
  2278. case_WLAN(MIC_FAILURE);
  2279. case_WLAN(4WAY_HANDSHAKE_TIMEOUT);
  2280. case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT);
  2281. case_WLAN(IE_DIFFERENT);
  2282. case_WLAN(INVALID_GROUP_CIPHER);
  2283. case_WLAN(INVALID_PAIRWISE_CIPHER);
  2284. case_WLAN(INVALID_AKMP);
  2285. case_WLAN(UNSUPP_RSN_VERSION);
  2286. case_WLAN(INVALID_RSN_IE_CAP);
  2287. case_WLAN(IEEE8021X_FAILED);
  2288. case_WLAN(CIPHER_SUITE_REJECTED);
  2289. case_WLAN(DISASSOC_UNSPECIFIED_QOS);
  2290. case_WLAN(DISASSOC_QAP_NO_BANDWIDTH);
  2291. case_WLAN(DISASSOC_LOW_ACK);
  2292. case_WLAN(DISASSOC_QAP_EXCEED_TXOP);
  2293. case_WLAN(QSTA_LEAVE_QBSS);
  2294. case_WLAN(QSTA_NOT_USE);
  2295. case_WLAN(QSTA_REQUIRE_SETUP);
  2296. case_WLAN(QSTA_TIMEOUT);
  2297. case_WLAN(QSTA_CIPHER_NOT_SUPP);
  2298. case_WLAN(MESH_PEER_CANCELED);
  2299. case_WLAN(MESH_MAX_PEERS);
  2300. case_WLAN(MESH_CONFIG);
  2301. case_WLAN(MESH_CLOSE);
  2302. case_WLAN(MESH_MAX_RETRIES);
  2303. case_WLAN(MESH_CONFIRM_TIMEOUT);
  2304. case_WLAN(MESH_INVALID_GTK);
  2305. case_WLAN(MESH_INCONSISTENT_PARAM);
  2306. case_WLAN(MESH_INVALID_SECURITY);
  2307. case_WLAN(MESH_PATH_ERROR);
  2308. case_WLAN(MESH_PATH_NOFORWARD);
  2309. case_WLAN(MESH_PATH_DEST_UNREACHABLE);
  2310. case_WLAN(MAC_EXISTS_IN_MBSS);
  2311. case_WLAN(MESH_CHAN_REGULATORY);
  2312. case_WLAN(MESH_CHAN);
  2313. default: return "<unknown>";
  2314. }
  2315. }
  2316. static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  2317. struct ieee80211_mgmt *mgmt, size_t len)
  2318. {
  2319. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2320. u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  2321. sdata_assert_lock(sdata);
  2322. if (len < 24 + 2)
  2323. return;
  2324. if (ifmgd->associated &&
  2325. ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid)) {
  2326. const u8 *bssid = ifmgd->associated->bssid;
  2327. sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n",
  2328. bssid, reason_code,
  2329. ieee80211_get_reason_code_string(reason_code));
  2330. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  2331. ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false,
  2332. reason_code);
  2333. return;
  2334. }
  2335. if (ifmgd->assoc_data &&
  2336. ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
  2337. const u8 *bssid = ifmgd->assoc_data->bss->bssid;
  2338. sdata_info(sdata,
  2339. "deauthenticated from %pM while associating (Reason: %u=%s)\n",
  2340. bssid, reason_code,
  2341. ieee80211_get_reason_code_string(reason_code));
  2342. ieee80211_destroy_assoc_data(sdata, false, true);
  2343. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  2344. return;
  2345. }
  2346. }
  2347. static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  2348. struct ieee80211_mgmt *mgmt, size_t len)
  2349. {
  2350. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2351. u16 reason_code;
  2352. sdata_assert_lock(sdata);
  2353. if (len < 24 + 2)
  2354. return;
  2355. if (!ifmgd->associated ||
  2356. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2357. return;
  2358. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  2359. sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n",
  2360. mgmt->sa, reason_code,
  2361. ieee80211_get_reason_code_string(reason_code));
  2362. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  2363. ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
  2364. }
  2365. static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
  2366. u8 *supp_rates, unsigned int supp_rates_len,
  2367. u32 *rates, u32 *basic_rates,
  2368. bool *have_higher_than_11mbit,
  2369. int *min_rate, int *min_rate_index,
  2370. int shift, u32 rate_flags)
  2371. {
  2372. int i, j;
  2373. for (i = 0; i < supp_rates_len; i++) {
  2374. int rate = supp_rates[i] & 0x7f;
  2375. bool is_basic = !!(supp_rates[i] & 0x80);
  2376. if ((rate * 5 * (1 << shift)) > 110)
  2377. *have_higher_than_11mbit = true;
  2378. /*
  2379. * Skip HT and VHT BSS membership selectors since they're not
  2380. * rates.
  2381. *
  2382. * Note: Even though the membership selector and the basic
  2383. * rate flag share the same bit, they are not exactly
  2384. * the same.
  2385. */
  2386. if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) ||
  2387. supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY))
  2388. continue;
  2389. for (j = 0; j < sband->n_bitrates; j++) {
  2390. struct ieee80211_rate *br;
  2391. int brate;
  2392. br = &sband->bitrates[j];
  2393. if ((rate_flags & br->flags) != rate_flags)
  2394. continue;
  2395. brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
  2396. if (brate == rate) {
  2397. *rates |= BIT(j);
  2398. if (is_basic)
  2399. *basic_rates |= BIT(j);
  2400. if ((rate * 5) < *min_rate) {
  2401. *min_rate = rate * 5;
  2402. *min_rate_index = j;
  2403. }
  2404. break;
  2405. }
  2406. }
  2407. }
  2408. }
  2409. static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
  2410. struct cfg80211_bss *cbss,
  2411. struct ieee80211_mgmt *mgmt, size_t len)
  2412. {
  2413. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2414. struct ieee80211_local *local = sdata->local;
  2415. struct ieee80211_supported_band *sband;
  2416. struct sta_info *sta;
  2417. u8 *pos;
  2418. u16 capab_info, aid;
  2419. struct ieee802_11_elems elems;
  2420. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  2421. const struct cfg80211_bss_ies *bss_ies = NULL;
  2422. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  2423. u32 changed = 0;
  2424. int err;
  2425. bool ret;
  2426. /* AssocResp and ReassocResp have identical structure */
  2427. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  2428. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  2429. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  2430. sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
  2431. aid);
  2432. aid &= ~(BIT(15) | BIT(14));
  2433. ifmgd->broken_ap = false;
  2434. if (aid == 0 || aid > IEEE80211_MAX_AID) {
  2435. sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
  2436. aid);
  2437. aid = 0;
  2438. ifmgd->broken_ap = true;
  2439. }
  2440. pos = mgmt->u.assoc_resp.variable;
  2441. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
  2442. if (!elems.supp_rates) {
  2443. sdata_info(sdata, "no SuppRates element in AssocResp\n");
  2444. return false;
  2445. }
  2446. ifmgd->aid = aid;
  2447. ifmgd->tdls_chan_switch_prohibited =
  2448. elems.ext_capab && elems.ext_capab_len >= 5 &&
  2449. (elems.ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED);
  2450. /*
  2451. * Some APs are erroneously not including some information in their
  2452. * (re)association response frames. Try to recover by using the data
  2453. * from the beacon or probe response. This seems to afflict mobile
  2454. * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
  2455. * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
  2456. */
  2457. if ((assoc_data->wmm && !elems.wmm_param) ||
  2458. (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  2459. (!elems.ht_cap_elem || !elems.ht_operation)) ||
  2460. (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  2461. (!elems.vht_cap_elem || !elems.vht_operation))) {
  2462. const struct cfg80211_bss_ies *ies;
  2463. struct ieee802_11_elems bss_elems;
  2464. rcu_read_lock();
  2465. ies = rcu_dereference(cbss->ies);
  2466. if (ies)
  2467. bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
  2468. GFP_ATOMIC);
  2469. rcu_read_unlock();
  2470. if (!bss_ies)
  2471. return false;
  2472. ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
  2473. false, &bss_elems);
  2474. if (assoc_data->wmm &&
  2475. !elems.wmm_param && bss_elems.wmm_param) {
  2476. elems.wmm_param = bss_elems.wmm_param;
  2477. sdata_info(sdata,
  2478. "AP bug: WMM param missing from AssocResp\n");
  2479. }
  2480. /*
  2481. * Also check if we requested HT/VHT, otherwise the AP doesn't
  2482. * have to include the IEs in the (re)association response.
  2483. */
  2484. if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
  2485. !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
  2486. elems.ht_cap_elem = bss_elems.ht_cap_elem;
  2487. sdata_info(sdata,
  2488. "AP bug: HT capability missing from AssocResp\n");
  2489. }
  2490. if (!elems.ht_operation && bss_elems.ht_operation &&
  2491. !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
  2492. elems.ht_operation = bss_elems.ht_operation;
  2493. sdata_info(sdata,
  2494. "AP bug: HT operation missing from AssocResp\n");
  2495. }
  2496. if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
  2497. !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
  2498. elems.vht_cap_elem = bss_elems.vht_cap_elem;
  2499. sdata_info(sdata,
  2500. "AP bug: VHT capa missing from AssocResp\n");
  2501. }
  2502. if (!elems.vht_operation && bss_elems.vht_operation &&
  2503. !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
  2504. elems.vht_operation = bss_elems.vht_operation;
  2505. sdata_info(sdata,
  2506. "AP bug: VHT operation missing from AssocResp\n");
  2507. }
  2508. }
  2509. /*
  2510. * We previously checked these in the beacon/probe response, so
  2511. * they should be present here. This is just a safety net.
  2512. */
  2513. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  2514. (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
  2515. sdata_info(sdata,
  2516. "HT AP is missing WMM params or HT capability/operation\n");
  2517. ret = false;
  2518. goto out;
  2519. }
  2520. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  2521. (!elems.vht_cap_elem || !elems.vht_operation)) {
  2522. sdata_info(sdata,
  2523. "VHT AP is missing VHT capability/operation\n");
  2524. ret = false;
  2525. goto out;
  2526. }
  2527. mutex_lock(&sdata->local->sta_mtx);
  2528. /*
  2529. * station info was already allocated and inserted before
  2530. * the association and should be available to us
  2531. */
  2532. sta = sta_info_get(sdata, cbss->bssid);
  2533. if (WARN_ON(!sta)) {
  2534. mutex_unlock(&sdata->local->sta_mtx);
  2535. ret = false;
  2536. goto out;
  2537. }
  2538. sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
  2539. /* Set up internal HT/VHT capabilities */
  2540. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
  2541. ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  2542. elems.ht_cap_elem, sta);
  2543. if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  2544. ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
  2545. elems.vht_cap_elem, sta);
  2546. /*
  2547. * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
  2548. * in their association response, so ignore that data for our own
  2549. * configuration. If it changed since the last beacon, we'll get the
  2550. * next beacon and update then.
  2551. */
  2552. /*
  2553. * If an operating mode notification IE is present, override the
  2554. * NSS calculation (that would be done in rate_control_rate_init())
  2555. * and use the # of streams from that element.
  2556. */
  2557. if (elems.opmode_notif &&
  2558. !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
  2559. u8 nss;
  2560. nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
  2561. nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
  2562. nss += 1;
  2563. sta->sta.rx_nss = nss;
  2564. }
  2565. rate_control_rate_init(sta);
  2566. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
  2567. set_sta_flag(sta, WLAN_STA_MFP);
  2568. sta->sta.mfp = true;
  2569. } else {
  2570. sta->sta.mfp = false;
  2571. }
  2572. sta->sta.wme = elems.wmm_param && local->hw.queues >= IEEE80211_NUM_ACS;
  2573. err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
  2574. if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
  2575. err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
  2576. if (err) {
  2577. sdata_info(sdata,
  2578. "failed to move station %pM to desired state\n",
  2579. sta->sta.addr);
  2580. WARN_ON(__sta_info_destroy(sta));
  2581. mutex_unlock(&sdata->local->sta_mtx);
  2582. ret = false;
  2583. goto out;
  2584. }
  2585. mutex_unlock(&sdata->local->sta_mtx);
  2586. /*
  2587. * Always handle WMM once after association regardless
  2588. * of the first value the AP uses. Setting -1 here has
  2589. * that effect because the AP values is an unsigned
  2590. * 4-bit value.
  2591. */
  2592. ifmgd->wmm_last_param_set = -1;
  2593. if (ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
  2594. ieee80211_set_wmm_default(sdata, false, false);
  2595. } else if (!ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  2596. elems.wmm_param_len)) {
  2597. /* still enable QoS since we might have HT/VHT */
  2598. ieee80211_set_wmm_default(sdata, false, true);
  2599. /* set the disable-WMM flag in this case to disable
  2600. * tracking WMM parameter changes in the beacon if
  2601. * the parameters weren't actually valid. Doing so
  2602. * avoids changing parameters very strangely when
  2603. * the AP is going back and forth between valid and
  2604. * invalid parameters.
  2605. */
  2606. ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
  2607. }
  2608. changed |= BSS_CHANGED_QOS;
  2609. /* set AID and assoc capability,
  2610. * ieee80211_set_associated() will tell the driver */
  2611. bss_conf->aid = aid;
  2612. bss_conf->assoc_capability = capab_info;
  2613. ieee80211_set_associated(sdata, cbss, changed);
  2614. /*
  2615. * If we're using 4-addr mode, let the AP know that we're
  2616. * doing so, so that it can create the STA VLAN on its side
  2617. */
  2618. if (ifmgd->use_4addr)
  2619. ieee80211_send_4addr_nullfunc(local, sdata);
  2620. /*
  2621. * Start timer to probe the connection to the AP now.
  2622. * Also start the timer that will detect beacon loss.
  2623. */
  2624. ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
  2625. ieee80211_sta_reset_beacon_monitor(sdata);
  2626. ret = true;
  2627. out:
  2628. kfree(bss_ies);
  2629. return ret;
  2630. }
  2631. static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  2632. struct ieee80211_mgmt *mgmt,
  2633. size_t len)
  2634. {
  2635. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2636. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  2637. u16 capab_info, status_code, aid;
  2638. struct ieee802_11_elems elems;
  2639. int ac, uapsd_queues = -1;
  2640. u8 *pos;
  2641. bool reassoc;
  2642. struct cfg80211_bss *bss;
  2643. struct ieee80211_event event = {
  2644. .type = MLME_EVENT,
  2645. .u.mlme.data = ASSOC_EVENT,
  2646. };
  2647. sdata_assert_lock(sdata);
  2648. if (!assoc_data)
  2649. return;
  2650. if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
  2651. return;
  2652. /*
  2653. * AssocResp and ReassocResp have identical structure, so process both
  2654. * of them in this function.
  2655. */
  2656. if (len < 24 + 6)
  2657. return;
  2658. reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
  2659. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  2660. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  2661. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  2662. sdata_info(sdata,
  2663. "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
  2664. reassoc ? "Rea" : "A", mgmt->sa,
  2665. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  2666. if (assoc_data->fils_kek_len &&
  2667. fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0)
  2668. return;
  2669. pos = mgmt->u.assoc_resp.variable;
  2670. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
  2671. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  2672. elems.timeout_int &&
  2673. elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  2674. u32 tu, ms;
  2675. tu = le32_to_cpu(elems.timeout_int->value);
  2676. ms = tu * 1024 / 1000;
  2677. sdata_info(sdata,
  2678. "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
  2679. mgmt->sa, tu, ms);
  2680. assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
  2681. assoc_data->timeout_started = true;
  2682. if (ms > IEEE80211_ASSOC_TIMEOUT)
  2683. run_again(sdata, assoc_data->timeout);
  2684. return;
  2685. }
  2686. bss = assoc_data->bss;
  2687. if (status_code != WLAN_STATUS_SUCCESS) {
  2688. sdata_info(sdata, "%pM denied association (code=%d)\n",
  2689. mgmt->sa, status_code);
  2690. ieee80211_destroy_assoc_data(sdata, false, false);
  2691. event.u.mlme.status = MLME_DENIED;
  2692. event.u.mlme.reason = status_code;
  2693. drv_event_callback(sdata->local, sdata, &event);
  2694. } else {
  2695. if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) {
  2696. /* oops -- internal error -- send timeout for now */
  2697. ieee80211_destroy_assoc_data(sdata, false, false);
  2698. cfg80211_assoc_timeout(sdata->dev, bss);
  2699. return;
  2700. }
  2701. event.u.mlme.status = MLME_SUCCESS;
  2702. drv_event_callback(sdata->local, sdata, &event);
  2703. sdata_info(sdata, "associated\n");
  2704. /*
  2705. * destroy assoc_data afterwards, as otherwise an idle
  2706. * recalc after assoc_data is NULL but before associated
  2707. * is set can cause the interface to go idle
  2708. */
  2709. ieee80211_destroy_assoc_data(sdata, true, false);
  2710. /* get uapsd queues configuration */
  2711. uapsd_queues = 0;
  2712. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  2713. if (sdata->tx_conf[ac].uapsd)
  2714. uapsd_queues |= ieee80211_ac_to_qos_mask[ac];
  2715. }
  2716. cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len, uapsd_queues);
  2717. }
  2718. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  2719. struct ieee80211_mgmt *mgmt, size_t len,
  2720. struct ieee80211_rx_status *rx_status,
  2721. struct ieee802_11_elems *elems)
  2722. {
  2723. struct ieee80211_local *local = sdata->local;
  2724. struct ieee80211_bss *bss;
  2725. struct ieee80211_channel *channel;
  2726. sdata_assert_lock(sdata);
  2727. channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
  2728. if (!channel)
  2729. return;
  2730. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  2731. channel);
  2732. if (bss) {
  2733. sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
  2734. ieee80211_rx_bss_put(local, bss);
  2735. }
  2736. }
  2737. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  2738. struct sk_buff *skb)
  2739. {
  2740. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  2741. struct ieee80211_if_managed *ifmgd;
  2742. struct ieee80211_rx_status *rx_status = (void *) skb->cb;
  2743. size_t baselen, len = skb->len;
  2744. struct ieee802_11_elems elems;
  2745. ifmgd = &sdata->u.mgd;
  2746. sdata_assert_lock(sdata);
  2747. if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
  2748. return; /* ignore ProbeResp to foreign address */
  2749. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  2750. if (baselen > len)
  2751. return;
  2752. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  2753. false, &elems);
  2754. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2755. if (ifmgd->associated &&
  2756. ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2757. ieee80211_reset_ap_probe(sdata);
  2758. }
  2759. /*
  2760. * This is the canonical list of information elements we care about,
  2761. * the filter code also gives us all changes to the Microsoft OUI
  2762. * (00:50:F2) vendor IE which is used for WMM which we need to track,
  2763. * as well as the DTPC IE (part of the Cisco OUI) used for signaling
  2764. * changes to requested client power.
  2765. *
  2766. * We implement beacon filtering in software since that means we can
  2767. * avoid processing the frame here and in cfg80211, and userspace
  2768. * will not be able to tell whether the hardware supports it or not.
  2769. *
  2770. * XXX: This list needs to be dynamic -- userspace needs to be able to
  2771. * add items it requires. It also needs to be able to tell us to
  2772. * look out for other vendor IEs.
  2773. */
  2774. static const u64 care_about_ies =
  2775. (1ULL << WLAN_EID_COUNTRY) |
  2776. (1ULL << WLAN_EID_ERP_INFO) |
  2777. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  2778. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  2779. (1ULL << WLAN_EID_HT_CAPABILITY) |
  2780. (1ULL << WLAN_EID_HT_OPERATION) |
  2781. (1ULL << WLAN_EID_EXT_CHANSWITCH_ANN);
  2782. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  2783. struct ieee80211_mgmt *mgmt, size_t len,
  2784. struct ieee80211_rx_status *rx_status)
  2785. {
  2786. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2787. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  2788. size_t baselen;
  2789. struct ieee802_11_elems elems;
  2790. struct ieee80211_local *local = sdata->local;
  2791. struct ieee80211_chanctx_conf *chanctx_conf;
  2792. struct ieee80211_channel *chan;
  2793. struct sta_info *sta;
  2794. u32 changed = 0;
  2795. bool erp_valid;
  2796. u8 erp_value = 0;
  2797. u32 ncrc;
  2798. u8 *bssid;
  2799. u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
  2800. sdata_assert_lock(sdata);
  2801. /* Process beacon from the current BSS */
  2802. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  2803. if (baselen > len)
  2804. return;
  2805. rcu_read_lock();
  2806. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  2807. if (!chanctx_conf) {
  2808. rcu_read_unlock();
  2809. return;
  2810. }
  2811. if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
  2812. rcu_read_unlock();
  2813. return;
  2814. }
  2815. chan = chanctx_conf->def.chan;
  2816. rcu_read_unlock();
  2817. if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
  2818. ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
  2819. ieee802_11_parse_elems(mgmt->u.beacon.variable,
  2820. len - baselen, false, &elems);
  2821. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2822. if (elems.tim && !elems.parse_error) {
  2823. const struct ieee80211_tim_ie *tim_ie = elems.tim;
  2824. ifmgd->dtim_period = tim_ie->dtim_period;
  2825. }
  2826. ifmgd->have_beacon = true;
  2827. ifmgd->assoc_data->need_beacon = false;
  2828. if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
  2829. sdata->vif.bss_conf.sync_tsf =
  2830. le64_to_cpu(mgmt->u.beacon.timestamp);
  2831. sdata->vif.bss_conf.sync_device_ts =
  2832. rx_status->device_timestamp;
  2833. if (elems.tim)
  2834. sdata->vif.bss_conf.sync_dtim_count =
  2835. elems.tim->dtim_count;
  2836. else
  2837. sdata->vif.bss_conf.sync_dtim_count = 0;
  2838. }
  2839. /* continue assoc process */
  2840. ifmgd->assoc_data->timeout = jiffies;
  2841. ifmgd->assoc_data->timeout_started = true;
  2842. run_again(sdata, ifmgd->assoc_data->timeout);
  2843. return;
  2844. }
  2845. if (!ifmgd->associated ||
  2846. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2847. return;
  2848. bssid = ifmgd->associated->bssid;
  2849. /* Track average RSSI from the Beacon frames of the current AP */
  2850. if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
  2851. ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
  2852. ewma_beacon_signal_init(&ifmgd->ave_beacon_signal);
  2853. ifmgd->last_cqm_event_signal = 0;
  2854. ifmgd->count_beacon_signal = 1;
  2855. ifmgd->last_ave_beacon_signal = 0;
  2856. } else {
  2857. ifmgd->count_beacon_signal++;
  2858. }
  2859. ewma_beacon_signal_add(&ifmgd->ave_beacon_signal, -rx_status->signal);
  2860. if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
  2861. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
  2862. int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
  2863. int last_sig = ifmgd->last_ave_beacon_signal;
  2864. struct ieee80211_event event = {
  2865. .type = RSSI_EVENT,
  2866. };
  2867. /*
  2868. * if signal crosses either of the boundaries, invoke callback
  2869. * with appropriate parameters
  2870. */
  2871. if (sig > ifmgd->rssi_max_thold &&
  2872. (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
  2873. ifmgd->last_ave_beacon_signal = sig;
  2874. event.u.rssi.data = RSSI_EVENT_HIGH;
  2875. drv_event_callback(local, sdata, &event);
  2876. } else if (sig < ifmgd->rssi_min_thold &&
  2877. (last_sig >= ifmgd->rssi_max_thold ||
  2878. last_sig == 0)) {
  2879. ifmgd->last_ave_beacon_signal = sig;
  2880. event.u.rssi.data = RSSI_EVENT_LOW;
  2881. drv_event_callback(local, sdata, &event);
  2882. }
  2883. }
  2884. if (bss_conf->cqm_rssi_thold &&
  2885. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
  2886. !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
  2887. int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
  2888. int last_event = ifmgd->last_cqm_event_signal;
  2889. int thold = bss_conf->cqm_rssi_thold;
  2890. int hyst = bss_conf->cqm_rssi_hyst;
  2891. if (sig < thold &&
  2892. (last_event == 0 || sig < last_event - hyst)) {
  2893. ifmgd->last_cqm_event_signal = sig;
  2894. ieee80211_cqm_rssi_notify(
  2895. &sdata->vif,
  2896. NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
  2897. sig, GFP_KERNEL);
  2898. } else if (sig > thold &&
  2899. (last_event == 0 || sig > last_event + hyst)) {
  2900. ifmgd->last_cqm_event_signal = sig;
  2901. ieee80211_cqm_rssi_notify(
  2902. &sdata->vif,
  2903. NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
  2904. sig, GFP_KERNEL);
  2905. }
  2906. }
  2907. if (bss_conf->cqm_rssi_low &&
  2908. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
  2909. int sig = -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
  2910. int last_event = ifmgd->last_cqm_event_signal;
  2911. int low = bss_conf->cqm_rssi_low;
  2912. int high = bss_conf->cqm_rssi_high;
  2913. if (sig < low &&
  2914. (last_event == 0 || last_event >= low)) {
  2915. ifmgd->last_cqm_event_signal = sig;
  2916. ieee80211_cqm_rssi_notify(
  2917. &sdata->vif,
  2918. NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
  2919. sig, GFP_KERNEL);
  2920. } else if (sig > high &&
  2921. (last_event == 0 || last_event <= high)) {
  2922. ifmgd->last_cqm_event_signal = sig;
  2923. ieee80211_cqm_rssi_notify(
  2924. &sdata->vif,
  2925. NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
  2926. sig, GFP_KERNEL);
  2927. }
  2928. }
  2929. if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
  2930. mlme_dbg_ratelimited(sdata,
  2931. "cancelling AP probe due to a received beacon\n");
  2932. ieee80211_reset_ap_probe(sdata);
  2933. }
  2934. /*
  2935. * Push the beacon loss detection into the future since
  2936. * we are processing a beacon from the AP just now.
  2937. */
  2938. ieee80211_sta_reset_beacon_monitor(sdata);
  2939. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  2940. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  2941. len - baselen, false, &elems,
  2942. care_about_ies, ncrc);
  2943. if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) &&
  2944. ieee80211_check_tim(elems.tim, elems.tim_len, ifmgd->aid)) {
  2945. if (local->hw.conf.dynamic_ps_timeout > 0) {
  2946. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  2947. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  2948. ieee80211_hw_config(local,
  2949. IEEE80211_CONF_CHANGE_PS);
  2950. }
  2951. ieee80211_send_nullfunc(local, sdata, false);
  2952. } else if (!local->pspolling && sdata->u.mgd.powersave) {
  2953. local->pspolling = true;
  2954. /*
  2955. * Here is assumed that the driver will be
  2956. * able to send ps-poll frame and receive a
  2957. * response even though power save mode is
  2958. * enabled, but some drivers might require
  2959. * to disable power save here. This needs
  2960. * to be investigated.
  2961. */
  2962. ieee80211_send_pspoll(local, sdata);
  2963. }
  2964. }
  2965. if (sdata->vif.p2p ||
  2966. sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) {
  2967. struct ieee80211_p2p_noa_attr noa = {};
  2968. int ret;
  2969. ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
  2970. len - baselen,
  2971. IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
  2972. (u8 *) &noa, sizeof(noa));
  2973. if (ret >= 2) {
  2974. if (sdata->u.mgd.p2p_noa_index != noa.index) {
  2975. /* valid noa_attr and index changed */
  2976. sdata->u.mgd.p2p_noa_index = noa.index;
  2977. memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
  2978. changed |= BSS_CHANGED_P2P_PS;
  2979. /*
  2980. * make sure we update all information, the CRC
  2981. * mechanism doesn't look at P2P attributes.
  2982. */
  2983. ifmgd->beacon_crc_valid = false;
  2984. }
  2985. } else if (sdata->u.mgd.p2p_noa_index != -1) {
  2986. /* noa_attr not found and we had valid noa_attr before */
  2987. sdata->u.mgd.p2p_noa_index = -1;
  2988. memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
  2989. changed |= BSS_CHANGED_P2P_PS;
  2990. ifmgd->beacon_crc_valid = false;
  2991. }
  2992. }
  2993. if (ifmgd->csa_waiting_bcn)
  2994. ieee80211_chswitch_post_beacon(sdata);
  2995. /*
  2996. * Update beacon timing and dtim count on every beacon appearance. This
  2997. * will allow the driver to use the most updated values. Do it before
  2998. * comparing this one with last received beacon.
  2999. * IMPORTANT: These parameters would possibly be out of sync by the time
  3000. * the driver will use them. The synchronized view is currently
  3001. * guaranteed only in certain callbacks.
  3002. */
  3003. if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
  3004. sdata->vif.bss_conf.sync_tsf =
  3005. le64_to_cpu(mgmt->u.beacon.timestamp);
  3006. sdata->vif.bss_conf.sync_device_ts =
  3007. rx_status->device_timestamp;
  3008. if (elems.tim)
  3009. sdata->vif.bss_conf.sync_dtim_count =
  3010. elems.tim->dtim_count;
  3011. else
  3012. sdata->vif.bss_conf.sync_dtim_count = 0;
  3013. }
  3014. if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
  3015. return;
  3016. ifmgd->beacon_crc = ncrc;
  3017. ifmgd->beacon_crc_valid = true;
  3018. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  3019. ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
  3020. rx_status->device_timestamp,
  3021. &elems, true);
  3022. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
  3023. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  3024. elems.wmm_param_len))
  3025. changed |= BSS_CHANGED_QOS;
  3026. /*
  3027. * If we haven't had a beacon before, tell the driver about the
  3028. * DTIM period (and beacon timing if desired) now.
  3029. */
  3030. if (!ifmgd->have_beacon) {
  3031. /* a few bogus AP send dtim_period = 0 or no TIM IE */
  3032. if (elems.tim)
  3033. bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
  3034. else
  3035. bss_conf->dtim_period = 1;
  3036. changed |= BSS_CHANGED_BEACON_INFO;
  3037. ifmgd->have_beacon = true;
  3038. mutex_lock(&local->iflist_mtx);
  3039. ieee80211_recalc_ps(local);
  3040. mutex_unlock(&local->iflist_mtx);
  3041. ieee80211_recalc_ps_vif(sdata);
  3042. }
  3043. if (elems.erp_info) {
  3044. erp_valid = true;
  3045. erp_value = elems.erp_info[0];
  3046. } else {
  3047. erp_valid = false;
  3048. }
  3049. changed |= ieee80211_handle_bss_capability(sdata,
  3050. le16_to_cpu(mgmt->u.beacon.capab_info),
  3051. erp_valid, erp_value);
  3052. mutex_lock(&local->sta_mtx);
  3053. sta = sta_info_get(sdata, bssid);
  3054. if (ieee80211_config_bw(sdata, sta,
  3055. elems.ht_cap_elem, elems.ht_operation,
  3056. elems.vht_operation, bssid, &changed)) {
  3057. mutex_unlock(&local->sta_mtx);
  3058. sdata_info(sdata,
  3059. "failed to follow AP %pM bandwidth change, disconnect\n",
  3060. bssid);
  3061. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  3062. WLAN_REASON_DEAUTH_LEAVING,
  3063. true, deauth_buf);
  3064. ieee80211_report_disconnect(sdata, deauth_buf,
  3065. sizeof(deauth_buf), true,
  3066. WLAN_REASON_DEAUTH_LEAVING);
  3067. return;
  3068. }
  3069. if (sta && elems.opmode_notif)
  3070. ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
  3071. rx_status->band);
  3072. mutex_unlock(&local->sta_mtx);
  3073. changed |= ieee80211_handle_pwr_constr(sdata, chan, mgmt,
  3074. elems.country_elem,
  3075. elems.country_elem_len,
  3076. elems.pwr_constr_elem,
  3077. elems.cisco_dtpc_elem);
  3078. ieee80211_bss_info_change_notify(sdata, changed);
  3079. }
  3080. void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  3081. struct sk_buff *skb)
  3082. {
  3083. struct ieee80211_rx_status *rx_status;
  3084. struct ieee80211_mgmt *mgmt;
  3085. u16 fc;
  3086. struct ieee802_11_elems elems;
  3087. int ies_len;
  3088. rx_status = (struct ieee80211_rx_status *) skb->cb;
  3089. mgmt = (struct ieee80211_mgmt *) skb->data;
  3090. fc = le16_to_cpu(mgmt->frame_control);
  3091. sdata_lock(sdata);
  3092. switch (fc & IEEE80211_FCTL_STYPE) {
  3093. case IEEE80211_STYPE_BEACON:
  3094. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
  3095. break;
  3096. case IEEE80211_STYPE_PROBE_RESP:
  3097. ieee80211_rx_mgmt_probe_resp(sdata, skb);
  3098. break;
  3099. case IEEE80211_STYPE_AUTH:
  3100. ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
  3101. break;
  3102. case IEEE80211_STYPE_DEAUTH:
  3103. ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  3104. break;
  3105. case IEEE80211_STYPE_DISASSOC:
  3106. ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  3107. break;
  3108. case IEEE80211_STYPE_ASSOC_RESP:
  3109. case IEEE80211_STYPE_REASSOC_RESP:
  3110. ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
  3111. break;
  3112. case IEEE80211_STYPE_ACTION:
  3113. if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
  3114. ies_len = skb->len -
  3115. offsetof(struct ieee80211_mgmt,
  3116. u.action.u.chan_switch.variable);
  3117. if (ies_len < 0)
  3118. break;
  3119. ieee802_11_parse_elems(
  3120. mgmt->u.action.u.chan_switch.variable,
  3121. ies_len, true, &elems);
  3122. if (elems.parse_error)
  3123. break;
  3124. ieee80211_sta_process_chanswitch(sdata,
  3125. rx_status->mactime,
  3126. rx_status->device_timestamp,
  3127. &elems, false);
  3128. } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
  3129. ies_len = skb->len -
  3130. offsetof(struct ieee80211_mgmt,
  3131. u.action.u.ext_chan_switch.variable);
  3132. if (ies_len < 0)
  3133. break;
  3134. ieee802_11_parse_elems(
  3135. mgmt->u.action.u.ext_chan_switch.variable,
  3136. ies_len, true, &elems);
  3137. if (elems.parse_error)
  3138. break;
  3139. /* for the handling code pretend this was also an IE */
  3140. elems.ext_chansw_ie =
  3141. &mgmt->u.action.u.ext_chan_switch.data;
  3142. ieee80211_sta_process_chanswitch(sdata,
  3143. rx_status->mactime,
  3144. rx_status->device_timestamp,
  3145. &elems, false);
  3146. }
  3147. break;
  3148. }
  3149. sdata_unlock(sdata);
  3150. }
  3151. static void ieee80211_sta_timer(unsigned long data)
  3152. {
  3153. struct ieee80211_sub_if_data *sdata =
  3154. (struct ieee80211_sub_if_data *) data;
  3155. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  3156. }
  3157. static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
  3158. u8 *bssid, u8 reason, bool tx)
  3159. {
  3160. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3161. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
  3162. tx, frame_buf);
  3163. ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
  3164. reason);
  3165. }
  3166. static int ieee80211_auth(struct ieee80211_sub_if_data *sdata)
  3167. {
  3168. struct ieee80211_local *local = sdata->local;
  3169. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3170. struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
  3171. u32 tx_flags = 0;
  3172. u16 trans = 1;
  3173. u16 status = 0;
  3174. sdata_assert_lock(sdata);
  3175. if (WARN_ON_ONCE(!auth_data))
  3176. return -EINVAL;
  3177. auth_data->tries++;
  3178. if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
  3179. sdata_info(sdata, "authentication with %pM timed out\n",
  3180. auth_data->bss->bssid);
  3181. /*
  3182. * Most likely AP is not in the range so remove the
  3183. * bss struct for that AP.
  3184. */
  3185. cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
  3186. return -ETIMEDOUT;
  3187. }
  3188. drv_mgd_prepare_tx(local, sdata);
  3189. sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
  3190. auth_data->bss->bssid, auth_data->tries,
  3191. IEEE80211_AUTH_MAX_TRIES);
  3192. auth_data->expected_transaction = 2;
  3193. if (auth_data->algorithm == WLAN_AUTH_SAE) {
  3194. trans = auth_data->sae_trans;
  3195. status = auth_data->sae_status;
  3196. auth_data->expected_transaction = trans;
  3197. }
  3198. if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
  3199. tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
  3200. IEEE80211_TX_INTFL_MLME_CONN_TX;
  3201. ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
  3202. auth_data->data, auth_data->data_len,
  3203. auth_data->bss->bssid,
  3204. auth_data->bss->bssid, NULL, 0, 0,
  3205. tx_flags);
  3206. if (tx_flags == 0) {
  3207. auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  3208. auth_data->timeout_started = true;
  3209. run_again(sdata, auth_data->timeout);
  3210. } else {
  3211. auth_data->timeout =
  3212. round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
  3213. auth_data->timeout_started = true;
  3214. run_again(sdata, auth_data->timeout);
  3215. }
  3216. return 0;
  3217. }
  3218. static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
  3219. {
  3220. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  3221. struct ieee80211_local *local = sdata->local;
  3222. sdata_assert_lock(sdata);
  3223. assoc_data->tries++;
  3224. if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
  3225. sdata_info(sdata, "association with %pM timed out\n",
  3226. assoc_data->bss->bssid);
  3227. /*
  3228. * Most likely AP is not in the range so remove the
  3229. * bss struct for that AP.
  3230. */
  3231. cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
  3232. return -ETIMEDOUT;
  3233. }
  3234. sdata_info(sdata, "associate with %pM (try %d/%d)\n",
  3235. assoc_data->bss->bssid, assoc_data->tries,
  3236. IEEE80211_ASSOC_MAX_TRIES);
  3237. ieee80211_send_assoc(sdata);
  3238. if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
  3239. assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
  3240. assoc_data->timeout_started = true;
  3241. run_again(sdata, assoc_data->timeout);
  3242. } else {
  3243. assoc_data->timeout =
  3244. round_jiffies_up(jiffies +
  3245. IEEE80211_ASSOC_TIMEOUT_LONG);
  3246. assoc_data->timeout_started = true;
  3247. run_again(sdata, assoc_data->timeout);
  3248. }
  3249. return 0;
  3250. }
  3251. void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
  3252. __le16 fc, bool acked)
  3253. {
  3254. struct ieee80211_local *local = sdata->local;
  3255. sdata->u.mgd.status_fc = fc;
  3256. sdata->u.mgd.status_acked = acked;
  3257. sdata->u.mgd.status_received = true;
  3258. ieee80211_queue_work(&local->hw, &sdata->work);
  3259. }
  3260. void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
  3261. {
  3262. struct ieee80211_local *local = sdata->local;
  3263. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3264. sdata_lock(sdata);
  3265. if (ifmgd->status_received) {
  3266. __le16 fc = ifmgd->status_fc;
  3267. bool status_acked = ifmgd->status_acked;
  3268. ifmgd->status_received = false;
  3269. if (ifmgd->auth_data && ieee80211_is_auth(fc)) {
  3270. if (status_acked) {
  3271. ifmgd->auth_data->timeout =
  3272. jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
  3273. run_again(sdata, ifmgd->auth_data->timeout);
  3274. } else {
  3275. ifmgd->auth_data->timeout = jiffies - 1;
  3276. }
  3277. ifmgd->auth_data->timeout_started = true;
  3278. } else if (ifmgd->assoc_data &&
  3279. (ieee80211_is_assoc_req(fc) ||
  3280. ieee80211_is_reassoc_req(fc))) {
  3281. if (status_acked) {
  3282. ifmgd->assoc_data->timeout =
  3283. jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
  3284. run_again(sdata, ifmgd->assoc_data->timeout);
  3285. } else {
  3286. ifmgd->assoc_data->timeout = jiffies - 1;
  3287. }
  3288. ifmgd->assoc_data->timeout_started = true;
  3289. }
  3290. }
  3291. if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
  3292. time_after(jiffies, ifmgd->auth_data->timeout)) {
  3293. if (ifmgd->auth_data->done) {
  3294. /*
  3295. * ok ... we waited for assoc but userspace didn't,
  3296. * so let's just kill the auth data
  3297. */
  3298. ieee80211_destroy_auth_data(sdata, false);
  3299. } else if (ieee80211_auth(sdata)) {
  3300. u8 bssid[ETH_ALEN];
  3301. struct ieee80211_event event = {
  3302. .type = MLME_EVENT,
  3303. .u.mlme.data = AUTH_EVENT,
  3304. .u.mlme.status = MLME_TIMEOUT,
  3305. };
  3306. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  3307. ieee80211_destroy_auth_data(sdata, false);
  3308. cfg80211_auth_timeout(sdata->dev, bssid);
  3309. drv_event_callback(sdata->local, sdata, &event);
  3310. }
  3311. } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
  3312. run_again(sdata, ifmgd->auth_data->timeout);
  3313. if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
  3314. time_after(jiffies, ifmgd->assoc_data->timeout)) {
  3315. if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
  3316. ieee80211_do_assoc(sdata)) {
  3317. struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
  3318. struct ieee80211_event event = {
  3319. .type = MLME_EVENT,
  3320. .u.mlme.data = ASSOC_EVENT,
  3321. .u.mlme.status = MLME_TIMEOUT,
  3322. };
  3323. ieee80211_destroy_assoc_data(sdata, false, false);
  3324. cfg80211_assoc_timeout(sdata->dev, bss);
  3325. drv_event_callback(sdata->local, sdata, &event);
  3326. }
  3327. } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
  3328. run_again(sdata, ifmgd->assoc_data->timeout);
  3329. if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
  3330. ifmgd->associated) {
  3331. u8 bssid[ETH_ALEN];
  3332. int max_tries;
  3333. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  3334. if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS))
  3335. max_tries = max_nullfunc_tries;
  3336. else
  3337. max_tries = max_probe_tries;
  3338. /* ACK received for nullfunc probing frame */
  3339. if (!ifmgd->probe_send_count)
  3340. ieee80211_reset_ap_probe(sdata);
  3341. else if (ifmgd->nullfunc_failed) {
  3342. if (ifmgd->probe_send_count < max_tries) {
  3343. mlme_dbg(sdata,
  3344. "No ack for nullfunc frame to AP %pM, try %d/%i\n",
  3345. bssid, ifmgd->probe_send_count,
  3346. max_tries);
  3347. ieee80211_mgd_probe_ap_send(sdata);
  3348. } else {
  3349. mlme_dbg(sdata,
  3350. "No ack for nullfunc frame to AP %pM, disconnecting.\n",
  3351. bssid);
  3352. ieee80211_sta_connection_lost(sdata, bssid,
  3353. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  3354. false);
  3355. }
  3356. } else if (time_is_after_jiffies(ifmgd->probe_timeout))
  3357. run_again(sdata, ifmgd->probe_timeout);
  3358. else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
  3359. mlme_dbg(sdata,
  3360. "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
  3361. bssid, probe_wait_ms);
  3362. ieee80211_sta_connection_lost(sdata, bssid,
  3363. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
  3364. } else if (ifmgd->probe_send_count < max_tries) {
  3365. mlme_dbg(sdata,
  3366. "No probe response from AP %pM after %dms, try %d/%i\n",
  3367. bssid, probe_wait_ms,
  3368. ifmgd->probe_send_count, max_tries);
  3369. ieee80211_mgd_probe_ap_send(sdata);
  3370. } else {
  3371. /*
  3372. * We actually lost the connection ... or did we?
  3373. * Let's make sure!
  3374. */
  3375. mlme_dbg(sdata,
  3376. "No probe response from AP %pM after %dms, disconnecting.\n",
  3377. bssid, probe_wait_ms);
  3378. ieee80211_sta_connection_lost(sdata, bssid,
  3379. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
  3380. }
  3381. }
  3382. sdata_unlock(sdata);
  3383. }
  3384. static void ieee80211_sta_bcn_mon_timer(unsigned long data)
  3385. {
  3386. struct ieee80211_sub_if_data *sdata =
  3387. (struct ieee80211_sub_if_data *) data;
  3388. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3389. if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
  3390. return;
  3391. sdata->u.mgd.connection_loss = false;
  3392. ieee80211_queue_work(&sdata->local->hw,
  3393. &sdata->u.mgd.beacon_connection_loss_work);
  3394. }
  3395. static void ieee80211_sta_conn_mon_timer(unsigned long data)
  3396. {
  3397. struct ieee80211_sub_if_data *sdata =
  3398. (struct ieee80211_sub_if_data *) data;
  3399. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3400. struct ieee80211_local *local = sdata->local;
  3401. if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
  3402. return;
  3403. ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
  3404. }
  3405. static void ieee80211_sta_monitor_work(struct work_struct *work)
  3406. {
  3407. struct ieee80211_sub_if_data *sdata =
  3408. container_of(work, struct ieee80211_sub_if_data,
  3409. u.mgd.monitor_work);
  3410. ieee80211_mgd_probe_ap(sdata, false);
  3411. }
  3412. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  3413. {
  3414. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  3415. __ieee80211_stop_poll(sdata);
  3416. /* let's probe the connection once */
  3417. if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR))
  3418. ieee80211_queue_work(&sdata->local->hw,
  3419. &sdata->u.mgd.monitor_work);
  3420. }
  3421. }
  3422. #ifdef CONFIG_PM
  3423. void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
  3424. {
  3425. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3426. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3427. sdata_lock(sdata);
  3428. if (ifmgd->auth_data || ifmgd->assoc_data) {
  3429. const u8 *bssid = ifmgd->auth_data ?
  3430. ifmgd->auth_data->bss->bssid :
  3431. ifmgd->assoc_data->bss->bssid;
  3432. /*
  3433. * If we are trying to authenticate / associate while suspending,
  3434. * cfg80211 won't know and won't actually abort those attempts,
  3435. * thus we need to do that ourselves.
  3436. */
  3437. ieee80211_send_deauth_disassoc(sdata, bssid,
  3438. IEEE80211_STYPE_DEAUTH,
  3439. WLAN_REASON_DEAUTH_LEAVING,
  3440. false, frame_buf);
  3441. if (ifmgd->assoc_data)
  3442. ieee80211_destroy_assoc_data(sdata, false, true);
  3443. if (ifmgd->auth_data)
  3444. ieee80211_destroy_auth_data(sdata, false);
  3445. cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
  3446. IEEE80211_DEAUTH_FRAME_LEN);
  3447. }
  3448. /* This is a bit of a hack - we should find a better and more generic
  3449. * solution to this. Normally when suspending, cfg80211 will in fact
  3450. * deauthenticate. However, it doesn't (and cannot) stop an ongoing
  3451. * auth (not so important) or assoc (this is the problem) process.
  3452. *
  3453. * As a consequence, it can happen that we are in the process of both
  3454. * associating and suspending, and receive an association response
  3455. * after cfg80211 has checked if it needs to disconnect, but before
  3456. * we actually set the flag to drop incoming frames. This will then
  3457. * cause the workqueue flush to process the association response in
  3458. * the suspend, resulting in a successful association just before it
  3459. * tries to remove the interface from the driver, which now though
  3460. * has a channel context assigned ... this results in issues.
  3461. *
  3462. * To work around this (for now) simply deauth here again if we're
  3463. * now connected.
  3464. */
  3465. if (ifmgd->associated && !sdata->local->wowlan) {
  3466. u8 bssid[ETH_ALEN];
  3467. struct cfg80211_deauth_request req = {
  3468. .reason_code = WLAN_REASON_DEAUTH_LEAVING,
  3469. .bssid = bssid,
  3470. };
  3471. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  3472. ieee80211_mgd_deauth(sdata, &req);
  3473. }
  3474. sdata_unlock(sdata);
  3475. }
  3476. void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
  3477. {
  3478. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3479. sdata_lock(sdata);
  3480. if (!ifmgd->associated) {
  3481. sdata_unlock(sdata);
  3482. return;
  3483. }
  3484. if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
  3485. sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
  3486. mlme_dbg(sdata, "driver requested disconnect after resume\n");
  3487. ieee80211_sta_connection_lost(sdata,
  3488. ifmgd->associated->bssid,
  3489. WLAN_REASON_UNSPECIFIED,
  3490. true);
  3491. sdata_unlock(sdata);
  3492. return;
  3493. }
  3494. sdata_unlock(sdata);
  3495. }
  3496. #endif
  3497. /* interface setup */
  3498. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  3499. {
  3500. struct ieee80211_if_managed *ifmgd;
  3501. ifmgd = &sdata->u.mgd;
  3502. INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
  3503. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  3504. INIT_WORK(&ifmgd->beacon_connection_loss_work,
  3505. ieee80211_beacon_connection_loss_work);
  3506. INIT_WORK(&ifmgd->csa_connection_drop_work,
  3507. ieee80211_csa_connection_drop_work);
  3508. INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
  3509. INIT_DELAYED_WORK(&ifmgd->tdls_peer_del_work,
  3510. ieee80211_tdls_peer_del_work);
  3511. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  3512. (unsigned long) sdata);
  3513. setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
  3514. (unsigned long) sdata);
  3515. setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
  3516. (unsigned long) sdata);
  3517. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  3518. (unsigned long) sdata);
  3519. INIT_DELAYED_WORK(&ifmgd->tx_tspec_wk,
  3520. ieee80211_sta_handle_tspec_ac_params_wk);
  3521. ifmgd->flags = 0;
  3522. ifmgd->powersave = sdata->wdev.ps;
  3523. ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
  3524. ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
  3525. ifmgd->p2p_noa_index = -1;
  3526. if (sdata->local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS)
  3527. ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
  3528. else
  3529. ifmgd->req_smps = IEEE80211_SMPS_OFF;
  3530. /* Setup TDLS data */
  3531. spin_lock_init(&ifmgd->teardown_lock);
  3532. ifmgd->teardown_skb = NULL;
  3533. ifmgd->orig_teardown_skb = NULL;
  3534. }
  3535. /* scan finished notification */
  3536. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  3537. {
  3538. struct ieee80211_sub_if_data *sdata;
  3539. /* Restart STA timers */
  3540. rcu_read_lock();
  3541. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  3542. if (ieee80211_sdata_running(sdata))
  3543. ieee80211_restart_sta_timer(sdata);
  3544. }
  3545. rcu_read_unlock();
  3546. }
  3547. static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
  3548. struct cfg80211_bss *cbss)
  3549. {
  3550. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3551. const u8 *ht_cap_ie, *vht_cap_ie;
  3552. const struct ieee80211_ht_cap *ht_cap;
  3553. const struct ieee80211_vht_cap *vht_cap;
  3554. u8 chains = 1;
  3555. if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
  3556. return chains;
  3557. ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
  3558. if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
  3559. ht_cap = (void *)(ht_cap_ie + 2);
  3560. chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
  3561. /*
  3562. * TODO: use "Tx Maximum Number Spatial Streams Supported" and
  3563. * "Tx Unequal Modulation Supported" fields.
  3564. */
  3565. }
  3566. if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
  3567. return chains;
  3568. vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
  3569. if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
  3570. u8 nss;
  3571. u16 tx_mcs_map;
  3572. vht_cap = (void *)(vht_cap_ie + 2);
  3573. tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
  3574. for (nss = 8; nss > 0; nss--) {
  3575. if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
  3576. IEEE80211_VHT_MCS_NOT_SUPPORTED)
  3577. break;
  3578. }
  3579. /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
  3580. chains = max(chains, nss);
  3581. }
  3582. return chains;
  3583. }
  3584. static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
  3585. struct cfg80211_bss *cbss)
  3586. {
  3587. struct ieee80211_local *local = sdata->local;
  3588. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3589. const struct ieee80211_ht_cap *ht_cap = NULL;
  3590. const struct ieee80211_ht_operation *ht_oper = NULL;
  3591. const struct ieee80211_vht_operation *vht_oper = NULL;
  3592. struct ieee80211_supported_band *sband;
  3593. struct cfg80211_chan_def chandef;
  3594. int ret;
  3595. u32 i;
  3596. bool have_80mhz;
  3597. sband = local->hw.wiphy->bands[cbss->channel->band];
  3598. ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
  3599. IEEE80211_STA_DISABLE_80P80MHZ |
  3600. IEEE80211_STA_DISABLE_160MHZ);
  3601. rcu_read_lock();
  3602. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  3603. sband->ht_cap.ht_supported) {
  3604. const u8 *ht_oper_ie, *ht_cap_ie;
  3605. ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
  3606. if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
  3607. ht_oper = (void *)(ht_oper_ie + 2);
  3608. ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
  3609. if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap))
  3610. ht_cap = (void *)(ht_cap_ie + 2);
  3611. if (!ht_cap) {
  3612. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3613. ht_oper = NULL;
  3614. }
  3615. }
  3616. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  3617. sband->vht_cap.vht_supported) {
  3618. const u8 *vht_oper_ie, *vht_cap;
  3619. vht_oper_ie = ieee80211_bss_get_ie(cbss,
  3620. WLAN_EID_VHT_OPERATION);
  3621. if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
  3622. vht_oper = (void *)(vht_oper_ie + 2);
  3623. if (vht_oper && !ht_oper) {
  3624. vht_oper = NULL;
  3625. sdata_info(sdata,
  3626. "AP advertised VHT without HT, disabling both\n");
  3627. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3628. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3629. }
  3630. vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
  3631. if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
  3632. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3633. vht_oper = NULL;
  3634. }
  3635. }
  3636. /* Allow VHT if at least one channel on the sband supports 80 MHz */
  3637. have_80mhz = false;
  3638. for (i = 0; i < sband->n_channels; i++) {
  3639. if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
  3640. IEEE80211_CHAN_NO_80MHZ))
  3641. continue;
  3642. have_80mhz = true;
  3643. break;
  3644. }
  3645. if (!have_80mhz)
  3646. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3647. ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
  3648. cbss->channel,
  3649. ht_cap, ht_oper, vht_oper,
  3650. &chandef, false);
  3651. sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
  3652. local->rx_chains);
  3653. rcu_read_unlock();
  3654. /* will change later if needed */
  3655. sdata->smps_mode = IEEE80211_SMPS_OFF;
  3656. mutex_lock(&local->mtx);
  3657. /*
  3658. * If this fails (possibly due to channel context sharing
  3659. * on incompatible channels, e.g. 80+80 and 160 sharing the
  3660. * same control channel) try to use a smaller bandwidth.
  3661. */
  3662. ret = ieee80211_vif_use_channel(sdata, &chandef,
  3663. IEEE80211_CHANCTX_SHARED);
  3664. /* don't downgrade for 5 and 10 MHz channels, though. */
  3665. if (chandef.width == NL80211_CHAN_WIDTH_5 ||
  3666. chandef.width == NL80211_CHAN_WIDTH_10)
  3667. goto out;
  3668. while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
  3669. ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
  3670. ret = ieee80211_vif_use_channel(sdata, &chandef,
  3671. IEEE80211_CHANCTX_SHARED);
  3672. }
  3673. out:
  3674. mutex_unlock(&local->mtx);
  3675. return ret;
  3676. }
  3677. static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
  3678. struct cfg80211_bss *cbss, bool assoc,
  3679. bool override)
  3680. {
  3681. struct ieee80211_local *local = sdata->local;
  3682. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3683. struct ieee80211_bss *bss = (void *)cbss->priv;
  3684. struct sta_info *new_sta = NULL;
  3685. struct ieee80211_supported_band *sband;
  3686. bool have_sta = false;
  3687. int err;
  3688. sband = local->hw.wiphy->bands[cbss->channel->band];
  3689. if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
  3690. return -EINVAL;
  3691. if (assoc) {
  3692. rcu_read_lock();
  3693. have_sta = sta_info_get(sdata, cbss->bssid);
  3694. rcu_read_unlock();
  3695. }
  3696. if (!have_sta) {
  3697. new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
  3698. if (!new_sta)
  3699. return -ENOMEM;
  3700. }
  3701. if (new_sta || override) {
  3702. err = ieee80211_prep_channel(sdata, cbss);
  3703. if (err) {
  3704. if (new_sta)
  3705. sta_info_free(local, new_sta);
  3706. return -EINVAL;
  3707. }
  3708. }
  3709. if (new_sta) {
  3710. u32 rates = 0, basic_rates = 0;
  3711. bool have_higher_than_11mbit;
  3712. int min_rate = INT_MAX, min_rate_index = -1;
  3713. struct ieee80211_chanctx_conf *chanctx_conf;
  3714. const struct cfg80211_bss_ies *ies;
  3715. int shift = ieee80211_vif_get_shift(&sdata->vif);
  3716. u32 rate_flags;
  3717. rcu_read_lock();
  3718. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  3719. if (WARN_ON(!chanctx_conf)) {
  3720. rcu_read_unlock();
  3721. sta_info_free(local, new_sta);
  3722. return -EINVAL;
  3723. }
  3724. rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
  3725. rcu_read_unlock();
  3726. ieee80211_get_rates(sband, bss->supp_rates,
  3727. bss->supp_rates_len,
  3728. &rates, &basic_rates,
  3729. &have_higher_than_11mbit,
  3730. &min_rate, &min_rate_index,
  3731. shift, rate_flags);
  3732. /*
  3733. * This used to be a workaround for basic rates missing
  3734. * in the association response frame. Now that we no
  3735. * longer use the basic rates from there, it probably
  3736. * doesn't happen any more, but keep the workaround so
  3737. * in case some *other* APs are buggy in different ways
  3738. * we can connect -- with a warning.
  3739. */
  3740. if (!basic_rates && min_rate_index >= 0) {
  3741. sdata_info(sdata,
  3742. "No basic rates, using min rate instead\n");
  3743. basic_rates = BIT(min_rate_index);
  3744. }
  3745. new_sta->sta.supp_rates[cbss->channel->band] = rates;
  3746. sdata->vif.bss_conf.basic_rates = basic_rates;
  3747. /* cf. IEEE 802.11 9.2.12 */
  3748. if (cbss->channel->band == NL80211_BAND_2GHZ &&
  3749. have_higher_than_11mbit)
  3750. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  3751. else
  3752. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  3753. memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
  3754. /* set timing information */
  3755. sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
  3756. rcu_read_lock();
  3757. ies = rcu_dereference(cbss->beacon_ies);
  3758. if (ies) {
  3759. const u8 *tim_ie;
  3760. sdata->vif.bss_conf.sync_tsf = ies->tsf;
  3761. sdata->vif.bss_conf.sync_device_ts =
  3762. bss->device_ts_beacon;
  3763. tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
  3764. ies->data, ies->len);
  3765. if (tim_ie && tim_ie[1] >= 2)
  3766. sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
  3767. else
  3768. sdata->vif.bss_conf.sync_dtim_count = 0;
  3769. } else if (!ieee80211_hw_check(&sdata->local->hw,
  3770. TIMING_BEACON_ONLY)) {
  3771. ies = rcu_dereference(cbss->proberesp_ies);
  3772. /* must be non-NULL since beacon IEs were NULL */
  3773. sdata->vif.bss_conf.sync_tsf = ies->tsf;
  3774. sdata->vif.bss_conf.sync_device_ts =
  3775. bss->device_ts_presp;
  3776. sdata->vif.bss_conf.sync_dtim_count = 0;
  3777. } else {
  3778. sdata->vif.bss_conf.sync_tsf = 0;
  3779. sdata->vif.bss_conf.sync_device_ts = 0;
  3780. sdata->vif.bss_conf.sync_dtim_count = 0;
  3781. }
  3782. rcu_read_unlock();
  3783. /* tell driver about BSSID, basic rates and timing */
  3784. ieee80211_bss_info_change_notify(sdata,
  3785. BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
  3786. BSS_CHANGED_BEACON_INT);
  3787. if (assoc)
  3788. sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
  3789. err = sta_info_insert(new_sta);
  3790. new_sta = NULL;
  3791. if (err) {
  3792. sdata_info(sdata,
  3793. "failed to insert STA entry for the AP (error %d)\n",
  3794. err);
  3795. return err;
  3796. }
  3797. } else
  3798. WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
  3799. /* Cancel scan to ensure that nothing interferes with connection */
  3800. if (local->scanning)
  3801. ieee80211_scan_cancel(local);
  3802. return 0;
  3803. }
  3804. /* config hooks */
  3805. int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
  3806. struct cfg80211_auth_request *req)
  3807. {
  3808. struct ieee80211_local *local = sdata->local;
  3809. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3810. struct ieee80211_mgd_auth_data *auth_data;
  3811. u16 auth_alg;
  3812. int err;
  3813. /* prepare auth data structure */
  3814. switch (req->auth_type) {
  3815. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  3816. auth_alg = WLAN_AUTH_OPEN;
  3817. break;
  3818. case NL80211_AUTHTYPE_SHARED_KEY:
  3819. if (IS_ERR(local->wep_tx_tfm))
  3820. return -EOPNOTSUPP;
  3821. auth_alg = WLAN_AUTH_SHARED_KEY;
  3822. break;
  3823. case NL80211_AUTHTYPE_FT:
  3824. auth_alg = WLAN_AUTH_FT;
  3825. break;
  3826. case NL80211_AUTHTYPE_NETWORK_EAP:
  3827. auth_alg = WLAN_AUTH_LEAP;
  3828. break;
  3829. case NL80211_AUTHTYPE_SAE:
  3830. auth_alg = WLAN_AUTH_SAE;
  3831. break;
  3832. case NL80211_AUTHTYPE_FILS_SK:
  3833. auth_alg = WLAN_AUTH_FILS_SK;
  3834. break;
  3835. case NL80211_AUTHTYPE_FILS_SK_PFS:
  3836. auth_alg = WLAN_AUTH_FILS_SK_PFS;
  3837. break;
  3838. case NL80211_AUTHTYPE_FILS_PK:
  3839. auth_alg = WLAN_AUTH_FILS_PK;
  3840. break;
  3841. default:
  3842. return -EOPNOTSUPP;
  3843. }
  3844. auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len +
  3845. req->ie_len, GFP_KERNEL);
  3846. if (!auth_data)
  3847. return -ENOMEM;
  3848. auth_data->bss = req->bss;
  3849. if (req->auth_data_len >= 4) {
  3850. if (req->auth_type == NL80211_AUTHTYPE_SAE) {
  3851. __le16 *pos = (__le16 *) req->auth_data;
  3852. auth_data->sae_trans = le16_to_cpu(pos[0]);
  3853. auth_data->sae_status = le16_to_cpu(pos[1]);
  3854. }
  3855. memcpy(auth_data->data, req->auth_data + 4,
  3856. req->auth_data_len - 4);
  3857. auth_data->data_len += req->auth_data_len - 4;
  3858. }
  3859. if (req->ie && req->ie_len) {
  3860. memcpy(&auth_data->data[auth_data->data_len],
  3861. req->ie, req->ie_len);
  3862. auth_data->data_len += req->ie_len;
  3863. }
  3864. if (req->key && req->key_len) {
  3865. auth_data->key_len = req->key_len;
  3866. auth_data->key_idx = req->key_idx;
  3867. memcpy(auth_data->key, req->key, req->key_len);
  3868. }
  3869. auth_data->algorithm = auth_alg;
  3870. /* try to authenticate/probe */
  3871. if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
  3872. ifmgd->assoc_data) {
  3873. err = -EBUSY;
  3874. goto err_free;
  3875. }
  3876. if (ifmgd->auth_data)
  3877. ieee80211_destroy_auth_data(sdata, false);
  3878. /* prep auth_data so we don't go into idle on disassoc */
  3879. ifmgd->auth_data = auth_data;
  3880. if (ifmgd->associated) {
  3881. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3882. sdata_info(sdata,
  3883. "disconnect from AP %pM for new auth to %pM\n",
  3884. ifmgd->associated->bssid, req->bss->bssid);
  3885. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  3886. WLAN_REASON_UNSPECIFIED,
  3887. false, frame_buf);
  3888. ieee80211_report_disconnect(sdata, frame_buf,
  3889. sizeof(frame_buf), true,
  3890. WLAN_REASON_UNSPECIFIED);
  3891. }
  3892. sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
  3893. err = ieee80211_prep_connection(sdata, req->bss, false, false);
  3894. if (err)
  3895. goto err_clear;
  3896. err = ieee80211_auth(sdata);
  3897. if (err) {
  3898. sta_info_destroy_addr(sdata, req->bss->bssid);
  3899. goto err_clear;
  3900. }
  3901. /* hold our own reference */
  3902. cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
  3903. return 0;
  3904. err_clear:
  3905. eth_zero_addr(ifmgd->bssid);
  3906. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  3907. ifmgd->auth_data = NULL;
  3908. mutex_lock(&sdata->local->mtx);
  3909. ieee80211_vif_release_channel(sdata);
  3910. mutex_unlock(&sdata->local->mtx);
  3911. err_free:
  3912. kfree(auth_data);
  3913. return err;
  3914. }
  3915. int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
  3916. struct cfg80211_assoc_request *req)
  3917. {
  3918. struct ieee80211_local *local = sdata->local;
  3919. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3920. struct ieee80211_bss *bss = (void *)req->bss->priv;
  3921. struct ieee80211_mgd_assoc_data *assoc_data;
  3922. const struct cfg80211_bss_ies *beacon_ies;
  3923. struct ieee80211_supported_band *sband;
  3924. const u8 *ssidie, *ht_ie, *vht_ie;
  3925. int i, err;
  3926. bool override = false;
  3927. assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
  3928. if (!assoc_data)
  3929. return -ENOMEM;
  3930. rcu_read_lock();
  3931. ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  3932. if (!ssidie) {
  3933. rcu_read_unlock();
  3934. kfree(assoc_data);
  3935. return -EINVAL;
  3936. }
  3937. memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
  3938. assoc_data->ssid_len = ssidie[1];
  3939. rcu_read_unlock();
  3940. if (ifmgd->associated) {
  3941. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3942. sdata_info(sdata,
  3943. "disconnect from AP %pM for new assoc to %pM\n",
  3944. ifmgd->associated->bssid, req->bss->bssid);
  3945. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  3946. WLAN_REASON_UNSPECIFIED,
  3947. false, frame_buf);
  3948. ieee80211_report_disconnect(sdata, frame_buf,
  3949. sizeof(frame_buf), true,
  3950. WLAN_REASON_UNSPECIFIED);
  3951. }
  3952. if (ifmgd->auth_data && !ifmgd->auth_data->done) {
  3953. err = -EBUSY;
  3954. goto err_free;
  3955. }
  3956. if (ifmgd->assoc_data) {
  3957. err = -EBUSY;
  3958. goto err_free;
  3959. }
  3960. if (ifmgd->auth_data) {
  3961. bool match;
  3962. /* keep sta info, bssid if matching */
  3963. match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
  3964. ieee80211_destroy_auth_data(sdata, match);
  3965. }
  3966. /* prepare assoc data */
  3967. ifmgd->beacon_crc_valid = false;
  3968. assoc_data->wmm = bss->wmm_used &&
  3969. (local->hw.queues >= IEEE80211_NUM_ACS);
  3970. /*
  3971. * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
  3972. * We still associate in non-HT mode (11a/b/g) if any one of these
  3973. * ciphers is configured as pairwise.
  3974. * We can set this to true for non-11n hardware, that'll be checked
  3975. * separately along with the peer capabilities.
  3976. */
  3977. for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
  3978. if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
  3979. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
  3980. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
  3981. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3982. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3983. netdev_info(sdata->dev,
  3984. "disabling HT/VHT due to WEP/TKIP use\n");
  3985. }
  3986. }
  3987. /* Also disable HT if we don't support it or the AP doesn't use WMM */
  3988. sband = local->hw.wiphy->bands[req->bss->channel->band];
  3989. if (!sband->ht_cap.ht_supported ||
  3990. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
  3991. ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
  3992. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3993. if (!bss->wmm_used &&
  3994. !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
  3995. netdev_info(sdata->dev,
  3996. "disabling HT as WMM/QoS is not supported by the AP\n");
  3997. }
  3998. /* disable VHT if we don't support it or the AP doesn't use WMM */
  3999. if (!sband->vht_cap.vht_supported ||
  4000. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
  4001. ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
  4002. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  4003. if (!bss->wmm_used &&
  4004. !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
  4005. netdev_info(sdata->dev,
  4006. "disabling VHT as WMM/QoS is not supported by the AP\n");
  4007. }
  4008. memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
  4009. memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
  4010. sizeof(ifmgd->ht_capa_mask));
  4011. memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
  4012. memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
  4013. sizeof(ifmgd->vht_capa_mask));
  4014. if (req->ie && req->ie_len) {
  4015. memcpy(assoc_data->ie, req->ie, req->ie_len);
  4016. assoc_data->ie_len = req->ie_len;
  4017. }
  4018. if (req->fils_kek) {
  4019. /* should already be checked in cfg80211 - so warn */
  4020. if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) {
  4021. err = -EINVAL;
  4022. goto err_free;
  4023. }
  4024. memcpy(assoc_data->fils_kek, req->fils_kek,
  4025. req->fils_kek_len);
  4026. assoc_data->fils_kek_len = req->fils_kek_len;
  4027. }
  4028. if (req->fils_nonces)
  4029. memcpy(assoc_data->fils_nonces, req->fils_nonces,
  4030. 2 * FILS_NONCE_LEN);
  4031. assoc_data->bss = req->bss;
  4032. if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
  4033. if (ifmgd->powersave)
  4034. sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
  4035. else
  4036. sdata->smps_mode = IEEE80211_SMPS_OFF;
  4037. } else
  4038. sdata->smps_mode = ifmgd->req_smps;
  4039. assoc_data->capability = req->bss->capability;
  4040. assoc_data->supp_rates = bss->supp_rates;
  4041. assoc_data->supp_rates_len = bss->supp_rates_len;
  4042. rcu_read_lock();
  4043. ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
  4044. if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
  4045. assoc_data->ap_ht_param =
  4046. ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
  4047. else
  4048. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  4049. vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
  4050. if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
  4051. memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
  4052. sizeof(struct ieee80211_vht_cap));
  4053. else
  4054. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  4055. rcu_read_unlock();
  4056. if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) &&
  4057. ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK),
  4058. "U-APSD not supported with HW_PS_NULLFUNC_STACK\n"))
  4059. sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
  4060. if (bss->wmm_used && bss->uapsd_supported &&
  4061. (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) {
  4062. assoc_data->uapsd = true;
  4063. ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
  4064. } else {
  4065. assoc_data->uapsd = false;
  4066. ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
  4067. }
  4068. if (req->prev_bssid)
  4069. memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
  4070. if (req->use_mfp) {
  4071. ifmgd->mfp = IEEE80211_MFP_REQUIRED;
  4072. ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
  4073. } else {
  4074. ifmgd->mfp = IEEE80211_MFP_DISABLED;
  4075. ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
  4076. }
  4077. if (req->flags & ASSOC_REQ_USE_RRM)
  4078. ifmgd->flags |= IEEE80211_STA_ENABLE_RRM;
  4079. else
  4080. ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM;
  4081. if (req->crypto.control_port)
  4082. ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
  4083. else
  4084. ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
  4085. sdata->control_port_protocol = req->crypto.control_port_ethertype;
  4086. sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
  4087. sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
  4088. sdata->vif.type);
  4089. /* kick off associate process */
  4090. ifmgd->assoc_data = assoc_data;
  4091. ifmgd->dtim_period = 0;
  4092. ifmgd->have_beacon = false;
  4093. /* override HT/VHT configuration only if the AP and we support it */
  4094. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
  4095. struct ieee80211_sta_ht_cap sta_ht_cap;
  4096. if (req->flags & ASSOC_REQ_DISABLE_HT)
  4097. override = true;
  4098. memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap));
  4099. ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap);
  4100. /* check for 40 MHz disable override */
  4101. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ) &&
  4102. sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
  4103. !(sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  4104. override = true;
  4105. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  4106. req->flags & ASSOC_REQ_DISABLE_VHT)
  4107. override = true;
  4108. }
  4109. if (req->flags & ASSOC_REQ_DISABLE_HT) {
  4110. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  4111. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  4112. }
  4113. if (req->flags & ASSOC_REQ_DISABLE_VHT)
  4114. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  4115. err = ieee80211_prep_connection(sdata, req->bss, true, override);
  4116. if (err)
  4117. goto err_clear;
  4118. rcu_read_lock();
  4119. beacon_ies = rcu_dereference(req->bss->beacon_ies);
  4120. if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC) &&
  4121. !beacon_ies) {
  4122. /*
  4123. * Wait up to one beacon interval ...
  4124. * should this be more if we miss one?
  4125. */
  4126. sdata_info(sdata, "waiting for beacon from %pM\n",
  4127. ifmgd->bssid);
  4128. assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
  4129. assoc_data->timeout_started = true;
  4130. assoc_data->need_beacon = true;
  4131. } else if (beacon_ies) {
  4132. const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
  4133. beacon_ies->data,
  4134. beacon_ies->len);
  4135. u8 dtim_count = 0;
  4136. if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
  4137. const struct ieee80211_tim_ie *tim;
  4138. tim = (void *)(tim_ie + 2);
  4139. ifmgd->dtim_period = tim->dtim_period;
  4140. dtim_count = tim->dtim_count;
  4141. }
  4142. ifmgd->have_beacon = true;
  4143. assoc_data->timeout = jiffies;
  4144. assoc_data->timeout_started = true;
  4145. if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) {
  4146. sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
  4147. sdata->vif.bss_conf.sync_device_ts =
  4148. bss->device_ts_beacon;
  4149. sdata->vif.bss_conf.sync_dtim_count = dtim_count;
  4150. }
  4151. } else {
  4152. assoc_data->timeout = jiffies;
  4153. assoc_data->timeout_started = true;
  4154. }
  4155. rcu_read_unlock();
  4156. run_again(sdata, assoc_data->timeout);
  4157. if (bss->corrupt_data) {
  4158. char *corrupt_type = "data";
  4159. if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
  4160. if (bss->corrupt_data &
  4161. IEEE80211_BSS_CORRUPT_PROBE_RESP)
  4162. corrupt_type = "beacon and probe response";
  4163. else
  4164. corrupt_type = "beacon";
  4165. } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
  4166. corrupt_type = "probe response";
  4167. sdata_info(sdata, "associating with AP with corrupt %s\n",
  4168. corrupt_type);
  4169. }
  4170. return 0;
  4171. err_clear:
  4172. eth_zero_addr(ifmgd->bssid);
  4173. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  4174. ifmgd->assoc_data = NULL;
  4175. err_free:
  4176. kfree(assoc_data);
  4177. return err;
  4178. }
  4179. int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
  4180. struct cfg80211_deauth_request *req)
  4181. {
  4182. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  4183. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  4184. bool tx = !req->local_state_change;
  4185. if (ifmgd->auth_data &&
  4186. ether_addr_equal(ifmgd->auth_data->bss->bssid, req->bssid)) {
  4187. sdata_info(sdata,
  4188. "aborting authentication with %pM by local choice (Reason: %u=%s)\n",
  4189. req->bssid, req->reason_code,
  4190. ieee80211_get_reason_code_string(req->reason_code));
  4191. drv_mgd_prepare_tx(sdata->local, sdata);
  4192. ieee80211_send_deauth_disassoc(sdata, req->bssid,
  4193. IEEE80211_STYPE_DEAUTH,
  4194. req->reason_code, tx,
  4195. frame_buf);
  4196. ieee80211_destroy_auth_data(sdata, false);
  4197. ieee80211_report_disconnect(sdata, frame_buf,
  4198. sizeof(frame_buf), true,
  4199. req->reason_code);
  4200. return 0;
  4201. }
  4202. if (ifmgd->assoc_data &&
  4203. ether_addr_equal(ifmgd->assoc_data->bss->bssid, req->bssid)) {
  4204. sdata_info(sdata,
  4205. "aborting association with %pM by local choice (Reason: %u=%s)\n",
  4206. req->bssid, req->reason_code,
  4207. ieee80211_get_reason_code_string(req->reason_code));
  4208. drv_mgd_prepare_tx(sdata->local, sdata);
  4209. ieee80211_send_deauth_disassoc(sdata, req->bssid,
  4210. IEEE80211_STYPE_DEAUTH,
  4211. req->reason_code, tx,
  4212. frame_buf);
  4213. ieee80211_destroy_assoc_data(sdata, false, true);
  4214. ieee80211_report_disconnect(sdata, frame_buf,
  4215. sizeof(frame_buf), true,
  4216. req->reason_code);
  4217. return 0;
  4218. }
  4219. if (ifmgd->associated &&
  4220. ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
  4221. sdata_info(sdata,
  4222. "deauthenticating from %pM by local choice (Reason: %u=%s)\n",
  4223. req->bssid, req->reason_code,
  4224. ieee80211_get_reason_code_string(req->reason_code));
  4225. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  4226. req->reason_code, tx, frame_buf);
  4227. ieee80211_report_disconnect(sdata, frame_buf,
  4228. sizeof(frame_buf), true,
  4229. req->reason_code);
  4230. return 0;
  4231. }
  4232. return -ENOTCONN;
  4233. }
  4234. int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
  4235. struct cfg80211_disassoc_request *req)
  4236. {
  4237. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  4238. u8 bssid[ETH_ALEN];
  4239. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  4240. /*
  4241. * cfg80211 should catch this ... but it's racy since
  4242. * we can receive a disassoc frame, process it, hand it
  4243. * to cfg80211 while that's in a locked section already
  4244. * trying to tell us that the user wants to disconnect.
  4245. */
  4246. if (ifmgd->associated != req->bss)
  4247. return -ENOLINK;
  4248. sdata_info(sdata,
  4249. "disassociating from %pM by local choice (Reason: %u=%s)\n",
  4250. req->bss->bssid, req->reason_code, ieee80211_get_reason_code_string(req->reason_code));
  4251. memcpy(bssid, req->bss->bssid, ETH_ALEN);
  4252. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
  4253. req->reason_code, !req->local_state_change,
  4254. frame_buf);
  4255. ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
  4256. req->reason_code);
  4257. return 0;
  4258. }
  4259. void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
  4260. {
  4261. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  4262. /*
  4263. * Make sure some work items will not run after this,
  4264. * they will not do anything but might not have been
  4265. * cancelled when disconnecting.
  4266. */
  4267. cancel_work_sync(&ifmgd->monitor_work);
  4268. cancel_work_sync(&ifmgd->beacon_connection_loss_work);
  4269. cancel_work_sync(&ifmgd->request_smps_work);
  4270. cancel_work_sync(&ifmgd->csa_connection_drop_work);
  4271. cancel_work_sync(&ifmgd->chswitch_work);
  4272. cancel_delayed_work_sync(&ifmgd->tdls_peer_del_work);
  4273. sdata_lock(sdata);
  4274. if (ifmgd->assoc_data) {
  4275. struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
  4276. ieee80211_destroy_assoc_data(sdata, false, false);
  4277. cfg80211_assoc_timeout(sdata->dev, bss);
  4278. }
  4279. if (ifmgd->auth_data)
  4280. ieee80211_destroy_auth_data(sdata, false);
  4281. spin_lock_bh(&ifmgd->teardown_lock);
  4282. if (ifmgd->teardown_skb) {
  4283. kfree_skb(ifmgd->teardown_skb);
  4284. ifmgd->teardown_skb = NULL;
  4285. ifmgd->orig_teardown_skb = NULL;
  4286. }
  4287. spin_unlock_bh(&ifmgd->teardown_lock);
  4288. del_timer_sync(&ifmgd->timer);
  4289. sdata_unlock(sdata);
  4290. }
  4291. void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
  4292. enum nl80211_cqm_rssi_threshold_event rssi_event,
  4293. s32 rssi_level,
  4294. gfp_t gfp)
  4295. {
  4296. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  4297. trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level);
  4298. cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp);
  4299. }
  4300. EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
  4301. void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp)
  4302. {
  4303. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  4304. trace_api_cqm_beacon_loss_notify(sdata->local, sdata);
  4305. cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp);
  4306. }
  4307. EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify);