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