driver_nl80211.c 177 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849
  1. /*
  2. * Driver interaction with Linux nl80211/cfg80211
  3. * Copyright (c) 2002-2010, 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 program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * Alternatively, this software may be distributed under the terms of BSD
  14. * license.
  15. *
  16. * See README and COPYING for more details.
  17. */
  18. #include "includes.h"
  19. #include <sys/ioctl.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <net/if.h>
  24. #include <netlink/genl/genl.h>
  25. #include <netlink/genl/family.h>
  26. #include <netlink/genl/ctrl.h>
  27. #include <linux/rtnetlink.h>
  28. #include <netpacket/packet.h>
  29. #include <linux/filter.h>
  30. #include "nl80211_copy.h"
  31. #include "common.h"
  32. #include "eloop.h"
  33. #include "utils/list.h"
  34. #include "common/ieee802_11_defs.h"
  35. #include "netlink.h"
  36. #include "linux_ioctl.h"
  37. #include "radiotap.h"
  38. #include "radiotap_iter.h"
  39. #include "rfkill.h"
  40. #include "driver.h"
  41. #ifdef CONFIG_LIBNL20
  42. /* libnl 2.0 compatibility code */
  43. #define nl_handle nl_sock
  44. #define nl80211_handle_alloc nl_socket_alloc_cb
  45. #define nl80211_handle_destroy nl_socket_free
  46. #else
  47. /*
  48. * libnl 1.1 has a bug, it tries to allocate socket numbers densely
  49. * but when you free a socket again it will mess up its bitmap and
  50. * and use the wrong number the next time it needs a socket ID.
  51. * Therefore, we wrap the handle alloc/destroy and add our own pid
  52. * accounting.
  53. */
  54. static uint32_t port_bitmap[32] = { 0 };
  55. static struct nl_handle *nl80211_handle_alloc(void *cb)
  56. {
  57. struct nl_handle *handle;
  58. uint32_t pid = getpid() & 0x3FFFFF;
  59. int i;
  60. handle = nl_handle_alloc_cb(cb);
  61. for (i = 0; i < 1024; i++) {
  62. if (port_bitmap[i / 32] & (1 << (i % 32)))
  63. continue;
  64. port_bitmap[i / 32] |= 1 << (i % 32);
  65. pid += i << 22;
  66. break;
  67. }
  68. nl_socket_set_local_port(handle, pid);
  69. return handle;
  70. }
  71. static void nl80211_handle_destroy(struct nl_handle *handle)
  72. {
  73. uint32_t port = nl_socket_get_local_port(handle);
  74. port >>= 22;
  75. port_bitmap[port / 32] &= ~(1 << (port % 32));
  76. nl_handle_destroy(handle);
  77. }
  78. #endif /* CONFIG_LIBNL20 */
  79. #ifndef IFF_LOWER_UP
  80. #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
  81. #endif
  82. #ifndef IFF_DORMANT
  83. #define IFF_DORMANT 0x20000 /* driver signals dormant */
  84. #endif
  85. #ifndef IF_OPER_DORMANT
  86. #define IF_OPER_DORMANT 5
  87. #endif
  88. #ifndef IF_OPER_UP
  89. #define IF_OPER_UP 6
  90. #endif
  91. struct nl80211_global {
  92. struct dl_list interfaces;
  93. int if_add_ifindex;
  94. };
  95. struct i802_bss {
  96. struct wpa_driver_nl80211_data *drv;
  97. struct i802_bss *next;
  98. int ifindex;
  99. char ifname[IFNAMSIZ + 1];
  100. char brname[IFNAMSIZ];
  101. unsigned int beacon_set:1;
  102. unsigned int added_if_into_bridge:1;
  103. unsigned int added_bridge:1;
  104. };
  105. struct wpa_driver_nl80211_data {
  106. struct nl80211_global *global;
  107. struct dl_list list;
  108. u8 addr[ETH_ALEN];
  109. char phyname[32];
  110. void *ctx;
  111. struct netlink_data *netlink;
  112. int ioctl_sock; /* socket for ioctl() use */
  113. int ifindex;
  114. int if_removed;
  115. int if_disabled;
  116. int ignore_if_down_event;
  117. struct rfkill_data *rfkill;
  118. struct wpa_driver_capa capa;
  119. int has_capability;
  120. int operstate;
  121. int scan_complete_events;
  122. struct nl_handle *nl_handle;
  123. struct nl_handle *nl_handle_event;
  124. struct nl_handle *nl_handle_preq;
  125. struct nl_cache *nl_cache;
  126. struct nl_cache *nl_cache_event;
  127. struct nl_cache *nl_cache_preq;
  128. struct nl_cb *nl_cb;
  129. struct genl_family *nl80211;
  130. u8 auth_bssid[ETH_ALEN];
  131. u8 bssid[ETH_ALEN];
  132. int associated;
  133. u8 ssid[32];
  134. size_t ssid_len;
  135. int nlmode;
  136. int ap_scan_as_station;
  137. unsigned int assoc_freq;
  138. int monitor_sock;
  139. int monitor_ifidx;
  140. int no_monitor_iface_capab;
  141. int disable_11b_rates;
  142. unsigned int pending_remain_on_chan:1;
  143. u64 remain_on_chan_cookie;
  144. u64 send_action_cookie;
  145. unsigned int last_mgmt_freq;
  146. struct wpa_driver_scan_filter *filter_ssids;
  147. size_t num_filter_ssids;
  148. struct i802_bss first_bss;
  149. #ifdef HOSTAPD
  150. int eapol_sock; /* socket for EAPOL frames */
  151. int default_if_indices[16];
  152. int *if_indices;
  153. int num_if_indices;
  154. int last_freq;
  155. int last_freq_ht;
  156. #endif /* HOSTAPD */
  157. };
  158. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
  159. void *timeout_ctx);
  160. static int wpa_driver_nl80211_set_mode(void *priv, int mode);
  161. static int
  162. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
  163. static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  164. const u8 *addr, int cmd, u16 reason_code,
  165. int local_state_change);
  166. static void nl80211_remove_monitor_interface(
  167. struct wpa_driver_nl80211_data *drv);
  168. static int nl80211_send_frame_cmd(struct wpa_driver_nl80211_data *drv,
  169. unsigned int freq, unsigned int wait,
  170. const u8 *buf, size_t buf_len, u64 *cookie);
  171. static int wpa_driver_nl80211_probe_req_report(void *priv, int report);
  172. #ifdef HOSTAPD
  173. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  174. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  175. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  176. static int wpa_driver_nl80211_if_remove(void *priv,
  177. enum wpa_driver_if_type type,
  178. const char *ifname);
  179. #else /* HOSTAPD */
  180. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  181. {
  182. return 0;
  183. }
  184. #endif /* HOSTAPD */
  185. static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
  186. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  187. int ifindex, int disabled);
  188. static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
  189. struct nl80211_bss_info_arg {
  190. struct wpa_driver_nl80211_data *drv;
  191. struct wpa_scan_results *res;
  192. unsigned int assoc_freq;
  193. };
  194. static int bss_info_handler(struct nl_msg *msg, void *arg);
  195. /* nl80211 code */
  196. static int ack_handler(struct nl_msg *msg, void *arg)
  197. {
  198. int *err = arg;
  199. *err = 0;
  200. return NL_STOP;
  201. }
  202. static int finish_handler(struct nl_msg *msg, void *arg)
  203. {
  204. int *ret = arg;
  205. *ret = 0;
  206. return NL_SKIP;
  207. }
  208. static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
  209. void *arg)
  210. {
  211. int *ret = arg;
  212. *ret = err->error;
  213. return NL_SKIP;
  214. }
  215. static int no_seq_check(struct nl_msg *msg, void *arg)
  216. {
  217. return NL_OK;
  218. }
  219. static int send_and_recv(struct wpa_driver_nl80211_data *drv,
  220. struct nl_handle *nl_handle, struct nl_msg *msg,
  221. int (*valid_handler)(struct nl_msg *, void *),
  222. void *valid_data)
  223. {
  224. struct nl_cb *cb;
  225. int err = -ENOMEM;
  226. cb = nl_cb_clone(drv->nl_cb);
  227. if (!cb)
  228. goto out;
  229. err = nl_send_auto_complete(nl_handle, msg);
  230. if (err < 0)
  231. goto out;
  232. err = 1;
  233. nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
  234. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
  235. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
  236. if (valid_handler)
  237. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
  238. valid_handler, valid_data);
  239. while (err > 0)
  240. nl_recvmsgs(nl_handle, cb);
  241. out:
  242. nl_cb_put(cb);
  243. nlmsg_free(msg);
  244. return err;
  245. }
  246. static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
  247. struct nl_msg *msg,
  248. int (*valid_handler)(struct nl_msg *, void *),
  249. void *valid_data)
  250. {
  251. return send_and_recv(drv, drv->nl_handle, msg, valid_handler,
  252. valid_data);
  253. }
  254. struct family_data {
  255. const char *group;
  256. int id;
  257. };
  258. static int family_handler(struct nl_msg *msg, void *arg)
  259. {
  260. struct family_data *res = arg;
  261. struct nlattr *tb[CTRL_ATTR_MAX + 1];
  262. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  263. struct nlattr *mcgrp;
  264. int i;
  265. nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  266. genlmsg_attrlen(gnlh, 0), NULL);
  267. if (!tb[CTRL_ATTR_MCAST_GROUPS])
  268. return NL_SKIP;
  269. nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
  270. struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
  271. nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
  272. nla_len(mcgrp), NULL);
  273. if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
  274. !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
  275. os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
  276. res->group,
  277. nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
  278. continue;
  279. res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
  280. break;
  281. };
  282. return NL_SKIP;
  283. }
  284. static int nl_get_multicast_id(struct wpa_driver_nl80211_data *drv,
  285. const char *family, const char *group)
  286. {
  287. struct nl_msg *msg;
  288. int ret = -1;
  289. struct family_data res = { group, -ENOENT };
  290. msg = nlmsg_alloc();
  291. if (!msg)
  292. return -ENOMEM;
  293. genlmsg_put(msg, 0, 0, genl_ctrl_resolve(drv->nl_handle, "nlctrl"),
  294. 0, 0, CTRL_CMD_GETFAMILY, 0);
  295. NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
  296. ret = send_and_recv_msgs(drv, msg, family_handler, &res);
  297. msg = NULL;
  298. if (ret == 0)
  299. ret = res.id;
  300. nla_put_failure:
  301. nlmsg_free(msg);
  302. return ret;
  303. }
  304. static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
  305. {
  306. struct i802_bss *bss = priv;
  307. struct wpa_driver_nl80211_data *drv = bss->drv;
  308. if (!drv->associated)
  309. return -1;
  310. os_memcpy(bssid, drv->bssid, ETH_ALEN);
  311. return 0;
  312. }
  313. static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
  314. {
  315. struct i802_bss *bss = priv;
  316. struct wpa_driver_nl80211_data *drv = bss->drv;
  317. if (!drv->associated)
  318. return -1;
  319. os_memcpy(ssid, drv->ssid, drv->ssid_len);
  320. return drv->ssid_len;
  321. }
  322. static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
  323. char *buf, size_t len, int del)
  324. {
  325. union wpa_event_data event;
  326. os_memset(&event, 0, sizeof(event));
  327. if (len > sizeof(event.interface_status.ifname))
  328. len = sizeof(event.interface_status.ifname) - 1;
  329. os_memcpy(event.interface_status.ifname, buf, len);
  330. event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
  331. EVENT_INTERFACE_ADDED;
  332. wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
  333. del ? "DEL" : "NEW",
  334. event.interface_status.ifname,
  335. del ? "removed" : "added");
  336. if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
  337. if (del)
  338. drv->if_removed = 1;
  339. else
  340. drv->if_removed = 0;
  341. }
  342. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
  343. }
  344. static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
  345. u8 *buf, size_t len)
  346. {
  347. int attrlen, rta_len;
  348. struct rtattr *attr;
  349. attrlen = len;
  350. attr = (struct rtattr *) buf;
  351. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  352. while (RTA_OK(attr, attrlen)) {
  353. if (attr->rta_type == IFLA_IFNAME) {
  354. if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
  355. == 0)
  356. return 1;
  357. else
  358. break;
  359. }
  360. attr = RTA_NEXT(attr, attrlen);
  361. }
  362. return 0;
  363. }
  364. static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
  365. int ifindex, u8 *buf, size_t len)
  366. {
  367. if (drv->ifindex == ifindex)
  368. return 1;
  369. if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
  370. drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
  371. wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
  372. "interface");
  373. wpa_driver_nl80211_finish_drv_init(drv);
  374. return 1;
  375. }
  376. return 0;
  377. }
  378. static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
  379. struct ifinfomsg *ifi,
  380. u8 *buf, size_t len)
  381. {
  382. struct wpa_driver_nl80211_data *drv = ctx;
  383. int attrlen, rta_len;
  384. struct rtattr *attr;
  385. u32 brid = 0;
  386. if (!wpa_driver_nl80211_own_ifindex(drv, ifi->ifi_index, buf, len) &&
  387. !have_ifidx(drv, ifi->ifi_index)) {
  388. wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
  389. "ifindex %d", ifi->ifi_index);
  390. return;
  391. }
  392. wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
  393. "(%s%s%s%s)",
  394. drv->operstate, ifi->ifi_flags,
  395. (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
  396. (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
  397. (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
  398. (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
  399. if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
  400. wpa_printf(MSG_DEBUG, "nl80211: Interface down");
  401. if (drv->ignore_if_down_event) {
  402. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
  403. "event generated by mode change");
  404. drv->ignore_if_down_event = 0;
  405. } else {
  406. drv->if_disabled = 1;
  407. wpa_supplicant_event(drv->ctx,
  408. EVENT_INTERFACE_DISABLED, NULL);
  409. }
  410. }
  411. if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
  412. wpa_printf(MSG_DEBUG, "nl80211: Interface up");
  413. drv->if_disabled = 0;
  414. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
  415. }
  416. /*
  417. * Some drivers send the association event before the operup event--in
  418. * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
  419. * fails. This will hit us when wpa_supplicant does not need to do
  420. * IEEE 802.1X authentication
  421. */
  422. if (drv->operstate == 1 &&
  423. (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
  424. !(ifi->ifi_flags & IFF_RUNNING))
  425. netlink_send_oper_ifla(drv->netlink, drv->ifindex,
  426. -1, IF_OPER_UP);
  427. attrlen = len;
  428. attr = (struct rtattr *) buf;
  429. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  430. while (RTA_OK(attr, attrlen)) {
  431. if (attr->rta_type == IFLA_IFNAME) {
  432. wpa_driver_nl80211_event_link(
  433. drv,
  434. ((char *) attr) + rta_len,
  435. attr->rta_len - rta_len, 0);
  436. } else if (attr->rta_type == IFLA_MASTER)
  437. brid = nla_get_u32((struct nlattr *) attr);
  438. attr = RTA_NEXT(attr, attrlen);
  439. }
  440. #ifdef HOSTAPD
  441. if (ifi->ifi_family == AF_BRIDGE && brid) {
  442. /* device has been added to bridge */
  443. char namebuf[IFNAMSIZ];
  444. if_indextoname(brid, namebuf);
  445. wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
  446. brid, namebuf);
  447. add_ifidx(drv, brid);
  448. }
  449. #endif /* HOSTAPD */
  450. }
  451. static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
  452. struct ifinfomsg *ifi,
  453. u8 *buf, size_t len)
  454. {
  455. struct wpa_driver_nl80211_data *drv = ctx;
  456. int attrlen, rta_len;
  457. struct rtattr *attr;
  458. u32 brid = 0;
  459. attrlen = len;
  460. attr = (struct rtattr *) buf;
  461. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  462. while (RTA_OK(attr, attrlen)) {
  463. if (attr->rta_type == IFLA_IFNAME) {
  464. wpa_driver_nl80211_event_link(
  465. drv,
  466. ((char *) attr) + rta_len,
  467. attr->rta_len - rta_len, 1);
  468. } else if (attr->rta_type == IFLA_MASTER)
  469. brid = nla_get_u32((struct nlattr *) attr);
  470. attr = RTA_NEXT(attr, attrlen);
  471. }
  472. #ifdef HOSTAPD
  473. if (ifi->ifi_family == AF_BRIDGE && brid) {
  474. /* device has been removed from bridge */
  475. char namebuf[IFNAMSIZ];
  476. if_indextoname(brid, namebuf);
  477. wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
  478. "%s", brid, namebuf);
  479. del_ifidx(drv, brid);
  480. }
  481. #endif /* HOSTAPD */
  482. }
  483. static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
  484. const u8 *frame, size_t len)
  485. {
  486. const struct ieee80211_mgmt *mgmt;
  487. union wpa_event_data event;
  488. mgmt = (const struct ieee80211_mgmt *) frame;
  489. if (len < 24 + sizeof(mgmt->u.auth)) {
  490. wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
  491. "frame");
  492. return;
  493. }
  494. os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
  495. os_memset(&event, 0, sizeof(event));
  496. os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
  497. event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
  498. event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
  499. if (len > 24 + sizeof(mgmt->u.auth)) {
  500. event.auth.ies = mgmt->u.auth.variable;
  501. event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
  502. }
  503. wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
  504. }
  505. static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
  506. {
  507. struct nl_msg *msg;
  508. int ret;
  509. struct nl80211_bss_info_arg arg;
  510. os_memset(&arg, 0, sizeof(arg));
  511. msg = nlmsg_alloc();
  512. if (!msg)
  513. goto nla_put_failure;
  514. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, NLM_F_DUMP,
  515. NL80211_CMD_GET_SCAN, 0);
  516. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  517. arg.drv = drv;
  518. ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
  519. msg = NULL;
  520. if (ret == 0) {
  521. wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
  522. "associated BSS from scan results: %u MHz",
  523. arg.assoc_freq);
  524. return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq;
  525. }
  526. wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
  527. "(%s)", ret, strerror(-ret));
  528. nla_put_failure:
  529. nlmsg_free(msg);
  530. return drv->assoc_freq;
  531. }
  532. static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
  533. const u8 *frame, size_t len)
  534. {
  535. const struct ieee80211_mgmt *mgmt;
  536. union wpa_event_data event;
  537. u16 status;
  538. mgmt = (const struct ieee80211_mgmt *) frame;
  539. if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
  540. wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
  541. "frame");
  542. return;
  543. }
  544. status = le_to_host16(mgmt->u.assoc_resp.status_code);
  545. if (status != WLAN_STATUS_SUCCESS) {
  546. os_memset(&event, 0, sizeof(event));
  547. event.assoc_reject.bssid = mgmt->bssid;
  548. if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
  549. event.assoc_reject.resp_ies =
  550. (u8 *) mgmt->u.assoc_resp.variable;
  551. event.assoc_reject.resp_ies_len =
  552. len - 24 - sizeof(mgmt->u.assoc_resp);
  553. }
  554. event.assoc_reject.status_code = status;
  555. wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
  556. return;
  557. }
  558. drv->associated = 1;
  559. os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
  560. os_memset(&event, 0, sizeof(event));
  561. if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
  562. event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
  563. event.assoc_info.resp_ies_len =
  564. len - 24 - sizeof(mgmt->u.assoc_resp);
  565. }
  566. event.assoc_info.freq = drv->assoc_freq;
  567. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
  568. }
  569. static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
  570. enum nl80211_commands cmd, struct nlattr *status,
  571. struct nlattr *addr, struct nlattr *req_ie,
  572. struct nlattr *resp_ie)
  573. {
  574. union wpa_event_data event;
  575. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  576. /*
  577. * Avoid reporting two association events that would confuse
  578. * the core code.
  579. */
  580. wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
  581. "when using userspace SME", cmd);
  582. return;
  583. }
  584. os_memset(&event, 0, sizeof(event));
  585. if (cmd == NL80211_CMD_CONNECT &&
  586. nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
  587. if (addr)
  588. event.assoc_reject.bssid = nla_data(addr);
  589. if (resp_ie) {
  590. event.assoc_reject.resp_ies = nla_data(resp_ie);
  591. event.assoc_reject.resp_ies_len = nla_len(resp_ie);
  592. }
  593. event.assoc_reject.status_code = nla_get_u16(status);
  594. wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
  595. return;
  596. }
  597. drv->associated = 1;
  598. if (addr)
  599. os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
  600. if (req_ie) {
  601. event.assoc_info.req_ies = nla_data(req_ie);
  602. event.assoc_info.req_ies_len = nla_len(req_ie);
  603. }
  604. if (resp_ie) {
  605. event.assoc_info.resp_ies = nla_data(resp_ie);
  606. event.assoc_info.resp_ies_len = nla_len(resp_ie);
  607. }
  608. event.assoc_info.freq = nl80211_get_assoc_freq(drv);
  609. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
  610. }
  611. static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
  612. enum nl80211_commands cmd, struct nlattr *addr)
  613. {
  614. union wpa_event_data event;
  615. enum wpa_event_type ev;
  616. if (nla_len(addr) != ETH_ALEN)
  617. return;
  618. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
  619. cmd, MAC2STR((u8 *) nla_data(addr)));
  620. if (cmd == NL80211_CMD_AUTHENTICATE)
  621. ev = EVENT_AUTH_TIMED_OUT;
  622. else if (cmd == NL80211_CMD_ASSOCIATE)
  623. ev = EVENT_ASSOC_TIMED_OUT;
  624. else
  625. return;
  626. os_memset(&event, 0, sizeof(event));
  627. os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
  628. wpa_supplicant_event(drv->ctx, ev, &event);
  629. }
  630. static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
  631. struct nlattr *freq, const u8 *frame, size_t len)
  632. {
  633. const struct ieee80211_mgmt *mgmt;
  634. union wpa_event_data event;
  635. u16 fc, stype;
  636. mgmt = (const struct ieee80211_mgmt *) frame;
  637. if (len < 24) {
  638. wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
  639. return;
  640. }
  641. fc = le_to_host16(mgmt->frame_control);
  642. stype = WLAN_FC_GET_STYPE(fc);
  643. os_memset(&event, 0, sizeof(event));
  644. if (freq) {
  645. event.rx_action.freq = nla_get_u32(freq);
  646. drv->last_mgmt_freq = event.rx_action.freq;
  647. }
  648. if (stype == WLAN_FC_STYPE_ACTION) {
  649. event.rx_action.da = mgmt->da;
  650. event.rx_action.sa = mgmt->sa;
  651. event.rx_action.bssid = mgmt->bssid;
  652. event.rx_action.category = mgmt->u.action.category;
  653. event.rx_action.data = &mgmt->u.action.category + 1;
  654. event.rx_action.len = frame + len - event.rx_action.data;
  655. wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
  656. } else {
  657. event.rx_mgmt.frame = frame;
  658. event.rx_mgmt.frame_len = len;
  659. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  660. }
  661. }
  662. static void mlme_event_action_tx_status(struct wpa_driver_nl80211_data *drv,
  663. struct nlattr *cookie, const u8 *frame,
  664. size_t len, struct nlattr *ack)
  665. {
  666. union wpa_event_data event;
  667. const struct ieee80211_hdr *hdr;
  668. u16 fc;
  669. u64 cookie_val;
  670. if (!cookie)
  671. return;
  672. cookie_val = nla_get_u64(cookie);
  673. wpa_printf(MSG_DEBUG, "nl80211: Action TX status: cookie=0%llx%s "
  674. "(ack=%d)",
  675. (long long unsigned int) cookie_val,
  676. cookie_val == drv->send_action_cookie ?
  677. " (match)" : " (unknown)", ack != NULL);
  678. if (cookie_val != drv->send_action_cookie)
  679. return;
  680. hdr = (const struct ieee80211_hdr *) frame;
  681. fc = le_to_host16(hdr->frame_control);
  682. os_memset(&event, 0, sizeof(event));
  683. event.tx_status.type = WLAN_FC_GET_TYPE(fc);
  684. event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
  685. event.tx_status.dst = hdr->addr1;
  686. event.tx_status.data = frame;
  687. event.tx_status.data_len = len;
  688. event.tx_status.ack = ack != NULL;
  689. wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
  690. }
  691. static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
  692. enum wpa_event_type type,
  693. const u8 *frame, size_t len)
  694. {
  695. const struct ieee80211_mgmt *mgmt;
  696. union wpa_event_data event;
  697. const u8 *bssid = NULL;
  698. u16 reason_code = 0;
  699. mgmt = (const struct ieee80211_mgmt *) frame;
  700. if (len >= 24) {
  701. bssid = mgmt->bssid;
  702. if (drv->associated != 0 &&
  703. os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
  704. os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
  705. /*
  706. * We have presumably received this deauth as a
  707. * response to a clear_state_mismatch() outgoing
  708. * deauth. Don't let it take us offline!
  709. */
  710. wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
  711. "from Unknown BSSID " MACSTR " -- ignoring",
  712. MAC2STR(bssid));
  713. return;
  714. }
  715. }
  716. drv->associated = 0;
  717. os_memset(&event, 0, sizeof(event));
  718. /* Note: Same offset for Reason Code in both frame subtypes */
  719. if (len >= 24 + sizeof(mgmt->u.deauth))
  720. reason_code = le_to_host16(mgmt->u.deauth.reason_code);
  721. if (type == EVENT_DISASSOC) {
  722. event.disassoc_info.addr = bssid;
  723. event.disassoc_info.reason_code = reason_code;
  724. if (frame + len > mgmt->u.disassoc.variable) {
  725. event.disassoc_info.ie = mgmt->u.disassoc.variable;
  726. event.disassoc_info.ie_len = frame + len -
  727. mgmt->u.disassoc.variable;
  728. }
  729. } else {
  730. event.deauth_info.addr = bssid;
  731. event.deauth_info.reason_code = reason_code;
  732. if (frame + len > mgmt->u.deauth.variable) {
  733. event.deauth_info.ie = mgmt->u.deauth.variable;
  734. event.deauth_info.ie_len = frame + len -
  735. mgmt->u.deauth.variable;
  736. }
  737. }
  738. wpa_supplicant_event(drv->ctx, type, &event);
  739. }
  740. static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
  741. enum wpa_event_type type,
  742. const u8 *frame, size_t len)
  743. {
  744. const struct ieee80211_mgmt *mgmt;
  745. union wpa_event_data event;
  746. u16 reason_code = 0;
  747. if (len < 24)
  748. return;
  749. mgmt = (const struct ieee80211_mgmt *) frame;
  750. os_memset(&event, 0, sizeof(event));
  751. /* Note: Same offset for Reason Code in both frame subtypes */
  752. if (len >= 24 + sizeof(mgmt->u.deauth))
  753. reason_code = le_to_host16(mgmt->u.deauth.reason_code);
  754. if (type == EVENT_UNPROT_DISASSOC) {
  755. event.unprot_disassoc.sa = mgmt->sa;
  756. event.unprot_disassoc.da = mgmt->da;
  757. event.unprot_disassoc.reason_code = reason_code;
  758. } else {
  759. event.unprot_deauth.sa = mgmt->sa;
  760. event.unprot_deauth.da = mgmt->da;
  761. event.unprot_deauth.reason_code = reason_code;
  762. }
  763. wpa_supplicant_event(drv->ctx, type, &event);
  764. }
  765. static void mlme_event(struct wpa_driver_nl80211_data *drv,
  766. enum nl80211_commands cmd, struct nlattr *frame,
  767. struct nlattr *addr, struct nlattr *timed_out,
  768. struct nlattr *freq, struct nlattr *ack,
  769. struct nlattr *cookie)
  770. {
  771. if (timed_out && addr) {
  772. mlme_timeout_event(drv, cmd, addr);
  773. return;
  774. }
  775. if (frame == NULL) {
  776. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
  777. "data", cmd);
  778. return;
  779. }
  780. wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
  781. wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
  782. nla_data(frame), nla_len(frame));
  783. switch (cmd) {
  784. case NL80211_CMD_AUTHENTICATE:
  785. mlme_event_auth(drv, nla_data(frame), nla_len(frame));
  786. break;
  787. case NL80211_CMD_ASSOCIATE:
  788. mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
  789. break;
  790. case NL80211_CMD_DEAUTHENTICATE:
  791. mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
  792. nla_data(frame), nla_len(frame));
  793. break;
  794. case NL80211_CMD_DISASSOCIATE:
  795. mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
  796. nla_data(frame), nla_len(frame));
  797. break;
  798. case NL80211_CMD_FRAME:
  799. mlme_event_mgmt(drv, freq, nla_data(frame), nla_len(frame));
  800. break;
  801. case NL80211_CMD_FRAME_TX_STATUS:
  802. mlme_event_action_tx_status(drv, cookie, nla_data(frame),
  803. nla_len(frame), ack);
  804. break;
  805. case NL80211_CMD_UNPROT_DEAUTHENTICATE:
  806. mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
  807. nla_data(frame), nla_len(frame));
  808. break;
  809. case NL80211_CMD_UNPROT_DISASSOCIATE:
  810. mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
  811. nla_data(frame), nla_len(frame));
  812. break;
  813. default:
  814. break;
  815. }
  816. }
  817. static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
  818. struct nlattr *tb[])
  819. {
  820. union wpa_event_data data;
  821. wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
  822. os_memset(&data, 0, sizeof(data));
  823. if (tb[NL80211_ATTR_MAC]) {
  824. wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
  825. nla_data(tb[NL80211_ATTR_MAC]),
  826. nla_len(tb[NL80211_ATTR_MAC]));
  827. data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
  828. }
  829. if (tb[NL80211_ATTR_KEY_SEQ]) {
  830. wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
  831. nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  832. nla_len(tb[NL80211_ATTR_KEY_SEQ]));
  833. }
  834. if (tb[NL80211_ATTR_KEY_TYPE]) {
  835. enum nl80211_key_type key_type =
  836. nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
  837. wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
  838. if (key_type == NL80211_KEYTYPE_PAIRWISE)
  839. data.michael_mic_failure.unicast = 1;
  840. } else
  841. data.michael_mic_failure.unicast = 1;
  842. if (tb[NL80211_ATTR_KEY_IDX]) {
  843. u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
  844. wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
  845. }
  846. wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
  847. }
  848. static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
  849. struct nlattr *tb[])
  850. {
  851. if (tb[NL80211_ATTR_MAC] == NULL) {
  852. wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
  853. "event");
  854. return;
  855. }
  856. os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
  857. drv->associated = 1;
  858. wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
  859. MAC2STR(drv->bssid));
  860. wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
  861. }
  862. static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
  863. int cancel_event, struct nlattr *tb[])
  864. {
  865. unsigned int freq, chan_type, duration;
  866. union wpa_event_data data;
  867. u64 cookie;
  868. if (tb[NL80211_ATTR_WIPHY_FREQ])
  869. freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
  870. else
  871. freq = 0;
  872. if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
  873. chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
  874. else
  875. chan_type = 0;
  876. if (tb[NL80211_ATTR_DURATION])
  877. duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
  878. else
  879. duration = 0;
  880. if (tb[NL80211_ATTR_COOKIE])
  881. cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
  882. else
  883. cookie = 0;
  884. wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
  885. "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
  886. cancel_event, freq, chan_type, duration,
  887. (long long unsigned int) cookie,
  888. cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
  889. if (cookie != drv->remain_on_chan_cookie)
  890. return; /* not for us */
  891. drv->pending_remain_on_chan = !cancel_event;
  892. os_memset(&data, 0, sizeof(data));
  893. data.remain_on_channel.freq = freq;
  894. data.remain_on_channel.duration = duration;
  895. wpa_supplicant_event(drv->ctx, cancel_event ?
  896. EVENT_CANCEL_REMAIN_ON_CHANNEL :
  897. EVENT_REMAIN_ON_CHANNEL, &data);
  898. }
  899. static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
  900. struct nlattr *tb[])
  901. {
  902. union wpa_event_data event;
  903. struct nlattr *nl;
  904. int rem;
  905. struct scan_info *info;
  906. #define MAX_REPORT_FREQS 50
  907. int freqs[MAX_REPORT_FREQS];
  908. int num_freqs = 0;
  909. os_memset(&event, 0, sizeof(event));
  910. info = &event.scan_info;
  911. info->aborted = aborted;
  912. if (tb[NL80211_ATTR_SCAN_SSIDS]) {
  913. nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
  914. struct wpa_driver_scan_ssid *s =
  915. &info->ssids[info->num_ssids];
  916. s->ssid = nla_data(nl);
  917. s->ssid_len = nla_len(nl);
  918. info->num_ssids++;
  919. if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
  920. break;
  921. }
  922. }
  923. if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
  924. nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
  925. {
  926. freqs[num_freqs] = nla_get_u32(nl);
  927. num_freqs++;
  928. if (num_freqs == MAX_REPORT_FREQS - 1)
  929. break;
  930. }
  931. info->freqs = freqs;
  932. info->num_freqs = num_freqs;
  933. }
  934. wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
  935. }
  936. static int get_link_signal(struct nl_msg *msg, void *arg)
  937. {
  938. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  939. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  940. struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
  941. static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
  942. [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
  943. };
  944. struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
  945. static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
  946. [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
  947. [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
  948. [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
  949. [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
  950. };
  951. struct wpa_signal_info *sig_change = arg;
  952. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  953. genlmsg_attrlen(gnlh, 0), NULL);
  954. if (!tb[NL80211_ATTR_STA_INFO] ||
  955. nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
  956. tb[NL80211_ATTR_STA_INFO], policy))
  957. return NL_SKIP;
  958. if (!sinfo[NL80211_STA_INFO_SIGNAL])
  959. return NL_SKIP;
  960. sig_change->current_signal =
  961. (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
  962. if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
  963. if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  964. sinfo[NL80211_STA_INFO_TX_BITRATE],
  965. rate_policy)) {
  966. sig_change->current_txrate = 0;
  967. } else {
  968. if (rinfo[NL80211_RATE_INFO_BITRATE]) {
  969. sig_change->current_txrate =
  970. nla_get_u16(rinfo[
  971. NL80211_RATE_INFO_BITRATE]) * 100;
  972. }
  973. }
  974. }
  975. return NL_SKIP;
  976. }
  977. static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
  978. struct wpa_signal_info *sig)
  979. {
  980. struct nl_msg *msg;
  981. sig->current_signal = -9999;
  982. sig->current_txrate = 0;
  983. msg = nlmsg_alloc();
  984. if (!msg)
  985. return -ENOMEM;
  986. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  987. 0, NL80211_CMD_GET_STATION, 0);
  988. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  989. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
  990. return send_and_recv_msgs(drv, msg, get_link_signal, sig);
  991. nla_put_failure:
  992. return -ENOBUFS;
  993. }
  994. static int get_link_noise(struct nl_msg *msg, void *arg)
  995. {
  996. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  997. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  998. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  999. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  1000. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  1001. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  1002. };
  1003. struct wpa_signal_info *sig_change = arg;
  1004. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1005. genlmsg_attrlen(gnlh, 0), NULL);
  1006. if (!tb[NL80211_ATTR_SURVEY_INFO]) {
  1007. wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
  1008. return NL_SKIP;
  1009. }
  1010. if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  1011. tb[NL80211_ATTR_SURVEY_INFO],
  1012. survey_policy)) {
  1013. wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
  1014. "attributes!");
  1015. return NL_SKIP;
  1016. }
  1017. if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
  1018. return NL_SKIP;
  1019. if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
  1020. sig_change->frequency)
  1021. return NL_SKIP;
  1022. if (!sinfo[NL80211_SURVEY_INFO_NOISE])
  1023. return NL_SKIP;
  1024. sig_change->current_noise =
  1025. (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  1026. return NL_SKIP;
  1027. }
  1028. static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
  1029. struct wpa_signal_info *sig_change)
  1030. {
  1031. struct nl_msg *msg;
  1032. sig_change->current_noise = 9999;
  1033. sig_change->frequency = drv->assoc_freq;
  1034. msg = nlmsg_alloc();
  1035. if (!msg)
  1036. return -ENOMEM;
  1037. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1038. NLM_F_DUMP, NL80211_CMD_GET_SURVEY, 0);
  1039. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1040. return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
  1041. nla_put_failure:
  1042. return -ENOBUFS;
  1043. }
  1044. static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
  1045. struct nlattr *tb[])
  1046. {
  1047. static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
  1048. [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
  1049. [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
  1050. [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
  1051. [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
  1052. };
  1053. struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
  1054. enum nl80211_cqm_rssi_threshold_event event;
  1055. union wpa_event_data ed;
  1056. struct wpa_signal_info sig;
  1057. int res;
  1058. if (tb[NL80211_ATTR_CQM] == NULL ||
  1059. nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
  1060. cqm_policy)) {
  1061. wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
  1062. return;
  1063. }
  1064. os_memset(&ed, 0, sizeof(ed));
  1065. if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
  1066. if (!tb[NL80211_ATTR_MAC])
  1067. return;
  1068. os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
  1069. ETH_ALEN);
  1070. wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
  1071. return;
  1072. }
  1073. if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
  1074. return;
  1075. event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
  1076. if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
  1077. wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
  1078. "event: RSSI high");
  1079. ed.signal_change.above_threshold = 1;
  1080. } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
  1081. wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
  1082. "event: RSSI low");
  1083. ed.signal_change.above_threshold = 0;
  1084. } else
  1085. return;
  1086. res = nl80211_get_link_signal(drv, &sig);
  1087. if (res == 0) {
  1088. ed.signal_change.current_signal = sig.current_signal;
  1089. ed.signal_change.current_txrate = sig.current_txrate;
  1090. wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
  1091. sig.current_signal, sig.current_txrate);
  1092. }
  1093. res = nl80211_get_link_noise(drv, &sig);
  1094. if (res == 0) {
  1095. ed.signal_change.current_noise = sig.current_noise;
  1096. wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
  1097. sig.current_noise);
  1098. }
  1099. wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
  1100. }
  1101. static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
  1102. struct nlattr **tb)
  1103. {
  1104. u8 *addr;
  1105. union wpa_event_data data;
  1106. if (tb[NL80211_ATTR_MAC] == NULL)
  1107. return;
  1108. addr = nla_data(tb[NL80211_ATTR_MAC]);
  1109. wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
  1110. if (drv->nlmode == NL80211_IFTYPE_AP &&
  1111. drv->no_monitor_iface_capab) {
  1112. u8 *ies = NULL;
  1113. size_t ies_len = 0;
  1114. if (tb[NL80211_ATTR_IE]) {
  1115. ies = nla_data(tb[NL80211_ATTR_IE]);
  1116. ies_len = nla_len(tb[NL80211_ATTR_IE]);
  1117. }
  1118. wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
  1119. drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
  1120. return;
  1121. }
  1122. if (drv->nlmode != NL80211_IFTYPE_ADHOC)
  1123. return;
  1124. os_memset(&data, 0, sizeof(data));
  1125. os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
  1126. wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
  1127. }
  1128. static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
  1129. struct nlattr **tb)
  1130. {
  1131. u8 *addr;
  1132. union wpa_event_data data;
  1133. if (tb[NL80211_ATTR_MAC] == NULL)
  1134. return;
  1135. addr = nla_data(tb[NL80211_ATTR_MAC]);
  1136. wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
  1137. MAC2STR(addr));
  1138. if (drv->nlmode == NL80211_IFTYPE_AP &&
  1139. drv->no_monitor_iface_capab) {
  1140. drv_event_disassoc(drv->ctx, addr);
  1141. return;
  1142. }
  1143. if (drv->nlmode != NL80211_IFTYPE_ADHOC)
  1144. return;
  1145. os_memset(&data, 0, sizeof(data));
  1146. os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
  1147. wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
  1148. }
  1149. static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
  1150. struct nlattr **tb)
  1151. {
  1152. struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
  1153. static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
  1154. [NL80211_REKEY_DATA_KEK] = {
  1155. .minlen = NL80211_KEK_LEN,
  1156. .maxlen = NL80211_KEK_LEN,
  1157. },
  1158. [NL80211_REKEY_DATA_KCK] = {
  1159. .minlen = NL80211_KCK_LEN,
  1160. .maxlen = NL80211_KCK_LEN,
  1161. },
  1162. [NL80211_REKEY_DATA_REPLAY_CTR] = {
  1163. .minlen = NL80211_REPLAY_CTR_LEN,
  1164. .maxlen = NL80211_REPLAY_CTR_LEN,
  1165. },
  1166. };
  1167. union wpa_event_data data;
  1168. if (!tb[NL80211_ATTR_MAC])
  1169. return;
  1170. if (!tb[NL80211_ATTR_REKEY_DATA])
  1171. return;
  1172. if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
  1173. tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
  1174. return;
  1175. if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
  1176. return;
  1177. os_memset(&data, 0, sizeof(data));
  1178. data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
  1179. wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
  1180. MAC2STR(data.driver_gtk_rekey.bssid));
  1181. data.driver_gtk_rekey.replay_ctr =
  1182. nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
  1183. wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
  1184. data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
  1185. wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
  1186. }
  1187. static int process_event(struct nl_msg *msg, void *arg)
  1188. {
  1189. struct wpa_driver_nl80211_data *drv = arg;
  1190. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1191. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1192. union wpa_event_data data;
  1193. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1194. genlmsg_attrlen(gnlh, 0), NULL);
  1195. if (tb[NL80211_ATTR_IFINDEX]) {
  1196. int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
  1197. if (ifindex != drv->ifindex && !have_ifidx(drv, ifindex)) {
  1198. wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)"
  1199. " for foreign interface (ifindex %d)",
  1200. gnlh->cmd, ifindex);
  1201. return NL_SKIP;
  1202. }
  1203. }
  1204. if (drv->ap_scan_as_station &&
  1205. (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
  1206. gnlh->cmd == NL80211_CMD_SCAN_ABORTED)) {
  1207. wpa_driver_nl80211_set_mode(&drv->first_bss,
  1208. IEEE80211_MODE_AP);
  1209. drv->ap_scan_as_station = 0;
  1210. }
  1211. switch (gnlh->cmd) {
  1212. case NL80211_CMD_TRIGGER_SCAN:
  1213. wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
  1214. break;
  1215. case NL80211_CMD_NEW_SCAN_RESULTS:
  1216. wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
  1217. drv->scan_complete_events = 1;
  1218. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
  1219. drv->ctx);
  1220. send_scan_event(drv, 0, tb);
  1221. break;
  1222. case NL80211_CMD_SCAN_ABORTED:
  1223. wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
  1224. /*
  1225. * Need to indicate that scan results are available in order
  1226. * not to make wpa_supplicant stop its scanning.
  1227. */
  1228. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
  1229. drv->ctx);
  1230. send_scan_event(drv, 1, tb);
  1231. break;
  1232. case NL80211_CMD_AUTHENTICATE:
  1233. case NL80211_CMD_ASSOCIATE:
  1234. case NL80211_CMD_DEAUTHENTICATE:
  1235. case NL80211_CMD_DISASSOCIATE:
  1236. case NL80211_CMD_FRAME:
  1237. case NL80211_CMD_FRAME_TX_STATUS:
  1238. case NL80211_CMD_UNPROT_DEAUTHENTICATE:
  1239. case NL80211_CMD_UNPROT_DISASSOCIATE:
  1240. mlme_event(drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
  1241. tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
  1242. tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
  1243. tb[NL80211_ATTR_COOKIE]);
  1244. break;
  1245. case NL80211_CMD_CONNECT:
  1246. case NL80211_CMD_ROAM:
  1247. mlme_event_connect(drv, gnlh->cmd,
  1248. tb[NL80211_ATTR_STATUS_CODE],
  1249. tb[NL80211_ATTR_MAC],
  1250. tb[NL80211_ATTR_REQ_IE],
  1251. tb[NL80211_ATTR_RESP_IE]);
  1252. break;
  1253. case NL80211_CMD_DISCONNECT:
  1254. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  1255. /*
  1256. * Avoid reporting two disassociation events that could
  1257. * confuse the core code.
  1258. */
  1259. wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
  1260. "event when using userspace SME");
  1261. break;
  1262. }
  1263. drv->associated = 0;
  1264. os_memset(&data, 0, sizeof(data));
  1265. if (tb[NL80211_ATTR_REASON_CODE])
  1266. data.disassoc_info.reason_code =
  1267. nla_get_u16(tb[NL80211_ATTR_REASON_CODE]);
  1268. wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, &data);
  1269. break;
  1270. case NL80211_CMD_MICHAEL_MIC_FAILURE:
  1271. mlme_event_michael_mic_failure(drv, tb);
  1272. break;
  1273. case NL80211_CMD_JOIN_IBSS:
  1274. mlme_event_join_ibss(drv, tb);
  1275. break;
  1276. case NL80211_CMD_REMAIN_ON_CHANNEL:
  1277. mlme_event_remain_on_channel(drv, 0, tb);
  1278. break;
  1279. case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
  1280. mlme_event_remain_on_channel(drv, 1, tb);
  1281. break;
  1282. case NL80211_CMD_NOTIFY_CQM:
  1283. nl80211_cqm_event(drv, tb);
  1284. break;
  1285. case NL80211_CMD_REG_CHANGE:
  1286. wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
  1287. wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
  1288. NULL);
  1289. break;
  1290. case NL80211_CMD_REG_BEACON_HINT:
  1291. wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
  1292. wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
  1293. NULL);
  1294. break;
  1295. case NL80211_CMD_NEW_STATION:
  1296. nl80211_new_station_event(drv, tb);
  1297. break;
  1298. case NL80211_CMD_DEL_STATION:
  1299. nl80211_del_station_event(drv, tb);
  1300. break;
  1301. case NL80211_CMD_SET_REKEY_OFFLOAD:
  1302. nl80211_rekey_offload_event(drv, tb);
  1303. break;
  1304. default:
  1305. wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
  1306. "(cmd=%d)", gnlh->cmd);
  1307. break;
  1308. }
  1309. return NL_SKIP;
  1310. }
  1311. static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
  1312. void *handle)
  1313. {
  1314. struct nl_cb *cb;
  1315. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  1316. wpa_printf(MSG_DEBUG, "nl80211: Event message available");
  1317. cb = nl_cb_clone(drv->nl_cb);
  1318. if (!cb)
  1319. return;
  1320. nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
  1321. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, process_event, drv);
  1322. nl_recvmsgs(handle, cb);
  1323. nl_cb_put(cb);
  1324. }
  1325. /**
  1326. * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
  1327. * @priv: driver_nl80211 private data
  1328. * @alpha2_arg: country to which to switch to
  1329. * Returns: 0 on success, -1 on failure
  1330. *
  1331. * This asks nl80211 to set the regulatory domain for given
  1332. * country ISO / IEC alpha2.
  1333. */
  1334. static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
  1335. {
  1336. struct i802_bss *bss = priv;
  1337. struct wpa_driver_nl80211_data *drv = bss->drv;
  1338. char alpha2[3];
  1339. struct nl_msg *msg;
  1340. msg = nlmsg_alloc();
  1341. if (!msg)
  1342. return -ENOMEM;
  1343. alpha2[0] = alpha2_arg[0];
  1344. alpha2[1] = alpha2_arg[1];
  1345. alpha2[2] = '\0';
  1346. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1347. 0, NL80211_CMD_REQ_SET_REG, 0);
  1348. NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
  1349. if (send_and_recv_msgs(drv, msg, NULL, NULL))
  1350. return -EINVAL;
  1351. return 0;
  1352. nla_put_failure:
  1353. return -EINVAL;
  1354. }
  1355. struct wiphy_info_data {
  1356. int max_scan_ssids;
  1357. int ap_supported;
  1358. int p2p_supported;
  1359. int auth_supported;
  1360. int connect_supported;
  1361. int offchan_tx_supported;
  1362. int max_remain_on_chan;
  1363. };
  1364. static int wiphy_info_handler(struct nl_msg *msg, void *arg)
  1365. {
  1366. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1367. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1368. struct wiphy_info_data *info = arg;
  1369. int p2p_go_supported = 0, p2p_client_supported = 0;
  1370. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1371. genlmsg_attrlen(gnlh, 0), NULL);
  1372. if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
  1373. info->max_scan_ssids =
  1374. nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
  1375. if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
  1376. struct nlattr *nl_mode;
  1377. int i;
  1378. nla_for_each_nested(nl_mode,
  1379. tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
  1380. switch (nla_type(nl_mode)) {
  1381. case NL80211_IFTYPE_AP:
  1382. info->ap_supported = 1;
  1383. break;
  1384. case NL80211_IFTYPE_P2P_GO:
  1385. p2p_go_supported = 1;
  1386. break;
  1387. case NL80211_IFTYPE_P2P_CLIENT:
  1388. p2p_client_supported = 1;
  1389. break;
  1390. }
  1391. }
  1392. }
  1393. info->p2p_supported = p2p_go_supported && p2p_client_supported;
  1394. if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
  1395. struct nlattr *nl_cmd;
  1396. int i;
  1397. nla_for_each_nested(nl_cmd,
  1398. tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
  1399. u32 cmd = nla_get_u32(nl_cmd);
  1400. if (cmd == NL80211_CMD_AUTHENTICATE)
  1401. info->auth_supported = 1;
  1402. else if (cmd == NL80211_CMD_CONNECT)
  1403. info->connect_supported = 1;
  1404. }
  1405. }
  1406. if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK])
  1407. info->offchan_tx_supported = 1;
  1408. if (tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION])
  1409. info->max_remain_on_chan =
  1410. nla_get_u32(tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
  1411. return NL_SKIP;
  1412. }
  1413. static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
  1414. struct wiphy_info_data *info)
  1415. {
  1416. struct nl_msg *msg;
  1417. os_memset(info, 0, sizeof(*info));
  1418. /* default to 5000 since early versions of mac80211 don't set it */
  1419. info->max_remain_on_chan = 5000;
  1420. msg = nlmsg_alloc();
  1421. if (!msg)
  1422. return -1;
  1423. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1424. 0, NL80211_CMD_GET_WIPHY, 0);
  1425. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
  1426. if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0)
  1427. return 0;
  1428. msg = NULL;
  1429. nla_put_failure:
  1430. nlmsg_free(msg);
  1431. return -1;
  1432. }
  1433. static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
  1434. {
  1435. struct wiphy_info_data info;
  1436. if (wpa_driver_nl80211_get_info(drv, &info))
  1437. return -1;
  1438. drv->has_capability = 1;
  1439. /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
  1440. drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1441. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1442. WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1443. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
  1444. drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
  1445. WPA_DRIVER_CAPA_ENC_WEP104 |
  1446. WPA_DRIVER_CAPA_ENC_TKIP |
  1447. WPA_DRIVER_CAPA_ENC_CCMP;
  1448. drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
  1449. WPA_DRIVER_AUTH_SHARED |
  1450. WPA_DRIVER_AUTH_LEAP;
  1451. drv->capa.max_scan_ssids = info.max_scan_ssids;
  1452. if (info.ap_supported)
  1453. drv->capa.flags |= WPA_DRIVER_FLAGS_AP;
  1454. if (info.auth_supported)
  1455. drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
  1456. else if (!info.connect_supported) {
  1457. wpa_printf(MSG_INFO, "nl80211: Driver does not support "
  1458. "authentication/association or connect commands");
  1459. return -1;
  1460. }
  1461. if (info.offchan_tx_supported) {
  1462. wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
  1463. "off-channel TX");
  1464. drv->capa.flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
  1465. }
  1466. drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
  1467. drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
  1468. if (info.p2p_supported)
  1469. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
  1470. drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
  1471. drv->capa.max_remain_on_chan = info.max_remain_on_chan;
  1472. return 0;
  1473. }
  1474. static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
  1475. {
  1476. int ret;
  1477. /* Initialize generic netlink and nl80211 */
  1478. drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  1479. if (drv->nl_cb == NULL) {
  1480. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  1481. "callbacks");
  1482. goto err1;
  1483. }
  1484. drv->nl_handle = nl80211_handle_alloc(drv->nl_cb);
  1485. if (drv->nl_handle == NULL) {
  1486. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  1487. "callbacks");
  1488. goto err2;
  1489. }
  1490. drv->nl_handle_event = nl80211_handle_alloc(drv->nl_cb);
  1491. if (drv->nl_handle_event == NULL) {
  1492. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  1493. "callbacks (event)");
  1494. goto err2b;
  1495. }
  1496. if (genl_connect(drv->nl_handle)) {
  1497. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
  1498. "netlink");
  1499. goto err3;
  1500. }
  1501. if (genl_connect(drv->nl_handle_event)) {
  1502. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
  1503. "netlink (event)");
  1504. goto err3;
  1505. }
  1506. #ifdef CONFIG_LIBNL20
  1507. if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
  1508. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  1509. "netlink cache");
  1510. goto err3;
  1511. }
  1512. if (genl_ctrl_alloc_cache(drv->nl_handle_event, &drv->nl_cache_event) <
  1513. 0) {
  1514. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  1515. "netlink cache (event)");
  1516. goto err3b;
  1517. }
  1518. #else /* CONFIG_LIBNL20 */
  1519. drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
  1520. if (drv->nl_cache == NULL) {
  1521. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  1522. "netlink cache");
  1523. goto err3;
  1524. }
  1525. drv->nl_cache_event = genl_ctrl_alloc_cache(drv->nl_handle_event);
  1526. if (drv->nl_cache_event == NULL) {
  1527. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  1528. "netlink cache (event)");
  1529. goto err3b;
  1530. }
  1531. #endif /* CONFIG_LIBNL20 */
  1532. drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
  1533. if (drv->nl80211 == NULL) {
  1534. wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
  1535. "found");
  1536. goto err4;
  1537. }
  1538. ret = nl_get_multicast_id(drv, "nl80211", "scan");
  1539. if (ret >= 0)
  1540. ret = nl_socket_add_membership(drv->nl_handle_event, ret);
  1541. if (ret < 0) {
  1542. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  1543. "membership for scan events: %d (%s)",
  1544. ret, strerror(-ret));
  1545. goto err4;
  1546. }
  1547. ret = nl_get_multicast_id(drv, "nl80211", "mlme");
  1548. if (ret >= 0)
  1549. ret = nl_socket_add_membership(drv->nl_handle_event, ret);
  1550. if (ret < 0) {
  1551. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  1552. "membership for mlme events: %d (%s)",
  1553. ret, strerror(-ret));
  1554. goto err4;
  1555. }
  1556. ret = nl_get_multicast_id(drv, "nl80211", "regulatory");
  1557. if (ret >= 0)
  1558. ret = nl_socket_add_membership(drv->nl_handle_event, ret);
  1559. if (ret < 0) {
  1560. wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
  1561. "membership for regulatory events: %d (%s)",
  1562. ret, strerror(-ret));
  1563. /* Continue without regulatory events */
  1564. }
  1565. eloop_register_read_sock(nl_socket_get_fd(drv->nl_handle_event),
  1566. wpa_driver_nl80211_event_receive, drv,
  1567. drv->nl_handle_event);
  1568. return 0;
  1569. err4:
  1570. nl_cache_free(drv->nl_cache_event);
  1571. err3b:
  1572. nl_cache_free(drv->nl_cache);
  1573. err3:
  1574. nl80211_handle_destroy(drv->nl_handle_event);
  1575. err2b:
  1576. nl80211_handle_destroy(drv->nl_handle);
  1577. err2:
  1578. nl_cb_put(drv->nl_cb);
  1579. err1:
  1580. return -1;
  1581. }
  1582. static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
  1583. {
  1584. wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
  1585. /*
  1586. * This may be for any interface; use ifdown event to disable
  1587. * interface.
  1588. */
  1589. }
  1590. static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
  1591. {
  1592. struct wpa_driver_nl80211_data *drv = ctx;
  1593. wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
  1594. if (linux_set_iface_flags(drv->ioctl_sock, drv->first_bss.ifname, 1)) {
  1595. wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
  1596. "after rfkill unblock");
  1597. return;
  1598. }
  1599. /* rtnetlink ifup handler will report interface as enabled */
  1600. }
  1601. static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
  1602. {
  1603. /* Find phy (radio) to which this interface belongs */
  1604. char buf[90], *pos;
  1605. int f, rv;
  1606. drv->phyname[0] = '\0';
  1607. snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
  1608. drv->first_bss.ifname);
  1609. f = open(buf, O_RDONLY);
  1610. if (f < 0) {
  1611. wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
  1612. buf, strerror(errno));
  1613. return;
  1614. }
  1615. rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
  1616. close(f);
  1617. if (rv < 0) {
  1618. wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
  1619. buf, strerror(errno));
  1620. return;
  1621. }
  1622. drv->phyname[rv] = '\0';
  1623. pos = os_strchr(drv->phyname, '\n');
  1624. if (pos)
  1625. *pos = '\0';
  1626. wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
  1627. drv->first_bss.ifname, drv->phyname);
  1628. }
  1629. /**
  1630. * wpa_driver_nl80211_init - Initialize nl80211 driver interface
  1631. * @ctx: context to be used when calling wpa_supplicant functions,
  1632. * e.g., wpa_supplicant_event()
  1633. * @ifname: interface name, e.g., wlan0
  1634. * @global_priv: private driver global data from global_init()
  1635. * Returns: Pointer to private data, %NULL on failure
  1636. */
  1637. static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
  1638. void *global_priv)
  1639. {
  1640. struct wpa_driver_nl80211_data *drv;
  1641. struct netlink_config *cfg;
  1642. struct rfkill_config *rcfg;
  1643. struct i802_bss *bss;
  1644. drv = os_zalloc(sizeof(*drv));
  1645. if (drv == NULL)
  1646. return NULL;
  1647. drv->global = global_priv;
  1648. drv->ctx = ctx;
  1649. bss = &drv->first_bss;
  1650. bss->drv = drv;
  1651. os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
  1652. drv->monitor_ifidx = -1;
  1653. drv->monitor_sock = -1;
  1654. drv->ioctl_sock = -1;
  1655. if (wpa_driver_nl80211_init_nl(drv)) {
  1656. os_free(drv);
  1657. return NULL;
  1658. }
  1659. nl80211_get_phy_name(drv);
  1660. drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  1661. if (drv->ioctl_sock < 0) {
  1662. perror("socket(PF_INET,SOCK_DGRAM)");
  1663. goto failed;
  1664. }
  1665. cfg = os_zalloc(sizeof(*cfg));
  1666. if (cfg == NULL)
  1667. goto failed;
  1668. cfg->ctx = drv;
  1669. cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
  1670. cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
  1671. drv->netlink = netlink_init(cfg);
  1672. if (drv->netlink == NULL) {
  1673. os_free(cfg);
  1674. goto failed;
  1675. }
  1676. rcfg = os_zalloc(sizeof(*rcfg));
  1677. if (rcfg == NULL)
  1678. goto failed;
  1679. rcfg->ctx = drv;
  1680. os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
  1681. rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
  1682. rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
  1683. drv->rfkill = rfkill_init(rcfg);
  1684. if (drv->rfkill == NULL) {
  1685. wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
  1686. os_free(rcfg);
  1687. }
  1688. if (wpa_driver_nl80211_finish_drv_init(drv))
  1689. goto failed;
  1690. if (drv->global)
  1691. dl_list_add(&drv->global->interfaces, &drv->list);
  1692. return bss;
  1693. failed:
  1694. rfkill_deinit(drv->rfkill);
  1695. netlink_deinit(drv->netlink);
  1696. if (drv->ioctl_sock >= 0)
  1697. close(drv->ioctl_sock);
  1698. genl_family_put(drv->nl80211);
  1699. nl_cache_free(drv->nl_cache);
  1700. nl80211_handle_destroy(drv->nl_handle);
  1701. nl_cb_put(drv->nl_cb);
  1702. eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
  1703. os_free(drv);
  1704. return NULL;
  1705. }
  1706. static int nl80211_register_frame(struct wpa_driver_nl80211_data *drv,
  1707. struct nl_handle *nl_handle,
  1708. u16 type, const u8 *match, size_t match_len)
  1709. {
  1710. struct nl_msg *msg;
  1711. int ret = -1;
  1712. msg = nlmsg_alloc();
  1713. if (!msg)
  1714. return -1;
  1715. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  1716. NL80211_CMD_REGISTER_ACTION, 0);
  1717. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1718. NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
  1719. NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
  1720. ret = send_and_recv(drv, nl_handle, msg, NULL, NULL);
  1721. msg = NULL;
  1722. if (ret) {
  1723. wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
  1724. "failed (type=%u): ret=%d (%s)",
  1725. type, ret, strerror(-ret));
  1726. wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
  1727. match, match_len);
  1728. goto nla_put_failure;
  1729. }
  1730. ret = 0;
  1731. nla_put_failure:
  1732. nlmsg_free(msg);
  1733. return ret;
  1734. }
  1735. static int nl80211_register_action_frame(struct wpa_driver_nl80211_data *drv,
  1736. const u8 *match, size_t match_len)
  1737. {
  1738. u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
  1739. return nl80211_register_frame(drv, drv->nl_handle_event,
  1740. type, match, match_len);
  1741. }
  1742. static int nl80211_register_action_frames(struct wpa_driver_nl80211_data *drv)
  1743. {
  1744. #ifdef CONFIG_P2P
  1745. /* GAS Initial Request */
  1746. if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0a", 2) < 0)
  1747. return -1;
  1748. /* GAS Initial Response */
  1749. if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0b", 2) < 0)
  1750. return -1;
  1751. /* GAS Comeback Request */
  1752. if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0c", 2) < 0)
  1753. return -1;
  1754. /* GAS Comeback Response */
  1755. if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0d", 2) < 0)
  1756. return -1;
  1757. /* P2P Public Action */
  1758. if (nl80211_register_action_frame(drv,
  1759. (u8 *) "\x04\x09\x50\x6f\x9a\x09",
  1760. 6) < 0)
  1761. return -1;
  1762. /* P2P Action */
  1763. if (nl80211_register_action_frame(drv,
  1764. (u8 *) "\x7f\x50\x6f\x9a\x09",
  1765. 5) < 0)
  1766. return -1;
  1767. #endif /* CONFIG_P2P */
  1768. #ifdef CONFIG_IEEE80211W
  1769. /* SA Query Response */
  1770. if (nl80211_register_action_frame(drv, (u8 *) "\x08\x01", 2) < 0)
  1771. return -1;
  1772. #endif /* CONFIG_IEEE80211W */
  1773. /* FT Action frames */
  1774. if (nl80211_register_action_frame(drv, (u8 *) "\x06", 1) < 0)
  1775. return -1;
  1776. else
  1777. drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
  1778. WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
  1779. return 0;
  1780. }
  1781. static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
  1782. {
  1783. wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
  1784. }
  1785. static int
  1786. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
  1787. {
  1788. struct i802_bss *bss = &drv->first_bss;
  1789. int send_rfkill_event = 0;
  1790. drv->ifindex = if_nametoindex(bss->ifname);
  1791. drv->first_bss.ifindex = drv->ifindex;
  1792. #ifndef HOSTAPD
  1793. /*
  1794. * Make sure the interface starts up in station mode unless this is a
  1795. * dynamically added interface (e.g., P2P) that was already configured
  1796. * with proper iftype.
  1797. */
  1798. if ((drv->global == NULL ||
  1799. drv->ifindex != drv->global->if_add_ifindex) &&
  1800. wpa_driver_nl80211_set_mode(bss, IEEE80211_MODE_INFRA) < 0) {
  1801. wpa_printf(MSG_DEBUG, "nl80211: Could not configure driver to "
  1802. "use managed mode");
  1803. }
  1804. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1)) {
  1805. if (rfkill_is_blocked(drv->rfkill)) {
  1806. wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
  1807. "interface '%s' due to rfkill",
  1808. bss->ifname);
  1809. drv->if_disabled = 1;
  1810. send_rfkill_event = 1;
  1811. } else {
  1812. wpa_printf(MSG_ERROR, "nl80211: Could not set "
  1813. "interface '%s' UP", bss->ifname);
  1814. return -1;
  1815. }
  1816. }
  1817. netlink_send_oper_ifla(drv->netlink, drv->ifindex,
  1818. 1, IF_OPER_DORMANT);
  1819. #endif /* HOSTAPD */
  1820. if (wpa_driver_nl80211_capa(drv))
  1821. return -1;
  1822. if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, drv->addr))
  1823. return -1;
  1824. if (nl80211_register_action_frames(drv) < 0) {
  1825. wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
  1826. "frame processing - ignore for now");
  1827. /*
  1828. * Older kernel versions did not support this, so ignore the
  1829. * error for now. Some functionality may not be available
  1830. * because of this.
  1831. */
  1832. }
  1833. if (send_rfkill_event) {
  1834. eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
  1835. drv, drv->ctx);
  1836. }
  1837. return 0;
  1838. }
  1839. static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
  1840. {
  1841. struct nl_msg *msg;
  1842. msg = nlmsg_alloc();
  1843. if (!msg)
  1844. return -ENOMEM;
  1845. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  1846. 0, NL80211_CMD_DEL_BEACON, 0);
  1847. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1848. return send_and_recv_msgs(drv, msg, NULL, NULL);
  1849. nla_put_failure:
  1850. return -ENOBUFS;
  1851. }
  1852. /**
  1853. * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
  1854. * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
  1855. *
  1856. * Shut down driver interface and processing of driver events. Free
  1857. * private data buffer if one was allocated in wpa_driver_nl80211_init().
  1858. */
  1859. static void wpa_driver_nl80211_deinit(void *priv)
  1860. {
  1861. struct i802_bss *bss = priv;
  1862. struct wpa_driver_nl80211_data *drv = bss->drv;
  1863. if (drv->nl_handle_preq)
  1864. wpa_driver_nl80211_probe_req_report(bss, 0);
  1865. if (bss->added_if_into_bridge) {
  1866. if (linux_br_del_if(drv->ioctl_sock, bss->brname, bss->ifname)
  1867. < 0)
  1868. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  1869. "interface %s from bridge %s: %s",
  1870. bss->ifname, bss->brname, strerror(errno));
  1871. }
  1872. if (bss->added_bridge) {
  1873. if (linux_br_del(drv->ioctl_sock, bss->brname) < 0)
  1874. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  1875. "bridge %s: %s",
  1876. bss->brname, strerror(errno));
  1877. }
  1878. nl80211_remove_monitor_interface(drv);
  1879. if (drv->nlmode == NL80211_IFTYPE_AP)
  1880. wpa_driver_nl80211_del_beacon(drv);
  1881. #ifdef HOSTAPD
  1882. if (drv->last_freq_ht) {
  1883. /* Clear HT flags from the driver */
  1884. struct hostapd_freq_params freq;
  1885. os_memset(&freq, 0, sizeof(freq));
  1886. freq.freq = drv->last_freq;
  1887. i802_set_freq(priv, &freq);
  1888. }
  1889. if (drv->eapol_sock >= 0) {
  1890. eloop_unregister_read_sock(drv->eapol_sock);
  1891. close(drv->eapol_sock);
  1892. }
  1893. if (drv->if_indices != drv->default_if_indices)
  1894. os_free(drv->if_indices);
  1895. #endif /* HOSTAPD */
  1896. if (drv->disable_11b_rates)
  1897. nl80211_disable_11b_rates(drv, drv->ifindex, 0);
  1898. netlink_send_oper_ifla(drv->netlink, drv->ifindex, 0, IF_OPER_UP);
  1899. netlink_deinit(drv->netlink);
  1900. rfkill_deinit(drv->rfkill);
  1901. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  1902. (void) linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 0);
  1903. wpa_driver_nl80211_set_mode(bss, IEEE80211_MODE_INFRA);
  1904. if (drv->ioctl_sock >= 0)
  1905. close(drv->ioctl_sock);
  1906. eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
  1907. genl_family_put(drv->nl80211);
  1908. nl_cache_free(drv->nl_cache);
  1909. nl_cache_free(drv->nl_cache_event);
  1910. nl80211_handle_destroy(drv->nl_handle);
  1911. nl80211_handle_destroy(drv->nl_handle_event);
  1912. nl_cb_put(drv->nl_cb);
  1913. os_free(drv->filter_ssids);
  1914. if (drv->global)
  1915. dl_list_del(&drv->list);
  1916. os_free(drv);
  1917. }
  1918. /**
  1919. * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
  1920. * @eloop_ctx: Driver private data
  1921. * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
  1922. *
  1923. * This function can be used as registered timeout when starting a scan to
  1924. * generate a scan completed event if the driver does not report this.
  1925. */
  1926. static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  1927. {
  1928. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  1929. if (drv->ap_scan_as_station) {
  1930. wpa_driver_nl80211_set_mode(&drv->first_bss,
  1931. IEEE80211_MODE_AP);
  1932. drv->ap_scan_as_station = 0;
  1933. }
  1934. wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
  1935. wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
  1936. }
  1937. /**
  1938. * wpa_driver_nl80211_scan - Request the driver to initiate scan
  1939. * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
  1940. * @params: Scan parameters
  1941. * Returns: 0 on success, -1 on failure
  1942. */
  1943. static int wpa_driver_nl80211_scan(void *priv,
  1944. struct wpa_driver_scan_params *params)
  1945. {
  1946. struct i802_bss *bss = priv;
  1947. struct wpa_driver_nl80211_data *drv = bss->drv;
  1948. int ret = 0, timeout;
  1949. struct nl_msg *msg, *ssids, *freqs;
  1950. size_t i;
  1951. msg = nlmsg_alloc();
  1952. ssids = nlmsg_alloc();
  1953. freqs = nlmsg_alloc();
  1954. if (!msg || !ssids || !freqs) {
  1955. nlmsg_free(msg);
  1956. nlmsg_free(ssids);
  1957. nlmsg_free(freqs);
  1958. return -1;
  1959. }
  1960. os_free(drv->filter_ssids);
  1961. drv->filter_ssids = params->filter_ssids;
  1962. params->filter_ssids = NULL;
  1963. drv->num_filter_ssids = params->num_filter_ssids;
  1964. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  1965. NL80211_CMD_TRIGGER_SCAN, 0);
  1966. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  1967. for (i = 0; i < params->num_ssids; i++) {
  1968. wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
  1969. params->ssids[i].ssid,
  1970. params->ssids[i].ssid_len);
  1971. NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
  1972. params->ssids[i].ssid);
  1973. }
  1974. if (params->num_ssids)
  1975. nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
  1976. if (params->extra_ies) {
  1977. wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan extra IEs",
  1978. params->extra_ies, params->extra_ies_len);
  1979. NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
  1980. params->extra_ies);
  1981. }
  1982. if (params->freqs) {
  1983. for (i = 0; params->freqs[i]; i++) {
  1984. wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
  1985. "MHz", params->freqs[i]);
  1986. NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
  1987. }
  1988. nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
  1989. }
  1990. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  1991. msg = NULL;
  1992. if (ret) {
  1993. wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
  1994. "(%s)", ret, strerror(-ret));
  1995. #ifdef HOSTAPD
  1996. if (drv->nlmode == NL80211_IFTYPE_AP) {
  1997. /*
  1998. * mac80211 does not allow scan requests in AP mode, so
  1999. * try to do this in station mode.
  2000. */
  2001. if (wpa_driver_nl80211_set_mode(bss,
  2002. IEEE80211_MODE_INFRA))
  2003. goto nla_put_failure;
  2004. if (wpa_driver_nl80211_scan(drv, params)) {
  2005. wpa_driver_nl80211_set_mode(bss,
  2006. IEEE80211_MODE_AP);
  2007. goto nla_put_failure;
  2008. }
  2009. /* Restore AP mode when processing scan results */
  2010. drv->ap_scan_as_station = 1;
  2011. ret = 0;
  2012. } else
  2013. goto nla_put_failure;
  2014. #else /* HOSTAPD */
  2015. goto nla_put_failure;
  2016. #endif /* HOSTAPD */
  2017. }
  2018. /* Not all drivers generate "scan completed" wireless event, so try to
  2019. * read results after a timeout. */
  2020. timeout = 10;
  2021. if (drv->scan_complete_events) {
  2022. /*
  2023. * The driver seems to deliver events to notify when scan is
  2024. * complete, so use longer timeout to avoid race conditions
  2025. * with scanning and following association request.
  2026. */
  2027. timeout = 30;
  2028. }
  2029. wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
  2030. "seconds", ret, timeout);
  2031. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  2032. eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
  2033. drv, drv->ctx);
  2034. nla_put_failure:
  2035. nlmsg_free(ssids);
  2036. nlmsg_free(msg);
  2037. nlmsg_free(freqs);
  2038. return ret;
  2039. }
  2040. static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
  2041. {
  2042. const u8 *end, *pos;
  2043. if (ies == NULL)
  2044. return NULL;
  2045. pos = ies;
  2046. end = ies + ies_len;
  2047. while (pos + 1 < end) {
  2048. if (pos + 2 + pos[1] > end)
  2049. break;
  2050. if (pos[0] == ie)
  2051. return pos;
  2052. pos += 2 + pos[1];
  2053. }
  2054. return NULL;
  2055. }
  2056. static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
  2057. const u8 *ie, size_t ie_len)
  2058. {
  2059. const u8 *ssid;
  2060. size_t i;
  2061. if (drv->filter_ssids == NULL)
  2062. return 0;
  2063. ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
  2064. if (ssid == NULL)
  2065. return 1;
  2066. for (i = 0; i < drv->num_filter_ssids; i++) {
  2067. if (ssid[1] == drv->filter_ssids[i].ssid_len &&
  2068. os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
  2069. 0)
  2070. return 0;
  2071. }
  2072. return 1;
  2073. }
  2074. static int bss_info_handler(struct nl_msg *msg, void *arg)
  2075. {
  2076. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  2077. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2078. struct nlattr *bss[NL80211_BSS_MAX + 1];
  2079. static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
  2080. [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
  2081. [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
  2082. [NL80211_BSS_TSF] = { .type = NLA_U64 },
  2083. [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
  2084. [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
  2085. [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
  2086. [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
  2087. [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
  2088. [NL80211_BSS_STATUS] = { .type = NLA_U32 },
  2089. [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
  2090. [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
  2091. };
  2092. struct nl80211_bss_info_arg *_arg = arg;
  2093. struct wpa_scan_results *res = _arg->res;
  2094. struct wpa_scan_res **tmp;
  2095. struct wpa_scan_res *r;
  2096. const u8 *ie, *beacon_ie;
  2097. size_t ie_len, beacon_ie_len;
  2098. u8 *pos;
  2099. size_t i;
  2100. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2101. genlmsg_attrlen(gnlh, 0), NULL);
  2102. if (!tb[NL80211_ATTR_BSS])
  2103. return NL_SKIP;
  2104. if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
  2105. bss_policy))
  2106. return NL_SKIP;
  2107. if (bss[NL80211_BSS_STATUS]) {
  2108. enum nl80211_bss_status status;
  2109. status = nla_get_u32(bss[NL80211_BSS_STATUS]);
  2110. if (status == NL80211_BSS_STATUS_ASSOCIATED &&
  2111. bss[NL80211_BSS_FREQUENCY]) {
  2112. _arg->assoc_freq =
  2113. nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
  2114. wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
  2115. _arg->assoc_freq);
  2116. }
  2117. }
  2118. if (!res)
  2119. return NL_SKIP;
  2120. if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
  2121. ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  2122. ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
  2123. } else {
  2124. ie = NULL;
  2125. ie_len = 0;
  2126. }
  2127. if (bss[NL80211_BSS_BEACON_IES]) {
  2128. beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
  2129. beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
  2130. } else {
  2131. beacon_ie = NULL;
  2132. beacon_ie_len = 0;
  2133. }
  2134. if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
  2135. ie ? ie_len : beacon_ie_len))
  2136. return NL_SKIP;
  2137. r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
  2138. if (r == NULL)
  2139. return NL_SKIP;
  2140. if (bss[NL80211_BSS_BSSID])
  2141. os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
  2142. ETH_ALEN);
  2143. if (bss[NL80211_BSS_FREQUENCY])
  2144. r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
  2145. if (bss[NL80211_BSS_BEACON_INTERVAL])
  2146. r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
  2147. if (bss[NL80211_BSS_CAPABILITY])
  2148. r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
  2149. r->flags |= WPA_SCAN_NOISE_INVALID;
  2150. if (bss[NL80211_BSS_SIGNAL_MBM]) {
  2151. r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
  2152. r->level /= 100; /* mBm to dBm */
  2153. r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
  2154. } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
  2155. r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
  2156. r->flags |= WPA_SCAN_LEVEL_INVALID;
  2157. } else
  2158. r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
  2159. if (bss[NL80211_BSS_TSF])
  2160. r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
  2161. if (bss[NL80211_BSS_SEEN_MS_AGO])
  2162. r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
  2163. r->ie_len = ie_len;
  2164. pos = (u8 *) (r + 1);
  2165. if (ie) {
  2166. os_memcpy(pos, ie, ie_len);
  2167. pos += ie_len;
  2168. }
  2169. r->beacon_ie_len = beacon_ie_len;
  2170. if (beacon_ie)
  2171. os_memcpy(pos, beacon_ie, beacon_ie_len);
  2172. if (bss[NL80211_BSS_STATUS]) {
  2173. enum nl80211_bss_status status;
  2174. status = nla_get_u32(bss[NL80211_BSS_STATUS]);
  2175. switch (status) {
  2176. case NL80211_BSS_STATUS_AUTHENTICATED:
  2177. r->flags |= WPA_SCAN_AUTHENTICATED;
  2178. break;
  2179. case NL80211_BSS_STATUS_ASSOCIATED:
  2180. r->flags |= WPA_SCAN_ASSOCIATED;
  2181. break;
  2182. default:
  2183. break;
  2184. }
  2185. }
  2186. /*
  2187. * cfg80211 maintains separate BSS table entries for APs if the same
  2188. * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
  2189. * not use frequency as a separate key in the BSS table, so filter out
  2190. * duplicated entries. Prefer associated BSS entry in such a case in
  2191. * order to get the correct frequency into the BSS table.
  2192. */
  2193. for (i = 0; i < res->num; i++) {
  2194. const u8 *s1, *s2;
  2195. if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
  2196. continue;
  2197. s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
  2198. res->res[i]->ie_len, WLAN_EID_SSID);
  2199. s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
  2200. if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
  2201. os_memcmp(s1, s2, 2 + s1[1]) != 0)
  2202. continue;
  2203. /* Same BSSID,SSID was already included in scan results */
  2204. wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
  2205. "for " MACSTR, MAC2STR(r->bssid));
  2206. if ((r->flags & WPA_SCAN_ASSOCIATED) &&
  2207. !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
  2208. os_free(res->res[i]);
  2209. res->res[i] = r;
  2210. } else
  2211. os_free(r);
  2212. return NL_SKIP;
  2213. }
  2214. tmp = os_realloc(res->res,
  2215. (res->num + 1) * sizeof(struct wpa_scan_res *));
  2216. if (tmp == NULL) {
  2217. os_free(r);
  2218. return NL_SKIP;
  2219. }
  2220. tmp[res->num++] = r;
  2221. res->res = tmp;
  2222. return NL_SKIP;
  2223. }
  2224. static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
  2225. const u8 *addr)
  2226. {
  2227. if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
  2228. wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
  2229. "mismatch (" MACSTR ")", MAC2STR(addr));
  2230. wpa_driver_nl80211_mlme(drv, addr,
  2231. NL80211_CMD_DEAUTHENTICATE,
  2232. WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
  2233. }
  2234. }
  2235. static void wpa_driver_nl80211_check_bss_status(
  2236. struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
  2237. {
  2238. size_t i;
  2239. for (i = 0; i < res->num; i++) {
  2240. struct wpa_scan_res *r = res->res[i];
  2241. if (r->flags & WPA_SCAN_AUTHENTICATED) {
  2242. wpa_printf(MSG_DEBUG, "nl80211: Scan results "
  2243. "indicates BSS status with " MACSTR
  2244. " as authenticated",
  2245. MAC2STR(r->bssid));
  2246. if (drv->nlmode == NL80211_IFTYPE_STATION &&
  2247. os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
  2248. os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
  2249. 0) {
  2250. wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
  2251. " in local state (auth=" MACSTR
  2252. " assoc=" MACSTR ")",
  2253. MAC2STR(drv->auth_bssid),
  2254. MAC2STR(drv->bssid));
  2255. clear_state_mismatch(drv, r->bssid);
  2256. }
  2257. }
  2258. if (r->flags & WPA_SCAN_ASSOCIATED) {
  2259. wpa_printf(MSG_DEBUG, "nl80211: Scan results "
  2260. "indicate BSS status with " MACSTR
  2261. " as associated",
  2262. MAC2STR(r->bssid));
  2263. if (drv->nlmode == NL80211_IFTYPE_STATION &&
  2264. !drv->associated) {
  2265. wpa_printf(MSG_DEBUG, "nl80211: Local state "
  2266. "(not associated) does not match "
  2267. "with BSS state");
  2268. clear_state_mismatch(drv, r->bssid);
  2269. } else if (drv->nlmode == NL80211_IFTYPE_STATION &&
  2270. os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
  2271. 0) {
  2272. wpa_printf(MSG_DEBUG, "nl80211: Local state "
  2273. "(associated with " MACSTR ") does "
  2274. "not match with BSS state",
  2275. MAC2STR(drv->bssid));
  2276. clear_state_mismatch(drv, r->bssid);
  2277. clear_state_mismatch(drv, drv->bssid);
  2278. }
  2279. }
  2280. }
  2281. }
  2282. static void wpa_scan_results_free(struct wpa_scan_results *res)
  2283. {
  2284. size_t i;
  2285. if (res == NULL)
  2286. return;
  2287. for (i = 0; i < res->num; i++)
  2288. os_free(res->res[i]);
  2289. os_free(res->res);
  2290. os_free(res);
  2291. }
  2292. static struct wpa_scan_results *
  2293. nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
  2294. {
  2295. struct nl_msg *msg;
  2296. struct wpa_scan_results *res;
  2297. int ret;
  2298. struct nl80211_bss_info_arg arg;
  2299. res = os_zalloc(sizeof(*res));
  2300. if (res == NULL)
  2301. return NULL;
  2302. msg = nlmsg_alloc();
  2303. if (!msg)
  2304. goto nla_put_failure;
  2305. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, NLM_F_DUMP,
  2306. NL80211_CMD_GET_SCAN, 0);
  2307. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2308. arg.drv = drv;
  2309. arg.res = res;
  2310. ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
  2311. msg = NULL;
  2312. if (ret == 0) {
  2313. wpa_printf(MSG_DEBUG, "Received scan results (%lu BSSes)",
  2314. (unsigned long) res->num);
  2315. return res;
  2316. }
  2317. wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
  2318. "(%s)", ret, strerror(-ret));
  2319. nla_put_failure:
  2320. nlmsg_free(msg);
  2321. wpa_scan_results_free(res);
  2322. return NULL;
  2323. }
  2324. /**
  2325. * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
  2326. * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
  2327. * Returns: Scan results on success, -1 on failure
  2328. */
  2329. static struct wpa_scan_results *
  2330. wpa_driver_nl80211_get_scan_results(void *priv)
  2331. {
  2332. struct i802_bss *bss = priv;
  2333. struct wpa_driver_nl80211_data *drv = bss->drv;
  2334. struct wpa_scan_results *res;
  2335. res = nl80211_get_scan_results(drv);
  2336. if (res)
  2337. wpa_driver_nl80211_check_bss_status(drv, res);
  2338. return res;
  2339. }
  2340. static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
  2341. {
  2342. struct wpa_scan_results *res;
  2343. size_t i;
  2344. res = nl80211_get_scan_results(drv);
  2345. if (res == NULL) {
  2346. wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
  2347. return;
  2348. }
  2349. wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
  2350. for (i = 0; i < res->num; i++) {
  2351. struct wpa_scan_res *r = res->res[i];
  2352. wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
  2353. (int) i, (int) res->num, MAC2STR(r->bssid),
  2354. r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
  2355. r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
  2356. }
  2357. wpa_scan_results_free(res);
  2358. }
  2359. static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
  2360. enum wpa_alg alg, const u8 *addr,
  2361. int key_idx, int set_tx,
  2362. const u8 *seq, size_t seq_len,
  2363. const u8 *key, size_t key_len)
  2364. {
  2365. struct i802_bss *bss = priv;
  2366. struct wpa_driver_nl80211_data *drv = bss->drv;
  2367. int ifindex = if_nametoindex(ifname);
  2368. struct nl_msg *msg;
  2369. int ret;
  2370. wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
  2371. "set_tx=%d seq_len=%lu key_len=%lu",
  2372. __func__, ifindex, alg, addr, key_idx, set_tx,
  2373. (unsigned long) seq_len, (unsigned long) key_len);
  2374. msg = nlmsg_alloc();
  2375. if (!msg)
  2376. return -ENOMEM;
  2377. if (alg == WPA_ALG_NONE) {
  2378. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2379. 0, NL80211_CMD_DEL_KEY, 0);
  2380. } else {
  2381. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2382. 0, NL80211_CMD_NEW_KEY, 0);
  2383. NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
  2384. switch (alg) {
  2385. case WPA_ALG_WEP:
  2386. if (key_len == 5)
  2387. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  2388. WLAN_CIPHER_SUITE_WEP40);
  2389. else
  2390. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  2391. WLAN_CIPHER_SUITE_WEP104);
  2392. break;
  2393. case WPA_ALG_TKIP:
  2394. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  2395. WLAN_CIPHER_SUITE_TKIP);
  2396. break;
  2397. case WPA_ALG_CCMP:
  2398. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  2399. WLAN_CIPHER_SUITE_CCMP);
  2400. break;
  2401. case WPA_ALG_IGTK:
  2402. NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
  2403. WLAN_CIPHER_SUITE_AES_CMAC);
  2404. break;
  2405. default:
  2406. wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
  2407. "algorithm %d", __func__, alg);
  2408. nlmsg_free(msg);
  2409. return -1;
  2410. }
  2411. }
  2412. if (seq && seq_len)
  2413. NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
  2414. if (addr && !is_broadcast_ether_addr(addr)) {
  2415. wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
  2416. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  2417. if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
  2418. wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
  2419. NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
  2420. NL80211_KEYTYPE_GROUP);
  2421. }
  2422. } else if (addr && is_broadcast_ether_addr(addr)) {
  2423. struct nl_msg *types;
  2424. int err;
  2425. wpa_printf(MSG_DEBUG, " broadcast key");
  2426. types = nlmsg_alloc();
  2427. if (!types)
  2428. goto nla_put_failure;
  2429. NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
  2430. err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
  2431. types);
  2432. nlmsg_free(types);
  2433. if (err)
  2434. goto nla_put_failure;
  2435. }
  2436. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  2437. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  2438. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2439. if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
  2440. ret = 0;
  2441. if (ret)
  2442. wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
  2443. ret, strerror(-ret));
  2444. /*
  2445. * If we failed or don't need to set the default TX key (below),
  2446. * we're done here.
  2447. */
  2448. if (ret || !set_tx || alg == WPA_ALG_NONE)
  2449. return ret;
  2450. if (drv->nlmode == NL80211_IFTYPE_AP && addr &&
  2451. !is_broadcast_ether_addr(addr))
  2452. return ret;
  2453. msg = nlmsg_alloc();
  2454. if (!msg)
  2455. return -ENOMEM;
  2456. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  2457. 0, NL80211_CMD_SET_KEY, 0);
  2458. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
  2459. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  2460. if (alg == WPA_ALG_IGTK)
  2461. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
  2462. else
  2463. NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
  2464. if (addr && is_broadcast_ether_addr(addr)) {
  2465. struct nl_msg *types;
  2466. int err;
  2467. types = nlmsg_alloc();
  2468. if (!types)
  2469. goto nla_put_failure;
  2470. NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
  2471. err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
  2472. types);
  2473. nlmsg_free(types);
  2474. if (err)
  2475. goto nla_put_failure;
  2476. } else if (addr) {
  2477. struct nl_msg *types;
  2478. int err;
  2479. types = nlmsg_alloc();
  2480. if (!types)
  2481. goto nla_put_failure;
  2482. NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST);
  2483. err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
  2484. types);
  2485. nlmsg_free(types);
  2486. if (err)
  2487. goto nla_put_failure;
  2488. }
  2489. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2490. if (ret == -ENOENT)
  2491. ret = 0;
  2492. if (ret)
  2493. wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
  2494. "err=%d %s)", ret, strerror(-ret));
  2495. return ret;
  2496. nla_put_failure:
  2497. return -ENOBUFS;
  2498. }
  2499. static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
  2500. int key_idx, int defkey,
  2501. const u8 *seq, size_t seq_len,
  2502. const u8 *key, size_t key_len)
  2503. {
  2504. struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
  2505. if (!key_attr)
  2506. return -1;
  2507. if (defkey && alg == WPA_ALG_IGTK)
  2508. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
  2509. else if (defkey)
  2510. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
  2511. NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
  2512. switch (alg) {
  2513. case WPA_ALG_WEP:
  2514. if (key_len == 5)
  2515. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  2516. WLAN_CIPHER_SUITE_WEP40);
  2517. else
  2518. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  2519. WLAN_CIPHER_SUITE_WEP104);
  2520. break;
  2521. case WPA_ALG_TKIP:
  2522. NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
  2523. break;
  2524. case WPA_ALG_CCMP:
  2525. NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
  2526. break;
  2527. case WPA_ALG_IGTK:
  2528. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  2529. WLAN_CIPHER_SUITE_AES_CMAC);
  2530. break;
  2531. default:
  2532. wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
  2533. "algorithm %d", __func__, alg);
  2534. return -1;
  2535. }
  2536. if (seq && seq_len)
  2537. NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
  2538. NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
  2539. nla_nest_end(msg, key_attr);
  2540. return 0;
  2541. nla_put_failure:
  2542. return -1;
  2543. }
  2544. static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
  2545. struct nl_msg *msg)
  2546. {
  2547. int i, privacy = 0;
  2548. struct nlattr *nl_keys, *nl_key;
  2549. for (i = 0; i < 4; i++) {
  2550. if (!params->wep_key[i])
  2551. continue;
  2552. privacy = 1;
  2553. break;
  2554. }
  2555. if (params->wps == WPS_MODE_PRIVACY)
  2556. privacy = 1;
  2557. if (params->pairwise_suite &&
  2558. params->pairwise_suite != WPA_CIPHER_NONE)
  2559. privacy = 1;
  2560. if (!privacy)
  2561. return 0;
  2562. NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
  2563. nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
  2564. if (!nl_keys)
  2565. goto nla_put_failure;
  2566. for (i = 0; i < 4; i++) {
  2567. if (!params->wep_key[i])
  2568. continue;
  2569. nl_key = nla_nest_start(msg, i);
  2570. if (!nl_key)
  2571. goto nla_put_failure;
  2572. NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
  2573. params->wep_key[i]);
  2574. if (params->wep_key_len[i] == 5)
  2575. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  2576. WLAN_CIPHER_SUITE_WEP40);
  2577. else
  2578. NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
  2579. WLAN_CIPHER_SUITE_WEP104);
  2580. NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
  2581. if (i == params->wep_tx_keyidx)
  2582. NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
  2583. nla_nest_end(msg, nl_key);
  2584. }
  2585. nla_nest_end(msg, nl_keys);
  2586. return 0;
  2587. nla_put_failure:
  2588. return -ENOBUFS;
  2589. }
  2590. static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  2591. const u8 *addr, int cmd, u16 reason_code,
  2592. int local_state_change)
  2593. {
  2594. int ret = -1;
  2595. struct nl_msg *msg;
  2596. msg = nlmsg_alloc();
  2597. if (!msg)
  2598. return -1;
  2599. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, cmd, 0);
  2600. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2601. NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
  2602. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  2603. if (local_state_change)
  2604. NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
  2605. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2606. msg = NULL;
  2607. if (ret) {
  2608. wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
  2609. "(%s)", ret, strerror(-ret));
  2610. goto nla_put_failure;
  2611. }
  2612. ret = 0;
  2613. nla_put_failure:
  2614. nlmsg_free(msg);
  2615. return ret;
  2616. }
  2617. static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
  2618. const u8 *addr, int reason_code)
  2619. {
  2620. wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
  2621. __func__, MAC2STR(addr), reason_code);
  2622. drv->associated = 0;
  2623. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT,
  2624. reason_code, 0);
  2625. }
  2626. static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
  2627. int reason_code)
  2628. {
  2629. struct i802_bss *bss = priv;
  2630. struct wpa_driver_nl80211_data *drv = bss->drv;
  2631. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
  2632. return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
  2633. wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
  2634. __func__, MAC2STR(addr), reason_code);
  2635. drv->associated = 0;
  2636. if (drv->nlmode == NL80211_IFTYPE_ADHOC)
  2637. return nl80211_leave_ibss(drv);
  2638. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
  2639. reason_code, 0);
  2640. }
  2641. static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
  2642. int reason_code)
  2643. {
  2644. struct i802_bss *bss = priv;
  2645. struct wpa_driver_nl80211_data *drv = bss->drv;
  2646. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
  2647. return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
  2648. wpa_printf(MSG_DEBUG, "%s", __func__);
  2649. drv->associated = 0;
  2650. return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
  2651. reason_code, 0);
  2652. }
  2653. static int wpa_driver_nl80211_authenticate(
  2654. void *priv, struct wpa_driver_auth_params *params)
  2655. {
  2656. struct i802_bss *bss = priv;
  2657. struct wpa_driver_nl80211_data *drv = bss->drv;
  2658. int ret = -1, i;
  2659. struct nl_msg *msg;
  2660. enum nl80211_auth_type type;
  2661. int count = 0;
  2662. drv->associated = 0;
  2663. os_memset(drv->auth_bssid, 0, ETH_ALEN);
  2664. /* FIX: IBSS mode */
  2665. if (drv->nlmode != NL80211_IFTYPE_STATION &&
  2666. wpa_driver_nl80211_set_mode(priv, IEEE80211_MODE_INFRA) < 0)
  2667. return -1;
  2668. retry:
  2669. msg = nlmsg_alloc();
  2670. if (!msg)
  2671. return -1;
  2672. wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
  2673. drv->ifindex);
  2674. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  2675. NL80211_CMD_AUTHENTICATE, 0);
  2676. for (i = 0; i < 4; i++) {
  2677. if (!params->wep_key[i])
  2678. continue;
  2679. wpa_driver_nl80211_set_key(bss->ifname, priv, WPA_ALG_WEP,
  2680. NULL, i,
  2681. i == params->wep_tx_keyidx, NULL, 0,
  2682. params->wep_key[i],
  2683. params->wep_key_len[i]);
  2684. if (params->wep_tx_keyidx != i)
  2685. continue;
  2686. if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
  2687. params->wep_key[i], params->wep_key_len[i])) {
  2688. nlmsg_free(msg);
  2689. return -1;
  2690. }
  2691. }
  2692. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  2693. if (params->bssid) {
  2694. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  2695. MAC2STR(params->bssid));
  2696. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  2697. }
  2698. if (params->freq) {
  2699. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  2700. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  2701. }
  2702. if (params->ssid) {
  2703. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  2704. params->ssid, params->ssid_len);
  2705. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  2706. params->ssid);
  2707. }
  2708. wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
  2709. if (params->ie)
  2710. NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
  2711. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  2712. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  2713. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  2714. type = NL80211_AUTHTYPE_SHARED_KEY;
  2715. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  2716. type = NL80211_AUTHTYPE_NETWORK_EAP;
  2717. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  2718. type = NL80211_AUTHTYPE_FT;
  2719. else
  2720. goto nla_put_failure;
  2721. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  2722. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
  2723. if (params->local_state_change) {
  2724. wpa_printf(MSG_DEBUG, " * Local state change only");
  2725. NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
  2726. }
  2727. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2728. msg = NULL;
  2729. if (ret) {
  2730. wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
  2731. "(%s)", ret, strerror(-ret));
  2732. count++;
  2733. if (ret == -EALREADY && count == 1 && params->bssid &&
  2734. !params->local_state_change) {
  2735. /*
  2736. * mac80211 does not currently accept new
  2737. * authentication if we are already authenticated. As a
  2738. * workaround, force deauthentication and try again.
  2739. */
  2740. wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
  2741. "after forced deauthentication");
  2742. wpa_driver_nl80211_deauthenticate(
  2743. bss, params->bssid,
  2744. WLAN_REASON_PREV_AUTH_NOT_VALID);
  2745. nlmsg_free(msg);
  2746. goto retry;
  2747. }
  2748. goto nla_put_failure;
  2749. }
  2750. ret = 0;
  2751. wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
  2752. "successfully");
  2753. nla_put_failure:
  2754. nlmsg_free(msg);
  2755. return ret;
  2756. }
  2757. struct phy_info_arg {
  2758. u16 *num_modes;
  2759. struct hostapd_hw_modes *modes;
  2760. };
  2761. static int phy_info_handler(struct nl_msg *msg, void *arg)
  2762. {
  2763. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  2764. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  2765. struct phy_info_arg *phy_info = arg;
  2766. struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
  2767. struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
  2768. static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  2769. [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
  2770. [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
  2771. [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
  2772. [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
  2773. [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
  2774. [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
  2775. };
  2776. struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
  2777. static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
  2778. [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
  2779. [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
  2780. };
  2781. struct nlattr *nl_band;
  2782. struct nlattr *nl_freq;
  2783. struct nlattr *nl_rate;
  2784. int rem_band, rem_freq, rem_rate;
  2785. struct hostapd_hw_modes *mode;
  2786. int idx, mode_is_set;
  2787. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  2788. genlmsg_attrlen(gnlh, 0), NULL);
  2789. if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
  2790. return NL_SKIP;
  2791. nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
  2792. mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
  2793. if (!mode)
  2794. return NL_SKIP;
  2795. phy_info->modes = mode;
  2796. mode_is_set = 0;
  2797. mode = &phy_info->modes[*(phy_info->num_modes)];
  2798. memset(mode, 0, sizeof(*mode));
  2799. *(phy_info->num_modes) += 1;
  2800. nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
  2801. nla_len(nl_band), NULL);
  2802. if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
  2803. mode->ht_capab = nla_get_u16(
  2804. tb_band[NL80211_BAND_ATTR_HT_CAPA]);
  2805. }
  2806. if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
  2807. mode->a_mpdu_params |= nla_get_u8(
  2808. tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
  2809. 0x03;
  2810. }
  2811. if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
  2812. mode->a_mpdu_params |= nla_get_u8(
  2813. tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
  2814. 2;
  2815. }
  2816. if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
  2817. nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
  2818. u8 *mcs;
  2819. mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
  2820. os_memcpy(mode->mcs_set, mcs, 16);
  2821. }
  2822. nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
  2823. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
  2824. nla_len(nl_freq), freq_policy);
  2825. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  2826. continue;
  2827. mode->num_channels++;
  2828. }
  2829. mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data));
  2830. if (!mode->channels)
  2831. return NL_SKIP;
  2832. idx = 0;
  2833. nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
  2834. nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
  2835. nla_len(nl_freq), freq_policy);
  2836. if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
  2837. continue;
  2838. mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
  2839. mode->channels[idx].flag = 0;
  2840. if (!mode_is_set) {
  2841. /* crude heuristic */
  2842. if (mode->channels[idx].freq < 4000)
  2843. mode->mode = HOSTAPD_MODE_IEEE80211B;
  2844. else
  2845. mode->mode = HOSTAPD_MODE_IEEE80211A;
  2846. mode_is_set = 1;
  2847. }
  2848. /* crude heuristic */
  2849. if (mode->channels[idx].freq < 4000)
  2850. if (mode->channels[idx].freq == 2484)
  2851. mode->channels[idx].chan = 14;
  2852. else
  2853. mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
  2854. else
  2855. mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
  2856. if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  2857. mode->channels[idx].flag |=
  2858. HOSTAPD_CHAN_DISABLED;
  2859. if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
  2860. mode->channels[idx].flag |=
  2861. HOSTAPD_CHAN_PASSIVE_SCAN;
  2862. if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
  2863. mode->channels[idx].flag |=
  2864. HOSTAPD_CHAN_NO_IBSS;
  2865. if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
  2866. mode->channels[idx].flag |=
  2867. HOSTAPD_CHAN_RADAR;
  2868. if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
  2869. !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
  2870. mode->channels[idx].max_tx_power =
  2871. nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
  2872. idx++;
  2873. }
  2874. nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
  2875. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
  2876. nla_len(nl_rate), rate_policy);
  2877. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  2878. continue;
  2879. mode->num_rates++;
  2880. }
  2881. mode->rates = os_zalloc(mode->num_rates * sizeof(int));
  2882. if (!mode->rates)
  2883. return NL_SKIP;
  2884. idx = 0;
  2885. nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
  2886. nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
  2887. nla_len(nl_rate), rate_policy);
  2888. if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
  2889. continue;
  2890. mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
  2891. /* crude heuristic */
  2892. if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
  2893. mode->rates[idx] > 200)
  2894. mode->mode = HOSTAPD_MODE_IEEE80211G;
  2895. idx++;
  2896. }
  2897. }
  2898. return NL_SKIP;
  2899. }
  2900. static struct hostapd_hw_modes *
  2901. wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
  2902. {
  2903. u16 m;
  2904. struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
  2905. int i, mode11g_idx = -1;
  2906. /* If only 802.11g mode is included, use it to construct matching
  2907. * 802.11b mode data. */
  2908. for (m = 0; m < *num_modes; m++) {
  2909. if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
  2910. return modes; /* 802.11b already included */
  2911. if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
  2912. mode11g_idx = m;
  2913. }
  2914. if (mode11g_idx < 0)
  2915. return modes; /* 2.4 GHz band not supported at all */
  2916. nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
  2917. if (nmodes == NULL)
  2918. return modes; /* Could not add 802.11b mode */
  2919. mode = &nmodes[*num_modes];
  2920. os_memset(mode, 0, sizeof(*mode));
  2921. (*num_modes)++;
  2922. modes = nmodes;
  2923. mode->mode = HOSTAPD_MODE_IEEE80211B;
  2924. mode11g = &modes[mode11g_idx];
  2925. mode->num_channels = mode11g->num_channels;
  2926. mode->channels = os_malloc(mode11g->num_channels *
  2927. sizeof(struct hostapd_channel_data));
  2928. if (mode->channels == NULL) {
  2929. (*num_modes)--;
  2930. return modes; /* Could not add 802.11b mode */
  2931. }
  2932. os_memcpy(mode->channels, mode11g->channels,
  2933. mode11g->num_channels * sizeof(struct hostapd_channel_data));
  2934. mode->num_rates = 0;
  2935. mode->rates = os_malloc(4 * sizeof(int));
  2936. if (mode->rates == NULL) {
  2937. os_free(mode->channels);
  2938. (*num_modes)--;
  2939. return modes; /* Could not add 802.11b mode */
  2940. }
  2941. for (i = 0; i < mode11g->num_rates; i++) {
  2942. if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
  2943. mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
  2944. continue;
  2945. mode->rates[mode->num_rates] = mode11g->rates[i];
  2946. mode->num_rates++;
  2947. if (mode->num_rates == 4)
  2948. break;
  2949. }
  2950. if (mode->num_rates == 0) {
  2951. os_free(mode->channels);
  2952. os_free(mode->rates);
  2953. (*num_modes)--;
  2954. return modes; /* No 802.11b rates */
  2955. }
  2956. wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
  2957. "information");
  2958. return modes;
  2959. }
  2960. static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
  2961. int end)
  2962. {
  2963. int c;
  2964. for (c = 0; c < mode->num_channels; c++) {
  2965. struct hostapd_channel_data *chan = &mode->channels[c];
  2966. if (chan->freq - 10 >= start && chan->freq + 10 <= end)
  2967. chan->flag |= HOSTAPD_CHAN_HT40;
  2968. }
  2969. }
  2970. static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
  2971. int end)
  2972. {
  2973. int c;
  2974. for (c = 0; c < mode->num_channels; c++) {
  2975. struct hostapd_channel_data *chan = &mode->channels[c];
  2976. if (!(chan->flag & HOSTAPD_CHAN_HT40))
  2977. continue;
  2978. if (chan->freq - 30 >= start && chan->freq - 10 <= end)
  2979. chan->flag |= HOSTAPD_CHAN_HT40MINUS;
  2980. if (chan->freq + 10 >= start && chan->freq + 30 <= end)
  2981. chan->flag |= HOSTAPD_CHAN_HT40PLUS;
  2982. }
  2983. }
  2984. static void nl80211_reg_rule_ht40(struct nlattr *tb[],
  2985. struct phy_info_arg *results)
  2986. {
  2987. u32 start, end, max_bw;
  2988. u16 m;
  2989. if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
  2990. tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
  2991. tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
  2992. return;
  2993. start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
  2994. end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
  2995. max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
  2996. wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
  2997. start, end, max_bw);
  2998. if (max_bw < 40)
  2999. return;
  3000. for (m = 0; m < *results->num_modes; m++) {
  3001. if (!(results->modes[m].ht_capab &
  3002. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  3003. continue;
  3004. nl80211_set_ht40_mode(&results->modes[m], start, end);
  3005. }
  3006. }
  3007. static void nl80211_reg_rule_sec(struct nlattr *tb[],
  3008. struct phy_info_arg *results)
  3009. {
  3010. u32 start, end, max_bw;
  3011. u16 m;
  3012. if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
  3013. tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
  3014. tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
  3015. return;
  3016. start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
  3017. end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
  3018. max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
  3019. if (max_bw < 20)
  3020. return;
  3021. for (m = 0; m < *results->num_modes; m++) {
  3022. if (!(results->modes[m].ht_capab &
  3023. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
  3024. continue;
  3025. nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
  3026. }
  3027. }
  3028. static int nl80211_get_reg(struct nl_msg *msg, void *arg)
  3029. {
  3030. struct phy_info_arg *results = arg;
  3031. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  3032. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  3033. struct nlattr *nl_rule;
  3034. struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
  3035. int rem_rule;
  3036. static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
  3037. [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
  3038. [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
  3039. [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
  3040. [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
  3041. [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
  3042. [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
  3043. };
  3044. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  3045. genlmsg_attrlen(gnlh, 0), NULL);
  3046. if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
  3047. !tb_msg[NL80211_ATTR_REG_RULES]) {
  3048. wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
  3049. "available");
  3050. return NL_SKIP;
  3051. }
  3052. wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
  3053. (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
  3054. nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
  3055. {
  3056. nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
  3057. nla_data(nl_rule), nla_len(nl_rule), reg_policy);
  3058. nl80211_reg_rule_ht40(tb_rule, results);
  3059. }
  3060. nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
  3061. {
  3062. nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
  3063. nla_data(nl_rule), nla_len(nl_rule), reg_policy);
  3064. nl80211_reg_rule_sec(tb_rule, results);
  3065. }
  3066. return NL_SKIP;
  3067. }
  3068. static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
  3069. struct phy_info_arg *results)
  3070. {
  3071. struct nl_msg *msg;
  3072. msg = nlmsg_alloc();
  3073. if (!msg)
  3074. return -ENOMEM;
  3075. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3076. 0, NL80211_CMD_GET_REG, 0);
  3077. return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
  3078. }
  3079. static struct hostapd_hw_modes *
  3080. wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
  3081. {
  3082. struct i802_bss *bss = priv;
  3083. struct wpa_driver_nl80211_data *drv = bss->drv;
  3084. struct nl_msg *msg;
  3085. struct phy_info_arg result = {
  3086. .num_modes = num_modes,
  3087. .modes = NULL,
  3088. };
  3089. *num_modes = 0;
  3090. *flags = 0;
  3091. msg = nlmsg_alloc();
  3092. if (!msg)
  3093. return NULL;
  3094. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3095. 0, NL80211_CMD_GET_WIPHY, 0);
  3096. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3097. if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
  3098. nl80211_set_ht40_flags(drv, &result);
  3099. return wpa_driver_nl80211_add_11b(result.modes, num_modes);
  3100. }
  3101. nla_put_failure:
  3102. return NULL;
  3103. }
  3104. static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data *drv,
  3105. const void *data, size_t len,
  3106. int encrypt)
  3107. {
  3108. __u8 rtap_hdr[] = {
  3109. 0x00, 0x00, /* radiotap version */
  3110. 0x0e, 0x00, /* radiotap length */
  3111. 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
  3112. IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
  3113. 0x00, /* padding */
  3114. 0x00, 0x00, /* RX and TX flags to indicate that */
  3115. 0x00, 0x00, /* this is the injected frame directly */
  3116. };
  3117. struct iovec iov[2] = {
  3118. {
  3119. .iov_base = &rtap_hdr,
  3120. .iov_len = sizeof(rtap_hdr),
  3121. },
  3122. {
  3123. .iov_base = (void *) data,
  3124. .iov_len = len,
  3125. }
  3126. };
  3127. struct msghdr msg = {
  3128. .msg_name = NULL,
  3129. .msg_namelen = 0,
  3130. .msg_iov = iov,
  3131. .msg_iovlen = 2,
  3132. .msg_control = NULL,
  3133. .msg_controllen = 0,
  3134. .msg_flags = 0,
  3135. };
  3136. int res;
  3137. if (encrypt)
  3138. rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
  3139. if (drv->monitor_sock < 0) {
  3140. wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
  3141. "for %s", __func__);
  3142. return -1;
  3143. }
  3144. res = sendmsg(drv->monitor_sock, &msg, 0);
  3145. if (res < 0) {
  3146. wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
  3147. return -1;
  3148. }
  3149. return 0;
  3150. }
  3151. static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
  3152. size_t data_len)
  3153. {
  3154. struct i802_bss *bss = priv;
  3155. struct wpa_driver_nl80211_data *drv = bss->drv;
  3156. struct ieee80211_mgmt *mgmt;
  3157. int encrypt = 1;
  3158. u16 fc;
  3159. mgmt = (struct ieee80211_mgmt *) data;
  3160. fc = le_to_host16(mgmt->frame_control);
  3161. if (drv->nlmode == NL80211_IFTYPE_STATION &&
  3162. WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  3163. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
  3164. /*
  3165. * The use of last_mgmt_freq is a bit of a hack,
  3166. * but it works due to the single-threaded nature
  3167. * of wpa_supplicant.
  3168. */
  3169. return nl80211_send_frame_cmd(drv, drv->last_mgmt_freq, 0,
  3170. data, data_len, NULL);
  3171. }
  3172. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  3173. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
  3174. /*
  3175. * Only one of the authentication frame types is encrypted.
  3176. * In order for static WEP encryption to work properly (i.e.,
  3177. * to not encrypt the frame), we need to tell mac80211 about
  3178. * the frames that must not be encrypted.
  3179. */
  3180. u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  3181. u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
  3182. if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
  3183. encrypt = 0;
  3184. }
  3185. return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
  3186. }
  3187. static int wpa_driver_nl80211_set_beacon(void *priv,
  3188. const u8 *head, size_t head_len,
  3189. const u8 *tail, size_t tail_len,
  3190. int dtim_period, int beacon_int)
  3191. {
  3192. struct i802_bss *bss = priv;
  3193. struct wpa_driver_nl80211_data *drv = bss->drv;
  3194. struct nl_msg *msg;
  3195. u8 cmd = NL80211_CMD_NEW_BEACON;
  3196. int ret;
  3197. int beacon_set;
  3198. int ifindex = if_nametoindex(bss->ifname);
  3199. beacon_set = bss->beacon_set;
  3200. msg = nlmsg_alloc();
  3201. if (!msg)
  3202. return -ENOMEM;
  3203. wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
  3204. beacon_set);
  3205. if (beacon_set)
  3206. cmd = NL80211_CMD_SET_BEACON;
  3207. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3208. 0, cmd, 0);
  3209. NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
  3210. NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
  3211. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  3212. NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, beacon_int);
  3213. NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
  3214. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3215. if (ret) {
  3216. wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
  3217. ret, strerror(-ret));
  3218. } else {
  3219. bss->beacon_set = 1;
  3220. }
  3221. return ret;
  3222. nla_put_failure:
  3223. return -ENOBUFS;
  3224. }
  3225. static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data *drv,
  3226. int freq, int ht_enabled,
  3227. int sec_channel_offset)
  3228. {
  3229. struct nl_msg *msg;
  3230. int ret;
  3231. msg = nlmsg_alloc();
  3232. if (!msg)
  3233. return -1;
  3234. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3235. NL80211_CMD_SET_WIPHY, 0);
  3236. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3237. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  3238. if (ht_enabled) {
  3239. switch (sec_channel_offset) {
  3240. case -1:
  3241. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  3242. NL80211_CHAN_HT40MINUS);
  3243. break;
  3244. case 1:
  3245. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  3246. NL80211_CHAN_HT40PLUS);
  3247. break;
  3248. default:
  3249. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
  3250. NL80211_CHAN_HT20);
  3251. break;
  3252. }
  3253. }
  3254. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3255. if (ret == 0)
  3256. return 0;
  3257. wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
  3258. "%d (%s)", freq, ret, strerror(-ret));
  3259. nla_put_failure:
  3260. return -1;
  3261. }
  3262. static u32 sta_flags_nl80211(int flags)
  3263. {
  3264. u32 f = 0;
  3265. if (flags & WPA_STA_AUTHORIZED)
  3266. f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
  3267. if (flags & WPA_STA_WMM)
  3268. f |= BIT(NL80211_STA_FLAG_WME);
  3269. if (flags & WPA_STA_SHORT_PREAMBLE)
  3270. f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
  3271. if (flags & WPA_STA_MFP)
  3272. f |= BIT(NL80211_STA_FLAG_MFP);
  3273. return f;
  3274. }
  3275. static int wpa_driver_nl80211_sta_add(void *priv,
  3276. struct hostapd_sta_add_params *params)
  3277. {
  3278. struct i802_bss *bss = priv;
  3279. struct wpa_driver_nl80211_data *drv = bss->drv;
  3280. struct nl_msg *msg;
  3281. struct nl80211_sta_flag_update upd;
  3282. int ret = -ENOBUFS;
  3283. msg = nlmsg_alloc();
  3284. if (!msg)
  3285. return -ENOMEM;
  3286. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3287. 0, NL80211_CMD_NEW_STATION, 0);
  3288. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  3289. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
  3290. NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
  3291. NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
  3292. params->supp_rates);
  3293. NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
  3294. params->listen_interval);
  3295. if (params->ht_capabilities) {
  3296. NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
  3297. sizeof(*params->ht_capabilities),
  3298. params->ht_capabilities);
  3299. }
  3300. os_memset(&upd, 0, sizeof(upd));
  3301. upd.mask = sta_flags_nl80211(params->flags);
  3302. upd.set = upd.mask;
  3303. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  3304. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3305. if (ret)
  3306. wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION "
  3307. "result: %d (%s)", ret, strerror(-ret));
  3308. if (ret == -EEXIST)
  3309. ret = 0;
  3310. nla_put_failure:
  3311. return ret;
  3312. }
  3313. static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
  3314. {
  3315. struct i802_bss *bss = priv;
  3316. struct wpa_driver_nl80211_data *drv = bss->drv;
  3317. struct nl_msg *msg;
  3318. int ret;
  3319. msg = nlmsg_alloc();
  3320. if (!msg)
  3321. return -ENOMEM;
  3322. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3323. 0, NL80211_CMD_DEL_STATION, 0);
  3324. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  3325. if_nametoindex(bss->ifname));
  3326. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  3327. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3328. if (ret == -ENOENT)
  3329. return 0;
  3330. return ret;
  3331. nla_put_failure:
  3332. return -ENOBUFS;
  3333. }
  3334. static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
  3335. int ifidx)
  3336. {
  3337. struct nl_msg *msg;
  3338. wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
  3339. #ifdef HOSTAPD
  3340. /* stop listening for EAPOL on this interface */
  3341. del_ifidx(drv, ifidx);
  3342. #endif /* HOSTAPD */
  3343. msg = nlmsg_alloc();
  3344. if (!msg)
  3345. goto nla_put_failure;
  3346. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3347. 0, NL80211_CMD_DEL_INTERFACE, 0);
  3348. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
  3349. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  3350. return;
  3351. nla_put_failure:
  3352. wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
  3353. }
  3354. static const char * nl80211_iftype_str(enum nl80211_iftype mode)
  3355. {
  3356. switch (mode) {
  3357. case NL80211_IFTYPE_ADHOC:
  3358. return "ADHOC";
  3359. case NL80211_IFTYPE_STATION:
  3360. return "STATION";
  3361. case NL80211_IFTYPE_AP:
  3362. return "AP";
  3363. case NL80211_IFTYPE_MONITOR:
  3364. return "MONITOR";
  3365. case NL80211_IFTYPE_P2P_CLIENT:
  3366. return "P2P_CLIENT";
  3367. case NL80211_IFTYPE_P2P_GO:
  3368. return "P2P_GO";
  3369. default:
  3370. return "unknown";
  3371. }
  3372. }
  3373. static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
  3374. const char *ifname,
  3375. enum nl80211_iftype iftype,
  3376. const u8 *addr, int wds)
  3377. {
  3378. struct nl_msg *msg, *flags = NULL;
  3379. int ifidx;
  3380. int ret = -ENOBUFS;
  3381. wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
  3382. iftype, nl80211_iftype_str(iftype));
  3383. msg = nlmsg_alloc();
  3384. if (!msg)
  3385. return -1;
  3386. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3387. 0, NL80211_CMD_NEW_INTERFACE, 0);
  3388. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3389. NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
  3390. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
  3391. if (iftype == NL80211_IFTYPE_MONITOR) {
  3392. int err;
  3393. flags = nlmsg_alloc();
  3394. if (!flags)
  3395. goto nla_put_failure;
  3396. NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
  3397. err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
  3398. nlmsg_free(flags);
  3399. if (err)
  3400. goto nla_put_failure;
  3401. } else if (wds) {
  3402. NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
  3403. }
  3404. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3405. if (ret) {
  3406. nla_put_failure:
  3407. wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
  3408. ifname, ret, strerror(-ret));
  3409. return ret;
  3410. }
  3411. ifidx = if_nametoindex(ifname);
  3412. wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
  3413. ifname, ifidx);
  3414. if (ifidx <= 0)
  3415. return -1;
  3416. #ifdef HOSTAPD
  3417. /* start listening for EAPOL on this interface */
  3418. add_ifidx(drv, ifidx);
  3419. #endif /* HOSTAPD */
  3420. if (addr && iftype != NL80211_IFTYPE_MONITOR &&
  3421. linux_set_ifhwaddr(drv->ioctl_sock, ifname, addr)) {
  3422. nl80211_remove_iface(drv, ifidx);
  3423. return -1;
  3424. }
  3425. return ifidx;
  3426. }
  3427. static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
  3428. const char *ifname, enum nl80211_iftype iftype,
  3429. const u8 *addr, int wds)
  3430. {
  3431. int ret;
  3432. ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
  3433. /* if error occured and interface exists already */
  3434. if (ret == -ENFILE && if_nametoindex(ifname)) {
  3435. wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
  3436. /* Try to remove the interface that was already there. */
  3437. nl80211_remove_iface(drv, if_nametoindex(ifname));
  3438. /* Try to create the interface again */
  3439. ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
  3440. wds);
  3441. }
  3442. if (ret >= 0 && drv->disable_11b_rates)
  3443. nl80211_disable_11b_rates(drv, ret, 1);
  3444. return ret;
  3445. }
  3446. static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
  3447. {
  3448. struct ieee80211_hdr *hdr;
  3449. u16 fc;
  3450. union wpa_event_data event;
  3451. hdr = (struct ieee80211_hdr *) buf;
  3452. fc = le_to_host16(hdr->frame_control);
  3453. os_memset(&event, 0, sizeof(event));
  3454. event.tx_status.type = WLAN_FC_GET_TYPE(fc);
  3455. event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
  3456. event.tx_status.dst = hdr->addr1;
  3457. event.tx_status.data = buf;
  3458. event.tx_status.data_len = len;
  3459. event.tx_status.ack = ok;
  3460. wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
  3461. }
  3462. static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
  3463. u8 *buf, size_t len)
  3464. {
  3465. union wpa_event_data event;
  3466. os_memset(&event, 0, sizeof(event));
  3467. event.rx_from_unknown.frame = buf;
  3468. event.rx_from_unknown.len = len;
  3469. wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
  3470. }
  3471. static void handle_frame(struct wpa_driver_nl80211_data *drv,
  3472. u8 *buf, size_t len, int datarate, int ssi_signal)
  3473. {
  3474. struct ieee80211_hdr *hdr;
  3475. u16 fc;
  3476. union wpa_event_data event;
  3477. hdr = (struct ieee80211_hdr *) buf;
  3478. fc = le_to_host16(hdr->frame_control);
  3479. switch (WLAN_FC_GET_TYPE(fc)) {
  3480. case WLAN_FC_TYPE_MGMT:
  3481. os_memset(&event, 0, sizeof(event));
  3482. event.rx_mgmt.frame = buf;
  3483. event.rx_mgmt.frame_len = len;
  3484. event.rx_mgmt.datarate = datarate;
  3485. event.rx_mgmt.ssi_signal = ssi_signal;
  3486. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  3487. break;
  3488. case WLAN_FC_TYPE_CTRL:
  3489. /* can only get here with PS-Poll frames */
  3490. wpa_printf(MSG_DEBUG, "CTRL");
  3491. from_unknown_sta(drv, buf, len);
  3492. break;
  3493. case WLAN_FC_TYPE_DATA:
  3494. from_unknown_sta(drv, buf, len);
  3495. break;
  3496. }
  3497. }
  3498. static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
  3499. {
  3500. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  3501. int len;
  3502. unsigned char buf[3000];
  3503. struct ieee80211_radiotap_iterator iter;
  3504. int ret;
  3505. int datarate = 0, ssi_signal = 0;
  3506. int injected = 0, failed = 0, rxflags = 0;
  3507. len = recv(sock, buf, sizeof(buf), 0);
  3508. if (len < 0) {
  3509. perror("recv");
  3510. return;
  3511. }
  3512. if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
  3513. printf("received invalid radiotap frame\n");
  3514. return;
  3515. }
  3516. while (1) {
  3517. ret = ieee80211_radiotap_iterator_next(&iter);
  3518. if (ret == -ENOENT)
  3519. break;
  3520. if (ret) {
  3521. printf("received invalid radiotap frame (%d)\n", ret);
  3522. return;
  3523. }
  3524. switch (iter.this_arg_index) {
  3525. case IEEE80211_RADIOTAP_FLAGS:
  3526. if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
  3527. len -= 4;
  3528. break;
  3529. case IEEE80211_RADIOTAP_RX_FLAGS:
  3530. rxflags = 1;
  3531. break;
  3532. case IEEE80211_RADIOTAP_TX_FLAGS:
  3533. injected = 1;
  3534. failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
  3535. IEEE80211_RADIOTAP_F_TX_FAIL;
  3536. break;
  3537. case IEEE80211_RADIOTAP_DATA_RETRIES:
  3538. break;
  3539. case IEEE80211_RADIOTAP_CHANNEL:
  3540. /* TODO: convert from freq/flags to channel number */
  3541. break;
  3542. case IEEE80211_RADIOTAP_RATE:
  3543. datarate = *iter.this_arg * 5;
  3544. break;
  3545. case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
  3546. ssi_signal = *iter.this_arg;
  3547. break;
  3548. }
  3549. }
  3550. if (rxflags && injected)
  3551. return;
  3552. if (!injected)
  3553. handle_frame(drv, buf + iter.max_length,
  3554. len - iter.max_length, datarate, ssi_signal);
  3555. else
  3556. handle_tx_callback(drv->ctx, buf + iter.max_length,
  3557. len - iter.max_length, !failed);
  3558. }
  3559. /*
  3560. * we post-process the filter code later and rewrite
  3561. * this to the offset to the last instruction
  3562. */
  3563. #define PASS 0xFF
  3564. #define FAIL 0xFE
  3565. static struct sock_filter msock_filter_insns[] = {
  3566. /*
  3567. * do a little-endian load of the radiotap length field
  3568. */
  3569. /* load lower byte into A */
  3570. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
  3571. /* put it into X (== index register) */
  3572. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  3573. /* load upper byte into A */
  3574. BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
  3575. /* left-shift it by 8 */
  3576. BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
  3577. /* or with X */
  3578. BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
  3579. /* put result into X */
  3580. BPF_STMT(BPF_MISC| BPF_TAX, 0),
  3581. /*
  3582. * Allow management frames through, this also gives us those
  3583. * management frames that we sent ourselves with status
  3584. */
  3585. /* load the lower byte of the IEEE 802.11 frame control field */
  3586. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  3587. /* mask off frame type and version */
  3588. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
  3589. /* accept frame if it's both 0, fall through otherwise */
  3590. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
  3591. /*
  3592. * TODO: add a bit to radiotap RX flags that indicates
  3593. * that the sending station is not associated, then
  3594. * add a filter here that filters on our DA and that flag
  3595. * to allow us to deauth frames to that bad station.
  3596. *
  3597. * For now allow all To DS data frames through.
  3598. */
  3599. /* load the IEEE 802.11 frame control field */
  3600. BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
  3601. /* mask off frame type, version and DS status */
  3602. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
  3603. /* accept frame if version 0, type 2 and To DS, fall through otherwise
  3604. */
  3605. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
  3606. #if 0
  3607. /*
  3608. * drop non-data frames
  3609. */
  3610. /* load the lower byte of the frame control field */
  3611. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  3612. /* mask off QoS bit */
  3613. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
  3614. /* drop non-data frames */
  3615. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
  3616. #endif
  3617. /* load the upper byte of the frame control field */
  3618. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
  3619. /* mask off toDS/fromDS */
  3620. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
  3621. /* accept WDS frames */
  3622. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
  3623. /*
  3624. * add header length to index
  3625. */
  3626. /* load the lower byte of the frame control field */
  3627. BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
  3628. /* mask off QoS bit */
  3629. BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
  3630. /* right shift it by 6 to give 0 or 2 */
  3631. BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
  3632. /* add data frame header length */
  3633. BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
  3634. /* add index, was start of 802.11 header */
  3635. BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
  3636. /* move to index, now start of LL header */
  3637. BPF_STMT(BPF_MISC | BPF_TAX, 0),
  3638. /*
  3639. * Accept empty data frames, we use those for
  3640. * polling activity.
  3641. */
  3642. BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
  3643. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
  3644. /*
  3645. * Accept EAPOL frames
  3646. */
  3647. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
  3648. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
  3649. BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
  3650. BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
  3651. /* keep these last two statements or change the code below */
  3652. /* return 0 == "DROP" */
  3653. BPF_STMT(BPF_RET | BPF_K, 0),
  3654. /* return ~0 == "keep all" */
  3655. BPF_STMT(BPF_RET | BPF_K, ~0),
  3656. };
  3657. static struct sock_fprog msock_filter = {
  3658. .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
  3659. .filter = msock_filter_insns,
  3660. };
  3661. static int add_monitor_filter(int s)
  3662. {
  3663. int idx;
  3664. /* rewrite all PASS/FAIL jump offsets */
  3665. for (idx = 0; idx < msock_filter.len; idx++) {
  3666. struct sock_filter *insn = &msock_filter_insns[idx];
  3667. if (BPF_CLASS(insn->code) == BPF_JMP) {
  3668. if (insn->code == (BPF_JMP|BPF_JA)) {
  3669. if (insn->k == PASS)
  3670. insn->k = msock_filter.len - idx - 2;
  3671. else if (insn->k == FAIL)
  3672. insn->k = msock_filter.len - idx - 3;
  3673. }
  3674. if (insn->jt == PASS)
  3675. insn->jt = msock_filter.len - idx - 2;
  3676. else if (insn->jt == FAIL)
  3677. insn->jt = msock_filter.len - idx - 3;
  3678. if (insn->jf == PASS)
  3679. insn->jf = msock_filter.len - idx - 2;
  3680. else if (insn->jf == FAIL)
  3681. insn->jf = msock_filter.len - idx - 3;
  3682. }
  3683. }
  3684. if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
  3685. &msock_filter, sizeof(msock_filter))) {
  3686. perror("SO_ATTACH_FILTER");
  3687. return -1;
  3688. }
  3689. return 0;
  3690. }
  3691. static void nl80211_remove_monitor_interface(
  3692. struct wpa_driver_nl80211_data *drv)
  3693. {
  3694. if (drv->monitor_ifidx >= 0) {
  3695. nl80211_remove_iface(drv, drv->monitor_ifidx);
  3696. drv->monitor_ifidx = -1;
  3697. }
  3698. if (drv->monitor_sock >= 0) {
  3699. eloop_unregister_read_sock(drv->monitor_sock);
  3700. close(drv->monitor_sock);
  3701. drv->monitor_sock = -1;
  3702. }
  3703. }
  3704. static int
  3705. nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
  3706. {
  3707. char buf[IFNAMSIZ];
  3708. struct sockaddr_ll ll;
  3709. int optval;
  3710. socklen_t optlen;
  3711. snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
  3712. buf[IFNAMSIZ - 1] = '\0';
  3713. drv->monitor_ifidx =
  3714. nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
  3715. 0);
  3716. if (drv->monitor_ifidx == -EOPNOTSUPP) {
  3717. wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
  3718. "monitor interface type - try to run without it");
  3719. drv->no_monitor_iface_capab = 1;
  3720. }
  3721. if (drv->monitor_ifidx < 0)
  3722. return -1;
  3723. if (linux_set_iface_flags(drv->ioctl_sock, buf, 1))
  3724. goto error;
  3725. memset(&ll, 0, sizeof(ll));
  3726. ll.sll_family = AF_PACKET;
  3727. ll.sll_ifindex = drv->monitor_ifidx;
  3728. drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  3729. if (drv->monitor_sock < 0) {
  3730. perror("socket[PF_PACKET,SOCK_RAW]");
  3731. goto error;
  3732. }
  3733. if (add_monitor_filter(drv->monitor_sock)) {
  3734. wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
  3735. "interface; do filtering in user space");
  3736. /* This works, but will cost in performance. */
  3737. }
  3738. if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
  3739. perror("monitor socket bind");
  3740. goto error;
  3741. }
  3742. optlen = sizeof(optval);
  3743. optval = 20;
  3744. if (setsockopt
  3745. (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
  3746. perror("Failed to set socket priority");
  3747. goto error;
  3748. }
  3749. if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
  3750. drv, NULL)) {
  3751. printf("Could not register monitor read socket\n");
  3752. goto error;
  3753. }
  3754. return 0;
  3755. error:
  3756. nl80211_remove_monitor_interface(drv);
  3757. return -1;
  3758. }
  3759. static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  3760. static int wpa_driver_nl80211_hapd_send_eapol(
  3761. void *priv, const u8 *addr, const u8 *data,
  3762. size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
  3763. {
  3764. struct i802_bss *bss = priv;
  3765. struct wpa_driver_nl80211_data *drv = bss->drv;
  3766. struct ieee80211_hdr *hdr;
  3767. size_t len;
  3768. u8 *pos;
  3769. int res;
  3770. int qos = flags & WPA_STA_WMM;
  3771. len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
  3772. data_len;
  3773. hdr = os_zalloc(len);
  3774. if (hdr == NULL) {
  3775. printf("malloc() failed for i802_send_data(len=%lu)\n",
  3776. (unsigned long) len);
  3777. return -1;
  3778. }
  3779. hdr->frame_control =
  3780. IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
  3781. hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
  3782. if (encrypt)
  3783. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  3784. if (qos) {
  3785. hdr->frame_control |=
  3786. host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
  3787. }
  3788. memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  3789. memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  3790. memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  3791. pos = (u8 *) (hdr + 1);
  3792. if (qos) {
  3793. /* add an empty QoS header if needed */
  3794. pos[0] = 0;
  3795. pos[1] = 0;
  3796. pos += 2;
  3797. }
  3798. memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
  3799. pos += sizeof(rfc1042_header);
  3800. WPA_PUT_BE16(pos, ETH_P_PAE);
  3801. pos += 2;
  3802. memcpy(pos, data, data_len);
  3803. res = wpa_driver_nl80211_send_frame(drv, (u8 *) hdr, len, encrypt);
  3804. if (res < 0) {
  3805. wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
  3806. "failed: %d (%s)",
  3807. (unsigned long) len, errno, strerror(errno));
  3808. }
  3809. os_free(hdr);
  3810. return res;
  3811. }
  3812. static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
  3813. int total_flags,
  3814. int flags_or, int flags_and)
  3815. {
  3816. struct i802_bss *bss = priv;
  3817. struct wpa_driver_nl80211_data *drv = bss->drv;
  3818. struct nl_msg *msg, *flags = NULL;
  3819. struct nl80211_sta_flag_update upd;
  3820. msg = nlmsg_alloc();
  3821. if (!msg)
  3822. return -ENOMEM;
  3823. flags = nlmsg_alloc();
  3824. if (!flags) {
  3825. nlmsg_free(msg);
  3826. return -ENOMEM;
  3827. }
  3828. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  3829. 0, NL80211_CMD_SET_STATION, 0);
  3830. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  3831. if_nametoindex(bss->ifname));
  3832. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  3833. /*
  3834. * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
  3835. * can be removed eventually.
  3836. */
  3837. if (total_flags & WPA_STA_AUTHORIZED)
  3838. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
  3839. if (total_flags & WPA_STA_WMM)
  3840. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
  3841. if (total_flags & WPA_STA_SHORT_PREAMBLE)
  3842. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
  3843. if (total_flags & WPA_STA_MFP)
  3844. NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
  3845. if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
  3846. goto nla_put_failure;
  3847. os_memset(&upd, 0, sizeof(upd));
  3848. upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
  3849. upd.set = sta_flags_nl80211(flags_or);
  3850. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  3851. nlmsg_free(flags);
  3852. return send_and_recv_msgs(drv, msg, NULL, NULL);
  3853. nla_put_failure:
  3854. nlmsg_free(flags);
  3855. return -ENOBUFS;
  3856. }
  3857. static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
  3858. struct wpa_driver_associate_params *params)
  3859. {
  3860. if (params->p2p)
  3861. wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
  3862. "group (GO)");
  3863. if (wpa_driver_nl80211_set_mode(&drv->first_bss, params->mode) ||
  3864. wpa_driver_nl80211_set_freq(drv, params->freq, 0, 0)) {
  3865. nl80211_remove_monitor_interface(drv);
  3866. return -1;
  3867. }
  3868. /* TODO: setup monitor interface (and add code somewhere to remove this
  3869. * when AP mode is stopped; associate with mode != 2 or drv_deinit) */
  3870. return 0;
  3871. }
  3872. static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
  3873. {
  3874. struct nl_msg *msg;
  3875. int ret = -1;
  3876. msg = nlmsg_alloc();
  3877. if (!msg)
  3878. return -1;
  3879. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3880. NL80211_CMD_LEAVE_IBSS, 0);
  3881. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3882. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3883. msg = NULL;
  3884. if (ret) {
  3885. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
  3886. "(%s)", ret, strerror(-ret));
  3887. goto nla_put_failure;
  3888. }
  3889. ret = 0;
  3890. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
  3891. nla_put_failure:
  3892. nlmsg_free(msg);
  3893. return ret;
  3894. }
  3895. static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
  3896. struct wpa_driver_associate_params *params)
  3897. {
  3898. struct nl_msg *msg;
  3899. int ret = -1;
  3900. int count = 0;
  3901. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
  3902. if (wpa_driver_nl80211_set_mode(&drv->first_bss, params->mode)) {
  3903. wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
  3904. "IBSS mode");
  3905. return -1;
  3906. }
  3907. retry:
  3908. msg = nlmsg_alloc();
  3909. if (!msg)
  3910. return -1;
  3911. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3912. NL80211_CMD_JOIN_IBSS, 0);
  3913. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3914. if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
  3915. goto nla_put_failure;
  3916. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  3917. params->ssid, params->ssid_len);
  3918. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  3919. params->ssid);
  3920. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  3921. drv->ssid_len = params->ssid_len;
  3922. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  3923. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  3924. ret = nl80211_set_conn_keys(params, msg);
  3925. if (ret)
  3926. goto nla_put_failure;
  3927. if (params->wpa_ie) {
  3928. wpa_hexdump(MSG_DEBUG,
  3929. " * Extra IEs for Beacon/Probe Response frames",
  3930. params->wpa_ie, params->wpa_ie_len);
  3931. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  3932. params->wpa_ie);
  3933. }
  3934. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3935. msg = NULL;
  3936. if (ret) {
  3937. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
  3938. ret, strerror(-ret));
  3939. count++;
  3940. if (ret == -EALREADY && count == 1) {
  3941. wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
  3942. "forced leave");
  3943. nl80211_leave_ibss(drv);
  3944. nlmsg_free(msg);
  3945. goto retry;
  3946. }
  3947. goto nla_put_failure;
  3948. }
  3949. ret = 0;
  3950. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
  3951. nla_put_failure:
  3952. nlmsg_free(msg);
  3953. return ret;
  3954. }
  3955. static int wpa_driver_nl80211_connect(
  3956. struct wpa_driver_nl80211_data *drv,
  3957. struct wpa_driver_associate_params *params)
  3958. {
  3959. struct nl_msg *msg;
  3960. enum nl80211_auth_type type;
  3961. int ret = 0;
  3962. int algs;
  3963. msg = nlmsg_alloc();
  3964. if (!msg)
  3965. return -1;
  3966. wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
  3967. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  3968. NL80211_CMD_CONNECT, 0);
  3969. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  3970. if (params->bssid) {
  3971. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  3972. MAC2STR(params->bssid));
  3973. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  3974. }
  3975. if (params->freq) {
  3976. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  3977. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  3978. }
  3979. if (params->ssid) {
  3980. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  3981. params->ssid, params->ssid_len);
  3982. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  3983. params->ssid);
  3984. if (params->ssid_len > sizeof(drv->ssid))
  3985. goto nla_put_failure;
  3986. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  3987. drv->ssid_len = params->ssid_len;
  3988. }
  3989. wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
  3990. if (params->wpa_ie)
  3991. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  3992. params->wpa_ie);
  3993. algs = 0;
  3994. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  3995. algs++;
  3996. if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  3997. algs++;
  3998. if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  3999. algs++;
  4000. if (algs > 1) {
  4001. wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
  4002. "selection");
  4003. goto skip_auth_type;
  4004. }
  4005. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  4006. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  4007. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  4008. type = NL80211_AUTHTYPE_SHARED_KEY;
  4009. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  4010. type = NL80211_AUTHTYPE_NETWORK_EAP;
  4011. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  4012. type = NL80211_AUTHTYPE_FT;
  4013. else
  4014. goto nla_put_failure;
  4015. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  4016. NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
  4017. skip_auth_type:
  4018. if (params->wpa_ie && params->wpa_ie_len) {
  4019. enum nl80211_wpa_versions ver;
  4020. if (params->wpa_ie[0] == WLAN_EID_RSN)
  4021. ver = NL80211_WPA_VERSION_2;
  4022. else
  4023. ver = NL80211_WPA_VERSION_1;
  4024. wpa_printf(MSG_DEBUG, " * WPA Version %d", ver);
  4025. NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
  4026. }
  4027. if (params->pairwise_suite != CIPHER_NONE) {
  4028. int cipher;
  4029. switch (params->pairwise_suite) {
  4030. case CIPHER_WEP40:
  4031. cipher = WLAN_CIPHER_SUITE_WEP40;
  4032. break;
  4033. case CIPHER_WEP104:
  4034. cipher = WLAN_CIPHER_SUITE_WEP104;
  4035. break;
  4036. case CIPHER_CCMP:
  4037. cipher = WLAN_CIPHER_SUITE_CCMP;
  4038. break;
  4039. case CIPHER_TKIP:
  4040. default:
  4041. cipher = WLAN_CIPHER_SUITE_TKIP;
  4042. break;
  4043. }
  4044. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
  4045. }
  4046. if (params->group_suite != CIPHER_NONE) {
  4047. int cipher;
  4048. switch (params->group_suite) {
  4049. case CIPHER_WEP40:
  4050. cipher = WLAN_CIPHER_SUITE_WEP40;
  4051. break;
  4052. case CIPHER_WEP104:
  4053. cipher = WLAN_CIPHER_SUITE_WEP104;
  4054. break;
  4055. case CIPHER_CCMP:
  4056. cipher = WLAN_CIPHER_SUITE_CCMP;
  4057. break;
  4058. case CIPHER_TKIP:
  4059. default:
  4060. cipher = WLAN_CIPHER_SUITE_TKIP;
  4061. break;
  4062. }
  4063. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
  4064. }
  4065. if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
  4066. params->key_mgmt_suite == KEY_MGMT_PSK) {
  4067. int mgmt = WLAN_AKM_SUITE_PSK;
  4068. switch (params->key_mgmt_suite) {
  4069. case KEY_MGMT_802_1X:
  4070. mgmt = WLAN_AKM_SUITE_8021X;
  4071. break;
  4072. case KEY_MGMT_PSK:
  4073. default:
  4074. mgmt = WLAN_AKM_SUITE_PSK;
  4075. break;
  4076. }
  4077. NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
  4078. }
  4079. ret = nl80211_set_conn_keys(params, msg);
  4080. if (ret)
  4081. goto nla_put_failure;
  4082. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4083. msg = NULL;
  4084. if (ret) {
  4085. wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
  4086. "(%s)", ret, strerror(-ret));
  4087. goto nla_put_failure;
  4088. }
  4089. ret = 0;
  4090. wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
  4091. nla_put_failure:
  4092. nlmsg_free(msg);
  4093. return ret;
  4094. }
  4095. static int wpa_driver_nl80211_associate(
  4096. void *priv, struct wpa_driver_associate_params *params)
  4097. {
  4098. struct i802_bss *bss = priv;
  4099. struct wpa_driver_nl80211_data *drv = bss->drv;
  4100. int ret = -1;
  4101. struct nl_msg *msg;
  4102. if (params->mode == IEEE80211_MODE_AP)
  4103. return wpa_driver_nl80211_ap(drv, params);
  4104. if (params->mode == IEEE80211_MODE_IBSS)
  4105. return wpa_driver_nl80211_ibss(drv, params);
  4106. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
  4107. if (wpa_driver_nl80211_set_mode(priv, params->mode) < 0)
  4108. return -1;
  4109. return wpa_driver_nl80211_connect(drv, params);
  4110. }
  4111. drv->associated = 0;
  4112. msg = nlmsg_alloc();
  4113. if (!msg)
  4114. return -1;
  4115. wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
  4116. drv->ifindex);
  4117. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  4118. NL80211_CMD_ASSOCIATE, 0);
  4119. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4120. if (params->bssid) {
  4121. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  4122. MAC2STR(params->bssid));
  4123. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
  4124. }
  4125. if (params->freq) {
  4126. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  4127. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
  4128. drv->assoc_freq = params->freq;
  4129. } else
  4130. drv->assoc_freq = 0;
  4131. if (params->ssid) {
  4132. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  4133. params->ssid, params->ssid_len);
  4134. NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
  4135. params->ssid);
  4136. if (params->ssid_len > sizeof(drv->ssid))
  4137. goto nla_put_failure;
  4138. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  4139. drv->ssid_len = params->ssid_len;
  4140. }
  4141. wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
  4142. if (params->wpa_ie)
  4143. NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  4144. params->wpa_ie);
  4145. if (params->pairwise_suite != CIPHER_NONE) {
  4146. int cipher;
  4147. switch (params->pairwise_suite) {
  4148. case CIPHER_WEP40:
  4149. cipher = WLAN_CIPHER_SUITE_WEP40;
  4150. break;
  4151. case CIPHER_WEP104:
  4152. cipher = WLAN_CIPHER_SUITE_WEP104;
  4153. break;
  4154. case CIPHER_CCMP:
  4155. cipher = WLAN_CIPHER_SUITE_CCMP;
  4156. break;
  4157. case CIPHER_TKIP:
  4158. default:
  4159. cipher = WLAN_CIPHER_SUITE_TKIP;
  4160. break;
  4161. }
  4162. wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
  4163. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
  4164. }
  4165. if (params->group_suite != CIPHER_NONE) {
  4166. int cipher;
  4167. switch (params->group_suite) {
  4168. case CIPHER_WEP40:
  4169. cipher = WLAN_CIPHER_SUITE_WEP40;
  4170. break;
  4171. case CIPHER_WEP104:
  4172. cipher = WLAN_CIPHER_SUITE_WEP104;
  4173. break;
  4174. case CIPHER_CCMP:
  4175. cipher = WLAN_CIPHER_SUITE_CCMP;
  4176. break;
  4177. case CIPHER_TKIP:
  4178. default:
  4179. cipher = WLAN_CIPHER_SUITE_TKIP;
  4180. break;
  4181. }
  4182. wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
  4183. NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
  4184. }
  4185. #ifdef CONFIG_IEEE80211W
  4186. if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
  4187. NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
  4188. #endif /* CONFIG_IEEE80211W */
  4189. NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
  4190. if (params->prev_bssid) {
  4191. wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
  4192. MAC2STR(params->prev_bssid));
  4193. NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
  4194. params->prev_bssid);
  4195. }
  4196. if (params->p2p)
  4197. wpa_printf(MSG_DEBUG, " * P2P group");
  4198. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4199. msg = NULL;
  4200. if (ret) {
  4201. wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
  4202. "(%s)", ret, strerror(-ret));
  4203. nl80211_dump_scan(drv);
  4204. goto nla_put_failure;
  4205. }
  4206. ret = 0;
  4207. wpa_printf(MSG_DEBUG, "nl80211: Association request send "
  4208. "successfully");
  4209. nla_put_failure:
  4210. nlmsg_free(msg);
  4211. return ret;
  4212. }
  4213. static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
  4214. int ifindex, enum nl80211_iftype mode)
  4215. {
  4216. struct nl_msg *msg;
  4217. int ret = -ENOBUFS;
  4218. wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
  4219. ifindex, mode, nl80211_iftype_str(mode));
  4220. msg = nlmsg_alloc();
  4221. if (!msg)
  4222. return -ENOMEM;
  4223. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4224. 0, NL80211_CMD_SET_INTERFACE, 0);
  4225. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  4226. NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
  4227. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4228. if (!ret)
  4229. return 0;
  4230. nla_put_failure:
  4231. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
  4232. " %d (%s)", ifindex, mode, ret, strerror(-ret));
  4233. return ret;
  4234. }
  4235. static int wpa_driver_nl80211_set_mode(void *priv, int mode)
  4236. {
  4237. struct i802_bss *bss = priv;
  4238. struct wpa_driver_nl80211_data *drv = bss->drv;
  4239. int ret = -1;
  4240. int nlmode;
  4241. int i;
  4242. switch (mode) {
  4243. case 0:
  4244. nlmode = NL80211_IFTYPE_STATION;
  4245. break;
  4246. case 1:
  4247. nlmode = NL80211_IFTYPE_ADHOC;
  4248. break;
  4249. case 2:
  4250. nlmode = NL80211_IFTYPE_AP;
  4251. break;
  4252. default:
  4253. return -1;
  4254. }
  4255. if (nl80211_set_mode(drv, drv->ifindex, nlmode) == 0) {
  4256. drv->nlmode = nlmode;
  4257. ret = 0;
  4258. goto done;
  4259. }
  4260. if (nlmode == drv->nlmode) {
  4261. wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
  4262. "requested mode - ignore error");
  4263. ret = 0;
  4264. goto done; /* Already in the requested mode */
  4265. }
  4266. /* mac80211 doesn't allow mode changes while the device is up, so
  4267. * take the device down, try to set the mode again, and bring the
  4268. * device back up.
  4269. */
  4270. wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
  4271. "interface down");
  4272. for (i = 0; i < 10; i++) {
  4273. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 0) ==
  4274. 0) {
  4275. /* Try to set the mode again while the interface is
  4276. * down */
  4277. ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
  4278. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname,
  4279. 1))
  4280. ret = -1;
  4281. if (!ret)
  4282. break;
  4283. } else
  4284. wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
  4285. "interface down");
  4286. os_sleep(0, 100000);
  4287. }
  4288. if (!ret) {
  4289. wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
  4290. "interface is down");
  4291. drv->nlmode = nlmode;
  4292. drv->ignore_if_down_event = 1;
  4293. }
  4294. done:
  4295. if (!ret && nlmode == NL80211_IFTYPE_AP) {
  4296. /* Setup additional AP mode functionality if needed */
  4297. if (!drv->no_monitor_iface_capab && drv->monitor_ifidx < 0 &&
  4298. nl80211_create_monitor_interface(drv) &&
  4299. !drv->no_monitor_iface_capab)
  4300. return -1;
  4301. } else if (!ret && nlmode != NL80211_IFTYPE_AP) {
  4302. /* Remove additional AP mode functionality */
  4303. nl80211_remove_monitor_interface(drv);
  4304. bss->beacon_set = 0;
  4305. }
  4306. if (ret)
  4307. wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
  4308. "from %d failed", nlmode, drv->nlmode);
  4309. return ret;
  4310. }
  4311. static int wpa_driver_nl80211_get_capa(void *priv,
  4312. struct wpa_driver_capa *capa)
  4313. {
  4314. struct i802_bss *bss = priv;
  4315. struct wpa_driver_nl80211_data *drv = bss->drv;
  4316. if (!drv->has_capability)
  4317. return -1;
  4318. os_memcpy(capa, &drv->capa, sizeof(*capa));
  4319. return 0;
  4320. }
  4321. static int wpa_driver_nl80211_set_operstate(void *priv, int state)
  4322. {
  4323. struct i802_bss *bss = priv;
  4324. struct wpa_driver_nl80211_data *drv = bss->drv;
  4325. wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
  4326. __func__, drv->operstate, state, state ? "UP" : "DORMANT");
  4327. drv->operstate = state;
  4328. return netlink_send_oper_ifla(drv->netlink, drv->ifindex, -1,
  4329. state ? IF_OPER_UP : IF_OPER_DORMANT);
  4330. }
  4331. static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
  4332. {
  4333. struct i802_bss *bss = priv;
  4334. struct wpa_driver_nl80211_data *drv = bss->drv;
  4335. struct nl_msg *msg;
  4336. struct nl80211_sta_flag_update upd;
  4337. msg = nlmsg_alloc();
  4338. if (!msg)
  4339. return -ENOMEM;
  4340. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4341. 0, NL80211_CMD_SET_STATION, 0);
  4342. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  4343. if_nametoindex(bss->ifname));
  4344. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
  4345. os_memset(&upd, 0, sizeof(upd));
  4346. upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
  4347. if (authorized)
  4348. upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
  4349. NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
  4350. return send_and_recv_msgs(drv, msg, NULL, NULL);
  4351. nla_put_failure:
  4352. return -ENOBUFS;
  4353. }
  4354. /* Set kernel driver on given frequency (MHz) */
  4355. static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
  4356. {
  4357. struct i802_bss *bss = priv;
  4358. struct wpa_driver_nl80211_data *drv = bss->drv;
  4359. return wpa_driver_nl80211_set_freq(drv, freq->freq, freq->ht_enabled,
  4360. freq->sec_channel_offset);
  4361. }
  4362. #if defined(HOSTAPD) || defined(CONFIG_AP)
  4363. static inline int min_int(int a, int b)
  4364. {
  4365. if (a < b)
  4366. return a;
  4367. return b;
  4368. }
  4369. static int get_key_handler(struct nl_msg *msg, void *arg)
  4370. {
  4371. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  4372. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  4373. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  4374. genlmsg_attrlen(gnlh, 0), NULL);
  4375. /*
  4376. * TODO: validate the key index and mac address!
  4377. * Otherwise, there's a race condition as soon as
  4378. * the kernel starts sending key notifications.
  4379. */
  4380. if (tb[NL80211_ATTR_KEY_SEQ])
  4381. memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  4382. min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
  4383. return NL_SKIP;
  4384. }
  4385. static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
  4386. int idx, u8 *seq)
  4387. {
  4388. struct i802_bss *bss = priv;
  4389. struct wpa_driver_nl80211_data *drv = bss->drv;
  4390. struct nl_msg *msg;
  4391. msg = nlmsg_alloc();
  4392. if (!msg)
  4393. return -ENOMEM;
  4394. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4395. 0, NL80211_CMD_GET_KEY, 0);
  4396. if (addr)
  4397. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  4398. NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
  4399. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
  4400. memset(seq, 0, 6);
  4401. return send_and_recv_msgs(drv, msg, get_key_handler, seq);
  4402. nla_put_failure:
  4403. return -ENOBUFS;
  4404. }
  4405. static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
  4406. int mode)
  4407. {
  4408. struct i802_bss *bss = priv;
  4409. struct wpa_driver_nl80211_data *drv = bss->drv;
  4410. struct nl_msg *msg;
  4411. u8 rates[NL80211_MAX_SUPP_RATES];
  4412. u8 rates_len = 0;
  4413. int i;
  4414. msg = nlmsg_alloc();
  4415. if (!msg)
  4416. return -ENOMEM;
  4417. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  4418. NL80211_CMD_SET_BSS, 0);
  4419. for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
  4420. rates[rates_len++] = basic_rates[i] / 5;
  4421. NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
  4422. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  4423. return send_and_recv_msgs(drv, msg, NULL, NULL);
  4424. nla_put_failure:
  4425. return -ENOBUFS;
  4426. }
  4427. static int i802_set_rts(void *priv, int rts)
  4428. {
  4429. struct i802_bss *bss = priv;
  4430. struct wpa_driver_nl80211_data *drv = bss->drv;
  4431. struct nl_msg *msg;
  4432. int ret = -ENOBUFS;
  4433. u32 val;
  4434. msg = nlmsg_alloc();
  4435. if (!msg)
  4436. return -ENOMEM;
  4437. if (rts >= 2347)
  4438. val = (u32) -1;
  4439. else
  4440. val = rts;
  4441. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4442. 0, NL80211_CMD_SET_WIPHY, 0);
  4443. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4444. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
  4445. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4446. if (!ret)
  4447. return 0;
  4448. nla_put_failure:
  4449. wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
  4450. "%d (%s)", rts, ret, strerror(-ret));
  4451. return ret;
  4452. }
  4453. static int i802_set_frag(void *priv, int frag)
  4454. {
  4455. struct i802_bss *bss = priv;
  4456. struct wpa_driver_nl80211_data *drv = bss->drv;
  4457. struct nl_msg *msg;
  4458. int ret = -ENOBUFS;
  4459. u32 val;
  4460. msg = nlmsg_alloc();
  4461. if (!msg)
  4462. return -ENOMEM;
  4463. if (frag >= 2346)
  4464. val = (u32) -1;
  4465. else
  4466. val = frag;
  4467. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4468. 0, NL80211_CMD_SET_WIPHY, 0);
  4469. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  4470. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
  4471. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4472. if (!ret)
  4473. return 0;
  4474. nla_put_failure:
  4475. wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
  4476. "%d: %d (%s)", frag, ret, strerror(-ret));
  4477. return ret;
  4478. }
  4479. static int i802_flush(void *priv)
  4480. {
  4481. struct i802_bss *bss = priv;
  4482. struct wpa_driver_nl80211_data *drv = bss->drv;
  4483. struct nl_msg *msg;
  4484. msg = nlmsg_alloc();
  4485. if (!msg)
  4486. return -1;
  4487. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4488. 0, NL80211_CMD_DEL_STATION, 0);
  4489. /*
  4490. * XXX: FIX! this needs to flush all VLANs too
  4491. */
  4492. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  4493. if_nametoindex(bss->ifname));
  4494. return send_and_recv_msgs(drv, msg, NULL, NULL);
  4495. nla_put_failure:
  4496. return -ENOBUFS;
  4497. }
  4498. static int get_sta_handler(struct nl_msg *msg, void *arg)
  4499. {
  4500. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  4501. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  4502. struct hostap_sta_driver_data *data = arg;
  4503. struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
  4504. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  4505. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  4506. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  4507. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  4508. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  4509. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  4510. };
  4511. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  4512. genlmsg_attrlen(gnlh, 0), NULL);
  4513. /*
  4514. * TODO: validate the interface and mac address!
  4515. * Otherwise, there's a race condition as soon as
  4516. * the kernel starts sending station notifications.
  4517. */
  4518. if (!tb[NL80211_ATTR_STA_INFO]) {
  4519. wpa_printf(MSG_DEBUG, "sta stats missing!");
  4520. return NL_SKIP;
  4521. }
  4522. if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
  4523. tb[NL80211_ATTR_STA_INFO],
  4524. stats_policy)) {
  4525. wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
  4526. return NL_SKIP;
  4527. }
  4528. if (stats[NL80211_STA_INFO_INACTIVE_TIME])
  4529. data->inactive_msec =
  4530. nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
  4531. if (stats[NL80211_STA_INFO_RX_BYTES])
  4532. data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
  4533. if (stats[NL80211_STA_INFO_TX_BYTES])
  4534. data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
  4535. if (stats[NL80211_STA_INFO_RX_PACKETS])
  4536. data->rx_packets =
  4537. nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
  4538. if (stats[NL80211_STA_INFO_TX_PACKETS])
  4539. data->tx_packets =
  4540. nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
  4541. return NL_SKIP;
  4542. }
  4543. static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
  4544. const u8 *addr)
  4545. {
  4546. struct i802_bss *bss = priv;
  4547. struct wpa_driver_nl80211_data *drv = bss->drv;
  4548. struct nl_msg *msg;
  4549. os_memset(data, 0, sizeof(*data));
  4550. msg = nlmsg_alloc();
  4551. if (!msg)
  4552. return -ENOMEM;
  4553. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4554. 0, NL80211_CMD_GET_STATION, 0);
  4555. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  4556. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  4557. return send_and_recv_msgs(drv, msg, get_sta_handler, data);
  4558. nla_put_failure:
  4559. return -ENOBUFS;
  4560. }
  4561. static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
  4562. int cw_min, int cw_max, int burst_time)
  4563. {
  4564. struct i802_bss *bss = priv;
  4565. struct wpa_driver_nl80211_data *drv = bss->drv;
  4566. struct nl_msg *msg;
  4567. struct nlattr *txq, *params;
  4568. msg = nlmsg_alloc();
  4569. if (!msg)
  4570. return -1;
  4571. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4572. 0, NL80211_CMD_SET_WIPHY, 0);
  4573. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  4574. txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
  4575. if (!txq)
  4576. goto nla_put_failure;
  4577. /* We are only sending parameters for a single TXQ at a time */
  4578. params = nla_nest_start(msg, 1);
  4579. if (!params)
  4580. goto nla_put_failure;
  4581. switch (queue) {
  4582. case 0:
  4583. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
  4584. break;
  4585. case 1:
  4586. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
  4587. break;
  4588. case 2:
  4589. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
  4590. break;
  4591. case 3:
  4592. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
  4593. break;
  4594. }
  4595. /* Burst time is configured in units of 0.1 msec and TXOP parameter in
  4596. * 32 usec, so need to convert the value here. */
  4597. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
  4598. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
  4599. NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
  4600. NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
  4601. nla_nest_end(msg, params);
  4602. nla_nest_end(msg, txq);
  4603. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  4604. return 0;
  4605. nla_put_failure:
  4606. return -1;
  4607. }
  4608. static int i802_set_bss(void *priv, int cts, int preamble, int slot,
  4609. int ht_opmode)
  4610. {
  4611. struct i802_bss *bss = priv;
  4612. struct wpa_driver_nl80211_data *drv = bss->drv;
  4613. struct nl_msg *msg;
  4614. msg = nlmsg_alloc();
  4615. if (!msg)
  4616. return -ENOMEM;
  4617. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  4618. NL80211_CMD_SET_BSS, 0);
  4619. if (cts >= 0)
  4620. NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
  4621. if (preamble >= 0)
  4622. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
  4623. if (slot >= 0)
  4624. NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
  4625. if (ht_opmode >= 0)
  4626. NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
  4627. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  4628. return send_and_recv_msgs(drv, msg, NULL, NULL);
  4629. nla_put_failure:
  4630. return -ENOBUFS;
  4631. }
  4632. static int i802_set_cts_protect(void *priv, int value)
  4633. {
  4634. return i802_set_bss(priv, value, -1, -1, -1);
  4635. }
  4636. static int i802_set_preamble(void *priv, int value)
  4637. {
  4638. return i802_set_bss(priv, -1, value, -1, -1);
  4639. }
  4640. static int i802_set_short_slot_time(void *priv, int value)
  4641. {
  4642. return i802_set_bss(priv, -1, -1, value, -1);
  4643. }
  4644. static int i802_set_sta_vlan(void *priv, const u8 *addr,
  4645. const char *ifname, int vlan_id)
  4646. {
  4647. struct i802_bss *bss = priv;
  4648. struct wpa_driver_nl80211_data *drv = bss->drv;
  4649. struct nl_msg *msg;
  4650. int ret = -ENOBUFS;
  4651. msg = nlmsg_alloc();
  4652. if (!msg)
  4653. return -ENOMEM;
  4654. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  4655. 0, NL80211_CMD_SET_STATION, 0);
  4656. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
  4657. if_nametoindex(bss->ifname));
  4658. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
  4659. NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
  4660. if_nametoindex(ifname));
  4661. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4662. if (ret < 0) {
  4663. wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
  4664. MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
  4665. MAC2STR(addr), ifname, vlan_id, ret,
  4666. strerror(-ret));
  4667. }
  4668. nla_put_failure:
  4669. return ret;
  4670. }
  4671. static int i802_set_ht_params(void *priv, const u8 *ht_capab,
  4672. size_t ht_capab_len, const u8 *ht_oper,
  4673. size_t ht_oper_len)
  4674. {
  4675. if (ht_oper_len >= 6) {
  4676. /* ht opmode uses 16bit in octet 5 & 6 */
  4677. u16 ht_opmode = le_to_host16(((u16 *) ht_oper)[2]);
  4678. return i802_set_bss(priv, -1, -1, -1, ht_opmode);
  4679. } else
  4680. return -1;
  4681. }
  4682. static int i802_get_inact_sec(void *priv, const u8 *addr)
  4683. {
  4684. struct hostap_sta_driver_data data;
  4685. int ret;
  4686. data.inactive_msec = (unsigned long) -1;
  4687. ret = i802_read_sta_data(priv, &data, addr);
  4688. if (ret || data.inactive_msec == (unsigned long) -1)
  4689. return -1;
  4690. return data.inactive_msec / 1000;
  4691. }
  4692. static int i802_sta_clear_stats(void *priv, const u8 *addr)
  4693. {
  4694. #if 0
  4695. /* TODO */
  4696. #endif
  4697. return 0;
  4698. }
  4699. static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
  4700. int reason)
  4701. {
  4702. struct i802_bss *bss = priv;
  4703. struct ieee80211_mgmt mgmt;
  4704. memset(&mgmt, 0, sizeof(mgmt));
  4705. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  4706. WLAN_FC_STYPE_DEAUTH);
  4707. memcpy(mgmt.da, addr, ETH_ALEN);
  4708. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  4709. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  4710. mgmt.u.deauth.reason_code = host_to_le16(reason);
  4711. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  4712. IEEE80211_HDRLEN +
  4713. sizeof(mgmt.u.deauth));
  4714. }
  4715. static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
  4716. int reason)
  4717. {
  4718. struct i802_bss *bss = priv;
  4719. struct ieee80211_mgmt mgmt;
  4720. memset(&mgmt, 0, sizeof(mgmt));
  4721. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  4722. WLAN_FC_STYPE_DISASSOC);
  4723. memcpy(mgmt.da, addr, ETH_ALEN);
  4724. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  4725. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  4726. mgmt.u.disassoc.reason_code = host_to_le16(reason);
  4727. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  4728. IEEE80211_HDRLEN +
  4729. sizeof(mgmt.u.disassoc));
  4730. }
  4731. #endif /* HOSTAPD || CONFIG_AP */
  4732. #ifdef HOSTAPD
  4733. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  4734. {
  4735. int i;
  4736. int *old;
  4737. wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
  4738. ifidx);
  4739. for (i = 0; i < drv->num_if_indices; i++) {
  4740. if (drv->if_indices[i] == 0) {
  4741. drv->if_indices[i] = ifidx;
  4742. return;
  4743. }
  4744. }
  4745. if (drv->if_indices != drv->default_if_indices)
  4746. old = drv->if_indices;
  4747. else
  4748. old = NULL;
  4749. drv->if_indices = os_realloc(old,
  4750. sizeof(int) * (drv->num_if_indices + 1));
  4751. if (!drv->if_indices) {
  4752. if (!old)
  4753. drv->if_indices = drv->default_if_indices;
  4754. else
  4755. drv->if_indices = old;
  4756. wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
  4757. "interfaces");
  4758. wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
  4759. return;
  4760. } else if (!old)
  4761. os_memcpy(drv->if_indices, drv->default_if_indices,
  4762. sizeof(drv->default_if_indices));
  4763. drv->if_indices[drv->num_if_indices] = ifidx;
  4764. drv->num_if_indices++;
  4765. }
  4766. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  4767. {
  4768. int i;
  4769. for (i = 0; i < drv->num_if_indices; i++) {
  4770. if (drv->if_indices[i] == ifidx) {
  4771. drv->if_indices[i] = 0;
  4772. break;
  4773. }
  4774. }
  4775. }
  4776. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  4777. {
  4778. int i;
  4779. for (i = 0; i < drv->num_if_indices; i++)
  4780. if (drv->if_indices[i] == ifidx)
  4781. return 1;
  4782. return 0;
  4783. }
  4784. static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
  4785. const char *bridge_ifname)
  4786. {
  4787. struct i802_bss *bss = priv;
  4788. struct wpa_driver_nl80211_data *drv = bss->drv;
  4789. char name[IFNAMSIZ + 1];
  4790. os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
  4791. wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
  4792. " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
  4793. if (val) {
  4794. if (!if_nametoindex(name)) {
  4795. if (nl80211_create_iface(drv, name,
  4796. NL80211_IFTYPE_AP_VLAN,
  4797. NULL, 1) < 0)
  4798. return -1;
  4799. if (bridge_ifname &&
  4800. linux_br_add_if(drv->ioctl_sock, bridge_ifname,
  4801. name) < 0)
  4802. return -1;
  4803. }
  4804. linux_set_iface_flags(drv->ioctl_sock, name, 1);
  4805. return i802_set_sta_vlan(priv, addr, name, 0);
  4806. } else {
  4807. i802_set_sta_vlan(priv, addr, bss->ifname, 0);
  4808. return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
  4809. name);
  4810. }
  4811. }
  4812. static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
  4813. {
  4814. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  4815. struct sockaddr_ll lladdr;
  4816. unsigned char buf[3000];
  4817. int len;
  4818. socklen_t fromlen = sizeof(lladdr);
  4819. len = recvfrom(sock, buf, sizeof(buf), 0,
  4820. (struct sockaddr *)&lladdr, &fromlen);
  4821. if (len < 0) {
  4822. perror("recv");
  4823. return;
  4824. }
  4825. if (have_ifidx(drv, lladdr.sll_ifindex))
  4826. drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
  4827. }
  4828. static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
  4829. struct i802_bss *bss,
  4830. const char *brname, const char *ifname)
  4831. {
  4832. int ifindex;
  4833. char in_br[IFNAMSIZ];
  4834. os_strlcpy(bss->brname, brname, IFNAMSIZ);
  4835. ifindex = if_nametoindex(brname);
  4836. if (ifindex == 0) {
  4837. /*
  4838. * Bridge was configured, but the bridge device does
  4839. * not exist. Try to add it now.
  4840. */
  4841. if (linux_br_add(drv->ioctl_sock, brname) < 0) {
  4842. wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
  4843. "bridge interface %s: %s",
  4844. brname, strerror(errno));
  4845. return -1;
  4846. }
  4847. bss->added_bridge = 1;
  4848. add_ifidx(drv, if_nametoindex(brname));
  4849. }
  4850. if (linux_br_get(in_br, ifname) == 0) {
  4851. if (os_strcmp(in_br, brname) == 0)
  4852. return 0; /* already in the bridge */
  4853. wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
  4854. "bridge %s", ifname, in_br);
  4855. if (linux_br_del_if(drv->ioctl_sock, in_br, ifname) < 0) {
  4856. wpa_printf(MSG_ERROR, "nl80211: Failed to "
  4857. "remove interface %s from bridge "
  4858. "%s: %s",
  4859. ifname, brname, strerror(errno));
  4860. return -1;
  4861. }
  4862. }
  4863. wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
  4864. ifname, brname);
  4865. if (linux_br_add_if(drv->ioctl_sock, brname, ifname) < 0) {
  4866. wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
  4867. "into bridge %s: %s",
  4868. ifname, brname, strerror(errno));
  4869. return -1;
  4870. }
  4871. bss->added_if_into_bridge = 1;
  4872. return 0;
  4873. }
  4874. static void *i802_init(struct hostapd_data *hapd,
  4875. struct wpa_init_params *params)
  4876. {
  4877. struct wpa_driver_nl80211_data *drv;
  4878. struct i802_bss *bss;
  4879. size_t i;
  4880. char brname[IFNAMSIZ];
  4881. int ifindex, br_ifindex;
  4882. int br_added = 0;
  4883. bss = wpa_driver_nl80211_init(hapd, params->ifname, NULL);
  4884. if (bss == NULL)
  4885. return NULL;
  4886. drv = bss->drv;
  4887. drv->nlmode = NL80211_IFTYPE_AP;
  4888. if (linux_br_get(brname, params->ifname) == 0) {
  4889. wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
  4890. params->ifname, brname);
  4891. br_ifindex = if_nametoindex(brname);
  4892. } else {
  4893. brname[0] = '\0';
  4894. br_ifindex = 0;
  4895. }
  4896. drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
  4897. drv->if_indices = drv->default_if_indices;
  4898. for (i = 0; i < params->num_bridge; i++) {
  4899. if (params->bridge[i]) {
  4900. ifindex = if_nametoindex(params->bridge[i]);
  4901. if (ifindex)
  4902. add_ifidx(drv, ifindex);
  4903. if (ifindex == br_ifindex)
  4904. br_added = 1;
  4905. }
  4906. }
  4907. if (!br_added && br_ifindex &&
  4908. (params->num_bridge == 0 || !params->bridge[0]))
  4909. add_ifidx(drv, br_ifindex);
  4910. /* start listening for EAPOL on the default AP interface */
  4911. add_ifidx(drv, drv->ifindex);
  4912. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 0))
  4913. goto failed;
  4914. if (params->bssid) {
  4915. if (linux_set_ifhwaddr(drv->ioctl_sock, bss->ifname,
  4916. params->bssid))
  4917. goto failed;
  4918. }
  4919. if (wpa_driver_nl80211_set_mode(bss, IEEE80211_MODE_AP)) {
  4920. wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
  4921. "into AP mode", bss->ifname);
  4922. goto failed;
  4923. }
  4924. if (params->num_bridge && params->bridge[0] &&
  4925. i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
  4926. goto failed;
  4927. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1))
  4928. goto failed;
  4929. drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
  4930. if (drv->eapol_sock < 0) {
  4931. perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
  4932. goto failed;
  4933. }
  4934. if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
  4935. {
  4936. printf("Could not register read socket for eapol\n");
  4937. goto failed;
  4938. }
  4939. if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, params->own_addr))
  4940. goto failed;
  4941. return bss;
  4942. failed:
  4943. nl80211_remove_monitor_interface(drv);
  4944. rfkill_deinit(drv->rfkill);
  4945. netlink_deinit(drv->netlink);
  4946. if (drv->ioctl_sock >= 0)
  4947. close(drv->ioctl_sock);
  4948. genl_family_put(drv->nl80211);
  4949. nl_cache_free(drv->nl_cache);
  4950. nl80211_handle_destroy(drv->nl_handle);
  4951. nl_cb_put(drv->nl_cb);
  4952. eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
  4953. os_free(drv);
  4954. return NULL;
  4955. }
  4956. static void i802_deinit(void *priv)
  4957. {
  4958. wpa_driver_nl80211_deinit(priv);
  4959. }
  4960. #endif /* HOSTAPD */
  4961. static enum nl80211_iftype wpa_driver_nl80211_if_type(
  4962. enum wpa_driver_if_type type)
  4963. {
  4964. switch (type) {
  4965. case WPA_IF_STATION:
  4966. return NL80211_IFTYPE_STATION;
  4967. case WPA_IF_P2P_CLIENT:
  4968. case WPA_IF_P2P_GROUP:
  4969. return NL80211_IFTYPE_P2P_CLIENT;
  4970. case WPA_IF_AP_VLAN:
  4971. return NL80211_IFTYPE_AP_VLAN;
  4972. case WPA_IF_AP_BSS:
  4973. return NL80211_IFTYPE_AP;
  4974. case WPA_IF_P2P_GO:
  4975. return NL80211_IFTYPE_P2P_GO;
  4976. }
  4977. return -1;
  4978. }
  4979. #ifdef CONFIG_P2P
  4980. static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
  4981. {
  4982. struct wpa_driver_nl80211_data *drv;
  4983. dl_list_for_each(drv, &global->interfaces,
  4984. struct wpa_driver_nl80211_data, list) {
  4985. if (os_memcmp(addr, drv->addr, ETH_ALEN) == 0)
  4986. return 1;
  4987. }
  4988. return 0;
  4989. }
  4990. static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
  4991. u8 *new_addr)
  4992. {
  4993. unsigned int idx;
  4994. if (!drv->global)
  4995. return -1;
  4996. os_memcpy(new_addr, drv->addr, ETH_ALEN);
  4997. for (idx = 0; idx < 64; idx++) {
  4998. new_addr[0] = drv->addr[0] | 0x02;
  4999. new_addr[0] ^= idx << 2;
  5000. if (!nl80211_addr_in_use(drv->global, new_addr))
  5001. break;
  5002. }
  5003. if (idx == 64)
  5004. return -1;
  5005. wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
  5006. MACSTR, MAC2STR(new_addr));
  5007. return 0;
  5008. }
  5009. #endif /* CONFIG_P2P */
  5010. static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
  5011. const char *ifname, const u8 *addr,
  5012. void *bss_ctx, void **drv_priv,
  5013. char *force_ifname, u8 *if_addr,
  5014. const char *bridge)
  5015. {
  5016. struct i802_bss *bss = priv;
  5017. struct wpa_driver_nl80211_data *drv = bss->drv;
  5018. int ifidx;
  5019. #ifdef HOSTAPD
  5020. struct i802_bss *new_bss = NULL;
  5021. if (type == WPA_IF_AP_BSS) {
  5022. new_bss = os_zalloc(sizeof(*new_bss));
  5023. if (new_bss == NULL)
  5024. return -1;
  5025. }
  5026. #endif /* HOSTAPD */
  5027. if (addr)
  5028. os_memcpy(if_addr, addr, ETH_ALEN);
  5029. ifidx = nl80211_create_iface(drv, ifname,
  5030. wpa_driver_nl80211_if_type(type), addr,
  5031. 0);
  5032. if (ifidx < 0) {
  5033. #ifdef HOSTAPD
  5034. os_free(new_bss);
  5035. #endif /* HOSTAPD */
  5036. return -1;
  5037. }
  5038. if (!addr &&
  5039. linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, if_addr) < 0) {
  5040. nl80211_remove_iface(drv, ifidx);
  5041. return -1;
  5042. }
  5043. #ifdef CONFIG_P2P
  5044. if (!addr &&
  5045. (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
  5046. type == WPA_IF_P2P_GO)) {
  5047. /* Enforce unique P2P Interface Address */
  5048. u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
  5049. if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, own_addr)
  5050. < 0 ||
  5051. linux_get_ifhwaddr(drv->ioctl_sock, ifname, new_addr) < 0)
  5052. {
  5053. nl80211_remove_iface(drv, ifidx);
  5054. return -1;
  5055. }
  5056. if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
  5057. wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
  5058. "for P2P group interface");
  5059. if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
  5060. nl80211_remove_iface(drv, ifidx);
  5061. return -1;
  5062. }
  5063. if (linux_set_ifhwaddr(drv->ioctl_sock, ifname,
  5064. new_addr) < 0) {
  5065. nl80211_remove_iface(drv, ifidx);
  5066. return -1;
  5067. }
  5068. }
  5069. os_memcpy(if_addr, new_addr, ETH_ALEN);
  5070. }
  5071. #endif /* CONFIG_P2P */
  5072. #ifdef HOSTAPD
  5073. if (bridge &&
  5074. i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
  5075. wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
  5076. "interface %s to a bridge %s", ifname, bridge);
  5077. nl80211_remove_iface(drv, ifidx);
  5078. os_free(new_bss);
  5079. return -1;
  5080. }
  5081. if (type == WPA_IF_AP_BSS) {
  5082. if (linux_set_iface_flags(drv->ioctl_sock, ifname, 1)) {
  5083. nl80211_remove_iface(drv, ifidx);
  5084. os_free(new_bss);
  5085. return -1;
  5086. }
  5087. os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
  5088. new_bss->ifindex = ifidx;
  5089. new_bss->drv = drv;
  5090. new_bss->next = drv->first_bss.next;
  5091. drv->first_bss.next = new_bss;
  5092. if (drv_priv)
  5093. *drv_priv = new_bss;
  5094. }
  5095. #endif /* HOSTAPD */
  5096. if (drv->global)
  5097. drv->global->if_add_ifindex = ifidx;
  5098. return 0;
  5099. }
  5100. static int wpa_driver_nl80211_if_remove(void *priv,
  5101. enum wpa_driver_if_type type,
  5102. const char *ifname)
  5103. {
  5104. struct i802_bss *bss = priv;
  5105. struct wpa_driver_nl80211_data *drv = bss->drv;
  5106. int ifindex = if_nametoindex(ifname);
  5107. wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
  5108. __func__, type, ifname, ifindex);
  5109. if (ifindex <= 0)
  5110. return -1;
  5111. #ifdef HOSTAPD
  5112. if (bss->added_if_into_bridge) {
  5113. if (linux_br_del_if(drv->ioctl_sock, bss->brname, bss->ifname)
  5114. < 0)
  5115. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  5116. "interface %s from bridge %s: %s",
  5117. bss->ifname, bss->brname, strerror(errno));
  5118. }
  5119. if (bss->added_bridge) {
  5120. if (linux_br_del(drv->ioctl_sock, bss->brname) < 0)
  5121. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  5122. "bridge %s: %s",
  5123. bss->brname, strerror(errno));
  5124. }
  5125. #endif /* HOSTAPD */
  5126. nl80211_remove_iface(drv, ifindex);
  5127. #ifdef HOSTAPD
  5128. if (type != WPA_IF_AP_BSS)
  5129. return 0;
  5130. if (bss != &drv->first_bss) {
  5131. struct i802_bss *tbss;
  5132. for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
  5133. if (tbss->next == bss) {
  5134. tbss->next = bss->next;
  5135. os_free(bss);
  5136. bss = NULL;
  5137. break;
  5138. }
  5139. }
  5140. if (bss)
  5141. wpa_printf(MSG_INFO, "nl80211: %s - could not find "
  5142. "BSS %p in the list", __func__, bss);
  5143. }
  5144. #endif /* HOSTAPD */
  5145. return 0;
  5146. }
  5147. static int cookie_handler(struct nl_msg *msg, void *arg)
  5148. {
  5149. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  5150. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  5151. u64 *cookie = arg;
  5152. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  5153. genlmsg_attrlen(gnlh, 0), NULL);
  5154. if (tb[NL80211_ATTR_COOKIE])
  5155. *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
  5156. return NL_SKIP;
  5157. }
  5158. static int nl80211_send_frame_cmd(struct wpa_driver_nl80211_data *drv,
  5159. unsigned int freq, unsigned int wait,
  5160. const u8 *buf, size_t buf_len,
  5161. u64 *cookie_out)
  5162. {
  5163. struct nl_msg *msg;
  5164. u64 cookie;
  5165. int ret = -1;
  5166. msg = nlmsg_alloc();
  5167. if (!msg)
  5168. return -1;
  5169. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  5170. NL80211_CMD_FRAME, 0);
  5171. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5172. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  5173. NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
  5174. NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
  5175. NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
  5176. cookie = 0;
  5177. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  5178. msg = NULL;
  5179. if (ret) {
  5180. wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
  5181. "(%s)", ret, strerror(-ret));
  5182. goto nla_put_failure;
  5183. }
  5184. wpa_printf(MSG_DEBUG, "nl80211: Frame TX command accepted; "
  5185. "cookie 0x%llx", (long long unsigned int) cookie);
  5186. if (cookie_out)
  5187. *cookie_out = cookie;
  5188. nla_put_failure:
  5189. nlmsg_free(msg);
  5190. return ret;
  5191. }
  5192. static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq,
  5193. unsigned int wait_time,
  5194. const u8 *dst, const u8 *src,
  5195. const u8 *bssid,
  5196. const u8 *data, size_t data_len)
  5197. {
  5198. struct i802_bss *bss = priv;
  5199. struct wpa_driver_nl80211_data *drv = bss->drv;
  5200. int ret = -1;
  5201. u8 *buf;
  5202. struct ieee80211_hdr *hdr;
  5203. wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
  5204. "wait=%d ms)", drv->ifindex, wait_time);
  5205. buf = os_zalloc(24 + data_len);
  5206. if (buf == NULL)
  5207. return ret;
  5208. os_memcpy(buf + 24, data, data_len);
  5209. hdr = (struct ieee80211_hdr *) buf;
  5210. hdr->frame_control =
  5211. IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
  5212. os_memcpy(hdr->addr1, dst, ETH_ALEN);
  5213. os_memcpy(hdr->addr2, src, ETH_ALEN);
  5214. os_memcpy(hdr->addr3, bssid, ETH_ALEN);
  5215. if (drv->nlmode == NL80211_IFTYPE_AP)
  5216. ret = wpa_driver_nl80211_send_mlme(priv, buf, 24 + data_len);
  5217. else
  5218. ret = nl80211_send_frame_cmd(drv, freq, wait_time, buf,
  5219. 24 + data_len,
  5220. &drv->send_action_cookie);
  5221. os_free(buf);
  5222. return ret;
  5223. }
  5224. static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
  5225. {
  5226. struct i802_bss *bss = priv;
  5227. struct wpa_driver_nl80211_data *drv = bss->drv;
  5228. struct nl_msg *msg;
  5229. int ret;
  5230. msg = nlmsg_alloc();
  5231. if (!msg)
  5232. return;
  5233. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  5234. NL80211_CMD_FRAME_WAIT_CANCEL, 0);
  5235. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5236. NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
  5237. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5238. msg = NULL;
  5239. if (ret)
  5240. wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
  5241. "(%s)", ret, strerror(-ret));
  5242. nla_put_failure:
  5243. nlmsg_free(msg);
  5244. }
  5245. static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
  5246. unsigned int duration)
  5247. {
  5248. struct i802_bss *bss = priv;
  5249. struct wpa_driver_nl80211_data *drv = bss->drv;
  5250. struct nl_msg *msg;
  5251. int ret;
  5252. u64 cookie;
  5253. msg = nlmsg_alloc();
  5254. if (!msg)
  5255. return -1;
  5256. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  5257. NL80211_CMD_REMAIN_ON_CHANNEL, 0);
  5258. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5259. NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
  5260. NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
  5261. cookie = 0;
  5262. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  5263. if (ret == 0) {
  5264. wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
  5265. "0x%llx for freq=%u MHz duration=%u",
  5266. (long long unsigned int) cookie, freq, duration);
  5267. drv->remain_on_chan_cookie = cookie;
  5268. return 0;
  5269. }
  5270. wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
  5271. "(freq=%d duration=%u): %d (%s)",
  5272. freq, duration, ret, strerror(-ret));
  5273. nla_put_failure:
  5274. return -1;
  5275. }
  5276. static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
  5277. {
  5278. struct i802_bss *bss = priv;
  5279. struct wpa_driver_nl80211_data *drv = bss->drv;
  5280. struct nl_msg *msg;
  5281. int ret;
  5282. if (!drv->pending_remain_on_chan) {
  5283. wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
  5284. "to cancel");
  5285. return -1;
  5286. }
  5287. wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
  5288. "0x%llx",
  5289. (long long unsigned int) drv->remain_on_chan_cookie);
  5290. msg = nlmsg_alloc();
  5291. if (!msg)
  5292. return -1;
  5293. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  5294. NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL, 0);
  5295. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
  5296. NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
  5297. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5298. if (ret == 0)
  5299. return 0;
  5300. wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
  5301. "%d (%s)", ret, strerror(-ret));
  5302. nla_put_failure:
  5303. return -1;
  5304. }
  5305. static int wpa_driver_nl80211_probe_req_report(void *priv, int report)
  5306. {
  5307. struct i802_bss *bss = priv;
  5308. struct wpa_driver_nl80211_data *drv = bss->drv;
  5309. if (drv->nlmode != NL80211_IFTYPE_STATION) {
  5310. wpa_printf(MSG_DEBUG, "nl80211: probe_req_report control only "
  5311. "allowed in station mode (iftype=%d)",
  5312. drv->nlmode);
  5313. return -1;
  5314. }
  5315. if (!report) {
  5316. if (drv->nl_handle_preq) {
  5317. eloop_unregister_read_sock(
  5318. nl_socket_get_fd(drv->nl_handle_preq));
  5319. nl_cache_free(drv->nl_cache_preq);
  5320. nl80211_handle_destroy(drv->nl_handle_preq);
  5321. drv->nl_handle_preq = NULL;
  5322. }
  5323. return 0;
  5324. }
  5325. if (drv->nl_handle_preq) {
  5326. wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
  5327. "already on!");
  5328. return 0;
  5329. }
  5330. drv->nl_handle_preq = nl80211_handle_alloc(drv->nl_cb);
  5331. if (drv->nl_handle_preq == NULL) {
  5332. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate "
  5333. "netlink callbacks (preq)");
  5334. goto out_err1;
  5335. }
  5336. if (genl_connect(drv->nl_handle_preq)) {
  5337. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to "
  5338. "generic netlink (preq)");
  5339. goto out_err2;
  5340. return -1;
  5341. }
  5342. #ifdef CONFIG_LIBNL20
  5343. if (genl_ctrl_alloc_cache(drv->nl_handle_preq,
  5344. &drv->nl_cache_preq) < 0) {
  5345. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  5346. "netlink cache (preq)");
  5347. goto out_err2;
  5348. }
  5349. #else /* CONFIG_LIBNL20 */
  5350. drv->nl_cache_preq = genl_ctrl_alloc_cache(drv->nl_handle_preq);
  5351. if (drv->nl_cache_preq == NULL) {
  5352. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
  5353. "netlink cache (preq)");
  5354. goto out_err2;
  5355. }
  5356. #endif /* CONFIG_LIBNL20 */
  5357. if (nl80211_register_frame(drv, drv->nl_handle_preq,
  5358. (WLAN_FC_TYPE_MGMT << 2) |
  5359. (WLAN_FC_STYPE_PROBE_REQ << 4),
  5360. NULL, 0) < 0) {
  5361. goto out_err3;
  5362. }
  5363. eloop_register_read_sock(nl_socket_get_fd(drv->nl_handle_preq),
  5364. wpa_driver_nl80211_event_receive, drv,
  5365. drv->nl_handle_preq);
  5366. return 0;
  5367. out_err3:
  5368. nl_cache_free(drv->nl_cache_preq);
  5369. out_err2:
  5370. nl80211_handle_destroy(drv->nl_handle_preq);
  5371. drv->nl_handle_preq = NULL;
  5372. out_err1:
  5373. return -1;
  5374. }
  5375. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  5376. int ifindex, int disabled)
  5377. {
  5378. struct nl_msg *msg;
  5379. struct nlattr *bands, *band;
  5380. int ret;
  5381. msg = nlmsg_alloc();
  5382. if (!msg)
  5383. return -1;
  5384. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  5385. NL80211_CMD_SET_TX_BITRATE_MASK, 0);
  5386. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
  5387. bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
  5388. if (!bands)
  5389. goto nla_put_failure;
  5390. /*
  5391. * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
  5392. * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
  5393. * rates. All 5 GHz rates are left enabled.
  5394. */
  5395. band = nla_nest_start(msg, NL80211_BAND_2GHZ);
  5396. if (!band)
  5397. goto nla_put_failure;
  5398. NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
  5399. "\x0c\x12\x18\x24\x30\x48\x60\x6c");
  5400. nla_nest_end(msg, band);
  5401. nla_nest_end(msg, bands);
  5402. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5403. msg = NULL;
  5404. if (ret) {
  5405. wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
  5406. "(%s)", ret, strerror(-ret));
  5407. }
  5408. return ret;
  5409. nla_put_failure:
  5410. nlmsg_free(msg);
  5411. return -1;
  5412. }
  5413. static int wpa_driver_nl80211_disable_11b_rates(void *priv, int disabled)
  5414. {
  5415. struct i802_bss *bss = priv;
  5416. struct wpa_driver_nl80211_data *drv = bss->drv;
  5417. drv->disable_11b_rates = disabled;
  5418. return nl80211_disable_11b_rates(drv, drv->ifindex, disabled);
  5419. }
  5420. static int wpa_driver_nl80211_deinit_ap(void *priv)
  5421. {
  5422. struct i802_bss *bss = priv;
  5423. struct wpa_driver_nl80211_data *drv = bss->drv;
  5424. if (drv->nlmode != NL80211_IFTYPE_AP)
  5425. return -1;
  5426. wpa_driver_nl80211_del_beacon(drv);
  5427. return wpa_driver_nl80211_set_mode(priv, IEEE80211_MODE_INFRA);
  5428. }
  5429. static void wpa_driver_nl80211_resume(void *priv)
  5430. {
  5431. struct i802_bss *bss = priv;
  5432. struct wpa_driver_nl80211_data *drv = bss->drv;
  5433. if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1)) {
  5434. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
  5435. "resume event");
  5436. }
  5437. }
  5438. static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
  5439. const u8 *ies, size_t ies_len)
  5440. {
  5441. struct i802_bss *bss = priv;
  5442. struct wpa_driver_nl80211_data *drv = bss->drv;
  5443. int ret;
  5444. u8 *data, *pos;
  5445. size_t data_len;
  5446. u8 own_addr[ETH_ALEN];
  5447. if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, own_addr) < 0)
  5448. return -1;
  5449. if (action != 1) {
  5450. wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
  5451. "action %d", action);
  5452. return -1;
  5453. }
  5454. /*
  5455. * Action frame payload:
  5456. * Category[1] = 6 (Fast BSS Transition)
  5457. * Action[1] = 1 (Fast BSS Transition Request)
  5458. * STA Address
  5459. * Target AP Address
  5460. * FT IEs
  5461. */
  5462. data_len = 2 + 2 * ETH_ALEN + ies_len;
  5463. data = os_malloc(data_len);
  5464. if (data == NULL)
  5465. return -1;
  5466. pos = data;
  5467. *pos++ = 0x06; /* FT Action category */
  5468. *pos++ = action;
  5469. os_memcpy(pos, own_addr, ETH_ALEN);
  5470. pos += ETH_ALEN;
  5471. os_memcpy(pos, target_ap, ETH_ALEN);
  5472. pos += ETH_ALEN;
  5473. os_memcpy(pos, ies, ies_len);
  5474. ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
  5475. drv->bssid, own_addr, drv->bssid,
  5476. data, data_len);
  5477. os_free(data);
  5478. return ret;
  5479. }
  5480. static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
  5481. {
  5482. struct i802_bss *bss = priv;
  5483. struct wpa_driver_nl80211_data *drv = bss->drv;
  5484. struct nl_msg *msg, *cqm = NULL;
  5485. wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
  5486. "hysteresis=%d", threshold, hysteresis);
  5487. msg = nlmsg_alloc();
  5488. if (!msg)
  5489. return -1;
  5490. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
  5491. 0, NL80211_CMD_SET_CQM, 0);
  5492. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  5493. cqm = nlmsg_alloc();
  5494. if (cqm == NULL)
  5495. return -1;
  5496. NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
  5497. NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
  5498. nla_put_nested(msg, NL80211_ATTR_CQM, cqm);
  5499. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  5500. return 0;
  5501. msg = NULL;
  5502. nla_put_failure:
  5503. if (cqm)
  5504. nlmsg_free(cqm);
  5505. nlmsg_free(msg);
  5506. return -1;
  5507. }
  5508. static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
  5509. {
  5510. struct i802_bss *bss = priv;
  5511. struct wpa_driver_nl80211_data *drv = bss->drv;
  5512. int res;
  5513. os_memset(si, 0, sizeof(*si));
  5514. res = nl80211_get_link_signal(drv, si);
  5515. if (res != 0)
  5516. return res;
  5517. return nl80211_get_link_noise(drv, si);
  5518. }
  5519. static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
  5520. int encrypt)
  5521. {
  5522. struct i802_bss *bss = priv;
  5523. struct wpa_driver_nl80211_data *drv = bss->drv;
  5524. return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
  5525. }
  5526. static int nl80211_set_intra_bss(void *priv, int enabled)
  5527. {
  5528. struct i802_bss *bss = priv;
  5529. struct wpa_driver_nl80211_data *drv = bss->drv;
  5530. struct nl_msg *msg;
  5531. msg = nlmsg_alloc();
  5532. if (!msg)
  5533. return -ENOMEM;
  5534. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  5535. NL80211_CMD_SET_BSS, 0);
  5536. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  5537. NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, !enabled);
  5538. return send_and_recv_msgs(drv, msg, NULL, NULL);
  5539. nla_put_failure:
  5540. return -ENOBUFS;
  5541. }
  5542. static int nl80211_set_param(void *priv, const char *param)
  5543. {
  5544. wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
  5545. if (param == NULL)
  5546. return 0;
  5547. #ifdef CONFIG_P2P
  5548. if (os_strstr(param, "use_p2p_group_interface=1")) {
  5549. struct i802_bss *bss = priv;
  5550. struct wpa_driver_nl80211_data *drv = bss->drv;
  5551. wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
  5552. "interface");
  5553. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
  5554. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
  5555. }
  5556. #endif /* CONFIG_P2P */
  5557. return 0;
  5558. }
  5559. static void * nl80211_global_init(void)
  5560. {
  5561. struct nl80211_global *global;
  5562. global = os_zalloc(sizeof(*global));
  5563. if (global == NULL)
  5564. return NULL;
  5565. dl_list_init(&global->interfaces);
  5566. global->if_add_ifindex = -1;
  5567. return global;
  5568. }
  5569. static void nl80211_global_deinit(void *priv)
  5570. {
  5571. struct nl80211_global *global = priv;
  5572. if (global == NULL)
  5573. return;
  5574. if (!dl_list_empty(&global->interfaces)) {
  5575. wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
  5576. "nl80211_global_deinit",
  5577. dl_list_len(&global->interfaces));
  5578. }
  5579. os_free(global);
  5580. }
  5581. static const char * nl80211_get_radio_name(void *priv)
  5582. {
  5583. struct i802_bss *bss = priv;
  5584. struct wpa_driver_nl80211_data *drv = bss->drv;
  5585. return drv->phyname;
  5586. }
  5587. static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
  5588. const u8 *pmkid)
  5589. {
  5590. struct nl_msg *msg;
  5591. msg = nlmsg_alloc();
  5592. if (!msg)
  5593. return -ENOMEM;
  5594. genlmsg_put(msg, 0, 0, genl_family_get_id(bss->drv->nl80211), 0, 0,
  5595. cmd, 0);
  5596. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
  5597. if (pmkid)
  5598. NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
  5599. if (bssid)
  5600. NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
  5601. return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  5602. nla_put_failure:
  5603. return -ENOBUFS;
  5604. }
  5605. static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
  5606. {
  5607. struct i802_bss *bss = priv;
  5608. wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
  5609. return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
  5610. }
  5611. static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
  5612. {
  5613. struct i802_bss *bss = priv;
  5614. wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
  5615. MAC2STR(bssid));
  5616. return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
  5617. }
  5618. static int nl80211_flush_pmkid(void *priv)
  5619. {
  5620. struct i802_bss *bss = priv;
  5621. wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
  5622. return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
  5623. }
  5624. static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
  5625. const u8 *replay_ctr)
  5626. {
  5627. struct i802_bss *bss = priv;
  5628. struct wpa_driver_nl80211_data *drv = bss->drv;
  5629. struct nlattr *replay_nested;
  5630. struct nl_msg *msg;
  5631. msg = nlmsg_alloc();
  5632. if (!msg)
  5633. return;
  5634. genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
  5635. NL80211_CMD_SET_REKEY_OFFLOAD, 0);
  5636. NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  5637. replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
  5638. if (!replay_nested)
  5639. goto nla_put_failure;
  5640. NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
  5641. NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
  5642. NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
  5643. replay_ctr);
  5644. nla_nest_end(msg, replay_nested);
  5645. send_and_recv_msgs(drv, msg, NULL, NULL);
  5646. return;
  5647. nla_put_failure:
  5648. nlmsg_free(msg);
  5649. }
  5650. const struct wpa_driver_ops wpa_driver_nl80211_ops = {
  5651. .name = "nl80211",
  5652. .desc = "Linux nl80211/cfg80211",
  5653. .get_bssid = wpa_driver_nl80211_get_bssid,
  5654. .get_ssid = wpa_driver_nl80211_get_ssid,
  5655. .set_key = wpa_driver_nl80211_set_key,
  5656. .scan2 = wpa_driver_nl80211_scan,
  5657. .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
  5658. .deauthenticate = wpa_driver_nl80211_deauthenticate,
  5659. .disassociate = wpa_driver_nl80211_disassociate,
  5660. .authenticate = wpa_driver_nl80211_authenticate,
  5661. .associate = wpa_driver_nl80211_associate,
  5662. .global_init = nl80211_global_init,
  5663. .global_deinit = nl80211_global_deinit,
  5664. .init2 = wpa_driver_nl80211_init,
  5665. .deinit = wpa_driver_nl80211_deinit,
  5666. .get_capa = wpa_driver_nl80211_get_capa,
  5667. .set_operstate = wpa_driver_nl80211_set_operstate,
  5668. .set_supp_port = wpa_driver_nl80211_set_supp_port,
  5669. .set_country = wpa_driver_nl80211_set_country,
  5670. .set_beacon = wpa_driver_nl80211_set_beacon,
  5671. .if_add = wpa_driver_nl80211_if_add,
  5672. .if_remove = wpa_driver_nl80211_if_remove,
  5673. .send_mlme = wpa_driver_nl80211_send_mlme,
  5674. .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
  5675. .sta_add = wpa_driver_nl80211_sta_add,
  5676. .sta_remove = wpa_driver_nl80211_sta_remove,
  5677. .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
  5678. .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
  5679. #ifdef HOSTAPD
  5680. .hapd_init = i802_init,
  5681. .hapd_deinit = i802_deinit,
  5682. .set_wds_sta = i802_set_wds_sta,
  5683. #endif /* HOSTAPD */
  5684. #if defined(HOSTAPD) || defined(CONFIG_AP)
  5685. .get_seqnum = i802_get_seqnum,
  5686. .flush = i802_flush,
  5687. .read_sta_data = i802_read_sta_data,
  5688. .get_inact_sec = i802_get_inact_sec,
  5689. .sta_clear_stats = i802_sta_clear_stats,
  5690. .set_rts = i802_set_rts,
  5691. .set_frag = i802_set_frag,
  5692. .set_cts_protect = i802_set_cts_protect,
  5693. .set_preamble = i802_set_preamble,
  5694. .set_short_slot_time = i802_set_short_slot_time,
  5695. .set_tx_queue_params = i802_set_tx_queue_params,
  5696. .set_sta_vlan = i802_set_sta_vlan,
  5697. .set_ht_params = i802_set_ht_params,
  5698. .set_rate_sets = i802_set_rate_sets,
  5699. .sta_deauth = i802_sta_deauth,
  5700. .sta_disassoc = i802_sta_disassoc,
  5701. #endif /* HOSTAPD || CONFIG_AP */
  5702. .set_freq = i802_set_freq,
  5703. .send_action = wpa_driver_nl80211_send_action,
  5704. .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
  5705. .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
  5706. .cancel_remain_on_channel =
  5707. wpa_driver_nl80211_cancel_remain_on_channel,
  5708. .probe_req_report = wpa_driver_nl80211_probe_req_report,
  5709. .disable_11b_rates = wpa_driver_nl80211_disable_11b_rates,
  5710. .deinit_ap = wpa_driver_nl80211_deinit_ap,
  5711. .resume = wpa_driver_nl80211_resume,
  5712. .send_ft_action = nl80211_send_ft_action,
  5713. .signal_monitor = nl80211_signal_monitor,
  5714. .signal_poll = nl80211_signal_poll,
  5715. .send_frame = nl80211_send_frame,
  5716. .set_intra_bss = nl80211_set_intra_bss,
  5717. .set_param = nl80211_set_param,
  5718. .get_radio_name = nl80211_get_radio_name,
  5719. .add_pmkid = nl80211_add_pmkid,
  5720. .remove_pmkid = nl80211_remove_pmkid,
  5721. .flush_pmkid = nl80211_flush_pmkid,
  5722. .set_rekey_info = nl80211_set_rekey_info,
  5723. };