ctrl_iface.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224
  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 "notify.h"
  33. #include "bss.h"
  34. #include "scan.h"
  35. #include "ctrl_iface.h"
  36. extern struct wpa_driver_ops *wpa_drivers[];
  37. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  38. char *buf, int len);
  39. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  40. char *buf, int len);
  41. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  42. char *cmd)
  43. {
  44. char *value;
  45. int ret = 0;
  46. value = os_strchr(cmd, ' ');
  47. if (value == NULL)
  48. return -1;
  49. *value++ = '\0';
  50. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  51. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  52. eapol_sm_configure(wpa_s->eapol,
  53. atoi(value), -1, -1, -1);
  54. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  55. eapol_sm_configure(wpa_s->eapol,
  56. -1, atoi(value), -1, -1);
  57. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  58. eapol_sm_configure(wpa_s->eapol,
  59. -1, -1, atoi(value), -1);
  60. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  61. eapol_sm_configure(wpa_s->eapol,
  62. -1, -1, -1, atoi(value));
  63. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  64. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  65. atoi(value)))
  66. ret = -1;
  67. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  68. 0) {
  69. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  70. atoi(value)))
  71. ret = -1;
  72. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  73. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  74. ret = -1;
  75. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  76. wpa_s->wps_fragment_size = atoi(value);
  77. } else {
  78. value[-1] = '=';
  79. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  80. if (ret == 0)
  81. wpa_supplicant_update_config(wpa_s);
  82. }
  83. return ret;
  84. }
  85. #ifdef IEEE8021X_EAPOL
  86. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  87. char *addr)
  88. {
  89. u8 bssid[ETH_ALEN];
  90. struct wpa_ssid *ssid = wpa_s->current_ssid;
  91. if (hwaddr_aton(addr, bssid)) {
  92. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  93. "'%s'", addr);
  94. return -1;
  95. }
  96. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  97. rsn_preauth_deinit(wpa_s->wpa);
  98. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  99. return -1;
  100. return 0;
  101. }
  102. #endif /* IEEE8021X_EAPOL */
  103. #ifdef CONFIG_PEERKEY
  104. /* MLME-STKSTART.request(peer) */
  105. static int wpa_supplicant_ctrl_iface_stkstart(
  106. struct wpa_supplicant *wpa_s, char *addr)
  107. {
  108. u8 peer[ETH_ALEN];
  109. if (hwaddr_aton(addr, peer)) {
  110. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  111. "address '%s'", addr);
  112. return -1;
  113. }
  114. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  115. MAC2STR(peer));
  116. return wpa_sm_stkstart(wpa_s->wpa, peer);
  117. }
  118. #endif /* CONFIG_PEERKEY */
  119. #ifdef CONFIG_IEEE80211R
  120. static int wpa_supplicant_ctrl_iface_ft_ds(
  121. struct wpa_supplicant *wpa_s, char *addr)
  122. {
  123. u8 target_ap[ETH_ALEN];
  124. struct wpa_bss *bss;
  125. const u8 *mdie;
  126. if (hwaddr_aton(addr, target_ap)) {
  127. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  128. "address '%s'", addr);
  129. return -1;
  130. }
  131. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  132. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  133. if (bss)
  134. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  135. else
  136. mdie = NULL;
  137. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  138. }
  139. #endif /* CONFIG_IEEE80211R */
  140. #ifdef CONFIG_WPS
  141. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  142. char *cmd)
  143. {
  144. u8 bssid[ETH_ALEN], *_bssid = bssid;
  145. if (cmd == NULL || os_strcmp(cmd, "any") == 0)
  146. _bssid = NULL;
  147. else if (hwaddr_aton(cmd, bssid)) {
  148. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  149. cmd);
  150. return -1;
  151. }
  152. #ifdef CONFIG_AP
  153. if (wpa_s->ap_iface)
  154. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid);
  155. #endif /* CONFIG_AP */
  156. return wpas_wps_start_pbc(wpa_s, _bssid);
  157. }
  158. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  159. char *cmd, char *buf,
  160. size_t buflen)
  161. {
  162. u8 bssid[ETH_ALEN], *_bssid = bssid;
  163. char *pin;
  164. int ret;
  165. pin = os_strchr(cmd, ' ');
  166. if (pin)
  167. *pin++ = '\0';
  168. if (os_strcmp(cmd, "any") == 0)
  169. _bssid = NULL;
  170. else if (hwaddr_aton(cmd, bssid)) {
  171. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  172. cmd);
  173. return -1;
  174. }
  175. #ifdef CONFIG_AP
  176. if (wpa_s->ap_iface)
  177. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  178. buf, buflen);
  179. #endif /* CONFIG_AP */
  180. if (pin) {
  181. ret = wpas_wps_start_pin(wpa_s, _bssid, pin);
  182. if (ret < 0)
  183. return -1;
  184. ret = os_snprintf(buf, buflen, "%s", pin);
  185. if (ret < 0 || (size_t) ret >= buflen)
  186. return -1;
  187. return ret;
  188. }
  189. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL);
  190. if (ret < 0)
  191. return -1;
  192. /* Return the generated PIN */
  193. ret = os_snprintf(buf, buflen, "%08d", ret);
  194. if (ret < 0 || (size_t) ret >= buflen)
  195. return -1;
  196. return ret;
  197. }
  198. #ifdef CONFIG_WPS_OOB
  199. static int wpa_supplicant_ctrl_iface_wps_oob(struct wpa_supplicant *wpa_s,
  200. char *cmd)
  201. {
  202. char *path, *method, *name;
  203. path = os_strchr(cmd, ' ');
  204. if (path == NULL)
  205. return -1;
  206. *path++ = '\0';
  207. method = os_strchr(path, ' ');
  208. if (method == NULL)
  209. return -1;
  210. *method++ = '\0';
  211. name = os_strchr(method, ' ');
  212. if (name != NULL)
  213. *name++ = '\0';
  214. return wpas_wps_start_oob(wpa_s, cmd, path, method, name);
  215. }
  216. #endif /* CONFIG_WPS_OOB */
  217. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  218. char *cmd)
  219. {
  220. u8 bssid[ETH_ALEN], *_bssid = bssid;
  221. char *pin;
  222. char *new_ssid;
  223. char *new_auth;
  224. char *new_encr;
  225. char *new_key;
  226. struct wps_new_ap_settings ap;
  227. pin = os_strchr(cmd, ' ');
  228. if (pin == NULL)
  229. return -1;
  230. *pin++ = '\0';
  231. if (os_strcmp(cmd, "any") == 0)
  232. _bssid = NULL;
  233. else if (hwaddr_aton(cmd, bssid)) {
  234. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  235. cmd);
  236. return -1;
  237. }
  238. new_ssid = os_strchr(pin, ' ');
  239. if (new_ssid == NULL)
  240. return wpas_wps_start_reg(wpa_s, _bssid, pin, NULL);
  241. *new_ssid++ = '\0';
  242. new_auth = os_strchr(new_ssid, ' ');
  243. if (new_auth == NULL)
  244. return -1;
  245. *new_auth++ = '\0';
  246. new_encr = os_strchr(new_auth, ' ');
  247. if (new_encr == NULL)
  248. return -1;
  249. *new_encr++ = '\0';
  250. new_key = os_strchr(new_encr, ' ');
  251. if (new_key == NULL)
  252. return -1;
  253. *new_key++ = '\0';
  254. os_memset(&ap, 0, sizeof(ap));
  255. ap.ssid_hex = new_ssid;
  256. ap.auth = new_auth;
  257. ap.encr = new_encr;
  258. ap.key_hex = new_key;
  259. return wpas_wps_start_reg(wpa_s, _bssid, pin, &ap);
  260. }
  261. #ifdef CONFIG_WPS_ER
  262. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  263. char *cmd)
  264. {
  265. char *uuid = cmd, *pin, *pos;
  266. u8 addr_buf[ETH_ALEN], *addr = NULL;
  267. pin = os_strchr(uuid, ' ');
  268. if (pin == NULL)
  269. return -1;
  270. *pin++ = '\0';
  271. pos = os_strchr(pin, ' ');
  272. if (pos) {
  273. *pos++ = '\0';
  274. if (hwaddr_aton(pos, addr_buf) == 0)
  275. addr = addr_buf;
  276. }
  277. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  278. }
  279. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  280. char *cmd)
  281. {
  282. char *uuid = cmd, *pin;
  283. pin = os_strchr(uuid, ' ');
  284. if (pin == NULL)
  285. return -1;
  286. *pin++ = '\0';
  287. return wpas_wps_er_learn(wpa_s, uuid, pin);
  288. }
  289. static int wpa_supplicant_ctrl_iface_wps_er_config(
  290. struct wpa_supplicant *wpa_s, char *cmd)
  291. {
  292. char *pin;
  293. char *new_ssid;
  294. char *new_auth;
  295. char *new_encr;
  296. char *new_key;
  297. struct wps_new_ap_settings ap;
  298. pin = os_strchr(cmd, ' ');
  299. if (pin == NULL)
  300. return -1;
  301. *pin++ = '\0';
  302. new_ssid = os_strchr(pin, ' ');
  303. if (new_ssid == NULL)
  304. return -1;
  305. *new_ssid++ = '\0';
  306. new_auth = os_strchr(new_ssid, ' ');
  307. if (new_auth == NULL)
  308. return -1;
  309. *new_auth++ = '\0';
  310. new_encr = os_strchr(new_auth, ' ');
  311. if (new_encr == NULL)
  312. return -1;
  313. *new_encr++ = '\0';
  314. new_key = os_strchr(new_encr, ' ');
  315. if (new_key == NULL)
  316. return -1;
  317. *new_key++ = '\0';
  318. os_memset(&ap, 0, sizeof(ap));
  319. ap.ssid_hex = new_ssid;
  320. ap.auth = new_auth;
  321. ap.encr = new_encr;
  322. ap.key_hex = new_key;
  323. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  324. }
  325. #endif /* CONFIG_WPS_ER */
  326. #endif /* CONFIG_WPS */
  327. #ifdef CONFIG_IBSS_RSN
  328. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  329. struct wpa_supplicant *wpa_s, char *addr)
  330. {
  331. u8 peer[ETH_ALEN];
  332. if (hwaddr_aton(addr, peer)) {
  333. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  334. "address '%s'", addr);
  335. return -1;
  336. }
  337. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  338. MAC2STR(peer));
  339. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  340. }
  341. #endif /* CONFIG_IBSS_RSN */
  342. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  343. char *rsp)
  344. {
  345. #ifdef IEEE8021X_EAPOL
  346. char *pos, *id_pos;
  347. int id;
  348. struct wpa_ssid *ssid;
  349. struct eap_peer_config *eap;
  350. pos = os_strchr(rsp, '-');
  351. if (pos == NULL)
  352. return -1;
  353. *pos++ = '\0';
  354. id_pos = pos;
  355. pos = os_strchr(pos, ':');
  356. if (pos == NULL)
  357. return -1;
  358. *pos++ = '\0';
  359. id = atoi(id_pos);
  360. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  361. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  362. (u8 *) pos, os_strlen(pos));
  363. ssid = wpa_config_get_network(wpa_s->conf, id);
  364. if (ssid == NULL) {
  365. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  366. "to update", id);
  367. return -1;
  368. }
  369. eap = &ssid->eap;
  370. if (os_strcmp(rsp, "IDENTITY") == 0) {
  371. os_free(eap->identity);
  372. eap->identity = (u8 *) os_strdup(pos);
  373. eap->identity_len = os_strlen(pos);
  374. eap->pending_req_identity = 0;
  375. if (ssid == wpa_s->current_ssid)
  376. wpa_s->reassociate = 1;
  377. } else if (os_strcmp(rsp, "PASSWORD") == 0) {
  378. os_free(eap->password);
  379. eap->password = (u8 *) os_strdup(pos);
  380. eap->password_len = os_strlen(pos);
  381. eap->pending_req_password = 0;
  382. if (ssid == wpa_s->current_ssid)
  383. wpa_s->reassociate = 1;
  384. } else if (os_strcmp(rsp, "NEW_PASSWORD") == 0) {
  385. os_free(eap->new_password);
  386. eap->new_password = (u8 *) os_strdup(pos);
  387. eap->new_password_len = os_strlen(pos);
  388. eap->pending_req_new_password = 0;
  389. if (ssid == wpa_s->current_ssid)
  390. wpa_s->reassociate = 1;
  391. } else if (os_strcmp(rsp, "PIN") == 0) {
  392. os_free(eap->pin);
  393. eap->pin = os_strdup(pos);
  394. eap->pending_req_pin = 0;
  395. if (ssid == wpa_s->current_ssid)
  396. wpa_s->reassociate = 1;
  397. } else if (os_strcmp(rsp, "OTP") == 0) {
  398. os_free(eap->otp);
  399. eap->otp = (u8 *) os_strdup(pos);
  400. eap->otp_len = os_strlen(pos);
  401. os_free(eap->pending_req_otp);
  402. eap->pending_req_otp = NULL;
  403. eap->pending_req_otp_len = 0;
  404. } else if (os_strcmp(rsp, "PASSPHRASE") == 0) {
  405. os_free(eap->private_key_passwd);
  406. eap->private_key_passwd = (u8 *) os_strdup(pos);
  407. eap->pending_req_passphrase = 0;
  408. if (ssid == wpa_s->current_ssid)
  409. wpa_s->reassociate = 1;
  410. } else {
  411. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", rsp);
  412. return -1;
  413. }
  414. return 0;
  415. #else /* IEEE8021X_EAPOL */
  416. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  417. return -1;
  418. #endif /* IEEE8021X_EAPOL */
  419. }
  420. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  421. const char *params,
  422. char *buf, size_t buflen)
  423. {
  424. char *pos, *end, tmp[30];
  425. int res, verbose, ret;
  426. verbose = os_strcmp(params, "-VERBOSE") == 0;
  427. pos = buf;
  428. end = buf + buflen;
  429. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  430. struct wpa_ssid *ssid = wpa_s->current_ssid;
  431. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  432. MAC2STR(wpa_s->bssid));
  433. if (ret < 0 || ret >= end - pos)
  434. return pos - buf;
  435. pos += ret;
  436. if (ssid) {
  437. u8 *_ssid = ssid->ssid;
  438. size_t ssid_len = ssid->ssid_len;
  439. u8 ssid_buf[MAX_SSID_LEN];
  440. if (ssid_len == 0) {
  441. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  442. if (_res < 0)
  443. ssid_len = 0;
  444. else
  445. ssid_len = _res;
  446. _ssid = ssid_buf;
  447. }
  448. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  449. wpa_ssid_txt(_ssid, ssid_len),
  450. ssid->id);
  451. if (ret < 0 || ret >= end - pos)
  452. return pos - buf;
  453. pos += ret;
  454. if (ssid->id_str) {
  455. ret = os_snprintf(pos, end - pos,
  456. "id_str=%s\n",
  457. ssid->id_str);
  458. if (ret < 0 || ret >= end - pos)
  459. return pos - buf;
  460. pos += ret;
  461. }
  462. switch (ssid->mode) {
  463. case WPAS_MODE_INFRA:
  464. ret = os_snprintf(pos, end - pos,
  465. "mode=station\n");
  466. break;
  467. case WPAS_MODE_IBSS:
  468. ret = os_snprintf(pos, end - pos,
  469. "mode=IBSS\n");
  470. break;
  471. case WPAS_MODE_AP:
  472. ret = os_snprintf(pos, end - pos,
  473. "mode=AP\n");
  474. break;
  475. default:
  476. ret = 0;
  477. break;
  478. }
  479. if (ret < 0 || ret >= end - pos)
  480. return pos - buf;
  481. pos += ret;
  482. }
  483. #ifdef CONFIG_AP
  484. if (wpa_s->ap_iface) {
  485. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  486. end - pos,
  487. verbose);
  488. } else
  489. #endif /* CONFIG_AP */
  490. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  491. }
  492. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  493. wpa_supplicant_state_txt(wpa_s->wpa_state));
  494. if (ret < 0 || ret >= end - pos)
  495. return pos - buf;
  496. pos += ret;
  497. if (wpa_s->l2 &&
  498. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  499. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  500. if (ret < 0 || ret >= end - pos)
  501. return pos - buf;
  502. pos += ret;
  503. }
  504. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  505. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  506. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  507. verbose);
  508. if (res >= 0)
  509. pos += res;
  510. }
  511. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  512. if (res >= 0)
  513. pos += res;
  514. return pos - buf;
  515. }
  516. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  517. char *cmd)
  518. {
  519. char *pos;
  520. int id;
  521. struct wpa_ssid *ssid;
  522. u8 bssid[ETH_ALEN];
  523. /* cmd: "<network id> <BSSID>" */
  524. pos = os_strchr(cmd, ' ');
  525. if (pos == NULL)
  526. return -1;
  527. *pos++ = '\0';
  528. id = atoi(cmd);
  529. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  530. if (hwaddr_aton(pos, bssid)) {
  531. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  532. return -1;
  533. }
  534. ssid = wpa_config_get_network(wpa_s->conf, id);
  535. if (ssid == NULL) {
  536. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  537. "to update", id);
  538. return -1;
  539. }
  540. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  541. ssid->bssid_set = !is_zero_ether_addr(bssid);
  542. return 0;
  543. }
  544. static int wpa_supplicant_ctrl_iface_list_networks(
  545. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  546. {
  547. char *pos, *end;
  548. struct wpa_ssid *ssid;
  549. int ret;
  550. pos = buf;
  551. end = buf + buflen;
  552. ret = os_snprintf(pos, end - pos,
  553. "network id / ssid / bssid / flags\n");
  554. if (ret < 0 || ret >= end - pos)
  555. return pos - buf;
  556. pos += ret;
  557. ssid = wpa_s->conf->ssid;
  558. while (ssid) {
  559. ret = os_snprintf(pos, end - pos, "%d\t%s",
  560. ssid->id,
  561. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  562. if (ret < 0 || ret >= end - pos)
  563. return pos - buf;
  564. pos += ret;
  565. if (ssid->bssid_set) {
  566. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  567. MAC2STR(ssid->bssid));
  568. } else {
  569. ret = os_snprintf(pos, end - pos, "\tany");
  570. }
  571. if (ret < 0 || ret >= end - pos)
  572. return pos - buf;
  573. pos += ret;
  574. ret = os_snprintf(pos, end - pos, "\t%s%s",
  575. ssid == wpa_s->current_ssid ?
  576. "[CURRENT]" : "",
  577. ssid->disabled ? "[DISABLED]" : "");
  578. if (ret < 0 || ret >= end - pos)
  579. return pos - buf;
  580. pos += ret;
  581. ret = os_snprintf(pos, end - pos, "\n");
  582. if (ret < 0 || ret >= end - pos)
  583. return pos - buf;
  584. pos += ret;
  585. ssid = ssid->next;
  586. }
  587. return pos - buf;
  588. }
  589. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  590. {
  591. int first = 1, ret;
  592. ret = os_snprintf(pos, end - pos, "-");
  593. if (ret < 0 || ret >= end - pos)
  594. return pos;
  595. pos += ret;
  596. if (cipher & WPA_CIPHER_NONE) {
  597. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
  598. if (ret < 0 || ret >= end - pos)
  599. return pos;
  600. pos += ret;
  601. first = 0;
  602. }
  603. if (cipher & WPA_CIPHER_WEP40) {
  604. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
  605. if (ret < 0 || ret >= end - pos)
  606. return pos;
  607. pos += ret;
  608. first = 0;
  609. }
  610. if (cipher & WPA_CIPHER_WEP104) {
  611. ret = os_snprintf(pos, end - pos, "%sWEP104",
  612. first ? "" : "+");
  613. if (ret < 0 || ret >= end - pos)
  614. return pos;
  615. pos += ret;
  616. first = 0;
  617. }
  618. if (cipher & WPA_CIPHER_TKIP) {
  619. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
  620. if (ret < 0 || ret >= end - pos)
  621. return pos;
  622. pos += ret;
  623. first = 0;
  624. }
  625. if (cipher & WPA_CIPHER_CCMP) {
  626. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
  627. if (ret < 0 || ret >= end - pos)
  628. return pos;
  629. pos += ret;
  630. first = 0;
  631. }
  632. return pos;
  633. }
  634. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  635. const u8 *ie, size_t ie_len)
  636. {
  637. struct wpa_ie_data data;
  638. int first, ret;
  639. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  640. if (ret < 0 || ret >= end - pos)
  641. return pos;
  642. pos += ret;
  643. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  644. ret = os_snprintf(pos, end - pos, "?]");
  645. if (ret < 0 || ret >= end - pos)
  646. return pos;
  647. pos += ret;
  648. return pos;
  649. }
  650. first = 1;
  651. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  652. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  653. if (ret < 0 || ret >= end - pos)
  654. return pos;
  655. pos += ret;
  656. first = 0;
  657. }
  658. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  659. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  660. if (ret < 0 || ret >= end - pos)
  661. return pos;
  662. pos += ret;
  663. first = 0;
  664. }
  665. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  666. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  667. if (ret < 0 || ret >= end - pos)
  668. return pos;
  669. pos += ret;
  670. first = 0;
  671. }
  672. #ifdef CONFIG_IEEE80211R
  673. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  674. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  675. first ? "" : "+");
  676. if (ret < 0 || ret >= end - pos)
  677. return pos;
  678. pos += ret;
  679. first = 0;
  680. }
  681. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  682. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  683. first ? "" : "+");
  684. if (ret < 0 || ret >= end - pos)
  685. return pos;
  686. pos += ret;
  687. first = 0;
  688. }
  689. #endif /* CONFIG_IEEE80211R */
  690. #ifdef CONFIG_IEEE80211W
  691. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  692. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  693. first ? "" : "+");
  694. if (ret < 0 || ret >= end - pos)
  695. return pos;
  696. pos += ret;
  697. first = 0;
  698. }
  699. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  700. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  701. first ? "" : "+");
  702. if (ret < 0 || ret >= end - pos)
  703. return pos;
  704. pos += ret;
  705. first = 0;
  706. }
  707. #endif /* CONFIG_IEEE80211W */
  708. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  709. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  710. ret = os_snprintf(pos, end - pos, "-preauth");
  711. if (ret < 0 || ret >= end - pos)
  712. return pos;
  713. pos += ret;
  714. }
  715. ret = os_snprintf(pos, end - pos, "]");
  716. if (ret < 0 || ret >= end - pos)
  717. return pos;
  718. pos += ret;
  719. return pos;
  720. }
  721. #ifdef CONFIG_WPS
  722. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  723. char *pos, char *end,
  724. struct wpabuf *wps_ie)
  725. {
  726. int ret;
  727. const char *txt;
  728. if (wps_ie == NULL)
  729. return pos;
  730. if (wps_is_selected_pbc_registrar(wps_ie))
  731. txt = "[WPS-PBC]";
  732. #ifdef CONFIG_WPS2
  733. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  734. txt = "[WPS-AUTH]";
  735. #endif /* CONFIG_WPS2 */
  736. else if (wps_is_selected_pin_registrar(wps_ie))
  737. txt = "[WPS-PIN]";
  738. else
  739. txt = "[WPS]";
  740. ret = os_snprintf(pos, end - pos, "%s", txt);
  741. if (ret >= 0 && ret < end - pos)
  742. pos += ret;
  743. wpabuf_free(wps_ie);
  744. return pos;
  745. }
  746. #endif /* CONFIG_WPS */
  747. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  748. char *pos, char *end,
  749. const struct wpa_bss *bss)
  750. {
  751. #ifdef CONFIG_WPS
  752. struct wpabuf *wps_ie;
  753. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  754. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  755. #else /* CONFIG_WPS */
  756. return pos;
  757. #endif /* CONFIG_WPS */
  758. }
  759. /* Format one result on one text line into a buffer. */
  760. static int wpa_supplicant_ctrl_iface_scan_result(
  761. struct wpa_supplicant *wpa_s,
  762. const struct wpa_bss *bss, char *buf, size_t buflen)
  763. {
  764. char *pos, *end;
  765. int ret;
  766. const u8 *ie, *ie2;
  767. pos = buf;
  768. end = buf + buflen;
  769. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  770. MAC2STR(bss->bssid), bss->freq, bss->level);
  771. if (ret < 0 || ret >= end - pos)
  772. return pos - buf;
  773. pos += ret;
  774. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  775. if (ie)
  776. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  777. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  778. if (ie2)
  779. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  780. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  781. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  782. ret = os_snprintf(pos, end - pos, "[WEP]");
  783. if (ret < 0 || ret >= end - pos)
  784. return pos - buf;
  785. pos += ret;
  786. }
  787. if (bss->caps & IEEE80211_CAP_IBSS) {
  788. ret = os_snprintf(pos, end - pos, "[IBSS]");
  789. if (ret < 0 || ret >= end - pos)
  790. return pos - buf;
  791. pos += ret;
  792. }
  793. if (bss->caps & IEEE80211_CAP_ESS) {
  794. ret = os_snprintf(pos, end - pos, "[ESS]");
  795. if (ret < 0 || ret >= end - pos)
  796. return pos - buf;
  797. pos += ret;
  798. }
  799. ret = os_snprintf(pos, end - pos, "\t%s",
  800. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  801. if (ret < 0 || ret >= end - pos)
  802. return pos - buf;
  803. pos += ret;
  804. ret = os_snprintf(pos, end - pos, "\n");
  805. if (ret < 0 || ret >= end - pos)
  806. return pos - buf;
  807. pos += ret;
  808. return pos - buf;
  809. }
  810. static int wpa_supplicant_ctrl_iface_scan_results(
  811. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  812. {
  813. char *pos, *end;
  814. struct wpa_bss *bss;
  815. int ret;
  816. pos = buf;
  817. end = buf + buflen;
  818. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  819. "flags / ssid\n");
  820. if (ret < 0 || ret >= end - pos)
  821. return pos - buf;
  822. pos += ret;
  823. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  824. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  825. end - pos);
  826. if (ret < 0 || ret >= end - pos)
  827. return pos - buf;
  828. pos += ret;
  829. }
  830. return pos - buf;
  831. }
  832. static int wpa_supplicant_ctrl_iface_select_network(
  833. struct wpa_supplicant *wpa_s, char *cmd)
  834. {
  835. int id;
  836. struct wpa_ssid *ssid;
  837. /* cmd: "<network id>" or "any" */
  838. if (os_strcmp(cmd, "any") == 0) {
  839. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  840. ssid = NULL;
  841. } else {
  842. id = atoi(cmd);
  843. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  844. ssid = wpa_config_get_network(wpa_s->conf, id);
  845. if (ssid == NULL) {
  846. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  847. "network id=%d", id);
  848. return -1;
  849. }
  850. }
  851. wpa_supplicant_select_network(wpa_s, ssid);
  852. return 0;
  853. }
  854. static int wpa_supplicant_ctrl_iface_enable_network(
  855. struct wpa_supplicant *wpa_s, char *cmd)
  856. {
  857. int id;
  858. struct wpa_ssid *ssid;
  859. /* cmd: "<network id>" or "all" */
  860. if (os_strcmp(cmd, "all") == 0) {
  861. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  862. ssid = NULL;
  863. } else {
  864. id = atoi(cmd);
  865. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  866. ssid = wpa_config_get_network(wpa_s->conf, id);
  867. if (ssid == NULL) {
  868. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  869. "network id=%d", id);
  870. return -1;
  871. }
  872. }
  873. wpa_supplicant_enable_network(wpa_s, ssid);
  874. return 0;
  875. }
  876. static int wpa_supplicant_ctrl_iface_disable_network(
  877. struct wpa_supplicant *wpa_s, char *cmd)
  878. {
  879. int id;
  880. struct wpa_ssid *ssid;
  881. /* cmd: "<network id>" or "all" */
  882. if (os_strcmp(cmd, "all") == 0) {
  883. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  884. ssid = NULL;
  885. } else {
  886. id = atoi(cmd);
  887. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  888. ssid = wpa_config_get_network(wpa_s->conf, id);
  889. if (ssid == NULL) {
  890. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  891. "network id=%d", id);
  892. return -1;
  893. }
  894. }
  895. wpa_supplicant_disable_network(wpa_s, ssid);
  896. return 0;
  897. }
  898. static int wpa_supplicant_ctrl_iface_add_network(
  899. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  900. {
  901. struct wpa_ssid *ssid;
  902. int ret;
  903. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  904. ssid = wpa_config_add_network(wpa_s->conf);
  905. if (ssid == NULL)
  906. return -1;
  907. wpas_notify_network_added(wpa_s, ssid);
  908. ssid->disabled = 1;
  909. wpa_config_set_network_defaults(ssid);
  910. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  911. if (ret < 0 || (size_t) ret >= buflen)
  912. return -1;
  913. return ret;
  914. }
  915. static int wpa_supplicant_ctrl_iface_remove_network(
  916. struct wpa_supplicant *wpa_s, char *cmd)
  917. {
  918. int id;
  919. struct wpa_ssid *ssid;
  920. /* cmd: "<network id>" or "all" */
  921. if (os_strcmp(cmd, "all") == 0) {
  922. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  923. ssid = wpa_s->conf->ssid;
  924. while (ssid) {
  925. struct wpa_ssid *remove_ssid = ssid;
  926. id = ssid->id;
  927. ssid = ssid->next;
  928. wpas_notify_network_removed(wpa_s, remove_ssid);
  929. wpa_config_remove_network(wpa_s->conf, id);
  930. }
  931. if (wpa_s->current_ssid) {
  932. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  933. wpa_supplicant_disassociate(wpa_s,
  934. WLAN_REASON_DEAUTH_LEAVING);
  935. }
  936. return 0;
  937. }
  938. id = atoi(cmd);
  939. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  940. ssid = wpa_config_get_network(wpa_s->conf, id);
  941. if (ssid == NULL ||
  942. wpa_config_remove_network(wpa_s->conf, id) < 0) {
  943. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  944. "id=%d", id);
  945. return -1;
  946. }
  947. if (ssid == wpa_s->current_ssid) {
  948. /*
  949. * Invalidate the EAP session cache if the current network is
  950. * removed.
  951. */
  952. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  953. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  954. }
  955. return 0;
  956. }
  957. static int wpa_supplicant_ctrl_iface_set_network(
  958. struct wpa_supplicant *wpa_s, char *cmd)
  959. {
  960. int id;
  961. struct wpa_ssid *ssid;
  962. char *name, *value;
  963. /* cmd: "<network id> <variable name> <value>" */
  964. name = os_strchr(cmd, ' ');
  965. if (name == NULL)
  966. return -1;
  967. *name++ = '\0';
  968. value = os_strchr(name, ' ');
  969. if (value == NULL)
  970. return -1;
  971. *value++ = '\0';
  972. id = atoi(cmd);
  973. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  974. id, name);
  975. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  976. (u8 *) value, os_strlen(value));
  977. ssid = wpa_config_get_network(wpa_s->conf, id);
  978. if (ssid == NULL) {
  979. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  980. "id=%d", id);
  981. return -1;
  982. }
  983. if (wpa_config_set(ssid, name, value, 0) < 0) {
  984. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  985. "variable '%s'", name);
  986. return -1;
  987. }
  988. if (wpa_s->current_ssid == ssid) {
  989. /*
  990. * Invalidate the EAP session cache if anything in the current
  991. * configuration changes.
  992. */
  993. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  994. }
  995. if ((os_strcmp(name, "psk") == 0 &&
  996. value[0] == '"' && ssid->ssid_len) ||
  997. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  998. wpa_config_update_psk(ssid);
  999. else if (os_strcmp(name, "priority") == 0)
  1000. wpa_config_update_prio_list(wpa_s->conf);
  1001. return 0;
  1002. }
  1003. static int wpa_supplicant_ctrl_iface_get_network(
  1004. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1005. {
  1006. int id;
  1007. size_t res;
  1008. struct wpa_ssid *ssid;
  1009. char *name, *value;
  1010. /* cmd: "<network id> <variable name>" */
  1011. name = os_strchr(cmd, ' ');
  1012. if (name == NULL || buflen == 0)
  1013. return -1;
  1014. *name++ = '\0';
  1015. id = atoi(cmd);
  1016. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  1017. id, name);
  1018. ssid = wpa_config_get_network(wpa_s->conf, id);
  1019. if (ssid == NULL) {
  1020. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1021. "id=%d", id);
  1022. return -1;
  1023. }
  1024. value = wpa_config_get_no_key(ssid, name);
  1025. if (value == NULL) {
  1026. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  1027. "variable '%s'", name);
  1028. return -1;
  1029. }
  1030. res = os_strlcpy(buf, value, buflen);
  1031. if (res >= buflen) {
  1032. os_free(value);
  1033. return -1;
  1034. }
  1035. os_free(value);
  1036. return res;
  1037. }
  1038. #ifndef CONFIG_NO_CONFIG_WRITE
  1039. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  1040. {
  1041. int ret;
  1042. if (!wpa_s->conf->update_config) {
  1043. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  1044. "to update configuration (update_config=0)");
  1045. return -1;
  1046. }
  1047. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  1048. if (ret) {
  1049. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  1050. "update configuration");
  1051. } else {
  1052. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  1053. " updated");
  1054. }
  1055. return ret;
  1056. }
  1057. #endif /* CONFIG_NO_CONFIG_WRITE */
  1058. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  1059. struct wpa_driver_capa *capa,
  1060. char *buf, size_t buflen)
  1061. {
  1062. int ret, first = 1;
  1063. char *pos, *end;
  1064. size_t len;
  1065. pos = buf;
  1066. end = pos + buflen;
  1067. if (res < 0) {
  1068. if (strict)
  1069. return 0;
  1070. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  1071. if (len >= buflen)
  1072. return -1;
  1073. return len;
  1074. }
  1075. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1076. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1077. if (ret < 0 || ret >= end - pos)
  1078. return pos - buf;
  1079. pos += ret;
  1080. first = 0;
  1081. }
  1082. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1083. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1084. if (ret < 0 || ret >= end - pos)
  1085. return pos - buf;
  1086. pos += ret;
  1087. first = 0;
  1088. }
  1089. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1090. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  1091. if (ret < 0 || ret >= end - pos)
  1092. return pos - buf;
  1093. pos += ret;
  1094. first = 0;
  1095. }
  1096. return pos - buf;
  1097. }
  1098. static int ctrl_iface_get_capability_group(int res, char *strict,
  1099. struct wpa_driver_capa *capa,
  1100. char *buf, size_t buflen)
  1101. {
  1102. int ret, first = 1;
  1103. char *pos, *end;
  1104. size_t len;
  1105. pos = buf;
  1106. end = pos + buflen;
  1107. if (res < 0) {
  1108. if (strict)
  1109. return 0;
  1110. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  1111. if (len >= buflen)
  1112. return -1;
  1113. return len;
  1114. }
  1115. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1116. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1117. if (ret < 0 || ret >= end - pos)
  1118. return pos - buf;
  1119. pos += ret;
  1120. first = 0;
  1121. }
  1122. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1123. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1124. if (ret < 0 || ret >= end - pos)
  1125. return pos - buf;
  1126. pos += ret;
  1127. first = 0;
  1128. }
  1129. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  1130. ret = os_snprintf(pos, end - pos, "%sWEP104",
  1131. 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_WEP40) {
  1138. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  1139. if (ret < 0 || ret >= end - pos)
  1140. return pos - buf;
  1141. pos += ret;
  1142. first = 0;
  1143. }
  1144. return pos - buf;
  1145. }
  1146. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  1147. struct wpa_driver_capa *capa,
  1148. char *buf, size_t buflen)
  1149. {
  1150. int ret;
  1151. char *pos, *end;
  1152. size_t len;
  1153. pos = buf;
  1154. end = pos + buflen;
  1155. if (res < 0) {
  1156. if (strict)
  1157. return 0;
  1158. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  1159. "NONE", buflen);
  1160. if (len >= buflen)
  1161. return -1;
  1162. return len;
  1163. }
  1164. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  1165. if (ret < 0 || ret >= end - pos)
  1166. return pos - buf;
  1167. pos += ret;
  1168. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1169. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  1170. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  1171. if (ret < 0 || ret >= end - pos)
  1172. return pos - buf;
  1173. pos += ret;
  1174. }
  1175. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1176. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1177. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  1178. if (ret < 0 || ret >= end - pos)
  1179. return pos - buf;
  1180. pos += ret;
  1181. }
  1182. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1183. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  1184. if (ret < 0 || ret >= end - pos)
  1185. return pos - buf;
  1186. pos += ret;
  1187. }
  1188. return pos - buf;
  1189. }
  1190. static int ctrl_iface_get_capability_proto(int res, char *strict,
  1191. struct wpa_driver_capa *capa,
  1192. char *buf, size_t buflen)
  1193. {
  1194. int ret, first = 1;
  1195. char *pos, *end;
  1196. size_t len;
  1197. pos = buf;
  1198. end = pos + buflen;
  1199. if (res < 0) {
  1200. if (strict)
  1201. return 0;
  1202. len = os_strlcpy(buf, "RSN WPA", buflen);
  1203. if (len >= buflen)
  1204. return -1;
  1205. return len;
  1206. }
  1207. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1208. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1209. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  1210. if (ret < 0 || ret >= end - pos)
  1211. return pos - buf;
  1212. pos += ret;
  1213. first = 0;
  1214. }
  1215. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1216. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  1217. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  1218. if (ret < 0 || ret >= end - pos)
  1219. return pos - buf;
  1220. pos += ret;
  1221. first = 0;
  1222. }
  1223. return pos - buf;
  1224. }
  1225. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  1226. struct wpa_driver_capa *capa,
  1227. char *buf, size_t buflen)
  1228. {
  1229. int ret, first = 1;
  1230. char *pos, *end;
  1231. size_t len;
  1232. pos = buf;
  1233. end = pos + buflen;
  1234. if (res < 0) {
  1235. if (strict)
  1236. return 0;
  1237. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  1238. if (len >= buflen)
  1239. return -1;
  1240. return len;
  1241. }
  1242. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  1243. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  1244. if (ret < 0 || ret >= end - pos)
  1245. return pos - buf;
  1246. pos += ret;
  1247. first = 0;
  1248. }
  1249. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  1250. ret = os_snprintf(pos, end - pos, "%sSHARED",
  1251. first ? "" : " ");
  1252. if (ret < 0 || ret >= end - pos)
  1253. return pos - buf;
  1254. pos += ret;
  1255. first = 0;
  1256. }
  1257. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  1258. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  1259. if (ret < 0 || ret >= end - pos)
  1260. return pos - buf;
  1261. pos += ret;
  1262. first = 0;
  1263. }
  1264. return pos - buf;
  1265. }
  1266. static int wpa_supplicant_ctrl_iface_get_capability(
  1267. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  1268. size_t buflen)
  1269. {
  1270. struct wpa_driver_capa capa;
  1271. int res;
  1272. char *strict;
  1273. char field[30];
  1274. size_t len;
  1275. /* Determine whether or not strict checking was requested */
  1276. len = os_strlcpy(field, _field, sizeof(field));
  1277. if (len >= sizeof(field))
  1278. return -1;
  1279. strict = os_strchr(field, ' ');
  1280. if (strict != NULL) {
  1281. *strict++ = '\0';
  1282. if (os_strcmp(strict, "strict") != 0)
  1283. return -1;
  1284. }
  1285. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  1286. field, strict ? strict : "");
  1287. if (os_strcmp(field, "eap") == 0) {
  1288. return eap_get_names(buf, buflen);
  1289. }
  1290. res = wpa_drv_get_capa(wpa_s, &capa);
  1291. if (os_strcmp(field, "pairwise") == 0)
  1292. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  1293. buf, buflen);
  1294. if (os_strcmp(field, "group") == 0)
  1295. return ctrl_iface_get_capability_group(res, strict, &capa,
  1296. buf, buflen);
  1297. if (os_strcmp(field, "key_mgmt") == 0)
  1298. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  1299. buf, buflen);
  1300. if (os_strcmp(field, "proto") == 0)
  1301. return ctrl_iface_get_capability_proto(res, strict, &capa,
  1302. buf, buflen);
  1303. if (os_strcmp(field, "auth_alg") == 0)
  1304. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  1305. buf, buflen);
  1306. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  1307. field);
  1308. return -1;
  1309. }
  1310. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  1311. const char *cmd, char *buf,
  1312. size_t buflen)
  1313. {
  1314. u8 bssid[ETH_ALEN];
  1315. size_t i;
  1316. struct wpa_bss *bss;
  1317. int ret;
  1318. char *pos, *end;
  1319. const u8 *ie, *ie2;
  1320. if (os_strcmp(cmd, "FIRST") == 0)
  1321. bss = dl_list_first(&wpa_s->bss, struct wpa_bss, list);
  1322. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  1323. i = atoi(cmd + 3);
  1324. bss = wpa_bss_get_id(wpa_s, i);
  1325. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  1326. i = atoi(cmd + 5);
  1327. bss = wpa_bss_get_id(wpa_s, i);
  1328. if (bss) {
  1329. struct dl_list *next = bss->list_id.next;
  1330. if (next == &wpa_s->bss_id)
  1331. bss = NULL;
  1332. else
  1333. bss = dl_list_entry(next, struct wpa_bss,
  1334. list_id);
  1335. }
  1336. } else if (hwaddr_aton(cmd, bssid) == 0)
  1337. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1338. else {
  1339. struct wpa_bss *tmp;
  1340. i = atoi(cmd);
  1341. bss = NULL;
  1342. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  1343. {
  1344. if (i-- == 0) {
  1345. bss = tmp;
  1346. break;
  1347. }
  1348. }
  1349. }
  1350. if (bss == NULL)
  1351. return 0;
  1352. pos = buf;
  1353. end = buf + buflen;
  1354. ret = os_snprintf(pos, end - pos,
  1355. "id=%u\n"
  1356. "bssid=" MACSTR "\n"
  1357. "freq=%d\n"
  1358. "beacon_int=%d\n"
  1359. "capabilities=0x%04x\n"
  1360. "qual=%d\n"
  1361. "noise=%d\n"
  1362. "level=%d\n"
  1363. "tsf=%016llu\n"
  1364. "ie=",
  1365. bss->id,
  1366. MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
  1367. bss->caps, bss->qual, bss->noise, bss->level,
  1368. (unsigned long long) bss->tsf);
  1369. if (ret < 0 || ret >= end - pos)
  1370. return pos - buf;
  1371. pos += ret;
  1372. ie = (const u8 *) (bss + 1);
  1373. for (i = 0; i < bss->ie_len; i++) {
  1374. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  1375. if (ret < 0 || ret >= end - pos)
  1376. return pos - buf;
  1377. pos += ret;
  1378. }
  1379. ret = os_snprintf(pos, end - pos, "\n");
  1380. if (ret < 0 || ret >= end - pos)
  1381. return pos - buf;
  1382. pos += ret;
  1383. ret = os_snprintf(pos, end - pos, "flags=");
  1384. if (ret < 0 || ret >= end - pos)
  1385. return pos - buf;
  1386. pos += ret;
  1387. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1388. if (ie)
  1389. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1390. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1391. if (ie2)
  1392. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1393. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1394. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1395. ret = os_snprintf(pos, end - pos, "[WEP]");
  1396. if (ret < 0 || ret >= end - pos)
  1397. return pos - buf;
  1398. pos += ret;
  1399. }
  1400. if (bss->caps & IEEE80211_CAP_IBSS) {
  1401. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1402. if (ret < 0 || ret >= end - pos)
  1403. return pos - buf;
  1404. pos += ret;
  1405. }
  1406. if (bss->caps & IEEE80211_CAP_ESS) {
  1407. ret = os_snprintf(pos, end - pos, "[ESS]");
  1408. if (ret < 0 || ret >= end - pos)
  1409. return pos - buf;
  1410. pos += ret;
  1411. }
  1412. ret = os_snprintf(pos, end - pos, "\n");
  1413. if (ret < 0 || ret >= end - pos)
  1414. return pos - buf;
  1415. pos += ret;
  1416. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  1417. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1418. if (ret < 0 || ret >= end - pos)
  1419. return pos - buf;
  1420. pos += ret;
  1421. #ifdef CONFIG_WPS
  1422. ie = (const u8 *) (bss + 1);
  1423. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  1424. if (ret < 0 || ret >= end - pos)
  1425. return pos - buf;
  1426. pos += ret;
  1427. #endif /* CONFIG_WPS */
  1428. return pos - buf;
  1429. }
  1430. static int wpa_supplicant_ctrl_iface_ap_scan(
  1431. struct wpa_supplicant *wpa_s, char *cmd)
  1432. {
  1433. int ap_scan = atoi(cmd);
  1434. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  1435. }
  1436. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  1437. {
  1438. u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
  1439. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  1440. /* MLME-DELETEKEYS.request */
  1441. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
  1442. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
  1443. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
  1444. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
  1445. #ifdef CONFIG_IEEE80211W
  1446. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
  1447. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
  1448. #endif /* CONFIG_IEEE80211W */
  1449. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  1450. 0);
  1451. /* MLME-SETPROTECTION.request(None) */
  1452. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  1453. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  1454. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  1455. wpa_sm_drop_sa(wpa_s->wpa);
  1456. }
  1457. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  1458. char *addr)
  1459. {
  1460. u8 bssid[ETH_ALEN];
  1461. struct wpa_bss *bss;
  1462. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1463. if (hwaddr_aton(addr, bssid)) {
  1464. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  1465. "address '%s'", addr);
  1466. return -1;
  1467. }
  1468. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  1469. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1470. if (!bss) {
  1471. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  1472. "from BSS table");
  1473. return -1;
  1474. }
  1475. /*
  1476. * TODO: Find best network configuration block from configuration to
  1477. * allow roaming to other networks
  1478. */
  1479. if (!ssid) {
  1480. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  1481. "configuration known for the target AP");
  1482. return -1;
  1483. }
  1484. wpa_s->reassociate = 1;
  1485. wpa_supplicant_connect(wpa_s, bss, ssid);
  1486. return 0;
  1487. }
  1488. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  1489. char *buf, size_t *resp_len)
  1490. {
  1491. char *reply;
  1492. const int reply_size = 2048;
  1493. int ctrl_rsp = 0;
  1494. int reply_len;
  1495. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  1496. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  1497. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  1498. (const u8 *) buf, os_strlen(buf));
  1499. } else {
  1500. wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface",
  1501. (const u8 *) buf, os_strlen(buf));
  1502. }
  1503. reply = os_malloc(reply_size);
  1504. if (reply == NULL) {
  1505. *resp_len = 1;
  1506. return NULL;
  1507. }
  1508. os_memcpy(reply, "OK\n", 3);
  1509. reply_len = 3;
  1510. if (os_strcmp(buf, "PING") == 0) {
  1511. os_memcpy(reply, "PONG\n", 5);
  1512. reply_len = 5;
  1513. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  1514. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  1515. } else if (os_strcmp(buf, "MIB") == 0) {
  1516. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  1517. if (reply_len >= 0) {
  1518. int res;
  1519. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  1520. reply_size - reply_len);
  1521. if (res < 0)
  1522. reply_len = -1;
  1523. else
  1524. reply_len += res;
  1525. }
  1526. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  1527. reply_len = wpa_supplicant_ctrl_iface_status(
  1528. wpa_s, buf + 6, reply, reply_size);
  1529. } else if (os_strcmp(buf, "PMKSA") == 0) {
  1530. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  1531. reply_size);
  1532. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1533. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  1534. reply_len = -1;
  1535. } else if (os_strcmp(buf, "LOGON") == 0) {
  1536. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  1537. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  1538. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  1539. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  1540. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  1541. reply_len = -1;
  1542. else {
  1543. wpa_s->disconnected = 0;
  1544. wpa_s->reassociate = 1;
  1545. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1546. }
  1547. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  1548. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  1549. reply_len = -1;
  1550. else if (wpa_s->disconnected) {
  1551. wpa_s->disconnected = 0;
  1552. wpa_s->reassociate = 1;
  1553. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1554. }
  1555. #ifdef IEEE8021X_EAPOL
  1556. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  1557. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  1558. reply_len = -1;
  1559. #endif /* IEEE8021X_EAPOL */
  1560. #ifdef CONFIG_PEERKEY
  1561. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  1562. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  1563. reply_len = -1;
  1564. #endif /* CONFIG_PEERKEY */
  1565. #ifdef CONFIG_IEEE80211R
  1566. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  1567. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  1568. reply_len = -1;
  1569. #endif /* CONFIG_IEEE80211R */
  1570. #ifdef CONFIG_WPS
  1571. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  1572. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL))
  1573. reply_len = -1;
  1574. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  1575. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8))
  1576. reply_len = -1;
  1577. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  1578. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  1579. reply,
  1580. reply_size);
  1581. #ifdef CONFIG_WPS_OOB
  1582. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  1583. if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
  1584. reply_len = -1;
  1585. #endif /* CONFIG_WPS_OOB */
  1586. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  1587. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  1588. reply_len = -1;
  1589. #ifdef CONFIG_WPS_ER
  1590. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  1591. if (wpas_wps_er_start(wpa_s, NULL))
  1592. reply_len = -1;
  1593. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  1594. if (wpas_wps_er_start(wpa_s, buf + 13))
  1595. reply_len = -1;
  1596. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  1597. if (wpas_wps_er_stop(wpa_s))
  1598. reply_len = -1;
  1599. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  1600. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  1601. reply_len = -1;
  1602. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  1603. if (wpas_wps_er_pbc(wpa_s, buf + 11))
  1604. reply_len = -1;
  1605. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  1606. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  1607. reply_len = -1;
  1608. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  1609. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  1610. reply_len = -1;
  1611. #endif /* CONFIG_WPS_ER */
  1612. #endif /* CONFIG_WPS */
  1613. #ifdef CONFIG_IBSS_RSN
  1614. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  1615. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  1616. reply_len = -1;
  1617. #endif /* CONFIG_IBSS_RSN */
  1618. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  1619. {
  1620. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  1621. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  1622. reply_len = -1;
  1623. else
  1624. ctrl_rsp = 1;
  1625. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  1626. if (wpa_supplicant_reload_configuration(wpa_s))
  1627. reply_len = -1;
  1628. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  1629. wpa_supplicant_terminate_proc(wpa_s->global);
  1630. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  1631. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  1632. reply_len = -1;
  1633. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  1634. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  1635. wpa_s, reply, reply_size);
  1636. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  1637. wpa_s->reassociate = 0;
  1638. wpa_s->disconnected = 1;
  1639. wpa_supplicant_deauthenticate(wpa_s,
  1640. WLAN_REASON_DEAUTH_LEAVING);
  1641. } else if (os_strcmp(buf, "SCAN") == 0) {
  1642. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  1643. reply_len = -1;
  1644. else {
  1645. wpa_s->scan_req = 2;
  1646. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1647. }
  1648. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  1649. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  1650. wpa_s, reply, reply_size);
  1651. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  1652. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  1653. reply_len = -1;
  1654. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  1655. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  1656. reply_len = -1;
  1657. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  1658. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  1659. reply_len = -1;
  1660. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  1661. reply_len = wpa_supplicant_ctrl_iface_add_network(
  1662. wpa_s, reply, reply_size);
  1663. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  1664. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  1665. reply_len = -1;
  1666. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  1667. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  1668. reply_len = -1;
  1669. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  1670. reply_len = wpa_supplicant_ctrl_iface_get_network(
  1671. wpa_s, buf + 12, reply, reply_size);
  1672. #ifndef CONFIG_NO_CONFIG_WRITE
  1673. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  1674. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  1675. reply_len = -1;
  1676. #endif /* CONFIG_NO_CONFIG_WRITE */
  1677. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  1678. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  1679. wpa_s, buf + 15, reply, reply_size);
  1680. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  1681. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  1682. reply_len = -1;
  1683. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  1684. reply_len = wpa_supplicant_global_iface_list(
  1685. wpa_s->global, reply, reply_size);
  1686. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  1687. reply_len = wpa_supplicant_global_iface_interfaces(
  1688. wpa_s->global, reply, reply_size);
  1689. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  1690. reply_len = wpa_supplicant_ctrl_iface_bss(
  1691. wpa_s, buf + 4, reply, reply_size);
  1692. #ifdef CONFIG_AP
  1693. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  1694. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  1695. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  1696. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  1697. reply_size);
  1698. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  1699. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  1700. reply_size);
  1701. #endif /* CONFIG_AP */
  1702. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  1703. wpas_notify_suspend(wpa_s->global);
  1704. } else if (os_strcmp(buf, "RESUME") == 0) {
  1705. wpas_notify_resume(wpa_s->global);
  1706. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  1707. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  1708. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  1709. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  1710. reply_len = -1;
  1711. } else {
  1712. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1713. reply_len = 16;
  1714. }
  1715. if (reply_len < 0) {
  1716. os_memcpy(reply, "FAIL\n", 5);
  1717. reply_len = 5;
  1718. }
  1719. if (ctrl_rsp)
  1720. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  1721. *resp_len = reply_len;
  1722. return reply;
  1723. }
  1724. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  1725. char *cmd)
  1726. {
  1727. struct wpa_interface iface;
  1728. char *pos;
  1729. /*
  1730. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  1731. * TAB<bridge_ifname>
  1732. */
  1733. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  1734. os_memset(&iface, 0, sizeof(iface));
  1735. do {
  1736. iface.ifname = pos = cmd;
  1737. pos = os_strchr(pos, '\t');
  1738. if (pos)
  1739. *pos++ = '\0';
  1740. if (iface.ifname[0] == '\0')
  1741. return -1;
  1742. if (pos == NULL)
  1743. break;
  1744. iface.confname = pos;
  1745. pos = os_strchr(pos, '\t');
  1746. if (pos)
  1747. *pos++ = '\0';
  1748. if (iface.confname[0] == '\0')
  1749. iface.confname = NULL;
  1750. if (pos == NULL)
  1751. break;
  1752. iface.driver = pos;
  1753. pos = os_strchr(pos, '\t');
  1754. if (pos)
  1755. *pos++ = '\0';
  1756. if (iface.driver[0] == '\0')
  1757. iface.driver = NULL;
  1758. if (pos == NULL)
  1759. break;
  1760. iface.ctrl_interface = pos;
  1761. pos = os_strchr(pos, '\t');
  1762. if (pos)
  1763. *pos++ = '\0';
  1764. if (iface.ctrl_interface[0] == '\0')
  1765. iface.ctrl_interface = NULL;
  1766. if (pos == NULL)
  1767. break;
  1768. iface.driver_param = pos;
  1769. pos = os_strchr(pos, '\t');
  1770. if (pos)
  1771. *pos++ = '\0';
  1772. if (iface.driver_param[0] == '\0')
  1773. iface.driver_param = NULL;
  1774. if (pos == NULL)
  1775. break;
  1776. iface.bridge_ifname = pos;
  1777. pos = os_strchr(pos, '\t');
  1778. if (pos)
  1779. *pos++ = '\0';
  1780. if (iface.bridge_ifname[0] == '\0')
  1781. iface.bridge_ifname = NULL;
  1782. if (pos == NULL)
  1783. break;
  1784. } while (0);
  1785. if (wpa_supplicant_get_iface(global, iface.ifname))
  1786. return -1;
  1787. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  1788. }
  1789. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  1790. char *cmd)
  1791. {
  1792. struct wpa_supplicant *wpa_s;
  1793. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  1794. wpa_s = wpa_supplicant_get_iface(global, cmd);
  1795. if (wpa_s == NULL)
  1796. return -1;
  1797. return wpa_supplicant_remove_iface(global, wpa_s);
  1798. }
  1799. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  1800. {
  1801. struct wpa_interface_info *prev;
  1802. while (iface) {
  1803. prev = iface;
  1804. iface = iface->next;
  1805. os_free(prev->ifname);
  1806. os_free(prev->desc);
  1807. os_free(prev);
  1808. }
  1809. }
  1810. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  1811. char *buf, int len)
  1812. {
  1813. int i, res;
  1814. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  1815. char *pos, *end;
  1816. for (i = 0; wpa_drivers[i]; i++) {
  1817. struct wpa_driver_ops *drv = wpa_drivers[i];
  1818. if (drv->get_interfaces == NULL)
  1819. continue;
  1820. tmp = drv->get_interfaces(global->drv_priv[i]);
  1821. if (tmp == NULL)
  1822. continue;
  1823. if (last == NULL)
  1824. iface = last = tmp;
  1825. else
  1826. last->next = tmp;
  1827. while (last->next)
  1828. last = last->next;
  1829. }
  1830. pos = buf;
  1831. end = buf + len;
  1832. for (tmp = iface; tmp; tmp = tmp->next) {
  1833. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  1834. tmp->drv_name, tmp->ifname,
  1835. tmp->desc ? tmp->desc : "");
  1836. if (res < 0 || res >= end - pos) {
  1837. *pos = '\0';
  1838. break;
  1839. }
  1840. pos += res;
  1841. }
  1842. wpa_free_iface_info(iface);
  1843. return pos - buf;
  1844. }
  1845. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  1846. char *buf, int len)
  1847. {
  1848. int res;
  1849. char *pos, *end;
  1850. struct wpa_supplicant *wpa_s;
  1851. wpa_s = global->ifaces;
  1852. pos = buf;
  1853. end = buf + len;
  1854. while (wpa_s) {
  1855. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  1856. if (res < 0 || res >= end - pos) {
  1857. *pos = '\0';
  1858. break;
  1859. }
  1860. pos += res;
  1861. wpa_s = wpa_s->next;
  1862. }
  1863. return pos - buf;
  1864. }
  1865. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  1866. char *buf, size_t *resp_len)
  1867. {
  1868. char *reply;
  1869. const int reply_size = 2048;
  1870. int reply_len;
  1871. wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
  1872. (const u8 *) buf, os_strlen(buf));
  1873. reply = os_malloc(reply_size);
  1874. if (reply == NULL) {
  1875. *resp_len = 1;
  1876. return NULL;
  1877. }
  1878. os_memcpy(reply, "OK\n", 3);
  1879. reply_len = 3;
  1880. if (os_strcmp(buf, "PING") == 0) {
  1881. os_memcpy(reply, "PONG\n", 5);
  1882. reply_len = 5;
  1883. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  1884. if (wpa_supplicant_global_iface_add(global, buf + 14))
  1885. reply_len = -1;
  1886. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  1887. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  1888. reply_len = -1;
  1889. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  1890. reply_len = wpa_supplicant_global_iface_list(
  1891. global, reply, reply_size);
  1892. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  1893. reply_len = wpa_supplicant_global_iface_interfaces(
  1894. global, reply, reply_size);
  1895. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  1896. wpa_supplicant_terminate_proc(global);
  1897. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  1898. wpas_notify_suspend(global);
  1899. } else if (os_strcmp(buf, "RESUME") == 0) {
  1900. wpas_notify_resume(global);
  1901. } else {
  1902. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1903. reply_len = 16;
  1904. }
  1905. if (reply_len < 0) {
  1906. os_memcpy(reply, "FAIL\n", 5);
  1907. reply_len = 5;
  1908. }
  1909. *resp_len = reply_len;
  1910. return reply;
  1911. }