driver_nl80211.c 187 KB

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