ctrl_iface.c 261 KB

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