ctrl_iface.c 233 KB

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