ctrl_iface.c 267 KB

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