ctrl_iface.c 160 KB

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