driver_nl80211.c 182 KB

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