ctrl_iface.c 253 KB

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