ctrl_iface.c 177 KB

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