mlme.c 158 KB

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