driver_nl80211.c 280 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809
  1. /*
  2. * Driver interaction with Linux nl80211/cfg80211
  3. * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2003-2004, Instant802 Networks, Inc.
  5. * Copyright (c) 2005-2006, Devicescape Software, Inc.
  6. * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright (c) 2009-2010, Atheros Communications
  8. *
  9. * This software may be distributed under the terms of the BSD license.
  10. * See README for more details.
  11. */
  12. #include "includes.h"
  13. #include <sys/ioctl.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #include <net/if.h>
  18. #include <netlink/genl/genl.h>
  19. #include <netlink/genl/family.h>
  20. #include <netlink/genl/ctrl.h>
  21. #include <linux/rtnetlink.h>
  22. #include <netpacket/packet.h>
  23. #include <linux/filter.h>
  24. #include <linux/errqueue.h>
  25. #include "nl80211_copy.h"
  26. #include "common.h"
  27. #include "eloop.h"
  28. #include "utils/list.h"
  29. #include "common/ieee802_11_defs.h"
  30. #include "common/ieee802_11_common.h"
  31. #include "l2_packet/l2_packet.h"
  32. #include "netlink.h"
  33. #include "linux_ioctl.h"
  34. #include "radiotap.h"
  35. #include "radiotap_iter.h"
  36. #include "rfkill.h"
  37. #include "driver.h"
  38. #ifndef SO_WIFI_STATUS
  39. # if defined(__sparc__)
  40. # define SO_WIFI_STATUS 0x0025
  41. # elif defined(__parisc__)
  42. # define SO_WIFI_STATUS 0x4022
  43. # else
  44. # define SO_WIFI_STATUS 41
  45. # endif
  46. # define SCM_WIFI_STATUS SO_WIFI_STATUS
  47. #endif
  48. #ifndef SO_EE_ORIGIN_TXSTATUS
  49. #define SO_EE_ORIGIN_TXSTATUS 4
  50. #endif
  51. #ifndef PACKET_TX_TIMESTAMP
  52. #define PACKET_TX_TIMESTAMP 16
  53. #endif
  54. #ifdef ANDROID
  55. #include "android_drv.h"
  56. #endif /* ANDROID */
  57. #ifdef CONFIG_LIBNL20
  58. /* libnl 2.0 compatibility code */
  59. #define nl_handle nl_sock
  60. #define nl80211_handle_alloc nl_socket_alloc_cb
  61. #define nl80211_handle_destroy nl_socket_free
  62. #else
  63. /*
  64. * libnl 1.1 has a bug, it tries to allocate socket numbers densely
  65. * but when you free a socket again it will mess up its bitmap and
  66. * and use the wrong number the next time it needs a socket ID.
  67. * Therefore, we wrap the handle alloc/destroy and add our own pid
  68. * accounting.
  69. */
  70. static uint32_t port_bitmap[32] = { 0 };
  71. static struct nl_handle *nl80211_handle_alloc(void *cb)
  72. {
  73. struct nl_handle *handle;
  74. uint32_t pid = getpid() & 0x3FFFFF;
  75. int i;
  76. handle = nl_handle_alloc_cb(cb);
  77. for (i = 0; i < 1024; i++) {
  78. if (port_bitmap[i / 32] & (1 << (i % 32)))
  79. continue;
  80. port_bitmap[i / 32] |= 1 << (i % 32);
  81. pid += i << 22;
  82. break;
  83. }
  84. nl_socket_set_local_port(handle, pid);
  85. return handle;
  86. }
  87. static void nl80211_handle_destroy(struct nl_handle *handle)
  88. {
  89. uint32_t port = nl_socket_get_local_port(handle);
  90. port >>= 22;
  91. port_bitmap[port / 32] &= ~(1 << (port % 32));
  92. nl_handle_destroy(handle);
  93. }
  94. #endif /* CONFIG_LIBNL20 */
  95. static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
  96. {
  97. struct nl_handle *handle;
  98. handle = nl80211_handle_alloc(cb);
  99. if (handle == NULL) {
  100. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  101. "callbacks (%s)", dbg);
  102. return NULL;
  103. }
  104. if (genl_connect(handle)) {
  105. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
  106. "netlink (%s)", dbg);
  107. nl80211_handle_destroy(handle);
  108. return NULL;
  109. }
  110. return handle;
  111. }
  112. static void nl_destroy_handles(struct nl_handle **handle)
  113. {
  114. if (*handle == NULL)
  115. return;
  116. nl80211_handle_destroy(*handle);
  117. *handle = NULL;
  118. }
  119. #ifndef IFF_LOWER_UP
  120. #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
  121. #endif
  122. #ifndef IFF_DORMANT
  123. #define IFF_DORMANT 0x20000 /* driver signals dormant */
  124. #endif
  125. #ifndef IF_OPER_DORMANT
  126. #define IF_OPER_DORMANT 5
  127. #endif
  128. #ifndef IF_OPER_UP
  129. #define IF_OPER_UP 6
  130. #endif
  131. struct nl80211_global {
  132. struct dl_list interfaces;
  133. int if_add_ifindex;
  134. u64 if_add_wdevid;
  135. int if_add_wdevid_set;
  136. struct netlink_data *netlink;
  137. struct nl_cb *nl_cb;
  138. struct nl_handle *nl;
  139. int nl80211_id;
  140. int ioctl_sock; /* socket for ioctl() use */
  141. struct nl_handle *nl_event;
  142. };
  143. struct nl80211_wiphy_data {
  144. struct dl_list list;
  145. struct dl_list bsss;
  146. struct dl_list drvs;
  147. struct nl_handle *nl_beacons;
  148. struct nl_cb *nl_cb;
  149. int wiphy_idx;
  150. };
  151. static void nl80211_global_deinit(void *priv);
  152. struct i802_bss {
  153. struct wpa_driver_nl80211_data *drv;
  154. struct i802_bss *next;
  155. int ifindex;
  156. u64 wdev_id;
  157. char ifname[IFNAMSIZ + 1];
  158. char brname[IFNAMSIZ];
  159. unsigned int beacon_set:1;
  160. unsigned int added_if_into_bridge:1;
  161. unsigned int added_bridge:1;
  162. unsigned int in_deinit:1;
  163. unsigned int wdev_id_set:1;
  164. u8 addr[ETH_ALEN];
  165. int freq;
  166. int if_dynamic;
  167. void *ctx;
  168. struct nl_handle *nl_preq, *nl_mgmt;
  169. struct nl_cb *nl_cb;
  170. struct nl80211_wiphy_data *wiphy_data;
  171. struct dl_list wiphy_list;
  172. };
  173. struct wpa_driver_nl80211_data {
  174. struct nl80211_global *global;
  175. struct dl_list list;
  176. struct dl_list wiphy_list;
  177. char phyname[32];
  178. void *ctx;
  179. int ifindex;
  180. int if_removed;
  181. int if_disabled;
  182. int ignore_if_down_event;
  183. struct rfkill_data *rfkill;
  184. struct wpa_driver_capa capa;
  185. u8 *extended_capa, *extended_capa_mask;
  186. unsigned int extended_capa_len;
  187. int has_capability;
  188. int operstate;
  189. int scan_complete_events;
  190. struct nl_cb *nl_cb;
  191. u8 auth_bssid[ETH_ALEN];
  192. u8 auth_attempt_bssid[ETH_ALEN];
  193. u8 bssid[ETH_ALEN];
  194. u8 prev_bssid[ETH_ALEN];
  195. int associated;
  196. u8 ssid[32];
  197. size_t ssid_len;
  198. enum nl80211_iftype nlmode;
  199. enum nl80211_iftype ap_scan_as_station;
  200. unsigned int assoc_freq;
  201. int monitor_sock;
  202. int monitor_ifidx;
  203. int monitor_refcount;
  204. unsigned int disabled_11b_rates:1;
  205. unsigned int pending_remain_on_chan:1;
  206. unsigned int in_interface_list:1;
  207. unsigned int device_ap_sme:1;
  208. unsigned int poll_command_supported:1;
  209. unsigned int data_tx_status:1;
  210. unsigned int scan_for_auth:1;
  211. unsigned int retry_auth:1;
  212. unsigned int use_monitor:1;
  213. unsigned int ignore_next_local_disconnect:1;
  214. unsigned int allow_p2p_device:1;
  215. u64 remain_on_chan_cookie;
  216. u64 send_action_cookie;
  217. unsigned int last_mgmt_freq;
  218. struct wpa_driver_scan_filter *filter_ssids;
  219. size_t num_filter_ssids;
  220. struct i802_bss first_bss;
  221. int eapol_tx_sock;
  222. #ifdef HOSTAPD
  223. int eapol_sock; /* socket for EAPOL frames */
  224. int default_if_indices[16];
  225. int *if_indices;
  226. int num_if_indices;
  227. int last_freq;
  228. int last_freq_ht;
  229. #endif /* HOSTAPD */
  230. /* From failed authentication command */
  231. int auth_freq;
  232. u8 auth_bssid_[ETH_ALEN];
  233. u8 auth_ssid[32];
  234. size_t auth_ssid_len;
  235. int auth_alg;
  236. u8 *auth_ie;
  237. size_t auth_ie_len;
  238. u8 auth_wep_key[4][16];
  239. size_t auth_wep_key_len[4];
  240. int auth_wep_tx_keyidx;
  241. int auth_local_state_change;
  242. int auth_p2p;
  243. };
  244. static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
  245. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
  246. void *timeout_ctx);
  247. static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
  248. enum nl80211_iftype nlmode);
  249. static int
  250. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
  251. static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  252. const u8 *addr, int cmd, u16 reason_code,
  253. int local_state_change);
  254. static void nl80211_remove_monitor_interface(
  255. struct wpa_driver_nl80211_data *drv);
  256. static int nl80211_send_frame_cmd(struct i802_bss *bss,
  257. unsigned int freq, unsigned int wait,
  258. const u8 *buf, size_t buf_len, u64 *cookie,
  259. int no_cck, int no_ack, int offchanok);
  260. static int nl80211_register_frame(struct i802_bss *bss,
  261. struct nl_handle *hl_handle,
  262. u16 type, const u8 *match, size_t match_len);
  263. static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
  264. int report);
  265. #ifdef ANDROID
  266. static int android_pno_start(struct i802_bss *bss,
  267. struct wpa_driver_scan_params *params);
  268. static int android_pno_stop(struct i802_bss *bss);
  269. #endif /* ANDROID */
  270. #ifdef HOSTAPD
  271. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  272. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  273. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  274. static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
  275. enum wpa_driver_if_type type,
  276. const char *ifname);
  277. #else /* HOSTAPD */
  278. static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  279. {
  280. }
  281. static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  282. {
  283. }
  284. static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  285. {
  286. return 0;
  287. }
  288. #endif /* HOSTAPD */
  289. static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
  290. struct hostapd_freq_params *freq);
  291. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  292. int ifindex, int disabled);
  293. static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
  294. static int wpa_driver_nl80211_authenticate_retry(
  295. struct wpa_driver_nl80211_data *drv);
  296. static int i802_set_iface_flags(struct i802_bss *bss, int up);
  297. static const char * nl80211_command_to_string(enum nl80211_commands cmd)
  298. {
  299. #define C2S(x) case x: return #x;
  300. switch (cmd) {
  301. C2S(NL80211_CMD_UNSPEC)
  302. C2S(NL80211_CMD_GET_WIPHY)
  303. C2S(NL80211_CMD_SET_WIPHY)
  304. C2S(NL80211_CMD_NEW_WIPHY)
  305. C2S(NL80211_CMD_DEL_WIPHY)
  306. C2S(NL80211_CMD_GET_INTERFACE)
  307. C2S(NL80211_CMD_SET_INTERFACE)
  308. C2S(NL80211_CMD_NEW_INTERFACE)
  309. C2S(NL80211_CMD_DEL_INTERFACE)
  310. C2S(NL80211_CMD_GET_KEY)
  311. C2S(NL80211_CMD_SET_KEY)
  312. C2S(NL80211_CMD_NEW_KEY)
  313. C2S(NL80211_CMD_DEL_KEY)
  314. C2S(NL80211_CMD_GET_BEACON)
  315. C2S(NL80211_CMD_SET_BEACON)
  316. C2S(NL80211_CMD_START_AP)
  317. C2S(NL80211_CMD_STOP_AP)
  318. C2S(NL80211_CMD_GET_STATION)
  319. C2S(NL80211_CMD_SET_STATION)
  320. C2S(NL80211_CMD_NEW_STATION)
  321. C2S(NL80211_CMD_DEL_STATION)
  322. C2S(NL80211_CMD_GET_MPATH)
  323. C2S(NL80211_CMD_SET_MPATH)
  324. C2S(NL80211_CMD_NEW_MPATH)
  325. C2S(NL80211_CMD_DEL_MPATH)
  326. C2S(NL80211_CMD_SET_BSS)
  327. C2S(NL80211_CMD_SET_REG)
  328. C2S(NL80211_CMD_REQ_SET_REG)
  329. C2S(NL80211_CMD_GET_MESH_CONFIG)
  330. C2S(NL80211_CMD_SET_MESH_CONFIG)
  331. C2S(NL80211_CMD_SET_MGMT_EXTRA_IE)
  332. C2S(NL80211_CMD_GET_REG)
  333. C2S(NL80211_CMD_GET_SCAN)
  334. C2S(NL80211_CMD_TRIGGER_SCAN)
  335. C2S(NL80211_CMD_NEW_SCAN_RESULTS)
  336. C2S(NL80211_CMD_SCAN_ABORTED)
  337. C2S(NL80211_CMD_REG_CHANGE)
  338. C2S(NL80211_CMD_AUTHENTICATE)
  339. C2S(NL80211_CMD_ASSOCIATE)
  340. C2S(NL80211_CMD_DEAUTHENTICATE)
  341. C2S(NL80211_CMD_DISASSOCIATE)
  342. C2S(NL80211_CMD_MICHAEL_MIC_FAILURE)
  343. C2S(NL80211_CMD_REG_BEACON_HINT)
  344. C2S(NL80211_CMD_JOIN_IBSS)
  345. C2S(NL80211_CMD_LEAVE_IBSS)
  346. C2S(NL80211_CMD_TESTMODE)
  347. C2S(NL80211_CMD_CONNECT)
  348. C2S(NL80211_CMD_ROAM)
  349. C2S(NL80211_CMD_DISCONNECT)
  350. C2S(NL80211_CMD_SET_WIPHY_NETNS)
  351. C2S(NL80211_CMD_GET_SURVEY)
  352. C2S(NL80211_CMD_NEW_SURVEY_RESULTS)
  353. C2S(NL80211_CMD_SET_PMKSA)
  354. C2S(NL80211_CMD_DEL_PMKSA)
  355. C2S(NL80211_CMD_FLUSH_PMKSA)
  356. C2S(NL80211_CMD_REMAIN_ON_CHANNEL)
  357. C2S(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL)
  358. C2S(NL80211_CMD_SET_TX_BITRATE_MASK)
  359. C2S(NL80211_CMD_REGISTER_FRAME)
  360. C2S(NL80211_CMD_FRAME)
  361. C2S(NL80211_CMD_FRAME_TX_STATUS)
  362. C2S(NL80211_CMD_SET_POWER_SAVE)
  363. C2S(NL80211_CMD_GET_POWER_SAVE)
  364. C2S(NL80211_CMD_SET_CQM)
  365. C2S(NL80211_CMD_NOTIFY_CQM)
  366. C2S(NL80211_CMD_SET_CHANNEL)
  367. C2S(NL80211_CMD_SET_WDS_PEER)
  368. C2S(NL80211_CMD_FRAME_WAIT_CANCEL)
  369. C2S(NL80211_CMD_JOIN_MESH)
  370. C2S(NL80211_CMD_LEAVE_MESH)
  371. C2S(NL80211_CMD_UNPROT_DEAUTHENTICATE)
  372. C2S(NL80211_CMD_UNPROT_DISASSOCIATE)
  373. C2S(NL80211_CMD_NEW_PEER_CANDIDATE)
  374. C2S(NL80211_CMD_GET_WOWLAN)
  375. C2S(NL80211_CMD_SET_WOWLAN)
  376. C2S(NL80211_CMD_START_SCHED_SCAN)
  377. C2S(NL80211_CMD_STOP_SCHED_SCAN)
  378. C2S(NL80211_CMD_SCHED_SCAN_RESULTS)
  379. C2S(NL80211_CMD_SCHED_SCAN_STOPPED)
  380. C2S(NL80211_CMD_SET_REKEY_OFFLOAD)
  381. C2S(NL80211_CMD_PMKSA_CANDIDATE)
  382. C2S(NL80211_CMD_TDLS_OPER)
  383. C2S(NL80211_CMD_TDLS_MGMT)
  384. C2S(NL80211_CMD_UNEXPECTED_FRAME)
  385. C2S(NL80211_CMD_PROBE_CLIENT)
  386. C2S(NL80211_CMD_REGISTER_BEACONS)
  387. C2S(NL80211_CMD_UNEXPECTED_4ADDR_FRAME)
  388. C2S(NL80211_CMD_SET_NOACK_MAP)
  389. C2S(NL80211_CMD_CH_SWITCH_NOTIFY)
  390. C2S(NL80211_CMD_START_P2P_DEVICE)
  391. C2S(NL80211_CMD_STOP_P2P_DEVICE)
  392. C2S(NL80211_CMD_CONN_FAILED)
  393. C2S(NL80211_CMD_SET_MCAST_RATE)
  394. C2S(NL80211_CMD_SET_MAC_ACL)
  395. C2S(NL80211_CMD_RADAR_DETECT)
  396. C2S(NL80211_CMD_GET_PROTOCOL_FEATURES)
  397. C2S(NL80211_CMD_UPDATE_FT_IES)
  398. C2S(NL80211_CMD_FT_EVENT)
  399. C2S(NL80211_CMD_CRIT_PROTOCOL_START)
  400. C2S(NL80211_CMD_CRIT_PROTOCOL_STOP)
  401. default:
  402. return "NL80211_CMD_UNKNOWN";
  403. }
  404. #undef C2S
  405. }
  406. static int is_ap_interface(enum nl80211_iftype nlmode)
  407. {
  408. return (nlmode == NL80211_IFTYPE_AP ||
  409. nlmode == NL80211_IFTYPE_P2P_GO);
  410. }
  411. static int is_sta_interface(enum nl80211_iftype nlmode)
  412. {
  413. return (nlmode == NL80211_IFTYPE_STATION ||
  414. nlmode == NL80211_IFTYPE_P2P_CLIENT);
  415. }
  416. static int is_p2p_net_interface(enum nl80211_iftype nlmode)
  417. {
  418. return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
  419. nlmode == NL80211_IFTYPE_P2P_GO);
  420. }
  421. static void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
  422. {
  423. if (drv->associated)
  424. os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
  425. drv->associated = 0;
  426. os_memset(drv->bssid, 0, ETH_ALEN);
  427. }
  428. struct nl80211_bss_info_arg {
  429. struct wpa_driver_nl80211_data *drv;
  430. struct wpa_scan_results *res;
  431. unsigned int assoc_freq;
  432. u8 assoc_bssid[ETH_ALEN];
  433. };
  434. static int bss_info_handler(struct nl_msg *msg, void *arg);
  435. /* nl80211 code */
  436. static int ack_handler(struct nl_msg *msg, void *arg)
  437. {
  438. int *err = arg;
  439. *err = 0;
  440. return NL_STOP;
  441. }
  442. static int finish_handler(struct nl_msg *msg, void *arg)
  443. {
  444. int *ret = arg;
  445. *ret = 0;
  446. return NL_SKIP;
  447. }
  448. static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
  449. void *arg)
  450. {
  451. int *ret = arg;
  452. *ret = err->error;
  453. return NL_SKIP;
  454. }
  455. static int no_seq_check(struct nl_msg *msg, void *arg)
  456. {
  457. return NL_OK;
  458. }
  459. static int send_and_recv(struct nl80211_global *global,
  460. struct nl_handle *nl_handle, struct nl_msg *msg,
  461. int (*valid_handler)(struct nl_msg *, void *),
  462. void *valid_data)
  463. {
  464. struct nl_cb *cb;
  465. int err = -ENOMEM;
  466. cb = nl_cb_clone(global->nl_cb);
  467. if (!cb)
  468. goto out;
  469. err = nl_send_auto_complete(nl_handle, msg);
  470. if (err < 0)
  471. goto out;
  472. err = 1;
  473. nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
  474. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
  475. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
  476. if (valid_handler)
  477. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
  478. valid_handler, valid_data);
  479. while (err > 0)
  480. nl_recvmsgs(nl_handle, cb);
  481. out:
  482. nl_cb_put(cb);
  483. nlmsg_free(msg);
  484. return err;
  485. }
  486. static int send_and_recv_msgs_global(struct nl80211_global *global,
  487. struct nl_msg *msg,
  488. int (*valid_handler)(struct nl_msg *, void *),
  489. void *valid_data)
  490. {
  491. return send_and_recv(global, global->nl, msg, valid_handler,
  492. valid_data);
  493. }
  494. static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
  495. struct nl_msg *msg,
  496. int (*valid_handler)(struct nl_msg *, void *),
  497. void *valid_data)
  498. {
  499. return send_and_recv(drv->global, drv->global->nl, msg,
  500. valid_handler, valid_data);
  501. }
  502. struct family_data {
  503. const char *group;
  504. int id;
  505. };
  506. static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
  507. {
  508. if (bss->wdev_id_set)
  509. NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
  510. else
  511. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  512. return 0;
  513. nla_put_failure:
  514. return -1;
  515. }
  516. static int family_handler(struct nl_msg *msg, void *arg)
  517. {
  518. struct family_data *res = arg;
  519. struct nlattr *tb[CTRL_ATTR_MAX + 1];
  520. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  521. struct nlattr *mcgrp;
  522. int i;
  523. nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  524. genlmsg_attrlen(gnlh, 0), NULL);
  525. if (!tb[CTRL_ATTR_MCAST_GROUPS])
  526. return NL_SKIP;
  527. nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
  528. struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
  529. nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
  530. nla_len(mcgrp), NULL);
  531. if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
  532. !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
  533. os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
  534. res->group,
  535. nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
  536. continue;
  537. res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
  538. break;
  539. };
  540. return NL_SKIP;
  541. }
  542. static int nl_get_multicast_id(struct nl80211_global *global,
  543. const char *family, const char *group)
  544. {
  545. struct nl_msg *msg;
  546. int ret = -1;
  547. struct family_data res = { group, -ENOENT };
  548. msg = nlmsg_alloc();
  549. if (!msg)
  550. return -ENOMEM;
  551. genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
  552. 0, 0, CTRL_CMD_GETFAMILY, 0);
  553. NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
  554. ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
  555. msg = NULL;
  556. if (ret == 0)
  557. ret = res.id;
  558. nla_put_failure:
  559. nlmsg_free(msg);
  560. return ret;
  561. }
  562. static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
  563. struct nl_msg *msg, int flags, uint8_t cmd)
  564. {
  565. return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
  566. 0, flags, cmd, 0);
  567. }
  568. struct wiphy_idx_data {
  569. int wiphy_idx;
  570. enum nl80211_iftype nlmode;
  571. u8 *macaddr;
  572. };
  573. static int netdev_info_handler(struct nl_msg *msg, void *arg)
  574. {
  575. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  576. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  577. struct wiphy_idx_data *info = arg;
  578. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  579. genlmsg_attrlen(gnlh, 0), NULL);
  580. if (tb[NL80211_ATTR_WIPHY])
  581. info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
  582. if (tb[NL80211_ATTR_IFTYPE])
  583. info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
  584. if (tb[NL80211_ATTR_MAC] && info->macaddr)
  585. os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
  586. ETH_ALEN);
  587. return NL_SKIP;
  588. }
  589. static int nl80211_get_wiphy_index(struct i802_bss *bss)
  590. {
  591. struct nl_msg *msg;
  592. struct wiphy_idx_data data = {
  593. .wiphy_idx = -1,
  594. .macaddr = NULL,
  595. };
  596. msg = nlmsg_alloc();
  597. if (!msg)
  598. return NL80211_IFTYPE_UNSPECIFIED;
  599. nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
  600. if (nl80211_set_iface_id(msg, bss) < 0)
  601. goto nla_put_failure;
  602. if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
  603. return data.wiphy_idx;
  604. msg = NULL;
  605. nla_put_failure:
  606. nlmsg_free(msg);
  607. return -1;
  608. }
  609. static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
  610. {
  611. struct nl_msg *msg;
  612. struct wiphy_idx_data data = {
  613. .nlmode = NL80211_IFTYPE_UNSPECIFIED,
  614. .macaddr = NULL,
  615. };
  616. msg = nlmsg_alloc();
  617. if (!msg)
  618. return -1;
  619. nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
  620. if (nl80211_set_iface_id(msg, bss) < 0)
  621. goto nla_put_failure;
  622. if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
  623. return data.nlmode;
  624. msg = NULL;
  625. nla_put_failure:
  626. nlmsg_free(msg);
  627. return NL80211_IFTYPE_UNSPECIFIED;
  628. }
  629. #ifndef HOSTAPD
  630. static int nl80211_get_macaddr(struct i802_bss *bss)
  631. {
  632. struct nl_msg *msg;
  633. struct wiphy_idx_data data = {
  634. .macaddr = bss->addr,
  635. };
  636. msg = nlmsg_alloc();
  637. if (!msg)
  638. return NL80211_IFTYPE_UNSPECIFIED;
  639. nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
  640. if (nl80211_set_iface_id(msg, bss) < 0)
  641. goto nla_put_failure;
  642. return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
  643. nla_put_failure:
  644. nlmsg_free(msg);
  645. return NL80211_IFTYPE_UNSPECIFIED;
  646. }
  647. #endif /* HOSTAPD */
  648. static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
  649. struct nl80211_wiphy_data *w)
  650. {
  651. struct nl_msg *msg;
  652. int ret = -1;
  653. msg = nlmsg_alloc();
  654. if (!msg)
  655. return -1;
  656. nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
  657. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
  658. ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
  659. msg = NULL;
  660. if (ret) {
  661. wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
  662. "failed: ret=%d (%s)",
  663. ret, strerror(-ret));
  664. goto nla_put_failure;
  665. }
  666. ret = 0;
  667. nla_put_failure:
  668. nlmsg_free(msg);
  669. return ret;
  670. }
  671. static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
  672. {
  673. struct nl80211_wiphy_data *w = eloop_ctx;
  674. wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
  675. nl_recvmsgs(handle, w->nl_cb);
  676. }
  677. static int process_beacon_event(struct nl_msg *msg, void *arg)
  678. {
  679. struct nl80211_wiphy_data *w = arg;
  680. struct wpa_driver_nl80211_data *drv;
  681. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  682. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  683. union wpa_event_data event;
  684. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  685. genlmsg_attrlen(gnlh, 0), NULL);
  686. if (gnlh->cmd != NL80211_CMD_FRAME) {
  687. wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
  688. gnlh->cmd);
  689. return NL_SKIP;
  690. }
  691. if (!tb[NL80211_ATTR_FRAME])
  692. return NL_SKIP;
  693. dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
  694. wiphy_list) {
  695. os_memset(&event, 0, sizeof(event));
  696. event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
  697. event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
  698. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  699. }
  700. return NL_SKIP;
  701. }
  702. static struct nl80211_wiphy_data *
  703. nl80211_get_wiphy_data_ap(struct i802_bss *bss)
  704. {
  705. static DEFINE_DL_LIST(nl80211_wiphys);
  706. struct nl80211_wiphy_data *w;
  707. int wiphy_idx, found = 0;
  708. struct i802_bss *tmp_bss;
  709. if (bss->wiphy_data != NULL)
  710. return bss->wiphy_data;
  711. wiphy_idx = nl80211_get_wiphy_index(bss);
  712. dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
  713. if (w->wiphy_idx == wiphy_idx)
  714. goto add;
  715. }
  716. /* alloc new one */
  717. w = os_zalloc(sizeof(*w));
  718. if (w == NULL)
  719. return NULL;
  720. w->wiphy_idx = wiphy_idx;
  721. dl_list_init(&w->bsss);
  722. dl_list_init(&w->drvs);
  723. w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  724. if (!w->nl_cb) {
  725. os_free(w);
  726. return NULL;
  727. }
  728. nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
  729. nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
  730. w);
  731. w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
  732. "wiphy beacons");
  733. if (w->nl_beacons == NULL) {
  734. os_free(w);
  735. return NULL;
  736. }
  737. if (nl80211_register_beacons(bss->drv, w)) {
  738. nl_destroy_handles(&w->nl_beacons);
  739. os_free(w);
  740. return NULL;
  741. }
  742. eloop_register_read_sock(nl_socket_get_fd(w->nl_beacons),
  743. nl80211_recv_beacons, w, w->nl_beacons);
  744. dl_list_add(&nl80211_wiphys, &w->list);
  745. add:
  746. /* drv entry for this bss already there? */
  747. dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
  748. if (tmp_bss->drv == bss->drv) {
  749. found = 1;
  750. break;
  751. }
  752. }
  753. /* if not add it */
  754. if (!found)
  755. dl_list_add(&w->drvs, &bss->drv->wiphy_list);
  756. dl_list_add(&w->bsss, &bss->wiphy_list);
  757. bss->wiphy_data = w;
  758. return w;
  759. }
  760. static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
  761. {
  762. struct nl80211_wiphy_data *w = bss->wiphy_data;
  763. struct i802_bss *tmp_bss;
  764. int found = 0;
  765. if (w == NULL)
  766. return;
  767. bss->wiphy_data = NULL;
  768. dl_list_del(&bss->wiphy_list);
  769. /* still any for this drv present? */
  770. dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
  771. if (tmp_bss->drv == bss->drv) {
  772. found = 1;
  773. break;
  774. }
  775. }
  776. /* if not remove it */
  777. if (!found)
  778. dl_list_del(&bss->drv->wiphy_list);
  779. if (!dl_list_empty(&w->bsss))
  780. return;
  781. eloop_unregister_read_sock(nl_socket_get_fd(w->nl_beacons));
  782. nl_cb_put(w->nl_cb);
  783. nl_destroy_handles(&w->nl_beacons);
  784. dl_list_del(&w->list);
  785. os_free(w);
  786. }
  787. static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
  788. {
  789. struct i802_bss *bss = priv;
  790. struct wpa_driver_nl80211_data *drv = bss->drv;
  791. if (!drv->associated)
  792. return -1;
  793. os_memcpy(bssid, drv->bssid, ETH_ALEN);
  794. return 0;
  795. }
  796. static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
  797. {
  798. struct i802_bss *bss = priv;
  799. struct wpa_driver_nl80211_data *drv = bss->drv;
  800. if (!drv->associated)
  801. return -1;
  802. os_memcpy(ssid, drv->ssid, drv->ssid_len);
  803. return drv->ssid_len;
  804. }
  805. static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
  806. char *buf, size_t len, int del)
  807. {
  808. union wpa_event_data event;
  809. os_memset(&event, 0, sizeof(event));
  810. if (len > sizeof(event.interface_status.ifname))
  811. len = sizeof(event.interface_status.ifname) - 1;
  812. os_memcpy(event.interface_status.ifname, buf, len);
  813. event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
  814. EVENT_INTERFACE_ADDED;
  815. wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
  816. del ? "DEL" : "NEW",
  817. event.interface_status.ifname,
  818. del ? "removed" : "added");
  819. if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
  820. if (del) {
  821. if (drv->if_removed) {
  822. wpa_printf(MSG_DEBUG, "nl80211: if_removed "
  823. "already set - ignore event");
  824. return;
  825. }
  826. drv->if_removed = 1;
  827. } else {
  828. if (if_nametoindex(drv->first_bss.ifname) == 0) {
  829. wpa_printf(MSG_DEBUG, "nl80211: Interface %s "
  830. "does not exist - ignore "
  831. "RTM_NEWLINK",
  832. drv->first_bss.ifname);
  833. return;
  834. }
  835. if (!drv->if_removed) {
  836. wpa_printf(MSG_DEBUG, "nl80211: if_removed "
  837. "already cleared - ignore event");
  838. return;
  839. }
  840. drv->if_removed = 0;
  841. }
  842. }
  843. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
  844. }
  845. static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
  846. u8 *buf, size_t len)
  847. {
  848. int attrlen, rta_len;
  849. struct rtattr *attr;
  850. attrlen = len;
  851. attr = (struct rtattr *) buf;
  852. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  853. while (RTA_OK(attr, attrlen)) {
  854. if (attr->rta_type == IFLA_IFNAME) {
  855. if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
  856. == 0)
  857. return 1;
  858. else
  859. break;
  860. }
  861. attr = RTA_NEXT(attr, attrlen);
  862. }
  863. return 0;
  864. }
  865. static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
  866. int ifindex, u8 *buf, size_t len)
  867. {
  868. if (drv->ifindex == ifindex)
  869. return 1;
  870. if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
  871. wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
  872. "interface");
  873. wpa_driver_nl80211_finish_drv_init(drv);
  874. return 1;
  875. }
  876. return 0;
  877. }
  878. static struct wpa_driver_nl80211_data *
  879. nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
  880. {
  881. struct wpa_driver_nl80211_data *drv;
  882. dl_list_for_each(drv, &global->interfaces,
  883. struct wpa_driver_nl80211_data, list) {
  884. if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
  885. have_ifidx(drv, idx))
  886. return drv;
  887. }
  888. return NULL;
  889. }
  890. static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
  891. struct ifinfomsg *ifi,
  892. u8 *buf, size_t len)
  893. {
  894. struct nl80211_global *global = ctx;
  895. struct wpa_driver_nl80211_data *drv;
  896. int attrlen, rta_len;
  897. struct rtattr *attr;
  898. u32 brid = 0;
  899. char namebuf[IFNAMSIZ];
  900. drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
  901. if (!drv) {
  902. wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
  903. "ifindex %d", ifi->ifi_index);
  904. return;
  905. }
  906. wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
  907. "(%s%s%s%s)",
  908. drv->operstate, ifi->ifi_flags,
  909. (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
  910. (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
  911. (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
  912. (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
  913. if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
  914. if (if_indextoname(ifi->ifi_index, namebuf) &&
  915. linux_iface_up(drv->global->ioctl_sock,
  916. drv->first_bss.ifname) > 0) {
  917. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
  918. "event since interface %s is up", namebuf);
  919. return;
  920. }
  921. wpa_printf(MSG_DEBUG, "nl80211: Interface down");
  922. if (drv->ignore_if_down_event) {
  923. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
  924. "event generated by mode change");
  925. drv->ignore_if_down_event = 0;
  926. } else {
  927. drv->if_disabled = 1;
  928. wpa_supplicant_event(drv->ctx,
  929. EVENT_INTERFACE_DISABLED, NULL);
  930. }
  931. }
  932. if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
  933. if (if_indextoname(ifi->ifi_index, namebuf) &&
  934. linux_iface_up(drv->global->ioctl_sock,
  935. drv->first_bss.ifname) == 0) {
  936. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
  937. "event since interface %s is down",
  938. namebuf);
  939. } else if (if_nametoindex(drv->first_bss.ifname) == 0) {
  940. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
  941. "event since interface %s does not exist",
  942. drv->first_bss.ifname);
  943. } else if (drv->if_removed) {
  944. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
  945. "event since interface %s is marked "
  946. "removed", drv->first_bss.ifname);
  947. } else {
  948. wpa_printf(MSG_DEBUG, "nl80211: Interface up");
  949. drv->if_disabled = 0;
  950. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
  951. NULL);
  952. }
  953. }
  954. /*
  955. * Some drivers send the association event before the operup event--in
  956. * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
  957. * fails. This will hit us when wpa_supplicant does not need to do
  958. * IEEE 802.1X authentication
  959. */
  960. if (drv->operstate == 1 &&
  961. (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
  962. !(ifi->ifi_flags & IFF_RUNNING))
  963. netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
  964. -1, IF_OPER_UP);
  965. attrlen = len;
  966. attr = (struct rtattr *) buf;
  967. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  968. while (RTA_OK(attr, attrlen)) {
  969. if (attr->rta_type == IFLA_IFNAME) {
  970. wpa_driver_nl80211_event_link(
  971. drv,
  972. ((char *) attr) + rta_len,
  973. attr->rta_len - rta_len, 0);
  974. } else if (attr->rta_type == IFLA_MASTER)
  975. brid = nla_get_u32((struct nlattr *) attr);
  976. attr = RTA_NEXT(attr, attrlen);
  977. }
  978. if (ifi->ifi_family == AF_BRIDGE && brid) {
  979. /* device has been added to bridge */
  980. if_indextoname(brid, namebuf);
  981. wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
  982. brid, namebuf);
  983. add_ifidx(drv, brid);
  984. }
  985. }
  986. static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
  987. struct ifinfomsg *ifi,
  988. u8 *buf, size_t len)
  989. {
  990. struct nl80211_global *global = ctx;
  991. struct wpa_driver_nl80211_data *drv;
  992. int attrlen, rta_len;
  993. struct rtattr *attr;
  994. u32 brid = 0;
  995. drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
  996. if (!drv) {
  997. wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
  998. "foreign ifindex %d", ifi->ifi_index);
  999. return;
  1000. }
  1001. attrlen = len;
  1002. attr = (struct rtattr *) buf;
  1003. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  1004. while (RTA_OK(attr, attrlen)) {
  1005. if (attr->rta_type == IFLA_IFNAME) {
  1006. wpa_driver_nl80211_event_link(
  1007. drv,
  1008. ((char *) attr) + rta_len,
  1009. attr->rta_len - rta_len, 1);
  1010. } else if (attr->rta_type == IFLA_MASTER)
  1011. brid = nla_get_u32((struct nlattr *) attr);
  1012. attr = RTA_NEXT(attr, attrlen);
  1013. }
  1014. if (ifi->ifi_family == AF_BRIDGE && brid) {
  1015. /* device has been removed from bridge */
  1016. char namebuf[IFNAMSIZ];
  1017. if_indextoname(brid, namebuf);
  1018. wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
  1019. "%s", brid, namebuf);
  1020. del_ifidx(drv, brid);
  1021. }
  1022. }
  1023. static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
  1024. const u8 *frame, size_t len)
  1025. {
  1026. const struct ieee80211_mgmt *mgmt;
  1027. union wpa_event_data event;
  1028. wpa_printf(MSG_DEBUG, "nl80211: Authenticate event");
  1029. mgmt = (const struct ieee80211_mgmt *) frame;
  1030. if (len < 24 + sizeof(mgmt->u.auth)) {
  1031. wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
  1032. "frame");
  1033. return;
  1034. }
  1035. os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
  1036. os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
  1037. os_memset(&event, 0, sizeof(event));
  1038. os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
  1039. event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
  1040. event.auth.auth_transaction =
  1041. le_to_host16(mgmt->u.auth.auth_transaction);
  1042. event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
  1043. if (len > 24 + sizeof(mgmt->u.auth)) {
  1044. event.auth.ies = mgmt->u.auth.variable;
  1045. event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
  1046. }
  1047. wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
  1048. }
  1049. static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
  1050. {
  1051. struct nl_msg *msg;
  1052. int ret;
  1053. struct nl80211_bss_info_arg arg;
  1054. os_memset(&arg, 0, sizeof(arg));
  1055. msg = nlmsg_alloc();
  1056. if (!msg)
  1057. goto nla_put_failure;
  1058. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
  1059. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1060. arg.drv = drv;
  1061. ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
  1062. msg = NULL;
  1063. if (ret == 0) {
  1064. wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
  1065. "associated BSS from scan results: %u MHz",
  1066. arg.assoc_freq);
  1067. if (arg.assoc_freq)
  1068. drv->assoc_freq = arg.assoc_freq;
  1069. return drv->assoc_freq;
  1070. }
  1071. wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
  1072. "(%s)", ret, strerror(-ret));
  1073. nla_put_failure:
  1074. nlmsg_free(msg);
  1075. return drv->assoc_freq;
  1076. }
  1077. static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
  1078. const u8 *frame, size_t len)
  1079. {
  1080. const struct ieee80211_mgmt *mgmt;
  1081. union wpa_event_data event;
  1082. u16 status;
  1083. wpa_printf(MSG_DEBUG, "nl80211: Associate event");
  1084. mgmt = (const struct ieee80211_mgmt *) frame;
  1085. if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
  1086. wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
  1087. "frame");
  1088. return;
  1089. }
  1090. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  1091. if (status != WLAN_STATUS_SUCCESS) {
  1092. os_memset(&event, 0, sizeof(event));
  1093. event.assoc_reject.bssid = mgmt->bssid;
  1094. if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
  1095. event.assoc_reject.resp_ies =
  1096. (u8 *) mgmt->u.assoc_resp.variable;
  1097. event.assoc_reject.resp_ies_len =
  1098. len - 24 - sizeof(mgmt->u.assoc_resp);
  1099. }
  1100. event.assoc_reject.status_code = status;
  1101. wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
  1102. return;
  1103. }
  1104. drv->associated = 1;
  1105. os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
  1106. os_memcpy(drv->prev_bssid, mgmt->sa, ETH_ALEN);
  1107. os_memset(&event, 0, sizeof(event));
  1108. if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
  1109. event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
  1110. event.assoc_info.resp_ies_len =
  1111. len - 24 - sizeof(mgmt->u.assoc_resp);
  1112. }
  1113. event.assoc_info.freq = drv->assoc_freq;
  1114. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
  1115. }
  1116. static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
  1117. enum nl80211_commands cmd, struct nlattr *status,
  1118. struct nlattr *addr, struct nlattr *req_ie,
  1119. struct nlattr *resp_ie)
  1120. {
  1121. union wpa_event_data event;
  1122. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  1123. /*
  1124. * Avoid reporting two association events that would confuse
  1125. * the core code.
  1126. */
  1127. wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
  1128. "when using userspace SME", cmd);
  1129. return;
  1130. }
  1131. if (cmd == NL80211_CMD_CONNECT)
  1132. wpa_printf(MSG_DEBUG, "nl80211: Connect event");
  1133. else if (cmd == NL80211_CMD_ROAM)
  1134. wpa_printf(MSG_DEBUG, "nl80211: Roam event");
  1135. os_memset(&event, 0, sizeof(event));
  1136. if (cmd == NL80211_CMD_CONNECT &&
  1137. nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
  1138. if (addr)
  1139. event.assoc_reject.bssid = nla_data(addr);
  1140. if (resp_ie) {
  1141. event.assoc_reject.resp_ies = nla_data(resp_ie);
  1142. event.assoc_reject.resp_ies_len = nla_len(resp_ie);
  1143. }
  1144. event.assoc_reject.status_code = nla_get_u16(status);
  1145. wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
  1146. return;
  1147. }
  1148. drv->associated = 1;
  1149. if (addr) {
  1150. os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
  1151. os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
  1152. }
  1153. if (req_ie) {
  1154. event.assoc_info.req_ies = nla_data(req_ie);
  1155. event.assoc_info.req_ies_len = nla_len(req_ie);
  1156. }
  1157. if (resp_ie) {
  1158. event.assoc_info.resp_ies = nla_data(resp_ie);
  1159. event.assoc_info.resp_ies_len = nla_len(resp_ie);
  1160. }
  1161. event.assoc_info.freq = nl80211_get_assoc_freq(drv);
  1162. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
  1163. }
  1164. static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
  1165. struct nlattr *reason, struct nlattr *addr,
  1166. struct nlattr *by_ap)
  1167. {
  1168. union wpa_event_data data;
  1169. unsigned int locally_generated = by_ap == NULL;
  1170. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  1171. /*
  1172. * Avoid reporting two disassociation events that could
  1173. * confuse the core code.
  1174. */
  1175. wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
  1176. "event when using userspace SME");
  1177. return;
  1178. }
  1179. if (drv->ignore_next_local_disconnect) {
  1180. drv->ignore_next_local_disconnect = 0;
  1181. if (locally_generated) {
  1182. wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
  1183. "event triggered during reassociation");
  1184. return;
  1185. }
  1186. wpa_printf(MSG_WARNING, "nl80211: Was expecting local "
  1187. "disconnect but got another disconnect "
  1188. "event first");
  1189. }
  1190. wpa_printf(MSG_DEBUG, "nl80211: Disconnect event");
  1191. nl80211_mark_disconnected(drv);
  1192. os_memset(&data, 0, sizeof(data));
  1193. if (reason)
  1194. data.deauth_info.reason_code = nla_get_u16(reason);
  1195. data.deauth_info.locally_generated = by_ap == NULL;
  1196. wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, &data);
  1197. }
  1198. static void mlme_event_ch_switch(struct wpa_driver_nl80211_data *drv,
  1199. struct nlattr *freq, struct nlattr *type)
  1200. {
  1201. union wpa_event_data data;
  1202. int ht_enabled = 1;
  1203. int chan_offset = 0;
  1204. wpa_printf(MSG_DEBUG, "nl80211: Channel switch event");
  1205. if (!freq || !type)
  1206. return;
  1207. switch (nla_get_u32(type)) {
  1208. case NL80211_CHAN_NO_HT:
  1209. ht_enabled = 0;
  1210. break;
  1211. case NL80211_CHAN_HT20:
  1212. break;
  1213. case NL80211_CHAN_HT40PLUS:
  1214. chan_offset = 1;
  1215. break;
  1216. case NL80211_CHAN_HT40MINUS:
  1217. chan_offset = -1;
  1218. break;
  1219. }
  1220. data.ch_switch.freq = nla_get_u32(freq);
  1221. data.ch_switch.ht_enabled = ht_enabled;
  1222. data.ch_switch.ch_offset = chan_offset;
  1223. wpa_supplicant_event(drv->ctx, EVENT_CH_SWITCH, &data);
  1224. }
  1225. static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
  1226. enum nl80211_commands cmd, struct nlattr *addr)
  1227. {
  1228. union wpa_event_data event;
  1229. enum wpa_event_type ev;
  1230. if (nla_len(addr) != ETH_ALEN)
  1231. return;
  1232. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
  1233. cmd, MAC2STR((u8 *) nla_data(addr)));
  1234. if (cmd == NL80211_CMD_AUTHENTICATE)
  1235. ev = EVENT_AUTH_TIMED_OUT;
  1236. else if (cmd == NL80211_CMD_ASSOCIATE)
  1237. ev = EVENT_ASSOC_TIMED_OUT;
  1238. else
  1239. return;
  1240. os_memset(&event, 0, sizeof(event));
  1241. os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
  1242. wpa_supplicant_event(drv->ctx, ev, &event);
  1243. }
  1244. static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
  1245. struct nlattr *freq, struct nlattr *sig,
  1246. const u8 *frame, size_t len)
  1247. {
  1248. const struct ieee80211_mgmt *mgmt;
  1249. union wpa_event_data event;
  1250. u16 fc, stype;
  1251. int ssi_signal = 0;
  1252. int rx_freq = 0;
  1253. wpa_printf(MSG_MSGDUMP, "nl80211: Frame event");
  1254. mgmt = (const struct ieee80211_mgmt *) frame;
  1255. if (len < 24) {
  1256. wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
  1257. return;
  1258. }
  1259. fc = le_to_host16(mgmt->frame_control);
  1260. stype = WLAN_FC_GET_STYPE(fc);
  1261. if (sig)
  1262. ssi_signal = (s32) nla_get_u32(sig);
  1263. os_memset(&event, 0, sizeof(event));
  1264. if (freq) {
  1265. event.rx_action.freq = nla_get_u32(freq);
  1266. rx_freq = drv->last_mgmt_freq = event.rx_action.freq;
  1267. }
  1268. wpa_printf(MSG_DEBUG,
  1269. "nl80211: RX frame freq=%d ssi_signal=%d stype=%u len=%u",
  1270. rx_freq, ssi_signal, stype, (unsigned int) len);
  1271. if (stype == WLAN_FC_STYPE_ACTION) {
  1272. event.rx_action.da = mgmt->da;
  1273. event.rx_action.sa = mgmt->sa;
  1274. event.rx_action.bssid = mgmt->bssid;
  1275. event.rx_action.category = mgmt->u.action.category;
  1276. event.rx_action.data = &mgmt->u.action.category + 1;
  1277. event.rx_action.len = frame + len - event.rx_action.data;
  1278. wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
  1279. } else {
  1280. event.rx_mgmt.frame = frame;
  1281. event.rx_mgmt.frame_len = len;
  1282. event.rx_mgmt.ssi_signal = ssi_signal;
  1283. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  1284. }
  1285. }
  1286. static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
  1287. struct nlattr *cookie, const u8 *frame,
  1288. size_t len, struct nlattr *ack)
  1289. {
  1290. union wpa_event_data event;
  1291. const struct ieee80211_hdr *hdr;
  1292. u16 fc;
  1293. wpa_printf(MSG_DEBUG, "nl80211: Frame TX status event");
  1294. if (!is_ap_interface(drv->nlmode)) {
  1295. u64 cookie_val;
  1296. if (!cookie)
  1297. return;
  1298. cookie_val = nla_get_u64(cookie);
  1299. wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
  1300. " cookie=0%llx%s (ack=%d)",
  1301. (long long unsigned int) cookie_val,
  1302. cookie_val == drv->send_action_cookie ?
  1303. " (match)" : " (unknown)", ack != NULL);
  1304. if (cookie_val != drv->send_action_cookie)
  1305. return;
  1306. }
  1307. hdr = (const struct ieee80211_hdr *) frame;
  1308. fc = le_to_host16(hdr->frame_control);
  1309. os_memset(&event, 0, sizeof(event));
  1310. event.tx_status.type = WLAN_FC_GET_TYPE(fc);
  1311. event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
  1312. event.tx_status.dst = hdr->addr1;
  1313. event.tx_status.data = frame;
  1314. event.tx_status.data_len = len;
  1315. event.tx_status.ack = ack != NULL;
  1316. wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
  1317. }
  1318. static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
  1319. enum wpa_event_type type,
  1320. const u8 *frame, size_t len)
  1321. {
  1322. const struct ieee80211_mgmt *mgmt;
  1323. union wpa_event_data event;
  1324. const u8 *bssid = NULL;
  1325. u16 reason_code = 0;
  1326. if (type == EVENT_DEAUTH)
  1327. wpa_printf(MSG_DEBUG, "nl80211: Deauthenticate event");
  1328. else
  1329. wpa_printf(MSG_DEBUG, "nl80211: Disassociate event");
  1330. mgmt = (const struct ieee80211_mgmt *) frame;
  1331. if (len >= 24) {
  1332. bssid = mgmt->bssid;
  1333. if ((drv->capa.flags & WPA_DRIVER_FLAGS_SME) &&
  1334. !drv->associated &&
  1335. os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0 &&
  1336. os_memcmp(bssid, drv->auth_attempt_bssid, ETH_ALEN) != 0 &&
  1337. os_memcmp(bssid, drv->prev_bssid, ETH_ALEN) == 0) {
  1338. /*
  1339. * Avoid issues with some roaming cases where
  1340. * disconnection event for the old AP may show up after
  1341. * we have started connection with the new AP.
  1342. */
  1343. wpa_printf(MSG_DEBUG, "nl80211: Ignore deauth/disassoc event from old AP " MACSTR " when already authenticating with " MACSTR,
  1344. MAC2STR(bssid),
  1345. MAC2STR(drv->auth_attempt_bssid));
  1346. return;
  1347. }
  1348. if (drv->associated != 0 &&
  1349. os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
  1350. os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
  1351. /*
  1352. * We have presumably received this deauth as a
  1353. * response to a clear_state_mismatch() outgoing
  1354. * deauth. Don't let it take us offline!
  1355. */
  1356. wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
  1357. "from Unknown BSSID " MACSTR " -- ignoring",
  1358. MAC2STR(bssid));
  1359. return;
  1360. }
  1361. }
  1362. nl80211_mark_disconnected(drv);
  1363. os_memset(&event, 0, sizeof(event));
  1364. /* Note: Same offset for Reason Code in both frame subtypes */
  1365. if (len >= 24 + sizeof(mgmt->u.deauth))
  1366. reason_code = le_to_host16(mgmt->u.deauth.reason_code);
  1367. if (type == EVENT_DISASSOC) {
  1368. event.disassoc_info.locally_generated =
  1369. !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
  1370. event.disassoc_info.addr = bssid;
  1371. event.disassoc_info.reason_code = reason_code;
  1372. if (frame + len > mgmt->u.disassoc.variable) {
  1373. event.disassoc_info.ie = mgmt->u.disassoc.variable;
  1374. event.disassoc_info.ie_len = frame + len -
  1375. mgmt->u.disassoc.variable;
  1376. }
  1377. } else {
  1378. event.deauth_info.locally_generated =
  1379. !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
  1380. event.deauth_info.addr = bssid;
  1381. event.deauth_info.reason_code = reason_code;
  1382. if (frame + len > mgmt->u.deauth.variable) {
  1383. event.deauth_info.ie = mgmt->u.deauth.variable;
  1384. event.deauth_info.ie_len = frame + len -
  1385. mgmt->u.deauth.variable;
  1386. }
  1387. }
  1388. wpa_supplicant_event(drv->ctx, type, &event);
  1389. }
  1390. static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
  1391. enum wpa_event_type type,
  1392. const u8 *frame, size_t len)
  1393. {
  1394. const struct ieee80211_mgmt *mgmt;
  1395. union wpa_event_data event;
  1396. u16 reason_code = 0;
  1397. if (type == EVENT_UNPROT_DEAUTH)
  1398. wpa_printf(MSG_DEBUG, "nl80211: Unprot Deauthenticate event");
  1399. else
  1400. wpa_printf(MSG_DEBUG, "nl80211: Unprot Disassociate event");
  1401. if (len < 24)
  1402. return;
  1403. mgmt = (const struct ieee80211_mgmt *) frame;
  1404. os_memset(&event, 0, sizeof(event));
  1405. /* Note: Same offset for Reason Code in both frame subtypes */
  1406. if (len >= 24 + sizeof(mgmt->u.deauth))
  1407. reason_code = le_to_host16(mgmt->u.deauth.reason_code);
  1408. if (type == EVENT_UNPROT_DISASSOC) {
  1409. event.unprot_disassoc.sa = mgmt->sa;
  1410. event.unprot_disassoc.da = mgmt->da;
  1411. event.unprot_disassoc.reason_code = reason_code;
  1412. } else {
  1413. event.unprot_deauth.sa = mgmt->sa;
  1414. event.unprot_deauth.da = mgmt->da;
  1415. event.unprot_deauth.reason_code = reason_code;
  1416. }
  1417. wpa_supplicant_event(drv->ctx, type, &event);
  1418. }
  1419. static void mlme_event(struct i802_bss *bss,
  1420. enum nl80211_commands cmd, struct nlattr *frame,
  1421. struct nlattr *addr, struct nlattr *timed_out,
  1422. struct nlattr *freq, struct nlattr *ack,
  1423. struct nlattr *cookie, struct nlattr *sig)
  1424. {
  1425. struct wpa_driver_nl80211_data *drv = bss->drv;
  1426. const u8 *data;
  1427. size_t len;
  1428. if (timed_out && addr) {
  1429. mlme_timeout_event(drv, cmd, addr);
  1430. return;
  1431. }
  1432. if (frame == NULL) {
  1433. wpa_printf(MSG_DEBUG,
  1434. "nl80211: MLME event %d (%s) without frame data",
  1435. cmd, nl80211_command_to_string(cmd));
  1436. return;
  1437. }
  1438. data = nla_data(frame);
  1439. len = nla_len(frame);
  1440. if (len < 4 + 2 * ETH_ALEN) {
  1441. wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s("
  1442. MACSTR ") - too short",
  1443. cmd, nl80211_command_to_string(cmd), bss->ifname,
  1444. MAC2STR(bss->addr));
  1445. return;
  1446. }
  1447. wpa_printf(MSG_MSGDUMP, "nl80211: MLME event %d (%s) on %s(" MACSTR
  1448. ") A1=" MACSTR " A2=" MACSTR, cmd,
  1449. nl80211_command_to_string(cmd), bss->ifname,
  1450. MAC2STR(bss->addr), MAC2STR(data + 4),
  1451. MAC2STR(data + 4 + ETH_ALEN));
  1452. if (cmd != NL80211_CMD_FRAME_TX_STATUS && !(data[4] & 0x01) &&
  1453. os_memcmp(bss->addr, data + 4, ETH_ALEN) != 0 &&
  1454. os_memcmp(bss->addr, data + 4 + ETH_ALEN, ETH_ALEN) != 0) {
  1455. wpa_printf(MSG_MSGDUMP, "nl80211: %s: Ignore MLME frame event "
  1456. "for foreign address", bss->ifname);
  1457. return;
  1458. }
  1459. wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
  1460. nla_data(frame), nla_len(frame));
  1461. switch (cmd) {
  1462. case NL80211_CMD_AUTHENTICATE:
  1463. mlme_event_auth(drv, nla_data(frame), nla_len(frame));
  1464. break;
  1465. case NL80211_CMD_ASSOCIATE:
  1466. mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
  1467. break;
  1468. case NL80211_CMD_DEAUTHENTICATE:
  1469. mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
  1470. nla_data(frame), nla_len(frame));
  1471. break;
  1472. case NL80211_CMD_DISASSOCIATE:
  1473. mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
  1474. nla_data(frame), nla_len(frame));
  1475. break;
  1476. case NL80211_CMD_FRAME:
  1477. mlme_event_mgmt(drv, freq, sig, nla_data(frame),
  1478. nla_len(frame));
  1479. break;
  1480. case NL80211_CMD_FRAME_TX_STATUS:
  1481. mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
  1482. nla_len(frame), ack);
  1483. break;
  1484. case NL80211_CMD_UNPROT_DEAUTHENTICATE:
  1485. mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
  1486. nla_data(frame), nla_len(frame));
  1487. break;
  1488. case NL80211_CMD_UNPROT_DISASSOCIATE:
  1489. mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
  1490. nla_data(frame), nla_len(frame));
  1491. break;
  1492. default:
  1493. break;
  1494. }
  1495. }
  1496. static void mlme_event_michael_mic_failure(struct i802_bss *bss,
  1497. struct nlattr *tb[])
  1498. {
  1499. union wpa_event_data data;
  1500. wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
  1501. os_memset(&data, 0, sizeof(data));
  1502. if (tb[NL80211_ATTR_MAC]) {
  1503. wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
  1504. nla_data(tb[NL80211_ATTR_MAC]),
  1505. nla_len(tb[NL80211_ATTR_MAC]));
  1506. data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
  1507. }
  1508. if (tb[NL80211_ATTR_KEY_SEQ]) {
  1509. wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
  1510. nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  1511. nla_len(tb[NL80211_ATTR_KEY_SEQ]));
  1512. }
  1513. if (tb[NL80211_ATTR_KEY_TYPE]) {
  1514. enum nl80211_key_type key_type =
  1515. nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
  1516. wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
  1517. if (key_type == NL80211_KEYTYPE_PAIRWISE)
  1518. data.michael_mic_failure.unicast = 1;
  1519. } else
  1520. data.michael_mic_failure.unicast = 1;
  1521. if (tb[NL80211_ATTR_KEY_IDX]) {
  1522. u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
  1523. wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
  1524. }
  1525. wpa_supplicant_event(bss->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
  1526. }
  1527. static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
  1528. struct nlattr *tb[])
  1529. {
  1530. u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
  1531. if (tb[NL80211_ATTR_MAC] == NULL) {
  1532. wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
  1533. "event");
  1534. return;
  1535. }
  1536. os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  1537. /* register for any AUTH message */
  1538. nl80211_register_frame(&drv->first_bss, drv->first_bss.nl_mgmt,
  1539. type, NULL, 0);
  1540. drv->associated = 1;
  1541. wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
  1542. MAC2STR(drv->bssid));
  1543. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
  1544. }
  1545. static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
  1546. int cancel_event, struct nlattr *tb[])
  1547. {
  1548. unsigned int freq, chan_type, duration;
  1549. union wpa_event_data data;
  1550. u64 cookie;
  1551. if (tb[NL80211_ATTR_WIPHY_FREQ])
  1552. freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
  1553. else
  1554. freq = 0;
  1555. if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
  1556. chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
  1557. else
  1558. chan_type = 0;
  1559. if (tb[NL80211_ATTR_DURATION])
  1560. duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
  1561. else
  1562. duration = 0;
  1563. if (tb[NL80211_ATTR_COOKIE])
  1564. cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
  1565. else
  1566. cookie = 0;
  1567. wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
  1568. "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
  1569. cancel_event, freq, chan_type, duration,
  1570. (long long unsigned int) cookie,
  1571. cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
  1572. if (cookie != drv->remain_on_chan_cookie)
  1573. return; /* not for us */
  1574. if (cancel_event)
  1575. drv->pending_remain_on_chan = 0;
  1576. os_memset(&data, 0, sizeof(data));
  1577. data.remain_on_channel.freq = freq;
  1578. data.remain_on_channel.duration = duration;
  1579. wpa_supplicant_event(drv->ctx, cancel_event ?
  1580. EVENT_CANCEL_REMAIN_ON_CHANNEL :
  1581. EVENT_REMAIN_ON_CHANNEL, &data);
  1582. }
  1583. static void mlme_event_ft_event(struct wpa_driver_nl80211_data *drv,
  1584. struct nlattr *tb[])
  1585. {
  1586. union wpa_event_data data;
  1587. os_memset(&data, 0, sizeof(data));
  1588. if (tb[NL80211_ATTR_IE]) {
  1589. data.ft_ies.ies = nla_data(tb[NL80211_ATTR_IE]);
  1590. data.ft_ies.ies_len = nla_len(tb[NL80211_ATTR_IE]);
  1591. }
  1592. if (tb[NL80211_ATTR_IE_RIC]) {
  1593. data.ft_ies.ric_ies = nla_data(tb[NL80211_ATTR_IE_RIC]);
  1594. data.ft_ies.ric_ies_len = nla_len(tb[NL80211_ATTR_IE_RIC]);
  1595. }
  1596. if (tb[NL80211_ATTR_MAC])
  1597. os_memcpy(data.ft_ies.target_ap,
  1598. nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  1599. wpa_printf(MSG_DEBUG, "nl80211: FT event target_ap " MACSTR,
  1600. MAC2STR(data.ft_ies.target_ap));
  1601. wpa_supplicant_event(drv->ctx, EVENT_FT_RESPONSE, &data);
  1602. }
  1603. static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
  1604. struct nlattr *tb[])
  1605. {
  1606. union wpa_event_data event;
  1607. struct nlattr *nl;
  1608. int rem;
  1609. struct scan_info *info;
  1610. #define MAX_REPORT_FREQS 50
  1611. int freqs[MAX_REPORT_FREQS];
  1612. int num_freqs = 0;
  1613. if (drv->scan_for_auth) {
  1614. drv->scan_for_auth = 0;
  1615. wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
  1616. "cfg80211 BSS entry");
  1617. wpa_driver_nl80211_authenticate_retry(drv);
  1618. return;
  1619. }
  1620. os_memset(&event, 0, sizeof(event));
  1621. info = &event.scan_info;
  1622. info->aborted = aborted;
  1623. if (tb[NL80211_ATTR_SCAN_SSIDS]) {
  1624. nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
  1625. struct wpa_driver_scan_ssid *s =
  1626. &info->ssids[info->num_ssids];
  1627. s->ssid = nla_data(nl);
  1628. s->ssid_len = nla_len(nl);
  1629. info->num_ssids++;
  1630. if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
  1631. break;
  1632. }
  1633. }
  1634. if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
  1635. nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
  1636. {
  1637. freqs[num_freqs] = nla_get_u32(nl);
  1638. num_freqs++;
  1639. if (num_freqs == MAX_REPORT_FREQS - 1)
  1640. break;
  1641. }
  1642. info->freqs = freqs;
  1643. info->num_freqs = num_freqs;
  1644. }
  1645. wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
  1646. }
  1647. static int get_link_signal(struct nl_msg *msg, void *arg)
  1648. {
  1649. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1650. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1651. struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
  1652. static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
  1653. [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
  1654. [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
  1655. };
  1656. struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
  1657. static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
  1658. [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
  1659. [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
  1660. [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
  1661. [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
  1662. };
  1663. struct wpa_signal_info *sig_change = arg;
  1664. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1665. genlmsg_attrlen(gnlh, 0), NULL);
  1666. if (!tb[NL80211_ATTR_STA_INFO] ||
  1667. nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
  1668. tb[NL80211_ATTR_STA_INFO], policy))
  1669. return NL_SKIP;
  1670. if (!sinfo[NL80211_STA_INFO_SIGNAL])
  1671. return NL_SKIP;
  1672. sig_change->current_signal =
  1673. (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
  1674. if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
  1675. sig_change->avg_signal =
  1676. (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
  1677. else
  1678. sig_change->avg_signal = 0;
  1679. if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
  1680. if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  1681. sinfo[NL80211_STA_INFO_TX_BITRATE],
  1682. rate_policy)) {
  1683. sig_change->current_txrate = 0;
  1684. } else {
  1685. if (rinfo[NL80211_RATE_INFO_BITRATE]) {
  1686. sig_change->current_txrate =
  1687. nla_get_u16(rinfo[
  1688. NL80211_RATE_INFO_BITRATE]) * 100;
  1689. }
  1690. }
  1691. }
  1692. return NL_SKIP;
  1693. }
  1694. static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
  1695. struct wpa_signal_info *sig)
  1696. {
  1697. struct nl_msg *msg;
  1698. sig->current_signal = -9999;
  1699. sig->current_txrate = 0;
  1700. msg = nlmsg_alloc();
  1701. if (!msg)
  1702. return -ENOMEM;
  1703. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
  1704. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1705. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
  1706. return send_and_recv_msgs(drv, msg, get_link_signal, sig);
  1707. nla_put_failure:
  1708. nlmsg_free(msg);
  1709. return -ENOBUFS;
  1710. }
  1711. static int get_link_noise(struct nl_msg *msg, void *arg)
  1712. {
  1713. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1714. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1715. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  1716. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  1717. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  1718. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  1719. };
  1720. struct wpa_signal_info *sig_change = arg;
  1721. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1722. genlmsg_attrlen(gnlh, 0), NULL);
  1723. if (!tb[NL80211_ATTR_SURVEY_INFO]) {
  1724. wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
  1725. return NL_SKIP;
  1726. }
  1727. if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  1728. tb[NL80211_ATTR_SURVEY_INFO],
  1729. survey_policy)) {
  1730. wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
  1731. "attributes!");
  1732. return NL_SKIP;
  1733. }
  1734. if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
  1735. return NL_SKIP;
  1736. if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
  1737. sig_change->frequency)
  1738. return NL_SKIP;
  1739. if (!sinfo[NL80211_SURVEY_INFO_NOISE])
  1740. return NL_SKIP;
  1741. sig_change->current_noise =
  1742. (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  1743. return NL_SKIP;
  1744. }
  1745. static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
  1746. struct wpa_signal_info *sig_change)
  1747. {
  1748. struct nl_msg *msg;
  1749. sig_change->current_noise = 9999;
  1750. sig_change->frequency = drv->assoc_freq;
  1751. msg = nlmsg_alloc();
  1752. if (!msg)
  1753. return -ENOMEM;
  1754. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
  1755. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1756. return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
  1757. nla_put_failure:
  1758. nlmsg_free(msg);
  1759. return -ENOBUFS;
  1760. }
  1761. static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
  1762. {
  1763. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1764. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1765. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  1766. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  1767. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  1768. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  1769. };
  1770. struct wpa_scan_results *scan_results = arg;
  1771. struct wpa_scan_res *scan_res;
  1772. size_t i;
  1773. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1774. genlmsg_attrlen(gnlh, 0), NULL);
  1775. if (!tb[NL80211_ATTR_SURVEY_INFO]) {
  1776. wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
  1777. return NL_SKIP;
  1778. }
  1779. if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  1780. tb[NL80211_ATTR_SURVEY_INFO],
  1781. survey_policy)) {
  1782. wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
  1783. "attributes");
  1784. return NL_SKIP;
  1785. }
  1786. if (!sinfo[NL80211_SURVEY_INFO_NOISE])
  1787. return NL_SKIP;
  1788. if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
  1789. return NL_SKIP;
  1790. for (i = 0; i < scan_results->num; ++i) {
  1791. scan_res = scan_results->res[i];
  1792. if (!scan_res)
  1793. continue;
  1794. if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
  1795. scan_res->freq)
  1796. continue;
  1797. if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
  1798. continue;
  1799. scan_res->noise = (s8)
  1800. nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  1801. scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
  1802. }
  1803. return NL_SKIP;
  1804. }
  1805. static int nl80211_get_noise_for_scan_results(
  1806. struct wpa_driver_nl80211_data *drv,
  1807. struct wpa_scan_results *scan_res)
  1808. {
  1809. struct nl_msg *msg;
  1810. msg = nlmsg_alloc();
  1811. if (!msg)
  1812. return -ENOMEM;
  1813. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
  1814. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1815. return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
  1816. scan_res);
  1817. nla_put_failure:
  1818. nlmsg_free(msg);
  1819. return -ENOBUFS;
  1820. }
  1821. static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
  1822. struct nlattr *tb[])
  1823. {
  1824. static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
  1825. [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
  1826. [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
  1827. [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
  1828. [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
  1829. };
  1830. struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
  1831. enum nl80211_cqm_rssi_threshold_event event;
  1832. union wpa_event_data ed;
  1833. struct wpa_signal_info sig;
  1834. int res;
  1835. if (tb[NL80211_ATTR_CQM] == NULL ||
  1836. nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
  1837. cqm_policy)) {
  1838. wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
  1839. return;
  1840. }
  1841. os_memset(&ed, 0, sizeof(ed));
  1842. if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
  1843. if (!tb[NL80211_ATTR_MAC])
  1844. return;
  1845. os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
  1846. ETH_ALEN);
  1847. wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
  1848. return;
  1849. }
  1850. if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
  1851. return;
  1852. event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
  1853. if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
  1854. wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
  1855. "event: RSSI high");
  1856. ed.signal_change.above_threshold = 1;
  1857. } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
  1858. wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
  1859. "event: RSSI low");
  1860. ed.signal_change.above_threshold = 0;
  1861. } else
  1862. return;
  1863. res = nl80211_get_link_signal(drv, &sig);
  1864. if (res == 0) {
  1865. ed.signal_change.current_signal = sig.current_signal;
  1866. ed.signal_change.current_txrate = sig.current_txrate;
  1867. wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
  1868. sig.current_signal, sig.current_txrate);
  1869. }
  1870. res = nl80211_get_link_noise(drv, &sig);
  1871. if (res == 0) {
  1872. ed.signal_change.current_noise = sig.current_noise;
  1873. wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
  1874. sig.current_noise);
  1875. }
  1876. wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
  1877. }
  1878. static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
  1879. struct nlattr **tb)
  1880. {
  1881. u8 *addr;
  1882. union wpa_event_data data;
  1883. if (tb[NL80211_ATTR_MAC] == NULL)
  1884. return;
  1885. addr = nla_data(tb[NL80211_ATTR_MAC]);
  1886. wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
  1887. if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
  1888. u8 *ies = NULL;
  1889. size_t ies_len = 0;
  1890. if (tb[NL80211_ATTR_IE]) {
  1891. ies = nla_data(tb[NL80211_ATTR_IE]);
  1892. ies_len = nla_len(tb[NL80211_ATTR_IE]);
  1893. }
  1894. wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
  1895. drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
  1896. return;
  1897. }
  1898. if (drv->nlmode != NL80211_IFTYPE_ADHOC)
  1899. return;
  1900. os_memset(&data, 0, sizeof(data));
  1901. os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
  1902. wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
  1903. }
  1904. static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
  1905. struct nlattr **tb)
  1906. {
  1907. u8 *addr;
  1908. union wpa_event_data data;
  1909. if (tb[NL80211_ATTR_MAC] == NULL)
  1910. return;
  1911. addr = nla_data(tb[NL80211_ATTR_MAC]);
  1912. wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
  1913. MAC2STR(addr));
  1914. if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
  1915. drv_event_disassoc(drv->ctx, addr);
  1916. return;
  1917. }
  1918. if (drv->nlmode != NL80211_IFTYPE_ADHOC)
  1919. return;
  1920. os_memset(&data, 0, sizeof(data));
  1921. os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
  1922. wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
  1923. }
  1924. static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
  1925. struct nlattr **tb)
  1926. {
  1927. struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
  1928. static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
  1929. [NL80211_REKEY_DATA_KEK] = {
  1930. .minlen = NL80211_KEK_LEN,
  1931. .maxlen = NL80211_KEK_LEN,
  1932. },
  1933. [NL80211_REKEY_DATA_KCK] = {
  1934. .minlen = NL80211_KCK_LEN,
  1935. .maxlen = NL80211_KCK_LEN,
  1936. },
  1937. [NL80211_REKEY_DATA_REPLAY_CTR] = {
  1938. .minlen = NL80211_REPLAY_CTR_LEN,
  1939. .maxlen = NL80211_REPLAY_CTR_LEN,
  1940. },
  1941. };
  1942. union wpa_event_data data;
  1943. if (!tb[NL80211_ATTR_MAC])
  1944. return;
  1945. if (!tb[NL80211_ATTR_REKEY_DATA])
  1946. return;
  1947. if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
  1948. tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
  1949. return;
  1950. if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
  1951. return;
  1952. os_memset(&data, 0, sizeof(data));
  1953. data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
  1954. wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
  1955. MAC2STR(data.driver_gtk_rekey.bssid));
  1956. data.driver_gtk_rekey.replay_ctr =
  1957. nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
  1958. wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
  1959. data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
  1960. wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
  1961. }
  1962. static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
  1963. struct nlattr **tb)
  1964. {
  1965. struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
  1966. static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
  1967. [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
  1968. [NL80211_PMKSA_CANDIDATE_BSSID] = {
  1969. .minlen = ETH_ALEN,
  1970. .maxlen = ETH_ALEN,
  1971. },
  1972. [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
  1973. };
  1974. union wpa_event_data data;
  1975. wpa_printf(MSG_DEBUG, "nl80211: PMKSA candidate event");
  1976. if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
  1977. return;
  1978. if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
  1979. tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
  1980. return;
  1981. if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
  1982. !cand[NL80211_PMKSA_CANDIDATE_BSSID])
  1983. return;
  1984. os_memset(&data, 0, sizeof(data));
  1985. os_memcpy(data.pmkid_candidate.bssid,
  1986. nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
  1987. data.pmkid_candidate.index =
  1988. nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
  1989. data.pmkid_candidate.preauth =
  1990. cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
  1991. wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
  1992. }
  1993. static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
  1994. struct nlattr **tb)
  1995. {
  1996. union wpa_event_data data;
  1997. wpa_printf(MSG_DEBUG, "nl80211: Probe client event");
  1998. if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
  1999. return;
  2000. os_memset(&data, 0, sizeof(data));
  2001. os_memcpy(data.client_poll.addr,
  2002. nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  2003. wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
  2004. }
  2005. static void nl80211_tdls_oper_event(struct wpa_driver_nl80211_data *drv,
  2006. struct nlattr **tb)
  2007. {
  2008. union wpa_event_data data;
  2009. wpa_printf(MSG_DEBUG, "nl80211: TDLS operation event");
  2010. if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_TDLS_OPERATION])
  2011. return;
  2012. os_memset(&data, 0, sizeof(data));
  2013. os_memcpy(data.tdls.peer, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  2014. switch (nla_get_u8(tb[NL80211_ATTR_TDLS_OPERATION])) {
  2015. case NL80211_TDLS_SETUP:
  2016. wpa_printf(MSG_DEBUG, "nl80211: TDLS setup request for peer "
  2017. MACSTR, MAC2STR(data.tdls.peer));
  2018. data.tdls.oper = TDLS_REQUEST_SETUP;
  2019. break;
  2020. case NL80211_TDLS_TEARDOWN:
  2021. wpa_printf(MSG_DEBUG, "nl80211: TDLS teardown request for peer "
  2022. MACSTR, MAC2STR(data.tdls.peer));
  2023. data.tdls.oper = TDLS_REQUEST_TEARDOWN;
  2024. break;
  2025. default:
  2026. wpa_printf(MSG_DEBUG, "nl80211: Unsupported TDLS operatione "
  2027. "event");
  2028. return;
  2029. }
  2030. if (tb[NL80211_ATTR_REASON_CODE]) {
  2031. data.tdls.reason_code =
  2032. nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
  2033. }
  2034. wpa_supplicant_event(drv->ctx, EVENT_TDLS, &data);
  2035. }
  2036. static void nl80211_stop_ap(struct wpa_driver_nl80211_data *drv,
  2037. struct nlattr **tb)
  2038. {
  2039. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_UNAVAILABLE, NULL);
  2040. }
  2041. static void nl80211_connect_failed_event(struct wpa_driver_nl80211_data *drv,
  2042. struct nlattr **tb)
  2043. {
  2044. union wpa_event_data data;
  2045. u32 reason;
  2046. wpa_printf(MSG_DEBUG, "nl80211: Connect failed event");
  2047. if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_CONN_FAILED_REASON])
  2048. return;
  2049. os_memset(&data, 0, sizeof(data));
  2050. os_memcpy(data.connect_failed_reason.addr,
  2051. nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  2052. reason = nla_get_u32(tb[NL80211_ATTR_CONN_FAILED_REASON]);
  2053. switch (reason) {
  2054. case NL80211_CONN_FAIL_MAX_CLIENTS:
  2055. wpa_printf(MSG_DEBUG, "nl80211: Max client reached");
  2056. data.connect_failed_reason.code = MAX_CLIENT_REACHED;
  2057. break;
  2058. case NL80211_CONN_FAIL_BLOCKED_CLIENT:
  2059. wpa_printf(MSG_DEBUG, "nl80211: Blocked client " MACSTR
  2060. " tried to connect",
  2061. MAC2STR(data.connect_failed_reason.addr));
  2062. data.connect_failed_reason.code = BLOCKED_CLIENT;
  2063. break;
  2064. default:
  2065. wpa_printf(MSG_DEBUG, "nl8021l: Unknown connect failed reason "
  2066. "%u", reason);
  2067. return;
  2068. }
  2069. wpa_supplicant_event(drv->ctx, EVENT_CONNECT_FAILED_REASON, &data);
  2070. }
  2071. static void nl80211_radar_event(struct wpa_driver_nl80211_data *drv,
  2072. struct nlattr **tb)
  2073. {
  2074. union wpa_event_data data;
  2075. enum nl80211_radar_event event_type;
  2076. if (!tb[NL80211_ATTR_WIPHY_FREQ] || !tb[NL80211_ATTR_RADAR_EVENT])
  2077. return;
  2078. os_memset(&data, 0, sizeof(data));
  2079. data.dfs_event.freq = nla_get_u16(tb[NL80211_ATTR_WIPHY_FREQ]);
  2080. event_type = nla_get_u8(tb[NL80211_ATTR_RADAR_EVENT]);
  2081. wpa_printf(MSG_DEBUG, "nl80211: DFS event on freq %d MHz",
  2082. data.dfs_event.freq);
  2083. switch (event_type) {
  2084. case NL80211_RADAR_DETECTED:
  2085. wpa_supplicant_event(drv->ctx, EVENT_DFS_RADAR_DETECTED, &data);
  2086. break;
  2087. case NL80211_RADAR_CAC_FINISHED:
  2088. wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_FINISHED, &data);
  2089. break;
  2090. case NL80211_RADAR_CAC_ABORTED:
  2091. wpa_supplicant_event(drv->ctx, EVENT_DFS_CAC_ABORTED, &data);
  2092. break;
  2093. case NL80211_RADAR_NOP_FINISHED:
  2094. wpa_supplicant_event(drv->ctx, EVENT_DFS_NOP_FINISHED, &data);
  2095. break;
  2096. default:
  2097. wpa_printf(MSG_DEBUG, "nl80211: Unknown radar event %d "
  2098. "received", event_type);
  2099. break;
  2100. }
  2101. }
  2102. static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
  2103. int wds)
  2104. {
  2105. struct wpa_driver_nl80211_data *drv = bss->drv;
  2106. union wpa_event_data event;
  2107. if (!tb[NL80211_ATTR_MAC])
  2108. return;
  2109. os_memset(&event, 0, sizeof(event));
  2110. event.rx_from_unknown.bssid = bss->addr;
  2111. event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
  2112. event.rx_from_unknown.wds = wds;
  2113. wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
  2114. }
  2115. static void do_process_drv_event(struct i802_bss *bss, int cmd,
  2116. struct nlattr **tb)
  2117. {
  2118. struct wpa_driver_nl80211_data *drv = bss->drv;
  2119. wpa_printf(MSG_DEBUG, "nl80211: Drv Event %d (%s) received for %s",
  2120. cmd, nl80211_command_to_string(cmd), bss->ifname);
  2121. if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
  2122. (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
  2123. cmd == NL80211_CMD_SCAN_ABORTED)) {
  2124. wpa_driver_nl80211_set_mode(&drv->first_bss,
  2125. drv->ap_scan_as_station);
  2126. drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
  2127. }
  2128. switch (cmd) {
  2129. case NL80211_CMD_TRIGGER_SCAN:
  2130. wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan trigger");
  2131. break;
  2132. case NL80211_CMD_START_SCHED_SCAN:
  2133. wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan started");
  2134. break;
  2135. case NL80211_CMD_SCHED_SCAN_STOPPED:
  2136. wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Sched scan stopped");
  2137. wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
  2138. break;
  2139. case NL80211_CMD_NEW_SCAN_RESULTS:
  2140. wpa_dbg(drv->ctx, MSG_DEBUG,
  2141. "nl80211: New scan results available");
  2142. drv->scan_complete_events = 1;
  2143. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
  2144. drv->ctx);
  2145. send_scan_event(drv, 0, tb);
  2146. break;
  2147. case NL80211_CMD_SCHED_SCAN_RESULTS:
  2148. wpa_dbg(drv->ctx, MSG_DEBUG,
  2149. "nl80211: New sched scan results available");
  2150. send_scan_event(drv, 0, tb);
  2151. break;
  2152. case NL80211_CMD_SCAN_ABORTED:
  2153. wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Scan aborted");
  2154. /*
  2155. * Need to indicate that scan results are available in order
  2156. * not to make wpa_supplicant stop its scanning.
  2157. */
  2158. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
  2159. drv->ctx);
  2160. send_scan_event(drv, 1, tb);
  2161. break;
  2162. case NL80211_CMD_AUTHENTICATE:
  2163. case NL80211_CMD_ASSOCIATE:
  2164. case NL80211_CMD_DEAUTHENTICATE:
  2165. case NL80211_CMD_DISASSOCIATE:
  2166. case NL80211_CMD_FRAME_TX_STATUS:
  2167. case NL80211_CMD_UNPROT_DEAUTHENTICATE:
  2168. case NL80211_CMD_UNPROT_DISASSOCIATE:
  2169. mlme_event(bss, cmd, tb[NL80211_ATTR_FRAME],
  2170. tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
  2171. tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
  2172. tb[NL80211_ATTR_COOKIE],
  2173. tb[NL80211_ATTR_RX_SIGNAL_DBM]);
  2174. break;
  2175. case NL80211_CMD_CONNECT:
  2176. case NL80211_CMD_ROAM:
  2177. mlme_event_connect(drv, cmd,
  2178. tb[NL80211_ATTR_STATUS_CODE],
  2179. tb[NL80211_ATTR_MAC],
  2180. tb[NL80211_ATTR_REQ_IE],
  2181. tb[NL80211_ATTR_RESP_IE]);
  2182. break;
  2183. case NL80211_CMD_CH_SWITCH_NOTIFY:
  2184. mlme_event_ch_switch(drv, tb[NL80211_ATTR_WIPHY_FREQ],
  2185. tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
  2186. break;
  2187. case NL80211_CMD_DISCONNECT:
  2188. mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
  2189. tb[NL80211_ATTR_MAC],
  2190. tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
  2191. break;
  2192. case NL80211_CMD_MICHAEL_MIC_FAILURE:
  2193. mlme_event_michael_mic_failure(bss, tb);
  2194. break;
  2195. case NL80211_CMD_JOIN_IBSS:
  2196. mlme_event_join_ibss(drv, tb);
  2197. break;
  2198. case NL80211_CMD_REMAIN_ON_CHANNEL:
  2199. mlme_event_remain_on_channel(drv, 0, tb);
  2200. break;
  2201. case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
  2202. mlme_event_remain_on_channel(drv, 1, tb);
  2203. break;
  2204. case NL80211_CMD_NOTIFY_CQM:
  2205. nl80211_cqm_event(drv, tb);
  2206. break;
  2207. case NL80211_CMD_REG_CHANGE:
  2208. wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
  2209. wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
  2210. NULL);
  2211. break;
  2212. case NL80211_CMD_REG_BEACON_HINT:
  2213. wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
  2214. wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
  2215. NULL);
  2216. break;
  2217. case NL80211_CMD_NEW_STATION:
  2218. nl80211_new_station_event(drv, tb);
  2219. break;
  2220. case NL80211_CMD_DEL_STATION:
  2221. nl80211_del_station_event(drv, tb);
  2222. break;
  2223. case NL80211_CMD_SET_REKEY_OFFLOAD:
  2224. nl80211_rekey_offload_event(drv, tb);
  2225. break;
  2226. case NL80211_CMD_PMKSA_CANDIDATE:
  2227. nl80211_pmksa_candidate_event(drv, tb);
  2228. break;
  2229. case NL80211_CMD_PROBE_CLIENT:
  2230. nl80211_client_probe_event(drv, tb);
  2231. break;
  2232. case NL80211_CMD_TDLS_OPER:
  2233. nl80211_tdls_oper_event(drv, tb);
  2234. break;
  2235. case NL80211_CMD_CONN_FAILED:
  2236. nl80211_connect_failed_event(drv, tb);
  2237. break;
  2238. case NL80211_CMD_FT_EVENT:
  2239. mlme_event_ft_event(drv, tb);
  2240. break;
  2241. case NL80211_CMD_RADAR_DETECT:
  2242. nl80211_radar_event(drv, tb);
  2243. break;
  2244. case NL80211_CMD_STOP_AP:
  2245. nl80211_stop_ap(drv, tb);
  2246. break;
  2247. default:
  2248. wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: Ignored unknown event "
  2249. "(cmd=%d)", cmd);
  2250. break;
  2251. }
  2252. }
  2253. static int process_drv_event(struct nl_msg *msg, void *arg)
  2254. {
  2255. struct wpa_driver_nl80211_data *drv = arg;
  2256. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2257. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  2258. struct i802_bss *bss;
  2259. int ifidx = -1;
  2260. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2261. genlmsg_attrlen(gnlh, 0), NULL);
  2262. if (tb[NL80211_ATTR_IFINDEX]) {
  2263. ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
  2264. for (bss = &drv->first_bss; bss; bss = bss->next)
  2265. if (ifidx == -1 || ifidx == bss->ifindex) {
  2266. do_process_drv_event(bss, gnlh->cmd, tb);
  2267. return NL_SKIP;
  2268. }
  2269. wpa_printf(MSG_DEBUG,
  2270. "nl80211: Ignored event (cmd=%d) for foreign interface (ifindex %d)",
  2271. gnlh->cmd, ifidx);
  2272. } else if (tb[NL80211_ATTR_WDEV]) {
  2273. u64 wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
  2274. wpa_printf(MSG_DEBUG, "nl80211: Process event on P2P device");
  2275. for (bss = &drv->first_bss; bss; bss = bss->next) {
  2276. if (bss->wdev_id_set && wdev_id == bss->wdev_id) {
  2277. do_process_drv_event(bss, gnlh->cmd, tb);
  2278. return NL_SKIP;
  2279. }
  2280. }
  2281. wpa_printf(MSG_DEBUG,
  2282. "nl80211: Ignored event (cmd=%d) for foreign interface (wdev 0x%llx)",
  2283. gnlh->cmd, (long long unsigned int) wdev_id);
  2284. }
  2285. return NL_SKIP;
  2286. }
  2287. static int process_global_event(struct nl_msg *msg, void *arg)
  2288. {
  2289. struct nl80211_global *global = arg;
  2290. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2291. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  2292. struct wpa_driver_nl80211_data *drv, *tmp;
  2293. int ifidx = -1;
  2294. struct i802_bss *bss;
  2295. u64 wdev_id = 0;
  2296. int wdev_id_set = 0;
  2297. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2298. genlmsg_attrlen(gnlh, 0), NULL);
  2299. if (tb[NL80211_ATTR_IFINDEX])
  2300. ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
  2301. else if (tb[NL80211_ATTR_WDEV]) {
  2302. wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
  2303. wdev_id_set = 1;
  2304. }
  2305. dl_list_for_each_safe(drv, tmp, &global->interfaces,
  2306. struct wpa_driver_nl80211_data, list) {
  2307. for (bss = &drv->first_bss; bss; bss = bss->next) {
  2308. if ((ifidx == -1 && !wdev_id_set) ||
  2309. ifidx == bss->ifindex ||
  2310. (wdev_id_set && bss->wdev_id_set &&
  2311. wdev_id == bss->wdev_id)) {
  2312. do_process_drv_event(bss, gnlh->cmd, tb);
  2313. return NL_SKIP;
  2314. }
  2315. }
  2316. }
  2317. return NL_SKIP;
  2318. }
  2319. static int process_bss_event(struct nl_msg *msg, void *arg)
  2320. {
  2321. struct i802_bss *bss = arg;
  2322. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2323. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  2324. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2325. genlmsg_attrlen(gnlh, 0), NULL);
  2326. wpa_printf(MSG_DEBUG, "nl80211: BSS Event %d (%s) received for %s",
  2327. gnlh->cmd, nl80211_command_to_string(gnlh->cmd),
  2328. bss->ifname);
  2329. switch (gnlh->cmd) {
  2330. case NL80211_CMD_FRAME:
  2331. case NL80211_CMD_FRAME_TX_STATUS:
  2332. mlme_event(bss, gnlh->cmd, tb[NL80211_ATTR_FRAME],
  2333. tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
  2334. tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
  2335. tb[NL80211_ATTR_COOKIE],
  2336. tb[NL80211_ATTR_RX_SIGNAL_DBM]);
  2337. break;
  2338. case NL80211_CMD_UNEXPECTED_FRAME:
  2339. nl80211_spurious_frame(bss, tb, 0);
  2340. break;
  2341. case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
  2342. nl80211_spurious_frame(bss, tb, 1);
  2343. break;
  2344. default:
  2345. wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
  2346. "(cmd=%d)", gnlh->cmd);
  2347. break;
  2348. }
  2349. return NL_SKIP;
  2350. }
  2351. static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
  2352. void *handle)
  2353. {
  2354. struct nl_cb *cb = eloop_ctx;
  2355. wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
  2356. nl_recvmsgs(handle, cb);
  2357. }
  2358. /**
  2359. * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
  2360. * @priv: driver_nl80211 private data
  2361. * @alpha2_arg: country to which to switch to
  2362. * Returns: 0 on success, -1 on failure
  2363. *
  2364. * This asks nl80211 to set the regulatory domain for given
  2365. * country ISO / IEC alpha2.
  2366. */
  2367. static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
  2368. {
  2369. struct i802_bss *bss = priv;
  2370. struct wpa_driver_nl80211_data *drv = bss->drv;
  2371. char alpha2[3];
  2372. struct nl_msg *msg;
  2373. msg = nlmsg_alloc();
  2374. if (!msg)
  2375. return -ENOMEM;
  2376. alpha2[0] = alpha2_arg[0];
  2377. alpha2[1] = alpha2_arg[1];
  2378. alpha2[2] = '\0';
  2379. nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
  2380. NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
  2381. if (send_and_recv_msgs(drv, msg, NULL, NULL))
  2382. return -EINVAL;
  2383. return 0;
  2384. nla_put_failure:
  2385. nlmsg_free(msg);
  2386. return -EINVAL;
  2387. }
  2388. static int protocol_feature_handler(struct nl_msg *msg, void *arg)
  2389. {
  2390. u32 *feat = arg;
  2391. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  2392. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2393. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2394. genlmsg_attrlen(gnlh, 0), NULL);
  2395. if (tb_msg[NL80211_ATTR_PROTOCOL_FEATURES])
  2396. *feat = nla_get_u32(tb_msg[NL80211_ATTR_PROTOCOL_FEATURES]);
  2397. return NL_SKIP;
  2398. }
  2399. static u32 get_nl80211_protocol_features(struct wpa_driver_nl80211_data *drv)
  2400. {
  2401. u32 feat = 0;
  2402. struct nl_msg *msg;
  2403. msg = nlmsg_alloc();
  2404. if (!msg)
  2405. goto nla_put_failure;
  2406. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_PROTOCOL_FEATURES);
  2407. if (send_and_recv_msgs(drv, msg, protocol_feature_handler, &feat) == 0)
  2408. return feat;
  2409. msg = NULL;
  2410. nla_put_failure:
  2411. nlmsg_free(msg);
  2412. return 0;
  2413. }
  2414. struct wiphy_info_data {
  2415. struct wpa_driver_nl80211_data *drv;
  2416. struct wpa_driver_capa *capa;
  2417. unsigned int num_multichan_concurrent;
  2418. unsigned int error:1;
  2419. unsigned int device_ap_sme:1;
  2420. unsigned int poll_command_supported:1;
  2421. unsigned int data_tx_status:1;
  2422. unsigned int monitor_supported:1;
  2423. unsigned int auth_supported:1;
  2424. unsigned int connect_supported:1;
  2425. unsigned int p2p_go_supported:1;
  2426. unsigned int p2p_client_supported:1;
  2427. unsigned int p2p_concurrent:1;
  2428. };
  2429. static unsigned int probe_resp_offload_support(int supp_protocols)
  2430. {
  2431. unsigned int prot = 0;
  2432. if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
  2433. prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
  2434. if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
  2435. prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
  2436. if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
  2437. prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
  2438. if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
  2439. prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
  2440. return prot;
  2441. }
  2442. static void wiphy_info_supported_iftypes(struct wiphy_info_data *info,
  2443. struct nlattr *tb)
  2444. {
  2445. struct nlattr *nl_mode;
  2446. int i;
  2447. if (tb == NULL)
  2448. return;
  2449. nla_for_each_nested(nl_mode, tb, i) {
  2450. switch (nla_type(nl_mode)) {
  2451. case NL80211_IFTYPE_AP:
  2452. info->capa->flags |= WPA_DRIVER_FLAGS_AP;
  2453. break;
  2454. case NL80211_IFTYPE_ADHOC:
  2455. info->capa->flags |= WPA_DRIVER_FLAGS_IBSS;
  2456. break;
  2457. case NL80211_IFTYPE_P2P_DEVICE:
  2458. info->capa->flags |=
  2459. WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
  2460. break;
  2461. case NL80211_IFTYPE_P2P_GO:
  2462. info->p2p_go_supported = 1;
  2463. break;
  2464. case NL80211_IFTYPE_P2P_CLIENT:
  2465. info->p2p_client_supported = 1;
  2466. break;
  2467. case NL80211_IFTYPE_MONITOR:
  2468. info->monitor_supported = 1;
  2469. break;
  2470. }
  2471. }
  2472. }
  2473. static int wiphy_info_iface_comb_process(struct wiphy_info_data *info,
  2474. struct nlattr *nl_combi)
  2475. {
  2476. struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
  2477. struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
  2478. struct nlattr *nl_limit, *nl_mode;
  2479. int err, rem_limit, rem_mode;
  2480. int combination_has_p2p = 0, combination_has_mgd = 0;
  2481. static struct nla_policy
  2482. iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
  2483. [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
  2484. [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
  2485. [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
  2486. [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
  2487. [NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS] = { .type = NLA_U32 },
  2488. },
  2489. iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
  2490. [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
  2491. [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
  2492. };
  2493. err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
  2494. nl_combi, iface_combination_policy);
  2495. if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
  2496. !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
  2497. !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
  2498. return 0; /* broken combination */
  2499. if (tb_comb[NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS])
  2500. info->capa->flags |= WPA_DRIVER_FLAGS_RADAR;
  2501. nla_for_each_nested(nl_limit, tb_comb[NL80211_IFACE_COMB_LIMITS],
  2502. rem_limit) {
  2503. err = nla_parse_nested(tb_limit, MAX_NL80211_IFACE_LIMIT,
  2504. nl_limit, iface_limit_policy);
  2505. if (err || !tb_limit[NL80211_IFACE_LIMIT_TYPES])
  2506. return 0; /* broken combination */
  2507. nla_for_each_nested(nl_mode,
  2508. tb_limit[NL80211_IFACE_LIMIT_TYPES],
  2509. rem_mode) {
  2510. int ift = nla_type(nl_mode);
  2511. if (ift == NL80211_IFTYPE_P2P_GO ||
  2512. ift == NL80211_IFTYPE_P2P_CLIENT)
  2513. combination_has_p2p = 1;
  2514. if (ift == NL80211_IFTYPE_STATION)
  2515. combination_has_mgd = 1;
  2516. }
  2517. if (combination_has_p2p && combination_has_mgd)
  2518. break;
  2519. }
  2520. if (combination_has_p2p && combination_has_mgd) {
  2521. info->p2p_concurrent = 1;
  2522. info->num_multichan_concurrent =
  2523. nla_get_u32(tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS]);
  2524. return 1;
  2525. }
  2526. return 0;
  2527. }
  2528. static void wiphy_info_iface_comb(struct wiphy_info_data *info,
  2529. struct nlattr *tb)
  2530. {
  2531. struct nlattr *nl_combi;
  2532. int rem_combi;
  2533. if (tb == NULL)
  2534. return;
  2535. nla_for_each_nested(nl_combi, tb, rem_combi) {
  2536. if (wiphy_info_iface_comb_process(info, nl_combi) > 0)
  2537. break;
  2538. }
  2539. }
  2540. static void wiphy_info_supp_cmds(struct wiphy_info_data *info,
  2541. struct nlattr *tb)
  2542. {
  2543. struct nlattr *nl_cmd;
  2544. int i;
  2545. if (tb == NULL)
  2546. return;
  2547. nla_for_each_nested(nl_cmd, tb, i) {
  2548. switch (nla_get_u32(nl_cmd)) {
  2549. case NL80211_CMD_AUTHENTICATE:
  2550. info->auth_supported = 1;
  2551. break;
  2552. case NL80211_CMD_CONNECT:
  2553. info->connect_supported = 1;
  2554. break;
  2555. case NL80211_CMD_START_SCHED_SCAN:
  2556. info->capa->sched_scan_supported = 1;
  2557. break;
  2558. case NL80211_CMD_PROBE_CLIENT:
  2559. info->poll_command_supported = 1;
  2560. break;
  2561. }
  2562. }
  2563. }
  2564. static void wiphy_info_max_roc(struct wpa_driver_capa *capa,
  2565. struct nlattr *tb)
  2566. {
  2567. if (tb)
  2568. capa->max_remain_on_chan = nla_get_u32(tb);
  2569. }
  2570. static void wiphy_info_tdls(struct wpa_driver_capa *capa, struct nlattr *tdls,
  2571. struct nlattr *ext_setup)
  2572. {
  2573. if (tdls == NULL)
  2574. return;
  2575. wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
  2576. capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
  2577. if (ext_setup) {
  2578. wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
  2579. capa->flags |= WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
  2580. }
  2581. }
  2582. static void wiphy_info_feature_flags(struct wiphy_info_data *info,
  2583. struct nlattr *tb)
  2584. {
  2585. u32 flags;
  2586. struct wpa_driver_capa *capa = info->capa;
  2587. if (tb == NULL)
  2588. return;
  2589. flags = nla_get_u32(tb);
  2590. if (flags & NL80211_FEATURE_SK_TX_STATUS)
  2591. info->data_tx_status = 1;
  2592. if (flags & NL80211_FEATURE_INACTIVITY_TIMER)
  2593. capa->flags |= WPA_DRIVER_FLAGS_INACTIVITY_TIMER;
  2594. if (flags & NL80211_FEATURE_SAE)
  2595. capa->flags |= WPA_DRIVER_FLAGS_SAE;
  2596. if (flags & NL80211_FEATURE_NEED_OBSS_SCAN)
  2597. capa->flags |= WPA_DRIVER_FLAGS_OBSS_SCAN;
  2598. }
  2599. static void wiphy_info_probe_resp_offload(struct wpa_driver_capa *capa,
  2600. struct nlattr *tb)
  2601. {
  2602. u32 protocols;
  2603. if (tb == NULL)
  2604. return;
  2605. protocols = nla_get_u32(tb);
  2606. wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response offload in AP "
  2607. "mode");
  2608. capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
  2609. capa->probe_resp_offloads = probe_resp_offload_support(protocols);
  2610. }
  2611. static int wiphy_info_handler(struct nl_msg *msg, void *arg)
  2612. {
  2613. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  2614. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2615. struct wiphy_info_data *info = arg;
  2616. struct wpa_driver_capa *capa = info->capa;
  2617. struct wpa_driver_nl80211_data *drv = info->drv;
  2618. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2619. genlmsg_attrlen(gnlh, 0), NULL);
  2620. if (tb[NL80211_ATTR_WIPHY_NAME])
  2621. os_strncpy(drv->phyname,
  2622. nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]),
  2623. sizeof(drv->phyname));
  2624. if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
  2625. capa->max_scan_ssids =
  2626. nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
  2627. if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
  2628. capa->max_sched_scan_ssids =
  2629. nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
  2630. if (tb[NL80211_ATTR_MAX_MATCH_SETS])
  2631. capa->max_match_sets =
  2632. nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
  2633. if (tb[NL80211_ATTR_MAC_ACL_MAX])
  2634. capa->max_acl_mac_addrs =
  2635. nla_get_u8(tb[NL80211_ATTR_MAC_ACL_MAX]);
  2636. wiphy_info_supported_iftypes(info, tb[NL80211_ATTR_SUPPORTED_IFTYPES]);
  2637. wiphy_info_iface_comb(info, tb[NL80211_ATTR_INTERFACE_COMBINATIONS]);
  2638. wiphy_info_supp_cmds(info, tb[NL80211_ATTR_SUPPORTED_COMMANDS]);
  2639. if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
  2640. wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
  2641. "off-channel TX");
  2642. capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
  2643. }
  2644. if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
  2645. wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
  2646. capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
  2647. }
  2648. wiphy_info_max_roc(capa,
  2649. tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
  2650. if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
  2651. capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
  2652. wiphy_info_tdls(capa, tb[NL80211_ATTR_TDLS_SUPPORT],
  2653. tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]);
  2654. if (tb[NL80211_ATTR_DEVICE_AP_SME])
  2655. info->device_ap_sme = 1;
  2656. wiphy_info_feature_flags(info, tb[NL80211_ATTR_FEATURE_FLAGS]);
  2657. wiphy_info_probe_resp_offload(capa,
  2658. tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
  2659. if (tb[NL80211_ATTR_EXT_CAPA] && tb[NL80211_ATTR_EXT_CAPA_MASK] &&
  2660. drv->extended_capa == NULL) {
  2661. drv->extended_capa =
  2662. os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
  2663. if (drv->extended_capa) {
  2664. os_memcpy(drv->extended_capa,
  2665. nla_data(tb[NL80211_ATTR_EXT_CAPA]),
  2666. nla_len(tb[NL80211_ATTR_EXT_CAPA]));
  2667. drv->extended_capa_len =
  2668. nla_len(tb[NL80211_ATTR_EXT_CAPA]);
  2669. }
  2670. drv->extended_capa_mask =
  2671. os_malloc(nla_len(tb[NL80211_ATTR_EXT_CAPA]));
  2672. if (drv->extended_capa_mask) {
  2673. os_memcpy(drv->extended_capa_mask,
  2674. nla_data(tb[NL80211_ATTR_EXT_CAPA]),
  2675. nla_len(tb[NL80211_ATTR_EXT_CAPA]));
  2676. } else {
  2677. os_free(drv->extended_capa);
  2678. drv->extended_capa = NULL;
  2679. drv->extended_capa_len = 0;
  2680. }
  2681. }
  2682. return NL_SKIP;
  2683. }
  2684. static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
  2685. struct wiphy_info_data *info)
  2686. {
  2687. u32 feat;
  2688. struct nl_msg *msg;
  2689. os_memset(info, 0, sizeof(*info));
  2690. info->capa = &drv->capa;
  2691. info->drv = drv;
  2692. msg = nlmsg_alloc();
  2693. if (!msg)
  2694. return -1;
  2695. feat = get_nl80211_protocol_features(drv);
  2696. if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
  2697. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
  2698. else
  2699. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
  2700. NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
  2701. if (nl80211_set_iface_id(msg, &drv->first_bss) < 0)
  2702. goto nla_put_failure;
  2703. if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info))
  2704. return -1;
  2705. if (info->auth_supported)
  2706. drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
  2707. else if (!info->connect_supported) {
  2708. wpa_printf(MSG_INFO, "nl80211: Driver does not support "
  2709. "authentication/association or connect commands");
  2710. info->error = 1;
  2711. }
  2712. if (info->p2p_go_supported && info->p2p_client_supported)
  2713. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
  2714. if (info->p2p_concurrent) {
  2715. wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
  2716. "interface (driver advertised support)");
  2717. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
  2718. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
  2719. }
  2720. if (info->num_multichan_concurrent > 1) {
  2721. wpa_printf(MSG_DEBUG, "nl80211: Enable multi-channel "
  2722. "concurrent (driver advertised support)");
  2723. drv->capa.num_multichan_concurrent =
  2724. info->num_multichan_concurrent;
  2725. }
  2726. /* default to 5000 since early versions of mac80211 don't set it */
  2727. if (!drv->capa.max_remain_on_chan)
  2728. drv->capa.max_remain_on_chan = 5000;
  2729. return 0;
  2730. nla_put_failure:
  2731. nlmsg_free(msg);
  2732. return -1;
  2733. }
  2734. static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
  2735. {
  2736. struct wiphy_info_data info;
  2737. if (wpa_driver_nl80211_get_info(drv, &info))
  2738. return -1;
  2739. if (info.error)
  2740. return -1;
  2741. drv->has_capability = 1;
  2742. /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
  2743. drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2744. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2745. WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2746. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
  2747. drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
  2748. WPA_DRIVER_CAPA_ENC_WEP104 |
  2749. WPA_DRIVER_CAPA_ENC_TKIP |
  2750. WPA_DRIVER_CAPA_ENC_CCMP;
  2751. drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
  2752. WPA_DRIVER_AUTH_SHARED |
  2753. WPA_DRIVER_AUTH_LEAP;
  2754. drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
  2755. drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
  2756. drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
  2757. if (!info.device_ap_sme) {
  2758. drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
  2759. /*
  2760. * No AP SME is currently assumed to also indicate no AP MLME
  2761. * in the driver/firmware.
  2762. */
  2763. drv->capa.flags |= WPA_DRIVER_FLAGS_AP_MLME;
  2764. }
  2765. drv->device_ap_sme = info.device_ap_sme;
  2766. drv->poll_command_supported = info.poll_command_supported;
  2767. drv->data_tx_status = info.data_tx_status;
  2768. /*
  2769. * If poll command and tx status are supported, mac80211 is new enough
  2770. * to have everything we need to not need monitor interfaces.
  2771. */
  2772. drv->use_monitor = !info.poll_command_supported || !info.data_tx_status;
  2773. if (drv->device_ap_sme && drv->use_monitor) {
  2774. /*
  2775. * Non-mac80211 drivers may not support monitor interface.
  2776. * Make sure we do not get stuck with incorrect capability here
  2777. * by explicitly testing this.
  2778. */
  2779. if (!info.monitor_supported) {
  2780. wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
  2781. "with device_ap_sme since no monitor mode "
  2782. "support detected");
  2783. drv->use_monitor = 0;
  2784. }
  2785. }
  2786. /*
  2787. * If we aren't going to use monitor interfaces, but the
  2788. * driver doesn't support data TX status, we won't get TX
  2789. * status for EAPOL frames.
  2790. */
  2791. if (!drv->use_monitor && !info.data_tx_status)
  2792. drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
  2793. return 0;
  2794. }
  2795. #ifdef ANDROID
  2796. static int android_genl_ctrl_resolve(struct nl_handle *handle,
  2797. const char *name)
  2798. {
  2799. /*
  2800. * Android ICS has very minimal genl_ctrl_resolve() implementation, so
  2801. * need to work around that.
  2802. */
  2803. struct nl_cache *cache = NULL;
  2804. struct genl_family *nl80211 = NULL;
  2805. int id = -1;
  2806. if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
  2807. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  2808. "netlink cache");
  2809. goto fail;
  2810. }
  2811. nl80211 = genl_ctrl_search_by_name(cache, name);
  2812. if (nl80211 == NULL)
  2813. goto fail;
  2814. id = genl_family_get_id(nl80211);
  2815. fail:
  2816. if (nl80211)
  2817. genl_family_put(nl80211);
  2818. if (cache)
  2819. nl_cache_free(cache);
  2820. return id;
  2821. }
  2822. #define genl_ctrl_resolve android_genl_ctrl_resolve
  2823. #endif /* ANDROID */
  2824. static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
  2825. {
  2826. int ret;
  2827. global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  2828. if (global->nl_cb == NULL) {
  2829. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  2830. "callbacks");
  2831. return -1;
  2832. }
  2833. global->nl = nl_create_handle(global->nl_cb, "nl");
  2834. if (global->nl == NULL)
  2835. goto err;
  2836. global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
  2837. if (global->nl80211_id < 0) {
  2838. wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
  2839. "found");
  2840. goto err;
  2841. }
  2842. global->nl_event = nl_create_handle(global->nl_cb, "event");
  2843. if (global->nl_event == NULL)
  2844. goto err;
  2845. ret = nl_get_multicast_id(global, "nl80211", "scan");
  2846. if (ret >= 0)
  2847. ret = nl_socket_add_membership(global->nl_event, ret);
  2848. if (ret < 0) {
  2849. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  2850. "membership for scan events: %d (%s)",
  2851. ret, strerror(-ret));
  2852. goto err;
  2853. }
  2854. ret = nl_get_multicast_id(global, "nl80211", "mlme");
  2855. if (ret >= 0)
  2856. ret = nl_socket_add_membership(global->nl_event, ret);
  2857. if (ret < 0) {
  2858. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  2859. "membership for mlme events: %d (%s)",
  2860. ret, strerror(-ret));
  2861. goto err;
  2862. }
  2863. ret = nl_get_multicast_id(global, "nl80211", "regulatory");
  2864. if (ret >= 0)
  2865. ret = nl_socket_add_membership(global->nl_event, ret);
  2866. if (ret < 0) {
  2867. wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
  2868. "membership for regulatory events: %d (%s)",
  2869. ret, strerror(-ret));
  2870. /* Continue without regulatory events */
  2871. }
  2872. nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
  2873. no_seq_check, NULL);
  2874. nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
  2875. process_global_event, global);
  2876. eloop_register_read_sock(nl_socket_get_fd(global->nl_event),
  2877. wpa_driver_nl80211_event_receive,
  2878. global->nl_cb, global->nl_event);
  2879. return 0;
  2880. err:
  2881. nl_destroy_handles(&global->nl_event);
  2882. nl_destroy_handles(&global->nl);
  2883. nl_cb_put(global->nl_cb);
  2884. global->nl_cb = NULL;
  2885. return -1;
  2886. }
  2887. static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
  2888. {
  2889. drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  2890. if (!drv->nl_cb) {
  2891. wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
  2892. return -1;
  2893. }
  2894. nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
  2895. no_seq_check, NULL);
  2896. nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
  2897. process_drv_event, drv);
  2898. return 0;
  2899. }
  2900. static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
  2901. {
  2902. wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
  2903. /*
  2904. * This may be for any interface; use ifdown event to disable
  2905. * interface.
  2906. */
  2907. }
  2908. static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
  2909. {
  2910. struct wpa_driver_nl80211_data *drv = ctx;
  2911. wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
  2912. if (i802_set_iface_flags(&drv->first_bss, 1)) {
  2913. wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
  2914. "after rfkill unblock");
  2915. return;
  2916. }
  2917. /* rtnetlink ifup handler will report interface as enabled */
  2918. }
  2919. static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
  2920. void *eloop_ctx,
  2921. void *handle)
  2922. {
  2923. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  2924. u8 data[2048];
  2925. struct msghdr msg;
  2926. struct iovec entry;
  2927. u8 control[512];
  2928. struct cmsghdr *cmsg;
  2929. int res, found_ee = 0, found_wifi = 0, acked = 0;
  2930. union wpa_event_data event;
  2931. memset(&msg, 0, sizeof(msg));
  2932. msg.msg_iov = &entry;
  2933. msg.msg_iovlen = 1;
  2934. entry.iov_base = data;
  2935. entry.iov_len = sizeof(data);
  2936. msg.msg_control = &control;
  2937. msg.msg_controllen = sizeof(control);
  2938. res = recvmsg(sock, &msg, MSG_ERRQUEUE);
  2939. /* if error or not fitting 802.3 header, return */
  2940. if (res < 14)
  2941. return;
  2942. for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
  2943. {
  2944. if (cmsg->cmsg_level == SOL_SOCKET &&
  2945. cmsg->cmsg_type == SCM_WIFI_STATUS) {
  2946. int *ack;
  2947. found_wifi = 1;
  2948. ack = (void *)CMSG_DATA(cmsg);
  2949. acked = *ack;
  2950. }
  2951. if (cmsg->cmsg_level == SOL_PACKET &&
  2952. cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
  2953. struct sock_extended_err *err =
  2954. (struct sock_extended_err *)CMSG_DATA(cmsg);
  2955. if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
  2956. found_ee = 1;
  2957. }
  2958. }
  2959. if (!found_ee || !found_wifi)
  2960. return;
  2961. memset(&event, 0, sizeof(event));
  2962. event.eapol_tx_status.dst = data;
  2963. event.eapol_tx_status.data = data + 14;
  2964. event.eapol_tx_status.data_len = res - 14;
  2965. event.eapol_tx_status.ack = acked;
  2966. wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
  2967. }
  2968. static int nl80211_init_bss(struct i802_bss *bss)
  2969. {
  2970. bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  2971. if (!bss->nl_cb)
  2972. return -1;
  2973. nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
  2974. no_seq_check, NULL);
  2975. nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
  2976. process_bss_event, bss);
  2977. return 0;
  2978. }
  2979. static void nl80211_destroy_bss(struct i802_bss *bss)
  2980. {
  2981. nl_cb_put(bss->nl_cb);
  2982. bss->nl_cb = NULL;
  2983. }
  2984. /**
  2985. * wpa_driver_nl80211_init - Initialize nl80211 driver interface
  2986. * @ctx: context to be used when calling wpa_supplicant functions,
  2987. * e.g., wpa_supplicant_event()
  2988. * @ifname: interface name, e.g., wlan0
  2989. * @global_priv: private driver global data from global_init()
  2990. * Returns: Pointer to private data, %NULL on failure
  2991. */
  2992. static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
  2993. void *global_priv)
  2994. {
  2995. struct wpa_driver_nl80211_data *drv;
  2996. struct rfkill_config *rcfg;
  2997. struct i802_bss *bss;
  2998. if (global_priv == NULL)
  2999. return NULL;
  3000. drv = os_zalloc(sizeof(*drv));
  3001. if (drv == NULL)
  3002. return NULL;
  3003. drv->global = global_priv;
  3004. drv->ctx = ctx;
  3005. bss = &drv->first_bss;
  3006. bss->drv = drv;
  3007. bss->ctx = ctx;
  3008. os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
  3009. drv->monitor_ifidx = -1;
  3010. drv->monitor_sock = -1;
  3011. drv->eapol_tx_sock = -1;
  3012. drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
  3013. if (wpa_driver_nl80211_init_nl(drv)) {
  3014. os_free(drv);
  3015. return NULL;
  3016. }
  3017. if (nl80211_init_bss(bss))
  3018. goto failed;
  3019. rcfg = os_zalloc(sizeof(*rcfg));
  3020. if (rcfg == NULL)
  3021. goto failed;
  3022. rcfg->ctx = drv;
  3023. os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
  3024. rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
  3025. rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
  3026. drv->rfkill = rfkill_init(rcfg);
  3027. if (drv->rfkill == NULL) {
  3028. wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
  3029. os_free(rcfg);
  3030. }
  3031. if (wpa_driver_nl80211_finish_drv_init(drv))
  3032. goto failed;
  3033. drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
  3034. if (drv->eapol_tx_sock < 0)
  3035. goto failed;
  3036. if (drv->data_tx_status) {
  3037. int enabled = 1;
  3038. if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
  3039. &enabled, sizeof(enabled)) < 0) {
  3040. wpa_printf(MSG_DEBUG,
  3041. "nl80211: wifi status sockopt failed\n");
  3042. drv->data_tx_status = 0;
  3043. if (!drv->use_monitor)
  3044. drv->capa.flags &=
  3045. ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
  3046. } else {
  3047. eloop_register_read_sock(drv->eapol_tx_sock,
  3048. wpa_driver_nl80211_handle_eapol_tx_status,
  3049. drv, NULL);
  3050. }
  3051. }
  3052. if (drv->global) {
  3053. dl_list_add(&drv->global->interfaces, &drv->list);
  3054. drv->in_interface_list = 1;
  3055. }
  3056. return bss;
  3057. failed:
  3058. wpa_driver_nl80211_deinit(bss);
  3059. return NULL;
  3060. }
  3061. static int nl80211_register_frame(struct i802_bss *bss,
  3062. struct nl_handle *nl_handle,
  3063. u16 type, const u8 *match, size_t match_len)
  3064. {
  3065. struct wpa_driver_nl80211_data *drv = bss->drv;
  3066. struct nl_msg *msg;
  3067. int ret = -1;
  3068. msg = nlmsg_alloc();
  3069. if (!msg)
  3070. return -1;
  3071. wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p",
  3072. type, nl_handle);
  3073. wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
  3074. match, match_len);
  3075. nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
  3076. if (nl80211_set_iface_id(msg, bss) < 0)
  3077. goto nla_put_failure;
  3078. NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
  3079. NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
  3080. ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
  3081. msg = NULL;
  3082. if (ret) {
  3083. wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
  3084. "failed (type=%u): ret=%d (%s)",
  3085. type, ret, strerror(-ret));
  3086. wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
  3087. match, match_len);
  3088. goto nla_put_failure;
  3089. }
  3090. ret = 0;
  3091. nla_put_failure:
  3092. nlmsg_free(msg);
  3093. return ret;
  3094. }
  3095. static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
  3096. {
  3097. struct wpa_driver_nl80211_data *drv = bss->drv;
  3098. if (bss->nl_mgmt) {
  3099. wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
  3100. "already on! (nl_mgmt=%p)", bss->nl_mgmt);
  3101. return -1;
  3102. }
  3103. bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
  3104. if (bss->nl_mgmt == NULL)
  3105. return -1;
  3106. eloop_register_read_sock(nl_socket_get_fd(bss->nl_mgmt),
  3107. wpa_driver_nl80211_event_receive, bss->nl_cb,
  3108. bss->nl_mgmt);
  3109. return 0;
  3110. }
  3111. static int nl80211_register_action_frame(struct i802_bss *bss,
  3112. const u8 *match, size_t match_len)
  3113. {
  3114. u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
  3115. return nl80211_register_frame(bss, bss->nl_mgmt,
  3116. type, match, match_len);
  3117. }
  3118. static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
  3119. {
  3120. struct wpa_driver_nl80211_data *drv = bss->drv;
  3121. if (nl80211_alloc_mgmt_handle(bss))
  3122. return -1;
  3123. wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
  3124. "handle %p", bss->nl_mgmt);
  3125. #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
  3126. /* GAS Initial Request */
  3127. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
  3128. return -1;
  3129. /* GAS Initial Response */
  3130. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
  3131. return -1;
  3132. /* GAS Comeback Request */
  3133. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
  3134. return -1;
  3135. /* GAS Comeback Response */
  3136. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
  3137. return -1;
  3138. #endif /* CONFIG_P2P || CONFIG_INTERWORKING */
  3139. #ifdef CONFIG_P2P
  3140. /* P2P Public Action */
  3141. if (nl80211_register_action_frame(bss,
  3142. (u8 *) "\x04\x09\x50\x6f\x9a\x09",
  3143. 6) < 0)
  3144. return -1;
  3145. /* P2P Action */
  3146. if (nl80211_register_action_frame(bss,
  3147. (u8 *) "\x7f\x50\x6f\x9a\x09",
  3148. 5) < 0)
  3149. return -1;
  3150. #endif /* CONFIG_P2P */
  3151. #ifdef CONFIG_IEEE80211W
  3152. /* SA Query Response */
  3153. if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
  3154. return -1;
  3155. #endif /* CONFIG_IEEE80211W */
  3156. #ifdef CONFIG_TDLS
  3157. if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
  3158. /* TDLS Discovery Response */
  3159. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
  3160. 0)
  3161. return -1;
  3162. }
  3163. #endif /* CONFIG_TDLS */
  3164. /* FT Action frames */
  3165. if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
  3166. return -1;
  3167. else
  3168. drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
  3169. WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
  3170. /* WNM - BSS Transition Management Request */
  3171. if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
  3172. return -1;
  3173. /* WNM-Sleep Mode Response */
  3174. if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
  3175. return -1;
  3176. return 0;
  3177. }
  3178. static int nl80211_register_spurious_class3(struct i802_bss *bss)
  3179. {
  3180. struct wpa_driver_nl80211_data *drv = bss->drv;
  3181. struct nl_msg *msg;
  3182. int ret = -1;
  3183. msg = nlmsg_alloc();
  3184. if (!msg)
  3185. return -1;
  3186. nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
  3187. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  3188. ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
  3189. msg = NULL;
  3190. if (ret) {
  3191. wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
  3192. "failed: ret=%d (%s)",
  3193. ret, strerror(-ret));
  3194. goto nla_put_failure;
  3195. }
  3196. ret = 0;
  3197. nla_put_failure:
  3198. nlmsg_free(msg);
  3199. return ret;
  3200. }
  3201. static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
  3202. {
  3203. static const int stypes[] = {
  3204. WLAN_FC_STYPE_AUTH,
  3205. WLAN_FC_STYPE_ASSOC_REQ,
  3206. WLAN_FC_STYPE_REASSOC_REQ,
  3207. WLAN_FC_STYPE_DISASSOC,
  3208. WLAN_FC_STYPE_DEAUTH,
  3209. WLAN_FC_STYPE_ACTION,
  3210. WLAN_FC_STYPE_PROBE_REQ,
  3211. /* Beacon doesn't work as mac80211 doesn't currently allow
  3212. * it, but it wouldn't really be the right thing anyway as
  3213. * it isn't per interface ... maybe just dump the scan
  3214. * results periodically for OLBC?
  3215. */
  3216. // WLAN_FC_STYPE_BEACON,
  3217. };
  3218. unsigned int i;
  3219. if (nl80211_alloc_mgmt_handle(bss))
  3220. return -1;
  3221. wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
  3222. "handle %p", bss->nl_mgmt);
  3223. for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) {
  3224. if (nl80211_register_frame(bss, bss->nl_mgmt,
  3225. (WLAN_FC_TYPE_MGMT << 2) |
  3226. (stypes[i] << 4),
  3227. NULL, 0) < 0) {
  3228. goto out_err;
  3229. }
  3230. }
  3231. if (nl80211_register_spurious_class3(bss))
  3232. goto out_err;
  3233. if (nl80211_get_wiphy_data_ap(bss) == NULL)
  3234. goto out_err;
  3235. return 0;
  3236. out_err:
  3237. eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
  3238. nl_destroy_handles(&bss->nl_mgmt);
  3239. return -1;
  3240. }
  3241. static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
  3242. {
  3243. if (nl80211_alloc_mgmt_handle(bss))
  3244. return -1;
  3245. wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
  3246. "handle %p (device SME)", bss->nl_mgmt);
  3247. if (nl80211_register_frame(bss, bss->nl_mgmt,
  3248. (WLAN_FC_TYPE_MGMT << 2) |
  3249. (WLAN_FC_STYPE_ACTION << 4),
  3250. NULL, 0) < 0)
  3251. goto out_err;
  3252. return 0;
  3253. out_err:
  3254. eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
  3255. nl_destroy_handles(&bss->nl_mgmt);
  3256. return -1;
  3257. }
  3258. static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
  3259. {
  3260. if (bss->nl_mgmt == NULL)
  3261. return;
  3262. wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
  3263. "(%s)", bss->nl_mgmt, reason);
  3264. eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
  3265. nl_destroy_handles(&bss->nl_mgmt);
  3266. nl80211_put_wiphy_data_ap(bss);
  3267. }
  3268. static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
  3269. {
  3270. wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
  3271. }
  3272. static void nl80211_del_p2pdev(struct i802_bss *bss)
  3273. {
  3274. struct wpa_driver_nl80211_data *drv = bss->drv;
  3275. struct nl_msg *msg;
  3276. int ret;
  3277. msg = nlmsg_alloc();
  3278. if (!msg)
  3279. return;
  3280. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
  3281. NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
  3282. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3283. msg = NULL;
  3284. wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
  3285. bss->ifname, (long long unsigned int) bss->wdev_id,
  3286. strerror(ret));
  3287. nla_put_failure:
  3288. nlmsg_free(msg);
  3289. }
  3290. static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
  3291. {
  3292. struct wpa_driver_nl80211_data *drv = bss->drv;
  3293. struct nl_msg *msg;
  3294. int ret = -1;
  3295. msg = nlmsg_alloc();
  3296. if (!msg)
  3297. return -1;
  3298. if (start)
  3299. nl80211_cmd(drv, msg, 0, NL80211_CMD_START_P2P_DEVICE);
  3300. else
  3301. nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_P2P_DEVICE);
  3302. NLA_PUT_U64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
  3303. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3304. msg = NULL;
  3305. wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
  3306. start ? "Start" : "Stop",
  3307. bss->ifname, (long long unsigned int) bss->wdev_id,
  3308. strerror(ret));
  3309. nla_put_failure:
  3310. nlmsg_free(msg);
  3311. return ret;
  3312. }
  3313. static int i802_set_iface_flags(struct i802_bss *bss, int up)
  3314. {
  3315. enum nl80211_iftype nlmode;
  3316. nlmode = nl80211_get_ifmode(bss);
  3317. if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
  3318. return linux_set_iface_flags(bss->drv->global->ioctl_sock,
  3319. bss->ifname, up);
  3320. }
  3321. /* P2P Device has start/stop which is equivalent */
  3322. return nl80211_set_p2pdev(bss, up);
  3323. }
  3324. static int
  3325. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
  3326. {
  3327. #ifndef HOSTAPD
  3328. enum nl80211_iftype nlmode = NL80211_IFTYPE_STATION;
  3329. #endif /* HOSTAPD */
  3330. struct i802_bss *bss = &drv->first_bss;
  3331. int send_rfkill_event = 0;
  3332. drv->ifindex = if_nametoindex(bss->ifname);
  3333. bss->ifindex = drv->ifindex;
  3334. bss->wdev_id = drv->global->if_add_wdevid;
  3335. bss->wdev_id_set = drv->global->if_add_wdevid_set;
  3336. bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
  3337. bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
  3338. drv->global->if_add_wdevid_set = 0;
  3339. if (wpa_driver_nl80211_capa(drv))
  3340. return -1;
  3341. wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
  3342. bss->ifname, drv->phyname);
  3343. #ifndef HOSTAPD
  3344. if (bss->if_dynamic)
  3345. nlmode = nl80211_get_ifmode(bss);
  3346. /*
  3347. * Make sure the interface starts up in station mode unless this is a
  3348. * dynamically added interface (e.g., P2P) that was already configured
  3349. * with proper iftype.
  3350. */
  3351. if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
  3352. wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to use managed mode");
  3353. return -1;
  3354. }
  3355. drv->nlmode = nlmode;
  3356. if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
  3357. int ret = nl80211_set_p2pdev(bss, 1);
  3358. if (ret < 0)
  3359. wpa_printf(MSG_ERROR, "nl80211: Could not start P2P device");
  3360. nl80211_get_macaddr(bss);
  3361. return ret;
  3362. }
  3363. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
  3364. if (rfkill_is_blocked(drv->rfkill)) {
  3365. wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
  3366. "interface '%s' due to rfkill",
  3367. bss->ifname);
  3368. drv->if_disabled = 1;
  3369. send_rfkill_event = 1;
  3370. } else {
  3371. wpa_printf(MSG_ERROR, "nl80211: Could not set "
  3372. "interface '%s' UP", bss->ifname);
  3373. return -1;
  3374. }
  3375. }
  3376. netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
  3377. 1, IF_OPER_DORMANT);
  3378. #endif /* HOSTAPD */
  3379. if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  3380. bss->addr))
  3381. return -1;
  3382. if (send_rfkill_event) {
  3383. eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
  3384. drv, drv->ctx);
  3385. }
  3386. return 0;
  3387. }
  3388. static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
  3389. {
  3390. struct nl_msg *msg;
  3391. msg = nlmsg_alloc();
  3392. if (!msg)
  3393. return -ENOMEM;
  3394. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
  3395. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3396. return send_and_recv_msgs(drv, msg, NULL, NULL);
  3397. nla_put_failure:
  3398. nlmsg_free(msg);
  3399. return -ENOBUFS;
  3400. }
  3401. /**
  3402. * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
  3403. * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
  3404. *
  3405. * Shut down driver interface and processing of driver events. Free
  3406. * private data buffer if one was allocated in wpa_driver_nl80211_init().
  3407. */
  3408. static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
  3409. {
  3410. struct wpa_driver_nl80211_data *drv = bss->drv;
  3411. bss->in_deinit = 1;
  3412. if (drv->data_tx_status)
  3413. eloop_unregister_read_sock(drv->eapol_tx_sock);
  3414. if (drv->eapol_tx_sock >= 0)
  3415. close(drv->eapol_tx_sock);
  3416. if (bss->nl_preq)
  3417. wpa_driver_nl80211_probe_req_report(bss, 0);
  3418. if (bss->added_if_into_bridge) {
  3419. if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
  3420. bss->ifname) < 0)
  3421. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  3422. "interface %s from bridge %s: %s",
  3423. bss->ifname, bss->brname, strerror(errno));
  3424. }
  3425. if (bss->added_bridge) {
  3426. if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
  3427. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  3428. "bridge %s: %s",
  3429. bss->brname, strerror(errno));
  3430. }
  3431. nl80211_remove_monitor_interface(drv);
  3432. if (is_ap_interface(drv->nlmode))
  3433. wpa_driver_nl80211_del_beacon(drv);
  3434. #ifdef HOSTAPD
  3435. if (drv->last_freq_ht) {
  3436. /* Clear HT flags from the driver */
  3437. struct hostapd_freq_params freq;
  3438. os_memset(&freq, 0, sizeof(freq));
  3439. freq.freq = drv->last_freq;
  3440. wpa_driver_nl80211_set_freq(bss, &freq);
  3441. }
  3442. if (drv->eapol_sock >= 0) {
  3443. eloop_unregister_read_sock(drv->eapol_sock);
  3444. close(drv->eapol_sock);
  3445. }
  3446. if (drv->if_indices != drv->default_if_indices)
  3447. os_free(drv->if_indices);
  3448. #endif /* HOSTAPD */
  3449. if (drv->disabled_11b_rates)
  3450. nl80211_disable_11b_rates(drv, drv->ifindex, 0);
  3451. netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
  3452. IF_OPER_UP);
  3453. rfkill_deinit(drv->rfkill);
  3454. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  3455. (void) i802_set_iface_flags(bss, 0);
  3456. if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
  3457. wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
  3458. nl80211_mgmt_unsubscribe(bss, "deinit");
  3459. } else {
  3460. nl80211_mgmt_unsubscribe(bss, "deinit");
  3461. nl80211_del_p2pdev(bss);
  3462. }
  3463. nl_cb_put(drv->nl_cb);
  3464. nl80211_destroy_bss(&drv->first_bss);
  3465. os_free(drv->filter_ssids);
  3466. os_free(drv->auth_ie);
  3467. if (drv->in_interface_list)
  3468. dl_list_del(&drv->list);
  3469. os_free(drv->extended_capa);
  3470. os_free(drv->extended_capa_mask);
  3471. os_free(drv);
  3472. }
  3473. /**
  3474. * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
  3475. * @eloop_ctx: Driver private data
  3476. * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
  3477. *
  3478. * This function can be used as registered timeout when starting a scan to
  3479. * generate a scan completed event if the driver does not report this.
  3480. */
  3481. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  3482. {
  3483. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  3484. if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
  3485. wpa_driver_nl80211_set_mode(&drv->first_bss,
  3486. drv->ap_scan_as_station);
  3487. drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
  3488. }
  3489. wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
  3490. wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
  3491. }
  3492. static struct nl_msg *
  3493. nl80211_scan_common(struct wpa_driver_nl80211_data *drv, u8 cmd,
  3494. struct wpa_driver_scan_params *params, u64 *wdev_id)
  3495. {
  3496. struct nl_msg *msg;
  3497. size_t i;
  3498. msg = nlmsg_alloc();
  3499. if (!msg)
  3500. return NULL;
  3501. nl80211_cmd(drv, msg, 0, cmd);
  3502. if (!wdev_id)
  3503. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3504. else
  3505. NLA_PUT_U64(msg, NL80211_ATTR_WDEV, *wdev_id);
  3506. if (params->num_ssids) {
  3507. struct nlattr *ssids;
  3508. ssids = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
  3509. if (ssids == NULL)
  3510. goto fail;
  3511. for (i = 0; i < params->num_ssids; i++) {
  3512. wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
  3513. params->ssids[i].ssid,
  3514. params->ssids[i].ssid_len);
  3515. if (nla_put(msg, i + 1, params->ssids[i].ssid_len,
  3516. params->ssids[i].ssid) < 0)
  3517. goto fail;
  3518. }
  3519. nla_nest_end(msg, ssids);
  3520. }
  3521. if (params->extra_ies) {
  3522. wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
  3523. params->extra_ies, params->extra_ies_len);
  3524. if (nla_put(msg, NL80211_ATTR_IE, params->extra_ies_len,
  3525. params->extra_ies) < 0)
  3526. goto fail;
  3527. }
  3528. if (params->freqs) {
  3529. struct nlattr *freqs;
  3530. freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
  3531. if (freqs == NULL)
  3532. goto fail;
  3533. for (i = 0; params->freqs[i]; i++) {
  3534. wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
  3535. "MHz", params->freqs[i]);
  3536. if (nla_put_u32(msg, i + 1, params->freqs[i]) < 0)
  3537. goto fail;
  3538. }
  3539. nla_nest_end(msg, freqs);
  3540. }
  3541. os_free(drv->filter_ssids);
  3542. drv->filter_ssids = params->filter_ssids;
  3543. params->filter_ssids = NULL;
  3544. drv->num_filter_ssids = params->num_filter_ssids;
  3545. return msg;
  3546. fail:
  3547. nla_put_failure:
  3548. nlmsg_free(msg);
  3549. return NULL;
  3550. }
  3551. /**
  3552. * wpa_driver_nl80211_scan - Request the driver to initiate scan
  3553. * @bss: Pointer to private driver data from wpa_driver_nl80211_init()
  3554. * @params: Scan parameters
  3555. * Returns: 0 on success, -1 on failure
  3556. */
  3557. static int wpa_driver_nl80211_scan(struct i802_bss *bss,
  3558. struct wpa_driver_scan_params *params)
  3559. {
  3560. struct wpa_driver_nl80211_data *drv = bss->drv;
  3561. int ret = -1, timeout;
  3562. struct nl_msg *msg = NULL;
  3563. wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: scan request");
  3564. drv->scan_for_auth = 0;
  3565. msg = nl80211_scan_common(drv, NL80211_CMD_TRIGGER_SCAN, params,
  3566. bss->wdev_id_set ? &bss->wdev_id : NULL);
  3567. if (!msg)
  3568. return -1;
  3569. if (params->p2p_probe) {
  3570. struct nlattr *rates;
  3571. wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
  3572. rates = nla_nest_start(msg, NL80211_ATTR_SCAN_SUPP_RATES);
  3573. if (rates == NULL)
  3574. goto nla_put_failure;
  3575. /*
  3576. * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
  3577. * by masking out everything else apart from the OFDM rates 6,
  3578. * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
  3579. * rates are left enabled.
  3580. */
  3581. NLA_PUT(msg, NL80211_BAND_2GHZ, 8,
  3582. "\x0c\x12\x18\x24\x30\x48\x60\x6c");
  3583. nla_nest_end(msg, rates);
  3584. NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
  3585. }
  3586. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3587. msg = NULL;
  3588. if (ret) {
  3589. wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
  3590. "(%s)", ret, strerror(-ret));
  3591. #ifdef HOSTAPD
  3592. if (is_ap_interface(drv->nlmode)) {
  3593. /*
  3594. * mac80211 does not allow scan requests in AP mode, so
  3595. * try to do this in station mode.
  3596. */
  3597. if (wpa_driver_nl80211_set_mode(
  3598. bss, NL80211_IFTYPE_STATION))
  3599. goto nla_put_failure;
  3600. if (wpa_driver_nl80211_scan(bss, params)) {
  3601. wpa_driver_nl80211_set_mode(bss, drv->nlmode);
  3602. goto nla_put_failure;
  3603. }
  3604. /* Restore AP mode when processing scan results */
  3605. drv->ap_scan_as_station = drv->nlmode;
  3606. ret = 0;
  3607. } else
  3608. goto nla_put_failure;
  3609. #else /* HOSTAPD */
  3610. goto nla_put_failure;
  3611. #endif /* HOSTAPD */
  3612. }
  3613. /* Not all drivers generate "scan completed" wireless event, so try to
  3614. * read results after a timeout. */
  3615. timeout = 10;
  3616. if (drv->scan_complete_events) {
  3617. /*
  3618. * The driver seems to deliver events to notify when scan is
  3619. * complete, so use longer timeout to avoid race conditions
  3620. * with scanning and following association request.
  3621. */
  3622. timeout = 30;
  3623. }
  3624. wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
  3625. "seconds", ret, timeout);
  3626. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  3627. eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
  3628. drv, drv->ctx);
  3629. nla_put_failure:
  3630. nlmsg_free(msg);
  3631. return ret;
  3632. }
  3633. /**
  3634. * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
  3635. * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
  3636. * @params: Scan parameters
  3637. * @interval: Interval between scan cycles in milliseconds
  3638. * Returns: 0 on success, -1 on failure or if not supported
  3639. */
  3640. static int wpa_driver_nl80211_sched_scan(void *priv,
  3641. struct wpa_driver_scan_params *params,
  3642. u32 interval)
  3643. {
  3644. struct i802_bss *bss = priv;
  3645. struct wpa_driver_nl80211_data *drv = bss->drv;
  3646. int ret = -1;
  3647. struct nl_msg *msg;
  3648. size_t i;
  3649. wpa_dbg(drv->ctx, MSG_DEBUG, "nl80211: sched_scan request");
  3650. #ifdef ANDROID
  3651. if (!drv->capa.sched_scan_supported)
  3652. return android_pno_start(bss, params);
  3653. #endif /* ANDROID */
  3654. msg = nl80211_scan_common(drv, NL80211_CMD_START_SCHED_SCAN, params,
  3655. bss->wdev_id_set ? &bss->wdev_id : NULL);
  3656. if (!msg)
  3657. goto nla_put_failure;
  3658. NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
  3659. if ((drv->num_filter_ssids &&
  3660. (int) drv->num_filter_ssids <= drv->capa.max_match_sets) ||
  3661. params->filter_rssi) {
  3662. struct nlattr *match_sets;
  3663. match_sets = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
  3664. if (match_sets == NULL)
  3665. goto nla_put_failure;
  3666. for (i = 0; i < drv->num_filter_ssids; i++) {
  3667. struct nlattr *match_set_ssid;
  3668. wpa_hexdump_ascii(MSG_MSGDUMP,
  3669. "nl80211: Sched scan filter SSID",
  3670. drv->filter_ssids[i].ssid,
  3671. drv->filter_ssids[i].ssid_len);
  3672. match_set_ssid = nla_nest_start(msg, i + 1);
  3673. if (match_set_ssid == NULL)
  3674. goto nla_put_failure;
  3675. NLA_PUT(msg, NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
  3676. drv->filter_ssids[i].ssid_len,
  3677. drv->filter_ssids[i].ssid);
  3678. nla_nest_end(msg, match_set_ssid);
  3679. }
  3680. if (params->filter_rssi) {
  3681. struct nlattr *match_set_rssi;
  3682. match_set_rssi = nla_nest_start(msg, 0);
  3683. if (match_set_rssi == NULL)
  3684. goto nla_put_failure;
  3685. NLA_PUT_U32(msg, NL80211_SCHED_SCAN_MATCH_ATTR_RSSI,
  3686. params->filter_rssi);
  3687. wpa_printf(MSG_MSGDUMP,
  3688. "nl80211: Sched scan RSSI filter %d dBm",
  3689. params->filter_rssi);
  3690. nla_nest_end(msg, match_set_rssi);
  3691. }
  3692. nla_nest_end(msg, match_sets);
  3693. }
  3694. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3695. /* TODO: if we get an error here, we should fall back to normal scan */
  3696. msg = NULL;
  3697. if (ret) {
  3698. wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
  3699. "ret=%d (%s)", ret, strerror(-ret));
  3700. goto nla_put_failure;
  3701. }
  3702. wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
  3703. "scan interval %d msec", ret, interval);
  3704. nla_put_failure:
  3705. nlmsg_free(msg);
  3706. return ret;
  3707. }
  3708. /**
  3709. * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
  3710. * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
  3711. * Returns: 0 on success, -1 on failure or if not supported
  3712. */
  3713. static int wpa_driver_nl80211_stop_sched_scan(void *priv)
  3714. {
  3715. struct i802_bss *bss = priv;
  3716. struct wpa_driver_nl80211_data *drv = bss->drv;
  3717. int ret = 0;
  3718. struct nl_msg *msg;
  3719. #ifdef ANDROID
  3720. if (!drv->capa.sched_scan_supported)
  3721. return android_pno_stop(bss);
  3722. #endif /* ANDROID */
  3723. msg = nlmsg_alloc();
  3724. if (!msg)
  3725. return -1;
  3726. nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
  3727. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3728. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3729. msg = NULL;
  3730. if (ret) {
  3731. wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
  3732. "ret=%d (%s)", ret, strerror(-ret));
  3733. goto nla_put_failure;
  3734. }
  3735. wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
  3736. nla_put_failure:
  3737. nlmsg_free(msg);
  3738. return ret;
  3739. }
  3740. static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
  3741. {
  3742. const u8 *end, *pos;
  3743. if (ies == NULL)
  3744. return NULL;
  3745. pos = ies;
  3746. end = ies + ies_len;
  3747. while (pos + 1 < end) {
  3748. if (pos + 2 + pos[1] > end)
  3749. break;
  3750. if (pos[0] == ie)
  3751. return pos;
  3752. pos += 2 + pos[1];
  3753. }
  3754. return NULL;
  3755. }
  3756. static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
  3757. const u8 *ie, size_t ie_len)
  3758. {
  3759. const u8 *ssid;
  3760. size_t i;
  3761. if (drv->filter_ssids == NULL)
  3762. return 0;
  3763. ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
  3764. if (ssid == NULL)
  3765. return 1;
  3766. for (i = 0; i < drv->num_filter_ssids; i++) {
  3767. if (ssid[1] == drv->filter_ssids[i].ssid_len &&
  3768. os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
  3769. 0)
  3770. return 0;
  3771. }
  3772. return 1;
  3773. }
  3774. static int bss_info_handler(struct nl_msg *msg, void *arg)
  3775. {
  3776. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  3777. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  3778. struct nlattr *bss[NL80211_BSS_MAX + 1];
  3779. static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
  3780. [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
  3781. [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
  3782. [NL80211_BSS_TSF] = { .type = NLA_U64 },
  3783. [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
  3784. [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
  3785. [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
  3786. [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
  3787. [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
  3788. [NL80211_BSS_STATUS] = { .type = NLA_U32 },
  3789. [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
  3790. [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
  3791. };
  3792. struct nl80211_bss_info_arg *_arg = arg;
  3793. struct wpa_scan_results *res = _arg->res;
  3794. struct wpa_scan_res **tmp;
  3795. struct wpa_scan_res *r;
  3796. const u8 *ie, *beacon_ie;
  3797. size_t ie_len, beacon_ie_len;
  3798. u8 *pos;
  3799. size_t i;
  3800. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  3801. genlmsg_attrlen(gnlh, 0), NULL);
  3802. if (!tb[NL80211_ATTR_BSS])
  3803. return NL_SKIP;
  3804. if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
  3805. bss_policy))
  3806. return NL_SKIP;
  3807. if (bss[NL80211_BSS_STATUS]) {
  3808. enum nl80211_bss_status status;
  3809. status = nla_get_u32(bss[NL80211_BSS_STATUS]);
  3810. if (status == NL80211_BSS_STATUS_ASSOCIATED &&
  3811. bss[NL80211_BSS_FREQUENCY]) {
  3812. _arg->assoc_freq =
  3813. nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
  3814. wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
  3815. _arg->assoc_freq);
  3816. }
  3817. if (status == NL80211_BSS_STATUS_ASSOCIATED &&
  3818. bss[NL80211_BSS_BSSID]) {
  3819. os_memcpy(_arg->assoc_bssid,
  3820. nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
  3821. wpa_printf(MSG_DEBUG, "nl80211: Associated with "
  3822. MACSTR, MAC2STR(_arg->assoc_bssid));
  3823. }
  3824. }
  3825. if (!res)
  3826. return NL_SKIP;
  3827. if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
  3828. ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  3829. ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  3830. } else {
  3831. ie = NULL;
  3832. ie_len = 0;
  3833. }
  3834. if (bss[NL80211_BSS_BEACON_IES]) {
  3835. beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
  3836. beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
  3837. } else {
  3838. beacon_ie = NULL;
  3839. beacon_ie_len = 0;
  3840. }
  3841. if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
  3842. ie ? ie_len : beacon_ie_len))
  3843. return NL_SKIP;
  3844. r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
  3845. if (r == NULL)
  3846. return NL_SKIP;
  3847. if (bss[NL80211_BSS_BSSID])
  3848. os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
  3849. ETH_ALEN);
  3850. if (bss[NL80211_BSS_FREQUENCY])
  3851. r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
  3852. if (bss[NL80211_BSS_BEACON_INTERVAL])
  3853. r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
  3854. if (bss[NL80211_BSS_CAPABILITY])
  3855. r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
  3856. r->flags |= WPA_SCAN_NOISE_INVALID;
  3857. if (bss[NL80211_BSS_SIGNAL_MBM]) {
  3858. r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
  3859. r->level /= 100; /* mBm to dBm */
  3860. r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
  3861. } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
  3862. r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
  3863. r->flags |= WPA_SCAN_QUAL_INVALID;
  3864. } else
  3865. r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
  3866. if (bss[NL80211_BSS_TSF])
  3867. r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
  3868. if (bss[NL80211_BSS_SEEN_MS_AGO])
  3869. r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
  3870. r->ie_len = ie_len;
  3871. pos = (u8 *) (r + 1);
  3872. if (ie) {
  3873. os_memcpy(pos, ie, ie_len);
  3874. pos += ie_len;
  3875. }
  3876. r->beacon_ie_len = beacon_ie_len;
  3877. if (beacon_ie)
  3878. os_memcpy(pos, beacon_ie, beacon_ie_len);
  3879. if (bss[NL80211_BSS_STATUS]) {
  3880. enum nl80211_bss_status status;
  3881. status = nla_get_u32(bss[NL80211_BSS_STATUS]);
  3882. switch (status) {
  3883. case NL80211_BSS_STATUS_AUTHENTICATED:
  3884. r->flags |= WPA_SCAN_AUTHENTICATED;
  3885. break;
  3886. case NL80211_BSS_STATUS_ASSOCIATED:
  3887. r->flags |= WPA_SCAN_ASSOCIATED;
  3888. break;
  3889. default:
  3890. break;
  3891. }
  3892. }
  3893. /*
  3894. * cfg80211 maintains separate BSS table entries for APs if the same
  3895. * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
  3896. * not use frequency as a separate key in the BSS table, so filter out
  3897. * duplicated entries. Prefer associated BSS entry in such a case in
  3898. * order to get the correct frequency into the BSS table.
  3899. */
  3900. for (i = 0; i < res->num; i++) {
  3901. const u8 *s1, *s2;
  3902. if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
  3903. continue;
  3904. s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
  3905. res->res[i]->ie_len, WLAN_EID_SSID);
  3906. s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
  3907. if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
  3908. os_memcmp(s1, s2, 2 + s1[1]) != 0)
  3909. continue;
  3910. /* Same BSSID,SSID was already included in scan results */
  3911. wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
  3912. "for " MACSTR, MAC2STR(r->bssid));
  3913. if ((r->flags & WPA_SCAN_ASSOCIATED) &&
  3914. !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
  3915. os_free(res->res[i]);
  3916. res->res[i] = r;
  3917. } else
  3918. os_free(r);
  3919. return NL_SKIP;
  3920. }
  3921. tmp = os_realloc_array(res->res, res->num + 1,
  3922. sizeof(struct wpa_scan_res *));
  3923. if (tmp == NULL) {
  3924. os_free(r);
  3925. return NL_SKIP;
  3926. }
  3927. tmp[res->num++] = r;
  3928. res->res = tmp;
  3929. return NL_SKIP;
  3930. }
  3931. static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
  3932. const u8 *addr)
  3933. {
  3934. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  3935. wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
  3936. "mismatch (" MACSTR ")", MAC2STR(addr));
  3937. wpa_driver_nl80211_mlme(drv, addr,
  3938. NL80211_CMD_DEAUTHENTICATE,
  3939. WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
  3940. }
  3941. }
  3942. static void wpa_driver_nl80211_check_bss_status(
  3943. struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
  3944. {
  3945. size_t i;
  3946. for (i = 0; i < res->num; i++) {
  3947. struct wpa_scan_res *r = res->res[i];
  3948. if (r->flags & WPA_SCAN_AUTHENTICATED) {
  3949. wpa_printf(MSG_DEBUG, "nl80211: Scan results "
  3950. "indicates BSS status with " MACSTR
  3951. " as authenticated",
  3952. MAC2STR(r->bssid));
  3953. if (is_sta_interface(drv->nlmode) &&
  3954. os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
  3955. os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
  3956. 0) {
  3957. wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
  3958. " in local state (auth=" MACSTR
  3959. " assoc=" MACSTR ")",
  3960. MAC2STR(drv->auth_bssid),
  3961. MAC2STR(drv->bssid));
  3962. clear_state_mismatch(drv, r->bssid);
  3963. }
  3964. }
  3965. if (r->flags & WPA_SCAN_ASSOCIATED) {
  3966. wpa_printf(MSG_DEBUG, "nl80211: Scan results "
  3967. "indicate BSS status with " MACSTR
  3968. " as associated",
  3969. MAC2STR(r->bssid));
  3970. if (is_sta_interface(drv->nlmode) &&
  3971. !drv->associated) {
  3972. wpa_printf(MSG_DEBUG, "nl80211: Local state "
  3973. "(not associated) does not match "
  3974. "with BSS state");
  3975. clear_state_mismatch(drv, r->bssid);
  3976. } else if (is_sta_interface(drv->nlmode) &&
  3977. os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
  3978. 0) {
  3979. wpa_printf(MSG_DEBUG, "nl80211: Local state "
  3980. "(associated with " MACSTR ") does "
  3981. "not match with BSS state",
  3982. MAC2STR(drv->bssid));
  3983. clear_state_mismatch(drv, r->bssid);
  3984. clear_state_mismatch(drv, drv->bssid);
  3985. }
  3986. }
  3987. }
  3988. }
  3989. static struct wpa_scan_results *
  3990. nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
  3991. {
  3992. struct nl_msg *msg;
  3993. struct wpa_scan_results *res;
  3994. int ret;
  3995. struct nl80211_bss_info_arg arg;
  3996. res = os_zalloc(sizeof(*res));
  3997. if (res == NULL)
  3998. return NULL;
  3999. msg = nlmsg_alloc();
  4000. if (!msg)
  4001. goto nla_put_failure;
  4002. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
  4003. if (nl80211_set_iface_id(msg, &drv->first_bss) < 0)
  4004. goto nla_put_failure;
  4005. arg.drv = drv;
  4006. arg.res = res;
  4007. ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
  4008. msg = NULL;
  4009. if (ret == 0) {
  4010. wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
  4011. "BSSes)", (unsigned long) res->num);
  4012. nl80211_get_noise_for_scan_results(drv, res);
  4013. return res;
  4014. }
  4015. wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
  4016. "(%s)", ret, strerror(-ret));
  4017. nla_put_failure:
  4018. nlmsg_free(msg);
  4019. wpa_scan_results_free(res);
  4020. return NULL;
  4021. }
  4022. /**
  4023. * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
  4024. * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
  4025. * Returns: Scan results on success, -1 on failure
  4026. */
  4027. static struct wpa_scan_results *
  4028. wpa_driver_nl80211_get_scan_results(void *priv)
  4029. {
  4030. struct i802_bss *bss = priv;
  4031. struct wpa_driver_nl80211_data *drv = bss->drv;
  4032. struct wpa_scan_results *res;
  4033. res = nl80211_get_scan_results(drv);
  4034. if (res)
  4035. wpa_driver_nl80211_check_bss_status(drv, res);
  4036. return res;
  4037. }
  4038. static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
  4039. {
  4040. struct wpa_scan_results *res;
  4041. size_t i;
  4042. res = nl80211_get_scan_results(drv);
  4043. if (res == NULL) {
  4044. wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
  4045. return;
  4046. }
  4047. wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
  4048. for (i = 0; i < res->num; i++) {
  4049. struct wpa_scan_res *r = res->res[i];
  4050. wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
  4051. (int) i, (int) res->num, MAC2STR(r->bssid),
  4052. r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
  4053. r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
  4054. }
  4055. wpa_scan_results_free(res);
  4056. }
  4057. static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
  4058. enum wpa_alg alg, const u8 *addr,
  4059. int key_idx, int set_tx,
  4060. const u8 *seq, size_t seq_len,
  4061. const u8 *key, size_t key_len)
  4062. {
  4063. struct wpa_driver_nl80211_data *drv = bss->drv;
  4064. int ifindex;
  4065. struct nl_msg *msg;
  4066. int ret;
  4067. int tdls = 0;
  4068. /* Ignore for P2P Device */
  4069. if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
  4070. return 0;
  4071. ifindex = if_nametoindex(ifname);
  4072. wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
  4073. "set_tx=%d seq_len=%lu key_len=%lu",
  4074. __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
  4075. (unsigned long) seq_len, (unsigned long) key_len);
  4076. #ifdef CONFIG_TDLS
  4077. if (key_idx == -1) {
  4078. key_idx = 0;
  4079. tdls = 1;
  4080. }
  4081. #endif /* CONFIG_TDLS */
  4082. msg = nlmsg_alloc();
  4083. if (!msg)
  4084. return -ENOMEM;
  4085. if (alg == WPA_ALG_NONE) {
  4086. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
  4087. } else {
  4088. nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
  4089. NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
  4090. switch (alg) {
  4091. case WPA_ALG_WEP:
  4092. if (key_len == 5)
  4093. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  4094. WLAN_CIPHER_SUITE_WEP40);
  4095. else
  4096. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  4097. WLAN_CIPHER_SUITE_WEP104);
  4098. break;
  4099. case WPA_ALG_TKIP:
  4100. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  4101. WLAN_CIPHER_SUITE_TKIP);
  4102. break;
  4103. case WPA_ALG_CCMP:
  4104. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  4105. WLAN_CIPHER_SUITE_CCMP);
  4106. break;
  4107. case WPA_ALG_GCMP:
  4108. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  4109. WLAN_CIPHER_SUITE_GCMP);
  4110. break;
  4111. case WPA_ALG_IGTK:
  4112. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  4113. WLAN_CIPHER_SUITE_AES_CMAC);
  4114. break;
  4115. case WPA_ALG_SMS4:
  4116. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  4117. WLAN_CIPHER_SUITE_SMS4);
  4118. break;
  4119. case WPA_ALG_KRK:
  4120. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  4121. WLAN_CIPHER_SUITE_KRK);
  4122. break;
  4123. default:
  4124. wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
  4125. "algorithm %d", __func__, alg);
  4126. nlmsg_free(msg);
  4127. return -1;
  4128. }
  4129. }
  4130. if (seq && seq_len)
  4131. NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
  4132. if (addr && !is_broadcast_ether_addr(addr)) {
  4133. wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
  4134. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  4135. if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
  4136. wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
  4137. NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
  4138. NL80211_KEYTYPE_GROUP);
  4139. }
  4140. } else if (addr && is_broadcast_ether_addr(addr)) {
  4141. struct nlattr *types;
  4142. wpa_printf(MSG_DEBUG, " broadcast key");
  4143. types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
  4144. if (!types)
  4145. goto nla_put_failure;
  4146. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
  4147. nla_nest_end(msg, types);
  4148. }
  4149. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  4150. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  4151. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4152. if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
  4153. ret = 0;
  4154. if (ret)
  4155. wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
  4156. ret, strerror(-ret));
  4157. /*
  4158. * If we failed or don't need to set the default TX key (below),
  4159. * we're done here.
  4160. */
  4161. if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
  4162. return ret;
  4163. if (is_ap_interface(drv->nlmode) && addr &&
  4164. !is_broadcast_ether_addr(addr))
  4165. return ret;
  4166. msg = nlmsg_alloc();
  4167. if (!msg)
  4168. return -ENOMEM;
  4169. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
  4170. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  4171. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  4172. if (alg == WPA_ALG_IGTK)
  4173. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
  4174. else
  4175. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
  4176. if (addr && is_broadcast_ether_addr(addr)) {
  4177. struct nlattr *types;
  4178. types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
  4179. if (!types)
  4180. goto nla_put_failure;
  4181. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
  4182. nla_nest_end(msg, types);
  4183. } else if (addr) {
  4184. struct nlattr *types;
  4185. types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
  4186. if (!types)
  4187. goto nla_put_failure;
  4188. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST);
  4189. nla_nest_end(msg, types);
  4190. }
  4191. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4192. if (ret == -ENOENT)
  4193. ret = 0;
  4194. if (ret)
  4195. wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
  4196. "err=%d %s)", ret, strerror(-ret));
  4197. return ret;
  4198. nla_put_failure:
  4199. nlmsg_free(msg);
  4200. return -ENOBUFS;
  4201. }
  4202. static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
  4203. int key_idx, int defkey,
  4204. const u8 *seq, size_t seq_len,
  4205. const u8 *key, size_t key_len)
  4206. {
  4207. struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
  4208. if (!key_attr)
  4209. return -1;
  4210. if (defkey && alg == WPA_ALG_IGTK)
  4211. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
  4212. else if (defkey)
  4213. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
  4214. NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
  4215. switch (alg) {
  4216. case WPA_ALG_WEP:
  4217. if (key_len == 5)
  4218. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  4219. WLAN_CIPHER_SUITE_WEP40);
  4220. else
  4221. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  4222. WLAN_CIPHER_SUITE_WEP104);
  4223. break;
  4224. case WPA_ALG_TKIP:
  4225. NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
  4226. break;
  4227. case WPA_ALG_CCMP:
  4228. NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
  4229. break;
  4230. case WPA_ALG_GCMP:
  4231. NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_GCMP);
  4232. break;
  4233. case WPA_ALG_IGTK:
  4234. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  4235. WLAN_CIPHER_SUITE_AES_CMAC);
  4236. break;
  4237. default:
  4238. wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
  4239. "algorithm %d", __func__, alg);
  4240. return -1;
  4241. }
  4242. if (seq && seq_len)
  4243. NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
  4244. NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
  4245. nla_nest_end(msg, key_attr);
  4246. return 0;
  4247. nla_put_failure:
  4248. return -1;
  4249. }
  4250. static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
  4251. struct nl_msg *msg)
  4252. {
  4253. int i, privacy = 0;
  4254. struct nlattr *nl_keys, *nl_key;
  4255. for (i = 0; i < 4; i++) {
  4256. if (!params->wep_key[i])
  4257. continue;
  4258. privacy = 1;
  4259. break;
  4260. }
  4261. if (params->wps == WPS_MODE_PRIVACY)
  4262. privacy = 1;
  4263. if (params->pairwise_suite &&
  4264. params->pairwise_suite != WPA_CIPHER_NONE)
  4265. privacy = 1;
  4266. if (!privacy)
  4267. return 0;
  4268. NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
  4269. nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
  4270. if (!nl_keys)
  4271. goto nla_put_failure;
  4272. for (i = 0; i < 4; i++) {
  4273. if (!params->wep_key[i])
  4274. continue;
  4275. nl_key = nla_nest_start(msg, i);
  4276. if (!nl_key)
  4277. goto nla_put_failure;
  4278. NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
  4279. params->wep_key[i]);
  4280. if (params->wep_key_len[i] == 5)
  4281. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  4282. WLAN_CIPHER_SUITE_WEP40);
  4283. else
  4284. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  4285. WLAN_CIPHER_SUITE_WEP104);
  4286. NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
  4287. if (i == params->wep_tx_keyidx)
  4288. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
  4289. nla_nest_end(msg, nl_key);
  4290. }
  4291. nla_nest_end(msg, nl_keys);
  4292. return 0;
  4293. nla_put_failure:
  4294. return -ENOBUFS;
  4295. }
  4296. static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  4297. const u8 *addr, int cmd, u16 reason_code,
  4298. int local_state_change)
  4299. {
  4300. int ret = -1;
  4301. struct nl_msg *msg;
  4302. msg = nlmsg_alloc();
  4303. if (!msg)
  4304. return -1;
  4305. nl80211_cmd(drv, msg, 0, cmd);
  4306. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4307. NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
  4308. if (addr)
  4309. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  4310. if (local_state_change)
  4311. NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
  4312. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4313. msg = NULL;
  4314. if (ret) {
  4315. wpa_dbg(drv->ctx, MSG_DEBUG,
  4316. "nl80211: MLME command failed: reason=%u ret=%d (%s)",
  4317. reason_code, ret, strerror(-ret));
  4318. goto nla_put_failure;
  4319. }
  4320. ret = 0;
  4321. nla_put_failure:
  4322. nlmsg_free(msg);
  4323. return ret;
  4324. }
  4325. static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
  4326. int reason_code)
  4327. {
  4328. int ret;
  4329. wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
  4330. nl80211_mark_disconnected(drv);
  4331. /* Disconnect command doesn't need BSSID - it uses cached value */
  4332. ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
  4333. reason_code, 0);
  4334. /*
  4335. * For locally generated disconnect, supplicant already generates a
  4336. * DEAUTH event, so ignore the event from NL80211.
  4337. */
  4338. drv->ignore_next_local_disconnect = ret == 0;
  4339. return ret;
  4340. }
  4341. static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
  4342. const u8 *addr, int reason_code)
  4343. {
  4344. struct wpa_driver_nl80211_data *drv = bss->drv;
  4345. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
  4346. return wpa_driver_nl80211_disconnect(drv, reason_code);
  4347. wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
  4348. __func__, MAC2STR(addr), reason_code);
  4349. nl80211_mark_disconnected(drv);
  4350. if (drv->nlmode == NL80211_IFTYPE_ADHOC)
  4351. return nl80211_leave_ibss(drv);
  4352. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
  4353. reason_code, 0);
  4354. }
  4355. static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
  4356. struct wpa_driver_auth_params *params)
  4357. {
  4358. int i;
  4359. drv->auth_freq = params->freq;
  4360. drv->auth_alg = params->auth_alg;
  4361. drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
  4362. drv->auth_local_state_change = params->local_state_change;
  4363. drv->auth_p2p = params->p2p;
  4364. if (params->bssid)
  4365. os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
  4366. else
  4367. os_memset(drv->auth_bssid_, 0, ETH_ALEN);
  4368. if (params->ssid) {
  4369. os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
  4370. drv->auth_ssid_len = params->ssid_len;
  4371. } else
  4372. drv->auth_ssid_len = 0;
  4373. os_free(drv->auth_ie);
  4374. drv->auth_ie = NULL;
  4375. drv->auth_ie_len = 0;
  4376. if (params->ie) {
  4377. drv->auth_ie = os_malloc(params->ie_len);
  4378. if (drv->auth_ie) {
  4379. os_memcpy(drv->auth_ie, params->ie, params->ie_len);
  4380. drv->auth_ie_len = params->ie_len;
  4381. }
  4382. }
  4383. for (i = 0; i < 4; i++) {
  4384. if (params->wep_key[i] && params->wep_key_len[i] &&
  4385. params->wep_key_len[i] <= 16) {
  4386. os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
  4387. params->wep_key_len[i]);
  4388. drv->auth_wep_key_len[i] = params->wep_key_len[i];
  4389. } else
  4390. drv->auth_wep_key_len[i] = 0;
  4391. }
  4392. }
  4393. static int wpa_driver_nl80211_authenticate(
  4394. struct i802_bss *bss, struct wpa_driver_auth_params *params)
  4395. {
  4396. struct wpa_driver_nl80211_data *drv = bss->drv;
  4397. int ret = -1, i;
  4398. struct nl_msg *msg;
  4399. enum nl80211_auth_type type;
  4400. enum nl80211_iftype nlmode;
  4401. int count = 0;
  4402. int is_retry;
  4403. is_retry = drv->retry_auth;
  4404. drv->retry_auth = 0;
  4405. nl80211_mark_disconnected(drv);
  4406. os_memset(drv->auth_bssid, 0, ETH_ALEN);
  4407. if (params->bssid)
  4408. os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
  4409. else
  4410. os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
  4411. /* FIX: IBSS mode */
  4412. nlmode = params->p2p ?
  4413. NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
  4414. if (drv->nlmode != nlmode &&
  4415. wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
  4416. return -1;
  4417. retry:
  4418. msg = nlmsg_alloc();
  4419. if (!msg)
  4420. return -1;
  4421. wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
  4422. drv->ifindex);
  4423. nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
  4424. for (i = 0; i < 4; i++) {
  4425. if (!params->wep_key[i])
  4426. continue;
  4427. wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
  4428. NULL, i,
  4429. i == params->wep_tx_keyidx, NULL, 0,
  4430. params->wep_key[i],
  4431. params->wep_key_len[i]);
  4432. if (params->wep_tx_keyidx != i)
  4433. continue;
  4434. if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
  4435. params->wep_key[i], params->wep_key_len[i])) {
  4436. nlmsg_free(msg);
  4437. return -1;
  4438. }
  4439. }
  4440. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4441. if (params->bssid) {
  4442. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  4443. MAC2STR(params->bssid));
  4444. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  4445. }
  4446. if (params->freq) {
  4447. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  4448. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  4449. }
  4450. if (params->ssid) {
  4451. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  4452. params->ssid, params->ssid_len);
  4453. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  4454. params->ssid);
  4455. }
  4456. wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
  4457. if (params->ie)
  4458. NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
  4459. if (params->sae_data) {
  4460. wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
  4461. params->sae_data_len);
  4462. NLA_PUT(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
  4463. params->sae_data);
  4464. }
  4465. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  4466. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  4467. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  4468. type = NL80211_AUTHTYPE_SHARED_KEY;
  4469. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  4470. type = NL80211_AUTHTYPE_NETWORK_EAP;
  4471. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  4472. type = NL80211_AUTHTYPE_FT;
  4473. else if (params->auth_alg & WPA_AUTH_ALG_SAE)
  4474. type = NL80211_AUTHTYPE_SAE;
  4475. else
  4476. goto nla_put_failure;
  4477. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  4478. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
  4479. if (params->local_state_change) {
  4480. wpa_printf(MSG_DEBUG, " * Local state change only");
  4481. NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
  4482. }
  4483. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4484. msg = NULL;
  4485. if (ret) {
  4486. wpa_dbg(drv->ctx, MSG_DEBUG,
  4487. "nl80211: MLME command failed (auth): ret=%d (%s)",
  4488. ret, strerror(-ret));
  4489. count++;
  4490. if (ret == -EALREADY && count == 1 && params->bssid &&
  4491. !params->local_state_change) {
  4492. /*
  4493. * mac80211 does not currently accept new
  4494. * authentication if we are already authenticated. As a
  4495. * workaround, force deauthentication and try again.
  4496. */
  4497. wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
  4498. "after forced deauthentication");
  4499. wpa_driver_nl80211_deauthenticate(
  4500. bss, params->bssid,
  4501. WLAN_REASON_PREV_AUTH_NOT_VALID);
  4502. nlmsg_free(msg);
  4503. goto retry;
  4504. }
  4505. if (ret == -ENOENT && params->freq && !is_retry) {
  4506. /*
  4507. * cfg80211 has likely expired the BSS entry even
  4508. * though it was previously available in our internal
  4509. * BSS table. To recover quickly, start a single
  4510. * channel scan on the specified channel.
  4511. */
  4512. struct wpa_driver_scan_params scan;
  4513. int freqs[2];
  4514. os_memset(&scan, 0, sizeof(scan));
  4515. scan.num_ssids = 1;
  4516. if (params->ssid) {
  4517. scan.ssids[0].ssid = params->ssid;
  4518. scan.ssids[0].ssid_len = params->ssid_len;
  4519. }
  4520. freqs[0] = params->freq;
  4521. freqs[1] = 0;
  4522. scan.freqs = freqs;
  4523. wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
  4524. "channel scan to refresh cfg80211 BSS "
  4525. "entry");
  4526. ret = wpa_driver_nl80211_scan(bss, &scan);
  4527. if (ret == 0) {
  4528. nl80211_copy_auth_params(drv, params);
  4529. drv->scan_for_auth = 1;
  4530. }
  4531. } else if (is_retry) {
  4532. /*
  4533. * Need to indicate this with an event since the return
  4534. * value from the retry is not delivered to core code.
  4535. */
  4536. union wpa_event_data event;
  4537. wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
  4538. "failed");
  4539. os_memset(&event, 0, sizeof(event));
  4540. os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
  4541. ETH_ALEN);
  4542. wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
  4543. &event);
  4544. }
  4545. goto nla_put_failure;
  4546. }
  4547. ret = 0;
  4548. wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
  4549. "successfully");
  4550. nla_put_failure:
  4551. nlmsg_free(msg);
  4552. return ret;
  4553. }
  4554. static int wpa_driver_nl80211_authenticate_retry(
  4555. struct wpa_driver_nl80211_data *drv)
  4556. {
  4557. struct wpa_driver_auth_params params;
  4558. struct i802_bss *bss = &drv->first_bss;
  4559. int i;
  4560. wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
  4561. os_memset(&params, 0, sizeof(params));
  4562. params.freq = drv->auth_freq;
  4563. params.auth_alg = drv->auth_alg;
  4564. params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
  4565. params.local_state_change = drv->auth_local_state_change;
  4566. params.p2p = drv->auth_p2p;
  4567. if (!is_zero_ether_addr(drv->auth_bssid_))
  4568. params.bssid = drv->auth_bssid_;
  4569. if (drv->auth_ssid_len) {
  4570. params.ssid = drv->auth_ssid;
  4571. params.ssid_len = drv->auth_ssid_len;
  4572. }
  4573. params.ie = drv->auth_ie;
  4574. params.ie_len = drv->auth_ie_len;
  4575. for (i = 0; i < 4; i++) {
  4576. if (drv->auth_wep_key_len[i]) {
  4577. params.wep_key[i] = drv->auth_wep_key[i];
  4578. params.wep_key_len[i] = drv->auth_wep_key_len[i];
  4579. }
  4580. }
  4581. drv->retry_auth = 1;
  4582. return wpa_driver_nl80211_authenticate(bss, &params);
  4583. }
  4584. struct phy_info_arg {
  4585. u16 *num_modes;
  4586. struct hostapd_hw_modes *modes;
  4587. int last_mode, last_chan_idx;
  4588. };
  4589. static void phy_info_ht_capa(struct hostapd_hw_modes *mode, struct nlattr *capa,
  4590. struct nlattr *ampdu_factor,
  4591. struct nlattr *ampdu_density,
  4592. struct nlattr *mcs_set)
  4593. {
  4594. if (capa)
  4595. mode->ht_capab = nla_get_u16(capa);
  4596. if (ampdu_factor)
  4597. mode->a_mpdu_params |= nla_get_u8(ampdu_factor) & 0x03;
  4598. if (ampdu_density)
  4599. mode->a_mpdu_params |= nla_get_u8(ampdu_density) << 2;
  4600. if (mcs_set && nla_len(mcs_set) >= 16) {
  4601. u8 *mcs;
  4602. mcs = nla_data(mcs_set);
  4603. os_memcpy(mode->mcs_set, mcs, 16);
  4604. }
  4605. }
  4606. static void phy_info_vht_capa(struct hostapd_hw_modes *mode,
  4607. struct nlattr *capa,
  4608. struct nlattr *mcs_set)
  4609. {
  4610. if (capa)
  4611. mode->vht_capab = nla_get_u32(capa);
  4612. if (mcs_set && nla_len(mcs_set) >= 8) {
  4613. u8 *mcs;
  4614. mcs = nla_data(mcs_set);
  4615. os_memcpy(mode->vht_mcs_set, mcs, 8);
  4616. }
  4617. }
  4618. static void phy_info_freq(struct hostapd_hw_modes *mode,
  4619. struct hostapd_channel_data *chan,
  4620. struct nlattr *tb_freq[])
  4621. {
  4622. u8 channel;
  4623. chan->freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
  4624. chan->flag = 0;
  4625. if (ieee80211_freq_to_chan(chan->freq, &channel) != NUM_HOSTAPD_MODES)
  4626. chan->chan = channel;
  4627. if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  4628. chan->flag |= HOSTAPD_CHAN_DISABLED;
  4629. if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
  4630. chan->flag |= HOSTAPD_CHAN_PASSIVE_SCAN;
  4631. if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
  4632. chan->flag |= HOSTAPD_CHAN_NO_IBSS;
  4633. if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
  4634. chan->flag |= HOSTAPD_CHAN_RADAR;
  4635. if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
  4636. !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  4637. chan->max_tx_power = nla_get_u32(
  4638. tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
  4639. if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
  4640. enum nl80211_dfs_state state =
  4641. nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
  4642. switch (state) {
  4643. case NL80211_DFS_USABLE:
  4644. chan->flag |= HOSTAPD_CHAN_DFS_USABLE;
  4645. break;
  4646. case NL80211_DFS_AVAILABLE:
  4647. chan->flag |= HOSTAPD_CHAN_DFS_AVAILABLE;
  4648. break;
  4649. case NL80211_DFS_UNAVAILABLE:
  4650. chan->flag |= HOSTAPD_CHAN_DFS_UNAVAILABLE;
  4651. break;
  4652. }
  4653. }
  4654. }
  4655. static int phy_info_freqs(struct phy_info_arg *phy_info,
  4656. struct hostapd_hw_modes *mode, struct nlattr *tb)
  4657. {
  4658. static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  4659. [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
  4660. [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
  4661. [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
  4662. [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
  4663. [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
  4664. [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
  4665. [NL80211_FREQUENCY_ATTR_DFS_STATE] = { .type = NLA_U32 },
  4666. };
  4667. int new_channels = 0;
  4668. struct hostapd_channel_data *channel;
  4669. struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
  4670. struct nlattr *nl_freq;
  4671. int rem_freq, idx;
  4672. if (tb == NULL)
  4673. return NL_OK;
  4674. nla_for_each_nested(nl_freq, tb, rem_freq) {
  4675. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
  4676. nla_data(nl_freq), nla_len(nl_freq), freq_policy);
  4677. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  4678. continue;
  4679. new_channels++;
  4680. }
  4681. channel = os_realloc_array(mode->channels,
  4682. mode->num_channels + new_channels,
  4683. sizeof(struct hostapd_channel_data));
  4684. if (!channel)
  4685. return NL_SKIP;
  4686. mode->channels = channel;
  4687. mode->num_channels += new_channels;
  4688. idx = phy_info->last_chan_idx;
  4689. nla_for_each_nested(nl_freq, tb, rem_freq) {
  4690. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX,
  4691. nla_data(nl_freq), nla_len(nl_freq), freq_policy);
  4692. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  4693. continue;
  4694. phy_info_freq(mode, &mode->channels[idx], tb_freq);
  4695. idx++;
  4696. }
  4697. phy_info->last_chan_idx = idx;
  4698. return NL_OK;
  4699. }
  4700. static int phy_info_rates(struct hostapd_hw_modes *mode, struct nlattr *tb)
  4701. {
  4702. static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
  4703. [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
  4704. [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] =
  4705. { .type = NLA_FLAG },
  4706. };
  4707. struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
  4708. struct nlattr *nl_rate;
  4709. int rem_rate, idx;
  4710. if (tb == NULL)
  4711. return NL_OK;
  4712. nla_for_each_nested(nl_rate, tb, rem_rate) {
  4713. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
  4714. nla_data(nl_rate), nla_len(nl_rate),
  4715. rate_policy);
  4716. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  4717. continue;
  4718. mode->num_rates++;
  4719. }
  4720. mode->rates = os_calloc(mode->num_rates, sizeof(int));
  4721. if (!mode->rates)
  4722. return NL_SKIP;
  4723. idx = 0;
  4724. nla_for_each_nested(nl_rate, tb, rem_rate) {
  4725. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX,
  4726. nla_data(nl_rate), nla_len(nl_rate),
  4727. rate_policy);
  4728. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  4729. continue;
  4730. mode->rates[idx] = nla_get_u32(
  4731. tb_rate[NL80211_BITRATE_ATTR_RATE]);
  4732. idx++;
  4733. }
  4734. return NL_OK;
  4735. }
  4736. static int phy_info_band(struct phy_info_arg *phy_info, struct nlattr *nl_band)
  4737. {
  4738. struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
  4739. struct hostapd_hw_modes *mode;
  4740. int ret;
  4741. if (phy_info->last_mode != nl_band->nla_type) {
  4742. mode = os_realloc_array(phy_info->modes,
  4743. *phy_info->num_modes + 1,
  4744. sizeof(*mode));
  4745. if (!mode)
  4746. return NL_SKIP;
  4747. phy_info->modes = mode;
  4748. mode = &phy_info->modes[*(phy_info->num_modes)];
  4749. os_memset(mode, 0, sizeof(*mode));
  4750. mode->mode = NUM_HOSTAPD_MODES;
  4751. mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN |
  4752. HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN;
  4753. /*
  4754. * Unsupported VHT MCS stream is defined as value 3, so the VHT
  4755. * MCS RX/TX map must be initialized with 0xffff to mark all 8
  4756. * possible streams as unsupported. This will be overridden if
  4757. * driver advertises VHT support.
  4758. */
  4759. mode->vht_mcs_set[0] = 0xff;
  4760. mode->vht_mcs_set[1] = 0xff;
  4761. mode->vht_mcs_set[4] = 0xff;
  4762. mode->vht_mcs_set[5] = 0xff;
  4763. *(phy_info->num_modes) += 1;
  4764. phy_info->last_mode = nl_band->nla_type;
  4765. phy_info->last_chan_idx = 0;
  4766. } else
  4767. mode = &phy_info->modes[*(phy_info->num_modes) - 1];
  4768. nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
  4769. nla_len(nl_band), NULL);
  4770. phy_info_ht_capa(mode, tb_band[NL80211_BAND_ATTR_HT_CAPA],
  4771. tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR],
  4772. tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY],
  4773. tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
  4774. phy_info_vht_capa(mode, tb_band[NL80211_BAND_ATTR_VHT_CAPA],
  4775. tb_band[NL80211_BAND_ATTR_VHT_MCS_SET]);
  4776. ret = phy_info_freqs(phy_info, mode, tb_band[NL80211_BAND_ATTR_FREQS]);
  4777. if (ret != NL_OK)
  4778. return ret;
  4779. ret = phy_info_rates(mode, tb_band[NL80211_BAND_ATTR_RATES]);
  4780. if (ret != NL_OK)
  4781. return ret;
  4782. return NL_OK;
  4783. }
  4784. static int phy_info_handler(struct nl_msg *msg, void *arg)
  4785. {
  4786. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  4787. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  4788. struct phy_info_arg *phy_info = arg;
  4789. struct nlattr *nl_band;
  4790. int rem_band;
  4791. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  4792. genlmsg_attrlen(gnlh, 0), NULL);
  4793. if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
  4794. return NL_SKIP;
  4795. nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band)
  4796. {
  4797. int res = phy_info_band(phy_info, nl_band);
  4798. if (res != NL_OK)
  4799. return res;
  4800. }
  4801. return NL_SKIP;
  4802. }
  4803. static struct hostapd_hw_modes *
  4804. wpa_driver_nl80211_postprocess_modes(struct hostapd_hw_modes *modes,
  4805. u16 *num_modes)
  4806. {
  4807. u16 m;
  4808. struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
  4809. int i, mode11g_idx = -1;
  4810. /* heuristic to set up modes */
  4811. for (m = 0; m < *num_modes; m++) {
  4812. if (!modes[m].num_channels)
  4813. continue;
  4814. if (modes[m].channels[0].freq < 4000) {
  4815. modes[m].mode = HOSTAPD_MODE_IEEE80211B;
  4816. for (i = 0; i < modes[m].num_rates; i++) {
  4817. if (modes[m].rates[i] > 200) {
  4818. modes[m].mode = HOSTAPD_MODE_IEEE80211G;
  4819. break;
  4820. }
  4821. }
  4822. } else if (modes[m].channels[0].freq > 50000)
  4823. modes[m].mode = HOSTAPD_MODE_IEEE80211AD;
  4824. else
  4825. modes[m].mode = HOSTAPD_MODE_IEEE80211A;
  4826. }
  4827. /* If only 802.11g mode is included, use it to construct matching
  4828. * 802.11b mode data. */
  4829. for (m = 0; m < *num_modes; m++) {
  4830. if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
  4831. return modes; /* 802.11b already included */
  4832. if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
  4833. mode11g_idx = m;
  4834. }
  4835. if (mode11g_idx < 0)
  4836. return modes; /* 2.4 GHz band not supported at all */
  4837. nmodes = os_realloc_array(modes, *num_modes + 1, sizeof(*nmodes));
  4838. if (nmodes == NULL)
  4839. return modes; /* Could not add 802.11b mode */
  4840. mode = &nmodes[*num_modes];
  4841. os_memset(mode, 0, sizeof(*mode));
  4842. (*num_modes)++;
  4843. modes = nmodes;
  4844. mode->mode = HOSTAPD_MODE_IEEE80211B;
  4845. mode11g = &modes[mode11g_idx];
  4846. mode->num_channels = mode11g->num_channels;
  4847. mode->channels = os_malloc(mode11g->num_channels *
  4848. sizeof(struct hostapd_channel_data));
  4849. if (mode->channels == NULL) {
  4850. (*num_modes)--;
  4851. return modes; /* Could not add 802.11b mode */
  4852. }
  4853. os_memcpy(mode->channels, mode11g->channels,
  4854. mode11g->num_channels * sizeof(struct hostapd_channel_data));
  4855. mode->num_rates = 0;
  4856. mode->rates = os_malloc(4 * sizeof(int));
  4857. if (mode->rates == NULL) {
  4858. os_free(mode->channels);
  4859. (*num_modes)--;
  4860. return modes; /* Could not add 802.11b mode */
  4861. }
  4862. for (i = 0; i < mode11g->num_rates; i++) {
  4863. if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
  4864. mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
  4865. continue;
  4866. mode->rates[mode->num_rates] = mode11g->rates[i];
  4867. mode->num_rates++;
  4868. if (mode->num_rates == 4)
  4869. break;
  4870. }
  4871. if (mode->num_rates == 0) {
  4872. os_free(mode->channels);
  4873. os_free(mode->rates);
  4874. (*num_modes)--;
  4875. return modes; /* No 802.11b rates */
  4876. }
  4877. wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
  4878. "information");
  4879. return modes;
  4880. }
  4881. static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
  4882. int end)
  4883. {
  4884. int c;
  4885. for (c = 0; c < mode->num_channels; c++) {
  4886. struct hostapd_channel_data *chan = &mode->channels[c];
  4887. if (chan->freq - 10 >= start && chan->freq + 10 <= end)
  4888. chan->flag |= HOSTAPD_CHAN_HT40;
  4889. }
  4890. }
  4891. static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
  4892. int end)
  4893. {
  4894. int c;
  4895. for (c = 0; c < mode->num_channels; c++) {
  4896. struct hostapd_channel_data *chan = &mode->channels[c];
  4897. if (!(chan->flag & HOSTAPD_CHAN_HT40))
  4898. continue;
  4899. if (chan->freq - 30 >= start && chan->freq - 10 <= end)
  4900. chan->flag |= HOSTAPD_CHAN_HT40MINUS;
  4901. if (chan->freq + 10 >= start && chan->freq + 30 <= end)
  4902. chan->flag |= HOSTAPD_CHAN_HT40PLUS;
  4903. }
  4904. }
  4905. static void nl80211_reg_rule_ht40(struct nlattr *tb[],
  4906. struct phy_info_arg *results)
  4907. {
  4908. u32 start, end, max_bw;
  4909. u16 m;
  4910. if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
  4911. tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
  4912. tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
  4913. return;
  4914. start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
  4915. end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
  4916. max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
  4917. wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
  4918. start, end, max_bw);
  4919. if (max_bw < 40)
  4920. return;
  4921. for (m = 0; m < *results->num_modes; m++) {
  4922. if (!(results->modes[m].ht_capab &
  4923. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  4924. continue;
  4925. nl80211_set_ht40_mode(&results->modes[m], start, end);
  4926. }
  4927. }
  4928. static void nl80211_reg_rule_sec(struct nlattr *tb[],
  4929. struct phy_info_arg *results)
  4930. {
  4931. u32 start, end, max_bw;
  4932. u16 m;
  4933. if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
  4934. tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
  4935. tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
  4936. return;
  4937. start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
  4938. end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
  4939. max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
  4940. if (max_bw < 20)
  4941. return;
  4942. for (m = 0; m < *results->num_modes; m++) {
  4943. if (!(results->modes[m].ht_capab &
  4944. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  4945. continue;
  4946. nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
  4947. }
  4948. }
  4949. static int nl80211_get_reg(struct nl_msg *msg, void *arg)
  4950. {
  4951. struct phy_info_arg *results = arg;
  4952. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  4953. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  4954. struct nlattr *nl_rule;
  4955. struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
  4956. int rem_rule;
  4957. static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  4958. [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
  4959. [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
  4960. [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
  4961. [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
  4962. [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
  4963. [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
  4964. };
  4965. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  4966. genlmsg_attrlen(gnlh, 0), NULL);
  4967. if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
  4968. !tb_msg[NL80211_ATTR_REG_RULES]) {
  4969. wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
  4970. "available");
  4971. return NL_SKIP;
  4972. }
  4973. wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
  4974. (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
  4975. nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
  4976. {
  4977. nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
  4978. nla_data(nl_rule), nla_len(nl_rule), reg_policy);
  4979. nl80211_reg_rule_ht40(tb_rule, results);
  4980. }
  4981. nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
  4982. {
  4983. nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
  4984. nla_data(nl_rule), nla_len(nl_rule), reg_policy);
  4985. nl80211_reg_rule_sec(tb_rule, results);
  4986. }
  4987. return NL_SKIP;
  4988. }
  4989. static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
  4990. struct phy_info_arg *results)
  4991. {
  4992. struct nl_msg *msg;
  4993. msg = nlmsg_alloc();
  4994. if (!msg)
  4995. return -ENOMEM;
  4996. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
  4997. return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
  4998. }
  4999. static struct hostapd_hw_modes *
  5000. wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
  5001. {
  5002. u32 feat;
  5003. struct i802_bss *bss = priv;
  5004. struct wpa_driver_nl80211_data *drv = bss->drv;
  5005. struct nl_msg *msg;
  5006. struct phy_info_arg result = {
  5007. .num_modes = num_modes,
  5008. .modes = NULL,
  5009. .last_mode = -1,
  5010. };
  5011. *num_modes = 0;
  5012. *flags = 0;
  5013. msg = nlmsg_alloc();
  5014. if (!msg)
  5015. return NULL;
  5016. feat = get_nl80211_protocol_features(drv);
  5017. if (feat & NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP)
  5018. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_WIPHY);
  5019. else
  5020. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
  5021. NLA_PUT_FLAG(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP);
  5022. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5023. if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
  5024. nl80211_set_ht40_flags(drv, &result);
  5025. return wpa_driver_nl80211_postprocess_modes(result.modes,
  5026. num_modes);
  5027. }
  5028. msg = NULL;
  5029. nla_put_failure:
  5030. nlmsg_free(msg);
  5031. return NULL;
  5032. }
  5033. static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
  5034. const void *data, size_t len,
  5035. int encrypt, int noack)
  5036. {
  5037. __u8 rtap_hdr[] = {
  5038. 0x00, 0x00, /* radiotap version */
  5039. 0x0e, 0x00, /* radiotap length */
  5040. 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
  5041. IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
  5042. 0x00, /* padding */
  5043. 0x00, 0x00, /* RX and TX flags to indicate that */
  5044. 0x00, 0x00, /* this is the injected frame directly */
  5045. };
  5046. struct iovec iov[2] = {
  5047. {
  5048. .iov_base = &rtap_hdr,
  5049. .iov_len = sizeof(rtap_hdr),
  5050. },
  5051. {
  5052. .iov_base = (void *) data,
  5053. .iov_len = len,
  5054. }
  5055. };
  5056. struct msghdr msg = {
  5057. .msg_name = NULL,
  5058. .msg_namelen = 0,
  5059. .msg_iov = iov,
  5060. .msg_iovlen = 2,
  5061. .msg_control = NULL,
  5062. .msg_controllen = 0,
  5063. .msg_flags = 0,
  5064. };
  5065. int res;
  5066. u16 txflags = 0;
  5067. if (encrypt)
  5068. rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
  5069. if (drv->monitor_sock < 0) {
  5070. wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
  5071. "for %s", __func__);
  5072. return -1;
  5073. }
  5074. if (noack)
  5075. txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
  5076. WPA_PUT_LE16(&rtap_hdr[12], txflags);
  5077. res = sendmsg(drv->monitor_sock, &msg, 0);
  5078. if (res < 0) {
  5079. wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
  5080. return -1;
  5081. }
  5082. return 0;
  5083. }
  5084. static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
  5085. const void *data, size_t len,
  5086. int encrypt, int noack,
  5087. unsigned int freq, int no_cck,
  5088. int offchanok, unsigned int wait_time)
  5089. {
  5090. struct wpa_driver_nl80211_data *drv = bss->drv;
  5091. u64 cookie;
  5092. if (freq == 0) {
  5093. wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
  5094. bss->freq);
  5095. freq = bss->freq;
  5096. }
  5097. if (drv->use_monitor) {
  5098. wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_mntr",
  5099. freq, bss->freq);
  5100. return wpa_driver_nl80211_send_mntr(drv, data, len,
  5101. encrypt, noack);
  5102. }
  5103. wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
  5104. return nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
  5105. &cookie, no_cck, noack, offchanok);
  5106. }
  5107. static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
  5108. size_t data_len, int noack,
  5109. unsigned int freq, int no_cck,
  5110. int offchanok,
  5111. unsigned int wait_time)
  5112. {
  5113. struct wpa_driver_nl80211_data *drv = bss->drv;
  5114. struct ieee80211_mgmt *mgmt;
  5115. int encrypt = 1;
  5116. u16 fc;
  5117. mgmt = (struct ieee80211_mgmt *) data;
  5118. fc = le_to_host16(mgmt->frame_control);
  5119. wpa_printf(MSG_DEBUG, "nl80211: send_mlme - noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x nlmode=%d",
  5120. noack, freq, no_cck, offchanok, wait_time, fc, drv->nlmode);
  5121. if ((is_sta_interface(drv->nlmode) ||
  5122. drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
  5123. WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  5124. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
  5125. /*
  5126. * The use of last_mgmt_freq is a bit of a hack,
  5127. * but it works due to the single-threaded nature
  5128. * of wpa_supplicant.
  5129. */
  5130. if (freq == 0) {
  5131. wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
  5132. drv->last_mgmt_freq);
  5133. freq = drv->last_mgmt_freq;
  5134. }
  5135. return nl80211_send_frame_cmd(bss, freq, 0,
  5136. data, data_len, NULL, 1, noack,
  5137. 1);
  5138. }
  5139. if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
  5140. if (freq == 0) {
  5141. wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
  5142. bss->freq);
  5143. freq = bss->freq;
  5144. }
  5145. return nl80211_send_frame_cmd(bss, freq,
  5146. (int) freq == bss->freq ? 0 :
  5147. wait_time,
  5148. data, data_len,
  5149. &drv->send_action_cookie,
  5150. no_cck, noack, offchanok);
  5151. }
  5152. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  5153. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
  5154. /*
  5155. * Only one of the authentication frame types is encrypted.
  5156. * In order for static WEP encryption to work properly (i.e.,
  5157. * to not encrypt the frame), we need to tell mac80211 about
  5158. * the frames that must not be encrypted.
  5159. */
  5160. u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  5161. u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
  5162. if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
  5163. encrypt = 0;
  5164. }
  5165. wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
  5166. return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
  5167. noack, freq, no_cck, offchanok,
  5168. wait_time);
  5169. }
  5170. static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
  5171. int slot, int ht_opmode, int ap_isolate,
  5172. int *basic_rates)
  5173. {
  5174. struct wpa_driver_nl80211_data *drv = bss->drv;
  5175. struct nl_msg *msg;
  5176. msg = nlmsg_alloc();
  5177. if (!msg)
  5178. return -ENOMEM;
  5179. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
  5180. if (cts >= 0)
  5181. NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
  5182. if (preamble >= 0)
  5183. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
  5184. if (slot >= 0)
  5185. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
  5186. if (ht_opmode >= 0)
  5187. NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
  5188. if (ap_isolate >= 0)
  5189. NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
  5190. if (basic_rates) {
  5191. u8 rates[NL80211_MAX_SUPP_RATES];
  5192. u8 rates_len = 0;
  5193. int i;
  5194. for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
  5195. i++)
  5196. rates[rates_len++] = basic_rates[i] / 5;
  5197. NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
  5198. }
  5199. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  5200. return send_and_recv_msgs(drv, msg, NULL, NULL);
  5201. nla_put_failure:
  5202. nlmsg_free(msg);
  5203. return -ENOBUFS;
  5204. }
  5205. static int wpa_driver_nl80211_set_acl(void *priv,
  5206. struct hostapd_acl_params *params)
  5207. {
  5208. struct i802_bss *bss = priv;
  5209. struct wpa_driver_nl80211_data *drv = bss->drv;
  5210. struct nl_msg *msg;
  5211. struct nlattr *acl;
  5212. unsigned int i;
  5213. int ret = 0;
  5214. if (!(drv->capa.max_acl_mac_addrs))
  5215. return -ENOTSUP;
  5216. if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
  5217. return -ENOTSUP;
  5218. msg = nlmsg_alloc();
  5219. if (!msg)
  5220. return -ENOMEM;
  5221. wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
  5222. params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
  5223. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_MAC_ACL);
  5224. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5225. NLA_PUT_U32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
  5226. NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
  5227. NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED);
  5228. acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
  5229. if (acl == NULL)
  5230. goto nla_put_failure;
  5231. for (i = 0; i < params->num_mac_acl; i++)
  5232. NLA_PUT(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr);
  5233. nla_nest_end(msg, acl);
  5234. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5235. msg = NULL;
  5236. if (ret) {
  5237. wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
  5238. ret, strerror(-ret));
  5239. }
  5240. nla_put_failure:
  5241. nlmsg_free(msg);
  5242. return ret;
  5243. }
  5244. static int wpa_driver_nl80211_set_ap(void *priv,
  5245. struct wpa_driver_ap_params *params)
  5246. {
  5247. struct i802_bss *bss = priv;
  5248. struct wpa_driver_nl80211_data *drv = bss->drv;
  5249. struct nl_msg *msg;
  5250. u8 cmd = NL80211_CMD_NEW_BEACON;
  5251. int ret;
  5252. int beacon_set;
  5253. int ifindex = if_nametoindex(bss->ifname);
  5254. int num_suites;
  5255. u32 suites[10];
  5256. u32 ver;
  5257. beacon_set = bss->beacon_set;
  5258. msg = nlmsg_alloc();
  5259. if (!msg)
  5260. return -ENOMEM;
  5261. wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
  5262. beacon_set);
  5263. if (beacon_set)
  5264. cmd = NL80211_CMD_SET_BEACON;
  5265. nl80211_cmd(drv, msg, 0, cmd);
  5266. wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
  5267. params->head, params->head_len);
  5268. NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
  5269. wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
  5270. params->tail, params->tail_len);
  5271. NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
  5272. wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", ifindex);
  5273. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  5274. wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
  5275. NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
  5276. wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
  5277. NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
  5278. wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
  5279. params->ssid, params->ssid_len);
  5280. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  5281. params->ssid);
  5282. if (params->proberesp && params->proberesp_len) {
  5283. wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
  5284. params->proberesp, params->proberesp_len);
  5285. NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
  5286. params->proberesp);
  5287. }
  5288. switch (params->hide_ssid) {
  5289. case NO_SSID_HIDING:
  5290. wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
  5291. NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
  5292. NL80211_HIDDEN_SSID_NOT_IN_USE);
  5293. break;
  5294. case HIDDEN_SSID_ZERO_LEN:
  5295. wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
  5296. NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
  5297. NL80211_HIDDEN_SSID_ZERO_LEN);
  5298. break;
  5299. case HIDDEN_SSID_ZERO_CONTENTS:
  5300. wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
  5301. NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
  5302. NL80211_HIDDEN_SSID_ZERO_CONTENTS);
  5303. break;
  5304. }
  5305. wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
  5306. if (params->privacy)
  5307. NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
  5308. wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
  5309. if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
  5310. (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
  5311. /* Leave out the attribute */
  5312. } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
  5313. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
  5314. NL80211_AUTHTYPE_SHARED_KEY);
  5315. else
  5316. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
  5317. NL80211_AUTHTYPE_OPEN_SYSTEM);
  5318. wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
  5319. ver = 0;
  5320. if (params->wpa_version & WPA_PROTO_WPA)
  5321. ver |= NL80211_WPA_VERSION_1;
  5322. if (params->wpa_version & WPA_PROTO_RSN)
  5323. ver |= NL80211_WPA_VERSION_2;
  5324. if (ver)
  5325. NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
  5326. wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
  5327. params->key_mgmt_suites);
  5328. num_suites = 0;
  5329. if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
  5330. suites[num_suites++] = WLAN_AKM_SUITE_8021X;
  5331. if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
  5332. suites[num_suites++] = WLAN_AKM_SUITE_PSK;
  5333. if (num_suites) {
  5334. NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
  5335. num_suites * sizeof(u32), suites);
  5336. }
  5337. if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
  5338. params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
  5339. NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
  5340. wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
  5341. params->pairwise_ciphers);
  5342. num_suites = 0;
  5343. if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
  5344. suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
  5345. if (params->pairwise_ciphers & WPA_CIPHER_GCMP)
  5346. suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
  5347. if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
  5348. suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
  5349. if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
  5350. suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
  5351. if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
  5352. suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
  5353. if (num_suites) {
  5354. NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
  5355. num_suites * sizeof(u32), suites);
  5356. }
  5357. wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
  5358. params->group_cipher);
  5359. switch (params->group_cipher) {
  5360. case WPA_CIPHER_CCMP:
  5361. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
  5362. WLAN_CIPHER_SUITE_CCMP);
  5363. break;
  5364. case WPA_CIPHER_GCMP:
  5365. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
  5366. WLAN_CIPHER_SUITE_GCMP);
  5367. break;
  5368. case WPA_CIPHER_TKIP:
  5369. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
  5370. WLAN_CIPHER_SUITE_TKIP);
  5371. break;
  5372. case WPA_CIPHER_WEP104:
  5373. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
  5374. WLAN_CIPHER_SUITE_WEP104);
  5375. break;
  5376. case WPA_CIPHER_WEP40:
  5377. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
  5378. WLAN_CIPHER_SUITE_WEP40);
  5379. break;
  5380. }
  5381. if (params->beacon_ies) {
  5382. wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
  5383. params->beacon_ies);
  5384. NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
  5385. wpabuf_head(params->beacon_ies));
  5386. }
  5387. if (params->proberesp_ies) {
  5388. wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
  5389. params->proberesp_ies);
  5390. NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
  5391. wpabuf_len(params->proberesp_ies),
  5392. wpabuf_head(params->proberesp_ies));
  5393. }
  5394. if (params->assocresp_ies) {
  5395. wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
  5396. params->assocresp_ies);
  5397. NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
  5398. wpabuf_len(params->assocresp_ies),
  5399. wpabuf_head(params->assocresp_ies));
  5400. }
  5401. if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
  5402. wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
  5403. params->ap_max_inactivity);
  5404. NLA_PUT_U16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
  5405. params->ap_max_inactivity);
  5406. }
  5407. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5408. if (ret) {
  5409. wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
  5410. ret, strerror(-ret));
  5411. } else {
  5412. bss->beacon_set = 1;
  5413. nl80211_set_bss(bss, params->cts_protect, params->preamble,
  5414. params->short_slot_time, params->ht_opmode,
  5415. params->isolate, params->basic_rates);
  5416. }
  5417. return ret;
  5418. nla_put_failure:
  5419. nlmsg_free(msg);
  5420. return -ENOBUFS;
  5421. }
  5422. static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
  5423. struct hostapd_freq_params *freq)
  5424. {
  5425. struct wpa_driver_nl80211_data *drv = bss->drv;
  5426. struct nl_msg *msg;
  5427. int ret;
  5428. wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d,"
  5429. " bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
  5430. freq->freq, freq->ht_enabled, freq->vht_enabled,
  5431. freq->bandwidth, freq->center_freq1, freq->center_freq2);
  5432. msg = nlmsg_alloc();
  5433. if (!msg)
  5434. return -1;
  5435. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
  5436. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5437. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
  5438. if (freq->vht_enabled) {
  5439. switch (freq->bandwidth) {
  5440. case 20:
  5441. NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
  5442. NL80211_CHAN_WIDTH_20);
  5443. break;
  5444. case 40:
  5445. NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
  5446. NL80211_CHAN_WIDTH_40);
  5447. break;
  5448. case 80:
  5449. if (freq->center_freq2)
  5450. NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
  5451. NL80211_CHAN_WIDTH_80P80);
  5452. else
  5453. NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
  5454. NL80211_CHAN_WIDTH_80);
  5455. break;
  5456. case 160:
  5457. NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH,
  5458. NL80211_CHAN_WIDTH_160);
  5459. break;
  5460. default:
  5461. return -1;
  5462. }
  5463. NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ1, freq->center_freq1);
  5464. if (freq->center_freq2)
  5465. NLA_PUT_U32(msg, NL80211_ATTR_CENTER_FREQ2,
  5466. freq->center_freq2);
  5467. } else if (freq->ht_enabled) {
  5468. switch (freq->sec_channel_offset) {
  5469. case -1:
  5470. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  5471. NL80211_CHAN_HT40MINUS);
  5472. break;
  5473. case 1:
  5474. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  5475. NL80211_CHAN_HT40PLUS);
  5476. break;
  5477. default:
  5478. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  5479. NL80211_CHAN_HT20);
  5480. break;
  5481. }
  5482. }
  5483. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5484. msg = NULL;
  5485. if (ret == 0) {
  5486. bss->freq = freq->freq;
  5487. return 0;
  5488. }
  5489. wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
  5490. "%d (%s)", freq->freq, ret, strerror(-ret));
  5491. nla_put_failure:
  5492. nlmsg_free(msg);
  5493. return -1;
  5494. }
  5495. static u32 sta_flags_nl80211(int flags)
  5496. {
  5497. u32 f = 0;
  5498. if (flags & WPA_STA_AUTHORIZED)
  5499. f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
  5500. if (flags & WPA_STA_WMM)
  5501. f |= BIT(NL80211_STA_FLAG_WME);
  5502. if (flags & WPA_STA_SHORT_PREAMBLE)
  5503. f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
  5504. if (flags & WPA_STA_MFP)
  5505. f |= BIT(NL80211_STA_FLAG_MFP);
  5506. if (flags & WPA_STA_TDLS_PEER)
  5507. f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
  5508. return f;
  5509. }
  5510. static int wpa_driver_nl80211_sta_add(void *priv,
  5511. struct hostapd_sta_add_params *params)
  5512. {
  5513. struct i802_bss *bss = priv;
  5514. struct wpa_driver_nl80211_data *drv = bss->drv;
  5515. struct nl_msg *msg;
  5516. struct nl80211_sta_flag_update upd;
  5517. int ret = -ENOBUFS;
  5518. if ((params->flags & WPA_STA_TDLS_PEER) &&
  5519. !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
  5520. return -EOPNOTSUPP;
  5521. msg = nlmsg_alloc();
  5522. if (!msg)
  5523. return -ENOMEM;
  5524. wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
  5525. params->set ? "Set" : "Add", MAC2STR(params->addr));
  5526. nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
  5527. NL80211_CMD_NEW_STATION);
  5528. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  5529. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
  5530. NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
  5531. params->supp_rates);
  5532. wpa_hexdump(MSG_DEBUG, " * supported rates", params->supp_rates,
  5533. params->supp_rates_len);
  5534. if (!params->set) {
  5535. if (params->aid) {
  5536. wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
  5537. NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
  5538. } else {
  5539. /*
  5540. * cfg80211 validates that AID is non-zero, so we have
  5541. * to make this a non-zero value for the TDLS case where
  5542. * a dummy STA entry is used for now.
  5543. */
  5544. wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
  5545. NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, 1);
  5546. }
  5547. wpa_printf(MSG_DEBUG, " * listen_interval=%u",
  5548. params->listen_interval);
  5549. NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
  5550. params->listen_interval);
  5551. } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
  5552. wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
  5553. NLA_PUT_U16(msg, NL80211_ATTR_PEER_AID, params->aid);
  5554. }
  5555. if (params->ht_capabilities) {
  5556. wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
  5557. (u8 *) params->ht_capabilities,
  5558. sizeof(*params->ht_capabilities));
  5559. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
  5560. sizeof(*params->ht_capabilities),
  5561. params->ht_capabilities);
  5562. }
  5563. if (params->vht_capabilities) {
  5564. wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
  5565. (u8 *) params->vht_capabilities,
  5566. sizeof(*params->vht_capabilities));
  5567. NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY,
  5568. sizeof(*params->vht_capabilities),
  5569. params->vht_capabilities);
  5570. }
  5571. wpa_printf(MSG_DEBUG, " * capability=0x%x", params->capability);
  5572. NLA_PUT_U16(msg, NL80211_ATTR_STA_CAPABILITY, params->capability);
  5573. if (params->ext_capab) {
  5574. wpa_hexdump(MSG_DEBUG, " * ext_capab",
  5575. params->ext_capab, params->ext_capab_len);
  5576. NLA_PUT(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
  5577. params->ext_capab_len, params->ext_capab);
  5578. }
  5579. os_memset(&upd, 0, sizeof(upd));
  5580. upd.mask = sta_flags_nl80211(params->flags);
  5581. upd.set = upd.mask;
  5582. wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
  5583. upd.set, upd.mask);
  5584. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  5585. if (params->flags & WPA_STA_WMM) {
  5586. struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
  5587. if (!wme)
  5588. goto nla_put_failure;
  5589. wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
  5590. NLA_PUT_U8(msg, NL80211_STA_WME_UAPSD_QUEUES,
  5591. params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
  5592. NLA_PUT_U8(msg, NL80211_STA_WME_MAX_SP,
  5593. (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
  5594. WMM_QOSINFO_STA_SP_MASK);
  5595. nla_nest_end(msg, wme);
  5596. }
  5597. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5598. msg = NULL;
  5599. if (ret)
  5600. wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
  5601. "result: %d (%s)", params->set ? "SET" : "NEW", ret,
  5602. strerror(-ret));
  5603. if (ret == -EEXIST)
  5604. ret = 0;
  5605. nla_put_failure:
  5606. nlmsg_free(msg);
  5607. return ret;
  5608. }
  5609. static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr)
  5610. {
  5611. struct wpa_driver_nl80211_data *drv = bss->drv;
  5612. struct nl_msg *msg;
  5613. int ret;
  5614. msg = nlmsg_alloc();
  5615. if (!msg)
  5616. return -ENOMEM;
  5617. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
  5618. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  5619. if_nametoindex(bss->ifname));
  5620. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  5621. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5622. wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
  5623. " --> %d (%s)",
  5624. bss->ifname, MAC2STR(addr), ret, strerror(-ret));
  5625. if (ret == -ENOENT)
  5626. return 0;
  5627. return ret;
  5628. nla_put_failure:
  5629. nlmsg_free(msg);
  5630. return -ENOBUFS;
  5631. }
  5632. static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
  5633. int ifidx)
  5634. {
  5635. struct nl_msg *msg;
  5636. wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
  5637. /* stop listening for EAPOL on this interface */
  5638. del_ifidx(drv, ifidx);
  5639. msg = nlmsg_alloc();
  5640. if (!msg)
  5641. goto nla_put_failure;
  5642. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
  5643. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
  5644. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  5645. return;
  5646. msg = NULL;
  5647. nla_put_failure:
  5648. nlmsg_free(msg);
  5649. wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
  5650. }
  5651. static const char * nl80211_iftype_str(enum nl80211_iftype mode)
  5652. {
  5653. switch (mode) {
  5654. case NL80211_IFTYPE_ADHOC:
  5655. return "ADHOC";
  5656. case NL80211_IFTYPE_STATION:
  5657. return "STATION";
  5658. case NL80211_IFTYPE_AP:
  5659. return "AP";
  5660. case NL80211_IFTYPE_AP_VLAN:
  5661. return "AP_VLAN";
  5662. case NL80211_IFTYPE_WDS:
  5663. return "WDS";
  5664. case NL80211_IFTYPE_MONITOR:
  5665. return "MONITOR";
  5666. case NL80211_IFTYPE_MESH_POINT:
  5667. return "MESH_POINT";
  5668. case NL80211_IFTYPE_P2P_CLIENT:
  5669. return "P2P_CLIENT";
  5670. case NL80211_IFTYPE_P2P_GO:
  5671. return "P2P_GO";
  5672. case NL80211_IFTYPE_P2P_DEVICE:
  5673. return "P2P_DEVICE";
  5674. default:
  5675. return "unknown";
  5676. }
  5677. }
  5678. static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
  5679. const char *ifname,
  5680. enum nl80211_iftype iftype,
  5681. const u8 *addr, int wds,
  5682. int (*handler)(struct nl_msg *, void *),
  5683. void *arg)
  5684. {
  5685. struct nl_msg *msg;
  5686. int ifidx;
  5687. int ret = -ENOBUFS;
  5688. wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
  5689. iftype, nl80211_iftype_str(iftype));
  5690. msg = nlmsg_alloc();
  5691. if (!msg)
  5692. return -1;
  5693. nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
  5694. if (nl80211_set_iface_id(msg, &drv->first_bss) < 0)
  5695. goto nla_put_failure;
  5696. NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
  5697. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
  5698. if (iftype == NL80211_IFTYPE_MONITOR) {
  5699. struct nlattr *flags;
  5700. flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
  5701. if (!flags)
  5702. goto nla_put_failure;
  5703. NLA_PUT_FLAG(msg, NL80211_MNTR_FLAG_COOK_FRAMES);
  5704. nla_nest_end(msg, flags);
  5705. } else if (wds) {
  5706. NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
  5707. }
  5708. ret = send_and_recv_msgs(drv, msg, handler, arg);
  5709. msg = NULL;
  5710. if (ret) {
  5711. nla_put_failure:
  5712. nlmsg_free(msg);
  5713. wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
  5714. ifname, ret, strerror(-ret));
  5715. return ret;
  5716. }
  5717. if (iftype == NL80211_IFTYPE_P2P_DEVICE)
  5718. return 0;
  5719. ifidx = if_nametoindex(ifname);
  5720. wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
  5721. ifname, ifidx);
  5722. if (ifidx <= 0)
  5723. return -1;
  5724. /* start listening for EAPOL on this interface */
  5725. add_ifidx(drv, ifidx);
  5726. if (addr && iftype != NL80211_IFTYPE_MONITOR &&
  5727. linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
  5728. nl80211_remove_iface(drv, ifidx);
  5729. return -1;
  5730. }
  5731. return ifidx;
  5732. }
  5733. static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
  5734. const char *ifname, enum nl80211_iftype iftype,
  5735. const u8 *addr, int wds,
  5736. int (*handler)(struct nl_msg *, void *),
  5737. void *arg)
  5738. {
  5739. int ret;
  5740. ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
  5741. arg);
  5742. /* if error occurred and interface exists already */
  5743. if (ret == -ENFILE && if_nametoindex(ifname)) {
  5744. wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
  5745. /* Try to remove the interface that was already there. */
  5746. nl80211_remove_iface(drv, if_nametoindex(ifname));
  5747. /* Try to create the interface again */
  5748. ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
  5749. wds, handler, arg);
  5750. }
  5751. if (ret >= 0 && is_p2p_net_interface(iftype))
  5752. nl80211_disable_11b_rates(drv, ret, 1);
  5753. return ret;
  5754. }
  5755. static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
  5756. {
  5757. struct ieee80211_hdr *hdr;
  5758. u16 fc;
  5759. union wpa_event_data event;
  5760. hdr = (struct ieee80211_hdr *) buf;
  5761. fc = le_to_host16(hdr->frame_control);
  5762. os_memset(&event, 0, sizeof(event));
  5763. event.tx_status.type = WLAN_FC_GET_TYPE(fc);
  5764. event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
  5765. event.tx_status.dst = hdr->addr1;
  5766. event.tx_status.data = buf;
  5767. event.tx_status.data_len = len;
  5768. event.tx_status.ack = ok;
  5769. wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
  5770. }
  5771. static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
  5772. u8 *buf, size_t len)
  5773. {
  5774. struct ieee80211_hdr *hdr = (void *)buf;
  5775. u16 fc;
  5776. union wpa_event_data event;
  5777. if (len < sizeof(*hdr))
  5778. return;
  5779. fc = le_to_host16(hdr->frame_control);
  5780. os_memset(&event, 0, sizeof(event));
  5781. event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
  5782. event.rx_from_unknown.addr = hdr->addr2;
  5783. event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
  5784. (WLAN_FC_FROMDS | WLAN_FC_TODS);
  5785. wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
  5786. }
  5787. static void handle_frame(struct wpa_driver_nl80211_data *drv,
  5788. u8 *buf, size_t len, int datarate, int ssi_signal)
  5789. {
  5790. struct ieee80211_hdr *hdr;
  5791. u16 fc;
  5792. union wpa_event_data event;
  5793. hdr = (struct ieee80211_hdr *) buf;
  5794. fc = le_to_host16(hdr->frame_control);
  5795. switch (WLAN_FC_GET_TYPE(fc)) {
  5796. case WLAN_FC_TYPE_MGMT:
  5797. os_memset(&event, 0, sizeof(event));
  5798. event.rx_mgmt.frame = buf;
  5799. event.rx_mgmt.frame_len = len;
  5800. event.rx_mgmt.datarate = datarate;
  5801. event.rx_mgmt.ssi_signal = ssi_signal;
  5802. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  5803. break;
  5804. case WLAN_FC_TYPE_CTRL:
  5805. /* can only get here with PS-Poll frames */
  5806. wpa_printf(MSG_DEBUG, "CTRL");
  5807. from_unknown_sta(drv, buf, len);
  5808. break;
  5809. case WLAN_FC_TYPE_DATA:
  5810. from_unknown_sta(drv, buf, len);
  5811. break;
  5812. }
  5813. }
  5814. static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
  5815. {
  5816. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  5817. int len;
  5818. unsigned char buf[3000];
  5819. struct ieee80211_radiotap_iterator iter;
  5820. int ret;
  5821. int datarate = 0, ssi_signal = 0;
  5822. int injected = 0, failed = 0, rxflags = 0;
  5823. len = recv(sock, buf, sizeof(buf), 0);
  5824. if (len < 0) {
  5825. perror("recv");
  5826. return;
  5827. }
  5828. if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
  5829. printf("received invalid radiotap frame\n");
  5830. return;
  5831. }
  5832. while (1) {
  5833. ret = ieee80211_radiotap_iterator_next(&iter);
  5834. if (ret == -ENOENT)
  5835. break;
  5836. if (ret) {
  5837. printf("received invalid radiotap frame (%d)\n", ret);
  5838. return;
  5839. }
  5840. switch (iter.this_arg_index) {
  5841. case IEEE80211_RADIOTAP_FLAGS:
  5842. if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
  5843. len -= 4;
  5844. break;
  5845. case IEEE80211_RADIOTAP_RX_FLAGS:
  5846. rxflags = 1;
  5847. break;
  5848. case IEEE80211_RADIOTAP_TX_FLAGS:
  5849. injected = 1;
  5850. failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
  5851. IEEE80211_RADIOTAP_F_TX_FAIL;
  5852. break;
  5853. case IEEE80211_RADIOTAP_DATA_RETRIES:
  5854. break;
  5855. case IEEE80211_RADIOTAP_CHANNEL:
  5856. /* TODO: convert from freq/flags to channel number */
  5857. break;
  5858. case IEEE80211_RADIOTAP_RATE:
  5859. datarate = *iter.this_arg * 5;
  5860. break;
  5861. case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
  5862. ssi_signal = (s8) *iter.this_arg;
  5863. break;
  5864. }
  5865. }
  5866. if (rxflags && injected)
  5867. return;
  5868. if (!injected)
  5869. handle_frame(drv, buf + iter.max_length,
  5870. len - iter.max_length, datarate, ssi_signal);
  5871. else
  5872. handle_tx_callback(drv->ctx, buf + iter.max_length,
  5873. len - iter.max_length, !failed);
  5874. }
  5875. /*
  5876. * we post-process the filter code later and rewrite
  5877. * this to the offset to the last instruction
  5878. */
  5879. #define PASS 0xFF
  5880. #define FAIL 0xFE
  5881. static struct sock_filter msock_filter_insns[] = {
  5882. /*
  5883. * do a little-endian load of the radiotap length field
  5884. */
  5885. /* load lower byte into A */
  5886. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
  5887. /* put it into X (== index register) */
  5888. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  5889. /* load upper byte into A */
  5890. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
  5891. /* left-shift it by 8 */
  5892. BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
  5893. /* or with X */
  5894. BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
  5895. /* put result into X */
  5896. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  5897. /*
  5898. * Allow management frames through, this also gives us those
  5899. * management frames that we sent ourselves with status
  5900. */
  5901. /* load the lower byte of the IEEE 802.11 frame control field */
  5902. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  5903. /* mask off frame type and version */
  5904. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
  5905. /* accept frame if it's both 0, fall through otherwise */
  5906. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
  5907. /*
  5908. * TODO: add a bit to radiotap RX flags that indicates
  5909. * that the sending station is not associated, then
  5910. * add a filter here that filters on our DA and that flag
  5911. * to allow us to deauth frames to that bad station.
  5912. *
  5913. * For now allow all To DS data frames through.
  5914. */
  5915. /* load the IEEE 802.11 frame control field */
  5916. BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
  5917. /* mask off frame type, version and DS status */
  5918. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
  5919. /* accept frame if version 0, type 2 and To DS, fall through otherwise
  5920. */
  5921. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
  5922. #if 0
  5923. /*
  5924. * drop non-data frames
  5925. */
  5926. /* load the lower byte of the frame control field */
  5927. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  5928. /* mask off QoS bit */
  5929. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
  5930. /* drop non-data frames */
  5931. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
  5932. #endif
  5933. /* load the upper byte of the frame control field */
  5934. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
  5935. /* mask off toDS/fromDS */
  5936. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
  5937. /* accept WDS frames */
  5938. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
  5939. /*
  5940. * add header length to index
  5941. */
  5942. /* load the lower byte of the frame control field */
  5943. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  5944. /* mask off QoS bit */
  5945. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
  5946. /* right shift it by 6 to give 0 or 2 */
  5947. BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
  5948. /* add data frame header length */
  5949. BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
  5950. /* add index, was start of 802.11 header */
  5951. BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
  5952. /* move to index, now start of LL header */
  5953. BPF_STMT(BPF_MISC | BPF_TAX, 0),
  5954. /*
  5955. * Accept empty data frames, we use those for
  5956. * polling activity.
  5957. */
  5958. BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
  5959. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
  5960. /*
  5961. * Accept EAPOL frames
  5962. */
  5963. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
  5964. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
  5965. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
  5966. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
  5967. /* keep these last two statements or change the code below */
  5968. /* return 0 == "DROP" */
  5969. BPF_STMT(BPF_RET | BPF_K, 0),
  5970. /* return ~0 == "keep all" */
  5971. BPF_STMT(BPF_RET | BPF_K, ~0),
  5972. };
  5973. static struct sock_fprog msock_filter = {
  5974. .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
  5975. .filter = msock_filter_insns,
  5976. };
  5977. static int add_monitor_filter(int s)
  5978. {
  5979. int idx;
  5980. /* rewrite all PASS/FAIL jump offsets */
  5981. for (idx = 0; idx < msock_filter.len; idx++) {
  5982. struct sock_filter *insn = &msock_filter_insns[idx];
  5983. if (BPF_CLASS(insn->code) == BPF_JMP) {
  5984. if (insn->code == (BPF_JMP|BPF_JA)) {
  5985. if (insn->k == PASS)
  5986. insn->k = msock_filter.len - idx - 2;
  5987. else if (insn->k == FAIL)
  5988. insn->k = msock_filter.len - idx - 3;
  5989. }
  5990. if (insn->jt == PASS)
  5991. insn->jt = msock_filter.len - idx - 2;
  5992. else if (insn->jt == FAIL)
  5993. insn->jt = msock_filter.len - idx - 3;
  5994. if (insn->jf == PASS)
  5995. insn->jf = msock_filter.len - idx - 2;
  5996. else if (insn->jf == FAIL)
  5997. insn->jf = msock_filter.len - idx - 3;
  5998. }
  5999. }
  6000. if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
  6001. &msock_filter, sizeof(msock_filter))) {
  6002. perror("SO_ATTACH_FILTER");
  6003. return -1;
  6004. }
  6005. return 0;
  6006. }
  6007. static void nl80211_remove_monitor_interface(
  6008. struct wpa_driver_nl80211_data *drv)
  6009. {
  6010. drv->monitor_refcount--;
  6011. if (drv->monitor_refcount > 0)
  6012. return;
  6013. if (drv->monitor_ifidx >= 0) {
  6014. nl80211_remove_iface(drv, drv->monitor_ifidx);
  6015. drv->monitor_ifidx = -1;
  6016. }
  6017. if (drv->monitor_sock >= 0) {
  6018. eloop_unregister_read_sock(drv->monitor_sock);
  6019. close(drv->monitor_sock);
  6020. drv->monitor_sock = -1;
  6021. }
  6022. }
  6023. static int
  6024. nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
  6025. {
  6026. char buf[IFNAMSIZ];
  6027. struct sockaddr_ll ll;
  6028. int optval;
  6029. socklen_t optlen;
  6030. if (drv->monitor_ifidx >= 0) {
  6031. drv->monitor_refcount++;
  6032. return 0;
  6033. }
  6034. if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
  6035. /*
  6036. * P2P interface name is of the format p2p-%s-%d. For monitor
  6037. * interface name corresponding to P2P GO, replace "p2p-" with
  6038. * "mon-" to retain the same interface name length and to
  6039. * indicate that it is a monitor interface.
  6040. */
  6041. snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
  6042. } else {
  6043. /* Non-P2P interface with AP functionality. */
  6044. snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
  6045. }
  6046. buf[IFNAMSIZ - 1] = '\0';
  6047. drv->monitor_ifidx =
  6048. nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
  6049. 0, NULL, NULL);
  6050. if (drv->monitor_ifidx == -EOPNOTSUPP) {
  6051. /*
  6052. * This is backward compatibility for a few versions of
  6053. * the kernel only that didn't advertise the right
  6054. * attributes for the only driver that then supported
  6055. * AP mode w/o monitor -- ath6kl.
  6056. */
  6057. wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
  6058. "monitor interface type - try to run without it");
  6059. drv->device_ap_sme = 1;
  6060. }
  6061. if (drv->monitor_ifidx < 0)
  6062. return -1;
  6063. if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
  6064. goto error;
  6065. memset(&ll, 0, sizeof(ll));
  6066. ll.sll_family = AF_PACKET;
  6067. ll.sll_ifindex = drv->monitor_ifidx;
  6068. drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  6069. if (drv->monitor_sock < 0) {
  6070. perror("socket[PF_PACKET,SOCK_RAW]");
  6071. goto error;
  6072. }
  6073. if (add_monitor_filter(drv->monitor_sock)) {
  6074. wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
  6075. "interface; do filtering in user space");
  6076. /* This works, but will cost in performance. */
  6077. }
  6078. if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
  6079. perror("monitor socket bind");
  6080. goto error;
  6081. }
  6082. optlen = sizeof(optval);
  6083. optval = 20;
  6084. if (setsockopt
  6085. (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
  6086. perror("Failed to set socket priority");
  6087. goto error;
  6088. }
  6089. if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
  6090. drv, NULL)) {
  6091. printf("Could not register monitor read socket\n");
  6092. goto error;
  6093. }
  6094. return 0;
  6095. error:
  6096. nl80211_remove_monitor_interface(drv);
  6097. return -1;
  6098. }
  6099. static int nl80211_setup_ap(struct i802_bss *bss)
  6100. {
  6101. struct wpa_driver_nl80211_data *drv = bss->drv;
  6102. wpa_printf(MSG_DEBUG, "nl80211: Setup AP - device_ap_sme=%d "
  6103. "use_monitor=%d", drv->device_ap_sme, drv->use_monitor);
  6104. /*
  6105. * Disable Probe Request reporting unless we need it in this way for
  6106. * devices that include the AP SME, in the other case (unless using
  6107. * monitor iface) we'll get it through the nl_mgmt socket instead.
  6108. */
  6109. if (!drv->device_ap_sme)
  6110. wpa_driver_nl80211_probe_req_report(bss, 0);
  6111. if (!drv->device_ap_sme && !drv->use_monitor)
  6112. if (nl80211_mgmt_subscribe_ap(bss))
  6113. return -1;
  6114. if (drv->device_ap_sme && !drv->use_monitor)
  6115. if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
  6116. return -1;
  6117. if (!drv->device_ap_sme && drv->use_monitor &&
  6118. nl80211_create_monitor_interface(drv) &&
  6119. !drv->device_ap_sme)
  6120. return -1;
  6121. if (drv->device_ap_sme &&
  6122. wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
  6123. wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
  6124. "Probe Request frame reporting in AP mode");
  6125. /* Try to survive without this */
  6126. }
  6127. return 0;
  6128. }
  6129. static void nl80211_teardown_ap(struct i802_bss *bss)
  6130. {
  6131. struct wpa_driver_nl80211_data *drv = bss->drv;
  6132. if (drv->device_ap_sme) {
  6133. wpa_driver_nl80211_probe_req_report(bss, 0);
  6134. if (!drv->use_monitor)
  6135. nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
  6136. } else if (drv->use_monitor)
  6137. nl80211_remove_monitor_interface(drv);
  6138. else
  6139. nl80211_mgmt_unsubscribe(bss, "AP teardown");
  6140. bss->beacon_set = 0;
  6141. }
  6142. static int nl80211_send_eapol_data(struct i802_bss *bss,
  6143. const u8 *addr, const u8 *data,
  6144. size_t data_len)
  6145. {
  6146. struct sockaddr_ll ll;
  6147. int ret;
  6148. if (bss->drv->eapol_tx_sock < 0) {
  6149. wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
  6150. return -1;
  6151. }
  6152. os_memset(&ll, 0, sizeof(ll));
  6153. ll.sll_family = AF_PACKET;
  6154. ll.sll_ifindex = bss->ifindex;
  6155. ll.sll_protocol = htons(ETH_P_PAE);
  6156. ll.sll_halen = ETH_ALEN;
  6157. os_memcpy(ll.sll_addr, addr, ETH_ALEN);
  6158. ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
  6159. (struct sockaddr *) &ll, sizeof(ll));
  6160. if (ret < 0)
  6161. wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
  6162. strerror(errno));
  6163. return ret;
  6164. }
  6165. static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  6166. static int wpa_driver_nl80211_hapd_send_eapol(
  6167. void *priv, const u8 *addr, const u8 *data,
  6168. size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
  6169. {
  6170. struct i802_bss *bss = priv;
  6171. struct wpa_driver_nl80211_data *drv = bss->drv;
  6172. struct ieee80211_hdr *hdr;
  6173. size_t len;
  6174. u8 *pos;
  6175. int res;
  6176. int qos = flags & WPA_STA_WMM;
  6177. if (drv->device_ap_sme || !drv->use_monitor)
  6178. return nl80211_send_eapol_data(bss, addr, data, data_len);
  6179. len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
  6180. data_len;
  6181. hdr = os_zalloc(len);
  6182. if (hdr == NULL) {
  6183. printf("malloc() failed for i802_send_data(len=%lu)\n",
  6184. (unsigned long) len);
  6185. return -1;
  6186. }
  6187. hdr->frame_control =
  6188. IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
  6189. hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
  6190. if (encrypt)
  6191. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  6192. if (qos) {
  6193. hdr->frame_control |=
  6194. host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
  6195. }
  6196. memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  6197. memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  6198. memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  6199. pos = (u8 *) (hdr + 1);
  6200. if (qos) {
  6201. /* Set highest priority in QoS header */
  6202. pos[0] = 7;
  6203. pos[1] = 0;
  6204. pos += 2;
  6205. }
  6206. memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
  6207. pos += sizeof(rfc1042_header);
  6208. WPA_PUT_BE16(pos, ETH_P_PAE);
  6209. pos += 2;
  6210. memcpy(pos, data, data_len);
  6211. res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
  6212. 0, 0, 0, 0);
  6213. if (res < 0) {
  6214. wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
  6215. "failed: %d (%s)",
  6216. (unsigned long) len, errno, strerror(errno));
  6217. }
  6218. os_free(hdr);
  6219. return res;
  6220. }
  6221. static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
  6222. int total_flags,
  6223. int flags_or, int flags_and)
  6224. {
  6225. struct i802_bss *bss = priv;
  6226. struct wpa_driver_nl80211_data *drv = bss->drv;
  6227. struct nl_msg *msg;
  6228. struct nlattr *flags;
  6229. struct nl80211_sta_flag_update upd;
  6230. msg = nlmsg_alloc();
  6231. if (!msg)
  6232. return -ENOMEM;
  6233. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
  6234. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  6235. if_nametoindex(bss->ifname));
  6236. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  6237. /*
  6238. * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
  6239. * can be removed eventually.
  6240. */
  6241. flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
  6242. if (!flags)
  6243. goto nla_put_failure;
  6244. if (total_flags & WPA_STA_AUTHORIZED)
  6245. NLA_PUT_FLAG(msg, NL80211_STA_FLAG_AUTHORIZED);
  6246. if (total_flags & WPA_STA_WMM)
  6247. NLA_PUT_FLAG(msg, NL80211_STA_FLAG_WME);
  6248. if (total_flags & WPA_STA_SHORT_PREAMBLE)
  6249. NLA_PUT_FLAG(msg, NL80211_STA_FLAG_SHORT_PREAMBLE);
  6250. if (total_flags & WPA_STA_MFP)
  6251. NLA_PUT_FLAG(msg, NL80211_STA_FLAG_MFP);
  6252. if (total_flags & WPA_STA_TDLS_PEER)
  6253. NLA_PUT_FLAG(msg, NL80211_STA_FLAG_TDLS_PEER);
  6254. nla_nest_end(msg, flags);
  6255. os_memset(&upd, 0, sizeof(upd));
  6256. upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
  6257. upd.set = sta_flags_nl80211(flags_or);
  6258. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  6259. return send_and_recv_msgs(drv, msg, NULL, NULL);
  6260. nla_put_failure:
  6261. nlmsg_free(msg);
  6262. return -ENOBUFS;
  6263. }
  6264. static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
  6265. struct wpa_driver_associate_params *params)
  6266. {
  6267. enum nl80211_iftype nlmode, old_mode;
  6268. struct hostapd_freq_params freq = {
  6269. .freq = params->freq,
  6270. };
  6271. if (params->p2p) {
  6272. wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
  6273. "group (GO)");
  6274. nlmode = NL80211_IFTYPE_P2P_GO;
  6275. } else
  6276. nlmode = NL80211_IFTYPE_AP;
  6277. old_mode = drv->nlmode;
  6278. if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode)) {
  6279. nl80211_remove_monitor_interface(drv);
  6280. return -1;
  6281. }
  6282. if (wpa_driver_nl80211_set_freq(&drv->first_bss, &freq)) {
  6283. if (old_mode != nlmode)
  6284. wpa_driver_nl80211_set_mode(&drv->first_bss, old_mode);
  6285. nl80211_remove_monitor_interface(drv);
  6286. return -1;
  6287. }
  6288. return 0;
  6289. }
  6290. static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
  6291. {
  6292. struct nl_msg *msg;
  6293. int ret = -1;
  6294. msg = nlmsg_alloc();
  6295. if (!msg)
  6296. return -1;
  6297. nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
  6298. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  6299. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6300. msg = NULL;
  6301. if (ret) {
  6302. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
  6303. "(%s)", ret, strerror(-ret));
  6304. goto nla_put_failure;
  6305. }
  6306. ret = 0;
  6307. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
  6308. nla_put_failure:
  6309. if (wpa_driver_nl80211_set_mode(&drv->first_bss,
  6310. NL80211_IFTYPE_STATION)) {
  6311. wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
  6312. "station mode");
  6313. }
  6314. nlmsg_free(msg);
  6315. return ret;
  6316. }
  6317. static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
  6318. struct wpa_driver_associate_params *params)
  6319. {
  6320. struct nl_msg *msg;
  6321. int ret = -1;
  6322. int count = 0;
  6323. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
  6324. if (wpa_driver_nl80211_set_mode(&drv->first_bss,
  6325. NL80211_IFTYPE_ADHOC)) {
  6326. wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
  6327. "IBSS mode");
  6328. return -1;
  6329. }
  6330. retry:
  6331. msg = nlmsg_alloc();
  6332. if (!msg)
  6333. return -1;
  6334. nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
  6335. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  6336. if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
  6337. goto nla_put_failure;
  6338. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  6339. params->ssid, params->ssid_len);
  6340. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  6341. params->ssid);
  6342. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  6343. drv->ssid_len = params->ssid_len;
  6344. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  6345. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  6346. ret = nl80211_set_conn_keys(params, msg);
  6347. if (ret)
  6348. goto nla_put_failure;
  6349. if (params->bssid && params->fixed_bssid) {
  6350. wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
  6351. MAC2STR(params->bssid));
  6352. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  6353. }
  6354. if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
  6355. params->key_mgmt_suite == KEY_MGMT_PSK ||
  6356. params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
  6357. params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
  6358. wpa_printf(MSG_DEBUG, " * control port");
  6359. NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
  6360. }
  6361. if (params->wpa_ie) {
  6362. wpa_hexdump(MSG_DEBUG,
  6363. " * Extra IEs for Beacon/Probe Response frames",
  6364. params->wpa_ie, params->wpa_ie_len);
  6365. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  6366. params->wpa_ie);
  6367. }
  6368. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6369. msg = NULL;
  6370. if (ret) {
  6371. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
  6372. ret, strerror(-ret));
  6373. count++;
  6374. if (ret == -EALREADY && count == 1) {
  6375. wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
  6376. "forced leave");
  6377. nl80211_leave_ibss(drv);
  6378. nlmsg_free(msg);
  6379. goto retry;
  6380. }
  6381. goto nla_put_failure;
  6382. }
  6383. ret = 0;
  6384. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
  6385. nla_put_failure:
  6386. nlmsg_free(msg);
  6387. return ret;
  6388. }
  6389. static int wpa_driver_nl80211_try_connect(
  6390. struct wpa_driver_nl80211_data *drv,
  6391. struct wpa_driver_associate_params *params)
  6392. {
  6393. struct nl_msg *msg;
  6394. enum nl80211_auth_type type;
  6395. int ret = 0;
  6396. int algs;
  6397. msg = nlmsg_alloc();
  6398. if (!msg)
  6399. return -1;
  6400. wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
  6401. nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
  6402. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  6403. if (params->bssid) {
  6404. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  6405. MAC2STR(params->bssid));
  6406. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  6407. }
  6408. if (params->freq) {
  6409. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  6410. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  6411. drv->assoc_freq = params->freq;
  6412. } else
  6413. drv->assoc_freq = 0;
  6414. if (params->bg_scan_period >= 0) {
  6415. wpa_printf(MSG_DEBUG, " * bg scan period=%d",
  6416. params->bg_scan_period);
  6417. NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
  6418. params->bg_scan_period);
  6419. }
  6420. if (params->ssid) {
  6421. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  6422. params->ssid, params->ssid_len);
  6423. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  6424. params->ssid);
  6425. if (params->ssid_len > sizeof(drv->ssid))
  6426. goto nla_put_failure;
  6427. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  6428. drv->ssid_len = params->ssid_len;
  6429. }
  6430. wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
  6431. if (params->wpa_ie)
  6432. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  6433. params->wpa_ie);
  6434. algs = 0;
  6435. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  6436. algs++;
  6437. if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  6438. algs++;
  6439. if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  6440. algs++;
  6441. if (algs > 1) {
  6442. wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
  6443. "selection");
  6444. goto skip_auth_type;
  6445. }
  6446. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  6447. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  6448. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  6449. type = NL80211_AUTHTYPE_SHARED_KEY;
  6450. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  6451. type = NL80211_AUTHTYPE_NETWORK_EAP;
  6452. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  6453. type = NL80211_AUTHTYPE_FT;
  6454. else
  6455. goto nla_put_failure;
  6456. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  6457. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
  6458. skip_auth_type:
  6459. if (params->wpa_proto) {
  6460. enum nl80211_wpa_versions ver = 0;
  6461. if (params->wpa_proto & WPA_PROTO_WPA)
  6462. ver |= NL80211_WPA_VERSION_1;
  6463. if (params->wpa_proto & WPA_PROTO_RSN)
  6464. ver |= NL80211_WPA_VERSION_2;
  6465. wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
  6466. NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
  6467. }
  6468. if (params->pairwise_suite != CIPHER_NONE) {
  6469. int cipher;
  6470. switch (params->pairwise_suite) {
  6471. case CIPHER_SMS4:
  6472. cipher = WLAN_CIPHER_SUITE_SMS4;
  6473. break;
  6474. case CIPHER_WEP40:
  6475. cipher = WLAN_CIPHER_SUITE_WEP40;
  6476. break;
  6477. case CIPHER_WEP104:
  6478. cipher = WLAN_CIPHER_SUITE_WEP104;
  6479. break;
  6480. case CIPHER_CCMP:
  6481. cipher = WLAN_CIPHER_SUITE_CCMP;
  6482. break;
  6483. case CIPHER_GCMP:
  6484. cipher = WLAN_CIPHER_SUITE_GCMP;
  6485. break;
  6486. case CIPHER_TKIP:
  6487. default:
  6488. cipher = WLAN_CIPHER_SUITE_TKIP;
  6489. break;
  6490. }
  6491. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
  6492. }
  6493. if (params->group_suite != CIPHER_NONE) {
  6494. int cipher;
  6495. switch (params->group_suite) {
  6496. case CIPHER_SMS4:
  6497. cipher = WLAN_CIPHER_SUITE_SMS4;
  6498. break;
  6499. case CIPHER_WEP40:
  6500. cipher = WLAN_CIPHER_SUITE_WEP40;
  6501. break;
  6502. case CIPHER_WEP104:
  6503. cipher = WLAN_CIPHER_SUITE_WEP104;
  6504. break;
  6505. case CIPHER_CCMP:
  6506. cipher = WLAN_CIPHER_SUITE_CCMP;
  6507. break;
  6508. case CIPHER_GCMP:
  6509. cipher = WLAN_CIPHER_SUITE_GCMP;
  6510. break;
  6511. case CIPHER_TKIP:
  6512. default:
  6513. cipher = WLAN_CIPHER_SUITE_TKIP;
  6514. break;
  6515. }
  6516. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
  6517. }
  6518. if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
  6519. params->key_mgmt_suite == KEY_MGMT_PSK ||
  6520. params->key_mgmt_suite == KEY_MGMT_FT_802_1X ||
  6521. params->key_mgmt_suite == KEY_MGMT_FT_PSK ||
  6522. params->key_mgmt_suite == KEY_MGMT_CCKM) {
  6523. int mgmt = WLAN_AKM_SUITE_PSK;
  6524. switch (params->key_mgmt_suite) {
  6525. case KEY_MGMT_CCKM:
  6526. mgmt = WLAN_AKM_SUITE_CCKM;
  6527. break;
  6528. case KEY_MGMT_802_1X:
  6529. mgmt = WLAN_AKM_SUITE_8021X;
  6530. break;
  6531. case KEY_MGMT_FT_802_1X:
  6532. mgmt = WLAN_AKM_SUITE_FT_8021X;
  6533. break;
  6534. case KEY_MGMT_FT_PSK:
  6535. mgmt = WLAN_AKM_SUITE_FT_PSK;
  6536. break;
  6537. case KEY_MGMT_PSK:
  6538. default:
  6539. mgmt = WLAN_AKM_SUITE_PSK;
  6540. break;
  6541. }
  6542. NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
  6543. }
  6544. #ifdef CONFIG_IEEE80211W
  6545. if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
  6546. NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
  6547. #endif /* CONFIG_IEEE80211W */
  6548. if (params->disable_ht)
  6549. NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
  6550. if (params->htcaps && params->htcaps_mask) {
  6551. int sz = sizeof(struct ieee80211_ht_capabilities);
  6552. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
  6553. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
  6554. params->htcaps_mask);
  6555. }
  6556. #ifdef CONFIG_VHT_OVERRIDES
  6557. if (params->disable_vht) {
  6558. wpa_printf(MSG_DEBUG, " * VHT disabled");
  6559. NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
  6560. }
  6561. if (params->vhtcaps && params->vhtcaps_mask) {
  6562. int sz = sizeof(struct ieee80211_vht_capabilities);
  6563. NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
  6564. NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
  6565. params->vhtcaps_mask);
  6566. }
  6567. #endif /* CONFIG_VHT_OVERRIDES */
  6568. ret = nl80211_set_conn_keys(params, msg);
  6569. if (ret)
  6570. goto nla_put_failure;
  6571. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6572. msg = NULL;
  6573. if (ret) {
  6574. wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
  6575. "(%s)", ret, strerror(-ret));
  6576. goto nla_put_failure;
  6577. }
  6578. ret = 0;
  6579. wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
  6580. nla_put_failure:
  6581. nlmsg_free(msg);
  6582. return ret;
  6583. }
  6584. static int wpa_driver_nl80211_connect(
  6585. struct wpa_driver_nl80211_data *drv,
  6586. struct wpa_driver_associate_params *params)
  6587. {
  6588. int ret = wpa_driver_nl80211_try_connect(drv, params);
  6589. if (ret == -EALREADY) {
  6590. /*
  6591. * cfg80211 does not currently accept new connections if
  6592. * we are already connected. As a workaround, force
  6593. * disconnection and try again.
  6594. */
  6595. wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
  6596. "disconnecting before reassociation "
  6597. "attempt");
  6598. if (wpa_driver_nl80211_disconnect(
  6599. drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
  6600. return -1;
  6601. ret = wpa_driver_nl80211_try_connect(drv, params);
  6602. }
  6603. return ret;
  6604. }
  6605. static int wpa_driver_nl80211_associate(
  6606. void *priv, struct wpa_driver_associate_params *params)
  6607. {
  6608. struct i802_bss *bss = priv;
  6609. struct wpa_driver_nl80211_data *drv = bss->drv;
  6610. int ret = -1;
  6611. struct nl_msg *msg;
  6612. if (params->mode == IEEE80211_MODE_AP)
  6613. return wpa_driver_nl80211_ap(drv, params);
  6614. if (params->mode == IEEE80211_MODE_IBSS)
  6615. return wpa_driver_nl80211_ibss(drv, params);
  6616. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
  6617. enum nl80211_iftype nlmode = params->p2p ?
  6618. NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
  6619. if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
  6620. return -1;
  6621. return wpa_driver_nl80211_connect(drv, params);
  6622. }
  6623. nl80211_mark_disconnected(drv);
  6624. msg = nlmsg_alloc();
  6625. if (!msg)
  6626. return -1;
  6627. wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
  6628. drv->ifindex);
  6629. nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
  6630. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  6631. if (params->bssid) {
  6632. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  6633. MAC2STR(params->bssid));
  6634. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  6635. }
  6636. if (params->freq) {
  6637. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  6638. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  6639. drv->assoc_freq = params->freq;
  6640. } else
  6641. drv->assoc_freq = 0;
  6642. if (params->bg_scan_period >= 0) {
  6643. wpa_printf(MSG_DEBUG, " * bg scan period=%d",
  6644. params->bg_scan_period);
  6645. NLA_PUT_U16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
  6646. params->bg_scan_period);
  6647. }
  6648. if (params->ssid) {
  6649. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  6650. params->ssid, params->ssid_len);
  6651. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  6652. params->ssid);
  6653. if (params->ssid_len > sizeof(drv->ssid))
  6654. goto nla_put_failure;
  6655. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  6656. drv->ssid_len = params->ssid_len;
  6657. }
  6658. wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
  6659. if (params->wpa_ie)
  6660. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  6661. params->wpa_ie);
  6662. if (params->pairwise_suite != CIPHER_NONE) {
  6663. int cipher;
  6664. switch (params->pairwise_suite) {
  6665. case CIPHER_WEP40:
  6666. cipher = WLAN_CIPHER_SUITE_WEP40;
  6667. break;
  6668. case CIPHER_WEP104:
  6669. cipher = WLAN_CIPHER_SUITE_WEP104;
  6670. break;
  6671. case CIPHER_CCMP:
  6672. cipher = WLAN_CIPHER_SUITE_CCMP;
  6673. break;
  6674. case CIPHER_GCMP:
  6675. cipher = WLAN_CIPHER_SUITE_GCMP;
  6676. break;
  6677. case CIPHER_TKIP:
  6678. default:
  6679. cipher = WLAN_CIPHER_SUITE_TKIP;
  6680. break;
  6681. }
  6682. wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
  6683. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
  6684. }
  6685. if (params->group_suite != CIPHER_NONE) {
  6686. int cipher;
  6687. switch (params->group_suite) {
  6688. case CIPHER_WEP40:
  6689. cipher = WLAN_CIPHER_SUITE_WEP40;
  6690. break;
  6691. case CIPHER_WEP104:
  6692. cipher = WLAN_CIPHER_SUITE_WEP104;
  6693. break;
  6694. case CIPHER_CCMP:
  6695. cipher = WLAN_CIPHER_SUITE_CCMP;
  6696. break;
  6697. case CIPHER_GCMP:
  6698. cipher = WLAN_CIPHER_SUITE_GCMP;
  6699. break;
  6700. case CIPHER_TKIP:
  6701. default:
  6702. cipher = WLAN_CIPHER_SUITE_TKIP;
  6703. break;
  6704. }
  6705. wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
  6706. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
  6707. }
  6708. #ifdef CONFIG_IEEE80211W
  6709. if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
  6710. NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
  6711. #endif /* CONFIG_IEEE80211W */
  6712. NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
  6713. if (params->prev_bssid) {
  6714. wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
  6715. MAC2STR(params->prev_bssid));
  6716. NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
  6717. params->prev_bssid);
  6718. }
  6719. if (params->disable_ht)
  6720. NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
  6721. if (params->htcaps && params->htcaps_mask) {
  6722. int sz = sizeof(struct ieee80211_ht_capabilities);
  6723. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
  6724. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
  6725. params->htcaps_mask);
  6726. }
  6727. #ifdef CONFIG_VHT_OVERRIDES
  6728. if (params->disable_vht) {
  6729. wpa_printf(MSG_DEBUG, " * VHT disabled");
  6730. NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_VHT);
  6731. }
  6732. if (params->vhtcaps && params->vhtcaps_mask) {
  6733. int sz = sizeof(struct ieee80211_vht_capabilities);
  6734. NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY, sz, params->vhtcaps);
  6735. NLA_PUT(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
  6736. params->vhtcaps_mask);
  6737. }
  6738. #endif /* CONFIG_VHT_OVERRIDES */
  6739. if (params->p2p)
  6740. wpa_printf(MSG_DEBUG, " * P2P group");
  6741. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6742. msg = NULL;
  6743. if (ret) {
  6744. wpa_dbg(drv->ctx, MSG_DEBUG,
  6745. "nl80211: MLME command failed (assoc): ret=%d (%s)",
  6746. ret, strerror(-ret));
  6747. nl80211_dump_scan(drv);
  6748. goto nla_put_failure;
  6749. }
  6750. ret = 0;
  6751. wpa_printf(MSG_DEBUG, "nl80211: Association request send "
  6752. "successfully");
  6753. nla_put_failure:
  6754. nlmsg_free(msg);
  6755. return ret;
  6756. }
  6757. static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
  6758. int ifindex, enum nl80211_iftype mode)
  6759. {
  6760. struct nl_msg *msg;
  6761. int ret = -ENOBUFS;
  6762. wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
  6763. ifindex, mode, nl80211_iftype_str(mode));
  6764. msg = nlmsg_alloc();
  6765. if (!msg)
  6766. return -ENOMEM;
  6767. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
  6768. if (nl80211_set_iface_id(msg, &drv->first_bss) < 0)
  6769. goto nla_put_failure;
  6770. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
  6771. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6772. msg = NULL;
  6773. if (!ret)
  6774. return 0;
  6775. nla_put_failure:
  6776. nlmsg_free(msg);
  6777. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
  6778. " %d (%s)", ifindex, mode, ret, strerror(-ret));
  6779. return ret;
  6780. }
  6781. static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
  6782. enum nl80211_iftype nlmode)
  6783. {
  6784. struct wpa_driver_nl80211_data *drv = bss->drv;
  6785. int ret = -1;
  6786. int i;
  6787. int was_ap = is_ap_interface(drv->nlmode);
  6788. int res;
  6789. res = nl80211_set_mode(drv, drv->ifindex, nlmode);
  6790. if (res && nlmode == nl80211_get_ifmode(bss))
  6791. res = 0;
  6792. if (res == 0) {
  6793. drv->nlmode = nlmode;
  6794. ret = 0;
  6795. goto done;
  6796. }
  6797. if (res == -ENODEV)
  6798. return -1;
  6799. if (nlmode == drv->nlmode) {
  6800. wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
  6801. "requested mode - ignore error");
  6802. ret = 0;
  6803. goto done; /* Already in the requested mode */
  6804. }
  6805. /* mac80211 doesn't allow mode changes while the device is up, so
  6806. * take the device down, try to set the mode again, and bring the
  6807. * device back up.
  6808. */
  6809. wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
  6810. "interface down");
  6811. for (i = 0; i < 10; i++) {
  6812. res = i802_set_iface_flags(bss, 0);
  6813. if (res == -EACCES || res == -ENODEV)
  6814. break;
  6815. if (res == 0) {
  6816. /* Try to set the mode again while the interface is
  6817. * down */
  6818. ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
  6819. if (ret == -EACCES)
  6820. break;
  6821. res = i802_set_iface_flags(bss, 1);
  6822. if (res && !ret)
  6823. ret = -1;
  6824. else if (ret != -EBUSY)
  6825. break;
  6826. } else
  6827. wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
  6828. "interface down");
  6829. os_sleep(0, 100000);
  6830. }
  6831. if (!ret) {
  6832. wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
  6833. "interface is down");
  6834. drv->nlmode = nlmode;
  6835. drv->ignore_if_down_event = 1;
  6836. }
  6837. done:
  6838. if (ret) {
  6839. wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
  6840. "from %d failed", nlmode, drv->nlmode);
  6841. return ret;
  6842. }
  6843. if (is_p2p_net_interface(nlmode))
  6844. nl80211_disable_11b_rates(drv, drv->ifindex, 1);
  6845. else if (drv->disabled_11b_rates)
  6846. nl80211_disable_11b_rates(drv, drv->ifindex, 0);
  6847. if (is_ap_interface(nlmode)) {
  6848. nl80211_mgmt_unsubscribe(bss, "start AP");
  6849. /* Setup additional AP mode functionality if needed */
  6850. if (nl80211_setup_ap(bss))
  6851. return -1;
  6852. } else if (was_ap) {
  6853. /* Remove additional AP mode functionality */
  6854. nl80211_teardown_ap(bss);
  6855. } else {
  6856. nl80211_mgmt_unsubscribe(bss, "mode change");
  6857. }
  6858. if (!bss->in_deinit && !is_ap_interface(nlmode) &&
  6859. nl80211_mgmt_subscribe_non_ap(bss) < 0)
  6860. wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
  6861. "frame processing - ignore for now");
  6862. return 0;
  6863. }
  6864. static int wpa_driver_nl80211_get_capa(void *priv,
  6865. struct wpa_driver_capa *capa)
  6866. {
  6867. struct i802_bss *bss = priv;
  6868. struct wpa_driver_nl80211_data *drv = bss->drv;
  6869. if (!drv->has_capability)
  6870. return -1;
  6871. os_memcpy(capa, &drv->capa, sizeof(*capa));
  6872. if (drv->extended_capa && drv->extended_capa_mask) {
  6873. capa->extended_capa = drv->extended_capa;
  6874. capa->extended_capa_mask = drv->extended_capa_mask;
  6875. capa->extended_capa_len = drv->extended_capa_len;
  6876. }
  6877. if ((capa->flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  6878. !drv->allow_p2p_device) {
  6879. wpa_printf(MSG_DEBUG, "nl80211: Do not indicate P2P_DEVICE support (p2p_device=1 driver param not specified)");
  6880. capa->flags &= ~WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE;
  6881. }
  6882. return 0;
  6883. }
  6884. static int wpa_driver_nl80211_set_operstate(void *priv, int state)
  6885. {
  6886. struct i802_bss *bss = priv;
  6887. struct wpa_driver_nl80211_data *drv = bss->drv;
  6888. wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
  6889. __func__, drv->operstate, state, state ? "UP" : "DORMANT");
  6890. drv->operstate = state;
  6891. return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
  6892. state ? IF_OPER_UP : IF_OPER_DORMANT);
  6893. }
  6894. static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
  6895. {
  6896. struct i802_bss *bss = priv;
  6897. struct wpa_driver_nl80211_data *drv = bss->drv;
  6898. struct nl_msg *msg;
  6899. struct nl80211_sta_flag_update upd;
  6900. wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
  6901. MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
  6902. msg = nlmsg_alloc();
  6903. if (!msg)
  6904. return -ENOMEM;
  6905. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
  6906. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  6907. if_nametoindex(bss->ifname));
  6908. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
  6909. os_memset(&upd, 0, sizeof(upd));
  6910. upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
  6911. if (authorized)
  6912. upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
  6913. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  6914. return send_and_recv_msgs(drv, msg, NULL, NULL);
  6915. nla_put_failure:
  6916. nlmsg_free(msg);
  6917. return -ENOBUFS;
  6918. }
  6919. /* Set kernel driver on given frequency (MHz) */
  6920. static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
  6921. {
  6922. struct i802_bss *bss = priv;
  6923. return wpa_driver_nl80211_set_freq(bss, freq);
  6924. }
  6925. #if defined(HOSTAPD) || defined(CONFIG_AP)
  6926. static inline int min_int(int a, int b)
  6927. {
  6928. if (a < b)
  6929. return a;
  6930. return b;
  6931. }
  6932. static int get_key_handler(struct nl_msg *msg, void *arg)
  6933. {
  6934. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  6935. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  6936. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  6937. genlmsg_attrlen(gnlh, 0), NULL);
  6938. /*
  6939. * TODO: validate the key index and mac address!
  6940. * Otherwise, there's a race condition as soon as
  6941. * the kernel starts sending key notifications.
  6942. */
  6943. if (tb[NL80211_ATTR_KEY_SEQ])
  6944. memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  6945. min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
  6946. return NL_SKIP;
  6947. }
  6948. static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
  6949. int idx, u8 *seq)
  6950. {
  6951. struct i802_bss *bss = priv;
  6952. struct wpa_driver_nl80211_data *drv = bss->drv;
  6953. struct nl_msg *msg;
  6954. msg = nlmsg_alloc();
  6955. if (!msg)
  6956. return -ENOMEM;
  6957. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
  6958. if (addr)
  6959. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  6960. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
  6961. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
  6962. memset(seq, 0, 6);
  6963. return send_and_recv_msgs(drv, msg, get_key_handler, seq);
  6964. nla_put_failure:
  6965. nlmsg_free(msg);
  6966. return -ENOBUFS;
  6967. }
  6968. static int i802_set_rts(void *priv, int rts)
  6969. {
  6970. struct i802_bss *bss = priv;
  6971. struct wpa_driver_nl80211_data *drv = bss->drv;
  6972. struct nl_msg *msg;
  6973. int ret = -ENOBUFS;
  6974. u32 val;
  6975. msg = nlmsg_alloc();
  6976. if (!msg)
  6977. return -ENOMEM;
  6978. if (rts >= 2347)
  6979. val = (u32) -1;
  6980. else
  6981. val = rts;
  6982. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
  6983. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  6984. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
  6985. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6986. msg = NULL;
  6987. if (!ret)
  6988. return 0;
  6989. nla_put_failure:
  6990. nlmsg_free(msg);
  6991. wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
  6992. "%d (%s)", rts, ret, strerror(-ret));
  6993. return ret;
  6994. }
  6995. static int i802_set_frag(void *priv, int frag)
  6996. {
  6997. struct i802_bss *bss = priv;
  6998. struct wpa_driver_nl80211_data *drv = bss->drv;
  6999. struct nl_msg *msg;
  7000. int ret = -ENOBUFS;
  7001. u32 val;
  7002. msg = nlmsg_alloc();
  7003. if (!msg)
  7004. return -ENOMEM;
  7005. if (frag >= 2346)
  7006. val = (u32) -1;
  7007. else
  7008. val = frag;
  7009. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
  7010. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  7011. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
  7012. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7013. msg = NULL;
  7014. if (!ret)
  7015. return 0;
  7016. nla_put_failure:
  7017. nlmsg_free(msg);
  7018. wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
  7019. "%d: %d (%s)", frag, ret, strerror(-ret));
  7020. return ret;
  7021. }
  7022. static int i802_flush(void *priv)
  7023. {
  7024. struct i802_bss *bss = priv;
  7025. struct wpa_driver_nl80211_data *drv = bss->drv;
  7026. struct nl_msg *msg;
  7027. int res;
  7028. msg = nlmsg_alloc();
  7029. if (!msg)
  7030. return -1;
  7031. wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
  7032. bss->ifname);
  7033. nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
  7034. /*
  7035. * XXX: FIX! this needs to flush all VLANs too
  7036. */
  7037. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  7038. if_nametoindex(bss->ifname));
  7039. res = send_and_recv_msgs(drv, msg, NULL, NULL);
  7040. if (res) {
  7041. wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
  7042. "(%s)", res, strerror(-res));
  7043. }
  7044. return res;
  7045. nla_put_failure:
  7046. nlmsg_free(msg);
  7047. return -ENOBUFS;
  7048. }
  7049. #endif /* HOSTAPD || CONFIG_AP */
  7050. static int get_sta_handler(struct nl_msg *msg, void *arg)
  7051. {
  7052. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  7053. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  7054. struct hostap_sta_driver_data *data = arg;
  7055. struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
  7056. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  7057. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  7058. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  7059. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  7060. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  7061. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  7062. [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
  7063. };
  7064. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  7065. genlmsg_attrlen(gnlh, 0), NULL);
  7066. /*
  7067. * TODO: validate the interface and mac address!
  7068. * Otherwise, there's a race condition as soon as
  7069. * the kernel starts sending station notifications.
  7070. */
  7071. if (!tb[NL80211_ATTR_STA_INFO]) {
  7072. wpa_printf(MSG_DEBUG, "sta stats missing!");
  7073. return NL_SKIP;
  7074. }
  7075. if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
  7076. tb[NL80211_ATTR_STA_INFO],
  7077. stats_policy)) {
  7078. wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
  7079. return NL_SKIP;
  7080. }
  7081. if (stats[NL80211_STA_INFO_INACTIVE_TIME])
  7082. data->inactive_msec =
  7083. nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
  7084. if (stats[NL80211_STA_INFO_RX_BYTES])
  7085. data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
  7086. if (stats[NL80211_STA_INFO_TX_BYTES])
  7087. data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
  7088. if (stats[NL80211_STA_INFO_RX_PACKETS])
  7089. data->rx_packets =
  7090. nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
  7091. if (stats[NL80211_STA_INFO_TX_PACKETS])
  7092. data->tx_packets =
  7093. nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
  7094. if (stats[NL80211_STA_INFO_TX_FAILED])
  7095. data->tx_retry_failed =
  7096. nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
  7097. return NL_SKIP;
  7098. }
  7099. static int i802_read_sta_data(struct i802_bss *bss,
  7100. struct hostap_sta_driver_data *data,
  7101. const u8 *addr)
  7102. {
  7103. struct wpa_driver_nl80211_data *drv = bss->drv;
  7104. struct nl_msg *msg;
  7105. os_memset(data, 0, sizeof(*data));
  7106. msg = nlmsg_alloc();
  7107. if (!msg)
  7108. return -ENOMEM;
  7109. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
  7110. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  7111. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  7112. return send_and_recv_msgs(drv, msg, get_sta_handler, data);
  7113. nla_put_failure:
  7114. nlmsg_free(msg);
  7115. return -ENOBUFS;
  7116. }
  7117. #if defined(HOSTAPD) || defined(CONFIG_AP)
  7118. static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
  7119. int cw_min, int cw_max, int burst_time)
  7120. {
  7121. struct i802_bss *bss = priv;
  7122. struct wpa_driver_nl80211_data *drv = bss->drv;
  7123. struct nl_msg *msg;
  7124. struct nlattr *txq, *params;
  7125. msg = nlmsg_alloc();
  7126. if (!msg)
  7127. return -1;
  7128. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
  7129. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  7130. txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
  7131. if (!txq)
  7132. goto nla_put_failure;
  7133. /* We are only sending parameters for a single TXQ at a time */
  7134. params = nla_nest_start(msg, 1);
  7135. if (!params)
  7136. goto nla_put_failure;
  7137. switch (queue) {
  7138. case 0:
  7139. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
  7140. break;
  7141. case 1:
  7142. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
  7143. break;
  7144. case 2:
  7145. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
  7146. break;
  7147. case 3:
  7148. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
  7149. break;
  7150. }
  7151. /* Burst time is configured in units of 0.1 msec and TXOP parameter in
  7152. * 32 usec, so need to convert the value here. */
  7153. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
  7154. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
  7155. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
  7156. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
  7157. nla_nest_end(msg, params);
  7158. nla_nest_end(msg, txq);
  7159. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  7160. return 0;
  7161. msg = NULL;
  7162. nla_put_failure:
  7163. nlmsg_free(msg);
  7164. return -1;
  7165. }
  7166. static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
  7167. const char *ifname, int vlan_id)
  7168. {
  7169. struct wpa_driver_nl80211_data *drv = bss->drv;
  7170. struct nl_msg *msg;
  7171. int ret = -ENOBUFS;
  7172. msg = nlmsg_alloc();
  7173. if (!msg)
  7174. return -ENOMEM;
  7175. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
  7176. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  7177. if_nametoindex(bss->ifname));
  7178. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  7179. NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
  7180. if_nametoindex(ifname));
  7181. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7182. msg = NULL;
  7183. if (ret < 0) {
  7184. wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
  7185. MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
  7186. MAC2STR(addr), ifname, vlan_id, ret,
  7187. strerror(-ret));
  7188. }
  7189. nla_put_failure:
  7190. nlmsg_free(msg);
  7191. return ret;
  7192. }
  7193. static int i802_get_inact_sec(void *priv, const u8 *addr)
  7194. {
  7195. struct hostap_sta_driver_data data;
  7196. int ret;
  7197. data.inactive_msec = (unsigned long) -1;
  7198. ret = i802_read_sta_data(priv, &data, addr);
  7199. if (ret || data.inactive_msec == (unsigned long) -1)
  7200. return -1;
  7201. return data.inactive_msec / 1000;
  7202. }
  7203. static int i802_sta_clear_stats(void *priv, const u8 *addr)
  7204. {
  7205. #if 0
  7206. /* TODO */
  7207. #endif
  7208. return 0;
  7209. }
  7210. static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
  7211. int reason)
  7212. {
  7213. struct i802_bss *bss = priv;
  7214. struct wpa_driver_nl80211_data *drv = bss->drv;
  7215. struct ieee80211_mgmt mgmt;
  7216. if (drv->device_ap_sme)
  7217. return wpa_driver_nl80211_sta_remove(bss, addr);
  7218. memset(&mgmt, 0, sizeof(mgmt));
  7219. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  7220. WLAN_FC_STYPE_DEAUTH);
  7221. memcpy(mgmt.da, addr, ETH_ALEN);
  7222. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  7223. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  7224. mgmt.u.deauth.reason_code = host_to_le16(reason);
  7225. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  7226. IEEE80211_HDRLEN +
  7227. sizeof(mgmt.u.deauth), 0, 0, 0, 0,
  7228. 0);
  7229. }
  7230. static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
  7231. int reason)
  7232. {
  7233. struct i802_bss *bss = priv;
  7234. struct wpa_driver_nl80211_data *drv = bss->drv;
  7235. struct ieee80211_mgmt mgmt;
  7236. if (drv->device_ap_sme)
  7237. return wpa_driver_nl80211_sta_remove(bss, addr);
  7238. memset(&mgmt, 0, sizeof(mgmt));
  7239. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  7240. WLAN_FC_STYPE_DISASSOC);
  7241. memcpy(mgmt.da, addr, ETH_ALEN);
  7242. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  7243. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  7244. mgmt.u.disassoc.reason_code = host_to_le16(reason);
  7245. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  7246. IEEE80211_HDRLEN +
  7247. sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
  7248. 0);
  7249. }
  7250. #endif /* HOSTAPD || CONFIG_AP */
  7251. #ifdef HOSTAPD
  7252. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  7253. {
  7254. int i;
  7255. int *old;
  7256. wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
  7257. ifidx);
  7258. for (i = 0; i < drv->num_if_indices; i++) {
  7259. if (drv->if_indices[i] == 0) {
  7260. drv->if_indices[i] = ifidx;
  7261. return;
  7262. }
  7263. }
  7264. if (drv->if_indices != drv->default_if_indices)
  7265. old = drv->if_indices;
  7266. else
  7267. old = NULL;
  7268. drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
  7269. sizeof(int));
  7270. if (!drv->if_indices) {
  7271. if (!old)
  7272. drv->if_indices = drv->default_if_indices;
  7273. else
  7274. drv->if_indices = old;
  7275. wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
  7276. "interfaces");
  7277. wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
  7278. return;
  7279. } else if (!old)
  7280. os_memcpy(drv->if_indices, drv->default_if_indices,
  7281. sizeof(drv->default_if_indices));
  7282. drv->if_indices[drv->num_if_indices] = ifidx;
  7283. drv->num_if_indices++;
  7284. }
  7285. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  7286. {
  7287. int i;
  7288. for (i = 0; i < drv->num_if_indices; i++) {
  7289. if (drv->if_indices[i] == ifidx) {
  7290. drv->if_indices[i] = 0;
  7291. break;
  7292. }
  7293. }
  7294. }
  7295. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  7296. {
  7297. int i;
  7298. for (i = 0; i < drv->num_if_indices; i++)
  7299. if (drv->if_indices[i] == ifidx)
  7300. return 1;
  7301. return 0;
  7302. }
  7303. static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
  7304. const char *bridge_ifname, char *ifname_wds)
  7305. {
  7306. struct i802_bss *bss = priv;
  7307. struct wpa_driver_nl80211_data *drv = bss->drv;
  7308. char name[IFNAMSIZ + 1];
  7309. os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
  7310. if (ifname_wds)
  7311. os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
  7312. wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
  7313. " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
  7314. if (val) {
  7315. if (!if_nametoindex(name)) {
  7316. if (nl80211_create_iface(drv, name,
  7317. NL80211_IFTYPE_AP_VLAN,
  7318. bss->addr, 1, NULL, NULL) < 0)
  7319. return -1;
  7320. if (bridge_ifname &&
  7321. linux_br_add_if(drv->global->ioctl_sock,
  7322. bridge_ifname, name) < 0)
  7323. return -1;
  7324. }
  7325. if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
  7326. wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
  7327. "interface %s up", name);
  7328. }
  7329. return i802_set_sta_vlan(priv, addr, name, 0);
  7330. } else {
  7331. if (bridge_ifname)
  7332. linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
  7333. name);
  7334. i802_set_sta_vlan(priv, addr, bss->ifname, 0);
  7335. return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
  7336. name);
  7337. }
  7338. }
  7339. static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
  7340. {
  7341. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  7342. struct sockaddr_ll lladdr;
  7343. unsigned char buf[3000];
  7344. int len;
  7345. socklen_t fromlen = sizeof(lladdr);
  7346. len = recvfrom(sock, buf, sizeof(buf), 0,
  7347. (struct sockaddr *)&lladdr, &fromlen);
  7348. if (len < 0) {
  7349. perror("recv");
  7350. return;
  7351. }
  7352. if (have_ifidx(drv, lladdr.sll_ifindex))
  7353. drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
  7354. }
  7355. static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
  7356. struct i802_bss *bss,
  7357. const char *brname, const char *ifname)
  7358. {
  7359. int ifindex;
  7360. char in_br[IFNAMSIZ];
  7361. os_strlcpy(bss->brname, brname, IFNAMSIZ);
  7362. ifindex = if_nametoindex(brname);
  7363. if (ifindex == 0) {
  7364. /*
  7365. * Bridge was configured, but the bridge device does
  7366. * not exist. Try to add it now.
  7367. */
  7368. if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
  7369. wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
  7370. "bridge interface %s: %s",
  7371. brname, strerror(errno));
  7372. return -1;
  7373. }
  7374. bss->added_bridge = 1;
  7375. add_ifidx(drv, if_nametoindex(brname));
  7376. }
  7377. if (linux_br_get(in_br, ifname) == 0) {
  7378. if (os_strcmp(in_br, brname) == 0)
  7379. return 0; /* already in the bridge */
  7380. wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
  7381. "bridge %s", ifname, in_br);
  7382. if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
  7383. 0) {
  7384. wpa_printf(MSG_ERROR, "nl80211: Failed to "
  7385. "remove interface %s from bridge "
  7386. "%s: %s",
  7387. ifname, brname, strerror(errno));
  7388. return -1;
  7389. }
  7390. }
  7391. wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
  7392. ifname, brname);
  7393. if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
  7394. wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
  7395. "into bridge %s: %s",
  7396. ifname, brname, strerror(errno));
  7397. return -1;
  7398. }
  7399. bss->added_if_into_bridge = 1;
  7400. return 0;
  7401. }
  7402. static void *i802_init(struct hostapd_data *hapd,
  7403. struct wpa_init_params *params)
  7404. {
  7405. struct wpa_driver_nl80211_data *drv;
  7406. struct i802_bss *bss;
  7407. size_t i;
  7408. char brname[IFNAMSIZ];
  7409. int ifindex, br_ifindex;
  7410. int br_added = 0;
  7411. bss = wpa_driver_nl80211_init(hapd, params->ifname,
  7412. params->global_priv);
  7413. if (bss == NULL)
  7414. return NULL;
  7415. drv = bss->drv;
  7416. drv->nlmode = NL80211_IFTYPE_AP;
  7417. drv->eapol_sock = -1;
  7418. if (linux_br_get(brname, params->ifname) == 0) {
  7419. wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
  7420. params->ifname, brname);
  7421. br_ifindex = if_nametoindex(brname);
  7422. } else {
  7423. brname[0] = '\0';
  7424. br_ifindex = 0;
  7425. }
  7426. drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
  7427. drv->if_indices = drv->default_if_indices;
  7428. for (i = 0; i < params->num_bridge; i++) {
  7429. if (params->bridge[i]) {
  7430. ifindex = if_nametoindex(params->bridge[i]);
  7431. if (ifindex)
  7432. add_ifidx(drv, ifindex);
  7433. if (ifindex == br_ifindex)
  7434. br_added = 1;
  7435. }
  7436. }
  7437. if (!br_added && br_ifindex &&
  7438. (params->num_bridge == 0 || !params->bridge[0]))
  7439. add_ifidx(drv, br_ifindex);
  7440. /* start listening for EAPOL on the default AP interface */
  7441. add_ifidx(drv, drv->ifindex);
  7442. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
  7443. goto failed;
  7444. if (params->bssid) {
  7445. if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  7446. params->bssid))
  7447. goto failed;
  7448. }
  7449. if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
  7450. wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
  7451. "into AP mode", bss->ifname);
  7452. goto failed;
  7453. }
  7454. if (params->num_bridge && params->bridge[0] &&
  7455. i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
  7456. goto failed;
  7457. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
  7458. goto failed;
  7459. drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
  7460. if (drv->eapol_sock < 0) {
  7461. perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
  7462. goto failed;
  7463. }
  7464. if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
  7465. {
  7466. printf("Could not register read socket for eapol\n");
  7467. goto failed;
  7468. }
  7469. if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  7470. params->own_addr))
  7471. goto failed;
  7472. memcpy(bss->addr, params->own_addr, ETH_ALEN);
  7473. return bss;
  7474. failed:
  7475. wpa_driver_nl80211_deinit(bss);
  7476. return NULL;
  7477. }
  7478. static void i802_deinit(void *priv)
  7479. {
  7480. struct i802_bss *bss = priv;
  7481. wpa_driver_nl80211_deinit(bss);
  7482. }
  7483. #endif /* HOSTAPD */
  7484. static enum nl80211_iftype wpa_driver_nl80211_if_type(
  7485. enum wpa_driver_if_type type)
  7486. {
  7487. switch (type) {
  7488. case WPA_IF_STATION:
  7489. return NL80211_IFTYPE_STATION;
  7490. case WPA_IF_P2P_CLIENT:
  7491. case WPA_IF_P2P_GROUP:
  7492. return NL80211_IFTYPE_P2P_CLIENT;
  7493. case WPA_IF_AP_VLAN:
  7494. return NL80211_IFTYPE_AP_VLAN;
  7495. case WPA_IF_AP_BSS:
  7496. return NL80211_IFTYPE_AP;
  7497. case WPA_IF_P2P_GO:
  7498. return NL80211_IFTYPE_P2P_GO;
  7499. case WPA_IF_P2P_DEVICE:
  7500. return NL80211_IFTYPE_P2P_DEVICE;
  7501. }
  7502. return -1;
  7503. }
  7504. #ifdef CONFIG_P2P
  7505. static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
  7506. {
  7507. struct wpa_driver_nl80211_data *drv;
  7508. dl_list_for_each(drv, &global->interfaces,
  7509. struct wpa_driver_nl80211_data, list) {
  7510. if (os_memcmp(addr, drv->first_bss.addr, ETH_ALEN) == 0)
  7511. return 1;
  7512. }
  7513. return 0;
  7514. }
  7515. static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
  7516. u8 *new_addr)
  7517. {
  7518. unsigned int idx;
  7519. if (!drv->global)
  7520. return -1;
  7521. os_memcpy(new_addr, drv->first_bss.addr, ETH_ALEN);
  7522. for (idx = 0; idx < 64; idx++) {
  7523. new_addr[0] = drv->first_bss.addr[0] | 0x02;
  7524. new_addr[0] ^= idx << 2;
  7525. if (!nl80211_addr_in_use(drv->global, new_addr))
  7526. break;
  7527. }
  7528. if (idx == 64)
  7529. return -1;
  7530. wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
  7531. MACSTR, MAC2STR(new_addr));
  7532. return 0;
  7533. }
  7534. #endif /* CONFIG_P2P */
  7535. struct wdev_info {
  7536. u64 wdev_id;
  7537. int wdev_id_set;
  7538. u8 macaddr[ETH_ALEN];
  7539. };
  7540. static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
  7541. {
  7542. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  7543. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  7544. struct wdev_info *wi = arg;
  7545. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  7546. genlmsg_attrlen(gnlh, 0), NULL);
  7547. if (tb[NL80211_ATTR_WDEV]) {
  7548. wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
  7549. wi->wdev_id_set = 1;
  7550. }
  7551. if (tb[NL80211_ATTR_MAC])
  7552. os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
  7553. ETH_ALEN);
  7554. return NL_SKIP;
  7555. }
  7556. static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
  7557. const char *ifname, const u8 *addr,
  7558. void *bss_ctx, void **drv_priv,
  7559. char *force_ifname, u8 *if_addr,
  7560. const char *bridge)
  7561. {
  7562. enum nl80211_iftype nlmode;
  7563. struct i802_bss *bss = priv;
  7564. struct wpa_driver_nl80211_data *drv = bss->drv;
  7565. int ifidx;
  7566. #ifdef HOSTAPD
  7567. struct i802_bss *new_bss = NULL;
  7568. if (type == WPA_IF_AP_BSS) {
  7569. new_bss = os_zalloc(sizeof(*new_bss));
  7570. if (new_bss == NULL)
  7571. return -1;
  7572. }
  7573. #endif /* HOSTAPD */
  7574. if (addr)
  7575. os_memcpy(if_addr, addr, ETH_ALEN);
  7576. nlmode = wpa_driver_nl80211_if_type(type);
  7577. if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
  7578. struct wdev_info p2pdev_info;
  7579. os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
  7580. ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
  7581. 0, nl80211_wdev_handler,
  7582. &p2pdev_info);
  7583. if (!p2pdev_info.wdev_id_set || ifidx != 0) {
  7584. wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
  7585. ifname);
  7586. return -1;
  7587. }
  7588. drv->global->if_add_wdevid = p2pdev_info.wdev_id;
  7589. drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
  7590. if (!is_zero_ether_addr(p2pdev_info.macaddr))
  7591. os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
  7592. wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
  7593. ifname,
  7594. (long long unsigned int) p2pdev_info.wdev_id);
  7595. } else {
  7596. ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
  7597. 0, NULL, NULL);
  7598. if (ifidx < 0) {
  7599. #ifdef HOSTAPD
  7600. os_free(new_bss);
  7601. #endif /* HOSTAPD */
  7602. return -1;
  7603. }
  7604. }
  7605. if (!addr) {
  7606. if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
  7607. os_memcpy(if_addr, bss->addr, ETH_ALEN);
  7608. else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
  7609. bss->ifname, if_addr) < 0) {
  7610. nl80211_remove_iface(drv, ifidx);
  7611. return -1;
  7612. }
  7613. }
  7614. #ifdef CONFIG_P2P
  7615. if (!addr &&
  7616. (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
  7617. type == WPA_IF_P2P_GO)) {
  7618. /* Enforce unique P2P Interface Address */
  7619. u8 new_addr[ETH_ALEN];
  7620. if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
  7621. new_addr) < 0) {
  7622. nl80211_remove_iface(drv, ifidx);
  7623. return -1;
  7624. }
  7625. if (nl80211_addr_in_use(drv->global, new_addr)) {
  7626. wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
  7627. "for P2P group interface");
  7628. if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
  7629. nl80211_remove_iface(drv, ifidx);
  7630. return -1;
  7631. }
  7632. if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
  7633. new_addr) < 0) {
  7634. nl80211_remove_iface(drv, ifidx);
  7635. return -1;
  7636. }
  7637. }
  7638. os_memcpy(if_addr, new_addr, ETH_ALEN);
  7639. }
  7640. #endif /* CONFIG_P2P */
  7641. #ifdef HOSTAPD
  7642. if (bridge &&
  7643. i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
  7644. wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
  7645. "interface %s to a bridge %s", ifname, bridge);
  7646. nl80211_remove_iface(drv, ifidx);
  7647. os_free(new_bss);
  7648. return -1;
  7649. }
  7650. if (type == WPA_IF_AP_BSS) {
  7651. if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
  7652. {
  7653. nl80211_remove_iface(drv, ifidx);
  7654. os_free(new_bss);
  7655. return -1;
  7656. }
  7657. os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
  7658. os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
  7659. new_bss->ifindex = ifidx;
  7660. new_bss->drv = drv;
  7661. new_bss->next = drv->first_bss.next;
  7662. new_bss->freq = drv->first_bss.freq;
  7663. new_bss->ctx = bss_ctx;
  7664. drv->first_bss.next = new_bss;
  7665. if (drv_priv)
  7666. *drv_priv = new_bss;
  7667. nl80211_init_bss(new_bss);
  7668. /* Subscribe management frames for this WPA_IF_AP_BSS */
  7669. if (nl80211_setup_ap(new_bss))
  7670. return -1;
  7671. }
  7672. #endif /* HOSTAPD */
  7673. if (drv->global)
  7674. drv->global->if_add_ifindex = ifidx;
  7675. return 0;
  7676. }
  7677. static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
  7678. enum wpa_driver_if_type type,
  7679. const char *ifname)
  7680. {
  7681. struct wpa_driver_nl80211_data *drv = bss->drv;
  7682. int ifindex = if_nametoindex(ifname);
  7683. wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
  7684. __func__, type, ifname, ifindex);
  7685. if (ifindex <= 0)
  7686. return -1;
  7687. nl80211_remove_iface(drv, ifindex);
  7688. #ifdef HOSTAPD
  7689. if (type != WPA_IF_AP_BSS)
  7690. return 0;
  7691. if (bss->added_if_into_bridge) {
  7692. if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
  7693. bss->ifname) < 0)
  7694. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  7695. "interface %s from bridge %s: %s",
  7696. bss->ifname, bss->brname, strerror(errno));
  7697. }
  7698. if (bss->added_bridge) {
  7699. if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
  7700. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  7701. "bridge %s: %s",
  7702. bss->brname, strerror(errno));
  7703. }
  7704. if (bss != &drv->first_bss) {
  7705. struct i802_bss *tbss;
  7706. for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
  7707. if (tbss->next == bss) {
  7708. tbss->next = bss->next;
  7709. /* Unsubscribe management frames */
  7710. nl80211_teardown_ap(bss);
  7711. nl80211_destroy_bss(bss);
  7712. os_free(bss);
  7713. bss = NULL;
  7714. break;
  7715. }
  7716. }
  7717. if (bss)
  7718. wpa_printf(MSG_INFO, "nl80211: %s - could not find "
  7719. "BSS %p in the list", __func__, bss);
  7720. }
  7721. #endif /* HOSTAPD */
  7722. return 0;
  7723. }
  7724. static int cookie_handler(struct nl_msg *msg, void *arg)
  7725. {
  7726. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  7727. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  7728. u64 *cookie = arg;
  7729. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  7730. genlmsg_attrlen(gnlh, 0), NULL);
  7731. if (tb[NL80211_ATTR_COOKIE])
  7732. *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
  7733. return NL_SKIP;
  7734. }
  7735. static int nl80211_send_frame_cmd(struct i802_bss *bss,
  7736. unsigned int freq, unsigned int wait,
  7737. const u8 *buf, size_t buf_len,
  7738. u64 *cookie_out, int no_cck, int no_ack,
  7739. int offchanok)
  7740. {
  7741. struct wpa_driver_nl80211_data *drv = bss->drv;
  7742. struct nl_msg *msg;
  7743. u64 cookie;
  7744. int ret = -1;
  7745. msg = nlmsg_alloc();
  7746. if (!msg)
  7747. return -1;
  7748. wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
  7749. "no_ack=%d offchanok=%d",
  7750. freq, wait, no_cck, no_ack, offchanok);
  7751. wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
  7752. nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
  7753. if (nl80211_set_iface_id(msg, bss) < 0)
  7754. goto nla_put_failure;
  7755. if (freq)
  7756. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  7757. if (wait)
  7758. NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
  7759. if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
  7760. NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
  7761. if (no_cck)
  7762. NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
  7763. if (no_ack)
  7764. NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
  7765. NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
  7766. cookie = 0;
  7767. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  7768. msg = NULL;
  7769. if (ret) {
  7770. wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
  7771. "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
  7772. freq, wait);
  7773. goto nla_put_failure;
  7774. }
  7775. wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
  7776. "cookie 0x%llx", no_ack ? " (no ACK)" : "",
  7777. (long long unsigned int) cookie);
  7778. if (cookie_out)
  7779. *cookie_out = no_ack ? (u64) -1 : cookie;
  7780. nla_put_failure:
  7781. nlmsg_free(msg);
  7782. return ret;
  7783. }
  7784. static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
  7785. unsigned int freq,
  7786. unsigned int wait_time,
  7787. const u8 *dst, const u8 *src,
  7788. const u8 *bssid,
  7789. const u8 *data, size_t data_len,
  7790. int no_cck)
  7791. {
  7792. struct wpa_driver_nl80211_data *drv = bss->drv;
  7793. int ret = -1;
  7794. u8 *buf;
  7795. struct ieee80211_hdr *hdr;
  7796. wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
  7797. "freq=%u MHz wait=%d ms no_cck=%d)",
  7798. drv->ifindex, freq, wait_time, no_cck);
  7799. buf = os_zalloc(24 + data_len);
  7800. if (buf == NULL)
  7801. return ret;
  7802. os_memcpy(buf + 24, data, data_len);
  7803. hdr = (struct ieee80211_hdr *) buf;
  7804. hdr->frame_control =
  7805. IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
  7806. os_memcpy(hdr->addr1, dst, ETH_ALEN);
  7807. os_memcpy(hdr->addr2, src, ETH_ALEN);
  7808. os_memcpy(hdr->addr3, bssid, ETH_ALEN);
  7809. if (is_ap_interface(drv->nlmode) &&
  7810. (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
  7811. (int) freq == bss->freq || drv->device_ap_sme ||
  7812. !drv->use_monitor))
  7813. ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
  7814. 0, freq, no_cck, 1,
  7815. wait_time);
  7816. else
  7817. ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
  7818. 24 + data_len,
  7819. &drv->send_action_cookie,
  7820. no_cck, 0, 1);
  7821. os_free(buf);
  7822. return ret;
  7823. }
  7824. static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
  7825. {
  7826. struct i802_bss *bss = priv;
  7827. struct wpa_driver_nl80211_data *drv = bss->drv;
  7828. struct nl_msg *msg;
  7829. int ret;
  7830. msg = nlmsg_alloc();
  7831. if (!msg)
  7832. return;
  7833. wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
  7834. (long long unsigned int) drv->send_action_cookie);
  7835. nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
  7836. if (nl80211_set_iface_id(msg, bss) < 0)
  7837. goto nla_put_failure;
  7838. NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
  7839. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7840. msg = NULL;
  7841. if (ret)
  7842. wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
  7843. "(%s)", ret, strerror(-ret));
  7844. nla_put_failure:
  7845. nlmsg_free(msg);
  7846. }
  7847. static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
  7848. unsigned int duration)
  7849. {
  7850. struct i802_bss *bss = priv;
  7851. struct wpa_driver_nl80211_data *drv = bss->drv;
  7852. struct nl_msg *msg;
  7853. int ret;
  7854. u64 cookie;
  7855. msg = nlmsg_alloc();
  7856. if (!msg)
  7857. return -1;
  7858. nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
  7859. if (nl80211_set_iface_id(msg, bss) < 0)
  7860. goto nla_put_failure;
  7861. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  7862. NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
  7863. cookie = 0;
  7864. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  7865. msg = NULL;
  7866. if (ret == 0) {
  7867. wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
  7868. "0x%llx for freq=%u MHz duration=%u",
  7869. (long long unsigned int) cookie, freq, duration);
  7870. drv->remain_on_chan_cookie = cookie;
  7871. drv->pending_remain_on_chan = 1;
  7872. return 0;
  7873. }
  7874. wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
  7875. "(freq=%d duration=%u): %d (%s)",
  7876. freq, duration, ret, strerror(-ret));
  7877. nla_put_failure:
  7878. nlmsg_free(msg);
  7879. return -1;
  7880. }
  7881. static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
  7882. {
  7883. struct i802_bss *bss = priv;
  7884. struct wpa_driver_nl80211_data *drv = bss->drv;
  7885. struct nl_msg *msg;
  7886. int ret;
  7887. if (!drv->pending_remain_on_chan) {
  7888. wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
  7889. "to cancel");
  7890. return -1;
  7891. }
  7892. wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
  7893. "0x%llx",
  7894. (long long unsigned int) drv->remain_on_chan_cookie);
  7895. msg = nlmsg_alloc();
  7896. if (!msg)
  7897. return -1;
  7898. nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
  7899. if (nl80211_set_iface_id(msg, bss) < 0)
  7900. goto nla_put_failure;
  7901. NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
  7902. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7903. msg = NULL;
  7904. if (ret == 0)
  7905. return 0;
  7906. wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
  7907. "%d (%s)", ret, strerror(-ret));
  7908. nla_put_failure:
  7909. nlmsg_free(msg);
  7910. return -1;
  7911. }
  7912. static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
  7913. {
  7914. struct wpa_driver_nl80211_data *drv = bss->drv;
  7915. if (!report) {
  7916. if (bss->nl_preq && drv->device_ap_sme &&
  7917. is_ap_interface(drv->nlmode)) {
  7918. /*
  7919. * Do not disable Probe Request reporting that was
  7920. * enabled in nl80211_setup_ap().
  7921. */
  7922. wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
  7923. "Probe Request reporting nl_preq=%p while "
  7924. "in AP mode", bss->nl_preq);
  7925. } else if (bss->nl_preq) {
  7926. wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
  7927. "reporting nl_preq=%p", bss->nl_preq);
  7928. eloop_unregister_read_sock(
  7929. nl_socket_get_fd(bss->nl_preq));
  7930. nl_destroy_handles(&bss->nl_preq);
  7931. }
  7932. return 0;
  7933. }
  7934. if (bss->nl_preq) {
  7935. wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
  7936. "already on! nl_preq=%p", bss->nl_preq);
  7937. return 0;
  7938. }
  7939. bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
  7940. if (bss->nl_preq == NULL)
  7941. return -1;
  7942. wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
  7943. "reporting nl_preq=%p", bss->nl_preq);
  7944. if (nl80211_register_frame(bss, bss->nl_preq,
  7945. (WLAN_FC_TYPE_MGMT << 2) |
  7946. (WLAN_FC_STYPE_PROBE_REQ << 4),
  7947. NULL, 0) < 0)
  7948. goto out_err;
  7949. eloop_register_read_sock(nl_socket_get_fd(bss->nl_preq),
  7950. wpa_driver_nl80211_event_receive, bss->nl_cb,
  7951. bss->nl_preq);
  7952. return 0;
  7953. out_err:
  7954. nl_destroy_handles(&bss->nl_preq);
  7955. return -1;
  7956. }
  7957. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  7958. int ifindex, int disabled)
  7959. {
  7960. struct nl_msg *msg;
  7961. struct nlattr *bands, *band;
  7962. int ret;
  7963. msg = nlmsg_alloc();
  7964. if (!msg)
  7965. return -1;
  7966. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
  7967. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  7968. bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
  7969. if (!bands)
  7970. goto nla_put_failure;
  7971. /*
  7972. * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
  7973. * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
  7974. * rates. All 5 GHz rates are left enabled.
  7975. */
  7976. band = nla_nest_start(msg, NL80211_BAND_2GHZ);
  7977. if (!band)
  7978. goto nla_put_failure;
  7979. if (disabled) {
  7980. NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
  7981. "\x0c\x12\x18\x24\x30\x48\x60\x6c");
  7982. }
  7983. nla_nest_end(msg, band);
  7984. nla_nest_end(msg, bands);
  7985. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7986. msg = NULL;
  7987. if (ret) {
  7988. wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
  7989. "(%s)", ret, strerror(-ret));
  7990. } else
  7991. drv->disabled_11b_rates = disabled;
  7992. return ret;
  7993. nla_put_failure:
  7994. nlmsg_free(msg);
  7995. return -1;
  7996. }
  7997. static int wpa_driver_nl80211_deinit_ap(void *priv)
  7998. {
  7999. struct i802_bss *bss = priv;
  8000. struct wpa_driver_nl80211_data *drv = bss->drv;
  8001. if (!is_ap_interface(drv->nlmode))
  8002. return -1;
  8003. wpa_driver_nl80211_del_beacon(drv);
  8004. /*
  8005. * If the P2P GO interface was dynamically added, then it is
  8006. * possible that the interface change to station is not possible.
  8007. */
  8008. if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
  8009. return 0;
  8010. return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
  8011. }
  8012. static int wpa_driver_nl80211_stop_ap(void *priv)
  8013. {
  8014. struct i802_bss *bss = priv;
  8015. struct wpa_driver_nl80211_data *drv = bss->drv;
  8016. if (!is_ap_interface(drv->nlmode))
  8017. return -1;
  8018. wpa_driver_nl80211_del_beacon(drv);
  8019. bss->beacon_set = 0;
  8020. return 0;
  8021. }
  8022. static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
  8023. {
  8024. struct i802_bss *bss = priv;
  8025. struct wpa_driver_nl80211_data *drv = bss->drv;
  8026. if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
  8027. return -1;
  8028. /*
  8029. * If the P2P Client interface was dynamically added, then it is
  8030. * possible that the interface change to station is not possible.
  8031. */
  8032. if (bss->if_dynamic)
  8033. return 0;
  8034. return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
  8035. }
  8036. static void wpa_driver_nl80211_resume(void *priv)
  8037. {
  8038. struct i802_bss *bss = priv;
  8039. if (i802_set_iface_flags(bss, 1))
  8040. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
  8041. }
  8042. static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
  8043. const u8 *ies, size_t ies_len)
  8044. {
  8045. struct i802_bss *bss = priv;
  8046. struct wpa_driver_nl80211_data *drv = bss->drv;
  8047. int ret;
  8048. u8 *data, *pos;
  8049. size_t data_len;
  8050. const u8 *own_addr = bss->addr;
  8051. if (action != 1) {
  8052. wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
  8053. "action %d", action);
  8054. return -1;
  8055. }
  8056. /*
  8057. * Action frame payload:
  8058. * Category[1] = 6 (Fast BSS Transition)
  8059. * Action[1] = 1 (Fast BSS Transition Request)
  8060. * STA Address
  8061. * Target AP Address
  8062. * FT IEs
  8063. */
  8064. data_len = 2 + 2 * ETH_ALEN + ies_len;
  8065. data = os_malloc(data_len);
  8066. if (data == NULL)
  8067. return -1;
  8068. pos = data;
  8069. *pos++ = 0x06; /* FT Action category */
  8070. *pos++ = action;
  8071. os_memcpy(pos, own_addr, ETH_ALEN);
  8072. pos += ETH_ALEN;
  8073. os_memcpy(pos, target_ap, ETH_ALEN);
  8074. pos += ETH_ALEN;
  8075. os_memcpy(pos, ies, ies_len);
  8076. ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
  8077. drv->bssid, own_addr, drv->bssid,
  8078. data, data_len, 0);
  8079. os_free(data);
  8080. return ret;
  8081. }
  8082. static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
  8083. {
  8084. struct i802_bss *bss = priv;
  8085. struct wpa_driver_nl80211_data *drv = bss->drv;
  8086. struct nl_msg *msg;
  8087. struct nlattr *cqm;
  8088. int ret = -1;
  8089. wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
  8090. "hysteresis=%d", threshold, hysteresis);
  8091. msg = nlmsg_alloc();
  8092. if (!msg)
  8093. return -1;
  8094. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
  8095. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  8096. cqm = nla_nest_start(msg, NL80211_ATTR_CQM);
  8097. if (cqm == NULL)
  8098. goto nla_put_failure;
  8099. NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
  8100. NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
  8101. nla_nest_end(msg, cqm);
  8102. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  8103. msg = NULL;
  8104. nla_put_failure:
  8105. nlmsg_free(msg);
  8106. return ret;
  8107. }
  8108. /* Converts nl80211_chan_width to a common format */
  8109. static enum chan_width convert2width(int width)
  8110. {
  8111. switch (width) {
  8112. case NL80211_CHAN_WIDTH_20_NOHT:
  8113. return CHAN_WIDTH_20_NOHT;
  8114. case NL80211_CHAN_WIDTH_20:
  8115. return CHAN_WIDTH_20;
  8116. case NL80211_CHAN_WIDTH_40:
  8117. return CHAN_WIDTH_40;
  8118. case NL80211_CHAN_WIDTH_80:
  8119. return CHAN_WIDTH_80;
  8120. case NL80211_CHAN_WIDTH_80P80:
  8121. return CHAN_WIDTH_80P80;
  8122. case NL80211_CHAN_WIDTH_160:
  8123. return CHAN_WIDTH_160;
  8124. }
  8125. return CHAN_WIDTH_UNKNOWN;
  8126. }
  8127. static int get_channel_width(struct nl_msg *msg, void *arg)
  8128. {
  8129. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  8130. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  8131. struct wpa_signal_info *sig_change = arg;
  8132. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  8133. genlmsg_attrlen(gnlh, 0), NULL);
  8134. sig_change->center_frq1 = -1;
  8135. sig_change->center_frq2 = -1;
  8136. sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
  8137. if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
  8138. sig_change->chanwidth = convert2width(
  8139. nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
  8140. if (tb[NL80211_ATTR_CENTER_FREQ1])
  8141. sig_change->center_frq1 =
  8142. nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
  8143. if (tb[NL80211_ATTR_CENTER_FREQ2])
  8144. sig_change->center_frq2 =
  8145. nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
  8146. }
  8147. return NL_SKIP;
  8148. }
  8149. static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
  8150. struct wpa_signal_info *sig)
  8151. {
  8152. struct nl_msg *msg;
  8153. msg = nlmsg_alloc();
  8154. if (!msg)
  8155. return -ENOMEM;
  8156. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_INTERFACE);
  8157. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  8158. return send_and_recv_msgs(drv, msg, get_channel_width, sig);
  8159. nla_put_failure:
  8160. nlmsg_free(msg);
  8161. return -ENOBUFS;
  8162. }
  8163. static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
  8164. {
  8165. struct i802_bss *bss = priv;
  8166. struct wpa_driver_nl80211_data *drv = bss->drv;
  8167. int res;
  8168. os_memset(si, 0, sizeof(*si));
  8169. res = nl80211_get_link_signal(drv, si);
  8170. if (res != 0)
  8171. return res;
  8172. res = nl80211_get_channel_width(drv, si);
  8173. if (res != 0)
  8174. return res;
  8175. return nl80211_get_link_noise(drv, si);
  8176. }
  8177. static int wpa_driver_nl80211_shared_freq(void *priv)
  8178. {
  8179. struct i802_bss *bss = priv;
  8180. struct wpa_driver_nl80211_data *drv = bss->drv;
  8181. struct wpa_driver_nl80211_data *driver;
  8182. int freq = 0;
  8183. /*
  8184. * If the same PHY is in connected state with some other interface,
  8185. * then retrieve the assoc freq.
  8186. */
  8187. wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
  8188. drv->phyname);
  8189. dl_list_for_each(driver, &drv->global->interfaces,
  8190. struct wpa_driver_nl80211_data, list) {
  8191. if (drv == driver ||
  8192. os_strcmp(drv->phyname, driver->phyname) != 0 ||
  8193. !driver->associated)
  8194. continue;
  8195. wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
  8196. MACSTR,
  8197. driver->phyname, driver->first_bss.ifname,
  8198. MAC2STR(driver->first_bss.addr));
  8199. if (is_ap_interface(driver->nlmode))
  8200. freq = driver->first_bss.freq;
  8201. else
  8202. freq = nl80211_get_assoc_freq(driver);
  8203. wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
  8204. drv->phyname, freq);
  8205. }
  8206. if (!freq)
  8207. wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
  8208. "PHY (%s) in associated state", drv->phyname);
  8209. return freq;
  8210. }
  8211. static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
  8212. int encrypt)
  8213. {
  8214. struct i802_bss *bss = priv;
  8215. return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
  8216. 0, 0, 0, 0);
  8217. }
  8218. static int nl80211_set_param(void *priv, const char *param)
  8219. {
  8220. wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
  8221. if (param == NULL)
  8222. return 0;
  8223. #ifdef CONFIG_P2P
  8224. if (os_strstr(param, "use_p2p_group_interface=1")) {
  8225. struct i802_bss *bss = priv;
  8226. struct wpa_driver_nl80211_data *drv = bss->drv;
  8227. wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
  8228. "interface");
  8229. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
  8230. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
  8231. }
  8232. if (os_strstr(param, "p2p_device=1")) {
  8233. struct i802_bss *bss = priv;
  8234. struct wpa_driver_nl80211_data *drv = bss->drv;
  8235. drv->allow_p2p_device = 1;
  8236. }
  8237. #endif /* CONFIG_P2P */
  8238. return 0;
  8239. }
  8240. static void * nl80211_global_init(void)
  8241. {
  8242. struct nl80211_global *global;
  8243. struct netlink_config *cfg;
  8244. global = os_zalloc(sizeof(*global));
  8245. if (global == NULL)
  8246. return NULL;
  8247. global->ioctl_sock = -1;
  8248. dl_list_init(&global->interfaces);
  8249. global->if_add_ifindex = -1;
  8250. cfg = os_zalloc(sizeof(*cfg));
  8251. if (cfg == NULL)
  8252. goto err;
  8253. cfg->ctx = global;
  8254. cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
  8255. cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
  8256. global->netlink = netlink_init(cfg);
  8257. if (global->netlink == NULL) {
  8258. os_free(cfg);
  8259. goto err;
  8260. }
  8261. if (wpa_driver_nl80211_init_nl_global(global) < 0)
  8262. goto err;
  8263. global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  8264. if (global->ioctl_sock < 0) {
  8265. perror("socket(PF_INET,SOCK_DGRAM)");
  8266. goto err;
  8267. }
  8268. return global;
  8269. err:
  8270. nl80211_global_deinit(global);
  8271. return NULL;
  8272. }
  8273. static void nl80211_global_deinit(void *priv)
  8274. {
  8275. struct nl80211_global *global = priv;
  8276. if (global == NULL)
  8277. return;
  8278. if (!dl_list_empty(&global->interfaces)) {
  8279. wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
  8280. "nl80211_global_deinit",
  8281. dl_list_len(&global->interfaces));
  8282. }
  8283. if (global->netlink)
  8284. netlink_deinit(global->netlink);
  8285. nl_destroy_handles(&global->nl);
  8286. if (global->nl_event) {
  8287. eloop_unregister_read_sock(
  8288. nl_socket_get_fd(global->nl_event));
  8289. nl_destroy_handles(&global->nl_event);
  8290. }
  8291. nl_cb_put(global->nl_cb);
  8292. if (global->ioctl_sock >= 0)
  8293. close(global->ioctl_sock);
  8294. os_free(global);
  8295. }
  8296. static const char * nl80211_get_radio_name(void *priv)
  8297. {
  8298. struct i802_bss *bss = priv;
  8299. struct wpa_driver_nl80211_data *drv = bss->drv;
  8300. return drv->phyname;
  8301. }
  8302. static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
  8303. const u8 *pmkid)
  8304. {
  8305. struct nl_msg *msg;
  8306. msg = nlmsg_alloc();
  8307. if (!msg)
  8308. return -ENOMEM;
  8309. nl80211_cmd(bss->drv, msg, 0, cmd);
  8310. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  8311. if (pmkid)
  8312. NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
  8313. if (bssid)
  8314. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
  8315. return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  8316. nla_put_failure:
  8317. nlmsg_free(msg);
  8318. return -ENOBUFS;
  8319. }
  8320. static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
  8321. {
  8322. struct i802_bss *bss = priv;
  8323. wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
  8324. return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
  8325. }
  8326. static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
  8327. {
  8328. struct i802_bss *bss = priv;
  8329. wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
  8330. MAC2STR(bssid));
  8331. return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
  8332. }
  8333. static int nl80211_flush_pmkid(void *priv)
  8334. {
  8335. struct i802_bss *bss = priv;
  8336. wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
  8337. return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
  8338. }
  8339. static void clean_survey_results(struct survey_results *survey_results)
  8340. {
  8341. struct freq_survey *survey, *tmp;
  8342. if (dl_list_empty(&survey_results->survey_list))
  8343. return;
  8344. dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
  8345. struct freq_survey, list) {
  8346. dl_list_del(&survey->list);
  8347. os_free(survey);
  8348. }
  8349. }
  8350. static void add_survey(struct nlattr **sinfo, u32 ifidx,
  8351. struct dl_list *survey_list)
  8352. {
  8353. struct freq_survey *survey;
  8354. survey = os_zalloc(sizeof(struct freq_survey));
  8355. if (!survey)
  8356. return;
  8357. survey->ifidx = ifidx;
  8358. survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
  8359. survey->filled = 0;
  8360. if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
  8361. survey->nf = (int8_t)
  8362. nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  8363. survey->filled |= SURVEY_HAS_NF;
  8364. }
  8365. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
  8366. survey->channel_time =
  8367. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
  8368. survey->filled |= SURVEY_HAS_CHAN_TIME;
  8369. }
  8370. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
  8371. survey->channel_time_busy =
  8372. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
  8373. survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
  8374. }
  8375. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
  8376. survey->channel_time_rx =
  8377. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
  8378. survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
  8379. }
  8380. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
  8381. survey->channel_time_tx =
  8382. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
  8383. survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
  8384. }
  8385. wpa_printf(MSG_DEBUG, "nl80211: Freq survey dump event (freq=%d MHz noise=%d channel_time=%ld busy_time=%ld tx_time=%ld rx_time=%ld filled=%04x)",
  8386. survey->freq,
  8387. survey->nf,
  8388. (unsigned long int) survey->channel_time,
  8389. (unsigned long int) survey->channel_time_busy,
  8390. (unsigned long int) survey->channel_time_tx,
  8391. (unsigned long int) survey->channel_time_rx,
  8392. survey->filled);
  8393. dl_list_add_tail(survey_list, &survey->list);
  8394. }
  8395. static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
  8396. unsigned int freq_filter)
  8397. {
  8398. if (!freq_filter)
  8399. return 1;
  8400. return freq_filter == surveyed_freq;
  8401. }
  8402. static int survey_handler(struct nl_msg *msg, void *arg)
  8403. {
  8404. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  8405. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  8406. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  8407. struct survey_results *survey_results;
  8408. u32 surveyed_freq = 0;
  8409. u32 ifidx;
  8410. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  8411. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  8412. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  8413. };
  8414. survey_results = (struct survey_results *) arg;
  8415. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  8416. genlmsg_attrlen(gnlh, 0), NULL);
  8417. ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
  8418. if (!tb[NL80211_ATTR_SURVEY_INFO])
  8419. return NL_SKIP;
  8420. if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  8421. tb[NL80211_ATTR_SURVEY_INFO],
  8422. survey_policy))
  8423. return NL_SKIP;
  8424. if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
  8425. wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
  8426. return NL_SKIP;
  8427. }
  8428. surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
  8429. if (!check_survey_ok(sinfo, surveyed_freq,
  8430. survey_results->freq_filter))
  8431. return NL_SKIP;
  8432. if (survey_results->freq_filter &&
  8433. survey_results->freq_filter != surveyed_freq) {
  8434. wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
  8435. surveyed_freq);
  8436. return NL_SKIP;
  8437. }
  8438. add_survey(sinfo, ifidx, &survey_results->survey_list);
  8439. return NL_SKIP;
  8440. }
  8441. static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
  8442. {
  8443. struct i802_bss *bss = priv;
  8444. struct wpa_driver_nl80211_data *drv = bss->drv;
  8445. struct nl_msg *msg;
  8446. int err = -ENOBUFS;
  8447. union wpa_event_data data;
  8448. struct survey_results *survey_results;
  8449. os_memset(&data, 0, sizeof(data));
  8450. survey_results = &data.survey_results;
  8451. dl_list_init(&survey_results->survey_list);
  8452. msg = nlmsg_alloc();
  8453. if (!msg)
  8454. goto nla_put_failure;
  8455. nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
  8456. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  8457. if (freq)
  8458. data.survey_results.freq_filter = freq;
  8459. do {
  8460. wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
  8461. err = send_and_recv_msgs(drv, msg, survey_handler,
  8462. survey_results);
  8463. } while (err > 0);
  8464. if (err) {
  8465. wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
  8466. goto out_clean;
  8467. }
  8468. wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
  8469. out_clean:
  8470. clean_survey_results(survey_results);
  8471. nla_put_failure:
  8472. return err;
  8473. }
  8474. static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
  8475. const u8 *replay_ctr)
  8476. {
  8477. struct i802_bss *bss = priv;
  8478. struct wpa_driver_nl80211_data *drv = bss->drv;
  8479. struct nlattr *replay_nested;
  8480. struct nl_msg *msg;
  8481. msg = nlmsg_alloc();
  8482. if (!msg)
  8483. return;
  8484. nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
  8485. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  8486. replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
  8487. if (!replay_nested)
  8488. goto nla_put_failure;
  8489. NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
  8490. NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
  8491. NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
  8492. replay_ctr);
  8493. nla_nest_end(msg, replay_nested);
  8494. send_and_recv_msgs(drv, msg, NULL, NULL);
  8495. return;
  8496. nla_put_failure:
  8497. nlmsg_free(msg);
  8498. }
  8499. static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
  8500. const u8 *addr, int qos)
  8501. {
  8502. /* send data frame to poll STA and check whether
  8503. * this frame is ACKed */
  8504. struct {
  8505. struct ieee80211_hdr hdr;
  8506. u16 qos_ctl;
  8507. } STRUCT_PACKED nulldata;
  8508. size_t size;
  8509. /* Send data frame to poll STA and check whether this frame is ACKed */
  8510. os_memset(&nulldata, 0, sizeof(nulldata));
  8511. if (qos) {
  8512. nulldata.hdr.frame_control =
  8513. IEEE80211_FC(WLAN_FC_TYPE_DATA,
  8514. WLAN_FC_STYPE_QOS_NULL);
  8515. size = sizeof(nulldata);
  8516. } else {
  8517. nulldata.hdr.frame_control =
  8518. IEEE80211_FC(WLAN_FC_TYPE_DATA,
  8519. WLAN_FC_STYPE_NULLFUNC);
  8520. size = sizeof(struct ieee80211_hdr);
  8521. }
  8522. nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
  8523. os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  8524. os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  8525. os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  8526. if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
  8527. 0, 0) < 0)
  8528. wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
  8529. "send poll frame");
  8530. }
  8531. static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
  8532. int qos)
  8533. {
  8534. struct i802_bss *bss = priv;
  8535. struct wpa_driver_nl80211_data *drv = bss->drv;
  8536. struct nl_msg *msg;
  8537. if (!drv->poll_command_supported) {
  8538. nl80211_send_null_frame(bss, own_addr, addr, qos);
  8539. return;
  8540. }
  8541. msg = nlmsg_alloc();
  8542. if (!msg)
  8543. return;
  8544. nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
  8545. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  8546. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  8547. send_and_recv_msgs(drv, msg, NULL, NULL);
  8548. return;
  8549. nla_put_failure:
  8550. nlmsg_free(msg);
  8551. }
  8552. static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
  8553. {
  8554. struct nl_msg *msg;
  8555. msg = nlmsg_alloc();
  8556. if (!msg)
  8557. return -ENOMEM;
  8558. nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
  8559. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  8560. NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
  8561. enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
  8562. return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  8563. nla_put_failure:
  8564. nlmsg_free(msg);
  8565. return -ENOBUFS;
  8566. }
  8567. static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
  8568. int ctwindow)
  8569. {
  8570. struct i802_bss *bss = priv;
  8571. wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
  8572. "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
  8573. if (opp_ps != -1 || ctwindow != -1)
  8574. return -1; /* Not yet supported */
  8575. if (legacy_ps == -1)
  8576. return 0;
  8577. if (legacy_ps != 0 && legacy_ps != 1)
  8578. return -1; /* Not yet supported */
  8579. return nl80211_set_power_save(bss, legacy_ps);
  8580. }
  8581. static int nl80211_start_radar_detection(void *priv, int freq)
  8582. {
  8583. struct i802_bss *bss = priv;
  8584. struct wpa_driver_nl80211_data *drv = bss->drv;
  8585. struct nl_msg *msg;
  8586. int ret;
  8587. wpa_printf(MSG_DEBUG, "nl80211: Start radar detection (CAC)");
  8588. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
  8589. wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
  8590. "detection");
  8591. return -1;
  8592. }
  8593. msg = nlmsg_alloc();
  8594. if (!msg)
  8595. return -1;
  8596. nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_RADAR_DETECT);
  8597. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  8598. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  8599. /* only HT20 is supported at this point */
  8600. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, NL80211_CHAN_HT20);
  8601. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  8602. if (ret == 0)
  8603. return 0;
  8604. wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
  8605. "%d (%s)", ret, strerror(-ret));
  8606. nla_put_failure:
  8607. return -1;
  8608. }
  8609. #ifdef CONFIG_TDLS
  8610. static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
  8611. u8 dialog_token, u16 status_code,
  8612. const u8 *buf, size_t len)
  8613. {
  8614. struct i802_bss *bss = priv;
  8615. struct wpa_driver_nl80211_data *drv = bss->drv;
  8616. struct nl_msg *msg;
  8617. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
  8618. return -EOPNOTSUPP;
  8619. if (!dst)
  8620. return -EINVAL;
  8621. msg = nlmsg_alloc();
  8622. if (!msg)
  8623. return -ENOMEM;
  8624. nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
  8625. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  8626. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
  8627. NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
  8628. NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
  8629. NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
  8630. NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
  8631. return send_and_recv_msgs(drv, msg, NULL, NULL);
  8632. nla_put_failure:
  8633. nlmsg_free(msg);
  8634. return -ENOBUFS;
  8635. }
  8636. static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
  8637. {
  8638. struct i802_bss *bss = priv;
  8639. struct wpa_driver_nl80211_data *drv = bss->drv;
  8640. struct nl_msg *msg;
  8641. enum nl80211_tdls_operation nl80211_oper;
  8642. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
  8643. return -EOPNOTSUPP;
  8644. switch (oper) {
  8645. case TDLS_DISCOVERY_REQ:
  8646. nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
  8647. break;
  8648. case TDLS_SETUP:
  8649. nl80211_oper = NL80211_TDLS_SETUP;
  8650. break;
  8651. case TDLS_TEARDOWN:
  8652. nl80211_oper = NL80211_TDLS_TEARDOWN;
  8653. break;
  8654. case TDLS_ENABLE_LINK:
  8655. nl80211_oper = NL80211_TDLS_ENABLE_LINK;
  8656. break;
  8657. case TDLS_DISABLE_LINK:
  8658. nl80211_oper = NL80211_TDLS_DISABLE_LINK;
  8659. break;
  8660. case TDLS_ENABLE:
  8661. return 0;
  8662. case TDLS_DISABLE:
  8663. return 0;
  8664. default:
  8665. return -EINVAL;
  8666. }
  8667. msg = nlmsg_alloc();
  8668. if (!msg)
  8669. return -ENOMEM;
  8670. nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
  8671. NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
  8672. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  8673. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
  8674. return send_and_recv_msgs(drv, msg, NULL, NULL);
  8675. nla_put_failure:
  8676. nlmsg_free(msg);
  8677. return -ENOBUFS;
  8678. }
  8679. #endif /* CONFIG TDLS */
  8680. #ifdef ANDROID
  8681. typedef struct android_wifi_priv_cmd {
  8682. char *buf;
  8683. int used_len;
  8684. int total_len;
  8685. } android_wifi_priv_cmd;
  8686. static int drv_errors = 0;
  8687. static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
  8688. {
  8689. drv_errors++;
  8690. if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
  8691. drv_errors = 0;
  8692. wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
  8693. }
  8694. }
  8695. static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
  8696. {
  8697. struct wpa_driver_nl80211_data *drv = bss->drv;
  8698. struct ifreq ifr;
  8699. android_wifi_priv_cmd priv_cmd;
  8700. char buf[MAX_DRV_CMD_SIZE];
  8701. int ret;
  8702. os_memset(&ifr, 0, sizeof(ifr));
  8703. os_memset(&priv_cmd, 0, sizeof(priv_cmd));
  8704. os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
  8705. os_memset(buf, 0, sizeof(buf));
  8706. os_strlcpy(buf, cmd, sizeof(buf));
  8707. priv_cmd.buf = buf;
  8708. priv_cmd.used_len = sizeof(buf);
  8709. priv_cmd.total_len = sizeof(buf);
  8710. ifr.ifr_data = &priv_cmd;
  8711. ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
  8712. if (ret < 0) {
  8713. wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
  8714. __func__);
  8715. wpa_driver_send_hang_msg(drv);
  8716. return ret;
  8717. }
  8718. drv_errors = 0;
  8719. return 0;
  8720. }
  8721. static int android_pno_start(struct i802_bss *bss,
  8722. struct wpa_driver_scan_params *params)
  8723. {
  8724. struct wpa_driver_nl80211_data *drv = bss->drv;
  8725. struct ifreq ifr;
  8726. android_wifi_priv_cmd priv_cmd;
  8727. int ret = 0, i = 0, bp;
  8728. char buf[WEXT_PNO_MAX_COMMAND_SIZE];
  8729. bp = WEXT_PNOSETUP_HEADER_SIZE;
  8730. os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
  8731. buf[bp++] = WEXT_PNO_TLV_PREFIX;
  8732. buf[bp++] = WEXT_PNO_TLV_VERSION;
  8733. buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
  8734. buf[bp++] = WEXT_PNO_TLV_RESERVED;
  8735. while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
  8736. /* Check that there is enough space needed for 1 more SSID, the
  8737. * other sections and null termination */
  8738. if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
  8739. WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
  8740. break;
  8741. wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
  8742. params->ssids[i].ssid,
  8743. params->ssids[i].ssid_len);
  8744. buf[bp++] = WEXT_PNO_SSID_SECTION;
  8745. buf[bp++] = params->ssids[i].ssid_len;
  8746. os_memcpy(&buf[bp], params->ssids[i].ssid,
  8747. params->ssids[i].ssid_len);
  8748. bp += params->ssids[i].ssid_len;
  8749. i++;
  8750. }
  8751. buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
  8752. os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
  8753. WEXT_PNO_SCAN_INTERVAL);
  8754. bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
  8755. buf[bp++] = WEXT_PNO_REPEAT_SECTION;
  8756. os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
  8757. WEXT_PNO_REPEAT);
  8758. bp += WEXT_PNO_REPEAT_LENGTH;
  8759. buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
  8760. os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
  8761. WEXT_PNO_MAX_REPEAT);
  8762. bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
  8763. memset(&ifr, 0, sizeof(ifr));
  8764. memset(&priv_cmd, 0, sizeof(priv_cmd));
  8765. os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
  8766. priv_cmd.buf = buf;
  8767. priv_cmd.used_len = bp;
  8768. priv_cmd.total_len = bp;
  8769. ifr.ifr_data = &priv_cmd;
  8770. ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
  8771. if (ret < 0) {
  8772. wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
  8773. ret);
  8774. wpa_driver_send_hang_msg(drv);
  8775. return ret;
  8776. }
  8777. drv_errors = 0;
  8778. return android_priv_cmd(bss, "PNOFORCE 1");
  8779. }
  8780. static int android_pno_stop(struct i802_bss *bss)
  8781. {
  8782. return android_priv_cmd(bss, "PNOFORCE 0");
  8783. }
  8784. #endif /* ANDROID */
  8785. static int driver_nl80211_set_key(const char *ifname, void *priv,
  8786. enum wpa_alg alg, const u8 *addr,
  8787. int key_idx, int set_tx,
  8788. const u8 *seq, size_t seq_len,
  8789. const u8 *key, size_t key_len)
  8790. {
  8791. struct i802_bss *bss = priv;
  8792. return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
  8793. set_tx, seq, seq_len, key, key_len);
  8794. }
  8795. static int driver_nl80211_scan2(void *priv,
  8796. struct wpa_driver_scan_params *params)
  8797. {
  8798. struct i802_bss *bss = priv;
  8799. return wpa_driver_nl80211_scan(bss, params);
  8800. }
  8801. static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
  8802. int reason_code)
  8803. {
  8804. struct i802_bss *bss = priv;
  8805. return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
  8806. }
  8807. static int driver_nl80211_authenticate(void *priv,
  8808. struct wpa_driver_auth_params *params)
  8809. {
  8810. struct i802_bss *bss = priv;
  8811. return wpa_driver_nl80211_authenticate(bss, params);
  8812. }
  8813. static void driver_nl80211_deinit(void *priv)
  8814. {
  8815. struct i802_bss *bss = priv;
  8816. wpa_driver_nl80211_deinit(bss);
  8817. }
  8818. static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
  8819. const char *ifname)
  8820. {
  8821. struct i802_bss *bss = priv;
  8822. return wpa_driver_nl80211_if_remove(bss, type, ifname);
  8823. }
  8824. static int driver_nl80211_send_mlme(void *priv, const u8 *data,
  8825. size_t data_len, int noack)
  8826. {
  8827. struct i802_bss *bss = priv;
  8828. return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
  8829. 0, 0, 0, 0);
  8830. }
  8831. static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
  8832. {
  8833. struct i802_bss *bss = priv;
  8834. return wpa_driver_nl80211_sta_remove(bss, addr);
  8835. }
  8836. #if defined(HOSTAPD) || defined(CONFIG_AP)
  8837. static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
  8838. const char *ifname, int vlan_id)
  8839. {
  8840. struct i802_bss *bss = priv;
  8841. return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
  8842. }
  8843. #endif /* HOSTAPD || CONFIG_AP */
  8844. static int driver_nl80211_read_sta_data(void *priv,
  8845. struct hostap_sta_driver_data *data,
  8846. const u8 *addr)
  8847. {
  8848. struct i802_bss *bss = priv;
  8849. return i802_read_sta_data(bss, data, addr);
  8850. }
  8851. static int driver_nl80211_send_action(void *priv, unsigned int freq,
  8852. unsigned int wait_time,
  8853. const u8 *dst, const u8 *src,
  8854. const u8 *bssid,
  8855. const u8 *data, size_t data_len,
  8856. int no_cck)
  8857. {
  8858. struct i802_bss *bss = priv;
  8859. return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
  8860. bssid, data, data_len, no_cck);
  8861. }
  8862. static int driver_nl80211_probe_req_report(void *priv, int report)
  8863. {
  8864. struct i802_bss *bss = priv;
  8865. return wpa_driver_nl80211_probe_req_report(bss, report);
  8866. }
  8867. static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
  8868. const u8 *ies, size_t ies_len)
  8869. {
  8870. int ret;
  8871. struct nl_msg *msg;
  8872. struct i802_bss *bss = priv;
  8873. struct wpa_driver_nl80211_data *drv = bss->drv;
  8874. u16 mdid = WPA_GET_LE16(md);
  8875. msg = nlmsg_alloc();
  8876. if (!msg)
  8877. return -ENOMEM;
  8878. wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
  8879. nl80211_cmd(drv, msg, 0, NL80211_CMD_UPDATE_FT_IES);
  8880. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  8881. NLA_PUT(msg, NL80211_ATTR_IE, ies_len, ies);
  8882. NLA_PUT_U16(msg, NL80211_ATTR_MDID, mdid);
  8883. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  8884. if (ret) {
  8885. wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
  8886. "err=%d (%s)", ret, strerror(-ret));
  8887. }
  8888. return ret;
  8889. nla_put_failure:
  8890. nlmsg_free(msg);
  8891. return -ENOBUFS;
  8892. }
  8893. const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
  8894. {
  8895. struct i802_bss *bss = priv;
  8896. struct wpa_driver_nl80211_data *drv = bss->drv;
  8897. if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
  8898. return NULL;
  8899. return bss->addr;
  8900. }
  8901. const struct wpa_driver_ops wpa_driver_nl80211_ops = {
  8902. .name = "nl80211",
  8903. .desc = "Linux nl80211/cfg80211",
  8904. .get_bssid = wpa_driver_nl80211_get_bssid,
  8905. .get_ssid = wpa_driver_nl80211_get_ssid,
  8906. .set_key = driver_nl80211_set_key,
  8907. .scan2 = driver_nl80211_scan2,
  8908. .sched_scan = wpa_driver_nl80211_sched_scan,
  8909. .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
  8910. .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
  8911. .deauthenticate = driver_nl80211_deauthenticate,
  8912. .authenticate = driver_nl80211_authenticate,
  8913. .associate = wpa_driver_nl80211_associate,
  8914. .global_init = nl80211_global_init,
  8915. .global_deinit = nl80211_global_deinit,
  8916. .init2 = wpa_driver_nl80211_init,
  8917. .deinit = driver_nl80211_deinit,
  8918. .get_capa = wpa_driver_nl80211_get_capa,
  8919. .set_operstate = wpa_driver_nl80211_set_operstate,
  8920. .set_supp_port = wpa_driver_nl80211_set_supp_port,
  8921. .set_country = wpa_driver_nl80211_set_country,
  8922. .set_ap = wpa_driver_nl80211_set_ap,
  8923. .set_acl = wpa_driver_nl80211_set_acl,
  8924. .if_add = wpa_driver_nl80211_if_add,
  8925. .if_remove = driver_nl80211_if_remove,
  8926. .send_mlme = driver_nl80211_send_mlme,
  8927. .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
  8928. .sta_add = wpa_driver_nl80211_sta_add,
  8929. .sta_remove = driver_nl80211_sta_remove,
  8930. .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
  8931. .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
  8932. #ifdef HOSTAPD
  8933. .hapd_init = i802_init,
  8934. .hapd_deinit = i802_deinit,
  8935. .set_wds_sta = i802_set_wds_sta,
  8936. #endif /* HOSTAPD */
  8937. #if defined(HOSTAPD) || defined(CONFIG_AP)
  8938. .get_seqnum = i802_get_seqnum,
  8939. .flush = i802_flush,
  8940. .get_inact_sec = i802_get_inact_sec,
  8941. .sta_clear_stats = i802_sta_clear_stats,
  8942. .set_rts = i802_set_rts,
  8943. .set_frag = i802_set_frag,
  8944. .set_tx_queue_params = i802_set_tx_queue_params,
  8945. .set_sta_vlan = driver_nl80211_set_sta_vlan,
  8946. .sta_deauth = i802_sta_deauth,
  8947. .sta_disassoc = i802_sta_disassoc,
  8948. #endif /* HOSTAPD || CONFIG_AP */
  8949. .read_sta_data = driver_nl80211_read_sta_data,
  8950. .set_freq = i802_set_freq,
  8951. .send_action = driver_nl80211_send_action,
  8952. .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
  8953. .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
  8954. .cancel_remain_on_channel =
  8955. wpa_driver_nl80211_cancel_remain_on_channel,
  8956. .probe_req_report = driver_nl80211_probe_req_report,
  8957. .deinit_ap = wpa_driver_nl80211_deinit_ap,
  8958. .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
  8959. .resume = wpa_driver_nl80211_resume,
  8960. .send_ft_action = nl80211_send_ft_action,
  8961. .signal_monitor = nl80211_signal_monitor,
  8962. .signal_poll = nl80211_signal_poll,
  8963. .send_frame = nl80211_send_frame,
  8964. .shared_freq = wpa_driver_nl80211_shared_freq,
  8965. .set_param = nl80211_set_param,
  8966. .get_radio_name = nl80211_get_radio_name,
  8967. .add_pmkid = nl80211_add_pmkid,
  8968. .remove_pmkid = nl80211_remove_pmkid,
  8969. .flush_pmkid = nl80211_flush_pmkid,
  8970. .set_rekey_info = nl80211_set_rekey_info,
  8971. .poll_client = nl80211_poll_client,
  8972. .set_p2p_powersave = nl80211_set_p2p_powersave,
  8973. .start_dfs_cac = nl80211_start_radar_detection,
  8974. .stop_ap = wpa_driver_nl80211_stop_ap,
  8975. #ifdef CONFIG_TDLS
  8976. .send_tdls_mgmt = nl80211_send_tdls_mgmt,
  8977. .tdls_oper = nl80211_tdls_oper,
  8978. #endif /* CONFIG_TDLS */
  8979. .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
  8980. .get_mac_addr = wpa_driver_nl80211_get_macaddr,
  8981. .get_survey = wpa_driver_nl80211_get_survey,
  8982. };