driver_nl80211.c 334 KB

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