ctrl_iface.c 114 KB

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