ctrl_iface.c 135 KB

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