ctrl_iface.c 149 KB

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