mlme.c 144 KB

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