ctrl_iface.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2007, 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 "includes.h"
  15. #include "common.h"
  16. #include "eloop.h"
  17. #include "wpa.h"
  18. #include "config.h"
  19. #include "eapol_supp/eapol_supp_sm.h"
  20. #include "wpa_supplicant_i.h"
  21. #include "ctrl_iface.h"
  22. #include "l2_packet/l2_packet.h"
  23. #include "preauth.h"
  24. #include "pmksa_cache.h"
  25. #include "wpa_ctrl.h"
  26. #include "eap_peer/eap.h"
  27. #include "ieee802_11_defs.h"
  28. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  29. char *buf, int len);
  30. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  31. char *cmd)
  32. {
  33. char *value;
  34. int ret = 0;
  35. value = os_strchr(cmd, ' ');
  36. if (value == NULL)
  37. return -1;
  38. *value++ = '\0';
  39. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  40. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  41. eapol_sm_configure(wpa_s->eapol,
  42. atoi(value), -1, -1, -1);
  43. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  44. eapol_sm_configure(wpa_s->eapol,
  45. -1, atoi(value), -1, -1);
  46. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  47. eapol_sm_configure(wpa_s->eapol,
  48. -1, -1, atoi(value), -1);
  49. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  50. eapol_sm_configure(wpa_s->eapol,
  51. -1, -1, -1, atoi(value));
  52. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  53. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  54. atoi(value)))
  55. ret = -1;
  56. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  57. 0) {
  58. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  59. atoi(value)))
  60. ret = -1;
  61. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  62. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  63. ret = -1;
  64. } else
  65. ret = -1;
  66. return ret;
  67. }
  68. #ifdef IEEE8021X_EAPOL
  69. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  70. char *addr)
  71. {
  72. u8 bssid[ETH_ALEN];
  73. struct wpa_ssid *ssid = wpa_s->current_ssid;
  74. if (hwaddr_aton(addr, bssid)) {
  75. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  76. "'%s'", addr);
  77. return -1;
  78. }
  79. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  80. rsn_preauth_deinit(wpa_s->wpa);
  81. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  82. return -1;
  83. return 0;
  84. }
  85. #endif /* IEEE8021X_EAPOL */
  86. #ifdef CONFIG_PEERKEY
  87. /* MLME-STKSTART.request(peer) */
  88. static int wpa_supplicant_ctrl_iface_stkstart(
  89. struct wpa_supplicant *wpa_s, char *addr)
  90. {
  91. u8 peer[ETH_ALEN];
  92. if (hwaddr_aton(addr, peer)) {
  93. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  94. "address '%s'", peer);
  95. return -1;
  96. }
  97. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  98. MAC2STR(peer));
  99. return wpa_sm_stkstart(wpa_s->wpa, peer);
  100. }
  101. #endif /* CONFIG_PEERKEY */
  102. #ifdef CONFIG_IEEE80211R
  103. static int wpa_supplicant_ctrl_iface_ft_ds(
  104. struct wpa_supplicant *wpa_s, char *addr)
  105. {
  106. u8 target_ap[ETH_ALEN];
  107. if (hwaddr_aton(addr, target_ap)) {
  108. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  109. "address '%s'", target_ap);
  110. return -1;
  111. }
  112. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  113. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap);
  114. }
  115. #endif /* CONFIG_IEEE80211R */
  116. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  117. char *rsp)
  118. {
  119. #ifdef IEEE8021X_EAPOL
  120. char *pos, *id_pos;
  121. int id;
  122. struct wpa_ssid *ssid;
  123. struct eap_peer_config *eap;
  124. pos = os_strchr(rsp, '-');
  125. if (pos == NULL)
  126. return -1;
  127. *pos++ = '\0';
  128. id_pos = pos;
  129. pos = os_strchr(pos, ':');
  130. if (pos == NULL)
  131. return -1;
  132. *pos++ = '\0';
  133. id = atoi(id_pos);
  134. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  135. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  136. (u8 *) pos, os_strlen(pos));
  137. ssid = wpa_config_get_network(wpa_s->conf, id);
  138. if (ssid == NULL) {
  139. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  140. "to update", id);
  141. return -1;
  142. }
  143. eap = &ssid->eap;
  144. if (os_strcmp(rsp, "IDENTITY") == 0) {
  145. os_free(eap->identity);
  146. eap->identity = (u8 *) os_strdup(pos);
  147. eap->identity_len = os_strlen(pos);
  148. eap->pending_req_identity = 0;
  149. if (ssid == wpa_s->current_ssid)
  150. wpa_s->reassociate = 1;
  151. } else if (os_strcmp(rsp, "PASSWORD") == 0) {
  152. os_free(eap->password);
  153. eap->password = (u8 *) os_strdup(pos);
  154. eap->password_len = os_strlen(pos);
  155. eap->pending_req_password = 0;
  156. if (ssid == wpa_s->current_ssid)
  157. wpa_s->reassociate = 1;
  158. } else if (os_strcmp(rsp, "NEW_PASSWORD") == 0) {
  159. os_free(eap->new_password);
  160. eap->new_password = (u8 *) os_strdup(pos);
  161. eap->new_password_len = os_strlen(pos);
  162. eap->pending_req_new_password = 0;
  163. if (ssid == wpa_s->current_ssid)
  164. wpa_s->reassociate = 1;
  165. } else if (os_strcmp(rsp, "PIN") == 0) {
  166. os_free(eap->pin);
  167. eap->pin = os_strdup(pos);
  168. eap->pending_req_pin = 0;
  169. if (ssid == wpa_s->current_ssid)
  170. wpa_s->reassociate = 1;
  171. } else if (os_strcmp(rsp, "OTP") == 0) {
  172. os_free(eap->otp);
  173. eap->otp = (u8 *) os_strdup(pos);
  174. eap->otp_len = os_strlen(pos);
  175. os_free(eap->pending_req_otp);
  176. eap->pending_req_otp = NULL;
  177. eap->pending_req_otp_len = 0;
  178. } else if (os_strcmp(rsp, "PASSPHRASE") == 0) {
  179. os_free(eap->private_key_passwd);
  180. eap->private_key_passwd = (u8 *) os_strdup(pos);
  181. eap->pending_req_passphrase = 0;
  182. if (ssid == wpa_s->current_ssid)
  183. wpa_s->reassociate = 1;
  184. } else {
  185. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", rsp);
  186. return -1;
  187. }
  188. return 0;
  189. #else /* IEEE8021X_EAPOL */
  190. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  191. return -1;
  192. #endif /* IEEE8021X_EAPOL */
  193. }
  194. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  195. const char *params,
  196. char *buf, size_t buflen)
  197. {
  198. char *pos, *end, tmp[30];
  199. int res, verbose, ret;
  200. verbose = os_strcmp(params, "-VERBOSE") == 0;
  201. pos = buf;
  202. end = buf + buflen;
  203. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  204. struct wpa_ssid *ssid = wpa_s->current_ssid;
  205. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  206. MAC2STR(wpa_s->bssid));
  207. if (ret < 0 || ret >= end - pos)
  208. return pos - buf;
  209. pos += ret;
  210. if (ssid) {
  211. u8 *_ssid = ssid->ssid;
  212. size_t ssid_len = ssid->ssid_len;
  213. u8 ssid_buf[MAX_SSID_LEN];
  214. if (ssid_len == 0) {
  215. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  216. if (_res < 0)
  217. ssid_len = 0;
  218. else
  219. ssid_len = _res;
  220. _ssid = ssid_buf;
  221. }
  222. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  223. wpa_ssid_txt(_ssid, ssid_len),
  224. ssid->id);
  225. if (ret < 0 || ret >= end - pos)
  226. return pos - buf;
  227. pos += ret;
  228. if (ssid->id_str) {
  229. ret = os_snprintf(pos, end - pos,
  230. "id_str=%s\n",
  231. ssid->id_str);
  232. if (ret < 0 || ret >= end - pos)
  233. return pos - buf;
  234. pos += ret;
  235. }
  236. }
  237. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  238. }
  239. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  240. wpa_supplicant_state_txt(wpa_s->wpa_state));
  241. if (ret < 0 || ret >= end - pos)
  242. return pos - buf;
  243. pos += ret;
  244. if (wpa_s->l2 &&
  245. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  246. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  247. if (ret < 0 || ret >= end - pos)
  248. return pos - buf;
  249. pos += ret;
  250. }
  251. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
  252. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
  253. wpa_s->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
  254. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  255. verbose);
  256. if (res >= 0)
  257. pos += res;
  258. }
  259. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  260. if (res >= 0)
  261. pos += res;
  262. return pos - buf;
  263. }
  264. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  265. char *cmd)
  266. {
  267. char *pos;
  268. int id;
  269. struct wpa_ssid *ssid;
  270. u8 bssid[ETH_ALEN];
  271. /* cmd: "<network id> <BSSID>" */
  272. pos = os_strchr(cmd, ' ');
  273. if (pos == NULL)
  274. return -1;
  275. *pos++ = '\0';
  276. id = atoi(cmd);
  277. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  278. if (hwaddr_aton(pos, bssid)) {
  279. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  280. return -1;
  281. }
  282. ssid = wpa_config_get_network(wpa_s->conf, id);
  283. if (ssid == NULL) {
  284. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  285. "to update", id);
  286. return -1;
  287. }
  288. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  289. ssid->bssid_set =
  290. os_memcmp(bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) != 0;
  291. return 0;
  292. }
  293. static int wpa_supplicant_ctrl_iface_list_networks(
  294. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  295. {
  296. char *pos, *end;
  297. struct wpa_ssid *ssid;
  298. int ret;
  299. pos = buf;
  300. end = buf + buflen;
  301. ret = os_snprintf(pos, end - pos,
  302. "network id / ssid / bssid / flags\n");
  303. if (ret < 0 || ret >= end - pos)
  304. return pos - buf;
  305. pos += ret;
  306. ssid = wpa_s->conf->ssid;
  307. while (ssid) {
  308. ret = os_snprintf(pos, end - pos, "%d\t%s",
  309. ssid->id,
  310. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  311. if (ret < 0 || ret >= end - pos)
  312. return pos - buf;
  313. pos += ret;
  314. if (ssid->bssid_set) {
  315. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  316. MAC2STR(ssid->bssid));
  317. } else {
  318. ret = os_snprintf(pos, end - pos, "\tany");
  319. }
  320. if (ret < 0 || ret >= end - pos)
  321. return pos - buf;
  322. pos += ret;
  323. ret = os_snprintf(pos, end - pos, "\t%s%s",
  324. ssid == wpa_s->current_ssid ?
  325. "[CURRENT]" : "",
  326. ssid->disabled ? "[DISABLED]" : "");
  327. if (ret < 0 || ret >= end - pos)
  328. return pos - buf;
  329. pos += ret;
  330. ret = os_snprintf(pos, end - pos, "\n");
  331. if (ret < 0 || ret >= end - pos)
  332. return pos - buf;
  333. pos += ret;
  334. ssid = ssid->next;
  335. }
  336. return pos - buf;
  337. }
  338. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  339. {
  340. int first = 1, ret;
  341. ret = os_snprintf(pos, end - pos, "-");
  342. if (ret < 0 || ret >= end - pos)
  343. return pos;
  344. pos += ret;
  345. if (cipher & WPA_CIPHER_NONE) {
  346. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
  347. if (ret < 0 || ret >= end - pos)
  348. return pos;
  349. pos += ret;
  350. first = 0;
  351. }
  352. if (cipher & WPA_CIPHER_WEP40) {
  353. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
  354. if (ret < 0 || ret >= end - pos)
  355. return pos;
  356. pos += ret;
  357. first = 0;
  358. }
  359. if (cipher & WPA_CIPHER_WEP104) {
  360. ret = os_snprintf(pos, end - pos, "%sWEP104",
  361. first ? "" : "+");
  362. if (ret < 0 || ret >= end - pos)
  363. return pos;
  364. pos += ret;
  365. first = 0;
  366. }
  367. if (cipher & WPA_CIPHER_TKIP) {
  368. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
  369. if (ret < 0 || ret >= end - pos)
  370. return pos;
  371. pos += ret;
  372. first = 0;
  373. }
  374. if (cipher & WPA_CIPHER_CCMP) {
  375. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
  376. if (ret < 0 || ret >= end - pos)
  377. return pos;
  378. pos += ret;
  379. first = 0;
  380. }
  381. return pos;
  382. }
  383. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  384. const u8 *ie, size_t ie_len)
  385. {
  386. struct wpa_ie_data data;
  387. int first, ret;
  388. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  389. if (ret < 0 || ret >= end - pos)
  390. return pos;
  391. pos += ret;
  392. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  393. ret = os_snprintf(pos, end - pos, "?]");
  394. if (ret < 0 || ret >= end - pos)
  395. return pos;
  396. pos += ret;
  397. return pos;
  398. }
  399. first = 1;
  400. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  401. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  402. if (ret < 0 || ret >= end - pos)
  403. return pos;
  404. pos += ret;
  405. first = 0;
  406. }
  407. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  408. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  409. if (ret < 0 || ret >= end - pos)
  410. return pos;
  411. pos += ret;
  412. first = 0;
  413. }
  414. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  415. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  416. if (ret < 0 || ret >= end - pos)
  417. return pos;
  418. pos += ret;
  419. first = 0;
  420. }
  421. #ifdef CONFIG_IEEE80211R
  422. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  423. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  424. first ? "" : "+");
  425. if (ret < 0 || ret >= end - pos)
  426. return pos;
  427. pos += ret;
  428. first = 0;
  429. }
  430. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  431. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  432. first ? "" : "+");
  433. if (ret < 0 || ret >= end - pos)
  434. return pos;
  435. pos += ret;
  436. first = 0;
  437. }
  438. #endif /* CONFIG_IEEE80211R */
  439. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  440. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  441. ret = os_snprintf(pos, end - pos, "-preauth");
  442. if (ret < 0 || ret >= end - pos)
  443. return pos;
  444. pos += ret;
  445. }
  446. ret = os_snprintf(pos, end - pos, "]");
  447. if (ret < 0 || ret >= end - pos)
  448. return pos;
  449. pos += ret;
  450. return pos;
  451. }
  452. /* Format one result on one text line into a buffer. */
  453. static int wpa_supplicant_ctrl_iface_scan_result(
  454. const struct wpa_scan_res *res, char *buf, size_t buflen)
  455. {
  456. char *pos, *end;
  457. int ret;
  458. const u8 *ie, *ie2;
  459. pos = buf;
  460. end = buf + buflen;
  461. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  462. MAC2STR(res->bssid), res->freq, res->level);
  463. if (ret < 0 || ret >= end - pos)
  464. return pos - buf;
  465. pos += ret;
  466. ie = wpa_scan_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
  467. if (ie)
  468. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  469. ie2 = wpa_scan_get_ie(res, WLAN_EID_RSN);
  470. if (ie2)
  471. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  472. if (!ie && !ie2 && res->caps & IEEE80211_CAP_PRIVACY) {
  473. ret = os_snprintf(pos, end - pos, "[WEP]");
  474. if (ret < 0 || ret >= end - pos)
  475. return pos - buf;
  476. pos += ret;
  477. }
  478. if (res->caps & IEEE80211_CAP_IBSS) {
  479. ret = os_snprintf(pos, end - pos, "[IBSS]");
  480. if (ret < 0 || ret >= end - pos)
  481. return pos - buf;
  482. pos += ret;
  483. }
  484. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  485. ret = os_snprintf(pos, end - pos, "\t%s",
  486. ie ? wpa_ssid_txt(ie + 2, ie[1]) : "");
  487. if (ret < 0 || ret >= end - pos)
  488. return pos - buf;
  489. pos += ret;
  490. ret = os_snprintf(pos, end - pos, "\n");
  491. if (ret < 0 || ret >= end - pos)
  492. return pos - buf;
  493. pos += ret;
  494. return pos - buf;
  495. }
  496. static int wpa_supplicant_ctrl_iface_scan_results(
  497. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  498. {
  499. char *pos, *end;
  500. struct wpa_scan_res *res;
  501. int ret;
  502. size_t i;
  503. if (wpa_s->scan_res == NULL &&
  504. wpa_supplicant_get_scan_results(wpa_s) < 0)
  505. return 0;
  506. pos = buf;
  507. end = buf + buflen;
  508. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  509. "flags / ssid\n");
  510. if (ret < 0 || ret >= end - pos)
  511. return pos - buf;
  512. pos += ret;
  513. for (i = 0; i < wpa_s->scan_res->num; i++) {
  514. res = wpa_s->scan_res->res[i];
  515. ret = wpa_supplicant_ctrl_iface_scan_result(res, pos,
  516. end - pos);
  517. if (ret < 0 || ret >= end - pos)
  518. return pos - buf;
  519. pos += ret;
  520. }
  521. return pos - buf;
  522. }
  523. static int wpa_supplicant_ctrl_iface_select_network(
  524. struct wpa_supplicant *wpa_s, char *cmd)
  525. {
  526. int id;
  527. struct wpa_ssid *ssid;
  528. /* cmd: "<network id>" or "any" */
  529. if (os_strcmp(cmd, "any") == 0) {
  530. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  531. ssid = wpa_s->conf->ssid;
  532. while (ssid) {
  533. ssid->disabled = 0;
  534. ssid = ssid->next;
  535. }
  536. wpa_s->reassociate = 1;
  537. wpa_supplicant_req_scan(wpa_s, 0, 0);
  538. return 0;
  539. }
  540. id = atoi(cmd);
  541. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  542. ssid = wpa_config_get_network(wpa_s->conf, id);
  543. if (ssid == NULL) {
  544. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  545. "id=%d", id);
  546. return -1;
  547. }
  548. if (ssid != wpa_s->current_ssid && wpa_s->current_ssid)
  549. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  550. /* Mark all other networks disabled and trigger reassociation */
  551. ssid = wpa_s->conf->ssid;
  552. while (ssid) {
  553. ssid->disabled = id != ssid->id;
  554. ssid = ssid->next;
  555. }
  556. wpa_s->reassociate = 1;
  557. wpa_supplicant_req_scan(wpa_s, 0, 0);
  558. return 0;
  559. }
  560. static int wpa_supplicant_ctrl_iface_enable_network(
  561. struct wpa_supplicant *wpa_s, char *cmd)
  562. {
  563. int id;
  564. struct wpa_ssid *ssid;
  565. /* cmd: "<network id>" or "all" */
  566. if (os_strcmp(cmd, "all") == 0) {
  567. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  568. ssid = wpa_s->conf->ssid;
  569. while (ssid) {
  570. if (ssid == wpa_s->current_ssid && ssid->disabled)
  571. wpa_s->reassociate = 1;
  572. ssid->disabled = 0;
  573. ssid = ssid->next;
  574. }
  575. if (wpa_s->reassociate)
  576. wpa_supplicant_req_scan(wpa_s, 0, 0);
  577. return 0;
  578. }
  579. id = atoi(cmd);
  580. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  581. ssid = wpa_config_get_network(wpa_s->conf, id);
  582. if (ssid == NULL) {
  583. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  584. "id=%d", id);
  585. return -1;
  586. }
  587. if (wpa_s->current_ssid == NULL && ssid->disabled) {
  588. /*
  589. * Try to reassociate since there is no current configuration
  590. * and a new network was made available. */
  591. wpa_s->reassociate = 1;
  592. wpa_supplicant_req_scan(wpa_s, 0, 0);
  593. }
  594. ssid->disabled = 0;
  595. return 0;
  596. }
  597. static int wpa_supplicant_ctrl_iface_disable_network(
  598. struct wpa_supplicant *wpa_s, char *cmd)
  599. {
  600. int id;
  601. struct wpa_ssid *ssid;
  602. /* cmd: "<network id>" or "all" */
  603. if (os_strcmp(cmd, "all") == 0) {
  604. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  605. ssid = wpa_s->conf->ssid;
  606. while (ssid) {
  607. ssid->disabled = 1;
  608. ssid = ssid->next;
  609. }
  610. if (wpa_s->current_ssid)
  611. wpa_supplicant_disassociate(wpa_s,
  612. WLAN_REASON_DEAUTH_LEAVING);
  613. return 0;
  614. }
  615. id = atoi(cmd);
  616. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  617. ssid = wpa_config_get_network(wpa_s->conf, id);
  618. if (ssid == NULL) {
  619. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  620. "id=%d", id);
  621. return -1;
  622. }
  623. if (ssid == wpa_s->current_ssid)
  624. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  625. ssid->disabled = 1;
  626. return 0;
  627. }
  628. static int wpa_supplicant_ctrl_iface_add_network(
  629. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  630. {
  631. struct wpa_ssid *ssid;
  632. int ret;
  633. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  634. ssid = wpa_config_add_network(wpa_s->conf);
  635. if (ssid == NULL)
  636. return -1;
  637. ssid->disabled = 1;
  638. wpa_config_set_network_defaults(ssid);
  639. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  640. if (ret < 0 || (size_t) ret >= buflen)
  641. return -1;
  642. return ret;
  643. }
  644. static int wpa_supplicant_ctrl_iface_remove_network(
  645. struct wpa_supplicant *wpa_s, char *cmd)
  646. {
  647. int id;
  648. struct wpa_ssid *ssid;
  649. /* cmd: "<network id>" or "all" */
  650. if (os_strcmp(cmd, "all") == 0) {
  651. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  652. ssid = wpa_s->conf->ssid;
  653. while (ssid) {
  654. id = ssid->id;
  655. ssid = ssid->next;
  656. wpa_config_remove_network(wpa_s->conf, id);
  657. }
  658. if (wpa_s->current_ssid) {
  659. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  660. wpa_supplicant_disassociate(wpa_s,
  661. WLAN_REASON_DEAUTH_LEAVING);
  662. }
  663. return 0;
  664. }
  665. id = atoi(cmd);
  666. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  667. ssid = wpa_config_get_network(wpa_s->conf, id);
  668. if (ssid == NULL ||
  669. wpa_config_remove_network(wpa_s->conf, id) < 0) {
  670. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  671. "id=%d", id);
  672. return -1;
  673. }
  674. if (ssid == wpa_s->current_ssid) {
  675. /*
  676. * Invalidate the EAP session cache if the current network is
  677. * removed.
  678. */
  679. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  680. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  681. }
  682. return 0;
  683. }
  684. static int wpa_supplicant_ctrl_iface_set_network(
  685. struct wpa_supplicant *wpa_s, char *cmd)
  686. {
  687. int id;
  688. struct wpa_ssid *ssid;
  689. char *name, *value;
  690. /* cmd: "<network id> <variable name> <value>" */
  691. name = os_strchr(cmd, ' ');
  692. if (name == NULL)
  693. return -1;
  694. *name++ = '\0';
  695. value = os_strchr(name, ' ');
  696. if (value == NULL)
  697. return -1;
  698. *value++ = '\0';
  699. id = atoi(cmd);
  700. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  701. id, name);
  702. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  703. (u8 *) value, os_strlen(value));
  704. ssid = wpa_config_get_network(wpa_s->conf, id);
  705. if (ssid == NULL) {
  706. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  707. "id=%d", id);
  708. return -1;
  709. }
  710. if (wpa_config_set(ssid, name, value, 0) < 0) {
  711. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  712. "variable '%s'", name);
  713. return -1;
  714. }
  715. if (wpa_s->current_ssid == ssid) {
  716. /*
  717. * Invalidate the EAP session cache if anything in the current
  718. * configuration changes.
  719. */
  720. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  721. }
  722. if ((os_strcmp(name, "psk") == 0 &&
  723. value[0] == '"' && ssid->ssid_len) ||
  724. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  725. wpa_config_update_psk(ssid);
  726. return 0;
  727. }
  728. static int wpa_supplicant_ctrl_iface_get_network(
  729. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  730. {
  731. int id;
  732. size_t res;
  733. struct wpa_ssid *ssid;
  734. char *name, *value;
  735. /* cmd: "<network id> <variable name>" */
  736. name = os_strchr(cmd, ' ');
  737. if (name == NULL || buflen == 0)
  738. return -1;
  739. *name++ = '\0';
  740. id = atoi(cmd);
  741. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  742. id, name);
  743. ssid = wpa_config_get_network(wpa_s->conf, id);
  744. if (ssid == NULL) {
  745. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  746. "id=%d", id);
  747. return -1;
  748. }
  749. value = wpa_config_get_no_key(ssid, name);
  750. if (value == NULL) {
  751. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  752. "variable '%s'", name);
  753. return -1;
  754. }
  755. res = os_strlcpy(buf, value, buflen);
  756. if (res >= buflen) {
  757. os_free(value);
  758. return -1;
  759. }
  760. os_free(value);
  761. return res;
  762. }
  763. #ifndef CONFIG_NO_CONFIG_WRITE
  764. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  765. {
  766. int ret;
  767. if (!wpa_s->conf->update_config) {
  768. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  769. "to update configuration (update_config=0)");
  770. return -1;
  771. }
  772. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  773. if (ret) {
  774. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  775. "update configuration");
  776. } else {
  777. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  778. " updated");
  779. }
  780. return ret;
  781. }
  782. #endif /* CONFIG_NO_CONFIG_WRITE */
  783. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  784. struct wpa_driver_capa *capa,
  785. char *buf, size_t buflen)
  786. {
  787. int ret, first = 1;
  788. char *pos, *end;
  789. size_t len;
  790. pos = buf;
  791. end = pos + buflen;
  792. if (res < 0) {
  793. if (strict)
  794. return 0;
  795. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  796. if (len >= buflen)
  797. return -1;
  798. return len;
  799. }
  800. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  801. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  802. if (ret < 0 || ret >= end - pos)
  803. return pos - buf;
  804. pos += ret;
  805. first = 0;
  806. }
  807. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  808. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  809. if (ret < 0 || ret >= end - pos)
  810. return pos - buf;
  811. pos += ret;
  812. first = 0;
  813. }
  814. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  815. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  816. if (ret < 0 || ret >= end - pos)
  817. return pos - buf;
  818. pos += ret;
  819. first = 0;
  820. }
  821. return pos - buf;
  822. }
  823. static int ctrl_iface_get_capability_group(int res, char *strict,
  824. struct wpa_driver_capa *capa,
  825. char *buf, size_t buflen)
  826. {
  827. int ret, first = 1;
  828. char *pos, *end;
  829. size_t len;
  830. pos = buf;
  831. end = pos + buflen;
  832. if (res < 0) {
  833. if (strict)
  834. return 0;
  835. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  836. if (len >= buflen)
  837. return -1;
  838. return len;
  839. }
  840. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  841. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  842. if (ret < 0 || ret >= end - pos)
  843. return pos - buf;
  844. pos += ret;
  845. first = 0;
  846. }
  847. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  848. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  849. if (ret < 0 || ret >= end - pos)
  850. return pos - buf;
  851. pos += ret;
  852. first = 0;
  853. }
  854. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  855. ret = os_snprintf(pos, end - pos, "%sWEP104",
  856. first ? "" : " ");
  857. if (ret < 0 || ret >= end - pos)
  858. return pos - buf;
  859. pos += ret;
  860. first = 0;
  861. }
  862. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  863. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  864. if (ret < 0 || ret >= end - pos)
  865. return pos - buf;
  866. pos += ret;
  867. first = 0;
  868. }
  869. return pos - buf;
  870. }
  871. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  872. struct wpa_driver_capa *capa,
  873. char *buf, size_t buflen)
  874. {
  875. int ret;
  876. char *pos, *end;
  877. size_t len;
  878. pos = buf;
  879. end = pos + buflen;
  880. if (res < 0) {
  881. if (strict)
  882. return 0;
  883. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  884. "NONE", buflen);
  885. if (len >= buflen)
  886. return -1;
  887. return len;
  888. }
  889. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  890. if (ret < 0 || ret >= end - pos)
  891. return pos - buf;
  892. pos += ret;
  893. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  894. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  895. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  896. if (ret < 0 || ret >= end - pos)
  897. return pos - buf;
  898. pos += ret;
  899. }
  900. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  901. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  902. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  903. if (ret < 0 || ret >= end - pos)
  904. return pos - buf;
  905. pos += ret;
  906. }
  907. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  908. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  909. if (ret < 0 || ret >= end - pos)
  910. return pos - buf;
  911. pos += ret;
  912. }
  913. return pos - buf;
  914. }
  915. static int ctrl_iface_get_capability_proto(int res, char *strict,
  916. struct wpa_driver_capa *capa,
  917. char *buf, size_t buflen)
  918. {
  919. int ret, first = 1;
  920. char *pos, *end;
  921. size_t len;
  922. pos = buf;
  923. end = pos + buflen;
  924. if (res < 0) {
  925. if (strict)
  926. return 0;
  927. len = os_strlcpy(buf, "RSN WPA", buflen);
  928. if (len >= buflen)
  929. return -1;
  930. return len;
  931. }
  932. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  933. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  934. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  935. if (ret < 0 || ret >= end - pos)
  936. return pos - buf;
  937. pos += ret;
  938. first = 0;
  939. }
  940. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  941. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  942. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  943. if (ret < 0 || ret >= end - pos)
  944. return pos - buf;
  945. pos += ret;
  946. first = 0;
  947. }
  948. return pos - buf;
  949. }
  950. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  951. struct wpa_driver_capa *capa,
  952. char *buf, size_t buflen)
  953. {
  954. int ret, first = 1;
  955. char *pos, *end;
  956. size_t len;
  957. pos = buf;
  958. end = pos + buflen;
  959. if (res < 0) {
  960. if (strict)
  961. return 0;
  962. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  963. if (len >= buflen)
  964. return -1;
  965. return len;
  966. }
  967. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  968. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  969. if (ret < 0 || ret >= end - pos)
  970. return pos - buf;
  971. pos += ret;
  972. first = 0;
  973. }
  974. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  975. ret = os_snprintf(pos, end - pos, "%sSHARED",
  976. first ? "" : " ");
  977. if (ret < 0 || ret >= end - pos)
  978. return pos - buf;
  979. pos += ret;
  980. first = 0;
  981. }
  982. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  983. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  984. if (ret < 0 || ret >= end - pos)
  985. return pos - buf;
  986. pos += ret;
  987. first = 0;
  988. }
  989. return pos - buf;
  990. }
  991. static int wpa_supplicant_ctrl_iface_get_capability(
  992. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  993. size_t buflen)
  994. {
  995. struct wpa_driver_capa capa;
  996. int res;
  997. char *strict;
  998. char field[30];
  999. size_t len;
  1000. /* Determine whether or not strict checking was requested */
  1001. len = os_strlcpy(field, _field, sizeof(field));
  1002. if (len >= sizeof(field))
  1003. return -1;
  1004. strict = os_strchr(field, ' ');
  1005. if (strict != NULL) {
  1006. *strict++ = '\0';
  1007. if (os_strcmp(strict, "strict") != 0)
  1008. return -1;
  1009. }
  1010. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  1011. field, strict ? strict : "");
  1012. if (os_strcmp(field, "eap") == 0) {
  1013. return eap_get_names(buf, buflen);
  1014. }
  1015. res = wpa_drv_get_capa(wpa_s, &capa);
  1016. if (os_strcmp(field, "pairwise") == 0)
  1017. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  1018. buf, buflen);
  1019. if (os_strcmp(field, "group") == 0)
  1020. return ctrl_iface_get_capability_group(res, strict, &capa,
  1021. buf, buflen);
  1022. if (os_strcmp(field, "key_mgmt") == 0)
  1023. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  1024. buf, buflen);
  1025. if (os_strcmp(field, "proto") == 0)
  1026. return ctrl_iface_get_capability_proto(res, strict, &capa,
  1027. buf, buflen);
  1028. if (os_strcmp(field, "auth_alg") == 0)
  1029. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  1030. buf, buflen);
  1031. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  1032. field);
  1033. return -1;
  1034. }
  1035. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  1036. const char *cmd, char *buf,
  1037. size_t buflen)
  1038. {
  1039. u8 bssid[ETH_ALEN];
  1040. size_t i;
  1041. struct wpa_scan_results *results;
  1042. struct wpa_scan_res *bss;
  1043. int ret;
  1044. char *pos, *end;
  1045. const u8 *ie, *ie2;
  1046. results = wpa_s->scan_res;
  1047. if (results == NULL)
  1048. return 0;
  1049. if (hwaddr_aton(cmd, bssid) == 0) {
  1050. for (i = 0; i < results->num; i++) {
  1051. if (os_memcmp(bssid, results->res[i]->bssid, ETH_ALEN)
  1052. == 0)
  1053. break;
  1054. }
  1055. } else
  1056. i = atoi(cmd);
  1057. if (i >= results->num || results->res[i] == NULL)
  1058. return 0; /* no match found */
  1059. bss = results->res[i];
  1060. pos = buf;
  1061. end = buf + buflen;
  1062. ret = snprintf(pos, end - pos,
  1063. "bssid=" MACSTR "\n"
  1064. "freq=%d\n"
  1065. "beacon_int=%d\n"
  1066. "capabilities=0x%04x\n"
  1067. "qual=%d\n"
  1068. "noise=%d\n"
  1069. "level=%d\n"
  1070. "tsf=%016llu\n"
  1071. "ie=",
  1072. MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
  1073. bss->caps, bss->qual, bss->noise, bss->level,
  1074. (unsigned long long) bss->tsf);
  1075. if (ret < 0 || ret >= end - pos)
  1076. return pos - buf;
  1077. pos += ret;
  1078. ie = (const u8 *) (bss + 1);
  1079. for (i = 0; i < bss->ie_len; i++) {
  1080. ret = snprintf(pos, end - pos, "%02x", *ie++);
  1081. if (ret < 0 || ret >= end - pos)
  1082. return pos - buf;
  1083. pos += ret;
  1084. }
  1085. ret = snprintf(pos, end - pos, "\n");
  1086. if (ret < 0 || ret >= end - pos)
  1087. return pos - buf;
  1088. pos += ret;
  1089. ret = os_snprintf(pos, end - pos, "flags=");
  1090. if (ret < 0 || ret >= end - pos)
  1091. return pos - buf;
  1092. pos += ret;
  1093. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1094. if (ie)
  1095. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1096. ie2 = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  1097. if (ie2)
  1098. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1099. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1100. ret = os_snprintf(pos, end - pos, "[WEP]");
  1101. if (ret < 0 || ret >= end - pos)
  1102. return pos - buf;
  1103. pos += ret;
  1104. }
  1105. if (bss->caps & IEEE80211_CAP_IBSS) {
  1106. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1107. if (ret < 0 || ret >= end - pos)
  1108. return pos - buf;
  1109. pos += ret;
  1110. }
  1111. ret = snprintf(pos, end - pos, "\n");
  1112. if (ret < 0 || ret >= end - pos)
  1113. return pos - buf;
  1114. pos += ret;
  1115. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  1116. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  1117. ie ? wpa_ssid_txt(ie + 2, ie[1]) : "");
  1118. if (ret < 0 || ret >= end - pos)
  1119. return pos - buf;
  1120. pos += ret;
  1121. return pos - buf;
  1122. }
  1123. static int wpa_supplicant_ctrl_iface_ap_scan(
  1124. struct wpa_supplicant *wpa_s, char *cmd)
  1125. {
  1126. int ap_scan = atoi(cmd);
  1127. if (ap_scan < 0 || ap_scan > 2)
  1128. return -1;
  1129. wpa_s->conf->ap_scan = ap_scan;
  1130. return 0;
  1131. }
  1132. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  1133. char *buf, size_t *resp_len)
  1134. {
  1135. char *reply;
  1136. const int reply_size = 2048;
  1137. int ctrl_rsp = 0;
  1138. int reply_len;
  1139. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  1140. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  1141. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  1142. (const u8 *) buf, os_strlen(buf));
  1143. } else {
  1144. wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface",
  1145. (const u8 *) buf, os_strlen(buf));
  1146. }
  1147. reply = os_malloc(reply_size);
  1148. if (reply == NULL) {
  1149. *resp_len = 1;
  1150. return NULL;
  1151. }
  1152. os_memcpy(reply, "OK\n", 3);
  1153. reply_len = 3;
  1154. if (os_strcmp(buf, "PING") == 0) {
  1155. os_memcpy(reply, "PONG\n", 5);
  1156. reply_len = 5;
  1157. } else if (os_strcmp(buf, "MIB") == 0) {
  1158. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  1159. if (reply_len >= 0) {
  1160. int res;
  1161. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  1162. reply_size - reply_len);
  1163. if (res < 0)
  1164. reply_len = -1;
  1165. else
  1166. reply_len += res;
  1167. }
  1168. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  1169. reply_len = wpa_supplicant_ctrl_iface_status(
  1170. wpa_s, buf + 6, reply, reply_size);
  1171. } else if (os_strcmp(buf, "PMKSA") == 0) {
  1172. reply_len = pmksa_cache_list(wpa_s->wpa, reply, reply_size);
  1173. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1174. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  1175. reply_len = -1;
  1176. } else if (os_strcmp(buf, "LOGON") == 0) {
  1177. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  1178. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  1179. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  1180. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  1181. wpa_s->disconnected = 0;
  1182. wpa_s->reassociate = 1;
  1183. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1184. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  1185. if (wpa_s->disconnected) {
  1186. wpa_s->disconnected = 0;
  1187. wpa_s->reassociate = 1;
  1188. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1189. }
  1190. #ifdef IEEE8021X_EAPOL
  1191. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  1192. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  1193. reply_len = -1;
  1194. #endif /* IEEE8021X_EAPOL */
  1195. #ifdef CONFIG_PEERKEY
  1196. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  1197. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  1198. reply_len = -1;
  1199. #endif /* CONFIG_PEERKEY */
  1200. #ifdef CONFIG_IEEE80211R
  1201. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  1202. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  1203. reply_len = -1;
  1204. #endif /* CONFIG_IEEE80211R */
  1205. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  1206. {
  1207. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  1208. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  1209. reply_len = -1;
  1210. else
  1211. ctrl_rsp = 1;
  1212. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  1213. if (wpa_supplicant_reload_configuration(wpa_s))
  1214. reply_len = -1;
  1215. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  1216. eloop_terminate();
  1217. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  1218. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  1219. reply_len = -1;
  1220. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  1221. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  1222. wpa_s, reply, reply_size);
  1223. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  1224. wpa_s->reassociate = 0;
  1225. wpa_s->disconnected = 1;
  1226. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1227. } else if (os_strcmp(buf, "SCAN") == 0) {
  1228. wpa_s->scan_req = 2;
  1229. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1230. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  1231. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  1232. wpa_s, reply, reply_size);
  1233. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  1234. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  1235. reply_len = -1;
  1236. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  1237. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  1238. reply_len = -1;
  1239. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  1240. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  1241. reply_len = -1;
  1242. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  1243. reply_len = wpa_supplicant_ctrl_iface_add_network(
  1244. wpa_s, reply, reply_size);
  1245. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  1246. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  1247. reply_len = -1;
  1248. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  1249. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  1250. reply_len = -1;
  1251. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  1252. reply_len = wpa_supplicant_ctrl_iface_get_network(
  1253. wpa_s, buf + 12, reply, reply_size);
  1254. #ifndef CONFIG_NO_CONFIG_WRITE
  1255. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  1256. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  1257. reply_len = -1;
  1258. #endif /* CONFIG_NO_CONFIG_WRITE */
  1259. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  1260. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  1261. wpa_s, buf + 15, reply, reply_size);
  1262. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  1263. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  1264. reply_len = -1;
  1265. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  1266. reply_len = wpa_supplicant_global_iface_interfaces(
  1267. wpa_s->global, reply, reply_size);
  1268. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  1269. reply_len = wpa_supplicant_ctrl_iface_bss(
  1270. wpa_s, buf + 4, reply, reply_size);
  1271. } else {
  1272. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1273. reply_len = 16;
  1274. }
  1275. if (reply_len < 0) {
  1276. os_memcpy(reply, "FAIL\n", 5);
  1277. reply_len = 5;
  1278. }
  1279. if (ctrl_rsp)
  1280. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  1281. *resp_len = reply_len;
  1282. return reply;
  1283. }
  1284. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  1285. char *cmd)
  1286. {
  1287. struct wpa_interface iface;
  1288. char *pos;
  1289. /*
  1290. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  1291. * TAB<bridge_ifname>
  1292. */
  1293. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  1294. os_memset(&iface, 0, sizeof(iface));
  1295. do {
  1296. iface.ifname = pos = cmd;
  1297. pos = os_strchr(pos, '\t');
  1298. if (pos)
  1299. *pos++ = '\0';
  1300. if (iface.ifname[0] == '\0')
  1301. return -1;
  1302. if (pos == NULL)
  1303. break;
  1304. iface.confname = pos;
  1305. pos = os_strchr(pos, '\t');
  1306. if (pos)
  1307. *pos++ = '\0';
  1308. if (iface.confname[0] == '\0')
  1309. iface.confname = NULL;
  1310. if (pos == NULL)
  1311. break;
  1312. iface.driver = pos;
  1313. pos = os_strchr(pos, '\t');
  1314. if (pos)
  1315. *pos++ = '\0';
  1316. if (iface.driver[0] == '\0')
  1317. iface.driver = NULL;
  1318. if (pos == NULL)
  1319. break;
  1320. iface.ctrl_interface = pos;
  1321. pos = os_strchr(pos, '\t');
  1322. if (pos)
  1323. *pos++ = '\0';
  1324. if (iface.ctrl_interface[0] == '\0')
  1325. iface.ctrl_interface = NULL;
  1326. if (pos == NULL)
  1327. break;
  1328. iface.driver_param = pos;
  1329. pos = os_strchr(pos, '\t');
  1330. if (pos)
  1331. *pos++ = '\0';
  1332. if (iface.driver_param[0] == '\0')
  1333. iface.driver_param = NULL;
  1334. if (pos == NULL)
  1335. break;
  1336. iface.bridge_ifname = pos;
  1337. pos = os_strchr(pos, '\t');
  1338. if (pos)
  1339. *pos++ = '\0';
  1340. if (iface.bridge_ifname[0] == '\0')
  1341. iface.bridge_ifname = NULL;
  1342. if (pos == NULL)
  1343. break;
  1344. } while (0);
  1345. if (wpa_supplicant_get_iface(global, iface.ifname))
  1346. return -1;
  1347. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  1348. }
  1349. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  1350. char *cmd)
  1351. {
  1352. struct wpa_supplicant *wpa_s;
  1353. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  1354. wpa_s = wpa_supplicant_get_iface(global, cmd);
  1355. if (wpa_s == NULL)
  1356. return -1;
  1357. return wpa_supplicant_remove_iface(global, wpa_s);
  1358. }
  1359. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  1360. char *buf, int len)
  1361. {
  1362. int res;
  1363. char *pos, *end;
  1364. struct wpa_supplicant *wpa_s;
  1365. wpa_s = global->ifaces;
  1366. pos = buf;
  1367. end = buf + len;
  1368. while (wpa_s) {
  1369. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  1370. if (res < 0 || res >= end - pos) {
  1371. *pos = '\0';
  1372. break;
  1373. }
  1374. pos += res;
  1375. wpa_s = wpa_s->next;
  1376. }
  1377. return pos - buf;
  1378. }
  1379. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  1380. char *buf, size_t *resp_len)
  1381. {
  1382. char *reply;
  1383. const int reply_size = 2048;
  1384. int reply_len;
  1385. wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
  1386. (const u8 *) buf, os_strlen(buf));
  1387. reply = os_malloc(reply_size);
  1388. if (reply == NULL) {
  1389. *resp_len = 1;
  1390. return NULL;
  1391. }
  1392. os_memcpy(reply, "OK\n", 3);
  1393. reply_len = 3;
  1394. if (os_strcmp(buf, "PING") == 0) {
  1395. os_memcpy(reply, "PONG\n", 5);
  1396. reply_len = 5;
  1397. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  1398. if (wpa_supplicant_global_iface_add(global, buf + 14))
  1399. reply_len = -1;
  1400. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  1401. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  1402. reply_len = -1;
  1403. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  1404. reply_len = wpa_supplicant_global_iface_interfaces(
  1405. global, reply, reply_size);
  1406. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  1407. eloop_terminate();
  1408. } else {
  1409. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1410. reply_len = 16;
  1411. }
  1412. if (reply_len < 0) {
  1413. os_memcpy(reply, "FAIL\n", 5);
  1414. reply_len = 5;
  1415. }
  1416. *resp_len = reply_len;
  1417. return reply;
  1418. }