ctrl_iface.c 193 KB

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