driver_nl80211.c 244 KB

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