ctrl_iface.c 270 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #ifdef CONFIG_TESTING_OPTIONS
  10. #include <net/ethernet.h>
  11. #include <netinet/ip.h>
  12. #endif /* CONFIG_TESTING_OPTIONS */
  13. #include "utils/common.h"
  14. #include "utils/eloop.h"
  15. #include "utils/uuid.h"
  16. #include "utils/module_tests.h"
  17. #include "common/version.h"
  18. #include "common/ieee802_11_defs.h"
  19. #include "common/ieee802_11_common.h"
  20. #include "common/wpa_ctrl.h"
  21. #include "common/dpp.h"
  22. #include "crypto/tls.h"
  23. #include "ap/hostapd.h"
  24. #include "eap_peer/eap.h"
  25. #include "eapol_supp/eapol_supp_sm.h"
  26. #include "rsn_supp/wpa.h"
  27. #include "rsn_supp/preauth.h"
  28. #include "rsn_supp/pmksa_cache.h"
  29. #include "l2_packet/l2_packet.h"
  30. #include "wps/wps.h"
  31. #include "fst/fst.h"
  32. #include "fst/fst_ctrl_iface.h"
  33. #include "config.h"
  34. #include "wpa_supplicant_i.h"
  35. #include "driver_i.h"
  36. #include "wps_supplicant.h"
  37. #include "ibss_rsn.h"
  38. #include "ap.h"
  39. #include "p2p_supplicant.h"
  40. #include "p2p/p2p.h"
  41. #include "hs20_supplicant.h"
  42. #include "wifi_display.h"
  43. #include "notify.h"
  44. #include "bss.h"
  45. #include "scan.h"
  46. #include "ctrl_iface.h"
  47. #include "interworking.h"
  48. #include "blacklist.h"
  49. #include "autoscan.h"
  50. #include "wnm_sta.h"
  51. #include "offchannel.h"
  52. #include "drivers/driver.h"
  53. #include "mesh.h"
  54. #include "dpp_supplicant.h"
  55. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  56. char *buf, int len);
  57. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  58. const char *input,
  59. char *buf, int len);
  60. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
  61. char *val);
  62. #ifdef CONFIG_FILS
  63. static int wpa_is_fils_supported(struct wpa_supplicant *wpa_s)
  64. {
  65. return (((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  66. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_FILS)) ||
  67. (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  68. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_FILS_SK_OFFLOAD)));
  69. }
  70. #ifdef CONFIG_FILS_SK_PFS
  71. static int wpa_is_fils_sk_pfs_supported(struct wpa_supplicant *wpa_s)
  72. {
  73. return (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  74. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_FILS);
  75. }
  76. #endif /* CONFIG_FILS_SK_PFS */
  77. #endif /* CONFIG_FILS */
  78. static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
  79. {
  80. char *pos;
  81. u8 addr[ETH_ALEN], *filter = NULL, *n;
  82. size_t count = 0;
  83. pos = val;
  84. while (pos) {
  85. if (*pos == '\0')
  86. break;
  87. if (hwaddr_aton(pos, addr)) {
  88. os_free(filter);
  89. return -1;
  90. }
  91. n = os_realloc_array(filter, count + 1, ETH_ALEN);
  92. if (n == NULL) {
  93. os_free(filter);
  94. return -1;
  95. }
  96. filter = n;
  97. os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
  98. count++;
  99. pos = os_strchr(pos, ' ');
  100. if (pos)
  101. pos++;
  102. }
  103. wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
  104. os_free(wpa_s->bssid_filter);
  105. wpa_s->bssid_filter = filter;
  106. wpa_s->bssid_filter_count = count;
  107. return 0;
  108. }
  109. static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
  110. {
  111. char *pos;
  112. u8 addr[ETH_ALEN], *bssid = NULL, *n;
  113. struct wpa_ssid_value *ssid = NULL, *ns;
  114. size_t count = 0, ssid_count = 0;
  115. struct wpa_ssid *c;
  116. /*
  117. * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
  118. * SSID_SPEC ::= ssid <SSID_HEX>
  119. * BSSID_SPEC ::= bssid <BSSID_HEX>
  120. */
  121. pos = val;
  122. while (pos) {
  123. if (*pos == '\0')
  124. break;
  125. if (os_strncmp(pos, "bssid ", 6) == 0) {
  126. int res;
  127. pos += 6;
  128. res = hwaddr_aton2(pos, addr);
  129. if (res < 0) {
  130. os_free(ssid);
  131. os_free(bssid);
  132. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  133. "BSSID value '%s'", pos);
  134. return -1;
  135. }
  136. pos += res;
  137. n = os_realloc_array(bssid, count + 1, ETH_ALEN);
  138. if (n == NULL) {
  139. os_free(ssid);
  140. os_free(bssid);
  141. return -1;
  142. }
  143. bssid = n;
  144. os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
  145. count++;
  146. } else if (os_strncmp(pos, "ssid ", 5) == 0) {
  147. char *end;
  148. pos += 5;
  149. end = pos;
  150. while (*end) {
  151. if (*end == '\0' || *end == ' ')
  152. break;
  153. end++;
  154. }
  155. ns = os_realloc_array(ssid, ssid_count + 1,
  156. sizeof(struct wpa_ssid_value));
  157. if (ns == NULL) {
  158. os_free(ssid);
  159. os_free(bssid);
  160. return -1;
  161. }
  162. ssid = ns;
  163. if ((end - pos) & 0x01 ||
  164. end - pos > 2 * SSID_MAX_LEN ||
  165. hexstr2bin(pos, ssid[ssid_count].ssid,
  166. (end - pos) / 2) < 0) {
  167. os_free(ssid);
  168. os_free(bssid);
  169. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  170. "SSID value '%s'", pos);
  171. return -1;
  172. }
  173. ssid[ssid_count].ssid_len = (end - pos) / 2;
  174. wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
  175. ssid[ssid_count].ssid,
  176. ssid[ssid_count].ssid_len);
  177. ssid_count++;
  178. pos = end;
  179. } else {
  180. wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
  181. "'%s'", pos);
  182. os_free(ssid);
  183. os_free(bssid);
  184. return -1;
  185. }
  186. pos = os_strchr(pos, ' ');
  187. if (pos)
  188. pos++;
  189. }
  190. wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
  191. os_free(wpa_s->disallow_aps_bssid);
  192. wpa_s->disallow_aps_bssid = bssid;
  193. wpa_s->disallow_aps_bssid_count = count;
  194. wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
  195. os_free(wpa_s->disallow_aps_ssid);
  196. wpa_s->disallow_aps_ssid = ssid;
  197. wpa_s->disallow_aps_ssid_count = ssid_count;
  198. if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
  199. return 0;
  200. c = wpa_s->current_ssid;
  201. if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
  202. return 0;
  203. if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
  204. !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
  205. return 0;
  206. wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
  207. "because current AP was marked disallowed");
  208. #ifdef CONFIG_SME
  209. wpa_s->sme.prev_bssid_set = 0;
  210. #endif /* CONFIG_SME */
  211. wpa_s->reassociate = 1;
  212. wpa_s->own_disconnect_req = 1;
  213. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  214. wpa_supplicant_req_scan(wpa_s, 0, 0);
  215. return 0;
  216. }
  217. #ifndef CONFIG_NO_CONFIG_BLOBS
  218. static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
  219. {
  220. char *name = pos;
  221. struct wpa_config_blob *blob;
  222. size_t len;
  223. pos = os_strchr(pos, ' ');
  224. if (pos == NULL)
  225. return -1;
  226. *pos++ = '\0';
  227. len = os_strlen(pos);
  228. if (len & 1)
  229. return -1;
  230. wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
  231. blob = os_zalloc(sizeof(*blob));
  232. if (blob == NULL)
  233. return -1;
  234. blob->name = os_strdup(name);
  235. blob->data = os_malloc(len / 2);
  236. if (blob->name == NULL || blob->data == NULL) {
  237. wpa_config_free_blob(blob);
  238. return -1;
  239. }
  240. if (hexstr2bin(pos, blob->data, len / 2) < 0) {
  241. wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
  242. wpa_config_free_blob(blob);
  243. return -1;
  244. }
  245. blob->len = len / 2;
  246. wpa_config_set_blob(wpa_s->conf, blob);
  247. return 0;
  248. }
  249. #endif /* CONFIG_NO_CONFIG_BLOBS */
  250. static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
  251. {
  252. char *params;
  253. char *pos;
  254. int *freqs = NULL;
  255. int ret;
  256. if (atoi(cmd)) {
  257. params = os_strchr(cmd, ' ');
  258. os_free(wpa_s->manual_sched_scan_freqs);
  259. if (params) {
  260. params++;
  261. pos = os_strstr(params, "freq=");
  262. if (pos)
  263. freqs = freq_range_to_channel_list(wpa_s,
  264. pos + 5);
  265. }
  266. wpa_s->manual_sched_scan_freqs = freqs;
  267. ret = wpas_start_pno(wpa_s);
  268. } else {
  269. ret = wpas_stop_pno(wpa_s);
  270. }
  271. return ret;
  272. }
  273. static int wpas_ctrl_set_band(struct wpa_supplicant *wpa_s, char *band)
  274. {
  275. union wpa_event_data event;
  276. if (os_strcmp(band, "AUTO") == 0)
  277. wpa_s->setband = WPA_SETBAND_AUTO;
  278. else if (os_strcmp(band, "5G") == 0)
  279. wpa_s->setband = WPA_SETBAND_5G;
  280. else if (os_strcmp(band, "2G") == 0)
  281. wpa_s->setband = WPA_SETBAND_2G;
  282. else
  283. return -1;
  284. if (wpa_drv_setband(wpa_s, wpa_s->setband) == 0) {
  285. os_memset(&event, 0, sizeof(event));
  286. event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
  287. event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
  288. wpa_supplicant_event(wpa_s, EVENT_CHANNEL_LIST_CHANGED, &event);
  289. }
  290. return 0;
  291. }
  292. static int wpas_ctrl_iface_set_lci(struct wpa_supplicant *wpa_s,
  293. const char *cmd)
  294. {
  295. struct wpabuf *lci;
  296. if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
  297. wpabuf_free(wpa_s->lci);
  298. wpa_s->lci = NULL;
  299. return 0;
  300. }
  301. lci = wpabuf_parse_bin(cmd);
  302. if (!lci)
  303. return -1;
  304. if (os_get_reltime(&wpa_s->lci_time)) {
  305. wpabuf_free(lci);
  306. return -1;
  307. }
  308. wpabuf_free(wpa_s->lci);
  309. wpa_s->lci = lci;
  310. return 0;
  311. }
  312. static int
  313. wpas_ctrl_set_relative_rssi(struct wpa_supplicant *wpa_s, const char *cmd)
  314. {
  315. int relative_rssi;
  316. if (os_strcmp(cmd, "disable") == 0) {
  317. wpa_s->srp.relative_rssi_set = 0;
  318. return 0;
  319. }
  320. relative_rssi = atoi(cmd);
  321. if (relative_rssi < 0 || relative_rssi > 100)
  322. return -1;
  323. wpa_s->srp.relative_rssi = relative_rssi;
  324. wpa_s->srp.relative_rssi_set = 1;
  325. return 0;
  326. }
  327. static int wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant *wpa_s,
  328. const char *cmd)
  329. {
  330. char *pos;
  331. int adjust_rssi;
  332. /* <band>:adjust_value */
  333. pos = os_strchr(cmd, ':');
  334. if (!pos)
  335. return -1;
  336. pos++;
  337. adjust_rssi = atoi(pos);
  338. if (adjust_rssi < -100 || adjust_rssi > 100)
  339. return -1;
  340. if (os_strncmp(cmd, "2G", 2) == 0)
  341. wpa_s->srp.relative_adjust_band = WPA_SETBAND_2G;
  342. else if (os_strncmp(cmd, "5G", 2) == 0)
  343. wpa_s->srp.relative_adjust_band = WPA_SETBAND_5G;
  344. else
  345. return -1;
  346. wpa_s->srp.relative_adjust_rssi = adjust_rssi;
  347. return 0;
  348. }
  349. static int wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant *wpa_s,
  350. const char *cmd)
  351. {
  352. struct wpabuf *ric_ies;
  353. if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
  354. wpabuf_free(wpa_s->ric_ies);
  355. wpa_s->ric_ies = NULL;
  356. return 0;
  357. }
  358. ric_ies = wpabuf_parse_bin(cmd);
  359. if (!ric_ies)
  360. return -1;
  361. wpabuf_free(wpa_s->ric_ies);
  362. wpa_s->ric_ies = ric_ies;
  363. return 0;
  364. }
  365. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  366. char *cmd)
  367. {
  368. char *value;
  369. int ret = 0;
  370. value = os_strchr(cmd, ' ');
  371. if (value == NULL)
  372. return -1;
  373. *value++ = '\0';
  374. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  375. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  376. eapol_sm_configure(wpa_s->eapol,
  377. atoi(value), -1, -1, -1);
  378. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  379. eapol_sm_configure(wpa_s->eapol,
  380. -1, atoi(value), -1, -1);
  381. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  382. eapol_sm_configure(wpa_s->eapol,
  383. -1, -1, atoi(value), -1);
  384. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  385. eapol_sm_configure(wpa_s->eapol,
  386. -1, -1, -1, atoi(value));
  387. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  388. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  389. atoi(value))) {
  390. ret = -1;
  391. } else {
  392. value[-1] = '=';
  393. wpa_config_process_global(wpa_s->conf, cmd, -1);
  394. }
  395. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  396. 0) {
  397. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  398. atoi(value))) {
  399. ret = -1;
  400. } else {
  401. value[-1] = '=';
  402. wpa_config_process_global(wpa_s->conf, cmd, -1);
  403. }
  404. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  405. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
  406. atoi(value))) {
  407. ret = -1;
  408. } else {
  409. value[-1] = '=';
  410. wpa_config_process_global(wpa_s->conf, cmd, -1);
  411. }
  412. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  413. wpa_s->wps_fragment_size = atoi(value);
  414. #ifdef CONFIG_WPS_TESTING
  415. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  416. long int val;
  417. val = strtol(value, NULL, 0);
  418. if (val < 0 || val > 0xff) {
  419. ret = -1;
  420. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  421. "wps_version_number %ld", val);
  422. } else {
  423. wps_version_number = val;
  424. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  425. "version %u.%u",
  426. (wps_version_number & 0xf0) >> 4,
  427. wps_version_number & 0x0f);
  428. }
  429. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  430. wps_testing_dummy_cred = atoi(value);
  431. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  432. wps_testing_dummy_cred);
  433. } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
  434. wps_corrupt_pkhash = atoi(value);
  435. wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
  436. wps_corrupt_pkhash);
  437. } else if (os_strcasecmp(cmd, "wps_force_auth_types") == 0) {
  438. if (value[0] == '\0') {
  439. wps_force_auth_types_in_use = 0;
  440. } else {
  441. wps_force_auth_types = strtol(value, NULL, 0);
  442. wps_force_auth_types_in_use = 1;
  443. }
  444. } else if (os_strcasecmp(cmd, "wps_force_encr_types") == 0) {
  445. if (value[0] == '\0') {
  446. wps_force_encr_types_in_use = 0;
  447. } else {
  448. wps_force_encr_types = strtol(value, NULL, 0);
  449. wps_force_encr_types_in_use = 1;
  450. }
  451. #endif /* CONFIG_WPS_TESTING */
  452. } else if (os_strcasecmp(cmd, "ampdu") == 0) {
  453. if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
  454. ret = -1;
  455. #ifdef CONFIG_TDLS
  456. #ifdef CONFIG_TDLS_TESTING
  457. } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
  458. tdls_testing = strtol(value, NULL, 0);
  459. wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
  460. #endif /* CONFIG_TDLS_TESTING */
  461. } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
  462. int disabled = atoi(value);
  463. wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
  464. if (disabled) {
  465. if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
  466. ret = -1;
  467. } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
  468. ret = -1;
  469. wpa_tdls_enable(wpa_s->wpa, !disabled);
  470. #endif /* CONFIG_TDLS */
  471. } else if (os_strcasecmp(cmd, "pno") == 0) {
  472. ret = wpas_ctrl_pno(wpa_s, value);
  473. } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
  474. int disabled = atoi(value);
  475. if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
  476. ret = -1;
  477. else if (disabled)
  478. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  479. } else if (os_strcasecmp(cmd, "uapsd") == 0) {
  480. if (os_strcmp(value, "disable") == 0)
  481. wpa_s->set_sta_uapsd = 0;
  482. else {
  483. int be, bk, vi, vo;
  484. char *pos;
  485. /* format: BE,BK,VI,VO;max SP Length */
  486. be = atoi(value);
  487. pos = os_strchr(value, ',');
  488. if (pos == NULL)
  489. return -1;
  490. pos++;
  491. bk = atoi(pos);
  492. pos = os_strchr(pos, ',');
  493. if (pos == NULL)
  494. return -1;
  495. pos++;
  496. vi = atoi(pos);
  497. pos = os_strchr(pos, ',');
  498. if (pos == NULL)
  499. return -1;
  500. pos++;
  501. vo = atoi(pos);
  502. /* ignore max SP Length for now */
  503. wpa_s->set_sta_uapsd = 1;
  504. wpa_s->sta_uapsd = 0;
  505. if (be)
  506. wpa_s->sta_uapsd |= BIT(0);
  507. if (bk)
  508. wpa_s->sta_uapsd |= BIT(1);
  509. if (vi)
  510. wpa_s->sta_uapsd |= BIT(2);
  511. if (vo)
  512. wpa_s->sta_uapsd |= BIT(3);
  513. }
  514. } else if (os_strcasecmp(cmd, "ps") == 0) {
  515. ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
  516. #ifdef CONFIG_WIFI_DISPLAY
  517. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  518. int enabled = !!atoi(value);
  519. if (enabled && !wpa_s->global->p2p)
  520. ret = -1;
  521. else
  522. wifi_display_enable(wpa_s->global, enabled);
  523. #endif /* CONFIG_WIFI_DISPLAY */
  524. } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
  525. ret = set_bssid_filter(wpa_s, value);
  526. } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
  527. ret = set_disallow_aps(wpa_s, value);
  528. } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
  529. wpa_s->no_keep_alive = !!atoi(value);
  530. #ifdef CONFIG_DPP
  531. } else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
  532. os_free(wpa_s->dpp_configurator_params);
  533. wpa_s->dpp_configurator_params = os_strdup(value);
  534. } else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
  535. wpa_s->dpp_init_max_tries = atoi(value);
  536. } else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
  537. wpa_s->dpp_init_retry_time = atoi(value);
  538. } else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
  539. wpa_s->dpp_resp_wait_time = atoi(value);
  540. } else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
  541. wpa_s->dpp_resp_max_tries = atoi(value);
  542. } else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
  543. wpa_s->dpp_resp_retry_time = atoi(value);
  544. #ifdef CONFIG_TESTING_OPTIONS
  545. } else if (os_strcasecmp(cmd, "dpp_pkex_own_mac_override") == 0) {
  546. if (hwaddr_aton(value, dpp_pkex_own_mac_override))
  547. ret = -1;
  548. } else if (os_strcasecmp(cmd, "dpp_pkex_peer_mac_override") == 0) {
  549. if (hwaddr_aton(value, dpp_pkex_peer_mac_override))
  550. ret = -1;
  551. } else if (os_strcasecmp(cmd, "dpp_pkex_ephemeral_key_override") == 0) {
  552. size_t hex_len = os_strlen(value);
  553. if (hex_len >
  554. 2 * sizeof(dpp_pkex_ephemeral_key_override))
  555. ret = -1;
  556. else if (hexstr2bin(value, dpp_pkex_ephemeral_key_override,
  557. hex_len / 2))
  558. ret = -1;
  559. else
  560. dpp_pkex_ephemeral_key_override_len = hex_len / 2;
  561. #endif /* CONFIG_TESTING_OPTIONS */
  562. #endif /* CONFIG_DPP */
  563. #ifdef CONFIG_TESTING_OPTIONS
  564. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  565. wpa_s->ext_mgmt_frame_handling = !!atoi(value);
  566. } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
  567. wpa_s->ext_eapol_frame_io = !!atoi(value);
  568. #ifdef CONFIG_AP
  569. if (wpa_s->ap_iface) {
  570. wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
  571. wpa_s->ext_eapol_frame_io;
  572. }
  573. #endif /* CONFIG_AP */
  574. } else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
  575. wpa_s->extra_roc_dur = atoi(value);
  576. } else if (os_strcasecmp(cmd, "test_failure") == 0) {
  577. wpa_s->test_failure = atoi(value);
  578. } else if (os_strcasecmp(cmd, "p2p_go_csa_on_inv") == 0) {
  579. wpa_s->p2p_go_csa_on_inv = !!atoi(value);
  580. } else if (os_strcasecmp(cmd, "ignore_auth_resp") == 0) {
  581. wpa_s->ignore_auth_resp = !!atoi(value);
  582. } else if (os_strcasecmp(cmd, "ignore_assoc_disallow") == 0) {
  583. wpa_s->ignore_assoc_disallow = !!atoi(value);
  584. wpa_drv_ignore_assoc_disallow(wpa_s,
  585. wpa_s->ignore_assoc_disallow);
  586. } else if (os_strcasecmp(cmd, "reject_btm_req_reason") == 0) {
  587. wpa_s->reject_btm_req_reason = atoi(value);
  588. } else if (os_strcasecmp(cmd, "get_pref_freq_list_override") == 0) {
  589. os_free(wpa_s->get_pref_freq_list_override);
  590. if (!value[0])
  591. wpa_s->get_pref_freq_list_override = NULL;
  592. else
  593. wpa_s->get_pref_freq_list_override = os_strdup(value);
  594. } else if (os_strcasecmp(cmd, "sae_commit_override") == 0) {
  595. wpabuf_free(wpa_s->sae_commit_override);
  596. if (value[0] == '\0')
  597. wpa_s->sae_commit_override = NULL;
  598. else
  599. wpa_s->sae_commit_override = wpabuf_parse_bin(value);
  600. #ifdef CONFIG_DPP
  601. } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
  602. os_free(wpa_s->dpp_config_obj_override);
  603. if (value[0] == '\0')
  604. wpa_s->dpp_config_obj_override = NULL;
  605. else
  606. wpa_s->dpp_config_obj_override = os_strdup(value);
  607. } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
  608. os_free(wpa_s->dpp_discovery_override);
  609. if (value[0] == '\0')
  610. wpa_s->dpp_discovery_override = NULL;
  611. else
  612. wpa_s->dpp_discovery_override = os_strdup(value);
  613. } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
  614. os_free(wpa_s->dpp_groups_override);
  615. if (value[0] == '\0')
  616. wpa_s->dpp_groups_override = NULL;
  617. else
  618. wpa_s->dpp_groups_override = os_strdup(value);
  619. } else if (os_strcasecmp(cmd,
  620. "dpp_ignore_netaccesskey_mismatch") == 0) {
  621. wpa_s->dpp_ignore_netaccesskey_mismatch = atoi(value);
  622. } else if (os_strcasecmp(cmd, "dpp_test") == 0) {
  623. dpp_test = atoi(value);
  624. #endif /* CONFIG_DPP */
  625. #endif /* CONFIG_TESTING_OPTIONS */
  626. #ifndef CONFIG_NO_CONFIG_BLOBS
  627. } else if (os_strcmp(cmd, "blob") == 0) {
  628. ret = wpas_ctrl_set_blob(wpa_s, value);
  629. #endif /* CONFIG_NO_CONFIG_BLOBS */
  630. } else if (os_strcasecmp(cmd, "setband") == 0) {
  631. ret = wpas_ctrl_set_band(wpa_s, value);
  632. #ifdef CONFIG_MBO
  633. } else if (os_strcasecmp(cmd, "non_pref_chan") == 0) {
  634. ret = wpas_mbo_update_non_pref_chan(wpa_s, value);
  635. if (ret == 0) {
  636. value[-1] = '=';
  637. wpa_config_process_global(wpa_s->conf, cmd, -1);
  638. }
  639. } else if (os_strcasecmp(cmd, "mbo_cell_capa") == 0) {
  640. wpas_mbo_update_cell_capa(wpa_s, atoi(value));
  641. } else if (os_strcasecmp(cmd, "oce") == 0) {
  642. wpa_s->conf->oce = atoi(value);
  643. if (wpa_s->conf->oce) {
  644. if ((wpa_s->conf->oce & OCE_STA) &&
  645. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA))
  646. wpa_s->enable_oce = OCE_STA;
  647. if ((wpa_s->conf->oce & OCE_STA_CFON) &&
  648. (wpa_s->drv_flags &
  649. WPA_DRIVER_FLAGS_OCE_STA_CFON)) {
  650. /* TODO: Need to add STA-CFON support */
  651. wpa_printf(MSG_ERROR,
  652. "OCE STA-CFON feature is not yet supported");
  653. return -1;
  654. }
  655. } else {
  656. wpa_s->enable_oce = 0;
  657. }
  658. wpa_supplicant_set_default_scan_ies(wpa_s);
  659. #endif /* CONFIG_MBO */
  660. } else if (os_strcasecmp(cmd, "lci") == 0) {
  661. ret = wpas_ctrl_iface_set_lci(wpa_s, value);
  662. } else if (os_strcasecmp(cmd, "tdls_trigger_control") == 0) {
  663. ret = wpa_drv_set_tdls_mode(wpa_s, atoi(value));
  664. } else if (os_strcasecmp(cmd, "relative_rssi") == 0) {
  665. ret = wpas_ctrl_set_relative_rssi(wpa_s, value);
  666. } else if (os_strcasecmp(cmd, "relative_band_adjust") == 0) {
  667. ret = wpas_ctrl_set_relative_band_adjust(wpa_s, value);
  668. } else if (os_strcasecmp(cmd, "ric_ies") == 0) {
  669. ret = wpas_ctrl_iface_set_ric_ies(wpa_s, value);
  670. } else if (os_strcasecmp(cmd, "roaming") == 0) {
  671. ret = wpa_drv_roaming(wpa_s, atoi(value), NULL);
  672. } else {
  673. value[-1] = '=';
  674. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  675. if (ret == 0)
  676. wpa_supplicant_update_config(wpa_s);
  677. }
  678. return ret;
  679. }
  680. static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
  681. char *cmd, char *buf, size_t buflen)
  682. {
  683. int res = -1;
  684. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  685. if (os_strcmp(cmd, "version") == 0) {
  686. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  687. } else if (os_strcasecmp(cmd, "country") == 0) {
  688. if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
  689. res = os_snprintf(buf, buflen, "%c%c",
  690. wpa_s->conf->country[0],
  691. wpa_s->conf->country[1]);
  692. #ifdef CONFIG_WIFI_DISPLAY
  693. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  694. int enabled;
  695. if (wpa_s->global->p2p == NULL ||
  696. wpa_s->global->p2p_disabled)
  697. enabled = 0;
  698. else
  699. enabled = wpa_s->global->wifi_display;
  700. res = os_snprintf(buf, buflen, "%d", enabled);
  701. #endif /* CONFIG_WIFI_DISPLAY */
  702. #ifdef CONFIG_TESTING_GET_GTK
  703. } else if (os_strcmp(cmd, "gtk") == 0) {
  704. if (wpa_s->last_gtk_len == 0)
  705. return -1;
  706. res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
  707. wpa_s->last_gtk_len);
  708. return res;
  709. #endif /* CONFIG_TESTING_GET_GTK */
  710. } else if (os_strcmp(cmd, "tls_library") == 0) {
  711. res = tls_get_library_version(buf, buflen);
  712. #ifdef CONFIG_TESTING_OPTIONS
  713. } else if (os_strcmp(cmd, "anonce") == 0) {
  714. return wpa_snprintf_hex(buf, buflen,
  715. wpa_sm_get_anonce(wpa_s->wpa),
  716. WPA_NONCE_LEN);
  717. #endif /* CONFIG_TESTING_OPTIONS */
  718. } else {
  719. res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
  720. }
  721. if (os_snprintf_error(buflen, res))
  722. return -1;
  723. return res;
  724. }
  725. #ifdef IEEE8021X_EAPOL
  726. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  727. char *addr)
  728. {
  729. u8 bssid[ETH_ALEN];
  730. struct wpa_ssid *ssid = wpa_s->current_ssid;
  731. if (hwaddr_aton(addr, bssid)) {
  732. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  733. "'%s'", addr);
  734. return -1;
  735. }
  736. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  737. rsn_preauth_deinit(wpa_s->wpa);
  738. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  739. return -1;
  740. return 0;
  741. }
  742. #endif /* IEEE8021X_EAPOL */
  743. #ifdef CONFIG_TDLS
  744. static int wpa_supplicant_ctrl_iface_tdls_discover(
  745. struct wpa_supplicant *wpa_s, char *addr)
  746. {
  747. u8 peer[ETH_ALEN];
  748. int ret;
  749. if (hwaddr_aton(addr, peer)) {
  750. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
  751. "address '%s'", addr);
  752. return -1;
  753. }
  754. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
  755. MAC2STR(peer));
  756. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  757. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  758. else
  759. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  760. return ret;
  761. }
  762. static int wpa_supplicant_ctrl_iface_tdls_setup(
  763. struct wpa_supplicant *wpa_s, char *addr)
  764. {
  765. u8 peer[ETH_ALEN];
  766. int ret;
  767. if (hwaddr_aton(addr, peer)) {
  768. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
  769. "address '%s'", addr);
  770. return -1;
  771. }
  772. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
  773. MAC2STR(peer));
  774. if ((wpa_s->conf->tdls_external_control) &&
  775. wpa_tdls_is_external_setup(wpa_s->wpa))
  776. return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  777. wpa_tdls_remove(wpa_s->wpa, peer);
  778. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  779. ret = wpa_tdls_start(wpa_s->wpa, peer);
  780. else
  781. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  782. return ret;
  783. }
  784. static int wpa_supplicant_ctrl_iface_tdls_teardown(
  785. struct wpa_supplicant *wpa_s, char *addr)
  786. {
  787. u8 peer[ETH_ALEN];
  788. int ret;
  789. if (os_strcmp(addr, "*") == 0) {
  790. /* remove everyone */
  791. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
  792. wpa_tdls_teardown_peers(wpa_s->wpa);
  793. return 0;
  794. }
  795. if (hwaddr_aton(addr, peer)) {
  796. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
  797. "address '%s'", addr);
  798. return -1;
  799. }
  800. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
  801. MAC2STR(peer));
  802. if ((wpa_s->conf->tdls_external_control) &&
  803. wpa_tdls_is_external_setup(wpa_s->wpa))
  804. return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  805. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  806. ret = wpa_tdls_teardown_link(
  807. wpa_s->wpa, peer,
  808. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  809. else
  810. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  811. return ret;
  812. }
  813. static int ctrl_iface_get_capability_tdls(
  814. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  815. {
  816. int ret;
  817. ret = os_snprintf(buf, buflen, "%s\n",
  818. wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
  819. (wpa_s->drv_flags &
  820. WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
  821. "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
  822. if (os_snprintf_error(buflen, ret))
  823. return -1;
  824. return ret;
  825. }
  826. static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
  827. struct wpa_supplicant *wpa_s, char *cmd)
  828. {
  829. u8 peer[ETH_ALEN];
  830. struct hostapd_freq_params freq_params;
  831. u8 oper_class;
  832. char *pos, *end;
  833. if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
  834. wpa_printf(MSG_INFO,
  835. "tdls_chanswitch: Only supported with external setup");
  836. return -1;
  837. }
  838. os_memset(&freq_params, 0, sizeof(freq_params));
  839. pos = os_strchr(cmd, ' ');
  840. if (pos == NULL)
  841. return -1;
  842. *pos++ = '\0';
  843. oper_class = strtol(pos, &end, 10);
  844. if (pos == end) {
  845. wpa_printf(MSG_INFO,
  846. "tdls_chanswitch: Invalid op class provided");
  847. return -1;
  848. }
  849. pos = end;
  850. freq_params.freq = atoi(pos);
  851. if (freq_params.freq == 0) {
  852. wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
  853. return -1;
  854. }
  855. #define SET_FREQ_SETTING(str) \
  856. do { \
  857. const char *pos2 = os_strstr(pos, " " #str "="); \
  858. if (pos2) { \
  859. pos2 += sizeof(" " #str "=") - 1; \
  860. freq_params.str = atoi(pos2); \
  861. } \
  862. } while (0)
  863. SET_FREQ_SETTING(center_freq1);
  864. SET_FREQ_SETTING(center_freq2);
  865. SET_FREQ_SETTING(bandwidth);
  866. SET_FREQ_SETTING(sec_channel_offset);
  867. #undef SET_FREQ_SETTING
  868. freq_params.ht_enabled = !!os_strstr(pos, " ht");
  869. freq_params.vht_enabled = !!os_strstr(pos, " vht");
  870. if (hwaddr_aton(cmd, peer)) {
  871. wpa_printf(MSG_DEBUG,
  872. "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
  873. cmd);
  874. return -1;
  875. }
  876. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
  877. " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
  878. MAC2STR(peer), oper_class, freq_params.freq,
  879. freq_params.center_freq1, freq_params.center_freq2,
  880. freq_params.bandwidth, freq_params.sec_channel_offset,
  881. freq_params.ht_enabled ? " HT" : "",
  882. freq_params.vht_enabled ? " VHT" : "");
  883. return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
  884. &freq_params);
  885. }
  886. static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
  887. struct wpa_supplicant *wpa_s, char *cmd)
  888. {
  889. u8 peer[ETH_ALEN];
  890. if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
  891. wpa_printf(MSG_INFO,
  892. "tdls_chanswitch: Only supported with external setup");
  893. return -1;
  894. }
  895. if (hwaddr_aton(cmd, peer)) {
  896. wpa_printf(MSG_DEBUG,
  897. "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
  898. cmd);
  899. return -1;
  900. }
  901. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
  902. MAC2STR(peer));
  903. return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
  904. }
  905. static int wpa_supplicant_ctrl_iface_tdls_link_status(
  906. struct wpa_supplicant *wpa_s, const char *addr,
  907. char *buf, size_t buflen)
  908. {
  909. u8 peer[ETH_ALEN];
  910. const char *tdls_status;
  911. int ret;
  912. if (hwaddr_aton(addr, peer)) {
  913. wpa_printf(MSG_DEBUG,
  914. "CTRL_IFACE TDLS_LINK_STATUS: Invalid address '%s'",
  915. addr);
  916. return -1;
  917. }
  918. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS " MACSTR,
  919. MAC2STR(peer));
  920. tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
  921. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS: %s", tdls_status);
  922. ret = os_snprintf(buf, buflen, "TDLS link status: %s\n", tdls_status);
  923. if (os_snprintf_error(buflen, ret))
  924. return -1;
  925. return ret;
  926. }
  927. #endif /* CONFIG_TDLS */
  928. static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
  929. {
  930. char *token, *context = NULL;
  931. struct wmm_ac_ts_setup_params params = {
  932. .tsid = 0xff,
  933. .direction = 0xff,
  934. };
  935. while ((token = str_token(cmd, " ", &context))) {
  936. if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
  937. sscanf(token, "up=%i", &params.user_priority) == 1 ||
  938. sscanf(token, "nominal_msdu_size=%i",
  939. &params.nominal_msdu_size) == 1 ||
  940. sscanf(token, "mean_data_rate=%i",
  941. &params.mean_data_rate) == 1 ||
  942. sscanf(token, "min_phy_rate=%i",
  943. &params.minimum_phy_rate) == 1 ||
  944. sscanf(token, "sba=%i",
  945. &params.surplus_bandwidth_allowance) == 1)
  946. continue;
  947. if (os_strcasecmp(token, "downlink") == 0) {
  948. params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
  949. } else if (os_strcasecmp(token, "uplink") == 0) {
  950. params.direction = WMM_TSPEC_DIRECTION_UPLINK;
  951. } else if (os_strcasecmp(token, "bidi") == 0) {
  952. params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
  953. } else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
  954. params.fixed_nominal_msdu = 1;
  955. } else {
  956. wpa_printf(MSG_DEBUG,
  957. "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
  958. token);
  959. return -1;
  960. }
  961. }
  962. return wpas_wmm_ac_addts(wpa_s, &params);
  963. }
  964. static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
  965. {
  966. u8 tsid = atoi(cmd);
  967. return wpas_wmm_ac_delts(wpa_s, tsid);
  968. }
  969. #ifdef CONFIG_IEEE80211R
  970. static int wpa_supplicant_ctrl_iface_ft_ds(
  971. struct wpa_supplicant *wpa_s, char *addr)
  972. {
  973. u8 target_ap[ETH_ALEN];
  974. struct wpa_bss *bss;
  975. const u8 *mdie;
  976. if (hwaddr_aton(addr, target_ap)) {
  977. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  978. "address '%s'", addr);
  979. return -1;
  980. }
  981. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  982. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  983. if (bss)
  984. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  985. else
  986. mdie = NULL;
  987. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  988. }
  989. #endif /* CONFIG_IEEE80211R */
  990. #ifdef CONFIG_WPS
  991. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  992. char *cmd)
  993. {
  994. u8 bssid[ETH_ALEN], *_bssid = bssid;
  995. #ifdef CONFIG_P2P
  996. u8 p2p_dev_addr[ETH_ALEN];
  997. #endif /* CONFIG_P2P */
  998. #ifdef CONFIG_AP
  999. u8 *_p2p_dev_addr = NULL;
  1000. #endif /* CONFIG_AP */
  1001. if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
  1002. _bssid = NULL;
  1003. #ifdef CONFIG_P2P
  1004. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  1005. if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
  1006. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
  1007. "P2P Device Address '%s'",
  1008. cmd + 13);
  1009. return -1;
  1010. }
  1011. _p2p_dev_addr = p2p_dev_addr;
  1012. #endif /* CONFIG_P2P */
  1013. } else if (hwaddr_aton(cmd, bssid)) {
  1014. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  1015. cmd);
  1016. return -1;
  1017. }
  1018. #ifdef CONFIG_AP
  1019. if (wpa_s->ap_iface)
  1020. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
  1021. #endif /* CONFIG_AP */
  1022. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  1023. }
  1024. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  1025. char *cmd, char *buf,
  1026. size_t buflen)
  1027. {
  1028. u8 bssid[ETH_ALEN], *_bssid = bssid;
  1029. char *pin;
  1030. int ret;
  1031. pin = os_strchr(cmd, ' ');
  1032. if (pin)
  1033. *pin++ = '\0';
  1034. if (os_strcmp(cmd, "any") == 0)
  1035. _bssid = NULL;
  1036. else if (os_strcmp(cmd, "get") == 0) {
  1037. if (wps_generate_pin((unsigned int *) &ret) < 0)
  1038. return -1;
  1039. goto done;
  1040. } else if (hwaddr_aton(cmd, bssid)) {
  1041. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  1042. cmd);
  1043. return -1;
  1044. }
  1045. #ifdef CONFIG_AP
  1046. if (wpa_s->ap_iface) {
  1047. int timeout = 0;
  1048. char *pos;
  1049. if (pin) {
  1050. pos = os_strchr(pin, ' ');
  1051. if (pos) {
  1052. *pos++ = '\0';
  1053. timeout = atoi(pos);
  1054. }
  1055. }
  1056. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  1057. buf, buflen, timeout);
  1058. }
  1059. #endif /* CONFIG_AP */
  1060. if (pin) {
  1061. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  1062. DEV_PW_DEFAULT);
  1063. if (ret < 0)
  1064. return -1;
  1065. ret = os_snprintf(buf, buflen, "%s", pin);
  1066. if (os_snprintf_error(buflen, ret))
  1067. return -1;
  1068. return ret;
  1069. }
  1070. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  1071. if (ret < 0)
  1072. return -1;
  1073. done:
  1074. /* Return the generated PIN */
  1075. ret = os_snprintf(buf, buflen, "%08d", ret);
  1076. if (os_snprintf_error(buflen, ret))
  1077. return -1;
  1078. return ret;
  1079. }
  1080. static int wpa_supplicant_ctrl_iface_wps_check_pin(
  1081. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1082. {
  1083. char pin[9];
  1084. size_t len;
  1085. char *pos;
  1086. int ret;
  1087. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  1088. (u8 *) cmd, os_strlen(cmd));
  1089. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  1090. if (*pos < '0' || *pos > '9')
  1091. continue;
  1092. pin[len++] = *pos;
  1093. if (len == 9) {
  1094. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  1095. return -1;
  1096. }
  1097. }
  1098. if (len != 4 && len != 8) {
  1099. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  1100. return -1;
  1101. }
  1102. pin[len] = '\0';
  1103. if (len == 8) {
  1104. unsigned int pin_val;
  1105. pin_val = atoi(pin);
  1106. if (!wps_pin_valid(pin_val)) {
  1107. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  1108. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  1109. if (os_snprintf_error(buflen, ret))
  1110. return -1;
  1111. return ret;
  1112. }
  1113. }
  1114. ret = os_snprintf(buf, buflen, "%s", pin);
  1115. if (os_snprintf_error(buflen, ret))
  1116. return -1;
  1117. return ret;
  1118. }
  1119. #ifdef CONFIG_WPS_NFC
  1120. static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
  1121. char *cmd)
  1122. {
  1123. u8 bssid[ETH_ALEN], *_bssid = bssid;
  1124. if (cmd == NULL || cmd[0] == '\0')
  1125. _bssid = NULL;
  1126. else if (hwaddr_aton(cmd, bssid))
  1127. return -1;
  1128. return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
  1129. 0, 0);
  1130. }
  1131. static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  1132. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1133. {
  1134. int ndef;
  1135. struct wpabuf *buf;
  1136. int res;
  1137. char *pos;
  1138. pos = os_strchr(cmd, ' ');
  1139. if (pos)
  1140. *pos++ = '\0';
  1141. if (os_strcmp(cmd, "WPS") == 0)
  1142. ndef = 0;
  1143. else if (os_strcmp(cmd, "NDEF") == 0)
  1144. ndef = 1;
  1145. else
  1146. return -1;
  1147. buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
  1148. if (buf == NULL)
  1149. return -1;
  1150. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1151. wpabuf_len(buf));
  1152. reply[res++] = '\n';
  1153. reply[res] = '\0';
  1154. wpabuf_free(buf);
  1155. return res;
  1156. }
  1157. static int wpa_supplicant_ctrl_iface_wps_nfc_token(
  1158. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1159. {
  1160. int ndef;
  1161. struct wpabuf *buf;
  1162. int res;
  1163. if (os_strcmp(cmd, "WPS") == 0)
  1164. ndef = 0;
  1165. else if (os_strcmp(cmd, "NDEF") == 0)
  1166. ndef = 1;
  1167. else
  1168. return -1;
  1169. buf = wpas_wps_nfc_token(wpa_s, ndef);
  1170. if (buf == NULL)
  1171. return -1;
  1172. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1173. wpabuf_len(buf));
  1174. reply[res++] = '\n';
  1175. reply[res] = '\0';
  1176. wpabuf_free(buf);
  1177. return res;
  1178. }
  1179. static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
  1180. struct wpa_supplicant *wpa_s, char *pos)
  1181. {
  1182. size_t len;
  1183. struct wpabuf *buf;
  1184. int ret;
  1185. char *freq;
  1186. int forced_freq = 0;
  1187. freq = strstr(pos, " freq=");
  1188. if (freq) {
  1189. *freq = '\0';
  1190. freq += 6;
  1191. forced_freq = atoi(freq);
  1192. }
  1193. len = os_strlen(pos);
  1194. if (len & 0x01)
  1195. return -1;
  1196. len /= 2;
  1197. buf = wpabuf_alloc(len);
  1198. if (buf == NULL)
  1199. return -1;
  1200. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  1201. wpabuf_free(buf);
  1202. return -1;
  1203. }
  1204. ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
  1205. wpabuf_free(buf);
  1206. return ret;
  1207. }
  1208. static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
  1209. char *reply, size_t max_len,
  1210. int ndef)
  1211. {
  1212. struct wpabuf *buf;
  1213. int res;
  1214. buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
  1215. if (buf == NULL)
  1216. return -1;
  1217. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1218. wpabuf_len(buf));
  1219. reply[res++] = '\n';
  1220. reply[res] = '\0';
  1221. wpabuf_free(buf);
  1222. return res;
  1223. }
  1224. #ifdef CONFIG_P2P
  1225. static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
  1226. char *reply, size_t max_len,
  1227. int ndef)
  1228. {
  1229. struct wpabuf *buf;
  1230. int res;
  1231. buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
  1232. if (buf == NULL) {
  1233. wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
  1234. return -1;
  1235. }
  1236. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1237. wpabuf_len(buf));
  1238. reply[res++] = '\n';
  1239. reply[res] = '\0';
  1240. wpabuf_free(buf);
  1241. return res;
  1242. }
  1243. #endif /* CONFIG_P2P */
  1244. static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
  1245. char *cmd, char *reply,
  1246. size_t max_len)
  1247. {
  1248. char *pos;
  1249. int ndef;
  1250. pos = os_strchr(cmd, ' ');
  1251. if (pos == NULL)
  1252. return -1;
  1253. *pos++ = '\0';
  1254. if (os_strcmp(cmd, "WPS") == 0)
  1255. ndef = 0;
  1256. else if (os_strcmp(cmd, "NDEF") == 0)
  1257. ndef = 1;
  1258. else
  1259. return -1;
  1260. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  1261. if (!ndef)
  1262. return -1;
  1263. return wpas_ctrl_nfc_get_handover_req_wps(
  1264. wpa_s, reply, max_len, ndef);
  1265. }
  1266. #ifdef CONFIG_P2P
  1267. if (os_strcmp(pos, "P2P-CR") == 0) {
  1268. return wpas_ctrl_nfc_get_handover_req_p2p(
  1269. wpa_s, reply, max_len, ndef);
  1270. }
  1271. #endif /* CONFIG_P2P */
  1272. return -1;
  1273. }
  1274. static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
  1275. char *reply, size_t max_len,
  1276. int ndef, int cr, char *uuid)
  1277. {
  1278. struct wpabuf *buf;
  1279. int res;
  1280. buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
  1281. if (buf == NULL)
  1282. return -1;
  1283. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1284. wpabuf_len(buf));
  1285. reply[res++] = '\n';
  1286. reply[res] = '\0';
  1287. wpabuf_free(buf);
  1288. return res;
  1289. }
  1290. #ifdef CONFIG_P2P
  1291. static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
  1292. char *reply, size_t max_len,
  1293. int ndef, int tag)
  1294. {
  1295. struct wpabuf *buf;
  1296. int res;
  1297. buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
  1298. if (buf == NULL)
  1299. return -1;
  1300. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1301. wpabuf_len(buf));
  1302. reply[res++] = '\n';
  1303. reply[res] = '\0';
  1304. wpabuf_free(buf);
  1305. return res;
  1306. }
  1307. #endif /* CONFIG_P2P */
  1308. static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
  1309. char *cmd, char *reply,
  1310. size_t max_len)
  1311. {
  1312. char *pos, *pos2;
  1313. int ndef;
  1314. pos = os_strchr(cmd, ' ');
  1315. if (pos == NULL)
  1316. return -1;
  1317. *pos++ = '\0';
  1318. if (os_strcmp(cmd, "WPS") == 0)
  1319. ndef = 0;
  1320. else if (os_strcmp(cmd, "NDEF") == 0)
  1321. ndef = 1;
  1322. else
  1323. return -1;
  1324. pos2 = os_strchr(pos, ' ');
  1325. if (pos2)
  1326. *pos2++ = '\0';
  1327. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  1328. if (!ndef)
  1329. return -1;
  1330. return wpas_ctrl_nfc_get_handover_sel_wps(
  1331. wpa_s, reply, max_len, ndef,
  1332. os_strcmp(pos, "WPS-CR") == 0, pos2);
  1333. }
  1334. #ifdef CONFIG_P2P
  1335. if (os_strcmp(pos, "P2P-CR") == 0) {
  1336. return wpas_ctrl_nfc_get_handover_sel_p2p(
  1337. wpa_s, reply, max_len, ndef, 0);
  1338. }
  1339. if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
  1340. return wpas_ctrl_nfc_get_handover_sel_p2p(
  1341. wpa_s, reply, max_len, ndef, 1);
  1342. }
  1343. #endif /* CONFIG_P2P */
  1344. return -1;
  1345. }
  1346. static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
  1347. char *cmd)
  1348. {
  1349. size_t len;
  1350. struct wpabuf *req, *sel;
  1351. int ret;
  1352. char *pos, *role, *type, *pos2;
  1353. #ifdef CONFIG_P2P
  1354. char *freq;
  1355. int forced_freq = 0;
  1356. freq = strstr(cmd, " freq=");
  1357. if (freq) {
  1358. *freq = '\0';
  1359. freq += 6;
  1360. forced_freq = atoi(freq);
  1361. }
  1362. #endif /* CONFIG_P2P */
  1363. role = cmd;
  1364. pos = os_strchr(role, ' ');
  1365. if (pos == NULL) {
  1366. wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
  1367. return -1;
  1368. }
  1369. *pos++ = '\0';
  1370. type = pos;
  1371. pos = os_strchr(type, ' ');
  1372. if (pos == NULL) {
  1373. wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
  1374. return -1;
  1375. }
  1376. *pos++ = '\0';
  1377. pos2 = os_strchr(pos, ' ');
  1378. if (pos2 == NULL) {
  1379. wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
  1380. return -1;
  1381. }
  1382. *pos2++ = '\0';
  1383. len = os_strlen(pos);
  1384. if (len & 0x01) {
  1385. wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
  1386. return -1;
  1387. }
  1388. len /= 2;
  1389. req = wpabuf_alloc(len);
  1390. if (req == NULL) {
  1391. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
  1392. return -1;
  1393. }
  1394. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  1395. wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
  1396. wpabuf_free(req);
  1397. return -1;
  1398. }
  1399. len = os_strlen(pos2);
  1400. if (len & 0x01) {
  1401. wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
  1402. wpabuf_free(req);
  1403. return -1;
  1404. }
  1405. len /= 2;
  1406. sel = wpabuf_alloc(len);
  1407. if (sel == NULL) {
  1408. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
  1409. wpabuf_free(req);
  1410. return -1;
  1411. }
  1412. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  1413. wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
  1414. wpabuf_free(req);
  1415. wpabuf_free(sel);
  1416. return -1;
  1417. }
  1418. wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
  1419. role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
  1420. if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
  1421. ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
  1422. #ifdef CONFIG_AP
  1423. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
  1424. {
  1425. ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
  1426. if (ret < 0)
  1427. ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
  1428. #endif /* CONFIG_AP */
  1429. #ifdef CONFIG_P2P
  1430. } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
  1431. {
  1432. ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
  1433. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
  1434. {
  1435. ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
  1436. forced_freq);
  1437. #endif /* CONFIG_P2P */
  1438. } else {
  1439. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  1440. "reported: role=%s type=%s", role, type);
  1441. ret = -1;
  1442. }
  1443. wpabuf_free(req);
  1444. wpabuf_free(sel);
  1445. if (ret)
  1446. wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
  1447. return ret;
  1448. }
  1449. #endif /* CONFIG_WPS_NFC */
  1450. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  1451. char *cmd)
  1452. {
  1453. u8 bssid[ETH_ALEN];
  1454. char *pin;
  1455. char *new_ssid;
  1456. char *new_auth;
  1457. char *new_encr;
  1458. char *new_key;
  1459. struct wps_new_ap_settings ap;
  1460. pin = os_strchr(cmd, ' ');
  1461. if (pin == NULL)
  1462. return -1;
  1463. *pin++ = '\0';
  1464. if (hwaddr_aton(cmd, bssid)) {
  1465. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  1466. cmd);
  1467. return -1;
  1468. }
  1469. new_ssid = os_strchr(pin, ' ');
  1470. if (new_ssid == NULL)
  1471. return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  1472. *new_ssid++ = '\0';
  1473. new_auth = os_strchr(new_ssid, ' ');
  1474. if (new_auth == NULL)
  1475. return -1;
  1476. *new_auth++ = '\0';
  1477. new_encr = os_strchr(new_auth, ' ');
  1478. if (new_encr == NULL)
  1479. return -1;
  1480. *new_encr++ = '\0';
  1481. new_key = os_strchr(new_encr, ' ');
  1482. if (new_key == NULL)
  1483. return -1;
  1484. *new_key++ = '\0';
  1485. os_memset(&ap, 0, sizeof(ap));
  1486. ap.ssid_hex = new_ssid;
  1487. ap.auth = new_auth;
  1488. ap.encr = new_encr;
  1489. ap.key_hex = new_key;
  1490. return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
  1491. }
  1492. #ifdef CONFIG_AP
  1493. static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
  1494. char *cmd, char *buf,
  1495. size_t buflen)
  1496. {
  1497. int timeout = 300;
  1498. char *pos;
  1499. const char *pin_txt;
  1500. if (!wpa_s->ap_iface)
  1501. return -1;
  1502. pos = os_strchr(cmd, ' ');
  1503. if (pos)
  1504. *pos++ = '\0';
  1505. if (os_strcmp(cmd, "disable") == 0) {
  1506. wpas_wps_ap_pin_disable(wpa_s);
  1507. return os_snprintf(buf, buflen, "OK\n");
  1508. }
  1509. if (os_strcmp(cmd, "random") == 0) {
  1510. if (pos)
  1511. timeout = atoi(pos);
  1512. pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
  1513. if (pin_txt == NULL)
  1514. return -1;
  1515. return os_snprintf(buf, buflen, "%s", pin_txt);
  1516. }
  1517. if (os_strcmp(cmd, "get") == 0) {
  1518. pin_txt = wpas_wps_ap_pin_get(wpa_s);
  1519. if (pin_txt == NULL)
  1520. return -1;
  1521. return os_snprintf(buf, buflen, "%s", pin_txt);
  1522. }
  1523. if (os_strcmp(cmd, "set") == 0) {
  1524. char *pin;
  1525. if (pos == NULL)
  1526. return -1;
  1527. pin = pos;
  1528. pos = os_strchr(pos, ' ');
  1529. if (pos) {
  1530. *pos++ = '\0';
  1531. timeout = atoi(pos);
  1532. }
  1533. if (os_strlen(pin) > buflen)
  1534. return -1;
  1535. if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
  1536. return -1;
  1537. return os_snprintf(buf, buflen, "%s", pin);
  1538. }
  1539. return -1;
  1540. }
  1541. #endif /* CONFIG_AP */
  1542. #ifdef CONFIG_WPS_ER
  1543. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  1544. char *cmd)
  1545. {
  1546. char *uuid = cmd, *pin, *pos;
  1547. u8 addr_buf[ETH_ALEN], *addr = NULL;
  1548. pin = os_strchr(uuid, ' ');
  1549. if (pin == NULL)
  1550. return -1;
  1551. *pin++ = '\0';
  1552. pos = os_strchr(pin, ' ');
  1553. if (pos) {
  1554. *pos++ = '\0';
  1555. if (hwaddr_aton(pos, addr_buf) == 0)
  1556. addr = addr_buf;
  1557. }
  1558. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  1559. }
  1560. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  1561. char *cmd)
  1562. {
  1563. char *uuid = cmd, *pin;
  1564. pin = os_strchr(uuid, ' ');
  1565. if (pin == NULL)
  1566. return -1;
  1567. *pin++ = '\0';
  1568. return wpas_wps_er_learn(wpa_s, uuid, pin);
  1569. }
  1570. static int wpa_supplicant_ctrl_iface_wps_er_set_config(
  1571. struct wpa_supplicant *wpa_s, char *cmd)
  1572. {
  1573. char *uuid = cmd, *id;
  1574. id = os_strchr(uuid, ' ');
  1575. if (id == NULL)
  1576. return -1;
  1577. *id++ = '\0';
  1578. return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
  1579. }
  1580. static int wpa_supplicant_ctrl_iface_wps_er_config(
  1581. struct wpa_supplicant *wpa_s, char *cmd)
  1582. {
  1583. char *pin;
  1584. char *new_ssid;
  1585. char *new_auth;
  1586. char *new_encr;
  1587. char *new_key;
  1588. struct wps_new_ap_settings ap;
  1589. pin = os_strchr(cmd, ' ');
  1590. if (pin == NULL)
  1591. return -1;
  1592. *pin++ = '\0';
  1593. new_ssid = os_strchr(pin, ' ');
  1594. if (new_ssid == NULL)
  1595. return -1;
  1596. *new_ssid++ = '\0';
  1597. new_auth = os_strchr(new_ssid, ' ');
  1598. if (new_auth == NULL)
  1599. return -1;
  1600. *new_auth++ = '\0';
  1601. new_encr = os_strchr(new_auth, ' ');
  1602. if (new_encr == NULL)
  1603. return -1;
  1604. *new_encr++ = '\0';
  1605. new_key = os_strchr(new_encr, ' ');
  1606. if (new_key == NULL)
  1607. return -1;
  1608. *new_key++ = '\0';
  1609. os_memset(&ap, 0, sizeof(ap));
  1610. ap.ssid_hex = new_ssid;
  1611. ap.auth = new_auth;
  1612. ap.encr = new_encr;
  1613. ap.key_hex = new_key;
  1614. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  1615. }
  1616. #ifdef CONFIG_WPS_NFC
  1617. static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  1618. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1619. {
  1620. int ndef;
  1621. struct wpabuf *buf;
  1622. int res;
  1623. char *uuid;
  1624. uuid = os_strchr(cmd, ' ');
  1625. if (uuid == NULL)
  1626. return -1;
  1627. *uuid++ = '\0';
  1628. if (os_strcmp(cmd, "WPS") == 0)
  1629. ndef = 0;
  1630. else if (os_strcmp(cmd, "NDEF") == 0)
  1631. ndef = 1;
  1632. else
  1633. return -1;
  1634. buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
  1635. if (buf == NULL)
  1636. return -1;
  1637. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1638. wpabuf_len(buf));
  1639. reply[res++] = '\n';
  1640. reply[res] = '\0';
  1641. wpabuf_free(buf);
  1642. return res;
  1643. }
  1644. #endif /* CONFIG_WPS_NFC */
  1645. #endif /* CONFIG_WPS_ER */
  1646. #endif /* CONFIG_WPS */
  1647. #ifdef CONFIG_IBSS_RSN
  1648. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  1649. struct wpa_supplicant *wpa_s, char *addr)
  1650. {
  1651. u8 peer[ETH_ALEN];
  1652. if (hwaddr_aton(addr, peer)) {
  1653. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  1654. "address '%s'", addr);
  1655. return -1;
  1656. }
  1657. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  1658. MAC2STR(peer));
  1659. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  1660. }
  1661. #endif /* CONFIG_IBSS_RSN */
  1662. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  1663. char *rsp)
  1664. {
  1665. #ifdef IEEE8021X_EAPOL
  1666. char *pos, *id_pos;
  1667. int id;
  1668. struct wpa_ssid *ssid;
  1669. pos = os_strchr(rsp, '-');
  1670. if (pos == NULL)
  1671. return -1;
  1672. *pos++ = '\0';
  1673. id_pos = pos;
  1674. pos = os_strchr(pos, ':');
  1675. if (pos == NULL)
  1676. return -1;
  1677. *pos++ = '\0';
  1678. id = atoi(id_pos);
  1679. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  1680. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1681. (u8 *) pos, os_strlen(pos));
  1682. ssid = wpa_config_get_network(wpa_s->conf, id);
  1683. if (ssid == NULL) {
  1684. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1685. "to update", id);
  1686. return -1;
  1687. }
  1688. return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
  1689. pos);
  1690. #else /* IEEE8021X_EAPOL */
  1691. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1692. return -1;
  1693. #endif /* IEEE8021X_EAPOL */
  1694. }
  1695. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  1696. const char *params,
  1697. char *buf, size_t buflen)
  1698. {
  1699. char *pos, *end, tmp[30];
  1700. int res, verbose, wps, ret;
  1701. #ifdef CONFIG_HS20
  1702. const u8 *hs20;
  1703. #endif /* CONFIG_HS20 */
  1704. const u8 *sess_id;
  1705. size_t sess_id_len;
  1706. if (os_strcmp(params, "-DRIVER") == 0)
  1707. return wpa_drv_status(wpa_s, buf, buflen);
  1708. verbose = os_strcmp(params, "-VERBOSE") == 0;
  1709. wps = os_strcmp(params, "-WPS") == 0;
  1710. pos = buf;
  1711. end = buf + buflen;
  1712. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1713. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1714. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  1715. MAC2STR(wpa_s->bssid));
  1716. if (os_snprintf_error(end - pos, ret))
  1717. return pos - buf;
  1718. pos += ret;
  1719. ret = os_snprintf(pos, end - pos, "freq=%u\n",
  1720. wpa_s->assoc_freq);
  1721. if (os_snprintf_error(end - pos, ret))
  1722. return pos - buf;
  1723. pos += ret;
  1724. if (ssid) {
  1725. u8 *_ssid = ssid->ssid;
  1726. size_t ssid_len = ssid->ssid_len;
  1727. u8 ssid_buf[SSID_MAX_LEN];
  1728. if (ssid_len == 0) {
  1729. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  1730. if (_res < 0)
  1731. ssid_len = 0;
  1732. else
  1733. ssid_len = _res;
  1734. _ssid = ssid_buf;
  1735. }
  1736. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  1737. wpa_ssid_txt(_ssid, ssid_len),
  1738. ssid->id);
  1739. if (os_snprintf_error(end - pos, ret))
  1740. return pos - buf;
  1741. pos += ret;
  1742. if (wps && ssid->passphrase &&
  1743. wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
  1744. (ssid->mode == WPAS_MODE_AP ||
  1745. ssid->mode == WPAS_MODE_P2P_GO)) {
  1746. ret = os_snprintf(pos, end - pos,
  1747. "passphrase=%s\n",
  1748. ssid->passphrase);
  1749. if (os_snprintf_error(end - pos, ret))
  1750. return pos - buf;
  1751. pos += ret;
  1752. }
  1753. if (ssid->id_str) {
  1754. ret = os_snprintf(pos, end - pos,
  1755. "id_str=%s\n",
  1756. ssid->id_str);
  1757. if (os_snprintf_error(end - pos, ret))
  1758. return pos - buf;
  1759. pos += ret;
  1760. }
  1761. switch (ssid->mode) {
  1762. case WPAS_MODE_INFRA:
  1763. ret = os_snprintf(pos, end - pos,
  1764. "mode=station\n");
  1765. break;
  1766. case WPAS_MODE_IBSS:
  1767. ret = os_snprintf(pos, end - pos,
  1768. "mode=IBSS\n");
  1769. break;
  1770. case WPAS_MODE_AP:
  1771. ret = os_snprintf(pos, end - pos,
  1772. "mode=AP\n");
  1773. break;
  1774. case WPAS_MODE_P2P_GO:
  1775. ret = os_snprintf(pos, end - pos,
  1776. "mode=P2P GO\n");
  1777. break;
  1778. case WPAS_MODE_P2P_GROUP_FORMATION:
  1779. ret = os_snprintf(pos, end - pos,
  1780. "mode=P2P GO - group "
  1781. "formation\n");
  1782. break;
  1783. case WPAS_MODE_MESH:
  1784. ret = os_snprintf(pos, end - pos,
  1785. "mode=mesh\n");
  1786. break;
  1787. default:
  1788. ret = 0;
  1789. break;
  1790. }
  1791. if (os_snprintf_error(end - pos, ret))
  1792. return pos - buf;
  1793. pos += ret;
  1794. }
  1795. #ifdef CONFIG_AP
  1796. if (wpa_s->ap_iface) {
  1797. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  1798. end - pos,
  1799. verbose);
  1800. } else
  1801. #endif /* CONFIG_AP */
  1802. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1803. }
  1804. #ifdef CONFIG_SME
  1805. #ifdef CONFIG_SAE
  1806. if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
  1807. #ifdef CONFIG_AP
  1808. !wpa_s->ap_iface &&
  1809. #endif /* CONFIG_AP */
  1810. wpa_s->sme.sae.state == SAE_ACCEPTED) {
  1811. ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
  1812. wpa_s->sme.sae.group);
  1813. if (os_snprintf_error(end - pos, ret))
  1814. return pos - buf;
  1815. pos += ret;
  1816. }
  1817. #endif /* CONFIG_SAE */
  1818. #endif /* CONFIG_SME */
  1819. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  1820. wpa_supplicant_state_txt(wpa_s->wpa_state));
  1821. if (os_snprintf_error(end - pos, ret))
  1822. return pos - buf;
  1823. pos += ret;
  1824. if (wpa_s->l2 &&
  1825. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  1826. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  1827. if (os_snprintf_error(end - pos, ret))
  1828. return pos - buf;
  1829. pos += ret;
  1830. }
  1831. #ifdef CONFIG_P2P
  1832. if (wpa_s->global->p2p) {
  1833. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  1834. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  1835. if (os_snprintf_error(end - pos, ret))
  1836. return pos - buf;
  1837. pos += ret;
  1838. }
  1839. #endif /* CONFIG_P2P */
  1840. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  1841. MAC2STR(wpa_s->own_addr));
  1842. if (os_snprintf_error(end - pos, ret))
  1843. return pos - buf;
  1844. pos += ret;
  1845. #ifdef CONFIG_HS20
  1846. if (wpa_s->current_bss &&
  1847. (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
  1848. HS20_IE_VENDOR_TYPE)) &&
  1849. wpa_s->wpa_proto == WPA_PROTO_RSN &&
  1850. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1851. int release = 1;
  1852. if (hs20[1] >= 5) {
  1853. u8 rel_num = (hs20[6] & 0xf0) >> 4;
  1854. release = rel_num + 1;
  1855. }
  1856. ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
  1857. if (os_snprintf_error(end - pos, ret))
  1858. return pos - buf;
  1859. pos += ret;
  1860. }
  1861. if (wpa_s->current_ssid) {
  1862. struct wpa_cred *cred;
  1863. char *type;
  1864. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1865. size_t i;
  1866. if (wpa_s->current_ssid->parent_cred != cred)
  1867. continue;
  1868. if (cred->provisioning_sp) {
  1869. ret = os_snprintf(pos, end - pos,
  1870. "provisioning_sp=%s\n",
  1871. cred->provisioning_sp);
  1872. if (os_snprintf_error(end - pos, ret))
  1873. return pos - buf;
  1874. pos += ret;
  1875. }
  1876. if (!cred->domain)
  1877. goto no_domain;
  1878. i = 0;
  1879. if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
  1880. struct wpabuf *names =
  1881. wpa_s->current_bss->anqp->domain_name;
  1882. for (i = 0; names && i < cred->num_domain; i++)
  1883. {
  1884. if (domain_name_list_contains(
  1885. names, cred->domain[i], 1))
  1886. break;
  1887. }
  1888. if (i == cred->num_domain)
  1889. i = 0; /* show first entry by default */
  1890. }
  1891. ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
  1892. cred->domain[i]);
  1893. if (os_snprintf_error(end - pos, ret))
  1894. return pos - buf;
  1895. pos += ret;
  1896. no_domain:
  1897. if (wpa_s->current_bss == NULL ||
  1898. wpa_s->current_bss->anqp == NULL)
  1899. res = -1;
  1900. else
  1901. res = interworking_home_sp_cred(
  1902. wpa_s, cred,
  1903. wpa_s->current_bss->anqp->domain_name);
  1904. if (res > 0)
  1905. type = "home";
  1906. else if (res == 0)
  1907. type = "roaming";
  1908. else
  1909. type = "unknown";
  1910. ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
  1911. if (os_snprintf_error(end - pos, ret))
  1912. return pos - buf;
  1913. pos += ret;
  1914. break;
  1915. }
  1916. }
  1917. #endif /* CONFIG_HS20 */
  1918. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1919. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1920. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  1921. verbose);
  1922. if (res >= 0)
  1923. pos += res;
  1924. }
  1925. #ifdef CONFIG_MACSEC
  1926. res = ieee802_1x_kay_get_status(wpa_s->kay, pos, end - pos);
  1927. if (res > 0)
  1928. pos += res;
  1929. #endif /* CONFIG_MACSEC */
  1930. sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
  1931. if (sess_id) {
  1932. char *start = pos;
  1933. ret = os_snprintf(pos, end - pos, "eap_session_id=");
  1934. if (os_snprintf_error(end - pos, ret))
  1935. return start - buf;
  1936. pos += ret;
  1937. ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
  1938. if (ret <= 0)
  1939. return start - buf;
  1940. pos += ret;
  1941. ret = os_snprintf(pos, end - pos, "\n");
  1942. if (os_snprintf_error(end - pos, ret))
  1943. return start - buf;
  1944. pos += ret;
  1945. }
  1946. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1947. if (res >= 0)
  1948. pos += res;
  1949. #ifdef CONFIG_WPS
  1950. {
  1951. char uuid_str[100];
  1952. uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
  1953. ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
  1954. if (os_snprintf_error(end - pos, ret))
  1955. return pos - buf;
  1956. pos += ret;
  1957. }
  1958. #endif /* CONFIG_WPS */
  1959. #ifdef ANDROID
  1960. /*
  1961. * Allow using the STATUS command with default behavior, say for debug,
  1962. * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
  1963. * events with STATUS-NO_EVENTS.
  1964. */
  1965. if (os_strcmp(params, "-NO_EVENTS")) {
  1966. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
  1967. "id=%d state=%d BSSID=" MACSTR " SSID=%s",
  1968. wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
  1969. wpa_s->wpa_state,
  1970. MAC2STR(wpa_s->bssid),
  1971. wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
  1972. wpa_ssid_txt(wpa_s->current_ssid->ssid,
  1973. wpa_s->current_ssid->ssid_len) : "");
  1974. if (wpa_s->wpa_state == WPA_COMPLETED) {
  1975. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1976. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
  1977. "- connection to " MACSTR
  1978. " completed %s [id=%d id_str=%s]",
  1979. MAC2STR(wpa_s->bssid), "(auth)",
  1980. ssid ? ssid->id : -1,
  1981. ssid && ssid->id_str ? ssid->id_str : "");
  1982. }
  1983. }
  1984. #endif /* ANDROID */
  1985. return pos - buf;
  1986. }
  1987. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1988. char *cmd)
  1989. {
  1990. char *pos;
  1991. int id;
  1992. struct wpa_ssid *ssid;
  1993. u8 bssid[ETH_ALEN];
  1994. /* cmd: "<network id> <BSSID>" */
  1995. pos = os_strchr(cmd, ' ');
  1996. if (pos == NULL)
  1997. return -1;
  1998. *pos++ = '\0';
  1999. id = atoi(cmd);
  2000. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  2001. if (hwaddr_aton(pos, bssid)) {
  2002. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  2003. return -1;
  2004. }
  2005. ssid = wpa_config_get_network(wpa_s->conf, id);
  2006. if (ssid == NULL) {
  2007. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  2008. "to update", id);
  2009. return -1;
  2010. }
  2011. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  2012. ssid->bssid_set = !is_zero_ether_addr(bssid);
  2013. return 0;
  2014. }
  2015. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  2016. char *cmd, char *buf,
  2017. size_t buflen)
  2018. {
  2019. u8 bssid[ETH_ALEN];
  2020. struct wpa_blacklist *e;
  2021. char *pos, *end;
  2022. int ret;
  2023. /* cmd: "BLACKLIST [<BSSID>]" */
  2024. if (*cmd == '\0') {
  2025. pos = buf;
  2026. end = buf + buflen;
  2027. e = wpa_s->blacklist;
  2028. while (e) {
  2029. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  2030. MAC2STR(e->bssid));
  2031. if (os_snprintf_error(end - pos, ret))
  2032. return pos - buf;
  2033. pos += ret;
  2034. e = e->next;
  2035. }
  2036. return pos - buf;
  2037. }
  2038. cmd++;
  2039. if (os_strncmp(cmd, "clear", 5) == 0) {
  2040. wpa_blacklist_clear(wpa_s);
  2041. os_memcpy(buf, "OK\n", 3);
  2042. return 3;
  2043. }
  2044. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  2045. if (hwaddr_aton(cmd, bssid)) {
  2046. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  2047. return -1;
  2048. }
  2049. /*
  2050. * Add the BSSID twice, so its count will be 2, causing it to be
  2051. * skipped when processing scan results.
  2052. */
  2053. ret = wpa_blacklist_add(wpa_s, bssid);
  2054. if (ret < 0)
  2055. return -1;
  2056. ret = wpa_blacklist_add(wpa_s, bssid);
  2057. if (ret < 0)
  2058. return -1;
  2059. os_memcpy(buf, "OK\n", 3);
  2060. return 3;
  2061. }
  2062. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  2063. char *cmd, char *buf,
  2064. size_t buflen)
  2065. {
  2066. char *pos, *end, *stamp;
  2067. int ret;
  2068. /* cmd: "LOG_LEVEL [<level>]" */
  2069. if (*cmd == '\0') {
  2070. pos = buf;
  2071. end = buf + buflen;
  2072. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  2073. "Timestamp: %d\n",
  2074. debug_level_str(wpa_debug_level),
  2075. wpa_debug_timestamp);
  2076. if (os_snprintf_error(end - pos, ret))
  2077. ret = 0;
  2078. return ret;
  2079. }
  2080. while (*cmd == ' ')
  2081. cmd++;
  2082. stamp = os_strchr(cmd, ' ');
  2083. if (stamp) {
  2084. *stamp++ = '\0';
  2085. while (*stamp == ' ') {
  2086. stamp++;
  2087. }
  2088. }
  2089. if (os_strlen(cmd)) {
  2090. int level = str_to_debug_level(cmd);
  2091. if (level < 0)
  2092. return -1;
  2093. wpa_debug_level = level;
  2094. }
  2095. if (stamp && os_strlen(stamp))
  2096. wpa_debug_timestamp = atoi(stamp);
  2097. os_memcpy(buf, "OK\n", 3);
  2098. return 3;
  2099. }
  2100. static int wpa_supplicant_ctrl_iface_list_networks(
  2101. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2102. {
  2103. char *pos, *end, *prev;
  2104. struct wpa_ssid *ssid;
  2105. int ret;
  2106. pos = buf;
  2107. end = buf + buflen;
  2108. ret = os_snprintf(pos, end - pos,
  2109. "network id / ssid / bssid / flags\n");
  2110. if (os_snprintf_error(end - pos, ret))
  2111. return pos - buf;
  2112. pos += ret;
  2113. ssid = wpa_s->conf->ssid;
  2114. /* skip over ssids until we find next one */
  2115. if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
  2116. int last_id = atoi(cmd + 8);
  2117. if (last_id != -1) {
  2118. while (ssid != NULL && ssid->id <= last_id) {
  2119. ssid = ssid->next;
  2120. }
  2121. }
  2122. }
  2123. while (ssid) {
  2124. prev = pos;
  2125. ret = os_snprintf(pos, end - pos, "%d\t%s",
  2126. ssid->id,
  2127. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  2128. if (os_snprintf_error(end - pos, ret))
  2129. return prev - buf;
  2130. pos += ret;
  2131. if (ssid->bssid_set) {
  2132. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  2133. MAC2STR(ssid->bssid));
  2134. } else {
  2135. ret = os_snprintf(pos, end - pos, "\tany");
  2136. }
  2137. if (os_snprintf_error(end - pos, ret))
  2138. return prev - buf;
  2139. pos += ret;
  2140. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  2141. ssid == wpa_s->current_ssid ?
  2142. "[CURRENT]" : "",
  2143. ssid->disabled ? "[DISABLED]" : "",
  2144. ssid->disabled_until.sec ?
  2145. "[TEMP-DISABLED]" : "",
  2146. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  2147. "");
  2148. if (os_snprintf_error(end - pos, ret))
  2149. return prev - buf;
  2150. pos += ret;
  2151. ret = os_snprintf(pos, end - pos, "\n");
  2152. if (os_snprintf_error(end - pos, ret))
  2153. return prev - buf;
  2154. pos += ret;
  2155. ssid = ssid->next;
  2156. }
  2157. return pos - buf;
  2158. }
  2159. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  2160. {
  2161. int ret;
  2162. ret = os_snprintf(pos, end - pos, "-");
  2163. if (os_snprintf_error(end - pos, ret))
  2164. return pos;
  2165. pos += ret;
  2166. ret = wpa_write_ciphers(pos, end, cipher, "+");
  2167. if (ret < 0)
  2168. return pos;
  2169. pos += ret;
  2170. return pos;
  2171. }
  2172. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  2173. const u8 *ie, size_t ie_len)
  2174. {
  2175. struct wpa_ie_data data;
  2176. char *start;
  2177. int ret;
  2178. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  2179. if (os_snprintf_error(end - pos, ret))
  2180. return pos;
  2181. pos += ret;
  2182. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  2183. ret = os_snprintf(pos, end - pos, "?]");
  2184. if (os_snprintf_error(end - pos, ret))
  2185. return pos;
  2186. pos += ret;
  2187. return pos;
  2188. }
  2189. start = pos;
  2190. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  2191. ret = os_snprintf(pos, end - pos, "%sEAP",
  2192. pos == start ? "" : "+");
  2193. if (os_snprintf_error(end - pos, ret))
  2194. return pos;
  2195. pos += ret;
  2196. }
  2197. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  2198. ret = os_snprintf(pos, end - pos, "%sPSK",
  2199. pos == start ? "" : "+");
  2200. if (os_snprintf_error(end - pos, ret))
  2201. return pos;
  2202. pos += ret;
  2203. }
  2204. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  2205. ret = os_snprintf(pos, end - pos, "%sNone",
  2206. pos == start ? "" : "+");
  2207. if (os_snprintf_error(end - pos, ret))
  2208. return pos;
  2209. pos += ret;
  2210. }
  2211. if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
  2212. ret = os_snprintf(pos, end - pos, "%sSAE",
  2213. pos == start ? "" : "+");
  2214. if (os_snprintf_error(end - pos, ret))
  2215. return pos;
  2216. pos += ret;
  2217. }
  2218. #ifdef CONFIG_IEEE80211R
  2219. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  2220. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  2221. pos == start ? "" : "+");
  2222. if (os_snprintf_error(end - pos, ret))
  2223. return pos;
  2224. pos += ret;
  2225. }
  2226. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  2227. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  2228. pos == start ? "" : "+");
  2229. if (os_snprintf_error(end - pos, ret))
  2230. return pos;
  2231. pos += ret;
  2232. }
  2233. if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
  2234. ret = os_snprintf(pos, end - pos, "%sFT/SAE",
  2235. pos == start ? "" : "+");
  2236. if (os_snprintf_error(end - pos, ret))
  2237. return pos;
  2238. pos += ret;
  2239. }
  2240. #endif /* CONFIG_IEEE80211R */
  2241. #ifdef CONFIG_IEEE80211W
  2242. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  2243. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  2244. pos == start ? "" : "+");
  2245. if (os_snprintf_error(end - pos, ret))
  2246. return pos;
  2247. pos += ret;
  2248. }
  2249. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  2250. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  2251. pos == start ? "" : "+");
  2252. if (os_snprintf_error(end - pos, ret))
  2253. return pos;
  2254. pos += ret;
  2255. }
  2256. #endif /* CONFIG_IEEE80211W */
  2257. #ifdef CONFIG_SUITEB
  2258. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  2259. ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
  2260. pos == start ? "" : "+");
  2261. if (os_snprintf_error(end - pos, ret))
  2262. return pos;
  2263. pos += ret;
  2264. }
  2265. #endif /* CONFIG_SUITEB */
  2266. #ifdef CONFIG_SUITEB192
  2267. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
  2268. ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
  2269. pos == start ? "" : "+");
  2270. if (os_snprintf_error(end - pos, ret))
  2271. return pos;
  2272. pos += ret;
  2273. }
  2274. #endif /* CONFIG_SUITEB192 */
  2275. #ifdef CONFIG_FILS
  2276. if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
  2277. ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
  2278. pos == start ? "" : "+");
  2279. if (os_snprintf_error(end - pos, ret))
  2280. return pos;
  2281. pos += ret;
  2282. }
  2283. if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
  2284. ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
  2285. pos == start ? "" : "+");
  2286. if (os_snprintf_error(end - pos, ret))
  2287. return pos;
  2288. pos += ret;
  2289. }
  2290. #ifdef CONFIG_IEEE80211R
  2291. if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
  2292. ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
  2293. pos == start ? "" : "+");
  2294. if (os_snprintf_error(end - pos, ret))
  2295. return pos;
  2296. pos += ret;
  2297. }
  2298. if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
  2299. ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
  2300. pos == start ? "" : "+");
  2301. if (os_snprintf_error(end - pos, ret))
  2302. return pos;
  2303. pos += ret;
  2304. }
  2305. #endif /* CONFIG_IEEE80211R */
  2306. #endif /* CONFIG_FILS */
  2307. #ifdef CONFIG_OWE
  2308. if (data.key_mgmt & WPA_KEY_MGMT_OWE) {
  2309. ret = os_snprintf(pos, end - pos, "%sOWE",
  2310. pos == start ? "" : "+");
  2311. if (os_snprintf_error(end - pos, ret))
  2312. return pos;
  2313. pos += ret;
  2314. }
  2315. #endif /* CONFIG_OWE */
  2316. #ifdef CONFIG_DPP
  2317. if (data.key_mgmt & WPA_KEY_MGMT_DPP) {
  2318. ret = os_snprintf(pos, end - pos, "%sDPP",
  2319. pos == start ? "" : "+");
  2320. if (os_snprintf_error(end - pos, ret))
  2321. return pos;
  2322. pos += ret;
  2323. }
  2324. #endif /* CONFIG_DPP */
  2325. if (data.key_mgmt & WPA_KEY_MGMT_OSEN) {
  2326. ret = os_snprintf(pos, end - pos, "%sOSEN",
  2327. pos == start ? "" : "+");
  2328. if (os_snprintf_error(end - pos, ret))
  2329. return pos;
  2330. pos += ret;
  2331. }
  2332. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  2333. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  2334. ret = os_snprintf(pos, end - pos, "-preauth");
  2335. if (os_snprintf_error(end - pos, ret))
  2336. return pos;
  2337. pos += ret;
  2338. }
  2339. ret = os_snprintf(pos, end - pos, "]");
  2340. if (os_snprintf_error(end - pos, ret))
  2341. return pos;
  2342. pos += ret;
  2343. return pos;
  2344. }
  2345. #ifdef CONFIG_WPS
  2346. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  2347. char *pos, char *end,
  2348. struct wpabuf *wps_ie)
  2349. {
  2350. int ret;
  2351. const char *txt;
  2352. if (wps_ie == NULL)
  2353. return pos;
  2354. if (wps_is_selected_pbc_registrar(wps_ie))
  2355. txt = "[WPS-PBC]";
  2356. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  2357. txt = "[WPS-AUTH]";
  2358. else if (wps_is_selected_pin_registrar(wps_ie))
  2359. txt = "[WPS-PIN]";
  2360. else
  2361. txt = "[WPS]";
  2362. ret = os_snprintf(pos, end - pos, "%s", txt);
  2363. if (!os_snprintf_error(end - pos, ret))
  2364. pos += ret;
  2365. wpabuf_free(wps_ie);
  2366. return pos;
  2367. }
  2368. #endif /* CONFIG_WPS */
  2369. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  2370. char *pos, char *end,
  2371. const struct wpa_bss *bss)
  2372. {
  2373. #ifdef CONFIG_WPS
  2374. struct wpabuf *wps_ie;
  2375. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  2376. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  2377. #else /* CONFIG_WPS */
  2378. return pos;
  2379. #endif /* CONFIG_WPS */
  2380. }
  2381. /* Format one result on one text line into a buffer. */
  2382. static int wpa_supplicant_ctrl_iface_scan_result(
  2383. struct wpa_supplicant *wpa_s,
  2384. const struct wpa_bss *bss, char *buf, size_t buflen)
  2385. {
  2386. char *pos, *end;
  2387. int ret;
  2388. const u8 *ie, *ie2, *osen_ie, *p2p, *mesh, *owe;
  2389. mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
  2390. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  2391. if (!p2p)
  2392. p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
  2393. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  2394. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  2395. 0)
  2396. return 0; /* Do not show P2P listen discovery results here */
  2397. pos = buf;
  2398. end = buf + buflen;
  2399. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  2400. MAC2STR(bss->bssid), bss->freq, bss->level);
  2401. if (os_snprintf_error(end - pos, ret))
  2402. return -1;
  2403. pos += ret;
  2404. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2405. if (ie)
  2406. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  2407. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2408. if (ie2) {
  2409. pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
  2410. ie2, 2 + ie2[1]);
  2411. }
  2412. osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  2413. if (osen_ie)
  2414. pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
  2415. osen_ie, 2 + osen_ie[1]);
  2416. owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
  2417. if (owe) {
  2418. ret = os_snprintf(pos, end - pos,
  2419. ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
  2420. if (os_snprintf_error(end - pos, ret))
  2421. return -1;
  2422. pos += ret;
  2423. }
  2424. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2425. if (!ie && !ie2 && !osen_ie && (bss->caps & IEEE80211_CAP_PRIVACY)) {
  2426. ret = os_snprintf(pos, end - pos, "[WEP]");
  2427. if (os_snprintf_error(end - pos, ret))
  2428. return -1;
  2429. pos += ret;
  2430. }
  2431. if (mesh) {
  2432. ret = os_snprintf(pos, end - pos, "[MESH]");
  2433. if (os_snprintf_error(end - pos, ret))
  2434. return -1;
  2435. pos += ret;
  2436. }
  2437. if (bss_is_dmg(bss)) {
  2438. const char *s;
  2439. ret = os_snprintf(pos, end - pos, "[DMG]");
  2440. if (os_snprintf_error(end - pos, ret))
  2441. return -1;
  2442. pos += ret;
  2443. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  2444. case IEEE80211_CAP_DMG_IBSS:
  2445. s = "[IBSS]";
  2446. break;
  2447. case IEEE80211_CAP_DMG_AP:
  2448. s = "[ESS]";
  2449. break;
  2450. case IEEE80211_CAP_DMG_PBSS:
  2451. s = "[PBSS]";
  2452. break;
  2453. default:
  2454. s = "";
  2455. break;
  2456. }
  2457. ret = os_snprintf(pos, end - pos, "%s", s);
  2458. if (os_snprintf_error(end - pos, ret))
  2459. return -1;
  2460. pos += ret;
  2461. } else {
  2462. if (bss->caps & IEEE80211_CAP_IBSS) {
  2463. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2464. if (os_snprintf_error(end - pos, ret))
  2465. return -1;
  2466. pos += ret;
  2467. }
  2468. if (bss->caps & IEEE80211_CAP_ESS) {
  2469. ret = os_snprintf(pos, end - pos, "[ESS]");
  2470. if (os_snprintf_error(end - pos, ret))
  2471. return -1;
  2472. pos += ret;
  2473. }
  2474. }
  2475. if (p2p) {
  2476. ret = os_snprintf(pos, end - pos, "[P2P]");
  2477. if (os_snprintf_error(end - pos, ret))
  2478. return -1;
  2479. pos += ret;
  2480. }
  2481. #ifdef CONFIG_HS20
  2482. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  2483. ret = os_snprintf(pos, end - pos, "[HS20]");
  2484. if (os_snprintf_error(end - pos, ret))
  2485. return -1;
  2486. pos += ret;
  2487. }
  2488. #endif /* CONFIG_HS20 */
  2489. #ifdef CONFIG_FILS
  2490. if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
  2491. ret = os_snprintf(pos, end - pos, "[FILS]");
  2492. if (os_snprintf_error(end - pos, ret))
  2493. return -1;
  2494. pos += ret;
  2495. }
  2496. #endif /* CONFIG_FILS */
  2497. #ifdef CONFIG_FST
  2498. if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
  2499. ret = os_snprintf(pos, end - pos, "[FST]");
  2500. if (os_snprintf_error(end - pos, ret))
  2501. return -1;
  2502. pos += ret;
  2503. }
  2504. #endif /* CONFIG_FST */
  2505. ret = os_snprintf(pos, end - pos, "\t%s",
  2506. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2507. if (os_snprintf_error(end - pos, ret))
  2508. return -1;
  2509. pos += ret;
  2510. ret = os_snprintf(pos, end - pos, "\n");
  2511. if (os_snprintf_error(end - pos, ret))
  2512. return -1;
  2513. pos += ret;
  2514. return pos - buf;
  2515. }
  2516. static int wpa_supplicant_ctrl_iface_scan_results(
  2517. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2518. {
  2519. char *pos, *end;
  2520. struct wpa_bss *bss;
  2521. int ret;
  2522. pos = buf;
  2523. end = buf + buflen;
  2524. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  2525. "flags / ssid\n");
  2526. if (os_snprintf_error(end - pos, ret))
  2527. return pos - buf;
  2528. pos += ret;
  2529. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  2530. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  2531. end - pos);
  2532. if (ret < 0 || ret >= end - pos)
  2533. return pos - buf;
  2534. pos += ret;
  2535. }
  2536. return pos - buf;
  2537. }
  2538. #ifdef CONFIG_MESH
  2539. static int wpa_supplicant_ctrl_iface_mesh_interface_add(
  2540. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  2541. {
  2542. char *pos, ifname[IFNAMSIZ + 1];
  2543. ifname[0] = '\0';
  2544. pos = os_strstr(cmd, "ifname=");
  2545. if (pos) {
  2546. pos += 7;
  2547. os_strlcpy(ifname, pos, sizeof(ifname));
  2548. }
  2549. if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
  2550. return -1;
  2551. os_strlcpy(reply, ifname, max_len);
  2552. return os_strlen(ifname);
  2553. }
  2554. static int wpa_supplicant_ctrl_iface_mesh_group_add(
  2555. struct wpa_supplicant *wpa_s, char *cmd)
  2556. {
  2557. int id;
  2558. struct wpa_ssid *ssid;
  2559. id = atoi(cmd);
  2560. wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
  2561. ssid = wpa_config_get_network(wpa_s->conf, id);
  2562. if (ssid == NULL) {
  2563. wpa_printf(MSG_DEBUG,
  2564. "CTRL_IFACE: Could not find network id=%d", id);
  2565. return -1;
  2566. }
  2567. if (ssid->mode != WPAS_MODE_MESH) {
  2568. wpa_printf(MSG_DEBUG,
  2569. "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
  2570. return -1;
  2571. }
  2572. if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
  2573. ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
  2574. wpa_printf(MSG_ERROR,
  2575. "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
  2576. return -1;
  2577. }
  2578. /*
  2579. * TODO: If necessary write our own group_add function,
  2580. * for now we can reuse select_network
  2581. */
  2582. wpa_supplicant_select_network(wpa_s, ssid);
  2583. return 0;
  2584. }
  2585. static int wpa_supplicant_ctrl_iface_mesh_group_remove(
  2586. struct wpa_supplicant *wpa_s, char *cmd)
  2587. {
  2588. struct wpa_supplicant *orig;
  2589. struct wpa_global *global;
  2590. int found = 0;
  2591. wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
  2592. global = wpa_s->global;
  2593. orig = wpa_s;
  2594. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2595. if (os_strcmp(wpa_s->ifname, cmd) == 0) {
  2596. found = 1;
  2597. break;
  2598. }
  2599. }
  2600. if (!found) {
  2601. wpa_printf(MSG_ERROR,
  2602. "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
  2603. cmd);
  2604. return -1;
  2605. }
  2606. if (wpa_s->mesh_if_created && wpa_s == orig) {
  2607. wpa_printf(MSG_ERROR,
  2608. "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
  2609. return -1;
  2610. }
  2611. wpa_s->reassociate = 0;
  2612. wpa_s->disconnected = 1;
  2613. wpa_supplicant_cancel_sched_scan(wpa_s);
  2614. wpa_supplicant_cancel_scan(wpa_s);
  2615. /*
  2616. * TODO: If necessary write our own group_remove function,
  2617. * for now we can reuse deauthenticate
  2618. */
  2619. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2620. if (wpa_s->mesh_if_created)
  2621. wpa_supplicant_remove_iface(global, wpa_s, 0);
  2622. return 0;
  2623. }
  2624. static int wpa_supplicant_ctrl_iface_mesh_peer_remove(
  2625. struct wpa_supplicant *wpa_s, char *cmd)
  2626. {
  2627. u8 addr[ETH_ALEN];
  2628. if (hwaddr_aton(cmd, addr) < 0)
  2629. return -1;
  2630. return wpas_mesh_peer_remove(wpa_s, addr);
  2631. }
  2632. static int wpa_supplicant_ctrl_iface_mesh_peer_add(
  2633. struct wpa_supplicant *wpa_s, char *cmd)
  2634. {
  2635. u8 addr[ETH_ALEN];
  2636. int duration;
  2637. char *pos;
  2638. pos = os_strstr(cmd, " duration=");
  2639. if (pos) {
  2640. *pos = '\0';
  2641. duration = atoi(pos + 10);
  2642. } else {
  2643. duration = -1;
  2644. }
  2645. if (hwaddr_aton(cmd, addr))
  2646. return -1;
  2647. return wpas_mesh_peer_add(wpa_s, addr, duration);
  2648. }
  2649. #endif /* CONFIG_MESH */
  2650. static int wpa_supplicant_ctrl_iface_select_network(
  2651. struct wpa_supplicant *wpa_s, char *cmd)
  2652. {
  2653. int id;
  2654. struct wpa_ssid *ssid;
  2655. char *pos;
  2656. /* cmd: "<network id>" or "any" */
  2657. if (os_strncmp(cmd, "any", 3) == 0) {
  2658. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  2659. ssid = NULL;
  2660. } else {
  2661. id = atoi(cmd);
  2662. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  2663. ssid = wpa_config_get_network(wpa_s->conf, id);
  2664. if (ssid == NULL) {
  2665. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2666. "network id=%d", id);
  2667. return -1;
  2668. }
  2669. if (ssid->disabled == 2) {
  2670. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2671. "SELECT_NETWORK with persistent P2P group");
  2672. return -1;
  2673. }
  2674. }
  2675. pos = os_strstr(cmd, " freq=");
  2676. if (pos) {
  2677. int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
  2678. if (freqs) {
  2679. os_free(wpa_s->select_network_scan_freqs);
  2680. wpa_s->select_network_scan_freqs = freqs;
  2681. }
  2682. }
  2683. wpa_s->scan_min_time.sec = 0;
  2684. wpa_s->scan_min_time.usec = 0;
  2685. wpa_supplicant_select_network(wpa_s, ssid);
  2686. return 0;
  2687. }
  2688. static int wpa_supplicant_ctrl_iface_enable_network(
  2689. struct wpa_supplicant *wpa_s, char *cmd)
  2690. {
  2691. int id;
  2692. struct wpa_ssid *ssid;
  2693. /* cmd: "<network id>" or "all" */
  2694. if (os_strcmp(cmd, "all") == 0) {
  2695. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  2696. ssid = NULL;
  2697. } else {
  2698. id = atoi(cmd);
  2699. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  2700. ssid = wpa_config_get_network(wpa_s->conf, id);
  2701. if (ssid == NULL) {
  2702. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2703. "network id=%d", id);
  2704. return -1;
  2705. }
  2706. if (ssid->disabled == 2) {
  2707. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2708. "ENABLE_NETWORK with persistent P2P group");
  2709. return -1;
  2710. }
  2711. if (os_strstr(cmd, " no-connect")) {
  2712. ssid->disabled = 0;
  2713. return 0;
  2714. }
  2715. }
  2716. wpa_s->scan_min_time.sec = 0;
  2717. wpa_s->scan_min_time.usec = 0;
  2718. wpa_supplicant_enable_network(wpa_s, ssid);
  2719. return 0;
  2720. }
  2721. static int wpa_supplicant_ctrl_iface_disable_network(
  2722. struct wpa_supplicant *wpa_s, char *cmd)
  2723. {
  2724. int id;
  2725. struct wpa_ssid *ssid;
  2726. /* cmd: "<network id>" or "all" */
  2727. if (os_strcmp(cmd, "all") == 0) {
  2728. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  2729. ssid = NULL;
  2730. } else {
  2731. id = atoi(cmd);
  2732. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  2733. ssid = wpa_config_get_network(wpa_s->conf, id);
  2734. if (ssid == NULL) {
  2735. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2736. "network id=%d", id);
  2737. return -1;
  2738. }
  2739. if (ssid->disabled == 2) {
  2740. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2741. "DISABLE_NETWORK with persistent P2P "
  2742. "group");
  2743. return -1;
  2744. }
  2745. }
  2746. wpa_supplicant_disable_network(wpa_s, ssid);
  2747. return 0;
  2748. }
  2749. static int wpa_supplicant_ctrl_iface_add_network(
  2750. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2751. {
  2752. struct wpa_ssid *ssid;
  2753. int ret;
  2754. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  2755. ssid = wpa_supplicant_add_network(wpa_s);
  2756. if (ssid == NULL)
  2757. return -1;
  2758. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  2759. if (os_snprintf_error(buflen, ret))
  2760. return -1;
  2761. return ret;
  2762. }
  2763. static int wpa_supplicant_ctrl_iface_remove_network(
  2764. struct wpa_supplicant *wpa_s, char *cmd)
  2765. {
  2766. int id;
  2767. struct wpa_ssid *ssid;
  2768. int result;
  2769. /* cmd: "<network id>" or "all" */
  2770. if (os_strcmp(cmd, "all") == 0) {
  2771. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  2772. if (wpa_s->sched_scanning)
  2773. wpa_supplicant_cancel_sched_scan(wpa_s);
  2774. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2775. if (wpa_s->current_ssid) {
  2776. #ifdef CONFIG_SME
  2777. wpa_s->sme.prev_bssid_set = 0;
  2778. #endif /* CONFIG_SME */
  2779. wpa_sm_set_config(wpa_s->wpa, NULL);
  2780. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2781. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2782. wpa_s->own_disconnect_req = 1;
  2783. wpa_supplicant_deauthenticate(
  2784. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2785. }
  2786. ssid = wpa_s->conf->ssid;
  2787. while (ssid) {
  2788. struct wpa_ssid *remove_ssid = ssid;
  2789. id = ssid->id;
  2790. ssid = ssid->next;
  2791. if (wpa_s->last_ssid == remove_ssid)
  2792. wpa_s->last_ssid = NULL;
  2793. wpas_notify_network_removed(wpa_s, remove_ssid);
  2794. wpa_config_remove_network(wpa_s->conf, id);
  2795. }
  2796. return 0;
  2797. }
  2798. id = atoi(cmd);
  2799. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  2800. result = wpa_supplicant_remove_network(wpa_s, id);
  2801. if (result == -1) {
  2802. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2803. "id=%d", id);
  2804. return -1;
  2805. }
  2806. if (result == -2) {
  2807. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  2808. "network id=%d", id);
  2809. return -1;
  2810. }
  2811. return 0;
  2812. }
  2813. static int wpa_supplicant_ctrl_iface_update_network(
  2814. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2815. char *name, char *value)
  2816. {
  2817. int ret;
  2818. ret = wpa_config_set(ssid, name, value, 0);
  2819. if (ret < 0) {
  2820. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  2821. "variable '%s'", name);
  2822. return -1;
  2823. }
  2824. if (ret == 1)
  2825. return 0; /* No change to the previously configured value */
  2826. if (os_strcmp(name, "bssid") != 0 &&
  2827. os_strcmp(name, "bssid_hint") != 0 &&
  2828. os_strcmp(name, "priority") != 0) {
  2829. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  2830. if (wpa_s->current_ssid == ssid ||
  2831. wpa_s->current_ssid == NULL) {
  2832. /*
  2833. * Invalidate the EAP session cache if anything in the
  2834. * current or previously used configuration changes.
  2835. */
  2836. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2837. }
  2838. }
  2839. if ((os_strcmp(name, "psk") == 0 &&
  2840. value[0] == '"' && ssid->ssid_len) ||
  2841. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  2842. wpa_config_update_psk(ssid);
  2843. else if (os_strcmp(name, "priority") == 0)
  2844. wpa_config_update_prio_list(wpa_s->conf);
  2845. return 0;
  2846. }
  2847. static int wpa_supplicant_ctrl_iface_set_network(
  2848. struct wpa_supplicant *wpa_s, char *cmd)
  2849. {
  2850. int id, ret, prev_bssid_set, prev_disabled;
  2851. struct wpa_ssid *ssid;
  2852. char *name, *value;
  2853. u8 prev_bssid[ETH_ALEN];
  2854. /* cmd: "<network id> <variable name> <value>" */
  2855. name = os_strchr(cmd, ' ');
  2856. if (name == NULL)
  2857. return -1;
  2858. *name++ = '\0';
  2859. value = os_strchr(name, ' ');
  2860. if (value == NULL)
  2861. return -1;
  2862. *value++ = '\0';
  2863. id = atoi(cmd);
  2864. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  2865. id, name);
  2866. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2867. (u8 *) value, os_strlen(value));
  2868. ssid = wpa_config_get_network(wpa_s->conf, id);
  2869. if (ssid == NULL) {
  2870. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2871. "id=%d", id);
  2872. return -1;
  2873. }
  2874. prev_bssid_set = ssid->bssid_set;
  2875. prev_disabled = ssid->disabled;
  2876. os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
  2877. ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
  2878. value);
  2879. if (ret == 0 &&
  2880. (ssid->bssid_set != prev_bssid_set ||
  2881. os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
  2882. wpas_notify_network_bssid_set_changed(wpa_s, ssid);
  2883. if (prev_disabled != ssid->disabled &&
  2884. (prev_disabled == 2 || ssid->disabled == 2))
  2885. wpas_notify_network_type_changed(wpa_s, ssid);
  2886. return ret;
  2887. }
  2888. static int wpa_supplicant_ctrl_iface_get_network(
  2889. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2890. {
  2891. int id;
  2892. size_t res;
  2893. struct wpa_ssid *ssid;
  2894. char *name, *value;
  2895. /* cmd: "<network id> <variable name>" */
  2896. name = os_strchr(cmd, ' ');
  2897. if (name == NULL || buflen == 0)
  2898. return -1;
  2899. *name++ = '\0';
  2900. id = atoi(cmd);
  2901. wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2902. id, name);
  2903. ssid = wpa_config_get_network(wpa_s->conf, id);
  2904. if (ssid == NULL) {
  2905. wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Could not find network "
  2906. "id=%d", id);
  2907. return -1;
  2908. }
  2909. value = wpa_config_get_no_key(ssid, name);
  2910. if (value == NULL) {
  2911. wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Failed to get network "
  2912. "variable '%s'", name);
  2913. return -1;
  2914. }
  2915. res = os_strlcpy(buf, value, buflen);
  2916. if (res >= buflen) {
  2917. os_free(value);
  2918. return -1;
  2919. }
  2920. os_free(value);
  2921. return res;
  2922. }
  2923. static int wpa_supplicant_ctrl_iface_dup_network(
  2924. struct wpa_supplicant *wpa_s, char *cmd,
  2925. struct wpa_supplicant *dst_wpa_s)
  2926. {
  2927. struct wpa_ssid *ssid_s, *ssid_d;
  2928. char *name, *id, *value;
  2929. int id_s, id_d, ret;
  2930. /* cmd: "<src network id> <dst network id> <variable name>" */
  2931. id = os_strchr(cmd, ' ');
  2932. if (id == NULL)
  2933. return -1;
  2934. *id++ = '\0';
  2935. name = os_strchr(id, ' ');
  2936. if (name == NULL)
  2937. return -1;
  2938. *name++ = '\0';
  2939. id_s = atoi(cmd);
  2940. id_d = atoi(id);
  2941. wpa_printf(MSG_DEBUG,
  2942. "CTRL_IFACE: DUP_NETWORK ifname=%s->%s id=%d->%d name='%s'",
  2943. wpa_s->ifname, dst_wpa_s->ifname, id_s, id_d, name);
  2944. ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
  2945. if (ssid_s == NULL) {
  2946. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2947. "network id=%d", id_s);
  2948. return -1;
  2949. }
  2950. ssid_d = wpa_config_get_network(dst_wpa_s->conf, id_d);
  2951. if (ssid_d == NULL) {
  2952. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2953. "network id=%d", id_d);
  2954. return -1;
  2955. }
  2956. value = wpa_config_get(ssid_s, name);
  2957. if (value == NULL) {
  2958. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2959. "variable '%s'", name);
  2960. return -1;
  2961. }
  2962. ret = wpa_supplicant_ctrl_iface_update_network(dst_wpa_s, ssid_d, name,
  2963. value);
  2964. os_free(value);
  2965. return ret;
  2966. }
  2967. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2968. char *buf, size_t buflen)
  2969. {
  2970. char *pos, *end;
  2971. struct wpa_cred *cred;
  2972. int ret;
  2973. pos = buf;
  2974. end = buf + buflen;
  2975. ret = os_snprintf(pos, end - pos,
  2976. "cred id / realm / username / domain / imsi\n");
  2977. if (os_snprintf_error(end - pos, ret))
  2978. return pos - buf;
  2979. pos += ret;
  2980. cred = wpa_s->conf->cred;
  2981. while (cred) {
  2982. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2983. cred->id, cred->realm ? cred->realm : "",
  2984. cred->username ? cred->username : "",
  2985. cred->domain ? cred->domain[0] : "",
  2986. cred->imsi ? cred->imsi : "");
  2987. if (os_snprintf_error(end - pos, ret))
  2988. return pos - buf;
  2989. pos += ret;
  2990. cred = cred->next;
  2991. }
  2992. return pos - buf;
  2993. }
  2994. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2995. char *buf, size_t buflen)
  2996. {
  2997. struct wpa_cred *cred;
  2998. int ret;
  2999. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  3000. cred = wpa_config_add_cred(wpa_s->conf);
  3001. if (cred == NULL)
  3002. return -1;
  3003. wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
  3004. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  3005. if (os_snprintf_error(buflen, ret))
  3006. return -1;
  3007. return ret;
  3008. }
  3009. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  3010. struct wpa_cred *cred)
  3011. {
  3012. struct wpa_ssid *ssid;
  3013. char str[20];
  3014. int id;
  3015. if (cred == NULL) {
  3016. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  3017. return -1;
  3018. }
  3019. id = cred->id;
  3020. if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
  3021. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  3022. return -1;
  3023. }
  3024. wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
  3025. /* Remove any network entry created based on the removed credential */
  3026. ssid = wpa_s->conf->ssid;
  3027. while (ssid) {
  3028. if (ssid->parent_cred == cred) {
  3029. int res;
  3030. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  3031. "used the removed credential", ssid->id);
  3032. res = os_snprintf(str, sizeof(str), "%d", ssid->id);
  3033. if (os_snprintf_error(sizeof(str), res))
  3034. str[sizeof(str) - 1] = '\0';
  3035. ssid = ssid->next;
  3036. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  3037. } else
  3038. ssid = ssid->next;
  3039. }
  3040. return 0;
  3041. }
  3042. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  3043. char *cmd)
  3044. {
  3045. int id;
  3046. struct wpa_cred *cred, *prev;
  3047. /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
  3048. * "provisioning_sp=<FQDN> */
  3049. if (os_strcmp(cmd, "all") == 0) {
  3050. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  3051. cred = wpa_s->conf->cred;
  3052. while (cred) {
  3053. prev = cred;
  3054. cred = cred->next;
  3055. wpas_ctrl_remove_cred(wpa_s, prev);
  3056. }
  3057. return 0;
  3058. }
  3059. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  3060. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  3061. cmd + 8);
  3062. cred = wpa_s->conf->cred;
  3063. while (cred) {
  3064. prev = cred;
  3065. cred = cred->next;
  3066. if (prev->domain) {
  3067. size_t i;
  3068. for (i = 0; i < prev->num_domain; i++) {
  3069. if (os_strcmp(prev->domain[i], cmd + 8)
  3070. != 0)
  3071. continue;
  3072. wpas_ctrl_remove_cred(wpa_s, prev);
  3073. break;
  3074. }
  3075. }
  3076. }
  3077. return 0;
  3078. }
  3079. if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
  3080. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
  3081. cmd + 16);
  3082. cred = wpa_s->conf->cred;
  3083. while (cred) {
  3084. prev = cred;
  3085. cred = cred->next;
  3086. if (prev->provisioning_sp &&
  3087. os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
  3088. wpas_ctrl_remove_cred(wpa_s, prev);
  3089. }
  3090. return 0;
  3091. }
  3092. id = atoi(cmd);
  3093. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  3094. cred = wpa_config_get_cred(wpa_s->conf, id);
  3095. return wpas_ctrl_remove_cred(wpa_s, cred);
  3096. }
  3097. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  3098. char *cmd)
  3099. {
  3100. int id;
  3101. struct wpa_cred *cred;
  3102. char *name, *value;
  3103. /* cmd: "<cred id> <variable name> <value>" */
  3104. name = os_strchr(cmd, ' ');
  3105. if (name == NULL)
  3106. return -1;
  3107. *name++ = '\0';
  3108. value = os_strchr(name, ' ');
  3109. if (value == NULL)
  3110. return -1;
  3111. *value++ = '\0';
  3112. id = atoi(cmd);
  3113. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  3114. id, name);
  3115. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  3116. (u8 *) value, os_strlen(value));
  3117. cred = wpa_config_get_cred(wpa_s->conf, id);
  3118. if (cred == NULL) {
  3119. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  3120. id);
  3121. return -1;
  3122. }
  3123. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  3124. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  3125. "variable '%s'", name);
  3126. return -1;
  3127. }
  3128. wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
  3129. return 0;
  3130. }
  3131. static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
  3132. char *cmd, char *buf,
  3133. size_t buflen)
  3134. {
  3135. int id;
  3136. size_t res;
  3137. struct wpa_cred *cred;
  3138. char *name, *value;
  3139. /* cmd: "<cred id> <variable name>" */
  3140. name = os_strchr(cmd, ' ');
  3141. if (name == NULL)
  3142. return -1;
  3143. *name++ = '\0';
  3144. id = atoi(cmd);
  3145. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
  3146. id, name);
  3147. cred = wpa_config_get_cred(wpa_s->conf, id);
  3148. if (cred == NULL) {
  3149. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  3150. id);
  3151. return -1;
  3152. }
  3153. value = wpa_config_get_cred_no_key(cred, name);
  3154. if (value == NULL) {
  3155. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
  3156. name);
  3157. return -1;
  3158. }
  3159. res = os_strlcpy(buf, value, buflen);
  3160. if (res >= buflen) {
  3161. os_free(value);
  3162. return -1;
  3163. }
  3164. os_free(value);
  3165. return res;
  3166. }
  3167. #ifndef CONFIG_NO_CONFIG_WRITE
  3168. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  3169. {
  3170. int ret;
  3171. if (!wpa_s->conf->update_config) {
  3172. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  3173. "to update configuration (update_config=0)");
  3174. return -1;
  3175. }
  3176. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  3177. if (ret) {
  3178. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  3179. "update configuration");
  3180. } else {
  3181. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  3182. " updated");
  3183. }
  3184. return ret;
  3185. }
  3186. #endif /* CONFIG_NO_CONFIG_WRITE */
  3187. struct cipher_info {
  3188. unsigned int capa;
  3189. const char *name;
  3190. int group_only;
  3191. };
  3192. static const struct cipher_info ciphers[] = {
  3193. { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
  3194. { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
  3195. { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
  3196. { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
  3197. { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
  3198. { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
  3199. { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
  3200. { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
  3201. };
  3202. static const struct cipher_info ciphers_group_mgmt[] = {
  3203. { WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
  3204. { WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
  3205. { WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
  3206. { WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
  3207. };
  3208. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  3209. struct wpa_driver_capa *capa,
  3210. char *buf, size_t buflen)
  3211. {
  3212. int ret;
  3213. char *pos, *end;
  3214. size_t len;
  3215. unsigned int i;
  3216. pos = buf;
  3217. end = pos + buflen;
  3218. if (res < 0) {
  3219. if (strict)
  3220. return 0;
  3221. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  3222. if (len >= buflen)
  3223. return -1;
  3224. return len;
  3225. }
  3226. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  3227. if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
  3228. ret = os_snprintf(pos, end - pos, "%s%s",
  3229. pos == buf ? "" : " ",
  3230. ciphers[i].name);
  3231. if (os_snprintf_error(end - pos, ret))
  3232. return pos - buf;
  3233. pos += ret;
  3234. }
  3235. }
  3236. return pos - buf;
  3237. }
  3238. static int ctrl_iface_get_capability_group(int res, char *strict,
  3239. struct wpa_driver_capa *capa,
  3240. char *buf, size_t buflen)
  3241. {
  3242. int ret;
  3243. char *pos, *end;
  3244. size_t len;
  3245. unsigned int i;
  3246. pos = buf;
  3247. end = pos + buflen;
  3248. if (res < 0) {
  3249. if (strict)
  3250. return 0;
  3251. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  3252. if (len >= buflen)
  3253. return -1;
  3254. return len;
  3255. }
  3256. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  3257. if (capa->enc & ciphers[i].capa) {
  3258. ret = os_snprintf(pos, end - pos, "%s%s",
  3259. pos == buf ? "" : " ",
  3260. ciphers[i].name);
  3261. if (os_snprintf_error(end - pos, ret))
  3262. return pos - buf;
  3263. pos += ret;
  3264. }
  3265. }
  3266. return pos - buf;
  3267. }
  3268. static int ctrl_iface_get_capability_group_mgmt(int res, char *strict,
  3269. struct wpa_driver_capa *capa,
  3270. char *buf, size_t buflen)
  3271. {
  3272. int ret;
  3273. char *pos, *end;
  3274. unsigned int i;
  3275. pos = buf;
  3276. end = pos + buflen;
  3277. if (res < 0)
  3278. return 0;
  3279. for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
  3280. if (capa->enc & ciphers_group_mgmt[i].capa) {
  3281. ret = os_snprintf(pos, end - pos, "%s%s",
  3282. pos == buf ? "" : " ",
  3283. ciphers_group_mgmt[i].name);
  3284. if (os_snprintf_error(end - pos, ret))
  3285. return pos - buf;
  3286. pos += ret;
  3287. }
  3288. }
  3289. return pos - buf;
  3290. }
  3291. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  3292. struct wpa_driver_capa *capa,
  3293. char *buf, size_t buflen)
  3294. {
  3295. int ret;
  3296. char *pos, *end;
  3297. size_t len;
  3298. pos = buf;
  3299. end = pos + buflen;
  3300. if (res < 0) {
  3301. if (strict)
  3302. return 0;
  3303. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  3304. "NONE", buflen);
  3305. if (len >= buflen)
  3306. return -1;
  3307. return len;
  3308. }
  3309. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  3310. if (os_snprintf_error(end - pos, ret))
  3311. return pos - buf;
  3312. pos += ret;
  3313. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  3314. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  3315. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  3316. if (os_snprintf_error(end - pos, ret))
  3317. return pos - buf;
  3318. pos += ret;
  3319. }
  3320. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  3321. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  3322. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  3323. if (os_snprintf_error(end - pos, ret))
  3324. return pos - buf;
  3325. pos += ret;
  3326. }
  3327. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  3328. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  3329. if (os_snprintf_error(end - pos, ret))
  3330. return pos - buf;
  3331. pos += ret;
  3332. }
  3333. #ifdef CONFIG_SUITEB
  3334. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
  3335. ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
  3336. if (os_snprintf_error(end - pos, ret))
  3337. return pos - buf;
  3338. pos += ret;
  3339. }
  3340. #endif /* CONFIG_SUITEB */
  3341. #ifdef CONFIG_SUITEB192
  3342. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
  3343. ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
  3344. if (os_snprintf_error(end - pos, ret))
  3345. return pos - buf;
  3346. pos += ret;
  3347. }
  3348. #endif /* CONFIG_SUITEB192 */
  3349. #ifdef CONFIG_OWE
  3350. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OWE) {
  3351. ret = os_snprintf(pos, end - pos, " OWE");
  3352. if (os_snprintf_error(end - pos, ret))
  3353. return pos - buf;
  3354. pos += ret;
  3355. }
  3356. #endif /* CONFIG_OWE */
  3357. #ifdef CONFIG_DPP
  3358. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_DPP) {
  3359. ret = os_snprintf(pos, end - pos, " DPP");
  3360. if (os_snprintf_error(end - pos, ret))
  3361. return pos - buf;
  3362. pos += ret;
  3363. }
  3364. #endif /* CONFIG_DPP */
  3365. #ifdef CONFIG_FILS
  3366. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA256) {
  3367. ret = os_snprintf(pos, end - pos, " FILS-SHA256");
  3368. if (os_snprintf_error(end - pos, ret))
  3369. return pos - buf;
  3370. pos += ret;
  3371. }
  3372. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA384) {
  3373. ret = os_snprintf(pos, end - pos, " FILS-SHA384");
  3374. if (os_snprintf_error(end - pos, ret))
  3375. return pos - buf;
  3376. pos += ret;
  3377. }
  3378. #ifdef CONFIG_IEEE80211R
  3379. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA256) {
  3380. ret = os_snprintf(pos, end - pos, " FT-FILS-SHA256");
  3381. if (os_snprintf_error(end - pos, ret))
  3382. return pos - buf;
  3383. pos += ret;
  3384. }
  3385. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA384) {
  3386. ret = os_snprintf(pos, end - pos, " FT-FILS-SHA384");
  3387. if (os_snprintf_error(end - pos, ret))
  3388. return pos - buf;
  3389. pos += ret;
  3390. }
  3391. #endif /* CONFIG_IEEE80211R */
  3392. #endif /* CONFIG_FILS */
  3393. return pos - buf;
  3394. }
  3395. static int ctrl_iface_get_capability_proto(int res, char *strict,
  3396. struct wpa_driver_capa *capa,
  3397. char *buf, size_t buflen)
  3398. {
  3399. int ret;
  3400. char *pos, *end;
  3401. size_t len;
  3402. pos = buf;
  3403. end = pos + buflen;
  3404. if (res < 0) {
  3405. if (strict)
  3406. return 0;
  3407. len = os_strlcpy(buf, "RSN WPA", buflen);
  3408. if (len >= buflen)
  3409. return -1;
  3410. return len;
  3411. }
  3412. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  3413. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  3414. ret = os_snprintf(pos, end - pos, "%sRSN",
  3415. pos == buf ? "" : " ");
  3416. if (os_snprintf_error(end - pos, ret))
  3417. return pos - buf;
  3418. pos += ret;
  3419. }
  3420. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  3421. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  3422. ret = os_snprintf(pos, end - pos, "%sWPA",
  3423. pos == buf ? "" : " ");
  3424. if (os_snprintf_error(end - pos, ret))
  3425. return pos - buf;
  3426. pos += ret;
  3427. }
  3428. return pos - buf;
  3429. }
  3430. static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
  3431. int res, char *strict,
  3432. struct wpa_driver_capa *capa,
  3433. char *buf, size_t buflen)
  3434. {
  3435. int ret;
  3436. char *pos, *end;
  3437. size_t len;
  3438. pos = buf;
  3439. end = pos + buflen;
  3440. if (res < 0) {
  3441. if (strict)
  3442. return 0;
  3443. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  3444. if (len >= buflen)
  3445. return -1;
  3446. return len;
  3447. }
  3448. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  3449. ret = os_snprintf(pos, end - pos, "%sOPEN",
  3450. pos == buf ? "" : " ");
  3451. if (os_snprintf_error(end - pos, ret))
  3452. return pos - buf;
  3453. pos += ret;
  3454. }
  3455. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  3456. ret = os_snprintf(pos, end - pos, "%sSHARED",
  3457. pos == buf ? "" : " ");
  3458. if (os_snprintf_error(end - pos, ret))
  3459. return pos - buf;
  3460. pos += ret;
  3461. }
  3462. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  3463. ret = os_snprintf(pos, end - pos, "%sLEAP",
  3464. pos == buf ? "" : " ");
  3465. if (os_snprintf_error(end - pos, ret))
  3466. return pos - buf;
  3467. pos += ret;
  3468. }
  3469. #ifdef CONFIG_SAE
  3470. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
  3471. ret = os_snprintf(pos, end - pos, "%sSAE",
  3472. pos == buf ? "" : " ");
  3473. if (os_snprintf_error(end - pos, ret))
  3474. return pos - buf;
  3475. pos += ret;
  3476. }
  3477. #endif /* CONFIG_SAE */
  3478. #ifdef CONFIG_FILS
  3479. if (wpa_is_fils_supported(wpa_s)) {
  3480. ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITHOUT_PFS",
  3481. pos == buf ? "" : " ");
  3482. if (os_snprintf_error(end - pos, ret))
  3483. return pos - buf;
  3484. pos += ret;
  3485. }
  3486. #ifdef CONFIG_FILS_SK_PFS
  3487. if (wpa_is_fils_sk_pfs_supported(wpa_s)) {
  3488. ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITH_PFS",
  3489. pos == buf ? "" : " ");
  3490. if (os_snprintf_error(end - pos, ret))
  3491. return pos - buf;
  3492. pos += ret;
  3493. }
  3494. #endif /* CONFIG_FILS_SK_PFS */
  3495. #endif /* CONFIG_FILS */
  3496. return pos - buf;
  3497. }
  3498. static int ctrl_iface_get_capability_modes(int res, char *strict,
  3499. struct wpa_driver_capa *capa,
  3500. char *buf, size_t buflen)
  3501. {
  3502. int ret;
  3503. char *pos, *end;
  3504. size_t len;
  3505. pos = buf;
  3506. end = pos + buflen;
  3507. if (res < 0) {
  3508. if (strict)
  3509. return 0;
  3510. len = os_strlcpy(buf, "IBSS AP", buflen);
  3511. if (len >= buflen)
  3512. return -1;
  3513. return len;
  3514. }
  3515. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  3516. ret = os_snprintf(pos, end - pos, "%sIBSS",
  3517. pos == buf ? "" : " ");
  3518. if (os_snprintf_error(end - pos, ret))
  3519. return pos - buf;
  3520. pos += ret;
  3521. }
  3522. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  3523. ret = os_snprintf(pos, end - pos, "%sAP",
  3524. pos == buf ? "" : " ");
  3525. if (os_snprintf_error(end - pos, ret))
  3526. return pos - buf;
  3527. pos += ret;
  3528. }
  3529. #ifdef CONFIG_MESH
  3530. if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
  3531. ret = os_snprintf(pos, end - pos, "%sMESH",
  3532. pos == buf ? "" : " ");
  3533. if (os_snprintf_error(end - pos, ret))
  3534. return pos - buf;
  3535. pos += ret;
  3536. }
  3537. #endif /* CONFIG_MESH */
  3538. return pos - buf;
  3539. }
  3540. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  3541. char *buf, size_t buflen)
  3542. {
  3543. struct hostapd_channel_data *chnl;
  3544. int ret, i, j;
  3545. char *pos, *end, *hmode;
  3546. pos = buf;
  3547. end = pos + buflen;
  3548. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  3549. switch (wpa_s->hw.modes[j].mode) {
  3550. case HOSTAPD_MODE_IEEE80211B:
  3551. hmode = "B";
  3552. break;
  3553. case HOSTAPD_MODE_IEEE80211G:
  3554. hmode = "G";
  3555. break;
  3556. case HOSTAPD_MODE_IEEE80211A:
  3557. hmode = "A";
  3558. break;
  3559. case HOSTAPD_MODE_IEEE80211AD:
  3560. hmode = "AD";
  3561. break;
  3562. default:
  3563. continue;
  3564. }
  3565. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  3566. if (os_snprintf_error(end - pos, ret))
  3567. return pos - buf;
  3568. pos += ret;
  3569. chnl = wpa_s->hw.modes[j].channels;
  3570. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  3571. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  3572. continue;
  3573. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  3574. if (os_snprintf_error(end - pos, ret))
  3575. return pos - buf;
  3576. pos += ret;
  3577. }
  3578. ret = os_snprintf(pos, end - pos, "\n");
  3579. if (os_snprintf_error(end - pos, ret))
  3580. return pos - buf;
  3581. pos += ret;
  3582. }
  3583. return pos - buf;
  3584. }
  3585. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  3586. char *buf, size_t buflen)
  3587. {
  3588. struct hostapd_channel_data *chnl;
  3589. int ret, i, j;
  3590. char *pos, *end, *hmode;
  3591. pos = buf;
  3592. end = pos + buflen;
  3593. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  3594. switch (wpa_s->hw.modes[j].mode) {
  3595. case HOSTAPD_MODE_IEEE80211B:
  3596. hmode = "B";
  3597. break;
  3598. case HOSTAPD_MODE_IEEE80211G:
  3599. hmode = "G";
  3600. break;
  3601. case HOSTAPD_MODE_IEEE80211A:
  3602. hmode = "A";
  3603. break;
  3604. case HOSTAPD_MODE_IEEE80211AD:
  3605. hmode = "AD";
  3606. break;
  3607. default:
  3608. continue;
  3609. }
  3610. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  3611. hmode);
  3612. if (os_snprintf_error(end - pos, ret))
  3613. return pos - buf;
  3614. pos += ret;
  3615. chnl = wpa_s->hw.modes[j].channels;
  3616. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  3617. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  3618. continue;
  3619. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
  3620. chnl[i].chan, chnl[i].freq,
  3621. chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
  3622. " (NO_IR)" : "",
  3623. chnl[i].flag & HOSTAPD_CHAN_RADAR ?
  3624. " (DFS)" : "");
  3625. if (os_snprintf_error(end - pos, ret))
  3626. return pos - buf;
  3627. pos += ret;
  3628. }
  3629. ret = os_snprintf(pos, end - pos, "\n");
  3630. if (os_snprintf_error(end - pos, ret))
  3631. return pos - buf;
  3632. pos += ret;
  3633. }
  3634. return pos - buf;
  3635. }
  3636. static int wpa_supplicant_ctrl_iface_get_capability(
  3637. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  3638. size_t buflen)
  3639. {
  3640. struct wpa_driver_capa capa;
  3641. int res;
  3642. char *strict;
  3643. char field[30];
  3644. size_t len;
  3645. /* Determine whether or not strict checking was requested */
  3646. len = os_strlcpy(field, _field, sizeof(field));
  3647. if (len >= sizeof(field))
  3648. return -1;
  3649. strict = os_strchr(field, ' ');
  3650. if (strict != NULL) {
  3651. *strict++ = '\0';
  3652. if (os_strcmp(strict, "strict") != 0)
  3653. return -1;
  3654. }
  3655. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  3656. field, strict ? strict : "");
  3657. if (os_strcmp(field, "eap") == 0) {
  3658. return eap_get_names(buf, buflen);
  3659. }
  3660. res = wpa_drv_get_capa(wpa_s, &capa);
  3661. if (os_strcmp(field, "pairwise") == 0)
  3662. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  3663. buf, buflen);
  3664. if (os_strcmp(field, "group") == 0)
  3665. return ctrl_iface_get_capability_group(res, strict, &capa,
  3666. buf, buflen);
  3667. if (os_strcmp(field, "group_mgmt") == 0)
  3668. return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
  3669. buf, buflen);
  3670. if (os_strcmp(field, "key_mgmt") == 0)
  3671. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  3672. buf, buflen);
  3673. if (os_strcmp(field, "proto") == 0)
  3674. return ctrl_iface_get_capability_proto(res, strict, &capa,
  3675. buf, buflen);
  3676. if (os_strcmp(field, "auth_alg") == 0)
  3677. return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
  3678. &capa, buf, buflen);
  3679. if (os_strcmp(field, "modes") == 0)
  3680. return ctrl_iface_get_capability_modes(res, strict, &capa,
  3681. buf, buflen);
  3682. if (os_strcmp(field, "channels") == 0)
  3683. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  3684. if (os_strcmp(field, "freq") == 0)
  3685. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  3686. #ifdef CONFIG_TDLS
  3687. if (os_strcmp(field, "tdls") == 0)
  3688. return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
  3689. #endif /* CONFIG_TDLS */
  3690. #ifdef CONFIG_ERP
  3691. if (os_strcmp(field, "erp") == 0) {
  3692. res = os_snprintf(buf, buflen, "ERP");
  3693. if (os_snprintf_error(buflen, res))
  3694. return -1;
  3695. return res;
  3696. }
  3697. #endif /* CONFIG_EPR */
  3698. #ifdef CONFIG_FIPS
  3699. if (os_strcmp(field, "fips") == 0) {
  3700. res = os_snprintf(buf, buflen, "FIPS");
  3701. if (os_snprintf_error(buflen, res))
  3702. return -1;
  3703. return res;
  3704. }
  3705. #endif /* CONFIG_FIPS */
  3706. #ifdef CONFIG_ACS
  3707. if (os_strcmp(field, "acs") == 0) {
  3708. res = os_snprintf(buf, buflen, "ACS");
  3709. if (os_snprintf_error(buflen, res))
  3710. return -1;
  3711. return res;
  3712. }
  3713. #endif /* CONFIG_ACS */
  3714. #ifdef CONFIG_FILS
  3715. if (os_strcmp(field, "fils") == 0) {
  3716. #ifdef CONFIG_FILS_SK_PFS
  3717. if (wpa_is_fils_supported(wpa_s) &&
  3718. wpa_is_fils_sk_pfs_supported(wpa_s)) {
  3719. res = os_snprintf(buf, buflen, "FILS FILS-SK-PFS");
  3720. if (os_snprintf_error(buflen, res))
  3721. return -1;
  3722. return res;
  3723. }
  3724. #endif /* CONFIG_FILS_SK_PFS */
  3725. if (wpa_is_fils_supported(wpa_s)) {
  3726. res = os_snprintf(buf, buflen, "FILS");
  3727. if (os_snprintf_error(buflen, res))
  3728. return -1;
  3729. return res;
  3730. }
  3731. }
  3732. #endif /* CONFIG_FILS */
  3733. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  3734. field);
  3735. return -1;
  3736. }
  3737. #ifdef CONFIG_INTERWORKING
  3738. static char * anqp_add_hex(char *pos, char *end, const char *title,
  3739. struct wpabuf *data)
  3740. {
  3741. char *start = pos;
  3742. size_t i;
  3743. int ret;
  3744. const u8 *d;
  3745. if (data == NULL)
  3746. return start;
  3747. ret = os_snprintf(pos, end - pos, "%s=", title);
  3748. if (os_snprintf_error(end - pos, ret))
  3749. return start;
  3750. pos += ret;
  3751. d = wpabuf_head_u8(data);
  3752. for (i = 0; i < wpabuf_len(data); i++) {
  3753. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  3754. if (os_snprintf_error(end - pos, ret))
  3755. return start;
  3756. pos += ret;
  3757. }
  3758. ret = os_snprintf(pos, end - pos, "\n");
  3759. if (os_snprintf_error(end - pos, ret))
  3760. return start;
  3761. pos += ret;
  3762. return pos;
  3763. }
  3764. #endif /* CONFIG_INTERWORKING */
  3765. #ifdef CONFIG_FILS
  3766. static int print_fils_indication(struct wpa_bss *bss, char *pos, char *end)
  3767. {
  3768. char *start = pos;
  3769. const u8 *ie, *ie_end;
  3770. u16 info, realms;
  3771. int ret;
  3772. ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
  3773. if (!ie)
  3774. return 0;
  3775. ie_end = ie + 2 + ie[1];
  3776. ie += 2;
  3777. if (ie_end - ie < 2)
  3778. return -1;
  3779. info = WPA_GET_LE16(ie);
  3780. ie += 2;
  3781. ret = os_snprintf(pos, end - pos, "fils_info=%04x\n", info);
  3782. if (os_snprintf_error(end - pos, ret))
  3783. return 0;
  3784. pos += ret;
  3785. if (info & BIT(7)) {
  3786. /* Cache Identifier Included */
  3787. if (ie_end - ie < 2)
  3788. return -1;
  3789. ret = os_snprintf(pos, end - pos, "fils_cache_id=%02x%02x\n",
  3790. ie[0], ie[1]);
  3791. if (os_snprintf_error(end - pos, ret))
  3792. return 0;
  3793. pos += ret;
  3794. ie += 2;
  3795. }
  3796. if (info & BIT(8)) {
  3797. /* HESSID Included */
  3798. if (ie_end - ie < ETH_ALEN)
  3799. return -1;
  3800. ret = os_snprintf(pos, end - pos, "fils_hessid=" MACSTR "\n",
  3801. MAC2STR(ie));
  3802. if (os_snprintf_error(end - pos, ret))
  3803. return 0;
  3804. pos += ret;
  3805. ie += ETH_ALEN;
  3806. }
  3807. realms = (info & (BIT(3) | BIT(4) | BIT(5))) >> 3;
  3808. if (realms) {
  3809. if (ie_end - ie < realms * 2)
  3810. return -1;
  3811. ret = os_snprintf(pos, end - pos, "fils_realms=");
  3812. if (os_snprintf_error(end - pos, ret))
  3813. return 0;
  3814. pos += ret;
  3815. ret = wpa_snprintf_hex(pos, end - pos, ie, realms * 2);
  3816. if (ret <= 0)
  3817. return 0;
  3818. pos += ret;
  3819. ie += realms * 2;
  3820. ret = os_snprintf(pos, end - pos, "\n");
  3821. if (os_snprintf_error(end - pos, ret))
  3822. return 0;
  3823. pos += ret;
  3824. }
  3825. return pos - start;
  3826. }
  3827. #endif /* CONFIG_FILS */
  3828. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  3829. unsigned long mask, char *buf, size_t buflen)
  3830. {
  3831. size_t i;
  3832. int ret;
  3833. char *pos, *end;
  3834. const u8 *ie, *ie2, *osen_ie, *mesh, *owe;
  3835. pos = buf;
  3836. end = buf + buflen;
  3837. if (mask & WPA_BSS_MASK_ID) {
  3838. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  3839. if (os_snprintf_error(end - pos, ret))
  3840. return 0;
  3841. pos += ret;
  3842. }
  3843. if (mask & WPA_BSS_MASK_BSSID) {
  3844. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  3845. MAC2STR(bss->bssid));
  3846. if (os_snprintf_error(end - pos, ret))
  3847. return 0;
  3848. pos += ret;
  3849. }
  3850. if (mask & WPA_BSS_MASK_FREQ) {
  3851. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  3852. if (os_snprintf_error(end - pos, ret))
  3853. return 0;
  3854. pos += ret;
  3855. }
  3856. if (mask & WPA_BSS_MASK_BEACON_INT) {
  3857. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  3858. bss->beacon_int);
  3859. if (os_snprintf_error(end - pos, ret))
  3860. return 0;
  3861. pos += ret;
  3862. }
  3863. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  3864. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  3865. bss->caps);
  3866. if (os_snprintf_error(end - pos, ret))
  3867. return 0;
  3868. pos += ret;
  3869. }
  3870. if (mask & WPA_BSS_MASK_QUAL) {
  3871. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  3872. if (os_snprintf_error(end - pos, ret))
  3873. return 0;
  3874. pos += ret;
  3875. }
  3876. if (mask & WPA_BSS_MASK_NOISE) {
  3877. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  3878. if (os_snprintf_error(end - pos, ret))
  3879. return 0;
  3880. pos += ret;
  3881. }
  3882. if (mask & WPA_BSS_MASK_LEVEL) {
  3883. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  3884. if (os_snprintf_error(end - pos, ret))
  3885. return 0;
  3886. pos += ret;
  3887. }
  3888. if (mask & WPA_BSS_MASK_TSF) {
  3889. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  3890. (unsigned long long) bss->tsf);
  3891. if (os_snprintf_error(end - pos, ret))
  3892. return 0;
  3893. pos += ret;
  3894. }
  3895. if (mask & WPA_BSS_MASK_AGE) {
  3896. struct os_reltime now;
  3897. os_get_reltime(&now);
  3898. ret = os_snprintf(pos, end - pos, "age=%d\n",
  3899. (int) (now.sec - bss->last_update.sec));
  3900. if (os_snprintf_error(end - pos, ret))
  3901. return 0;
  3902. pos += ret;
  3903. }
  3904. if (mask & WPA_BSS_MASK_IE) {
  3905. ret = os_snprintf(pos, end - pos, "ie=");
  3906. if (os_snprintf_error(end - pos, ret))
  3907. return 0;
  3908. pos += ret;
  3909. ie = (const u8 *) (bss + 1);
  3910. for (i = 0; i < bss->ie_len; i++) {
  3911. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  3912. if (os_snprintf_error(end - pos, ret))
  3913. return 0;
  3914. pos += ret;
  3915. }
  3916. ret = os_snprintf(pos, end - pos, "\n");
  3917. if (os_snprintf_error(end - pos, ret))
  3918. return 0;
  3919. pos += ret;
  3920. }
  3921. if (mask & WPA_BSS_MASK_FLAGS) {
  3922. ret = os_snprintf(pos, end - pos, "flags=");
  3923. if (os_snprintf_error(end - pos, ret))
  3924. return 0;
  3925. pos += ret;
  3926. mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
  3927. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  3928. if (ie)
  3929. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  3930. 2 + ie[1]);
  3931. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  3932. if (ie2)
  3933. pos = wpa_supplicant_ie_txt(pos, end,
  3934. mesh ? "RSN" : "WPA2", ie2,
  3935. 2 + ie2[1]);
  3936. osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  3937. if (osen_ie)
  3938. pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
  3939. osen_ie, 2 + osen_ie[1]);
  3940. owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
  3941. if (owe) {
  3942. ret = os_snprintf(
  3943. pos, end - pos,
  3944. ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
  3945. if (os_snprintf_error(end - pos, ret))
  3946. return 0;
  3947. pos += ret;
  3948. }
  3949. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  3950. if (!ie && !ie2 && !osen_ie &&
  3951. (bss->caps & IEEE80211_CAP_PRIVACY)) {
  3952. ret = os_snprintf(pos, end - pos, "[WEP]");
  3953. if (os_snprintf_error(end - pos, ret))
  3954. return 0;
  3955. pos += ret;
  3956. }
  3957. if (mesh) {
  3958. ret = os_snprintf(pos, end - pos, "[MESH]");
  3959. if (os_snprintf_error(end - pos, ret))
  3960. return 0;
  3961. pos += ret;
  3962. }
  3963. if (bss_is_dmg(bss)) {
  3964. const char *s;
  3965. ret = os_snprintf(pos, end - pos, "[DMG]");
  3966. if (os_snprintf_error(end - pos, ret))
  3967. return 0;
  3968. pos += ret;
  3969. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  3970. case IEEE80211_CAP_DMG_IBSS:
  3971. s = "[IBSS]";
  3972. break;
  3973. case IEEE80211_CAP_DMG_AP:
  3974. s = "[ESS]";
  3975. break;
  3976. case IEEE80211_CAP_DMG_PBSS:
  3977. s = "[PBSS]";
  3978. break;
  3979. default:
  3980. s = "";
  3981. break;
  3982. }
  3983. ret = os_snprintf(pos, end - pos, "%s", s);
  3984. if (os_snprintf_error(end - pos, ret))
  3985. return 0;
  3986. pos += ret;
  3987. } else {
  3988. if (bss->caps & IEEE80211_CAP_IBSS) {
  3989. ret = os_snprintf(pos, end - pos, "[IBSS]");
  3990. if (os_snprintf_error(end - pos, ret))
  3991. return 0;
  3992. pos += ret;
  3993. }
  3994. if (bss->caps & IEEE80211_CAP_ESS) {
  3995. ret = os_snprintf(pos, end - pos, "[ESS]");
  3996. if (os_snprintf_error(end - pos, ret))
  3997. return 0;
  3998. pos += ret;
  3999. }
  4000. }
  4001. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  4002. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  4003. ret = os_snprintf(pos, end - pos, "[P2P]");
  4004. if (os_snprintf_error(end - pos, ret))
  4005. return 0;
  4006. pos += ret;
  4007. }
  4008. #ifdef CONFIG_HS20
  4009. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  4010. ret = os_snprintf(pos, end - pos, "[HS20]");
  4011. if (os_snprintf_error(end - pos, ret))
  4012. return 0;
  4013. pos += ret;
  4014. }
  4015. #endif /* CONFIG_HS20 */
  4016. #ifdef CONFIG_FILS
  4017. if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
  4018. ret = os_snprintf(pos, end - pos, "[FILS]");
  4019. if (os_snprintf_error(end - pos, ret))
  4020. return 0;
  4021. pos += ret;
  4022. }
  4023. #endif /* CONFIG_FILS */
  4024. ret = os_snprintf(pos, end - pos, "\n");
  4025. if (os_snprintf_error(end - pos, ret))
  4026. return 0;
  4027. pos += ret;
  4028. }
  4029. if (mask & WPA_BSS_MASK_SSID) {
  4030. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  4031. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  4032. if (os_snprintf_error(end - pos, ret))
  4033. return 0;
  4034. pos += ret;
  4035. }
  4036. #ifdef CONFIG_WPS
  4037. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  4038. ie = (const u8 *) (bss + 1);
  4039. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  4040. if (ret >= end - pos)
  4041. return 0;
  4042. if (ret > 0)
  4043. pos += ret;
  4044. }
  4045. #endif /* CONFIG_WPS */
  4046. #ifdef CONFIG_P2P
  4047. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  4048. ie = (const u8 *) (bss + 1);
  4049. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  4050. if (ret >= end - pos)
  4051. return 0;
  4052. if (ret > 0)
  4053. pos += ret;
  4054. }
  4055. #endif /* CONFIG_P2P */
  4056. #ifdef CONFIG_WIFI_DISPLAY
  4057. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  4058. struct wpabuf *wfd;
  4059. ie = (const u8 *) (bss + 1);
  4060. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  4061. WFD_IE_VENDOR_TYPE);
  4062. if (wfd) {
  4063. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  4064. if (os_snprintf_error(end - pos, ret)) {
  4065. wpabuf_free(wfd);
  4066. return 0;
  4067. }
  4068. pos += ret;
  4069. pos += wpa_snprintf_hex(pos, end - pos,
  4070. wpabuf_head(wfd),
  4071. wpabuf_len(wfd));
  4072. wpabuf_free(wfd);
  4073. ret = os_snprintf(pos, end - pos, "\n");
  4074. if (os_snprintf_error(end - pos, ret))
  4075. return 0;
  4076. pos += ret;
  4077. }
  4078. }
  4079. #endif /* CONFIG_WIFI_DISPLAY */
  4080. #ifdef CONFIG_INTERWORKING
  4081. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  4082. struct wpa_bss_anqp *anqp = bss->anqp;
  4083. struct wpa_bss_anqp_elem *elem;
  4084. pos = anqp_add_hex(pos, end, "anqp_capability_list",
  4085. anqp->capability_list);
  4086. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  4087. anqp->venue_name);
  4088. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  4089. anqp->network_auth_type);
  4090. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  4091. anqp->roaming_consortium);
  4092. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  4093. anqp->ip_addr_type_availability);
  4094. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  4095. anqp->nai_realm);
  4096. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  4097. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  4098. anqp->domain_name);
  4099. pos = anqp_add_hex(pos, end, "anqp_fils_realm_info",
  4100. anqp->fils_realm_info);
  4101. #ifdef CONFIG_HS20
  4102. pos = anqp_add_hex(pos, end, "hs20_capability_list",
  4103. anqp->hs20_capability_list);
  4104. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  4105. anqp->hs20_operator_friendly_name);
  4106. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  4107. anqp->hs20_wan_metrics);
  4108. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  4109. anqp->hs20_connection_capability);
  4110. pos = anqp_add_hex(pos, end, "hs20_operating_class",
  4111. anqp->hs20_operating_class);
  4112. pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
  4113. anqp->hs20_osu_providers_list);
  4114. #endif /* CONFIG_HS20 */
  4115. dl_list_for_each(elem, &anqp->anqp_elems,
  4116. struct wpa_bss_anqp_elem, list) {
  4117. char title[20];
  4118. os_snprintf(title, sizeof(title), "anqp[%u]",
  4119. elem->infoid);
  4120. pos = anqp_add_hex(pos, end, title, elem->payload);
  4121. }
  4122. }
  4123. #endif /* CONFIG_INTERWORKING */
  4124. #ifdef CONFIG_MESH
  4125. if (mask & WPA_BSS_MASK_MESH_SCAN) {
  4126. ie = (const u8 *) (bss + 1);
  4127. ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
  4128. if (ret >= end - pos)
  4129. return 0;
  4130. if (ret > 0)
  4131. pos += ret;
  4132. }
  4133. #endif /* CONFIG_MESH */
  4134. if (mask & WPA_BSS_MASK_SNR) {
  4135. ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
  4136. if (os_snprintf_error(end - pos, ret))
  4137. return 0;
  4138. pos += ret;
  4139. }
  4140. if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
  4141. ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
  4142. bss->est_throughput);
  4143. if (os_snprintf_error(end - pos, ret))
  4144. return 0;
  4145. pos += ret;
  4146. }
  4147. #ifdef CONFIG_FST
  4148. if (mask & WPA_BSS_MASK_FST) {
  4149. ret = fst_ctrl_iface_mb_info(bss->bssid, pos, end - pos);
  4150. if (ret < 0 || ret >= end - pos)
  4151. return 0;
  4152. pos += ret;
  4153. }
  4154. #endif /* CONFIG_FST */
  4155. if (mask & WPA_BSS_MASK_UPDATE_IDX) {
  4156. ret = os_snprintf(pos, end - pos, "update_idx=%u\n",
  4157. bss->last_update_idx);
  4158. if (os_snprintf_error(end - pos, ret))
  4159. return 0;
  4160. pos += ret;
  4161. }
  4162. if ((mask & WPA_BSS_MASK_BEACON_IE) && bss->beacon_ie_len) {
  4163. ret = os_snprintf(pos, end - pos, "beacon_ie=");
  4164. if (os_snprintf_error(end - pos, ret))
  4165. return 0;
  4166. pos += ret;
  4167. ie = (const u8 *) (bss + 1);
  4168. ie += bss->ie_len;
  4169. for (i = 0; i < bss->beacon_ie_len; i++) {
  4170. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  4171. if (os_snprintf_error(end - pos, ret))
  4172. return 0;
  4173. pos += ret;
  4174. }
  4175. ret = os_snprintf(pos, end - pos, "\n");
  4176. if (os_snprintf_error(end - pos, ret))
  4177. return 0;
  4178. pos += ret;
  4179. }
  4180. #ifdef CONFIG_FILS
  4181. if (mask & WPA_BSS_MASK_FILS_INDICATION) {
  4182. ret = print_fils_indication(bss, pos, end);
  4183. if (ret < 0)
  4184. return 0;
  4185. pos += ret;
  4186. }
  4187. #endif /* CONFIG_FILS */
  4188. if (mask & WPA_BSS_MASK_DELIM) {
  4189. ret = os_snprintf(pos, end - pos, "====\n");
  4190. if (os_snprintf_error(end - pos, ret))
  4191. return 0;
  4192. pos += ret;
  4193. }
  4194. return pos - buf;
  4195. }
  4196. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  4197. const char *cmd, char *buf,
  4198. size_t buflen)
  4199. {
  4200. u8 bssid[ETH_ALEN];
  4201. size_t i;
  4202. struct wpa_bss *bss;
  4203. struct wpa_bss *bsslast = NULL;
  4204. struct dl_list *next;
  4205. int ret = 0;
  4206. int len;
  4207. char *ctmp, *end = buf + buflen;
  4208. unsigned long mask = WPA_BSS_MASK_ALL;
  4209. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  4210. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  4211. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  4212. list_id);
  4213. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  4214. list_id);
  4215. } else { /* N1-N2 */
  4216. unsigned int id1, id2;
  4217. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  4218. wpa_printf(MSG_INFO, "Wrong BSS range "
  4219. "format");
  4220. return 0;
  4221. }
  4222. if (*(cmd + 6) == '-')
  4223. id1 = 0;
  4224. else
  4225. id1 = atoi(cmd + 6);
  4226. ctmp++;
  4227. if (*ctmp >= '0' && *ctmp <= '9')
  4228. id2 = atoi(ctmp);
  4229. else
  4230. id2 = (unsigned int) -1;
  4231. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  4232. if (id2 == (unsigned int) -1)
  4233. bsslast = dl_list_last(&wpa_s->bss_id,
  4234. struct wpa_bss,
  4235. list_id);
  4236. else {
  4237. bsslast = wpa_bss_get_id(wpa_s, id2);
  4238. if (bsslast == NULL && bss && id2 > id1) {
  4239. struct wpa_bss *tmp = bss;
  4240. for (;;) {
  4241. next = tmp->list_id.next;
  4242. if (next == &wpa_s->bss_id)
  4243. break;
  4244. tmp = dl_list_entry(
  4245. next, struct wpa_bss,
  4246. list_id);
  4247. if (tmp->id > id2)
  4248. break;
  4249. bsslast = tmp;
  4250. }
  4251. }
  4252. }
  4253. }
  4254. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  4255. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  4256. else if (os_strncmp(cmd, "LAST", 4) == 0)
  4257. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  4258. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  4259. i = atoi(cmd + 3);
  4260. bss = wpa_bss_get_id(wpa_s, i);
  4261. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  4262. i = atoi(cmd + 5);
  4263. bss = wpa_bss_get_id(wpa_s, i);
  4264. if (bss) {
  4265. next = bss->list_id.next;
  4266. if (next == &wpa_s->bss_id)
  4267. bss = NULL;
  4268. else
  4269. bss = dl_list_entry(next, struct wpa_bss,
  4270. list_id);
  4271. }
  4272. } else if (os_strncmp(cmd, "CURRENT", 7) == 0) {
  4273. bss = wpa_s->current_bss;
  4274. #ifdef CONFIG_P2P
  4275. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  4276. if (hwaddr_aton(cmd + 13, bssid) == 0)
  4277. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  4278. else
  4279. bss = NULL;
  4280. #endif /* CONFIG_P2P */
  4281. } else if (hwaddr_aton(cmd, bssid) == 0)
  4282. bss = wpa_bss_get_bssid(wpa_s, bssid);
  4283. else {
  4284. struct wpa_bss *tmp;
  4285. i = atoi(cmd);
  4286. bss = NULL;
  4287. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  4288. {
  4289. if (i-- == 0) {
  4290. bss = tmp;
  4291. break;
  4292. }
  4293. }
  4294. }
  4295. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  4296. mask = strtoul(ctmp + 5, NULL, 0x10);
  4297. if (mask == 0)
  4298. mask = WPA_BSS_MASK_ALL;
  4299. }
  4300. if (bss == NULL)
  4301. return 0;
  4302. if (bsslast == NULL)
  4303. bsslast = bss;
  4304. do {
  4305. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  4306. ret += len;
  4307. buf += len;
  4308. buflen -= len;
  4309. if (bss == bsslast) {
  4310. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  4311. (bss == dl_list_last(&wpa_s->bss_id,
  4312. struct wpa_bss, list_id))) {
  4313. int res;
  4314. res = os_snprintf(buf - 5, end - buf + 5,
  4315. "####\n");
  4316. if (os_snprintf_error(end - buf + 5, res)) {
  4317. wpa_printf(MSG_DEBUG,
  4318. "Could not add end delim");
  4319. }
  4320. }
  4321. break;
  4322. }
  4323. next = bss->list_id.next;
  4324. if (next == &wpa_s->bss_id)
  4325. break;
  4326. bss = dl_list_entry(next, struct wpa_bss, list_id);
  4327. } while (bss && len);
  4328. return ret;
  4329. }
  4330. static int wpa_supplicant_ctrl_iface_ap_scan(
  4331. struct wpa_supplicant *wpa_s, char *cmd)
  4332. {
  4333. int ap_scan = atoi(cmd);
  4334. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  4335. }
  4336. static int wpa_supplicant_ctrl_iface_scan_interval(
  4337. struct wpa_supplicant *wpa_s, char *cmd)
  4338. {
  4339. int scan_int = atoi(cmd);
  4340. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  4341. }
  4342. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  4343. struct wpa_supplicant *wpa_s, char *cmd)
  4344. {
  4345. int expire_age = atoi(cmd);
  4346. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  4347. }
  4348. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  4349. struct wpa_supplicant *wpa_s, char *cmd)
  4350. {
  4351. int expire_count = atoi(cmd);
  4352. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  4353. }
  4354. static void wpa_supplicant_ctrl_iface_bss_flush(
  4355. struct wpa_supplicant *wpa_s, char *cmd)
  4356. {
  4357. int flush_age = atoi(cmd);
  4358. if (flush_age == 0)
  4359. wpa_bss_flush(wpa_s);
  4360. else
  4361. wpa_bss_flush_by_age(wpa_s, flush_age);
  4362. }
  4363. #ifdef CONFIG_TESTING_OPTIONS
  4364. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  4365. {
  4366. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  4367. /* MLME-DELETEKEYS.request */
  4368. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  4369. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  4370. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  4371. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  4372. #ifdef CONFIG_IEEE80211W
  4373. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  4374. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  4375. #endif /* CONFIG_IEEE80211W */
  4376. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  4377. 0);
  4378. /* MLME-SETPROTECTION.request(None) */
  4379. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  4380. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  4381. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  4382. wpa_sm_drop_sa(wpa_s->wpa);
  4383. }
  4384. #endif /* CONFIG_TESTING_OPTIONS */
  4385. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  4386. char *addr)
  4387. {
  4388. #ifdef CONFIG_NO_SCAN_PROCESSING
  4389. return -1;
  4390. #else /* CONFIG_NO_SCAN_PROCESSING */
  4391. u8 bssid[ETH_ALEN];
  4392. struct wpa_bss *bss;
  4393. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4394. if (hwaddr_aton(addr, bssid)) {
  4395. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  4396. "address '%s'", addr);
  4397. return -1;
  4398. }
  4399. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  4400. if (!ssid) {
  4401. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  4402. "configuration known for the target AP");
  4403. return -1;
  4404. }
  4405. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  4406. if (!bss) {
  4407. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  4408. "from BSS table");
  4409. return -1;
  4410. }
  4411. /*
  4412. * TODO: Find best network configuration block from configuration to
  4413. * allow roaming to other networks
  4414. */
  4415. wpa_s->reassociate = 1;
  4416. wpa_supplicant_connect(wpa_s, bss, ssid);
  4417. return 0;
  4418. #endif /* CONFIG_NO_SCAN_PROCESSING */
  4419. }
  4420. #ifdef CONFIG_P2P
  4421. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  4422. {
  4423. unsigned int timeout = atoi(cmd);
  4424. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  4425. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  4426. u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
  4427. char *pos;
  4428. unsigned int search_delay;
  4429. const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
  4430. u8 seek_count = 0;
  4431. int freq = 0;
  4432. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  4433. wpa_dbg(wpa_s, MSG_INFO,
  4434. "Reject P2P_FIND since interface is disabled");
  4435. return -1;
  4436. }
  4437. if (os_strstr(cmd, "type=social"))
  4438. type = P2P_FIND_ONLY_SOCIAL;
  4439. else if (os_strstr(cmd, "type=progressive"))
  4440. type = P2P_FIND_PROGRESSIVE;
  4441. pos = os_strstr(cmd, "dev_id=");
  4442. if (pos) {
  4443. pos += 7;
  4444. if (hwaddr_aton(pos, dev_id))
  4445. return -1;
  4446. _dev_id = dev_id;
  4447. }
  4448. pos = os_strstr(cmd, "dev_type=");
  4449. if (pos) {
  4450. pos += 9;
  4451. if (wps_dev_type_str2bin(pos, dev_type) < 0)
  4452. return -1;
  4453. _dev_type = dev_type;
  4454. }
  4455. pos = os_strstr(cmd, "delay=");
  4456. if (pos) {
  4457. pos += 6;
  4458. search_delay = atoi(pos);
  4459. } else
  4460. search_delay = wpas_p2p_search_delay(wpa_s);
  4461. pos = os_strstr(cmd, "freq=");
  4462. if (pos) {
  4463. pos += 5;
  4464. freq = atoi(pos);
  4465. if (freq <= 0)
  4466. return -1;
  4467. }
  4468. /* Must be searched for last, because it adds nul termination */
  4469. pos = os_strstr(cmd, " seek=");
  4470. if (pos)
  4471. pos += 6;
  4472. while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
  4473. char *term;
  4474. _seek[seek_count++] = pos;
  4475. seek = _seek;
  4476. term = os_strchr(pos, ' ');
  4477. if (!term)
  4478. break;
  4479. *term = '\0';
  4480. pos = os_strstr(term + 1, "seek=");
  4481. if (pos)
  4482. pos += 5;
  4483. }
  4484. if (seek_count > P2P_MAX_QUERY_HASH) {
  4485. seek[0] = NULL;
  4486. seek_count = 1;
  4487. }
  4488. return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
  4489. _dev_id, search_delay, seek_count, seek, freq);
  4490. }
  4491. static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
  4492. {
  4493. const char *last = NULL;
  4494. const char *token;
  4495. long int token_len;
  4496. unsigned int i;
  4497. /* Expected predefined CPT names delimited by ':' */
  4498. for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
  4499. if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
  4500. wpa_printf(MSG_ERROR,
  4501. "P2PS: CPT name list is too long, expected up to %d names",
  4502. P2PS_FEATURE_CAPAB_CPT_MAX);
  4503. cpt[0] = 0;
  4504. return -1;
  4505. }
  4506. token_len = last - token;
  4507. if (token_len == 3 &&
  4508. os_memcmp(token, "UDP", token_len) == 0) {
  4509. cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
  4510. } else if (token_len == 3 &&
  4511. os_memcmp(token, "MAC", token_len) == 0) {
  4512. cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
  4513. } else {
  4514. wpa_printf(MSG_ERROR,
  4515. "P2PS: Unsupported CPT name '%s'", token);
  4516. cpt[0] = 0;
  4517. return -1;
  4518. }
  4519. if (isblank((unsigned char) *last)) {
  4520. i++;
  4521. break;
  4522. }
  4523. }
  4524. cpt[i] = 0;
  4525. return 0;
  4526. }
  4527. static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
  4528. {
  4529. struct p2ps_provision *p2ps_prov;
  4530. char *pos;
  4531. size_t info_len = 0;
  4532. char *info = NULL;
  4533. u8 role = P2PS_SETUP_NONE;
  4534. long long unsigned val;
  4535. int i;
  4536. pos = os_strstr(cmd, "info=");
  4537. if (pos) {
  4538. pos += 5;
  4539. info_len = os_strlen(pos);
  4540. if (info_len) {
  4541. info = os_malloc(info_len + 1);
  4542. if (info) {
  4543. info_len = utf8_unescape(pos, info_len,
  4544. info, info_len + 1);
  4545. } else
  4546. info_len = 0;
  4547. }
  4548. }
  4549. p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
  4550. if (p2ps_prov == NULL) {
  4551. os_free(info);
  4552. return NULL;
  4553. }
  4554. if (info) {
  4555. os_memcpy(p2ps_prov->info, info, info_len);
  4556. p2ps_prov->info[info_len] = '\0';
  4557. os_free(info);
  4558. }
  4559. pos = os_strstr(cmd, "status=");
  4560. if (pos)
  4561. p2ps_prov->status = atoi(pos + 7);
  4562. else
  4563. p2ps_prov->status = -1;
  4564. pos = os_strstr(cmd, "adv_id=");
  4565. if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
  4566. goto invalid_args;
  4567. p2ps_prov->adv_id = val;
  4568. pos = os_strstr(cmd, "method=");
  4569. if (pos)
  4570. p2ps_prov->method = strtol(pos + 7, NULL, 16);
  4571. else
  4572. p2ps_prov->method = 0;
  4573. pos = os_strstr(cmd, "session=");
  4574. if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
  4575. goto invalid_args;
  4576. p2ps_prov->session_id = val;
  4577. pos = os_strstr(cmd, "adv_mac=");
  4578. if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
  4579. goto invalid_args;
  4580. pos = os_strstr(cmd, "session_mac=");
  4581. if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
  4582. goto invalid_args;
  4583. pos = os_strstr(cmd, "cpt=");
  4584. if (pos) {
  4585. if (p2ps_ctrl_parse_cpt_priority(pos + 4,
  4586. p2ps_prov->cpt_priority))
  4587. goto invalid_args;
  4588. } else {
  4589. p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
  4590. }
  4591. for (i = 0; p2ps_prov->cpt_priority[i]; i++)
  4592. p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
  4593. /* force conncap with tstCap (no sanity checks) */
  4594. pos = os_strstr(cmd, "tstCap=");
  4595. if (pos) {
  4596. role = strtol(pos + 7, NULL, 16);
  4597. } else {
  4598. pos = os_strstr(cmd, "role=");
  4599. if (pos) {
  4600. role = strtol(pos + 5, NULL, 16);
  4601. if (role != P2PS_SETUP_CLIENT &&
  4602. role != P2PS_SETUP_GROUP_OWNER)
  4603. role = P2PS_SETUP_NONE;
  4604. }
  4605. }
  4606. p2ps_prov->role = role;
  4607. return p2ps_prov;
  4608. invalid_args:
  4609. os_free(p2ps_prov);
  4610. return NULL;
  4611. }
  4612. static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
  4613. {
  4614. u8 addr[ETH_ALEN];
  4615. struct p2ps_provision *p2ps_prov;
  4616. char *pos;
  4617. /* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
  4618. wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
  4619. if (hwaddr_aton(cmd, addr))
  4620. return -1;
  4621. pos = cmd + 17;
  4622. if (*pos != ' ')
  4623. return -1;
  4624. p2ps_prov = p2p_parse_asp_provision_cmd(pos);
  4625. if (!p2ps_prov)
  4626. return -1;
  4627. if (p2ps_prov->status < 0) {
  4628. os_free(p2ps_prov);
  4629. return -1;
  4630. }
  4631. return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
  4632. p2ps_prov);
  4633. }
  4634. static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
  4635. {
  4636. u8 addr[ETH_ALEN];
  4637. struct p2ps_provision *p2ps_prov;
  4638. char *pos;
  4639. /* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
  4640. * session=<ses_id> mac=<ses_mac> [info=<infodata>]
  4641. */
  4642. wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
  4643. if (hwaddr_aton(cmd, addr))
  4644. return -1;
  4645. pos = cmd + 17;
  4646. if (*pos != ' ')
  4647. return -1;
  4648. p2ps_prov = p2p_parse_asp_provision_cmd(pos);
  4649. if (!p2ps_prov)
  4650. return -1;
  4651. p2ps_prov->pd_seeker = 1;
  4652. return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
  4653. p2ps_prov);
  4654. }
  4655. static int parse_freq(int chwidth, int freq2)
  4656. {
  4657. if (freq2 < 0)
  4658. return -1;
  4659. if (freq2)
  4660. return VHT_CHANWIDTH_80P80MHZ;
  4661. switch (chwidth) {
  4662. case 0:
  4663. case 20:
  4664. case 40:
  4665. return VHT_CHANWIDTH_USE_HT;
  4666. case 80:
  4667. return VHT_CHANWIDTH_80MHZ;
  4668. case 160:
  4669. return VHT_CHANWIDTH_160MHZ;
  4670. default:
  4671. wpa_printf(MSG_DEBUG, "Unknown max oper bandwidth: %d",
  4672. chwidth);
  4673. return -1;
  4674. }
  4675. }
  4676. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  4677. char *buf, size_t buflen)
  4678. {
  4679. u8 addr[ETH_ALEN];
  4680. char *pos, *pos2;
  4681. char *pin = NULL;
  4682. enum p2p_wps_method wps_method;
  4683. int new_pin;
  4684. int ret;
  4685. int persistent_group, persistent_id = -1;
  4686. int join;
  4687. int auth;
  4688. int automatic;
  4689. int go_intent = -1;
  4690. int freq = 0;
  4691. int pd;
  4692. int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
  4693. u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
  4694. size_t group_ssid_len = 0;
  4695. if (!wpa_s->global->p2p_init_wpa_s)
  4696. return -1;
  4697. if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
  4698. wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
  4699. wpa_s->global->p2p_init_wpa_s->ifname);
  4700. wpa_s = wpa_s->global->p2p_init_wpa_s;
  4701. }
  4702. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad|p2ps]
  4703. * [persistent|persistent=<network id>]
  4704. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  4705. * [ht40] [vht] [auto] [ssid=<hexdump>] */
  4706. if (hwaddr_aton(cmd, addr))
  4707. return -1;
  4708. pos = cmd + 17;
  4709. if (*pos != ' ')
  4710. return -1;
  4711. pos++;
  4712. persistent_group = os_strstr(pos, " persistent") != NULL;
  4713. pos2 = os_strstr(pos, " persistent=");
  4714. if (pos2) {
  4715. struct wpa_ssid *ssid;
  4716. persistent_id = atoi(pos2 + 12);
  4717. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  4718. if (ssid == NULL || ssid->disabled != 2 ||
  4719. ssid->mode != WPAS_MODE_P2P_GO) {
  4720. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  4721. "SSID id=%d for persistent P2P group (GO)",
  4722. persistent_id);
  4723. return -1;
  4724. }
  4725. }
  4726. join = os_strstr(pos, " join") != NULL;
  4727. auth = os_strstr(pos, " auth") != NULL;
  4728. automatic = os_strstr(pos, " auto") != NULL;
  4729. pd = os_strstr(pos, " provdisc") != NULL;
  4730. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  4731. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  4732. vht;
  4733. pos2 = os_strstr(pos, " go_intent=");
  4734. if (pos2) {
  4735. pos2 += 11;
  4736. go_intent = atoi(pos2);
  4737. if (go_intent < 0 || go_intent > 15)
  4738. return -1;
  4739. }
  4740. pos2 = os_strstr(pos, " freq=");
  4741. if (pos2) {
  4742. pos2 += 6;
  4743. freq = atoi(pos2);
  4744. if (freq <= 0)
  4745. return -1;
  4746. }
  4747. pos2 = os_strstr(pos, " freq2=");
  4748. if (pos2)
  4749. freq2 = atoi(pos2 + 7);
  4750. pos2 = os_strstr(pos, " max_oper_chwidth=");
  4751. if (pos2)
  4752. chwidth = atoi(pos2 + 18);
  4753. max_oper_chwidth = parse_freq(chwidth, freq2);
  4754. if (max_oper_chwidth < 0)
  4755. return -1;
  4756. pos2 = os_strstr(pos, " ssid=");
  4757. if (pos2) {
  4758. char *end;
  4759. pos2 += 6;
  4760. end = os_strchr(pos2, ' ');
  4761. if (!end)
  4762. group_ssid_len = os_strlen(pos2) / 2;
  4763. else
  4764. group_ssid_len = (end - pos2) / 2;
  4765. if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
  4766. hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
  4767. return -1;
  4768. group_ssid = _group_ssid;
  4769. }
  4770. if (os_strncmp(pos, "pin", 3) == 0) {
  4771. /* Request random PIN (to be displayed) and enable the PIN */
  4772. wps_method = WPS_PIN_DISPLAY;
  4773. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  4774. wps_method = WPS_PBC;
  4775. } else if (os_strstr(pos, "p2ps") != NULL) {
  4776. wps_method = WPS_P2PS;
  4777. } else {
  4778. pin = pos;
  4779. pos = os_strchr(pin, ' ');
  4780. wps_method = WPS_PIN_KEYPAD;
  4781. if (pos) {
  4782. *pos++ = '\0';
  4783. if (os_strncmp(pos, "display", 7) == 0)
  4784. wps_method = WPS_PIN_DISPLAY;
  4785. }
  4786. if (!wps_pin_str_valid(pin)) {
  4787. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  4788. return 17;
  4789. }
  4790. }
  4791. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  4792. persistent_group, automatic, join,
  4793. auth, go_intent, freq, freq2, persistent_id,
  4794. pd, ht40, vht, max_oper_chwidth,
  4795. group_ssid, group_ssid_len);
  4796. if (new_pin == -2) {
  4797. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  4798. return 25;
  4799. }
  4800. if (new_pin == -3) {
  4801. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  4802. return 25;
  4803. }
  4804. if (new_pin < 0)
  4805. return -1;
  4806. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  4807. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  4808. if (os_snprintf_error(buflen, ret))
  4809. return -1;
  4810. return ret;
  4811. }
  4812. os_memcpy(buf, "OK\n", 3);
  4813. return 3;
  4814. }
  4815. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  4816. {
  4817. unsigned int timeout = atoi(cmd);
  4818. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  4819. wpa_dbg(wpa_s, MSG_INFO,
  4820. "Reject P2P_LISTEN since interface is disabled");
  4821. return -1;
  4822. }
  4823. return wpas_p2p_listen(wpa_s, timeout);
  4824. }
  4825. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  4826. {
  4827. u8 addr[ETH_ALEN];
  4828. char *pos;
  4829. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  4830. /* <addr> <config method> [join|auto] */
  4831. if (hwaddr_aton(cmd, addr))
  4832. return -1;
  4833. pos = cmd + 17;
  4834. if (*pos != ' ')
  4835. return -1;
  4836. pos++;
  4837. if (os_strstr(pos, " join") != NULL)
  4838. use = WPAS_P2P_PD_FOR_JOIN;
  4839. else if (os_strstr(pos, " auto") != NULL)
  4840. use = WPAS_P2P_PD_AUTO;
  4841. return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
  4842. }
  4843. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  4844. size_t buflen)
  4845. {
  4846. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4847. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  4848. ssid->passphrase == NULL)
  4849. return -1;
  4850. os_strlcpy(buf, ssid->passphrase, buflen);
  4851. return os_strlen(buf);
  4852. }
  4853. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  4854. char *buf, size_t buflen)
  4855. {
  4856. u64 ref;
  4857. int res;
  4858. u8 dst_buf[ETH_ALEN], *dst;
  4859. struct wpabuf *tlvs;
  4860. char *pos;
  4861. size_t len;
  4862. if (hwaddr_aton(cmd, dst_buf))
  4863. return -1;
  4864. dst = dst_buf;
  4865. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  4866. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  4867. dst = NULL;
  4868. pos = cmd + 17;
  4869. if (*pos != ' ')
  4870. return -1;
  4871. pos++;
  4872. if (os_strncmp(pos, "upnp ", 5) == 0) {
  4873. u8 version;
  4874. pos += 5;
  4875. if (hexstr2bin(pos, &version, 1) < 0)
  4876. return -1;
  4877. pos += 2;
  4878. if (*pos != ' ')
  4879. return -1;
  4880. pos++;
  4881. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  4882. #ifdef CONFIG_WIFI_DISPLAY
  4883. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  4884. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  4885. #endif /* CONFIG_WIFI_DISPLAY */
  4886. } else if (os_strncmp(pos, "asp ", 4) == 0) {
  4887. char *svc_str;
  4888. char *svc_info = NULL;
  4889. u32 id;
  4890. pos += 4;
  4891. if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
  4892. return -1;
  4893. pos = os_strchr(pos, ' ');
  4894. if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
  4895. return -1;
  4896. svc_str = pos + 1;
  4897. pos = os_strchr(svc_str, ' ');
  4898. if (pos)
  4899. *pos++ = '\0';
  4900. /* All remaining data is the svc_info string */
  4901. if (pos && pos[0] && pos[0] != ' ') {
  4902. len = os_strlen(pos);
  4903. /* Unescape in place */
  4904. len = utf8_unescape(pos, len, pos, len);
  4905. if (len > 0xff)
  4906. return -1;
  4907. svc_info = pos;
  4908. }
  4909. ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
  4910. svc_str, svc_info);
  4911. } else {
  4912. len = os_strlen(pos);
  4913. if (len & 1)
  4914. return -1;
  4915. len /= 2;
  4916. tlvs = wpabuf_alloc(len);
  4917. if (tlvs == NULL)
  4918. return -1;
  4919. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  4920. wpabuf_free(tlvs);
  4921. return -1;
  4922. }
  4923. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  4924. wpabuf_free(tlvs);
  4925. }
  4926. if (ref == 0)
  4927. return -1;
  4928. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  4929. if (os_snprintf_error(buflen, res))
  4930. return -1;
  4931. return res;
  4932. }
  4933. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  4934. char *cmd)
  4935. {
  4936. long long unsigned val;
  4937. u64 req;
  4938. if (sscanf(cmd, "%llx", &val) != 1)
  4939. return -1;
  4940. req = val;
  4941. return wpas_p2p_sd_cancel_request(wpa_s, req);
  4942. }
  4943. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  4944. {
  4945. int freq;
  4946. u8 dst[ETH_ALEN];
  4947. u8 dialog_token;
  4948. struct wpabuf *resp_tlvs;
  4949. char *pos, *pos2;
  4950. size_t len;
  4951. pos = os_strchr(cmd, ' ');
  4952. if (pos == NULL)
  4953. return -1;
  4954. *pos++ = '\0';
  4955. freq = atoi(cmd);
  4956. if (freq == 0)
  4957. return -1;
  4958. if (hwaddr_aton(pos, dst))
  4959. return -1;
  4960. pos += 17;
  4961. if (*pos != ' ')
  4962. return -1;
  4963. pos++;
  4964. pos2 = os_strchr(pos, ' ');
  4965. if (pos2 == NULL)
  4966. return -1;
  4967. *pos2++ = '\0';
  4968. dialog_token = atoi(pos);
  4969. len = os_strlen(pos2);
  4970. if (len & 1)
  4971. return -1;
  4972. len /= 2;
  4973. resp_tlvs = wpabuf_alloc(len);
  4974. if (resp_tlvs == NULL)
  4975. return -1;
  4976. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  4977. wpabuf_free(resp_tlvs);
  4978. return -1;
  4979. }
  4980. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  4981. wpabuf_free(resp_tlvs);
  4982. return 0;
  4983. }
  4984. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  4985. char *cmd)
  4986. {
  4987. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  4988. return -1;
  4989. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  4990. return 0;
  4991. }
  4992. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  4993. char *cmd)
  4994. {
  4995. char *pos;
  4996. size_t len;
  4997. struct wpabuf *query, *resp;
  4998. pos = os_strchr(cmd, ' ');
  4999. if (pos == NULL)
  5000. return -1;
  5001. *pos++ = '\0';
  5002. len = os_strlen(cmd);
  5003. if (len & 1)
  5004. return -1;
  5005. len /= 2;
  5006. query = wpabuf_alloc(len);
  5007. if (query == NULL)
  5008. return -1;
  5009. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  5010. wpabuf_free(query);
  5011. return -1;
  5012. }
  5013. len = os_strlen(pos);
  5014. if (len & 1) {
  5015. wpabuf_free(query);
  5016. return -1;
  5017. }
  5018. len /= 2;
  5019. resp = wpabuf_alloc(len);
  5020. if (resp == NULL) {
  5021. wpabuf_free(query);
  5022. return -1;
  5023. }
  5024. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  5025. wpabuf_free(query);
  5026. wpabuf_free(resp);
  5027. return -1;
  5028. }
  5029. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  5030. wpabuf_free(query);
  5031. wpabuf_free(resp);
  5032. return -1;
  5033. }
  5034. return 0;
  5035. }
  5036. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  5037. {
  5038. char *pos;
  5039. u8 version;
  5040. pos = os_strchr(cmd, ' ');
  5041. if (pos == NULL)
  5042. return -1;
  5043. *pos++ = '\0';
  5044. if (hexstr2bin(cmd, &version, 1) < 0)
  5045. return -1;
  5046. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  5047. }
  5048. static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
  5049. u8 replace, char *cmd)
  5050. {
  5051. char *pos;
  5052. char *adv_str;
  5053. u32 auto_accept, adv_id, svc_state, config_methods;
  5054. char *svc_info = NULL;
  5055. char *cpt_prio_str;
  5056. u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
  5057. pos = os_strchr(cmd, ' ');
  5058. if (pos == NULL)
  5059. return -1;
  5060. *pos++ = '\0';
  5061. /* Auto-Accept value is mandatory, and must be one of the
  5062. * single values (0, 1, 2, 4) */
  5063. auto_accept = atoi(cmd);
  5064. switch (auto_accept) {
  5065. case P2PS_SETUP_NONE: /* No auto-accept */
  5066. case P2PS_SETUP_NEW:
  5067. case P2PS_SETUP_CLIENT:
  5068. case P2PS_SETUP_GROUP_OWNER:
  5069. break;
  5070. default:
  5071. return -1;
  5072. }
  5073. /* Advertisement ID is mandatory */
  5074. cmd = pos;
  5075. pos = os_strchr(cmd, ' ');
  5076. if (pos == NULL)
  5077. return -1;
  5078. *pos++ = '\0';
  5079. /* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
  5080. if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
  5081. return -1;
  5082. /* Only allow replacements if exist, and adds if not */
  5083. if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
  5084. if (!replace)
  5085. return -1;
  5086. } else {
  5087. if (replace)
  5088. return -1;
  5089. }
  5090. /* svc_state between 0 - 0xff is mandatory */
  5091. if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
  5092. return -1;
  5093. pos = os_strchr(pos, ' ');
  5094. if (pos == NULL)
  5095. return -1;
  5096. /* config_methods is mandatory */
  5097. pos++;
  5098. if (sscanf(pos, "%x", &config_methods) != 1)
  5099. return -1;
  5100. if (!(config_methods &
  5101. (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
  5102. return -1;
  5103. pos = os_strchr(pos, ' ');
  5104. if (pos == NULL)
  5105. return -1;
  5106. pos++;
  5107. adv_str = pos;
  5108. /* Advertisement string is mandatory */
  5109. if (!pos[0] || pos[0] == ' ')
  5110. return -1;
  5111. /* Terminate svc string */
  5112. pos = os_strchr(pos, ' ');
  5113. if (pos != NULL)
  5114. *pos++ = '\0';
  5115. cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
  5116. if (cpt_prio_str) {
  5117. pos = os_strchr(pos, ' ');
  5118. if (pos != NULL)
  5119. *pos++ = '\0';
  5120. if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
  5121. return -1;
  5122. } else {
  5123. cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
  5124. cpt_prio[1] = 0;
  5125. }
  5126. /* Service and Response Information are optional */
  5127. if (pos && pos[0]) {
  5128. size_t len;
  5129. /* Note the bare ' included, which cannot exist legally
  5130. * in unescaped string. */
  5131. svc_info = os_strstr(pos, "svc_info='");
  5132. if (svc_info) {
  5133. svc_info += 9;
  5134. len = os_strlen(svc_info);
  5135. utf8_unescape(svc_info, len, svc_info, len);
  5136. }
  5137. }
  5138. return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
  5139. (u8) svc_state, (u16) config_methods,
  5140. svc_info, cpt_prio);
  5141. }
  5142. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  5143. {
  5144. char *pos;
  5145. pos = os_strchr(cmd, ' ');
  5146. if (pos == NULL)
  5147. return -1;
  5148. *pos++ = '\0';
  5149. if (os_strcmp(cmd, "bonjour") == 0)
  5150. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  5151. if (os_strcmp(cmd, "upnp") == 0)
  5152. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  5153. if (os_strcmp(cmd, "asp") == 0)
  5154. return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
  5155. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  5156. return -1;
  5157. }
  5158. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  5159. char *cmd)
  5160. {
  5161. size_t len;
  5162. struct wpabuf *query;
  5163. int ret;
  5164. len = os_strlen(cmd);
  5165. if (len & 1)
  5166. return -1;
  5167. len /= 2;
  5168. query = wpabuf_alloc(len);
  5169. if (query == NULL)
  5170. return -1;
  5171. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  5172. wpabuf_free(query);
  5173. return -1;
  5174. }
  5175. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  5176. wpabuf_free(query);
  5177. return ret;
  5178. }
  5179. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  5180. {
  5181. char *pos;
  5182. u8 version;
  5183. pos = os_strchr(cmd, ' ');
  5184. if (pos == NULL)
  5185. return -1;
  5186. *pos++ = '\0';
  5187. if (hexstr2bin(cmd, &version, 1) < 0)
  5188. return -1;
  5189. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  5190. }
  5191. static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
  5192. {
  5193. u32 adv_id;
  5194. if (os_strcmp(cmd, "all") == 0) {
  5195. wpas_p2p_service_flush_asp(wpa_s);
  5196. return 0;
  5197. }
  5198. if (sscanf(cmd, "%x", &adv_id) != 1)
  5199. return -1;
  5200. return wpas_p2p_service_del_asp(wpa_s, adv_id);
  5201. }
  5202. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  5203. {
  5204. char *pos;
  5205. pos = os_strchr(cmd, ' ');
  5206. if (pos == NULL)
  5207. return -1;
  5208. *pos++ = '\0';
  5209. if (os_strcmp(cmd, "bonjour") == 0)
  5210. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  5211. if (os_strcmp(cmd, "upnp") == 0)
  5212. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  5213. if (os_strcmp(cmd, "asp") == 0)
  5214. return p2p_ctrl_service_del_asp(wpa_s, pos);
  5215. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  5216. return -1;
  5217. }
  5218. static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
  5219. {
  5220. char *pos;
  5221. pos = os_strchr(cmd, ' ');
  5222. if (pos == NULL)
  5223. return -1;
  5224. *pos++ = '\0';
  5225. if (os_strcmp(cmd, "asp") == 0)
  5226. return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
  5227. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  5228. return -1;
  5229. }
  5230. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  5231. {
  5232. u8 addr[ETH_ALEN];
  5233. /* <addr> */
  5234. if (hwaddr_aton(cmd, addr))
  5235. return -1;
  5236. return wpas_p2p_reject(wpa_s, addr);
  5237. }
  5238. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  5239. {
  5240. char *pos;
  5241. int id;
  5242. struct wpa_ssid *ssid;
  5243. u8 *_peer = NULL, peer[ETH_ALEN];
  5244. int freq = 0, pref_freq = 0;
  5245. int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
  5246. id = atoi(cmd);
  5247. pos = os_strstr(cmd, " peer=");
  5248. if (pos) {
  5249. pos += 6;
  5250. if (hwaddr_aton(pos, peer))
  5251. return -1;
  5252. _peer = peer;
  5253. }
  5254. ssid = wpa_config_get_network(wpa_s->conf, id);
  5255. if (ssid == NULL || ssid->disabled != 2) {
  5256. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  5257. "for persistent P2P group",
  5258. id);
  5259. return -1;
  5260. }
  5261. pos = os_strstr(cmd, " freq=");
  5262. if (pos) {
  5263. pos += 6;
  5264. freq = atoi(pos);
  5265. if (freq <= 0)
  5266. return -1;
  5267. }
  5268. pos = os_strstr(cmd, " pref=");
  5269. if (pos) {
  5270. pos += 6;
  5271. pref_freq = atoi(pos);
  5272. if (pref_freq <= 0)
  5273. return -1;
  5274. }
  5275. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  5276. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  5277. vht;
  5278. pos = os_strstr(cmd, "freq2=");
  5279. if (pos)
  5280. freq2 = atoi(pos + 6);
  5281. pos = os_strstr(cmd, " max_oper_chwidth=");
  5282. if (pos)
  5283. chwidth = atoi(pos + 18);
  5284. max_oper_chwidth = parse_freq(chwidth, freq2);
  5285. if (max_oper_chwidth < 0)
  5286. return -1;
  5287. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
  5288. max_oper_chwidth, pref_freq);
  5289. }
  5290. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  5291. {
  5292. char *pos;
  5293. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  5294. pos = os_strstr(cmd, " peer=");
  5295. if (!pos)
  5296. return -1;
  5297. *pos = '\0';
  5298. pos += 6;
  5299. if (hwaddr_aton(pos, peer)) {
  5300. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  5301. return -1;
  5302. }
  5303. pos = os_strstr(pos, " go_dev_addr=");
  5304. if (pos) {
  5305. pos += 13;
  5306. if (hwaddr_aton(pos, go_dev_addr)) {
  5307. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  5308. pos);
  5309. return -1;
  5310. }
  5311. go_dev = go_dev_addr;
  5312. }
  5313. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  5314. }
  5315. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  5316. {
  5317. if (os_strncmp(cmd, "persistent=", 11) == 0)
  5318. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  5319. if (os_strncmp(cmd, "group=", 6) == 0)
  5320. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  5321. return -1;
  5322. }
  5323. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  5324. int id, int freq, int vht_center_freq2,
  5325. int ht40, int vht, int vht_chwidth)
  5326. {
  5327. struct wpa_ssid *ssid;
  5328. ssid = wpa_config_get_network(wpa_s->conf, id);
  5329. if (ssid == NULL || ssid->disabled != 2) {
  5330. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  5331. "for persistent P2P group",
  5332. id);
  5333. return -1;
  5334. }
  5335. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq,
  5336. vht_center_freq2, 0, ht40, vht,
  5337. vht_chwidth, NULL, 0, 0);
  5338. }
  5339. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  5340. {
  5341. int freq = 0, persistent = 0, group_id = -1;
  5342. int vht = wpa_s->conf->p2p_go_vht;
  5343. int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
  5344. int max_oper_chwidth, chwidth = 0, freq2 = 0;
  5345. char *token, *context = NULL;
  5346. #ifdef CONFIG_ACS
  5347. int acs = 0;
  5348. #endif /* CONFIG_ACS */
  5349. while ((token = str_token(cmd, " ", &context))) {
  5350. if (sscanf(token, "freq2=%d", &freq2) == 1 ||
  5351. sscanf(token, "persistent=%d", &group_id) == 1 ||
  5352. sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1) {
  5353. continue;
  5354. #ifdef CONFIG_ACS
  5355. } else if (os_strcmp(token, "freq=acs") == 0) {
  5356. acs = 1;
  5357. #endif /* CONFIG_ACS */
  5358. } else if (sscanf(token, "freq=%d", &freq) == 1) {
  5359. continue;
  5360. } else if (os_strcmp(token, "ht40") == 0) {
  5361. ht40 = 1;
  5362. } else if (os_strcmp(token, "vht") == 0) {
  5363. vht = 1;
  5364. ht40 = 1;
  5365. } else if (os_strcmp(token, "persistent") == 0) {
  5366. persistent = 1;
  5367. } else {
  5368. wpa_printf(MSG_DEBUG,
  5369. "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
  5370. token);
  5371. return -1;
  5372. }
  5373. }
  5374. #ifdef CONFIG_ACS
  5375. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
  5376. (acs || freq == 2 || freq == 5)) {
  5377. if (freq == 2 && wpa_s->best_24_freq <= 0) {
  5378. wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211G;
  5379. wpa_s->p2p_go_do_acs = 1;
  5380. freq = 0;
  5381. } else if (freq == 5 && wpa_s->best_5_freq <= 0) {
  5382. wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211A;
  5383. wpa_s->p2p_go_do_acs = 1;
  5384. freq = 0;
  5385. } else {
  5386. wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211ANY;
  5387. wpa_s->p2p_go_do_acs = 1;
  5388. }
  5389. }
  5390. #endif /* CONFIG_ACS */
  5391. max_oper_chwidth = parse_freq(chwidth, freq2);
  5392. if (max_oper_chwidth < 0)
  5393. return -1;
  5394. if (group_id >= 0)
  5395. return p2p_ctrl_group_add_persistent(wpa_s, group_id,
  5396. freq, freq2, ht40, vht,
  5397. max_oper_chwidth);
  5398. return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
  5399. max_oper_chwidth);
  5400. }
  5401. static int p2p_ctrl_group_member(struct wpa_supplicant *wpa_s, const char *cmd,
  5402. char *buf, size_t buflen)
  5403. {
  5404. u8 dev_addr[ETH_ALEN];
  5405. struct wpa_ssid *ssid;
  5406. int res;
  5407. const u8 *iaddr;
  5408. ssid = wpa_s->current_ssid;
  5409. if (!wpa_s->global->p2p || !ssid || ssid->mode != WPAS_MODE_P2P_GO ||
  5410. hwaddr_aton(cmd, dev_addr))
  5411. return -1;
  5412. iaddr = p2p_group_get_client_interface_addr(wpa_s->p2p_group, dev_addr);
  5413. if (!iaddr)
  5414. return -1;
  5415. res = os_snprintf(buf, buflen, MACSTR, MAC2STR(iaddr));
  5416. if (os_snprintf_error(buflen, res))
  5417. return -1;
  5418. return res;
  5419. }
  5420. static int wpas_find_p2p_dev_addr_bss(struct wpa_global *global,
  5421. const u8 *p2p_dev_addr)
  5422. {
  5423. struct wpa_supplicant *wpa_s;
  5424. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5425. if (wpa_bss_get_p2p_dev_addr(wpa_s, p2p_dev_addr))
  5426. return 1;
  5427. }
  5428. return 0;
  5429. }
  5430. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  5431. char *buf, size_t buflen)
  5432. {
  5433. u8 addr[ETH_ALEN], *addr_ptr, group_capab;
  5434. int next, res;
  5435. const struct p2p_peer_info *info;
  5436. char *pos, *end;
  5437. char devtype[WPS_DEV_TYPE_BUFSIZE];
  5438. struct wpa_ssid *ssid;
  5439. size_t i;
  5440. if (!wpa_s->global->p2p)
  5441. return -1;
  5442. if (os_strcmp(cmd, "FIRST") == 0) {
  5443. addr_ptr = NULL;
  5444. next = 0;
  5445. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  5446. if (hwaddr_aton(cmd + 5, addr) < 0)
  5447. return -1;
  5448. addr_ptr = addr;
  5449. next = 1;
  5450. } else {
  5451. if (hwaddr_aton(cmd, addr) < 0)
  5452. return -1;
  5453. addr_ptr = addr;
  5454. next = 0;
  5455. }
  5456. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  5457. if (info == NULL)
  5458. return -1;
  5459. group_capab = info->group_capab;
  5460. if (group_capab &&
  5461. !wpas_find_p2p_dev_addr_bss(wpa_s->global, info->p2p_device_addr)) {
  5462. wpa_printf(MSG_DEBUG,
  5463. "P2P: Could not find any BSS with p2p_dev_addr "
  5464. MACSTR ", hence override group_capab from 0x%x to 0",
  5465. MAC2STR(info->p2p_device_addr), group_capab);
  5466. group_capab = 0;
  5467. }
  5468. pos = buf;
  5469. end = buf + buflen;
  5470. res = os_snprintf(pos, end - pos, MACSTR "\n"
  5471. "pri_dev_type=%s\n"
  5472. "device_name=%s\n"
  5473. "manufacturer=%s\n"
  5474. "model_name=%s\n"
  5475. "model_number=%s\n"
  5476. "serial_number=%s\n"
  5477. "config_methods=0x%x\n"
  5478. "dev_capab=0x%x\n"
  5479. "group_capab=0x%x\n"
  5480. "level=%d\n",
  5481. MAC2STR(info->p2p_device_addr),
  5482. wps_dev_type_bin2str(info->pri_dev_type,
  5483. devtype, sizeof(devtype)),
  5484. info->device_name,
  5485. info->manufacturer,
  5486. info->model_name,
  5487. info->model_number,
  5488. info->serial_number,
  5489. info->config_methods,
  5490. info->dev_capab,
  5491. group_capab,
  5492. info->level);
  5493. if (os_snprintf_error(end - pos, res))
  5494. return pos - buf;
  5495. pos += res;
  5496. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  5497. {
  5498. const u8 *t;
  5499. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  5500. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  5501. wps_dev_type_bin2str(t, devtype,
  5502. sizeof(devtype)));
  5503. if (os_snprintf_error(end - pos, res))
  5504. return pos - buf;
  5505. pos += res;
  5506. }
  5507. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  5508. if (ssid) {
  5509. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  5510. if (os_snprintf_error(end - pos, res))
  5511. return pos - buf;
  5512. pos += res;
  5513. }
  5514. res = p2p_get_peer_info_txt(info, pos, end - pos);
  5515. if (res < 0)
  5516. return pos - buf;
  5517. pos += res;
  5518. if (info->vendor_elems) {
  5519. res = os_snprintf(pos, end - pos, "vendor_elems=");
  5520. if (os_snprintf_error(end - pos, res))
  5521. return pos - buf;
  5522. pos += res;
  5523. pos += wpa_snprintf_hex(pos, end - pos,
  5524. wpabuf_head(info->vendor_elems),
  5525. wpabuf_len(info->vendor_elems));
  5526. res = os_snprintf(pos, end - pos, "\n");
  5527. if (os_snprintf_error(end - pos, res))
  5528. return pos - buf;
  5529. pos += res;
  5530. }
  5531. return pos - buf;
  5532. }
  5533. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  5534. const char *param)
  5535. {
  5536. unsigned int i;
  5537. if (wpa_s->global->p2p == NULL)
  5538. return -1;
  5539. if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
  5540. return -1;
  5541. for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
  5542. struct wpa_freq_range *freq;
  5543. freq = &wpa_s->global->p2p_disallow_freq.range[i];
  5544. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  5545. freq->min, freq->max);
  5546. }
  5547. wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
  5548. return 0;
  5549. }
  5550. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  5551. {
  5552. char *param;
  5553. if (wpa_s->global->p2p == NULL)
  5554. return -1;
  5555. param = os_strchr(cmd, ' ');
  5556. if (param == NULL)
  5557. return -1;
  5558. *param++ = '\0';
  5559. if (os_strcmp(cmd, "discoverability") == 0) {
  5560. p2p_set_client_discoverability(wpa_s->global->p2p,
  5561. atoi(param));
  5562. return 0;
  5563. }
  5564. if (os_strcmp(cmd, "managed") == 0) {
  5565. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  5566. return 0;
  5567. }
  5568. if (os_strcmp(cmd, "listen_channel") == 0) {
  5569. char *pos;
  5570. u8 channel, op_class;
  5571. channel = atoi(param);
  5572. pos = os_strchr(param, ' ');
  5573. op_class = pos ? atoi(pos) : 81;
  5574. return p2p_set_listen_channel(wpa_s->global->p2p, op_class,
  5575. channel, 1);
  5576. }
  5577. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  5578. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  5579. os_strlen(param));
  5580. }
  5581. if (os_strcmp(cmd, "noa") == 0) {
  5582. char *pos;
  5583. int count, start, duration;
  5584. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  5585. count = atoi(param);
  5586. pos = os_strchr(param, ',');
  5587. if (pos == NULL)
  5588. return -1;
  5589. pos++;
  5590. start = atoi(pos);
  5591. pos = os_strchr(pos, ',');
  5592. if (pos == NULL)
  5593. return -1;
  5594. pos++;
  5595. duration = atoi(pos);
  5596. if (count < 0 || count > 255 || start < 0 || duration < 0)
  5597. return -1;
  5598. if (count == 0 && duration > 0)
  5599. return -1;
  5600. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  5601. "start=%d duration=%d", count, start, duration);
  5602. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  5603. }
  5604. if (os_strcmp(cmd, "ps") == 0)
  5605. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  5606. if (os_strcmp(cmd, "oppps") == 0)
  5607. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  5608. if (os_strcmp(cmd, "ctwindow") == 0)
  5609. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  5610. if (os_strcmp(cmd, "disabled") == 0) {
  5611. wpa_s->global->p2p_disabled = atoi(param);
  5612. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  5613. wpa_s->global->p2p_disabled ?
  5614. "disabled" : "enabled");
  5615. if (wpa_s->global->p2p_disabled) {
  5616. wpas_p2p_stop_find(wpa_s);
  5617. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  5618. p2p_flush(wpa_s->global->p2p);
  5619. }
  5620. return 0;
  5621. }
  5622. if (os_strcmp(cmd, "conc_pref") == 0) {
  5623. if (os_strcmp(param, "sta") == 0)
  5624. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  5625. else if (os_strcmp(param, "p2p") == 0)
  5626. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  5627. else {
  5628. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  5629. return -1;
  5630. }
  5631. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  5632. "%s", param);
  5633. return 0;
  5634. }
  5635. if (os_strcmp(cmd, "force_long_sd") == 0) {
  5636. wpa_s->force_long_sd = atoi(param);
  5637. return 0;
  5638. }
  5639. if (os_strcmp(cmd, "peer_filter") == 0) {
  5640. u8 addr[ETH_ALEN];
  5641. if (hwaddr_aton(param, addr))
  5642. return -1;
  5643. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  5644. return 0;
  5645. }
  5646. if (os_strcmp(cmd, "cross_connect") == 0)
  5647. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  5648. if (os_strcmp(cmd, "go_apsd") == 0) {
  5649. if (os_strcmp(param, "disable") == 0)
  5650. wpa_s->set_ap_uapsd = 0;
  5651. else {
  5652. wpa_s->set_ap_uapsd = 1;
  5653. wpa_s->ap_uapsd = atoi(param);
  5654. }
  5655. return 0;
  5656. }
  5657. if (os_strcmp(cmd, "client_apsd") == 0) {
  5658. if (os_strcmp(param, "disable") == 0)
  5659. wpa_s->set_sta_uapsd = 0;
  5660. else {
  5661. int be, bk, vi, vo;
  5662. char *pos;
  5663. /* format: BE,BK,VI,VO;max SP Length */
  5664. be = atoi(param);
  5665. pos = os_strchr(param, ',');
  5666. if (pos == NULL)
  5667. return -1;
  5668. pos++;
  5669. bk = atoi(pos);
  5670. pos = os_strchr(pos, ',');
  5671. if (pos == NULL)
  5672. return -1;
  5673. pos++;
  5674. vi = atoi(pos);
  5675. pos = os_strchr(pos, ',');
  5676. if (pos == NULL)
  5677. return -1;
  5678. pos++;
  5679. vo = atoi(pos);
  5680. /* ignore max SP Length for now */
  5681. wpa_s->set_sta_uapsd = 1;
  5682. wpa_s->sta_uapsd = 0;
  5683. if (be)
  5684. wpa_s->sta_uapsd |= BIT(0);
  5685. if (bk)
  5686. wpa_s->sta_uapsd |= BIT(1);
  5687. if (vi)
  5688. wpa_s->sta_uapsd |= BIT(2);
  5689. if (vo)
  5690. wpa_s->sta_uapsd |= BIT(3);
  5691. }
  5692. return 0;
  5693. }
  5694. if (os_strcmp(cmd, "disallow_freq") == 0)
  5695. return p2p_ctrl_disallow_freq(wpa_s, param);
  5696. if (os_strcmp(cmd, "disc_int") == 0) {
  5697. int min_disc_int, max_disc_int, max_disc_tu;
  5698. char *pos;
  5699. pos = param;
  5700. min_disc_int = atoi(pos);
  5701. pos = os_strchr(pos, ' ');
  5702. if (pos == NULL)
  5703. return -1;
  5704. *pos++ = '\0';
  5705. max_disc_int = atoi(pos);
  5706. pos = os_strchr(pos, ' ');
  5707. if (pos == NULL)
  5708. return -1;
  5709. *pos++ = '\0';
  5710. max_disc_tu = atoi(pos);
  5711. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  5712. max_disc_int, max_disc_tu);
  5713. }
  5714. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  5715. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  5716. return 0;
  5717. }
  5718. #ifdef CONFIG_WPS_NFC
  5719. if (os_strcmp(cmd, "nfc_tag") == 0)
  5720. return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
  5721. #endif /* CONFIG_WPS_NFC */
  5722. if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
  5723. wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
  5724. return 0;
  5725. }
  5726. if (os_strcmp(cmd, "override_pref_op_chan") == 0) {
  5727. int op_class, chan;
  5728. op_class = atoi(param);
  5729. param = os_strchr(param, ':');
  5730. if (!param)
  5731. return -1;
  5732. param++;
  5733. chan = atoi(param);
  5734. p2p_set_override_pref_op_chan(wpa_s->global->p2p, op_class,
  5735. chan);
  5736. return 0;
  5737. }
  5738. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  5739. cmd);
  5740. return -1;
  5741. }
  5742. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  5743. {
  5744. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  5745. wpa_s->force_long_sd = 0;
  5746. #ifdef CONFIG_TESTING_OPTIONS
  5747. os_free(wpa_s->get_pref_freq_list_override);
  5748. wpa_s->get_pref_freq_list_override = NULL;
  5749. #endif /* CONFIG_TESTING_OPTIONS */
  5750. wpas_p2p_stop_find(wpa_s);
  5751. wpa_s->parent->p2ps_method_config_any = 0;
  5752. if (wpa_s->global->p2p)
  5753. p2p_flush(wpa_s->global->p2p);
  5754. }
  5755. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  5756. {
  5757. char *pos, *pos2;
  5758. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  5759. if (cmd[0]) {
  5760. pos = os_strchr(cmd, ' ');
  5761. if (pos == NULL)
  5762. return -1;
  5763. *pos++ = '\0';
  5764. dur1 = atoi(cmd);
  5765. pos2 = os_strchr(pos, ' ');
  5766. if (pos2)
  5767. *pos2++ = '\0';
  5768. int1 = atoi(pos);
  5769. } else
  5770. pos2 = NULL;
  5771. if (pos2) {
  5772. pos = os_strchr(pos2, ' ');
  5773. if (pos == NULL)
  5774. return -1;
  5775. *pos++ = '\0';
  5776. dur2 = atoi(pos2);
  5777. int2 = atoi(pos);
  5778. }
  5779. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  5780. }
  5781. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  5782. {
  5783. char *pos;
  5784. unsigned int period = 0, interval = 0;
  5785. if (cmd[0]) {
  5786. pos = os_strchr(cmd, ' ');
  5787. if (pos == NULL)
  5788. return -1;
  5789. *pos++ = '\0';
  5790. period = atoi(cmd);
  5791. interval = atoi(pos);
  5792. }
  5793. return wpas_p2p_ext_listen(wpa_s, period, interval);
  5794. }
  5795. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  5796. {
  5797. const char *pos;
  5798. u8 peer[ETH_ALEN];
  5799. int iface_addr = 0;
  5800. pos = cmd;
  5801. if (os_strncmp(pos, "iface=", 6) == 0) {
  5802. iface_addr = 1;
  5803. pos += 6;
  5804. }
  5805. if (hwaddr_aton(pos, peer))
  5806. return -1;
  5807. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  5808. return 0;
  5809. }
  5810. static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
  5811. {
  5812. int freq = 0, period = 0, interval = 0, count = 0;
  5813. if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
  5814. {
  5815. wpa_printf(MSG_DEBUG,
  5816. "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
  5817. return -1;
  5818. }
  5819. return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
  5820. }
  5821. #endif /* CONFIG_P2P */
  5822. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
  5823. {
  5824. struct wpa_freq_range_list ranges;
  5825. int *freqs = NULL;
  5826. struct hostapd_hw_modes *mode;
  5827. u16 i;
  5828. if (wpa_s->hw.modes == NULL)
  5829. return NULL;
  5830. os_memset(&ranges, 0, sizeof(ranges));
  5831. if (freq_range_list_parse(&ranges, val) < 0)
  5832. return NULL;
  5833. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  5834. int j;
  5835. mode = &wpa_s->hw.modes[i];
  5836. for (j = 0; j < mode->num_channels; j++) {
  5837. unsigned int freq;
  5838. if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
  5839. continue;
  5840. freq = mode->channels[j].freq;
  5841. if (!freq_range_list_includes(&ranges, freq))
  5842. continue;
  5843. int_array_add_unique(&freqs, freq);
  5844. }
  5845. }
  5846. os_free(ranges.range);
  5847. return freqs;
  5848. }
  5849. #ifdef CONFIG_INTERWORKING
  5850. static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
  5851. {
  5852. int auto_sel = 0;
  5853. int *freqs = NULL;
  5854. if (param) {
  5855. char *pos;
  5856. auto_sel = os_strstr(param, "auto") != NULL;
  5857. pos = os_strstr(param, "freq=");
  5858. if (pos) {
  5859. freqs = freq_range_to_channel_list(wpa_s, pos + 5);
  5860. if (freqs == NULL)
  5861. return -1;
  5862. }
  5863. }
  5864. return interworking_select(wpa_s, auto_sel, freqs);
  5865. }
  5866. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
  5867. int only_add)
  5868. {
  5869. u8 bssid[ETH_ALEN];
  5870. struct wpa_bss *bss;
  5871. if (hwaddr_aton(dst, bssid)) {
  5872. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  5873. return -1;
  5874. }
  5875. bss = wpa_bss_get_bssid(wpa_s, bssid);
  5876. if (bss == NULL) {
  5877. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  5878. MAC2STR(bssid));
  5879. return -1;
  5880. }
  5881. if (bss->ssid_len == 0) {
  5882. int found = 0;
  5883. wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
  5884. " does not have SSID information", MAC2STR(bssid));
  5885. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
  5886. list) {
  5887. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
  5888. bss->ssid_len > 0) {
  5889. found = 1;
  5890. break;
  5891. }
  5892. }
  5893. if (!found)
  5894. return -1;
  5895. wpa_printf(MSG_DEBUG,
  5896. "Found another matching BSS entry with SSID");
  5897. }
  5898. return interworking_connect(wpa_s, bss, only_add);
  5899. }
  5900. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  5901. {
  5902. u8 dst_addr[ETH_ALEN];
  5903. int used;
  5904. char *pos;
  5905. #define MAX_ANQP_INFO_ID 100
  5906. u16 id[MAX_ANQP_INFO_ID];
  5907. size_t num_id = 0;
  5908. u32 subtypes = 0;
  5909. u32 mbo_subtypes = 0;
  5910. used = hwaddr_aton2(dst, dst_addr);
  5911. if (used < 0)
  5912. return -1;
  5913. pos = dst + used;
  5914. if (*pos == ' ')
  5915. pos++;
  5916. while (num_id < MAX_ANQP_INFO_ID) {
  5917. if (os_strncmp(pos, "hs20:", 5) == 0) {
  5918. #ifdef CONFIG_HS20
  5919. int num = atoi(pos + 5);
  5920. if (num <= 0 || num > 31)
  5921. return -1;
  5922. subtypes |= BIT(num);
  5923. #else /* CONFIG_HS20 */
  5924. return -1;
  5925. #endif /* CONFIG_HS20 */
  5926. } else if (os_strncmp(pos, "mbo:", 4) == 0) {
  5927. #ifdef CONFIG_MBO
  5928. int num = atoi(pos + 4);
  5929. if (num <= 0 || num > MAX_MBO_ANQP_SUBTYPE)
  5930. return -1;
  5931. mbo_subtypes |= BIT(num);
  5932. #else /* CONFIG_MBO */
  5933. return -1;
  5934. #endif /* CONFIG_MBO */
  5935. } else {
  5936. id[num_id] = atoi(pos);
  5937. if (id[num_id])
  5938. num_id++;
  5939. }
  5940. pos = os_strchr(pos + 1, ',');
  5941. if (pos == NULL)
  5942. break;
  5943. pos++;
  5944. }
  5945. if (num_id == 0 && !subtypes && !mbo_subtypes)
  5946. return -1;
  5947. return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes,
  5948. mbo_subtypes);
  5949. }
  5950. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  5951. {
  5952. u8 dst_addr[ETH_ALEN];
  5953. struct wpabuf *advproto, *query = NULL;
  5954. int used, ret = -1;
  5955. char *pos, *end;
  5956. size_t len;
  5957. used = hwaddr_aton2(cmd, dst_addr);
  5958. if (used < 0)
  5959. return -1;
  5960. pos = cmd + used;
  5961. while (*pos == ' ')
  5962. pos++;
  5963. /* Advertisement Protocol ID */
  5964. end = os_strchr(pos, ' ');
  5965. if (end)
  5966. len = end - pos;
  5967. else
  5968. len = os_strlen(pos);
  5969. if (len & 0x01)
  5970. return -1;
  5971. len /= 2;
  5972. if (len == 0)
  5973. return -1;
  5974. advproto = wpabuf_alloc(len);
  5975. if (advproto == NULL)
  5976. return -1;
  5977. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  5978. goto fail;
  5979. if (end) {
  5980. /* Optional Query Request */
  5981. pos = end + 1;
  5982. while (*pos == ' ')
  5983. pos++;
  5984. len = os_strlen(pos);
  5985. if (len) {
  5986. if (len & 0x01)
  5987. goto fail;
  5988. len /= 2;
  5989. if (len == 0)
  5990. goto fail;
  5991. query = wpabuf_alloc(len);
  5992. if (query == NULL)
  5993. goto fail;
  5994. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  5995. goto fail;
  5996. }
  5997. }
  5998. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  5999. fail:
  6000. wpabuf_free(advproto);
  6001. wpabuf_free(query);
  6002. return ret;
  6003. }
  6004. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  6005. size_t buflen)
  6006. {
  6007. u8 addr[ETH_ALEN];
  6008. int dialog_token;
  6009. int used;
  6010. char *pos;
  6011. size_t resp_len, start, requested_len;
  6012. struct wpabuf *resp;
  6013. int ret;
  6014. used = hwaddr_aton2(cmd, addr);
  6015. if (used < 0)
  6016. return -1;
  6017. pos = cmd + used;
  6018. while (*pos == ' ')
  6019. pos++;
  6020. dialog_token = atoi(pos);
  6021. if (wpa_s->last_gas_resp &&
  6022. os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
  6023. dialog_token == wpa_s->last_gas_dialog_token)
  6024. resp = wpa_s->last_gas_resp;
  6025. else if (wpa_s->prev_gas_resp &&
  6026. os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
  6027. dialog_token == wpa_s->prev_gas_dialog_token)
  6028. resp = wpa_s->prev_gas_resp;
  6029. else
  6030. return -1;
  6031. resp_len = wpabuf_len(resp);
  6032. start = 0;
  6033. requested_len = resp_len;
  6034. pos = os_strchr(pos, ' ');
  6035. if (pos) {
  6036. start = atoi(pos);
  6037. if (start > resp_len)
  6038. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  6039. pos = os_strchr(pos, ',');
  6040. if (pos == NULL)
  6041. return -1;
  6042. pos++;
  6043. requested_len = atoi(pos);
  6044. if (start + requested_len > resp_len)
  6045. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  6046. }
  6047. if (requested_len * 2 + 1 > buflen)
  6048. return os_snprintf(buf, buflen, "FAIL-Too long response");
  6049. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
  6050. requested_len);
  6051. if (start + requested_len == resp_len) {
  6052. /*
  6053. * Free memory by dropping the response after it has been
  6054. * fetched.
  6055. */
  6056. if (resp == wpa_s->prev_gas_resp) {
  6057. wpabuf_free(wpa_s->prev_gas_resp);
  6058. wpa_s->prev_gas_resp = NULL;
  6059. } else {
  6060. wpabuf_free(wpa_s->last_gas_resp);
  6061. wpa_s->last_gas_resp = NULL;
  6062. }
  6063. }
  6064. return ret;
  6065. }
  6066. #endif /* CONFIG_INTERWORKING */
  6067. #ifdef CONFIG_HS20
  6068. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  6069. {
  6070. u8 dst_addr[ETH_ALEN];
  6071. int used;
  6072. char *pos;
  6073. u32 subtypes = 0;
  6074. used = hwaddr_aton2(dst, dst_addr);
  6075. if (used < 0)
  6076. return -1;
  6077. pos = dst + used;
  6078. if (*pos == ' ')
  6079. pos++;
  6080. for (;;) {
  6081. int num = atoi(pos);
  6082. if (num <= 0 || num > 31)
  6083. return -1;
  6084. subtypes |= BIT(num);
  6085. pos = os_strchr(pos + 1, ',');
  6086. if (pos == NULL)
  6087. break;
  6088. pos++;
  6089. }
  6090. if (subtypes == 0)
  6091. return -1;
  6092. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
  6093. }
  6094. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  6095. const u8 *addr, const char *realm)
  6096. {
  6097. u8 *buf;
  6098. size_t rlen, len;
  6099. int ret;
  6100. rlen = os_strlen(realm);
  6101. len = 3 + rlen;
  6102. buf = os_malloc(len);
  6103. if (buf == NULL)
  6104. return -1;
  6105. buf[0] = 1; /* NAI Home Realm Count */
  6106. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  6107. buf[2] = rlen;
  6108. os_memcpy(buf + 3, realm, rlen);
  6109. ret = hs20_anqp_send_req(wpa_s, addr,
  6110. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  6111. buf, len, 0);
  6112. os_free(buf);
  6113. return ret;
  6114. }
  6115. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  6116. char *dst)
  6117. {
  6118. struct wpa_cred *cred = wpa_s->conf->cred;
  6119. u8 dst_addr[ETH_ALEN];
  6120. int used;
  6121. u8 *buf;
  6122. size_t len;
  6123. int ret;
  6124. used = hwaddr_aton2(dst, dst_addr);
  6125. if (used < 0)
  6126. return -1;
  6127. while (dst[used] == ' ')
  6128. used++;
  6129. if (os_strncmp(dst + used, "realm=", 6) == 0)
  6130. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  6131. dst + used + 6);
  6132. len = os_strlen(dst + used);
  6133. if (len == 0 && cred && cred->realm)
  6134. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  6135. if (len & 1)
  6136. return -1;
  6137. len /= 2;
  6138. buf = os_malloc(len);
  6139. if (buf == NULL)
  6140. return -1;
  6141. if (hexstr2bin(dst + used, buf, len) < 0) {
  6142. os_free(buf);
  6143. return -1;
  6144. }
  6145. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  6146. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  6147. buf, len, 0);
  6148. os_free(buf);
  6149. return ret;
  6150. }
  6151. static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
  6152. int buflen)
  6153. {
  6154. u8 dst_addr[ETH_ALEN];
  6155. int used;
  6156. char *ctx = NULL, *icon, *poffset, *psize;
  6157. used = hwaddr_aton2(cmd, dst_addr);
  6158. if (used < 0)
  6159. return -1;
  6160. cmd += used;
  6161. icon = str_token(cmd, " ", &ctx);
  6162. poffset = str_token(cmd, " ", &ctx);
  6163. psize = str_token(cmd, " ", &ctx);
  6164. if (!icon || !poffset || !psize)
  6165. return -1;
  6166. wpa_s->fetch_osu_icon_in_progress = 0;
  6167. return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
  6168. reply, buflen);
  6169. }
  6170. static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
  6171. {
  6172. u8 dst_addr[ETH_ALEN];
  6173. int used;
  6174. char *icon;
  6175. if (!cmd[0])
  6176. return hs20_del_icon(wpa_s, NULL, NULL);
  6177. used = hwaddr_aton2(cmd, dst_addr);
  6178. if (used < 0)
  6179. return -1;
  6180. while (cmd[used] == ' ')
  6181. used++;
  6182. icon = cmd[used] ? &cmd[used] : NULL;
  6183. return hs20_del_icon(wpa_s, dst_addr, icon);
  6184. }
  6185. static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
  6186. {
  6187. u8 dst_addr[ETH_ALEN];
  6188. int used;
  6189. char *icon;
  6190. used = hwaddr_aton2(cmd, dst_addr);
  6191. if (used < 0)
  6192. return -1;
  6193. while (cmd[used] == ' ')
  6194. used++;
  6195. icon = &cmd[used];
  6196. wpa_s->fetch_osu_icon_in_progress = 0;
  6197. return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
  6198. (u8 *) icon, os_strlen(icon), inmem);
  6199. }
  6200. #endif /* CONFIG_HS20 */
  6201. #ifdef CONFIG_AUTOSCAN
  6202. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  6203. char *cmd)
  6204. {
  6205. enum wpa_states state = wpa_s->wpa_state;
  6206. char *new_params = NULL;
  6207. if (os_strlen(cmd) > 0) {
  6208. new_params = os_strdup(cmd);
  6209. if (new_params == NULL)
  6210. return -1;
  6211. }
  6212. os_free(wpa_s->conf->autoscan);
  6213. wpa_s->conf->autoscan = new_params;
  6214. if (wpa_s->conf->autoscan == NULL)
  6215. autoscan_deinit(wpa_s);
  6216. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  6217. autoscan_init(wpa_s, 1);
  6218. else if (state == WPA_SCANNING)
  6219. wpa_supplicant_reinit_autoscan(wpa_s);
  6220. else
  6221. wpa_printf(MSG_DEBUG, "No autoscan update in state %s",
  6222. wpa_supplicant_state_txt(state));
  6223. return 0;
  6224. }
  6225. #endif /* CONFIG_AUTOSCAN */
  6226. #ifdef CONFIG_WNM
  6227. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  6228. {
  6229. int enter;
  6230. int intval = 0;
  6231. char *pos;
  6232. int ret;
  6233. struct wpabuf *tfs_req = NULL;
  6234. if (os_strncmp(cmd, "enter", 5) == 0)
  6235. enter = 1;
  6236. else if (os_strncmp(cmd, "exit", 4) == 0)
  6237. enter = 0;
  6238. else
  6239. return -1;
  6240. pos = os_strstr(cmd, " interval=");
  6241. if (pos)
  6242. intval = atoi(pos + 10);
  6243. pos = os_strstr(cmd, " tfs_req=");
  6244. if (pos) {
  6245. char *end;
  6246. size_t len;
  6247. pos += 9;
  6248. end = os_strchr(pos, ' ');
  6249. if (end)
  6250. len = end - pos;
  6251. else
  6252. len = os_strlen(pos);
  6253. if (len & 1)
  6254. return -1;
  6255. len /= 2;
  6256. tfs_req = wpabuf_alloc(len);
  6257. if (tfs_req == NULL)
  6258. return -1;
  6259. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  6260. wpabuf_free(tfs_req);
  6261. return -1;
  6262. }
  6263. }
  6264. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  6265. WNM_SLEEP_MODE_EXIT, intval,
  6266. tfs_req);
  6267. wpabuf_free(tfs_req);
  6268. return ret;
  6269. }
  6270. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  6271. {
  6272. int query_reason, list = 0;
  6273. char *btm_candidates = NULL;
  6274. query_reason = atoi(cmd);
  6275. cmd = os_strchr(cmd, ' ');
  6276. if (cmd) {
  6277. if (os_strncmp(cmd, " list", 5) == 0)
  6278. list = 1;
  6279. else
  6280. btm_candidates = cmd;
  6281. }
  6282. wpa_printf(MSG_DEBUG,
  6283. "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
  6284. query_reason, list ? " candidate list" : "");
  6285. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason,
  6286. btm_candidates,
  6287. list);
  6288. }
  6289. #endif /* CONFIG_WNM */
  6290. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  6291. size_t buflen)
  6292. {
  6293. struct wpa_signal_info si;
  6294. int ret;
  6295. char *pos, *end;
  6296. ret = wpa_drv_signal_poll(wpa_s, &si);
  6297. if (ret)
  6298. return -1;
  6299. pos = buf;
  6300. end = buf + buflen;
  6301. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  6302. "NOISE=%d\nFREQUENCY=%u\n",
  6303. si.current_signal, si.current_txrate / 1000,
  6304. si.current_noise, si.frequency);
  6305. if (os_snprintf_error(end - pos, ret))
  6306. return -1;
  6307. pos += ret;
  6308. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  6309. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  6310. channel_width_to_string(si.chanwidth));
  6311. if (os_snprintf_error(end - pos, ret))
  6312. return -1;
  6313. pos += ret;
  6314. }
  6315. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  6316. ret = os_snprintf(pos, end - pos,
  6317. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  6318. si.center_frq1, si.center_frq2);
  6319. if (os_snprintf_error(end - pos, ret))
  6320. return -1;
  6321. pos += ret;
  6322. }
  6323. if (si.avg_signal) {
  6324. ret = os_snprintf(pos, end - pos,
  6325. "AVG_RSSI=%d\n", si.avg_signal);
  6326. if (os_snprintf_error(end - pos, ret))
  6327. return -1;
  6328. pos += ret;
  6329. }
  6330. if (si.avg_beacon_signal) {
  6331. ret = os_snprintf(pos, end - pos,
  6332. "AVG_BEACON_RSSI=%d\n", si.avg_beacon_signal);
  6333. if (os_snprintf_error(end - pos, ret))
  6334. return -1;
  6335. pos += ret;
  6336. }
  6337. return pos - buf;
  6338. }
  6339. static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
  6340. const char *cmd)
  6341. {
  6342. const char *pos;
  6343. int threshold = 0;
  6344. int hysteresis = 0;
  6345. if (wpa_s->bgscan && wpa_s->bgscan_priv) {
  6346. wpa_printf(MSG_DEBUG,
  6347. "Reject SIGNAL_MONITOR command - bgscan is active");
  6348. return -1;
  6349. }
  6350. pos = os_strstr(cmd, "THRESHOLD=");
  6351. if (pos)
  6352. threshold = atoi(pos + 10);
  6353. pos = os_strstr(cmd, "HYSTERESIS=");
  6354. if (pos)
  6355. hysteresis = atoi(pos + 11);
  6356. return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
  6357. }
  6358. #ifdef CONFIG_TESTING_OPTIONS
  6359. int wpas_ctrl_iface_get_pref_freq_list_override(struct wpa_supplicant *wpa_s,
  6360. enum wpa_driver_if_type if_type,
  6361. unsigned int *num,
  6362. unsigned int *freq_list)
  6363. {
  6364. char *pos = wpa_s->get_pref_freq_list_override;
  6365. char *end;
  6366. unsigned int count = 0;
  6367. /* Override string format:
  6368. * <if_type1>:<freq1>,<freq2>,... <if_type2>:... */
  6369. while (pos) {
  6370. if (atoi(pos) == (int) if_type)
  6371. break;
  6372. pos = os_strchr(pos, ' ');
  6373. if (pos)
  6374. pos++;
  6375. }
  6376. if (!pos)
  6377. return -1;
  6378. pos = os_strchr(pos, ':');
  6379. if (!pos)
  6380. return -1;
  6381. pos++;
  6382. end = os_strchr(pos, ' ');
  6383. while (pos && (!end || pos < end) && count < *num) {
  6384. freq_list[count++] = atoi(pos);
  6385. pos = os_strchr(pos, ',');
  6386. if (pos)
  6387. pos++;
  6388. }
  6389. *num = count;
  6390. return 0;
  6391. }
  6392. #endif /* CONFIG_TESTING_OPTIONS */
  6393. static int wpas_ctrl_iface_get_pref_freq_list(
  6394. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  6395. {
  6396. unsigned int freq_list[100], num = 100, i;
  6397. int ret;
  6398. enum wpa_driver_if_type iface_type;
  6399. char *pos, *end;
  6400. pos = buf;
  6401. end = buf + buflen;
  6402. /* buf: "<interface_type>" */
  6403. if (os_strcmp(cmd, "STATION") == 0)
  6404. iface_type = WPA_IF_STATION;
  6405. else if (os_strcmp(cmd, "AP") == 0)
  6406. iface_type = WPA_IF_AP_BSS;
  6407. else if (os_strcmp(cmd, "P2P_GO") == 0)
  6408. iface_type = WPA_IF_P2P_GO;
  6409. else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
  6410. iface_type = WPA_IF_P2P_CLIENT;
  6411. else if (os_strcmp(cmd, "IBSS") == 0)
  6412. iface_type = WPA_IF_IBSS;
  6413. else if (os_strcmp(cmd, "TDLS") == 0)
  6414. iface_type = WPA_IF_TDLS;
  6415. else
  6416. return -1;
  6417. wpa_printf(MSG_DEBUG,
  6418. "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
  6419. iface_type, buf);
  6420. ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
  6421. if (ret)
  6422. return -1;
  6423. for (i = 0; i < num; i++) {
  6424. ret = os_snprintf(pos, end - pos, "%s%u",
  6425. i > 0 ? "," : "", freq_list[i]);
  6426. if (os_snprintf_error(end - pos, ret))
  6427. return -1;
  6428. pos += ret;
  6429. }
  6430. return pos - buf;
  6431. }
  6432. static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
  6433. char *buf, size_t buflen)
  6434. {
  6435. int ret, i;
  6436. char *pos, *end;
  6437. ret = os_snprintf(buf, buflen, "%016llX:\n",
  6438. (long long unsigned) wpa_s->drv_flags);
  6439. if (os_snprintf_error(buflen, ret))
  6440. return -1;
  6441. pos = buf + ret;
  6442. end = buf + buflen;
  6443. for (i = 0; i < 64; i++) {
  6444. if (wpa_s->drv_flags & (1LLU << i)) {
  6445. ret = os_snprintf(pos, end - pos, "%s\n",
  6446. driver_flag_to_string(1LLU << i));
  6447. if (os_snprintf_error(end - pos, ret))
  6448. return -1;
  6449. pos += ret;
  6450. }
  6451. }
  6452. return pos - buf;
  6453. }
  6454. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  6455. size_t buflen)
  6456. {
  6457. struct hostap_sta_driver_data sta;
  6458. int ret;
  6459. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  6460. if (ret)
  6461. return -1;
  6462. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  6463. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  6464. if (os_snprintf_error(buflen, ret))
  6465. return -1;
  6466. return ret;
  6467. }
  6468. #ifdef ANDROID
  6469. static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  6470. char *buf, size_t buflen)
  6471. {
  6472. int ret;
  6473. ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
  6474. if (ret == 0) {
  6475. if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
  6476. struct p2p_data *p2p = wpa_s->global->p2p;
  6477. if (p2p) {
  6478. char country[3];
  6479. country[0] = cmd[8];
  6480. country[1] = cmd[9];
  6481. country[2] = 0x04;
  6482. p2p_set_country(p2p, country);
  6483. }
  6484. }
  6485. ret = os_snprintf(buf, buflen, "%s\n", "OK");
  6486. if (os_snprintf_error(buflen, ret))
  6487. ret = -1;
  6488. }
  6489. return ret;
  6490. }
  6491. #endif /* ANDROID */
  6492. static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  6493. char *buf, size_t buflen)
  6494. {
  6495. int ret;
  6496. char *pos;
  6497. u8 *data = NULL;
  6498. unsigned int vendor_id, subcmd;
  6499. struct wpabuf *reply;
  6500. size_t data_len = 0;
  6501. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  6502. vendor_id = strtoul(cmd, &pos, 16);
  6503. if (!isblank((unsigned char) *pos))
  6504. return -EINVAL;
  6505. subcmd = strtoul(pos, &pos, 10);
  6506. if (*pos != '\0') {
  6507. if (!isblank((unsigned char) *pos++))
  6508. return -EINVAL;
  6509. data_len = os_strlen(pos);
  6510. }
  6511. if (data_len) {
  6512. data_len /= 2;
  6513. data = os_malloc(data_len);
  6514. if (!data)
  6515. return -1;
  6516. if (hexstr2bin(pos, data, data_len)) {
  6517. wpa_printf(MSG_DEBUG,
  6518. "Vendor command: wrong parameter format");
  6519. os_free(data);
  6520. return -EINVAL;
  6521. }
  6522. }
  6523. reply = wpabuf_alloc((buflen - 1) / 2);
  6524. if (!reply) {
  6525. os_free(data);
  6526. return -1;
  6527. }
  6528. ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
  6529. reply);
  6530. if (ret == 0)
  6531. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  6532. wpabuf_len(reply));
  6533. wpabuf_free(reply);
  6534. os_free(data);
  6535. return ret;
  6536. }
  6537. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  6538. {
  6539. #ifdef CONFIG_P2P
  6540. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
  6541. wpa_s->global->p2p_init_wpa_s : wpa_s;
  6542. #endif /* CONFIG_P2P */
  6543. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  6544. if (wpas_abort_ongoing_scan(wpa_s) == 0)
  6545. wpa_s->ignore_post_flush_scan_res = 1;
  6546. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  6547. /*
  6548. * Avoid possible auto connect re-connection on getting
  6549. * disconnected due to state flush.
  6550. */
  6551. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  6552. }
  6553. #ifdef CONFIG_P2P
  6554. wpas_p2p_group_remove(p2p_wpa_s, "*");
  6555. wpas_p2p_cancel(p2p_wpa_s);
  6556. p2p_ctrl_flush(p2p_wpa_s);
  6557. wpas_p2p_service_flush(p2p_wpa_s);
  6558. p2p_wpa_s->global->p2p_disabled = 0;
  6559. p2p_wpa_s->global->p2p_per_sta_psk = 0;
  6560. p2p_wpa_s->conf->num_sec_device_types = 0;
  6561. p2p_wpa_s->p2p_disable_ip_addr_req = 0;
  6562. os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
  6563. p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
  6564. p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
  6565. p2p_wpa_s->global->pending_p2ps_group = 0;
  6566. p2p_wpa_s->global->pending_p2ps_group_freq = 0;
  6567. #endif /* CONFIG_P2P */
  6568. #ifdef CONFIG_WPS_TESTING
  6569. wps_version_number = 0x20;
  6570. wps_testing_dummy_cred = 0;
  6571. wps_corrupt_pkhash = 0;
  6572. wps_force_auth_types_in_use = 0;
  6573. wps_force_encr_types_in_use = 0;
  6574. #endif /* CONFIG_WPS_TESTING */
  6575. #ifdef CONFIG_WPS
  6576. wpa_s->wps_fragment_size = 0;
  6577. wpas_wps_cancel(wpa_s);
  6578. wps_registrar_flush(wpa_s->wps->registrar);
  6579. #endif /* CONFIG_WPS */
  6580. wpa_s->after_wps = 0;
  6581. wpa_s->known_wps_freq = 0;
  6582. #ifdef CONFIG_DPP
  6583. wpas_dpp_deinit(wpa_s);
  6584. wpa_s->dpp_init_max_tries = 0;
  6585. wpa_s->dpp_init_retry_time = 0;
  6586. wpa_s->dpp_resp_wait_time = 0;
  6587. wpa_s->dpp_resp_max_tries = 0;
  6588. wpa_s->dpp_resp_retry_time = 0;
  6589. #ifdef CONFIG_TESTING_OPTIONS
  6590. os_memset(dpp_pkex_own_mac_override, 0, ETH_ALEN);
  6591. os_memset(dpp_pkex_peer_mac_override, 0, ETH_ALEN);
  6592. dpp_pkex_ephemeral_key_override_len = 0;
  6593. #endif /* CONFIG_TESTING_OPTIONS */
  6594. #endif /* CONFIG_DPP */
  6595. #ifdef CONFIG_TDLS
  6596. #ifdef CONFIG_TDLS_TESTING
  6597. tdls_testing = 0;
  6598. #endif /* CONFIG_TDLS_TESTING */
  6599. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  6600. wpa_tdls_enable(wpa_s->wpa, 1);
  6601. #endif /* CONFIG_TDLS */
  6602. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  6603. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  6604. wpa_s->no_keep_alive = 0;
  6605. wpa_s->own_disconnect_req = 0;
  6606. os_free(wpa_s->disallow_aps_bssid);
  6607. wpa_s->disallow_aps_bssid = NULL;
  6608. wpa_s->disallow_aps_bssid_count = 0;
  6609. os_free(wpa_s->disallow_aps_ssid);
  6610. wpa_s->disallow_aps_ssid = NULL;
  6611. wpa_s->disallow_aps_ssid_count = 0;
  6612. wpa_s->set_sta_uapsd = 0;
  6613. wpa_s->sta_uapsd = 0;
  6614. wpa_drv_radio_disable(wpa_s, 0);
  6615. wpa_blacklist_clear(wpa_s);
  6616. wpa_s->extra_blacklist_count = 0;
  6617. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  6618. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  6619. wpa_config_flush_blobs(wpa_s->conf);
  6620. wpa_s->conf->auto_interworking = 0;
  6621. wpa_s->conf->okc = 0;
  6622. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  6623. rsn_preauth_deinit(wpa_s->wpa);
  6624. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
  6625. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
  6626. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
  6627. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  6628. radio_remove_works(wpa_s, NULL, 1);
  6629. wpa_s->ext_work_in_progress = 0;
  6630. wpa_s->next_ssid = NULL;
  6631. #ifdef CONFIG_INTERWORKING
  6632. #ifdef CONFIG_HS20
  6633. hs20_cancel_fetch_osu(wpa_s);
  6634. hs20_del_icon(wpa_s, NULL, NULL);
  6635. #endif /* CONFIG_HS20 */
  6636. #endif /* CONFIG_INTERWORKING */
  6637. wpa_s->ext_mgmt_frame_handling = 0;
  6638. wpa_s->ext_eapol_frame_io = 0;
  6639. #ifdef CONFIG_TESTING_OPTIONS
  6640. wpa_s->extra_roc_dur = 0;
  6641. wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
  6642. wpa_s->p2p_go_csa_on_inv = 0;
  6643. wpa_s->ignore_auth_resp = 0;
  6644. wpa_s->ignore_assoc_disallow = 0;
  6645. wpa_s->testing_resend_assoc = 0;
  6646. wpa_s->reject_btm_req_reason = 0;
  6647. wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
  6648. os_free(wpa_s->get_pref_freq_list_override);
  6649. wpa_s->get_pref_freq_list_override = NULL;
  6650. wpabuf_free(wpa_s->sae_commit_override);
  6651. wpa_s->sae_commit_override = NULL;
  6652. #ifdef CONFIG_DPP
  6653. os_free(wpa_s->dpp_config_obj_override);
  6654. wpa_s->dpp_config_obj_override = NULL;
  6655. os_free(wpa_s->dpp_discovery_override);
  6656. wpa_s->dpp_discovery_override = NULL;
  6657. os_free(wpa_s->dpp_groups_override);
  6658. wpa_s->dpp_groups_override = NULL;
  6659. dpp_test = DPP_TEST_DISABLED;
  6660. #endif /* CONFIG_DPP */
  6661. #endif /* CONFIG_TESTING_OPTIONS */
  6662. wpa_s->disconnected = 0;
  6663. os_free(wpa_s->next_scan_freqs);
  6664. wpa_s->next_scan_freqs = NULL;
  6665. os_free(wpa_s->select_network_scan_freqs);
  6666. wpa_s->select_network_scan_freqs = NULL;
  6667. wpa_bss_flush(wpa_s);
  6668. if (!dl_list_empty(&wpa_s->bss)) {
  6669. wpa_printf(MSG_DEBUG,
  6670. "BSS table not empty after flush: %u entries, current_bss=%p bssid="
  6671. MACSTR " pending_bssid=" MACSTR,
  6672. dl_list_len(&wpa_s->bss), wpa_s->current_bss,
  6673. MAC2STR(wpa_s->bssid),
  6674. MAC2STR(wpa_s->pending_bssid));
  6675. }
  6676. eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
  6677. wpa_s->wnmsleep_used = 0;
  6678. #ifdef CONFIG_SME
  6679. wpa_s->sme.last_unprot_disconnect.sec = 0;
  6680. #endif /* CONFIG_SME */
  6681. wpabuf_free(wpa_s->ric_ies);
  6682. wpa_s->ric_ies = NULL;
  6683. }
  6684. static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
  6685. char *buf, size_t buflen)
  6686. {
  6687. struct wpa_radio_work *work;
  6688. char *pos, *end;
  6689. struct os_reltime now, diff;
  6690. pos = buf;
  6691. end = buf + buflen;
  6692. os_get_reltime(&now);
  6693. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  6694. {
  6695. int ret;
  6696. os_reltime_sub(&now, &work->time, &diff);
  6697. ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
  6698. work->type, work->wpa_s->ifname, work->freq,
  6699. work->started, diff.sec, diff.usec);
  6700. if (os_snprintf_error(end - pos, ret))
  6701. break;
  6702. pos += ret;
  6703. }
  6704. return pos - buf;
  6705. }
  6706. static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
  6707. {
  6708. struct wpa_radio_work *work = eloop_ctx;
  6709. struct wpa_external_work *ework = work->ctx;
  6710. wpa_dbg(work->wpa_s, MSG_DEBUG,
  6711. "Timing out external radio work %u (%s)",
  6712. ework->id, work->type);
  6713. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
  6714. work->wpa_s->ext_work_in_progress = 0;
  6715. radio_work_done(work);
  6716. os_free(ework);
  6717. }
  6718. static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
  6719. {
  6720. struct wpa_external_work *ework = work->ctx;
  6721. if (deinit) {
  6722. if (work->started)
  6723. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  6724. work, NULL);
  6725. /*
  6726. * work->type points to a buffer in ework, so need to replace
  6727. * that here with a fixed string to avoid use of freed memory
  6728. * in debug prints.
  6729. */
  6730. work->type = "freed-ext-work";
  6731. work->ctx = NULL;
  6732. os_free(ework);
  6733. return;
  6734. }
  6735. wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
  6736. ework->id, ework->type);
  6737. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
  6738. work->wpa_s->ext_work_in_progress = 1;
  6739. if (!ework->timeout)
  6740. ework->timeout = 10;
  6741. eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
  6742. work, NULL);
  6743. }
  6744. static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
  6745. char *buf, size_t buflen)
  6746. {
  6747. struct wpa_external_work *ework;
  6748. char *pos, *pos2;
  6749. size_t type_len;
  6750. int ret;
  6751. unsigned int freq = 0;
  6752. /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
  6753. ework = os_zalloc(sizeof(*ework));
  6754. if (ework == NULL)
  6755. return -1;
  6756. pos = os_strchr(cmd, ' ');
  6757. if (pos) {
  6758. type_len = pos - cmd;
  6759. pos++;
  6760. pos2 = os_strstr(pos, "freq=");
  6761. if (pos2)
  6762. freq = atoi(pos2 + 5);
  6763. pos2 = os_strstr(pos, "timeout=");
  6764. if (pos2)
  6765. ework->timeout = atoi(pos2 + 8);
  6766. } else {
  6767. type_len = os_strlen(cmd);
  6768. }
  6769. if (4 + type_len >= sizeof(ework->type))
  6770. type_len = sizeof(ework->type) - 4 - 1;
  6771. os_strlcpy(ework->type, "ext:", sizeof(ework->type));
  6772. os_memcpy(ework->type + 4, cmd, type_len);
  6773. ework->type[4 + type_len] = '\0';
  6774. wpa_s->ext_work_id++;
  6775. if (wpa_s->ext_work_id == 0)
  6776. wpa_s->ext_work_id++;
  6777. ework->id = wpa_s->ext_work_id;
  6778. if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
  6779. ework) < 0) {
  6780. os_free(ework);
  6781. return -1;
  6782. }
  6783. ret = os_snprintf(buf, buflen, "%u", ework->id);
  6784. if (os_snprintf_error(buflen, ret))
  6785. return -1;
  6786. return ret;
  6787. }
  6788. static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
  6789. {
  6790. struct wpa_radio_work *work;
  6791. unsigned int id = atoi(cmd);
  6792. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  6793. {
  6794. struct wpa_external_work *ework;
  6795. if (os_strncmp(work->type, "ext:", 4) != 0)
  6796. continue;
  6797. ework = work->ctx;
  6798. if (id && ework->id != id)
  6799. continue;
  6800. wpa_dbg(wpa_s, MSG_DEBUG,
  6801. "Completed external radio work %u (%s)",
  6802. ework->id, ework->type);
  6803. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
  6804. wpa_s->ext_work_in_progress = 0;
  6805. radio_work_done(work);
  6806. os_free(ework);
  6807. return 3; /* "OK\n" */
  6808. }
  6809. return -1;
  6810. }
  6811. static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
  6812. char *buf, size_t buflen)
  6813. {
  6814. if (os_strcmp(cmd, "show") == 0)
  6815. return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
  6816. if (os_strncmp(cmd, "add ", 4) == 0)
  6817. return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
  6818. if (os_strncmp(cmd, "done ", 5) == 0)
  6819. return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
  6820. return -1;
  6821. }
  6822. void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
  6823. {
  6824. struct wpa_radio_work *work, *tmp;
  6825. if (!wpa_s || !wpa_s->radio)
  6826. return;
  6827. dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
  6828. struct wpa_radio_work, list) {
  6829. struct wpa_external_work *ework;
  6830. if (os_strncmp(work->type, "ext:", 4) != 0)
  6831. continue;
  6832. ework = work->ctx;
  6833. wpa_dbg(wpa_s, MSG_DEBUG,
  6834. "Flushing%s external radio work %u (%s)",
  6835. work->started ? " started" : "", ework->id,
  6836. ework->type);
  6837. if (work->started)
  6838. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  6839. work, NULL);
  6840. radio_work_done(work);
  6841. os_free(ework);
  6842. }
  6843. }
  6844. static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
  6845. {
  6846. struct wpa_supplicant *wpa_s = eloop_ctx;
  6847. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  6848. }
  6849. static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
  6850. unsigned int *scan_id_count, int scan_id[])
  6851. {
  6852. const char *pos = value;
  6853. while (pos) {
  6854. if (*pos == ' ' || *pos == '\0')
  6855. break;
  6856. if (*scan_id_count == MAX_SCAN_ID)
  6857. return -1;
  6858. scan_id[(*scan_id_count)++] = atoi(pos);
  6859. pos = os_strchr(pos, ',');
  6860. if (pos)
  6861. pos++;
  6862. }
  6863. return 0;
  6864. }
  6865. static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
  6866. char *reply, int reply_size, int *reply_len)
  6867. {
  6868. char *pos;
  6869. unsigned int manual_scan_passive = 0;
  6870. unsigned int manual_scan_use_id = 0;
  6871. unsigned int manual_scan_only_new = 0;
  6872. unsigned int scan_only = 0;
  6873. unsigned int scan_id_count = 0;
  6874. int scan_id[MAX_SCAN_ID];
  6875. void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
  6876. struct wpa_scan_results *scan_res);
  6877. int *manual_scan_freqs = NULL;
  6878. struct wpa_ssid_value *ssid = NULL, *ns;
  6879. unsigned int ssid_count = 0;
  6880. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  6881. *reply_len = -1;
  6882. return;
  6883. }
  6884. if (radio_work_pending(wpa_s, "scan")) {
  6885. wpa_printf(MSG_DEBUG,
  6886. "Pending scan scheduled - reject new request");
  6887. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  6888. return;
  6889. }
  6890. #ifdef CONFIG_INTERWORKING
  6891. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
  6892. wpa_printf(MSG_DEBUG,
  6893. "Interworking select in progress - reject new scan");
  6894. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  6895. return;
  6896. }
  6897. #endif /* CONFIG_INTERWORKING */
  6898. if (params) {
  6899. if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
  6900. scan_only = 1;
  6901. pos = os_strstr(params, "freq=");
  6902. if (pos) {
  6903. manual_scan_freqs = freq_range_to_channel_list(wpa_s,
  6904. pos + 5);
  6905. if (manual_scan_freqs == NULL) {
  6906. *reply_len = -1;
  6907. goto done;
  6908. }
  6909. }
  6910. pos = os_strstr(params, "passive=");
  6911. if (pos)
  6912. manual_scan_passive = !!atoi(pos + 8);
  6913. pos = os_strstr(params, "use_id=");
  6914. if (pos)
  6915. manual_scan_use_id = atoi(pos + 7);
  6916. pos = os_strstr(params, "only_new=1");
  6917. if (pos)
  6918. manual_scan_only_new = 1;
  6919. pos = os_strstr(params, "scan_id=");
  6920. if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
  6921. scan_id) < 0) {
  6922. *reply_len = -1;
  6923. goto done;
  6924. }
  6925. pos = os_strstr(params, "bssid=");
  6926. if (pos) {
  6927. u8 bssid[ETH_ALEN];
  6928. pos += 6;
  6929. if (hwaddr_aton(pos, bssid)) {
  6930. wpa_printf(MSG_ERROR, "Invalid BSSID %s", pos);
  6931. *reply_len = -1;
  6932. goto done;
  6933. }
  6934. os_memcpy(wpa_s->next_scan_bssid, bssid, ETH_ALEN);
  6935. }
  6936. pos = params;
  6937. while (pos && *pos != '\0') {
  6938. if (os_strncmp(pos, "ssid ", 5) == 0) {
  6939. char *end;
  6940. pos += 5;
  6941. end = pos;
  6942. while (*end) {
  6943. if (*end == '\0' || *end == ' ')
  6944. break;
  6945. end++;
  6946. }
  6947. ns = os_realloc_array(
  6948. ssid, ssid_count + 1,
  6949. sizeof(struct wpa_ssid_value));
  6950. if (ns == NULL) {
  6951. *reply_len = -1;
  6952. goto done;
  6953. }
  6954. ssid = ns;
  6955. if ((end - pos) & 0x01 ||
  6956. end - pos > 2 * SSID_MAX_LEN ||
  6957. hexstr2bin(pos, ssid[ssid_count].ssid,
  6958. (end - pos) / 2) < 0) {
  6959. wpa_printf(MSG_DEBUG,
  6960. "Invalid SSID value '%s'",
  6961. pos);
  6962. *reply_len = -1;
  6963. goto done;
  6964. }
  6965. ssid[ssid_count].ssid_len = (end - pos) / 2;
  6966. wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
  6967. ssid[ssid_count].ssid,
  6968. ssid[ssid_count].ssid_len);
  6969. ssid_count++;
  6970. pos = end;
  6971. }
  6972. pos = os_strchr(pos, ' ');
  6973. if (pos)
  6974. pos++;
  6975. }
  6976. }
  6977. wpa_s->num_ssids_from_scan_req = ssid_count;
  6978. os_free(wpa_s->ssids_from_scan_req);
  6979. if (ssid_count) {
  6980. wpa_s->ssids_from_scan_req = ssid;
  6981. ssid = NULL;
  6982. } else {
  6983. wpa_s->ssids_from_scan_req = NULL;
  6984. }
  6985. if (scan_only)
  6986. scan_res_handler = scan_only_handler;
  6987. else if (wpa_s->scan_res_handler == scan_only_handler)
  6988. scan_res_handler = NULL;
  6989. else
  6990. scan_res_handler = wpa_s->scan_res_handler;
  6991. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  6992. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  6993. (wpa_s->wpa_state == WPA_COMPLETED))) {
  6994. wpa_s->manual_scan_passive = manual_scan_passive;
  6995. wpa_s->manual_scan_use_id = manual_scan_use_id;
  6996. wpa_s->manual_scan_only_new = manual_scan_only_new;
  6997. wpa_s->scan_id_count = scan_id_count;
  6998. os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
  6999. wpa_s->scan_res_handler = scan_res_handler;
  7000. os_free(wpa_s->manual_scan_freqs);
  7001. wpa_s->manual_scan_freqs = manual_scan_freqs;
  7002. manual_scan_freqs = NULL;
  7003. wpa_s->normal_scans = 0;
  7004. wpa_s->scan_req = MANUAL_SCAN_REQ;
  7005. wpa_s->after_wps = 0;
  7006. wpa_s->known_wps_freq = 0;
  7007. wpa_supplicant_req_scan(wpa_s, 0, 0);
  7008. if (wpa_s->manual_scan_use_id) {
  7009. wpa_s->manual_scan_id++;
  7010. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  7011. wpa_s->manual_scan_id);
  7012. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  7013. wpa_s->manual_scan_id);
  7014. }
  7015. } else if (wpa_s->sched_scanning) {
  7016. wpa_s->manual_scan_passive = manual_scan_passive;
  7017. wpa_s->manual_scan_use_id = manual_scan_use_id;
  7018. wpa_s->manual_scan_only_new = manual_scan_only_new;
  7019. wpa_s->scan_id_count = scan_id_count;
  7020. os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
  7021. wpa_s->scan_res_handler = scan_res_handler;
  7022. os_free(wpa_s->manual_scan_freqs);
  7023. wpa_s->manual_scan_freqs = manual_scan_freqs;
  7024. manual_scan_freqs = NULL;
  7025. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
  7026. wpa_supplicant_cancel_sched_scan(wpa_s);
  7027. wpa_s->scan_req = MANUAL_SCAN_REQ;
  7028. wpa_supplicant_req_scan(wpa_s, 0, 0);
  7029. if (wpa_s->manual_scan_use_id) {
  7030. wpa_s->manual_scan_id++;
  7031. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  7032. wpa_s->manual_scan_id);
  7033. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  7034. wpa_s->manual_scan_id);
  7035. }
  7036. } else {
  7037. wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
  7038. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  7039. }
  7040. done:
  7041. os_free(manual_scan_freqs);
  7042. os_free(ssid);
  7043. }
  7044. #ifdef CONFIG_TESTING_OPTIONS
  7045. static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
  7046. unsigned int freq, const u8 *dst,
  7047. const u8 *src, const u8 *bssid,
  7048. const u8 *data, size_t data_len,
  7049. enum offchannel_send_action_result
  7050. result)
  7051. {
  7052. wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
  7053. " src=" MACSTR " bssid=" MACSTR " result=%s",
  7054. freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
  7055. result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
  7056. "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
  7057. "NO_ACK" : "FAILED"));
  7058. }
  7059. static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
  7060. {
  7061. char *pos, *param;
  7062. size_t len;
  7063. u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
  7064. int res, used;
  7065. int freq = 0, no_cck = 0, wait_time = 0;
  7066. /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
  7067. * <action=Action frame payload> */
  7068. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  7069. pos = cmd;
  7070. used = hwaddr_aton2(pos, da);
  7071. if (used < 0)
  7072. return -1;
  7073. pos += used;
  7074. while (*pos == ' ')
  7075. pos++;
  7076. used = hwaddr_aton2(pos, bssid);
  7077. if (used < 0)
  7078. return -1;
  7079. pos += used;
  7080. param = os_strstr(pos, " freq=");
  7081. if (param) {
  7082. param += 6;
  7083. freq = atoi(param);
  7084. }
  7085. param = os_strstr(pos, " no_cck=");
  7086. if (param) {
  7087. param += 8;
  7088. no_cck = atoi(param);
  7089. }
  7090. param = os_strstr(pos, " wait_time=");
  7091. if (param) {
  7092. param += 11;
  7093. wait_time = atoi(param);
  7094. }
  7095. param = os_strstr(pos, " action=");
  7096. if (param == NULL)
  7097. return -1;
  7098. param += 8;
  7099. len = os_strlen(param);
  7100. if (len & 1)
  7101. return -1;
  7102. len /= 2;
  7103. buf = os_malloc(len);
  7104. if (buf == NULL)
  7105. return -1;
  7106. if (hexstr2bin(param, buf, len) < 0) {
  7107. os_free(buf);
  7108. return -1;
  7109. }
  7110. res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
  7111. buf, len, wait_time,
  7112. wpas_ctrl_iface_mgmt_tx_cb, no_cck);
  7113. os_free(buf);
  7114. return res;
  7115. }
  7116. static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
  7117. {
  7118. wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
  7119. offchannel_send_action_done(wpa_s);
  7120. }
  7121. static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
  7122. char *cmd)
  7123. {
  7124. char *pos, *param;
  7125. size_t len;
  7126. u8 *buf;
  7127. int freq = 0, datarate = 0, ssi_signal = 0;
  7128. union wpa_event_data event;
  7129. if (!wpa_s->ext_mgmt_frame_handling)
  7130. return -1;
  7131. /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
  7132. wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
  7133. pos = cmd;
  7134. param = os_strstr(pos, "freq=");
  7135. if (param) {
  7136. param += 5;
  7137. freq = atoi(param);
  7138. }
  7139. param = os_strstr(pos, " datarate=");
  7140. if (param) {
  7141. param += 10;
  7142. datarate = atoi(param);
  7143. }
  7144. param = os_strstr(pos, " ssi_signal=");
  7145. if (param) {
  7146. param += 12;
  7147. ssi_signal = atoi(param);
  7148. }
  7149. param = os_strstr(pos, " frame=");
  7150. if (param == NULL)
  7151. return -1;
  7152. param += 7;
  7153. len = os_strlen(param);
  7154. if (len & 1)
  7155. return -1;
  7156. len /= 2;
  7157. buf = os_malloc(len);
  7158. if (buf == NULL)
  7159. return -1;
  7160. if (hexstr2bin(param, buf, len) < 0) {
  7161. os_free(buf);
  7162. return -1;
  7163. }
  7164. os_memset(&event, 0, sizeof(event));
  7165. event.rx_mgmt.freq = freq;
  7166. event.rx_mgmt.frame = buf;
  7167. event.rx_mgmt.frame_len = len;
  7168. event.rx_mgmt.ssi_signal = ssi_signal;
  7169. event.rx_mgmt.datarate = datarate;
  7170. wpa_s->ext_mgmt_frame_handling = 0;
  7171. wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
  7172. wpa_s->ext_mgmt_frame_handling = 1;
  7173. os_free(buf);
  7174. return 0;
  7175. }
  7176. static int wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant *wpa_s,
  7177. char *param)
  7178. {
  7179. struct wpa_scan_res *res;
  7180. struct os_reltime now;
  7181. char *pos, *end;
  7182. int ret = -1;
  7183. if (!param)
  7184. return -1;
  7185. if (os_strcmp(param, "START") == 0) {
  7186. wpa_bss_update_start(wpa_s);
  7187. return 0;
  7188. }
  7189. if (os_strcmp(param, "END") == 0) {
  7190. wpa_bss_update_end(wpa_s, NULL, 1);
  7191. return 0;
  7192. }
  7193. if (os_strncmp(param, "BSS ", 4) != 0)
  7194. return -1;
  7195. param += 3;
  7196. res = os_zalloc(sizeof(*res) + os_strlen(param) / 2);
  7197. if (!res)
  7198. return -1;
  7199. pos = os_strstr(param, " flags=");
  7200. if (pos)
  7201. res->flags = strtol(pos + 7, NULL, 16);
  7202. pos = os_strstr(param, " bssid=");
  7203. if (pos && hwaddr_aton(pos + 7, res->bssid))
  7204. goto fail;
  7205. pos = os_strstr(param, " freq=");
  7206. if (pos)
  7207. res->freq = atoi(pos + 6);
  7208. pos = os_strstr(param, " beacon_int=");
  7209. if (pos)
  7210. res->beacon_int = atoi(pos + 12);
  7211. pos = os_strstr(param, " caps=");
  7212. if (pos)
  7213. res->caps = strtol(pos + 6, NULL, 16);
  7214. pos = os_strstr(param, " qual=");
  7215. if (pos)
  7216. res->qual = atoi(pos + 6);
  7217. pos = os_strstr(param, " noise=");
  7218. if (pos)
  7219. res->noise = atoi(pos + 7);
  7220. pos = os_strstr(param, " level=");
  7221. if (pos)
  7222. res->level = atoi(pos + 7);
  7223. pos = os_strstr(param, " tsf=");
  7224. if (pos)
  7225. res->tsf = strtoll(pos + 5, NULL, 16);
  7226. pos = os_strstr(param, " age=");
  7227. if (pos)
  7228. res->age = atoi(pos + 5);
  7229. pos = os_strstr(param, " est_throughput=");
  7230. if (pos)
  7231. res->est_throughput = atoi(pos + 16);
  7232. pos = os_strstr(param, " snr=");
  7233. if (pos)
  7234. res->snr = atoi(pos + 5);
  7235. pos = os_strstr(param, " parent_tsf=");
  7236. if (pos)
  7237. res->parent_tsf = strtoll(pos + 7, NULL, 16);
  7238. pos = os_strstr(param, " tsf_bssid=");
  7239. if (pos && hwaddr_aton(pos + 11, res->tsf_bssid))
  7240. goto fail;
  7241. pos = os_strstr(param, " ie=");
  7242. if (pos) {
  7243. pos += 4;
  7244. end = os_strchr(pos, ' ');
  7245. if (!end)
  7246. end = pos + os_strlen(pos);
  7247. res->ie_len = (end - pos) / 2;
  7248. if (hexstr2bin(pos, (u8 *) (res + 1), res->ie_len))
  7249. goto fail;
  7250. }
  7251. pos = os_strstr(param, " beacon_ie=");
  7252. if (pos) {
  7253. pos += 11;
  7254. end = os_strchr(pos, ' ');
  7255. if (!end)
  7256. end = pos + os_strlen(pos);
  7257. res->beacon_ie_len = (end - pos) / 2;
  7258. if (hexstr2bin(pos, ((u8 *) (res + 1)) + res->ie_len,
  7259. res->beacon_ie_len))
  7260. goto fail;
  7261. }
  7262. os_get_reltime(&now);
  7263. wpa_bss_update_scan_res(wpa_s, res, &now);
  7264. ret = 0;
  7265. fail:
  7266. os_free(res);
  7267. return ret;
  7268. }
  7269. static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
  7270. {
  7271. char *pos, *param;
  7272. union wpa_event_data event;
  7273. enum wpa_event_type ev;
  7274. /* <event name> [parameters..] */
  7275. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
  7276. pos = cmd;
  7277. param = os_strchr(pos, ' ');
  7278. if (param)
  7279. *param++ = '\0';
  7280. os_memset(&event, 0, sizeof(event));
  7281. if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
  7282. ev = EVENT_INTERFACE_ENABLED;
  7283. } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
  7284. ev = EVENT_INTERFACE_DISABLED;
  7285. } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
  7286. ev = EVENT_AVOID_FREQUENCIES;
  7287. if (param == NULL)
  7288. param = "";
  7289. if (freq_range_list_parse(&event.freq_range, param) < 0)
  7290. return -1;
  7291. wpa_supplicant_event(wpa_s, ev, &event);
  7292. os_free(event.freq_range.range);
  7293. return 0;
  7294. } else if (os_strcmp(cmd, "SCAN_RES") == 0) {
  7295. return wpas_ctrl_iface_driver_scan_res(wpa_s, param);
  7296. } else {
  7297. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
  7298. cmd);
  7299. return -1;
  7300. }
  7301. wpa_supplicant_event(wpa_s, ev, &event);
  7302. return 0;
  7303. }
  7304. static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
  7305. {
  7306. char *pos;
  7307. u8 src[ETH_ALEN], *buf;
  7308. int used;
  7309. size_t len;
  7310. wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
  7311. pos = cmd;
  7312. used = hwaddr_aton2(pos, src);
  7313. if (used < 0)
  7314. return -1;
  7315. pos += used;
  7316. while (*pos == ' ')
  7317. pos++;
  7318. len = os_strlen(pos);
  7319. if (len & 1)
  7320. return -1;
  7321. len /= 2;
  7322. buf = os_malloc(len);
  7323. if (buf == NULL)
  7324. return -1;
  7325. if (hexstr2bin(pos, buf, len) < 0) {
  7326. os_free(buf);
  7327. return -1;
  7328. }
  7329. wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
  7330. os_free(buf);
  7331. return 0;
  7332. }
  7333. static u16 ipv4_hdr_checksum(const void *buf, size_t len)
  7334. {
  7335. size_t i;
  7336. u32 sum = 0;
  7337. const u16 *pos = buf;
  7338. for (i = 0; i < len / 2; i++)
  7339. sum += *pos++;
  7340. while (sum >> 16)
  7341. sum = (sum & 0xffff) + (sum >> 16);
  7342. return sum ^ 0xffff;
  7343. }
  7344. #define HWSIM_PACKETLEN 1500
  7345. #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
  7346. static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
  7347. size_t len)
  7348. {
  7349. struct wpa_supplicant *wpa_s = ctx;
  7350. const struct ether_header *eth;
  7351. struct iphdr ip;
  7352. const u8 *pos;
  7353. unsigned int i;
  7354. if (len != HWSIM_PACKETLEN)
  7355. return;
  7356. eth = (const struct ether_header *) buf;
  7357. os_memcpy(&ip, eth + 1, sizeof(ip));
  7358. pos = &buf[sizeof(*eth) + sizeof(ip)];
  7359. if (ip.ihl != 5 || ip.version != 4 ||
  7360. ntohs(ip.tot_len) != HWSIM_IP_LEN)
  7361. return;
  7362. for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
  7363. if (*pos != (u8) i)
  7364. return;
  7365. pos++;
  7366. }
  7367. wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
  7368. MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
  7369. }
  7370. static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
  7371. char *cmd)
  7372. {
  7373. int enabled = atoi(cmd);
  7374. char *pos;
  7375. const char *ifname;
  7376. if (!enabled) {
  7377. if (wpa_s->l2_test) {
  7378. l2_packet_deinit(wpa_s->l2_test);
  7379. wpa_s->l2_test = NULL;
  7380. wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
  7381. }
  7382. return 0;
  7383. }
  7384. if (wpa_s->l2_test)
  7385. return 0;
  7386. pos = os_strstr(cmd, " ifname=");
  7387. if (pos)
  7388. ifname = pos + 8;
  7389. else
  7390. ifname = wpa_s->ifname;
  7391. wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
  7392. ETHERTYPE_IP, wpas_data_test_rx,
  7393. wpa_s, 1);
  7394. if (wpa_s->l2_test == NULL)
  7395. return -1;
  7396. wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
  7397. return 0;
  7398. }
  7399. static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
  7400. {
  7401. u8 dst[ETH_ALEN], src[ETH_ALEN];
  7402. char *pos;
  7403. int used;
  7404. long int val;
  7405. u8 tos;
  7406. u8 buf[2 + HWSIM_PACKETLEN];
  7407. struct ether_header *eth;
  7408. struct iphdr *ip;
  7409. u8 *dpos;
  7410. unsigned int i;
  7411. if (wpa_s->l2_test == NULL)
  7412. return -1;
  7413. /* format: <dst> <src> <tos> */
  7414. pos = cmd;
  7415. used = hwaddr_aton2(pos, dst);
  7416. if (used < 0)
  7417. return -1;
  7418. pos += used;
  7419. while (*pos == ' ')
  7420. pos++;
  7421. used = hwaddr_aton2(pos, src);
  7422. if (used < 0)
  7423. return -1;
  7424. pos += used;
  7425. val = strtol(pos, NULL, 0);
  7426. if (val < 0 || val > 0xff)
  7427. return -1;
  7428. tos = val;
  7429. eth = (struct ether_header *) &buf[2];
  7430. os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
  7431. os_memcpy(eth->ether_shost, src, ETH_ALEN);
  7432. eth->ether_type = htons(ETHERTYPE_IP);
  7433. ip = (struct iphdr *) (eth + 1);
  7434. os_memset(ip, 0, sizeof(*ip));
  7435. ip->ihl = 5;
  7436. ip->version = 4;
  7437. ip->ttl = 64;
  7438. ip->tos = tos;
  7439. ip->tot_len = htons(HWSIM_IP_LEN);
  7440. ip->protocol = 1;
  7441. ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
  7442. ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
  7443. ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
  7444. dpos = (u8 *) (ip + 1);
  7445. for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
  7446. *dpos++ = i;
  7447. if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
  7448. HWSIM_PACKETLEN) < 0)
  7449. return -1;
  7450. wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
  7451. " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
  7452. return 0;
  7453. }
  7454. static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
  7455. char *cmd)
  7456. {
  7457. u8 *buf;
  7458. struct ether_header *eth;
  7459. struct l2_packet_data *l2 = NULL;
  7460. size_t len;
  7461. u16 ethertype;
  7462. int res = -1;
  7463. len = os_strlen(cmd);
  7464. if (len & 1 || len < ETH_HLEN * 2)
  7465. return -1;
  7466. len /= 2;
  7467. buf = os_malloc(len);
  7468. if (buf == NULL)
  7469. return -1;
  7470. if (hexstr2bin(cmd, buf, len) < 0)
  7471. goto done;
  7472. eth = (struct ether_header *) buf;
  7473. ethertype = ntohs(eth->ether_type);
  7474. l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
  7475. wpas_data_test_rx, wpa_s, 1);
  7476. if (l2 == NULL)
  7477. goto done;
  7478. res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
  7479. wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
  7480. done:
  7481. if (l2)
  7482. l2_packet_deinit(l2);
  7483. os_free(buf);
  7484. return res < 0 ? -1 : 0;
  7485. }
  7486. static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
  7487. {
  7488. #ifdef WPA_TRACE_BFD
  7489. char *pos;
  7490. wpa_trace_fail_after = atoi(cmd);
  7491. pos = os_strchr(cmd, ':');
  7492. if (pos) {
  7493. pos++;
  7494. os_strlcpy(wpa_trace_fail_func, pos,
  7495. sizeof(wpa_trace_fail_func));
  7496. } else {
  7497. wpa_trace_fail_after = 0;
  7498. }
  7499. return 0;
  7500. #else /* WPA_TRACE_BFD */
  7501. return -1;
  7502. #endif /* WPA_TRACE_BFD */
  7503. }
  7504. static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
  7505. char *buf, size_t buflen)
  7506. {
  7507. #ifdef WPA_TRACE_BFD
  7508. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
  7509. wpa_trace_fail_func);
  7510. #else /* WPA_TRACE_BFD */
  7511. return -1;
  7512. #endif /* WPA_TRACE_BFD */
  7513. }
  7514. static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd)
  7515. {
  7516. #ifdef WPA_TRACE_BFD
  7517. char *pos;
  7518. wpa_trace_test_fail_after = atoi(cmd);
  7519. pos = os_strchr(cmd, ':');
  7520. if (pos) {
  7521. pos++;
  7522. os_strlcpy(wpa_trace_test_fail_func, pos,
  7523. sizeof(wpa_trace_test_fail_func));
  7524. } else {
  7525. wpa_trace_test_fail_after = 0;
  7526. }
  7527. return 0;
  7528. #else /* WPA_TRACE_BFD */
  7529. return -1;
  7530. #endif /* WPA_TRACE_BFD */
  7531. }
  7532. static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s,
  7533. char *buf, size_t buflen)
  7534. {
  7535. #ifdef WPA_TRACE_BFD
  7536. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
  7537. wpa_trace_test_fail_func);
  7538. #else /* WPA_TRACE_BFD */
  7539. return -1;
  7540. #endif /* WPA_TRACE_BFD */
  7541. }
  7542. static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
  7543. {
  7544. struct wpa_supplicant *wpa_s = eloop_ctx;
  7545. int i, count = (intptr_t) timeout_ctx;
  7546. wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
  7547. count);
  7548. for (i = 0; i < count; i++) {
  7549. wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
  7550. i + 1, count);
  7551. }
  7552. }
  7553. static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
  7554. {
  7555. int count;
  7556. count = atoi(cmd);
  7557. if (count <= 0)
  7558. return -1;
  7559. return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
  7560. (void *) (intptr_t) count);
  7561. }
  7562. static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
  7563. const char *cmd)
  7564. {
  7565. struct wpabuf *buf;
  7566. size_t len;
  7567. len = os_strlen(cmd);
  7568. if (len & 1)
  7569. return -1;
  7570. len /= 2;
  7571. if (len == 0) {
  7572. buf = NULL;
  7573. } else {
  7574. buf = wpabuf_alloc(len);
  7575. if (buf == NULL)
  7576. return -1;
  7577. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  7578. wpabuf_free(buf);
  7579. return -1;
  7580. }
  7581. }
  7582. wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
  7583. return 0;
  7584. }
  7585. static int wpas_ctrl_reset_pn(struct wpa_supplicant *wpa_s)
  7586. {
  7587. u8 zero[WPA_TK_MAX_LEN];
  7588. if (wpa_s->last_tk_alg == WPA_ALG_NONE)
  7589. return -1;
  7590. wpa_printf(MSG_INFO, "TESTING: Reset PN");
  7591. os_memset(zero, 0, sizeof(zero));
  7592. /* First, use a zero key to avoid any possible duplicate key avoidance
  7593. * in the driver. */
  7594. if (wpa_drv_set_key(wpa_s, wpa_s->last_tk_alg, wpa_s->last_tk_addr,
  7595. wpa_s->last_tk_key_idx, 1, zero, 6,
  7596. zero, wpa_s->last_tk_len) < 0)
  7597. return -1;
  7598. /* Set the previously configured key to reset its TSC/RSC */
  7599. return wpa_drv_set_key(wpa_s, wpa_s->last_tk_alg, wpa_s->last_tk_addr,
  7600. wpa_s->last_tk_key_idx, 1, zero, 6,
  7601. wpa_s->last_tk, wpa_s->last_tk_len);
  7602. }
  7603. static int wpas_ctrl_key_request(struct wpa_supplicant *wpa_s, const char *cmd)
  7604. {
  7605. const char *pos = cmd;
  7606. int error, pairwise;
  7607. error = atoi(pos);
  7608. pos = os_strchr(pos, ' ');
  7609. if (!pos)
  7610. return -1;
  7611. pairwise = atoi(pos);
  7612. wpa_sm_key_request(wpa_s->wpa, error, pairwise);
  7613. return 0;
  7614. }
  7615. static int wpas_ctrl_resend_assoc(struct wpa_supplicant *wpa_s)
  7616. {
  7617. #ifdef CONFIG_SME
  7618. struct wpa_driver_associate_params params;
  7619. int ret;
  7620. os_memset(&params, 0, sizeof(params));
  7621. params.bssid = wpa_s->bssid;
  7622. params.ssid = wpa_s->sme.ssid;
  7623. params.ssid_len = wpa_s->sme.ssid_len;
  7624. params.freq.freq = wpa_s->sme.freq;
  7625. if (wpa_s->last_assoc_req_wpa_ie) {
  7626. params.wpa_ie = wpabuf_head(wpa_s->last_assoc_req_wpa_ie);
  7627. params.wpa_ie_len = wpabuf_len(wpa_s->last_assoc_req_wpa_ie);
  7628. }
  7629. params.pairwise_suite = wpa_s->pairwise_cipher;
  7630. params.group_suite = wpa_s->group_cipher;
  7631. params.mgmt_group_suite = wpa_s->mgmt_group_cipher;
  7632. params.key_mgmt_suite = wpa_s->key_mgmt;
  7633. params.wpa_proto = wpa_s->wpa_proto;
  7634. params.mgmt_frame_protection = wpa_s->sme.mfp;
  7635. params.rrm_used = wpa_s->rrm.rrm_used;
  7636. if (wpa_s->sme.prev_bssid_set)
  7637. params.prev_bssid = wpa_s->sme.prev_bssid;
  7638. wpa_printf(MSG_INFO, "TESTING: Resend association request");
  7639. ret = wpa_drv_associate(wpa_s, &params);
  7640. wpa_s->testing_resend_assoc = 1;
  7641. return ret;
  7642. #else /* CONFIG_SME */
  7643. return -1;
  7644. #endif /* CONFIG_SME */
  7645. }
  7646. #endif /* CONFIG_TESTING_OPTIONS */
  7647. static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
  7648. {
  7649. char *pos = cmd;
  7650. int frame;
  7651. size_t len;
  7652. struct wpabuf *buf;
  7653. struct ieee802_11_elems elems;
  7654. frame = atoi(pos);
  7655. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  7656. return -1;
  7657. wpa_s = wpas_vendor_elem(wpa_s, frame);
  7658. pos = os_strchr(pos, ' ');
  7659. if (pos == NULL)
  7660. return -1;
  7661. pos++;
  7662. len = os_strlen(pos);
  7663. if (len == 0)
  7664. return 0;
  7665. if (len & 1)
  7666. return -1;
  7667. len /= 2;
  7668. buf = wpabuf_alloc(len);
  7669. if (buf == NULL)
  7670. return -1;
  7671. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  7672. wpabuf_free(buf);
  7673. return -1;
  7674. }
  7675. if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
  7676. ParseFailed) {
  7677. wpabuf_free(buf);
  7678. return -1;
  7679. }
  7680. if (wpa_s->vendor_elem[frame] == NULL) {
  7681. wpa_s->vendor_elem[frame] = buf;
  7682. wpas_vendor_elem_update(wpa_s);
  7683. return 0;
  7684. }
  7685. if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
  7686. wpabuf_free(buf);
  7687. return -1;
  7688. }
  7689. wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
  7690. wpabuf_free(buf);
  7691. wpas_vendor_elem_update(wpa_s);
  7692. return 0;
  7693. }
  7694. static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
  7695. char *buf, size_t buflen)
  7696. {
  7697. int frame = atoi(cmd);
  7698. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  7699. return -1;
  7700. wpa_s = wpas_vendor_elem(wpa_s, frame);
  7701. if (wpa_s->vendor_elem[frame] == NULL)
  7702. return 0;
  7703. return wpa_snprintf_hex(buf, buflen,
  7704. wpabuf_head_u8(wpa_s->vendor_elem[frame]),
  7705. wpabuf_len(wpa_s->vendor_elem[frame]));
  7706. }
  7707. static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
  7708. {
  7709. char *pos = cmd;
  7710. int frame;
  7711. size_t len;
  7712. u8 *buf;
  7713. struct ieee802_11_elems elems;
  7714. int res;
  7715. frame = atoi(pos);
  7716. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  7717. return -1;
  7718. wpa_s = wpas_vendor_elem(wpa_s, frame);
  7719. pos = os_strchr(pos, ' ');
  7720. if (pos == NULL)
  7721. return -1;
  7722. pos++;
  7723. if (*pos == '*') {
  7724. wpabuf_free(wpa_s->vendor_elem[frame]);
  7725. wpa_s->vendor_elem[frame] = NULL;
  7726. wpas_vendor_elem_update(wpa_s);
  7727. return 0;
  7728. }
  7729. if (wpa_s->vendor_elem[frame] == NULL)
  7730. return -1;
  7731. len = os_strlen(pos);
  7732. if (len == 0)
  7733. return 0;
  7734. if (len & 1)
  7735. return -1;
  7736. len /= 2;
  7737. buf = os_malloc(len);
  7738. if (buf == NULL)
  7739. return -1;
  7740. if (hexstr2bin(pos, buf, len) < 0) {
  7741. os_free(buf);
  7742. return -1;
  7743. }
  7744. if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
  7745. os_free(buf);
  7746. return -1;
  7747. }
  7748. res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
  7749. os_free(buf);
  7750. return res;
  7751. }
  7752. static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
  7753. {
  7754. struct wpa_supplicant *wpa_s = ctx;
  7755. size_t len;
  7756. const u8 *data;
  7757. /*
  7758. * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
  7759. * BSSID[6]
  7760. * BSSID Information[4]
  7761. * Operating Class[1]
  7762. * Channel Number[1]
  7763. * PHY Type[1]
  7764. * Optional Subelements[variable]
  7765. */
  7766. #define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
  7767. if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
  7768. wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
  7769. goto out;
  7770. }
  7771. data = wpabuf_head_u8(neighbor_rep);
  7772. len = wpabuf_len(neighbor_rep);
  7773. while (len >= 2 + NR_IE_MIN_LEN) {
  7774. const u8 *nr;
  7775. char lci[256 * 2 + 1];
  7776. char civic[256 * 2 + 1];
  7777. u8 nr_len = data[1];
  7778. const u8 *pos = data, *end;
  7779. if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
  7780. nr_len < NR_IE_MIN_LEN) {
  7781. wpa_printf(MSG_DEBUG,
  7782. "CTRL: Invalid Neighbor Report element: id=%u len=%u",
  7783. data[0], nr_len);
  7784. goto out;
  7785. }
  7786. if (2U + nr_len > len) {
  7787. wpa_printf(MSG_DEBUG,
  7788. "CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
  7789. data[0], len, nr_len);
  7790. goto out;
  7791. }
  7792. pos += 2;
  7793. end = pos + nr_len;
  7794. nr = pos;
  7795. pos += NR_IE_MIN_LEN;
  7796. lci[0] = '\0';
  7797. civic[0] = '\0';
  7798. while (end - pos > 2) {
  7799. u8 s_id, s_len;
  7800. s_id = *pos++;
  7801. s_len = *pos++;
  7802. if (s_len > end - pos)
  7803. goto out;
  7804. if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
  7805. /* Measurement Token[1] */
  7806. /* Measurement Report Mode[1] */
  7807. /* Measurement Type[1] */
  7808. /* Measurement Report[variable] */
  7809. switch (pos[2]) {
  7810. case MEASURE_TYPE_LCI:
  7811. if (lci[0])
  7812. break;
  7813. wpa_snprintf_hex(lci, sizeof(lci),
  7814. pos, s_len);
  7815. break;
  7816. case MEASURE_TYPE_LOCATION_CIVIC:
  7817. if (civic[0])
  7818. break;
  7819. wpa_snprintf_hex(civic, sizeof(civic),
  7820. pos, s_len);
  7821. break;
  7822. }
  7823. }
  7824. pos += s_len;
  7825. }
  7826. wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
  7827. "bssid=" MACSTR
  7828. " info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
  7829. MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
  7830. nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
  7831. nr[ETH_ALEN + 6],
  7832. lci[0] ? " lci=" : "", lci,
  7833. civic[0] ? " civic=" : "", civic);
  7834. data = end;
  7835. len -= 2 + nr_len;
  7836. }
  7837. out:
  7838. wpabuf_free(neighbor_rep);
  7839. }
  7840. static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
  7841. char *cmd)
  7842. {
  7843. struct wpa_ssid_value ssid, *ssid_p = NULL;
  7844. int ret, lci = 0, civic = 0;
  7845. char *ssid_s;
  7846. ssid_s = os_strstr(cmd, "ssid=");
  7847. if (ssid_s) {
  7848. if (ssid_parse(ssid_s + 5, &ssid)) {
  7849. wpa_printf(MSG_ERROR,
  7850. "CTRL: Send Neighbor Report: bad SSID");
  7851. return -1;
  7852. }
  7853. ssid_p = &ssid;
  7854. /*
  7855. * Move cmd after the SSID text that may include "lci" or
  7856. * "civic".
  7857. */
  7858. cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
  7859. if (cmd)
  7860. cmd++;
  7861. }
  7862. if (cmd && os_strstr(cmd, "lci"))
  7863. lci = 1;
  7864. if (cmd && os_strstr(cmd, "civic"))
  7865. civic = 1;
  7866. ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
  7867. wpas_ctrl_neighbor_rep_cb,
  7868. wpa_s);
  7869. return ret;
  7870. }
  7871. static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
  7872. {
  7873. eapol_sm_erp_flush(wpa_s->eapol);
  7874. return 0;
  7875. }
  7876. static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
  7877. char *cmd)
  7878. {
  7879. char *token, *context = NULL;
  7880. unsigned int enable = ~0, type = 0;
  7881. u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
  7882. u8 *addr = NULL, *mask = NULL;
  7883. while ((token = str_token(cmd, " ", &context))) {
  7884. if (os_strcasecmp(token, "scan") == 0) {
  7885. type |= MAC_ADDR_RAND_SCAN;
  7886. } else if (os_strcasecmp(token, "sched") == 0) {
  7887. type |= MAC_ADDR_RAND_SCHED_SCAN;
  7888. } else if (os_strcasecmp(token, "pno") == 0) {
  7889. type |= MAC_ADDR_RAND_PNO;
  7890. } else if (os_strcasecmp(token, "all") == 0) {
  7891. type = wpa_s->mac_addr_rand_supported;
  7892. } else if (os_strncasecmp(token, "enable=", 7) == 0) {
  7893. enable = atoi(token + 7);
  7894. } else if (os_strncasecmp(token, "addr=", 5) == 0) {
  7895. addr = _addr;
  7896. if (hwaddr_aton(token + 5, addr)) {
  7897. wpa_printf(MSG_INFO,
  7898. "CTRL: Invalid MAC address: %s",
  7899. token);
  7900. return -1;
  7901. }
  7902. } else if (os_strncasecmp(token, "mask=", 5) == 0) {
  7903. mask = _mask;
  7904. if (hwaddr_aton(token + 5, mask)) {
  7905. wpa_printf(MSG_INFO,
  7906. "CTRL: Invalid MAC address mask: %s",
  7907. token);
  7908. return -1;
  7909. }
  7910. } else {
  7911. wpa_printf(MSG_INFO,
  7912. "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
  7913. token);
  7914. return -1;
  7915. }
  7916. }
  7917. if (!type) {
  7918. wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
  7919. return -1;
  7920. }
  7921. if ((wpa_s->mac_addr_rand_supported & type) != type) {
  7922. wpa_printf(MSG_INFO,
  7923. "CTRL: MAC_RAND_SCAN types=%u != supported=%u",
  7924. type, wpa_s->mac_addr_rand_supported);
  7925. return -1;
  7926. }
  7927. if (enable > 1) {
  7928. wpa_printf(MSG_INFO,
  7929. "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
  7930. return -1;
  7931. }
  7932. if (!enable) {
  7933. wpas_mac_addr_rand_scan_clear(wpa_s, type);
  7934. if (wpa_s->pno) {
  7935. if (type & MAC_ADDR_RAND_PNO) {
  7936. wpas_stop_pno(wpa_s);
  7937. wpas_start_pno(wpa_s);
  7938. }
  7939. } else if (wpa_s->sched_scanning &&
  7940. (type & MAC_ADDR_RAND_SCHED_SCAN)) {
  7941. wpas_scan_restart_sched_scan(wpa_s);
  7942. }
  7943. return 0;
  7944. }
  7945. if ((addr && !mask) || (!addr && mask)) {
  7946. wpa_printf(MSG_INFO,
  7947. "CTRL: MAC_RAND_SCAN invalid addr/mask combination");
  7948. return -1;
  7949. }
  7950. if (addr && mask && (!(mask[0] & 0x01) || (addr[0] & 0x01))) {
  7951. wpa_printf(MSG_INFO,
  7952. "CTRL: MAC_RAND_SCAN cannot allow multicast address");
  7953. return -1;
  7954. }
  7955. if (type & MAC_ADDR_RAND_SCAN) {
  7956. wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCAN,
  7957. addr, mask);
  7958. }
  7959. if (type & MAC_ADDR_RAND_SCHED_SCAN) {
  7960. wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCHED_SCAN,
  7961. addr, mask);
  7962. if (wpa_s->sched_scanning && !wpa_s->pno)
  7963. wpas_scan_restart_sched_scan(wpa_s);
  7964. }
  7965. if (type & MAC_ADDR_RAND_PNO) {
  7966. wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_PNO,
  7967. addr, mask);
  7968. if (wpa_s->pno) {
  7969. wpas_stop_pno(wpa_s);
  7970. wpas_start_pno(wpa_s);
  7971. }
  7972. }
  7973. return 0;
  7974. }
  7975. static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
  7976. char *buf, size_t buflen)
  7977. {
  7978. size_t reply_len;
  7979. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
  7980. #ifdef CONFIG_AP
  7981. reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
  7982. buflen - reply_len);
  7983. #endif /* CONFIG_AP */
  7984. return reply_len;
  7985. }
  7986. static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
  7987. {
  7988. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  7989. #ifdef CONFIG_AP
  7990. wpas_ap_pmksa_cache_flush(wpa_s);
  7991. #endif /* CONFIG_AP */
  7992. }
  7993. #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
  7994. static int wpas_ctrl_iface_pmksa_get(struct wpa_supplicant *wpa_s,
  7995. const char *cmd, char *buf, size_t buflen)
  7996. {
  7997. struct rsn_pmksa_cache_entry *entry;
  7998. struct wpa_ssid *ssid;
  7999. char *pos, *pos2, *end;
  8000. int ret;
  8001. struct os_reltime now;
  8002. ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
  8003. if (!ssid)
  8004. return -1;
  8005. pos = buf;
  8006. end = buf + buflen;
  8007. os_get_reltime(&now);
  8008. /*
  8009. * Entry format:
  8010. * <BSSID> <PMKID> <PMK> <reauth_time in seconds>
  8011. * <expiration in seconds> <akmp> <opportunistic>
  8012. * [FILS Cache Identifier]
  8013. */
  8014. for (entry = wpa_sm_pmksa_cache_head(wpa_s->wpa); entry;
  8015. entry = entry->next) {
  8016. if (entry->network_ctx != ssid)
  8017. continue;
  8018. pos2 = pos;
  8019. ret = os_snprintf(pos2, end - pos2, MACSTR " ",
  8020. MAC2STR(entry->aa));
  8021. if (os_snprintf_error(end - pos2, ret))
  8022. break;
  8023. pos2 += ret;
  8024. pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmkid,
  8025. PMKID_LEN);
  8026. ret = os_snprintf(pos2, end - pos2, " ");
  8027. if (os_snprintf_error(end - pos2, ret))
  8028. break;
  8029. pos2 += ret;
  8030. pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmk,
  8031. entry->pmk_len);
  8032. ret = os_snprintf(pos2, end - pos2, " %d %d %d %d",
  8033. (int) (entry->reauth_time - now.sec),
  8034. (int) (entry->expiration - now.sec),
  8035. entry->akmp,
  8036. entry->opportunistic);
  8037. if (os_snprintf_error(end - pos2, ret))
  8038. break;
  8039. pos2 += ret;
  8040. if (entry->fils_cache_id_set) {
  8041. ret = os_snprintf(pos2, end - pos2, " %02x%02x",
  8042. entry->fils_cache_id[0],
  8043. entry->fils_cache_id[1]);
  8044. if (os_snprintf_error(end - pos2, ret))
  8045. break;
  8046. pos2 += ret;
  8047. }
  8048. ret = os_snprintf(pos2, end - pos2, "\n");
  8049. if (os_snprintf_error(end - pos2, ret))
  8050. break;
  8051. pos2 += ret;
  8052. pos = pos2;
  8053. }
  8054. return pos - buf;
  8055. }
  8056. static int wpas_ctrl_iface_pmksa_add(struct wpa_supplicant *wpa_s,
  8057. char *cmd)
  8058. {
  8059. struct rsn_pmksa_cache_entry *entry;
  8060. struct wpa_ssid *ssid;
  8061. char *pos, *pos2;
  8062. int ret = -1;
  8063. struct os_reltime now;
  8064. int reauth_time = 0, expiration = 0, i;
  8065. /*
  8066. * Entry format:
  8067. * <network_id> <BSSID> <PMKID> <PMK> <reauth_time in seconds>
  8068. * <expiration in seconds> <akmp> <opportunistic>
  8069. * [FILS Cache Identifier]
  8070. */
  8071. ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
  8072. if (!ssid)
  8073. return -1;
  8074. pos = os_strchr(cmd, ' ');
  8075. if (!pos)
  8076. return -1;
  8077. pos++;
  8078. entry = os_zalloc(sizeof(*entry));
  8079. if (!entry)
  8080. return -1;
  8081. if (hwaddr_aton(pos, entry->aa))
  8082. goto fail;
  8083. pos = os_strchr(pos, ' ');
  8084. if (!pos)
  8085. goto fail;
  8086. pos++;
  8087. if (hexstr2bin(pos, entry->pmkid, PMKID_LEN) < 0)
  8088. goto fail;
  8089. pos = os_strchr(pos, ' ');
  8090. if (!pos)
  8091. goto fail;
  8092. pos++;
  8093. pos2 = os_strchr(pos, ' ');
  8094. if (!pos2)
  8095. goto fail;
  8096. entry->pmk_len = (pos2 - pos) / 2;
  8097. if (entry->pmk_len < PMK_LEN || entry->pmk_len > PMK_LEN_MAX ||
  8098. hexstr2bin(pos, entry->pmk, entry->pmk_len) < 0)
  8099. goto fail;
  8100. pos = os_strchr(pos, ' ');
  8101. if (!pos)
  8102. goto fail;
  8103. pos++;
  8104. if (sscanf(pos, "%d %d %d %d", &reauth_time, &expiration,
  8105. &entry->akmp, &entry->opportunistic) != 4)
  8106. goto fail;
  8107. for (i = 0; i < 4; i++) {
  8108. pos = os_strchr(pos, ' ');
  8109. if (!pos) {
  8110. if (i < 3)
  8111. goto fail;
  8112. break;
  8113. }
  8114. pos++;
  8115. }
  8116. if (pos) {
  8117. if (hexstr2bin(pos, entry->fils_cache_id,
  8118. FILS_CACHE_ID_LEN) < 0)
  8119. goto fail;
  8120. entry->fils_cache_id_set = 1;
  8121. }
  8122. os_get_reltime(&now);
  8123. entry->expiration = now.sec + expiration;
  8124. entry->reauth_time = now.sec + reauth_time;
  8125. entry->network_ctx = ssid;
  8126. wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
  8127. entry = NULL;
  8128. ret = 0;
  8129. fail:
  8130. os_free(entry);
  8131. return ret;
  8132. }
  8133. #ifdef CONFIG_MESH
  8134. static int wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant *wpa_s,
  8135. const char *cmd, char *buf,
  8136. size_t buflen)
  8137. {
  8138. u8 spa[ETH_ALEN];
  8139. if (!wpa_s->ifmsh)
  8140. return -1;
  8141. if (os_strcasecmp(cmd, "any") == 0)
  8142. return wpas_ap_pmksa_cache_list_mesh(wpa_s, NULL, buf, buflen);
  8143. if (hwaddr_aton(cmd, spa))
  8144. return -1;
  8145. return wpas_ap_pmksa_cache_list_mesh(wpa_s, spa, buf, buflen);
  8146. }
  8147. static int wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant *wpa_s,
  8148. char *cmd)
  8149. {
  8150. /*
  8151. * We do not check mesh interface existance because PMKSA should be
  8152. * stored before wpa_s->ifmsh creation to suppress commit message
  8153. * creation.
  8154. */
  8155. return wpas_ap_pmksa_cache_add_external(wpa_s, cmd);
  8156. }
  8157. #endif /* CONFIG_MESH */
  8158. #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
  8159. #ifdef CONFIG_FILS
  8160. static int wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant *wpa_s,
  8161. const char *cmd)
  8162. {
  8163. struct fils_hlp_req *req;
  8164. const char *pos;
  8165. /* format: <dst> <packet starting from ethertype> */
  8166. req = os_zalloc(sizeof(*req));
  8167. if (!req)
  8168. return -1;
  8169. if (hwaddr_aton(cmd, req->dst))
  8170. goto fail;
  8171. pos = os_strchr(cmd, ' ');
  8172. if (!pos)
  8173. goto fail;
  8174. pos++;
  8175. req->pkt = wpabuf_parse_bin(pos);
  8176. if (!req->pkt)
  8177. goto fail;
  8178. dl_list_add_tail(&wpa_s->fils_hlp_req, &req->list);
  8179. return 0;
  8180. fail:
  8181. wpabuf_free(req->pkt);
  8182. os_free(req);
  8183. return -1;
  8184. }
  8185. #endif /* CONFIG_FILS */
  8186. static int wpas_ctrl_cmd_debug_level(const char *cmd)
  8187. {
  8188. if (os_strcmp(cmd, "PING") == 0 ||
  8189. os_strncmp(cmd, "BSS ", 4) == 0 ||
  8190. os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
  8191. os_strncmp(cmd, "STATUS", 6) == 0 ||
  8192. os_strncmp(cmd, "STA ", 4) == 0 ||
  8193. os_strncmp(cmd, "STA-", 4) == 0)
  8194. return MSG_EXCESSIVE;
  8195. return MSG_DEBUG;
  8196. }
  8197. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  8198. char *buf, size_t *resp_len)
  8199. {
  8200. char *reply;
  8201. const int reply_size = 4096;
  8202. int reply_len;
  8203. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  8204. os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
  8205. os_strncmp(buf, "PMKSA_ADD ", 10) == 0 ||
  8206. os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
  8207. if (wpa_debug_show_keys)
  8208. wpa_dbg(wpa_s, MSG_DEBUG,
  8209. "Control interface command '%s'", buf);
  8210. else
  8211. wpa_dbg(wpa_s, MSG_DEBUG,
  8212. "Control interface command '%s [REMOVED]'",
  8213. os_strncmp(buf, WPA_CTRL_RSP,
  8214. os_strlen(WPA_CTRL_RSP)) == 0 ?
  8215. WPA_CTRL_RSP :
  8216. (os_strncmp(buf, "SET_NETWORK ", 12) == 0 ?
  8217. "SET_NETWORK" : "key-add"));
  8218. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  8219. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
  8220. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  8221. (const u8 *) buf, os_strlen(buf));
  8222. } else {
  8223. int level = wpas_ctrl_cmd_debug_level(buf);
  8224. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  8225. }
  8226. reply = os_malloc(reply_size);
  8227. if (reply == NULL) {
  8228. *resp_len = 1;
  8229. return NULL;
  8230. }
  8231. os_memcpy(reply, "OK\n", 3);
  8232. reply_len = 3;
  8233. if (os_strcmp(buf, "PING") == 0) {
  8234. os_memcpy(reply, "PONG\n", 5);
  8235. reply_len = 5;
  8236. } else if (os_strcmp(buf, "IFNAME") == 0) {
  8237. reply_len = os_strlen(wpa_s->ifname);
  8238. os_memcpy(reply, wpa_s->ifname, reply_len);
  8239. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  8240. if (wpa_debug_reopen_file() < 0)
  8241. reply_len = -1;
  8242. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  8243. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  8244. } else if (os_strcmp(buf, "MIB") == 0) {
  8245. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  8246. if (reply_len >= 0) {
  8247. reply_len += eapol_sm_get_mib(wpa_s->eapol,
  8248. reply + reply_len,
  8249. reply_size - reply_len);
  8250. }
  8251. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  8252. reply_len = wpa_supplicant_ctrl_iface_status(
  8253. wpa_s, buf + 6, reply, reply_size);
  8254. } else if (os_strcmp(buf, "PMKSA") == 0) {
  8255. reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
  8256. } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
  8257. wpas_ctrl_iface_pmksa_flush(wpa_s);
  8258. #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
  8259. } else if (os_strncmp(buf, "PMKSA_GET ", 10) == 0) {
  8260. reply_len = wpas_ctrl_iface_pmksa_get(wpa_s, buf + 10,
  8261. reply, reply_size);
  8262. } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
  8263. if (wpas_ctrl_iface_pmksa_add(wpa_s, buf + 10) < 0)
  8264. reply_len = -1;
  8265. #ifdef CONFIG_MESH
  8266. } else if (os_strncmp(buf, "MESH_PMKSA_GET ", 15) == 0) {
  8267. reply_len = wpas_ctrl_iface_mesh_pmksa_get(wpa_s, buf + 15,
  8268. reply, reply_size);
  8269. } else if (os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
  8270. if (wpas_ctrl_iface_mesh_pmksa_add(wpa_s, buf + 15) < 0)
  8271. reply_len = -1;
  8272. #endif /* CONFIG_MESH */
  8273. #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
  8274. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  8275. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  8276. reply_len = -1;
  8277. } else if (os_strncmp(buf, "DUMP", 4) == 0) {
  8278. reply_len = wpa_config_dump_values(wpa_s->conf,
  8279. reply, reply_size);
  8280. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  8281. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  8282. reply, reply_size);
  8283. } else if (os_strcmp(buf, "LOGON") == 0) {
  8284. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  8285. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  8286. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  8287. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  8288. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  8289. reply_len = -1;
  8290. else
  8291. wpas_request_connection(wpa_s);
  8292. } else if (os_strcmp(buf, "REATTACH") == 0) {
  8293. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
  8294. !wpa_s->current_ssid)
  8295. reply_len = -1;
  8296. else {
  8297. wpa_s->reattach = 1;
  8298. wpas_request_connection(wpa_s);
  8299. }
  8300. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  8301. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  8302. reply_len = -1;
  8303. else if (wpa_s->disconnected)
  8304. wpas_request_connection(wpa_s);
  8305. #ifdef IEEE8021X_EAPOL
  8306. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  8307. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  8308. reply_len = -1;
  8309. #endif /* IEEE8021X_EAPOL */
  8310. #ifdef CONFIG_IEEE80211R
  8311. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  8312. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  8313. reply_len = -1;
  8314. #endif /* CONFIG_IEEE80211R */
  8315. #ifdef CONFIG_WPS
  8316. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  8317. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  8318. if (res == -2) {
  8319. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  8320. reply_len = 17;
  8321. } else if (res)
  8322. reply_len = -1;
  8323. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  8324. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  8325. if (res == -2) {
  8326. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  8327. reply_len = 17;
  8328. } else if (res)
  8329. reply_len = -1;
  8330. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  8331. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  8332. reply,
  8333. reply_size);
  8334. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  8335. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  8336. wpa_s, buf + 14, reply, reply_size);
  8337. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  8338. if (wpas_wps_cancel(wpa_s))
  8339. reply_len = -1;
  8340. #ifdef CONFIG_WPS_NFC
  8341. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  8342. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  8343. reply_len = -1;
  8344. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  8345. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  8346. reply_len = -1;
  8347. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  8348. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  8349. wpa_s, buf + 21, reply, reply_size);
  8350. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  8351. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  8352. wpa_s, buf + 14, reply, reply_size);
  8353. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  8354. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  8355. buf + 17))
  8356. reply_len = -1;
  8357. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  8358. reply_len = wpas_ctrl_nfc_get_handover_req(
  8359. wpa_s, buf + 21, reply, reply_size);
  8360. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  8361. reply_len = wpas_ctrl_nfc_get_handover_sel(
  8362. wpa_s, buf + 21, reply, reply_size);
  8363. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  8364. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  8365. reply_len = -1;
  8366. #endif /* CONFIG_WPS_NFC */
  8367. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  8368. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  8369. reply_len = -1;
  8370. #ifdef CONFIG_AP
  8371. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  8372. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  8373. wpa_s, buf + 11, reply, reply_size);
  8374. #endif /* CONFIG_AP */
  8375. #ifdef CONFIG_WPS_ER
  8376. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  8377. if (wpas_wps_er_start(wpa_s, NULL))
  8378. reply_len = -1;
  8379. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  8380. if (wpas_wps_er_start(wpa_s, buf + 13))
  8381. reply_len = -1;
  8382. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  8383. wpas_wps_er_stop(wpa_s);
  8384. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  8385. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  8386. reply_len = -1;
  8387. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  8388. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  8389. if (ret == -2) {
  8390. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  8391. reply_len = 17;
  8392. } else if (ret == -3) {
  8393. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  8394. reply_len = 18;
  8395. } else if (ret == -4) {
  8396. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  8397. reply_len = 20;
  8398. } else if (ret)
  8399. reply_len = -1;
  8400. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  8401. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  8402. reply_len = -1;
  8403. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  8404. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  8405. buf + 18))
  8406. reply_len = -1;
  8407. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  8408. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  8409. reply_len = -1;
  8410. #ifdef CONFIG_WPS_NFC
  8411. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  8412. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  8413. wpa_s, buf + 24, reply, reply_size);
  8414. #endif /* CONFIG_WPS_NFC */
  8415. #endif /* CONFIG_WPS_ER */
  8416. #endif /* CONFIG_WPS */
  8417. #ifdef CONFIG_IBSS_RSN
  8418. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  8419. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  8420. reply_len = -1;
  8421. #endif /* CONFIG_IBSS_RSN */
  8422. #ifdef CONFIG_MESH
  8423. } else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
  8424. reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
  8425. wpa_s, buf + 19, reply, reply_size);
  8426. } else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
  8427. reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
  8428. wpa_s, "", reply, reply_size);
  8429. } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
  8430. if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
  8431. reply_len = -1;
  8432. } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
  8433. if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
  8434. buf + 18))
  8435. reply_len = -1;
  8436. } else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
  8437. if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
  8438. reply_len = -1;
  8439. } else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
  8440. if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
  8441. reply_len = -1;
  8442. #endif /* CONFIG_MESH */
  8443. #ifdef CONFIG_P2P
  8444. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  8445. if (p2p_ctrl_find(wpa_s, buf + 8))
  8446. reply_len = -1;
  8447. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  8448. if (p2p_ctrl_find(wpa_s, ""))
  8449. reply_len = -1;
  8450. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  8451. wpas_p2p_stop_find(wpa_s);
  8452. } else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
  8453. if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
  8454. reply_len = -1;
  8455. } else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
  8456. if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
  8457. reply_len = -1;
  8458. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  8459. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  8460. reply_size);
  8461. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  8462. if (p2p_ctrl_listen(wpa_s, buf + 11))
  8463. reply_len = -1;
  8464. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  8465. if (p2p_ctrl_listen(wpa_s, ""))
  8466. reply_len = -1;
  8467. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  8468. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  8469. reply_len = -1;
  8470. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  8471. if (p2p_ctrl_group_add(wpa_s, ""))
  8472. reply_len = -1;
  8473. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  8474. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  8475. reply_len = -1;
  8476. } else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
  8477. reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
  8478. reply_size);
  8479. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  8480. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  8481. reply_len = -1;
  8482. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  8483. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  8484. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  8485. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  8486. reply_size);
  8487. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  8488. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  8489. reply_len = -1;
  8490. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  8491. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  8492. reply_len = -1;
  8493. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  8494. wpas_p2p_sd_service_update(wpa_s);
  8495. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  8496. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  8497. reply_len = -1;
  8498. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  8499. wpas_p2p_service_flush(wpa_s);
  8500. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  8501. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  8502. reply_len = -1;
  8503. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  8504. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  8505. reply_len = -1;
  8506. } else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
  8507. if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
  8508. reply_len = -1;
  8509. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  8510. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  8511. reply_len = -1;
  8512. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  8513. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  8514. reply_len = -1;
  8515. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  8516. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  8517. reply_size);
  8518. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  8519. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  8520. reply_len = -1;
  8521. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  8522. p2p_ctrl_flush(wpa_s);
  8523. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  8524. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  8525. reply_len = -1;
  8526. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  8527. if (wpas_p2p_cancel(wpa_s))
  8528. reply_len = -1;
  8529. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  8530. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  8531. reply_len = -1;
  8532. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  8533. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  8534. reply_len = -1;
  8535. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  8536. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  8537. reply_len = -1;
  8538. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  8539. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  8540. reply_len = -1;
  8541. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  8542. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  8543. reply_len = -1;
  8544. } else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
  8545. if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
  8546. reply_len = -1;
  8547. } else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
  8548. if (wpas_p2p_lo_stop(wpa_s))
  8549. reply_len = -1;
  8550. #endif /* CONFIG_P2P */
  8551. #ifdef CONFIG_WIFI_DISPLAY
  8552. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  8553. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  8554. reply_len = -1;
  8555. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  8556. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  8557. reply, reply_size);
  8558. #endif /* CONFIG_WIFI_DISPLAY */
  8559. #ifdef CONFIG_INTERWORKING
  8560. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  8561. if (interworking_fetch_anqp(wpa_s) < 0)
  8562. reply_len = -1;
  8563. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  8564. interworking_stop_fetch_anqp(wpa_s);
  8565. } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
  8566. if (ctrl_interworking_select(wpa_s, NULL) < 0)
  8567. reply_len = -1;
  8568. } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
  8569. if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
  8570. reply_len = -1;
  8571. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  8572. if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
  8573. reply_len = -1;
  8574. } else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
  8575. int id;
  8576. id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
  8577. if (id < 0)
  8578. reply_len = -1;
  8579. else {
  8580. reply_len = os_snprintf(reply, reply_size, "%d\n", id);
  8581. if (os_snprintf_error(reply_size, reply_len))
  8582. reply_len = -1;
  8583. }
  8584. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  8585. if (get_anqp(wpa_s, buf + 9) < 0)
  8586. reply_len = -1;
  8587. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  8588. if (gas_request(wpa_s, buf + 12) < 0)
  8589. reply_len = -1;
  8590. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  8591. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  8592. reply_size);
  8593. #endif /* CONFIG_INTERWORKING */
  8594. #ifdef CONFIG_HS20
  8595. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  8596. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  8597. reply_len = -1;
  8598. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  8599. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  8600. reply_len = -1;
  8601. } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
  8602. if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
  8603. reply_len = -1;
  8604. } else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
  8605. if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
  8606. reply_len = -1;
  8607. } else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
  8608. reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
  8609. } else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
  8610. if (del_hs20_icon(wpa_s, buf + 14) < 0)
  8611. reply_len = -1;
  8612. } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
  8613. if (hs20_fetch_osu(wpa_s, 0) < 0)
  8614. reply_len = -1;
  8615. } else if (os_strcmp(buf, "FETCH_OSU no-scan") == 0) {
  8616. if (hs20_fetch_osu(wpa_s, 1) < 0)
  8617. reply_len = -1;
  8618. } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
  8619. hs20_cancel_fetch_osu(wpa_s);
  8620. #endif /* CONFIG_HS20 */
  8621. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  8622. {
  8623. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  8624. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  8625. reply_len = -1;
  8626. else {
  8627. /*
  8628. * Notify response from timeout to allow the control
  8629. * interface response to be sent first.
  8630. */
  8631. eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
  8632. wpa_s, NULL);
  8633. }
  8634. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  8635. if (wpa_supplicant_reload_configuration(wpa_s))
  8636. reply_len = -1;
  8637. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  8638. wpa_supplicant_terminate_proc(wpa_s->global);
  8639. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  8640. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  8641. reply_len = -1;
  8642. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  8643. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  8644. wpa_s, buf + 9, reply, reply_size);
  8645. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  8646. reply_len = wpa_supplicant_ctrl_iface_log_level(
  8647. wpa_s, buf + 9, reply, reply_size);
  8648. } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
  8649. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  8650. wpa_s, buf + 14, reply, reply_size);
  8651. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  8652. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  8653. wpa_s, NULL, reply, reply_size);
  8654. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  8655. wpas_request_disconnection(wpa_s);
  8656. } else if (os_strcmp(buf, "SCAN") == 0) {
  8657. wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
  8658. } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
  8659. wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
  8660. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  8661. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  8662. wpa_s, reply, reply_size);
  8663. } else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
  8664. if (wpas_abort_ongoing_scan(wpa_s) < 0)
  8665. reply_len = -1;
  8666. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  8667. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  8668. reply_len = -1;
  8669. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  8670. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  8671. reply_len = -1;
  8672. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  8673. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  8674. reply_len = -1;
  8675. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  8676. reply_len = wpa_supplicant_ctrl_iface_add_network(
  8677. wpa_s, reply, reply_size);
  8678. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  8679. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  8680. reply_len = -1;
  8681. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  8682. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  8683. reply_len = -1;
  8684. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  8685. reply_len = wpa_supplicant_ctrl_iface_get_network(
  8686. wpa_s, buf + 12, reply, reply_size);
  8687. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  8688. if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
  8689. wpa_s))
  8690. reply_len = -1;
  8691. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  8692. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  8693. wpa_s, reply, reply_size);
  8694. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  8695. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  8696. wpa_s, reply, reply_size);
  8697. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  8698. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  8699. reply_len = -1;
  8700. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  8701. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  8702. reply_len = -1;
  8703. } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
  8704. reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
  8705. reply,
  8706. reply_size);
  8707. #ifndef CONFIG_NO_CONFIG_WRITE
  8708. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  8709. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  8710. reply_len = -1;
  8711. #endif /* CONFIG_NO_CONFIG_WRITE */
  8712. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  8713. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  8714. wpa_s, buf + 15, reply, reply_size);
  8715. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  8716. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  8717. reply_len = -1;
  8718. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  8719. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  8720. reply_len = -1;
  8721. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  8722. reply_len = wpa_supplicant_global_iface_list(
  8723. wpa_s->global, reply, reply_size);
  8724. } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
  8725. reply_len = wpa_supplicant_global_iface_interfaces(
  8726. wpa_s->global, buf + 10, reply, reply_size);
  8727. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  8728. reply_len = wpa_supplicant_ctrl_iface_bss(
  8729. wpa_s, buf + 4, reply, reply_size);
  8730. #ifdef CONFIG_AP
  8731. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  8732. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  8733. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  8734. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  8735. reply_size);
  8736. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  8737. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  8738. reply_size);
  8739. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  8740. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  8741. reply_len = -1;
  8742. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  8743. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  8744. reply_len = -1;
  8745. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  8746. if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
  8747. reply_len = -1;
  8748. } else if (os_strcmp(buf, "STOP_AP") == 0) {
  8749. if (wpas_ap_stop_ap(wpa_s))
  8750. reply_len = -1;
  8751. #endif /* CONFIG_AP */
  8752. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  8753. wpas_notify_suspend(wpa_s->global);
  8754. } else if (os_strcmp(buf, "RESUME") == 0) {
  8755. wpas_notify_resume(wpa_s->global);
  8756. #ifdef CONFIG_TESTING_OPTIONS
  8757. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  8758. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  8759. #endif /* CONFIG_TESTING_OPTIONS */
  8760. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  8761. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  8762. reply_len = -1;
  8763. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  8764. wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
  8765. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  8766. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  8767. reply_len = -1;
  8768. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  8769. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  8770. buf + 17))
  8771. reply_len = -1;
  8772. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  8773. wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
  8774. #ifdef CONFIG_TDLS
  8775. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  8776. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  8777. reply_len = -1;
  8778. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  8779. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  8780. reply_len = -1;
  8781. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  8782. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  8783. reply_len = -1;
  8784. } else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
  8785. if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
  8786. buf + 17))
  8787. reply_len = -1;
  8788. } else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
  8789. if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
  8790. buf + 24))
  8791. reply_len = -1;
  8792. } else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
  8793. reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
  8794. wpa_s, buf + 17, reply, reply_size);
  8795. #endif /* CONFIG_TDLS */
  8796. } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
  8797. reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
  8798. } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
  8799. if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
  8800. reply_len = -1;
  8801. } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
  8802. if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
  8803. reply_len = -1;
  8804. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  8805. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  8806. reply_size);
  8807. } else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
  8808. if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
  8809. reply_len = -1;
  8810. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  8811. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  8812. reply_size);
  8813. #ifdef CONFIG_AUTOSCAN
  8814. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  8815. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  8816. reply_len = -1;
  8817. #endif /* CONFIG_AUTOSCAN */
  8818. } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
  8819. reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
  8820. reply_size);
  8821. #ifdef ANDROID
  8822. } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
  8823. reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
  8824. reply_size);
  8825. #endif /* ANDROID */
  8826. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  8827. reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
  8828. reply_size);
  8829. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  8830. pmksa_cache_clear_current(wpa_s->wpa);
  8831. eapol_sm_request_reauth(wpa_s->eapol);
  8832. #ifdef CONFIG_WNM
  8833. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  8834. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  8835. reply_len = -1;
  8836. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
  8837. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
  8838. reply_len = -1;
  8839. #endif /* CONFIG_WNM */
  8840. } else if (os_strcmp(buf, "FLUSH") == 0) {
  8841. wpa_supplicant_ctrl_iface_flush(wpa_s);
  8842. } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
  8843. reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
  8844. reply_size);
  8845. #ifdef CONFIG_TESTING_OPTIONS
  8846. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  8847. if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
  8848. reply_len = -1;
  8849. } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
  8850. wpas_ctrl_iface_mgmt_tx_done(wpa_s);
  8851. } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
  8852. if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
  8853. reply_len = -1;
  8854. } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
  8855. if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
  8856. reply_len = -1;
  8857. } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
  8858. if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
  8859. reply_len = -1;
  8860. } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
  8861. if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
  8862. reply_len = -1;
  8863. } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
  8864. if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
  8865. reply_len = -1;
  8866. } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
  8867. if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
  8868. reply_len = -1;
  8869. } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
  8870. if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
  8871. reply_len = -1;
  8872. } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
  8873. reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
  8874. } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
  8875. if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0)
  8876. reply_len = -1;
  8877. } else if (os_strcmp(buf, "GET_FAIL") == 0) {
  8878. reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size);
  8879. } else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
  8880. if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
  8881. reply_len = -1;
  8882. } else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
  8883. if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
  8884. reply_len = -1;
  8885. } else if (os_strcmp(buf, "RESET_PN") == 0) {
  8886. if (wpas_ctrl_reset_pn(wpa_s) < 0)
  8887. reply_len = -1;
  8888. } else if (os_strncmp(buf, "KEY_REQUEST ", 12) == 0) {
  8889. if (wpas_ctrl_key_request(wpa_s, buf + 12) < 0)
  8890. reply_len = -1;
  8891. } else if (os_strcmp(buf, "RESEND_ASSOC") == 0) {
  8892. if (wpas_ctrl_resend_assoc(wpa_s) < 0)
  8893. reply_len = -1;
  8894. #endif /* CONFIG_TESTING_OPTIONS */
  8895. } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
  8896. if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
  8897. reply_len = -1;
  8898. } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
  8899. reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
  8900. reply_size);
  8901. } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
  8902. if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
  8903. reply_len = -1;
  8904. } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
  8905. if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
  8906. reply_len = -1;
  8907. } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
  8908. wpas_ctrl_iface_erp_flush(wpa_s);
  8909. } else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
  8910. if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
  8911. reply_len = -1;
  8912. } else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
  8913. reply_len = wpas_ctrl_iface_get_pref_freq_list(
  8914. wpa_s, buf + 19, reply, reply_size);
  8915. #ifdef CONFIG_FILS
  8916. } else if (os_strncmp(buf, "FILS_HLP_REQ_ADD ", 17) == 0) {
  8917. if (wpas_ctrl_iface_fils_hlp_req_add(wpa_s, buf + 17))
  8918. reply_len = -1;
  8919. } else if (os_strcmp(buf, "FILS_HLP_REQ_FLUSH") == 0) {
  8920. wpas_flush_fils_hlp_req(wpa_s);
  8921. #endif /* CONFIG_FILS */
  8922. #ifdef CONFIG_DPP
  8923. } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
  8924. int res;
  8925. res = wpas_dpp_qr_code(wpa_s, buf + 12);
  8926. if (res < 0) {
  8927. reply_len = -1;
  8928. } else {
  8929. reply_len = os_snprintf(reply, reply_size, "%d", res);
  8930. if (os_snprintf_error(reply_size, reply_len))
  8931. reply_len = -1;
  8932. }
  8933. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
  8934. int res;
  8935. res = wpas_dpp_bootstrap_gen(wpa_s, buf + 18);
  8936. if (res < 0) {
  8937. reply_len = -1;
  8938. } else {
  8939. reply_len = os_snprintf(reply, reply_size, "%d", res);
  8940. if (os_snprintf_error(reply_size, reply_len))
  8941. reply_len = -1;
  8942. }
  8943. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
  8944. if (wpas_dpp_bootstrap_remove(wpa_s, buf + 21) < 0)
  8945. reply_len = -1;
  8946. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
  8947. const char *uri;
  8948. uri = wpas_dpp_bootstrap_get_uri(wpa_s, atoi(buf + 22));
  8949. if (!uri) {
  8950. reply_len = -1;
  8951. } else {
  8952. reply_len = os_snprintf(reply, reply_size, "%s", uri);
  8953. if (os_snprintf_error(reply_size, reply_len))
  8954. reply_len = -1;
  8955. }
  8956. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
  8957. reply_len = wpas_dpp_bootstrap_info(wpa_s, atoi(buf + 19),
  8958. reply, reply_size);
  8959. } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
  8960. if (wpas_dpp_auth_init(wpa_s, buf + 13) < 0)
  8961. reply_len = -1;
  8962. } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
  8963. if (wpas_dpp_listen(wpa_s, buf + 11) < 0)
  8964. reply_len = -1;
  8965. } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
  8966. wpas_dpp_stop(wpa_s);
  8967. wpas_dpp_listen_stop(wpa_s);
  8968. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
  8969. int res;
  8970. res = wpas_dpp_configurator_add(wpa_s, buf + 20);
  8971. if (res < 0) {
  8972. reply_len = -1;
  8973. } else {
  8974. reply_len = os_snprintf(reply, reply_size, "%d", res);
  8975. if (os_snprintf_error(reply_size, reply_len))
  8976. reply_len = -1;
  8977. }
  8978. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
  8979. if (wpas_dpp_configurator_remove(wpa_s, buf + 24) < 0)
  8980. reply_len = -1;
  8981. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
  8982. if (wpas_dpp_configurator_sign(wpa_s, buf + 22) < 0)
  8983. reply_len = -1;
  8984. } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
  8985. int res;
  8986. res = wpas_dpp_pkex_add(wpa_s, buf + 12);
  8987. if (res < 0) {
  8988. reply_len = -1;
  8989. } else {
  8990. reply_len = os_snprintf(reply, reply_size, "%d", res);
  8991. if (os_snprintf_error(reply_size, reply_len))
  8992. reply_len = -1;
  8993. }
  8994. } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
  8995. if (wpas_dpp_pkex_remove(wpa_s, buf + 16) < 0)
  8996. reply_len = -1;
  8997. #endif /* CONFIG_DPP */
  8998. } else {
  8999. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  9000. reply_len = 16;
  9001. }
  9002. if (reply_len < 0) {
  9003. os_memcpy(reply, "FAIL\n", 5);
  9004. reply_len = 5;
  9005. }
  9006. *resp_len = reply_len;
  9007. return reply;
  9008. }
  9009. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  9010. char *cmd)
  9011. {
  9012. struct wpa_interface iface;
  9013. char *pos, *extra;
  9014. struct wpa_supplicant *wpa_s;
  9015. unsigned int create_iface = 0;
  9016. u8 mac_addr[ETH_ALEN];
  9017. enum wpa_driver_if_type type = WPA_IF_STATION;
  9018. /*
  9019. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  9020. * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
  9021. */
  9022. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  9023. os_memset(&iface, 0, sizeof(iface));
  9024. do {
  9025. iface.ifname = pos = cmd;
  9026. pos = os_strchr(pos, '\t');
  9027. if (pos)
  9028. *pos++ = '\0';
  9029. if (iface.ifname[0] == '\0')
  9030. return -1;
  9031. if (pos == NULL)
  9032. break;
  9033. iface.confname = pos;
  9034. pos = os_strchr(pos, '\t');
  9035. if (pos)
  9036. *pos++ = '\0';
  9037. if (iface.confname[0] == '\0')
  9038. iface.confname = NULL;
  9039. if (pos == NULL)
  9040. break;
  9041. iface.driver = pos;
  9042. pos = os_strchr(pos, '\t');
  9043. if (pos)
  9044. *pos++ = '\0';
  9045. if (iface.driver[0] == '\0')
  9046. iface.driver = NULL;
  9047. if (pos == NULL)
  9048. break;
  9049. iface.ctrl_interface = pos;
  9050. pos = os_strchr(pos, '\t');
  9051. if (pos)
  9052. *pos++ = '\0';
  9053. if (iface.ctrl_interface[0] == '\0')
  9054. iface.ctrl_interface = NULL;
  9055. if (pos == NULL)
  9056. break;
  9057. iface.driver_param = pos;
  9058. pos = os_strchr(pos, '\t');
  9059. if (pos)
  9060. *pos++ = '\0';
  9061. if (iface.driver_param[0] == '\0')
  9062. iface.driver_param = NULL;
  9063. if (pos == NULL)
  9064. break;
  9065. iface.bridge_ifname = pos;
  9066. pos = os_strchr(pos, '\t');
  9067. if (pos)
  9068. *pos++ = '\0';
  9069. if (iface.bridge_ifname[0] == '\0')
  9070. iface.bridge_ifname = NULL;
  9071. if (pos == NULL)
  9072. break;
  9073. extra = pos;
  9074. pos = os_strchr(pos, '\t');
  9075. if (pos)
  9076. *pos++ = '\0';
  9077. if (!extra[0])
  9078. break;
  9079. if (os_strcmp(extra, "create") == 0) {
  9080. create_iface = 1;
  9081. if (!pos)
  9082. break;
  9083. if (os_strcmp(pos, "sta") == 0) {
  9084. type = WPA_IF_STATION;
  9085. } else if (os_strcmp(pos, "ap") == 0) {
  9086. type = WPA_IF_AP_BSS;
  9087. } else {
  9088. wpa_printf(MSG_DEBUG,
  9089. "INTERFACE_ADD unsupported interface type: '%s'",
  9090. pos);
  9091. return -1;
  9092. }
  9093. } else {
  9094. wpa_printf(MSG_DEBUG,
  9095. "INTERFACE_ADD unsupported extra parameter: '%s'",
  9096. extra);
  9097. return -1;
  9098. }
  9099. } while (0);
  9100. if (create_iface) {
  9101. wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
  9102. iface.ifname);
  9103. if (!global->ifaces)
  9104. return -1;
  9105. if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
  9106. NULL, NULL, NULL, mac_addr, NULL) < 0) {
  9107. wpa_printf(MSG_ERROR,
  9108. "CTRL_IFACE interface creation failed");
  9109. return -1;
  9110. }
  9111. wpa_printf(MSG_DEBUG,
  9112. "CTRL_IFACE interface '%s' created with MAC addr: "
  9113. MACSTR, iface.ifname, MAC2STR(mac_addr));
  9114. }
  9115. if (wpa_supplicant_get_iface(global, iface.ifname))
  9116. goto fail;
  9117. wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
  9118. if (!wpa_s)
  9119. goto fail;
  9120. wpa_s->added_vif = create_iface;
  9121. return 0;
  9122. fail:
  9123. if (create_iface)
  9124. wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
  9125. return -1;
  9126. }
  9127. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  9128. char *cmd)
  9129. {
  9130. struct wpa_supplicant *wpa_s;
  9131. int ret;
  9132. unsigned int delete_iface;
  9133. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  9134. wpa_s = wpa_supplicant_get_iface(global, cmd);
  9135. if (wpa_s == NULL)
  9136. return -1;
  9137. delete_iface = wpa_s->added_vif;
  9138. ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
  9139. if (!ret && delete_iface) {
  9140. wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
  9141. cmd);
  9142. ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
  9143. }
  9144. return ret;
  9145. }
  9146. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  9147. {
  9148. struct wpa_interface_info *prev;
  9149. while (iface) {
  9150. prev = iface;
  9151. iface = iface->next;
  9152. os_free(prev->ifname);
  9153. os_free(prev->desc);
  9154. os_free(prev);
  9155. }
  9156. }
  9157. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  9158. char *buf, int len)
  9159. {
  9160. int i, res;
  9161. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  9162. char *pos, *end;
  9163. for (i = 0; wpa_drivers[i]; i++) {
  9164. const struct wpa_driver_ops *drv = wpa_drivers[i];
  9165. if (drv->get_interfaces == NULL)
  9166. continue;
  9167. tmp = drv->get_interfaces(global->drv_priv[i]);
  9168. if (tmp == NULL)
  9169. continue;
  9170. if (last == NULL)
  9171. iface = last = tmp;
  9172. else
  9173. last->next = tmp;
  9174. while (last->next)
  9175. last = last->next;
  9176. }
  9177. pos = buf;
  9178. end = buf + len;
  9179. for (tmp = iface; tmp; tmp = tmp->next) {
  9180. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  9181. tmp->drv_name, tmp->ifname,
  9182. tmp->desc ? tmp->desc : "");
  9183. if (os_snprintf_error(end - pos, res)) {
  9184. *pos = '\0';
  9185. break;
  9186. }
  9187. pos += res;
  9188. }
  9189. wpa_free_iface_info(iface);
  9190. return pos - buf;
  9191. }
  9192. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  9193. const char *input,
  9194. char *buf, int len)
  9195. {
  9196. int res;
  9197. char *pos, *end;
  9198. struct wpa_supplicant *wpa_s;
  9199. int show_ctrl = 0;
  9200. if (input)
  9201. show_ctrl = !!os_strstr(input, "ctrl");
  9202. wpa_s = global->ifaces;
  9203. pos = buf;
  9204. end = buf + len;
  9205. while (wpa_s) {
  9206. if (show_ctrl)
  9207. res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
  9208. wpa_s->ifname,
  9209. wpa_s->conf->ctrl_interface ?
  9210. wpa_s->conf->ctrl_interface : "N/A");
  9211. else
  9212. res = os_snprintf(pos, end - pos, "%s\n",
  9213. wpa_s->ifname);
  9214. if (os_snprintf_error(end - pos, res)) {
  9215. *pos = '\0';
  9216. break;
  9217. }
  9218. pos += res;
  9219. wpa_s = wpa_s->next;
  9220. }
  9221. return pos - buf;
  9222. }
  9223. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  9224. const char *ifname,
  9225. char *cmd, size_t *resp_len)
  9226. {
  9227. struct wpa_supplicant *wpa_s;
  9228. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  9229. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  9230. break;
  9231. }
  9232. if (wpa_s == NULL) {
  9233. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  9234. if (resp)
  9235. *resp_len = os_strlen(resp);
  9236. else
  9237. *resp_len = 1;
  9238. return resp;
  9239. }
  9240. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  9241. }
  9242. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  9243. char *buf, size_t *resp_len)
  9244. {
  9245. #ifdef CONFIG_P2P
  9246. static const char * cmd[] = {
  9247. "LIST_NETWORKS",
  9248. "P2P_FIND",
  9249. "P2P_STOP_FIND",
  9250. "P2P_LISTEN",
  9251. "P2P_GROUP_ADD",
  9252. "P2P_GET_PASSPHRASE",
  9253. "P2P_SERVICE_UPDATE",
  9254. "P2P_SERVICE_FLUSH",
  9255. "P2P_FLUSH",
  9256. "P2P_CANCEL",
  9257. "P2P_PRESENCE_REQ",
  9258. "P2P_EXT_LISTEN",
  9259. #ifdef CONFIG_AP
  9260. "STA-FIRST",
  9261. #endif /* CONFIG_AP */
  9262. NULL
  9263. };
  9264. static const char * prefix[] = {
  9265. #ifdef ANDROID
  9266. "DRIVER ",
  9267. #endif /* ANDROID */
  9268. "GET_CAPABILITY ",
  9269. "GET_NETWORK ",
  9270. "REMOVE_NETWORK ",
  9271. "P2P_FIND ",
  9272. "P2P_CONNECT ",
  9273. "P2P_LISTEN ",
  9274. "P2P_GROUP_REMOVE ",
  9275. "P2P_GROUP_ADD ",
  9276. "P2P_GROUP_MEMBER ",
  9277. "P2P_PROV_DISC ",
  9278. "P2P_SERV_DISC_REQ ",
  9279. "P2P_SERV_DISC_CANCEL_REQ ",
  9280. "P2P_SERV_DISC_RESP ",
  9281. "P2P_SERV_DISC_EXTERNAL ",
  9282. "P2P_SERVICE_ADD ",
  9283. "P2P_SERVICE_DEL ",
  9284. "P2P_SERVICE_REP ",
  9285. "P2P_REJECT ",
  9286. "P2P_INVITE ",
  9287. "P2P_PEER ",
  9288. "P2P_SET ",
  9289. "P2P_UNAUTHORIZE ",
  9290. "P2P_PRESENCE_REQ ",
  9291. "P2P_EXT_LISTEN ",
  9292. "P2P_REMOVE_CLIENT ",
  9293. "WPS_NFC_TOKEN ",
  9294. "WPS_NFC_TAG_READ ",
  9295. "NFC_GET_HANDOVER_SEL ",
  9296. "NFC_GET_HANDOVER_REQ ",
  9297. "NFC_REPORT_HANDOVER ",
  9298. "P2P_ASP_PROVISION ",
  9299. "P2P_ASP_PROVISION_RESP ",
  9300. #ifdef CONFIG_AP
  9301. "STA ",
  9302. "STA-NEXT ",
  9303. #endif /* CONFIG_AP */
  9304. NULL
  9305. };
  9306. int found = 0;
  9307. int i;
  9308. if (global->p2p_init_wpa_s == NULL)
  9309. return NULL;
  9310. for (i = 0; !found && cmd[i]; i++) {
  9311. if (os_strcmp(buf, cmd[i]) == 0)
  9312. found = 1;
  9313. }
  9314. for (i = 0; !found && prefix[i]; i++) {
  9315. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  9316. found = 1;
  9317. }
  9318. if (found)
  9319. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  9320. buf, resp_len);
  9321. #endif /* CONFIG_P2P */
  9322. return NULL;
  9323. }
  9324. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  9325. char *buf, size_t *resp_len)
  9326. {
  9327. #ifdef CONFIG_WIFI_DISPLAY
  9328. if (global->p2p_init_wpa_s == NULL)
  9329. return NULL;
  9330. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  9331. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  9332. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  9333. buf, resp_len);
  9334. #endif /* CONFIG_WIFI_DISPLAY */
  9335. return NULL;
  9336. }
  9337. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  9338. char *buf, size_t *resp_len)
  9339. {
  9340. char *ret;
  9341. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  9342. if (ret)
  9343. return ret;
  9344. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  9345. if (ret)
  9346. return ret;
  9347. return NULL;
  9348. }
  9349. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  9350. {
  9351. char *value;
  9352. value = os_strchr(cmd, ' ');
  9353. if (value == NULL)
  9354. return -1;
  9355. *value++ = '\0';
  9356. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  9357. #ifdef CONFIG_WIFI_DISPLAY
  9358. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  9359. wifi_display_enable(global, !!atoi(value));
  9360. return 0;
  9361. }
  9362. #endif /* CONFIG_WIFI_DISPLAY */
  9363. /* Restore cmd to its original value to allow redirection */
  9364. value[-1] = ' ';
  9365. return -1;
  9366. }
  9367. static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
  9368. char *cmd)
  9369. {
  9370. struct wpa_supplicant *wpa_s[2]; /* src, dst */
  9371. char *p;
  9372. unsigned int i;
  9373. /* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
  9374. * <variable name> */
  9375. for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
  9376. p = os_strchr(cmd, ' ');
  9377. if (p == NULL)
  9378. return -1;
  9379. *p = '\0';
  9380. wpa_s[i] = global->ifaces;
  9381. for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
  9382. if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
  9383. break;
  9384. }
  9385. if (!wpa_s[i]) {
  9386. wpa_printf(MSG_DEBUG,
  9387. "CTRL_IFACE: Could not find iface=%s", cmd);
  9388. return -1;
  9389. }
  9390. cmd = p + 1;
  9391. }
  9392. return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
  9393. }
  9394. #ifndef CONFIG_NO_CONFIG_WRITE
  9395. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  9396. {
  9397. int ret = 0, saved = 0;
  9398. struct wpa_supplicant *wpa_s;
  9399. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  9400. if (!wpa_s->conf->update_config) {
  9401. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  9402. continue;
  9403. }
  9404. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  9405. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  9406. ret = 1;
  9407. } else {
  9408. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  9409. saved++;
  9410. }
  9411. }
  9412. if (!saved && !ret) {
  9413. wpa_dbg(wpa_s, MSG_DEBUG,
  9414. "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
  9415. ret = 1;
  9416. }
  9417. return ret;
  9418. }
  9419. #endif /* CONFIG_NO_CONFIG_WRITE */
  9420. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  9421. char *buf, size_t buflen)
  9422. {
  9423. char *pos, *end;
  9424. int ret;
  9425. struct wpa_supplicant *wpa_s;
  9426. pos = buf;
  9427. end = buf + buflen;
  9428. #ifdef CONFIG_P2P
  9429. if (global->p2p && !global->p2p_disabled) {
  9430. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  9431. "\n"
  9432. "p2p_state=%s\n",
  9433. MAC2STR(global->p2p_dev_addr),
  9434. p2p_get_state_txt(global->p2p));
  9435. if (os_snprintf_error(end - pos, ret))
  9436. return pos - buf;
  9437. pos += ret;
  9438. } else if (global->p2p) {
  9439. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  9440. if (os_snprintf_error(end - pos, ret))
  9441. return pos - buf;
  9442. pos += ret;
  9443. }
  9444. #endif /* CONFIG_P2P */
  9445. #ifdef CONFIG_WIFI_DISPLAY
  9446. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  9447. !!global->wifi_display);
  9448. if (os_snprintf_error(end - pos, ret))
  9449. return pos - buf;
  9450. pos += ret;
  9451. #endif /* CONFIG_WIFI_DISPLAY */
  9452. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  9453. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  9454. "address=" MACSTR "\n",
  9455. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  9456. if (os_snprintf_error(end - pos, ret))
  9457. return pos - buf;
  9458. pos += ret;
  9459. }
  9460. return pos - buf;
  9461. }
  9462. #ifdef CONFIG_FST
  9463. static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
  9464. char *cmd, char *buf,
  9465. size_t reply_size)
  9466. {
  9467. char ifname[IFNAMSIZ + 1];
  9468. struct fst_iface_cfg cfg;
  9469. struct wpa_supplicant *wpa_s;
  9470. struct fst_wpa_obj iface_obj;
  9471. if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
  9472. wpa_s = wpa_supplicant_get_iface(global, ifname);
  9473. if (wpa_s) {
  9474. if (wpa_s->fst) {
  9475. wpa_printf(MSG_INFO, "FST: Already attached");
  9476. return -1;
  9477. }
  9478. fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
  9479. wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
  9480. &iface_obj, &cfg);
  9481. if (wpa_s->fst)
  9482. return os_snprintf(buf, reply_size, "OK\n");
  9483. }
  9484. }
  9485. return -1;
  9486. }
  9487. static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
  9488. char *cmd, char *buf,
  9489. size_t reply_size)
  9490. {
  9491. char ifname[IFNAMSIZ + 1];
  9492. struct wpa_supplicant *wpa_s;
  9493. if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
  9494. wpa_s = wpa_supplicant_get_iface(global, ifname);
  9495. if (wpa_s) {
  9496. if (!fst_iface_detach(ifname)) {
  9497. wpa_s->fst = NULL;
  9498. return os_snprintf(buf, reply_size, "OK\n");
  9499. }
  9500. }
  9501. }
  9502. return -1;
  9503. }
  9504. #endif /* CONFIG_FST */
  9505. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  9506. char *buf, size_t *resp_len)
  9507. {
  9508. char *reply;
  9509. const int reply_size = 2048;
  9510. int reply_len;
  9511. int level = MSG_DEBUG;
  9512. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  9513. char *pos = os_strchr(buf + 7, ' ');
  9514. if (pos) {
  9515. *pos++ = '\0';
  9516. return wpas_global_ctrl_iface_ifname(global,
  9517. buf + 7, pos,
  9518. resp_len);
  9519. }
  9520. }
  9521. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  9522. if (reply)
  9523. return reply;
  9524. if (os_strcmp(buf, "PING") == 0)
  9525. level = MSG_EXCESSIVE;
  9526. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  9527. (const u8 *) buf, os_strlen(buf));
  9528. reply = os_malloc(reply_size);
  9529. if (reply == NULL) {
  9530. *resp_len = 1;
  9531. return NULL;
  9532. }
  9533. os_memcpy(reply, "OK\n", 3);
  9534. reply_len = 3;
  9535. if (os_strcmp(buf, "PING") == 0) {
  9536. os_memcpy(reply, "PONG\n", 5);
  9537. reply_len = 5;
  9538. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  9539. if (wpa_supplicant_global_iface_add(global, buf + 14))
  9540. reply_len = -1;
  9541. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  9542. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  9543. reply_len = -1;
  9544. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  9545. reply_len = wpa_supplicant_global_iface_list(
  9546. global, reply, reply_size);
  9547. } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
  9548. reply_len = wpa_supplicant_global_iface_interfaces(
  9549. global, buf + 10, reply, reply_size);
  9550. #ifdef CONFIG_FST
  9551. } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
  9552. reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
  9553. reply,
  9554. reply_size);
  9555. } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
  9556. reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
  9557. reply,
  9558. reply_size);
  9559. } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
  9560. reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
  9561. #endif /* CONFIG_FST */
  9562. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  9563. wpa_supplicant_terminate_proc(global);
  9564. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  9565. wpas_notify_suspend(global);
  9566. } else if (os_strcmp(buf, "RESUME") == 0) {
  9567. wpas_notify_resume(global);
  9568. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  9569. if (wpas_global_ctrl_iface_set(global, buf + 4)) {
  9570. #ifdef CONFIG_P2P
  9571. if (global->p2p_init_wpa_s) {
  9572. os_free(reply);
  9573. /* Check if P2P redirection would work for this
  9574. * command. */
  9575. return wpa_supplicant_ctrl_iface_process(
  9576. global->p2p_init_wpa_s,
  9577. buf, resp_len);
  9578. }
  9579. #endif /* CONFIG_P2P */
  9580. reply_len = -1;
  9581. }
  9582. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  9583. if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
  9584. reply_len = -1;
  9585. #ifndef CONFIG_NO_CONFIG_WRITE
  9586. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  9587. if (wpas_global_ctrl_iface_save_config(global))
  9588. reply_len = -1;
  9589. #endif /* CONFIG_NO_CONFIG_WRITE */
  9590. } else if (os_strcmp(buf, "STATUS") == 0) {
  9591. reply_len = wpas_global_ctrl_iface_status(global, reply,
  9592. reply_size);
  9593. #ifdef CONFIG_MODULE_TESTS
  9594. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  9595. if (wpas_module_tests() < 0)
  9596. reply_len = -1;
  9597. #endif /* CONFIG_MODULE_TESTS */
  9598. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  9599. if (wpa_debug_reopen_file() < 0)
  9600. reply_len = -1;
  9601. } else {
  9602. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  9603. reply_len = 16;
  9604. }
  9605. if (reply_len < 0) {
  9606. os_memcpy(reply, "FAIL\n", 5);
  9607. reply_len = 5;
  9608. }
  9609. *resp_len = reply_len;
  9610. return reply;
  9611. }