ctrl_iface.c 213 KB

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