]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/ctrl_iface.c
hostapd_cli: Add completion for get command
[thirdparty/hostap.git] / wpa_supplicant / ctrl_iface.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant / Control interface (shared code for all backends)
5e3b5197 3 * Copyright (c) 2004-2015, 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"
50a17a76 18#include "utils/module_tests.h"
acec8d32 19#include "common/version.h"
3a068632 20#include "common/ieee802_11_defs.h"
337c781f 21#include "common/ieee802_11_common.h"
3a068632 22#include "common/wpa_ctrl.h"
a1651451 23#include "crypto/tls.h"
9d4ff04a 24#include "ap/hostapd.h"
3a068632
JM
25#include "eap_peer/eap.h"
26#include "eapol_supp/eapol_supp_sm.h"
3acb5005 27#include "rsn_supp/wpa.h"
3a068632
JM
28#include "rsn_supp/preauth.h"
29#include "rsn_supp/pmksa_cache.h"
30#include "l2_packet/l2_packet.h"
31#include "wps/wps.h"
df4cea89 32#include "fst/fst.h"
3794af2d 33#include "fst/fst_ctrl_iface.h"
6fc6879b 34#include "config.h"
6fc6879b 35#include "wpa_supplicant_i.h"
2d5b792d 36#include "driver_i.h"
fcc60db4 37#include "wps_supplicant.h"
11ef8d35 38#include "ibss_rsn.h"
3ec97afe 39#include "ap.h"
b563b388
JM
40#include "p2p_supplicant.h"
41#include "p2p/p2p.h"
a8918e86 42#include "hs20_supplicant.h"
9675ce35 43#include "wifi_display.h"
8bac466b 44#include "notify.h"
3a068632 45#include "bss.h"
9ba9fa07 46#include "scan.h"
3a068632 47#include "ctrl_iface.h"
afc064fe 48#include "interworking.h"
9aa10e2b 49#include "blacklist.h"
bc5d330a 50#include "autoscan.h"
e9199e31 51#include "wnm_sta.h"
60b893df 52#include "offchannel.h"
7a4a93b9 53#include "drivers/driver.h"
79070906 54#include "mesh.h"
6fc6879b 55
4b4a8ae5
JM
56static int wpa_supplicant_global_iface_list(struct wpa_global *global,
57 char *buf, int len);
6fc6879b 58static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
56e2fc2c 59 const char *input,
6fc6879b 60 char *buf, int len);
d3c9c35f
DS
61static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
62 char *val);
6fc6879b 63
d445a5cd
JM
64static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
65{
66 char *pos;
67 u8 addr[ETH_ALEN], *filter = NULL, *n;
68 size_t count = 0;
69
70 pos = val;
71 while (pos) {
72 if (*pos == '\0')
73 break;
1485ec07
JM
74 if (hwaddr_aton(pos, addr)) {
75 os_free(filter);
d445a5cd 76 return -1;
1485ec07 77 }
067ffa26 78 n = os_realloc_array(filter, count + 1, ETH_ALEN);
d445a5cd
JM
79 if (n == NULL) {
80 os_free(filter);
81 return -1;
82 }
83 filter = n;
84 os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
85 count++;
86
87 pos = os_strchr(pos, ' ');
88 if (pos)
89 pos++;
90 }
91
92 wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
93 os_free(wpa_s->bssid_filter);
94 wpa_s->bssid_filter = filter;
95 wpa_s->bssid_filter_count = count;
96
97 return 0;
98}
99
100
6407f413
JM
101static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
102{
103 char *pos;
104 u8 addr[ETH_ALEN], *bssid = NULL, *n;
105 struct wpa_ssid_value *ssid = NULL, *ns;
106 size_t count = 0, ssid_count = 0;
107 struct wpa_ssid *c;
108
109 /*
65015b2d 110 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
6407f413
JM
111 * SSID_SPEC ::= ssid <SSID_HEX>
112 * BSSID_SPEC ::= bssid <BSSID_HEX>
113 */
114
115 pos = val;
116 while (pos) {
117 if (*pos == '\0')
118 break;
119 if (os_strncmp(pos, "bssid ", 6) == 0) {
120 int res;
121 pos += 6;
122 res = hwaddr_aton2(pos, addr);
123 if (res < 0) {
124 os_free(ssid);
125 os_free(bssid);
126 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
127 "BSSID value '%s'", pos);
128 return -1;
129 }
130 pos += res;
131 n = os_realloc_array(bssid, count + 1, ETH_ALEN);
132 if (n == NULL) {
133 os_free(ssid);
134 os_free(bssid);
135 return -1;
136 }
137 bssid = n;
138 os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
139 count++;
140 } else if (os_strncmp(pos, "ssid ", 5) == 0) {
141 char *end;
142 pos += 5;
143
144 end = pos;
145 while (*end) {
146 if (*end == '\0' || *end == ' ')
147 break;
148 end++;
149 }
150
151 ns = os_realloc_array(ssid, ssid_count + 1,
152 sizeof(struct wpa_ssid_value));
153 if (ns == NULL) {
154 os_free(ssid);
155 os_free(bssid);
156 return -1;
157 }
158 ssid = ns;
159
d9d1b952
JM
160 if ((end - pos) & 0x01 ||
161 end - pos > 2 * SSID_MAX_LEN ||
6407f413
JM
162 hexstr2bin(pos, ssid[ssid_count].ssid,
163 (end - pos) / 2) < 0) {
164 os_free(ssid);
165 os_free(bssid);
166 wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
167 "SSID value '%s'", pos);
168 return -1;
169 }
170 ssid[ssid_count].ssid_len = (end - pos) / 2;
171 wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
172 ssid[ssid_count].ssid,
173 ssid[ssid_count].ssid_len);
174 ssid_count++;
175 pos = end;
176 } else {
177 wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
178 "'%s'", pos);
179 os_free(ssid);
180 os_free(bssid);
181 return -1;
182 }
183
184 pos = os_strchr(pos, ' ');
185 if (pos)
186 pos++;
187 }
188
189 wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
190 os_free(wpa_s->disallow_aps_bssid);
191 wpa_s->disallow_aps_bssid = bssid;
192 wpa_s->disallow_aps_bssid_count = count;
193
194 wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
195 os_free(wpa_s->disallow_aps_ssid);
196 wpa_s->disallow_aps_ssid = ssid;
197 wpa_s->disallow_aps_ssid_count = ssid_count;
198
199 if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
200 return 0;
201
202 c = wpa_s->current_ssid;
203 if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
204 return 0;
205
206 if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
207 !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
208 return 0;
209
210 wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
211 "because current AP was marked disallowed");
212
213#ifdef CONFIG_SME
214 wpa_s->sme.prev_bssid_set = 0;
215#endif /* CONFIG_SME */
216 wpa_s->reassociate = 1;
c2805909 217 wpa_s->own_disconnect_req = 1;
6407f413
JM
218 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
219 wpa_supplicant_req_scan(wpa_s, 0, 0);
220
221 return 0;
222}
223
224
1120e452
JM
225#ifndef CONFIG_NO_CONFIG_BLOBS
226static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
227{
228 char *name = pos;
229 struct wpa_config_blob *blob;
230 size_t len;
231
232 pos = os_strchr(pos, ' ');
233 if (pos == NULL)
234 return -1;
235 *pos++ = '\0';
236 len = os_strlen(pos);
237 if (len & 1)
238 return -1;
239
240 wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
241 blob = os_zalloc(sizeof(*blob));
242 if (blob == NULL)
243 return -1;
244 blob->name = os_strdup(name);
245 blob->data = os_malloc(len / 2);
246 if (blob->name == NULL || blob->data == NULL) {
247 wpa_config_free_blob(blob);
248 return -1;
249 }
250
251 if (hexstr2bin(pos, blob->data, len / 2) < 0) {
252 wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
253 wpa_config_free_blob(blob);
254 return -1;
255 }
256 blob->len = len / 2;
257
258 wpa_config_set_blob(wpa_s->conf, blob);
259
260 return 0;
261}
262#endif /* CONFIG_NO_CONFIG_BLOBS */
263
d3c9c35f
DS
264
265static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
266{
267 char *params;
268 char *pos;
269 int *freqs = NULL;
270 int ret;
271
272 if (atoi(cmd)) {
273 params = os_strchr(cmd, ' ');
274 os_free(wpa_s->manual_sched_scan_freqs);
275 if (params) {
276 params++;
277 pos = os_strstr(params, "freq=");
278 if (pos)
279 freqs = freq_range_to_channel_list(wpa_s,
280 pos + 5);
281 }
282 wpa_s->manual_sched_scan_freqs = freqs;
283 ret = wpas_start_pno(wpa_s);
284 } else {
285 ret = wpas_stop_pno(wpa_s);
286 }
287 return ret;
288}
289
290
844dfeb8
SD
291static int wpas_ctrl_set_band(struct wpa_supplicant *wpa_s, char *band)
292{
293 union wpa_event_data event;
294
295 if (os_strcmp(band, "AUTO") == 0)
296 wpa_s->setband = WPA_SETBAND_AUTO;
297 else if (os_strcmp(band, "5G") == 0)
298 wpa_s->setband = WPA_SETBAND_5G;
299 else if (os_strcmp(band, "2G") == 0)
300 wpa_s->setband = WPA_SETBAND_2G;
301 else
302 return -1;
303
304 if (wpa_drv_setband(wpa_s, wpa_s->setband) == 0) {
305 os_memset(&event, 0, sizeof(event));
306 event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
307 event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
308 wpa_supplicant_event(wpa_s, EVENT_CHANNEL_LIST_CHANGED, &event);
309 }
310
311 return 0;
312}
313
314
4a742011
DS
315static int wpas_ctrl_iface_set_lci(struct wpa_supplicant *wpa_s,
316 const char *cmd)
317{
318 struct wpabuf *lci;
319
320 if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
321 wpabuf_free(wpa_s->lci);
322 wpa_s->lci = NULL;
323 return 0;
324 }
325
326 lci = wpabuf_parse_bin(cmd);
327 if (!lci)
328 return -1;
329
330 if (os_get_reltime(&wpa_s->lci_time)) {
331 wpabuf_free(lci);
332 return -1;
333 }
334
335 wpabuf_free(wpa_s->lci);
336 wpa_s->lci = lci;
337
338 return 0;
339}
340
341
6fc6879b
JM
342static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
343 char *cmd)
344{
345 char *value;
346 int ret = 0;
347
348 value = os_strchr(cmd, ' ');
349 if (value == NULL)
350 return -1;
351 *value++ = '\0';
352
353 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
354 if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
355 eapol_sm_configure(wpa_s->eapol,
356 atoi(value), -1, -1, -1);
357 } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
358 eapol_sm_configure(wpa_s->eapol,
359 -1, atoi(value), -1, -1);
360 } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
361 eapol_sm_configure(wpa_s->eapol,
362 -1, -1, atoi(value), -1);
363 } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
364 eapol_sm_configure(wpa_s->eapol,
365 -1, -1, -1, atoi(value));
366 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
367 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
368 atoi(value)))
369 ret = -1;
370 } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
371 0) {
372 if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
373 atoi(value)))
374 ret = -1;
375 } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
376 if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
377 ret = -1;
42f50264
JM
378 } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
379 wpa_s->wps_fragment_size = atoi(value);
b4e34f2f
JM
380#ifdef CONFIG_WPS_TESTING
381 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
382 long int val;
383 val = strtol(value, NULL, 0);
384 if (val < 0 || val > 0xff) {
385 ret = -1;
386 wpa_printf(MSG_DEBUG, "WPS: Invalid "
387 "wps_version_number %ld", val);
388 } else {
389 wps_version_number = val;
390 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
391 "version %u.%u",
392 (wps_version_number & 0xf0) >> 4,
393 wps_version_number & 0x0f);
394 }
395 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
396 wps_testing_dummy_cred = atoi(value);
397 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
398 wps_testing_dummy_cred);
91226e0d
JM
399 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
400 wps_corrupt_pkhash = atoi(value);
401 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
402 wps_corrupt_pkhash);
6e379c6c
JM
403 } else if (os_strcasecmp(cmd, "wps_force_auth_types") == 0) {
404 if (value[0] == '\0') {
405 wps_force_auth_types_in_use = 0;
406 } else {
407 wps_force_auth_types = strtol(value, NULL, 0);
408 wps_force_auth_types_in_use = 1;
409 }
410 } else if (os_strcasecmp(cmd, "wps_force_encr_types") == 0) {
411 if (value[0] == '\0') {
412 wps_force_encr_types_in_use = 0;
413 } else {
414 wps_force_encr_types = strtol(value, NULL, 0);
415 wps_force_encr_types_in_use = 1;
416 }
b4e34f2f 417#endif /* CONFIG_WPS_TESTING */
b6c79a99
JM
418 } else if (os_strcasecmp(cmd, "ampdu") == 0) {
419 if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
420 ret = -1;
9d2cb3ec 421#ifdef CONFIG_TDLS
5b0e6ece
JM
422#ifdef CONFIG_TDLS_TESTING
423 } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
5b0e6ece
JM
424 tdls_testing = strtol(value, NULL, 0);
425 wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
426#endif /* CONFIG_TDLS_TESTING */
b8f64582
JM
427 } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
428 int disabled = atoi(value);
429 wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
430 if (disabled) {
431 if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
432 ret = -1;
433 } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
434 ret = -1;
435 wpa_tdls_enable(wpa_s->wpa, !disabled);
436#endif /* CONFIG_TDLS */
b5c68312 437 } else if (os_strcasecmp(cmd, "pno") == 0) {
d3c9c35f 438 ret = wpas_ctrl_pno(wpa_s, value);
8b9d0bfa
JM
439 } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
440 int disabled = atoi(value);
441 if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
442 ret = -1;
443 else if (disabled)
444 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
aa074a64
JM
445 } else if (os_strcasecmp(cmd, "uapsd") == 0) {
446 if (os_strcmp(value, "disable") == 0)
447 wpa_s->set_sta_uapsd = 0;
448 else {
449 int be, bk, vi, vo;
450 char *pos;
451 /* format: BE,BK,VI,VO;max SP Length */
452 be = atoi(value);
453 pos = os_strchr(value, ',');
454 if (pos == NULL)
455 return -1;
456 pos++;
457 bk = atoi(pos);
458 pos = os_strchr(pos, ',');
459 if (pos == NULL)
460 return -1;
461 pos++;
462 vi = atoi(pos);
463 pos = os_strchr(pos, ',');
464 if (pos == NULL)
465 return -1;
466 pos++;
467 vo = atoi(pos);
468 /* ignore max SP Length for now */
469
470 wpa_s->set_sta_uapsd = 1;
471 wpa_s->sta_uapsd = 0;
472 if (be)
473 wpa_s->sta_uapsd |= BIT(0);
474 if (bk)
475 wpa_s->sta_uapsd |= BIT(1);
476 if (vi)
477 wpa_s->sta_uapsd |= BIT(2);
478 if (vo)
479 wpa_s->sta_uapsd |= BIT(3);
480 }
b2ff1681
JM
481 } else if (os_strcasecmp(cmd, "ps") == 0) {
482 ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
9675ce35
JM
483#ifdef CONFIG_WIFI_DISPLAY
484 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
bab6677a
JM
485 int enabled = !!atoi(value);
486 if (enabled && !wpa_s->global->p2p)
487 ret = -1;
488 else
489 wifi_display_enable(wpa_s->global, enabled);
9675ce35 490#endif /* CONFIG_WIFI_DISPLAY */
d445a5cd
JM
491 } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
492 ret = set_bssid_filter(wpa_s, value);
6407f413
JM
493 } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
494 ret = set_disallow_aps(wpa_s, value);
2ec535fd
JM
495 } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
496 wpa_s->no_keep_alive = !!atoi(value);
60b893df
JM
497#ifdef CONFIG_TESTING_OPTIONS
498 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
499 wpa_s->ext_mgmt_frame_handling = !!atoi(value);
9d4ff04a
JM
500 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
501 wpa_s->ext_eapol_frame_io = !!atoi(value);
502#ifdef CONFIG_AP
503 if (wpa_s->ap_iface) {
504 wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
505 wpa_s->ext_eapol_frame_io;
506 }
507#endif /* CONFIG_AP */
1f94e4ee
JM
508 } else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
509 wpa_s->extra_roc_dur = atoi(value);
911942ee
JM
510 } else if (os_strcasecmp(cmd, "test_failure") == 0) {
511 wpa_s->test_failure = atoi(value);
ed7820b4
IP
512 } else if (os_strcasecmp(cmd, "p2p_go_csa_on_inv") == 0) {
513 wpa_s->p2p_go_csa_on_inv = !!atoi(value);
02adead5
MK
514 } else if (os_strcasecmp(cmd, "ignore_auth_resp") == 0) {
515 wpa_s->ignore_auth_resp = !!atoi(value);
6ad37d73 516 } else if (os_strcasecmp(cmd, "ignore_assoc_disallow") == 0) {
517 wpa_s->ignore_assoc_disallow = !!atoi(value);
a483c6f1 518 } else if (os_strcasecmp(cmd, "reject_btm_req_reason") == 0) {
519 wpa_s->reject_btm_req_reason = atoi(value);
60b893df 520#endif /* CONFIG_TESTING_OPTIONS */
1120e452
JM
521#ifndef CONFIG_NO_CONFIG_BLOBS
522 } else if (os_strcmp(cmd, "blob") == 0) {
523 ret = wpas_ctrl_set_blob(wpa_s, value);
524#endif /* CONFIG_NO_CONFIG_BLOBS */
209702d4 525 } else if (os_strcasecmp(cmd, "setband") == 0) {
844dfeb8 526 ret = wpas_ctrl_set_band(wpa_s, value);
92c6e2e3
DS
527#ifdef CONFIG_MBO
528 } else if (os_strcasecmp(cmd, "non_pref_chan") == 0) {
529 ret = wpas_mbo_update_non_pref_chan(wpa_s, value);
016082e9
AS
530 } else if (os_strcasecmp(cmd, "mbo_cell_capa") == 0) {
531 wpas_mbo_update_cell_capa(wpa_s, atoi(value));
92c6e2e3 532#endif /* CONFIG_MBO */
4a742011
DS
533 } else if (os_strcasecmp(cmd, "lci") == 0) {
534 ret = wpas_ctrl_iface_set_lci(wpa_s, value);
2e4e4fb7
SD
535 } else if (os_strcasecmp(cmd, "tdls_trigger_control") == 0) {
536 ret = wpa_drv_set_tdls_mode(wpa_s, atoi(value));
611aea7d
JM
537 } else {
538 value[-1] = '=';
539 ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
540 if (ret == 0)
541 wpa_supplicant_update_config(wpa_s);
542 }
6fc6879b
JM
543
544 return ret;
545}
546
547
acec8d32
JM
548static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
549 char *cmd, char *buf, size_t buflen)
550{
6ce937b8 551 int res = -1;
acec8d32
JM
552
553 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
554
555 if (os_strcmp(cmd, "version") == 0) {
556 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
6ce937b8
DS
557 } else if (os_strcasecmp(cmd, "country") == 0) {
558 if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
559 res = os_snprintf(buf, buflen, "%c%c",
560 wpa_s->conf->country[0],
561 wpa_s->conf->country[1]);
9675ce35
JM
562#ifdef CONFIG_WIFI_DISPLAY
563 } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
bab6677a
JM
564 int enabled;
565 if (wpa_s->global->p2p == NULL ||
566 wpa_s->global->p2p_disabled)
567 enabled = 0;
568 else
569 enabled = wpa_s->global->wifi_display;
570 res = os_snprintf(buf, buflen, "%d", enabled);
9675ce35 571#endif /* CONFIG_WIFI_DISPLAY */
fa7ae950
JM
572#ifdef CONFIG_TESTING_GET_GTK
573 } else if (os_strcmp(cmd, "gtk") == 0) {
574 if (wpa_s->last_gtk_len == 0)
575 return -1;
576 res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
577 wpa_s->last_gtk_len);
578 return res;
579#endif /* CONFIG_TESTING_GET_GTK */
a1651451
JM
580 } else if (os_strcmp(cmd, "tls_library") == 0) {
581 res = tls_get_library_version(buf, buflen);
10263dc2
OO
582 } else {
583 res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
acec8d32
JM
584 }
585
1f102d3b 586 if (os_snprintf_error(buflen, res))
6ce937b8
DS
587 return -1;
588 return res;
acec8d32
JM
589}
590
591
ec717917 592#ifdef IEEE8021X_EAPOL
6fc6879b
JM
593static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
594 char *addr)
595{
596 u8 bssid[ETH_ALEN];
597 struct wpa_ssid *ssid = wpa_s->current_ssid;
598
599 if (hwaddr_aton(addr, bssid)) {
600 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
601 "'%s'", addr);
602 return -1;
603 }
604
605 wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
606 rsn_preauth_deinit(wpa_s->wpa);
607 if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
608 return -1;
609
610 return 0;
611}
ec717917 612#endif /* IEEE8021X_EAPOL */
6fc6879b
JM
613
614
615#ifdef CONFIG_PEERKEY
616/* MLME-STKSTART.request(peer) */
617static int wpa_supplicant_ctrl_iface_stkstart(
618 struct wpa_supplicant *wpa_s, char *addr)
619{
620 u8 peer[ETH_ALEN];
621
622 if (hwaddr_aton(addr, peer)) {
623 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
a7b6c422 624 "address '%s'", addr);
6fc6879b
JM
625 return -1;
626 }
627
628 wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
629 MAC2STR(peer));
630
631 return wpa_sm_stkstart(wpa_s->wpa, peer);
632}
633#endif /* CONFIG_PEERKEY */
634
635
281ff0aa
GP
636#ifdef CONFIG_TDLS
637
638static int wpa_supplicant_ctrl_iface_tdls_discover(
639 struct wpa_supplicant *wpa_s, char *addr)
640{
641 u8 peer[ETH_ALEN];
2d565a61 642 int ret;
281ff0aa
GP
643
644 if (hwaddr_aton(addr, peer)) {
645 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
646 "address '%s'", addr);
647 return -1;
648 }
649
650 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
651 MAC2STR(peer));
652
2d565a61
AN
653 if (wpa_tdls_is_external_setup(wpa_s->wpa))
654 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
655 else
656 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
657
658 return ret;
281ff0aa
GP
659}
660
661
662static int wpa_supplicant_ctrl_iface_tdls_setup(
663 struct wpa_supplicant *wpa_s, char *addr)
664{
665 u8 peer[ETH_ALEN];
94377fbc 666 int ret;
281ff0aa
GP
667
668 if (hwaddr_aton(addr, peer)) {
669 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
670 "address '%s'", addr);
671 return -1;
672 }
673
674 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
675 MAC2STR(peer));
676
800d5872
SD
677 if ((wpa_s->conf->tdls_external_control) &&
678 wpa_tdls_is_external_setup(wpa_s->wpa))
679 return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
680
3887878e
SD
681 wpa_tdls_remove(wpa_s->wpa, peer);
682
683 if (wpa_tdls_is_external_setup(wpa_s->wpa))
684 ret = wpa_tdls_start(wpa_s->wpa, peer);
685 else
686 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
2d565a61 687
94377fbc 688 return ret;
281ff0aa
GP
689}
690
691
692static int wpa_supplicant_ctrl_iface_tdls_teardown(
693 struct wpa_supplicant *wpa_s, char *addr)
694{
695 u8 peer[ETH_ALEN];
4ed8d954 696 int ret;
281ff0aa 697
38ddccae
AN
698 if (os_strcmp(addr, "*") == 0) {
699 /* remove everyone */
700 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
701 wpa_tdls_teardown_peers(wpa_s->wpa);
702 return 0;
703 }
704
281ff0aa
GP
705 if (hwaddr_aton(addr, peer)) {
706 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
707 "address '%s'", addr);
708 return -1;
709 }
710
711 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
712 MAC2STR(peer));
713
800d5872
SD
714 if ((wpa_s->conf->tdls_external_control) &&
715 wpa_tdls_is_external_setup(wpa_s->wpa))
716 return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
717
4ed8d954
AS
718 if (wpa_tdls_is_external_setup(wpa_s->wpa))
719 ret = wpa_tdls_teardown_link(
720 wpa_s->wpa, peer,
721 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
722 else
723 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
724
725 return ret;
281ff0aa
GP
726}
727
6e9375e4
DS
728
729static int ctrl_iface_get_capability_tdls(
730 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
731{
732 int ret;
733
734 ret = os_snprintf(buf, buflen, "%s\n",
735 wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
736 (wpa_s->drv_flags &
737 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
738 "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
7bdd8981 739 if (os_snprintf_error(buflen, ret))
6e9375e4
DS
740 return -1;
741 return ret;
742}
743
6b90deae
AN
744
745static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
746 struct wpa_supplicant *wpa_s, char *cmd)
747{
748 u8 peer[ETH_ALEN];
749 struct hostapd_freq_params freq_params;
750 u8 oper_class;
751 char *pos, *end;
752
753 if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
754 wpa_printf(MSG_INFO,
755 "tdls_chanswitch: Only supported with external setup");
756 return -1;
757 }
758
759 os_memset(&freq_params, 0, sizeof(freq_params));
760
761 pos = os_strchr(cmd, ' ');
762 if (pos == NULL)
763 return -1;
764 *pos++ = '\0';
765
766 oper_class = strtol(pos, &end, 10);
767 if (pos == end) {
768 wpa_printf(MSG_INFO,
769 "tdls_chanswitch: Invalid op class provided");
770 return -1;
771 }
772
773 pos = end;
774 freq_params.freq = atoi(pos);
775 if (freq_params.freq == 0) {
776 wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
777 return -1;
778 }
779
780#define SET_FREQ_SETTING(str) \
781 do { \
782 const char *pos2 = os_strstr(pos, " " #str "="); \
783 if (pos2) { \
784 pos2 += sizeof(" " #str "=") - 1; \
785 freq_params.str = atoi(pos2); \
786 } \
787 } while (0)
788
789 SET_FREQ_SETTING(center_freq1);
790 SET_FREQ_SETTING(center_freq2);
791 SET_FREQ_SETTING(bandwidth);
792 SET_FREQ_SETTING(sec_channel_offset);
793#undef SET_FREQ_SETTING
794
795 freq_params.ht_enabled = !!os_strstr(pos, " ht");
796 freq_params.vht_enabled = !!os_strstr(pos, " vht");
797
798 if (hwaddr_aton(cmd, peer)) {
799 wpa_printf(MSG_DEBUG,
800 "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
801 cmd);
802 return -1;
803 }
804
805 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
806 " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
807 MAC2STR(peer), oper_class, freq_params.freq,
808 freq_params.center_freq1, freq_params.center_freq2,
809 freq_params.bandwidth, freq_params.sec_channel_offset,
810 freq_params.ht_enabled ? " HT" : "",
811 freq_params.vht_enabled ? " VHT" : "");
812
813 return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
814 &freq_params);
815}
816
817
818static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
819 struct wpa_supplicant *wpa_s, char *cmd)
820{
821 u8 peer[ETH_ALEN];
822
823 if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
824 wpa_printf(MSG_INFO,
825 "tdls_chanswitch: Only supported with external setup");
826 return -1;
827 }
828
829 if (hwaddr_aton(cmd, peer)) {
830 wpa_printf(MSG_DEBUG,
831 "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
832 cmd);
833 return -1;
834 }
835
836 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
837 MAC2STR(peer));
838
839 return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
840}
841
4504621f
OG
842
843static int wpa_supplicant_ctrl_iface_tdls_link_status(
844 struct wpa_supplicant *wpa_s, const char *addr,
845 char *buf, size_t buflen)
846{
847 u8 peer[ETH_ALEN];
848 const char *tdls_status;
849 int ret;
850
851 if (hwaddr_aton(addr, peer)) {
852 wpa_printf(MSG_DEBUG,
853 "CTRL_IFACE TDLS_LINK_STATUS: Invalid address '%s'",
854 addr);
855 return -1;
856 }
857 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS " MACSTR,
858 MAC2STR(peer));
859
860 tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
861 wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS: %s", tdls_status);
862 ret = os_snprintf(buf, buflen, "TDLS link status: %s\n", tdls_status);
863 if (os_snprintf_error(buflen, ret))
864 return -1;
865
866 return ret;
867}
868
281ff0aa
GP
869#endif /* CONFIG_TDLS */
870
871
eb2f2088
MB
872static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
873{
874 char *token, *context = NULL;
875 struct wmm_ac_ts_setup_params params = {
876 .tsid = 0xff,
877 .direction = 0xff,
878 };
879
880 while ((token = str_token(cmd, " ", &context))) {
881 if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
882 sscanf(token, "up=%i", &params.user_priority) == 1 ||
883 sscanf(token, "nominal_msdu_size=%i",
884 &params.nominal_msdu_size) == 1 ||
885 sscanf(token, "mean_data_rate=%i",
886 &params.mean_data_rate) == 1 ||
887 sscanf(token, "min_phy_rate=%i",
888 &params.minimum_phy_rate) == 1 ||
889 sscanf(token, "sba=%i",
890 &params.surplus_bandwidth_allowance) == 1)
891 continue;
892
893 if (os_strcasecmp(token, "downlink") == 0) {
894 params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
895 } else if (os_strcasecmp(token, "uplink") == 0) {
896 params.direction = WMM_TSPEC_DIRECTION_UPLINK;
897 } else if (os_strcasecmp(token, "bidi") == 0) {
898 params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
899 } else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
900 params.fixed_nominal_msdu = 1;
901 } else {
902 wpa_printf(MSG_DEBUG,
903 "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
904 token);
905 return -1;
906 }
907
908 }
909
910 return wpas_wmm_ac_addts(wpa_s, &params);
911}
912
913
914static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
915{
916 u8 tsid = atoi(cmd);
917
918 return wpas_wmm_ac_delts(wpa_s, tsid);
919}
920
921
6fc6879b
JM
922#ifdef CONFIG_IEEE80211R
923static int wpa_supplicant_ctrl_iface_ft_ds(
924 struct wpa_supplicant *wpa_s, char *addr)
925{
926 u8 target_ap[ETH_ALEN];
76b7981d
JM
927 struct wpa_bss *bss;
928 const u8 *mdie;
6fc6879b
JM
929
930 if (hwaddr_aton(addr, target_ap)) {
931 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
a7b6c422 932 "address '%s'", addr);
6fc6879b
JM
933 return -1;
934 }
935
936 wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
937
76b7981d
JM
938 bss = wpa_bss_get_bssid(wpa_s, target_ap);
939 if (bss)
940 mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
941 else
942 mdie = NULL;
943
944 return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
6fc6879b
JM
945}
946#endif /* CONFIG_IEEE80211R */
947
948
fcc60db4
JM
949#ifdef CONFIG_WPS
950static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
951 char *cmd)
952{
3ec97afe 953 u8 bssid[ETH_ALEN], *_bssid = bssid;
ceb34f25 954#ifdef CONFIG_P2P
634ce802 955 u8 p2p_dev_addr[ETH_ALEN];
ceb34f25 956#endif /* CONFIG_P2P */
634ce802
JM
957#ifdef CONFIG_AP
958 u8 *_p2p_dev_addr = NULL;
959#endif /* CONFIG_AP */
fcc60db4 960
d601247c 961 if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
3ec97afe 962 _bssid = NULL;
d601247c
JM
963#ifdef CONFIG_P2P
964 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
965 if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
966 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
967 "P2P Device Address '%s'",
968 cmd + 13);
969 return -1;
970 }
971 _p2p_dev_addr = p2p_dev_addr;
972#endif /* CONFIG_P2P */
973 } else if (hwaddr_aton(cmd, bssid)) {
fcc60db4
JM
974 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
975 cmd);
976 return -1;
977 }
978
3ec97afe
JM
979#ifdef CONFIG_AP
980 if (wpa_s->ap_iface)
d601247c 981 return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
3ec97afe
JM
982#endif /* CONFIG_AP */
983
9fa243b2 984 return wpas_wps_start_pbc(wpa_s, _bssid, 0);
fcc60db4
JM
985}
986
987
988static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
989 char *cmd, char *buf,
990 size_t buflen)
991{
992 u8 bssid[ETH_ALEN], *_bssid = bssid;
993 char *pin;
994 int ret;
995
996 pin = os_strchr(cmd, ' ');
997 if (pin)
998 *pin++ = '\0';
999
1000 if (os_strcmp(cmd, "any") == 0)
1001 _bssid = NULL;
98aa7ca5 1002 else if (os_strcmp(cmd, "get") == 0) {
98a516ea
NL
1003 if (wps_generate_pin((unsigned int *) &ret) < 0)
1004 return -1;
98aa7ca5
JM
1005 goto done;
1006 } else if (hwaddr_aton(cmd, bssid)) {
3c1e2765 1007 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
fcc60db4
JM
1008 cmd);
1009 return -1;
1010 }
1011
3ec97afe 1012#ifdef CONFIG_AP
c423708f
JM
1013 if (wpa_s->ap_iface) {
1014 int timeout = 0;
1015 char *pos;
1016
1017 if (pin) {
1018 pos = os_strchr(pin, ' ');
1019 if (pos) {
1020 *pos++ = '\0';
1021 timeout = atoi(pos);
1022 }
1023 }
1024
3ec97afe 1025 return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
c423708f
JM
1026 buf, buflen, timeout);
1027 }
3ec97afe
JM
1028#endif /* CONFIG_AP */
1029
fcc60db4 1030 if (pin) {
3c5126a4
JM
1031 ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
1032 DEV_PW_DEFAULT);
fcc60db4
JM
1033 if (ret < 0)
1034 return -1;
1035 ret = os_snprintf(buf, buflen, "%s", pin);
d85e1fc8 1036 if (os_snprintf_error(buflen, ret))
fcc60db4
JM
1037 return -1;
1038 return ret;
1039 }
1040
3c5126a4 1041 ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
fcc60db4
JM
1042 if (ret < 0)
1043 return -1;
1044
98aa7ca5 1045done:
fcc60db4
JM
1046 /* Return the generated PIN */
1047 ret = os_snprintf(buf, buflen, "%08d", ret);
d85e1fc8 1048 if (os_snprintf_error(buflen, ret))
fcc60db4
JM
1049 return -1;
1050 return ret;
1051}
1052
1053
3981cb3c
JM
1054static int wpa_supplicant_ctrl_iface_wps_check_pin(
1055 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1056{
1057 char pin[9];
1058 size_t len;
1059 char *pos;
1060 int ret;
1061
1062 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
1063 (u8 *) cmd, os_strlen(cmd));
1064 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
1065 if (*pos < '0' || *pos > '9')
1066 continue;
1067 pin[len++] = *pos;
1068 if (len == 9) {
1069 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
1070 return -1;
1071 }
1072 }
1073 if (len != 4 && len != 8) {
1074 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
1075 return -1;
1076 }
1077 pin[len] = '\0';
1078
1079 if (len == 8) {
1080 unsigned int pin_val;
1081 pin_val = atoi(pin);
1082 if (!wps_pin_valid(pin_val)) {
1083 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
1084 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
d85e1fc8 1085 if (os_snprintf_error(buflen, ret))
3981cb3c
JM
1086 return -1;
1087 return ret;
1088 }
1089 }
1090
1091 ret = os_snprintf(buf, buflen, "%s", pin);
d85e1fc8 1092 if (os_snprintf_error(buflen, ret))
3981cb3c
JM
1093 return -1;
1094
1095 return ret;
1096}
1097
1098
71892384 1099#ifdef CONFIG_WPS_NFC
3f2c8ba6
JM
1100
1101static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
1102 char *cmd)
1103{
1104 u8 bssid[ETH_ALEN], *_bssid = bssid;
1105
1106 if (cmd == NULL || cmd[0] == '\0')
1107 _bssid = NULL;
1108 else if (hwaddr_aton(cmd, bssid))
1109 return -1;
1110
23318bea 1111 return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
91a65018 1112 0, 0);
3f2c8ba6
JM
1113}
1114
1115
bbf41865
JM
1116static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
1117 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1118{
1119 int ndef;
1120 struct wpabuf *buf;
1121 int res;
88c8bf31 1122 char *pos;
bbf41865 1123
88c8bf31
JM
1124 pos = os_strchr(cmd, ' ');
1125 if (pos)
1126 *pos++ = '\0';
bbf41865
JM
1127 if (os_strcmp(cmd, "WPS") == 0)
1128 ndef = 0;
1129 else if (os_strcmp(cmd, "NDEF") == 0)
1130 ndef = 1;
1131 else
1132 return -1;
1133
88c8bf31 1134 buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
bbf41865
JM
1135 if (buf == NULL)
1136 return -1;
1137
1138 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1139 wpabuf_len(buf));
1140 reply[res++] = '\n';
1141 reply[res] = '\0';
1142
1143 wpabuf_free(buf);
1144
1145 return res;
1146}
1147
1148
3f2c8ba6
JM
1149static int wpa_supplicant_ctrl_iface_wps_nfc_token(
1150 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1151{
1152 int ndef;
1153 struct wpabuf *buf;
1154 int res;
1155
1156 if (os_strcmp(cmd, "WPS") == 0)
1157 ndef = 0;
1158 else if (os_strcmp(cmd, "NDEF") == 0)
1159 ndef = 1;
1160 else
1161 return -1;
1162
1163 buf = wpas_wps_nfc_token(wpa_s, ndef);
1164 if (buf == NULL)
1165 return -1;
1166
1167 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1168 wpabuf_len(buf));
1169 reply[res++] = '\n';
1170 reply[res] = '\0';
1171
1172 wpabuf_free(buf);
1173
1174 return res;
1175}
d7645d23
JM
1176
1177
1178static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
1179 struct wpa_supplicant *wpa_s, char *pos)
1180{
1181 size_t len;
1182 struct wpabuf *buf;
1183 int ret;
b56f6c88
JM
1184 char *freq;
1185 int forced_freq = 0;
1186
1187 freq = strstr(pos, " freq=");
1188 if (freq) {
1189 *freq = '\0';
1190 freq += 6;
1191 forced_freq = atoi(freq);
1192 }
d7645d23
JM
1193
1194 len = os_strlen(pos);
1195 if (len & 0x01)
1196 return -1;
1197 len /= 2;
1198
1199 buf = wpabuf_alloc(len);
1200 if (buf == NULL)
1201 return -1;
1202 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
1203 wpabuf_free(buf);
1204 return -1;
1205 }
1206
b56f6c88 1207 ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
d7645d23
JM
1208 wpabuf_free(buf);
1209
1210 return ret;
1211}
71892384 1212
e65552dd
JM
1213
1214static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
bbaaaee1 1215 char *reply, size_t max_len,
41f9ffb6 1216 int ndef)
e65552dd
JM
1217{
1218 struct wpabuf *buf;
1219 int res;
1220
41f9ffb6 1221 buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
e65552dd
JM
1222 if (buf == NULL)
1223 return -1;
1224
1225 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1226 wpabuf_len(buf));
1227 reply[res++] = '\n';
1228 reply[res] = '\0';
1229
1230 wpabuf_free(buf);
1231
1232 return res;
1233}
1234
1235
88853aed 1236#ifdef CONFIG_P2P
93588780
JM
1237static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
1238 char *reply, size_t max_len,
1239 int ndef)
1240{
1241 struct wpabuf *buf;
1242 int res;
1243
1244 buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
1245 if (buf == NULL) {
1246 wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
1247 return -1;
1248 }
1249
1250 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1251 wpabuf_len(buf));
1252 reply[res++] = '\n';
1253 reply[res] = '\0';
1254
1255 wpabuf_free(buf);
1256
1257 return res;
1258}
88853aed 1259#endif /* CONFIG_P2P */
93588780
JM
1260
1261
e65552dd
JM
1262static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
1263 char *cmd, char *reply,
1264 size_t max_len)
1265{
1266 char *pos;
41f9ffb6 1267 int ndef;
e65552dd
JM
1268
1269 pos = os_strchr(cmd, ' ');
1270 if (pos == NULL)
1271 return -1;
1272 *pos++ = '\0';
1273
41f9ffb6
JM
1274 if (os_strcmp(cmd, "WPS") == 0)
1275 ndef = 0;
1276 else if (os_strcmp(cmd, "NDEF") == 0)
1277 ndef = 1;
1278 else
e65552dd
JM
1279 return -1;
1280
bbaaaee1 1281 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
41f9ffb6
JM
1282 if (!ndef)
1283 return -1;
bbaaaee1 1284 return wpas_ctrl_nfc_get_handover_req_wps(
41f9ffb6 1285 wpa_s, reply, max_len, ndef);
e65552dd
JM
1286 }
1287
88853aed 1288#ifdef CONFIG_P2P
93588780
JM
1289 if (os_strcmp(pos, "P2P-CR") == 0) {
1290 return wpas_ctrl_nfc_get_handover_req_p2p(
1291 wpa_s, reply, max_len, ndef);
1292 }
88853aed 1293#endif /* CONFIG_P2P */
93588780 1294
e65552dd
JM
1295 return -1;
1296}
1297
1298
1299static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
5ab9a6a5 1300 char *reply, size_t max_len,
f3f2ba2e 1301 int ndef, int cr, char *uuid)
e65552dd
JM
1302{
1303 struct wpabuf *buf;
1304 int res;
1305
f3f2ba2e 1306 buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
e65552dd
JM
1307 if (buf == NULL)
1308 return -1;
1309
1310 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1311 wpabuf_len(buf));
1312 reply[res++] = '\n';
1313 reply[res] = '\0';
1314
1315 wpabuf_free(buf);
1316
1317 return res;
1318}
1319
1320
88853aed 1321#ifdef CONFIG_P2P
93588780
JM
1322static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1323 char *reply, size_t max_len,
1324 int ndef, int tag)
1325{
1326 struct wpabuf *buf;
1327 int res;
1328
1329 buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1330 if (buf == NULL)
1331 return -1;
1332
1333 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1334 wpabuf_len(buf));
1335 reply[res++] = '\n';
1336 reply[res] = '\0';
1337
1338 wpabuf_free(buf);
1339
1340 return res;
1341}
88853aed 1342#endif /* CONFIG_P2P */
93588780
JM
1343
1344
e65552dd
JM
1345static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1346 char *cmd, char *reply,
1347 size_t max_len)
1348{
f3f2ba2e 1349 char *pos, *pos2;
5ab9a6a5 1350 int ndef;
e65552dd
JM
1351
1352 pos = os_strchr(cmd, ' ');
1353 if (pos == NULL)
1354 return -1;
1355 *pos++ = '\0';
1356
5ab9a6a5
JM
1357 if (os_strcmp(cmd, "WPS") == 0)
1358 ndef = 0;
1359 else if (os_strcmp(cmd, "NDEF") == 0)
1360 ndef = 1;
1361 else
e65552dd
JM
1362 return -1;
1363
f3f2ba2e
JM
1364 pos2 = os_strchr(pos, ' ');
1365 if (pos2)
1366 *pos2++ = '\0';
5ab9a6a5 1367 if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
93588780
JM
1368 if (!ndef)
1369 return -1;
5ab9a6a5
JM
1370 return wpas_ctrl_nfc_get_handover_sel_wps(
1371 wpa_s, reply, max_len, ndef,
f3f2ba2e 1372 os_strcmp(pos, "WPS-CR") == 0, pos2);
e65552dd
JM
1373 }
1374
88853aed 1375#ifdef CONFIG_P2P
93588780
JM
1376 if (os_strcmp(pos, "P2P-CR") == 0) {
1377 return wpas_ctrl_nfc_get_handover_sel_p2p(
1378 wpa_s, reply, max_len, ndef, 0);
1379 }
1380
1381 if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1382 return wpas_ctrl_nfc_get_handover_sel_p2p(
1383 wpa_s, reply, max_len, ndef, 1);
1384 }
88853aed 1385#endif /* CONFIG_P2P */
93588780 1386
e65552dd
JM
1387 return -1;
1388}
1389
1390
e4758827
JM
1391static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1392 char *cmd)
1393{
1394 size_t len;
1395 struct wpabuf *req, *sel;
1396 int ret;
1397 char *pos, *role, *type, *pos2;
88853aed 1398#ifdef CONFIG_P2P
b56f6c88
JM
1399 char *freq;
1400 int forced_freq = 0;
1401
1402 freq = strstr(cmd, " freq=");
1403 if (freq) {
1404 *freq = '\0';
1405 freq += 6;
1406 forced_freq = atoi(freq);
1407 }
88853aed 1408#endif /* CONFIG_P2P */
e4758827
JM
1409
1410 role = cmd;
1411 pos = os_strchr(role, ' ');
73127764
JM
1412 if (pos == NULL) {
1413 wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
e4758827 1414 return -1;
73127764 1415 }
e4758827
JM
1416 *pos++ = '\0';
1417
1418 type = pos;
1419 pos = os_strchr(type, ' ');
73127764
JM
1420 if (pos == NULL) {
1421 wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
e4758827 1422 return -1;
73127764 1423 }
e4758827
JM
1424 *pos++ = '\0';
1425
1426 pos2 = os_strchr(pos, ' ');
73127764
JM
1427 if (pos2 == NULL) {
1428 wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
e4758827 1429 return -1;
73127764 1430 }
e4758827
JM
1431 *pos2++ = '\0';
1432
1433 len = os_strlen(pos);
73127764
JM
1434 if (len & 0x01) {
1435 wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
e4758827 1436 return -1;
73127764 1437 }
e4758827
JM
1438 len /= 2;
1439
1440 req = wpabuf_alloc(len);
73127764
JM
1441 if (req == NULL) {
1442 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
e4758827 1443 return -1;
73127764 1444 }
e4758827 1445 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
73127764 1446 wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
e4758827
JM
1447 wpabuf_free(req);
1448 return -1;
1449 }
1450
1451 len = os_strlen(pos2);
1452 if (len & 0x01) {
73127764 1453 wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
e4758827
JM
1454 wpabuf_free(req);
1455 return -1;
1456 }
1457 len /= 2;
1458
1459 sel = wpabuf_alloc(len);
1460 if (sel == NULL) {
73127764 1461 wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
e4758827
JM
1462 wpabuf_free(req);
1463 return -1;
1464 }
1465 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
73127764 1466 wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
e4758827
JM
1467 wpabuf_free(req);
1468 wpabuf_free(sel);
1469 return -1;
1470 }
1471
73127764
JM
1472 wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1473 role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1474
e4758827
JM
1475 if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1476 ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
88853aed 1477#ifdef CONFIG_AP
d9507936
JM
1478 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1479 {
1480 ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
50d1f890
JM
1481 if (ret < 0)
1482 ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
88853aed
JM
1483#endif /* CONFIG_AP */
1484#ifdef CONFIG_P2P
db6ae69e
JM
1485 } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1486 {
b56f6c88 1487 ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
db6ae69e
JM
1488 } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1489 {
b56f6c88
JM
1490 ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1491 forced_freq);
88853aed 1492#endif /* CONFIG_P2P */
e4758827
JM
1493 } else {
1494 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1495 "reported: role=%s type=%s", role, type);
1496 ret = -1;
1497 }
1498 wpabuf_free(req);
1499 wpabuf_free(sel);
1500
73127764
JM
1501 if (ret)
1502 wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1503
e4758827
JM
1504 return ret;
1505}
1506
71892384 1507#endif /* CONFIG_WPS_NFC */
46bdb83a
MH
1508
1509
fcc60db4
JM
1510static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1511 char *cmd)
1512{
129eb428 1513 u8 bssid[ETH_ALEN];
fcc60db4 1514 char *pin;
52eb293d
JM
1515 char *new_ssid;
1516 char *new_auth;
1517 char *new_encr;
1518 char *new_key;
1519 struct wps_new_ap_settings ap;
fcc60db4
JM
1520
1521 pin = os_strchr(cmd, ' ');
1522 if (pin == NULL)
1523 return -1;
1524 *pin++ = '\0';
1525
129eb428 1526 if (hwaddr_aton(cmd, bssid)) {
fcc60db4
JM
1527 wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1528 cmd);
1529 return -1;
1530 }
1531
52eb293d
JM
1532 new_ssid = os_strchr(pin, ' ');
1533 if (new_ssid == NULL)
129eb428 1534 return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
52eb293d
JM
1535 *new_ssid++ = '\0';
1536
1537 new_auth = os_strchr(new_ssid, ' ');
1538 if (new_auth == NULL)
1539 return -1;
1540 *new_auth++ = '\0';
1541
1542 new_encr = os_strchr(new_auth, ' ');
1543 if (new_encr == NULL)
1544 return -1;
1545 *new_encr++ = '\0';
1546
1547 new_key = os_strchr(new_encr, ' ');
1548 if (new_key == NULL)
1549 return -1;
1550 *new_key++ = '\0';
1551
1552 os_memset(&ap, 0, sizeof(ap));
1553 ap.ssid_hex = new_ssid;
1554 ap.auth = new_auth;
1555 ap.encr = new_encr;
1556 ap.key_hex = new_key;
129eb428 1557 return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
fcc60db4 1558}
72df2f5f
JM
1559
1560
70d84f11
JM
1561#ifdef CONFIG_AP
1562static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1563 char *cmd, char *buf,
1564 size_t buflen)
1565{
1566 int timeout = 300;
1567 char *pos;
1568 const char *pin_txt;
1569
1570 if (!wpa_s->ap_iface)
1571 return -1;
1572
1573 pos = os_strchr(cmd, ' ');
1574 if (pos)
1575 *pos++ = '\0';
1576
1577 if (os_strcmp(cmd, "disable") == 0) {
1578 wpas_wps_ap_pin_disable(wpa_s);
1579 return os_snprintf(buf, buflen, "OK\n");
1580 }
1581
1582 if (os_strcmp(cmd, "random") == 0) {
1583 if (pos)
1584 timeout = atoi(pos);
1585 pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1586 if (pin_txt == NULL)
1587 return -1;
1588 return os_snprintf(buf, buflen, "%s", pin_txt);
1589 }
1590
1591 if (os_strcmp(cmd, "get") == 0) {
1592 pin_txt = wpas_wps_ap_pin_get(wpa_s);
1593 if (pin_txt == NULL)
1594 return -1;
1595 return os_snprintf(buf, buflen, "%s", pin_txt);
1596 }
1597
1598 if (os_strcmp(cmd, "set") == 0) {
1599 char *pin;
1600 if (pos == NULL)
1601 return -1;
1602 pin = pos;
1603 pos = os_strchr(pos, ' ');
1604 if (pos) {
1605 *pos++ = '\0';
1606 timeout = atoi(pos);
1607 }
1608 if (os_strlen(pin) > buflen)
1609 return -1;
1610 if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1611 return -1;
1612 return os_snprintf(buf, buflen, "%s", pin);
1613 }
1614
1615 return -1;
1616}
1617#endif /* CONFIG_AP */
1618
1619
72df2f5f
JM
1620#ifdef CONFIG_WPS_ER
1621static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1622 char *cmd)
1623{
31fcea93
JM
1624 char *uuid = cmd, *pin, *pos;
1625 u8 addr_buf[ETH_ALEN], *addr = NULL;
72df2f5f
JM
1626 pin = os_strchr(uuid, ' ');
1627 if (pin == NULL)
1628 return -1;
1629 *pin++ = '\0';
31fcea93
JM
1630 pos = os_strchr(pin, ' ');
1631 if (pos) {
1632 *pos++ = '\0';
1633 if (hwaddr_aton(pos, addr_buf) == 0)
1634 addr = addr_buf;
1635 }
1636 return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
72df2f5f 1637}
e64dcfd5
JM
1638
1639
1640static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1641 char *cmd)
1642{
1643 char *uuid = cmd, *pin;
1644 pin = os_strchr(uuid, ' ');
1645 if (pin == NULL)
1646 return -1;
1647 *pin++ = '\0';
1648 return wpas_wps_er_learn(wpa_s, uuid, pin);
1649}
7d6640a6
JM
1650
1651
ef10f473
JM
1652static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1653 struct wpa_supplicant *wpa_s, char *cmd)
1654{
1655 char *uuid = cmd, *id;
1656 id = os_strchr(uuid, ' ');
1657 if (id == NULL)
1658 return -1;
1659 *id++ = '\0';
1660 return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
1661}
1662
1663
7d6640a6
JM
1664static int wpa_supplicant_ctrl_iface_wps_er_config(
1665 struct wpa_supplicant *wpa_s, char *cmd)
1666{
1667 char *pin;
1668 char *new_ssid;
1669 char *new_auth;
1670 char *new_encr;
1671 char *new_key;
1672 struct wps_new_ap_settings ap;
1673
1674 pin = os_strchr(cmd, ' ');
1675 if (pin == NULL)
1676 return -1;
1677 *pin++ = '\0';
1678
1679 new_ssid = os_strchr(pin, ' ');
1680 if (new_ssid == NULL)
1681 return -1;
1682 *new_ssid++ = '\0';
1683
1684 new_auth = os_strchr(new_ssid, ' ');
1685 if (new_auth == NULL)
1686 return -1;
1687 *new_auth++ = '\0';
1688
1689 new_encr = os_strchr(new_auth, ' ');
1690 if (new_encr == NULL)
1691 return -1;
1692 *new_encr++ = '\0';
1693
1694 new_key = os_strchr(new_encr, ' ');
1695 if (new_key == NULL)
1696 return -1;
1697 *new_key++ = '\0';
1698
1699 os_memset(&ap, 0, sizeof(ap));
1700 ap.ssid_hex = new_ssid;
1701 ap.auth = new_auth;
1702 ap.encr = new_encr;
1703 ap.key_hex = new_key;
1704 return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
1705}
1cea09a9
JM
1706
1707
1708#ifdef CONFIG_WPS_NFC
1709static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
1710 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1711{
1712 int ndef;
1713 struct wpabuf *buf;
1714 int res;
1715 char *uuid;
1716
1717 uuid = os_strchr(cmd, ' ');
1718 if (uuid == NULL)
1719 return -1;
1720 *uuid++ = '\0';
1721
1722 if (os_strcmp(cmd, "WPS") == 0)
1723 ndef = 0;
1724 else if (os_strcmp(cmd, "NDEF") == 0)
1725 ndef = 1;
1726 else
1727 return -1;
1728
1729 buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
1730 if (buf == NULL)
1731 return -1;
1732
1733 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1734 wpabuf_len(buf));
1735 reply[res++] = '\n';
1736 reply[res] = '\0';
1737
1738 wpabuf_free(buf);
1739
1740 return res;
1741}
1742#endif /* CONFIG_WPS_NFC */
72df2f5f
JM
1743#endif /* CONFIG_WPS_ER */
1744
fcc60db4
JM
1745#endif /* CONFIG_WPS */
1746
1747
11ef8d35
JM
1748#ifdef CONFIG_IBSS_RSN
1749static int wpa_supplicant_ctrl_iface_ibss_rsn(
1750 struct wpa_supplicant *wpa_s, char *addr)
1751{
1752 u8 peer[ETH_ALEN];
1753
1754 if (hwaddr_aton(addr, peer)) {
1755 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
a7b6c422 1756 "address '%s'", addr);
11ef8d35
JM
1757 return -1;
1758 }
1759
1760 wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
1761 MAC2STR(peer));
1762
1763 return ibss_rsn_start(wpa_s->ibss_rsn, peer);
1764}
1765#endif /* CONFIG_IBSS_RSN */
1766
1767
7de5688d
DW
1768static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
1769 char *rsp)
1770{
1771#ifdef IEEE8021X_EAPOL
1772 char *pos, *id_pos;
1773 int id;
1774 struct wpa_ssid *ssid;
1775
1776 pos = os_strchr(rsp, '-');
1777 if (pos == NULL)
1778 return -1;
1779 *pos++ = '\0';
1780 id_pos = pos;
1781 pos = os_strchr(pos, ':');
1782 if (pos == NULL)
1783 return -1;
1784 *pos++ = '\0';
1785 id = atoi(id_pos);
1786 wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
1787 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
1788 (u8 *) pos, os_strlen(pos));
1789
1790 ssid = wpa_config_get_network(wpa_s->conf, id);
1791 if (ssid == NULL) {
1792 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
1793 "to update", id);
1794 return -1;
1795 }
1796
1797 return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
1798 pos);
6fc6879b
JM
1799#else /* IEEE8021X_EAPOL */
1800 wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1801 return -1;
1802#endif /* IEEE8021X_EAPOL */
1803}
1804
1805
1806static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
1807 const char *params,
1808 char *buf, size_t buflen)
1809{
1810 char *pos, *end, tmp[30];
0bc13468 1811 int res, verbose, wps, ret;
f9cd147d
JM
1812#ifdef CONFIG_HS20
1813 const u8 *hs20;
1814#endif /* CONFIG_HS20 */
993a8654
JM
1815 const u8 *sess_id;
1816 size_t sess_id_len;
6fc6879b 1817
a771c07d
JM
1818 if (os_strcmp(params, "-DRIVER") == 0)
1819 return wpa_drv_status(wpa_s, buf, buflen);
6fc6879b 1820 verbose = os_strcmp(params, "-VERBOSE") == 0;
0bc13468 1821 wps = os_strcmp(params, "-WPS") == 0;
6fc6879b
JM
1822 pos = buf;
1823 end = buf + buflen;
1824 if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
1825 struct wpa_ssid *ssid = wpa_s->current_ssid;
1826 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
1827 MAC2STR(wpa_s->bssid));
d85e1fc8 1828 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
1829 return pos - buf;
1830 pos += ret;
b6ebdfbe
BP
1831 ret = os_snprintf(pos, end - pos, "freq=%u\n",
1832 wpa_s->assoc_freq);
d85e1fc8 1833 if (os_snprintf_error(end - pos, ret))
b6ebdfbe
BP
1834 return pos - buf;
1835 pos += ret;
6fc6879b
JM
1836 if (ssid) {
1837 u8 *_ssid = ssid->ssid;
1838 size_t ssid_len = ssid->ssid_len;
eaa8eefe 1839 u8 ssid_buf[SSID_MAX_LEN];
6fc6879b
JM
1840 if (ssid_len == 0) {
1841 int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
1842 if (_res < 0)
1843 ssid_len = 0;
1844 else
1845 ssid_len = _res;
1846 _ssid = ssid_buf;
1847 }
1848 ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
1849 wpa_ssid_txt(_ssid, ssid_len),
1850 ssid->id);
d85e1fc8 1851 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
1852 return pos - buf;
1853 pos += ret;
1854
0bc13468
JM
1855 if (wps && ssid->passphrase &&
1856 wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
1857 (ssid->mode == WPAS_MODE_AP ||
1858 ssid->mode == WPAS_MODE_P2P_GO)) {
1859 ret = os_snprintf(pos, end - pos,
1860 "passphrase=%s\n",
1861 ssid->passphrase);
d85e1fc8 1862 if (os_snprintf_error(end - pos, ret))
0bc13468
JM
1863 return pos - buf;
1864 pos += ret;
1865 }
6fc6879b
JM
1866 if (ssid->id_str) {
1867 ret = os_snprintf(pos, end - pos,
1868 "id_str=%s\n",
1869 ssid->id_str);
d85e1fc8 1870 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
1871 return pos - buf;
1872 pos += ret;
1873 }
0e15e529
JM
1874
1875 switch (ssid->mode) {
d7dcba70 1876 case WPAS_MODE_INFRA:
0e15e529
JM
1877 ret = os_snprintf(pos, end - pos,
1878 "mode=station\n");
1879 break;
d7dcba70 1880 case WPAS_MODE_IBSS:
0e15e529
JM
1881 ret = os_snprintf(pos, end - pos,
1882 "mode=IBSS\n");
1883 break;
d7dcba70 1884 case WPAS_MODE_AP:
0e15e529
JM
1885 ret = os_snprintf(pos, end - pos,
1886 "mode=AP\n");
1887 break;
2c5d725c
JM
1888 case WPAS_MODE_P2P_GO:
1889 ret = os_snprintf(pos, end - pos,
1890 "mode=P2P GO\n");
1891 break;
1892 case WPAS_MODE_P2P_GROUP_FORMATION:
1893 ret = os_snprintf(pos, end - pos,
1894 "mode=P2P GO - group "
1895 "formation\n");
1896 break;
cee0be73
SB
1897 case WPAS_MODE_MESH:
1898 ret = os_snprintf(pos, end - pos,
1899 "mode=mesh\n");
1900 break;
0e15e529
JM
1901 default:
1902 ret = 0;
1903 break;
1904 }
1f102d3b 1905 if (os_snprintf_error(end - pos, ret))
0e15e529
JM
1906 return pos - buf;
1907 pos += ret;
6fc6879b
JM
1908 }
1909
43fb5297
JM
1910#ifdef CONFIG_AP
1911 if (wpa_s->ap_iface) {
1912 pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
1913 end - pos,
1914 verbose);
1915 } else
1916#endif /* CONFIG_AP */
6fc6879b
JM
1917 pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
1918 }
4954c859
JM
1919#ifdef CONFIG_SAE
1920 if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
e1ae5d74
JM
1921#ifdef CONFIG_AP
1922 !wpa_s->ap_iface &&
1923#endif /* CONFIG_AP */
1924 wpa_s->sme.sae.state == SAE_ACCEPTED) {
4954c859
JM
1925 ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
1926 wpa_s->sme.sae.group);
d85e1fc8 1927 if (os_snprintf_error(end - pos, ret))
4954c859
JM
1928 return pos - buf;
1929 pos += ret;
1930 }
1931#endif /* CONFIG_SAE */
6fc6879b
JM
1932 ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
1933 wpa_supplicant_state_txt(wpa_s->wpa_state));
d85e1fc8 1934 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
1935 return pos - buf;
1936 pos += ret;
1937
1938 if (wpa_s->l2 &&
1939 l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
1940 ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
d85e1fc8 1941 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
1942 return pos - buf;
1943 pos += ret;
1944 }
1945
d23bd894
JM
1946#ifdef CONFIG_P2P
1947 if (wpa_s->global->p2p) {
1948 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
1949 "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
d85e1fc8 1950 if (os_snprintf_error(end - pos, ret))
d23bd894
JM
1951 return pos - buf;
1952 pos += ret;
1953 }
b21e2c84 1954#endif /* CONFIG_P2P */
6d4747a9
JM
1955
1956 ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
1957 MAC2STR(wpa_s->own_addr));
d85e1fc8 1958 if (os_snprintf_error(end - pos, ret))
6d4747a9
JM
1959 return pos - buf;
1960 pos += ret;
d23bd894 1961
64855b96
JM
1962#ifdef CONFIG_HS20
1963 if (wpa_s->current_bss &&
f9cd147d
JM
1964 (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1965 HS20_IE_VENDOR_TYPE)) &&
4ed34f5a
JM
1966 wpa_s->wpa_proto == WPA_PROTO_RSN &&
1967 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
f9cd147d
JM
1968 int release = 1;
1969 if (hs20[1] >= 5) {
1970 u8 rel_num = (hs20[6] & 0xf0) >> 4;
1971 release = rel_num + 1;
1972 }
1973 ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
d85e1fc8 1974 if (os_snprintf_error(end - pos, ret))
64855b96
JM
1975 return pos - buf;
1976 pos += ret;
1977 }
e99b4f3a
JM
1978
1979 if (wpa_s->current_ssid) {
1980 struct wpa_cred *cred;
1981 char *type;
1982
1983 for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
463c8ffb
JM
1984 size_t i;
1985
e99b4f3a
JM
1986 if (wpa_s->current_ssid->parent_cred != cred)
1987 continue;
e99b4f3a 1988
aa26ba68 1989 if (cred->provisioning_sp) {
463c8ffb 1990 ret = os_snprintf(pos, end - pos,
aa26ba68
JM
1991 "provisioning_sp=%s\n",
1992 cred->provisioning_sp);
d85e1fc8 1993 if (os_snprintf_error(end - pos, ret))
463c8ffb
JM
1994 return pos - buf;
1995 pos += ret;
1996 }
e99b4f3a 1997
aa26ba68
JM
1998 if (!cred->domain)
1999 goto no_domain;
2000
2001 i = 0;
2002 if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
2003 struct wpabuf *names =
2004 wpa_s->current_bss->anqp->domain_name;
2005 for (i = 0; names && i < cred->num_domain; i++)
2006 {
2007 if (domain_name_list_contains(
2008 names, cred->domain[i], 1))
2009 break;
2010 }
2011 if (i == cred->num_domain)
2012 i = 0; /* show first entry by default */
2013 }
2014 ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
2015 cred->domain[i]);
d85e1fc8 2016 if (os_snprintf_error(end - pos, ret))
aa26ba68
JM
2017 return pos - buf;
2018 pos += ret;
2019
2020 no_domain:
e99b4f3a
JM
2021 if (wpa_s->current_bss == NULL ||
2022 wpa_s->current_bss->anqp == NULL)
2023 res = -1;
2024 else
2025 res = interworking_home_sp_cred(
2026 wpa_s, cred,
2027 wpa_s->current_bss->anqp->domain_name);
2028 if (res > 0)
2029 type = "home";
2030 else if (res == 0)
2031 type = "roaming";
2032 else
2033 type = "unknown";
2034
2035 ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
d85e1fc8 2036 if (os_snprintf_error(end - pos, ret))
e99b4f3a
JM
2037 return pos - buf;
2038 pos += ret;
2039
2040 break;
2041 }
2042 }
64855b96
JM
2043#endif /* CONFIG_HS20 */
2044
56586197
JM
2045 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2046 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
6fc6879b
JM
2047 res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
2048 verbose);
2049 if (res >= 0)
2050 pos += res;
2051 }
2052
7508c2ad
BA
2053#ifdef CONFIG_MACSEC
2054 res = ieee802_1x_kay_get_status(wpa_s->kay, pos, end - pos);
2055 if (res > 0)
2056 pos += res;
2057#endif /* CONFIG_MACSEC */
2058
993a8654
JM
2059 sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
2060 if (sess_id) {
2061 char *start = pos;
2062
2063 ret = os_snprintf(pos, end - pos, "eap_session_id=");
2064 if (os_snprintf_error(end - pos, ret))
2065 return start - buf;
2066 pos += ret;
2067 ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
2068 if (ret <= 0)
2069 return start - buf;
2070 pos += ret;
2071 ret = os_snprintf(pos, end - pos, "\n");
2072 if (os_snprintf_error(end - pos, ret))
2073 return start - buf;
2074 pos += ret;
2075 }
2076
6fc6879b
JM
2077 res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
2078 if (res >= 0)
2079 pos += res;
2080
8aaafcee
JM
2081#ifdef CONFIG_WPS
2082 {
2083 char uuid_str[100];
2084 uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
2085 ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
d85e1fc8 2086 if (os_snprintf_error(end - pos, ret))
8aaafcee
JM
2087 return pos - buf;
2088 pos += ret;
2089 }
2090#endif /* CONFIG_WPS */
2091
f6c2b8c3 2092#ifdef ANDROID
a6ab82d7 2093 /*
2094 * Allow using the STATUS command with default behavior, say for debug,
2095 * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
2096 * events with STATUS-NO_EVENTS.
2097 */
2098 if (os_strcmp(params, "-NO_EVENTS")) {
2099 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
2100 "id=%d state=%d BSSID=" MACSTR " SSID=%s",
2101 wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
2102 wpa_s->wpa_state,
2103 MAC2STR(wpa_s->bssid),
2104 wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
2105 wpa_ssid_txt(wpa_s->current_ssid->ssid,
2106 wpa_s->current_ssid->ssid_len) : "");
2107 if (wpa_s->wpa_state == WPA_COMPLETED) {
2108 struct wpa_ssid *ssid = wpa_s->current_ssid;
2109 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
2110 "- connection to " MACSTR
2111 " completed %s [id=%d id_str=%s]",
2112 MAC2STR(wpa_s->bssid), "(auth)",
2113 ssid ? ssid->id : -1,
2114 ssid && ssid->id_str ? ssid->id_str : "");
2115 }
f6c2b8c3
DS
2116 }
2117#endif /* ANDROID */
2118
6fc6879b
JM
2119 return pos - buf;
2120}
2121
2122
2123static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
2124 char *cmd)
2125{
2126 char *pos;
2127 int id;
2128 struct wpa_ssid *ssid;
2129 u8 bssid[ETH_ALEN];
2130
2131 /* cmd: "<network id> <BSSID>" */
2132 pos = os_strchr(cmd, ' ');
2133 if (pos == NULL)
2134 return -1;
2135 *pos++ = '\0';
2136 id = atoi(cmd);
2137 wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
2138 if (hwaddr_aton(pos, bssid)) {
2139 wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
2140 return -1;
2141 }
2142
2143 ssid = wpa_config_get_network(wpa_s->conf, id);
2144 if (ssid == NULL) {
2145 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2146 "to update", id);
2147 return -1;
2148 }
2149
2150 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
a8e16edc 2151 ssid->bssid_set = !is_zero_ether_addr(bssid);
6fc6879b
JM
2152
2153 return 0;
2154}
2155
2156
9aa10e2b
DS
2157static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
2158 char *cmd, char *buf,
2159 size_t buflen)
2160{
2161 u8 bssid[ETH_ALEN];
2162 struct wpa_blacklist *e;
2163 char *pos, *end;
2164 int ret;
2165
2166 /* cmd: "BLACKLIST [<BSSID>]" */
2167 if (*cmd == '\0') {
2168 pos = buf;
2169 end = buf + buflen;
2170 e = wpa_s->blacklist;
2171 while (e) {
2172 ret = os_snprintf(pos, end - pos, MACSTR "\n",
2173 MAC2STR(e->bssid));
d85e1fc8 2174 if (os_snprintf_error(end - pos, ret))
9aa10e2b
DS
2175 return pos - buf;
2176 pos += ret;
2177 e = e->next;
2178 }
2179 return pos - buf;
2180 }
2181
2182 cmd++;
2183 if (os_strncmp(cmd, "clear", 5) == 0) {
2184 wpa_blacklist_clear(wpa_s);
2185 os_memcpy(buf, "OK\n", 3);
2186 return 3;
2187 }
2188
2189 wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
2190 if (hwaddr_aton(cmd, bssid)) {
2191 wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
2192 return -1;
2193 }
2194
2195 /*
2196 * Add the BSSID twice, so its count will be 2, causing it to be
2197 * skipped when processing scan results.
2198 */
2199 ret = wpa_blacklist_add(wpa_s, bssid);
bd8838a3 2200 if (ret < 0)
9aa10e2b
DS
2201 return -1;
2202 ret = wpa_blacklist_add(wpa_s, bssid);
bd8838a3 2203 if (ret < 0)
9aa10e2b
DS
2204 return -1;
2205 os_memcpy(buf, "OK\n", 3);
2206 return 3;
2207}
2208
2209
0597a5b5
DS
2210static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
2211 char *cmd, char *buf,
2212 size_t buflen)
2213{
2214 char *pos, *end, *stamp;
2215 int ret;
2216
0597a5b5
DS
2217 /* cmd: "LOG_LEVEL [<level>]" */
2218 if (*cmd == '\0') {
2219 pos = buf;
2220 end = buf + buflen;
2221 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2222 "Timestamp: %d\n",
2223 debug_level_str(wpa_debug_level),
2224 wpa_debug_timestamp);
d85e1fc8 2225 if (os_snprintf_error(end - pos, ret))
0597a5b5
DS
2226 ret = 0;
2227
2228 return ret;
2229 }
2230
2231 while (*cmd == ' ')
2232 cmd++;
2233
2234 stamp = os_strchr(cmd, ' ');
2235 if (stamp) {
2236 *stamp++ = '\0';
2237 while (*stamp == ' ') {
2238 stamp++;
2239 }
2240 }
2241
137b2939 2242 if (os_strlen(cmd)) {
0597a5b5
DS
2243 int level = str_to_debug_level(cmd);
2244 if (level < 0)
2245 return -1;
2246 wpa_debug_level = level;
2247 }
2248
2249 if (stamp && os_strlen(stamp))
2250 wpa_debug_timestamp = atoi(stamp);
2251
2252 os_memcpy(buf, "OK\n", 3);
2253 return 3;
2254}
2255
2256
6fc6879b 2257static int wpa_supplicant_ctrl_iface_list_networks(
90903a77 2258 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
6fc6879b 2259{
f34891a3 2260 char *pos, *end, *prev;
6fc6879b
JM
2261 struct wpa_ssid *ssid;
2262 int ret;
2263
2264 pos = buf;
2265 end = buf + buflen;
2266 ret = os_snprintf(pos, end - pos,
2267 "network id / ssid / bssid / flags\n");
d85e1fc8 2268 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2269 return pos - buf;
2270 pos += ret;
2271
2272 ssid = wpa_s->conf->ssid;
90903a77
VD
2273
2274 /* skip over ssids until we find next one */
2275 if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
2276 int last_id = atoi(cmd + 8);
2277 if (last_id != -1) {
2278 while (ssid != NULL && ssid->id <= last_id) {
2279 ssid = ssid->next;
2280 }
2281 }
2282 }
2283
6fc6879b 2284 while (ssid) {
f34891a3 2285 prev = pos;
6fc6879b
JM
2286 ret = os_snprintf(pos, end - pos, "%d\t%s",
2287 ssid->id,
2288 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
d85e1fc8 2289 if (os_snprintf_error(end - pos, ret))
f34891a3 2290 return prev - buf;
6fc6879b
JM
2291 pos += ret;
2292 if (ssid->bssid_set) {
2293 ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2294 MAC2STR(ssid->bssid));
2295 } else {
2296 ret = os_snprintf(pos, end - pos, "\tany");
2297 }
d85e1fc8 2298 if (os_snprintf_error(end - pos, ret))
f34891a3 2299 return prev - buf;
6fc6879b 2300 pos += ret;
00e5e3d5 2301 ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
6fc6879b
JM
2302 ssid == wpa_s->current_ssid ?
2303 "[CURRENT]" : "",
4dac0245 2304 ssid->disabled ? "[DISABLED]" : "",
00e5e3d5
JM
2305 ssid->disabled_until.sec ?
2306 "[TEMP-DISABLED]" : "",
4dac0245
JM
2307 ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2308 "");
d85e1fc8 2309 if (os_snprintf_error(end - pos, ret))
f34891a3 2310 return prev - buf;
6fc6879b
JM
2311 pos += ret;
2312 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 2313 if (os_snprintf_error(end - pos, ret))
f34891a3 2314 return prev - buf;
6fc6879b
JM
2315 pos += ret;
2316
2317 ssid = ssid->next;
2318 }
2319
2320 return pos - buf;
2321}
2322
2323
2324static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2325{
0282a8c4 2326 int ret;
6fc6879b 2327 ret = os_snprintf(pos, end - pos, "-");
d85e1fc8 2328 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2329 return pos;
2330 pos += ret;
0282a8c4
JM
2331 ret = wpa_write_ciphers(pos, end, cipher, "+");
2332 if (ret < 0)
2333 return pos;
2334 pos += ret;
6fc6879b
JM
2335 return pos;
2336}
2337
2338
2339static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2340 const u8 *ie, size_t ie_len)
2341{
2342 struct wpa_ie_data data;
ea3b8c1d
JM
2343 char *start;
2344 int ret;
6fc6879b
JM
2345
2346 ret = os_snprintf(pos, end - pos, "[%s-", proto);
d85e1fc8 2347 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2348 return pos;
2349 pos += ret;
2350
2351 if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2352 ret = os_snprintf(pos, end - pos, "?]");
d85e1fc8 2353 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2354 return pos;
2355 pos += ret;
2356 return pos;
2357 }
2358
ea3b8c1d 2359 start = pos;
6fc6879b 2360 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
ea3b8c1d
JM
2361 ret = os_snprintf(pos, end - pos, "%sEAP",
2362 pos == start ? "" : "+");
d85e1fc8 2363 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2364 return pos;
2365 pos += ret;
6fc6879b
JM
2366 }
2367 if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
ea3b8c1d
JM
2368 ret = os_snprintf(pos, end - pos, "%sPSK",
2369 pos == start ? "" : "+");
d85e1fc8 2370 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2371 return pos;
2372 pos += ret;
6fc6879b
JM
2373 }
2374 if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
ea3b8c1d
JM
2375 ret = os_snprintf(pos, end - pos, "%sNone",
2376 pos == start ? "" : "+");
d85e1fc8 2377 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2378 return pos;
2379 pos += ret;
6fc6879b 2380 }
be6b29f6
JA
2381 if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
2382 ret = os_snprintf(pos, end - pos, "%sSAE",
2383 pos == start ? "" : "+");
d85e1fc8 2384 if (os_snprintf_error(end - pos, ret))
be6b29f6
JA
2385 return pos;
2386 pos += ret;
2387 }
6fc6879b
JM
2388#ifdef CONFIG_IEEE80211R
2389 if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2390 ret = os_snprintf(pos, end - pos, "%sFT/EAP",
ea3b8c1d 2391 pos == start ? "" : "+");
d85e1fc8 2392 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2393 return pos;
2394 pos += ret;
6fc6879b
JM
2395 }
2396 if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2397 ret = os_snprintf(pos, end - pos, "%sFT/PSK",
ea3b8c1d 2398 pos == start ? "" : "+");
d85e1fc8 2399 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2400 return pos;
2401 pos += ret;
6fc6879b 2402 }
be6b29f6
JA
2403 if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
2404 ret = os_snprintf(pos, end - pos, "%sFT/SAE",
2405 pos == start ? "" : "+");
d85e1fc8 2406 if (os_snprintf_error(end - pos, ret))
be6b29f6
JA
2407 return pos;
2408 pos += ret;
2409 }
6fc6879b 2410#endif /* CONFIG_IEEE80211R */
56586197
JM
2411#ifdef CONFIG_IEEE80211W
2412 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2413 ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
ea3b8c1d 2414 pos == start ? "" : "+");
d85e1fc8 2415 if (os_snprintf_error(end - pos, ret))
56586197
JM
2416 return pos;
2417 pos += ret;
56586197
JM
2418 }
2419 if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2420 ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
ea3b8c1d 2421 pos == start ? "" : "+");
d85e1fc8 2422 if (os_snprintf_error(end - pos, ret))
56586197
JM
2423 return pos;
2424 pos += ret;
56586197
JM
2425 }
2426#endif /* CONFIG_IEEE80211W */
6fc6879b 2427
5e3b5197 2428#ifdef CONFIG_SUITEB
666497c8
JM
2429 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
2430 ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
2431 pos == start ? "" : "+");
d85e1fc8 2432 if (os_snprintf_error(end - pos, ret))
666497c8
JM
2433 return pos;
2434 pos += ret;
2435 }
5e3b5197
JM
2436#endif /* CONFIG_SUITEB */
2437
2438#ifdef CONFIG_SUITEB192
2439 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
2440 ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
2441 pos == start ? "" : "+");
2442 if (os_snprintf_error(end - pos, ret))
2443 return pos;
2444 pos += ret;
2445 }
2446#endif /* CONFIG_SUITEB192 */
666497c8 2447
7147a834
JM
2448#ifdef CONFIG_FILS
2449 if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
2450 ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
2451 pos == start ? "" : "+");
2452 if (os_snprintf_error(end - pos, ret))
2453 return pos;
2454 pos += ret;
2455 }
2456 if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
2457 ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
2458 pos == start ? "" : "+");
2459 if (os_snprintf_error(end - pos, ret))
2460 return pos;
2461 pos += ret;
2462 }
2463#ifdef CONFIG_IEEE80211R
2464 if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
2465 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
2466 pos == start ? "" : "+");
2467 if (os_snprintf_error(end - pos, ret))
2468 return pos;
2469 pos += ret;
2470 }
2471 if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
2472 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
2473 pos == start ? "" : "+");
2474 if (os_snprintf_error(end - pos, ret))
2475 return pos;
2476 pos += ret;
2477 }
2478#endif /* CONFIG_IEEE80211R */
2479#endif /* CONFIG_FILS */
2480
0f8385e6
BG
2481 if (data.key_mgmt & WPA_KEY_MGMT_OSEN) {
2482 ret = os_snprintf(pos, end - pos, "%sOSEN",
2483 pos == start ? "" : "+");
2484 if (os_snprintf_error(end - pos, ret))
2485 return pos;
2486 pos += ret;
2487 }
2488
6fc6879b
JM
2489 pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2490
2491 if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2492 ret = os_snprintf(pos, end - pos, "-preauth");
d85e1fc8 2493 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2494 return pos;
2495 pos += ret;
2496 }
2497
2498 ret = os_snprintf(pos, end - pos, "]");
d85e1fc8 2499 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2500 return pos;
2501 pos += ret;
2502
2503 return pos;
2504}
2505
3a068632 2506
eef7d7a1 2507#ifdef CONFIG_WPS
31fcea93
JM
2508static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2509 char *pos, char *end,
3a068632
JM
2510 struct wpabuf *wps_ie)
2511{
eef7d7a1
JM
2512 int ret;
2513 const char *txt;
2514
eef7d7a1
JM
2515 if (wps_ie == NULL)
2516 return pos;
eef7d7a1
JM
2517 if (wps_is_selected_pbc_registrar(wps_ie))
2518 txt = "[WPS-PBC]";
31fcea93
JM
2519 else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2520 txt = "[WPS-AUTH]";
eef7d7a1
JM
2521 else if (wps_is_selected_pin_registrar(wps_ie))
2522 txt = "[WPS-PIN]";
2523 else
2524 txt = "[WPS]";
2525
2526 ret = os_snprintf(pos, end - pos, "%s", txt);
a80ba67a 2527 if (!os_snprintf_error(end - pos, ret))
eef7d7a1
JM
2528 pos += ret;
2529 wpabuf_free(wps_ie);
3a068632
JM
2530 return pos;
2531}
2532#endif /* CONFIG_WPS */
2533
2534
31fcea93
JM
2535static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2536 char *pos, char *end,
16b71ac2 2537 const struct wpa_bss *bss)
3a068632
JM
2538{
2539#ifdef CONFIG_WPS
2540 struct wpabuf *wps_ie;
2541 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
31fcea93 2542 return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
3a068632 2543#else /* CONFIG_WPS */
eef7d7a1 2544 return pos;
3a068632 2545#endif /* CONFIG_WPS */
eef7d7a1
JM
2546}
2547
6fc6879b
JM
2548
2549/* Format one result on one text line into a buffer. */
2550static int wpa_supplicant_ctrl_iface_scan_result(
31fcea93 2551 struct wpa_supplicant *wpa_s,
16b71ac2 2552 const struct wpa_bss *bss, char *buf, size_t buflen)
6fc6879b
JM
2553{
2554 char *pos, *end;
2555 int ret;
0f8385e6 2556 const u8 *ie, *ie2, *osen_ie, *p2p, *mesh;
0c6b310e 2557
638d9456 2558 mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
0c6b310e 2559 p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
bb50ae43
JM
2560 if (!p2p)
2561 p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
0c6b310e
JM
2562 if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2563 os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2564 0)
2565 return 0; /* Do not show P2P listen discovery results here */
6fc6879b
JM
2566
2567 pos = buf;
2568 end = buf + buflen;
2569
2570 ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
16b71ac2 2571 MAC2STR(bss->bssid), bss->freq, bss->level);
d85e1fc8 2572 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2573 return -1;
6fc6879b 2574 pos += ret;
16b71ac2 2575 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
6fc6879b
JM
2576 if (ie)
2577 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
16b71ac2 2578 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
638d9456
JA
2579 if (ie2) {
2580 pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
2581 ie2, 2 + ie2[1]);
2582 }
0f8385e6
BG
2583 osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
2584 if (osen_ie)
2585 pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
2586 osen_ie, 2 + osen_ie[1]);
31fcea93 2587 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
0f8385e6 2588 if (!ie && !ie2 && !osen_ie && (bss->caps & IEEE80211_CAP_PRIVACY)) {
6fc6879b 2589 ret = os_snprintf(pos, end - pos, "[WEP]");
d85e1fc8 2590 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2591 return -1;
6fc6879b
JM
2592 pos += ret;
2593 }
638d9456
JA
2594 if (mesh) {
2595 ret = os_snprintf(pos, end - pos, "[MESH]");
d85e1fc8 2596 if (os_snprintf_error(end - pos, ret))
638d9456
JA
2597 return -1;
2598 pos += ret;
2599 }
e403ba85
BS
2600 if (bss_is_dmg(bss)) {
2601 const char *s;
2602 ret = os_snprintf(pos, end - pos, "[DMG]");
d85e1fc8 2603 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2604 return -1;
6fc6879b 2605 pos += ret;
e403ba85
BS
2606 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
2607 case IEEE80211_CAP_DMG_IBSS:
2608 s = "[IBSS]";
2609 break;
2610 case IEEE80211_CAP_DMG_AP:
2611 s = "[ESS]";
2612 break;
2613 case IEEE80211_CAP_DMG_PBSS:
2614 s = "[PBSS]";
2615 break;
2616 default:
2617 s = "";
2618 break;
2619 }
2620 ret = os_snprintf(pos, end - pos, "%s", s);
d85e1fc8 2621 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2622 return -1;
bd1af96a 2623 pos += ret;
e403ba85
BS
2624 } else {
2625 if (bss->caps & IEEE80211_CAP_IBSS) {
2626 ret = os_snprintf(pos, end - pos, "[IBSS]");
d85e1fc8 2627 if (os_snprintf_error(end - pos, ret))
e403ba85
BS
2628 return -1;
2629 pos += ret;
2630 }
2631 if (bss->caps & IEEE80211_CAP_ESS) {
2632 ret = os_snprintf(pos, end - pos, "[ESS]");
d85e1fc8 2633 if (os_snprintf_error(end - pos, ret))
e403ba85
BS
2634 return -1;
2635 pos += ret;
2636 }
bd1af96a 2637 }
0c6b310e
JM
2638 if (p2p) {
2639 ret = os_snprintf(pos, end - pos, "[P2P]");
d85e1fc8 2640 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2641 return -1;
0c6b310e
JM
2642 pos += ret;
2643 }
64855b96 2644#ifdef CONFIG_HS20
4ed34f5a 2645 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
64855b96 2646 ret = os_snprintf(pos, end - pos, "[HS20]");
d85e1fc8 2647 if (os_snprintf_error(end - pos, ret))
64855b96
JM
2648 return -1;
2649 pos += ret;
2650 }
2651#endif /* CONFIG_HS20 */
7147a834
JM
2652#ifdef CONFIG_FILS
2653 if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
2654 ret = os_snprintf(pos, end - pos, "[FILS]");
2655 if (os_snprintf_error(end - pos, ret))
2656 return -1;
2657 pos += ret;
2658 }
2659#endif /* CONFIG_FILS */
55de4d4b
AN
2660#ifdef CONFIG_FST
2661 if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
2662 ret = os_snprintf(pos, end - pos, "[FST]");
2663 if (os_snprintf_error(end - pos, ret))
2664 return -1;
2665 pos += ret;
2666 }
2667#endif /* CONFIG_FST */
6fc6879b 2668
6fc6879b 2669 ret = os_snprintf(pos, end - pos, "\t%s",
16b71ac2 2670 wpa_ssid_txt(bss->ssid, bss->ssid_len));
d85e1fc8 2671 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2672 return -1;
6fc6879b
JM
2673 pos += ret;
2674
2675 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 2676 if (os_snprintf_error(end - pos, ret))
fb0e5bd7 2677 return -1;
6fc6879b
JM
2678 pos += ret;
2679
2680 return pos - buf;
2681}
2682
2683
2684static int wpa_supplicant_ctrl_iface_scan_results(
2685 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2686{
2687 char *pos, *end;
16b71ac2 2688 struct wpa_bss *bss;
6fc6879b 2689 int ret;
6fc6879b
JM
2690
2691 pos = buf;
2692 end = buf + buflen;
2693 ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
2694 "flags / ssid\n");
d85e1fc8 2695 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2696 return pos - buf;
2697 pos += ret;
2698
16b71ac2 2699 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
31fcea93 2700 ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
6fc6879b
JM
2701 end - pos);
2702 if (ret < 0 || ret >= end - pos)
2703 return pos - buf;
2704 pos += ret;
2705 }
2706
2707 return pos - buf;
2708}
2709
2710
603a3f34
JL
2711#ifdef CONFIG_MESH
2712
5b78493f
MH
2713static int wpa_supplicant_ctrl_iface_mesh_interface_add(
2714 struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
2715{
2716 char *pos, ifname[IFNAMSIZ + 1];
2717
2718 ifname[0] = '\0';
2719
2720 pos = os_strstr(cmd, "ifname=");
2721 if (pos) {
2722 pos += 7;
2723 os_strlcpy(ifname, pos, sizeof(ifname));
2724 }
2725
2726 if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
2727 return -1;
2728
2729 os_strlcpy(reply, ifname, max_len);
2730 return os_strlen(ifname);
2731}
2732
2733
603a3f34
JL
2734static int wpa_supplicant_ctrl_iface_mesh_group_add(
2735 struct wpa_supplicant *wpa_s, char *cmd)
2736{
2737 int id;
2738 struct wpa_ssid *ssid;
2739
2740 id = atoi(cmd);
2741 wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
2742
2743 ssid = wpa_config_get_network(wpa_s->conf, id);
2744 if (ssid == NULL) {
2745 wpa_printf(MSG_DEBUG,
2746 "CTRL_IFACE: Could not find network id=%d", id);
2747 return -1;
2748 }
2749 if (ssid->mode != WPAS_MODE_MESH) {
2750 wpa_printf(MSG_DEBUG,
2751 "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
2752 return -1;
2753 }
0c6099f3
MH
2754 if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
2755 ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
2756 wpa_printf(MSG_ERROR,
2757 "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
2758 return -1;
2759 }
603a3f34
JL
2760
2761 /*
2762 * TODO: If necessary write our own group_add function,
2763 * for now we can reuse select_network
2764 */
2765 wpa_supplicant_select_network(wpa_s, ssid);
2766
2767 return 0;
2768}
2769
2770
2771static int wpa_supplicant_ctrl_iface_mesh_group_remove(
2772 struct wpa_supplicant *wpa_s, char *cmd)
2773{
5b78493f
MH
2774 struct wpa_supplicant *orig;
2775 struct wpa_global *global;
2776 int found = 0;
2777
2778 wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
2779
2780 global = wpa_s->global;
2781 orig = wpa_s;
2782
2783 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2784 if (os_strcmp(wpa_s->ifname, cmd) == 0) {
2785 found = 1;
2786 break;
2787 }
2788 }
2789 if (!found) {
2790 wpa_printf(MSG_ERROR,
2791 "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
603a3f34
JL
2792 cmd);
2793 return -1;
2794 }
5b78493f
MH
2795 if (wpa_s->mesh_if_created && wpa_s == orig) {
2796 wpa_printf(MSG_ERROR,
2797 "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
2798 return -1;
2799 }
603a3f34
JL
2800
2801 wpa_s->reassociate = 0;
2802 wpa_s->disconnected = 1;
2803 wpa_supplicant_cancel_sched_scan(wpa_s);
2804 wpa_supplicant_cancel_scan(wpa_s);
2805
2806 /*
2807 * TODO: If necessary write our own group_remove function,
2808 * for now we can reuse deauthenticate
2809 */
2810 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2811
5b78493f
MH
2812 if (wpa_s->mesh_if_created)
2813 wpa_supplicant_remove_iface(global, wpa_s, 0);
2814
603a3f34
JL
2815 return 0;
2816}
2817
e174ef34
MH
2818
2819static int wpa_supplicant_ctrl_iface_mesh_peer_remove(
2820 struct wpa_supplicant *wpa_s, char *cmd)
2821{
2822 u8 addr[ETH_ALEN];
2823
2824 if (hwaddr_aton(cmd, addr) < 0)
2825 return -1;
2826
2827 return wpas_mesh_peer_remove(wpa_s, addr);
2828}
2829
2604edbf
MH
2830
2831static int wpa_supplicant_ctrl_iface_mesh_peer_add(
2832 struct wpa_supplicant *wpa_s, char *cmd)
2833{
2834 u8 addr[ETH_ALEN];
9f2cf23e
MH
2835 int duration;
2836 char *pos;
2837
2838 pos = os_strstr(cmd, " duration=");
2839 if (pos) {
2840 *pos = '\0';
2841 duration = atoi(pos + 10);
2842 } else {
2843 duration = -1;
2844 }
2604edbf
MH
2845
2846 if (hwaddr_aton(cmd, addr))
2847 return -1;
2848
9f2cf23e 2849 return wpas_mesh_peer_add(wpa_s, addr, duration);
2604edbf
MH
2850}
2851
603a3f34
JL
2852#endif /* CONFIG_MESH */
2853
2854
6fc6879b
JM
2855static int wpa_supplicant_ctrl_iface_select_network(
2856 struct wpa_supplicant *wpa_s, char *cmd)
2857{
2858 int id;
2859 struct wpa_ssid *ssid;
204c9ac4 2860 char *pos;
6fc6879b
JM
2861
2862 /* cmd: "<network id>" or "any" */
204c9ac4 2863 if (os_strncmp(cmd, "any", 3) == 0) {
6fc6879b 2864 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
86b89452
WS
2865 ssid = NULL;
2866 } else {
2867 id = atoi(cmd);
2868 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
6fc6879b 2869
86b89452
WS
2870 ssid = wpa_config_get_network(wpa_s->conf, id);
2871 if (ssid == NULL) {
2872 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2873 "network id=%d", id);
2874 return -1;
2875 }
4dac0245
JM
2876 if (ssid->disabled == 2) {
2877 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2878 "SELECT_NETWORK with persistent P2P group");
2879 return -1;
2880 }
6fc6879b
JM
2881 }
2882
204c9ac4
DS
2883 pos = os_strstr(cmd, " freq=");
2884 if (pos) {
2885 int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
2886 if (freqs) {
2887 wpa_s->scan_req = MANUAL_SCAN_REQ;
2888 os_free(wpa_s->manual_scan_freqs);
2889 wpa_s->manual_scan_freqs = freqs;
2890 }
2891 }
2892
dfaf11d6
JM
2893 wpa_s->scan_min_time.sec = 0;
2894 wpa_s->scan_min_time.usec = 0;
86b89452 2895 wpa_supplicant_select_network(wpa_s, ssid);
6fc6879b
JM
2896
2897 return 0;
2898}
2899
2900
2901static int wpa_supplicant_ctrl_iface_enable_network(
2902 struct wpa_supplicant *wpa_s, char *cmd)
2903{
2904 int id;
2905 struct wpa_ssid *ssid;
2906
2907 /* cmd: "<network id>" or "all" */
2908 if (os_strcmp(cmd, "all") == 0) {
2909 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
86b89452
WS
2910 ssid = NULL;
2911 } else {
2912 id = atoi(cmd);
2913 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
6fc6879b 2914
86b89452
WS
2915 ssid = wpa_config_get_network(wpa_s->conf, id);
2916 if (ssid == NULL) {
2917 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2918 "network id=%d", id);
2919 return -1;
2920 }
4dac0245
JM
2921 if (ssid->disabled == 2) {
2922 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2923 "ENABLE_NETWORK with persistent P2P group");
2924 return -1;
2925 }
84c78f95
JM
2926
2927 if (os_strstr(cmd, " no-connect")) {
2928 ssid->disabled = 0;
2929 return 0;
2930 }
6fc6879b 2931 }
dfaf11d6
JM
2932 wpa_s->scan_min_time.sec = 0;
2933 wpa_s->scan_min_time.usec = 0;
86b89452 2934 wpa_supplicant_enable_network(wpa_s, ssid);
6fc6879b
JM
2935
2936 return 0;
2937}
2938
2939
2940static int wpa_supplicant_ctrl_iface_disable_network(
2941 struct wpa_supplicant *wpa_s, char *cmd)
2942{
2943 int id;
2944 struct wpa_ssid *ssid;
2945
2946 /* cmd: "<network id>" or "all" */
2947 if (os_strcmp(cmd, "all") == 0) {
2948 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
86b89452
WS
2949 ssid = NULL;
2950 } else {
2951 id = atoi(cmd);
2952 wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
6fc6879b 2953
86b89452
WS
2954 ssid = wpa_config_get_network(wpa_s->conf, id);
2955 if (ssid == NULL) {
2956 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
2957 "network id=%d", id);
2958 return -1;
2959 }
4dac0245
JM
2960 if (ssid->disabled == 2) {
2961 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
2962 "DISABLE_NETWORK with persistent P2P "
2963 "group");
2964 return -1;
2965 }
6fc6879b 2966 }
86b89452 2967 wpa_supplicant_disable_network(wpa_s, ssid);
6fc6879b
JM
2968
2969 return 0;
2970}
2971
2972
2973static int wpa_supplicant_ctrl_iface_add_network(
2974 struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
2975{
2976 struct wpa_ssid *ssid;
2977 int ret;
2978
2979 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
2980
d015bb05 2981 ssid = wpa_supplicant_add_network(wpa_s);
6fc6879b
JM
2982 if (ssid == NULL)
2983 return -1;
8bac466b 2984
6fc6879b 2985 ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
d85e1fc8 2986 if (os_snprintf_error(buflen, ret))
6fc6879b
JM
2987 return -1;
2988 return ret;
2989}
2990
2991
2992static int wpa_supplicant_ctrl_iface_remove_network(
2993 struct wpa_supplicant *wpa_s, char *cmd)
2994{
2995 int id;
2996 struct wpa_ssid *ssid;
d015bb05 2997 int result;
6fc6879b
JM
2998
2999 /* cmd: "<network id>" or "all" */
3000 if (os_strcmp(cmd, "all") == 0) {
3001 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
725fc39e
DS
3002 if (wpa_s->sched_scanning)
3003 wpa_supplicant_cancel_sched_scan(wpa_s);
3004
d8a790b9 3005 eapol_sm_invalidate_cached_session(wpa_s->eapol);
6fc6879b 3006 if (wpa_s->current_ssid) {
83df8149
JM
3007#ifdef CONFIG_SME
3008 wpa_s->sme.prev_bssid_set = 0;
3009#endif /* CONFIG_SME */
20a0b03d
JM
3010 wpa_sm_set_config(wpa_s->wpa, NULL);
3011 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
e66bcedd
JM
3012 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3013 wpa_s->own_disconnect_req = 1;
07783eaa
JM
3014 wpa_supplicant_deauthenticate(
3015 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
6fc6879b 3016 }
391f4925
JK
3017 ssid = wpa_s->conf->ssid;
3018 while (ssid) {
3019 struct wpa_ssid *remove_ssid = ssid;
3020 id = ssid->id;
3021 ssid = ssid->next;
c267753b
JM
3022 if (wpa_s->last_ssid == remove_ssid)
3023 wpa_s->last_ssid = NULL;
391f4925
JK
3024 wpas_notify_network_removed(wpa_s, remove_ssid);
3025 wpa_config_remove_network(wpa_s->conf, id);
3026 }
6fc6879b
JM
3027 return 0;
3028 }
3029
3030 id = atoi(cmd);
3031 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
3032
d015bb05
RP
3033 result = wpa_supplicant_remove_network(wpa_s, id);
3034 if (result == -1) {
6fc6879b
JM
3035 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3036 "id=%d", id);
3037 return -1;
3038 }
d015bb05 3039 if (result == -2) {
59ff6653
DG
3040 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
3041 "network id=%d", id);
3042 return -1;
3043 }
6fc6879b
JM
3044 return 0;
3045}
3046
3047
1c330a2f
DS
3048static int wpa_supplicant_ctrl_iface_update_network(
3049 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3050 char *name, char *value)
3051{
5cd317d3
BKB
3052 int ret;
3053
3054 ret = wpa_config_set(ssid, name, value, 0);
3055 if (ret < 0) {
1c330a2f
DS
3056 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
3057 "variable '%s'", name);
3058 return -1;
3059 }
5cd317d3
BKB
3060 if (ret == 1)
3061 return 0; /* No change to the previously configured value */
1c330a2f
DS
3062
3063 if (os_strcmp(name, "bssid") != 0 &&
c3dc68e8 3064 os_strcmp(name, "priority") != 0) {
1c330a2f
DS
3065 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
3066
c3dc68e8
JM
3067 if (wpa_s->current_ssid == ssid ||
3068 wpa_s->current_ssid == NULL) {
3069 /*
3070 * Invalidate the EAP session cache if anything in the
3071 * current or previously used configuration changes.
3072 */
3073 eapol_sm_invalidate_cached_session(wpa_s->eapol);
3074 }
1c330a2f
DS
3075 }
3076
3077 if ((os_strcmp(name, "psk") == 0 &&
3078 value[0] == '"' && ssid->ssid_len) ||
3079 (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
3080 wpa_config_update_psk(ssid);
3081 else if (os_strcmp(name, "priority") == 0)
3082 wpa_config_update_prio_list(wpa_s->conf);
3083
3084 return 0;
3085}
3086
3087
6fc6879b
JM
3088static int wpa_supplicant_ctrl_iface_set_network(
3089 struct wpa_supplicant *wpa_s, char *cmd)
3090{
1e529832 3091 int id, ret, prev_bssid_set, prev_disabled;
6fc6879b
JM
3092 struct wpa_ssid *ssid;
3093 char *name, *value;
0ef023e4 3094 u8 prev_bssid[ETH_ALEN];
6fc6879b
JM
3095
3096 /* cmd: "<network id> <variable name> <value>" */
3097 name = os_strchr(cmd, ' ');
3098 if (name == NULL)
3099 return -1;
3100 *name++ = '\0';
3101
3102 value = os_strchr(name, ' ');
3103 if (value == NULL)
3104 return -1;
3105 *value++ = '\0';
3106
3107 id = atoi(cmd);
3108 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
3109 id, name);
3110 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3111 (u8 *) value, os_strlen(value));
3112
3113 ssid = wpa_config_get_network(wpa_s->conf, id);
3114 if (ssid == NULL) {
3115 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3116 "id=%d", id);
3117 return -1;
3118 }
3119
0ef023e4 3120 prev_bssid_set = ssid->bssid_set;
1e529832 3121 prev_disabled = ssid->disabled;
0ef023e4
JM
3122 os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
3123 ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
3124 value);
3125 if (ret == 0 &&
3126 (ssid->bssid_set != prev_bssid_set ||
3127 os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
3128 wpas_notify_network_bssid_set_changed(wpa_s, ssid);
1e529832
JM
3129
3130 if (prev_disabled != ssid->disabled &&
3131 (prev_disabled == 2 || ssid->disabled == 2))
3132 wpas_notify_network_type_changed(wpa_s, ssid);
3133
0ef023e4 3134 return ret;
6fc6879b
JM
3135}
3136
3137
3138static int wpa_supplicant_ctrl_iface_get_network(
3139 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
3140{
3141 int id;
3142 size_t res;
3143 struct wpa_ssid *ssid;
3144 char *name, *value;
3145
3146 /* cmd: "<network id> <variable name>" */
3147 name = os_strchr(cmd, ' ');
3148 if (name == NULL || buflen == 0)
3149 return -1;
3150 *name++ = '\0';
3151
3152 id = atoi(cmd);
8db9a79d 3153 wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
6fc6879b
JM
3154 id, name);
3155
3156 ssid = wpa_config_get_network(wpa_s->conf, id);
3157 if (ssid == NULL) {
8db9a79d 3158 wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Could not find network "
6fc6879b
JM
3159 "id=%d", id);
3160 return -1;
3161 }
3162
3163 value = wpa_config_get_no_key(ssid, name);
3164 if (value == NULL) {
8db9a79d 3165 wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Failed to get network "
6fc6879b
JM
3166 "variable '%s'", 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
1c330a2f 3182static int wpa_supplicant_ctrl_iface_dup_network(
daae4995
AN
3183 struct wpa_supplicant *wpa_s, char *cmd,
3184 struct wpa_supplicant *dst_wpa_s)
1c330a2f
DS
3185{
3186 struct wpa_ssid *ssid_s, *ssid_d;
3187 char *name, *id, *value;
3188 int id_s, id_d, ret;
3189
3190 /* cmd: "<src network id> <dst network id> <variable name>" */
3191 id = os_strchr(cmd, ' ');
3192 if (id == NULL)
3193 return -1;
3194 *id++ = '\0';
3195
3196 name = os_strchr(id, ' ');
3197 if (name == NULL)
3198 return -1;
3199 *name++ = '\0';
3200
3201 id_s = atoi(cmd);
3202 id_d = atoi(id);
daae4995
AN
3203
3204 wpa_printf(MSG_DEBUG,
3205 "CTRL_IFACE: DUP_NETWORK ifname=%s->%s id=%d->%d name='%s'",
3206 wpa_s->ifname, dst_wpa_s->ifname, id_s, id_d, name);
1c330a2f
DS
3207
3208 ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
3209 if (ssid_s == NULL) {
3210 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3211 "network id=%d", id_s);
3212 return -1;
3213 }
3214
daae4995 3215 ssid_d = wpa_config_get_network(dst_wpa_s->conf, id_d);
1c330a2f
DS
3216 if (ssid_d == NULL) {
3217 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
c0541906 3218 "network id=%d", id_d);
1c330a2f
DS
3219 return -1;
3220 }
3221
3222 value = wpa_config_get(ssid_s, name);
3223 if (value == NULL) {
3224 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
3225 "variable '%s'", name);
3226 return -1;
3227 }
3228
daae4995 3229 ret = wpa_supplicant_ctrl_iface_update_network(dst_wpa_s, ssid_d, name,
1c330a2f
DS
3230 value);
3231
3232 os_free(value);
3233
3234 return ret;
3235}
3236
3237
d94c9ee6
JM
3238static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
3239 char *buf, size_t buflen)
3240{
3241 char *pos, *end;
3242 struct wpa_cred *cred;
3243 int ret;
3244
3245 pos = buf;
3246 end = buf + buflen;
3247 ret = os_snprintf(pos, end - pos,
3248 "cred id / realm / username / domain / imsi\n");
d85e1fc8 3249 if (os_snprintf_error(end - pos, ret))
d94c9ee6
JM
3250 return pos - buf;
3251 pos += ret;
3252
3253 cred = wpa_s->conf->cred;
3254 while (cred) {
3255 ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
3256 cred->id, cred->realm ? cred->realm : "",
3257 cred->username ? cred->username : "",
463c8ffb 3258 cred->domain ? cred->domain[0] : "",
d94c9ee6 3259 cred->imsi ? cred->imsi : "");
d85e1fc8 3260 if (os_snprintf_error(end - pos, ret))
d94c9ee6
JM
3261 return pos - buf;
3262 pos += ret;
3263
3264 cred = cred->next;
3265 }
3266
3267 return pos - buf;
3268}
3269
3270
3271static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
3272 char *buf, size_t buflen)
3273{
3274 struct wpa_cred *cred;
3275 int ret;
3276
3277 wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
3278
3279 cred = wpa_config_add_cred(wpa_s->conf);
3280 if (cred == NULL)
3281 return -1;
3282
1619e9d5
JM
3283 wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
3284
d94c9ee6 3285 ret = os_snprintf(buf, buflen, "%d\n", cred->id);
d85e1fc8 3286 if (os_snprintf_error(buflen, ret))
d94c9ee6
JM
3287 return -1;
3288 return ret;
3289}
3290
3291
736d4f2d
JM
3292static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
3293 struct wpa_cred *cred)
3294{
3295 struct wpa_ssid *ssid;
3296 char str[20];
1619e9d5 3297 int id;
736d4f2d 3298
1619e9d5 3299 if (cred == NULL) {
736d4f2d
JM
3300 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3301 return -1;
3302 }
3303
1619e9d5
JM
3304 id = cred->id;
3305 if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
3306 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3307 return -1;
3308 }
3309
3310 wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
3311
736d4f2d
JM
3312 /* Remove any network entry created based on the removed credential */
3313 ssid = wpa_s->conf->ssid;
3314 while (ssid) {
3315 if (ssid->parent_cred == cred) {
1d399771
JM
3316 int res;
3317
736d4f2d
JM
3318 wpa_printf(MSG_DEBUG, "Remove network id %d since it "
3319 "used the removed credential", ssid->id);
1d399771
JM
3320 res = os_snprintf(str, sizeof(str), "%d", ssid->id);
3321 if (os_snprintf_error(sizeof(str), res))
3322 str[sizeof(str) - 1] = '\0';
736d4f2d
JM
3323 ssid = ssid->next;
3324 wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
3325 } else
3326 ssid = ssid->next;
3327 }
3328
3329 return 0;
3330}
3331
3332
d94c9ee6
JM
3333static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
3334 char *cmd)
3335{
3336 int id;
736d4f2d 3337 struct wpa_cred *cred, *prev;
d94c9ee6 3338
aa26ba68
JM
3339 /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
3340 * "provisioning_sp=<FQDN> */
d94c9ee6
JM
3341 if (os_strcmp(cmd, "all") == 0) {
3342 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
3343 cred = wpa_s->conf->cred;
3344 while (cred) {
736d4f2d 3345 prev = cred;
d94c9ee6 3346 cred = cred->next;
736d4f2d 3347 wpas_ctrl_remove_cred(wpa_s, prev);
d94c9ee6
JM
3348 }
3349 return 0;
3350 }
3351
9afe52eb
JM
3352 if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
3353 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
3354 cmd + 8);
3355 cred = wpa_s->conf->cred;
3356 while (cred) {
3357 prev = cred;
3358 cred = cred->next;
463c8ffb
JM
3359 if (prev->domain) {
3360 size_t i;
3361 for (i = 0; i < prev->num_domain; i++) {
3362 if (os_strcmp(prev->domain[i], cmd + 8)
3363 != 0)
3364 continue;
3365 wpas_ctrl_remove_cred(wpa_s, prev);
3366 break;
3367 }
3368 }
9afe52eb
JM
3369 }
3370 return 0;
3371 }
3372
aa26ba68
JM
3373 if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
3374 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
3375 cmd + 16);
3376 cred = wpa_s->conf->cred;
3377 while (cred) {
3378 prev = cred;
3379 cred = cred->next;
3380 if (prev->provisioning_sp &&
3381 os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
3382 wpas_ctrl_remove_cred(wpa_s, prev);
3383 }
3384 return 0;
3385 }
3386
d94c9ee6
JM
3387 id = atoi(cmd);
3388 wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
3389
3390 cred = wpa_config_get_cred(wpa_s->conf, id);
736d4f2d 3391 return wpas_ctrl_remove_cred(wpa_s, cred);
d94c9ee6
JM
3392}
3393
3394
3395static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
3396 char *cmd)
3397{
3398 int id;
3399 struct wpa_cred *cred;
3400 char *name, *value;
3401
3402 /* cmd: "<cred id> <variable name> <value>" */
3403 name = os_strchr(cmd, ' ');
3404 if (name == NULL)
3405 return -1;
3406 *name++ = '\0';
3407
3408 value = os_strchr(name, ' ');
3409 if (value == NULL)
3410 return -1;
3411 *value++ = '\0';
3412
3413 id = atoi(cmd);
3414 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
3415 id, name);
3416 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3417 (u8 *) value, os_strlen(value));
3418
3419 cred = wpa_config_get_cred(wpa_s->conf, id);
3420 if (cred == NULL) {
3421 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3422 id);
3423 return -1;
3424 }
3425
3426 if (wpa_config_set_cred(cred, name, value, 0) < 0) {
3427 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
3428 "variable '%s'", name);
3429 return -1;
3430 }
3431
1619e9d5
JM
3432 wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
3433
d94c9ee6
JM
3434 return 0;
3435}
3436
3437
c880ab87
JM
3438static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
3439 char *cmd, char *buf,
3440 size_t buflen)
3441{
3442 int id;
3443 size_t res;
3444 struct wpa_cred *cred;
3445 char *name, *value;
3446
3447 /* cmd: "<cred id> <variable name>" */
3448 name = os_strchr(cmd, ' ');
3449 if (name == NULL)
3450 return -1;
3451 *name++ = '\0';
3452
3453 id = atoi(cmd);
3454 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
3455 id, name);
3456
3457 cred = wpa_config_get_cred(wpa_s->conf, id);
3458 if (cred == NULL) {
3459 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3460 id);
3461 return -1;
3462 }
3463
3464 value = wpa_config_get_cred_no_key(cred, name);
3465 if (value == NULL) {
3466 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
3467 name);
3468 return -1;
3469 }
3470
3471 res = os_strlcpy(buf, value, buflen);
3472 if (res >= buflen) {
3473 os_free(value);
3474 return -1;
3475 }
3476
3477 os_free(value);
3478
3479 return res;
3480}
3481
3482
6fc6879b
JM
3483#ifndef CONFIG_NO_CONFIG_WRITE
3484static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
3485{
3486 int ret;
3487
3488 if (!wpa_s->conf->update_config) {
3489 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
3490 "to update configuration (update_config=0)");
3491 return -1;
3492 }
3493
3494 ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
3495 if (ret) {
3496 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
3497 "update configuration");
3498 } else {
3499 wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
3500 " updated");
3501 }
3502
3503 return ret;
3504}
3505#endif /* CONFIG_NO_CONFIG_WRITE */
3506
3507
4daa011b
JM
3508struct cipher_info {
3509 unsigned int capa;
3510 const char *name;
3511 int group_only;
3512};
3513
3514static const struct cipher_info ciphers[] = {
3515 { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
3516 { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
3517 { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
3518 { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
3519 { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
3520 { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
3521 { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
3522 { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
3523};
3524
b5f045de
JM
3525static const struct cipher_info ciphers_group_mgmt[] = {
3526 { WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
3527 { WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
3528 { WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
3529 { WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
3530};
3531
4daa011b 3532
6fc6879b
JM
3533static int ctrl_iface_get_capability_pairwise(int res, char *strict,
3534 struct wpa_driver_capa *capa,
3535 char *buf, size_t buflen)
3536{
ea3b8c1d 3537 int ret;
6fc6879b
JM
3538 char *pos, *end;
3539 size_t len;
4daa011b 3540 unsigned int i;
6fc6879b
JM
3541
3542 pos = buf;
3543 end = pos + buflen;
3544
3545 if (res < 0) {
3546 if (strict)
3547 return 0;
3548 len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
3549 if (len >= buflen)
3550 return -1;
3551 return len;
3552 }
3553
4daa011b
JM
3554 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3555 if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
3556 ret = os_snprintf(pos, end - pos, "%s%s",
ea3b8c1d
JM
3557 pos == buf ? "" : " ",
3558 ciphers[i].name);
d85e1fc8 3559 if (os_snprintf_error(end - pos, ret))
4daa011b
JM
3560 return pos - buf;
3561 pos += ret;
4daa011b 3562 }
6fc6879b
JM
3563 }
3564
3565 return pos - buf;
3566}
3567
3568
3569static int ctrl_iface_get_capability_group(int res, char *strict,
3570 struct wpa_driver_capa *capa,
3571 char *buf, size_t buflen)
3572{
ea3b8c1d 3573 int ret;
6fc6879b
JM
3574 char *pos, *end;
3575 size_t len;
4daa011b 3576 unsigned int i;
6fc6879b
JM
3577
3578 pos = buf;
3579 end = pos + buflen;
3580
3581 if (res < 0) {
3582 if (strict)
3583 return 0;
3584 len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
3585 if (len >= buflen)
3586 return -1;
3587 return len;
3588 }
3589
4daa011b
JM
3590 for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
3591 if (capa->enc & ciphers[i].capa) {
3592 ret = os_snprintf(pos, end - pos, "%s%s",
ea3b8c1d
JM
3593 pos == buf ? "" : " ",
3594 ciphers[i].name);
d85e1fc8 3595 if (os_snprintf_error(end - pos, ret))
4daa011b
JM
3596 return pos - buf;
3597 pos += ret;
4daa011b 3598 }
6fc6879b
JM
3599 }
3600
3601 return pos - buf;
3602}
3603
3604
b5f045de
JM
3605static int ctrl_iface_get_capability_group_mgmt(int res, char *strict,
3606 struct wpa_driver_capa *capa,
3607 char *buf, size_t buflen)
3608{
3609 int ret;
3610 char *pos, *end;
3611 unsigned int i;
3612
3613 pos = buf;
3614 end = pos + buflen;
3615
3616 if (res < 0)
3617 return 0;
3618
3619 for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
3620 if (capa->enc & ciphers_group_mgmt[i].capa) {
3621 ret = os_snprintf(pos, end - pos, "%s%s",
3622 pos == buf ? "" : " ",
3623 ciphers_group_mgmt[i].name);
3624 if (os_snprintf_error(end - pos, ret))
3625 return pos - buf;
3626 pos += ret;
3627 }
3628 }
3629
3630 return pos - buf;
3631}
3632
3633
6fc6879b
JM
3634static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
3635 struct wpa_driver_capa *capa,
3636 char *buf, size_t buflen)
3637{
3638 int ret;
3639 char *pos, *end;
3640 size_t len;
3641
3642 pos = buf;
3643 end = pos + buflen;
3644
3645 if (res < 0) {
3646 if (strict)
3647 return 0;
3648 len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
3649 "NONE", buflen);
3650 if (len >= buflen)
3651 return -1;
3652 return len;
3653 }
3654
3655 ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
d85e1fc8 3656 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3657 return pos - buf;
3658 pos += ret;
3659
3660 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3661 WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
3662 ret = os_snprintf(pos, end - pos, " WPA-EAP");
d85e1fc8 3663 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3664 return pos - buf;
3665 pos += ret;
3666 }
3667
3668 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
3669 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
3670 ret = os_snprintf(pos, end - pos, " WPA-PSK");
d85e1fc8 3671 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3672 return pos - buf;
3673 pos += ret;
3674 }
3675
3676 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
3677 ret = os_snprintf(pos, end - pos, " WPA-NONE");
d85e1fc8 3678 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3679 return pos - buf;
3680 pos += ret;
3681 }
3682
399e6135
JM
3683#ifdef CONFIG_SUITEB
3684 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
3685 ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
3686 if (os_snprintf_error(end - pos, ret))
3687 return pos - buf;
3688 pos += ret;
3689 }
3690#endif /* CONFIG_SUITEB */
3691#ifdef CONFIG_SUITEB192
3692 if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
3693 ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
3694 if (os_snprintf_error(end - pos, ret))
3695 return pos - buf;
3696 pos += ret;
3697 }
3698#endif /* CONFIG_SUITEB192 */
3699
6fc6879b
JM
3700 return pos - buf;
3701}
3702
3703
3704static int ctrl_iface_get_capability_proto(int res, char *strict,
3705 struct wpa_driver_capa *capa,
3706 char *buf, size_t buflen)
3707{
ea3b8c1d 3708 int ret;
6fc6879b
JM
3709 char *pos, *end;
3710 size_t len;
3711
3712 pos = buf;
3713 end = pos + buflen;
3714
3715 if (res < 0) {
3716 if (strict)
3717 return 0;
3718 len = os_strlcpy(buf, "RSN WPA", buflen);
3719 if (len >= buflen)
3720 return -1;
3721 return len;
3722 }
3723
3724 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
3725 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
ea3b8c1d
JM
3726 ret = os_snprintf(pos, end - pos, "%sRSN",
3727 pos == buf ? "" : " ");
d85e1fc8 3728 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3729 return pos - buf;
3730 pos += ret;
6fc6879b
JM
3731 }
3732
3733 if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
3734 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
ea3b8c1d
JM
3735 ret = os_snprintf(pos, end - pos, "%sWPA",
3736 pos == buf ? "" : " ");
d85e1fc8 3737 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3738 return pos - buf;
3739 pos += ret;
6fc6879b
JM
3740 }
3741
3742 return pos - buf;
3743}
3744
3745
db5adfe7
JM
3746static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
3747 int res, char *strict,
6fc6879b
JM
3748 struct wpa_driver_capa *capa,
3749 char *buf, size_t buflen)
3750{
ea3b8c1d 3751 int ret;
6fc6879b
JM
3752 char *pos, *end;
3753 size_t len;
3754
3755 pos = buf;
3756 end = pos + buflen;
3757
3758 if (res < 0) {
3759 if (strict)
3760 return 0;
3761 len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
3762 if (len >= buflen)
3763 return -1;
3764 return len;
3765 }
3766
3767 if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
ea3b8c1d
JM
3768 ret = os_snprintf(pos, end - pos, "%sOPEN",
3769 pos == buf ? "" : " ");
d85e1fc8 3770 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3771 return pos - buf;
3772 pos += ret;
6fc6879b
JM
3773 }
3774
3775 if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
3776 ret = os_snprintf(pos, end - pos, "%sSHARED",
ea3b8c1d 3777 pos == buf ? "" : " ");
d85e1fc8 3778 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3779 return pos - buf;
3780 pos += ret;
6fc6879b
JM
3781 }
3782
3783 if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
ea3b8c1d
JM
3784 ret = os_snprintf(pos, end - pos, "%sLEAP",
3785 pos == buf ? "" : " ");
d85e1fc8 3786 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3787 return pos - buf;
3788 pos += ret;
6fc6879b
JM
3789 }
3790
db5adfe7
JM
3791#ifdef CONFIG_SAE
3792 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
3793 ret = os_snprintf(pos, end - pos, "%sSAE",
3794 pos == buf ? "" : " ");
3795 if (os_snprintf_error(end - pos, ret))
3796 return pos - buf;
3797 pos += ret;
3798 }
3799#endif /* CONFIG_SAE */
3800
6fc6879b
JM
3801 return pos - buf;
3802}
3803
3804
65d52fc1
BR
3805static int ctrl_iface_get_capability_modes(int res, char *strict,
3806 struct wpa_driver_capa *capa,
3807 char *buf, size_t buflen)
3808{
ea3b8c1d 3809 int ret;
65d52fc1
BR
3810 char *pos, *end;
3811 size_t len;
3812
3813 pos = buf;
3814 end = pos + buflen;
3815
3816 if (res < 0) {
3817 if (strict)
3818 return 0;
3819 len = os_strlcpy(buf, "IBSS AP", buflen);
3820 if (len >= buflen)
3821 return -1;
3822 return len;
3823 }
3824
3825 if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
ea3b8c1d
JM
3826 ret = os_snprintf(pos, end - pos, "%sIBSS",
3827 pos == buf ? "" : " ");
d85e1fc8 3828 if (os_snprintf_error(end - pos, ret))
65d52fc1
BR
3829 return pos - buf;
3830 pos += ret;
65d52fc1
BR
3831 }
3832
3833 if (capa->flags & WPA_DRIVER_FLAGS_AP) {
ea3b8c1d
JM
3834 ret = os_snprintf(pos, end - pos, "%sAP",
3835 pos == buf ? "" : " ");
d85e1fc8 3836 if (os_snprintf_error(end - pos, ret))
65d52fc1
BR
3837 return pos - buf;
3838 pos += ret;
65d52fc1
BR
3839 }
3840
cf08e9b1
JM
3841#ifdef CONFIG_MESH
3842 if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
3843 ret = os_snprintf(pos, end - pos, "%sMESH",
3844 pos == buf ? "" : " ");
3845 if (os_snprintf_error(end - pos, ret))
3846 return pos - buf;
3847 pos += ret;
3848 }
3849#endif /* CONFIG_MESH */
3850
65d52fc1
BR
3851 return pos - buf;
3852}
3853
3854
35aa088a
DS
3855static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
3856 char *buf, size_t buflen)
3857{
3858 struct hostapd_channel_data *chnl;
3859 int ret, i, j;
3860 char *pos, *end, *hmode;
3861
3862 pos = buf;
3863 end = pos + buflen;
3864
3865 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3866 switch (wpa_s->hw.modes[j].mode) {
3867 case HOSTAPD_MODE_IEEE80211B:
3868 hmode = "B";
3869 break;
3870 case HOSTAPD_MODE_IEEE80211G:
3871 hmode = "G";
3872 break;
3873 case HOSTAPD_MODE_IEEE80211A:
3874 hmode = "A";
3875 break;
7829894c
VK
3876 case HOSTAPD_MODE_IEEE80211AD:
3877 hmode = "AD";
3878 break;
35aa088a
DS
3879 default:
3880 continue;
3881 }
3882 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
d85e1fc8 3883 if (os_snprintf_error(end - pos, ret))
35aa088a
DS
3884 return pos - buf;
3885 pos += ret;
3886 chnl = wpa_s->hw.modes[j].channels;
3887 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3888 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3889 continue;
3890 ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
d85e1fc8 3891 if (os_snprintf_error(end - pos, ret))
35aa088a
DS
3892 return pos - buf;
3893 pos += ret;
3894 }
3895 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 3896 if (os_snprintf_error(end - pos, ret))
35aa088a
DS
3897 return pos - buf;
3898 pos += ret;
3899 }
3900
3901 return pos - buf;
3902}
3903
3904
06060522
BR
3905static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
3906 char *buf, size_t buflen)
3907{
3908 struct hostapd_channel_data *chnl;
3909 int ret, i, j;
3910 char *pos, *end, *hmode;
3911
3912 pos = buf;
3913 end = pos + buflen;
3914
3915 for (j = 0; j < wpa_s->hw.num_modes; j++) {
3916 switch (wpa_s->hw.modes[j].mode) {
3917 case HOSTAPD_MODE_IEEE80211B:
3918 hmode = "B";
3919 break;
3920 case HOSTAPD_MODE_IEEE80211G:
3921 hmode = "G";
3922 break;
3923 case HOSTAPD_MODE_IEEE80211A:
3924 hmode = "A";
3925 break;
3926 case HOSTAPD_MODE_IEEE80211AD:
3927 hmode = "AD";
3928 break;
3929 default:
3930 continue;
3931 }
3932 ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
3933 hmode);
d85e1fc8 3934 if (os_snprintf_error(end - pos, ret))
06060522
BR
3935 return pos - buf;
3936 pos += ret;
3937 chnl = wpa_s->hw.modes[j].channels;
3938 for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
3939 if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
3940 continue;
0547124d 3941 ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
06060522 3942 chnl[i].chan, chnl[i].freq,
0a443580
IP
3943 chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
3944 " (NO_IR)" : "",
0547124d
DS
3945 chnl[i].flag & HOSTAPD_CHAN_RADAR ?
3946 " (DFS)" : "");
3947
d85e1fc8 3948 if (os_snprintf_error(end - pos, ret))
06060522
BR
3949 return pos - buf;
3950 pos += ret;
3951 }
3952 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 3953 if (os_snprintf_error(end - pos, ret))
06060522
BR
3954 return pos - buf;
3955 pos += ret;
3956 }
3957
3958 return pos - buf;
3959}
3960
3961
6fc6879b
JM
3962static int wpa_supplicant_ctrl_iface_get_capability(
3963 struct wpa_supplicant *wpa_s, const char *_field, char *buf,
3964 size_t buflen)
3965{
3966 struct wpa_driver_capa capa;
3967 int res;
3968 char *strict;
3969 char field[30];
3970 size_t len;
3971
3972 /* Determine whether or not strict checking was requested */
3973 len = os_strlcpy(field, _field, sizeof(field));
3974 if (len >= sizeof(field))
3975 return -1;
3976 strict = os_strchr(field, ' ');
3977 if (strict != NULL) {
3978 *strict++ = '\0';
3979 if (os_strcmp(strict, "strict") != 0)
3980 return -1;
3981 }
3982
3983 wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
3984 field, strict ? strict : "");
3985
3986 if (os_strcmp(field, "eap") == 0) {
3987 return eap_get_names(buf, buflen);
3988 }
3989
3990 res = wpa_drv_get_capa(wpa_s, &capa);
3991
3992 if (os_strcmp(field, "pairwise") == 0)
3993 return ctrl_iface_get_capability_pairwise(res, strict, &capa,
3994 buf, buflen);
3995
3996 if (os_strcmp(field, "group") == 0)
3997 return ctrl_iface_get_capability_group(res, strict, &capa,
3998 buf, buflen);
3999
b5f045de
JM
4000 if (os_strcmp(field, "group_mgmt") == 0)
4001 return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
4002 buf, buflen);
4003
6fc6879b
JM
4004 if (os_strcmp(field, "key_mgmt") == 0)
4005 return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
4006 buf, buflen);
4007
4008 if (os_strcmp(field, "proto") == 0)
4009 return ctrl_iface_get_capability_proto(res, strict, &capa,
4010 buf, buflen);
4011
4012 if (os_strcmp(field, "auth_alg") == 0)
db5adfe7
JM
4013 return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
4014 &capa, buf, buflen);
6fc6879b 4015
65d52fc1
BR
4016 if (os_strcmp(field, "modes") == 0)
4017 return ctrl_iface_get_capability_modes(res, strict, &capa,
4018 buf, buflen);
4019
35aa088a
DS
4020 if (os_strcmp(field, "channels") == 0)
4021 return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
4022
06060522
BR
4023 if (os_strcmp(field, "freq") == 0)
4024 return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
4025
6e9375e4
DS
4026#ifdef CONFIG_TDLS
4027 if (os_strcmp(field, "tdls") == 0)
4028 return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
4029#endif /* CONFIG_TDLS */
4030
02a8d45a
JM
4031#ifdef CONFIG_ERP
4032 if (os_strcmp(field, "erp") == 0) {
4033 res = os_snprintf(buf, buflen, "ERP");
d85e1fc8 4034 if (os_snprintf_error(buflen, res))
02a8d45a
JM
4035 return -1;
4036 return res;
4037 }
4038#endif /* CONFIG_EPR */
4039
1e4f7bf5
JM
4040#ifdef CONFIG_FIPS
4041 if (os_strcmp(field, "fips") == 0) {
4042 res = os_snprintf(buf, buflen, "FIPS");
4043 if (os_snprintf_error(buflen, res))
4044 return -1;
4045 return res;
4046 }
4047#endif /* CONFIG_FIPS */
4048
7d2f6743
JM
4049#ifdef CONFIG_ACS
4050 if (os_strcmp(field, "acs") == 0) {
4051 res = os_snprintf(buf, buflen, "ACS");
4052 if (os_snprintf_error(buflen, res))
4053 return -1;
4054 return res;
4055 }
4056#endif /* CONFIG_ACS */
4057
379e2b4d 4058#ifdef CONFIG_FILS
061dac1d
JM
4059 if (os_strcmp(field, "fils") == 0 &&
4060 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_FILS)) {
379e2b4d
JM
4061 res = os_snprintf(buf, buflen, "FILS");
4062 if (os_snprintf_error(buflen, res))
4063 return -1;
4064 return res;
4065 }
4066#endif /* CONFIG_FILS */
4067
6fc6879b
JM
4068 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
4069 field);
4070
4071 return -1;
4072}
4073
4074
afc064fe
JM
4075#ifdef CONFIG_INTERWORKING
4076static char * anqp_add_hex(char *pos, char *end, const char *title,
4077 struct wpabuf *data)
4078{
4079 char *start = pos;
4080 size_t i;
4081 int ret;
4082 const u8 *d;
4083
4084 if (data == NULL)
4085 return start;
4086
4087 ret = os_snprintf(pos, end - pos, "%s=", title);
d85e1fc8 4088 if (os_snprintf_error(end - pos, ret))
afc064fe
JM
4089 return start;
4090 pos += ret;
4091
4092 d = wpabuf_head_u8(data);
4093 for (i = 0; i < wpabuf_len(data); i++) {
4094 ret = os_snprintf(pos, end - pos, "%02x", *d++);
d85e1fc8 4095 if (os_snprintf_error(end - pos, ret))
afc064fe
JM
4096 return start;
4097 pos += ret;
4098 }
4099
4100 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 4101 if (os_snprintf_error(end - pos, ret))
afc064fe
JM
4102 return start;
4103 pos += ret;
4104
4105 return pos;
4106}
4107#endif /* CONFIG_INTERWORKING */
4108
4109
b54f4339
JM
4110#ifdef CONFIG_FILS
4111static int print_fils_indication(struct wpa_bss *bss, char *pos, char *end)
4112{
4113 char *start = pos;
4114 const u8 *ie, *ie_end;
4115 u16 info, realms;
4116 int ret;
4117
4118 ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
4119 if (!ie)
4120 return 0;
4121 ie_end = ie + 2 + ie[1];
4122 ie += 2;
4123 if (ie_end - ie < 2)
4124 return -1;
4125
4126 info = WPA_GET_LE16(ie);
4127 ie += 2;
4128 ret = os_snprintf(pos, end - pos, "fils_info=%04x\n", info);
4129 if (os_snprintf_error(end - pos, ret))
4130 return 0;
4131 pos += ret;
4132
4133 if (info & BIT(7)) {
4134 /* Cache Identifier Included */
4135 if (ie_end - ie < 2)
4136 return -1;
4137 ret = os_snprintf(pos, end - pos, "fils_cache_id=%02x%02x\n",
4138 ie[0], ie[1]);
4139 if (os_snprintf_error(end - pos, ret))
4140 return 0;
4141 pos += ret;
4142 ie += 2;
4143 }
4144
4145 if (info & BIT(8)) {
4146 /* HESSID Included */
4147 if (ie_end - ie < ETH_ALEN)
4148 return -1;
4149 ret = os_snprintf(pos, end - pos, "fils_hessid=" MACSTR "\n",
4150 MAC2STR(ie));
4151 if (os_snprintf_error(end - pos, ret))
4152 return 0;
4153 pos += ret;
4154 ie += ETH_ALEN;
4155 }
4156
4157 realms = (info & (BIT(3) | BIT(4) | BIT(5))) >> 3;
4158 if (realms) {
4159 if (ie_end - ie < realms * 2)
4160 return -1;
4161 ret = os_snprintf(pos, end - pos, "fils_realms=");
4162 if (os_snprintf_error(end - pos, ret))
4163 return 0;
4164 pos += ret;
4165
4166 ret = wpa_snprintf_hex(pos, end - pos, ie, realms * 2);
4167 if (ret <= 0)
4168 return 0;
4169 pos += ret;
4170 ie += realms * 2;
4171 ret = os_snprintf(pos, end - pos, "\n");
4172 if (os_snprintf_error(end - pos, ret))
4173 return 0;
4174 pos += ret;
4175 }
4176
4177 return pos - start;
4178}
4179#endif /* CONFIG_FILS */
4180
4181
61ce9085 4182static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5f97dd1c 4183 unsigned long mask, char *buf, size_t buflen)
6fc6879b 4184{
6fc6879b 4185 size_t i;
6fc6879b
JM
4186 int ret;
4187 char *pos, *end;
0f8385e6 4188 const u8 *ie, *ie2, *osen_ie;
6fc6879b 4189
6fc6879b
JM
4190 pos = buf;
4191 end = buf + buflen;
6fc6879b 4192
5f97dd1c
DS
4193 if (mask & WPA_BSS_MASK_ID) {
4194 ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
d85e1fc8 4195 if (os_snprintf_error(end - pos, ret))
5f97dd1c 4196 return 0;
6fc6879b
JM
4197 pos += ret;
4198 }
4199
5f97dd1c
DS
4200 if (mask & WPA_BSS_MASK_BSSID) {
4201 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
4202 MAC2STR(bss->bssid));
d85e1fc8 4203 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4204 return 0;
4205 pos += ret;
4206 }
6fc6879b 4207
5f97dd1c
DS
4208 if (mask & WPA_BSS_MASK_FREQ) {
4209 ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
d85e1fc8 4210 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4211 return 0;
4212 pos += ret;
4213 }
6fc6879b 4214
5f97dd1c
DS
4215 if (mask & WPA_BSS_MASK_BEACON_INT) {
4216 ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
4217 bss->beacon_int);
d85e1fc8 4218 if (os_snprintf_error(end - pos, ret))
5f97dd1c 4219 return 0;
6fc6879b
JM
4220 pos += ret;
4221 }
5f97dd1c
DS
4222
4223 if (mask & WPA_BSS_MASK_CAPABILITIES) {
4224 ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
4225 bss->caps);
d85e1fc8 4226 if (os_snprintf_error(end - pos, ret))
5f97dd1c 4227 return 0;
6fc6879b
JM
4228 pos += ret;
4229 }
5f97dd1c
DS
4230
4231 if (mask & WPA_BSS_MASK_QUAL) {
4232 ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
d85e1fc8 4233 if (os_snprintf_error(end - pos, ret))
5f97dd1c 4234 return 0;
bd1af96a
JM
4235 pos += ret;
4236 }
5f97dd1c
DS
4237
4238 if (mask & WPA_BSS_MASK_NOISE) {
4239 ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
d85e1fc8 4240 if (os_snprintf_error(end - pos, ret))
5f97dd1c 4241 return 0;
cc81110d
JM
4242 pos += ret;
4243 }
6fc6879b 4244
5f97dd1c
DS
4245 if (mask & WPA_BSS_MASK_LEVEL) {
4246 ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
d85e1fc8 4247 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4248 return 0;
4249 pos += ret;
4250 }
6fc6879b 4251
5f97dd1c
DS
4252 if (mask & WPA_BSS_MASK_TSF) {
4253 ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
4254 (unsigned long long) bss->tsf);
d85e1fc8 4255 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4256 return 0;
4257 pos += ret;
4258 }
4259
4260 if (mask & WPA_BSS_MASK_AGE) {
acb69cec 4261 struct os_reltime now;
5f97dd1c 4262
acb69cec 4263 os_get_reltime(&now);
5f97dd1c
DS
4264 ret = os_snprintf(pos, end - pos, "age=%d\n",
4265 (int) (now.sec - bss->last_update.sec));
d85e1fc8 4266 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4267 return 0;
4268 pos += ret;
4269 }
4270
4271 if (mask & WPA_BSS_MASK_IE) {
4272 ret = os_snprintf(pos, end - pos, "ie=");
d85e1fc8 4273 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4274 return 0;
4275 pos += ret;
4276
4277 ie = (const u8 *) (bss + 1);
4278 for (i = 0; i < bss->ie_len; i++) {
4279 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
d85e1fc8 4280 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4281 return 0;
4282 pos += ret;
4283 }
4284
4285 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 4286 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4287 return 0;
4288 pos += ret;
4289 }
4290
4291 if (mask & WPA_BSS_MASK_FLAGS) {
4292 ret = os_snprintf(pos, end - pos, "flags=");
d85e1fc8 4293 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4294 return 0;
4295 pos += ret;
4296
4297 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
4298 if (ie)
4299 pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
4300 2 + ie[1]);
4301 ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
4302 if (ie2)
4303 pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
4304 2 + ie2[1]);
0f8385e6
BG
4305 osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
4306 if (osen_ie)
4307 pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
4308 osen_ie, 2 + osen_ie[1]);
5f97dd1c 4309 pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
0f8385e6
BG
4310 if (!ie && !ie2 && !osen_ie &&
4311 (bss->caps & IEEE80211_CAP_PRIVACY)) {
5f97dd1c 4312 ret = os_snprintf(pos, end - pos, "[WEP]");
d85e1fc8 4313 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4314 return 0;
4315 pos += ret;
4316 }
e403ba85
BS
4317 if (bss_is_dmg(bss)) {
4318 const char *s;
4319 ret = os_snprintf(pos, end - pos, "[DMG]");
d85e1fc8 4320 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4321 return 0;
4322 pos += ret;
e403ba85
BS
4323 switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
4324 case IEEE80211_CAP_DMG_IBSS:
4325 s = "[IBSS]";
4326 break;
4327 case IEEE80211_CAP_DMG_AP:
4328 s = "[ESS]";
4329 break;
4330 case IEEE80211_CAP_DMG_PBSS:
4331 s = "[PBSS]";
4332 break;
4333 default:
4334 s = "";
4335 break;
4336 }
4337 ret = os_snprintf(pos, end - pos, "%s", s);
d85e1fc8 4338 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4339 return 0;
4340 pos += ret;
e403ba85
BS
4341 } else {
4342 if (bss->caps & IEEE80211_CAP_IBSS) {
4343 ret = os_snprintf(pos, end - pos, "[IBSS]");
d85e1fc8 4344 if (os_snprintf_error(end - pos, ret))
e403ba85
BS
4345 return 0;
4346 pos += ret;
4347 }
4348 if (bss->caps & IEEE80211_CAP_ESS) {
4349 ret = os_snprintf(pos, end - pos, "[ESS]");
d85e1fc8 4350 if (os_snprintf_error(end - pos, ret))
e403ba85
BS
4351 return 0;
4352 pos += ret;
4353 }
5f97dd1c 4354 }
bb50ae43
JM
4355 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
4356 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
5f97dd1c 4357 ret = os_snprintf(pos, end - pos, "[P2P]");
d85e1fc8 4358 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4359 return 0;
4360 pos += ret;
4361 }
64855b96
JM
4362#ifdef CONFIG_HS20
4363 if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
4364 ret = os_snprintf(pos, end - pos, "[HS20]");
d85e1fc8 4365 if (os_snprintf_error(end - pos, ret))
ff486913 4366 return 0;
64855b96
JM
4367 pos += ret;
4368 }
4369#endif /* CONFIG_HS20 */
7147a834
JM
4370#ifdef CONFIG_FILS
4371 if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
4372 ret = os_snprintf(pos, end - pos, "[FILS]");
4373 if (os_snprintf_error(end - pos, ret))
4374 return 0;
4375 pos += ret;
4376 }
4377#endif /* CONFIG_FILS */
5f97dd1c
DS
4378
4379 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 4380 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4381 return 0;
4382 pos += ret;
4383 }
4384
4385 if (mask & WPA_BSS_MASK_SSID) {
4386 ret = os_snprintf(pos, end - pos, "ssid=%s\n",
4387 wpa_ssid_txt(bss->ssid, bss->ssid_len));
d85e1fc8 4388 if (os_snprintf_error(end - pos, ret))
5f97dd1c
DS
4389 return 0;
4390 pos += ret;
4391 }
6fc6879b 4392
611ed491 4393#ifdef CONFIG_WPS
5f97dd1c
DS
4394 if (mask & WPA_BSS_MASK_WPS_SCAN) {
4395 ie = (const u8 *) (bss + 1);
4396 ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
fc078be2 4397 if (ret >= end - pos)
5f97dd1c 4398 return 0;
fc078be2
JM
4399 if (ret > 0)
4400 pos += ret;
5f97dd1c 4401 }
611ed491
JM
4402#endif /* CONFIG_WPS */
4403
0c6b310e 4404#ifdef CONFIG_P2P
5f97dd1c
DS
4405 if (mask & WPA_BSS_MASK_P2P_SCAN) {
4406 ie = (const u8 *) (bss + 1);
4407 ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
3b208346 4408 if (ret >= end - pos)
5f97dd1c 4409 return 0;
3b208346
JA
4410 if (ret > 0)
4411 pos += ret;
5f97dd1c 4412 }
0c6b310e
JM
4413#endif /* CONFIG_P2P */
4414
337c781f
JM
4415#ifdef CONFIG_WIFI_DISPLAY
4416 if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
4417 struct wpabuf *wfd;
4418 ie = (const u8 *) (bss + 1);
4419 wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
4420 WFD_IE_VENDOR_TYPE);
4421 if (wfd) {
4422 ret = os_snprintf(pos, end - pos, "wfd_subelems=");
d85e1fc8 4423 if (os_snprintf_error(end - pos, ret)) {
5e6aa04b 4424 wpabuf_free(wfd);
ff486913 4425 return 0;
5e6aa04b 4426 }
337c781f
JM
4427 pos += ret;
4428
4429 pos += wpa_snprintf_hex(pos, end - pos,
4430 wpabuf_head(wfd),
4431 wpabuf_len(wfd));
4432 wpabuf_free(wfd);
4433
4434 ret = os_snprintf(pos, end - pos, "\n");
d85e1fc8 4435 if (os_snprintf_error(end - pos, ret))
ff486913 4436 return 0;
337c781f
JM
4437 pos += ret;
4438 }
4439 }
4440#endif /* CONFIG_WIFI_DISPLAY */
4441
afc064fe 4442#ifdef CONFIG_INTERWORKING
476aed35
JM
4443 if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
4444 struct wpa_bss_anqp *anqp = bss->anqp;
8c4a1026
JM
4445 struct wpa_bss_anqp_elem *elem;
4446
5ce6ac11
AN
4447 pos = anqp_add_hex(pos, end, "anqp_capability_list",
4448 anqp->capability_list);
5f97dd1c 4449 pos = anqp_add_hex(pos, end, "anqp_venue_name",
476aed35 4450 anqp->venue_name);
5f97dd1c 4451 pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
476aed35 4452 anqp->network_auth_type);
5f97dd1c 4453 pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
476aed35 4454 anqp->roaming_consortium);
5f97dd1c 4455 pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
476aed35 4456 anqp->ip_addr_type_availability);
5f97dd1c 4457 pos = anqp_add_hex(pos, end, "anqp_nai_realm",
476aed35
JM
4458 anqp->nai_realm);
4459 pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
5f97dd1c 4460 pos = anqp_add_hex(pos, end, "anqp_domain_name",
476aed35 4461 anqp->domain_name);
9cad6186
JM
4462 pos = anqp_add_hex(pos, end, "anqp_fils_realm_info",
4463 anqp->fils_realm_info);
25471fe3 4464#ifdef CONFIG_HS20
185ada47
AN
4465 pos = anqp_add_hex(pos, end, "hs20_capability_list",
4466 anqp->hs20_capability_list);
25471fe3 4467 pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
476aed35 4468 anqp->hs20_operator_friendly_name);
25471fe3 4469 pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
476aed35 4470 anqp->hs20_wan_metrics);
25471fe3 4471 pos = anqp_add_hex(pos, end, "hs20_connection_capability",
476aed35 4472 anqp->hs20_connection_capability);
1d2215fc
JM
4473 pos = anqp_add_hex(pos, end, "hs20_operating_class",
4474 anqp->hs20_operating_class);
4475 pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
4476 anqp->hs20_osu_providers_list);
25471fe3 4477#endif /* CONFIG_HS20 */
8c4a1026
JM
4478
4479 dl_list_for_each(elem, &anqp->anqp_elems,
4480 struct wpa_bss_anqp_elem, list) {
4481 char title[20];
4482
4483 os_snprintf(title, sizeof(title), "anqp[%u]",
4484 elem->infoid);
4485 pos = anqp_add_hex(pos, end, title, elem->payload);
4486 }
5f97dd1c 4487 }
afc064fe
JM
4488#endif /* CONFIG_INTERWORKING */
4489
79070906
MH
4490#ifdef CONFIG_MESH
4491 if (mask & WPA_BSS_MASK_MESH_SCAN) {
4492 ie = (const u8 *) (bss + 1);
4493 ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
3b208346 4494 if (ret >= end - pos)
79070906 4495 return 0;
3b208346
JA
4496 if (ret > 0)
4497 pos += ret;
79070906
MH
4498 }
4499#endif /* CONFIG_MESH */
4500
1d747e2a
JM
4501 if (mask & WPA_BSS_MASK_SNR) {
4502 ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
4503 if (os_snprintf_error(end - pos, ret))
4504 return 0;
4505 pos += ret;
4506 }
4507
4508 if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
4509 ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
4510 bss->est_throughput);
4511 if (os_snprintf_error(end - pos, ret))
4512 return 0;
4513 pos += ret;
4514 }
4515
3794af2d
AN
4516#ifdef CONFIG_FST
4517 if (mask & WPA_BSS_MASK_FST) {
4518 ret = fst_ctrl_iface_mb_info(bss->bssid, pos, end - pos);
4519 if (ret < 0 || ret >= end - pos)
4520 return 0;
4521 pos += ret;
4522 }
4523#endif /* CONFIG_FST */
4524
71ac9345
JM
4525 if (mask & WPA_BSS_MASK_UPDATE_IDX) {
4526 ret = os_snprintf(pos, end - pos, "update_idx=%u\n",
4527 bss->last_update_idx);
4528 if (os_snprintf_error(end - pos, ret))
4529 return 0;
4530 pos += ret;
4531 }
4532
19810d29
JM
4533 if ((mask & WPA_BSS_MASK_BEACON_IE) && bss->beacon_ie_len) {
4534 ret = os_snprintf(pos, end - pos, "beacon_ie=");
4535 if (os_snprintf_error(end - pos, ret))
4536 return 0;
4537 pos += ret;
4538
4539 ie = (const u8 *) (bss + 1);
4540 ie += bss->ie_len;
4541 for (i = 0; i < bss->beacon_ie_len; i++) {
4542 ret = os_snprintf(pos, end - pos, "%02x", *ie++);
4543 if (os_snprintf_error(end - pos, ret))
4544 return 0;
4545 pos += ret;
4546 }
4547
4548 ret = os_snprintf(pos, end - pos, "\n");
4549 if (os_snprintf_error(end - pos, ret))
4550 return 0;
4551 pos += ret;
4552 }
4553
b54f4339
JM
4554#ifdef CONFIG_FILS
4555 if (mask & WPA_BSS_MASK_FILS_INDICATION) {
4556 ret = print_fils_indication(bss, pos, end);
4557 if (ret < 0)
4558 return 0;
4559 pos += ret;
4560 }
4561#endif /* CONFIG_FILS */
4562
c6673429
DS
4563 if (mask & WPA_BSS_MASK_DELIM) {
4564 ret = os_snprintf(pos, end - pos, "====\n");
d85e1fc8 4565 if (os_snprintf_error(end - pos, ret))
c6673429
DS
4566 return 0;
4567 pos += ret;
4568 }
4569
6fc6879b
JM
4570 return pos - buf;
4571}
4572
4573
61ce9085
DS
4574static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
4575 const char *cmd, char *buf,
4576 size_t buflen)
4577{
4578 u8 bssid[ETH_ALEN];
4579 size_t i;
4580 struct wpa_bss *bss;
eff1a95b
DS
4581 struct wpa_bss *bsslast = NULL;
4582 struct dl_list *next;
4583 int ret = 0;
4584 int len;
1d399771 4585 char *ctmp, *end = buf + buflen;
5f97dd1c 4586 unsigned long mask = WPA_BSS_MASK_ALL;
61ce9085 4587
eff1a95b
DS
4588 if (os_strncmp(cmd, "RANGE=", 6) == 0) {
4589 if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
4590 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
4591 list_id);
4592 bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
4593 list_id);
4594 } else { /* N1-N2 */
4595 unsigned int id1, id2;
4596
4597 if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
4598 wpa_printf(MSG_INFO, "Wrong BSS range "
4599 "format");
4600 return 0;
4601 }
4602
9f42d49c
AS
4603 if (*(cmd + 6) == '-')
4604 id1 = 0;
4605 else
4606 id1 = atoi(cmd + 6);
4607 ctmp++;
4608 if (*ctmp >= '0' && *ctmp <= '9')
4609 id2 = atoi(ctmp);
4610 else
4611 id2 = (unsigned int) -1;
4612 bss = wpa_bss_get_id_range(wpa_s, id1, id2);
4613 if (id2 == (unsigned int) -1)
eff1a95b
DS
4614 bsslast = dl_list_last(&wpa_s->bss_id,
4615 struct wpa_bss,
4616 list_id);
4617 else {
4618 bsslast = wpa_bss_get_id(wpa_s, id2);
4619 if (bsslast == NULL && bss && id2 > id1) {
4620 struct wpa_bss *tmp = bss;
4621 for (;;) {
4622 next = tmp->list_id.next;
4623 if (next == &wpa_s->bss_id)
4624 break;
4625 tmp = dl_list_entry(
4626 next, struct wpa_bss,
4627 list_id);
4628 if (tmp->id > id2)
4629 break;
4630 bsslast = tmp;
4631 }
4632 }
4633 }
4634 }
f330b4b4 4635 } else if (os_strncmp(cmd, "FIRST", 5) == 0)
51a0c3d4 4636 bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
cc03d0fe
AS
4637 else if (os_strncmp(cmd, "LAST", 4) == 0)
4638 bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
61ce9085
DS
4639 else if (os_strncmp(cmd, "ID-", 3) == 0) {
4640 i = atoi(cmd + 3);
4641 bss = wpa_bss_get_id(wpa_s, i);
4642 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
4643 i = atoi(cmd + 5);
4644 bss = wpa_bss_get_id(wpa_s, i);
4645 if (bss) {
eff1a95b 4646 next = bss->list_id.next;
61ce9085
DS
4647 if (next == &wpa_s->bss_id)
4648 bss = NULL;
4649 else
4650 bss = dl_list_entry(next, struct wpa_bss,
4651 list_id);
4652 }
9187b13a
JC
4653 } else if (os_strncmp(cmd, "CURRENT", 7) == 0) {
4654 bss = wpa_s->current_bss;
61ce9085
DS
4655#ifdef CONFIG_P2P
4656 } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
4657 if (hwaddr_aton(cmd + 13, bssid) == 0)
4658 bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
4659 else
4660 bss = NULL;
4661#endif /* CONFIG_P2P */
4662 } else if (hwaddr_aton(cmd, bssid) == 0)
4663 bss = wpa_bss_get_bssid(wpa_s, bssid);
4664 else {
4665 struct wpa_bss *tmp;
4666 i = atoi(cmd);
4667 bss = NULL;
4668 dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
4669 {
4670 if (i-- == 0) {
4671 bss = tmp;
4672 break;
4673 }
4674 }
4675 }
4676
5f97dd1c
DS
4677 if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
4678 mask = strtoul(ctmp + 5, NULL, 0x10);
4679 if (mask == 0)
4680 mask = WPA_BSS_MASK_ALL;
4681 }
4682
61ce9085
DS
4683 if (bss == NULL)
4684 return 0;
4685
eff1a95b
DS
4686 if (bsslast == NULL)
4687 bsslast = bss;
4688 do {
4689 len = print_bss_info(wpa_s, bss, mask, buf, buflen);
4690 ret += len;
4691 buf += len;
4692 buflen -= len;
cfd42c94
DS
4693 if (bss == bsslast) {
4694 if ((mask & WPA_BSS_MASK_DELIM) && len &&
4695 (bss == dl_list_last(&wpa_s->bss_id,
1d399771
JM
4696 struct wpa_bss, list_id))) {
4697 int res;
4698
4699 res = os_snprintf(buf - 5, end - buf + 5,
4700 "####\n");
4701 if (os_snprintf_error(end - buf + 5, res)) {
4702 wpa_printf(MSG_DEBUG,
4703 "Could not add end delim");
4704 }
4705 }
eff1a95b 4706 break;
cfd42c94 4707 }
eff1a95b
DS
4708 next = bss->list_id.next;
4709 if (next == &wpa_s->bss_id)
4710 break;
4711 bss = dl_list_entry(next, struct wpa_bss, list_id);
4712 } while (bss && len);
4713
4714 return ret;
61ce9085
DS
4715}
4716
4717
6fc6879b
JM
4718static int wpa_supplicant_ctrl_iface_ap_scan(
4719 struct wpa_supplicant *wpa_s, char *cmd)
4720{
4721 int ap_scan = atoi(cmd);
86b89452 4722 return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
6fc6879b
JM
4723}
4724
4725
67b9bd08
DS
4726static int wpa_supplicant_ctrl_iface_scan_interval(
4727 struct wpa_supplicant *wpa_s, char *cmd)
4728{
4729 int scan_int = atoi(cmd);
c6e86b63 4730 return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
67b9bd08
DS
4731}
4732
4733
78633c37
SL
4734static int wpa_supplicant_ctrl_iface_bss_expire_age(
4735 struct wpa_supplicant *wpa_s, char *cmd)
4736{
4737 int expire_age = atoi(cmd);
4738 return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
4739}
4740
4741
4742static int wpa_supplicant_ctrl_iface_bss_expire_count(
4743 struct wpa_supplicant *wpa_s, char *cmd)
4744{
4745 int expire_count = atoi(cmd);
4746 return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
4747}
4748
4749
a1144000 4750static void wpa_supplicant_ctrl_iface_bss_flush(
39ee845f
DS
4751 struct wpa_supplicant *wpa_s, char *cmd)
4752{
4753 int flush_age = atoi(cmd);
4754
4755 if (flush_age == 0)
4756 wpa_bss_flush(wpa_s);
4757 else
4758 wpa_bss_flush_by_age(wpa_s, flush_age);
39ee845f
DS
4759}
4760
4761
9ff4de6d 4762#ifdef CONFIG_TESTING_OPTIONS
32d5295f
JM
4763static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
4764{
32d5295f
JM
4765 wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
4766 /* MLME-DELETEKEYS.request */
0382097e
JM
4767 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
4768 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
4769 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
4770 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
32d5295f 4771#ifdef CONFIG_IEEE80211W
0382097e
JM
4772 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
4773 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
32d5295f
JM
4774#endif /* CONFIG_IEEE80211W */
4775
4776 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
4777 0);
4778 /* MLME-SETPROTECTION.request(None) */
4779 wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
4780 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
4781 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
4782 wpa_sm_drop_sa(wpa_s->wpa);
4783}
9ff4de6d 4784#endif /* CONFIG_TESTING_OPTIONS */
32d5295f
JM
4785
4786
86d4f806
JM
4787static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
4788 char *addr)
4789{
90b8fc8f
JM
4790#ifdef CONFIG_NO_SCAN_PROCESSING
4791 return -1;
4792#else /* CONFIG_NO_SCAN_PROCESSING */
86d4f806
JM
4793 u8 bssid[ETH_ALEN];
4794 struct wpa_bss *bss;
4795 struct wpa_ssid *ssid = wpa_s->current_ssid;
4796
4797 if (hwaddr_aton(addr, bssid)) {
4798 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
4799 "address '%s'", addr);
4800 return -1;
4801 }
4802
4803 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
4804
2f9b66d3
JM
4805 if (!ssid) {
4806 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
4807 "configuration known for the target AP");
4808 return -1;
4809 }
4810
4811 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
86d4f806
JM
4812 if (!bss) {
4813 wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
4814 "from BSS table");
4815 return -1;
4816 }
4817
4818 /*
4819 * TODO: Find best network configuration block from configuration to
4820 * allow roaming to other networks
4821 */
4822
86d4f806
JM
4823 wpa_s->reassociate = 1;
4824 wpa_supplicant_connect(wpa_s, bss, ssid);
4825
4826 return 0;
90b8fc8f 4827#endif /* CONFIG_NO_SCAN_PROCESSING */
86d4f806
JM
4828}
4829
4830
b563b388
JM
4831#ifdef CONFIG_P2P
4832static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
4833{
4834 unsigned int timeout = atoi(cmd);
4835 enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
6d92fa6e 4836 u8 dev_id[ETH_ALEN], *_dev_id = NULL;
2b384109 4837 u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
6d92fa6e 4838 char *pos;
05a77b3b 4839 unsigned int search_delay;
9542f21f 4840 const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
51775096 4841 u8 seek_count = 0;
fa9f381f 4842 int freq = 0;
b563b388 4843
e9eb648e
JM
4844 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
4845 wpa_dbg(wpa_s, MSG_INFO,
4846 "Reject P2P_FIND since interface is disabled");
4847 return -1;
4848 }
b563b388
JM
4849 if (os_strstr(cmd, "type=social"))
4850 type = P2P_FIND_ONLY_SOCIAL;
4851 else if (os_strstr(cmd, "type=progressive"))
4852 type = P2P_FIND_PROGRESSIVE;
4853
6d92fa6e
JM
4854 pos = os_strstr(cmd, "dev_id=");
4855 if (pos) {
4856 pos += 7;
4857 if (hwaddr_aton(pos, dev_id))
4858 return -1;
4859 _dev_id = dev_id;
4860 }
4861
2b384109
JM
4862 pos = os_strstr(cmd, "dev_type=");
4863 if (pos) {
4864 pos += 9;
4865 if (wps_dev_type_str2bin(pos, dev_type) < 0)
4866 return -1;
4867 _dev_type = dev_type;
4868 }
4869
37448ede
JM
4870 pos = os_strstr(cmd, "delay=");
4871 if (pos) {
4872 pos += 6;
4873 search_delay = atoi(pos);
05a77b3b
JM
4874 } else
4875 search_delay = wpas_p2p_search_delay(wpa_s);
37448ede 4876
a9ea609c
SM
4877 pos = os_strstr(cmd, "freq=");
4878 if (pos) {
4879 pos += 5;
4880 freq = atoi(pos);
4881 if (freq <= 0)
4882 return -1;
4883 }
4884
51775096
BG
4885 /* Must be searched for last, because it adds nul termination */
4886 pos = os_strstr(cmd, " seek=");
129b6216
JM
4887 if (pos)
4888 pos += 6;
51775096
BG
4889 while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
4890 char *term;
4891
129b6216 4892 _seek[seek_count++] = pos;
9542f21f 4893 seek = _seek;
129b6216
JM
4894 term = os_strchr(pos, ' ');
4895 if (!term)
4896 break;
4897 *term = '\0';
4898 pos = os_strstr(term + 1, "seek=");
4899 if (pos)
4900 pos += 5;
51775096 4901 }
9542f21f
JM
4902 if (seek_count > P2P_MAX_QUERY_HASH) {
4903 seek[0] = NULL;
4904 seek_count = 1;
4905 }
51775096 4906
2b384109 4907 return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
fa9f381f 4908 _dev_id, search_delay, seek_count, seek, freq);
b563b388
JM
4909}
4910
4911
e2b7fbf2
MS
4912static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
4913{
4914 const char *last = NULL;
4915 const char *token;
4916 long int token_len;
4917 unsigned int i;
4918
4919 /* Expected predefined CPT names delimited by ':' */
4920 for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
4921 if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
4922 wpa_printf(MSG_ERROR,
4923 "P2PS: CPT name list is too long, expected up to %d names",
4924 P2PS_FEATURE_CAPAB_CPT_MAX);
4925 cpt[0] = 0;
4926 return -1;
4927 }
4928
4929 token_len = last - token;
4930
4931 if (token_len == 3 &&
4932 os_memcmp(token, "UDP", token_len) == 0) {
4933 cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
4934 } else if (token_len == 3 &&
4935 os_memcmp(token, "MAC", token_len) == 0) {
4936 cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
4937 } else {
4938 wpa_printf(MSG_ERROR,
4939 "P2PS: Unsupported CPT name '%s'", token);
4940 cpt[0] = 0;
4941 return -1;
4942 }
4943
640b0b93 4944 if (isblank((unsigned char) *last)) {
e2b7fbf2
MS
4945 i++;
4946 break;
4947 }
4948 }
4949 cpt[i] = 0;
4950 return 0;
4951}
4952
4953
f309c18e
KV
4954static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
4955{
4956 struct p2ps_provision *p2ps_prov;
4957 char *pos;
4958 size_t info_len = 0;
4959 char *info = NULL;
4960 u8 role = P2PS_SETUP_NONE;
4961 long long unsigned val;
0670de74 4962 int i;
f309c18e
KV
4963
4964 pos = os_strstr(cmd, "info=");
4965 if (pos) {
4966 pos += 5;
4967 info_len = os_strlen(pos);
4968
4969 if (info_len) {
4970 info = os_malloc(info_len + 1);
4971 if (info) {
4972 info_len = utf8_unescape(pos, info_len,
4973 info, info_len + 1);
4974 } else
4975 info_len = 0;
4976 }
4977 }
4978
4979 p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
4980 if (p2ps_prov == NULL) {
4981 os_free(info);
4982 return NULL;
4983 }
4984
4985 if (info) {
4986 os_memcpy(p2ps_prov->info, info, info_len);
4987 p2ps_prov->info[info_len] = '\0';
4988 os_free(info);
4989 }
4990
4991 pos = os_strstr(cmd, "status=");
4992 if (pos)
4993 p2ps_prov->status = atoi(pos + 7);
4994 else
4995 p2ps_prov->status = -1;
4996
4997 pos = os_strstr(cmd, "adv_id=");
4998 if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
4999 goto invalid_args;
5000 p2ps_prov->adv_id = val;
5001
5002 pos = os_strstr(cmd, "method=");
5003 if (pos)
5004 p2ps_prov->method = strtol(pos + 7, NULL, 16);
5005 else
5006 p2ps_prov->method = 0;
5007
5008 pos = os_strstr(cmd, "session=");
5009 if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
5010 goto invalid_args;
5011 p2ps_prov->session_id = val;
5012
5013 pos = os_strstr(cmd, "adv_mac=");
5014 if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
5015 goto invalid_args;
5016
5017 pos = os_strstr(cmd, "session_mac=");
5018 if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
5019 goto invalid_args;
5020
0670de74
MS
5021 pos = os_strstr(cmd, "cpt=");
5022 if (pos) {
5023 if (p2ps_ctrl_parse_cpt_priority(pos + 4,
5024 p2ps_prov->cpt_priority))
5025 goto invalid_args;
5026 } else {
5027 p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
5028 }
5029
5030 for (i = 0; p2ps_prov->cpt_priority[i]; i++)
5031 p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
5032
f309c18e
KV
5033 /* force conncap with tstCap (no sanity checks) */
5034 pos = os_strstr(cmd, "tstCap=");
5035 if (pos) {
5036 role = strtol(pos + 7, NULL, 16);
5037 } else {
5038 pos = os_strstr(cmd, "role=");
5039 if (pos) {
5040 role = strtol(pos + 5, NULL, 16);
5041 if (role != P2PS_SETUP_CLIENT &&
5042 role != P2PS_SETUP_GROUP_OWNER)
5043 role = P2PS_SETUP_NONE;
5044 }
5045 }
5046 p2ps_prov->role = role;
5047
5048 return p2ps_prov;
5049
5050invalid_args:
5051 os_free(p2ps_prov);
5052 return NULL;
5053}
5054
5055
5056static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
5057{
5058 u8 addr[ETH_ALEN];
5059 struct p2ps_provision *p2ps_prov;
5060 char *pos;
5061
5062 /* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
5063
5064 wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
5065
5066 if (hwaddr_aton(cmd, addr))
5067 return -1;
5068
5069 pos = cmd + 17;
5070 if (*pos != ' ')
5071 return -1;
5072
5073 p2ps_prov = p2p_parse_asp_provision_cmd(pos);
5074 if (!p2ps_prov)
5075 return -1;
5076
5077 if (p2ps_prov->status < 0) {
5078 os_free(p2ps_prov);
5079 return -1;
5080 }
5081
5082 return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
5083 p2ps_prov);
5084}
5085
5086
5087static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
5088{
5089 u8 addr[ETH_ALEN];
5090 struct p2ps_provision *p2ps_prov;
5091 char *pos;
5092
5093 /* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
5094 * session=<ses_id> mac=<ses_mac> [info=<infodata>]
5095 */
5096
5097 wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
5098 if (hwaddr_aton(cmd, addr))
5099 return -1;
5100
5101 pos = cmd + 17;
5102 if (*pos != ' ')
5103 return -1;
5104
5105 p2ps_prov = p2p_parse_asp_provision_cmd(pos);
5106 if (!p2ps_prov)
5107 return -1;
5108
93f22b45
MS
5109 p2ps_prov->pd_seeker = 1;
5110
f309c18e
KV
5111 return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
5112 p2ps_prov);
5113}
5114
5115
c27f4c90
AK
5116static int parse_freq(int chwidth, int freq2)
5117{
5118 if (freq2 < 0)
5119 return -1;
5120 if (freq2)
5121 return VHT_CHANWIDTH_80P80MHZ;
5122
5123 switch (chwidth) {
5124 case 0:
5125 case 20:
5126 case 40:
5127 return VHT_CHANWIDTH_USE_HT;
5128 case 80:
5129 return VHT_CHANWIDTH_80MHZ;
5130 case 160:
5131 return VHT_CHANWIDTH_160MHZ;
5132 default:
5133 wpa_printf(MSG_DEBUG, "Unknown max oper bandwidth: %d",
5134 chwidth);
5135 return -1;
5136 }
5137}
5138
5139
b563b388
JM
5140static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
5141 char *buf, size_t buflen)
5142{
5143 u8 addr[ETH_ALEN];
5144 char *pos, *pos2;
5145 char *pin = NULL;
5146 enum p2p_wps_method wps_method;
5147 int new_pin;
5148 int ret;
23c84252 5149 int persistent_group, persistent_id = -1;
b563b388
JM
5150 int join;
5151 int auth;
b31be3a0 5152 int automatic;
b563b388
JM
5153 int go_intent = -1;
5154 int freq = 0;
3bc462cb 5155 int pd;
c27f4c90 5156 int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
8edd9f10
JM
5157 u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
5158 size_t group_ssid_len = 0;
b563b388 5159
bdf0518b
JM
5160 if (!wpa_s->global->p2p_init_wpa_s)
5161 return -1;
5162 if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
5163 wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
5164 wpa_s->global->p2p_init_wpa_s->ifname);
5165 wpa_s = wpa_s->global->p2p_init_wpa_s;
5166 }
5167
4f88fc04 5168 /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad|p2ps]
23c84252 5169 * [persistent|persistent=<network id>]
e2308e4b 5170 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
8edd9f10 5171 * [ht40] [vht] [auto] [ssid=<hexdump>] */
b563b388
JM
5172
5173 if (hwaddr_aton(cmd, addr))
5174 return -1;
5175
5176 pos = cmd + 17;
5177 if (*pos != ' ')
5178 return -1;
5179 pos++;
5180
5181 persistent_group = os_strstr(pos, " persistent") != NULL;
23c84252
JM
5182 pos2 = os_strstr(pos, " persistent=");
5183 if (pos2) {
5184 struct wpa_ssid *ssid;
5185 persistent_id = atoi(pos2 + 12);
5186 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5187 if (ssid == NULL || ssid->disabled != 2 ||
5188 ssid->mode != WPAS_MODE_P2P_GO) {
5189 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
5190 "SSID id=%d for persistent P2P group (GO)",
5191 persistent_id);
5192 return -1;
5193 }
5194 }
b563b388
JM
5195 join = os_strstr(pos, " join") != NULL;
5196 auth = os_strstr(pos, " auth") != NULL;
b31be3a0 5197 automatic = os_strstr(pos, " auto") != NULL;
3bc462cb 5198 pd = os_strstr(pos, " provdisc") != NULL;
20ea1ca4
EP
5199 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
5200 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
5201 vht;
b563b388
JM
5202
5203 pos2 = os_strstr(pos, " go_intent=");
5204 if (pos2) {
5205 pos2 += 11;
5206 go_intent = atoi(pos2);
5207 if (go_intent < 0 || go_intent > 15)
5208 return -1;
5209 }
5210
5211 pos2 = os_strstr(pos, " freq=");
5212 if (pos2) {
5213 pos2 += 6;
5214 freq = atoi(pos2);
5215 if (freq <= 0)
5216 return -1;
5217 }
5218
c27f4c90
AK
5219 pos2 = os_strstr(pos, " freq2=");
5220 if (pos2)
5221 freq2 = atoi(pos2 + 7);
5222
5223 pos2 = os_strstr(pos, " max_oper_chwidth=");
5224 if (pos2)
5225 chwidth = atoi(pos2 + 18);
5226
5227 max_oper_chwidth = parse_freq(chwidth, freq2);
5228 if (max_oper_chwidth < 0)
5229 return -1;
5230
8edd9f10
JM
5231 pos2 = os_strstr(pos, " ssid=");
5232 if (pos2) {
5233 char *end;
5234
5235 pos2 += 6;
5236 end = os_strchr(pos2, ' ');
5237 if (!end)
5238 group_ssid_len = os_strlen(pos2) / 2;
5239 else
5240 group_ssid_len = (end - pos2) / 2;
5241 if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
5242 hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
5243 return -1;
5244 group_ssid = _group_ssid;
5245 }
5246
b563b388
JM
5247 if (os_strncmp(pos, "pin", 3) == 0) {
5248 /* Request random PIN (to be displayed) and enable the PIN */
5249 wps_method = WPS_PIN_DISPLAY;
5250 } else if (os_strncmp(pos, "pbc", 3) == 0) {
5251 wps_method = WPS_PBC;
31d7fb14
PK
5252 } else if (os_strstr(pos, "p2ps") != NULL) {
5253 wps_method = WPS_P2PS;
b563b388
JM
5254 } else {
5255 pin = pos;
5256 pos = os_strchr(pin, ' ');
5257 wps_method = WPS_PIN_KEYPAD;
5258 if (pos) {
5259 *pos++ = '\0';
07fecd39 5260 if (os_strncmp(pos, "display", 7) == 0)
b563b388
JM
5261 wps_method = WPS_PIN_DISPLAY;
5262 }
dcc33057 5263 if (!wps_pin_str_valid(pin)) {
36ebf7a1
MH
5264 os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
5265 return 17;
5266 }
b563b388
JM
5267 }
5268
5269 new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
b31be3a0 5270 persistent_group, automatic, join,
c27f4c90 5271 auth, go_intent, freq, freq2, persistent_id,
8edd9f10
JM
5272 pd, ht40, vht, max_oper_chwidth,
5273 group_ssid, group_ssid_len);
d054a462
JM
5274 if (new_pin == -2) {
5275 os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
5276 return 25;
5277 }
5278 if (new_pin == -3) {
5279 os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
5280 return 25;
5281 }
b563b388
JM
5282 if (new_pin < 0)
5283 return -1;
5284 if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
5285 ret = os_snprintf(buf, buflen, "%08d", new_pin);
d85e1fc8 5286 if (os_snprintf_error(buflen, ret))
b563b388
JM
5287 return -1;
5288 return ret;
5289 }
5290
5291 os_memcpy(buf, "OK\n", 3);
5292 return 3;
5293}
5294
5295
5296static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
5297{
5298 unsigned int timeout = atoi(cmd);
e9eb648e
JM
5299 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5300 wpa_dbg(wpa_s, MSG_INFO,
5301 "Reject P2P_LISTEN since interface is disabled");
5302 return -1;
5303 }
b563b388
JM
5304 return wpas_p2p_listen(wpa_s, timeout);
5305}
5306
5307
5308static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
5309{
5310 u8 addr[ETH_ALEN];
5311 char *pos;
0918c4bf 5312 enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
b563b388 5313
0918c4bf 5314 /* <addr> <config method> [join|auto] */
b563b388
JM
5315
5316 if (hwaddr_aton(cmd, addr))
5317 return -1;
5318
5319 pos = cmd + 17;
5320 if (*pos != ' ')
5321 return -1;
5322 pos++;
5323
0918c4bf
JM
5324 if (os_strstr(pos, " join") != NULL)
5325 use = WPAS_P2P_PD_FOR_JOIN;
5326 else if (os_strstr(pos, " auto") != NULL)
5327 use = WPAS_P2P_PD_AUTO;
5328
6d908514 5329 return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
b563b388
JM
5330}
5331
5332
5333static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
5334 size_t buflen)
5335{
5336 struct wpa_ssid *ssid = wpa_s->current_ssid;
5337
5338 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
5339 ssid->passphrase == NULL)
5340 return -1;
5341
5342 os_strlcpy(buf, ssid->passphrase, buflen);
5343 return os_strlen(buf);
5344}
5345
5346
5347static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
5348 char *buf, size_t buflen)
5349{
5350 u64 ref;
5351 int res;
5352 u8 dst_buf[ETH_ALEN], *dst;
5353 struct wpabuf *tlvs;
5354 char *pos;
5355 size_t len;
5356
5357 if (hwaddr_aton(cmd, dst_buf))
5358 return -1;
5359 dst = dst_buf;
5360 if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
5361 dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
5362 dst = NULL;
5363 pos = cmd + 17;
5364 if (*pos != ' ')
5365 return -1;
5366 pos++;
5367
5368 if (os_strncmp(pos, "upnp ", 5) == 0) {
5369 u8 version;
5370 pos += 5;
5371 if (hexstr2bin(pos, &version, 1) < 0)
5372 return -1;
5373 pos += 2;
5374 if (*pos != ' ')
5375 return -1;
5376 pos++;
7165c5dc 5377 ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
347d6a5b
JM
5378#ifdef CONFIG_WIFI_DISPLAY
5379 } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
5380 ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
5381#endif /* CONFIG_WIFI_DISPLAY */
5a4102ce
KV
5382 } else if (os_strncmp(pos, "asp ", 4) == 0) {
5383 char *svc_str;
5384 char *svc_info = NULL;
5385 u32 id;
5386
5387 pos += 4;
5388 if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
5389 return -1;
5390
5391 pos = os_strchr(pos, ' ');
5392 if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
5393 return -1;
5394
5395 svc_str = pos + 1;
5396
5397 pos = os_strchr(svc_str, ' ');
5398
5399 if (pos)
5400 *pos++ = '\0';
5401
5402 /* All remaining data is the svc_info string */
5403 if (pos && pos[0] && pos[0] != ' ') {
5404 len = os_strlen(pos);
5405
5406 /* Unescape in place */
5407 len = utf8_unescape(pos, len, pos, len);
5408 if (len > 0xff)
5409 return -1;
5410
5411 svc_info = pos;
5412 }
5413
5414 ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
5415 svc_str, svc_info);
b563b388
JM
5416 } else {
5417 len = os_strlen(pos);
5418 if (len & 1)
5419 return -1;
5420 len /= 2;
5421 tlvs = wpabuf_alloc(len);
5422 if (tlvs == NULL)
5423 return -1;
5424 if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
5425 wpabuf_free(tlvs);
5426 return -1;
5427 }
5428
7165c5dc 5429 ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
b563b388
JM
5430 wpabuf_free(tlvs);
5431 }
7165c5dc
JM
5432 if (ref == 0)
5433 return -1;
b563b388 5434 res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
d85e1fc8 5435 if (os_snprintf_error(buflen, res))
b563b388
JM
5436 return -1;
5437 return res;
5438}
5439
5440
5441static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
5442 char *cmd)
5443{
5444 long long unsigned val;
5445 u64 req;
5446 if (sscanf(cmd, "%llx", &val) != 1)
5447 return -1;
5448 req = val;
7165c5dc 5449 return wpas_p2p_sd_cancel_request(wpa_s, req);
b563b388
JM
5450}
5451
5452
5453static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
5454{
5455 int freq;
d25f7212 5456 u8 dst[ETH_ALEN];
b563b388
JM
5457 u8 dialog_token;
5458 struct wpabuf *resp_tlvs;
5459 char *pos, *pos2;
5460 size_t len;
5461
5462 pos = os_strchr(cmd, ' ');
5463 if (pos == NULL)
5464 return -1;
5465 *pos++ = '\0';
5466 freq = atoi(cmd);
5467 if (freq == 0)
5468 return -1;
5469
d25f7212 5470 if (hwaddr_aton(pos, dst))
b563b388 5471 return -1;
b563b388
JM
5472 pos += 17;
5473 if (*pos != ' ')
5474 return -1;
5475 pos++;
5476
5477 pos2 = os_strchr(pos, ' ');
5478 if (pos2 == NULL)
5479 return -1;
5480 *pos2++ = '\0';
5481 dialog_token = atoi(pos);
5482
5483 len = os_strlen(pos2);
5484 if (len & 1)
5485 return -1;
5486 len /= 2;
5487 resp_tlvs = wpabuf_alloc(len);
5488 if (resp_tlvs == NULL)
5489 return -1;
5490 if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
5491 wpabuf_free(resp_tlvs);
5492 return -1;
5493 }
5494
5495 wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
5496 wpabuf_free(resp_tlvs);
5497 return 0;
5498}
5499
5500
5501static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
5502 char *cmd)
5503{
28ef705d
GB
5504 if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
5505 return -1;
b563b388
JM
5506 wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
5507 return 0;
5508}
5509
5510
5511static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
5512 char *cmd)
5513{
5514 char *pos;
5515 size_t len;
5516 struct wpabuf *query, *resp;
5517
5518 pos = os_strchr(cmd, ' ');
5519 if (pos == NULL)
5520 return -1;
5521 *pos++ = '\0';
5522
5523 len = os_strlen(cmd);
5524 if (len & 1)
5525 return -1;
5526 len /= 2;
5527 query = wpabuf_alloc(len);
5528 if (query == NULL)
5529 return -1;
5530 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
5531 wpabuf_free(query);
5532 return -1;
5533 }
5534
5535 len = os_strlen(pos);
5536 if (len & 1) {
5537 wpabuf_free(query);
5538 return -1;
5539 }
5540 len /= 2;
5541 resp = wpabuf_alloc(len);
5542 if (resp == NULL) {
5543 wpabuf_free(query);
5544 return -1;
5545 }
5546 if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
5547 wpabuf_free(query);
5548 wpabuf_free(resp);
5549 return -1;
5550 }
5551
5552 if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
5553 wpabuf_free(query);
5554 wpabuf_free(resp);
5555 return -1;
5556 }
5557 return 0;
5558}
5559
5560
5561static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
5562{
5563 char *pos;
5564 u8 version;
5565
5566 pos = os_strchr(cmd, ' ');
5567 if (pos == NULL)
5568 return -1;
5569 *pos++ = '\0';
5570
5571 if (hexstr2bin(cmd, &version, 1) < 0)
5572 return -1;
5573
5574 return wpas_p2p_service_add_upnp(wpa_s, version, pos);
5575}
5576
5577
ae9d45f3
KV
5578static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
5579 u8 replace, char *cmd)
5580{
5581 char *pos;
5582 char *adv_str;
5583 u32 auto_accept, adv_id, svc_state, config_methods;
5584 char *svc_info = NULL;
e2b7fbf2
MS
5585 char *cpt_prio_str;
5586 u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
ae9d45f3
KV
5587
5588 pos = os_strchr(cmd, ' ');
5589 if (pos == NULL)
5590 return -1;
5591 *pos++ = '\0';
5592
5593 /* Auto-Accept value is mandatory, and must be one of the
5594 * single values (0, 1, 2, 4) */
5595 auto_accept = atoi(cmd);
5596 switch (auto_accept) {
5597 case P2PS_SETUP_NONE: /* No auto-accept */
5598 case P2PS_SETUP_NEW:
5599 case P2PS_SETUP_CLIENT:
5600 case P2PS_SETUP_GROUP_OWNER:
5601 break;
5602 default:
5603 return -1;
5604 }
5605
5606 /* Advertisement ID is mandatory */
5607 cmd = pos;
5608 pos = os_strchr(cmd, ' ');
5609 if (pos == NULL)
5610 return -1;
5611 *pos++ = '\0';
5612
5613 /* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
5614 if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
5615 return -1;
5616
5617 /* Only allow replacements if exist, and adds if not */
5618 if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
5619 if (!replace)
5620 return -1;
5621 } else {
5622 if (replace)
5623 return -1;
5624 }
5625
5626 /* svc_state between 0 - 0xff is mandatory */
5627 if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
5628 return -1;
5629
5630 pos = os_strchr(pos, ' ');
5631 if (pos == NULL)
5632 return -1;
5633
5634 /* config_methods is mandatory */
5635 pos++;
5636 if (sscanf(pos, "%x", &config_methods) != 1)
5637 return -1;
5638
5639 if (!(config_methods &
5640 (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
5641 return -1;
5642
5643 pos = os_strchr(pos, ' ');
5644 if (pos == NULL)
5645 return -1;
5646
5647 pos++;
5648 adv_str = pos;
5649
5650 /* Advertisement string is mandatory */
5651 if (!pos[0] || pos[0] == ' ')
5652 return -1;
5653
5654 /* Terminate svc string */
5655 pos = os_strchr(pos, ' ');
5656 if (pos != NULL)
5657 *pos++ = '\0';
5658
e2b7fbf2
MS
5659 cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
5660 if (cpt_prio_str) {
5661 pos = os_strchr(pos, ' ');
5662 if (pos != NULL)
5663 *pos++ = '\0';
5664
5665 if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
5666 return -1;
5667 } else {
5668 cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
5669 cpt_prio[1] = 0;
5670 }
5671
ae9d45f3
KV
5672 /* Service and Response Information are optional */
5673 if (pos && pos[0]) {
5674 size_t len;
5675
5676 /* Note the bare ' included, which cannot exist legally
5677 * in unescaped string. */
5678 svc_info = os_strstr(pos, "svc_info='");
5679
5680 if (svc_info) {
5681 svc_info += 9;
5682 len = os_strlen(svc_info);
5683 utf8_unescape(svc_info, len, svc_info, len);
5684 }
5685 }
5686
5687 return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
5688 (u8) svc_state, (u16) config_methods,
e2b7fbf2 5689 svc_info, cpt_prio);
ae9d45f3
KV
5690}
5691
5692
b563b388
JM
5693static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
5694{
5695 char *pos;
5696
5697 pos = os_strchr(cmd, ' ');
5698 if (pos == NULL)
5699 return -1;
5700 *pos++ = '\0';
5701
5702 if (os_strcmp(cmd, "bonjour") == 0)
5703 return p2p_ctrl_service_add_bonjour(wpa_s, pos);
5704 if (os_strcmp(cmd, "upnp") == 0)
5705 return p2p_ctrl_service_add_upnp(wpa_s, pos);
ae9d45f3
KV
5706 if (os_strcmp(cmd, "asp") == 0)
5707 return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
b563b388
JM
5708 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
5709 return -1;
5710}
5711
5712
5713static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
5714 char *cmd)
5715{
5716 size_t len;
5717 struct wpabuf *query;
5718 int ret;
5719
5720 len = os_strlen(cmd);
5721 if (len & 1)
5722 return -1;
5723 len /= 2;
5724 query = wpabuf_alloc(len);
5725 if (query == NULL)
5726 return -1;
5727 if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
5728 wpabuf_free(query);
5729 return -1;
5730 }
5731
5732 ret = wpas_p2p_service_del_bonjour(wpa_s, query);
5733 wpabuf_free(query);
5734 return ret;
5735}
5736
5737
5738static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
5739{
5740 char *pos;
5741 u8 version;
5742
5743 pos = os_strchr(cmd, ' ');
5744 if (pos == NULL)
5745 return -1;
5746 *pos++ = '\0';
5747
5748 if (hexstr2bin(cmd, &version, 1) < 0)
5749 return -1;
5750
5751 return wpas_p2p_service_del_upnp(wpa_s, version, pos);
5752}
5753
5754
ae9d45f3
KV
5755static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
5756{
5757 u32 adv_id;
5758
e9d28050
MS
5759 if (os_strcmp(cmd, "all") == 0) {
5760 wpas_p2p_service_flush_asp(wpa_s);
5761 return 0;
5762 }
5763
ae9d45f3
KV
5764 if (sscanf(cmd, "%x", &adv_id) != 1)
5765 return -1;
5766
5767 return wpas_p2p_service_del_asp(wpa_s, adv_id);
5768}
5769
5770
b563b388
JM
5771static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
5772{
5773 char *pos;
5774
5775 pos = os_strchr(cmd, ' ');
5776 if (pos == NULL)
5777 return -1;
5778 *pos++ = '\0';
5779
5780 if (os_strcmp(cmd, "bonjour") == 0)
5781 return p2p_ctrl_service_del_bonjour(wpa_s, pos);
5782 if (os_strcmp(cmd, "upnp") == 0)
5783 return p2p_ctrl_service_del_upnp(wpa_s, pos);
ae9d45f3
KV
5784 if (os_strcmp(cmd, "asp") == 0)
5785 return p2p_ctrl_service_del_asp(wpa_s, pos);
5786 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
5787 return -1;
5788}
5789
5790
5791static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
5792{
5793 char *pos;
5794
5795 pos = os_strchr(cmd, ' ');
5796 if (pos == NULL)
5797 return -1;
5798 *pos++ = '\0';
5799
5800 if (os_strcmp(cmd, "asp") == 0)
5801 return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
5802
b563b388
JM
5803 wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
5804 return -1;
5805}
5806
5807
5808static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
5809{
5810 u8 addr[ETH_ALEN];
5811
5812 /* <addr> */
5813
5814 if (hwaddr_aton(cmd, addr))
5815 return -1;
5816
5817 return wpas_p2p_reject(wpa_s, addr);
5818}
5819
5820
5821static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
5822{
5823 char *pos;
5824 int id;
5825 struct wpa_ssid *ssid;
54c61e6e 5826 u8 *_peer = NULL, peer[ETH_ALEN];
f5877af0 5827 int freq = 0, pref_freq = 0;
c27f4c90 5828 int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
b563b388
JM
5829
5830 id = atoi(cmd);
5831 pos = os_strstr(cmd, " peer=");
5832 if (pos) {
5833 pos += 6;
5834 if (hwaddr_aton(pos, peer))
5835 return -1;
54c61e6e 5836 _peer = peer;
b563b388
JM
5837 }
5838 ssid = wpa_config_get_network(wpa_s->conf, id);
5839 if (ssid == NULL || ssid->disabled != 2) {
5840 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
5841 "for persistent P2P group",
5842 id);
5843 return -1;
5844 }
5845
4d32c0c4
JM
5846 pos = os_strstr(cmd, " freq=");
5847 if (pos) {
5848 pos += 6;
5849 freq = atoi(pos);
5850 if (freq <= 0)
5851 return -1;
5852 }
5853
f5877af0
JM
5854 pos = os_strstr(cmd, " pref=");
5855 if (pos) {
5856 pos += 6;
5857 pref_freq = atoi(pos);
5858 if (pref_freq <= 0)
5859 return -1;
5860 }
5861
20ea1ca4
EP
5862 vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
5863 ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
5864 vht;
4d32c0c4 5865
c27f4c90
AK
5866 pos = os_strstr(cmd, "freq2=");
5867 if (pos)
5868 freq2 = atoi(pos + 6);
5869
5870 pos = os_strstr(cmd, " max_oper_chwidth=");
5871 if (pos)
5872 chwidth = atoi(pos + 18);
5873
5874 max_oper_chwidth = parse_freq(chwidth, freq2);
5875 if (max_oper_chwidth < 0)
5876 return -1;
5877
5878 return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
5879 max_oper_chwidth, pref_freq);
b563b388
JM
5880}
5881
5882
5883static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
5884{
5885 char *pos;
5886 u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
5887
5888 pos = os_strstr(cmd, " peer=");
5889 if (!pos)
5890 return -1;
5891
5892 *pos = '\0';
5893 pos += 6;
5894 if (hwaddr_aton(pos, peer)) {
5895 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
5896 return -1;
5897 }
5898
5899 pos = os_strstr(pos, " go_dev_addr=");
5900 if (pos) {
5901 pos += 13;
5902 if (hwaddr_aton(pos, go_dev_addr)) {
5903 wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
5904 pos);
5905 return -1;
5906 }
5907 go_dev = go_dev_addr;
5908 }
5909
5910 return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
5911}
5912
5913
5914static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
5915{
5916 if (os_strncmp(cmd, "persistent=", 11) == 0)
5917 return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
5918 if (os_strncmp(cmd, "group=", 6) == 0)
5919 return p2p_ctrl_invite_group(wpa_s, cmd + 6);
5920
5921 return -1;
5922}
5923
5924
5925static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
c27f4c90
AK
5926 int id, int freq, int vht_center_freq2,
5927 int ht40, int vht, int vht_chwidth)
b563b388 5928{
b563b388
JM
5929 struct wpa_ssid *ssid;
5930
b563b388
JM
5931 ssid = wpa_config_get_network(wpa_s->conf, id);
5932 if (ssid == NULL || ssid->disabled != 2) {
5933 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
5934 "for persistent P2P group",
5935 id);
5936 return -1;
5937 }
5938
c27f4c90
AK
5939 return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq,
5940 vht_center_freq2, 0, ht40, vht,
5941 vht_chwidth, NULL, 0, 0);
b563b388
JM
5942}
5943
5944
5945static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
5946{
29292d53
EP
5947 int freq = 0, persistent = 0, group_id = -1;
5948 int vht = wpa_s->conf->p2p_go_vht;
5949 int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
c27f4c90 5950 int max_oper_chwidth, chwidth = 0, freq2 = 0;
29292d53 5951 char *token, *context = NULL;
e11776a5 5952
29292d53
EP
5953 while ((token = str_token(cmd, " ", &context))) {
5954 if (sscanf(token, "freq=%d", &freq) == 1 ||
c27f4c90
AK
5955 sscanf(token, "freq2=%d", &freq2) == 1 ||
5956 sscanf(token, "persistent=%d", &group_id) == 1 ||
5957 sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1) {
29292d53
EP
5958 continue;
5959 } else if (os_strcmp(token, "ht40") == 0) {
5960 ht40 = 1;
5961 } else if (os_strcmp(token, "vht") == 0) {
5962 vht = 1;
5963 ht40 = 1;
5964 } else if (os_strcmp(token, "persistent") == 0) {
5965 persistent = 1;
5966 } else {
5967 wpa_printf(MSG_DEBUG,
5968 "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
5969 token);
5970 return -1;
5971 }
e11776a5
PK
5972 }
5973
c27f4c90
AK
5974 max_oper_chwidth = parse_freq(chwidth, freq2);
5975 if (max_oper_chwidth < 0)
5976 return -1;
5977
29292d53
EP
5978 if (group_id >= 0)
5979 return p2p_ctrl_group_add_persistent(wpa_s, group_id,
c27f4c90
AK
5980 freq, freq2, ht40, vht,
5981 max_oper_chwidth);
29292d53 5982
c27f4c90
AK
5983 return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
5984 max_oper_chwidth);
b563b388
JM
5985}
5986
5987
57b38882
PK
5988static int p2p_ctrl_group_member(struct wpa_supplicant *wpa_s, const char *cmd,
5989 char *buf, size_t buflen)
5990{
5991 u8 dev_addr[ETH_ALEN];
5992 struct wpa_ssid *ssid;
5993 int res;
5994 const u8 *iaddr;
5995
5996 ssid = wpa_s->current_ssid;
5997 if (!wpa_s->global->p2p || !ssid || ssid->mode != WPAS_MODE_P2P_GO ||
5998 hwaddr_aton(cmd, dev_addr))
5999 return -1;
6000
6001 iaddr = p2p_group_get_client_interface_addr(wpa_s->p2p_group, dev_addr);
6002 if (!iaddr)
6003 return -1;
6004 res = os_snprintf(buf, buflen, MACSTR, MAC2STR(iaddr));
6005 if (os_snprintf_error(buflen, res))
6006 return -1;
6007 return res;
6008}
6009
6010
b563b388
JM
6011static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
6012 char *buf, size_t buflen)
6013{
6014 u8 addr[ETH_ALEN], *addr_ptr;
b3ffc80b
JM
6015 int next, res;
6016 const struct p2p_peer_info *info;
6017 char *pos, *end;
6018 char devtype[WPS_DEV_TYPE_BUFSIZE];
87f841a1 6019 struct wpa_ssid *ssid;
f3989ced 6020 size_t i;
b563b388
JM
6021
6022 if (!wpa_s->global->p2p)
6023 return -1;
6024
6025 if (os_strcmp(cmd, "FIRST") == 0) {
6026 addr_ptr = NULL;
6027 next = 0;
6028 } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
6029 if (hwaddr_aton(cmd + 5, addr) < 0)
6030 return -1;
6031 addr_ptr = addr;
6032 next = 1;
6033 } else {
6034 if (hwaddr_aton(cmd, addr) < 0)
6035 return -1;
6036 addr_ptr = addr;
6037 next = 0;
6038 }
6039
b3ffc80b
JM
6040 info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
6041 if (info == NULL)
6042 return -1;
6043
6044 pos = buf;
6045 end = buf + buflen;
6046
6047 res = os_snprintf(pos, end - pos, MACSTR "\n"
6048 "pri_dev_type=%s\n"
6049 "device_name=%s\n"
6050 "manufacturer=%s\n"
6051 "model_name=%s\n"
6052 "model_number=%s\n"
6053 "serial_number=%s\n"
6054 "config_methods=0x%x\n"
6055 "dev_capab=0x%x\n"
6056 "group_capab=0x%x\n"
6057 "level=%d\n",
6058 MAC2STR(info->p2p_device_addr),
6059 wps_dev_type_bin2str(info->pri_dev_type,
6060 devtype, sizeof(devtype)),
6061 info->device_name,
6062 info->manufacturer,
6063 info->model_name,
6064 info->model_number,
6065 info->serial_number,
6066 info->config_methods,
6067 info->dev_capab,
6068 info->group_capab,
6069 info->level);
d85e1fc8 6070 if (os_snprintf_error(end - pos, res))
b3ffc80b
JM
6071 return pos - buf;
6072 pos += res;
6073
f3989ced
JM
6074 for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
6075 {
6076 const u8 *t;
6077 t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
6078 res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
6079 wps_dev_type_bin2str(t, devtype,
6080 sizeof(devtype)));
d85e1fc8 6081 if (os_snprintf_error(end - pos, res))
f3989ced
JM
6082 return pos - buf;
6083 pos += res;
6084 }
6085
c427ac92 6086 ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
87f841a1
JM
6087 if (ssid) {
6088 res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
d85e1fc8 6089 if (os_snprintf_error(end - pos, res))
87f841a1
JM
6090 return pos - buf;
6091 pos += res;
6092 }
6093
b3ffc80b
JM
6094 res = p2p_get_peer_info_txt(info, pos, end - pos);
6095 if (res < 0)
87f841a1 6096 return pos - buf;
b3ffc80b
JM
6097 pos += res;
6098
71a0e395
JM
6099 if (info->vendor_elems) {
6100 res = os_snprintf(pos, end - pos, "vendor_elems=");
d85e1fc8 6101 if (os_snprintf_error(end - pos, res))
71a0e395
JM
6102 return pos - buf;
6103 pos += res;
6104
6105 pos += wpa_snprintf_hex(pos, end - pos,
6106 wpabuf_head(info->vendor_elems),
6107 wpabuf_len(info->vendor_elems));
6108
6109 res = os_snprintf(pos, end - pos, "\n");
d85e1fc8 6110 if (os_snprintf_error(end - pos, res))
71a0e395
JM
6111 return pos - buf;
6112 pos += res;
6113 }
6114
b3ffc80b 6115 return pos - buf;
b563b388
JM
6116}
6117
6118
6f3bc72b
JM
6119static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
6120 const char *param)
6121{
af8a827b 6122 unsigned int i;
6f3bc72b
JM
6123
6124 if (wpa_s->global->p2p == NULL)
6125 return -1;
6126
af8a827b
JM
6127 if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
6128 return -1;
6f3bc72b 6129
af8a827b
JM
6130 for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
6131 struct wpa_freq_range *freq;
6132 freq = &wpa_s->global->p2p_disallow_freq.range[i];
6f3bc72b 6133 wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
af8a827b 6134 freq->min, freq->max);
6f3bc72b
JM
6135 }
6136
3a8f008a 6137 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
6f3bc72b
JM
6138 return 0;
6139}
6140
6141
b563b388
JM
6142static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
6143{
6144 char *param;
6145
6146 if (wpa_s->global->p2p == NULL)
6147 return -1;
6148
6149 param = os_strchr(cmd, ' ');
6150 if (param == NULL)
6151 return -1;
6152 *param++ = '\0';
6153
6154 if (os_strcmp(cmd, "discoverability") == 0) {
6155 p2p_set_client_discoverability(wpa_s->global->p2p,
6156 atoi(param));
6157 return 0;
6158 }
6159
6160 if (os_strcmp(cmd, "managed") == 0) {
6161 p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
6162 return 0;
6163 }
6164
6165 if (os_strcmp(cmd, "listen_channel") == 0) {
dfe0745c
LD
6166 char *pos;
6167 u8 channel, op_class;
6168
6169 channel = atoi(param);
6170 pos = os_strchr(param, ' ');
6171 op_class = pos ? atoi(pos) : 81;
6172
6173 return p2p_set_listen_channel(wpa_s->global->p2p, op_class,
6174 channel, 1);
b563b388
JM
6175 }
6176
6177 if (os_strcmp(cmd, "ssid_postfix") == 0) {
6178 return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
6179 os_strlen(param));
6180 }
6181
6182 if (os_strcmp(cmd, "noa") == 0) {
6183 char *pos;
6184 int count, start, duration;
6185 /* GO NoA parameters: count,start_offset(ms),duration(ms) */
6186 count = atoi(param);
6187 pos = os_strchr(param, ',');
6188 if (pos == NULL)
6189 return -1;
6190 pos++;
6191 start = atoi(pos);
6192 pos = os_strchr(pos, ',');
6193 if (pos == NULL)
6194 return -1;
6195 pos++;
6196 duration = atoi(pos);
6197 if (count < 0 || count > 255 || start < 0 || duration < 0)
6198 return -1;
6199 if (count == 0 && duration > 0)
6200 return -1;
6201 wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
6202 "start=%d duration=%d", count, start, duration);
aefb53bd 6203 return wpas_p2p_set_noa(wpa_s, count, start, duration);
b563b388
JM
6204 }
6205
c381508d
JM
6206 if (os_strcmp(cmd, "ps") == 0)
6207 return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
6208
6209 if (os_strcmp(cmd, "oppps") == 0)
6210 return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
6211
6212 if (os_strcmp(cmd, "ctwindow") == 0)
6213 return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
6214
b563b388
JM
6215 if (os_strcmp(cmd, "disabled") == 0) {
6216 wpa_s->global->p2p_disabled = atoi(param);
6217 wpa_printf(MSG_DEBUG, "P2P functionality %s",
6218 wpa_s->global->p2p_disabled ?
6219 "disabled" : "enabled");
6220 if (wpa_s->global->p2p_disabled) {
6221 wpas_p2p_stop_find(wpa_s);
108def93 6222 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
b563b388
JM
6223 p2p_flush(wpa_s->global->p2p);
6224 }
6225 return 0;
6226 }
6227
b9cfc09a
JJ
6228 if (os_strcmp(cmd, "conc_pref") == 0) {
6229 if (os_strcmp(param, "sta") == 0)
6230 wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
6231 else if (os_strcmp(param, "p2p") == 0)
6232 wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
6233 else {
6234 wpa_printf(MSG_INFO, "Invalid conc_pref value");
6235 return -1;
6236 }
6237 wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
6238 "%s", param);
6239 return 0;
6240 }
6241
6e6963ea
JM
6242 if (os_strcmp(cmd, "force_long_sd") == 0) {
6243 wpa_s->force_long_sd = atoi(param);
6244 return 0;
6245 }
6246
80c9582a
JM
6247 if (os_strcmp(cmd, "peer_filter") == 0) {
6248 u8 addr[ETH_ALEN];
6249 if (hwaddr_aton(param, addr))
6250 return -1;
6251 p2p_set_peer_filter(wpa_s->global->p2p, addr);
6252 return 0;
6253 }
6254
72044390
JM
6255 if (os_strcmp(cmd, "cross_connect") == 0)
6256 return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
6257
eea2fd9e
JM
6258 if (os_strcmp(cmd, "go_apsd") == 0) {
6259 if (os_strcmp(param, "disable") == 0)
6260 wpa_s->set_ap_uapsd = 0;
6261 else {
6262 wpa_s->set_ap_uapsd = 1;
6263 wpa_s->ap_uapsd = atoi(param);
6264 }
6265 return 0;
6266 }
6267
6268 if (os_strcmp(cmd, "client_apsd") == 0) {
6269 if (os_strcmp(param, "disable") == 0)
6270 wpa_s->set_sta_uapsd = 0;
6271 else {
6272 int be, bk, vi, vo;
6273 char *pos;
6274 /* format: BE,BK,VI,VO;max SP Length */
6275 be = atoi(param);
6276 pos = os_strchr(param, ',');
6277 if (pos == NULL)
6278 return -1;
6279 pos++;
6280 bk = atoi(pos);
6281 pos = os_strchr(pos, ',');
6282 if (pos == NULL)
6283 return -1;
6284 pos++;
6285 vi = atoi(pos);
6286 pos = os_strchr(pos, ',');
6287 if (pos == NULL)
6288 return -1;
6289 pos++;
6290 vo = atoi(pos);
6291 /* ignore max SP Length for now */
6292
6293 wpa_s->set_sta_uapsd = 1;
6294 wpa_s->sta_uapsd = 0;
6295 if (be)
6296 wpa_s->sta_uapsd |= BIT(0);
6297 if (bk)
6298 wpa_s->sta_uapsd |= BIT(1);
6299 if (vi)
6300 wpa_s->sta_uapsd |= BIT(2);
6301 if (vo)
6302 wpa_s->sta_uapsd |= BIT(3);
6303 }
6304 return 0;
6305 }
6306
6f3bc72b
JM
6307 if (os_strcmp(cmd, "disallow_freq") == 0)
6308 return p2p_ctrl_disallow_freq(wpa_s, param);
6309
96beff11
JM
6310 if (os_strcmp(cmd, "disc_int") == 0) {
6311 int min_disc_int, max_disc_int, max_disc_tu;
6312 char *pos;
6313
6314 pos = param;
6315
6316 min_disc_int = atoi(pos);
6317 pos = os_strchr(pos, ' ');
6318 if (pos == NULL)
6319 return -1;
6320 *pos++ = '\0';
6321
6322 max_disc_int = atoi(pos);
6323 pos = os_strchr(pos, ' ');
6324 if (pos == NULL)
6325 return -1;
6326 *pos++ = '\0';
6327
6328 max_disc_tu = atoi(pos);
6329
6330 return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
6331 max_disc_int, max_disc_tu);
6332 }
6333
05766ed8
JM
6334 if (os_strcmp(cmd, "per_sta_psk") == 0) {
6335 wpa_s->global->p2p_per_sta_psk = !!atoi(param);
6336 return 0;
6337 }
6338
c4f87a70
JM
6339#ifdef CONFIG_WPS_NFC
6340 if (os_strcmp(cmd, "nfc_tag") == 0)
6341 return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
6342#endif /* CONFIG_WPS_NFC */
6343
201b0f5f
JM
6344 if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
6345 wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
6346 return 0;
6347 }
6348
b563b388
JM
6349 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
6350 cmd);
6351
6352 return -1;
6353}
6354
6355
acb54643
JM
6356static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
6357{
6358 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6359 wpa_s->force_long_sd = 0;
477b082c 6360 wpas_p2p_stop_find(wpa_s);
b9da88d6 6361 wpa_s->parent->p2ps_method_config_any = 0;
acb54643
JM
6362 if (wpa_s->global->p2p)
6363 p2p_flush(wpa_s->global->p2p);
6364}
6365
6366
b563b388
JM
6367static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
6368{
6369 char *pos, *pos2;
6370 unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
6371
6372 if (cmd[0]) {
6373 pos = os_strchr(cmd, ' ');
6374 if (pos == NULL)
6375 return -1;
6376 *pos++ = '\0';
6377 dur1 = atoi(cmd);
6378
6379 pos2 = os_strchr(pos, ' ');
6380 if (pos2)
6381 *pos2++ = '\0';
6382 int1 = atoi(pos);
6383 } else
6384 pos2 = NULL;
6385
6386 if (pos2) {
6387 pos = os_strchr(pos2, ' ');
6388 if (pos == NULL)
6389 return -1;
6390 *pos++ = '\0';
6391 dur2 = atoi(pos2);
6392 int2 = atoi(pos);
6393 }
6394
6395 return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
6396}
6397
6398
6399static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
6400{
6401 char *pos;
6402 unsigned int period = 0, interval = 0;
6403
6404 if (cmd[0]) {
6405 pos = os_strchr(cmd, ' ');
6406 if (pos == NULL)
6407 return -1;
6408 *pos++ = '\0';
6409 period = atoi(cmd);
6410 interval = atoi(pos);
6411 }
6412
6413 return wpas_p2p_ext_listen(wpa_s, period, interval);
6414}
6415
f2c56602
JM
6416
6417static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
6418{
6419 const char *pos;
6420 u8 peer[ETH_ALEN];
6421 int iface_addr = 0;
6422
6423 pos = cmd;
6424 if (os_strncmp(pos, "iface=", 6) == 0) {
6425 iface_addr = 1;
6426 pos += 6;
6427 }
6428 if (hwaddr_aton(pos, peer))
6429 return -1;
6430
6431 wpas_p2p_remove_client(wpa_s, peer, iface_addr);
6432 return 0;
6433}
6434
a6f5b193
PX
6435
6436static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
6437{
6438 int freq = 0, period = 0, interval = 0, count = 0;
6439
6440 if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
6441 {
6442 wpa_printf(MSG_DEBUG,
6443 "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
6444 return -1;
6445 }
6446
6447 return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
6448}
6449
b563b388
JM
6450#endif /* CONFIG_P2P */
6451
6452
356d1488
JM
6453static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
6454{
6455 struct wpa_freq_range_list ranges;
6456 int *freqs = NULL;
6457 struct hostapd_hw_modes *mode;
6458 u16 i;
6459
6460 if (wpa_s->hw.modes == NULL)
6461 return NULL;
6462
6463 os_memset(&ranges, 0, sizeof(ranges));
6464 if (freq_range_list_parse(&ranges, val) < 0)
6465 return NULL;
6466
6467 for (i = 0; i < wpa_s->hw.num_modes; i++) {
6468 int j;
6469
6470 mode = &wpa_s->hw.modes[i];
6471 for (j = 0; j < mode->num_channels; j++) {
6472 unsigned int freq;
6473
6474 if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
6475 continue;
6476
6477 freq = mode->channels[j].freq;
6478 if (!freq_range_list_includes(&ranges, freq))
6479 continue;
6480
6481 int_array_add_unique(&freqs, freq);
6482 }
6483 }
6484
6485 os_free(ranges.range);
6486 return freqs;
6487}
6488
6489
afc064fe 6490#ifdef CONFIG_INTERWORKING
356d1488
JM
6491
6492static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
6493{
6494 int auto_sel = 0;
6495 int *freqs = NULL;
6496
6497 if (param) {
6498 char *pos;
6499
6500 auto_sel = os_strstr(param, "auto") != NULL;
6501
6502 pos = os_strstr(param, "freq=");
6503 if (pos) {
6504 freqs = freq_range_to_channel_list(wpa_s, pos + 5);
6505 if (freqs == NULL)
6506 return -1;
6507 }
6508
6509 }
6510
6511 return interworking_select(wpa_s, auto_sel, freqs);
6512}
6513
6514
f91a512f
JM
6515static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
6516 int only_add)
b02fe7ff
JM
6517{
6518 u8 bssid[ETH_ALEN];
6519 struct wpa_bss *bss;
6520
6521 if (hwaddr_aton(dst, bssid)) {
6522 wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
6523 return -1;
6524 }
6525
6526 bss = wpa_bss_get_bssid(wpa_s, bssid);
6527 if (bss == NULL) {
6528 wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
6529 MAC2STR(bssid));
6530 return -1;
6531 }
6532
783b2a97
JM
6533 if (bss->ssid_len == 0) {
6534 int found = 0;
6535
6536 wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
6537 " does not have SSID information", MAC2STR(bssid));
6538
6539 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
6540 list) {
6541 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
6542 bss->ssid_len > 0) {
6543 found = 1;
6544 break;
6545 }
6546 }
6547
6548 if (!found)
6549 return -1;
6550 wpa_printf(MSG_DEBUG,
6551 "Found another matching BSS entry with SSID");
6552 }
6553
f91a512f 6554 return interworking_connect(wpa_s, bss, only_add);
b02fe7ff
JM
6555}
6556
6557
afc064fe
JM
6558static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
6559{
6560 u8 dst_addr[ETH_ALEN];
6561 int used;
6562 char *pos;
6563#define MAX_ANQP_INFO_ID 100
6564 u16 id[MAX_ANQP_INFO_ID];
6565 size_t num_id = 0;
cf28c66b 6566 u32 subtypes = 0;
8f479174 6567 int get_cell_pref = 0;
afc064fe
JM
6568
6569 used = hwaddr_aton2(dst, dst_addr);
6570 if (used < 0)
6571 return -1;
6572 pos = dst + used;
b68d602d
JM
6573 if (*pos == ' ')
6574 pos++;
afc064fe 6575 while (num_id < MAX_ANQP_INFO_ID) {
cf28c66b
DS
6576 if (os_strncmp(pos, "hs20:", 5) == 0) {
6577#ifdef CONFIG_HS20
6578 int num = atoi(pos + 5);
6579 if (num <= 0 || num > 31)
6580 return -1;
6581 subtypes |= BIT(num);
6582#else /* CONFIG_HS20 */
6583 return -1;
6584#endif /* CONFIG_HS20 */
8f479174 6585 } else if (os_strncmp(pos, "mbo:", 4) == 0) {
6586#ifdef CONFIG_MBO
6587 int num = atoi(pos + 4);
6588 if (num != MBO_ANQP_SUBTYPE_CELL_CONN_PREF)
6589 return -1;
6590 get_cell_pref = 1;
6591#else /* CONFIG_MBO */
6592 return -1;
6593#endif /* CONFIG_MBO */
cf28c66b
DS
6594 } else {
6595 id[num_id] = atoi(pos);
6596 if (id[num_id])
6597 num_id++;
6598 }
afc064fe
JM
6599 pos = os_strchr(pos + 1, ',');
6600 if (pos == NULL)
6601 break;
6602 pos++;
6603 }
6604
6605 if (num_id == 0)
6606 return -1;
6607
8f479174 6608 return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes,
6609 get_cell_pref);
afc064fe 6610}
b1f12296
JM
6611
6612
6613static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
6614{
6615 u8 dst_addr[ETH_ALEN];
6616 struct wpabuf *advproto, *query = NULL;
6617 int used, ret = -1;
6618 char *pos, *end;
6619 size_t len;
6620
6621 used = hwaddr_aton2(cmd, dst_addr);
6622 if (used < 0)
6623 return -1;
6624
6625 pos = cmd + used;
6626 while (*pos == ' ')
6627 pos++;
6628
6629 /* Advertisement Protocol ID */
6630 end = os_strchr(pos, ' ');
6631 if (end)
6632 len = end - pos;
6633 else
6634 len = os_strlen(pos);
6635 if (len & 0x01)
6636 return -1;
6637 len /= 2;
6638 if (len == 0)
6639 return -1;
6640 advproto = wpabuf_alloc(len);
6641 if (advproto == NULL)
6642 return -1;
6643 if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
6644 goto fail;
6645
6646 if (end) {
6647 /* Optional Query Request */
6648 pos = end + 1;
6649 while (*pos == ' ')
6650 pos++;
6651
6652 len = os_strlen(pos);
6653 if (len) {
6654 if (len & 0x01)
6655 goto fail;
6656 len /= 2;
6657 if (len == 0)
6658 goto fail;
6659 query = wpabuf_alloc(len);
6660 if (query == NULL)
6661 goto fail;
6662 if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
6663 goto fail;
6664 }
6665 }
6666
6667 ret = gas_send_request(wpa_s, dst_addr, advproto, query);
6668
6669fail:
6670 wpabuf_free(advproto);
6671 wpabuf_free(query);
6672
6673 return ret;
6674}
6675
6676
6677static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
6678 size_t buflen)
6679{
6680 u8 addr[ETH_ALEN];
6681 int dialog_token;
6682 int used;
6683 char *pos;
6684 size_t resp_len, start, requested_len;
b6a9590b
JM
6685 struct wpabuf *resp;
6686 int ret;
b1f12296
JM
6687
6688 used = hwaddr_aton2(cmd, addr);
6689 if (used < 0)
6690 return -1;
6691
6692 pos = cmd + used;
6693 while (*pos == ' ')
6694 pos++;
6695 dialog_token = atoi(pos);
6696
b6a9590b
JM
6697 if (wpa_s->last_gas_resp &&
6698 os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
6699 dialog_token == wpa_s->last_gas_dialog_token)
6700 resp = wpa_s->last_gas_resp;
6701 else if (wpa_s->prev_gas_resp &&
6702 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
6703 dialog_token == wpa_s->prev_gas_dialog_token)
6704 resp = wpa_s->prev_gas_resp;
6705 else
b1f12296
JM
6706 return -1;
6707
b6a9590b 6708 resp_len = wpabuf_len(resp);
b1f12296
JM
6709 start = 0;
6710 requested_len = resp_len;
6711
6712 pos = os_strchr(pos, ' ');
6713 if (pos) {
6714 start = atoi(pos);
6715 if (start > resp_len)
6716 return os_snprintf(buf, buflen, "FAIL-Invalid range");
6717 pos = os_strchr(pos, ',');
6718 if (pos == NULL)
6719 return -1;
6720 pos++;
6721 requested_len = atoi(pos);
6722 if (start + requested_len > resp_len)
6723 return os_snprintf(buf, buflen, "FAIL-Invalid range");
6724 }
6725
6726 if (requested_len * 2 + 1 > buflen)
6727 return os_snprintf(buf, buflen, "FAIL-Too long response");
6728
b6a9590b
JM
6729 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
6730 requested_len);
6731
6732 if (start + requested_len == resp_len) {
6733 /*
6734 * Free memory by dropping the response after it has been
6735 * fetched.
6736 */
6737 if (resp == wpa_s->prev_gas_resp) {
6738 wpabuf_free(wpa_s->prev_gas_resp);
6739 wpa_s->prev_gas_resp = NULL;
6740 } else {
6741 wpabuf_free(wpa_s->last_gas_resp);
6742 wpa_s->last_gas_resp = NULL;
6743 }
6744 }
6745
6746 return ret;
b1f12296 6747}
afc064fe
JM
6748#endif /* CONFIG_INTERWORKING */
6749
6750
a8918e86
JK
6751#ifdef CONFIG_HS20
6752
6753static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
6754{
6755 u8 dst_addr[ETH_ALEN];
6756 int used;
6757 char *pos;
6758 u32 subtypes = 0;
6759
6760 used = hwaddr_aton2(dst, dst_addr);
6761 if (used < 0)
6762 return -1;
6763 pos = dst + used;
b68d602d
JM
6764 if (*pos == ' ')
6765 pos++;
a8918e86
JK
6766 for (;;) {
6767 int num = atoi(pos);
6768 if (num <= 0 || num > 31)
6769 return -1;
6770 subtypes |= BIT(num);
6771 pos = os_strchr(pos + 1, ',');
6772 if (pos == NULL)
6773 break;
6774 pos++;
6775 }
6776
6777 if (subtypes == 0)
6778 return -1;
6779
8dd5c1b4 6780 return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
a8918e86
JK
6781}
6782
6783
6784static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
6785 const u8 *addr, const char *realm)
6786{
6787 u8 *buf;
6788 size_t rlen, len;
6789 int ret;
6790
6791 rlen = os_strlen(realm);
6792 len = 3 + rlen;
6793 buf = os_malloc(len);
6794 if (buf == NULL)
6795 return -1;
6796 buf[0] = 1; /* NAI Home Realm Count */
6797 buf[1] = 0; /* Formatted in accordance with RFC 4282 */
6798 buf[2] = rlen;
6799 os_memcpy(buf + 3, realm, rlen);
6800
6801 ret = hs20_anqp_send_req(wpa_s, addr,
6802 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
8dd5c1b4 6803 buf, len, 0);
a8918e86
JK
6804
6805 os_free(buf);
6806
6807 return ret;
6808}
6809
6810
6811static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
6812 char *dst)
6813{
6814 struct wpa_cred *cred = wpa_s->conf->cred;
6815 u8 dst_addr[ETH_ALEN];
6816 int used;
6817 u8 *buf;
6818 size_t len;
6819 int ret;
6820
6821 used = hwaddr_aton2(dst, dst_addr);
6822 if (used < 0)
6823 return -1;
6824
6825 while (dst[used] == ' ')
6826 used++;
6827 if (os_strncmp(dst + used, "realm=", 6) == 0)
6828 return hs20_nai_home_realm_list(wpa_s, dst_addr,
6829 dst + used + 6);
6830
6831 len = os_strlen(dst + used);
6832
6833 if (len == 0 && cred && cred->realm)
6834 return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
6835
0e87e798 6836 if (len & 1)
a8918e86
JK
6837 return -1;
6838 len /= 2;
6839 buf = os_malloc(len);
6840 if (buf == NULL)
6841 return -1;
6842 if (hexstr2bin(dst + used, buf, len) < 0) {
6843 os_free(buf);
6844 return -1;
6845 }
6846
6847 ret = hs20_anqp_send_req(wpa_s, dst_addr,
6848 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
8dd5c1b4 6849 buf, len, 0);
a8918e86
JK
6850 os_free(buf);
6851
6852 return ret;
6853}
6854
184e110c 6855
8dd5c1b4
JN
6856static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
6857 int buflen)
6858{
6859 u8 dst_addr[ETH_ALEN];
6860 int used;
6861 char *ctx = NULL, *icon, *poffset, *psize;
6862
6863 used = hwaddr_aton2(cmd, dst_addr);
6864 if (used < 0)
6865 return -1;
6866 cmd += used;
6867
6868 icon = str_token(cmd, " ", &ctx);
6869 poffset = str_token(cmd, " ", &ctx);
6870 psize = str_token(cmd, " ", &ctx);
6871 if (!icon || !poffset || !psize)
6872 return -1;
6873
6874 wpa_s->fetch_osu_icon_in_progress = 0;
6875 return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
6876 reply, buflen);
6877}
6878
6879
6880static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
6881{
6882 u8 dst_addr[ETH_ALEN];
6883 int used;
6884 char *icon;
6885
6886 if (!cmd[0])
6887 return hs20_del_icon(wpa_s, NULL, NULL);
6888
6889 used = hwaddr_aton2(cmd, dst_addr);
6890 if (used < 0)
6891 return -1;
6892
6893 while (cmd[used] == ' ')
6894 used++;
6895 icon = cmd[used] ? &cmd[used] : NULL;
6896
6897 return hs20_del_icon(wpa_s, dst_addr, icon);
6898}
6899
6900
6901static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
184e110c
JM
6902{
6903 u8 dst_addr[ETH_ALEN];
6904 int used;
6905 char *icon;
6906
6907 used = hwaddr_aton2(cmd, dst_addr);
6908 if (used < 0)
6909 return -1;
6910
6911 while (cmd[used] == ' ')
6912 used++;
6913 icon = &cmd[used];
6914
b572df86 6915 wpa_s->fetch_osu_icon_in_progress = 0;
184e110c 6916 return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
8dd5c1b4 6917 (u8 *) icon, os_strlen(icon), inmem);
184e110c
JM
6918}
6919
a8918e86
JK
6920#endif /* CONFIG_HS20 */
6921
6922
bc5d330a
TB
6923#ifdef CONFIG_AUTOSCAN
6924
6925static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
6926 char *cmd)
6927{
6928 enum wpa_states state = wpa_s->wpa_state;
6929 char *new_params = NULL;
6930
6931 if (os_strlen(cmd) > 0) {
6932 new_params = os_strdup(cmd);
6933 if (new_params == NULL)
6934 return -1;
6935 }
6936
6937 os_free(wpa_s->conf->autoscan);
6938 wpa_s->conf->autoscan = new_params;
6939
6940 if (wpa_s->conf->autoscan == NULL)
6941 autoscan_deinit(wpa_s);
6942 else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
99218999 6943 autoscan_init(wpa_s, 1);
99f00324
JM
6944 else if (state == WPA_SCANNING)
6945 wpa_supplicant_reinit_autoscan(wpa_s);
9d3f4a74
JM
6946 else
6947 wpa_printf(MSG_DEBUG, "No autoscan update in state %s",
6948 wpa_supplicant_state_txt(state));
bc5d330a
TB
6949
6950 return 0;
6951}
6952
6953#endif /* CONFIG_AUTOSCAN */
6954
6955
e9199e31
JM
6956#ifdef CONFIG_WNM
6957
6958static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
6959{
6960 int enter;
6961 int intval = 0;
6962 char *pos;
cd0ef657
JM
6963 int ret;
6964 struct wpabuf *tfs_req = NULL;
e9199e31
JM
6965
6966 if (os_strncmp(cmd, "enter", 5) == 0)
6967 enter = 1;
6968 else if (os_strncmp(cmd, "exit", 4) == 0)
6969 enter = 0;
6970 else
6971 return -1;
6972
6973 pos = os_strstr(cmd, " interval=");
6974 if (pos)
6975 intval = atoi(pos + 10);
6976
cd0ef657
JM
6977 pos = os_strstr(cmd, " tfs_req=");
6978 if (pos) {
6979 char *end;
6980 size_t len;
6981 pos += 9;
6982 end = os_strchr(pos, ' ');
6983 if (end)
6984 len = end - pos;
6985 else
6986 len = os_strlen(pos);
6987 if (len & 1)
6988 return -1;
6989 len /= 2;
6990 tfs_req = wpabuf_alloc(len);
6991 if (tfs_req == NULL)
6992 return -1;
6993 if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
6994 wpabuf_free(tfs_req);
6995 return -1;
6996 }
6997 }
6998
df80a0cc
JM
6999 ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
7000 WNM_SLEEP_MODE_EXIT, intval,
cd0ef657
JM
7001 tfs_req);
7002 wpabuf_free(tfs_req);
7003
7004 return ret;
e9199e31
JM
7005}
7006
65bcd0a9
VK
7007
7008static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
7009{
9a493fab 7010 int query_reason, list = 0;
65bcd0a9
VK
7011
7012 query_reason = atoi(cmd);
7013
9a493fab
AS
7014 cmd = os_strchr(cmd, ' ');
7015 if (cmd) {
7016 cmd++;
7017 if (os_strncmp(cmd, "list", 4) == 0) {
7018 list = 1;
7019 } else {
7020 wpa_printf(MSG_DEBUG, "WNM Query: Invalid option %s",
7021 cmd);
7022 return -1;
7023 }
7024 }
7025
7026 wpa_printf(MSG_DEBUG,
7027 "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
7028 query_reason, list ? " candidate list" : "");
65bcd0a9 7029
9a493fab 7030 return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason, list);
65bcd0a9
VK
7031}
7032
e9199e31
JM
7033#endif /* CONFIG_WNM */
7034
7035
60b24b0d
DS
7036static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
7037 size_t buflen)
7038{
7039 struct wpa_signal_info si;
7040 int ret;
2cc8d8f4 7041 char *pos, *end;
60b24b0d
DS
7042
7043 ret = wpa_drv_signal_poll(wpa_s, &si);
7044 if (ret)
7045 return -1;
7046
2cc8d8f4
AO
7047 pos = buf;
7048 end = buf + buflen;
7049
7050 ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
60b24b0d
DS
7051 "NOISE=%d\nFREQUENCY=%u\n",
7052 si.current_signal, si.current_txrate / 1000,
7053 si.current_noise, si.frequency);
7bdd8981 7054 if (os_snprintf_error(end - pos, ret))
60b24b0d 7055 return -1;
2cc8d8f4
AO
7056 pos += ret;
7057
7058 if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
7059 ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
7a4a93b9 7060 channel_width_to_string(si.chanwidth));
7bdd8981 7061 if (os_snprintf_error(end - pos, ret))
2cc8d8f4
AO
7062 return -1;
7063 pos += ret;
7064 }
7065
7066 if (si.center_frq1 > 0 && si.center_frq2 > 0) {
7067 ret = os_snprintf(pos, end - pos,
7068 "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
7069 si.center_frq1, si.center_frq2);
7bdd8981 7070 if (os_snprintf_error(end - pos, ret))
2cc8d8f4
AO
7071 return -1;
7072 pos += ret;
7073 }
7074
95783298
AO
7075 if (si.avg_signal) {
7076 ret = os_snprintf(pos, end - pos,
7077 "AVG_RSSI=%d\n", si.avg_signal);
d85e1fc8 7078 if (os_snprintf_error(end - pos, ret))
95783298
AO
7079 return -1;
7080 pos += ret;
7081 }
7082
74fa78b2
JM
7083 if (si.avg_beacon_signal) {
7084 ret = os_snprintf(pos, end - pos,
7085 "AVG_BEACON_RSSI=%d\n", si.avg_beacon_signal);
7086 if (os_snprintf_error(end - pos, ret))
7087 return -1;
7088 pos += ret;
7089 }
7090
2cc8d8f4 7091 return pos - buf;
60b24b0d
DS
7092}
7093
7094
96e8d831
DS
7095static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
7096 const char *cmd)
7097{
7098 const char *pos;
7099 int threshold = 0;
7100 int hysteresis = 0;
7101
7102 if (wpa_s->bgscan && wpa_s->bgscan_priv) {
7103 wpa_printf(MSG_DEBUG,
7104 "Reject SIGNAL_MONITOR command - bgscan is active");
7105 return -1;
7106 }
7107 pos = os_strstr(cmd, "THRESHOLD=");
7108 if (pos)
7109 threshold = atoi(pos + 10);
7110 pos = os_strstr(cmd, "HYSTERESIS=");
7111 if (pos)
7112 hysteresis = atoi(pos + 11);
7113 return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
7114}
7115
7116
98342208
AK
7117static int wpas_ctrl_iface_get_pref_freq_list(
7118 struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
7119{
7120 unsigned int freq_list[100], num = 100, i;
7121 int ret;
7122 enum wpa_driver_if_type iface_type;
7123 char *pos, *end;
7124
7125 pos = buf;
7126 end = buf + buflen;
7127
7128 /* buf: "<interface_type>" */
7129 if (os_strcmp(cmd, "STATION") == 0)
7130 iface_type = WPA_IF_STATION;
7131 else if (os_strcmp(cmd, "AP") == 0)
7132 iface_type = WPA_IF_AP_BSS;
7133 else if (os_strcmp(cmd, "P2P_GO") == 0)
7134 iface_type = WPA_IF_P2P_GO;
7135 else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
7136 iface_type = WPA_IF_P2P_CLIENT;
7137 else if (os_strcmp(cmd, "IBSS") == 0)
7138 iface_type = WPA_IF_IBSS;
7139 else if (os_strcmp(cmd, "TDLS") == 0)
7140 iface_type = WPA_IF_TDLS;
7141 else
7142 return -1;
7143
7144 wpa_printf(MSG_DEBUG,
7145 "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
7146 iface_type, buf);
7147
7148 ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
7149 if (ret)
7150 return -1;
7151
7152 for (i = 0; i < num; i++) {
7153 ret = os_snprintf(pos, end - pos, "%s%u",
7154 i > 0 ? "," : "", freq_list[i]);
7155 if (os_snprintf_error(end - pos, ret))
7156 return -1;
7157 pos += ret;
7158 }
7159
7160 return pos - buf;
7161}
7162
7163
4d7aab78
EL
7164static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
7165 char *buf, size_t buflen)
7166{
7167 int ret, i;
7168 char *pos, *end;
7169
7170 ret = os_snprintf(buf, buflen, "%016llX:\n",
7171 (long long unsigned) wpa_s->drv_flags);
7172 if (os_snprintf_error(buflen, ret))
7173 return -1;
7174
7175 pos = buf + ret;
7176 end = buf + buflen;
7177
7178 for (i = 0; i < 64; i++) {
7179 if (wpa_s->drv_flags & (1LLU << i)) {
7180 ret = os_snprintf(pos, end - pos, "%s\n",
7181 driver_flag_to_string(1LLU << i));
7182 if (os_snprintf_error(end - pos, ret))
7183 return -1;
7184 pos += ret;
7185 }
7186 }
7187
7188 return pos - buf;
7189}
7190
7191
dc7785f8
YZ
7192static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
7193 size_t buflen)
7194{
7195 struct hostap_sta_driver_data sta;
7196 int ret;
7197
7198 ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
7199 if (ret)
7200 return -1;
7201
7202 ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
7203 sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
7bdd8981 7204 if (os_snprintf_error(buflen, ret))
dc7785f8
YZ
7205 return -1;
7206 return ret;
7207}
7208
7209
5e2c3490
JM
7210#ifdef ANDROID
7211static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
7212 char *buf, size_t buflen)
7213{
7214 int ret;
7215
7216 ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
a94737ea
DS
7217 if (ret == 0) {
7218 if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
7219 struct p2p_data *p2p = wpa_s->global->p2p;
7220 if (p2p) {
7221 char country[3];
7222 country[0] = cmd[8];
7223 country[1] = cmd[9];
7224 country[2] = 0x04;
7225 p2p_set_country(p2p, country);
7226 }
7227 }
5e2c3490 7228 ret = os_snprintf(buf, buflen, "%s\n", "OK");
aaadd727
JM
7229 if (os_snprintf_error(buflen, ret))
7230 ret = -1;
a94737ea 7231 }
5e2c3490
JM
7232 return ret;
7233}
7234#endif /* ANDROID */
7235
7236
adef8948
BL
7237static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
7238 char *buf, size_t buflen)
7239{
7240 int ret;
7241 char *pos;
7242 u8 *data = NULL;
7243 unsigned int vendor_id, subcmd;
7244 struct wpabuf *reply;
7245 size_t data_len = 0;
7246
7247 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
7248 vendor_id = strtoul(cmd, &pos, 16);
640b0b93 7249 if (!isblank((unsigned char) *pos))
adef8948
BL
7250 return -EINVAL;
7251
7252 subcmd = strtoul(pos, &pos, 10);
7253
7254 if (*pos != '\0') {
640b0b93 7255 if (!isblank((unsigned char) *pos++))
adef8948
BL
7256 return -EINVAL;
7257 data_len = os_strlen(pos);
7258 }
7259
7260 if (data_len) {
7261 data_len /= 2;
7262 data = os_malloc(data_len);
7263 if (!data)
2cebdee6 7264 return -1;
adef8948
BL
7265
7266 if (hexstr2bin(pos, data, data_len)) {
7267 wpa_printf(MSG_DEBUG,
7268 "Vendor command: wrong parameter format");
7269 os_free(data);
7270 return -EINVAL;
7271 }
7272 }
7273
7274 reply = wpabuf_alloc((buflen - 1) / 2);
7275 if (!reply) {
7276 os_free(data);
2cebdee6 7277 return -1;
adef8948
BL
7278 }
7279
7280 ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
7281 reply);
7282
7283 if (ret == 0)
7284 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
7285 wpabuf_len(reply));
7286
7287 wpabuf_free(reply);
7288 os_free(data);
7289
7290 return ret;
7291}
7292
7293
acb54643
JM
7294static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
7295{
7e608d1d
IP
7296#ifdef CONFIG_P2P
7297 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
7298 wpa_s->global->p2p_init_wpa_s : wpa_s;
7299#endif /* CONFIG_P2P */
7300
acb54643
JM
7301 wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
7302
6e374bd4
JM
7303 if (wpas_abort_ongoing_scan(wpa_s) == 0)
7304 wpa_s->ignore_post_flush_scan_res = 1;
53401e91 7305
e9ccfc38
JM
7306 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
7307 /*
7308 * Avoid possible auto connect re-connection on getting
7309 * disconnected due to state flush.
7310 */
7311 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
7312 }
7313
acb54643 7314#ifdef CONFIG_P2P
1d20c66e 7315 wpas_p2p_group_remove(p2p_wpa_s, "*");
7e608d1d
IP
7316 wpas_p2p_cancel(p2p_wpa_s);
7317 p2p_ctrl_flush(p2p_wpa_s);
7e608d1d
IP
7318 wpas_p2p_service_flush(p2p_wpa_s);
7319 p2p_wpa_s->global->p2p_disabled = 0;
7320 p2p_wpa_s->global->p2p_per_sta_psk = 0;
7321 p2p_wpa_s->conf->num_sec_device_types = 0;
7322 p2p_wpa_s->p2p_disable_ip_addr_req = 0;
7323 os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
7324 p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
85e152b6 7325 p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
9a58e521 7326 p2p_wpa_s->global->pending_p2ps_group = 0;
8bb8e6ed 7327 p2p_wpa_s->global->pending_p2ps_group_freq = 0;
acb54643
JM
7328#endif /* CONFIG_P2P */
7329
7330#ifdef CONFIG_WPS_TESTING
7331 wps_version_number = 0x20;
7332 wps_testing_dummy_cred = 0;
91226e0d 7333 wps_corrupt_pkhash = 0;
6e379c6c
JM
7334 wps_force_auth_types_in_use = 0;
7335 wps_force_encr_types_in_use = 0;
acb54643
JM
7336#endif /* CONFIG_WPS_TESTING */
7337#ifdef CONFIG_WPS
7b02375a 7338 wpa_s->wps_fragment_size = 0;
acb54643 7339 wpas_wps_cancel(wpa_s);
422ba11e 7340 wps_registrar_flush(wpa_s->wps->registrar);
acb54643 7341#endif /* CONFIG_WPS */
7255983b 7342 wpa_s->after_wps = 0;
4d9fb08d 7343 wpa_s->known_wps_freq = 0;
acb54643 7344
9d2cb3ec 7345#ifdef CONFIG_TDLS
acb54643 7346#ifdef CONFIG_TDLS_TESTING
acb54643
JM
7347 tdls_testing = 0;
7348#endif /* CONFIG_TDLS_TESTING */
acb54643
JM
7349 wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
7350 wpa_tdls_enable(wpa_s->wpa, 1);
7351#endif /* CONFIG_TDLS */
7352
e78aaca0
JM
7353 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
7354 wpa_supplicant_stop_countermeasures(wpa_s, NULL);
7355
acb54643 7356 wpa_s->no_keep_alive = 0;
c2805909 7357 wpa_s->own_disconnect_req = 0;
acb54643
JM
7358
7359 os_free(wpa_s->disallow_aps_bssid);
7360 wpa_s->disallow_aps_bssid = NULL;
7361 wpa_s->disallow_aps_bssid_count = 0;
7362 os_free(wpa_s->disallow_aps_ssid);
7363 wpa_s->disallow_aps_ssid = NULL;
7364 wpa_s->disallow_aps_ssid_count = 0;
7365
7366 wpa_s->set_sta_uapsd = 0;
7367 wpa_s->sta_uapsd = 0;
7368
7369 wpa_drv_radio_disable(wpa_s, 0);
acb54643 7370 wpa_blacklist_clear(wpa_s);
a8a7890d 7371 wpa_s->extra_blacklist_count = 0;
acb54643
JM
7372 wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
7373 wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
d9bb2821 7374 wpa_config_flush_blobs(wpa_s->conf);
ea6e040c
JM
7375 wpa_s->conf->auto_interworking = 0;
7376 wpa_s->conf->okc = 0;
04f7ecc6 7377
b925506a
JM
7378 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
7379 rsn_preauth_deinit(wpa_s->wpa);
7380
04f7ecc6
JM
7381 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
7382 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
7383 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
0d79b50a 7384 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
b1ae396f 7385
b3253ebb 7386 radio_remove_works(wpa_s, NULL, 1);
e3745228 7387 wpa_s->ext_work_in_progress = 0;
3d910ef4
JM
7388
7389 wpa_s->next_ssid = NULL;
b572df86
JM
7390
7391#ifdef CONFIG_INTERWORKING
876e74aa 7392#ifdef CONFIG_HS20
b572df86 7393 hs20_cancel_fetch_osu(wpa_s);
8dd5c1b4 7394 hs20_del_icon(wpa_s, NULL, NULL);
876e74aa 7395#endif /* CONFIG_HS20 */
b572df86 7396#endif /* CONFIG_INTERWORKING */
60b893df
JM
7397
7398 wpa_s->ext_mgmt_frame_handling = 0;
9d4ff04a 7399 wpa_s->ext_eapol_frame_io = 0;
1f94e4ee
JM
7400#ifdef CONFIG_TESTING_OPTIONS
7401 wpa_s->extra_roc_dur = 0;
911942ee 7402 wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
ed7820b4 7403 wpa_s->p2p_go_csa_on_inv = 0;
02adead5 7404 wpa_s->ignore_auth_resp = 0;
6ad37d73 7405 wpa_s->ignore_assoc_disallow = 0;
a483c6f1 7406 wpa_s->reject_btm_req_reason = 0;
651c6a84 7407 wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
1f94e4ee 7408#endif /* CONFIG_TESTING_OPTIONS */
97cfe110
JM
7409
7410 wpa_s->disconnected = 0;
b8db1dfc
JM
7411 os_free(wpa_s->next_scan_freqs);
7412 wpa_s->next_scan_freqs = NULL;
6c699913
JM
7413
7414 wpa_bss_flush(wpa_s);
7415 if (!dl_list_empty(&wpa_s->bss)) {
7416 wpa_printf(MSG_DEBUG,
7417 "BSS table not empty after flush: %u entries, current_bss=%p bssid="
7418 MACSTR " pending_bssid=" MACSTR,
7419 dl_list_len(&wpa_s->bss), wpa_s->current_bss,
7420 MAC2STR(wpa_s->bssid),
7421 MAC2STR(wpa_s->pending_bssid));
7422 }
9bd566a3
AS
7423
7424 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
03ed0a52 7425 wpa_s->wnmsleep_used = 0;
ab6ab07a
JM
7426
7427#ifdef CONFIG_SME
7428 wpa_s->sme.last_unprot_disconnect.sec = 0;
7429#endif /* CONFIG_SME */
acb54643
JM
7430}
7431
7432
1f965e62
JM
7433static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
7434 char *buf, size_t buflen)
7435{
7436 struct wpa_radio_work *work;
7437 char *pos, *end;
7438 struct os_reltime now, diff;
7439
7440 pos = buf;
7441 end = buf + buflen;
7442
7443 os_get_reltime(&now);
7444
7445 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
7446 {
7447 int ret;
7448
7449 os_reltime_sub(&now, &work->time, &diff);
7450 ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
7451 work->type, work->wpa_s->ifname, work->freq,
7452 work->started, diff.sec, diff.usec);
d85e1fc8 7453 if (os_snprintf_error(end - pos, ret))
1f965e62
JM
7454 break;
7455 pos += ret;
7456 }
7457
7458 return pos - buf;
7459}
7460
7461
7462static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
7463{
7464 struct wpa_radio_work *work = eloop_ctx;
7465 struct wpa_external_work *ework = work->ctx;
7466
7467 wpa_dbg(work->wpa_s, MSG_DEBUG,
7468 "Timing out external radio work %u (%s)",
7469 ework->id, work->type);
7470 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
e3745228 7471 work->wpa_s->ext_work_in_progress = 0;
1f965e62 7472 radio_work_done(work);
df48efc5 7473 os_free(ework);
1f965e62
JM
7474}
7475
7476
7477static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
7478{
7479 struct wpa_external_work *ework = work->ctx;
7480
7481 if (deinit) {
b3253ebb
AO
7482 if (work->started)
7483 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
7484 work, NULL);
7485
7d1007a6
JM
7486 /*
7487 * work->type points to a buffer in ework, so need to replace
7488 * that here with a fixed string to avoid use of freed memory
7489 * in debug prints.
7490 */
7491 work->type = "freed-ext-work";
7492 work->ctx = NULL;
1f965e62
JM
7493 os_free(ework);
7494 return;
7495 }
7496
7497 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
7498 ework->id, ework->type);
7499 wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
e3745228 7500 work->wpa_s->ext_work_in_progress = 1;
1f965e62
JM
7501 if (!ework->timeout)
7502 ework->timeout = 10;
7503 eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
7504 work, NULL);
7505}
7506
7507
7508static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
7509 char *buf, size_t buflen)
7510{
7511 struct wpa_external_work *ework;
7512 char *pos, *pos2;
7513 size_t type_len;
7514 int ret;
7515 unsigned int freq = 0;
7516
7517 /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
7518
7519 ework = os_zalloc(sizeof(*ework));
7520 if (ework == NULL)
7521 return -1;
7522
7523 pos = os_strchr(cmd, ' ');
7524 if (pos) {
7525 type_len = pos - cmd;
7526 pos++;
7527
7528 pos2 = os_strstr(pos, "freq=");
7529 if (pos2)
7530 freq = atoi(pos2 + 5);
7531
7532 pos2 = os_strstr(pos, "timeout=");
7533 if (pos2)
7534 ework->timeout = atoi(pos2 + 8);
7535 } else {
7536 type_len = os_strlen(cmd);
7537 }
7538 if (4 + type_len >= sizeof(ework->type))
7539 type_len = sizeof(ework->type) - 4 - 1;
7540 os_strlcpy(ework->type, "ext:", sizeof(ework->type));
7541 os_memcpy(ework->type + 4, cmd, type_len);
7542 ework->type[4 + type_len] = '\0';
7543
7544 wpa_s->ext_work_id++;
7545 if (wpa_s->ext_work_id == 0)
7546 wpa_s->ext_work_id++;
7547 ework->id = wpa_s->ext_work_id;
7548
7549 if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
7550 ework) < 0) {
7551 os_free(ework);
7552 return -1;
7553 }
7554
7555 ret = os_snprintf(buf, buflen, "%u", ework->id);
d85e1fc8 7556 if (os_snprintf_error(buflen, ret))
1f965e62
JM
7557 return -1;
7558 return ret;
7559}
7560
7561
7562static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
7563{
7564 struct wpa_radio_work *work;
7565 unsigned int id = atoi(cmd);
7566
7567 dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
7568 {
7569 struct wpa_external_work *ework;
7570
7571 if (os_strncmp(work->type, "ext:", 4) != 0)
7572 continue;
7573 ework = work->ctx;
7574 if (id && ework->id != id)
7575 continue;
7576 wpa_dbg(wpa_s, MSG_DEBUG,
7577 "Completed external radio work %u (%s)",
7578 ework->id, ework->type);
7579 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
e3745228 7580 wpa_s->ext_work_in_progress = 0;
1f965e62 7581 radio_work_done(work);
6829da39 7582 os_free(ework);
1f965e62
JM
7583 return 3; /* "OK\n" */
7584 }
7585
7586 return -1;
7587}
7588
7589
7590static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
7591 char *buf, size_t buflen)
7592{
7593 if (os_strcmp(cmd, "show") == 0)
7594 return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
7595 if (os_strncmp(cmd, "add ", 4) == 0)
7596 return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
7597 if (os_strncmp(cmd, "done ", 5) == 0)
7598 return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
7599 return -1;
7600}
7601
7602
7603void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
7604{
7605 struct wpa_radio_work *work, *tmp;
7606
a6cff8bf
MS
7607 if (!wpa_s || !wpa_s->radio)
7608 return;
7609
1f965e62
JM
7610 dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
7611 struct wpa_radio_work, list) {
7612 struct wpa_external_work *ework;
7613
7614 if (os_strncmp(work->type, "ext:", 4) != 0)
7615 continue;
7616 ework = work->ctx;
7617 wpa_dbg(wpa_s, MSG_DEBUG,
023b466d 7618 "Flushing%s external radio work %u (%s)",
1f965e62
JM
7619 work->started ? " started" : "", ework->id,
7620 ework->type);
7621 if (work->started)
7622 eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
7623 work, NULL);
1f965e62 7624 radio_work_done(work);
df48efc5 7625 os_free(ework);
1f965e62
JM
7626 }
7627}
7628
7629
bceb8431
JM
7630static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
7631{
7632 struct wpa_supplicant *wpa_s = eloop_ctx;
7633 eapol_sm_notify_ctrl_response(wpa_s->eapol);
7634}
7635
7636
43a66ecb
JM
7637static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
7638 unsigned int *scan_id_count, int scan_id[])
6891f0e6
LJ
7639{
7640 const char *pos = value;
7641
7642 while (pos) {
7643 if (*pos == ' ' || *pos == '\0')
7644 break;
43a66ecb 7645 if (*scan_id_count == MAX_SCAN_ID)
6891f0e6 7646 return -1;
43a66ecb 7647 scan_id[(*scan_id_count)++] = atoi(pos);
6891f0e6
LJ
7648 pos = os_strchr(pos, ',');
7649 if (pos)
7650 pos++;
7651 }
7652
7653 return 0;
7654}
7655
7656
fee52342
JM
7657static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
7658 char *reply, int reply_size, int *reply_len)
7659{
7660 char *pos;
43a66ecb
JM
7661 unsigned int manual_scan_passive = 0;
7662 unsigned int manual_scan_use_id = 0;
7663 unsigned int manual_scan_only_new = 0;
7664 unsigned int scan_only = 0;
7665 unsigned int scan_id_count = 0;
7666 int scan_id[MAX_SCAN_ID];
7667 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
7668 struct wpa_scan_results *scan_res);
7669 int *manual_scan_freqs = NULL;
a80651d0
KV
7670 struct wpa_ssid_value *ssid = NULL, *ns;
7671 unsigned int ssid_count = 0;
fee52342
JM
7672
7673 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
7674 *reply_len = -1;
7675 return;
7676 }
7677
e69ae5ff
JM
7678 if (radio_work_pending(wpa_s, "scan")) {
7679 wpa_printf(MSG_DEBUG,
7680 "Pending scan scheduled - reject new request");
7681 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
7682 return;
7683 }
7684
9772af66
NM
7685#ifdef CONFIG_INTERWORKING
7686 if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
7687 wpa_printf(MSG_DEBUG,
7688 "Interworking select in progress - reject new scan");
7689 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
7690 return;
7691 }
7692#endif /* CONFIG_INTERWORKING */
7693
fee52342
JM
7694 if (params) {
7695 if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
43a66ecb 7696 scan_only = 1;
fee52342
JM
7697
7698 pos = os_strstr(params, "freq=");
43a66ecb
JM
7699 if (pos) {
7700 manual_scan_freqs = freq_range_to_channel_list(wpa_s,
7701 pos + 5);
7702 if (manual_scan_freqs == NULL) {
7703 *reply_len = -1;
7704 goto done;
7705 }
fee52342 7706 }
88c2d488
JM
7707
7708 pos = os_strstr(params, "passive=");
7709 if (pos)
43a66ecb 7710 manual_scan_passive = !!atoi(pos + 8);
d81c73be
JM
7711
7712 pos = os_strstr(params, "use_id=");
7713 if (pos)
43a66ecb 7714 manual_scan_use_id = atoi(pos + 7);
949938aa
JM
7715
7716 pos = os_strstr(params, "only_new=1");
7717 if (pos)
43a66ecb 7718 manual_scan_only_new = 1;
6891f0e6
LJ
7719
7720 pos = os_strstr(params, "scan_id=");
43a66ecb
JM
7721 if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
7722 scan_id) < 0) {
6891f0e6 7723 *reply_len = -1;
43a66ecb 7724 goto done;
6891f0e6 7725 }
a80651d0
KV
7726
7727 pos = params;
7728 while (pos && *pos != '\0') {
7729 if (os_strncmp(pos, "ssid ", 5) == 0) {
7730 char *end;
7731
7732 pos += 5;
7733 end = pos;
7734 while (*end) {
7735 if (*end == '\0' || *end == ' ')
7736 break;
7737 end++;
7738 }
7739
7740 ns = os_realloc_array(
7741 ssid, ssid_count + 1,
7742 sizeof(struct wpa_ssid_value));
7743 if (ns == NULL) {
7744 *reply_len = -1;
7745 goto done;
7746 }
7747 ssid = ns;
7748
7749 if ((end - pos) & 0x01 ||
7750 end - pos > 2 * SSID_MAX_LEN ||
7751 hexstr2bin(pos, ssid[ssid_count].ssid,
7752 (end - pos) / 2) < 0) {
7753 wpa_printf(MSG_DEBUG,
7754 "Invalid SSID value '%s'",
7755 pos);
7756 *reply_len = -1;
7757 goto done;
7758 }
7759 ssid[ssid_count].ssid_len = (end - pos) / 2;
7760 wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
7761 ssid[ssid_count].ssid,
7762 ssid[ssid_count].ssid_len);
7763 ssid_count++;
7764 pos = end;
7765 }
7766
7767 pos = os_strchr(pos, ' ');
7768 if (pos)
7769 pos++;
7770 }
7771 }
7772
7773 wpa_s->num_ssids_from_scan_req = ssid_count;
7774 os_free(wpa_s->ssids_from_scan_req);
7775 if (ssid_count) {
7776 wpa_s->ssids_from_scan_req = ssid;
7777 ssid = NULL;
7778 } else {
7779 wpa_s->ssids_from_scan_req = NULL;
fee52342
JM
7780 }
7781
43a66ecb
JM
7782 if (scan_only)
7783 scan_res_handler = scan_only_handler;
7784 else if (wpa_s->scan_res_handler == scan_only_handler)
7785 scan_res_handler = NULL;
7786 else
7787 scan_res_handler = wpa_s->scan_res_handler;
7788
fee52342
JM
7789 if (!wpa_s->sched_scanning && !wpa_s->scanning &&
7790 ((wpa_s->wpa_state <= WPA_SCANNING) ||
7791 (wpa_s->wpa_state == WPA_COMPLETED))) {
43a66ecb
JM
7792 wpa_s->manual_scan_passive = manual_scan_passive;
7793 wpa_s->manual_scan_use_id = manual_scan_use_id;
7794 wpa_s->manual_scan_only_new = manual_scan_only_new;
7795 wpa_s->scan_id_count = scan_id_count;
7796 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
7797 wpa_s->scan_res_handler = scan_res_handler;
7798 os_free(wpa_s->manual_scan_freqs);
7799 wpa_s->manual_scan_freqs = manual_scan_freqs;
7800 manual_scan_freqs = NULL;
7801
fee52342
JM
7802 wpa_s->normal_scans = 0;
7803 wpa_s->scan_req = MANUAL_SCAN_REQ;
7804 wpa_s->after_wps = 0;
7805 wpa_s->known_wps_freq = 0;
7806 wpa_supplicant_req_scan(wpa_s, 0, 0);
d81c73be
JM
7807 if (wpa_s->manual_scan_use_id) {
7808 wpa_s->manual_scan_id++;
7809 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
7810 wpa_s->manual_scan_id);
7811 *reply_len = os_snprintf(reply, reply_size, "%u\n",
7812 wpa_s->manual_scan_id);
7813 }
fee52342 7814 } else if (wpa_s->sched_scanning) {
43a66ecb
JM
7815 wpa_s->manual_scan_passive = manual_scan_passive;
7816 wpa_s->manual_scan_use_id = manual_scan_use_id;
7817 wpa_s->manual_scan_only_new = manual_scan_only_new;
7818 wpa_s->scan_id_count = scan_id_count;
7819 os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
7820 wpa_s->scan_res_handler = scan_res_handler;
7821 os_free(wpa_s->manual_scan_freqs);
7822 wpa_s->manual_scan_freqs = manual_scan_freqs;
7823 manual_scan_freqs = NULL;
7824
fee52342
JM
7825 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
7826 wpa_supplicant_cancel_sched_scan(wpa_s);
7827 wpa_s->scan_req = MANUAL_SCAN_REQ;
7828 wpa_supplicant_req_scan(wpa_s, 0, 0);
d81c73be
JM
7829 if (wpa_s->manual_scan_use_id) {
7830 wpa_s->manual_scan_id++;
7831 *reply_len = os_snprintf(reply, reply_size, "%u\n",
7832 wpa_s->manual_scan_id);
7833 wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
7834 wpa_s->manual_scan_id);
7835 }
fee52342
JM
7836 } else {
7837 wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
7838 *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
7839 }
43a66ecb
JM
7840
7841done:
7842 os_free(manual_scan_freqs);
a80651d0 7843 os_free(ssid);
fee52342
JM
7844}
7845
7846
60b893df
JM
7847#ifdef CONFIG_TESTING_OPTIONS
7848
7849static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
7850 unsigned int freq, const u8 *dst,
7851 const u8 *src, const u8 *bssid,
7852 const u8 *data, size_t data_len,
7853 enum offchannel_send_action_result
7854 result)
7855{
7856 wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
7857 " src=" MACSTR " bssid=" MACSTR " result=%s",
7858 freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
7859 result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
7860 "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
7861 "NO_ACK" : "FAILED"));
7862}
7863
7864
7865static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
7866{
7867 char *pos, *param;
7868 size_t len;
7869 u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
7870 int res, used;
7871 int freq = 0, no_cck = 0, wait_time = 0;
7872
7873 /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
7874 * <action=Action frame payload> */
7875
7876 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
7877
7878 pos = cmd;
7879 used = hwaddr_aton2(pos, da);
7880 if (used < 0)
7881 return -1;
7882 pos += used;
7883 while (*pos == ' ')
7884 pos++;
7885 used = hwaddr_aton2(pos, bssid);
7886 if (used < 0)
7887 return -1;
7888 pos += used;
7889
7890 param = os_strstr(pos, " freq=");
7891 if (param) {
7892 param += 6;
7893 freq = atoi(param);
7894 }
7895
7896 param = os_strstr(pos, " no_cck=");
7897 if (param) {
7898 param += 8;
7899 no_cck = atoi(param);
7900 }
7901
7902 param = os_strstr(pos, " wait_time=");
7903 if (param) {
7904 param += 11;
7905 wait_time = atoi(param);
7906 }
7907
7908 param = os_strstr(pos, " action=");
7909 if (param == NULL)
7910 return -1;
7911 param += 8;
7912
7913 len = os_strlen(param);
7914 if (len & 1)
7915 return -1;
7916 len /= 2;
7917
7918 buf = os_malloc(len);
7919 if (buf == NULL)
7920 return -1;
7921
7922 if (hexstr2bin(param, buf, len) < 0) {
7923 os_free(buf);
7924 return -1;
7925 }
7926
7927 res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
7928 buf, len, wait_time,
7929 wpas_ctrl_iface_mgmt_tx_cb, no_cck);
7930 os_free(buf);
7931 return res;
7932}
7933
7934
7935static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
7936{
7937 wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
7938 offchannel_send_action_done(wpa_s);
7939}
7940
ad12f2f4 7941
4de70e23
JM
7942static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
7943 char *cmd)
7944{
7945 char *pos, *param;
7946 size_t len;
7947 u8 *buf;
7948 int freq = 0, datarate = 0, ssi_signal = 0;
7949 union wpa_event_data event;
7950
7951 if (!wpa_s->ext_mgmt_frame_handling)
7952 return -1;
7953
7954 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
7955
7956 wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
7957
7958 pos = cmd;
7959 param = os_strstr(pos, "freq=");
7960 if (param) {
7961 param += 5;
7962 freq = atoi(param);
7963 }
7964
7965 param = os_strstr(pos, " datarate=");
7966 if (param) {
7967 param += 10;
7968 datarate = atoi(param);
7969 }
7970
7971 param = os_strstr(pos, " ssi_signal=");
7972 if (param) {
7973 param += 12;
7974 ssi_signal = atoi(param);
7975 }
7976
7977 param = os_strstr(pos, " frame=");
7978 if (param == NULL)
7979 return -1;
7980 param += 7;
7981
7982 len = os_strlen(param);
7983 if (len & 1)
7984 return -1;
7985 len /= 2;
7986
7987 buf = os_malloc(len);
7988 if (buf == NULL)
7989 return -1;
7990
7991 if (hexstr2bin(param, buf, len) < 0) {
7992 os_free(buf);
7993 return -1;
7994 }
7995
7996 os_memset(&event, 0, sizeof(event));
7997 event.rx_mgmt.freq = freq;
7998 event.rx_mgmt.frame = buf;
7999 event.rx_mgmt.frame_len = len;
8000 event.rx_mgmt.ssi_signal = ssi_signal;
8001 event.rx_mgmt.datarate = datarate;
8002 wpa_s->ext_mgmt_frame_handling = 0;
8003 wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
8004 wpa_s->ext_mgmt_frame_handling = 1;
8005
8006 os_free(buf);
8007
8008 return 0;
8009}
8010
8011
ad12f2f4
JM
8012static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
8013{
8014 char *pos, *param;
8015 union wpa_event_data event;
8016 enum wpa_event_type ev;
8017
8018 /* <event name> [parameters..] */
8019
8020 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
8021
8022 pos = cmd;
8023 param = os_strchr(pos, ' ');
8024 if (param)
8025 *param++ = '\0';
8026
8027 os_memset(&event, 0, sizeof(event));
8028
8029 if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
8030 ev = EVENT_INTERFACE_ENABLED;
8031 } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
8032 ev = EVENT_INTERFACE_DISABLED;
7bb70909
JM
8033 } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
8034 ev = EVENT_AVOID_FREQUENCIES;
8035 if (param == NULL)
8036 param = "";
8037 if (freq_range_list_parse(&event.freq_range, param) < 0)
8038 return -1;
8039 wpa_supplicant_event(wpa_s, ev, &event);
8040 os_free(event.freq_range.range);
8041 return 0;
ad12f2f4
JM
8042 } else {
8043 wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
8044 cmd);
8045 return -1;
8046 }
8047
8048 wpa_supplicant_event(wpa_s, ev, &event);
8049
8050 return 0;
8051}
8052
9d4ff04a
JM
8053
8054static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
8055{
8056 char *pos;
8057 u8 src[ETH_ALEN], *buf;
8058 int used;
8059 size_t len;
8060
8061 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
8062
8063 pos = cmd;
8064 used = hwaddr_aton2(pos, src);
8065 if (used < 0)
8066 return -1;
8067 pos += used;
8068 while (*pos == ' ')
8069 pos++;
8070
8071 len = os_strlen(pos);
8072 if (len & 1)
8073 return -1;
8074 len /= 2;
8075
8076 buf = os_malloc(len);
8077 if (buf == NULL)
8078 return -1;
8079
8080 if (hexstr2bin(pos, buf, len) < 0) {
8081 os_free(buf);
8082 return -1;
8083 }
8084
8085 wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
8086 os_free(buf);
8087
8088 return 0;
8089}
8090
4a6cc862
JM
8091
8092static u16 ipv4_hdr_checksum(const void *buf, size_t len)
8093{
8094 size_t i;
8095 u32 sum = 0;
8096 const u16 *pos = buf;
8097
8098 for (i = 0; i < len / 2; i++)
8099 sum += *pos++;
8100
8101 while (sum >> 16)
8102 sum = (sum & 0xffff) + (sum >> 16);
8103
8104 return sum ^ 0xffff;
8105}
8106
8107
8108#define HWSIM_PACKETLEN 1500
8109#define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
8110
0dbe22be
JM
8111static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
8112 size_t len)
4a6cc862
JM
8113{
8114 struct wpa_supplicant *wpa_s = ctx;
8115 const struct ether_header *eth;
75352270 8116 struct iphdr ip;
4a6cc862
JM
8117 const u8 *pos;
8118 unsigned int i;
8119
8120 if (len != HWSIM_PACKETLEN)
8121 return;
8122
8123 eth = (const struct ether_header *) buf;
75352270
JM
8124 os_memcpy(&ip, eth + 1, sizeof(ip));
8125 pos = &buf[sizeof(*eth) + sizeof(ip)];
4a6cc862 8126
75352270
JM
8127 if (ip.ihl != 5 || ip.version != 4 ||
8128 ntohs(ip.tot_len) != HWSIM_IP_LEN)
4a6cc862
JM
8129 return;
8130
75352270 8131 for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
4a6cc862
JM
8132 if (*pos != (u8) i)
8133 return;
8134 pos++;
8135 }
8136
8137 wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
8138 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
8139}
8140
8141
8142static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
8143 char *cmd)
8144{
8145 int enabled = atoi(cmd);
ba91e920
MB
8146 char *pos;
8147 const char *ifname;
4a6cc862
JM
8148
8149 if (!enabled) {
8150 if (wpa_s->l2_test) {
8151 l2_packet_deinit(wpa_s->l2_test);
8152 wpa_s->l2_test = NULL;
8153 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
8154 }
8155 return 0;
8156 }
8157
8158 if (wpa_s->l2_test)
8159 return 0;
8160
ba91e920
MB
8161 pos = os_strstr(cmd, " ifname=");
8162 if (pos)
8163 ifname = pos + 8;
8164 else
8165 ifname = wpa_s->ifname;
8166
8167 wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
4a6cc862
JM
8168 ETHERTYPE_IP, wpas_data_test_rx,
8169 wpa_s, 1);
8170 if (wpa_s->l2_test == NULL)
8171 return -1;
8172
8173 wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
8174
8175 return 0;
8176}
8177
8178
8179static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
8180{
8181 u8 dst[ETH_ALEN], src[ETH_ALEN];
8182 char *pos;
8183 int used;
8184 long int val;
8185 u8 tos;
75352270 8186 u8 buf[2 + HWSIM_PACKETLEN];
4a6cc862
JM
8187 struct ether_header *eth;
8188 struct iphdr *ip;
8189 u8 *dpos;
8190 unsigned int i;
8191
8192 if (wpa_s->l2_test == NULL)
8193 return -1;
8194
8195 /* format: <dst> <src> <tos> */
8196
8197 pos = cmd;
8198 used = hwaddr_aton2(pos, dst);
8199 if (used < 0)
8200 return -1;
8201 pos += used;
8202 while (*pos == ' ')
8203 pos++;
8204 used = hwaddr_aton2(pos, src);
8205 if (used < 0)
8206 return -1;
8207 pos += used;
8208
8209 val = strtol(pos, NULL, 0);
8210 if (val < 0 || val > 0xff)
8211 return -1;
8212 tos = val;
8213
75352270 8214 eth = (struct ether_header *) &buf[2];
4a6cc862
JM
8215 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
8216 os_memcpy(eth->ether_shost, src, ETH_ALEN);
8217 eth->ether_type = htons(ETHERTYPE_IP);
8218 ip = (struct iphdr *) (eth + 1);
8219 os_memset(ip, 0, sizeof(*ip));
8220 ip->ihl = 5;
8221 ip->version = 4;
8222 ip->ttl = 64;
8223 ip->tos = tos;
8224 ip->tot_len = htons(HWSIM_IP_LEN);
8225 ip->protocol = 1;
66f1e078
JM
8226 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
8227 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
4a6cc862
JM
8228 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
8229 dpos = (u8 *) (ip + 1);
8230 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
8231 *dpos++ = i;
8232
75352270 8233 if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
4a6cc862
JM
8234 HWSIM_PACKETLEN) < 0)
8235 return -1;
8236
8237 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
8238 " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
8239
8240 return 0;
8241}
8242
fc0ef7c0
JM
8243
8244static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
8245 char *cmd)
8246{
8247 u8 *buf;
8248 struct ether_header *eth;
8249 struct l2_packet_data *l2 = NULL;
8250 size_t len;
8251 u16 ethertype;
8252 int res = -1;
8253
8254 len = os_strlen(cmd);
8255 if (len & 1 || len < ETH_HLEN * 2)
8256 return -1;
8257 len /= 2;
8258
8259 buf = os_malloc(len);
8260 if (buf == NULL)
8261 return -1;
8262
8263 if (hexstr2bin(cmd, buf, len) < 0)
8264 goto done;
8265
8266 eth = (struct ether_header *) buf;
8267 ethertype = ntohs(eth->ether_type);
8268
8269 l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
8270 wpas_data_test_rx, wpa_s, 1);
8271 if (l2 == NULL)
8272 goto done;
8273
8274 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
8275 wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
8276done:
8277 if (l2)
8278 l2_packet_deinit(l2);
8279 os_free(buf);
8280
8281 return res < 0 ? -1 : 0;
8282}
8283
a156ffda
JM
8284
8285static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
8286{
8287#ifdef WPA_TRACE_BFD
a156ffda
JM
8288 char *pos;
8289
8290 wpa_trace_fail_after = atoi(cmd);
8291 pos = os_strchr(cmd, ':');
8292 if (pos) {
8293 pos++;
8294 os_strlcpy(wpa_trace_fail_func, pos,
8295 sizeof(wpa_trace_fail_func));
8296 } else {
8297 wpa_trace_fail_after = 0;
8298 }
8299 return 0;
8300#else /* WPA_TRACE_BFD */
8301 return -1;
8302#endif /* WPA_TRACE_BFD */
8303}
8304
8305
8306static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
8307 char *buf, size_t buflen)
8308{
8309#ifdef WPA_TRACE_BFD
a156ffda
JM
8310 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
8311 wpa_trace_fail_func);
8312#else /* WPA_TRACE_BFD */
8313 return -1;
8314#endif /* WPA_TRACE_BFD */
8315}
8316
2da52565
JM
8317
8318static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd)
8319{
8320#ifdef WPA_TRACE_BFD
2da52565
JM
8321 char *pos;
8322
8323 wpa_trace_test_fail_after = atoi(cmd);
8324 pos = os_strchr(cmd, ':');
8325 if (pos) {
8326 pos++;
8327 os_strlcpy(wpa_trace_test_fail_func, pos,
8328 sizeof(wpa_trace_test_fail_func));
8329 } else {
8330 wpa_trace_test_fail_after = 0;
8331 }
8332 return 0;
8333#else /* WPA_TRACE_BFD */
8334 return -1;
8335#endif /* WPA_TRACE_BFD */
8336}
8337
8338
8339static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s,
8340 char *buf, size_t buflen)
8341{
8342#ifdef WPA_TRACE_BFD
2da52565
JM
8343 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
8344 wpa_trace_test_fail_func);
8345#else /* WPA_TRACE_BFD */
8346 return -1;
8347#endif /* WPA_TRACE_BFD */
8348}
8349
a530fe77
JM
8350
8351static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
8352{
8353 struct wpa_supplicant *wpa_s = eloop_ctx;
8354 int i, count = (intptr_t) timeout_ctx;
8355
8356 wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
8357 count);
8358 for (i = 0; i < count; i++) {
8359 wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
8360 i + 1, count);
8361 }
8362}
8363
8364
8365static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
8366{
8367 int count;
8368
8369 count = atoi(cmd);
8370 if (count <= 0)
8371 return -1;
8372
8373 return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
8374 (void *) (intptr_t) count);
8375}
8376
651c6a84
JM
8377
8378static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
8379 const char *cmd)
8380{
8381 struct wpabuf *buf;
8382 size_t len;
8383
8384 len = os_strlen(cmd);
8385 if (len & 1)
8386 return -1;
8387 len /= 2;
8388
8389 if (len == 0) {
8390 buf = NULL;
8391 } else {
8392 buf = wpabuf_alloc(len);
8393 if (buf == NULL)
8394 return -1;
8395
8396 if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
8397 wpabuf_free(buf);
8398 return -1;
8399 }
8400 }
8401
8402 wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
8403 return 0;
8404}
8405
60b893df
JM
8406#endif /* CONFIG_TESTING_OPTIONS */
8407
8408
86bd36f0
JM
8409static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
8410{
8411 char *pos = cmd;
8412 int frame;
8413 size_t len;
8414 struct wpabuf *buf;
8415 struct ieee802_11_elems elems;
8416
8417 frame = atoi(pos);
8418 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
8419 return -1;
af041f99 8420 wpa_s = wpas_vendor_elem(wpa_s, frame);
86bd36f0
JM
8421
8422 pos = os_strchr(pos, ' ');
8423 if (pos == NULL)
8424 return -1;
8425 pos++;
8426
8427 len = os_strlen(pos);
8428 if (len == 0)
8429 return 0;
8430 if (len & 1)
8431 return -1;
8432 len /= 2;
8433
8434 buf = wpabuf_alloc(len);
8435 if (buf == NULL)
8436 return -1;
8437
8438 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
8439 wpabuf_free(buf);
8440 return -1;
8441 }
8442
8443 if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
8444 ParseFailed) {
8445 wpabuf_free(buf);
8446 return -1;
8447 }
8448
8449 if (wpa_s->vendor_elem[frame] == NULL) {
8450 wpa_s->vendor_elem[frame] = buf;
af041f99 8451 wpas_vendor_elem_update(wpa_s);
86bd36f0
JM
8452 return 0;
8453 }
8454
8455 if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
8456 wpabuf_free(buf);
8457 return -1;
8458 }
8459
8460 wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
8461 wpabuf_free(buf);
af041f99 8462 wpas_vendor_elem_update(wpa_s);
86bd36f0
JM
8463
8464 return 0;
8465}
8466
8467
8468static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
8469 char *buf, size_t buflen)
8470{
8471 int frame = atoi(cmd);
8472
8473 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
8474 return -1;
af041f99 8475 wpa_s = wpas_vendor_elem(wpa_s, frame);
86bd36f0
JM
8476
8477 if (wpa_s->vendor_elem[frame] == NULL)
8478 return 0;
8479
8480 return wpa_snprintf_hex(buf, buflen,
8481 wpabuf_head_u8(wpa_s->vendor_elem[frame]),
8482 wpabuf_len(wpa_s->vendor_elem[frame]));
8483}
8484
8485
8486static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
8487{
8488 char *pos = cmd;
8489 int frame;
8490 size_t len;
8491 u8 *buf;
8492 struct ieee802_11_elems elems;
af041f99 8493 int res;
86bd36f0
JM
8494
8495 frame = atoi(pos);
8496 if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
8497 return -1;
af041f99 8498 wpa_s = wpas_vendor_elem(wpa_s, frame);
86bd36f0
JM
8499
8500 pos = os_strchr(pos, ' ');
8501 if (pos == NULL)
8502 return -1;
8503 pos++;
8504
8505 if (*pos == '*') {
8506 wpabuf_free(wpa_s->vendor_elem[frame]);
8507 wpa_s->vendor_elem[frame] = NULL;
af041f99 8508 wpas_vendor_elem_update(wpa_s);
86bd36f0
JM
8509 return 0;
8510 }
8511
8512 if (wpa_s->vendor_elem[frame] == NULL)
8513 return -1;
8514
8515 len = os_strlen(pos);
8516 if (len == 0)
8517 return 0;
8518 if (len & 1)
8519 return -1;
8520 len /= 2;
8521
8522 buf = os_malloc(len);
8523 if (buf == NULL)
8524 return -1;
8525
8526 if (hexstr2bin(pos, buf, len) < 0) {
8527 os_free(buf);
8528 return -1;
8529 }
8530
8531 if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
8532 os_free(buf);
8533 return -1;
8534 }
8535
af041f99 8536 res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
86bd36f0 8537 os_free(buf);
af041f99 8538 return res;
86bd36f0
JM
8539}
8540
8541
f4b8bfae
AK
8542static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
8543{
8544 struct wpa_supplicant *wpa_s = ctx;
cf667c66
IP
8545 size_t len;
8546 const u8 *data;
f4b8bfae 8547
cf667c66
IP
8548 /*
8549 * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
8550 * BSSID[6]
8551 * BSSID Information[4]
8552 * Operating Class[1]
8553 * Channel Number[1]
8554 * PHY Type[1]
8555 * Optional Subelements[variable]
8556 */
8557#define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
8558
8559 if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
f4b8bfae 8560 wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
cf667c66
IP
8561 goto out;
8562 }
8563
8564 data = wpabuf_head_u8(neighbor_rep);
8565 len = wpabuf_len(neighbor_rep);
8566
8567 while (len >= 2 + NR_IE_MIN_LEN) {
8568 const u8 *nr;
8569 char lci[256 * 2 + 1];
8570 char civic[256 * 2 + 1];
8571 u8 nr_len = data[1];
8572 const u8 *pos = data, *end;
8573
8574 if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
8575 nr_len < NR_IE_MIN_LEN) {
8576 wpa_printf(MSG_DEBUG,
8577 "CTRL: Invalid Neighbor Report element: id=%u len=%u",
8578 data[0], nr_len);
8579 goto out;
8580 }
8581
8582 if (2U + nr_len > len) {
8583 wpa_printf(MSG_DEBUG,
8584 "CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
8585 data[0], len, nr_len);
8586 goto out;
8587 }
8588 pos += 2;
8589 end = pos + nr_len;
8590
8591 nr = pos;
8592 pos += NR_IE_MIN_LEN;
8593
8594 lci[0] = '\0';
8595 civic[0] = '\0';
8596 while (end - pos > 2) {
8597 u8 s_id, s_len;
8598
8599 s_id = *pos++;
8600 s_len = *pos++;
8601 if (s_len > end - pos)
8602 goto out;
8603 if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
8604 /* Measurement Token[1] */
8605 /* Measurement Report Mode[1] */
8606 /* Measurement Type[1] */
8607 /* Measurement Report[variable] */
8608 switch (pos[2]) {
8609 case MEASURE_TYPE_LCI:
8610 if (lci[0])
8611 break;
8612 wpa_snprintf_hex(lci, sizeof(lci),
8613 pos, s_len);
8614 break;
8615 case MEASURE_TYPE_LOCATION_CIVIC:
8616 if (civic[0])
8617 break;
8618 wpa_snprintf_hex(civic, sizeof(civic),
8619 pos, s_len);
8620 break;
8621 }
8622 }
8623
8624 pos += s_len;
8625 }
8626
8627 wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
8628 "bssid=" MACSTR
8629 " info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
8630 MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
8631 nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
8632 nr[ETH_ALEN + 6],
8633 lci[0] ? " lci=" : "", lci,
8634 civic[0] ? " civic=" : "", civic);
8635
8636 data = end;
8637 len -= 2 + nr_len;
f4b8bfae 8638 }
cf667c66
IP
8639
8640out:
8641 wpabuf_free(neighbor_rep);
f4b8bfae
AK
8642}
8643
8644
6a4f0ed7
JM
8645static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
8646 char *cmd)
f4b8bfae 8647{
d41a5352
DS
8648 struct wpa_ssid_value ssid, *ssid_p = NULL;
8649 int ret, lci = 0, civic = 0;
8650 char *ssid_s;
4c4b2305 8651
d41a5352
DS
8652 ssid_s = os_strstr(cmd, "ssid=");
8653 if (ssid_s) {
8654 if (ssid_parse(ssid_s + 5, &ssid)) {
8655 wpa_printf(MSG_ERROR,
8656 "CTRL: Send Neighbor Report: bad SSID");
4c4b2305 8657 return -1;
d41a5352
DS
8658 }
8659
4c4b2305 8660 ssid_p = &ssid;
d41a5352
DS
8661
8662 /*
8663 * Move cmd after the SSID text that may include "lci" or
8664 * "civic".
8665 */
8666 cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
8667 if (cmd)
8668 cmd++;
8669
4c4b2305
AK
8670 }
8671
d41a5352
DS
8672 if (cmd && os_strstr(cmd, "lci"))
8673 lci = 1;
8674
8675 if (cmd && os_strstr(cmd, "civic"))
8676 civic = 1;
8677
8678 ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
4c4b2305
AK
8679 wpas_ctrl_neighbor_rep_cb,
8680 wpa_s);
8681
8682 return ret;
f4b8bfae
AK
8683}
8684
8685
65d9a5e2
JM
8686static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
8687{
8688 eapol_sm_erp_flush(wpa_s->eapol);
8689 return 0;
8690}
8691
8692
fb375883
IP
8693static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
8694 char *cmd)
8695{
8696 char *token, *context = NULL;
8697 unsigned int enable = ~0, type = 0;
8698 u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
8699 u8 *addr = NULL, *mask = NULL;
8700
8701 while ((token = str_token(cmd, " ", &context))) {
8702 if (os_strcasecmp(token, "scan") == 0) {
8703 type |= MAC_ADDR_RAND_SCAN;
8704 } else if (os_strcasecmp(token, "sched") == 0) {
8705 type |= MAC_ADDR_RAND_SCHED_SCAN;
8706 } else if (os_strcasecmp(token, "pno") == 0) {
8707 type |= MAC_ADDR_RAND_PNO;
8708 } else if (os_strcasecmp(token, "all") == 0) {
8709 type = wpa_s->mac_addr_rand_supported;
8710 } else if (os_strncasecmp(token, "enable=", 7) == 0) {
8711 enable = atoi(token + 7);
8712 } else if (os_strncasecmp(token, "addr=", 5) == 0) {
8713 addr = _addr;
8714 if (hwaddr_aton(token + 5, addr)) {
8715 wpa_printf(MSG_INFO,
8716 "CTRL: Invalid MAC address: %s",
8717 token);
8718 return -1;
8719 }
8720 } else if (os_strncasecmp(token, "mask=", 5) == 0) {
8721 mask = _mask;
8722 if (hwaddr_aton(token + 5, mask)) {
8723 wpa_printf(MSG_INFO,
8724 "CTRL: Invalid MAC address mask: %s",
8725 token);
8726 return -1;
8727 }
8728 } else {
8729 wpa_printf(MSG_INFO,
8730 "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
8731 token);
8732 return -1;
8733 }
8734 }
8735
8736 if (!type) {
8737 wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
8738 return -1;
8739 }
8740
8741 if ((wpa_s->mac_addr_rand_supported & type) != type) {
8742 wpa_printf(MSG_INFO,
8743 "CTRL: MAC_RAND_SCAN types=%u != supported=%u",
8744 type, wpa_s->mac_addr_rand_supported);
8745 return -1;
8746 }
8747
8748 if (enable > 1) {
8749 wpa_printf(MSG_INFO,
8750 "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
8751 return -1;
8752 }
8753
8754 if (!enable) {
8755 wpas_mac_addr_rand_scan_clear(wpa_s, type);
8756 if (wpa_s->pno) {
8757 if (type & MAC_ADDR_RAND_PNO) {
8758 wpas_stop_pno(wpa_s);
8759 wpas_start_pno(wpa_s);
8760 }
8761 } else if (wpa_s->sched_scanning &&
8762 (type & MAC_ADDR_RAND_SCHED_SCAN)) {
5bb7327a 8763 wpas_scan_restart_sched_scan(wpa_s);
fb375883
IP
8764 }
8765 return 0;
8766 }
8767
8768 if ((addr && !mask) || (!addr && mask)) {
8769 wpa_printf(MSG_INFO,
8770 "CTRL: MAC_RAND_SCAN invalid addr/mask combination");
8771 return -1;
8772 }
8773
8774 if (addr && mask && (!(mask[0] & 0x01) || (addr[0] & 0x01))) {
8775 wpa_printf(MSG_INFO,
8776 "CTRL: MAC_RAND_SCAN cannot allow multicast address");
8777 return -1;
8778 }
8779
8780 if (type & MAC_ADDR_RAND_SCAN) {
8781 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCAN,
8782 addr, mask);
8783 }
8784
8785 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
8786 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCHED_SCAN,
8787 addr, mask);
8788
5bb7327a
JM
8789 if (wpa_s->sched_scanning && !wpa_s->pno)
8790 wpas_scan_restart_sched_scan(wpa_s);
fb375883
IP
8791 }
8792
8793 if (type & MAC_ADDR_RAND_PNO) {
8794 wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_PNO,
8795 addr, mask);
8796 if (wpa_s->pno) {
8797 wpas_stop_pno(wpa_s);
8798 wpas_start_pno(wpa_s);
8799 }
8800 }
8801
8802 return 0;
8803}
8804
8805
b8daac18
MH
8806static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
8807 char *buf, size_t buflen)
8808{
8809 size_t reply_len;
8810
8811 reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
8812#ifdef CONFIG_AP
8813 reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
8814 buflen - reply_len);
8815#endif /* CONFIG_AP */
8816 return reply_len;
8817}
8818
8819
4c522c77
MH
8820static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
8821{
8822 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
8823#ifdef CONFIG_AP
8824 wpas_ap_pmksa_cache_flush(wpa_s);
8825#endif /* CONFIG_AP */
8826}
8827
8828
3459381d
JM
8829#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
8830
8831static int wpas_ctrl_iface_pmksa_get(struct wpa_supplicant *wpa_s,
8832 const char *cmd, char *buf, size_t buflen)
8833{
8834 struct rsn_pmksa_cache_entry *entry;
8835 struct wpa_ssid *ssid;
8836 char *pos, *pos2, *end;
8837 int ret;
8838 struct os_reltime now;
8839
8840 ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
8841 if (!ssid)
8842 return -1;
8843
8844 pos = buf;
8845 end = buf + buflen;
8846
8847 os_get_reltime(&now);
8848
8849 /*
8850 * Entry format:
8851 * <BSSID> <PMKID> <PMK> <reauth_time in seconds>
8852 * <expiration in seconds> <akmp> <opportunistic>
8853 */
8854
8855 for (entry = wpa_sm_pmksa_cache_head(wpa_s->wpa); entry;
8856 entry = entry->next) {
8857 if (entry->network_ctx != ssid)
8858 continue;
8859
8860 pos2 = pos;
8861 ret = os_snprintf(pos2, end - pos2, MACSTR " ",
8862 MAC2STR(entry->aa));
8863 if (os_snprintf_error(end - pos2, ret))
8864 break;
8865 pos2 += ret;
8866
8867 pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmkid,
8868 PMKID_LEN);
8869
8870 ret = os_snprintf(pos2, end - pos2, " ");
8871 if (os_snprintf_error(end - pos2, ret))
8872 break;
8873 pos2 += ret;
8874
8875 pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmk,
8876 entry->pmk_len);
8877
8878 ret = os_snprintf(pos2, end - pos2, " %d %d %d %d",
8879 (int) (entry->reauth_time - now.sec),
8880 (int) (entry->expiration - now.sec),
8881 entry->akmp,
8882 entry->opportunistic);
8883 if (os_snprintf_error(end - pos2, ret))
8884 break;
8885 pos2 += ret;
8886
8887 ret = os_snprintf(pos2, end - pos2, "\n");
8888 if (os_snprintf_error(end - pos2, ret))
8889 break;
8890 pos2 += ret;
8891
8892 pos = pos2;
8893 }
8894
8895 return pos - buf;
8896}
8897
8898
8899static int wpas_ctrl_iface_pmksa_add(struct wpa_supplicant *wpa_s,
8900 char *cmd)
8901{
8902 struct rsn_pmksa_cache_entry *entry;
8903 struct wpa_ssid *ssid;
8904 char *pos, *pos2;
8905 int ret = -1;
8906 struct os_reltime now;
8907 int reauth_time = 0, expiration = 0;
8908
8909 /*
8910 * Entry format:
8911 * <network_id> <BSSID> <PMKID> <PMK> <reauth_time in seconds>
8912 * <expiration in seconds> <akmp> <opportunistic>
8913 */
8914
8915 ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
8916 if (!ssid)
8917 return -1;
8918
8919 pos = os_strchr(cmd, ' ');
8920 if (!pos)
8921 return -1;
8922 pos++;
8923
8924 entry = os_zalloc(sizeof(*entry));
8925 if (!entry)
8926 return -1;
8927
8928 if (hwaddr_aton(pos, entry->aa))
8929 goto fail;
8930
8931 pos = os_strchr(pos, ' ');
8932 if (!pos)
8933 goto fail;
8934 pos++;
8935
8936 if (hexstr2bin(pos, entry->pmkid, PMKID_LEN) < 0)
8937 goto fail;
8938
8939 pos = os_strchr(pos, ' ');
8940 if (!pos)
8941 goto fail;
8942 pos++;
8943
8944 pos2 = os_strchr(pos, ' ');
8945 if (!pos2)
8946 goto fail;
8947 entry->pmk_len = (pos2 - pos) / 2;
8948 if (entry->pmk_len < PMK_LEN || entry->pmk_len > PMK_LEN_MAX ||
8949 hexstr2bin(pos, entry->pmk, entry->pmk_len) < 0)
8950 goto fail;
8951
8952 pos = os_strchr(pos, ' ');
8953 if (!pos)
8954 goto fail;
8955 pos++;
8956
8957 if (sscanf(pos, "%d %d %d %d", &reauth_time, &expiration,
8958 &entry->akmp, &entry->opportunistic) != 4)
8959 goto fail;
8960 os_get_reltime(&now);
8961 entry->expiration = now.sec + expiration;
8962 entry->reauth_time = now.sec + reauth_time;
8963
8964 entry->network_ctx = ssid;
8965
8966 wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
8967 entry = NULL;
8968 ret = 0;
8969fail:
8970 os_free(entry);
8971 return ret;
8972}
8973
8974#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
8975
8976
8db9a79d
JM
8977static int wpas_ctrl_cmd_debug_level(const char *cmd)
8978{
8979 if (os_strcmp(cmd, "PING") == 0 ||
8980 os_strncmp(cmd, "BSS ", 4) == 0 ||
8981 os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
8982 os_strncmp(cmd, "STATUS", 6) == 0 ||
8983 os_strncmp(cmd, "STA ", 4) == 0 ||
8984 os_strncmp(cmd, "STA-", 4) == 0)
8985 return MSG_EXCESSIVE;
8986 return MSG_DEBUG;
8987}
8988
8989
6fc6879b
JM
8990char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
8991 char *buf, size_t *resp_len)
8992{
8993 char *reply;
b563b388 8994 const int reply_size = 4096;
6fc6879b
JM
8995 int reply_len;
8996
8997 if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
d31b5ac7
JM
8998 os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
8999 if (wpa_debug_show_keys)
9000 wpa_dbg(wpa_s, MSG_DEBUG,
9001 "Control interface command '%s'", buf);
9002 else
9003 wpa_dbg(wpa_s, MSG_DEBUG,
9004 "Control interface command '%s [REMOVED]'",
9005 os_strncmp(buf, WPA_CTRL_RSP,
9006 os_strlen(WPA_CTRL_RSP)) == 0 ?
9007 WPA_CTRL_RSP : "SET_NETWORK");
9008 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
679f2e7c 9009 os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
6fc6879b
JM
9010 wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
9011 (const u8 *) buf, os_strlen(buf));
9012 } else {
8db9a79d 9013 int level = wpas_ctrl_cmd_debug_level(buf);
b470b2bf 9014 wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
6fc6879b
JM
9015 }
9016
9017 reply = os_malloc(reply_size);
9018 if (reply == NULL) {
9019 *resp_len = 1;
9020 return NULL;
9021 }
9022
9023 os_memcpy(reply, "OK\n", 3);
9024 reply_len = 3;
9025
9026 if (os_strcmp(buf, "PING") == 0) {
9027 os_memcpy(reply, "PONG\n", 5);
9028 reply_len = 5;
0eed2a8d
JD
9029 } else if (os_strcmp(buf, "IFNAME") == 0) {
9030 reply_len = os_strlen(wpa_s->ifname);
9031 os_memcpy(reply, wpa_s->ifname, reply_len);
ac6912b5
BG
9032 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
9033 if (wpa_debug_reopen_file() < 0)
9034 reply_len = -1;
77895cd9
JM
9035 } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
9036 wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
6fc6879b
JM
9037 } else if (os_strcmp(buf, "MIB") == 0) {
9038 reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
9039 if (reply_len >= 0) {
5ac73acf
JM
9040 reply_len += eapol_sm_get_mib(wpa_s->eapol,
9041 reply + reply_len,
9042 reply_size - reply_len);
6fc6879b
JM
9043 }
9044 } else if (os_strncmp(buf, "STATUS", 6) == 0) {
9045 reply_len = wpa_supplicant_ctrl_iface_status(
9046 wpa_s, buf + 6, reply, reply_size);
9047 } else if (os_strcmp(buf, "PMKSA") == 0) {
b8daac18 9048 reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
79e2b1cc 9049 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
4c522c77 9050 wpas_ctrl_iface_pmksa_flush(wpa_s);
3459381d
JM
9051#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
9052 } else if (os_strncmp(buf, "PMKSA_GET ", 10) == 0) {
9053 reply_len = wpas_ctrl_iface_pmksa_get(wpa_s, buf + 10,
9054 reply, reply_size);
9055 } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
9056 if (wpas_ctrl_iface_pmksa_add(wpa_s, buf + 10) < 0)
9057 reply_len = -1;
9058#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
6fc6879b
JM
9059 } else if (os_strncmp(buf, "SET ", 4) == 0) {
9060 if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
9061 reply_len = -1;
10263dc2
OO
9062 } else if (os_strncmp(buf, "DUMP", 4) == 0) {
9063 reply_len = wpa_config_dump_values(wpa_s->conf,
9064 reply, reply_size);
acec8d32
JM
9065 } else if (os_strncmp(buf, "GET ", 4) == 0) {
9066 reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
9067 reply, reply_size);
6fc6879b
JM
9068 } else if (os_strcmp(buf, "LOGON") == 0) {
9069 eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
9070 } else if (os_strcmp(buf, "LOGOFF") == 0) {
9071 eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
9072 } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
8401a6b0
JM
9073 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
9074 reply_len = -1;
9796a86c
JM
9075 else
9076 wpas_request_connection(wpa_s);
0f44ec8e
PQ
9077 } else if (os_strcmp(buf, "REATTACH") == 0) {
9078 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
9079 !wpa_s->current_ssid)
9080 reply_len = -1;
9081 else {
9082 wpa_s->reattach = 1;
9083 wpas_request_connection(wpa_s);
9084 }
6fc6879b 9085 } else if (os_strcmp(buf, "RECONNECT") == 0) {
8401a6b0
JM
9086 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
9087 reply_len = -1;
9796a86c
JM
9088 else if (wpa_s->disconnected)
9089 wpas_request_connection(wpa_s);
ec717917 9090#ifdef IEEE8021X_EAPOL
6fc6879b
JM
9091 } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
9092 if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
9093 reply_len = -1;
ec717917 9094#endif /* IEEE8021X_EAPOL */
6fc6879b
JM
9095#ifdef CONFIG_PEERKEY
9096 } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
9097 if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
9098 reply_len = -1;
9099#endif /* CONFIG_PEERKEY */
9100#ifdef CONFIG_IEEE80211R
9101 } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
9102 if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
9103 reply_len = -1;
9104#endif /* CONFIG_IEEE80211R */
fcc60db4
JM
9105#ifdef CONFIG_WPS
9106 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
3152ff42
CWY
9107 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
9108 if (res == -2) {
9109 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
9110 reply_len = 17;
9111 } else if (res)
fcc60db4
JM
9112 reply_len = -1;
9113 } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
3152ff42
CWY
9114 int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
9115 if (res == -2) {
9116 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
9117 reply_len = 17;
9118 } else if (res)
fcc60db4
JM
9119 reply_len = -1;
9120 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
9121 reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
9122 reply,
9123 reply_size);
3981cb3c
JM
9124 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
9125 reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
9126 wpa_s, buf + 14, reply, reply_size);
2f9929ff
AC
9127 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
9128 if (wpas_wps_cancel(wpa_s))
9129 reply_len = -1;
71892384 9130#ifdef CONFIG_WPS_NFC
3f2c8ba6
JM
9131 } else if (os_strcmp(buf, "WPS_NFC") == 0) {
9132 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
9133 reply_len = -1;
9134 } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
9135 if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
9136 reply_len = -1;
bbf41865
JM
9137 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
9138 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
9139 wpa_s, buf + 21, reply, reply_size);
3f2c8ba6
JM
9140 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
9141 reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
9142 wpa_s, buf + 14, reply, reply_size);
d7645d23
JM
9143 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
9144 if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
9145 buf + 17))
9146 reply_len = -1;
e65552dd
JM
9147 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
9148 reply_len = wpas_ctrl_nfc_get_handover_req(
9149 wpa_s, buf + 21, reply, reply_size);
9150 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
9151 reply_len = wpas_ctrl_nfc_get_handover_sel(
9152 wpa_s, buf + 21, reply, reply_size);
e4758827
JM
9153 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
9154 if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
9155 reply_len = -1;
71892384 9156#endif /* CONFIG_WPS_NFC */
fcc60db4
JM
9157 } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
9158 if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
9159 reply_len = -1;
70d84f11
JM
9160#ifdef CONFIG_AP
9161 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
9162 reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
9163 wpa_s, buf + 11, reply, reply_size);
9164#endif /* CONFIG_AP */
72df2f5f 9165#ifdef CONFIG_WPS_ER
e9bcfebf 9166 } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
08486685
JM
9167 if (wpas_wps_er_start(wpa_s, NULL))
9168 reply_len = -1;
9169 } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
9170 if (wpas_wps_er_start(wpa_s, buf + 13))
e9bcfebf
JM
9171 reply_len = -1;
9172 } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
f77cedc1 9173 wpas_wps_er_stop(wpa_s);
72df2f5f
JM
9174 } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
9175 if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
9176 reply_len = -1;
564cd7fa 9177 } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
ed159ad4
JM
9178 int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
9179 if (ret == -2) {
9180 os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
9181 reply_len = 17;
9182 } else if (ret == -3) {
9183 os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
9184 reply_len = 18;
9185 } else if (ret == -4) {
9186 os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
9187 reply_len = 20;
9188 } else if (ret)
564cd7fa 9189 reply_len = -1;
e64dcfd5
JM
9190 } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
9191 if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
9192 reply_len = -1;
ef10f473
JM
9193 } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
9194 if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
9195 buf + 18))
9196 reply_len = -1;
7d6640a6
JM
9197 } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
9198 if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
9199 reply_len = -1;
1cea09a9
JM
9200#ifdef CONFIG_WPS_NFC
9201 } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
9202 reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
9203 wpa_s, buf + 24, reply, reply_size);
9204#endif /* CONFIG_WPS_NFC */
72df2f5f 9205#endif /* CONFIG_WPS_ER */
fcc60db4 9206#endif /* CONFIG_WPS */
11ef8d35
JM
9207#ifdef CONFIG_IBSS_RSN
9208 } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
9209 if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
9210 reply_len = -1;
9211#endif /* CONFIG_IBSS_RSN */
603a3f34 9212#ifdef CONFIG_MESH
5b78493f
MH
9213 } else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
9214 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
9215 wpa_s, buf + 19, reply, reply_size);
9216 } else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
9217 reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
9218 wpa_s, "", reply, reply_size);
603a3f34
JL
9219 } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
9220 if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
9221 reply_len = -1;
9222 } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
9223 if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
9224 buf + 18))
9225 reply_len = -1;
e174ef34
MH
9226 } else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
9227 if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
9228 reply_len = -1;
2604edbf
MH
9229 } else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
9230 if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
9231 reply_len = -1;
603a3f34 9232#endif /* CONFIG_MESH */
b563b388
JM
9233#ifdef CONFIG_P2P
9234 } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
51775096 9235 if (p2p_ctrl_find(wpa_s, buf + 8))
b563b388
JM
9236 reply_len = -1;
9237 } else if (os_strcmp(buf, "P2P_FIND") == 0) {
9238 if (p2p_ctrl_find(wpa_s, ""))
9239 reply_len = -1;
9240 } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
9241 wpas_p2p_stop_find(wpa_s);
f309c18e
KV
9242 } else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
9243 if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
9244 reply_len = -1;
9245 } else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
9246 if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
9247 reply_len = -1;
b563b388
JM
9248 } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
9249 reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
9250 reply_size);
9251 } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
9252 if (p2p_ctrl_listen(wpa_s, buf + 11))
9253 reply_len = -1;
9254 } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
9255 if (p2p_ctrl_listen(wpa_s, ""))
9256 reply_len = -1;
9257 } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
9258 if (wpas_p2p_group_remove(wpa_s, buf + 17))
9259 reply_len = -1;
9260 } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
38dcc86c 9261 if (p2p_ctrl_group_add(wpa_s, ""))
b563b388
JM
9262 reply_len = -1;
9263 } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
9264 if (p2p_ctrl_group_add(wpa_s, buf + 14))
9265 reply_len = -1;
57b38882
PK
9266 } else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
9267 reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
9268 reply_size);
b563b388
JM
9269 } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
9270 if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
9271 reply_len = -1;
9272 } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
9273 reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
9274 } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
9275 reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
9276 reply_size);
9277 } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
9278 if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
9279 reply_len = -1;
9280 } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
9281 if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
9282 reply_len = -1;
9283 } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
9284 wpas_p2p_sd_service_update(wpa_s);
9285 } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
9286 if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
9287 reply_len = -1;
9288 } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
9289 wpas_p2p_service_flush(wpa_s);
9290 } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
9291 if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
9292 reply_len = -1;
9293 } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
9294 if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
9295 reply_len = -1;
ae9d45f3
KV
9296 } else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
9297 if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
9298 reply_len = -1;
b563b388
JM
9299 } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
9300 if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
9301 reply_len = -1;
9302 } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
9303 if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
9304 reply_len = -1;
9305 } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
9306 reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
9307 reply_size);
9308 } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
9309 if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
9310 reply_len = -1;
9311 } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
acb54643 9312 p2p_ctrl_flush(wpa_s);
9d562b79
SS
9313 } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
9314 if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
9315 reply_len = -1;
59eba7a2
JM
9316 } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
9317 if (wpas_p2p_cancel(wpa_s))
9318 reply_len = -1;
b563b388
JM
9319 } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
9320 if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
9321 reply_len = -1;
9322 } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
9323 if (p2p_ctrl_presence_req(wpa_s, "") < 0)
9324 reply_len = -1;
9325 } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
9326 if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
9327 reply_len = -1;
9328 } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
9329 if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
9330 reply_len = -1;
f2c56602
JM
9331 } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
9332 if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
9333 reply_len = -1;
a6f5b193
PX
9334 } else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
9335 if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
9336 reply_len = -1;
9337 } else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
9338 if (wpas_p2p_lo_stop(wpa_s))
9339 reply_len = -1;
b563b388 9340#endif /* CONFIG_P2P */
9675ce35
JM
9341#ifdef CONFIG_WIFI_DISPLAY
9342 } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
9343 if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
9344 reply_len = -1;
9345 } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
9346 reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
9347 reply, reply_size);
9348#endif /* CONFIG_WIFI_DISPLAY */
afc064fe
JM
9349#ifdef CONFIG_INTERWORKING
9350 } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
9351 if (interworking_fetch_anqp(wpa_s) < 0)
9352 reply_len = -1;
9353 } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
9354 interworking_stop_fetch_anqp(wpa_s);
356d1488
JM
9355 } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
9356 if (ctrl_interworking_select(wpa_s, NULL) < 0)
9357 reply_len = -1;
9358 } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
9359 if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
b02fe7ff
JM
9360 reply_len = -1;
9361 } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
f91a512f 9362 if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
b02fe7ff 9363 reply_len = -1;
f91a512f
JM
9364 } else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
9365 int id;
9366
9367 id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
9368 if (id < 0)
9369 reply_len = -1;
9370 else {
9371 reply_len = os_snprintf(reply, reply_size, "%d\n", id);
9372 if (os_snprintf_error(reply_size, reply_len))
9373 reply_len = -1;
9374 }
afc064fe
JM
9375 } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
9376 if (get_anqp(wpa_s, buf + 9) < 0)
9377 reply_len = -1;
b1f12296
JM
9378 } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
9379 if (gas_request(wpa_s, buf + 12) < 0)
9380 reply_len = -1;
9381 } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
9382 reply_len = gas_response_get(wpa_s, buf + 17, reply,
9383 reply_size);
afc064fe 9384#endif /* CONFIG_INTERWORKING */
a8918e86
JK
9385#ifdef CONFIG_HS20
9386 } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
9387 if (get_hs20_anqp(wpa_s, buf + 14) < 0)
9388 reply_len = -1;
9389 } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
9390 if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
9391 reply_len = -1;
184e110c 9392 } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
8dd5c1b4
JN
9393 if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
9394 reply_len = -1;
9395 } else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
9396 if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
9397 reply_len = -1;
9398 } else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
9399 reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
9400 } else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
9401 if (del_hs20_icon(wpa_s, buf + 14) < 0)
184e110c 9402 reply_len = -1;
b572df86 9403 } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
dd20eabd
JM
9404 if (hs20_fetch_osu(wpa_s, 0) < 0)
9405 reply_len = -1;
9406 } else if (os_strcmp(buf, "FETCH_OSU no-scan") == 0) {
9407 if (hs20_fetch_osu(wpa_s, 1) < 0)
b572df86
JM
9408 reply_len = -1;
9409 } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
9410 hs20_cancel_fetch_osu(wpa_s);
a8918e86 9411#endif /* CONFIG_HS20 */
6fc6879b
JM
9412 } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
9413 {
9414 if (wpa_supplicant_ctrl_iface_ctrl_rsp(
9415 wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
9416 reply_len = -1;
bceb8431
JM
9417 else {
9418 /*
9419 * Notify response from timeout to allow the control
9420 * interface response to be sent first.
9421 */
9422 eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
9423 wpa_s, NULL);
9424 }
6fc6879b
JM
9425 } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
9426 if (wpa_supplicant_reload_configuration(wpa_s))
9427 reply_len = -1;
9428 } else if (os_strcmp(buf, "TERMINATE") == 0) {
1a1bf008 9429 wpa_supplicant_terminate_proc(wpa_s->global);
6fc6879b
JM
9430 } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
9431 if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
9432 reply_len = -1;
9aa10e2b
DS
9433 } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
9434 reply_len = wpa_supplicant_ctrl_iface_blacklist(
9435 wpa_s, buf + 9, reply, reply_size);
0597a5b5
DS
9436 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
9437 reply_len = wpa_supplicant_ctrl_iface_log_level(
9438 wpa_s, buf + 9, reply, reply_size);
90903a77
VD
9439 } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
9440 reply_len = wpa_supplicant_ctrl_iface_list_networks(
9441 wpa_s, buf + 14, reply, reply_size);
6fc6879b
JM
9442 } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
9443 reply_len = wpa_supplicant_ctrl_iface_list_networks(
90903a77 9444 wpa_s, NULL, reply, reply_size);
6fc6879b 9445 } else if (os_strcmp(buf, "DISCONNECT") == 0) {
5f040be4 9446 wpas_request_disconnection(wpa_s);
fee52342
JM
9447 } else if (os_strcmp(buf, "SCAN") == 0) {
9448 wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
9449 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
9450 wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
6fc6879b
JM
9451 } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
9452 reply_len = wpa_supplicant_ctrl_iface_scan_results(
9453 wpa_s, reply, reply_size);
2ea2166d
JM
9454 } else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
9455 if (wpas_abort_ongoing_scan(wpa_s) < 0)
9456 reply_len = -1;
6fc6879b
JM
9457 } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
9458 if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
9459 reply_len = -1;
9460 } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
9461 if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
9462 reply_len = -1;
9463 } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
9464 if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
9465 reply_len = -1;
9466 } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
9467 reply_len = wpa_supplicant_ctrl_iface_add_network(
9468 wpa_s, reply, reply_size);
9469 } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
9470 if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
9471 reply_len = -1;
9472 } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
9473 if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
9474 reply_len = -1;
9475 } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
9476 reply_len = wpa_supplicant_ctrl_iface_get_network(
9477 wpa_s, buf + 12, reply, reply_size);
1c330a2f 9478 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
daae4995
AN
9479 if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
9480 wpa_s))
1c330a2f 9481 reply_len = -1;
d94c9ee6
JM
9482 } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
9483 reply_len = wpa_supplicant_ctrl_iface_list_creds(
9484 wpa_s, reply, reply_size);
9485 } else if (os_strcmp(buf, "ADD_CRED") == 0) {
9486 reply_len = wpa_supplicant_ctrl_iface_add_cred(
9487 wpa_s, reply, reply_size);
9488 } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
9489 if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
9490 reply_len = -1;
9491 } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
9492 if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
9493 reply_len = -1;
c880ab87
JM
9494 } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
9495 reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
9496 reply,
9497 reply_size);
6fc6879b
JM
9498#ifndef CONFIG_NO_CONFIG_WRITE
9499 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
9500 if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
9501 reply_len = -1;
9502#endif /* CONFIG_NO_CONFIG_WRITE */
9503 } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
9504 reply_len = wpa_supplicant_ctrl_iface_get_capability(
9505 wpa_s, buf + 15, reply, reply_size);
9506 } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
9507 if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
9508 reply_len = -1;
67b9bd08
DS
9509 } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
9510 if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
9511 reply_len = -1;
4b4a8ae5
JM
9512 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
9513 reply_len = wpa_supplicant_global_iface_list(
9514 wpa_s->global, reply, reply_size);
56e2fc2c 9515 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
6fc6879b 9516 reply_len = wpa_supplicant_global_iface_interfaces(
56e2fc2c 9517 wpa_s->global, buf + 10, reply, reply_size);
6fc6879b
JM
9518 } else if (os_strncmp(buf, "BSS ", 4) == 0) {
9519 reply_len = wpa_supplicant_ctrl_iface_bss(
9520 wpa_s, buf + 4, reply, reply_size);
e653b622
JM
9521#ifdef CONFIG_AP
9522 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
9523 reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
9524 } else if (os_strncmp(buf, "STA ", 4) == 0) {
9525 reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
9526 reply_size);
9527 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
9528 reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
9529 reply_size);
e60b2951
JJ
9530 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
9531 if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
9532 reply_len = -1;
9533 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
9534 if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
9535 reply_len = -1;
334bf36a
AO
9536 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
9537 if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
9538 reply_len = -1;
99650cad
JM
9539 } else if (os_strcmp(buf, "STOP_AP") == 0) {
9540 if (wpas_ap_stop_ap(wpa_s))
9541 reply_len = -1;
e653b622 9542#endif /* CONFIG_AP */
207ef3fb
JM
9543 } else if (os_strcmp(buf, "SUSPEND") == 0) {
9544 wpas_notify_suspend(wpa_s->global);
9545 } else if (os_strcmp(buf, "RESUME") == 0) {
9546 wpas_notify_resume(wpa_s->global);
9ff4de6d 9547#ifdef CONFIG_TESTING_OPTIONS
32d5295f
JM
9548 } else if (os_strcmp(buf, "DROP_SA") == 0) {
9549 wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
9ff4de6d 9550#endif /* CONFIG_TESTING_OPTIONS */
86d4f806
JM
9551 } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
9552 if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
9553 reply_len = -1;
0d0a8ca1 9554 } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
5407c69d 9555 wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
78633c37
SL
9556 } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
9557 if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
9558 reply_len = -1;
9559 } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
9560 if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
9561 buf + 17))
9562 reply_len = -1;
39ee845f 9563 } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
a1144000 9564 wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
281ff0aa
GP
9565#ifdef CONFIG_TDLS
9566 } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
9567 if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
9568 reply_len = -1;
9569 } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
9570 if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
9571 reply_len = -1;
9572 } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
9573 if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
9574 reply_len = -1;
6b90deae
AN
9575 } else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
9576 if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
9577 buf + 17))
9578 reply_len = -1;
9579 } else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
9580 if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
9581 buf + 24))
9582 reply_len = -1;
4504621f
OG
9583 } else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
9584 reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
9585 wpa_s, buf + 17, reply, reply_size);
281ff0aa 9586#endif /* CONFIG_TDLS */
8506ea6f
MB
9587 } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
9588 reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
eb2f2088
MB
9589 } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
9590 if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
9591 reply_len = -1;
9592 } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
9593 if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
9594 reply_len = -1;
60b24b0d
DS
9595 } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
9596 reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
9597 reply_size);
96e8d831
DS
9598 } else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
9599 if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
9600 reply_len = -1;
dc7785f8
YZ
9601 } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
9602 reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
9603 reply_size);
bc5d330a
TB
9604#ifdef CONFIG_AUTOSCAN
9605 } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
9606 if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
9607 reply_len = -1;
9608#endif /* CONFIG_AUTOSCAN */
4d7aab78
EL
9609 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
9610 reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
9611 reply_size);
5e2c3490
JM
9612#ifdef ANDROID
9613 } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
9614 reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
9615 reply_size);
9616#endif /* ANDROID */
adef8948
BL
9617 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
9618 reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
9619 reply_size);
9482426e 9620 } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
f5f37d3a 9621 pmksa_cache_clear_current(wpa_s->wpa);
9482426e 9622 eapol_sm_request_reauth(wpa_s->eapol);
e9199e31
JM
9623#ifdef CONFIG_WNM
9624 } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
9625 if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
9626 reply_len = -1;
07565ab0
MG
9627 } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
9628 if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
65bcd0a9 9629 reply_len = -1;
e9199e31 9630#endif /* CONFIG_WNM */
acb54643
JM
9631 } else if (os_strcmp(buf, "FLUSH") == 0) {
9632 wpa_supplicant_ctrl_iface_flush(wpa_s);
1f965e62
JM
9633 } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
9634 reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
9635 reply_size);
60b893df
JM
9636#ifdef CONFIG_TESTING_OPTIONS
9637 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
9638 if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
9639 reply_len = -1;
9640 } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
9641 wpas_ctrl_iface_mgmt_tx_done(wpa_s);
4de70e23
JM
9642 } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
9643 if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
9644 reply_len = -1;
ad12f2f4
JM
9645 } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
9646 if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
9647 reply_len = -1;
9d4ff04a
JM
9648 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
9649 if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
9650 reply_len = -1;
4a6cc862
JM
9651 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
9652 if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
9653 reply_len = -1;
9654 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
9655 if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
9656 reply_len = -1;
fc0ef7c0
JM
9657 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
9658 if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
9659 reply_len = -1;
a156ffda
JM
9660 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
9661 if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
9662 reply_len = -1;
9663 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
9664 reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
2da52565
JM
9665 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
9666 if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0)
9667 reply_len = -1;
9668 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
9669 reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size);
a530fe77
JM
9670 } else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
9671 if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
9672 reply_len = -1;
651c6a84
JM
9673 } else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
9674 if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
9675 reply_len = -1;
60b893df 9676#endif /* CONFIG_TESTING_OPTIONS */
86bd36f0
JM
9677 } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
9678 if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
9679 reply_len = -1;
9680 } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
9681 reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
9682 reply_size);
9683 } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
9684 if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
9685 reply_len = -1;
f4b8bfae 9686 } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
6a4f0ed7 9687 if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
f4b8bfae 9688 reply_len = -1;
65d9a5e2
JM
9689 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
9690 wpas_ctrl_iface_erp_flush(wpa_s);
fb375883
IP
9691 } else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
9692 if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
9693 reply_len = -1;
98342208
AK
9694 } else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
9695 reply_len = wpas_ctrl_iface_get_pref_freq_list(
9696 wpa_s, buf + 19, reply, reply_size);
6fc6879b
JM
9697 } else {
9698 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
9699 reply_len = 16;
9700 }
9701
9702 if (reply_len < 0) {
9703 os_memcpy(reply, "FAIL\n", 5);
9704 reply_len = 5;
9705 }
9706
6fc6879b
JM
9707 *resp_len = reply_len;
9708 return reply;
9709}
9710
9711
9712static int wpa_supplicant_global_iface_add(struct wpa_global *global,
9713 char *cmd)
9714{
9715 struct wpa_interface iface;
efa232f9
JJ
9716 char *pos, *extra;
9717 struct wpa_supplicant *wpa_s;
9718 unsigned int create_iface = 0;
9719 u8 mac_addr[ETH_ALEN];
0f039e34 9720 enum wpa_driver_if_type type = WPA_IF_STATION;
6fc6879b
JM
9721
9722 /*
9723 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
0f039e34 9724 * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
6fc6879b
JM
9725 */
9726 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
9727
9728 os_memset(&iface, 0, sizeof(iface));
9729
9730 do {
9731 iface.ifname = pos = cmd;
9732 pos = os_strchr(pos, '\t');
9733 if (pos)
9734 *pos++ = '\0';
9735 if (iface.ifname[0] == '\0')
9736 return -1;
9737 if (pos == NULL)
9738 break;
9739
9740 iface.confname = pos;
9741 pos = os_strchr(pos, '\t');
9742 if (pos)
9743 *pos++ = '\0';
9744 if (iface.confname[0] == '\0')
9745 iface.confname = NULL;
9746 if (pos == NULL)
9747 break;
9748
9749 iface.driver = pos;
9750 pos = os_strchr(pos, '\t');
9751 if (pos)
9752 *pos++ = '\0';
9753 if (iface.driver[0] == '\0')
9754 iface.driver = NULL;
9755 if (pos == NULL)
9756 break;
9757
9758 iface.ctrl_interface = pos;
9759 pos = os_strchr(pos, '\t');
9760 if (pos)
9761 *pos++ = '\0';
9762 if (iface.ctrl_interface[0] == '\0')
9763 iface.ctrl_interface = NULL;
9764 if (pos == NULL)
9765 break;
9766
9767 iface.driver_param = pos;
9768 pos = os_strchr(pos, '\t');
9769 if (pos)
9770 *pos++ = '\0';
9771 if (iface.driver_param[0] == '\0')
9772 iface.driver_param = NULL;
9773 if (pos == NULL)
9774 break;
9775
9776 iface.bridge_ifname = pos;
9777 pos = os_strchr(pos, '\t');
9778 if (pos)
9779 *pos++ = '\0';
9780 if (iface.bridge_ifname[0] == '\0')
9781 iface.bridge_ifname = NULL;
9782 if (pos == NULL)
9783 break;
efa232f9
JJ
9784
9785 extra = pos;
9786 pos = os_strchr(pos, '\t');
9787 if (pos)
9788 *pos++ = '\0';
da3db681
BG
9789 if (!extra[0])
9790 break;
9791
0f039e34 9792 if (os_strcmp(extra, "create") == 0) {
efa232f9 9793 create_iface = 1;
0f039e34
AS
9794 if (!pos)
9795 break;
9796
9797 if (os_strcmp(pos, "sta") == 0) {
9798 type = WPA_IF_STATION;
9799 } else if (os_strcmp(pos, "ap") == 0) {
9800 type = WPA_IF_AP_BSS;
9801 } else {
9802 wpa_printf(MSG_DEBUG,
9803 "INTERFACE_ADD unsupported interface type: '%s'",
9804 pos);
9805 return -1;
9806 }
9807 } else {
da3db681
BG
9808 wpa_printf(MSG_DEBUG,
9809 "INTERFACE_ADD unsupported extra parameter: '%s'",
9810 extra);
efa232f9 9811 return -1;
da3db681 9812 }
6fc6879b
JM
9813 } while (0);
9814
efa232f9
JJ
9815 if (create_iface) {
9816 wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
9817 iface.ifname);
9818 if (!global->ifaces)
9819 return -1;
0f039e34 9820 if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
efa232f9
JJ
9821 NULL, NULL, NULL, mac_addr, NULL) < 0) {
9822 wpa_printf(MSG_ERROR,
9823 "CTRL_IFACE interface creation failed");
9824 return -1;
9825 }
9826
9827 wpa_printf(MSG_DEBUG,
9828 "CTRL_IFACE interface '%s' created with MAC addr: "
9829 MACSTR, iface.ifname, MAC2STR(mac_addr));
9830 }
9831
6fc6879b 9832 if (wpa_supplicant_get_iface(global, iface.ifname))
efa232f9
JJ
9833 goto fail;
9834
9835 wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
9836 if (!wpa_s)
9837 goto fail;
9838 wpa_s->added_vif = create_iface;
9839 return 0;
6fc6879b 9840
efa232f9
JJ
9841fail:
9842 if (create_iface)
9843 wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
9844 return -1;
6fc6879b
JM
9845}
9846
9847
9848static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
9849 char *cmd)
9850{
9851 struct wpa_supplicant *wpa_s;
efa232f9
JJ
9852 int ret;
9853 unsigned int delete_iface;
6fc6879b
JM
9854
9855 wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
9856
9857 wpa_s = wpa_supplicant_get_iface(global, cmd);
9858 if (wpa_s == NULL)
9859 return -1;
efa232f9
JJ
9860 delete_iface = wpa_s->added_vif;
9861 ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
9862 if (!ret && delete_iface) {
9863 wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
9864 cmd);
9865 ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
9866 }
9867 return ret;
6fc6879b
JM
9868}
9869
9870
4b4a8ae5
JM
9871static void wpa_free_iface_info(struct wpa_interface_info *iface)
9872{
9873 struct wpa_interface_info *prev;
9874
9875 while (iface) {
9876 prev = iface;
9877 iface = iface->next;
9878
9879 os_free(prev->ifname);
9880 os_free(prev->desc);
9881 os_free(prev);
9882 }
9883}
9884
9885
9886static int wpa_supplicant_global_iface_list(struct wpa_global *global,
9887 char *buf, int len)
9888{
9889 int i, res;
9890 struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
9891 char *pos, *end;
9892
c5121837 9893 for (i = 0; wpa_drivers[i]; i++) {
8b423edb 9894 const struct wpa_driver_ops *drv = wpa_drivers[i];
4b4a8ae5
JM
9895 if (drv->get_interfaces == NULL)
9896 continue;
5fbc1f27 9897 tmp = drv->get_interfaces(global->drv_priv[i]);
4b4a8ae5
JM
9898 if (tmp == NULL)
9899 continue;
9900
9901 if (last == NULL)
9902 iface = last = tmp;
9903 else
9904 last->next = tmp;
9905 while (last->next)
9906 last = last->next;
9907 }
9908
9909 pos = buf;
9910 end = buf + len;
9911 for (tmp = iface; tmp; tmp = tmp->next) {
9912 res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
9913 tmp->drv_name, tmp->ifname,
9914 tmp->desc ? tmp->desc : "");
d85e1fc8 9915 if (os_snprintf_error(end - pos, res)) {
4b4a8ae5
JM
9916 *pos = '\0';
9917 break;
9918 }
9919 pos += res;
9920 }
9921
9922 wpa_free_iface_info(iface);
9923
9924 return pos - buf;
9925}
9926
9927
6fc6879b 9928static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
56e2fc2c 9929 const char *input,
6fc6879b
JM
9930 char *buf, int len)
9931{
9932 int res;
9933 char *pos, *end;
9934 struct wpa_supplicant *wpa_s;
56e2fc2c
JD
9935 int show_ctrl = 0;
9936
9937 if (input)
9938 show_ctrl = !!os_strstr(input, "ctrl");
6fc6879b
JM
9939
9940 wpa_s = global->ifaces;
9941 pos = buf;
9942 end = buf + len;
9943
9944 while (wpa_s) {
56e2fc2c
JD
9945 if (show_ctrl)
9946 res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
9947 wpa_s->ifname,
9948 wpa_s->conf->ctrl_interface ?
9949 wpa_s->conf->ctrl_interface : "N/A");
9950 else
9951 res = os_snprintf(pos, end - pos, "%s\n",
9952 wpa_s->ifname);
9953
d85e1fc8 9954 if (os_snprintf_error(end - pos, res)) {
6fc6879b
JM
9955 *pos = '\0';
9956 break;
9957 }
9958 pos += res;
9959 wpa_s = wpa_s->next;
9960 }
9961 return pos - buf;
9962}
9963
9964
cf3bebf2
JM
9965static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
9966 const char *ifname,
9967 char *cmd, size_t *resp_len)
9968{
9969 struct wpa_supplicant *wpa_s;
9970
9971 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9972 if (os_strcmp(ifname, wpa_s->ifname) == 0)
9973 break;
9974 }
9975
9976 if (wpa_s == NULL) {
9977 char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
9978 if (resp)
9979 *resp_len = os_strlen(resp);
9980 else
9981 *resp_len = 1;
9982 return resp;
9983 }
9984
9985 return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
9986}
9987
9988
576bce9c
JM
9989static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
9990 char *buf, size_t *resp_len)
9991{
9992#ifdef CONFIG_P2P
9993 static const char * cmd[] = {
443427e4 9994 "LIST_NETWORKS",
576bce9c
JM
9995 "P2P_FIND",
9996 "P2P_STOP_FIND",
9997 "P2P_LISTEN",
9998 "P2P_GROUP_ADD",
9999 "P2P_GET_PASSPHRASE",
10000 "P2P_SERVICE_UPDATE",
10001 "P2P_SERVICE_FLUSH",
10002 "P2P_FLUSH",
10003 "P2P_CANCEL",
10004 "P2P_PRESENCE_REQ",
10005 "P2P_EXT_LISTEN",
10006 NULL
10007 };
10008 static const char * prefix[] = {
443427e4
DS
10009#ifdef ANDROID
10010 "DRIVER ",
10011#endif /* ANDROID */
10012 "GET_NETWORK ",
10013 "REMOVE_NETWORK ",
576bce9c
JM
10014 "P2P_FIND ",
10015 "P2P_CONNECT ",
10016 "P2P_LISTEN ",
10017 "P2P_GROUP_REMOVE ",
10018 "P2P_GROUP_ADD ",
57b38882 10019 "P2P_GROUP_MEMBER ",
576bce9c
JM
10020 "P2P_PROV_DISC ",
10021 "P2P_SERV_DISC_REQ ",
10022 "P2P_SERV_DISC_CANCEL_REQ ",
10023 "P2P_SERV_DISC_RESP ",
10024 "P2P_SERV_DISC_EXTERNAL ",
10025 "P2P_SERVICE_ADD ",
10026 "P2P_SERVICE_DEL ",
87d5ef5a 10027 "P2P_SERVICE_REP ",
576bce9c
JM
10028 "P2P_REJECT ",
10029 "P2P_INVITE ",
10030 "P2P_PEER ",
10031 "P2P_SET ",
10032 "P2P_UNAUTHORIZE ",
10033 "P2P_PRESENCE_REQ ",
10034 "P2P_EXT_LISTEN ",
f2c56602 10035 "P2P_REMOVE_CLIENT ",
8ad8bc5c
DS
10036 "WPS_NFC_TOKEN ",
10037 "WPS_NFC_TAG_READ ",
f3ff9487
AM
10038 "NFC_GET_HANDOVER_SEL ",
10039 "NFC_GET_HANDOVER_REQ ",
10040 "NFC_REPORT_HANDOVER ",
87d5ef5a
KV
10041 "P2P_ASP_PROVISION ",
10042 "P2P_ASP_PROVISION_RESP ",
576bce9c
JM
10043 NULL
10044 };
10045 int found = 0;
10046 int i;
10047
10048 if (global->p2p_init_wpa_s == NULL)
10049 return NULL;
10050
10051 for (i = 0; !found && cmd[i]; i++) {
10052 if (os_strcmp(buf, cmd[i]) == 0)
10053 found = 1;
10054 }
10055
10056 for (i = 0; !found && prefix[i]; i++) {
10057 if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
10058 found = 1;
10059 }
10060
10061 if (found)
10062 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
10063 buf, resp_len);
10064#endif /* CONFIG_P2P */
10065 return NULL;
10066}
10067
10068
10069static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
10070 char *buf, size_t *resp_len)
10071{
10072#ifdef CONFIG_WIFI_DISPLAY
10073 if (global->p2p_init_wpa_s == NULL)
10074 return NULL;
10075 if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
10076 os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
10077 return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
10078 buf, resp_len);
10079#endif /* CONFIG_WIFI_DISPLAY */
10080 return NULL;
10081}
10082
10083
10084static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
10085 char *buf, size_t *resp_len)
10086{
10087 char *ret;
10088
10089 ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
10090 if (ret)
10091 return ret;
10092
10093 ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
10094 if (ret)
10095 return ret;
10096
10097 return NULL;
10098}
10099
10100
1b9b31c1
JM
10101static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
10102{
10103 char *value;
10104
10105 value = os_strchr(cmd, ' ');
10106 if (value == NULL)
10107 return -1;
10108 *value++ = '\0';
10109
10110 wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
10111
10112#ifdef CONFIG_WIFI_DISPLAY
10113 if (os_strcasecmp(cmd, "wifi_display") == 0) {
10114 wifi_display_enable(global, !!atoi(value));
10115 return 0;
10116 }
10117#endif /* CONFIG_WIFI_DISPLAY */
10118
a7ca6dac
JM
10119 /* Restore cmd to its original value to allow redirection */
10120 value[-1] = ' ';
10121
1b9b31c1
JM
10122 return -1;
10123}
10124
10125
daae4995
AN
10126static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
10127 char *cmd)
10128{
10129 struct wpa_supplicant *wpa_s[2]; /* src, dst */
10130 char *p;
10131 unsigned int i;
10132
10133 /* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
10134 * <variable name> */
10135
10136 for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
10137 p = os_strchr(cmd, ' ');
10138 if (p == NULL)
10139 return -1;
10140 *p = '\0';
10141
10142 wpa_s[i] = global->ifaces;
10143 for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
10144 if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
10145 break;
10146 }
10147
10148 if (!wpa_s[i]) {
10149 wpa_printf(MSG_DEBUG,
10150 "CTRL_IFACE: Could not find iface=%s", cmd);
10151 return -1;
10152 }
10153
10154 cmd = p + 1;
10155 }
10156
10157 return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
10158}
10159
10160
42868f14
JM
10161#ifndef CONFIG_NO_CONFIG_WRITE
10162static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
10163{
d6b818ef 10164 int ret = 0, saved = 0;
42868f14
JM
10165 struct wpa_supplicant *wpa_s;
10166
10167 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
10168 if (!wpa_s->conf->update_config) {
10169 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
10170 continue;
10171 }
10172
10173 if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
10174 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
10175 ret = 1;
10176 } else {
10177 wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
d6b818ef 10178 saved++;
42868f14
JM
10179 }
10180 }
10181
d6b818ef
JM
10182 if (!saved && !ret) {
10183 wpa_dbg(wpa_s, MSG_DEBUG,
10184 "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
10185 ret = 1;
10186 }
10187
42868f14
JM
10188 return ret;
10189}
10190#endif /* CONFIG_NO_CONFIG_WRITE */
10191
10192
ae8c27f7
JM
10193static int wpas_global_ctrl_iface_status(struct wpa_global *global,
10194 char *buf, size_t buflen)
10195{
10196 char *pos, *end;
10197 int ret;
10198 struct wpa_supplicant *wpa_s;
10199
10200 pos = buf;
10201 end = buf + buflen;
10202
10203#ifdef CONFIG_P2P
4c559019 10204 if (global->p2p && !global->p2p_disabled) {
ae8c27f7 10205 ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
4c559019
JM
10206 "\n"
10207 "p2p_state=%s\n",
10208 MAC2STR(global->p2p_dev_addr),
10209 p2p_get_state_txt(global->p2p));
d85e1fc8 10210 if (os_snprintf_error(end - pos, ret))
4c559019
JM
10211 return pos - buf;
10212 pos += ret;
10213 } else if (global->p2p) {
10214 ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
d85e1fc8 10215 if (os_snprintf_error(end - pos, ret))
ae8c27f7
JM
10216 return pos - buf;
10217 pos += ret;
10218 }
10219#endif /* CONFIG_P2P */
10220
10221#ifdef CONFIG_WIFI_DISPLAY
10222 ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
10223 !!global->wifi_display);
d85e1fc8 10224 if (os_snprintf_error(end - pos, ret))
ae8c27f7
JM
10225 return pos - buf;
10226 pos += ret;
10227#endif /* CONFIG_WIFI_DISPLAY */
10228
10229 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
10230 ret = os_snprintf(pos, end - pos, "ifname=%s\n"
10231 "address=" MACSTR "\n",
10232 wpa_s->ifname, MAC2STR(wpa_s->own_addr));
d85e1fc8 10233 if (os_snprintf_error(end - pos, ret))
ae8c27f7
JM
10234 return pos - buf;
10235 pos += ret;
10236 }
10237
10238 return pos - buf;
10239}
10240
10241
3794af2d
AN
10242#ifdef CONFIG_FST
10243
10244static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
10245 char *cmd, char *buf,
10246 size_t reply_size)
10247{
10248 char ifname[IFNAMSIZ + 1];
10249 struct fst_iface_cfg cfg;
10250 struct wpa_supplicant *wpa_s;
10251 struct fst_wpa_obj iface_obj;
10252
10253 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
10254 wpa_s = wpa_supplicant_get_iface(global, ifname);
10255 if (wpa_s) {
5dbd3bf9
JM
10256 if (wpa_s->fst) {
10257 wpa_printf(MSG_INFO, "FST: Already attached");
10258 return -1;
10259 }
3794af2d
AN
10260 fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
10261 wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
10262 &iface_obj, &cfg);
10263 if (wpa_s->fst)
10264 return os_snprintf(buf, reply_size, "OK\n");
10265 }
10266 }
10267
10268 return -1;
10269}
10270
10271
10272static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
10273 char *cmd, char *buf,
10274 size_t reply_size)
10275{
10276 char ifname[IFNAMSIZ + 1];
10277 struct wpa_supplicant *wpa_s;
10278
10279 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
10280 wpa_s = wpa_supplicant_get_iface(global, ifname);
10281 if (wpa_s) {
10282 if (!fst_iface_detach(ifname)) {
10283 wpa_s->fst = NULL;
10284 return os_snprintf(buf, reply_size, "OK\n");
10285 }
10286 }
10287 }
10288
10289 return -1;
10290}
10291
10292#endif /* CONFIG_FST */
10293
10294
6fc6879b
JM
10295char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
10296 char *buf, size_t *resp_len)
10297{
10298 char *reply;
10299 const int reply_size = 2048;
10300 int reply_len;
f4a0a82c 10301 int level = MSG_DEBUG;
6fc6879b 10302
cf3bebf2
JM
10303 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
10304 char *pos = os_strchr(buf + 7, ' ');
10305 if (pos) {
10306 *pos++ = '\0';
10307 return wpas_global_ctrl_iface_ifname(global,
10308 buf + 7, pos,
10309 resp_len);
10310 }
10311 }
10312
576bce9c
JM
10313 reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
10314 if (reply)
10315 return reply;
10316
f4a0a82c
JM
10317 if (os_strcmp(buf, "PING") == 0)
10318 level = MSG_EXCESSIVE;
10319 wpa_hexdump_ascii(level, "RX global ctrl_iface",
6fc6879b
JM
10320 (const u8 *) buf, os_strlen(buf));
10321
10322 reply = os_malloc(reply_size);
10323 if (reply == NULL) {
10324 *resp_len = 1;
10325 return NULL;
10326 }
10327
10328 os_memcpy(reply, "OK\n", 3);
10329 reply_len = 3;
10330
10331 if (os_strcmp(buf, "PING") == 0) {
10332 os_memcpy(reply, "PONG\n", 5);
10333 reply_len = 5;
10334 } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
10335 if (wpa_supplicant_global_iface_add(global, buf + 14))
10336 reply_len = -1;
10337 } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
10338 if (wpa_supplicant_global_iface_remove(global, buf + 17))
10339 reply_len = -1;
4b4a8ae5
JM
10340 } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
10341 reply_len = wpa_supplicant_global_iface_list(
10342 global, reply, reply_size);
56e2fc2c 10343 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
6fc6879b 10344 reply_len = wpa_supplicant_global_iface_interfaces(
56e2fc2c 10345 global, buf + 10, reply, reply_size);
3794af2d
AN
10346#ifdef CONFIG_FST
10347 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
10348 reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
10349 reply,
10350 reply_size);
10351 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
10352 reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
10353 reply,
10354 reply_size);
10355 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
10356 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
10357#endif /* CONFIG_FST */
6fc6879b 10358 } else if (os_strcmp(buf, "TERMINATE") == 0) {
1a1bf008 10359 wpa_supplicant_terminate_proc(global);
207ef3fb
JM
10360 } else if (os_strcmp(buf, "SUSPEND") == 0) {
10361 wpas_notify_suspend(global);
10362 } else if (os_strcmp(buf, "RESUME") == 0) {
10363 wpas_notify_resume(global);
1b9b31c1 10364 } else if (os_strncmp(buf, "SET ", 4) == 0) {
a7ca6dac
JM
10365 if (wpas_global_ctrl_iface_set(global, buf + 4)) {
10366#ifdef CONFIG_P2P
10367 if (global->p2p_init_wpa_s) {
10368 os_free(reply);
10369 /* Check if P2P redirection would work for this
10370 * command. */
10371 return wpa_supplicant_ctrl_iface_process(
10372 global->p2p_init_wpa_s,
10373 buf, resp_len);
10374 }
10375#endif /* CONFIG_P2P */
1b9b31c1 10376 reply_len = -1;
a7ca6dac 10377 }
daae4995
AN
10378 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
10379 if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
10380 reply_len = -1;
42868f14
JM
10381#ifndef CONFIG_NO_CONFIG_WRITE
10382 } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
10383 if (wpas_global_ctrl_iface_save_config(global))
10384 reply_len = -1;
10385#endif /* CONFIG_NO_CONFIG_WRITE */
ae8c27f7
JM
10386 } else if (os_strcmp(buf, "STATUS") == 0) {
10387 reply_len = wpas_global_ctrl_iface_status(global, reply,
10388 reply_size);
ea449b5b
JM
10389#ifdef CONFIG_MODULE_TESTS
10390 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
ea449b5b
JM
10391 if (wpas_module_tests() < 0)
10392 reply_len = -1;
10393#endif /* CONFIG_MODULE_TESTS */
5f797376
JM
10394 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
10395 if (wpa_debug_reopen_file() < 0)
10396 reply_len = -1;
6fc6879b
JM
10397 } else {
10398 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
10399 reply_len = 16;
10400 }
10401
10402 if (reply_len < 0) {
10403 os_memcpy(reply, "FAIL\n", 5);
10404 reply_len = 5;
10405 }
10406
10407 *resp_len = reply_len;
10408 return reply;
10409}