ctrl_iface.c 165 KB

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