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