ctrl_iface.c 212 KB

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