driver_nl80211.c 318 KB

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