ctrl_iface.c 230 KB

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