ctrl_iface.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233
  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, 0);
  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, 0);
  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, 0);
  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. case WPAS_MODE_P2P_GO:
  476. ret = os_snprintf(pos, end - pos,
  477. "mode=P2P GO\n");
  478. break;
  479. case WPAS_MODE_P2P_GROUP_FORMATION:
  480. ret = os_snprintf(pos, end - pos,
  481. "mode=P2P GO - group "
  482. "formation\n");
  483. break;
  484. default:
  485. ret = 0;
  486. break;
  487. }
  488. if (ret < 0 || ret >= end - pos)
  489. return pos - buf;
  490. pos += ret;
  491. }
  492. #ifdef CONFIG_AP
  493. if (wpa_s->ap_iface) {
  494. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  495. end - pos,
  496. verbose);
  497. } else
  498. #endif /* CONFIG_AP */
  499. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  500. }
  501. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  502. wpa_supplicant_state_txt(wpa_s->wpa_state));
  503. if (ret < 0 || ret >= end - pos)
  504. return pos - buf;
  505. pos += ret;
  506. if (wpa_s->l2 &&
  507. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  508. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  509. if (ret < 0 || ret >= end - pos)
  510. return pos - buf;
  511. pos += ret;
  512. }
  513. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  514. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  515. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  516. verbose);
  517. if (res >= 0)
  518. pos += res;
  519. }
  520. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  521. if (res >= 0)
  522. pos += res;
  523. return pos - buf;
  524. }
  525. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  526. char *cmd)
  527. {
  528. char *pos;
  529. int id;
  530. struct wpa_ssid *ssid;
  531. u8 bssid[ETH_ALEN];
  532. /* cmd: "<network id> <BSSID>" */
  533. pos = os_strchr(cmd, ' ');
  534. if (pos == NULL)
  535. return -1;
  536. *pos++ = '\0';
  537. id = atoi(cmd);
  538. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  539. if (hwaddr_aton(pos, bssid)) {
  540. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  541. return -1;
  542. }
  543. ssid = wpa_config_get_network(wpa_s->conf, id);
  544. if (ssid == NULL) {
  545. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  546. "to update", id);
  547. return -1;
  548. }
  549. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  550. ssid->bssid_set = !is_zero_ether_addr(bssid);
  551. return 0;
  552. }
  553. static int wpa_supplicant_ctrl_iface_list_networks(
  554. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  555. {
  556. char *pos, *end;
  557. struct wpa_ssid *ssid;
  558. int ret;
  559. pos = buf;
  560. end = buf + buflen;
  561. ret = os_snprintf(pos, end - pos,
  562. "network id / ssid / bssid / flags\n");
  563. if (ret < 0 || ret >= end - pos)
  564. return pos - buf;
  565. pos += ret;
  566. ssid = wpa_s->conf->ssid;
  567. while (ssid) {
  568. ret = os_snprintf(pos, end - pos, "%d\t%s",
  569. ssid->id,
  570. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  571. if (ret < 0 || ret >= end - pos)
  572. return pos - buf;
  573. pos += ret;
  574. if (ssid->bssid_set) {
  575. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  576. MAC2STR(ssid->bssid));
  577. } else {
  578. ret = os_snprintf(pos, end - pos, "\tany");
  579. }
  580. if (ret < 0 || ret >= end - pos)
  581. return pos - buf;
  582. pos += ret;
  583. ret = os_snprintf(pos, end - pos, "\t%s%s",
  584. ssid == wpa_s->current_ssid ?
  585. "[CURRENT]" : "",
  586. ssid->disabled ? "[DISABLED]" : "");
  587. if (ret < 0 || ret >= end - pos)
  588. return pos - buf;
  589. pos += ret;
  590. ret = os_snprintf(pos, end - pos, "\n");
  591. if (ret < 0 || ret >= end - pos)
  592. return pos - buf;
  593. pos += ret;
  594. ssid = ssid->next;
  595. }
  596. return pos - buf;
  597. }
  598. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  599. {
  600. int first = 1, ret;
  601. ret = os_snprintf(pos, end - pos, "-");
  602. if (ret < 0 || ret >= end - pos)
  603. return pos;
  604. pos += ret;
  605. if (cipher & WPA_CIPHER_NONE) {
  606. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
  607. if (ret < 0 || ret >= end - pos)
  608. return pos;
  609. pos += ret;
  610. first = 0;
  611. }
  612. if (cipher & WPA_CIPHER_WEP40) {
  613. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
  614. if (ret < 0 || ret >= end - pos)
  615. return pos;
  616. pos += ret;
  617. first = 0;
  618. }
  619. if (cipher & WPA_CIPHER_WEP104) {
  620. ret = os_snprintf(pos, end - pos, "%sWEP104",
  621. first ? "" : "+");
  622. if (ret < 0 || ret >= end - pos)
  623. return pos;
  624. pos += ret;
  625. first = 0;
  626. }
  627. if (cipher & WPA_CIPHER_TKIP) {
  628. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
  629. if (ret < 0 || ret >= end - pos)
  630. return pos;
  631. pos += ret;
  632. first = 0;
  633. }
  634. if (cipher & WPA_CIPHER_CCMP) {
  635. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
  636. if (ret < 0 || ret >= end - pos)
  637. return pos;
  638. pos += ret;
  639. first = 0;
  640. }
  641. return pos;
  642. }
  643. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  644. const u8 *ie, size_t ie_len)
  645. {
  646. struct wpa_ie_data data;
  647. int first, ret;
  648. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  649. if (ret < 0 || ret >= end - pos)
  650. return pos;
  651. pos += ret;
  652. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  653. ret = os_snprintf(pos, end - pos, "?]");
  654. if (ret < 0 || ret >= end - pos)
  655. return pos;
  656. pos += ret;
  657. return pos;
  658. }
  659. first = 1;
  660. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  661. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  662. if (ret < 0 || ret >= end - pos)
  663. return pos;
  664. pos += ret;
  665. first = 0;
  666. }
  667. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  668. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  669. if (ret < 0 || ret >= end - pos)
  670. return pos;
  671. pos += ret;
  672. first = 0;
  673. }
  674. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  675. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  676. if (ret < 0 || ret >= end - pos)
  677. return pos;
  678. pos += ret;
  679. first = 0;
  680. }
  681. #ifdef CONFIG_IEEE80211R
  682. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  683. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  684. first ? "" : "+");
  685. if (ret < 0 || ret >= end - pos)
  686. return pos;
  687. pos += ret;
  688. first = 0;
  689. }
  690. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  691. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  692. first ? "" : "+");
  693. if (ret < 0 || ret >= end - pos)
  694. return pos;
  695. pos += ret;
  696. first = 0;
  697. }
  698. #endif /* CONFIG_IEEE80211R */
  699. #ifdef CONFIG_IEEE80211W
  700. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  701. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  702. first ? "" : "+");
  703. if (ret < 0 || ret >= end - pos)
  704. return pos;
  705. pos += ret;
  706. first = 0;
  707. }
  708. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  709. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  710. first ? "" : "+");
  711. if (ret < 0 || ret >= end - pos)
  712. return pos;
  713. pos += ret;
  714. first = 0;
  715. }
  716. #endif /* CONFIG_IEEE80211W */
  717. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  718. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  719. ret = os_snprintf(pos, end - pos, "-preauth");
  720. if (ret < 0 || ret >= end - pos)
  721. return pos;
  722. pos += ret;
  723. }
  724. ret = os_snprintf(pos, end - pos, "]");
  725. if (ret < 0 || ret >= end - pos)
  726. return pos;
  727. pos += ret;
  728. return pos;
  729. }
  730. #ifdef CONFIG_WPS
  731. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  732. char *pos, char *end,
  733. struct wpabuf *wps_ie)
  734. {
  735. int ret;
  736. const char *txt;
  737. if (wps_ie == NULL)
  738. return pos;
  739. if (wps_is_selected_pbc_registrar(wps_ie))
  740. txt = "[WPS-PBC]";
  741. #ifdef CONFIG_WPS2
  742. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  743. txt = "[WPS-AUTH]";
  744. #endif /* CONFIG_WPS2 */
  745. else if (wps_is_selected_pin_registrar(wps_ie))
  746. txt = "[WPS-PIN]";
  747. else
  748. txt = "[WPS]";
  749. ret = os_snprintf(pos, end - pos, "%s", txt);
  750. if (ret >= 0 && ret < end - pos)
  751. pos += ret;
  752. wpabuf_free(wps_ie);
  753. return pos;
  754. }
  755. #endif /* CONFIG_WPS */
  756. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  757. char *pos, char *end,
  758. const struct wpa_bss *bss)
  759. {
  760. #ifdef CONFIG_WPS
  761. struct wpabuf *wps_ie;
  762. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  763. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  764. #else /* CONFIG_WPS */
  765. return pos;
  766. #endif /* CONFIG_WPS */
  767. }
  768. /* Format one result on one text line into a buffer. */
  769. static int wpa_supplicant_ctrl_iface_scan_result(
  770. struct wpa_supplicant *wpa_s,
  771. const struct wpa_bss *bss, char *buf, size_t buflen)
  772. {
  773. char *pos, *end;
  774. int ret;
  775. const u8 *ie, *ie2;
  776. pos = buf;
  777. end = buf + buflen;
  778. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  779. MAC2STR(bss->bssid), bss->freq, bss->level);
  780. if (ret < 0 || ret >= end - pos)
  781. return pos - buf;
  782. pos += ret;
  783. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  784. if (ie)
  785. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  786. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  787. if (ie2)
  788. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  789. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  790. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  791. ret = os_snprintf(pos, end - pos, "[WEP]");
  792. if (ret < 0 || ret >= end - pos)
  793. return pos - buf;
  794. pos += ret;
  795. }
  796. if (bss->caps & IEEE80211_CAP_IBSS) {
  797. ret = os_snprintf(pos, end - pos, "[IBSS]");
  798. if (ret < 0 || ret >= end - pos)
  799. return pos - buf;
  800. pos += ret;
  801. }
  802. if (bss->caps & IEEE80211_CAP_ESS) {
  803. ret = os_snprintf(pos, end - pos, "[ESS]");
  804. if (ret < 0 || ret >= end - pos)
  805. return pos - buf;
  806. pos += ret;
  807. }
  808. ret = os_snprintf(pos, end - pos, "\t%s",
  809. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  810. if (ret < 0 || ret >= end - pos)
  811. return pos - buf;
  812. pos += ret;
  813. ret = os_snprintf(pos, end - pos, "\n");
  814. if (ret < 0 || ret >= end - pos)
  815. return pos - buf;
  816. pos += ret;
  817. return pos - buf;
  818. }
  819. static int wpa_supplicant_ctrl_iface_scan_results(
  820. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  821. {
  822. char *pos, *end;
  823. struct wpa_bss *bss;
  824. int ret;
  825. pos = buf;
  826. end = buf + buflen;
  827. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  828. "flags / ssid\n");
  829. if (ret < 0 || ret >= end - pos)
  830. return pos - buf;
  831. pos += ret;
  832. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  833. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  834. end - pos);
  835. if (ret < 0 || ret >= end - pos)
  836. return pos - buf;
  837. pos += ret;
  838. }
  839. return pos - buf;
  840. }
  841. static int wpa_supplicant_ctrl_iface_select_network(
  842. struct wpa_supplicant *wpa_s, char *cmd)
  843. {
  844. int id;
  845. struct wpa_ssid *ssid;
  846. /* cmd: "<network id>" or "any" */
  847. if (os_strcmp(cmd, "any") == 0) {
  848. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  849. ssid = NULL;
  850. } else {
  851. id = atoi(cmd);
  852. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  853. ssid = wpa_config_get_network(wpa_s->conf, id);
  854. if (ssid == NULL) {
  855. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  856. "network id=%d", id);
  857. return -1;
  858. }
  859. }
  860. wpa_supplicant_select_network(wpa_s, ssid);
  861. return 0;
  862. }
  863. static int wpa_supplicant_ctrl_iface_enable_network(
  864. struct wpa_supplicant *wpa_s, char *cmd)
  865. {
  866. int id;
  867. struct wpa_ssid *ssid;
  868. /* cmd: "<network id>" or "all" */
  869. if (os_strcmp(cmd, "all") == 0) {
  870. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  871. ssid = NULL;
  872. } else {
  873. id = atoi(cmd);
  874. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  875. ssid = wpa_config_get_network(wpa_s->conf, id);
  876. if (ssid == NULL) {
  877. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  878. "network id=%d", id);
  879. return -1;
  880. }
  881. }
  882. wpa_supplicant_enable_network(wpa_s, ssid);
  883. return 0;
  884. }
  885. static int wpa_supplicant_ctrl_iface_disable_network(
  886. struct wpa_supplicant *wpa_s, char *cmd)
  887. {
  888. int id;
  889. struct wpa_ssid *ssid;
  890. /* cmd: "<network id>" or "all" */
  891. if (os_strcmp(cmd, "all") == 0) {
  892. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  893. ssid = NULL;
  894. } else {
  895. id = atoi(cmd);
  896. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  897. ssid = wpa_config_get_network(wpa_s->conf, id);
  898. if (ssid == NULL) {
  899. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  900. "network id=%d", id);
  901. return -1;
  902. }
  903. }
  904. wpa_supplicant_disable_network(wpa_s, ssid);
  905. return 0;
  906. }
  907. static int wpa_supplicant_ctrl_iface_add_network(
  908. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  909. {
  910. struct wpa_ssid *ssid;
  911. int ret;
  912. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  913. ssid = wpa_config_add_network(wpa_s->conf);
  914. if (ssid == NULL)
  915. return -1;
  916. wpas_notify_network_added(wpa_s, ssid);
  917. ssid->disabled = 1;
  918. wpa_config_set_network_defaults(ssid);
  919. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  920. if (ret < 0 || (size_t) ret >= buflen)
  921. return -1;
  922. return ret;
  923. }
  924. static int wpa_supplicant_ctrl_iface_remove_network(
  925. struct wpa_supplicant *wpa_s, char *cmd)
  926. {
  927. int id;
  928. struct wpa_ssid *ssid;
  929. /* cmd: "<network id>" or "all" */
  930. if (os_strcmp(cmd, "all") == 0) {
  931. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  932. ssid = wpa_s->conf->ssid;
  933. while (ssid) {
  934. struct wpa_ssid *remove_ssid = ssid;
  935. id = ssid->id;
  936. ssid = ssid->next;
  937. wpas_notify_network_removed(wpa_s, remove_ssid);
  938. wpa_config_remove_network(wpa_s->conf, id);
  939. }
  940. if (wpa_s->current_ssid) {
  941. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  942. wpa_supplicant_disassociate(wpa_s,
  943. WLAN_REASON_DEAUTH_LEAVING);
  944. }
  945. return 0;
  946. }
  947. id = atoi(cmd);
  948. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  949. ssid = wpa_config_get_network(wpa_s->conf, id);
  950. if (ssid == NULL ||
  951. wpa_config_remove_network(wpa_s->conf, id) < 0) {
  952. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  953. "id=%d", id);
  954. return -1;
  955. }
  956. if (ssid == wpa_s->current_ssid) {
  957. /*
  958. * Invalidate the EAP session cache if the current network is
  959. * removed.
  960. */
  961. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  962. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  963. }
  964. return 0;
  965. }
  966. static int wpa_supplicant_ctrl_iface_set_network(
  967. struct wpa_supplicant *wpa_s, char *cmd)
  968. {
  969. int id;
  970. struct wpa_ssid *ssid;
  971. char *name, *value;
  972. /* cmd: "<network id> <variable name> <value>" */
  973. name = os_strchr(cmd, ' ');
  974. if (name == NULL)
  975. return -1;
  976. *name++ = '\0';
  977. value = os_strchr(name, ' ');
  978. if (value == NULL)
  979. return -1;
  980. *value++ = '\0';
  981. id = atoi(cmd);
  982. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  983. id, name);
  984. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  985. (u8 *) value, os_strlen(value));
  986. ssid = wpa_config_get_network(wpa_s->conf, id);
  987. if (ssid == NULL) {
  988. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  989. "id=%d", id);
  990. return -1;
  991. }
  992. if (wpa_config_set(ssid, name, value, 0) < 0) {
  993. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  994. "variable '%s'", name);
  995. return -1;
  996. }
  997. if (wpa_s->current_ssid == ssid) {
  998. /*
  999. * Invalidate the EAP session cache if anything in the current
  1000. * configuration changes.
  1001. */
  1002. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1003. }
  1004. if ((os_strcmp(name, "psk") == 0 &&
  1005. value[0] == '"' && ssid->ssid_len) ||
  1006. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  1007. wpa_config_update_psk(ssid);
  1008. else if (os_strcmp(name, "priority") == 0)
  1009. wpa_config_update_prio_list(wpa_s->conf);
  1010. return 0;
  1011. }
  1012. static int wpa_supplicant_ctrl_iface_get_network(
  1013. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1014. {
  1015. int id;
  1016. size_t res;
  1017. struct wpa_ssid *ssid;
  1018. char *name, *value;
  1019. /* cmd: "<network id> <variable name>" */
  1020. name = os_strchr(cmd, ' ');
  1021. if (name == NULL || buflen == 0)
  1022. return -1;
  1023. *name++ = '\0';
  1024. id = atoi(cmd);
  1025. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  1026. id, name);
  1027. ssid = wpa_config_get_network(wpa_s->conf, id);
  1028. if (ssid == NULL) {
  1029. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1030. "id=%d", id);
  1031. return -1;
  1032. }
  1033. value = wpa_config_get_no_key(ssid, name);
  1034. if (value == NULL) {
  1035. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  1036. "variable '%s'", name);
  1037. return -1;
  1038. }
  1039. res = os_strlcpy(buf, value, buflen);
  1040. if (res >= buflen) {
  1041. os_free(value);
  1042. return -1;
  1043. }
  1044. os_free(value);
  1045. return res;
  1046. }
  1047. #ifndef CONFIG_NO_CONFIG_WRITE
  1048. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  1049. {
  1050. int ret;
  1051. if (!wpa_s->conf->update_config) {
  1052. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  1053. "to update configuration (update_config=0)");
  1054. return -1;
  1055. }
  1056. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  1057. if (ret) {
  1058. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  1059. "update configuration");
  1060. } else {
  1061. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  1062. " updated");
  1063. }
  1064. return ret;
  1065. }
  1066. #endif /* CONFIG_NO_CONFIG_WRITE */
  1067. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  1068. struct wpa_driver_capa *capa,
  1069. char *buf, size_t buflen)
  1070. {
  1071. int ret, first = 1;
  1072. char *pos, *end;
  1073. size_t len;
  1074. pos = buf;
  1075. end = pos + buflen;
  1076. if (res < 0) {
  1077. if (strict)
  1078. return 0;
  1079. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  1080. if (len >= buflen)
  1081. return -1;
  1082. return len;
  1083. }
  1084. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1085. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1086. if (ret < 0 || ret >= end - pos)
  1087. return pos - buf;
  1088. pos += ret;
  1089. first = 0;
  1090. }
  1091. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1092. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1093. if (ret < 0 || ret >= end - pos)
  1094. return pos - buf;
  1095. pos += ret;
  1096. first = 0;
  1097. }
  1098. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1099. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  1100. if (ret < 0 || ret >= end - pos)
  1101. return pos - buf;
  1102. pos += ret;
  1103. first = 0;
  1104. }
  1105. return pos - buf;
  1106. }
  1107. static int ctrl_iface_get_capability_group(int res, char *strict,
  1108. struct wpa_driver_capa *capa,
  1109. char *buf, size_t buflen)
  1110. {
  1111. int ret, first = 1;
  1112. char *pos, *end;
  1113. size_t len;
  1114. pos = buf;
  1115. end = pos + buflen;
  1116. if (res < 0) {
  1117. if (strict)
  1118. return 0;
  1119. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  1120. if (len >= buflen)
  1121. return -1;
  1122. return len;
  1123. }
  1124. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1125. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1126. if (ret < 0 || ret >= end - pos)
  1127. return pos - buf;
  1128. pos += ret;
  1129. first = 0;
  1130. }
  1131. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1132. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1133. if (ret < 0 || ret >= end - pos)
  1134. return pos - buf;
  1135. pos += ret;
  1136. first = 0;
  1137. }
  1138. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  1139. ret = os_snprintf(pos, end - pos, "%sWEP104",
  1140. first ? "" : " ");
  1141. if (ret < 0 || ret >= end - pos)
  1142. return pos - buf;
  1143. pos += ret;
  1144. first = 0;
  1145. }
  1146. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  1147. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  1148. if (ret < 0 || ret >= end - pos)
  1149. return pos - buf;
  1150. pos += ret;
  1151. first = 0;
  1152. }
  1153. return pos - buf;
  1154. }
  1155. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  1156. struct wpa_driver_capa *capa,
  1157. char *buf, size_t buflen)
  1158. {
  1159. int ret;
  1160. char *pos, *end;
  1161. size_t len;
  1162. pos = buf;
  1163. end = pos + buflen;
  1164. if (res < 0) {
  1165. if (strict)
  1166. return 0;
  1167. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  1168. "NONE", buflen);
  1169. if (len >= buflen)
  1170. return -1;
  1171. return len;
  1172. }
  1173. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  1174. if (ret < 0 || ret >= end - pos)
  1175. return pos - buf;
  1176. pos += ret;
  1177. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1178. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  1179. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  1180. if (ret < 0 || ret >= end - pos)
  1181. return pos - buf;
  1182. pos += ret;
  1183. }
  1184. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1185. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1186. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  1187. if (ret < 0 || ret >= end - pos)
  1188. return pos - buf;
  1189. pos += ret;
  1190. }
  1191. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1192. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  1193. if (ret < 0 || ret >= end - pos)
  1194. return pos - buf;
  1195. pos += ret;
  1196. }
  1197. return pos - buf;
  1198. }
  1199. static int ctrl_iface_get_capability_proto(int res, char *strict,
  1200. struct wpa_driver_capa *capa,
  1201. char *buf, size_t buflen)
  1202. {
  1203. int ret, first = 1;
  1204. char *pos, *end;
  1205. size_t len;
  1206. pos = buf;
  1207. end = pos + buflen;
  1208. if (res < 0) {
  1209. if (strict)
  1210. return 0;
  1211. len = os_strlcpy(buf, "RSN WPA", buflen);
  1212. if (len >= buflen)
  1213. return -1;
  1214. return len;
  1215. }
  1216. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1217. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1218. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  1219. if (ret < 0 || ret >= end - pos)
  1220. return pos - buf;
  1221. pos += ret;
  1222. first = 0;
  1223. }
  1224. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1225. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  1226. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  1227. if (ret < 0 || ret >= end - pos)
  1228. return pos - buf;
  1229. pos += ret;
  1230. first = 0;
  1231. }
  1232. return pos - buf;
  1233. }
  1234. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  1235. struct wpa_driver_capa *capa,
  1236. char *buf, size_t buflen)
  1237. {
  1238. int ret, first = 1;
  1239. char *pos, *end;
  1240. size_t len;
  1241. pos = buf;
  1242. end = pos + buflen;
  1243. if (res < 0) {
  1244. if (strict)
  1245. return 0;
  1246. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  1247. if (len >= buflen)
  1248. return -1;
  1249. return len;
  1250. }
  1251. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  1252. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  1253. if (ret < 0 || ret >= end - pos)
  1254. return pos - buf;
  1255. pos += ret;
  1256. first = 0;
  1257. }
  1258. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  1259. ret = os_snprintf(pos, end - pos, "%sSHARED",
  1260. first ? "" : " ");
  1261. if (ret < 0 || ret >= end - pos)
  1262. return pos - buf;
  1263. pos += ret;
  1264. first = 0;
  1265. }
  1266. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  1267. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  1268. if (ret < 0 || ret >= end - pos)
  1269. return pos - buf;
  1270. pos += ret;
  1271. first = 0;
  1272. }
  1273. return pos - buf;
  1274. }
  1275. static int wpa_supplicant_ctrl_iface_get_capability(
  1276. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  1277. size_t buflen)
  1278. {
  1279. struct wpa_driver_capa capa;
  1280. int res;
  1281. char *strict;
  1282. char field[30];
  1283. size_t len;
  1284. /* Determine whether or not strict checking was requested */
  1285. len = os_strlcpy(field, _field, sizeof(field));
  1286. if (len >= sizeof(field))
  1287. return -1;
  1288. strict = os_strchr(field, ' ');
  1289. if (strict != NULL) {
  1290. *strict++ = '\0';
  1291. if (os_strcmp(strict, "strict") != 0)
  1292. return -1;
  1293. }
  1294. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  1295. field, strict ? strict : "");
  1296. if (os_strcmp(field, "eap") == 0) {
  1297. return eap_get_names(buf, buflen);
  1298. }
  1299. res = wpa_drv_get_capa(wpa_s, &capa);
  1300. if (os_strcmp(field, "pairwise") == 0)
  1301. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  1302. buf, buflen);
  1303. if (os_strcmp(field, "group") == 0)
  1304. return ctrl_iface_get_capability_group(res, strict, &capa,
  1305. buf, buflen);
  1306. if (os_strcmp(field, "key_mgmt") == 0)
  1307. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  1308. buf, buflen);
  1309. if (os_strcmp(field, "proto") == 0)
  1310. return ctrl_iface_get_capability_proto(res, strict, &capa,
  1311. buf, buflen);
  1312. if (os_strcmp(field, "auth_alg") == 0)
  1313. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  1314. buf, buflen);
  1315. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  1316. field);
  1317. return -1;
  1318. }
  1319. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  1320. const char *cmd, char *buf,
  1321. size_t buflen)
  1322. {
  1323. u8 bssid[ETH_ALEN];
  1324. size_t i;
  1325. struct wpa_bss *bss;
  1326. int ret;
  1327. char *pos, *end;
  1328. const u8 *ie, *ie2;
  1329. if (os_strcmp(cmd, "FIRST") == 0)
  1330. bss = dl_list_first(&wpa_s->bss, struct wpa_bss, list);
  1331. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  1332. i = atoi(cmd + 3);
  1333. bss = wpa_bss_get_id(wpa_s, i);
  1334. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  1335. i = atoi(cmd + 5);
  1336. bss = wpa_bss_get_id(wpa_s, i);
  1337. if (bss) {
  1338. struct dl_list *next = bss->list_id.next;
  1339. if (next == &wpa_s->bss_id)
  1340. bss = NULL;
  1341. else
  1342. bss = dl_list_entry(next, struct wpa_bss,
  1343. list_id);
  1344. }
  1345. } else if (hwaddr_aton(cmd, bssid) == 0)
  1346. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1347. else {
  1348. struct wpa_bss *tmp;
  1349. i = atoi(cmd);
  1350. bss = NULL;
  1351. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  1352. {
  1353. if (i-- == 0) {
  1354. bss = tmp;
  1355. break;
  1356. }
  1357. }
  1358. }
  1359. if (bss == NULL)
  1360. return 0;
  1361. pos = buf;
  1362. end = buf + buflen;
  1363. ret = os_snprintf(pos, end - pos,
  1364. "id=%u\n"
  1365. "bssid=" MACSTR "\n"
  1366. "freq=%d\n"
  1367. "beacon_int=%d\n"
  1368. "capabilities=0x%04x\n"
  1369. "qual=%d\n"
  1370. "noise=%d\n"
  1371. "level=%d\n"
  1372. "tsf=%016llu\n"
  1373. "ie=",
  1374. bss->id,
  1375. MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
  1376. bss->caps, bss->qual, bss->noise, bss->level,
  1377. (unsigned long long) bss->tsf);
  1378. if (ret < 0 || ret >= end - pos)
  1379. return pos - buf;
  1380. pos += ret;
  1381. ie = (const u8 *) (bss + 1);
  1382. for (i = 0; i < bss->ie_len; i++) {
  1383. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  1384. if (ret < 0 || ret >= end - pos)
  1385. return pos - buf;
  1386. pos += ret;
  1387. }
  1388. ret = os_snprintf(pos, end - pos, "\n");
  1389. if (ret < 0 || ret >= end - pos)
  1390. return pos - buf;
  1391. pos += ret;
  1392. ret = os_snprintf(pos, end - pos, "flags=");
  1393. if (ret < 0 || ret >= end - pos)
  1394. return pos - buf;
  1395. pos += ret;
  1396. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1397. if (ie)
  1398. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1399. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1400. if (ie2)
  1401. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1402. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1403. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1404. ret = os_snprintf(pos, end - pos, "[WEP]");
  1405. if (ret < 0 || ret >= end - pos)
  1406. return pos - buf;
  1407. pos += ret;
  1408. }
  1409. if (bss->caps & IEEE80211_CAP_IBSS) {
  1410. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1411. if (ret < 0 || ret >= end - pos)
  1412. return pos - buf;
  1413. pos += ret;
  1414. }
  1415. if (bss->caps & IEEE80211_CAP_ESS) {
  1416. ret = os_snprintf(pos, end - pos, "[ESS]");
  1417. if (ret < 0 || ret >= end - pos)
  1418. return pos - buf;
  1419. pos += ret;
  1420. }
  1421. ret = os_snprintf(pos, end - pos, "\n");
  1422. if (ret < 0 || ret >= end - pos)
  1423. return pos - buf;
  1424. pos += ret;
  1425. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  1426. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1427. if (ret < 0 || ret >= end - pos)
  1428. return pos - buf;
  1429. pos += ret;
  1430. #ifdef CONFIG_WPS
  1431. ie = (const u8 *) (bss + 1);
  1432. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  1433. if (ret < 0 || ret >= end - pos)
  1434. return pos - buf;
  1435. pos += ret;
  1436. #endif /* CONFIG_WPS */
  1437. return pos - buf;
  1438. }
  1439. static int wpa_supplicant_ctrl_iface_ap_scan(
  1440. struct wpa_supplicant *wpa_s, char *cmd)
  1441. {
  1442. int ap_scan = atoi(cmd);
  1443. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  1444. }
  1445. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  1446. {
  1447. u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
  1448. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  1449. /* MLME-DELETEKEYS.request */
  1450. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
  1451. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
  1452. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
  1453. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
  1454. #ifdef CONFIG_IEEE80211W
  1455. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
  1456. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
  1457. #endif /* CONFIG_IEEE80211W */
  1458. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  1459. 0);
  1460. /* MLME-SETPROTECTION.request(None) */
  1461. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  1462. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  1463. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  1464. wpa_sm_drop_sa(wpa_s->wpa);
  1465. }
  1466. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  1467. char *addr)
  1468. {
  1469. u8 bssid[ETH_ALEN];
  1470. struct wpa_bss *bss;
  1471. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1472. if (hwaddr_aton(addr, bssid)) {
  1473. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  1474. "address '%s'", addr);
  1475. return -1;
  1476. }
  1477. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  1478. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1479. if (!bss) {
  1480. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  1481. "from BSS table");
  1482. return -1;
  1483. }
  1484. /*
  1485. * TODO: Find best network configuration block from configuration to
  1486. * allow roaming to other networks
  1487. */
  1488. if (!ssid) {
  1489. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  1490. "configuration known for the target AP");
  1491. return -1;
  1492. }
  1493. wpa_s->reassociate = 1;
  1494. wpa_supplicant_connect(wpa_s, bss, ssid);
  1495. return 0;
  1496. }
  1497. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  1498. char *buf, size_t *resp_len)
  1499. {
  1500. char *reply;
  1501. const int reply_size = 2048;
  1502. int ctrl_rsp = 0;
  1503. int reply_len;
  1504. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  1505. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  1506. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  1507. (const u8 *) buf, os_strlen(buf));
  1508. } else {
  1509. wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface",
  1510. (const u8 *) buf, os_strlen(buf));
  1511. }
  1512. reply = os_malloc(reply_size);
  1513. if (reply == NULL) {
  1514. *resp_len = 1;
  1515. return NULL;
  1516. }
  1517. os_memcpy(reply, "OK\n", 3);
  1518. reply_len = 3;
  1519. if (os_strcmp(buf, "PING") == 0) {
  1520. os_memcpy(reply, "PONG\n", 5);
  1521. reply_len = 5;
  1522. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  1523. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  1524. } else if (os_strcmp(buf, "MIB") == 0) {
  1525. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  1526. if (reply_len >= 0) {
  1527. int res;
  1528. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  1529. reply_size - reply_len);
  1530. if (res < 0)
  1531. reply_len = -1;
  1532. else
  1533. reply_len += res;
  1534. }
  1535. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  1536. reply_len = wpa_supplicant_ctrl_iface_status(
  1537. wpa_s, buf + 6, reply, reply_size);
  1538. } else if (os_strcmp(buf, "PMKSA") == 0) {
  1539. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  1540. reply_size);
  1541. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1542. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  1543. reply_len = -1;
  1544. } else if (os_strcmp(buf, "LOGON") == 0) {
  1545. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  1546. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  1547. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  1548. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  1549. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  1550. reply_len = -1;
  1551. else {
  1552. wpa_s->disconnected = 0;
  1553. wpa_s->reassociate = 1;
  1554. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1555. }
  1556. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  1557. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  1558. reply_len = -1;
  1559. else if (wpa_s->disconnected) {
  1560. wpa_s->disconnected = 0;
  1561. wpa_s->reassociate = 1;
  1562. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1563. }
  1564. #ifdef IEEE8021X_EAPOL
  1565. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  1566. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  1567. reply_len = -1;
  1568. #endif /* IEEE8021X_EAPOL */
  1569. #ifdef CONFIG_PEERKEY
  1570. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  1571. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  1572. reply_len = -1;
  1573. #endif /* CONFIG_PEERKEY */
  1574. #ifdef CONFIG_IEEE80211R
  1575. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  1576. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  1577. reply_len = -1;
  1578. #endif /* CONFIG_IEEE80211R */
  1579. #ifdef CONFIG_WPS
  1580. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  1581. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL))
  1582. reply_len = -1;
  1583. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  1584. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8))
  1585. reply_len = -1;
  1586. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  1587. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  1588. reply,
  1589. reply_size);
  1590. #ifdef CONFIG_WPS_OOB
  1591. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  1592. if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
  1593. reply_len = -1;
  1594. #endif /* CONFIG_WPS_OOB */
  1595. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  1596. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  1597. reply_len = -1;
  1598. #ifdef CONFIG_WPS_ER
  1599. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  1600. if (wpas_wps_er_start(wpa_s, NULL))
  1601. reply_len = -1;
  1602. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  1603. if (wpas_wps_er_start(wpa_s, buf + 13))
  1604. reply_len = -1;
  1605. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  1606. if (wpas_wps_er_stop(wpa_s))
  1607. reply_len = -1;
  1608. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  1609. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  1610. reply_len = -1;
  1611. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  1612. if (wpas_wps_er_pbc(wpa_s, buf + 11))
  1613. reply_len = -1;
  1614. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  1615. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  1616. reply_len = -1;
  1617. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  1618. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  1619. reply_len = -1;
  1620. #endif /* CONFIG_WPS_ER */
  1621. #endif /* CONFIG_WPS */
  1622. #ifdef CONFIG_IBSS_RSN
  1623. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  1624. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  1625. reply_len = -1;
  1626. #endif /* CONFIG_IBSS_RSN */
  1627. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  1628. {
  1629. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  1630. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  1631. reply_len = -1;
  1632. else
  1633. ctrl_rsp = 1;
  1634. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  1635. if (wpa_supplicant_reload_configuration(wpa_s))
  1636. reply_len = -1;
  1637. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  1638. wpa_supplicant_terminate_proc(wpa_s->global);
  1639. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  1640. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  1641. reply_len = -1;
  1642. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  1643. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  1644. wpa_s, reply, reply_size);
  1645. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  1646. wpa_s->reassociate = 0;
  1647. wpa_s->disconnected = 1;
  1648. wpa_supplicant_deauthenticate(wpa_s,
  1649. WLAN_REASON_DEAUTH_LEAVING);
  1650. } else if (os_strcmp(buf, "SCAN") == 0) {
  1651. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  1652. reply_len = -1;
  1653. else {
  1654. wpa_s->scan_req = 2;
  1655. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1656. }
  1657. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  1658. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  1659. wpa_s, reply, reply_size);
  1660. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  1661. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  1662. reply_len = -1;
  1663. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  1664. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  1665. reply_len = -1;
  1666. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  1667. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  1668. reply_len = -1;
  1669. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  1670. reply_len = wpa_supplicant_ctrl_iface_add_network(
  1671. wpa_s, reply, reply_size);
  1672. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  1673. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  1674. reply_len = -1;
  1675. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  1676. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  1677. reply_len = -1;
  1678. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  1679. reply_len = wpa_supplicant_ctrl_iface_get_network(
  1680. wpa_s, buf + 12, reply, reply_size);
  1681. #ifndef CONFIG_NO_CONFIG_WRITE
  1682. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  1683. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  1684. reply_len = -1;
  1685. #endif /* CONFIG_NO_CONFIG_WRITE */
  1686. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  1687. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  1688. wpa_s, buf + 15, reply, reply_size);
  1689. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  1690. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  1691. reply_len = -1;
  1692. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  1693. reply_len = wpa_supplicant_global_iface_list(
  1694. wpa_s->global, reply, reply_size);
  1695. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  1696. reply_len = wpa_supplicant_global_iface_interfaces(
  1697. wpa_s->global, reply, reply_size);
  1698. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  1699. reply_len = wpa_supplicant_ctrl_iface_bss(
  1700. wpa_s, buf + 4, reply, reply_size);
  1701. #ifdef CONFIG_AP
  1702. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  1703. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  1704. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  1705. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  1706. reply_size);
  1707. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  1708. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  1709. reply_size);
  1710. #endif /* CONFIG_AP */
  1711. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  1712. wpas_notify_suspend(wpa_s->global);
  1713. } else if (os_strcmp(buf, "RESUME") == 0) {
  1714. wpas_notify_resume(wpa_s->global);
  1715. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  1716. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  1717. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  1718. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  1719. reply_len = -1;
  1720. } else {
  1721. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1722. reply_len = 16;
  1723. }
  1724. if (reply_len < 0) {
  1725. os_memcpy(reply, "FAIL\n", 5);
  1726. reply_len = 5;
  1727. }
  1728. if (ctrl_rsp)
  1729. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  1730. *resp_len = reply_len;
  1731. return reply;
  1732. }
  1733. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  1734. char *cmd)
  1735. {
  1736. struct wpa_interface iface;
  1737. char *pos;
  1738. /*
  1739. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  1740. * TAB<bridge_ifname>
  1741. */
  1742. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  1743. os_memset(&iface, 0, sizeof(iface));
  1744. do {
  1745. iface.ifname = pos = cmd;
  1746. pos = os_strchr(pos, '\t');
  1747. if (pos)
  1748. *pos++ = '\0';
  1749. if (iface.ifname[0] == '\0')
  1750. return -1;
  1751. if (pos == NULL)
  1752. break;
  1753. iface.confname = pos;
  1754. pos = os_strchr(pos, '\t');
  1755. if (pos)
  1756. *pos++ = '\0';
  1757. if (iface.confname[0] == '\0')
  1758. iface.confname = NULL;
  1759. if (pos == NULL)
  1760. break;
  1761. iface.driver = pos;
  1762. pos = os_strchr(pos, '\t');
  1763. if (pos)
  1764. *pos++ = '\0';
  1765. if (iface.driver[0] == '\0')
  1766. iface.driver = NULL;
  1767. if (pos == NULL)
  1768. break;
  1769. iface.ctrl_interface = pos;
  1770. pos = os_strchr(pos, '\t');
  1771. if (pos)
  1772. *pos++ = '\0';
  1773. if (iface.ctrl_interface[0] == '\0')
  1774. iface.ctrl_interface = NULL;
  1775. if (pos == NULL)
  1776. break;
  1777. iface.driver_param = pos;
  1778. pos = os_strchr(pos, '\t');
  1779. if (pos)
  1780. *pos++ = '\0';
  1781. if (iface.driver_param[0] == '\0')
  1782. iface.driver_param = NULL;
  1783. if (pos == NULL)
  1784. break;
  1785. iface.bridge_ifname = pos;
  1786. pos = os_strchr(pos, '\t');
  1787. if (pos)
  1788. *pos++ = '\0';
  1789. if (iface.bridge_ifname[0] == '\0')
  1790. iface.bridge_ifname = NULL;
  1791. if (pos == NULL)
  1792. break;
  1793. } while (0);
  1794. if (wpa_supplicant_get_iface(global, iface.ifname))
  1795. return -1;
  1796. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  1797. }
  1798. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  1799. char *cmd)
  1800. {
  1801. struct wpa_supplicant *wpa_s;
  1802. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  1803. wpa_s = wpa_supplicant_get_iface(global, cmd);
  1804. if (wpa_s == NULL)
  1805. return -1;
  1806. return wpa_supplicant_remove_iface(global, wpa_s);
  1807. }
  1808. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  1809. {
  1810. struct wpa_interface_info *prev;
  1811. while (iface) {
  1812. prev = iface;
  1813. iface = iface->next;
  1814. os_free(prev->ifname);
  1815. os_free(prev->desc);
  1816. os_free(prev);
  1817. }
  1818. }
  1819. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  1820. char *buf, int len)
  1821. {
  1822. int i, res;
  1823. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  1824. char *pos, *end;
  1825. for (i = 0; wpa_drivers[i]; i++) {
  1826. struct wpa_driver_ops *drv = wpa_drivers[i];
  1827. if (drv->get_interfaces == NULL)
  1828. continue;
  1829. tmp = drv->get_interfaces(global->drv_priv[i]);
  1830. if (tmp == NULL)
  1831. continue;
  1832. if (last == NULL)
  1833. iface = last = tmp;
  1834. else
  1835. last->next = tmp;
  1836. while (last->next)
  1837. last = last->next;
  1838. }
  1839. pos = buf;
  1840. end = buf + len;
  1841. for (tmp = iface; tmp; tmp = tmp->next) {
  1842. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  1843. tmp->drv_name, tmp->ifname,
  1844. tmp->desc ? tmp->desc : "");
  1845. if (res < 0 || res >= end - pos) {
  1846. *pos = '\0';
  1847. break;
  1848. }
  1849. pos += res;
  1850. }
  1851. wpa_free_iface_info(iface);
  1852. return pos - buf;
  1853. }
  1854. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  1855. char *buf, int len)
  1856. {
  1857. int res;
  1858. char *pos, *end;
  1859. struct wpa_supplicant *wpa_s;
  1860. wpa_s = global->ifaces;
  1861. pos = buf;
  1862. end = buf + len;
  1863. while (wpa_s) {
  1864. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  1865. if (res < 0 || res >= end - pos) {
  1866. *pos = '\0';
  1867. break;
  1868. }
  1869. pos += res;
  1870. wpa_s = wpa_s->next;
  1871. }
  1872. return pos - buf;
  1873. }
  1874. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  1875. char *buf, size_t *resp_len)
  1876. {
  1877. char *reply;
  1878. const int reply_size = 2048;
  1879. int reply_len;
  1880. wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
  1881. (const u8 *) buf, os_strlen(buf));
  1882. reply = os_malloc(reply_size);
  1883. if (reply == NULL) {
  1884. *resp_len = 1;
  1885. return NULL;
  1886. }
  1887. os_memcpy(reply, "OK\n", 3);
  1888. reply_len = 3;
  1889. if (os_strcmp(buf, "PING") == 0) {
  1890. os_memcpy(reply, "PONG\n", 5);
  1891. reply_len = 5;
  1892. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  1893. if (wpa_supplicant_global_iface_add(global, buf + 14))
  1894. reply_len = -1;
  1895. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  1896. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  1897. reply_len = -1;
  1898. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  1899. reply_len = wpa_supplicant_global_iface_list(
  1900. global, reply, reply_size);
  1901. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  1902. reply_len = wpa_supplicant_global_iface_interfaces(
  1903. global, reply, reply_size);
  1904. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  1905. wpa_supplicant_terminate_proc(global);
  1906. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  1907. wpas_notify_suspend(global);
  1908. } else if (os_strcmp(buf, "RESUME") == 0) {
  1909. wpas_notify_resume(global);
  1910. } else {
  1911. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1912. reply_len = 16;
  1913. }
  1914. if (reply_len < 0) {
  1915. os_memcpy(reply, "FAIL\n", 5);
  1916. reply_len = 5;
  1917. }
  1918. *resp_len = reply_len;
  1919. return reply;
  1920. }