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