ctrl_iface.c 264 KB

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