ctrl_iface.c 214 KB

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