ctrl_iface.c 165 KB

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