ctrl_iface.c 267 KB

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