ctrl_iface.c 241 KB

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