driver_nl80211.c 261 KB

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