ctrl_iface.c 145 KB

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