blob: 059c586dd3f3bd279b89815665160a9e07943e53 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Control interface (shared code for all backends)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003 * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
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"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070015#include "common/ieee802_11_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070016#include "common/wpa_ctrl.h"
17#include "eap_peer/eap.h"
18#include "eapol_supp/eapol_supp_sm.h"
19#include "rsn_supp/wpa.h"
20#include "rsn_supp/preauth.h"
21#include "rsn_supp/pmksa_cache.h"
22#include "l2_packet/l2_packet.h"
23#include "wps/wps.h"
24#include "config.h"
25#include "wpa_supplicant_i.h"
26#include "driver_i.h"
27#include "wps_supplicant.h"
28#include "ibss_rsn.h"
29#include "ap.h"
30#include "p2p_supplicant.h"
31#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070032#include "hs20_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070033#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "notify.h"
35#include "bss.h"
36#include "scan.h"
37#include "ctrl_iface.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080038#include "interworking.h"
Dmitry Shmidte19501d2011-03-16 14:32:18 -070039#include "blacklist.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080040#include "wpas_glue.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070041#include "autoscan.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042
43extern struct wpa_driver_ops *wpa_drivers[];
44
45static int wpa_supplicant_global_iface_list(struct wpa_global *global,
46 char *buf, int len);
47static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
48 char *buf, int len);
49
50
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080051static int pno_start(struct wpa_supplicant *wpa_s)
52{
53 int ret;
54 size_t i, num_ssid;
55 struct wpa_ssid *ssid;
56 struct wpa_driver_scan_params params;
57
58 if (wpa_s->pno)
59 return 0;
60
61 os_memset(&params, 0, sizeof(params));
62
63 num_ssid = 0;
64 ssid = wpa_s->conf->ssid;
65 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070066 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080067 num_ssid++;
68 ssid = ssid->next;
69 }
70 if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
71 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
72 "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
73 num_ssid = WPAS_MAX_SCAN_SSIDS;
74 }
75
76 if (num_ssid == 0) {
77 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
78 return -1;
79 }
80
81 params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
82 num_ssid);
83 if (params.filter_ssids == NULL)
84 return -1;
85 i = 0;
86 ssid = wpa_s->conf->ssid;
87 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -070088 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080089 params.ssids[i].ssid = ssid->ssid;
90 params.ssids[i].ssid_len = ssid->ssid_len;
91 params.num_ssids++;
92 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
93 ssid->ssid_len);
94 params.filter_ssids[i].ssid_len = ssid->ssid_len;
95 params.num_filter_ssids++;
96 i++;
97 if (i == num_ssid)
98 break;
99 }
100 ssid = ssid->next;
101 }
102
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700103 if (wpa_s->conf->filter_rssi)
104 params.filter_rssi = wpa_s->conf->filter_rssi;
105
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800106 ret = wpa_drv_sched_scan(wpa_s, &params, 10 * 1000);
107 os_free(params.filter_ssids);
108 if (ret == 0)
109 wpa_s->pno = 1;
110 return ret;
111}
112
113
114static int pno_stop(struct wpa_supplicant *wpa_s)
115{
116 if (wpa_s->pno) {
117 wpa_s->pno = 0;
118 return wpa_drv_stop_sched_scan(wpa_s);
119 }
120 return 0;
121}
122
123
Dmitry Shmidt04949592012-07-19 12:16:46 -0700124static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
125{
126 char *pos;
127 u8 addr[ETH_ALEN], *filter = NULL, *n;
128 size_t count = 0;
129
130 pos = val;
131 while (pos) {
132 if (*pos == '\0')
133 break;
134 if (hwaddr_aton(pos, addr)) {
135 os_free(filter);
136 return -1;
137 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700138 n = os_realloc_array(filter, count + 1, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700139 if (n == NULL) {
140 os_free(filter);
141 return -1;
142 }
143 filter = n;
144 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
145 count++;
146
147 pos = os_strchr(pos, ' ');
148 if (pos)
149 pos++;
150 }
151
152 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
153 os_free(wpa_s->bssid_filter);
154 wpa_s->bssid_filter = filter;
155 wpa_s->bssid_filter_count = count;
156
157 return 0;
158}
159
160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
162 char *cmd)
163{
164 char *value;
165 int ret = 0;
166
167 value = os_strchr(cmd, ' ');
168 if (value == NULL)
169 return -1;
170 *value++ = '\0';
171
172 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
173 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
174 eapol_sm_configure(wpa_s->eapol,
175 atoi(value), -1, -1, -1);
176 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
177 eapol_sm_configure(wpa_s->eapol,
178 -1, atoi(value), -1, -1);
179 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
180 eapol_sm_configure(wpa_s->eapol,
181 -1, -1, atoi(value), -1);
182 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
183 eapol_sm_configure(wpa_s->eapol,
184 -1, -1, -1, atoi(value));
185 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
186 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
187 atoi(value)))
188 ret = -1;
189 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
190 0) {
191 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
192 atoi(value)))
193 ret = -1;
194 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
195 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
196 ret = -1;
197 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
198 wpa_s->wps_fragment_size = atoi(value);
199#ifdef CONFIG_WPS_TESTING
200 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
201 long int val;
202 val = strtol(value, NULL, 0);
203 if (val < 0 || val > 0xff) {
204 ret = -1;
205 wpa_printf(MSG_DEBUG, "WPS: Invalid "
206 "wps_version_number %ld", val);
207 } else {
208 wps_version_number = val;
209 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
210 "version %u.%u",
211 (wps_version_number & 0xf0) >> 4,
212 wps_version_number & 0x0f);
213 }
214 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
215 wps_testing_dummy_cred = atoi(value);
216 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
217 wps_testing_dummy_cred);
218#endif /* CONFIG_WPS_TESTING */
219 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
220 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
221 ret = -1;
222#ifdef CONFIG_TDLS_TESTING
223 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
224 extern unsigned int tdls_testing;
225 tdls_testing = strtol(value, NULL, 0);
226 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
227#endif /* CONFIG_TDLS_TESTING */
228#ifdef CONFIG_TDLS
229 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
230 int disabled = atoi(value);
231 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
232 if (disabled) {
233 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
234 ret = -1;
235 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
236 ret = -1;
237 wpa_tdls_enable(wpa_s->wpa, !disabled);
238#endif /* CONFIG_TDLS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800239 } else if (os_strcasecmp(cmd, "pno") == 0) {
240 if (atoi(value))
241 ret = pno_start(wpa_s);
242 else
243 ret = pno_stop(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700244 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
245 int disabled = atoi(value);
246 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
247 ret = -1;
248 else if (disabled)
249 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
250 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
251 if (os_strcmp(value, "disable") == 0)
252 wpa_s->set_sta_uapsd = 0;
253 else {
254 int be, bk, vi, vo;
255 char *pos;
256 /* format: BE,BK,VI,VO;max SP Length */
257 be = atoi(value);
258 pos = os_strchr(value, ',');
259 if (pos == NULL)
260 return -1;
261 pos++;
262 bk = atoi(pos);
263 pos = os_strchr(pos, ',');
264 if (pos == NULL)
265 return -1;
266 pos++;
267 vi = atoi(pos);
268 pos = os_strchr(pos, ',');
269 if (pos == NULL)
270 return -1;
271 pos++;
272 vo = atoi(pos);
273 /* ignore max SP Length for now */
274
275 wpa_s->set_sta_uapsd = 1;
276 wpa_s->sta_uapsd = 0;
277 if (be)
278 wpa_s->sta_uapsd |= BIT(0);
279 if (bk)
280 wpa_s->sta_uapsd |= BIT(1);
281 if (vi)
282 wpa_s->sta_uapsd |= BIT(2);
283 if (vo)
284 wpa_s->sta_uapsd |= BIT(3);
285 }
Jouni Malinen21d6bc82012-04-10 16:17:59 -0700286 } else if (os_strcasecmp(cmd, "ps") == 0) {
287 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700288#ifdef CONFIG_WIFI_DISPLAY
289 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
290 wifi_display_enable(wpa_s->global, !!atoi(value));
291#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700292 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
293 ret = set_bssid_filter(wpa_s, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700294 } else {
295 value[-1] = '=';
296 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
297 if (ret == 0)
298 wpa_supplicant_update_config(wpa_s);
299 }
300
301 return ret;
302}
303
304
305static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
306 char *cmd, char *buf, size_t buflen)
307{
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700308 int res = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309
310 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
311
312 if (os_strcmp(cmd, "version") == 0) {
313 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700314 } else if (os_strcasecmp(cmd, "country") == 0) {
315 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
316 res = os_snprintf(buf, buflen, "%c%c",
317 wpa_s->conf->country[0],
318 wpa_s->conf->country[1]);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700319#ifdef CONFIG_WIFI_DISPLAY
320 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
321 res = os_snprintf(buf, buflen, "%d",
322 wpa_s->global->wifi_display);
323 if (res < 0 || (unsigned int) res >= buflen)
324 return -1;
325 return res;
326#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 }
328
Dmitry Shmidt6f3bdcf2011-04-19 16:42:47 -0700329 if (res < 0 || (unsigned int) res >= buflen)
330 return -1;
331 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332}
333
334
335#ifdef IEEE8021X_EAPOL
336static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
337 char *addr)
338{
339 u8 bssid[ETH_ALEN];
340 struct wpa_ssid *ssid = wpa_s->current_ssid;
341
342 if (hwaddr_aton(addr, bssid)) {
343 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
344 "'%s'", addr);
345 return -1;
346 }
347
348 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
349 rsn_preauth_deinit(wpa_s->wpa);
350 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
351 return -1;
352
353 return 0;
354}
355#endif /* IEEE8021X_EAPOL */
356
357
358#ifdef CONFIG_PEERKEY
359/* MLME-STKSTART.request(peer) */
360static int wpa_supplicant_ctrl_iface_stkstart(
361 struct wpa_supplicant *wpa_s, char *addr)
362{
363 u8 peer[ETH_ALEN];
364
365 if (hwaddr_aton(addr, peer)) {
366 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
367 "address '%s'", addr);
368 return -1;
369 }
370
371 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
372 MAC2STR(peer));
373
374 return wpa_sm_stkstart(wpa_s->wpa, peer);
375}
376#endif /* CONFIG_PEERKEY */
377
378
379#ifdef CONFIG_TDLS
380
381static int wpa_supplicant_ctrl_iface_tdls_discover(
382 struct wpa_supplicant *wpa_s, char *addr)
383{
384 u8 peer[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800385 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386
387 if (hwaddr_aton(addr, peer)) {
388 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
389 "address '%s'", addr);
390 return -1;
391 }
392
393 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
394 MAC2STR(peer));
395
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800396 if (wpa_tdls_is_external_setup(wpa_s->wpa))
397 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
398 else
399 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
400
401 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402}
403
404
405static int wpa_supplicant_ctrl_iface_tdls_setup(
406 struct wpa_supplicant *wpa_s, char *addr)
407{
408 u8 peer[ETH_ALEN];
409 int ret;
410
411 if (hwaddr_aton(addr, peer)) {
412 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
413 "address '%s'", addr);
414 return -1;
415 }
416
417 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
418 MAC2STR(peer));
419
420 ret = wpa_tdls_reneg(wpa_s->wpa, peer);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800421 if (ret) {
422 if (wpa_tdls_is_external_setup(wpa_s->wpa))
423 ret = wpa_tdls_start(wpa_s->wpa, peer);
424 else
425 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
426 }
427
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428 return ret;
429}
430
431
432static int wpa_supplicant_ctrl_iface_tdls_teardown(
433 struct wpa_supplicant *wpa_s, char *addr)
434{
435 u8 peer[ETH_ALEN];
436
437 if (hwaddr_aton(addr, peer)) {
438 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
439 "address '%s'", addr);
440 return -1;
441 }
442
443 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
444 MAC2STR(peer));
445
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800446 return wpa_tdls_teardown_link(wpa_s->wpa, peer,
447 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448}
449
450#endif /* CONFIG_TDLS */
451
452
453#ifdef CONFIG_IEEE80211R
454static int wpa_supplicant_ctrl_iface_ft_ds(
455 struct wpa_supplicant *wpa_s, char *addr)
456{
457 u8 target_ap[ETH_ALEN];
458 struct wpa_bss *bss;
459 const u8 *mdie;
460
461 if (hwaddr_aton(addr, target_ap)) {
462 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
463 "address '%s'", addr);
464 return -1;
465 }
466
467 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
468
469 bss = wpa_bss_get_bssid(wpa_s, target_ap);
470 if (bss)
471 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
472 else
473 mdie = NULL;
474
475 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
476}
477#endif /* CONFIG_IEEE80211R */
478
479
480#ifdef CONFIG_WPS
481static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
482 char *cmd)
483{
484 u8 bssid[ETH_ALEN], *_bssid = bssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700485#ifdef CONFIG_P2P
486 u8 p2p_dev_addr[ETH_ALEN];
487#endif /* CONFIG_P2P */
488#ifdef CONFIG_AP
489 u8 *_p2p_dev_addr = NULL;
490#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700492 if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
493 _bssid = NULL;
494#ifdef CONFIG_P2P
495 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
496 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
497 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
498 "P2P Device Address '%s'",
499 cmd + 13);
500 return -1;
501 }
502 _p2p_dev_addr = p2p_dev_addr;
503#endif /* CONFIG_P2P */
504 } else if (hwaddr_aton(cmd, bssid)) {
505 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
506 cmd);
507 return -1;
508 }
509
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800510#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511 if (wpa_s->ap_iface)
512 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
513#endif /* CONFIG_AP */
514
515 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
516}
517
518
519static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
520 char *cmd, char *buf,
521 size_t buflen)
522{
523 u8 bssid[ETH_ALEN], *_bssid = bssid;
524 char *pin;
525 int ret;
526
527 pin = os_strchr(cmd, ' ');
528 if (pin)
529 *pin++ = '\0';
530
531 if (os_strcmp(cmd, "any") == 0)
532 _bssid = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800533 else if (os_strcmp(cmd, "get") == 0) {
534 ret = wps_generate_pin();
535 goto done;
536 } else if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
538 cmd);
539 return -1;
540 }
541
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800542#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543 if (wpa_s->ap_iface)
544 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
545 buf, buflen);
546#endif /* CONFIG_AP */
547
548 if (pin) {
549 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
550 DEV_PW_DEFAULT);
551 if (ret < 0)
552 return -1;
553 ret = os_snprintf(buf, buflen, "%s", pin);
554 if (ret < 0 || (size_t) ret >= buflen)
555 return -1;
556 return ret;
557 }
558
559 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
560 if (ret < 0)
561 return -1;
562
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800563done:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564 /* Return the generated PIN */
565 ret = os_snprintf(buf, buflen, "%08d", ret);
566 if (ret < 0 || (size_t) ret >= buflen)
567 return -1;
568 return ret;
569}
570
571
572static int wpa_supplicant_ctrl_iface_wps_check_pin(
573 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
574{
575 char pin[9];
576 size_t len;
577 char *pos;
578 int ret;
579
580 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
581 (u8 *) cmd, os_strlen(cmd));
582 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
583 if (*pos < '0' || *pos > '9')
584 continue;
585 pin[len++] = *pos;
586 if (len == 9) {
587 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
588 return -1;
589 }
590 }
591 if (len != 4 && len != 8) {
592 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
593 return -1;
594 }
595 pin[len] = '\0';
596
597 if (len == 8) {
598 unsigned int pin_val;
599 pin_val = atoi(pin);
600 if (!wps_pin_valid(pin_val)) {
601 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
602 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
603 if (ret < 0 || (size_t) ret >= buflen)
604 return -1;
605 return ret;
606 }
607 }
608
609 ret = os_snprintf(buf, buflen, "%s", pin);
610 if (ret < 0 || (size_t) ret >= buflen)
611 return -1;
612
613 return ret;
614}
615
616
617#ifdef CONFIG_WPS_OOB
618static int wpa_supplicant_ctrl_iface_wps_oob(struct wpa_supplicant *wpa_s,
619 char *cmd)
620{
621 char *path, *method, *name;
622
623 path = os_strchr(cmd, ' ');
624 if (path == NULL)
625 return -1;
626 *path++ = '\0';
627
628 method = os_strchr(path, ' ');
629 if (method == NULL)
630 return -1;
631 *method++ = '\0';
632
633 name = os_strchr(method, ' ');
634 if (name != NULL)
635 *name++ = '\0';
636
637 return wpas_wps_start_oob(wpa_s, cmd, path, method, name);
638}
639#endif /* CONFIG_WPS_OOB */
640
641
Dmitry Shmidt04949592012-07-19 12:16:46 -0700642#ifdef CONFIG_WPS_NFC
643
644static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
645 char *cmd)
646{
647 u8 bssid[ETH_ALEN], *_bssid = bssid;
648
649 if (cmd == NULL || cmd[0] == '\0')
650 _bssid = NULL;
651 else if (hwaddr_aton(cmd, bssid))
652 return -1;
653
654 return wpas_wps_start_nfc(wpa_s, _bssid);
655}
656
657
658static int wpa_supplicant_ctrl_iface_wps_nfc_token(
659 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
660{
661 int ndef;
662 struct wpabuf *buf;
663 int res;
664
665 if (os_strcmp(cmd, "WPS") == 0)
666 ndef = 0;
667 else if (os_strcmp(cmd, "NDEF") == 0)
668 ndef = 1;
669 else
670 return -1;
671
672 buf = wpas_wps_nfc_token(wpa_s, ndef);
673 if (buf == NULL)
674 return -1;
675
676 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
677 wpabuf_len(buf));
678 reply[res++] = '\n';
679 reply[res] = '\0';
680
681 wpabuf_free(buf);
682
683 return res;
684}
685
686
687static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
688 struct wpa_supplicant *wpa_s, char *pos)
689{
690 size_t len;
691 struct wpabuf *buf;
692 int ret;
693
694 len = os_strlen(pos);
695 if (len & 0x01)
696 return -1;
697 len /= 2;
698
699 buf = wpabuf_alloc(len);
700 if (buf == NULL)
701 return -1;
702 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
703 wpabuf_free(buf);
704 return -1;
705 }
706
707 ret = wpas_wps_nfc_tag_read(wpa_s, buf);
708 wpabuf_free(buf);
709
710 return ret;
711}
712
713#endif /* CONFIG_WPS_NFC */
714
715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
717 char *cmd)
718{
719 u8 bssid[ETH_ALEN];
720 char *pin;
721 char *new_ssid;
722 char *new_auth;
723 char *new_encr;
724 char *new_key;
725 struct wps_new_ap_settings ap;
726
727 pin = os_strchr(cmd, ' ');
728 if (pin == NULL)
729 return -1;
730 *pin++ = '\0';
731
732 if (hwaddr_aton(cmd, bssid)) {
733 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
734 cmd);
735 return -1;
736 }
737
738 new_ssid = os_strchr(pin, ' ');
739 if (new_ssid == NULL)
740 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
741 *new_ssid++ = '\0';
742
743 new_auth = os_strchr(new_ssid, ' ');
744 if (new_auth == NULL)
745 return -1;
746 *new_auth++ = '\0';
747
748 new_encr = os_strchr(new_auth, ' ');
749 if (new_encr == NULL)
750 return -1;
751 *new_encr++ = '\0';
752
753 new_key = os_strchr(new_encr, ' ');
754 if (new_key == NULL)
755 return -1;
756 *new_key++ = '\0';
757
758 os_memset(&ap, 0, sizeof(ap));
759 ap.ssid_hex = new_ssid;
760 ap.auth = new_auth;
761 ap.encr = new_encr;
762 ap.key_hex = new_key;
763 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
764}
765
766
767#ifdef CONFIG_AP
768static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
769 char *cmd, char *buf,
770 size_t buflen)
771{
772 int timeout = 300;
773 char *pos;
774 const char *pin_txt;
775
776 if (!wpa_s->ap_iface)
777 return -1;
778
779 pos = os_strchr(cmd, ' ');
780 if (pos)
781 *pos++ = '\0';
782
783 if (os_strcmp(cmd, "disable") == 0) {
784 wpas_wps_ap_pin_disable(wpa_s);
785 return os_snprintf(buf, buflen, "OK\n");
786 }
787
788 if (os_strcmp(cmd, "random") == 0) {
789 if (pos)
790 timeout = atoi(pos);
791 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
792 if (pin_txt == NULL)
793 return -1;
794 return os_snprintf(buf, buflen, "%s", pin_txt);
795 }
796
797 if (os_strcmp(cmd, "get") == 0) {
798 pin_txt = wpas_wps_ap_pin_get(wpa_s);
799 if (pin_txt == NULL)
800 return -1;
801 return os_snprintf(buf, buflen, "%s", pin_txt);
802 }
803
804 if (os_strcmp(cmd, "set") == 0) {
805 char *pin;
806 if (pos == NULL)
807 return -1;
808 pin = pos;
809 pos = os_strchr(pos, ' ');
810 if (pos) {
811 *pos++ = '\0';
812 timeout = atoi(pos);
813 }
814 if (os_strlen(pin) > buflen)
815 return -1;
816 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
817 return -1;
818 return os_snprintf(buf, buflen, "%s", pin);
819 }
820
821 return -1;
822}
823#endif /* CONFIG_AP */
824
825
826#ifdef CONFIG_WPS_ER
827static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
828 char *cmd)
829{
830 char *uuid = cmd, *pin, *pos;
831 u8 addr_buf[ETH_ALEN], *addr = NULL;
832 pin = os_strchr(uuid, ' ');
833 if (pin == NULL)
834 return -1;
835 *pin++ = '\0';
836 pos = os_strchr(pin, ' ');
837 if (pos) {
838 *pos++ = '\0';
839 if (hwaddr_aton(pos, addr_buf) == 0)
840 addr = addr_buf;
841 }
842 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
843}
844
845
846static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
847 char *cmd)
848{
849 char *uuid = cmd, *pin;
850 pin = os_strchr(uuid, ' ');
851 if (pin == NULL)
852 return -1;
853 *pin++ = '\0';
854 return wpas_wps_er_learn(wpa_s, uuid, pin);
855}
856
857
858static int wpa_supplicant_ctrl_iface_wps_er_set_config(
859 struct wpa_supplicant *wpa_s, char *cmd)
860{
861 char *uuid = cmd, *id;
862 id = os_strchr(uuid, ' ');
863 if (id == NULL)
864 return -1;
865 *id++ = '\0';
866 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
867}
868
869
870static int wpa_supplicant_ctrl_iface_wps_er_config(
871 struct wpa_supplicant *wpa_s, char *cmd)
872{
873 char *pin;
874 char *new_ssid;
875 char *new_auth;
876 char *new_encr;
877 char *new_key;
878 struct wps_new_ap_settings ap;
879
880 pin = os_strchr(cmd, ' ');
881 if (pin == NULL)
882 return -1;
883 *pin++ = '\0';
884
885 new_ssid = os_strchr(pin, ' ');
886 if (new_ssid == NULL)
887 return -1;
888 *new_ssid++ = '\0';
889
890 new_auth = os_strchr(new_ssid, ' ');
891 if (new_auth == NULL)
892 return -1;
893 *new_auth++ = '\0';
894
895 new_encr = os_strchr(new_auth, ' ');
896 if (new_encr == NULL)
897 return -1;
898 *new_encr++ = '\0';
899
900 new_key = os_strchr(new_encr, ' ');
901 if (new_key == NULL)
902 return -1;
903 *new_key++ = '\0';
904
905 os_memset(&ap, 0, sizeof(ap));
906 ap.ssid_hex = new_ssid;
907 ap.auth = new_auth;
908 ap.encr = new_encr;
909 ap.key_hex = new_key;
910 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
911}
Dmitry Shmidt04949592012-07-19 12:16:46 -0700912
913
914#ifdef CONFIG_WPS_NFC
915static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
916 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
917{
918 int ndef;
919 struct wpabuf *buf;
920 int res;
921 char *uuid;
922
923 uuid = os_strchr(cmd, ' ');
924 if (uuid == NULL)
925 return -1;
926 *uuid++ = '\0';
927
928 if (os_strcmp(cmd, "WPS") == 0)
929 ndef = 0;
930 else if (os_strcmp(cmd, "NDEF") == 0)
931 ndef = 1;
932 else
933 return -1;
934
935 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
936 if (buf == NULL)
937 return -1;
938
939 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
940 wpabuf_len(buf));
941 reply[res++] = '\n';
942 reply[res] = '\0';
943
944 wpabuf_free(buf);
945
946 return res;
947}
948#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949#endif /* CONFIG_WPS_ER */
950
951#endif /* CONFIG_WPS */
952
953
954#ifdef CONFIG_IBSS_RSN
955static int wpa_supplicant_ctrl_iface_ibss_rsn(
956 struct wpa_supplicant *wpa_s, char *addr)
957{
958 u8 peer[ETH_ALEN];
959
960 if (hwaddr_aton(addr, peer)) {
961 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
962 "address '%s'", addr);
963 return -1;
964 }
965
966 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
967 MAC2STR(peer));
968
969 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
970}
971#endif /* CONFIG_IBSS_RSN */
972
973
974static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
975 char *rsp)
976{
977#ifdef IEEE8021X_EAPOL
978 char *pos, *id_pos;
979 int id;
980 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981
982 pos = os_strchr(rsp, '-');
983 if (pos == NULL)
984 return -1;
985 *pos++ = '\0';
986 id_pos = pos;
987 pos = os_strchr(pos, ':');
988 if (pos == NULL)
989 return -1;
990 *pos++ = '\0';
991 id = atoi(id_pos);
992 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
993 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
994 (u8 *) pos, os_strlen(pos));
995
996 ssid = wpa_config_get_network(wpa_s->conf, id);
997 if (ssid == NULL) {
998 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
999 "to update", id);
1000 return -1;
1001 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001003 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1004 pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005#else /* IEEE8021X_EAPOL */
1006 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1007 return -1;
1008#endif /* IEEE8021X_EAPOL */
1009}
1010
1011
1012static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1013 const char *params,
1014 char *buf, size_t buflen)
1015{
1016 char *pos, *end, tmp[30];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001017 int res, verbose, wps, ret;
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001018
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019 verbose = os_strcmp(params, "-VERBOSE") == 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001020 wps = os_strcmp(params, "-WPS") == 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021 pos = buf;
1022 end = buf + buflen;
1023 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1024 struct wpa_ssid *ssid = wpa_s->current_ssid;
1025 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1026 MAC2STR(wpa_s->bssid));
1027 if (ret < 0 || ret >= end - pos)
1028 return pos - buf;
1029 pos += ret;
1030 if (ssid) {
1031 u8 *_ssid = ssid->ssid;
1032 size_t ssid_len = ssid->ssid_len;
1033 u8 ssid_buf[MAX_SSID_LEN];
1034 if (ssid_len == 0) {
1035 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1036 if (_res < 0)
1037 ssid_len = 0;
1038 else
1039 ssid_len = _res;
1040 _ssid = ssid_buf;
1041 }
1042 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1043 wpa_ssid_txt(_ssid, ssid_len),
1044 ssid->id);
1045 if (ret < 0 || ret >= end - pos)
1046 return pos - buf;
1047 pos += ret;
1048
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001049 if (wps && ssid->passphrase &&
1050 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1051 (ssid->mode == WPAS_MODE_AP ||
1052 ssid->mode == WPAS_MODE_P2P_GO)) {
1053 ret = os_snprintf(pos, end - pos,
1054 "passphrase=%s\n",
1055 ssid->passphrase);
1056 if (ret < 0 || ret >= end - pos)
1057 return pos - buf;
1058 pos += ret;
1059 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 if (ssid->id_str) {
1061 ret = os_snprintf(pos, end - pos,
1062 "id_str=%s\n",
1063 ssid->id_str);
1064 if (ret < 0 || ret >= end - pos)
1065 return pos - buf;
1066 pos += ret;
1067 }
1068
1069 switch (ssid->mode) {
1070 case WPAS_MODE_INFRA:
1071 ret = os_snprintf(pos, end - pos,
1072 "mode=station\n");
1073 break;
1074 case WPAS_MODE_IBSS:
1075 ret = os_snprintf(pos, end - pos,
1076 "mode=IBSS\n");
1077 break;
1078 case WPAS_MODE_AP:
1079 ret = os_snprintf(pos, end - pos,
1080 "mode=AP\n");
1081 break;
1082 case WPAS_MODE_P2P_GO:
1083 ret = os_snprintf(pos, end - pos,
1084 "mode=P2P GO\n");
1085 break;
1086 case WPAS_MODE_P2P_GROUP_FORMATION:
1087 ret = os_snprintf(pos, end - pos,
1088 "mode=P2P GO - group "
1089 "formation\n");
1090 break;
1091 default:
1092 ret = 0;
1093 break;
1094 }
1095 if (ret < 0 || ret >= end - pos)
1096 return pos - buf;
1097 pos += ret;
1098 }
1099
1100#ifdef CONFIG_AP
1101 if (wpa_s->ap_iface) {
1102 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1103 end - pos,
1104 verbose);
1105 } else
1106#endif /* CONFIG_AP */
1107 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1108 }
1109 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1110 wpa_supplicant_state_txt(wpa_s->wpa_state));
1111 if (ret < 0 || ret >= end - pos)
1112 return pos - buf;
1113 pos += ret;
1114
1115 if (wpa_s->l2 &&
1116 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1117 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
1118 if (ret < 0 || ret >= end - pos)
1119 return pos - buf;
1120 pos += ret;
1121 }
1122
1123#ifdef CONFIG_P2P
1124 if (wpa_s->global->p2p) {
1125 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1126 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
1127 if (ret < 0 || ret >= end - pos)
1128 return pos - buf;
1129 pos += ret;
1130 }
1131#endif /* CONFIG_P2P */
1132
1133 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1134 MAC2STR(wpa_s->own_addr));
1135 if (ret < 0 || ret >= end - pos)
1136 return pos - buf;
1137 pos += ret;
1138
Dmitry Shmidt04949592012-07-19 12:16:46 -07001139#ifdef CONFIG_HS20
1140 if (wpa_s->current_bss &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001141 wpa_bss_get_vendor_ie(wpa_s->current_bss, HS20_IE_VENDOR_TYPE) &&
1142 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1143 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001144 ret = os_snprintf(pos, end - pos, "hs20=1\n");
1145 if (ret < 0 || ret >= end - pos)
1146 return pos - buf;
1147 pos += ret;
1148 }
1149#endif /* CONFIG_HS20 */
1150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1152 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1153 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1154 verbose);
1155 if (res >= 0)
1156 pos += res;
1157 }
1158
1159 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1160 if (res >= 0)
1161 pos += res;
1162
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001163#ifdef ANDROID
1164 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
Irfan Sherifff20a4432012-04-16 16:48:34 -07001165 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001166 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
Irfan Sherifff20a4432012-04-16 16:48:34 -07001167 wpa_s->wpa_state,
Irfan Sheriffe78e7672012-08-01 11:10:15 -07001168 MAC2STR(wpa_s->bssid),
andy2_kuo5b5fb022012-05-22 11:53:07 -07001169 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1170 wpa_ssid_txt(wpa_s->current_ssid->ssid,
Irfan Sheriff10294772012-05-11 11:23:35 -07001171 wpa_s->current_ssid->ssid_len) : "");
Irfan Sheriffbf5edf42012-01-11 16:54:57 -08001172 if (wpa_s->wpa_state == WPA_COMPLETED) {
1173 struct wpa_ssid *ssid = wpa_s->current_ssid;
1174 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- connection to "
1175 MACSTR " completed %s [id=%d id_str=%s]",
1176 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
1177 "(reauth)" : "(auth)",
1178 ssid ? ssid->id : -1,
1179 ssid && ssid->id_str ? ssid->id_str : "");
1180 }
Dmitry Shmidt2fd7fa62011-12-19 11:19:09 -08001181#endif /* ANDROID */
1182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001183 return pos - buf;
1184}
1185
1186
1187static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1188 char *cmd)
1189{
1190 char *pos;
1191 int id;
1192 struct wpa_ssid *ssid;
1193 u8 bssid[ETH_ALEN];
1194
1195 /* cmd: "<network id> <BSSID>" */
1196 pos = os_strchr(cmd, ' ');
1197 if (pos == NULL)
1198 return -1;
1199 *pos++ = '\0';
1200 id = atoi(cmd);
1201 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1202 if (hwaddr_aton(pos, bssid)) {
1203 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1204 return -1;
1205 }
1206
1207 ssid = wpa_config_get_network(wpa_s->conf, id);
1208 if (ssid == NULL) {
1209 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1210 "to update", id);
1211 return -1;
1212 }
1213
1214 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
1215 ssid->bssid_set = !is_zero_ether_addr(bssid);
1216
1217 return 0;
1218}
1219
1220
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001221static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001222 char *cmd, char *buf,
1223 size_t buflen)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001224{
1225 u8 bssid[ETH_ALEN];
1226 struct wpa_blacklist *e;
1227 char *pos, *end;
1228 int ret;
1229
1230 /* cmd: "BLACKLIST [<BSSID>]" */
1231 if (*cmd == '\0') {
1232 pos = buf;
1233 end = buf + buflen;
1234 e = wpa_s->blacklist;
1235 while (e) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001236 ret = os_snprintf(pos, end - pos, MACSTR "\n",
1237 MAC2STR(e->bssid));
1238 if (ret < 0 || ret >= end - pos)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001239 return pos - buf;
1240 pos += ret;
1241 e = e->next;
1242 }
1243 return pos - buf;
1244 }
1245
1246 cmd++;
1247 if (os_strncmp(cmd, "clear", 5) == 0) {
1248 wpa_blacklist_clear(wpa_s);
1249 os_memcpy(buf, "OK\n", 3);
1250 return 3;
1251 }
1252
1253 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1254 if (hwaddr_aton(cmd, bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001255 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001256 return -1;
1257 }
1258
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001259 /*
1260 * Add the BSSID twice, so its count will be 2, causing it to be
1261 * skipped when processing scan results.
1262 */
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001263 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001264 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001265 return -1;
1266 ret = wpa_blacklist_add(wpa_s, bssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001267 if (ret != 0)
Dmitry Shmidte19501d2011-03-16 14:32:18 -07001268 return -1;
1269 os_memcpy(buf, "OK\n", 3);
1270 return 3;
1271}
1272
1273
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001274extern int wpa_debug_level;
1275extern int wpa_debug_timestamp;
1276
1277static const char * debug_level_str(int level)
1278{
1279 switch (level) {
1280 case MSG_EXCESSIVE:
1281 return "EXCESSIVE";
1282 case MSG_MSGDUMP:
1283 return "MSGDUMP";
1284 case MSG_DEBUG:
1285 return "DEBUG";
1286 case MSG_INFO:
1287 return "INFO";
1288 case MSG_WARNING:
1289 return "WARNING";
1290 case MSG_ERROR:
1291 return "ERROR";
1292 default:
1293 return "?";
1294 }
1295}
1296
1297
1298static int str_to_debug_level(const char *s)
1299{
1300 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1301 return MSG_EXCESSIVE;
1302 if (os_strcasecmp(s, "MSGDUMP") == 0)
1303 return MSG_MSGDUMP;
1304 if (os_strcasecmp(s, "DEBUG") == 0)
1305 return MSG_DEBUG;
1306 if (os_strcasecmp(s, "INFO") == 0)
1307 return MSG_INFO;
1308 if (os_strcasecmp(s, "WARNING") == 0)
1309 return MSG_WARNING;
1310 if (os_strcasecmp(s, "ERROR") == 0)
1311 return MSG_ERROR;
1312 return -1;
1313}
1314
1315
1316static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1317 char *cmd, char *buf,
1318 size_t buflen)
1319{
1320 char *pos, *end, *stamp;
1321 int ret;
1322
1323 if (cmd == NULL) {
1324 return -1;
1325 }
1326
1327 /* cmd: "LOG_LEVEL [<level>]" */
1328 if (*cmd == '\0') {
1329 pos = buf;
1330 end = buf + buflen;
1331 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1332 "Timestamp: %d\n",
1333 debug_level_str(wpa_debug_level),
1334 wpa_debug_timestamp);
1335 if (ret < 0 || ret >= end - pos)
1336 ret = 0;
1337
1338 return ret;
1339 }
1340
1341 while (*cmd == ' ')
1342 cmd++;
1343
1344 stamp = os_strchr(cmd, ' ');
1345 if (stamp) {
1346 *stamp++ = '\0';
1347 while (*stamp == ' ') {
1348 stamp++;
1349 }
1350 }
1351
1352 if (cmd && os_strlen(cmd)) {
1353 int level = str_to_debug_level(cmd);
1354 if (level < 0)
1355 return -1;
1356 wpa_debug_level = level;
1357 }
1358
1359 if (stamp && os_strlen(stamp))
1360 wpa_debug_timestamp = atoi(stamp);
1361
1362 os_memcpy(buf, "OK\n", 3);
1363 return 3;
1364}
1365
1366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367static int wpa_supplicant_ctrl_iface_list_networks(
1368 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1369{
1370 char *pos, *end;
1371 struct wpa_ssid *ssid;
1372 int ret;
1373
1374 pos = buf;
1375 end = buf + buflen;
1376 ret = os_snprintf(pos, end - pos,
1377 "network id / ssid / bssid / flags\n");
1378 if (ret < 0 || ret >= end - pos)
1379 return pos - buf;
1380 pos += ret;
1381
1382 ssid = wpa_s->conf->ssid;
1383 while (ssid) {
1384 ret = os_snprintf(pos, end - pos, "%d\t%s",
1385 ssid->id,
1386 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1387 if (ret < 0 || ret >= end - pos)
1388 return pos - buf;
1389 pos += ret;
1390 if (ssid->bssid_set) {
1391 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
1392 MAC2STR(ssid->bssid));
1393 } else {
1394 ret = os_snprintf(pos, end - pos, "\tany");
1395 }
1396 if (ret < 0 || ret >= end - pos)
1397 return pos - buf;
1398 pos += ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001399 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 ssid == wpa_s->current_ssid ?
1401 "[CURRENT]" : "",
1402 ssid->disabled ? "[DISABLED]" : "",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001403 ssid->disabled_until.sec ?
1404 "[TEMP-DISABLED]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
1406 "");
1407 if (ret < 0 || ret >= end - pos)
1408 return pos - buf;
1409 pos += ret;
1410 ret = os_snprintf(pos, end - pos, "\n");
1411 if (ret < 0 || ret >= end - pos)
1412 return pos - buf;
1413 pos += ret;
1414
1415 ssid = ssid->next;
1416 }
1417
1418 return pos - buf;
1419}
1420
1421
1422static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
1423{
1424 int first = 1, ret;
1425 ret = os_snprintf(pos, end - pos, "-");
1426 if (ret < 0 || ret >= end - pos)
1427 return pos;
1428 pos += ret;
1429 if (cipher & WPA_CIPHER_NONE) {
1430 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
1431 if (ret < 0 || ret >= end - pos)
1432 return pos;
1433 pos += ret;
1434 first = 0;
1435 }
1436 if (cipher & WPA_CIPHER_WEP40) {
1437 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
1438 if (ret < 0 || ret >= end - pos)
1439 return pos;
1440 pos += ret;
1441 first = 0;
1442 }
1443 if (cipher & WPA_CIPHER_WEP104) {
1444 ret = os_snprintf(pos, end - pos, "%sWEP104",
1445 first ? "" : "+");
1446 if (ret < 0 || ret >= end - pos)
1447 return pos;
1448 pos += ret;
1449 first = 0;
1450 }
1451 if (cipher & WPA_CIPHER_TKIP) {
1452 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
1453 if (ret < 0 || ret >= end - pos)
1454 return pos;
1455 pos += ret;
1456 first = 0;
1457 }
1458 if (cipher & WPA_CIPHER_CCMP) {
1459 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
1460 if (ret < 0 || ret >= end - pos)
1461 return pos;
1462 pos += ret;
1463 first = 0;
1464 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001465 if (cipher & WPA_CIPHER_GCMP) {
1466 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : "+");
1467 if (ret < 0 || ret >= end - pos)
1468 return pos;
1469 pos += ret;
1470 first = 0;
1471 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472 return pos;
1473}
1474
1475
1476static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
1477 const u8 *ie, size_t ie_len)
1478{
1479 struct wpa_ie_data data;
1480 int first, ret;
1481
1482 ret = os_snprintf(pos, end - pos, "[%s-", proto);
1483 if (ret < 0 || ret >= end - pos)
1484 return pos;
1485 pos += ret;
1486
1487 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
1488 ret = os_snprintf(pos, end - pos, "?]");
1489 if (ret < 0 || ret >= end - pos)
1490 return pos;
1491 pos += ret;
1492 return pos;
1493 }
1494
1495 first = 1;
1496 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1497 ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
1498 if (ret < 0 || ret >= end - pos)
1499 return pos;
1500 pos += ret;
1501 first = 0;
1502 }
1503 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
1504 ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
1505 if (ret < 0 || ret >= end - pos)
1506 return pos;
1507 pos += ret;
1508 first = 0;
1509 }
1510 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
1511 ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
1512 if (ret < 0 || ret >= end - pos)
1513 return pos;
1514 pos += ret;
1515 first = 0;
1516 }
1517#ifdef CONFIG_IEEE80211R
1518 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1519 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
1520 first ? "" : "+");
1521 if (ret < 0 || ret >= end - pos)
1522 return pos;
1523 pos += ret;
1524 first = 0;
1525 }
1526 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1527 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
1528 first ? "" : "+");
1529 if (ret < 0 || ret >= end - pos)
1530 return pos;
1531 pos += ret;
1532 first = 0;
1533 }
1534#endif /* CONFIG_IEEE80211R */
1535#ifdef CONFIG_IEEE80211W
1536 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1537 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
1538 first ? "" : "+");
1539 if (ret < 0 || ret >= end - pos)
1540 return pos;
1541 pos += ret;
1542 first = 0;
1543 }
1544 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1545 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
1546 first ? "" : "+");
1547 if (ret < 0 || ret >= end - pos)
1548 return pos;
1549 pos += ret;
1550 first = 0;
1551 }
1552#endif /* CONFIG_IEEE80211W */
1553
1554 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
1555
1556 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
1557 ret = os_snprintf(pos, end - pos, "-preauth");
1558 if (ret < 0 || ret >= end - pos)
1559 return pos;
1560 pos += ret;
1561 }
1562
1563 ret = os_snprintf(pos, end - pos, "]");
1564 if (ret < 0 || ret >= end - pos)
1565 return pos;
1566 pos += ret;
1567
1568 return pos;
1569}
1570
1571
1572#ifdef CONFIG_WPS
1573static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
1574 char *pos, char *end,
1575 struct wpabuf *wps_ie)
1576{
1577 int ret;
1578 const char *txt;
1579
1580 if (wps_ie == NULL)
1581 return pos;
1582 if (wps_is_selected_pbc_registrar(wps_ie))
1583 txt = "[WPS-PBC]";
1584#ifdef CONFIG_WPS2
1585 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
1586 txt = "[WPS-AUTH]";
1587#endif /* CONFIG_WPS2 */
1588 else if (wps_is_selected_pin_registrar(wps_ie))
1589 txt = "[WPS-PIN]";
1590 else
1591 txt = "[WPS]";
1592
1593 ret = os_snprintf(pos, end - pos, "%s", txt);
1594 if (ret >= 0 && ret < end - pos)
1595 pos += ret;
1596 wpabuf_free(wps_ie);
1597 return pos;
1598}
1599#endif /* CONFIG_WPS */
1600
1601
1602static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
1603 char *pos, char *end,
1604 const struct wpa_bss *bss)
1605{
1606#ifdef CONFIG_WPS
1607 struct wpabuf *wps_ie;
1608 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
1609 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
1610#else /* CONFIG_WPS */
1611 return pos;
1612#endif /* CONFIG_WPS */
1613}
1614
1615
1616/* Format one result on one text line into a buffer. */
1617static int wpa_supplicant_ctrl_iface_scan_result(
1618 struct wpa_supplicant *wpa_s,
1619 const struct wpa_bss *bss, char *buf, size_t buflen)
1620{
1621 char *pos, *end;
1622 int ret;
1623 const u8 *ie, *ie2, *p2p;
1624
1625 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
1626 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
1627 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
1628 0)
1629 return 0; /* Do not show P2P listen discovery results here */
1630
1631 pos = buf;
1632 end = buf + buflen;
1633
1634 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
1635 MAC2STR(bss->bssid), bss->freq, bss->level);
1636 if (ret < 0 || ret >= end - pos)
1637 return -1;
1638 pos += ret;
1639 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1640 if (ie)
1641 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
1642 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1643 if (ie2)
1644 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
1645 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
1646 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
1647 ret = os_snprintf(pos, end - pos, "[WEP]");
1648 if (ret < 0 || ret >= end - pos)
1649 return -1;
1650 pos += ret;
1651 }
1652 if (bss->caps & IEEE80211_CAP_IBSS) {
1653 ret = os_snprintf(pos, end - pos, "[IBSS]");
1654 if (ret < 0 || ret >= end - pos)
1655 return -1;
1656 pos += ret;
1657 }
1658 if (bss->caps & IEEE80211_CAP_ESS) {
1659 ret = os_snprintf(pos, end - pos, "[ESS]");
1660 if (ret < 0 || ret >= end - pos)
1661 return -1;
1662 pos += ret;
1663 }
1664 if (p2p) {
1665 ret = os_snprintf(pos, end - pos, "[P2P]");
1666 if (ret < 0 || ret >= end - pos)
1667 return -1;
1668 pos += ret;
1669 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001670#ifdef CONFIG_HS20
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001671 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001672 ret = os_snprintf(pos, end - pos, "[HS20]");
1673 if (ret < 0 || ret >= end - pos)
1674 return -1;
1675 pos += ret;
1676 }
1677#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678
1679 ret = os_snprintf(pos, end - pos, "\t%s",
1680 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1681 if (ret < 0 || ret >= end - pos)
1682 return -1;
1683 pos += ret;
1684
1685 ret = os_snprintf(pos, end - pos, "\n");
1686 if (ret < 0 || ret >= end - pos)
1687 return -1;
1688 pos += ret;
1689
1690 return pos - buf;
1691}
1692
1693
1694static int wpa_supplicant_ctrl_iface_scan_results(
1695 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1696{
1697 char *pos, *end;
1698 struct wpa_bss *bss;
1699 int ret;
1700
1701 pos = buf;
1702 end = buf + buflen;
1703 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
1704 "flags / ssid\n");
1705 if (ret < 0 || ret >= end - pos)
1706 return pos - buf;
1707 pos += ret;
1708
1709 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
1710 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
1711 end - pos);
1712 if (ret < 0 || ret >= end - pos)
1713 return pos - buf;
1714 pos += ret;
1715 }
1716
1717 return pos - buf;
1718}
1719
1720
1721static int wpa_supplicant_ctrl_iface_select_network(
1722 struct wpa_supplicant *wpa_s, char *cmd)
1723{
1724 int id;
1725 struct wpa_ssid *ssid;
1726
1727 /* cmd: "<network id>" or "any" */
1728 if (os_strcmp(cmd, "any") == 0) {
1729 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
1730 ssid = NULL;
1731 } else {
1732 id = atoi(cmd);
1733 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
1734
1735 ssid = wpa_config_get_network(wpa_s->conf, id);
1736 if (ssid == NULL) {
1737 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
1738 "network id=%d", id);
1739 return -1;
1740 }
1741 if (ssid->disabled == 2) {
1742 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
1743 "SELECT_NETWORK with persistent P2P group");
1744 return -1;
1745 }
1746 }
1747
1748 wpa_supplicant_select_network(wpa_s, ssid);
1749
1750 return 0;
1751}
1752
1753
1754static int wpa_supplicant_ctrl_iface_enable_network(
1755 struct wpa_supplicant *wpa_s, char *cmd)
1756{
1757 int id;
1758 struct wpa_ssid *ssid;
1759
1760 /* cmd: "<network id>" or "all" */
1761 if (os_strcmp(cmd, "all") == 0) {
1762 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
1763 ssid = NULL;
1764 } else {
1765 id = atoi(cmd);
1766 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
1767
1768 ssid = wpa_config_get_network(wpa_s->conf, id);
1769 if (ssid == NULL) {
1770 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
1771 "network id=%d", id);
1772 return -1;
1773 }
1774 if (ssid->disabled == 2) {
1775 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
1776 "ENABLE_NETWORK with persistent P2P group");
1777 return -1;
1778 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001779
1780 if (os_strstr(cmd, " no-connect")) {
1781 ssid->disabled = 0;
1782 return 0;
1783 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001784 }
1785 wpa_supplicant_enable_network(wpa_s, ssid);
1786
1787 return 0;
1788}
1789
1790
1791static int wpa_supplicant_ctrl_iface_disable_network(
1792 struct wpa_supplicant *wpa_s, char *cmd)
1793{
1794 int id;
1795 struct wpa_ssid *ssid;
1796
1797 /* cmd: "<network id>" or "all" */
1798 if (os_strcmp(cmd, "all") == 0) {
1799 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
1800 ssid = NULL;
1801 } else {
1802 id = atoi(cmd);
1803 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
1804
1805 ssid = wpa_config_get_network(wpa_s->conf, id);
1806 if (ssid == NULL) {
1807 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
1808 "network id=%d", id);
1809 return -1;
1810 }
1811 if (ssid->disabled == 2) {
1812 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
1813 "DISABLE_NETWORK with persistent P2P "
1814 "group");
1815 return -1;
1816 }
1817 }
1818 wpa_supplicant_disable_network(wpa_s, ssid);
1819
1820 return 0;
1821}
1822
1823
1824static int wpa_supplicant_ctrl_iface_add_network(
1825 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1826{
1827 struct wpa_ssid *ssid;
1828 int ret;
1829
1830 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
1831
1832 ssid = wpa_config_add_network(wpa_s->conf);
1833 if (ssid == NULL)
1834 return -1;
1835
1836 wpas_notify_network_added(wpa_s, ssid);
1837
1838 ssid->disabled = 1;
1839 wpa_config_set_network_defaults(ssid);
1840
1841 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
1842 if (ret < 0 || (size_t) ret >= buflen)
1843 return -1;
1844 return ret;
1845}
1846
1847
1848static int wpa_supplicant_ctrl_iface_remove_network(
1849 struct wpa_supplicant *wpa_s, char *cmd)
1850{
1851 int id;
1852 struct wpa_ssid *ssid;
1853
1854 /* cmd: "<network id>" or "all" */
1855 if (os_strcmp(cmd, "all") == 0) {
1856 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
1857 ssid = wpa_s->conf->ssid;
1858 while (ssid) {
1859 struct wpa_ssid *remove_ssid = ssid;
1860 id = ssid->id;
1861 ssid = ssid->next;
1862 wpas_notify_network_removed(wpa_s, remove_ssid);
1863 wpa_config_remove_network(wpa_s->conf, id);
1864 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001865 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001866 if (wpa_s->current_ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001867#ifdef CONFIG_SME
1868 wpa_s->sme.prev_bssid_set = 0;
1869#endif /* CONFIG_SME */
Jouni Malinen75ecf522011-06-27 15:19:46 -07001870 wpa_sm_set_config(wpa_s->wpa, NULL);
1871 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872 wpa_supplicant_disassociate(wpa_s,
1873 WLAN_REASON_DEAUTH_LEAVING);
1874 }
1875 return 0;
1876 }
1877
1878 id = atoi(cmd);
1879 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
1880
1881 ssid = wpa_config_get_network(wpa_s->conf, id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001882 if (ssid)
1883 wpas_notify_network_removed(wpa_s, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884 if (ssid == NULL ||
1885 wpa_config_remove_network(wpa_s->conf, id) < 0) {
1886 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1887 "id=%d", id);
1888 return -1;
1889 }
1890
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001891 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001892#ifdef CONFIG_SME
1893 wpa_s->sme.prev_bssid_set = 0;
1894#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001895 /*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001896 * Invalidate the EAP session cache if the current or
1897 * previously used network is removed.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898 */
1899 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001900 }
1901
1902 if (ssid == wpa_s->current_ssid) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001903 wpa_sm_set_config(wpa_s->wpa, NULL);
1904 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001905
1906 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1907 }
1908
1909 return 0;
1910}
1911
1912
1913static int wpa_supplicant_ctrl_iface_set_network(
1914 struct wpa_supplicant *wpa_s, char *cmd)
1915{
1916 int id;
1917 struct wpa_ssid *ssid;
1918 char *name, *value;
1919
1920 /* cmd: "<network id> <variable name> <value>" */
1921 name = os_strchr(cmd, ' ');
1922 if (name == NULL)
1923 return -1;
1924 *name++ = '\0';
1925
1926 value = os_strchr(name, ' ');
1927 if (value == NULL)
1928 return -1;
1929 *value++ = '\0';
1930
1931 id = atoi(cmd);
1932 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
1933 id, name);
1934 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1935 (u8 *) value, os_strlen(value));
1936
1937 ssid = wpa_config_get_network(wpa_s->conf, id);
1938 if (ssid == NULL) {
1939 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1940 "id=%d", id);
1941 return -1;
1942 }
1943
1944 if (wpa_config_set(ssid, name, value, 0) < 0) {
1945 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
1946 "variable '%s'", name);
1947 return -1;
1948 }
1949
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001950 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
1951
1952 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953 /*
1954 * Invalidate the EAP session cache if anything in the current
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001955 * or previously used configuration changes.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001956 */
1957 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1958 }
1959
1960 if ((os_strcmp(name, "psk") == 0 &&
1961 value[0] == '"' && ssid->ssid_len) ||
1962 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
1963 wpa_config_update_psk(ssid);
1964 else if (os_strcmp(name, "priority") == 0)
1965 wpa_config_update_prio_list(wpa_s->conf);
1966
1967 return 0;
1968}
1969
1970
1971static int wpa_supplicant_ctrl_iface_get_network(
1972 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1973{
1974 int id;
1975 size_t res;
1976 struct wpa_ssid *ssid;
1977 char *name, *value;
1978
1979 /* cmd: "<network id> <variable name>" */
1980 name = os_strchr(cmd, ' ');
1981 if (name == NULL || buflen == 0)
1982 return -1;
1983 *name++ = '\0';
1984
1985 id = atoi(cmd);
1986 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
1987 id, name);
1988
1989 ssid = wpa_config_get_network(wpa_s->conf, id);
1990 if (ssid == NULL) {
1991 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
1992 "id=%d", id);
1993 return -1;
1994 }
1995
1996 value = wpa_config_get_no_key(ssid, name);
1997 if (value == NULL) {
1998 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
1999 "variable '%s'", name);
2000 return -1;
2001 }
2002
2003 res = os_strlcpy(buf, value, buflen);
2004 if (res >= buflen) {
2005 os_free(value);
2006 return -1;
2007 }
2008
2009 os_free(value);
2010
2011 return res;
2012}
2013
2014
Dmitry Shmidt04949592012-07-19 12:16:46 -07002015static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2016 char *buf, size_t buflen)
2017{
2018 char *pos, *end;
2019 struct wpa_cred *cred;
2020 int ret;
2021
2022 pos = buf;
2023 end = buf + buflen;
2024 ret = os_snprintf(pos, end - pos,
2025 "cred id / realm / username / domain / imsi\n");
2026 if (ret < 0 || ret >= end - pos)
2027 return pos - buf;
2028 pos += ret;
2029
2030 cred = wpa_s->conf->cred;
2031 while (cred) {
2032 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2033 cred->id, cred->realm ? cred->realm : "",
2034 cred->username ? cred->username : "",
2035 cred->domain ? cred->domain : "",
2036 cred->imsi ? cred->imsi : "");
2037 if (ret < 0 || ret >= end - pos)
2038 return pos - buf;
2039 pos += ret;
2040
2041 cred = cred->next;
2042 }
2043
2044 return pos - buf;
2045}
2046
2047
2048static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2049 char *buf, size_t buflen)
2050{
2051 struct wpa_cred *cred;
2052 int ret;
2053
2054 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2055
2056 cred = wpa_config_add_cred(wpa_s->conf);
2057 if (cred == NULL)
2058 return -1;
2059
2060 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
2061 if (ret < 0 || (size_t) ret >= buflen)
2062 return -1;
2063 return ret;
2064}
2065
2066
2067static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
2068 char *cmd)
2069{
2070 int id;
2071 struct wpa_cred *cred;
2072
2073 /* cmd: "<cred id>" or "all" */
2074 if (os_strcmp(cmd, "all") == 0) {
2075 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
2076 cred = wpa_s->conf->cred;
2077 while (cred) {
2078 id = cred->id;
2079 cred = cred->next;
2080 wpa_config_remove_cred(wpa_s->conf, id);
2081 }
2082 return 0;
2083 }
2084
2085 id = atoi(cmd);
2086 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
2087
2088 cred = wpa_config_get_cred(wpa_s->conf, id);
2089 if (cred == NULL ||
2090 wpa_config_remove_cred(wpa_s->conf, id) < 0) {
2091 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2092 id);
2093 return -1;
2094 }
2095
2096 return 0;
2097}
2098
2099
2100static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
2101 char *cmd)
2102{
2103 int id;
2104 struct wpa_cred *cred;
2105 char *name, *value;
2106
2107 /* cmd: "<cred id> <variable name> <value>" */
2108 name = os_strchr(cmd, ' ');
2109 if (name == NULL)
2110 return -1;
2111 *name++ = '\0';
2112
2113 value = os_strchr(name, ' ');
2114 if (value == NULL)
2115 return -1;
2116 *value++ = '\0';
2117
2118 id = atoi(cmd);
2119 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
2120 id, name);
2121 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2122 (u8 *) value, os_strlen(value));
2123
2124 cred = wpa_config_get_cred(wpa_s->conf, id);
2125 if (cred == NULL) {
2126 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
2127 id);
2128 return -1;
2129 }
2130
2131 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
2132 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
2133 "variable '%s'", name);
2134 return -1;
2135 }
2136
2137 return 0;
2138}
2139
2140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002141#ifndef CONFIG_NO_CONFIG_WRITE
2142static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
2143{
2144 int ret;
2145
2146 if (!wpa_s->conf->update_config) {
2147 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
2148 "to update configuration (update_config=0)");
2149 return -1;
2150 }
2151
2152 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
2153 if (ret) {
2154 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
2155 "update configuration");
2156 } else {
2157 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
2158 " updated");
2159 }
2160
2161 return ret;
2162}
2163#endif /* CONFIG_NO_CONFIG_WRITE */
2164
2165
2166static int ctrl_iface_get_capability_pairwise(int res, char *strict,
2167 struct wpa_driver_capa *capa,
2168 char *buf, size_t buflen)
2169{
2170 int ret, first = 1;
2171 char *pos, *end;
2172 size_t len;
2173
2174 pos = buf;
2175 end = pos + buflen;
2176
2177 if (res < 0) {
2178 if (strict)
2179 return 0;
2180 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
2181 if (len >= buflen)
2182 return -1;
2183 return len;
2184 }
2185
2186 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2187 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2188 if (ret < 0 || ret >= end - pos)
2189 return pos - buf;
2190 pos += ret;
2191 first = 0;
2192 }
2193
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002194 if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2195 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
2196 if (ret < 0 || ret >= end - pos)
2197 return pos - buf;
2198 pos += ret;
2199 first = 0;
2200 }
2201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2203 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2204 if (ret < 0 || ret >= end - pos)
2205 return pos - buf;
2206 pos += ret;
2207 first = 0;
2208 }
2209
2210 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2211 ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
2212 if (ret < 0 || ret >= end - pos)
2213 return pos - buf;
2214 pos += ret;
2215 first = 0;
2216 }
2217
2218 return pos - buf;
2219}
2220
2221
2222static int ctrl_iface_get_capability_group(int res, char *strict,
2223 struct wpa_driver_capa *capa,
2224 char *buf, size_t buflen)
2225{
2226 int ret, first = 1;
2227 char *pos, *end;
2228 size_t len;
2229
2230 pos = buf;
2231 end = pos + buflen;
2232
2233 if (res < 0) {
2234 if (strict)
2235 return 0;
2236 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
2237 if (len >= buflen)
2238 return -1;
2239 return len;
2240 }
2241
2242 if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2243 ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
2244 if (ret < 0 || ret >= end - pos)
2245 return pos - buf;
2246 pos += ret;
2247 first = 0;
2248 }
2249
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002250 if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2251 ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
2252 if (ret < 0 || ret >= end - pos)
2253 return pos - buf;
2254 pos += ret;
2255 first = 0;
2256 }
2257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002258 if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2259 ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
2260 if (ret < 0 || ret >= end - pos)
2261 return pos - buf;
2262 pos += ret;
2263 first = 0;
2264 }
2265
2266 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
2267 ret = os_snprintf(pos, end - pos, "%sWEP104",
2268 first ? "" : " ");
2269 if (ret < 0 || ret >= end - pos)
2270 return pos - buf;
2271 pos += ret;
2272 first = 0;
2273 }
2274
2275 if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
2276 ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
2277 if (ret < 0 || ret >= end - pos)
2278 return pos - buf;
2279 pos += ret;
2280 first = 0;
2281 }
2282
2283 return pos - buf;
2284}
2285
2286
2287static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
2288 struct wpa_driver_capa *capa,
2289 char *buf, size_t buflen)
2290{
2291 int ret;
2292 char *pos, *end;
2293 size_t len;
2294
2295 pos = buf;
2296 end = pos + buflen;
2297
2298 if (res < 0) {
2299 if (strict)
2300 return 0;
2301 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
2302 "NONE", buflen);
2303 if (len >= buflen)
2304 return -1;
2305 return len;
2306 }
2307
2308 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
2309 if (ret < 0 || ret >= end - pos)
2310 return pos - buf;
2311 pos += ret;
2312
2313 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2314 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
2315 ret = os_snprintf(pos, end - pos, " WPA-EAP");
2316 if (ret < 0 || ret >= end - pos)
2317 return pos - buf;
2318 pos += ret;
2319 }
2320
2321 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2322 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2323 ret = os_snprintf(pos, end - pos, " WPA-PSK");
2324 if (ret < 0 || ret >= end - pos)
2325 return pos - buf;
2326 pos += ret;
2327 }
2328
2329 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2330 ret = os_snprintf(pos, end - pos, " WPA-NONE");
2331 if (ret < 0 || ret >= end - pos)
2332 return pos - buf;
2333 pos += ret;
2334 }
2335
2336 return pos - buf;
2337}
2338
2339
2340static int ctrl_iface_get_capability_proto(int res, char *strict,
2341 struct wpa_driver_capa *capa,
2342 char *buf, size_t buflen)
2343{
2344 int ret, first = 1;
2345 char *pos, *end;
2346 size_t len;
2347
2348 pos = buf;
2349 end = pos + buflen;
2350
2351 if (res < 0) {
2352 if (strict)
2353 return 0;
2354 len = os_strlcpy(buf, "RSN WPA", buflen);
2355 if (len >= buflen)
2356 return -1;
2357 return len;
2358 }
2359
2360 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2361 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2362 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
2363 if (ret < 0 || ret >= end - pos)
2364 return pos - buf;
2365 pos += ret;
2366 first = 0;
2367 }
2368
2369 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2370 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
2371 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
2372 if (ret < 0 || ret >= end - pos)
2373 return pos - buf;
2374 pos += ret;
2375 first = 0;
2376 }
2377
2378 return pos - buf;
2379}
2380
2381
2382static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
2383 struct wpa_driver_capa *capa,
2384 char *buf, size_t buflen)
2385{
2386 int ret, first = 1;
2387 char *pos, *end;
2388 size_t len;
2389
2390 pos = buf;
2391 end = pos + buflen;
2392
2393 if (res < 0) {
2394 if (strict)
2395 return 0;
2396 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
2397 if (len >= buflen)
2398 return -1;
2399 return len;
2400 }
2401
2402 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
2403 ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
2404 if (ret < 0 || ret >= end - pos)
2405 return pos - buf;
2406 pos += ret;
2407 first = 0;
2408 }
2409
2410 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
2411 ret = os_snprintf(pos, end - pos, "%sSHARED",
2412 first ? "" : " ");
2413 if (ret < 0 || ret >= end - pos)
2414 return pos - buf;
2415 pos += ret;
2416 first = 0;
2417 }
2418
2419 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
2420 ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
2421 if (ret < 0 || ret >= end - pos)
2422 return pos - buf;
2423 pos += ret;
2424 first = 0;
2425 }
2426
2427 return pos - buf;
2428}
2429
2430
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002431static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
2432 char *buf, size_t buflen)
2433{
2434 struct hostapd_channel_data *chnl;
2435 int ret, i, j;
2436 char *pos, *end, *hmode;
2437
2438 pos = buf;
2439 end = pos + buflen;
2440
2441 for (j = 0; j < wpa_s->hw.num_modes; j++) {
2442 switch (wpa_s->hw.modes[j].mode) {
2443 case HOSTAPD_MODE_IEEE80211B:
2444 hmode = "B";
2445 break;
2446 case HOSTAPD_MODE_IEEE80211G:
2447 hmode = "G";
2448 break;
2449 case HOSTAPD_MODE_IEEE80211A:
2450 hmode = "A";
2451 break;
2452 default:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002453 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002454 }
2455 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
2456 if (ret < 0 || ret >= end - pos)
2457 return pos - buf;
2458 pos += ret;
2459 chnl = wpa_s->hw.modes[j].channels;
2460 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002461 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
2462 continue;
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002463 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
2464 if (ret < 0 || ret >= end - pos)
2465 return pos - buf;
2466 pos += ret;
2467 }
2468 ret = os_snprintf(pos, end - pos, "\n");
2469 if (ret < 0 || ret >= end - pos)
2470 return pos - buf;
2471 pos += ret;
2472 }
2473
2474 return pos - buf;
2475}
2476
2477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002478static int wpa_supplicant_ctrl_iface_get_capability(
2479 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
2480 size_t buflen)
2481{
2482 struct wpa_driver_capa capa;
2483 int res;
2484 char *strict;
2485 char field[30];
2486 size_t len;
2487
2488 /* Determine whether or not strict checking was requested */
2489 len = os_strlcpy(field, _field, sizeof(field));
2490 if (len >= sizeof(field))
2491 return -1;
2492 strict = os_strchr(field, ' ');
2493 if (strict != NULL) {
2494 *strict++ = '\0';
2495 if (os_strcmp(strict, "strict") != 0)
2496 return -1;
2497 }
2498
2499 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
2500 field, strict ? strict : "");
2501
2502 if (os_strcmp(field, "eap") == 0) {
2503 return eap_get_names(buf, buflen);
2504 }
2505
2506 res = wpa_drv_get_capa(wpa_s, &capa);
2507
2508 if (os_strcmp(field, "pairwise") == 0)
2509 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
2510 buf, buflen);
2511
2512 if (os_strcmp(field, "group") == 0)
2513 return ctrl_iface_get_capability_group(res, strict, &capa,
2514 buf, buflen);
2515
2516 if (os_strcmp(field, "key_mgmt") == 0)
2517 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
2518 buf, buflen);
2519
2520 if (os_strcmp(field, "proto") == 0)
2521 return ctrl_iface_get_capability_proto(res, strict, &capa,
2522 buf, buflen);
2523
2524 if (os_strcmp(field, "auth_alg") == 0)
2525 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
2526 buf, buflen);
2527
Dmitry Shmidt0e6d08e2012-07-10 12:49:30 -07002528 if (os_strcmp(field, "channels") == 0)
2529 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
2530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002531 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
2532 field);
2533
2534 return -1;
2535}
2536
2537
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002538#ifdef CONFIG_INTERWORKING
2539static char * anqp_add_hex(char *pos, char *end, const char *title,
2540 struct wpabuf *data)
2541{
2542 char *start = pos;
2543 size_t i;
2544 int ret;
2545 const u8 *d;
2546
2547 if (data == NULL)
2548 return start;
2549
2550 ret = os_snprintf(pos, end - pos, "%s=", title);
2551 if (ret < 0 || ret >= end - pos)
2552 return start;
2553 pos += ret;
2554
2555 d = wpabuf_head_u8(data);
2556 for (i = 0; i < wpabuf_len(data); i++) {
2557 ret = os_snprintf(pos, end - pos, "%02x", *d++);
2558 if (ret < 0 || ret >= end - pos)
2559 return start;
2560 pos += ret;
2561 }
2562
2563 ret = os_snprintf(pos, end - pos, "\n");
2564 if (ret < 0 || ret >= end - pos)
2565 return start;
2566 pos += ret;
2567
2568 return pos;
2569}
2570#endif /* CONFIG_INTERWORKING */
2571
2572
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002573static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
2574 unsigned long mask, char *buf, size_t buflen)
2575{
2576 size_t i;
2577 int ret;
2578 char *pos, *end;
2579 const u8 *ie, *ie2;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002580
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002581 pos = buf;
2582 end = buf + buflen;
2583
2584 if (mask & WPA_BSS_MASK_ID) {
2585 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
2586 if (ret < 0 || ret >= end - pos)
2587 return 0;
2588 pos += ret;
2589 }
2590
2591 if (mask & WPA_BSS_MASK_BSSID) {
2592 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
2593 MAC2STR(bss->bssid));
2594 if (ret < 0 || ret >= end - pos)
2595 return 0;
2596 pos += ret;
2597 }
2598
2599 if (mask & WPA_BSS_MASK_FREQ) {
2600 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
2601 if (ret < 0 || ret >= end - pos)
2602 return 0;
2603 pos += ret;
2604 }
2605
2606 if (mask & WPA_BSS_MASK_BEACON_INT) {
2607 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
2608 bss->beacon_int);
2609 if (ret < 0 || ret >= end - pos)
2610 return 0;
2611 pos += ret;
2612 }
2613
2614 if (mask & WPA_BSS_MASK_CAPABILITIES) {
2615 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
2616 bss->caps);
2617 if (ret < 0 || ret >= end - pos)
2618 return 0;
2619 pos += ret;
2620 }
2621
2622 if (mask & WPA_BSS_MASK_QUAL) {
2623 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
2624 if (ret < 0 || ret >= end - pos)
2625 return 0;
2626 pos += ret;
2627 }
2628
2629 if (mask & WPA_BSS_MASK_NOISE) {
2630 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
2631 if (ret < 0 || ret >= end - pos)
2632 return 0;
2633 pos += ret;
2634 }
2635
2636 if (mask & WPA_BSS_MASK_LEVEL) {
2637 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
2638 if (ret < 0 || ret >= end - pos)
2639 return 0;
2640 pos += ret;
2641 }
2642
2643 if (mask & WPA_BSS_MASK_TSF) {
2644 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
2645 (unsigned long long) bss->tsf);
2646 if (ret < 0 || ret >= end - pos)
2647 return 0;
2648 pos += ret;
2649 }
2650
2651 if (mask & WPA_BSS_MASK_AGE) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002652 struct os_time now;
2653
2654 os_get_time(&now);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002655 ret = os_snprintf(pos, end - pos, "age=%d\n",
2656 (int) (now.sec - bss->last_update.sec));
2657 if (ret < 0 || ret >= end - pos)
2658 return 0;
2659 pos += ret;
2660 }
2661
2662 if (mask & WPA_BSS_MASK_IE) {
2663 ret = os_snprintf(pos, end - pos, "ie=");
2664 if (ret < 0 || ret >= end - pos)
2665 return 0;
2666 pos += ret;
2667
2668 ie = (const u8 *) (bss + 1);
2669 for (i = 0; i < bss->ie_len; i++) {
2670 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
2671 if (ret < 0 || ret >= end - pos)
2672 return 0;
2673 pos += ret;
2674 }
2675
2676 ret = os_snprintf(pos, end - pos, "\n");
2677 if (ret < 0 || ret >= end - pos)
2678 return 0;
2679 pos += ret;
2680 }
2681
2682 if (mask & WPA_BSS_MASK_FLAGS) {
2683 ret = os_snprintf(pos, end - pos, "flags=");
2684 if (ret < 0 || ret >= end - pos)
2685 return 0;
2686 pos += ret;
2687
2688 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2689 if (ie)
2690 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
2691 2 + ie[1]);
2692 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2693 if (ie2)
2694 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
2695 2 + ie2[1]);
2696 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2697 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
2698 ret = os_snprintf(pos, end - pos, "[WEP]");
2699 if (ret < 0 || ret >= end - pos)
2700 return 0;
2701 pos += ret;
2702 }
2703 if (bss->caps & IEEE80211_CAP_IBSS) {
2704 ret = os_snprintf(pos, end - pos, "[IBSS]");
2705 if (ret < 0 || ret >= end - pos)
2706 return 0;
2707 pos += ret;
2708 }
2709 if (bss->caps & IEEE80211_CAP_ESS) {
2710 ret = os_snprintf(pos, end - pos, "[ESS]");
2711 if (ret < 0 || ret >= end - pos)
2712 return 0;
2713 pos += ret;
2714 }
2715 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE)) {
2716 ret = os_snprintf(pos, end - pos, "[P2P]");
2717 if (ret < 0 || ret >= end - pos)
2718 return 0;
2719 pos += ret;
2720 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002721#ifdef CONFIG_HS20
2722 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
2723 ret = os_snprintf(pos, end - pos, "[HS20]");
2724 if (ret < 0 || ret >= end - pos)
2725 return -1;
2726 pos += ret;
2727 }
2728#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002729
2730 ret = os_snprintf(pos, end - pos, "\n");
2731 if (ret < 0 || ret >= end - pos)
2732 return 0;
2733 pos += ret;
2734 }
2735
2736 if (mask & WPA_BSS_MASK_SSID) {
2737 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
2738 wpa_ssid_txt(bss->ssid, bss->ssid_len));
2739 if (ret < 0 || ret >= end - pos)
2740 return 0;
2741 pos += ret;
2742 }
2743
2744#ifdef CONFIG_WPS
2745 if (mask & WPA_BSS_MASK_WPS_SCAN) {
2746 ie = (const u8 *) (bss + 1);
2747 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
2748 if (ret < 0 || ret >= end - pos)
2749 return 0;
2750 pos += ret;
2751 }
2752#endif /* CONFIG_WPS */
2753
2754#ifdef CONFIG_P2P
2755 if (mask & WPA_BSS_MASK_P2P_SCAN) {
2756 ie = (const u8 *) (bss + 1);
2757 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
2758 if (ret < 0 || ret >= end - pos)
2759 return 0;
2760 pos += ret;
2761 }
2762#endif /* CONFIG_P2P */
2763
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002764#ifdef CONFIG_WIFI_DISPLAY
2765 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
2766 struct wpabuf *wfd;
2767 ie = (const u8 *) (bss + 1);
2768 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
2769 WFD_IE_VENDOR_TYPE);
2770 if (wfd) {
2771 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
2772 if (ret < 0 || ret >= end - pos)
2773 return pos - buf;
2774 pos += ret;
2775
2776 pos += wpa_snprintf_hex(pos, end - pos,
2777 wpabuf_head(wfd),
2778 wpabuf_len(wfd));
2779 wpabuf_free(wfd);
2780
2781 ret = os_snprintf(pos, end - pos, "\n");
2782 if (ret < 0 || ret >= end - pos)
2783 return pos - buf;
2784 pos += ret;
2785 }
2786 }
2787#endif /* CONFIG_WIFI_DISPLAY */
2788
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002789#ifdef CONFIG_INTERWORKING
2790 if (mask & WPA_BSS_MASK_INTERNETW) {
2791 pos = anqp_add_hex(pos, end, "anqp_venue_name",
2792 bss->anqp_venue_name);
2793 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
2794 bss->anqp_network_auth_type);
2795 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
2796 bss->anqp_roaming_consortium);
2797 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
2798 bss->anqp_ip_addr_type_availability);
2799 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
2800 bss->anqp_nai_realm);
2801 pos = anqp_add_hex(pos, end, "anqp_3gpp", bss->anqp_3gpp);
2802 pos = anqp_add_hex(pos, end, "anqp_domain_name",
2803 bss->anqp_domain_name);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002804#ifdef CONFIG_HS20
2805 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
2806 bss->hs20_operator_friendly_name);
2807 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
2808 bss->hs20_wan_metrics);
2809 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
2810 bss->hs20_connection_capability);
2811#endif /* CONFIG_HS20 */
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002812 }
2813#endif /* CONFIG_INTERWORKING */
2814
Irfan Sheriffe2ea0082012-08-13 10:56:16 -07002815#ifdef ANDROID
2816 ret = os_snprintf(pos, end - pos, "====\n");
2817 if (ret < 0 || ret >= end - pos)
2818 return 0;
2819 pos += ret;
2820#endif
2821
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002822 return pos - buf;
2823}
2824
Dmitry Shmidt04949592012-07-19 12:16:46 -07002825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002826static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
2827 const char *cmd, char *buf,
2828 size_t buflen)
2829{
2830 u8 bssid[ETH_ALEN];
2831 size_t i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002832 struct wpa_bss *bss;
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002833 struct wpa_bss *bsslast = NULL;
2834 struct dl_list *next;
2835 int ret = 0;
2836 int len;
2837 char *ctmp;
2838 unsigned long mask = WPA_BSS_MASK_ALL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002840 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
2841 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
2842 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002843 list_id);
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002844 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
2845 list_id);
2846 } else { /* N1-N2 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002847 unsigned int id1, id2;
2848
2849 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
2850 wpa_printf(MSG_INFO, "Wrong BSS range "
2851 "format");
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002852 return 0;
2853 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002854
2855 id1 = atoi(cmd + 6);
2856 bss = wpa_bss_get_id(wpa_s, id1);
2857 id2 = atoi(ctmp + 1);
2858 if (id2 == 0)
2859 bsslast = dl_list_last(&wpa_s->bss_id,
2860 struct wpa_bss,
2861 list_id);
2862 else {
2863 bsslast = wpa_bss_get_id(wpa_s, id2);
2864 if (bsslast == NULL && bss && id2 > id1) {
2865 struct wpa_bss *tmp = bss;
2866 for (;;) {
2867 next = tmp->list_id.next;
2868 if (next == &wpa_s->bss_id)
2869 break;
2870 tmp = dl_list_entry(
2871 next, struct wpa_bss,
2872 list_id);
2873 if (tmp->id > id2)
2874 break;
2875 bsslast = tmp;
2876 }
2877 }
2878 }
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002879 }
2880 } else if (os_strcmp(cmd, "FIRST") == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002881 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002882 else if (os_strncmp(cmd, "ID-", 3) == 0) {
2883 i = atoi(cmd + 3);
2884 bss = wpa_bss_get_id(wpa_s, i);
2885 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
2886 i = atoi(cmd + 5);
2887 bss = wpa_bss_get_id(wpa_s, i);
2888 if (bss) {
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002889 next = bss->list_id.next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002890 if (next == &wpa_s->bss_id)
2891 bss = NULL;
2892 else
2893 bss = dl_list_entry(next, struct wpa_bss,
2894 list_id);
2895 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002896#ifdef CONFIG_P2P
2897 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
2898 if (hwaddr_aton(cmd + 13, bssid) == 0)
2899 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
2900 else
2901 bss = NULL;
2902#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903 } else if (hwaddr_aton(cmd, bssid) == 0)
2904 bss = wpa_bss_get_bssid(wpa_s, bssid);
2905 else {
2906 struct wpa_bss *tmp;
2907 i = atoi(cmd);
2908 bss = NULL;
2909 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
2910 {
2911 if (i-- == 0) {
2912 bss = tmp;
2913 break;
2914 }
2915 }
2916 }
2917
Dmitry Shmidt04949592012-07-19 12:16:46 -07002918 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
2919 mask = strtoul(ctmp + 5, NULL, 0x10);
2920 if (mask == 0)
2921 mask = WPA_BSS_MASK_ALL;
2922 }
2923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002924 if (bss == NULL)
2925 return 0;
2926
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002927 if (bsslast == NULL)
2928 bsslast = bss;
2929 do {
2930 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
2931 ret += len;
2932 buf += len;
2933 buflen -= len;
2934 if (bss == bsslast)
2935 break;
2936 next = bss->list_id.next;
2937 if (next == &wpa_s->bss_id)
2938 break;
2939 bss = dl_list_entry(next, struct wpa_bss, list_id);
2940 } while (bss && len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002941
Dmitry Shmidtf2df2f22012-03-26 12:43:26 -07002942 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002943}
2944
2945
2946static int wpa_supplicant_ctrl_iface_ap_scan(
2947 struct wpa_supplicant *wpa_s, char *cmd)
2948{
2949 int ap_scan = atoi(cmd);
2950 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
2951}
2952
2953
2954static int wpa_supplicant_ctrl_iface_scan_interval(
2955 struct wpa_supplicant *wpa_s, char *cmd)
2956{
2957 int scan_int = atoi(cmd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002958 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002959}
2960
2961
2962static int wpa_supplicant_ctrl_iface_bss_expire_age(
2963 struct wpa_supplicant *wpa_s, char *cmd)
2964{
2965 int expire_age = atoi(cmd);
2966 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
2967}
2968
2969
2970static int wpa_supplicant_ctrl_iface_bss_expire_count(
2971 struct wpa_supplicant *wpa_s, char *cmd)
2972{
2973 int expire_count = atoi(cmd);
2974 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
2975}
2976
2977
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07002978static int wpa_supplicant_ctrl_iface_bss_flush(
2979 struct wpa_supplicant *wpa_s, char *cmd)
2980{
2981 int flush_age = atoi(cmd);
2982
2983 if (flush_age == 0)
2984 wpa_bss_flush(wpa_s);
2985 else
2986 wpa_bss_flush_by_age(wpa_s, flush_age);
2987 return 0;
2988}
2989
2990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002991static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
2992{
2993 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
2994 /* MLME-DELETEKEYS.request */
2995 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
2996 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
2997 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
2998 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
2999#ifdef CONFIG_IEEE80211W
3000 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
3001 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
3002#endif /* CONFIG_IEEE80211W */
3003
3004 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
3005 0);
3006 /* MLME-SETPROTECTION.request(None) */
3007 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
3008 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
3009 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
3010 wpa_sm_drop_sa(wpa_s->wpa);
3011}
3012
3013
3014static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
3015 char *addr)
3016{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003017#ifdef CONFIG_NO_SCAN_PROCESSING
3018 return -1;
3019#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020 u8 bssid[ETH_ALEN];
3021 struct wpa_bss *bss;
3022 struct wpa_ssid *ssid = wpa_s->current_ssid;
3023
3024 if (hwaddr_aton(addr, bssid)) {
3025 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
3026 "address '%s'", addr);
3027 return -1;
3028 }
3029
3030 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
3031
3032 bss = wpa_bss_get_bssid(wpa_s, bssid);
3033 if (!bss) {
3034 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
3035 "from BSS table");
3036 return -1;
3037 }
3038
3039 /*
3040 * TODO: Find best network configuration block from configuration to
3041 * allow roaming to other networks
3042 */
3043
3044 if (!ssid) {
3045 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
3046 "configuration known for the target AP");
3047 return -1;
3048 }
3049
3050 wpa_s->reassociate = 1;
3051 wpa_supplicant_connect(wpa_s, bss, ssid);
3052
3053 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003054#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003055}
3056
3057
3058#ifdef CONFIG_P2P
3059static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
3060{
3061 unsigned int timeout = atoi(cmd);
3062 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003063 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
3064 char *pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003065 unsigned int search_delay;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003066
3067 if (os_strstr(cmd, "type=social"))
3068 type = P2P_FIND_ONLY_SOCIAL;
3069 else if (os_strstr(cmd, "type=progressive"))
3070 type = P2P_FIND_PROGRESSIVE;
3071
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003072 pos = os_strstr(cmd, "dev_id=");
3073 if (pos) {
3074 pos += 7;
3075 if (hwaddr_aton(pos, dev_id))
3076 return -1;
3077 _dev_id = dev_id;
3078 }
3079
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003080 pos = os_strstr(cmd, "delay=");
3081 if (pos) {
3082 pos += 6;
3083 search_delay = atoi(pos);
3084 } else
3085 search_delay = wpas_p2p_search_delay(wpa_s);
3086
3087 return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id,
3088 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003089}
3090
3091
3092static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
3093 char *buf, size_t buflen)
3094{
3095 u8 addr[ETH_ALEN];
3096 char *pos, *pos2;
3097 char *pin = NULL;
3098 enum p2p_wps_method wps_method;
3099 int new_pin;
3100 int ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003101 int persistent_group, persistent_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003102 int join;
3103 int auth;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003104 int automatic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105 int go_intent = -1;
3106 int freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003107 int pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003108 int ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003109
Dmitry Shmidt04949592012-07-19 12:16:46 -07003110 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
3111 * [persistent|persistent=<network id>]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003112 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
3113 * [ht40] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003114
3115 if (hwaddr_aton(cmd, addr))
3116 return -1;
3117
3118 pos = cmd + 17;
3119 if (*pos != ' ')
3120 return -1;
3121 pos++;
3122
3123 persistent_group = os_strstr(pos, " persistent") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003124 pos2 = os_strstr(pos, " persistent=");
3125 if (pos2) {
3126 struct wpa_ssid *ssid;
3127 persistent_id = atoi(pos2 + 12);
3128 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3129 if (ssid == NULL || ssid->disabled != 2 ||
3130 ssid->mode != WPAS_MODE_P2P_GO) {
3131 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3132 "SSID id=%d for persistent P2P group (GO)",
3133 persistent_id);
3134 return -1;
3135 }
3136 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003137 join = os_strstr(pos, " join") != NULL;
3138 auth = os_strstr(pos, " auth") != NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003139 automatic = os_strstr(pos, " auto") != NULL;
3140 pd = os_strstr(pos, " provdisc") != NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003141 ht40 = os_strstr(pos, " ht40") != NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003142
3143 pos2 = os_strstr(pos, " go_intent=");
3144 if (pos2) {
3145 pos2 += 11;
3146 go_intent = atoi(pos2);
3147 if (go_intent < 0 || go_intent > 15)
3148 return -1;
3149 }
3150
3151 pos2 = os_strstr(pos, " freq=");
3152 if (pos2) {
3153 pos2 += 6;
3154 freq = atoi(pos2);
3155 if (freq <= 0)
3156 return -1;
3157 }
3158
3159 if (os_strncmp(pos, "pin", 3) == 0) {
3160 /* Request random PIN (to be displayed) and enable the PIN */
3161 wps_method = WPS_PIN_DISPLAY;
3162 } else if (os_strncmp(pos, "pbc", 3) == 0) {
3163 wps_method = WPS_PBC;
3164 } else {
3165 pin = pos;
3166 pos = os_strchr(pin, ' ');
3167 wps_method = WPS_PIN_KEYPAD;
3168 if (pos) {
3169 *pos++ = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003170 if (os_strncmp(pos, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003171 wps_method = WPS_PIN_DISPLAY;
3172 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003173 if (!wps_pin_str_valid(pin)) {
3174 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
3175 return 17;
3176 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003177 }
3178
3179 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003180 persistent_group, automatic, join,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003181 auth, go_intent, freq, persistent_id, pd,
3182 ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003183 if (new_pin == -2) {
3184 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
3185 return 25;
3186 }
3187 if (new_pin == -3) {
3188 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
3189 return 25;
3190 }
3191 if (new_pin < 0)
3192 return -1;
3193 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
3194 ret = os_snprintf(buf, buflen, "%08d", new_pin);
3195 if (ret < 0 || (size_t) ret >= buflen)
3196 return -1;
3197 return ret;
3198 }
3199
3200 os_memcpy(buf, "OK\n", 3);
3201 return 3;
3202}
3203
3204
3205static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
3206{
3207 unsigned int timeout = atoi(cmd);
3208 return wpas_p2p_listen(wpa_s, timeout);
3209}
3210
3211
3212static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
3213{
3214 u8 addr[ETH_ALEN];
3215 char *pos;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003216 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003217
Dmitry Shmidt04949592012-07-19 12:16:46 -07003218 /* <addr> <config method> [join|auto] */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003219
3220 if (hwaddr_aton(cmd, addr))
3221 return -1;
3222
3223 pos = cmd + 17;
3224 if (*pos != ' ')
3225 return -1;
3226 pos++;
3227
Dmitry Shmidt04949592012-07-19 12:16:46 -07003228 if (os_strstr(pos, " join") != NULL)
3229 use = WPAS_P2P_PD_FOR_JOIN;
3230 else if (os_strstr(pos, " auto") != NULL)
3231 use = WPAS_P2P_PD_AUTO;
3232
3233 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003234}
3235
3236
3237static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
3238 size_t buflen)
3239{
3240 struct wpa_ssid *ssid = wpa_s->current_ssid;
3241
3242 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3243 ssid->passphrase == NULL)
3244 return -1;
3245
3246 os_strlcpy(buf, ssid->passphrase, buflen);
3247 return os_strlen(buf);
3248}
3249
3250
3251static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
3252 char *buf, size_t buflen)
3253{
3254 u64 ref;
3255 int res;
3256 u8 dst_buf[ETH_ALEN], *dst;
3257 struct wpabuf *tlvs;
3258 char *pos;
3259 size_t len;
3260
3261 if (hwaddr_aton(cmd, dst_buf))
3262 return -1;
3263 dst = dst_buf;
3264 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
3265 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
3266 dst = NULL;
3267 pos = cmd + 17;
3268 if (*pos != ' ')
3269 return -1;
3270 pos++;
3271
3272 if (os_strncmp(pos, "upnp ", 5) == 0) {
3273 u8 version;
3274 pos += 5;
3275 if (hexstr2bin(pos, &version, 1) < 0)
3276 return -1;
3277 pos += 2;
3278 if (*pos != ' ')
3279 return -1;
3280 pos++;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003281 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003282#ifdef CONFIG_WIFI_DISPLAY
3283 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
3284 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
3285#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003286 } else {
3287 len = os_strlen(pos);
3288 if (len & 1)
3289 return -1;
3290 len /= 2;
3291 tlvs = wpabuf_alloc(len);
3292 if (tlvs == NULL)
3293 return -1;
3294 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
3295 wpabuf_free(tlvs);
3296 return -1;
3297 }
3298
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003299 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003300 wpabuf_free(tlvs);
3301 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003302 if (ref == 0)
3303 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
3305 if (res < 0 || (unsigned) res >= buflen)
3306 return -1;
3307 return res;
3308}
3309
3310
3311static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
3312 char *cmd)
3313{
3314 long long unsigned val;
3315 u64 req;
3316 if (sscanf(cmd, "%llx", &val) != 1)
3317 return -1;
3318 req = val;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003319 return wpas_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003320}
3321
3322
3323static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
3324{
3325 int freq;
3326 u8 dst[ETH_ALEN];
3327 u8 dialog_token;
3328 struct wpabuf *resp_tlvs;
3329 char *pos, *pos2;
3330 size_t len;
3331
3332 pos = os_strchr(cmd, ' ');
3333 if (pos == NULL)
3334 return -1;
3335 *pos++ = '\0';
3336 freq = atoi(cmd);
3337 if (freq == 0)
3338 return -1;
3339
3340 if (hwaddr_aton(pos, dst))
3341 return -1;
3342 pos += 17;
3343 if (*pos != ' ')
3344 return -1;
3345 pos++;
3346
3347 pos2 = os_strchr(pos, ' ');
3348 if (pos2 == NULL)
3349 return -1;
3350 *pos2++ = '\0';
3351 dialog_token = atoi(pos);
3352
3353 len = os_strlen(pos2);
3354 if (len & 1)
3355 return -1;
3356 len /= 2;
3357 resp_tlvs = wpabuf_alloc(len);
3358 if (resp_tlvs == NULL)
3359 return -1;
3360 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
3361 wpabuf_free(resp_tlvs);
3362 return -1;
3363 }
3364
3365 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
3366 wpabuf_free(resp_tlvs);
3367 return 0;
3368}
3369
3370
3371static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
3372 char *cmd)
3373{
Dmitry Shmidt04949592012-07-19 12:16:46 -07003374 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
3375 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003376 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
3377 return 0;
3378}
3379
3380
3381static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
3382 char *cmd)
3383{
3384 char *pos;
3385 size_t len;
3386 struct wpabuf *query, *resp;
3387
3388 pos = os_strchr(cmd, ' ');
3389 if (pos == NULL)
3390 return -1;
3391 *pos++ = '\0';
3392
3393 len = os_strlen(cmd);
3394 if (len & 1)
3395 return -1;
3396 len /= 2;
3397 query = wpabuf_alloc(len);
3398 if (query == NULL)
3399 return -1;
3400 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
3401 wpabuf_free(query);
3402 return -1;
3403 }
3404
3405 len = os_strlen(pos);
3406 if (len & 1) {
3407 wpabuf_free(query);
3408 return -1;
3409 }
3410 len /= 2;
3411 resp = wpabuf_alloc(len);
3412 if (resp == NULL) {
3413 wpabuf_free(query);
3414 return -1;
3415 }
3416 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
3417 wpabuf_free(query);
3418 wpabuf_free(resp);
3419 return -1;
3420 }
3421
3422 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
3423 wpabuf_free(query);
3424 wpabuf_free(resp);
3425 return -1;
3426 }
3427 return 0;
3428}
3429
3430
3431static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
3432{
3433 char *pos;
3434 u8 version;
3435
3436 pos = os_strchr(cmd, ' ');
3437 if (pos == NULL)
3438 return -1;
3439 *pos++ = '\0';
3440
3441 if (hexstr2bin(cmd, &version, 1) < 0)
3442 return -1;
3443
3444 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
3445}
3446
3447
3448static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
3449{
3450 char *pos;
3451
3452 pos = os_strchr(cmd, ' ');
3453 if (pos == NULL)
3454 return -1;
3455 *pos++ = '\0';
3456
3457 if (os_strcmp(cmd, "bonjour") == 0)
3458 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
3459 if (os_strcmp(cmd, "upnp") == 0)
3460 return p2p_ctrl_service_add_upnp(wpa_s, pos);
3461 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
3462 return -1;
3463}
3464
3465
3466static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
3467 char *cmd)
3468{
3469 size_t len;
3470 struct wpabuf *query;
3471 int ret;
3472
3473 len = os_strlen(cmd);
3474 if (len & 1)
3475 return -1;
3476 len /= 2;
3477 query = wpabuf_alloc(len);
3478 if (query == NULL)
3479 return -1;
3480 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
3481 wpabuf_free(query);
3482 return -1;
3483 }
3484
3485 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
3486 wpabuf_free(query);
3487 return ret;
3488}
3489
3490
3491static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
3492{
3493 char *pos;
3494 u8 version;
3495
3496 pos = os_strchr(cmd, ' ');
3497 if (pos == NULL)
3498 return -1;
3499 *pos++ = '\0';
3500
3501 if (hexstr2bin(cmd, &version, 1) < 0)
3502 return -1;
3503
3504 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
3505}
3506
3507
3508static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
3509{
3510 char *pos;
3511
3512 pos = os_strchr(cmd, ' ');
3513 if (pos == NULL)
3514 return -1;
3515 *pos++ = '\0';
3516
3517 if (os_strcmp(cmd, "bonjour") == 0)
3518 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
3519 if (os_strcmp(cmd, "upnp") == 0)
3520 return p2p_ctrl_service_del_upnp(wpa_s, pos);
3521 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
3522 return -1;
3523}
3524
3525
3526static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
3527{
3528 u8 addr[ETH_ALEN];
3529
3530 /* <addr> */
3531
3532 if (hwaddr_aton(cmd, addr))
3533 return -1;
3534
3535 return wpas_p2p_reject(wpa_s, addr);
3536}
3537
3538
3539static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
3540{
3541 char *pos;
3542 int id;
3543 struct wpa_ssid *ssid;
3544 u8 peer[ETH_ALEN];
3545
3546 id = atoi(cmd);
3547 pos = os_strstr(cmd, " peer=");
3548 if (pos) {
3549 pos += 6;
3550 if (hwaddr_aton(pos, peer))
3551 return -1;
3552 }
3553 ssid = wpa_config_get_network(wpa_s->conf, id);
3554 if (ssid == NULL || ssid->disabled != 2) {
3555 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
3556 "for persistent P2P group",
3557 id);
3558 return -1;
3559 }
3560
3561 return wpas_p2p_invite(wpa_s, pos ? peer : NULL, ssid, NULL);
3562}
3563
3564
3565static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
3566{
3567 char *pos;
3568 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
3569
3570 pos = os_strstr(cmd, " peer=");
3571 if (!pos)
3572 return -1;
3573
3574 *pos = '\0';
3575 pos += 6;
3576 if (hwaddr_aton(pos, peer)) {
3577 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
3578 return -1;
3579 }
3580
3581 pos = os_strstr(pos, " go_dev_addr=");
3582 if (pos) {
3583 pos += 13;
3584 if (hwaddr_aton(pos, go_dev_addr)) {
3585 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
3586 pos);
3587 return -1;
3588 }
3589 go_dev = go_dev_addr;
3590 }
3591
3592 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
3593}
3594
3595
3596static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
3597{
3598 if (os_strncmp(cmd, "persistent=", 11) == 0)
3599 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
3600 if (os_strncmp(cmd, "group=", 6) == 0)
3601 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
3602
3603 return -1;
3604}
3605
3606
3607static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003608 char *cmd, int freq, int ht40)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609{
3610 int id;
3611 struct wpa_ssid *ssid;
3612
3613 id = atoi(cmd);
3614 ssid = wpa_config_get_network(wpa_s->conf, id);
3615 if (ssid == NULL || ssid->disabled != 2) {
3616 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
3617 "for persistent P2P group",
3618 id);
3619 return -1;
3620 }
3621
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003622 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623}
3624
3625
3626static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
3627{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003628 int freq = 0, ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629 char *pos;
3630
3631 pos = os_strstr(cmd, "freq=");
3632 if (pos)
3633 freq = atoi(pos + 5);
3634
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003635 ht40 = os_strstr(cmd, "ht40") != NULL;
3636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637 if (os_strncmp(cmd, "persistent=", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003638 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
3639 ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003640 if (os_strcmp(cmd, "persistent") == 0 ||
3641 os_strncmp(cmd, "persistent ", 11) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003642 return wpas_p2p_group_add(wpa_s, 1, freq, ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003643 if (os_strncmp(cmd, "freq=", 5) == 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003644 return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
3645 if (ht40)
3646 return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003647
3648 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
3649 cmd);
3650 return -1;
3651}
3652
3653
3654static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
3655 char *buf, size_t buflen)
3656{
3657 u8 addr[ETH_ALEN], *addr_ptr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003658 int next, res;
3659 const struct p2p_peer_info *info;
3660 char *pos, *end;
3661 char devtype[WPS_DEV_TYPE_BUFSIZE];
3662 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003663
3664 if (!wpa_s->global->p2p)
3665 return -1;
3666
3667 if (os_strcmp(cmd, "FIRST") == 0) {
3668 addr_ptr = NULL;
3669 next = 0;
3670 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
3671 if (hwaddr_aton(cmd + 5, addr) < 0)
3672 return -1;
3673 addr_ptr = addr;
3674 next = 1;
3675 } else {
3676 if (hwaddr_aton(cmd, addr) < 0)
3677 return -1;
3678 addr_ptr = addr;
3679 next = 0;
3680 }
3681
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003682 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
3683 if (info == NULL)
3684 return -1;
3685
3686 pos = buf;
3687 end = buf + buflen;
3688
3689 res = os_snprintf(pos, end - pos, MACSTR "\n"
3690 "pri_dev_type=%s\n"
3691 "device_name=%s\n"
3692 "manufacturer=%s\n"
3693 "model_name=%s\n"
3694 "model_number=%s\n"
3695 "serial_number=%s\n"
3696 "config_methods=0x%x\n"
3697 "dev_capab=0x%x\n"
3698 "group_capab=0x%x\n"
3699 "level=%d\n",
3700 MAC2STR(info->p2p_device_addr),
3701 wps_dev_type_bin2str(info->pri_dev_type,
3702 devtype, sizeof(devtype)),
3703 info->device_name,
3704 info->manufacturer,
3705 info->model_name,
3706 info->model_number,
3707 info->serial_number,
3708 info->config_methods,
3709 info->dev_capab,
3710 info->group_capab,
3711 info->level);
3712 if (res < 0 || res >= end - pos)
3713 return pos - buf;
3714 pos += res;
3715
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003716 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003717 if (ssid) {
3718 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
3719 if (res < 0 || res >= end - pos)
3720 return pos - buf;
3721 pos += res;
3722 }
3723
3724 res = p2p_get_peer_info_txt(info, pos, end - pos);
3725 if (res < 0)
3726 return pos - buf;
3727 pos += res;
3728
3729 return pos - buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003730}
3731
3732
Dmitry Shmidt04949592012-07-19 12:16:46 -07003733static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
3734 const char *param)
3735{
3736 struct wpa_freq_range *freq = NULL, *n;
3737 unsigned int count = 0, i;
3738 const char *pos, *pos2, *pos3;
3739
3740 if (wpa_s->global->p2p == NULL)
3741 return -1;
3742
3743 /*
3744 * param includes comma separated frequency range.
3745 * For example: 2412-2432,2462,5000-6000
3746 */
3747 pos = param;
3748 while (pos && pos[0]) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003749 n = os_realloc_array(freq, count + 1,
3750 sizeof(struct wpa_freq_range));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003751 if (n == NULL) {
3752 os_free(freq);
3753 return -1;
3754 }
3755 freq = n;
3756 freq[count].min = atoi(pos);
3757 pos2 = os_strchr(pos, '-');
3758 pos3 = os_strchr(pos, ',');
3759 if (pos2 && (!pos3 || pos2 < pos3)) {
3760 pos2++;
3761 freq[count].max = atoi(pos2);
3762 } else
3763 freq[count].max = freq[count].min;
3764 pos = pos3;
3765 if (pos)
3766 pos++;
3767 count++;
3768 }
3769
3770 for (i = 0; i < count; i++) {
3771 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
3772 freq[i].min, freq[i].max);
3773 }
3774
3775 os_free(wpa_s->global->p2p_disallow_freq);
3776 wpa_s->global->p2p_disallow_freq = freq;
3777 wpa_s->global->num_p2p_disallow_freq = count;
3778 wpas_p2p_update_channel_list(wpa_s);
3779 return 0;
3780}
3781
3782
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003783static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
3784{
3785 char *param;
3786
3787 if (wpa_s->global->p2p == NULL)
3788 return -1;
3789
3790 param = os_strchr(cmd, ' ');
3791 if (param == NULL)
3792 return -1;
3793 *param++ = '\0';
3794
3795 if (os_strcmp(cmd, "discoverability") == 0) {
3796 p2p_set_client_discoverability(wpa_s->global->p2p,
3797 atoi(param));
3798 return 0;
3799 }
3800
3801 if (os_strcmp(cmd, "managed") == 0) {
3802 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
3803 return 0;
3804 }
3805
3806 if (os_strcmp(cmd, "listen_channel") == 0) {
3807 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
3808 atoi(param));
3809 }
3810
3811 if (os_strcmp(cmd, "ssid_postfix") == 0) {
3812 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
3813 os_strlen(param));
3814 }
3815
3816 if (os_strcmp(cmd, "noa") == 0) {
3817 char *pos;
3818 int count, start, duration;
3819 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
3820 count = atoi(param);
3821 pos = os_strchr(param, ',');
3822 if (pos == NULL)
3823 return -1;
3824 pos++;
3825 start = atoi(pos);
3826 pos = os_strchr(pos, ',');
3827 if (pos == NULL)
3828 return -1;
3829 pos++;
3830 duration = atoi(pos);
3831 if (count < 0 || count > 255 || start < 0 || duration < 0)
3832 return -1;
3833 if (count == 0 && duration > 0)
3834 return -1;
3835 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
3836 "start=%d duration=%d", count, start, duration);
3837 return wpas_p2p_set_noa(wpa_s, count, start, duration);
3838 }
3839
3840 if (os_strcmp(cmd, "ps") == 0)
3841 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
3842
3843 if (os_strcmp(cmd, "oppps") == 0)
3844 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
3845
3846 if (os_strcmp(cmd, "ctwindow") == 0)
3847 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
3848
3849 if (os_strcmp(cmd, "disabled") == 0) {
3850 wpa_s->global->p2p_disabled = atoi(param);
3851 wpa_printf(MSG_DEBUG, "P2P functionality %s",
3852 wpa_s->global->p2p_disabled ?
3853 "disabled" : "enabled");
3854 if (wpa_s->global->p2p_disabled) {
3855 wpas_p2p_stop_find(wpa_s);
3856 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3857 p2p_flush(wpa_s->global->p2p);
3858 }
3859 return 0;
3860 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003861
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07003862 if (os_strcmp(cmd, "conc_pref") == 0) {
3863 if (os_strcmp(param, "sta") == 0)
3864 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
3865 else if (os_strcmp(param, "p2p") == 0)
3866 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07003867 else {
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07003868 wpa_printf(MSG_INFO, "Invalid conc_pref value");
Dmitry Shmidt687922c2012-03-26 14:02:32 -07003869 return -1;
3870 }
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07003871 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
Dmitry Shmidt04949592012-07-19 12:16:46 -07003872 "%s", param);
Dmitry Shmidt687922c2012-03-26 14:02:32 -07003873 return 0;
3874 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003876 if (os_strcmp(cmd, "force_long_sd") == 0) {
3877 wpa_s->force_long_sd = atoi(param);
3878 return 0;
3879 }
3880
3881 if (os_strcmp(cmd, "peer_filter") == 0) {
3882 u8 addr[ETH_ALEN];
3883 if (hwaddr_aton(param, addr))
3884 return -1;
3885 p2p_set_peer_filter(wpa_s->global->p2p, addr);
3886 return 0;
3887 }
3888
3889 if (os_strcmp(cmd, "cross_connect") == 0)
3890 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
3891
3892 if (os_strcmp(cmd, "go_apsd") == 0) {
3893 if (os_strcmp(param, "disable") == 0)
3894 wpa_s->set_ap_uapsd = 0;
3895 else {
3896 wpa_s->set_ap_uapsd = 1;
3897 wpa_s->ap_uapsd = atoi(param);
3898 }
3899 return 0;
3900 }
3901
3902 if (os_strcmp(cmd, "client_apsd") == 0) {
3903 if (os_strcmp(param, "disable") == 0)
3904 wpa_s->set_sta_uapsd = 0;
3905 else {
3906 int be, bk, vi, vo;
3907 char *pos;
3908 /* format: BE,BK,VI,VO;max SP Length */
3909 be = atoi(param);
3910 pos = os_strchr(param, ',');
3911 if (pos == NULL)
3912 return -1;
3913 pos++;
3914 bk = atoi(pos);
3915 pos = os_strchr(pos, ',');
3916 if (pos == NULL)
3917 return -1;
3918 pos++;
3919 vi = atoi(pos);
3920 pos = os_strchr(pos, ',');
3921 if (pos == NULL)
3922 return -1;
3923 pos++;
3924 vo = atoi(pos);
3925 /* ignore max SP Length for now */
3926
3927 wpa_s->set_sta_uapsd = 1;
3928 wpa_s->sta_uapsd = 0;
3929 if (be)
3930 wpa_s->sta_uapsd |= BIT(0);
3931 if (bk)
3932 wpa_s->sta_uapsd |= BIT(1);
3933 if (vi)
3934 wpa_s->sta_uapsd |= BIT(2);
3935 if (vo)
3936 wpa_s->sta_uapsd |= BIT(3);
3937 }
3938 return 0;
3939 }
3940
Dmitry Shmidt04949592012-07-19 12:16:46 -07003941 if (os_strcmp(cmd, "disallow_freq") == 0)
3942 return p2p_ctrl_disallow_freq(wpa_s, param);
3943
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
3945 cmd);
3946
3947 return -1;
3948}
3949
3950
3951static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
3952{
3953 char *pos, *pos2;
3954 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
3955
3956 if (cmd[0]) {
3957 pos = os_strchr(cmd, ' ');
3958 if (pos == NULL)
3959 return -1;
3960 *pos++ = '\0';
3961 dur1 = atoi(cmd);
3962
3963 pos2 = os_strchr(pos, ' ');
3964 if (pos2)
3965 *pos2++ = '\0';
3966 int1 = atoi(pos);
3967 } else
3968 pos2 = NULL;
3969
3970 if (pos2) {
3971 pos = os_strchr(pos2, ' ');
3972 if (pos == NULL)
3973 return -1;
3974 *pos++ = '\0';
3975 dur2 = atoi(pos2);
3976 int2 = atoi(pos);
3977 }
3978
3979 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
3980}
3981
3982
3983static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
3984{
3985 char *pos;
3986 unsigned int period = 0, interval = 0;
3987
3988 if (cmd[0]) {
3989 pos = os_strchr(cmd, ' ');
3990 if (pos == NULL)
3991 return -1;
3992 *pos++ = '\0';
3993 period = atoi(cmd);
3994 interval = atoi(pos);
3995 }
3996
3997 return wpas_p2p_ext_listen(wpa_s, period, interval);
3998}
3999
4000#endif /* CONFIG_P2P */
4001
4002
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004003#ifdef CONFIG_INTERWORKING
4004static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
4005{
4006 u8 bssid[ETH_ALEN];
4007 struct wpa_bss *bss;
4008
4009 if (hwaddr_aton(dst, bssid)) {
4010 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
4011 return -1;
4012 }
4013
4014 bss = wpa_bss_get_bssid(wpa_s, bssid);
4015 if (bss == NULL) {
4016 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
4017 MAC2STR(bssid));
4018 return -1;
4019 }
4020
4021 return interworking_connect(wpa_s, bss);
4022}
4023
4024
4025static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
4026{
4027 u8 dst_addr[ETH_ALEN];
4028 int used;
4029 char *pos;
4030#define MAX_ANQP_INFO_ID 100
4031 u16 id[MAX_ANQP_INFO_ID];
4032 size_t num_id = 0;
4033
4034 used = hwaddr_aton2(dst, dst_addr);
4035 if (used < 0)
4036 return -1;
4037 pos = dst + used;
4038 while (num_id < MAX_ANQP_INFO_ID) {
4039 id[num_id] = atoi(pos);
4040 if (id[num_id])
4041 num_id++;
4042 pos = os_strchr(pos + 1, ',');
4043 if (pos == NULL)
4044 break;
4045 pos++;
4046 }
4047
4048 if (num_id == 0)
4049 return -1;
4050
4051 return anqp_send_req(wpa_s, dst_addr, id, num_id);
4052}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004053
4054
4055static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
4056{
4057 u8 dst_addr[ETH_ALEN];
4058 struct wpabuf *advproto, *query = NULL;
4059 int used, ret = -1;
4060 char *pos, *end;
4061 size_t len;
4062
4063 used = hwaddr_aton2(cmd, dst_addr);
4064 if (used < 0)
4065 return -1;
4066
4067 pos = cmd + used;
4068 while (*pos == ' ')
4069 pos++;
4070
4071 /* Advertisement Protocol ID */
4072 end = os_strchr(pos, ' ');
4073 if (end)
4074 len = end - pos;
4075 else
4076 len = os_strlen(pos);
4077 if (len & 0x01)
4078 return -1;
4079 len /= 2;
4080 if (len == 0)
4081 return -1;
4082 advproto = wpabuf_alloc(len);
4083 if (advproto == NULL)
4084 return -1;
4085 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
4086 goto fail;
4087
4088 if (end) {
4089 /* Optional Query Request */
4090 pos = end + 1;
4091 while (*pos == ' ')
4092 pos++;
4093
4094 len = os_strlen(pos);
4095 if (len) {
4096 if (len & 0x01)
4097 goto fail;
4098 len /= 2;
4099 if (len == 0)
4100 goto fail;
4101 query = wpabuf_alloc(len);
4102 if (query == NULL)
4103 goto fail;
4104 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
4105 goto fail;
4106 }
4107 }
4108
4109 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
4110
4111fail:
4112 wpabuf_free(advproto);
4113 wpabuf_free(query);
4114
4115 return ret;
4116}
4117
4118
4119static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
4120 size_t buflen)
4121{
4122 u8 addr[ETH_ALEN];
4123 int dialog_token;
4124 int used;
4125 char *pos;
4126 size_t resp_len, start, requested_len;
4127
4128 if (!wpa_s->last_gas_resp)
4129 return -1;
4130
4131 used = hwaddr_aton2(cmd, addr);
4132 if (used < 0)
4133 return -1;
4134
4135 pos = cmd + used;
4136 while (*pos == ' ')
4137 pos++;
4138 dialog_token = atoi(pos);
4139
4140 if (os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) != 0 ||
4141 dialog_token != wpa_s->last_gas_dialog_token)
4142 return -1;
4143
4144 resp_len = wpabuf_len(wpa_s->last_gas_resp);
4145 start = 0;
4146 requested_len = resp_len;
4147
4148 pos = os_strchr(pos, ' ');
4149 if (pos) {
4150 start = atoi(pos);
4151 if (start > resp_len)
4152 return os_snprintf(buf, buflen, "FAIL-Invalid range");
4153 pos = os_strchr(pos, ',');
4154 if (pos == NULL)
4155 return -1;
4156 pos++;
4157 requested_len = atoi(pos);
4158 if (start + requested_len > resp_len)
4159 return os_snprintf(buf, buflen, "FAIL-Invalid range");
4160 }
4161
4162 if (requested_len * 2 + 1 > buflen)
4163 return os_snprintf(buf, buflen, "FAIL-Too long response");
4164
4165 return wpa_snprintf_hex(buf, buflen,
4166 wpabuf_head_u8(wpa_s->last_gas_resp) + start,
4167 requested_len);
4168}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004169#endif /* CONFIG_INTERWORKING */
4170
4171
Dmitry Shmidt04949592012-07-19 12:16:46 -07004172#ifdef CONFIG_HS20
4173
4174static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
4175{
4176 u8 dst_addr[ETH_ALEN];
4177 int used;
4178 char *pos;
4179 u32 subtypes = 0;
4180
4181 used = hwaddr_aton2(dst, dst_addr);
4182 if (used < 0)
4183 return -1;
4184 pos = dst + used;
4185 for (;;) {
4186 int num = atoi(pos);
4187 if (num <= 0 || num > 31)
4188 return -1;
4189 subtypes |= BIT(num);
4190 pos = os_strchr(pos + 1, ',');
4191 if (pos == NULL)
4192 break;
4193 pos++;
4194 }
4195
4196 if (subtypes == 0)
4197 return -1;
4198
4199 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
4200}
4201
4202
4203static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4204 const u8 *addr, const char *realm)
4205{
4206 u8 *buf;
4207 size_t rlen, len;
4208 int ret;
4209
4210 rlen = os_strlen(realm);
4211 len = 3 + rlen;
4212 buf = os_malloc(len);
4213 if (buf == NULL)
4214 return -1;
4215 buf[0] = 1; /* NAI Home Realm Count */
4216 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
4217 buf[2] = rlen;
4218 os_memcpy(buf + 3, realm, rlen);
4219
4220 ret = hs20_anqp_send_req(wpa_s, addr,
4221 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4222 buf, len);
4223
4224 os_free(buf);
4225
4226 return ret;
4227}
4228
4229
4230static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
4231 char *dst)
4232{
4233 struct wpa_cred *cred = wpa_s->conf->cred;
4234 u8 dst_addr[ETH_ALEN];
4235 int used;
4236 u8 *buf;
4237 size_t len;
4238 int ret;
4239
4240 used = hwaddr_aton2(dst, dst_addr);
4241 if (used < 0)
4242 return -1;
4243
4244 while (dst[used] == ' ')
4245 used++;
4246 if (os_strncmp(dst + used, "realm=", 6) == 0)
4247 return hs20_nai_home_realm_list(wpa_s, dst_addr,
4248 dst + used + 6);
4249
4250 len = os_strlen(dst + used);
4251
4252 if (len == 0 && cred && cred->realm)
4253 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
4254
4255 if (len % 1)
4256 return -1;
4257 len /= 2;
4258 buf = os_malloc(len);
4259 if (buf == NULL)
4260 return -1;
4261 if (hexstr2bin(dst + used, buf, len) < 0) {
4262 os_free(buf);
4263 return -1;
4264 }
4265
4266 ret = hs20_anqp_send_req(wpa_s, dst_addr,
4267 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
4268 buf, len);
4269 os_free(buf);
4270
4271 return ret;
4272}
4273
4274#endif /* CONFIG_HS20 */
4275
4276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004277static int wpa_supplicant_ctrl_iface_sta_autoconnect(
4278 struct wpa_supplicant *wpa_s, char *cmd)
4279{
4280 wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
4281 return 0;
4282}
4283
4284
Dmitry Shmidt04949592012-07-19 12:16:46 -07004285#ifdef CONFIG_AUTOSCAN
4286
4287static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
4288 char *cmd)
4289{
4290 enum wpa_states state = wpa_s->wpa_state;
4291 char *new_params = NULL;
4292
4293 if (os_strlen(cmd) > 0) {
4294 new_params = os_strdup(cmd);
4295 if (new_params == NULL)
4296 return -1;
4297 }
4298
4299 os_free(wpa_s->conf->autoscan);
4300 wpa_s->conf->autoscan = new_params;
4301
4302 if (wpa_s->conf->autoscan == NULL)
4303 autoscan_deinit(wpa_s);
4304 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
4305 autoscan_init(wpa_s, 1);
4306 else if (state == WPA_SCANNING)
4307 wpa_supplicant_reinit_autoscan(wpa_s);
4308
4309 return 0;
4310}
4311
4312#endif /* CONFIG_AUTOSCAN */
4313
4314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004315static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
4316 size_t buflen)
4317{
4318 struct wpa_signal_info si;
4319 int ret;
4320
4321 ret = wpa_drv_signal_poll(wpa_s, &si);
4322 if (ret)
4323 return -1;
4324
4325 ret = os_snprintf(buf, buflen, "RSSI=%d\nLINKSPEED=%d\n"
4326 "NOISE=%d\nFREQUENCY=%u\n",
4327 si.current_signal, si.current_txrate / 1000,
4328 si.current_noise, si.frequency);
4329 if (ret < 0 || (unsigned int) ret > buflen)
4330 return -1;
4331 return ret;
4332}
4333
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07004334static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
4335 size_t buflen)
4336{
4337 struct hostap_sta_driver_data sta;
4338 int ret;
4339
4340 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
4341 if (ret)
4342 return -1;
4343
4344 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
4345 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
4346 if (ret < 0 || (unsigned int) ret > buflen)
4347 return -1;
4348 return ret;
4349}
4350
4351
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004352#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07004353static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
4354 char *buf, size_t buflen)
4355{
4356 int ret;
4357
4358 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
4359 if (ret == 0)
4360 ret = sprintf(buf, "%s\n", "OK");
4361 return ret;
4362}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004363#endif
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07004364
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004365char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
4366 char *buf, size_t *resp_len)
4367{
4368 char *reply;
4369 const int reply_size = 4096;
4370 int ctrl_rsp = 0;
4371 int reply_len;
4372
4373 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
4374 os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
4375 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
4376 (const u8 *) buf, os_strlen(buf));
4377 } else {
4378 int level = MSG_DEBUG;
4379 if (os_strcmp(buf, "PING") == 0)
4380 level = MSG_EXCESSIVE;
4381 wpa_hexdump_ascii(level, "RX ctrl_iface",
4382 (const u8 *) buf, os_strlen(buf));
4383 }
4384
4385 reply = os_malloc(reply_size);
4386 if (reply == NULL) {
4387 *resp_len = 1;
4388 return NULL;
4389 }
4390
4391 os_memcpy(reply, "OK\n", 3);
4392 reply_len = 3;
4393
4394 if (os_strcmp(buf, "PING") == 0) {
4395 os_memcpy(reply, "PONG\n", 5);
4396 reply_len = 5;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004397 } else if (os_strcmp(buf, "IFNAME") == 0) {
4398 reply_len = os_strlen(wpa_s->ifname);
4399 os_memcpy(reply, wpa_s->ifname, reply_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004400 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
4401 if (wpa_debug_reopen_file() < 0)
4402 reply_len = -1;
4403 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
4404 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
4405 } else if (os_strcmp(buf, "MIB") == 0) {
4406 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
4407 if (reply_len >= 0) {
4408 int res;
4409 res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
4410 reply_size - reply_len);
4411 if (res < 0)
4412 reply_len = -1;
4413 else
4414 reply_len += res;
4415 }
4416 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
4417 reply_len = wpa_supplicant_ctrl_iface_status(
4418 wpa_s, buf + 6, reply, reply_size);
4419 } else if (os_strcmp(buf, "PMKSA") == 0) {
4420 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
4421 reply_size);
4422 } else if (os_strncmp(buf, "SET ", 4) == 0) {
4423 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
4424 reply_len = -1;
4425 } else if (os_strncmp(buf, "GET ", 4) == 0) {
4426 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
4427 reply, reply_size);
4428 } else if (os_strcmp(buf, "LOGON") == 0) {
4429 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
4430 } else if (os_strcmp(buf, "LOGOFF") == 0) {
4431 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
4432 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004433 wpa_s->normal_scans = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004434 wpa_supplicant_reinit_autoscan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004435 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4436 reply_len = -1;
4437 else {
4438 wpa_s->disconnected = 0;
4439 wpa_s->reassociate = 1;
4440 wpa_supplicant_req_scan(wpa_s, 0, 0);
4441 }
4442 } else if (os_strcmp(buf, "RECONNECT") == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004443 wpa_s->normal_scans = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004444 wpa_supplicant_reinit_autoscan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004445 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4446 reply_len = -1;
4447 else if (wpa_s->disconnected) {
4448 wpa_s->disconnected = 0;
4449 wpa_s->reassociate = 1;
4450 wpa_supplicant_req_scan(wpa_s, 0, 0);
4451 }
4452#ifdef IEEE8021X_EAPOL
4453 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
4454 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
4455 reply_len = -1;
4456#endif /* IEEE8021X_EAPOL */
4457#ifdef CONFIG_PEERKEY
4458 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
4459 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
4460 reply_len = -1;
4461#endif /* CONFIG_PEERKEY */
4462#ifdef CONFIG_IEEE80211R
4463 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
4464 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
4465 reply_len = -1;
4466#endif /* CONFIG_IEEE80211R */
4467#ifdef CONFIG_WPS
4468 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
4469 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
4470 if (res == -2) {
4471 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
4472 reply_len = 17;
4473 } else if (res)
4474 reply_len = -1;
4475 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
4476 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
4477 if (res == -2) {
4478 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
4479 reply_len = 17;
4480 } else if (res)
4481 reply_len = -1;
4482 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
4483 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
4484 reply,
4485 reply_size);
4486 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
4487 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
4488 wpa_s, buf + 14, reply, reply_size);
4489 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
4490 if (wpas_wps_cancel(wpa_s))
4491 reply_len = -1;
4492#ifdef CONFIG_WPS_OOB
4493 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
4494 if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
4495 reply_len = -1;
4496#endif /* CONFIG_WPS_OOB */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004497#ifdef CONFIG_WPS_NFC
4498 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
4499 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
4500 reply_len = -1;
4501 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
4502 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
4503 reply_len = -1;
4504 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
4505 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
4506 wpa_s, buf + 14, reply, reply_size);
4507 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
4508 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
4509 buf + 17))
4510 reply_len = -1;
4511#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
4513 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
4514 reply_len = -1;
4515#ifdef CONFIG_AP
4516 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
4517 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
4518 wpa_s, buf + 11, reply, reply_size);
4519#endif /* CONFIG_AP */
4520#ifdef CONFIG_WPS_ER
4521 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
4522 if (wpas_wps_er_start(wpa_s, NULL))
4523 reply_len = -1;
4524 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
4525 if (wpas_wps_er_start(wpa_s, buf + 13))
4526 reply_len = -1;
4527 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
4528 if (wpas_wps_er_stop(wpa_s))
4529 reply_len = -1;
4530 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
4531 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
4532 reply_len = -1;
4533 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
4534 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
4535 if (ret == -2) {
4536 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
4537 reply_len = 17;
4538 } else if (ret == -3) {
4539 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
4540 reply_len = 18;
4541 } else if (ret == -4) {
4542 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
4543 reply_len = 20;
4544 } else if (ret)
4545 reply_len = -1;
4546 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
4547 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
4548 reply_len = -1;
4549 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
4550 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
4551 buf + 18))
4552 reply_len = -1;
4553 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
4554 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
4555 reply_len = -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004556#ifdef CONFIG_WPS_NFC
4557 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
4558 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
4559 wpa_s, buf + 24, reply, reply_size);
4560#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004561#endif /* CONFIG_WPS_ER */
4562#endif /* CONFIG_WPS */
4563#ifdef CONFIG_IBSS_RSN
4564 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
4565 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
4566 reply_len = -1;
4567#endif /* CONFIG_IBSS_RSN */
4568#ifdef CONFIG_P2P
4569 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
4570 if (p2p_ctrl_find(wpa_s, buf + 9))
4571 reply_len = -1;
4572 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
4573 if (p2p_ctrl_find(wpa_s, ""))
4574 reply_len = -1;
4575 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
4576 wpas_p2p_stop_find(wpa_s);
4577 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
4578 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
4579 reply_size);
4580 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
4581 if (p2p_ctrl_listen(wpa_s, buf + 11))
4582 reply_len = -1;
4583 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
4584 if (p2p_ctrl_listen(wpa_s, ""))
4585 reply_len = -1;
4586 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
4587 if (wpas_p2p_group_remove(wpa_s, buf + 17))
4588 reply_len = -1;
4589 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004590 if (wpas_p2p_group_add(wpa_s, 0, 0, 0))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004591 reply_len = -1;
4592 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
4593 if (p2p_ctrl_group_add(wpa_s, buf + 14))
4594 reply_len = -1;
4595 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
4596 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
4597 reply_len = -1;
4598 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
4599 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
4600 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
4601 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
4602 reply_size);
4603 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
4604 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
4605 reply_len = -1;
4606 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
4607 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
4608 reply_len = -1;
4609 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07004610#ifdef ANDROID_P2P
4611 wpas_p2p_sd_service_update(wpa_s, SRV_UPDATE);
4612#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004613 wpas_p2p_sd_service_update(wpa_s);
Dmitry Shmidtb5e8f062012-08-08 10:56:33 -07004614#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
4616 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
4617 reply_len = -1;
4618 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
4619 wpas_p2p_service_flush(wpa_s);
4620 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
4621 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
4622 reply_len = -1;
4623 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
4624 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
4625 reply_len = -1;
4626 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
4627 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
4628 reply_len = -1;
4629 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
4630 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
4631 reply_len = -1;
4632 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
4633 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
4634 reply_size);
4635 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
4636 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
4637 reply_len = -1;
4638 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
4639 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
4640 wpa_s->force_long_sd = 0;
4641 if (wpa_s->global->p2p)
4642 p2p_flush(wpa_s->global->p2p);
4643 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
4644 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
4645 reply_len = -1;
4646 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
4647 if (wpas_p2p_cancel(wpa_s))
4648 reply_len = -1;
4649 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
4650 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
4651 reply_len = -1;
4652 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
4653 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
4654 reply_len = -1;
4655 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
4656 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
4657 reply_len = -1;
4658 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
4659 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
4660 reply_len = -1;
4661#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004662#ifdef CONFIG_WIFI_DISPLAY
4663 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
4664 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
4665 reply_len = -1;
4666 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
4667 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
4668 reply, reply_size);
4669#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004670#ifdef CONFIG_INTERWORKING
4671 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
4672 if (interworking_fetch_anqp(wpa_s) < 0)
4673 reply_len = -1;
4674 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
4675 interworking_stop_fetch_anqp(wpa_s);
4676 } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
4677 if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
4678 NULL) < 0)
4679 reply_len = -1;
4680 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
4681 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
4682 reply_len = -1;
4683 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
4684 if (get_anqp(wpa_s, buf + 9) < 0)
4685 reply_len = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004686 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
4687 if (gas_request(wpa_s, buf + 12) < 0)
4688 reply_len = -1;
4689 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
4690 reply_len = gas_response_get(wpa_s, buf + 17, reply,
4691 reply_size);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004692#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004693#ifdef CONFIG_HS20
4694 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
4695 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
4696 reply_len = -1;
4697 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
4698 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
4699 reply_len = -1;
4700#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004701 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
4702 {
4703 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
4704 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
4705 reply_len = -1;
4706 else
4707 ctrl_rsp = 1;
4708 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
4709 if (wpa_supplicant_reload_configuration(wpa_s))
4710 reply_len = -1;
4711 } else if (os_strcmp(buf, "TERMINATE") == 0) {
4712 wpa_supplicant_terminate_proc(wpa_s->global);
4713 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
4714 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
4715 reply_len = -1;
Dmitry Shmidte19501d2011-03-16 14:32:18 -07004716 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004717 reply_len = wpa_supplicant_ctrl_iface_blacklist(
4718 wpa_s, buf + 9, reply, reply_size);
4719 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
4720 reply_len = wpa_supplicant_ctrl_iface_log_level(
4721 wpa_s, buf + 9, reply, reply_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
4723 reply_len = wpa_supplicant_ctrl_iface_list_networks(
4724 wpa_s, reply, reply_size);
4725 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004726#ifdef CONFIG_SME
4727 wpa_s->sme.prev_bssid_set = 0;
4728#endif /* CONFIG_SME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004729 wpa_s->reassociate = 0;
4730 wpa_s->disconnected = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004731 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004732 wpa_supplicant_cancel_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004733 wpa_supplicant_deauthenticate(wpa_s,
4734 WLAN_REASON_DEAUTH_LEAVING);
4735 } else if (os_strcmp(buf, "SCAN") == 0) {
4736 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4737 reply_len = -1;
4738 else {
4739 if (!wpa_s->scanning &&
4740 ((wpa_s->wpa_state <= WPA_SCANNING) ||
4741 (wpa_s->wpa_state == WPA_COMPLETED))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004742 wpa_s->normal_scans = 0;
4743 wpa_s->scan_req = 2;
4744 wpa_supplicant_req_scan(wpa_s, 0, 0);
4745 } else if (wpa_s->sched_scanning) {
4746 wpa_printf(MSG_DEBUG, "Stop ongoing "
4747 "sched_scan to allow requested "
4748 "full scan to proceed");
4749 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004750 wpa_s->scan_req = 2;
4751 wpa_supplicant_req_scan(wpa_s, 0, 0);
4752 } else {
4753 wpa_printf(MSG_DEBUG, "Ongoing scan action - "
4754 "reject new request");
4755 reply_len = os_snprintf(reply, reply_size,
4756 "FAIL-BUSY\n");
4757 }
4758 }
4759 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
4760 reply_len = wpa_supplicant_ctrl_iface_scan_results(
4761 wpa_s, reply, reply_size);
4762 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
4763 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
4764 reply_len = -1;
4765 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
4766 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
4767 reply_len = -1;
4768 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
4769 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
4770 reply_len = -1;
4771 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
4772 reply_len = wpa_supplicant_ctrl_iface_add_network(
4773 wpa_s, reply, reply_size);
4774 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
4775 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
4776 reply_len = -1;
4777 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
4778 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
4779 reply_len = -1;
4780 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
4781 reply_len = wpa_supplicant_ctrl_iface_get_network(
4782 wpa_s, buf + 12, reply, reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004783 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
4784 reply_len = wpa_supplicant_ctrl_iface_list_creds(
4785 wpa_s, reply, reply_size);
4786 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
4787 reply_len = wpa_supplicant_ctrl_iface_add_cred(
4788 wpa_s, reply, reply_size);
4789 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
4790 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
4791 reply_len = -1;
4792 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
4793 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
4794 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004795#ifndef CONFIG_NO_CONFIG_WRITE
4796 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
4797 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
4798 reply_len = -1;
4799#endif /* CONFIG_NO_CONFIG_WRITE */
4800 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
4801 reply_len = wpa_supplicant_ctrl_iface_get_capability(
4802 wpa_s, buf + 15, reply, reply_size);
4803 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
4804 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
4805 reply_len = -1;
4806 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
4807 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
4808 reply_len = -1;
4809 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
4810 reply_len = wpa_supplicant_global_iface_list(
4811 wpa_s->global, reply, reply_size);
4812 } else if (os_strcmp(buf, "INTERFACES") == 0) {
4813 reply_len = wpa_supplicant_global_iface_interfaces(
4814 wpa_s->global, reply, reply_size);
4815 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
4816 reply_len = wpa_supplicant_ctrl_iface_bss(
4817 wpa_s, buf + 4, reply, reply_size);
4818#ifdef CONFIG_AP
4819 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
4820 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
4821 } else if (os_strncmp(buf, "STA ", 4) == 0) {
4822 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
4823 reply_size);
4824 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
4825 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
4826 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004827 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
4828 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
4829 reply_len = -1;
4830 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
4831 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
4832 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004833#endif /* CONFIG_AP */
4834 } else if (os_strcmp(buf, "SUSPEND") == 0) {
4835 wpas_notify_suspend(wpa_s->global);
4836 } else if (os_strcmp(buf, "RESUME") == 0) {
4837 wpas_notify_resume(wpa_s->global);
4838 } else if (os_strcmp(buf, "DROP_SA") == 0) {
4839 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
4840 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
4841 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
4842 reply_len = -1;
4843 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
4844 if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
4845 reply_len = -1;
4846 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
4847 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
4848 reply_len = -1;
4849 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
4850 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
4851 buf + 17))
4852 reply_len = -1;
Dmitry Shmidtf48e4f92012-08-24 11:14:44 -07004853 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
4854 if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
4855 reply_len = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004856#ifdef CONFIG_TDLS
4857 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
4858 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
4859 reply_len = -1;
4860 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
4861 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
4862 reply_len = -1;
4863 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
4864 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
4865 reply_len = -1;
4866#endif /* CONFIG_TDLS */
4867 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
4868 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
4869 reply_size);
Yuhao Zhengfcd6f212012-07-27 10:37:52 -07004870 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
4871 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
4872 reply_size);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004873#ifdef CONFIG_AUTOSCAN
4874 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
4875 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
4876 reply_len = -1;
4877#endif /* CONFIG_AUTOSCAN */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004878#ifdef ANDROID
Dmitry Shmidtbd567ad2011-05-09 14:17:09 -07004879 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
4880 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
4881 reply_size);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004882#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004883 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
4884 eapol_sm_request_reauth(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004885 } else {
4886 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
4887 reply_len = 16;
4888 }
4889
4890 if (reply_len < 0) {
4891 os_memcpy(reply, "FAIL\n", 5);
4892 reply_len = 5;
4893 }
4894
4895 if (ctrl_rsp)
4896 eapol_sm_notify_ctrl_response(wpa_s->eapol);
4897
4898 *resp_len = reply_len;
4899 return reply;
4900}
4901
4902
4903static int wpa_supplicant_global_iface_add(struct wpa_global *global,
4904 char *cmd)
4905{
4906 struct wpa_interface iface;
4907 char *pos;
4908
4909 /*
4910 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
4911 * TAB<bridge_ifname>
4912 */
4913 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
4914
4915 os_memset(&iface, 0, sizeof(iface));
4916
4917 do {
4918 iface.ifname = pos = cmd;
4919 pos = os_strchr(pos, '\t');
4920 if (pos)
4921 *pos++ = '\0';
4922 if (iface.ifname[0] == '\0')
4923 return -1;
4924 if (pos == NULL)
4925 break;
4926
4927 iface.confname = pos;
4928 pos = os_strchr(pos, '\t');
4929 if (pos)
4930 *pos++ = '\0';
4931 if (iface.confname[0] == '\0')
4932 iface.confname = NULL;
4933 if (pos == NULL)
4934 break;
4935
4936 iface.driver = pos;
4937 pos = os_strchr(pos, '\t');
4938 if (pos)
4939 *pos++ = '\0';
4940 if (iface.driver[0] == '\0')
4941 iface.driver = NULL;
4942 if (pos == NULL)
4943 break;
4944
4945 iface.ctrl_interface = pos;
4946 pos = os_strchr(pos, '\t');
4947 if (pos)
4948 *pos++ = '\0';
4949 if (iface.ctrl_interface[0] == '\0')
4950 iface.ctrl_interface = NULL;
4951 if (pos == NULL)
4952 break;
4953
4954 iface.driver_param = pos;
4955 pos = os_strchr(pos, '\t');
4956 if (pos)
4957 *pos++ = '\0';
4958 if (iface.driver_param[0] == '\0')
4959 iface.driver_param = NULL;
4960 if (pos == NULL)
4961 break;
4962
4963 iface.bridge_ifname = pos;
4964 pos = os_strchr(pos, '\t');
4965 if (pos)
4966 *pos++ = '\0';
4967 if (iface.bridge_ifname[0] == '\0')
4968 iface.bridge_ifname = NULL;
4969 if (pos == NULL)
4970 break;
4971 } while (0);
4972
4973 if (wpa_supplicant_get_iface(global, iface.ifname))
4974 return -1;
4975
4976 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
4977}
4978
4979
4980static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
4981 char *cmd)
4982{
4983 struct wpa_supplicant *wpa_s;
4984
4985 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
4986
4987 wpa_s = wpa_supplicant_get_iface(global, cmd);
4988 if (wpa_s == NULL)
4989 return -1;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07004990 return wpa_supplicant_remove_iface(global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004991}
4992
4993
4994static void wpa_free_iface_info(struct wpa_interface_info *iface)
4995{
4996 struct wpa_interface_info *prev;
4997
4998 while (iface) {
4999 prev = iface;
5000 iface = iface->next;
5001
5002 os_free(prev->ifname);
5003 os_free(prev->desc);
5004 os_free(prev);
5005 }
5006}
5007
5008
5009static int wpa_supplicant_global_iface_list(struct wpa_global *global,
5010 char *buf, int len)
5011{
5012 int i, res;
5013 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
5014 char *pos, *end;
5015
5016 for (i = 0; wpa_drivers[i]; i++) {
5017 struct wpa_driver_ops *drv = wpa_drivers[i];
5018 if (drv->get_interfaces == NULL)
5019 continue;
5020 tmp = drv->get_interfaces(global->drv_priv[i]);
5021 if (tmp == NULL)
5022 continue;
5023
5024 if (last == NULL)
5025 iface = last = tmp;
5026 else
5027 last->next = tmp;
5028 while (last->next)
5029 last = last->next;
5030 }
5031
5032 pos = buf;
5033 end = buf + len;
5034 for (tmp = iface; tmp; tmp = tmp->next) {
5035 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
5036 tmp->drv_name, tmp->ifname,
5037 tmp->desc ? tmp->desc : "");
5038 if (res < 0 || res >= end - pos) {
5039 *pos = '\0';
5040 break;
5041 }
5042 pos += res;
5043 }
5044
5045 wpa_free_iface_info(iface);
5046
5047 return pos - buf;
5048}
5049
5050
5051static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
5052 char *buf, int len)
5053{
5054 int res;
5055 char *pos, *end;
5056 struct wpa_supplicant *wpa_s;
5057
5058 wpa_s = global->ifaces;
5059 pos = buf;
5060 end = buf + len;
5061
5062 while (wpa_s) {
5063 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
5064 if (res < 0 || res >= end - pos) {
5065 *pos = '\0';
5066 break;
5067 }
5068 pos += res;
5069 wpa_s = wpa_s->next;
5070 }
5071 return pos - buf;
5072}
5073
5074
5075char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
5076 char *buf, size_t *resp_len)
5077{
5078 char *reply;
5079 const int reply_size = 2048;
5080 int reply_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005081 int level = MSG_DEBUG;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005082
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005083 if (os_strcmp(buf, "PING") == 0)
5084 level = MSG_EXCESSIVE;
5085 wpa_hexdump_ascii(level, "RX global ctrl_iface",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005086 (const u8 *) buf, os_strlen(buf));
5087
5088 reply = os_malloc(reply_size);
5089 if (reply == NULL) {
5090 *resp_len = 1;
5091 return NULL;
5092 }
5093
5094 os_memcpy(reply, "OK\n", 3);
5095 reply_len = 3;
5096
5097 if (os_strcmp(buf, "PING") == 0) {
5098 os_memcpy(reply, "PONG\n", 5);
5099 reply_len = 5;
5100 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
5101 if (wpa_supplicant_global_iface_add(global, buf + 14))
5102 reply_len = -1;
5103 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
5104 if (wpa_supplicant_global_iface_remove(global, buf + 17))
5105 reply_len = -1;
5106 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
5107 reply_len = wpa_supplicant_global_iface_list(
5108 global, reply, reply_size);
5109 } else if (os_strcmp(buf, "INTERFACES") == 0) {
5110 reply_len = wpa_supplicant_global_iface_interfaces(
5111 global, reply, reply_size);
5112 } else if (os_strcmp(buf, "TERMINATE") == 0) {
5113 wpa_supplicant_terminate_proc(global);
5114 } else if (os_strcmp(buf, "SUSPEND") == 0) {
5115 wpas_notify_suspend(global);
5116 } else if (os_strcmp(buf, "RESUME") == 0) {
5117 wpas_notify_resume(global);
5118 } else {
5119 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
5120 reply_len = 16;
5121 }
5122
5123 if (reply_len < 0) {
5124 os_memcpy(reply, "FAIL\n", 5);
5125 reply_len = 5;
5126 }
5127
5128 *resp_len = reply_len;
5129 return reply;
5130}