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