ctrl_iface.c 182 KB

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