ctrl_iface.c 149 KB

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