ctrl_iface.c 163 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2014, 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. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "utils/uuid.h"
  12. #include "common/version.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/ieee802_11_common.h"
  15. #include "common/wpa_ctrl.h"
  16. #include "eap_peer/eap.h"
  17. #include "eapol_supp/eapol_supp_sm.h"
  18. #include "rsn_supp/wpa.h"
  19. #include "rsn_supp/preauth.h"
  20. #include "rsn_supp/pmksa_cache.h"
  21. #include "l2_packet/l2_packet.h"
  22. #include "wps/wps.h"
  23. #include "config.h"
  24. #include "wpa_supplicant_i.h"
  25. #include "driver_i.h"
  26. #include "wps_supplicant.h"
  27. #include "ibss_rsn.h"
  28. #include "ap.h"
  29. #include "p2p_supplicant.h"
  30. #include "p2p/p2p.h"
  31. #include "hs20_supplicant.h"
  32. #include "wifi_display.h"
  33. #include "notify.h"
  34. #include "bss.h"
  35. #include "scan.h"
  36. #include "ctrl_iface.h"
  37. #include "interworking.h"
  38. #include "blacklist.h"
  39. #include "autoscan.h"
  40. #include "wnm_sta.h"
  41. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  42. char *buf, int len);
  43. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  44. char *buf, int len);
  45. static int pno_start(struct wpa_supplicant *wpa_s)
  46. {
  47. int ret, interval;
  48. size_t i, num_ssid;
  49. struct wpa_ssid *ssid;
  50. struct wpa_driver_scan_params params;
  51. if (wpa_s->pno || wpa_s->pno_sched_pending)
  52. return 0;
  53. if ((wpa_s->wpa_state > WPA_SCANNING) &&
  54. (wpa_s->wpa_state <= WPA_COMPLETED)) {
  55. wpa_printf(MSG_ERROR, "PNO: In assoc process");
  56. return -EAGAIN;
  57. }
  58. if (wpa_s->wpa_state == WPA_SCANNING) {
  59. wpa_supplicant_cancel_scan(wpa_s);
  60. if (wpa_s->sched_scanning) {
  61. wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
  62. "ongoing sched scan");
  63. wpa_supplicant_cancel_sched_scan(wpa_s);
  64. wpa_s->pno_sched_pending = 1;
  65. return 0;
  66. }
  67. }
  68. os_memset(&params, 0, sizeof(params));
  69. num_ssid = 0;
  70. ssid = wpa_s->conf->ssid;
  71. while (ssid) {
  72. if (!wpas_network_disabled(wpa_s, ssid))
  73. num_ssid++;
  74. ssid = ssid->next;
  75. }
  76. if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
  77. wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
  78. "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
  79. num_ssid = WPAS_MAX_SCAN_SSIDS;
  80. }
  81. if (num_ssid == 0) {
  82. wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
  83. return -1;
  84. }
  85. params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
  86. num_ssid);
  87. if (params.filter_ssids == NULL)
  88. return -1;
  89. i = 0;
  90. ssid = wpa_s->conf->ssid;
  91. while (ssid) {
  92. if (!wpas_network_disabled(wpa_s, ssid)) {
  93. params.ssids[i].ssid = ssid->ssid;
  94. params.ssids[i].ssid_len = ssid->ssid_len;
  95. params.num_ssids++;
  96. os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
  97. ssid->ssid_len);
  98. params.filter_ssids[i].ssid_len = ssid->ssid_len;
  99. params.num_filter_ssids++;
  100. i++;
  101. if (i == num_ssid)
  102. break;
  103. }
  104. ssid = ssid->next;
  105. }
  106. if (wpa_s->conf->filter_rssi)
  107. params.filter_rssi = wpa_s->conf->filter_rssi;
  108. interval = wpa_s->conf->sched_scan_interval ?
  109. wpa_s->conf->sched_scan_interval : 10;
  110. ret = wpa_supplicant_start_sched_scan(wpa_s, &params, interval);
  111. os_free(params.filter_ssids);
  112. if (ret == 0)
  113. wpa_s->pno = 1;
  114. return ret;
  115. }
  116. static int pno_stop(struct wpa_supplicant *wpa_s)
  117. {
  118. int ret = 0;
  119. if (wpa_s->pno || wpa_s->sched_scanning) {
  120. wpa_s->pno = 0;
  121. ret = wpa_supplicant_stop_sched_scan(wpa_s);
  122. }
  123. wpa_s->pno_sched_pending = 0;
  124. if (wpa_s->wpa_state == WPA_SCANNING)
  125. wpa_supplicant_req_scan(wpa_s, 0, 0);
  126. return ret;
  127. }
  128. static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
  129. {
  130. char *pos;
  131. u8 addr[ETH_ALEN], *filter = NULL, *n;
  132. size_t count = 0;
  133. pos = val;
  134. while (pos) {
  135. if (*pos == '\0')
  136. break;
  137. if (hwaddr_aton(pos, addr)) {
  138. os_free(filter);
  139. return -1;
  140. }
  141. n = os_realloc_array(filter, count + 1, ETH_ALEN);
  142. if (n == NULL) {
  143. os_free(filter);
  144. return -1;
  145. }
  146. filter = n;
  147. os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
  148. count++;
  149. pos = os_strchr(pos, ' ');
  150. if (pos)
  151. pos++;
  152. }
  153. wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
  154. os_free(wpa_s->bssid_filter);
  155. wpa_s->bssid_filter = filter;
  156. wpa_s->bssid_filter_count = count;
  157. return 0;
  158. }
  159. static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
  160. {
  161. char *pos;
  162. u8 addr[ETH_ALEN], *bssid = NULL, *n;
  163. struct wpa_ssid_value *ssid = NULL, *ns;
  164. size_t count = 0, ssid_count = 0;
  165. struct wpa_ssid *c;
  166. /*
  167. * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
  168. * SSID_SPEC ::= ssid <SSID_HEX>
  169. * BSSID_SPEC ::= bssid <BSSID_HEX>
  170. */
  171. pos = val;
  172. while (pos) {
  173. if (*pos == '\0')
  174. break;
  175. if (os_strncmp(pos, "bssid ", 6) == 0) {
  176. int res;
  177. pos += 6;
  178. res = hwaddr_aton2(pos, addr);
  179. if (res < 0) {
  180. os_free(ssid);
  181. os_free(bssid);
  182. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  183. "BSSID value '%s'", pos);
  184. return -1;
  185. }
  186. pos += res;
  187. n = os_realloc_array(bssid, count + 1, ETH_ALEN);
  188. if (n == NULL) {
  189. os_free(ssid);
  190. os_free(bssid);
  191. return -1;
  192. }
  193. bssid = n;
  194. os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
  195. count++;
  196. } else if (os_strncmp(pos, "ssid ", 5) == 0) {
  197. char *end;
  198. pos += 5;
  199. end = pos;
  200. while (*end) {
  201. if (*end == '\0' || *end == ' ')
  202. break;
  203. end++;
  204. }
  205. ns = os_realloc_array(ssid, ssid_count + 1,
  206. sizeof(struct wpa_ssid_value));
  207. if (ns == NULL) {
  208. os_free(ssid);
  209. os_free(bssid);
  210. return -1;
  211. }
  212. ssid = ns;
  213. if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
  214. hexstr2bin(pos, ssid[ssid_count].ssid,
  215. (end - pos) / 2) < 0) {
  216. os_free(ssid);
  217. os_free(bssid);
  218. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  219. "SSID value '%s'", pos);
  220. return -1;
  221. }
  222. ssid[ssid_count].ssid_len = (end - pos) / 2;
  223. wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
  224. ssid[ssid_count].ssid,
  225. ssid[ssid_count].ssid_len);
  226. ssid_count++;
  227. pos = end;
  228. } else {
  229. wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
  230. "'%s'", pos);
  231. os_free(ssid);
  232. os_free(bssid);
  233. return -1;
  234. }
  235. pos = os_strchr(pos, ' ');
  236. if (pos)
  237. pos++;
  238. }
  239. wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
  240. os_free(wpa_s->disallow_aps_bssid);
  241. wpa_s->disallow_aps_bssid = bssid;
  242. wpa_s->disallow_aps_bssid_count = count;
  243. wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
  244. os_free(wpa_s->disallow_aps_ssid);
  245. wpa_s->disallow_aps_ssid = ssid;
  246. wpa_s->disallow_aps_ssid_count = ssid_count;
  247. if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
  248. return 0;
  249. c = wpa_s->current_ssid;
  250. if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
  251. return 0;
  252. if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
  253. !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
  254. return 0;
  255. wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
  256. "because current AP was marked disallowed");
  257. #ifdef CONFIG_SME
  258. wpa_s->sme.prev_bssid_set = 0;
  259. #endif /* CONFIG_SME */
  260. wpa_s->reassociate = 1;
  261. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  262. wpa_supplicant_req_scan(wpa_s, 0, 0);
  263. return 0;
  264. }
  265. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  266. char *cmd)
  267. {
  268. char *value;
  269. int ret = 0;
  270. value = os_strchr(cmd, ' ');
  271. if (value == NULL)
  272. return -1;
  273. *value++ = '\0';
  274. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  275. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  276. eapol_sm_configure(wpa_s->eapol,
  277. atoi(value), -1, -1, -1);
  278. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  279. eapol_sm_configure(wpa_s->eapol,
  280. -1, atoi(value), -1, -1);
  281. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  282. eapol_sm_configure(wpa_s->eapol,
  283. -1, -1, atoi(value), -1);
  284. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  285. eapol_sm_configure(wpa_s->eapol,
  286. -1, -1, -1, atoi(value));
  287. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  288. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  289. atoi(value)))
  290. ret = -1;
  291. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  292. 0) {
  293. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  294. atoi(value)))
  295. ret = -1;
  296. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  297. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  298. ret = -1;
  299. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  300. wpa_s->wps_fragment_size = atoi(value);
  301. #ifdef CONFIG_WPS_TESTING
  302. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  303. long int val;
  304. val = strtol(value, NULL, 0);
  305. if (val < 0 || val > 0xff) {
  306. ret = -1;
  307. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  308. "wps_version_number %ld", val);
  309. } else {
  310. wps_version_number = val;
  311. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  312. "version %u.%u",
  313. (wps_version_number & 0xf0) >> 4,
  314. wps_version_number & 0x0f);
  315. }
  316. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  317. wps_testing_dummy_cred = atoi(value);
  318. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  319. wps_testing_dummy_cred);
  320. } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
  321. wps_corrupt_pkhash = atoi(value);
  322. wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
  323. wps_corrupt_pkhash);
  324. #endif /* CONFIG_WPS_TESTING */
  325. } else if (os_strcasecmp(cmd, "ampdu") == 0) {
  326. if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
  327. ret = -1;
  328. #ifdef CONFIG_TDLS
  329. #ifdef CONFIG_TDLS_TESTING
  330. } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
  331. extern unsigned int tdls_testing;
  332. tdls_testing = strtol(value, NULL, 0);
  333. wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
  334. #endif /* CONFIG_TDLS_TESTING */
  335. } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
  336. int disabled = atoi(value);
  337. wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
  338. if (disabled) {
  339. if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
  340. ret = -1;
  341. } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
  342. ret = -1;
  343. wpa_tdls_enable(wpa_s->wpa, !disabled);
  344. #endif /* CONFIG_TDLS */
  345. } else if (os_strcasecmp(cmd, "pno") == 0) {
  346. if (atoi(value))
  347. ret = pno_start(wpa_s);
  348. else
  349. ret = pno_stop(wpa_s);
  350. } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
  351. int disabled = atoi(value);
  352. if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
  353. ret = -1;
  354. else if (disabled)
  355. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  356. } else if (os_strcasecmp(cmd, "uapsd") == 0) {
  357. if (os_strcmp(value, "disable") == 0)
  358. wpa_s->set_sta_uapsd = 0;
  359. else {
  360. int be, bk, vi, vo;
  361. char *pos;
  362. /* format: BE,BK,VI,VO;max SP Length */
  363. be = atoi(value);
  364. pos = os_strchr(value, ',');
  365. if (pos == NULL)
  366. return -1;
  367. pos++;
  368. bk = atoi(pos);
  369. pos = os_strchr(pos, ',');
  370. if (pos == NULL)
  371. return -1;
  372. pos++;
  373. vi = atoi(pos);
  374. pos = os_strchr(pos, ',');
  375. if (pos == NULL)
  376. return -1;
  377. pos++;
  378. vo = atoi(pos);
  379. /* ignore max SP Length for now */
  380. wpa_s->set_sta_uapsd = 1;
  381. wpa_s->sta_uapsd = 0;
  382. if (be)
  383. wpa_s->sta_uapsd |= BIT(0);
  384. if (bk)
  385. wpa_s->sta_uapsd |= BIT(1);
  386. if (vi)
  387. wpa_s->sta_uapsd |= BIT(2);
  388. if (vo)
  389. wpa_s->sta_uapsd |= BIT(3);
  390. }
  391. } else if (os_strcasecmp(cmd, "ps") == 0) {
  392. ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
  393. #ifdef CONFIG_WIFI_DISPLAY
  394. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  395. int enabled = !!atoi(value);
  396. if (enabled && !wpa_s->global->p2p)
  397. ret = -1;
  398. else
  399. wifi_display_enable(wpa_s->global, enabled);
  400. #endif /* CONFIG_WIFI_DISPLAY */
  401. } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
  402. ret = set_bssid_filter(wpa_s, value);
  403. } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
  404. ret = set_disallow_aps(wpa_s, value);
  405. } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
  406. wpa_s->no_keep_alive = !!atoi(value);
  407. } else {
  408. value[-1] = '=';
  409. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  410. if (ret == 0)
  411. wpa_supplicant_update_config(wpa_s);
  412. }
  413. return ret;
  414. }
  415. static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
  416. char *cmd, char *buf, size_t buflen)
  417. {
  418. int res = -1;
  419. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  420. if (os_strcmp(cmd, "version") == 0) {
  421. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  422. } else if (os_strcasecmp(cmd, "country") == 0) {
  423. if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
  424. res = os_snprintf(buf, buflen, "%c%c",
  425. wpa_s->conf->country[0],
  426. wpa_s->conf->country[1]);
  427. #ifdef CONFIG_WIFI_DISPLAY
  428. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  429. int enabled;
  430. if (wpa_s->global->p2p == NULL ||
  431. wpa_s->global->p2p_disabled)
  432. enabled = 0;
  433. else
  434. enabled = wpa_s->global->wifi_display;
  435. res = os_snprintf(buf, buflen, "%d", enabled);
  436. if (res < 0 || (unsigned int) res >= buflen)
  437. return -1;
  438. return res;
  439. #endif /* CONFIG_WIFI_DISPLAY */
  440. #ifdef CONFIG_TESTING_GET_GTK
  441. } else if (os_strcmp(cmd, "gtk") == 0) {
  442. if (wpa_s->last_gtk_len == 0)
  443. return -1;
  444. res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
  445. wpa_s->last_gtk_len);
  446. return res;
  447. #endif /* CONFIG_TESTING_GET_GTK */
  448. }
  449. if (res < 0 || (unsigned int) res >= buflen)
  450. return -1;
  451. return res;
  452. }
  453. #ifdef IEEE8021X_EAPOL
  454. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  455. char *addr)
  456. {
  457. u8 bssid[ETH_ALEN];
  458. struct wpa_ssid *ssid = wpa_s->current_ssid;
  459. if (hwaddr_aton(addr, bssid)) {
  460. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  461. "'%s'", addr);
  462. return -1;
  463. }
  464. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  465. rsn_preauth_deinit(wpa_s->wpa);
  466. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  467. return -1;
  468. return 0;
  469. }
  470. #endif /* IEEE8021X_EAPOL */
  471. #ifdef CONFIG_PEERKEY
  472. /* MLME-STKSTART.request(peer) */
  473. static int wpa_supplicant_ctrl_iface_stkstart(
  474. struct wpa_supplicant *wpa_s, char *addr)
  475. {
  476. u8 peer[ETH_ALEN];
  477. if (hwaddr_aton(addr, peer)) {
  478. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  479. "address '%s'", addr);
  480. return -1;
  481. }
  482. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  483. MAC2STR(peer));
  484. return wpa_sm_stkstart(wpa_s->wpa, peer);
  485. }
  486. #endif /* CONFIG_PEERKEY */
  487. #ifdef CONFIG_TDLS
  488. static int wpa_supplicant_ctrl_iface_tdls_discover(
  489. struct wpa_supplicant *wpa_s, char *addr)
  490. {
  491. u8 peer[ETH_ALEN];
  492. int ret;
  493. if (hwaddr_aton(addr, peer)) {
  494. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
  495. "address '%s'", addr);
  496. return -1;
  497. }
  498. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
  499. MAC2STR(peer));
  500. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  501. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  502. else
  503. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  504. return ret;
  505. }
  506. static int wpa_supplicant_ctrl_iface_tdls_setup(
  507. struct wpa_supplicant *wpa_s, char *addr)
  508. {
  509. u8 peer[ETH_ALEN];
  510. int ret;
  511. if (hwaddr_aton(addr, peer)) {
  512. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
  513. "address '%s'", addr);
  514. return -1;
  515. }
  516. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
  517. MAC2STR(peer));
  518. if ((wpa_s->conf->tdls_external_control) &&
  519. wpa_tdls_is_external_setup(wpa_s->wpa))
  520. return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  521. wpa_tdls_remove(wpa_s->wpa, peer);
  522. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  523. ret = wpa_tdls_start(wpa_s->wpa, peer);
  524. else
  525. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  526. return ret;
  527. }
  528. static int wpa_supplicant_ctrl_iface_tdls_teardown(
  529. struct wpa_supplicant *wpa_s, char *addr)
  530. {
  531. u8 peer[ETH_ALEN];
  532. int ret;
  533. if (hwaddr_aton(addr, peer)) {
  534. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
  535. "address '%s'", addr);
  536. return -1;
  537. }
  538. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
  539. MAC2STR(peer));
  540. if ((wpa_s->conf->tdls_external_control) &&
  541. wpa_tdls_is_external_setup(wpa_s->wpa))
  542. return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  543. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  544. ret = wpa_tdls_teardown_link(
  545. wpa_s->wpa, peer,
  546. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  547. else
  548. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  549. return ret;
  550. }
  551. #endif /* CONFIG_TDLS */
  552. #ifdef CONFIG_IEEE80211R
  553. static int wpa_supplicant_ctrl_iface_ft_ds(
  554. struct wpa_supplicant *wpa_s, char *addr)
  555. {
  556. u8 target_ap[ETH_ALEN];
  557. struct wpa_bss *bss;
  558. const u8 *mdie;
  559. if (hwaddr_aton(addr, target_ap)) {
  560. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  561. "address '%s'", addr);
  562. return -1;
  563. }
  564. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  565. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  566. if (bss)
  567. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  568. else
  569. mdie = NULL;
  570. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  571. }
  572. #endif /* CONFIG_IEEE80211R */
  573. #ifdef CONFIG_WPS
  574. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  575. char *cmd)
  576. {
  577. u8 bssid[ETH_ALEN], *_bssid = bssid;
  578. #ifdef CONFIG_P2P
  579. u8 p2p_dev_addr[ETH_ALEN];
  580. #endif /* CONFIG_P2P */
  581. #ifdef CONFIG_AP
  582. u8 *_p2p_dev_addr = NULL;
  583. #endif /* CONFIG_AP */
  584. if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
  585. _bssid = NULL;
  586. #ifdef CONFIG_P2P
  587. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  588. if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
  589. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
  590. "P2P Device Address '%s'",
  591. cmd + 13);
  592. return -1;
  593. }
  594. _p2p_dev_addr = p2p_dev_addr;
  595. #endif /* CONFIG_P2P */
  596. } else if (hwaddr_aton(cmd, bssid)) {
  597. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  598. cmd);
  599. return -1;
  600. }
  601. #ifdef CONFIG_AP
  602. if (wpa_s->ap_iface)
  603. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
  604. #endif /* CONFIG_AP */
  605. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  606. }
  607. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  608. char *cmd, char *buf,
  609. size_t buflen)
  610. {
  611. u8 bssid[ETH_ALEN], *_bssid = bssid;
  612. char *pin;
  613. int ret;
  614. pin = os_strchr(cmd, ' ');
  615. if (pin)
  616. *pin++ = '\0';
  617. if (os_strcmp(cmd, "any") == 0)
  618. _bssid = NULL;
  619. else if (os_strcmp(cmd, "get") == 0) {
  620. ret = wps_generate_pin();
  621. goto done;
  622. } else if (hwaddr_aton(cmd, bssid)) {
  623. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  624. cmd);
  625. return -1;
  626. }
  627. #ifdef CONFIG_AP
  628. if (wpa_s->ap_iface) {
  629. int timeout = 0;
  630. char *pos;
  631. if (pin) {
  632. pos = os_strchr(pin, ' ');
  633. if (pos) {
  634. *pos++ = '\0';
  635. timeout = atoi(pos);
  636. }
  637. }
  638. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  639. buf, buflen, timeout);
  640. }
  641. #endif /* CONFIG_AP */
  642. if (pin) {
  643. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  644. DEV_PW_DEFAULT);
  645. if (ret < 0)
  646. return -1;
  647. ret = os_snprintf(buf, buflen, "%s", pin);
  648. if (ret < 0 || (size_t) ret >= buflen)
  649. return -1;
  650. return ret;
  651. }
  652. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  653. if (ret < 0)
  654. return -1;
  655. done:
  656. /* Return the generated PIN */
  657. ret = os_snprintf(buf, buflen, "%08d", ret);
  658. if (ret < 0 || (size_t) ret >= buflen)
  659. return -1;
  660. return ret;
  661. }
  662. static int wpa_supplicant_ctrl_iface_wps_check_pin(
  663. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  664. {
  665. char pin[9];
  666. size_t len;
  667. char *pos;
  668. int ret;
  669. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  670. (u8 *) cmd, os_strlen(cmd));
  671. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  672. if (*pos < '0' || *pos > '9')
  673. continue;
  674. pin[len++] = *pos;
  675. if (len == 9) {
  676. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  677. return -1;
  678. }
  679. }
  680. if (len != 4 && len != 8) {
  681. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  682. return -1;
  683. }
  684. pin[len] = '\0';
  685. if (len == 8) {
  686. unsigned int pin_val;
  687. pin_val = atoi(pin);
  688. if (!wps_pin_valid(pin_val)) {
  689. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  690. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  691. if (ret < 0 || (size_t) ret >= buflen)
  692. return -1;
  693. return ret;
  694. }
  695. }
  696. ret = os_snprintf(buf, buflen, "%s", pin);
  697. if (ret < 0 || (size_t) ret >= buflen)
  698. return -1;
  699. return ret;
  700. }
  701. #ifdef CONFIG_WPS_NFC
  702. static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
  703. char *cmd)
  704. {
  705. u8 bssid[ETH_ALEN], *_bssid = bssid;
  706. if (cmd == NULL || cmd[0] == '\0')
  707. _bssid = NULL;
  708. else if (hwaddr_aton(cmd, bssid))
  709. return -1;
  710. return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
  711. 0, 0);
  712. }
  713. static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  714. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  715. {
  716. int ndef;
  717. struct wpabuf *buf;
  718. int res;
  719. char *pos;
  720. pos = os_strchr(cmd, ' ');
  721. if (pos)
  722. *pos++ = '\0';
  723. if (os_strcmp(cmd, "WPS") == 0)
  724. ndef = 0;
  725. else if (os_strcmp(cmd, "NDEF") == 0)
  726. ndef = 1;
  727. else
  728. return -1;
  729. buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
  730. if (buf == NULL)
  731. return -1;
  732. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  733. wpabuf_len(buf));
  734. reply[res++] = '\n';
  735. reply[res] = '\0';
  736. wpabuf_free(buf);
  737. return res;
  738. }
  739. static int wpa_supplicant_ctrl_iface_wps_nfc_token(
  740. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  741. {
  742. int ndef;
  743. struct wpabuf *buf;
  744. int res;
  745. if (os_strcmp(cmd, "WPS") == 0)
  746. ndef = 0;
  747. else if (os_strcmp(cmd, "NDEF") == 0)
  748. ndef = 1;
  749. else
  750. return -1;
  751. buf = wpas_wps_nfc_token(wpa_s, ndef);
  752. if (buf == NULL)
  753. return -1;
  754. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  755. wpabuf_len(buf));
  756. reply[res++] = '\n';
  757. reply[res] = '\0';
  758. wpabuf_free(buf);
  759. return res;
  760. }
  761. static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
  762. struct wpa_supplicant *wpa_s, char *pos)
  763. {
  764. size_t len;
  765. struct wpabuf *buf;
  766. int ret;
  767. char *freq;
  768. int forced_freq = 0;
  769. freq = strstr(pos, " freq=");
  770. if (freq) {
  771. *freq = '\0';
  772. freq += 6;
  773. forced_freq = atoi(freq);
  774. }
  775. len = os_strlen(pos);
  776. if (len & 0x01)
  777. return -1;
  778. len /= 2;
  779. buf = wpabuf_alloc(len);
  780. if (buf == NULL)
  781. return -1;
  782. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  783. wpabuf_free(buf);
  784. return -1;
  785. }
  786. ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
  787. wpabuf_free(buf);
  788. return ret;
  789. }
  790. static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
  791. char *reply, size_t max_len,
  792. int ndef)
  793. {
  794. struct wpabuf *buf;
  795. int res;
  796. buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
  797. if (buf == NULL)
  798. return -1;
  799. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  800. wpabuf_len(buf));
  801. reply[res++] = '\n';
  802. reply[res] = '\0';
  803. wpabuf_free(buf);
  804. return res;
  805. }
  806. #ifdef CONFIG_P2P
  807. static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
  808. char *reply, size_t max_len,
  809. int ndef)
  810. {
  811. struct wpabuf *buf;
  812. int res;
  813. buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
  814. if (buf == NULL) {
  815. wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
  816. return -1;
  817. }
  818. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  819. wpabuf_len(buf));
  820. reply[res++] = '\n';
  821. reply[res] = '\0';
  822. wpabuf_free(buf);
  823. return res;
  824. }
  825. #endif /* CONFIG_P2P */
  826. static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
  827. char *cmd, char *reply,
  828. size_t max_len)
  829. {
  830. char *pos;
  831. int ndef;
  832. pos = os_strchr(cmd, ' ');
  833. if (pos == NULL)
  834. return -1;
  835. *pos++ = '\0';
  836. if (os_strcmp(cmd, "WPS") == 0)
  837. ndef = 0;
  838. else if (os_strcmp(cmd, "NDEF") == 0)
  839. ndef = 1;
  840. else
  841. return -1;
  842. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  843. if (!ndef)
  844. return -1;
  845. return wpas_ctrl_nfc_get_handover_req_wps(
  846. wpa_s, reply, max_len, ndef);
  847. }
  848. #ifdef CONFIG_P2P
  849. if (os_strcmp(pos, "P2P-CR") == 0) {
  850. return wpas_ctrl_nfc_get_handover_req_p2p(
  851. wpa_s, reply, max_len, ndef);
  852. }
  853. #endif /* CONFIG_P2P */
  854. return -1;
  855. }
  856. static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
  857. char *reply, size_t max_len,
  858. int ndef, int cr, char *uuid)
  859. {
  860. struct wpabuf *buf;
  861. int res;
  862. buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
  863. if (buf == NULL)
  864. return -1;
  865. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  866. wpabuf_len(buf));
  867. reply[res++] = '\n';
  868. reply[res] = '\0';
  869. wpabuf_free(buf);
  870. return res;
  871. }
  872. #ifdef CONFIG_P2P
  873. static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
  874. char *reply, size_t max_len,
  875. int ndef, int tag)
  876. {
  877. struct wpabuf *buf;
  878. int res;
  879. buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
  880. if (buf == NULL)
  881. return -1;
  882. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  883. wpabuf_len(buf));
  884. reply[res++] = '\n';
  885. reply[res] = '\0';
  886. wpabuf_free(buf);
  887. return res;
  888. }
  889. #endif /* CONFIG_P2P */
  890. static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
  891. char *cmd, char *reply,
  892. size_t max_len)
  893. {
  894. char *pos, *pos2;
  895. int ndef;
  896. pos = os_strchr(cmd, ' ');
  897. if (pos == NULL)
  898. return -1;
  899. *pos++ = '\0';
  900. if (os_strcmp(cmd, "WPS") == 0)
  901. ndef = 0;
  902. else if (os_strcmp(cmd, "NDEF") == 0)
  903. ndef = 1;
  904. else
  905. return -1;
  906. pos2 = os_strchr(pos, ' ');
  907. if (pos2)
  908. *pos2++ = '\0';
  909. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  910. if (!ndef)
  911. return -1;
  912. return wpas_ctrl_nfc_get_handover_sel_wps(
  913. wpa_s, reply, max_len, ndef,
  914. os_strcmp(pos, "WPS-CR") == 0, pos2);
  915. }
  916. #ifdef CONFIG_P2P
  917. if (os_strcmp(pos, "P2P-CR") == 0) {
  918. return wpas_ctrl_nfc_get_handover_sel_p2p(
  919. wpa_s, reply, max_len, ndef, 0);
  920. }
  921. if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
  922. return wpas_ctrl_nfc_get_handover_sel_p2p(
  923. wpa_s, reply, max_len, ndef, 1);
  924. }
  925. #endif /* CONFIG_P2P */
  926. return -1;
  927. }
  928. static int wpas_ctrl_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
  929. char *cmd)
  930. {
  931. size_t len;
  932. struct wpabuf *buf;
  933. int ret;
  934. len = os_strlen(cmd);
  935. if (len & 0x01)
  936. return -1;
  937. len /= 2;
  938. buf = wpabuf_alloc(len);
  939. if (buf == NULL)
  940. return -1;
  941. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  942. wpabuf_free(buf);
  943. return -1;
  944. }
  945. ret = wpas_wps_nfc_rx_handover_sel(wpa_s, buf);
  946. wpabuf_free(buf);
  947. return ret;
  948. }
  949. static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
  950. char *cmd)
  951. {
  952. size_t len;
  953. struct wpabuf *req, *sel;
  954. int ret;
  955. char *pos, *role, *type, *pos2;
  956. #ifdef CONFIG_P2P
  957. char *freq;
  958. int forced_freq = 0;
  959. freq = strstr(cmd, " freq=");
  960. if (freq) {
  961. *freq = '\0';
  962. freq += 6;
  963. forced_freq = atoi(freq);
  964. }
  965. #endif /* CONFIG_P2P */
  966. role = cmd;
  967. pos = os_strchr(role, ' ');
  968. if (pos == NULL) {
  969. wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
  970. return -1;
  971. }
  972. *pos++ = '\0';
  973. type = pos;
  974. pos = os_strchr(type, ' ');
  975. if (pos == NULL) {
  976. wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
  977. return -1;
  978. }
  979. *pos++ = '\0';
  980. pos2 = os_strchr(pos, ' ');
  981. if (pos2 == NULL) {
  982. wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
  983. return -1;
  984. }
  985. *pos2++ = '\0';
  986. len = os_strlen(pos);
  987. if (len & 0x01) {
  988. wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
  989. return -1;
  990. }
  991. len /= 2;
  992. req = wpabuf_alloc(len);
  993. if (req == NULL) {
  994. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
  995. return -1;
  996. }
  997. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  998. wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
  999. wpabuf_free(req);
  1000. return -1;
  1001. }
  1002. len = os_strlen(pos2);
  1003. if (len & 0x01) {
  1004. wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
  1005. wpabuf_free(req);
  1006. return -1;
  1007. }
  1008. len /= 2;
  1009. sel = wpabuf_alloc(len);
  1010. if (sel == NULL) {
  1011. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
  1012. wpabuf_free(req);
  1013. return -1;
  1014. }
  1015. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  1016. wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
  1017. wpabuf_free(req);
  1018. wpabuf_free(sel);
  1019. return -1;
  1020. }
  1021. wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
  1022. role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
  1023. if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
  1024. ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
  1025. #ifdef CONFIG_AP
  1026. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
  1027. {
  1028. ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
  1029. if (ret < 0)
  1030. ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
  1031. #endif /* CONFIG_AP */
  1032. #ifdef CONFIG_P2P
  1033. } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
  1034. {
  1035. ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
  1036. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
  1037. {
  1038. ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
  1039. forced_freq);
  1040. #endif /* CONFIG_P2P */
  1041. } else {
  1042. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  1043. "reported: role=%s type=%s", role, type);
  1044. ret = -1;
  1045. }
  1046. wpabuf_free(req);
  1047. wpabuf_free(sel);
  1048. if (ret)
  1049. wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
  1050. return ret;
  1051. }
  1052. #endif /* CONFIG_WPS_NFC */
  1053. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  1054. char *cmd)
  1055. {
  1056. u8 bssid[ETH_ALEN];
  1057. char *pin;
  1058. char *new_ssid;
  1059. char *new_auth;
  1060. char *new_encr;
  1061. char *new_key;
  1062. struct wps_new_ap_settings ap;
  1063. pin = os_strchr(cmd, ' ');
  1064. if (pin == NULL)
  1065. return -1;
  1066. *pin++ = '\0';
  1067. if (hwaddr_aton(cmd, bssid)) {
  1068. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  1069. cmd);
  1070. return -1;
  1071. }
  1072. new_ssid = os_strchr(pin, ' ');
  1073. if (new_ssid == NULL)
  1074. return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  1075. *new_ssid++ = '\0';
  1076. new_auth = os_strchr(new_ssid, ' ');
  1077. if (new_auth == NULL)
  1078. return -1;
  1079. *new_auth++ = '\0';
  1080. new_encr = os_strchr(new_auth, ' ');
  1081. if (new_encr == NULL)
  1082. return -1;
  1083. *new_encr++ = '\0';
  1084. new_key = os_strchr(new_encr, ' ');
  1085. if (new_key == NULL)
  1086. return -1;
  1087. *new_key++ = '\0';
  1088. os_memset(&ap, 0, sizeof(ap));
  1089. ap.ssid_hex = new_ssid;
  1090. ap.auth = new_auth;
  1091. ap.encr = new_encr;
  1092. ap.key_hex = new_key;
  1093. return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
  1094. }
  1095. #ifdef CONFIG_AP
  1096. static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
  1097. char *cmd, char *buf,
  1098. size_t buflen)
  1099. {
  1100. int timeout = 300;
  1101. char *pos;
  1102. const char *pin_txt;
  1103. if (!wpa_s->ap_iface)
  1104. return -1;
  1105. pos = os_strchr(cmd, ' ');
  1106. if (pos)
  1107. *pos++ = '\0';
  1108. if (os_strcmp(cmd, "disable") == 0) {
  1109. wpas_wps_ap_pin_disable(wpa_s);
  1110. return os_snprintf(buf, buflen, "OK\n");
  1111. }
  1112. if (os_strcmp(cmd, "random") == 0) {
  1113. if (pos)
  1114. timeout = atoi(pos);
  1115. pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
  1116. if (pin_txt == NULL)
  1117. return -1;
  1118. return os_snprintf(buf, buflen, "%s", pin_txt);
  1119. }
  1120. if (os_strcmp(cmd, "get") == 0) {
  1121. pin_txt = wpas_wps_ap_pin_get(wpa_s);
  1122. if (pin_txt == NULL)
  1123. return -1;
  1124. return os_snprintf(buf, buflen, "%s", pin_txt);
  1125. }
  1126. if (os_strcmp(cmd, "set") == 0) {
  1127. char *pin;
  1128. if (pos == NULL)
  1129. return -1;
  1130. pin = pos;
  1131. pos = os_strchr(pos, ' ');
  1132. if (pos) {
  1133. *pos++ = '\0';
  1134. timeout = atoi(pos);
  1135. }
  1136. if (os_strlen(pin) > buflen)
  1137. return -1;
  1138. if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
  1139. return -1;
  1140. return os_snprintf(buf, buflen, "%s", pin);
  1141. }
  1142. return -1;
  1143. }
  1144. #endif /* CONFIG_AP */
  1145. #ifdef CONFIG_WPS_ER
  1146. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  1147. char *cmd)
  1148. {
  1149. char *uuid = cmd, *pin, *pos;
  1150. u8 addr_buf[ETH_ALEN], *addr = NULL;
  1151. pin = os_strchr(uuid, ' ');
  1152. if (pin == NULL)
  1153. return -1;
  1154. *pin++ = '\0';
  1155. pos = os_strchr(pin, ' ');
  1156. if (pos) {
  1157. *pos++ = '\0';
  1158. if (hwaddr_aton(pos, addr_buf) == 0)
  1159. addr = addr_buf;
  1160. }
  1161. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  1162. }
  1163. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  1164. char *cmd)
  1165. {
  1166. char *uuid = cmd, *pin;
  1167. pin = os_strchr(uuid, ' ');
  1168. if (pin == NULL)
  1169. return -1;
  1170. *pin++ = '\0';
  1171. return wpas_wps_er_learn(wpa_s, uuid, pin);
  1172. }
  1173. static int wpa_supplicant_ctrl_iface_wps_er_set_config(
  1174. struct wpa_supplicant *wpa_s, char *cmd)
  1175. {
  1176. char *uuid = cmd, *id;
  1177. id = os_strchr(uuid, ' ');
  1178. if (id == NULL)
  1179. return -1;
  1180. *id++ = '\0';
  1181. return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
  1182. }
  1183. static int wpa_supplicant_ctrl_iface_wps_er_config(
  1184. struct wpa_supplicant *wpa_s, char *cmd)
  1185. {
  1186. char *pin;
  1187. char *new_ssid;
  1188. char *new_auth;
  1189. char *new_encr;
  1190. char *new_key;
  1191. struct wps_new_ap_settings ap;
  1192. pin = os_strchr(cmd, ' ');
  1193. if (pin == NULL)
  1194. return -1;
  1195. *pin++ = '\0';
  1196. new_ssid = os_strchr(pin, ' ');
  1197. if (new_ssid == NULL)
  1198. return -1;
  1199. *new_ssid++ = '\0';
  1200. new_auth = os_strchr(new_ssid, ' ');
  1201. if (new_auth == NULL)
  1202. return -1;
  1203. *new_auth++ = '\0';
  1204. new_encr = os_strchr(new_auth, ' ');
  1205. if (new_encr == NULL)
  1206. return -1;
  1207. *new_encr++ = '\0';
  1208. new_key = os_strchr(new_encr, ' ');
  1209. if (new_key == NULL)
  1210. return -1;
  1211. *new_key++ = '\0';
  1212. os_memset(&ap, 0, sizeof(ap));
  1213. ap.ssid_hex = new_ssid;
  1214. ap.auth = new_auth;
  1215. ap.encr = new_encr;
  1216. ap.key_hex = new_key;
  1217. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  1218. }
  1219. #ifdef CONFIG_WPS_NFC
  1220. static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  1221. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1222. {
  1223. int ndef;
  1224. struct wpabuf *buf;
  1225. int res;
  1226. char *uuid;
  1227. uuid = os_strchr(cmd, ' ');
  1228. if (uuid == NULL)
  1229. return -1;
  1230. *uuid++ = '\0';
  1231. if (os_strcmp(cmd, "WPS") == 0)
  1232. ndef = 0;
  1233. else if (os_strcmp(cmd, "NDEF") == 0)
  1234. ndef = 1;
  1235. else
  1236. return -1;
  1237. buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
  1238. if (buf == NULL)
  1239. return -1;
  1240. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1241. wpabuf_len(buf));
  1242. reply[res++] = '\n';
  1243. reply[res] = '\0';
  1244. wpabuf_free(buf);
  1245. return res;
  1246. }
  1247. #endif /* CONFIG_WPS_NFC */
  1248. #endif /* CONFIG_WPS_ER */
  1249. #endif /* CONFIG_WPS */
  1250. #ifdef CONFIG_IBSS_RSN
  1251. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  1252. struct wpa_supplicant *wpa_s, char *addr)
  1253. {
  1254. u8 peer[ETH_ALEN];
  1255. if (hwaddr_aton(addr, peer)) {
  1256. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  1257. "address '%s'", addr);
  1258. return -1;
  1259. }
  1260. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  1261. MAC2STR(peer));
  1262. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  1263. }
  1264. #endif /* CONFIG_IBSS_RSN */
  1265. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  1266. char *rsp)
  1267. {
  1268. #ifdef IEEE8021X_EAPOL
  1269. char *pos, *id_pos;
  1270. int id;
  1271. struct wpa_ssid *ssid;
  1272. pos = os_strchr(rsp, '-');
  1273. if (pos == NULL)
  1274. return -1;
  1275. *pos++ = '\0';
  1276. id_pos = pos;
  1277. pos = os_strchr(pos, ':');
  1278. if (pos == NULL)
  1279. return -1;
  1280. *pos++ = '\0';
  1281. id = atoi(id_pos);
  1282. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  1283. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1284. (u8 *) pos, os_strlen(pos));
  1285. ssid = wpa_config_get_network(wpa_s->conf, id);
  1286. if (ssid == NULL) {
  1287. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1288. "to update", id);
  1289. return -1;
  1290. }
  1291. return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
  1292. pos);
  1293. #else /* IEEE8021X_EAPOL */
  1294. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1295. return -1;
  1296. #endif /* IEEE8021X_EAPOL */
  1297. }
  1298. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  1299. const char *params,
  1300. char *buf, size_t buflen)
  1301. {
  1302. char *pos, *end, tmp[30];
  1303. int res, verbose, wps, ret;
  1304. #ifdef CONFIG_HS20
  1305. const u8 *hs20;
  1306. #endif /* CONFIG_HS20 */
  1307. if (os_strcmp(params, "-DRIVER") == 0)
  1308. return wpa_drv_status(wpa_s, buf, buflen);
  1309. verbose = os_strcmp(params, "-VERBOSE") == 0;
  1310. wps = os_strcmp(params, "-WPS") == 0;
  1311. pos = buf;
  1312. end = buf + buflen;
  1313. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1314. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1315. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  1316. MAC2STR(wpa_s->bssid));
  1317. if (ret < 0 || ret >= end - pos)
  1318. return pos - buf;
  1319. pos += ret;
  1320. if (ssid) {
  1321. u8 *_ssid = ssid->ssid;
  1322. size_t ssid_len = ssid->ssid_len;
  1323. u8 ssid_buf[MAX_SSID_LEN];
  1324. if (ssid_len == 0) {
  1325. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  1326. if (_res < 0)
  1327. ssid_len = 0;
  1328. else
  1329. ssid_len = _res;
  1330. _ssid = ssid_buf;
  1331. }
  1332. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  1333. wpa_ssid_txt(_ssid, ssid_len),
  1334. ssid->id);
  1335. if (ret < 0 || ret >= end - pos)
  1336. return pos - buf;
  1337. pos += ret;
  1338. if (wps && ssid->passphrase &&
  1339. wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
  1340. (ssid->mode == WPAS_MODE_AP ||
  1341. ssid->mode == WPAS_MODE_P2P_GO)) {
  1342. ret = os_snprintf(pos, end - pos,
  1343. "passphrase=%s\n",
  1344. ssid->passphrase);
  1345. if (ret < 0 || ret >= end - pos)
  1346. return pos - buf;
  1347. pos += ret;
  1348. }
  1349. if (ssid->id_str) {
  1350. ret = os_snprintf(pos, end - pos,
  1351. "id_str=%s\n",
  1352. ssid->id_str);
  1353. if (ret < 0 || ret >= end - pos)
  1354. return pos - buf;
  1355. pos += ret;
  1356. }
  1357. switch (ssid->mode) {
  1358. case WPAS_MODE_INFRA:
  1359. ret = os_snprintf(pos, end - pos,
  1360. "mode=station\n");
  1361. break;
  1362. case WPAS_MODE_IBSS:
  1363. ret = os_snprintf(pos, end - pos,
  1364. "mode=IBSS\n");
  1365. break;
  1366. case WPAS_MODE_AP:
  1367. ret = os_snprintf(pos, end - pos,
  1368. "mode=AP\n");
  1369. break;
  1370. case WPAS_MODE_P2P_GO:
  1371. ret = os_snprintf(pos, end - pos,
  1372. "mode=P2P GO\n");
  1373. break;
  1374. case WPAS_MODE_P2P_GROUP_FORMATION:
  1375. ret = os_snprintf(pos, end - pos,
  1376. "mode=P2P GO - group "
  1377. "formation\n");
  1378. break;
  1379. default:
  1380. ret = 0;
  1381. break;
  1382. }
  1383. if (ret < 0 || ret >= end - pos)
  1384. return pos - buf;
  1385. pos += ret;
  1386. }
  1387. #ifdef CONFIG_AP
  1388. if (wpa_s->ap_iface) {
  1389. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  1390. end - pos,
  1391. verbose);
  1392. } else
  1393. #endif /* CONFIG_AP */
  1394. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1395. }
  1396. #ifdef CONFIG_SAE
  1397. if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
  1398. #ifdef CONFIG_AP
  1399. !wpa_s->ap_iface &&
  1400. #endif /* CONFIG_AP */
  1401. wpa_s->sme.sae.state == SAE_ACCEPTED) {
  1402. ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
  1403. wpa_s->sme.sae.group);
  1404. if (ret < 0 || ret >= end - pos)
  1405. return pos - buf;
  1406. pos += ret;
  1407. }
  1408. #endif /* CONFIG_SAE */
  1409. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  1410. wpa_supplicant_state_txt(wpa_s->wpa_state));
  1411. if (ret < 0 || ret >= end - pos)
  1412. return pos - buf;
  1413. pos += ret;
  1414. if (wpa_s->l2 &&
  1415. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  1416. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  1417. if (ret < 0 || ret >= end - pos)
  1418. return pos - buf;
  1419. pos += ret;
  1420. }
  1421. #ifdef CONFIG_P2P
  1422. if (wpa_s->global->p2p) {
  1423. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  1424. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  1425. if (ret < 0 || ret >= end - pos)
  1426. return pos - buf;
  1427. pos += ret;
  1428. }
  1429. #endif /* CONFIG_P2P */
  1430. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  1431. MAC2STR(wpa_s->own_addr));
  1432. if (ret < 0 || ret >= end - pos)
  1433. return pos - buf;
  1434. pos += ret;
  1435. #ifdef CONFIG_HS20
  1436. if (wpa_s->current_bss &&
  1437. (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
  1438. HS20_IE_VENDOR_TYPE)) &&
  1439. wpa_s->wpa_proto == WPA_PROTO_RSN &&
  1440. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1441. int release = 1;
  1442. if (hs20[1] >= 5) {
  1443. u8 rel_num = (hs20[6] & 0xf0) >> 4;
  1444. release = rel_num + 1;
  1445. }
  1446. ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
  1447. if (ret < 0 || ret >= end - pos)
  1448. return pos - buf;
  1449. pos += ret;
  1450. }
  1451. if (wpa_s->current_ssid) {
  1452. struct wpa_cred *cred;
  1453. char *type;
  1454. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1455. size_t i;
  1456. if (wpa_s->current_ssid->parent_cred != cred)
  1457. continue;
  1458. if (cred->provisioning_sp) {
  1459. ret = os_snprintf(pos, end - pos,
  1460. "provisioning_sp=%s\n",
  1461. cred->provisioning_sp);
  1462. if (ret < 0 || ret >= end - pos)
  1463. return pos - buf;
  1464. pos += ret;
  1465. }
  1466. if (!cred->domain)
  1467. goto no_domain;
  1468. i = 0;
  1469. if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
  1470. struct wpabuf *names =
  1471. wpa_s->current_bss->anqp->domain_name;
  1472. for (i = 0; names && i < cred->num_domain; i++)
  1473. {
  1474. if (domain_name_list_contains(
  1475. names, cred->domain[i], 1))
  1476. break;
  1477. }
  1478. if (i == cred->num_domain)
  1479. i = 0; /* show first entry by default */
  1480. }
  1481. ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
  1482. cred->domain[i]);
  1483. if (ret < 0 || ret >= end - pos)
  1484. return pos - buf;
  1485. pos += ret;
  1486. no_domain:
  1487. if (wpa_s->current_bss == NULL ||
  1488. wpa_s->current_bss->anqp == NULL)
  1489. res = -1;
  1490. else
  1491. res = interworking_home_sp_cred(
  1492. wpa_s, cred,
  1493. wpa_s->current_bss->anqp->domain_name);
  1494. if (res > 0)
  1495. type = "home";
  1496. else if (res == 0)
  1497. type = "roaming";
  1498. else
  1499. type = "unknown";
  1500. ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
  1501. if (ret < 0 || ret >= end - pos)
  1502. return pos - buf;
  1503. pos += ret;
  1504. break;
  1505. }
  1506. }
  1507. #endif /* CONFIG_HS20 */
  1508. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1509. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1510. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  1511. verbose);
  1512. if (res >= 0)
  1513. pos += res;
  1514. }
  1515. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1516. if (res >= 0)
  1517. pos += res;
  1518. #ifdef CONFIG_WPS
  1519. {
  1520. char uuid_str[100];
  1521. uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
  1522. ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
  1523. if (ret < 0 || ret >= end - pos)
  1524. return pos - buf;
  1525. pos += ret;
  1526. }
  1527. #endif /* CONFIG_WPS */
  1528. #ifdef ANDROID
  1529. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
  1530. "id=%d state=%d BSSID=" MACSTR " SSID=%s",
  1531. wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
  1532. wpa_s->wpa_state,
  1533. MAC2STR(wpa_s->bssid),
  1534. wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
  1535. wpa_ssid_txt(wpa_s->current_ssid->ssid,
  1536. wpa_s->current_ssid->ssid_len) : "");
  1537. if (wpa_s->wpa_state == WPA_COMPLETED) {
  1538. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1539. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
  1540. "- connection to " MACSTR
  1541. " completed %s [id=%d id_str=%s]",
  1542. MAC2STR(wpa_s->bssid), "(auth)",
  1543. ssid ? ssid->id : -1,
  1544. ssid && ssid->id_str ? ssid->id_str : "");
  1545. }
  1546. #endif /* ANDROID */
  1547. return pos - buf;
  1548. }
  1549. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1550. char *cmd)
  1551. {
  1552. char *pos;
  1553. int id;
  1554. struct wpa_ssid *ssid;
  1555. u8 bssid[ETH_ALEN];
  1556. /* cmd: "<network id> <BSSID>" */
  1557. pos = os_strchr(cmd, ' ');
  1558. if (pos == NULL)
  1559. return -1;
  1560. *pos++ = '\0';
  1561. id = atoi(cmd);
  1562. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  1563. if (hwaddr_aton(pos, bssid)) {
  1564. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  1565. return -1;
  1566. }
  1567. ssid = wpa_config_get_network(wpa_s->conf, id);
  1568. if (ssid == NULL) {
  1569. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1570. "to update", id);
  1571. return -1;
  1572. }
  1573. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  1574. ssid->bssid_set = !is_zero_ether_addr(bssid);
  1575. return 0;
  1576. }
  1577. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  1578. char *cmd, char *buf,
  1579. size_t buflen)
  1580. {
  1581. u8 bssid[ETH_ALEN];
  1582. struct wpa_blacklist *e;
  1583. char *pos, *end;
  1584. int ret;
  1585. /* cmd: "BLACKLIST [<BSSID>]" */
  1586. if (*cmd == '\0') {
  1587. pos = buf;
  1588. end = buf + buflen;
  1589. e = wpa_s->blacklist;
  1590. while (e) {
  1591. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  1592. MAC2STR(e->bssid));
  1593. if (ret < 0 || ret >= end - pos)
  1594. return pos - buf;
  1595. pos += ret;
  1596. e = e->next;
  1597. }
  1598. return pos - buf;
  1599. }
  1600. cmd++;
  1601. if (os_strncmp(cmd, "clear", 5) == 0) {
  1602. wpa_blacklist_clear(wpa_s);
  1603. os_memcpy(buf, "OK\n", 3);
  1604. return 3;
  1605. }
  1606. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  1607. if (hwaddr_aton(cmd, bssid)) {
  1608. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  1609. return -1;
  1610. }
  1611. /*
  1612. * Add the BSSID twice, so its count will be 2, causing it to be
  1613. * skipped when processing scan results.
  1614. */
  1615. ret = wpa_blacklist_add(wpa_s, bssid);
  1616. if (ret != 0)
  1617. return -1;
  1618. ret = wpa_blacklist_add(wpa_s, bssid);
  1619. if (ret != 0)
  1620. return -1;
  1621. os_memcpy(buf, "OK\n", 3);
  1622. return 3;
  1623. }
  1624. static const char * debug_level_str(int level)
  1625. {
  1626. switch (level) {
  1627. case MSG_EXCESSIVE:
  1628. return "EXCESSIVE";
  1629. case MSG_MSGDUMP:
  1630. return "MSGDUMP";
  1631. case MSG_DEBUG:
  1632. return "DEBUG";
  1633. case MSG_INFO:
  1634. return "INFO";
  1635. case MSG_WARNING:
  1636. return "WARNING";
  1637. case MSG_ERROR:
  1638. return "ERROR";
  1639. default:
  1640. return "?";
  1641. }
  1642. }
  1643. static int str_to_debug_level(const char *s)
  1644. {
  1645. if (os_strcasecmp(s, "EXCESSIVE") == 0)
  1646. return MSG_EXCESSIVE;
  1647. if (os_strcasecmp(s, "MSGDUMP") == 0)
  1648. return MSG_MSGDUMP;
  1649. if (os_strcasecmp(s, "DEBUG") == 0)
  1650. return MSG_DEBUG;
  1651. if (os_strcasecmp(s, "INFO") == 0)
  1652. return MSG_INFO;
  1653. if (os_strcasecmp(s, "WARNING") == 0)
  1654. return MSG_WARNING;
  1655. if (os_strcasecmp(s, "ERROR") == 0)
  1656. return MSG_ERROR;
  1657. return -1;
  1658. }
  1659. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  1660. char *cmd, char *buf,
  1661. size_t buflen)
  1662. {
  1663. char *pos, *end, *stamp;
  1664. int ret;
  1665. if (cmd == NULL) {
  1666. return -1;
  1667. }
  1668. /* cmd: "LOG_LEVEL [<level>]" */
  1669. if (*cmd == '\0') {
  1670. pos = buf;
  1671. end = buf + buflen;
  1672. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1673. "Timestamp: %d\n",
  1674. debug_level_str(wpa_debug_level),
  1675. wpa_debug_timestamp);
  1676. if (ret < 0 || ret >= end - pos)
  1677. ret = 0;
  1678. return ret;
  1679. }
  1680. while (*cmd == ' ')
  1681. cmd++;
  1682. stamp = os_strchr(cmd, ' ');
  1683. if (stamp) {
  1684. *stamp++ = '\0';
  1685. while (*stamp == ' ') {
  1686. stamp++;
  1687. }
  1688. }
  1689. if (cmd && os_strlen(cmd)) {
  1690. int level = str_to_debug_level(cmd);
  1691. if (level < 0)
  1692. return -1;
  1693. wpa_debug_level = level;
  1694. }
  1695. if (stamp && os_strlen(stamp))
  1696. wpa_debug_timestamp = atoi(stamp);
  1697. os_memcpy(buf, "OK\n", 3);
  1698. return 3;
  1699. }
  1700. static int wpa_supplicant_ctrl_iface_list_networks(
  1701. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1702. {
  1703. char *pos, *end;
  1704. struct wpa_ssid *ssid;
  1705. int ret;
  1706. pos = buf;
  1707. end = buf + buflen;
  1708. ret = os_snprintf(pos, end - pos,
  1709. "network id / ssid / bssid / flags\n");
  1710. if (ret < 0 || ret >= end - pos)
  1711. return pos - buf;
  1712. pos += ret;
  1713. ssid = wpa_s->conf->ssid;
  1714. while (ssid) {
  1715. ret = os_snprintf(pos, end - pos, "%d\t%s",
  1716. ssid->id,
  1717. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1718. if (ret < 0 || ret >= end - pos)
  1719. return pos - buf;
  1720. pos += ret;
  1721. if (ssid->bssid_set) {
  1722. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  1723. MAC2STR(ssid->bssid));
  1724. } else {
  1725. ret = os_snprintf(pos, end - pos, "\tany");
  1726. }
  1727. if (ret < 0 || ret >= end - pos)
  1728. return pos - buf;
  1729. pos += ret;
  1730. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  1731. ssid == wpa_s->current_ssid ?
  1732. "[CURRENT]" : "",
  1733. ssid->disabled ? "[DISABLED]" : "",
  1734. ssid->disabled_until.sec ?
  1735. "[TEMP-DISABLED]" : "",
  1736. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  1737. "");
  1738. if (ret < 0 || ret >= end - pos)
  1739. return pos - buf;
  1740. pos += ret;
  1741. ret = os_snprintf(pos, end - pos, "\n");
  1742. if (ret < 0 || ret >= end - pos)
  1743. return pos - buf;
  1744. pos += ret;
  1745. ssid = ssid->next;
  1746. }
  1747. return pos - buf;
  1748. }
  1749. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  1750. {
  1751. int ret;
  1752. ret = os_snprintf(pos, end - pos, "-");
  1753. if (ret < 0 || ret >= end - pos)
  1754. return pos;
  1755. pos += ret;
  1756. ret = wpa_write_ciphers(pos, end, cipher, "+");
  1757. if (ret < 0)
  1758. return pos;
  1759. pos += ret;
  1760. return pos;
  1761. }
  1762. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  1763. const u8 *ie, size_t ie_len)
  1764. {
  1765. struct wpa_ie_data data;
  1766. char *start;
  1767. int ret;
  1768. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  1769. if (ret < 0 || ret >= end - pos)
  1770. return pos;
  1771. pos += ret;
  1772. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  1773. ret = os_snprintf(pos, end - pos, "?]");
  1774. if (ret < 0 || ret >= end - pos)
  1775. return pos;
  1776. pos += ret;
  1777. return pos;
  1778. }
  1779. start = pos;
  1780. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  1781. ret = os_snprintf(pos, end - pos, "%sEAP",
  1782. pos == start ? "" : "+");
  1783. if (ret < 0 || ret >= end - pos)
  1784. return pos;
  1785. pos += ret;
  1786. }
  1787. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  1788. ret = os_snprintf(pos, end - pos, "%sPSK",
  1789. pos == start ? "" : "+");
  1790. if (ret < 0 || ret >= end - pos)
  1791. return pos;
  1792. pos += ret;
  1793. }
  1794. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  1795. ret = os_snprintf(pos, end - pos, "%sNone",
  1796. pos == start ? "" : "+");
  1797. if (ret < 0 || ret >= end - pos)
  1798. return pos;
  1799. pos += ret;
  1800. }
  1801. #ifdef CONFIG_IEEE80211R
  1802. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  1803. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  1804. pos == start ? "" : "+");
  1805. if (ret < 0 || ret >= end - pos)
  1806. return pos;
  1807. pos += ret;
  1808. }
  1809. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  1810. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  1811. pos == start ? "" : "+");
  1812. if (ret < 0 || ret >= end - pos)
  1813. return pos;
  1814. pos += ret;
  1815. }
  1816. #endif /* CONFIG_IEEE80211R */
  1817. #ifdef CONFIG_IEEE80211W
  1818. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1819. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  1820. pos == start ? "" : "+");
  1821. if (ret < 0 || ret >= end - pos)
  1822. return pos;
  1823. pos += ret;
  1824. }
  1825. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  1826. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  1827. pos == start ? "" : "+");
  1828. if (ret < 0 || ret >= end - pos)
  1829. return pos;
  1830. pos += ret;
  1831. }
  1832. #endif /* CONFIG_IEEE80211W */
  1833. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  1834. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  1835. ret = os_snprintf(pos, end - pos, "-preauth");
  1836. if (ret < 0 || ret >= end - pos)
  1837. return pos;
  1838. pos += ret;
  1839. }
  1840. ret = os_snprintf(pos, end - pos, "]");
  1841. if (ret < 0 || ret >= end - pos)
  1842. return pos;
  1843. pos += ret;
  1844. return pos;
  1845. }
  1846. #ifdef CONFIG_WPS
  1847. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  1848. char *pos, char *end,
  1849. struct wpabuf *wps_ie)
  1850. {
  1851. int ret;
  1852. const char *txt;
  1853. if (wps_ie == NULL)
  1854. return pos;
  1855. if (wps_is_selected_pbc_registrar(wps_ie))
  1856. txt = "[WPS-PBC]";
  1857. #ifdef CONFIG_WPS2
  1858. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  1859. txt = "[WPS-AUTH]";
  1860. #endif /* CONFIG_WPS2 */
  1861. else if (wps_is_selected_pin_registrar(wps_ie))
  1862. txt = "[WPS-PIN]";
  1863. else
  1864. txt = "[WPS]";
  1865. ret = os_snprintf(pos, end - pos, "%s", txt);
  1866. if (ret >= 0 && ret < end - pos)
  1867. pos += ret;
  1868. wpabuf_free(wps_ie);
  1869. return pos;
  1870. }
  1871. #endif /* CONFIG_WPS */
  1872. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  1873. char *pos, char *end,
  1874. const struct wpa_bss *bss)
  1875. {
  1876. #ifdef CONFIG_WPS
  1877. struct wpabuf *wps_ie;
  1878. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1879. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  1880. #else /* CONFIG_WPS */
  1881. return pos;
  1882. #endif /* CONFIG_WPS */
  1883. }
  1884. /* Format one result on one text line into a buffer. */
  1885. static int wpa_supplicant_ctrl_iface_scan_result(
  1886. struct wpa_supplicant *wpa_s,
  1887. const struct wpa_bss *bss, char *buf, size_t buflen)
  1888. {
  1889. char *pos, *end;
  1890. int ret;
  1891. const u8 *ie, *ie2, *p2p;
  1892. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  1893. if (!p2p)
  1894. p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
  1895. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  1896. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  1897. 0)
  1898. return 0; /* Do not show P2P listen discovery results here */
  1899. pos = buf;
  1900. end = buf + buflen;
  1901. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  1902. MAC2STR(bss->bssid), bss->freq, bss->level);
  1903. if (ret < 0 || ret >= end - pos)
  1904. return -1;
  1905. pos += ret;
  1906. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1907. if (ie)
  1908. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1909. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1910. if (ie2)
  1911. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1912. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1913. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1914. ret = os_snprintf(pos, end - pos, "[WEP]");
  1915. if (ret < 0 || ret >= end - pos)
  1916. return -1;
  1917. pos += ret;
  1918. }
  1919. if (bss->caps & IEEE80211_CAP_IBSS) {
  1920. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1921. if (ret < 0 || ret >= end - pos)
  1922. return -1;
  1923. pos += ret;
  1924. }
  1925. if (bss->caps & IEEE80211_CAP_ESS) {
  1926. ret = os_snprintf(pos, end - pos, "[ESS]");
  1927. if (ret < 0 || ret >= end - pos)
  1928. return -1;
  1929. pos += ret;
  1930. }
  1931. if (p2p) {
  1932. ret = os_snprintf(pos, end - pos, "[P2P]");
  1933. if (ret < 0 || ret >= end - pos)
  1934. return -1;
  1935. pos += ret;
  1936. }
  1937. #ifdef CONFIG_HS20
  1938. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  1939. ret = os_snprintf(pos, end - pos, "[HS20]");
  1940. if (ret < 0 || ret >= end - pos)
  1941. return -1;
  1942. pos += ret;
  1943. }
  1944. #endif /* CONFIG_HS20 */
  1945. ret = os_snprintf(pos, end - pos, "\t%s",
  1946. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1947. if (ret < 0 || ret >= end - pos)
  1948. return -1;
  1949. pos += ret;
  1950. ret = os_snprintf(pos, end - pos, "\n");
  1951. if (ret < 0 || ret >= end - pos)
  1952. return -1;
  1953. pos += ret;
  1954. return pos - buf;
  1955. }
  1956. static int wpa_supplicant_ctrl_iface_scan_results(
  1957. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1958. {
  1959. char *pos, *end;
  1960. struct wpa_bss *bss;
  1961. int ret;
  1962. pos = buf;
  1963. end = buf + buflen;
  1964. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  1965. "flags / ssid\n");
  1966. if (ret < 0 || ret >= end - pos)
  1967. return pos - buf;
  1968. pos += ret;
  1969. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  1970. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  1971. end - pos);
  1972. if (ret < 0 || ret >= end - pos)
  1973. return pos - buf;
  1974. pos += ret;
  1975. }
  1976. return pos - buf;
  1977. }
  1978. static int wpa_supplicant_ctrl_iface_select_network(
  1979. struct wpa_supplicant *wpa_s, char *cmd)
  1980. {
  1981. int id;
  1982. struct wpa_ssid *ssid;
  1983. /* cmd: "<network id>" or "any" */
  1984. if (os_strcmp(cmd, "any") == 0) {
  1985. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  1986. ssid = NULL;
  1987. } else {
  1988. id = atoi(cmd);
  1989. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  1990. ssid = wpa_config_get_network(wpa_s->conf, id);
  1991. if (ssid == NULL) {
  1992. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1993. "network id=%d", id);
  1994. return -1;
  1995. }
  1996. if (ssid->disabled == 2) {
  1997. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1998. "SELECT_NETWORK with persistent P2P group");
  1999. return -1;
  2000. }
  2001. }
  2002. wpa_supplicant_select_network(wpa_s, ssid);
  2003. return 0;
  2004. }
  2005. static int wpa_supplicant_ctrl_iface_enable_network(
  2006. struct wpa_supplicant *wpa_s, char *cmd)
  2007. {
  2008. int id;
  2009. struct wpa_ssid *ssid;
  2010. /* cmd: "<network id>" or "all" */
  2011. if (os_strcmp(cmd, "all") == 0) {
  2012. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  2013. ssid = NULL;
  2014. } else {
  2015. id = atoi(cmd);
  2016. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  2017. ssid = wpa_config_get_network(wpa_s->conf, id);
  2018. if (ssid == NULL) {
  2019. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2020. "network id=%d", id);
  2021. return -1;
  2022. }
  2023. if (ssid->disabled == 2) {
  2024. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2025. "ENABLE_NETWORK with persistent P2P group");
  2026. return -1;
  2027. }
  2028. if (os_strstr(cmd, " no-connect")) {
  2029. ssid->disabled = 0;
  2030. return 0;
  2031. }
  2032. }
  2033. wpa_supplicant_enable_network(wpa_s, ssid);
  2034. return 0;
  2035. }
  2036. static int wpa_supplicant_ctrl_iface_disable_network(
  2037. struct wpa_supplicant *wpa_s, char *cmd)
  2038. {
  2039. int id;
  2040. struct wpa_ssid *ssid;
  2041. /* cmd: "<network id>" or "all" */
  2042. if (os_strcmp(cmd, "all") == 0) {
  2043. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  2044. ssid = NULL;
  2045. } else {
  2046. id = atoi(cmd);
  2047. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  2048. ssid = wpa_config_get_network(wpa_s->conf, id);
  2049. if (ssid == NULL) {
  2050. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2051. "network id=%d", id);
  2052. return -1;
  2053. }
  2054. if (ssid->disabled == 2) {
  2055. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2056. "DISABLE_NETWORK with persistent P2P "
  2057. "group");
  2058. return -1;
  2059. }
  2060. }
  2061. wpa_supplicant_disable_network(wpa_s, ssid);
  2062. return 0;
  2063. }
  2064. static int wpa_supplicant_ctrl_iface_add_network(
  2065. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2066. {
  2067. struct wpa_ssid *ssid;
  2068. int ret;
  2069. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  2070. ssid = wpa_config_add_network(wpa_s->conf);
  2071. if (ssid == NULL)
  2072. return -1;
  2073. wpas_notify_network_added(wpa_s, ssid);
  2074. ssid->disabled = 1;
  2075. wpa_config_set_network_defaults(ssid);
  2076. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  2077. if (ret < 0 || (size_t) ret >= buflen)
  2078. return -1;
  2079. return ret;
  2080. }
  2081. static int wpa_supplicant_ctrl_iface_remove_network(
  2082. struct wpa_supplicant *wpa_s, char *cmd)
  2083. {
  2084. int id;
  2085. struct wpa_ssid *ssid;
  2086. int was_disabled;
  2087. /* cmd: "<network id>" or "all" */
  2088. if (os_strcmp(cmd, "all") == 0) {
  2089. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  2090. if (wpa_s->sched_scanning)
  2091. wpa_supplicant_cancel_sched_scan(wpa_s);
  2092. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2093. if (wpa_s->current_ssid) {
  2094. #ifdef CONFIG_SME
  2095. wpa_s->sme.prev_bssid_set = 0;
  2096. #endif /* CONFIG_SME */
  2097. wpa_sm_set_config(wpa_s->wpa, NULL);
  2098. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2099. wpa_supplicant_deauthenticate(
  2100. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2101. }
  2102. ssid = wpa_s->conf->ssid;
  2103. while (ssid) {
  2104. struct wpa_ssid *remove_ssid = ssid;
  2105. id = ssid->id;
  2106. ssid = ssid->next;
  2107. wpas_notify_network_removed(wpa_s, remove_ssid);
  2108. wpa_config_remove_network(wpa_s->conf, id);
  2109. }
  2110. return 0;
  2111. }
  2112. id = atoi(cmd);
  2113. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  2114. ssid = wpa_config_get_network(wpa_s->conf, id);
  2115. if (ssid)
  2116. wpas_notify_network_removed(wpa_s, ssid);
  2117. if (ssid == NULL) {
  2118. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2119. "id=%d", id);
  2120. return -1;
  2121. }
  2122. if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
  2123. #ifdef CONFIG_SME
  2124. wpa_s->sme.prev_bssid_set = 0;
  2125. #endif /* CONFIG_SME */
  2126. /*
  2127. * Invalidate the EAP session cache if the current or
  2128. * previously used network is removed.
  2129. */
  2130. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2131. }
  2132. if (ssid == wpa_s->current_ssid) {
  2133. wpa_sm_set_config(wpa_s->wpa, NULL);
  2134. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2135. wpa_supplicant_deauthenticate(wpa_s,
  2136. WLAN_REASON_DEAUTH_LEAVING);
  2137. }
  2138. was_disabled = ssid->disabled;
  2139. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  2140. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  2141. "network id=%d", id);
  2142. return -1;
  2143. }
  2144. if (!was_disabled && wpa_s->sched_scanning) {
  2145. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
  2146. "network from filters");
  2147. wpa_supplicant_cancel_sched_scan(wpa_s);
  2148. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2149. }
  2150. return 0;
  2151. }
  2152. static int wpa_supplicant_ctrl_iface_set_network(
  2153. struct wpa_supplicant *wpa_s, char *cmd)
  2154. {
  2155. int id;
  2156. struct wpa_ssid *ssid;
  2157. char *name, *value;
  2158. /* cmd: "<network id> <variable name> <value>" */
  2159. name = os_strchr(cmd, ' ');
  2160. if (name == NULL)
  2161. return -1;
  2162. *name++ = '\0';
  2163. value = os_strchr(name, ' ');
  2164. if (value == NULL)
  2165. return -1;
  2166. *value++ = '\0';
  2167. id = atoi(cmd);
  2168. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  2169. id, name);
  2170. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2171. (u8 *) value, os_strlen(value));
  2172. ssid = wpa_config_get_network(wpa_s->conf, id);
  2173. if (ssid == NULL) {
  2174. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2175. "id=%d", id);
  2176. return -1;
  2177. }
  2178. if (wpa_config_set(ssid, name, value, 0) < 0) {
  2179. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  2180. "variable '%s'", name);
  2181. return -1;
  2182. }
  2183. if (os_strcmp(name, "bssid") != 0 &&
  2184. os_strcmp(name, "priority") != 0)
  2185. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  2186. if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
  2187. /*
  2188. * Invalidate the EAP session cache if anything in the current
  2189. * or previously used configuration changes.
  2190. */
  2191. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2192. }
  2193. if ((os_strcmp(name, "psk") == 0 &&
  2194. value[0] == '"' && ssid->ssid_len) ||
  2195. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  2196. wpa_config_update_psk(ssid);
  2197. else if (os_strcmp(name, "priority") == 0)
  2198. wpa_config_update_prio_list(wpa_s->conf);
  2199. return 0;
  2200. }
  2201. static int wpa_supplicant_ctrl_iface_get_network(
  2202. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2203. {
  2204. int id;
  2205. size_t res;
  2206. struct wpa_ssid *ssid;
  2207. char *name, *value;
  2208. /* cmd: "<network id> <variable name>" */
  2209. name = os_strchr(cmd, ' ');
  2210. if (name == NULL || buflen == 0)
  2211. return -1;
  2212. *name++ = '\0';
  2213. id = atoi(cmd);
  2214. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2215. id, name);
  2216. ssid = wpa_config_get_network(wpa_s->conf, id);
  2217. if (ssid == NULL) {
  2218. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2219. "id=%d", id);
  2220. return -1;
  2221. }
  2222. value = wpa_config_get_no_key(ssid, name);
  2223. if (value == NULL) {
  2224. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2225. "variable '%s'", name);
  2226. return -1;
  2227. }
  2228. res = os_strlcpy(buf, value, buflen);
  2229. if (res >= buflen) {
  2230. os_free(value);
  2231. return -1;
  2232. }
  2233. os_free(value);
  2234. return res;
  2235. }
  2236. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2237. char *buf, size_t buflen)
  2238. {
  2239. char *pos, *end;
  2240. struct wpa_cred *cred;
  2241. int ret;
  2242. pos = buf;
  2243. end = buf + buflen;
  2244. ret = os_snprintf(pos, end - pos,
  2245. "cred id / realm / username / domain / imsi\n");
  2246. if (ret < 0 || ret >= end - pos)
  2247. return pos - buf;
  2248. pos += ret;
  2249. cred = wpa_s->conf->cred;
  2250. while (cred) {
  2251. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2252. cred->id, cred->realm ? cred->realm : "",
  2253. cred->username ? cred->username : "",
  2254. cred->domain ? cred->domain[0] : "",
  2255. cred->imsi ? cred->imsi : "");
  2256. if (ret < 0 || ret >= end - pos)
  2257. return pos - buf;
  2258. pos += ret;
  2259. cred = cred->next;
  2260. }
  2261. return pos - buf;
  2262. }
  2263. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2264. char *buf, size_t buflen)
  2265. {
  2266. struct wpa_cred *cred;
  2267. int ret;
  2268. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2269. cred = wpa_config_add_cred(wpa_s->conf);
  2270. if (cred == NULL)
  2271. return -1;
  2272. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2273. if (ret < 0 || (size_t) ret >= buflen)
  2274. return -1;
  2275. return ret;
  2276. }
  2277. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2278. struct wpa_cred *cred)
  2279. {
  2280. struct wpa_ssid *ssid;
  2281. char str[20];
  2282. if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
  2283. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2284. return -1;
  2285. }
  2286. /* Remove any network entry created based on the removed credential */
  2287. ssid = wpa_s->conf->ssid;
  2288. while (ssid) {
  2289. if (ssid->parent_cred == cred) {
  2290. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2291. "used the removed credential", ssid->id);
  2292. os_snprintf(str, sizeof(str), "%d", ssid->id);
  2293. ssid = ssid->next;
  2294. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2295. } else
  2296. ssid = ssid->next;
  2297. }
  2298. return 0;
  2299. }
  2300. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2301. char *cmd)
  2302. {
  2303. int id;
  2304. struct wpa_cred *cred, *prev;
  2305. /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
  2306. * "provisioning_sp=<FQDN> */
  2307. if (os_strcmp(cmd, "all") == 0) {
  2308. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2309. cred = wpa_s->conf->cred;
  2310. while (cred) {
  2311. prev = cred;
  2312. cred = cred->next;
  2313. wpas_ctrl_remove_cred(wpa_s, prev);
  2314. }
  2315. return 0;
  2316. }
  2317. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2318. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2319. cmd + 8);
  2320. cred = wpa_s->conf->cred;
  2321. while (cred) {
  2322. prev = cred;
  2323. cred = cred->next;
  2324. if (prev->domain) {
  2325. size_t i;
  2326. for (i = 0; i < prev->num_domain; i++) {
  2327. if (os_strcmp(prev->domain[i], cmd + 8)
  2328. != 0)
  2329. continue;
  2330. wpas_ctrl_remove_cred(wpa_s, prev);
  2331. break;
  2332. }
  2333. }
  2334. }
  2335. return 0;
  2336. }
  2337. if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
  2338. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
  2339. cmd + 16);
  2340. cred = wpa_s->conf->cred;
  2341. while (cred) {
  2342. prev = cred;
  2343. cred = cred->next;
  2344. if (prev->provisioning_sp &&
  2345. os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
  2346. wpas_ctrl_remove_cred(wpa_s, prev);
  2347. }
  2348. return 0;
  2349. }
  2350. id = atoi(cmd);
  2351. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2352. cred = wpa_config_get_cred(wpa_s->conf, id);
  2353. return wpas_ctrl_remove_cred(wpa_s, cred);
  2354. }
  2355. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2356. char *cmd)
  2357. {
  2358. int id;
  2359. struct wpa_cred *cred;
  2360. char *name, *value;
  2361. /* cmd: "<cred id> <variable name> <value>" */
  2362. name = os_strchr(cmd, ' ');
  2363. if (name == NULL)
  2364. return -1;
  2365. *name++ = '\0';
  2366. value = os_strchr(name, ' ');
  2367. if (value == NULL)
  2368. return -1;
  2369. *value++ = '\0';
  2370. id = atoi(cmd);
  2371. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2372. id, name);
  2373. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2374. (u8 *) value, os_strlen(value));
  2375. cred = wpa_config_get_cred(wpa_s->conf, id);
  2376. if (cred == NULL) {
  2377. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2378. id);
  2379. return -1;
  2380. }
  2381. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2382. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2383. "variable '%s'", name);
  2384. return -1;
  2385. }
  2386. return 0;
  2387. }
  2388. #ifndef CONFIG_NO_CONFIG_WRITE
  2389. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2390. {
  2391. int ret;
  2392. if (!wpa_s->conf->update_config) {
  2393. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2394. "to update configuration (update_config=0)");
  2395. return -1;
  2396. }
  2397. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2398. if (ret) {
  2399. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2400. "update configuration");
  2401. } else {
  2402. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2403. " updated");
  2404. }
  2405. return ret;
  2406. }
  2407. #endif /* CONFIG_NO_CONFIG_WRITE */
  2408. struct cipher_info {
  2409. unsigned int capa;
  2410. const char *name;
  2411. int group_only;
  2412. };
  2413. static const struct cipher_info ciphers[] = {
  2414. { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
  2415. { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
  2416. { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
  2417. { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
  2418. { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
  2419. { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
  2420. { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
  2421. { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
  2422. };
  2423. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2424. struct wpa_driver_capa *capa,
  2425. char *buf, size_t buflen)
  2426. {
  2427. int ret;
  2428. char *pos, *end;
  2429. size_t len;
  2430. unsigned int i;
  2431. pos = buf;
  2432. end = pos + buflen;
  2433. if (res < 0) {
  2434. if (strict)
  2435. return 0;
  2436. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2437. if (len >= buflen)
  2438. return -1;
  2439. return len;
  2440. }
  2441. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2442. if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
  2443. ret = os_snprintf(pos, end - pos, "%s%s",
  2444. pos == buf ? "" : " ",
  2445. ciphers[i].name);
  2446. if (ret < 0 || ret >= end - pos)
  2447. return pos - buf;
  2448. pos += ret;
  2449. }
  2450. }
  2451. return pos - buf;
  2452. }
  2453. static int ctrl_iface_get_capability_group(int res, char *strict,
  2454. struct wpa_driver_capa *capa,
  2455. char *buf, size_t buflen)
  2456. {
  2457. int ret;
  2458. char *pos, *end;
  2459. size_t len;
  2460. unsigned int i;
  2461. pos = buf;
  2462. end = pos + buflen;
  2463. if (res < 0) {
  2464. if (strict)
  2465. return 0;
  2466. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2467. if (len >= buflen)
  2468. return -1;
  2469. return len;
  2470. }
  2471. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2472. if (capa->enc & ciphers[i].capa) {
  2473. ret = os_snprintf(pos, end - pos, "%s%s",
  2474. pos == buf ? "" : " ",
  2475. ciphers[i].name);
  2476. if (ret < 0 || ret >= end - pos)
  2477. return pos - buf;
  2478. pos += ret;
  2479. }
  2480. }
  2481. return pos - buf;
  2482. }
  2483. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  2484. struct wpa_driver_capa *capa,
  2485. char *buf, size_t buflen)
  2486. {
  2487. int ret;
  2488. char *pos, *end;
  2489. size_t len;
  2490. pos = buf;
  2491. end = pos + buflen;
  2492. if (res < 0) {
  2493. if (strict)
  2494. return 0;
  2495. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  2496. "NONE", buflen);
  2497. if (len >= buflen)
  2498. return -1;
  2499. return len;
  2500. }
  2501. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  2502. if (ret < 0 || ret >= end - pos)
  2503. return pos - buf;
  2504. pos += ret;
  2505. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2506. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2507. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  2508. if (ret < 0 || ret >= end - pos)
  2509. return pos - buf;
  2510. pos += ret;
  2511. }
  2512. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2513. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2514. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  2515. if (ret < 0 || ret >= end - pos)
  2516. return pos - buf;
  2517. pos += ret;
  2518. }
  2519. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2520. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  2521. if (ret < 0 || ret >= end - pos)
  2522. return pos - buf;
  2523. pos += ret;
  2524. }
  2525. return pos - buf;
  2526. }
  2527. static int ctrl_iface_get_capability_proto(int res, char *strict,
  2528. struct wpa_driver_capa *capa,
  2529. char *buf, size_t buflen)
  2530. {
  2531. int ret;
  2532. char *pos, *end;
  2533. size_t len;
  2534. pos = buf;
  2535. end = pos + buflen;
  2536. if (res < 0) {
  2537. if (strict)
  2538. return 0;
  2539. len = os_strlcpy(buf, "RSN WPA", buflen);
  2540. if (len >= buflen)
  2541. return -1;
  2542. return len;
  2543. }
  2544. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2545. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2546. ret = os_snprintf(pos, end - pos, "%sRSN",
  2547. pos == buf ? "" : " ");
  2548. if (ret < 0 || ret >= end - pos)
  2549. return pos - buf;
  2550. pos += ret;
  2551. }
  2552. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2553. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  2554. ret = os_snprintf(pos, end - pos, "%sWPA",
  2555. pos == buf ? "" : " ");
  2556. if (ret < 0 || ret >= end - pos)
  2557. return pos - buf;
  2558. pos += ret;
  2559. }
  2560. return pos - buf;
  2561. }
  2562. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  2563. struct wpa_driver_capa *capa,
  2564. char *buf, size_t buflen)
  2565. {
  2566. int ret;
  2567. char *pos, *end;
  2568. size_t len;
  2569. pos = buf;
  2570. end = pos + buflen;
  2571. if (res < 0) {
  2572. if (strict)
  2573. return 0;
  2574. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  2575. if (len >= buflen)
  2576. return -1;
  2577. return len;
  2578. }
  2579. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  2580. ret = os_snprintf(pos, end - pos, "%sOPEN",
  2581. pos == buf ? "" : " ");
  2582. if (ret < 0 || ret >= end - pos)
  2583. return pos - buf;
  2584. pos += ret;
  2585. }
  2586. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  2587. ret = os_snprintf(pos, end - pos, "%sSHARED",
  2588. pos == buf ? "" : " ");
  2589. if (ret < 0 || ret >= end - pos)
  2590. return pos - buf;
  2591. pos += ret;
  2592. }
  2593. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  2594. ret = os_snprintf(pos, end - pos, "%sLEAP",
  2595. pos == buf ? "" : " ");
  2596. if (ret < 0 || ret >= end - pos)
  2597. return pos - buf;
  2598. pos += ret;
  2599. }
  2600. return pos - buf;
  2601. }
  2602. static int ctrl_iface_get_capability_modes(int res, char *strict,
  2603. struct wpa_driver_capa *capa,
  2604. char *buf, size_t buflen)
  2605. {
  2606. int ret;
  2607. char *pos, *end;
  2608. size_t len;
  2609. pos = buf;
  2610. end = pos + buflen;
  2611. if (res < 0) {
  2612. if (strict)
  2613. return 0;
  2614. len = os_strlcpy(buf, "IBSS AP", buflen);
  2615. if (len >= buflen)
  2616. return -1;
  2617. return len;
  2618. }
  2619. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  2620. ret = os_snprintf(pos, end - pos, "%sIBSS",
  2621. pos == buf ? "" : " ");
  2622. if (ret < 0 || ret >= end - pos)
  2623. return pos - buf;
  2624. pos += ret;
  2625. }
  2626. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  2627. ret = os_snprintf(pos, end - pos, "%sAP",
  2628. pos == buf ? "" : " ");
  2629. if (ret < 0 || ret >= end - pos)
  2630. return pos - buf;
  2631. pos += ret;
  2632. }
  2633. return pos - buf;
  2634. }
  2635. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  2636. char *buf, size_t buflen)
  2637. {
  2638. struct hostapd_channel_data *chnl;
  2639. int ret, i, j;
  2640. char *pos, *end, *hmode;
  2641. pos = buf;
  2642. end = pos + buflen;
  2643. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2644. switch (wpa_s->hw.modes[j].mode) {
  2645. case HOSTAPD_MODE_IEEE80211B:
  2646. hmode = "B";
  2647. break;
  2648. case HOSTAPD_MODE_IEEE80211G:
  2649. hmode = "G";
  2650. break;
  2651. case HOSTAPD_MODE_IEEE80211A:
  2652. hmode = "A";
  2653. break;
  2654. case HOSTAPD_MODE_IEEE80211AD:
  2655. hmode = "AD";
  2656. break;
  2657. default:
  2658. continue;
  2659. }
  2660. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  2661. if (ret < 0 || ret >= end - pos)
  2662. return pos - buf;
  2663. pos += ret;
  2664. chnl = wpa_s->hw.modes[j].channels;
  2665. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2666. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2667. continue;
  2668. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  2669. if (ret < 0 || ret >= end - pos)
  2670. return pos - buf;
  2671. pos += ret;
  2672. }
  2673. ret = os_snprintf(pos, end - pos, "\n");
  2674. if (ret < 0 || ret >= end - pos)
  2675. return pos - buf;
  2676. pos += ret;
  2677. }
  2678. return pos - buf;
  2679. }
  2680. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  2681. char *buf, size_t buflen)
  2682. {
  2683. struct hostapd_channel_data *chnl;
  2684. int ret, i, j;
  2685. char *pos, *end, *hmode;
  2686. pos = buf;
  2687. end = pos + buflen;
  2688. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2689. switch (wpa_s->hw.modes[j].mode) {
  2690. case HOSTAPD_MODE_IEEE80211B:
  2691. hmode = "B";
  2692. break;
  2693. case HOSTAPD_MODE_IEEE80211G:
  2694. hmode = "G";
  2695. break;
  2696. case HOSTAPD_MODE_IEEE80211A:
  2697. hmode = "A";
  2698. break;
  2699. case HOSTAPD_MODE_IEEE80211AD:
  2700. hmode = "AD";
  2701. break;
  2702. default:
  2703. continue;
  2704. }
  2705. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  2706. hmode);
  2707. if (ret < 0 || ret >= end - pos)
  2708. return pos - buf;
  2709. pos += ret;
  2710. chnl = wpa_s->hw.modes[j].channels;
  2711. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2712. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2713. continue;
  2714. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
  2715. chnl[i].chan, chnl[i].freq,
  2716. chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
  2717. " (NO_IBSS)" : "",
  2718. chnl[i].flag & HOSTAPD_CHAN_RADAR ?
  2719. " (DFS)" : "");
  2720. if (ret < 0 || ret >= end - pos)
  2721. return pos - buf;
  2722. pos += ret;
  2723. }
  2724. ret = os_snprintf(pos, end - pos, "\n");
  2725. if (ret < 0 || ret >= end - pos)
  2726. return pos - buf;
  2727. pos += ret;
  2728. }
  2729. return pos - buf;
  2730. }
  2731. static int wpa_supplicant_ctrl_iface_get_capability(
  2732. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  2733. size_t buflen)
  2734. {
  2735. struct wpa_driver_capa capa;
  2736. int res;
  2737. char *strict;
  2738. char field[30];
  2739. size_t len;
  2740. /* Determine whether or not strict checking was requested */
  2741. len = os_strlcpy(field, _field, sizeof(field));
  2742. if (len >= sizeof(field))
  2743. return -1;
  2744. strict = os_strchr(field, ' ');
  2745. if (strict != NULL) {
  2746. *strict++ = '\0';
  2747. if (os_strcmp(strict, "strict") != 0)
  2748. return -1;
  2749. }
  2750. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  2751. field, strict ? strict : "");
  2752. if (os_strcmp(field, "eap") == 0) {
  2753. return eap_get_names(buf, buflen);
  2754. }
  2755. res = wpa_drv_get_capa(wpa_s, &capa);
  2756. if (os_strcmp(field, "pairwise") == 0)
  2757. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  2758. buf, buflen);
  2759. if (os_strcmp(field, "group") == 0)
  2760. return ctrl_iface_get_capability_group(res, strict, &capa,
  2761. buf, buflen);
  2762. if (os_strcmp(field, "key_mgmt") == 0)
  2763. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  2764. buf, buflen);
  2765. if (os_strcmp(field, "proto") == 0)
  2766. return ctrl_iface_get_capability_proto(res, strict, &capa,
  2767. buf, buflen);
  2768. if (os_strcmp(field, "auth_alg") == 0)
  2769. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  2770. buf, buflen);
  2771. if (os_strcmp(field, "modes") == 0)
  2772. return ctrl_iface_get_capability_modes(res, strict, &capa,
  2773. buf, buflen);
  2774. if (os_strcmp(field, "channels") == 0)
  2775. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  2776. if (os_strcmp(field, "freq") == 0)
  2777. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  2778. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  2779. field);
  2780. return -1;
  2781. }
  2782. #ifdef CONFIG_INTERWORKING
  2783. static char * anqp_add_hex(char *pos, char *end, const char *title,
  2784. struct wpabuf *data)
  2785. {
  2786. char *start = pos;
  2787. size_t i;
  2788. int ret;
  2789. const u8 *d;
  2790. if (data == NULL)
  2791. return start;
  2792. ret = os_snprintf(pos, end - pos, "%s=", title);
  2793. if (ret < 0 || ret >= end - pos)
  2794. return start;
  2795. pos += ret;
  2796. d = wpabuf_head_u8(data);
  2797. for (i = 0; i < wpabuf_len(data); i++) {
  2798. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  2799. if (ret < 0 || ret >= end - pos)
  2800. return start;
  2801. pos += ret;
  2802. }
  2803. ret = os_snprintf(pos, end - pos, "\n");
  2804. if (ret < 0 || ret >= end - pos)
  2805. return start;
  2806. pos += ret;
  2807. return pos;
  2808. }
  2809. #endif /* CONFIG_INTERWORKING */
  2810. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2811. unsigned long mask, char *buf, size_t buflen)
  2812. {
  2813. size_t i;
  2814. int ret;
  2815. char *pos, *end;
  2816. const u8 *ie, *ie2;
  2817. pos = buf;
  2818. end = buf + buflen;
  2819. if (mask & WPA_BSS_MASK_ID) {
  2820. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  2821. if (ret < 0 || ret >= end - pos)
  2822. return 0;
  2823. pos += ret;
  2824. }
  2825. if (mask & WPA_BSS_MASK_BSSID) {
  2826. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  2827. MAC2STR(bss->bssid));
  2828. if (ret < 0 || ret >= end - pos)
  2829. return 0;
  2830. pos += ret;
  2831. }
  2832. if (mask & WPA_BSS_MASK_FREQ) {
  2833. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  2834. if (ret < 0 || ret >= end - pos)
  2835. return 0;
  2836. pos += ret;
  2837. }
  2838. if (mask & WPA_BSS_MASK_BEACON_INT) {
  2839. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  2840. bss->beacon_int);
  2841. if (ret < 0 || ret >= end - pos)
  2842. return 0;
  2843. pos += ret;
  2844. }
  2845. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  2846. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  2847. bss->caps);
  2848. if (ret < 0 || ret >= end - pos)
  2849. return 0;
  2850. pos += ret;
  2851. }
  2852. if (mask & WPA_BSS_MASK_QUAL) {
  2853. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  2854. if (ret < 0 || ret >= end - pos)
  2855. return 0;
  2856. pos += ret;
  2857. }
  2858. if (mask & WPA_BSS_MASK_NOISE) {
  2859. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  2860. if (ret < 0 || ret >= end - pos)
  2861. return 0;
  2862. pos += ret;
  2863. }
  2864. if (mask & WPA_BSS_MASK_LEVEL) {
  2865. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  2866. if (ret < 0 || ret >= end - pos)
  2867. return 0;
  2868. pos += ret;
  2869. }
  2870. if (mask & WPA_BSS_MASK_TSF) {
  2871. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  2872. (unsigned long long) bss->tsf);
  2873. if (ret < 0 || ret >= end - pos)
  2874. return 0;
  2875. pos += ret;
  2876. }
  2877. if (mask & WPA_BSS_MASK_AGE) {
  2878. struct os_reltime now;
  2879. os_get_reltime(&now);
  2880. ret = os_snprintf(pos, end - pos, "age=%d\n",
  2881. (int) (now.sec - bss->last_update.sec));
  2882. if (ret < 0 || ret >= end - pos)
  2883. return 0;
  2884. pos += ret;
  2885. }
  2886. if (mask & WPA_BSS_MASK_IE) {
  2887. ret = os_snprintf(pos, end - pos, "ie=");
  2888. if (ret < 0 || ret >= end - pos)
  2889. return 0;
  2890. pos += ret;
  2891. ie = (const u8 *) (bss + 1);
  2892. for (i = 0; i < bss->ie_len; i++) {
  2893. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  2894. if (ret < 0 || ret >= end - pos)
  2895. return 0;
  2896. pos += ret;
  2897. }
  2898. ret = os_snprintf(pos, end - pos, "\n");
  2899. if (ret < 0 || ret >= end - pos)
  2900. return 0;
  2901. pos += ret;
  2902. }
  2903. if (mask & WPA_BSS_MASK_FLAGS) {
  2904. ret = os_snprintf(pos, end - pos, "flags=");
  2905. if (ret < 0 || ret >= end - pos)
  2906. return 0;
  2907. pos += ret;
  2908. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2909. if (ie)
  2910. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  2911. 2 + ie[1]);
  2912. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2913. if (ie2)
  2914. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  2915. 2 + ie2[1]);
  2916. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2917. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  2918. ret = os_snprintf(pos, end - pos, "[WEP]");
  2919. if (ret < 0 || ret >= end - pos)
  2920. return 0;
  2921. pos += ret;
  2922. }
  2923. if (bss->caps & IEEE80211_CAP_IBSS) {
  2924. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2925. if (ret < 0 || ret >= end - pos)
  2926. return 0;
  2927. pos += ret;
  2928. }
  2929. if (bss->caps & IEEE80211_CAP_ESS) {
  2930. ret = os_snprintf(pos, end - pos, "[ESS]");
  2931. if (ret < 0 || ret >= end - pos)
  2932. return 0;
  2933. pos += ret;
  2934. }
  2935. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  2936. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  2937. ret = os_snprintf(pos, end - pos, "[P2P]");
  2938. if (ret < 0 || ret >= end - pos)
  2939. return 0;
  2940. pos += ret;
  2941. }
  2942. #ifdef CONFIG_HS20
  2943. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  2944. ret = os_snprintf(pos, end - pos, "[HS20]");
  2945. if (ret < 0 || ret >= end - pos)
  2946. return 0;
  2947. pos += ret;
  2948. }
  2949. #endif /* CONFIG_HS20 */
  2950. ret = os_snprintf(pos, end - pos, "\n");
  2951. if (ret < 0 || ret >= end - pos)
  2952. return 0;
  2953. pos += ret;
  2954. }
  2955. if (mask & WPA_BSS_MASK_SSID) {
  2956. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  2957. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2958. if (ret < 0 || ret >= end - pos)
  2959. return 0;
  2960. pos += ret;
  2961. }
  2962. #ifdef CONFIG_WPS
  2963. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  2964. ie = (const u8 *) (bss + 1);
  2965. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  2966. if (ret < 0 || ret >= end - pos)
  2967. return 0;
  2968. pos += ret;
  2969. }
  2970. #endif /* CONFIG_WPS */
  2971. #ifdef CONFIG_P2P
  2972. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  2973. ie = (const u8 *) (bss + 1);
  2974. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  2975. if (ret < 0 || ret >= end - pos)
  2976. return 0;
  2977. pos += ret;
  2978. }
  2979. #endif /* CONFIG_P2P */
  2980. #ifdef CONFIG_WIFI_DISPLAY
  2981. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  2982. struct wpabuf *wfd;
  2983. ie = (const u8 *) (bss + 1);
  2984. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  2985. WFD_IE_VENDOR_TYPE);
  2986. if (wfd) {
  2987. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  2988. if (ret < 0 || ret >= end - pos) {
  2989. wpabuf_free(wfd);
  2990. return 0;
  2991. }
  2992. pos += ret;
  2993. pos += wpa_snprintf_hex(pos, end - pos,
  2994. wpabuf_head(wfd),
  2995. wpabuf_len(wfd));
  2996. wpabuf_free(wfd);
  2997. ret = os_snprintf(pos, end - pos, "\n");
  2998. if (ret < 0 || ret >= end - pos)
  2999. return 0;
  3000. pos += ret;
  3001. }
  3002. }
  3003. #endif /* CONFIG_WIFI_DISPLAY */
  3004. #ifdef CONFIG_INTERWORKING
  3005. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  3006. struct wpa_bss_anqp *anqp = bss->anqp;
  3007. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  3008. anqp->venue_name);
  3009. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  3010. anqp->network_auth_type);
  3011. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  3012. anqp->roaming_consortium);
  3013. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  3014. anqp->ip_addr_type_availability);
  3015. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  3016. anqp->nai_realm);
  3017. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  3018. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  3019. anqp->domain_name);
  3020. #ifdef CONFIG_HS20
  3021. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  3022. anqp->hs20_operator_friendly_name);
  3023. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  3024. anqp->hs20_wan_metrics);
  3025. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  3026. anqp->hs20_connection_capability);
  3027. pos = anqp_add_hex(pos, end, "hs20_operating_class",
  3028. anqp->hs20_operating_class);
  3029. pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
  3030. anqp->hs20_osu_providers_list);
  3031. #endif /* CONFIG_HS20 */
  3032. }
  3033. #endif /* CONFIG_INTERWORKING */
  3034. if (mask & WPA_BSS_MASK_DELIM) {
  3035. ret = os_snprintf(pos, end - pos, "====\n");
  3036. if (ret < 0 || ret >= end - pos)
  3037. return 0;
  3038. pos += ret;
  3039. }
  3040. return pos - buf;
  3041. }
  3042. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  3043. const char *cmd, char *buf,
  3044. size_t buflen)
  3045. {
  3046. u8 bssid[ETH_ALEN];
  3047. size_t i;
  3048. struct wpa_bss *bss;
  3049. struct wpa_bss *bsslast = NULL;
  3050. struct dl_list *next;
  3051. int ret = 0;
  3052. int len;
  3053. char *ctmp;
  3054. unsigned long mask = WPA_BSS_MASK_ALL;
  3055. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  3056. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  3057. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  3058. list_id);
  3059. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  3060. list_id);
  3061. } else { /* N1-N2 */
  3062. unsigned int id1, id2;
  3063. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  3064. wpa_printf(MSG_INFO, "Wrong BSS range "
  3065. "format");
  3066. return 0;
  3067. }
  3068. if (*(cmd + 6) == '-')
  3069. id1 = 0;
  3070. else
  3071. id1 = atoi(cmd + 6);
  3072. ctmp++;
  3073. if (*ctmp >= '0' && *ctmp <= '9')
  3074. id2 = atoi(ctmp);
  3075. else
  3076. id2 = (unsigned int) -1;
  3077. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  3078. if (id2 == (unsigned int) -1)
  3079. bsslast = dl_list_last(&wpa_s->bss_id,
  3080. struct wpa_bss,
  3081. list_id);
  3082. else {
  3083. bsslast = wpa_bss_get_id(wpa_s, id2);
  3084. if (bsslast == NULL && bss && id2 > id1) {
  3085. struct wpa_bss *tmp = bss;
  3086. for (;;) {
  3087. next = tmp->list_id.next;
  3088. if (next == &wpa_s->bss_id)
  3089. break;
  3090. tmp = dl_list_entry(
  3091. next, struct wpa_bss,
  3092. list_id);
  3093. if (tmp->id > id2)
  3094. break;
  3095. bsslast = tmp;
  3096. }
  3097. }
  3098. }
  3099. }
  3100. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  3101. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  3102. else if (os_strncmp(cmd, "LAST", 4) == 0)
  3103. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  3104. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  3105. i = atoi(cmd + 3);
  3106. bss = wpa_bss_get_id(wpa_s, i);
  3107. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3108. i = atoi(cmd + 5);
  3109. bss = wpa_bss_get_id(wpa_s, i);
  3110. if (bss) {
  3111. next = bss->list_id.next;
  3112. if (next == &wpa_s->bss_id)
  3113. bss = NULL;
  3114. else
  3115. bss = dl_list_entry(next, struct wpa_bss,
  3116. list_id);
  3117. }
  3118. #ifdef CONFIG_P2P
  3119. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  3120. if (hwaddr_aton(cmd + 13, bssid) == 0)
  3121. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  3122. else
  3123. bss = NULL;
  3124. #endif /* CONFIG_P2P */
  3125. } else if (hwaddr_aton(cmd, bssid) == 0)
  3126. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3127. else {
  3128. struct wpa_bss *tmp;
  3129. i = atoi(cmd);
  3130. bss = NULL;
  3131. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  3132. {
  3133. if (i-- == 0) {
  3134. bss = tmp;
  3135. break;
  3136. }
  3137. }
  3138. }
  3139. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  3140. mask = strtoul(ctmp + 5, NULL, 0x10);
  3141. if (mask == 0)
  3142. mask = WPA_BSS_MASK_ALL;
  3143. }
  3144. if (bss == NULL)
  3145. return 0;
  3146. if (bsslast == NULL)
  3147. bsslast = bss;
  3148. do {
  3149. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  3150. ret += len;
  3151. buf += len;
  3152. buflen -= len;
  3153. if (bss == bsslast) {
  3154. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  3155. (bss == dl_list_last(&wpa_s->bss_id,
  3156. struct wpa_bss, list_id)))
  3157. os_snprintf(buf - 5, 5, "####\n");
  3158. break;
  3159. }
  3160. next = bss->list_id.next;
  3161. if (next == &wpa_s->bss_id)
  3162. break;
  3163. bss = dl_list_entry(next, struct wpa_bss, list_id);
  3164. } while (bss && len);
  3165. return ret;
  3166. }
  3167. static int wpa_supplicant_ctrl_iface_ap_scan(
  3168. struct wpa_supplicant *wpa_s, char *cmd)
  3169. {
  3170. int ap_scan = atoi(cmd);
  3171. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  3172. }
  3173. static int wpa_supplicant_ctrl_iface_scan_interval(
  3174. struct wpa_supplicant *wpa_s, char *cmd)
  3175. {
  3176. int scan_int = atoi(cmd);
  3177. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  3178. }
  3179. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  3180. struct wpa_supplicant *wpa_s, char *cmd)
  3181. {
  3182. int expire_age = atoi(cmd);
  3183. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  3184. }
  3185. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  3186. struct wpa_supplicant *wpa_s, char *cmd)
  3187. {
  3188. int expire_count = atoi(cmd);
  3189. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  3190. }
  3191. static int wpa_supplicant_ctrl_iface_bss_flush(
  3192. struct wpa_supplicant *wpa_s, char *cmd)
  3193. {
  3194. int flush_age = atoi(cmd);
  3195. if (flush_age == 0)
  3196. wpa_bss_flush(wpa_s);
  3197. else
  3198. wpa_bss_flush_by_age(wpa_s, flush_age);
  3199. return 0;
  3200. }
  3201. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  3202. {
  3203. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  3204. /* MLME-DELETEKEYS.request */
  3205. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  3206. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  3207. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  3208. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  3209. #ifdef CONFIG_IEEE80211W
  3210. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  3211. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  3212. #endif /* CONFIG_IEEE80211W */
  3213. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  3214. 0);
  3215. /* MLME-SETPROTECTION.request(None) */
  3216. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  3217. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  3218. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  3219. wpa_sm_drop_sa(wpa_s->wpa);
  3220. }
  3221. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  3222. char *addr)
  3223. {
  3224. #ifdef CONFIG_NO_SCAN_PROCESSING
  3225. return -1;
  3226. #else /* CONFIG_NO_SCAN_PROCESSING */
  3227. u8 bssid[ETH_ALEN];
  3228. struct wpa_bss *bss;
  3229. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3230. if (hwaddr_aton(addr, bssid)) {
  3231. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  3232. "address '%s'", addr);
  3233. return -1;
  3234. }
  3235. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  3236. if (!ssid) {
  3237. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  3238. "configuration known for the target AP");
  3239. return -1;
  3240. }
  3241. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  3242. if (!bss) {
  3243. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  3244. "from BSS table");
  3245. return -1;
  3246. }
  3247. /*
  3248. * TODO: Find best network configuration block from configuration to
  3249. * allow roaming to other networks
  3250. */
  3251. wpa_s->reassociate = 1;
  3252. wpa_supplicant_connect(wpa_s, bss, ssid);
  3253. return 0;
  3254. #endif /* CONFIG_NO_SCAN_PROCESSING */
  3255. }
  3256. #ifdef CONFIG_P2P
  3257. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  3258. {
  3259. unsigned int timeout = atoi(cmd);
  3260. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  3261. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  3262. u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
  3263. char *pos;
  3264. unsigned int search_delay;
  3265. if (os_strstr(cmd, "type=social"))
  3266. type = P2P_FIND_ONLY_SOCIAL;
  3267. else if (os_strstr(cmd, "type=progressive"))
  3268. type = P2P_FIND_PROGRESSIVE;
  3269. pos = os_strstr(cmd, "dev_id=");
  3270. if (pos) {
  3271. pos += 7;
  3272. if (hwaddr_aton(pos, dev_id))
  3273. return -1;
  3274. _dev_id = dev_id;
  3275. }
  3276. pos = os_strstr(cmd, "dev_type=");
  3277. if (pos) {
  3278. pos += 9;
  3279. if (wps_dev_type_str2bin(pos, dev_type) < 0)
  3280. return -1;
  3281. _dev_type = dev_type;
  3282. }
  3283. pos = os_strstr(cmd, "delay=");
  3284. if (pos) {
  3285. pos += 6;
  3286. search_delay = atoi(pos);
  3287. } else
  3288. search_delay = wpas_p2p_search_delay(wpa_s);
  3289. return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
  3290. _dev_id, search_delay);
  3291. }
  3292. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  3293. char *buf, size_t buflen)
  3294. {
  3295. u8 addr[ETH_ALEN];
  3296. char *pos, *pos2;
  3297. char *pin = NULL;
  3298. enum p2p_wps_method wps_method;
  3299. int new_pin;
  3300. int ret;
  3301. int persistent_group, persistent_id = -1;
  3302. int join;
  3303. int auth;
  3304. int automatic;
  3305. int go_intent = -1;
  3306. int freq = 0;
  3307. int pd;
  3308. int ht40, vht;
  3309. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
  3310. * [persistent|persistent=<network id>]
  3311. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  3312. * [ht40] [vht] */
  3313. if (hwaddr_aton(cmd, addr))
  3314. return -1;
  3315. pos = cmd + 17;
  3316. if (*pos != ' ')
  3317. return -1;
  3318. pos++;
  3319. persistent_group = os_strstr(pos, " persistent") != NULL;
  3320. pos2 = os_strstr(pos, " persistent=");
  3321. if (pos2) {
  3322. struct wpa_ssid *ssid;
  3323. persistent_id = atoi(pos2 + 12);
  3324. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3325. if (ssid == NULL || ssid->disabled != 2 ||
  3326. ssid->mode != WPAS_MODE_P2P_GO) {
  3327. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  3328. "SSID id=%d for persistent P2P group (GO)",
  3329. persistent_id);
  3330. return -1;
  3331. }
  3332. }
  3333. join = os_strstr(pos, " join") != NULL;
  3334. auth = os_strstr(pos, " auth") != NULL;
  3335. automatic = os_strstr(pos, " auto") != NULL;
  3336. pd = os_strstr(pos, " provdisc") != NULL;
  3337. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3338. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3339. vht;
  3340. pos2 = os_strstr(pos, " go_intent=");
  3341. if (pos2) {
  3342. pos2 += 11;
  3343. go_intent = atoi(pos2);
  3344. if (go_intent < 0 || go_intent > 15)
  3345. return -1;
  3346. }
  3347. pos2 = os_strstr(pos, " freq=");
  3348. if (pos2) {
  3349. pos2 += 6;
  3350. freq = atoi(pos2);
  3351. if (freq <= 0)
  3352. return -1;
  3353. }
  3354. if (os_strncmp(pos, "pin", 3) == 0) {
  3355. /* Request random PIN (to be displayed) and enable the PIN */
  3356. wps_method = WPS_PIN_DISPLAY;
  3357. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  3358. wps_method = WPS_PBC;
  3359. } else {
  3360. pin = pos;
  3361. pos = os_strchr(pin, ' ');
  3362. wps_method = WPS_PIN_KEYPAD;
  3363. if (pos) {
  3364. *pos++ = '\0';
  3365. if (os_strncmp(pos, "display", 7) == 0)
  3366. wps_method = WPS_PIN_DISPLAY;
  3367. }
  3368. if (!wps_pin_str_valid(pin)) {
  3369. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  3370. return 17;
  3371. }
  3372. }
  3373. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  3374. persistent_group, automatic, join,
  3375. auth, go_intent, freq, persistent_id, pd,
  3376. ht40, vht);
  3377. if (new_pin == -2) {
  3378. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  3379. return 25;
  3380. }
  3381. if (new_pin == -3) {
  3382. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  3383. return 25;
  3384. }
  3385. if (new_pin < 0)
  3386. return -1;
  3387. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  3388. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  3389. if (ret < 0 || (size_t) ret >= buflen)
  3390. return -1;
  3391. return ret;
  3392. }
  3393. os_memcpy(buf, "OK\n", 3);
  3394. return 3;
  3395. }
  3396. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3397. {
  3398. unsigned int timeout = atoi(cmd);
  3399. return wpas_p2p_listen(wpa_s, timeout);
  3400. }
  3401. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  3402. {
  3403. u8 addr[ETH_ALEN];
  3404. char *pos;
  3405. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  3406. /* <addr> <config method> [join|auto] */
  3407. if (hwaddr_aton(cmd, addr))
  3408. return -1;
  3409. pos = cmd + 17;
  3410. if (*pos != ' ')
  3411. return -1;
  3412. pos++;
  3413. if (os_strstr(pos, " join") != NULL)
  3414. use = WPAS_P2P_PD_FOR_JOIN;
  3415. else if (os_strstr(pos, " auto") != NULL)
  3416. use = WPAS_P2P_PD_AUTO;
  3417. return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
  3418. }
  3419. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  3420. size_t buflen)
  3421. {
  3422. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3423. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  3424. ssid->passphrase == NULL)
  3425. return -1;
  3426. os_strlcpy(buf, ssid->passphrase, buflen);
  3427. return os_strlen(buf);
  3428. }
  3429. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  3430. char *buf, size_t buflen)
  3431. {
  3432. u64 ref;
  3433. int res;
  3434. u8 dst_buf[ETH_ALEN], *dst;
  3435. struct wpabuf *tlvs;
  3436. char *pos;
  3437. size_t len;
  3438. if (hwaddr_aton(cmd, dst_buf))
  3439. return -1;
  3440. dst = dst_buf;
  3441. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  3442. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  3443. dst = NULL;
  3444. pos = cmd + 17;
  3445. if (*pos != ' ')
  3446. return -1;
  3447. pos++;
  3448. if (os_strncmp(pos, "upnp ", 5) == 0) {
  3449. u8 version;
  3450. pos += 5;
  3451. if (hexstr2bin(pos, &version, 1) < 0)
  3452. return -1;
  3453. pos += 2;
  3454. if (*pos != ' ')
  3455. return -1;
  3456. pos++;
  3457. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  3458. #ifdef CONFIG_WIFI_DISPLAY
  3459. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  3460. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  3461. #endif /* CONFIG_WIFI_DISPLAY */
  3462. } else {
  3463. len = os_strlen(pos);
  3464. if (len & 1)
  3465. return -1;
  3466. len /= 2;
  3467. tlvs = wpabuf_alloc(len);
  3468. if (tlvs == NULL)
  3469. return -1;
  3470. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  3471. wpabuf_free(tlvs);
  3472. return -1;
  3473. }
  3474. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  3475. wpabuf_free(tlvs);
  3476. }
  3477. if (ref == 0)
  3478. return -1;
  3479. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  3480. if (res < 0 || (unsigned) res >= buflen)
  3481. return -1;
  3482. return res;
  3483. }
  3484. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  3485. char *cmd)
  3486. {
  3487. long long unsigned val;
  3488. u64 req;
  3489. if (sscanf(cmd, "%llx", &val) != 1)
  3490. return -1;
  3491. req = val;
  3492. return wpas_p2p_sd_cancel_request(wpa_s, req);
  3493. }
  3494. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  3495. {
  3496. int freq;
  3497. u8 dst[ETH_ALEN];
  3498. u8 dialog_token;
  3499. struct wpabuf *resp_tlvs;
  3500. char *pos, *pos2;
  3501. size_t len;
  3502. pos = os_strchr(cmd, ' ');
  3503. if (pos == NULL)
  3504. return -1;
  3505. *pos++ = '\0';
  3506. freq = atoi(cmd);
  3507. if (freq == 0)
  3508. return -1;
  3509. if (hwaddr_aton(pos, dst))
  3510. return -1;
  3511. pos += 17;
  3512. if (*pos != ' ')
  3513. return -1;
  3514. pos++;
  3515. pos2 = os_strchr(pos, ' ');
  3516. if (pos2 == NULL)
  3517. return -1;
  3518. *pos2++ = '\0';
  3519. dialog_token = atoi(pos);
  3520. len = os_strlen(pos2);
  3521. if (len & 1)
  3522. return -1;
  3523. len /= 2;
  3524. resp_tlvs = wpabuf_alloc(len);
  3525. if (resp_tlvs == NULL)
  3526. return -1;
  3527. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  3528. wpabuf_free(resp_tlvs);
  3529. return -1;
  3530. }
  3531. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  3532. wpabuf_free(resp_tlvs);
  3533. return 0;
  3534. }
  3535. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  3536. char *cmd)
  3537. {
  3538. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  3539. return -1;
  3540. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  3541. return 0;
  3542. }
  3543. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  3544. char *cmd)
  3545. {
  3546. char *pos;
  3547. size_t len;
  3548. struct wpabuf *query, *resp;
  3549. pos = os_strchr(cmd, ' ');
  3550. if (pos == NULL)
  3551. return -1;
  3552. *pos++ = '\0';
  3553. len = os_strlen(cmd);
  3554. if (len & 1)
  3555. return -1;
  3556. len /= 2;
  3557. query = wpabuf_alloc(len);
  3558. if (query == NULL)
  3559. return -1;
  3560. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3561. wpabuf_free(query);
  3562. return -1;
  3563. }
  3564. len = os_strlen(pos);
  3565. if (len & 1) {
  3566. wpabuf_free(query);
  3567. return -1;
  3568. }
  3569. len /= 2;
  3570. resp = wpabuf_alloc(len);
  3571. if (resp == NULL) {
  3572. wpabuf_free(query);
  3573. return -1;
  3574. }
  3575. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  3576. wpabuf_free(query);
  3577. wpabuf_free(resp);
  3578. return -1;
  3579. }
  3580. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  3581. wpabuf_free(query);
  3582. wpabuf_free(resp);
  3583. return -1;
  3584. }
  3585. return 0;
  3586. }
  3587. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3588. {
  3589. char *pos;
  3590. u8 version;
  3591. pos = os_strchr(cmd, ' ');
  3592. if (pos == NULL)
  3593. return -1;
  3594. *pos++ = '\0';
  3595. if (hexstr2bin(cmd, &version, 1) < 0)
  3596. return -1;
  3597. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  3598. }
  3599. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  3600. {
  3601. char *pos;
  3602. pos = os_strchr(cmd, ' ');
  3603. if (pos == NULL)
  3604. return -1;
  3605. *pos++ = '\0';
  3606. if (os_strcmp(cmd, "bonjour") == 0)
  3607. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  3608. if (os_strcmp(cmd, "upnp") == 0)
  3609. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  3610. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3611. return -1;
  3612. }
  3613. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  3614. char *cmd)
  3615. {
  3616. size_t len;
  3617. struct wpabuf *query;
  3618. int ret;
  3619. len = os_strlen(cmd);
  3620. if (len & 1)
  3621. return -1;
  3622. len /= 2;
  3623. query = wpabuf_alloc(len);
  3624. if (query == NULL)
  3625. return -1;
  3626. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3627. wpabuf_free(query);
  3628. return -1;
  3629. }
  3630. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  3631. wpabuf_free(query);
  3632. return ret;
  3633. }
  3634. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3635. {
  3636. char *pos;
  3637. u8 version;
  3638. pos = os_strchr(cmd, ' ');
  3639. if (pos == NULL)
  3640. return -1;
  3641. *pos++ = '\0';
  3642. if (hexstr2bin(cmd, &version, 1) < 0)
  3643. return -1;
  3644. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  3645. }
  3646. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  3647. {
  3648. char *pos;
  3649. pos = os_strchr(cmd, ' ');
  3650. if (pos == NULL)
  3651. return -1;
  3652. *pos++ = '\0';
  3653. if (os_strcmp(cmd, "bonjour") == 0)
  3654. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  3655. if (os_strcmp(cmd, "upnp") == 0)
  3656. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  3657. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3658. return -1;
  3659. }
  3660. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  3661. {
  3662. u8 addr[ETH_ALEN];
  3663. /* <addr> */
  3664. if (hwaddr_aton(cmd, addr))
  3665. return -1;
  3666. return wpas_p2p_reject(wpa_s, addr);
  3667. }
  3668. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  3669. {
  3670. char *pos;
  3671. int id;
  3672. struct wpa_ssid *ssid;
  3673. u8 *_peer = NULL, peer[ETH_ALEN];
  3674. int freq = 0, pref_freq = 0;
  3675. int ht40, vht;
  3676. id = atoi(cmd);
  3677. pos = os_strstr(cmd, " peer=");
  3678. if (pos) {
  3679. pos += 6;
  3680. if (hwaddr_aton(pos, peer))
  3681. return -1;
  3682. _peer = peer;
  3683. }
  3684. ssid = wpa_config_get_network(wpa_s->conf, id);
  3685. if (ssid == NULL || ssid->disabled != 2) {
  3686. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3687. "for persistent P2P group",
  3688. id);
  3689. return -1;
  3690. }
  3691. pos = os_strstr(cmd, " freq=");
  3692. if (pos) {
  3693. pos += 6;
  3694. freq = atoi(pos);
  3695. if (freq <= 0)
  3696. return -1;
  3697. }
  3698. pos = os_strstr(cmd, " pref=");
  3699. if (pos) {
  3700. pos += 6;
  3701. pref_freq = atoi(pos);
  3702. if (pref_freq <= 0)
  3703. return -1;
  3704. }
  3705. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3706. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3707. vht;
  3708. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
  3709. pref_freq);
  3710. }
  3711. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  3712. {
  3713. char *pos;
  3714. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  3715. pos = os_strstr(cmd, " peer=");
  3716. if (!pos)
  3717. return -1;
  3718. *pos = '\0';
  3719. pos += 6;
  3720. if (hwaddr_aton(pos, peer)) {
  3721. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  3722. return -1;
  3723. }
  3724. pos = os_strstr(pos, " go_dev_addr=");
  3725. if (pos) {
  3726. pos += 13;
  3727. if (hwaddr_aton(pos, go_dev_addr)) {
  3728. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  3729. pos);
  3730. return -1;
  3731. }
  3732. go_dev = go_dev_addr;
  3733. }
  3734. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  3735. }
  3736. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  3737. {
  3738. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3739. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  3740. if (os_strncmp(cmd, "group=", 6) == 0)
  3741. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  3742. return -1;
  3743. }
  3744. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  3745. char *cmd, int freq, int ht40,
  3746. int vht)
  3747. {
  3748. int id;
  3749. struct wpa_ssid *ssid;
  3750. id = atoi(cmd);
  3751. ssid = wpa_config_get_network(wpa_s->conf, id);
  3752. if (ssid == NULL || ssid->disabled != 2) {
  3753. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3754. "for persistent P2P group",
  3755. id);
  3756. return -1;
  3757. }
  3758. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
  3759. NULL, 0);
  3760. }
  3761. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  3762. {
  3763. int freq = 0, ht40, vht;
  3764. char *pos;
  3765. pos = os_strstr(cmd, "freq=");
  3766. if (pos)
  3767. freq = atoi(pos + 5);
  3768. vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3769. ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3770. vht;
  3771. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3772. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
  3773. ht40, vht);
  3774. if (os_strcmp(cmd, "persistent") == 0 ||
  3775. os_strncmp(cmd, "persistent ", 11) == 0)
  3776. return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
  3777. if (os_strncmp(cmd, "freq=", 5) == 0)
  3778. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3779. if (ht40)
  3780. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3781. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  3782. cmd);
  3783. return -1;
  3784. }
  3785. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  3786. char *buf, size_t buflen)
  3787. {
  3788. u8 addr[ETH_ALEN], *addr_ptr;
  3789. int next, res;
  3790. const struct p2p_peer_info *info;
  3791. char *pos, *end;
  3792. char devtype[WPS_DEV_TYPE_BUFSIZE];
  3793. struct wpa_ssid *ssid;
  3794. size_t i;
  3795. if (!wpa_s->global->p2p)
  3796. return -1;
  3797. if (os_strcmp(cmd, "FIRST") == 0) {
  3798. addr_ptr = NULL;
  3799. next = 0;
  3800. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3801. if (hwaddr_aton(cmd + 5, addr) < 0)
  3802. return -1;
  3803. addr_ptr = addr;
  3804. next = 1;
  3805. } else {
  3806. if (hwaddr_aton(cmd, addr) < 0)
  3807. return -1;
  3808. addr_ptr = addr;
  3809. next = 0;
  3810. }
  3811. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  3812. if (info == NULL)
  3813. return -1;
  3814. pos = buf;
  3815. end = buf + buflen;
  3816. res = os_snprintf(pos, end - pos, MACSTR "\n"
  3817. "pri_dev_type=%s\n"
  3818. "device_name=%s\n"
  3819. "manufacturer=%s\n"
  3820. "model_name=%s\n"
  3821. "model_number=%s\n"
  3822. "serial_number=%s\n"
  3823. "config_methods=0x%x\n"
  3824. "dev_capab=0x%x\n"
  3825. "group_capab=0x%x\n"
  3826. "level=%d\n",
  3827. MAC2STR(info->p2p_device_addr),
  3828. wps_dev_type_bin2str(info->pri_dev_type,
  3829. devtype, sizeof(devtype)),
  3830. info->device_name,
  3831. info->manufacturer,
  3832. info->model_name,
  3833. info->model_number,
  3834. info->serial_number,
  3835. info->config_methods,
  3836. info->dev_capab,
  3837. info->group_capab,
  3838. info->level);
  3839. if (res < 0 || res >= end - pos)
  3840. return pos - buf;
  3841. pos += res;
  3842. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  3843. {
  3844. const u8 *t;
  3845. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  3846. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  3847. wps_dev_type_bin2str(t, devtype,
  3848. sizeof(devtype)));
  3849. if (res < 0 || res >= end - pos)
  3850. return pos - buf;
  3851. pos += res;
  3852. }
  3853. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  3854. if (ssid) {
  3855. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  3856. if (res < 0 || res >= end - pos)
  3857. return pos - buf;
  3858. pos += res;
  3859. }
  3860. res = p2p_get_peer_info_txt(info, pos, end - pos);
  3861. if (res < 0)
  3862. return pos - buf;
  3863. pos += res;
  3864. return pos - buf;
  3865. }
  3866. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  3867. const char *param)
  3868. {
  3869. unsigned int i;
  3870. if (wpa_s->global->p2p == NULL)
  3871. return -1;
  3872. if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
  3873. return -1;
  3874. for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
  3875. struct wpa_freq_range *freq;
  3876. freq = &wpa_s->global->p2p_disallow_freq.range[i];
  3877. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  3878. freq->min, freq->max);
  3879. }
  3880. wpas_p2p_update_channel_list(wpa_s);
  3881. return 0;
  3882. }
  3883. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  3884. {
  3885. char *param;
  3886. if (wpa_s->global->p2p == NULL)
  3887. return -1;
  3888. param = os_strchr(cmd, ' ');
  3889. if (param == NULL)
  3890. return -1;
  3891. *param++ = '\0';
  3892. if (os_strcmp(cmd, "discoverability") == 0) {
  3893. p2p_set_client_discoverability(wpa_s->global->p2p,
  3894. atoi(param));
  3895. return 0;
  3896. }
  3897. if (os_strcmp(cmd, "managed") == 0) {
  3898. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  3899. return 0;
  3900. }
  3901. if (os_strcmp(cmd, "listen_channel") == 0) {
  3902. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  3903. atoi(param));
  3904. }
  3905. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  3906. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  3907. os_strlen(param));
  3908. }
  3909. if (os_strcmp(cmd, "noa") == 0) {
  3910. char *pos;
  3911. int count, start, duration;
  3912. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  3913. count = atoi(param);
  3914. pos = os_strchr(param, ',');
  3915. if (pos == NULL)
  3916. return -1;
  3917. pos++;
  3918. start = atoi(pos);
  3919. pos = os_strchr(pos, ',');
  3920. if (pos == NULL)
  3921. return -1;
  3922. pos++;
  3923. duration = atoi(pos);
  3924. if (count < 0 || count > 255 || start < 0 || duration < 0)
  3925. return -1;
  3926. if (count == 0 && duration > 0)
  3927. return -1;
  3928. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  3929. "start=%d duration=%d", count, start, duration);
  3930. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  3931. }
  3932. if (os_strcmp(cmd, "ps") == 0)
  3933. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  3934. if (os_strcmp(cmd, "oppps") == 0)
  3935. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  3936. if (os_strcmp(cmd, "ctwindow") == 0)
  3937. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  3938. if (os_strcmp(cmd, "disabled") == 0) {
  3939. wpa_s->global->p2p_disabled = atoi(param);
  3940. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  3941. wpa_s->global->p2p_disabled ?
  3942. "disabled" : "enabled");
  3943. if (wpa_s->global->p2p_disabled) {
  3944. wpas_p2p_stop_find(wpa_s);
  3945. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3946. p2p_flush(wpa_s->global->p2p);
  3947. }
  3948. return 0;
  3949. }
  3950. if (os_strcmp(cmd, "conc_pref") == 0) {
  3951. if (os_strcmp(param, "sta") == 0)
  3952. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  3953. else if (os_strcmp(param, "p2p") == 0)
  3954. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  3955. else {
  3956. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  3957. return -1;
  3958. }
  3959. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  3960. "%s", param);
  3961. return 0;
  3962. }
  3963. if (os_strcmp(cmd, "force_long_sd") == 0) {
  3964. wpa_s->force_long_sd = atoi(param);
  3965. return 0;
  3966. }
  3967. if (os_strcmp(cmd, "peer_filter") == 0) {
  3968. u8 addr[ETH_ALEN];
  3969. if (hwaddr_aton(param, addr))
  3970. return -1;
  3971. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  3972. return 0;
  3973. }
  3974. if (os_strcmp(cmd, "cross_connect") == 0)
  3975. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  3976. if (os_strcmp(cmd, "go_apsd") == 0) {
  3977. if (os_strcmp(param, "disable") == 0)
  3978. wpa_s->set_ap_uapsd = 0;
  3979. else {
  3980. wpa_s->set_ap_uapsd = 1;
  3981. wpa_s->ap_uapsd = atoi(param);
  3982. }
  3983. return 0;
  3984. }
  3985. if (os_strcmp(cmd, "client_apsd") == 0) {
  3986. if (os_strcmp(param, "disable") == 0)
  3987. wpa_s->set_sta_uapsd = 0;
  3988. else {
  3989. int be, bk, vi, vo;
  3990. char *pos;
  3991. /* format: BE,BK,VI,VO;max SP Length */
  3992. be = atoi(param);
  3993. pos = os_strchr(param, ',');
  3994. if (pos == NULL)
  3995. return -1;
  3996. pos++;
  3997. bk = atoi(pos);
  3998. pos = os_strchr(pos, ',');
  3999. if (pos == NULL)
  4000. return -1;
  4001. pos++;
  4002. vi = atoi(pos);
  4003. pos = os_strchr(pos, ',');
  4004. if (pos == NULL)
  4005. return -1;
  4006. pos++;
  4007. vo = atoi(pos);
  4008. /* ignore max SP Length for now */
  4009. wpa_s->set_sta_uapsd = 1;
  4010. wpa_s->sta_uapsd = 0;
  4011. if (be)
  4012. wpa_s->sta_uapsd |= BIT(0);
  4013. if (bk)
  4014. wpa_s->sta_uapsd |= BIT(1);
  4015. if (vi)
  4016. wpa_s->sta_uapsd |= BIT(2);
  4017. if (vo)
  4018. wpa_s->sta_uapsd |= BIT(3);
  4019. }
  4020. return 0;
  4021. }
  4022. if (os_strcmp(cmd, "disallow_freq") == 0)
  4023. return p2p_ctrl_disallow_freq(wpa_s, param);
  4024. if (os_strcmp(cmd, "disc_int") == 0) {
  4025. int min_disc_int, max_disc_int, max_disc_tu;
  4026. char *pos;
  4027. pos = param;
  4028. min_disc_int = atoi(pos);
  4029. pos = os_strchr(pos, ' ');
  4030. if (pos == NULL)
  4031. return -1;
  4032. *pos++ = '\0';
  4033. max_disc_int = atoi(pos);
  4034. pos = os_strchr(pos, ' ');
  4035. if (pos == NULL)
  4036. return -1;
  4037. *pos++ = '\0';
  4038. max_disc_tu = atoi(pos);
  4039. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  4040. max_disc_int, max_disc_tu);
  4041. }
  4042. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  4043. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  4044. return 0;
  4045. }
  4046. #ifdef CONFIG_WPS_NFC
  4047. if (os_strcmp(cmd, "nfc_tag") == 0)
  4048. return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
  4049. #endif /* CONFIG_WPS_NFC */
  4050. if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
  4051. wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
  4052. return 0;
  4053. }
  4054. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  4055. cmd);
  4056. return -1;
  4057. }
  4058. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  4059. {
  4060. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4061. wpa_s->force_long_sd = 0;
  4062. if (wpa_s->global->p2p)
  4063. p2p_flush(wpa_s->global->p2p);
  4064. }
  4065. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  4066. {
  4067. char *pos, *pos2;
  4068. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  4069. if (cmd[0]) {
  4070. pos = os_strchr(cmd, ' ');
  4071. if (pos == NULL)
  4072. return -1;
  4073. *pos++ = '\0';
  4074. dur1 = atoi(cmd);
  4075. pos2 = os_strchr(pos, ' ');
  4076. if (pos2)
  4077. *pos2++ = '\0';
  4078. int1 = atoi(pos);
  4079. } else
  4080. pos2 = NULL;
  4081. if (pos2) {
  4082. pos = os_strchr(pos2, ' ');
  4083. if (pos == NULL)
  4084. return -1;
  4085. *pos++ = '\0';
  4086. dur2 = atoi(pos2);
  4087. int2 = atoi(pos);
  4088. }
  4089. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  4090. }
  4091. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  4092. {
  4093. char *pos;
  4094. unsigned int period = 0, interval = 0;
  4095. if (cmd[0]) {
  4096. pos = os_strchr(cmd, ' ');
  4097. if (pos == NULL)
  4098. return -1;
  4099. *pos++ = '\0';
  4100. period = atoi(cmd);
  4101. interval = atoi(pos);
  4102. }
  4103. return wpas_p2p_ext_listen(wpa_s, period, interval);
  4104. }
  4105. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  4106. {
  4107. const char *pos;
  4108. u8 peer[ETH_ALEN];
  4109. int iface_addr = 0;
  4110. pos = cmd;
  4111. if (os_strncmp(pos, "iface=", 6) == 0) {
  4112. iface_addr = 1;
  4113. pos += 6;
  4114. }
  4115. if (hwaddr_aton(pos, peer))
  4116. return -1;
  4117. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  4118. return 0;
  4119. }
  4120. #endif /* CONFIG_P2P */
  4121. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
  4122. {
  4123. struct wpa_freq_range_list ranges;
  4124. int *freqs = NULL;
  4125. struct hostapd_hw_modes *mode;
  4126. u16 i;
  4127. if (wpa_s->hw.modes == NULL)
  4128. return NULL;
  4129. os_memset(&ranges, 0, sizeof(ranges));
  4130. if (freq_range_list_parse(&ranges, val) < 0)
  4131. return NULL;
  4132. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  4133. int j;
  4134. mode = &wpa_s->hw.modes[i];
  4135. for (j = 0; j < mode->num_channels; j++) {
  4136. unsigned int freq;
  4137. if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
  4138. continue;
  4139. freq = mode->channels[j].freq;
  4140. if (!freq_range_list_includes(&ranges, freq))
  4141. continue;
  4142. int_array_add_unique(&freqs, freq);
  4143. }
  4144. }
  4145. os_free(ranges.range);
  4146. return freqs;
  4147. }
  4148. #ifdef CONFIG_INTERWORKING
  4149. static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
  4150. {
  4151. int auto_sel = 0;
  4152. int *freqs = NULL;
  4153. if (param) {
  4154. char *pos;
  4155. auto_sel = os_strstr(param, "auto") != NULL;
  4156. pos = os_strstr(param, "freq=");
  4157. if (pos) {
  4158. freqs = freq_range_to_channel_list(wpa_s, pos + 5);
  4159. if (freqs == NULL)
  4160. return -1;
  4161. }
  4162. }
  4163. return interworking_select(wpa_s, auto_sel, freqs);
  4164. }
  4165. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  4166. {
  4167. u8 bssid[ETH_ALEN];
  4168. struct wpa_bss *bss;
  4169. if (hwaddr_aton(dst, bssid)) {
  4170. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  4171. return -1;
  4172. }
  4173. bss = wpa_bss_get_bssid(wpa_s, bssid);
  4174. if (bss == NULL) {
  4175. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  4176. MAC2STR(bssid));
  4177. return -1;
  4178. }
  4179. return interworking_connect(wpa_s, bss);
  4180. }
  4181. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4182. {
  4183. u8 dst_addr[ETH_ALEN];
  4184. int used;
  4185. char *pos;
  4186. #define MAX_ANQP_INFO_ID 100
  4187. u16 id[MAX_ANQP_INFO_ID];
  4188. size_t num_id = 0;
  4189. used = hwaddr_aton2(dst, dst_addr);
  4190. if (used < 0)
  4191. return -1;
  4192. pos = dst + used;
  4193. while (num_id < MAX_ANQP_INFO_ID) {
  4194. id[num_id] = atoi(pos);
  4195. if (id[num_id])
  4196. num_id++;
  4197. pos = os_strchr(pos + 1, ',');
  4198. if (pos == NULL)
  4199. break;
  4200. pos++;
  4201. }
  4202. if (num_id == 0)
  4203. return -1;
  4204. return anqp_send_req(wpa_s, dst_addr, id, num_id);
  4205. }
  4206. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  4207. {
  4208. u8 dst_addr[ETH_ALEN];
  4209. struct wpabuf *advproto, *query = NULL;
  4210. int used, ret = -1;
  4211. char *pos, *end;
  4212. size_t len;
  4213. used = hwaddr_aton2(cmd, dst_addr);
  4214. if (used < 0)
  4215. return -1;
  4216. pos = cmd + used;
  4217. while (*pos == ' ')
  4218. pos++;
  4219. /* Advertisement Protocol ID */
  4220. end = os_strchr(pos, ' ');
  4221. if (end)
  4222. len = end - pos;
  4223. else
  4224. len = os_strlen(pos);
  4225. if (len & 0x01)
  4226. return -1;
  4227. len /= 2;
  4228. if (len == 0)
  4229. return -1;
  4230. advproto = wpabuf_alloc(len);
  4231. if (advproto == NULL)
  4232. return -1;
  4233. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  4234. goto fail;
  4235. if (end) {
  4236. /* Optional Query Request */
  4237. pos = end + 1;
  4238. while (*pos == ' ')
  4239. pos++;
  4240. len = os_strlen(pos);
  4241. if (len) {
  4242. if (len & 0x01)
  4243. goto fail;
  4244. len /= 2;
  4245. if (len == 0)
  4246. goto fail;
  4247. query = wpabuf_alloc(len);
  4248. if (query == NULL)
  4249. goto fail;
  4250. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  4251. goto fail;
  4252. }
  4253. }
  4254. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  4255. fail:
  4256. wpabuf_free(advproto);
  4257. wpabuf_free(query);
  4258. return ret;
  4259. }
  4260. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  4261. size_t buflen)
  4262. {
  4263. u8 addr[ETH_ALEN];
  4264. int dialog_token;
  4265. int used;
  4266. char *pos;
  4267. size_t resp_len, start, requested_len;
  4268. struct wpabuf *resp;
  4269. int ret;
  4270. used = hwaddr_aton2(cmd, addr);
  4271. if (used < 0)
  4272. return -1;
  4273. pos = cmd + used;
  4274. while (*pos == ' ')
  4275. pos++;
  4276. dialog_token = atoi(pos);
  4277. if (wpa_s->last_gas_resp &&
  4278. os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
  4279. dialog_token == wpa_s->last_gas_dialog_token)
  4280. resp = wpa_s->last_gas_resp;
  4281. else if (wpa_s->prev_gas_resp &&
  4282. os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
  4283. dialog_token == wpa_s->prev_gas_dialog_token)
  4284. resp = wpa_s->prev_gas_resp;
  4285. else
  4286. return -1;
  4287. resp_len = wpabuf_len(resp);
  4288. start = 0;
  4289. requested_len = resp_len;
  4290. pos = os_strchr(pos, ' ');
  4291. if (pos) {
  4292. start = atoi(pos);
  4293. if (start > resp_len)
  4294. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4295. pos = os_strchr(pos, ',');
  4296. if (pos == NULL)
  4297. return -1;
  4298. pos++;
  4299. requested_len = atoi(pos);
  4300. if (start + requested_len > resp_len)
  4301. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4302. }
  4303. if (requested_len * 2 + 1 > buflen)
  4304. return os_snprintf(buf, buflen, "FAIL-Too long response");
  4305. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
  4306. requested_len);
  4307. if (start + requested_len == resp_len) {
  4308. /*
  4309. * Free memory by dropping the response after it has been
  4310. * fetched.
  4311. */
  4312. if (resp == wpa_s->prev_gas_resp) {
  4313. wpabuf_free(wpa_s->prev_gas_resp);
  4314. wpa_s->prev_gas_resp = NULL;
  4315. } else {
  4316. wpabuf_free(wpa_s->last_gas_resp);
  4317. wpa_s->last_gas_resp = NULL;
  4318. }
  4319. }
  4320. return ret;
  4321. }
  4322. #endif /* CONFIG_INTERWORKING */
  4323. #ifdef CONFIG_HS20
  4324. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4325. {
  4326. u8 dst_addr[ETH_ALEN];
  4327. int used;
  4328. char *pos;
  4329. u32 subtypes = 0;
  4330. used = hwaddr_aton2(dst, dst_addr);
  4331. if (used < 0)
  4332. return -1;
  4333. pos = dst + used;
  4334. for (;;) {
  4335. int num = atoi(pos);
  4336. if (num <= 0 || num > 31)
  4337. return -1;
  4338. subtypes |= BIT(num);
  4339. pos = os_strchr(pos + 1, ',');
  4340. if (pos == NULL)
  4341. break;
  4342. pos++;
  4343. }
  4344. if (subtypes == 0)
  4345. return -1;
  4346. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
  4347. }
  4348. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4349. const u8 *addr, const char *realm)
  4350. {
  4351. u8 *buf;
  4352. size_t rlen, len;
  4353. int ret;
  4354. rlen = os_strlen(realm);
  4355. len = 3 + rlen;
  4356. buf = os_malloc(len);
  4357. if (buf == NULL)
  4358. return -1;
  4359. buf[0] = 1; /* NAI Home Realm Count */
  4360. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  4361. buf[2] = rlen;
  4362. os_memcpy(buf + 3, realm, rlen);
  4363. ret = hs20_anqp_send_req(wpa_s, addr,
  4364. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4365. buf, len);
  4366. os_free(buf);
  4367. return ret;
  4368. }
  4369. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4370. char *dst)
  4371. {
  4372. struct wpa_cred *cred = wpa_s->conf->cred;
  4373. u8 dst_addr[ETH_ALEN];
  4374. int used;
  4375. u8 *buf;
  4376. size_t len;
  4377. int ret;
  4378. used = hwaddr_aton2(dst, dst_addr);
  4379. if (used < 0)
  4380. return -1;
  4381. while (dst[used] == ' ')
  4382. used++;
  4383. if (os_strncmp(dst + used, "realm=", 6) == 0)
  4384. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  4385. dst + used + 6);
  4386. len = os_strlen(dst + used);
  4387. if (len == 0 && cred && cred->realm)
  4388. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  4389. if (len % 1)
  4390. return -1;
  4391. len /= 2;
  4392. buf = os_malloc(len);
  4393. if (buf == NULL)
  4394. return -1;
  4395. if (hexstr2bin(dst + used, buf, len) < 0) {
  4396. os_free(buf);
  4397. return -1;
  4398. }
  4399. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  4400. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4401. buf, len);
  4402. os_free(buf);
  4403. return ret;
  4404. }
  4405. static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
  4406. {
  4407. u8 dst_addr[ETH_ALEN];
  4408. int used;
  4409. char *icon;
  4410. used = hwaddr_aton2(cmd, dst_addr);
  4411. if (used < 0)
  4412. return -1;
  4413. while (cmd[used] == ' ')
  4414. used++;
  4415. icon = &cmd[used];
  4416. wpa_s->fetch_osu_icon_in_progress = 0;
  4417. return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
  4418. (u8 *) icon, os_strlen(icon));
  4419. }
  4420. #endif /* CONFIG_HS20 */
  4421. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  4422. struct wpa_supplicant *wpa_s, char *cmd)
  4423. {
  4424. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  4425. return 0;
  4426. }
  4427. #ifdef CONFIG_AUTOSCAN
  4428. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  4429. char *cmd)
  4430. {
  4431. enum wpa_states state = wpa_s->wpa_state;
  4432. char *new_params = NULL;
  4433. if (os_strlen(cmd) > 0) {
  4434. new_params = os_strdup(cmd);
  4435. if (new_params == NULL)
  4436. return -1;
  4437. }
  4438. os_free(wpa_s->conf->autoscan);
  4439. wpa_s->conf->autoscan = new_params;
  4440. if (wpa_s->conf->autoscan == NULL)
  4441. autoscan_deinit(wpa_s);
  4442. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  4443. autoscan_init(wpa_s, 1);
  4444. else if (state == WPA_SCANNING)
  4445. wpa_supplicant_reinit_autoscan(wpa_s);
  4446. return 0;
  4447. }
  4448. #endif /* CONFIG_AUTOSCAN */
  4449. #ifdef CONFIG_WNM
  4450. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  4451. {
  4452. int enter;
  4453. int intval = 0;
  4454. char *pos;
  4455. int ret;
  4456. struct wpabuf *tfs_req = NULL;
  4457. if (os_strncmp(cmd, "enter", 5) == 0)
  4458. enter = 1;
  4459. else if (os_strncmp(cmd, "exit", 4) == 0)
  4460. enter = 0;
  4461. else
  4462. return -1;
  4463. pos = os_strstr(cmd, " interval=");
  4464. if (pos)
  4465. intval = atoi(pos + 10);
  4466. pos = os_strstr(cmd, " tfs_req=");
  4467. if (pos) {
  4468. char *end;
  4469. size_t len;
  4470. pos += 9;
  4471. end = os_strchr(pos, ' ');
  4472. if (end)
  4473. len = end - pos;
  4474. else
  4475. len = os_strlen(pos);
  4476. if (len & 1)
  4477. return -1;
  4478. len /= 2;
  4479. tfs_req = wpabuf_alloc(len);
  4480. if (tfs_req == NULL)
  4481. return -1;
  4482. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  4483. wpabuf_free(tfs_req);
  4484. return -1;
  4485. }
  4486. }
  4487. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  4488. WNM_SLEEP_MODE_EXIT, intval,
  4489. tfs_req);
  4490. wpabuf_free(tfs_req);
  4491. return ret;
  4492. }
  4493. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  4494. {
  4495. int query_reason;
  4496. query_reason = atoi(cmd);
  4497. wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
  4498. query_reason);
  4499. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
  4500. }
  4501. #endif /* CONFIG_WNM */
  4502. /* Get string representation of channel width */
  4503. static const char * channel_width_name(enum chan_width width)
  4504. {
  4505. switch (width) {
  4506. case CHAN_WIDTH_20_NOHT:
  4507. return "20 MHz (no HT)";
  4508. case CHAN_WIDTH_20:
  4509. return "20 MHz";
  4510. case CHAN_WIDTH_40:
  4511. return "40 MHz";
  4512. case CHAN_WIDTH_80:
  4513. return "80 MHz";
  4514. case CHAN_WIDTH_80P80:
  4515. return "80+80 MHz";
  4516. case CHAN_WIDTH_160:
  4517. return "160 MHz";
  4518. default:
  4519. return "unknown";
  4520. }
  4521. }
  4522. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  4523. size_t buflen)
  4524. {
  4525. struct wpa_signal_info si;
  4526. int ret;
  4527. char *pos, *end;
  4528. ret = wpa_drv_signal_poll(wpa_s, &si);
  4529. if (ret)
  4530. return -1;
  4531. pos = buf;
  4532. end = buf + buflen;
  4533. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  4534. "NOISE=%d\nFREQUENCY=%u\n",
  4535. si.current_signal, si.current_txrate / 1000,
  4536. si.current_noise, si.frequency);
  4537. if (ret < 0 || ret > end - pos)
  4538. return -1;
  4539. pos += ret;
  4540. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  4541. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  4542. channel_width_name(si.chanwidth));
  4543. if (ret < 0 || ret > end - pos)
  4544. return -1;
  4545. pos += ret;
  4546. }
  4547. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  4548. ret = os_snprintf(pos, end - pos,
  4549. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  4550. si.center_frq1, si.center_frq2);
  4551. if (ret < 0 || ret > end - pos)
  4552. return -1;
  4553. pos += ret;
  4554. }
  4555. if (si.avg_signal) {
  4556. ret = os_snprintf(pos, end - pos,
  4557. "AVG_RSSI=%d\n", si.avg_signal);
  4558. if (ret < 0 || ret >= end - pos)
  4559. return -1;
  4560. pos += ret;
  4561. }
  4562. return pos - buf;
  4563. }
  4564. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  4565. size_t buflen)
  4566. {
  4567. struct hostap_sta_driver_data sta;
  4568. int ret;
  4569. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  4570. if (ret)
  4571. return -1;
  4572. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  4573. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  4574. if (ret < 0 || (size_t) ret > buflen)
  4575. return -1;
  4576. return ret;
  4577. }
  4578. #ifdef ANDROID
  4579. static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  4580. char *buf, size_t buflen)
  4581. {
  4582. int ret;
  4583. ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
  4584. if (ret == 0) {
  4585. if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
  4586. struct p2p_data *p2p = wpa_s->global->p2p;
  4587. if (p2p) {
  4588. char country[3];
  4589. country[0] = cmd[8];
  4590. country[1] = cmd[9];
  4591. country[2] = 0x04;
  4592. p2p_set_country(p2p, country);
  4593. }
  4594. }
  4595. ret = os_snprintf(buf, buflen, "%s\n", "OK");
  4596. }
  4597. return ret;
  4598. }
  4599. #endif /* ANDROID */
  4600. static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  4601. char *buf, size_t buflen)
  4602. {
  4603. int ret;
  4604. char *pos;
  4605. u8 *data = NULL;
  4606. unsigned int vendor_id, subcmd;
  4607. struct wpabuf *reply;
  4608. size_t data_len = 0;
  4609. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  4610. vendor_id = strtoul(cmd, &pos, 16);
  4611. if (!isblank(*pos))
  4612. return -EINVAL;
  4613. subcmd = strtoul(pos, &pos, 10);
  4614. if (*pos != '\0') {
  4615. if (!isblank(*pos++))
  4616. return -EINVAL;
  4617. data_len = os_strlen(pos);
  4618. }
  4619. if (data_len) {
  4620. data_len /= 2;
  4621. data = os_malloc(data_len);
  4622. if (!data)
  4623. return -ENOBUFS;
  4624. if (hexstr2bin(pos, data, data_len)) {
  4625. wpa_printf(MSG_DEBUG,
  4626. "Vendor command: wrong parameter format");
  4627. os_free(data);
  4628. return -EINVAL;
  4629. }
  4630. }
  4631. reply = wpabuf_alloc((buflen - 1) / 2);
  4632. if (!reply) {
  4633. os_free(data);
  4634. return -ENOBUFS;
  4635. }
  4636. ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
  4637. reply);
  4638. if (ret == 0)
  4639. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  4640. wpabuf_len(reply));
  4641. wpabuf_free(reply);
  4642. os_free(data);
  4643. return ret;
  4644. }
  4645. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  4646. {
  4647. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  4648. #ifdef CONFIG_P2P
  4649. wpas_p2p_stop_find(wpa_s);
  4650. p2p_ctrl_flush(wpa_s);
  4651. wpas_p2p_group_remove(wpa_s, "*");
  4652. wpas_p2p_service_flush(wpa_s);
  4653. wpa_s->global->p2p_disabled = 0;
  4654. wpa_s->global->p2p_per_sta_psk = 0;
  4655. wpa_s->conf->num_sec_device_types = 0;
  4656. wpa_s->p2p_disable_ip_addr_req = 0;
  4657. #endif /* CONFIG_P2P */
  4658. #ifdef CONFIG_WPS_TESTING
  4659. wps_version_number = 0x20;
  4660. wps_testing_dummy_cred = 0;
  4661. wps_corrupt_pkhash = 0;
  4662. #endif /* CONFIG_WPS_TESTING */
  4663. #ifdef CONFIG_WPS
  4664. wpa_s->wps_fragment_size = 0;
  4665. wpas_wps_cancel(wpa_s);
  4666. #endif /* CONFIG_WPS */
  4667. wpa_s->after_wps = 0;
  4668. wpa_s->known_wps_freq = 0;
  4669. #ifdef CONFIG_TDLS
  4670. #ifdef CONFIG_TDLS_TESTING
  4671. extern unsigned int tdls_testing;
  4672. tdls_testing = 0;
  4673. #endif /* CONFIG_TDLS_TESTING */
  4674. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  4675. wpa_tdls_enable(wpa_s->wpa, 1);
  4676. #endif /* CONFIG_TDLS */
  4677. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  4678. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  4679. wpa_s->no_keep_alive = 0;
  4680. os_free(wpa_s->disallow_aps_bssid);
  4681. wpa_s->disallow_aps_bssid = NULL;
  4682. wpa_s->disallow_aps_bssid_count = 0;
  4683. os_free(wpa_s->disallow_aps_ssid);
  4684. wpa_s->disallow_aps_ssid = NULL;
  4685. wpa_s->disallow_aps_ssid_count = 0;
  4686. wpa_s->set_sta_uapsd = 0;
  4687. wpa_s->sta_uapsd = 0;
  4688. wpa_drv_radio_disable(wpa_s, 0);
  4689. wpa_bss_flush(wpa_s);
  4690. wpa_blacklist_clear(wpa_s);
  4691. wpa_s->extra_blacklist_count = 0;
  4692. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  4693. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  4694. wpa_config_flush_blobs(wpa_s->conf);
  4695. wpa_s->conf->auto_interworking = 0;
  4696. wpa_s->conf->okc = 0;
  4697. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
  4698. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
  4699. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
  4700. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  4701. radio_remove_works(wpa_s, NULL, 1);
  4702. wpa_s->next_ssid = NULL;
  4703. #ifdef CONFIG_INTERWORKING
  4704. hs20_cancel_fetch_osu(wpa_s);
  4705. #endif /* CONFIG_INTERWORKING */
  4706. }
  4707. static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
  4708. char *buf, size_t buflen)
  4709. {
  4710. struct wpa_radio_work *work;
  4711. char *pos, *end;
  4712. struct os_reltime now, diff;
  4713. pos = buf;
  4714. end = buf + buflen;
  4715. os_get_reltime(&now);
  4716. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  4717. {
  4718. int ret;
  4719. os_reltime_sub(&now, &work->time, &diff);
  4720. ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
  4721. work->type, work->wpa_s->ifname, work->freq,
  4722. work->started, diff.sec, diff.usec);
  4723. if (ret < 0 || ret >= end - pos)
  4724. break;
  4725. pos += ret;
  4726. }
  4727. return pos - buf;
  4728. }
  4729. static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
  4730. {
  4731. struct wpa_radio_work *work = eloop_ctx;
  4732. struct wpa_external_work *ework = work->ctx;
  4733. wpa_dbg(work->wpa_s, MSG_DEBUG,
  4734. "Timing out external radio work %u (%s)",
  4735. ework->id, work->type);
  4736. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
  4737. os_free(ework);
  4738. radio_work_done(work);
  4739. }
  4740. static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
  4741. {
  4742. struct wpa_external_work *ework = work->ctx;
  4743. if (deinit) {
  4744. if (work->started)
  4745. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  4746. work, NULL);
  4747. os_free(ework);
  4748. return;
  4749. }
  4750. wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
  4751. ework->id, ework->type);
  4752. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
  4753. if (!ework->timeout)
  4754. ework->timeout = 10;
  4755. eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
  4756. work, NULL);
  4757. }
  4758. static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
  4759. char *buf, size_t buflen)
  4760. {
  4761. struct wpa_external_work *ework;
  4762. char *pos, *pos2;
  4763. size_t type_len;
  4764. int ret;
  4765. unsigned int freq = 0;
  4766. /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
  4767. ework = os_zalloc(sizeof(*ework));
  4768. if (ework == NULL)
  4769. return -1;
  4770. pos = os_strchr(cmd, ' ');
  4771. if (pos) {
  4772. type_len = pos - cmd;
  4773. pos++;
  4774. pos2 = os_strstr(pos, "freq=");
  4775. if (pos2)
  4776. freq = atoi(pos2 + 5);
  4777. pos2 = os_strstr(pos, "timeout=");
  4778. if (pos2)
  4779. ework->timeout = atoi(pos2 + 8);
  4780. } else {
  4781. type_len = os_strlen(cmd);
  4782. }
  4783. if (4 + type_len >= sizeof(ework->type))
  4784. type_len = sizeof(ework->type) - 4 - 1;
  4785. os_strlcpy(ework->type, "ext:", sizeof(ework->type));
  4786. os_memcpy(ework->type + 4, cmd, type_len);
  4787. ework->type[4 + type_len] = '\0';
  4788. wpa_s->ext_work_id++;
  4789. if (wpa_s->ext_work_id == 0)
  4790. wpa_s->ext_work_id++;
  4791. ework->id = wpa_s->ext_work_id;
  4792. if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
  4793. ework) < 0) {
  4794. os_free(ework);
  4795. return -1;
  4796. }
  4797. ret = os_snprintf(buf, buflen, "%u", ework->id);
  4798. if (ret < 0 || (size_t) ret >= buflen)
  4799. return -1;
  4800. return ret;
  4801. }
  4802. static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
  4803. {
  4804. struct wpa_radio_work *work;
  4805. unsigned int id = atoi(cmd);
  4806. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  4807. {
  4808. struct wpa_external_work *ework;
  4809. if (os_strncmp(work->type, "ext:", 4) != 0)
  4810. continue;
  4811. ework = work->ctx;
  4812. if (id && ework->id != id)
  4813. continue;
  4814. wpa_dbg(wpa_s, MSG_DEBUG,
  4815. "Completed external radio work %u (%s)",
  4816. ework->id, ework->type);
  4817. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
  4818. os_free(ework);
  4819. radio_work_done(work);
  4820. return 3; /* "OK\n" */
  4821. }
  4822. return -1;
  4823. }
  4824. static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
  4825. char *buf, size_t buflen)
  4826. {
  4827. if (os_strcmp(cmd, "show") == 0)
  4828. return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
  4829. if (os_strncmp(cmd, "add ", 4) == 0)
  4830. return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
  4831. if (os_strncmp(cmd, "done ", 5) == 0)
  4832. return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
  4833. return -1;
  4834. }
  4835. void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
  4836. {
  4837. struct wpa_radio_work *work, *tmp;
  4838. if (!wpa_s || !wpa_s->radio)
  4839. return;
  4840. dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
  4841. struct wpa_radio_work, list) {
  4842. struct wpa_external_work *ework;
  4843. if (os_strncmp(work->type, "ext:", 4) != 0)
  4844. continue;
  4845. ework = work->ctx;
  4846. wpa_dbg(wpa_s, MSG_DEBUG,
  4847. "Flushing %sexternal radio work %u (%s)",
  4848. work->started ? " started" : "", ework->id,
  4849. ework->type);
  4850. if (work->started)
  4851. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  4852. work, NULL);
  4853. os_free(ework);
  4854. radio_work_done(work);
  4855. }
  4856. }
  4857. static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
  4858. {
  4859. struct wpa_supplicant *wpa_s = eloop_ctx;
  4860. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  4861. }
  4862. static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
  4863. {
  4864. int *freqs = NULL;
  4865. freqs = freq_range_to_channel_list(wpa_s, val);
  4866. if (freqs == NULL)
  4867. return -1;
  4868. os_free(wpa_s->manual_scan_freqs);
  4869. wpa_s->manual_scan_freqs = freqs;
  4870. return 0;
  4871. }
  4872. static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
  4873. char *reply, int reply_size, int *reply_len)
  4874. {
  4875. char *pos;
  4876. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  4877. *reply_len = -1;
  4878. return;
  4879. }
  4880. wpa_s->manual_scan_passive = 0;
  4881. wpa_s->manual_scan_use_id = 0;
  4882. wpa_s->manual_scan_only_new = 0;
  4883. if (params) {
  4884. if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
  4885. wpa_s->scan_res_handler = scan_only_handler;
  4886. pos = os_strstr(params, "freq=");
  4887. if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
  4888. *reply_len = -1;
  4889. return;
  4890. }
  4891. pos = os_strstr(params, "passive=");
  4892. if (pos)
  4893. wpa_s->manual_scan_passive = !!atoi(pos + 8);
  4894. pos = os_strstr(params, "use_id=");
  4895. if (pos)
  4896. wpa_s->manual_scan_use_id = atoi(pos + 7);
  4897. pos = os_strstr(params, "only_new=1");
  4898. if (pos)
  4899. wpa_s->manual_scan_only_new = 1;
  4900. } else {
  4901. os_free(wpa_s->manual_scan_freqs);
  4902. wpa_s->manual_scan_freqs = NULL;
  4903. if (wpa_s->scan_res_handler == scan_only_handler)
  4904. wpa_s->scan_res_handler = NULL;
  4905. }
  4906. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  4907. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  4908. (wpa_s->wpa_state == WPA_COMPLETED))) {
  4909. wpa_s->normal_scans = 0;
  4910. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4911. wpa_s->after_wps = 0;
  4912. wpa_s->known_wps_freq = 0;
  4913. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4914. if (wpa_s->manual_scan_use_id) {
  4915. wpa_s->manual_scan_id++;
  4916. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  4917. wpa_s->manual_scan_id);
  4918. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  4919. wpa_s->manual_scan_id);
  4920. }
  4921. } else if (wpa_s->sched_scanning) {
  4922. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
  4923. wpa_supplicant_cancel_sched_scan(wpa_s);
  4924. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4925. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4926. if (wpa_s->manual_scan_use_id) {
  4927. wpa_s->manual_scan_id++;
  4928. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  4929. wpa_s->manual_scan_id);
  4930. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  4931. wpa_s->manual_scan_id);
  4932. }
  4933. } else {
  4934. wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
  4935. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  4936. }
  4937. }
  4938. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  4939. char *buf, size_t *resp_len)
  4940. {
  4941. char *reply;
  4942. const int reply_size = 4096;
  4943. int reply_len;
  4944. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  4945. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  4946. if (wpa_debug_show_keys)
  4947. wpa_dbg(wpa_s, MSG_DEBUG,
  4948. "Control interface command '%s'", buf);
  4949. else
  4950. wpa_dbg(wpa_s, MSG_DEBUG,
  4951. "Control interface command '%s [REMOVED]'",
  4952. os_strncmp(buf, WPA_CTRL_RSP,
  4953. os_strlen(WPA_CTRL_RSP)) == 0 ?
  4954. WPA_CTRL_RSP : "SET_NETWORK");
  4955. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  4956. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
  4957. os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
  4958. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  4959. (const u8 *) buf, os_strlen(buf));
  4960. } else {
  4961. int level = MSG_DEBUG;
  4962. if (os_strcmp(buf, "PING") == 0)
  4963. level = MSG_EXCESSIVE;
  4964. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  4965. }
  4966. reply = os_malloc(reply_size);
  4967. if (reply == NULL) {
  4968. *resp_len = 1;
  4969. return NULL;
  4970. }
  4971. os_memcpy(reply, "OK\n", 3);
  4972. reply_len = 3;
  4973. if (os_strcmp(buf, "PING") == 0) {
  4974. os_memcpy(reply, "PONG\n", 5);
  4975. reply_len = 5;
  4976. } else if (os_strcmp(buf, "IFNAME") == 0) {
  4977. reply_len = os_strlen(wpa_s->ifname);
  4978. os_memcpy(reply, wpa_s->ifname, reply_len);
  4979. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  4980. if (wpa_debug_reopen_file() < 0)
  4981. reply_len = -1;
  4982. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  4983. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  4984. } else if (os_strcmp(buf, "MIB") == 0) {
  4985. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  4986. if (reply_len >= 0) {
  4987. int res;
  4988. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  4989. reply_size - reply_len);
  4990. if (res < 0)
  4991. reply_len = -1;
  4992. else
  4993. reply_len += res;
  4994. }
  4995. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  4996. reply_len = wpa_supplicant_ctrl_iface_status(
  4997. wpa_s, buf + 6, reply, reply_size);
  4998. } else if (os_strcmp(buf, "PMKSA") == 0) {
  4999. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  5000. reply_size);
  5001. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  5002. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  5003. reply_len = -1;
  5004. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  5005. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  5006. reply, reply_size);
  5007. } else if (os_strcmp(buf, "LOGON") == 0) {
  5008. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  5009. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  5010. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  5011. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  5012. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  5013. reply_len = -1;
  5014. else
  5015. wpas_request_connection(wpa_s);
  5016. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  5017. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  5018. reply_len = -1;
  5019. else if (wpa_s->disconnected)
  5020. wpas_request_connection(wpa_s);
  5021. #ifdef IEEE8021X_EAPOL
  5022. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  5023. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  5024. reply_len = -1;
  5025. #endif /* IEEE8021X_EAPOL */
  5026. #ifdef CONFIG_PEERKEY
  5027. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  5028. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  5029. reply_len = -1;
  5030. #endif /* CONFIG_PEERKEY */
  5031. #ifdef CONFIG_IEEE80211R
  5032. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  5033. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  5034. reply_len = -1;
  5035. #endif /* CONFIG_IEEE80211R */
  5036. #ifdef CONFIG_WPS
  5037. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  5038. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  5039. if (res == -2) {
  5040. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5041. reply_len = 17;
  5042. } else if (res)
  5043. reply_len = -1;
  5044. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  5045. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  5046. if (res == -2) {
  5047. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5048. reply_len = 17;
  5049. } else if (res)
  5050. reply_len = -1;
  5051. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  5052. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  5053. reply,
  5054. reply_size);
  5055. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  5056. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  5057. wpa_s, buf + 14, reply, reply_size);
  5058. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  5059. if (wpas_wps_cancel(wpa_s))
  5060. reply_len = -1;
  5061. #ifdef CONFIG_WPS_NFC
  5062. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  5063. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  5064. reply_len = -1;
  5065. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  5066. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  5067. reply_len = -1;
  5068. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  5069. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  5070. wpa_s, buf + 21, reply, reply_size);
  5071. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  5072. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  5073. wpa_s, buf + 14, reply, reply_size);
  5074. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  5075. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  5076. buf + 17))
  5077. reply_len = -1;
  5078. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  5079. reply_len = wpas_ctrl_nfc_get_handover_req(
  5080. wpa_s, buf + 21, reply, reply_size);
  5081. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  5082. reply_len = wpas_ctrl_nfc_get_handover_sel(
  5083. wpa_s, buf + 21, reply, reply_size);
  5084. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
  5085. if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
  5086. reply_len = -1;
  5087. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  5088. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  5089. reply_len = -1;
  5090. #endif /* CONFIG_WPS_NFC */
  5091. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  5092. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  5093. reply_len = -1;
  5094. #ifdef CONFIG_AP
  5095. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  5096. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  5097. wpa_s, buf + 11, reply, reply_size);
  5098. #endif /* CONFIG_AP */
  5099. #ifdef CONFIG_WPS_ER
  5100. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  5101. if (wpas_wps_er_start(wpa_s, NULL))
  5102. reply_len = -1;
  5103. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  5104. if (wpas_wps_er_start(wpa_s, buf + 13))
  5105. reply_len = -1;
  5106. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  5107. if (wpas_wps_er_stop(wpa_s))
  5108. reply_len = -1;
  5109. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  5110. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  5111. reply_len = -1;
  5112. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  5113. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  5114. if (ret == -2) {
  5115. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5116. reply_len = 17;
  5117. } else if (ret == -3) {
  5118. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  5119. reply_len = 18;
  5120. } else if (ret == -4) {
  5121. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  5122. reply_len = 20;
  5123. } else if (ret)
  5124. reply_len = -1;
  5125. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  5126. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  5127. reply_len = -1;
  5128. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  5129. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  5130. buf + 18))
  5131. reply_len = -1;
  5132. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  5133. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  5134. reply_len = -1;
  5135. #ifdef CONFIG_WPS_NFC
  5136. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  5137. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  5138. wpa_s, buf + 24, reply, reply_size);
  5139. #endif /* CONFIG_WPS_NFC */
  5140. #endif /* CONFIG_WPS_ER */
  5141. #endif /* CONFIG_WPS */
  5142. #ifdef CONFIG_IBSS_RSN
  5143. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  5144. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  5145. reply_len = -1;
  5146. #endif /* CONFIG_IBSS_RSN */
  5147. #ifdef CONFIG_P2P
  5148. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  5149. if (p2p_ctrl_find(wpa_s, buf + 9))
  5150. reply_len = -1;
  5151. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  5152. if (p2p_ctrl_find(wpa_s, ""))
  5153. reply_len = -1;
  5154. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  5155. wpas_p2p_stop_find(wpa_s);
  5156. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  5157. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  5158. reply_size);
  5159. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  5160. if (p2p_ctrl_listen(wpa_s, buf + 11))
  5161. reply_len = -1;
  5162. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  5163. if (p2p_ctrl_listen(wpa_s, ""))
  5164. reply_len = -1;
  5165. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  5166. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  5167. reply_len = -1;
  5168. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  5169. if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
  5170. reply_len = -1;
  5171. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  5172. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  5173. reply_len = -1;
  5174. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  5175. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  5176. reply_len = -1;
  5177. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  5178. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  5179. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  5180. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  5181. reply_size);
  5182. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  5183. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  5184. reply_len = -1;
  5185. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  5186. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  5187. reply_len = -1;
  5188. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  5189. wpas_p2p_sd_service_update(wpa_s);
  5190. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  5191. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  5192. reply_len = -1;
  5193. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  5194. wpas_p2p_service_flush(wpa_s);
  5195. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  5196. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  5197. reply_len = -1;
  5198. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  5199. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  5200. reply_len = -1;
  5201. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  5202. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  5203. reply_len = -1;
  5204. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  5205. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  5206. reply_len = -1;
  5207. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  5208. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  5209. reply_size);
  5210. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  5211. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  5212. reply_len = -1;
  5213. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  5214. p2p_ctrl_flush(wpa_s);
  5215. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  5216. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  5217. reply_len = -1;
  5218. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  5219. if (wpas_p2p_cancel(wpa_s))
  5220. reply_len = -1;
  5221. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  5222. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  5223. reply_len = -1;
  5224. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  5225. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  5226. reply_len = -1;
  5227. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  5228. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  5229. reply_len = -1;
  5230. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  5231. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  5232. reply_len = -1;
  5233. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  5234. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  5235. reply_len = -1;
  5236. #endif /* CONFIG_P2P */
  5237. #ifdef CONFIG_WIFI_DISPLAY
  5238. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  5239. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  5240. reply_len = -1;
  5241. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  5242. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  5243. reply, reply_size);
  5244. #endif /* CONFIG_WIFI_DISPLAY */
  5245. #ifdef CONFIG_INTERWORKING
  5246. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  5247. if (interworking_fetch_anqp(wpa_s) < 0)
  5248. reply_len = -1;
  5249. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  5250. interworking_stop_fetch_anqp(wpa_s);
  5251. } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
  5252. if (ctrl_interworking_select(wpa_s, NULL) < 0)
  5253. reply_len = -1;
  5254. } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
  5255. if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
  5256. reply_len = -1;
  5257. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  5258. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  5259. reply_len = -1;
  5260. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  5261. if (get_anqp(wpa_s, buf + 9) < 0)
  5262. reply_len = -1;
  5263. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  5264. if (gas_request(wpa_s, buf + 12) < 0)
  5265. reply_len = -1;
  5266. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  5267. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  5268. reply_size);
  5269. #endif /* CONFIG_INTERWORKING */
  5270. #ifdef CONFIG_HS20
  5271. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  5272. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  5273. reply_len = -1;
  5274. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  5275. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  5276. reply_len = -1;
  5277. } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
  5278. if (hs20_icon_request(wpa_s, buf + 18) < 0)
  5279. reply_len = -1;
  5280. } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
  5281. if (hs20_fetch_osu(wpa_s) < 0)
  5282. reply_len = -1;
  5283. } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
  5284. hs20_cancel_fetch_osu(wpa_s);
  5285. #endif /* CONFIG_HS20 */
  5286. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  5287. {
  5288. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  5289. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  5290. reply_len = -1;
  5291. else {
  5292. /*
  5293. * Notify response from timeout to allow the control
  5294. * interface response to be sent first.
  5295. */
  5296. eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
  5297. wpa_s, NULL);
  5298. }
  5299. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  5300. if (wpa_supplicant_reload_configuration(wpa_s))
  5301. reply_len = -1;
  5302. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  5303. wpa_supplicant_terminate_proc(wpa_s->global);
  5304. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  5305. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  5306. reply_len = -1;
  5307. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  5308. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  5309. wpa_s, buf + 9, reply, reply_size);
  5310. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  5311. reply_len = wpa_supplicant_ctrl_iface_log_level(
  5312. wpa_s, buf + 9, reply, reply_size);
  5313. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  5314. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  5315. wpa_s, reply, reply_size);
  5316. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  5317. #ifdef CONFIG_SME
  5318. wpa_s->sme.prev_bssid_set = 0;
  5319. #endif /* CONFIG_SME */
  5320. wpa_s->reassociate = 0;
  5321. wpa_s->disconnected = 1;
  5322. wpa_supplicant_cancel_sched_scan(wpa_s);
  5323. wpa_supplicant_cancel_scan(wpa_s);
  5324. wpa_supplicant_deauthenticate(wpa_s,
  5325. WLAN_REASON_DEAUTH_LEAVING);
  5326. } else if (os_strcmp(buf, "SCAN") == 0) {
  5327. wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
  5328. } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
  5329. wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
  5330. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  5331. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  5332. wpa_s, reply, reply_size);
  5333. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  5334. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  5335. reply_len = -1;
  5336. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  5337. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  5338. reply_len = -1;
  5339. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  5340. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  5341. reply_len = -1;
  5342. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  5343. reply_len = wpa_supplicant_ctrl_iface_add_network(
  5344. wpa_s, reply, reply_size);
  5345. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  5346. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  5347. reply_len = -1;
  5348. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  5349. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  5350. reply_len = -1;
  5351. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  5352. reply_len = wpa_supplicant_ctrl_iface_get_network(
  5353. wpa_s, buf + 12, reply, reply_size);
  5354. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  5355. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  5356. wpa_s, reply, reply_size);
  5357. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  5358. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  5359. wpa_s, reply, reply_size);
  5360. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  5361. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  5362. reply_len = -1;
  5363. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  5364. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  5365. reply_len = -1;
  5366. #ifndef CONFIG_NO_CONFIG_WRITE
  5367. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  5368. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  5369. reply_len = -1;
  5370. #endif /* CONFIG_NO_CONFIG_WRITE */
  5371. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  5372. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  5373. wpa_s, buf + 15, reply, reply_size);
  5374. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  5375. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  5376. reply_len = -1;
  5377. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  5378. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  5379. reply_len = -1;
  5380. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  5381. reply_len = wpa_supplicant_global_iface_list(
  5382. wpa_s->global, reply, reply_size);
  5383. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  5384. reply_len = wpa_supplicant_global_iface_interfaces(
  5385. wpa_s->global, reply, reply_size);
  5386. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  5387. reply_len = wpa_supplicant_ctrl_iface_bss(
  5388. wpa_s, buf + 4, reply, reply_size);
  5389. #ifdef CONFIG_AP
  5390. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  5391. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  5392. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  5393. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  5394. reply_size);
  5395. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  5396. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  5397. reply_size);
  5398. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  5399. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  5400. reply_len = -1;
  5401. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  5402. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  5403. reply_len = -1;
  5404. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  5405. if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
  5406. reply_len = -1;
  5407. #endif /* CONFIG_AP */
  5408. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  5409. wpas_notify_suspend(wpa_s->global);
  5410. } else if (os_strcmp(buf, "RESUME") == 0) {
  5411. wpas_notify_resume(wpa_s->global);
  5412. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  5413. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  5414. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  5415. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  5416. reply_len = -1;
  5417. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  5418. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  5419. reply_len = -1;
  5420. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  5421. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  5422. reply_len = -1;
  5423. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  5424. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  5425. buf + 17))
  5426. reply_len = -1;
  5427. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  5428. if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
  5429. reply_len = -1;
  5430. #ifdef CONFIG_TDLS
  5431. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  5432. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  5433. reply_len = -1;
  5434. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  5435. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  5436. reply_len = -1;
  5437. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  5438. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  5439. reply_len = -1;
  5440. #endif /* CONFIG_TDLS */
  5441. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  5442. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  5443. reply_size);
  5444. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  5445. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  5446. reply_size);
  5447. #ifdef CONFIG_AUTOSCAN
  5448. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  5449. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  5450. reply_len = -1;
  5451. #endif /* CONFIG_AUTOSCAN */
  5452. #ifdef ANDROID
  5453. } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
  5454. reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
  5455. reply_size);
  5456. #endif /* ANDROID */
  5457. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  5458. reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
  5459. reply_size);
  5460. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  5461. pmksa_cache_clear_current(wpa_s->wpa);
  5462. eapol_sm_request_reauth(wpa_s->eapol);
  5463. #ifdef CONFIG_WNM
  5464. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  5465. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  5466. reply_len = -1;
  5467. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
  5468. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
  5469. reply_len = -1;
  5470. #endif /* CONFIG_WNM */
  5471. } else if (os_strcmp(buf, "FLUSH") == 0) {
  5472. wpa_supplicant_ctrl_iface_flush(wpa_s);
  5473. } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
  5474. reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
  5475. reply_size);
  5476. } else {
  5477. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  5478. reply_len = 16;
  5479. }
  5480. if (reply_len < 0) {
  5481. os_memcpy(reply, "FAIL\n", 5);
  5482. reply_len = 5;
  5483. }
  5484. *resp_len = reply_len;
  5485. return reply;
  5486. }
  5487. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  5488. char *cmd)
  5489. {
  5490. struct wpa_interface iface;
  5491. char *pos;
  5492. /*
  5493. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  5494. * TAB<bridge_ifname>
  5495. */
  5496. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  5497. os_memset(&iface, 0, sizeof(iface));
  5498. do {
  5499. iface.ifname = pos = cmd;
  5500. pos = os_strchr(pos, '\t');
  5501. if (pos)
  5502. *pos++ = '\0';
  5503. if (iface.ifname[0] == '\0')
  5504. return -1;
  5505. if (pos == NULL)
  5506. break;
  5507. iface.confname = pos;
  5508. pos = os_strchr(pos, '\t');
  5509. if (pos)
  5510. *pos++ = '\0';
  5511. if (iface.confname[0] == '\0')
  5512. iface.confname = NULL;
  5513. if (pos == NULL)
  5514. break;
  5515. iface.driver = pos;
  5516. pos = os_strchr(pos, '\t');
  5517. if (pos)
  5518. *pos++ = '\0';
  5519. if (iface.driver[0] == '\0')
  5520. iface.driver = NULL;
  5521. if (pos == NULL)
  5522. break;
  5523. iface.ctrl_interface = pos;
  5524. pos = os_strchr(pos, '\t');
  5525. if (pos)
  5526. *pos++ = '\0';
  5527. if (iface.ctrl_interface[0] == '\0')
  5528. iface.ctrl_interface = NULL;
  5529. if (pos == NULL)
  5530. break;
  5531. iface.driver_param = pos;
  5532. pos = os_strchr(pos, '\t');
  5533. if (pos)
  5534. *pos++ = '\0';
  5535. if (iface.driver_param[0] == '\0')
  5536. iface.driver_param = NULL;
  5537. if (pos == NULL)
  5538. break;
  5539. iface.bridge_ifname = pos;
  5540. pos = os_strchr(pos, '\t');
  5541. if (pos)
  5542. *pos++ = '\0';
  5543. if (iface.bridge_ifname[0] == '\0')
  5544. iface.bridge_ifname = NULL;
  5545. if (pos == NULL)
  5546. break;
  5547. } while (0);
  5548. if (wpa_supplicant_get_iface(global, iface.ifname))
  5549. return -1;
  5550. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  5551. }
  5552. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  5553. char *cmd)
  5554. {
  5555. struct wpa_supplicant *wpa_s;
  5556. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  5557. wpa_s = wpa_supplicant_get_iface(global, cmd);
  5558. if (wpa_s == NULL)
  5559. return -1;
  5560. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  5561. }
  5562. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  5563. {
  5564. struct wpa_interface_info *prev;
  5565. while (iface) {
  5566. prev = iface;
  5567. iface = iface->next;
  5568. os_free(prev->ifname);
  5569. os_free(prev->desc);
  5570. os_free(prev);
  5571. }
  5572. }
  5573. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  5574. char *buf, int len)
  5575. {
  5576. int i, res;
  5577. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  5578. char *pos, *end;
  5579. for (i = 0; wpa_drivers[i]; i++) {
  5580. struct wpa_driver_ops *drv = wpa_drivers[i];
  5581. if (drv->get_interfaces == NULL)
  5582. continue;
  5583. tmp = drv->get_interfaces(global->drv_priv[i]);
  5584. if (tmp == NULL)
  5585. continue;
  5586. if (last == NULL)
  5587. iface = last = tmp;
  5588. else
  5589. last->next = tmp;
  5590. while (last->next)
  5591. last = last->next;
  5592. }
  5593. pos = buf;
  5594. end = buf + len;
  5595. for (tmp = iface; tmp; tmp = tmp->next) {
  5596. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  5597. tmp->drv_name, tmp->ifname,
  5598. tmp->desc ? tmp->desc : "");
  5599. if (res < 0 || res >= end - pos) {
  5600. *pos = '\0';
  5601. break;
  5602. }
  5603. pos += res;
  5604. }
  5605. wpa_free_iface_info(iface);
  5606. return pos - buf;
  5607. }
  5608. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  5609. char *buf, int len)
  5610. {
  5611. int res;
  5612. char *pos, *end;
  5613. struct wpa_supplicant *wpa_s;
  5614. wpa_s = global->ifaces;
  5615. pos = buf;
  5616. end = buf + len;
  5617. while (wpa_s) {
  5618. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  5619. if (res < 0 || res >= end - pos) {
  5620. *pos = '\0';
  5621. break;
  5622. }
  5623. pos += res;
  5624. wpa_s = wpa_s->next;
  5625. }
  5626. return pos - buf;
  5627. }
  5628. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  5629. const char *ifname,
  5630. char *cmd, size_t *resp_len)
  5631. {
  5632. struct wpa_supplicant *wpa_s;
  5633. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5634. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  5635. break;
  5636. }
  5637. if (wpa_s == NULL) {
  5638. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  5639. if (resp)
  5640. *resp_len = os_strlen(resp);
  5641. else
  5642. *resp_len = 1;
  5643. return resp;
  5644. }
  5645. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  5646. }
  5647. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  5648. char *buf, size_t *resp_len)
  5649. {
  5650. #ifdef CONFIG_P2P
  5651. static const char * cmd[] = {
  5652. "LIST_NETWORKS",
  5653. "SAVE_CONFIG",
  5654. "P2P_FIND",
  5655. "P2P_STOP_FIND",
  5656. "P2P_LISTEN",
  5657. "P2P_GROUP_ADD",
  5658. "P2P_GET_PASSPHRASE",
  5659. "P2P_SERVICE_UPDATE",
  5660. "P2P_SERVICE_FLUSH",
  5661. "P2P_FLUSH",
  5662. "P2P_CANCEL",
  5663. "P2P_PRESENCE_REQ",
  5664. "P2P_EXT_LISTEN",
  5665. NULL
  5666. };
  5667. static const char * prefix[] = {
  5668. #ifdef ANDROID
  5669. "DRIVER ",
  5670. #endif /* ANDROID */
  5671. "GET_NETWORK ",
  5672. "REMOVE_NETWORK ",
  5673. "SET ",
  5674. "P2P_FIND ",
  5675. "P2P_CONNECT ",
  5676. "P2P_LISTEN ",
  5677. "P2P_GROUP_REMOVE ",
  5678. "P2P_GROUP_ADD ",
  5679. "P2P_PROV_DISC ",
  5680. "P2P_SERV_DISC_REQ ",
  5681. "P2P_SERV_DISC_CANCEL_REQ ",
  5682. "P2P_SERV_DISC_RESP ",
  5683. "P2P_SERV_DISC_EXTERNAL ",
  5684. "P2P_SERVICE_ADD ",
  5685. "P2P_SERVICE_DEL ",
  5686. "P2P_REJECT ",
  5687. "P2P_INVITE ",
  5688. "P2P_PEER ",
  5689. "P2P_SET ",
  5690. "P2P_UNAUTHORIZE ",
  5691. "P2P_PRESENCE_REQ ",
  5692. "P2P_EXT_LISTEN ",
  5693. "P2P_REMOVE_CLIENT ",
  5694. NULL
  5695. };
  5696. int found = 0;
  5697. int i;
  5698. if (global->p2p_init_wpa_s == NULL)
  5699. return NULL;
  5700. for (i = 0; !found && cmd[i]; i++) {
  5701. if (os_strcmp(buf, cmd[i]) == 0)
  5702. found = 1;
  5703. }
  5704. for (i = 0; !found && prefix[i]; i++) {
  5705. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  5706. found = 1;
  5707. }
  5708. if (found)
  5709. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  5710. buf, resp_len);
  5711. #endif /* CONFIG_P2P */
  5712. return NULL;
  5713. }
  5714. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  5715. char *buf, size_t *resp_len)
  5716. {
  5717. #ifdef CONFIG_WIFI_DISPLAY
  5718. if (global->p2p_init_wpa_s == NULL)
  5719. return NULL;
  5720. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  5721. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  5722. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  5723. buf, resp_len);
  5724. #endif /* CONFIG_WIFI_DISPLAY */
  5725. return NULL;
  5726. }
  5727. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  5728. char *buf, size_t *resp_len)
  5729. {
  5730. char *ret;
  5731. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  5732. if (ret)
  5733. return ret;
  5734. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  5735. if (ret)
  5736. return ret;
  5737. return NULL;
  5738. }
  5739. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  5740. {
  5741. char *value;
  5742. value = os_strchr(cmd, ' ');
  5743. if (value == NULL)
  5744. return -1;
  5745. *value++ = '\0';
  5746. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  5747. #ifdef CONFIG_WIFI_DISPLAY
  5748. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  5749. wifi_display_enable(global, !!atoi(value));
  5750. return 0;
  5751. }
  5752. #endif /* CONFIG_WIFI_DISPLAY */
  5753. return -1;
  5754. }
  5755. #ifndef CONFIG_NO_CONFIG_WRITE
  5756. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  5757. {
  5758. int ret = 0;
  5759. struct wpa_supplicant *wpa_s;
  5760. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5761. if (!wpa_s->conf->update_config) {
  5762. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  5763. continue;
  5764. }
  5765. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  5766. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  5767. ret = 1;
  5768. } else {
  5769. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  5770. }
  5771. }
  5772. return ret;
  5773. }
  5774. #endif /* CONFIG_NO_CONFIG_WRITE */
  5775. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  5776. char *buf, size_t buflen)
  5777. {
  5778. char *pos, *end;
  5779. int ret;
  5780. struct wpa_supplicant *wpa_s;
  5781. pos = buf;
  5782. end = buf + buflen;
  5783. #ifdef CONFIG_P2P
  5784. if (global->p2p && !global->p2p_disabled) {
  5785. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  5786. "\n"
  5787. "p2p_state=%s\n",
  5788. MAC2STR(global->p2p_dev_addr),
  5789. p2p_get_state_txt(global->p2p));
  5790. if (ret < 0 || ret >= end - pos)
  5791. return pos - buf;
  5792. pos += ret;
  5793. } else if (global->p2p) {
  5794. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  5795. if (ret < 0 || ret >= end - pos)
  5796. return pos - buf;
  5797. pos += ret;
  5798. }
  5799. #endif /* CONFIG_P2P */
  5800. #ifdef CONFIG_WIFI_DISPLAY
  5801. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  5802. !!global->wifi_display);
  5803. if (ret < 0 || ret >= end - pos)
  5804. return pos - buf;
  5805. pos += ret;
  5806. #endif /* CONFIG_WIFI_DISPLAY */
  5807. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5808. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  5809. "address=" MACSTR "\n",
  5810. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  5811. if (ret < 0 || ret >= end - pos)
  5812. return pos - buf;
  5813. pos += ret;
  5814. }
  5815. return pos - buf;
  5816. }
  5817. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  5818. char *buf, size_t *resp_len)
  5819. {
  5820. char *reply;
  5821. const int reply_size = 2048;
  5822. int reply_len;
  5823. int level = MSG_DEBUG;
  5824. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  5825. char *pos = os_strchr(buf + 7, ' ');
  5826. if (pos) {
  5827. *pos++ = '\0';
  5828. return wpas_global_ctrl_iface_ifname(global,
  5829. buf + 7, pos,
  5830. resp_len);
  5831. }
  5832. }
  5833. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  5834. if (reply)
  5835. return reply;
  5836. if (os_strcmp(buf, "PING") == 0)
  5837. level = MSG_EXCESSIVE;
  5838. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  5839. (const u8 *) buf, os_strlen(buf));
  5840. reply = os_malloc(reply_size);
  5841. if (reply == NULL) {
  5842. *resp_len = 1;
  5843. return NULL;
  5844. }
  5845. os_memcpy(reply, "OK\n", 3);
  5846. reply_len = 3;
  5847. if (os_strcmp(buf, "PING") == 0) {
  5848. os_memcpy(reply, "PONG\n", 5);
  5849. reply_len = 5;
  5850. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  5851. if (wpa_supplicant_global_iface_add(global, buf + 14))
  5852. reply_len = -1;
  5853. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  5854. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  5855. reply_len = -1;
  5856. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  5857. reply_len = wpa_supplicant_global_iface_list(
  5858. global, reply, reply_size);
  5859. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  5860. reply_len = wpa_supplicant_global_iface_interfaces(
  5861. global, reply, reply_size);
  5862. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  5863. wpa_supplicant_terminate_proc(global);
  5864. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  5865. wpas_notify_suspend(global);
  5866. } else if (os_strcmp(buf, "RESUME") == 0) {
  5867. wpas_notify_resume(global);
  5868. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  5869. if (wpas_global_ctrl_iface_set(global, buf + 4))
  5870. reply_len = -1;
  5871. #ifndef CONFIG_NO_CONFIG_WRITE
  5872. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  5873. if (wpas_global_ctrl_iface_save_config(global))
  5874. reply_len = -1;
  5875. #endif /* CONFIG_NO_CONFIG_WRITE */
  5876. } else if (os_strcmp(buf, "STATUS") == 0) {
  5877. reply_len = wpas_global_ctrl_iface_status(global, reply,
  5878. reply_size);
  5879. #ifdef CONFIG_MODULE_TESTS
  5880. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  5881. int wpas_module_tests(void);
  5882. if (wpas_module_tests() < 0)
  5883. reply_len = -1;
  5884. #endif /* CONFIG_MODULE_TESTS */
  5885. } else {
  5886. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  5887. reply_len = 16;
  5888. }
  5889. if (reply_len < 0) {
  5890. os_memcpy(reply, "FAIL\n", 5);
  5891. reply_len = 5;
  5892. }
  5893. *resp_len = reply_len;
  5894. return reply;
  5895. }