ctrl_iface.c 227 KB

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