ctrl_iface.c 238 KB

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