ctrl_iface.c 165 KB

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