driver_nl80211.c 169 KB

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