ctrl_iface.c 222 KB

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