ctrl_iface.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "common/wpa_ctrl.h"
  19. #include "eap_peer/eap.h"
  20. #include "eapol_supp/eapol_supp_sm.h"
  21. #include "rsn_supp/wpa.h"
  22. #include "rsn_supp/preauth.h"
  23. #include "rsn_supp/pmksa_cache.h"
  24. #include "l2_packet/l2_packet.h"
  25. #include "wps/wps.h"
  26. #include "config.h"
  27. #include "wpa_supplicant_i.h"
  28. #include "driver_i.h"
  29. #include "wps_supplicant.h"
  30. #include "ibss_rsn.h"
  31. #include "ap.h"
  32. #include "p2p_supplicant.h"
  33. #include "p2p/p2p.h"
  34. #include "notify.h"
  35. #include "bss.h"
  36. #include "scan.h"
  37. #include "ctrl_iface.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 wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  44. char *cmd)
  45. {
  46. char *value;
  47. int ret = 0;
  48. value = os_strchr(cmd, ' ');
  49. if (value == NULL)
  50. return -1;
  51. *value++ = '\0';
  52. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  53. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  54. eapol_sm_configure(wpa_s->eapol,
  55. atoi(value), -1, -1, -1);
  56. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  57. eapol_sm_configure(wpa_s->eapol,
  58. -1, atoi(value), -1, -1);
  59. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  60. eapol_sm_configure(wpa_s->eapol,
  61. -1, -1, atoi(value), -1);
  62. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  63. eapol_sm_configure(wpa_s->eapol,
  64. -1, -1, -1, atoi(value));
  65. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  66. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  67. atoi(value)))
  68. ret = -1;
  69. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  70. 0) {
  71. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  72. atoi(value)))
  73. ret = -1;
  74. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  75. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  76. ret = -1;
  77. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  78. wpa_s->wps_fragment_size = atoi(value);
  79. } else {
  80. value[-1] = '=';
  81. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  82. if (ret == 0)
  83. wpa_supplicant_update_config(wpa_s);
  84. }
  85. return ret;
  86. }
  87. #ifdef IEEE8021X_EAPOL
  88. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  89. char *addr)
  90. {
  91. u8 bssid[ETH_ALEN];
  92. struct wpa_ssid *ssid = wpa_s->current_ssid;
  93. if (hwaddr_aton(addr, bssid)) {
  94. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  95. "'%s'", addr);
  96. return -1;
  97. }
  98. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  99. rsn_preauth_deinit(wpa_s->wpa);
  100. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  101. return -1;
  102. return 0;
  103. }
  104. #endif /* IEEE8021X_EAPOL */
  105. #ifdef CONFIG_PEERKEY
  106. /* MLME-STKSTART.request(peer) */
  107. static int wpa_supplicant_ctrl_iface_stkstart(
  108. struct wpa_supplicant *wpa_s, char *addr)
  109. {
  110. u8 peer[ETH_ALEN];
  111. if (hwaddr_aton(addr, peer)) {
  112. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  113. "address '%s'", addr);
  114. return -1;
  115. }
  116. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  117. MAC2STR(peer));
  118. return wpa_sm_stkstart(wpa_s->wpa, peer);
  119. }
  120. #endif /* CONFIG_PEERKEY */
  121. #ifdef CONFIG_IEEE80211R
  122. static int wpa_supplicant_ctrl_iface_ft_ds(
  123. struct wpa_supplicant *wpa_s, char *addr)
  124. {
  125. u8 target_ap[ETH_ALEN];
  126. struct wpa_bss *bss;
  127. const u8 *mdie;
  128. if (hwaddr_aton(addr, target_ap)) {
  129. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  130. "address '%s'", addr);
  131. return -1;
  132. }
  133. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  134. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  135. if (bss)
  136. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  137. else
  138. mdie = NULL;
  139. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  140. }
  141. #endif /* CONFIG_IEEE80211R */
  142. #ifdef CONFIG_WPS
  143. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  144. char *cmd)
  145. {
  146. u8 bssid[ETH_ALEN], *_bssid = bssid;
  147. if (cmd == NULL || os_strcmp(cmd, "any") == 0)
  148. _bssid = NULL;
  149. else if (hwaddr_aton(cmd, bssid)) {
  150. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  151. cmd);
  152. return -1;
  153. }
  154. #ifdef CONFIG_AP
  155. if (wpa_s->ap_iface)
  156. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid);
  157. #endif /* CONFIG_AP */
  158. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  159. }
  160. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  161. char *cmd, char *buf,
  162. size_t buflen)
  163. {
  164. u8 bssid[ETH_ALEN], *_bssid = bssid;
  165. char *pin;
  166. int ret;
  167. pin = os_strchr(cmd, ' ');
  168. if (pin)
  169. *pin++ = '\0';
  170. if (os_strcmp(cmd, "any") == 0)
  171. _bssid = NULL;
  172. else if (hwaddr_aton(cmd, bssid)) {
  173. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  174. cmd);
  175. return -1;
  176. }
  177. #ifdef CONFIG_AP
  178. if (wpa_s->ap_iface)
  179. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  180. buf, buflen);
  181. #endif /* CONFIG_AP */
  182. if (pin) {
  183. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  184. DEV_PW_DEFAULT);
  185. if (ret < 0)
  186. return -1;
  187. ret = os_snprintf(buf, buflen, "%s", pin);
  188. if (ret < 0 || (size_t) ret >= buflen)
  189. return -1;
  190. return ret;
  191. }
  192. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  193. if (ret < 0)
  194. return -1;
  195. /* Return the generated PIN */
  196. ret = os_snprintf(buf, buflen, "%08d", ret);
  197. if (ret < 0 || (size_t) ret >= buflen)
  198. return -1;
  199. return ret;
  200. }
  201. #ifdef CONFIG_WPS_OOB
  202. static int wpa_supplicant_ctrl_iface_wps_oob(struct wpa_supplicant *wpa_s,
  203. char *cmd)
  204. {
  205. char *path, *method, *name;
  206. path = os_strchr(cmd, ' ');
  207. if (path == NULL)
  208. return -1;
  209. *path++ = '\0';
  210. method = os_strchr(path, ' ');
  211. if (method == NULL)
  212. return -1;
  213. *method++ = '\0';
  214. name = os_strchr(method, ' ');
  215. if (name != NULL)
  216. *name++ = '\0';
  217. return wpas_wps_start_oob(wpa_s, cmd, path, method, name);
  218. }
  219. #endif /* CONFIG_WPS_OOB */
  220. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  221. char *cmd)
  222. {
  223. u8 bssid[ETH_ALEN], *_bssid = bssid;
  224. char *pin;
  225. char *new_ssid;
  226. char *new_auth;
  227. char *new_encr;
  228. char *new_key;
  229. struct wps_new_ap_settings ap;
  230. pin = os_strchr(cmd, ' ');
  231. if (pin == NULL)
  232. return -1;
  233. *pin++ = '\0';
  234. if (os_strcmp(cmd, "any") == 0)
  235. _bssid = NULL;
  236. else if (hwaddr_aton(cmd, bssid)) {
  237. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  238. cmd);
  239. return -1;
  240. }
  241. new_ssid = os_strchr(pin, ' ');
  242. if (new_ssid == NULL)
  243. return wpas_wps_start_reg(wpa_s, _bssid, pin, NULL);
  244. *new_ssid++ = '\0';
  245. new_auth = os_strchr(new_ssid, ' ');
  246. if (new_auth == NULL)
  247. return -1;
  248. *new_auth++ = '\0';
  249. new_encr = os_strchr(new_auth, ' ');
  250. if (new_encr == NULL)
  251. return -1;
  252. *new_encr++ = '\0';
  253. new_key = os_strchr(new_encr, ' ');
  254. if (new_key == NULL)
  255. return -1;
  256. *new_key++ = '\0';
  257. os_memset(&ap, 0, sizeof(ap));
  258. ap.ssid_hex = new_ssid;
  259. ap.auth = new_auth;
  260. ap.encr = new_encr;
  261. ap.key_hex = new_key;
  262. return wpas_wps_start_reg(wpa_s, _bssid, pin, &ap);
  263. }
  264. #ifdef CONFIG_WPS_ER
  265. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  266. char *cmd)
  267. {
  268. char *uuid = cmd, *pin, *pos;
  269. u8 addr_buf[ETH_ALEN], *addr = NULL;
  270. pin = os_strchr(uuid, ' ');
  271. if (pin == NULL)
  272. return -1;
  273. *pin++ = '\0';
  274. pos = os_strchr(pin, ' ');
  275. if (pos) {
  276. *pos++ = '\0';
  277. if (hwaddr_aton(pos, addr_buf) == 0)
  278. addr = addr_buf;
  279. }
  280. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  281. }
  282. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  283. char *cmd)
  284. {
  285. char *uuid = cmd, *pin;
  286. pin = os_strchr(uuid, ' ');
  287. if (pin == NULL)
  288. return -1;
  289. *pin++ = '\0';
  290. return wpas_wps_er_learn(wpa_s, uuid, pin);
  291. }
  292. static int wpa_supplicant_ctrl_iface_wps_er_config(
  293. struct wpa_supplicant *wpa_s, char *cmd)
  294. {
  295. char *pin;
  296. char *new_ssid;
  297. char *new_auth;
  298. char *new_encr;
  299. char *new_key;
  300. struct wps_new_ap_settings ap;
  301. pin = os_strchr(cmd, ' ');
  302. if (pin == NULL)
  303. return -1;
  304. *pin++ = '\0';
  305. new_ssid = os_strchr(pin, ' ');
  306. if (new_ssid == NULL)
  307. return -1;
  308. *new_ssid++ = '\0';
  309. new_auth = os_strchr(new_ssid, ' ');
  310. if (new_auth == NULL)
  311. return -1;
  312. *new_auth++ = '\0';
  313. new_encr = os_strchr(new_auth, ' ');
  314. if (new_encr == NULL)
  315. return -1;
  316. *new_encr++ = '\0';
  317. new_key = os_strchr(new_encr, ' ');
  318. if (new_key == NULL)
  319. return -1;
  320. *new_key++ = '\0';
  321. os_memset(&ap, 0, sizeof(ap));
  322. ap.ssid_hex = new_ssid;
  323. ap.auth = new_auth;
  324. ap.encr = new_encr;
  325. ap.key_hex = new_key;
  326. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  327. }
  328. #endif /* CONFIG_WPS_ER */
  329. #endif /* CONFIG_WPS */
  330. #ifdef CONFIG_IBSS_RSN
  331. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  332. struct wpa_supplicant *wpa_s, char *addr)
  333. {
  334. u8 peer[ETH_ALEN];
  335. if (hwaddr_aton(addr, peer)) {
  336. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  337. "address '%s'", addr);
  338. return -1;
  339. }
  340. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  341. MAC2STR(peer));
  342. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  343. }
  344. #endif /* CONFIG_IBSS_RSN */
  345. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  346. char *rsp)
  347. {
  348. #ifdef IEEE8021X_EAPOL
  349. char *pos, *id_pos;
  350. int id;
  351. struct wpa_ssid *ssid;
  352. struct eap_peer_config *eap;
  353. pos = os_strchr(rsp, '-');
  354. if (pos == NULL)
  355. return -1;
  356. *pos++ = '\0';
  357. id_pos = pos;
  358. pos = os_strchr(pos, ':');
  359. if (pos == NULL)
  360. return -1;
  361. *pos++ = '\0';
  362. id = atoi(id_pos);
  363. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  364. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  365. (u8 *) pos, os_strlen(pos));
  366. ssid = wpa_config_get_network(wpa_s->conf, id);
  367. if (ssid == NULL) {
  368. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  369. "to update", id);
  370. return -1;
  371. }
  372. eap = &ssid->eap;
  373. if (os_strcmp(rsp, "IDENTITY") == 0) {
  374. os_free(eap->identity);
  375. eap->identity = (u8 *) os_strdup(pos);
  376. eap->identity_len = os_strlen(pos);
  377. eap->pending_req_identity = 0;
  378. if (ssid == wpa_s->current_ssid)
  379. wpa_s->reassociate = 1;
  380. } else if (os_strcmp(rsp, "PASSWORD") == 0) {
  381. os_free(eap->password);
  382. eap->password = (u8 *) os_strdup(pos);
  383. eap->password_len = os_strlen(pos);
  384. eap->pending_req_password = 0;
  385. if (ssid == wpa_s->current_ssid)
  386. wpa_s->reassociate = 1;
  387. } else if (os_strcmp(rsp, "NEW_PASSWORD") == 0) {
  388. os_free(eap->new_password);
  389. eap->new_password = (u8 *) os_strdup(pos);
  390. eap->new_password_len = os_strlen(pos);
  391. eap->pending_req_new_password = 0;
  392. if (ssid == wpa_s->current_ssid)
  393. wpa_s->reassociate = 1;
  394. } else if (os_strcmp(rsp, "PIN") == 0) {
  395. os_free(eap->pin);
  396. eap->pin = os_strdup(pos);
  397. eap->pending_req_pin = 0;
  398. if (ssid == wpa_s->current_ssid)
  399. wpa_s->reassociate = 1;
  400. } else if (os_strcmp(rsp, "OTP") == 0) {
  401. os_free(eap->otp);
  402. eap->otp = (u8 *) os_strdup(pos);
  403. eap->otp_len = os_strlen(pos);
  404. os_free(eap->pending_req_otp);
  405. eap->pending_req_otp = NULL;
  406. eap->pending_req_otp_len = 0;
  407. } else if (os_strcmp(rsp, "PASSPHRASE") == 0) {
  408. os_free(eap->private_key_passwd);
  409. eap->private_key_passwd = (u8 *) os_strdup(pos);
  410. eap->pending_req_passphrase = 0;
  411. if (ssid == wpa_s->current_ssid)
  412. wpa_s->reassociate = 1;
  413. } else {
  414. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", rsp);
  415. return -1;
  416. }
  417. return 0;
  418. #else /* IEEE8021X_EAPOL */
  419. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  420. return -1;
  421. #endif /* IEEE8021X_EAPOL */
  422. }
  423. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  424. const char *params,
  425. char *buf, size_t buflen)
  426. {
  427. char *pos, *end, tmp[30];
  428. int res, verbose, ret;
  429. verbose = os_strcmp(params, "-VERBOSE") == 0;
  430. pos = buf;
  431. end = buf + buflen;
  432. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  433. struct wpa_ssid *ssid = wpa_s->current_ssid;
  434. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  435. MAC2STR(wpa_s->bssid));
  436. if (ret < 0 || ret >= end - pos)
  437. return pos - buf;
  438. pos += ret;
  439. if (ssid) {
  440. u8 *_ssid = ssid->ssid;
  441. size_t ssid_len = ssid->ssid_len;
  442. u8 ssid_buf[MAX_SSID_LEN];
  443. if (ssid_len == 0) {
  444. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  445. if (_res < 0)
  446. ssid_len = 0;
  447. else
  448. ssid_len = _res;
  449. _ssid = ssid_buf;
  450. }
  451. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  452. wpa_ssid_txt(_ssid, ssid_len),
  453. ssid->id);
  454. if (ret < 0 || ret >= end - pos)
  455. return pos - buf;
  456. pos += ret;
  457. if (ssid->id_str) {
  458. ret = os_snprintf(pos, end - pos,
  459. "id_str=%s\n",
  460. ssid->id_str);
  461. if (ret < 0 || ret >= end - pos)
  462. return pos - buf;
  463. pos += ret;
  464. }
  465. switch (ssid->mode) {
  466. case WPAS_MODE_INFRA:
  467. ret = os_snprintf(pos, end - pos,
  468. "mode=station\n");
  469. break;
  470. case WPAS_MODE_IBSS:
  471. ret = os_snprintf(pos, end - pos,
  472. "mode=IBSS\n");
  473. break;
  474. case WPAS_MODE_AP:
  475. ret = os_snprintf(pos, end - pos,
  476. "mode=AP\n");
  477. break;
  478. case WPAS_MODE_P2P_GO:
  479. ret = os_snprintf(pos, end - pos,
  480. "mode=P2P GO\n");
  481. break;
  482. case WPAS_MODE_P2P_GROUP_FORMATION:
  483. ret = os_snprintf(pos, end - pos,
  484. "mode=P2P GO - group "
  485. "formation\n");
  486. break;
  487. default:
  488. ret = 0;
  489. break;
  490. }
  491. if (ret < 0 || ret >= end - pos)
  492. return pos - buf;
  493. pos += ret;
  494. }
  495. #ifdef CONFIG_AP
  496. if (wpa_s->ap_iface) {
  497. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  498. end - pos,
  499. verbose);
  500. } else
  501. #endif /* CONFIG_AP */
  502. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  503. }
  504. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  505. wpa_supplicant_state_txt(wpa_s->wpa_state));
  506. if (ret < 0 || ret >= end - pos)
  507. return pos - buf;
  508. pos += ret;
  509. if (wpa_s->l2 &&
  510. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  511. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  512. if (ret < 0 || ret >= end - pos)
  513. return pos - buf;
  514. pos += ret;
  515. }
  516. #ifdef CONFIG_P2P
  517. if (wpa_s->global->p2p) {
  518. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  519. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  520. if (ret < 0 || ret >= end - pos)
  521. return pos - buf;
  522. pos += ret;
  523. }
  524. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  525. MAC2STR(wpa_s->own_addr));
  526. if (ret < 0 || ret >= end - pos)
  527. return pos - buf;
  528. pos += ret;
  529. #endif /* CONFIG_P2P */
  530. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  531. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  532. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  533. verbose);
  534. if (res >= 0)
  535. pos += res;
  536. }
  537. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  538. if (res >= 0)
  539. pos += res;
  540. return pos - buf;
  541. }
  542. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  543. char *cmd)
  544. {
  545. char *pos;
  546. int id;
  547. struct wpa_ssid *ssid;
  548. u8 bssid[ETH_ALEN];
  549. /* cmd: "<network id> <BSSID>" */
  550. pos = os_strchr(cmd, ' ');
  551. if (pos == NULL)
  552. return -1;
  553. *pos++ = '\0';
  554. id = atoi(cmd);
  555. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  556. if (hwaddr_aton(pos, bssid)) {
  557. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  558. return -1;
  559. }
  560. ssid = wpa_config_get_network(wpa_s->conf, id);
  561. if (ssid == NULL) {
  562. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  563. "to update", id);
  564. return -1;
  565. }
  566. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  567. ssid->bssid_set = !is_zero_ether_addr(bssid);
  568. return 0;
  569. }
  570. static int wpa_supplicant_ctrl_iface_list_networks(
  571. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  572. {
  573. char *pos, *end;
  574. struct wpa_ssid *ssid;
  575. int ret;
  576. pos = buf;
  577. end = buf + buflen;
  578. ret = os_snprintf(pos, end - pos,
  579. "network id / ssid / bssid / flags\n");
  580. if (ret < 0 || ret >= end - pos)
  581. return pos - buf;
  582. pos += ret;
  583. ssid = wpa_s->conf->ssid;
  584. while (ssid) {
  585. ret = os_snprintf(pos, end - pos, "%d\t%s",
  586. ssid->id,
  587. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  588. if (ret < 0 || ret >= end - pos)
  589. return pos - buf;
  590. pos += ret;
  591. if (ssid->bssid_set) {
  592. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  593. MAC2STR(ssid->bssid));
  594. } else {
  595. ret = os_snprintf(pos, end - pos, "\tany");
  596. }
  597. if (ret < 0 || ret >= end - pos)
  598. return pos - buf;
  599. pos += ret;
  600. ret = os_snprintf(pos, end - pos, "\t%s%s%s",
  601. ssid == wpa_s->current_ssid ?
  602. "[CURRENT]" : "",
  603. ssid->disabled ? "[DISABLED]" : "",
  604. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  605. "");
  606. if (ret < 0 || ret >= end - pos)
  607. return pos - buf;
  608. pos += ret;
  609. ret = os_snprintf(pos, end - pos, "\n");
  610. if (ret < 0 || ret >= end - pos)
  611. return pos - buf;
  612. pos += ret;
  613. ssid = ssid->next;
  614. }
  615. return pos - buf;
  616. }
  617. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  618. {
  619. int first = 1, ret;
  620. ret = os_snprintf(pos, end - pos, "-");
  621. if (ret < 0 || ret >= end - pos)
  622. return pos;
  623. pos += ret;
  624. if (cipher & WPA_CIPHER_NONE) {
  625. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
  626. if (ret < 0 || ret >= end - pos)
  627. return pos;
  628. pos += ret;
  629. first = 0;
  630. }
  631. if (cipher & WPA_CIPHER_WEP40) {
  632. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
  633. if (ret < 0 || ret >= end - pos)
  634. return pos;
  635. pos += ret;
  636. first = 0;
  637. }
  638. if (cipher & WPA_CIPHER_WEP104) {
  639. ret = os_snprintf(pos, end - pos, "%sWEP104",
  640. first ? "" : "+");
  641. if (ret < 0 || ret >= end - pos)
  642. return pos;
  643. pos += ret;
  644. first = 0;
  645. }
  646. if (cipher & WPA_CIPHER_TKIP) {
  647. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
  648. if (ret < 0 || ret >= end - pos)
  649. return pos;
  650. pos += ret;
  651. first = 0;
  652. }
  653. if (cipher & WPA_CIPHER_CCMP) {
  654. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
  655. if (ret < 0 || ret >= end - pos)
  656. return pos;
  657. pos += ret;
  658. first = 0;
  659. }
  660. return pos;
  661. }
  662. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  663. const u8 *ie, size_t ie_len)
  664. {
  665. struct wpa_ie_data data;
  666. int first, ret;
  667. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  668. if (ret < 0 || ret >= end - pos)
  669. return pos;
  670. pos += ret;
  671. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  672. ret = os_snprintf(pos, end - pos, "?]");
  673. if (ret < 0 || ret >= end - pos)
  674. return pos;
  675. pos += ret;
  676. return pos;
  677. }
  678. first = 1;
  679. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  680. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  681. if (ret < 0 || ret >= end - pos)
  682. return pos;
  683. pos += ret;
  684. first = 0;
  685. }
  686. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  687. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  688. if (ret < 0 || ret >= end - pos)
  689. return pos;
  690. pos += ret;
  691. first = 0;
  692. }
  693. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  694. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  695. if (ret < 0 || ret >= end - pos)
  696. return pos;
  697. pos += ret;
  698. first = 0;
  699. }
  700. #ifdef CONFIG_IEEE80211R
  701. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  702. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  703. first ? "" : "+");
  704. if (ret < 0 || ret >= end - pos)
  705. return pos;
  706. pos += ret;
  707. first = 0;
  708. }
  709. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  710. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  711. first ? "" : "+");
  712. if (ret < 0 || ret >= end - pos)
  713. return pos;
  714. pos += ret;
  715. first = 0;
  716. }
  717. #endif /* CONFIG_IEEE80211R */
  718. #ifdef CONFIG_IEEE80211W
  719. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  720. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  721. first ? "" : "+");
  722. if (ret < 0 || ret >= end - pos)
  723. return pos;
  724. pos += ret;
  725. first = 0;
  726. }
  727. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  728. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  729. first ? "" : "+");
  730. if (ret < 0 || ret >= end - pos)
  731. return pos;
  732. pos += ret;
  733. first = 0;
  734. }
  735. #endif /* CONFIG_IEEE80211W */
  736. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  737. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  738. ret = os_snprintf(pos, end - pos, "-preauth");
  739. if (ret < 0 || ret >= end - pos)
  740. return pos;
  741. pos += ret;
  742. }
  743. ret = os_snprintf(pos, end - pos, "]");
  744. if (ret < 0 || ret >= end - pos)
  745. return pos;
  746. pos += ret;
  747. return pos;
  748. }
  749. #ifdef CONFIG_WPS
  750. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  751. char *pos, char *end,
  752. struct wpabuf *wps_ie)
  753. {
  754. int ret;
  755. const char *txt;
  756. if (wps_ie == NULL)
  757. return pos;
  758. if (wps_is_selected_pbc_registrar(wps_ie))
  759. txt = "[WPS-PBC]";
  760. #ifdef CONFIG_WPS2
  761. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  762. txt = "[WPS-AUTH]";
  763. #endif /* CONFIG_WPS2 */
  764. else if (wps_is_selected_pin_registrar(wps_ie))
  765. txt = "[WPS-PIN]";
  766. else
  767. txt = "[WPS]";
  768. ret = os_snprintf(pos, end - pos, "%s", txt);
  769. if (ret >= 0 && ret < end - pos)
  770. pos += ret;
  771. wpabuf_free(wps_ie);
  772. return pos;
  773. }
  774. #endif /* CONFIG_WPS */
  775. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  776. char *pos, char *end,
  777. const struct wpa_bss *bss)
  778. {
  779. #ifdef CONFIG_WPS
  780. struct wpabuf *wps_ie;
  781. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  782. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  783. #else /* CONFIG_WPS */
  784. return pos;
  785. #endif /* CONFIG_WPS */
  786. }
  787. /* Format one result on one text line into a buffer. */
  788. static int wpa_supplicant_ctrl_iface_scan_result(
  789. struct wpa_supplicant *wpa_s,
  790. const struct wpa_bss *bss, char *buf, size_t buflen)
  791. {
  792. char *pos, *end;
  793. int ret;
  794. const u8 *ie, *ie2, *p2p;
  795. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  796. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  797. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  798. 0)
  799. return 0; /* Do not show P2P listen discovery results here */
  800. pos = buf;
  801. end = buf + buflen;
  802. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  803. MAC2STR(bss->bssid), bss->freq, bss->level);
  804. if (ret < 0 || ret >= end - pos)
  805. return pos - buf;
  806. pos += ret;
  807. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  808. if (ie)
  809. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  810. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  811. if (ie2)
  812. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  813. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  814. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  815. ret = os_snprintf(pos, end - pos, "[WEP]");
  816. if (ret < 0 || ret >= end - pos)
  817. return pos - buf;
  818. pos += ret;
  819. }
  820. if (bss->caps & IEEE80211_CAP_IBSS) {
  821. ret = os_snprintf(pos, end - pos, "[IBSS]");
  822. if (ret < 0 || ret >= end - pos)
  823. return pos - buf;
  824. pos += ret;
  825. }
  826. if (bss->caps & IEEE80211_CAP_ESS) {
  827. ret = os_snprintf(pos, end - pos, "[ESS]");
  828. if (ret < 0 || ret >= end - pos)
  829. return pos - buf;
  830. pos += ret;
  831. }
  832. if (p2p) {
  833. ret = os_snprintf(pos, end - pos, "[P2P]");
  834. if (ret < 0 || ret >= end - pos)
  835. return pos - buf;
  836. pos += ret;
  837. }
  838. ret = os_snprintf(pos, end - pos, "\t%s",
  839. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  840. if (ret < 0 || ret >= end - pos)
  841. return pos - buf;
  842. pos += ret;
  843. ret = os_snprintf(pos, end - pos, "\n");
  844. if (ret < 0 || ret >= end - pos)
  845. return pos - buf;
  846. pos += ret;
  847. return pos - buf;
  848. }
  849. static int wpa_supplicant_ctrl_iface_scan_results(
  850. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  851. {
  852. char *pos, *end;
  853. struct wpa_bss *bss;
  854. int ret;
  855. pos = buf;
  856. end = buf + buflen;
  857. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  858. "flags / ssid\n");
  859. if (ret < 0 || ret >= end - pos)
  860. return pos - buf;
  861. pos += ret;
  862. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  863. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  864. end - pos);
  865. if (ret < 0 || ret >= end - pos)
  866. return pos - buf;
  867. pos += ret;
  868. }
  869. return pos - buf;
  870. }
  871. static int wpa_supplicant_ctrl_iface_select_network(
  872. struct wpa_supplicant *wpa_s, char *cmd)
  873. {
  874. int id;
  875. struct wpa_ssid *ssid;
  876. /* cmd: "<network id>" or "any" */
  877. if (os_strcmp(cmd, "any") == 0) {
  878. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  879. ssid = NULL;
  880. } else {
  881. id = atoi(cmd);
  882. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  883. ssid = wpa_config_get_network(wpa_s->conf, id);
  884. if (ssid == NULL) {
  885. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  886. "network id=%d", id);
  887. return -1;
  888. }
  889. if (ssid->disabled == 2) {
  890. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  891. "SELECT_NETWORK with persistent P2P group");
  892. return -1;
  893. }
  894. }
  895. wpa_supplicant_select_network(wpa_s, ssid);
  896. return 0;
  897. }
  898. static int wpa_supplicant_ctrl_iface_enable_network(
  899. struct wpa_supplicant *wpa_s, char *cmd)
  900. {
  901. int id;
  902. struct wpa_ssid *ssid;
  903. /* cmd: "<network id>" or "all" */
  904. if (os_strcmp(cmd, "all") == 0) {
  905. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  906. ssid = NULL;
  907. } else {
  908. id = atoi(cmd);
  909. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  910. ssid = wpa_config_get_network(wpa_s->conf, id);
  911. if (ssid == NULL) {
  912. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  913. "network id=%d", id);
  914. return -1;
  915. }
  916. if (ssid->disabled == 2) {
  917. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  918. "ENABLE_NETWORK with persistent P2P group");
  919. return -1;
  920. }
  921. }
  922. wpa_supplicant_enable_network(wpa_s, ssid);
  923. return 0;
  924. }
  925. static int wpa_supplicant_ctrl_iface_disable_network(
  926. struct wpa_supplicant *wpa_s, char *cmd)
  927. {
  928. int id;
  929. struct wpa_ssid *ssid;
  930. /* cmd: "<network id>" or "all" */
  931. if (os_strcmp(cmd, "all") == 0) {
  932. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  933. ssid = NULL;
  934. } else {
  935. id = atoi(cmd);
  936. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  937. ssid = wpa_config_get_network(wpa_s->conf, id);
  938. if (ssid == NULL) {
  939. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  940. "network id=%d", id);
  941. return -1;
  942. }
  943. if (ssid->disabled == 2) {
  944. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  945. "DISABLE_NETWORK with persistent P2P "
  946. "group");
  947. return -1;
  948. }
  949. }
  950. wpa_supplicant_disable_network(wpa_s, ssid);
  951. return 0;
  952. }
  953. static int wpa_supplicant_ctrl_iface_add_network(
  954. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  955. {
  956. struct wpa_ssid *ssid;
  957. int ret;
  958. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  959. ssid = wpa_config_add_network(wpa_s->conf);
  960. if (ssid == NULL)
  961. return -1;
  962. wpas_notify_network_added(wpa_s, ssid);
  963. ssid->disabled = 1;
  964. wpa_config_set_network_defaults(ssid);
  965. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  966. if (ret < 0 || (size_t) ret >= buflen)
  967. return -1;
  968. return ret;
  969. }
  970. static int wpa_supplicant_ctrl_iface_remove_network(
  971. struct wpa_supplicant *wpa_s, char *cmd)
  972. {
  973. int id;
  974. struct wpa_ssid *ssid;
  975. /* cmd: "<network id>" or "all" */
  976. if (os_strcmp(cmd, "all") == 0) {
  977. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  978. ssid = wpa_s->conf->ssid;
  979. while (ssid) {
  980. struct wpa_ssid *remove_ssid = ssid;
  981. id = ssid->id;
  982. ssid = ssid->next;
  983. wpas_notify_network_removed(wpa_s, remove_ssid);
  984. wpa_config_remove_network(wpa_s->conf, id);
  985. }
  986. if (wpa_s->current_ssid) {
  987. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  988. wpa_supplicant_disassociate(wpa_s,
  989. WLAN_REASON_DEAUTH_LEAVING);
  990. }
  991. return 0;
  992. }
  993. id = atoi(cmd);
  994. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  995. ssid = wpa_config_get_network(wpa_s->conf, id);
  996. if (ssid == NULL ||
  997. wpa_config_remove_network(wpa_s->conf, id) < 0) {
  998. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  999. "id=%d", id);
  1000. return -1;
  1001. }
  1002. if (ssid == wpa_s->current_ssid) {
  1003. /*
  1004. * Invalidate the EAP session cache if the current network is
  1005. * removed.
  1006. */
  1007. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1008. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1009. }
  1010. return 0;
  1011. }
  1012. static int wpa_supplicant_ctrl_iface_set_network(
  1013. struct wpa_supplicant *wpa_s, char *cmd)
  1014. {
  1015. int id;
  1016. struct wpa_ssid *ssid;
  1017. char *name, *value;
  1018. /* cmd: "<network id> <variable name> <value>" */
  1019. name = os_strchr(cmd, ' ');
  1020. if (name == NULL)
  1021. return -1;
  1022. *name++ = '\0';
  1023. value = os_strchr(name, ' ');
  1024. if (value == NULL)
  1025. return -1;
  1026. *value++ = '\0';
  1027. id = atoi(cmd);
  1028. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  1029. id, name);
  1030. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1031. (u8 *) value, os_strlen(value));
  1032. ssid = wpa_config_get_network(wpa_s->conf, id);
  1033. if (ssid == NULL) {
  1034. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1035. "id=%d", id);
  1036. return -1;
  1037. }
  1038. if (wpa_config_set(ssid, name, value, 0) < 0) {
  1039. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  1040. "variable '%s'", name);
  1041. return -1;
  1042. }
  1043. if (wpa_s->current_ssid == ssid) {
  1044. /*
  1045. * Invalidate the EAP session cache if anything in the current
  1046. * configuration changes.
  1047. */
  1048. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1049. }
  1050. if ((os_strcmp(name, "psk") == 0 &&
  1051. value[0] == '"' && ssid->ssid_len) ||
  1052. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  1053. wpa_config_update_psk(ssid);
  1054. else if (os_strcmp(name, "priority") == 0)
  1055. wpa_config_update_prio_list(wpa_s->conf);
  1056. return 0;
  1057. }
  1058. static int wpa_supplicant_ctrl_iface_get_network(
  1059. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1060. {
  1061. int id;
  1062. size_t res;
  1063. struct wpa_ssid *ssid;
  1064. char *name, *value;
  1065. /* cmd: "<network id> <variable name>" */
  1066. name = os_strchr(cmd, ' ');
  1067. if (name == NULL || buflen == 0)
  1068. return -1;
  1069. *name++ = '\0';
  1070. id = atoi(cmd);
  1071. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  1072. id, name);
  1073. ssid = wpa_config_get_network(wpa_s->conf, id);
  1074. if (ssid == NULL) {
  1075. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1076. "id=%d", id);
  1077. return -1;
  1078. }
  1079. value = wpa_config_get_no_key(ssid, name);
  1080. if (value == NULL) {
  1081. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  1082. "variable '%s'", name);
  1083. return -1;
  1084. }
  1085. res = os_strlcpy(buf, value, buflen);
  1086. if (res >= buflen) {
  1087. os_free(value);
  1088. return -1;
  1089. }
  1090. os_free(value);
  1091. return res;
  1092. }
  1093. #ifndef CONFIG_NO_CONFIG_WRITE
  1094. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  1095. {
  1096. int ret;
  1097. if (!wpa_s->conf->update_config) {
  1098. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  1099. "to update configuration (update_config=0)");
  1100. return -1;
  1101. }
  1102. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  1103. if (ret) {
  1104. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  1105. "update configuration");
  1106. } else {
  1107. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  1108. " updated");
  1109. }
  1110. return ret;
  1111. }
  1112. #endif /* CONFIG_NO_CONFIG_WRITE */
  1113. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  1114. struct wpa_driver_capa *capa,
  1115. char *buf, size_t buflen)
  1116. {
  1117. int ret, first = 1;
  1118. char *pos, *end;
  1119. size_t len;
  1120. pos = buf;
  1121. end = pos + buflen;
  1122. if (res < 0) {
  1123. if (strict)
  1124. return 0;
  1125. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  1126. if (len >= buflen)
  1127. return -1;
  1128. return len;
  1129. }
  1130. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1131. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1132. if (ret < 0 || ret >= end - pos)
  1133. return pos - buf;
  1134. pos += ret;
  1135. first = 0;
  1136. }
  1137. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1138. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1139. if (ret < 0 || ret >= end - pos)
  1140. return pos - buf;
  1141. pos += ret;
  1142. first = 0;
  1143. }
  1144. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1145. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  1146. if (ret < 0 || ret >= end - pos)
  1147. return pos - buf;
  1148. pos += ret;
  1149. first = 0;
  1150. }
  1151. return pos - buf;
  1152. }
  1153. static int ctrl_iface_get_capability_group(int res, char *strict,
  1154. struct wpa_driver_capa *capa,
  1155. char *buf, size_t buflen)
  1156. {
  1157. int ret, first = 1;
  1158. char *pos, *end;
  1159. size_t len;
  1160. pos = buf;
  1161. end = pos + buflen;
  1162. if (res < 0) {
  1163. if (strict)
  1164. return 0;
  1165. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  1166. if (len >= buflen)
  1167. return -1;
  1168. return len;
  1169. }
  1170. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1171. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1172. if (ret < 0 || ret >= end - pos)
  1173. return pos - buf;
  1174. pos += ret;
  1175. first = 0;
  1176. }
  1177. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1178. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1179. if (ret < 0 || ret >= end - pos)
  1180. return pos - buf;
  1181. pos += ret;
  1182. first = 0;
  1183. }
  1184. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  1185. ret = os_snprintf(pos, end - pos, "%sWEP104",
  1186. first ? "" : " ");
  1187. if (ret < 0 || ret >= end - pos)
  1188. return pos - buf;
  1189. pos += ret;
  1190. first = 0;
  1191. }
  1192. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  1193. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  1194. if (ret < 0 || ret >= end - pos)
  1195. return pos - buf;
  1196. pos += ret;
  1197. first = 0;
  1198. }
  1199. return pos - buf;
  1200. }
  1201. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  1202. struct wpa_driver_capa *capa,
  1203. char *buf, size_t buflen)
  1204. {
  1205. int ret;
  1206. char *pos, *end;
  1207. size_t len;
  1208. pos = buf;
  1209. end = pos + buflen;
  1210. if (res < 0) {
  1211. if (strict)
  1212. return 0;
  1213. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  1214. "NONE", buflen);
  1215. if (len >= buflen)
  1216. return -1;
  1217. return len;
  1218. }
  1219. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  1220. if (ret < 0 || ret >= end - pos)
  1221. return pos - buf;
  1222. pos += ret;
  1223. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1224. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  1225. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  1226. if (ret < 0 || ret >= end - pos)
  1227. return pos - buf;
  1228. pos += ret;
  1229. }
  1230. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1231. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1232. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  1233. if (ret < 0 || ret >= end - pos)
  1234. return pos - buf;
  1235. pos += ret;
  1236. }
  1237. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1238. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  1239. if (ret < 0 || ret >= end - pos)
  1240. return pos - buf;
  1241. pos += ret;
  1242. }
  1243. return pos - buf;
  1244. }
  1245. static int ctrl_iface_get_capability_proto(int res, char *strict,
  1246. struct wpa_driver_capa *capa,
  1247. char *buf, size_t buflen)
  1248. {
  1249. int ret, first = 1;
  1250. char *pos, *end;
  1251. size_t len;
  1252. pos = buf;
  1253. end = pos + buflen;
  1254. if (res < 0) {
  1255. if (strict)
  1256. return 0;
  1257. len = os_strlcpy(buf, "RSN WPA", buflen);
  1258. if (len >= buflen)
  1259. return -1;
  1260. return len;
  1261. }
  1262. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1263. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1264. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  1265. if (ret < 0 || ret >= end - pos)
  1266. return pos - buf;
  1267. pos += ret;
  1268. first = 0;
  1269. }
  1270. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1271. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  1272. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  1273. if (ret < 0 || ret >= end - pos)
  1274. return pos - buf;
  1275. pos += ret;
  1276. first = 0;
  1277. }
  1278. return pos - buf;
  1279. }
  1280. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  1281. struct wpa_driver_capa *capa,
  1282. char *buf, size_t buflen)
  1283. {
  1284. int ret, first = 1;
  1285. char *pos, *end;
  1286. size_t len;
  1287. pos = buf;
  1288. end = pos + buflen;
  1289. if (res < 0) {
  1290. if (strict)
  1291. return 0;
  1292. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  1293. if (len >= buflen)
  1294. return -1;
  1295. return len;
  1296. }
  1297. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  1298. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  1299. if (ret < 0 || ret >= end - pos)
  1300. return pos - buf;
  1301. pos += ret;
  1302. first = 0;
  1303. }
  1304. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  1305. ret = os_snprintf(pos, end - pos, "%sSHARED",
  1306. first ? "" : " ");
  1307. if (ret < 0 || ret >= end - pos)
  1308. return pos - buf;
  1309. pos += ret;
  1310. first = 0;
  1311. }
  1312. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  1313. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  1314. if (ret < 0 || ret >= end - pos)
  1315. return pos - buf;
  1316. pos += ret;
  1317. first = 0;
  1318. }
  1319. return pos - buf;
  1320. }
  1321. static int wpa_supplicant_ctrl_iface_get_capability(
  1322. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  1323. size_t buflen)
  1324. {
  1325. struct wpa_driver_capa capa;
  1326. int res;
  1327. char *strict;
  1328. char field[30];
  1329. size_t len;
  1330. /* Determine whether or not strict checking was requested */
  1331. len = os_strlcpy(field, _field, sizeof(field));
  1332. if (len >= sizeof(field))
  1333. return -1;
  1334. strict = os_strchr(field, ' ');
  1335. if (strict != NULL) {
  1336. *strict++ = '\0';
  1337. if (os_strcmp(strict, "strict") != 0)
  1338. return -1;
  1339. }
  1340. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  1341. field, strict ? strict : "");
  1342. if (os_strcmp(field, "eap") == 0) {
  1343. return eap_get_names(buf, buflen);
  1344. }
  1345. res = wpa_drv_get_capa(wpa_s, &capa);
  1346. if (os_strcmp(field, "pairwise") == 0)
  1347. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  1348. buf, buflen);
  1349. if (os_strcmp(field, "group") == 0)
  1350. return ctrl_iface_get_capability_group(res, strict, &capa,
  1351. buf, buflen);
  1352. if (os_strcmp(field, "key_mgmt") == 0)
  1353. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  1354. buf, buflen);
  1355. if (os_strcmp(field, "proto") == 0)
  1356. return ctrl_iface_get_capability_proto(res, strict, &capa,
  1357. buf, buflen);
  1358. if (os_strcmp(field, "auth_alg") == 0)
  1359. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  1360. buf, buflen);
  1361. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  1362. field);
  1363. return -1;
  1364. }
  1365. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  1366. const char *cmd, char *buf,
  1367. size_t buflen)
  1368. {
  1369. u8 bssid[ETH_ALEN];
  1370. size_t i;
  1371. struct wpa_bss *bss;
  1372. int ret;
  1373. char *pos, *end;
  1374. const u8 *ie, *ie2;
  1375. if (os_strcmp(cmd, "FIRST") == 0)
  1376. bss = dl_list_first(&wpa_s->bss, struct wpa_bss, list);
  1377. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  1378. i = atoi(cmd + 3);
  1379. bss = wpa_bss_get_id(wpa_s, i);
  1380. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  1381. i = atoi(cmd + 5);
  1382. bss = wpa_bss_get_id(wpa_s, i);
  1383. if (bss) {
  1384. struct dl_list *next = bss->list_id.next;
  1385. if (next == &wpa_s->bss_id)
  1386. bss = NULL;
  1387. else
  1388. bss = dl_list_entry(next, struct wpa_bss,
  1389. list_id);
  1390. }
  1391. } else if (hwaddr_aton(cmd, bssid) == 0)
  1392. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1393. else {
  1394. struct wpa_bss *tmp;
  1395. i = atoi(cmd);
  1396. bss = NULL;
  1397. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  1398. {
  1399. if (i-- == 0) {
  1400. bss = tmp;
  1401. break;
  1402. }
  1403. }
  1404. }
  1405. if (bss == NULL)
  1406. return 0;
  1407. pos = buf;
  1408. end = buf + buflen;
  1409. ret = os_snprintf(pos, end - pos,
  1410. "id=%u\n"
  1411. "bssid=" MACSTR "\n"
  1412. "freq=%d\n"
  1413. "beacon_int=%d\n"
  1414. "capabilities=0x%04x\n"
  1415. "qual=%d\n"
  1416. "noise=%d\n"
  1417. "level=%d\n"
  1418. "tsf=%016llu\n"
  1419. "ie=",
  1420. bss->id,
  1421. MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
  1422. bss->caps, bss->qual, bss->noise, bss->level,
  1423. (unsigned long long) bss->tsf);
  1424. if (ret < 0 || ret >= end - pos)
  1425. return pos - buf;
  1426. pos += ret;
  1427. ie = (const u8 *) (bss + 1);
  1428. for (i = 0; i < bss->ie_len; i++) {
  1429. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  1430. if (ret < 0 || ret >= end - pos)
  1431. return pos - buf;
  1432. pos += ret;
  1433. }
  1434. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE)) {
  1435. ret = os_snprintf(pos, end - pos, "[P2P]");
  1436. if (ret < 0 || ret >= end - pos)
  1437. return pos - buf;
  1438. pos += ret;
  1439. }
  1440. ret = os_snprintf(pos, end - pos, "\n");
  1441. if (ret < 0 || ret >= end - pos)
  1442. return pos - buf;
  1443. pos += ret;
  1444. ret = os_snprintf(pos, end - pos, "flags=");
  1445. if (ret < 0 || ret >= end - pos)
  1446. return pos - buf;
  1447. pos += ret;
  1448. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1449. if (ie)
  1450. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1451. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1452. if (ie2)
  1453. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1454. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1455. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1456. ret = os_snprintf(pos, end - pos, "[WEP]");
  1457. if (ret < 0 || ret >= end - pos)
  1458. return pos - buf;
  1459. pos += ret;
  1460. }
  1461. if (bss->caps & IEEE80211_CAP_IBSS) {
  1462. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1463. if (ret < 0 || ret >= end - pos)
  1464. return pos - buf;
  1465. pos += ret;
  1466. }
  1467. if (bss->caps & IEEE80211_CAP_ESS) {
  1468. ret = os_snprintf(pos, end - pos, "[ESS]");
  1469. if (ret < 0 || ret >= end - pos)
  1470. return pos - buf;
  1471. pos += ret;
  1472. }
  1473. ret = os_snprintf(pos, end - pos, "\n");
  1474. if (ret < 0 || ret >= end - pos)
  1475. return pos - buf;
  1476. pos += ret;
  1477. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  1478. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1479. if (ret < 0 || ret >= end - pos)
  1480. return pos - buf;
  1481. pos += ret;
  1482. #ifdef CONFIG_WPS
  1483. ie = (const u8 *) (bss + 1);
  1484. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  1485. if (ret < 0 || ret >= end - pos)
  1486. return pos - buf;
  1487. pos += ret;
  1488. #endif /* CONFIG_WPS */
  1489. #ifdef CONFIG_P2P
  1490. ie = (const u8 *) (bss + 1);
  1491. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  1492. if (ret < 0 || ret >= end - pos)
  1493. return pos - buf;
  1494. pos += ret;
  1495. #endif /* CONFIG_P2P */
  1496. return pos - buf;
  1497. }
  1498. static int wpa_supplicant_ctrl_iface_ap_scan(
  1499. struct wpa_supplicant *wpa_s, char *cmd)
  1500. {
  1501. int ap_scan = atoi(cmd);
  1502. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  1503. }
  1504. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  1505. {
  1506. u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
  1507. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  1508. /* MLME-DELETEKEYS.request */
  1509. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
  1510. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
  1511. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
  1512. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
  1513. #ifdef CONFIG_IEEE80211W
  1514. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
  1515. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
  1516. #endif /* CONFIG_IEEE80211W */
  1517. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  1518. 0);
  1519. /* MLME-SETPROTECTION.request(None) */
  1520. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  1521. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  1522. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  1523. wpa_sm_drop_sa(wpa_s->wpa);
  1524. }
  1525. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  1526. char *addr)
  1527. {
  1528. u8 bssid[ETH_ALEN];
  1529. struct wpa_bss *bss;
  1530. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1531. if (hwaddr_aton(addr, bssid)) {
  1532. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  1533. "address '%s'", addr);
  1534. return -1;
  1535. }
  1536. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  1537. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1538. if (!bss) {
  1539. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  1540. "from BSS table");
  1541. return -1;
  1542. }
  1543. /*
  1544. * TODO: Find best network configuration block from configuration to
  1545. * allow roaming to other networks
  1546. */
  1547. if (!ssid) {
  1548. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  1549. "configuration known for the target AP");
  1550. return -1;
  1551. }
  1552. wpa_s->reassociate = 1;
  1553. wpa_supplicant_connect(wpa_s, bss, ssid);
  1554. return 0;
  1555. }
  1556. #ifdef CONFIG_P2P
  1557. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  1558. {
  1559. unsigned int timeout = atoi(cmd);
  1560. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  1561. if (os_strstr(cmd, "type=social"))
  1562. type = P2P_FIND_ONLY_SOCIAL;
  1563. else if (os_strstr(cmd, "type=progressive"))
  1564. type = P2P_FIND_PROGRESSIVE;
  1565. wpas_p2p_find(wpa_s, timeout, type);
  1566. return 0;
  1567. }
  1568. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  1569. char *buf, size_t buflen)
  1570. {
  1571. u8 addr[ETH_ALEN];
  1572. char *pos, *pos2;
  1573. char *pin = NULL;
  1574. enum p2p_wps_method wps_method;
  1575. int new_pin;
  1576. int ret;
  1577. int persistent_group;
  1578. int join;
  1579. int auth;
  1580. int go_intent = -1;
  1581. int freq = 0;
  1582. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad] [persistent]
  1583. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] */
  1584. if (hwaddr_aton(cmd, addr))
  1585. return -1;
  1586. pos = cmd + 17;
  1587. if (*pos != ' ')
  1588. return -1;
  1589. pos++;
  1590. persistent_group = os_strstr(pos, " persistent") != NULL;
  1591. join = os_strstr(pos, " join") != NULL;
  1592. auth = os_strstr(pos, " auth") != NULL;
  1593. pos2 = os_strstr(pos, " go_intent=");
  1594. if (pos2) {
  1595. pos2 += 11;
  1596. go_intent = atoi(pos2);
  1597. if (go_intent < 0 || go_intent > 15)
  1598. return -1;
  1599. }
  1600. pos2 = os_strstr(pos, " freq=");
  1601. if (pos2) {
  1602. pos2 += 6;
  1603. freq = atoi(pos2);
  1604. if (freq <= 0)
  1605. return -1;
  1606. }
  1607. if (os_strncmp(pos, "pin", 3) == 0) {
  1608. /* Request random PIN (to be displayed) and enable the PIN */
  1609. wps_method = WPS_PIN_DISPLAY;
  1610. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  1611. wps_method = WPS_PBC;
  1612. } else {
  1613. pin = pos;
  1614. pos = os_strchr(pin, ' ');
  1615. wps_method = WPS_PIN_KEYPAD;
  1616. if (pos) {
  1617. *pos++ = '\0';
  1618. if (os_strncmp(pos, "label", 5) == 0)
  1619. wps_method = WPS_PIN_LABEL;
  1620. else if (os_strncmp(pos, "display", 7) == 0)
  1621. wps_method = WPS_PIN_DISPLAY;
  1622. }
  1623. }
  1624. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  1625. persistent_group, join, auth, go_intent,
  1626. freq);
  1627. if (new_pin < 0)
  1628. return -1;
  1629. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  1630. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  1631. if (ret < 0 || (size_t) ret >= buflen)
  1632. return -1;
  1633. return ret;
  1634. }
  1635. os_memcpy(buf, "OK\n", 3);
  1636. return 3;
  1637. }
  1638. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  1639. {
  1640. unsigned int timeout = atoi(cmd);
  1641. return wpas_p2p_listen(wpa_s, timeout);
  1642. }
  1643. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  1644. {
  1645. u8 addr[ETH_ALEN];
  1646. char *pos;
  1647. /* <addr> <config method> */
  1648. if (hwaddr_aton(cmd, addr))
  1649. return -1;
  1650. pos = cmd + 17;
  1651. if (*pos != ' ')
  1652. return -1;
  1653. pos++;
  1654. return wpas_p2p_prov_disc(wpa_s, addr, pos);
  1655. }
  1656. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  1657. size_t buflen)
  1658. {
  1659. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1660. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  1661. ssid->passphrase == NULL)
  1662. return -1;
  1663. os_strlcpy(buf, ssid->passphrase, buflen);
  1664. return os_strlen(buf);
  1665. }
  1666. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  1667. char *buf, size_t buflen)
  1668. {
  1669. u64 ref;
  1670. int res;
  1671. u8 dst_buf[ETH_ALEN], *dst;
  1672. struct wpabuf *tlvs;
  1673. char *pos;
  1674. size_t len;
  1675. if (hwaddr_aton(cmd, dst_buf))
  1676. return -1;
  1677. dst = dst_buf;
  1678. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  1679. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  1680. dst = NULL;
  1681. pos = cmd + 17;
  1682. if (*pos != ' ')
  1683. return -1;
  1684. pos++;
  1685. if (os_strncmp(pos, "upnp ", 5) == 0) {
  1686. u8 version;
  1687. pos += 5;
  1688. if (hexstr2bin(pos, &version, 1) < 0)
  1689. return -1;
  1690. pos += 2;
  1691. if (*pos != ' ')
  1692. return -1;
  1693. pos++;
  1694. ref = (u64) wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  1695. } else {
  1696. len = os_strlen(pos);
  1697. if (len & 1)
  1698. return -1;
  1699. len /= 2;
  1700. tlvs = wpabuf_alloc(len);
  1701. if (tlvs == NULL)
  1702. return -1;
  1703. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  1704. wpabuf_free(tlvs);
  1705. return -1;
  1706. }
  1707. ref = (u64) wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1708. wpabuf_free(tlvs);
  1709. }
  1710. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  1711. if (res < 0 || (unsigned) res >= buflen)
  1712. return -1;
  1713. return res;
  1714. }
  1715. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  1716. char *cmd)
  1717. {
  1718. long long unsigned val;
  1719. u64 req;
  1720. if (sscanf(cmd, "%llx", &val) != 1)
  1721. return -1;
  1722. req = val;
  1723. return wpas_p2p_sd_cancel_request(wpa_s, (void *) req);
  1724. }
  1725. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  1726. {
  1727. int freq;
  1728. u8 dst_buf[ETH_ALEN], *dst;
  1729. u8 dialog_token;
  1730. struct wpabuf *resp_tlvs;
  1731. char *pos, *pos2;
  1732. size_t len;
  1733. pos = os_strchr(cmd, ' ');
  1734. if (pos == NULL)
  1735. return -1;
  1736. *pos++ = '\0';
  1737. freq = atoi(cmd);
  1738. if (freq == 0)
  1739. return -1;
  1740. if (hwaddr_aton(pos, dst_buf))
  1741. return -1;
  1742. dst = dst_buf;
  1743. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  1744. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  1745. dst = NULL;
  1746. pos += 17;
  1747. if (*pos != ' ')
  1748. return -1;
  1749. pos++;
  1750. pos2 = os_strchr(pos, ' ');
  1751. if (pos2 == NULL)
  1752. return -1;
  1753. *pos2++ = '\0';
  1754. dialog_token = atoi(pos);
  1755. len = os_strlen(pos2);
  1756. if (len & 1)
  1757. return -1;
  1758. len /= 2;
  1759. resp_tlvs = wpabuf_alloc(len);
  1760. if (resp_tlvs == NULL)
  1761. return -1;
  1762. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  1763. wpabuf_free(resp_tlvs);
  1764. return -1;
  1765. }
  1766. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  1767. wpabuf_free(resp_tlvs);
  1768. return 0;
  1769. }
  1770. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  1771. char *cmd)
  1772. {
  1773. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  1774. return 0;
  1775. }
  1776. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  1777. char *cmd)
  1778. {
  1779. char *pos;
  1780. size_t len;
  1781. struct wpabuf *query, *resp;
  1782. pos = os_strchr(cmd, ' ');
  1783. if (pos == NULL)
  1784. return -1;
  1785. *pos++ = '\0';
  1786. len = os_strlen(cmd);
  1787. if (len & 1)
  1788. return -1;
  1789. len /= 2;
  1790. query = wpabuf_alloc(len);
  1791. if (query == NULL)
  1792. return -1;
  1793. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  1794. wpabuf_free(query);
  1795. return -1;
  1796. }
  1797. len = os_strlen(pos);
  1798. if (len & 1) {
  1799. wpabuf_free(query);
  1800. return -1;
  1801. }
  1802. len /= 2;
  1803. resp = wpabuf_alloc(len);
  1804. if (resp == NULL) {
  1805. wpabuf_free(query);
  1806. return -1;
  1807. }
  1808. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  1809. wpabuf_free(query);
  1810. wpabuf_free(resp);
  1811. return -1;
  1812. }
  1813. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  1814. wpabuf_free(query);
  1815. wpabuf_free(resp);
  1816. return -1;
  1817. }
  1818. return 0;
  1819. }
  1820. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  1821. {
  1822. char *pos;
  1823. u8 version;
  1824. pos = os_strchr(cmd, ' ');
  1825. if (pos == NULL)
  1826. return -1;
  1827. *pos++ = '\0';
  1828. if (hexstr2bin(cmd, &version, 1) < 0)
  1829. return -1;
  1830. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  1831. }
  1832. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  1833. {
  1834. char *pos;
  1835. pos = os_strchr(cmd, ' ');
  1836. if (pos == NULL)
  1837. return -1;
  1838. *pos++ = '\0';
  1839. if (os_strcmp(cmd, "bonjour") == 0)
  1840. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  1841. if (os_strcmp(cmd, "upnp") == 0)
  1842. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  1843. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  1844. return -1;
  1845. }
  1846. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  1847. char *cmd)
  1848. {
  1849. size_t len;
  1850. struct wpabuf *query;
  1851. int ret;
  1852. len = os_strlen(cmd);
  1853. if (len & 1)
  1854. return -1;
  1855. len /= 2;
  1856. query = wpabuf_alloc(len);
  1857. if (query == NULL)
  1858. return -1;
  1859. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  1860. wpabuf_free(query);
  1861. return -1;
  1862. }
  1863. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  1864. wpabuf_free(query);
  1865. return ret;
  1866. }
  1867. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  1868. {
  1869. char *pos;
  1870. u8 version;
  1871. pos = os_strchr(cmd, ' ');
  1872. if (pos == NULL)
  1873. return -1;
  1874. *pos++ = '\0';
  1875. if (hexstr2bin(cmd, &version, 1) < 0)
  1876. return -1;
  1877. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  1878. }
  1879. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  1880. {
  1881. char *pos;
  1882. pos = os_strchr(cmd, ' ');
  1883. if (pos == NULL)
  1884. return -1;
  1885. *pos++ = '\0';
  1886. if (os_strcmp(cmd, "bonjour") == 0)
  1887. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  1888. if (os_strcmp(cmd, "upnp") == 0)
  1889. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  1890. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  1891. return -1;
  1892. }
  1893. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  1894. {
  1895. u8 addr[ETH_ALEN];
  1896. /* <addr> */
  1897. if (hwaddr_aton(cmd, addr))
  1898. return -1;
  1899. return wpas_p2p_reject(wpa_s, addr);
  1900. }
  1901. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  1902. {
  1903. char *pos;
  1904. int id;
  1905. struct wpa_ssid *ssid;
  1906. u8 peer[ETH_ALEN];
  1907. id = atoi(cmd);
  1908. pos = os_strstr(cmd, " peer=");
  1909. if (pos) {
  1910. pos += 6;
  1911. if (hwaddr_aton(pos, peer))
  1912. return -1;
  1913. }
  1914. ssid = wpa_config_get_network(wpa_s->conf, id);
  1915. if (ssid == NULL || ssid->disabled != 2) {
  1916. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1917. "for persistent P2P group",
  1918. id);
  1919. return -1;
  1920. }
  1921. return wpas_p2p_invite(wpa_s, pos ? peer : NULL, ssid, NULL);
  1922. }
  1923. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  1924. {
  1925. char *pos;
  1926. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  1927. pos = os_strstr(cmd, " peer=");
  1928. if (!pos)
  1929. return -1;
  1930. *pos = '\0';
  1931. pos += 6;
  1932. if (hwaddr_aton(pos, peer)) {
  1933. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  1934. return -1;
  1935. }
  1936. pos = os_strstr(pos, " go_dev_addr=");
  1937. if (pos) {
  1938. pos += 13;
  1939. if (hwaddr_aton(pos, go_dev_addr)) {
  1940. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  1941. pos);
  1942. return -1;
  1943. }
  1944. go_dev = go_dev_addr;
  1945. }
  1946. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  1947. }
  1948. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  1949. {
  1950. if (os_strncmp(cmd, "persistent=", 11) == 0)
  1951. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  1952. if (os_strncmp(cmd, "group=", 6) == 0)
  1953. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  1954. return -1;
  1955. }
  1956. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  1957. char *cmd, int freq)
  1958. {
  1959. int id;
  1960. struct wpa_ssid *ssid;
  1961. id = atoi(cmd);
  1962. ssid = wpa_config_get_network(wpa_s->conf, id);
  1963. if (ssid == NULL || ssid->disabled != 2) {
  1964. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1965. "for persistent P2P group",
  1966. id);
  1967. return -1;
  1968. }
  1969. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq);
  1970. }
  1971. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  1972. {
  1973. int freq = 0;
  1974. char *pos;
  1975. pos = os_strstr(cmd, "freq=");
  1976. if (pos)
  1977. freq = atoi(pos + 5);
  1978. if (os_strncmp(cmd, "persistent=", 11) == 0)
  1979. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq);
  1980. if (os_strcmp(cmd, "persistent") == 0 ||
  1981. os_strncmp(cmd, "persistent ", 11) == 0)
  1982. return wpas_p2p_group_add(wpa_s, 1, freq);
  1983. if (os_strncmp(cmd, "freq=", 5) == 0)
  1984. return wpas_p2p_group_add(wpa_s, 0, freq);
  1985. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  1986. cmd);
  1987. return -1;
  1988. }
  1989. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  1990. char *buf, size_t buflen)
  1991. {
  1992. u8 addr[ETH_ALEN], *addr_ptr;
  1993. int next;
  1994. if (!wpa_s->global->p2p)
  1995. return -1;
  1996. if (os_strcmp(cmd, "FIRST") == 0) {
  1997. addr_ptr = NULL;
  1998. next = 0;
  1999. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  2000. if (hwaddr_aton(cmd + 5, addr) < 0)
  2001. return -1;
  2002. addr_ptr = addr;
  2003. next = 1;
  2004. } else {
  2005. if (hwaddr_aton(cmd, addr) < 0)
  2006. return -1;
  2007. addr_ptr = addr;
  2008. next = 0;
  2009. }
  2010. return p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next,
  2011. buf, buflen);
  2012. }
  2013. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  2014. {
  2015. char *param;
  2016. if (wpa_s->global->p2p == NULL)
  2017. return -1;
  2018. param = os_strchr(cmd, ' ');
  2019. if (param == NULL)
  2020. return -1;
  2021. *param++ = '\0';
  2022. if (os_strcmp(cmd, "discoverability") == 0) {
  2023. p2p_set_client_discoverability(wpa_s->global->p2p,
  2024. atoi(param));
  2025. return 0;
  2026. }
  2027. if (os_strcmp(cmd, "managed") == 0) {
  2028. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  2029. return 0;
  2030. }
  2031. if (os_strcmp(cmd, "listen_channel") == 0) {
  2032. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  2033. atoi(param));
  2034. }
  2035. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  2036. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  2037. os_strlen(param));
  2038. }
  2039. if (os_strcmp(cmd, "noa") == 0) {
  2040. char *pos;
  2041. int count, start, duration;
  2042. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  2043. count = atoi(param);
  2044. pos = os_strchr(param, ',');
  2045. if (pos == NULL)
  2046. return -1;
  2047. pos++;
  2048. start = atoi(pos);
  2049. pos = os_strchr(pos, ',');
  2050. if (pos == NULL)
  2051. return -1;
  2052. pos++;
  2053. duration = atoi(pos);
  2054. if (count < 0 || count > 255 || start < 0 || duration < 0)
  2055. return -1;
  2056. if (count == 0 && duration > 0)
  2057. return -1;
  2058. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  2059. "start=%d duration=%d", count, start, duration);
  2060. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  2061. }
  2062. if (os_strcmp(cmd, "ps") == 0)
  2063. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  2064. if (os_strcmp(cmd, "oppps") == 0)
  2065. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  2066. if (os_strcmp(cmd, "ctwindow") == 0)
  2067. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  2068. if (os_strcmp(cmd, "disabled") == 0) {
  2069. wpa_s->global->p2p_disabled = atoi(param);
  2070. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  2071. wpa_s->global->p2p_disabled ?
  2072. "disabled" : "enabled");
  2073. if (wpa_s->global->p2p_disabled) {
  2074. wpas_p2p_stop_find(wpa_s);
  2075. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  2076. p2p_flush(wpa_s->global->p2p);
  2077. }
  2078. return 0;
  2079. }
  2080. if (os_strcmp(cmd, "force_long_sd") == 0) {
  2081. wpa_s->force_long_sd = atoi(param);
  2082. return 0;
  2083. }
  2084. if (os_strcmp(cmd, "peer_filter") == 0) {
  2085. u8 addr[ETH_ALEN];
  2086. if (hwaddr_aton(param, addr))
  2087. return -1;
  2088. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  2089. return 0;
  2090. }
  2091. if (os_strcmp(cmd, "cross_connect") == 0)
  2092. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  2093. if (os_strcmp(cmd, "go_apsd") == 0) {
  2094. if (os_strcmp(param, "disable") == 0)
  2095. wpa_s->set_ap_uapsd = 0;
  2096. else {
  2097. wpa_s->set_ap_uapsd = 1;
  2098. wpa_s->ap_uapsd = atoi(param);
  2099. }
  2100. return 0;
  2101. }
  2102. if (os_strcmp(cmd, "client_apsd") == 0) {
  2103. if (os_strcmp(param, "disable") == 0)
  2104. wpa_s->set_sta_uapsd = 0;
  2105. else {
  2106. int be, bk, vi, vo;
  2107. char *pos;
  2108. /* format: BE,BK,VI,VO;max SP Length */
  2109. be = atoi(param);
  2110. pos = os_strchr(param, ',');
  2111. if (pos == NULL)
  2112. return -1;
  2113. pos++;
  2114. bk = atoi(pos);
  2115. pos = os_strchr(pos, ',');
  2116. if (pos == NULL)
  2117. return -1;
  2118. pos++;
  2119. vi = atoi(pos);
  2120. pos = os_strchr(pos, ',');
  2121. if (pos == NULL)
  2122. return -1;
  2123. pos++;
  2124. vo = atoi(pos);
  2125. /* ignore max SP Length for now */
  2126. wpa_s->set_sta_uapsd = 1;
  2127. wpa_s->sta_uapsd = 0;
  2128. if (be)
  2129. wpa_s->sta_uapsd |= BIT(0);
  2130. if (bk)
  2131. wpa_s->sta_uapsd |= BIT(1);
  2132. if (vi)
  2133. wpa_s->sta_uapsd |= BIT(2);
  2134. if (vo)
  2135. wpa_s->sta_uapsd |= BIT(3);
  2136. }
  2137. return 0;
  2138. }
  2139. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  2140. cmd);
  2141. return -1;
  2142. }
  2143. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  2144. {
  2145. char *pos, *pos2;
  2146. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  2147. if (cmd[0]) {
  2148. pos = os_strchr(cmd, ' ');
  2149. if (pos == NULL)
  2150. return -1;
  2151. *pos++ = '\0';
  2152. dur1 = atoi(cmd);
  2153. pos2 = os_strchr(pos, ' ');
  2154. if (pos2)
  2155. *pos2++ = '\0';
  2156. int1 = atoi(pos);
  2157. } else
  2158. pos2 = NULL;
  2159. if (pos2) {
  2160. pos = os_strchr(pos2, ' ');
  2161. if (pos == NULL)
  2162. return -1;
  2163. *pos++ = '\0';
  2164. dur2 = atoi(pos2);
  2165. int2 = atoi(pos);
  2166. }
  2167. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  2168. }
  2169. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  2170. {
  2171. char *pos;
  2172. unsigned int period = 0, interval = 0;
  2173. if (cmd[0]) {
  2174. pos = os_strchr(cmd, ' ');
  2175. if (pos == NULL)
  2176. return -1;
  2177. *pos++ = '\0';
  2178. period = atoi(cmd);
  2179. interval = atoi(pos);
  2180. }
  2181. return wpas_p2p_ext_listen(wpa_s, period, interval);
  2182. }
  2183. #endif /* CONFIG_P2P */
  2184. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  2185. char *buf, size_t *resp_len)
  2186. {
  2187. char *reply;
  2188. const int reply_size = 4096;
  2189. int ctrl_rsp = 0;
  2190. int reply_len;
  2191. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  2192. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  2193. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  2194. (const u8 *) buf, os_strlen(buf));
  2195. } else {
  2196. wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface",
  2197. (const u8 *) buf, os_strlen(buf));
  2198. }
  2199. reply = os_malloc(reply_size);
  2200. if (reply == NULL) {
  2201. *resp_len = 1;
  2202. return NULL;
  2203. }
  2204. os_memcpy(reply, "OK\n", 3);
  2205. reply_len = 3;
  2206. if (os_strcmp(buf, "PING") == 0) {
  2207. os_memcpy(reply, "PONG\n", 5);
  2208. reply_len = 5;
  2209. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  2210. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  2211. } else if (os_strcmp(buf, "MIB") == 0) {
  2212. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  2213. if (reply_len >= 0) {
  2214. int res;
  2215. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  2216. reply_size - reply_len);
  2217. if (res < 0)
  2218. reply_len = -1;
  2219. else
  2220. reply_len += res;
  2221. }
  2222. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  2223. reply_len = wpa_supplicant_ctrl_iface_status(
  2224. wpa_s, buf + 6, reply, reply_size);
  2225. } else if (os_strcmp(buf, "PMKSA") == 0) {
  2226. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  2227. reply_size);
  2228. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  2229. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  2230. reply_len = -1;
  2231. } else if (os_strcmp(buf, "LOGON") == 0) {
  2232. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  2233. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  2234. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  2235. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  2236. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  2237. reply_len = -1;
  2238. else {
  2239. wpa_s->disconnected = 0;
  2240. wpa_s->reassociate = 1;
  2241. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2242. }
  2243. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  2244. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  2245. reply_len = -1;
  2246. else if (wpa_s->disconnected) {
  2247. wpa_s->disconnected = 0;
  2248. wpa_s->reassociate = 1;
  2249. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2250. }
  2251. #ifdef IEEE8021X_EAPOL
  2252. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  2253. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  2254. reply_len = -1;
  2255. #endif /* IEEE8021X_EAPOL */
  2256. #ifdef CONFIG_PEERKEY
  2257. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  2258. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  2259. reply_len = -1;
  2260. #endif /* CONFIG_PEERKEY */
  2261. #ifdef CONFIG_IEEE80211R
  2262. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  2263. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  2264. reply_len = -1;
  2265. #endif /* CONFIG_IEEE80211R */
  2266. #ifdef CONFIG_WPS
  2267. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  2268. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL))
  2269. reply_len = -1;
  2270. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  2271. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8))
  2272. reply_len = -1;
  2273. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  2274. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  2275. reply,
  2276. reply_size);
  2277. #ifdef CONFIG_WPS_OOB
  2278. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  2279. if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
  2280. reply_len = -1;
  2281. #endif /* CONFIG_WPS_OOB */
  2282. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  2283. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  2284. reply_len = -1;
  2285. #ifdef CONFIG_WPS_ER
  2286. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  2287. if (wpas_wps_er_start(wpa_s, NULL))
  2288. reply_len = -1;
  2289. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  2290. if (wpas_wps_er_start(wpa_s, buf + 13))
  2291. reply_len = -1;
  2292. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  2293. if (wpas_wps_er_stop(wpa_s))
  2294. reply_len = -1;
  2295. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  2296. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  2297. reply_len = -1;
  2298. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  2299. if (wpas_wps_er_pbc(wpa_s, buf + 11))
  2300. reply_len = -1;
  2301. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  2302. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  2303. reply_len = -1;
  2304. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  2305. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  2306. reply_len = -1;
  2307. #endif /* CONFIG_WPS_ER */
  2308. #endif /* CONFIG_WPS */
  2309. #ifdef CONFIG_IBSS_RSN
  2310. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  2311. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  2312. reply_len = -1;
  2313. #endif /* CONFIG_IBSS_RSN */
  2314. #ifdef CONFIG_P2P
  2315. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  2316. if (p2p_ctrl_find(wpa_s, buf + 9))
  2317. reply_len = -1;
  2318. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  2319. if (p2p_ctrl_find(wpa_s, ""))
  2320. reply_len = -1;
  2321. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  2322. wpas_p2p_stop_find(wpa_s);
  2323. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  2324. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  2325. reply_size);
  2326. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  2327. if (p2p_ctrl_listen(wpa_s, buf + 11))
  2328. reply_len = -1;
  2329. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  2330. if (p2p_ctrl_listen(wpa_s, ""))
  2331. reply_len = -1;
  2332. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  2333. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  2334. reply_len = -1;
  2335. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  2336. if (wpas_p2p_group_add(wpa_s, 0, 0))
  2337. reply_len = -1;
  2338. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  2339. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  2340. reply_len = -1;
  2341. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  2342. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  2343. reply_len = -1;
  2344. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  2345. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  2346. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  2347. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  2348. reply_size);
  2349. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  2350. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  2351. reply_len = -1;
  2352. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  2353. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  2354. reply_len = -1;
  2355. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  2356. wpas_p2p_sd_service_update(wpa_s);
  2357. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  2358. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  2359. reply_len = -1;
  2360. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  2361. wpas_p2p_service_flush(wpa_s);
  2362. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  2363. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  2364. reply_len = -1;
  2365. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  2366. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  2367. reply_len = -1;
  2368. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  2369. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  2370. reply_len = -1;
  2371. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  2372. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  2373. reply_len = -1;
  2374. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  2375. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  2376. reply_size);
  2377. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  2378. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  2379. reply_len = -1;
  2380. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  2381. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  2382. wpa_s->force_long_sd = 0;
  2383. p2p_flush(wpa_s->global->p2p);
  2384. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  2385. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  2386. reply_len = -1;
  2387. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  2388. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  2389. reply_len = -1;
  2390. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  2391. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  2392. reply_len = -1;
  2393. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  2394. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  2395. reply_len = -1;
  2396. #endif /* CONFIG_P2P */
  2397. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  2398. {
  2399. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  2400. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  2401. reply_len = -1;
  2402. else
  2403. ctrl_rsp = 1;
  2404. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  2405. if (wpa_supplicant_reload_configuration(wpa_s))
  2406. reply_len = -1;
  2407. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  2408. wpa_supplicant_terminate_proc(wpa_s->global);
  2409. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  2410. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  2411. reply_len = -1;
  2412. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  2413. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  2414. wpa_s, reply, reply_size);
  2415. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  2416. wpa_s->reassociate = 0;
  2417. wpa_s->disconnected = 1;
  2418. wpa_supplicant_deauthenticate(wpa_s,
  2419. WLAN_REASON_DEAUTH_LEAVING);
  2420. } else if (os_strcmp(buf, "SCAN") == 0) {
  2421. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  2422. reply_len = -1;
  2423. else {
  2424. wpa_s->scan_req = 2;
  2425. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2426. }
  2427. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  2428. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  2429. wpa_s, reply, reply_size);
  2430. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  2431. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  2432. reply_len = -1;
  2433. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  2434. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  2435. reply_len = -1;
  2436. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  2437. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  2438. reply_len = -1;
  2439. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  2440. reply_len = wpa_supplicant_ctrl_iface_add_network(
  2441. wpa_s, reply, reply_size);
  2442. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  2443. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  2444. reply_len = -1;
  2445. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  2446. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  2447. reply_len = -1;
  2448. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  2449. reply_len = wpa_supplicant_ctrl_iface_get_network(
  2450. wpa_s, buf + 12, reply, reply_size);
  2451. #ifndef CONFIG_NO_CONFIG_WRITE
  2452. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  2453. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  2454. reply_len = -1;
  2455. #endif /* CONFIG_NO_CONFIG_WRITE */
  2456. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  2457. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  2458. wpa_s, buf + 15, reply, reply_size);
  2459. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  2460. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  2461. reply_len = -1;
  2462. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  2463. reply_len = wpa_supplicant_global_iface_list(
  2464. wpa_s->global, reply, reply_size);
  2465. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  2466. reply_len = wpa_supplicant_global_iface_interfaces(
  2467. wpa_s->global, reply, reply_size);
  2468. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  2469. reply_len = wpa_supplicant_ctrl_iface_bss(
  2470. wpa_s, buf + 4, reply, reply_size);
  2471. #ifdef CONFIG_AP
  2472. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  2473. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  2474. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  2475. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  2476. reply_size);
  2477. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  2478. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  2479. reply_size);
  2480. #endif /* CONFIG_AP */
  2481. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  2482. wpas_notify_suspend(wpa_s->global);
  2483. } else if (os_strcmp(buf, "RESUME") == 0) {
  2484. wpas_notify_resume(wpa_s->global);
  2485. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  2486. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  2487. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  2488. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  2489. reply_len = -1;
  2490. } else {
  2491. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  2492. reply_len = 16;
  2493. }
  2494. if (reply_len < 0) {
  2495. os_memcpy(reply, "FAIL\n", 5);
  2496. reply_len = 5;
  2497. }
  2498. if (ctrl_rsp)
  2499. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  2500. *resp_len = reply_len;
  2501. return reply;
  2502. }
  2503. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  2504. char *cmd)
  2505. {
  2506. struct wpa_interface iface;
  2507. char *pos;
  2508. /*
  2509. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  2510. * TAB<bridge_ifname>
  2511. */
  2512. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  2513. os_memset(&iface, 0, sizeof(iface));
  2514. do {
  2515. iface.ifname = pos = cmd;
  2516. pos = os_strchr(pos, '\t');
  2517. if (pos)
  2518. *pos++ = '\0';
  2519. if (iface.ifname[0] == '\0')
  2520. return -1;
  2521. if (pos == NULL)
  2522. break;
  2523. iface.confname = pos;
  2524. pos = os_strchr(pos, '\t');
  2525. if (pos)
  2526. *pos++ = '\0';
  2527. if (iface.confname[0] == '\0')
  2528. iface.confname = NULL;
  2529. if (pos == NULL)
  2530. break;
  2531. iface.driver = pos;
  2532. pos = os_strchr(pos, '\t');
  2533. if (pos)
  2534. *pos++ = '\0';
  2535. if (iface.driver[0] == '\0')
  2536. iface.driver = NULL;
  2537. if (pos == NULL)
  2538. break;
  2539. iface.ctrl_interface = pos;
  2540. pos = os_strchr(pos, '\t');
  2541. if (pos)
  2542. *pos++ = '\0';
  2543. if (iface.ctrl_interface[0] == '\0')
  2544. iface.ctrl_interface = NULL;
  2545. if (pos == NULL)
  2546. break;
  2547. iface.driver_param = pos;
  2548. pos = os_strchr(pos, '\t');
  2549. if (pos)
  2550. *pos++ = '\0';
  2551. if (iface.driver_param[0] == '\0')
  2552. iface.driver_param = NULL;
  2553. if (pos == NULL)
  2554. break;
  2555. iface.bridge_ifname = pos;
  2556. pos = os_strchr(pos, '\t');
  2557. if (pos)
  2558. *pos++ = '\0';
  2559. if (iface.bridge_ifname[0] == '\0')
  2560. iface.bridge_ifname = NULL;
  2561. if (pos == NULL)
  2562. break;
  2563. } while (0);
  2564. if (wpa_supplicant_get_iface(global, iface.ifname))
  2565. return -1;
  2566. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  2567. }
  2568. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  2569. char *cmd)
  2570. {
  2571. struct wpa_supplicant *wpa_s;
  2572. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  2573. wpa_s = wpa_supplicant_get_iface(global, cmd);
  2574. if (wpa_s == NULL)
  2575. return -1;
  2576. return wpa_supplicant_remove_iface(global, wpa_s);
  2577. }
  2578. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  2579. {
  2580. struct wpa_interface_info *prev;
  2581. while (iface) {
  2582. prev = iface;
  2583. iface = iface->next;
  2584. os_free(prev->ifname);
  2585. os_free(prev->desc);
  2586. os_free(prev);
  2587. }
  2588. }
  2589. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  2590. char *buf, int len)
  2591. {
  2592. int i, res;
  2593. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  2594. char *pos, *end;
  2595. for (i = 0; wpa_drivers[i]; i++) {
  2596. struct wpa_driver_ops *drv = wpa_drivers[i];
  2597. if (drv->get_interfaces == NULL)
  2598. continue;
  2599. tmp = drv->get_interfaces(global->drv_priv[i]);
  2600. if (tmp == NULL)
  2601. continue;
  2602. if (last == NULL)
  2603. iface = last = tmp;
  2604. else
  2605. last->next = tmp;
  2606. while (last->next)
  2607. last = last->next;
  2608. }
  2609. pos = buf;
  2610. end = buf + len;
  2611. for (tmp = iface; tmp; tmp = tmp->next) {
  2612. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  2613. tmp->drv_name, tmp->ifname,
  2614. tmp->desc ? tmp->desc : "");
  2615. if (res < 0 || res >= end - pos) {
  2616. *pos = '\0';
  2617. break;
  2618. }
  2619. pos += res;
  2620. }
  2621. wpa_free_iface_info(iface);
  2622. return pos - buf;
  2623. }
  2624. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  2625. char *buf, int len)
  2626. {
  2627. int res;
  2628. char *pos, *end;
  2629. struct wpa_supplicant *wpa_s;
  2630. wpa_s = global->ifaces;
  2631. pos = buf;
  2632. end = buf + len;
  2633. while (wpa_s) {
  2634. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  2635. if (res < 0 || res >= end - pos) {
  2636. *pos = '\0';
  2637. break;
  2638. }
  2639. pos += res;
  2640. wpa_s = wpa_s->next;
  2641. }
  2642. return pos - buf;
  2643. }
  2644. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  2645. char *buf, size_t *resp_len)
  2646. {
  2647. char *reply;
  2648. const int reply_size = 2048;
  2649. int reply_len;
  2650. wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
  2651. (const u8 *) buf, os_strlen(buf));
  2652. reply = os_malloc(reply_size);
  2653. if (reply == NULL) {
  2654. *resp_len = 1;
  2655. return NULL;
  2656. }
  2657. os_memcpy(reply, "OK\n", 3);
  2658. reply_len = 3;
  2659. if (os_strcmp(buf, "PING") == 0) {
  2660. os_memcpy(reply, "PONG\n", 5);
  2661. reply_len = 5;
  2662. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  2663. if (wpa_supplicant_global_iface_add(global, buf + 14))
  2664. reply_len = -1;
  2665. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  2666. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  2667. reply_len = -1;
  2668. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  2669. reply_len = wpa_supplicant_global_iface_list(
  2670. global, reply, reply_size);
  2671. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  2672. reply_len = wpa_supplicant_global_iface_interfaces(
  2673. global, reply, reply_size);
  2674. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  2675. wpa_supplicant_terminate_proc(global);
  2676. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  2677. wpas_notify_suspend(global);
  2678. } else if (os_strcmp(buf, "RESUME") == 0) {
  2679. wpas_notify_resume(global);
  2680. } else {
  2681. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  2682. reply_len = 16;
  2683. }
  2684. if (reply_len < 0) {
  2685. os_memcpy(reply, "FAIL\n", 5);
  2686. reply_len = 5;
  2687. }
  2688. *resp_len = reply_len;
  2689. return reply;
  2690. }