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