ctrl_iface.c 247 KB

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