ctrl_iface.c 247 KB

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