mlme.c 159 KB

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