PageRenderTime 69ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/external/bsd/wpa/dist/wpa_supplicant/ctrl_iface.c

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