driver_nl80211.c 302 KB

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