ctrl_iface.c 251 KB

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