ctrl_iface.c 224 KB

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