ctrl_iface.c 231 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2015, 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. #ifdef CONFIG_TESTING_OPTIONS
  10. #include <net/ethernet.h>
  11. #include <netinet/ip.h>
  12. #endif /* CONFIG_TESTING_OPTIONS */
  13. #include "utils/common.h"
  14. #include "utils/eloop.h"
  15. #include "utils/uuid.h"
  16. #include "common/version.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "common/ieee802_11_common.h"
  19. #include "common/wpa_ctrl.h"
  20. #include "crypto/tls.h"
  21. #include "ap/hostapd.h"
  22. #include "eap_peer/eap.h"
  23. #include "eapol_supp/eapol_supp_sm.h"
  24. #include "rsn_supp/wpa.h"
  25. #include "rsn_supp/preauth.h"
  26. #include "rsn_supp/pmksa_cache.h"
  27. #include "l2_packet/l2_packet.h"
  28. #include "wps/wps.h"
  29. #include "fst/fst.h"
  30. #include "fst/fst_ctrl_iface.h"
  31. #include "config.h"
  32. #include "wpa_supplicant_i.h"
  33. #include "driver_i.h"
  34. #include "wps_supplicant.h"
  35. #include "ibss_rsn.h"
  36. #include "ap.h"
  37. #include "p2p_supplicant.h"
  38. #include "p2p/p2p.h"
  39. #include "hs20_supplicant.h"
  40. #include "wifi_display.h"
  41. #include "notify.h"
  42. #include "bss.h"
  43. #include "scan.h"
  44. #include "ctrl_iface.h"
  45. #include "interworking.h"
  46. #include "blacklist.h"
  47. #include "autoscan.h"
  48. #include "wnm_sta.h"
  49. #include "offchannel.h"
  50. #include "drivers/driver.h"
  51. #include "mesh.h"
  52. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  53. char *buf, int len);
  54. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  55. char *buf, int len);
  56. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
  57. char *val);
  58. static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
  59. {
  60. char *pos;
  61. u8 addr[ETH_ALEN], *filter = NULL, *n;
  62. size_t count = 0;
  63. pos = val;
  64. while (pos) {
  65. if (*pos == '\0')
  66. break;
  67. if (hwaddr_aton(pos, addr)) {
  68. os_free(filter);
  69. return -1;
  70. }
  71. n = os_realloc_array(filter, count + 1, ETH_ALEN);
  72. if (n == NULL) {
  73. os_free(filter);
  74. return -1;
  75. }
  76. filter = n;
  77. os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
  78. count++;
  79. pos = os_strchr(pos, ' ');
  80. if (pos)
  81. pos++;
  82. }
  83. wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
  84. os_free(wpa_s->bssid_filter);
  85. wpa_s->bssid_filter = filter;
  86. wpa_s->bssid_filter_count = count;
  87. return 0;
  88. }
  89. static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
  90. {
  91. char *pos;
  92. u8 addr[ETH_ALEN], *bssid = NULL, *n;
  93. struct wpa_ssid_value *ssid = NULL, *ns;
  94. size_t count = 0, ssid_count = 0;
  95. struct wpa_ssid *c;
  96. /*
  97. * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
  98. * SSID_SPEC ::= ssid <SSID_HEX>
  99. * BSSID_SPEC ::= bssid <BSSID_HEX>
  100. */
  101. pos = val;
  102. while (pos) {
  103. if (*pos == '\0')
  104. break;
  105. if (os_strncmp(pos, "bssid ", 6) == 0) {
  106. int res;
  107. pos += 6;
  108. res = hwaddr_aton2(pos, addr);
  109. if (res < 0) {
  110. os_free(ssid);
  111. os_free(bssid);
  112. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  113. "BSSID value '%s'", pos);
  114. return -1;
  115. }
  116. pos += res;
  117. n = os_realloc_array(bssid, count + 1, ETH_ALEN);
  118. if (n == NULL) {
  119. os_free(ssid);
  120. os_free(bssid);
  121. return -1;
  122. }
  123. bssid = n;
  124. os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
  125. count++;
  126. } else if (os_strncmp(pos, "ssid ", 5) == 0) {
  127. char *end;
  128. pos += 5;
  129. end = pos;
  130. while (*end) {
  131. if (*end == '\0' || *end == ' ')
  132. break;
  133. end++;
  134. }
  135. ns = os_realloc_array(ssid, ssid_count + 1,
  136. sizeof(struct wpa_ssid_value));
  137. if (ns == NULL) {
  138. os_free(ssid);
  139. os_free(bssid);
  140. return -1;
  141. }
  142. ssid = ns;
  143. if ((end - pos) & 0x01 ||
  144. end - pos > 2 * SSID_MAX_LEN ||
  145. hexstr2bin(pos, ssid[ssid_count].ssid,
  146. (end - pos) / 2) < 0) {
  147. os_free(ssid);
  148. os_free(bssid);
  149. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  150. "SSID value '%s'", pos);
  151. return -1;
  152. }
  153. ssid[ssid_count].ssid_len = (end - pos) / 2;
  154. wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
  155. ssid[ssid_count].ssid,
  156. ssid[ssid_count].ssid_len);
  157. ssid_count++;
  158. pos = end;
  159. } else {
  160. wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
  161. "'%s'", pos);
  162. os_free(ssid);
  163. os_free(bssid);
  164. return -1;
  165. }
  166. pos = os_strchr(pos, ' ');
  167. if (pos)
  168. pos++;
  169. }
  170. wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
  171. os_free(wpa_s->disallow_aps_bssid);
  172. wpa_s->disallow_aps_bssid = bssid;
  173. wpa_s->disallow_aps_bssid_count = count;
  174. wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
  175. os_free(wpa_s->disallow_aps_ssid);
  176. wpa_s->disallow_aps_ssid = ssid;
  177. wpa_s->disallow_aps_ssid_count = ssid_count;
  178. if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
  179. return 0;
  180. c = wpa_s->current_ssid;
  181. if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
  182. return 0;
  183. if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
  184. !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
  185. return 0;
  186. wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
  187. "because current AP was marked disallowed");
  188. #ifdef CONFIG_SME
  189. wpa_s->sme.prev_bssid_set = 0;
  190. #endif /* CONFIG_SME */
  191. wpa_s->reassociate = 1;
  192. wpa_s->own_disconnect_req = 1;
  193. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  194. wpa_supplicant_req_scan(wpa_s, 0, 0);
  195. return 0;
  196. }
  197. #ifndef CONFIG_NO_CONFIG_BLOBS
  198. static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
  199. {
  200. char *name = pos;
  201. struct wpa_config_blob *blob;
  202. size_t len;
  203. pos = os_strchr(pos, ' ');
  204. if (pos == NULL)
  205. return -1;
  206. *pos++ = '\0';
  207. len = os_strlen(pos);
  208. if (len & 1)
  209. return -1;
  210. wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
  211. blob = os_zalloc(sizeof(*blob));
  212. if (blob == NULL)
  213. return -1;
  214. blob->name = os_strdup(name);
  215. blob->data = os_malloc(len / 2);
  216. if (blob->name == NULL || blob->data == NULL) {
  217. wpa_config_free_blob(blob);
  218. return -1;
  219. }
  220. if (hexstr2bin(pos, blob->data, len / 2) < 0) {
  221. wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
  222. wpa_config_free_blob(blob);
  223. return -1;
  224. }
  225. blob->len = len / 2;
  226. wpa_config_set_blob(wpa_s->conf, blob);
  227. return 0;
  228. }
  229. #endif /* CONFIG_NO_CONFIG_BLOBS */
  230. static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
  231. {
  232. char *params;
  233. char *pos;
  234. int *freqs = NULL;
  235. int ret;
  236. if (atoi(cmd)) {
  237. params = os_strchr(cmd, ' ');
  238. os_free(wpa_s->manual_sched_scan_freqs);
  239. if (params) {
  240. params++;
  241. pos = os_strstr(params, "freq=");
  242. if (pos)
  243. freqs = freq_range_to_channel_list(wpa_s,
  244. pos + 5);
  245. }
  246. wpa_s->manual_sched_scan_freqs = freqs;
  247. ret = wpas_start_pno(wpa_s);
  248. } else {
  249. ret = wpas_stop_pno(wpa_s);
  250. }
  251. return ret;
  252. }
  253. static int wpas_ctrl_set_band(struct wpa_supplicant *wpa_s, char *band)
  254. {
  255. union wpa_event_data event;
  256. if (os_strcmp(band, "AUTO") == 0)
  257. wpa_s->setband = WPA_SETBAND_AUTO;
  258. else if (os_strcmp(band, "5G") == 0)
  259. wpa_s->setband = WPA_SETBAND_5G;
  260. else if (os_strcmp(band, "2G") == 0)
  261. wpa_s->setband = WPA_SETBAND_2G;
  262. else
  263. return -1;
  264. if (wpa_drv_setband(wpa_s, wpa_s->setband) == 0) {
  265. os_memset(&event, 0, sizeof(event));
  266. event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
  267. event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
  268. wpa_supplicant_event(wpa_s, EVENT_CHANNEL_LIST_CHANGED, &event);
  269. }
  270. return 0;
  271. }
  272. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  273. char *cmd)
  274. {
  275. char *value;
  276. int ret = 0;
  277. value = os_strchr(cmd, ' ');
  278. if (value == NULL)
  279. return -1;
  280. *value++ = '\0';
  281. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  282. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  283. eapol_sm_configure(wpa_s->eapol,
  284. atoi(value), -1, -1, -1);
  285. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  286. eapol_sm_configure(wpa_s->eapol,
  287. -1, atoi(value), -1, -1);
  288. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  289. eapol_sm_configure(wpa_s->eapol,
  290. -1, -1, atoi(value), -1);
  291. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  292. eapol_sm_configure(wpa_s->eapol,
  293. -1, -1, -1, atoi(value));
  294. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  295. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  296. atoi(value)))
  297. ret = -1;
  298. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  299. 0) {
  300. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  301. atoi(value)))
  302. ret = -1;
  303. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  304. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  305. ret = -1;
  306. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  307. wpa_s->wps_fragment_size = atoi(value);
  308. #ifdef CONFIG_WPS_TESTING
  309. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  310. long int val;
  311. val = strtol(value, NULL, 0);
  312. if (val < 0 || val > 0xff) {
  313. ret = -1;
  314. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  315. "wps_version_number %ld", val);
  316. } else {
  317. wps_version_number = val;
  318. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  319. "version %u.%u",
  320. (wps_version_number & 0xf0) >> 4,
  321. wps_version_number & 0x0f);
  322. }
  323. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  324. wps_testing_dummy_cred = atoi(value);
  325. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  326. wps_testing_dummy_cred);
  327. } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
  328. wps_corrupt_pkhash = atoi(value);
  329. wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
  330. wps_corrupt_pkhash);
  331. } else if (os_strcasecmp(cmd, "wps_force_auth_types") == 0) {
  332. if (value[0] == '\0') {
  333. wps_force_auth_types_in_use = 0;
  334. } else {
  335. wps_force_auth_types = strtol(value, NULL, 0);
  336. wps_force_auth_types_in_use = 1;
  337. }
  338. } else if (os_strcasecmp(cmd, "wps_force_encr_types") == 0) {
  339. if (value[0] == '\0') {
  340. wps_force_encr_types_in_use = 0;
  341. } else {
  342. wps_force_encr_types = strtol(value, NULL, 0);
  343. wps_force_encr_types_in_use = 1;
  344. }
  345. #endif /* CONFIG_WPS_TESTING */
  346. } else if (os_strcasecmp(cmd, "ampdu") == 0) {
  347. if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
  348. ret = -1;
  349. #ifdef CONFIG_TDLS
  350. #ifdef CONFIG_TDLS_TESTING
  351. } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
  352. extern unsigned int tdls_testing;
  353. tdls_testing = strtol(value, NULL, 0);
  354. wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
  355. #endif /* CONFIG_TDLS_TESTING */
  356. } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
  357. int disabled = atoi(value);
  358. wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
  359. if (disabled) {
  360. if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
  361. ret = -1;
  362. } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
  363. ret = -1;
  364. wpa_tdls_enable(wpa_s->wpa, !disabled);
  365. #endif /* CONFIG_TDLS */
  366. } else if (os_strcasecmp(cmd, "pno") == 0) {
  367. ret = wpas_ctrl_pno(wpa_s, value);
  368. } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
  369. int disabled = atoi(value);
  370. if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
  371. ret = -1;
  372. else if (disabled)
  373. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  374. } else if (os_strcasecmp(cmd, "uapsd") == 0) {
  375. if (os_strcmp(value, "disable") == 0)
  376. wpa_s->set_sta_uapsd = 0;
  377. else {
  378. int be, bk, vi, vo;
  379. char *pos;
  380. /* format: BE,BK,VI,VO;max SP Length */
  381. be = atoi(value);
  382. pos = os_strchr(value, ',');
  383. if (pos == NULL)
  384. return -1;
  385. pos++;
  386. bk = atoi(pos);
  387. pos = os_strchr(pos, ',');
  388. if (pos == NULL)
  389. return -1;
  390. pos++;
  391. vi = atoi(pos);
  392. pos = os_strchr(pos, ',');
  393. if (pos == NULL)
  394. return -1;
  395. pos++;
  396. vo = atoi(pos);
  397. /* ignore max SP Length for now */
  398. wpa_s->set_sta_uapsd = 1;
  399. wpa_s->sta_uapsd = 0;
  400. if (be)
  401. wpa_s->sta_uapsd |= BIT(0);
  402. if (bk)
  403. wpa_s->sta_uapsd |= BIT(1);
  404. if (vi)
  405. wpa_s->sta_uapsd |= BIT(2);
  406. if (vo)
  407. wpa_s->sta_uapsd |= BIT(3);
  408. }
  409. } else if (os_strcasecmp(cmd, "ps") == 0) {
  410. ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
  411. #ifdef CONFIG_WIFI_DISPLAY
  412. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  413. int enabled = !!atoi(value);
  414. if (enabled && !wpa_s->global->p2p)
  415. ret = -1;
  416. else
  417. wifi_display_enable(wpa_s->global, enabled);
  418. #endif /* CONFIG_WIFI_DISPLAY */
  419. } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
  420. ret = set_bssid_filter(wpa_s, value);
  421. } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
  422. ret = set_disallow_aps(wpa_s, value);
  423. } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
  424. wpa_s->no_keep_alive = !!atoi(value);
  425. #ifdef CONFIG_TESTING_OPTIONS
  426. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  427. wpa_s->ext_mgmt_frame_handling = !!atoi(value);
  428. } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
  429. wpa_s->ext_eapol_frame_io = !!atoi(value);
  430. #ifdef CONFIG_AP
  431. if (wpa_s->ap_iface) {
  432. wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
  433. wpa_s->ext_eapol_frame_io;
  434. }
  435. #endif /* CONFIG_AP */
  436. } else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
  437. wpa_s->extra_roc_dur = atoi(value);
  438. } else if (os_strcasecmp(cmd, "test_failure") == 0) {
  439. wpa_s->test_failure = atoi(value);
  440. } else if (os_strcasecmp(cmd, "p2p_go_csa_on_inv") == 0) {
  441. wpa_s->p2p_go_csa_on_inv = !!atoi(value);
  442. #endif /* CONFIG_TESTING_OPTIONS */
  443. #ifndef CONFIG_NO_CONFIG_BLOBS
  444. } else if (os_strcmp(cmd, "blob") == 0) {
  445. ret = wpas_ctrl_set_blob(wpa_s, value);
  446. #endif /* CONFIG_NO_CONFIG_BLOBS */
  447. } else if (os_strcasecmp(cmd, "setband") == 0) {
  448. ret = wpas_ctrl_set_band(wpa_s, value);
  449. } else {
  450. value[-1] = '=';
  451. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  452. if (ret == 0)
  453. wpa_supplicant_update_config(wpa_s);
  454. }
  455. return ret;
  456. }
  457. static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
  458. char *cmd, char *buf, size_t buflen)
  459. {
  460. int res = -1;
  461. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  462. if (os_strcmp(cmd, "version") == 0) {
  463. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  464. } else if (os_strcasecmp(cmd, "country") == 0) {
  465. if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
  466. res = os_snprintf(buf, buflen, "%c%c",
  467. wpa_s->conf->country[0],
  468. wpa_s->conf->country[1]);
  469. #ifdef CONFIG_WIFI_DISPLAY
  470. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  471. int enabled;
  472. if (wpa_s->global->p2p == NULL ||
  473. wpa_s->global->p2p_disabled)
  474. enabled = 0;
  475. else
  476. enabled = wpa_s->global->wifi_display;
  477. res = os_snprintf(buf, buflen, "%d", enabled);
  478. #endif /* CONFIG_WIFI_DISPLAY */
  479. #ifdef CONFIG_TESTING_GET_GTK
  480. } else if (os_strcmp(cmd, "gtk") == 0) {
  481. if (wpa_s->last_gtk_len == 0)
  482. return -1;
  483. res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
  484. wpa_s->last_gtk_len);
  485. return res;
  486. #endif /* CONFIG_TESTING_GET_GTK */
  487. } else if (os_strcmp(cmd, "tls_library") == 0) {
  488. res = tls_get_library_version(buf, buflen);
  489. } else {
  490. res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
  491. }
  492. if (os_snprintf_error(buflen, res))
  493. return -1;
  494. return res;
  495. }
  496. #ifdef IEEE8021X_EAPOL
  497. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  498. char *addr)
  499. {
  500. u8 bssid[ETH_ALEN];
  501. struct wpa_ssid *ssid = wpa_s->current_ssid;
  502. if (hwaddr_aton(addr, bssid)) {
  503. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  504. "'%s'", addr);
  505. return -1;
  506. }
  507. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  508. rsn_preauth_deinit(wpa_s->wpa);
  509. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  510. return -1;
  511. return 0;
  512. }
  513. #endif /* IEEE8021X_EAPOL */
  514. #ifdef CONFIG_PEERKEY
  515. /* MLME-STKSTART.request(peer) */
  516. static int wpa_supplicant_ctrl_iface_stkstart(
  517. struct wpa_supplicant *wpa_s, char *addr)
  518. {
  519. u8 peer[ETH_ALEN];
  520. if (hwaddr_aton(addr, peer)) {
  521. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  522. "address '%s'", addr);
  523. return -1;
  524. }
  525. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  526. MAC2STR(peer));
  527. return wpa_sm_stkstart(wpa_s->wpa, peer);
  528. }
  529. #endif /* CONFIG_PEERKEY */
  530. #ifdef CONFIG_TDLS
  531. static int wpa_supplicant_ctrl_iface_tdls_discover(
  532. struct wpa_supplicant *wpa_s, char *addr)
  533. {
  534. u8 peer[ETH_ALEN];
  535. int ret;
  536. if (hwaddr_aton(addr, peer)) {
  537. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
  538. "address '%s'", addr);
  539. return -1;
  540. }
  541. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
  542. MAC2STR(peer));
  543. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  544. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  545. else
  546. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  547. return ret;
  548. }
  549. static int wpa_supplicant_ctrl_iface_tdls_setup(
  550. struct wpa_supplicant *wpa_s, char *addr)
  551. {
  552. u8 peer[ETH_ALEN];
  553. int ret;
  554. if (hwaddr_aton(addr, peer)) {
  555. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
  556. "address '%s'", addr);
  557. return -1;
  558. }
  559. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
  560. MAC2STR(peer));
  561. if ((wpa_s->conf->tdls_external_control) &&
  562. wpa_tdls_is_external_setup(wpa_s->wpa))
  563. return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  564. wpa_tdls_remove(wpa_s->wpa, peer);
  565. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  566. ret = wpa_tdls_start(wpa_s->wpa, peer);
  567. else
  568. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  569. return ret;
  570. }
  571. static int wpa_supplicant_ctrl_iface_tdls_teardown(
  572. struct wpa_supplicant *wpa_s, char *addr)
  573. {
  574. u8 peer[ETH_ALEN];
  575. int ret;
  576. if (os_strcmp(addr, "*") == 0) {
  577. /* remove everyone */
  578. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
  579. wpa_tdls_teardown_peers(wpa_s->wpa);
  580. return 0;
  581. }
  582. if (hwaddr_aton(addr, peer)) {
  583. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
  584. "address '%s'", addr);
  585. return -1;
  586. }
  587. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
  588. MAC2STR(peer));
  589. if ((wpa_s->conf->tdls_external_control) &&
  590. wpa_tdls_is_external_setup(wpa_s->wpa))
  591. return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  592. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  593. ret = wpa_tdls_teardown_link(
  594. wpa_s->wpa, peer,
  595. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  596. else
  597. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  598. return ret;
  599. }
  600. static int ctrl_iface_get_capability_tdls(
  601. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  602. {
  603. int ret;
  604. ret = os_snprintf(buf, buflen, "%s\n",
  605. wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
  606. (wpa_s->drv_flags &
  607. WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
  608. "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
  609. if (os_snprintf_error(buflen, ret))
  610. return -1;
  611. return ret;
  612. }
  613. static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
  614. struct wpa_supplicant *wpa_s, char *cmd)
  615. {
  616. u8 peer[ETH_ALEN];
  617. struct hostapd_freq_params freq_params;
  618. u8 oper_class;
  619. char *pos, *end;
  620. if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
  621. wpa_printf(MSG_INFO,
  622. "tdls_chanswitch: Only supported with external setup");
  623. return -1;
  624. }
  625. os_memset(&freq_params, 0, sizeof(freq_params));
  626. pos = os_strchr(cmd, ' ');
  627. if (pos == NULL)
  628. return -1;
  629. *pos++ = '\0';
  630. oper_class = strtol(pos, &end, 10);
  631. if (pos == end) {
  632. wpa_printf(MSG_INFO,
  633. "tdls_chanswitch: Invalid op class provided");
  634. return -1;
  635. }
  636. pos = end;
  637. freq_params.freq = atoi(pos);
  638. if (freq_params.freq == 0) {
  639. wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
  640. return -1;
  641. }
  642. #define SET_FREQ_SETTING(str) \
  643. do { \
  644. const char *pos2 = os_strstr(pos, " " #str "="); \
  645. if (pos2) { \
  646. pos2 += sizeof(" " #str "=") - 1; \
  647. freq_params.str = atoi(pos2); \
  648. } \
  649. } while (0)
  650. SET_FREQ_SETTING(center_freq1);
  651. SET_FREQ_SETTING(center_freq2);
  652. SET_FREQ_SETTING(bandwidth);
  653. SET_FREQ_SETTING(sec_channel_offset);
  654. #undef SET_FREQ_SETTING
  655. freq_params.ht_enabled = !!os_strstr(pos, " ht");
  656. freq_params.vht_enabled = !!os_strstr(pos, " vht");
  657. if (hwaddr_aton(cmd, peer)) {
  658. wpa_printf(MSG_DEBUG,
  659. "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
  660. cmd);
  661. return -1;
  662. }
  663. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
  664. " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
  665. MAC2STR(peer), oper_class, freq_params.freq,
  666. freq_params.center_freq1, freq_params.center_freq2,
  667. freq_params.bandwidth, freq_params.sec_channel_offset,
  668. freq_params.ht_enabled ? " HT" : "",
  669. freq_params.vht_enabled ? " VHT" : "");
  670. return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
  671. &freq_params);
  672. }
  673. static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
  674. struct wpa_supplicant *wpa_s, char *cmd)
  675. {
  676. u8 peer[ETH_ALEN];
  677. if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
  678. wpa_printf(MSG_INFO,
  679. "tdls_chanswitch: Only supported with external setup");
  680. return -1;
  681. }
  682. if (hwaddr_aton(cmd, peer)) {
  683. wpa_printf(MSG_DEBUG,
  684. "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
  685. cmd);
  686. return -1;
  687. }
  688. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
  689. MAC2STR(peer));
  690. return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
  691. }
  692. static int wpa_supplicant_ctrl_iface_tdls_link_status(
  693. struct wpa_supplicant *wpa_s, const char *addr,
  694. char *buf, size_t buflen)
  695. {
  696. u8 peer[ETH_ALEN];
  697. const char *tdls_status;
  698. int ret;
  699. if (hwaddr_aton(addr, peer)) {
  700. wpa_printf(MSG_DEBUG,
  701. "CTRL_IFACE TDLS_LINK_STATUS: Invalid address '%s'",
  702. addr);
  703. return -1;
  704. }
  705. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS " MACSTR,
  706. MAC2STR(peer));
  707. tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
  708. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS: %s", tdls_status);
  709. ret = os_snprintf(buf, buflen, "TDLS link status: %s\n", tdls_status);
  710. if (os_snprintf_error(buflen, ret))
  711. return -1;
  712. return ret;
  713. }
  714. #endif /* CONFIG_TDLS */
  715. static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
  716. {
  717. char *token, *context = NULL;
  718. struct wmm_ac_ts_setup_params params = {
  719. .tsid = 0xff,
  720. .direction = 0xff,
  721. };
  722. while ((token = str_token(cmd, " ", &context))) {
  723. if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
  724. sscanf(token, "up=%i", &params.user_priority) == 1 ||
  725. sscanf(token, "nominal_msdu_size=%i",
  726. &params.nominal_msdu_size) == 1 ||
  727. sscanf(token, "mean_data_rate=%i",
  728. &params.mean_data_rate) == 1 ||
  729. sscanf(token, "min_phy_rate=%i",
  730. &params.minimum_phy_rate) == 1 ||
  731. sscanf(token, "sba=%i",
  732. &params.surplus_bandwidth_allowance) == 1)
  733. continue;
  734. if (os_strcasecmp(token, "downlink") == 0) {
  735. params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
  736. } else if (os_strcasecmp(token, "uplink") == 0) {
  737. params.direction = WMM_TSPEC_DIRECTION_UPLINK;
  738. } else if (os_strcasecmp(token, "bidi") == 0) {
  739. params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
  740. } else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
  741. params.fixed_nominal_msdu = 1;
  742. } else {
  743. wpa_printf(MSG_DEBUG,
  744. "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
  745. token);
  746. return -1;
  747. }
  748. }
  749. return wpas_wmm_ac_addts(wpa_s, &params);
  750. }
  751. static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
  752. {
  753. u8 tsid = atoi(cmd);
  754. return wpas_wmm_ac_delts(wpa_s, tsid);
  755. }
  756. #ifdef CONFIG_IEEE80211R
  757. static int wpa_supplicant_ctrl_iface_ft_ds(
  758. struct wpa_supplicant *wpa_s, char *addr)
  759. {
  760. u8 target_ap[ETH_ALEN];
  761. struct wpa_bss *bss;
  762. const u8 *mdie;
  763. if (hwaddr_aton(addr, target_ap)) {
  764. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  765. "address '%s'", addr);
  766. return -1;
  767. }
  768. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  769. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  770. if (bss)
  771. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  772. else
  773. mdie = NULL;
  774. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  775. }
  776. #endif /* CONFIG_IEEE80211R */
  777. #ifdef CONFIG_WPS
  778. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  779. char *cmd)
  780. {
  781. u8 bssid[ETH_ALEN], *_bssid = bssid;
  782. #ifdef CONFIG_P2P
  783. u8 p2p_dev_addr[ETH_ALEN];
  784. #endif /* CONFIG_P2P */
  785. #ifdef CONFIG_AP
  786. u8 *_p2p_dev_addr = NULL;
  787. #endif /* CONFIG_AP */
  788. if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
  789. _bssid = NULL;
  790. #ifdef CONFIG_P2P
  791. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  792. if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
  793. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
  794. "P2P Device Address '%s'",
  795. cmd + 13);
  796. return -1;
  797. }
  798. _p2p_dev_addr = p2p_dev_addr;
  799. #endif /* CONFIG_P2P */
  800. } else if (hwaddr_aton(cmd, bssid)) {
  801. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  802. cmd);
  803. return -1;
  804. }
  805. #ifdef CONFIG_AP
  806. if (wpa_s->ap_iface)
  807. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
  808. #endif /* CONFIG_AP */
  809. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  810. }
  811. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  812. char *cmd, char *buf,
  813. size_t buflen)
  814. {
  815. u8 bssid[ETH_ALEN], *_bssid = bssid;
  816. char *pin;
  817. int ret;
  818. pin = os_strchr(cmd, ' ');
  819. if (pin)
  820. *pin++ = '\0';
  821. if (os_strcmp(cmd, "any") == 0)
  822. _bssid = NULL;
  823. else if (os_strcmp(cmd, "get") == 0) {
  824. ret = wps_generate_pin();
  825. goto done;
  826. } else if (hwaddr_aton(cmd, bssid)) {
  827. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  828. cmd);
  829. return -1;
  830. }
  831. #ifdef CONFIG_AP
  832. if (wpa_s->ap_iface) {
  833. int timeout = 0;
  834. char *pos;
  835. if (pin) {
  836. pos = os_strchr(pin, ' ');
  837. if (pos) {
  838. *pos++ = '\0';
  839. timeout = atoi(pos);
  840. }
  841. }
  842. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  843. buf, buflen, timeout);
  844. }
  845. #endif /* CONFIG_AP */
  846. if (pin) {
  847. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  848. DEV_PW_DEFAULT);
  849. if (ret < 0)
  850. return -1;
  851. ret = os_snprintf(buf, buflen, "%s", pin);
  852. if (os_snprintf_error(buflen, ret))
  853. return -1;
  854. return ret;
  855. }
  856. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  857. if (ret < 0)
  858. return -1;
  859. done:
  860. /* Return the generated PIN */
  861. ret = os_snprintf(buf, buflen, "%08d", ret);
  862. if (os_snprintf_error(buflen, ret))
  863. return -1;
  864. return ret;
  865. }
  866. static int wpa_supplicant_ctrl_iface_wps_check_pin(
  867. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  868. {
  869. char pin[9];
  870. size_t len;
  871. char *pos;
  872. int ret;
  873. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  874. (u8 *) cmd, os_strlen(cmd));
  875. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  876. if (*pos < '0' || *pos > '9')
  877. continue;
  878. pin[len++] = *pos;
  879. if (len == 9) {
  880. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  881. return -1;
  882. }
  883. }
  884. if (len != 4 && len != 8) {
  885. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  886. return -1;
  887. }
  888. pin[len] = '\0';
  889. if (len == 8) {
  890. unsigned int pin_val;
  891. pin_val = atoi(pin);
  892. if (!wps_pin_valid(pin_val)) {
  893. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  894. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  895. if (os_snprintf_error(buflen, ret))
  896. return -1;
  897. return ret;
  898. }
  899. }
  900. ret = os_snprintf(buf, buflen, "%s", pin);
  901. if (os_snprintf_error(buflen, ret))
  902. return -1;
  903. return ret;
  904. }
  905. #ifdef CONFIG_WPS_NFC
  906. static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
  907. char *cmd)
  908. {
  909. u8 bssid[ETH_ALEN], *_bssid = bssid;
  910. if (cmd == NULL || cmd[0] == '\0')
  911. _bssid = NULL;
  912. else if (hwaddr_aton(cmd, bssid))
  913. return -1;
  914. return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
  915. 0, 0);
  916. }
  917. static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  918. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  919. {
  920. int ndef;
  921. struct wpabuf *buf;
  922. int res;
  923. char *pos;
  924. pos = os_strchr(cmd, ' ');
  925. if (pos)
  926. *pos++ = '\0';
  927. if (os_strcmp(cmd, "WPS") == 0)
  928. ndef = 0;
  929. else if (os_strcmp(cmd, "NDEF") == 0)
  930. ndef = 1;
  931. else
  932. return -1;
  933. buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
  934. if (buf == NULL)
  935. return -1;
  936. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  937. wpabuf_len(buf));
  938. reply[res++] = '\n';
  939. reply[res] = '\0';
  940. wpabuf_free(buf);
  941. return res;
  942. }
  943. static int wpa_supplicant_ctrl_iface_wps_nfc_token(
  944. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  945. {
  946. int ndef;
  947. struct wpabuf *buf;
  948. int res;
  949. if (os_strcmp(cmd, "WPS") == 0)
  950. ndef = 0;
  951. else if (os_strcmp(cmd, "NDEF") == 0)
  952. ndef = 1;
  953. else
  954. return -1;
  955. buf = wpas_wps_nfc_token(wpa_s, ndef);
  956. if (buf == NULL)
  957. return -1;
  958. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  959. wpabuf_len(buf));
  960. reply[res++] = '\n';
  961. reply[res] = '\0';
  962. wpabuf_free(buf);
  963. return res;
  964. }
  965. static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
  966. struct wpa_supplicant *wpa_s, char *pos)
  967. {
  968. size_t len;
  969. struct wpabuf *buf;
  970. int ret;
  971. char *freq;
  972. int forced_freq = 0;
  973. freq = strstr(pos, " freq=");
  974. if (freq) {
  975. *freq = '\0';
  976. freq += 6;
  977. forced_freq = atoi(freq);
  978. }
  979. len = os_strlen(pos);
  980. if (len & 0x01)
  981. return -1;
  982. len /= 2;
  983. buf = wpabuf_alloc(len);
  984. if (buf == NULL)
  985. return -1;
  986. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  987. wpabuf_free(buf);
  988. return -1;
  989. }
  990. ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
  991. wpabuf_free(buf);
  992. return ret;
  993. }
  994. static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
  995. char *reply, size_t max_len,
  996. int ndef)
  997. {
  998. struct wpabuf *buf;
  999. int res;
  1000. buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
  1001. if (buf == NULL)
  1002. return -1;
  1003. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1004. wpabuf_len(buf));
  1005. reply[res++] = '\n';
  1006. reply[res] = '\0';
  1007. wpabuf_free(buf);
  1008. return res;
  1009. }
  1010. #ifdef CONFIG_P2P
  1011. static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
  1012. char *reply, size_t max_len,
  1013. int ndef)
  1014. {
  1015. struct wpabuf *buf;
  1016. int res;
  1017. buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
  1018. if (buf == NULL) {
  1019. wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
  1020. return -1;
  1021. }
  1022. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1023. wpabuf_len(buf));
  1024. reply[res++] = '\n';
  1025. reply[res] = '\0';
  1026. wpabuf_free(buf);
  1027. return res;
  1028. }
  1029. #endif /* CONFIG_P2P */
  1030. static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
  1031. char *cmd, char *reply,
  1032. size_t max_len)
  1033. {
  1034. char *pos;
  1035. int ndef;
  1036. pos = os_strchr(cmd, ' ');
  1037. if (pos == NULL)
  1038. return -1;
  1039. *pos++ = '\0';
  1040. if (os_strcmp(cmd, "WPS") == 0)
  1041. ndef = 0;
  1042. else if (os_strcmp(cmd, "NDEF") == 0)
  1043. ndef = 1;
  1044. else
  1045. return -1;
  1046. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  1047. if (!ndef)
  1048. return -1;
  1049. return wpas_ctrl_nfc_get_handover_req_wps(
  1050. wpa_s, reply, max_len, ndef);
  1051. }
  1052. #ifdef CONFIG_P2P
  1053. if (os_strcmp(pos, "P2P-CR") == 0) {
  1054. return wpas_ctrl_nfc_get_handover_req_p2p(
  1055. wpa_s, reply, max_len, ndef);
  1056. }
  1057. #endif /* CONFIG_P2P */
  1058. return -1;
  1059. }
  1060. static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
  1061. char *reply, size_t max_len,
  1062. int ndef, int cr, char *uuid)
  1063. {
  1064. struct wpabuf *buf;
  1065. int res;
  1066. buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
  1067. if (buf == NULL)
  1068. return -1;
  1069. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1070. wpabuf_len(buf));
  1071. reply[res++] = '\n';
  1072. reply[res] = '\0';
  1073. wpabuf_free(buf);
  1074. return res;
  1075. }
  1076. #ifdef CONFIG_P2P
  1077. static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
  1078. char *reply, size_t max_len,
  1079. int ndef, int tag)
  1080. {
  1081. struct wpabuf *buf;
  1082. int res;
  1083. buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
  1084. if (buf == NULL)
  1085. return -1;
  1086. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1087. wpabuf_len(buf));
  1088. reply[res++] = '\n';
  1089. reply[res] = '\0';
  1090. wpabuf_free(buf);
  1091. return res;
  1092. }
  1093. #endif /* CONFIG_P2P */
  1094. static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
  1095. char *cmd, char *reply,
  1096. size_t max_len)
  1097. {
  1098. char *pos, *pos2;
  1099. int ndef;
  1100. pos = os_strchr(cmd, ' ');
  1101. if (pos == NULL)
  1102. return -1;
  1103. *pos++ = '\0';
  1104. if (os_strcmp(cmd, "WPS") == 0)
  1105. ndef = 0;
  1106. else if (os_strcmp(cmd, "NDEF") == 0)
  1107. ndef = 1;
  1108. else
  1109. return -1;
  1110. pos2 = os_strchr(pos, ' ');
  1111. if (pos2)
  1112. *pos2++ = '\0';
  1113. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  1114. if (!ndef)
  1115. return -1;
  1116. return wpas_ctrl_nfc_get_handover_sel_wps(
  1117. wpa_s, reply, max_len, ndef,
  1118. os_strcmp(pos, "WPS-CR") == 0, pos2);
  1119. }
  1120. #ifdef CONFIG_P2P
  1121. if (os_strcmp(pos, "P2P-CR") == 0) {
  1122. return wpas_ctrl_nfc_get_handover_sel_p2p(
  1123. wpa_s, reply, max_len, ndef, 0);
  1124. }
  1125. if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
  1126. return wpas_ctrl_nfc_get_handover_sel_p2p(
  1127. wpa_s, reply, max_len, ndef, 1);
  1128. }
  1129. #endif /* CONFIG_P2P */
  1130. return -1;
  1131. }
  1132. static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
  1133. char *cmd)
  1134. {
  1135. size_t len;
  1136. struct wpabuf *req, *sel;
  1137. int ret;
  1138. char *pos, *role, *type, *pos2;
  1139. #ifdef CONFIG_P2P
  1140. char *freq;
  1141. int forced_freq = 0;
  1142. freq = strstr(cmd, " freq=");
  1143. if (freq) {
  1144. *freq = '\0';
  1145. freq += 6;
  1146. forced_freq = atoi(freq);
  1147. }
  1148. #endif /* CONFIG_P2P */
  1149. role = cmd;
  1150. pos = os_strchr(role, ' ');
  1151. if (pos == NULL) {
  1152. wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
  1153. return -1;
  1154. }
  1155. *pos++ = '\0';
  1156. type = pos;
  1157. pos = os_strchr(type, ' ');
  1158. if (pos == NULL) {
  1159. wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
  1160. return -1;
  1161. }
  1162. *pos++ = '\0';
  1163. pos2 = os_strchr(pos, ' ');
  1164. if (pos2 == NULL) {
  1165. wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
  1166. return -1;
  1167. }
  1168. *pos2++ = '\0';
  1169. len = os_strlen(pos);
  1170. if (len & 0x01) {
  1171. wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
  1172. return -1;
  1173. }
  1174. len /= 2;
  1175. req = wpabuf_alloc(len);
  1176. if (req == NULL) {
  1177. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
  1178. return -1;
  1179. }
  1180. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  1181. wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
  1182. wpabuf_free(req);
  1183. return -1;
  1184. }
  1185. len = os_strlen(pos2);
  1186. if (len & 0x01) {
  1187. wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
  1188. wpabuf_free(req);
  1189. return -1;
  1190. }
  1191. len /= 2;
  1192. sel = wpabuf_alloc(len);
  1193. if (sel == NULL) {
  1194. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
  1195. wpabuf_free(req);
  1196. return -1;
  1197. }
  1198. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  1199. wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
  1200. wpabuf_free(req);
  1201. wpabuf_free(sel);
  1202. return -1;
  1203. }
  1204. wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
  1205. role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
  1206. if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
  1207. ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
  1208. #ifdef CONFIG_AP
  1209. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
  1210. {
  1211. ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
  1212. if (ret < 0)
  1213. ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
  1214. #endif /* CONFIG_AP */
  1215. #ifdef CONFIG_P2P
  1216. } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
  1217. {
  1218. ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
  1219. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
  1220. {
  1221. ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
  1222. forced_freq);
  1223. #endif /* CONFIG_P2P */
  1224. } else {
  1225. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  1226. "reported: role=%s type=%s", role, type);
  1227. ret = -1;
  1228. }
  1229. wpabuf_free(req);
  1230. wpabuf_free(sel);
  1231. if (ret)
  1232. wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
  1233. return ret;
  1234. }
  1235. #endif /* CONFIG_WPS_NFC */
  1236. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  1237. char *cmd)
  1238. {
  1239. u8 bssid[ETH_ALEN];
  1240. char *pin;
  1241. char *new_ssid;
  1242. char *new_auth;
  1243. char *new_encr;
  1244. char *new_key;
  1245. struct wps_new_ap_settings ap;
  1246. pin = os_strchr(cmd, ' ');
  1247. if (pin == NULL)
  1248. return -1;
  1249. *pin++ = '\0';
  1250. if (hwaddr_aton(cmd, bssid)) {
  1251. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  1252. cmd);
  1253. return -1;
  1254. }
  1255. new_ssid = os_strchr(pin, ' ');
  1256. if (new_ssid == NULL)
  1257. return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  1258. *new_ssid++ = '\0';
  1259. new_auth = os_strchr(new_ssid, ' ');
  1260. if (new_auth == NULL)
  1261. return -1;
  1262. *new_auth++ = '\0';
  1263. new_encr = os_strchr(new_auth, ' ');
  1264. if (new_encr == NULL)
  1265. return -1;
  1266. *new_encr++ = '\0';
  1267. new_key = os_strchr(new_encr, ' ');
  1268. if (new_key == NULL)
  1269. return -1;
  1270. *new_key++ = '\0';
  1271. os_memset(&ap, 0, sizeof(ap));
  1272. ap.ssid_hex = new_ssid;
  1273. ap.auth = new_auth;
  1274. ap.encr = new_encr;
  1275. ap.key_hex = new_key;
  1276. return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
  1277. }
  1278. #ifdef CONFIG_AP
  1279. static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
  1280. char *cmd, char *buf,
  1281. size_t buflen)
  1282. {
  1283. int timeout = 300;
  1284. char *pos;
  1285. const char *pin_txt;
  1286. if (!wpa_s->ap_iface)
  1287. return -1;
  1288. pos = os_strchr(cmd, ' ');
  1289. if (pos)
  1290. *pos++ = '\0';
  1291. if (os_strcmp(cmd, "disable") == 0) {
  1292. wpas_wps_ap_pin_disable(wpa_s);
  1293. return os_snprintf(buf, buflen, "OK\n");
  1294. }
  1295. if (os_strcmp(cmd, "random") == 0) {
  1296. if (pos)
  1297. timeout = atoi(pos);
  1298. pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
  1299. if (pin_txt == NULL)
  1300. return -1;
  1301. return os_snprintf(buf, buflen, "%s", pin_txt);
  1302. }
  1303. if (os_strcmp(cmd, "get") == 0) {
  1304. pin_txt = wpas_wps_ap_pin_get(wpa_s);
  1305. if (pin_txt == NULL)
  1306. return -1;
  1307. return os_snprintf(buf, buflen, "%s", pin_txt);
  1308. }
  1309. if (os_strcmp(cmd, "set") == 0) {
  1310. char *pin;
  1311. if (pos == NULL)
  1312. return -1;
  1313. pin = pos;
  1314. pos = os_strchr(pos, ' ');
  1315. if (pos) {
  1316. *pos++ = '\0';
  1317. timeout = atoi(pos);
  1318. }
  1319. if (os_strlen(pin) > buflen)
  1320. return -1;
  1321. if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
  1322. return -1;
  1323. return os_snprintf(buf, buflen, "%s", pin);
  1324. }
  1325. return -1;
  1326. }
  1327. #endif /* CONFIG_AP */
  1328. #ifdef CONFIG_WPS_ER
  1329. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  1330. char *cmd)
  1331. {
  1332. char *uuid = cmd, *pin, *pos;
  1333. u8 addr_buf[ETH_ALEN], *addr = NULL;
  1334. pin = os_strchr(uuid, ' ');
  1335. if (pin == NULL)
  1336. return -1;
  1337. *pin++ = '\0';
  1338. pos = os_strchr(pin, ' ');
  1339. if (pos) {
  1340. *pos++ = '\0';
  1341. if (hwaddr_aton(pos, addr_buf) == 0)
  1342. addr = addr_buf;
  1343. }
  1344. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  1345. }
  1346. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  1347. char *cmd)
  1348. {
  1349. char *uuid = cmd, *pin;
  1350. pin = os_strchr(uuid, ' ');
  1351. if (pin == NULL)
  1352. return -1;
  1353. *pin++ = '\0';
  1354. return wpas_wps_er_learn(wpa_s, uuid, pin);
  1355. }
  1356. static int wpa_supplicant_ctrl_iface_wps_er_set_config(
  1357. struct wpa_supplicant *wpa_s, char *cmd)
  1358. {
  1359. char *uuid = cmd, *id;
  1360. id = os_strchr(uuid, ' ');
  1361. if (id == NULL)
  1362. return -1;
  1363. *id++ = '\0';
  1364. return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
  1365. }
  1366. static int wpa_supplicant_ctrl_iface_wps_er_config(
  1367. struct wpa_supplicant *wpa_s, char *cmd)
  1368. {
  1369. char *pin;
  1370. char *new_ssid;
  1371. char *new_auth;
  1372. char *new_encr;
  1373. char *new_key;
  1374. struct wps_new_ap_settings ap;
  1375. pin = os_strchr(cmd, ' ');
  1376. if (pin == NULL)
  1377. return -1;
  1378. *pin++ = '\0';
  1379. new_ssid = os_strchr(pin, ' ');
  1380. if (new_ssid == NULL)
  1381. return -1;
  1382. *new_ssid++ = '\0';
  1383. new_auth = os_strchr(new_ssid, ' ');
  1384. if (new_auth == NULL)
  1385. return -1;
  1386. *new_auth++ = '\0';
  1387. new_encr = os_strchr(new_auth, ' ');
  1388. if (new_encr == NULL)
  1389. return -1;
  1390. *new_encr++ = '\0';
  1391. new_key = os_strchr(new_encr, ' ');
  1392. if (new_key == NULL)
  1393. return -1;
  1394. *new_key++ = '\0';
  1395. os_memset(&ap, 0, sizeof(ap));
  1396. ap.ssid_hex = new_ssid;
  1397. ap.auth = new_auth;
  1398. ap.encr = new_encr;
  1399. ap.key_hex = new_key;
  1400. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  1401. }
  1402. #ifdef CONFIG_WPS_NFC
  1403. static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  1404. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1405. {
  1406. int ndef;
  1407. struct wpabuf *buf;
  1408. int res;
  1409. char *uuid;
  1410. uuid = os_strchr(cmd, ' ');
  1411. if (uuid == NULL)
  1412. return -1;
  1413. *uuid++ = '\0';
  1414. if (os_strcmp(cmd, "WPS") == 0)
  1415. ndef = 0;
  1416. else if (os_strcmp(cmd, "NDEF") == 0)
  1417. ndef = 1;
  1418. else
  1419. return -1;
  1420. buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
  1421. if (buf == NULL)
  1422. return -1;
  1423. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1424. wpabuf_len(buf));
  1425. reply[res++] = '\n';
  1426. reply[res] = '\0';
  1427. wpabuf_free(buf);
  1428. return res;
  1429. }
  1430. #endif /* CONFIG_WPS_NFC */
  1431. #endif /* CONFIG_WPS_ER */
  1432. #endif /* CONFIG_WPS */
  1433. #ifdef CONFIG_IBSS_RSN
  1434. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  1435. struct wpa_supplicant *wpa_s, char *addr)
  1436. {
  1437. u8 peer[ETH_ALEN];
  1438. if (hwaddr_aton(addr, peer)) {
  1439. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  1440. "address '%s'", addr);
  1441. return -1;
  1442. }
  1443. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  1444. MAC2STR(peer));
  1445. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  1446. }
  1447. #endif /* CONFIG_IBSS_RSN */
  1448. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  1449. char *rsp)
  1450. {
  1451. #ifdef IEEE8021X_EAPOL
  1452. char *pos, *id_pos;
  1453. int id;
  1454. struct wpa_ssid *ssid;
  1455. pos = os_strchr(rsp, '-');
  1456. if (pos == NULL)
  1457. return -1;
  1458. *pos++ = '\0';
  1459. id_pos = pos;
  1460. pos = os_strchr(pos, ':');
  1461. if (pos == NULL)
  1462. return -1;
  1463. *pos++ = '\0';
  1464. id = atoi(id_pos);
  1465. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  1466. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1467. (u8 *) pos, os_strlen(pos));
  1468. ssid = wpa_config_get_network(wpa_s->conf, id);
  1469. if (ssid == NULL) {
  1470. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1471. "to update", id);
  1472. return -1;
  1473. }
  1474. return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
  1475. pos);
  1476. #else /* IEEE8021X_EAPOL */
  1477. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1478. return -1;
  1479. #endif /* IEEE8021X_EAPOL */
  1480. }
  1481. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  1482. const char *params,
  1483. char *buf, size_t buflen)
  1484. {
  1485. char *pos, *end, tmp[30];
  1486. int res, verbose, wps, ret;
  1487. #ifdef CONFIG_HS20
  1488. const u8 *hs20;
  1489. #endif /* CONFIG_HS20 */
  1490. const u8 *sess_id;
  1491. size_t sess_id_len;
  1492. if (os_strcmp(params, "-DRIVER") == 0)
  1493. return wpa_drv_status(wpa_s, buf, buflen);
  1494. verbose = os_strcmp(params, "-VERBOSE") == 0;
  1495. wps = os_strcmp(params, "-WPS") == 0;
  1496. pos = buf;
  1497. end = buf + buflen;
  1498. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1499. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1500. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  1501. MAC2STR(wpa_s->bssid));
  1502. if (os_snprintf_error(end - pos, ret))
  1503. return pos - buf;
  1504. pos += ret;
  1505. ret = os_snprintf(pos, end - pos, "freq=%u\n",
  1506. wpa_s->assoc_freq);
  1507. if (os_snprintf_error(end - pos, ret))
  1508. return pos - buf;
  1509. pos += ret;
  1510. if (ssid) {
  1511. u8 *_ssid = ssid->ssid;
  1512. size_t ssid_len = ssid->ssid_len;
  1513. u8 ssid_buf[SSID_MAX_LEN];
  1514. if (ssid_len == 0) {
  1515. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  1516. if (_res < 0)
  1517. ssid_len = 0;
  1518. else
  1519. ssid_len = _res;
  1520. _ssid = ssid_buf;
  1521. }
  1522. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  1523. wpa_ssid_txt(_ssid, ssid_len),
  1524. ssid->id);
  1525. if (os_snprintf_error(end - pos, ret))
  1526. return pos - buf;
  1527. pos += ret;
  1528. if (wps && ssid->passphrase &&
  1529. wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
  1530. (ssid->mode == WPAS_MODE_AP ||
  1531. ssid->mode == WPAS_MODE_P2P_GO)) {
  1532. ret = os_snprintf(pos, end - pos,
  1533. "passphrase=%s\n",
  1534. ssid->passphrase);
  1535. if (os_snprintf_error(end - pos, ret))
  1536. return pos - buf;
  1537. pos += ret;
  1538. }
  1539. if (ssid->id_str) {
  1540. ret = os_snprintf(pos, end - pos,
  1541. "id_str=%s\n",
  1542. ssid->id_str);
  1543. if (os_snprintf_error(end - pos, ret))
  1544. return pos - buf;
  1545. pos += ret;
  1546. }
  1547. switch (ssid->mode) {
  1548. case WPAS_MODE_INFRA:
  1549. ret = os_snprintf(pos, end - pos,
  1550. "mode=station\n");
  1551. break;
  1552. case WPAS_MODE_IBSS:
  1553. ret = os_snprintf(pos, end - pos,
  1554. "mode=IBSS\n");
  1555. break;
  1556. case WPAS_MODE_AP:
  1557. ret = os_snprintf(pos, end - pos,
  1558. "mode=AP\n");
  1559. break;
  1560. case WPAS_MODE_P2P_GO:
  1561. ret = os_snprintf(pos, end - pos,
  1562. "mode=P2P GO\n");
  1563. break;
  1564. case WPAS_MODE_P2P_GROUP_FORMATION:
  1565. ret = os_snprintf(pos, end - pos,
  1566. "mode=P2P GO - group "
  1567. "formation\n");
  1568. break;
  1569. default:
  1570. ret = 0;
  1571. break;
  1572. }
  1573. if (os_snprintf_error(end - pos, ret))
  1574. return pos - buf;
  1575. pos += ret;
  1576. }
  1577. #ifdef CONFIG_AP
  1578. if (wpa_s->ap_iface) {
  1579. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  1580. end - pos,
  1581. verbose);
  1582. } else
  1583. #endif /* CONFIG_AP */
  1584. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1585. }
  1586. #ifdef CONFIG_SAE
  1587. if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
  1588. #ifdef CONFIG_AP
  1589. !wpa_s->ap_iface &&
  1590. #endif /* CONFIG_AP */
  1591. wpa_s->sme.sae.state == SAE_ACCEPTED) {
  1592. ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
  1593. wpa_s->sme.sae.group);
  1594. if (os_snprintf_error(end - pos, ret))
  1595. return pos - buf;
  1596. pos += ret;
  1597. }
  1598. #endif /* CONFIG_SAE */
  1599. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  1600. wpa_supplicant_state_txt(wpa_s->wpa_state));
  1601. if (os_snprintf_error(end - pos, ret))
  1602. return pos - buf;
  1603. pos += ret;
  1604. if (wpa_s->l2 &&
  1605. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  1606. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  1607. if (os_snprintf_error(end - pos, ret))
  1608. return pos - buf;
  1609. pos += ret;
  1610. }
  1611. #ifdef CONFIG_P2P
  1612. if (wpa_s->global->p2p) {
  1613. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  1614. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  1615. if (os_snprintf_error(end - pos, ret))
  1616. return pos - buf;
  1617. pos += ret;
  1618. }
  1619. #endif /* CONFIG_P2P */
  1620. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  1621. MAC2STR(wpa_s->own_addr));
  1622. if (os_snprintf_error(end - pos, ret))
  1623. return pos - buf;
  1624. pos += ret;
  1625. #ifdef CONFIG_HS20
  1626. if (wpa_s->current_bss &&
  1627. (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
  1628. HS20_IE_VENDOR_TYPE)) &&
  1629. wpa_s->wpa_proto == WPA_PROTO_RSN &&
  1630. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1631. int release = 1;
  1632. if (hs20[1] >= 5) {
  1633. u8 rel_num = (hs20[6] & 0xf0) >> 4;
  1634. release = rel_num + 1;
  1635. }
  1636. ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
  1637. if (os_snprintf_error(end - pos, ret))
  1638. return pos - buf;
  1639. pos += ret;
  1640. }
  1641. if (wpa_s->current_ssid) {
  1642. struct wpa_cred *cred;
  1643. char *type;
  1644. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1645. size_t i;
  1646. if (wpa_s->current_ssid->parent_cred != cred)
  1647. continue;
  1648. if (cred->provisioning_sp) {
  1649. ret = os_snprintf(pos, end - pos,
  1650. "provisioning_sp=%s\n",
  1651. cred->provisioning_sp);
  1652. if (os_snprintf_error(end - pos, ret))
  1653. return pos - buf;
  1654. pos += ret;
  1655. }
  1656. if (!cred->domain)
  1657. goto no_domain;
  1658. i = 0;
  1659. if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
  1660. struct wpabuf *names =
  1661. wpa_s->current_bss->anqp->domain_name;
  1662. for (i = 0; names && i < cred->num_domain; i++)
  1663. {
  1664. if (domain_name_list_contains(
  1665. names, cred->domain[i], 1))
  1666. break;
  1667. }
  1668. if (i == cred->num_domain)
  1669. i = 0; /* show first entry by default */
  1670. }
  1671. ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
  1672. cred->domain[i]);
  1673. if (os_snprintf_error(end - pos, ret))
  1674. return pos - buf;
  1675. pos += ret;
  1676. no_domain:
  1677. if (wpa_s->current_bss == NULL ||
  1678. wpa_s->current_bss->anqp == NULL)
  1679. res = -1;
  1680. else
  1681. res = interworking_home_sp_cred(
  1682. wpa_s, cred,
  1683. wpa_s->current_bss->anqp->domain_name);
  1684. if (res > 0)
  1685. type = "home";
  1686. else if (res == 0)
  1687. type = "roaming";
  1688. else
  1689. type = "unknown";
  1690. ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
  1691. if (os_snprintf_error(end - pos, ret))
  1692. return pos - buf;
  1693. pos += ret;
  1694. break;
  1695. }
  1696. }
  1697. #endif /* CONFIG_HS20 */
  1698. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1699. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1700. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  1701. verbose);
  1702. if (res >= 0)
  1703. pos += res;
  1704. }
  1705. sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
  1706. if (sess_id) {
  1707. char *start = pos;
  1708. ret = os_snprintf(pos, end - pos, "eap_session_id=");
  1709. if (os_snprintf_error(end - pos, ret))
  1710. return start - buf;
  1711. pos += ret;
  1712. ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
  1713. if (ret <= 0)
  1714. return start - buf;
  1715. pos += ret;
  1716. ret = os_snprintf(pos, end - pos, "\n");
  1717. if (os_snprintf_error(end - pos, ret))
  1718. return start - buf;
  1719. pos += ret;
  1720. }
  1721. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1722. if (res >= 0)
  1723. pos += res;
  1724. #ifdef CONFIG_WPS
  1725. {
  1726. char uuid_str[100];
  1727. uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
  1728. ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
  1729. if (os_snprintf_error(end - pos, ret))
  1730. return pos - buf;
  1731. pos += ret;
  1732. }
  1733. #endif /* CONFIG_WPS */
  1734. #ifdef ANDROID
  1735. /*
  1736. * Allow using the STATUS command with default behavior, say for debug,
  1737. * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
  1738. * events with STATUS-NO_EVENTS.
  1739. */
  1740. if (os_strcmp(params, "-NO_EVENTS")) {
  1741. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
  1742. "id=%d state=%d BSSID=" MACSTR " SSID=%s",
  1743. wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
  1744. wpa_s->wpa_state,
  1745. MAC2STR(wpa_s->bssid),
  1746. wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
  1747. wpa_ssid_txt(wpa_s->current_ssid->ssid,
  1748. wpa_s->current_ssid->ssid_len) : "");
  1749. if (wpa_s->wpa_state == WPA_COMPLETED) {
  1750. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1751. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
  1752. "- connection to " MACSTR
  1753. " completed %s [id=%d id_str=%s]",
  1754. MAC2STR(wpa_s->bssid), "(auth)",
  1755. ssid ? ssid->id : -1,
  1756. ssid && ssid->id_str ? ssid->id_str : "");
  1757. }
  1758. }
  1759. #endif /* ANDROID */
  1760. return pos - buf;
  1761. }
  1762. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1763. char *cmd)
  1764. {
  1765. char *pos;
  1766. int id;
  1767. struct wpa_ssid *ssid;
  1768. u8 bssid[ETH_ALEN];
  1769. /* cmd: "<network id> <BSSID>" */
  1770. pos = os_strchr(cmd, ' ');
  1771. if (pos == NULL)
  1772. return -1;
  1773. *pos++ = '\0';
  1774. id = atoi(cmd);
  1775. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  1776. if (hwaddr_aton(pos, bssid)) {
  1777. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  1778. return -1;
  1779. }
  1780. ssid = wpa_config_get_network(wpa_s->conf, id);
  1781. if (ssid == NULL) {
  1782. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1783. "to update", id);
  1784. return -1;
  1785. }
  1786. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  1787. ssid->bssid_set = !is_zero_ether_addr(bssid);
  1788. return 0;
  1789. }
  1790. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  1791. char *cmd, char *buf,
  1792. size_t buflen)
  1793. {
  1794. u8 bssid[ETH_ALEN];
  1795. struct wpa_blacklist *e;
  1796. char *pos, *end;
  1797. int ret;
  1798. /* cmd: "BLACKLIST [<BSSID>]" */
  1799. if (*cmd == '\0') {
  1800. pos = buf;
  1801. end = buf + buflen;
  1802. e = wpa_s->blacklist;
  1803. while (e) {
  1804. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  1805. MAC2STR(e->bssid));
  1806. if (os_snprintf_error(end - pos, ret))
  1807. return pos - buf;
  1808. pos += ret;
  1809. e = e->next;
  1810. }
  1811. return pos - buf;
  1812. }
  1813. cmd++;
  1814. if (os_strncmp(cmd, "clear", 5) == 0) {
  1815. wpa_blacklist_clear(wpa_s);
  1816. os_memcpy(buf, "OK\n", 3);
  1817. return 3;
  1818. }
  1819. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  1820. if (hwaddr_aton(cmd, bssid)) {
  1821. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  1822. return -1;
  1823. }
  1824. /*
  1825. * Add the BSSID twice, so its count will be 2, causing it to be
  1826. * skipped when processing scan results.
  1827. */
  1828. ret = wpa_blacklist_add(wpa_s, bssid);
  1829. if (ret < 0)
  1830. return -1;
  1831. ret = wpa_blacklist_add(wpa_s, bssid);
  1832. if (ret < 0)
  1833. return -1;
  1834. os_memcpy(buf, "OK\n", 3);
  1835. return 3;
  1836. }
  1837. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  1838. char *cmd, char *buf,
  1839. size_t buflen)
  1840. {
  1841. char *pos, *end, *stamp;
  1842. int ret;
  1843. /* cmd: "LOG_LEVEL [<level>]" */
  1844. if (*cmd == '\0') {
  1845. pos = buf;
  1846. end = buf + buflen;
  1847. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1848. "Timestamp: %d\n",
  1849. debug_level_str(wpa_debug_level),
  1850. wpa_debug_timestamp);
  1851. if (os_snprintf_error(end - pos, ret))
  1852. ret = 0;
  1853. return ret;
  1854. }
  1855. while (*cmd == ' ')
  1856. cmd++;
  1857. stamp = os_strchr(cmd, ' ');
  1858. if (stamp) {
  1859. *stamp++ = '\0';
  1860. while (*stamp == ' ') {
  1861. stamp++;
  1862. }
  1863. }
  1864. if (os_strlen(cmd)) {
  1865. int level = str_to_debug_level(cmd);
  1866. if (level < 0)
  1867. return -1;
  1868. wpa_debug_level = level;
  1869. }
  1870. if (stamp && os_strlen(stamp))
  1871. wpa_debug_timestamp = atoi(stamp);
  1872. os_memcpy(buf, "OK\n", 3);
  1873. return 3;
  1874. }
  1875. static int wpa_supplicant_ctrl_iface_list_networks(
  1876. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1877. {
  1878. char *pos, *end, *prev;
  1879. struct wpa_ssid *ssid;
  1880. int ret;
  1881. pos = buf;
  1882. end = buf + buflen;
  1883. ret = os_snprintf(pos, end - pos,
  1884. "network id / ssid / bssid / flags\n");
  1885. if (os_snprintf_error(end - pos, ret))
  1886. return pos - buf;
  1887. pos += ret;
  1888. ssid = wpa_s->conf->ssid;
  1889. /* skip over ssids until we find next one */
  1890. if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
  1891. int last_id = atoi(cmd + 8);
  1892. if (last_id != -1) {
  1893. while (ssid != NULL && ssid->id <= last_id) {
  1894. ssid = ssid->next;
  1895. }
  1896. }
  1897. }
  1898. while (ssid) {
  1899. prev = pos;
  1900. ret = os_snprintf(pos, end - pos, "%d\t%s",
  1901. ssid->id,
  1902. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1903. if (os_snprintf_error(end - pos, ret))
  1904. return prev - buf;
  1905. pos += ret;
  1906. if (ssid->bssid_set) {
  1907. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  1908. MAC2STR(ssid->bssid));
  1909. } else {
  1910. ret = os_snprintf(pos, end - pos, "\tany");
  1911. }
  1912. if (os_snprintf_error(end - pos, ret))
  1913. return prev - buf;
  1914. pos += ret;
  1915. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  1916. ssid == wpa_s->current_ssid ?
  1917. "[CURRENT]" : "",
  1918. ssid->disabled ? "[DISABLED]" : "",
  1919. ssid->disabled_until.sec ?
  1920. "[TEMP-DISABLED]" : "",
  1921. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  1922. "");
  1923. if (os_snprintf_error(end - pos, ret))
  1924. return prev - buf;
  1925. pos += ret;
  1926. ret = os_snprintf(pos, end - pos, "\n");
  1927. if (os_snprintf_error(end - pos, ret))
  1928. return prev - buf;
  1929. pos += ret;
  1930. ssid = ssid->next;
  1931. }
  1932. return pos - buf;
  1933. }
  1934. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  1935. {
  1936. int ret;
  1937. ret = os_snprintf(pos, end - pos, "-");
  1938. if (os_snprintf_error(end - pos, ret))
  1939. return pos;
  1940. pos += ret;
  1941. ret = wpa_write_ciphers(pos, end, cipher, "+");
  1942. if (ret < 0)
  1943. return pos;
  1944. pos += ret;
  1945. return pos;
  1946. }
  1947. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  1948. const u8 *ie, size_t ie_len)
  1949. {
  1950. struct wpa_ie_data data;
  1951. char *start;
  1952. int ret;
  1953. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  1954. if (os_snprintf_error(end - pos, ret))
  1955. return pos;
  1956. pos += ret;
  1957. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  1958. ret = os_snprintf(pos, end - pos, "?]");
  1959. if (os_snprintf_error(end - pos, ret))
  1960. return pos;
  1961. pos += ret;
  1962. return pos;
  1963. }
  1964. start = pos;
  1965. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  1966. ret = os_snprintf(pos, end - pos, "%sEAP",
  1967. pos == start ? "" : "+");
  1968. if (os_snprintf_error(end - pos, ret))
  1969. return pos;
  1970. pos += ret;
  1971. }
  1972. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  1973. ret = os_snprintf(pos, end - pos, "%sPSK",
  1974. pos == start ? "" : "+");
  1975. if (os_snprintf_error(end - pos, ret))
  1976. return pos;
  1977. pos += ret;
  1978. }
  1979. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  1980. ret = os_snprintf(pos, end - pos, "%sNone",
  1981. pos == start ? "" : "+");
  1982. if (os_snprintf_error(end - pos, ret))
  1983. return pos;
  1984. pos += ret;
  1985. }
  1986. if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
  1987. ret = os_snprintf(pos, end - pos, "%sSAE",
  1988. pos == start ? "" : "+");
  1989. if (os_snprintf_error(end - pos, ret))
  1990. return pos;
  1991. pos += ret;
  1992. }
  1993. #ifdef CONFIG_IEEE80211R
  1994. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  1995. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  1996. pos == start ? "" : "+");
  1997. if (os_snprintf_error(end - pos, ret))
  1998. return pos;
  1999. pos += ret;
  2000. }
  2001. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  2002. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  2003. pos == start ? "" : "+");
  2004. if (os_snprintf_error(end - pos, ret))
  2005. return pos;
  2006. pos += ret;
  2007. }
  2008. if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
  2009. ret = os_snprintf(pos, end - pos, "%sFT/SAE",
  2010. pos == start ? "" : "+");
  2011. if (os_snprintf_error(end - pos, ret))
  2012. return pos;
  2013. pos += ret;
  2014. }
  2015. #endif /* CONFIG_IEEE80211R */
  2016. #ifdef CONFIG_IEEE80211W
  2017. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  2018. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  2019. pos == start ? "" : "+");
  2020. if (os_snprintf_error(end - pos, ret))
  2021. return pos;
  2022. pos += ret;
  2023. }
  2024. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  2025. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  2026. pos == start ? "" : "+");
  2027. if (os_snprintf_error(end - pos, ret))
  2028. return pos;
  2029. pos += ret;
  2030. }
  2031. #endif /* CONFIG_IEEE80211W */
  2032. #ifdef CONFIG_SUITEB
  2033. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  2034. ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
  2035. pos == start ? "" : "+");
  2036. if (os_snprintf_error(end - pos, ret))
  2037. return pos;
  2038. pos += ret;
  2039. }
  2040. #endif /* CONFIG_SUITEB */
  2041. #ifdef CONFIG_SUITEB192
  2042. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
  2043. ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
  2044. pos == start ? "" : "+");
  2045. if (os_snprintf_error(end - pos, ret))
  2046. return pos;
  2047. pos += ret;
  2048. }
  2049. #endif /* CONFIG_SUITEB192 */
  2050. if (data.key_mgmt & WPA_KEY_MGMT_OSEN) {
  2051. ret = os_snprintf(pos, end - pos, "%sOSEN",
  2052. pos == start ? "" : "+");
  2053. if (os_snprintf_error(end - pos, ret))
  2054. return pos;
  2055. pos += ret;
  2056. }
  2057. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  2058. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  2059. ret = os_snprintf(pos, end - pos, "-preauth");
  2060. if (os_snprintf_error(end - pos, ret))
  2061. return pos;
  2062. pos += ret;
  2063. }
  2064. ret = os_snprintf(pos, end - pos, "]");
  2065. if (os_snprintf_error(end - pos, ret))
  2066. return pos;
  2067. pos += ret;
  2068. return pos;
  2069. }
  2070. #ifdef CONFIG_WPS
  2071. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  2072. char *pos, char *end,
  2073. struct wpabuf *wps_ie)
  2074. {
  2075. int ret;
  2076. const char *txt;
  2077. if (wps_ie == NULL)
  2078. return pos;
  2079. if (wps_is_selected_pbc_registrar(wps_ie))
  2080. txt = "[WPS-PBC]";
  2081. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  2082. txt = "[WPS-AUTH]";
  2083. else if (wps_is_selected_pin_registrar(wps_ie))
  2084. txt = "[WPS-PIN]";
  2085. else
  2086. txt = "[WPS]";
  2087. ret = os_snprintf(pos, end - pos, "%s", txt);
  2088. if (!os_snprintf_error(end - pos, ret))
  2089. pos += ret;
  2090. wpabuf_free(wps_ie);
  2091. return pos;
  2092. }
  2093. #endif /* CONFIG_WPS */
  2094. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  2095. char *pos, char *end,
  2096. const struct wpa_bss *bss)
  2097. {
  2098. #ifdef CONFIG_WPS
  2099. struct wpabuf *wps_ie;
  2100. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  2101. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  2102. #else /* CONFIG_WPS */
  2103. return pos;
  2104. #endif /* CONFIG_WPS */
  2105. }
  2106. /* Format one result on one text line into a buffer. */
  2107. static int wpa_supplicant_ctrl_iface_scan_result(
  2108. struct wpa_supplicant *wpa_s,
  2109. const struct wpa_bss *bss, char *buf, size_t buflen)
  2110. {
  2111. char *pos, *end;
  2112. int ret;
  2113. const u8 *ie, *ie2, *osen_ie, *p2p, *mesh;
  2114. mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
  2115. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  2116. if (!p2p)
  2117. p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
  2118. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  2119. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  2120. 0)
  2121. return 0; /* Do not show P2P listen discovery results here */
  2122. pos = buf;
  2123. end = buf + buflen;
  2124. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  2125. MAC2STR(bss->bssid), bss->freq, bss->level);
  2126. if (os_snprintf_error(end - pos, ret))
  2127. return -1;
  2128. pos += ret;
  2129. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2130. if (ie)
  2131. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  2132. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2133. if (ie2) {
  2134. pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
  2135. ie2, 2 + ie2[1]);
  2136. }
  2137. osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  2138. if (osen_ie)
  2139. pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
  2140. osen_ie, 2 + osen_ie[1]);
  2141. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2142. if (!ie && !ie2 && !osen_ie && (bss->caps & IEEE80211_CAP_PRIVACY)) {
  2143. ret = os_snprintf(pos, end - pos, "[WEP]");
  2144. if (os_snprintf_error(end - pos, ret))
  2145. return -1;
  2146. pos += ret;
  2147. }
  2148. if (mesh) {
  2149. ret = os_snprintf(pos, end - pos, "[MESH]");
  2150. if (os_snprintf_error(end - pos, ret))
  2151. return -1;
  2152. pos += ret;
  2153. }
  2154. if (bss_is_dmg(bss)) {
  2155. const char *s;
  2156. ret = os_snprintf(pos, end - pos, "[DMG]");
  2157. if (os_snprintf_error(end - pos, ret))
  2158. return -1;
  2159. pos += ret;
  2160. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  2161. case IEEE80211_CAP_DMG_IBSS:
  2162. s = "[IBSS]";
  2163. break;
  2164. case IEEE80211_CAP_DMG_AP:
  2165. s = "[ESS]";
  2166. break;
  2167. case IEEE80211_CAP_DMG_PBSS:
  2168. s = "[PBSS]";
  2169. break;
  2170. default:
  2171. s = "";
  2172. break;
  2173. }
  2174. ret = os_snprintf(pos, end - pos, "%s", s);
  2175. if (os_snprintf_error(end - pos, ret))
  2176. return -1;
  2177. pos += ret;
  2178. } else {
  2179. if (bss->caps & IEEE80211_CAP_IBSS) {
  2180. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2181. if (os_snprintf_error(end - pos, ret))
  2182. return -1;
  2183. pos += ret;
  2184. }
  2185. if (bss->caps & IEEE80211_CAP_ESS) {
  2186. ret = os_snprintf(pos, end - pos, "[ESS]");
  2187. if (os_snprintf_error(end - pos, ret))
  2188. return -1;
  2189. pos += ret;
  2190. }
  2191. }
  2192. if (p2p) {
  2193. ret = os_snprintf(pos, end - pos, "[P2P]");
  2194. if (os_snprintf_error(end - pos, ret))
  2195. return -1;
  2196. pos += ret;
  2197. }
  2198. #ifdef CONFIG_HS20
  2199. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  2200. ret = os_snprintf(pos, end - pos, "[HS20]");
  2201. if (os_snprintf_error(end - pos, ret))
  2202. return -1;
  2203. pos += ret;
  2204. }
  2205. #endif /* CONFIG_HS20 */
  2206. #ifdef CONFIG_FST
  2207. if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
  2208. ret = os_snprintf(pos, end - pos, "[FST]");
  2209. if (os_snprintf_error(end - pos, ret))
  2210. return -1;
  2211. pos += ret;
  2212. }
  2213. #endif /* CONFIG_FST */
  2214. ret = os_snprintf(pos, end - pos, "\t%s",
  2215. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2216. if (os_snprintf_error(end - pos, ret))
  2217. return -1;
  2218. pos += ret;
  2219. ret = os_snprintf(pos, end - pos, "\n");
  2220. if (os_snprintf_error(end - pos, ret))
  2221. return -1;
  2222. pos += ret;
  2223. return pos - buf;
  2224. }
  2225. static int wpa_supplicant_ctrl_iface_scan_results(
  2226. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2227. {
  2228. char *pos, *end;
  2229. struct wpa_bss *bss;
  2230. int ret;
  2231. pos = buf;
  2232. end = buf + buflen;
  2233. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  2234. "flags / ssid\n");
  2235. if (os_snprintf_error(end - pos, ret))
  2236. return pos - buf;
  2237. pos += ret;
  2238. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  2239. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  2240. end - pos);
  2241. if (ret < 0 || ret >= end - pos)
  2242. return pos - buf;
  2243. pos += ret;
  2244. }
  2245. return pos - buf;
  2246. }
  2247. #ifdef CONFIG_MESH
  2248. static int wpa_supplicant_ctrl_iface_mesh_interface_add(
  2249. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  2250. {
  2251. char *pos, ifname[IFNAMSIZ + 1];
  2252. ifname[0] = '\0';
  2253. pos = os_strstr(cmd, "ifname=");
  2254. if (pos) {
  2255. pos += 7;
  2256. os_strlcpy(ifname, pos, sizeof(ifname));
  2257. }
  2258. if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
  2259. return -1;
  2260. os_strlcpy(reply, ifname, max_len);
  2261. return os_strlen(ifname);
  2262. }
  2263. static int wpa_supplicant_ctrl_iface_mesh_group_add(
  2264. struct wpa_supplicant *wpa_s, char *cmd)
  2265. {
  2266. int id;
  2267. struct wpa_ssid *ssid;
  2268. id = atoi(cmd);
  2269. wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
  2270. ssid = wpa_config_get_network(wpa_s->conf, id);
  2271. if (ssid == NULL) {
  2272. wpa_printf(MSG_DEBUG,
  2273. "CTRL_IFACE: Could not find network id=%d", id);
  2274. return -1;
  2275. }
  2276. if (ssid->mode != WPAS_MODE_MESH) {
  2277. wpa_printf(MSG_DEBUG,
  2278. "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
  2279. return -1;
  2280. }
  2281. if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
  2282. ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
  2283. wpa_printf(MSG_ERROR,
  2284. "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
  2285. return -1;
  2286. }
  2287. /*
  2288. * TODO: If necessary write our own group_add function,
  2289. * for now we can reuse select_network
  2290. */
  2291. wpa_supplicant_select_network(wpa_s, ssid);
  2292. return 0;
  2293. }
  2294. static int wpa_supplicant_ctrl_iface_mesh_group_remove(
  2295. struct wpa_supplicant *wpa_s, char *cmd)
  2296. {
  2297. struct wpa_supplicant *orig;
  2298. struct wpa_global *global;
  2299. int found = 0;
  2300. wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
  2301. global = wpa_s->global;
  2302. orig = wpa_s;
  2303. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2304. if (os_strcmp(wpa_s->ifname, cmd) == 0) {
  2305. found = 1;
  2306. break;
  2307. }
  2308. }
  2309. if (!found) {
  2310. wpa_printf(MSG_ERROR,
  2311. "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
  2312. cmd);
  2313. return -1;
  2314. }
  2315. if (wpa_s->mesh_if_created && wpa_s == orig) {
  2316. wpa_printf(MSG_ERROR,
  2317. "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
  2318. return -1;
  2319. }
  2320. wpa_s->reassociate = 0;
  2321. wpa_s->disconnected = 1;
  2322. wpa_supplicant_cancel_sched_scan(wpa_s);
  2323. wpa_supplicant_cancel_scan(wpa_s);
  2324. /*
  2325. * TODO: If necessary write our own group_remove function,
  2326. * for now we can reuse deauthenticate
  2327. */
  2328. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2329. if (wpa_s->mesh_if_created)
  2330. wpa_supplicant_remove_iface(global, wpa_s, 0);
  2331. return 0;
  2332. }
  2333. #endif /* CONFIG_MESH */
  2334. static int wpa_supplicant_ctrl_iface_select_network(
  2335. struct wpa_supplicant *wpa_s, char *cmd)
  2336. {
  2337. int id;
  2338. struct wpa_ssid *ssid;
  2339. char *pos;
  2340. /* cmd: "<network id>" or "any" */
  2341. if (os_strncmp(cmd, "any", 3) == 0) {
  2342. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  2343. ssid = NULL;
  2344. } else {
  2345. id = atoi(cmd);
  2346. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  2347. ssid = wpa_config_get_network(wpa_s->conf, id);
  2348. if (ssid == NULL) {
  2349. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2350. "network id=%d", id);
  2351. return -1;
  2352. }
  2353. if (ssid->disabled == 2) {
  2354. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2355. "SELECT_NETWORK with persistent P2P group");
  2356. return -1;
  2357. }
  2358. }
  2359. pos = os_strstr(cmd, " freq=");
  2360. if (pos) {
  2361. int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
  2362. if (freqs) {
  2363. wpa_s->scan_req = MANUAL_SCAN_REQ;
  2364. os_free(wpa_s->manual_scan_freqs);
  2365. wpa_s->manual_scan_freqs = freqs;
  2366. }
  2367. }
  2368. wpa_s->scan_min_time.sec = 0;
  2369. wpa_s->scan_min_time.usec = 0;
  2370. wpa_supplicant_select_network(wpa_s, ssid);
  2371. return 0;
  2372. }
  2373. static int wpa_supplicant_ctrl_iface_enable_network(
  2374. struct wpa_supplicant *wpa_s, char *cmd)
  2375. {
  2376. int id;
  2377. struct wpa_ssid *ssid;
  2378. /* cmd: "<network id>" or "all" */
  2379. if (os_strcmp(cmd, "all") == 0) {
  2380. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  2381. ssid = NULL;
  2382. } else {
  2383. id = atoi(cmd);
  2384. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  2385. ssid = wpa_config_get_network(wpa_s->conf, id);
  2386. if (ssid == NULL) {
  2387. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2388. "network id=%d", id);
  2389. return -1;
  2390. }
  2391. if (ssid->disabled == 2) {
  2392. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2393. "ENABLE_NETWORK with persistent P2P group");
  2394. return -1;
  2395. }
  2396. if (os_strstr(cmd, " no-connect")) {
  2397. ssid->disabled = 0;
  2398. return 0;
  2399. }
  2400. }
  2401. wpa_s->scan_min_time.sec = 0;
  2402. wpa_s->scan_min_time.usec = 0;
  2403. wpa_supplicant_enable_network(wpa_s, ssid);
  2404. return 0;
  2405. }
  2406. static int wpa_supplicant_ctrl_iface_disable_network(
  2407. struct wpa_supplicant *wpa_s, char *cmd)
  2408. {
  2409. int id;
  2410. struct wpa_ssid *ssid;
  2411. /* cmd: "<network id>" or "all" */
  2412. if (os_strcmp(cmd, "all") == 0) {
  2413. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  2414. ssid = NULL;
  2415. } else {
  2416. id = atoi(cmd);
  2417. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  2418. ssid = wpa_config_get_network(wpa_s->conf, id);
  2419. if (ssid == NULL) {
  2420. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2421. "network id=%d", id);
  2422. return -1;
  2423. }
  2424. if (ssid->disabled == 2) {
  2425. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2426. "DISABLE_NETWORK with persistent P2P "
  2427. "group");
  2428. return -1;
  2429. }
  2430. }
  2431. wpa_supplicant_disable_network(wpa_s, ssid);
  2432. return 0;
  2433. }
  2434. static int wpa_supplicant_ctrl_iface_add_network(
  2435. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2436. {
  2437. struct wpa_ssid *ssid;
  2438. int ret;
  2439. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  2440. ssid = wpa_config_add_network(wpa_s->conf);
  2441. if (ssid == NULL)
  2442. return -1;
  2443. wpas_notify_network_added(wpa_s, ssid);
  2444. ssid->disabled = 1;
  2445. wpa_config_set_network_defaults(ssid);
  2446. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  2447. if (os_snprintf_error(buflen, ret))
  2448. return -1;
  2449. return ret;
  2450. }
  2451. static int wpa_supplicant_ctrl_iface_remove_network(
  2452. struct wpa_supplicant *wpa_s, char *cmd)
  2453. {
  2454. int id;
  2455. struct wpa_ssid *ssid;
  2456. int was_disabled;
  2457. /* cmd: "<network id>" or "all" */
  2458. if (os_strcmp(cmd, "all") == 0) {
  2459. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  2460. if (wpa_s->sched_scanning)
  2461. wpa_supplicant_cancel_sched_scan(wpa_s);
  2462. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2463. if (wpa_s->current_ssid) {
  2464. #ifdef CONFIG_SME
  2465. wpa_s->sme.prev_bssid_set = 0;
  2466. #endif /* CONFIG_SME */
  2467. wpa_sm_set_config(wpa_s->wpa, NULL);
  2468. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2469. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2470. wpa_s->own_disconnect_req = 1;
  2471. wpa_supplicant_deauthenticate(
  2472. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2473. }
  2474. ssid = wpa_s->conf->ssid;
  2475. while (ssid) {
  2476. struct wpa_ssid *remove_ssid = ssid;
  2477. id = ssid->id;
  2478. ssid = ssid->next;
  2479. if (wpa_s->last_ssid == remove_ssid)
  2480. wpa_s->last_ssid = NULL;
  2481. wpas_notify_network_removed(wpa_s, remove_ssid);
  2482. wpa_config_remove_network(wpa_s->conf, id);
  2483. }
  2484. return 0;
  2485. }
  2486. id = atoi(cmd);
  2487. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  2488. ssid = wpa_config_get_network(wpa_s->conf, id);
  2489. if (ssid)
  2490. wpas_notify_network_removed(wpa_s, ssid);
  2491. if (ssid == NULL) {
  2492. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2493. "id=%d", id);
  2494. return -1;
  2495. }
  2496. if (wpa_s->last_ssid == ssid)
  2497. wpa_s->last_ssid = NULL;
  2498. if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
  2499. #ifdef CONFIG_SME
  2500. wpa_s->sme.prev_bssid_set = 0;
  2501. #endif /* CONFIG_SME */
  2502. /*
  2503. * Invalidate the EAP session cache if the current or
  2504. * previously used network is removed.
  2505. */
  2506. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2507. }
  2508. if (ssid == wpa_s->current_ssid) {
  2509. wpa_sm_set_config(wpa_s->wpa, NULL);
  2510. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2511. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2512. wpa_s->own_disconnect_req = 1;
  2513. wpa_supplicant_deauthenticate(wpa_s,
  2514. WLAN_REASON_DEAUTH_LEAVING);
  2515. }
  2516. was_disabled = ssid->disabled;
  2517. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  2518. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  2519. "network id=%d", id);
  2520. return -1;
  2521. }
  2522. if (!was_disabled && wpa_s->sched_scanning) {
  2523. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
  2524. "network from filters");
  2525. wpa_supplicant_cancel_sched_scan(wpa_s);
  2526. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2527. }
  2528. return 0;
  2529. }
  2530. static int wpa_supplicant_ctrl_iface_update_network(
  2531. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2532. char *name, char *value)
  2533. {
  2534. if (wpa_config_set(ssid, name, value, 0) < 0) {
  2535. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  2536. "variable '%s'", name);
  2537. return -1;
  2538. }
  2539. if (os_strcmp(name, "bssid") != 0 &&
  2540. os_strcmp(name, "priority") != 0)
  2541. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  2542. if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
  2543. /*
  2544. * Invalidate the EAP session cache if anything in the current
  2545. * or previously used configuration changes.
  2546. */
  2547. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2548. }
  2549. if ((os_strcmp(name, "psk") == 0 &&
  2550. value[0] == '"' && ssid->ssid_len) ||
  2551. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  2552. wpa_config_update_psk(ssid);
  2553. else if (os_strcmp(name, "priority") == 0)
  2554. wpa_config_update_prio_list(wpa_s->conf);
  2555. return 0;
  2556. }
  2557. static int wpa_supplicant_ctrl_iface_set_network(
  2558. struct wpa_supplicant *wpa_s, char *cmd)
  2559. {
  2560. int id, ret, prev_bssid_set, prev_disabled;
  2561. struct wpa_ssid *ssid;
  2562. char *name, *value;
  2563. u8 prev_bssid[ETH_ALEN];
  2564. /* cmd: "<network id> <variable name> <value>" */
  2565. name = os_strchr(cmd, ' ');
  2566. if (name == NULL)
  2567. return -1;
  2568. *name++ = '\0';
  2569. value = os_strchr(name, ' ');
  2570. if (value == NULL)
  2571. return -1;
  2572. *value++ = '\0';
  2573. id = atoi(cmd);
  2574. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  2575. id, name);
  2576. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2577. (u8 *) value, os_strlen(value));
  2578. ssid = wpa_config_get_network(wpa_s->conf, id);
  2579. if (ssid == NULL) {
  2580. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2581. "id=%d", id);
  2582. return -1;
  2583. }
  2584. prev_bssid_set = ssid->bssid_set;
  2585. prev_disabled = ssid->disabled;
  2586. os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
  2587. ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
  2588. value);
  2589. if (ret == 0 &&
  2590. (ssid->bssid_set != prev_bssid_set ||
  2591. os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
  2592. wpas_notify_network_bssid_set_changed(wpa_s, ssid);
  2593. if (prev_disabled != ssid->disabled &&
  2594. (prev_disabled == 2 || ssid->disabled == 2))
  2595. wpas_notify_network_type_changed(wpa_s, ssid);
  2596. return ret;
  2597. }
  2598. static int wpa_supplicant_ctrl_iface_get_network(
  2599. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2600. {
  2601. int id;
  2602. size_t res;
  2603. struct wpa_ssid *ssid;
  2604. char *name, *value;
  2605. /* cmd: "<network id> <variable name>" */
  2606. name = os_strchr(cmd, ' ');
  2607. if (name == NULL || buflen == 0)
  2608. return -1;
  2609. *name++ = '\0';
  2610. id = atoi(cmd);
  2611. wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2612. id, name);
  2613. ssid = wpa_config_get_network(wpa_s->conf, id);
  2614. if (ssid == NULL) {
  2615. wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Could not find network "
  2616. "id=%d", id);
  2617. return -1;
  2618. }
  2619. value = wpa_config_get_no_key(ssid, name);
  2620. if (value == NULL) {
  2621. wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Failed to get network "
  2622. "variable '%s'", name);
  2623. return -1;
  2624. }
  2625. res = os_strlcpy(buf, value, buflen);
  2626. if (res >= buflen) {
  2627. os_free(value);
  2628. return -1;
  2629. }
  2630. os_free(value);
  2631. return res;
  2632. }
  2633. static int wpa_supplicant_ctrl_iface_dup_network(
  2634. struct wpa_supplicant *wpa_s, char *cmd,
  2635. struct wpa_supplicant *dst_wpa_s)
  2636. {
  2637. struct wpa_ssid *ssid_s, *ssid_d;
  2638. char *name, *id, *value;
  2639. int id_s, id_d, ret;
  2640. /* cmd: "<src network id> <dst network id> <variable name>" */
  2641. id = os_strchr(cmd, ' ');
  2642. if (id == NULL)
  2643. return -1;
  2644. *id++ = '\0';
  2645. name = os_strchr(id, ' ');
  2646. if (name == NULL)
  2647. return -1;
  2648. *name++ = '\0';
  2649. id_s = atoi(cmd);
  2650. id_d = atoi(id);
  2651. wpa_printf(MSG_DEBUG,
  2652. "CTRL_IFACE: DUP_NETWORK ifname=%s->%s id=%d->%d name='%s'",
  2653. wpa_s->ifname, dst_wpa_s->ifname, id_s, id_d, name);
  2654. ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
  2655. if (ssid_s == NULL) {
  2656. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2657. "network id=%d", id_s);
  2658. return -1;
  2659. }
  2660. ssid_d = wpa_config_get_network(dst_wpa_s->conf, id_d);
  2661. if (ssid_d == NULL) {
  2662. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2663. "network id=%d", id_d);
  2664. return -1;
  2665. }
  2666. value = wpa_config_get(ssid_s, name);
  2667. if (value == NULL) {
  2668. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2669. "variable '%s'", name);
  2670. return -1;
  2671. }
  2672. ret = wpa_supplicant_ctrl_iface_update_network(dst_wpa_s, ssid_d, name,
  2673. value);
  2674. os_free(value);
  2675. return ret;
  2676. }
  2677. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2678. char *buf, size_t buflen)
  2679. {
  2680. char *pos, *end;
  2681. struct wpa_cred *cred;
  2682. int ret;
  2683. pos = buf;
  2684. end = buf + buflen;
  2685. ret = os_snprintf(pos, end - pos,
  2686. "cred id / realm / username / domain / imsi\n");
  2687. if (os_snprintf_error(end - pos, ret))
  2688. return pos - buf;
  2689. pos += ret;
  2690. cred = wpa_s->conf->cred;
  2691. while (cred) {
  2692. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2693. cred->id, cred->realm ? cred->realm : "",
  2694. cred->username ? cred->username : "",
  2695. cred->domain ? cred->domain[0] : "",
  2696. cred->imsi ? cred->imsi : "");
  2697. if (os_snprintf_error(end - pos, ret))
  2698. return pos - buf;
  2699. pos += ret;
  2700. cred = cred->next;
  2701. }
  2702. return pos - buf;
  2703. }
  2704. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2705. char *buf, size_t buflen)
  2706. {
  2707. struct wpa_cred *cred;
  2708. int ret;
  2709. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2710. cred = wpa_config_add_cred(wpa_s->conf);
  2711. if (cred == NULL)
  2712. return -1;
  2713. wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
  2714. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2715. if (os_snprintf_error(buflen, ret))
  2716. return -1;
  2717. return ret;
  2718. }
  2719. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2720. struct wpa_cred *cred)
  2721. {
  2722. struct wpa_ssid *ssid;
  2723. char str[20];
  2724. int id;
  2725. if (cred == NULL) {
  2726. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2727. return -1;
  2728. }
  2729. id = cred->id;
  2730. if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
  2731. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2732. return -1;
  2733. }
  2734. wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
  2735. /* Remove any network entry created based on the removed credential */
  2736. ssid = wpa_s->conf->ssid;
  2737. while (ssid) {
  2738. if (ssid->parent_cred == cred) {
  2739. int res;
  2740. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2741. "used the removed credential", ssid->id);
  2742. res = os_snprintf(str, sizeof(str), "%d", ssid->id);
  2743. if (os_snprintf_error(sizeof(str), res))
  2744. str[sizeof(str) - 1] = '\0';
  2745. ssid = ssid->next;
  2746. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2747. } else
  2748. ssid = ssid->next;
  2749. }
  2750. return 0;
  2751. }
  2752. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2753. char *cmd)
  2754. {
  2755. int id;
  2756. struct wpa_cred *cred, *prev;
  2757. /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
  2758. * "provisioning_sp=<FQDN> */
  2759. if (os_strcmp(cmd, "all") == 0) {
  2760. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2761. cred = wpa_s->conf->cred;
  2762. while (cred) {
  2763. prev = cred;
  2764. cred = cred->next;
  2765. wpas_ctrl_remove_cred(wpa_s, prev);
  2766. }
  2767. return 0;
  2768. }
  2769. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2770. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2771. cmd + 8);
  2772. cred = wpa_s->conf->cred;
  2773. while (cred) {
  2774. prev = cred;
  2775. cred = cred->next;
  2776. if (prev->domain) {
  2777. size_t i;
  2778. for (i = 0; i < prev->num_domain; i++) {
  2779. if (os_strcmp(prev->domain[i], cmd + 8)
  2780. != 0)
  2781. continue;
  2782. wpas_ctrl_remove_cred(wpa_s, prev);
  2783. break;
  2784. }
  2785. }
  2786. }
  2787. return 0;
  2788. }
  2789. if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
  2790. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
  2791. cmd + 16);
  2792. cred = wpa_s->conf->cred;
  2793. while (cred) {
  2794. prev = cred;
  2795. cred = cred->next;
  2796. if (prev->provisioning_sp &&
  2797. os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
  2798. wpas_ctrl_remove_cred(wpa_s, prev);
  2799. }
  2800. return 0;
  2801. }
  2802. id = atoi(cmd);
  2803. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2804. cred = wpa_config_get_cred(wpa_s->conf, id);
  2805. return wpas_ctrl_remove_cred(wpa_s, cred);
  2806. }
  2807. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2808. char *cmd)
  2809. {
  2810. int id;
  2811. struct wpa_cred *cred;
  2812. char *name, *value;
  2813. /* cmd: "<cred id> <variable name> <value>" */
  2814. name = os_strchr(cmd, ' ');
  2815. if (name == NULL)
  2816. return -1;
  2817. *name++ = '\0';
  2818. value = os_strchr(name, ' ');
  2819. if (value == NULL)
  2820. return -1;
  2821. *value++ = '\0';
  2822. id = atoi(cmd);
  2823. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2824. id, name);
  2825. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2826. (u8 *) value, os_strlen(value));
  2827. cred = wpa_config_get_cred(wpa_s->conf, id);
  2828. if (cred == NULL) {
  2829. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2830. id);
  2831. return -1;
  2832. }
  2833. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2834. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2835. "variable '%s'", name);
  2836. return -1;
  2837. }
  2838. wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
  2839. return 0;
  2840. }
  2841. static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
  2842. char *cmd, char *buf,
  2843. size_t buflen)
  2844. {
  2845. int id;
  2846. size_t res;
  2847. struct wpa_cred *cred;
  2848. char *name, *value;
  2849. /* cmd: "<cred id> <variable name>" */
  2850. name = os_strchr(cmd, ' ');
  2851. if (name == NULL)
  2852. return -1;
  2853. *name++ = '\0';
  2854. id = atoi(cmd);
  2855. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
  2856. id, name);
  2857. cred = wpa_config_get_cred(wpa_s->conf, id);
  2858. if (cred == NULL) {
  2859. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2860. id);
  2861. return -1;
  2862. }
  2863. value = wpa_config_get_cred_no_key(cred, name);
  2864. if (value == NULL) {
  2865. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
  2866. name);
  2867. return -1;
  2868. }
  2869. res = os_strlcpy(buf, value, buflen);
  2870. if (res >= buflen) {
  2871. os_free(value);
  2872. return -1;
  2873. }
  2874. os_free(value);
  2875. return res;
  2876. }
  2877. #ifndef CONFIG_NO_CONFIG_WRITE
  2878. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2879. {
  2880. int ret;
  2881. if (!wpa_s->conf->update_config) {
  2882. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2883. "to update configuration (update_config=0)");
  2884. return -1;
  2885. }
  2886. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2887. if (ret) {
  2888. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2889. "update configuration");
  2890. } else {
  2891. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2892. " updated");
  2893. }
  2894. return ret;
  2895. }
  2896. #endif /* CONFIG_NO_CONFIG_WRITE */
  2897. struct cipher_info {
  2898. unsigned int capa;
  2899. const char *name;
  2900. int group_only;
  2901. };
  2902. static const struct cipher_info ciphers[] = {
  2903. { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
  2904. { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
  2905. { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
  2906. { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
  2907. { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
  2908. { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
  2909. { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
  2910. { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
  2911. };
  2912. static const struct cipher_info ciphers_group_mgmt[] = {
  2913. { WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
  2914. { WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
  2915. { WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
  2916. { WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
  2917. };
  2918. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2919. struct wpa_driver_capa *capa,
  2920. char *buf, size_t buflen)
  2921. {
  2922. int ret;
  2923. char *pos, *end;
  2924. size_t len;
  2925. unsigned int i;
  2926. pos = buf;
  2927. end = pos + buflen;
  2928. if (res < 0) {
  2929. if (strict)
  2930. return 0;
  2931. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2932. if (len >= buflen)
  2933. return -1;
  2934. return len;
  2935. }
  2936. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2937. if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
  2938. ret = os_snprintf(pos, end - pos, "%s%s",
  2939. pos == buf ? "" : " ",
  2940. ciphers[i].name);
  2941. if (os_snprintf_error(end - pos, ret))
  2942. return pos - buf;
  2943. pos += ret;
  2944. }
  2945. }
  2946. return pos - buf;
  2947. }
  2948. static int ctrl_iface_get_capability_group(int res, char *strict,
  2949. struct wpa_driver_capa *capa,
  2950. char *buf, size_t buflen)
  2951. {
  2952. int ret;
  2953. char *pos, *end;
  2954. size_t len;
  2955. unsigned int i;
  2956. pos = buf;
  2957. end = pos + buflen;
  2958. if (res < 0) {
  2959. if (strict)
  2960. return 0;
  2961. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2962. if (len >= buflen)
  2963. return -1;
  2964. return len;
  2965. }
  2966. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2967. if (capa->enc & ciphers[i].capa) {
  2968. ret = os_snprintf(pos, end - pos, "%s%s",
  2969. pos == buf ? "" : " ",
  2970. ciphers[i].name);
  2971. if (os_snprintf_error(end - pos, ret))
  2972. return pos - buf;
  2973. pos += ret;
  2974. }
  2975. }
  2976. return pos - buf;
  2977. }
  2978. static int ctrl_iface_get_capability_group_mgmt(int res, char *strict,
  2979. struct wpa_driver_capa *capa,
  2980. char *buf, size_t buflen)
  2981. {
  2982. int ret;
  2983. char *pos, *end;
  2984. unsigned int i;
  2985. pos = buf;
  2986. end = pos + buflen;
  2987. if (res < 0)
  2988. return 0;
  2989. for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
  2990. if (capa->enc & ciphers_group_mgmt[i].capa) {
  2991. ret = os_snprintf(pos, end - pos, "%s%s",
  2992. pos == buf ? "" : " ",
  2993. ciphers_group_mgmt[i].name);
  2994. if (os_snprintf_error(end - pos, ret))
  2995. return pos - buf;
  2996. pos += ret;
  2997. }
  2998. }
  2999. return pos - buf;
  3000. }
  3001. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  3002. struct wpa_driver_capa *capa,
  3003. char *buf, size_t buflen)
  3004. {
  3005. int ret;
  3006. char *pos, *end;
  3007. size_t len;
  3008. pos = buf;
  3009. end = pos + buflen;
  3010. if (res < 0) {
  3011. if (strict)
  3012. return 0;
  3013. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  3014. "NONE", buflen);
  3015. if (len >= buflen)
  3016. return -1;
  3017. return len;
  3018. }
  3019. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  3020. if (os_snprintf_error(end - pos, ret))
  3021. return pos - buf;
  3022. pos += ret;
  3023. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  3024. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  3025. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  3026. if (os_snprintf_error(end - pos, ret))
  3027. return pos - buf;
  3028. pos += ret;
  3029. }
  3030. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  3031. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  3032. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  3033. if (os_snprintf_error(end - pos, ret))
  3034. return pos - buf;
  3035. pos += ret;
  3036. }
  3037. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  3038. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  3039. if (os_snprintf_error(end - pos, ret))
  3040. return pos - buf;
  3041. pos += ret;
  3042. }
  3043. #ifdef CONFIG_SUITEB
  3044. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
  3045. ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
  3046. if (os_snprintf_error(end - pos, ret))
  3047. return pos - buf;
  3048. pos += ret;
  3049. }
  3050. #endif /* CONFIG_SUITEB */
  3051. #ifdef CONFIG_SUITEB192
  3052. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
  3053. ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
  3054. if (os_snprintf_error(end - pos, ret))
  3055. return pos - buf;
  3056. pos += ret;
  3057. }
  3058. #endif /* CONFIG_SUITEB192 */
  3059. return pos - buf;
  3060. }
  3061. static int ctrl_iface_get_capability_proto(int res, char *strict,
  3062. struct wpa_driver_capa *capa,
  3063. char *buf, size_t buflen)
  3064. {
  3065. int ret;
  3066. char *pos, *end;
  3067. size_t len;
  3068. pos = buf;
  3069. end = pos + buflen;
  3070. if (res < 0) {
  3071. if (strict)
  3072. return 0;
  3073. len = os_strlcpy(buf, "RSN WPA", buflen);
  3074. if (len >= buflen)
  3075. return -1;
  3076. return len;
  3077. }
  3078. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  3079. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  3080. ret = os_snprintf(pos, end - pos, "%sRSN",
  3081. pos == buf ? "" : " ");
  3082. if (os_snprintf_error(end - pos, ret))
  3083. return pos - buf;
  3084. pos += ret;
  3085. }
  3086. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  3087. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  3088. ret = os_snprintf(pos, end - pos, "%sWPA",
  3089. pos == buf ? "" : " ");
  3090. if (os_snprintf_error(end - pos, ret))
  3091. return pos - buf;
  3092. pos += ret;
  3093. }
  3094. return pos - buf;
  3095. }
  3096. static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
  3097. int res, char *strict,
  3098. struct wpa_driver_capa *capa,
  3099. char *buf, size_t buflen)
  3100. {
  3101. int ret;
  3102. char *pos, *end;
  3103. size_t len;
  3104. pos = buf;
  3105. end = pos + buflen;
  3106. if (res < 0) {
  3107. if (strict)
  3108. return 0;
  3109. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  3110. if (len >= buflen)
  3111. return -1;
  3112. return len;
  3113. }
  3114. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  3115. ret = os_snprintf(pos, end - pos, "%sOPEN",
  3116. pos == buf ? "" : " ");
  3117. if (os_snprintf_error(end - pos, ret))
  3118. return pos - buf;
  3119. pos += ret;
  3120. }
  3121. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  3122. ret = os_snprintf(pos, end - pos, "%sSHARED",
  3123. pos == buf ? "" : " ");
  3124. if (os_snprintf_error(end - pos, ret))
  3125. return pos - buf;
  3126. pos += ret;
  3127. }
  3128. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  3129. ret = os_snprintf(pos, end - pos, "%sLEAP",
  3130. pos == buf ? "" : " ");
  3131. if (os_snprintf_error(end - pos, ret))
  3132. return pos - buf;
  3133. pos += ret;
  3134. }
  3135. #ifdef CONFIG_SAE
  3136. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
  3137. ret = os_snprintf(pos, end - pos, "%sSAE",
  3138. pos == buf ? "" : " ");
  3139. if (os_snprintf_error(end - pos, ret))
  3140. return pos - buf;
  3141. pos += ret;
  3142. }
  3143. #endif /* CONFIG_SAE */
  3144. return pos - buf;
  3145. }
  3146. static int ctrl_iface_get_capability_modes(int res, char *strict,
  3147. struct wpa_driver_capa *capa,
  3148. char *buf, size_t buflen)
  3149. {
  3150. int ret;
  3151. char *pos, *end;
  3152. size_t len;
  3153. pos = buf;
  3154. end = pos + buflen;
  3155. if (res < 0) {
  3156. if (strict)
  3157. return 0;
  3158. len = os_strlcpy(buf, "IBSS AP", buflen);
  3159. if (len >= buflen)
  3160. return -1;
  3161. return len;
  3162. }
  3163. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  3164. ret = os_snprintf(pos, end - pos, "%sIBSS",
  3165. pos == buf ? "" : " ");
  3166. if (os_snprintf_error(end - pos, ret))
  3167. return pos - buf;
  3168. pos += ret;
  3169. }
  3170. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  3171. ret = os_snprintf(pos, end - pos, "%sAP",
  3172. pos == buf ? "" : " ");
  3173. if (os_snprintf_error(end - pos, ret))
  3174. return pos - buf;
  3175. pos += ret;
  3176. }
  3177. #ifdef CONFIG_MESH
  3178. if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
  3179. ret = os_snprintf(pos, end - pos, "%sMESH",
  3180. pos == buf ? "" : " ");
  3181. if (os_snprintf_error(end - pos, ret))
  3182. return pos - buf;
  3183. pos += ret;
  3184. }
  3185. #endif /* CONFIG_MESH */
  3186. return pos - buf;
  3187. }
  3188. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  3189. char *buf, size_t buflen)
  3190. {
  3191. struct hostapd_channel_data *chnl;
  3192. int ret, i, j;
  3193. char *pos, *end, *hmode;
  3194. pos = buf;
  3195. end = pos + buflen;
  3196. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  3197. switch (wpa_s->hw.modes[j].mode) {
  3198. case HOSTAPD_MODE_IEEE80211B:
  3199. hmode = "B";
  3200. break;
  3201. case HOSTAPD_MODE_IEEE80211G:
  3202. hmode = "G";
  3203. break;
  3204. case HOSTAPD_MODE_IEEE80211A:
  3205. hmode = "A";
  3206. break;
  3207. case HOSTAPD_MODE_IEEE80211AD:
  3208. hmode = "AD";
  3209. break;
  3210. default:
  3211. continue;
  3212. }
  3213. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  3214. if (os_snprintf_error(end - pos, ret))
  3215. return pos - buf;
  3216. pos += ret;
  3217. chnl = wpa_s->hw.modes[j].channels;
  3218. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  3219. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  3220. continue;
  3221. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  3222. if (os_snprintf_error(end - pos, ret))
  3223. return pos - buf;
  3224. pos += ret;
  3225. }
  3226. ret = os_snprintf(pos, end - pos, "\n");
  3227. if (os_snprintf_error(end - pos, ret))
  3228. return pos - buf;
  3229. pos += ret;
  3230. }
  3231. return pos - buf;
  3232. }
  3233. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  3234. char *buf, size_t buflen)
  3235. {
  3236. struct hostapd_channel_data *chnl;
  3237. int ret, i, j;
  3238. char *pos, *end, *hmode;
  3239. pos = buf;
  3240. end = pos + buflen;
  3241. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  3242. switch (wpa_s->hw.modes[j].mode) {
  3243. case HOSTAPD_MODE_IEEE80211B:
  3244. hmode = "B";
  3245. break;
  3246. case HOSTAPD_MODE_IEEE80211G:
  3247. hmode = "G";
  3248. break;
  3249. case HOSTAPD_MODE_IEEE80211A:
  3250. hmode = "A";
  3251. break;
  3252. case HOSTAPD_MODE_IEEE80211AD:
  3253. hmode = "AD";
  3254. break;
  3255. default:
  3256. continue;
  3257. }
  3258. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  3259. hmode);
  3260. if (os_snprintf_error(end - pos, ret))
  3261. return pos - buf;
  3262. pos += ret;
  3263. chnl = wpa_s->hw.modes[j].channels;
  3264. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  3265. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  3266. continue;
  3267. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
  3268. chnl[i].chan, chnl[i].freq,
  3269. chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
  3270. " (NO_IR)" : "",
  3271. chnl[i].flag & HOSTAPD_CHAN_RADAR ?
  3272. " (DFS)" : "");
  3273. if (os_snprintf_error(end - pos, ret))
  3274. return pos - buf;
  3275. pos += ret;
  3276. }
  3277. ret = os_snprintf(pos, end - pos, "\n");
  3278. if (os_snprintf_error(end - pos, ret))
  3279. return pos - buf;
  3280. pos += ret;
  3281. }
  3282. return pos - buf;
  3283. }
  3284. static int wpa_supplicant_ctrl_iface_get_capability(
  3285. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  3286. size_t buflen)
  3287. {
  3288. struct wpa_driver_capa capa;
  3289. int res;
  3290. char *strict;
  3291. char field[30];
  3292. size_t len;
  3293. /* Determine whether or not strict checking was requested */
  3294. len = os_strlcpy(field, _field, sizeof(field));
  3295. if (len >= sizeof(field))
  3296. return -1;
  3297. strict = os_strchr(field, ' ');
  3298. if (strict != NULL) {
  3299. *strict++ = '\0';
  3300. if (os_strcmp(strict, "strict") != 0)
  3301. return -1;
  3302. }
  3303. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  3304. field, strict ? strict : "");
  3305. if (os_strcmp(field, "eap") == 0) {
  3306. return eap_get_names(buf, buflen);
  3307. }
  3308. res = wpa_drv_get_capa(wpa_s, &capa);
  3309. if (os_strcmp(field, "pairwise") == 0)
  3310. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  3311. buf, buflen);
  3312. if (os_strcmp(field, "group") == 0)
  3313. return ctrl_iface_get_capability_group(res, strict, &capa,
  3314. buf, buflen);
  3315. if (os_strcmp(field, "group_mgmt") == 0)
  3316. return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
  3317. buf, buflen);
  3318. if (os_strcmp(field, "key_mgmt") == 0)
  3319. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  3320. buf, buflen);
  3321. if (os_strcmp(field, "proto") == 0)
  3322. return ctrl_iface_get_capability_proto(res, strict, &capa,
  3323. buf, buflen);
  3324. if (os_strcmp(field, "auth_alg") == 0)
  3325. return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
  3326. &capa, buf, buflen);
  3327. if (os_strcmp(field, "modes") == 0)
  3328. return ctrl_iface_get_capability_modes(res, strict, &capa,
  3329. buf, buflen);
  3330. if (os_strcmp(field, "channels") == 0)
  3331. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  3332. if (os_strcmp(field, "freq") == 0)
  3333. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  3334. #ifdef CONFIG_TDLS
  3335. if (os_strcmp(field, "tdls") == 0)
  3336. return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
  3337. #endif /* CONFIG_TDLS */
  3338. #ifdef CONFIG_ERP
  3339. if (os_strcmp(field, "erp") == 0) {
  3340. res = os_snprintf(buf, buflen, "ERP");
  3341. if (os_snprintf_error(buflen, res))
  3342. return -1;
  3343. return res;
  3344. }
  3345. #endif /* CONFIG_EPR */
  3346. #ifdef CONFIG_FIPS
  3347. if (os_strcmp(field, "fips") == 0) {
  3348. res = os_snprintf(buf, buflen, "FIPS");
  3349. if (os_snprintf_error(buflen, res))
  3350. return -1;
  3351. return res;
  3352. }
  3353. #endif /* CONFIG_FIPS */
  3354. #ifdef CONFIG_ACS
  3355. if (os_strcmp(field, "acs") == 0) {
  3356. res = os_snprintf(buf, buflen, "ACS");
  3357. if (os_snprintf_error(buflen, res))
  3358. return -1;
  3359. return res;
  3360. }
  3361. #endif /* CONFIG_ACS */
  3362. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  3363. field);
  3364. return -1;
  3365. }
  3366. #ifdef CONFIG_INTERWORKING
  3367. static char * anqp_add_hex(char *pos, char *end, const char *title,
  3368. struct wpabuf *data)
  3369. {
  3370. char *start = pos;
  3371. size_t i;
  3372. int ret;
  3373. const u8 *d;
  3374. if (data == NULL)
  3375. return start;
  3376. ret = os_snprintf(pos, end - pos, "%s=", title);
  3377. if (os_snprintf_error(end - pos, ret))
  3378. return start;
  3379. pos += ret;
  3380. d = wpabuf_head_u8(data);
  3381. for (i = 0; i < wpabuf_len(data); i++) {
  3382. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  3383. if (os_snprintf_error(end - pos, ret))
  3384. return start;
  3385. pos += ret;
  3386. }
  3387. ret = os_snprintf(pos, end - pos, "\n");
  3388. if (os_snprintf_error(end - pos, ret))
  3389. return start;
  3390. pos += ret;
  3391. return pos;
  3392. }
  3393. #endif /* CONFIG_INTERWORKING */
  3394. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  3395. unsigned long mask, char *buf, size_t buflen)
  3396. {
  3397. size_t i;
  3398. int ret;
  3399. char *pos, *end;
  3400. const u8 *ie, *ie2, *osen_ie;
  3401. pos = buf;
  3402. end = buf + buflen;
  3403. if (mask & WPA_BSS_MASK_ID) {
  3404. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  3405. if (os_snprintf_error(end - pos, ret))
  3406. return 0;
  3407. pos += ret;
  3408. }
  3409. if (mask & WPA_BSS_MASK_BSSID) {
  3410. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  3411. MAC2STR(bss->bssid));
  3412. if (os_snprintf_error(end - pos, ret))
  3413. return 0;
  3414. pos += ret;
  3415. }
  3416. if (mask & WPA_BSS_MASK_FREQ) {
  3417. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  3418. if (os_snprintf_error(end - pos, ret))
  3419. return 0;
  3420. pos += ret;
  3421. }
  3422. if (mask & WPA_BSS_MASK_BEACON_INT) {
  3423. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  3424. bss->beacon_int);
  3425. if (os_snprintf_error(end - pos, ret))
  3426. return 0;
  3427. pos += ret;
  3428. }
  3429. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  3430. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  3431. bss->caps);
  3432. if (os_snprintf_error(end - pos, ret))
  3433. return 0;
  3434. pos += ret;
  3435. }
  3436. if (mask & WPA_BSS_MASK_QUAL) {
  3437. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  3438. if (os_snprintf_error(end - pos, ret))
  3439. return 0;
  3440. pos += ret;
  3441. }
  3442. if (mask & WPA_BSS_MASK_NOISE) {
  3443. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  3444. if (os_snprintf_error(end - pos, ret))
  3445. return 0;
  3446. pos += ret;
  3447. }
  3448. if (mask & WPA_BSS_MASK_LEVEL) {
  3449. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  3450. if (os_snprintf_error(end - pos, ret))
  3451. return 0;
  3452. pos += ret;
  3453. }
  3454. if (mask & WPA_BSS_MASK_TSF) {
  3455. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  3456. (unsigned long long) bss->tsf);
  3457. if (os_snprintf_error(end - pos, ret))
  3458. return 0;
  3459. pos += ret;
  3460. }
  3461. if (mask & WPA_BSS_MASK_AGE) {
  3462. struct os_reltime now;
  3463. os_get_reltime(&now);
  3464. ret = os_snprintf(pos, end - pos, "age=%d\n",
  3465. (int) (now.sec - bss->last_update.sec));
  3466. if (os_snprintf_error(end - pos, ret))
  3467. return 0;
  3468. pos += ret;
  3469. }
  3470. if (mask & WPA_BSS_MASK_IE) {
  3471. ret = os_snprintf(pos, end - pos, "ie=");
  3472. if (os_snprintf_error(end - pos, ret))
  3473. return 0;
  3474. pos += ret;
  3475. ie = (const u8 *) (bss + 1);
  3476. for (i = 0; i < bss->ie_len; i++) {
  3477. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  3478. if (os_snprintf_error(end - pos, ret))
  3479. return 0;
  3480. pos += ret;
  3481. }
  3482. ret = os_snprintf(pos, end - pos, "\n");
  3483. if (os_snprintf_error(end - pos, ret))
  3484. return 0;
  3485. pos += ret;
  3486. }
  3487. if (mask & WPA_BSS_MASK_FLAGS) {
  3488. ret = os_snprintf(pos, end - pos, "flags=");
  3489. if (os_snprintf_error(end - pos, ret))
  3490. return 0;
  3491. pos += ret;
  3492. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  3493. if (ie)
  3494. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  3495. 2 + ie[1]);
  3496. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  3497. if (ie2)
  3498. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  3499. 2 + ie2[1]);
  3500. osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  3501. if (osen_ie)
  3502. pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
  3503. osen_ie, 2 + osen_ie[1]);
  3504. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  3505. if (!ie && !ie2 && !osen_ie &&
  3506. (bss->caps & IEEE80211_CAP_PRIVACY)) {
  3507. ret = os_snprintf(pos, end - pos, "[WEP]");
  3508. if (os_snprintf_error(end - pos, ret))
  3509. return 0;
  3510. pos += ret;
  3511. }
  3512. if (bss_is_dmg(bss)) {
  3513. const char *s;
  3514. ret = os_snprintf(pos, end - pos, "[DMG]");
  3515. if (os_snprintf_error(end - pos, ret))
  3516. return 0;
  3517. pos += ret;
  3518. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  3519. case IEEE80211_CAP_DMG_IBSS:
  3520. s = "[IBSS]";
  3521. break;
  3522. case IEEE80211_CAP_DMG_AP:
  3523. s = "[ESS]";
  3524. break;
  3525. case IEEE80211_CAP_DMG_PBSS:
  3526. s = "[PBSS]";
  3527. break;
  3528. default:
  3529. s = "";
  3530. break;
  3531. }
  3532. ret = os_snprintf(pos, end - pos, "%s", s);
  3533. if (os_snprintf_error(end - pos, ret))
  3534. return 0;
  3535. pos += ret;
  3536. } else {
  3537. if (bss->caps & IEEE80211_CAP_IBSS) {
  3538. ret = os_snprintf(pos, end - pos, "[IBSS]");
  3539. if (os_snprintf_error(end - pos, ret))
  3540. return 0;
  3541. pos += ret;
  3542. }
  3543. if (bss->caps & IEEE80211_CAP_ESS) {
  3544. ret = os_snprintf(pos, end - pos, "[ESS]");
  3545. if (os_snprintf_error(end - pos, ret))
  3546. return 0;
  3547. pos += ret;
  3548. }
  3549. }
  3550. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  3551. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  3552. ret = os_snprintf(pos, end - pos, "[P2P]");
  3553. if (os_snprintf_error(end - pos, ret))
  3554. return 0;
  3555. pos += ret;
  3556. }
  3557. #ifdef CONFIG_HS20
  3558. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  3559. ret = os_snprintf(pos, end - pos, "[HS20]");
  3560. if (os_snprintf_error(end - pos, ret))
  3561. return 0;
  3562. pos += ret;
  3563. }
  3564. #endif /* CONFIG_HS20 */
  3565. ret = os_snprintf(pos, end - pos, "\n");
  3566. if (os_snprintf_error(end - pos, ret))
  3567. return 0;
  3568. pos += ret;
  3569. }
  3570. if (mask & WPA_BSS_MASK_SSID) {
  3571. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  3572. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  3573. if (os_snprintf_error(end - pos, ret))
  3574. return 0;
  3575. pos += ret;
  3576. }
  3577. #ifdef CONFIG_WPS
  3578. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  3579. ie = (const u8 *) (bss + 1);
  3580. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  3581. if (ret >= end - pos)
  3582. return 0;
  3583. if (ret > 0)
  3584. pos += ret;
  3585. }
  3586. #endif /* CONFIG_WPS */
  3587. #ifdef CONFIG_P2P
  3588. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  3589. ie = (const u8 *) (bss + 1);
  3590. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  3591. if (ret < 0 || ret >= end - pos)
  3592. return 0;
  3593. pos += ret;
  3594. }
  3595. #endif /* CONFIG_P2P */
  3596. #ifdef CONFIG_WIFI_DISPLAY
  3597. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  3598. struct wpabuf *wfd;
  3599. ie = (const u8 *) (bss + 1);
  3600. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  3601. WFD_IE_VENDOR_TYPE);
  3602. if (wfd) {
  3603. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  3604. if (os_snprintf_error(end - pos, ret)) {
  3605. wpabuf_free(wfd);
  3606. return 0;
  3607. }
  3608. pos += ret;
  3609. pos += wpa_snprintf_hex(pos, end - pos,
  3610. wpabuf_head(wfd),
  3611. wpabuf_len(wfd));
  3612. wpabuf_free(wfd);
  3613. ret = os_snprintf(pos, end - pos, "\n");
  3614. if (os_snprintf_error(end - pos, ret))
  3615. return 0;
  3616. pos += ret;
  3617. }
  3618. }
  3619. #endif /* CONFIG_WIFI_DISPLAY */
  3620. #ifdef CONFIG_INTERWORKING
  3621. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  3622. struct wpa_bss_anqp *anqp = bss->anqp;
  3623. struct wpa_bss_anqp_elem *elem;
  3624. pos = anqp_add_hex(pos, end, "anqp_capability_list",
  3625. anqp->capability_list);
  3626. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  3627. anqp->venue_name);
  3628. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  3629. anqp->network_auth_type);
  3630. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  3631. anqp->roaming_consortium);
  3632. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  3633. anqp->ip_addr_type_availability);
  3634. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  3635. anqp->nai_realm);
  3636. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  3637. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  3638. anqp->domain_name);
  3639. #ifdef CONFIG_HS20
  3640. pos = anqp_add_hex(pos, end, "hs20_capability_list",
  3641. anqp->hs20_capability_list);
  3642. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  3643. anqp->hs20_operator_friendly_name);
  3644. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  3645. anqp->hs20_wan_metrics);
  3646. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  3647. anqp->hs20_connection_capability);
  3648. pos = anqp_add_hex(pos, end, "hs20_operating_class",
  3649. anqp->hs20_operating_class);
  3650. pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
  3651. anqp->hs20_osu_providers_list);
  3652. #endif /* CONFIG_HS20 */
  3653. dl_list_for_each(elem, &anqp->anqp_elems,
  3654. struct wpa_bss_anqp_elem, list) {
  3655. char title[20];
  3656. os_snprintf(title, sizeof(title), "anqp[%u]",
  3657. elem->infoid);
  3658. pos = anqp_add_hex(pos, end, title, elem->payload);
  3659. }
  3660. }
  3661. #endif /* CONFIG_INTERWORKING */
  3662. #ifdef CONFIG_MESH
  3663. if (mask & WPA_BSS_MASK_MESH_SCAN) {
  3664. ie = (const u8 *) (bss + 1);
  3665. ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
  3666. if (ret < 0 || ret >= end - pos)
  3667. return 0;
  3668. pos += ret;
  3669. }
  3670. #endif /* CONFIG_MESH */
  3671. if (mask & WPA_BSS_MASK_SNR) {
  3672. ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
  3673. if (os_snprintf_error(end - pos, ret))
  3674. return 0;
  3675. pos += ret;
  3676. }
  3677. if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
  3678. ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
  3679. bss->est_throughput);
  3680. if (os_snprintf_error(end - pos, ret))
  3681. return 0;
  3682. pos += ret;
  3683. }
  3684. #ifdef CONFIG_FST
  3685. if (mask & WPA_BSS_MASK_FST) {
  3686. ret = fst_ctrl_iface_mb_info(bss->bssid, pos, end - pos);
  3687. if (ret < 0 || ret >= end - pos)
  3688. return 0;
  3689. pos += ret;
  3690. }
  3691. #endif /* CONFIG_FST */
  3692. if (mask & WPA_BSS_MASK_DELIM) {
  3693. ret = os_snprintf(pos, end - pos, "====\n");
  3694. if (os_snprintf_error(end - pos, ret))
  3695. return 0;
  3696. pos += ret;
  3697. }
  3698. return pos - buf;
  3699. }
  3700. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  3701. const char *cmd, char *buf,
  3702. size_t buflen)
  3703. {
  3704. u8 bssid[ETH_ALEN];
  3705. size_t i;
  3706. struct wpa_bss *bss;
  3707. struct wpa_bss *bsslast = NULL;
  3708. struct dl_list *next;
  3709. int ret = 0;
  3710. int len;
  3711. char *ctmp, *end = buf + buflen;
  3712. unsigned long mask = WPA_BSS_MASK_ALL;
  3713. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  3714. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  3715. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  3716. list_id);
  3717. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  3718. list_id);
  3719. } else { /* N1-N2 */
  3720. unsigned int id1, id2;
  3721. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  3722. wpa_printf(MSG_INFO, "Wrong BSS range "
  3723. "format");
  3724. return 0;
  3725. }
  3726. if (*(cmd + 6) == '-')
  3727. id1 = 0;
  3728. else
  3729. id1 = atoi(cmd + 6);
  3730. ctmp++;
  3731. if (*ctmp >= '0' && *ctmp <= '9')
  3732. id2 = atoi(ctmp);
  3733. else
  3734. id2 = (unsigned int) -1;
  3735. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  3736. if (id2 == (unsigned int) -1)
  3737. bsslast = dl_list_last(&wpa_s->bss_id,
  3738. struct wpa_bss,
  3739. list_id);
  3740. else {
  3741. bsslast = wpa_bss_get_id(wpa_s, id2);
  3742. if (bsslast == NULL && bss && id2 > id1) {
  3743. struct wpa_bss *tmp = bss;
  3744. for (;;) {
  3745. next = tmp->list_id.next;
  3746. if (next == &wpa_s->bss_id)
  3747. break;
  3748. tmp = dl_list_entry(
  3749. next, struct wpa_bss,
  3750. list_id);
  3751. if (tmp->id > id2)
  3752. break;
  3753. bsslast = tmp;
  3754. }
  3755. }
  3756. }
  3757. }
  3758. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  3759. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  3760. else if (os_strncmp(cmd, "LAST", 4) == 0)
  3761. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  3762. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  3763. i = atoi(cmd + 3);
  3764. bss = wpa_bss_get_id(wpa_s, i);
  3765. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3766. i = atoi(cmd + 5);
  3767. bss = wpa_bss_get_id(wpa_s, i);
  3768. if (bss) {
  3769. next = bss->list_id.next;
  3770. if (next == &wpa_s->bss_id)
  3771. bss = NULL;
  3772. else
  3773. bss = dl_list_entry(next, struct wpa_bss,
  3774. list_id);
  3775. }
  3776. #ifdef CONFIG_P2P
  3777. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  3778. if (hwaddr_aton(cmd + 13, bssid) == 0)
  3779. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  3780. else
  3781. bss = NULL;
  3782. #endif /* CONFIG_P2P */
  3783. } else if (hwaddr_aton(cmd, bssid) == 0)
  3784. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3785. else {
  3786. struct wpa_bss *tmp;
  3787. i = atoi(cmd);
  3788. bss = NULL;
  3789. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  3790. {
  3791. if (i-- == 0) {
  3792. bss = tmp;
  3793. break;
  3794. }
  3795. }
  3796. }
  3797. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  3798. mask = strtoul(ctmp + 5, NULL, 0x10);
  3799. if (mask == 0)
  3800. mask = WPA_BSS_MASK_ALL;
  3801. }
  3802. if (bss == NULL)
  3803. return 0;
  3804. if (bsslast == NULL)
  3805. bsslast = bss;
  3806. do {
  3807. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  3808. ret += len;
  3809. buf += len;
  3810. buflen -= len;
  3811. if (bss == bsslast) {
  3812. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  3813. (bss == dl_list_last(&wpa_s->bss_id,
  3814. struct wpa_bss, list_id))) {
  3815. int res;
  3816. res = os_snprintf(buf - 5, end - buf + 5,
  3817. "####\n");
  3818. if (os_snprintf_error(end - buf + 5, res)) {
  3819. wpa_printf(MSG_DEBUG,
  3820. "Could not add end delim");
  3821. }
  3822. }
  3823. break;
  3824. }
  3825. next = bss->list_id.next;
  3826. if (next == &wpa_s->bss_id)
  3827. break;
  3828. bss = dl_list_entry(next, struct wpa_bss, list_id);
  3829. } while (bss && len);
  3830. return ret;
  3831. }
  3832. static int wpa_supplicant_ctrl_iface_ap_scan(
  3833. struct wpa_supplicant *wpa_s, char *cmd)
  3834. {
  3835. int ap_scan = atoi(cmd);
  3836. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  3837. }
  3838. static int wpa_supplicant_ctrl_iface_scan_interval(
  3839. struct wpa_supplicant *wpa_s, char *cmd)
  3840. {
  3841. int scan_int = atoi(cmd);
  3842. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  3843. }
  3844. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  3845. struct wpa_supplicant *wpa_s, char *cmd)
  3846. {
  3847. int expire_age = atoi(cmd);
  3848. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  3849. }
  3850. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  3851. struct wpa_supplicant *wpa_s, char *cmd)
  3852. {
  3853. int expire_count = atoi(cmd);
  3854. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  3855. }
  3856. static void wpa_supplicant_ctrl_iface_bss_flush(
  3857. struct wpa_supplicant *wpa_s, char *cmd)
  3858. {
  3859. int flush_age = atoi(cmd);
  3860. if (flush_age == 0)
  3861. wpa_bss_flush(wpa_s);
  3862. else
  3863. wpa_bss_flush_by_age(wpa_s, flush_age);
  3864. }
  3865. #ifdef CONFIG_TESTING_OPTIONS
  3866. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  3867. {
  3868. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  3869. /* MLME-DELETEKEYS.request */
  3870. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  3871. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  3872. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  3873. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  3874. #ifdef CONFIG_IEEE80211W
  3875. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  3876. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  3877. #endif /* CONFIG_IEEE80211W */
  3878. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  3879. 0);
  3880. /* MLME-SETPROTECTION.request(None) */
  3881. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  3882. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  3883. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  3884. wpa_sm_drop_sa(wpa_s->wpa);
  3885. }
  3886. #endif /* CONFIG_TESTING_OPTIONS */
  3887. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  3888. char *addr)
  3889. {
  3890. #ifdef CONFIG_NO_SCAN_PROCESSING
  3891. return -1;
  3892. #else /* CONFIG_NO_SCAN_PROCESSING */
  3893. u8 bssid[ETH_ALEN];
  3894. struct wpa_bss *bss;
  3895. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3896. if (hwaddr_aton(addr, bssid)) {
  3897. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  3898. "address '%s'", addr);
  3899. return -1;
  3900. }
  3901. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  3902. if (!ssid) {
  3903. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  3904. "configuration known for the target AP");
  3905. return -1;
  3906. }
  3907. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  3908. if (!bss) {
  3909. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  3910. "from BSS table");
  3911. return -1;
  3912. }
  3913. /*
  3914. * TODO: Find best network configuration block from configuration to
  3915. * allow roaming to other networks
  3916. */
  3917. wpa_s->reassociate = 1;
  3918. wpa_supplicant_connect(wpa_s, bss, ssid);
  3919. return 0;
  3920. #endif /* CONFIG_NO_SCAN_PROCESSING */
  3921. }
  3922. #ifdef CONFIG_P2P
  3923. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  3924. {
  3925. unsigned int timeout = atoi(cmd);
  3926. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  3927. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  3928. u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
  3929. char *pos;
  3930. unsigned int search_delay;
  3931. const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
  3932. u8 seek_count = 0;
  3933. int freq = 0;
  3934. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  3935. wpa_dbg(wpa_s, MSG_INFO,
  3936. "Reject P2P_FIND since interface is disabled");
  3937. return -1;
  3938. }
  3939. if (os_strstr(cmd, "type=social"))
  3940. type = P2P_FIND_ONLY_SOCIAL;
  3941. else if (os_strstr(cmd, "type=progressive"))
  3942. type = P2P_FIND_PROGRESSIVE;
  3943. pos = os_strstr(cmd, "dev_id=");
  3944. if (pos) {
  3945. pos += 7;
  3946. if (hwaddr_aton(pos, dev_id))
  3947. return -1;
  3948. _dev_id = dev_id;
  3949. }
  3950. pos = os_strstr(cmd, "dev_type=");
  3951. if (pos) {
  3952. pos += 9;
  3953. if (wps_dev_type_str2bin(pos, dev_type) < 0)
  3954. return -1;
  3955. _dev_type = dev_type;
  3956. }
  3957. pos = os_strstr(cmd, "delay=");
  3958. if (pos) {
  3959. pos += 6;
  3960. search_delay = atoi(pos);
  3961. } else
  3962. search_delay = wpas_p2p_search_delay(wpa_s);
  3963. pos = os_strstr(cmd, "freq=");
  3964. if (pos) {
  3965. pos += 5;
  3966. freq = atoi(pos);
  3967. if (freq <= 0)
  3968. return -1;
  3969. }
  3970. /* Must be searched for last, because it adds nul termination */
  3971. pos = os_strstr(cmd, " seek=");
  3972. if (pos)
  3973. pos += 6;
  3974. while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
  3975. char *term;
  3976. _seek[seek_count++] = pos;
  3977. seek = _seek;
  3978. term = os_strchr(pos, ' ');
  3979. if (!term)
  3980. break;
  3981. *term = '\0';
  3982. pos = os_strstr(term + 1, "seek=");
  3983. if (pos)
  3984. pos += 5;
  3985. }
  3986. if (seek_count > P2P_MAX_QUERY_HASH) {
  3987. seek[0] = NULL;
  3988. seek_count = 1;
  3989. }
  3990. return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
  3991. _dev_id, search_delay, seek_count, seek, freq);
  3992. }
  3993. static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
  3994. {
  3995. const char *last = NULL;
  3996. const char *token;
  3997. long int token_len;
  3998. unsigned int i;
  3999. /* Expected predefined CPT names delimited by ':' */
  4000. for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
  4001. if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
  4002. wpa_printf(MSG_ERROR,
  4003. "P2PS: CPT name list is too long, expected up to %d names",
  4004. P2PS_FEATURE_CAPAB_CPT_MAX);
  4005. cpt[0] = 0;
  4006. return -1;
  4007. }
  4008. token_len = last - token;
  4009. if (token_len == 3 &&
  4010. os_memcmp(token, "UDP", token_len) == 0) {
  4011. cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
  4012. } else if (token_len == 3 &&
  4013. os_memcmp(token, "MAC", token_len) == 0) {
  4014. cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
  4015. } else {
  4016. wpa_printf(MSG_ERROR,
  4017. "P2PS: Unsupported CPT name '%s'", token);
  4018. cpt[0] = 0;
  4019. return -1;
  4020. }
  4021. if (isblank((unsigned char) *last)) {
  4022. i++;
  4023. break;
  4024. }
  4025. }
  4026. cpt[i] = 0;
  4027. return 0;
  4028. }
  4029. static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
  4030. {
  4031. struct p2ps_provision *p2ps_prov;
  4032. char *pos;
  4033. size_t info_len = 0;
  4034. char *info = NULL;
  4035. u8 role = P2PS_SETUP_NONE;
  4036. long long unsigned val;
  4037. int i;
  4038. pos = os_strstr(cmd, "info=");
  4039. if (pos) {
  4040. pos += 5;
  4041. info_len = os_strlen(pos);
  4042. if (info_len) {
  4043. info = os_malloc(info_len + 1);
  4044. if (info) {
  4045. info_len = utf8_unescape(pos, info_len,
  4046. info, info_len + 1);
  4047. } else
  4048. info_len = 0;
  4049. }
  4050. }
  4051. p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
  4052. if (p2ps_prov == NULL) {
  4053. os_free(info);
  4054. return NULL;
  4055. }
  4056. if (info) {
  4057. os_memcpy(p2ps_prov->info, info, info_len);
  4058. p2ps_prov->info[info_len] = '\0';
  4059. os_free(info);
  4060. }
  4061. pos = os_strstr(cmd, "status=");
  4062. if (pos)
  4063. p2ps_prov->status = atoi(pos + 7);
  4064. else
  4065. p2ps_prov->status = -1;
  4066. pos = os_strstr(cmd, "adv_id=");
  4067. if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
  4068. goto invalid_args;
  4069. p2ps_prov->adv_id = val;
  4070. pos = os_strstr(cmd, "method=");
  4071. if (pos)
  4072. p2ps_prov->method = strtol(pos + 7, NULL, 16);
  4073. else
  4074. p2ps_prov->method = 0;
  4075. pos = os_strstr(cmd, "session=");
  4076. if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
  4077. goto invalid_args;
  4078. p2ps_prov->session_id = val;
  4079. pos = os_strstr(cmd, "adv_mac=");
  4080. if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
  4081. goto invalid_args;
  4082. pos = os_strstr(cmd, "session_mac=");
  4083. if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
  4084. goto invalid_args;
  4085. pos = os_strstr(cmd, "cpt=");
  4086. if (pos) {
  4087. if (p2ps_ctrl_parse_cpt_priority(pos + 4,
  4088. p2ps_prov->cpt_priority))
  4089. goto invalid_args;
  4090. } else {
  4091. p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
  4092. }
  4093. for (i = 0; p2ps_prov->cpt_priority[i]; i++)
  4094. p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
  4095. /* force conncap with tstCap (no sanity checks) */
  4096. pos = os_strstr(cmd, "tstCap=");
  4097. if (pos) {
  4098. role = strtol(pos + 7, NULL, 16);
  4099. } else {
  4100. pos = os_strstr(cmd, "role=");
  4101. if (pos) {
  4102. role = strtol(pos + 5, NULL, 16);
  4103. if (role != P2PS_SETUP_CLIENT &&
  4104. role != P2PS_SETUP_GROUP_OWNER)
  4105. role = P2PS_SETUP_NONE;
  4106. }
  4107. }
  4108. p2ps_prov->role = role;
  4109. return p2ps_prov;
  4110. invalid_args:
  4111. os_free(p2ps_prov);
  4112. return NULL;
  4113. }
  4114. static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
  4115. {
  4116. u8 addr[ETH_ALEN];
  4117. struct p2ps_provision *p2ps_prov;
  4118. char *pos;
  4119. /* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
  4120. wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
  4121. if (hwaddr_aton(cmd, addr))
  4122. return -1;
  4123. pos = cmd + 17;
  4124. if (*pos != ' ')
  4125. return -1;
  4126. p2ps_prov = p2p_parse_asp_provision_cmd(pos);
  4127. if (!p2ps_prov)
  4128. return -1;
  4129. if (p2ps_prov->status < 0) {
  4130. os_free(p2ps_prov);
  4131. return -1;
  4132. }
  4133. return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
  4134. p2ps_prov);
  4135. }
  4136. static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
  4137. {
  4138. u8 addr[ETH_ALEN];
  4139. struct p2ps_provision *p2ps_prov;
  4140. char *pos;
  4141. /* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
  4142. * session=<ses_id> mac=<ses_mac> [info=<infodata>]
  4143. */
  4144. wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
  4145. if (hwaddr_aton(cmd, addr))
  4146. return -1;
  4147. pos = cmd + 17;
  4148. if (*pos != ' ')
  4149. return -1;
  4150. p2ps_prov = p2p_parse_asp_provision_cmd(pos);
  4151. if (!p2ps_prov)
  4152. return -1;
  4153. p2ps_prov->pd_seeker = 1;
  4154. return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
  4155. p2ps_prov);
  4156. }
  4157. static int parse_freq(int chwidth, int freq2)
  4158. {
  4159. if (freq2 < 0)
  4160. return -1;
  4161. if (freq2)
  4162. return VHT_CHANWIDTH_80P80MHZ;
  4163. switch (chwidth) {
  4164. case 0:
  4165. case 20:
  4166. case 40:
  4167. return VHT_CHANWIDTH_USE_HT;
  4168. case 80:
  4169. return VHT_CHANWIDTH_80MHZ;
  4170. case 160:
  4171. return VHT_CHANWIDTH_160MHZ;
  4172. default:
  4173. wpa_printf(MSG_DEBUG, "Unknown max oper bandwidth: %d",
  4174. chwidth);
  4175. return -1;
  4176. }
  4177. }
  4178. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  4179. char *buf, size_t buflen)
  4180. {
  4181. u8 addr[ETH_ALEN];
  4182. char *pos, *pos2;
  4183. char *pin = NULL;
  4184. enum p2p_wps_method wps_method;
  4185. int new_pin;
  4186. int ret;
  4187. int persistent_group, persistent_id = -1;
  4188. int join;
  4189. int auth;
  4190. int automatic;
  4191. int go_intent = -1;
  4192. int freq = 0;
  4193. int pd;
  4194. int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
  4195. u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
  4196. size_t group_ssid_len = 0;
  4197. if (!wpa_s->global->p2p_init_wpa_s)
  4198. return -1;
  4199. if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
  4200. wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
  4201. wpa_s->global->p2p_init_wpa_s->ifname);
  4202. wpa_s = wpa_s->global->p2p_init_wpa_s;
  4203. }
  4204. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad|p2ps]
  4205. * [persistent|persistent=<network id>]
  4206. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  4207. * [ht40] [vht] [auto] [ssid=<hexdump>] */
  4208. if (hwaddr_aton(cmd, addr))
  4209. return -1;
  4210. pos = cmd + 17;
  4211. if (*pos != ' ')
  4212. return -1;
  4213. pos++;
  4214. persistent_group = os_strstr(pos, " persistent") != NULL;
  4215. pos2 = os_strstr(pos, " persistent=");
  4216. if (pos2) {
  4217. struct wpa_ssid *ssid;
  4218. persistent_id = atoi(pos2 + 12);
  4219. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  4220. if (ssid == NULL || ssid->disabled != 2 ||
  4221. ssid->mode != WPAS_MODE_P2P_GO) {
  4222. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  4223. "SSID id=%d for persistent P2P group (GO)",
  4224. persistent_id);
  4225. return -1;
  4226. }
  4227. }
  4228. join = os_strstr(pos, " join") != NULL;
  4229. auth = os_strstr(pos, " auth") != NULL;
  4230. automatic = os_strstr(pos, " auto") != NULL;
  4231. pd = os_strstr(pos, " provdisc") != NULL;
  4232. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  4233. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  4234. vht;
  4235. pos2 = os_strstr(pos, " go_intent=");
  4236. if (pos2) {
  4237. pos2 += 11;
  4238. go_intent = atoi(pos2);
  4239. if (go_intent < 0 || go_intent > 15)
  4240. return -1;
  4241. }
  4242. pos2 = os_strstr(pos, " freq=");
  4243. if (pos2) {
  4244. pos2 += 6;
  4245. freq = atoi(pos2);
  4246. if (freq <= 0)
  4247. return -1;
  4248. }
  4249. pos2 = os_strstr(pos, " freq2=");
  4250. if (pos2)
  4251. freq2 = atoi(pos2 + 7);
  4252. pos2 = os_strstr(pos, " max_oper_chwidth=");
  4253. if (pos2)
  4254. chwidth = atoi(pos2 + 18);
  4255. max_oper_chwidth = parse_freq(chwidth, freq2);
  4256. if (max_oper_chwidth < 0)
  4257. return -1;
  4258. pos2 = os_strstr(pos, " ssid=");
  4259. if (pos2) {
  4260. char *end;
  4261. pos2 += 6;
  4262. end = os_strchr(pos2, ' ');
  4263. if (!end)
  4264. group_ssid_len = os_strlen(pos2) / 2;
  4265. else
  4266. group_ssid_len = (end - pos2) / 2;
  4267. if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
  4268. hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
  4269. return -1;
  4270. group_ssid = _group_ssid;
  4271. }
  4272. if (os_strncmp(pos, "pin", 3) == 0) {
  4273. /* Request random PIN (to be displayed) and enable the PIN */
  4274. wps_method = WPS_PIN_DISPLAY;
  4275. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  4276. wps_method = WPS_PBC;
  4277. } else {
  4278. pin = pos;
  4279. pos = os_strchr(pin, ' ');
  4280. wps_method = WPS_PIN_KEYPAD;
  4281. if (pos) {
  4282. *pos++ = '\0';
  4283. if (os_strncmp(pos, "display", 7) == 0)
  4284. wps_method = WPS_PIN_DISPLAY;
  4285. else if (os_strncmp(pos, "p2ps", 4) == 0)
  4286. wps_method = WPS_P2PS;
  4287. }
  4288. if (!wps_pin_str_valid(pin)) {
  4289. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  4290. return 17;
  4291. }
  4292. }
  4293. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  4294. persistent_group, automatic, join,
  4295. auth, go_intent, freq, freq2, persistent_id,
  4296. pd, ht40, vht, max_oper_chwidth,
  4297. group_ssid, group_ssid_len);
  4298. if (new_pin == -2) {
  4299. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  4300. return 25;
  4301. }
  4302. if (new_pin == -3) {
  4303. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  4304. return 25;
  4305. }
  4306. if (new_pin < 0)
  4307. return -1;
  4308. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  4309. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  4310. if (os_snprintf_error(buflen, ret))
  4311. return -1;
  4312. return ret;
  4313. }
  4314. os_memcpy(buf, "OK\n", 3);
  4315. return 3;
  4316. }
  4317. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  4318. {
  4319. unsigned int timeout = atoi(cmd);
  4320. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  4321. wpa_dbg(wpa_s, MSG_INFO,
  4322. "Reject P2P_LISTEN since interface is disabled");
  4323. return -1;
  4324. }
  4325. return wpas_p2p_listen(wpa_s, timeout);
  4326. }
  4327. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  4328. {
  4329. u8 addr[ETH_ALEN];
  4330. char *pos;
  4331. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  4332. /* <addr> <config method> [join|auto] */
  4333. if (hwaddr_aton(cmd, addr))
  4334. return -1;
  4335. pos = cmd + 17;
  4336. if (*pos != ' ')
  4337. return -1;
  4338. pos++;
  4339. if (os_strstr(pos, " join") != NULL)
  4340. use = WPAS_P2P_PD_FOR_JOIN;
  4341. else if (os_strstr(pos, " auto") != NULL)
  4342. use = WPAS_P2P_PD_AUTO;
  4343. return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
  4344. }
  4345. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  4346. size_t buflen)
  4347. {
  4348. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4349. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  4350. ssid->passphrase == NULL)
  4351. return -1;
  4352. os_strlcpy(buf, ssid->passphrase, buflen);
  4353. return os_strlen(buf);
  4354. }
  4355. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  4356. char *buf, size_t buflen)
  4357. {
  4358. u64 ref;
  4359. int res;
  4360. u8 dst_buf[ETH_ALEN], *dst;
  4361. struct wpabuf *tlvs;
  4362. char *pos;
  4363. size_t len;
  4364. if (hwaddr_aton(cmd, dst_buf))
  4365. return -1;
  4366. dst = dst_buf;
  4367. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  4368. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  4369. dst = NULL;
  4370. pos = cmd + 17;
  4371. if (*pos != ' ')
  4372. return -1;
  4373. pos++;
  4374. if (os_strncmp(pos, "upnp ", 5) == 0) {
  4375. u8 version;
  4376. pos += 5;
  4377. if (hexstr2bin(pos, &version, 1) < 0)
  4378. return -1;
  4379. pos += 2;
  4380. if (*pos != ' ')
  4381. return -1;
  4382. pos++;
  4383. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  4384. #ifdef CONFIG_WIFI_DISPLAY
  4385. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  4386. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  4387. #endif /* CONFIG_WIFI_DISPLAY */
  4388. } else if (os_strncmp(pos, "asp ", 4) == 0) {
  4389. char *svc_str;
  4390. char *svc_info = NULL;
  4391. u32 id;
  4392. pos += 4;
  4393. if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
  4394. return -1;
  4395. pos = os_strchr(pos, ' ');
  4396. if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
  4397. return -1;
  4398. svc_str = pos + 1;
  4399. pos = os_strchr(svc_str, ' ');
  4400. if (pos)
  4401. *pos++ = '\0';
  4402. /* All remaining data is the svc_info string */
  4403. if (pos && pos[0] && pos[0] != ' ') {
  4404. len = os_strlen(pos);
  4405. /* Unescape in place */
  4406. len = utf8_unescape(pos, len, pos, len);
  4407. if (len > 0xff)
  4408. return -1;
  4409. svc_info = pos;
  4410. }
  4411. ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
  4412. svc_str, svc_info);
  4413. } else {
  4414. len = os_strlen(pos);
  4415. if (len & 1)
  4416. return -1;
  4417. len /= 2;
  4418. tlvs = wpabuf_alloc(len);
  4419. if (tlvs == NULL)
  4420. return -1;
  4421. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  4422. wpabuf_free(tlvs);
  4423. return -1;
  4424. }
  4425. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  4426. wpabuf_free(tlvs);
  4427. }
  4428. if (ref == 0)
  4429. return -1;
  4430. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  4431. if (os_snprintf_error(buflen, res))
  4432. return -1;
  4433. return res;
  4434. }
  4435. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  4436. char *cmd)
  4437. {
  4438. long long unsigned val;
  4439. u64 req;
  4440. if (sscanf(cmd, "%llx", &val) != 1)
  4441. return -1;
  4442. req = val;
  4443. return wpas_p2p_sd_cancel_request(wpa_s, req);
  4444. }
  4445. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  4446. {
  4447. int freq;
  4448. u8 dst[ETH_ALEN];
  4449. u8 dialog_token;
  4450. struct wpabuf *resp_tlvs;
  4451. char *pos, *pos2;
  4452. size_t len;
  4453. pos = os_strchr(cmd, ' ');
  4454. if (pos == NULL)
  4455. return -1;
  4456. *pos++ = '\0';
  4457. freq = atoi(cmd);
  4458. if (freq == 0)
  4459. return -1;
  4460. if (hwaddr_aton(pos, dst))
  4461. return -1;
  4462. pos += 17;
  4463. if (*pos != ' ')
  4464. return -1;
  4465. pos++;
  4466. pos2 = os_strchr(pos, ' ');
  4467. if (pos2 == NULL)
  4468. return -1;
  4469. *pos2++ = '\0';
  4470. dialog_token = atoi(pos);
  4471. len = os_strlen(pos2);
  4472. if (len & 1)
  4473. return -1;
  4474. len /= 2;
  4475. resp_tlvs = wpabuf_alloc(len);
  4476. if (resp_tlvs == NULL)
  4477. return -1;
  4478. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  4479. wpabuf_free(resp_tlvs);
  4480. return -1;
  4481. }
  4482. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  4483. wpabuf_free(resp_tlvs);
  4484. return 0;
  4485. }
  4486. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  4487. char *cmd)
  4488. {
  4489. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  4490. return -1;
  4491. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  4492. return 0;
  4493. }
  4494. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  4495. char *cmd)
  4496. {
  4497. char *pos;
  4498. size_t len;
  4499. struct wpabuf *query, *resp;
  4500. pos = os_strchr(cmd, ' ');
  4501. if (pos == NULL)
  4502. return -1;
  4503. *pos++ = '\0';
  4504. len = os_strlen(cmd);
  4505. if (len & 1)
  4506. return -1;
  4507. len /= 2;
  4508. query = wpabuf_alloc(len);
  4509. if (query == NULL)
  4510. return -1;
  4511. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  4512. wpabuf_free(query);
  4513. return -1;
  4514. }
  4515. len = os_strlen(pos);
  4516. if (len & 1) {
  4517. wpabuf_free(query);
  4518. return -1;
  4519. }
  4520. len /= 2;
  4521. resp = wpabuf_alloc(len);
  4522. if (resp == NULL) {
  4523. wpabuf_free(query);
  4524. return -1;
  4525. }
  4526. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  4527. wpabuf_free(query);
  4528. wpabuf_free(resp);
  4529. return -1;
  4530. }
  4531. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  4532. wpabuf_free(query);
  4533. wpabuf_free(resp);
  4534. return -1;
  4535. }
  4536. return 0;
  4537. }
  4538. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  4539. {
  4540. char *pos;
  4541. u8 version;
  4542. pos = os_strchr(cmd, ' ');
  4543. if (pos == NULL)
  4544. return -1;
  4545. *pos++ = '\0';
  4546. if (hexstr2bin(cmd, &version, 1) < 0)
  4547. return -1;
  4548. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  4549. }
  4550. static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
  4551. u8 replace, char *cmd)
  4552. {
  4553. char *pos;
  4554. char *adv_str;
  4555. u32 auto_accept, adv_id, svc_state, config_methods;
  4556. char *svc_info = NULL;
  4557. char *cpt_prio_str;
  4558. u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
  4559. pos = os_strchr(cmd, ' ');
  4560. if (pos == NULL)
  4561. return -1;
  4562. *pos++ = '\0';
  4563. /* Auto-Accept value is mandatory, and must be one of the
  4564. * single values (0, 1, 2, 4) */
  4565. auto_accept = atoi(cmd);
  4566. switch (auto_accept) {
  4567. case P2PS_SETUP_NONE: /* No auto-accept */
  4568. case P2PS_SETUP_NEW:
  4569. case P2PS_SETUP_CLIENT:
  4570. case P2PS_SETUP_GROUP_OWNER:
  4571. break;
  4572. default:
  4573. return -1;
  4574. }
  4575. /* Advertisement ID is mandatory */
  4576. cmd = pos;
  4577. pos = os_strchr(cmd, ' ');
  4578. if (pos == NULL)
  4579. return -1;
  4580. *pos++ = '\0';
  4581. /* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
  4582. if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
  4583. return -1;
  4584. /* Only allow replacements if exist, and adds if not */
  4585. if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
  4586. if (!replace)
  4587. return -1;
  4588. } else {
  4589. if (replace)
  4590. return -1;
  4591. }
  4592. /* svc_state between 0 - 0xff is mandatory */
  4593. if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
  4594. return -1;
  4595. pos = os_strchr(pos, ' ');
  4596. if (pos == NULL)
  4597. return -1;
  4598. /* config_methods is mandatory */
  4599. pos++;
  4600. if (sscanf(pos, "%x", &config_methods) != 1)
  4601. return -1;
  4602. if (!(config_methods &
  4603. (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
  4604. return -1;
  4605. pos = os_strchr(pos, ' ');
  4606. if (pos == NULL)
  4607. return -1;
  4608. pos++;
  4609. adv_str = pos;
  4610. /* Advertisement string is mandatory */
  4611. if (!pos[0] || pos[0] == ' ')
  4612. return -1;
  4613. /* Terminate svc string */
  4614. pos = os_strchr(pos, ' ');
  4615. if (pos != NULL)
  4616. *pos++ = '\0';
  4617. cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
  4618. if (cpt_prio_str) {
  4619. pos = os_strchr(pos, ' ');
  4620. if (pos != NULL)
  4621. *pos++ = '\0';
  4622. if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
  4623. return -1;
  4624. } else {
  4625. cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
  4626. cpt_prio[1] = 0;
  4627. }
  4628. /* Service and Response Information are optional */
  4629. if (pos && pos[0]) {
  4630. size_t len;
  4631. /* Note the bare ' included, which cannot exist legally
  4632. * in unescaped string. */
  4633. svc_info = os_strstr(pos, "svc_info='");
  4634. if (svc_info) {
  4635. svc_info += 9;
  4636. len = os_strlen(svc_info);
  4637. utf8_unescape(svc_info, len, svc_info, len);
  4638. }
  4639. }
  4640. return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
  4641. (u8) svc_state, (u16) config_methods,
  4642. svc_info, cpt_prio);
  4643. }
  4644. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  4645. {
  4646. char *pos;
  4647. pos = os_strchr(cmd, ' ');
  4648. if (pos == NULL)
  4649. return -1;
  4650. *pos++ = '\0';
  4651. if (os_strcmp(cmd, "bonjour") == 0)
  4652. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  4653. if (os_strcmp(cmd, "upnp") == 0)
  4654. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  4655. if (os_strcmp(cmd, "asp") == 0)
  4656. return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
  4657. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  4658. return -1;
  4659. }
  4660. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  4661. char *cmd)
  4662. {
  4663. size_t len;
  4664. struct wpabuf *query;
  4665. int ret;
  4666. len = os_strlen(cmd);
  4667. if (len & 1)
  4668. return -1;
  4669. len /= 2;
  4670. query = wpabuf_alloc(len);
  4671. if (query == NULL)
  4672. return -1;
  4673. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  4674. wpabuf_free(query);
  4675. return -1;
  4676. }
  4677. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  4678. wpabuf_free(query);
  4679. return ret;
  4680. }
  4681. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  4682. {
  4683. char *pos;
  4684. u8 version;
  4685. pos = os_strchr(cmd, ' ');
  4686. if (pos == NULL)
  4687. return -1;
  4688. *pos++ = '\0';
  4689. if (hexstr2bin(cmd, &version, 1) < 0)
  4690. return -1;
  4691. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  4692. }
  4693. static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
  4694. {
  4695. u32 adv_id;
  4696. if (os_strcmp(cmd, "all") == 0) {
  4697. wpas_p2p_service_flush_asp(wpa_s);
  4698. return 0;
  4699. }
  4700. if (sscanf(cmd, "%x", &adv_id) != 1)
  4701. return -1;
  4702. return wpas_p2p_service_del_asp(wpa_s, adv_id);
  4703. }
  4704. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  4705. {
  4706. char *pos;
  4707. pos = os_strchr(cmd, ' ');
  4708. if (pos == NULL)
  4709. return -1;
  4710. *pos++ = '\0';
  4711. if (os_strcmp(cmd, "bonjour") == 0)
  4712. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  4713. if (os_strcmp(cmd, "upnp") == 0)
  4714. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  4715. if (os_strcmp(cmd, "asp") == 0)
  4716. return p2p_ctrl_service_del_asp(wpa_s, pos);
  4717. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  4718. return -1;
  4719. }
  4720. static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
  4721. {
  4722. char *pos;
  4723. pos = os_strchr(cmd, ' ');
  4724. if (pos == NULL)
  4725. return -1;
  4726. *pos++ = '\0';
  4727. if (os_strcmp(cmd, "asp") == 0)
  4728. return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
  4729. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  4730. return -1;
  4731. }
  4732. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  4733. {
  4734. u8 addr[ETH_ALEN];
  4735. /* <addr> */
  4736. if (hwaddr_aton(cmd, addr))
  4737. return -1;
  4738. return wpas_p2p_reject(wpa_s, addr);
  4739. }
  4740. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  4741. {
  4742. char *pos;
  4743. int id;
  4744. struct wpa_ssid *ssid;
  4745. u8 *_peer = NULL, peer[ETH_ALEN];
  4746. int freq = 0, pref_freq = 0;
  4747. int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
  4748. id = atoi(cmd);
  4749. pos = os_strstr(cmd, " peer=");
  4750. if (pos) {
  4751. pos += 6;
  4752. if (hwaddr_aton(pos, peer))
  4753. return -1;
  4754. _peer = peer;
  4755. }
  4756. ssid = wpa_config_get_network(wpa_s->conf, id);
  4757. if (ssid == NULL || ssid->disabled != 2) {
  4758. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  4759. "for persistent P2P group",
  4760. id);
  4761. return -1;
  4762. }
  4763. pos = os_strstr(cmd, " freq=");
  4764. if (pos) {
  4765. pos += 6;
  4766. freq = atoi(pos);
  4767. if (freq <= 0)
  4768. return -1;
  4769. }
  4770. pos = os_strstr(cmd, " pref=");
  4771. if (pos) {
  4772. pos += 6;
  4773. pref_freq = atoi(pos);
  4774. if (pref_freq <= 0)
  4775. return -1;
  4776. }
  4777. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  4778. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  4779. vht;
  4780. pos = os_strstr(cmd, "freq2=");
  4781. if (pos)
  4782. freq2 = atoi(pos + 6);
  4783. pos = os_strstr(cmd, " max_oper_chwidth=");
  4784. if (pos)
  4785. chwidth = atoi(pos + 18);
  4786. max_oper_chwidth = parse_freq(chwidth, freq2);
  4787. if (max_oper_chwidth < 0)
  4788. return -1;
  4789. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
  4790. max_oper_chwidth, pref_freq);
  4791. }
  4792. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  4793. {
  4794. char *pos;
  4795. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  4796. pos = os_strstr(cmd, " peer=");
  4797. if (!pos)
  4798. return -1;
  4799. *pos = '\0';
  4800. pos += 6;
  4801. if (hwaddr_aton(pos, peer)) {
  4802. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  4803. return -1;
  4804. }
  4805. pos = os_strstr(pos, " go_dev_addr=");
  4806. if (pos) {
  4807. pos += 13;
  4808. if (hwaddr_aton(pos, go_dev_addr)) {
  4809. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  4810. pos);
  4811. return -1;
  4812. }
  4813. go_dev = go_dev_addr;
  4814. }
  4815. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  4816. }
  4817. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  4818. {
  4819. if (os_strncmp(cmd, "persistent=", 11) == 0)
  4820. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  4821. if (os_strncmp(cmd, "group=", 6) == 0)
  4822. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  4823. return -1;
  4824. }
  4825. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  4826. int id, int freq, int vht_center_freq2,
  4827. int ht40, int vht, int vht_chwidth)
  4828. {
  4829. struct wpa_ssid *ssid;
  4830. ssid = wpa_config_get_network(wpa_s->conf, id);
  4831. if (ssid == NULL || ssid->disabled != 2) {
  4832. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  4833. "for persistent P2P group",
  4834. id);
  4835. return -1;
  4836. }
  4837. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq,
  4838. vht_center_freq2, 0, ht40, vht,
  4839. vht_chwidth, NULL, 0, 0);
  4840. }
  4841. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  4842. {
  4843. int freq = 0, persistent = 0, group_id = -1;
  4844. int vht = wpa_s->conf->p2p_go_vht;
  4845. int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
  4846. int max_oper_chwidth, chwidth = 0, freq2 = 0;
  4847. char *token, *context = NULL;
  4848. while ((token = str_token(cmd, " ", &context))) {
  4849. if (sscanf(token, "freq=%d", &freq) == 1 ||
  4850. sscanf(token, "freq2=%d", &freq2) == 1 ||
  4851. sscanf(token, "persistent=%d", &group_id) == 1 ||
  4852. sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1) {
  4853. continue;
  4854. } else if (os_strcmp(token, "ht40") == 0) {
  4855. ht40 = 1;
  4856. } else if (os_strcmp(token, "vht") == 0) {
  4857. vht = 1;
  4858. ht40 = 1;
  4859. } else if (os_strcmp(token, "persistent") == 0) {
  4860. persistent = 1;
  4861. } else {
  4862. wpa_printf(MSG_DEBUG,
  4863. "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
  4864. token);
  4865. return -1;
  4866. }
  4867. }
  4868. max_oper_chwidth = parse_freq(chwidth, freq2);
  4869. if (max_oper_chwidth < 0)
  4870. return -1;
  4871. if (group_id >= 0)
  4872. return p2p_ctrl_group_add_persistent(wpa_s, group_id,
  4873. freq, freq2, ht40, vht,
  4874. max_oper_chwidth);
  4875. return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
  4876. max_oper_chwidth);
  4877. }
  4878. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  4879. char *buf, size_t buflen)
  4880. {
  4881. u8 addr[ETH_ALEN], *addr_ptr;
  4882. int next, res;
  4883. const struct p2p_peer_info *info;
  4884. char *pos, *end;
  4885. char devtype[WPS_DEV_TYPE_BUFSIZE];
  4886. struct wpa_ssid *ssid;
  4887. size_t i;
  4888. if (!wpa_s->global->p2p)
  4889. return -1;
  4890. if (os_strcmp(cmd, "FIRST") == 0) {
  4891. addr_ptr = NULL;
  4892. next = 0;
  4893. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  4894. if (hwaddr_aton(cmd + 5, addr) < 0)
  4895. return -1;
  4896. addr_ptr = addr;
  4897. next = 1;
  4898. } else {
  4899. if (hwaddr_aton(cmd, addr) < 0)
  4900. return -1;
  4901. addr_ptr = addr;
  4902. next = 0;
  4903. }
  4904. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  4905. if (info == NULL)
  4906. return -1;
  4907. pos = buf;
  4908. end = buf + buflen;
  4909. res = os_snprintf(pos, end - pos, MACSTR "\n"
  4910. "pri_dev_type=%s\n"
  4911. "device_name=%s\n"
  4912. "manufacturer=%s\n"
  4913. "model_name=%s\n"
  4914. "model_number=%s\n"
  4915. "serial_number=%s\n"
  4916. "config_methods=0x%x\n"
  4917. "dev_capab=0x%x\n"
  4918. "group_capab=0x%x\n"
  4919. "level=%d\n",
  4920. MAC2STR(info->p2p_device_addr),
  4921. wps_dev_type_bin2str(info->pri_dev_type,
  4922. devtype, sizeof(devtype)),
  4923. info->device_name,
  4924. info->manufacturer,
  4925. info->model_name,
  4926. info->model_number,
  4927. info->serial_number,
  4928. info->config_methods,
  4929. info->dev_capab,
  4930. info->group_capab,
  4931. info->level);
  4932. if (os_snprintf_error(end - pos, res))
  4933. return pos - buf;
  4934. pos += res;
  4935. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  4936. {
  4937. const u8 *t;
  4938. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  4939. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  4940. wps_dev_type_bin2str(t, devtype,
  4941. sizeof(devtype)));
  4942. if (os_snprintf_error(end - pos, res))
  4943. return pos - buf;
  4944. pos += res;
  4945. }
  4946. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  4947. if (ssid) {
  4948. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  4949. if (os_snprintf_error(end - pos, res))
  4950. return pos - buf;
  4951. pos += res;
  4952. }
  4953. res = p2p_get_peer_info_txt(info, pos, end - pos);
  4954. if (res < 0)
  4955. return pos - buf;
  4956. pos += res;
  4957. if (info->vendor_elems) {
  4958. res = os_snprintf(pos, end - pos, "vendor_elems=");
  4959. if (os_snprintf_error(end - pos, res))
  4960. return pos - buf;
  4961. pos += res;
  4962. pos += wpa_snprintf_hex(pos, end - pos,
  4963. wpabuf_head(info->vendor_elems),
  4964. wpabuf_len(info->vendor_elems));
  4965. res = os_snprintf(pos, end - pos, "\n");
  4966. if (os_snprintf_error(end - pos, res))
  4967. return pos - buf;
  4968. pos += res;
  4969. }
  4970. return pos - buf;
  4971. }
  4972. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  4973. const char *param)
  4974. {
  4975. unsigned int i;
  4976. if (wpa_s->global->p2p == NULL)
  4977. return -1;
  4978. if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
  4979. return -1;
  4980. for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
  4981. struct wpa_freq_range *freq;
  4982. freq = &wpa_s->global->p2p_disallow_freq.range[i];
  4983. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  4984. freq->min, freq->max);
  4985. }
  4986. wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
  4987. return 0;
  4988. }
  4989. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  4990. {
  4991. char *param;
  4992. if (wpa_s->global->p2p == NULL)
  4993. return -1;
  4994. param = os_strchr(cmd, ' ');
  4995. if (param == NULL)
  4996. return -1;
  4997. *param++ = '\0';
  4998. if (os_strcmp(cmd, "discoverability") == 0) {
  4999. p2p_set_client_discoverability(wpa_s->global->p2p,
  5000. atoi(param));
  5001. return 0;
  5002. }
  5003. if (os_strcmp(cmd, "managed") == 0) {
  5004. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  5005. return 0;
  5006. }
  5007. if (os_strcmp(cmd, "listen_channel") == 0) {
  5008. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  5009. atoi(param), 1);
  5010. }
  5011. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  5012. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  5013. os_strlen(param));
  5014. }
  5015. if (os_strcmp(cmd, "noa") == 0) {
  5016. char *pos;
  5017. int count, start, duration;
  5018. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  5019. count = atoi(param);
  5020. pos = os_strchr(param, ',');
  5021. if (pos == NULL)
  5022. return -1;
  5023. pos++;
  5024. start = atoi(pos);
  5025. pos = os_strchr(pos, ',');
  5026. if (pos == NULL)
  5027. return -1;
  5028. pos++;
  5029. duration = atoi(pos);
  5030. if (count < 0 || count > 255 || start < 0 || duration < 0)
  5031. return -1;
  5032. if (count == 0 && duration > 0)
  5033. return -1;
  5034. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  5035. "start=%d duration=%d", count, start, duration);
  5036. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  5037. }
  5038. if (os_strcmp(cmd, "ps") == 0)
  5039. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  5040. if (os_strcmp(cmd, "oppps") == 0)
  5041. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  5042. if (os_strcmp(cmd, "ctwindow") == 0)
  5043. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  5044. if (os_strcmp(cmd, "disabled") == 0) {
  5045. wpa_s->global->p2p_disabled = atoi(param);
  5046. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  5047. wpa_s->global->p2p_disabled ?
  5048. "disabled" : "enabled");
  5049. if (wpa_s->global->p2p_disabled) {
  5050. wpas_p2p_stop_find(wpa_s);
  5051. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  5052. p2p_flush(wpa_s->global->p2p);
  5053. }
  5054. return 0;
  5055. }
  5056. if (os_strcmp(cmd, "conc_pref") == 0) {
  5057. if (os_strcmp(param, "sta") == 0)
  5058. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  5059. else if (os_strcmp(param, "p2p") == 0)
  5060. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  5061. else {
  5062. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  5063. return -1;
  5064. }
  5065. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  5066. "%s", param);
  5067. return 0;
  5068. }
  5069. if (os_strcmp(cmd, "force_long_sd") == 0) {
  5070. wpa_s->force_long_sd = atoi(param);
  5071. return 0;
  5072. }
  5073. if (os_strcmp(cmd, "peer_filter") == 0) {
  5074. u8 addr[ETH_ALEN];
  5075. if (hwaddr_aton(param, addr))
  5076. return -1;
  5077. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  5078. return 0;
  5079. }
  5080. if (os_strcmp(cmd, "cross_connect") == 0)
  5081. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  5082. if (os_strcmp(cmd, "go_apsd") == 0) {
  5083. if (os_strcmp(param, "disable") == 0)
  5084. wpa_s->set_ap_uapsd = 0;
  5085. else {
  5086. wpa_s->set_ap_uapsd = 1;
  5087. wpa_s->ap_uapsd = atoi(param);
  5088. }
  5089. return 0;
  5090. }
  5091. if (os_strcmp(cmd, "client_apsd") == 0) {
  5092. if (os_strcmp(param, "disable") == 0)
  5093. wpa_s->set_sta_uapsd = 0;
  5094. else {
  5095. int be, bk, vi, vo;
  5096. char *pos;
  5097. /* format: BE,BK,VI,VO;max SP Length */
  5098. be = atoi(param);
  5099. pos = os_strchr(param, ',');
  5100. if (pos == NULL)
  5101. return -1;
  5102. pos++;
  5103. bk = atoi(pos);
  5104. pos = os_strchr(pos, ',');
  5105. if (pos == NULL)
  5106. return -1;
  5107. pos++;
  5108. vi = atoi(pos);
  5109. pos = os_strchr(pos, ',');
  5110. if (pos == NULL)
  5111. return -1;
  5112. pos++;
  5113. vo = atoi(pos);
  5114. /* ignore max SP Length for now */
  5115. wpa_s->set_sta_uapsd = 1;
  5116. wpa_s->sta_uapsd = 0;
  5117. if (be)
  5118. wpa_s->sta_uapsd |= BIT(0);
  5119. if (bk)
  5120. wpa_s->sta_uapsd |= BIT(1);
  5121. if (vi)
  5122. wpa_s->sta_uapsd |= BIT(2);
  5123. if (vo)
  5124. wpa_s->sta_uapsd |= BIT(3);
  5125. }
  5126. return 0;
  5127. }
  5128. if (os_strcmp(cmd, "disallow_freq") == 0)
  5129. return p2p_ctrl_disallow_freq(wpa_s, param);
  5130. if (os_strcmp(cmd, "disc_int") == 0) {
  5131. int min_disc_int, max_disc_int, max_disc_tu;
  5132. char *pos;
  5133. pos = param;
  5134. min_disc_int = atoi(pos);
  5135. pos = os_strchr(pos, ' ');
  5136. if (pos == NULL)
  5137. return -1;
  5138. *pos++ = '\0';
  5139. max_disc_int = atoi(pos);
  5140. pos = os_strchr(pos, ' ');
  5141. if (pos == NULL)
  5142. return -1;
  5143. *pos++ = '\0';
  5144. max_disc_tu = atoi(pos);
  5145. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  5146. max_disc_int, max_disc_tu);
  5147. }
  5148. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  5149. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  5150. return 0;
  5151. }
  5152. #ifdef CONFIG_WPS_NFC
  5153. if (os_strcmp(cmd, "nfc_tag") == 0)
  5154. return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
  5155. #endif /* CONFIG_WPS_NFC */
  5156. if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
  5157. wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
  5158. return 0;
  5159. }
  5160. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  5161. cmd);
  5162. return -1;
  5163. }
  5164. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  5165. {
  5166. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  5167. wpa_s->force_long_sd = 0;
  5168. wpas_p2p_stop_find(wpa_s);
  5169. wpa_s->parent->p2ps_method_config_any = 0;
  5170. if (wpa_s->global->p2p)
  5171. p2p_flush(wpa_s->global->p2p);
  5172. }
  5173. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  5174. {
  5175. char *pos, *pos2;
  5176. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  5177. if (cmd[0]) {
  5178. pos = os_strchr(cmd, ' ');
  5179. if (pos == NULL)
  5180. return -1;
  5181. *pos++ = '\0';
  5182. dur1 = atoi(cmd);
  5183. pos2 = os_strchr(pos, ' ');
  5184. if (pos2)
  5185. *pos2++ = '\0';
  5186. int1 = atoi(pos);
  5187. } else
  5188. pos2 = NULL;
  5189. if (pos2) {
  5190. pos = os_strchr(pos2, ' ');
  5191. if (pos == NULL)
  5192. return -1;
  5193. *pos++ = '\0';
  5194. dur2 = atoi(pos2);
  5195. int2 = atoi(pos);
  5196. }
  5197. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  5198. }
  5199. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  5200. {
  5201. char *pos;
  5202. unsigned int period = 0, interval = 0;
  5203. if (cmd[0]) {
  5204. pos = os_strchr(cmd, ' ');
  5205. if (pos == NULL)
  5206. return -1;
  5207. *pos++ = '\0';
  5208. period = atoi(cmd);
  5209. interval = atoi(pos);
  5210. }
  5211. return wpas_p2p_ext_listen(wpa_s, period, interval);
  5212. }
  5213. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  5214. {
  5215. const char *pos;
  5216. u8 peer[ETH_ALEN];
  5217. int iface_addr = 0;
  5218. pos = cmd;
  5219. if (os_strncmp(pos, "iface=", 6) == 0) {
  5220. iface_addr = 1;
  5221. pos += 6;
  5222. }
  5223. if (hwaddr_aton(pos, peer))
  5224. return -1;
  5225. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  5226. return 0;
  5227. }
  5228. #endif /* CONFIG_P2P */
  5229. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
  5230. {
  5231. struct wpa_freq_range_list ranges;
  5232. int *freqs = NULL;
  5233. struct hostapd_hw_modes *mode;
  5234. u16 i;
  5235. if (wpa_s->hw.modes == NULL)
  5236. return NULL;
  5237. os_memset(&ranges, 0, sizeof(ranges));
  5238. if (freq_range_list_parse(&ranges, val) < 0)
  5239. return NULL;
  5240. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  5241. int j;
  5242. mode = &wpa_s->hw.modes[i];
  5243. for (j = 0; j < mode->num_channels; j++) {
  5244. unsigned int freq;
  5245. if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
  5246. continue;
  5247. freq = mode->channels[j].freq;
  5248. if (!freq_range_list_includes(&ranges, freq))
  5249. continue;
  5250. int_array_add_unique(&freqs, freq);
  5251. }
  5252. }
  5253. os_free(ranges.range);
  5254. return freqs;
  5255. }
  5256. #ifdef CONFIG_INTERWORKING
  5257. static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
  5258. {
  5259. int auto_sel = 0;
  5260. int *freqs = NULL;
  5261. if (param) {
  5262. char *pos;
  5263. auto_sel = os_strstr(param, "auto") != NULL;
  5264. pos = os_strstr(param, "freq=");
  5265. if (pos) {
  5266. freqs = freq_range_to_channel_list(wpa_s, pos + 5);
  5267. if (freqs == NULL)
  5268. return -1;
  5269. }
  5270. }
  5271. return interworking_select(wpa_s, auto_sel, freqs);
  5272. }
  5273. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
  5274. int only_add)
  5275. {
  5276. u8 bssid[ETH_ALEN];
  5277. struct wpa_bss *bss;
  5278. if (hwaddr_aton(dst, bssid)) {
  5279. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  5280. return -1;
  5281. }
  5282. bss = wpa_bss_get_bssid(wpa_s, bssid);
  5283. if (bss == NULL) {
  5284. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  5285. MAC2STR(bssid));
  5286. return -1;
  5287. }
  5288. if (bss->ssid_len == 0) {
  5289. int found = 0;
  5290. wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
  5291. " does not have SSID information", MAC2STR(bssid));
  5292. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
  5293. list) {
  5294. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
  5295. bss->ssid_len > 0) {
  5296. found = 1;
  5297. break;
  5298. }
  5299. }
  5300. if (!found)
  5301. return -1;
  5302. wpa_printf(MSG_DEBUG,
  5303. "Found another matching BSS entry with SSID");
  5304. }
  5305. return interworking_connect(wpa_s, bss, only_add);
  5306. }
  5307. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  5308. {
  5309. u8 dst_addr[ETH_ALEN];
  5310. int used;
  5311. char *pos;
  5312. #define MAX_ANQP_INFO_ID 100
  5313. u16 id[MAX_ANQP_INFO_ID];
  5314. size_t num_id = 0;
  5315. u32 subtypes = 0;
  5316. used = hwaddr_aton2(dst, dst_addr);
  5317. if (used < 0)
  5318. return -1;
  5319. pos = dst + used;
  5320. if (*pos == ' ')
  5321. pos++;
  5322. while (num_id < MAX_ANQP_INFO_ID) {
  5323. if (os_strncmp(pos, "hs20:", 5) == 0) {
  5324. #ifdef CONFIG_HS20
  5325. int num = atoi(pos + 5);
  5326. if (num <= 0 || num > 31)
  5327. return -1;
  5328. subtypes |= BIT(num);
  5329. #else /* CONFIG_HS20 */
  5330. return -1;
  5331. #endif /* CONFIG_HS20 */
  5332. } else {
  5333. id[num_id] = atoi(pos);
  5334. if (id[num_id])
  5335. num_id++;
  5336. }
  5337. pos = os_strchr(pos + 1, ',');
  5338. if (pos == NULL)
  5339. break;
  5340. pos++;
  5341. }
  5342. if (num_id == 0)
  5343. return -1;
  5344. return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
  5345. }
  5346. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  5347. {
  5348. u8 dst_addr[ETH_ALEN];
  5349. struct wpabuf *advproto, *query = NULL;
  5350. int used, ret = -1;
  5351. char *pos, *end;
  5352. size_t len;
  5353. used = hwaddr_aton2(cmd, dst_addr);
  5354. if (used < 0)
  5355. return -1;
  5356. pos = cmd + used;
  5357. while (*pos == ' ')
  5358. pos++;
  5359. /* Advertisement Protocol ID */
  5360. end = os_strchr(pos, ' ');
  5361. if (end)
  5362. len = end - pos;
  5363. else
  5364. len = os_strlen(pos);
  5365. if (len & 0x01)
  5366. return -1;
  5367. len /= 2;
  5368. if (len == 0)
  5369. return -1;
  5370. advproto = wpabuf_alloc(len);
  5371. if (advproto == NULL)
  5372. return -1;
  5373. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  5374. goto fail;
  5375. if (end) {
  5376. /* Optional Query Request */
  5377. pos = end + 1;
  5378. while (*pos == ' ')
  5379. pos++;
  5380. len = os_strlen(pos);
  5381. if (len) {
  5382. if (len & 0x01)
  5383. goto fail;
  5384. len /= 2;
  5385. if (len == 0)
  5386. goto fail;
  5387. query = wpabuf_alloc(len);
  5388. if (query == NULL)
  5389. goto fail;
  5390. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  5391. goto fail;
  5392. }
  5393. }
  5394. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  5395. fail:
  5396. wpabuf_free(advproto);
  5397. wpabuf_free(query);
  5398. return ret;
  5399. }
  5400. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  5401. size_t buflen)
  5402. {
  5403. u8 addr[ETH_ALEN];
  5404. int dialog_token;
  5405. int used;
  5406. char *pos;
  5407. size_t resp_len, start, requested_len;
  5408. struct wpabuf *resp;
  5409. int ret;
  5410. used = hwaddr_aton2(cmd, addr);
  5411. if (used < 0)
  5412. return -1;
  5413. pos = cmd + used;
  5414. while (*pos == ' ')
  5415. pos++;
  5416. dialog_token = atoi(pos);
  5417. if (wpa_s->last_gas_resp &&
  5418. os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
  5419. dialog_token == wpa_s->last_gas_dialog_token)
  5420. resp = wpa_s->last_gas_resp;
  5421. else if (wpa_s->prev_gas_resp &&
  5422. os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
  5423. dialog_token == wpa_s->prev_gas_dialog_token)
  5424. resp = wpa_s->prev_gas_resp;
  5425. else
  5426. return -1;
  5427. resp_len = wpabuf_len(resp);
  5428. start = 0;
  5429. requested_len = resp_len;
  5430. pos = os_strchr(pos, ' ');
  5431. if (pos) {
  5432. start = atoi(pos);
  5433. if (start > resp_len)
  5434. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  5435. pos = os_strchr(pos, ',');
  5436. if (pos == NULL)
  5437. return -1;
  5438. pos++;
  5439. requested_len = atoi(pos);
  5440. if (start + requested_len > resp_len)
  5441. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  5442. }
  5443. if (requested_len * 2 + 1 > buflen)
  5444. return os_snprintf(buf, buflen, "FAIL-Too long response");
  5445. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
  5446. requested_len);
  5447. if (start + requested_len == resp_len) {
  5448. /*
  5449. * Free memory by dropping the response after it has been
  5450. * fetched.
  5451. */
  5452. if (resp == wpa_s->prev_gas_resp) {
  5453. wpabuf_free(wpa_s->prev_gas_resp);
  5454. wpa_s->prev_gas_resp = NULL;
  5455. } else {
  5456. wpabuf_free(wpa_s->last_gas_resp);
  5457. wpa_s->last_gas_resp = NULL;
  5458. }
  5459. }
  5460. return ret;
  5461. }
  5462. #endif /* CONFIG_INTERWORKING */
  5463. #ifdef CONFIG_HS20
  5464. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  5465. {
  5466. u8 dst_addr[ETH_ALEN];
  5467. int used;
  5468. char *pos;
  5469. u32 subtypes = 0;
  5470. used = hwaddr_aton2(dst, dst_addr);
  5471. if (used < 0)
  5472. return -1;
  5473. pos = dst + used;
  5474. if (*pos == ' ')
  5475. pos++;
  5476. for (;;) {
  5477. int num = atoi(pos);
  5478. if (num <= 0 || num > 31)
  5479. return -1;
  5480. subtypes |= BIT(num);
  5481. pos = os_strchr(pos + 1, ',');
  5482. if (pos == NULL)
  5483. break;
  5484. pos++;
  5485. }
  5486. if (subtypes == 0)
  5487. return -1;
  5488. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
  5489. }
  5490. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  5491. const u8 *addr, const char *realm)
  5492. {
  5493. u8 *buf;
  5494. size_t rlen, len;
  5495. int ret;
  5496. rlen = os_strlen(realm);
  5497. len = 3 + rlen;
  5498. buf = os_malloc(len);
  5499. if (buf == NULL)
  5500. return -1;
  5501. buf[0] = 1; /* NAI Home Realm Count */
  5502. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  5503. buf[2] = rlen;
  5504. os_memcpy(buf + 3, realm, rlen);
  5505. ret = hs20_anqp_send_req(wpa_s, addr,
  5506. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  5507. buf, len, 0);
  5508. os_free(buf);
  5509. return ret;
  5510. }
  5511. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  5512. char *dst)
  5513. {
  5514. struct wpa_cred *cred = wpa_s->conf->cred;
  5515. u8 dst_addr[ETH_ALEN];
  5516. int used;
  5517. u8 *buf;
  5518. size_t len;
  5519. int ret;
  5520. used = hwaddr_aton2(dst, dst_addr);
  5521. if (used < 0)
  5522. return -1;
  5523. while (dst[used] == ' ')
  5524. used++;
  5525. if (os_strncmp(dst + used, "realm=", 6) == 0)
  5526. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  5527. dst + used + 6);
  5528. len = os_strlen(dst + used);
  5529. if (len == 0 && cred && cred->realm)
  5530. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  5531. if (len & 1)
  5532. return -1;
  5533. len /= 2;
  5534. buf = os_malloc(len);
  5535. if (buf == NULL)
  5536. return -1;
  5537. if (hexstr2bin(dst + used, buf, len) < 0) {
  5538. os_free(buf);
  5539. return -1;
  5540. }
  5541. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  5542. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  5543. buf, len, 0);
  5544. os_free(buf);
  5545. return ret;
  5546. }
  5547. static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
  5548. int buflen)
  5549. {
  5550. u8 dst_addr[ETH_ALEN];
  5551. int used;
  5552. char *ctx = NULL, *icon, *poffset, *psize;
  5553. used = hwaddr_aton2(cmd, dst_addr);
  5554. if (used < 0)
  5555. return -1;
  5556. cmd += used;
  5557. icon = str_token(cmd, " ", &ctx);
  5558. poffset = str_token(cmd, " ", &ctx);
  5559. psize = str_token(cmd, " ", &ctx);
  5560. if (!icon || !poffset || !psize)
  5561. return -1;
  5562. wpa_s->fetch_osu_icon_in_progress = 0;
  5563. return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
  5564. reply, buflen);
  5565. }
  5566. static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
  5567. {
  5568. u8 dst_addr[ETH_ALEN];
  5569. int used;
  5570. char *icon;
  5571. if (!cmd[0])
  5572. return hs20_del_icon(wpa_s, NULL, NULL);
  5573. used = hwaddr_aton2(cmd, dst_addr);
  5574. if (used < 0)
  5575. return -1;
  5576. while (cmd[used] == ' ')
  5577. used++;
  5578. icon = cmd[used] ? &cmd[used] : NULL;
  5579. return hs20_del_icon(wpa_s, dst_addr, icon);
  5580. }
  5581. static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
  5582. {
  5583. u8 dst_addr[ETH_ALEN];
  5584. int used;
  5585. char *icon;
  5586. used = hwaddr_aton2(cmd, dst_addr);
  5587. if (used < 0)
  5588. return -1;
  5589. while (cmd[used] == ' ')
  5590. used++;
  5591. icon = &cmd[used];
  5592. wpa_s->fetch_osu_icon_in_progress = 0;
  5593. return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
  5594. (u8 *) icon, os_strlen(icon), inmem);
  5595. }
  5596. #endif /* CONFIG_HS20 */
  5597. #ifdef CONFIG_AUTOSCAN
  5598. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  5599. char *cmd)
  5600. {
  5601. enum wpa_states state = wpa_s->wpa_state;
  5602. char *new_params = NULL;
  5603. if (os_strlen(cmd) > 0) {
  5604. new_params = os_strdup(cmd);
  5605. if (new_params == NULL)
  5606. return -1;
  5607. }
  5608. os_free(wpa_s->conf->autoscan);
  5609. wpa_s->conf->autoscan = new_params;
  5610. if (wpa_s->conf->autoscan == NULL)
  5611. autoscan_deinit(wpa_s);
  5612. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  5613. autoscan_init(wpa_s, 1);
  5614. else if (state == WPA_SCANNING)
  5615. wpa_supplicant_reinit_autoscan(wpa_s);
  5616. return 0;
  5617. }
  5618. #endif /* CONFIG_AUTOSCAN */
  5619. #ifdef CONFIG_WNM
  5620. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  5621. {
  5622. int enter;
  5623. int intval = 0;
  5624. char *pos;
  5625. int ret;
  5626. struct wpabuf *tfs_req = NULL;
  5627. if (os_strncmp(cmd, "enter", 5) == 0)
  5628. enter = 1;
  5629. else if (os_strncmp(cmd, "exit", 4) == 0)
  5630. enter = 0;
  5631. else
  5632. return -1;
  5633. pos = os_strstr(cmd, " interval=");
  5634. if (pos)
  5635. intval = atoi(pos + 10);
  5636. pos = os_strstr(cmd, " tfs_req=");
  5637. if (pos) {
  5638. char *end;
  5639. size_t len;
  5640. pos += 9;
  5641. end = os_strchr(pos, ' ');
  5642. if (end)
  5643. len = end - pos;
  5644. else
  5645. len = os_strlen(pos);
  5646. if (len & 1)
  5647. return -1;
  5648. len /= 2;
  5649. tfs_req = wpabuf_alloc(len);
  5650. if (tfs_req == NULL)
  5651. return -1;
  5652. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  5653. wpabuf_free(tfs_req);
  5654. return -1;
  5655. }
  5656. }
  5657. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  5658. WNM_SLEEP_MODE_EXIT, intval,
  5659. tfs_req);
  5660. wpabuf_free(tfs_req);
  5661. return ret;
  5662. }
  5663. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  5664. {
  5665. int query_reason;
  5666. query_reason = atoi(cmd);
  5667. wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
  5668. query_reason);
  5669. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
  5670. }
  5671. #endif /* CONFIG_WNM */
  5672. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  5673. size_t buflen)
  5674. {
  5675. struct wpa_signal_info si;
  5676. int ret;
  5677. char *pos, *end;
  5678. ret = wpa_drv_signal_poll(wpa_s, &si);
  5679. if (ret)
  5680. return -1;
  5681. pos = buf;
  5682. end = buf + buflen;
  5683. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  5684. "NOISE=%d\nFREQUENCY=%u\n",
  5685. si.current_signal, si.current_txrate / 1000,
  5686. si.current_noise, si.frequency);
  5687. if (os_snprintf_error(end - pos, ret))
  5688. return -1;
  5689. pos += ret;
  5690. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  5691. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  5692. channel_width_to_string(si.chanwidth));
  5693. if (os_snprintf_error(end - pos, ret))
  5694. return -1;
  5695. pos += ret;
  5696. }
  5697. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  5698. ret = os_snprintf(pos, end - pos,
  5699. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  5700. si.center_frq1, si.center_frq2);
  5701. if (os_snprintf_error(end - pos, ret))
  5702. return -1;
  5703. pos += ret;
  5704. }
  5705. if (si.avg_signal) {
  5706. ret = os_snprintf(pos, end - pos,
  5707. "AVG_RSSI=%d\n", si.avg_signal);
  5708. if (os_snprintf_error(end - pos, ret))
  5709. return -1;
  5710. pos += ret;
  5711. }
  5712. if (si.avg_beacon_signal) {
  5713. ret = os_snprintf(pos, end - pos,
  5714. "AVG_BEACON_RSSI=%d\n", si.avg_beacon_signal);
  5715. if (os_snprintf_error(end - pos, ret))
  5716. return -1;
  5717. pos += ret;
  5718. }
  5719. return pos - buf;
  5720. }
  5721. static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
  5722. const char *cmd)
  5723. {
  5724. const char *pos;
  5725. int threshold = 0;
  5726. int hysteresis = 0;
  5727. if (wpa_s->bgscan && wpa_s->bgscan_priv) {
  5728. wpa_printf(MSG_DEBUG,
  5729. "Reject SIGNAL_MONITOR command - bgscan is active");
  5730. return -1;
  5731. }
  5732. pos = os_strstr(cmd, "THRESHOLD=");
  5733. if (pos)
  5734. threshold = atoi(pos + 10);
  5735. pos = os_strstr(cmd, "HYSTERESIS=");
  5736. if (pos)
  5737. hysteresis = atoi(pos + 11);
  5738. return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
  5739. }
  5740. static int wpas_ctrl_iface_get_pref_freq_list(
  5741. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  5742. {
  5743. unsigned int freq_list[100], num = 100, i;
  5744. int ret;
  5745. enum wpa_driver_if_type iface_type;
  5746. char *pos, *end;
  5747. pos = buf;
  5748. end = buf + buflen;
  5749. /* buf: "<interface_type>" */
  5750. if (os_strcmp(cmd, "STATION") == 0)
  5751. iface_type = WPA_IF_STATION;
  5752. else if (os_strcmp(cmd, "AP") == 0)
  5753. iface_type = WPA_IF_AP_BSS;
  5754. else if (os_strcmp(cmd, "P2P_GO") == 0)
  5755. iface_type = WPA_IF_P2P_GO;
  5756. else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
  5757. iface_type = WPA_IF_P2P_CLIENT;
  5758. else if (os_strcmp(cmd, "IBSS") == 0)
  5759. iface_type = WPA_IF_IBSS;
  5760. else if (os_strcmp(cmd, "TDLS") == 0)
  5761. iface_type = WPA_IF_TDLS;
  5762. else
  5763. return -1;
  5764. wpa_printf(MSG_DEBUG,
  5765. "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
  5766. iface_type, buf);
  5767. ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
  5768. if (ret)
  5769. return -1;
  5770. for (i = 0; i < num; i++) {
  5771. ret = os_snprintf(pos, end - pos, "%s%u",
  5772. i > 0 ? "," : "", freq_list[i]);
  5773. if (os_snprintf_error(end - pos, ret))
  5774. return -1;
  5775. pos += ret;
  5776. }
  5777. return pos - buf;
  5778. }
  5779. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  5780. size_t buflen)
  5781. {
  5782. struct hostap_sta_driver_data sta;
  5783. int ret;
  5784. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  5785. if (ret)
  5786. return -1;
  5787. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  5788. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  5789. if (os_snprintf_error(buflen, ret))
  5790. return -1;
  5791. return ret;
  5792. }
  5793. #ifdef ANDROID
  5794. static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  5795. char *buf, size_t buflen)
  5796. {
  5797. int ret;
  5798. ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
  5799. if (ret == 0) {
  5800. if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
  5801. struct p2p_data *p2p = wpa_s->global->p2p;
  5802. if (p2p) {
  5803. char country[3];
  5804. country[0] = cmd[8];
  5805. country[1] = cmd[9];
  5806. country[2] = 0x04;
  5807. p2p_set_country(p2p, country);
  5808. }
  5809. }
  5810. ret = os_snprintf(buf, buflen, "%s\n", "OK");
  5811. if (os_snprintf_error(buflen, ret))
  5812. ret = -1;
  5813. }
  5814. return ret;
  5815. }
  5816. #endif /* ANDROID */
  5817. static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  5818. char *buf, size_t buflen)
  5819. {
  5820. int ret;
  5821. char *pos;
  5822. u8 *data = NULL;
  5823. unsigned int vendor_id, subcmd;
  5824. struct wpabuf *reply;
  5825. size_t data_len = 0;
  5826. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  5827. vendor_id = strtoul(cmd, &pos, 16);
  5828. if (!isblank((unsigned char) *pos))
  5829. return -EINVAL;
  5830. subcmd = strtoul(pos, &pos, 10);
  5831. if (*pos != '\0') {
  5832. if (!isblank((unsigned char) *pos++))
  5833. return -EINVAL;
  5834. data_len = os_strlen(pos);
  5835. }
  5836. if (data_len) {
  5837. data_len /= 2;
  5838. data = os_malloc(data_len);
  5839. if (!data)
  5840. return -1;
  5841. if (hexstr2bin(pos, data, data_len)) {
  5842. wpa_printf(MSG_DEBUG,
  5843. "Vendor command: wrong parameter format");
  5844. os_free(data);
  5845. return -EINVAL;
  5846. }
  5847. }
  5848. reply = wpabuf_alloc((buflen - 1) / 2);
  5849. if (!reply) {
  5850. os_free(data);
  5851. return -1;
  5852. }
  5853. ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
  5854. reply);
  5855. if (ret == 0)
  5856. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  5857. wpabuf_len(reply));
  5858. wpabuf_free(reply);
  5859. os_free(data);
  5860. return ret;
  5861. }
  5862. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  5863. {
  5864. #ifdef CONFIG_P2P
  5865. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
  5866. wpa_s->global->p2p_init_wpa_s : wpa_s;
  5867. #endif /* CONFIG_P2P */
  5868. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  5869. wpas_abort_ongoing_scan(wpa_s);
  5870. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  5871. /*
  5872. * Avoid possible auto connect re-connection on getting
  5873. * disconnected due to state flush.
  5874. */
  5875. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  5876. }
  5877. #ifdef CONFIG_P2P
  5878. wpas_p2p_group_remove(p2p_wpa_s, "*");
  5879. wpas_p2p_cancel(p2p_wpa_s);
  5880. p2p_ctrl_flush(p2p_wpa_s);
  5881. wpas_p2p_service_flush(p2p_wpa_s);
  5882. p2p_wpa_s->global->p2p_disabled = 0;
  5883. p2p_wpa_s->global->p2p_per_sta_psk = 0;
  5884. p2p_wpa_s->conf->num_sec_device_types = 0;
  5885. p2p_wpa_s->p2p_disable_ip_addr_req = 0;
  5886. os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
  5887. p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
  5888. p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
  5889. p2p_wpa_s->global->pending_p2ps_group = 0;
  5890. p2p_wpa_s->global->pending_p2ps_group_freq = 0;
  5891. #endif /* CONFIG_P2P */
  5892. #ifdef CONFIG_WPS_TESTING
  5893. wps_version_number = 0x20;
  5894. wps_testing_dummy_cred = 0;
  5895. wps_corrupt_pkhash = 0;
  5896. wps_force_auth_types_in_use = 0;
  5897. wps_force_encr_types_in_use = 0;
  5898. #endif /* CONFIG_WPS_TESTING */
  5899. #ifdef CONFIG_WPS
  5900. wpa_s->wps_fragment_size = 0;
  5901. wpas_wps_cancel(wpa_s);
  5902. wps_registrar_flush(wpa_s->wps->registrar);
  5903. #endif /* CONFIG_WPS */
  5904. wpa_s->after_wps = 0;
  5905. wpa_s->known_wps_freq = 0;
  5906. #ifdef CONFIG_TDLS
  5907. #ifdef CONFIG_TDLS_TESTING
  5908. extern unsigned int tdls_testing;
  5909. tdls_testing = 0;
  5910. #endif /* CONFIG_TDLS_TESTING */
  5911. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  5912. wpa_tdls_enable(wpa_s->wpa, 1);
  5913. #endif /* CONFIG_TDLS */
  5914. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  5915. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  5916. wpa_s->no_keep_alive = 0;
  5917. wpa_s->own_disconnect_req = 0;
  5918. os_free(wpa_s->disallow_aps_bssid);
  5919. wpa_s->disallow_aps_bssid = NULL;
  5920. wpa_s->disallow_aps_bssid_count = 0;
  5921. os_free(wpa_s->disallow_aps_ssid);
  5922. wpa_s->disallow_aps_ssid = NULL;
  5923. wpa_s->disallow_aps_ssid_count = 0;
  5924. wpa_s->set_sta_uapsd = 0;
  5925. wpa_s->sta_uapsd = 0;
  5926. wpa_drv_radio_disable(wpa_s, 0);
  5927. wpa_blacklist_clear(wpa_s);
  5928. wpa_s->extra_blacklist_count = 0;
  5929. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  5930. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  5931. wpa_config_flush_blobs(wpa_s->conf);
  5932. wpa_s->conf->auto_interworking = 0;
  5933. wpa_s->conf->okc = 0;
  5934. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  5935. rsn_preauth_deinit(wpa_s->wpa);
  5936. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
  5937. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
  5938. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
  5939. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  5940. radio_remove_works(wpa_s, NULL, 1);
  5941. wpa_s->ext_work_in_progress = 0;
  5942. wpa_s->next_ssid = NULL;
  5943. #ifdef CONFIG_INTERWORKING
  5944. #ifdef CONFIG_HS20
  5945. hs20_cancel_fetch_osu(wpa_s);
  5946. hs20_del_icon(wpa_s, NULL, NULL);
  5947. #endif /* CONFIG_HS20 */
  5948. #endif /* CONFIG_INTERWORKING */
  5949. wpa_s->ext_mgmt_frame_handling = 0;
  5950. wpa_s->ext_eapol_frame_io = 0;
  5951. #ifdef CONFIG_TESTING_OPTIONS
  5952. wpa_s->extra_roc_dur = 0;
  5953. wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
  5954. wpa_s->p2p_go_csa_on_inv = 0;
  5955. wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
  5956. #endif /* CONFIG_TESTING_OPTIONS */
  5957. wpa_s->disconnected = 0;
  5958. os_free(wpa_s->next_scan_freqs);
  5959. wpa_s->next_scan_freqs = NULL;
  5960. wpa_bss_flush(wpa_s);
  5961. if (!dl_list_empty(&wpa_s->bss)) {
  5962. wpa_printf(MSG_DEBUG,
  5963. "BSS table not empty after flush: %u entries, current_bss=%p bssid="
  5964. MACSTR " pending_bssid=" MACSTR,
  5965. dl_list_len(&wpa_s->bss), wpa_s->current_bss,
  5966. MAC2STR(wpa_s->bssid),
  5967. MAC2STR(wpa_s->pending_bssid));
  5968. }
  5969. eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
  5970. wpa_s->wnmsleep_used = 0;
  5971. }
  5972. static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
  5973. char *buf, size_t buflen)
  5974. {
  5975. struct wpa_radio_work *work;
  5976. char *pos, *end;
  5977. struct os_reltime now, diff;
  5978. pos = buf;
  5979. end = buf + buflen;
  5980. os_get_reltime(&now);
  5981. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  5982. {
  5983. int ret;
  5984. os_reltime_sub(&now, &work->time, &diff);
  5985. ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
  5986. work->type, work->wpa_s->ifname, work->freq,
  5987. work->started, diff.sec, diff.usec);
  5988. if (os_snprintf_error(end - pos, ret))
  5989. break;
  5990. pos += ret;
  5991. }
  5992. return pos - buf;
  5993. }
  5994. static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
  5995. {
  5996. struct wpa_radio_work *work = eloop_ctx;
  5997. struct wpa_external_work *ework = work->ctx;
  5998. wpa_dbg(work->wpa_s, MSG_DEBUG,
  5999. "Timing out external radio work %u (%s)",
  6000. ework->id, work->type);
  6001. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
  6002. work->wpa_s->ext_work_in_progress = 0;
  6003. radio_work_done(work);
  6004. os_free(ework);
  6005. }
  6006. static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
  6007. {
  6008. struct wpa_external_work *ework = work->ctx;
  6009. if (deinit) {
  6010. if (work->started)
  6011. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  6012. work, NULL);
  6013. os_free(ework);
  6014. return;
  6015. }
  6016. wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
  6017. ework->id, ework->type);
  6018. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
  6019. work->wpa_s->ext_work_in_progress = 1;
  6020. if (!ework->timeout)
  6021. ework->timeout = 10;
  6022. eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
  6023. work, NULL);
  6024. }
  6025. static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
  6026. char *buf, size_t buflen)
  6027. {
  6028. struct wpa_external_work *ework;
  6029. char *pos, *pos2;
  6030. size_t type_len;
  6031. int ret;
  6032. unsigned int freq = 0;
  6033. /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
  6034. ework = os_zalloc(sizeof(*ework));
  6035. if (ework == NULL)
  6036. return -1;
  6037. pos = os_strchr(cmd, ' ');
  6038. if (pos) {
  6039. type_len = pos - cmd;
  6040. pos++;
  6041. pos2 = os_strstr(pos, "freq=");
  6042. if (pos2)
  6043. freq = atoi(pos2 + 5);
  6044. pos2 = os_strstr(pos, "timeout=");
  6045. if (pos2)
  6046. ework->timeout = atoi(pos2 + 8);
  6047. } else {
  6048. type_len = os_strlen(cmd);
  6049. }
  6050. if (4 + type_len >= sizeof(ework->type))
  6051. type_len = sizeof(ework->type) - 4 - 1;
  6052. os_strlcpy(ework->type, "ext:", sizeof(ework->type));
  6053. os_memcpy(ework->type + 4, cmd, type_len);
  6054. ework->type[4 + type_len] = '\0';
  6055. wpa_s->ext_work_id++;
  6056. if (wpa_s->ext_work_id == 0)
  6057. wpa_s->ext_work_id++;
  6058. ework->id = wpa_s->ext_work_id;
  6059. if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
  6060. ework) < 0) {
  6061. os_free(ework);
  6062. return -1;
  6063. }
  6064. ret = os_snprintf(buf, buflen, "%u", ework->id);
  6065. if (os_snprintf_error(buflen, ret))
  6066. return -1;
  6067. return ret;
  6068. }
  6069. static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
  6070. {
  6071. struct wpa_radio_work *work;
  6072. unsigned int id = atoi(cmd);
  6073. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  6074. {
  6075. struct wpa_external_work *ework;
  6076. if (os_strncmp(work->type, "ext:", 4) != 0)
  6077. continue;
  6078. ework = work->ctx;
  6079. if (id && ework->id != id)
  6080. continue;
  6081. wpa_dbg(wpa_s, MSG_DEBUG,
  6082. "Completed external radio work %u (%s)",
  6083. ework->id, ework->type);
  6084. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
  6085. wpa_s->ext_work_in_progress = 0;
  6086. radio_work_done(work);
  6087. os_free(ework);
  6088. return 3; /* "OK\n" */
  6089. }
  6090. return -1;
  6091. }
  6092. static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
  6093. char *buf, size_t buflen)
  6094. {
  6095. if (os_strcmp(cmd, "show") == 0)
  6096. return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
  6097. if (os_strncmp(cmd, "add ", 4) == 0)
  6098. return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
  6099. if (os_strncmp(cmd, "done ", 5) == 0)
  6100. return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
  6101. return -1;
  6102. }
  6103. void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
  6104. {
  6105. struct wpa_radio_work *work, *tmp;
  6106. if (!wpa_s || !wpa_s->radio)
  6107. return;
  6108. dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
  6109. struct wpa_radio_work, list) {
  6110. struct wpa_external_work *ework;
  6111. if (os_strncmp(work->type, "ext:", 4) != 0)
  6112. continue;
  6113. ework = work->ctx;
  6114. wpa_dbg(wpa_s, MSG_DEBUG,
  6115. "Flushing%s external radio work %u (%s)",
  6116. work->started ? " started" : "", ework->id,
  6117. ework->type);
  6118. if (work->started)
  6119. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  6120. work, NULL);
  6121. radio_work_done(work);
  6122. os_free(ework);
  6123. }
  6124. }
  6125. static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
  6126. {
  6127. struct wpa_supplicant *wpa_s = eloop_ctx;
  6128. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  6129. }
  6130. static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
  6131. unsigned int *scan_id_count, int scan_id[])
  6132. {
  6133. const char *pos = value;
  6134. while (pos) {
  6135. if (*pos == ' ' || *pos == '\0')
  6136. break;
  6137. if (*scan_id_count == MAX_SCAN_ID)
  6138. return -1;
  6139. scan_id[(*scan_id_count)++] = atoi(pos);
  6140. pos = os_strchr(pos, ',');
  6141. if (pos)
  6142. pos++;
  6143. }
  6144. return 0;
  6145. }
  6146. static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
  6147. char *reply, int reply_size, int *reply_len)
  6148. {
  6149. char *pos;
  6150. unsigned int manual_scan_passive = 0;
  6151. unsigned int manual_scan_use_id = 0;
  6152. unsigned int manual_scan_only_new = 0;
  6153. unsigned int scan_only = 0;
  6154. unsigned int scan_id_count = 0;
  6155. int scan_id[MAX_SCAN_ID];
  6156. void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
  6157. struct wpa_scan_results *scan_res);
  6158. int *manual_scan_freqs = NULL;
  6159. struct wpa_ssid_value *ssid = NULL, *ns;
  6160. unsigned int ssid_count = 0;
  6161. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  6162. *reply_len = -1;
  6163. return;
  6164. }
  6165. if (radio_work_pending(wpa_s, "scan")) {
  6166. wpa_printf(MSG_DEBUG,
  6167. "Pending scan scheduled - reject new request");
  6168. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  6169. return;
  6170. }
  6171. #ifdef CONFIG_INTERWORKING
  6172. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
  6173. wpa_printf(MSG_DEBUG,
  6174. "Interworking select in progress - reject new scan");
  6175. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  6176. return;
  6177. }
  6178. #endif /* CONFIG_INTERWORKING */
  6179. if (params) {
  6180. if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
  6181. scan_only = 1;
  6182. pos = os_strstr(params, "freq=");
  6183. if (pos) {
  6184. manual_scan_freqs = freq_range_to_channel_list(wpa_s,
  6185. pos + 5);
  6186. if (manual_scan_freqs == NULL) {
  6187. *reply_len = -1;
  6188. goto done;
  6189. }
  6190. }
  6191. pos = os_strstr(params, "passive=");
  6192. if (pos)
  6193. manual_scan_passive = !!atoi(pos + 8);
  6194. pos = os_strstr(params, "use_id=");
  6195. if (pos)
  6196. manual_scan_use_id = atoi(pos + 7);
  6197. pos = os_strstr(params, "only_new=1");
  6198. if (pos)
  6199. manual_scan_only_new = 1;
  6200. pos = os_strstr(params, "scan_id=");
  6201. if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
  6202. scan_id) < 0) {
  6203. *reply_len = -1;
  6204. goto done;
  6205. }
  6206. pos = params;
  6207. while (pos && *pos != '\0') {
  6208. if (os_strncmp(pos, "ssid ", 5) == 0) {
  6209. char *end;
  6210. pos += 5;
  6211. end = pos;
  6212. while (*end) {
  6213. if (*end == '\0' || *end == ' ')
  6214. break;
  6215. end++;
  6216. }
  6217. ns = os_realloc_array(
  6218. ssid, ssid_count + 1,
  6219. sizeof(struct wpa_ssid_value));
  6220. if (ns == NULL) {
  6221. *reply_len = -1;
  6222. goto done;
  6223. }
  6224. ssid = ns;
  6225. if ((end - pos) & 0x01 ||
  6226. end - pos > 2 * SSID_MAX_LEN ||
  6227. hexstr2bin(pos, ssid[ssid_count].ssid,
  6228. (end - pos) / 2) < 0) {
  6229. wpa_printf(MSG_DEBUG,
  6230. "Invalid SSID value '%s'",
  6231. pos);
  6232. *reply_len = -1;
  6233. goto done;
  6234. }
  6235. ssid[ssid_count].ssid_len = (end - pos) / 2;
  6236. wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
  6237. ssid[ssid_count].ssid,
  6238. ssid[ssid_count].ssid_len);
  6239. ssid_count++;
  6240. pos = end;
  6241. }
  6242. pos = os_strchr(pos, ' ');
  6243. if (pos)
  6244. pos++;
  6245. }
  6246. }
  6247. wpa_s->num_ssids_from_scan_req = ssid_count;
  6248. os_free(wpa_s->ssids_from_scan_req);
  6249. if (ssid_count) {
  6250. wpa_s->ssids_from_scan_req = ssid;
  6251. ssid = NULL;
  6252. } else {
  6253. wpa_s->ssids_from_scan_req = NULL;
  6254. }
  6255. if (scan_only)
  6256. scan_res_handler = scan_only_handler;
  6257. else if (wpa_s->scan_res_handler == scan_only_handler)
  6258. scan_res_handler = NULL;
  6259. else
  6260. scan_res_handler = wpa_s->scan_res_handler;
  6261. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  6262. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  6263. (wpa_s->wpa_state == WPA_COMPLETED))) {
  6264. wpa_s->manual_scan_passive = manual_scan_passive;
  6265. wpa_s->manual_scan_use_id = manual_scan_use_id;
  6266. wpa_s->manual_scan_only_new = manual_scan_only_new;
  6267. wpa_s->scan_id_count = scan_id_count;
  6268. os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
  6269. wpa_s->scan_res_handler = scan_res_handler;
  6270. os_free(wpa_s->manual_scan_freqs);
  6271. wpa_s->manual_scan_freqs = manual_scan_freqs;
  6272. manual_scan_freqs = NULL;
  6273. wpa_s->normal_scans = 0;
  6274. wpa_s->scan_req = MANUAL_SCAN_REQ;
  6275. wpa_s->after_wps = 0;
  6276. wpa_s->known_wps_freq = 0;
  6277. wpa_supplicant_req_scan(wpa_s, 0, 0);
  6278. if (wpa_s->manual_scan_use_id) {
  6279. wpa_s->manual_scan_id++;
  6280. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  6281. wpa_s->manual_scan_id);
  6282. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  6283. wpa_s->manual_scan_id);
  6284. }
  6285. } else if (wpa_s->sched_scanning) {
  6286. wpa_s->manual_scan_passive = manual_scan_passive;
  6287. wpa_s->manual_scan_use_id = manual_scan_use_id;
  6288. wpa_s->manual_scan_only_new = manual_scan_only_new;
  6289. wpa_s->scan_id_count = scan_id_count;
  6290. os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
  6291. wpa_s->scan_res_handler = scan_res_handler;
  6292. os_free(wpa_s->manual_scan_freqs);
  6293. wpa_s->manual_scan_freqs = manual_scan_freqs;
  6294. manual_scan_freqs = NULL;
  6295. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
  6296. wpa_supplicant_cancel_sched_scan(wpa_s);
  6297. wpa_s->scan_req = MANUAL_SCAN_REQ;
  6298. wpa_supplicant_req_scan(wpa_s, 0, 0);
  6299. if (wpa_s->manual_scan_use_id) {
  6300. wpa_s->manual_scan_id++;
  6301. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  6302. wpa_s->manual_scan_id);
  6303. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  6304. wpa_s->manual_scan_id);
  6305. }
  6306. } else {
  6307. wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
  6308. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  6309. }
  6310. done:
  6311. os_free(manual_scan_freqs);
  6312. os_free(ssid);
  6313. }
  6314. #ifdef CONFIG_TESTING_OPTIONS
  6315. static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
  6316. unsigned int freq, const u8 *dst,
  6317. const u8 *src, const u8 *bssid,
  6318. const u8 *data, size_t data_len,
  6319. enum offchannel_send_action_result
  6320. result)
  6321. {
  6322. wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
  6323. " src=" MACSTR " bssid=" MACSTR " result=%s",
  6324. freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
  6325. result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
  6326. "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
  6327. "NO_ACK" : "FAILED"));
  6328. }
  6329. static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
  6330. {
  6331. char *pos, *param;
  6332. size_t len;
  6333. u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
  6334. int res, used;
  6335. int freq = 0, no_cck = 0, wait_time = 0;
  6336. /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
  6337. * <action=Action frame payload> */
  6338. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  6339. pos = cmd;
  6340. used = hwaddr_aton2(pos, da);
  6341. if (used < 0)
  6342. return -1;
  6343. pos += used;
  6344. while (*pos == ' ')
  6345. pos++;
  6346. used = hwaddr_aton2(pos, bssid);
  6347. if (used < 0)
  6348. return -1;
  6349. pos += used;
  6350. param = os_strstr(pos, " freq=");
  6351. if (param) {
  6352. param += 6;
  6353. freq = atoi(param);
  6354. }
  6355. param = os_strstr(pos, " no_cck=");
  6356. if (param) {
  6357. param += 8;
  6358. no_cck = atoi(param);
  6359. }
  6360. param = os_strstr(pos, " wait_time=");
  6361. if (param) {
  6362. param += 11;
  6363. wait_time = atoi(param);
  6364. }
  6365. param = os_strstr(pos, " action=");
  6366. if (param == NULL)
  6367. return -1;
  6368. param += 8;
  6369. len = os_strlen(param);
  6370. if (len & 1)
  6371. return -1;
  6372. len /= 2;
  6373. buf = os_malloc(len);
  6374. if (buf == NULL)
  6375. return -1;
  6376. if (hexstr2bin(param, buf, len) < 0) {
  6377. os_free(buf);
  6378. return -1;
  6379. }
  6380. res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
  6381. buf, len, wait_time,
  6382. wpas_ctrl_iface_mgmt_tx_cb, no_cck);
  6383. os_free(buf);
  6384. return res;
  6385. }
  6386. static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
  6387. {
  6388. wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
  6389. offchannel_send_action_done(wpa_s);
  6390. }
  6391. static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
  6392. {
  6393. char *pos, *param;
  6394. union wpa_event_data event;
  6395. enum wpa_event_type ev;
  6396. /* <event name> [parameters..] */
  6397. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
  6398. pos = cmd;
  6399. param = os_strchr(pos, ' ');
  6400. if (param)
  6401. *param++ = '\0';
  6402. os_memset(&event, 0, sizeof(event));
  6403. if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
  6404. ev = EVENT_INTERFACE_ENABLED;
  6405. } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
  6406. ev = EVENT_INTERFACE_DISABLED;
  6407. } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
  6408. ev = EVENT_AVOID_FREQUENCIES;
  6409. if (param == NULL)
  6410. param = "";
  6411. if (freq_range_list_parse(&event.freq_range, param) < 0)
  6412. return -1;
  6413. wpa_supplicant_event(wpa_s, ev, &event);
  6414. os_free(event.freq_range.range);
  6415. return 0;
  6416. } else {
  6417. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
  6418. cmd);
  6419. return -1;
  6420. }
  6421. wpa_supplicant_event(wpa_s, ev, &event);
  6422. return 0;
  6423. }
  6424. static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
  6425. {
  6426. char *pos;
  6427. u8 src[ETH_ALEN], *buf;
  6428. int used;
  6429. size_t len;
  6430. wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
  6431. pos = cmd;
  6432. used = hwaddr_aton2(pos, src);
  6433. if (used < 0)
  6434. return -1;
  6435. pos += used;
  6436. while (*pos == ' ')
  6437. pos++;
  6438. len = os_strlen(pos);
  6439. if (len & 1)
  6440. return -1;
  6441. len /= 2;
  6442. buf = os_malloc(len);
  6443. if (buf == NULL)
  6444. return -1;
  6445. if (hexstr2bin(pos, buf, len) < 0) {
  6446. os_free(buf);
  6447. return -1;
  6448. }
  6449. wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
  6450. os_free(buf);
  6451. return 0;
  6452. }
  6453. static u16 ipv4_hdr_checksum(const void *buf, size_t len)
  6454. {
  6455. size_t i;
  6456. u32 sum = 0;
  6457. const u16 *pos = buf;
  6458. for (i = 0; i < len / 2; i++)
  6459. sum += *pos++;
  6460. while (sum >> 16)
  6461. sum = (sum & 0xffff) + (sum >> 16);
  6462. return sum ^ 0xffff;
  6463. }
  6464. #define HWSIM_PACKETLEN 1500
  6465. #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
  6466. void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
  6467. {
  6468. struct wpa_supplicant *wpa_s = ctx;
  6469. const struct ether_header *eth;
  6470. struct iphdr ip;
  6471. const u8 *pos;
  6472. unsigned int i;
  6473. if (len != HWSIM_PACKETLEN)
  6474. return;
  6475. eth = (const struct ether_header *) buf;
  6476. os_memcpy(&ip, eth + 1, sizeof(ip));
  6477. pos = &buf[sizeof(*eth) + sizeof(ip)];
  6478. if (ip.ihl != 5 || ip.version != 4 ||
  6479. ntohs(ip.tot_len) != HWSIM_IP_LEN)
  6480. return;
  6481. for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
  6482. if (*pos != (u8) i)
  6483. return;
  6484. pos++;
  6485. }
  6486. wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
  6487. MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
  6488. }
  6489. static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
  6490. char *cmd)
  6491. {
  6492. int enabled = atoi(cmd);
  6493. char *pos;
  6494. const char *ifname;
  6495. if (!enabled) {
  6496. if (wpa_s->l2_test) {
  6497. l2_packet_deinit(wpa_s->l2_test);
  6498. wpa_s->l2_test = NULL;
  6499. wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
  6500. }
  6501. return 0;
  6502. }
  6503. if (wpa_s->l2_test)
  6504. return 0;
  6505. pos = os_strstr(cmd, " ifname=");
  6506. if (pos)
  6507. ifname = pos + 8;
  6508. else
  6509. ifname = wpa_s->ifname;
  6510. wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
  6511. ETHERTYPE_IP, wpas_data_test_rx,
  6512. wpa_s, 1);
  6513. if (wpa_s->l2_test == NULL)
  6514. return -1;
  6515. wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
  6516. return 0;
  6517. }
  6518. static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
  6519. {
  6520. u8 dst[ETH_ALEN], src[ETH_ALEN];
  6521. char *pos;
  6522. int used;
  6523. long int val;
  6524. u8 tos;
  6525. u8 buf[2 + HWSIM_PACKETLEN];
  6526. struct ether_header *eth;
  6527. struct iphdr *ip;
  6528. u8 *dpos;
  6529. unsigned int i;
  6530. if (wpa_s->l2_test == NULL)
  6531. return -1;
  6532. /* format: <dst> <src> <tos> */
  6533. pos = cmd;
  6534. used = hwaddr_aton2(pos, dst);
  6535. if (used < 0)
  6536. return -1;
  6537. pos += used;
  6538. while (*pos == ' ')
  6539. pos++;
  6540. used = hwaddr_aton2(pos, src);
  6541. if (used < 0)
  6542. return -1;
  6543. pos += used;
  6544. val = strtol(pos, NULL, 0);
  6545. if (val < 0 || val > 0xff)
  6546. return -1;
  6547. tos = val;
  6548. eth = (struct ether_header *) &buf[2];
  6549. os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
  6550. os_memcpy(eth->ether_shost, src, ETH_ALEN);
  6551. eth->ether_type = htons(ETHERTYPE_IP);
  6552. ip = (struct iphdr *) (eth + 1);
  6553. os_memset(ip, 0, sizeof(*ip));
  6554. ip->ihl = 5;
  6555. ip->version = 4;
  6556. ip->ttl = 64;
  6557. ip->tos = tos;
  6558. ip->tot_len = htons(HWSIM_IP_LEN);
  6559. ip->protocol = 1;
  6560. ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
  6561. ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
  6562. ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
  6563. dpos = (u8 *) (ip + 1);
  6564. for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
  6565. *dpos++ = i;
  6566. if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
  6567. HWSIM_PACKETLEN) < 0)
  6568. return -1;
  6569. wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
  6570. " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
  6571. return 0;
  6572. }
  6573. static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
  6574. char *cmd)
  6575. {
  6576. u8 *buf;
  6577. struct ether_header *eth;
  6578. struct l2_packet_data *l2 = NULL;
  6579. size_t len;
  6580. u16 ethertype;
  6581. int res = -1;
  6582. len = os_strlen(cmd);
  6583. if (len & 1 || len < ETH_HLEN * 2)
  6584. return -1;
  6585. len /= 2;
  6586. buf = os_malloc(len);
  6587. if (buf == NULL)
  6588. return -1;
  6589. if (hexstr2bin(cmd, buf, len) < 0)
  6590. goto done;
  6591. eth = (struct ether_header *) buf;
  6592. ethertype = ntohs(eth->ether_type);
  6593. l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
  6594. wpas_data_test_rx, wpa_s, 1);
  6595. if (l2 == NULL)
  6596. goto done;
  6597. res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
  6598. wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
  6599. done:
  6600. if (l2)
  6601. l2_packet_deinit(l2);
  6602. os_free(buf);
  6603. return res < 0 ? -1 : 0;
  6604. }
  6605. static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
  6606. {
  6607. #ifdef WPA_TRACE_BFD
  6608. extern char wpa_trace_fail_func[256];
  6609. extern unsigned int wpa_trace_fail_after;
  6610. char *pos;
  6611. wpa_trace_fail_after = atoi(cmd);
  6612. pos = os_strchr(cmd, ':');
  6613. if (pos) {
  6614. pos++;
  6615. os_strlcpy(wpa_trace_fail_func, pos,
  6616. sizeof(wpa_trace_fail_func));
  6617. } else {
  6618. wpa_trace_fail_after = 0;
  6619. }
  6620. return 0;
  6621. #else /* WPA_TRACE_BFD */
  6622. return -1;
  6623. #endif /* WPA_TRACE_BFD */
  6624. }
  6625. static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
  6626. char *buf, size_t buflen)
  6627. {
  6628. #ifdef WPA_TRACE_BFD
  6629. extern char wpa_trace_fail_func[256];
  6630. extern unsigned int wpa_trace_fail_after;
  6631. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
  6632. wpa_trace_fail_func);
  6633. #else /* WPA_TRACE_BFD */
  6634. return -1;
  6635. #endif /* WPA_TRACE_BFD */
  6636. }
  6637. static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd)
  6638. {
  6639. #ifdef WPA_TRACE_BFD
  6640. extern char wpa_trace_test_fail_func[256];
  6641. extern unsigned int wpa_trace_test_fail_after;
  6642. char *pos;
  6643. wpa_trace_test_fail_after = atoi(cmd);
  6644. pos = os_strchr(cmd, ':');
  6645. if (pos) {
  6646. pos++;
  6647. os_strlcpy(wpa_trace_test_fail_func, pos,
  6648. sizeof(wpa_trace_test_fail_func));
  6649. } else {
  6650. wpa_trace_test_fail_after = 0;
  6651. }
  6652. return 0;
  6653. #else /* WPA_TRACE_BFD */
  6654. return -1;
  6655. #endif /* WPA_TRACE_BFD */
  6656. }
  6657. static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s,
  6658. char *buf, size_t buflen)
  6659. {
  6660. #ifdef WPA_TRACE_BFD
  6661. extern char wpa_trace_test_fail_func[256];
  6662. extern unsigned int wpa_trace_test_fail_after;
  6663. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
  6664. wpa_trace_test_fail_func);
  6665. #else /* WPA_TRACE_BFD */
  6666. return -1;
  6667. #endif /* WPA_TRACE_BFD */
  6668. }
  6669. static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
  6670. {
  6671. struct wpa_supplicant *wpa_s = eloop_ctx;
  6672. int i, count = (intptr_t) timeout_ctx;
  6673. wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
  6674. count);
  6675. for (i = 0; i < count; i++) {
  6676. wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
  6677. i + 1, count);
  6678. }
  6679. }
  6680. static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
  6681. {
  6682. int count;
  6683. count = atoi(cmd);
  6684. if (count <= 0)
  6685. return -1;
  6686. return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
  6687. (void *) (intptr_t) count);
  6688. }
  6689. static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
  6690. const char *cmd)
  6691. {
  6692. struct wpabuf *buf;
  6693. size_t len;
  6694. len = os_strlen(cmd);
  6695. if (len & 1)
  6696. return -1;
  6697. len /= 2;
  6698. if (len == 0) {
  6699. buf = NULL;
  6700. } else {
  6701. buf = wpabuf_alloc(len);
  6702. if (buf == NULL)
  6703. return -1;
  6704. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  6705. wpabuf_free(buf);
  6706. return -1;
  6707. }
  6708. }
  6709. wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
  6710. return 0;
  6711. }
  6712. #endif /* CONFIG_TESTING_OPTIONS */
  6713. static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
  6714. {
  6715. char *pos = cmd;
  6716. int frame;
  6717. size_t len;
  6718. struct wpabuf *buf;
  6719. struct ieee802_11_elems elems;
  6720. frame = atoi(pos);
  6721. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  6722. return -1;
  6723. wpa_s = wpas_vendor_elem(wpa_s, frame);
  6724. pos = os_strchr(pos, ' ');
  6725. if (pos == NULL)
  6726. return -1;
  6727. pos++;
  6728. len = os_strlen(pos);
  6729. if (len == 0)
  6730. return 0;
  6731. if (len & 1)
  6732. return -1;
  6733. len /= 2;
  6734. buf = wpabuf_alloc(len);
  6735. if (buf == NULL)
  6736. return -1;
  6737. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  6738. wpabuf_free(buf);
  6739. return -1;
  6740. }
  6741. if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
  6742. ParseFailed) {
  6743. wpabuf_free(buf);
  6744. return -1;
  6745. }
  6746. if (wpa_s->vendor_elem[frame] == NULL) {
  6747. wpa_s->vendor_elem[frame] = buf;
  6748. wpas_vendor_elem_update(wpa_s);
  6749. return 0;
  6750. }
  6751. if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
  6752. wpabuf_free(buf);
  6753. return -1;
  6754. }
  6755. wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
  6756. wpabuf_free(buf);
  6757. wpas_vendor_elem_update(wpa_s);
  6758. return 0;
  6759. }
  6760. static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
  6761. char *buf, size_t buflen)
  6762. {
  6763. int frame = atoi(cmd);
  6764. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  6765. return -1;
  6766. wpa_s = wpas_vendor_elem(wpa_s, frame);
  6767. if (wpa_s->vendor_elem[frame] == NULL)
  6768. return 0;
  6769. return wpa_snprintf_hex(buf, buflen,
  6770. wpabuf_head_u8(wpa_s->vendor_elem[frame]),
  6771. wpabuf_len(wpa_s->vendor_elem[frame]));
  6772. }
  6773. static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
  6774. {
  6775. char *pos = cmd;
  6776. int frame;
  6777. size_t len;
  6778. u8 *buf;
  6779. struct ieee802_11_elems elems;
  6780. int res;
  6781. frame = atoi(pos);
  6782. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  6783. return -1;
  6784. wpa_s = wpas_vendor_elem(wpa_s, frame);
  6785. pos = os_strchr(pos, ' ');
  6786. if (pos == NULL)
  6787. return -1;
  6788. pos++;
  6789. if (*pos == '*') {
  6790. wpabuf_free(wpa_s->vendor_elem[frame]);
  6791. wpa_s->vendor_elem[frame] = NULL;
  6792. wpas_vendor_elem_update(wpa_s);
  6793. return 0;
  6794. }
  6795. if (wpa_s->vendor_elem[frame] == NULL)
  6796. return -1;
  6797. len = os_strlen(pos);
  6798. if (len == 0)
  6799. return 0;
  6800. if (len & 1)
  6801. return -1;
  6802. len /= 2;
  6803. buf = os_malloc(len);
  6804. if (buf == NULL)
  6805. return -1;
  6806. if (hexstr2bin(pos, buf, len) < 0) {
  6807. os_free(buf);
  6808. return -1;
  6809. }
  6810. if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
  6811. os_free(buf);
  6812. return -1;
  6813. }
  6814. res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
  6815. os_free(buf);
  6816. return res;
  6817. }
  6818. static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
  6819. {
  6820. struct wpa_supplicant *wpa_s = ctx;
  6821. if (neighbor_rep) {
  6822. wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
  6823. "length=%u",
  6824. (unsigned int) wpabuf_len(neighbor_rep));
  6825. wpabuf_free(neighbor_rep);
  6826. } else {
  6827. wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
  6828. }
  6829. }
  6830. static int wpas_ctrl_iface_send_neigbor_rep(struct wpa_supplicant *wpa_s,
  6831. char *cmd)
  6832. {
  6833. struct wpa_ssid ssid;
  6834. struct wpa_ssid *ssid_p = NULL;
  6835. int ret = 0;
  6836. if (os_strncmp(cmd, " ssid=", 6) == 0) {
  6837. ssid.ssid_len = os_strlen(cmd + 6);
  6838. if (ssid.ssid_len > SSID_MAX_LEN)
  6839. return -1;
  6840. ssid.ssid = (u8 *) (cmd + 6);
  6841. ssid_p = &ssid;
  6842. }
  6843. ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p,
  6844. wpas_ctrl_neighbor_rep_cb,
  6845. wpa_s);
  6846. return ret;
  6847. }
  6848. static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
  6849. {
  6850. eapol_sm_erp_flush(wpa_s->eapol);
  6851. return 0;
  6852. }
  6853. static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
  6854. char *cmd)
  6855. {
  6856. char *token, *context = NULL;
  6857. unsigned int enable = ~0, type = 0;
  6858. u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
  6859. u8 *addr = NULL, *mask = NULL;
  6860. while ((token = str_token(cmd, " ", &context))) {
  6861. if (os_strcasecmp(token, "scan") == 0) {
  6862. type |= MAC_ADDR_RAND_SCAN;
  6863. } else if (os_strcasecmp(token, "sched") == 0) {
  6864. type |= MAC_ADDR_RAND_SCHED_SCAN;
  6865. } else if (os_strcasecmp(token, "pno") == 0) {
  6866. type |= MAC_ADDR_RAND_PNO;
  6867. } else if (os_strcasecmp(token, "all") == 0) {
  6868. type = wpa_s->mac_addr_rand_supported;
  6869. } else if (os_strncasecmp(token, "enable=", 7) == 0) {
  6870. enable = atoi(token + 7);
  6871. } else if (os_strncasecmp(token, "addr=", 5) == 0) {
  6872. addr = _addr;
  6873. if (hwaddr_aton(token + 5, addr)) {
  6874. wpa_printf(MSG_INFO,
  6875. "CTRL: Invalid MAC address: %s",
  6876. token);
  6877. return -1;
  6878. }
  6879. } else if (os_strncasecmp(token, "mask=", 5) == 0) {
  6880. mask = _mask;
  6881. if (hwaddr_aton(token + 5, mask)) {
  6882. wpa_printf(MSG_INFO,
  6883. "CTRL: Invalid MAC address mask: %s",
  6884. token);
  6885. return -1;
  6886. }
  6887. } else {
  6888. wpa_printf(MSG_INFO,
  6889. "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
  6890. token);
  6891. return -1;
  6892. }
  6893. }
  6894. if (!type) {
  6895. wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
  6896. return -1;
  6897. }
  6898. if ((wpa_s->mac_addr_rand_supported & type) != type) {
  6899. wpa_printf(MSG_INFO,
  6900. "CTRL: MAC_RAND_SCAN types=%u != supported=%u",
  6901. type, wpa_s->mac_addr_rand_supported);
  6902. return -1;
  6903. }
  6904. if (enable > 1) {
  6905. wpa_printf(MSG_INFO,
  6906. "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
  6907. return -1;
  6908. }
  6909. if (!enable) {
  6910. wpas_mac_addr_rand_scan_clear(wpa_s, type);
  6911. if (wpa_s->pno) {
  6912. if (type & MAC_ADDR_RAND_PNO) {
  6913. wpas_stop_pno(wpa_s);
  6914. wpas_start_pno(wpa_s);
  6915. }
  6916. } else if (wpa_s->sched_scanning &&
  6917. (type & MAC_ADDR_RAND_SCHED_SCAN)) {
  6918. /* simulate timeout to restart the sched scan */
  6919. wpa_s->sched_scan_timed_out = 1;
  6920. wpa_s->prev_sched_ssid = NULL;
  6921. wpa_supplicant_cancel_sched_scan(wpa_s);
  6922. }
  6923. return 0;
  6924. }
  6925. if ((addr && !mask) || (!addr && mask)) {
  6926. wpa_printf(MSG_INFO,
  6927. "CTRL: MAC_RAND_SCAN invalid addr/mask combination");
  6928. return -1;
  6929. }
  6930. if (addr && mask && (!(mask[0] & 0x01) || (addr[0] & 0x01))) {
  6931. wpa_printf(MSG_INFO,
  6932. "CTRL: MAC_RAND_SCAN cannot allow multicast address");
  6933. return -1;
  6934. }
  6935. if (type & MAC_ADDR_RAND_SCAN) {
  6936. wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCAN,
  6937. addr, mask);
  6938. }
  6939. if (type & MAC_ADDR_RAND_SCHED_SCAN) {
  6940. wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCHED_SCAN,
  6941. addr, mask);
  6942. if (wpa_s->sched_scanning && !wpa_s->pno) {
  6943. /* simulate timeout to restart the sched scan */
  6944. wpa_s->sched_scan_timed_out = 1;
  6945. wpa_s->prev_sched_ssid = NULL;
  6946. wpa_supplicant_cancel_sched_scan(wpa_s);
  6947. }
  6948. }
  6949. if (type & MAC_ADDR_RAND_PNO) {
  6950. wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_PNO,
  6951. addr, mask);
  6952. if (wpa_s->pno) {
  6953. wpas_stop_pno(wpa_s);
  6954. wpas_start_pno(wpa_s);
  6955. }
  6956. }
  6957. return 0;
  6958. }
  6959. static int wpas_ctrl_cmd_debug_level(const char *cmd)
  6960. {
  6961. if (os_strcmp(cmd, "PING") == 0 ||
  6962. os_strncmp(cmd, "BSS ", 4) == 0 ||
  6963. os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
  6964. os_strncmp(cmd, "STATUS", 6) == 0 ||
  6965. os_strncmp(cmd, "STA ", 4) == 0 ||
  6966. os_strncmp(cmd, "STA-", 4) == 0)
  6967. return MSG_EXCESSIVE;
  6968. return MSG_DEBUG;
  6969. }
  6970. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  6971. char *buf, size_t *resp_len)
  6972. {
  6973. char *reply;
  6974. const int reply_size = 4096;
  6975. int reply_len;
  6976. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  6977. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  6978. if (wpa_debug_show_keys)
  6979. wpa_dbg(wpa_s, MSG_DEBUG,
  6980. "Control interface command '%s'", buf);
  6981. else
  6982. wpa_dbg(wpa_s, MSG_DEBUG,
  6983. "Control interface command '%s [REMOVED]'",
  6984. os_strncmp(buf, WPA_CTRL_RSP,
  6985. os_strlen(WPA_CTRL_RSP)) == 0 ?
  6986. WPA_CTRL_RSP : "SET_NETWORK");
  6987. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  6988. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
  6989. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  6990. (const u8 *) buf, os_strlen(buf));
  6991. } else {
  6992. int level = wpas_ctrl_cmd_debug_level(buf);
  6993. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  6994. }
  6995. reply = os_malloc(reply_size);
  6996. if (reply == NULL) {
  6997. *resp_len = 1;
  6998. return NULL;
  6999. }
  7000. os_memcpy(reply, "OK\n", 3);
  7001. reply_len = 3;
  7002. if (os_strcmp(buf, "PING") == 0) {
  7003. os_memcpy(reply, "PONG\n", 5);
  7004. reply_len = 5;
  7005. } else if (os_strcmp(buf, "IFNAME") == 0) {
  7006. reply_len = os_strlen(wpa_s->ifname);
  7007. os_memcpy(reply, wpa_s->ifname, reply_len);
  7008. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  7009. if (wpa_debug_reopen_file() < 0)
  7010. reply_len = -1;
  7011. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  7012. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  7013. } else if (os_strcmp(buf, "MIB") == 0) {
  7014. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  7015. if (reply_len >= 0) {
  7016. reply_len += eapol_sm_get_mib(wpa_s->eapol,
  7017. reply + reply_len,
  7018. reply_size - reply_len);
  7019. }
  7020. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  7021. reply_len = wpa_supplicant_ctrl_iface_status(
  7022. wpa_s, buf + 6, reply, reply_size);
  7023. } else if (os_strcmp(buf, "PMKSA") == 0) {
  7024. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  7025. reply_size);
  7026. } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
  7027. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  7028. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  7029. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  7030. reply_len = -1;
  7031. } else if (os_strncmp(buf, "DUMP", 4) == 0) {
  7032. reply_len = wpa_config_dump_values(wpa_s->conf,
  7033. reply, reply_size);
  7034. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  7035. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  7036. reply, reply_size);
  7037. } else if (os_strcmp(buf, "LOGON") == 0) {
  7038. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  7039. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  7040. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  7041. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  7042. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  7043. reply_len = -1;
  7044. else
  7045. wpas_request_connection(wpa_s);
  7046. } else if (os_strcmp(buf, "REATTACH") == 0) {
  7047. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
  7048. !wpa_s->current_ssid)
  7049. reply_len = -1;
  7050. else {
  7051. wpa_s->reattach = 1;
  7052. wpas_request_connection(wpa_s);
  7053. }
  7054. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  7055. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  7056. reply_len = -1;
  7057. else if (wpa_s->disconnected)
  7058. wpas_request_connection(wpa_s);
  7059. #ifdef IEEE8021X_EAPOL
  7060. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  7061. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  7062. reply_len = -1;
  7063. #endif /* IEEE8021X_EAPOL */
  7064. #ifdef CONFIG_PEERKEY
  7065. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  7066. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  7067. reply_len = -1;
  7068. #endif /* CONFIG_PEERKEY */
  7069. #ifdef CONFIG_IEEE80211R
  7070. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  7071. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  7072. reply_len = -1;
  7073. #endif /* CONFIG_IEEE80211R */
  7074. #ifdef CONFIG_WPS
  7075. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  7076. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  7077. if (res == -2) {
  7078. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  7079. reply_len = 17;
  7080. } else if (res)
  7081. reply_len = -1;
  7082. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  7083. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  7084. if (res == -2) {
  7085. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  7086. reply_len = 17;
  7087. } else if (res)
  7088. reply_len = -1;
  7089. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  7090. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  7091. reply,
  7092. reply_size);
  7093. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  7094. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  7095. wpa_s, buf + 14, reply, reply_size);
  7096. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  7097. if (wpas_wps_cancel(wpa_s))
  7098. reply_len = -1;
  7099. #ifdef CONFIG_WPS_NFC
  7100. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  7101. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  7102. reply_len = -1;
  7103. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  7104. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  7105. reply_len = -1;
  7106. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  7107. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  7108. wpa_s, buf + 21, reply, reply_size);
  7109. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  7110. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  7111. wpa_s, buf + 14, reply, reply_size);
  7112. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  7113. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  7114. buf + 17))
  7115. reply_len = -1;
  7116. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  7117. reply_len = wpas_ctrl_nfc_get_handover_req(
  7118. wpa_s, buf + 21, reply, reply_size);
  7119. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  7120. reply_len = wpas_ctrl_nfc_get_handover_sel(
  7121. wpa_s, buf + 21, reply, reply_size);
  7122. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  7123. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  7124. reply_len = -1;
  7125. #endif /* CONFIG_WPS_NFC */
  7126. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  7127. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  7128. reply_len = -1;
  7129. #ifdef CONFIG_AP
  7130. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  7131. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  7132. wpa_s, buf + 11, reply, reply_size);
  7133. #endif /* CONFIG_AP */
  7134. #ifdef CONFIG_WPS_ER
  7135. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  7136. if (wpas_wps_er_start(wpa_s, NULL))
  7137. reply_len = -1;
  7138. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  7139. if (wpas_wps_er_start(wpa_s, buf + 13))
  7140. reply_len = -1;
  7141. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  7142. wpas_wps_er_stop(wpa_s);
  7143. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  7144. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  7145. reply_len = -1;
  7146. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  7147. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  7148. if (ret == -2) {
  7149. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  7150. reply_len = 17;
  7151. } else if (ret == -3) {
  7152. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  7153. reply_len = 18;
  7154. } else if (ret == -4) {
  7155. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  7156. reply_len = 20;
  7157. } else if (ret)
  7158. reply_len = -1;
  7159. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  7160. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  7161. reply_len = -1;
  7162. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  7163. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  7164. buf + 18))
  7165. reply_len = -1;
  7166. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  7167. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  7168. reply_len = -1;
  7169. #ifdef CONFIG_WPS_NFC
  7170. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  7171. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  7172. wpa_s, buf + 24, reply, reply_size);
  7173. #endif /* CONFIG_WPS_NFC */
  7174. #endif /* CONFIG_WPS_ER */
  7175. #endif /* CONFIG_WPS */
  7176. #ifdef CONFIG_IBSS_RSN
  7177. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  7178. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  7179. reply_len = -1;
  7180. #endif /* CONFIG_IBSS_RSN */
  7181. #ifdef CONFIG_MESH
  7182. } else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
  7183. reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
  7184. wpa_s, buf + 19, reply, reply_size);
  7185. } else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
  7186. reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
  7187. wpa_s, "", reply, reply_size);
  7188. } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
  7189. if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
  7190. reply_len = -1;
  7191. } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
  7192. if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
  7193. buf + 18))
  7194. reply_len = -1;
  7195. #endif /* CONFIG_MESH */
  7196. #ifdef CONFIG_P2P
  7197. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  7198. if (p2p_ctrl_find(wpa_s, buf + 8))
  7199. reply_len = -1;
  7200. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  7201. if (p2p_ctrl_find(wpa_s, ""))
  7202. reply_len = -1;
  7203. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  7204. wpas_p2p_stop_find(wpa_s);
  7205. } else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
  7206. if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
  7207. reply_len = -1;
  7208. } else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
  7209. if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
  7210. reply_len = -1;
  7211. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  7212. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  7213. reply_size);
  7214. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  7215. if (p2p_ctrl_listen(wpa_s, buf + 11))
  7216. reply_len = -1;
  7217. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  7218. if (p2p_ctrl_listen(wpa_s, ""))
  7219. reply_len = -1;
  7220. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  7221. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  7222. reply_len = -1;
  7223. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  7224. if (p2p_ctrl_group_add(wpa_s, ""))
  7225. reply_len = -1;
  7226. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  7227. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  7228. reply_len = -1;
  7229. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  7230. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  7231. reply_len = -1;
  7232. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  7233. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  7234. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  7235. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  7236. reply_size);
  7237. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  7238. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  7239. reply_len = -1;
  7240. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  7241. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  7242. reply_len = -1;
  7243. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  7244. wpas_p2p_sd_service_update(wpa_s);
  7245. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  7246. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  7247. reply_len = -1;
  7248. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  7249. wpas_p2p_service_flush(wpa_s);
  7250. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  7251. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  7252. reply_len = -1;
  7253. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  7254. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  7255. reply_len = -1;
  7256. } else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
  7257. if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
  7258. reply_len = -1;
  7259. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  7260. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  7261. reply_len = -1;
  7262. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  7263. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  7264. reply_len = -1;
  7265. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  7266. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  7267. reply_size);
  7268. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  7269. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  7270. reply_len = -1;
  7271. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  7272. p2p_ctrl_flush(wpa_s);
  7273. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  7274. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  7275. reply_len = -1;
  7276. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  7277. if (wpas_p2p_cancel(wpa_s))
  7278. reply_len = -1;
  7279. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  7280. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  7281. reply_len = -1;
  7282. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  7283. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  7284. reply_len = -1;
  7285. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  7286. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  7287. reply_len = -1;
  7288. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  7289. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  7290. reply_len = -1;
  7291. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  7292. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  7293. reply_len = -1;
  7294. #endif /* CONFIG_P2P */
  7295. #ifdef CONFIG_WIFI_DISPLAY
  7296. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  7297. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  7298. reply_len = -1;
  7299. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  7300. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  7301. reply, reply_size);
  7302. #endif /* CONFIG_WIFI_DISPLAY */
  7303. #ifdef CONFIG_INTERWORKING
  7304. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  7305. if (interworking_fetch_anqp(wpa_s) < 0)
  7306. reply_len = -1;
  7307. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  7308. interworking_stop_fetch_anqp(wpa_s);
  7309. } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
  7310. if (ctrl_interworking_select(wpa_s, NULL) < 0)
  7311. reply_len = -1;
  7312. } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
  7313. if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
  7314. reply_len = -1;
  7315. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  7316. if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
  7317. reply_len = -1;
  7318. } else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
  7319. int id;
  7320. id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
  7321. if (id < 0)
  7322. reply_len = -1;
  7323. else {
  7324. reply_len = os_snprintf(reply, reply_size, "%d\n", id);
  7325. if (os_snprintf_error(reply_size, reply_len))
  7326. reply_len = -1;
  7327. }
  7328. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  7329. if (get_anqp(wpa_s, buf + 9) < 0)
  7330. reply_len = -1;
  7331. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  7332. if (gas_request(wpa_s, buf + 12) < 0)
  7333. reply_len = -1;
  7334. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  7335. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  7336. reply_size);
  7337. #endif /* CONFIG_INTERWORKING */
  7338. #ifdef CONFIG_HS20
  7339. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  7340. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  7341. reply_len = -1;
  7342. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  7343. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  7344. reply_len = -1;
  7345. } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
  7346. if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
  7347. reply_len = -1;
  7348. } else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
  7349. if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
  7350. reply_len = -1;
  7351. } else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
  7352. reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
  7353. } else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
  7354. if (del_hs20_icon(wpa_s, buf + 14) < 0)
  7355. reply_len = -1;
  7356. } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
  7357. if (hs20_fetch_osu(wpa_s) < 0)
  7358. reply_len = -1;
  7359. } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
  7360. hs20_cancel_fetch_osu(wpa_s);
  7361. #endif /* CONFIG_HS20 */
  7362. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  7363. {
  7364. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  7365. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  7366. reply_len = -1;
  7367. else {
  7368. /*
  7369. * Notify response from timeout to allow the control
  7370. * interface response to be sent first.
  7371. */
  7372. eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
  7373. wpa_s, NULL);
  7374. }
  7375. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  7376. if (wpa_supplicant_reload_configuration(wpa_s))
  7377. reply_len = -1;
  7378. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  7379. wpa_supplicant_terminate_proc(wpa_s->global);
  7380. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  7381. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  7382. reply_len = -1;
  7383. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  7384. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  7385. wpa_s, buf + 9, reply, reply_size);
  7386. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  7387. reply_len = wpa_supplicant_ctrl_iface_log_level(
  7388. wpa_s, buf + 9, reply, reply_size);
  7389. } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
  7390. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  7391. wpa_s, buf + 14, reply, reply_size);
  7392. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  7393. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  7394. wpa_s, NULL, reply, reply_size);
  7395. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  7396. #ifdef CONFIG_SME
  7397. wpa_s->sme.prev_bssid_set = 0;
  7398. #endif /* CONFIG_SME */
  7399. wpa_s->reassociate = 0;
  7400. wpa_s->disconnected = 1;
  7401. wpa_supplicant_cancel_sched_scan(wpa_s);
  7402. wpa_supplicant_cancel_scan(wpa_s);
  7403. wpa_supplicant_deauthenticate(wpa_s,
  7404. WLAN_REASON_DEAUTH_LEAVING);
  7405. eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
  7406. } else if (os_strcmp(buf, "SCAN") == 0) {
  7407. wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
  7408. } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
  7409. wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
  7410. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  7411. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  7412. wpa_s, reply, reply_size);
  7413. } else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
  7414. if (wpas_abort_ongoing_scan(wpa_s) < 0)
  7415. reply_len = -1;
  7416. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  7417. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  7418. reply_len = -1;
  7419. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  7420. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  7421. reply_len = -1;
  7422. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  7423. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  7424. reply_len = -1;
  7425. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  7426. reply_len = wpa_supplicant_ctrl_iface_add_network(
  7427. wpa_s, reply, reply_size);
  7428. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  7429. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  7430. reply_len = -1;
  7431. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  7432. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  7433. reply_len = -1;
  7434. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  7435. reply_len = wpa_supplicant_ctrl_iface_get_network(
  7436. wpa_s, buf + 12, reply, reply_size);
  7437. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  7438. if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
  7439. wpa_s))
  7440. reply_len = -1;
  7441. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  7442. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  7443. wpa_s, reply, reply_size);
  7444. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  7445. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  7446. wpa_s, reply, reply_size);
  7447. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  7448. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  7449. reply_len = -1;
  7450. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  7451. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  7452. reply_len = -1;
  7453. } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
  7454. reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
  7455. reply,
  7456. reply_size);
  7457. #ifndef CONFIG_NO_CONFIG_WRITE
  7458. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  7459. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  7460. reply_len = -1;
  7461. #endif /* CONFIG_NO_CONFIG_WRITE */
  7462. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  7463. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  7464. wpa_s, buf + 15, reply, reply_size);
  7465. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  7466. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  7467. reply_len = -1;
  7468. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  7469. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  7470. reply_len = -1;
  7471. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  7472. reply_len = wpa_supplicant_global_iface_list(
  7473. wpa_s->global, reply, reply_size);
  7474. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  7475. reply_len = wpa_supplicant_global_iface_interfaces(
  7476. wpa_s->global, reply, reply_size);
  7477. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  7478. reply_len = wpa_supplicant_ctrl_iface_bss(
  7479. wpa_s, buf + 4, reply, reply_size);
  7480. #ifdef CONFIG_AP
  7481. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  7482. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  7483. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  7484. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  7485. reply_size);
  7486. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  7487. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  7488. reply_size);
  7489. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  7490. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  7491. reply_len = -1;
  7492. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  7493. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  7494. reply_len = -1;
  7495. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  7496. if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
  7497. reply_len = -1;
  7498. } else if (os_strcmp(buf, "STOP_AP") == 0) {
  7499. if (wpas_ap_stop_ap(wpa_s))
  7500. reply_len = -1;
  7501. #endif /* CONFIG_AP */
  7502. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  7503. wpas_notify_suspend(wpa_s->global);
  7504. } else if (os_strcmp(buf, "RESUME") == 0) {
  7505. wpas_notify_resume(wpa_s->global);
  7506. #ifdef CONFIG_TESTING_OPTIONS
  7507. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  7508. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  7509. #endif /* CONFIG_TESTING_OPTIONS */
  7510. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  7511. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  7512. reply_len = -1;
  7513. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  7514. wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
  7515. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  7516. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  7517. reply_len = -1;
  7518. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  7519. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  7520. buf + 17))
  7521. reply_len = -1;
  7522. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  7523. wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
  7524. #ifdef CONFIG_TDLS
  7525. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  7526. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  7527. reply_len = -1;
  7528. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  7529. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  7530. reply_len = -1;
  7531. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  7532. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  7533. reply_len = -1;
  7534. } else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
  7535. if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
  7536. buf + 17))
  7537. reply_len = -1;
  7538. } else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
  7539. if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
  7540. buf + 24))
  7541. reply_len = -1;
  7542. } else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
  7543. reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
  7544. wpa_s, buf + 17, reply, reply_size);
  7545. #endif /* CONFIG_TDLS */
  7546. } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
  7547. reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
  7548. } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
  7549. if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
  7550. reply_len = -1;
  7551. } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
  7552. if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
  7553. reply_len = -1;
  7554. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  7555. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  7556. reply_size);
  7557. } else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
  7558. if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
  7559. reply_len = -1;
  7560. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  7561. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  7562. reply_size);
  7563. #ifdef CONFIG_AUTOSCAN
  7564. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  7565. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  7566. reply_len = -1;
  7567. #endif /* CONFIG_AUTOSCAN */
  7568. #ifdef ANDROID
  7569. } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
  7570. reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
  7571. reply_size);
  7572. #endif /* ANDROID */
  7573. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  7574. reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
  7575. reply_size);
  7576. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  7577. pmksa_cache_clear_current(wpa_s->wpa);
  7578. eapol_sm_request_reauth(wpa_s->eapol);
  7579. #ifdef CONFIG_WNM
  7580. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  7581. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  7582. reply_len = -1;
  7583. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
  7584. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
  7585. reply_len = -1;
  7586. #endif /* CONFIG_WNM */
  7587. } else if (os_strcmp(buf, "FLUSH") == 0) {
  7588. wpa_supplicant_ctrl_iface_flush(wpa_s);
  7589. } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
  7590. reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
  7591. reply_size);
  7592. #ifdef CONFIG_TESTING_OPTIONS
  7593. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  7594. if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
  7595. reply_len = -1;
  7596. } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
  7597. wpas_ctrl_iface_mgmt_tx_done(wpa_s);
  7598. } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
  7599. if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
  7600. reply_len = -1;
  7601. } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
  7602. if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
  7603. reply_len = -1;
  7604. } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
  7605. if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
  7606. reply_len = -1;
  7607. } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
  7608. if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
  7609. reply_len = -1;
  7610. } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
  7611. if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
  7612. reply_len = -1;
  7613. } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
  7614. if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
  7615. reply_len = -1;
  7616. } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
  7617. reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
  7618. } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
  7619. if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0)
  7620. reply_len = -1;
  7621. } else if (os_strcmp(buf, "GET_FAIL") == 0) {
  7622. reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size);
  7623. } else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
  7624. if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
  7625. reply_len = -1;
  7626. } else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
  7627. if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
  7628. reply_len = -1;
  7629. #endif /* CONFIG_TESTING_OPTIONS */
  7630. } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
  7631. if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
  7632. reply_len = -1;
  7633. } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
  7634. reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
  7635. reply_size);
  7636. } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
  7637. if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
  7638. reply_len = -1;
  7639. } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
  7640. if (wpas_ctrl_iface_send_neigbor_rep(wpa_s, buf + 20))
  7641. reply_len = -1;
  7642. } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
  7643. wpas_ctrl_iface_erp_flush(wpa_s);
  7644. } else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
  7645. if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
  7646. reply_len = -1;
  7647. } else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
  7648. reply_len = wpas_ctrl_iface_get_pref_freq_list(
  7649. wpa_s, buf + 19, reply, reply_size);
  7650. } else {
  7651. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  7652. reply_len = 16;
  7653. }
  7654. if (reply_len < 0) {
  7655. os_memcpy(reply, "FAIL\n", 5);
  7656. reply_len = 5;
  7657. }
  7658. *resp_len = reply_len;
  7659. return reply;
  7660. }
  7661. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  7662. char *cmd)
  7663. {
  7664. struct wpa_interface iface;
  7665. char *pos, *extra;
  7666. struct wpa_supplicant *wpa_s;
  7667. unsigned int create_iface = 0;
  7668. u8 mac_addr[ETH_ALEN];
  7669. enum wpa_driver_if_type type = WPA_IF_STATION;
  7670. /*
  7671. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  7672. * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
  7673. */
  7674. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  7675. os_memset(&iface, 0, sizeof(iface));
  7676. do {
  7677. iface.ifname = pos = cmd;
  7678. pos = os_strchr(pos, '\t');
  7679. if (pos)
  7680. *pos++ = '\0';
  7681. if (iface.ifname[0] == '\0')
  7682. return -1;
  7683. if (pos == NULL)
  7684. break;
  7685. iface.confname = pos;
  7686. pos = os_strchr(pos, '\t');
  7687. if (pos)
  7688. *pos++ = '\0';
  7689. if (iface.confname[0] == '\0')
  7690. iface.confname = NULL;
  7691. if (pos == NULL)
  7692. break;
  7693. iface.driver = pos;
  7694. pos = os_strchr(pos, '\t');
  7695. if (pos)
  7696. *pos++ = '\0';
  7697. if (iface.driver[0] == '\0')
  7698. iface.driver = NULL;
  7699. if (pos == NULL)
  7700. break;
  7701. iface.ctrl_interface = pos;
  7702. pos = os_strchr(pos, '\t');
  7703. if (pos)
  7704. *pos++ = '\0';
  7705. if (iface.ctrl_interface[0] == '\0')
  7706. iface.ctrl_interface = NULL;
  7707. if (pos == NULL)
  7708. break;
  7709. iface.driver_param = pos;
  7710. pos = os_strchr(pos, '\t');
  7711. if (pos)
  7712. *pos++ = '\0';
  7713. if (iface.driver_param[0] == '\0')
  7714. iface.driver_param = NULL;
  7715. if (pos == NULL)
  7716. break;
  7717. iface.bridge_ifname = pos;
  7718. pos = os_strchr(pos, '\t');
  7719. if (pos)
  7720. *pos++ = '\0';
  7721. if (iface.bridge_ifname[0] == '\0')
  7722. iface.bridge_ifname = NULL;
  7723. if (pos == NULL)
  7724. break;
  7725. extra = pos;
  7726. pos = os_strchr(pos, '\t');
  7727. if (pos)
  7728. *pos++ = '\0';
  7729. if (!extra[0])
  7730. break;
  7731. if (os_strcmp(extra, "create") == 0) {
  7732. create_iface = 1;
  7733. if (!pos)
  7734. break;
  7735. if (os_strcmp(pos, "sta") == 0) {
  7736. type = WPA_IF_STATION;
  7737. } else if (os_strcmp(pos, "ap") == 0) {
  7738. type = WPA_IF_AP_BSS;
  7739. } else {
  7740. wpa_printf(MSG_DEBUG,
  7741. "INTERFACE_ADD unsupported interface type: '%s'",
  7742. pos);
  7743. return -1;
  7744. }
  7745. } else {
  7746. wpa_printf(MSG_DEBUG,
  7747. "INTERFACE_ADD unsupported extra parameter: '%s'",
  7748. extra);
  7749. return -1;
  7750. }
  7751. } while (0);
  7752. if (create_iface) {
  7753. wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
  7754. iface.ifname);
  7755. if (!global->ifaces)
  7756. return -1;
  7757. if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
  7758. NULL, NULL, NULL, mac_addr, NULL) < 0) {
  7759. wpa_printf(MSG_ERROR,
  7760. "CTRL_IFACE interface creation failed");
  7761. return -1;
  7762. }
  7763. wpa_printf(MSG_DEBUG,
  7764. "CTRL_IFACE interface '%s' created with MAC addr: "
  7765. MACSTR, iface.ifname, MAC2STR(mac_addr));
  7766. }
  7767. if (wpa_supplicant_get_iface(global, iface.ifname))
  7768. goto fail;
  7769. wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
  7770. if (!wpa_s)
  7771. goto fail;
  7772. wpa_s->added_vif = create_iface;
  7773. return 0;
  7774. fail:
  7775. if (create_iface)
  7776. wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
  7777. return -1;
  7778. }
  7779. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  7780. char *cmd)
  7781. {
  7782. struct wpa_supplicant *wpa_s;
  7783. int ret;
  7784. unsigned int delete_iface;
  7785. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  7786. wpa_s = wpa_supplicant_get_iface(global, cmd);
  7787. if (wpa_s == NULL)
  7788. return -1;
  7789. delete_iface = wpa_s->added_vif;
  7790. ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
  7791. if (!ret && delete_iface) {
  7792. wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
  7793. cmd);
  7794. ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
  7795. }
  7796. return ret;
  7797. }
  7798. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  7799. {
  7800. struct wpa_interface_info *prev;
  7801. while (iface) {
  7802. prev = iface;
  7803. iface = iface->next;
  7804. os_free(prev->ifname);
  7805. os_free(prev->desc);
  7806. os_free(prev);
  7807. }
  7808. }
  7809. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  7810. char *buf, int len)
  7811. {
  7812. int i, res;
  7813. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  7814. char *pos, *end;
  7815. for (i = 0; wpa_drivers[i]; i++) {
  7816. const struct wpa_driver_ops *drv = wpa_drivers[i];
  7817. if (drv->get_interfaces == NULL)
  7818. continue;
  7819. tmp = drv->get_interfaces(global->drv_priv[i]);
  7820. if (tmp == NULL)
  7821. continue;
  7822. if (last == NULL)
  7823. iface = last = tmp;
  7824. else
  7825. last->next = tmp;
  7826. while (last->next)
  7827. last = last->next;
  7828. }
  7829. pos = buf;
  7830. end = buf + len;
  7831. for (tmp = iface; tmp; tmp = tmp->next) {
  7832. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  7833. tmp->drv_name, tmp->ifname,
  7834. tmp->desc ? tmp->desc : "");
  7835. if (os_snprintf_error(end - pos, res)) {
  7836. *pos = '\0';
  7837. break;
  7838. }
  7839. pos += res;
  7840. }
  7841. wpa_free_iface_info(iface);
  7842. return pos - buf;
  7843. }
  7844. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  7845. char *buf, int len)
  7846. {
  7847. int res;
  7848. char *pos, *end;
  7849. struct wpa_supplicant *wpa_s;
  7850. wpa_s = global->ifaces;
  7851. pos = buf;
  7852. end = buf + len;
  7853. while (wpa_s) {
  7854. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  7855. if (os_snprintf_error(end - pos, res)) {
  7856. *pos = '\0';
  7857. break;
  7858. }
  7859. pos += res;
  7860. wpa_s = wpa_s->next;
  7861. }
  7862. return pos - buf;
  7863. }
  7864. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  7865. const char *ifname,
  7866. char *cmd, size_t *resp_len)
  7867. {
  7868. struct wpa_supplicant *wpa_s;
  7869. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  7870. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  7871. break;
  7872. }
  7873. if (wpa_s == NULL) {
  7874. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  7875. if (resp)
  7876. *resp_len = os_strlen(resp);
  7877. else
  7878. *resp_len = 1;
  7879. return resp;
  7880. }
  7881. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  7882. }
  7883. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  7884. char *buf, size_t *resp_len)
  7885. {
  7886. #ifdef CONFIG_P2P
  7887. static const char * cmd[] = {
  7888. "LIST_NETWORKS",
  7889. "P2P_FIND",
  7890. "P2P_STOP_FIND",
  7891. "P2P_LISTEN",
  7892. "P2P_GROUP_ADD",
  7893. "P2P_GET_PASSPHRASE",
  7894. "P2P_SERVICE_UPDATE",
  7895. "P2P_SERVICE_FLUSH",
  7896. "P2P_FLUSH",
  7897. "P2P_CANCEL",
  7898. "P2P_PRESENCE_REQ",
  7899. "P2P_EXT_LISTEN",
  7900. NULL
  7901. };
  7902. static const char * prefix[] = {
  7903. #ifdef ANDROID
  7904. "DRIVER ",
  7905. #endif /* ANDROID */
  7906. "GET_NETWORK ",
  7907. "REMOVE_NETWORK ",
  7908. "P2P_FIND ",
  7909. "P2P_CONNECT ",
  7910. "P2P_LISTEN ",
  7911. "P2P_GROUP_REMOVE ",
  7912. "P2P_GROUP_ADD ",
  7913. "P2P_PROV_DISC ",
  7914. "P2P_SERV_DISC_REQ ",
  7915. "P2P_SERV_DISC_CANCEL_REQ ",
  7916. "P2P_SERV_DISC_RESP ",
  7917. "P2P_SERV_DISC_EXTERNAL ",
  7918. "P2P_SERVICE_ADD ",
  7919. "P2P_SERVICE_DEL ",
  7920. "P2P_SERVICE_REP ",
  7921. "P2P_REJECT ",
  7922. "P2P_INVITE ",
  7923. "P2P_PEER ",
  7924. "P2P_SET ",
  7925. "P2P_UNAUTHORIZE ",
  7926. "P2P_PRESENCE_REQ ",
  7927. "P2P_EXT_LISTEN ",
  7928. "P2P_REMOVE_CLIENT ",
  7929. "WPS_NFC_TOKEN ",
  7930. "WPS_NFC_TAG_READ ",
  7931. "NFC_GET_HANDOVER_SEL ",
  7932. "NFC_GET_HANDOVER_REQ ",
  7933. "NFC_REPORT_HANDOVER ",
  7934. "P2P_ASP_PROVISION ",
  7935. "P2P_ASP_PROVISION_RESP ",
  7936. NULL
  7937. };
  7938. int found = 0;
  7939. int i;
  7940. if (global->p2p_init_wpa_s == NULL)
  7941. return NULL;
  7942. for (i = 0; !found && cmd[i]; i++) {
  7943. if (os_strcmp(buf, cmd[i]) == 0)
  7944. found = 1;
  7945. }
  7946. for (i = 0; !found && prefix[i]; i++) {
  7947. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  7948. found = 1;
  7949. }
  7950. if (found)
  7951. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  7952. buf, resp_len);
  7953. #endif /* CONFIG_P2P */
  7954. return NULL;
  7955. }
  7956. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  7957. char *buf, size_t *resp_len)
  7958. {
  7959. #ifdef CONFIG_WIFI_DISPLAY
  7960. if (global->p2p_init_wpa_s == NULL)
  7961. return NULL;
  7962. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  7963. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  7964. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  7965. buf, resp_len);
  7966. #endif /* CONFIG_WIFI_DISPLAY */
  7967. return NULL;
  7968. }
  7969. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  7970. char *buf, size_t *resp_len)
  7971. {
  7972. char *ret;
  7973. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  7974. if (ret)
  7975. return ret;
  7976. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  7977. if (ret)
  7978. return ret;
  7979. return NULL;
  7980. }
  7981. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  7982. {
  7983. char *value;
  7984. value = os_strchr(cmd, ' ');
  7985. if (value == NULL)
  7986. return -1;
  7987. *value++ = '\0';
  7988. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  7989. #ifdef CONFIG_WIFI_DISPLAY
  7990. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  7991. wifi_display_enable(global, !!atoi(value));
  7992. return 0;
  7993. }
  7994. #endif /* CONFIG_WIFI_DISPLAY */
  7995. /* Restore cmd to its original value to allow redirection */
  7996. value[-1] = ' ';
  7997. return -1;
  7998. }
  7999. static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
  8000. char *cmd)
  8001. {
  8002. struct wpa_supplicant *wpa_s[2]; /* src, dst */
  8003. char *p;
  8004. unsigned int i;
  8005. /* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
  8006. * <variable name> */
  8007. for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
  8008. p = os_strchr(cmd, ' ');
  8009. if (p == NULL)
  8010. return -1;
  8011. *p = '\0';
  8012. wpa_s[i] = global->ifaces;
  8013. for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
  8014. if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
  8015. break;
  8016. }
  8017. if (!wpa_s[i]) {
  8018. wpa_printf(MSG_DEBUG,
  8019. "CTRL_IFACE: Could not find iface=%s", cmd);
  8020. return -1;
  8021. }
  8022. cmd = p + 1;
  8023. }
  8024. return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
  8025. }
  8026. #ifndef CONFIG_NO_CONFIG_WRITE
  8027. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  8028. {
  8029. int ret = 0, saved = 0;
  8030. struct wpa_supplicant *wpa_s;
  8031. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  8032. if (!wpa_s->conf->update_config) {
  8033. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  8034. continue;
  8035. }
  8036. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  8037. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  8038. ret = 1;
  8039. } else {
  8040. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  8041. saved++;
  8042. }
  8043. }
  8044. if (!saved && !ret) {
  8045. wpa_dbg(wpa_s, MSG_DEBUG,
  8046. "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
  8047. ret = 1;
  8048. }
  8049. return ret;
  8050. }
  8051. #endif /* CONFIG_NO_CONFIG_WRITE */
  8052. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  8053. char *buf, size_t buflen)
  8054. {
  8055. char *pos, *end;
  8056. int ret;
  8057. struct wpa_supplicant *wpa_s;
  8058. pos = buf;
  8059. end = buf + buflen;
  8060. #ifdef CONFIG_P2P
  8061. if (global->p2p && !global->p2p_disabled) {
  8062. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  8063. "\n"
  8064. "p2p_state=%s\n",
  8065. MAC2STR(global->p2p_dev_addr),
  8066. p2p_get_state_txt(global->p2p));
  8067. if (os_snprintf_error(end - pos, ret))
  8068. return pos - buf;
  8069. pos += ret;
  8070. } else if (global->p2p) {
  8071. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  8072. if (os_snprintf_error(end - pos, ret))
  8073. return pos - buf;
  8074. pos += ret;
  8075. }
  8076. #endif /* CONFIG_P2P */
  8077. #ifdef CONFIG_WIFI_DISPLAY
  8078. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  8079. !!global->wifi_display);
  8080. if (os_snprintf_error(end - pos, ret))
  8081. return pos - buf;
  8082. pos += ret;
  8083. #endif /* CONFIG_WIFI_DISPLAY */
  8084. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  8085. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  8086. "address=" MACSTR "\n",
  8087. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  8088. if (os_snprintf_error(end - pos, ret))
  8089. return pos - buf;
  8090. pos += ret;
  8091. }
  8092. return pos - buf;
  8093. }
  8094. #ifdef CONFIG_FST
  8095. static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
  8096. char *cmd, char *buf,
  8097. size_t reply_size)
  8098. {
  8099. char ifname[IFNAMSIZ + 1];
  8100. struct fst_iface_cfg cfg;
  8101. struct wpa_supplicant *wpa_s;
  8102. struct fst_wpa_obj iface_obj;
  8103. if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
  8104. wpa_s = wpa_supplicant_get_iface(global, ifname);
  8105. if (wpa_s) {
  8106. if (wpa_s->fst) {
  8107. wpa_printf(MSG_INFO, "FST: Already attached");
  8108. return -1;
  8109. }
  8110. fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
  8111. wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
  8112. &iface_obj, &cfg);
  8113. if (wpa_s->fst)
  8114. return os_snprintf(buf, reply_size, "OK\n");
  8115. }
  8116. }
  8117. return -1;
  8118. }
  8119. static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
  8120. char *cmd, char *buf,
  8121. size_t reply_size)
  8122. {
  8123. char ifname[IFNAMSIZ + 1];
  8124. struct wpa_supplicant *wpa_s;
  8125. if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
  8126. wpa_s = wpa_supplicant_get_iface(global, ifname);
  8127. if (wpa_s) {
  8128. if (!fst_iface_detach(ifname)) {
  8129. wpa_s->fst = NULL;
  8130. return os_snprintf(buf, reply_size, "OK\n");
  8131. }
  8132. }
  8133. }
  8134. return -1;
  8135. }
  8136. #endif /* CONFIG_FST */
  8137. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  8138. char *buf, size_t *resp_len)
  8139. {
  8140. char *reply;
  8141. const int reply_size = 2048;
  8142. int reply_len;
  8143. int level = MSG_DEBUG;
  8144. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  8145. char *pos = os_strchr(buf + 7, ' ');
  8146. if (pos) {
  8147. *pos++ = '\0';
  8148. return wpas_global_ctrl_iface_ifname(global,
  8149. buf + 7, pos,
  8150. resp_len);
  8151. }
  8152. }
  8153. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  8154. if (reply)
  8155. return reply;
  8156. if (os_strcmp(buf, "PING") == 0)
  8157. level = MSG_EXCESSIVE;
  8158. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  8159. (const u8 *) buf, os_strlen(buf));
  8160. reply = os_malloc(reply_size);
  8161. if (reply == NULL) {
  8162. *resp_len = 1;
  8163. return NULL;
  8164. }
  8165. os_memcpy(reply, "OK\n", 3);
  8166. reply_len = 3;
  8167. if (os_strcmp(buf, "PING") == 0) {
  8168. os_memcpy(reply, "PONG\n", 5);
  8169. reply_len = 5;
  8170. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  8171. if (wpa_supplicant_global_iface_add(global, buf + 14))
  8172. reply_len = -1;
  8173. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  8174. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  8175. reply_len = -1;
  8176. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  8177. reply_len = wpa_supplicant_global_iface_list(
  8178. global, reply, reply_size);
  8179. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  8180. reply_len = wpa_supplicant_global_iface_interfaces(
  8181. global, reply, reply_size);
  8182. #ifdef CONFIG_FST
  8183. } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
  8184. reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
  8185. reply,
  8186. reply_size);
  8187. } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
  8188. reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
  8189. reply,
  8190. reply_size);
  8191. } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
  8192. reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
  8193. #endif /* CONFIG_FST */
  8194. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  8195. wpa_supplicant_terminate_proc(global);
  8196. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  8197. wpas_notify_suspend(global);
  8198. } else if (os_strcmp(buf, "RESUME") == 0) {
  8199. wpas_notify_resume(global);
  8200. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  8201. if (wpas_global_ctrl_iface_set(global, buf + 4)) {
  8202. #ifdef CONFIG_P2P
  8203. if (global->p2p_init_wpa_s) {
  8204. os_free(reply);
  8205. /* Check if P2P redirection would work for this
  8206. * command. */
  8207. return wpa_supplicant_ctrl_iface_process(
  8208. global->p2p_init_wpa_s,
  8209. buf, resp_len);
  8210. }
  8211. #endif /* CONFIG_P2P */
  8212. reply_len = -1;
  8213. }
  8214. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  8215. if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
  8216. reply_len = -1;
  8217. #ifndef CONFIG_NO_CONFIG_WRITE
  8218. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  8219. if (wpas_global_ctrl_iface_save_config(global))
  8220. reply_len = -1;
  8221. #endif /* CONFIG_NO_CONFIG_WRITE */
  8222. } else if (os_strcmp(buf, "STATUS") == 0) {
  8223. reply_len = wpas_global_ctrl_iface_status(global, reply,
  8224. reply_size);
  8225. #ifdef CONFIG_MODULE_TESTS
  8226. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  8227. int wpas_module_tests(void);
  8228. if (wpas_module_tests() < 0)
  8229. reply_len = -1;
  8230. #endif /* CONFIG_MODULE_TESTS */
  8231. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  8232. if (wpa_debug_reopen_file() < 0)
  8233. reply_len = -1;
  8234. } else {
  8235. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  8236. reply_len = 16;
  8237. }
  8238. if (reply_len < 0) {
  8239. os_memcpy(reply, "FAIL\n", 5);
  8240. reply_len = 5;
  8241. }
  8242. *resp_len = reply_len;
  8243. return reply;
  8244. }