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