ctrl_iface.c 216 KB

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