ctrl_iface.c 217 KB

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