ctrl_iface.c 267 KB

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