ctrl_iface.c 232 KB

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