driver_nl80211.c 263 KB

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