ctrl_iface.c 145 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208
  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)
  1713. p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
  1714. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  1715. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  1716. 0)
  1717. return 0; /* Do not show P2P listen discovery results here */
  1718. pos = buf;
  1719. end = buf + buflen;
  1720. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  1721. MAC2STR(bss->bssid), bss->freq, bss->level);
  1722. if (ret < 0 || ret >= end - pos)
  1723. return -1;
  1724. pos += ret;
  1725. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1726. if (ie)
  1727. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1728. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1729. if (ie2)
  1730. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1731. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1732. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1733. ret = os_snprintf(pos, end - pos, "[WEP]");
  1734. if (ret < 0 || ret >= end - pos)
  1735. return -1;
  1736. pos += ret;
  1737. }
  1738. if (bss->caps & IEEE80211_CAP_IBSS) {
  1739. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1740. if (ret < 0 || ret >= end - pos)
  1741. return -1;
  1742. pos += ret;
  1743. }
  1744. if (bss->caps & IEEE80211_CAP_ESS) {
  1745. ret = os_snprintf(pos, end - pos, "[ESS]");
  1746. if (ret < 0 || ret >= end - pos)
  1747. return -1;
  1748. pos += ret;
  1749. }
  1750. if (p2p) {
  1751. ret = os_snprintf(pos, end - pos, "[P2P]");
  1752. if (ret < 0 || ret >= end - pos)
  1753. return -1;
  1754. pos += ret;
  1755. }
  1756. #ifdef CONFIG_HS20
  1757. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  1758. ret = os_snprintf(pos, end - pos, "[HS20]");
  1759. if (ret < 0 || ret >= end - pos)
  1760. return -1;
  1761. pos += ret;
  1762. }
  1763. #endif /* CONFIG_HS20 */
  1764. ret = os_snprintf(pos, end - pos, "\t%s",
  1765. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1766. if (ret < 0 || ret >= end - pos)
  1767. return -1;
  1768. pos += ret;
  1769. ret = os_snprintf(pos, end - pos, "\n");
  1770. if (ret < 0 || ret >= end - pos)
  1771. return -1;
  1772. pos += ret;
  1773. return pos - buf;
  1774. }
  1775. static int wpa_supplicant_ctrl_iface_scan_results(
  1776. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1777. {
  1778. char *pos, *end;
  1779. struct wpa_bss *bss;
  1780. int ret;
  1781. pos = buf;
  1782. end = buf + buflen;
  1783. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  1784. "flags / ssid\n");
  1785. if (ret < 0 || ret >= end - pos)
  1786. return pos - buf;
  1787. pos += ret;
  1788. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  1789. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  1790. end - pos);
  1791. if (ret < 0 || ret >= end - pos)
  1792. return pos - buf;
  1793. pos += ret;
  1794. }
  1795. return pos - buf;
  1796. }
  1797. static int wpa_supplicant_ctrl_iface_select_network(
  1798. struct wpa_supplicant *wpa_s, char *cmd)
  1799. {
  1800. int id;
  1801. struct wpa_ssid *ssid;
  1802. /* cmd: "<network id>" or "any" */
  1803. if (os_strcmp(cmd, "any") == 0) {
  1804. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  1805. ssid = NULL;
  1806. } else {
  1807. id = atoi(cmd);
  1808. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  1809. ssid = wpa_config_get_network(wpa_s->conf, id);
  1810. if (ssid == NULL) {
  1811. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1812. "network id=%d", id);
  1813. return -1;
  1814. }
  1815. if (ssid->disabled == 2) {
  1816. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1817. "SELECT_NETWORK with persistent P2P group");
  1818. return -1;
  1819. }
  1820. }
  1821. wpa_supplicant_select_network(wpa_s, ssid);
  1822. return 0;
  1823. }
  1824. static int wpa_supplicant_ctrl_iface_enable_network(
  1825. struct wpa_supplicant *wpa_s, char *cmd)
  1826. {
  1827. int id;
  1828. struct wpa_ssid *ssid;
  1829. /* cmd: "<network id>" or "all" */
  1830. if (os_strcmp(cmd, "all") == 0) {
  1831. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  1832. ssid = NULL;
  1833. } else {
  1834. id = atoi(cmd);
  1835. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  1836. ssid = wpa_config_get_network(wpa_s->conf, id);
  1837. if (ssid == NULL) {
  1838. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1839. "network id=%d", id);
  1840. return -1;
  1841. }
  1842. if (ssid->disabled == 2) {
  1843. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1844. "ENABLE_NETWORK with persistent P2P group");
  1845. return -1;
  1846. }
  1847. if (os_strstr(cmd, " no-connect")) {
  1848. ssid->disabled = 0;
  1849. return 0;
  1850. }
  1851. }
  1852. wpa_supplicant_enable_network(wpa_s, ssid);
  1853. return 0;
  1854. }
  1855. static int wpa_supplicant_ctrl_iface_disable_network(
  1856. struct wpa_supplicant *wpa_s, char *cmd)
  1857. {
  1858. int id;
  1859. struct wpa_ssid *ssid;
  1860. /* cmd: "<network id>" or "all" */
  1861. if (os_strcmp(cmd, "all") == 0) {
  1862. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  1863. ssid = NULL;
  1864. } else {
  1865. id = atoi(cmd);
  1866. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  1867. ssid = wpa_config_get_network(wpa_s->conf, id);
  1868. if (ssid == NULL) {
  1869. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1870. "network id=%d", id);
  1871. return -1;
  1872. }
  1873. if (ssid->disabled == 2) {
  1874. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1875. "DISABLE_NETWORK with persistent P2P "
  1876. "group");
  1877. return -1;
  1878. }
  1879. }
  1880. wpa_supplicant_disable_network(wpa_s, ssid);
  1881. return 0;
  1882. }
  1883. static int wpa_supplicant_ctrl_iface_add_network(
  1884. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1885. {
  1886. struct wpa_ssid *ssid;
  1887. int ret;
  1888. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  1889. ssid = wpa_config_add_network(wpa_s->conf);
  1890. if (ssid == NULL)
  1891. return -1;
  1892. wpas_notify_network_added(wpa_s, ssid);
  1893. ssid->disabled = 1;
  1894. wpa_config_set_network_defaults(ssid);
  1895. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  1896. if (ret < 0 || (size_t) ret >= buflen)
  1897. return -1;
  1898. return ret;
  1899. }
  1900. static int wpa_supplicant_ctrl_iface_remove_network(
  1901. struct wpa_supplicant *wpa_s, char *cmd)
  1902. {
  1903. int id;
  1904. struct wpa_ssid *ssid;
  1905. int was_disabled;
  1906. /* cmd: "<network id>" or "all" */
  1907. if (os_strcmp(cmd, "all") == 0) {
  1908. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  1909. if (wpa_s->sched_scanning)
  1910. wpa_supplicant_cancel_sched_scan(wpa_s);
  1911. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1912. if (wpa_s->current_ssid) {
  1913. #ifdef CONFIG_SME
  1914. wpa_s->sme.prev_bssid_set = 0;
  1915. #endif /* CONFIG_SME */
  1916. wpa_sm_set_config(wpa_s->wpa, NULL);
  1917. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1918. wpa_supplicant_deauthenticate(
  1919. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1920. }
  1921. ssid = wpa_s->conf->ssid;
  1922. while (ssid) {
  1923. struct wpa_ssid *remove_ssid = ssid;
  1924. id = ssid->id;
  1925. ssid = ssid->next;
  1926. wpas_notify_network_removed(wpa_s, remove_ssid);
  1927. wpa_config_remove_network(wpa_s->conf, id);
  1928. }
  1929. return 0;
  1930. }
  1931. id = atoi(cmd);
  1932. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  1933. ssid = wpa_config_get_network(wpa_s->conf, id);
  1934. if (ssid)
  1935. wpas_notify_network_removed(wpa_s, ssid);
  1936. if (ssid == NULL) {
  1937. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1938. "id=%d", id);
  1939. return -1;
  1940. }
  1941. if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
  1942. #ifdef CONFIG_SME
  1943. wpa_s->sme.prev_bssid_set = 0;
  1944. #endif /* CONFIG_SME */
  1945. /*
  1946. * Invalidate the EAP session cache if the current or
  1947. * previously used network is removed.
  1948. */
  1949. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1950. }
  1951. if (ssid == wpa_s->current_ssid) {
  1952. wpa_sm_set_config(wpa_s->wpa, NULL);
  1953. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1954. wpa_supplicant_deauthenticate(wpa_s,
  1955. WLAN_REASON_DEAUTH_LEAVING);
  1956. }
  1957. was_disabled = ssid->disabled;
  1958. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1959. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  1960. "network id=%d", id);
  1961. return -1;
  1962. }
  1963. if (!was_disabled && wpa_s->sched_scanning) {
  1964. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
  1965. "network from filters");
  1966. wpa_supplicant_cancel_sched_scan(wpa_s);
  1967. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1968. }
  1969. return 0;
  1970. }
  1971. static int wpa_supplicant_ctrl_iface_set_network(
  1972. struct wpa_supplicant *wpa_s, char *cmd)
  1973. {
  1974. int id;
  1975. struct wpa_ssid *ssid;
  1976. char *name, *value;
  1977. /* cmd: "<network id> <variable name> <value>" */
  1978. name = os_strchr(cmd, ' ');
  1979. if (name == NULL)
  1980. return -1;
  1981. *name++ = '\0';
  1982. value = os_strchr(name, ' ');
  1983. if (value == NULL)
  1984. return -1;
  1985. *value++ = '\0';
  1986. id = atoi(cmd);
  1987. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  1988. id, name);
  1989. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1990. (u8 *) value, os_strlen(value));
  1991. ssid = wpa_config_get_network(wpa_s->conf, id);
  1992. if (ssid == NULL) {
  1993. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1994. "id=%d", id);
  1995. return -1;
  1996. }
  1997. if (wpa_config_set(ssid, name, value, 0) < 0) {
  1998. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  1999. "variable '%s'", name);
  2000. return -1;
  2001. }
  2002. if (os_strcmp(name, "bssid") != 0 &&
  2003. os_strcmp(name, "priority") != 0)
  2004. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  2005. if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
  2006. /*
  2007. * Invalidate the EAP session cache if anything in the current
  2008. * or previously used configuration changes.
  2009. */
  2010. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2011. }
  2012. if ((os_strcmp(name, "psk") == 0 &&
  2013. value[0] == '"' && ssid->ssid_len) ||
  2014. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  2015. wpa_config_update_psk(ssid);
  2016. else if (os_strcmp(name, "priority") == 0)
  2017. wpa_config_update_prio_list(wpa_s->conf);
  2018. return 0;
  2019. }
  2020. static int wpa_supplicant_ctrl_iface_get_network(
  2021. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2022. {
  2023. int id;
  2024. size_t res;
  2025. struct wpa_ssid *ssid;
  2026. char *name, *value;
  2027. /* cmd: "<network id> <variable name>" */
  2028. name = os_strchr(cmd, ' ');
  2029. if (name == NULL || buflen == 0)
  2030. return -1;
  2031. *name++ = '\0';
  2032. id = atoi(cmd);
  2033. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2034. id, name);
  2035. ssid = wpa_config_get_network(wpa_s->conf, id);
  2036. if (ssid == NULL) {
  2037. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2038. "id=%d", id);
  2039. return -1;
  2040. }
  2041. value = wpa_config_get_no_key(ssid, name);
  2042. if (value == NULL) {
  2043. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2044. "variable '%s'", name);
  2045. return -1;
  2046. }
  2047. res = os_strlcpy(buf, value, buflen);
  2048. if (res >= buflen) {
  2049. os_free(value);
  2050. return -1;
  2051. }
  2052. os_free(value);
  2053. return res;
  2054. }
  2055. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2056. char *buf, size_t buflen)
  2057. {
  2058. char *pos, *end;
  2059. struct wpa_cred *cred;
  2060. int ret;
  2061. pos = buf;
  2062. end = buf + buflen;
  2063. ret = os_snprintf(pos, end - pos,
  2064. "cred id / realm / username / domain / imsi\n");
  2065. if (ret < 0 || ret >= end - pos)
  2066. return pos - buf;
  2067. pos += ret;
  2068. cred = wpa_s->conf->cred;
  2069. while (cred) {
  2070. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2071. cred->id, cred->realm ? cred->realm : "",
  2072. cred->username ? cred->username : "",
  2073. cred->domain ? cred->domain : "",
  2074. cred->imsi ? cred->imsi : "");
  2075. if (ret < 0 || ret >= end - pos)
  2076. return pos - buf;
  2077. pos += ret;
  2078. cred = cred->next;
  2079. }
  2080. return pos - buf;
  2081. }
  2082. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2083. char *buf, size_t buflen)
  2084. {
  2085. struct wpa_cred *cred;
  2086. int ret;
  2087. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2088. cred = wpa_config_add_cred(wpa_s->conf);
  2089. if (cred == NULL)
  2090. return -1;
  2091. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2092. if (ret < 0 || (size_t) ret >= buflen)
  2093. return -1;
  2094. return ret;
  2095. }
  2096. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2097. struct wpa_cred *cred)
  2098. {
  2099. struct wpa_ssid *ssid;
  2100. char str[20];
  2101. if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
  2102. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2103. return -1;
  2104. }
  2105. /* Remove any network entry created based on the removed credential */
  2106. ssid = wpa_s->conf->ssid;
  2107. while (ssid) {
  2108. if (ssid->parent_cred == cred) {
  2109. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2110. "used the removed credential", ssid->id);
  2111. os_snprintf(str, sizeof(str), "%d", ssid->id);
  2112. ssid = ssid->next;
  2113. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2114. } else
  2115. ssid = ssid->next;
  2116. }
  2117. return 0;
  2118. }
  2119. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2120. char *cmd)
  2121. {
  2122. int id;
  2123. struct wpa_cred *cred, *prev;
  2124. /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
  2125. if (os_strcmp(cmd, "all") == 0) {
  2126. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2127. cred = wpa_s->conf->cred;
  2128. while (cred) {
  2129. prev = cred;
  2130. cred = cred->next;
  2131. wpas_ctrl_remove_cred(wpa_s, prev);
  2132. }
  2133. return 0;
  2134. }
  2135. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2136. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2137. cmd + 8);
  2138. cred = wpa_s->conf->cred;
  2139. while (cred) {
  2140. prev = cred;
  2141. cred = cred->next;
  2142. if (prev->domain &&
  2143. os_strcmp(prev->domain, cmd + 8) == 0)
  2144. wpas_ctrl_remove_cred(wpa_s, prev);
  2145. }
  2146. return 0;
  2147. }
  2148. id = atoi(cmd);
  2149. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2150. cred = wpa_config_get_cred(wpa_s->conf, id);
  2151. return wpas_ctrl_remove_cred(wpa_s, cred);
  2152. }
  2153. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2154. char *cmd)
  2155. {
  2156. int id;
  2157. struct wpa_cred *cred;
  2158. char *name, *value;
  2159. /* cmd: "<cred id> <variable name> <value>" */
  2160. name = os_strchr(cmd, ' ');
  2161. if (name == NULL)
  2162. return -1;
  2163. *name++ = '\0';
  2164. value = os_strchr(name, ' ');
  2165. if (value == NULL)
  2166. return -1;
  2167. *value++ = '\0';
  2168. id = atoi(cmd);
  2169. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2170. id, name);
  2171. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2172. (u8 *) value, os_strlen(value));
  2173. cred = wpa_config_get_cred(wpa_s->conf, id);
  2174. if (cred == NULL) {
  2175. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2176. id);
  2177. return -1;
  2178. }
  2179. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2180. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2181. "variable '%s'", name);
  2182. return -1;
  2183. }
  2184. return 0;
  2185. }
  2186. #ifndef CONFIG_NO_CONFIG_WRITE
  2187. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2188. {
  2189. int ret;
  2190. if (!wpa_s->conf->update_config) {
  2191. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2192. "to update configuration (update_config=0)");
  2193. return -1;
  2194. }
  2195. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2196. if (ret) {
  2197. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2198. "update configuration");
  2199. } else {
  2200. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2201. " updated");
  2202. }
  2203. return ret;
  2204. }
  2205. #endif /* CONFIG_NO_CONFIG_WRITE */
  2206. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2207. struct wpa_driver_capa *capa,
  2208. char *buf, size_t buflen)
  2209. {
  2210. int ret, first = 1;
  2211. char *pos, *end;
  2212. size_t len;
  2213. pos = buf;
  2214. end = pos + buflen;
  2215. if (res < 0) {
  2216. if (strict)
  2217. return 0;
  2218. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2219. if (len >= buflen)
  2220. return -1;
  2221. return len;
  2222. }
  2223. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  2224. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  2225. if (ret < 0 || ret >= end - pos)
  2226. return pos - buf;
  2227. pos += ret;
  2228. first = 0;
  2229. }
  2230. if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
  2231. ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
  2232. if (ret < 0 || ret >= end - pos)
  2233. return pos - buf;
  2234. pos += ret;
  2235. first = 0;
  2236. }
  2237. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  2238. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  2239. if (ret < 0 || ret >= end - pos)
  2240. return pos - buf;
  2241. pos += ret;
  2242. first = 0;
  2243. }
  2244. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2245. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  2246. if (ret < 0 || ret >= end - pos)
  2247. return pos - buf;
  2248. pos += ret;
  2249. first = 0;
  2250. }
  2251. return pos - buf;
  2252. }
  2253. static int ctrl_iface_get_capability_group(int res, char *strict,
  2254. struct wpa_driver_capa *capa,
  2255. char *buf, size_t buflen)
  2256. {
  2257. int ret, first = 1;
  2258. char *pos, *end;
  2259. size_t len;
  2260. pos = buf;
  2261. end = pos + buflen;
  2262. if (res < 0) {
  2263. if (strict)
  2264. return 0;
  2265. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2266. if (len >= buflen)
  2267. return -1;
  2268. return len;
  2269. }
  2270. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  2271. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  2272. if (ret < 0 || ret >= end - pos)
  2273. return pos - buf;
  2274. pos += ret;
  2275. first = 0;
  2276. }
  2277. if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
  2278. ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
  2279. if (ret < 0 || ret >= end - pos)
  2280. return pos - buf;
  2281. pos += ret;
  2282. first = 0;
  2283. }
  2284. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  2285. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  2286. if (ret < 0 || ret >= end - pos)
  2287. return pos - buf;
  2288. pos += ret;
  2289. first = 0;
  2290. }
  2291. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  2292. ret = os_snprintf(pos, end - pos, "%sWEP104",
  2293. first ? "" : " ");
  2294. if (ret < 0 || ret >= end - pos)
  2295. return pos - buf;
  2296. pos += ret;
  2297. first = 0;
  2298. }
  2299. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  2300. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  2301. if (ret < 0 || ret >= end - pos)
  2302. return pos - buf;
  2303. pos += ret;
  2304. first = 0;
  2305. }
  2306. return pos - buf;
  2307. }
  2308. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  2309. struct wpa_driver_capa *capa,
  2310. char *buf, size_t buflen)
  2311. {
  2312. int ret;
  2313. char *pos, *end;
  2314. size_t len;
  2315. pos = buf;
  2316. end = pos + buflen;
  2317. if (res < 0) {
  2318. if (strict)
  2319. return 0;
  2320. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  2321. "NONE", buflen);
  2322. if (len >= buflen)
  2323. return -1;
  2324. return len;
  2325. }
  2326. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  2327. if (ret < 0 || ret >= end - pos)
  2328. return pos - buf;
  2329. pos += ret;
  2330. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2331. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2332. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  2333. if (ret < 0 || ret >= end - pos)
  2334. return pos - buf;
  2335. pos += ret;
  2336. }
  2337. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2338. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2339. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  2340. if (ret < 0 || ret >= end - pos)
  2341. return pos - buf;
  2342. pos += ret;
  2343. }
  2344. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2345. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  2346. if (ret < 0 || ret >= end - pos)
  2347. return pos - buf;
  2348. pos += ret;
  2349. }
  2350. return pos - buf;
  2351. }
  2352. static int ctrl_iface_get_capability_proto(int res, char *strict,
  2353. struct wpa_driver_capa *capa,
  2354. char *buf, size_t buflen)
  2355. {
  2356. int ret, first = 1;
  2357. char *pos, *end;
  2358. size_t len;
  2359. pos = buf;
  2360. end = pos + buflen;
  2361. if (res < 0) {
  2362. if (strict)
  2363. return 0;
  2364. len = os_strlcpy(buf, "RSN WPA", buflen);
  2365. if (len >= buflen)
  2366. return -1;
  2367. return len;
  2368. }
  2369. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2370. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2371. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  2372. if (ret < 0 || ret >= end - pos)
  2373. return pos - buf;
  2374. pos += ret;
  2375. first = 0;
  2376. }
  2377. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2378. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  2379. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  2380. if (ret < 0 || ret >= end - pos)
  2381. return pos - buf;
  2382. pos += ret;
  2383. first = 0;
  2384. }
  2385. return pos - buf;
  2386. }
  2387. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  2388. struct wpa_driver_capa *capa,
  2389. char *buf, size_t buflen)
  2390. {
  2391. int ret, first = 1;
  2392. char *pos, *end;
  2393. size_t len;
  2394. pos = buf;
  2395. end = pos + buflen;
  2396. if (res < 0) {
  2397. if (strict)
  2398. return 0;
  2399. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  2400. if (len >= buflen)
  2401. return -1;
  2402. return len;
  2403. }
  2404. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  2405. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  2406. if (ret < 0 || ret >= end - pos)
  2407. return pos - buf;
  2408. pos += ret;
  2409. first = 0;
  2410. }
  2411. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  2412. ret = os_snprintf(pos, end - pos, "%sSHARED",
  2413. first ? "" : " ");
  2414. if (ret < 0 || ret >= end - pos)
  2415. return pos - buf;
  2416. pos += ret;
  2417. first = 0;
  2418. }
  2419. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  2420. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  2421. if (ret < 0 || ret >= end - pos)
  2422. return pos - buf;
  2423. pos += ret;
  2424. first = 0;
  2425. }
  2426. return pos - buf;
  2427. }
  2428. static int ctrl_iface_get_capability_modes(int res, char *strict,
  2429. struct wpa_driver_capa *capa,
  2430. char *buf, size_t buflen)
  2431. {
  2432. int ret, first = 1;
  2433. char *pos, *end;
  2434. size_t len;
  2435. pos = buf;
  2436. end = pos + buflen;
  2437. if (res < 0) {
  2438. if (strict)
  2439. return 0;
  2440. len = os_strlcpy(buf, "IBSS AP", buflen);
  2441. if (len >= buflen)
  2442. return -1;
  2443. return len;
  2444. }
  2445. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  2446. ret = os_snprintf(pos, end - pos, "%sIBSS", first ? "" : " ");
  2447. if (ret < 0 || ret >= end - pos)
  2448. return pos - buf;
  2449. pos += ret;
  2450. first = 0;
  2451. }
  2452. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  2453. ret = os_snprintf(pos, end - pos, "%sAP", first ? "" : " ");
  2454. if (ret < 0 || ret >= end - pos)
  2455. return pos - buf;
  2456. pos += ret;
  2457. first = 0;
  2458. }
  2459. return pos - buf;
  2460. }
  2461. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  2462. char *buf, size_t buflen)
  2463. {
  2464. struct hostapd_channel_data *chnl;
  2465. int ret, i, j;
  2466. char *pos, *end, *hmode;
  2467. pos = buf;
  2468. end = pos + buflen;
  2469. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2470. switch (wpa_s->hw.modes[j].mode) {
  2471. case HOSTAPD_MODE_IEEE80211B:
  2472. hmode = "B";
  2473. break;
  2474. case HOSTAPD_MODE_IEEE80211G:
  2475. hmode = "G";
  2476. break;
  2477. case HOSTAPD_MODE_IEEE80211A:
  2478. hmode = "A";
  2479. break;
  2480. case HOSTAPD_MODE_IEEE80211AD:
  2481. hmode = "AD";
  2482. break;
  2483. default:
  2484. continue;
  2485. }
  2486. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  2487. if (ret < 0 || ret >= end - pos)
  2488. return pos - buf;
  2489. pos += ret;
  2490. chnl = wpa_s->hw.modes[j].channels;
  2491. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2492. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2493. continue;
  2494. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  2495. if (ret < 0 || ret >= end - pos)
  2496. return pos - buf;
  2497. pos += ret;
  2498. }
  2499. ret = os_snprintf(pos, end - pos, "\n");
  2500. if (ret < 0 || ret >= end - pos)
  2501. return pos - buf;
  2502. pos += ret;
  2503. }
  2504. return pos - buf;
  2505. }
  2506. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  2507. char *buf, size_t buflen)
  2508. {
  2509. struct hostapd_channel_data *chnl;
  2510. int ret, i, j;
  2511. char *pos, *end, *hmode;
  2512. pos = buf;
  2513. end = pos + buflen;
  2514. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2515. switch (wpa_s->hw.modes[j].mode) {
  2516. case HOSTAPD_MODE_IEEE80211B:
  2517. hmode = "B";
  2518. break;
  2519. case HOSTAPD_MODE_IEEE80211G:
  2520. hmode = "G";
  2521. break;
  2522. case HOSTAPD_MODE_IEEE80211A:
  2523. hmode = "A";
  2524. break;
  2525. case HOSTAPD_MODE_IEEE80211AD:
  2526. hmode = "AD";
  2527. break;
  2528. default:
  2529. continue;
  2530. }
  2531. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  2532. hmode);
  2533. if (ret < 0 || ret >= end - pos)
  2534. return pos - buf;
  2535. pos += ret;
  2536. chnl = wpa_s->hw.modes[j].channels;
  2537. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2538. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2539. continue;
  2540. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s\n",
  2541. chnl[i].chan, chnl[i].freq,
  2542. chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
  2543. " (NO_IBSS)" : "");
  2544. if (ret < 0 || ret >= end - pos)
  2545. return pos - buf;
  2546. pos += ret;
  2547. }
  2548. ret = os_snprintf(pos, end - pos, "\n");
  2549. if (ret < 0 || ret >= end - pos)
  2550. return pos - buf;
  2551. pos += ret;
  2552. }
  2553. return pos - buf;
  2554. }
  2555. static int wpa_supplicant_ctrl_iface_get_capability(
  2556. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  2557. size_t buflen)
  2558. {
  2559. struct wpa_driver_capa capa;
  2560. int res;
  2561. char *strict;
  2562. char field[30];
  2563. size_t len;
  2564. /* Determine whether or not strict checking was requested */
  2565. len = os_strlcpy(field, _field, sizeof(field));
  2566. if (len >= sizeof(field))
  2567. return -1;
  2568. strict = os_strchr(field, ' ');
  2569. if (strict != NULL) {
  2570. *strict++ = '\0';
  2571. if (os_strcmp(strict, "strict") != 0)
  2572. return -1;
  2573. }
  2574. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  2575. field, strict ? strict : "");
  2576. if (os_strcmp(field, "eap") == 0) {
  2577. return eap_get_names(buf, buflen);
  2578. }
  2579. res = wpa_drv_get_capa(wpa_s, &capa);
  2580. if (os_strcmp(field, "pairwise") == 0)
  2581. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  2582. buf, buflen);
  2583. if (os_strcmp(field, "group") == 0)
  2584. return ctrl_iface_get_capability_group(res, strict, &capa,
  2585. buf, buflen);
  2586. if (os_strcmp(field, "key_mgmt") == 0)
  2587. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  2588. buf, buflen);
  2589. if (os_strcmp(field, "proto") == 0)
  2590. return ctrl_iface_get_capability_proto(res, strict, &capa,
  2591. buf, buflen);
  2592. if (os_strcmp(field, "auth_alg") == 0)
  2593. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  2594. buf, buflen);
  2595. if (os_strcmp(field, "modes") == 0)
  2596. return ctrl_iface_get_capability_modes(res, strict, &capa,
  2597. buf, buflen);
  2598. if (os_strcmp(field, "channels") == 0)
  2599. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  2600. if (os_strcmp(field, "freq") == 0)
  2601. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  2602. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  2603. field);
  2604. return -1;
  2605. }
  2606. #ifdef CONFIG_INTERWORKING
  2607. static char * anqp_add_hex(char *pos, char *end, const char *title,
  2608. struct wpabuf *data)
  2609. {
  2610. char *start = pos;
  2611. size_t i;
  2612. int ret;
  2613. const u8 *d;
  2614. if (data == NULL)
  2615. return start;
  2616. ret = os_snprintf(pos, end - pos, "%s=", title);
  2617. if (ret < 0 || ret >= end - pos)
  2618. return start;
  2619. pos += ret;
  2620. d = wpabuf_head_u8(data);
  2621. for (i = 0; i < wpabuf_len(data); i++) {
  2622. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  2623. if (ret < 0 || ret >= end - pos)
  2624. return start;
  2625. pos += ret;
  2626. }
  2627. ret = os_snprintf(pos, end - pos, "\n");
  2628. if (ret < 0 || ret >= end - pos)
  2629. return start;
  2630. pos += ret;
  2631. return pos;
  2632. }
  2633. #endif /* CONFIG_INTERWORKING */
  2634. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2635. unsigned long mask, char *buf, size_t buflen)
  2636. {
  2637. size_t i;
  2638. int ret;
  2639. char *pos, *end;
  2640. const u8 *ie, *ie2;
  2641. pos = buf;
  2642. end = buf + buflen;
  2643. if (mask & WPA_BSS_MASK_ID) {
  2644. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  2645. if (ret < 0 || ret >= end - pos)
  2646. return 0;
  2647. pos += ret;
  2648. }
  2649. if (mask & WPA_BSS_MASK_BSSID) {
  2650. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  2651. MAC2STR(bss->bssid));
  2652. if (ret < 0 || ret >= end - pos)
  2653. return 0;
  2654. pos += ret;
  2655. }
  2656. if (mask & WPA_BSS_MASK_FREQ) {
  2657. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  2658. if (ret < 0 || ret >= end - pos)
  2659. return 0;
  2660. pos += ret;
  2661. }
  2662. if (mask & WPA_BSS_MASK_BEACON_INT) {
  2663. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  2664. bss->beacon_int);
  2665. if (ret < 0 || ret >= end - pos)
  2666. return 0;
  2667. pos += ret;
  2668. }
  2669. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  2670. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  2671. bss->caps);
  2672. if (ret < 0 || ret >= end - pos)
  2673. return 0;
  2674. pos += ret;
  2675. }
  2676. if (mask & WPA_BSS_MASK_QUAL) {
  2677. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  2678. if (ret < 0 || ret >= end - pos)
  2679. return 0;
  2680. pos += ret;
  2681. }
  2682. if (mask & WPA_BSS_MASK_NOISE) {
  2683. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  2684. if (ret < 0 || ret >= end - pos)
  2685. return 0;
  2686. pos += ret;
  2687. }
  2688. if (mask & WPA_BSS_MASK_LEVEL) {
  2689. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  2690. if (ret < 0 || ret >= end - pos)
  2691. return 0;
  2692. pos += ret;
  2693. }
  2694. if (mask & WPA_BSS_MASK_TSF) {
  2695. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  2696. (unsigned long long) bss->tsf);
  2697. if (ret < 0 || ret >= end - pos)
  2698. return 0;
  2699. pos += ret;
  2700. }
  2701. if (mask & WPA_BSS_MASK_AGE) {
  2702. struct os_time now;
  2703. os_get_time(&now);
  2704. ret = os_snprintf(pos, end - pos, "age=%d\n",
  2705. (int) (now.sec - bss->last_update.sec));
  2706. if (ret < 0 || ret >= end - pos)
  2707. return 0;
  2708. pos += ret;
  2709. }
  2710. if (mask & WPA_BSS_MASK_IE) {
  2711. ret = os_snprintf(pos, end - pos, "ie=");
  2712. if (ret < 0 || ret >= end - pos)
  2713. return 0;
  2714. pos += ret;
  2715. ie = (const u8 *) (bss + 1);
  2716. for (i = 0; i < bss->ie_len; i++) {
  2717. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  2718. if (ret < 0 || ret >= end - pos)
  2719. return 0;
  2720. pos += ret;
  2721. }
  2722. ret = os_snprintf(pos, end - pos, "\n");
  2723. if (ret < 0 || ret >= end - pos)
  2724. return 0;
  2725. pos += ret;
  2726. }
  2727. if (mask & WPA_BSS_MASK_FLAGS) {
  2728. ret = os_snprintf(pos, end - pos, "flags=");
  2729. if (ret < 0 || ret >= end - pos)
  2730. return 0;
  2731. pos += ret;
  2732. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2733. if (ie)
  2734. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  2735. 2 + ie[1]);
  2736. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2737. if (ie2)
  2738. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  2739. 2 + ie2[1]);
  2740. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2741. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  2742. ret = os_snprintf(pos, end - pos, "[WEP]");
  2743. if (ret < 0 || ret >= end - pos)
  2744. return 0;
  2745. pos += ret;
  2746. }
  2747. if (bss->caps & IEEE80211_CAP_IBSS) {
  2748. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2749. if (ret < 0 || ret >= end - pos)
  2750. return 0;
  2751. pos += ret;
  2752. }
  2753. if (bss->caps & IEEE80211_CAP_ESS) {
  2754. ret = os_snprintf(pos, end - pos, "[ESS]");
  2755. if (ret < 0 || ret >= end - pos)
  2756. return 0;
  2757. pos += ret;
  2758. }
  2759. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  2760. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  2761. ret = os_snprintf(pos, end - pos, "[P2P]");
  2762. if (ret < 0 || ret >= end - pos)
  2763. return 0;
  2764. pos += ret;
  2765. }
  2766. #ifdef CONFIG_HS20
  2767. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  2768. ret = os_snprintf(pos, end - pos, "[HS20]");
  2769. if (ret < 0 || ret >= end - pos)
  2770. return 0;
  2771. pos += ret;
  2772. }
  2773. #endif /* CONFIG_HS20 */
  2774. ret = os_snprintf(pos, end - pos, "\n");
  2775. if (ret < 0 || ret >= end - pos)
  2776. return 0;
  2777. pos += ret;
  2778. }
  2779. if (mask & WPA_BSS_MASK_SSID) {
  2780. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  2781. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2782. if (ret < 0 || ret >= end - pos)
  2783. return 0;
  2784. pos += ret;
  2785. }
  2786. #ifdef CONFIG_WPS
  2787. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  2788. ie = (const u8 *) (bss + 1);
  2789. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  2790. if (ret < 0 || ret >= end - pos)
  2791. return 0;
  2792. pos += ret;
  2793. }
  2794. #endif /* CONFIG_WPS */
  2795. #ifdef CONFIG_P2P
  2796. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  2797. ie = (const u8 *) (bss + 1);
  2798. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  2799. if (ret < 0 || ret >= end - pos)
  2800. return 0;
  2801. pos += ret;
  2802. }
  2803. #endif /* CONFIG_P2P */
  2804. #ifdef CONFIG_WIFI_DISPLAY
  2805. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  2806. struct wpabuf *wfd;
  2807. ie = (const u8 *) (bss + 1);
  2808. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  2809. WFD_IE_VENDOR_TYPE);
  2810. if (wfd) {
  2811. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  2812. if (ret < 0 || ret >= end - pos)
  2813. return 0;
  2814. pos += ret;
  2815. pos += wpa_snprintf_hex(pos, end - pos,
  2816. wpabuf_head(wfd),
  2817. wpabuf_len(wfd));
  2818. wpabuf_free(wfd);
  2819. ret = os_snprintf(pos, end - pos, "\n");
  2820. if (ret < 0 || ret >= end - pos)
  2821. return 0;
  2822. pos += ret;
  2823. }
  2824. }
  2825. #endif /* CONFIG_WIFI_DISPLAY */
  2826. #ifdef CONFIG_INTERWORKING
  2827. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  2828. struct wpa_bss_anqp *anqp = bss->anqp;
  2829. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  2830. anqp->venue_name);
  2831. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  2832. anqp->network_auth_type);
  2833. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  2834. anqp->roaming_consortium);
  2835. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  2836. anqp->ip_addr_type_availability);
  2837. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  2838. anqp->nai_realm);
  2839. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  2840. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  2841. anqp->domain_name);
  2842. #ifdef CONFIG_HS20
  2843. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  2844. anqp->hs20_operator_friendly_name);
  2845. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  2846. anqp->hs20_wan_metrics);
  2847. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  2848. anqp->hs20_connection_capability);
  2849. #endif /* CONFIG_HS20 */
  2850. }
  2851. #endif /* CONFIG_INTERWORKING */
  2852. if (mask & WPA_BSS_MASK_DELIM) {
  2853. ret = os_snprintf(pos, end - pos, "====\n");
  2854. if (ret < 0 || ret >= end - pos)
  2855. return 0;
  2856. pos += ret;
  2857. }
  2858. return pos - buf;
  2859. }
  2860. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  2861. const char *cmd, char *buf,
  2862. size_t buflen)
  2863. {
  2864. u8 bssid[ETH_ALEN];
  2865. size_t i;
  2866. struct wpa_bss *bss;
  2867. struct wpa_bss *bsslast = NULL;
  2868. struct dl_list *next;
  2869. int ret = 0;
  2870. int len;
  2871. char *ctmp;
  2872. unsigned long mask = WPA_BSS_MASK_ALL;
  2873. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  2874. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  2875. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  2876. list_id);
  2877. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  2878. list_id);
  2879. } else { /* N1-N2 */
  2880. unsigned int id1, id2;
  2881. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  2882. wpa_printf(MSG_INFO, "Wrong BSS range "
  2883. "format");
  2884. return 0;
  2885. }
  2886. if (*(cmd + 6) == '-')
  2887. id1 = 0;
  2888. else
  2889. id1 = atoi(cmd + 6);
  2890. ctmp++;
  2891. if (*ctmp >= '0' && *ctmp <= '9')
  2892. id2 = atoi(ctmp);
  2893. else
  2894. id2 = (unsigned int) -1;
  2895. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  2896. if (id2 == (unsigned int) -1)
  2897. bsslast = dl_list_last(&wpa_s->bss_id,
  2898. struct wpa_bss,
  2899. list_id);
  2900. else {
  2901. bsslast = wpa_bss_get_id(wpa_s, id2);
  2902. if (bsslast == NULL && bss && id2 > id1) {
  2903. struct wpa_bss *tmp = bss;
  2904. for (;;) {
  2905. next = tmp->list_id.next;
  2906. if (next == &wpa_s->bss_id)
  2907. break;
  2908. tmp = dl_list_entry(
  2909. next, struct wpa_bss,
  2910. list_id);
  2911. if (tmp->id > id2)
  2912. break;
  2913. bsslast = tmp;
  2914. }
  2915. }
  2916. }
  2917. }
  2918. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  2919. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  2920. else if (os_strncmp(cmd, "LAST", 4) == 0)
  2921. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  2922. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  2923. i = atoi(cmd + 3);
  2924. bss = wpa_bss_get_id(wpa_s, i);
  2925. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  2926. i = atoi(cmd + 5);
  2927. bss = wpa_bss_get_id(wpa_s, i);
  2928. if (bss) {
  2929. next = bss->list_id.next;
  2930. if (next == &wpa_s->bss_id)
  2931. bss = NULL;
  2932. else
  2933. bss = dl_list_entry(next, struct wpa_bss,
  2934. list_id);
  2935. }
  2936. #ifdef CONFIG_P2P
  2937. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  2938. if (hwaddr_aton(cmd + 13, bssid) == 0)
  2939. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  2940. else
  2941. bss = NULL;
  2942. #endif /* CONFIG_P2P */
  2943. } else if (hwaddr_aton(cmd, bssid) == 0)
  2944. bss = wpa_bss_get_bssid(wpa_s, bssid);
  2945. else {
  2946. struct wpa_bss *tmp;
  2947. i = atoi(cmd);
  2948. bss = NULL;
  2949. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  2950. {
  2951. if (i-- == 0) {
  2952. bss = tmp;
  2953. break;
  2954. }
  2955. }
  2956. }
  2957. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  2958. mask = strtoul(ctmp + 5, NULL, 0x10);
  2959. if (mask == 0)
  2960. mask = WPA_BSS_MASK_ALL;
  2961. }
  2962. if (bss == NULL)
  2963. return 0;
  2964. if (bsslast == NULL)
  2965. bsslast = bss;
  2966. do {
  2967. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  2968. ret += len;
  2969. buf += len;
  2970. buflen -= len;
  2971. if (bss == bsslast) {
  2972. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  2973. (bss == dl_list_last(&wpa_s->bss_id,
  2974. struct wpa_bss, list_id)))
  2975. os_snprintf(buf - 5, 5, "####\n");
  2976. break;
  2977. }
  2978. next = bss->list_id.next;
  2979. if (next == &wpa_s->bss_id)
  2980. break;
  2981. bss = dl_list_entry(next, struct wpa_bss, list_id);
  2982. } while (bss && len);
  2983. return ret;
  2984. }
  2985. static int wpa_supplicant_ctrl_iface_ap_scan(
  2986. struct wpa_supplicant *wpa_s, char *cmd)
  2987. {
  2988. int ap_scan = atoi(cmd);
  2989. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  2990. }
  2991. static int wpa_supplicant_ctrl_iface_scan_interval(
  2992. struct wpa_supplicant *wpa_s, char *cmd)
  2993. {
  2994. int scan_int = atoi(cmd);
  2995. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  2996. }
  2997. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  2998. struct wpa_supplicant *wpa_s, char *cmd)
  2999. {
  3000. int expire_age = atoi(cmd);
  3001. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  3002. }
  3003. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  3004. struct wpa_supplicant *wpa_s, char *cmd)
  3005. {
  3006. int expire_count = atoi(cmd);
  3007. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  3008. }
  3009. static int wpa_supplicant_ctrl_iface_bss_flush(
  3010. struct wpa_supplicant *wpa_s, char *cmd)
  3011. {
  3012. int flush_age = atoi(cmd);
  3013. if (flush_age == 0)
  3014. wpa_bss_flush(wpa_s);
  3015. else
  3016. wpa_bss_flush_by_age(wpa_s, flush_age);
  3017. return 0;
  3018. }
  3019. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  3020. {
  3021. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  3022. /* MLME-DELETEKEYS.request */
  3023. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  3024. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  3025. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  3026. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  3027. #ifdef CONFIG_IEEE80211W
  3028. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  3029. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  3030. #endif /* CONFIG_IEEE80211W */
  3031. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  3032. 0);
  3033. /* MLME-SETPROTECTION.request(None) */
  3034. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  3035. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  3036. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  3037. wpa_sm_drop_sa(wpa_s->wpa);
  3038. }
  3039. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  3040. char *addr)
  3041. {
  3042. #ifdef CONFIG_NO_SCAN_PROCESSING
  3043. return -1;
  3044. #else /* CONFIG_NO_SCAN_PROCESSING */
  3045. u8 bssid[ETH_ALEN];
  3046. struct wpa_bss *bss;
  3047. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3048. if (hwaddr_aton(addr, bssid)) {
  3049. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  3050. "address '%s'", addr);
  3051. return -1;
  3052. }
  3053. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  3054. if (!ssid) {
  3055. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  3056. "configuration known for the target AP");
  3057. return -1;
  3058. }
  3059. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  3060. if (!bss) {
  3061. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  3062. "from BSS table");
  3063. return -1;
  3064. }
  3065. /*
  3066. * TODO: Find best network configuration block from configuration to
  3067. * allow roaming to other networks
  3068. */
  3069. wpa_s->reassociate = 1;
  3070. wpa_supplicant_connect(wpa_s, bss, ssid);
  3071. return 0;
  3072. #endif /* CONFIG_NO_SCAN_PROCESSING */
  3073. }
  3074. #ifdef CONFIG_P2P
  3075. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  3076. {
  3077. unsigned int timeout = atoi(cmd);
  3078. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  3079. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  3080. char *pos;
  3081. unsigned int search_delay;
  3082. if (os_strstr(cmd, "type=social"))
  3083. type = P2P_FIND_ONLY_SOCIAL;
  3084. else if (os_strstr(cmd, "type=progressive"))
  3085. type = P2P_FIND_PROGRESSIVE;
  3086. pos = os_strstr(cmd, "dev_id=");
  3087. if (pos) {
  3088. pos += 7;
  3089. if (hwaddr_aton(pos, dev_id))
  3090. return -1;
  3091. _dev_id = dev_id;
  3092. }
  3093. pos = os_strstr(cmd, "delay=");
  3094. if (pos) {
  3095. pos += 6;
  3096. search_delay = atoi(pos);
  3097. } else
  3098. search_delay = wpas_p2p_search_delay(wpa_s);
  3099. return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id,
  3100. search_delay);
  3101. }
  3102. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  3103. char *buf, size_t buflen)
  3104. {
  3105. u8 addr[ETH_ALEN];
  3106. char *pos, *pos2;
  3107. char *pin = NULL;
  3108. enum p2p_wps_method wps_method;
  3109. int new_pin;
  3110. int ret;
  3111. int persistent_group, persistent_id = -1;
  3112. int join;
  3113. int auth;
  3114. int automatic;
  3115. int go_intent = -1;
  3116. int freq = 0;
  3117. int pd;
  3118. int ht40;
  3119. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
  3120. * [persistent|persistent=<network id>]
  3121. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  3122. * [ht40] */
  3123. if (hwaddr_aton(cmd, addr))
  3124. return -1;
  3125. pos = cmd + 17;
  3126. if (*pos != ' ')
  3127. return -1;
  3128. pos++;
  3129. persistent_group = os_strstr(pos, " persistent") != NULL;
  3130. pos2 = os_strstr(pos, " persistent=");
  3131. if (pos2) {
  3132. struct wpa_ssid *ssid;
  3133. persistent_id = atoi(pos2 + 12);
  3134. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3135. if (ssid == NULL || ssid->disabled != 2 ||
  3136. ssid->mode != WPAS_MODE_P2P_GO) {
  3137. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  3138. "SSID id=%d for persistent P2P group (GO)",
  3139. persistent_id);
  3140. return -1;
  3141. }
  3142. }
  3143. join = os_strstr(pos, " join") != NULL;
  3144. auth = os_strstr(pos, " auth") != NULL;
  3145. automatic = os_strstr(pos, " auto") != NULL;
  3146. pd = os_strstr(pos, " provdisc") != NULL;
  3147. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
  3148. pos2 = os_strstr(pos, " go_intent=");
  3149. if (pos2) {
  3150. pos2 += 11;
  3151. go_intent = atoi(pos2);
  3152. if (go_intent < 0 || go_intent > 15)
  3153. return -1;
  3154. }
  3155. pos2 = os_strstr(pos, " freq=");
  3156. if (pos2) {
  3157. pos2 += 6;
  3158. freq = atoi(pos2);
  3159. if (freq <= 0)
  3160. return -1;
  3161. }
  3162. if (os_strncmp(pos, "pin", 3) == 0) {
  3163. /* Request random PIN (to be displayed) and enable the PIN */
  3164. wps_method = WPS_PIN_DISPLAY;
  3165. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  3166. wps_method = WPS_PBC;
  3167. } else {
  3168. pin = pos;
  3169. pos = os_strchr(pin, ' ');
  3170. wps_method = WPS_PIN_KEYPAD;
  3171. if (pos) {
  3172. *pos++ = '\0';
  3173. if (os_strncmp(pos, "display", 7) == 0)
  3174. wps_method = WPS_PIN_DISPLAY;
  3175. }
  3176. if (!wps_pin_str_valid(pin)) {
  3177. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  3178. return 17;
  3179. }
  3180. }
  3181. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  3182. persistent_group, automatic, join,
  3183. auth, go_intent, freq, persistent_id, pd,
  3184. ht40);
  3185. if (new_pin == -2) {
  3186. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  3187. return 25;
  3188. }
  3189. if (new_pin == -3) {
  3190. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  3191. return 25;
  3192. }
  3193. if (new_pin < 0)
  3194. return -1;
  3195. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  3196. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  3197. if (ret < 0 || (size_t) ret >= buflen)
  3198. return -1;
  3199. return ret;
  3200. }
  3201. os_memcpy(buf, "OK\n", 3);
  3202. return 3;
  3203. }
  3204. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3205. {
  3206. unsigned int timeout = atoi(cmd);
  3207. return wpas_p2p_listen(wpa_s, timeout);
  3208. }
  3209. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  3210. {
  3211. u8 addr[ETH_ALEN];
  3212. char *pos;
  3213. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  3214. /* <addr> <config method> [join|auto] */
  3215. if (hwaddr_aton(cmd, addr))
  3216. return -1;
  3217. pos = cmd + 17;
  3218. if (*pos != ' ')
  3219. return -1;
  3220. pos++;
  3221. if (os_strstr(pos, " join") != NULL)
  3222. use = WPAS_P2P_PD_FOR_JOIN;
  3223. else if (os_strstr(pos, " auto") != NULL)
  3224. use = WPAS_P2P_PD_AUTO;
  3225. return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
  3226. }
  3227. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  3228. size_t buflen)
  3229. {
  3230. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3231. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  3232. ssid->passphrase == NULL)
  3233. return -1;
  3234. os_strlcpy(buf, ssid->passphrase, buflen);
  3235. return os_strlen(buf);
  3236. }
  3237. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  3238. char *buf, size_t buflen)
  3239. {
  3240. u64 ref;
  3241. int res;
  3242. u8 dst_buf[ETH_ALEN], *dst;
  3243. struct wpabuf *tlvs;
  3244. char *pos;
  3245. size_t len;
  3246. if (hwaddr_aton(cmd, dst_buf))
  3247. return -1;
  3248. dst = dst_buf;
  3249. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  3250. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  3251. dst = NULL;
  3252. pos = cmd + 17;
  3253. if (*pos != ' ')
  3254. return -1;
  3255. pos++;
  3256. if (os_strncmp(pos, "upnp ", 5) == 0) {
  3257. u8 version;
  3258. pos += 5;
  3259. if (hexstr2bin(pos, &version, 1) < 0)
  3260. return -1;
  3261. pos += 2;
  3262. if (*pos != ' ')
  3263. return -1;
  3264. pos++;
  3265. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  3266. #ifdef CONFIG_WIFI_DISPLAY
  3267. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  3268. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  3269. #endif /* CONFIG_WIFI_DISPLAY */
  3270. } else {
  3271. len = os_strlen(pos);
  3272. if (len & 1)
  3273. return -1;
  3274. len /= 2;
  3275. tlvs = wpabuf_alloc(len);
  3276. if (tlvs == NULL)
  3277. return -1;
  3278. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  3279. wpabuf_free(tlvs);
  3280. return -1;
  3281. }
  3282. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  3283. wpabuf_free(tlvs);
  3284. }
  3285. if (ref == 0)
  3286. return -1;
  3287. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  3288. if (res < 0 || (unsigned) res >= buflen)
  3289. return -1;
  3290. return res;
  3291. }
  3292. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  3293. char *cmd)
  3294. {
  3295. long long unsigned val;
  3296. u64 req;
  3297. if (sscanf(cmd, "%llx", &val) != 1)
  3298. return -1;
  3299. req = val;
  3300. return wpas_p2p_sd_cancel_request(wpa_s, req);
  3301. }
  3302. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  3303. {
  3304. int freq;
  3305. u8 dst[ETH_ALEN];
  3306. u8 dialog_token;
  3307. struct wpabuf *resp_tlvs;
  3308. char *pos, *pos2;
  3309. size_t len;
  3310. pos = os_strchr(cmd, ' ');
  3311. if (pos == NULL)
  3312. return -1;
  3313. *pos++ = '\0';
  3314. freq = atoi(cmd);
  3315. if (freq == 0)
  3316. return -1;
  3317. if (hwaddr_aton(pos, dst))
  3318. return -1;
  3319. pos += 17;
  3320. if (*pos != ' ')
  3321. return -1;
  3322. pos++;
  3323. pos2 = os_strchr(pos, ' ');
  3324. if (pos2 == NULL)
  3325. return -1;
  3326. *pos2++ = '\0';
  3327. dialog_token = atoi(pos);
  3328. len = os_strlen(pos2);
  3329. if (len & 1)
  3330. return -1;
  3331. len /= 2;
  3332. resp_tlvs = wpabuf_alloc(len);
  3333. if (resp_tlvs == NULL)
  3334. return -1;
  3335. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  3336. wpabuf_free(resp_tlvs);
  3337. return -1;
  3338. }
  3339. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  3340. wpabuf_free(resp_tlvs);
  3341. return 0;
  3342. }
  3343. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  3344. char *cmd)
  3345. {
  3346. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  3347. return -1;
  3348. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  3349. return 0;
  3350. }
  3351. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  3352. char *cmd)
  3353. {
  3354. char *pos;
  3355. size_t len;
  3356. struct wpabuf *query, *resp;
  3357. pos = os_strchr(cmd, ' ');
  3358. if (pos == NULL)
  3359. return -1;
  3360. *pos++ = '\0';
  3361. len = os_strlen(cmd);
  3362. if (len & 1)
  3363. return -1;
  3364. len /= 2;
  3365. query = wpabuf_alloc(len);
  3366. if (query == NULL)
  3367. return -1;
  3368. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3369. wpabuf_free(query);
  3370. return -1;
  3371. }
  3372. len = os_strlen(pos);
  3373. if (len & 1) {
  3374. wpabuf_free(query);
  3375. return -1;
  3376. }
  3377. len /= 2;
  3378. resp = wpabuf_alloc(len);
  3379. if (resp == NULL) {
  3380. wpabuf_free(query);
  3381. return -1;
  3382. }
  3383. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  3384. wpabuf_free(query);
  3385. wpabuf_free(resp);
  3386. return -1;
  3387. }
  3388. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  3389. wpabuf_free(query);
  3390. wpabuf_free(resp);
  3391. return -1;
  3392. }
  3393. return 0;
  3394. }
  3395. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3396. {
  3397. char *pos;
  3398. u8 version;
  3399. pos = os_strchr(cmd, ' ');
  3400. if (pos == NULL)
  3401. return -1;
  3402. *pos++ = '\0';
  3403. if (hexstr2bin(cmd, &version, 1) < 0)
  3404. return -1;
  3405. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  3406. }
  3407. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  3408. {
  3409. char *pos;
  3410. pos = os_strchr(cmd, ' ');
  3411. if (pos == NULL)
  3412. return -1;
  3413. *pos++ = '\0';
  3414. if (os_strcmp(cmd, "bonjour") == 0)
  3415. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  3416. if (os_strcmp(cmd, "upnp") == 0)
  3417. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  3418. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3419. return -1;
  3420. }
  3421. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  3422. char *cmd)
  3423. {
  3424. size_t len;
  3425. struct wpabuf *query;
  3426. int ret;
  3427. len = os_strlen(cmd);
  3428. if (len & 1)
  3429. return -1;
  3430. len /= 2;
  3431. query = wpabuf_alloc(len);
  3432. if (query == NULL)
  3433. return -1;
  3434. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3435. wpabuf_free(query);
  3436. return -1;
  3437. }
  3438. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  3439. wpabuf_free(query);
  3440. return ret;
  3441. }
  3442. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3443. {
  3444. char *pos;
  3445. u8 version;
  3446. pos = os_strchr(cmd, ' ');
  3447. if (pos == NULL)
  3448. return -1;
  3449. *pos++ = '\0';
  3450. if (hexstr2bin(cmd, &version, 1) < 0)
  3451. return -1;
  3452. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  3453. }
  3454. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  3455. {
  3456. char *pos;
  3457. pos = os_strchr(cmd, ' ');
  3458. if (pos == NULL)
  3459. return -1;
  3460. *pos++ = '\0';
  3461. if (os_strcmp(cmd, "bonjour") == 0)
  3462. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  3463. if (os_strcmp(cmd, "upnp") == 0)
  3464. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  3465. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3466. return -1;
  3467. }
  3468. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  3469. {
  3470. u8 addr[ETH_ALEN];
  3471. /* <addr> */
  3472. if (hwaddr_aton(cmd, addr))
  3473. return -1;
  3474. return wpas_p2p_reject(wpa_s, addr);
  3475. }
  3476. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  3477. {
  3478. char *pos;
  3479. int id;
  3480. struct wpa_ssid *ssid;
  3481. u8 *_peer = NULL, peer[ETH_ALEN];
  3482. int freq = 0, pref_freq = 0;
  3483. int ht40;
  3484. id = atoi(cmd);
  3485. pos = os_strstr(cmd, " peer=");
  3486. if (pos) {
  3487. pos += 6;
  3488. if (hwaddr_aton(pos, peer))
  3489. return -1;
  3490. _peer = peer;
  3491. }
  3492. ssid = wpa_config_get_network(wpa_s->conf, id);
  3493. if (ssid == NULL || ssid->disabled != 2) {
  3494. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3495. "for persistent P2P group",
  3496. id);
  3497. return -1;
  3498. }
  3499. pos = os_strstr(cmd, " freq=");
  3500. if (pos) {
  3501. pos += 6;
  3502. freq = atoi(pos);
  3503. if (freq <= 0)
  3504. return -1;
  3505. }
  3506. pos = os_strstr(cmd, " pref=");
  3507. if (pos) {
  3508. pos += 6;
  3509. pref_freq = atoi(pos);
  3510. if (pref_freq <= 0)
  3511. return -1;
  3512. }
  3513. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
  3514. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, pref_freq);
  3515. }
  3516. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  3517. {
  3518. char *pos;
  3519. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  3520. pos = os_strstr(cmd, " peer=");
  3521. if (!pos)
  3522. return -1;
  3523. *pos = '\0';
  3524. pos += 6;
  3525. if (hwaddr_aton(pos, peer)) {
  3526. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  3527. return -1;
  3528. }
  3529. pos = os_strstr(pos, " go_dev_addr=");
  3530. if (pos) {
  3531. pos += 13;
  3532. if (hwaddr_aton(pos, go_dev_addr)) {
  3533. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  3534. pos);
  3535. return -1;
  3536. }
  3537. go_dev = go_dev_addr;
  3538. }
  3539. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  3540. }
  3541. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  3542. {
  3543. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3544. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  3545. if (os_strncmp(cmd, "group=", 6) == 0)
  3546. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  3547. return -1;
  3548. }
  3549. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  3550. char *cmd, int freq, int ht40)
  3551. {
  3552. int id;
  3553. struct wpa_ssid *ssid;
  3554. id = atoi(cmd);
  3555. ssid = wpa_config_get_network(wpa_s->conf, id);
  3556. if (ssid == NULL || ssid->disabled != 2) {
  3557. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3558. "for persistent P2P group",
  3559. id);
  3560. return -1;
  3561. }
  3562. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40, NULL,
  3563. 0);
  3564. }
  3565. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  3566. {
  3567. int freq = 0, ht40;
  3568. char *pos;
  3569. pos = os_strstr(cmd, "freq=");
  3570. if (pos)
  3571. freq = atoi(pos + 5);
  3572. ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
  3573. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3574. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
  3575. ht40);
  3576. if (os_strcmp(cmd, "persistent") == 0 ||
  3577. os_strncmp(cmd, "persistent ", 11) == 0)
  3578. return wpas_p2p_group_add(wpa_s, 1, freq, ht40);
  3579. if (os_strncmp(cmd, "freq=", 5) == 0)
  3580. return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
  3581. if (ht40)
  3582. return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
  3583. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  3584. cmd);
  3585. return -1;
  3586. }
  3587. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  3588. char *buf, size_t buflen)
  3589. {
  3590. u8 addr[ETH_ALEN], *addr_ptr;
  3591. int next, res;
  3592. const struct p2p_peer_info *info;
  3593. char *pos, *end;
  3594. char devtype[WPS_DEV_TYPE_BUFSIZE];
  3595. struct wpa_ssid *ssid;
  3596. size_t i;
  3597. if (!wpa_s->global->p2p)
  3598. return -1;
  3599. if (os_strcmp(cmd, "FIRST") == 0) {
  3600. addr_ptr = NULL;
  3601. next = 0;
  3602. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3603. if (hwaddr_aton(cmd + 5, addr) < 0)
  3604. return -1;
  3605. addr_ptr = addr;
  3606. next = 1;
  3607. } else {
  3608. if (hwaddr_aton(cmd, addr) < 0)
  3609. return -1;
  3610. addr_ptr = addr;
  3611. next = 0;
  3612. }
  3613. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  3614. if (info == NULL)
  3615. return -1;
  3616. pos = buf;
  3617. end = buf + buflen;
  3618. res = os_snprintf(pos, end - pos, MACSTR "\n"
  3619. "pri_dev_type=%s\n"
  3620. "device_name=%s\n"
  3621. "manufacturer=%s\n"
  3622. "model_name=%s\n"
  3623. "model_number=%s\n"
  3624. "serial_number=%s\n"
  3625. "config_methods=0x%x\n"
  3626. "dev_capab=0x%x\n"
  3627. "group_capab=0x%x\n"
  3628. "level=%d\n",
  3629. MAC2STR(info->p2p_device_addr),
  3630. wps_dev_type_bin2str(info->pri_dev_type,
  3631. devtype, sizeof(devtype)),
  3632. info->device_name,
  3633. info->manufacturer,
  3634. info->model_name,
  3635. info->model_number,
  3636. info->serial_number,
  3637. info->config_methods,
  3638. info->dev_capab,
  3639. info->group_capab,
  3640. info->level);
  3641. if (res < 0 || res >= end - pos)
  3642. return pos - buf;
  3643. pos += res;
  3644. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  3645. {
  3646. const u8 *t;
  3647. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  3648. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  3649. wps_dev_type_bin2str(t, devtype,
  3650. sizeof(devtype)));
  3651. if (res < 0 || res >= end - pos)
  3652. return pos - buf;
  3653. pos += res;
  3654. }
  3655. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  3656. if (ssid) {
  3657. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  3658. if (res < 0 || res >= end - pos)
  3659. return pos - buf;
  3660. pos += res;
  3661. }
  3662. res = p2p_get_peer_info_txt(info, pos, end - pos);
  3663. if (res < 0)
  3664. return pos - buf;
  3665. pos += res;
  3666. return pos - buf;
  3667. }
  3668. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  3669. const char *param)
  3670. {
  3671. struct wpa_freq_range *freq = NULL, *n;
  3672. unsigned int count = 0, i;
  3673. const char *pos, *pos2, *pos3;
  3674. if (wpa_s->global->p2p == NULL)
  3675. return -1;
  3676. /*
  3677. * param includes comma separated frequency range.
  3678. * For example: 2412-2432,2462,5000-6000
  3679. */
  3680. pos = param;
  3681. while (pos && pos[0]) {
  3682. n = os_realloc_array(freq, count + 1,
  3683. sizeof(struct wpa_freq_range));
  3684. if (n == NULL) {
  3685. os_free(freq);
  3686. return -1;
  3687. }
  3688. freq = n;
  3689. freq[count].min = atoi(pos);
  3690. pos2 = os_strchr(pos, '-');
  3691. pos3 = os_strchr(pos, ',');
  3692. if (pos2 && (!pos3 || pos2 < pos3)) {
  3693. pos2++;
  3694. freq[count].max = atoi(pos2);
  3695. } else
  3696. freq[count].max = freq[count].min;
  3697. pos = pos3;
  3698. if (pos)
  3699. pos++;
  3700. count++;
  3701. }
  3702. for (i = 0; i < count; i++) {
  3703. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  3704. freq[i].min, freq[i].max);
  3705. }
  3706. os_free(wpa_s->global->p2p_disallow_freq);
  3707. wpa_s->global->p2p_disallow_freq = freq;
  3708. wpa_s->global->num_p2p_disallow_freq = count;
  3709. wpas_p2p_update_channel_list(wpa_s);
  3710. return 0;
  3711. }
  3712. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  3713. {
  3714. char *param;
  3715. if (wpa_s->global->p2p == NULL)
  3716. return -1;
  3717. param = os_strchr(cmd, ' ');
  3718. if (param == NULL)
  3719. return -1;
  3720. *param++ = '\0';
  3721. if (os_strcmp(cmd, "discoverability") == 0) {
  3722. p2p_set_client_discoverability(wpa_s->global->p2p,
  3723. atoi(param));
  3724. return 0;
  3725. }
  3726. if (os_strcmp(cmd, "managed") == 0) {
  3727. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  3728. return 0;
  3729. }
  3730. if (os_strcmp(cmd, "listen_channel") == 0) {
  3731. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  3732. atoi(param));
  3733. }
  3734. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  3735. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  3736. os_strlen(param));
  3737. }
  3738. if (os_strcmp(cmd, "noa") == 0) {
  3739. char *pos;
  3740. int count, start, duration;
  3741. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  3742. count = atoi(param);
  3743. pos = os_strchr(param, ',');
  3744. if (pos == NULL)
  3745. return -1;
  3746. pos++;
  3747. start = atoi(pos);
  3748. pos = os_strchr(pos, ',');
  3749. if (pos == NULL)
  3750. return -1;
  3751. pos++;
  3752. duration = atoi(pos);
  3753. if (count < 0 || count > 255 || start < 0 || duration < 0)
  3754. return -1;
  3755. if (count == 0 && duration > 0)
  3756. return -1;
  3757. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  3758. "start=%d duration=%d", count, start, duration);
  3759. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  3760. }
  3761. if (os_strcmp(cmd, "ps") == 0)
  3762. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  3763. if (os_strcmp(cmd, "oppps") == 0)
  3764. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  3765. if (os_strcmp(cmd, "ctwindow") == 0)
  3766. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  3767. if (os_strcmp(cmd, "disabled") == 0) {
  3768. wpa_s->global->p2p_disabled = atoi(param);
  3769. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  3770. wpa_s->global->p2p_disabled ?
  3771. "disabled" : "enabled");
  3772. if (wpa_s->global->p2p_disabled) {
  3773. wpas_p2p_stop_find(wpa_s);
  3774. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3775. p2p_flush(wpa_s->global->p2p);
  3776. }
  3777. return 0;
  3778. }
  3779. if (os_strcmp(cmd, "conc_pref") == 0) {
  3780. if (os_strcmp(param, "sta") == 0)
  3781. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  3782. else if (os_strcmp(param, "p2p") == 0)
  3783. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  3784. else {
  3785. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  3786. return -1;
  3787. }
  3788. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  3789. "%s", param);
  3790. return 0;
  3791. }
  3792. if (os_strcmp(cmd, "force_long_sd") == 0) {
  3793. wpa_s->force_long_sd = atoi(param);
  3794. return 0;
  3795. }
  3796. if (os_strcmp(cmd, "peer_filter") == 0) {
  3797. u8 addr[ETH_ALEN];
  3798. if (hwaddr_aton(param, addr))
  3799. return -1;
  3800. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  3801. return 0;
  3802. }
  3803. if (os_strcmp(cmd, "cross_connect") == 0)
  3804. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  3805. if (os_strcmp(cmd, "go_apsd") == 0) {
  3806. if (os_strcmp(param, "disable") == 0)
  3807. wpa_s->set_ap_uapsd = 0;
  3808. else {
  3809. wpa_s->set_ap_uapsd = 1;
  3810. wpa_s->ap_uapsd = atoi(param);
  3811. }
  3812. return 0;
  3813. }
  3814. if (os_strcmp(cmd, "client_apsd") == 0) {
  3815. if (os_strcmp(param, "disable") == 0)
  3816. wpa_s->set_sta_uapsd = 0;
  3817. else {
  3818. int be, bk, vi, vo;
  3819. char *pos;
  3820. /* format: BE,BK,VI,VO;max SP Length */
  3821. be = atoi(param);
  3822. pos = os_strchr(param, ',');
  3823. if (pos == NULL)
  3824. return -1;
  3825. pos++;
  3826. bk = atoi(pos);
  3827. pos = os_strchr(pos, ',');
  3828. if (pos == NULL)
  3829. return -1;
  3830. pos++;
  3831. vi = atoi(pos);
  3832. pos = os_strchr(pos, ',');
  3833. if (pos == NULL)
  3834. return -1;
  3835. pos++;
  3836. vo = atoi(pos);
  3837. /* ignore max SP Length for now */
  3838. wpa_s->set_sta_uapsd = 1;
  3839. wpa_s->sta_uapsd = 0;
  3840. if (be)
  3841. wpa_s->sta_uapsd |= BIT(0);
  3842. if (bk)
  3843. wpa_s->sta_uapsd |= BIT(1);
  3844. if (vi)
  3845. wpa_s->sta_uapsd |= BIT(2);
  3846. if (vo)
  3847. wpa_s->sta_uapsd |= BIT(3);
  3848. }
  3849. return 0;
  3850. }
  3851. if (os_strcmp(cmd, "disallow_freq") == 0)
  3852. return p2p_ctrl_disallow_freq(wpa_s, param);
  3853. if (os_strcmp(cmd, "disc_int") == 0) {
  3854. int min_disc_int, max_disc_int, max_disc_tu;
  3855. char *pos;
  3856. pos = param;
  3857. min_disc_int = atoi(pos);
  3858. pos = os_strchr(pos, ' ');
  3859. if (pos == NULL)
  3860. return -1;
  3861. *pos++ = '\0';
  3862. max_disc_int = atoi(pos);
  3863. pos = os_strchr(pos, ' ');
  3864. if (pos == NULL)
  3865. return -1;
  3866. *pos++ = '\0';
  3867. max_disc_tu = atoi(pos);
  3868. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  3869. max_disc_int, max_disc_tu);
  3870. }
  3871. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  3872. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  3873. return 0;
  3874. }
  3875. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  3876. cmd);
  3877. return -1;
  3878. }
  3879. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  3880. {
  3881. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3882. wpa_s->force_long_sd = 0;
  3883. if (wpa_s->global->p2p)
  3884. p2p_flush(wpa_s->global->p2p);
  3885. }
  3886. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  3887. {
  3888. char *pos, *pos2;
  3889. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  3890. if (cmd[0]) {
  3891. pos = os_strchr(cmd, ' ');
  3892. if (pos == NULL)
  3893. return -1;
  3894. *pos++ = '\0';
  3895. dur1 = atoi(cmd);
  3896. pos2 = os_strchr(pos, ' ');
  3897. if (pos2)
  3898. *pos2++ = '\0';
  3899. int1 = atoi(pos);
  3900. } else
  3901. pos2 = NULL;
  3902. if (pos2) {
  3903. pos = os_strchr(pos2, ' ');
  3904. if (pos == NULL)
  3905. return -1;
  3906. *pos++ = '\0';
  3907. dur2 = atoi(pos2);
  3908. int2 = atoi(pos);
  3909. }
  3910. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  3911. }
  3912. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3913. {
  3914. char *pos;
  3915. unsigned int period = 0, interval = 0;
  3916. if (cmd[0]) {
  3917. pos = os_strchr(cmd, ' ');
  3918. if (pos == NULL)
  3919. return -1;
  3920. *pos++ = '\0';
  3921. period = atoi(cmd);
  3922. interval = atoi(pos);
  3923. }
  3924. return wpas_p2p_ext_listen(wpa_s, period, interval);
  3925. }
  3926. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  3927. {
  3928. const char *pos;
  3929. u8 peer[ETH_ALEN];
  3930. int iface_addr = 0;
  3931. pos = cmd;
  3932. if (os_strncmp(pos, "iface=", 6) == 0) {
  3933. iface_addr = 1;
  3934. pos += 6;
  3935. }
  3936. if (hwaddr_aton(pos, peer))
  3937. return -1;
  3938. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  3939. return 0;
  3940. }
  3941. #endif /* CONFIG_P2P */
  3942. #ifdef CONFIG_INTERWORKING
  3943. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  3944. {
  3945. u8 bssid[ETH_ALEN];
  3946. struct wpa_bss *bss;
  3947. if (hwaddr_aton(dst, bssid)) {
  3948. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  3949. return -1;
  3950. }
  3951. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3952. if (bss == NULL) {
  3953. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  3954. MAC2STR(bssid));
  3955. return -1;
  3956. }
  3957. return interworking_connect(wpa_s, bss);
  3958. }
  3959. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  3960. {
  3961. u8 dst_addr[ETH_ALEN];
  3962. int used;
  3963. char *pos;
  3964. #define MAX_ANQP_INFO_ID 100
  3965. u16 id[MAX_ANQP_INFO_ID];
  3966. size_t num_id = 0;
  3967. used = hwaddr_aton2(dst, dst_addr);
  3968. if (used < 0)
  3969. return -1;
  3970. pos = dst + used;
  3971. while (num_id < MAX_ANQP_INFO_ID) {
  3972. id[num_id] = atoi(pos);
  3973. if (id[num_id])
  3974. num_id++;
  3975. pos = os_strchr(pos + 1, ',');
  3976. if (pos == NULL)
  3977. break;
  3978. pos++;
  3979. }
  3980. if (num_id == 0)
  3981. return -1;
  3982. return anqp_send_req(wpa_s, dst_addr, id, num_id);
  3983. }
  3984. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  3985. {
  3986. u8 dst_addr[ETH_ALEN];
  3987. struct wpabuf *advproto, *query = NULL;
  3988. int used, ret = -1;
  3989. char *pos, *end;
  3990. size_t len;
  3991. used = hwaddr_aton2(cmd, dst_addr);
  3992. if (used < 0)
  3993. return -1;
  3994. pos = cmd + used;
  3995. while (*pos == ' ')
  3996. pos++;
  3997. /* Advertisement Protocol ID */
  3998. end = os_strchr(pos, ' ');
  3999. if (end)
  4000. len = end - pos;
  4001. else
  4002. len = os_strlen(pos);
  4003. if (len & 0x01)
  4004. return -1;
  4005. len /= 2;
  4006. if (len == 0)
  4007. return -1;
  4008. advproto = wpabuf_alloc(len);
  4009. if (advproto == NULL)
  4010. return -1;
  4011. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  4012. goto fail;
  4013. if (end) {
  4014. /* Optional Query Request */
  4015. pos = end + 1;
  4016. while (*pos == ' ')
  4017. pos++;
  4018. len = os_strlen(pos);
  4019. if (len) {
  4020. if (len & 0x01)
  4021. goto fail;
  4022. len /= 2;
  4023. if (len == 0)
  4024. goto fail;
  4025. query = wpabuf_alloc(len);
  4026. if (query == NULL)
  4027. goto fail;
  4028. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  4029. goto fail;
  4030. }
  4031. }
  4032. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  4033. fail:
  4034. wpabuf_free(advproto);
  4035. wpabuf_free(query);
  4036. return ret;
  4037. }
  4038. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  4039. size_t buflen)
  4040. {
  4041. u8 addr[ETH_ALEN];
  4042. int dialog_token;
  4043. int used;
  4044. char *pos;
  4045. size_t resp_len, start, requested_len;
  4046. if (!wpa_s->last_gas_resp)
  4047. return -1;
  4048. used = hwaddr_aton2(cmd, addr);
  4049. if (used < 0)
  4050. return -1;
  4051. pos = cmd + used;
  4052. while (*pos == ' ')
  4053. pos++;
  4054. dialog_token = atoi(pos);
  4055. if (os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) != 0 ||
  4056. dialog_token != wpa_s->last_gas_dialog_token)
  4057. return -1;
  4058. resp_len = wpabuf_len(wpa_s->last_gas_resp);
  4059. start = 0;
  4060. requested_len = resp_len;
  4061. pos = os_strchr(pos, ' ');
  4062. if (pos) {
  4063. start = atoi(pos);
  4064. if (start > resp_len)
  4065. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4066. pos = os_strchr(pos, ',');
  4067. if (pos == NULL)
  4068. return -1;
  4069. pos++;
  4070. requested_len = atoi(pos);
  4071. if (start + requested_len > resp_len)
  4072. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4073. }
  4074. if (requested_len * 2 + 1 > buflen)
  4075. return os_snprintf(buf, buflen, "FAIL-Too long response");
  4076. return wpa_snprintf_hex(buf, buflen,
  4077. wpabuf_head_u8(wpa_s->last_gas_resp) + start,
  4078. requested_len);
  4079. }
  4080. #endif /* CONFIG_INTERWORKING */
  4081. #ifdef CONFIG_HS20
  4082. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4083. {
  4084. u8 dst_addr[ETH_ALEN];
  4085. int used;
  4086. char *pos;
  4087. u32 subtypes = 0;
  4088. used = hwaddr_aton2(dst, dst_addr);
  4089. if (used < 0)
  4090. return -1;
  4091. pos = dst + used;
  4092. for (;;) {
  4093. int num = atoi(pos);
  4094. if (num <= 0 || num > 31)
  4095. return -1;
  4096. subtypes |= BIT(num);
  4097. pos = os_strchr(pos + 1, ',');
  4098. if (pos == NULL)
  4099. break;
  4100. pos++;
  4101. }
  4102. if (subtypes == 0)
  4103. return -1;
  4104. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
  4105. }
  4106. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4107. const u8 *addr, const char *realm)
  4108. {
  4109. u8 *buf;
  4110. size_t rlen, len;
  4111. int ret;
  4112. rlen = os_strlen(realm);
  4113. len = 3 + rlen;
  4114. buf = os_malloc(len);
  4115. if (buf == NULL)
  4116. return -1;
  4117. buf[0] = 1; /* NAI Home Realm Count */
  4118. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  4119. buf[2] = rlen;
  4120. os_memcpy(buf + 3, realm, rlen);
  4121. ret = hs20_anqp_send_req(wpa_s, addr,
  4122. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4123. buf, len);
  4124. os_free(buf);
  4125. return ret;
  4126. }
  4127. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4128. char *dst)
  4129. {
  4130. struct wpa_cred *cred = wpa_s->conf->cred;
  4131. u8 dst_addr[ETH_ALEN];
  4132. int used;
  4133. u8 *buf;
  4134. size_t len;
  4135. int ret;
  4136. used = hwaddr_aton2(dst, dst_addr);
  4137. if (used < 0)
  4138. return -1;
  4139. while (dst[used] == ' ')
  4140. used++;
  4141. if (os_strncmp(dst + used, "realm=", 6) == 0)
  4142. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  4143. dst + used + 6);
  4144. len = os_strlen(dst + used);
  4145. if (len == 0 && cred && cred->realm)
  4146. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  4147. if (len % 1)
  4148. return -1;
  4149. len /= 2;
  4150. buf = os_malloc(len);
  4151. if (buf == NULL)
  4152. return -1;
  4153. if (hexstr2bin(dst + used, buf, len) < 0) {
  4154. os_free(buf);
  4155. return -1;
  4156. }
  4157. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  4158. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4159. buf, len);
  4160. os_free(buf);
  4161. return ret;
  4162. }
  4163. #endif /* CONFIG_HS20 */
  4164. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  4165. struct wpa_supplicant *wpa_s, char *cmd)
  4166. {
  4167. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  4168. return 0;
  4169. }
  4170. #ifdef CONFIG_AUTOSCAN
  4171. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  4172. char *cmd)
  4173. {
  4174. enum wpa_states state = wpa_s->wpa_state;
  4175. char *new_params = NULL;
  4176. if (os_strlen(cmd) > 0) {
  4177. new_params = os_strdup(cmd);
  4178. if (new_params == NULL)
  4179. return -1;
  4180. }
  4181. os_free(wpa_s->conf->autoscan);
  4182. wpa_s->conf->autoscan = new_params;
  4183. if (wpa_s->conf->autoscan == NULL)
  4184. autoscan_deinit(wpa_s);
  4185. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  4186. autoscan_init(wpa_s, 1);
  4187. else if (state == WPA_SCANNING)
  4188. wpa_supplicant_reinit_autoscan(wpa_s);
  4189. return 0;
  4190. }
  4191. #endif /* CONFIG_AUTOSCAN */
  4192. #ifdef CONFIG_WNM
  4193. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  4194. {
  4195. int enter;
  4196. int intval = 0;
  4197. char *pos;
  4198. int ret;
  4199. struct wpabuf *tfs_req = NULL;
  4200. if (os_strncmp(cmd, "enter", 5) == 0)
  4201. enter = 1;
  4202. else if (os_strncmp(cmd, "exit", 4) == 0)
  4203. enter = 0;
  4204. else
  4205. return -1;
  4206. pos = os_strstr(cmd, " interval=");
  4207. if (pos)
  4208. intval = atoi(pos + 10);
  4209. pos = os_strstr(cmd, " tfs_req=");
  4210. if (pos) {
  4211. char *end;
  4212. size_t len;
  4213. pos += 9;
  4214. end = os_strchr(pos, ' ');
  4215. if (end)
  4216. len = end - pos;
  4217. else
  4218. len = os_strlen(pos);
  4219. if (len & 1)
  4220. return -1;
  4221. len /= 2;
  4222. tfs_req = wpabuf_alloc(len);
  4223. if (tfs_req == NULL)
  4224. return -1;
  4225. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  4226. wpabuf_free(tfs_req);
  4227. return -1;
  4228. }
  4229. }
  4230. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  4231. WNM_SLEEP_MODE_EXIT, intval,
  4232. tfs_req);
  4233. wpabuf_free(tfs_req);
  4234. return ret;
  4235. }
  4236. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  4237. {
  4238. int query_reason;
  4239. query_reason = atoi(cmd);
  4240. wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
  4241. query_reason);
  4242. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
  4243. }
  4244. #endif /* CONFIG_WNM */
  4245. /* Get string representation of channel width */
  4246. static const char * channel_width_name(enum chan_width width)
  4247. {
  4248. switch (width) {
  4249. case CHAN_WIDTH_20_NOHT:
  4250. return "20 MHz (no HT)";
  4251. case CHAN_WIDTH_20:
  4252. return "20 MHz";
  4253. case CHAN_WIDTH_40:
  4254. return "40 MHz";
  4255. case CHAN_WIDTH_80:
  4256. return "80 MHz";
  4257. case CHAN_WIDTH_80P80:
  4258. return "80+80 MHz";
  4259. case CHAN_WIDTH_160:
  4260. return "160 MHz";
  4261. default:
  4262. return "unknown";
  4263. }
  4264. }
  4265. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  4266. size_t buflen)
  4267. {
  4268. struct wpa_signal_info si;
  4269. int ret;
  4270. char *pos, *end;
  4271. ret = wpa_drv_signal_poll(wpa_s, &si);
  4272. if (ret)
  4273. return -1;
  4274. pos = buf;
  4275. end = buf + buflen;
  4276. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  4277. "NOISE=%d\nFREQUENCY=%u\n",
  4278. si.current_signal, si.current_txrate / 1000,
  4279. si.current_noise, si.frequency);
  4280. if (ret < 0 || ret > end - pos)
  4281. return -1;
  4282. pos += ret;
  4283. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  4284. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  4285. channel_width_name(si.chanwidth));
  4286. if (ret < 0 || ret > end - pos)
  4287. return -1;
  4288. pos += ret;
  4289. }
  4290. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  4291. ret = os_snprintf(pos, end - pos,
  4292. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  4293. si.center_frq1, si.center_frq2);
  4294. if (ret < 0 || ret > end - pos)
  4295. return -1;
  4296. pos += ret;
  4297. }
  4298. if (si.avg_signal) {
  4299. ret = os_snprintf(pos, end - pos,
  4300. "AVG_RSSI=%d\n", si.avg_signal);
  4301. if (ret < 0 || ret >= end - pos)
  4302. return -1;
  4303. pos += ret;
  4304. }
  4305. return pos - buf;
  4306. }
  4307. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  4308. size_t buflen)
  4309. {
  4310. struct hostap_sta_driver_data sta;
  4311. int ret;
  4312. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  4313. if (ret)
  4314. return -1;
  4315. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  4316. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  4317. if (ret < 0 || (size_t) ret > buflen)
  4318. return -1;
  4319. return ret;
  4320. }
  4321. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  4322. {
  4323. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  4324. #ifdef CONFIG_P2P
  4325. wpas_p2p_stop_find(wpa_s);
  4326. p2p_ctrl_flush(wpa_s);
  4327. wpas_p2p_group_remove(wpa_s, "*");
  4328. #endif /* CONFIG_P2P */
  4329. #ifdef CONFIG_WPS_TESTING
  4330. wps_version_number = 0x20;
  4331. wps_testing_dummy_cred = 0;
  4332. #endif /* CONFIG_WPS_TESTING */
  4333. #ifdef CONFIG_WPS
  4334. wpas_wps_cancel(wpa_s);
  4335. #endif /* CONFIG_WPS */
  4336. #ifdef CONFIG_TDLS_TESTING
  4337. extern unsigned int tdls_testing;
  4338. tdls_testing = 0;
  4339. #endif /* CONFIG_TDLS_TESTING */
  4340. #ifdef CONFIG_TDLS
  4341. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  4342. wpa_tdls_enable(wpa_s->wpa, 1);
  4343. #endif /* CONFIG_TDLS */
  4344. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  4345. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  4346. wpa_s->no_keep_alive = 0;
  4347. os_free(wpa_s->disallow_aps_bssid);
  4348. wpa_s->disallow_aps_bssid = NULL;
  4349. wpa_s->disallow_aps_bssid_count = 0;
  4350. os_free(wpa_s->disallow_aps_ssid);
  4351. wpa_s->disallow_aps_ssid = NULL;
  4352. wpa_s->disallow_aps_ssid_count = 0;
  4353. wpa_s->set_sta_uapsd = 0;
  4354. wpa_s->sta_uapsd = 0;
  4355. wpa_drv_radio_disable(wpa_s, 0);
  4356. wpa_bss_flush(wpa_s);
  4357. wpa_blacklist_clear(wpa_s);
  4358. wpa_s->extra_blacklist_count = 0;
  4359. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  4360. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  4361. }
  4362. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  4363. char *buf, size_t *resp_len)
  4364. {
  4365. char *reply;
  4366. const int reply_size = 4096;
  4367. int ctrl_rsp = 0;
  4368. int reply_len;
  4369. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  4370. os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
  4371. os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  4372. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
  4373. os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
  4374. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  4375. (const u8 *) buf, os_strlen(buf));
  4376. } else {
  4377. int level = MSG_DEBUG;
  4378. if (os_strcmp(buf, "PING") == 0)
  4379. level = MSG_EXCESSIVE;
  4380. wpa_hexdump_ascii(level, "RX ctrl_iface",
  4381. (const u8 *) buf, os_strlen(buf));
  4382. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  4383. }
  4384. reply = os_malloc(reply_size);
  4385. if (reply == NULL) {
  4386. *resp_len = 1;
  4387. return NULL;
  4388. }
  4389. os_memcpy(reply, "OK\n", 3);
  4390. reply_len = 3;
  4391. if (os_strcmp(buf, "PING") == 0) {
  4392. os_memcpy(reply, "PONG\n", 5);
  4393. reply_len = 5;
  4394. } else if (os_strcmp(buf, "IFNAME") == 0) {
  4395. reply_len = os_strlen(wpa_s->ifname);
  4396. os_memcpy(reply, wpa_s->ifname, reply_len);
  4397. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  4398. if (wpa_debug_reopen_file() < 0)
  4399. reply_len = -1;
  4400. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  4401. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  4402. } else if (os_strcmp(buf, "MIB") == 0) {
  4403. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  4404. if (reply_len >= 0) {
  4405. int res;
  4406. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  4407. reply_size - reply_len);
  4408. if (res < 0)
  4409. reply_len = -1;
  4410. else
  4411. reply_len += res;
  4412. }
  4413. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  4414. reply_len = wpa_supplicant_ctrl_iface_status(
  4415. wpa_s, buf + 6, reply, reply_size);
  4416. } else if (os_strcmp(buf, "PMKSA") == 0) {
  4417. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  4418. reply_size);
  4419. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  4420. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  4421. reply_len = -1;
  4422. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  4423. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  4424. reply, reply_size);
  4425. } else if (os_strcmp(buf, "LOGON") == 0) {
  4426. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  4427. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  4428. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  4429. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  4430. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4431. reply_len = -1;
  4432. else
  4433. wpas_request_connection(wpa_s);
  4434. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  4435. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4436. reply_len = -1;
  4437. else if (wpa_s->disconnected)
  4438. wpas_request_connection(wpa_s);
  4439. #ifdef IEEE8021X_EAPOL
  4440. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  4441. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  4442. reply_len = -1;
  4443. #endif /* IEEE8021X_EAPOL */
  4444. #ifdef CONFIG_PEERKEY
  4445. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  4446. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  4447. reply_len = -1;
  4448. #endif /* CONFIG_PEERKEY */
  4449. #ifdef CONFIG_IEEE80211R
  4450. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  4451. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  4452. reply_len = -1;
  4453. #endif /* CONFIG_IEEE80211R */
  4454. #ifdef CONFIG_WPS
  4455. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  4456. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  4457. if (res == -2) {
  4458. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4459. reply_len = 17;
  4460. } else if (res)
  4461. reply_len = -1;
  4462. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  4463. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  4464. if (res == -2) {
  4465. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4466. reply_len = 17;
  4467. } else if (res)
  4468. reply_len = -1;
  4469. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  4470. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  4471. reply,
  4472. reply_size);
  4473. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  4474. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  4475. wpa_s, buf + 14, reply, reply_size);
  4476. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  4477. if (wpas_wps_cancel(wpa_s))
  4478. reply_len = -1;
  4479. #ifdef CONFIG_WPS_NFC
  4480. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  4481. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  4482. reply_len = -1;
  4483. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  4484. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  4485. reply_len = -1;
  4486. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  4487. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  4488. wpa_s, buf + 21, reply, reply_size);
  4489. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  4490. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  4491. wpa_s, buf + 14, reply, reply_size);
  4492. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  4493. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  4494. buf + 17))
  4495. reply_len = -1;
  4496. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  4497. reply_len = wpas_ctrl_nfc_get_handover_req(
  4498. wpa_s, buf + 21, reply, reply_size);
  4499. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  4500. reply_len = wpas_ctrl_nfc_get_handover_sel(
  4501. wpa_s, buf + 21, reply, reply_size);
  4502. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
  4503. reply_len = wpas_ctrl_nfc_rx_handover_req(
  4504. wpa_s, buf + 20, reply, reply_size);
  4505. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
  4506. if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
  4507. reply_len = -1;
  4508. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  4509. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  4510. reply_len = -1;
  4511. #endif /* CONFIG_WPS_NFC */
  4512. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  4513. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  4514. reply_len = -1;
  4515. #ifdef CONFIG_AP
  4516. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  4517. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  4518. wpa_s, buf + 11, reply, reply_size);
  4519. #endif /* CONFIG_AP */
  4520. #ifdef CONFIG_WPS_ER
  4521. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  4522. if (wpas_wps_er_start(wpa_s, NULL))
  4523. reply_len = -1;
  4524. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  4525. if (wpas_wps_er_start(wpa_s, buf + 13))
  4526. reply_len = -1;
  4527. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  4528. if (wpas_wps_er_stop(wpa_s))
  4529. reply_len = -1;
  4530. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  4531. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  4532. reply_len = -1;
  4533. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  4534. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  4535. if (ret == -2) {
  4536. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4537. reply_len = 17;
  4538. } else if (ret == -3) {
  4539. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  4540. reply_len = 18;
  4541. } else if (ret == -4) {
  4542. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  4543. reply_len = 20;
  4544. } else if (ret)
  4545. reply_len = -1;
  4546. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  4547. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  4548. reply_len = -1;
  4549. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  4550. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  4551. buf + 18))
  4552. reply_len = -1;
  4553. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  4554. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  4555. reply_len = -1;
  4556. #ifdef CONFIG_WPS_NFC
  4557. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  4558. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  4559. wpa_s, buf + 24, reply, reply_size);
  4560. #endif /* CONFIG_WPS_NFC */
  4561. #endif /* CONFIG_WPS_ER */
  4562. #endif /* CONFIG_WPS */
  4563. #ifdef CONFIG_IBSS_RSN
  4564. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  4565. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  4566. reply_len = -1;
  4567. #endif /* CONFIG_IBSS_RSN */
  4568. #ifdef CONFIG_P2P
  4569. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  4570. if (p2p_ctrl_find(wpa_s, buf + 9))
  4571. reply_len = -1;
  4572. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  4573. if (p2p_ctrl_find(wpa_s, ""))
  4574. reply_len = -1;
  4575. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  4576. wpas_p2p_stop_find(wpa_s);
  4577. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  4578. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  4579. reply_size);
  4580. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  4581. if (p2p_ctrl_listen(wpa_s, buf + 11))
  4582. reply_len = -1;
  4583. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  4584. if (p2p_ctrl_listen(wpa_s, ""))
  4585. reply_len = -1;
  4586. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  4587. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  4588. reply_len = -1;
  4589. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  4590. if (wpas_p2p_group_add(wpa_s, 0, 0, 0))
  4591. reply_len = -1;
  4592. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  4593. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  4594. reply_len = -1;
  4595. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  4596. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  4597. reply_len = -1;
  4598. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  4599. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  4600. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  4601. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  4602. reply_size);
  4603. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  4604. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  4605. reply_len = -1;
  4606. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  4607. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  4608. reply_len = -1;
  4609. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  4610. wpas_p2p_sd_service_update(wpa_s);
  4611. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  4612. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  4613. reply_len = -1;
  4614. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  4615. wpas_p2p_service_flush(wpa_s);
  4616. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  4617. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  4618. reply_len = -1;
  4619. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  4620. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  4621. reply_len = -1;
  4622. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  4623. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  4624. reply_len = -1;
  4625. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  4626. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  4627. reply_len = -1;
  4628. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  4629. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  4630. reply_size);
  4631. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  4632. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  4633. reply_len = -1;
  4634. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  4635. p2p_ctrl_flush(wpa_s);
  4636. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  4637. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  4638. reply_len = -1;
  4639. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  4640. if (wpas_p2p_cancel(wpa_s))
  4641. reply_len = -1;
  4642. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  4643. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  4644. reply_len = -1;
  4645. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  4646. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  4647. reply_len = -1;
  4648. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  4649. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  4650. reply_len = -1;
  4651. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  4652. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  4653. reply_len = -1;
  4654. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  4655. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  4656. reply_len = -1;
  4657. #endif /* CONFIG_P2P */
  4658. #ifdef CONFIG_WIFI_DISPLAY
  4659. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  4660. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  4661. reply_len = -1;
  4662. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  4663. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  4664. reply, reply_size);
  4665. #endif /* CONFIG_WIFI_DISPLAY */
  4666. #ifdef CONFIG_INTERWORKING
  4667. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  4668. if (interworking_fetch_anqp(wpa_s) < 0)
  4669. reply_len = -1;
  4670. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  4671. interworking_stop_fetch_anqp(wpa_s);
  4672. } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
  4673. if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
  4674. NULL) < 0)
  4675. reply_len = -1;
  4676. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  4677. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  4678. reply_len = -1;
  4679. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  4680. if (get_anqp(wpa_s, buf + 9) < 0)
  4681. reply_len = -1;
  4682. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  4683. if (gas_request(wpa_s, buf + 12) < 0)
  4684. reply_len = -1;
  4685. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  4686. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  4687. reply_size);
  4688. #endif /* CONFIG_INTERWORKING */
  4689. #ifdef CONFIG_HS20
  4690. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  4691. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  4692. reply_len = -1;
  4693. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  4694. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  4695. reply_len = -1;
  4696. #endif /* CONFIG_HS20 */
  4697. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  4698. {
  4699. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  4700. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  4701. reply_len = -1;
  4702. else
  4703. ctrl_rsp = 1;
  4704. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  4705. if (wpa_supplicant_reload_configuration(wpa_s))
  4706. reply_len = -1;
  4707. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  4708. wpa_supplicant_terminate_proc(wpa_s->global);
  4709. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  4710. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  4711. reply_len = -1;
  4712. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  4713. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  4714. wpa_s, buf + 9, reply, reply_size);
  4715. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  4716. reply_len = wpa_supplicant_ctrl_iface_log_level(
  4717. wpa_s, buf + 9, reply, reply_size);
  4718. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  4719. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  4720. wpa_s, reply, reply_size);
  4721. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  4722. #ifdef CONFIG_SME
  4723. wpa_s->sme.prev_bssid_set = 0;
  4724. #endif /* CONFIG_SME */
  4725. wpa_s->reassociate = 0;
  4726. wpa_s->disconnected = 1;
  4727. wpa_supplicant_cancel_sched_scan(wpa_s);
  4728. wpa_supplicant_cancel_scan(wpa_s);
  4729. wpa_supplicant_deauthenticate(wpa_s,
  4730. WLAN_REASON_DEAUTH_LEAVING);
  4731. } else if (os_strcmp(buf, "SCAN") == 0 ||
  4732. os_strncmp(buf, "SCAN ", 5) == 0) {
  4733. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4734. reply_len = -1;
  4735. else {
  4736. if (os_strlen(buf) > 4 &&
  4737. os_strncasecmp(buf + 5, "TYPE=ONLY", 9) == 0)
  4738. wpa_s->scan_res_handler = scan_only_handler;
  4739. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  4740. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  4741. (wpa_s->wpa_state == WPA_COMPLETED))) {
  4742. wpa_s->normal_scans = 0;
  4743. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4744. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4745. } else if (wpa_s->sched_scanning) {
  4746. wpa_printf(MSG_DEBUG, "Stop ongoing "
  4747. "sched_scan to allow requested "
  4748. "full scan to proceed");
  4749. wpa_supplicant_cancel_sched_scan(wpa_s);
  4750. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4751. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4752. } else {
  4753. wpa_printf(MSG_DEBUG, "Ongoing scan action - "
  4754. "reject new request");
  4755. reply_len = os_snprintf(reply, reply_size,
  4756. "FAIL-BUSY\n");
  4757. }
  4758. }
  4759. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  4760. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  4761. wpa_s, reply, reply_size);
  4762. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  4763. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  4764. reply_len = -1;
  4765. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  4766. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  4767. reply_len = -1;
  4768. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  4769. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  4770. reply_len = -1;
  4771. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  4772. reply_len = wpa_supplicant_ctrl_iface_add_network(
  4773. wpa_s, reply, reply_size);
  4774. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  4775. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  4776. reply_len = -1;
  4777. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  4778. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  4779. reply_len = -1;
  4780. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  4781. reply_len = wpa_supplicant_ctrl_iface_get_network(
  4782. wpa_s, buf + 12, reply, reply_size);
  4783. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  4784. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  4785. wpa_s, reply, reply_size);
  4786. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  4787. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  4788. wpa_s, reply, reply_size);
  4789. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  4790. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  4791. reply_len = -1;
  4792. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  4793. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  4794. reply_len = -1;
  4795. #ifndef CONFIG_NO_CONFIG_WRITE
  4796. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  4797. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  4798. reply_len = -1;
  4799. #endif /* CONFIG_NO_CONFIG_WRITE */
  4800. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  4801. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  4802. wpa_s, buf + 15, reply, reply_size);
  4803. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  4804. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  4805. reply_len = -1;
  4806. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  4807. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  4808. reply_len = -1;
  4809. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  4810. reply_len = wpa_supplicant_global_iface_list(
  4811. wpa_s->global, reply, reply_size);
  4812. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  4813. reply_len = wpa_supplicant_global_iface_interfaces(
  4814. wpa_s->global, reply, reply_size);
  4815. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  4816. reply_len = wpa_supplicant_ctrl_iface_bss(
  4817. wpa_s, buf + 4, reply, reply_size);
  4818. #ifdef CONFIG_AP
  4819. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  4820. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  4821. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  4822. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  4823. reply_size);
  4824. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  4825. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  4826. reply_size);
  4827. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  4828. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  4829. reply_len = -1;
  4830. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  4831. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  4832. reply_len = -1;
  4833. #endif /* CONFIG_AP */
  4834. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  4835. wpas_notify_suspend(wpa_s->global);
  4836. } else if (os_strcmp(buf, "RESUME") == 0) {
  4837. wpas_notify_resume(wpa_s->global);
  4838. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  4839. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  4840. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  4841. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  4842. reply_len = -1;
  4843. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  4844. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  4845. reply_len = -1;
  4846. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  4847. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  4848. reply_len = -1;
  4849. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  4850. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  4851. buf + 17))
  4852. reply_len = -1;
  4853. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  4854. if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
  4855. reply_len = -1;
  4856. #ifdef CONFIG_TDLS
  4857. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  4858. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  4859. reply_len = -1;
  4860. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  4861. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  4862. reply_len = -1;
  4863. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  4864. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  4865. reply_len = -1;
  4866. #endif /* CONFIG_TDLS */
  4867. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  4868. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  4869. reply_size);
  4870. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  4871. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  4872. reply_size);
  4873. #ifdef CONFIG_AUTOSCAN
  4874. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  4875. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  4876. reply_len = -1;
  4877. #endif /* CONFIG_AUTOSCAN */
  4878. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  4879. pmksa_cache_clear_current(wpa_s->wpa);
  4880. eapol_sm_request_reauth(wpa_s->eapol);
  4881. #ifdef CONFIG_WNM
  4882. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  4883. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  4884. reply_len = -1;
  4885. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
  4886. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
  4887. reply_len = -1;
  4888. #endif /* CONFIG_WNM */
  4889. } else if (os_strcmp(buf, "FLUSH") == 0) {
  4890. wpa_supplicant_ctrl_iface_flush(wpa_s);
  4891. } else {
  4892. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  4893. reply_len = 16;
  4894. }
  4895. if (reply_len < 0) {
  4896. os_memcpy(reply, "FAIL\n", 5);
  4897. reply_len = 5;
  4898. }
  4899. if (ctrl_rsp)
  4900. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  4901. *resp_len = reply_len;
  4902. return reply;
  4903. }
  4904. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  4905. char *cmd)
  4906. {
  4907. struct wpa_interface iface;
  4908. char *pos;
  4909. /*
  4910. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  4911. * TAB<bridge_ifname>
  4912. */
  4913. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  4914. os_memset(&iface, 0, sizeof(iface));
  4915. do {
  4916. iface.ifname = pos = cmd;
  4917. pos = os_strchr(pos, '\t');
  4918. if (pos)
  4919. *pos++ = '\0';
  4920. if (iface.ifname[0] == '\0')
  4921. return -1;
  4922. if (pos == NULL)
  4923. break;
  4924. iface.confname = pos;
  4925. pos = os_strchr(pos, '\t');
  4926. if (pos)
  4927. *pos++ = '\0';
  4928. if (iface.confname[0] == '\0')
  4929. iface.confname = NULL;
  4930. if (pos == NULL)
  4931. break;
  4932. iface.driver = pos;
  4933. pos = os_strchr(pos, '\t');
  4934. if (pos)
  4935. *pos++ = '\0';
  4936. if (iface.driver[0] == '\0')
  4937. iface.driver = NULL;
  4938. if (pos == NULL)
  4939. break;
  4940. iface.ctrl_interface = pos;
  4941. pos = os_strchr(pos, '\t');
  4942. if (pos)
  4943. *pos++ = '\0';
  4944. if (iface.ctrl_interface[0] == '\0')
  4945. iface.ctrl_interface = NULL;
  4946. if (pos == NULL)
  4947. break;
  4948. iface.driver_param = pos;
  4949. pos = os_strchr(pos, '\t');
  4950. if (pos)
  4951. *pos++ = '\0';
  4952. if (iface.driver_param[0] == '\0')
  4953. iface.driver_param = NULL;
  4954. if (pos == NULL)
  4955. break;
  4956. iface.bridge_ifname = pos;
  4957. pos = os_strchr(pos, '\t');
  4958. if (pos)
  4959. *pos++ = '\0';
  4960. if (iface.bridge_ifname[0] == '\0')
  4961. iface.bridge_ifname = NULL;
  4962. if (pos == NULL)
  4963. break;
  4964. } while (0);
  4965. if (wpa_supplicant_get_iface(global, iface.ifname))
  4966. return -1;
  4967. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  4968. }
  4969. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  4970. char *cmd)
  4971. {
  4972. struct wpa_supplicant *wpa_s;
  4973. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  4974. wpa_s = wpa_supplicant_get_iface(global, cmd);
  4975. if (wpa_s == NULL)
  4976. return -1;
  4977. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  4978. }
  4979. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  4980. {
  4981. struct wpa_interface_info *prev;
  4982. while (iface) {
  4983. prev = iface;
  4984. iface = iface->next;
  4985. os_free(prev->ifname);
  4986. os_free(prev->desc);
  4987. os_free(prev);
  4988. }
  4989. }
  4990. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  4991. char *buf, int len)
  4992. {
  4993. int i, res;
  4994. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  4995. char *pos, *end;
  4996. for (i = 0; wpa_drivers[i]; i++) {
  4997. struct wpa_driver_ops *drv = wpa_drivers[i];
  4998. if (drv->get_interfaces == NULL)
  4999. continue;
  5000. tmp = drv->get_interfaces(global->drv_priv[i]);
  5001. if (tmp == NULL)
  5002. continue;
  5003. if (last == NULL)
  5004. iface = last = tmp;
  5005. else
  5006. last->next = tmp;
  5007. while (last->next)
  5008. last = last->next;
  5009. }
  5010. pos = buf;
  5011. end = buf + len;
  5012. for (tmp = iface; tmp; tmp = tmp->next) {
  5013. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  5014. tmp->drv_name, tmp->ifname,
  5015. tmp->desc ? tmp->desc : "");
  5016. if (res < 0 || res >= end - pos) {
  5017. *pos = '\0';
  5018. break;
  5019. }
  5020. pos += res;
  5021. }
  5022. wpa_free_iface_info(iface);
  5023. return pos - buf;
  5024. }
  5025. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  5026. char *buf, int len)
  5027. {
  5028. int res;
  5029. char *pos, *end;
  5030. struct wpa_supplicant *wpa_s;
  5031. wpa_s = global->ifaces;
  5032. pos = buf;
  5033. end = buf + len;
  5034. while (wpa_s) {
  5035. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  5036. if (res < 0 || res >= end - pos) {
  5037. *pos = '\0';
  5038. break;
  5039. }
  5040. pos += res;
  5041. wpa_s = wpa_s->next;
  5042. }
  5043. return pos - buf;
  5044. }
  5045. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  5046. const char *ifname,
  5047. char *cmd, size_t *resp_len)
  5048. {
  5049. struct wpa_supplicant *wpa_s;
  5050. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5051. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  5052. break;
  5053. }
  5054. if (wpa_s == NULL) {
  5055. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  5056. if (resp)
  5057. *resp_len = os_strlen(resp);
  5058. else
  5059. *resp_len = 1;
  5060. return resp;
  5061. }
  5062. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  5063. }
  5064. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  5065. char *buf, size_t *resp_len)
  5066. {
  5067. #ifdef CONFIG_P2P
  5068. static const char * cmd[] = {
  5069. "P2P_FIND",
  5070. "P2P_STOP_FIND",
  5071. "P2P_LISTEN",
  5072. "P2P_GROUP_ADD",
  5073. "P2P_GET_PASSPHRASE",
  5074. "P2P_SERVICE_UPDATE",
  5075. "P2P_SERVICE_FLUSH",
  5076. "P2P_FLUSH",
  5077. "P2P_CANCEL",
  5078. "P2P_PRESENCE_REQ",
  5079. "P2P_EXT_LISTEN",
  5080. NULL
  5081. };
  5082. static const char * prefix[] = {
  5083. "P2P_FIND ",
  5084. "P2P_CONNECT ",
  5085. "P2P_LISTEN ",
  5086. "P2P_GROUP_REMOVE ",
  5087. "P2P_GROUP_ADD ",
  5088. "P2P_PROV_DISC ",
  5089. "P2P_SERV_DISC_REQ ",
  5090. "P2P_SERV_DISC_CANCEL_REQ ",
  5091. "P2P_SERV_DISC_RESP ",
  5092. "P2P_SERV_DISC_EXTERNAL ",
  5093. "P2P_SERVICE_ADD ",
  5094. "P2P_SERVICE_DEL ",
  5095. "P2P_REJECT ",
  5096. "P2P_INVITE ",
  5097. "P2P_PEER ",
  5098. "P2P_SET ",
  5099. "P2P_UNAUTHORIZE ",
  5100. "P2P_PRESENCE_REQ ",
  5101. "P2P_EXT_LISTEN ",
  5102. "P2P_REMOVE_CLIENT ",
  5103. NULL
  5104. };
  5105. int found = 0;
  5106. int i;
  5107. if (global->p2p_init_wpa_s == NULL)
  5108. return NULL;
  5109. for (i = 0; !found && cmd[i]; i++) {
  5110. if (os_strcmp(buf, cmd[i]) == 0)
  5111. found = 1;
  5112. }
  5113. for (i = 0; !found && prefix[i]; i++) {
  5114. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  5115. found = 1;
  5116. }
  5117. if (found)
  5118. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  5119. buf, resp_len);
  5120. #endif /* CONFIG_P2P */
  5121. return NULL;
  5122. }
  5123. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  5124. char *buf, size_t *resp_len)
  5125. {
  5126. #ifdef CONFIG_WIFI_DISPLAY
  5127. if (global->p2p_init_wpa_s == NULL)
  5128. return NULL;
  5129. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  5130. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  5131. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  5132. buf, resp_len);
  5133. #endif /* CONFIG_WIFI_DISPLAY */
  5134. return NULL;
  5135. }
  5136. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  5137. char *buf, size_t *resp_len)
  5138. {
  5139. char *ret;
  5140. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  5141. if (ret)
  5142. return ret;
  5143. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  5144. if (ret)
  5145. return ret;
  5146. return NULL;
  5147. }
  5148. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  5149. {
  5150. char *value;
  5151. value = os_strchr(cmd, ' ');
  5152. if (value == NULL)
  5153. return -1;
  5154. *value++ = '\0';
  5155. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  5156. #ifdef CONFIG_WIFI_DISPLAY
  5157. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  5158. wifi_display_enable(global, !!atoi(value));
  5159. return 0;
  5160. }
  5161. #endif /* CONFIG_WIFI_DISPLAY */
  5162. return -1;
  5163. }
  5164. #ifndef CONFIG_NO_CONFIG_WRITE
  5165. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  5166. {
  5167. int ret = 0;
  5168. struct wpa_supplicant *wpa_s;
  5169. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5170. if (!wpa_s->conf->update_config) {
  5171. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  5172. continue;
  5173. }
  5174. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  5175. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  5176. ret = 1;
  5177. } else {
  5178. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  5179. }
  5180. }
  5181. return ret;
  5182. }
  5183. #endif /* CONFIG_NO_CONFIG_WRITE */
  5184. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  5185. char *buf, size_t buflen)
  5186. {
  5187. char *pos, *end;
  5188. int ret;
  5189. struct wpa_supplicant *wpa_s;
  5190. pos = buf;
  5191. end = buf + buflen;
  5192. #ifdef CONFIG_P2P
  5193. if (global->p2p && !global->p2p_disabled) {
  5194. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  5195. "\n"
  5196. "p2p_state=%s\n",
  5197. MAC2STR(global->p2p_dev_addr),
  5198. p2p_get_state_txt(global->p2p));
  5199. if (ret < 0 || ret >= end - pos)
  5200. return pos - buf;
  5201. pos += ret;
  5202. } else if (global->p2p) {
  5203. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  5204. if (ret < 0 || ret >= end - pos)
  5205. return pos - buf;
  5206. pos += ret;
  5207. }
  5208. #endif /* CONFIG_P2P */
  5209. #ifdef CONFIG_WIFI_DISPLAY
  5210. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  5211. !!global->wifi_display);
  5212. if (ret < 0 || ret >= end - pos)
  5213. return pos - buf;
  5214. pos += ret;
  5215. #endif /* CONFIG_WIFI_DISPLAY */
  5216. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5217. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  5218. "address=" MACSTR "\n",
  5219. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  5220. if (ret < 0 || ret >= end - pos)
  5221. return pos - buf;
  5222. pos += ret;
  5223. }
  5224. return pos - buf;
  5225. }
  5226. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  5227. char *buf, size_t *resp_len)
  5228. {
  5229. char *reply;
  5230. const int reply_size = 2048;
  5231. int reply_len;
  5232. int level = MSG_DEBUG;
  5233. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  5234. char *pos = os_strchr(buf + 7, ' ');
  5235. if (pos) {
  5236. *pos++ = '\0';
  5237. return wpas_global_ctrl_iface_ifname(global,
  5238. buf + 7, pos,
  5239. resp_len);
  5240. }
  5241. }
  5242. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  5243. if (reply)
  5244. return reply;
  5245. if (os_strcmp(buf, "PING") == 0)
  5246. level = MSG_EXCESSIVE;
  5247. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  5248. (const u8 *) buf, os_strlen(buf));
  5249. reply = os_malloc(reply_size);
  5250. if (reply == NULL) {
  5251. *resp_len = 1;
  5252. return NULL;
  5253. }
  5254. os_memcpy(reply, "OK\n", 3);
  5255. reply_len = 3;
  5256. if (os_strcmp(buf, "PING") == 0) {
  5257. os_memcpy(reply, "PONG\n", 5);
  5258. reply_len = 5;
  5259. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  5260. if (wpa_supplicant_global_iface_add(global, buf + 14))
  5261. reply_len = -1;
  5262. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  5263. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  5264. reply_len = -1;
  5265. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  5266. reply_len = wpa_supplicant_global_iface_list(
  5267. global, reply, reply_size);
  5268. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  5269. reply_len = wpa_supplicant_global_iface_interfaces(
  5270. global, reply, reply_size);
  5271. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  5272. wpa_supplicant_terminate_proc(global);
  5273. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  5274. wpas_notify_suspend(global);
  5275. } else if (os_strcmp(buf, "RESUME") == 0) {
  5276. wpas_notify_resume(global);
  5277. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  5278. if (wpas_global_ctrl_iface_set(global, buf + 4))
  5279. reply_len = -1;
  5280. #ifndef CONFIG_NO_CONFIG_WRITE
  5281. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  5282. if (wpas_global_ctrl_iface_save_config(global))
  5283. reply_len = -1;
  5284. #endif /* CONFIG_NO_CONFIG_WRITE */
  5285. } else if (os_strcmp(buf, "STATUS") == 0) {
  5286. reply_len = wpas_global_ctrl_iface_status(global, reply,
  5287. reply_size);
  5288. } else {
  5289. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  5290. reply_len = 16;
  5291. }
  5292. if (reply_len < 0) {
  5293. os_memcpy(reply, "FAIL\n", 5);
  5294. reply_len = 5;
  5295. }
  5296. *resp_len = reply_len;
  5297. return reply;
  5298. }