ctrl_iface.c 145 KB

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