]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/ctrl_iface.c
Reject new SCAN commands if there is a pending request
[thirdparty/hostap.git] / wpa_supplicant / ctrl_iface.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant / Control interface (shared code for all backends)
d31b5ac7 3 * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
3a068632 9#include "utils/includes.h"
4a6cc862
JM
10#ifdef CONFIG_TESTING_OPTIONS
11#include <net/ethernet.h>
12#include <netinet/ip.h>
13#endif /* CONFIG_TESTING_OPTIONS */
6fc6879b 14
3a068632
JM
15#include "utils/common.h"
16#include "utils/eloop.h"
8aaafcee 17#include "utils/uuid.h"
acec8d32 18#include "common/version.h"
3a068632 19#include "common/ieee802_11_defs.h"
337c781f 20#include "common/ieee802_11_common.h"
3a068632 21#include "common/wpa_ctrl.h"
9d4ff04a 22#include "ap/hostapd.h"
3a068632
JM
23#include "eap_peer/eap.h"
24#include "eapol_supp/eapol_supp_sm.h"
3acb5005 25#include "rsn_supp/wpa.h"
3a068632
JM
26#include "rsn_supp/preauth.h"
27#include "rsn_supp/pmksa_cache.h"
28#include "l2_packet/l2_packet.h"
29#include "wps/wps.h"
6fc6879b 30#include "config.h"
6fc6879b 31#include "wpa_supplicant_i.h"
2d5b792d 32#include "driver_i.h"
fcc60db4 33#include "wps_supplicant.h"
11ef8d35 34#include "ibss_rsn.h"
3ec97afe 35#include "ap.h"
b563b388
JM
36#include "p2p_supplicant.h"
37#include "p2p/p2p.h"
a8918e86 38#include "hs20_supplicant.h"
9675ce35 39#include "wifi_display.h"
8bac466b 40#include "notify.h"
3a068632 41#include "bss.h"
9ba9fa07 42#include "scan.h"
3a068632 43#include "ctrl_iface.h"
afc064fe 44#include "interworking.h"
9aa10e2b 45#include "blacklist.h"
bc5d330a 46#include "autoscan.h"
e9199e31 47#include "wnm_sta.h"
60b893df 48#include "offchannel.h"
7a4a93b9 49#include "drivers/driver.h"
79070906 50#include "mesh.h"
6fc6879b 51
4b4a8ae5
JM
52static int wpa_supplicant_global_iface_list(struct wpa_global *global,
53 char *buf, int len);
6fc6879b
JM
54static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
55 char *buf, int len);
d3c9c35f
DS
56static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
57 char *val);
6fc6879b 58
d445a5cd
JM
59static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
60{
61 char *pos;
62 u8 addr[ETH_ALEN], *filter = NULL, *n;
63 size_t count = 0;
64
65 pos = val;
66 while (pos) {
67 if (*pos == '\0')
68 break;
1485ec07
JM
69 if (hwaddr_aton(pos, addr)) {
70 os_free(filter);
d445a5cd 71 return -1;
1485ec07 72 }
067ffa26 73 n = os_realloc_array(filter, count + 1, ETH_ALEN);
d445a5cd
JM
74 if (n == NULL) {
75 os_free(filter);
76 return -1;
77 }
78 filter = n;
79 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
80 count++;
81
82 pos = os_strchr(pos, ' ');
83 if (pos)
84 pos++;
85 }
86
87 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
88 os_free(wpa_s->bssid_filter);
89 wpa_s->bssid_filter = filter;
90 wpa_s->bssid_filter_count = count;
91
92 return 0;
93}
94
95
6407f413
JM
96static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
97{
98 char *pos;
99 u8 addr[ETH_ALEN], *bssid = NULL, *n;
100 struct wpa_ssid_value *ssid = NULL, *ns;
101 size_t count = 0, ssid_count = 0;
102 struct wpa_ssid *c;
103
104 /*
65015b2d 105 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
6407f413
JM
106 * SSID_SPEC ::= ssid <SSID_HEX>
107 * BSSID_SPEC ::= bssid <BSSID_HEX>
108 */
109
110 pos = val;
111 while (pos) {
112 if (*pos == '\0')
113 break;
114 if (os_strncmp(pos, "bssid ", 6) == 0) {
115 int res;
116 pos += 6;
117 res = hwaddr_aton2(pos, addr);
118 if (res < 0) {
119 os_free(ssid);
120 os_free(bssid);
121 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
122 "BSSID value '%s'", pos);
123 return -1;
124 }
125 pos += res;
126 n = os_realloc_array(bssid, count + 1, ETH_ALEN);
127 if (n == NULL) {
128 os_free(ssid);
129 os_free(bssid);
130 return -1;
131 }
132 bssid = n;
133 os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
134 count++;
135 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
136 char *end;
137 pos += 5;
138
139 end = pos;
140 while (*end) {
141 if (*end == '\0' || *end == ' ')
142 break;
143 end++;
144 }
145
146 ns = os_realloc_array(ssid, ssid_count + 1,
147 sizeof(struct wpa_ssid_value));
148 if (ns == NULL) {
149 os_free(ssid);
150 os_free(bssid);
151 return -1;
152 }
153 ssid = ns;
154
155 if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
156 hexstr2bin(pos, ssid[ssid_count].ssid,
157 (end - pos) / 2) < 0) {
158 os_free(ssid);
159 os_free(bssid);
160 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
161 "SSID value '%s'", pos);
162 return -1;
163 }
164 ssid[ssid_count].ssid_len = (end - pos) / 2;
165 wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
166 ssid[ssid_count].ssid,
167 ssid[ssid_count].ssid_len);
168 ssid_count++;
169 pos = end;
170 } else {
171 wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
172 "'%s'", pos);
173 os_free(ssid);
174 os_free(bssid);
175 return -1;
176 }
177
178 pos = os_strchr(pos, ' ');
179 if (pos)
180 pos++;
181 }
182
183 wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
184 os_free(wpa_s->disallow_aps_bssid);
185 wpa_s->disallow_aps_bssid = bssid;
186 wpa_s->disallow_aps_bssid_count = count;
187
188 wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
189 os_free(wpa_s->disallow_aps_ssid);
190 wpa_s->disallow_aps_ssid = ssid;
191 wpa_s->disallow_aps_ssid_count = ssid_count;
192
193 if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
194 return 0;
195
196 c = wpa_s->current_ssid;
197 if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
198 return 0;
199
200 if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
201 !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
202 return 0;
203
204 wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
205 "because current AP was marked disallowed");
206
207#ifdef CONFIG_SME
208 wpa_s->sme.prev_bssid_set = 0;
209#endif /* CONFIG_SME */
210 wpa_s->reassociate = 1;
211 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
212 wpa_supplicant_req_scan(wpa_s, 0, 0);
213
214 return 0;
215}
216
217
1120e452
JM
218#ifndef CONFIG_NO_CONFIG_BLOBS
219static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
220{
221 char *name = pos;
222 struct wpa_config_blob *blob;
223 size_t len;
224
225 pos = os_strchr(pos, ' ');
226 if (pos == NULL)
227 return -1;
228 *pos++ = '\0';
229 len = os_strlen(pos);
230 if (len & 1)
231 return -1;
232
233 wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
234 blob = os_zalloc(sizeof(*blob));
235 if (blob == NULL)
236 return -1;
237 blob->name = os_strdup(name);
238 blob->data = os_malloc(len / 2);
239 if (blob->name == NULL || blob->data == NULL) {
240 wpa_config_free_blob(blob);
241 return -1;
242 }
243
244 if (hexstr2bin(pos, blob->data, len / 2) < 0) {
245 wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
246 wpa_config_free_blob(blob);
247 return -1;
248 }
249 blob->len = len / 2;
250
251 wpa_config_set_blob(wpa_s->conf, blob);
252
253 return 0;
254}
255#endif /* CONFIG_NO_CONFIG_BLOBS */
256
d3c9c35f
DS
257
258static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
259{
260 char *params;
261 char *pos;
262 int *freqs = NULL;
263 int ret;
264
265 if (atoi(cmd)) {
266 params = os_strchr(cmd, ' ');
267 os_free(wpa_s->manual_sched_scan_freqs);
268 if (params) {
269 params++;
270 pos = os_strstr(params, "freq=");
271 if (pos)
272 freqs = freq_range_to_channel_list(wpa_s,
273 pos + 5);
274 }
275 wpa_s->manual_sched_scan_freqs = freqs;
276 ret = wpas_start_pno(wpa_s);
277 } else {
278 ret = wpas_stop_pno(wpa_s);
279 }
280 return ret;
281}
282
283
6fc6879b
JM
284static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
285 char *cmd)
286{
287 char *value;
288 int ret = 0;
289
290 value = os_strchr(cmd, ' ');
291 if (value == NULL)
292 return -1;
293 *value++ = '\0';
294
295 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
296 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
297 eapol_sm_configure(wpa_s->eapol,
298 atoi(value), -1, -1, -1);
299 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
300 eapol_sm_configure(wpa_s->eapol,
301 -1, atoi(value), -1, -1);
302 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
303 eapol_sm_configure(wpa_s->eapol,
304 -1, -1, atoi(value), -1);
305 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
306 eapol_sm_configure(wpa_s->eapol,
307 -1, -1, -1, atoi(value));
308 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
309 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
310 atoi(value)))
311 ret = -1;
312 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
313 0) {
314 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
315 atoi(value)))
316 ret = -1;
317 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
318 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
319 ret = -1;
42f50264
JM
320 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
321 wpa_s->wps_fragment_size = atoi(value);
b4e34f2f
JM
322#ifdef CONFIG_WPS_TESTING
323 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
324 long int val;
325 val = strtol(value, NULL, 0);
326 if (val < 0 || val > 0xff) {
327 ret = -1;
328 wpa_printf(MSG_DEBUG, "WPS: Invalid "
329 "wps_version_number %ld", val);
330 } else {
331 wps_version_number = val;
332 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
333 "version %u.%u",
334 (wps_version_number & 0xf0) >> 4,
335 wps_version_number & 0x0f);
336 }
337 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
338 wps_testing_dummy_cred = atoi(value);
339 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
340 wps_testing_dummy_cred);
91226e0d
JM
341 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
342 wps_corrupt_pkhash = atoi(value);
343 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
344 wps_corrupt_pkhash);
b4e34f2f 345#endif /* CONFIG_WPS_TESTING */
b6c79a99
JM
346 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
347 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
348 ret = -1;
9d2cb3ec 349#ifdef CONFIG_TDLS
5b0e6ece
JM
350#ifdef CONFIG_TDLS_TESTING
351 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
352 extern unsigned int tdls_testing;
353 tdls_testing = strtol(value, NULL, 0);
354 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
355#endif /* CONFIG_TDLS_TESTING */
b8f64582
JM
356 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
357 int disabled = atoi(value);
358 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
359 if (disabled) {
360 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
361 ret = -1;
362 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
363 ret = -1;
364 wpa_tdls_enable(wpa_s->wpa, !disabled);
365#endif /* CONFIG_TDLS */
b5c68312 366 } else if (os_strcasecmp(cmd, "pno") == 0) {
d3c9c35f 367 ret = wpas_ctrl_pno(wpa_s, value);
8b9d0bfa
JM
368 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
369 int disabled = atoi(value);
370 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
371 ret = -1;
372 else if (disabled)
373 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
aa074a64
JM
374 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
375 if (os_strcmp(value, "disable") == 0)
376 wpa_s->set_sta_uapsd = 0;
377 else {
378 int be, bk, vi, vo;
379 char *pos;
380 /* format: BE,BK,VI,VO;max SP Length */
381 be = atoi(value);
382 pos = os_strchr(value, ',');
383 if (pos == NULL)
384 return -1;
385 pos++;
386 bk = atoi(pos);
387 pos = os_strchr(pos, ',');
388 if (pos == NULL)
389 return -1;
390 pos++;
391 vi = atoi(pos);
392 pos = os_strchr(pos, ',');
393 if (pos == NULL)
394 return -1;
395 pos++;
396 vo = atoi(pos);
397 /* ignore max SP Length for now */
398
399 wpa_s->set_sta_uapsd = 1;
400 wpa_s->sta_uapsd = 0;
401 if (be)
402 wpa_s->sta_uapsd |= BIT(0);
403 if (bk)
404 wpa_s->sta_uapsd |= BIT(1);
405 if (vi)
406 wpa_s->sta_uapsd |= BIT(2);
407 if (vo)
408 wpa_s->sta_uapsd |= BIT(3);
409 }
b2ff1681
JM
410 } else if (os_strcasecmp(cmd, "ps") == 0) {
411 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
9675ce35
JM
412#ifdef CONFIG_WIFI_DISPLAY
413 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
bab6677a
JM
414 int enabled = !!atoi(value);
415 if (enabled && !wpa_s->global->p2p)
416 ret = -1;
417 else
418 wifi_display_enable(wpa_s->global, enabled);
9675ce35 419#endif /* CONFIG_WIFI_DISPLAY */
d445a5cd
JM
420 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
421 ret = set_bssid_filter(wpa_s, value);
6407f413
JM
422 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
423 ret = set_disallow_aps(wpa_s, value);
2ec535fd
JM
424 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
425 wpa_s->no_keep_alive = !!atoi(value);
60b893df
JM
426#ifdef CONFIG_TESTING_OPTIONS
427 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
428 wpa_s->ext_mgmt_frame_handling = !!atoi(value);
9d4ff04a
JM
429 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
430 wpa_s->ext_eapol_frame_io = !!atoi(value);
431#ifdef CONFIG_AP
432 if (wpa_s->ap_iface) {
433 wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
434 wpa_s->ext_eapol_frame_io;
435 }
436#endif /* CONFIG_AP */
1f94e4ee
JM
437 } else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
438 wpa_s->extra_roc_dur = atoi(value);
60b893df 439#endif /* CONFIG_TESTING_OPTIONS */
1120e452
JM
440#ifndef CONFIG_NO_CONFIG_BLOBS
441 } else if (os_strcmp(cmd, "blob") == 0) {
442 ret = wpas_ctrl_set_blob(wpa_s, value);
443#endif /* CONFIG_NO_CONFIG_BLOBS */
611aea7d
JM
444 } else {
445 value[-1] = '=';
446 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
447 if (ret == 0)
448 wpa_supplicant_update_config(wpa_s);
449 }
6fc6879b
JM
450
451 return ret;
452}
453
454
acec8d32
JM
455static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
456 char *cmd, char *buf, size_t buflen)
457{
6ce937b8 458 int res = -1;
acec8d32
JM
459
460 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
461
462 if (os_strcmp(cmd, "version") == 0) {
463 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
6ce937b8
DS
464 } else if (os_strcasecmp(cmd, "country") == 0) {
465 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
466 res = os_snprintf(buf, buflen, "%c%c",
467 wpa_s->conf->country[0],
468 wpa_s->conf->country[1]);
9675ce35
JM
469#ifdef CONFIG_WIFI_DISPLAY
470 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
bab6677a
JM
471 int enabled;
472 if (wpa_s->global->p2p == NULL ||
473 wpa_s->global->p2p_disabled)
474 enabled = 0;
475 else
476 enabled = wpa_s->global->wifi_display;
477 res = os_snprintf(buf, buflen, "%d", enabled);
9675ce35 478#endif /* CONFIG_WIFI_DISPLAY */
fa7ae950
JM
479#ifdef CONFIG_TESTING_GET_GTK
480 } else if (os_strcmp(cmd, "gtk") == 0) {
481 if (wpa_s->last_gtk_len == 0)
482 return -1;
483 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
484 wpa_s->last_gtk_len);
485 return res;
486#endif /* CONFIG_TESTING_GET_GTK */
acec8d32
JM
487 }
488
1f102d3b 489 if (os_snprintf_error(buflen, res))
6ce937b8
DS
490 return -1;
491 return res;
acec8d32
JM
492}
493
494
ec717917 495#ifdef IEEE8021X_EAPOL
6fc6879b
JM
496static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
497 char *addr)
498{
499 u8 bssid[ETH_ALEN];
500 struct wpa_ssid *ssid = wpa_s->current_ssid;
501
502 if (hwaddr_aton(addr, bssid)) {
503 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
504 "'%s'", addr);
505 return -1;
506 }
507
508 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
509 rsn_preauth_deinit(wpa_s->wpa);
510 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
511 return -1;
512
513 return 0;
514}
ec717917 515#endif /* IEEE8021X_EAPOL */
6fc6879b
JM
516
517
518#ifdef CONFIG_PEERKEY
519/* MLME-STKSTART.request(peer) */
520static int wpa_supplicant_ctrl_iface_stkstart(
521 struct wpa_supplicant *wpa_s, char *addr)
522{
523 u8 peer[ETH_ALEN];
524
525 if (hwaddr_aton(addr, peer)) {
526 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
a7b6c422 527 "address '%s'", addr);
6fc6879b
JM
528 return -1;
529 }
530
531 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
532 MAC2STR(peer));
533
534 return wpa_sm_stkstart(wpa_s->wpa, peer);
535}
536#endif /* CONFIG_PEERKEY */
537
538
281ff0aa
GP
539#ifdef CONFIG_TDLS
540
541static int wpa_supplicant_ctrl_iface_tdls_discover(
542 struct wpa_supplicant *wpa_s, char *addr)
543{
544 u8 peer[ETH_ALEN];
2d565a61 545 int ret;
281ff0aa
GP
546
547 if (hwaddr_aton(addr, peer)) {
548 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
549 "address '%s'", addr);
550 return -1;
551 }
552
553 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
554 MAC2STR(peer));
555
2d565a61
AN
556 if (wpa_tdls_is_external_setup(wpa_s->wpa))
557 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
558 else
559 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
560
561 return ret;
281ff0aa
GP
562}
563
564
565static int wpa_supplicant_ctrl_iface_tdls_setup(
566 struct wpa_supplicant *wpa_s, char *addr)
567{
568 u8 peer[ETH_ALEN];
94377fbc 569 int ret;
281ff0aa
GP
570
571 if (hwaddr_aton(addr, peer)) {
572 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
573 "address '%s'", addr);
574 return -1;
575 }
576
577 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
578 MAC2STR(peer));
579
800d5872
SD
580 if ((wpa_s->conf->tdls_external_control) &&
581 wpa_tdls_is_external_setup(wpa_s->wpa))
582 return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
583
3887878e
SD
584 wpa_tdls_remove(wpa_s->wpa, peer);
585
586 if (wpa_tdls_is_external_setup(wpa_s->wpa))
587 ret = wpa_tdls_start(wpa_s->wpa, peer);
588 else
589 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
2d565a61 590
94377fbc 591 return ret;
281ff0aa
GP
592}
593
594
595static int wpa_supplicant_ctrl_iface_tdls_teardown(
596 struct wpa_supplicant *wpa_s, char *addr)
597{
598 u8 peer[ETH_ALEN];
4ed8d954 599 int ret;
281ff0aa 600
38ddccae
AN
601 if (os_strcmp(addr, "*") == 0) {
602 /* remove everyone */
603 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
604 wpa_tdls_teardown_peers(wpa_s->wpa);
605 return 0;
606 }
607
281ff0aa
GP
608 if (hwaddr_aton(addr, peer)) {
609 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
610 "address '%s'", addr);
611 return -1;
612 }
613
614 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
615 MAC2STR(peer));
616
800d5872
SD
617 if ((wpa_s->conf->tdls_external_control) &&
618 wpa_tdls_is_external_setup(wpa_s->wpa))
619 return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
620
4ed8d954
AS
621 if (wpa_tdls_is_external_setup(wpa_s->wpa))
622 ret = wpa_tdls_teardown_link(
623 wpa_s->wpa, peer,
624 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
625 else
626 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
627
628 return ret;
281ff0aa
GP
629}
630
6e9375e4
DS
631
632static int ctrl_iface_get_capability_tdls(
633 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
634{
635 int ret;
636
637 ret = os_snprintf(buf, buflen, "%s\n",
638 wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
639 (wpa_s->drv_flags &
640 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
641 "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
7bdd8981 642 if (os_snprintf_error(buflen, ret))
6e9375e4
DS
643 return -1;
644 return ret;
645}
646
281ff0aa
GP
647#endif /* CONFIG_TDLS */
648
649
eb2f2088
MB
650static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
651{
652 char *token, *context = NULL;
653 struct wmm_ac_ts_setup_params params = {
654 .tsid = 0xff,
655 .direction = 0xff,
656 };
657
658 while ((token = str_token(cmd, " ", &context))) {
659 if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
660 sscanf(token, "up=%i", &params.user_priority) == 1 ||
661 sscanf(token, "nominal_msdu_size=%i",
662 &params.nominal_msdu_size) == 1 ||
663 sscanf(token, "mean_data_rate=%i",
664 &params.mean_data_rate) == 1 ||
665 sscanf(token, "min_phy_rate=%i",
666 &params.minimum_phy_rate) == 1 ||
667 sscanf(token, "sba=%i",
668 &params.surplus_bandwidth_allowance) == 1)
669 continue;
670
671 if (os_strcasecmp(token, "downlink") == 0) {
672 params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
673 } else if (os_strcasecmp(token, "uplink") == 0) {
674 params.direction = WMM_TSPEC_DIRECTION_UPLINK;
675 } else if (os_strcasecmp(token, "bidi") == 0) {
676 params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
677 } else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
678 params.fixed_nominal_msdu = 1;
679 } else {
680 wpa_printf(MSG_DEBUG,
681 "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
682 token);
683 return -1;
684 }
685
686 }
687
688 return wpas_wmm_ac_addts(wpa_s, &params);
689}
690
691
692static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
693{
694 u8 tsid = atoi(cmd);
695
696 return wpas_wmm_ac_delts(wpa_s, tsid);
697}
698
699
6fc6879b
JM
700#ifdef CONFIG_IEEE80211R
701static int wpa_supplicant_ctrl_iface_ft_ds(
702 struct wpa_supplicant *wpa_s, char *addr)
703{
704 u8 target_ap[ETH_ALEN];
76b7981d
JM
705 struct wpa_bss *bss;
706 const u8 *mdie;
6fc6879b
JM
707
708 if (hwaddr_aton(addr, target_ap)) {
709 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
a7b6c422 710 "address '%s'", addr);
6fc6879b
JM
711 return -1;
712 }
713
714 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
715
76b7981d
JM
716 bss = wpa_bss_get_bssid(wpa_s, target_ap);
717 if (bss)
718 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
719 else
720 mdie = NULL;
721
722 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
6fc6879b
JM
723}
724#endif /* CONFIG_IEEE80211R */
725
726
fcc60db4
JM
727#ifdef CONFIG_WPS
728static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
729 char *cmd)
730{
3ec97afe 731 u8 bssid[ETH_ALEN], *_bssid = bssid;
ceb34f25 732#ifdef CONFIG_P2P
634ce802 733 u8 p2p_dev_addr[ETH_ALEN];
ceb34f25 734#endif /* CONFIG_P2P */
634ce802
JM
735#ifdef CONFIG_AP
736 u8 *_p2p_dev_addr = NULL;
737#endif /* CONFIG_AP */
fcc60db4 738
d601247c 739 if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
3ec97afe 740 _bssid = NULL;
d601247c
JM
741#ifdef CONFIG_P2P
742 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
743 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
744 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
745 "P2P Device Address '%s'",
746 cmd + 13);
747 return -1;
748 }
749 _p2p_dev_addr = p2p_dev_addr;
750#endif /* CONFIG_P2P */
751 } else if (hwaddr_aton(cmd, bssid)) {
fcc60db4
JM
752 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
753 cmd);
754 return -1;
755 }
756
3ec97afe
JM
757#ifdef CONFIG_AP
758 if (wpa_s->ap_iface)
d601247c 759 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
3ec97afe
JM
760#endif /* CONFIG_AP */
761
9fa243b2 762 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
fcc60db4
JM
763}
764
765
766static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
767 char *cmd, char *buf,
768 size_t buflen)
769{
770 u8 bssid[ETH_ALEN], *_bssid = bssid;
771 char *pin;
772 int ret;
773
774 pin = os_strchr(cmd, ' ');
775 if (pin)
776 *pin++ = '\0';
777
778 if (os_strcmp(cmd, "any") == 0)
779 _bssid = NULL;
98aa7ca5
JM
780 else if (os_strcmp(cmd, "get") == 0) {
781 ret = wps_generate_pin();
782 goto done;
783 } else if (hwaddr_aton(cmd, bssid)) {
3c1e2765 784 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
fcc60db4
JM
785 cmd);
786 return -1;
787 }
788
3ec97afe 789#ifdef CONFIG_AP
c423708f
JM
790 if (wpa_s->ap_iface) {
791 int timeout = 0;
792 char *pos;
793
794 if (pin) {
795 pos = os_strchr(pin, ' ');
796 if (pos) {
797 *pos++ = '\0';
798 timeout = atoi(pos);
799 }
800 }
801
3ec97afe 802 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
c423708f
JM
803 buf, buflen, timeout);
804 }
3ec97afe
JM
805#endif /* CONFIG_AP */
806
fcc60db4 807 if (pin) {
3c5126a4
JM
808 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
809 DEV_PW_DEFAULT);
fcc60db4
JM
810 if (ret < 0)
811 return -1;
812 ret = os_snprintf(buf, buflen, "%s", pin);
d85e1fc8 813 if (os_snprintf_error(buflen, ret))
fcc60db4
JM
814 return -1;
815 return ret;
816 }
817
3c5126a4 818 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
fcc60db4
JM
819 if (ret < 0)
820 return -1;
821
98aa7ca5 822done:
fcc60db4
JM
823 /* Return the generated PIN */
824 ret = os_snprintf(buf, buflen, "%08d", ret);
d85e1fc8 825 if (os_snprintf_error(buflen, ret))
fcc60db4
JM
826 return -1;
827 return ret;
828}
829
830
3981cb3c
JM
831static int wpa_supplicant_ctrl_iface_wps_check_pin(
832 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
833{
834 char pin[9];
835 size_t len;
836 char *pos;
837 int ret;
838
839 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
840 (u8 *) cmd, os_strlen(cmd));
841 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
842 if (*pos < '0' || *pos > '9')
843 continue;
844 pin[len++] = *pos;
845 if (len == 9) {
846 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
847 return -1;
848 }
849 }
850 if (len != 4 && len != 8) {
851 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
852 return -1;
853 }
854 pin[len] = '\0';
855
856 if (len == 8) {
857 unsigned int pin_val;
858 pin_val = atoi(pin);
859 if (!wps_pin_valid(pin_val)) {
860 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
861 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
d85e1fc8 862 if (os_snprintf_error(buflen, ret))
3981cb3c
JM
863 return -1;
864 return ret;
865 }
866 }
867
868 ret = os_snprintf(buf, buflen, "%s", pin);
d85e1fc8 869 if (os_snprintf_error(buflen, ret))
3981cb3c
JM
870 return -1;
871
872 return ret;
873}
874
875
71892384 876#ifdef CONFIG_WPS_NFC
3f2c8ba6
JM
877
878static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
879 char *cmd)
880{
881 u8 bssid[ETH_ALEN], *_bssid = bssid;
882
883 if (cmd == NULL || cmd[0] == '\0')
884 _bssid = NULL;
885 else if (hwaddr_aton(cmd, bssid))
886 return -1;
887
23318bea 888 return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
91a65018 889 0, 0);
3f2c8ba6
JM
890}
891
892
bbf41865
JM
893static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
894 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
895{
896 int ndef;
897 struct wpabuf *buf;
898 int res;
88c8bf31 899 char *pos;
bbf41865 900
88c8bf31
JM
901 pos = os_strchr(cmd, ' ');
902 if (pos)
903 *pos++ = '\0';
bbf41865
JM
904 if (os_strcmp(cmd, "WPS") == 0)
905 ndef = 0;
906 else if (os_strcmp(cmd, "NDEF") == 0)
907 ndef = 1;
908 else
909 return -1;
910
88c8bf31 911 buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
bbf41865
JM
912 if (buf == NULL)
913 return -1;
914
915 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
916 wpabuf_len(buf));
917 reply[res++] = '\n';
918 reply[res] = '\0';
919
920 wpabuf_free(buf);
921
922 return res;
923}
924
925
3f2c8ba6
JM
926static int wpa_supplicant_ctrl_iface_wps_nfc_token(
927 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
928{
929 int ndef;
930 struct wpabuf *buf;
931 int res;
932
933 if (os_strcmp(cmd, "WPS") == 0)
934 ndef = 0;
935 else if (os_strcmp(cmd, "NDEF") == 0)
936 ndef = 1;
937 else
938 return -1;
939
940 buf = wpas_wps_nfc_token(wpa_s, ndef);
941 if (buf == NULL)
942 return -1;
943
944 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
945 wpabuf_len(buf));
946 reply[res++] = '\n';
947 reply[res] = '\0';
948
949 wpabuf_free(buf);
950
951 return res;
952}
d7645d23
JM
953
954
955static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
956 struct wpa_supplicant *wpa_s, char *pos)
957{
958 size_t len;
959 struct wpabuf *buf;
960 int ret;
b56f6c88
JM
961 char *freq;
962 int forced_freq = 0;
963
964 freq = strstr(pos, " freq=");
965 if (freq) {
966 *freq = '\0';
967 freq += 6;
968 forced_freq = atoi(freq);
969 }
d7645d23
JM
970
971 len = os_strlen(pos);
972 if (len & 0x01)
973 return -1;
974 len /= 2;
975
976 buf = wpabuf_alloc(len);
977 if (buf == NULL)
978 return -1;
979 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
980 wpabuf_free(buf);
981 return -1;
982 }
983
b56f6c88 984 ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
d7645d23
JM
985 wpabuf_free(buf);
986
987 return ret;
988}
71892384 989
e65552dd
JM
990
991static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
bbaaaee1 992 char *reply, size_t max_len,
41f9ffb6 993 int ndef)
e65552dd
JM
994{
995 struct wpabuf *buf;
996 int res;
997
41f9ffb6 998 buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
e65552dd
JM
999 if (buf == NULL)
1000 return -1;
1001
1002 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1003 wpabuf_len(buf));
1004 reply[res++] = '\n';
1005 reply[res] = '\0';
1006
1007 wpabuf_free(buf);
1008
1009 return res;
1010}
1011
1012
88853aed 1013#ifdef CONFIG_P2P
93588780
JM
1014static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
1015 char *reply, size_t max_len,
1016 int ndef)
1017{
1018 struct wpabuf *buf;
1019 int res;
1020
1021 buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
1022 if (buf == NULL) {
1023 wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
1024 return -1;
1025 }
1026
1027 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1028 wpabuf_len(buf));
1029 reply[res++] = '\n';
1030 reply[res] = '\0';
1031
1032 wpabuf_free(buf);
1033
1034 return res;
1035}
88853aed 1036#endif /* CONFIG_P2P */
93588780
JM
1037
1038
e65552dd
JM
1039static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
1040 char *cmd, char *reply,
1041 size_t max_len)
1042{
1043 char *pos;
41f9ffb6 1044 int ndef;
e65552dd
JM
1045
1046 pos = os_strchr(cmd, ' ');
1047 if (pos == NULL)
1048 return -1;
1049 *pos++ = '\0';
1050
41f9ffb6
JM
1051 if (os_strcmp(cmd, "WPS") == 0)
1052 ndef = 0;
1053 else if (os_strcmp(cmd, "NDEF") == 0)
1054 ndef = 1;
1055 else
e65552dd
JM
1056 return -1;
1057
bbaaaee1 1058 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
41f9ffb6
JM
1059 if (!ndef)
1060 return -1;
bbaaaee1 1061 return wpas_ctrl_nfc_get_handover_req_wps(
41f9ffb6 1062 wpa_s, reply, max_len, ndef);
e65552dd
JM
1063 }
1064
88853aed 1065#ifdef CONFIG_P2P
93588780
JM
1066 if (os_strcmp(pos, "P2P-CR") == 0) {
1067 return wpas_ctrl_nfc_get_handover_req_p2p(
1068 wpa_s, reply, max_len, ndef);
1069 }
88853aed 1070#endif /* CONFIG_P2P */
93588780 1071
e65552dd
JM
1072 return -1;
1073}
1074
1075
1076static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
5ab9a6a5 1077 char *reply, size_t max_len,
f3f2ba2e 1078 int ndef, int cr, char *uuid)
e65552dd
JM
1079{
1080 struct wpabuf *buf;
1081 int res;
1082
f3f2ba2e 1083 buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
e65552dd
JM
1084 if (buf == NULL)
1085 return -1;
1086
1087 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1088 wpabuf_len(buf));
1089 reply[res++] = '\n';
1090 reply[res] = '\0';
1091
1092 wpabuf_free(buf);
1093
1094 return res;
1095}
1096
1097
88853aed 1098#ifdef CONFIG_P2P
93588780
JM
1099static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1100 char *reply, size_t max_len,
1101 int ndef, int tag)
1102{
1103 struct wpabuf *buf;
1104 int res;
1105
1106 buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1107 if (buf == NULL)
1108 return -1;
1109
1110 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1111 wpabuf_len(buf));
1112 reply[res++] = '\n';
1113 reply[res] = '\0';
1114
1115 wpabuf_free(buf);
1116
1117 return res;
1118}
88853aed 1119#endif /* CONFIG_P2P */
93588780
JM
1120
1121
e65552dd
JM
1122static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1123 char *cmd, char *reply,
1124 size_t max_len)
1125{
f3f2ba2e 1126 char *pos, *pos2;
5ab9a6a5 1127 int ndef;
e65552dd
JM
1128
1129 pos = os_strchr(cmd, ' ');
1130 if (pos == NULL)
1131 return -1;
1132 *pos++ = '\0';
1133
5ab9a6a5
JM
1134 if (os_strcmp(cmd, "WPS") == 0)
1135 ndef = 0;
1136 else if (os_strcmp(cmd, "NDEF") == 0)
1137 ndef = 1;
1138 else
e65552dd
JM
1139 return -1;
1140
f3f2ba2e
JM
1141 pos2 = os_strchr(pos, ' ');
1142 if (pos2)
1143 *pos2++ = '\0';
5ab9a6a5 1144 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
93588780
JM
1145 if (!ndef)
1146 return -1;
5ab9a6a5
JM
1147 return wpas_ctrl_nfc_get_handover_sel_wps(
1148 wpa_s, reply, max_len, ndef,
f3f2ba2e 1149 os_strcmp(pos, "WPS-CR") == 0, pos2);
e65552dd
JM
1150 }
1151
88853aed 1152#ifdef CONFIG_P2P
93588780
JM
1153 if (os_strcmp(pos, "P2P-CR") == 0) {
1154 return wpas_ctrl_nfc_get_handover_sel_p2p(
1155 wpa_s, reply, max_len, ndef, 0);
1156 }
1157
1158 if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1159 return wpas_ctrl_nfc_get_handover_sel_p2p(
1160 wpa_s, reply, max_len, ndef, 1);
1161 }
88853aed 1162#endif /* CONFIG_P2P */
93588780 1163
e65552dd
JM
1164 return -1;
1165}
1166
1167
e4758827
JM
1168static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1169 char *cmd)
1170{
1171 size_t len;
1172 struct wpabuf *req, *sel;
1173 int ret;
1174 char *pos, *role, *type, *pos2;
88853aed 1175#ifdef CONFIG_P2P
b56f6c88
JM
1176 char *freq;
1177 int forced_freq = 0;
1178
1179 freq = strstr(cmd, " freq=");
1180 if (freq) {
1181 *freq = '\0';
1182 freq += 6;
1183 forced_freq = atoi(freq);
1184 }
88853aed 1185#endif /* CONFIG_P2P */
e4758827
JM
1186
1187 role = cmd;
1188 pos = os_strchr(role, ' ');
73127764
JM
1189 if (pos == NULL) {
1190 wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
e4758827 1191 return -1;
73127764 1192 }
e4758827
JM
1193 *pos++ = '\0';
1194
1195 type = pos;
1196 pos = os_strchr(type, ' ');
73127764
JM
1197 if (pos == NULL) {
1198 wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
e4758827 1199 return -1;
73127764 1200 }
e4758827
JM
1201 *pos++ = '\0';
1202
1203 pos2 = os_strchr(pos, ' ');
73127764
JM
1204 if (pos2 == NULL) {
1205 wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
e4758827 1206 return -1;
73127764 1207 }
e4758827
JM
1208 *pos2++ = '\0';
1209
1210 len = os_strlen(pos);
73127764
JM
1211 if (len & 0x01) {
1212 wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
e4758827 1213 return -1;
73127764 1214 }
e4758827
JM
1215 len /= 2;
1216
1217 req = wpabuf_alloc(len);
73127764
JM
1218 if (req == NULL) {
1219 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
e4758827 1220 return -1;
73127764 1221 }
e4758827 1222 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
73127764 1223 wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
e4758827
JM
1224 wpabuf_free(req);
1225 return -1;
1226 }
1227
1228 len = os_strlen(pos2);
1229 if (len & 0x01) {
73127764 1230 wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
e4758827
JM
1231 wpabuf_free(req);
1232 return -1;
1233 }
1234 len /= 2;
1235
1236 sel = wpabuf_alloc(len);
1237 if (sel == NULL) {
73127764 1238 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
e4758827
JM
1239 wpabuf_free(req);
1240 return -1;
1241 }
1242 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
73127764 1243 wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
e4758827
JM
1244 wpabuf_free(req);
1245 wpabuf_free(sel);
1246 return -1;
1247 }
1248
73127764
JM
1249 wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1250 role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1251
e4758827
JM
1252 if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1253 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
88853aed 1254#ifdef CONFIG_AP
d9507936
JM
1255 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1256 {
1257 ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
50d1f890
JM
1258 if (ret < 0)
1259 ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
88853aed
JM
1260#endif /* CONFIG_AP */
1261#ifdef CONFIG_P2P
db6ae69e
JM
1262 } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1263 {
b56f6c88 1264 ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
db6ae69e
JM
1265 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1266 {
b56f6c88
JM
1267 ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1268 forced_freq);
88853aed 1269#endif /* CONFIG_P2P */
e4758827
JM
1270 } else {
1271 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1272 "reported: role=%s type=%s", role, type);
1273 ret = -1;
1274 }
1275 wpabuf_free(req);
1276 wpabuf_free(sel);
1277
73127764
JM
1278 if (ret)
1279 wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1280
e4758827
JM
1281 return ret;
1282}
1283
71892384 1284#endif /* CONFIG_WPS_NFC */
46bdb83a
MH
1285
1286
fcc60db4
JM
1287static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1288 char *cmd)
1289{
129eb428 1290 u8 bssid[ETH_ALEN];
fcc60db4 1291 char *pin;
52eb293d
JM
1292 char *new_ssid;
1293 char *new_auth;
1294 char *new_encr;
1295 char *new_key;
1296 struct wps_new_ap_settings ap;
fcc60db4
JM
1297
1298 pin = os_strchr(cmd, ' ');
1299 if (pin == NULL)
1300 return -1;
1301 *pin++ = '\0';
1302
129eb428 1303 if (hwaddr_aton(cmd, bssid)) {
fcc60db4
JM
1304 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1305 cmd);
1306 return -1;
1307 }
1308
52eb293d
JM
1309 new_ssid = os_strchr(pin, ' ');
1310 if (new_ssid == NULL)
129eb428 1311 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
52eb293d
JM
1312 *new_ssid++ = '\0';
1313
1314 new_auth = os_strchr(new_ssid, ' ');
1315 if (new_auth == NULL)
1316 return -1;
1317 *new_auth++ = '\0';
1318
1319 new_encr = os_strchr(new_auth, ' ');
1320 if (new_encr == NULL)
1321 return -1;
1322 *new_encr++ = '\0';
1323
1324 new_key = os_strchr(new_encr, ' ');
1325 if (new_key == NULL)
1326 return -1;
1327 *new_key++ = '\0';
1328
1329 os_memset(&ap, 0, sizeof(ap));
1330 ap.ssid_hex = new_ssid;
1331 ap.auth = new_auth;
1332 ap.encr = new_encr;
1333 ap.key_hex = new_key;
129eb428 1334 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
fcc60db4 1335}
72df2f5f
JM
1336
1337
70d84f11
JM
1338#ifdef CONFIG_AP
1339static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1340 char *cmd, char *buf,
1341 size_t buflen)
1342{
1343 int timeout = 300;
1344 char *pos;
1345 const char *pin_txt;
1346
1347 if (!wpa_s->ap_iface)
1348 return -1;
1349
1350 pos = os_strchr(cmd, ' ');
1351 if (pos)
1352 *pos++ = '\0';
1353
1354 if (os_strcmp(cmd, "disable") == 0) {
1355 wpas_wps_ap_pin_disable(wpa_s);
1356 return os_snprintf(buf, buflen, "OK\n");
1357 }
1358
1359 if (os_strcmp(cmd, "random") == 0) {
1360 if (pos)
1361 timeout = atoi(pos);
1362 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1363 if (pin_txt == NULL)
1364 return -1;
1365 return os_snprintf(buf, buflen, "%s", pin_txt);
1366 }
1367
1368 if (os_strcmp(cmd, "get") == 0) {
1369 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1370 if (pin_txt == NULL)
1371 return -1;
1372 return os_snprintf(buf, buflen, "%s", pin_txt);
1373 }
1374
1375 if (os_strcmp(cmd, "set") == 0) {
1376 char *pin;
1377 if (pos == NULL)
1378 return -1;
1379 pin = pos;
1380 pos = os_strchr(pos, ' ');
1381 if (pos) {
1382 *pos++ = '\0';
1383 timeout = atoi(pos);
1384 }
1385 if (os_strlen(pin) > buflen)
1386 return -1;
1387 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1388 return -1;
1389 return os_snprintf(buf, buflen, "%s", pin);
1390 }
1391
1392 return -1;
1393}
1394#endif /* CONFIG_AP */
1395
1396
72df2f5f
JM
1397#ifdef CONFIG_WPS_ER
1398static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1399 char *cmd)
1400{
31fcea93
JM
1401 char *uuid = cmd, *pin, *pos;
1402 u8 addr_buf[ETH_ALEN], *addr = NULL;
72df2f5f
JM
1403 pin = os_strchr(uuid, ' ');
1404 if (pin == NULL)
1405 return -1;
1406 *pin++ = '\0';
31fcea93
JM
1407 pos = os_strchr(pin, ' ');
1408 if (pos) {
1409 *pos++ = '\0';
1410 if (hwaddr_aton(pos, addr_buf) == 0)
1411 addr = addr_buf;
1412 }
1413 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
72df2f5f 1414}
e64dcfd5
JM
1415
1416
1417static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1418 char *cmd)
1419{
1420 char *uuid = cmd, *pin;
1421 pin = os_strchr(uuid, ' ');
1422 if (pin == NULL)
1423 return -1;
1424 *pin++ = '\0';
1425 return wpas_wps_er_learn(wpa_s, uuid, pin);
1426}
7d6640a6
JM
1427
1428
ef10f473
JM
1429static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1430 struct wpa_supplicant *wpa_s, char *cmd)
1431{
1432 char *uuid = cmd, *id;
1433 id = os_strchr(uuid, ' ');
1434 if (id == NULL)
1435 return -1;
1436 *id++ = '\0';
1437 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1438}
1439
1440
7d6640a6
JM
1441static int wpa_supplicant_ctrl_iface_wps_er_config(
1442 struct wpa_supplicant *wpa_s, char *cmd)
1443{
1444 char *pin;
1445 char *new_ssid;
1446 char *new_auth;
1447 char *new_encr;
1448 char *new_key;
1449 struct wps_new_ap_settings ap;
1450
1451 pin = os_strchr(cmd, ' ');
1452 if (pin == NULL)
1453 return -1;
1454 *pin++ = '\0';
1455
1456 new_ssid = os_strchr(pin, ' ');
1457 if (new_ssid == NULL)
1458 return -1;
1459 *new_ssid++ = '\0';
1460
1461 new_auth = os_strchr(new_ssid, ' ');
1462 if (new_auth == NULL)
1463 return -1;
1464 *new_auth++ = '\0';
1465
1466 new_encr = os_strchr(new_auth, ' ');
1467 if (new_encr == NULL)
1468 return -1;
1469 *new_encr++ = '\0';
1470
1471 new_key = os_strchr(new_encr, ' ');
1472 if (new_key == NULL)
1473 return -1;
1474 *new_key++ = '\0';
1475
1476 os_memset(&ap, 0, sizeof(ap));
1477 ap.ssid_hex = new_ssid;
1478 ap.auth = new_auth;
1479 ap.encr = new_encr;
1480 ap.key_hex = new_key;
1481 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1482}
1cea09a9
JM
1483
1484
1485#ifdef CONFIG_WPS_NFC
1486static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1487 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1488{
1489 int ndef;
1490 struct wpabuf *buf;
1491 int res;
1492 char *uuid;
1493
1494 uuid = os_strchr(cmd, ' ');
1495 if (uuid == NULL)
1496 return -1;
1497 *uuid++ = '\0';
1498
1499 if (os_strcmp(cmd, "WPS") == 0)
1500 ndef = 0;
1501 else if (os_strcmp(cmd, "NDEF") == 0)
1502 ndef = 1;
1503 else
1504 return -1;
1505
1506 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1507 if (buf == NULL)
1508 return -1;
1509
1510 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1511 wpabuf_len(buf));
1512 reply[res++] = '\n';
1513 reply[res] = '\0';
1514
1515 wpabuf_free(buf);
1516
1517 return res;
1518}
1519#endif /* CONFIG_WPS_NFC */
72df2f5f
JM
1520#endif /* CONFIG_WPS_ER */
1521
fcc60db4
JM
1522#endif /* CONFIG_WPS */
1523
1524
11ef8d35
JM
1525#ifdef CONFIG_IBSS_RSN
1526static int wpa_supplicant_ctrl_iface_ibss_rsn(
1527 struct wpa_supplicant *wpa_s, char *addr)
1528{
1529 u8 peer[ETH_ALEN];
1530
1531 if (hwaddr_aton(addr, peer)) {
1532 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
a7b6c422 1533 "address '%s'", addr);
11ef8d35
JM
1534 return -1;
1535 }
1536
1537 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1538 MAC2STR(peer));
1539
1540 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1541}
1542#endif /* CONFIG_IBSS_RSN */
1543
1544
7de5688d
DW
1545static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1546 char *rsp)
1547{
1548#ifdef IEEE8021X_EAPOL
1549 char *pos, *id_pos;
1550 int id;
1551 struct wpa_ssid *ssid;
1552
1553 pos = os_strchr(rsp, '-');
1554 if (pos == NULL)
1555 return -1;
1556 *pos++ = '\0';
1557 id_pos = pos;
1558 pos = os_strchr(pos, ':');
1559 if (pos == NULL)
1560 return -1;
1561 *pos++ = '\0';
1562 id = atoi(id_pos);
1563 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1564 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1565 (u8 *) pos, os_strlen(pos));
1566
1567 ssid = wpa_config_get_network(wpa_s->conf, id);
1568 if (ssid == NULL) {
1569 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1570 "to update", id);
1571 return -1;
1572 }
1573
1574 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1575 pos);
6fc6879b
JM
1576#else /* IEEE8021X_EAPOL */
1577 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1578 return -1;
1579#endif /* IEEE8021X_EAPOL */
1580}
1581
1582
1583static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1584 const char *params,
1585 char *buf, size_t buflen)
1586{
1587 char *pos, *end, tmp[30];
0bc13468 1588 int res, verbose, wps, ret;
f9cd147d
JM
1589#ifdef CONFIG_HS20
1590 const u8 *hs20;
1591#endif /* CONFIG_HS20 */
6fc6879b 1592
a771c07d
JM
1593 if (os_strcmp(params, "-DRIVER") == 0)
1594 return wpa_drv_status(wpa_s, buf, buflen);
6fc6879b 1595 verbose = os_strcmp(params, "-VERBOSE") == 0;
0bc13468 1596 wps = os_strcmp(params, "-WPS") == 0;
6fc6879b
JM
1597 pos = buf;
1598 end = buf + buflen;
1599 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1600 struct wpa_ssid *ssid = wpa_s->current_ssid;
1601 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1602 MAC2STR(wpa_s->bssid));
d85e1fc8 1603 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
1604 return pos - buf;
1605 pos += ret;
b6ebdfbe
BP
1606 ret = os_snprintf(pos, end - pos, "freq=%u\n",
1607 wpa_s->assoc_freq);
d85e1fc8 1608 if (os_snprintf_error(end - pos, ret))
b6ebdfbe
BP
1609 return pos - buf;
1610 pos += ret;
6fc6879b
JM
1611 if (ssid) {
1612 u8 *_ssid = ssid->ssid;
1613 size_t ssid_len = ssid->ssid_len;
1614 u8 ssid_buf[MAX_SSID_LEN];
1615 if (ssid_len == 0) {
1616 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1617 if (_res < 0)
1618 ssid_len = 0;
1619 else
1620 ssid_len = _res;
1621 _ssid = ssid_buf;
1622 }
1623 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1624 wpa_ssid_txt(_ssid, ssid_len),
1625 ssid->id);
d85e1fc8 1626 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
1627 return pos - buf;
1628 pos += ret;
1629
0bc13468
JM
1630 if (wps && ssid->passphrase &&
1631 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1632 (ssid->mode == WPAS_MODE_AP ||
1633 ssid->mode == WPAS_MODE_P2P_GO)) {
1634 ret = os_snprintf(pos, end - pos,
1635 "passphrase=%s\n",
1636 ssid->passphrase);
d85e1fc8 1637 if (os_snprintf_error(end - pos, ret))
0bc13468
JM
1638 return pos - buf;
1639 pos += ret;
1640 }
6fc6879b
JM
1641 if (ssid->id_str) {
1642 ret = os_snprintf(pos, end - pos,
1643 "id_str=%s\n",
1644 ssid->id_str);
d85e1fc8 1645 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
1646 return pos - buf;
1647 pos += ret;
1648 }
0e15e529
JM
1649
1650 switch (ssid->mode) {
d7dcba70 1651 case WPAS_MODE_INFRA:
0e15e529
JM
1652 ret = os_snprintf(pos, end - pos,
1653 "mode=station\n");
1654 break;
d7dcba70 1655 case WPAS_MODE_IBSS:
0e15e529
JM
1656 ret = os_snprintf(pos, end - pos,
1657 "mode=IBSS\n");
1658 break;
d7dcba70 1659 case WPAS_MODE_AP:
0e15e529
JM
1660 ret = os_snprintf(pos, end - pos,
1661 "mode=AP\n");
1662 break;
2c5d725c
JM
1663 case WPAS_MODE_P2P_GO:
1664 ret = os_snprintf(pos, end - pos,
1665 "mode=P2P GO\n");
1666 break;
1667 case WPAS_MODE_P2P_GROUP_FORMATION:
1668 ret = os_snprintf(pos, end - pos,
1669 "mode=P2P GO - group "
1670 "formation\n");
1671 break;
0e15e529
JM
1672 default:
1673 ret = 0;
1674 break;
1675 }
1f102d3b 1676 if (os_snprintf_error(end - pos, ret))
0e15e529
JM
1677 return pos - buf;
1678 pos += ret;
6fc6879b
JM
1679 }
1680
43fb5297
JM
1681#ifdef CONFIG_AP
1682 if (wpa_s->ap_iface) {
1683 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1684 end - pos,
1685 verbose);
1686 } else
1687#endif /* CONFIG_AP */
6fc6879b
JM
1688 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1689 }
4954c859
JM
1690#ifdef CONFIG_SAE
1691 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
e1ae5d74
JM
1692#ifdef CONFIG_AP
1693 !wpa_s->ap_iface &&
1694#endif /* CONFIG_AP */
1695 wpa_s->sme.sae.state == SAE_ACCEPTED) {
4954c859
JM
1696 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1697 wpa_s->sme.sae.group);
d85e1fc8 1698 if (os_snprintf_error(end - pos, ret))
4954c859
JM
1699 return pos - buf;
1700 pos += ret;
1701 }
1702#endif /* CONFIG_SAE */
6fc6879b
JM
1703 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1704 wpa_supplicant_state_txt(wpa_s->wpa_state));
d85e1fc8 1705 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
1706 return pos - buf;
1707 pos += ret;
1708
1709 if (wpa_s->l2 &&
1710 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1711 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
d85e1fc8 1712 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
1713 return pos - buf;
1714 pos += ret;
1715 }
1716
d23bd894
JM
1717#ifdef CONFIG_P2P
1718 if (wpa_s->global->p2p) {
1719 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1720 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
d85e1fc8 1721 if (os_snprintf_error(end - pos, ret))
d23bd894
JM
1722 return pos - buf;
1723 pos += ret;
1724 }
b21e2c84 1725#endif /* CONFIG_P2P */
6d4747a9
JM
1726
1727 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1728 MAC2STR(wpa_s->own_addr));
d85e1fc8 1729 if (os_snprintf_error(end - pos, ret))
6d4747a9
JM
1730 return pos - buf;
1731 pos += ret;
d23bd894 1732
64855b96
JM
1733#ifdef CONFIG_HS20
1734 if (wpa_s->current_bss &&
f9cd147d
JM
1735 (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1736 HS20_IE_VENDOR_TYPE)) &&
4ed34f5a
JM
1737 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1738 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
f9cd147d
JM
1739 int release = 1;
1740 if (hs20[1] >= 5) {
1741 u8 rel_num = (hs20[6] & 0xf0) >> 4;
1742 release = rel_num + 1;
1743 }
1744 ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
d85e1fc8 1745 if (os_snprintf_error(end - pos, ret))
64855b96
JM
1746 return pos - buf;
1747 pos += ret;
1748 }
e99b4f3a
JM
1749
1750 if (wpa_s->current_ssid) {
1751 struct wpa_cred *cred;
1752 char *type;
1753
1754 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
463c8ffb
JM
1755 size_t i;
1756
e99b4f3a
JM
1757 if (wpa_s->current_ssid->parent_cred != cred)
1758 continue;
e99b4f3a 1759
aa26ba68 1760 if (cred->provisioning_sp) {
463c8ffb 1761 ret = os_snprintf(pos, end - pos,
aa26ba68
JM
1762 "provisioning_sp=%s\n",
1763 cred->provisioning_sp);
d85e1fc8 1764 if (os_snprintf_error(end - pos, ret))
463c8ffb
JM
1765 return pos - buf;
1766 pos += ret;
1767 }
e99b4f3a 1768
aa26ba68
JM
1769 if (!cred->domain)
1770 goto no_domain;
1771
1772 i = 0;
1773 if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
1774 struct wpabuf *names =
1775 wpa_s->current_bss->anqp->domain_name;
1776 for (i = 0; names && i < cred->num_domain; i++)
1777 {
1778 if (domain_name_list_contains(
1779 names, cred->domain[i], 1))
1780 break;
1781 }
1782 if (i == cred->num_domain)
1783 i = 0; /* show first entry by default */
1784 }
1785 ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
1786 cred->domain[i]);
d85e1fc8 1787 if (os_snprintf_error(end - pos, ret))
aa26ba68
JM
1788 return pos - buf;
1789 pos += ret;
1790
1791 no_domain:
e99b4f3a
JM
1792 if (wpa_s->current_bss == NULL ||
1793 wpa_s->current_bss->anqp == NULL)
1794 res = -1;
1795 else
1796 res = interworking_home_sp_cred(
1797 wpa_s, cred,
1798 wpa_s->current_bss->anqp->domain_name);
1799 if (res > 0)
1800 type = "home";
1801 else if (res == 0)
1802 type = "roaming";
1803 else
1804 type = "unknown";
1805
1806 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
d85e1fc8 1807 if (os_snprintf_error(end - pos, ret))
e99b4f3a
JM
1808 return pos - buf;
1809 pos += ret;
1810
1811 break;
1812 }
1813 }
64855b96
JM
1814#endif /* CONFIG_HS20 */
1815
56586197
JM
1816 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1817 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
6fc6879b
JM
1818 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
1819 verbose);
1820 if (res >= 0)
1821 pos += res;
1822 }
1823
1824 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
1825 if (res >= 0)
1826 pos += res;
1827
8aaafcee
JM
1828#ifdef CONFIG_WPS
1829 {
1830 char uuid_str[100];
1831 uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
1832 ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
d85e1fc8 1833 if (os_snprintf_error(end - pos, ret))
8aaafcee
JM
1834 return pos - buf;
1835 pos += ret;
1836 }
1837#endif /* CONFIG_WPS */
1838
f6c2b8c3 1839#ifdef ANDROID
a6ab82d7 1840 /*
1841 * Allow using the STATUS command with default behavior, say for debug,
1842 * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
1843 * events with STATUS-NO_EVENTS.
1844 */
1845 if (os_strcmp(params, "-NO_EVENTS")) {
1846 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
1847 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
1848 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
1849 wpa_s->wpa_state,
1850 MAC2STR(wpa_s->bssid),
1851 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
1852 wpa_ssid_txt(wpa_s->current_ssid->ssid,
1853 wpa_s->current_ssid->ssid_len) : "");
1854 if (wpa_s->wpa_state == WPA_COMPLETED) {
1855 struct wpa_ssid *ssid = wpa_s->current_ssid;
1856 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
1857 "- connection to " MACSTR
1858 " completed %s [id=%d id_str=%s]",
1859 MAC2STR(wpa_s->bssid), "(auth)",
1860 ssid ? ssid->id : -1,
1861 ssid && ssid->id_str ? ssid->id_str : "");
1862 }
f6c2b8c3
DS
1863 }
1864#endif /* ANDROID */
1865
6fc6879b
JM
1866 return pos - buf;
1867}
1868
1869
1870static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
1871 char *cmd)
1872{
1873 char *pos;
1874 int id;
1875 struct wpa_ssid *ssid;
1876 u8 bssid[ETH_ALEN];
1877
1878 /* cmd: "<network id> <BSSID>" */
1879 pos = os_strchr(cmd, ' ');
1880 if (pos == NULL)
1881 return -1;
1882 *pos++ = '\0';
1883 id = atoi(cmd);
1884 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
1885 if (hwaddr_aton(pos, bssid)) {
1886 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
1887 return -1;
1888 }
1889
1890 ssid = wpa_config_get_network(wpa_s->conf, id);
1891 if (ssid == NULL) {
1892 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1893 "to update", id);
1894 return -1;
1895 }
1896
1897 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
a8e16edc 1898 ssid->bssid_set = !is_zero_ether_addr(bssid);
6fc6879b
JM
1899
1900 return 0;
1901}
1902
1903
9aa10e2b
DS
1904static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
1905 char *cmd, char *buf,
1906 size_t buflen)
1907{
1908 u8 bssid[ETH_ALEN];
1909 struct wpa_blacklist *e;
1910 char *pos, *end;
1911 int ret;
1912
1913 /* cmd: "BLACKLIST [<BSSID>]" */
1914 if (*cmd == '\0') {
1915 pos = buf;
1916 end = buf + buflen;
1917 e = wpa_s->blacklist;
1918 while (e) {
1919 ret = os_snprintf(pos, end - pos, MACSTR "\n",
1920 MAC2STR(e->bssid));
d85e1fc8 1921 if (os_snprintf_error(end - pos, ret))
9aa10e2b
DS
1922 return pos - buf;
1923 pos += ret;
1924 e = e->next;
1925 }
1926 return pos - buf;
1927 }
1928
1929 cmd++;
1930 if (os_strncmp(cmd, "clear", 5) == 0) {
1931 wpa_blacklist_clear(wpa_s);
1932 os_memcpy(buf, "OK\n", 3);
1933 return 3;
1934 }
1935
1936 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
1937 if (hwaddr_aton(cmd, bssid)) {
1938 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
1939 return -1;
1940 }
1941
1942 /*
1943 * Add the BSSID twice, so its count will be 2, causing it to be
1944 * skipped when processing scan results.
1945 */
1946 ret = wpa_blacklist_add(wpa_s, bssid);
bd8838a3 1947 if (ret < 0)
9aa10e2b
DS
1948 return -1;
1949 ret = wpa_blacklist_add(wpa_s, bssid);
bd8838a3 1950 if (ret < 0)
9aa10e2b
DS
1951 return -1;
1952 os_memcpy(buf, "OK\n", 3);
1953 return 3;
1954}
1955
1956
0597a5b5
DS
1957static const char * debug_level_str(int level)
1958{
1959 switch (level) {
1960 case MSG_EXCESSIVE:
1961 return "EXCESSIVE";
1962 case MSG_MSGDUMP:
1963 return "MSGDUMP";
1964 case MSG_DEBUG:
1965 return "DEBUG";
1966 case MSG_INFO:
1967 return "INFO";
1968 case MSG_WARNING:
1969 return "WARNING";
1970 case MSG_ERROR:
1971 return "ERROR";
1972 default:
1973 return "?";
1974 }
1975}
1976
1977
1978static int str_to_debug_level(const char *s)
1979{
1980 if (os_strcasecmp(s, "EXCESSIVE") == 0)
1981 return MSG_EXCESSIVE;
1982 if (os_strcasecmp(s, "MSGDUMP") == 0)
1983 return MSG_MSGDUMP;
1984 if (os_strcasecmp(s, "DEBUG") == 0)
1985 return MSG_DEBUG;
1986 if (os_strcasecmp(s, "INFO") == 0)
1987 return MSG_INFO;
1988 if (os_strcasecmp(s, "WARNING") == 0)
1989 return MSG_WARNING;
1990 if (os_strcasecmp(s, "ERROR") == 0)
1991 return MSG_ERROR;
1992 return -1;
1993}
1994
1995
1996static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
1997 char *cmd, char *buf,
1998 size_t buflen)
1999{
2000 char *pos, *end, *stamp;
2001 int ret;
2002
0597a5b5
DS
2003 /* cmd: "LOG_LEVEL [<level>]" */
2004 if (*cmd == '\0') {
2005 pos = buf;
2006 end = buf + buflen;
2007 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2008 "Timestamp: %d\n",
2009 debug_level_str(wpa_debug_level),
2010 wpa_debug_timestamp);
d85e1fc8 2011 if (os_snprintf_error(end - pos, ret))
0597a5b5
DS
2012 ret = 0;
2013
2014 return ret;
2015 }
2016
2017 while (*cmd == ' ')
2018 cmd++;
2019
2020 stamp = os_strchr(cmd, ' ');
2021 if (stamp) {
2022 *stamp++ = '\0';
2023 while (*stamp == ' ') {
2024 stamp++;
2025 }
2026 }
2027
2028 if (cmd && os_strlen(cmd)) {
2029 int level = str_to_debug_level(cmd);
2030 if (level < 0)
2031 return -1;
2032 wpa_debug_level = level;
2033 }
2034
2035 if (stamp && os_strlen(stamp))
2036 wpa_debug_timestamp = atoi(stamp);
2037
2038 os_memcpy(buf, "OK\n", 3);
2039 return 3;
2040}
2041
2042
6fc6879b 2043static int wpa_supplicant_ctrl_iface_list_networks(
90903a77 2044 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
6fc6879b 2045{
f34891a3 2046 char *pos, *end, *prev;
6fc6879b
JM
2047 struct wpa_ssid *ssid;
2048 int ret;
2049
2050 pos = buf;
2051 end = buf + buflen;
2052 ret = os_snprintf(pos, end - pos,
2053 "network id / ssid / bssid / flags\n");
d85e1fc8 2054 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2055 return pos - buf;
2056 pos += ret;
2057
2058 ssid = wpa_s->conf->ssid;
90903a77
VD
2059
2060 /* skip over ssids until we find next one */
2061 if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
2062 int last_id = atoi(cmd + 8);
2063 if (last_id != -1) {
2064 while (ssid != NULL && ssid->id <= last_id) {
2065 ssid = ssid->next;
2066 }
2067 }
2068 }
2069
6fc6879b 2070 while (ssid) {
f34891a3 2071 prev = pos;
6fc6879b
JM
2072 ret = os_snprintf(pos, end - pos, "%d\t%s",
2073 ssid->id,
2074 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
d85e1fc8 2075 if (os_snprintf_error(end - pos, ret))
f34891a3 2076 return prev - buf;
6fc6879b
JM
2077 pos += ret;
2078 if (ssid->bssid_set) {
2079 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2080 MAC2STR(ssid->bssid));
2081 } else {
2082 ret = os_snprintf(pos, end - pos, "\tany");
2083 }
d85e1fc8 2084 if (os_snprintf_error(end - pos, ret))
f34891a3 2085 return prev - buf;
6fc6879b 2086 pos += ret;
00e5e3d5 2087 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
6fc6879b
JM
2088 ssid == wpa_s->current_ssid ?
2089 "[CURRENT]" : "",
4dac0245 2090 ssid->disabled ? "[DISABLED]" : "",
00e5e3d5
JM
2091 ssid->disabled_until.sec ?
2092 "[TEMP-DISABLED]" : "",
4dac0245
JM
2093 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2094 "");
d85e1fc8 2095 if (os_snprintf_error(end - pos, ret))
f34891a3 2096 return prev - buf;
6fc6879b
JM
2097 pos += ret;
2098 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 2099 if (os_snprintf_error(end - pos, ret))
f34891a3 2100 return prev - buf;
6fc6879b
JM
2101 pos += ret;
2102
2103 ssid = ssid->next;
2104 }
2105
2106 return pos - buf;
2107}
2108
2109
2110static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2111{
0282a8c4 2112 int ret;
6fc6879b 2113 ret = os_snprintf(pos, end - pos, "-");
d85e1fc8 2114 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2115 return pos;
2116 pos += ret;
0282a8c4
JM
2117 ret = wpa_write_ciphers(pos, end, cipher, "+");
2118 if (ret < 0)
2119 return pos;
2120 pos += ret;
6fc6879b
JM
2121 return pos;
2122}
2123
2124
2125static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2126 const u8 *ie, size_t ie_len)
2127{
2128 struct wpa_ie_data data;
ea3b8c1d
JM
2129 char *start;
2130 int ret;
6fc6879b
JM
2131
2132 ret = os_snprintf(pos, end - pos, "[%s-", proto);
d85e1fc8 2133 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2134 return pos;
2135 pos += ret;
2136
2137 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2138 ret = os_snprintf(pos, end - pos, "?]");
d85e1fc8 2139 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2140 return pos;
2141 pos += ret;
2142 return pos;
2143 }
2144
ea3b8c1d 2145 start = pos;
6fc6879b 2146 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
ea3b8c1d
JM
2147 ret = os_snprintf(pos, end - pos, "%sEAP",
2148 pos == start ? "" : "+");
d85e1fc8 2149 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2150 return pos;
2151 pos += ret;
6fc6879b
JM
2152 }
2153 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
ea3b8c1d
JM
2154 ret = os_snprintf(pos, end - pos, "%sPSK",
2155 pos == start ? "" : "+");
d85e1fc8 2156 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2157 return pos;
2158 pos += ret;
6fc6879b
JM
2159 }
2160 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
ea3b8c1d
JM
2161 ret = os_snprintf(pos, end - pos, "%sNone",
2162 pos == start ? "" : "+");
d85e1fc8 2163 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2164 return pos;
2165 pos += ret;
6fc6879b 2166 }
be6b29f6
JA
2167 if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
2168 ret = os_snprintf(pos, end - pos, "%sSAE",
2169 pos == start ? "" : "+");
d85e1fc8 2170 if (os_snprintf_error(end - pos, ret))
be6b29f6
JA
2171 return pos;
2172 pos += ret;
2173 }
6fc6879b
JM
2174#ifdef CONFIG_IEEE80211R
2175 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2176 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
ea3b8c1d 2177 pos == start ? "" : "+");
d85e1fc8 2178 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2179 return pos;
2180 pos += ret;
6fc6879b
JM
2181 }
2182 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2183 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
ea3b8c1d 2184 pos == start ? "" : "+");
d85e1fc8 2185 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2186 return pos;
2187 pos += ret;
6fc6879b 2188 }
be6b29f6
JA
2189 if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
2190 ret = os_snprintf(pos, end - pos, "%sFT/SAE",
2191 pos == start ? "" : "+");
d85e1fc8 2192 if (os_snprintf_error(end - pos, ret))
be6b29f6
JA
2193 return pos;
2194 pos += ret;
2195 }
6fc6879b 2196#endif /* CONFIG_IEEE80211R */
56586197
JM
2197#ifdef CONFIG_IEEE80211W
2198 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2199 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
ea3b8c1d 2200 pos == start ? "" : "+");
d85e1fc8 2201 if (os_snprintf_error(end - pos, ret))
56586197
JM
2202 return pos;
2203 pos += ret;
56586197
JM
2204 }
2205 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2206 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
ea3b8c1d 2207 pos == start ? "" : "+");
d85e1fc8 2208 if (os_snprintf_error(end - pos, ret))
56586197
JM
2209 return pos;
2210 pos += ret;
56586197
JM
2211 }
2212#endif /* CONFIG_IEEE80211W */
6fc6879b 2213
666497c8
JM
2214 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
2215 ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
2216 pos == start ? "" : "+");
d85e1fc8 2217 if (os_snprintf_error(end - pos, ret))
666497c8
JM
2218 return pos;
2219 pos += ret;
2220 }
2221
6fc6879b
JM
2222 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2223
2224 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2225 ret = os_snprintf(pos, end - pos, "-preauth");
d85e1fc8 2226 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2227 return pos;
2228 pos += ret;
2229 }
2230
2231 ret = os_snprintf(pos, end - pos, "]");
d85e1fc8 2232 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2233 return pos;
2234 pos += ret;
2235
2236 return pos;
2237}
2238
3a068632 2239
eef7d7a1 2240#ifdef CONFIG_WPS
31fcea93
JM
2241static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2242 char *pos, char *end,
3a068632
JM
2243 struct wpabuf *wps_ie)
2244{
eef7d7a1
JM
2245 int ret;
2246 const char *txt;
2247
eef7d7a1
JM
2248 if (wps_ie == NULL)
2249 return pos;
eef7d7a1
JM
2250 if (wps_is_selected_pbc_registrar(wps_ie))
2251 txt = "[WPS-PBC]";
31fcea93
JM
2252 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2253 txt = "[WPS-AUTH]";
eef7d7a1
JM
2254 else if (wps_is_selected_pin_registrar(wps_ie))
2255 txt = "[WPS-PIN]";
2256 else
2257 txt = "[WPS]";
2258
2259 ret = os_snprintf(pos, end - pos, "%s", txt);
a80ba67a 2260 if (!os_snprintf_error(end - pos, ret))
eef7d7a1
JM
2261 pos += ret;
2262 wpabuf_free(wps_ie);
3a068632
JM
2263 return pos;
2264}
2265#endif /* CONFIG_WPS */
2266
2267
31fcea93
JM
2268static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2269 char *pos, char *end,
16b71ac2 2270 const struct wpa_bss *bss)
3a068632
JM
2271{
2272#ifdef CONFIG_WPS
2273 struct wpabuf *wps_ie;
2274 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
31fcea93 2275 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
3a068632 2276#else /* CONFIG_WPS */
eef7d7a1 2277 return pos;
3a068632 2278#endif /* CONFIG_WPS */
eef7d7a1
JM
2279}
2280
6fc6879b
JM
2281
2282/* Format one result on one text line into a buffer. */
2283static int wpa_supplicant_ctrl_iface_scan_result(
31fcea93 2284 struct wpa_supplicant *wpa_s,
16b71ac2 2285 const struct wpa_bss *bss, char *buf, size_t buflen)
6fc6879b
JM
2286{
2287 char *pos, *end;
2288 int ret;
638d9456 2289 const u8 *ie, *ie2, *p2p, *mesh;
0c6b310e 2290
638d9456 2291 mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
0c6b310e 2292 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
bb50ae43
JM
2293 if (!p2p)
2294 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
0c6b310e
JM
2295 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2296 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2297 0)
2298 return 0; /* Do not show P2P listen discovery results here */
6fc6879b
JM
2299
2300 pos = buf;
2301 end = buf + buflen;
2302
2303 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
16b71ac2 2304 MAC2STR(bss->bssid), bss->freq, bss->level);
d85e1fc8 2305 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2306 return -1;
6fc6879b 2307 pos += ret;
16b71ac2 2308 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
6fc6879b
JM
2309 if (ie)
2310 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
16b71ac2 2311 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
638d9456
JA
2312 if (ie2) {
2313 pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
2314 ie2, 2 + ie2[1]);
2315 }
31fcea93 2316 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
16b71ac2 2317 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
6fc6879b 2318 ret = os_snprintf(pos, end - pos, "[WEP]");
d85e1fc8 2319 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2320 return -1;
6fc6879b
JM
2321 pos += ret;
2322 }
638d9456
JA
2323 if (mesh) {
2324 ret = os_snprintf(pos, end - pos, "[MESH]");
d85e1fc8 2325 if (os_snprintf_error(end - pos, ret))
638d9456
JA
2326 return -1;
2327 pos += ret;
2328 }
e403ba85
BS
2329 if (bss_is_dmg(bss)) {
2330 const char *s;
2331 ret = os_snprintf(pos, end - pos, "[DMG]");
d85e1fc8 2332 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2333 return -1;
6fc6879b 2334 pos += ret;
e403ba85
BS
2335 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
2336 case IEEE80211_CAP_DMG_IBSS:
2337 s = "[IBSS]";
2338 break;
2339 case IEEE80211_CAP_DMG_AP:
2340 s = "[ESS]";
2341 break;
2342 case IEEE80211_CAP_DMG_PBSS:
2343 s = "[PBSS]";
2344 break;
2345 default:
2346 s = "";
2347 break;
2348 }
2349 ret = os_snprintf(pos, end - pos, "%s", s);
d85e1fc8 2350 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2351 return -1;
bd1af96a 2352 pos += ret;
e403ba85
BS
2353 } else {
2354 if (bss->caps & IEEE80211_CAP_IBSS) {
2355 ret = os_snprintf(pos, end - pos, "[IBSS]");
d85e1fc8 2356 if (os_snprintf_error(end - pos, ret))
e403ba85
BS
2357 return -1;
2358 pos += ret;
2359 }
2360 if (bss->caps & IEEE80211_CAP_ESS) {
2361 ret = os_snprintf(pos, end - pos, "[ESS]");
d85e1fc8 2362 if (os_snprintf_error(end - pos, ret))
e403ba85
BS
2363 return -1;
2364 pos += ret;
2365 }
bd1af96a 2366 }
0c6b310e
JM
2367 if (p2p) {
2368 ret = os_snprintf(pos, end - pos, "[P2P]");
d85e1fc8 2369 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2370 return -1;
0c6b310e
JM
2371 pos += ret;
2372 }
64855b96 2373#ifdef CONFIG_HS20
4ed34f5a 2374 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
64855b96 2375 ret = os_snprintf(pos, end - pos, "[HS20]");
d85e1fc8 2376 if (os_snprintf_error(end - pos, ret))
64855b96
JM
2377 return -1;
2378 pos += ret;
2379 }
2380#endif /* CONFIG_HS20 */
6fc6879b 2381
6fc6879b 2382 ret = os_snprintf(pos, end - pos, "\t%s",
16b71ac2 2383 wpa_ssid_txt(bss->ssid, bss->ssid_len));
d85e1fc8 2384 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2385 return -1;
6fc6879b
JM
2386 pos += ret;
2387
2388 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 2389 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2390 return -1;
6fc6879b
JM
2391 pos += ret;
2392
2393 return pos - buf;
2394}
2395
2396
2397static int wpa_supplicant_ctrl_iface_scan_results(
2398 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2399{
2400 char *pos, *end;
16b71ac2 2401 struct wpa_bss *bss;
6fc6879b 2402 int ret;
6fc6879b
JM
2403
2404 pos = buf;
2405 end = buf + buflen;
2406 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2407 "flags / ssid\n");
d85e1fc8 2408 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2409 return pos - buf;
2410 pos += ret;
2411
16b71ac2 2412 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
31fcea93 2413 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
6fc6879b
JM
2414 end - pos);
2415 if (ret < 0 || ret >= end - pos)
2416 return pos - buf;
2417 pos += ret;
2418 }
2419
2420 return pos - buf;
2421}
2422
2423
603a3f34
JL
2424#ifdef CONFIG_MESH
2425
5b78493f
MH
2426static int wpa_supplicant_ctrl_iface_mesh_interface_add(
2427 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
2428{
2429 char *pos, ifname[IFNAMSIZ + 1];
2430
2431 ifname[0] = '\0';
2432
2433 pos = os_strstr(cmd, "ifname=");
2434 if (pos) {
2435 pos += 7;
2436 os_strlcpy(ifname, pos, sizeof(ifname));
2437 }
2438
2439 if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
2440 return -1;
2441
2442 os_strlcpy(reply, ifname, max_len);
2443 return os_strlen(ifname);
2444}
2445
2446
603a3f34
JL
2447static int wpa_supplicant_ctrl_iface_mesh_group_add(
2448 struct wpa_supplicant *wpa_s, char *cmd)
2449{
2450 int id;
2451 struct wpa_ssid *ssid;
2452
2453 id = atoi(cmd);
2454 wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
2455
2456 ssid = wpa_config_get_network(wpa_s->conf, id);
2457 if (ssid == NULL) {
2458 wpa_printf(MSG_DEBUG,
2459 "CTRL_IFACE: Could not find network id=%d", id);
2460 return -1;
2461 }
2462 if (ssid->mode != WPAS_MODE_MESH) {
2463 wpa_printf(MSG_DEBUG,
2464 "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
2465 return -1;
2466 }
0c6099f3
MH
2467 if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
2468 ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
2469 wpa_printf(MSG_ERROR,
2470 "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
2471 return -1;
2472 }
603a3f34
JL
2473
2474 /*
2475 * TODO: If necessary write our own group_add function,
2476 * for now we can reuse select_network
2477 */
2478 wpa_supplicant_select_network(wpa_s, ssid);
2479
2480 return 0;
2481}
2482
2483
2484static int wpa_supplicant_ctrl_iface_mesh_group_remove(
2485 struct wpa_supplicant *wpa_s, char *cmd)
2486{
5b78493f
MH
2487 struct wpa_supplicant *orig;
2488 struct wpa_global *global;
2489 int found = 0;
2490
2491 wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
2492
2493 global = wpa_s->global;
2494 orig = wpa_s;
2495
2496 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2497 if (os_strcmp(wpa_s->ifname, cmd) == 0) {
2498 found = 1;
2499 break;
2500 }
2501 }
2502 if (!found) {
2503 wpa_printf(MSG_ERROR,
2504 "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
603a3f34
JL
2505 cmd);
2506 return -1;
2507 }
5b78493f
MH
2508 if (wpa_s->mesh_if_created && wpa_s == orig) {
2509 wpa_printf(MSG_ERROR,
2510 "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
2511 return -1;
2512 }
603a3f34
JL
2513
2514 wpa_s->reassociate = 0;
2515 wpa_s->disconnected = 1;
2516 wpa_supplicant_cancel_sched_scan(wpa_s);
2517 wpa_supplicant_cancel_scan(wpa_s);
2518
2519 /*
2520 * TODO: If necessary write our own group_remove function,
2521 * for now we can reuse deauthenticate
2522 */
2523 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2524
5b78493f
MH
2525 if (wpa_s->mesh_if_created)
2526 wpa_supplicant_remove_iface(global, wpa_s, 0);
2527
603a3f34
JL
2528 return 0;
2529}
2530
2531#endif /* CONFIG_MESH */
2532
2533
6fc6879b
JM
2534static int wpa_supplicant_ctrl_iface_select_network(
2535 struct wpa_supplicant *wpa_s, char *cmd)
2536{
2537 int id;
2538 struct wpa_ssid *ssid;
204c9ac4 2539 char *pos;
6fc6879b
JM
2540
2541 /* cmd: "<network id>" or "any" */
204c9ac4 2542 if (os_strncmp(cmd, "any", 3) == 0) {
6fc6879b 2543 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
86b89452
WS
2544 ssid = NULL;
2545 } else {
2546 id = atoi(cmd);
2547 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
6fc6879b 2548
86b89452
WS
2549 ssid = wpa_config_get_network(wpa_s->conf, id);
2550 if (ssid == NULL) {
2551 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2552 "network id=%d", id);
2553 return -1;
2554 }
4dac0245
JM
2555 if (ssid->disabled == 2) {
2556 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2557 "SELECT_NETWORK with persistent P2P group");
2558 return -1;
2559 }
6fc6879b
JM
2560 }
2561
204c9ac4
DS
2562 pos = os_strstr(cmd, " freq=");
2563 if (pos) {
2564 int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
2565 if (freqs) {
2566 wpa_s->scan_req = MANUAL_SCAN_REQ;
2567 os_free(wpa_s->manual_scan_freqs);
2568 wpa_s->manual_scan_freqs = freqs;
2569 }
2570 }
2571
86b89452 2572 wpa_supplicant_select_network(wpa_s, ssid);
6fc6879b
JM
2573
2574 return 0;
2575}
2576
2577
2578static int wpa_supplicant_ctrl_iface_enable_network(
2579 struct wpa_supplicant *wpa_s, char *cmd)
2580{
2581 int id;
2582 struct wpa_ssid *ssid;
2583
2584 /* cmd: "<network id>" or "all" */
2585 if (os_strcmp(cmd, "all") == 0) {
2586 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
86b89452
WS
2587 ssid = NULL;
2588 } else {
2589 id = atoi(cmd);
2590 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
6fc6879b 2591
86b89452
WS
2592 ssid = wpa_config_get_network(wpa_s->conf, id);
2593 if (ssid == NULL) {
2594 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2595 "network id=%d", id);
2596 return -1;
2597 }
4dac0245
JM
2598 if (ssid->disabled == 2) {
2599 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2600 "ENABLE_NETWORK with persistent P2P group");
2601 return -1;
2602 }
84c78f95
JM
2603
2604 if (os_strstr(cmd, " no-connect")) {
2605 ssid->disabled = 0;
2606 return 0;
2607 }
6fc6879b 2608 }
86b89452 2609 wpa_supplicant_enable_network(wpa_s, ssid);
6fc6879b
JM
2610
2611 return 0;
2612}
2613
2614
2615static int wpa_supplicant_ctrl_iface_disable_network(
2616 struct wpa_supplicant *wpa_s, char *cmd)
2617{
2618 int id;
2619 struct wpa_ssid *ssid;
2620
2621 /* cmd: "<network id>" or "all" */
2622 if (os_strcmp(cmd, "all") == 0) {
2623 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
86b89452
WS
2624 ssid = NULL;
2625 } else {
2626 id = atoi(cmd);
2627 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
6fc6879b 2628
86b89452
WS
2629 ssid = wpa_config_get_network(wpa_s->conf, id);
2630 if (ssid == NULL) {
2631 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2632 "network id=%d", id);
2633 return -1;
2634 }
4dac0245
JM
2635 if (ssid->disabled == 2) {
2636 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2637 "DISABLE_NETWORK with persistent P2P "
2638 "group");
2639 return -1;
2640 }
6fc6879b 2641 }
86b89452 2642 wpa_supplicant_disable_network(wpa_s, ssid);
6fc6879b
JM
2643
2644 return 0;
2645}
2646
2647
2648static int wpa_supplicant_ctrl_iface_add_network(
2649 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2650{
2651 struct wpa_ssid *ssid;
2652 int ret;
2653
2654 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2655
2656 ssid = wpa_config_add_network(wpa_s->conf);
2657 if (ssid == NULL)
2658 return -1;
8bac466b
JM
2659
2660 wpas_notify_network_added(wpa_s, ssid);
2661
6fc6879b
JM
2662 ssid->disabled = 1;
2663 wpa_config_set_network_defaults(ssid);
2664
2665 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
d85e1fc8 2666 if (os_snprintf_error(buflen, ret))
6fc6879b
JM
2667 return -1;
2668 return ret;
2669}
2670
2671
2672static int wpa_supplicant_ctrl_iface_remove_network(
2673 struct wpa_supplicant *wpa_s, char *cmd)
2674{
2675 int id;
2676 struct wpa_ssid *ssid;
725fc39e 2677 int was_disabled;
6fc6879b
JM
2678
2679 /* cmd: "<network id>" or "all" */
2680 if (os_strcmp(cmd, "all") == 0) {
2681 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
725fc39e
DS
2682 if (wpa_s->sched_scanning)
2683 wpa_supplicant_cancel_sched_scan(wpa_s);
2684
d8a790b9 2685 eapol_sm_invalidate_cached_session(wpa_s->eapol);
6fc6879b 2686 if (wpa_s->current_ssid) {
83df8149
JM
2687#ifdef CONFIG_SME
2688 wpa_s->sme.prev_bssid_set = 0;
2689#endif /* CONFIG_SME */
20a0b03d
JM
2690 wpa_sm_set_config(wpa_s->wpa, NULL);
2691 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
07783eaa
JM
2692 wpa_supplicant_deauthenticate(
2693 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
6fc6879b 2694 }
391f4925
JK
2695 ssid = wpa_s->conf->ssid;
2696 while (ssid) {
2697 struct wpa_ssid *remove_ssid = ssid;
2698 id = ssid->id;
2699 ssid = ssid->next;
c267753b
JM
2700 if (wpa_s->last_ssid == remove_ssid)
2701 wpa_s->last_ssid = NULL;
391f4925
JK
2702 wpas_notify_network_removed(wpa_s, remove_ssid);
2703 wpa_config_remove_network(wpa_s->conf, id);
2704 }
6fc6879b
JM
2705 return 0;
2706 }
2707
2708 id = atoi(cmd);
2709 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
2710
2711 ssid = wpa_config_get_network(wpa_s->conf, id);
f3857c2e
JM
2712 if (ssid)
2713 wpas_notify_network_removed(wpa_s, ssid);
59ff6653 2714 if (ssid == NULL) {
6fc6879b
JM
2715 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2716 "id=%d", id);
2717 return -1;
2718 }
2719
c267753b
JM
2720 if (wpa_s->last_ssid == ssid)
2721 wpa_s->last_ssid = NULL;
2722
d8a790b9 2723 if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
83df8149
JM
2724#ifdef CONFIG_SME
2725 wpa_s->sme.prev_bssid_set = 0;
2726#endif /* CONFIG_SME */
6fc6879b 2727 /*
d8a790b9
JM
2728 * Invalidate the EAP session cache if the current or
2729 * previously used network is removed.
6fc6879b
JM
2730 */
2731 eapol_sm_invalidate_cached_session(wpa_s->eapol);
d8a790b9
JM
2732 }
2733
2734 if (ssid == wpa_s->current_ssid) {
20a0b03d
JM
2735 wpa_sm_set_config(wpa_s->wpa, NULL);
2736 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
6fc6879b 2737
07783eaa
JM
2738 wpa_supplicant_deauthenticate(wpa_s,
2739 WLAN_REASON_DEAUTH_LEAVING);
6fc6879b
JM
2740 }
2741
725fc39e
DS
2742 was_disabled = ssid->disabled;
2743
59ff6653
DG
2744 if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
2745 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
2746 "network id=%d", id);
2747 return -1;
2748 }
2749
725fc39e
DS
2750 if (!was_disabled && wpa_s->sched_scanning) {
2751 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
2752 "network from filters");
2753 wpa_supplicant_cancel_sched_scan(wpa_s);
2754 wpa_supplicant_req_scan(wpa_s, 0, 0);
2755 }
2756
6fc6879b
JM
2757 return 0;
2758}
2759
2760
1c330a2f
DS
2761static int wpa_supplicant_ctrl_iface_update_network(
2762 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2763 char *name, char *value)
2764{
2765 if (wpa_config_set(ssid, name, value, 0) < 0) {
2766 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
2767 "variable '%s'", name);
2768 return -1;
2769 }
2770
2771 if (os_strcmp(name, "bssid") != 0 &&
2772 os_strcmp(name, "priority") != 0)
2773 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
2774
2775 if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
2776 /*
2777 * Invalidate the EAP session cache if anything in the current
2778 * or previously used configuration changes.
2779 */
2780 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2781 }
2782
2783 if ((os_strcmp(name, "psk") == 0 &&
2784 value[0] == '"' && ssid->ssid_len) ||
2785 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
2786 wpa_config_update_psk(ssid);
2787 else if (os_strcmp(name, "priority") == 0)
2788 wpa_config_update_prio_list(wpa_s->conf);
07cb45cc
TP
2789 else if (os_strcmp(name, "no_auto_peer") == 0)
2790 ssid->no_auto_peer = atoi(value);
1c330a2f
DS
2791
2792 return 0;
2793}
2794
2795
6fc6879b
JM
2796static int wpa_supplicant_ctrl_iface_set_network(
2797 struct wpa_supplicant *wpa_s, char *cmd)
2798{
0ef023e4 2799 int id, ret, prev_bssid_set;
6fc6879b
JM
2800 struct wpa_ssid *ssid;
2801 char *name, *value;
0ef023e4 2802 u8 prev_bssid[ETH_ALEN];
6fc6879b
JM
2803
2804 /* cmd: "<network id> <variable name> <value>" */
2805 name = os_strchr(cmd, ' ');
2806 if (name == NULL)
2807 return -1;
2808 *name++ = '\0';
2809
2810 value = os_strchr(name, ' ');
2811 if (value == NULL)
2812 return -1;
2813 *value++ = '\0';
2814
2815 id = atoi(cmd);
2816 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
2817 id, name);
2818 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2819 (u8 *) value, os_strlen(value));
2820
2821 ssid = wpa_config_get_network(wpa_s->conf, id);
2822 if (ssid == NULL) {
2823 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2824 "id=%d", id);
2825 return -1;
2826 }
2827
0ef023e4
JM
2828 prev_bssid_set = ssid->bssid_set;
2829 os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
2830 ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
2831 value);
2832 if (ret == 0 &&
2833 (ssid->bssid_set != prev_bssid_set ||
2834 os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
2835 wpas_notify_network_bssid_set_changed(wpa_s, ssid);
2836 return ret;
6fc6879b
JM
2837}
2838
2839
2840static int wpa_supplicant_ctrl_iface_get_network(
2841 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2842{
2843 int id;
2844 size_t res;
2845 struct wpa_ssid *ssid;
2846 char *name, *value;
2847
2848 /* cmd: "<network id> <variable name>" */
2849 name = os_strchr(cmd, ' ');
2850 if (name == NULL || buflen == 0)
2851 return -1;
2852 *name++ = '\0';
2853
2854 id = atoi(cmd);
2855 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
2856 id, name);
2857
2858 ssid = wpa_config_get_network(wpa_s->conf, id);
2859 if (ssid == NULL) {
2860 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
2861 "id=%d", id);
2862 return -1;
2863 }
2864
2865 value = wpa_config_get_no_key(ssid, name);
2866 if (value == NULL) {
2867 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2868 "variable '%s'", name);
2869 return -1;
2870 }
2871
2872 res = os_strlcpy(buf, value, buflen);
2873 if (res >= buflen) {
2874 os_free(value);
2875 return -1;
2876 }
2877
2878 os_free(value);
2879
2880 return res;
2881}
2882
2883
1c330a2f
DS
2884static int wpa_supplicant_ctrl_iface_dup_network(
2885 struct wpa_supplicant *wpa_s, char *cmd)
2886{
2887 struct wpa_ssid *ssid_s, *ssid_d;
2888 char *name, *id, *value;
2889 int id_s, id_d, ret;
2890
2891 /* cmd: "<src network id> <dst network id> <variable name>" */
2892 id = os_strchr(cmd, ' ');
2893 if (id == NULL)
2894 return -1;
2895 *id++ = '\0';
2896
2897 name = os_strchr(id, ' ');
2898 if (name == NULL)
2899 return -1;
2900 *name++ = '\0';
2901
2902 id_s = atoi(cmd);
2903 id_d = atoi(id);
2904 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DUP_NETWORK id=%d -> %d name='%s'",
2905 id_s, id_d, name);
2906
2907 ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
2908 if (ssid_s == NULL) {
2909 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2910 "network id=%d", id_s);
2911 return -1;
2912 }
2913
2914 ssid_d = wpa_config_get_network(wpa_s->conf, id_d);
2915 if (ssid_d == NULL) {
2916 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
c0541906 2917 "network id=%d", id_d);
1c330a2f
DS
2918 return -1;
2919 }
2920
2921 value = wpa_config_get(ssid_s, name);
2922 if (value == NULL) {
2923 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
2924 "variable '%s'", name);
2925 return -1;
2926 }
2927
2928 ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid_d, name,
2929 value);
2930
2931 os_free(value);
2932
2933 return ret;
2934}
2935
2936
d94c9ee6
JM
2937static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
2938 char *buf, size_t buflen)
2939{
2940 char *pos, *end;
2941 struct wpa_cred *cred;
2942 int ret;
2943
2944 pos = buf;
2945 end = buf + buflen;
2946 ret = os_snprintf(pos, end - pos,
2947 "cred id / realm / username / domain / imsi\n");
d85e1fc8 2948 if (os_snprintf_error(end - pos, ret))
d94c9ee6
JM
2949 return pos - buf;
2950 pos += ret;
2951
2952 cred = wpa_s->conf->cred;
2953 while (cred) {
2954 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
2955 cred->id, cred->realm ? cred->realm : "",
2956 cred->username ? cred->username : "",
463c8ffb 2957 cred->domain ? cred->domain[0] : "",
d94c9ee6 2958 cred->imsi ? cred->imsi : "");
d85e1fc8 2959 if (os_snprintf_error(end - pos, ret))
d94c9ee6
JM
2960 return pos - buf;
2961 pos += ret;
2962
2963 cred = cred->next;
2964 }
2965
2966 return pos - buf;
2967}
2968
2969
2970static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
2971 char *buf, size_t buflen)
2972{
2973 struct wpa_cred *cred;
2974 int ret;
2975
2976 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
2977
2978 cred = wpa_config_add_cred(wpa_s->conf);
2979 if (cred == NULL)
2980 return -1;
2981
1619e9d5
JM
2982 wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
2983
d94c9ee6 2984 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
d85e1fc8 2985 if (os_snprintf_error(buflen, ret))
d94c9ee6
JM
2986 return -1;
2987 return ret;
2988}
2989
2990
736d4f2d
JM
2991static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
2992 struct wpa_cred *cred)
2993{
2994 struct wpa_ssid *ssid;
2995 char str[20];
1619e9d5 2996 int id;
736d4f2d 2997
1619e9d5 2998 if (cred == NULL) {
736d4f2d
JM
2999 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3000 return -1;
3001 }
3002
1619e9d5
JM
3003 id = cred->id;
3004 if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
3005 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3006 return -1;
3007 }
3008
3009 wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
3010
736d4f2d
JM
3011 /* Remove any network entry created based on the removed credential */
3012 ssid = wpa_s->conf->ssid;
3013 while (ssid) {
3014 if (ssid->parent_cred == cred) {
1d399771
JM
3015 int res;
3016
736d4f2d
JM
3017 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
3018 "used the removed credential", ssid->id);
1d399771
JM
3019 res = os_snprintf(str, sizeof(str), "%d", ssid->id);
3020 if (os_snprintf_error(sizeof(str), res))
3021 str[sizeof(str) - 1] = '\0';
736d4f2d
JM
3022 ssid = ssid->next;
3023 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
3024 } else
3025 ssid = ssid->next;
3026 }
3027
3028 return 0;
3029}
3030
3031
d94c9ee6
JM
3032static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
3033 char *cmd)
3034{
3035 int id;
736d4f2d 3036 struct wpa_cred *cred, *prev;
d94c9ee6 3037
aa26ba68
JM
3038 /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
3039 * "provisioning_sp=<FQDN> */
d94c9ee6
JM
3040 if (os_strcmp(cmd, "all") == 0) {
3041 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
3042 cred = wpa_s->conf->cred;
3043 while (cred) {
736d4f2d 3044 prev = cred;
d94c9ee6 3045 cred = cred->next;
736d4f2d 3046 wpas_ctrl_remove_cred(wpa_s, prev);
d94c9ee6
JM
3047 }
3048 return 0;
3049 }
3050
9afe52eb
JM
3051 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
3052 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
3053 cmd + 8);
3054 cred = wpa_s->conf->cred;
3055 while (cred) {
3056 prev = cred;
3057 cred = cred->next;
463c8ffb
JM
3058 if (prev->domain) {
3059 size_t i;
3060 for (i = 0; i < prev->num_domain; i++) {
3061 if (os_strcmp(prev->domain[i], cmd + 8)
3062 != 0)
3063 continue;
3064 wpas_ctrl_remove_cred(wpa_s, prev);
3065 break;
3066 }
3067 }
9afe52eb
JM
3068 }
3069 return 0;
3070 }
3071
aa26ba68
JM
3072 if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
3073 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
3074 cmd + 16);
3075 cred = wpa_s->conf->cred;
3076 while (cred) {
3077 prev = cred;
3078 cred = cred->next;
3079 if (prev->provisioning_sp &&
3080 os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
3081 wpas_ctrl_remove_cred(wpa_s, prev);
3082 }
3083 return 0;
3084 }
3085
d94c9ee6
JM
3086 id = atoi(cmd);
3087 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
3088
3089 cred = wpa_config_get_cred(wpa_s->conf, id);
736d4f2d 3090 return wpas_ctrl_remove_cred(wpa_s, cred);
d94c9ee6
JM
3091}
3092
3093
3094static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
3095 char *cmd)
3096{
3097 int id;
3098 struct wpa_cred *cred;
3099 char *name, *value;
3100
3101 /* cmd: "<cred id> <variable name> <value>" */
3102 name = os_strchr(cmd, ' ');
3103 if (name == NULL)
3104 return -1;
3105 *name++ = '\0';
3106
3107 value = os_strchr(name, ' ');
3108 if (value == NULL)
3109 return -1;
3110 *value++ = '\0';
3111
3112 id = atoi(cmd);
3113 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
3114 id, name);
3115 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3116 (u8 *) value, os_strlen(value));
3117
3118 cred = wpa_config_get_cred(wpa_s->conf, id);
3119 if (cred == NULL) {
3120 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3121 id);
3122 return -1;
3123 }
3124
3125 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
3126 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
3127 "variable '%s'", name);
3128 return -1;
3129 }
3130
1619e9d5
JM
3131 wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
3132
d94c9ee6
JM
3133 return 0;
3134}
3135
3136
c880ab87
JM
3137static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
3138 char *cmd, char *buf,
3139 size_t buflen)
3140{
3141 int id;
3142 size_t res;
3143 struct wpa_cred *cred;
3144 char *name, *value;
3145
3146 /* cmd: "<cred id> <variable name>" */
3147 name = os_strchr(cmd, ' ');
3148 if (name == NULL)
3149 return -1;
3150 *name++ = '\0';
3151
3152 id = atoi(cmd);
3153 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
3154 id, name);
3155
3156 cred = wpa_config_get_cred(wpa_s->conf, id);
3157 if (cred == NULL) {
3158 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3159 id);
3160 return -1;
3161 }
3162
3163 value = wpa_config_get_cred_no_key(cred, name);
3164 if (value == NULL) {
3165 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
3166 name);
3167 return -1;
3168 }
3169
3170 res = os_strlcpy(buf, value, buflen);
3171 if (res >= buflen) {
3172 os_free(value);
3173 return -1;
3174 }
3175
3176 os_free(value);
3177
3178 return res;
3179}
3180
3181
6fc6879b
JM
3182#ifndef CONFIG_NO_CONFIG_WRITE
3183static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
3184{
3185 int ret;
3186
3187 if (!wpa_s->conf->update_config) {
3188 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
3189 "to update configuration (update_config=0)");
3190 return -1;
3191 }
3192
3193 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
3194 if (ret) {
3195 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
3196 "update configuration");
3197 } else {
3198 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
3199 " updated");
3200 }
3201
3202 return ret;
3203}
3204#endif /* CONFIG_NO_CONFIG_WRITE */
3205
3206
4daa011b
JM
3207struct cipher_info {
3208 unsigned int capa;
3209 const char *name;
3210 int group_only;
3211};
3212
3213static const struct cipher_info ciphers[] = {
3214 { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
3215 { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
3216 { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
3217 { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
3218 { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
3219 { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
3220 { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
3221 { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
3222};
3223
3224
6fc6879b
JM
3225static int ctrl_iface_get_capability_pairwise(int res, char *strict,
3226 struct wpa_driver_capa *capa,
3227 char *buf, size_t buflen)
3228{
ea3b8c1d 3229 int ret;
6fc6879b
JM
3230 char *pos, *end;
3231 size_t len;
4daa011b 3232 unsigned int i;
6fc6879b
JM
3233
3234 pos = buf;
3235 end = pos + buflen;
3236
3237 if (res < 0) {
3238 if (strict)
3239 return 0;
3240 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
3241 if (len >= buflen)
3242 return -1;
3243 return len;
3244 }
3245
4daa011b
JM
3246 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3247 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
3248 ret = os_snprintf(pos, end - pos, "%s%s",
ea3b8c1d
JM
3249 pos == buf ? "" : " ",
3250 ciphers[i].name);
d85e1fc8 3251 if (os_snprintf_error(end - pos, ret))
4daa011b
JM
3252 return pos - buf;
3253 pos += ret;
4daa011b 3254 }
6fc6879b
JM
3255 }
3256
3257 return pos - buf;
3258}
3259
3260
3261static int ctrl_iface_get_capability_group(int res, char *strict,
3262 struct wpa_driver_capa *capa,
3263 char *buf, size_t buflen)
3264{
ea3b8c1d 3265 int ret;
6fc6879b
JM
3266 char *pos, *end;
3267 size_t len;
4daa011b 3268 unsigned int i;
6fc6879b
JM
3269
3270 pos = buf;
3271 end = pos + buflen;
3272
3273 if (res < 0) {
3274 if (strict)
3275 return 0;
3276 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
3277 if (len >= buflen)
3278 return -1;
3279 return len;
3280 }
3281
4daa011b
JM
3282 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3283 if (capa->enc & ciphers[i].capa) {
3284 ret = os_snprintf(pos, end - pos, "%s%s",
ea3b8c1d
JM
3285 pos == buf ? "" : " ",
3286 ciphers[i].name);
d85e1fc8 3287 if (os_snprintf_error(end - pos, ret))
4daa011b
JM
3288 return pos - buf;
3289 pos += ret;
4daa011b 3290 }
6fc6879b
JM
3291 }
3292
3293 return pos - buf;
3294}
3295
3296
3297static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
3298 struct wpa_driver_capa *capa,
3299 char *buf, size_t buflen)
3300{
3301 int ret;
3302 char *pos, *end;
3303 size_t len;
3304
3305 pos = buf;
3306 end = pos + buflen;
3307
3308 if (res < 0) {
3309 if (strict)
3310 return 0;
3311 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
3312 "NONE", buflen);
3313 if (len >= buflen)
3314 return -1;
3315 return len;
3316 }
3317
3318 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
d85e1fc8 3319 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3320 return pos - buf;
3321 pos += ret;
3322
3323 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3324 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
3325 ret = os_snprintf(pos, end - pos, " WPA-EAP");
d85e1fc8 3326 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3327 return pos - buf;
3328 pos += ret;
3329 }
3330
3331 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3332 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
3333 ret = os_snprintf(pos, end - pos, " WPA-PSK");
d85e1fc8 3334 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3335 return pos - buf;
3336 pos += ret;
3337 }
3338
3339 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
3340 ret = os_snprintf(pos, end - pos, " WPA-NONE");
d85e1fc8 3341 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3342 return pos - buf;
3343 pos += ret;
3344 }
3345
3346 return pos - buf;
3347}
3348
3349
3350static int ctrl_iface_get_capability_proto(int res, char *strict,
3351 struct wpa_driver_capa *capa,
3352 char *buf, size_t buflen)
3353{
ea3b8c1d 3354 int ret;
6fc6879b
JM
3355 char *pos, *end;
3356 size_t len;
3357
3358 pos = buf;
3359 end = pos + buflen;
3360
3361 if (res < 0) {
3362 if (strict)
3363 return 0;
3364 len = os_strlcpy(buf, "RSN WPA", buflen);
3365 if (len >= buflen)
3366 return -1;
3367 return len;
3368 }
3369
3370 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3371 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
ea3b8c1d
JM
3372 ret = os_snprintf(pos, end - pos, "%sRSN",
3373 pos == buf ? "" : " ");
d85e1fc8 3374 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3375 return pos - buf;
3376 pos += ret;
6fc6879b
JM
3377 }
3378
3379 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3380 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
ea3b8c1d
JM
3381 ret = os_snprintf(pos, end - pos, "%sWPA",
3382 pos == buf ? "" : " ");
d85e1fc8 3383 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3384 return pos - buf;
3385 pos += ret;
6fc6879b
JM
3386 }
3387
3388 return pos - buf;
3389}
3390
3391
3392static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
3393 struct wpa_driver_capa *capa,
3394 char *buf, size_t buflen)
3395{
ea3b8c1d 3396 int ret;
6fc6879b
JM
3397 char *pos, *end;
3398 size_t len;
3399
3400 pos = buf;
3401 end = pos + buflen;
3402
3403 if (res < 0) {
3404 if (strict)
3405 return 0;
3406 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
3407 if (len >= buflen)
3408 return -1;
3409 return len;
3410 }
3411
3412 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
ea3b8c1d
JM
3413 ret = os_snprintf(pos, end - pos, "%sOPEN",
3414 pos == buf ? "" : " ");
d85e1fc8 3415 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3416 return pos - buf;
3417 pos += ret;
6fc6879b
JM
3418 }
3419
3420 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
3421 ret = os_snprintf(pos, end - pos, "%sSHARED",
ea3b8c1d 3422 pos == buf ? "" : " ");
d85e1fc8 3423 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3424 return pos - buf;
3425 pos += ret;
6fc6879b
JM
3426 }
3427
3428 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
ea3b8c1d
JM
3429 ret = os_snprintf(pos, end - pos, "%sLEAP",
3430 pos == buf ? "" : " ");
d85e1fc8 3431 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3432 return pos - buf;
3433 pos += ret;
6fc6879b
JM
3434 }
3435
3436 return pos - buf;
3437}
3438
3439
65d52fc1
BR
3440static int ctrl_iface_get_capability_modes(int res, char *strict,
3441 struct wpa_driver_capa *capa,
3442 char *buf, size_t buflen)
3443{
ea3b8c1d 3444 int ret;
65d52fc1
BR
3445 char *pos, *end;
3446 size_t len;
3447
3448 pos = buf;
3449 end = pos + buflen;
3450
3451 if (res < 0) {
3452 if (strict)
3453 return 0;
3454 len = os_strlcpy(buf, "IBSS AP", buflen);
3455 if (len >= buflen)
3456 return -1;
3457 return len;
3458 }
3459
3460 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
ea3b8c1d
JM
3461 ret = os_snprintf(pos, end - pos, "%sIBSS",
3462 pos == buf ? "" : " ");
d85e1fc8 3463 if (os_snprintf_error(end - pos, ret))
65d52fc1
BR
3464 return pos - buf;
3465 pos += ret;
65d52fc1
BR
3466 }
3467
3468 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
ea3b8c1d
JM
3469 ret = os_snprintf(pos, end - pos, "%sAP",
3470 pos == buf ? "" : " ");
d85e1fc8 3471 if (os_snprintf_error(end - pos, ret))
65d52fc1
BR
3472 return pos - buf;
3473 pos += ret;
65d52fc1
BR
3474 }
3475
3476 return pos - buf;
3477}
3478
3479
35aa088a
DS
3480static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
3481 char *buf, size_t buflen)
3482{
3483 struct hostapd_channel_data *chnl;
3484 int ret, i, j;
3485 char *pos, *end, *hmode;
3486
3487 pos = buf;
3488 end = pos + buflen;
3489
3490 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3491 switch (wpa_s->hw.modes[j].mode) {
3492 case HOSTAPD_MODE_IEEE80211B:
3493 hmode = "B";
3494 break;
3495 case HOSTAPD_MODE_IEEE80211G:
3496 hmode = "G";
3497 break;
3498 case HOSTAPD_MODE_IEEE80211A:
3499 hmode = "A";
3500 break;
7829894c
VK
3501 case HOSTAPD_MODE_IEEE80211AD:
3502 hmode = "AD";
3503 break;
35aa088a
DS
3504 default:
3505 continue;
3506 }
3507 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
d85e1fc8 3508 if (os_snprintf_error(end - pos, ret))
35aa088a
DS
3509 return pos - buf;
3510 pos += ret;
3511 chnl = wpa_s->hw.modes[j].channels;
3512 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3513 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3514 continue;
3515 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
d85e1fc8 3516 if (os_snprintf_error(end - pos, ret))
35aa088a
DS
3517 return pos - buf;
3518 pos += ret;
3519 }
3520 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 3521 if (os_snprintf_error(end - pos, ret))
35aa088a
DS
3522 return pos - buf;
3523 pos += ret;
3524 }
3525
3526 return pos - buf;
3527}
3528
3529
06060522
BR
3530static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
3531 char *buf, size_t buflen)
3532{
3533 struct hostapd_channel_data *chnl;
3534 int ret, i, j;
3535 char *pos, *end, *hmode;
3536
3537 pos = buf;
3538 end = pos + buflen;
3539
3540 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3541 switch (wpa_s->hw.modes[j].mode) {
3542 case HOSTAPD_MODE_IEEE80211B:
3543 hmode = "B";
3544 break;
3545 case HOSTAPD_MODE_IEEE80211G:
3546 hmode = "G";
3547 break;
3548 case HOSTAPD_MODE_IEEE80211A:
3549 hmode = "A";
3550 break;
3551 case HOSTAPD_MODE_IEEE80211AD:
3552 hmode = "AD";
3553 break;
3554 default:
3555 continue;
3556 }
3557 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3558 hmode);
d85e1fc8 3559 if (os_snprintf_error(end - pos, ret))
06060522
BR
3560 return pos - buf;
3561 pos += ret;
3562 chnl = wpa_s->hw.modes[j].channels;
3563 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3564 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3565 continue;
0547124d 3566 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
06060522 3567 chnl[i].chan, chnl[i].freq,
0a443580
IP
3568 chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
3569 " (NO_IR)" : "",
0547124d
DS
3570 chnl[i].flag & HOSTAPD_CHAN_RADAR ?
3571 " (DFS)" : "");
3572
d85e1fc8 3573 if (os_snprintf_error(end - pos, ret))
06060522
BR
3574 return pos - buf;
3575 pos += ret;
3576 }
3577 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 3578 if (os_snprintf_error(end - pos, ret))
06060522
BR
3579 return pos - buf;
3580 pos += ret;
3581 }
3582
3583 return pos - buf;
3584}
3585
3586
6fc6879b
JM
3587static int wpa_supplicant_ctrl_iface_get_capability(
3588 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3589 size_t buflen)
3590{
3591 struct wpa_driver_capa capa;
3592 int res;
3593 char *strict;
3594 char field[30];
3595 size_t len;
3596
3597 /* Determine whether or not strict checking was requested */
3598 len = os_strlcpy(field, _field, sizeof(field));
3599 if (len >= sizeof(field))
3600 return -1;
3601 strict = os_strchr(field, ' ');
3602 if (strict != NULL) {
3603 *strict++ = '\0';
3604 if (os_strcmp(strict, "strict") != 0)
3605 return -1;
3606 }
3607
3608 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3609 field, strict ? strict : "");
3610
3611 if (os_strcmp(field, "eap") == 0) {
3612 return eap_get_names(buf, buflen);
3613 }
3614
3615 res = wpa_drv_get_capa(wpa_s, &capa);
3616
3617 if (os_strcmp(field, "pairwise") == 0)
3618 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3619 buf, buflen);
3620
3621 if (os_strcmp(field, "group") == 0)
3622 return ctrl_iface_get_capability_group(res, strict, &capa,
3623 buf, buflen);
3624
3625 if (os_strcmp(field, "key_mgmt") == 0)
3626 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
3627 buf, buflen);
3628
3629 if (os_strcmp(field, "proto") == 0)
3630 return ctrl_iface_get_capability_proto(res, strict, &capa,
3631 buf, buflen);
3632
3633 if (os_strcmp(field, "auth_alg") == 0)
3634 return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
3635 buf, buflen);
3636
65d52fc1
BR
3637 if (os_strcmp(field, "modes") == 0)
3638 return ctrl_iface_get_capability_modes(res, strict, &capa,
3639 buf, buflen);
3640
35aa088a
DS
3641 if (os_strcmp(field, "channels") == 0)
3642 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
3643
06060522
BR
3644 if (os_strcmp(field, "freq") == 0)
3645 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
3646
6e9375e4
DS
3647#ifdef CONFIG_TDLS
3648 if (os_strcmp(field, "tdls") == 0)
3649 return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
3650#endif /* CONFIG_TDLS */
3651
02a8d45a
JM
3652#ifdef CONFIG_ERP
3653 if (os_strcmp(field, "erp") == 0) {
3654 res = os_snprintf(buf, buflen, "ERP");
d85e1fc8 3655 if (os_snprintf_error(buflen, res))
02a8d45a
JM
3656 return -1;
3657 return res;
3658 }
3659#endif /* CONFIG_EPR */
3660
6fc6879b
JM
3661 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3662 field);
3663
3664 return -1;
3665}
3666
3667
afc064fe
JM
3668#ifdef CONFIG_INTERWORKING
3669static char * anqp_add_hex(char *pos, char *end, const char *title,
3670 struct wpabuf *data)
3671{
3672 char *start = pos;
3673 size_t i;
3674 int ret;
3675 const u8 *d;
3676
3677 if (data == NULL)
3678 return start;
3679
3680 ret = os_snprintf(pos, end - pos, "%s=", title);
d85e1fc8 3681 if (os_snprintf_error(end - pos, ret))
afc064fe
JM
3682 return start;
3683 pos += ret;
3684
3685 d = wpabuf_head_u8(data);
3686 for (i = 0; i < wpabuf_len(data); i++) {
3687 ret = os_snprintf(pos, end - pos, "%02x", *d++);
d85e1fc8 3688 if (os_snprintf_error(end - pos, ret))
afc064fe
JM
3689 return start;
3690 pos += ret;
3691 }
3692
3693 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 3694 if (os_snprintf_error(end - pos, ret))
afc064fe
JM
3695 return start;
3696 pos += ret;
3697
3698 return pos;
3699}
3700#endif /* CONFIG_INTERWORKING */
3701
3702
61ce9085 3703static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5f97dd1c 3704 unsigned long mask, char *buf, size_t buflen)
6fc6879b 3705{
6fc6879b 3706 size_t i;
6fc6879b
JM
3707 int ret;
3708 char *pos, *end;
3709 const u8 *ie, *ie2;
3710
6fc6879b
JM
3711 pos = buf;
3712 end = buf + buflen;
6fc6879b 3713
5f97dd1c
DS
3714 if (mask & WPA_BSS_MASK_ID) {
3715 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
d85e1fc8 3716 if (os_snprintf_error(end - pos, ret))
5f97dd1c 3717 return 0;
6fc6879b
JM
3718 pos += ret;
3719 }
3720
5f97dd1c
DS
3721 if (mask & WPA_BSS_MASK_BSSID) {
3722 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
3723 MAC2STR(bss->bssid));
d85e1fc8 3724 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
3725 return 0;
3726 pos += ret;
3727 }
6fc6879b 3728
5f97dd1c
DS
3729 if (mask & WPA_BSS_MASK_FREQ) {
3730 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
d85e1fc8 3731 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
3732 return 0;
3733 pos += ret;
3734 }
6fc6879b 3735
5f97dd1c
DS
3736 if (mask & WPA_BSS_MASK_BEACON_INT) {
3737 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
3738 bss->beacon_int);
d85e1fc8 3739 if (os_snprintf_error(end - pos, ret))
5f97dd1c 3740 return 0;
6fc6879b
JM
3741 pos += ret;
3742 }
5f97dd1c
DS
3743
3744 if (mask & WPA_BSS_MASK_CAPABILITIES) {
3745 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
3746 bss->caps);
d85e1fc8 3747 if (os_snprintf_error(end - pos, ret))
5f97dd1c 3748 return 0;
6fc6879b
JM
3749 pos += ret;
3750 }
5f97dd1c
DS
3751
3752 if (mask & WPA_BSS_MASK_QUAL) {
3753 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
d85e1fc8 3754 if (os_snprintf_error(end - pos, ret))
5f97dd1c 3755 return 0;
bd1af96a
JM
3756 pos += ret;
3757 }
5f97dd1c
DS
3758
3759 if (mask & WPA_BSS_MASK_NOISE) {
3760 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
d85e1fc8 3761 if (os_snprintf_error(end - pos, ret))
5f97dd1c 3762 return 0;
cc81110d
JM
3763 pos += ret;
3764 }
6fc6879b 3765
5f97dd1c
DS
3766 if (mask & WPA_BSS_MASK_LEVEL) {
3767 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
d85e1fc8 3768 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
3769 return 0;
3770 pos += ret;
3771 }
6fc6879b 3772
5f97dd1c
DS
3773 if (mask & WPA_BSS_MASK_TSF) {
3774 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
3775 (unsigned long long) bss->tsf);
d85e1fc8 3776 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
3777 return 0;
3778 pos += ret;
3779 }
3780
3781 if (mask & WPA_BSS_MASK_AGE) {
acb69cec 3782 struct os_reltime now;
5f97dd1c 3783
acb69cec 3784 os_get_reltime(&now);
5f97dd1c
DS
3785 ret = os_snprintf(pos, end - pos, "age=%d\n",
3786 (int) (now.sec - bss->last_update.sec));
d85e1fc8 3787 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
3788 return 0;
3789 pos += ret;
3790 }
3791
3792 if (mask & WPA_BSS_MASK_IE) {
3793 ret = os_snprintf(pos, end - pos, "ie=");
d85e1fc8 3794 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
3795 return 0;
3796 pos += ret;
3797
3798 ie = (const u8 *) (bss + 1);
3799 for (i = 0; i < bss->ie_len; i++) {
3800 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
d85e1fc8 3801 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
3802 return 0;
3803 pos += ret;
3804 }
3805
3806 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 3807 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
3808 return 0;
3809 pos += ret;
3810 }
3811
3812 if (mask & WPA_BSS_MASK_FLAGS) {
3813 ret = os_snprintf(pos, end - pos, "flags=");
d85e1fc8 3814 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
3815 return 0;
3816 pos += ret;
3817
3818 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3819 if (ie)
3820 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
3821 2 + ie[1]);
3822 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3823 if (ie2)
3824 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
3825 2 + ie2[1]);
3826 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3827 if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
3828 ret = os_snprintf(pos, end - pos, "[WEP]");
d85e1fc8 3829 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
3830 return 0;
3831 pos += ret;
3832 }
e403ba85
BS
3833 if (bss_is_dmg(bss)) {
3834 const char *s;
3835 ret = os_snprintf(pos, end - pos, "[DMG]");
d85e1fc8 3836 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
3837 return 0;
3838 pos += ret;
e403ba85
BS
3839 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
3840 case IEEE80211_CAP_DMG_IBSS:
3841 s = "[IBSS]";
3842 break;
3843 case IEEE80211_CAP_DMG_AP:
3844 s = "[ESS]";
3845 break;
3846 case IEEE80211_CAP_DMG_PBSS:
3847 s = "[PBSS]";
3848 break;
3849 default:
3850 s = "";
3851 break;
3852 }
3853 ret = os_snprintf(pos, end - pos, "%s", s);
d85e1fc8 3854 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
3855 return 0;
3856 pos += ret;
e403ba85
BS
3857 } else {
3858 if (bss->caps & IEEE80211_CAP_IBSS) {
3859 ret = os_snprintf(pos, end - pos, "[IBSS]");
d85e1fc8 3860 if (os_snprintf_error(end - pos, ret))
e403ba85
BS
3861 return 0;
3862 pos += ret;
3863 }
3864 if (bss->caps & IEEE80211_CAP_ESS) {
3865 ret = os_snprintf(pos, end - pos, "[ESS]");
d85e1fc8 3866 if (os_snprintf_error(end - pos, ret))
e403ba85
BS
3867 return 0;
3868 pos += ret;
3869 }
5f97dd1c 3870 }
bb50ae43
JM
3871 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
3872 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
5f97dd1c 3873 ret = os_snprintf(pos, end - pos, "[P2P]");
d85e1fc8 3874 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
3875 return 0;
3876 pos += ret;
3877 }
64855b96
JM
3878#ifdef CONFIG_HS20
3879 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
3880 ret = os_snprintf(pos, end - pos, "[HS20]");
d85e1fc8 3881 if (os_snprintf_error(end - pos, ret))
ff486913 3882 return 0;
64855b96
JM
3883 pos += ret;
3884 }
3885#endif /* CONFIG_HS20 */
5f97dd1c
DS
3886
3887 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 3888 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
3889 return 0;
3890 pos += ret;
3891 }
3892
3893 if (mask & WPA_BSS_MASK_SSID) {
3894 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
3895 wpa_ssid_txt(bss->ssid, bss->ssid_len));
d85e1fc8 3896 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
3897 return 0;
3898 pos += ret;
3899 }
6fc6879b 3900
611ed491 3901#ifdef CONFIG_WPS
5f97dd1c
DS
3902 if (mask & WPA_BSS_MASK_WPS_SCAN) {
3903 ie = (const u8 *) (bss + 1);
3904 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
3905 if (ret < 0 || ret >= end - pos)
3906 return 0;
3907 pos += ret;
3908 }
611ed491
JM
3909#endif /* CONFIG_WPS */
3910
0c6b310e 3911#ifdef CONFIG_P2P
5f97dd1c
DS
3912 if (mask & WPA_BSS_MASK_P2P_SCAN) {
3913 ie = (const u8 *) (bss + 1);
3914 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3915 if (ret < 0 || ret >= end - pos)
3916 return 0;
3917 pos += ret;
3918 }
0c6b310e
JM
3919#endif /* CONFIG_P2P */
3920
337c781f
JM
3921#ifdef CONFIG_WIFI_DISPLAY
3922 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
3923 struct wpabuf *wfd;
3924 ie = (const u8 *) (bss + 1);
3925 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
3926 WFD_IE_VENDOR_TYPE);
3927 if (wfd) {
3928 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
d85e1fc8 3929 if (os_snprintf_error(end - pos, ret)) {
5e6aa04b 3930 wpabuf_free(wfd);
ff486913 3931 return 0;
5e6aa04b 3932 }
337c781f
JM
3933 pos += ret;
3934
3935 pos += wpa_snprintf_hex(pos, end - pos,
3936 wpabuf_head(wfd),
3937 wpabuf_len(wfd));
3938 wpabuf_free(wfd);
3939
3940 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 3941 if (os_snprintf_error(end - pos, ret))
ff486913 3942 return 0;
337c781f
JM
3943 pos += ret;
3944 }
3945 }
3946#endif /* CONFIG_WIFI_DISPLAY */
3947
afc064fe 3948#ifdef CONFIG_INTERWORKING
476aed35
JM
3949 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
3950 struct wpa_bss_anqp *anqp = bss->anqp;
5f97dd1c 3951 pos = anqp_add_hex(pos, end, "anqp_venue_name",
476aed35 3952 anqp->venue_name);
5f97dd1c 3953 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
476aed35 3954 anqp->network_auth_type);
5f97dd1c 3955 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
476aed35 3956 anqp->roaming_consortium);
5f97dd1c 3957 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
476aed35 3958 anqp->ip_addr_type_availability);
5f97dd1c 3959 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
476aed35
JM
3960 anqp->nai_realm);
3961 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
5f97dd1c 3962 pos = anqp_add_hex(pos, end, "anqp_domain_name",
476aed35 3963 anqp->domain_name);
25471fe3
JK
3964#ifdef CONFIG_HS20
3965 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
476aed35 3966 anqp->hs20_operator_friendly_name);
25471fe3 3967 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
476aed35 3968 anqp->hs20_wan_metrics);
25471fe3 3969 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
476aed35 3970 anqp->hs20_connection_capability);
1d2215fc
JM
3971 pos = anqp_add_hex(pos, end, "hs20_operating_class",
3972 anqp->hs20_operating_class);
3973 pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
3974 anqp->hs20_osu_providers_list);
25471fe3 3975#endif /* CONFIG_HS20 */
5f97dd1c 3976 }
afc064fe
JM
3977#endif /* CONFIG_INTERWORKING */
3978
79070906
MH
3979#ifdef CONFIG_MESH
3980 if (mask & WPA_BSS_MASK_MESH_SCAN) {
3981 ie = (const u8 *) (bss + 1);
3982 ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
3983 if (ret < 0 || ret >= end - pos)
3984 return 0;
3985 pos += ret;
3986 }
3987#endif /* CONFIG_MESH */
3988
c6673429
DS
3989 if (mask & WPA_BSS_MASK_DELIM) {
3990 ret = os_snprintf(pos, end - pos, "====\n");
d85e1fc8 3991 if (os_snprintf_error(end - pos, ret))
c6673429
DS
3992 return 0;
3993 pos += ret;
3994 }
3995
6fc6879b
JM
3996 return pos - buf;
3997}
3998
3999
61ce9085
DS
4000static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
4001 const char *cmd, char *buf,
4002 size_t buflen)
4003{
4004 u8 bssid[ETH_ALEN];
4005 size_t i;
4006 struct wpa_bss *bss;
eff1a95b
DS
4007 struct wpa_bss *bsslast = NULL;
4008 struct dl_list *next;
4009 int ret = 0;
4010 int len;
1d399771 4011 char *ctmp, *end = buf + buflen;
5f97dd1c 4012 unsigned long mask = WPA_BSS_MASK_ALL;
61ce9085 4013
eff1a95b
DS
4014 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
4015 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
4016 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
4017 list_id);
4018 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
4019 list_id);
4020 } else { /* N1-N2 */
4021 unsigned int id1, id2;
4022
4023 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
4024 wpa_printf(MSG_INFO, "Wrong BSS range "
4025 "format");
4026 return 0;
4027 }
4028
9f42d49c
AS
4029 if (*(cmd + 6) == '-')
4030 id1 = 0;
4031 else
4032 id1 = atoi(cmd + 6);
4033 ctmp++;
4034 if (*ctmp >= '0' && *ctmp <= '9')
4035 id2 = atoi(ctmp);
4036 else
4037 id2 = (unsigned int) -1;
4038 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
4039 if (id2 == (unsigned int) -1)
eff1a95b
DS
4040 bsslast = dl_list_last(&wpa_s->bss_id,
4041 struct wpa_bss,
4042 list_id);
4043 else {
4044 bsslast = wpa_bss_get_id(wpa_s, id2);
4045 if (bsslast == NULL && bss && id2 > id1) {
4046 struct wpa_bss *tmp = bss;
4047 for (;;) {
4048 next = tmp->list_id.next;
4049 if (next == &wpa_s->bss_id)
4050 break;
4051 tmp = dl_list_entry(
4052 next, struct wpa_bss,
4053 list_id);
4054 if (tmp->id > id2)
4055 break;
4056 bsslast = tmp;
4057 }
4058 }
4059 }
4060 }
f330b4b4 4061 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
51a0c3d4 4062 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
cc03d0fe
AS
4063 else if (os_strncmp(cmd, "LAST", 4) == 0)
4064 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
61ce9085
DS
4065 else if (os_strncmp(cmd, "ID-", 3) == 0) {
4066 i = atoi(cmd + 3);
4067 bss = wpa_bss_get_id(wpa_s, i);
4068 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4069 i = atoi(cmd + 5);
4070 bss = wpa_bss_get_id(wpa_s, i);
4071 if (bss) {
eff1a95b 4072 next = bss->list_id.next;
61ce9085
DS
4073 if (next == &wpa_s->bss_id)
4074 bss = NULL;
4075 else
4076 bss = dl_list_entry(next, struct wpa_bss,
4077 list_id);
4078 }
4079#ifdef CONFIG_P2P
4080 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
4081 if (hwaddr_aton(cmd + 13, bssid) == 0)
4082 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
4083 else
4084 bss = NULL;
4085#endif /* CONFIG_P2P */
4086 } else if (hwaddr_aton(cmd, bssid) == 0)
4087 bss = wpa_bss_get_bssid(wpa_s, bssid);
4088 else {
4089 struct wpa_bss *tmp;
4090 i = atoi(cmd);
4091 bss = NULL;
4092 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
4093 {
4094 if (i-- == 0) {
4095 bss = tmp;
4096 break;
4097 }
4098 }
4099 }
4100
5f97dd1c
DS
4101 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
4102 mask = strtoul(ctmp + 5, NULL, 0x10);
4103 if (mask == 0)
4104 mask = WPA_BSS_MASK_ALL;
4105 }
4106
61ce9085
DS
4107 if (bss == NULL)
4108 return 0;
4109
eff1a95b
DS
4110 if (bsslast == NULL)
4111 bsslast = bss;
4112 do {
4113 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
4114 ret += len;
4115 buf += len;
4116 buflen -= len;
cfd42c94
DS
4117 if (bss == bsslast) {
4118 if ((mask & WPA_BSS_MASK_DELIM) && len &&
4119 (bss == dl_list_last(&wpa_s->bss_id,
1d399771
JM
4120 struct wpa_bss, list_id))) {
4121 int res;
4122
4123 res = os_snprintf(buf - 5, end - buf + 5,
4124 "####\n");
4125 if (os_snprintf_error(end - buf + 5, res)) {
4126 wpa_printf(MSG_DEBUG,
4127 "Could not add end delim");
4128 }
4129 }
eff1a95b 4130 break;
cfd42c94 4131 }
eff1a95b
DS
4132 next = bss->list_id.next;
4133 if (next == &wpa_s->bss_id)
4134 break;
4135 bss = dl_list_entry(next, struct wpa_bss, list_id);
4136 } while (bss && len);
4137
4138 return ret;
61ce9085
DS
4139}
4140
4141
6fc6879b
JM
4142static int wpa_supplicant_ctrl_iface_ap_scan(
4143 struct wpa_supplicant *wpa_s, char *cmd)
4144{
4145 int ap_scan = atoi(cmd);
86b89452 4146 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
6fc6879b
JM
4147}
4148
4149
67b9bd08
DS
4150static int wpa_supplicant_ctrl_iface_scan_interval(
4151 struct wpa_supplicant *wpa_s, char *cmd)
4152{
4153 int scan_int = atoi(cmd);
c6e86b63 4154 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
67b9bd08
DS
4155}
4156
4157
78633c37
SL
4158static int wpa_supplicant_ctrl_iface_bss_expire_age(
4159 struct wpa_supplicant *wpa_s, char *cmd)
4160{
4161 int expire_age = atoi(cmd);
4162 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
4163}
4164
4165
4166static int wpa_supplicant_ctrl_iface_bss_expire_count(
4167 struct wpa_supplicant *wpa_s, char *cmd)
4168{
4169 int expire_count = atoi(cmd);
4170 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
4171}
4172
4173
a1144000 4174static void wpa_supplicant_ctrl_iface_bss_flush(
39ee845f
DS
4175 struct wpa_supplicant *wpa_s, char *cmd)
4176{
4177 int flush_age = atoi(cmd);
4178
4179 if (flush_age == 0)
4180 wpa_bss_flush(wpa_s);
4181 else
4182 wpa_bss_flush_by_age(wpa_s, flush_age);
39ee845f
DS
4183}
4184
4185
9ff4de6d 4186#ifdef CONFIG_TESTING_OPTIONS
32d5295f
JM
4187static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
4188{
32d5295f
JM
4189 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
4190 /* MLME-DELETEKEYS.request */
0382097e
JM
4191 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
4192 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
4193 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
4194 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
32d5295f 4195#ifdef CONFIG_IEEE80211W
0382097e
JM
4196 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
4197 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
32d5295f
JM
4198#endif /* CONFIG_IEEE80211W */
4199
4200 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
4201 0);
4202 /* MLME-SETPROTECTION.request(None) */
4203 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
4204 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
4205 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
4206 wpa_sm_drop_sa(wpa_s->wpa);
4207}
9ff4de6d 4208#endif /* CONFIG_TESTING_OPTIONS */
32d5295f
JM
4209
4210
86d4f806
JM
4211static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
4212 char *addr)
4213{
90b8fc8f
JM
4214#ifdef CONFIG_NO_SCAN_PROCESSING
4215 return -1;
4216#else /* CONFIG_NO_SCAN_PROCESSING */
86d4f806
JM
4217 u8 bssid[ETH_ALEN];
4218 struct wpa_bss *bss;
4219 struct wpa_ssid *ssid = wpa_s->current_ssid;
4220
4221 if (hwaddr_aton(addr, bssid)) {
4222 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
4223 "address '%s'", addr);
4224 return -1;
4225 }
4226
4227 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
4228
2f9b66d3
JM
4229 if (!ssid) {
4230 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
4231 "configuration known for the target AP");
4232 return -1;
4233 }
4234
4235 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
86d4f806
JM
4236 if (!bss) {
4237 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
4238 "from BSS table");
4239 return -1;
4240 }
4241
4242 /*
4243 * TODO: Find best network configuration block from configuration to
4244 * allow roaming to other networks
4245 */
4246
86d4f806
JM
4247 wpa_s->reassociate = 1;
4248 wpa_supplicant_connect(wpa_s, bss, ssid);
4249
4250 return 0;
90b8fc8f 4251#endif /* CONFIG_NO_SCAN_PROCESSING */
86d4f806
JM
4252}
4253
4254
b563b388
JM
4255#ifdef CONFIG_P2P
4256static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
4257{
4258 unsigned int timeout = atoi(cmd);
4259 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
6d92fa6e 4260 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
2b384109 4261 u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
6d92fa6e 4262 char *pos;
05a77b3b 4263 unsigned int search_delay;
b563b388 4264
e9eb648e
JM
4265 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4266 wpa_dbg(wpa_s, MSG_INFO,
4267 "Reject P2P_FIND since interface is disabled");
4268 return -1;
4269 }
b563b388
JM
4270 if (os_strstr(cmd, "type=social"))
4271 type = P2P_FIND_ONLY_SOCIAL;
4272 else if (os_strstr(cmd, "type=progressive"))
4273 type = P2P_FIND_PROGRESSIVE;
4274
6d92fa6e
JM
4275 pos = os_strstr(cmd, "dev_id=");
4276 if (pos) {
4277 pos += 7;
4278 if (hwaddr_aton(pos, dev_id))
4279 return -1;
4280 _dev_id = dev_id;
4281 }
4282
2b384109
JM
4283 pos = os_strstr(cmd, "dev_type=");
4284 if (pos) {
4285 pos += 9;
4286 if (wps_dev_type_str2bin(pos, dev_type) < 0)
4287 return -1;
4288 _dev_type = dev_type;
4289 }
4290
37448ede
JM
4291 pos = os_strstr(cmd, "delay=");
4292 if (pos) {
4293 pos += 6;
4294 search_delay = atoi(pos);
05a77b3b
JM
4295 } else
4296 search_delay = wpas_p2p_search_delay(wpa_s);
37448ede 4297
2b384109
JM
4298 return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
4299 _dev_id, search_delay);
b563b388
JM
4300}
4301
4302
4303static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
4304 char *buf, size_t buflen)
4305{
4306 u8 addr[ETH_ALEN];
4307 char *pos, *pos2;
4308 char *pin = NULL;
4309 enum p2p_wps_method wps_method;
4310 int new_pin;
4311 int ret;
23c84252 4312 int persistent_group, persistent_id = -1;
b563b388
JM
4313 int join;
4314 int auth;
b31be3a0 4315 int automatic;
b563b388
JM
4316 int go_intent = -1;
4317 int freq = 0;
3bc462cb 4318 int pd;
20ea1ca4 4319 int ht40, vht;
b563b388 4320
23c84252
JM
4321 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
4322 * [persistent|persistent=<network id>]
e2308e4b 4323 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
20ea1ca4 4324 * [ht40] [vht] */
b563b388
JM
4325
4326 if (hwaddr_aton(cmd, addr))
4327 return -1;
4328
4329 pos = cmd + 17;
4330 if (*pos != ' ')
4331 return -1;
4332 pos++;
4333
4334 persistent_group = os_strstr(pos, " persistent") != NULL;
23c84252
JM
4335 pos2 = os_strstr(pos, " persistent=");
4336 if (pos2) {
4337 struct wpa_ssid *ssid;
4338 persistent_id = atoi(pos2 + 12);
4339 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4340 if (ssid == NULL || ssid->disabled != 2 ||
4341 ssid->mode != WPAS_MODE_P2P_GO) {
4342 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
4343 "SSID id=%d for persistent P2P group (GO)",
4344 persistent_id);
4345 return -1;
4346 }
4347 }
b563b388
JM
4348 join = os_strstr(pos, " join") != NULL;
4349 auth = os_strstr(pos, " auth") != NULL;
b31be3a0 4350 automatic = os_strstr(pos, " auto") != NULL;
3bc462cb 4351 pd = os_strstr(pos, " provdisc") != NULL;
20ea1ca4
EP
4352 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4353 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4354 vht;
b563b388
JM
4355
4356 pos2 = os_strstr(pos, " go_intent=");
4357 if (pos2) {
4358 pos2 += 11;
4359 go_intent = atoi(pos2);
4360 if (go_intent < 0 || go_intent > 15)
4361 return -1;
4362 }
4363
4364 pos2 = os_strstr(pos, " freq=");
4365 if (pos2) {
4366 pos2 += 6;
4367 freq = atoi(pos2);
4368 if (freq <= 0)
4369 return -1;
4370 }
4371
4372 if (os_strncmp(pos, "pin", 3) == 0) {
4373 /* Request random PIN (to be displayed) and enable the PIN */
4374 wps_method = WPS_PIN_DISPLAY;
4375 } else if (os_strncmp(pos, "pbc", 3) == 0) {
4376 wps_method = WPS_PBC;
4377 } else {
4378 pin = pos;
4379 pos = os_strchr(pin, ' ');
4380 wps_method = WPS_PIN_KEYPAD;
4381 if (pos) {
4382 *pos++ = '\0';
07fecd39 4383 if (os_strncmp(pos, "display", 7) == 0)
b563b388
JM
4384 wps_method = WPS_PIN_DISPLAY;
4385 }
dcc33057 4386 if (!wps_pin_str_valid(pin)) {
36ebf7a1
MH
4387 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
4388 return 17;
4389 }
b563b388
JM
4390 }
4391
4392 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
b31be3a0 4393 persistent_group, automatic, join,
e2308e4b 4394 auth, go_intent, freq, persistent_id, pd,
20ea1ca4 4395 ht40, vht);
d054a462
JM
4396 if (new_pin == -2) {
4397 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
4398 return 25;
4399 }
4400 if (new_pin == -3) {
4401 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
4402 return 25;
4403 }
b563b388
JM
4404 if (new_pin < 0)
4405 return -1;
4406 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
4407 ret = os_snprintf(buf, buflen, "%08d", new_pin);
d85e1fc8 4408 if (os_snprintf_error(buflen, ret))
b563b388
JM
4409 return -1;
4410 return ret;
4411 }
4412
4413 os_memcpy(buf, "OK\n", 3);
4414 return 3;
4415}
4416
4417
4418static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
4419{
4420 unsigned int timeout = atoi(cmd);
e9eb648e
JM
4421 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4422 wpa_dbg(wpa_s, MSG_INFO,
4423 "Reject P2P_LISTEN since interface is disabled");
4424 return -1;
4425 }
b563b388
JM
4426 return wpas_p2p_listen(wpa_s, timeout);
4427}
4428
4429
4430static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
4431{
4432 u8 addr[ETH_ALEN];
4433 char *pos;
0918c4bf 4434 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
b563b388 4435
0918c4bf 4436 /* <addr> <config method> [join|auto] */
b563b388
JM
4437
4438 if (hwaddr_aton(cmd, addr))
4439 return -1;
4440
4441 pos = cmd + 17;
4442 if (*pos != ' ')
4443 return -1;
4444 pos++;
4445
0918c4bf
JM
4446 if (os_strstr(pos, " join") != NULL)
4447 use = WPAS_P2P_PD_FOR_JOIN;
4448 else if (os_strstr(pos, " auto") != NULL)
4449 use = WPAS_P2P_PD_AUTO;
4450
4451 return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
b563b388
JM
4452}
4453
4454
4455static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
4456 size_t buflen)
4457{
4458 struct wpa_ssid *ssid = wpa_s->current_ssid;
4459
4460 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
4461 ssid->passphrase == NULL)
4462 return -1;
4463
4464 os_strlcpy(buf, ssid->passphrase, buflen);
4465 return os_strlen(buf);
4466}
4467
4468
4469static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
4470 char *buf, size_t buflen)
4471{
4472 u64 ref;
4473 int res;
4474 u8 dst_buf[ETH_ALEN], *dst;
4475 struct wpabuf *tlvs;
4476 char *pos;
4477 size_t len;
4478
4479 if (hwaddr_aton(cmd, dst_buf))
4480 return -1;
4481 dst = dst_buf;
4482 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
4483 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
4484 dst = NULL;
4485 pos = cmd + 17;
4486 if (*pos != ' ')
4487 return -1;
4488 pos++;
4489
4490 if (os_strncmp(pos, "upnp ", 5) == 0) {
4491 u8 version;
4492 pos += 5;
4493 if (hexstr2bin(pos, &version, 1) < 0)
4494 return -1;
4495 pos += 2;
4496 if (*pos != ' ')
4497 return -1;
4498 pos++;
7165c5dc 4499 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
347d6a5b
JM
4500#ifdef CONFIG_WIFI_DISPLAY
4501 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
4502 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
4503#endif /* CONFIG_WIFI_DISPLAY */
b563b388
JM
4504 } else {
4505 len = os_strlen(pos);
4506 if (len & 1)
4507 return -1;
4508 len /= 2;
4509 tlvs = wpabuf_alloc(len);
4510 if (tlvs == NULL)
4511 return -1;
4512 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
4513 wpabuf_free(tlvs);
4514 return -1;
4515 }
4516
7165c5dc 4517 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
b563b388
JM
4518 wpabuf_free(tlvs);
4519 }
7165c5dc
JM
4520 if (ref == 0)
4521 return -1;
b563b388 4522 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
d85e1fc8 4523 if (os_snprintf_error(buflen, res))
b563b388
JM
4524 return -1;
4525 return res;
4526}
4527
4528
4529static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
4530 char *cmd)
4531{
4532 long long unsigned val;
4533 u64 req;
4534 if (sscanf(cmd, "%llx", &val) != 1)
4535 return -1;
4536 req = val;
7165c5dc 4537 return wpas_p2p_sd_cancel_request(wpa_s, req);
b563b388
JM
4538}
4539
4540
4541static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
4542{
4543 int freq;
d25f7212 4544 u8 dst[ETH_ALEN];
b563b388
JM
4545 u8 dialog_token;
4546 struct wpabuf *resp_tlvs;
4547 char *pos, *pos2;
4548 size_t len;
4549
4550 pos = os_strchr(cmd, ' ');
4551 if (pos == NULL)
4552 return -1;
4553 *pos++ = '\0';
4554 freq = atoi(cmd);
4555 if (freq == 0)
4556 return -1;
4557
d25f7212 4558 if (hwaddr_aton(pos, dst))
b563b388 4559 return -1;
b563b388
JM
4560 pos += 17;
4561 if (*pos != ' ')
4562 return -1;
4563 pos++;
4564
4565 pos2 = os_strchr(pos, ' ');
4566 if (pos2 == NULL)
4567 return -1;
4568 *pos2++ = '\0';
4569 dialog_token = atoi(pos);
4570
4571 len = os_strlen(pos2);
4572 if (len & 1)
4573 return -1;
4574 len /= 2;
4575 resp_tlvs = wpabuf_alloc(len);
4576 if (resp_tlvs == NULL)
4577 return -1;
4578 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
4579 wpabuf_free(resp_tlvs);
4580 return -1;
4581 }
4582
4583 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
4584 wpabuf_free(resp_tlvs);
4585 return 0;
4586}
4587
4588
4589static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
4590 char *cmd)
4591{
28ef705d
GB
4592 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
4593 return -1;
b563b388
JM
4594 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
4595 return 0;
4596}
4597
4598
4599static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
4600 char *cmd)
4601{
4602 char *pos;
4603 size_t len;
4604 struct wpabuf *query, *resp;
4605
4606 pos = os_strchr(cmd, ' ');
4607 if (pos == NULL)
4608 return -1;
4609 *pos++ = '\0';
4610
4611 len = os_strlen(cmd);
4612 if (len & 1)
4613 return -1;
4614 len /= 2;
4615 query = wpabuf_alloc(len);
4616 if (query == NULL)
4617 return -1;
4618 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4619 wpabuf_free(query);
4620 return -1;
4621 }
4622
4623 len = os_strlen(pos);
4624 if (len & 1) {
4625 wpabuf_free(query);
4626 return -1;
4627 }
4628 len /= 2;
4629 resp = wpabuf_alloc(len);
4630 if (resp == NULL) {
4631 wpabuf_free(query);
4632 return -1;
4633 }
4634 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
4635 wpabuf_free(query);
4636 wpabuf_free(resp);
4637 return -1;
4638 }
4639
4640 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
4641 wpabuf_free(query);
4642 wpabuf_free(resp);
4643 return -1;
4644 }
4645 return 0;
4646}
4647
4648
4649static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4650{
4651 char *pos;
4652 u8 version;
4653
4654 pos = os_strchr(cmd, ' ');
4655 if (pos == NULL)
4656 return -1;
4657 *pos++ = '\0';
4658
4659 if (hexstr2bin(cmd, &version, 1) < 0)
4660 return -1;
4661
4662 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
4663}
4664
4665
4666static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
4667{
4668 char *pos;
4669
4670 pos = os_strchr(cmd, ' ');
4671 if (pos == NULL)
4672 return -1;
4673 *pos++ = '\0';
4674
4675 if (os_strcmp(cmd, "bonjour") == 0)
4676 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
4677 if (os_strcmp(cmd, "upnp") == 0)
4678 return p2p_ctrl_service_add_upnp(wpa_s, pos);
4679 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4680 return -1;
4681}
4682
4683
4684static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
4685 char *cmd)
4686{
4687 size_t len;
4688 struct wpabuf *query;
4689 int ret;
4690
4691 len = os_strlen(cmd);
4692 if (len & 1)
4693 return -1;
4694 len /= 2;
4695 query = wpabuf_alloc(len);
4696 if (query == NULL)
4697 return -1;
4698 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
4699 wpabuf_free(query);
4700 return -1;
4701 }
4702
4703 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
4704 wpabuf_free(query);
4705 return ret;
4706}
4707
4708
4709static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
4710{
4711 char *pos;
4712 u8 version;
4713
4714 pos = os_strchr(cmd, ' ');
4715 if (pos == NULL)
4716 return -1;
4717 *pos++ = '\0';
4718
4719 if (hexstr2bin(cmd, &version, 1) < 0)
4720 return -1;
4721
4722 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
4723}
4724
4725
4726static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
4727{
4728 char *pos;
4729
4730 pos = os_strchr(cmd, ' ');
4731 if (pos == NULL)
4732 return -1;
4733 *pos++ = '\0';
4734
4735 if (os_strcmp(cmd, "bonjour") == 0)
4736 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
4737 if (os_strcmp(cmd, "upnp") == 0)
4738 return p2p_ctrl_service_del_upnp(wpa_s, pos);
4739 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
4740 return -1;
4741}
4742
4743
4744static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
4745{
4746 u8 addr[ETH_ALEN];
4747
4748 /* <addr> */
4749
4750 if (hwaddr_aton(cmd, addr))
4751 return -1;
4752
4753 return wpas_p2p_reject(wpa_s, addr);
4754}
4755
4756
4757static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
4758{
4759 char *pos;
4760 int id;
4761 struct wpa_ssid *ssid;
54c61e6e 4762 u8 *_peer = NULL, peer[ETH_ALEN];
f5877af0 4763 int freq = 0, pref_freq = 0;
20ea1ca4 4764 int ht40, vht;
b563b388
JM
4765
4766 id = atoi(cmd);
4767 pos = os_strstr(cmd, " peer=");
4768 if (pos) {
4769 pos += 6;
4770 if (hwaddr_aton(pos, peer))
4771 return -1;
54c61e6e 4772 _peer = peer;
b563b388
JM
4773 }
4774 ssid = wpa_config_get_network(wpa_s->conf, id);
4775 if (ssid == NULL || ssid->disabled != 2) {
4776 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4777 "for persistent P2P group",
4778 id);
4779 return -1;
4780 }
4781
4d32c0c4
JM
4782 pos = os_strstr(cmd, " freq=");
4783 if (pos) {
4784 pos += 6;
4785 freq = atoi(pos);
4786 if (freq <= 0)
4787 return -1;
4788 }
4789
f5877af0
JM
4790 pos = os_strstr(cmd, " pref=");
4791 if (pos) {
4792 pos += 6;
4793 pref_freq = atoi(pos);
4794 if (pref_freq <= 0)
4795 return -1;
4796 }
4797
20ea1ca4
EP
4798 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
4799 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4800 vht;
4d32c0c4 4801
20ea1ca4
EP
4802 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
4803 pref_freq);
b563b388
JM
4804}
4805
4806
4807static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
4808{
4809 char *pos;
4810 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
4811
4812 pos = os_strstr(cmd, " peer=");
4813 if (!pos)
4814 return -1;
4815
4816 *pos = '\0';
4817 pos += 6;
4818 if (hwaddr_aton(pos, peer)) {
4819 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
4820 return -1;
4821 }
4822
4823 pos = os_strstr(pos, " go_dev_addr=");
4824 if (pos) {
4825 pos += 13;
4826 if (hwaddr_aton(pos, go_dev_addr)) {
4827 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
4828 pos);
4829 return -1;
4830 }
4831 go_dev = go_dev_addr;
4832 }
4833
4834 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
4835}
4836
4837
4838static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
4839{
4840 if (os_strncmp(cmd, "persistent=", 11) == 0)
4841 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
4842 if (os_strncmp(cmd, "group=", 6) == 0)
4843 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
4844
4845 return -1;
4846}
4847
4848
4849static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
20ea1ca4
EP
4850 char *cmd, int freq, int ht40,
4851 int vht)
b563b388
JM
4852{
4853 int id;
4854 struct wpa_ssid *ssid;
4855
4856 id = atoi(cmd);
4857 ssid = wpa_config_get_network(wpa_s->conf, id);
4858 if (ssid == NULL || ssid->disabled != 2) {
4859 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
4860 "for persistent P2P group",
4861 id);
4862 return -1;
4863 }
4864
062a7c0d 4865 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
20ea1ca4 4866 NULL, 0);
b563b388
JM
4867}
4868
4869
4870static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
4871{
20ea1ca4 4872 int freq = 0, ht40, vht;
b563b388
JM
4873 char *pos;
4874
4875 pos = os_strstr(cmd, "freq=");
4876 if (pos)
4877 freq = atoi(pos + 5);
4878
20ea1ca4
EP
4879 vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
4880 ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
4881 vht;
7aeac985 4882
b563b388 4883 if (os_strncmp(cmd, "persistent=", 11) == 0)
7aeac985 4884 return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
20ea1ca4 4885 ht40, vht);
b563b388
JM
4886 if (os_strcmp(cmd, "persistent") == 0 ||
4887 os_strncmp(cmd, "persistent ", 11) == 0)
20ea1ca4 4888 return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
b563b388 4889 if (os_strncmp(cmd, "freq=", 5) == 0)
20ea1ca4 4890 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
7aeac985 4891 if (ht40)
20ea1ca4 4892 return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
b563b388
JM
4893
4894 wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
4895 cmd);
4896 return -1;
4897}
4898
4899
4900static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
4901 char *buf, size_t buflen)
4902{
4903 u8 addr[ETH_ALEN], *addr_ptr;
b3ffc80b
JM
4904 int next, res;
4905 const struct p2p_peer_info *info;
4906 char *pos, *end;
4907 char devtype[WPS_DEV_TYPE_BUFSIZE];
87f841a1 4908 struct wpa_ssid *ssid;
f3989ced 4909 size_t i;
b563b388
JM
4910
4911 if (!wpa_s->global->p2p)
4912 return -1;
4913
4914 if (os_strcmp(cmd, "FIRST") == 0) {
4915 addr_ptr = NULL;
4916 next = 0;
4917 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4918 if (hwaddr_aton(cmd + 5, addr) < 0)
4919 return -1;
4920 addr_ptr = addr;
4921 next = 1;
4922 } else {
4923 if (hwaddr_aton(cmd, addr) < 0)
4924 return -1;
4925 addr_ptr = addr;
4926 next = 0;
4927 }
4928
b3ffc80b
JM
4929 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
4930 if (info == NULL)
4931 return -1;
4932
4933 pos = buf;
4934 end = buf + buflen;
4935
4936 res = os_snprintf(pos, end - pos, MACSTR "\n"
4937 "pri_dev_type=%s\n"
4938 "device_name=%s\n"
4939 "manufacturer=%s\n"
4940 "model_name=%s\n"
4941 "model_number=%s\n"
4942 "serial_number=%s\n"
4943 "config_methods=0x%x\n"
4944 "dev_capab=0x%x\n"
4945 "group_capab=0x%x\n"
4946 "level=%d\n",
4947 MAC2STR(info->p2p_device_addr),
4948 wps_dev_type_bin2str(info->pri_dev_type,
4949 devtype, sizeof(devtype)),
4950 info->device_name,
4951 info->manufacturer,
4952 info->model_name,
4953 info->model_number,
4954 info->serial_number,
4955 info->config_methods,
4956 info->dev_capab,
4957 info->group_capab,
4958 info->level);
d85e1fc8 4959 if (os_snprintf_error(end - pos, res))
b3ffc80b
JM
4960 return pos - buf;
4961 pos += res;
4962
f3989ced
JM
4963 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
4964 {
4965 const u8 *t;
4966 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
4967 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
4968 wps_dev_type_bin2str(t, devtype,
4969 sizeof(devtype)));
d85e1fc8 4970 if (os_snprintf_error(end - pos, res))
f3989ced
JM
4971 return pos - buf;
4972 pos += res;
4973 }
4974
c427ac92 4975 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
87f841a1
JM
4976 if (ssid) {
4977 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
d85e1fc8 4978 if (os_snprintf_error(end - pos, res))
87f841a1
JM
4979 return pos - buf;
4980 pos += res;
4981 }
4982
b3ffc80b
JM
4983 res = p2p_get_peer_info_txt(info, pos, end - pos);
4984 if (res < 0)
87f841a1 4985 return pos - buf;
b3ffc80b
JM
4986 pos += res;
4987
71a0e395
JM
4988 if (info->vendor_elems) {
4989 res = os_snprintf(pos, end - pos, "vendor_elems=");
d85e1fc8 4990 if (os_snprintf_error(end - pos, res))
71a0e395
JM
4991 return pos - buf;
4992 pos += res;
4993
4994 pos += wpa_snprintf_hex(pos, end - pos,
4995 wpabuf_head(info->vendor_elems),
4996 wpabuf_len(info->vendor_elems));
4997
4998 res = os_snprintf(pos, end - pos, "\n");
d85e1fc8 4999 if (os_snprintf_error(end - pos, res))
71a0e395
JM
5000 return pos - buf;
5001 pos += res;
5002 }
5003
b3ffc80b 5004 return pos - buf;
b563b388
JM
5005}
5006
5007
6f3bc72b
JM
5008static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
5009 const char *param)
5010{
af8a827b 5011 unsigned int i;
6f3bc72b
JM
5012
5013 if (wpa_s->global->p2p == NULL)
5014 return -1;
5015
af8a827b
JM
5016 if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
5017 return -1;
6f3bc72b 5018
af8a827b
JM
5019 for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
5020 struct wpa_freq_range *freq;
5021 freq = &wpa_s->global->p2p_disallow_freq.range[i];
6f3bc72b 5022 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
af8a827b 5023 freq->min, freq->max);
6f3bc72b
JM
5024 }
5025
6f3bc72b
JM
5026 wpas_p2p_update_channel_list(wpa_s);
5027 return 0;
5028}
5029
5030
b563b388
JM
5031static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
5032{
5033 char *param;
5034
5035 if (wpa_s->global->p2p == NULL)
5036 return -1;
5037
5038 param = os_strchr(cmd, ' ');
5039 if (param == NULL)
5040 return -1;
5041 *param++ = '\0';
5042
5043 if (os_strcmp(cmd, "discoverability") == 0) {
5044 p2p_set_client_discoverability(wpa_s->global->p2p,
5045 atoi(param));
5046 return 0;
5047 }
5048
5049 if (os_strcmp(cmd, "managed") == 0) {
5050 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
5051 return 0;
5052 }
5053
5054 if (os_strcmp(cmd, "listen_channel") == 0) {
5055 return p2p_set_listen_channel(wpa_s->global->p2p, 81,
e3bd6e9d 5056 atoi(param), 1);
b563b388
JM
5057 }
5058
5059 if (os_strcmp(cmd, "ssid_postfix") == 0) {
5060 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
5061 os_strlen(param));
5062 }
5063
5064 if (os_strcmp(cmd, "noa") == 0) {
5065 char *pos;
5066 int count, start, duration;
5067 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
5068 count = atoi(param);
5069 pos = os_strchr(param, ',');
5070 if (pos == NULL)
5071 return -1;
5072 pos++;
5073 start = atoi(pos);
5074 pos = os_strchr(pos, ',');
5075 if (pos == NULL)
5076 return -1;
5077 pos++;
5078 duration = atoi(pos);
5079 if (count < 0 || count > 255 || start < 0 || duration < 0)
5080 return -1;
5081 if (count == 0 && duration > 0)
5082 return -1;
5083 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
5084 "start=%d duration=%d", count, start, duration);
aefb53bd 5085 return wpas_p2p_set_noa(wpa_s, count, start, duration);
b563b388
JM
5086 }
5087
c381508d
JM
5088 if (os_strcmp(cmd, "ps") == 0)
5089 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
5090
5091 if (os_strcmp(cmd, "oppps") == 0)
5092 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
5093
5094 if (os_strcmp(cmd, "ctwindow") == 0)
5095 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
5096
b563b388
JM
5097 if (os_strcmp(cmd, "disabled") == 0) {
5098 wpa_s->global->p2p_disabled = atoi(param);
5099 wpa_printf(MSG_DEBUG, "P2P functionality %s",
5100 wpa_s->global->p2p_disabled ?
5101 "disabled" : "enabled");
5102 if (wpa_s->global->p2p_disabled) {
5103 wpas_p2p_stop_find(wpa_s);
108def93 5104 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
b563b388
JM
5105 p2p_flush(wpa_s->global->p2p);
5106 }
5107 return 0;
5108 }
5109
b9cfc09a
JJ
5110 if (os_strcmp(cmd, "conc_pref") == 0) {
5111 if (os_strcmp(param, "sta") == 0)
5112 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
5113 else if (os_strcmp(param, "p2p") == 0)
5114 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
5115 else {
5116 wpa_printf(MSG_INFO, "Invalid conc_pref value");
5117 return -1;
5118 }
5119 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
5120 "%s", param);
5121 return 0;
5122 }
5123
6e6963ea
JM
5124 if (os_strcmp(cmd, "force_long_sd") == 0) {
5125 wpa_s->force_long_sd = atoi(param);
5126 return 0;
5127 }
5128
80c9582a
JM
5129 if (os_strcmp(cmd, "peer_filter") == 0) {
5130 u8 addr[ETH_ALEN];
5131 if (hwaddr_aton(param, addr))
5132 return -1;
5133 p2p_set_peer_filter(wpa_s->global->p2p, addr);
5134 return 0;
5135 }
5136
72044390
JM
5137 if (os_strcmp(cmd, "cross_connect") == 0)
5138 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
5139
eea2fd9e
JM
5140 if (os_strcmp(cmd, "go_apsd") == 0) {
5141 if (os_strcmp(param, "disable") == 0)
5142 wpa_s->set_ap_uapsd = 0;
5143 else {
5144 wpa_s->set_ap_uapsd = 1;
5145 wpa_s->ap_uapsd = atoi(param);
5146 }
5147 return 0;
5148 }
5149
5150 if (os_strcmp(cmd, "client_apsd") == 0) {
5151 if (os_strcmp(param, "disable") == 0)
5152 wpa_s->set_sta_uapsd = 0;
5153 else {
5154 int be, bk, vi, vo;
5155 char *pos;
5156 /* format: BE,BK,VI,VO;max SP Length */
5157 be = atoi(param);
5158 pos = os_strchr(param, ',');
5159 if (pos == NULL)
5160 return -1;
5161 pos++;
5162 bk = atoi(pos);
5163 pos = os_strchr(pos, ',');
5164 if (pos == NULL)
5165 return -1;
5166 pos++;
5167 vi = atoi(pos);
5168 pos = os_strchr(pos, ',');
5169 if (pos == NULL)
5170 return -1;
5171 pos++;
5172 vo = atoi(pos);
5173 /* ignore max SP Length for now */
5174
5175 wpa_s->set_sta_uapsd = 1;
5176 wpa_s->sta_uapsd = 0;
5177 if (be)
5178 wpa_s->sta_uapsd |= BIT(0);
5179 if (bk)
5180 wpa_s->sta_uapsd |= BIT(1);
5181 if (vi)
5182 wpa_s->sta_uapsd |= BIT(2);
5183 if (vo)
5184 wpa_s->sta_uapsd |= BIT(3);
5185 }
5186 return 0;
5187 }
5188
6f3bc72b
JM
5189 if (os_strcmp(cmd, "disallow_freq") == 0)
5190 return p2p_ctrl_disallow_freq(wpa_s, param);
5191
96beff11
JM
5192 if (os_strcmp(cmd, "disc_int") == 0) {
5193 int min_disc_int, max_disc_int, max_disc_tu;
5194 char *pos;
5195
5196 pos = param;
5197
5198 min_disc_int = atoi(pos);
5199 pos = os_strchr(pos, ' ');
5200 if (pos == NULL)
5201 return -1;
5202 *pos++ = '\0';
5203
5204 max_disc_int = atoi(pos);
5205 pos = os_strchr(pos, ' ');
5206 if (pos == NULL)
5207 return -1;
5208 *pos++ = '\0';
5209
5210 max_disc_tu = atoi(pos);
5211
5212 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
5213 max_disc_int, max_disc_tu);
5214 }
5215
05766ed8
JM
5216 if (os_strcmp(cmd, "per_sta_psk") == 0) {
5217 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
5218 return 0;
5219 }
5220
c4f87a70
JM
5221#ifdef CONFIG_WPS_NFC
5222 if (os_strcmp(cmd, "nfc_tag") == 0)
5223 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
5224#endif /* CONFIG_WPS_NFC */
5225
201b0f5f
JM
5226 if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
5227 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
5228 return 0;
5229 }
5230
b563b388
JM
5231 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
5232 cmd);
5233
5234 return -1;
5235}
5236
5237
acb54643
JM
5238static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
5239{
5240 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5241 wpa_s->force_long_sd = 0;
477b082c 5242 wpas_p2p_stop_find(wpa_s);
acb54643
JM
5243 if (wpa_s->global->p2p)
5244 p2p_flush(wpa_s->global->p2p);
5245}
5246
5247
b563b388
JM
5248static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
5249{
5250 char *pos, *pos2;
5251 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
5252
5253 if (cmd[0]) {
5254 pos = os_strchr(cmd, ' ');
5255 if (pos == NULL)
5256 return -1;
5257 *pos++ = '\0';
5258 dur1 = atoi(cmd);
5259
5260 pos2 = os_strchr(pos, ' ');
5261 if (pos2)
5262 *pos2++ = '\0';
5263 int1 = atoi(pos);
5264 } else
5265 pos2 = NULL;
5266
5267 if (pos2) {
5268 pos = os_strchr(pos2, ' ');
5269 if (pos == NULL)
5270 return -1;
5271 *pos++ = '\0';
5272 dur2 = atoi(pos2);
5273 int2 = atoi(pos);
5274 }
5275
5276 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
5277}
5278
5279
5280static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
5281{
5282 char *pos;
5283 unsigned int period = 0, interval = 0;
5284
5285 if (cmd[0]) {
5286 pos = os_strchr(cmd, ' ');
5287 if (pos == NULL)
5288 return -1;
5289 *pos++ = '\0';
5290 period = atoi(cmd);
5291 interval = atoi(pos);
5292 }
5293
5294 return wpas_p2p_ext_listen(wpa_s, period, interval);
5295}
5296
f2c56602
JM
5297
5298static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
5299{
5300 const char *pos;
5301 u8 peer[ETH_ALEN];
5302 int iface_addr = 0;
5303
5304 pos = cmd;
5305 if (os_strncmp(pos, "iface=", 6) == 0) {
5306 iface_addr = 1;
5307 pos += 6;
5308 }
5309 if (hwaddr_aton(pos, peer))
5310 return -1;
5311
5312 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
5313 return 0;
5314}
5315
b563b388
JM
5316#endif /* CONFIG_P2P */
5317
5318
356d1488
JM
5319static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
5320{
5321 struct wpa_freq_range_list ranges;
5322 int *freqs = NULL;
5323 struct hostapd_hw_modes *mode;
5324 u16 i;
5325
5326 if (wpa_s->hw.modes == NULL)
5327 return NULL;
5328
5329 os_memset(&ranges, 0, sizeof(ranges));
5330 if (freq_range_list_parse(&ranges, val) < 0)
5331 return NULL;
5332
5333 for (i = 0; i < wpa_s->hw.num_modes; i++) {
5334 int j;
5335
5336 mode = &wpa_s->hw.modes[i];
5337 for (j = 0; j < mode->num_channels; j++) {
5338 unsigned int freq;
5339
5340 if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
5341 continue;
5342
5343 freq = mode->channels[j].freq;
5344 if (!freq_range_list_includes(&ranges, freq))
5345 continue;
5346
5347 int_array_add_unique(&freqs, freq);
5348 }
5349 }
5350
5351 os_free(ranges.range);
5352 return freqs;
5353}
5354
5355
afc064fe 5356#ifdef CONFIG_INTERWORKING
356d1488
JM
5357
5358static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
5359{
5360 int auto_sel = 0;
5361 int *freqs = NULL;
5362
5363 if (param) {
5364 char *pos;
5365
5366 auto_sel = os_strstr(param, "auto") != NULL;
5367
5368 pos = os_strstr(param, "freq=");
5369 if (pos) {
5370 freqs = freq_range_to_channel_list(wpa_s, pos + 5);
5371 if (freqs == NULL)
5372 return -1;
5373 }
5374
5375 }
5376
5377 return interworking_select(wpa_s, auto_sel, freqs);
5378}
5379
5380
b02fe7ff
JM
5381static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
5382{
5383 u8 bssid[ETH_ALEN];
5384 struct wpa_bss *bss;
5385
5386 if (hwaddr_aton(dst, bssid)) {
5387 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
5388 return -1;
5389 }
5390
5391 bss = wpa_bss_get_bssid(wpa_s, bssid);
5392 if (bss == NULL) {
5393 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
5394 MAC2STR(bssid));
5395 return -1;
5396 }
5397
5398 return interworking_connect(wpa_s, bss);
5399}
5400
5401
afc064fe
JM
5402static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
5403{
5404 u8 dst_addr[ETH_ALEN];
5405 int used;
5406 char *pos;
5407#define MAX_ANQP_INFO_ID 100
5408 u16 id[MAX_ANQP_INFO_ID];
5409 size_t num_id = 0;
cf28c66b 5410 u32 subtypes = 0;
afc064fe
JM
5411
5412 used = hwaddr_aton2(dst, dst_addr);
5413 if (used < 0)
5414 return -1;
5415 pos = dst + used;
b68d602d
JM
5416 if (*pos == ' ')
5417 pos++;
afc064fe 5418 while (num_id < MAX_ANQP_INFO_ID) {
cf28c66b
DS
5419 if (os_strncmp(pos, "hs20:", 5) == 0) {
5420#ifdef CONFIG_HS20
5421 int num = atoi(pos + 5);
5422 if (num <= 0 || num > 31)
5423 return -1;
5424 subtypes |= BIT(num);
5425#else /* CONFIG_HS20 */
5426 return -1;
5427#endif /* CONFIG_HS20 */
5428 } else {
5429 id[num_id] = atoi(pos);
5430 if (id[num_id])
5431 num_id++;
5432 }
afc064fe
JM
5433 pos = os_strchr(pos + 1, ',');
5434 if (pos == NULL)
5435 break;
5436 pos++;
5437 }
5438
5439 if (num_id == 0)
5440 return -1;
5441
cf28c66b 5442 return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
afc064fe 5443}
b1f12296
JM
5444
5445
5446static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
5447{
5448 u8 dst_addr[ETH_ALEN];
5449 struct wpabuf *advproto, *query = NULL;
5450 int used, ret = -1;
5451 char *pos, *end;
5452 size_t len;
5453
5454 used = hwaddr_aton2(cmd, dst_addr);
5455 if (used < 0)
5456 return -1;
5457
5458 pos = cmd + used;
5459 while (*pos == ' ')
5460 pos++;
5461
5462 /* Advertisement Protocol ID */
5463 end = os_strchr(pos, ' ');
5464 if (end)
5465 len = end - pos;
5466 else
5467 len = os_strlen(pos);
5468 if (len & 0x01)
5469 return -1;
5470 len /= 2;
5471 if (len == 0)
5472 return -1;
5473 advproto = wpabuf_alloc(len);
5474 if (advproto == NULL)
5475 return -1;
5476 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
5477 goto fail;
5478
5479 if (end) {
5480 /* Optional Query Request */
5481 pos = end + 1;
5482 while (*pos == ' ')
5483 pos++;
5484
5485 len = os_strlen(pos);
5486 if (len) {
5487 if (len & 0x01)
5488 goto fail;
5489 len /= 2;
5490 if (len == 0)
5491 goto fail;
5492 query = wpabuf_alloc(len);
5493 if (query == NULL)
5494 goto fail;
5495 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
5496 goto fail;
5497 }
5498 }
5499
5500 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
5501
5502fail:
5503 wpabuf_free(advproto);
5504 wpabuf_free(query);
5505
5506 return ret;
5507}
5508
5509
5510static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
5511 size_t buflen)
5512{
5513 u8 addr[ETH_ALEN];
5514 int dialog_token;
5515 int used;
5516 char *pos;
5517 size_t resp_len, start, requested_len;
b6a9590b
JM
5518 struct wpabuf *resp;
5519 int ret;
b1f12296
JM
5520
5521 used = hwaddr_aton2(cmd, addr);
5522 if (used < 0)
5523 return -1;
5524
5525 pos = cmd + used;
5526 while (*pos == ' ')
5527 pos++;
5528 dialog_token = atoi(pos);
5529
b6a9590b
JM
5530 if (wpa_s->last_gas_resp &&
5531 os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
5532 dialog_token == wpa_s->last_gas_dialog_token)
5533 resp = wpa_s->last_gas_resp;
5534 else if (wpa_s->prev_gas_resp &&
5535 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
5536 dialog_token == wpa_s->prev_gas_dialog_token)
5537 resp = wpa_s->prev_gas_resp;
5538 else
b1f12296
JM
5539 return -1;
5540
b6a9590b 5541 resp_len = wpabuf_len(resp);
b1f12296
JM
5542 start = 0;
5543 requested_len = resp_len;
5544
5545 pos = os_strchr(pos, ' ');
5546 if (pos) {
5547 start = atoi(pos);
5548 if (start > resp_len)
5549 return os_snprintf(buf, buflen, "FAIL-Invalid range");
5550 pos = os_strchr(pos, ',');
5551 if (pos == NULL)
5552 return -1;
5553 pos++;
5554 requested_len = atoi(pos);
5555 if (start + requested_len > resp_len)
5556 return os_snprintf(buf, buflen, "FAIL-Invalid range");
5557 }
5558
5559 if (requested_len * 2 + 1 > buflen)
5560 return os_snprintf(buf, buflen, "FAIL-Too long response");
5561
b6a9590b
JM
5562 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
5563 requested_len);
5564
5565 if (start + requested_len == resp_len) {
5566 /*
5567 * Free memory by dropping the response after it has been
5568 * fetched.
5569 */
5570 if (resp == wpa_s->prev_gas_resp) {
5571 wpabuf_free(wpa_s->prev_gas_resp);
5572 wpa_s->prev_gas_resp = NULL;
5573 } else {
5574 wpabuf_free(wpa_s->last_gas_resp);
5575 wpa_s->last_gas_resp = NULL;
5576 }
5577 }
5578
5579 return ret;
b1f12296 5580}
afc064fe
JM
5581#endif /* CONFIG_INTERWORKING */
5582
5583
a8918e86
JK
5584#ifdef CONFIG_HS20
5585
5586static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
5587{
5588 u8 dst_addr[ETH_ALEN];
5589 int used;
5590 char *pos;
5591 u32 subtypes = 0;
5592
5593 used = hwaddr_aton2(dst, dst_addr);
5594 if (used < 0)
5595 return -1;
5596 pos = dst + used;
b68d602d
JM
5597 if (*pos == ' ')
5598 pos++;
a8918e86
JK
5599 for (;;) {
5600 int num = atoi(pos);
5601 if (num <= 0 || num > 31)
5602 return -1;
5603 subtypes |= BIT(num);
5604 pos = os_strchr(pos + 1, ',');
5605 if (pos == NULL)
5606 break;
5607 pos++;
5608 }
5609
5610 if (subtypes == 0)
5611 return -1;
5612
5613 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
5614}
5615
5616
5617static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5618 const u8 *addr, const char *realm)
5619{
5620 u8 *buf;
5621 size_t rlen, len;
5622 int ret;
5623
5624 rlen = os_strlen(realm);
5625 len = 3 + rlen;
5626 buf = os_malloc(len);
5627 if (buf == NULL)
5628 return -1;
5629 buf[0] = 1; /* NAI Home Realm Count */
5630 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
5631 buf[2] = rlen;
5632 os_memcpy(buf + 3, realm, rlen);
5633
5634 ret = hs20_anqp_send_req(wpa_s, addr,
5635 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5636 buf, len);
5637
5638 os_free(buf);
5639
5640 return ret;
5641}
5642
5643
5644static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
5645 char *dst)
5646{
5647 struct wpa_cred *cred = wpa_s->conf->cred;
5648 u8 dst_addr[ETH_ALEN];
5649 int used;
5650 u8 *buf;
5651 size_t len;
5652 int ret;
5653
5654 used = hwaddr_aton2(dst, dst_addr);
5655 if (used < 0)
5656 return -1;
5657
5658 while (dst[used] == ' ')
5659 used++;
5660 if (os_strncmp(dst + used, "realm=", 6) == 0)
5661 return hs20_nai_home_realm_list(wpa_s, dst_addr,
5662 dst + used + 6);
5663
5664 len = os_strlen(dst + used);
5665
5666 if (len == 0 && cred && cred->realm)
5667 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
5668
0e87e798 5669 if (len & 1)
a8918e86
JK
5670 return -1;
5671 len /= 2;
5672 buf = os_malloc(len);
5673 if (buf == NULL)
5674 return -1;
5675 if (hexstr2bin(dst + used, buf, len) < 0) {
5676 os_free(buf);
5677 return -1;
5678 }
5679
5680 ret = hs20_anqp_send_req(wpa_s, dst_addr,
5681 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
5682 buf, len);
5683 os_free(buf);
5684
5685 return ret;
5686}
5687
184e110c
JM
5688
5689static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
5690{
5691 u8 dst_addr[ETH_ALEN];
5692 int used;
5693 char *icon;
5694
5695 used = hwaddr_aton2(cmd, dst_addr);
5696 if (used < 0)
5697 return -1;
5698
5699 while (cmd[used] == ' ')
5700 used++;
5701 icon = &cmd[used];
5702
b572df86 5703 wpa_s->fetch_osu_icon_in_progress = 0;
184e110c
JM
5704 return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
5705 (u8 *) icon, os_strlen(icon));
5706}
5707
a8918e86
JK
5708#endif /* CONFIG_HS20 */
5709
5710
bc5d330a
TB
5711#ifdef CONFIG_AUTOSCAN
5712
5713static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
5714 char *cmd)
5715{
5716 enum wpa_states state = wpa_s->wpa_state;
5717 char *new_params = NULL;
5718
5719 if (os_strlen(cmd) > 0) {
5720 new_params = os_strdup(cmd);
5721 if (new_params == NULL)
5722 return -1;
5723 }
5724
5725 os_free(wpa_s->conf->autoscan);
5726 wpa_s->conf->autoscan = new_params;
5727
5728 if (wpa_s->conf->autoscan == NULL)
5729 autoscan_deinit(wpa_s);
5730 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
99218999 5731 autoscan_init(wpa_s, 1);
99f00324
JM
5732 else if (state == WPA_SCANNING)
5733 wpa_supplicant_reinit_autoscan(wpa_s);
bc5d330a
TB
5734
5735 return 0;
5736}
5737
5738#endif /* CONFIG_AUTOSCAN */
5739
5740
e9199e31
JM
5741#ifdef CONFIG_WNM
5742
5743static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
5744{
5745 int enter;
5746 int intval = 0;
5747 char *pos;
cd0ef657
JM
5748 int ret;
5749 struct wpabuf *tfs_req = NULL;
e9199e31
JM
5750
5751 if (os_strncmp(cmd, "enter", 5) == 0)
5752 enter = 1;
5753 else if (os_strncmp(cmd, "exit", 4) == 0)
5754 enter = 0;
5755 else
5756 return -1;
5757
5758 pos = os_strstr(cmd, " interval=");
5759 if (pos)
5760 intval = atoi(pos + 10);
5761
cd0ef657
JM
5762 pos = os_strstr(cmd, " tfs_req=");
5763 if (pos) {
5764 char *end;
5765 size_t len;
5766 pos += 9;
5767 end = os_strchr(pos, ' ');
5768 if (end)
5769 len = end - pos;
5770 else
5771 len = os_strlen(pos);
5772 if (len & 1)
5773 return -1;
5774 len /= 2;
5775 tfs_req = wpabuf_alloc(len);
5776 if (tfs_req == NULL)
5777 return -1;
5778 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
5779 wpabuf_free(tfs_req);
5780 return -1;
5781 }
5782 }
5783
df80a0cc
JM
5784 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
5785 WNM_SLEEP_MODE_EXIT, intval,
cd0ef657
JM
5786 tfs_req);
5787 wpabuf_free(tfs_req);
5788
5789 return ret;
e9199e31
JM
5790}
5791
65bcd0a9
VK
5792
5793static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
5794{
5795 int query_reason;
5796
5797 query_reason = atoi(cmd);
5798
5799 wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
5800 query_reason);
5801
5802 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
5803}
5804
e9199e31
JM
5805#endif /* CONFIG_WNM */
5806
5807
60b24b0d
DS
5808static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
5809 size_t buflen)
5810{
5811 struct wpa_signal_info si;
5812 int ret;
2cc8d8f4 5813 char *pos, *end;
60b24b0d
DS
5814
5815 ret = wpa_drv_signal_poll(wpa_s, &si);
5816 if (ret)
5817 return -1;
5818
2cc8d8f4
AO
5819 pos = buf;
5820 end = buf + buflen;
5821
5822 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
60b24b0d
DS
5823 "NOISE=%d\nFREQUENCY=%u\n",
5824 si.current_signal, si.current_txrate / 1000,
5825 si.current_noise, si.frequency);
7bdd8981 5826 if (os_snprintf_error(end - pos, ret))
60b24b0d 5827 return -1;
2cc8d8f4
AO
5828 pos += ret;
5829
5830 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
5831 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
7a4a93b9 5832 channel_width_to_string(si.chanwidth));
7bdd8981 5833 if (os_snprintf_error(end - pos, ret))
2cc8d8f4
AO
5834 return -1;
5835 pos += ret;
5836 }
5837
5838 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
5839 ret = os_snprintf(pos, end - pos,
5840 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
5841 si.center_frq1, si.center_frq2);
7bdd8981 5842 if (os_snprintf_error(end - pos, ret))
2cc8d8f4
AO
5843 return -1;
5844 pos += ret;
5845 }
5846
95783298
AO
5847 if (si.avg_signal) {
5848 ret = os_snprintf(pos, end - pos,
5849 "AVG_RSSI=%d\n", si.avg_signal);
d85e1fc8 5850 if (os_snprintf_error(end - pos, ret))
95783298
AO
5851 return -1;
5852 pos += ret;
5853 }
5854
2cc8d8f4 5855 return pos - buf;
60b24b0d
DS
5856}
5857
5858
dc7785f8
YZ
5859static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
5860 size_t buflen)
5861{
5862 struct hostap_sta_driver_data sta;
5863 int ret;
5864
5865 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
5866 if (ret)
5867 return -1;
5868
5869 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
5870 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
7bdd8981 5871 if (os_snprintf_error(buflen, ret))
dc7785f8
YZ
5872 return -1;
5873 return ret;
5874}
5875
5876
5e2c3490
JM
5877#ifdef ANDROID
5878static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5879 char *buf, size_t buflen)
5880{
5881 int ret;
5882
5883 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
a94737ea
DS
5884 if (ret == 0) {
5885 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
5886 struct p2p_data *p2p = wpa_s->global->p2p;
5887 if (p2p) {
5888 char country[3];
5889 country[0] = cmd[8];
5890 country[1] = cmd[9];
5891 country[2] = 0x04;
5892 p2p_set_country(p2p, country);
5893 }
5894 }
5e2c3490 5895 ret = os_snprintf(buf, buflen, "%s\n", "OK");
aaadd727
JM
5896 if (os_snprintf_error(buflen, ret))
5897 ret = -1;
a94737ea 5898 }
5e2c3490
JM
5899 return ret;
5900}
5901#endif /* ANDROID */
5902
5903
adef8948
BL
5904static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
5905 char *buf, size_t buflen)
5906{
5907 int ret;
5908 char *pos;
5909 u8 *data = NULL;
5910 unsigned int vendor_id, subcmd;
5911 struct wpabuf *reply;
5912 size_t data_len = 0;
5913
5914 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
5915 vendor_id = strtoul(cmd, &pos, 16);
5916 if (!isblank(*pos))
5917 return -EINVAL;
5918
5919 subcmd = strtoul(pos, &pos, 10);
5920
5921 if (*pos != '\0') {
5922 if (!isblank(*pos++))
5923 return -EINVAL;
5924 data_len = os_strlen(pos);
5925 }
5926
5927 if (data_len) {
5928 data_len /= 2;
5929 data = os_malloc(data_len);
5930 if (!data)
2cebdee6 5931 return -1;
adef8948
BL
5932
5933 if (hexstr2bin(pos, data, data_len)) {
5934 wpa_printf(MSG_DEBUG,
5935 "Vendor command: wrong parameter format");
5936 os_free(data);
5937 return -EINVAL;
5938 }
5939 }
5940
5941 reply = wpabuf_alloc((buflen - 1) / 2);
5942 if (!reply) {
5943 os_free(data);
2cebdee6 5944 return -1;
adef8948
BL
5945 }
5946
5947 ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
5948 reply);
5949
5950 if (ret == 0)
5951 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
5952 wpabuf_len(reply));
5953
5954 wpabuf_free(reply);
5955 os_free(data);
5956
5957 return ret;
5958}
5959
5960
acb54643
JM
5961static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
5962{
5963 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
5964
5965#ifdef CONFIG_P2P
f05cee97 5966 wpas_p2p_cancel(wpa_s);
acb54643
JM
5967 wpas_p2p_stop_find(wpa_s);
5968 p2p_ctrl_flush(wpa_s);
5969 wpas_p2p_group_remove(wpa_s, "*");
3f45fc40 5970 wpas_p2p_service_flush(wpa_s);
083916c0
JM
5971 wpa_s->global->p2p_disabled = 0;
5972 wpa_s->global->p2p_per_sta_psk = 0;
78f0c933 5973 wpa_s->conf->num_sec_device_types = 0;
201b0f5f 5974 wpa_s->p2p_disable_ip_addr_req = 0;
7bb70909
JM
5975 os_free(wpa_s->global->p2p_go_avoid_freq.range);
5976 wpa_s->global->p2p_go_avoid_freq.range = NULL;
acb54643
JM
5977#endif /* CONFIG_P2P */
5978
5979#ifdef CONFIG_WPS_TESTING
5980 wps_version_number = 0x20;
5981 wps_testing_dummy_cred = 0;
91226e0d 5982 wps_corrupt_pkhash = 0;
acb54643
JM
5983#endif /* CONFIG_WPS_TESTING */
5984#ifdef CONFIG_WPS
7b02375a 5985 wpa_s->wps_fragment_size = 0;
acb54643
JM
5986 wpas_wps_cancel(wpa_s);
5987#endif /* CONFIG_WPS */
7255983b 5988 wpa_s->after_wps = 0;
4d9fb08d 5989 wpa_s->known_wps_freq = 0;
acb54643 5990
9d2cb3ec 5991#ifdef CONFIG_TDLS
acb54643
JM
5992#ifdef CONFIG_TDLS_TESTING
5993 extern unsigned int tdls_testing;
5994 tdls_testing = 0;
5995#endif /* CONFIG_TDLS_TESTING */
acb54643
JM
5996 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
5997 wpa_tdls_enable(wpa_s->wpa, 1);
5998#endif /* CONFIG_TDLS */
5999
e78aaca0
JM
6000 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
6001 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
6002
acb54643
JM
6003 wpa_s->no_keep_alive = 0;
6004
6005 os_free(wpa_s->disallow_aps_bssid);
6006 wpa_s->disallow_aps_bssid = NULL;
6007 wpa_s->disallow_aps_bssid_count = 0;
6008 os_free(wpa_s->disallow_aps_ssid);
6009 wpa_s->disallow_aps_ssid = NULL;
6010 wpa_s->disallow_aps_ssid_count = 0;
6011
6012 wpa_s->set_sta_uapsd = 0;
6013 wpa_s->sta_uapsd = 0;
6014
6015 wpa_drv_radio_disable(wpa_s, 0);
6016
6017 wpa_bss_flush(wpa_s);
6018 wpa_blacklist_clear(wpa_s);
a8a7890d 6019 wpa_s->extra_blacklist_count = 0;
acb54643
JM
6020 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
6021 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
d9bb2821 6022 wpa_config_flush_blobs(wpa_s->conf);
ea6e040c
JM
6023 wpa_s->conf->auto_interworking = 0;
6024 wpa_s->conf->okc = 0;
04f7ecc6 6025
b925506a
JM
6026 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
6027 rsn_preauth_deinit(wpa_s->wpa);
6028
04f7ecc6
JM
6029 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
6030 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
6031 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
0d79b50a 6032 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
b1ae396f 6033
b3253ebb 6034 radio_remove_works(wpa_s, NULL, 1);
e3745228 6035 wpa_s->ext_work_in_progress = 0;
3d910ef4
JM
6036
6037 wpa_s->next_ssid = NULL;
b572df86
JM
6038
6039#ifdef CONFIG_INTERWORKING
6040 hs20_cancel_fetch_osu(wpa_s);
6041#endif /* CONFIG_INTERWORKING */
60b893df
JM
6042
6043 wpa_s->ext_mgmt_frame_handling = 0;
9d4ff04a 6044 wpa_s->ext_eapol_frame_io = 0;
1f94e4ee
JM
6045#ifdef CONFIG_TESTING_OPTIONS
6046 wpa_s->extra_roc_dur = 0;
6047#endif /* CONFIG_TESTING_OPTIONS */
97cfe110
JM
6048
6049 wpa_s->disconnected = 0;
acb54643
JM
6050}
6051
6052
1f965e62
JM
6053static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
6054 char *buf, size_t buflen)
6055{
6056 struct wpa_radio_work *work;
6057 char *pos, *end;
6058 struct os_reltime now, diff;
6059
6060 pos = buf;
6061 end = buf + buflen;
6062
6063 os_get_reltime(&now);
6064
6065 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
6066 {
6067 int ret;
6068
6069 os_reltime_sub(&now, &work->time, &diff);
6070 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
6071 work->type, work->wpa_s->ifname, work->freq,
6072 work->started, diff.sec, diff.usec);
d85e1fc8 6073 if (os_snprintf_error(end - pos, ret))
1f965e62
JM
6074 break;
6075 pos += ret;
6076 }
6077
6078 return pos - buf;
6079}
6080
6081
6082static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
6083{
6084 struct wpa_radio_work *work = eloop_ctx;
6085 struct wpa_external_work *ework = work->ctx;
6086
6087 wpa_dbg(work->wpa_s, MSG_DEBUG,
6088 "Timing out external radio work %u (%s)",
6089 ework->id, work->type);
6090 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
e3745228 6091 work->wpa_s->ext_work_in_progress = 0;
1f965e62 6092 radio_work_done(work);
df48efc5 6093 os_free(ework);
1f965e62
JM
6094}
6095
6096
6097static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
6098{
6099 struct wpa_external_work *ework = work->ctx;
6100
6101 if (deinit) {
b3253ebb
AO
6102 if (work->started)
6103 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
6104 work, NULL);
6105
1f965e62
JM
6106 os_free(ework);
6107 return;
6108 }
6109
6110 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
6111 ework->id, ework->type);
6112 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
e3745228 6113 work->wpa_s->ext_work_in_progress = 1;
1f965e62
JM
6114 if (!ework->timeout)
6115 ework->timeout = 10;
6116 eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
6117 work, NULL);
6118}
6119
6120
6121static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
6122 char *buf, size_t buflen)
6123{
6124 struct wpa_external_work *ework;
6125 char *pos, *pos2;
6126 size_t type_len;
6127 int ret;
6128 unsigned int freq = 0;
6129
6130 /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
6131
6132 ework = os_zalloc(sizeof(*ework));
6133 if (ework == NULL)
6134 return -1;
6135
6136 pos = os_strchr(cmd, ' ');
6137 if (pos) {
6138 type_len = pos - cmd;
6139 pos++;
6140
6141 pos2 = os_strstr(pos, "freq=");
6142 if (pos2)
6143 freq = atoi(pos2 + 5);
6144
6145 pos2 = os_strstr(pos, "timeout=");
6146 if (pos2)
6147 ework->timeout = atoi(pos2 + 8);
6148 } else {
6149 type_len = os_strlen(cmd);
6150 }
6151 if (4 + type_len >= sizeof(ework->type))
6152 type_len = sizeof(ework->type) - 4 - 1;
6153 os_strlcpy(ework->type, "ext:", sizeof(ework->type));
6154 os_memcpy(ework->type + 4, cmd, type_len);
6155 ework->type[4 + type_len] = '\0';
6156
6157 wpa_s->ext_work_id++;
6158 if (wpa_s->ext_work_id == 0)
6159 wpa_s->ext_work_id++;
6160 ework->id = wpa_s->ext_work_id;
6161
6162 if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
6163 ework) < 0) {
6164 os_free(ework);
6165 return -1;
6166 }
6167
6168 ret = os_snprintf(buf, buflen, "%u", ework->id);
d85e1fc8 6169 if (os_snprintf_error(buflen, ret))
1f965e62
JM
6170 return -1;
6171 return ret;
6172}
6173
6174
6175static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
6176{
6177 struct wpa_radio_work *work;
6178 unsigned int id = atoi(cmd);
6179
6180 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
6181 {
6182 struct wpa_external_work *ework;
6183
6184 if (os_strncmp(work->type, "ext:", 4) != 0)
6185 continue;
6186 ework = work->ctx;
6187 if (id && ework->id != id)
6188 continue;
6189 wpa_dbg(wpa_s, MSG_DEBUG,
6190 "Completed external radio work %u (%s)",
6191 ework->id, ework->type);
6192 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
e3745228 6193 wpa_s->ext_work_in_progress = 0;
1f965e62 6194 radio_work_done(work);
6829da39 6195 os_free(ework);
1f965e62
JM
6196 return 3; /* "OK\n" */
6197 }
6198
6199 return -1;
6200}
6201
6202
6203static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
6204 char *buf, size_t buflen)
6205{
6206 if (os_strcmp(cmd, "show") == 0)
6207 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
6208 if (os_strncmp(cmd, "add ", 4) == 0)
6209 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
6210 if (os_strncmp(cmd, "done ", 5) == 0)
6211 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
6212 return -1;
6213}
6214
6215
6216void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
6217{
6218 struct wpa_radio_work *work, *tmp;
6219
a6cff8bf
MS
6220 if (!wpa_s || !wpa_s->radio)
6221 return;
6222
1f965e62
JM
6223 dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
6224 struct wpa_radio_work, list) {
6225 struct wpa_external_work *ework;
6226
6227 if (os_strncmp(work->type, "ext:", 4) != 0)
6228 continue;
6229 ework = work->ctx;
6230 wpa_dbg(wpa_s, MSG_DEBUG,
023b466d 6231 "Flushing%s external radio work %u (%s)",
1f965e62
JM
6232 work->started ? " started" : "", ework->id,
6233 ework->type);
6234 if (work->started)
6235 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
6236 work, NULL);
1f965e62 6237 radio_work_done(work);
df48efc5 6238 os_free(ework);
1f965e62
JM
6239 }
6240}
6241
6242
bceb8431
JM
6243static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
6244{
6245 struct wpa_supplicant *wpa_s = eloop_ctx;
6246 eapol_sm_notify_ctrl_response(wpa_s->eapol);
6247}
6248
6249
43a66ecb
JM
6250static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
6251 unsigned int *scan_id_count, int scan_id[])
6891f0e6
LJ
6252{
6253 const char *pos = value;
6254
6255 while (pos) {
6256 if (*pos == ' ' || *pos == '\0')
6257 break;
43a66ecb 6258 if (*scan_id_count == MAX_SCAN_ID)
6891f0e6 6259 return -1;
43a66ecb 6260 scan_id[(*scan_id_count)++] = atoi(pos);
6891f0e6
LJ
6261 pos = os_strchr(pos, ',');
6262 if (pos)
6263 pos++;
6264 }
6265
6266 return 0;
6267}
6268
6269
fee52342
JM
6270static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
6271 char *reply, int reply_size, int *reply_len)
6272{
6273 char *pos;
43a66ecb
JM
6274 unsigned int manual_scan_passive = 0;
6275 unsigned int manual_scan_use_id = 0;
6276 unsigned int manual_scan_only_new = 0;
6277 unsigned int scan_only = 0;
6278 unsigned int scan_id_count = 0;
6279 int scan_id[MAX_SCAN_ID];
6280 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
6281 struct wpa_scan_results *scan_res);
6282 int *manual_scan_freqs = NULL;
fee52342
JM
6283
6284 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6285 *reply_len = -1;
6286 return;
6287 }
6288
e69ae5ff
JM
6289 if (radio_work_pending(wpa_s, "scan")) {
6290 wpa_printf(MSG_DEBUG,
6291 "Pending scan scheduled - reject new request");
6292 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
6293 return;
6294 }
6295
fee52342
JM
6296 if (params) {
6297 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
43a66ecb 6298 scan_only = 1;
fee52342
JM
6299
6300 pos = os_strstr(params, "freq=");
43a66ecb
JM
6301 if (pos) {
6302 manual_scan_freqs = freq_range_to_channel_list(wpa_s,
6303 pos + 5);
6304 if (manual_scan_freqs == NULL) {
6305 *reply_len = -1;
6306 goto done;
6307 }
fee52342 6308 }
88c2d488
JM
6309
6310 pos = os_strstr(params, "passive=");
6311 if (pos)
43a66ecb 6312 manual_scan_passive = !!atoi(pos + 8);
d81c73be
JM
6313
6314 pos = os_strstr(params, "use_id=");
6315 if (pos)
43a66ecb 6316 manual_scan_use_id = atoi(pos + 7);
949938aa
JM
6317
6318 pos = os_strstr(params, "only_new=1");
6319 if (pos)
43a66ecb 6320 manual_scan_only_new = 1;
6891f0e6
LJ
6321
6322 pos = os_strstr(params, "scan_id=");
43a66ecb
JM
6323 if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
6324 scan_id) < 0) {
6891f0e6 6325 *reply_len = -1;
43a66ecb 6326 goto done;
6891f0e6 6327 }
fee52342
JM
6328 }
6329
43a66ecb
JM
6330 if (scan_only)
6331 scan_res_handler = scan_only_handler;
6332 else if (wpa_s->scan_res_handler == scan_only_handler)
6333 scan_res_handler = NULL;
6334 else
6335 scan_res_handler = wpa_s->scan_res_handler;
6336
fee52342
JM
6337 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
6338 ((wpa_s->wpa_state <= WPA_SCANNING) ||
6339 (wpa_s->wpa_state == WPA_COMPLETED))) {
43a66ecb
JM
6340 wpa_s->manual_scan_passive = manual_scan_passive;
6341 wpa_s->manual_scan_use_id = manual_scan_use_id;
6342 wpa_s->manual_scan_only_new = manual_scan_only_new;
6343 wpa_s->scan_id_count = scan_id_count;
6344 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
6345 wpa_s->scan_res_handler = scan_res_handler;
6346 os_free(wpa_s->manual_scan_freqs);
6347 wpa_s->manual_scan_freqs = manual_scan_freqs;
6348 manual_scan_freqs = NULL;
6349
fee52342
JM
6350 wpa_s->normal_scans = 0;
6351 wpa_s->scan_req = MANUAL_SCAN_REQ;
6352 wpa_s->after_wps = 0;
6353 wpa_s->known_wps_freq = 0;
6354 wpa_supplicant_req_scan(wpa_s, 0, 0);
d81c73be
JM
6355 if (wpa_s->manual_scan_use_id) {
6356 wpa_s->manual_scan_id++;
6357 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
6358 wpa_s->manual_scan_id);
6359 *reply_len = os_snprintf(reply, reply_size, "%u\n",
6360 wpa_s->manual_scan_id);
6361 }
fee52342 6362 } else if (wpa_s->sched_scanning) {
43a66ecb
JM
6363 wpa_s->manual_scan_passive = manual_scan_passive;
6364 wpa_s->manual_scan_use_id = manual_scan_use_id;
6365 wpa_s->manual_scan_only_new = manual_scan_only_new;
6366 wpa_s->scan_id_count = scan_id_count;
6367 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
6368 wpa_s->scan_res_handler = scan_res_handler;
6369 os_free(wpa_s->manual_scan_freqs);
6370 wpa_s->manual_scan_freqs = manual_scan_freqs;
6371 manual_scan_freqs = NULL;
6372
fee52342
JM
6373 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
6374 wpa_supplicant_cancel_sched_scan(wpa_s);
6375 wpa_s->scan_req = MANUAL_SCAN_REQ;
6376 wpa_supplicant_req_scan(wpa_s, 0, 0);
d81c73be
JM
6377 if (wpa_s->manual_scan_use_id) {
6378 wpa_s->manual_scan_id++;
6379 *reply_len = os_snprintf(reply, reply_size, "%u\n",
6380 wpa_s->manual_scan_id);
6381 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
6382 wpa_s->manual_scan_id);
6383 }
fee52342
JM
6384 } else {
6385 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
6386 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
6387 }
43a66ecb
JM
6388
6389done:
6390 os_free(manual_scan_freqs);
fee52342
JM
6391}
6392
6393
60b893df
JM
6394#ifdef CONFIG_TESTING_OPTIONS
6395
6396static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
6397 unsigned int freq, const u8 *dst,
6398 const u8 *src, const u8 *bssid,
6399 const u8 *data, size_t data_len,
6400 enum offchannel_send_action_result
6401 result)
6402{
6403 wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
6404 " src=" MACSTR " bssid=" MACSTR " result=%s",
6405 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
6406 result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
6407 "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
6408 "NO_ACK" : "FAILED"));
6409}
6410
6411
6412static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
6413{
6414 char *pos, *param;
6415 size_t len;
6416 u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
6417 int res, used;
6418 int freq = 0, no_cck = 0, wait_time = 0;
6419
6420 /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
6421 * <action=Action frame payload> */
6422
6423 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
6424
6425 pos = cmd;
6426 used = hwaddr_aton2(pos, da);
6427 if (used < 0)
6428 return -1;
6429 pos += used;
6430 while (*pos == ' ')
6431 pos++;
6432 used = hwaddr_aton2(pos, bssid);
6433 if (used < 0)
6434 return -1;
6435 pos += used;
6436
6437 param = os_strstr(pos, " freq=");
6438 if (param) {
6439 param += 6;
6440 freq = atoi(param);
6441 }
6442
6443 param = os_strstr(pos, " no_cck=");
6444 if (param) {
6445 param += 8;
6446 no_cck = atoi(param);
6447 }
6448
6449 param = os_strstr(pos, " wait_time=");
6450 if (param) {
6451 param += 11;
6452 wait_time = atoi(param);
6453 }
6454
6455 param = os_strstr(pos, " action=");
6456 if (param == NULL)
6457 return -1;
6458 param += 8;
6459
6460 len = os_strlen(param);
6461 if (len & 1)
6462 return -1;
6463 len /= 2;
6464
6465 buf = os_malloc(len);
6466 if (buf == NULL)
6467 return -1;
6468
6469 if (hexstr2bin(param, buf, len) < 0) {
6470 os_free(buf);
6471 return -1;
6472 }
6473
6474 res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
6475 buf, len, wait_time,
6476 wpas_ctrl_iface_mgmt_tx_cb, no_cck);
6477 os_free(buf);
6478 return res;
6479}
6480
6481
6482static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
6483{
6484 wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
6485 offchannel_send_action_done(wpa_s);
6486}
6487
ad12f2f4
JM
6488
6489static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
6490{
6491 char *pos, *param;
6492 union wpa_event_data event;
6493 enum wpa_event_type ev;
6494
6495 /* <event name> [parameters..] */
6496
6497 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
6498
6499 pos = cmd;
6500 param = os_strchr(pos, ' ');
6501 if (param)
6502 *param++ = '\0';
6503
6504 os_memset(&event, 0, sizeof(event));
6505
6506 if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
6507 ev = EVENT_INTERFACE_ENABLED;
6508 } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
6509 ev = EVENT_INTERFACE_DISABLED;
7bb70909
JM
6510 } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
6511 ev = EVENT_AVOID_FREQUENCIES;
6512 if (param == NULL)
6513 param = "";
6514 if (freq_range_list_parse(&event.freq_range, param) < 0)
6515 return -1;
6516 wpa_supplicant_event(wpa_s, ev, &event);
6517 os_free(event.freq_range.range);
6518 return 0;
ad12f2f4
JM
6519 } else {
6520 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
6521 cmd);
6522 return -1;
6523 }
6524
6525 wpa_supplicant_event(wpa_s, ev, &event);
6526
6527 return 0;
6528}
6529
9d4ff04a
JM
6530
6531static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
6532{
6533 char *pos;
6534 u8 src[ETH_ALEN], *buf;
6535 int used;
6536 size_t len;
6537
6538 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
6539
6540 pos = cmd;
6541 used = hwaddr_aton2(pos, src);
6542 if (used < 0)
6543 return -1;
6544 pos += used;
6545 while (*pos == ' ')
6546 pos++;
6547
6548 len = os_strlen(pos);
6549 if (len & 1)
6550 return -1;
6551 len /= 2;
6552
6553 buf = os_malloc(len);
6554 if (buf == NULL)
6555 return -1;
6556
6557 if (hexstr2bin(pos, buf, len) < 0) {
6558 os_free(buf);
6559 return -1;
6560 }
6561
6562 wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
6563 os_free(buf);
6564
6565 return 0;
6566}
6567
4a6cc862
JM
6568
6569static u16 ipv4_hdr_checksum(const void *buf, size_t len)
6570{
6571 size_t i;
6572 u32 sum = 0;
6573 const u16 *pos = buf;
6574
6575 for (i = 0; i < len / 2; i++)
6576 sum += *pos++;
6577
6578 while (sum >> 16)
6579 sum = (sum & 0xffff) + (sum >> 16);
6580
6581 return sum ^ 0xffff;
6582}
6583
6584
6585#define HWSIM_PACKETLEN 1500
6586#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
6587
6588void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
6589{
6590 struct wpa_supplicant *wpa_s = ctx;
6591 const struct ether_header *eth;
6592 const struct iphdr *ip;
6593 const u8 *pos;
6594 unsigned int i;
6595
6596 if (len != HWSIM_PACKETLEN)
6597 return;
6598
6599 eth = (const struct ether_header *) buf;
6600 ip = (const struct iphdr *) (eth + 1);
6601 pos = (const u8 *) (ip + 1);
6602
6603 if (ip->ihl != 5 || ip->version != 4 ||
6604 ntohs(ip->tot_len) != HWSIM_IP_LEN)
6605 return;
6606
6607 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++) {
6608 if (*pos != (u8) i)
6609 return;
6610 pos++;
6611 }
6612
6613 wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
6614 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
6615}
6616
6617
6618static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
6619 char *cmd)
6620{
6621 int enabled = atoi(cmd);
6622
6623 if (!enabled) {
6624 if (wpa_s->l2_test) {
6625 l2_packet_deinit(wpa_s->l2_test);
6626 wpa_s->l2_test = NULL;
6627 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
6628 }
6629 return 0;
6630 }
6631
6632 if (wpa_s->l2_test)
6633 return 0;
6634
6635 wpa_s->l2_test = l2_packet_init(wpa_s->ifname, wpa_s->own_addr,
6636 ETHERTYPE_IP, wpas_data_test_rx,
6637 wpa_s, 1);
6638 if (wpa_s->l2_test == NULL)
6639 return -1;
6640
6641 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
6642
6643 return 0;
6644}
6645
6646
6647static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
6648{
6649 u8 dst[ETH_ALEN], src[ETH_ALEN];
6650 char *pos;
6651 int used;
6652 long int val;
6653 u8 tos;
6654 u8 buf[HWSIM_PACKETLEN];
6655 struct ether_header *eth;
6656 struct iphdr *ip;
6657 u8 *dpos;
6658 unsigned int i;
6659
6660 if (wpa_s->l2_test == NULL)
6661 return -1;
6662
6663 /* format: <dst> <src> <tos> */
6664
6665 pos = cmd;
6666 used = hwaddr_aton2(pos, dst);
6667 if (used < 0)
6668 return -1;
6669 pos += used;
6670 while (*pos == ' ')
6671 pos++;
6672 used = hwaddr_aton2(pos, src);
6673 if (used < 0)
6674 return -1;
6675 pos += used;
6676
6677 val = strtol(pos, NULL, 0);
6678 if (val < 0 || val > 0xff)
6679 return -1;
6680 tos = val;
6681
6682 eth = (struct ether_header *) buf;
6683 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
6684 os_memcpy(eth->ether_shost, src, ETH_ALEN);
6685 eth->ether_type = htons(ETHERTYPE_IP);
6686 ip = (struct iphdr *) (eth + 1);
6687 os_memset(ip, 0, sizeof(*ip));
6688 ip->ihl = 5;
6689 ip->version = 4;
6690 ip->ttl = 64;
6691 ip->tos = tos;
6692 ip->tot_len = htons(HWSIM_IP_LEN);
6693 ip->protocol = 1;
6694 ip->saddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 1);
6695 ip->daddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 2);
6696 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
6697 dpos = (u8 *) (ip + 1);
6698 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
6699 *dpos++ = i;
6700
6701 if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, buf,
6702 HWSIM_PACKETLEN) < 0)
6703 return -1;
6704
6705 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
6706 " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
6707
6708 return 0;
6709}
6710
fc0ef7c0
JM
6711
6712static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
6713 char *cmd)
6714{
6715 u8 *buf;
6716 struct ether_header *eth;
6717 struct l2_packet_data *l2 = NULL;
6718 size_t len;
6719 u16 ethertype;
6720 int res = -1;
6721
6722 len = os_strlen(cmd);
6723 if (len & 1 || len < ETH_HLEN * 2)
6724 return -1;
6725 len /= 2;
6726
6727 buf = os_malloc(len);
6728 if (buf == NULL)
6729 return -1;
6730
6731 if (hexstr2bin(cmd, buf, len) < 0)
6732 goto done;
6733
6734 eth = (struct ether_header *) buf;
6735 ethertype = ntohs(eth->ether_type);
6736
6737 l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
6738 wpas_data_test_rx, wpa_s, 1);
6739 if (l2 == NULL)
6740 goto done;
6741
6742 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
6743 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
6744done:
6745 if (l2)
6746 l2_packet_deinit(l2);
6747 os_free(buf);
6748
6749 return res < 0 ? -1 : 0;
6750}
6751
60b893df
JM
6752#endif /* CONFIG_TESTING_OPTIONS */
6753
6754
86bd36f0
JM
6755static void wpas_ctrl_vendor_elem_update(struct wpa_supplicant *wpa_s)
6756{
6757 unsigned int i;
6758 char buf[30];
6759
6760 wpa_printf(MSG_DEBUG, "Update vendor elements");
6761
6762 for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
6763 if (wpa_s->vendor_elem[i]) {
1d399771
JM
6764 int res;
6765
6766 res = os_snprintf(buf, sizeof(buf), "frame[%u]", i);
6767 if (!os_snprintf_error(sizeof(buf), res)) {
6768 wpa_hexdump_buf(MSG_DEBUG, buf,
6769 wpa_s->vendor_elem[i]);
6770 }
86bd36f0
JM
6771 }
6772 }
6773
6774#ifdef CONFIG_P2P
6775 if (wpa_s->parent == wpa_s &&
6776 wpa_s->global->p2p &&
6777 !wpa_s->global->p2p_disabled)
6778 p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
6779#endif /* CONFIG_P2P */
6780}
6781
6782
6783static struct wpa_supplicant *
6784wpas_ctrl_vendor_elem_iface(struct wpa_supplicant *wpa_s,
6785 enum wpa_vendor_elem_frame frame)
6786{
6787 switch (frame) {
6788#ifdef CONFIG_P2P
6789 case VENDOR_ELEM_PROBE_REQ_P2P:
6790 case VENDOR_ELEM_PROBE_RESP_P2P:
6791 case VENDOR_ELEM_PROBE_RESP_P2P_GO:
6792 case VENDOR_ELEM_BEACON_P2P_GO:
6793 case VENDOR_ELEM_P2P_PD_REQ:
6794 case VENDOR_ELEM_P2P_PD_RESP:
6795 case VENDOR_ELEM_P2P_GO_NEG_REQ:
6796 case VENDOR_ELEM_P2P_GO_NEG_RESP:
6797 case VENDOR_ELEM_P2P_GO_NEG_CONF:
6798 case VENDOR_ELEM_P2P_INV_REQ:
6799 case VENDOR_ELEM_P2P_INV_RESP:
6800 case VENDOR_ELEM_P2P_ASSOC_REQ:
6801 return wpa_s->parent;
6802#endif /* CONFIG_P2P */
6803 default:
6804 return wpa_s;
6805 }
6806}
6807
6808
6809static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
6810{
6811 char *pos = cmd;
6812 int frame;
6813 size_t len;
6814 struct wpabuf *buf;
6815 struct ieee802_11_elems elems;
6816
6817 frame = atoi(pos);
6818 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
6819 return -1;
6820 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
6821
6822 pos = os_strchr(pos, ' ');
6823 if (pos == NULL)
6824 return -1;
6825 pos++;
6826
6827 len = os_strlen(pos);
6828 if (len == 0)
6829 return 0;
6830 if (len & 1)
6831 return -1;
6832 len /= 2;
6833
6834 buf = wpabuf_alloc(len);
6835 if (buf == NULL)
6836 return -1;
6837
6838 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
6839 wpabuf_free(buf);
6840 return -1;
6841 }
6842
6843 if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
6844 ParseFailed) {
6845 wpabuf_free(buf);
6846 return -1;
6847 }
6848
6849 if (wpa_s->vendor_elem[frame] == NULL) {
6850 wpa_s->vendor_elem[frame] = buf;
6851 wpas_ctrl_vendor_elem_update(wpa_s);
6852 return 0;
6853 }
6854
6855 if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
6856 wpabuf_free(buf);
6857 return -1;
6858 }
6859
6860 wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
6861 wpabuf_free(buf);
6862 wpas_ctrl_vendor_elem_update(wpa_s);
6863
6864 return 0;
6865}
6866
6867
6868static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
6869 char *buf, size_t buflen)
6870{
6871 int frame = atoi(cmd);
6872
6873 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
6874 return -1;
6875 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
6876
6877 if (wpa_s->vendor_elem[frame] == NULL)
6878 return 0;
6879
6880 return wpa_snprintf_hex(buf, buflen,
6881 wpabuf_head_u8(wpa_s->vendor_elem[frame]),
6882 wpabuf_len(wpa_s->vendor_elem[frame]));
6883}
6884
6885
6886static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
6887{
6888 char *pos = cmd;
6889 int frame;
6890 size_t len;
6891 u8 *buf;
6892 struct ieee802_11_elems elems;
6893 u8 *ie, *end;
6894
6895 frame = atoi(pos);
6896 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
6897 return -1;
6898 wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
6899
6900 pos = os_strchr(pos, ' ');
6901 if (pos == NULL)
6902 return -1;
6903 pos++;
6904
6905 if (*pos == '*') {
6906 wpabuf_free(wpa_s->vendor_elem[frame]);
6907 wpa_s->vendor_elem[frame] = NULL;
6908 wpas_ctrl_vendor_elem_update(wpa_s);
6909 return 0;
6910 }
6911
6912 if (wpa_s->vendor_elem[frame] == NULL)
6913 return -1;
6914
6915 len = os_strlen(pos);
6916 if (len == 0)
6917 return 0;
6918 if (len & 1)
6919 return -1;
6920 len /= 2;
6921
6922 buf = os_malloc(len);
6923 if (buf == NULL)
6924 return -1;
6925
6926 if (hexstr2bin(pos, buf, len) < 0) {
6927 os_free(buf);
6928 return -1;
6929 }
6930
6931 if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
6932 os_free(buf);
6933 return -1;
6934 }
6935
6936 ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
6937 end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
6938
6939 for (; ie + 1 < end; ie += 2 + ie[1]) {
6940 if (ie + len > end)
6941 break;
6942 if (os_memcmp(ie, buf, len) != 0)
6943 continue;
6944
6945 if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
6946 wpabuf_free(wpa_s->vendor_elem[frame]);
6947 wpa_s->vendor_elem[frame] = NULL;
6948 } else {
6949 os_memmove(ie, ie + len,
45d85015 6950 end - (ie + len));
86bd36f0
JM
6951 wpa_s->vendor_elem[frame]->used -= len;
6952 }
6953 os_free(buf);
6954 wpas_ctrl_vendor_elem_update(wpa_s);
6955 return 0;
6956 }
6957
6958 os_free(buf);
6959
6960 return -1;
6961}
6962
6963
f4b8bfae
AK
6964static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
6965{
6966 struct wpa_supplicant *wpa_s = ctx;
6967
6968 if (neighbor_rep) {
6969 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
6970 "length=%u",
6971 (unsigned int) wpabuf_len(neighbor_rep));
6972 wpabuf_free(neighbor_rep);
6973 } else {
6974 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
6975 }
6976}
6977
6978
4c4b2305
AK
6979static int wpas_ctrl_iface_send_neigbor_rep(struct wpa_supplicant *wpa_s,
6980 char *cmd)
f4b8bfae 6981{
4c4b2305
AK
6982 struct wpa_ssid ssid;
6983 struct wpa_ssid *ssid_p = NULL;
6984 int ret = 0;
6985
6986 if (os_strncmp(cmd, " ssid=", 6) == 0) {
6987 ssid.ssid_len = os_strlen(cmd + 6);
6988 if (ssid.ssid_len > 32)
6989 return -1;
6990 ssid.ssid = (u8 *) (cmd + 6);
6991 ssid_p = &ssid;
6992 }
6993
6994 ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p,
6995 wpas_ctrl_neighbor_rep_cb,
6996 wpa_s);
6997
6998 return ret;
f4b8bfae
AK
6999}
7000
7001
65d9a5e2
JM
7002static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
7003{
7004 eapol_sm_erp_flush(wpa_s->eapol);
7005 return 0;
7006}
7007
7008
6fc6879b
JM
7009char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
7010 char *buf, size_t *resp_len)
7011{
7012 char *reply;
b563b388 7013 const int reply_size = 4096;
6fc6879b
JM
7014 int reply_len;
7015
7016 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
d31b5ac7
JM
7017 os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
7018 if (wpa_debug_show_keys)
7019 wpa_dbg(wpa_s, MSG_DEBUG,
7020 "Control interface command '%s'", buf);
7021 else
7022 wpa_dbg(wpa_s, MSG_DEBUG,
7023 "Control interface command '%s [REMOVED]'",
7024 os_strncmp(buf, WPA_CTRL_RSP,
7025 os_strlen(WPA_CTRL_RSP)) == 0 ?
7026 WPA_CTRL_RSP : "SET_NETWORK");
7027 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
679f2e7c 7028 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
6fc6879b
JM
7029 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
7030 (const u8 *) buf, os_strlen(buf));
7031 } else {
235f69fc
JM
7032 int level = MSG_DEBUG;
7033 if (os_strcmp(buf, "PING") == 0)
7034 level = MSG_EXCESSIVE;
b470b2bf 7035 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
6fc6879b
JM
7036 }
7037
7038 reply = os_malloc(reply_size);
7039 if (reply == NULL) {
7040 *resp_len = 1;
7041 return NULL;
7042 }
7043
7044 os_memcpy(reply, "OK\n", 3);
7045 reply_len = 3;
7046
7047 if (os_strcmp(buf, "PING") == 0) {
7048 os_memcpy(reply, "PONG\n", 5);
7049 reply_len = 5;
0eed2a8d
JD
7050 } else if (os_strcmp(buf, "IFNAME") == 0) {
7051 reply_len = os_strlen(wpa_s->ifname);
7052 os_memcpy(reply, wpa_s->ifname, reply_len);
ac6912b5
BG
7053 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
7054 if (wpa_debug_reopen_file() < 0)
7055 reply_len = -1;
77895cd9
JM
7056 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
7057 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
6fc6879b
JM
7058 } else if (os_strcmp(buf, "MIB") == 0) {
7059 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
7060 if (reply_len >= 0) {
5ac73acf
JM
7061 reply_len += eapol_sm_get_mib(wpa_s->eapol,
7062 reply + reply_len,
7063 reply_size - reply_len);
6fc6879b
JM
7064 }
7065 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
7066 reply_len = wpa_supplicant_ctrl_iface_status(
7067 wpa_s, buf + 6, reply, reply_size);
7068 } else if (os_strcmp(buf, "PMKSA") == 0) {
540264a7
JM
7069 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
7070 reply_size);
79e2b1cc
AK
7071 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
7072 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
6fc6879b
JM
7073 } else if (os_strncmp(buf, "SET ", 4) == 0) {
7074 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
7075 reply_len = -1;
acec8d32
JM
7076 } else if (os_strncmp(buf, "GET ", 4) == 0) {
7077 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
7078 reply, reply_size);
6fc6879b
JM
7079 } else if (os_strcmp(buf, "LOGON") == 0) {
7080 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
7081 } else if (os_strcmp(buf, "LOGOFF") == 0) {
7082 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
7083 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
8401a6b0
JM
7084 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
7085 reply_len = -1;
9796a86c
JM
7086 else
7087 wpas_request_connection(wpa_s);
0f44ec8e
PQ
7088 } else if (os_strcmp(buf, "REATTACH") == 0) {
7089 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
7090 !wpa_s->current_ssid)
7091 reply_len = -1;
7092 else {
7093 wpa_s->reattach = 1;
7094 wpas_request_connection(wpa_s);
7095 }
6fc6879b 7096 } else if (os_strcmp(buf, "RECONNECT") == 0) {
8401a6b0
JM
7097 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
7098 reply_len = -1;
9796a86c
JM
7099 else if (wpa_s->disconnected)
7100 wpas_request_connection(wpa_s);
ec717917 7101#ifdef IEEE8021X_EAPOL
6fc6879b
JM
7102 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
7103 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
7104 reply_len = -1;
ec717917 7105#endif /* IEEE8021X_EAPOL */
6fc6879b
JM
7106#ifdef CONFIG_PEERKEY
7107 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
7108 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
7109 reply_len = -1;
7110#endif /* CONFIG_PEERKEY */
7111#ifdef CONFIG_IEEE80211R
7112 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
7113 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
7114 reply_len = -1;
7115#endif /* CONFIG_IEEE80211R */
fcc60db4
JM
7116#ifdef CONFIG_WPS
7117 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3152ff42
CWY
7118 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
7119 if (res == -2) {
7120 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
7121 reply_len = 17;
7122 } else if (res)
fcc60db4
JM
7123 reply_len = -1;
7124 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
3152ff42
CWY
7125 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
7126 if (res == -2) {
7127 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
7128 reply_len = 17;
7129 } else if (res)
fcc60db4
JM
7130 reply_len = -1;
7131 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
7132 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
7133 reply,
7134 reply_size);
3981cb3c
JM
7135 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
7136 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
7137 wpa_s, buf + 14, reply, reply_size);
2f9929ff
AC
7138 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
7139 if (wpas_wps_cancel(wpa_s))
7140 reply_len = -1;
71892384 7141#ifdef CONFIG_WPS_NFC
3f2c8ba6
JM
7142 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
7143 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
7144 reply_len = -1;
7145 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
7146 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
7147 reply_len = -1;
bbf41865
JM
7148 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
7149 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
7150 wpa_s, buf + 21, reply, reply_size);
3f2c8ba6
JM
7151 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
7152 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
7153 wpa_s, buf + 14, reply, reply_size);
d7645d23
JM
7154 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
7155 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
7156 buf + 17))
7157 reply_len = -1;
e65552dd
JM
7158 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
7159 reply_len = wpas_ctrl_nfc_get_handover_req(
7160 wpa_s, buf + 21, reply, reply_size);
7161 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
7162 reply_len = wpas_ctrl_nfc_get_handover_sel(
7163 wpa_s, buf + 21, reply, reply_size);
e4758827
JM
7164 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
7165 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
7166 reply_len = -1;
71892384 7167#endif /* CONFIG_WPS_NFC */
fcc60db4
JM
7168 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
7169 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
7170 reply_len = -1;
70d84f11
JM
7171#ifdef CONFIG_AP
7172 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
7173 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
7174 wpa_s, buf + 11, reply, reply_size);
7175#endif /* CONFIG_AP */
72df2f5f 7176#ifdef CONFIG_WPS_ER
e9bcfebf 7177 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
08486685
JM
7178 if (wpas_wps_er_start(wpa_s, NULL))
7179 reply_len = -1;
7180 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
7181 if (wpas_wps_er_start(wpa_s, buf + 13))
e9bcfebf
JM
7182 reply_len = -1;
7183 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
f77cedc1 7184 wpas_wps_er_stop(wpa_s);
72df2f5f
JM
7185 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
7186 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
7187 reply_len = -1;
564cd7fa 7188 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
ed159ad4
JM
7189 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
7190 if (ret == -2) {
7191 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
7192 reply_len = 17;
7193 } else if (ret == -3) {
7194 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
7195 reply_len = 18;
7196 } else if (ret == -4) {
7197 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
7198 reply_len = 20;
7199 } else if (ret)
564cd7fa 7200 reply_len = -1;
e64dcfd5
JM
7201 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
7202 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
7203 reply_len = -1;
ef10f473
JM
7204 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
7205 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
7206 buf + 18))
7207 reply_len = -1;
7d6640a6
JM
7208 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
7209 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
7210 reply_len = -1;
1cea09a9
JM
7211#ifdef CONFIG_WPS_NFC
7212 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
7213 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
7214 wpa_s, buf + 24, reply, reply_size);
7215#endif /* CONFIG_WPS_NFC */
72df2f5f 7216#endif /* CONFIG_WPS_ER */
fcc60db4 7217#endif /* CONFIG_WPS */
11ef8d35
JM
7218#ifdef CONFIG_IBSS_RSN
7219 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
7220 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
7221 reply_len = -1;
7222#endif /* CONFIG_IBSS_RSN */
603a3f34 7223#ifdef CONFIG_MESH
5b78493f
MH
7224 } else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
7225 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
7226 wpa_s, buf + 19, reply, reply_size);
7227 } else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
7228 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
7229 wpa_s, "", reply, reply_size);
603a3f34
JL
7230 } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
7231 if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
7232 reply_len = -1;
7233 } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
7234 if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
7235 buf + 18))
7236 reply_len = -1;
7237#endif /* CONFIG_MESH */
b563b388
JM
7238#ifdef CONFIG_P2P
7239 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
7240 if (p2p_ctrl_find(wpa_s, buf + 9))
7241 reply_len = -1;
7242 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
7243 if (p2p_ctrl_find(wpa_s, ""))
7244 reply_len = -1;
7245 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
7246 wpas_p2p_stop_find(wpa_s);
7247 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
7248 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
7249 reply_size);
7250 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
7251 if (p2p_ctrl_listen(wpa_s, buf + 11))
7252 reply_len = -1;
7253 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
7254 if (p2p_ctrl_listen(wpa_s, ""))
7255 reply_len = -1;
7256 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
7257 if (wpas_p2p_group_remove(wpa_s, buf + 17))
7258 reply_len = -1;
7259 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
20ea1ca4 7260 if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
b563b388
JM
7261 reply_len = -1;
7262 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
7263 if (p2p_ctrl_group_add(wpa_s, buf + 14))
7264 reply_len = -1;
7265 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
7266 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
7267 reply_len = -1;
7268 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
7269 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
7270 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
7271 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
7272 reply_size);
7273 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
7274 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
7275 reply_len = -1;
7276 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
7277 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
7278 reply_len = -1;
7279 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
7280 wpas_p2p_sd_service_update(wpa_s);
7281 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
7282 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
7283 reply_len = -1;
7284 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
7285 wpas_p2p_service_flush(wpa_s);
7286 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
7287 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
7288 reply_len = -1;
7289 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
7290 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
7291 reply_len = -1;
7292 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
7293 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
7294 reply_len = -1;
7295 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
7296 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
7297 reply_len = -1;
7298 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
7299 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
7300 reply_size);
7301 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
7302 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
7303 reply_len = -1;
7304 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
acb54643 7305 p2p_ctrl_flush(wpa_s);
9d562b79
SS
7306 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
7307 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
7308 reply_len = -1;
59eba7a2
JM
7309 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
7310 if (wpas_p2p_cancel(wpa_s))
7311 reply_len = -1;
b563b388
JM
7312 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
7313 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
7314 reply_len = -1;
7315 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
7316 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
7317 reply_len = -1;
7318 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
7319 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
7320 reply_len = -1;
7321 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
7322 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
7323 reply_len = -1;
f2c56602
JM
7324 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
7325 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
7326 reply_len = -1;
b563b388 7327#endif /* CONFIG_P2P */
9675ce35
JM
7328#ifdef CONFIG_WIFI_DISPLAY
7329 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
7330 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
7331 reply_len = -1;
7332 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
7333 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
7334 reply, reply_size);
7335#endif /* CONFIG_WIFI_DISPLAY */
afc064fe
JM
7336#ifdef CONFIG_INTERWORKING
7337 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
7338 if (interworking_fetch_anqp(wpa_s) < 0)
7339 reply_len = -1;
7340 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
7341 interworking_stop_fetch_anqp(wpa_s);
356d1488
JM
7342 } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
7343 if (ctrl_interworking_select(wpa_s, NULL) < 0)
7344 reply_len = -1;
7345 } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
7346 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
b02fe7ff
JM
7347 reply_len = -1;
7348 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
7349 if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
7350 reply_len = -1;
afc064fe
JM
7351 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
7352 if (get_anqp(wpa_s, buf + 9) < 0)
7353 reply_len = -1;
b1f12296
JM
7354 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
7355 if (gas_request(wpa_s, buf + 12) < 0)
7356 reply_len = -1;
7357 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
7358 reply_len = gas_response_get(wpa_s, buf + 17, reply,
7359 reply_size);
afc064fe 7360#endif /* CONFIG_INTERWORKING */
a8918e86
JK
7361#ifdef CONFIG_HS20
7362 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
7363 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
7364 reply_len = -1;
7365 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
7366 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
7367 reply_len = -1;
184e110c
JM
7368 } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
7369 if (hs20_icon_request(wpa_s, buf + 18) < 0)
7370 reply_len = -1;
b572df86
JM
7371 } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
7372 if (hs20_fetch_osu(wpa_s) < 0)
7373 reply_len = -1;
7374 } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
7375 hs20_cancel_fetch_osu(wpa_s);
a8918e86 7376#endif /* CONFIG_HS20 */
6fc6879b
JM
7377 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
7378 {
7379 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
7380 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
7381 reply_len = -1;
bceb8431
JM
7382 else {
7383 /*
7384 * Notify response from timeout to allow the control
7385 * interface response to be sent first.
7386 */
7387 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
7388 wpa_s, NULL);
7389 }
6fc6879b
JM
7390 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
7391 if (wpa_supplicant_reload_configuration(wpa_s))
7392 reply_len = -1;
7393 } else if (os_strcmp(buf, "TERMINATE") == 0) {
1a1bf008 7394 wpa_supplicant_terminate_proc(wpa_s->global);
6fc6879b
JM
7395 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
7396 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
7397 reply_len = -1;
9aa10e2b
DS
7398 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
7399 reply_len = wpa_supplicant_ctrl_iface_blacklist(
7400 wpa_s, buf + 9, reply, reply_size);
0597a5b5
DS
7401 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
7402 reply_len = wpa_supplicant_ctrl_iface_log_level(
7403 wpa_s, buf + 9, reply, reply_size);
90903a77
VD
7404 } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
7405 reply_len = wpa_supplicant_ctrl_iface_list_networks(
7406 wpa_s, buf + 14, reply, reply_size);
6fc6879b
JM
7407 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
7408 reply_len = wpa_supplicant_ctrl_iface_list_networks(
90903a77 7409 wpa_s, NULL, reply, reply_size);
6fc6879b 7410 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
83df8149
JM
7411#ifdef CONFIG_SME
7412 wpa_s->sme.prev_bssid_set = 0;
7413#endif /* CONFIG_SME */
6fc6879b
JM
7414 wpa_s->reassociate = 0;
7415 wpa_s->disconnected = 1;
6ad9c911 7416 wpa_supplicant_cancel_sched_scan(wpa_s);
d7ded758 7417 wpa_supplicant_cancel_scan(wpa_s);
cf4783e3
JM
7418 wpa_supplicant_deauthenticate(wpa_s,
7419 WLAN_REASON_DEAUTH_LEAVING);
fee52342
JM
7420 } else if (os_strcmp(buf, "SCAN") == 0) {
7421 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
7422 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
7423 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
6fc6879b
JM
7424 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
7425 reply_len = wpa_supplicant_ctrl_iface_scan_results(
7426 wpa_s, reply, reply_size);
7427 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
7428 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
7429 reply_len = -1;
7430 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
7431 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
7432 reply_len = -1;
7433 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
7434 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
7435 reply_len = -1;
7436 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
7437 reply_len = wpa_supplicant_ctrl_iface_add_network(
7438 wpa_s, reply, reply_size);
7439 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
7440 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
7441 reply_len = -1;
7442 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
7443 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
7444 reply_len = -1;
7445 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
7446 reply_len = wpa_supplicant_ctrl_iface_get_network(
7447 wpa_s, buf + 12, reply, reply_size);
1c330a2f
DS
7448 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
7449 if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12))
7450 reply_len = -1;
d94c9ee6
JM
7451 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
7452 reply_len = wpa_supplicant_ctrl_iface_list_creds(
7453 wpa_s, reply, reply_size);
7454 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
7455 reply_len = wpa_supplicant_ctrl_iface_add_cred(
7456 wpa_s, reply, reply_size);
7457 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
7458 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
7459 reply_len = -1;
7460 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
7461 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
7462 reply_len = -1;
c880ab87
JM
7463 } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
7464 reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
7465 reply,
7466 reply_size);
6fc6879b
JM
7467#ifndef CONFIG_NO_CONFIG_WRITE
7468 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
7469 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
7470 reply_len = -1;
7471#endif /* CONFIG_NO_CONFIG_WRITE */
7472 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
7473 reply_len = wpa_supplicant_ctrl_iface_get_capability(
7474 wpa_s, buf + 15, reply, reply_size);
7475 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
7476 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
7477 reply_len = -1;
67b9bd08
DS
7478 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
7479 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
7480 reply_len = -1;
4b4a8ae5
JM
7481 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
7482 reply_len = wpa_supplicant_global_iface_list(
7483 wpa_s->global, reply, reply_size);
6fc6879b
JM
7484 } else if (os_strcmp(buf, "INTERFACES") == 0) {
7485 reply_len = wpa_supplicant_global_iface_interfaces(
7486 wpa_s->global, reply, reply_size);
7487 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
7488 reply_len = wpa_supplicant_ctrl_iface_bss(
7489 wpa_s, buf + 4, reply, reply_size);
e653b622
JM
7490#ifdef CONFIG_AP
7491 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
7492 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
7493 } else if (os_strncmp(buf, "STA ", 4) == 0) {
7494 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
7495 reply_size);
7496 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
7497 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
7498 reply_size);
e60b2951
JJ
7499 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
7500 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
7501 reply_len = -1;
7502 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
7503 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
7504 reply_len = -1;
334bf36a
AO
7505 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
7506 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
7507 reply_len = -1;
e653b622 7508#endif /* CONFIG_AP */
207ef3fb
JM
7509 } else if (os_strcmp(buf, "SUSPEND") == 0) {
7510 wpas_notify_suspend(wpa_s->global);
7511 } else if (os_strcmp(buf, "RESUME") == 0) {
7512 wpas_notify_resume(wpa_s->global);
9ff4de6d 7513#ifdef CONFIG_TESTING_OPTIONS
32d5295f
JM
7514 } else if (os_strcmp(buf, "DROP_SA") == 0) {
7515 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
9ff4de6d 7516#endif /* CONFIG_TESTING_OPTIONS */
86d4f806
JM
7517 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
7518 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
7519 reply_len = -1;
0d0a8ca1 7520 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
5407c69d 7521 wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
78633c37
SL
7522 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
7523 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
7524 reply_len = -1;
7525 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
7526 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
7527 buf + 17))
7528 reply_len = -1;
39ee845f 7529 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
a1144000 7530 wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
281ff0aa
GP
7531#ifdef CONFIG_TDLS
7532 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
7533 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
7534 reply_len = -1;
7535 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
7536 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
7537 reply_len = -1;
7538 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
7539 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
7540 reply_len = -1;
7541#endif /* CONFIG_TDLS */
8506ea6f
MB
7542 } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
7543 reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
eb2f2088
MB
7544 } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
7545 if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
7546 reply_len = -1;
7547 } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
7548 if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
7549 reply_len = -1;
60b24b0d
DS
7550 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
7551 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
7552 reply_size);
dc7785f8
YZ
7553 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
7554 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
7555 reply_size);
bc5d330a
TB
7556#ifdef CONFIG_AUTOSCAN
7557 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
7558 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
7559 reply_len = -1;
7560#endif /* CONFIG_AUTOSCAN */
5e2c3490
JM
7561#ifdef ANDROID
7562 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
7563 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
7564 reply_size);
7565#endif /* ANDROID */
adef8948
BL
7566 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
7567 reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
7568 reply_size);
9482426e 7569 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
f5f37d3a 7570 pmksa_cache_clear_current(wpa_s->wpa);
9482426e 7571 eapol_sm_request_reauth(wpa_s->eapol);
e9199e31
JM
7572#ifdef CONFIG_WNM
7573 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
7574 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
7575 reply_len = -1;
65bcd0a9
VK
7576 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
7577 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
7578 reply_len = -1;
e9199e31 7579#endif /* CONFIG_WNM */
acb54643
JM
7580 } else if (os_strcmp(buf, "FLUSH") == 0) {
7581 wpa_supplicant_ctrl_iface_flush(wpa_s);
1f965e62
JM
7582 } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
7583 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
7584 reply_size);
60b893df
JM
7585#ifdef CONFIG_TESTING_OPTIONS
7586 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
7587 if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
7588 reply_len = -1;
7589 } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
7590 wpas_ctrl_iface_mgmt_tx_done(wpa_s);
ad12f2f4
JM
7591 } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
7592 if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
7593 reply_len = -1;
9d4ff04a
JM
7594 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
7595 if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
7596 reply_len = -1;
4a6cc862
JM
7597 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
7598 if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
7599 reply_len = -1;
7600 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
7601 if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
7602 reply_len = -1;
fc0ef7c0
JM
7603 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
7604 if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
7605 reply_len = -1;
60b893df 7606#endif /* CONFIG_TESTING_OPTIONS */
86bd36f0
JM
7607 } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
7608 if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
7609 reply_len = -1;
7610 } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
7611 reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
7612 reply_size);
7613 } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
7614 if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
7615 reply_len = -1;
f4b8bfae 7616 } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
4c4b2305 7617 if (wpas_ctrl_iface_send_neigbor_rep(wpa_s, buf + 20))
f4b8bfae 7618 reply_len = -1;
65d9a5e2
JM
7619 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
7620 wpas_ctrl_iface_erp_flush(wpa_s);
6fc6879b
JM
7621 } else {
7622 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
7623 reply_len = 16;
7624 }
7625
7626 if (reply_len < 0) {
7627 os_memcpy(reply, "FAIL\n", 5);
7628 reply_len = 5;
7629 }
7630
6fc6879b
JM
7631 *resp_len = reply_len;
7632 return reply;
7633}
7634
7635
7636static int wpa_supplicant_global_iface_add(struct wpa_global *global,
7637 char *cmd)
7638{
7639 struct wpa_interface iface;
7640 char *pos;
7641
7642 /*
7643 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
7644 * TAB<bridge_ifname>
7645 */
7646 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
7647
7648 os_memset(&iface, 0, sizeof(iface));
7649
7650 do {
7651 iface.ifname = pos = cmd;
7652 pos = os_strchr(pos, '\t');
7653 if (pos)
7654 *pos++ = '\0';
7655 if (iface.ifname[0] == '\0')
7656 return -1;
7657 if (pos == NULL)
7658 break;
7659
7660 iface.confname = pos;
7661 pos = os_strchr(pos, '\t');
7662 if (pos)
7663 *pos++ = '\0';
7664 if (iface.confname[0] == '\0')
7665 iface.confname = NULL;
7666 if (pos == NULL)
7667 break;
7668
7669 iface.driver = pos;
7670 pos = os_strchr(pos, '\t');
7671 if (pos)
7672 *pos++ = '\0';
7673 if (iface.driver[0] == '\0')
7674 iface.driver = NULL;
7675 if (pos == NULL)
7676 break;
7677
7678 iface.ctrl_interface = pos;
7679 pos = os_strchr(pos, '\t');
7680 if (pos)
7681 *pos++ = '\0';
7682 if (iface.ctrl_interface[0] == '\0')
7683 iface.ctrl_interface = NULL;
7684 if (pos == NULL)
7685 break;
7686
7687 iface.driver_param = pos;
7688 pos = os_strchr(pos, '\t');
7689 if (pos)
7690 *pos++ = '\0';
7691 if (iface.driver_param[0] == '\0')
7692 iface.driver_param = NULL;
7693 if (pos == NULL)
7694 break;
7695
7696 iface.bridge_ifname = pos;
7697 pos = os_strchr(pos, '\t');
7698 if (pos)
7699 *pos++ = '\0';
7700 if (iface.bridge_ifname[0] == '\0')
7701 iface.bridge_ifname = NULL;
7702 if (pos == NULL)
7703 break;
7704 } while (0);
7705
7706 if (wpa_supplicant_get_iface(global, iface.ifname))
7707 return -1;
7708
7709 return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
7710}
7711
7712
7713static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
7714 char *cmd)
7715{
7716 struct wpa_supplicant *wpa_s;
7717
7718 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
7719
7720 wpa_s = wpa_supplicant_get_iface(global, cmd);
7721 if (wpa_s == NULL)
7722 return -1;
df509539 7723 return wpa_supplicant_remove_iface(global, wpa_s, 0);
6fc6879b
JM
7724}
7725
7726
4b4a8ae5
JM
7727static void wpa_free_iface_info(struct wpa_interface_info *iface)
7728{
7729 struct wpa_interface_info *prev;
7730
7731 while (iface) {
7732 prev = iface;
7733 iface = iface->next;
7734
7735 os_free(prev->ifname);
7736 os_free(prev->desc);
7737 os_free(prev);
7738 }
7739}
7740
7741
7742static int wpa_supplicant_global_iface_list(struct wpa_global *global,
7743 char *buf, int len)
7744{
7745 int i, res;
7746 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
7747 char *pos, *end;
7748
c5121837
JM
7749 for (i = 0; wpa_drivers[i]; i++) {
7750 struct wpa_driver_ops *drv = wpa_drivers[i];
4b4a8ae5
JM
7751 if (drv->get_interfaces == NULL)
7752 continue;
5fbc1f27 7753 tmp = drv->get_interfaces(global->drv_priv[i]);
4b4a8ae5
JM
7754 if (tmp == NULL)
7755 continue;
7756
7757 if (last == NULL)
7758 iface = last = tmp;
7759 else
7760 last->next = tmp;
7761 while (last->next)
7762 last = last->next;
7763 }
7764
7765 pos = buf;
7766 end = buf + len;
7767 for (tmp = iface; tmp; tmp = tmp->next) {
7768 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
7769 tmp->drv_name, tmp->ifname,
7770 tmp->desc ? tmp->desc : "");
d85e1fc8 7771 if (os_snprintf_error(end - pos, res)) {
4b4a8ae5
JM
7772 *pos = '\0';
7773 break;
7774 }
7775 pos += res;
7776 }
7777
7778 wpa_free_iface_info(iface);
7779
7780 return pos - buf;
7781}
7782
7783
6fc6879b
JM
7784static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
7785 char *buf, int len)
7786{
7787 int res;
7788 char *pos, *end;
7789 struct wpa_supplicant *wpa_s;
7790
7791 wpa_s = global->ifaces;
7792 pos = buf;
7793 end = buf + len;
7794
7795 while (wpa_s) {
7796 res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
d85e1fc8 7797 if (os_snprintf_error(end - pos, res)) {
6fc6879b
JM
7798 *pos = '\0';
7799 break;
7800 }
7801 pos += res;
7802 wpa_s = wpa_s->next;
7803 }
7804 return pos - buf;
7805}
7806
7807
cf3bebf2
JM
7808static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
7809 const char *ifname,
7810 char *cmd, size_t *resp_len)
7811{
7812 struct wpa_supplicant *wpa_s;
7813
7814 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7815 if (os_strcmp(ifname, wpa_s->ifname) == 0)
7816 break;
7817 }
7818
7819 if (wpa_s == NULL) {
7820 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
7821 if (resp)
7822 *resp_len = os_strlen(resp);
7823 else
7824 *resp_len = 1;
7825 return resp;
7826 }
7827
7828 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
7829}
7830
7831
576bce9c
JM
7832static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
7833 char *buf, size_t *resp_len)
7834{
7835#ifdef CONFIG_P2P
7836 static const char * cmd[] = {
443427e4 7837 "LIST_NETWORKS",
576bce9c
JM
7838 "P2P_FIND",
7839 "P2P_STOP_FIND",
7840 "P2P_LISTEN",
7841 "P2P_GROUP_ADD",
7842 "P2P_GET_PASSPHRASE",
7843 "P2P_SERVICE_UPDATE",
7844 "P2P_SERVICE_FLUSH",
7845 "P2P_FLUSH",
7846 "P2P_CANCEL",
7847 "P2P_PRESENCE_REQ",
7848 "P2P_EXT_LISTEN",
7849 NULL
7850 };
7851 static const char * prefix[] = {
443427e4
DS
7852#ifdef ANDROID
7853 "DRIVER ",
7854#endif /* ANDROID */
7855 "GET_NETWORK ",
7856 "REMOVE_NETWORK ",
576bce9c
JM
7857 "P2P_FIND ",
7858 "P2P_CONNECT ",
7859 "P2P_LISTEN ",
7860 "P2P_GROUP_REMOVE ",
7861 "P2P_GROUP_ADD ",
7862 "P2P_PROV_DISC ",
7863 "P2P_SERV_DISC_REQ ",
7864 "P2P_SERV_DISC_CANCEL_REQ ",
7865 "P2P_SERV_DISC_RESP ",
7866 "P2P_SERV_DISC_EXTERNAL ",
7867 "P2P_SERVICE_ADD ",
7868 "P2P_SERVICE_DEL ",
7869 "P2P_REJECT ",
7870 "P2P_INVITE ",
7871 "P2P_PEER ",
7872 "P2P_SET ",
7873 "P2P_UNAUTHORIZE ",
7874 "P2P_PRESENCE_REQ ",
7875 "P2P_EXT_LISTEN ",
f2c56602 7876 "P2P_REMOVE_CLIENT ",
f3ff9487
AM
7877 "NFC_GET_HANDOVER_SEL ",
7878 "NFC_GET_HANDOVER_REQ ",
7879 "NFC_REPORT_HANDOVER ",
576bce9c
JM
7880 NULL
7881 };
7882 int found = 0;
7883 int i;
7884
7885 if (global->p2p_init_wpa_s == NULL)
7886 return NULL;
7887
7888 for (i = 0; !found && cmd[i]; i++) {
7889 if (os_strcmp(buf, cmd[i]) == 0)
7890 found = 1;
7891 }
7892
7893 for (i = 0; !found && prefix[i]; i++) {
7894 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
7895 found = 1;
7896 }
7897
7898 if (found)
7899 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
7900 buf, resp_len);
7901#endif /* CONFIG_P2P */
7902 return NULL;
7903}
7904
7905
7906static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
7907 char *buf, size_t *resp_len)
7908{
7909#ifdef CONFIG_WIFI_DISPLAY
7910 if (global->p2p_init_wpa_s == NULL)
7911 return NULL;
7912 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
7913 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
7914 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
7915 buf, resp_len);
7916#endif /* CONFIG_WIFI_DISPLAY */
7917 return NULL;
7918}
7919
7920
7921static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
7922 char *buf, size_t *resp_len)
7923{
7924 char *ret;
7925
7926 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
7927 if (ret)
7928 return ret;
7929
7930 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
7931 if (ret)
7932 return ret;
7933
7934 return NULL;
7935}
7936
7937
1b9b31c1
JM
7938static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
7939{
7940 char *value;
7941
7942 value = os_strchr(cmd, ' ');
7943 if (value == NULL)
7944 return -1;
7945 *value++ = '\0';
7946
7947 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
7948
7949#ifdef CONFIG_WIFI_DISPLAY
7950 if (os_strcasecmp(cmd, "wifi_display") == 0) {
7951 wifi_display_enable(global, !!atoi(value));
7952 return 0;
7953 }
7954#endif /* CONFIG_WIFI_DISPLAY */
7955
a7ca6dac
JM
7956 /* Restore cmd to its original value to allow redirection */
7957 value[-1] = ' ';
7958
1b9b31c1
JM
7959 return -1;
7960}
7961
7962
42868f14
JM
7963#ifndef CONFIG_NO_CONFIG_WRITE
7964static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
7965{
d6b818ef 7966 int ret = 0, saved = 0;
42868f14
JM
7967 struct wpa_supplicant *wpa_s;
7968
7969 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7970 if (!wpa_s->conf->update_config) {
7971 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
7972 continue;
7973 }
7974
7975 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
7976 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
7977 ret = 1;
7978 } else {
7979 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
d6b818ef 7980 saved++;
42868f14
JM
7981 }
7982 }
7983
d6b818ef
JM
7984 if (!saved && !ret) {
7985 wpa_dbg(wpa_s, MSG_DEBUG,
7986 "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
7987 ret = 1;
7988 }
7989
42868f14
JM
7990 return ret;
7991}
7992#endif /* CONFIG_NO_CONFIG_WRITE */
7993
7994
ae8c27f7
JM
7995static int wpas_global_ctrl_iface_status(struct wpa_global *global,
7996 char *buf, size_t buflen)
7997{
7998 char *pos, *end;
7999 int ret;
8000 struct wpa_supplicant *wpa_s;
8001
8002 pos = buf;
8003 end = buf + buflen;
8004
8005#ifdef CONFIG_P2P
4c559019 8006 if (global->p2p && !global->p2p_disabled) {
ae8c27f7 8007 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
4c559019
JM
8008 "\n"
8009 "p2p_state=%s\n",
8010 MAC2STR(global->p2p_dev_addr),
8011 p2p_get_state_txt(global->p2p));
d85e1fc8 8012 if (os_snprintf_error(end - pos, ret))
4c559019
JM
8013 return pos - buf;
8014 pos += ret;
8015 } else if (global->p2p) {
8016 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
d85e1fc8 8017 if (os_snprintf_error(end - pos, ret))
ae8c27f7
JM
8018 return pos - buf;
8019 pos += ret;
8020 }
8021#endif /* CONFIG_P2P */
8022
8023#ifdef CONFIG_WIFI_DISPLAY
8024 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
8025 !!global->wifi_display);
d85e1fc8 8026 if (os_snprintf_error(end - pos, ret))
ae8c27f7
JM
8027 return pos - buf;
8028 pos += ret;
8029#endif /* CONFIG_WIFI_DISPLAY */
8030
8031 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8032 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
8033 "address=" MACSTR "\n",
8034 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
d85e1fc8 8035 if (os_snprintf_error(end - pos, ret))
ae8c27f7
JM
8036 return pos - buf;
8037 pos += ret;
8038 }
8039
8040 return pos - buf;
8041}
8042
8043
6fc6879b
JM
8044char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
8045 char *buf, size_t *resp_len)
8046{
8047 char *reply;
8048 const int reply_size = 2048;
8049 int reply_len;
f4a0a82c 8050 int level = MSG_DEBUG;
6fc6879b 8051
cf3bebf2
JM
8052 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
8053 char *pos = os_strchr(buf + 7, ' ');
8054 if (pos) {
8055 *pos++ = '\0';
8056 return wpas_global_ctrl_iface_ifname(global,
8057 buf + 7, pos,
8058 resp_len);
8059 }
8060 }
8061
576bce9c
JM
8062 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
8063 if (reply)
8064 return reply;
8065
f4a0a82c
JM
8066 if (os_strcmp(buf, "PING") == 0)
8067 level = MSG_EXCESSIVE;
8068 wpa_hexdump_ascii(level, "RX global ctrl_iface",
6fc6879b
JM
8069 (const u8 *) buf, os_strlen(buf));
8070
8071 reply = os_malloc(reply_size);
8072 if (reply == NULL) {
8073 *resp_len = 1;
8074 return NULL;
8075 }
8076
8077 os_memcpy(reply, "OK\n", 3);
8078 reply_len = 3;
8079
8080 if (os_strcmp(buf, "PING") == 0) {
8081 os_memcpy(reply, "PONG\n", 5);
8082 reply_len = 5;
8083 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
8084 if (wpa_supplicant_global_iface_add(global, buf + 14))
8085 reply_len = -1;
8086 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
8087 if (wpa_supplicant_global_iface_remove(global, buf + 17))
8088 reply_len = -1;
4b4a8ae5
JM
8089 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
8090 reply_len = wpa_supplicant_global_iface_list(
8091 global, reply, reply_size);
6fc6879b
JM
8092 } else if (os_strcmp(buf, "INTERFACES") == 0) {
8093 reply_len = wpa_supplicant_global_iface_interfaces(
8094 global, reply, reply_size);
8095 } else if (os_strcmp(buf, "TERMINATE") == 0) {
1a1bf008 8096 wpa_supplicant_terminate_proc(global);
207ef3fb
JM
8097 } else if (os_strcmp(buf, "SUSPEND") == 0) {
8098 wpas_notify_suspend(global);
8099 } else if (os_strcmp(buf, "RESUME") == 0) {
8100 wpas_notify_resume(global);
1b9b31c1 8101 } else if (os_strncmp(buf, "SET ", 4) == 0) {
a7ca6dac
JM
8102 if (wpas_global_ctrl_iface_set(global, buf + 4)) {
8103#ifdef CONFIG_P2P
8104 if (global->p2p_init_wpa_s) {
8105 os_free(reply);
8106 /* Check if P2P redirection would work for this
8107 * command. */
8108 return wpa_supplicant_ctrl_iface_process(
8109 global->p2p_init_wpa_s,
8110 buf, resp_len);
8111 }
8112#endif /* CONFIG_P2P */
1b9b31c1 8113 reply_len = -1;
a7ca6dac 8114 }
42868f14
JM
8115#ifndef CONFIG_NO_CONFIG_WRITE
8116 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
8117 if (wpas_global_ctrl_iface_save_config(global))
8118 reply_len = -1;
8119#endif /* CONFIG_NO_CONFIG_WRITE */
ae8c27f7
JM
8120 } else if (os_strcmp(buf, "STATUS") == 0) {
8121 reply_len = wpas_global_ctrl_iface_status(global, reply,
8122 reply_size);
ea449b5b
JM
8123#ifdef CONFIG_MODULE_TESTS
8124 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
8125 int wpas_module_tests(void);
8126 if (wpas_module_tests() < 0)
8127 reply_len = -1;
8128#endif /* CONFIG_MODULE_TESTS */
5f797376
JM
8129 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
8130 if (wpa_debug_reopen_file() < 0)
8131 reply_len = -1;
6fc6879b
JM
8132 } else {
8133 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
8134 reply_len = 16;
8135 }
8136
8137 if (reply_len < 0) {
8138 os_memcpy(reply, "FAIL\n", 5);
8139 reply_len = 5;
8140 }
8141
8142 *resp_len = reply_len;
8143 return reply;
8144}