ctrl_iface.c 177 KB

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