ctrl_iface.c 203 KB

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