ctrl_iface.c 265 KB

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