• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * WPA Supplicant / Control interface (shared code for all backends)
3  * Copyright (c) 2004-2020, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 #ifdef CONFIG_TESTING_OPTIONS
11 #include <netinet/ip.h>
12 #endif /* CONFIG_TESTING_OPTIONS */
13 
14 #include "utils/common.h"
15 #include "utils/eloop.h"
16 #include "utils/uuid.h"
17 #include "utils/module_tests.h"
18 #include "common/version.h"
19 #include "common/ieee802_11_defs.h"
20 #include "common/ieee802_11_common.h"
21 #include "common/wpa_ctrl.h"
22 #ifdef CONFIG_DPP
23 #include "common/dpp.h"
24 #endif /* CONFIG_DPP */
25 #include "common/ptksa_cache.h"
26 #include "crypto/tls.h"
27 #include "ap/hostapd.h"
28 #include "eap_peer/eap.h"
29 #include "eapol_supp/eapol_supp_sm.h"
30 #include "rsn_supp/wpa.h"
31 #include "rsn_supp/preauth.h"
32 #include "rsn_supp/pmksa_cache.h"
33 #include "l2_packet/l2_packet.h"
34 #include "wps/wps.h"
35 #include "fst/fst.h"
36 #include "fst/fst_ctrl_iface.h"
37 #include "config.h"
38 #include "wpa_supplicant_i.h"
39 #include "driver_i.h"
40 #include "wps_supplicant.h"
41 #include "ibss_rsn.h"
42 #include "wpas_glue.h"
43 #include "ap.h"
44 #include "p2p_supplicant.h"
45 #include "p2p/p2p.h"
46 #include "hs20_supplicant.h"
47 #include "wifi_display.h"
48 #include "notify.h"
49 #include "bss.h"
50 #include "scan.h"
51 #include "ctrl_iface.h"
52 #include "interworking.h"
53 #include "bssid_ignore.h"
54 #include "autoscan.h"
55 #include "wnm_sta.h"
56 #include "offchannel.h"
57 #include "drivers/driver.h"
58 #include "mesh.h"
59 #include "dpp_supplicant.h"
60 #include "sme.h"
61 
62 #ifdef __NetBSD__
63 #include <net/if_ether.h>
64 #elif !defined(__CYGWIN__) && !defined(CONFIG_NATIVE_WINDOWS)
65 #include <net/ethernet.h>
66 #endif
67 
68 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
69 					    char *buf, int len);
70 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
71 						  const char *input,
72 						  char *buf, int len);
73 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
74 					char *val);
75 
76 
set_bssid_filter(struct wpa_supplicant * wpa_s,char * val)77 static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
78 {
79 	char *pos;
80 	u8 addr[ETH_ALEN], *filter = NULL, *n;
81 	size_t count = 0;
82 
83 	pos = val;
84 	while (pos) {
85 		if (*pos == '\0')
86 			break;
87 		if (hwaddr_aton(pos, addr)) {
88 			os_free(filter);
89 			return -1;
90 		}
91 		n = os_realloc_array(filter, count + 1, ETH_ALEN);
92 		if (n == NULL) {
93 			os_free(filter);
94 			return -1;
95 		}
96 		filter = n;
97 		os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
98 		count++;
99 
100 		pos = os_strchr(pos, ' ');
101 		if (pos)
102 			pos++;
103 	}
104 
105 	wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
106 	os_free(wpa_s->bssid_filter);
107 	wpa_s->bssid_filter = filter;
108 	wpa_s->bssid_filter_count = count;
109 
110 	return 0;
111 }
112 
113 
set_disallow_aps(struct wpa_supplicant * wpa_s,char * val)114 static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
115 {
116 	char *pos;
117 	u8 addr[ETH_ALEN], *bssid = NULL, *n;
118 	struct wpa_ssid_value *ssid = NULL, *ns;
119 	size_t count = 0, ssid_count = 0;
120 	struct wpa_ssid *c;
121 
122 	/*
123 	 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
124 	 * SSID_SPEC ::= ssid <SSID_HEX>
125 	 * BSSID_SPEC ::= bssid <BSSID_HEX>
126 	 */
127 
128 	pos = val;
129 	while (pos) {
130 		if (*pos == '\0')
131 			break;
132 		if (os_strncmp(pos, "bssid ", 6) == 0) {
133 			int res;
134 			pos += 6;
135 			res = hwaddr_aton2(pos, addr);
136 			if (res < 0) {
137 				os_free(ssid);
138 				os_free(bssid);
139 				wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
140 					   "BSSID value '%s'", pos);
141 				return -1;
142 			}
143 			pos += res;
144 			n = os_realloc_array(bssid, count + 1, ETH_ALEN);
145 			if (n == NULL) {
146 				os_free(ssid);
147 				os_free(bssid);
148 				return -1;
149 			}
150 			bssid = n;
151 			os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
152 			count++;
153 		} else if (os_strncmp(pos, "ssid ", 5) == 0) {
154 			char *end;
155 			pos += 5;
156 
157 			end = pos;
158 			while (*end) {
159 				if (*end == '\0' || *end == ' ')
160 					break;
161 				end++;
162 			}
163 
164 			ns = os_realloc_array(ssid, ssid_count + 1,
165 					      sizeof(struct wpa_ssid_value));
166 			if (ns == NULL) {
167 				os_free(ssid);
168 				os_free(bssid);
169 				return -1;
170 			}
171 			ssid = ns;
172 
173 			if ((end - pos) & 0x01 ||
174 			    end - pos > 2 * SSID_MAX_LEN ||
175 			    hexstr2bin(pos, ssid[ssid_count].ssid,
176 				       (end - pos) / 2) < 0) {
177 				os_free(ssid);
178 				os_free(bssid);
179 				wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
180 					   "SSID value '%s'", pos);
181 				return -1;
182 			}
183 			ssid[ssid_count].ssid_len = (end - pos) / 2;
184 			wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
185 					  ssid[ssid_count].ssid,
186 					  ssid[ssid_count].ssid_len);
187 			ssid_count++;
188 			pos = end;
189 		} else {
190 			wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
191 				   "'%s'", pos);
192 			os_free(ssid);
193 			os_free(bssid);
194 			return -1;
195 		}
196 
197 		pos = os_strchr(pos, ' ');
198 		if (pos)
199 			pos++;
200 	}
201 
202 	wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
203 	os_free(wpa_s->disallow_aps_bssid);
204 	wpa_s->disallow_aps_bssid = bssid;
205 	wpa_s->disallow_aps_bssid_count = count;
206 
207 	wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
208 	os_free(wpa_s->disallow_aps_ssid);
209 	wpa_s->disallow_aps_ssid = ssid;
210 	wpa_s->disallow_aps_ssid_count = ssid_count;
211 
212 	if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
213 		return 0;
214 
215 	c = wpa_s->current_ssid;
216 	if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
217 		return 0;
218 
219 	if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
220 	    !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
221 		return 0;
222 
223 	wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
224 		   "because current AP was marked disallowed");
225 
226 #ifdef CONFIG_SME
227 	wpa_s->sme.prev_bssid_set = 0;
228 #endif /* CONFIG_SME */
229 	wpa_s->reassociate = 1;
230 	wpa_s->own_disconnect_req = 1;
231 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
232 	wpa_supplicant_req_scan(wpa_s, 0, 0);
233 
234 	return 0;
235 }
236 
237 
238 #ifndef CONFIG_NO_CONFIG_BLOBS
wpas_ctrl_set_blob(struct wpa_supplicant * wpa_s,char * pos)239 static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
240 {
241 	char *name = pos;
242 	struct wpa_config_blob *blob;
243 	size_t len;
244 
245 	pos = os_strchr(pos, ' ');
246 	if (pos == NULL)
247 		return -1;
248 	*pos++ = '\0';
249 	len = os_strlen(pos);
250 	if (len & 1)
251 		return -1;
252 
253 	wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
254 	blob = os_zalloc(sizeof(*blob));
255 	if (blob == NULL)
256 		return -1;
257 	blob->name = os_strdup(name);
258 	blob->data = os_malloc(len / 2);
259 	if (blob->name == NULL || blob->data == NULL) {
260 		wpa_config_free_blob(blob);
261 		return -1;
262 	}
263 
264 	if (hexstr2bin(pos, blob->data, len / 2) < 0) {
265 		wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
266 		wpa_config_free_blob(blob);
267 		return -1;
268 	}
269 	blob->len = len / 2;
270 
271 	wpa_config_set_blob(wpa_s->conf, blob);
272 
273 	return 0;
274 }
275 #endif /* CONFIG_NO_CONFIG_BLOBS */
276 
277 
wpas_ctrl_pno(struct wpa_supplicant * wpa_s,char * cmd)278 static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
279 {
280 	char *params;
281 	char *pos;
282 	int *freqs = NULL;
283 	int ret;
284 
285 	if (atoi(cmd)) {
286 		params = os_strchr(cmd, ' ');
287 		os_free(wpa_s->manual_sched_scan_freqs);
288 		if (params) {
289 			params++;
290 			pos = os_strstr(params, "freq=");
291 			if (pos)
292 				freqs = freq_range_to_channel_list(wpa_s,
293 								   pos + 5);
294 		}
295 		wpa_s->manual_sched_scan_freqs = freqs;
296 		ret = wpas_start_pno(wpa_s);
297 	} else {
298 		ret = wpas_stop_pno(wpa_s);
299 	}
300 	return ret;
301 }
302 
303 
wpas_ctrl_set_band(struct wpa_supplicant * wpa_s,char * bands)304 static int wpas_ctrl_set_band(struct wpa_supplicant *wpa_s, char *bands)
305 {
306 	union wpa_event_data event;
307 	u32 setband_mask = WPA_SETBAND_AUTO;
308 
309 	/*
310 	 * For example:
311 	 *  SET setband 2G,6G
312 	 *  SET setband 5G
313 	 *  SET setband AUTO
314 	 */
315 	if (!os_strstr(bands, "AUTO")) {
316 		if (os_strstr(bands, "5G"))
317 			setband_mask |= WPA_SETBAND_5G;
318 		if (os_strstr(bands, "6G"))
319 			setband_mask |= WPA_SETBAND_6G;
320 		if (os_strstr(bands, "2G"))
321 			setband_mask |= WPA_SETBAND_2G;
322 		if (setband_mask == WPA_SETBAND_AUTO)
323 			return -1;
324 	}
325 
326 	wpa_s->setband_mask = setband_mask;
327 	if (wpa_drv_setband(wpa_s, wpa_s->setband_mask) == 0) {
328 		os_memset(&event, 0, sizeof(event));
329 		event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
330 		event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
331 		wpa_supplicant_event(wpa_s, EVENT_CHANNEL_LIST_CHANGED, &event);
332 	}
333 
334 	return 0;
335 }
336 
337 
wpas_ctrl_iface_set_lci(struct wpa_supplicant * wpa_s,const char * cmd)338 static int wpas_ctrl_iface_set_lci(struct wpa_supplicant *wpa_s,
339 				   const char *cmd)
340 {
341 	struct wpabuf *lci;
342 
343 	if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
344 		wpabuf_free(wpa_s->lci);
345 		wpa_s->lci = NULL;
346 		return 0;
347 	}
348 
349 	lci = wpabuf_parse_bin(cmd);
350 	if (!lci)
351 		return -1;
352 
353 	if (os_get_reltime(&wpa_s->lci_time)) {
354 		wpabuf_free(lci);
355 		return -1;
356 	}
357 
358 	wpabuf_free(wpa_s->lci);
359 	wpa_s->lci = lci;
360 
361 	return 0;
362 }
363 
364 
365 static int
wpas_ctrl_set_relative_rssi(struct wpa_supplicant * wpa_s,const char * cmd)366 wpas_ctrl_set_relative_rssi(struct wpa_supplicant *wpa_s, const char *cmd)
367 {
368 	int relative_rssi;
369 
370 	if (os_strcmp(cmd, "disable") == 0) {
371 		wpa_s->srp.relative_rssi_set = 0;
372 		return 0;
373 	}
374 
375 	relative_rssi = atoi(cmd);
376 	if (relative_rssi < 0 || relative_rssi > 100)
377 		return -1;
378 	wpa_s->srp.relative_rssi = relative_rssi;
379 	wpa_s->srp.relative_rssi_set = 1;
380 	return 0;
381 }
382 
383 
wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant * wpa_s,const char * cmd)384 static int wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant *wpa_s,
385 					      const char *cmd)
386 {
387 	char *pos;
388 	int adjust_rssi;
389 
390 	/* <band>:adjust_value */
391 	pos = os_strchr(cmd, ':');
392 	if (!pos)
393 		return -1;
394 	pos++;
395 	adjust_rssi = atoi(pos);
396 	if (adjust_rssi < -100 || adjust_rssi > 100)
397 		return -1;
398 
399 	if (os_strncmp(cmd, "2G", 2) == 0)
400 		wpa_s->srp.relative_adjust_band = WPA_SETBAND_2G;
401 	else if (os_strncmp(cmd, "5G", 2) == 0)
402 		wpa_s->srp.relative_adjust_band = WPA_SETBAND_5G;
403 	else
404 		return -1;
405 
406 	wpa_s->srp.relative_adjust_rssi = adjust_rssi;
407 
408 	return 0;
409 }
410 
411 
wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant * wpa_s,const char * cmd)412 static int wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant *wpa_s,
413 				   const char *cmd)
414 {
415 	struct wpabuf *ric_ies;
416 
417 	if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
418 		wpabuf_free(wpa_s->ric_ies);
419 		wpa_s->ric_ies = NULL;
420 		return 0;
421 	}
422 
423 	ric_ies = wpabuf_parse_bin(cmd);
424 	if (!ric_ies)
425 		return -1;
426 
427 	wpabuf_free(wpa_s->ric_ies);
428 	wpa_s->ric_ies = ric_ies;
429 
430 	return 0;
431 }
432 
433 
434 #ifdef CONFIG_TESTING_OPTIONS
wpas_ctrl_iface_set_dso(struct wpa_supplicant * wpa_s,const char * val)435 static int wpas_ctrl_iface_set_dso(struct wpa_supplicant *wpa_s,
436 				   const char *val)
437 {
438 	u8 bssid[ETH_ALEN];
439 	const char *pos = val;
440 	struct driver_signal_override *dso = NULL, *tmp, parsed;
441 
442 	if (hwaddr_aton(pos, bssid))
443 		return -1;
444 	pos = os_strchr(pos, ' ');
445 
446 	dl_list_for_each(tmp, &wpa_s->drv_signal_override,
447 			 struct driver_signal_override, list) {
448 		if (os_memcmp(bssid, tmp->bssid, ETH_ALEN) == 0) {
449 			dso = tmp;
450 			break;
451 		}
452 	}
453 
454 	if (!pos) {
455 		/* Remove existing entry */
456 		if (dso) {
457 			dl_list_del(&dso->list);
458 			os_free(dso);
459 		}
460 		return 0;
461 	}
462 	pos++;
463 
464 	/* Update an existing entry or add a new one */
465 	os_memset(&parsed, 0, sizeof(parsed));
466 	if (sscanf(pos, "%d %d %d %d %d",
467 		   &parsed.si_current_signal,
468 		   &parsed.si_avg_signal,
469 		   &parsed.si_avg_beacon_signal,
470 		   &parsed.si_current_noise,
471 		   &parsed.scan_level) != 5)
472 		return -1;
473 
474 	if (!dso) {
475 		dso = os_zalloc(sizeof(*dso));
476 		if (!dso)
477 			return -1;
478 		os_memcpy(dso->bssid, bssid, ETH_ALEN);
479 		dl_list_add(&wpa_s->drv_signal_override, &dso->list);
480 	}
481 	dso->si_current_signal = parsed.si_current_signal;
482 	dso->si_avg_signal = parsed.si_avg_signal;
483 	dso->si_avg_beacon_signal = parsed.si_avg_beacon_signal;
484 	dso->si_current_noise = parsed.si_current_noise;
485 	dso->scan_level = parsed.scan_level;
486 
487 	return 0;
488 }
489 #endif /* CONFIG_TESTING_OPTIONS */
490 
491 
wpa_supplicant_ctrl_iface_set(struct wpa_supplicant * wpa_s,char * cmd)492 static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
493 					 char *cmd)
494 {
495 	char *value;
496 	int ret = 0;
497 
498 	value = os_strchr(cmd, ' ');
499 	if (value == NULL)
500 		return -1;
501 	*value++ = '\0';
502 
503 	wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
504 	if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
505 		eapol_sm_configure(wpa_s->eapol,
506 				   atoi(value), -1, -1, -1);
507 	} else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
508 		eapol_sm_configure(wpa_s->eapol,
509 				   -1, atoi(value), -1, -1);
510 	} else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
511 		eapol_sm_configure(wpa_s->eapol,
512 				   -1, -1, atoi(value), -1);
513 	} else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
514 		eapol_sm_configure(wpa_s->eapol,
515 				   -1, -1, -1, atoi(value));
516 #ifdef CONFIG_TESTING_OPTIONS
517 	} else if (os_strcasecmp(cmd, "EAPOL::portControl") == 0) {
518 		if (os_strcmp(value, "Auto") == 0)
519 			eapol_sm_notify_portControl(wpa_s->eapol, Auto);
520 		else if (os_strcmp(value, "ForceUnauthorized") == 0)
521 			eapol_sm_notify_portControl(wpa_s->eapol,
522 						    ForceUnauthorized);
523 		else if (os_strcmp(value, "ForceAuthorized") == 0)
524 			eapol_sm_notify_portControl(wpa_s->eapol,
525 						    ForceAuthorized);
526 		else
527 			ret = -1;
528 #endif /* CONFIG_TESTING_OPTIONS */
529 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
530 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
531 				     atoi(value))) {
532 			ret = -1;
533 		} else {
534 			value[-1] = '=';
535 			wpa_config_process_global(wpa_s->conf, cmd, -1);
536 		}
537 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
538 		   0) {
539 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
540 				     atoi(value))) {
541 			ret = -1;
542 		} else {
543 			value[-1] = '=';
544 			wpa_config_process_global(wpa_s->conf, cmd, -1);
545 		}
546 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
547 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
548 				     atoi(value))) {
549 			ret = -1;
550 		} else {
551 			value[-1] = '=';
552 			wpa_config_process_global(wpa_s->conf, cmd, -1);
553 		}
554 	} else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
555 		wpa_s->wps_fragment_size = atoi(value);
556 #ifdef CONFIG_WPS_TESTING
557 	} else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
558 		long int val;
559 		val = strtol(value, NULL, 0);
560 		if (val < 0 || val > 0xff) {
561 			ret = -1;
562 			wpa_printf(MSG_DEBUG, "WPS: Invalid "
563 				   "wps_version_number %ld", val);
564 		} else {
565 			wps_version_number = val;
566 			wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
567 				   "version %u.%u",
568 				   (wps_version_number & 0xf0) >> 4,
569 				   wps_version_number & 0x0f);
570 		}
571 	} else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) {
572 		wps_testing_stub_cred = atoi(value);
573 		wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d",
574 			   wps_testing_stub_cred);
575 	} else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
576 		wps_corrupt_pkhash = atoi(value);
577 		wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
578 			   wps_corrupt_pkhash);
579 	} else if (os_strcasecmp(cmd, "wps_force_auth_types") == 0) {
580 		if (value[0] == '\0') {
581 			wps_force_auth_types_in_use = 0;
582 		} else {
583 			wps_force_auth_types = strtol(value, NULL, 0);
584 			wps_force_auth_types_in_use = 1;
585 		}
586 	} else if (os_strcasecmp(cmd, "wps_force_encr_types") == 0) {
587 		if (value[0] == '\0') {
588 			wps_force_encr_types_in_use = 0;
589 		} else {
590 			wps_force_encr_types = strtol(value, NULL, 0);
591 			wps_force_encr_types_in_use = 1;
592 		}
593 #endif /* CONFIG_WPS_TESTING */
594 	} else if (os_strcasecmp(cmd, "ampdu") == 0) {
595 		if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
596 			ret = -1;
597 #ifdef CONFIG_TDLS
598 #ifdef CONFIG_TDLS_TESTING
599 	} else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
600 		tdls_testing = strtol(value, NULL, 0);
601 		wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
602 #endif /* CONFIG_TDLS_TESTING */
603 	} else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
604 		int disabled = atoi(value);
605 		wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
606 		if (disabled) {
607 			if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
608 				ret = -1;
609 		} else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
610 			ret = -1;
611 		wpa_tdls_enable(wpa_s->wpa, !disabled);
612 #endif /* CONFIG_TDLS */
613 	} else if (os_strcasecmp(cmd, "pno") == 0) {
614 		ret = wpas_ctrl_pno(wpa_s, value);
615 	} else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
616 		int disabled = atoi(value);
617 		if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
618 			ret = -1;
619 		else if (disabled)
620 			wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
621 	} else if (os_strcasecmp(cmd, "uapsd") == 0) {
622 		if (os_strcmp(value, "disable") == 0)
623 			wpa_s->set_sta_uapsd = 0;
624 		else {
625 			int be, bk, vi, vo;
626 			char *pos;
627 			/* format: BE,BK,VI,VO;max SP Length */
628 			be = atoi(value);
629 			pos = os_strchr(value, ',');
630 			if (pos == NULL)
631 				return -1;
632 			pos++;
633 			bk = atoi(pos);
634 			pos = os_strchr(pos, ',');
635 			if (pos == NULL)
636 				return -1;
637 			pos++;
638 			vi = atoi(pos);
639 			pos = os_strchr(pos, ',');
640 			if (pos == NULL)
641 				return -1;
642 			pos++;
643 			vo = atoi(pos);
644 			/* ignore max SP Length for now */
645 
646 			wpa_s->set_sta_uapsd = 1;
647 			wpa_s->sta_uapsd = 0;
648 			if (be)
649 				wpa_s->sta_uapsd |= BIT(0);
650 			if (bk)
651 				wpa_s->sta_uapsd |= BIT(1);
652 			if (vi)
653 				wpa_s->sta_uapsd |= BIT(2);
654 			if (vo)
655 				wpa_s->sta_uapsd |= BIT(3);
656 		}
657 	} else if (os_strcasecmp(cmd, "ps") == 0) {
658 		ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
659 #ifdef CONFIG_WIFI_DISPLAY
660 	} else if (os_strcasecmp(cmd, "wifi_display") == 0) {
661 		int enabled = !!atoi(value);
662 		if (enabled && !wpa_s->global->p2p)
663 			ret = -1;
664 		else
665 			wifi_display_enable(wpa_s->global, enabled);
666 #endif /* CONFIG_WIFI_DISPLAY */
667 	} else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
668 		ret = set_bssid_filter(wpa_s, value);
669 	} else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
670 		ret = set_disallow_aps(wpa_s, value);
671 	} else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
672 		wpa_s->no_keep_alive = !!atoi(value);
673 #ifdef CONFIG_DPP
674 	} else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
675 		os_free(wpa_s->dpp_configurator_params);
676 		wpa_s->dpp_configurator_params = os_strdup(value);
677 #ifdef CONFIG_DPP2
678 		dpp_controller_set_params(wpa_s->dpp, value);
679 #endif /* CONFIG_DPP2 */
680 	} else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
681 		wpa_s->dpp_init_max_tries = atoi(value);
682 	} else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
683 		wpa_s->dpp_init_retry_time = atoi(value);
684 	} else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
685 		wpa_s->dpp_resp_wait_time = atoi(value);
686 	} else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
687 		wpa_s->dpp_resp_max_tries = atoi(value);
688 	} else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
689 		wpa_s->dpp_resp_retry_time = atoi(value);
690 #ifdef CONFIG_TESTING_OPTIONS
691 	} else if (os_strcasecmp(cmd, "dpp_pkex_own_mac_override") == 0) {
692 		if (hwaddr_aton(value, dpp_pkex_own_mac_override))
693 			ret = -1;
694 	} else if (os_strcasecmp(cmd, "dpp_pkex_peer_mac_override") == 0) {
695 		if (hwaddr_aton(value, dpp_pkex_peer_mac_override))
696 			ret = -1;
697 	} else if (os_strcasecmp(cmd, "dpp_pkex_ephemeral_key_override") == 0) {
698 		size_t hex_len = os_strlen(value);
699 
700 		if (hex_len >
701 		    2 * sizeof(dpp_pkex_ephemeral_key_override))
702 			ret = -1;
703 		else if (hexstr2bin(value, dpp_pkex_ephemeral_key_override,
704 				    hex_len / 2))
705 			ret = -1;
706 		else
707 			dpp_pkex_ephemeral_key_override_len = hex_len / 2;
708 	} else if (os_strcasecmp(cmd, "dpp_protocol_key_override") == 0) {
709 		size_t hex_len = os_strlen(value);
710 
711 		if (hex_len > 2 * sizeof(dpp_protocol_key_override))
712 			ret = -1;
713 		else if (hexstr2bin(value, dpp_protocol_key_override,
714 				    hex_len / 2))
715 			ret = -1;
716 		else
717 			dpp_protocol_key_override_len = hex_len / 2;
718 	} else if (os_strcasecmp(cmd, "dpp_nonce_override") == 0) {
719 		size_t hex_len = os_strlen(value);
720 
721 		if (hex_len > 2 * sizeof(dpp_nonce_override))
722 			ret = -1;
723 		else if (hexstr2bin(value, dpp_nonce_override, hex_len / 2))
724 			ret = -1;
725 		else
726 			dpp_nonce_override_len = hex_len / 2;
727 	} else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
728 		dpp_version_override = atoi(value);
729 #endif /* CONFIG_TESTING_OPTIONS */
730 #endif /* CONFIG_DPP */
731 #ifdef CONFIG_TESTING_OPTIONS
732 	} else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
733 		wpa_s->ext_mgmt_frame_handling = !!atoi(value);
734 	} else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
735 		wpa_s->ext_eapol_frame_io = !!atoi(value);
736 #ifdef CONFIG_AP
737 		if (wpa_s->ap_iface) {
738 			wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
739 				wpa_s->ext_eapol_frame_io;
740 		}
741 #endif /* CONFIG_AP */
742 	} else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
743 		wpa_s->extra_roc_dur = atoi(value);
744 	} else if (os_strcasecmp(cmd, "test_failure") == 0) {
745 		wpa_s->test_failure = atoi(value);
746 	} else if (os_strcasecmp(cmd, "p2p_go_csa_on_inv") == 0) {
747 		wpa_s->p2p_go_csa_on_inv = !!atoi(value);
748 	} else if (os_strcasecmp(cmd, "ignore_auth_resp") == 0) {
749 		wpa_s->ignore_auth_resp = !!atoi(value);
750 	} else if (os_strcasecmp(cmd, "ignore_assoc_disallow") == 0) {
751 		wpa_s->ignore_assoc_disallow = !!atoi(value);
752 		wpa_drv_ignore_assoc_disallow(wpa_s,
753 					      wpa_s->ignore_assoc_disallow);
754 	} else if (os_strcasecmp(cmd, "disable_sa_query") == 0) {
755 		wpa_s->disable_sa_query = !!atoi(value);
756 	} else if (os_strcasecmp(cmd, "ignore_sae_h2e_only") == 0) {
757 		wpa_s->ignore_sae_h2e_only = !!atoi(value);
758 	} else if (os_strcasecmp(cmd, "extra_sae_rejected_groups") == 0) {
759 		char *pos;
760 
761 		os_free(wpa_s->extra_sae_rejected_groups);
762 		wpa_s->extra_sae_rejected_groups = NULL;
763 		pos = value;
764 		while (pos && pos[0]) {
765 			int group;
766 
767 			group = atoi(pos);
768 			wpa_printf(MSG_DEBUG,
769 				   "TESTING: Extra rejection of SAE group %d",
770 				   group);
771 			if (group)
772 				int_array_add_unique(
773 					&wpa_s->extra_sae_rejected_groups,
774 					group);
775 			pos = os_strchr(pos, ' ');
776 			if (!pos)
777 				break;
778 			pos++;
779 		}
780 	} else if (os_strcasecmp(cmd, "ft_rsnxe_used") == 0) {
781 		wpa_s->ft_rsnxe_used = atoi(value);
782 	} else if (os_strcasecmp(cmd, "oci_freq_override_eapol") == 0) {
783 		wpa_s->oci_freq_override_eapol = atoi(value);
784 	} else if (os_strcasecmp(cmd, "oci_freq_override_saquery_req") == 0) {
785 		wpa_s->oci_freq_override_saquery_req = atoi(value);
786 	} else if (os_strcasecmp(cmd, "oci_freq_override_saquery_resp") == 0) {
787 		wpa_s->oci_freq_override_saquery_resp = atoi(value);
788 	} else if (os_strcasecmp(cmd, "oci_freq_override_eapol_g2") == 0) {
789 		wpa_s->oci_freq_override_eapol_g2 = atoi(value);
790 		/* Populate value to wpa_sm if already associated. */
791 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_EAPOL_G2,
792 				 wpa_s->oci_freq_override_eapol_g2);
793 	} else if (os_strcasecmp(cmd, "oci_freq_override_ft_assoc") == 0) {
794 		wpa_s->oci_freq_override_ft_assoc = atoi(value);
795 		/* Populate value to wpa_sm if already associated. */
796 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_FT_ASSOC,
797 				 wpa_s->oci_freq_override_ft_assoc);
798 	} else if (os_strcasecmp(cmd, "oci_freq_override_fils_assoc") == 0) {
799 		wpa_s->oci_freq_override_fils_assoc = atoi(value);
800 	} else if (os_strcasecmp(cmd, "oci_freq_override_wnm_sleep") == 0) {
801 		wpa_s->oci_freq_override_wnm_sleep = atoi(value);
802 	} else if (os_strcasecmp(cmd, "rsne_override_eapol") == 0) {
803 		wpabuf_free(wpa_s->rsne_override_eapol);
804 		if (os_strcmp(value, "NULL") == 0)
805 			wpa_s->rsne_override_eapol = NULL;
806 		else
807 			wpa_s->rsne_override_eapol = wpabuf_parse_bin(value);
808 	} else if (os_strcasecmp(cmd, "rsnxe_override_assoc") == 0) {
809 		wpabuf_free(wpa_s->rsnxe_override_assoc);
810 		if (os_strcmp(value, "NULL") == 0)
811 			wpa_s->rsnxe_override_assoc = NULL;
812 		else
813 			wpa_s->rsnxe_override_assoc = wpabuf_parse_bin(value);
814 	} else if (os_strcasecmp(cmd, "rsnxe_override_eapol") == 0) {
815 		wpabuf_free(wpa_s->rsnxe_override_eapol);
816 		if (os_strcmp(value, "NULL") == 0)
817 			wpa_s->rsnxe_override_eapol = NULL;
818 		else
819 			wpa_s->rsnxe_override_eapol = wpabuf_parse_bin(value);
820 	} else if (os_strcasecmp(cmd, "reject_btm_req_reason") == 0) {
821 		wpa_s->reject_btm_req_reason = atoi(value);
822 	} else if (os_strcasecmp(cmd, "get_pref_freq_list_override") == 0) {
823 		os_free(wpa_s->get_pref_freq_list_override);
824 		if (!value[0])
825 			wpa_s->get_pref_freq_list_override = NULL;
826 		else
827 			wpa_s->get_pref_freq_list_override = os_strdup(value);
828 	} else if (os_strcasecmp(cmd, "sae_commit_override") == 0) {
829 		wpabuf_free(wpa_s->sae_commit_override);
830 		if (value[0] == '\0')
831 			wpa_s->sae_commit_override = NULL;
832 		else
833 			wpa_s->sae_commit_override = wpabuf_parse_bin(value);
834 	} else if (os_strcasecmp(cmd, "driver_signal_override") == 0) {
835 		ret = wpas_ctrl_iface_set_dso(wpa_s, value);
836 	} else if (os_strcasecmp(cmd, "disable_scs_support") == 0) {
837 		wpa_s->disable_scs_support = !!atoi(value);
838 	} else if (os_strcasecmp(cmd, "disable_mscs_support") == 0) {
839 		wpa_s->disable_mscs_support = !!atoi(value);
840 	} else if (os_strcasecmp(cmd, "disable_eapol_g2_tx") == 0) {
841 		wpa_s->disable_eapol_g2_tx = !!atoi(value);
842 		/* Populate value to wpa_sm if already associated. */
843 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_DISABLE_EAPOL_G2_TX,
844 				 wpa_s->disable_eapol_g2_tx);
845 #ifdef CONFIG_DPP
846 	} else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
847 		os_free(wpa_s->dpp_config_obj_override);
848 		if (value[0] == '\0')
849 			wpa_s->dpp_config_obj_override = NULL;
850 		else
851 			wpa_s->dpp_config_obj_override = os_strdup(value);
852 	} else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
853 		os_free(wpa_s->dpp_discovery_override);
854 		if (value[0] == '\0')
855 			wpa_s->dpp_discovery_override = NULL;
856 		else
857 			wpa_s->dpp_discovery_override = os_strdup(value);
858 	} else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
859 		os_free(wpa_s->dpp_groups_override);
860 		if (value[0] == '\0')
861 			wpa_s->dpp_groups_override = NULL;
862 		else
863 			wpa_s->dpp_groups_override = os_strdup(value);
864 	} else if (os_strcasecmp(cmd,
865 				 "dpp_ignore_netaccesskey_mismatch") == 0) {
866 		wpa_s->dpp_ignore_netaccesskey_mismatch = atoi(value);
867 	} else if (os_strcasecmp(cmd, "dpp_discard_public_action") == 0) {
868 		wpa_s->dpp_discard_public_action = atoi(value);
869 	} else if (os_strcasecmp(cmd, "dpp_test") == 0) {
870 		dpp_test = atoi(value);
871 #endif /* CONFIG_DPP */
872 #endif /* CONFIG_TESTING_OPTIONS */
873 #ifdef CONFIG_FILS
874 	} else if (os_strcasecmp(cmd, "disable_fils") == 0) {
875 		wpa_s->disable_fils = !!atoi(value);
876 		wpa_drv_disable_fils(wpa_s, wpa_s->disable_fils);
877 		wpa_supplicant_set_default_scan_ies(wpa_s);
878 #endif /* CONFIG_FILS */
879 #ifndef CONFIG_NO_CONFIG_BLOBS
880 	} else if (os_strcmp(cmd, "blob") == 0) {
881 		ret = wpas_ctrl_set_blob(wpa_s, value);
882 #endif /* CONFIG_NO_CONFIG_BLOBS */
883 	} else if (os_strcasecmp(cmd, "setband") == 0) {
884 		ret = wpas_ctrl_set_band(wpa_s, value);
885 #ifdef CONFIG_MBO
886 	} else if (os_strcasecmp(cmd, "non_pref_chan") == 0) {
887 		ret = wpas_mbo_update_non_pref_chan(wpa_s, value);
888 		if (ret == 0) {
889 			value[-1] = '=';
890 			wpa_config_process_global(wpa_s->conf, cmd, -1);
891 		}
892 	} else if (os_strcasecmp(cmd, "mbo_cell_capa") == 0) {
893 		wpas_mbo_update_cell_capa(wpa_s, atoi(value));
894 	} else if (os_strcasecmp(cmd, "oce") == 0) {
895 		wpa_s->conf->oce = atoi(value);
896 		if (wpa_s->conf->oce) {
897 			if ((wpa_s->conf->oce & OCE_STA) &&
898 			    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA))
899 				wpa_s->enable_oce = OCE_STA;
900 
901 			if ((wpa_s->conf->oce & OCE_STA_CFON) &&
902 			    (wpa_s->drv_flags &
903 			     WPA_DRIVER_FLAGS_OCE_STA_CFON)) {
904 				/* TODO: Need to add STA-CFON support */
905 				wpa_printf(MSG_ERROR,
906 					   "OCE STA-CFON feature is not yet supported");
907 				return -1;
908 			}
909 		} else {
910 			wpa_s->enable_oce = 0;
911 		}
912 		wpa_supplicant_set_default_scan_ies(wpa_s);
913 #endif /* CONFIG_MBO */
914 	} else if (os_strcasecmp(cmd, "lci") == 0) {
915 		ret = wpas_ctrl_iface_set_lci(wpa_s, value);
916 	} else if (os_strcasecmp(cmd, "tdls_trigger_control") == 0) {
917 		ret = wpa_drv_set_tdls_mode(wpa_s, atoi(value));
918 	} else if (os_strcasecmp(cmd, "relative_rssi") == 0) {
919 		ret = wpas_ctrl_set_relative_rssi(wpa_s, value);
920 	} else if (os_strcasecmp(cmd, "relative_band_adjust") == 0) {
921 		ret = wpas_ctrl_set_relative_band_adjust(wpa_s, value);
922 	} else if (os_strcasecmp(cmd, "ric_ies") == 0) {
923 		ret = wpas_ctrl_iface_set_ric_ies(wpa_s, value);
924 	} else if (os_strcasecmp(cmd, "roaming") == 0) {
925 		ret = wpa_drv_roaming(wpa_s, atoi(value), NULL);
926 #ifdef CONFIG_WNM
927 	} else if (os_strcasecmp(cmd, "coloc_intf_elems") == 0) {
928 		struct wpabuf *elems;
929 
930 		elems = wpabuf_parse_bin(value);
931 		if (!elems)
932 			return -1;
933 		wnm_set_coloc_intf_elems(wpa_s, elems);
934 #endif /* CONFIG_WNM */
935 	} else if (os_strcasecmp(cmd, "enable_dscp_policy_capa") == 0) {
936 		wpa_s->enable_dscp_policy_capa = !!atoi(value);
937 	} else {
938 		value[-1] = '=';
939 		ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
940 		if (ret == 0)
941 			wpa_supplicant_update_config(wpa_s);
942 		else if (ret == 1)
943 			ret = 0;
944 	}
945 
946 	return ret;
947 }
948 
949 
wpa_supplicant_ctrl_iface_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)950 static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
951 					 char *cmd, char *buf, size_t buflen)
952 {
953 	int res = -1;
954 
955 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
956 
957 	if (os_strcmp(cmd, "version") == 0) {
958 		res = os_snprintf(buf, buflen, "%s", VERSION_STR);
959 	} else if (os_strcasecmp(cmd, "max_command_len") == 0) {
960 		res = os_snprintf(buf, buflen, "%u", CTRL_IFACE_MAX_LEN);
961 	} else if (os_strcasecmp(cmd, "country") == 0) {
962 		if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
963 			res = os_snprintf(buf, buflen, "%c%c",
964 					  wpa_s->conf->country[0],
965 					  wpa_s->conf->country[1]);
966 #ifdef CONFIG_WIFI_DISPLAY
967 	} else if (os_strcasecmp(cmd, "wifi_display") == 0) {
968 		int enabled;
969 		if (wpa_s->global->p2p == NULL ||
970 		    wpa_s->global->p2p_disabled)
971 			enabled = 0;
972 		else
973 			enabled = wpa_s->global->wifi_display;
974 		res = os_snprintf(buf, buflen, "%d", enabled);
975 #endif /* CONFIG_WIFI_DISPLAY */
976 #ifdef CONFIG_TESTING_GET_GTK
977 	} else if (os_strcmp(cmd, "gtk") == 0) {
978 		if (wpa_s->last_gtk_len == 0)
979 			return -1;
980 		res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
981 				       wpa_s->last_gtk_len);
982 		return res;
983 #endif /* CONFIG_TESTING_GET_GTK */
984 	} else if (os_strcmp(cmd, "tls_library") == 0) {
985 		res = tls_get_library_version(buf, buflen);
986 #ifdef CONFIG_TESTING_OPTIONS
987 	} else if (os_strcmp(cmd, "anonce") == 0) {
988 		return wpa_snprintf_hex(buf, buflen,
989 					wpa_sm_get_anonce(wpa_s->wpa),
990 					WPA_NONCE_LEN);
991 	} else if (os_strcasecmp(cmd, "last_tk_key_idx") == 0) {
992 		res = os_snprintf(buf, buflen, "%d", wpa_s->last_tk_key_idx);
993 #endif /* CONFIG_TESTING_OPTIONS */
994 	} else {
995 		res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
996 	}
997 
998 	if (os_snprintf_error(buflen, res))
999 		return -1;
1000 	return res;
1001 }
1002 
1003 
1004 #ifdef IEEE8021X_EAPOL
wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant * wpa_s,char * addr)1005 static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
1006 					     char *addr)
1007 {
1008 	u8 bssid[ETH_ALEN];
1009 	struct wpa_ssid *ssid = wpa_s->current_ssid;
1010 
1011 	if (hwaddr_aton(addr, bssid)) {
1012 		wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
1013 			   "'%s'", addr);
1014 		return -1;
1015 	}
1016 
1017 	wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
1018 	rsn_preauth_deinit(wpa_s->wpa);
1019 	if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
1020 		return -1;
1021 
1022 	return 0;
1023 }
1024 #endif /* IEEE8021X_EAPOL */
1025 
1026 
1027 #ifdef CONFIG_TDLS
1028 
wpa_supplicant_ctrl_iface_tdls_discover(struct wpa_supplicant * wpa_s,char * addr)1029 static int wpa_supplicant_ctrl_iface_tdls_discover(
1030 	struct wpa_supplicant *wpa_s, char *addr)
1031 {
1032 	u8 peer[ETH_ALEN];
1033 	int ret;
1034 
1035 	if (hwaddr_aton(addr, peer)) {
1036 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
1037 			   "address '%s'", addr);
1038 		return -1;
1039 	}
1040 
1041 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
1042 		   MAC2STR(peer));
1043 
1044 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1045 		ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
1046 	else
1047 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
1048 
1049 	return ret;
1050 }
1051 
1052 
wpa_supplicant_ctrl_iface_tdls_setup(struct wpa_supplicant * wpa_s,char * addr)1053 static int wpa_supplicant_ctrl_iface_tdls_setup(
1054 	struct wpa_supplicant *wpa_s, char *addr)
1055 {
1056 	u8 peer[ETH_ALEN];
1057 	int ret;
1058 
1059 	if (hwaddr_aton(addr, peer)) {
1060 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
1061 			   "address '%s'", addr);
1062 		return -1;
1063 	}
1064 
1065 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
1066 		   MAC2STR(peer));
1067 
1068 	if ((wpa_s->conf->tdls_external_control) &&
1069 	    wpa_tdls_is_external_setup(wpa_s->wpa))
1070 		return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
1071 
1072 	wpa_tdls_remove(wpa_s->wpa, peer);
1073 
1074 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1075 		ret = wpa_tdls_start(wpa_s->wpa, peer);
1076 	else
1077 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
1078 
1079 	return ret;
1080 }
1081 
1082 
wpa_supplicant_ctrl_iface_tdls_teardown(struct wpa_supplicant * wpa_s,char * addr)1083 static int wpa_supplicant_ctrl_iface_tdls_teardown(
1084 	struct wpa_supplicant *wpa_s, char *addr)
1085 {
1086 	u8 peer[ETH_ALEN];
1087 	int ret;
1088 
1089 	if (os_strcmp(addr, "*") == 0) {
1090 		/* remove everyone */
1091 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
1092 		wpa_tdls_teardown_peers(wpa_s->wpa);
1093 		return 0;
1094 	}
1095 
1096 	if (hwaddr_aton(addr, peer)) {
1097 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
1098 			   "address '%s'", addr);
1099 		return -1;
1100 	}
1101 
1102 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
1103 		   MAC2STR(peer));
1104 
1105 	if ((wpa_s->conf->tdls_external_control) &&
1106 	    wpa_tdls_is_external_setup(wpa_s->wpa))
1107 		return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
1108 
1109 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1110 		ret = wpa_tdls_teardown_link(
1111 			wpa_s->wpa, peer,
1112 			WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
1113 	else
1114 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
1115 
1116 	return ret;
1117 }
1118 
1119 
ctrl_iface_get_capability_tdls(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)1120 static int ctrl_iface_get_capability_tdls(
1121 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1122 {
1123 	int ret;
1124 
1125 	ret = os_snprintf(buf, buflen, "%s\n",
1126 			  wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
1127 			  (wpa_s->drv_flags &
1128 			   WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
1129 			   "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
1130 	if (os_snprintf_error(buflen, ret))
1131 		return -1;
1132 	return ret;
1133 }
1134 
1135 
wpa_supplicant_ctrl_iface_tdls_chan_switch(struct wpa_supplicant * wpa_s,char * cmd)1136 static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
1137 	struct wpa_supplicant *wpa_s, char *cmd)
1138 {
1139 	u8 peer[ETH_ALEN];
1140 	struct hostapd_freq_params freq_params;
1141 	u8 oper_class;
1142 	char *pos, *end;
1143 
1144 	if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
1145 		wpa_printf(MSG_INFO,
1146 			   "tdls_chanswitch: Only supported with external setup");
1147 		return -1;
1148 	}
1149 
1150 	os_memset(&freq_params, 0, sizeof(freq_params));
1151 
1152 	pos = os_strchr(cmd, ' ');
1153 	if (pos == NULL)
1154 		return -1;
1155 	*pos++ = '\0';
1156 
1157 	oper_class = strtol(pos, &end, 10);
1158 	if (pos == end) {
1159 		wpa_printf(MSG_INFO,
1160 			   "tdls_chanswitch: Invalid op class provided");
1161 		return -1;
1162 	}
1163 
1164 	pos = end;
1165 	freq_params.freq = atoi(pos);
1166 	if (freq_params.freq == 0) {
1167 		wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
1168 		return -1;
1169 	}
1170 
1171 #define SET_FREQ_SETTING(str) \
1172 	do { \
1173 		const char *pos2 = os_strstr(pos, " " #str "="); \
1174 		if (pos2) { \
1175 			pos2 += sizeof(" " #str "=") - 1; \
1176 			freq_params.str = atoi(pos2); \
1177 		} \
1178 	} while (0)
1179 
1180 	SET_FREQ_SETTING(center_freq1);
1181 	SET_FREQ_SETTING(center_freq2);
1182 	SET_FREQ_SETTING(bandwidth);
1183 	SET_FREQ_SETTING(sec_channel_offset);
1184 #undef SET_FREQ_SETTING
1185 
1186 	freq_params.ht_enabled = !!os_strstr(pos, " ht");
1187 	freq_params.vht_enabled = !!os_strstr(pos, " vht");
1188 
1189 	if (hwaddr_aton(cmd, peer)) {
1190 		wpa_printf(MSG_DEBUG,
1191 			   "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
1192 			   cmd);
1193 		return -1;
1194 	}
1195 
1196 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
1197 		   " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
1198 		   MAC2STR(peer), oper_class, freq_params.freq,
1199 		   freq_params.center_freq1, freq_params.center_freq2,
1200 		   freq_params.bandwidth, freq_params.sec_channel_offset,
1201 		   freq_params.ht_enabled ? " HT" : "",
1202 		   freq_params.vht_enabled ? " VHT" : "");
1203 
1204 	return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
1205 					   &freq_params);
1206 }
1207 
1208 
wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(struct wpa_supplicant * wpa_s,char * cmd)1209 static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
1210 	struct wpa_supplicant *wpa_s, char *cmd)
1211 {
1212 	u8 peer[ETH_ALEN];
1213 
1214 	if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
1215 		wpa_printf(MSG_INFO,
1216 			   "tdls_chanswitch: Only supported with external setup");
1217 		return -1;
1218 	}
1219 
1220 	if (hwaddr_aton(cmd, peer)) {
1221 		wpa_printf(MSG_DEBUG,
1222 			   "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
1223 			   cmd);
1224 		return -1;
1225 	}
1226 
1227 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
1228 		   MAC2STR(peer));
1229 
1230 	return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
1231 }
1232 
1233 
wpa_supplicant_ctrl_iface_tdls_link_status(struct wpa_supplicant * wpa_s,const char * addr,char * buf,size_t buflen)1234 static int wpa_supplicant_ctrl_iface_tdls_link_status(
1235 	struct wpa_supplicant *wpa_s, const char *addr,
1236 	char *buf, size_t buflen)
1237 {
1238 	u8 peer[ETH_ALEN];
1239 	const char *tdls_status;
1240 	int ret;
1241 
1242 	if (hwaddr_aton(addr, peer)) {
1243 		wpa_printf(MSG_DEBUG,
1244 			   "CTRL_IFACE TDLS_LINK_STATUS: Invalid address '%s'",
1245 			   addr);
1246 		return -1;
1247 	}
1248 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS " MACSTR,
1249 		   MAC2STR(peer));
1250 
1251 	tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
1252 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS: %s", tdls_status);
1253 	ret = os_snprintf(buf, buflen, "TDLS link status: %s\n", tdls_status);
1254 	if (os_snprintf_error(buflen, ret))
1255 		return -1;
1256 
1257 	return ret;
1258 }
1259 
1260 #endif /* CONFIG_TDLS */
1261 
1262 
wmm_ac_ctrl_addts(struct wpa_supplicant * wpa_s,char * cmd)1263 static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
1264 {
1265 	char *token, *context = NULL;
1266 	struct wmm_ac_ts_setup_params params = {
1267 		.tsid = 0xff,
1268 		.direction = 0xff,
1269 	};
1270 
1271 	while ((token = str_token(cmd, " ", &context))) {
1272 		if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
1273 		    sscanf(token, "up=%i", &params.user_priority) == 1 ||
1274 		    sscanf(token, "nominal_msdu_size=%i",
1275 			   &params.nominal_msdu_size) == 1 ||
1276 		    sscanf(token, "mean_data_rate=%i",
1277 			   &params.mean_data_rate) == 1 ||
1278 		    sscanf(token, "min_phy_rate=%i",
1279 			   &params.minimum_phy_rate) == 1 ||
1280 		    sscanf(token, "sba=%i",
1281 			   &params.surplus_bandwidth_allowance) == 1)
1282 			continue;
1283 
1284 		if (os_strcasecmp(token, "downlink") == 0) {
1285 			params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
1286 		} else if (os_strcasecmp(token, "uplink") == 0) {
1287 			params.direction = WMM_TSPEC_DIRECTION_UPLINK;
1288 		} else if (os_strcasecmp(token, "bidi") == 0) {
1289 			params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
1290 		} else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
1291 			params.fixed_nominal_msdu = 1;
1292 		} else {
1293 			wpa_printf(MSG_DEBUG,
1294 				   "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
1295 				   token);
1296 			return -1;
1297 		}
1298 
1299 	}
1300 
1301 	return wpas_wmm_ac_addts(wpa_s, &params);
1302 }
1303 
1304 
wmm_ac_ctrl_delts(struct wpa_supplicant * wpa_s,char * cmd)1305 static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
1306 {
1307 	u8 tsid = atoi(cmd);
1308 
1309 	return wpas_wmm_ac_delts(wpa_s, tsid);
1310 }
1311 
1312 
1313 #ifdef CONFIG_IEEE80211R
wpa_supplicant_ctrl_iface_ft_ds(struct wpa_supplicant * wpa_s,char * addr)1314 static int wpa_supplicant_ctrl_iface_ft_ds(
1315 	struct wpa_supplicant *wpa_s, char *addr)
1316 {
1317 	u8 target_ap[ETH_ALEN];
1318 	struct wpa_bss *bss;
1319 	const u8 *mdie;
1320 	bool force = os_strstr(addr, " force") != NULL;
1321 
1322 	if (hwaddr_aton(addr, target_ap)) {
1323 		wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
1324 			   "address '%s'", addr);
1325 		return -1;
1326 	}
1327 
1328 	wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
1329 
1330 	bss = wpa_bss_get_bssid(wpa_s, target_ap);
1331 	if (bss)
1332 		mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1333 	else
1334 		mdie = NULL;
1335 
1336 	return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie, force);
1337 }
1338 #endif /* CONFIG_IEEE80211R */
1339 
1340 
1341 #ifdef CONFIG_WPS
wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant * wpa_s,char * cmd)1342 static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
1343 					     char *cmd)
1344 {
1345 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1346 #ifdef CONFIG_P2P
1347 	u8 p2p_dev_addr[ETH_ALEN];
1348 #endif /* CONFIG_P2P */
1349 #ifdef CONFIG_AP
1350 	u8 *_p2p_dev_addr = NULL;
1351 #endif /* CONFIG_AP */
1352 	char *pos;
1353 	int multi_ap = 0;
1354 
1355 	if (!cmd || os_strcmp(cmd, "any") == 0 ||
1356 	    os_strncmp(cmd, "any ", 4) == 0) {
1357 		_bssid = NULL;
1358 #ifdef CONFIG_P2P
1359 	} else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
1360 		if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
1361 			wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
1362 				   "P2P Device Address '%s'",
1363 				   cmd + 13);
1364 			return -1;
1365 		}
1366 		_p2p_dev_addr = p2p_dev_addr;
1367 #endif /* CONFIG_P2P */
1368 	} else if (os_strncmp(cmd, "multi_ap=", 9) == 0) {
1369 		_bssid = NULL;
1370 		multi_ap = atoi(cmd + 9);
1371 	} else if (hwaddr_aton(cmd, bssid)) {
1372 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
1373 			   cmd);
1374 		return -1;
1375 	}
1376 
1377 	if (cmd) {
1378 		pos = os_strstr(cmd, " multi_ap=");
1379 		if (pos) {
1380 			pos += 10;
1381 			multi_ap = atoi(pos);
1382 		}
1383 	}
1384 
1385 #ifdef CONFIG_AP
1386 	if (wpa_s->ap_iface)
1387 		return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
1388 #endif /* CONFIG_AP */
1389 
1390 	return wpas_wps_start_pbc(wpa_s, _bssid, 0, multi_ap);
1391 }
1392 
1393 
wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1394 static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
1395 					     char *cmd, char *buf,
1396 					     size_t buflen)
1397 {
1398 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1399 	char *pin;
1400 	int ret;
1401 
1402 	pin = os_strchr(cmd, ' ');
1403 	if (pin)
1404 		*pin++ = '\0';
1405 
1406 	if (os_strcmp(cmd, "any") == 0)
1407 		_bssid = NULL;
1408 	else if (os_strcmp(cmd, "get") == 0) {
1409 		if (wps_generate_pin((unsigned int *) &ret) < 0)
1410 			return -1;
1411 		goto done;
1412 	} else if (hwaddr_aton(cmd, bssid)) {
1413 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
1414 			   cmd);
1415 		return -1;
1416 	}
1417 
1418 #ifdef CONFIG_AP
1419 	if (wpa_s->ap_iface) {
1420 		int timeout = 0;
1421 		char *pos;
1422 
1423 		if (pin) {
1424 			pos = os_strchr(pin, ' ');
1425 			if (pos) {
1426 				*pos++ = '\0';
1427 				timeout = atoi(pos);
1428 			}
1429 		}
1430 
1431 		return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
1432 						 buf, buflen, timeout);
1433 	}
1434 #endif /* CONFIG_AP */
1435 
1436 	if (pin) {
1437 		ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
1438 					 DEV_PW_DEFAULT);
1439 		if (ret < 0)
1440 			return -1;
1441 		ret = os_snprintf(buf, buflen, "%s", pin);
1442 		if (os_snprintf_error(buflen, ret))
1443 			return -1;
1444 		return ret;
1445 	}
1446 
1447 	ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
1448 	if (ret < 0)
1449 		return -1;
1450 
1451 done:
1452 	/* Return the generated PIN */
1453 	ret = os_snprintf(buf, buflen, "%08d", ret);
1454 	if (os_snprintf_error(buflen, ret))
1455 		return -1;
1456 	return ret;
1457 }
1458 
1459 
wpa_supplicant_ctrl_iface_wps_check_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1460 static int wpa_supplicant_ctrl_iface_wps_check_pin(
1461 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1462 {
1463 	char pin[9];
1464 	size_t len;
1465 	char *pos;
1466 	int ret;
1467 
1468 	wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
1469 			      (u8 *) cmd, os_strlen(cmd));
1470 	for (pos = cmd, len = 0; *pos != '\0'; pos++) {
1471 		if (*pos < '0' || *pos > '9')
1472 			continue;
1473 		pin[len++] = *pos;
1474 		if (len == 9) {
1475 			wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
1476 			return -1;
1477 		}
1478 	}
1479 	if (len != 4 && len != 8) {
1480 		wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
1481 		return -1;
1482 	}
1483 	pin[len] = '\0';
1484 
1485 	if (len == 8) {
1486 		unsigned int pin_val;
1487 		pin_val = atoi(pin);
1488 		if (!wps_pin_valid(pin_val)) {
1489 			wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
1490 			ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
1491 			if (os_snprintf_error(buflen, ret))
1492 				return -1;
1493 			return ret;
1494 		}
1495 	}
1496 
1497 	ret = os_snprintf(buf, buflen, "%s", pin);
1498 	if (os_snprintf_error(buflen, ret))
1499 		return -1;
1500 
1501 	return ret;
1502 }
1503 
1504 
1505 #ifdef CONFIG_WPS_NFC
1506 
wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant * wpa_s,char * cmd)1507 static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
1508 					     char *cmd)
1509 {
1510 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1511 
1512 	if (cmd == NULL || cmd[0] == '\0')
1513 		_bssid = NULL;
1514 	else if (hwaddr_aton(cmd, bssid))
1515 		return -1;
1516 
1517 	return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
1518 				  0, 0);
1519 }
1520 
1521 
wpa_supplicant_ctrl_iface_wps_nfc_config_token(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1522 static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
1523 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1524 {
1525 	int ndef;
1526 	struct wpabuf *buf;
1527 	int res;
1528 	char *pos;
1529 
1530 	pos = os_strchr(cmd, ' ');
1531 	if (pos)
1532 		*pos++ = '\0';
1533 	if (os_strcmp(cmd, "WPS") == 0)
1534 		ndef = 0;
1535 	else if (os_strcmp(cmd, "NDEF") == 0)
1536 		ndef = 1;
1537 	else
1538 		return -1;
1539 
1540 	buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
1541 	if (buf == NULL)
1542 		return -1;
1543 
1544 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1545 					 wpabuf_len(buf));
1546 	reply[res++] = '\n';
1547 	reply[res] = '\0';
1548 
1549 	wpabuf_free(buf);
1550 
1551 	return res;
1552 }
1553 
1554 
wpa_supplicant_ctrl_iface_wps_nfc_token(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1555 static int wpa_supplicant_ctrl_iface_wps_nfc_token(
1556 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1557 {
1558 	int ndef;
1559 	struct wpabuf *buf;
1560 	int res;
1561 
1562 	if (os_strcmp(cmd, "WPS") == 0)
1563 		ndef = 0;
1564 	else if (os_strcmp(cmd, "NDEF") == 0)
1565 		ndef = 1;
1566 	else
1567 		return -1;
1568 
1569 	buf = wpas_wps_nfc_token(wpa_s, ndef);
1570 	if (buf == NULL)
1571 		return -1;
1572 
1573 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1574 					 wpabuf_len(buf));
1575 	reply[res++] = '\n';
1576 	reply[res] = '\0';
1577 
1578 	wpabuf_free(buf);
1579 
1580 	return res;
1581 }
1582 
1583 
wpa_supplicant_ctrl_iface_wps_nfc_tag_read(struct wpa_supplicant * wpa_s,char * pos)1584 static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
1585 	struct wpa_supplicant *wpa_s, char *pos)
1586 {
1587 	size_t len;
1588 	struct wpabuf *buf;
1589 	int ret;
1590 	char *freq;
1591 	int forced_freq = 0;
1592 
1593 	freq = strstr(pos, " freq=");
1594 	if (freq) {
1595 		*freq = '\0';
1596 		freq += 6;
1597 		forced_freq = atoi(freq);
1598 	}
1599 
1600 	len = os_strlen(pos);
1601 	if (len & 0x01)
1602 		return -1;
1603 	len /= 2;
1604 
1605 	buf = wpabuf_alloc(len);
1606 	if (buf == NULL)
1607 		return -1;
1608 	if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
1609 		wpabuf_free(buf);
1610 		return -1;
1611 	}
1612 
1613 	ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
1614 	wpabuf_free(buf);
1615 
1616 	return ret;
1617 }
1618 
1619 
wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef)1620 static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
1621 					      char *reply, size_t max_len,
1622 					      int ndef)
1623 {
1624 	struct wpabuf *buf;
1625 	int res;
1626 
1627 	buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
1628 	if (buf == NULL)
1629 		return -1;
1630 
1631 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1632 					 wpabuf_len(buf));
1633 	reply[res++] = '\n';
1634 	reply[res] = '\0';
1635 
1636 	wpabuf_free(buf);
1637 
1638 	return res;
1639 }
1640 
1641 
1642 #ifdef CONFIG_P2P
wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef)1643 static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
1644 					      char *reply, size_t max_len,
1645 					      int ndef)
1646 {
1647 	struct wpabuf *buf;
1648 	int res;
1649 
1650 	buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
1651 	if (buf == NULL) {
1652 		wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
1653 		return -1;
1654 	}
1655 
1656 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1657 					 wpabuf_len(buf));
1658 	reply[res++] = '\n';
1659 	reply[res] = '\0';
1660 
1661 	wpabuf_free(buf);
1662 
1663 	return res;
1664 }
1665 #endif /* CONFIG_P2P */
1666 
1667 
wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1668 static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
1669 					  char *cmd, char *reply,
1670 					  size_t max_len)
1671 {
1672 	char *pos;
1673 	int ndef;
1674 
1675 	pos = os_strchr(cmd, ' ');
1676 	if (pos == NULL)
1677 		return -1;
1678 	*pos++ = '\0';
1679 
1680 	if (os_strcmp(cmd, "WPS") == 0)
1681 		ndef = 0;
1682 	else if (os_strcmp(cmd, "NDEF") == 0)
1683 		ndef = 1;
1684 	else
1685 		return -1;
1686 
1687 	if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
1688 		if (!ndef)
1689 			return -1;
1690 		return wpas_ctrl_nfc_get_handover_req_wps(
1691 			wpa_s, reply, max_len, ndef);
1692 	}
1693 
1694 #ifdef CONFIG_P2P
1695 	if (os_strcmp(pos, "P2P-CR") == 0) {
1696 		return wpas_ctrl_nfc_get_handover_req_p2p(
1697 			wpa_s, reply, max_len, ndef);
1698 	}
1699 #endif /* CONFIG_P2P */
1700 
1701 	return -1;
1702 }
1703 
1704 
wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef,int cr,char * uuid)1705 static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
1706 					      char *reply, size_t max_len,
1707 					      int ndef, int cr, char *uuid)
1708 {
1709 	struct wpabuf *buf;
1710 	int res;
1711 
1712 	buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
1713 	if (buf == NULL)
1714 		return -1;
1715 
1716 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1717 					 wpabuf_len(buf));
1718 	reply[res++] = '\n';
1719 	reply[res] = '\0';
1720 
1721 	wpabuf_free(buf);
1722 
1723 	return res;
1724 }
1725 
1726 
1727 #ifdef CONFIG_P2P
wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef,int tag)1728 static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1729 					      char *reply, size_t max_len,
1730 					      int ndef, int tag)
1731 {
1732 	struct wpabuf *buf;
1733 	int res;
1734 
1735 	buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1736 	if (buf == NULL)
1737 		return -1;
1738 
1739 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1740 					 wpabuf_len(buf));
1741 	reply[res++] = '\n';
1742 	reply[res] = '\0';
1743 
1744 	wpabuf_free(buf);
1745 
1746 	return res;
1747 }
1748 #endif /* CONFIG_P2P */
1749 
1750 
wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1751 static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1752 					  char *cmd, char *reply,
1753 					  size_t max_len)
1754 {
1755 	char *pos, *pos2;
1756 	int ndef;
1757 
1758 	pos = os_strchr(cmd, ' ');
1759 	if (pos == NULL)
1760 		return -1;
1761 	*pos++ = '\0';
1762 
1763 	if (os_strcmp(cmd, "WPS") == 0)
1764 		ndef = 0;
1765 	else if (os_strcmp(cmd, "NDEF") == 0)
1766 		ndef = 1;
1767 	else
1768 		return -1;
1769 
1770 	pos2 = os_strchr(pos, ' ');
1771 	if (pos2)
1772 		*pos2++ = '\0';
1773 	if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
1774 		if (!ndef)
1775 			return -1;
1776 		return wpas_ctrl_nfc_get_handover_sel_wps(
1777 			wpa_s, reply, max_len, ndef,
1778 			os_strcmp(pos, "WPS-CR") == 0, pos2);
1779 	}
1780 
1781 #ifdef CONFIG_P2P
1782 	if (os_strcmp(pos, "P2P-CR") == 0) {
1783 		return wpas_ctrl_nfc_get_handover_sel_p2p(
1784 			wpa_s, reply, max_len, ndef, 0);
1785 	}
1786 
1787 	if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1788 		return wpas_ctrl_nfc_get_handover_sel_p2p(
1789 			wpa_s, reply, max_len, ndef, 1);
1790 	}
1791 #endif /* CONFIG_P2P */
1792 
1793 	return -1;
1794 }
1795 
1796 
wpas_ctrl_nfc_report_handover(struct wpa_supplicant * wpa_s,char * cmd)1797 static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1798 					 char *cmd)
1799 {
1800 	size_t len;
1801 	struct wpabuf *req, *sel;
1802 	int ret;
1803 	char *pos, *role, *type, *pos2;
1804 #ifdef CONFIG_P2P
1805 	char *freq;
1806 	int forced_freq = 0;
1807 
1808 	freq = strstr(cmd, " freq=");
1809 	if (freq) {
1810 		*freq = '\0';
1811 		freq += 6;
1812 		forced_freq = atoi(freq);
1813 	}
1814 #endif /* CONFIG_P2P */
1815 
1816 	role = cmd;
1817 	pos = os_strchr(role, ' ');
1818 	if (pos == NULL) {
1819 		wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
1820 		return -1;
1821 	}
1822 	*pos++ = '\0';
1823 
1824 	type = pos;
1825 	pos = os_strchr(type, ' ');
1826 	if (pos == NULL) {
1827 		wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
1828 		return -1;
1829 	}
1830 	*pos++ = '\0';
1831 
1832 	pos2 = os_strchr(pos, ' ');
1833 	if (pos2 == NULL) {
1834 		wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
1835 		return -1;
1836 	}
1837 	*pos2++ = '\0';
1838 
1839 	len = os_strlen(pos);
1840 	if (len & 0x01) {
1841 		wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
1842 		return -1;
1843 	}
1844 	len /= 2;
1845 
1846 	req = wpabuf_alloc(len);
1847 	if (req == NULL) {
1848 		wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
1849 		return -1;
1850 	}
1851 	if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
1852 		wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
1853 		wpabuf_free(req);
1854 		return -1;
1855 	}
1856 
1857 	len = os_strlen(pos2);
1858 	if (len & 0x01) {
1859 		wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
1860 		wpabuf_free(req);
1861 		return -1;
1862 	}
1863 	len /= 2;
1864 
1865 	sel = wpabuf_alloc(len);
1866 	if (sel == NULL) {
1867 		wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
1868 		wpabuf_free(req);
1869 		return -1;
1870 	}
1871 	if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
1872 		wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
1873 		wpabuf_free(req);
1874 		wpabuf_free(sel);
1875 		return -1;
1876 	}
1877 
1878 	wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1879 		   role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1880 
1881 	if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1882 		ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
1883 #ifdef CONFIG_AP
1884 	} else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1885 	{
1886 		ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
1887 		if (ret < 0)
1888 			ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
1889 #endif /* CONFIG_AP */
1890 #ifdef CONFIG_P2P
1891 	} else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1892 	{
1893 		ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
1894 	} else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1895 	{
1896 		ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1897 						   forced_freq);
1898 #endif /* CONFIG_P2P */
1899 	} else {
1900 		wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1901 			   "reported: role=%s type=%s", role, type);
1902 		ret = -1;
1903 	}
1904 	wpabuf_free(req);
1905 	wpabuf_free(sel);
1906 
1907 	if (ret)
1908 		wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1909 
1910 	return ret;
1911 }
1912 
1913 #endif /* CONFIG_WPS_NFC */
1914 
1915 
wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant * wpa_s,char * cmd)1916 static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1917 					     char *cmd)
1918 {
1919 	u8 bssid[ETH_ALEN];
1920 	char *pin;
1921 	char *new_ssid;
1922 	char *new_auth;
1923 	char *new_encr;
1924 	char *new_key;
1925 	struct wps_new_ap_settings ap;
1926 
1927 	pin = os_strchr(cmd, ' ');
1928 	if (pin == NULL)
1929 		return -1;
1930 	*pin++ = '\0';
1931 
1932 	if (hwaddr_aton(cmd, bssid)) {
1933 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1934 			   cmd);
1935 		return -1;
1936 	}
1937 
1938 	new_ssid = os_strchr(pin, ' ');
1939 	if (new_ssid == NULL)
1940 		return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1941 	*new_ssid++ = '\0';
1942 
1943 	new_auth = os_strchr(new_ssid, ' ');
1944 	if (new_auth == NULL)
1945 		return -1;
1946 	*new_auth++ = '\0';
1947 
1948 	new_encr = os_strchr(new_auth, ' ');
1949 	if (new_encr == NULL)
1950 		return -1;
1951 	*new_encr++ = '\0';
1952 
1953 	new_key = os_strchr(new_encr, ' ');
1954 	if (new_key == NULL)
1955 		return -1;
1956 	*new_key++ = '\0';
1957 
1958 	os_memset(&ap, 0, sizeof(ap));
1959 	ap.ssid_hex = new_ssid;
1960 	ap.auth = new_auth;
1961 	ap.encr = new_encr;
1962 	ap.key_hex = new_key;
1963 	return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1964 }
1965 
1966 
1967 #ifdef CONFIG_AP
wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1968 static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1969 						char *cmd, char *buf,
1970 						size_t buflen)
1971 {
1972 	int timeout = 300;
1973 	char *pos;
1974 	const char *pin_txt;
1975 
1976 	if (!wpa_s->ap_iface)
1977 		return -1;
1978 
1979 	pos = os_strchr(cmd, ' ');
1980 	if (pos)
1981 		*pos++ = '\0';
1982 
1983 	if (os_strcmp(cmd, "disable") == 0) {
1984 		wpas_wps_ap_pin_disable(wpa_s);
1985 		return os_snprintf(buf, buflen, "OK\n");
1986 	}
1987 
1988 	if (os_strcmp(cmd, "random") == 0) {
1989 		if (pos)
1990 			timeout = atoi(pos);
1991 		pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1992 		if (pin_txt == NULL)
1993 			return -1;
1994 		return os_snprintf(buf, buflen, "%s", pin_txt);
1995 	}
1996 
1997 	if (os_strcmp(cmd, "get") == 0) {
1998 		pin_txt = wpas_wps_ap_pin_get(wpa_s);
1999 		if (pin_txt == NULL)
2000 			return -1;
2001 		return os_snprintf(buf, buflen, "%s", pin_txt);
2002 	}
2003 
2004 	if (os_strcmp(cmd, "set") == 0) {
2005 		char *pin;
2006 		if (pos == NULL)
2007 			return -1;
2008 		pin = pos;
2009 		pos = os_strchr(pos, ' ');
2010 		if (pos) {
2011 			*pos++ = '\0';
2012 			timeout = atoi(pos);
2013 		}
2014 		if (os_strlen(pin) > buflen)
2015 			return -1;
2016 		if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
2017 			return -1;
2018 		return os_snprintf(buf, buflen, "%s", pin);
2019 	}
2020 
2021 	return -1;
2022 }
2023 #endif /* CONFIG_AP */
2024 
2025 
2026 #ifdef CONFIG_WPS_ER
wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant * wpa_s,char * cmd)2027 static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
2028 						char *cmd)
2029 {
2030 	char *uuid = cmd, *pin, *pos;
2031 	u8 addr_buf[ETH_ALEN], *addr = NULL;
2032 	pin = os_strchr(uuid, ' ');
2033 	if (pin == NULL)
2034 		return -1;
2035 	*pin++ = '\0';
2036 	pos = os_strchr(pin, ' ');
2037 	if (pos) {
2038 		*pos++ = '\0';
2039 		if (hwaddr_aton(pos, addr_buf) == 0)
2040 			addr = addr_buf;
2041 	}
2042 	return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
2043 }
2044 
2045 
wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant * wpa_s,char * cmd)2046 static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
2047 						  char *cmd)
2048 {
2049 	char *uuid = cmd, *pin;
2050 	pin = os_strchr(uuid, ' ');
2051 	if (pin == NULL)
2052 		return -1;
2053 	*pin++ = '\0';
2054 	return wpas_wps_er_learn(wpa_s, uuid, pin);
2055 }
2056 
2057 
wpa_supplicant_ctrl_iface_wps_er_set_config(struct wpa_supplicant * wpa_s,char * cmd)2058 static int wpa_supplicant_ctrl_iface_wps_er_set_config(
2059 	struct wpa_supplicant *wpa_s, char *cmd)
2060 {
2061 	char *uuid = cmd, *id;
2062 	id = os_strchr(uuid, ' ');
2063 	if (id == NULL)
2064 		return -1;
2065 	*id++ = '\0';
2066 	return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
2067 }
2068 
2069 
wpa_supplicant_ctrl_iface_wps_er_config(struct wpa_supplicant * wpa_s,char * cmd)2070 static int wpa_supplicant_ctrl_iface_wps_er_config(
2071 	struct wpa_supplicant *wpa_s, char *cmd)
2072 {
2073 	char *pin;
2074 	char *new_ssid;
2075 	char *new_auth;
2076 	char *new_encr;
2077 	char *new_key;
2078 	struct wps_new_ap_settings ap;
2079 
2080 	pin = os_strchr(cmd, ' ');
2081 	if (pin == NULL)
2082 		return -1;
2083 	*pin++ = '\0';
2084 
2085 	new_ssid = os_strchr(pin, ' ');
2086 	if (new_ssid == NULL)
2087 		return -1;
2088 	*new_ssid++ = '\0';
2089 
2090 	new_auth = os_strchr(new_ssid, ' ');
2091 	if (new_auth == NULL)
2092 		return -1;
2093 	*new_auth++ = '\0';
2094 
2095 	new_encr = os_strchr(new_auth, ' ');
2096 	if (new_encr == NULL)
2097 		return -1;
2098 	*new_encr++ = '\0';
2099 
2100 	new_key = os_strchr(new_encr, ' ');
2101 	if (new_key == NULL)
2102 		return -1;
2103 	*new_key++ = '\0';
2104 
2105 	os_memset(&ap, 0, sizeof(ap));
2106 	ap.ssid_hex = new_ssid;
2107 	ap.auth = new_auth;
2108 	ap.encr = new_encr;
2109 	ap.key_hex = new_key;
2110 	return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
2111 }
2112 
2113 
2114 #ifdef CONFIG_WPS_NFC
wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)2115 static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
2116 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
2117 {
2118 	int ndef;
2119 	struct wpabuf *buf;
2120 	int res;
2121 	char *uuid;
2122 
2123 	uuid = os_strchr(cmd, ' ');
2124 	if (uuid == NULL)
2125 		return -1;
2126 	*uuid++ = '\0';
2127 
2128 	if (os_strcmp(cmd, "WPS") == 0)
2129 		ndef = 0;
2130 	else if (os_strcmp(cmd, "NDEF") == 0)
2131 		ndef = 1;
2132 	else
2133 		return -1;
2134 
2135 	buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
2136 	if (buf == NULL)
2137 		return -1;
2138 
2139 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
2140 					 wpabuf_len(buf));
2141 	reply[res++] = '\n';
2142 	reply[res] = '\0';
2143 
2144 	wpabuf_free(buf);
2145 
2146 	return res;
2147 }
2148 #endif /* CONFIG_WPS_NFC */
2149 #endif /* CONFIG_WPS_ER */
2150 
2151 #endif /* CONFIG_WPS */
2152 
2153 
2154 #ifdef CONFIG_IBSS_RSN
wpa_supplicant_ctrl_iface_ibss_rsn(struct wpa_supplicant * wpa_s,char * addr)2155 static int wpa_supplicant_ctrl_iface_ibss_rsn(
2156 	struct wpa_supplicant *wpa_s, char *addr)
2157 {
2158 	u8 peer[ETH_ALEN];
2159 
2160 	if (hwaddr_aton(addr, peer)) {
2161 		wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
2162 			   "address '%s'", addr);
2163 		return -1;
2164 	}
2165 
2166 	wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
2167 		   MAC2STR(peer));
2168 
2169 	return ibss_rsn_start(wpa_s->ibss_rsn, peer);
2170 }
2171 #endif /* CONFIG_IBSS_RSN */
2172 
2173 
wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant * wpa_s,char * rsp)2174 static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
2175 					      char *rsp)
2176 {
2177 #ifdef IEEE8021X_EAPOL
2178 	char *pos, *id_pos;
2179 	int id;
2180 	struct wpa_ssid *ssid;
2181 
2182 	pos = os_strchr(rsp, '-');
2183 	if (pos == NULL)
2184 		return -1;
2185 	*pos++ = '\0';
2186 	id_pos = pos;
2187 	pos = os_strchr(pos, ':');
2188 	if (pos == NULL)
2189 		return -1;
2190 	*pos++ = '\0';
2191 	id = atoi(id_pos);
2192 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
2193 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2194 			      (u8 *) pos, os_strlen(pos));
2195 
2196 	ssid = wpa_config_get_network(wpa_s->conf, id);
2197 	if (ssid == NULL) {
2198 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2199 			   "to update", id);
2200 		return -1;
2201 	}
2202 
2203 	return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
2204 							 pos);
2205 #else /* IEEE8021X_EAPOL */
2206 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
2207 	return -1;
2208 #endif /* IEEE8021X_EAPOL */
2209 }
2210 
2211 
wpa_supplicant_ctrl_iface_status(struct wpa_supplicant * wpa_s,const char * params,char * buf,size_t buflen)2212 static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
2213 					    const char *params,
2214 					    char *buf, size_t buflen)
2215 {
2216 	char *pos, *end, tmp[30];
2217 	int res, verbose, wps, ret;
2218 #ifdef CONFIG_HS20
2219 	const u8 *hs20;
2220 #endif /* CONFIG_HS20 */
2221 	const u8 *sess_id;
2222 	size_t sess_id_len;
2223 
2224 	if (os_strcmp(params, "-DRIVER") == 0)
2225 		return wpa_drv_status(wpa_s, buf, buflen);
2226 	verbose = os_strcmp(params, "-VERBOSE") == 0;
2227 	wps = os_strcmp(params, "-WPS") == 0;
2228 	pos = buf;
2229 	end = buf + buflen;
2230 	if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
2231 		struct wpa_ssid *ssid = wpa_s->current_ssid;
2232 		ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
2233 				  MAC2STR(wpa_s->bssid));
2234 		if (os_snprintf_error(end - pos, ret))
2235 			return pos - buf;
2236 		pos += ret;
2237 		ret = os_snprintf(pos, end - pos, "freq=%u\n",
2238 				  wpa_s->assoc_freq);
2239 		if (os_snprintf_error(end - pos, ret))
2240 			return pos - buf;
2241 		pos += ret;
2242 		if (ssid) {
2243 			u8 *_ssid = ssid->ssid;
2244 			size_t ssid_len = ssid->ssid_len;
2245 			u8 ssid_buf[SSID_MAX_LEN];
2246 			if (ssid_len == 0) {
2247 				int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
2248 				if (_res < 0)
2249 					ssid_len = 0;
2250 				else
2251 					ssid_len = _res;
2252 				_ssid = ssid_buf;
2253 			}
2254 			ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
2255 					  wpa_ssid_txt(_ssid, ssid_len),
2256 					  ssid->id);
2257 			if (os_snprintf_error(end - pos, ret))
2258 				return pos - buf;
2259 			pos += ret;
2260 
2261 			if (wps && ssid->passphrase &&
2262 			    wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
2263 			    (ssid->mode == WPAS_MODE_AP ||
2264 			     ssid->mode == WPAS_MODE_P2P_GO)) {
2265 				ret = os_snprintf(pos, end - pos,
2266 						  "passphrase=%s\n",
2267 						  ssid->passphrase);
2268 				if (os_snprintf_error(end - pos, ret))
2269 					return pos - buf;
2270 				pos += ret;
2271 			}
2272 			if (ssid->id_str) {
2273 				ret = os_snprintf(pos, end - pos,
2274 						  "id_str=%s\n",
2275 						  ssid->id_str);
2276 				if (os_snprintf_error(end - pos, ret))
2277 					return pos - buf;
2278 				pos += ret;
2279 			}
2280 
2281 			switch (ssid->mode) {
2282 			case WPAS_MODE_INFRA:
2283 				ret = os_snprintf(pos, end - pos,
2284 						  "mode=station\n");
2285 				break;
2286 			case WPAS_MODE_IBSS:
2287 				ret = os_snprintf(pos, end - pos,
2288 						  "mode=IBSS\n");
2289 				break;
2290 			case WPAS_MODE_AP:
2291 				ret = os_snprintf(pos, end - pos,
2292 						  "mode=AP\n");
2293 				break;
2294 			case WPAS_MODE_P2P_GO:
2295 				ret = os_snprintf(pos, end - pos,
2296 						  "mode=P2P GO\n");
2297 				break;
2298 			case WPAS_MODE_P2P_GROUP_FORMATION:
2299 				ret = os_snprintf(pos, end - pos,
2300 						  "mode=P2P GO - group "
2301 						  "formation\n");
2302 				break;
2303 			case WPAS_MODE_MESH:
2304 				ret = os_snprintf(pos, end - pos,
2305 						  "mode=mesh\n");
2306 				break;
2307 			default:
2308 				ret = 0;
2309 				break;
2310 			}
2311 			if (os_snprintf_error(end - pos, ret))
2312 				return pos - buf;
2313 			pos += ret;
2314 		}
2315 
2316 		if (wpa_s->connection_set &&
2317 		    (wpa_s->connection_ht || wpa_s->connection_vht ||
2318 		     wpa_s->connection_he || wpa_s->connection_eht)) {
2319 			ret = os_snprintf(pos, end - pos,
2320 					  "wifi_generation=%u\n",
2321 					  wpa_s->connection_eht ? 7 :
2322 					  (wpa_s->connection_he ? 6 :
2323 					   (wpa_s->connection_vht ? 5 : 4)));
2324 			if (os_snprintf_error(end - pos, ret))
2325 				return pos - buf;
2326 			pos += ret;
2327 		}
2328 
2329 #ifdef CONFIG_AP
2330 		if (wpa_s->ap_iface) {
2331 			pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
2332 							    end - pos,
2333 							    verbose);
2334 		} else
2335 #endif /* CONFIG_AP */
2336 		pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
2337 	}
2338 #ifdef CONFIG_SME
2339 #ifdef CONFIG_SAE
2340 	if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
2341 #ifdef CONFIG_AP
2342 	    !wpa_s->ap_iface &&
2343 #endif /* CONFIG_AP */
2344 	    wpa_s->sme.sae.state == SAE_ACCEPTED) {
2345 		ret = os_snprintf(pos, end - pos, "sae_group=%d\n"
2346 				  "sae_h2e=%d\n"
2347 				  "sae_pk=%d\n",
2348 				  wpa_s->sme.sae.group,
2349 				  wpa_s->sme.sae.h2e,
2350 				  wpa_s->sme.sae.pk);
2351 		if (os_snprintf_error(end - pos, ret))
2352 			return pos - buf;
2353 		pos += ret;
2354 	}
2355 #endif /* CONFIG_SAE */
2356 #endif /* CONFIG_SME */
2357 	ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
2358 			  wpa_supplicant_state_txt(wpa_s->wpa_state));
2359 	if (os_snprintf_error(end - pos, ret))
2360 		return pos - buf;
2361 	pos += ret;
2362 
2363 	if (wpa_s->l2 &&
2364 	    l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
2365 		ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
2366 		if (os_snprintf_error(end - pos, ret))
2367 			return pos - buf;
2368 		pos += ret;
2369 	}
2370 
2371 #ifdef CONFIG_P2P
2372 	if (wpa_s->global->p2p) {
2373 		ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
2374 				  "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
2375 		if (os_snprintf_error(end - pos, ret))
2376 			return pos - buf;
2377 		pos += ret;
2378 	}
2379 #endif /* CONFIG_P2P */
2380 
2381 	ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
2382 			  MAC2STR(wpa_s->own_addr));
2383 	if (os_snprintf_error(end - pos, ret))
2384 		return pos - buf;
2385 	pos += ret;
2386 
2387 	if (wpa_s->valid_links) {
2388 		ret = os_snprintf(pos, end - pos, "ap_mld_addr=" MACSTR "\n",
2389 				  MAC2STR(wpa_s->ap_mld_addr));
2390 		if (os_snprintf_error(end - pos, ret))
2391 			return pos - buf;
2392 		pos += ret;
2393 	}
2394 
2395 #ifdef CONFIG_HS20
2396 	if (wpa_s->current_bss &&
2397 	    (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2398 					  HS20_IE_VENDOR_TYPE)) &&
2399 	    wpa_s->wpa_proto == WPA_PROTO_RSN &&
2400 	    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2401 		int release = 1;
2402 		if (hs20[1] >= 5) {
2403 			u8 rel_num = (hs20[6] & 0xf0) >> 4;
2404 			release = rel_num + 1;
2405 		}
2406 		ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
2407 		if (os_snprintf_error(end - pos, ret))
2408 			return pos - buf;
2409 		pos += ret;
2410 	}
2411 
2412 	if (wpa_s->current_ssid) {
2413 		struct wpa_cred *cred;
2414 		char *type;
2415 
2416 		for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2417 			size_t i;
2418 
2419 			if (wpa_s->current_ssid->parent_cred != cred)
2420 				continue;
2421 
2422 			if (cred->provisioning_sp) {
2423 				ret = os_snprintf(pos, end - pos,
2424 						  "provisioning_sp=%s\n",
2425 						  cred->provisioning_sp);
2426 				if (os_snprintf_error(end - pos, ret))
2427 					return pos - buf;
2428 				pos += ret;
2429 			}
2430 
2431 			if (!cred->domain)
2432 				goto no_domain;
2433 
2434 			i = 0;
2435 			if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
2436 				struct wpabuf *names =
2437 					wpa_s->current_bss->anqp->domain_name;
2438 				for (i = 0; names && i < cred->num_domain; i++)
2439 				{
2440 					if (domain_name_list_contains(
2441 						    names, cred->domain[i], 1))
2442 						break;
2443 				}
2444 				if (i == cred->num_domain)
2445 					i = 0; /* show first entry by default */
2446 			}
2447 			ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
2448 					  cred->domain[i]);
2449 			if (os_snprintf_error(end - pos, ret))
2450 				return pos - buf;
2451 			pos += ret;
2452 
2453 		no_domain:
2454 			if (wpa_s->current_bss == NULL ||
2455 			    wpa_s->current_bss->anqp == NULL)
2456 				res = -1;
2457 			else
2458 				res = interworking_home_sp_cred(
2459 					wpa_s, cred,
2460 					wpa_s->current_bss->anqp->domain_name);
2461 			if (res > 0)
2462 				type = "home";
2463 			else if (res == 0)
2464 				type = "roaming";
2465 			else
2466 				type = "unknown";
2467 
2468 			ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
2469 			if (os_snprintf_error(end - pos, ret))
2470 				return pos - buf;
2471 			pos += ret;
2472 
2473 			break;
2474 		}
2475 	}
2476 #endif /* CONFIG_HS20 */
2477 
2478 	if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2479 	    wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2480 		res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
2481 					  verbose);
2482 		if (res >= 0)
2483 			pos += res;
2484 	}
2485 
2486 #ifdef CONFIG_MACSEC
2487 	res = ieee802_1x_kay_get_status(wpa_s->kay, pos, end - pos);
2488 	if (res > 0)
2489 		pos += res;
2490 #endif /* CONFIG_MACSEC */
2491 
2492 	sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
2493 	if (sess_id) {
2494 		char *start = pos;
2495 
2496 		ret = os_snprintf(pos, end - pos, "eap_session_id=");
2497 		if (os_snprintf_error(end - pos, ret))
2498 			return start - buf;
2499 		pos += ret;
2500 		ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
2501 		if (ret <= 0)
2502 			return start - buf;
2503 		pos += ret;
2504 		ret = os_snprintf(pos, end - pos, "\n");
2505 		if (os_snprintf_error(end - pos, ret))
2506 			return start - buf;
2507 		pos += ret;
2508 	}
2509 
2510 	res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
2511 	if (res >= 0)
2512 		pos += res;
2513 
2514 #ifdef CONFIG_WPS
2515 	{
2516 		char uuid_str[100];
2517 		uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
2518 		ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
2519 		if (os_snprintf_error(end - pos, ret))
2520 			return pos - buf;
2521 		pos += ret;
2522 	}
2523 #endif /* CONFIG_WPS */
2524 
2525 	if (wpa_s->ieee80211ac) {
2526 		ret = os_snprintf(pos, end - pos, "ieee80211ac=1\n");
2527 		if (os_snprintf_error(end - pos, ret))
2528 			return pos - buf;
2529 		pos += ret;
2530 	}
2531 
2532 #ifdef ANDROID
2533 	/*
2534 	 * Allow using the STATUS command with default behavior, say for debug,
2535 	 * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
2536 	 * events with STATUS-NO_EVENTS.
2537 	 */
2538 	if (os_strcmp(params, "-NO_EVENTS")) {
2539 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
2540 			     "id=%d state=%d BSSID=" MACSTR " SSID=%s",
2541 			     wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
2542 			     wpa_s->wpa_state,
2543 			     MAC2STR(wpa_s->bssid),
2544 			     wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
2545 			     wpa_ssid_txt(wpa_s->current_ssid->ssid,
2546 					  wpa_s->current_ssid->ssid_len) : "");
2547 		if (wpa_s->wpa_state == WPA_COMPLETED) {
2548 			struct wpa_ssid *ssid = wpa_s->current_ssid;
2549 			char mld_addr[50];
2550 
2551 			mld_addr[0] = '\0';
2552 			if (wpa_s->valid_links)
2553 				os_snprintf(mld_addr, sizeof(mld_addr),
2554 					    " ap_mld_addr=" MACSTR,
2555 					    MAC2STR(wpa_s->ap_mld_addr));
2556 
2557 			wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
2558 				     "- connection to " MACSTR
2559 				     " completed %s [id=%d id_str=%s]%s",
2560 				     MAC2STR(wpa_s->bssid), "(auth)",
2561 				     ssid ? ssid->id : -1,
2562 				     ssid && ssid->id_str ? ssid->id_str : "",
2563 				     mld_addr);
2564 		}
2565 	}
2566 #endif /* ANDROID */
2567 
2568 	return pos - buf;
2569 }
2570 
2571 
wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant * wpa_s,char * cmd)2572 static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
2573 					   char *cmd)
2574 {
2575 	char *pos;
2576 	int id;
2577 	struct wpa_ssid *ssid;
2578 	u8 bssid[ETH_ALEN];
2579 
2580 	/* cmd: "<network id> <BSSID>" */
2581 	pos = os_strchr(cmd, ' ');
2582 	if (pos == NULL)
2583 		return -1;
2584 	*pos++ = '\0';
2585 	id = atoi(cmd);
2586 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
2587 	if (hwaddr_aton(pos, bssid)) {
2588 		wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
2589 		return -1;
2590 	}
2591 
2592 	ssid = wpa_config_get_network(wpa_s->conf, id);
2593 	if (ssid == NULL) {
2594 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2595 			   "to update", id);
2596 		return -1;
2597 	}
2598 
2599 	os_memcpy(ssid->bssid, bssid, ETH_ALEN);
2600 	ssid->bssid_set = !is_zero_ether_addr(bssid);
2601 
2602 	return 0;
2603 }
2604 
2605 
wpa_supplicant_ctrl_iface_bssid_ignore(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2606 static int wpa_supplicant_ctrl_iface_bssid_ignore(struct wpa_supplicant *wpa_s,
2607 						  char *cmd, char *buf,
2608 						  size_t buflen)
2609 {
2610 	u8 bssid[ETH_ALEN];
2611 	struct wpa_bssid_ignore *e;
2612 	char *pos, *end;
2613 	int ret;
2614 
2615 	/* cmd: "BSSID_IGNORE [<BSSID>]" */
2616 	if (*cmd == '\0') {
2617 		pos = buf;
2618 		end = buf + buflen;
2619 		e = wpa_s->bssid_ignore;
2620 		while (e) {
2621 			ret = os_snprintf(pos, end - pos, MACSTR "\n",
2622 					  MAC2STR(e->bssid));
2623 			if (os_snprintf_error(end - pos, ret))
2624 				return pos - buf;
2625 			pos += ret;
2626 			e = e->next;
2627 		}
2628 		return pos - buf;
2629 	}
2630 
2631 	cmd++;
2632 	if (os_strncmp(cmd, "clear", 5) == 0) {
2633 		wpa_bssid_ignore_clear(wpa_s);
2634 		os_memcpy(buf, "OK\n", 3);
2635 		return 3;
2636 	}
2637 
2638 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: BSSID_IGNORE bssid='%s'", cmd);
2639 	if (hwaddr_aton(cmd, bssid)) {
2640 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
2641 		return -1;
2642 	}
2643 
2644 	/*
2645 	 * Add the BSSID twice, so its count will be 2, causing it to be
2646 	 * skipped when processing scan results.
2647 	 */
2648 	ret = wpa_bssid_ignore_add(wpa_s, bssid);
2649 	if (ret < 0)
2650 		return -1;
2651 	ret = wpa_bssid_ignore_add(wpa_s, bssid);
2652 	if (ret < 0)
2653 		return -1;
2654 	os_memcpy(buf, "OK\n", 3);
2655 	return 3;
2656 }
2657 
2658 
wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2659 static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
2660 					       char *cmd, char *buf,
2661 					       size_t buflen)
2662 {
2663 	char *pos, *end, *stamp;
2664 	int ret;
2665 
2666 	/* cmd: "LOG_LEVEL [<level>]" */
2667 	if (*cmd == '\0') {
2668 		pos = buf;
2669 		end = buf + buflen;
2670 		ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2671 				  "Timestamp: %d\n",
2672 				  debug_level_str(wpa_debug_level),
2673 				  wpa_debug_timestamp);
2674 		if (os_snprintf_error(end - pos, ret))
2675 			ret = 0;
2676 
2677 		return ret;
2678 	}
2679 
2680 	while (*cmd == ' ')
2681 		cmd++;
2682 
2683 	stamp = os_strchr(cmd, ' ');
2684 	if (stamp) {
2685 		*stamp++ = '\0';
2686 		while (*stamp == ' ') {
2687 			stamp++;
2688 		}
2689 	}
2690 
2691 	if (os_strlen(cmd)) {
2692 		int level = str_to_debug_level(cmd);
2693 		if (level < 0)
2694 			return -1;
2695 		wpa_debug_level = level;
2696 	}
2697 
2698 	if (stamp && os_strlen(stamp))
2699 		wpa_debug_timestamp = atoi(stamp);
2700 
2701 	os_memcpy(buf, "OK\n", 3);
2702 	return 3;
2703 }
2704 
2705 
wpa_supplicant_ctrl_iface_list_networks(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2706 static int wpa_supplicant_ctrl_iface_list_networks(
2707 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2708 {
2709 	char *pos, *end, *prev;
2710 	struct wpa_ssid *ssid;
2711 	int ret;
2712 
2713 	pos = buf;
2714 	end = buf + buflen;
2715 	ret = os_snprintf(pos, end - pos,
2716 			  "network id / ssid / bssid / flags\n");
2717 	if (os_snprintf_error(end - pos, ret))
2718 		return pos - buf;
2719 	pos += ret;
2720 
2721 	ssid = wpa_s->conf->ssid;
2722 
2723 	/* skip over ssids until we find next one */
2724 	if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
2725 		int last_id = atoi(cmd + 8);
2726 		if (last_id != -1) {
2727 			while (ssid != NULL && ssid->id <= last_id) {
2728 				ssid = ssid->next;
2729 			}
2730 		}
2731 	}
2732 
2733 	while (ssid) {
2734 		prev = pos;
2735 		ret = os_snprintf(pos, end - pos, "%d\t%s",
2736 				  ssid->id,
2737 				  wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2738 		if (os_snprintf_error(end - pos, ret))
2739 			return prev - buf;
2740 		pos += ret;
2741 		if (ssid->bssid_set) {
2742 			ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2743 					  MAC2STR(ssid->bssid));
2744 		} else {
2745 			ret = os_snprintf(pos, end - pos, "\tany");
2746 		}
2747 		if (os_snprintf_error(end - pos, ret))
2748 			return prev - buf;
2749 		pos += ret;
2750 		ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
2751 				  ssid == wpa_s->current_ssid ?
2752 				  "[CURRENT]" : "",
2753 				  ssid->disabled ? "[DISABLED]" : "",
2754 				  ssid->disabled_until.sec ?
2755 				  "[TEMP-DISABLED]" : "",
2756 				  ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2757 				  "");
2758 		if (os_snprintf_error(end - pos, ret))
2759 			return prev - buf;
2760 		pos += ret;
2761 		ret = os_snprintf(pos, end - pos, "\n");
2762 		if (os_snprintf_error(end - pos, ret))
2763 			return prev - buf;
2764 		pos += ret;
2765 
2766 		ssid = ssid->next;
2767 	}
2768 
2769 	return pos - buf;
2770 }
2771 
2772 
wpa_supplicant_cipher_txt(char * pos,char * end,int cipher)2773 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2774 {
2775 	int ret;
2776 	ret = os_snprintf(pos, end - pos, "-");
2777 	if (os_snprintf_error(end - pos, ret))
2778 		return pos;
2779 	pos += ret;
2780 	ret = wpa_write_ciphers(pos, end, cipher, "+");
2781 	if (ret < 0)
2782 		return pos;
2783 	pos += ret;
2784 	return pos;
2785 }
2786 
2787 
wpa_supplicant_ie_txt(char * pos,char * end,const char * proto,const u8 * ie,size_t ie_len)2788 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2789 				    const u8 *ie, size_t ie_len)
2790 {
2791 	struct wpa_ie_data data;
2792 	char *start;
2793 	int ret;
2794 
2795 	ret = os_snprintf(pos, end - pos, "[%s-", proto);
2796 	if (os_snprintf_error(end - pos, ret))
2797 		return pos;
2798 	pos += ret;
2799 
2800 	if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2801 		ret = os_snprintf(pos, end - pos, "?]");
2802 		if (os_snprintf_error(end - pos, ret))
2803 			return pos;
2804 		pos += ret;
2805 		return pos;
2806 	}
2807 
2808 	start = pos;
2809 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
2810 		ret = os_snprintf(pos, end - pos, "%sEAP",
2811 				  pos == start ? "" : "+");
2812 		if (os_snprintf_error(end - pos, ret))
2813 			return pos;
2814 		pos += ret;
2815 	}
2816 	if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
2817 		ret = os_snprintf(pos, end - pos, "%sPSK",
2818 				  pos == start ? "" : "+");
2819 		if (os_snprintf_error(end - pos, ret))
2820 			return pos;
2821 		pos += ret;
2822 	}
2823 	if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
2824 		ret = os_snprintf(pos, end - pos, "%sNone",
2825 				  pos == start ? "" : "+");
2826 		if (os_snprintf_error(end - pos, ret))
2827 			return pos;
2828 		pos += ret;
2829 	}
2830 	if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
2831 		ret = os_snprintf(pos, end - pos, "%sSAE",
2832 				  pos == start ? "" : "+");
2833 		if (os_snprintf_error(end - pos, ret))
2834 			return pos;
2835 		pos += ret;
2836 	}
2837 	if (data.key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
2838 		ret = os_snprintf(pos, end - pos, "%sSAE-EXT-KEY",
2839 				  pos == start ? "" : "+");
2840 		if (os_snprintf_error(end - pos, ret))
2841 			return pos;
2842 		pos += ret;
2843 	}
2844 #ifdef CONFIG_IEEE80211R
2845 	if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2846 		ret = os_snprintf(pos, end - pos, "%sFT/EAP",
2847 				  pos == start ? "" : "+");
2848 		if (os_snprintf_error(end - pos, ret))
2849 			return pos;
2850 		pos += ret;
2851 	}
2852 	if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2853 		ret = os_snprintf(pos, end - pos, "%sFT/PSK",
2854 				  pos == start ? "" : "+");
2855 		if (os_snprintf_error(end - pos, ret))
2856 			return pos;
2857 		pos += ret;
2858 	}
2859 	if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
2860 		ret = os_snprintf(pos, end - pos, "%sFT/SAE",
2861 				  pos == start ? "" : "+");
2862 		if (os_snprintf_error(end - pos, ret))
2863 			return pos;
2864 		pos += ret;
2865 	}
2866 	if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
2867 		ret = os_snprintf(pos, end - pos, "%sFT/SAE-EXT-KEY",
2868 				  pos == start ? "" : "+");
2869 		if (os_snprintf_error(end - pos, ret))
2870 			return pos;
2871 		pos += ret;
2872 	}
2873 #endif /* CONFIG_IEEE80211R */
2874 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2875 		ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
2876 				  pos == start ? "" : "+");
2877 		if (os_snprintf_error(end - pos, ret))
2878 			return pos;
2879 		pos += ret;
2880 	}
2881 	if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2882 		ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
2883 				  pos == start ? "" : "+");
2884 		if (os_snprintf_error(end - pos, ret))
2885 			return pos;
2886 		pos += ret;
2887 	}
2888 
2889 #ifdef CONFIG_SUITEB
2890 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
2891 		ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
2892 				  pos == start ? "" : "+");
2893 		if (os_snprintf_error(end - pos, ret))
2894 			return pos;
2895 		pos += ret;
2896 	}
2897 #endif /* CONFIG_SUITEB */
2898 
2899 #ifdef CONFIG_SUITEB192
2900 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
2901 		ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
2902 				  pos == start ? "" : "+");
2903 		if (os_snprintf_error(end - pos, ret))
2904 			return pos;
2905 		pos += ret;
2906 	}
2907 #endif /* CONFIG_SUITEB192 */
2908 
2909 #ifdef CONFIG_FILS
2910 	if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
2911 		ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
2912 				  pos == start ? "" : "+");
2913 		if (os_snprintf_error(end - pos, ret))
2914 			return pos;
2915 		pos += ret;
2916 	}
2917 	if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
2918 		ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
2919 				  pos == start ? "" : "+");
2920 		if (os_snprintf_error(end - pos, ret))
2921 			return pos;
2922 		pos += ret;
2923 	}
2924 #ifdef CONFIG_IEEE80211R
2925 	if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
2926 		ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
2927 				  pos == start ? "" : "+");
2928 		if (os_snprintf_error(end - pos, ret))
2929 			return pos;
2930 		pos += ret;
2931 	}
2932 	if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
2933 		ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
2934 				  pos == start ? "" : "+");
2935 		if (os_snprintf_error(end - pos, ret))
2936 			return pos;
2937 		pos += ret;
2938 	}
2939 #endif /* CONFIG_IEEE80211R */
2940 #endif /* CONFIG_FILS */
2941 
2942 #ifdef CONFIG_OWE
2943 	if (data.key_mgmt & WPA_KEY_MGMT_OWE) {
2944 		ret = os_snprintf(pos, end - pos, "%sOWE",
2945 				  pos == start ? "" : "+");
2946 		if (os_snprintf_error(end - pos, ret))
2947 			return pos;
2948 		pos += ret;
2949 	}
2950 #endif /* CONFIG_OWE */
2951 
2952 #ifdef CONFIG_DPP
2953 	if (data.key_mgmt & WPA_KEY_MGMT_DPP) {
2954 		ret = os_snprintf(pos, end - pos, "%sDPP",
2955 				  pos == start ? "" : "+");
2956 		if (os_snprintf_error(end - pos, ret))
2957 			return pos;
2958 		pos += ret;
2959 	}
2960 #endif /* CONFIG_DPP */
2961 
2962 	if (data.key_mgmt & WPA_KEY_MGMT_OSEN) {
2963 		ret = os_snprintf(pos, end - pos, "%sOSEN",
2964 				  pos == start ? "" : "+");
2965 		if (os_snprintf_error(end - pos, ret))
2966 			return pos;
2967 		pos += ret;
2968 	}
2969 
2970 	pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2971 
2972 	if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2973 		ret = os_snprintf(pos, end - pos, "-preauth");
2974 		if (os_snprintf_error(end - pos, ret))
2975 			return pos;
2976 		pos += ret;
2977 	}
2978 
2979 	ret = os_snprintf(pos, end - pos, "]");
2980 	if (os_snprintf_error(end - pos, ret))
2981 		return pos;
2982 	pos += ret;
2983 
2984 	return pos;
2985 }
2986 
2987 
2988 #ifdef CONFIG_WPS
wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant * wpa_s,char * pos,char * end,struct wpabuf * wps_ie)2989 static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2990 					    char *pos, char *end,
2991 					    struct wpabuf *wps_ie)
2992 {
2993 	int ret;
2994 	const char *txt;
2995 
2996 	if (wps_ie == NULL)
2997 		return pos;
2998 	if (wps_is_selected_pbc_registrar(wps_ie))
2999 		txt = "[WPS-PBC]";
3000 	else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
3001 		txt = "[WPS-AUTH]";
3002 	else if (wps_is_selected_pin_registrar(wps_ie))
3003 		txt = "[WPS-PIN]";
3004 	else
3005 		txt = "[WPS]";
3006 
3007 	ret = os_snprintf(pos, end - pos, "%s", txt);
3008 	if (!os_snprintf_error(end - pos, ret))
3009 		pos += ret;
3010 	wpabuf_free(wps_ie);
3011 	return pos;
3012 }
3013 #endif /* CONFIG_WPS */
3014 
3015 
wpa_supplicant_wps_ie_txt(struct wpa_supplicant * wpa_s,char * pos,char * end,const struct wpa_bss * bss)3016 static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
3017 					char *pos, char *end,
3018 					const struct wpa_bss *bss)
3019 {
3020 #ifdef CONFIG_WPS
3021 	struct wpabuf *wps_ie;
3022 	wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
3023 	return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
3024 #else /* CONFIG_WPS */
3025 	return pos;
3026 #endif /* CONFIG_WPS */
3027 }
3028 
3029 
3030 /* Format one result on one text line into a buffer. */
wpa_supplicant_ctrl_iface_scan_result(struct wpa_supplicant * wpa_s,const struct wpa_bss * bss,char * buf,size_t buflen)3031 static int wpa_supplicant_ctrl_iface_scan_result(
3032 	struct wpa_supplicant *wpa_s,
3033 	const struct wpa_bss *bss, char *buf, size_t buflen)
3034 {
3035 	char *pos, *end;
3036 	int ret;
3037 	const u8 *ie, *ie2, *osen_ie, *p2p, *mesh, *owe, *rsnxe;
3038 
3039 	mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
3040 	p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
3041 	if (!p2p)
3042 		p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
3043 	if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
3044 	    os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
3045 	    0)
3046 		return 0; /* Do not show P2P listen discovery results here */
3047 
3048 	pos = buf;
3049 	end = buf + buflen;
3050 
3051 	ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
3052 			  MAC2STR(bss->bssid), bss->freq, bss->level);
3053 	if (os_snprintf_error(end - pos, ret))
3054 		return -1;
3055 	pos += ret;
3056 	ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3057 	if (ie)
3058 		pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
3059 	ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3060 	if (ie2) {
3061 		pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
3062 					    ie2, 2 + ie2[1]);
3063 	}
3064 	rsnxe = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
3065 	if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_H2E)) {
3066 		ret = os_snprintf(pos, end - pos, "[SAE-H2E]");
3067 		if (os_snprintf_error(end - pos, ret))
3068 			return -1;
3069 		pos += ret;
3070 	}
3071 	if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_PK)) {
3072 		ret = os_snprintf(pos, end - pos, "[SAE-PK]");
3073 		if (os_snprintf_error(end - pos, ret))
3074 			return -1;
3075 		pos += ret;
3076 	}
3077 	osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
3078 	if (osen_ie)
3079 		pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
3080 					    osen_ie, 2 + osen_ie[1]);
3081 	owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
3082 	if (owe) {
3083 		ret = os_snprintf(pos, end - pos,
3084 				  ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
3085 		if (os_snprintf_error(end - pos, ret))
3086 			return -1;
3087 		pos += ret;
3088 	}
3089 	pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3090 	if (!ie && !ie2 && !osen_ie && (bss->caps & IEEE80211_CAP_PRIVACY)) {
3091 		ret = os_snprintf(pos, end - pos, "[WEP]");
3092 		if (os_snprintf_error(end - pos, ret))
3093 			return -1;
3094 		pos += ret;
3095 	}
3096 	if (mesh) {
3097 		ret = os_snprintf(pos, end - pos, "[MESH]");
3098 		if (os_snprintf_error(end - pos, ret))
3099 			return -1;
3100 		pos += ret;
3101 	}
3102 	if (bss_is_dmg(bss)) {
3103 		const char *s;
3104 
3105 		if (wpa_bss_get_ie_ext(bss, WLAN_EID_EXT_EDMG_OPERATION)) {
3106 			ret = os_snprintf(pos, end - pos, "[EDMG]");
3107 			if (os_snprintf_error(end - pos, ret))
3108 				return -1;
3109 			pos += ret;
3110 		}
3111 
3112 		ret = os_snprintf(pos, end - pos, "[DMG]");
3113 		if (os_snprintf_error(end - pos, ret))
3114 			return -1;
3115 		pos += ret;
3116 		switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
3117 		case IEEE80211_CAP_DMG_IBSS:
3118 			s = "[IBSS]";
3119 			break;
3120 		case IEEE80211_CAP_DMG_AP:
3121 			s = "[ESS]";
3122 			break;
3123 		case IEEE80211_CAP_DMG_PBSS:
3124 			s = "[PBSS]";
3125 			break;
3126 		default:
3127 			s = "";
3128 			break;
3129 		}
3130 		ret = os_snprintf(pos, end - pos, "%s", s);
3131 		if (os_snprintf_error(end - pos, ret))
3132 			return -1;
3133 		pos += ret;
3134 	} else {
3135 		if (bss->caps & IEEE80211_CAP_IBSS) {
3136 			ret = os_snprintf(pos, end - pos, "[IBSS]");
3137 			if (os_snprintf_error(end - pos, ret))
3138 				return -1;
3139 			pos += ret;
3140 		}
3141 		if (bss->caps & IEEE80211_CAP_ESS) {
3142 			ret = os_snprintf(pos, end - pos, "[ESS]");
3143 			if (os_snprintf_error(end - pos, ret))
3144 				return -1;
3145 			pos += ret;
3146 		}
3147 	}
3148 	if (p2p) {
3149 		ret = os_snprintf(pos, end - pos, "[P2P]");
3150 		if (os_snprintf_error(end - pos, ret))
3151 			return -1;
3152 		pos += ret;
3153 	}
3154 #ifdef CONFIG_HS20
3155 	if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
3156 		ret = os_snprintf(pos, end - pos, "[HS20]");
3157 		if (os_snprintf_error(end - pos, ret))
3158 			return -1;
3159 		pos += ret;
3160 	}
3161 #endif /* CONFIG_HS20 */
3162 #ifdef CONFIG_FILS
3163 	if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
3164 		ret = os_snprintf(pos, end - pos, "[FILS]");
3165 		if (os_snprintf_error(end - pos, ret))
3166 			return -1;
3167 		pos += ret;
3168 	}
3169 #endif /* CONFIG_FILS */
3170 #ifdef CONFIG_FST
3171 	if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
3172 		ret = os_snprintf(pos, end - pos, "[FST]");
3173 		if (os_snprintf_error(end - pos, ret))
3174 			return -1;
3175 		pos += ret;
3176 	}
3177 #endif /* CONFIG_FST */
3178 	if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_UTF_8_SSID)) {
3179 		ret = os_snprintf(pos, end - pos, "[UTF-8]");
3180 		if (os_snprintf_error(end - pos, ret))
3181 			return -1;
3182 		pos += ret;
3183 	}
3184 
3185 	ret = os_snprintf(pos, end - pos, "\t%s",
3186 			  wpa_ssid_txt(bss->ssid, bss->ssid_len));
3187 	if (os_snprintf_error(end - pos, ret))
3188 		return -1;
3189 	pos += ret;
3190 
3191 	ret = os_snprintf(pos, end - pos, "\n");
3192 	if (os_snprintf_error(end - pos, ret))
3193 		return -1;
3194 	pos += ret;
3195 
3196 	return pos - buf;
3197 }
3198 
3199 
wpa_supplicant_ctrl_iface_scan_results(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3200 static int wpa_supplicant_ctrl_iface_scan_results(
3201 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
3202 {
3203 	char *pos, *end;
3204 	struct wpa_bss *bss;
3205 	int ret;
3206 
3207 	pos = buf;
3208 	end = buf + buflen;
3209 	ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
3210 			  "flags / ssid\n");
3211 	if (os_snprintf_error(end - pos, ret))
3212 		return pos - buf;
3213 	pos += ret;
3214 
3215 	dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
3216 		ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
3217 							    end - pos);
3218 		if (ret < 0 || ret >= end - pos)
3219 			return pos - buf;
3220 		pos += ret;
3221 	}
3222 
3223 	return pos - buf;
3224 }
3225 
3226 
3227 #ifdef CONFIG_MESH
3228 
wpa_supplicant_ctrl_iface_mesh_interface_add(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)3229 static int wpa_supplicant_ctrl_iface_mesh_interface_add(
3230 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
3231 {
3232 	char *pos, ifname[IFNAMSIZ + 1];
3233 
3234 	ifname[0] = '\0';
3235 
3236 	pos = os_strstr(cmd, "ifname=");
3237 	if (pos) {
3238 		pos += 7;
3239 		os_strlcpy(ifname, pos, sizeof(ifname));
3240 	}
3241 
3242 	if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
3243 		return -1;
3244 
3245 	os_strlcpy(reply, ifname, max_len);
3246 	return os_strlen(ifname);
3247 }
3248 
3249 
wpa_supplicant_ctrl_iface_mesh_group_add(struct wpa_supplicant * wpa_s,char * cmd)3250 static int wpa_supplicant_ctrl_iface_mesh_group_add(
3251 	struct wpa_supplicant *wpa_s, char *cmd)
3252 {
3253 	int id;
3254 	struct wpa_ssid *ssid;
3255 
3256 	id = atoi(cmd);
3257 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
3258 
3259 	ssid = wpa_config_get_network(wpa_s->conf, id);
3260 	if (ssid == NULL) {
3261 		wpa_printf(MSG_DEBUG,
3262 			   "CTRL_IFACE: Could not find network id=%d", id);
3263 		return -1;
3264 	}
3265 	if (ssid->mode != WPAS_MODE_MESH) {
3266 		wpa_printf(MSG_DEBUG,
3267 			   "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
3268 		return -1;
3269 	}
3270 	if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
3271 	    ssid->key_mgmt != WPA_KEY_MGMT_SAE &&
3272 	    ssid->key_mgmt != WPA_KEY_MGMT_SAE_EXT_KEY) {
3273 		wpa_printf(MSG_ERROR,
3274 			   "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
3275 		return -1;
3276 	}
3277 
3278 	/*
3279 	 * TODO: If necessary write our own group_add function,
3280 	 * for now we can reuse select_network
3281 	 */
3282 	wpa_supplicant_select_network(wpa_s, ssid);
3283 
3284 	return 0;
3285 }
3286 
3287 
wpa_supplicant_ctrl_iface_mesh_group_remove(struct wpa_supplicant * wpa_s,char * cmd)3288 static int wpa_supplicant_ctrl_iface_mesh_group_remove(
3289 	struct wpa_supplicant *wpa_s, char *cmd)
3290 {
3291 	struct wpa_supplicant *orig;
3292 	struct wpa_global *global;
3293 	int found = 0;
3294 
3295 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
3296 
3297 	global = wpa_s->global;
3298 	orig = wpa_s;
3299 
3300 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3301 		if (os_strcmp(wpa_s->ifname, cmd) == 0) {
3302 			found = 1;
3303 			break;
3304 		}
3305 	}
3306 	if (!found) {
3307 		wpa_printf(MSG_ERROR,
3308 			   "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
3309 			   cmd);
3310 		return -1;
3311 	}
3312 	if (wpa_s->mesh_if_created && wpa_s == orig) {
3313 		wpa_printf(MSG_ERROR,
3314 			   "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
3315 		return -1;
3316 	}
3317 
3318 	wpa_s->reassociate = 0;
3319 	wpa_s->disconnected = 1;
3320 	wpa_supplicant_cancel_sched_scan(wpa_s);
3321 	wpa_supplicant_cancel_scan(wpa_s);
3322 
3323 	/*
3324 	 * TODO: If necessary write our own group_remove function,
3325 	 * for now we can reuse deauthenticate
3326 	 */
3327 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3328 
3329 	if (wpa_s->mesh_if_created)
3330 		wpa_supplicant_remove_iface(global, wpa_s, 0);
3331 
3332 	return 0;
3333 }
3334 
3335 
wpa_supplicant_ctrl_iface_mesh_peer_remove(struct wpa_supplicant * wpa_s,char * cmd)3336 static int wpa_supplicant_ctrl_iface_mesh_peer_remove(
3337 	struct wpa_supplicant *wpa_s, char *cmd)
3338 {
3339 	u8 addr[ETH_ALEN];
3340 
3341 	if (hwaddr_aton(cmd, addr) < 0)
3342 		return -1;
3343 
3344 	return wpas_mesh_peer_remove(wpa_s, addr);
3345 }
3346 
3347 
wpa_supplicant_ctrl_iface_mesh_peer_add(struct wpa_supplicant * wpa_s,char * cmd)3348 static int wpa_supplicant_ctrl_iface_mesh_peer_add(
3349 	struct wpa_supplicant *wpa_s, char *cmd)
3350 {
3351 	u8 addr[ETH_ALEN];
3352 	int duration;
3353 	char *pos;
3354 
3355 	pos = os_strstr(cmd, " duration=");
3356 	if (pos) {
3357 		*pos = '\0';
3358 		duration = atoi(pos + 10);
3359 	} else {
3360 		duration = -1;
3361 	}
3362 
3363 	if (hwaddr_aton(cmd, addr))
3364 		return -1;
3365 
3366 	return wpas_mesh_peer_add(wpa_s, addr, duration);
3367 }
3368 
3369 
wpa_supplicant_ctrl_iface_mesh_link_probe(struct wpa_supplicant * wpa_s,char * cmd)3370 static int wpa_supplicant_ctrl_iface_mesh_link_probe(
3371 	struct wpa_supplicant *wpa_s, char *cmd)
3372 {
3373 	struct ether_header *eth;
3374 	u8 addr[ETH_ALEN];
3375 	u8 *buf;
3376 	char *pos;
3377 	size_t payload_len = 0, len;
3378 	int ret = -1;
3379 
3380 	if (hwaddr_aton(cmd, addr))
3381 		return -1;
3382 
3383 	pos = os_strstr(cmd, " payload=");
3384 	if (pos) {
3385 		pos = pos + 9;
3386 		payload_len = os_strlen(pos);
3387 		if (payload_len & 1)
3388 			return -1;
3389 
3390 		payload_len /= 2;
3391 	}
3392 
3393 	len = ETH_HLEN + payload_len;
3394 	buf = os_malloc(len);
3395 	if (!buf)
3396 		return -1;
3397 
3398 	eth = (struct ether_header *) buf;
3399 	os_memcpy(eth->ether_dhost, addr, ETH_ALEN);
3400 	os_memcpy(eth->ether_shost, wpa_s->own_addr, ETH_ALEN);
3401 	eth->ether_type = htons(ETH_P_802_3);
3402 
3403 	if (payload_len && hexstr2bin(pos, buf + ETH_HLEN, payload_len) < 0)
3404 		goto fail;
3405 
3406 	ret = wpa_drv_mesh_link_probe(wpa_s, addr, buf, len);
3407 fail:
3408 	os_free(buf);
3409 	return -ret;
3410 }
3411 
3412 #endif /* CONFIG_MESH */
3413 
3414 
wpa_supplicant_ctrl_iface_select_network(struct wpa_supplicant * wpa_s,char * cmd)3415 static int wpa_supplicant_ctrl_iface_select_network(
3416 	struct wpa_supplicant *wpa_s, char *cmd)
3417 {
3418 	int id;
3419 	struct wpa_ssid *ssid;
3420 	char *pos;
3421 
3422 	/* cmd: "<network id>" or "any" */
3423 	if (os_strncmp(cmd, "any", 3) == 0) {
3424 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
3425 		ssid = NULL;
3426 	} else {
3427 		id = atoi(cmd);
3428 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
3429 
3430 		ssid = wpa_config_get_network(wpa_s->conf, id);
3431 		if (ssid == NULL) {
3432 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3433 				   "network id=%d", id);
3434 			return -1;
3435 		}
3436 		if (ssid->disabled == 2) {
3437 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3438 				   "SELECT_NETWORK with persistent P2P group");
3439 			return -1;
3440 		}
3441 	}
3442 
3443 	pos = os_strstr(cmd, " freq=");
3444 	if (pos) {
3445 		int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
3446 		if (freqs) {
3447 			os_free(wpa_s->select_network_scan_freqs);
3448 			wpa_s->select_network_scan_freqs = freqs;
3449 		}
3450 	}
3451 
3452 	wpa_s->scan_min_time.sec = 0;
3453 	wpa_s->scan_min_time.usec = 0;
3454 	wpa_supplicant_select_network(wpa_s, ssid);
3455 
3456 	return 0;
3457 }
3458 
3459 
wpa_supplicant_ctrl_iface_enable_network(struct wpa_supplicant * wpa_s,char * cmd)3460 static int wpa_supplicant_ctrl_iface_enable_network(
3461 	struct wpa_supplicant *wpa_s, char *cmd)
3462 {
3463 	int id;
3464 	struct wpa_ssid *ssid;
3465 
3466 	/* cmd: "<network id>" or "all" */
3467 	if (os_strcmp(cmd, "all") == 0) {
3468 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
3469 		ssid = NULL;
3470 	} else {
3471 		id = atoi(cmd);
3472 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
3473 
3474 		ssid = wpa_config_get_network(wpa_s->conf, id);
3475 		if (ssid == NULL) {
3476 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3477 				   "network id=%d", id);
3478 			return -1;
3479 		}
3480 		if (ssid->disabled == 2) {
3481 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3482 				   "ENABLE_NETWORK with persistent P2P group");
3483 			return -1;
3484 		}
3485 
3486 		if (os_strstr(cmd, " no-connect")) {
3487 			ssid->disabled = 0;
3488 			return 0;
3489 		}
3490 	}
3491 	wpa_s->scan_min_time.sec = 0;
3492 	wpa_s->scan_min_time.usec = 0;
3493 	wpa_supplicant_enable_network(wpa_s, ssid);
3494 
3495 	return 0;
3496 }
3497 
3498 
wpa_supplicant_ctrl_iface_disable_network(struct wpa_supplicant * wpa_s,char * cmd)3499 static int wpa_supplicant_ctrl_iface_disable_network(
3500 	struct wpa_supplicant *wpa_s, char *cmd)
3501 {
3502 	int id;
3503 	struct wpa_ssid *ssid;
3504 
3505 	/* cmd: "<network id>" or "all" */
3506 	if (os_strcmp(cmd, "all") == 0) {
3507 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
3508 		ssid = NULL;
3509 	} else {
3510 		id = atoi(cmd);
3511 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
3512 
3513 		ssid = wpa_config_get_network(wpa_s->conf, id);
3514 		if (ssid == NULL) {
3515 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3516 				   "network id=%d", id);
3517 			return -1;
3518 		}
3519 		if (ssid->disabled == 2) {
3520 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3521 				   "DISABLE_NETWORK with persistent P2P "
3522 				   "group");
3523 			return -1;
3524 		}
3525 	}
3526 	wpa_supplicant_disable_network(wpa_s, ssid);
3527 
3528 	return 0;
3529 }
3530 
3531 
wpa_supplicant_ctrl_iface_add_network(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3532 static int wpa_supplicant_ctrl_iface_add_network(
3533 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
3534 {
3535 	struct wpa_ssid *ssid;
3536 	int ret;
3537 
3538 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
3539 
3540 	ssid = wpa_supplicant_add_network(wpa_s);
3541 	if (ssid == NULL)
3542 		return -1;
3543 
3544 	ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
3545 	if (os_snprintf_error(buflen, ret))
3546 		return -1;
3547 	return ret;
3548 }
3549 
3550 
wpa_supplicant_ctrl_iface_remove_network(struct wpa_supplicant * wpa_s,char * cmd)3551 static int wpa_supplicant_ctrl_iface_remove_network(
3552 	struct wpa_supplicant *wpa_s, char *cmd)
3553 {
3554 	int id;
3555 	int result;
3556 
3557 	/* cmd: "<network id>" or "all" */
3558 	if (os_strcmp(cmd, "all") == 0) {
3559 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
3560 		return wpa_supplicant_remove_all_networks(wpa_s);
3561 	}
3562 
3563 	id = atoi(cmd);
3564 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
3565 
3566 	result = wpa_supplicant_remove_network(wpa_s, id);
3567 	if (result == -1) {
3568 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3569 			   "id=%d", id);
3570 		return -1;
3571 	}
3572 	if (result == -2) {
3573 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
3574 			   "network id=%d", id);
3575 		return -1;
3576 	}
3577 	return 0;
3578 }
3579 
3580 
wpa_supplicant_ctrl_iface_update_network(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,char * name,char * value)3581 static int wpa_supplicant_ctrl_iface_update_network(
3582 	struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3583 	char *name, char *value)
3584 {
3585 	int ret;
3586 
3587 	ret = wpa_config_set(ssid, name, value, 0);
3588 	if (ret < 0) {
3589 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
3590 			   "variable '%s'", name);
3591 		return -1;
3592 	}
3593 	if (ret == 1)
3594 		return 0; /* No change to the previously configured value */
3595 
3596 #ifdef CONFIG_BGSCAN
3597 	if (os_strcmp(name, "bgscan") == 0) {
3598 		/*
3599 		 * Reset the bgscan parameters for the current network and
3600 		 * return. There's no need to flush caches for bgscan parameter
3601 		 * changes.
3602 		 */
3603 		if (wpa_s->current_ssid == ssid &&
3604 		    wpa_s->wpa_state == WPA_COMPLETED)
3605 			wpa_supplicant_reset_bgscan(wpa_s);
3606 		return 0;
3607 	}
3608 #endif /* CONFIG_BGSCAN */
3609 
3610 	if (os_strcmp(name, "bssid") != 0 &&
3611 	    os_strcmp(name, "bssid_hint") != 0 &&
3612 	    os_strcmp(name, "priority") != 0) {
3613 		wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
3614 
3615 		if (wpa_s->current_ssid == ssid ||
3616 		    wpa_s->current_ssid == NULL) {
3617 			/*
3618 			 * Invalidate the EAP session cache if anything in the
3619 			 * current or previously used configuration changes.
3620 			 */
3621 			eapol_sm_invalidate_cached_session(wpa_s->eapol);
3622 		}
3623 	}
3624 
3625 	if ((os_strcmp(name, "psk") == 0 &&
3626 	     value[0] == '"' && ssid->ssid_len) ||
3627 	    (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
3628 		wpa_config_update_psk(ssid);
3629 	else if (os_strcmp(name, "priority") == 0)
3630 		wpa_config_update_prio_list(wpa_s->conf);
3631 
3632 	return 0;
3633 }
3634 
3635 
wpa_supplicant_ctrl_iface_set_network(struct wpa_supplicant * wpa_s,char * cmd)3636 static int wpa_supplicant_ctrl_iface_set_network(
3637 	struct wpa_supplicant *wpa_s, char *cmd)
3638 {
3639 	int id, ret, prev_bssid_set, prev_disabled;
3640 	struct wpa_ssid *ssid;
3641 	char *name, *value;
3642 	u8 prev_bssid[ETH_ALEN];
3643 
3644 	/* cmd: "<network id> <variable name> <value>" */
3645 	name = os_strchr(cmd, ' ');
3646 	if (name == NULL)
3647 		return -1;
3648 	*name++ = '\0';
3649 
3650 	value = os_strchr(name, ' ');
3651 	if (value == NULL)
3652 		return -1;
3653 	*value++ = '\0';
3654 
3655 	id = atoi(cmd);
3656 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
3657 		   id, name);
3658 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3659 			      (u8 *) value, os_strlen(value));
3660 
3661 	ssid = wpa_config_get_network(wpa_s->conf, id);
3662 	if (ssid == NULL) {
3663 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3664 			   "id=%d", id);
3665 		return -1;
3666 	}
3667 
3668 	prev_bssid_set = ssid->bssid_set;
3669 	prev_disabled = ssid->disabled;
3670 	os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
3671 	ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
3672 						       value);
3673 	if (ret == 0 &&
3674 	    (ssid->bssid_set != prev_bssid_set ||
3675 	     os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
3676 		wpas_notify_network_bssid_set_changed(wpa_s, ssid);
3677 
3678 	if (prev_disabled != ssid->disabled &&
3679 	    (prev_disabled == 2 || ssid->disabled == 2))
3680 		wpas_notify_network_type_changed(wpa_s, ssid);
3681 
3682 	return ret;
3683 }
3684 
3685 
wpa_supplicant_ctrl_iface_get_network(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)3686 static int wpa_supplicant_ctrl_iface_get_network(
3687 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
3688 {
3689 	int id;
3690 	size_t res;
3691 	struct wpa_ssid *ssid;
3692 	char *name, *value;
3693 
3694 	/* cmd: "<network id> <variable name>" */
3695 	name = os_strchr(cmd, ' ');
3696 	if (name == NULL || buflen == 0)
3697 		return -1;
3698 	*name++ = '\0';
3699 
3700 	id = atoi(cmd);
3701 	wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
3702 		   id, name);
3703 
3704 	ssid = wpa_config_get_network(wpa_s->conf, id);
3705 	if (ssid == NULL) {
3706 		wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Could not find network "
3707 			   "id=%d", id);
3708 		return -1;
3709 	}
3710 
3711 	value = wpa_config_get_no_key(ssid, name);
3712 	if (value == NULL) {
3713 		wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Failed to get network "
3714 			   "variable '%s'", name);
3715 		return -1;
3716 	}
3717 
3718 	res = os_strlcpy(buf, value, buflen);
3719 	if (res >= buflen) {
3720 		os_free(value);
3721 		return -1;
3722 	}
3723 
3724 	os_free(value);
3725 
3726 	return res;
3727 }
3728 
3729 
wpa_supplicant_ctrl_iface_dup_network(struct wpa_supplicant * wpa_s,char * cmd,struct wpa_supplicant * dst_wpa_s)3730 static int wpa_supplicant_ctrl_iface_dup_network(
3731 	struct wpa_supplicant *wpa_s, char *cmd,
3732 	struct wpa_supplicant *dst_wpa_s)
3733 {
3734 	struct wpa_ssid *ssid_s, *ssid_d;
3735 	char *name, *id, *value;
3736 	int id_s, id_d, ret;
3737 
3738 	/* cmd: "<src network id> <dst network id> <variable name>" */
3739 	id = os_strchr(cmd, ' ');
3740 	if (id == NULL)
3741 		return -1;
3742 	*id++ = '\0';
3743 
3744 	name = os_strchr(id, ' ');
3745 	if (name == NULL)
3746 		return -1;
3747 	*name++ = '\0';
3748 
3749 	id_s = atoi(cmd);
3750 	id_d = atoi(id);
3751 
3752 	wpa_printf(MSG_DEBUG,
3753 		   "CTRL_IFACE: DUP_NETWORK ifname=%s->%s id=%d->%d name='%s'",
3754 		   wpa_s->ifname, dst_wpa_s->ifname, id_s, id_d, name);
3755 
3756 	ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
3757 	if (ssid_s == NULL) {
3758 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3759 			   "network id=%d", id_s);
3760 		return -1;
3761 	}
3762 
3763 	ssid_d = wpa_config_get_network(dst_wpa_s->conf, id_d);
3764 	if (ssid_d == NULL) {
3765 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3766 			   "network id=%d", id_d);
3767 		return -1;
3768 	}
3769 
3770 	value = wpa_config_get(ssid_s, name);
3771 	if (value == NULL) {
3772 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
3773 			   "variable '%s'", name);
3774 		return -1;
3775 	}
3776 
3777 	ret = wpa_supplicant_ctrl_iface_update_network(dst_wpa_s, ssid_d, name,
3778 						       value);
3779 
3780 	os_free(value);
3781 
3782 	return ret;
3783 }
3784 
3785 
wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3786 static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
3787 						char *buf, size_t buflen)
3788 {
3789 	char *pos, *end;
3790 	struct wpa_cred *cred;
3791 	int ret;
3792 
3793 	pos = buf;
3794 	end = buf + buflen;
3795 	ret = os_snprintf(pos, end - pos,
3796 			  "cred id / realm / username / domain / imsi\n");
3797 	if (os_snprintf_error(end - pos, ret))
3798 		return pos - buf;
3799 	pos += ret;
3800 
3801 	cred = wpa_s->conf->cred;
3802 	while (cred) {
3803 		ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
3804 				  cred->id, cred->realm ? cred->realm : "",
3805 				  cred->username ? cred->username : "",
3806 				  cred->domain ? cred->domain[0] : "",
3807 				  cred->imsi ? cred->imsi : "");
3808 		if (os_snprintf_error(end - pos, ret))
3809 			return pos - buf;
3810 		pos += ret;
3811 
3812 		cred = cred->next;
3813 	}
3814 
3815 	return pos - buf;
3816 }
3817 
3818 
wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3819 static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
3820 					      char *buf, size_t buflen)
3821 {
3822 	struct wpa_cred *cred;
3823 	int ret;
3824 
3825 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
3826 
3827 	cred = wpa_config_add_cred(wpa_s->conf);
3828 	if (cred == NULL)
3829 		return -1;
3830 
3831 	wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
3832 
3833 	ret = os_snprintf(buf, buflen, "%d\n", cred->id);
3834 	if (os_snprintf_error(buflen, ret))
3835 		return -1;
3836 	return ret;
3837 }
3838 
3839 
wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant * wpa_s,char * cmd)3840 static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
3841 						 char *cmd)
3842 {
3843 	int id;
3844 	struct wpa_cred *cred, *prev;
3845 
3846 	/* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
3847 	 * "provisioning_sp=<FQDN> */
3848 	if (os_strcmp(cmd, "all") == 0) {
3849 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
3850 		return wpas_remove_all_creds(wpa_s);
3851 	}
3852 
3853 	if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
3854 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
3855 			   cmd + 8);
3856 		cred = wpa_s->conf->cred;
3857 		while (cred) {
3858 			prev = cred;
3859 			cred = cred->next;
3860 			if (prev->domain) {
3861 				size_t i;
3862 				for (i = 0; i < prev->num_domain; i++) {
3863 					if (os_strcmp(prev->domain[i], cmd + 8)
3864 					    != 0)
3865 						continue;
3866 					wpas_remove_cred(wpa_s, prev);
3867 					break;
3868 				}
3869 			}
3870 		}
3871 		return 0;
3872 	}
3873 
3874 	if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
3875 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
3876 			   cmd + 16);
3877 		cred = wpa_s->conf->cred;
3878 		while (cred) {
3879 			prev = cred;
3880 			cred = cred->next;
3881 			if (prev->provisioning_sp &&
3882 			    os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
3883 				wpas_remove_cred(wpa_s, prev);
3884 		}
3885 		return 0;
3886 	}
3887 
3888 	id = atoi(cmd);
3889 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
3890 
3891 	cred = wpa_config_get_cred(wpa_s->conf, id);
3892 	return wpas_remove_cred(wpa_s, cred);
3893 }
3894 
3895 
wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant * wpa_s,char * cmd)3896 static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
3897 					      char *cmd)
3898 {
3899 	int id;
3900 	struct wpa_cred *cred;
3901 	char *name, *value;
3902 
3903 	/* cmd: "<cred id> <variable name> <value>" */
3904 	name = os_strchr(cmd, ' ');
3905 	if (name == NULL)
3906 		return -1;
3907 	*name++ = '\0';
3908 
3909 	value = os_strchr(name, ' ');
3910 	if (value == NULL)
3911 		return -1;
3912 	*value++ = '\0';
3913 
3914 	id = atoi(cmd);
3915 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
3916 		   id, name);
3917 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3918 			      (u8 *) value, os_strlen(value));
3919 
3920 	cred = wpa_config_get_cred(wpa_s->conf, id);
3921 	if (cred == NULL) {
3922 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3923 			   id);
3924 		return -1;
3925 	}
3926 
3927 	if (wpa_config_set_cred(cred, name, value, 0) < 0) {
3928 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
3929 			   "variable '%s'", name);
3930 		return -1;
3931 	}
3932 
3933 	wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
3934 
3935 	return 0;
3936 }
3937 
3938 
wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)3939 static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
3940 					      char *cmd, char *buf,
3941 					      size_t buflen)
3942 {
3943 	int id;
3944 	size_t res;
3945 	struct wpa_cred *cred;
3946 	char *name, *value;
3947 
3948 	/* cmd: "<cred id> <variable name>" */
3949 	name = os_strchr(cmd, ' ');
3950 	if (name == NULL)
3951 		return -1;
3952 	*name++ = '\0';
3953 
3954 	id = atoi(cmd);
3955 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
3956 		   id, name);
3957 
3958 	cred = wpa_config_get_cred(wpa_s->conf, id);
3959 	if (cred == NULL) {
3960 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3961 			   id);
3962 		return -1;
3963 	}
3964 
3965 	value = wpa_config_get_cred_no_key(cred, name);
3966 	if (value == NULL) {
3967 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
3968 			   name);
3969 		return -1;
3970 	}
3971 
3972 	res = os_strlcpy(buf, value, buflen);
3973 	if (res >= buflen) {
3974 		os_free(value);
3975 		return -1;
3976 	}
3977 
3978 	os_free(value);
3979 
3980 	return res;
3981 }
3982 
3983 
3984 #ifndef CONFIG_NO_CONFIG_WRITE
wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant * wpa_s)3985 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
3986 {
3987 	int ret;
3988 
3989 	if (!wpa_s->conf->update_config) {
3990 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
3991 			   "to update configuration (update_config=0)");
3992 		return -1;
3993 	}
3994 
3995 	ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
3996 	if (ret) {
3997 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
3998 			   "update configuration");
3999 	} else {
4000 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
4001 			   " updated");
4002 	}
4003 
4004 	return ret;
4005 }
4006 #endif /* CONFIG_NO_CONFIG_WRITE */
4007 
4008 
4009 struct cipher_info {
4010 	unsigned int capa;
4011 	const char *name;
4012 	int group_only;
4013 };
4014 
4015 static const struct cipher_info ciphers[] = {
4016 	{ WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
4017 	{ WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
4018 	{ WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
4019 	{ WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
4020 #ifndef CONFIG_NO_TKIP
4021 	{ WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
4022 #endif /* CONFIG_NO_TKIP */
4023 	{ WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
4024 #ifdef CONFIG_WEP
4025 	{ WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
4026 	{ WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
4027 #endif /* CONFIG_WEP */
4028 };
4029 
4030 static const struct cipher_info ciphers_group_mgmt[] = {
4031 	{ WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
4032 	{ WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
4033 	{ WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
4034 	{ WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
4035 };
4036 
4037 
ctrl_iface_get_capability_pairwise(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4038 static int ctrl_iface_get_capability_pairwise(int res, bool strict,
4039 					      struct wpa_driver_capa *capa,
4040 					      char *buf, size_t buflen)
4041 {
4042 	int ret;
4043 	char *pos, *end;
4044 	size_t len;
4045 	unsigned int i;
4046 
4047 	pos = buf;
4048 	end = pos + buflen;
4049 
4050 	if (res < 0) {
4051 		if (strict)
4052 			return 0;
4053 #ifdef CONFIG_NO_TKIP
4054 		len = os_strlcpy(buf, "CCMP NONE", buflen);
4055 #else /* CONFIG_NO_TKIP */
4056 		len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
4057 #endif /* CONFIG_NO_TKIP */
4058 		if (len >= buflen)
4059 			return -1;
4060 		return len;
4061 	}
4062 
4063 	for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
4064 		if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
4065 			ret = os_snprintf(pos, end - pos, "%s%s",
4066 					  pos == buf ? "" : " ",
4067 					  ciphers[i].name);
4068 			if (os_snprintf_error(end - pos, ret))
4069 				return pos - buf;
4070 			pos += ret;
4071 		}
4072 	}
4073 
4074 	return pos - buf;
4075 }
4076 
4077 
ctrl_iface_get_capability_group(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4078 static int ctrl_iface_get_capability_group(int res, bool strict,
4079 					   struct wpa_driver_capa *capa,
4080 					   char *buf, size_t buflen)
4081 {
4082 	int ret;
4083 	char *pos, *end;
4084 	size_t len;
4085 	unsigned int i;
4086 
4087 	pos = buf;
4088 	end = pos + buflen;
4089 
4090 	if (res < 0) {
4091 		if (strict)
4092 			return 0;
4093 #ifdef CONFIG_WEP
4094 #ifdef CONFIG_NO_TKIP
4095 		len = os_strlcpy(buf, "CCMP WEP104 WEP40", buflen);
4096 #else /* CONFIG_NO_TKIP */
4097 		len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
4098 #endif /* CONFIG_NO_TKIP */
4099 #else /* CONFIG_WEP */
4100 #ifdef CONFIG_NO_TKIP
4101 		len = os_strlcpy(buf, "CCMP", buflen);
4102 #else /* CONFIG_NO_TKIP */
4103 		len = os_strlcpy(buf, "CCMP TKIP", buflen);
4104 #endif /* CONFIG_NO_TKIP */
4105 #endif /* CONFIG_WEP */
4106 		if (len >= buflen)
4107 			return -1;
4108 		return len;
4109 	}
4110 
4111 	for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
4112 		if (capa->enc & ciphers[i].capa) {
4113 			ret = os_snprintf(pos, end - pos, "%s%s",
4114 					  pos == buf ? "" : " ",
4115 					  ciphers[i].name);
4116 			if (os_snprintf_error(end - pos, ret))
4117 				return pos - buf;
4118 			pos += ret;
4119 		}
4120 	}
4121 
4122 	return pos - buf;
4123 }
4124 
4125 
ctrl_iface_get_capability_group_mgmt(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4126 static int ctrl_iface_get_capability_group_mgmt(int res, bool strict,
4127 						struct wpa_driver_capa *capa,
4128 						char *buf, size_t buflen)
4129 {
4130 	int ret;
4131 	char *pos, *end;
4132 	unsigned int i;
4133 
4134 	pos = buf;
4135 	end = pos + buflen;
4136 
4137 	if (res < 0)
4138 		return 0;
4139 
4140 	for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
4141 		if (capa->enc & ciphers_group_mgmt[i].capa) {
4142 			ret = os_snprintf(pos, end - pos, "%s%s",
4143 					  pos == buf ? "" : " ",
4144 					  ciphers_group_mgmt[i].name);
4145 			if (os_snprintf_error(end - pos, ret))
4146 				return pos - buf;
4147 			pos += ret;
4148 		}
4149 	}
4150 
4151 	return pos - buf;
4152 }
4153 
4154 
iftype_str_to_index(const char * iftype_str)4155 static int iftype_str_to_index(const char *iftype_str)
4156 {
4157 	if (!iftype_str)
4158 		return WPA_IF_MAX;
4159 
4160 	if (os_strcmp(iftype_str, "STATION") == 0)
4161 		return WPA_IF_STATION;
4162 
4163 	if (os_strcmp(iftype_str, "AP_VLAN") == 0)
4164 		return WPA_IF_AP_VLAN;
4165 
4166 	if (os_strcmp(iftype_str, "AP") == 0)
4167 		return WPA_IF_AP_BSS;
4168 
4169 	if (os_strcmp(iftype_str, "P2P_GO") == 0)
4170 		return WPA_IF_P2P_GO;
4171 
4172 	if (os_strcmp(iftype_str, "P2P_CLIENT") == 0)
4173 		return WPA_IF_P2P_CLIENT;
4174 
4175 	if (os_strcmp(iftype_str, "P2P_DEVICE") == 0)
4176 		return WPA_IF_P2P_DEVICE;
4177 
4178 	if (os_strcmp(iftype_str, "MESH") == 0)
4179 		return WPA_IF_MESH;
4180 
4181 	if (os_strcmp(iftype_str, "IBSS") == 0)
4182 		return WPA_IF_IBSS;
4183 
4184 	if (os_strcmp(iftype_str, "NAN") == 0)
4185 		return WPA_IF_NAN;
4186 
4187 	return WPA_IF_MAX;
4188 }
4189 
4190 
ctrl_iface_get_capability_key_mgmt(int res,bool strict,struct wpa_driver_capa * capa,const char * iftype_str,char * buf,size_t buflen)4191 static int ctrl_iface_get_capability_key_mgmt(int res, bool strict,
4192 					      struct wpa_driver_capa *capa,
4193 					      const char *iftype_str,
4194 					      char *buf, size_t buflen)
4195 {
4196 	int ret;
4197 	unsigned int key_mgmt;
4198 	char *pos, *end;
4199 	size_t len;
4200 
4201 	pos = buf;
4202 	end = pos + buflen;
4203 
4204 	if (res < 0) {
4205 		if (strict)
4206 			return 0;
4207 		len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
4208 				 "NONE", buflen);
4209 		if (len >= buflen)
4210 			return -1;
4211 		return len;
4212 	}
4213 
4214 	if (iftype_str) {
4215 		enum wpa_driver_if_type iftype;
4216 
4217 		iftype = iftype_str_to_index(iftype_str);
4218 		if (iftype == WPA_IF_MAX)
4219 			return -1;
4220 		key_mgmt = capa->key_mgmt_iftype[iftype];
4221 	} else {
4222 		key_mgmt = capa->key_mgmt;
4223 	}
4224 
4225 	ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
4226 	if (os_snprintf_error(end - pos, ret))
4227 		return pos - buf;
4228 	pos += ret;
4229 
4230 	if (key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4231 			WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
4232 		ret = os_snprintf(pos, end - pos, " WPA-EAP");
4233 		if (os_snprintf_error(end - pos, ret))
4234 			return pos - buf;
4235 		pos += ret;
4236 	}
4237 
4238 	if (key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
4239 			WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
4240 		ret = os_snprintf(pos, end - pos, " WPA-PSK");
4241 		if (os_snprintf_error(end - pos, ret))
4242 			return pos - buf;
4243 		pos += ret;
4244 	}
4245 
4246 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
4247 		ret = os_snprintf(pos, end - pos, " WPA-NONE");
4248 		if (os_snprintf_error(end - pos, ret))
4249 			return pos - buf;
4250 		pos += ret;
4251 	}
4252 
4253 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WAPI_PSK) {
4254 		ret = os_snprintf(pos, end - pos, " WAPI-PSK");
4255 		if (os_snprintf_error(end - pos, ret))
4256 			return pos - buf;
4257 		pos += ret;
4258 	}
4259 
4260 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_TPK_HANDSHAKE) {
4261 		ret = os_snprintf(pos, end - pos, " TPK-HANDSHAKE");
4262 		if (os_snprintf_error(end - pos, ret))
4263 			return pos - buf;
4264 		pos += ret;
4265 	}
4266 
4267 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_CCKM) {
4268 		ret = os_snprintf(pos, end - pos, " CCKM");
4269 		if (os_snprintf_error(end - pos, ret))
4270 			return pos - buf;
4271 		pos += ret;
4272 	}
4273 
4274 #ifdef CONFIG_SUITEB
4275 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
4276 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
4277 		if (os_snprintf_error(end - pos, ret))
4278 			return pos - buf;
4279 		pos += ret;
4280 	}
4281 #endif /* CONFIG_SUITEB */
4282 #ifdef CONFIG_SUITEB192
4283 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
4284 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
4285 		if (os_snprintf_error(end - pos, ret))
4286 			return pos - buf;
4287 		pos += ret;
4288 	}
4289 #endif /* CONFIG_SUITEB192 */
4290 #ifdef CONFIG_OWE
4291 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OWE) {
4292 		ret = os_snprintf(pos, end - pos, " OWE");
4293 		if (os_snprintf_error(end - pos, ret))
4294 			return pos - buf;
4295 		pos += ret;
4296 	}
4297 #endif /* CONFIG_OWE */
4298 #ifdef CONFIG_DPP
4299 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_DPP) {
4300 		ret = os_snprintf(pos, end - pos, " DPP");
4301 		if (os_snprintf_error(end - pos, ret))
4302 			return pos - buf;
4303 		pos += ret;
4304 	}
4305 #endif /* CONFIG_DPP */
4306 #ifdef CONFIG_FILS
4307 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA256) {
4308 		ret = os_snprintf(pos, end - pos, " FILS-SHA256");
4309 		if (os_snprintf_error(end - pos, ret))
4310 			return pos - buf;
4311 		pos += ret;
4312 	}
4313 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA384) {
4314 		ret = os_snprintf(pos, end - pos, " FILS-SHA384");
4315 		if (os_snprintf_error(end - pos, ret))
4316 			return pos - buf;
4317 		pos += ret;
4318 	}
4319 #ifdef CONFIG_IEEE80211R
4320 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA256) {
4321 		ret = os_snprintf(pos, end - pos, " FT-FILS-SHA256");
4322 		if (os_snprintf_error(end - pos, ret))
4323 			return pos - buf;
4324 		pos += ret;
4325 	}
4326 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA384) {
4327 		ret = os_snprintf(pos, end - pos, " FT-FILS-SHA384");
4328 		if (os_snprintf_error(end - pos, ret))
4329 			return pos - buf;
4330 		pos += ret;
4331 	}
4332 #endif /* CONFIG_IEEE80211R */
4333 #endif /* CONFIG_FILS */
4334 #ifdef CONFIG_IEEE80211R
4335 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK) {
4336 		ret = os_snprintf(pos, end - pos, " FT-PSK");
4337 		if (os_snprintf_error(end - pos, ret))
4338 			return pos - buf;
4339 		pos += ret;
4340 	}
4341 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) {
4342 		ret = os_snprintf(pos, end - pos, " FT-EAP");
4343 		if (os_snprintf_error(end - pos, ret))
4344 			return pos - buf;
4345 		pos += ret;
4346 	}
4347 #ifdef CONFIG_SAE
4348 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_SAE) {
4349 		ret = os_snprintf(pos, end - pos, " FT-SAE");
4350 		if (os_snprintf_error(end - pos, ret))
4351 			return pos - buf;
4352 		pos += ret;
4353 	}
4354 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_SAE_EXT_KEY) {
4355 		ret = os_snprintf(pos, end - pos, " FT-SAE-EXT-KEY");
4356 		if (os_snprintf_error(end - pos, ret))
4357 			return pos - buf;
4358 		pos += ret;
4359 	}
4360 #endif /* CONFIG_SAE */
4361 #ifdef CONFIG_SHA384
4362 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_802_1X_SHA384) {
4363 		ret = os_snprintf(pos, end - pos, " FT-EAP-SHA384");
4364 		if (os_snprintf_error(end - pos, ret))
4365 			return pos - buf;
4366 		pos += ret;
4367 	}
4368 #endif /* CONFIG_SHA384 */
4369 #endif /* CONFIG_IEEE80211R */
4370 #ifdef CONFIG_SAE
4371 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE) {
4372 		ret = os_snprintf(pos, end - pos, " SAE");
4373 		if (os_snprintf_error(end - pos, ret))
4374 			return pos - buf;
4375 		pos += ret;
4376 	}
4377 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE_EXT_KEY) {
4378 		ret = os_snprintf(pos, end - pos, " SAE-EXT-KEY");
4379 		if (os_snprintf_error(end - pos, ret))
4380 			return pos - buf;
4381 		pos += ret;
4382 	}
4383 #endif /* CONFIG_SAE */
4384 #ifdef CONFIG_SHA256
4385 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_802_1X_SHA256) {
4386 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SHA256");
4387 		if (os_snprintf_error(end - pos, ret))
4388 			return pos - buf;
4389 		pos += ret;
4390 	}
4391 
4392 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_PSK_SHA256) {
4393 		ret = os_snprintf(pos, end - pos, " WPA-PSK-SHA256");
4394 		if (os_snprintf_error(end - pos, ret))
4395 			return pos - buf;
4396 		pos += ret;
4397 	}
4398 #endif /* CONFIG_SHA256 */
4399 #ifdef CONFIG_HS20
4400 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OSEN) {
4401 		ret = os_snprintf(pos, end - pos, " OSEN");
4402 		if (os_snprintf_error(end - pos, ret))
4403 			return pos - buf;
4404 		pos += ret;
4405 	}
4406 #endif /* CONFIG_HS20 */
4407 
4408 	return pos - buf;
4409 }
4410 
4411 
ctrl_iface_get_capability_proto(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4412 static int ctrl_iface_get_capability_proto(int res, bool strict,
4413 					   struct wpa_driver_capa *capa,
4414 					   char *buf, size_t buflen)
4415 {
4416 	int ret;
4417 	char *pos, *end;
4418 	size_t len;
4419 
4420 	pos = buf;
4421 	end = pos + buflen;
4422 
4423 	if (res < 0) {
4424 		if (strict)
4425 			return 0;
4426 		len = os_strlcpy(buf, "RSN WPA", buflen);
4427 		if (len >= buflen)
4428 			return -1;
4429 		return len;
4430 	}
4431 
4432 	if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
4433 			      WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
4434 		ret = os_snprintf(pos, end - pos, "%sRSN",
4435 				  pos == buf ? "" : " ");
4436 		if (os_snprintf_error(end - pos, ret))
4437 			return pos - buf;
4438 		pos += ret;
4439 	}
4440 
4441 	if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4442 			      WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
4443 		ret = os_snprintf(pos, end - pos, "%sWPA",
4444 				  pos == buf ? "" : " ");
4445 		if (os_snprintf_error(end - pos, ret))
4446 			return pos - buf;
4447 		pos += ret;
4448 	}
4449 
4450 	return pos - buf;
4451 }
4452 
4453 
ctrl_iface_get_capability_auth_alg(struct wpa_supplicant * wpa_s,int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4454 static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
4455 					      int res, bool strict,
4456 					      struct wpa_driver_capa *capa,
4457 					      char *buf, size_t buflen)
4458 {
4459 	int ret;
4460 	char *pos, *end;
4461 	size_t len;
4462 
4463 	pos = buf;
4464 	end = pos + buflen;
4465 
4466 	if (res < 0) {
4467 		if (strict)
4468 			return 0;
4469 		len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
4470 		if (len >= buflen)
4471 			return -1;
4472 		return len;
4473 	}
4474 
4475 	if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
4476 		ret = os_snprintf(pos, end - pos, "%sOPEN",
4477 				  pos == buf ? "" : " ");
4478 		if (os_snprintf_error(end - pos, ret))
4479 			return pos - buf;
4480 		pos += ret;
4481 	}
4482 
4483 	if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
4484 		ret = os_snprintf(pos, end - pos, "%sSHARED",
4485 				  pos == buf ? "" : " ");
4486 		if (os_snprintf_error(end - pos, ret))
4487 			return pos - buf;
4488 		pos += ret;
4489 	}
4490 
4491 	if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
4492 		ret = os_snprintf(pos, end - pos, "%sLEAP",
4493 				  pos == buf ? "" : " ");
4494 		if (os_snprintf_error(end - pos, ret))
4495 			return pos - buf;
4496 		pos += ret;
4497 	}
4498 
4499 #ifdef CONFIG_SAE
4500 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
4501 		ret = os_snprintf(pos, end - pos, "%sSAE",
4502 				  pos == buf ? "" : " ");
4503 		if (os_snprintf_error(end - pos, ret))
4504 			return pos - buf;
4505 		pos += ret;
4506 	}
4507 #endif /* CONFIG_SAE */
4508 
4509 #ifdef CONFIG_FILS
4510 	if (wpa_is_fils_supported(wpa_s)) {
4511 		ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITHOUT_PFS",
4512 				  pos == buf ? "" : " ");
4513 		if (os_snprintf_error(end - pos, ret))
4514 			return pos - buf;
4515 		pos += ret;
4516 	}
4517 
4518 #ifdef CONFIG_FILS_SK_PFS
4519 	if (wpa_is_fils_sk_pfs_supported(wpa_s)) {
4520 		ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITH_PFS",
4521 				  pos == buf ? "" : " ");
4522 		if (os_snprintf_error(end - pos, ret))
4523 			return pos - buf;
4524 		pos += ret;
4525 	}
4526 #endif /* CONFIG_FILS_SK_PFS */
4527 #endif /* CONFIG_FILS */
4528 
4529 #ifdef CONFIG_PASN
4530 	ret = os_snprintf(pos, end - pos, "%sPASN",
4531 			  pos == buf ? "" : " ");
4532 	if (os_snprintf_error(end - pos, ret))
4533 		return pos - buf;
4534 	pos += ret;
4535 
4536 #endif /* CONFIG_PASN */
4537 
4538 	return pos - buf;
4539 }
4540 
4541 
ctrl_iface_get_capability_modes(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4542 static int ctrl_iface_get_capability_modes(int res, bool strict,
4543 					   struct wpa_driver_capa *capa,
4544 					   char *buf, size_t buflen)
4545 {
4546 	int ret;
4547 	char *pos, *end;
4548 	size_t len;
4549 
4550 	pos = buf;
4551 	end = pos + buflen;
4552 
4553 	if (res < 0) {
4554 		if (strict)
4555 			return 0;
4556 		len = os_strlcpy(buf, "IBSS AP", buflen);
4557 		if (len >= buflen)
4558 			return -1;
4559 		return len;
4560 	}
4561 
4562 	if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
4563 		ret = os_snprintf(pos, end - pos, "%sIBSS",
4564 				  pos == buf ? "" : " ");
4565 		if (os_snprintf_error(end - pos, ret))
4566 			return pos - buf;
4567 		pos += ret;
4568 	}
4569 
4570 	if (capa->flags & WPA_DRIVER_FLAGS_AP) {
4571 		ret = os_snprintf(pos, end - pos, "%sAP",
4572 				  pos == buf ? "" : " ");
4573 		if (os_snprintf_error(end - pos, ret))
4574 			return pos - buf;
4575 		pos += ret;
4576 	}
4577 
4578 #ifdef CONFIG_MESH
4579 	if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
4580 		ret = os_snprintf(pos, end - pos, "%sMESH",
4581 				  pos == buf ? "" : " ");
4582 		if (os_snprintf_error(end - pos, ret))
4583 			return pos - buf;
4584 		pos += ret;
4585 	}
4586 #endif /* CONFIG_MESH */
4587 
4588 	return pos - buf;
4589 }
4590 
4591 
ctrl_iface_get_capability_channels(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)4592 static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
4593 					      char *buf, size_t buflen)
4594 {
4595 	struct hostapd_channel_data *chnl;
4596 	int ret, i, j;
4597 	char *pos, *end, *hmode;
4598 
4599 	pos = buf;
4600 	end = pos + buflen;
4601 
4602 	for (j = 0; j < wpa_s->hw.num_modes; j++) {
4603 		switch (wpa_s->hw.modes[j].mode) {
4604 		case HOSTAPD_MODE_IEEE80211B:
4605 			hmode = "B";
4606 			break;
4607 		case HOSTAPD_MODE_IEEE80211G:
4608 			hmode = "G";
4609 			break;
4610 		case HOSTAPD_MODE_IEEE80211A:
4611 			hmode = "A";
4612 			break;
4613 		case HOSTAPD_MODE_IEEE80211AD:
4614 			hmode = "AD";
4615 			break;
4616 		default:
4617 			continue;
4618 		}
4619 		ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
4620 		if (os_snprintf_error(end - pos, ret))
4621 			return pos - buf;
4622 		pos += ret;
4623 		chnl = wpa_s->hw.modes[j].channels;
4624 		for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
4625 			if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
4626 				continue;
4627 			ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
4628 			if (os_snprintf_error(end - pos, ret))
4629 				return pos - buf;
4630 			pos += ret;
4631 		}
4632 		ret = os_snprintf(pos, end - pos, "\n");
4633 		if (os_snprintf_error(end - pos, ret))
4634 			return pos - buf;
4635 		pos += ret;
4636 	}
4637 
4638 	return pos - buf;
4639 }
4640 
4641 
ctrl_iface_get_capability_freq(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)4642 static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
4643 					  char *buf, size_t buflen)
4644 {
4645 	struct hostapd_channel_data *chnl;
4646 	int ret, i, j;
4647 	char *pos, *end, *hmode;
4648 
4649 	pos = buf;
4650 	end = pos + buflen;
4651 
4652 	for (j = 0; j < wpa_s->hw.num_modes; j++) {
4653 		switch (wpa_s->hw.modes[j].mode) {
4654 		case HOSTAPD_MODE_IEEE80211B:
4655 			hmode = "B";
4656 			break;
4657 		case HOSTAPD_MODE_IEEE80211G:
4658 			hmode = "G";
4659 			break;
4660 		case HOSTAPD_MODE_IEEE80211A:
4661 			hmode = "A";
4662 			break;
4663 		case HOSTAPD_MODE_IEEE80211AD:
4664 			hmode = "AD";
4665 			break;
4666 		default:
4667 			continue;
4668 		}
4669 		ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
4670 				  hmode);
4671 		if (os_snprintf_error(end - pos, ret))
4672 			return pos - buf;
4673 		pos += ret;
4674 		chnl = wpa_s->hw.modes[j].channels;
4675 		for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
4676 			if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
4677 				continue;
4678 			ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
4679 					  chnl[i].chan, chnl[i].freq,
4680 					  chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
4681 					  " (NO_IR)" : "",
4682 					  chnl[i].flag & HOSTAPD_CHAN_RADAR ?
4683 					  " (DFS)" : "");
4684 
4685 			if (os_snprintf_error(end - pos, ret))
4686 				return pos - buf;
4687 			pos += ret;
4688 		}
4689 		ret = os_snprintf(pos, end - pos, "\n");
4690 		if (os_snprintf_error(end - pos, ret))
4691 			return pos - buf;
4692 		pos += ret;
4693 	}
4694 
4695 	return pos - buf;
4696 }
4697 
4698 
wpa_supplicant_ctrl_iface_get_capability(struct wpa_supplicant * wpa_s,const char * _field,char * buf,size_t buflen)4699 static int wpa_supplicant_ctrl_iface_get_capability(
4700 	struct wpa_supplicant *wpa_s, const char *_field, char *buf,
4701 	size_t buflen)
4702 {
4703 	struct wpa_driver_capa capa;
4704 	int res;
4705 	char *next_param, *curr_param, *iftype = NULL;
4706 	bool strict = false;
4707 	char field[50];
4708 	size_t len;
4709 
4710 	/* Determine whether or not strict checking was requested */
4711 	len = os_strlcpy(field, _field, sizeof(field));
4712 	if (len >= sizeof(field))
4713 		return -1;
4714 
4715 	next_param = os_strchr(field, ' ');
4716 	while (next_param) {
4717 		*next_param++ = '\0';
4718 		curr_param = next_param;
4719 		next_param = os_strchr(next_param, ' ');
4720 
4721 		if (next_param)
4722 			*next_param = '\0';
4723 
4724 		if (os_strcmp(curr_param, "strict") == 0)
4725 			strict = true;
4726 		else if (os_strncmp(curr_param, "iftype=", 7) == 0)
4727 			iftype = curr_param + 7;
4728 		else
4729 			return -1;
4730 	}
4731 
4732 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'%s%s%s",
4733 		   field, iftype ? " iftype=" : "", iftype ? iftype : "",
4734 		   strict ? " strict" : "");
4735 
4736 	if (os_strcmp(field, "eap") == 0) {
4737 		return eap_get_names(buf, buflen);
4738 	}
4739 
4740 	res = wpa_drv_get_capa(wpa_s, &capa);
4741 
4742 	if (os_strcmp(field, "pairwise") == 0)
4743 		return ctrl_iface_get_capability_pairwise(res, strict, &capa,
4744 							  buf, buflen);
4745 
4746 	if (os_strcmp(field, "group") == 0)
4747 		return ctrl_iface_get_capability_group(res, strict, &capa,
4748 						       buf, buflen);
4749 
4750 	if (os_strcmp(field, "group_mgmt") == 0)
4751 		return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
4752 							    buf, buflen);
4753 
4754 	if (os_strcmp(field, "key_mgmt") == 0)
4755 		return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
4756 							  iftype, buf, buflen);
4757 
4758 	if (os_strcmp(field, "proto") == 0)
4759 		return ctrl_iface_get_capability_proto(res, strict, &capa,
4760 						       buf, buflen);
4761 
4762 	if (os_strcmp(field, "auth_alg") == 0)
4763 		return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
4764 							  &capa, buf, buflen);
4765 
4766 	if (os_strcmp(field, "modes") == 0)
4767 		return ctrl_iface_get_capability_modes(res, strict, &capa,
4768 						       buf, buflen);
4769 
4770 	if (os_strcmp(field, "channels") == 0)
4771 		return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
4772 
4773 	if (os_strcmp(field, "freq") == 0)
4774 		return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
4775 
4776 #ifdef CONFIG_TDLS
4777 	if (os_strcmp(field, "tdls") == 0)
4778 		return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
4779 #endif /* CONFIG_TDLS */
4780 
4781 #ifdef CONFIG_ERP
4782 	if (os_strcmp(field, "erp") == 0) {
4783 		res = os_snprintf(buf, buflen, "ERP");
4784 		if (os_snprintf_error(buflen, res))
4785 			return -1;
4786 		return res;
4787 	}
4788 #endif /* CONFIG_EPR */
4789 
4790 #ifdef CONFIG_FIPS
4791 	if (os_strcmp(field, "fips") == 0) {
4792 		res = os_snprintf(buf, buflen, "FIPS");
4793 		if (os_snprintf_error(buflen, res))
4794 			return -1;
4795 		return res;
4796 	}
4797 #endif /* CONFIG_FIPS */
4798 
4799 #ifdef CONFIG_ACS
4800 	if (os_strcmp(field, "acs") == 0) {
4801 		res = os_snprintf(buf, buflen, "ACS");
4802 		if (os_snprintf_error(buflen, res))
4803 			return -1;
4804 		return res;
4805 	}
4806 #endif /* CONFIG_ACS */
4807 
4808 #ifdef CONFIG_FILS
4809 	if (os_strcmp(field, "fils") == 0) {
4810 #ifdef CONFIG_FILS_SK_PFS
4811 		if (wpa_is_fils_supported(wpa_s) &&
4812 		    wpa_is_fils_sk_pfs_supported(wpa_s)) {
4813 			res = os_snprintf(buf, buflen, "FILS FILS-SK-PFS");
4814 			if (os_snprintf_error(buflen, res))
4815 				return -1;
4816 			return res;
4817 		}
4818 #endif /* CONFIG_FILS_SK_PFS */
4819 
4820 		if (wpa_is_fils_supported(wpa_s)) {
4821 			res = os_snprintf(buf, buflen, "FILS");
4822 			if (os_snprintf_error(buflen, res))
4823 				return -1;
4824 			return res;
4825 		}
4826 	}
4827 #endif /* CONFIG_FILS */
4828 
4829 	if (os_strcmp(field, "multibss") == 0 && wpa_s->multi_bss_support) {
4830 		res = os_snprintf(buf, buflen, "MULTIBSS-STA");
4831 		if (os_snprintf_error(buflen, res))
4832 			return -1;
4833 		return res;
4834 	}
4835 
4836 #ifdef CONFIG_DPP
4837 	if (os_strcmp(field, "dpp") == 0) {
4838 #ifdef CONFIG_DPP3
4839 		res = os_snprintf(buf, buflen, "DPP=3");
4840 #elif defined(CONFIG_DPP2)
4841 		res = os_snprintf(buf, buflen, "DPP=2");
4842 #else /* CONFIG_DPP2 */
4843 		res = os_snprintf(buf, buflen, "DPP=1");
4844 #endif /* CONFIG_DPP2 */
4845 		if (os_snprintf_error(buflen, res))
4846 			return -1;
4847 		return res;
4848 	}
4849 #endif /* CONFIG_DPP */
4850 
4851 #ifdef CONFIG_SAE
4852 	if (os_strcmp(field, "sae") == 0 &&
4853 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE)) {
4854 #ifdef CONFIG_SAE_PK
4855 		res = os_snprintf(buf, buflen, "H2E PK");
4856 #else /* CONFIG_SAE_PK */
4857 		res = os_snprintf(buf, buflen, "H2E");
4858 #endif /* CONFIG_SAE_PK */
4859 		if (os_snprintf_error(buflen, res))
4860 			return -1;
4861 		return res;
4862 	}
4863 #endif /* CONFIG_SAE */
4864 
4865 #ifdef CONFIG_OCV
4866 	if (os_strcmp(field, "ocv") == 0) {
4867 		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
4868 		    (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_OCV))
4869 			res = os_snprintf(buf, buflen, "supported");
4870 		else
4871 			res = os_snprintf(buf, buflen, "not supported");
4872 		if (os_snprintf_error(buflen, res))
4873 			return -1;
4874 		return res;
4875 	}
4876 #endif /* CONFIG_OCV */
4877 
4878 	if (os_strcmp(field, "beacon_prot") == 0) {
4879 		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_BEACON_PROTECTION) ||
4880 		    (wpa_s->drv_flags2 &
4881 		     WPA_DRIVER_FLAGS2_BEACON_PROTECTION_CLIENT))
4882 			res = os_snprintf(buf, buflen, "supported");
4883 		else
4884 			res = os_snprintf(buf, buflen, "not supported");
4885 		if (os_snprintf_error(buflen, res))
4886 			return -1;
4887 		return res;
4888 	}
4889 
4890 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
4891 		   field);
4892 
4893 	return -1;
4894 }
4895 
4896 
4897 #ifdef CONFIG_INTERWORKING
anqp_add_hex(char * pos,char * end,const char * title,struct wpabuf * data)4898 static char * anqp_add_hex(char *pos, char *end, const char *title,
4899 			   struct wpabuf *data)
4900 {
4901 	char *start = pos;
4902 	size_t i;
4903 	int ret;
4904 	const u8 *d;
4905 
4906 	if (data == NULL)
4907 		return start;
4908 
4909 	ret = os_snprintf(pos, end - pos, "%s=", title);
4910 	if (os_snprintf_error(end - pos, ret))
4911 		return start;
4912 	pos += ret;
4913 
4914 	d = wpabuf_head_u8(data);
4915 	for (i = 0; i < wpabuf_len(data); i++) {
4916 		ret = os_snprintf(pos, end - pos, "%02x", *d++);
4917 		if (os_snprintf_error(end - pos, ret))
4918 			return start;
4919 		pos += ret;
4920 	}
4921 
4922 	ret = os_snprintf(pos, end - pos, "\n");
4923 	if (os_snprintf_error(end - pos, ret))
4924 		return start;
4925 	pos += ret;
4926 
4927 	return pos;
4928 }
4929 #endif /* CONFIG_INTERWORKING */
4930 
4931 
4932 #ifdef CONFIG_FILS
print_fils_indication(struct wpa_bss * bss,char * pos,char * end)4933 static int print_fils_indication(struct wpa_bss *bss, char *pos, char *end)
4934 {
4935 	char *start = pos;
4936 	const u8 *ie, *ie_end;
4937 	u16 info, realms;
4938 	int ret;
4939 
4940 	ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
4941 	if (!ie)
4942 		return 0;
4943 	ie_end = ie + 2 + ie[1];
4944 	ie += 2;
4945 	if (ie_end - ie < 2)
4946 		return -1;
4947 
4948 	info = WPA_GET_LE16(ie);
4949 	ie += 2;
4950 	ret = os_snprintf(pos, end - pos, "fils_info=%04x\n", info);
4951 	if (os_snprintf_error(end - pos, ret))
4952 		return 0;
4953 	pos += ret;
4954 
4955 	if (info & BIT(7)) {
4956 		/* Cache Identifier Included */
4957 		if (ie_end - ie < 2)
4958 			return -1;
4959 		ret = os_snprintf(pos, end - pos, "fils_cache_id=%02x%02x\n",
4960 				  ie[0], ie[1]);
4961 		if (os_snprintf_error(end - pos, ret))
4962 			return 0;
4963 		pos += ret;
4964 		ie += 2;
4965 	}
4966 
4967 	if (info & BIT(8)) {
4968 		/* HESSID Included */
4969 		if (ie_end - ie < ETH_ALEN)
4970 			return -1;
4971 		ret = os_snprintf(pos, end - pos, "fils_hessid=" MACSTR "\n",
4972 				  MAC2STR(ie));
4973 		if (os_snprintf_error(end - pos, ret))
4974 			return 0;
4975 		pos += ret;
4976 		ie += ETH_ALEN;
4977 	}
4978 
4979 	realms = (info & (BIT(3) | BIT(4) | BIT(5))) >> 3;
4980 	if (realms) {
4981 		if (ie_end - ie < realms * 2)
4982 			return -1;
4983 		ret = os_snprintf(pos, end - pos, "fils_realms=");
4984 		if (os_snprintf_error(end - pos, ret))
4985 			return 0;
4986 		pos += ret;
4987 
4988 		ret = wpa_snprintf_hex(pos, end - pos, ie, realms * 2);
4989 		if (ret <= 0)
4990 			return 0;
4991 		pos += ret;
4992 		ie += realms * 2;
4993 		ret = os_snprintf(pos, end - pos, "\n");
4994 		if (os_snprintf_error(end - pos, ret))
4995 			return 0;
4996 		pos += ret;
4997 	}
4998 
4999 	return pos - start;
5000 }
5001 #endif /* CONFIG_FILS */
5002 
5003 
print_rnr(struct wpa_bss * bss,char * pos,char * end)5004 static int print_rnr(struct wpa_bss *bss, char *pos, char *end)
5005 {
5006 	char *start = pos;
5007 	const u8 *ie, *ie_end;
5008 	unsigned int n = 0;
5009 	int ret;
5010 
5011 	ie = wpa_bss_get_ie(bss, WLAN_EID_REDUCED_NEIGHBOR_REPORT);
5012 	if (!ie)
5013 		return 0;
5014 
5015 	ie_end = ie + 2 + ie[1];
5016 	ie += 2;
5017 
5018 	while (ie < ie_end) {
5019 		const struct ieee80211_neighbor_ap_info *info =
5020 			(const struct ieee80211_neighbor_ap_info *) ie;
5021 		const u8 *tbtt_start;
5022 		size_t left = ie_end - ie;
5023 
5024 		if (left < sizeof(struct ieee80211_neighbor_ap_info))
5025 			return 0;
5026 
5027 		left -= sizeof(struct ieee80211_neighbor_ap_info);
5028 		if (left < info->tbtt_info_len)
5029 			return 0;
5030 
5031 		ret = os_snprintf(pos, end - pos,
5032 				  "ap_info[%u]: tbtt_info: hdr=0x%x, len=%u, op_c=%u, channel=%u, ",
5033 				  n, *ie, info->tbtt_info_len,
5034 				  info->op_class, info->channel);
5035 		if (os_snprintf_error(end - pos, ret))
5036 			return 0;
5037 		pos += ret;
5038 
5039 		ie += sizeof(struct ieee80211_neighbor_ap_info);
5040 		tbtt_start = ie;
5041 		if (info->tbtt_info_len >= 1) {
5042 			ret = os_snprintf(pos, end - pos,
5043 					  "tbtt_offset=%u, ", *ie);
5044 			if (os_snprintf_error(end - pos, ret))
5045 				return 0;
5046 
5047 			ie++;
5048 			pos += ret;
5049 		}
5050 
5051 		if (info->tbtt_info_len >= 7) {
5052 			ret = os_snprintf(pos, end - pos,
5053 					  "bssid=" MACSTR ", ",
5054 					  MAC2STR(ie));
5055 			if (os_snprintf_error(end - pos, ret))
5056 				return 0;
5057 
5058 			ie += ETH_ALEN;
5059 			pos += ret;
5060 		}
5061 
5062 		if (info->tbtt_info_len >= 11) {
5063 			ret = os_snprintf(pos, end - pos,
5064 					  "short SSID=0x%x, ",
5065 					  WPA_GET_LE32(ie));
5066 			if (os_snprintf_error(end - pos, ret))
5067 				return 0;
5068 
5069 			ie += 4;
5070 			pos += ret;
5071 		}
5072 
5073 		if (info->tbtt_info_len >= 12) {
5074 			ret = os_snprintf(pos, end - pos,
5075 					  "bss_params=0x%x, ", *ie);
5076 			if (os_snprintf_error(end - pos, ret))
5077 				return 0;
5078 
5079 			ie++;
5080 			pos += ret;
5081 		}
5082 
5083 		if (info->tbtt_info_len >= 13) {
5084 			ret = os_snprintf(pos, end - pos,
5085 					  "PSD=0x%x, ", *ie);
5086 			if (os_snprintf_error(end - pos, ret))
5087 				return 0;
5088 
5089 			ie++;
5090 			pos += ret;
5091 		}
5092 
5093 		if (info->tbtt_info_len >= 16) {
5094 			ret = os_snprintf(pos, end - pos,
5095 					  "mld ID=%u, link ID=%u",
5096 					  *ie, *(ie + 1) & 0xF);
5097 			if (os_snprintf_error(end - pos, ret))
5098 				return 0;
5099 
5100 			ie += 3;
5101 			pos += ret;
5102 		}
5103 
5104 		ie = tbtt_start + info->tbtt_info_len;
5105 
5106 		ret = os_snprintf(pos, end - pos, "\n");
5107 		if (os_snprintf_error(end - pos, ret))
5108 			return 0;
5109 		pos += ret;
5110 
5111 		n++;
5112 	}
5113 
5114 	return pos - start;
5115 }
5116 
5117 
print_ml(struct wpa_bss * bss,char * pos,char * end)5118 static int print_ml(struct wpa_bss *bss, char *pos, char *end)
5119 {
5120 	const struct ieee80211_eht_ml *ml;
5121 	char *start = pos;
5122 	const u8 *ie, *ie_end;
5123 	u16 ml_control;
5124 	u8 common_info_length;
5125 	int ret;
5126 
5127 	ie = get_ml_ie(wpa_bss_ie_ptr(bss), bss->ie_len,
5128 		       MULTI_LINK_CONTROL_TYPE_BASIC);
5129 	if (!ie)
5130 		return 0;
5131 
5132 	ie_end = ie + 2 + ie[1];
5133 	ie += 3;
5134 	ml = (const struct ieee80211_eht_ml *) ie;
5135 
5136 	/* control + common info length + MLD MAC Address */
5137 	if (ie_end - ie < 2 + 1 + ETH_ALEN)
5138 		return 0;
5139 
5140 	ml_control = le_to_host16(ml->ml_control);
5141 
5142 	common_info_length = *(ie + 2);
5143 	ret = os_snprintf(pos, end - pos,
5144 			  "multi-link: control=0x%x, common info len=%u",
5145 			  ml_control, common_info_length);
5146 	if (os_snprintf_error(end - pos, ret))
5147 		return 0;
5148 	pos += ret;
5149 
5150 	ie += 2;
5151 	if (ie_end - ie < common_info_length)
5152 		return 0;
5153 
5154 	ie++;
5155 	common_info_length--;
5156 
5157 	if (common_info_length < ETH_ALEN)
5158 		return 0;
5159 
5160 	ret = os_snprintf(pos, end - pos, ", MLD addr=" MACSTR, MAC2STR(ie));
5161 	if (os_snprintf_error(end - pos, ret))
5162 		return 0;
5163 	pos += ret;
5164 
5165 	ie += ETH_ALEN;
5166 	common_info_length -= ETH_ALEN;
5167 
5168 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_LINK_ID) {
5169 		if (common_info_length < 1)
5170 			return 0;
5171 
5172 		ret = os_snprintf(pos, end - pos, ", link ID=%u", *ie & 0x0f);
5173 		if (os_snprintf_error(end - pos, ret))
5174 			return 0;
5175 		pos += ret;
5176 		ie++;
5177 		common_info_length--;
5178 	}
5179 
5180 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_BSS_PARAM_CH_COUNT) {
5181 		if (common_info_length < 1)
5182 			return 0;
5183 
5184 		ret = os_snprintf(pos, end - pos,
5185 				  ", BSS change parameters=0x%x", *ie);
5186 		if (os_snprintf_error(end - pos, ret))
5187 			return 0;
5188 		pos += ret;
5189 		ie++;
5190 		common_info_length--;
5191 	}
5192 
5193 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_MSD_INFO) {
5194 		if (common_info_length < 2)
5195 			return 0;
5196 
5197 		ret = os_snprintf(pos, end - pos, ", MSD Info=0x%x",
5198 				  WPA_GET_LE16(ie));
5199 		if (os_snprintf_error(end - pos, ret))
5200 			return 0;
5201 		pos += ret;
5202 		ie += 2;
5203 		common_info_length -= 2;
5204 	}
5205 
5206 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_EML_CAPA) {
5207 		if (common_info_length < 2)
5208 			return 0;
5209 
5210 		ret = os_snprintf(pos, end - pos, ", EML capabilities=0x%x",
5211 				  WPA_GET_LE16(ie));
5212 		if (os_snprintf_error(end - pos, ret))
5213 			return 0;
5214 		pos += ret;
5215 		ie += 2;
5216 		common_info_length -= 2;
5217 	}
5218 
5219 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_MLD_CAPA) {
5220 		if (common_info_length < 2)
5221 			return 0;
5222 
5223 		ret = os_snprintf(pos, end - pos, ", MLD capabilities=0x%x",
5224 				  WPA_GET_LE16(ie));
5225 		if (os_snprintf_error(end - pos, ret))
5226 			return 0;
5227 		pos += ret;
5228 		ie += 2;
5229 		common_info_length -= 2;
5230 	}
5231 
5232 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_AP_MLD_ID) {
5233 		if (common_info_length < 1)
5234 			return 0;
5235 
5236 		ret = os_snprintf(pos, end - pos, ", MLD ID=0x%x\n", *ie);
5237 		if (os_snprintf_error(end - pos, ret))
5238 			return 0;
5239 		pos += ret;
5240 		ie += 1;
5241 		common_info_length--;
5242 	}
5243 
5244 	return pos - start;
5245 }
5246 
5247 
print_bss_info(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,unsigned long mask,char * buf,size_t buflen)5248 static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5249 			  unsigned long mask, char *buf, size_t buflen)
5250 {
5251 	size_t i;
5252 	int ret;
5253 	char *pos, *end;
5254 	const u8 *ie, *ie2, *osen_ie, *mesh, *owe, *rsnxe;
5255 
5256 	pos = buf;
5257 	end = buf + buflen;
5258 
5259 	if (mask & WPA_BSS_MASK_ID) {
5260 		ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
5261 		if (os_snprintf_error(end - pos, ret))
5262 			return 0;
5263 		pos += ret;
5264 	}
5265 
5266 	if (mask & WPA_BSS_MASK_BSSID) {
5267 		ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
5268 				  MAC2STR(bss->bssid));
5269 		if (os_snprintf_error(end - pos, ret))
5270 			return 0;
5271 		pos += ret;
5272 	}
5273 
5274 	if (mask & WPA_BSS_MASK_FREQ) {
5275 		ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
5276 		if (os_snprintf_error(end - pos, ret))
5277 			return 0;
5278 		pos += ret;
5279 	}
5280 
5281 	if (mask & WPA_BSS_MASK_BEACON_INT) {
5282 		ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
5283 				  bss->beacon_int);
5284 		if (os_snprintf_error(end - pos, ret))
5285 			return 0;
5286 		pos += ret;
5287 	}
5288 
5289 	if (mask & WPA_BSS_MASK_CAPABILITIES) {
5290 		ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
5291 				  bss->caps);
5292 		if (os_snprintf_error(end - pos, ret))
5293 			return 0;
5294 		pos += ret;
5295 	}
5296 
5297 	if (mask & WPA_BSS_MASK_QUAL) {
5298 		ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
5299 		if (os_snprintf_error(end - pos, ret))
5300 			return 0;
5301 		pos += ret;
5302 	}
5303 
5304 	if (mask & WPA_BSS_MASK_NOISE) {
5305 		ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
5306 		if (os_snprintf_error(end - pos, ret))
5307 			return 0;
5308 		pos += ret;
5309 	}
5310 
5311 	if (mask & WPA_BSS_MASK_LEVEL) {
5312 		ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
5313 		if (os_snprintf_error(end - pos, ret))
5314 			return 0;
5315 		pos += ret;
5316 	}
5317 
5318 	if (mask & WPA_BSS_MASK_TSF) {
5319 		ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
5320 				  (unsigned long long) bss->tsf);
5321 		if (os_snprintf_error(end - pos, ret))
5322 			return 0;
5323 		pos += ret;
5324 	}
5325 
5326 	if (mask & WPA_BSS_MASK_AGE) {
5327 		struct os_reltime now;
5328 
5329 		os_get_reltime(&now);
5330 		ret = os_snprintf(pos, end - pos, "age=%d\n",
5331 				  (int) (now.sec - bss->last_update.sec));
5332 		if (os_snprintf_error(end - pos, ret))
5333 			return 0;
5334 		pos += ret;
5335 	}
5336 
5337 	if (mask & WPA_BSS_MASK_IE) {
5338 		ret = os_snprintf(pos, end - pos, "ie=");
5339 		if (os_snprintf_error(end - pos, ret))
5340 			return 0;
5341 		pos += ret;
5342 
5343 		ie = wpa_bss_ie_ptr(bss);
5344 		for (i = 0; i < bss->ie_len; i++) {
5345 			ret = os_snprintf(pos, end - pos, "%02x", *ie++);
5346 			if (os_snprintf_error(end - pos, ret))
5347 				return 0;
5348 			pos += ret;
5349 		}
5350 
5351 		ret = os_snprintf(pos, end - pos, "\n");
5352 		if (os_snprintf_error(end - pos, ret))
5353 			return 0;
5354 		pos += ret;
5355 	}
5356 
5357 	if (mask & WPA_BSS_MASK_FLAGS) {
5358 		ret = os_snprintf(pos, end - pos, "flags=");
5359 		if (os_snprintf_error(end - pos, ret))
5360 			return 0;
5361 		pos += ret;
5362 
5363 		mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
5364 
5365 		ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
5366 		if (ie)
5367 			pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
5368 						    2 + ie[1]);
5369 		ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
5370 		if (ie2)
5371 			pos = wpa_supplicant_ie_txt(pos, end,
5372 						    mesh ? "RSN" : "WPA2", ie2,
5373 						    2 + ie2[1]);
5374 		rsnxe = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
5375 		if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_H2E)) {
5376 			ret = os_snprintf(pos, end - pos, "[SAE-H2E]");
5377 			if (os_snprintf_error(end - pos, ret))
5378 				return -1;
5379 			pos += ret;
5380 		}
5381 		if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_PK)) {
5382 			ret = os_snprintf(pos, end - pos, "[SAE-PK]");
5383 			if (os_snprintf_error(end - pos, ret))
5384 				return -1;
5385 			pos += ret;
5386 		}
5387 		osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
5388 		if (osen_ie)
5389 			pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
5390 						    osen_ie, 2 + osen_ie[1]);
5391 		owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
5392 		if (owe) {
5393 			ret = os_snprintf(
5394 				pos, end - pos,
5395 				ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
5396 			if (os_snprintf_error(end - pos, ret))
5397 				return 0;
5398 			pos += ret;
5399 		}
5400 		pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
5401 		if (!ie && !ie2 && !osen_ie &&
5402 		    (bss->caps & IEEE80211_CAP_PRIVACY)) {
5403 			ret = os_snprintf(pos, end - pos, "[WEP]");
5404 			if (os_snprintf_error(end - pos, ret))
5405 				return 0;
5406 			pos += ret;
5407 		}
5408 
5409 		if (mesh) {
5410 			ret = os_snprintf(pos, end - pos, "[MESH]");
5411 			if (os_snprintf_error(end - pos, ret))
5412 				return 0;
5413 			pos += ret;
5414 		}
5415 
5416 		if (bss_is_dmg(bss)) {
5417 			const char *s;
5418 			ret = os_snprintf(pos, end - pos, "[DMG]");
5419 			if (os_snprintf_error(end - pos, ret))
5420 				return 0;
5421 			pos += ret;
5422 			switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
5423 			case IEEE80211_CAP_DMG_IBSS:
5424 				s = "[IBSS]";
5425 				break;
5426 			case IEEE80211_CAP_DMG_AP:
5427 				s = "[ESS]";
5428 				break;
5429 			case IEEE80211_CAP_DMG_PBSS:
5430 				s = "[PBSS]";
5431 				break;
5432 			default:
5433 				s = "";
5434 				break;
5435 			}
5436 			ret = os_snprintf(pos, end - pos, "%s", s);
5437 			if (os_snprintf_error(end - pos, ret))
5438 				return 0;
5439 			pos += ret;
5440 		} else {
5441 			if (bss->caps & IEEE80211_CAP_IBSS) {
5442 				ret = os_snprintf(pos, end - pos, "[IBSS]");
5443 				if (os_snprintf_error(end - pos, ret))
5444 					return 0;
5445 				pos += ret;
5446 			}
5447 			if (bss->caps & IEEE80211_CAP_ESS) {
5448 				ret = os_snprintf(pos, end - pos, "[ESS]");
5449 				if (os_snprintf_error(end - pos, ret))
5450 					return 0;
5451 				pos += ret;
5452 			}
5453 		}
5454 		if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
5455 		    wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
5456 			ret = os_snprintf(pos, end - pos, "[P2P]");
5457 			if (os_snprintf_error(end - pos, ret))
5458 				return 0;
5459 			pos += ret;
5460 		}
5461 #ifdef CONFIG_HS20
5462 		if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
5463 			ret = os_snprintf(pos, end - pos, "[HS20]");
5464 			if (os_snprintf_error(end - pos, ret))
5465 				return 0;
5466 			pos += ret;
5467 		}
5468 #endif /* CONFIG_HS20 */
5469 #ifdef CONFIG_FILS
5470 		if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
5471 			ret = os_snprintf(pos, end - pos, "[FILS]");
5472 			if (os_snprintf_error(end - pos, ret))
5473 				return 0;
5474 			pos += ret;
5475 		}
5476 #endif /* CONFIG_FILS */
5477 #ifdef CONFIG_FST
5478 		if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
5479 			ret = os_snprintf(pos, end - pos, "[FST]");
5480 			if (os_snprintf_error(end - pos, ret))
5481 				return 0;
5482 			pos += ret;
5483 		}
5484 #endif /* CONFIG_FST */
5485 		if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_UTF_8_SSID)) {
5486 			ret = os_snprintf(pos, end - pos, "[UTF-8]");
5487 			if (os_snprintf_error(end - pos, ret))
5488 				return 0;
5489 			pos += ret;
5490 		}
5491 
5492 		ret = os_snprintf(pos, end - pos, "\n");
5493 		if (os_snprintf_error(end - pos, ret))
5494 			return 0;
5495 		pos += ret;
5496 	}
5497 
5498 	if (mask & WPA_BSS_MASK_SSID) {
5499 		ret = os_snprintf(pos, end - pos, "ssid=%s\n",
5500 				  wpa_ssid_txt(bss->ssid, bss->ssid_len));
5501 		if (os_snprintf_error(end - pos, ret))
5502 			return 0;
5503 		pos += ret;
5504 	}
5505 
5506 #ifdef CONFIG_WPS
5507 	if (mask & WPA_BSS_MASK_WPS_SCAN) {
5508 		ie = wpa_bss_ie_ptr(bss);
5509 		ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
5510 		if (ret >= end - pos)
5511 			return 0;
5512 		if (ret > 0)
5513 			pos += ret;
5514 	}
5515 #endif /* CONFIG_WPS */
5516 
5517 #ifdef CONFIG_P2P
5518 	if (mask & WPA_BSS_MASK_P2P_SCAN) {
5519 		ie = wpa_bss_ie_ptr(bss);
5520 		ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
5521 		if (ret >= end - pos)
5522 			return 0;
5523 		if (ret > 0)
5524 			pos += ret;
5525 	}
5526 #endif /* CONFIG_P2P */
5527 
5528 #ifdef CONFIG_WIFI_DISPLAY
5529 	if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
5530 		struct wpabuf *wfd;
5531 
5532 		ie = wpa_bss_ie_ptr(bss);
5533 		wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
5534 						  WFD_IE_VENDOR_TYPE);
5535 		if (wfd) {
5536 			ret = os_snprintf(pos, end - pos, "wfd_subelems=");
5537 			if (os_snprintf_error(end - pos, ret)) {
5538 				wpabuf_free(wfd);
5539 				return 0;
5540 			}
5541 			pos += ret;
5542 
5543 			pos += wpa_snprintf_hex(pos, end - pos,
5544 						wpabuf_head(wfd),
5545 						wpabuf_len(wfd));
5546 			wpabuf_free(wfd);
5547 
5548 			ret = os_snprintf(pos, end - pos, "\n");
5549 			if (os_snprintf_error(end - pos, ret))
5550 				return 0;
5551 			pos += ret;
5552 		}
5553 	}
5554 #endif /* CONFIG_WIFI_DISPLAY */
5555 
5556 #ifdef CONFIG_INTERWORKING
5557 	if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
5558 		struct wpa_bss_anqp *anqp = bss->anqp;
5559 		struct wpa_bss_anqp_elem *elem;
5560 
5561 		pos = anqp_add_hex(pos, end, "anqp_capability_list",
5562 				   anqp->capability_list);
5563 		pos = anqp_add_hex(pos, end, "anqp_venue_name",
5564 				   anqp->venue_name);
5565 		pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
5566 				   anqp->network_auth_type);
5567 		pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
5568 				   anqp->roaming_consortium);
5569 		pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
5570 				   anqp->ip_addr_type_availability);
5571 		pos = anqp_add_hex(pos, end, "anqp_nai_realm",
5572 				   anqp->nai_realm);
5573 		pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
5574 		pos = anqp_add_hex(pos, end, "anqp_domain_name",
5575 				   anqp->domain_name);
5576 		pos = anqp_add_hex(pos, end, "anqp_fils_realm_info",
5577 				   anqp->fils_realm_info);
5578 #ifdef CONFIG_HS20
5579 		pos = anqp_add_hex(pos, end, "hs20_capability_list",
5580 				   anqp->hs20_capability_list);
5581 		pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
5582 				   anqp->hs20_operator_friendly_name);
5583 		pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
5584 				   anqp->hs20_wan_metrics);
5585 		pos = anqp_add_hex(pos, end, "hs20_connection_capability",
5586 				   anqp->hs20_connection_capability);
5587 		pos = anqp_add_hex(pos, end, "hs20_operating_class",
5588 				   anqp->hs20_operating_class);
5589 		pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
5590 				   anqp->hs20_osu_providers_list);
5591 		pos = anqp_add_hex(pos, end, "hs20_operator_icon_metadata",
5592 				   anqp->hs20_operator_icon_metadata);
5593 		pos = anqp_add_hex(pos, end, "hs20_osu_providers_nai_list",
5594 				   anqp->hs20_osu_providers_nai_list);
5595 #endif /* CONFIG_HS20 */
5596 
5597 		dl_list_for_each(elem, &anqp->anqp_elems,
5598 				 struct wpa_bss_anqp_elem, list) {
5599 			char title[20];
5600 
5601 			os_snprintf(title, sizeof(title), "anqp[%u]",
5602 				    elem->infoid);
5603 			pos = anqp_add_hex(pos, end, title, elem->payload);
5604 			if (elem->protected_response) {
5605 				ret = os_snprintf(pos, end - pos,
5606 						  "protected-anqp-info[%u]=1\n",
5607 						  elem->infoid);
5608 				if (os_snprintf_error(end - pos, ret))
5609 					return 0;
5610 				pos += ret;
5611 			}
5612 		}
5613 	}
5614 #endif /* CONFIG_INTERWORKING */
5615 
5616 #ifdef CONFIG_MESH
5617 	if (mask & WPA_BSS_MASK_MESH_SCAN) {
5618 		ie = wpa_bss_ie_ptr(bss);
5619 		ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
5620 		if (ret >= end - pos)
5621 			return 0;
5622 		if (ret > 0)
5623 			pos += ret;
5624 	}
5625 #endif /* CONFIG_MESH */
5626 
5627 	if (mask & WPA_BSS_MASK_SNR) {
5628 		ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
5629 		if (os_snprintf_error(end - pos, ret))
5630 			return 0;
5631 		pos += ret;
5632 	}
5633 
5634 	if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
5635 		ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
5636 				  bss->est_throughput);
5637 		if (os_snprintf_error(end - pos, ret))
5638 			return 0;
5639 		pos += ret;
5640 	}
5641 
5642 #ifdef CONFIG_FST
5643 	if (mask & WPA_BSS_MASK_FST) {
5644 		ret = fst_ctrl_iface_mb_info(bss->bssid, pos, end - pos);
5645 		if (ret < 0 || ret >= end - pos)
5646 			return 0;
5647 		pos += ret;
5648 	}
5649 #endif /* CONFIG_FST */
5650 
5651 	if (mask & WPA_BSS_MASK_UPDATE_IDX) {
5652 		ret = os_snprintf(pos, end - pos, "update_idx=%u\n",
5653 				  bss->last_update_idx);
5654 		if (os_snprintf_error(end - pos, ret))
5655 			return 0;
5656 		pos += ret;
5657 	}
5658 
5659 	if ((mask & WPA_BSS_MASK_BEACON_IE) && bss->beacon_ie_len) {
5660 		ret = os_snprintf(pos, end - pos, "beacon_ie=");
5661 		if (os_snprintf_error(end - pos, ret))
5662 			return 0;
5663 		pos += ret;
5664 
5665 		ie = wpa_bss_ie_ptr(bss);
5666 		ie += bss->ie_len;
5667 		for (i = 0; i < bss->beacon_ie_len; i++) {
5668 			ret = os_snprintf(pos, end - pos, "%02x", *ie++);
5669 			if (os_snprintf_error(end - pos, ret))
5670 				return 0;
5671 			pos += ret;
5672 		}
5673 
5674 		ret = os_snprintf(pos, end - pos, "\n");
5675 		if (os_snprintf_error(end - pos, ret))
5676 			return 0;
5677 		pos += ret;
5678 	}
5679 
5680 #ifdef CONFIG_FILS
5681 	if (mask & WPA_BSS_MASK_FILS_INDICATION) {
5682 		ret = print_fils_indication(bss, pos, end);
5683 		if (ret < 0)
5684 			return 0;
5685 		pos += ret;
5686 	}
5687 #endif /* CONFIG_FILS */
5688 
5689 	if (!is_zero_ether_addr(bss->mld_addr)) {
5690 		ret = os_snprintf(pos, end - pos,
5691 				  "ap_mld_addr=" MACSTR "\n",
5692 				  MAC2STR(bss->mld_addr));
5693 		if (os_snprintf_error(end - pos, ret))
5694 			return 0;
5695 		pos += ret;
5696 	}
5697 
5698 	if (mask & WPA_BSS_MASK_RNR)
5699 		pos += print_rnr(bss, pos, end);
5700 
5701 	if (mask & WPA_BSS_MASK_ML)
5702 		pos += print_ml(bss, pos, end);
5703 
5704 	if (mask & WPA_BSS_MASK_DELIM) {
5705 		ret = os_snprintf(pos, end - pos, "====\n");
5706 		if (os_snprintf_error(end - pos, ret))
5707 			return 0;
5708 		pos += ret;
5709 	}
5710 
5711 	return pos - buf;
5712 }
5713 
5714 
wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)5715 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
5716 					 const char *cmd, char *buf,
5717 					 size_t buflen)
5718 {
5719 	u8 bssid[ETH_ALEN];
5720 	size_t i;
5721 	struct wpa_bss *bss;
5722 	struct wpa_bss *bsslast = NULL;
5723 	struct dl_list *next;
5724 	int ret = 0;
5725 	int len;
5726 	char *ctmp, *end = buf + buflen;
5727 	unsigned long mask = WPA_BSS_MASK_ALL;
5728 
5729 	if (os_strncmp(cmd, "RANGE=", 6) == 0) {
5730 		if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
5731 			bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
5732 					    list_id);
5733 			bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
5734 					       list_id);
5735 		} else { /* N1-N2 */
5736 			unsigned int id1, id2;
5737 
5738 			if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
5739 				wpa_printf(MSG_INFO, "Wrong BSS range "
5740 					   "format");
5741 				return 0;
5742 			}
5743 
5744 			if (*(cmd + 6) == '-')
5745 				id1 = 0;
5746 			else
5747 				id1 = atoi(cmd + 6);
5748 			ctmp++;
5749 			if (*ctmp >= '0' && *ctmp <= '9')
5750 				id2 = atoi(ctmp);
5751 			else
5752 				id2 = (unsigned int) -1;
5753 			bss = wpa_bss_get_id_range(wpa_s, id1, id2);
5754 			if (id2 == (unsigned int) -1)
5755 				bsslast = dl_list_last(&wpa_s->bss_id,
5756 						       struct wpa_bss,
5757 						       list_id);
5758 			else {
5759 				bsslast = wpa_bss_get_id(wpa_s, id2);
5760 				if (bsslast == NULL && bss && id2 > id1) {
5761 					struct wpa_bss *tmp = bss;
5762 					for (;;) {
5763 						next = tmp->list_id.next;
5764 						if (next == &wpa_s->bss_id)
5765 							break;
5766 						tmp = dl_list_entry(
5767 							next, struct wpa_bss,
5768 							list_id);
5769 						if (tmp->id > id2)
5770 							break;
5771 						bsslast = tmp;
5772 					}
5773 				}
5774 			}
5775 		}
5776 	} else if (os_strncmp(cmd, "FIRST", 5) == 0)
5777 		bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
5778 	else if (os_strncmp(cmd, "LAST", 4) == 0)
5779 		bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
5780 	else if (os_strncmp(cmd, "ID-", 3) == 0) {
5781 		i = atoi(cmd + 3);
5782 		bss = wpa_bss_get_id(wpa_s, i);
5783 	} else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
5784 		i = atoi(cmd + 5);
5785 		bss = wpa_bss_get_id(wpa_s, i);
5786 		if (bss) {
5787 			next = bss->list_id.next;
5788 			if (next == &wpa_s->bss_id)
5789 				bss = NULL;
5790 			else
5791 				bss = dl_list_entry(next, struct wpa_bss,
5792 						    list_id);
5793 		}
5794 	} else if (os_strncmp(cmd, "CURRENT", 7) == 0) {
5795 		bss = wpa_s->current_bss;
5796 #ifdef CONFIG_P2P
5797 	} else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
5798 		if (hwaddr_aton(cmd + 13, bssid) == 0)
5799 			bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
5800 		else
5801 			bss = NULL;
5802 #endif /* CONFIG_P2P */
5803 	} else if (hwaddr_aton(cmd, bssid) == 0)
5804 		bss = wpa_bss_get_bssid(wpa_s, bssid);
5805 	else {
5806 		struct wpa_bss *tmp;
5807 		i = atoi(cmd);
5808 		bss = NULL;
5809 		dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
5810 		{
5811 			if (i == 0) {
5812 				bss = tmp;
5813 				break;
5814 			}
5815 			i--;
5816 		}
5817 	}
5818 
5819 	if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
5820 		mask = strtoul(ctmp + 5, NULL, 0x10);
5821 		if (mask == 0)
5822 			mask = WPA_BSS_MASK_ALL;
5823 	}
5824 
5825 	if (bss == NULL)
5826 		return 0;
5827 
5828 	if (bsslast == NULL)
5829 		bsslast = bss;
5830 	do {
5831 		len = print_bss_info(wpa_s, bss, mask, buf, buflen);
5832 		ret += len;
5833 		buf += len;
5834 		buflen -= len;
5835 		if (bss == bsslast) {
5836 			if ((mask & WPA_BSS_MASK_DELIM) && len &&
5837 			    (bss == dl_list_last(&wpa_s->bss_id,
5838 						 struct wpa_bss, list_id))) {
5839 				int res;
5840 
5841 				res = os_snprintf(buf - 5, end - buf + 5,
5842 						  "####\n");
5843 				if (os_snprintf_error(end - buf + 5, res)) {
5844 					wpa_printf(MSG_DEBUG,
5845 						   "Could not add end delim");
5846 				}
5847 			}
5848 			break;
5849 		}
5850 		next = bss->list_id.next;
5851 		if (next == &wpa_s->bss_id)
5852 			break;
5853 		bss = dl_list_entry(next, struct wpa_bss, list_id);
5854 	} while (bss && len);
5855 
5856 	return ret;
5857 }
5858 
5859 
wpa_supplicant_ctrl_iface_ap_scan(struct wpa_supplicant * wpa_s,char * cmd)5860 static int wpa_supplicant_ctrl_iface_ap_scan(
5861 	struct wpa_supplicant *wpa_s, char *cmd)
5862 {
5863 	int ap_scan = atoi(cmd);
5864 	return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
5865 }
5866 
5867 
wpa_supplicant_ctrl_iface_scan_interval(struct wpa_supplicant * wpa_s,char * cmd)5868 static int wpa_supplicant_ctrl_iface_scan_interval(
5869 	struct wpa_supplicant *wpa_s, char *cmd)
5870 {
5871 	int scan_int = atoi(cmd);
5872 	return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
5873 }
5874 
5875 
wpa_supplicant_ctrl_iface_bss_expire_age(struct wpa_supplicant * wpa_s,char * cmd)5876 static int wpa_supplicant_ctrl_iface_bss_expire_age(
5877 	struct wpa_supplicant *wpa_s, char *cmd)
5878 {
5879 	int expire_age = atoi(cmd);
5880 	return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
5881 }
5882 
5883 
wpa_supplicant_ctrl_iface_bss_expire_count(struct wpa_supplicant * wpa_s,char * cmd)5884 static int wpa_supplicant_ctrl_iface_bss_expire_count(
5885 	struct wpa_supplicant *wpa_s, char *cmd)
5886 {
5887 	int expire_count = atoi(cmd);
5888 	return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
5889 }
5890 
5891 
wpa_supplicant_ctrl_iface_bss_flush(struct wpa_supplicant * wpa_s,char * cmd)5892 static void wpa_supplicant_ctrl_iface_bss_flush(
5893 	struct wpa_supplicant *wpa_s, char *cmd)
5894 {
5895 	int flush_age = atoi(cmd);
5896 
5897 	if (flush_age == 0)
5898 		wpa_bss_flush(wpa_s);
5899 	else
5900 		wpa_bss_flush_by_age(wpa_s, flush_age);
5901 }
5902 
5903 
5904 #ifdef CONFIG_TESTING_OPTIONS
wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant * wpa_s)5905 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
5906 {
5907 	wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
5908 	/* MLME-DELETEKEYS.request */
5909 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL,
5910 			0, KEY_FLAG_GROUP);
5911 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL,
5912 			0, KEY_FLAG_GROUP);
5913 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL,
5914 			0, KEY_FLAG_GROUP);
5915 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL,
5916 			0, KEY_FLAG_GROUP);
5917 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL,
5918 			0, KEY_FLAG_GROUP);
5919 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL,
5920 			0, KEY_FLAG_GROUP);
5921 
5922 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0,
5923 			NULL, 0, KEY_FLAG_PAIRWISE);
5924 	if (wpa_sm_ext_key_id(wpa_s->wpa))
5925 		wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, wpa_s->bssid, 1, 0,
5926 				NULL, 0, NULL, 0, KEY_FLAG_PAIRWISE);
5927 	/* MLME-SETPROTECTION.request(None) */
5928 	wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
5929 				   MLME_SETPROTECTION_PROTECT_TYPE_NONE,
5930 				   MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
5931 	wpa_sm_drop_sa(wpa_s->wpa);
5932 }
5933 #endif /* CONFIG_TESTING_OPTIONS */
5934 
5935 
wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant * wpa_s,char * addr)5936 static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
5937 					  char *addr)
5938 {
5939 #ifdef CONFIG_NO_SCAN_PROCESSING
5940 	return -1;
5941 #else /* CONFIG_NO_SCAN_PROCESSING */
5942 	u8 bssid[ETH_ALEN];
5943 	struct wpa_bss *bss;
5944 	struct wpa_ssid *ssid = wpa_s->current_ssid;
5945 	struct wpa_radio_work *already_connecting;
5946 
5947 	if (hwaddr_aton(addr, bssid)) {
5948 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
5949 			   "address '%s'", addr);
5950 		return -1;
5951 	}
5952 
5953 	wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
5954 
5955 	if (!ssid) {
5956 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
5957 			   "configuration known for the target AP");
5958 		return -1;
5959 	}
5960 
5961 	bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
5962 	if (!bss) {
5963 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
5964 			   "from BSS table");
5965 		return -1;
5966 	}
5967 
5968 	/*
5969 	 * TODO: Find best network configuration block from configuration to
5970 	 * allow roaming to other networks
5971 	 */
5972 
5973 	already_connecting = radio_work_pending(wpa_s, "sme-connect");
5974 	wpa_s->reassociate = 1;
5975 	wpa_supplicant_connect(wpa_s, bss, ssid);
5976 
5977 	/*
5978 	 * Indicate that an explicitly requested roam is in progress so scan
5979 	 * results that come in before the 'sme-connect' radio work gets
5980 	 * executed do not override the original connection attempt.
5981 	 */
5982 	if (!already_connecting && radio_work_pending(wpa_s, "sme-connect"))
5983 		wpa_s->roam_in_progress = true;
5984 
5985 	return 0;
5986 #endif /* CONFIG_NO_SCAN_PROCESSING */
5987 }
5988 
5989 
5990 #ifdef CONFIG_P2P
p2p_ctrl_find(struct wpa_supplicant * wpa_s,char * cmd)5991 static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
5992 {
5993 	unsigned int timeout = atoi(cmd);
5994 	enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
5995 	u8 dev_id[ETH_ALEN], *_dev_id = NULL;
5996 	u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
5997 	char *pos;
5998 	unsigned int search_delay;
5999 	const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
6000 	u8 seek_count = 0;
6001 	int freq = 0;
6002 	bool include_6ghz = false;
6003 
6004 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6005 		wpa_dbg(wpa_s, MSG_INFO,
6006 			"Reject P2P_FIND since interface is disabled");
6007 		return -1;
6008 	}
6009 
6010 	if (os_strstr(cmd, " include_6ghz"))
6011 		include_6ghz = true;
6012 	if (os_strstr(cmd, "type=social"))
6013 		type = P2P_FIND_ONLY_SOCIAL;
6014 	else if (os_strstr(cmd, "type=progressive"))
6015 		type = P2P_FIND_PROGRESSIVE;
6016 
6017 	pos = os_strstr(cmd, "dev_id=");
6018 	if (pos) {
6019 		pos += 7;
6020 		if (hwaddr_aton(pos, dev_id))
6021 			return -1;
6022 		_dev_id = dev_id;
6023 	}
6024 
6025 	pos = os_strstr(cmd, "dev_type=");
6026 	if (pos) {
6027 		pos += 9;
6028 		if (wps_dev_type_str2bin(pos, dev_type) < 0)
6029 			return -1;
6030 		_dev_type = dev_type;
6031 	}
6032 
6033 	pos = os_strstr(cmd, "delay=");
6034 	if (pos) {
6035 		pos += 6;
6036 		search_delay = atoi(pos);
6037 	} else
6038 		search_delay = wpas_p2p_search_delay(wpa_s);
6039 
6040 	pos = os_strstr(cmd, "freq=");
6041 	if (pos) {
6042 		pos += 5;
6043 		freq = atoi(pos);
6044 		if (freq <= 0)
6045 			return -1;
6046 	}
6047 
6048 	/* Must be searched for last, because it adds nul termination */
6049 	pos = os_strstr(cmd, " seek=");
6050 	if (pos)
6051 		pos += 6;
6052 	while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
6053 		char *term;
6054 
6055 		_seek[seek_count++] = pos;
6056 		seek = _seek;
6057 		term = os_strchr(pos, ' ');
6058 		if (!term)
6059 			break;
6060 		*term = '\0';
6061 		pos = os_strstr(term + 1, "seek=");
6062 		if (pos)
6063 			pos += 5;
6064 	}
6065 	if (seek_count > P2P_MAX_QUERY_HASH) {
6066 		seek[0] = NULL;
6067 		seek_count = 1;
6068 	}
6069 
6070 	return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
6071 			     _dev_id, search_delay, seek_count, seek, freq,
6072 			     include_6ghz);
6073 }
6074 
6075 
p2ps_ctrl_parse_cpt_priority(const char * pos,u8 * cpt)6076 static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
6077 {
6078 	const char *last = NULL;
6079 	const char *token;
6080 	long int token_len;
6081 	unsigned int i;
6082 
6083 	/* Expected predefined CPT names delimited by ':' */
6084 	for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
6085 		if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
6086 			wpa_printf(MSG_ERROR,
6087 				   "P2PS: CPT name list is too long, expected up to %d names",
6088 				   P2PS_FEATURE_CAPAB_CPT_MAX);
6089 			cpt[0] = 0;
6090 			return -1;
6091 		}
6092 
6093 		token_len = last - token;
6094 
6095 		if (token_len  == 3 &&
6096 		    os_memcmp(token, "UDP", token_len) == 0) {
6097 			cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
6098 		} else if (token_len == 3 &&
6099 			   os_memcmp(token, "MAC", token_len) == 0) {
6100 			cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
6101 		} else {
6102 			wpa_printf(MSG_ERROR,
6103 				   "P2PS: Unsupported CPT name '%s'", token);
6104 			cpt[0] = 0;
6105 			return -1;
6106 		}
6107 
6108 		if (isblank((unsigned char) *last)) {
6109 			i++;
6110 			break;
6111 		}
6112 	}
6113 	cpt[i] = 0;
6114 	return 0;
6115 }
6116 
6117 
p2p_parse_asp_provision_cmd(const char * cmd)6118 static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
6119 {
6120 	struct p2ps_provision *p2ps_prov;
6121 	char *pos;
6122 	size_t info_len = 0;
6123 	char *info = NULL;
6124 	u8 role = P2PS_SETUP_NONE;
6125 	long long unsigned val;
6126 	int i;
6127 
6128 	pos = os_strstr(cmd, "info=");
6129 	if (pos) {
6130 		pos += 5;
6131 		info_len = os_strlen(pos);
6132 
6133 		if (info_len) {
6134 			info = os_malloc(info_len + 1);
6135 			if (info) {
6136 				info_len = utf8_unescape(pos, info_len,
6137 							 info, info_len + 1);
6138 			} else
6139 				info_len = 0;
6140 		}
6141 	}
6142 
6143 	p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
6144 	if (p2ps_prov == NULL) {
6145 		os_free(info);
6146 		return NULL;
6147 	}
6148 
6149 	if (info) {
6150 		os_memcpy(p2ps_prov->info, info, info_len);
6151 		p2ps_prov->info[info_len] = '\0';
6152 		os_free(info);
6153 	}
6154 
6155 	pos = os_strstr(cmd, "status=");
6156 	if (pos)
6157 		p2ps_prov->status = atoi(pos + 7);
6158 	else
6159 		p2ps_prov->status = -1;
6160 
6161 	pos = os_strstr(cmd, "adv_id=");
6162 	if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
6163 		goto invalid_args;
6164 	p2ps_prov->adv_id = val;
6165 
6166 	pos = os_strstr(cmd, "method=");
6167 	if (pos)
6168 		p2ps_prov->method = strtol(pos + 7, NULL, 16);
6169 	else
6170 		p2ps_prov->method = 0;
6171 
6172 	pos = os_strstr(cmd, "session=");
6173 	if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
6174 		goto invalid_args;
6175 	p2ps_prov->session_id = val;
6176 
6177 	pos = os_strstr(cmd, "adv_mac=");
6178 	if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
6179 		goto invalid_args;
6180 
6181 	pos = os_strstr(cmd, "session_mac=");
6182 	if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
6183 		goto invalid_args;
6184 
6185 	pos = os_strstr(cmd, "cpt=");
6186 	if (pos) {
6187 		if (p2ps_ctrl_parse_cpt_priority(pos + 4,
6188 						 p2ps_prov->cpt_priority))
6189 			goto invalid_args;
6190 	} else {
6191 		p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
6192 	}
6193 
6194 	for (i = 0; p2ps_prov->cpt_priority[i]; i++)
6195 		p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
6196 
6197 	/* force conncap with tstCap (no validity checks) */
6198 	pos = os_strstr(cmd, "tstCap=");
6199 	if (pos) {
6200 		role = strtol(pos + 7, NULL, 16);
6201 	} else {
6202 		pos = os_strstr(cmd, "role=");
6203 		if (pos) {
6204 			role = strtol(pos + 5, NULL, 16);
6205 			if (role != P2PS_SETUP_CLIENT &&
6206 			    role != P2PS_SETUP_GROUP_OWNER)
6207 				role = P2PS_SETUP_NONE;
6208 		}
6209 	}
6210 	p2ps_prov->role = role;
6211 
6212 	return p2ps_prov;
6213 
6214 invalid_args:
6215 	os_free(p2ps_prov);
6216 	return NULL;
6217 }
6218 
6219 
p2p_ctrl_asp_provision_resp(struct wpa_supplicant * wpa_s,char * cmd)6220 static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
6221 {
6222 	u8 addr[ETH_ALEN];
6223 	struct p2ps_provision *p2ps_prov;
6224 	char *pos;
6225 
6226 	/* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
6227 
6228 	wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
6229 
6230 	if (hwaddr_aton(cmd, addr))
6231 		return -1;
6232 
6233 	pos = cmd + 17;
6234 	if (*pos != ' ')
6235 		return -1;
6236 
6237 	p2ps_prov = p2p_parse_asp_provision_cmd(pos);
6238 	if (!p2ps_prov)
6239 		return -1;
6240 
6241 	if (p2ps_prov->status < 0) {
6242 		os_free(p2ps_prov);
6243 		return -1;
6244 	}
6245 
6246 	return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
6247 				  p2ps_prov);
6248 }
6249 
6250 
p2p_ctrl_asp_provision(struct wpa_supplicant * wpa_s,char * cmd)6251 static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
6252 {
6253 	u8 addr[ETH_ALEN];
6254 	struct p2ps_provision *p2ps_prov;
6255 	char *pos;
6256 
6257 	/* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
6258 	 *        session=<ses_id> mac=<ses_mac> [info=<infodata>]
6259 	 */
6260 
6261 	wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
6262 	if (hwaddr_aton(cmd, addr))
6263 		return -1;
6264 
6265 	pos = cmd + 17;
6266 	if (*pos != ' ')
6267 		return -1;
6268 
6269 	p2ps_prov = p2p_parse_asp_provision_cmd(pos);
6270 	if (!p2ps_prov)
6271 		return -1;
6272 
6273 	p2ps_prov->pd_seeker = 1;
6274 
6275 	return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
6276 				  p2ps_prov);
6277 }
6278 
6279 
parse_freq(int chwidth,int freq2)6280 static int parse_freq(int chwidth, int freq2)
6281 {
6282 	if (freq2 < 0)
6283 		return -1;
6284 	if (freq2)
6285 		return CONF_OPER_CHWIDTH_80P80MHZ;
6286 
6287 	switch (chwidth) {
6288 	case 0:
6289 	case 20:
6290 	case 40:
6291 		return CONF_OPER_CHWIDTH_USE_HT;
6292 	case 80:
6293 		return CONF_OPER_CHWIDTH_80MHZ;
6294 	case 160:
6295 		return CONF_OPER_CHWIDTH_160MHZ;
6296 	case 320:
6297 		return CONF_OPER_CHWIDTH_320MHZ;
6298 	default:
6299 		wpa_printf(MSG_DEBUG, "Unknown max oper bandwidth: %d",
6300 			   chwidth);
6301 		return -1;
6302 	}
6303 }
6304 
6305 
p2p_ctrl_connect(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)6306 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
6307 			    char *buf, size_t buflen)
6308 {
6309 	u8 addr[ETH_ALEN];
6310 	char *pos, *pos2;
6311 	char *pin = NULL;
6312 	enum p2p_wps_method wps_method;
6313 	int new_pin;
6314 	int ret;
6315 	int persistent_group, persistent_id = -1;
6316 	int join;
6317 	int auth;
6318 	int automatic;
6319 	int go_intent = -1;
6320 	int freq = 0;
6321 	int pd;
6322 	int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
6323 	int edmg;
6324 	u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
6325 	size_t group_ssid_len = 0;
6326 	int he;
6327 	bool allow_6ghz;
6328 
6329 	if (!wpa_s->global->p2p_init_wpa_s)
6330 		return -1;
6331 	if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
6332 		wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
6333 			wpa_s->global->p2p_init_wpa_s->ifname);
6334 		wpa_s = wpa_s->global->p2p_init_wpa_s;
6335 	}
6336 
6337 	/* <addr> <"pbc" | "pin" | PIN> [label|display|keypad|p2ps]
6338 	 * [persistent|persistent=<network id>]
6339 	 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
6340 	 * [ht40] [vht] [he] [edmg] [auto] [ssid=<hexdump>] */
6341 
6342 	if (hwaddr_aton(cmd, addr))
6343 		return -1;
6344 
6345 	pos = cmd + 17;
6346 	if (*pos != ' ')
6347 		return -1;
6348 	pos++;
6349 
6350 	persistent_group = os_strstr(pos, " persistent") != NULL;
6351 	pos2 = os_strstr(pos, " persistent=");
6352 	if (pos2) {
6353 		struct wpa_ssid *ssid;
6354 		persistent_id = atoi(pos2 + 12);
6355 		ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6356 		if (ssid == NULL || ssid->disabled != 2 ||
6357 		    ssid->mode != WPAS_MODE_P2P_GO) {
6358 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
6359 				   "SSID id=%d for persistent P2P group (GO)",
6360 				   persistent_id);
6361 			return -1;
6362 		}
6363 	}
6364 	join = os_strstr(pos, " join") != NULL;
6365 	allow_6ghz = os_strstr(pos, " allow_6ghz") != NULL;
6366 	auth = os_strstr(pos, " auth") != NULL;
6367 	automatic = os_strstr(pos, " auto") != NULL;
6368 	pd = os_strstr(pos, " provdisc") != NULL;
6369 	vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
6370 	ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
6371 		vht;
6372 	he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
6373 	edmg = (os_strstr(cmd, " edmg") != NULL) || wpa_s->conf->p2p_go_edmg;
6374 
6375 	pos2 = os_strstr(pos, " go_intent=");
6376 	if (pos2) {
6377 		pos2 += 11;
6378 		go_intent = atoi(pos2);
6379 		if (go_intent < 0 || go_intent > 15)
6380 			return -1;
6381 	}
6382 
6383 	pos2 = os_strstr(pos, " freq=");
6384 	if (pos2) {
6385 		pos2 += 6;
6386 		freq = atoi(pos2);
6387 		if (freq <= 0)
6388 			return -1;
6389 	}
6390 
6391 	pos2 = os_strstr(pos, " freq2=");
6392 	if (pos2)
6393 		freq2 = atoi(pos2 + 7);
6394 
6395 	pos2 = os_strstr(pos, " max_oper_chwidth=");
6396 	if (pos2)
6397 		chwidth = atoi(pos2 + 18);
6398 
6399 	max_oper_chwidth = parse_freq(chwidth, freq2);
6400 	if (max_oper_chwidth < 0)
6401 		return -1;
6402 
6403 	if (allow_6ghz && chwidth == 40)
6404 		max_oper_chwidth = CONF_OPER_CHWIDTH_40MHZ_6GHZ;
6405 
6406 	pos2 = os_strstr(pos, " ssid=");
6407 	if (pos2) {
6408 		char *end;
6409 
6410 		pos2 += 6;
6411 		end = os_strchr(pos2, ' ');
6412 		if (!end)
6413 			group_ssid_len = os_strlen(pos2) / 2;
6414 		else
6415 			group_ssid_len = (end - pos2) / 2;
6416 		if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
6417 		    hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
6418 			return -1;
6419 		group_ssid = _group_ssid;
6420 	}
6421 
6422 	if (os_strncmp(pos, "pin", 3) == 0) {
6423 		/* Request random PIN (to be displayed) and enable the PIN */
6424 		wps_method = WPS_PIN_DISPLAY;
6425 	} else if (os_strncmp(pos, "pbc", 3) == 0) {
6426 		wps_method = WPS_PBC;
6427 	} else if (os_strstr(pos, "p2ps") != NULL) {
6428 		wps_method = WPS_P2PS;
6429 	} else {
6430 		pin = pos;
6431 		pos = os_strchr(pin, ' ');
6432 		wps_method = WPS_PIN_KEYPAD;
6433 		if (pos) {
6434 			*pos++ = '\0';
6435 			if (os_strncmp(pos, "display", 7) == 0)
6436 				wps_method = WPS_PIN_DISPLAY;
6437 		}
6438 		if (!wps_pin_str_valid(pin)) {
6439 			os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
6440 			return 17;
6441 		}
6442 	}
6443 
6444 	new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
6445 				   persistent_group, automatic, join,
6446 				   auth, go_intent, freq, freq2, persistent_id,
6447 				   pd, ht40, vht, max_oper_chwidth, he, edmg,
6448 				   group_ssid, group_ssid_len, allow_6ghz);
6449 	if (new_pin == -2) {
6450 		os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
6451 		return 25;
6452 	}
6453 	if (new_pin == -3) {
6454 		os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
6455 		return 25;
6456 	}
6457 	if (new_pin < 0)
6458 		return -1;
6459 	if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
6460 		ret = os_snprintf(buf, buflen, "%08d", new_pin);
6461 		if (os_snprintf_error(buflen, ret))
6462 			return -1;
6463 		return ret;
6464 	}
6465 
6466 	os_memcpy(buf, "OK\n", 3);
6467 	return 3;
6468 }
6469 
6470 
p2p_ctrl_listen(struct wpa_supplicant * wpa_s,char * cmd)6471 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
6472 {
6473 	unsigned int timeout = atoi(cmd);
6474 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6475 		wpa_dbg(wpa_s, MSG_INFO,
6476 			"Reject P2P_LISTEN since interface is disabled");
6477 		return -1;
6478 	}
6479 	return wpas_p2p_listen(wpa_s, timeout);
6480 }
6481 
6482 
p2p_ctrl_prov_disc(struct wpa_supplicant * wpa_s,char * cmd)6483 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
6484 {
6485 	u8 addr[ETH_ALEN];
6486 	char *pos;
6487 	enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
6488 
6489 	/* <addr> <config method> [join|auto] */
6490 
6491 	if (hwaddr_aton(cmd, addr))
6492 		return -1;
6493 
6494 	pos = cmd + 17;
6495 	if (*pos != ' ')
6496 		return -1;
6497 	pos++;
6498 
6499 	if (os_strstr(pos, " join") != NULL)
6500 		use = WPAS_P2P_PD_FOR_JOIN;
6501 	else if (os_strstr(pos, " auto") != NULL)
6502 		use = WPAS_P2P_PD_AUTO;
6503 
6504 	return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
6505 }
6506 
6507 
p2p_get_passphrase(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)6508 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
6509 			      size_t buflen)
6510 {
6511 	struct wpa_ssid *ssid = wpa_s->current_ssid;
6512 
6513 	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
6514 	    ssid->passphrase == NULL)
6515 		return -1;
6516 
6517 	os_strlcpy(buf, ssid->passphrase, buflen);
6518 	return os_strlen(buf);
6519 }
6520 
6521 
p2p_ctrl_serv_disc_req(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)6522 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
6523 				  char *buf, size_t buflen)
6524 {
6525 	u64 ref;
6526 	int res;
6527 	u8 dst_buf[ETH_ALEN], *dst;
6528 	struct wpabuf *tlvs;
6529 	char *pos;
6530 	size_t len;
6531 
6532 	if (hwaddr_aton(cmd, dst_buf))
6533 		return -1;
6534 	dst = dst_buf;
6535 	if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
6536 	    dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
6537 		dst = NULL;
6538 	pos = cmd + 17;
6539 	if (*pos != ' ')
6540 		return -1;
6541 	pos++;
6542 
6543 	if (os_strncmp(pos, "upnp ", 5) == 0) {
6544 		u8 version;
6545 		pos += 5;
6546 		if (hexstr2bin(pos, &version, 1) < 0)
6547 			return -1;
6548 		pos += 2;
6549 		if (*pos != ' ')
6550 			return -1;
6551 		pos++;
6552 		ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
6553 #ifdef CONFIG_WIFI_DISPLAY
6554 	} else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
6555 		ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
6556 #endif /* CONFIG_WIFI_DISPLAY */
6557 	} else if (os_strncmp(pos, "asp ", 4) == 0) {
6558 		char *svc_str;
6559 		char *svc_info = NULL;
6560 		u32 id;
6561 
6562 		pos += 4;
6563 		if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
6564 			return -1;
6565 
6566 		pos = os_strchr(pos, ' ');
6567 		if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
6568 			return -1;
6569 
6570 		svc_str = pos + 1;
6571 
6572 		pos = os_strchr(svc_str, ' ');
6573 
6574 		if (pos)
6575 			*pos++ = '\0';
6576 
6577 		/* All remaining data is the svc_info string */
6578 		if (pos && pos[0] && pos[0] != ' ') {
6579 			len = os_strlen(pos);
6580 
6581 			/* Unescape in place */
6582 			len = utf8_unescape(pos, len, pos, len);
6583 			if (len > 0xff)
6584 				return -1;
6585 
6586 			svc_info = pos;
6587 		}
6588 
6589 		ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
6590 					      svc_str, svc_info);
6591 	} else {
6592 		len = os_strlen(pos);
6593 		if (len & 1)
6594 			return -1;
6595 		len /= 2;
6596 		tlvs = wpabuf_alloc(len);
6597 		if (tlvs == NULL)
6598 			return -1;
6599 		if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
6600 			wpabuf_free(tlvs);
6601 			return -1;
6602 		}
6603 
6604 		ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
6605 		wpabuf_free(tlvs);
6606 	}
6607 	if (ref == 0)
6608 		return -1;
6609 	res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
6610 	if (os_snprintf_error(buflen, res))
6611 		return -1;
6612 	return res;
6613 }
6614 
6615 
p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant * wpa_s,char * cmd)6616 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
6617 					 char *cmd)
6618 {
6619 	long long unsigned val;
6620 	u64 req;
6621 	if (sscanf(cmd, "%llx", &val) != 1)
6622 		return -1;
6623 	req = val;
6624 	return wpas_p2p_sd_cancel_request(wpa_s, req);
6625 }
6626 
6627 
p2p_ctrl_serv_disc_resp(struct wpa_supplicant * wpa_s,char * cmd)6628 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
6629 {
6630 	int freq;
6631 	u8 dst[ETH_ALEN];
6632 	u8 dialog_token;
6633 	struct wpabuf *resp_tlvs;
6634 	char *pos, *pos2;
6635 	size_t len;
6636 
6637 	pos = os_strchr(cmd, ' ');
6638 	if (pos == NULL)
6639 		return -1;
6640 	*pos++ = '\0';
6641 	freq = atoi(cmd);
6642 	if (freq == 0)
6643 		return -1;
6644 
6645 	if (hwaddr_aton(pos, dst))
6646 		return -1;
6647 	pos += 17;
6648 	if (*pos != ' ')
6649 		return -1;
6650 	pos++;
6651 
6652 	pos2 = os_strchr(pos, ' ');
6653 	if (pos2 == NULL)
6654 		return -1;
6655 	*pos2++ = '\0';
6656 	dialog_token = atoi(pos);
6657 
6658 	len = os_strlen(pos2);
6659 	if (len & 1)
6660 		return -1;
6661 	len /= 2;
6662 	resp_tlvs = wpabuf_alloc(len);
6663 	if (resp_tlvs == NULL)
6664 		return -1;
6665 	if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
6666 		wpabuf_free(resp_tlvs);
6667 		return -1;
6668 	}
6669 
6670 	wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
6671 	wpabuf_free(resp_tlvs);
6672 	return 0;
6673 }
6674 
6675 
p2p_ctrl_serv_disc_external(struct wpa_supplicant * wpa_s,char * cmd)6676 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
6677 				       char *cmd)
6678 {
6679 	if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
6680 		return -1;
6681 	wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
6682 	return 0;
6683 }
6684 
6685 
p2p_ctrl_service_add_bonjour(struct wpa_supplicant * wpa_s,char * cmd)6686 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
6687 					char *cmd)
6688 {
6689 	char *pos;
6690 	size_t len;
6691 	struct wpabuf *query, *resp;
6692 
6693 	pos = os_strchr(cmd, ' ');
6694 	if (pos == NULL)
6695 		return -1;
6696 	*pos++ = '\0';
6697 
6698 	len = os_strlen(cmd);
6699 	if (len & 1)
6700 		return -1;
6701 	len /= 2;
6702 	query = wpabuf_alloc(len);
6703 	if (query == NULL)
6704 		return -1;
6705 	if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
6706 		wpabuf_free(query);
6707 		return -1;
6708 	}
6709 
6710 	len = os_strlen(pos);
6711 	if (len & 1) {
6712 		wpabuf_free(query);
6713 		return -1;
6714 	}
6715 	len /= 2;
6716 	resp = wpabuf_alloc(len);
6717 	if (resp == NULL) {
6718 		wpabuf_free(query);
6719 		return -1;
6720 	}
6721 	if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
6722 		wpabuf_free(query);
6723 		wpabuf_free(resp);
6724 		return -1;
6725 	}
6726 
6727 	if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
6728 		wpabuf_free(query);
6729 		wpabuf_free(resp);
6730 		return -1;
6731 	}
6732 	return 0;
6733 }
6734 
6735 
p2p_ctrl_service_add_upnp(struct wpa_supplicant * wpa_s,char * cmd)6736 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
6737 {
6738 	char *pos;
6739 	u8 version;
6740 
6741 	pos = os_strchr(cmd, ' ');
6742 	if (pos == NULL)
6743 		return -1;
6744 	*pos++ = '\0';
6745 
6746 	if (hexstr2bin(cmd, &version, 1) < 0)
6747 		return -1;
6748 
6749 	return wpas_p2p_service_add_upnp(wpa_s, version, pos);
6750 }
6751 
6752 
p2p_ctrl_service_add_asp(struct wpa_supplicant * wpa_s,u8 replace,char * cmd)6753 static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
6754 				    u8 replace, char *cmd)
6755 {
6756 	char *pos;
6757 	char *adv_str;
6758 	u32 auto_accept, adv_id, svc_state, config_methods;
6759 	char *svc_info = NULL;
6760 	char *cpt_prio_str;
6761 	u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
6762 
6763 	pos = os_strchr(cmd, ' ');
6764 	if (pos == NULL)
6765 		return -1;
6766 	*pos++ = '\0';
6767 
6768 	/* Auto-Accept value is mandatory, and must be one of the
6769 	 * single values (0, 1, 2, 4) */
6770 	auto_accept = atoi(cmd);
6771 	switch (auto_accept) {
6772 	case P2PS_SETUP_NONE: /* No auto-accept */
6773 	case P2PS_SETUP_NEW:
6774 	case P2PS_SETUP_CLIENT:
6775 	case P2PS_SETUP_GROUP_OWNER:
6776 		break;
6777 	default:
6778 		return -1;
6779 	}
6780 
6781 	/* Advertisement ID is mandatory */
6782 	cmd = pos;
6783 	pos = os_strchr(cmd, ' ');
6784 	if (pos == NULL)
6785 		return -1;
6786 	*pos++ = '\0';
6787 
6788 	/* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
6789 	if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
6790 		return -1;
6791 
6792 	/* Only allow replacements if exist, and adds if not */
6793 	if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
6794 		if (!replace)
6795 			return -1;
6796 	} else {
6797 		if (replace)
6798 			return -1;
6799 	}
6800 
6801 	/* svc_state between 0 - 0xff is mandatory */
6802 	if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
6803 		return -1;
6804 
6805 	pos = os_strchr(pos, ' ');
6806 	if (pos == NULL)
6807 		return -1;
6808 
6809 	/* config_methods is mandatory */
6810 	pos++;
6811 	if (sscanf(pos, "%x", &config_methods) != 1)
6812 		return -1;
6813 
6814 	if (!(config_methods &
6815 	      (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
6816 		return -1;
6817 
6818 	pos = os_strchr(pos, ' ');
6819 	if (pos == NULL)
6820 		return -1;
6821 
6822 	pos++;
6823 	adv_str = pos;
6824 
6825 	/* Advertisement string is mandatory */
6826 	if (!pos[0] || pos[0] == ' ')
6827 		return -1;
6828 
6829 	/* Terminate svc string */
6830 	pos = os_strchr(pos, ' ');
6831 	if (pos != NULL)
6832 		*pos++ = '\0';
6833 
6834 	cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
6835 	if (cpt_prio_str) {
6836 		pos = os_strchr(pos, ' ');
6837 		if (pos != NULL)
6838 			*pos++ = '\0';
6839 
6840 		if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
6841 			return -1;
6842 	} else {
6843 		cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
6844 		cpt_prio[1] = 0;
6845 	}
6846 
6847 	/* Service and Response Information are optional */
6848 	if (pos && pos[0]) {
6849 		size_t len;
6850 
6851 		/* Note the bare ' included, which cannot exist legally
6852 		 * in unescaped string. */
6853 		svc_info = os_strstr(pos, "svc_info='");
6854 
6855 		if (svc_info) {
6856 			svc_info += 9;
6857 			len = os_strlen(svc_info);
6858 			utf8_unescape(svc_info, len, svc_info, len);
6859 		}
6860 	}
6861 
6862 	return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
6863 					(u8) svc_state, (u16) config_methods,
6864 					svc_info, cpt_prio);
6865 }
6866 
6867 
p2p_ctrl_service_add(struct wpa_supplicant * wpa_s,char * cmd)6868 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
6869 {
6870 	char *pos;
6871 
6872 	pos = os_strchr(cmd, ' ');
6873 	if (pos == NULL)
6874 		return -1;
6875 	*pos++ = '\0';
6876 
6877 	if (os_strcmp(cmd, "bonjour") == 0)
6878 		return p2p_ctrl_service_add_bonjour(wpa_s, pos);
6879 	if (os_strcmp(cmd, "upnp") == 0)
6880 		return p2p_ctrl_service_add_upnp(wpa_s, pos);
6881 	if (os_strcmp(cmd, "asp") == 0)
6882 		return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
6883 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6884 	return -1;
6885 }
6886 
6887 
p2p_ctrl_service_del_bonjour(struct wpa_supplicant * wpa_s,char * cmd)6888 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
6889 					char *cmd)
6890 {
6891 	size_t len;
6892 	struct wpabuf *query;
6893 	int ret;
6894 
6895 	len = os_strlen(cmd);
6896 	if (len & 1)
6897 		return -1;
6898 	len /= 2;
6899 	query = wpabuf_alloc(len);
6900 	if (query == NULL)
6901 		return -1;
6902 	if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
6903 		wpabuf_free(query);
6904 		return -1;
6905 	}
6906 
6907 	ret = wpas_p2p_service_del_bonjour(wpa_s, query);
6908 	wpabuf_free(query);
6909 	return ret;
6910 }
6911 
6912 
p2p_ctrl_service_del_upnp(struct wpa_supplicant * wpa_s,char * cmd)6913 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
6914 {
6915 	char *pos;
6916 	u8 version;
6917 
6918 	pos = os_strchr(cmd, ' ');
6919 	if (pos == NULL)
6920 		return -1;
6921 	*pos++ = '\0';
6922 
6923 	if (hexstr2bin(cmd, &version, 1) < 0)
6924 		return -1;
6925 
6926 	return wpas_p2p_service_del_upnp(wpa_s, version, pos);
6927 }
6928 
6929 
p2p_ctrl_service_del_asp(struct wpa_supplicant * wpa_s,char * cmd)6930 static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
6931 {
6932 	u32 adv_id;
6933 
6934 	if (os_strcmp(cmd, "all") == 0) {
6935 		wpas_p2p_service_flush_asp(wpa_s);
6936 		return 0;
6937 	}
6938 
6939 	if (sscanf(cmd, "%x", &adv_id) != 1)
6940 		return -1;
6941 
6942 	return wpas_p2p_service_del_asp(wpa_s, adv_id);
6943 }
6944 
6945 
p2p_ctrl_service_del(struct wpa_supplicant * wpa_s,char * cmd)6946 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
6947 {
6948 	char *pos;
6949 
6950 	pos = os_strchr(cmd, ' ');
6951 	if (pos == NULL)
6952 		return -1;
6953 	*pos++ = '\0';
6954 
6955 	if (os_strcmp(cmd, "bonjour") == 0)
6956 		return p2p_ctrl_service_del_bonjour(wpa_s, pos);
6957 	if (os_strcmp(cmd, "upnp") == 0)
6958 		return p2p_ctrl_service_del_upnp(wpa_s, pos);
6959 	if (os_strcmp(cmd, "asp") == 0)
6960 		return p2p_ctrl_service_del_asp(wpa_s, pos);
6961 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6962 	return -1;
6963 }
6964 
6965 
p2p_ctrl_service_replace(struct wpa_supplicant * wpa_s,char * cmd)6966 static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
6967 {
6968 	char *pos;
6969 
6970 	pos = os_strchr(cmd, ' ');
6971 	if (pos == NULL)
6972 		return -1;
6973 	*pos++ = '\0';
6974 
6975 	if (os_strcmp(cmd, "asp") == 0)
6976 		return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
6977 
6978 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6979 	return -1;
6980 }
6981 
6982 
p2p_ctrl_reject(struct wpa_supplicant * wpa_s,char * cmd)6983 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
6984 {
6985 	u8 addr[ETH_ALEN];
6986 
6987 	/* <addr> */
6988 
6989 	if (hwaddr_aton(cmd, addr))
6990 		return -1;
6991 
6992 	return wpas_p2p_reject(wpa_s, addr);
6993 }
6994 
6995 
p2p_ctrl_invite_persistent(struct wpa_supplicant * wpa_s,char * cmd)6996 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
6997 {
6998 	char *pos;
6999 	int id;
7000 	struct wpa_ssid *ssid;
7001 	u8 *_peer = NULL, peer[ETH_ALEN];
7002 	int freq = 0, pref_freq = 0;
7003 	int ht40, vht, he, max_oper_chwidth, chwidth = 0, freq2 = 0;
7004 	int edmg;
7005 	bool allow_6ghz;
7006 
7007 	id = atoi(cmd);
7008 	pos = os_strstr(cmd, " peer=");
7009 	if (pos) {
7010 		pos += 6;
7011 		if (hwaddr_aton(pos, peer))
7012 			return -1;
7013 		_peer = peer;
7014 	}
7015 	ssid = wpa_config_get_network(wpa_s->conf, id);
7016 	if (ssid == NULL || ssid->disabled != 2) {
7017 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
7018 			   "for persistent P2P group",
7019 			   id);
7020 		return -1;
7021 	}
7022 
7023 	pos = os_strstr(cmd, " freq=");
7024 	if (pos) {
7025 		pos += 6;
7026 		freq = atoi(pos);
7027 		if (freq <= 0)
7028 			return -1;
7029 	}
7030 
7031 	pos = os_strstr(cmd, " pref=");
7032 	if (pos) {
7033 		pos += 6;
7034 		pref_freq = atoi(pos);
7035 		if (pref_freq <= 0)
7036 			return -1;
7037 	}
7038 
7039 	vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
7040 	ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
7041 		vht;
7042 	he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
7043 	edmg = (os_strstr(cmd, " edmg") != NULL) || wpa_s->conf->p2p_go_edmg;
7044 
7045 	pos = os_strstr(cmd, "freq2=");
7046 	if (pos)
7047 		freq2 = atoi(pos + 6);
7048 
7049 	pos = os_strstr(cmd, " max_oper_chwidth=");
7050 	if (pos)
7051 		chwidth = atoi(pos + 18);
7052 
7053 	max_oper_chwidth = parse_freq(chwidth, freq2);
7054 	if (max_oper_chwidth < 0)
7055 		return -1;
7056 
7057 	allow_6ghz = os_strstr(cmd, " allow_6ghz") != NULL;
7058 
7059 	if (allow_6ghz && chwidth == 40)
7060 		max_oper_chwidth = CONF_OPER_CHWIDTH_40MHZ_6GHZ;
7061 
7062 	return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
7063 			       max_oper_chwidth, pref_freq, he, edmg,
7064 			       allow_6ghz);
7065 }
7066 
7067 
p2p_ctrl_invite_group(struct wpa_supplicant * wpa_s,char * cmd)7068 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
7069 {
7070 	char *pos;
7071 	u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
7072 	bool allow_6ghz;
7073 
7074 	pos = os_strstr(cmd, " peer=");
7075 	if (!pos)
7076 		return -1;
7077 
7078 	*pos = '\0';
7079 	pos += 6;
7080 	if (hwaddr_aton(pos, peer)) {
7081 		wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
7082 		return -1;
7083 	}
7084 
7085 	allow_6ghz = os_strstr(pos, " allow_6ghz") != NULL;
7086 
7087 	pos = os_strstr(pos, " go_dev_addr=");
7088 	if (pos) {
7089 		pos += 13;
7090 		if (hwaddr_aton(pos, go_dev_addr)) {
7091 			wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
7092 				   pos);
7093 			return -1;
7094 		}
7095 		go_dev = go_dev_addr;
7096 	}
7097 
7098 	return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev, allow_6ghz);
7099 }
7100 
7101 
p2p_ctrl_invite(struct wpa_supplicant * wpa_s,char * cmd)7102 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
7103 {
7104 	if (os_strncmp(cmd, "persistent=", 11) == 0)
7105 		return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
7106 	if (os_strncmp(cmd, "group=", 6) == 0)
7107 		return p2p_ctrl_invite_group(wpa_s, cmd + 6);
7108 
7109 	return -1;
7110 }
7111 
7112 
p2p_ctrl_group_add_persistent(struct wpa_supplicant * wpa_s,int id,int freq,int vht_center_freq2,int ht40,int vht,int vht_chwidth,int he,int edmg,bool allow_6ghz,const u8 * go_bssid)7113 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
7114 					 int id, int freq, int vht_center_freq2,
7115 					 int ht40, int vht, int vht_chwidth,
7116 					 int he, int edmg, bool allow_6ghz,
7117 					 const u8 *go_bssid)
7118 {
7119 	struct wpa_ssid *ssid;
7120 
7121 	ssid = wpa_config_get_network(wpa_s->conf, id);
7122 	if (ssid == NULL || ssid->disabled != 2) {
7123 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
7124 			   "for persistent P2P group",
7125 			   id);
7126 		return -1;
7127 	}
7128 
7129 	return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq,
7130 					     vht_center_freq2, 0, ht40, vht,
7131 					     vht_chwidth, he, edmg,
7132 					     NULL, 0, 0, allow_6ghz, 0,
7133 					     go_bssid);
7134 }
7135 
7136 
p2p_ctrl_group_add(struct wpa_supplicant * wpa_s,char * cmd)7137 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
7138 {
7139 	int freq = 0, persistent = 0, group_id = -1;
7140 	bool allow_6ghz = false;
7141 	int vht = wpa_s->conf->p2p_go_vht;
7142 	int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
7143 	int he = wpa_s->conf->p2p_go_he;
7144 	int edmg = wpa_s->conf->p2p_go_edmg;
7145 	int max_oper_chwidth, chwidth = 0, freq2 = 0;
7146 	char *token, *context = NULL;
7147 	u8 go_bssid_buf[ETH_ALEN], *go_bssid = NULL;
7148 #ifdef CONFIG_ACS
7149 	int acs = 0;
7150 #endif /* CONFIG_ACS */
7151 
7152 	while ((token = str_token(cmd, " ", &context))) {
7153 		if (sscanf(token, "freq2=%d", &freq2) == 1 ||
7154 		    sscanf(token, "persistent=%d", &group_id) == 1 ||
7155 		    sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1) {
7156 			continue;
7157 #ifdef CONFIG_ACS
7158 		} else if (os_strcmp(token, "freq=acs") == 0) {
7159 			acs = 1;
7160 #endif /* CONFIG_ACS */
7161 		} else if (sscanf(token, "freq=%d", &freq) == 1) {
7162 			continue;
7163 		} else if (os_strcmp(token, "ht40") == 0) {
7164 			ht40 = 1;
7165 		} else if (os_strcmp(token, "vht") == 0) {
7166 			vht = 1;
7167 			ht40 = 1;
7168 		} else if (os_strcmp(token, "he") == 0) {
7169 			he = 1;
7170 		} else if (os_strcmp(token, "edmg") == 0) {
7171 			edmg = 1;
7172 		} else if (os_strcmp(token, "persistent") == 0) {
7173 			persistent = 1;
7174 		} else if (os_strcmp(token, "allow_6ghz") == 0) {
7175 			allow_6ghz = true;
7176 		} else if (os_strncmp(token, "go_bssid=", 9) == 0) {
7177 			if (hwaddr_aton(token + 9, go_bssid_buf))
7178 				return -1;
7179 			go_bssid = go_bssid_buf;
7180 		} else {
7181 			wpa_printf(MSG_DEBUG,
7182 				   "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
7183 				   token);
7184 			return -1;
7185 		}
7186 	}
7187 
7188 #ifdef CONFIG_ACS
7189 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
7190 	    (acs || freq == 2 || freq == 5)) {
7191 		if (freq == 2 && wpa_s->best_24_freq <= 0) {
7192 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211G;
7193 			wpa_s->p2p_go_do_acs = 1;
7194 			freq = 0;
7195 		} else if (freq == 5 && wpa_s->best_5_freq <= 0) {
7196 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211A;
7197 			wpa_s->p2p_go_do_acs = 1;
7198 			freq = 0;
7199 		} else {
7200 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211ANY;
7201 			wpa_s->p2p_go_do_acs = 1;
7202 		}
7203 	} else {
7204 		wpa_s->p2p_go_do_acs = 0;
7205 	}
7206 #endif /* CONFIG_ACS */
7207 
7208 	max_oper_chwidth = parse_freq(chwidth, freq2);
7209 	if (max_oper_chwidth < 0)
7210 		return -1;
7211 
7212 	if (allow_6ghz && chwidth == 40)
7213 		max_oper_chwidth = CONF_OPER_CHWIDTH_40MHZ_6GHZ;
7214 
7215 	/* Allow DFS to be used for Autonomous GO */
7216 	wpa_s->p2p_go_allow_dfs = !!(wpa_s->drv_flags &
7217 				     WPA_DRIVER_FLAGS_DFS_OFFLOAD);
7218 
7219 	if (group_id >= 0)
7220 		return p2p_ctrl_group_add_persistent(wpa_s, group_id,
7221 						     freq, freq2, ht40, vht,
7222 						     max_oper_chwidth, he,
7223 						     edmg, allow_6ghz,
7224 						     go_bssid);
7225 
7226 	return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
7227 				  max_oper_chwidth, he, edmg, allow_6ghz);
7228 }
7229 
7230 
p2p_ctrl_group_member(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)7231 static int p2p_ctrl_group_member(struct wpa_supplicant *wpa_s, const char *cmd,
7232 				 char *buf, size_t buflen)
7233 {
7234 	u8 dev_addr[ETH_ALEN];
7235 	struct wpa_ssid *ssid;
7236 	int res;
7237 	const u8 *iaddr;
7238 
7239 	ssid = wpa_s->current_ssid;
7240 	if (!wpa_s->global->p2p || !ssid || ssid->mode != WPAS_MODE_P2P_GO ||
7241 	    hwaddr_aton(cmd, dev_addr))
7242 		return -1;
7243 
7244 	iaddr = p2p_group_get_client_interface_addr(wpa_s->p2p_group, dev_addr);
7245 	if (!iaddr)
7246 		return -1;
7247 	res = os_snprintf(buf, buflen, MACSTR, MAC2STR(iaddr));
7248 	if (os_snprintf_error(buflen, res))
7249 		return -1;
7250 	return res;
7251 }
7252 
7253 
wpas_find_p2p_dev_addr_bss(struct wpa_global * global,const u8 * p2p_dev_addr)7254 static int wpas_find_p2p_dev_addr_bss(struct wpa_global *global,
7255 				      const u8 *p2p_dev_addr)
7256 {
7257 	struct wpa_supplicant *wpa_s;
7258 
7259 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7260 		if (wpa_bss_get_p2p_dev_addr(wpa_s, p2p_dev_addr))
7261 			return 1;
7262 	}
7263 
7264 	return 0;
7265 }
7266 
7267 
p2p_ctrl_peer(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)7268 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
7269 			 char *buf, size_t buflen)
7270 {
7271 	u8 addr[ETH_ALEN], *addr_ptr, group_capab;
7272 	int next, res;
7273 	const struct p2p_peer_info *info;
7274 	char *pos, *end;
7275 	char devtype[WPS_DEV_TYPE_BUFSIZE];
7276 	struct wpa_ssid *ssid;
7277 	size_t i;
7278 
7279 	if (!wpa_s->global->p2p)
7280 		return -1;
7281 
7282 	if (os_strcmp(cmd, "FIRST") == 0) {
7283 		addr_ptr = NULL;
7284 		next = 0;
7285 	} else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
7286 		if (hwaddr_aton(cmd + 5, addr) < 0)
7287 			return -1;
7288 		addr_ptr = addr;
7289 		next = 1;
7290 	} else {
7291 		if (hwaddr_aton(cmd, addr) < 0)
7292 			return -1;
7293 		addr_ptr = addr;
7294 		next = 0;
7295 	}
7296 
7297 	info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
7298 	if (info == NULL)
7299 		return -1;
7300 	group_capab = info->group_capab;
7301 
7302 	if (group_capab &&
7303 	    !wpas_find_p2p_dev_addr_bss(wpa_s->global, info->p2p_device_addr)) {
7304 		wpa_printf(MSG_DEBUG,
7305 			   "P2P: Could not find any BSS with p2p_dev_addr "
7306 			   MACSTR ", hence override group_capab from 0x%x to 0",
7307 			   MAC2STR(info->p2p_device_addr), group_capab);
7308 		group_capab = 0;
7309 	}
7310 
7311 	pos = buf;
7312 	end = buf + buflen;
7313 
7314 	res = os_snprintf(pos, end - pos, MACSTR "\n"
7315 			  "pri_dev_type=%s\n"
7316 			  "device_name=%s\n"
7317 			  "manufacturer=%s\n"
7318 			  "model_name=%s\n"
7319 			  "model_number=%s\n"
7320 			  "serial_number=%s\n"
7321 			  "config_methods=0x%x\n"
7322 			  "dev_capab=0x%x\n"
7323 			  "group_capab=0x%x\n"
7324 			  "level=%d\n",
7325 			  MAC2STR(info->p2p_device_addr),
7326 			  wps_dev_type_bin2str(info->pri_dev_type,
7327 					       devtype, sizeof(devtype)),
7328 			  info->device_name,
7329 			  info->manufacturer,
7330 			  info->model_name,
7331 			  info->model_number,
7332 			  info->serial_number,
7333 			  info->config_methods,
7334 			  info->dev_capab,
7335 			  group_capab,
7336 			  info->level);
7337 	if (os_snprintf_error(end - pos, res))
7338 		return pos - buf;
7339 	pos += res;
7340 
7341 	for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
7342 	{
7343 		const u8 *t;
7344 		t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
7345 		res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
7346 				  wps_dev_type_bin2str(t, devtype,
7347 						       sizeof(devtype)));
7348 		if (os_snprintf_error(end - pos, res))
7349 			return pos - buf;
7350 		pos += res;
7351 	}
7352 
7353 	ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
7354 	if (ssid) {
7355 		res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
7356 		if (os_snprintf_error(end - pos, res))
7357 			return pos - buf;
7358 		pos += res;
7359 	}
7360 
7361 	res = p2p_get_peer_info_txt(info, pos, end - pos);
7362 	if (res < 0)
7363 		return pos - buf;
7364 	pos += res;
7365 
7366 	if (info->vendor_elems) {
7367 		res = os_snprintf(pos, end - pos, "vendor_elems=");
7368 		if (os_snprintf_error(end - pos, res))
7369 			return pos - buf;
7370 		pos += res;
7371 
7372 		pos += wpa_snprintf_hex(pos, end - pos,
7373 					wpabuf_head(info->vendor_elems),
7374 					wpabuf_len(info->vendor_elems));
7375 
7376 		res = os_snprintf(pos, end - pos, "\n");
7377 		if (os_snprintf_error(end - pos, res))
7378 			return pos - buf;
7379 		pos += res;
7380 	}
7381 
7382 	return pos - buf;
7383 }
7384 
7385 
p2p_ctrl_disallow_freq(struct wpa_supplicant * wpa_s,const char * param)7386 static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
7387 				  const char *param)
7388 {
7389 	unsigned int i;
7390 
7391 	if (wpa_s->global->p2p == NULL)
7392 		return -1;
7393 
7394 	if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
7395 		return -1;
7396 
7397 	for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
7398 		struct wpa_freq_range *freq;
7399 		freq = &wpa_s->global->p2p_disallow_freq.range[i];
7400 		wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
7401 			   freq->min, freq->max);
7402 	}
7403 
7404 	wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
7405 	return 0;
7406 }
7407 
7408 
p2p_ctrl_set(struct wpa_supplicant * wpa_s,char * cmd)7409 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
7410 {
7411 	char *param;
7412 
7413 	if (wpa_s->global->p2p == NULL)
7414 		return -1;
7415 
7416 	param = os_strchr(cmd, ' ');
7417 	if (param == NULL)
7418 		return -1;
7419 	*param++ = '\0';
7420 
7421 	if (os_strcmp(cmd, "discoverability") == 0) {
7422 		p2p_set_client_discoverability(wpa_s->global->p2p,
7423 					       atoi(param));
7424 		return 0;
7425 	}
7426 
7427 	if (os_strcmp(cmd, "managed") == 0) {
7428 		p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
7429 		return 0;
7430 	}
7431 
7432 	if (os_strcmp(cmd, "listen_channel") == 0) {
7433 		char *pos;
7434 		u8 channel, op_class;
7435 
7436 		channel = atoi(param);
7437 		pos = os_strchr(param, ' ');
7438 		op_class = pos ? atoi(pos) : 81;
7439 
7440 		return p2p_set_listen_channel(wpa_s->global->p2p, op_class,
7441 					      channel, 1);
7442 	}
7443 
7444 	if (os_strcmp(cmd, "ssid_postfix") == 0) {
7445 		return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
7446 					    os_strlen(param));
7447 	}
7448 
7449 	if (os_strcmp(cmd, "noa") == 0) {
7450 		char *pos;
7451 		int count, start, duration;
7452 		/* GO NoA parameters: count,start_offset(ms),duration(ms) */
7453 		count = atoi(param);
7454 		pos = os_strchr(param, ',');
7455 		if (pos == NULL)
7456 			return -1;
7457 		pos++;
7458 		start = atoi(pos);
7459 		pos = os_strchr(pos, ',');
7460 		if (pos == NULL)
7461 			return -1;
7462 		pos++;
7463 		duration = atoi(pos);
7464 		if (count < 0 || count > 255 || start < 0 || duration < 0)
7465 			return -1;
7466 		if (count == 0 && duration > 0)
7467 			return -1;
7468 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
7469 			   "start=%d duration=%d", count, start, duration);
7470 		return wpas_p2p_set_noa(wpa_s, count, start, duration);
7471 	}
7472 
7473 	if (os_strcmp(cmd, "ps") == 0)
7474 		return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
7475 
7476 	if (os_strcmp(cmd, "oppps") == 0)
7477 		return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
7478 
7479 	if (os_strcmp(cmd, "ctwindow") == 0)
7480 		return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
7481 
7482 	if (os_strcmp(cmd, "disabled") == 0) {
7483 		wpa_s->global->p2p_disabled = atoi(param);
7484 		wpa_printf(MSG_DEBUG, "P2P functionality %s",
7485 			   wpa_s->global->p2p_disabled ?
7486 			   "disabled" : "enabled");
7487 		if (wpa_s->global->p2p_disabled) {
7488 			wpas_p2p_stop_find(wpa_s);
7489 			os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7490 			p2p_flush(wpa_s->global->p2p);
7491 		}
7492 		return 0;
7493 	}
7494 
7495 	if (os_strcmp(cmd, "conc_pref") == 0) {
7496 		if (os_strcmp(param, "sta") == 0)
7497 			wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
7498 		else if (os_strcmp(param, "p2p") == 0)
7499 			wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
7500 		else {
7501 			wpa_printf(MSG_INFO, "Invalid conc_pref value");
7502 			return -1;
7503 		}
7504 		wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
7505 			   "%s", param);
7506 		return 0;
7507 	}
7508 
7509 	if (os_strcmp(cmd, "force_long_sd") == 0) {
7510 		wpa_s->force_long_sd = atoi(param);
7511 		return 0;
7512 	}
7513 
7514 	if (os_strcmp(cmd, "peer_filter") == 0) {
7515 		u8 addr[ETH_ALEN];
7516 		if (hwaddr_aton(param, addr))
7517 			return -1;
7518 		p2p_set_peer_filter(wpa_s->global->p2p, addr);
7519 		return 0;
7520 	}
7521 
7522 	if (os_strcmp(cmd, "cross_connect") == 0)
7523 		return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
7524 
7525 	if (os_strcmp(cmd, "go_apsd") == 0) {
7526 		if (os_strcmp(param, "disable") == 0)
7527 			wpa_s->set_ap_uapsd = 0;
7528 		else {
7529 			wpa_s->set_ap_uapsd = 1;
7530 			wpa_s->ap_uapsd = atoi(param);
7531 		}
7532 		return 0;
7533 	}
7534 
7535 	if (os_strcmp(cmd, "client_apsd") == 0) {
7536 		if (os_strcmp(param, "disable") == 0)
7537 			wpa_s->set_sta_uapsd = 0;
7538 		else {
7539 			int be, bk, vi, vo;
7540 			char *pos;
7541 			/* format: BE,BK,VI,VO;max SP Length */
7542 			be = atoi(param);
7543 			pos = os_strchr(param, ',');
7544 			if (pos == NULL)
7545 				return -1;
7546 			pos++;
7547 			bk = atoi(pos);
7548 			pos = os_strchr(pos, ',');
7549 			if (pos == NULL)
7550 				return -1;
7551 			pos++;
7552 			vi = atoi(pos);
7553 			pos = os_strchr(pos, ',');
7554 			if (pos == NULL)
7555 				return -1;
7556 			pos++;
7557 			vo = atoi(pos);
7558 			/* ignore max SP Length for now */
7559 
7560 			wpa_s->set_sta_uapsd = 1;
7561 			wpa_s->sta_uapsd = 0;
7562 			if (be)
7563 				wpa_s->sta_uapsd |= BIT(0);
7564 			if (bk)
7565 				wpa_s->sta_uapsd |= BIT(1);
7566 			if (vi)
7567 				wpa_s->sta_uapsd |= BIT(2);
7568 			if (vo)
7569 				wpa_s->sta_uapsd |= BIT(3);
7570 		}
7571 		return 0;
7572 	}
7573 
7574 	if (os_strcmp(cmd, "disallow_freq") == 0)
7575 		return p2p_ctrl_disallow_freq(wpa_s, param);
7576 
7577 	if (os_strcmp(cmd, "disc_int") == 0) {
7578 		int min_disc_int, max_disc_int, max_disc_tu;
7579 		char *pos;
7580 
7581 		pos = param;
7582 
7583 		min_disc_int = atoi(pos);
7584 		pos = os_strchr(pos, ' ');
7585 		if (pos == NULL)
7586 			return -1;
7587 		*pos++ = '\0';
7588 
7589 		max_disc_int = atoi(pos);
7590 		pos = os_strchr(pos, ' ');
7591 		if (pos == NULL)
7592 			return -1;
7593 		*pos++ = '\0';
7594 
7595 		max_disc_tu = atoi(pos);
7596 
7597 		return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
7598 					max_disc_int, max_disc_tu);
7599 	}
7600 
7601 	if (os_strcmp(cmd, "per_sta_psk") == 0) {
7602 		wpa_s->global->p2p_per_sta_psk = !!atoi(param);
7603 		return 0;
7604 	}
7605 
7606 #ifdef CONFIG_WPS_NFC
7607 	if (os_strcmp(cmd, "nfc_tag") == 0)
7608 		return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
7609 #endif /* CONFIG_WPS_NFC */
7610 
7611 	if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
7612 		wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
7613 		return 0;
7614 	}
7615 
7616 	if (os_strcmp(cmd, "override_pref_op_chan") == 0) {
7617 		int op_class, chan;
7618 
7619 		op_class = atoi(param);
7620 		param = os_strchr(param, ':');
7621 		if (!param)
7622 			return -1;
7623 		param++;
7624 		chan = atoi(param);
7625 		p2p_set_override_pref_op_chan(wpa_s->global->p2p, op_class,
7626 					      chan);
7627 		return 0;
7628 	}
7629 
7630 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
7631 		   cmd);
7632 
7633 	return -1;
7634 }
7635 
7636 
p2p_ctrl_flush(struct wpa_supplicant * wpa_s)7637 static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
7638 {
7639 	os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7640 	wpa_s->force_long_sd = 0;
7641 
7642 #ifdef CONFIG_TESTING_OPTIONS
7643 	os_free(wpa_s->get_pref_freq_list_override);
7644 	wpa_s->get_pref_freq_list_override = NULL;
7645 #endif /* CONFIG_TESTING_OPTIONS */
7646 
7647 	wpas_p2p_stop_find(wpa_s);
7648 	wpa_s->parent->p2ps_method_config_any = 0;
7649 	if (wpa_s->global->p2p)
7650 		p2p_flush(wpa_s->global->p2p);
7651 }
7652 
7653 
p2p_ctrl_presence_req(struct wpa_supplicant * wpa_s,char * cmd)7654 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
7655 {
7656 	char *pos, *pos2;
7657 	unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
7658 
7659 	if (cmd[0]) {
7660 		pos = os_strchr(cmd, ' ');
7661 		if (pos == NULL)
7662 			return -1;
7663 		*pos++ = '\0';
7664 		dur1 = atoi(cmd);
7665 
7666 		pos2 = os_strchr(pos, ' ');
7667 		if (pos2)
7668 			*pos2++ = '\0';
7669 		int1 = atoi(pos);
7670 	} else
7671 		pos2 = NULL;
7672 
7673 	if (pos2) {
7674 		pos = os_strchr(pos2, ' ');
7675 		if (pos == NULL)
7676 			return -1;
7677 		*pos++ = '\0';
7678 		dur2 = atoi(pos2);
7679 		int2 = atoi(pos);
7680 	}
7681 
7682 	return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
7683 }
7684 
7685 
p2p_ctrl_ext_listen(struct wpa_supplicant * wpa_s,char * cmd)7686 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
7687 {
7688 	char *pos;
7689 	unsigned int period = 0, interval = 0;
7690 
7691 	if (cmd[0]) {
7692 		pos = os_strchr(cmd, ' ');
7693 		if (pos == NULL)
7694 			return -1;
7695 		*pos++ = '\0';
7696 		period = atoi(cmd);
7697 		interval = atoi(pos);
7698 	}
7699 
7700 	return wpas_p2p_ext_listen(wpa_s, period, interval);
7701 }
7702 
7703 
p2p_ctrl_remove_client(struct wpa_supplicant * wpa_s,const char * cmd)7704 static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
7705 {
7706 	const char *pos;
7707 	u8 peer[ETH_ALEN];
7708 	int iface_addr = 0;
7709 
7710 	pos = cmd;
7711 	if (os_strncmp(pos, "iface=", 6) == 0) {
7712 		iface_addr = 1;
7713 		pos += 6;
7714 	}
7715 	if (hwaddr_aton(pos, peer))
7716 		return -1;
7717 
7718 	wpas_p2p_remove_client(wpa_s, peer, iface_addr);
7719 	return 0;
7720 }
7721 
7722 
p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant * wpa_s,char * cmd)7723 static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
7724 {
7725 	int freq = 0, period = 0, interval = 0, count = 0;
7726 
7727 	if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
7728 	{
7729 		wpa_printf(MSG_DEBUG,
7730 			   "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
7731 		return -1;
7732 	}
7733 
7734 	return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
7735 }
7736 
7737 #endif /* CONFIG_P2P */
7738 
7739 
freq_range_to_channel_list(struct wpa_supplicant * wpa_s,char * val)7740 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
7741 {
7742 	struct wpa_freq_range_list ranges;
7743 	int *freqs = NULL;
7744 	struct hostapd_hw_modes *mode;
7745 	u16 i;
7746 
7747 	if (wpa_s->hw.modes == NULL)
7748 		return NULL;
7749 
7750 	os_memset(&ranges, 0, sizeof(ranges));
7751 	if (freq_range_list_parse(&ranges, val) < 0)
7752 		return NULL;
7753 
7754 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
7755 		int j;
7756 
7757 		mode = &wpa_s->hw.modes[i];
7758 		for (j = 0; j < mode->num_channels; j++) {
7759 			unsigned int freq;
7760 
7761 			if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
7762 				continue;
7763 
7764 			freq = mode->channels[j].freq;
7765 			if (!freq_range_list_includes(&ranges, freq))
7766 				continue;
7767 
7768 			int_array_add_unique(&freqs, freq);
7769 		}
7770 	}
7771 
7772 	os_free(ranges.range);
7773 	return freqs;
7774 }
7775 
7776 
7777 #ifdef CONFIG_INTERWORKING
7778 
ctrl_interworking_select(struct wpa_supplicant * wpa_s,char * param)7779 static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
7780 {
7781 	int auto_sel = 0;
7782 	int *freqs = NULL;
7783 
7784 	if (param) {
7785 		char *pos;
7786 
7787 		auto_sel = os_strstr(param, "auto") != NULL;
7788 
7789 		pos = os_strstr(param, "freq=");
7790 		if (pos) {
7791 			freqs = freq_range_to_channel_list(wpa_s, pos + 5);
7792 			if (freqs == NULL)
7793 				return -1;
7794 		}
7795 
7796 	}
7797 
7798 	return interworking_select(wpa_s, auto_sel, freqs);
7799 }
7800 
7801 
ctrl_interworking_connect(struct wpa_supplicant * wpa_s,char * dst,int only_add)7802 static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
7803 				     int only_add)
7804 {
7805 	u8 bssid[ETH_ALEN];
7806 	struct wpa_bss *bss;
7807 
7808 	if (hwaddr_aton(dst, bssid)) {
7809 		wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
7810 		return -1;
7811 	}
7812 
7813 	bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
7814 	if (bss == NULL) {
7815 		wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
7816 			   MAC2STR(bssid));
7817 		return -1;
7818 	}
7819 
7820 	if (bss->ssid_len == 0) {
7821 		int found = 0;
7822 
7823 		wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
7824 			   " does not have SSID information", MAC2STR(bssid));
7825 
7826 		dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
7827 					 list) {
7828 			if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
7829 			    bss->ssid_len > 0) {
7830 				found = 1;
7831 				break;
7832 			}
7833 		}
7834 
7835 		if (!found)
7836 			return -1;
7837 		wpa_printf(MSG_DEBUG,
7838 			   "Found another matching BSS entry with SSID");
7839 	}
7840 
7841 	return interworking_connect(wpa_s, bss, only_add);
7842 }
7843 
7844 
get_anqp(struct wpa_supplicant * wpa_s,char * dst)7845 static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
7846 {
7847 	u8 dst_addr[ETH_ALEN];
7848 	int used, freq = 0;
7849 	char *pos;
7850 #define MAX_ANQP_INFO_ID 100
7851 	u16 id[MAX_ANQP_INFO_ID];
7852 	size_t num_id = 0;
7853 	u32 subtypes = 0;
7854 	u32 mbo_subtypes = 0;
7855 
7856 	used = hwaddr_aton2(dst, dst_addr);
7857 	if (used < 0)
7858 		return -1;
7859 	pos = dst + used;
7860 	if (*pos == ' ')
7861 		pos++;
7862 
7863 	if (os_strncmp(pos, "freq=", 5) == 0) {
7864 		freq = atoi(pos + 5);
7865 		pos = os_strchr(pos, ' ');
7866 		if (!pos)
7867 			return -1;
7868 		pos++;
7869 	}
7870 
7871 	while (num_id < MAX_ANQP_INFO_ID) {
7872 		if (os_strncmp(pos, "hs20:", 5) == 0) {
7873 #ifdef CONFIG_HS20
7874 			int num = atoi(pos + 5);
7875 			if (num <= 0 || num > 31)
7876 				return -1;
7877 			subtypes |= BIT(num);
7878 #else /* CONFIG_HS20 */
7879 			return -1;
7880 #endif /* CONFIG_HS20 */
7881 		} else if (os_strncmp(pos, "mbo:", 4) == 0) {
7882 #ifdef CONFIG_MBO
7883 			int num = atoi(pos + 4);
7884 
7885 			if (num <= 0 || num > MAX_MBO_ANQP_SUBTYPE)
7886 				return -1;
7887 			mbo_subtypes |= BIT(num);
7888 #else /* CONFIG_MBO */
7889 			return -1;
7890 #endif /* CONFIG_MBO */
7891 		} else {
7892 			id[num_id] = atoi(pos);
7893 			if (id[num_id])
7894 				num_id++;
7895 		}
7896 		pos = os_strchr(pos + 1, ',');
7897 		if (pos == NULL)
7898 			break;
7899 		pos++;
7900 	}
7901 
7902 	if (num_id == 0 && !subtypes && !mbo_subtypes)
7903 		return -1;
7904 
7905 	return anqp_send_req(wpa_s, dst_addr, freq, id, num_id, subtypes,
7906 			     mbo_subtypes);
7907 }
7908 
7909 
gas_request(struct wpa_supplicant * wpa_s,char * cmd)7910 static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
7911 {
7912 	u8 dst_addr[ETH_ALEN];
7913 	struct wpabuf *advproto, *query = NULL;
7914 	int used, ret = -1;
7915 	char *pos, *end;
7916 	size_t len;
7917 
7918 	used = hwaddr_aton2(cmd, dst_addr);
7919 	if (used < 0)
7920 		return -1;
7921 
7922 	pos = cmd + used;
7923 	while (*pos == ' ')
7924 		pos++;
7925 
7926 	/* Advertisement Protocol ID */
7927 	end = os_strchr(pos, ' ');
7928 	if (end)
7929 		len = end - pos;
7930 	else
7931 		len = os_strlen(pos);
7932 	if (len & 0x01)
7933 		return -1;
7934 	len /= 2;
7935 	if (len == 0)
7936 		return -1;
7937 	advproto = wpabuf_alloc(len);
7938 	if (advproto == NULL)
7939 		return -1;
7940 	if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
7941 		goto fail;
7942 
7943 	if (end) {
7944 		/* Optional Query Request */
7945 		pos = end + 1;
7946 		while (*pos == ' ')
7947 			pos++;
7948 
7949 		len = os_strlen(pos);
7950 		if (len) {
7951 			if (len & 0x01)
7952 				goto fail;
7953 			len /= 2;
7954 			if (len == 0)
7955 				goto fail;
7956 			query = wpabuf_alloc(len);
7957 			if (query == NULL)
7958 				goto fail;
7959 			if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
7960 				goto fail;
7961 		}
7962 	}
7963 
7964 	ret = gas_send_request(wpa_s, dst_addr, advproto, query);
7965 
7966 fail:
7967 	wpabuf_free(advproto);
7968 	wpabuf_free(query);
7969 
7970 	return ret;
7971 }
7972 
7973 
gas_response_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)7974 static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
7975 			    size_t buflen)
7976 {
7977 	u8 addr[ETH_ALEN];
7978 	int dialog_token;
7979 	int used;
7980 	char *pos;
7981 	size_t resp_len, start, requested_len;
7982 	struct wpabuf *resp;
7983 	int ret;
7984 
7985 	used = hwaddr_aton2(cmd, addr);
7986 	if (used < 0)
7987 		return -1;
7988 
7989 	pos = cmd + used;
7990 	while (*pos == ' ')
7991 		pos++;
7992 	dialog_token = atoi(pos);
7993 
7994 	if (wpa_s->last_gas_resp &&
7995 	    os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
7996 	    dialog_token == wpa_s->last_gas_dialog_token)
7997 		resp = wpa_s->last_gas_resp;
7998 	else if (wpa_s->prev_gas_resp &&
7999 		 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
8000 		 dialog_token == wpa_s->prev_gas_dialog_token)
8001 		resp = wpa_s->prev_gas_resp;
8002 	else
8003 		return -1;
8004 
8005 	resp_len = wpabuf_len(resp);
8006 	start = 0;
8007 	requested_len = resp_len;
8008 
8009 	pos = os_strchr(pos, ' ');
8010 	if (pos) {
8011 		start = atoi(pos);
8012 		if (start > resp_len)
8013 			return os_snprintf(buf, buflen, "FAIL-Invalid range");
8014 		pos = os_strchr(pos, ',');
8015 		if (pos == NULL)
8016 			return -1;
8017 		pos++;
8018 		requested_len = atoi(pos);
8019 		if (start + requested_len > resp_len)
8020 			return os_snprintf(buf, buflen, "FAIL-Invalid range");
8021 	}
8022 
8023 	if (requested_len * 2 + 1 > buflen)
8024 		return os_snprintf(buf, buflen, "FAIL-Too long response");
8025 
8026 	ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
8027 			       requested_len);
8028 
8029 	if (start + requested_len == resp_len) {
8030 		/*
8031 		 * Free memory by dropping the response after it has been
8032 		 * fetched.
8033 		 */
8034 		if (resp == wpa_s->prev_gas_resp) {
8035 			wpabuf_free(wpa_s->prev_gas_resp);
8036 			wpa_s->prev_gas_resp = NULL;
8037 		} else {
8038 			wpabuf_free(wpa_s->last_gas_resp);
8039 			wpa_s->last_gas_resp = NULL;
8040 		}
8041 	}
8042 
8043 	return ret;
8044 }
8045 #endif /* CONFIG_INTERWORKING */
8046 
8047 
8048 #ifdef CONFIG_HS20
8049 
get_hs20_anqp(struct wpa_supplicant * wpa_s,char * dst)8050 static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
8051 {
8052 	u8 dst_addr[ETH_ALEN];
8053 	int used;
8054 	char *pos;
8055 	u32 subtypes = 0;
8056 
8057 	used = hwaddr_aton2(dst, dst_addr);
8058 	if (used < 0)
8059 		return -1;
8060 	pos = dst + used;
8061 	if (*pos == ' ')
8062 		pos++;
8063 	for (;;) {
8064 		int num = atoi(pos);
8065 		if (num <= 0 || num > 31)
8066 			return -1;
8067 		subtypes |= BIT(num);
8068 		pos = os_strchr(pos + 1, ',');
8069 		if (pos == NULL)
8070 			break;
8071 		pos++;
8072 	}
8073 
8074 	if (subtypes == 0)
8075 		return -1;
8076 
8077 	return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
8078 }
8079 
8080 
hs20_nai_home_realm_list(struct wpa_supplicant * wpa_s,const u8 * addr,const char * realm)8081 static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
8082 				    const u8 *addr, const char *realm)
8083 {
8084 	u8 *buf;
8085 	size_t rlen, len;
8086 	int ret;
8087 
8088 	rlen = os_strlen(realm);
8089 	len = 3 + rlen;
8090 	buf = os_malloc(len);
8091 	if (buf == NULL)
8092 		return -1;
8093 	buf[0] = 1; /* NAI Home Realm Count */
8094 	buf[1] = 0; /* Formatted in accordance with RFC 4282 */
8095 	buf[2] = rlen;
8096 	os_memcpy(buf + 3, realm, rlen);
8097 
8098 	ret = hs20_anqp_send_req(wpa_s, addr,
8099 				 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
8100 				 buf, len, 0);
8101 
8102 	os_free(buf);
8103 
8104 	return ret;
8105 }
8106 
8107 
hs20_get_nai_home_realm_list(struct wpa_supplicant * wpa_s,char * dst)8108 static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
8109 					char *dst)
8110 {
8111 	struct wpa_cred *cred = wpa_s->conf->cred;
8112 	u8 dst_addr[ETH_ALEN];
8113 	int used;
8114 	u8 *buf;
8115 	size_t len;
8116 	int ret;
8117 
8118 	used = hwaddr_aton2(dst, dst_addr);
8119 	if (used < 0)
8120 		return -1;
8121 
8122 	while (dst[used] == ' ')
8123 		used++;
8124 	if (os_strncmp(dst + used, "realm=", 6) == 0)
8125 		return hs20_nai_home_realm_list(wpa_s, dst_addr,
8126 						dst + used + 6);
8127 
8128 	len = os_strlen(dst + used);
8129 
8130 	if (len == 0 && cred && cred->realm)
8131 		return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
8132 
8133 	if (len & 1)
8134 		return -1;
8135 	len /= 2;
8136 	buf = os_malloc(len);
8137 	if (buf == NULL)
8138 		return -1;
8139 	if (hexstr2bin(dst + used, buf, len) < 0) {
8140 		os_free(buf);
8141 		return -1;
8142 	}
8143 
8144 	ret = hs20_anqp_send_req(wpa_s, dst_addr,
8145 				 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
8146 				 buf, len, 0);
8147 	os_free(buf);
8148 
8149 	return ret;
8150 }
8151 
8152 
get_hs20_icon(struct wpa_supplicant * wpa_s,char * cmd,char * reply,int buflen)8153 static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
8154 			 int buflen)
8155 {
8156 	u8 dst_addr[ETH_ALEN];
8157 	int used;
8158 	char *ctx = NULL, *icon, *poffset, *psize;
8159 
8160 	used = hwaddr_aton2(cmd, dst_addr);
8161 	if (used < 0)
8162 		return -1;
8163 	cmd += used;
8164 
8165 	icon = str_token(cmd, " ", &ctx);
8166 	poffset = str_token(cmd, " ", &ctx);
8167 	psize = str_token(cmd, " ", &ctx);
8168 	if (!icon || !poffset || !psize)
8169 		return -1;
8170 
8171 	wpa_s->fetch_osu_icon_in_progress = 0;
8172 	return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
8173 			     reply, buflen);
8174 }
8175 
8176 
del_hs20_icon(struct wpa_supplicant * wpa_s,char * cmd)8177 static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
8178 {
8179 	u8 dst_addr[ETH_ALEN];
8180 	int used;
8181 	char *icon;
8182 
8183 	if (!cmd[0])
8184 		return hs20_del_icon(wpa_s, NULL, NULL);
8185 
8186 	used = hwaddr_aton2(cmd, dst_addr);
8187 	if (used < 0)
8188 		return -1;
8189 
8190 	while (cmd[used] == ' ')
8191 		used++;
8192 	icon = cmd[used] ? &cmd[used] : NULL;
8193 
8194 	return hs20_del_icon(wpa_s, dst_addr, icon);
8195 }
8196 
8197 
hs20_icon_request(struct wpa_supplicant * wpa_s,char * cmd,int inmem)8198 static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
8199 {
8200 	u8 dst_addr[ETH_ALEN];
8201 	int used;
8202 	char *icon;
8203 
8204 	used = hwaddr_aton2(cmd, dst_addr);
8205 	if (used < 0)
8206 		return -1;
8207 
8208 	while (cmd[used] == ' ')
8209 		used++;
8210 	icon = &cmd[used];
8211 
8212 	wpa_s->fetch_osu_icon_in_progress = 0;
8213 	return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
8214 				  (u8 *) icon, os_strlen(icon), inmem);
8215 }
8216 
8217 #endif /* CONFIG_HS20 */
8218 
8219 
8220 #ifdef CONFIG_AUTOSCAN
8221 
wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant * wpa_s,char * cmd)8222 static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
8223 					      char *cmd)
8224 {
8225 	enum wpa_states state = wpa_s->wpa_state;
8226 	char *new_params = NULL;
8227 
8228 	if (os_strlen(cmd) > 0) {
8229 		new_params = os_strdup(cmd);
8230 		if (new_params == NULL)
8231 			return -1;
8232 	}
8233 
8234 	os_free(wpa_s->conf->autoscan);
8235 	wpa_s->conf->autoscan = new_params;
8236 
8237 	if (wpa_s->conf->autoscan == NULL)
8238 		autoscan_deinit(wpa_s);
8239 	else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
8240 		autoscan_init(wpa_s, 1);
8241 	else if (state == WPA_SCANNING)
8242 		wpa_supplicant_reinit_autoscan(wpa_s);
8243 	else
8244 		wpa_printf(MSG_DEBUG, "No autoscan update in state %s",
8245 			   wpa_supplicant_state_txt(state));
8246 
8247 	return 0;
8248 }
8249 
8250 #endif /* CONFIG_AUTOSCAN */
8251 
8252 
8253 #ifdef CONFIG_WNM
8254 
wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant * wpa_s,char * cmd)8255 static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
8256 {
8257 	int enter;
8258 	int intval = 0;
8259 	char *pos;
8260 	int ret;
8261 	struct wpabuf *tfs_req = NULL;
8262 
8263 	if (os_strncmp(cmd, "enter", 5) == 0)
8264 		enter = 1;
8265 	else if (os_strncmp(cmd, "exit", 4) == 0)
8266 		enter = 0;
8267 	else
8268 		return -1;
8269 
8270 	pos = os_strstr(cmd, " interval=");
8271 	if (pos)
8272 		intval = atoi(pos + 10);
8273 
8274 	pos = os_strstr(cmd, " tfs_req=");
8275 	if (pos) {
8276 		char *end;
8277 		size_t len;
8278 		pos += 9;
8279 		end = os_strchr(pos, ' ');
8280 		if (end)
8281 			len = end - pos;
8282 		else
8283 			len = os_strlen(pos);
8284 		if (len & 1)
8285 			return -1;
8286 		len /= 2;
8287 		tfs_req = wpabuf_alloc(len);
8288 		if (tfs_req == NULL)
8289 			return -1;
8290 		if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
8291 			wpabuf_free(tfs_req);
8292 			return -1;
8293 		}
8294 	}
8295 
8296 	ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
8297 					   WNM_SLEEP_MODE_EXIT, intval,
8298 					   tfs_req);
8299 	wpabuf_free(tfs_req);
8300 
8301 	return ret;
8302 }
8303 
8304 
wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant * wpa_s,char * cmd)8305 static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
8306 {
8307 	int query_reason, list = 0;
8308 	char *btm_candidates = NULL;
8309 
8310 	query_reason = atoi(cmd);
8311 
8312 	cmd = os_strchr(cmd, ' ');
8313 	if (cmd) {
8314 		if (os_strncmp(cmd, " list", 5) == 0)
8315 			list = 1;
8316 		else
8317 			btm_candidates = cmd;
8318 	}
8319 
8320 	wpa_printf(MSG_DEBUG,
8321 		   "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
8322 		   query_reason, list ? " candidate list" : "");
8323 
8324 	return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason,
8325 						  btm_candidates,
8326 						  list);
8327 }
8328 
8329 
wpas_ctrl_iface_coloc_intf_report(struct wpa_supplicant * wpa_s,char * cmd)8330 static int wpas_ctrl_iface_coloc_intf_report(struct wpa_supplicant *wpa_s,
8331 					     char *cmd)
8332 {
8333 	struct wpabuf *elems;
8334 	int ret;
8335 
8336 	elems = wpabuf_parse_bin(cmd);
8337 	if (!elems)
8338 		return -1;
8339 
8340 	ret = wnm_send_coloc_intf_report(wpa_s, 0, elems);
8341 	wpabuf_free(elems);
8342 	return ret;
8343 }
8344 
8345 #endif /* CONFIG_WNM */
8346 
8347 
wpa_supplicant_signal_poll(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8348 static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
8349 				      size_t buflen)
8350 {
8351 	struct wpa_signal_info si;
8352 	int ret;
8353 	char *pos, *end;
8354 
8355 	ret = wpa_drv_signal_poll(wpa_s, &si);
8356 	if (ret)
8357 		return -1;
8358 
8359 	pos = buf;
8360 	end = buf + buflen;
8361 
8362 	ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%lu\n"
8363 			  "NOISE=%d\nFREQUENCY=%u\n",
8364 			  si.data.signal, si.data.current_tx_rate / 1000,
8365 			  si.current_noise, si.frequency);
8366 	if (os_snprintf_error(end - pos, ret))
8367 		return -1;
8368 	pos += ret;
8369 
8370 	if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
8371 		ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
8372 				  channel_width_to_string(si.chanwidth));
8373 		if (os_snprintf_error(end - pos, ret))
8374 			return -1;
8375 		pos += ret;
8376 	}
8377 
8378 	if (si.center_frq1 > 0) {
8379 		ret = os_snprintf(pos, end - pos, "CENTER_FRQ1=%d\n",
8380 				  si.center_frq1);
8381 		if (os_snprintf_error(end - pos, ret))
8382 			return -1;
8383 		pos += ret;
8384 	}
8385 
8386 	if (si.center_frq2 > 0) {
8387 		ret = os_snprintf(pos, end - pos, "CENTER_FRQ2=%d\n",
8388 				  si.center_frq2);
8389 		if (os_snprintf_error(end - pos, ret))
8390 			return -1;
8391 		pos += ret;
8392 	}
8393 
8394 	if (si.data.avg_signal) {
8395 		ret = os_snprintf(pos, end - pos,
8396 				  "AVG_RSSI=%d\n", si.data.avg_signal);
8397 		if (os_snprintf_error(end - pos, ret))
8398 			return -1;
8399 		pos += ret;
8400 	}
8401 
8402 	if (si.data.avg_beacon_signal) {
8403 		ret = os_snprintf(pos, end - pos,
8404 				  "AVG_BEACON_RSSI=%d\n",
8405 				  si.data.avg_beacon_signal);
8406 		if (os_snprintf_error(end - pos, ret))
8407 			return -1;
8408 		pos += ret;
8409 	}
8410 
8411 	return pos - buf;
8412 }
8413 
8414 
wpas_ctrl_iface_signal_monitor(struct wpa_supplicant * wpa_s,const char * cmd)8415 static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
8416 					  const char *cmd)
8417 {
8418 	const char *pos;
8419 	int threshold = 0;
8420 	int hysteresis = 0;
8421 
8422 	if (wpa_s->bgscan && wpa_s->bgscan_priv) {
8423 		wpa_printf(MSG_DEBUG,
8424 			   "Reject SIGNAL_MONITOR command - bgscan is active");
8425 		return -1;
8426 	}
8427 	pos = os_strstr(cmd, "THRESHOLD=");
8428 	if (pos)
8429 		threshold = atoi(pos + 10);
8430 	pos = os_strstr(cmd, "HYSTERESIS=");
8431 	if (pos)
8432 		hysteresis = atoi(pos + 11);
8433 	return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
8434 }
8435 
8436 
8437 #ifdef CONFIG_TESTING_OPTIONS
wpas_ctrl_iface_get_pref_freq_list_override(struct wpa_supplicant * wpa_s,enum wpa_driver_if_type if_type,unsigned int * num,struct weighted_pcl * freq_list)8438 int wpas_ctrl_iface_get_pref_freq_list_override(struct wpa_supplicant *wpa_s,
8439 						enum wpa_driver_if_type if_type,
8440 						unsigned int *num,
8441 						struct weighted_pcl *freq_list)
8442 {
8443 	char *pos = wpa_s->get_pref_freq_list_override;
8444 	char *end;
8445 	unsigned int count = 0;
8446 
8447 	/* Override string format:
8448 	 *  <if_type1>:<freq1>,<freq2>,... <if_type2>:... */
8449 
8450 	while (pos) {
8451 		if (atoi(pos) == (int) if_type)
8452 			break;
8453 		pos = os_strchr(pos, ' ');
8454 		if (pos)
8455 			pos++;
8456 	}
8457 	if (!pos)
8458 		return -1;
8459 	pos = os_strchr(pos, ':');
8460 	if (!pos)
8461 		return -1;
8462 	pos++;
8463 	end = os_strchr(pos, ' ');
8464 	while (pos && (!end || pos < end) && count < *num) {
8465 		freq_list[count].freq = atoi(pos);
8466 		freq_list[count++].flag = WEIGHTED_PCL_GO | WEIGHTED_PCL_CLI;
8467 		pos = os_strchr(pos, ',');
8468 		if (pos)
8469 			pos++;
8470 	}
8471 
8472 	*num = count;
8473 	return 0;
8474 }
8475 #endif /* CONFIG_TESTING_OPTIONS */
8476 
8477 
wpas_ctrl_iface_get_pref_freq_list(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8478 static int wpas_ctrl_iface_get_pref_freq_list(
8479 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
8480 {
8481 	unsigned int num = 100, i;
8482 	int ret;
8483 	enum wpa_driver_if_type iface_type;
8484 	char *pos, *end;
8485 	struct weighted_pcl freq_list[100];
8486 
8487 	pos = buf;
8488 	end = buf + buflen;
8489 
8490 	/* buf: "<interface_type>" */
8491 	if (os_strcmp(cmd, "STATION") == 0)
8492 		iface_type = WPA_IF_STATION;
8493 	else if (os_strcmp(cmd, "AP") == 0)
8494 		iface_type = WPA_IF_AP_BSS;
8495 	else if (os_strcmp(cmd, "P2P_GO") == 0)
8496 		iface_type = WPA_IF_P2P_GO;
8497 	else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
8498 		iface_type = WPA_IF_P2P_CLIENT;
8499 	else if (os_strcmp(cmd, "IBSS") == 0)
8500 		iface_type = WPA_IF_IBSS;
8501 	else if (os_strcmp(cmd, "TDLS") == 0)
8502 		iface_type = WPA_IF_TDLS;
8503 	else
8504 		return -1;
8505 
8506 	wpa_printf(MSG_DEBUG,
8507 		   "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
8508 		   iface_type, cmd);
8509 
8510 	ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
8511 	if (ret)
8512 		return -1;
8513 
8514 	for (i = 0; i < num; i++) {
8515 		ret = os_snprintf(pos, end - pos, "%s%u",
8516 				  i > 0 ? "," : "", freq_list[i].freq);
8517 		if (os_snprintf_error(end - pos, ret))
8518 			return -1;
8519 		pos += ret;
8520 	}
8521 
8522 	return pos - buf;
8523 }
8524 
8525 
wpas_ctrl_iface_driver_flags(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8526 static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
8527 					char *buf, size_t buflen)
8528 {
8529 	int ret, i;
8530 	char *pos, *end;
8531 
8532 	ret = os_snprintf(buf, buflen, "%016llX:\n",
8533 			  (long long unsigned) wpa_s->drv_flags);
8534 	if (os_snprintf_error(buflen, ret))
8535 		return -1;
8536 
8537 	pos = buf + ret;
8538 	end = buf + buflen;
8539 
8540 	for (i = 0; i < 64; i++) {
8541 		if (wpa_s->drv_flags & (1LLU << i)) {
8542 			ret = os_snprintf(pos, end - pos, "%s\n",
8543 					  driver_flag_to_string(1LLU << i));
8544 			if (os_snprintf_error(end - pos, ret))
8545 				return -1;
8546 			pos += ret;
8547 		}
8548 	}
8549 
8550 	return pos - buf;
8551 }
8552 
8553 
wpas_ctrl_iface_driver_flags2(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8554 static int wpas_ctrl_iface_driver_flags2(struct wpa_supplicant *wpa_s,
8555 					 char *buf, size_t buflen)
8556 {
8557 	int ret, i;
8558 	char *pos, *end;
8559 
8560 	ret = os_snprintf(buf, buflen, "%016llX:\n",
8561 			  (long long unsigned) wpa_s->drv_flags2);
8562 	if (os_snprintf_error(buflen, ret))
8563 		return -1;
8564 
8565 	pos = buf + ret;
8566 	end = buf + buflen;
8567 
8568 	for (i = 0; i < 64; i++) {
8569 		if (wpa_s->drv_flags2 & (1LLU << i)) {
8570 			ret = os_snprintf(pos, end - pos, "%s\n",
8571 					  driver_flag2_to_string(1LLU << i));
8572 			if (os_snprintf_error(end - pos, ret))
8573 				return -1;
8574 			pos += ret;
8575 		}
8576 	}
8577 
8578 	return pos - buf;
8579 }
8580 
8581 
wpa_supplicant_pktcnt_poll(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8582 static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
8583 				      size_t buflen)
8584 {
8585 	struct hostap_sta_driver_data sta;
8586 	int ret;
8587 
8588 	ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
8589 	if (ret)
8590 		return -1;
8591 
8592 	ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
8593 			  sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
8594 	if (os_snprintf_error(buflen, ret))
8595 		return -1;
8596 	return ret;
8597 }
8598 
8599 
8600 #ifdef ANDROID
wpa_supplicant_driver_cmd(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8601 static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
8602 				     char *buf, size_t buflen)
8603 {
8604 	int ret;
8605 
8606 	ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
8607 	if (ret == 0) {
8608 		if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
8609 			struct p2p_data *p2p = wpa_s->global->p2p;
8610 			if (p2p) {
8611 				char country[3];
8612 				country[0] = cmd[8];
8613 				country[1] = cmd[9];
8614 				country[2] = 0x04;
8615 				p2p_set_country(p2p, country);
8616 			}
8617 		}
8618 		ret = os_snprintf(buf, buflen, "%s\n", "OK");
8619 		if (os_snprintf_error(buflen, ret))
8620 			ret = -1;
8621 	}
8622 	return ret;
8623 }
8624 #endif /* ANDROID */
8625 
8626 
wpa_supplicant_vendor_cmd(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8627 static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
8628 				     char *buf, size_t buflen)
8629 {
8630 	int ret;
8631 	char *pos, *temp = NULL;
8632 	u8 *data = NULL;
8633 	unsigned int vendor_id, subcmd;
8634 	enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
8635 	struct wpabuf *reply;
8636 	size_t data_len = 0;
8637 
8638 	/**
8639 	 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
8640 	 * [nested=<0|1>]
8641 	 */
8642 	vendor_id = strtoul(cmd, &pos, 16);
8643 	if (!isblank((unsigned char) *pos))
8644 		return -EINVAL;
8645 
8646 	subcmd = strtoul(pos, &pos, 10);
8647 
8648 	if (*pos != '\0') {
8649 		if (!isblank((unsigned char) *pos++))
8650 			return -EINVAL;
8651 
8652 		temp = os_strchr(pos, ' ');
8653 		data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
8654 	}
8655 
8656 	if (data_len) {
8657 		data_len /= 2;
8658 		data = os_malloc(data_len);
8659 		if (!data)
8660 			return -1;
8661 
8662 		if (hexstr2bin(pos, data, data_len)) {
8663 			wpa_printf(MSG_DEBUG,
8664 				   "Vendor command: wrong parameter format");
8665 			os_free(data);
8666 			return -EINVAL;
8667 		}
8668 	}
8669 
8670 	pos = os_strstr(cmd, "nested=");
8671 	if (pos)
8672 		nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
8673 			NESTED_ATTR_NOT_USED;
8674 
8675 	reply = wpabuf_alloc((buflen - 1) / 2);
8676 	if (!reply) {
8677 		os_free(data);
8678 		return -1;
8679 	}
8680 
8681 	ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
8682 				 nested_attr_flag, reply);
8683 
8684 	if (ret == 0)
8685 		ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
8686 				       wpabuf_len(reply));
8687 
8688 	wpabuf_free(reply);
8689 	os_free(data);
8690 
8691 	return ret;
8692 }
8693 
8694 
wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant * wpa_s)8695 static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
8696 {
8697 #ifdef CONFIG_P2P
8698 	struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
8699 		wpa_s->global->p2p_init_wpa_s : wpa_s;
8700 #endif /* CONFIG_P2P */
8701 
8702 	wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
8703 
8704 	if (wpas_abort_ongoing_scan(wpa_s) == 0)
8705 		wpa_s->ignore_post_flush_scan_res = 1;
8706 
8707 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
8708 		/*
8709 		 * Avoid possible auto connect re-connection on getting
8710 		 * disconnected due to state flush.
8711 		 */
8712 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
8713 	}
8714 
8715 #ifdef CONFIG_P2P
8716 	wpas_p2p_group_remove(p2p_wpa_s, "*");
8717 	wpas_p2p_cancel(p2p_wpa_s);
8718 	p2p_ctrl_flush(p2p_wpa_s);
8719 	wpas_p2p_service_flush(p2p_wpa_s);
8720 	p2p_wpa_s->global->p2p_disabled = 0;
8721 	p2p_wpa_s->global->p2p_per_sta_psk = 0;
8722 	p2p_wpa_s->conf->num_sec_device_types = 0;
8723 	p2p_wpa_s->p2p_disable_ip_addr_req = 0;
8724 	os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
8725 	p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
8726 	p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
8727 	p2p_wpa_s->global->pending_p2ps_group = 0;
8728 	p2p_wpa_s->global->pending_p2ps_group_freq = 0;
8729 #endif /* CONFIG_P2P */
8730 
8731 #ifdef CONFIG_WPS_TESTING
8732 	wps_version_number = 0x20;
8733 	wps_testing_stub_cred = 0;
8734 	wps_corrupt_pkhash = 0;
8735 	wps_force_auth_types_in_use = 0;
8736 	wps_force_encr_types_in_use = 0;
8737 #endif /* CONFIG_WPS_TESTING */
8738 #ifdef CONFIG_WPS
8739 	wpa_s->wps_fragment_size = 0;
8740 	wpas_wps_cancel(wpa_s);
8741 	wps_registrar_flush(wpa_s->wps->registrar);
8742 #endif /* CONFIG_WPS */
8743 	wpa_s->after_wps = 0;
8744 	wpa_s->known_wps_freq = 0;
8745 
8746 #ifdef CONFIG_DPP
8747 	wpas_dpp_deinit(wpa_s);
8748 	wpa_s->dpp_init_max_tries = 0;
8749 	wpa_s->dpp_init_retry_time = 0;
8750 	wpa_s->dpp_resp_wait_time = 0;
8751 	wpa_s->dpp_resp_max_tries = 0;
8752 	wpa_s->dpp_resp_retry_time = 0;
8753 #ifdef CONFIG_DPP2
8754 	wpas_dpp_chirp_stop(wpa_s);
8755 	wpa_s->dpp_pfs_fallback = 0;
8756 #endif /* CONFIG_DPP2 */
8757 #ifdef CONFIG_DPP3
8758 	{
8759 		int i;
8760 
8761 		for (i = 0; i < DPP_PB_INFO_COUNT; i++) {
8762 			struct dpp_pb_info *info;
8763 
8764 			info = &wpa_s->dpp_pb[i];
8765 			info->rx_time.sec = 0;
8766 			info->rx_time.usec = 0;
8767 		}
8768 	}
8769 #endif /* CONFIG_DPP3 */
8770 #ifdef CONFIG_TESTING_OPTIONS
8771 	os_memset(dpp_pkex_own_mac_override, 0, ETH_ALEN);
8772 	os_memset(dpp_pkex_peer_mac_override, 0, ETH_ALEN);
8773 	dpp_pkex_ephemeral_key_override_len = 0;
8774 	dpp_protocol_key_override_len = 0;
8775 	dpp_nonce_override_len = 0;
8776 #ifdef CONFIG_DPP3
8777 	dpp_version_override = 3;
8778 #elif defined(CONFIG_DPP2)
8779 	dpp_version_override = 2;
8780 #else /* CONFIG_DPP2 */
8781 	dpp_version_override = 1;
8782 #endif /* CONFIG_DPP2 */
8783 #endif /* CONFIG_TESTING_OPTIONS */
8784 #endif /* CONFIG_DPP */
8785 
8786 #ifdef CONFIG_TDLS
8787 #ifdef CONFIG_TDLS_TESTING
8788 	tdls_testing = 0;
8789 #endif /* CONFIG_TDLS_TESTING */
8790 	wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
8791 	wpa_tdls_enable(wpa_s->wpa, 1);
8792 #endif /* CONFIG_TDLS */
8793 
8794 	eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
8795 	wpa_supplicant_stop_countermeasures(wpa_s, NULL);
8796 	wpa_s->last_michael_mic_error.sec = 0;
8797 
8798 	wpa_s->no_keep_alive = 0;
8799 	wpa_s->own_disconnect_req = 0;
8800 	wpa_s->own_reconnect_req = 0;
8801 	wpa_s->deny_ptk0_rekey = 0;
8802 
8803 	os_free(wpa_s->disallow_aps_bssid);
8804 	wpa_s->disallow_aps_bssid = NULL;
8805 	wpa_s->disallow_aps_bssid_count = 0;
8806 	os_free(wpa_s->disallow_aps_ssid);
8807 	wpa_s->disallow_aps_ssid = NULL;
8808 	wpa_s->disallow_aps_ssid_count = 0;
8809 
8810 	wpa_s->set_sta_uapsd = 0;
8811 	wpa_s->sta_uapsd = 0;
8812 
8813 	wpa_s->consecutive_conn_failures = 0;
8814 
8815 	wpa_drv_radio_disable(wpa_s, 0);
8816 	wpa_bssid_ignore_clear(wpa_s);
8817 	wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
8818 	wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
8819 	wpa_config_flush_blobs(wpa_s->conf);
8820 	wpa_s->conf->auto_interworking = 0;
8821 	wpa_s->conf->okc = 0;
8822 
8823 	ptksa_cache_flush(wpa_s->ptksa, NULL, WPA_CIPHER_NONE);
8824 	wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
8825 	rsn_preauth_deinit(wpa_s->wpa);
8826 
8827 	wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
8828 	wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
8829 	wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
8830 	eapol_sm_notify_logoff(wpa_s->eapol, false);
8831 
8832 	radio_remove_works(wpa_s, NULL, 1);
8833 	wpa_s->ext_work_in_progress = 0;
8834 
8835 	wpa_s->next_ssid = NULL;
8836 
8837 #ifdef CONFIG_INTERWORKING
8838 #ifdef CONFIG_HS20
8839 	hs20_cancel_fetch_osu(wpa_s);
8840 	hs20_del_icon(wpa_s, NULL, NULL);
8841 #endif /* CONFIG_HS20 */
8842 #endif /* CONFIG_INTERWORKING */
8843 
8844 	wpa_s->ext_mgmt_frame_handling = 0;
8845 	wpa_s->ext_eapol_frame_io = 0;
8846 #ifdef CONFIG_TESTING_OPTIONS
8847 	wpa_s->extra_roc_dur = 0;
8848 	wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
8849 	wpa_s->p2p_go_csa_on_inv = 0;
8850 	wpa_s->ignore_auth_resp = 0;
8851 	wpa_s->ignore_assoc_disallow = 0;
8852 	wpa_s->disable_sa_query = 0;
8853 	wpa_s->testing_resend_assoc = 0;
8854 	wpa_s->ignore_sae_h2e_only = 0;
8855 	wpa_s->ft_rsnxe_used = 0;
8856 	wpa_s->reject_btm_req_reason = 0;
8857 	wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
8858 	os_free(wpa_s->get_pref_freq_list_override);
8859 	wpa_s->get_pref_freq_list_override = NULL;
8860 	wpabuf_free(wpa_s->sae_commit_override);
8861 	wpa_s->sae_commit_override = NULL;
8862 	os_free(wpa_s->extra_sae_rejected_groups);
8863 	wpa_s->extra_sae_rejected_groups = NULL;
8864 	wpabuf_free(wpa_s->rsne_override_eapol);
8865 	wpa_s->rsne_override_eapol = NULL;
8866 	wpabuf_free(wpa_s->rsnxe_override_assoc);
8867 	wpa_s->rsnxe_override_assoc = NULL;
8868 	wpabuf_free(wpa_s->rsnxe_override_eapol);
8869 	wpa_s->rsnxe_override_eapol = NULL;
8870 	wpas_clear_driver_signal_override(wpa_s);
8871 	wpa_s->disable_scs_support = 0;
8872 	wpa_s->disable_mscs_support = 0;
8873 	wpa_s->enable_dscp_policy_capa = 0;
8874 	wpa_s->oci_freq_override_eapol = 0;
8875 	wpa_s->oci_freq_override_saquery_req = 0;
8876 	wpa_s->oci_freq_override_saquery_resp = 0;
8877 	wpa_s->oci_freq_override_eapol_g2 = 0;
8878 	wpa_s->oci_freq_override_ft_assoc = 0;
8879 	wpa_s->oci_freq_override_fils_assoc = 0;
8880 	wpa_s->oci_freq_override_wnm_sleep = 0;
8881 	wpa_s->disable_eapol_g2_tx = 0;
8882 #ifdef CONFIG_DPP
8883 	os_free(wpa_s->dpp_config_obj_override);
8884 	wpa_s->dpp_config_obj_override = NULL;
8885 	os_free(wpa_s->dpp_discovery_override);
8886 	wpa_s->dpp_discovery_override = NULL;
8887 	os_free(wpa_s->dpp_groups_override);
8888 	wpa_s->dpp_groups_override = NULL;
8889 	wpa_s->dpp_ignore_netaccesskey_mismatch = 0;
8890 	wpa_s->dpp_discard_public_action = 0;
8891 	dpp_test = DPP_TEST_DISABLED;
8892 #endif /* CONFIG_DPP */
8893 #endif /* CONFIG_TESTING_OPTIONS */
8894 
8895 	wpa_s->disconnected = 0;
8896 	os_free(wpa_s->next_scan_freqs);
8897 	wpa_s->next_scan_freqs = NULL;
8898 	os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
8899 	wpa_s->next_scan_bssid_wildcard_ssid = 0;
8900 	os_free(wpa_s->select_network_scan_freqs);
8901 	wpa_s->select_network_scan_freqs = NULL;
8902 	os_memset(&wpa_s->robust_av, 0, sizeof(struct robust_av_data));
8903 
8904 	wpa_bss_flush(wpa_s);
8905 	if (!dl_list_empty(&wpa_s->bss)) {
8906 		wpa_printf(MSG_DEBUG,
8907 			   "BSS table not empty after flush: %u entries, current_bss=%p bssid="
8908 			   MACSTR " pending_bssid=" MACSTR,
8909 			   dl_list_len(&wpa_s->bss), wpa_s->current_bss,
8910 			   MAC2STR(wpa_s->bssid),
8911 			   MAC2STR(wpa_s->pending_bssid));
8912 	}
8913 
8914 	eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
8915 	wpa_s->wnmsleep_used = 0;
8916 
8917 #ifdef CONFIG_SME
8918 	wpa_s->sme.last_unprot_disconnect.sec = 0;
8919 	wpa_s->sme.auth_alg = 0;
8920 #endif /* CONFIG_SME */
8921 
8922 	wpabuf_free(wpa_s->ric_ies);
8923 	wpa_s->ric_ies = NULL;
8924 
8925 	wpa_supplicant_update_channel_list(wpa_s, NULL);
8926 
8927 	free_bss_tmp_disallowed(wpa_s);
8928 
8929 	os_memset(&wpa_s->robust_av, 0, sizeof(struct robust_av_data));
8930 
8931 #ifdef CONFIG_PASN
8932 	wpas_pasn_auth_stop(wpa_s);
8933 #endif /* CONFIG_PASN */
8934 
8935 	if (wpa_s->mac_addr_changed && wpa_s->conf->mac_addr == 0)
8936 		wpas_restore_permanent_mac_addr(wpa_s);
8937 
8938 	wpa_s->conf->ignore_old_scan_res = 0;
8939 }
8940 
8941 
wpas_ctrl_radio_work_show(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8942 static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
8943 				     char *buf, size_t buflen)
8944 {
8945 	struct wpa_radio_work *work;
8946 	char *pos, *end;
8947 	struct os_reltime now, diff;
8948 
8949 	pos = buf;
8950 	end = buf + buflen;
8951 
8952 	os_get_reltime(&now);
8953 
8954 	dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
8955 	{
8956 		int ret;
8957 
8958 		os_reltime_sub(&now, &work->time, &diff);
8959 		ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
8960 				  work->type, work->wpa_s->ifname, work->freq,
8961 				  work->started, diff.sec, diff.usec);
8962 		if (os_snprintf_error(end - pos, ret))
8963 			break;
8964 		pos += ret;
8965 	}
8966 
8967 	return pos - buf;
8968 }
8969 
8970 
wpas_ctrl_radio_work_timeout(void * eloop_ctx,void * timeout_ctx)8971 static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
8972 {
8973 	struct wpa_radio_work *work = eloop_ctx;
8974 	struct wpa_external_work *ework = work->ctx;
8975 
8976 	wpa_dbg(work->wpa_s, MSG_DEBUG,
8977 		"Timing out external radio work %u (%s)",
8978 		ework->id, work->type);
8979 	wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
8980 	work->wpa_s->ext_work_in_progress = 0;
8981 	radio_work_done(work);
8982 	os_free(ework);
8983 }
8984 
8985 
wpas_ctrl_radio_work_cb(struct wpa_radio_work * work,int deinit)8986 static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
8987 {
8988 	struct wpa_external_work *ework = work->ctx;
8989 
8990 	if (deinit) {
8991 		if (work->started)
8992 			eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
8993 					     work, NULL);
8994 
8995 		/*
8996 		 * work->type points to a buffer in ework, so need to replace
8997 		 * that here with a fixed string to avoid use of freed memory
8998 		 * in debug prints.
8999 		 */
9000 		work->type = "freed-ext-work";
9001 		work->ctx = NULL;
9002 		os_free(ework);
9003 		return;
9004 	}
9005 
9006 	wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
9007 		ework->id, ework->type);
9008 	wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
9009 	work->wpa_s->ext_work_in_progress = 1;
9010 	if (!ework->timeout)
9011 		ework->timeout = 10;
9012 	eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
9013 			       work, NULL);
9014 }
9015 
9016 
wpas_ctrl_radio_work_add(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)9017 static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
9018 				    char *buf, size_t buflen)
9019 {
9020 	struct wpa_external_work *ework;
9021 	char *pos, *pos2;
9022 	size_t type_len;
9023 	int ret;
9024 	unsigned int freq = 0;
9025 
9026 	/* format: <name> [freq=<MHz>] [timeout=<seconds>] */
9027 
9028 	ework = os_zalloc(sizeof(*ework));
9029 	if (ework == NULL)
9030 		return -1;
9031 
9032 	pos = os_strchr(cmd, ' ');
9033 	if (pos) {
9034 		type_len = pos - cmd;
9035 		pos++;
9036 
9037 		pos2 = os_strstr(pos, "freq=");
9038 		if (pos2)
9039 			freq = atoi(pos2 + 5);
9040 
9041 		pos2 = os_strstr(pos, "timeout=");
9042 		if (pos2)
9043 			ework->timeout = atoi(pos2 + 8);
9044 	} else {
9045 		type_len = os_strlen(cmd);
9046 	}
9047 	if (4 + type_len >= sizeof(ework->type))
9048 		type_len = sizeof(ework->type) - 4 - 1;
9049 	os_strlcpy(ework->type, "ext:", sizeof(ework->type));
9050 	os_memcpy(ework->type + 4, cmd, type_len);
9051 	ework->type[4 + type_len] = '\0';
9052 
9053 	wpa_s->ext_work_id++;
9054 	if (wpa_s->ext_work_id == 0)
9055 		wpa_s->ext_work_id++;
9056 	ework->id = wpa_s->ext_work_id;
9057 
9058 	if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
9059 			   ework) < 0) {
9060 		os_free(ework);
9061 		return -1;
9062 	}
9063 
9064 	ret = os_snprintf(buf, buflen, "%u", ework->id);
9065 	if (os_snprintf_error(buflen, ret))
9066 		return -1;
9067 	return ret;
9068 }
9069 
9070 
wpas_ctrl_radio_work_done(struct wpa_supplicant * wpa_s,char * cmd)9071 static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
9072 {
9073 	struct wpa_radio_work *work;
9074 	unsigned int id = atoi(cmd);
9075 
9076 	dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
9077 	{
9078 		struct wpa_external_work *ework;
9079 
9080 		if (os_strncmp(work->type, "ext:", 4) != 0)
9081 			continue;
9082 		ework = work->ctx;
9083 		if (id && ework->id != id)
9084 			continue;
9085 		wpa_dbg(wpa_s, MSG_DEBUG,
9086 			"Completed external radio work %u (%s)",
9087 			ework->id, ework->type);
9088 		eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
9089 		wpa_s->ext_work_in_progress = 0;
9090 		radio_work_done(work);
9091 		os_free(ework);
9092 		return 3; /* "OK\n" */
9093 	}
9094 
9095 	return -1;
9096 }
9097 
9098 
wpas_ctrl_radio_work(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)9099 static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
9100 				char *buf, size_t buflen)
9101 {
9102 	if (os_strcmp(cmd, "show") == 0)
9103 		return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
9104 	if (os_strncmp(cmd, "add ", 4) == 0)
9105 		return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
9106 	if (os_strncmp(cmd, "done ", 5) == 0)
9107 		return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
9108 	return -1;
9109 }
9110 
9111 
wpas_ctrl_radio_work_flush(struct wpa_supplicant * wpa_s)9112 void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
9113 {
9114 	struct wpa_radio_work *work, *tmp;
9115 
9116 	if (!wpa_s || !wpa_s->radio)
9117 		return;
9118 
9119 	dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
9120 			      struct wpa_radio_work, list) {
9121 		struct wpa_external_work *ework;
9122 
9123 		if (os_strncmp(work->type, "ext:", 4) != 0)
9124 			continue;
9125 		ework = work->ctx;
9126 		wpa_dbg(wpa_s, MSG_DEBUG,
9127 			"Flushing%s external radio work %u (%s)",
9128 			work->started ? " started" : "", ework->id,
9129 			ework->type);
9130 		if (work->started)
9131 			eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
9132 					     work, NULL);
9133 		radio_work_done(work);
9134 		os_free(ework);
9135 	}
9136 }
9137 
9138 
wpas_ctrl_eapol_response(void * eloop_ctx,void * timeout_ctx)9139 static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
9140 {
9141 	struct wpa_supplicant *wpa_s = eloop_ctx;
9142 	eapol_sm_notify_ctrl_response(wpa_s->eapol);
9143 }
9144 
9145 
scan_id_list_parse(struct wpa_supplicant * wpa_s,const char * value,unsigned int * scan_id_count,int scan_id[])9146 static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
9147 			      unsigned int *scan_id_count, int scan_id[])
9148 {
9149 	const char *pos = value;
9150 
9151 	while (pos) {
9152 		if (*pos == ' ' || *pos == '\0')
9153 			break;
9154 		if (*scan_id_count == MAX_SCAN_ID)
9155 			return -1;
9156 		scan_id[(*scan_id_count)++] = atoi(pos);
9157 		pos = os_strchr(pos, ',');
9158 		if (pos)
9159 			pos++;
9160 	}
9161 
9162 	return 0;
9163 }
9164 
9165 
wpas_ctrl_scan(struct wpa_supplicant * wpa_s,char * params,char * reply,int reply_size,int * reply_len)9166 static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
9167 			   char *reply, int reply_size, int *reply_len)
9168 {
9169 	char *pos;
9170 	unsigned int manual_scan_passive = 0;
9171 	unsigned int manual_scan_use_id = 0;
9172 	unsigned int manual_scan_only_new = 0;
9173 	unsigned int scan_only = 0;
9174 	unsigned int scan_id_count = 0;
9175 	unsigned int manual_non_coloc_6ghz = 0;
9176 	int scan_id[MAX_SCAN_ID];
9177 	void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
9178 				 struct wpa_scan_results *scan_res);
9179 	int *manual_scan_freqs = NULL;
9180 	struct wpa_ssid_value *ssid = NULL, *ns;
9181 	unsigned int ssid_count = 0;
9182 
9183 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
9184 		*reply_len = -1;
9185 		return;
9186 	}
9187 
9188 	if (radio_work_pending(wpa_s, "scan")) {
9189 		wpa_printf(MSG_DEBUG,
9190 			   "Pending scan scheduled - reject new request");
9191 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
9192 		return;
9193 	}
9194 
9195 #ifdef CONFIG_INTERWORKING
9196 	if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
9197 		wpa_printf(MSG_DEBUG,
9198 			   "Interworking select in progress - reject new scan");
9199 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
9200 		return;
9201 	}
9202 #endif /* CONFIG_INTERWORKING */
9203 
9204 	if (params) {
9205 		if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
9206 			scan_only = 1;
9207 
9208 		pos = os_strstr(params, "freq=");
9209 		if (pos) {
9210 			manual_scan_freqs = freq_range_to_channel_list(wpa_s,
9211 								       pos + 5);
9212 			if (manual_scan_freqs == NULL) {
9213 				*reply_len = -1;
9214 				goto done;
9215 			}
9216 		}
9217 
9218 		pos = os_strstr(params, "passive=");
9219 		if (pos)
9220 			manual_scan_passive = !!atoi(pos + 8);
9221 
9222 		pos = os_strstr(params, "use_id=");
9223 		if (pos)
9224 			manual_scan_use_id = atoi(pos + 7);
9225 
9226 		pos = os_strstr(params, "only_new=1");
9227 		if (pos)
9228 			manual_scan_only_new = 1;
9229 
9230 		pos = os_strstr(params, "scan_id=");
9231 		if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
9232 					      scan_id) < 0) {
9233 			*reply_len = -1;
9234 			goto done;
9235 		}
9236 
9237 		pos = os_strstr(params, "bssid=");
9238 		if (pos) {
9239 			u8 bssid[ETH_ALEN];
9240 
9241 			pos += 6;
9242 			if (hwaddr_aton(pos, bssid)) {
9243 				wpa_printf(MSG_ERROR, "Invalid BSSID %s", pos);
9244 				*reply_len = -1;
9245 				goto done;
9246 			}
9247 			os_memcpy(wpa_s->next_scan_bssid, bssid, ETH_ALEN);
9248 
9249 			wpa_s->next_scan_bssid_wildcard_ssid =
9250 				os_strstr(params, "wildcard_ssid=1") != NULL;
9251 		}
9252 
9253 		pos = os_strstr(params, "non_coloc_6ghz=");
9254 		if (pos)
9255 			manual_non_coloc_6ghz = !!atoi(pos + 15);
9256 
9257 		pos = params;
9258 		while (pos && *pos != '\0') {
9259 			if (os_strncmp(pos, "ssid ", 5) == 0) {
9260 				char *end;
9261 
9262 				pos += 5;
9263 				end = pos;
9264 				while (*end) {
9265 					if (*end == '\0' || *end == ' ')
9266 						break;
9267 					end++;
9268 				}
9269 
9270 				ns = os_realloc_array(
9271 					ssid, ssid_count + 1,
9272 					sizeof(struct wpa_ssid_value));
9273 				if (ns == NULL) {
9274 					*reply_len = -1;
9275 					goto done;
9276 				}
9277 				ssid = ns;
9278 
9279 				if ((end - pos) & 0x01 ||
9280 				    end - pos > 2 * SSID_MAX_LEN ||
9281 				    hexstr2bin(pos, ssid[ssid_count].ssid,
9282 					       (end - pos) / 2) < 0) {
9283 					wpa_printf(MSG_DEBUG,
9284 						   "Invalid SSID value '%s'",
9285 						   pos);
9286 					*reply_len = -1;
9287 					goto done;
9288 				}
9289 				ssid[ssid_count].ssid_len = (end - pos) / 2;
9290 				wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
9291 						  ssid[ssid_count].ssid,
9292 						  ssid[ssid_count].ssid_len);
9293 				ssid_count++;
9294 				pos = end;
9295 			}
9296 
9297 			pos = os_strchr(pos, ' ');
9298 			if (pos)
9299 				pos++;
9300 		}
9301 	}
9302 
9303 	wpa_s->num_ssids_from_scan_req = ssid_count;
9304 	os_free(wpa_s->ssids_from_scan_req);
9305 	if (ssid_count) {
9306 		wpa_s->ssids_from_scan_req = ssid;
9307 		ssid = NULL;
9308 	} else {
9309 		wpa_s->ssids_from_scan_req = NULL;
9310 	}
9311 
9312 	if (scan_only)
9313 		scan_res_handler = scan_only_handler;
9314 	else if (wpa_s->scan_res_handler == scan_only_handler)
9315 		scan_res_handler = NULL;
9316 	else
9317 		scan_res_handler = wpa_s->scan_res_handler;
9318 
9319 	if (!wpa_s->sched_scanning && !wpa_s->scanning &&
9320 	    ((wpa_s->wpa_state <= WPA_SCANNING) ||
9321 	     (wpa_s->wpa_state == WPA_COMPLETED))) {
9322 		wpa_s->manual_scan_passive = manual_scan_passive;
9323 		wpa_s->manual_scan_use_id = manual_scan_use_id;
9324 		wpa_s->manual_scan_only_new = manual_scan_only_new;
9325 		wpa_s->scan_id_count = scan_id_count;
9326 		wpa_s->manual_non_coloc_6ghz = manual_non_coloc_6ghz;
9327 		os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
9328 		wpa_s->scan_res_handler = scan_res_handler;
9329 		os_free(wpa_s->manual_scan_freqs);
9330 		wpa_s->manual_scan_freqs = manual_scan_freqs;
9331 		manual_scan_freqs = NULL;
9332 
9333 		wpa_s->normal_scans = 0;
9334 		wpa_s->scan_req = MANUAL_SCAN_REQ;
9335 		wpa_s->after_wps = 0;
9336 		wpa_s->known_wps_freq = 0;
9337 		wpa_supplicant_req_scan(wpa_s, 0, 0);
9338 		if (wpa_s->manual_scan_use_id) {
9339 			wpa_s->manual_scan_id++;
9340 			wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
9341 				wpa_s->manual_scan_id);
9342 			*reply_len = os_snprintf(reply, reply_size, "%u\n",
9343 						 wpa_s->manual_scan_id);
9344 		}
9345 	} else if (wpa_s->sched_scanning) {
9346 		wpa_s->manual_scan_passive = manual_scan_passive;
9347 		wpa_s->manual_scan_use_id = manual_scan_use_id;
9348 		wpa_s->manual_scan_only_new = manual_scan_only_new;
9349 		wpa_s->scan_id_count = scan_id_count;
9350 		wpa_s->manual_non_coloc_6ghz = manual_non_coloc_6ghz;
9351 		os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
9352 		wpa_s->scan_res_handler = scan_res_handler;
9353 		os_free(wpa_s->manual_scan_freqs);
9354 		wpa_s->manual_scan_freqs = manual_scan_freqs;
9355 		manual_scan_freqs = NULL;
9356 
9357 		wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
9358 		wpa_supplicant_cancel_sched_scan(wpa_s);
9359 		wpa_s->scan_req = MANUAL_SCAN_REQ;
9360 		wpa_supplicant_req_scan(wpa_s, 0, 0);
9361 		if (wpa_s->manual_scan_use_id) {
9362 			wpa_s->manual_scan_id++;
9363 			*reply_len = os_snprintf(reply, reply_size, "%u\n",
9364 						 wpa_s->manual_scan_id);
9365 			wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
9366 				wpa_s->manual_scan_id);
9367 		}
9368 	} else {
9369 		wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
9370 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
9371 	}
9372 
9373 done:
9374 	os_free(manual_scan_freqs);
9375 	os_free(ssid);
9376 }
9377 
9378 
9379 #ifdef CONFIG_TESTING_OPTIONS
9380 
wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant * wpa_s,unsigned int freq,const u8 * dst,const u8 * src,const u8 * bssid,const u8 * data,size_t data_len,enum offchannel_send_action_result result)9381 static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
9382 				       unsigned int freq, const u8 *dst,
9383 				       const u8 *src, const u8 *bssid,
9384 				       const u8 *data, size_t data_len,
9385 				       enum offchannel_send_action_result
9386 				       result)
9387 {
9388 	wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
9389 		" src=" MACSTR " bssid=" MACSTR " result=%s",
9390 		freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
9391 		result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
9392 		"SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
9393 			     "NO_ACK" : "FAILED"));
9394 }
9395 
9396 
wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant * wpa_s,char * cmd)9397 static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
9398 {
9399 	char *pos, *param;
9400 	size_t len;
9401 	u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
9402 	int res, used;
9403 	int freq = 0, no_cck = 0, wait_time = 0;
9404 
9405 	/* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
9406 	 *    <action=Action frame payload> */
9407 
9408 	wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
9409 
9410 	pos = cmd;
9411 	used = hwaddr_aton2(pos, da);
9412 	if (used < 0)
9413 		return -1;
9414 	pos += used;
9415 	while (*pos == ' ')
9416 		pos++;
9417 	used = hwaddr_aton2(pos, bssid);
9418 	if (used < 0)
9419 		return -1;
9420 	pos += used;
9421 
9422 	param = os_strstr(pos, " freq=");
9423 	if (param) {
9424 		param += 6;
9425 		freq = atoi(param);
9426 	}
9427 
9428 	param = os_strstr(pos, " no_cck=");
9429 	if (param) {
9430 		param += 8;
9431 		no_cck = atoi(param);
9432 	}
9433 
9434 	param = os_strstr(pos, " wait_time=");
9435 	if (param) {
9436 		param += 11;
9437 		wait_time = atoi(param);
9438 	}
9439 
9440 	param = os_strstr(pos, " action=");
9441 	if (param == NULL)
9442 		return -1;
9443 	param += 8;
9444 
9445 	len = os_strlen(param);
9446 	if (len & 1)
9447 		return -1;
9448 	len /= 2;
9449 
9450 	buf = os_malloc(len);
9451 	if (buf == NULL)
9452 		return -1;
9453 
9454 	if (hexstr2bin(param, buf, len) < 0) {
9455 		os_free(buf);
9456 		return -1;
9457 	}
9458 
9459 	res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
9460 				     buf, len, wait_time,
9461 				     wpas_ctrl_iface_mgmt_tx_cb, no_cck);
9462 	os_free(buf);
9463 	return res;
9464 }
9465 
9466 
wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant * wpa_s)9467 static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
9468 {
9469 	wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
9470 	offchannel_send_action_done(wpa_s);
9471 }
9472 
9473 
wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant * wpa_s,char * cmd)9474 static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
9475 					   char *cmd)
9476 {
9477 	char *pos, *param;
9478 	size_t len;
9479 	u8 *buf;
9480 	int freq = 0, datarate = 0, ssi_signal = 0;
9481 	union wpa_event_data event;
9482 
9483 	if (!wpa_s->ext_mgmt_frame_handling)
9484 		return -1;
9485 
9486 	/* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
9487 
9488 	wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
9489 
9490 	pos = cmd;
9491 	param = os_strstr(pos, "freq=");
9492 	if (param) {
9493 		param += 5;
9494 		freq = atoi(param);
9495 	}
9496 
9497 	param = os_strstr(pos, " datarate=");
9498 	if (param) {
9499 		param += 10;
9500 		datarate = atoi(param);
9501 	}
9502 
9503 	param = os_strstr(pos, " ssi_signal=");
9504 	if (param) {
9505 		param += 12;
9506 		ssi_signal = atoi(param);
9507 	}
9508 
9509 	param = os_strstr(pos, " frame=");
9510 	if (param == NULL)
9511 		return -1;
9512 	param += 7;
9513 
9514 	len = os_strlen(param);
9515 	if (len & 1)
9516 		return -1;
9517 	len /= 2;
9518 
9519 	buf = os_malloc(len);
9520 	if (buf == NULL)
9521 		return -1;
9522 
9523 	if (hexstr2bin(param, buf, len) < 0) {
9524 		os_free(buf);
9525 		return -1;
9526 	}
9527 
9528 	os_memset(&event, 0, sizeof(event));
9529 	event.rx_mgmt.freq = freq;
9530 	event.rx_mgmt.frame = buf;
9531 	event.rx_mgmt.frame_len = len;
9532 	event.rx_mgmt.ssi_signal = ssi_signal;
9533 	event.rx_mgmt.datarate = datarate;
9534 	wpa_s->ext_mgmt_frame_handling = 0;
9535 	wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
9536 	wpa_s->ext_mgmt_frame_handling = 1;
9537 
9538 	os_free(buf);
9539 
9540 	return 0;
9541 }
9542 
9543 
wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant * wpa_s,char * param)9544 static int wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant *wpa_s,
9545 					   char *param)
9546 {
9547 	struct wpa_scan_res *res;
9548 	struct os_reltime now;
9549 	char *pos, *end;
9550 	int ret = -1;
9551 
9552 	if (!param)
9553 		return -1;
9554 
9555 	if (os_strcmp(param, "START") == 0) {
9556 		wpa_bss_update_start(wpa_s);
9557 		return 0;
9558 	}
9559 
9560 	if (os_strcmp(param, "END") == 0) {
9561 		wpa_bss_update_end(wpa_s, NULL, 1);
9562 		return 0;
9563 	}
9564 
9565 	if (os_strncmp(param, "BSS ", 4) != 0)
9566 		return -1;
9567 	param += 3;
9568 
9569 	res = os_zalloc(sizeof(*res) + os_strlen(param) / 2);
9570 	if (!res)
9571 		return -1;
9572 
9573 	pos = os_strstr(param, " flags=");
9574 	if (pos)
9575 		res->flags = strtol(pos + 7, NULL, 16);
9576 
9577 	pos = os_strstr(param, " bssid=");
9578 	if (pos && hwaddr_aton(pos + 7, res->bssid))
9579 		goto fail;
9580 
9581 	pos = os_strstr(param, " freq=");
9582 	if (pos)
9583 		res->freq = atoi(pos + 6);
9584 
9585 	pos = os_strstr(param, " beacon_int=");
9586 	if (pos)
9587 		res->beacon_int = atoi(pos + 12);
9588 
9589 	pos = os_strstr(param, " caps=");
9590 	if (pos)
9591 		res->caps = strtol(pos + 6, NULL, 16);
9592 
9593 	pos = os_strstr(param, " qual=");
9594 	if (pos)
9595 		res->qual = atoi(pos + 6);
9596 
9597 	pos = os_strstr(param, " noise=");
9598 	if (pos)
9599 		res->noise = atoi(pos + 7);
9600 
9601 	pos = os_strstr(param, " level=");
9602 	if (pos)
9603 		res->level = atoi(pos + 7);
9604 
9605 	pos = os_strstr(param, " tsf=");
9606 	if (pos)
9607 		res->tsf = strtoll(pos + 5, NULL, 16);
9608 
9609 	pos = os_strstr(param, " age=");
9610 	if (pos)
9611 		res->age = atoi(pos + 5);
9612 
9613 	pos = os_strstr(param, " est_throughput=");
9614 	if (pos)
9615 		res->est_throughput = atoi(pos + 16);
9616 
9617 	pos = os_strstr(param, " snr=");
9618 	if (pos)
9619 		res->snr = atoi(pos + 5);
9620 
9621 	pos = os_strstr(param, " parent_tsf=");
9622 	if (pos)
9623 		res->parent_tsf = strtoll(pos + 7, NULL, 16);
9624 
9625 	pos = os_strstr(param, " tsf_bssid=");
9626 	if (pos && hwaddr_aton(pos + 11, res->tsf_bssid))
9627 		goto fail;
9628 
9629 	pos = os_strstr(param, " ie=");
9630 	if (pos) {
9631 		pos += 4;
9632 		end = os_strchr(pos, ' ');
9633 		if (!end)
9634 			end = pos + os_strlen(pos);
9635 		res->ie_len = (end - pos) / 2;
9636 		if (hexstr2bin(pos, (u8 *) (res + 1), res->ie_len))
9637 			goto fail;
9638 	}
9639 
9640 	pos = os_strstr(param, " beacon_ie=");
9641 	if (pos) {
9642 		pos += 11;
9643 		end = os_strchr(pos, ' ');
9644 		if (!end)
9645 			end = pos + os_strlen(pos);
9646 		res->beacon_ie_len = (end - pos) / 2;
9647 		if (hexstr2bin(pos, ((u8 *) (res + 1)) + res->ie_len,
9648 			       res->beacon_ie_len))
9649 			goto fail;
9650 	}
9651 
9652 	os_get_reltime(&now);
9653 	wpa_bss_update_scan_res(wpa_s, res, &now);
9654 	ret = 0;
9655 fail:
9656 	os_free(res);
9657 
9658 	return ret;
9659 }
9660 
9661 
wpas_ctrl_iface_driver_event_assoc(struct wpa_supplicant * wpa_s,char * param)9662 static int wpas_ctrl_iface_driver_event_assoc(struct wpa_supplicant *wpa_s,
9663 					      char *param)
9664 {
9665 	union wpa_event_data event;
9666 	struct assoc_info *ai;
9667 	char *ctx = NULL;
9668 	int ret = -1;
9669 	struct wpabuf *req_ies = NULL;
9670 	struct wpabuf *resp_ies = NULL;
9671 	struct wpabuf *resp_frame = NULL;
9672 	struct wpabuf *beacon_ies = NULL;
9673 	struct wpabuf *key_replay_ctr = NULL;
9674 	struct wpabuf *ptk_kck = NULL;
9675 	struct wpabuf *ptk_kek = NULL;
9676 	struct wpabuf *fils_pmk = NULL;
9677 	char *str, *pos;
9678 	u8 addr[ETH_ALEN];
9679 	u8 fils_pmkid[PMKID_LEN];
9680 
9681 	os_memset(&event, 0, sizeof(event));
9682 	ai = &event.assoc_info;
9683 
9684 	while ((str = str_token(param, " ", &ctx))) {
9685 		pos = os_strchr(str, '=');
9686 		if (!pos)
9687 			goto fail;
9688 		*pos++ = '\0';
9689 
9690 		if (os_strcmp(str, "reassoc") == 0) {
9691 			ai->reassoc = atoi(pos);
9692 		} else if (os_strcmp(str, "req_ies") == 0) {
9693 			wpabuf_free(req_ies);
9694 			req_ies = wpabuf_parse_bin(pos);
9695 			if (!req_ies)
9696 				goto fail;
9697 			ai->req_ies = wpabuf_head(req_ies);
9698 			ai->req_ies_len = wpabuf_len(req_ies);
9699 		} else if (os_strcmp(str, "resp_ies") == 0) {
9700 			wpabuf_free(resp_ies);
9701 			resp_ies = wpabuf_parse_bin(pos);
9702 			if (!resp_ies)
9703 				goto fail;
9704 			ai->resp_ies = wpabuf_head(resp_ies);
9705 			ai->resp_ies_len = wpabuf_len(resp_ies);
9706 		} else if (os_strcmp(str, "resp_frame") == 0) {
9707 			wpabuf_free(resp_frame);
9708 			resp_frame = wpabuf_parse_bin(pos);
9709 			if (!resp_frame)
9710 				goto fail;
9711 			ai->resp_frame = wpabuf_head(resp_frame);
9712 			ai->resp_frame_len = wpabuf_len(resp_frame);
9713 		} else if (os_strcmp(str, "beacon_ies") == 0) {
9714 			wpabuf_free(beacon_ies);
9715 			beacon_ies = wpabuf_parse_bin(pos);
9716 			if (!beacon_ies)
9717 				goto fail;
9718 			ai->beacon_ies = wpabuf_head(beacon_ies);
9719 			ai->beacon_ies_len = wpabuf_len(beacon_ies);
9720 		} else if (os_strcmp(str, "freq") == 0) {
9721 			ai->freq = atoi(pos);
9722 		} else if (os_strcmp(str, "wmm::info_bitmap") == 0) {
9723 			ai->wmm_params.info_bitmap = atoi(pos);
9724 		} else if (os_strcmp(str, "wmm::uapsd_queues") == 0) {
9725 			ai->wmm_params.uapsd_queues = atoi(pos);
9726 		} else if (os_strcmp(str, "addr") == 0) {
9727 			if (hwaddr_aton(pos, addr))
9728 				goto fail;
9729 			ai->addr = addr;
9730 		} else if (os_strcmp(str, "authorized") == 0) {
9731 			ai->authorized = atoi(pos);
9732 		} else if (os_strcmp(str, "key_replay_ctr") == 0) {
9733 			wpabuf_free(key_replay_ctr);
9734 			key_replay_ctr = wpabuf_parse_bin(pos);
9735 			if (!key_replay_ctr)
9736 				goto fail;
9737 			ai->key_replay_ctr = wpabuf_head(key_replay_ctr);
9738 			ai->key_replay_ctr_len = wpabuf_len(key_replay_ctr);
9739 		} else if (os_strcmp(str, "ptk_kck") == 0) {
9740 			wpabuf_free(ptk_kck);
9741 			ptk_kck = wpabuf_parse_bin(pos);
9742 			if (!ptk_kck)
9743 				goto fail;
9744 			ai->ptk_kck = wpabuf_head(ptk_kck);
9745 			ai->ptk_kck_len = wpabuf_len(ptk_kck);
9746 		} else if (os_strcmp(str, "ptk_kek") == 0) {
9747 			wpabuf_free(ptk_kek);
9748 			ptk_kek = wpabuf_parse_bin(pos);
9749 			if (!ptk_kek)
9750 				goto fail;
9751 			ai->ptk_kek = wpabuf_head(ptk_kek);
9752 			ai->ptk_kek_len = wpabuf_len(ptk_kek);
9753 		} else if (os_strcmp(str, "subnet_status") == 0) {
9754 			ai->subnet_status = atoi(pos);
9755 		} else if (os_strcmp(str, "fils_erp_next_seq_num") == 0) {
9756 			ai->fils_erp_next_seq_num = atoi(pos);
9757 		} else if (os_strcmp(str, "fils_pmk") == 0) {
9758 			wpabuf_free(fils_pmk);
9759 			fils_pmk = wpabuf_parse_bin(pos);
9760 			if (!fils_pmk)
9761 				goto fail;
9762 			ai->fils_pmk = wpabuf_head(fils_pmk);
9763 			ai->fils_pmk_len = wpabuf_len(fils_pmk);
9764 		} else if (os_strcmp(str, "fils_pmkid") == 0) {
9765 			if (hexstr2bin(pos, fils_pmkid, PMKID_LEN) < 0)
9766 				goto fail;
9767 			ai->fils_pmkid = fils_pmkid;
9768 		} else {
9769 			goto fail;
9770 		}
9771 	}
9772 
9773 	wpa_supplicant_event(wpa_s, EVENT_ASSOC, &event);
9774 	ret = 0;
9775 fail:
9776 	wpabuf_free(req_ies);
9777 	wpabuf_free(resp_ies);
9778 	wpabuf_free(resp_frame);
9779 	wpabuf_free(beacon_ies);
9780 	wpabuf_free(key_replay_ctr);
9781 	wpabuf_free(ptk_kck);
9782 	wpabuf_free(ptk_kek);
9783 	wpabuf_free(fils_pmk);
9784 	return ret;
9785 }
9786 
9787 
wpas_ctrl_iface_driver_event(struct wpa_supplicant * wpa_s,char * cmd)9788 static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
9789 {
9790 	char *pos, *param;
9791 	union wpa_event_data event;
9792 	enum wpa_event_type ev;
9793 
9794 	/* <event name> [parameters..] */
9795 
9796 	wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
9797 
9798 	pos = cmd;
9799 	param = os_strchr(pos, ' ');
9800 	if (param)
9801 		*param++ = '\0';
9802 
9803 	os_memset(&event, 0, sizeof(event));
9804 
9805 	if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
9806 		ev = EVENT_INTERFACE_ENABLED;
9807 	} else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
9808 		ev = EVENT_INTERFACE_DISABLED;
9809 	} else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
9810 		ev = EVENT_AVOID_FREQUENCIES;
9811 		if (param == NULL)
9812 			param = "";
9813 		if (freq_range_list_parse(&event.freq_range, param) < 0)
9814 			return -1;
9815 		wpa_supplicant_event(wpa_s, ev, &event);
9816 		os_free(event.freq_range.range);
9817 		return 0;
9818 	} else if (os_strcmp(cmd, "SCAN_RES") == 0) {
9819 		return wpas_ctrl_iface_driver_scan_res(wpa_s, param);
9820 	} else if (os_strcmp(cmd, "ASSOC") == 0) {
9821 		return wpas_ctrl_iface_driver_event_assoc(wpa_s, param);
9822 	} else {
9823 		wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
9824 			cmd);
9825 		return -1;
9826 	}
9827 
9828 	wpa_supplicant_event(wpa_s, ev, &event);
9829 
9830 	return 0;
9831 }
9832 
9833 
wpas_ctrl_iface_eapol_rx(struct wpa_supplicant * wpa_s,char * cmd)9834 static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
9835 {
9836 	char *pos;
9837 	u8 src[ETH_ALEN], *buf;
9838 	int used;
9839 	size_t len;
9840 
9841 	wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
9842 
9843 	pos = cmd;
9844 	used = hwaddr_aton2(pos, src);
9845 	if (used < 0)
9846 		return -1;
9847 	pos += used;
9848 	while (*pos == ' ')
9849 		pos++;
9850 
9851 	len = os_strlen(pos);
9852 	if (len & 1)
9853 		return -1;
9854 	len /= 2;
9855 
9856 	buf = os_malloc(len);
9857 	if (buf == NULL)
9858 		return -1;
9859 
9860 	if (hexstr2bin(pos, buf, len) < 0) {
9861 		os_free(buf);
9862 		return -1;
9863 	}
9864 
9865 	wpa_supplicant_rx_eapol(wpa_s, src, buf, len, FRAME_ENCRYPTION_UNKNOWN);
9866 	os_free(buf);
9867 
9868 	return 0;
9869 }
9870 
9871 
wpas_ctrl_iface_eapol_tx(struct wpa_supplicant * wpa_s,char * cmd)9872 static int wpas_ctrl_iface_eapol_tx(struct wpa_supplicant *wpa_s, char *cmd)
9873 {
9874 	char *pos;
9875 	u8 dst[ETH_ALEN], *buf;
9876 	int used, ret;
9877 	size_t len;
9878 	unsigned int prev;
9879 
9880 	wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
9881 
9882 	pos = cmd;
9883 	used = hwaddr_aton2(pos, dst);
9884 	if (used < 0)
9885 		return -1;
9886 	pos += used;
9887 	while (*pos == ' ')
9888 		pos++;
9889 
9890 	len = os_strlen(pos);
9891 	if (len & 1)
9892 		return -1;
9893 	len /= 2;
9894 
9895 	buf = os_malloc(len);
9896 	if (!buf || hexstr2bin(pos, buf, len) < 0) {
9897 		os_free(buf);
9898 		return -1;
9899 	}
9900 
9901 	prev = wpa_s->ext_eapol_frame_io;
9902 	wpa_s->ext_eapol_frame_io = 0;
9903 	ret = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, buf, len);
9904 	wpa_s->ext_eapol_frame_io = prev;
9905 	os_free(buf);
9906 
9907 	return ret;
9908 }
9909 
9910 
ipv4_hdr_checksum(const void * buf,size_t len)9911 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
9912 {
9913 	size_t i;
9914 	u32 sum = 0;
9915 	const u16 *pos = buf;
9916 
9917 	for (i = 0; i < len / 2; i++)
9918 		sum += *pos++;
9919 
9920 	while (sum >> 16)
9921 		sum = (sum & 0xffff) + (sum >> 16);
9922 
9923 	return sum ^ 0xffff;
9924 }
9925 
9926 
9927 #define HWSIM_PACKETLEN 1500
9928 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
9929 
wpas_data_test_rx(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)9930 static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
9931 			      size_t len)
9932 {
9933 	struct wpa_supplicant *wpa_s = ctx;
9934 	const struct ether_header *eth;
9935 	struct ip ip;
9936 	const u8 *pos;
9937 	unsigned int i;
9938 	char extra[30];
9939 
9940 	if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
9941 		wpa_printf(MSG_DEBUG,
9942 			   "test data: RX - ignore unexpected length %d",
9943 			   (int) len);
9944 		return;
9945 	}
9946 
9947 	eth = (const struct ether_header *) buf;
9948 	os_memcpy(&ip, eth + 1, sizeof(ip));
9949 	pos = &buf[sizeof(*eth) + sizeof(ip)];
9950 
9951 	if (ip.ip_hl != 5 || ip.ip_v != 4 || ntohs(ip.ip_len) > HWSIM_IP_LEN) {
9952 		wpa_printf(MSG_DEBUG,
9953 			   "test data: RX - ignore unexpected IP header");
9954 		return;
9955 	}
9956 
9957 	for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
9958 		if (*pos != (u8) i) {
9959 			wpa_printf(MSG_DEBUG,
9960 				   "test data: RX - ignore mismatching payload");
9961 			return;
9962 		}
9963 		pos++;
9964 	}
9965 	extra[0] = '\0';
9966 	if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
9967 		os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
9968 	wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
9969 		MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
9970 }
9971 
9972 
wpas_ctrl_iface_data_test_config(struct wpa_supplicant * wpa_s,char * cmd)9973 static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
9974 					    char *cmd)
9975 {
9976 	int enabled = atoi(cmd);
9977 	char *pos;
9978 	const char *ifname;
9979 
9980 	if (!enabled) {
9981 		if (wpa_s->l2_test) {
9982 			l2_packet_deinit(wpa_s->l2_test);
9983 			wpa_s->l2_test = NULL;
9984 			wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
9985 		}
9986 		return 0;
9987 	}
9988 
9989 	if (wpa_s->l2_test)
9990 		return 0;
9991 
9992 	pos = os_strstr(cmd, " ifname=");
9993 	if (pos)
9994 		ifname = pos + 8;
9995 	else
9996 		ifname = wpa_s->ifname;
9997 
9998 	wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
9999 					ETHERTYPE_IP, wpas_data_test_rx,
10000 					wpa_s, 1);
10001 	if (wpa_s->l2_test == NULL)
10002 		return -1;
10003 
10004 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
10005 
10006 	return 0;
10007 }
10008 
10009 
wpas_ctrl_iface_data_test_tx(struct wpa_supplicant * wpa_s,char * cmd)10010 static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
10011 {
10012 	u8 dst[ETH_ALEN], src[ETH_ALEN];
10013 	char *pos, *pos2;
10014 	int used;
10015 	long int val;
10016 	u8 tos;
10017 	u8 buf[2 + HWSIM_PACKETLEN];
10018 	struct ether_header *eth;
10019 	struct ip *ip;
10020 	u8 *dpos;
10021 	unsigned int i;
10022 	size_t send_len = HWSIM_IP_LEN;
10023 
10024 	if (wpa_s->l2_test == NULL)
10025 		return -1;
10026 
10027 	/* format: <dst> <src> <tos> [len=<length>] */
10028 
10029 	pos = cmd;
10030 	used = hwaddr_aton2(pos, dst);
10031 	if (used < 0)
10032 		return -1;
10033 	pos += used;
10034 	while (*pos == ' ')
10035 		pos++;
10036 	used = hwaddr_aton2(pos, src);
10037 	if (used < 0)
10038 		return -1;
10039 	pos += used;
10040 
10041 	val = strtol(pos, &pos2, 0);
10042 	if (val < 0 || val > 0xff)
10043 		return -1;
10044 	tos = val;
10045 
10046 	pos = os_strstr(pos2, " len=");
10047 	if (pos) {
10048 		i = atoi(pos + 5);
10049 		if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
10050 			return -1;
10051 		send_len = i;
10052 	}
10053 
10054 	eth = (struct ether_header *) &buf[2];
10055 	os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
10056 	os_memcpy(eth->ether_shost, src, ETH_ALEN);
10057 	eth->ether_type = htons(ETHERTYPE_IP);
10058 	ip = (struct ip *) (eth + 1);
10059 	os_memset(ip, 0, sizeof(*ip));
10060 	ip->ip_hl = 5;
10061 	ip->ip_v = 4;
10062 	ip->ip_ttl = 64;
10063 	ip->ip_tos = tos;
10064 	ip->ip_len = htons(send_len);
10065 	ip->ip_p = 1;
10066 	ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
10067 	ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
10068 	ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
10069 	dpos = (u8 *) (ip + 1);
10070 	for (i = 0; i < send_len - sizeof(*ip); i++)
10071 		*dpos++ = i;
10072 
10073 	if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
10074 			   sizeof(struct ether_header) + send_len) < 0)
10075 		return -1;
10076 
10077 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
10078 		" tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
10079 
10080 	return 0;
10081 }
10082 
10083 
wpas_ctrl_iface_data_test_frame(struct wpa_supplicant * wpa_s,char * cmd)10084 static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
10085 					   char *cmd)
10086 {
10087 	u8 *buf;
10088 	struct ether_header *eth;
10089 	struct l2_packet_data *l2 = NULL;
10090 	size_t len;
10091 	u16 ethertype;
10092 	int res = -1;
10093 
10094 	len = os_strlen(cmd);
10095 	if (len & 1 || len < ETH_HLEN * 2)
10096 		return -1;
10097 	len /= 2;
10098 
10099 	buf = os_malloc(len);
10100 	if (buf == NULL)
10101 		return -1;
10102 
10103 	if (hexstr2bin(cmd, buf, len) < 0)
10104 		goto done;
10105 
10106 	eth = (struct ether_header *) buf;
10107 	ethertype = ntohs(eth->ether_type);
10108 
10109 	l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
10110 			    wpas_data_test_rx, wpa_s, 1);
10111 	if (l2 == NULL)
10112 		goto done;
10113 
10114 	res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
10115 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
10116 done:
10117 	if (l2)
10118 		l2_packet_deinit(l2);
10119 	os_free(buf);
10120 
10121 	return res < 0 ? -1 : 0;
10122 }
10123 
10124 
wpas_ctrl_test_alloc_fail(struct wpa_supplicant * wpa_s,char * cmd)10125 static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
10126 {
10127 #ifdef WPA_TRACE_BFD
10128 	char *pos;
10129 
10130 	wpa_trace_fail_after = atoi(cmd);
10131 	pos = os_strchr(cmd, ':');
10132 	if (pos) {
10133 		pos++;
10134 		os_strlcpy(wpa_trace_fail_func, pos,
10135 			   sizeof(wpa_trace_fail_func));
10136 	} else {
10137 		wpa_trace_fail_after = 0;
10138 	}
10139 	return 0;
10140 #else /* WPA_TRACE_BFD */
10141 	return -1;
10142 #endif /* WPA_TRACE_BFD */
10143 }
10144 
10145 
wpas_ctrl_get_alloc_fail(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)10146 static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
10147 				    char *buf, size_t buflen)
10148 {
10149 #ifdef WPA_TRACE_BFD
10150 	return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
10151 			   wpa_trace_fail_func);
10152 #else /* WPA_TRACE_BFD */
10153 	return -1;
10154 #endif /* WPA_TRACE_BFD */
10155 }
10156 
10157 
wpas_ctrl_test_fail(struct wpa_supplicant * wpa_s,char * cmd)10158 static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd)
10159 {
10160 #ifdef WPA_TRACE_BFD
10161 	char *pos;
10162 
10163 	wpa_trace_test_fail_after = atoi(cmd);
10164 	pos = os_strchr(cmd, ':');
10165 	if (pos) {
10166 		pos++;
10167 		os_strlcpy(wpa_trace_test_fail_func, pos,
10168 			   sizeof(wpa_trace_test_fail_func));
10169 	} else {
10170 		wpa_trace_test_fail_after = 0;
10171 	}
10172 	return 0;
10173 #else /* WPA_TRACE_BFD */
10174 	return -1;
10175 #endif /* WPA_TRACE_BFD */
10176 }
10177 
10178 
wpas_ctrl_get_fail(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)10179 static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s,
10180 				    char *buf, size_t buflen)
10181 {
10182 #ifdef WPA_TRACE_BFD
10183 	return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
10184 			   wpa_trace_test_fail_func);
10185 #else /* WPA_TRACE_BFD */
10186 	return -1;
10187 #endif /* WPA_TRACE_BFD */
10188 }
10189 
10190 
wpas_ctrl_event_test_cb(void * eloop_ctx,void * timeout_ctx)10191 static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
10192 {
10193 	struct wpa_supplicant *wpa_s = eloop_ctx;
10194 	int i, count = (intptr_t) timeout_ctx;
10195 
10196 	wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
10197 		   count);
10198 	for (i = 0; i < count; i++) {
10199 		wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
10200 			     i + 1, count);
10201 	}
10202 }
10203 
10204 
wpas_ctrl_event_test(struct wpa_supplicant * wpa_s,const char * cmd)10205 static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
10206 {
10207 	int count;
10208 
10209 	count = atoi(cmd);
10210 	if (count <= 0)
10211 		return -1;
10212 
10213 	return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
10214 				      (void *) (intptr_t) count);
10215 }
10216 
10217 
wpas_ctrl_test_assoc_ie(struct wpa_supplicant * wpa_s,const char * cmd)10218 static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
10219 				   const char *cmd)
10220 {
10221 	struct wpabuf *buf;
10222 	size_t len;
10223 
10224 	len = os_strlen(cmd);
10225 	if (len & 1)
10226 		return -1;
10227 	len /= 2;
10228 
10229 	if (len == 0) {
10230 		buf = NULL;
10231 	} else {
10232 		buf = wpabuf_alloc(len);
10233 		if (buf == NULL)
10234 			return -1;
10235 
10236 		if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
10237 			wpabuf_free(buf);
10238 			return -1;
10239 		}
10240 	}
10241 
10242 	wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
10243 	return 0;
10244 }
10245 
10246 
wpas_ctrl_reset_pn(struct wpa_supplicant * wpa_s)10247 static int wpas_ctrl_reset_pn(struct wpa_supplicant *wpa_s)
10248 {
10249 	u8 zero[WPA_TK_MAX_LEN];
10250 
10251 	if (wpa_s->last_tk_alg == WPA_ALG_NONE)
10252 		return -1;
10253 
10254 	wpa_printf(MSG_INFO, "TESTING: Reset PN");
10255 	os_memset(zero, 0, sizeof(zero));
10256 
10257 	/* First, use a zero key to avoid any possible duplicate key avoidance
10258 	 * in the driver. */
10259 	if (wpa_drv_set_key(wpa_s, -1, wpa_s->last_tk_alg, wpa_s->last_tk_addr,
10260 			    wpa_s->last_tk_key_idx, 1, zero, 6,
10261 			    zero, wpa_s->last_tk_len,
10262 			    KEY_FLAG_PAIRWISE_RX_TX) < 0)
10263 		return -1;
10264 
10265 	/* Set the previously configured key to reset its TSC/RSC */
10266 	return wpa_drv_set_key(wpa_s, -1, wpa_s->last_tk_alg,
10267 			       wpa_s->last_tk_addr,
10268 			       wpa_s->last_tk_key_idx, 1, zero, 6,
10269 			       wpa_s->last_tk, wpa_s->last_tk_len,
10270 			       KEY_FLAG_PAIRWISE_RX_TX);
10271 }
10272 
10273 
wpas_ctrl_key_request(struct wpa_supplicant * wpa_s,const char * cmd)10274 static int wpas_ctrl_key_request(struct wpa_supplicant *wpa_s, const char *cmd)
10275 {
10276 	const char *pos = cmd;
10277 	int error, pairwise;
10278 
10279 	error = atoi(pos);
10280 	pos = os_strchr(pos, ' ');
10281 	if (!pos)
10282 		return -1;
10283 	pairwise = atoi(pos);
10284 	wpa_sm_key_request(wpa_s->wpa, error, pairwise);
10285 	return 0;
10286 }
10287 
10288 
wpas_ctrl_resend_assoc(struct wpa_supplicant * wpa_s)10289 static int wpas_ctrl_resend_assoc(struct wpa_supplicant *wpa_s)
10290 {
10291 #ifdef CONFIG_SME
10292 	struct wpa_driver_associate_params params;
10293 	int ret;
10294 
10295 	os_memset(&params, 0, sizeof(params));
10296 	params.bssid = wpa_s->bssid;
10297 	params.ssid = wpa_s->sme.ssid;
10298 	params.ssid_len = wpa_s->sme.ssid_len;
10299 	params.freq.freq = wpa_s->sme.freq;
10300 	if (wpa_s->last_assoc_req_wpa_ie) {
10301 		params.wpa_ie = wpabuf_head(wpa_s->last_assoc_req_wpa_ie);
10302 		params.wpa_ie_len = wpabuf_len(wpa_s->last_assoc_req_wpa_ie);
10303 	}
10304 	params.pairwise_suite = wpa_s->pairwise_cipher;
10305 	params.group_suite = wpa_s->group_cipher;
10306 	params.mgmt_group_suite = wpa_s->mgmt_group_cipher;
10307 	params.key_mgmt_suite = wpa_s->key_mgmt;
10308 	params.wpa_proto = wpa_s->wpa_proto;
10309 	params.mgmt_frame_protection = wpa_s->sme.mfp;
10310 	params.rrm_used = wpa_s->rrm.rrm_used;
10311 	if (wpa_s->sme.prev_bssid_set)
10312 		params.prev_bssid = wpa_s->sme.prev_bssid;
10313 	wpa_printf(MSG_INFO, "TESTING: Resend association request");
10314 	ret = wpa_drv_associate(wpa_s, &params);
10315 	wpa_s->testing_resend_assoc = 1;
10316 	return ret;
10317 #else /* CONFIG_SME */
10318 	return -1;
10319 #endif /* CONFIG_SME */
10320 }
10321 
10322 
wpas_ctrl_iface_send_twt_setup(struct wpa_supplicant * wpa_s,const char * cmd)10323 static int wpas_ctrl_iface_send_twt_setup(struct wpa_supplicant *wpa_s,
10324 					  const char *cmd)
10325 {
10326 	u8 dtok = 1;
10327 	int exponent = 10;
10328 	int mantissa = 8192;
10329 	u8 min_twt = 255;
10330 	unsigned long long twt = 0;
10331 	bool requestor = true;
10332 	int setup_cmd = 0;
10333 	bool trigger = true;
10334 	bool implicit = true;
10335 	bool flow_type = true;
10336 	int flow_id = 0;
10337 	bool protection = false;
10338 	u8 twt_channel = 0;
10339 	u8 control = BIT(4); /* Control field (IEEE Std 802.11ax-2021,
10340 			      * Figure 9-687 - Control field format):
10341 			      * B4 = TWT Information Frame Disabled */
10342 	const char *tok_s;
10343 
10344 	tok_s = os_strstr(cmd, " dialog=");
10345 	if (tok_s)
10346 		dtok = atoi(tok_s + os_strlen(" dialog="));
10347 
10348 	tok_s = os_strstr(cmd, " exponent=");
10349 	if (tok_s)
10350 		exponent = atoi(tok_s + os_strlen(" exponent="));
10351 
10352 	tok_s = os_strstr(cmd, " mantissa=");
10353 	if (tok_s)
10354 		mantissa = atoi(tok_s + os_strlen(" mantissa="));
10355 
10356 	tok_s = os_strstr(cmd, " min_twt=");
10357 	if (tok_s)
10358 		min_twt = atoi(tok_s + os_strlen(" min_twt="));
10359 
10360 	tok_s = os_strstr(cmd, " setup_cmd=");
10361 	if (tok_s)
10362 		setup_cmd = atoi(tok_s + os_strlen(" setup_cmd="));
10363 
10364 	tok_s = os_strstr(cmd, " twt=");
10365 	if (tok_s &&
10366 	    sscanf(tok_s + os_strlen(" twt="), "%llu", &twt) != 1)
10367 		return -1;
10368 
10369 	tok_s = os_strstr(cmd, " requestor=");
10370 	if (tok_s)
10371 		requestor = atoi(tok_s + os_strlen(" requestor="));
10372 
10373 	tok_s = os_strstr(cmd, " trigger=");
10374 	if (tok_s)
10375 		trigger = atoi(tok_s + os_strlen(" trigger="));
10376 
10377 	tok_s = os_strstr(cmd, " implicit=");
10378 	if (tok_s)
10379 		implicit = atoi(tok_s + os_strlen(" implicit="));
10380 
10381 	tok_s = os_strstr(cmd, " flow_type=");
10382 	if (tok_s)
10383 		flow_type = atoi(tok_s + os_strlen(" flow_type="));
10384 
10385 	tok_s = os_strstr(cmd, " flow_id=");
10386 	if (tok_s)
10387 		flow_id = atoi(tok_s + os_strlen(" flow_id="));
10388 
10389 	tok_s = os_strstr(cmd, " protection=");
10390 	if (tok_s)
10391 		protection = atoi(tok_s + os_strlen(" protection="));
10392 
10393 	tok_s = os_strstr(cmd, " twt_channel=");
10394 	if (tok_s)
10395 		twt_channel = atoi(tok_s + os_strlen(" twt_channel="));
10396 
10397 	tok_s = os_strstr(cmd, " control=");
10398 	if (tok_s)
10399 		control = atoi(tok_s + os_strlen(" control="));
10400 
10401 	return wpas_twt_send_setup(wpa_s, dtok, exponent, mantissa, min_twt,
10402 				   setup_cmd, twt, requestor, trigger, implicit,
10403 				   flow_type, flow_id, protection, twt_channel,
10404 				   control);
10405 }
10406 
10407 
wpas_ctrl_iface_send_twt_teardown(struct wpa_supplicant * wpa_s,const char * cmd)10408 static int wpas_ctrl_iface_send_twt_teardown(struct wpa_supplicant *wpa_s,
10409 					     const char *cmd)
10410 {
10411 	u8 flags = 0x1;
10412 	const char *tok_s;
10413 
10414 	tok_s = os_strstr(cmd, " flags=");
10415 	if (tok_s)
10416 		flags = atoi(tok_s + os_strlen(" flags="));
10417 
10418 	return wpas_twt_send_teardown(wpa_s, flags);
10419 }
10420 
10421 #endif /* CONFIG_TESTING_OPTIONS */
10422 
10423 
wpas_ctrl_vendor_elem_add(struct wpa_supplicant * wpa_s,char * cmd)10424 static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
10425 {
10426 	char *pos = cmd;
10427 	int frame;
10428 	size_t len;
10429 	struct wpabuf *buf;
10430 	struct ieee802_11_elems elems;
10431 
10432 	frame = atoi(pos);
10433 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
10434 		return -1;
10435 	wpa_s = wpas_vendor_elem(wpa_s, frame);
10436 
10437 	pos = os_strchr(pos, ' ');
10438 	if (pos == NULL)
10439 		return -1;
10440 	pos++;
10441 
10442 	len = os_strlen(pos);
10443 	if (len == 0)
10444 		return 0;
10445 	if (len & 1)
10446 		return -1;
10447 	len /= 2;
10448 
10449 	buf = wpabuf_alloc(len);
10450 	if (buf == NULL)
10451 		return -1;
10452 
10453 	if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
10454 		wpabuf_free(buf);
10455 		return -1;
10456 	}
10457 
10458 	if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
10459 	    ParseFailed) {
10460 		wpabuf_free(buf);
10461 		return -1;
10462 	}
10463 
10464 	if (wpa_s->vendor_elem[frame] == NULL) {
10465 		wpa_s->vendor_elem[frame] = buf;
10466 		goto update_ies;
10467 	}
10468 
10469 	if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
10470 		wpabuf_free(buf);
10471 		return -1;
10472 	}
10473 
10474 	wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
10475 	wpabuf_free(buf);
10476 
10477 update_ies:
10478 	wpas_vendor_elem_update(wpa_s);
10479 
10480 	if (frame == VENDOR_ELEM_PROBE_REQ ||
10481 	    frame == VENDOR_ELEM_PROBE_REQ_P2P)
10482 		wpa_supplicant_set_default_scan_ies(wpa_s);
10483 
10484 	return 0;
10485 }
10486 
10487 
wpas_ctrl_vendor_elem_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)10488 static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
10489 				     char *buf, size_t buflen)
10490 {
10491 	int frame = atoi(cmd);
10492 
10493 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
10494 		return -1;
10495 	wpa_s = wpas_vendor_elem(wpa_s, frame);
10496 
10497 	if (wpa_s->vendor_elem[frame] == NULL)
10498 		return 0;
10499 
10500 	return wpa_snprintf_hex(buf, buflen,
10501 				wpabuf_head_u8(wpa_s->vendor_elem[frame]),
10502 				wpabuf_len(wpa_s->vendor_elem[frame]));
10503 }
10504 
10505 
wpas_ctrl_vendor_elem_remove(struct wpa_supplicant * wpa_s,char * cmd)10506 static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
10507 {
10508 	char *pos = cmd;
10509 	int frame;
10510 	size_t len;
10511 	u8 *buf;
10512 	struct ieee802_11_elems elems;
10513 	int res;
10514 
10515 	frame = atoi(pos);
10516 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
10517 		return -1;
10518 	wpa_s = wpas_vendor_elem(wpa_s, frame);
10519 
10520 	pos = os_strchr(pos, ' ');
10521 	if (pos == NULL)
10522 		return -1;
10523 	pos++;
10524 
10525 	if (*pos == '*') {
10526 		wpabuf_free(wpa_s->vendor_elem[frame]);
10527 		wpa_s->vendor_elem[frame] = NULL;
10528 		wpas_vendor_elem_update(wpa_s);
10529 		return 0;
10530 	}
10531 
10532 	if (wpa_s->vendor_elem[frame] == NULL)
10533 		return -1;
10534 
10535 	len = os_strlen(pos);
10536 	if (len == 0)
10537 		return 0;
10538 	if (len & 1)
10539 		return -1;
10540 	len /= 2;
10541 
10542 	buf = os_malloc(len);
10543 	if (buf == NULL)
10544 		return -1;
10545 
10546 	if (hexstr2bin(pos, buf, len) < 0) {
10547 		os_free(buf);
10548 		return -1;
10549 	}
10550 
10551 	if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
10552 		os_free(buf);
10553 		return -1;
10554 	}
10555 
10556 	res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
10557 	os_free(buf);
10558 	return res;
10559 }
10560 
10561 
wpas_ctrl_neighbor_rep_cb(void * ctx,struct wpabuf * neighbor_rep)10562 static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
10563 {
10564 	struct wpa_supplicant *wpa_s = ctx;
10565 	size_t len;
10566 	const u8 *data;
10567 
10568 	/*
10569 	 * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
10570 	 * BSSID[6]
10571 	 * BSSID Information[4]
10572 	 * Operating Class[1]
10573 	 * Channel Number[1]
10574 	 * PHY Type[1]
10575 	 * Optional Subelements[variable]
10576 	 */
10577 #define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
10578 
10579 	if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
10580 		wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
10581 		goto out;
10582 	}
10583 
10584 	data = wpabuf_head_u8(neighbor_rep);
10585 	len = wpabuf_len(neighbor_rep);
10586 
10587 	while (len >= 2 + NR_IE_MIN_LEN) {
10588 		const u8 *nr;
10589 		char lci[256 * 2 + 1];
10590 		char civic[256 * 2 + 1];
10591 		u8 nr_len = data[1];
10592 		const u8 *pos = data, *end;
10593 
10594 		if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
10595 		    nr_len < NR_IE_MIN_LEN) {
10596 			wpa_dbg(wpa_s, MSG_DEBUG,
10597 				"CTRL: Invalid Neighbor Report element: id=%u len=%u",
10598 				data[0], nr_len);
10599 			goto out;
10600 		}
10601 
10602 		if (2U + nr_len > len) {
10603 			wpa_dbg(wpa_s, MSG_DEBUG,
10604 				"CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
10605 				data[0], len, nr_len);
10606 			goto out;
10607 		}
10608 		pos += 2;
10609 		end = pos + nr_len;
10610 
10611 		nr = pos;
10612 		pos += NR_IE_MIN_LEN;
10613 
10614 		lci[0] = '\0';
10615 		civic[0] = '\0';
10616 		while (end - pos > 2) {
10617 			u8 s_id, s_len;
10618 
10619 			s_id = *pos++;
10620 			s_len = *pos++;
10621 			if (s_len > end - pos)
10622 				goto out;
10623 			if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
10624 				/* Measurement Token[1] */
10625 				/* Measurement Report Mode[1] */
10626 				/* Measurement Type[1] */
10627 				/* Measurement Report[variable] */
10628 				switch (pos[2]) {
10629 				case MEASURE_TYPE_LCI:
10630 					if (lci[0])
10631 						break;
10632 					wpa_snprintf_hex(lci, sizeof(lci),
10633 							 pos, s_len);
10634 					break;
10635 				case MEASURE_TYPE_LOCATION_CIVIC:
10636 					if (civic[0])
10637 						break;
10638 					wpa_snprintf_hex(civic, sizeof(civic),
10639 							 pos, s_len);
10640 					break;
10641 				}
10642 			}
10643 
10644 			pos += s_len;
10645 		}
10646 
10647 		wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
10648 			"bssid=" MACSTR
10649 			" info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
10650 			MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
10651 			nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
10652 			nr[ETH_ALEN + 6],
10653 			lci[0] ? " lci=" : "", lci,
10654 			civic[0] ? " civic=" : "", civic);
10655 
10656 		data = end;
10657 		len -= 2 + nr_len;
10658 	}
10659 
10660 out:
10661 	wpabuf_free(neighbor_rep);
10662 }
10663 
10664 
wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant * wpa_s,char * cmd)10665 static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
10666 					     char *cmd)
10667 {
10668 	struct wpa_ssid_value ssid, *ssid_p = NULL;
10669 	int ret, lci = 0, civic = 0;
10670 	char *ssid_s;
10671 
10672 	ssid_s = os_strstr(cmd, "ssid=");
10673 	if (ssid_s) {
10674 		if (ssid_parse(ssid_s + 5, &ssid)) {
10675 			wpa_msg(wpa_s, MSG_INFO,
10676 				"CTRL: Send Neighbor Report: bad SSID");
10677 			return -1;
10678 		}
10679 
10680 		ssid_p = &ssid;
10681 
10682 		/*
10683 		 * Move cmd after the SSID text that may include "lci" or
10684 		 * "civic".
10685 		 */
10686 		cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
10687 		if (cmd)
10688 			cmd++;
10689 
10690 	}
10691 
10692 	if (cmd && os_strstr(cmd, "lci"))
10693 		lci = 1;
10694 
10695 	if (cmd && os_strstr(cmd, "civic"))
10696 		civic = 1;
10697 
10698 	ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
10699 						 wpas_ctrl_neighbor_rep_cb,
10700 						 wpa_s);
10701 
10702 	return ret;
10703 }
10704 
10705 
wpas_ctrl_iface_erp_flush(struct wpa_supplicant * wpa_s)10706 static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
10707 {
10708 	eapol_sm_erp_flush(wpa_s->eapol);
10709 	return 0;
10710 }
10711 
10712 
wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant * wpa_s,char * cmd)10713 static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
10714 					 char *cmd)
10715 {
10716 	char *token, *context = NULL;
10717 	unsigned int enable = ~0, type = 0;
10718 	u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
10719 	u8 *addr = NULL, *mask = NULL;
10720 
10721 	while ((token = str_token(cmd, " ", &context))) {
10722 		if (os_strcasecmp(token, "scan") == 0) {
10723 			type |= MAC_ADDR_RAND_SCAN;
10724 		} else if (os_strcasecmp(token, "sched") == 0) {
10725 			type |= MAC_ADDR_RAND_SCHED_SCAN;
10726 		} else if (os_strcasecmp(token, "pno") == 0) {
10727 			type |= MAC_ADDR_RAND_PNO;
10728 		} else if (os_strcasecmp(token, "all") == 0) {
10729 			type = wpa_s->mac_addr_rand_supported;
10730 		} else if (os_strncasecmp(token, "enable=", 7) == 0) {
10731 			enable = atoi(token + 7);
10732 		} else if (os_strncasecmp(token, "addr=", 5) == 0) {
10733 			addr = _addr;
10734 			if (hwaddr_aton(token + 5, addr)) {
10735 				wpa_printf(MSG_INFO,
10736 					   "CTRL: Invalid MAC address: %s",
10737 					   token);
10738 				return -1;
10739 			}
10740 		} else if (os_strncasecmp(token, "mask=", 5) == 0) {
10741 			mask = _mask;
10742 			if (hwaddr_aton(token + 5, mask)) {
10743 				wpa_printf(MSG_INFO,
10744 					   "CTRL: Invalid MAC address mask: %s",
10745 					   token);
10746 				return -1;
10747 			}
10748 		} else {
10749 			wpa_printf(MSG_INFO,
10750 				   "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
10751 				   token);
10752 			return -1;
10753 		}
10754 	}
10755 
10756 	if (!type) {
10757 		wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
10758 		return -1;
10759 	}
10760 
10761 	if (enable > 1) {
10762 		wpa_printf(MSG_INFO,
10763 			   "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
10764 		return -1;
10765 	}
10766 
10767 	if (!enable)
10768 		return wpas_disable_mac_addr_randomization(wpa_s, type);
10769 
10770 	return wpas_enable_mac_addr_randomization(wpa_s, type, addr, mask);
10771 }
10772 
10773 
wpas_ctrl_iface_pmksa(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)10774 static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
10775 				 char *buf, size_t buflen)
10776 {
10777 	size_t reply_len;
10778 
10779 	reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
10780 #ifdef CONFIG_AP
10781 	reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
10782 					      buflen - reply_len);
10783 #endif /* CONFIG_AP */
10784 	return reply_len;
10785 }
10786 
10787 
wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant * wpa_s)10788 static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
10789 {
10790 	ptksa_cache_flush(wpa_s->ptksa, NULL, WPA_CIPHER_NONE);
10791 	wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
10792 #ifdef CONFIG_AP
10793 	wpas_ap_pmksa_cache_flush(wpa_s);
10794 #endif /* CONFIG_AP */
10795 }
10796 
10797 
10798 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
10799 
wpas_ctrl_iface_pmksa_get(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)10800 static int wpas_ctrl_iface_pmksa_get(struct wpa_supplicant *wpa_s,
10801 				     const char *cmd, char *buf, size_t buflen)
10802 {
10803 	struct rsn_pmksa_cache_entry *entry;
10804 	struct wpa_ssid *ssid;
10805 	char *pos, *pos2, *end;
10806 	int ret;
10807 	struct os_reltime now;
10808 
10809 	ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
10810 	if (!ssid)
10811 		return -1;
10812 
10813 	pos = buf;
10814 	end = buf + buflen;
10815 
10816 	os_get_reltime(&now);
10817 
10818 	/*
10819 	 * Entry format:
10820 	 * <BSSID> <PMKID> <PMK> <reauth_time in seconds>
10821 	 * <expiration in seconds> <akmp> <opportunistic>
10822 	 * [FILS Cache Identifier]
10823 	 */
10824 
10825 	for (entry = wpa_sm_pmksa_cache_head(wpa_s->wpa); entry;
10826 	     entry = entry->next) {
10827 		if (entry->network_ctx != ssid)
10828 			continue;
10829 
10830 		pos2 = pos;
10831 		ret = os_snprintf(pos2, end - pos2, MACSTR " ",
10832 				  MAC2STR(entry->aa));
10833 		if (os_snprintf_error(end - pos2, ret))
10834 			break;
10835 		pos2 += ret;
10836 
10837 		pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmkid,
10838 					 PMKID_LEN);
10839 
10840 		ret = os_snprintf(pos2, end - pos2, " ");
10841 		if (os_snprintf_error(end - pos2, ret))
10842 			break;
10843 		pos2 += ret;
10844 
10845 		pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmk,
10846 					 entry->pmk_len);
10847 
10848 		ret = os_snprintf(pos2, end - pos2, " %d %d %d %d",
10849 				  (int) (entry->reauth_time - now.sec),
10850 				  (int) (entry->expiration - now.sec),
10851 				  entry->akmp,
10852 				  entry->opportunistic);
10853 		if (os_snprintf_error(end - pos2, ret))
10854 			break;
10855 		pos2 += ret;
10856 
10857 		if (entry->fils_cache_id_set) {
10858 			ret = os_snprintf(pos2, end - pos2, " %02x%02x",
10859 					  entry->fils_cache_id[0],
10860 					  entry->fils_cache_id[1]);
10861 			if (os_snprintf_error(end - pos2, ret))
10862 				break;
10863 			pos2 += ret;
10864 		}
10865 
10866 		ret = os_snprintf(pos2, end - pos2, "\n");
10867 		if (os_snprintf_error(end - pos2, ret))
10868 			break;
10869 		pos2 += ret;
10870 
10871 		pos = pos2;
10872 	}
10873 
10874 	return pos - buf;
10875 }
10876 
10877 
wpas_ctrl_iface_pmksa_add(struct wpa_supplicant * wpa_s,char * cmd)10878 static int wpas_ctrl_iface_pmksa_add(struct wpa_supplicant *wpa_s,
10879 				     char *cmd)
10880 {
10881 	struct rsn_pmksa_cache_entry *entry;
10882 	struct wpa_ssid *ssid;
10883 	char *pos, *pos2;
10884 	int ret = -1;
10885 	struct os_reltime now;
10886 	int reauth_time = 0, expiration = 0, i;
10887 
10888 	/*
10889 	 * Entry format:
10890 	 * <network_id> <BSSID> <PMKID> <PMK> <reauth_time in seconds>
10891 	 * <expiration in seconds> <akmp> <opportunistic>
10892 	 * [FILS Cache Identifier]
10893 	 */
10894 
10895 	ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
10896 	if (!ssid)
10897 		return -1;
10898 
10899 	pos = os_strchr(cmd, ' ');
10900 	if (!pos)
10901 		return -1;
10902 	pos++;
10903 
10904 	entry = os_zalloc(sizeof(*entry));
10905 	if (!entry)
10906 		return -1;
10907 
10908 	if (hwaddr_aton(pos, entry->aa))
10909 		goto fail;
10910 
10911 	pos = os_strchr(pos, ' ');
10912 	if (!pos)
10913 		goto fail;
10914 	pos++;
10915 
10916 	if (hexstr2bin(pos, entry->pmkid, PMKID_LEN) < 0)
10917 		goto fail;
10918 
10919 	pos = os_strchr(pos, ' ');
10920 	if (!pos)
10921 		goto fail;
10922 	pos++;
10923 
10924 	pos2 = os_strchr(pos, ' ');
10925 	if (!pos2)
10926 		goto fail;
10927 	entry->pmk_len = (pos2 - pos) / 2;
10928 	if (entry->pmk_len < PMK_LEN || entry->pmk_len > PMK_LEN_MAX ||
10929 	    hexstr2bin(pos, entry->pmk, entry->pmk_len) < 0)
10930 		goto fail;
10931 
10932 	pos = os_strchr(pos, ' ');
10933 	if (!pos)
10934 		goto fail;
10935 	pos++;
10936 
10937 	if (sscanf(pos, "%d %d %d %d", &reauth_time, &expiration,
10938 		   &entry->akmp, &entry->opportunistic) != 4)
10939 		goto fail;
10940 	if (reauth_time > expiration)
10941 		goto fail;
10942 	for (i = 0; i < 4; i++) {
10943 		pos = os_strchr(pos, ' ');
10944 		if (!pos) {
10945 			if (i < 3)
10946 				goto fail;
10947 			break;
10948 		}
10949 		pos++;
10950 	}
10951 	if (pos) {
10952 		if (hexstr2bin(pos, entry->fils_cache_id,
10953 			       FILS_CACHE_ID_LEN) < 0)
10954 			goto fail;
10955 		entry->fils_cache_id_set = 1;
10956 	}
10957 	os_get_reltime(&now);
10958 	entry->expiration = now.sec + expiration;
10959 	entry->reauth_time = now.sec + reauth_time;
10960 
10961 	entry->network_ctx = ssid;
10962 	os_memcpy(entry->spa, wpa_s->own_addr, ETH_ALEN);
10963 
10964 	entry->external = true;
10965 
10966 	wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
10967 	entry = NULL;
10968 	ret = 0;
10969 fail:
10970 	os_free(entry);
10971 	return ret;
10972 }
10973 
10974 
10975 #ifdef CONFIG_MESH
10976 
wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)10977 static int wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant *wpa_s,
10978 					  const char *cmd, char *buf,
10979 					  size_t buflen)
10980 {
10981 	u8 spa[ETH_ALEN];
10982 
10983 	if (!wpa_s->ifmsh)
10984 		return -1;
10985 
10986 	if (os_strcasecmp(cmd, "any") == 0)
10987 		return wpas_ap_pmksa_cache_list_mesh(wpa_s, NULL, buf, buflen);
10988 
10989 	if (hwaddr_aton(cmd, spa))
10990 		return -1;
10991 
10992 	return wpas_ap_pmksa_cache_list_mesh(wpa_s, spa, buf, buflen);
10993 }
10994 
10995 
wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant * wpa_s,char * cmd)10996 static int wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant *wpa_s,
10997 					  char *cmd)
10998 {
10999 	/*
11000 	 * We do not check mesh interface existence because PMKSA should be
11001 	 * stored before wpa_s->ifmsh creation to suppress commit message
11002 	 * creation.
11003 	 */
11004 	return wpas_ap_pmksa_cache_add_external(wpa_s, cmd);
11005 }
11006 
11007 #endif /* CONFIG_MESH */
11008 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
11009 
11010 
11011 #ifdef CONFIG_FILS
wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant * wpa_s,const char * cmd)11012 static int wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant *wpa_s,
11013 					    const char *cmd)
11014 {
11015 	struct fils_hlp_req *req;
11016 	const char *pos;
11017 
11018 	/* format: <dst> <packet starting from ethertype> */
11019 
11020 	req = os_zalloc(sizeof(*req));
11021 	if (!req)
11022 		return -1;
11023 
11024 	if (hwaddr_aton(cmd, req->dst))
11025 		goto fail;
11026 
11027 	pos = os_strchr(cmd, ' ');
11028 	if (!pos)
11029 		goto fail;
11030 	pos++;
11031 	req->pkt = wpabuf_parse_bin(pos);
11032 	if (!req->pkt)
11033 		goto fail;
11034 
11035 	dl_list_add_tail(&wpa_s->fils_hlp_req, &req->list);
11036 	return 0;
11037 fail:
11038 	wpabuf_free(req->pkt);
11039 	os_free(req);
11040 	return -1;
11041 }
11042 #endif /* CONFIG_FILS */
11043 
11044 
wpas_ctrl_cmd_debug_level(const char * cmd)11045 int wpas_ctrl_cmd_debug_level(const char *cmd)
11046 {
11047 	if (os_strcmp(cmd, "PING") == 0 ||
11048 	    os_strncmp(cmd, "BSS ", 4) == 0 ||
11049 	    os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
11050 	    os_strncmp(cmd, "STATUS", 6) == 0 ||
11051 	    os_strncmp(cmd, "STA ", 4) == 0 ||
11052 	    os_strncmp(cmd, "STA-", 4) == 0)
11053 		return MSG_EXCESSIVE;
11054 	return MSG_DEBUG;
11055 }
11056 
11057 
wpas_ctrl_iface_configure_mscs(struct wpa_supplicant * wpa_s,const char * cmd)11058 static int wpas_ctrl_iface_configure_mscs(struct wpa_supplicant *wpa_s,
11059 					  const char *cmd)
11060 {
11061 	size_t frame_classifier_len;
11062 	const char *pos, *end;
11063 	struct robust_av_data *robust_av = &wpa_s->robust_av;
11064 	int val;
11065 
11066 	/*
11067 	 * format:
11068 	 * <add|remove|change> [up_bitmap=<hex byte>] [up_limit=<integer>]
11069 	 * [stream_timeout=<in TUs>] [frame_classifier=<hex bytes>]
11070 	 */
11071 	os_memset(robust_av, 0, sizeof(struct robust_av_data));
11072 	if (os_strncmp(cmd, "add ", 4) == 0) {
11073 		robust_av->request_type = SCS_REQ_ADD;
11074 	} else if (os_strcmp(cmd, "remove") == 0) {
11075 		robust_av->request_type = SCS_REQ_REMOVE;
11076 		robust_av->valid_config = false;
11077 		return wpas_send_mscs_req(wpa_s);
11078 	} else if (os_strncmp(cmd, "change ", 7) == 0) {
11079 		robust_av->request_type = SCS_REQ_CHANGE;
11080 	} else {
11081 		return -1;
11082 	}
11083 
11084 	pos = os_strstr(cmd, "up_bitmap=");
11085 	if (!pos)
11086 		return -1;
11087 
11088 	val = hex2byte(pos + 10);
11089 	if (val < 0)
11090 		return -1;
11091 	robust_av->up_bitmap = val;
11092 
11093 	pos = os_strstr(cmd, "up_limit=");
11094 	if (!pos)
11095 		return -1;
11096 
11097 	robust_av->up_limit = atoi(pos + 9);
11098 
11099 	pos = os_strstr(cmd, "stream_timeout=");
11100 	if (!pos)
11101 		return -1;
11102 
11103 	robust_av->stream_timeout = atoi(pos + 15);
11104 	if (robust_av->stream_timeout == 0)
11105 		return -1;
11106 
11107 	pos = os_strstr(cmd, "frame_classifier=");
11108 	if (!pos)
11109 		return -1;
11110 
11111 	pos += 17;
11112 	end = os_strchr(pos, ' ');
11113 	if (!end)
11114 		end = pos + os_strlen(pos);
11115 
11116 	frame_classifier_len = (end - pos) / 2;
11117 	if (frame_classifier_len > sizeof(robust_av->frame_classifier) ||
11118 	    hexstr2bin(pos, robust_av->frame_classifier, frame_classifier_len))
11119 		return -1;
11120 
11121 	robust_av->frame_classifier_len = frame_classifier_len;
11122 	robust_av->valid_config = true;
11123 
11124 	return wpas_send_mscs_req(wpa_s);
11125 }
11126 
11127 
11128 #ifdef CONFIG_PASN
wpas_ctrl_iface_pasn_start(struct wpa_supplicant * wpa_s,char * cmd)11129 static int wpas_ctrl_iface_pasn_start(struct wpa_supplicant *wpa_s, char *cmd)
11130 {
11131 	char *token, *context = NULL;
11132 	u8 bssid[ETH_ALEN];
11133 	int akmp = -1, cipher = -1, got_bssid = 0;
11134 	u16 group = 0xFFFF;
11135 	u8 *comeback = NULL;
11136 	size_t comeback_len = 0;
11137 	int id = 0, ret = -1;
11138 
11139 	/*
11140 	 * Entry format: bssid=<BSSID> akmp=<AKMP> cipher=<CIPHER> group=<group>
11141 	 *    [comeback=<hexdump>]
11142 	 */
11143 	while ((token = str_token(cmd, " ", &context))) {
11144 		if (os_strncmp(token, "bssid=", 6) == 0) {
11145 			if (hwaddr_aton(token + 6, bssid))
11146 				goto out;
11147 			got_bssid = 1;
11148 		} else if (os_strcmp(token, "akmp=PASN") == 0) {
11149 			akmp = WPA_KEY_MGMT_PASN;
11150 #ifdef CONFIG_IEEE80211R
11151 		} else if (os_strcmp(token, "akmp=FT-PSK") == 0) {
11152 			akmp = WPA_KEY_MGMT_FT_PSK;
11153 		} else if (os_strcmp(token, "akmp=FT-EAP-SHA384") == 0) {
11154 			akmp = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
11155 		} else if (os_strcmp(token, "akmp=FT-EAP") == 0) {
11156 			akmp = WPA_KEY_MGMT_FT_IEEE8021X;
11157 #endif /* CONFIG_IEEE80211R */
11158 #ifdef CONFIG_SAE
11159 		} else if (os_strcmp(token, "akmp=SAE") == 0) {
11160 			akmp = WPA_KEY_MGMT_SAE;
11161 		} else if (os_strcmp(token, "akmp=SAE-EXT-KEY") == 0) {
11162 			akmp = WPA_KEY_MGMT_SAE_EXT_KEY;
11163 #endif /* CONFIG_SAE */
11164 #ifdef CONFIG_FILS
11165 		} else if (os_strcmp(token, "akmp=FILS-SHA256") == 0) {
11166 			akmp = WPA_KEY_MGMT_FILS_SHA256;
11167 		} else if (os_strcmp(token, "akmp=FILS-SHA384") == 0) {
11168 			akmp = WPA_KEY_MGMT_FILS_SHA384;
11169 #endif /* CONFIG_FILS */
11170 		} else if (os_strcmp(token, "cipher=CCMP-256") == 0) {
11171 			cipher = WPA_CIPHER_CCMP_256;
11172 		} else if (os_strcmp(token, "cipher=GCMP-256") == 0) {
11173 			cipher = WPA_CIPHER_GCMP_256;
11174 		} else if (os_strcmp(token, "cipher=CCMP") == 0) {
11175 			cipher = WPA_CIPHER_CCMP;
11176 		} else if (os_strcmp(token, "cipher=GCMP") == 0) {
11177 			cipher = WPA_CIPHER_GCMP;
11178 		} else if (os_strncmp(token, "group=", 6) == 0) {
11179 			group = atoi(token + 6);
11180 		} else if (os_strncmp(token, "nid=", 4) == 0) {
11181 			id = atoi(token + 4);
11182 		} else if (os_strncmp(token, "comeback=", 9) == 0) {
11183 			comeback_len = os_strlen(token + 9);
11184 			if (comeback || !comeback_len || comeback_len % 2)
11185 				goto out;
11186 
11187 			comeback_len /= 2;
11188 			comeback = os_malloc(comeback_len);
11189 			if (!comeback ||
11190 			    hexstr2bin(token + 9, comeback, comeback_len))
11191 				goto out;
11192 		} else {
11193 			wpa_printf(MSG_DEBUG,
11194 				   "CTRL: PASN Invalid parameter: '%s'",
11195 				   token);
11196 			goto out;
11197 		}
11198 	}
11199 
11200 	if (!got_bssid || akmp == -1 || cipher == -1 || group == 0xFFFF) {
11201 		wpa_printf(MSG_DEBUG,"CTRL: PASN missing parameter");
11202 		goto out;
11203 	}
11204 
11205 	ret = wpas_pasn_auth_start(wpa_s, wpa_s->own_addr, bssid, akmp, cipher,
11206 				   group, id, comeback, comeback_len);
11207 out:
11208 	os_free(comeback);
11209 	return ret;
11210 }
11211 
11212 
wpas_ctrl_iface_pasn_deauthenticate(struct wpa_supplicant * wpa_s,const char * cmd)11213 static int wpas_ctrl_iface_pasn_deauthenticate(struct wpa_supplicant *wpa_s,
11214 					       const char *cmd)
11215 {
11216 	u8 bssid[ETH_ALEN];
11217 
11218 	if (os_strncmp(cmd, "bssid=", 6) != 0 || hwaddr_aton(cmd + 6, bssid)) {
11219 		wpa_printf(MSG_DEBUG,
11220 			   "CTRL: PASN_DEAUTH without valid BSSID");
11221 		return -1;
11222 	}
11223 
11224 	return wpas_pasn_deauthenticate(wpa_s, wpa_s->own_addr, bssid);
11225 }
11226 
11227 #endif /* CONFIG_PASN */
11228 
11229 
set_type4_frame_classifier(const char * cmd,struct type4_params * param)11230 static int set_type4_frame_classifier(const char *cmd,
11231 				      struct type4_params *param)
11232 {
11233 	const char *pos, *end;
11234 	u8 classifier_mask = 0;
11235 	int ret;
11236 	char addr[INET6_ADDRSTRLEN];
11237 	size_t alen;
11238 
11239 	if (os_strstr(cmd, "ip_version=ipv4")) {
11240 		param->ip_version = IPV4;
11241 	} else if (os_strstr(cmd, "ip_version=ipv6")) {
11242 		param->ip_version = IPV6;
11243 	} else {
11244 		wpa_printf(MSG_ERROR, "IP version missing/invalid");
11245 		return -1;
11246 	}
11247 
11248 	classifier_mask |= BIT(0);
11249 
11250 	pos = os_strstr(cmd, "src_ip=");
11251 	if (pos) {
11252 		pos += 7;
11253 		end = os_strchr(pos, ' ');
11254 		if (!end)
11255 			end = pos + os_strlen(pos);
11256 
11257 		alen = end - pos;
11258 		if (alen >= INET6_ADDRSTRLEN)
11259 			return -1;
11260 		os_memcpy(addr, pos, alen);
11261 		addr[alen] = '\0';
11262 		if (param->ip_version == IPV4)
11263 			ret = inet_pton(AF_INET, addr,
11264 					&param->ip_params.v4.src_ip);
11265 		else
11266 			ret = inet_pton(AF_INET6, addr,
11267 					&param->ip_params.v6.src_ip);
11268 
11269 		if (ret != 1) {
11270 			wpa_printf(MSG_ERROR,
11271 				   "Error converting src IP address to binary ret=%d",
11272 				   ret);
11273 			return -1;
11274 		}
11275 
11276 		classifier_mask |= BIT(1);
11277 	}
11278 
11279 	pos = os_strstr(cmd, "dst_ip=");
11280 	if (pos) {
11281 		pos += 7;
11282 		end = os_strchr(pos, ' ');
11283 		if (!end)
11284 			end = pos + os_strlen(pos);
11285 
11286 		alen = end - pos;
11287 		if (alen >= INET6_ADDRSTRLEN)
11288 			return -1;
11289 		os_memcpy(addr, pos, alen);
11290 		addr[alen] = '\0';
11291 		if (param->ip_version == IPV4)
11292 			ret = inet_pton(AF_INET, addr,
11293 					&param->ip_params.v4.dst_ip);
11294 		else
11295 			ret = inet_pton(AF_INET6, addr,
11296 					&param->ip_params.v6.dst_ip);
11297 
11298 		if (ret != 1) {
11299 			wpa_printf(MSG_ERROR,
11300 				   "Error converting dst IP address to binary ret=%d",
11301 				   ret);
11302 			return -1;
11303 		}
11304 
11305 		classifier_mask |= BIT(2);
11306 	}
11307 
11308 	pos = os_strstr(cmd, "src_port=");
11309 	if (pos && atoi(pos + 9) > 0) {
11310 		if (param->ip_version == IPV4)
11311 			param->ip_params.v4.src_port = atoi(pos + 9);
11312 		else
11313 			param->ip_params.v6.src_port = atoi(pos + 9);
11314 		classifier_mask |= BIT(3);
11315 	}
11316 
11317 	pos = os_strstr(cmd, "dst_port=");
11318 	if (pos && atoi(pos + 9) > 0) {
11319 		if (param->ip_version == IPV4)
11320 			param->ip_params.v4.dst_port = atoi(pos + 9);
11321 		else
11322 			param->ip_params.v6.dst_port = atoi(pos + 9);
11323 		classifier_mask |= BIT(4);
11324 	}
11325 
11326 	pos = os_strstr(cmd, "dscp=");
11327 	if (pos && atoi(pos + 5) > 0) {
11328 		if (param->ip_version == IPV4)
11329 			param->ip_params.v4.dscp = atoi(pos + 5);
11330 		else
11331 			param->ip_params.v6.dscp = atoi(pos + 5);
11332 		classifier_mask |= BIT(5);
11333 	}
11334 
11335 	if (param->ip_version == IPV4) {
11336 		pos = os_strstr(cmd, "protocol=");
11337 		if (pos) {
11338 			if (os_strstr(pos, "udp")) {
11339 				param->ip_params.v4.protocol = 17;
11340 			} else if (os_strstr(pos, "tcp")) {
11341 				param->ip_params.v4.protocol = 6;
11342 			} else if (os_strstr(pos, "esp")) {
11343 				param->ip_params.v4.protocol = 50;
11344 			} else {
11345 				wpa_printf(MSG_ERROR, "Invalid protocol");
11346 				return -1;
11347 			}
11348 			classifier_mask |= BIT(6);
11349 		}
11350 	} else {
11351 		pos = os_strstr(cmd, "next_header=");
11352 		if (pos) {
11353 			if (os_strstr(pos, "udp")) {
11354 				param->ip_params.v6.next_header = 17;
11355 			} else if (os_strstr(pos, "tcp")) {
11356 				param->ip_params.v6.next_header = 6;
11357 			} else if (os_strstr(pos, "esp")) {
11358 				param->ip_params.v6.next_header = 50;
11359 			} else {
11360 				wpa_printf(MSG_ERROR, "Invalid next header");
11361 				return -1;
11362 			}
11363 
11364 			classifier_mask |= BIT(6);
11365 		}
11366 
11367 		pos = os_strstr(cmd, "flow_label=");
11368 		if (pos) {
11369 			pos += 11;
11370 			end = os_strchr(pos, ' ');
11371 			if (!end)
11372 				end = pos + os_strlen(pos);
11373 
11374 			if (end - pos != 6 ||
11375 			    hexstr2bin(pos, param->ip_params.v6.flow_label,
11376 				       3) ||
11377 			    param->ip_params.v6.flow_label[0] > 0x0F) {
11378 				wpa_printf(MSG_ERROR, "Invalid flow label");
11379 				return -1;
11380 			}
11381 
11382 			classifier_mask |= BIT(7);
11383 		}
11384 	}
11385 
11386 	param->classifier_mask = classifier_mask;
11387 	return 0;
11388 }
11389 
11390 
set_type10_frame_classifier(const char * cmd,struct type10_params * param)11391 static int set_type10_frame_classifier(const char *cmd,
11392 				       struct type10_params *param)
11393 {
11394 	const char *pos, *end;
11395 	size_t filter_len;
11396 
11397 	pos = os_strstr(cmd, "prot_instance=");
11398 	if (!pos) {
11399 		wpa_printf(MSG_ERROR, "Protocol instance missing");
11400 		return -1;
11401 	}
11402 	param->prot_instance = atoi(pos + 14);
11403 
11404 	pos = os_strstr(cmd, "prot_number=");
11405 	if (!pos) {
11406 		wpa_printf(MSG_ERROR, "Protocol number missing");
11407 		return -1;
11408 	}
11409 	if (os_strstr(pos, "udp")) {
11410 		param->prot_number = 17;
11411 	} else if (os_strstr(pos, "tcp")) {
11412 		param->prot_number = 6;
11413 	} else if (os_strstr(pos, "esp")) {
11414 		param->prot_number = 50;
11415 	} else {
11416 		wpa_printf(MSG_ERROR, "Invalid protocol number");
11417 		return -1;
11418 	}
11419 
11420 	pos = os_strstr(cmd, "filter_value=");
11421 	if (!pos) {
11422 		wpa_printf(MSG_ERROR,
11423 			   "Classifier parameter filter_value missing");
11424 		return -1;
11425 	}
11426 
11427 	pos += 13;
11428 	end = os_strchr(pos, ' ');
11429 	if (!end)
11430 		end = pos + os_strlen(pos);
11431 
11432 	filter_len = (end - pos) / 2;
11433 	param->filter_value = os_malloc(filter_len);
11434 	if (!param->filter_value)
11435 		return -1;
11436 
11437 	if (hexstr2bin(pos, param->filter_value, filter_len)) {
11438 		wpa_printf(MSG_ERROR, "Invalid filter_value %s", pos);
11439 		goto free;
11440 	}
11441 
11442 	pos = os_strstr(cmd, "filter_mask=");
11443 	if (!pos) {
11444 		wpa_printf(MSG_ERROR,
11445 			   "Classifier parameter filter_mask missing");
11446 		goto free;
11447 	}
11448 
11449 	pos += 12;
11450 	end = os_strchr(pos, ' ');
11451 	if (!end)
11452 		end = pos + os_strlen(pos);
11453 
11454 	if (filter_len != (size_t) (end - pos) / 2) {
11455 		wpa_printf(MSG_ERROR,
11456 			   "Filter mask length mismatch expected=%zu received=%zu",
11457 			   filter_len, (size_t) (end - pos) / 2);
11458 		goto free;
11459 	}
11460 
11461 	param->filter_mask = os_malloc(filter_len);
11462 	if (!param->filter_mask)
11463 		goto free;
11464 
11465 	if (hexstr2bin(pos, param->filter_mask, filter_len)) {
11466 		wpa_printf(MSG_ERROR, "Invalid filter mask %s", pos);
11467 		os_free(param->filter_mask);
11468 		param->filter_mask = NULL;
11469 		goto free;
11470 	}
11471 
11472 	param->filter_len = filter_len;
11473 	return 0;
11474 free:
11475 	os_free(param->filter_value);
11476 	param->filter_value = NULL;
11477 	return -1;
11478 }
11479 
11480 
scs_parse_type4(struct tclas_element * elem,const char * pos)11481 static int scs_parse_type4(struct tclas_element *elem, const char *pos)
11482 {
11483 	struct type4_params type4_param = { 0 };
11484 
11485 	if (set_type4_frame_classifier(pos, &type4_param) == -1) {
11486 		wpa_printf(MSG_ERROR, "Failed to set frame_classifier 4");
11487 		return -1;
11488 	}
11489 
11490 	os_memcpy(&elem->frame_classifier.type4_param,
11491 		  &type4_param, sizeof(struct type4_params));
11492 	return 0;
11493 }
11494 
11495 
scs_parse_type10(struct tclas_element * elem,const char * pos)11496 static int scs_parse_type10(struct tclas_element *elem, const char *pos)
11497 {
11498 	struct type10_params type10_param = { 0 };
11499 
11500 	if (set_type10_frame_classifier(pos, &type10_param) == -1) {
11501 		wpa_printf(MSG_ERROR, "Failed to set frame_classifier 10");
11502 		return -1;
11503 	}
11504 
11505 	os_memcpy(&elem->frame_classifier.type10_param,
11506 		  &type10_param, sizeof(struct type10_params));
11507 	return 0;
11508 }
11509 
11510 
wpas_ctrl_iface_configure_scs(struct wpa_supplicant * wpa_s,char * cmd)11511 static int wpas_ctrl_iface_configure_scs(struct wpa_supplicant *wpa_s,
11512 					 char *cmd)
11513 {
11514 	char *pos1, *pos;
11515 	struct scs_robust_av_data *scs_data = &wpa_s->scs_robust_av_req;
11516 	struct scs_desc_elem desc_elem = { 0 };
11517 	int val;
11518 	unsigned int num_scs_desc = 0;
11519 
11520 	if (wpa_s->ongoing_scs_req) {
11521 		wpa_printf(MSG_ERROR, "%s: SCS Request already in queue",
11522 			   __func__);
11523 		return -1;
11524 	}
11525 
11526 	/**
11527 	 * format:
11528 	 * [scs_id=<decimal number>] <add|remove|change> [scs_up=<0-7>]
11529 	 * [classifier_type=<4|10>]
11530 	 * [classifier params based on classifier type]
11531 	 * [tclas_processing=<0|1>] [scs_id=<decimal number>] ...
11532 	 */
11533 	pos1 = os_strstr(cmd, "scs_id=");
11534 	if (!pos1) {
11535 		wpa_printf(MSG_ERROR, "SCSID not present");
11536 		return -1;
11537 	}
11538 
11539 	free_up_scs_desc(scs_data);
11540 
11541 	while (pos1) {
11542 		struct scs_desc_elem *n1;
11543 		struct active_scs_elem *active_scs_desc;
11544 		char *next_scs_desc;
11545 		unsigned int num_tclas_elem = 0;
11546 		bool scsid_active = false;
11547 
11548 		desc_elem.scs_id = atoi(pos1 + 7);
11549 		pos1 += 7;
11550 
11551 		next_scs_desc = os_strstr(pos1, "scs_id=");
11552 		if (next_scs_desc) {
11553 			char temp[20];
11554 
11555 			os_snprintf(temp, sizeof(temp), "scs_id=%d ",
11556 				    desc_elem.scs_id);
11557 			if (os_strstr(next_scs_desc, temp)) {
11558 				wpa_printf(MSG_ERROR,
11559 					   "Multiple SCS descriptors configured with same SCSID(=%d)",
11560 					   desc_elem.scs_id);
11561 				goto free_scs_desc;
11562 			}
11563 			pos1[next_scs_desc - pos1 - 1] = '\0';
11564 		}
11565 
11566 		dl_list_for_each(active_scs_desc, &wpa_s->active_scs_ids,
11567 				 struct active_scs_elem, list) {
11568 			if (desc_elem.scs_id == active_scs_desc->scs_id) {
11569 				scsid_active = true;
11570 				break;
11571 			}
11572 		}
11573 
11574 		if (os_strstr(pos1, "add ")) {
11575 			desc_elem.request_type = SCS_REQ_ADD;
11576 			if (scsid_active) {
11577 				wpa_printf(MSG_ERROR, "SCSID %d already active",
11578 					   desc_elem.scs_id);
11579 				return -1;
11580 			}
11581 		} else if (os_strstr(pos1, "remove")) {
11582 			desc_elem.request_type = SCS_REQ_REMOVE;
11583 			if (!scsid_active) {
11584 				wpa_printf(MSG_ERROR, "SCSID %d not active",
11585 					   desc_elem.scs_id);
11586 				return -1;
11587 			}
11588 			goto scs_desc_end;
11589 		} else if (os_strstr(pos1, "change ")) {
11590 			desc_elem.request_type = SCS_REQ_CHANGE;
11591 			if (!scsid_active) {
11592 				wpa_printf(MSG_ERROR, "SCSID %d not active",
11593 					   desc_elem.scs_id);
11594 				return -1;
11595 			}
11596 		} else {
11597 			wpa_printf(MSG_ERROR, "SCS Request type invalid");
11598 			goto free_scs_desc;
11599 		}
11600 
11601 		pos1 = os_strstr(pos1, "scs_up=");
11602 		if (!pos1) {
11603 			wpa_printf(MSG_ERROR,
11604 				   "Intra-Access user priority not present");
11605 			goto free_scs_desc;
11606 		}
11607 
11608 		val = atoi(pos1 + 7);
11609 		if (val < 0 || val > 7) {
11610 			wpa_printf(MSG_ERROR,
11611 				   "Intra-Access user priority invalid %d",
11612 				   val);
11613 			goto free_scs_desc;
11614 		}
11615 
11616 		desc_elem.intra_access_priority = val;
11617 		desc_elem.scs_up_avail = true;
11618 
11619 		pos = os_strstr(pos1, "classifier_type=");
11620 		if (!pos) {
11621 			wpa_printf(MSG_ERROR, "classifier type empty");
11622 			goto free_scs_desc;
11623 		}
11624 
11625 		while (pos) {
11626 			struct tclas_element elem = { 0 }, *n;
11627 			char *next_tclas_elem;
11628 
11629 			val = atoi(pos + 16);
11630 			if (val != 4 && val != 10) {
11631 				wpa_printf(MSG_ERROR,
11632 					   "classifier type invalid %d", val);
11633 				goto free_scs_desc;
11634 			}
11635 
11636 			elem.classifier_type = val;
11637 			pos += 16;
11638 
11639 			next_tclas_elem = os_strstr(pos, "classifier_type=");
11640 			if (next_tclas_elem) {
11641 				pos1 = next_tclas_elem;
11642 				pos[next_tclas_elem - pos - 1] = '\0';
11643 			}
11644 
11645 			switch (val) {
11646 			case 4:
11647 				if (scs_parse_type4(&elem, pos) < 0)
11648 					goto free_scs_desc;
11649 				break;
11650 			case 10:
11651 				if (scs_parse_type10(&elem, pos) < 0)
11652 					goto free_scs_desc;
11653 				break;
11654 			}
11655 
11656 			n = os_realloc(desc_elem.tclas_elems,
11657 				       (num_tclas_elem + 1) * sizeof(elem));
11658 			if (!n)
11659 				goto free_scs_desc;
11660 
11661 			desc_elem.tclas_elems = n;
11662 			os_memcpy((u8 *) desc_elem.tclas_elems +
11663 				  num_tclas_elem * sizeof(elem),
11664 				  &elem, sizeof(elem));
11665 			num_tclas_elem++;
11666 			desc_elem.num_tclas_elem = num_tclas_elem;
11667 			pos = next_tclas_elem;
11668 		}
11669 
11670 		if (desc_elem.num_tclas_elem > 1) {
11671 			pos1 = os_strstr(pos1, "tclas_processing=");
11672 			if (!pos1) {
11673 				wpa_printf(MSG_ERROR, "tclas_processing empty");
11674 				goto free_scs_desc;
11675 			}
11676 
11677 			val = atoi(pos1 + 17);
11678 			if (val != 0 && val != 1) {
11679 				wpa_printf(MSG_ERROR,
11680 					   "tclas_processing invalid");
11681 				goto free_scs_desc;
11682 			}
11683 
11684 			desc_elem.tclas_processing = val;
11685 		}
11686 
11687 scs_desc_end:
11688 		n1 = os_realloc(scs_data->scs_desc_elems, (num_scs_desc + 1) *
11689 				sizeof(struct scs_desc_elem));
11690 		if (!n1)
11691 			goto free_scs_desc;
11692 
11693 		scs_data->scs_desc_elems = n1;
11694 		os_memcpy((u8 *) scs_data->scs_desc_elems + num_scs_desc *
11695 			  sizeof(desc_elem), &desc_elem, sizeof(desc_elem));
11696 		num_scs_desc++;
11697 		scs_data->num_scs_desc = num_scs_desc;
11698 		pos1 = next_scs_desc;
11699 		os_memset(&desc_elem, 0, sizeof(desc_elem));
11700 	}
11701 
11702 	return wpas_send_scs_req(wpa_s);
11703 
11704 free_scs_desc:
11705 	free_up_tclas_elem(&desc_elem);
11706 	free_up_scs_desc(scs_data);
11707 	return -1;
11708 }
11709 
11710 
wpas_ctrl_iface_send_dscp_resp(struct wpa_supplicant * wpa_s,const char * cmd)11711 static int wpas_ctrl_iface_send_dscp_resp(struct wpa_supplicant *wpa_s,
11712 					  const char *cmd)
11713 {
11714 	char *pos;
11715 	struct dscp_policy_status *policy = NULL, *n;
11716 	int num_policies = 0, ret = -1;
11717 	struct dscp_resp_data resp_data;
11718 
11719 	/*
11720 	 * format:
11721 	 * <[reset]>/<[solicited] [policy_id=1 status=0...]> [more]
11722 	 */
11723 
11724 	os_memset(&resp_data, 0, sizeof(resp_data));
11725 
11726 	resp_data.more = os_strstr(cmd, "more") != NULL;
11727 
11728 	if (os_strstr(cmd, "reset")) {
11729 		resp_data.reset = true;
11730 		resp_data.solicited = false;
11731 		goto send_resp;
11732 	}
11733 
11734 	resp_data.solicited = os_strstr(cmd, "solicited") != NULL;
11735 
11736 	pos = os_strstr(cmd, "policy_id=");
11737 	while (pos) {
11738 		n = os_realloc(policy, (num_policies + 1) * sizeof(*policy));
11739 		if (!n)
11740 			goto fail;
11741 
11742 		policy = n;
11743 		pos += 10;
11744 		policy[num_policies].id = atoi(pos);
11745 		if (policy[num_policies].id == 0) {
11746 			wpa_printf(MSG_ERROR, "DSCP: Invalid policy id");
11747 			goto fail;
11748 		}
11749 
11750 		pos = os_strstr(pos, "status=");
11751 		if (!pos) {
11752 			wpa_printf(MSG_ERROR,
11753 				   "DSCP: Status is not found for a policy");
11754 			goto fail;
11755 		}
11756 
11757 		pos += 7;
11758 		policy[num_policies].status = atoi(pos);
11759 		num_policies++;
11760 
11761 		pos = os_strstr(pos, "policy_id");
11762 	}
11763 
11764 	resp_data.policy = policy;
11765 	resp_data.num_policies = num_policies;
11766 send_resp:
11767 	ret = wpas_send_dscp_response(wpa_s, &resp_data);
11768 	if (ret)
11769 		wpa_printf(MSG_ERROR, "DSCP: Failed to send DSCP response");
11770 fail:
11771 	os_free(policy);
11772 	return ret;
11773 }
11774 
11775 
wpas_ctrl_iface_send_dscp_query(struct wpa_supplicant * wpa_s,const char * cmd)11776 static int wpas_ctrl_iface_send_dscp_query(struct wpa_supplicant *wpa_s,
11777 					   const char *cmd)
11778 {
11779 	char *pos;
11780 
11781 	/*
11782 	 * format:
11783 	 * Wildcard DSCP query
11784 	 * <wildcard>
11785 	 *
11786 	 * DSCP query with a domain name attribute:
11787 	 * [domain_name=<string>]
11788 	 */
11789 
11790 	if (os_strstr(cmd, "wildcard")) {
11791 		wpa_printf(MSG_DEBUG, "QM: Send wildcard DSCP policy query");
11792 		return wpas_send_dscp_query(wpa_s, NULL, 0);
11793 	}
11794 
11795 	pos = os_strstr(cmd, "domain_name=");
11796 	if (!pos || !os_strlen(pos + 12)) {
11797 		wpa_printf(MSG_ERROR, "QM: Domain name not preset");
11798 		return -1;
11799 	}
11800 
11801 	return wpas_send_dscp_query(wpa_s, pos + 12, os_strlen(pos + 12));
11802 }
11803 
11804 
wpas_ctrl_iface_mlo_signal_poll(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)11805 static int wpas_ctrl_iface_mlo_signal_poll(struct wpa_supplicant *wpa_s,
11806 					   char *buf, size_t buflen)
11807 {
11808 	int ret, i;
11809 	char *pos, *end;
11810 	struct wpa_mlo_signal_info mlo_si;
11811 
11812 	if (!wpa_s->valid_links)
11813 		return -1;
11814 
11815 	ret = wpa_drv_mlo_signal_poll(wpa_s, &mlo_si);
11816 	if (ret)
11817 		return -1;
11818 
11819 	pos = buf;
11820 	end = buf + buflen;
11821 
11822 	for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
11823 		if (!(mlo_si.valid_links & BIT(i)))
11824 			continue;
11825 
11826 		ret = os_snprintf(pos, end - pos,
11827 				  "LINK_ID=%d\nRSSI=%d\nLINKSPEED=%lu\n"
11828 				  "NOISE=%d\nFREQUENCY=%u\n",
11829 				  i, mlo_si.links[i].data.signal,
11830 				  mlo_si.links[i].data.current_tx_rate / 1000,
11831 				  mlo_si.links[i].current_noise,
11832 				  mlo_si.links[i].frequency);
11833 		if (os_snprintf_error(end - pos, ret))
11834 			return -1;
11835 		pos += ret;
11836 
11837 		if (mlo_si.links[i].chanwidth != CHAN_WIDTH_UNKNOWN) {
11838 			ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
11839 					  channel_width_to_string(
11840 						  mlo_si.links[i].chanwidth));
11841 			if (os_snprintf_error(end - pos, ret))
11842 				return -1;
11843 			pos += ret;
11844 		}
11845 
11846 		if (mlo_si.links[i].center_frq1 > 0) {
11847 			ret = os_snprintf(pos, end - pos, "CENTER_FRQ1=%d\n",
11848 					  mlo_si.links[i].center_frq1);
11849 			if (os_snprintf_error(end - pos, ret))
11850 				return -1;
11851 			pos += ret;
11852 		}
11853 
11854 		if (mlo_si.links[i].center_frq2 > 0) {
11855 			ret = os_snprintf(pos, end - pos, "CENTER_FRQ2=%d\n",
11856 					  mlo_si.links[i].center_frq2);
11857 			if (os_snprintf_error(end - pos, ret))
11858 				return -1;
11859 			pos += ret;
11860 		}
11861 
11862 		if (mlo_si.links[i].data.avg_signal) {
11863 			ret = os_snprintf(pos, end - pos,
11864 					  "AVG_RSSI=%d\n",
11865 					  mlo_si.links[i].data.avg_signal);
11866 			if (os_snprintf_error(end - pos, ret))
11867 				return -1;
11868 			pos += ret;
11869 		}
11870 
11871 		if (mlo_si.links[i].data.avg_beacon_signal) {
11872 			ret = os_snprintf(
11873 				pos, end - pos, "AVG_BEACON_RSSI=%d\n",
11874 				mlo_si.links[i].data.avg_beacon_signal);
11875 			if (os_snprintf_error(end - pos, ret))
11876 				return -1;
11877 			pos += ret;
11878 		}
11879 	}
11880 
11881 	return pos - buf;
11882 }
11883 
11884 
wpas_ctrl_iface_mlo_status(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)11885 static int wpas_ctrl_iface_mlo_status(struct wpa_supplicant *wpa_s,
11886 				      char *buf, size_t buflen)
11887 {
11888 	int ret, i;
11889 	char *pos, *end;
11890 
11891 	if (!wpa_s->valid_links)
11892 		return -1;
11893 
11894 	pos = buf;
11895 	end = buf + buflen;
11896 
11897 	for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
11898 		if (!(wpa_s->valid_links & BIT(i)))
11899 			continue;
11900 
11901 		ret = os_snprintf(pos, end - pos, "link_id=%d\nfreq=%u\n"
11902 				  "ap_link_addr=" MACSTR
11903 				  "\nsta_link_addr=" MACSTR "\n",
11904 				  i, wpa_s->links[i].freq,
11905 				  MAC2STR(wpa_s->links[i].bssid),
11906 				  MAC2STR(wpa_s->links[i].addr));
11907 		if (os_snprintf_error(end - pos, ret))
11908 			return pos - buf;
11909 		pos += ret;
11910 	}
11911 
11912 	return pos - buf;
11913 }
11914 
11915 
wpa_supplicant_ctrl_iface_process(struct wpa_supplicant * wpa_s,char * buf,size_t * resp_len)11916 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
11917 					 char *buf, size_t *resp_len)
11918 {
11919 	char *reply;
11920 	const int reply_size = 4096;
11921 	int reply_len;
11922 
11923 	if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
11924 	    os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
11925 	    os_strncmp(buf, "PMKSA_ADD ", 10) == 0 ||
11926 	    os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
11927 		if (wpa_debug_show_keys)
11928 			wpa_dbg(wpa_s, MSG_DEBUG,
11929 				"Control interface command '%s'", buf);
11930 		else
11931 			wpa_dbg(wpa_s, MSG_DEBUG,
11932 				"Control interface command '%s [REMOVED]'",
11933 				os_strncmp(buf, WPA_CTRL_RSP,
11934 					   os_strlen(WPA_CTRL_RSP)) == 0 ?
11935 				WPA_CTRL_RSP :
11936 				(os_strncmp(buf, "SET_NETWORK ", 12) == 0 ?
11937 				 "SET_NETWORK" : "key-add"));
11938 	} else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
11939 		   os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
11940 		wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
11941 				      (const u8 *) buf, os_strlen(buf));
11942 	} else {
11943 		int level = wpas_ctrl_cmd_debug_level(buf);
11944 		wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
11945 	}
11946 
11947 	reply = os_malloc(reply_size);
11948 	if (reply == NULL) {
11949 		*resp_len = 1;
11950 		return NULL;
11951 	}
11952 
11953 	os_memcpy(reply, "OK\n", 3);
11954 	reply_len = 3;
11955 
11956 	if (os_strcmp(buf, "PING") == 0) {
11957 		os_memcpy(reply, "PONG\n", 5);
11958 		reply_len = 5;
11959 	} else if (os_strcmp(buf, "IFNAME") == 0) {
11960 		reply_len = os_strlen(wpa_s->ifname);
11961 		os_memcpy(reply, wpa_s->ifname, reply_len);
11962 	} else if (os_strncmp(buf, "RELOG", 5) == 0) {
11963 		if (wpa_debug_reopen_file() < 0)
11964 			reply_len = -1;
11965 	} else if (os_strncmp(buf, "NOTE ", 5) == 0) {
11966 		wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
11967 	} else if (os_strcmp(buf, "MIB") == 0) {
11968 		reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
11969 		if (reply_len >= 0) {
11970 			reply_len += eapol_sm_get_mib(wpa_s->eapol,
11971 						      reply + reply_len,
11972 						      reply_size - reply_len);
11973 #ifdef CONFIG_MACSEC
11974 			reply_len += ieee802_1x_kay_get_mib(
11975 				wpa_s->kay, reply + reply_len,
11976 				reply_size - reply_len);
11977 #endif /* CONFIG_MACSEC */
11978 		}
11979 	} else if (os_strncmp(buf, "STATUS", 6) == 0) {
11980 		reply_len = wpa_supplicant_ctrl_iface_status(
11981 			wpa_s, buf + 6, reply, reply_size);
11982 	} else if (os_strcmp(buf, "PMKSA") == 0) {
11983 		reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
11984 	} else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
11985 		wpas_ctrl_iface_pmksa_flush(wpa_s);
11986 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
11987 	} else if (os_strncmp(buf, "PMKSA_GET ", 10) == 0) {
11988 		reply_len = wpas_ctrl_iface_pmksa_get(wpa_s, buf + 10,
11989 						      reply, reply_size);
11990 	} else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
11991 		if (wpas_ctrl_iface_pmksa_add(wpa_s, buf + 10) < 0)
11992 			reply_len = -1;
11993 #ifdef CONFIG_MESH
11994 	} else if (os_strncmp(buf, "MESH_PMKSA_GET ", 15) == 0) {
11995 		reply_len = wpas_ctrl_iface_mesh_pmksa_get(wpa_s, buf + 15,
11996 							   reply, reply_size);
11997 	} else if (os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
11998 		if (wpas_ctrl_iface_mesh_pmksa_add(wpa_s, buf + 15) < 0)
11999 			reply_len = -1;
12000 #endif /* CONFIG_MESH */
12001 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
12002 	} else if (os_strncmp(buf, "SET ", 4) == 0) {
12003 		if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
12004 			reply_len = -1;
12005 	} else if (os_strncmp(buf, "DUMP", 4) == 0) {
12006 		reply_len = wpa_config_dump_values(wpa_s->conf,
12007 						   reply, reply_size);
12008 	} else if (os_strncmp(buf, "GET ", 4) == 0) {
12009 		reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
12010 							  reply, reply_size);
12011 	} else if (os_strcmp(buf, "LOGON") == 0) {
12012 		eapol_sm_notify_logoff(wpa_s->eapol, false);
12013 	} else if (os_strcmp(buf, "LOGOFF") == 0) {
12014 		eapol_sm_notify_logoff(wpa_s->eapol, true);
12015 	} else if (os_strcmp(buf, "REASSOCIATE") == 0) {
12016 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
12017 			reply_len = -1;
12018 		else
12019 			wpas_request_connection(wpa_s);
12020 	} else if (os_strcmp(buf, "REATTACH") == 0) {
12021 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
12022 		    !wpa_s->current_ssid)
12023 			reply_len = -1;
12024 		else {
12025 			wpa_s->reattach = 1;
12026 			wpas_request_connection(wpa_s);
12027 		}
12028 	} else if (os_strcmp(buf, "RECONNECT") == 0) {
12029 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
12030 			reply_len = -1;
12031 		else if (wpa_s->disconnected)
12032 			wpas_request_connection(wpa_s);
12033 #ifdef IEEE8021X_EAPOL
12034 	} else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
12035 		if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
12036 			reply_len = -1;
12037 #endif /* IEEE8021X_EAPOL */
12038 #ifdef CONFIG_IEEE80211R
12039 	} else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
12040 		if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
12041 			reply_len = -1;
12042 #endif /* CONFIG_IEEE80211R */
12043 #ifdef CONFIG_WPS
12044 	} else if (os_strcmp(buf, "WPS_PBC") == 0) {
12045 		int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
12046 		if (res == -2) {
12047 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
12048 			reply_len = 17;
12049 		} else if (res)
12050 			reply_len = -1;
12051 	} else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
12052 		int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
12053 		if (res == -2) {
12054 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
12055 			reply_len = 17;
12056 		} else if (res)
12057 			reply_len = -1;
12058 	} else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
12059 		reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
12060 							      reply,
12061 							      reply_size);
12062 	} else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
12063 		reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
12064 			wpa_s, buf + 14, reply, reply_size);
12065 	} else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
12066 		if (wpas_wps_cancel(wpa_s))
12067 			reply_len = -1;
12068 #ifdef CONFIG_WPS_NFC
12069 	} else if (os_strcmp(buf, "WPS_NFC") == 0) {
12070 		if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
12071 			reply_len = -1;
12072 	} else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
12073 		if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
12074 			reply_len = -1;
12075 	} else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
12076 		reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
12077 			wpa_s, buf + 21, reply, reply_size);
12078 	} else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
12079 		reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
12080 			wpa_s, buf + 14, reply, reply_size);
12081 	} else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
12082 		if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
12083 							       buf + 17))
12084 			reply_len = -1;
12085 	} else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
12086 		reply_len = wpas_ctrl_nfc_get_handover_req(
12087 			wpa_s, buf + 21, reply, reply_size);
12088 	} else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
12089 		reply_len = wpas_ctrl_nfc_get_handover_sel(
12090 			wpa_s, buf + 21, reply, reply_size);
12091 	} else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
12092 		if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
12093 			reply_len = -1;
12094 #endif /* CONFIG_WPS_NFC */
12095 	} else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
12096 		if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
12097 			reply_len = -1;
12098 #ifdef CONFIG_AP
12099 	} else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
12100 		reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
12101 			wpa_s, buf + 11, reply, reply_size);
12102 #endif /* CONFIG_AP */
12103 #ifdef CONFIG_WPS_ER
12104 	} else if (os_strcmp(buf, "WPS_ER_START") == 0) {
12105 		if (wpas_wps_er_start(wpa_s, NULL))
12106 			reply_len = -1;
12107 	} else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
12108 		if (wpas_wps_er_start(wpa_s, buf + 13))
12109 			reply_len = -1;
12110 	} else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
12111 		wpas_wps_er_stop(wpa_s);
12112 	} else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
12113 		if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
12114 			reply_len = -1;
12115 	} else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
12116 		int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
12117 		if (ret == -2) {
12118 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
12119 			reply_len = 17;
12120 		} else if (ret == -3) {
12121 			os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
12122 			reply_len = 18;
12123 		} else if (ret == -4) {
12124 			os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
12125 			reply_len = 20;
12126 		} else if (ret)
12127 			reply_len = -1;
12128 	} else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
12129 		if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
12130 			reply_len = -1;
12131 	} else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
12132 		if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
12133 								buf + 18))
12134 			reply_len = -1;
12135 	} else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
12136 		if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
12137 			reply_len = -1;
12138 #ifdef CONFIG_WPS_NFC
12139 	} else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
12140 		reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
12141 			wpa_s, buf + 24, reply, reply_size);
12142 #endif /* CONFIG_WPS_NFC */
12143 #endif /* CONFIG_WPS_ER */
12144 #endif /* CONFIG_WPS */
12145 #ifdef CONFIG_IBSS_RSN
12146 	} else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
12147 		if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
12148 			reply_len = -1;
12149 #endif /* CONFIG_IBSS_RSN */
12150 #ifdef CONFIG_MESH
12151 	} else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
12152 		reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
12153 			wpa_s, buf + 19, reply, reply_size);
12154 	} else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
12155 		reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
12156 			wpa_s, "", reply, reply_size);
12157 	} else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
12158 		if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
12159 			reply_len = -1;
12160 	} else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
12161 		if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
12162 								buf + 18))
12163 			reply_len = -1;
12164 	} else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
12165 		if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
12166 			reply_len = -1;
12167 	} else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
12168 		if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
12169 			reply_len = -1;
12170 	} else if (os_strncmp(buf, "MESH_LINK_PROBE ", 16) == 0) {
12171 		if (wpa_supplicant_ctrl_iface_mesh_link_probe(wpa_s, buf + 16))
12172 			reply_len = -1;
12173 #endif /* CONFIG_MESH */
12174 #ifdef CONFIG_P2P
12175 	} else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
12176 		if (p2p_ctrl_find(wpa_s, buf + 8))
12177 			reply_len = -1;
12178 	} else if (os_strcmp(buf, "P2P_FIND") == 0) {
12179 		if (p2p_ctrl_find(wpa_s, ""))
12180 			reply_len = -1;
12181 	} else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
12182 		wpas_p2p_stop_find(wpa_s);
12183 	} else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
12184 		if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
12185 			reply_len = -1;
12186 	} else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
12187 		if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
12188 			reply_len = -1;
12189 	} else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
12190 		reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
12191 					     reply_size);
12192 	} else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
12193 		if (p2p_ctrl_listen(wpa_s, buf + 11))
12194 			reply_len = -1;
12195 	} else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
12196 		if (p2p_ctrl_listen(wpa_s, ""))
12197 			reply_len = -1;
12198 	} else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
12199 		if (wpas_p2p_group_remove(wpa_s, buf + 17))
12200 			reply_len = -1;
12201 	} else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
12202 		if (p2p_ctrl_group_add(wpa_s, ""))
12203 			reply_len = -1;
12204 	} else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
12205 		if (p2p_ctrl_group_add(wpa_s, buf + 14))
12206 			reply_len = -1;
12207 	} else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
12208 		reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
12209 						  reply_size);
12210 	} else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
12211 		if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
12212 			reply_len = -1;
12213 	} else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
12214 		reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
12215 	} else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
12216 		reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
12217 						   reply_size);
12218 	} else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
12219 		if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
12220 			reply_len = -1;
12221 	} else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
12222 		if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
12223 			reply_len = -1;
12224 	} else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
12225 		wpas_p2p_sd_service_update(wpa_s);
12226 	} else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
12227 		if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
12228 			reply_len = -1;
12229 	} else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
12230 		wpas_p2p_service_flush(wpa_s);
12231 	} else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
12232 		if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
12233 			reply_len = -1;
12234 	} else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
12235 		if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
12236 			reply_len = -1;
12237 	} else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
12238 		if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
12239 			reply_len = -1;
12240 	} else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
12241 		if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
12242 			reply_len = -1;
12243 	} else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
12244 		if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
12245 			reply_len = -1;
12246 	} else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
12247 		reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
12248 					      reply_size);
12249 	} else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
12250 		if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
12251 			reply_len = -1;
12252 	} else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
12253 		p2p_ctrl_flush(wpa_s);
12254 	} else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
12255 		if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
12256 			reply_len = -1;
12257 	} else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
12258 		if (wpas_p2p_cancel(wpa_s))
12259 			reply_len = -1;
12260 	} else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
12261 		if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
12262 			reply_len = -1;
12263 	} else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
12264 		if (p2p_ctrl_presence_req(wpa_s, "") < 0)
12265 			reply_len = -1;
12266 	} else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
12267 		if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
12268 			reply_len = -1;
12269 	} else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
12270 		if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
12271 			reply_len = -1;
12272 	} else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
12273 		if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
12274 			reply_len = -1;
12275 	} else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
12276 		if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
12277 			reply_len = -1;
12278 	} else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
12279 		if (wpas_p2p_lo_stop(wpa_s))
12280 			reply_len = -1;
12281 #endif /* CONFIG_P2P */
12282 #ifdef CONFIG_WIFI_DISPLAY
12283 	} else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
12284 		if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
12285 			reply_len = -1;
12286 	} else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
12287 		reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
12288 						     reply, reply_size);
12289 #endif /* CONFIG_WIFI_DISPLAY */
12290 #ifdef CONFIG_INTERWORKING
12291 	} else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
12292 		if (interworking_fetch_anqp(wpa_s) < 0)
12293 			reply_len = -1;
12294 	} else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
12295 		interworking_stop_fetch_anqp(wpa_s);
12296 	} else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
12297 		if (ctrl_interworking_select(wpa_s, NULL) < 0)
12298 			reply_len = -1;
12299 	} else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
12300 		if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
12301 			reply_len = -1;
12302 	} else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
12303 		if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
12304 			reply_len = -1;
12305 	} else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
12306 		int id;
12307 
12308 		id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
12309 		if (id < 0)
12310 			reply_len = -1;
12311 		else {
12312 			reply_len = os_snprintf(reply, reply_size, "%d\n", id);
12313 			if (os_snprintf_error(reply_size, reply_len))
12314 				reply_len = -1;
12315 		}
12316 	} else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
12317 		if (get_anqp(wpa_s, buf + 9) < 0)
12318 			reply_len = -1;
12319 	} else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
12320 		if (gas_request(wpa_s, buf + 12) < 0)
12321 			reply_len = -1;
12322 	} else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
12323 		reply_len = gas_response_get(wpa_s, buf + 17, reply,
12324 					     reply_size);
12325 #endif /* CONFIG_INTERWORKING */
12326 #ifdef CONFIG_HS20
12327 	} else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
12328 		if (get_hs20_anqp(wpa_s, buf + 14) < 0)
12329 			reply_len = -1;
12330 	} else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
12331 		if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
12332 			reply_len = -1;
12333 	} else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
12334 		if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
12335 			reply_len = -1;
12336 	} else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
12337 		if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
12338 			reply_len = -1;
12339 	} else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
12340 		reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
12341 	} else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
12342 		if (del_hs20_icon(wpa_s, buf + 14) < 0)
12343 			reply_len = -1;
12344 	} else if (os_strcmp(buf, "FETCH_OSU") == 0) {
12345 		if (hs20_fetch_osu(wpa_s, 0) < 0)
12346 			reply_len = -1;
12347 	} else if (os_strcmp(buf, "FETCH_OSU no-scan") == 0) {
12348 		if (hs20_fetch_osu(wpa_s, 1) < 0)
12349 			reply_len = -1;
12350 	} else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
12351 		hs20_cancel_fetch_osu(wpa_s);
12352 #endif /* CONFIG_HS20 */
12353 	} else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
12354 	{
12355 		if (wpa_supplicant_ctrl_iface_ctrl_rsp(
12356 			    wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
12357 			reply_len = -1;
12358 		else {
12359 			/*
12360 			 * Notify response from timeout to allow the control
12361 			 * interface response to be sent first.
12362 			 */
12363 			eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
12364 					       wpa_s, NULL);
12365 		}
12366 	} else if (os_strcmp(buf, "RECONFIGURE") == 0) {
12367 		if (wpa_supplicant_reload_configuration(wpa_s))
12368 			reply_len = -1;
12369 	} else if (os_strcmp(buf, "TERMINATE") == 0) {
12370 		wpa_supplicant_terminate_proc(wpa_s->global);
12371 	} else if (os_strncmp(buf, "BSSID ", 6) == 0) {
12372 		if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
12373 			reply_len = -1;
12374 	} else if (os_strncmp(buf, "BSSID_IGNORE", 12) == 0) {
12375 		reply_len = wpa_supplicant_ctrl_iface_bssid_ignore(
12376 			wpa_s, buf + 12, reply, reply_size);
12377 	} else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
12378 		/* deprecated backwards compatibility alias for BSSID_IGNORE */
12379 		reply_len = wpa_supplicant_ctrl_iface_bssid_ignore(
12380 			wpa_s, buf + 9, reply, reply_size);
12381 	} else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
12382 		reply_len = wpa_supplicant_ctrl_iface_log_level(
12383 			wpa_s, buf + 9, reply, reply_size);
12384 	} else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
12385 		reply_len = wpa_supplicant_ctrl_iface_list_networks(
12386 			wpa_s, buf + 14, reply, reply_size);
12387 	} else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
12388 		reply_len = wpa_supplicant_ctrl_iface_list_networks(
12389 			wpa_s, NULL, reply, reply_size);
12390 	} else if (os_strcmp(buf, "DISCONNECT") == 0) {
12391 		wpas_request_disconnection(wpa_s);
12392 	} else if (os_strcmp(buf, "SCAN") == 0) {
12393 		wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
12394 	} else if (os_strncmp(buf, "SCAN ", 5) == 0) {
12395 		wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
12396 	} else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
12397 		reply_len = wpa_supplicant_ctrl_iface_scan_results(
12398 			wpa_s, reply, reply_size);
12399 	} else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
12400 		if (wpas_abort_ongoing_scan(wpa_s) < 0)
12401 			reply_len = -1;
12402 	} else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
12403 		if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
12404 			reply_len = -1;
12405 	} else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
12406 		if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
12407 			reply_len = -1;
12408 	} else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
12409 		if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
12410 			reply_len = -1;
12411 	} else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
12412 		reply_len = wpa_supplicant_ctrl_iface_add_network(
12413 			wpa_s, reply, reply_size);
12414 	} else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
12415 		if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
12416 			reply_len = -1;
12417 	} else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
12418 		if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
12419 			reply_len = -1;
12420 	} else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
12421 		reply_len = wpa_supplicant_ctrl_iface_get_network(
12422 			wpa_s, buf + 12, reply, reply_size);
12423 	} else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
12424 		if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
12425 							  wpa_s))
12426 			reply_len = -1;
12427 	} else if (os_strcmp(buf, "LIST_CREDS") == 0) {
12428 		reply_len = wpa_supplicant_ctrl_iface_list_creds(
12429 			wpa_s, reply, reply_size);
12430 	} else if (os_strcmp(buf, "ADD_CRED") == 0) {
12431 		reply_len = wpa_supplicant_ctrl_iface_add_cred(
12432 			wpa_s, reply, reply_size);
12433 	} else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
12434 		if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
12435 			reply_len = -1;
12436 	} else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
12437 		if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
12438 			reply_len = -1;
12439 	} else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
12440 		reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
12441 							       reply,
12442 							       reply_size);
12443 #ifndef CONFIG_NO_CONFIG_WRITE
12444 	} else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
12445 		if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
12446 			reply_len = -1;
12447 #endif /* CONFIG_NO_CONFIG_WRITE */
12448 	} else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
12449 		reply_len = wpa_supplicant_ctrl_iface_get_capability(
12450 			wpa_s, buf + 15, reply, reply_size);
12451 	} else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
12452 		if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
12453 			reply_len = -1;
12454 	} else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
12455 		if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
12456 			reply_len = -1;
12457 	} else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
12458 		reply_len = wpa_supplicant_global_iface_list(
12459 			wpa_s->global, reply, reply_size);
12460 	} else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
12461 		reply_len = wpa_supplicant_global_iface_interfaces(
12462 			wpa_s->global, buf + 10, reply, reply_size);
12463 	} else if (os_strncmp(buf, "BSS ", 4) == 0) {
12464 		reply_len = wpa_supplicant_ctrl_iface_bss(
12465 			wpa_s, buf + 4, reply, reply_size);
12466 #ifdef CONFIG_AP
12467 	} else if (os_strcmp(buf, "STA-FIRST") == 0) {
12468 		reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
12469 	} else if (os_strncmp(buf, "STA ", 4) == 0) {
12470 		reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
12471 					      reply_size);
12472 	} else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
12473 		reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
12474 						   reply_size);
12475 	} else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
12476 		if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
12477 			reply_len = -1;
12478 	} else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
12479 		if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
12480 			reply_len = -1;
12481 	} else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
12482 		if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
12483 			reply_len = -1;
12484 	} else if (os_strcmp(buf, "STOP_AP") == 0) {
12485 		if (wpas_ap_stop_ap(wpa_s))
12486 			reply_len = -1;
12487 	} else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
12488 		if (wpas_ap_update_beacon(wpa_s))
12489 			reply_len = -1;
12490 	} else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
12491 		if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
12492 			if (ap_ctrl_iface_acl_add_mac(wpa_s,
12493 						      DENY_UNLESS_ACCEPTED,
12494 						      buf + 19) ||
12495 			    ap_ctrl_iface_set_acl(wpa_s))
12496 				reply_len = -1;
12497 		} else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
12498 			if (ap_ctrl_iface_acl_del_mac(wpa_s,
12499 						      DENY_UNLESS_ACCEPTED,
12500 						      buf + 19) ||
12501 			    ap_ctrl_iface_set_acl(wpa_s) ||
12502 			    ap_ctrl_iface_disassoc_accept_mac(wpa_s))
12503 				reply_len = -1;
12504 		} else if (os_strcmp(buf + 11, "SHOW") == 0) {
12505 			reply_len = ap_ctrl_iface_acl_show_mac(
12506 				wpa_s, DENY_UNLESS_ACCEPTED,
12507 				reply, reply_size);
12508 		} else if (os_strcmp(buf + 11, "CLEAR") == 0) {
12509 			ap_ctrl_iface_acl_clear_list(wpa_s,
12510 						     DENY_UNLESS_ACCEPTED);
12511 			if (ap_ctrl_iface_set_acl(wpa_s) ||
12512 			    ap_ctrl_iface_disassoc_accept_mac(wpa_s))
12513 				reply_len = -1;
12514 		} else {
12515 			reply_len = -1;
12516 		}
12517 	} else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
12518 		if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
12519 			if (ap_ctrl_iface_acl_add_mac(wpa_s,
12520 						      ACCEPT_UNLESS_DENIED,
12521 						      buf + 17) ||
12522 			    ap_ctrl_iface_set_acl(wpa_s) ||
12523 			    ap_ctrl_iface_disassoc_deny_mac(wpa_s))
12524 				reply_len = -1;
12525 		} else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
12526 			if (ap_ctrl_iface_acl_del_mac(wpa_s,
12527 						      ACCEPT_UNLESS_DENIED,
12528 						      buf + 17) ||
12529 			    ap_ctrl_iface_set_acl(wpa_s))
12530 				reply_len = -1;
12531 		} else if (os_strcmp(buf + 9, "SHOW") == 0) {
12532 			reply_len = ap_ctrl_iface_acl_show_mac(
12533 				wpa_s, ACCEPT_UNLESS_DENIED, reply, reply_size);
12534 		} else if (os_strcmp(buf + 9, "CLEAR") == 0) {
12535 			ap_ctrl_iface_acl_clear_list(wpa_s,
12536 						     ACCEPT_UNLESS_DENIED);
12537 			if (ap_ctrl_iface_set_acl(wpa_s))
12538 				reply_len = -1;
12539 		} else {
12540 			reply_len = -1;
12541 		}
12542 #endif /* CONFIG_AP */
12543 	} else if (os_strcmp(buf, "SUSPEND") == 0) {
12544 		wpas_notify_suspend(wpa_s->global);
12545 	} else if (os_strcmp(buf, "RESUME") == 0) {
12546 		wpas_notify_resume(wpa_s->global);
12547 #ifdef CONFIG_TESTING_OPTIONS
12548 	} else if (os_strcmp(buf, "DROP_SA") == 0) {
12549 		wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
12550 #endif /* CONFIG_TESTING_OPTIONS */
12551 	} else if (os_strncmp(buf, "ROAM ", 5) == 0) {
12552 		if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
12553 			reply_len = -1;
12554 	} else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
12555 		wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
12556 	} else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
12557 		if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
12558 			reply_len = -1;
12559 	} else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
12560 		if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
12561 							       buf + 17))
12562 			reply_len = -1;
12563 	} else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
12564 		wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
12565 #ifdef CONFIG_TDLS
12566 	} else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
12567 		if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
12568 			reply_len = -1;
12569 	} else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
12570 		if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
12571 			reply_len = -1;
12572 	} else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
12573 		if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
12574 			reply_len = -1;
12575 	} else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
12576 		if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
12577 							       buf + 17))
12578 			reply_len = -1;
12579 	} else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
12580 		if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
12581 								      buf + 24))
12582 			reply_len = -1;
12583 	} else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
12584 		reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
12585 			wpa_s, buf + 17, reply, reply_size);
12586 #endif /* CONFIG_TDLS */
12587 	} else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
12588 		reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
12589 	} else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
12590 		if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
12591 			reply_len = -1;
12592 	} else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
12593 		if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
12594 			reply_len = -1;
12595 	} else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
12596 		reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
12597 						       reply_size);
12598 	} else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
12599 		if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
12600 			reply_len = -1;
12601 	} else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
12602 		reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
12603 						       reply_size);
12604 #ifdef CONFIG_AUTOSCAN
12605 	} else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
12606 		if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
12607 			reply_len = -1;
12608 #endif /* CONFIG_AUTOSCAN */
12609 	} else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
12610 		reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
12611 							 reply_size);
12612 	} else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
12613 		reply_len = wpas_ctrl_iface_driver_flags2(wpa_s, reply,
12614 							  reply_size);
12615 #ifdef ANDROID
12616 	} else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
12617 		reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
12618 						      reply_size);
12619 #endif /* ANDROID */
12620 	} else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
12621 		reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
12622 						      reply_size);
12623 	} else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
12624 		pmksa_cache_clear_current(wpa_s->wpa);
12625 		eapol_sm_request_reauth(wpa_s->eapol);
12626 #ifdef CONFIG_WNM
12627 	} else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
12628 		if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
12629 			reply_len = -1;
12630 	} else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
12631 		if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
12632 				reply_len = -1;
12633 	} else if (os_strncmp(buf, "COLOC_INTF_REPORT ", 18) == 0) {
12634 		if (wpas_ctrl_iface_coloc_intf_report(wpa_s, buf + 18))
12635 			reply_len = -1;
12636 #endif /* CONFIG_WNM */
12637 #ifdef CONFIG_WNM_AP
12638 	} else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
12639 		if (ap_ctrl_iface_disassoc_imminent(wpa_s, buf + 18))
12640 			reply_len = -1;
12641 	} else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
12642 		if (ap_ctrl_iface_ess_disassoc(wpa_s, buf + 13))
12643 			reply_len = -1;
12644 	} else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
12645 		if (ap_ctrl_iface_bss_tm_req(wpa_s, buf + 11))
12646 			reply_len = -1;
12647 #endif /* CONFIG_WNM_AP */
12648 	} else if (os_strcmp(buf, "FLUSH") == 0) {
12649 		wpa_supplicant_ctrl_iface_flush(wpa_s);
12650 	} else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
12651 		reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
12652 						 reply_size);
12653 #ifdef CONFIG_TESTING_OPTIONS
12654 	} else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
12655 		if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
12656 			reply_len = -1;
12657 	} else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
12658 		wpas_ctrl_iface_mgmt_tx_done(wpa_s);
12659 	} else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
12660 		if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
12661 			reply_len = -1;
12662 	} else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
12663 		if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
12664 			reply_len = -1;
12665 	} else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
12666 		if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
12667 			reply_len = -1;
12668 	} else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
12669 		if (wpas_ctrl_iface_eapol_tx(wpa_s, buf + 9) < 0)
12670 			reply_len = -1;
12671 	} else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
12672 		if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
12673 			reply_len = -1;
12674 	} else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
12675 		if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
12676 			reply_len = -1;
12677 	} else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
12678 		if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
12679 			reply_len = -1;
12680 	} else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
12681 		if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
12682 			reply_len = -1;
12683 	} else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
12684 		reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
12685 	} else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
12686 		if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0)
12687 			reply_len = -1;
12688 	} else if (os_strcmp(buf, "GET_FAIL") == 0) {
12689 		reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size);
12690 	} else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
12691 		if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
12692 			reply_len = -1;
12693 	} else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
12694 		if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
12695 			reply_len = -1;
12696 	} else if (os_strcmp(buf, "RESET_PN") == 0) {
12697 		if (wpas_ctrl_reset_pn(wpa_s) < 0)
12698 			reply_len = -1;
12699 	} else if (os_strncmp(buf, "KEY_REQUEST ", 12) == 0) {
12700 		if (wpas_ctrl_key_request(wpa_s, buf + 12) < 0)
12701 			reply_len = -1;
12702 	} else if (os_strcmp(buf, "RESEND_ASSOC") == 0) {
12703 		if (wpas_ctrl_resend_assoc(wpa_s) < 0)
12704 			reply_len = -1;
12705 	} else if (os_strcmp(buf, "UNPROT_DEAUTH") == 0) {
12706 		sme_event_unprot_disconnect(
12707 			wpa_s, wpa_s->bssid, NULL,
12708 			WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
12709 	} else if (os_strncmp(buf, "TWT_SETUP ", 10) == 0) {
12710 		if (wpas_ctrl_iface_send_twt_setup(wpa_s, buf + 9))
12711 			reply_len = -1;
12712 	} else if (os_strcmp(buf, "TWT_SETUP") == 0) {
12713 		if (wpas_ctrl_iface_send_twt_setup(wpa_s, ""))
12714 			reply_len = -1;
12715 	} else if (os_strncmp(buf, "TWT_TEARDOWN ", 13) == 0) {
12716 		if (wpas_ctrl_iface_send_twt_teardown(wpa_s, buf + 12))
12717 			reply_len = -1;
12718 	} else if (os_strcmp(buf, "TWT_TEARDOWN") == 0) {
12719 		if (wpas_ctrl_iface_send_twt_teardown(wpa_s, ""))
12720 			reply_len = -1;
12721 #endif /* CONFIG_TESTING_OPTIONS */
12722 	} else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
12723 		if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
12724 			reply_len = -1;
12725 	} else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
12726 		reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
12727 						      reply_size);
12728 	} else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
12729 		if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
12730 			reply_len = -1;
12731 	} else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
12732 		if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
12733 			reply_len = -1;
12734 	} else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
12735 		wpas_ctrl_iface_erp_flush(wpa_s);
12736 	} else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
12737 		if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
12738 			reply_len = -1;
12739 	} else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
12740 		reply_len = wpas_ctrl_iface_get_pref_freq_list(
12741 			wpa_s, buf + 19, reply, reply_size);
12742 #ifdef CONFIG_FILS
12743 	} else if (os_strncmp(buf, "FILS_HLP_REQ_ADD ", 17) == 0) {
12744 		if (wpas_ctrl_iface_fils_hlp_req_add(wpa_s, buf + 17))
12745 			reply_len = -1;
12746 	} else if (os_strcmp(buf, "FILS_HLP_REQ_FLUSH") == 0) {
12747 		wpas_flush_fils_hlp_req(wpa_s);
12748 #endif /* CONFIG_FILS */
12749 #ifdef CONFIG_DPP
12750 	} else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
12751 		int res;
12752 
12753 		res = wpas_dpp_qr_code(wpa_s, buf + 12);
12754 		if (res < 0) {
12755 			reply_len = -1;
12756 		} else {
12757 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12758 			if (os_snprintf_error(reply_size, reply_len))
12759 				reply_len = -1;
12760 		}
12761 	} else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
12762 		int res;
12763 
12764 		res = wpas_dpp_nfc_uri(wpa_s, buf + 12);
12765 		if (res < 0) {
12766 			reply_len = -1;
12767 		} else {
12768 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12769 			if (os_snprintf_error(reply_size, reply_len))
12770 				reply_len = -1;
12771 		}
12772 	} else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
12773 		int res;
12774 
12775 		res = wpas_dpp_nfc_handover_req(wpa_s, buf + 20);
12776 		if (res < 0) {
12777 			reply_len = -1;
12778 		} else {
12779 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12780 			if (os_snprintf_error(reply_size, reply_len))
12781 				reply_len = -1;
12782 		}
12783 	} else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
12784 		int res;
12785 
12786 		res = wpas_dpp_nfc_handover_sel(wpa_s, buf + 20);
12787 		if (res < 0) {
12788 			reply_len = -1;
12789 		} else {
12790 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12791 			if (os_snprintf_error(reply_size, reply_len))
12792 				reply_len = -1;
12793 		}
12794 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
12795 		int res;
12796 
12797 		res = dpp_bootstrap_gen(wpa_s->dpp, buf + 18);
12798 		if (res < 0) {
12799 			reply_len = -1;
12800 		} else {
12801 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12802 			if (os_snprintf_error(reply_size, reply_len))
12803 				reply_len = -1;
12804 		}
12805 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
12806 		if (dpp_bootstrap_remove(wpa_s->dpp, buf + 21) < 0)
12807 			reply_len = -1;
12808 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
12809 		const char *uri;
12810 
12811 		uri = dpp_bootstrap_get_uri(wpa_s->dpp, atoi(buf + 22));
12812 		if (!uri) {
12813 			reply_len = -1;
12814 		} else {
12815 			reply_len = os_snprintf(reply, reply_size, "%s", uri);
12816 			if (os_snprintf_error(reply_size, reply_len))
12817 				reply_len = -1;
12818 		}
12819 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
12820 		reply_len = dpp_bootstrap_info(wpa_s->dpp, atoi(buf + 19),
12821 					       reply, reply_size);
12822 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
12823 		if (dpp_bootstrap_set(wpa_s->dpp, atoi(buf + 18),
12824 				      os_strchr(buf + 18, ' ')) < 0)
12825 			reply_len = -1;
12826 	} else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
12827 		if (wpas_dpp_auth_init(wpa_s, buf + 13) < 0)
12828 			reply_len = -1;
12829 	} else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
12830 		if (wpas_dpp_listen(wpa_s, buf + 11) < 0)
12831 			reply_len = -1;
12832 	} else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
12833 		wpas_dpp_stop(wpa_s);
12834 		wpas_dpp_listen_stop(wpa_s);
12835 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
12836 		int res;
12837 
12838 		res = dpp_configurator_add(wpa_s->dpp, buf + 20);
12839 		if (res < 0) {
12840 			reply_len = -1;
12841 		} else {
12842 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12843 			if (os_snprintf_error(reply_size, reply_len))
12844 				reply_len = -1;
12845 		}
12846 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_SET ", 21) == 0) {
12847 		if (dpp_configurator_set(wpa_s->dpp, buf + 20) < 0)
12848 			reply_len = -1;
12849 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
12850 		if (dpp_configurator_remove(wpa_s->dpp, buf + 24) < 0)
12851 			reply_len = -1;
12852 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
12853 		if (wpas_dpp_configurator_sign(wpa_s, buf + 21) < 0)
12854 			reply_len = -1;
12855 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
12856 		reply_len = dpp_configurator_get_key_id(wpa_s->dpp,
12857 							atoi(buf + 25),
12858 							reply, reply_size);
12859 	} else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
12860 		int res;
12861 
12862 		res = wpas_dpp_pkex_add(wpa_s, buf + 12);
12863 		if (res < 0) {
12864 			reply_len = -1;
12865 		} else {
12866 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12867 			if (os_snprintf_error(reply_size, reply_len))
12868 				reply_len = -1;
12869 		}
12870 	} else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
12871 		if (wpas_dpp_pkex_remove(wpa_s, buf + 16) < 0)
12872 			reply_len = -1;
12873 	} else if (os_strncmp(buf, "DPP_CONF_SET ", 13) == 0) {
12874 		if (wpas_dpp_conf_set(wpa_s, buf + 12) < 0)
12875 			reply_len = -1;
12876 #ifdef CONFIG_DPP2
12877 	} else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
12878 		if (wpas_dpp_controller_start(wpa_s, buf + 20) < 0)
12879 			reply_len = -1;
12880 	} else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
12881 		if (wpas_dpp_controller_start(wpa_s, NULL) < 0)
12882 			reply_len = -1;
12883 	} else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
12884 		dpp_controller_stop(wpa_s->dpp);
12885 	} else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
12886 		if (wpas_dpp_chirp(wpa_s, buf + 9) < 0)
12887 			reply_len = -1;
12888 	} else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
12889 		wpas_dpp_chirp_stop(wpa_s);
12890 	} else if (os_strncmp(buf, "DPP_RECONFIG ", 13) == 0) {
12891 		if (wpas_dpp_reconfig(wpa_s, buf + 13) < 0)
12892 			reply_len = -1;
12893 	} else if (os_strncmp(buf, "DPP_CA_SET ", 11) == 0) {
12894 		if (wpas_dpp_ca_set(wpa_s, buf + 10) < 0)
12895 			reply_len = -1;
12896 #endif /* CONFIG_DPP2 */
12897 #ifdef CONFIG_DPP3
12898 	} else if (os_strcmp(buf, "DPP_PUSH_BUTTON") == 0) {
12899 		if (wpas_dpp_push_button(wpa_s, NULL) < 0)
12900 			reply_len = -1;
12901 	} else if (os_strncmp(buf, "DPP_PUSH_BUTTON ", 16) == 0) {
12902 		if (wpas_dpp_push_button(wpa_s, buf + 15) < 0)
12903 			reply_len = -1;
12904 #endif /* CONFIG_DPP3 */
12905 #endif /* CONFIG_DPP */
12906 	} else if (os_strncmp(buf, "MSCS ", 5) == 0) {
12907 		if (wpas_ctrl_iface_configure_mscs(wpa_s, buf + 5))
12908 			reply_len = -1;
12909 #ifdef CONFIG_PASN
12910 	} else if (os_strncmp(buf, "PASN_START ", 11) == 0) {
12911 		if (wpas_ctrl_iface_pasn_start(wpa_s, buf + 11) < 0)
12912 			reply_len = -1;
12913 	} else if (os_strcmp(buf, "PASN_STOP") == 0) {
12914 		wpas_pasn_auth_stop(wpa_s);
12915 	} else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
12916 		reply_len = ptksa_cache_list(wpa_s->ptksa, reply, reply_size);
12917 	} else if (os_strncmp(buf, "PASN_DEAUTH ", 12) == 0) {
12918 		if (wpas_ctrl_iface_pasn_deauthenticate(wpa_s, buf + 12) < 0)
12919 			reply_len = -1;
12920 #endif /* CONFIG_PASN */
12921 	} else if (os_strncmp(buf, "SCS ", 4) == 0) {
12922 		if (wpas_ctrl_iface_configure_scs(wpa_s, buf + 4))
12923 			reply_len = -1;
12924 	} else if (os_strncmp(buf, "DSCP_RESP ", 10) == 0) {
12925 		if (wpas_ctrl_iface_send_dscp_resp(wpa_s, buf + 10))
12926 			reply_len = -1;
12927 	} else if (os_strncmp(buf, "DSCP_QUERY ", 11) == 0) {
12928 		if (wpas_ctrl_iface_send_dscp_query(wpa_s, buf + 11))
12929 			reply_len = -1;
12930 	} else if (os_strcmp(buf, "MLO_STATUS") == 0) {
12931 		reply_len = wpas_ctrl_iface_mlo_status(wpa_s, reply,
12932 						       reply_size);
12933 	} else if (os_strcmp(buf, "MLO_SIGNAL_POLL") == 0) {
12934 		reply_len = wpas_ctrl_iface_mlo_signal_poll(wpa_s, reply,
12935 							    reply_size);
12936 	} else {
12937 		os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
12938 		reply_len = 16;
12939 	}
12940 
12941 	if (reply_len < 0) {
12942 		os_memcpy(reply, "FAIL\n", 5);
12943 		reply_len = 5;
12944 	}
12945 
12946 	*resp_len = reply_len;
12947 	return reply;
12948 }
12949 
12950 
wpa_supplicant_global_iface_add(struct wpa_global * global,char * cmd)12951 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
12952 					   char *cmd)
12953 {
12954 	struct wpa_interface iface;
12955 	char *pos, *extra;
12956 	struct wpa_supplicant *wpa_s;
12957 	unsigned int create_iface = 0;
12958 	u8 mac_addr[ETH_ALEN];
12959 	enum wpa_driver_if_type type = WPA_IF_STATION;
12960 
12961 	/*
12962 	 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
12963 	 * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
12964 	 */
12965 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
12966 
12967 	os_memset(&iface, 0, sizeof(iface));
12968 
12969 	do {
12970 		iface.ifname = pos = cmd;
12971 		pos = os_strchr(pos, '\t');
12972 		if (pos)
12973 			*pos++ = '\0';
12974 		if (iface.ifname[0] == '\0')
12975 			return -1;
12976 		if (pos == NULL)
12977 			break;
12978 
12979 		iface.confname = pos;
12980 		pos = os_strchr(pos, '\t');
12981 		if (pos)
12982 			*pos++ = '\0';
12983 		if (iface.confname[0] == '\0')
12984 			iface.confname = NULL;
12985 		if (pos == NULL)
12986 			break;
12987 
12988 		iface.driver = pos;
12989 		pos = os_strchr(pos, '\t');
12990 		if (pos)
12991 			*pos++ = '\0';
12992 		if (iface.driver[0] == '\0')
12993 			iface.driver = NULL;
12994 		if (pos == NULL)
12995 			break;
12996 
12997 		iface.ctrl_interface = pos;
12998 		pos = os_strchr(pos, '\t');
12999 		if (pos)
13000 			*pos++ = '\0';
13001 		if (iface.ctrl_interface[0] == '\0')
13002 			iface.ctrl_interface = NULL;
13003 		if (pos == NULL)
13004 			break;
13005 
13006 		iface.driver_param = pos;
13007 		pos = os_strchr(pos, '\t');
13008 		if (pos)
13009 			*pos++ = '\0';
13010 		if (iface.driver_param[0] == '\0')
13011 			iface.driver_param = NULL;
13012 		if (pos == NULL)
13013 			break;
13014 
13015 		iface.bridge_ifname = pos;
13016 		pos = os_strchr(pos, '\t');
13017 		if (pos)
13018 			*pos++ = '\0';
13019 		if (iface.bridge_ifname[0] == '\0')
13020 			iface.bridge_ifname = NULL;
13021 		if (pos == NULL)
13022 			break;
13023 
13024 		extra = pos;
13025 		pos = os_strchr(pos, '\t');
13026 		if (pos)
13027 			*pos++ = '\0';
13028 		if (!extra[0])
13029 			break;
13030 
13031 		if (os_strcmp(extra, "create") == 0) {
13032 			create_iface = 1;
13033 			if (!pos)
13034 				break;
13035 
13036 			if (os_strcmp(pos, "sta") == 0) {
13037 				type = WPA_IF_STATION;
13038 			} else if (os_strcmp(pos, "ap") == 0) {
13039 				type = WPA_IF_AP_BSS;
13040 			} else {
13041 				wpa_printf(MSG_DEBUG,
13042 					   "INTERFACE_ADD unsupported interface type: '%s'",
13043 					   pos);
13044 				return -1;
13045 			}
13046 		} else {
13047 			wpa_printf(MSG_DEBUG,
13048 				   "INTERFACE_ADD unsupported extra parameter: '%s'",
13049 				   extra);
13050 			return -1;
13051 		}
13052 	} while (0);
13053 
13054 	if (create_iface) {
13055 		wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
13056 			   iface.ifname);
13057 		if (!global->ifaces)
13058 			return -1;
13059 		if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
13060 				   NULL, NULL, NULL, mac_addr, NULL) < 0) {
13061 			wpa_printf(MSG_ERROR,
13062 				   "CTRL_IFACE interface creation failed");
13063 			return -1;
13064 		}
13065 
13066 		wpa_printf(MSG_DEBUG,
13067 			   "CTRL_IFACE interface '%s' created with MAC addr: "
13068 			   MACSTR, iface.ifname, MAC2STR(mac_addr));
13069 	}
13070 
13071 	if (wpa_supplicant_get_iface(global, iface.ifname))
13072 		goto fail;
13073 
13074 	wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
13075 	if (!wpa_s)
13076 		goto fail;
13077 	wpa_s->added_vif = create_iface;
13078 	return 0;
13079 
13080 fail:
13081 	if (create_iface) {
13082 		/* wpa_supplicant does not create multi-BSS AP, so collapse to
13083 		 * WPA_IF_STATION to avoid unwanted clean up in the driver. */
13084 		wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
13085 	}
13086 	return -1;
13087 }
13088 
13089 
wpa_supplicant_global_iface_remove(struct wpa_global * global,char * cmd)13090 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
13091 					      char *cmd)
13092 {
13093 	struct wpa_supplicant *wpa_s;
13094 	int ret;
13095 	unsigned int delete_iface;
13096 
13097 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
13098 
13099 	wpa_s = wpa_supplicant_get_iface(global, cmd);
13100 	if (wpa_s == NULL)
13101 		return -1;
13102 	delete_iface = wpa_s->added_vif;
13103 	ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
13104 	if (!ret && delete_iface) {
13105 		wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
13106 			   cmd);
13107 		/* wpa_supplicant does not create multi-BSS AP, so collapse to
13108 		 * WPA_IF_STATION to avoid unwanted clean up in the driver. */
13109 		ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
13110 	}
13111 	return ret;
13112 }
13113 
13114 
wpa_free_iface_info(struct wpa_interface_info * iface)13115 static void wpa_free_iface_info(struct wpa_interface_info *iface)
13116 {
13117 	struct wpa_interface_info *prev;
13118 
13119 	while (iface) {
13120 		prev = iface;
13121 		iface = iface->next;
13122 
13123 		os_free(prev->ifname);
13124 		os_free(prev->desc);
13125 		os_free(prev);
13126 	}
13127 }
13128 
13129 
wpa_supplicant_global_iface_list(struct wpa_global * global,char * buf,int len)13130 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
13131 					    char *buf, int len)
13132 {
13133 	int i, res;
13134 	struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
13135 	char *pos, *end;
13136 
13137 	for (i = 0; wpa_drivers[i]; i++) {
13138 		const struct wpa_driver_ops *drv = wpa_drivers[i];
13139 		if (drv->get_interfaces == NULL)
13140 			continue;
13141 		tmp = drv->get_interfaces(global->drv_priv[i]);
13142 		if (tmp == NULL)
13143 			continue;
13144 
13145 		if (last == NULL)
13146 			iface = last = tmp;
13147 		else
13148 			last->next = tmp;
13149 		while (last->next)
13150 			last = last->next;
13151 	}
13152 
13153 	pos = buf;
13154 	end = buf + len;
13155 	for (tmp = iface; tmp; tmp = tmp->next) {
13156 		res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
13157 				  tmp->drv_name, tmp->ifname,
13158 				  tmp->desc ? tmp->desc : "");
13159 		if (os_snprintf_error(end - pos, res)) {
13160 			*pos = '\0';
13161 			break;
13162 		}
13163 		pos += res;
13164 	}
13165 
13166 	wpa_free_iface_info(iface);
13167 
13168 	return pos - buf;
13169 }
13170 
13171 
wpa_supplicant_global_iface_interfaces(struct wpa_global * global,const char * input,char * buf,int len)13172 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
13173 						  const char *input,
13174 						  char *buf, int len)
13175 {
13176 	int res;
13177 	char *pos, *end;
13178 	struct wpa_supplicant *wpa_s;
13179 	int show_ctrl = 0;
13180 
13181 	if (input)
13182 		show_ctrl = !!os_strstr(input, "ctrl");
13183 
13184 	wpa_s = global->ifaces;
13185 	pos = buf;
13186 	end = buf + len;
13187 
13188 	while (wpa_s) {
13189 		if (show_ctrl)
13190 			res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
13191 					  wpa_s->ifname,
13192 					  wpa_s->conf->ctrl_interface ?
13193 					  wpa_s->conf->ctrl_interface : "N/A");
13194 		else
13195 			res = os_snprintf(pos, end - pos, "%s\n",
13196 					  wpa_s->ifname);
13197 
13198 		if (os_snprintf_error(end - pos, res)) {
13199 			*pos = '\0';
13200 			break;
13201 		}
13202 		pos += res;
13203 		wpa_s = wpa_s->next;
13204 	}
13205 	return pos - buf;
13206 }
13207 
13208 
wpas_global_ctrl_iface_ifname(struct wpa_global * global,const char * ifname,char * cmd,size_t * resp_len)13209 static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
13210 					    const char *ifname,
13211 					    char *cmd, size_t *resp_len)
13212 {
13213 	struct wpa_supplicant *wpa_s;
13214 
13215 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
13216 		if (os_strcmp(ifname, wpa_s->ifname) == 0)
13217 			break;
13218 	}
13219 
13220 	if (wpa_s == NULL) {
13221 		char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
13222 		if (resp)
13223 			*resp_len = os_strlen(resp);
13224 		else
13225 			*resp_len = 1;
13226 		return resp;
13227 	}
13228 
13229 	return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
13230 }
13231 
13232 
wpas_global_ctrl_iface_redir_p2p(struct wpa_global * global,char * buf,size_t * resp_len)13233 static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
13234 					       char *buf, size_t *resp_len)
13235 {
13236 #ifdef CONFIG_P2P
13237 	static const char * cmd[] = {
13238 		"LIST_NETWORKS",
13239 		"P2P_FIND",
13240 		"P2P_STOP_FIND",
13241 		"P2P_LISTEN",
13242 		"P2P_GROUP_ADD",
13243 		"P2P_GET_PASSPHRASE",
13244 		"P2P_SERVICE_UPDATE",
13245 		"P2P_SERVICE_FLUSH",
13246 		"P2P_FLUSH",
13247 		"P2P_CANCEL",
13248 		"P2P_PRESENCE_REQ",
13249 		"P2P_EXT_LISTEN",
13250 #ifdef CONFIG_AP
13251 		"STA-FIRST",
13252 #endif /* CONFIG_AP */
13253 		NULL
13254 	};
13255 	static const char * prefix[] = {
13256 #ifdef ANDROID
13257 		"DRIVER ",
13258 #endif /* ANDROID */
13259 		"GET_CAPABILITY ",
13260 		"GET_NETWORK ",
13261 		"REMOVE_NETWORK ",
13262 		"P2P_FIND ",
13263 		"P2P_CONNECT ",
13264 		"P2P_LISTEN ",
13265 		"P2P_GROUP_REMOVE ",
13266 		"P2P_GROUP_ADD ",
13267 		"P2P_GROUP_MEMBER ",
13268 		"P2P_PROV_DISC ",
13269 		"P2P_SERV_DISC_REQ ",
13270 		"P2P_SERV_DISC_CANCEL_REQ ",
13271 		"P2P_SERV_DISC_RESP ",
13272 		"P2P_SERV_DISC_EXTERNAL ",
13273 		"P2P_SERVICE_ADD ",
13274 		"P2P_SERVICE_DEL ",
13275 		"P2P_SERVICE_REP ",
13276 		"P2P_REJECT ",
13277 		"P2P_INVITE ",
13278 		"P2P_PEER ",
13279 		"P2P_SET ",
13280 		"P2P_UNAUTHORIZE ",
13281 		"P2P_PRESENCE_REQ ",
13282 		"P2P_EXT_LISTEN ",
13283 		"P2P_REMOVE_CLIENT ",
13284 		"WPS_NFC_TOKEN ",
13285 		"WPS_NFC_TAG_READ ",
13286 		"NFC_GET_HANDOVER_SEL ",
13287 		"NFC_GET_HANDOVER_REQ ",
13288 		"NFC_REPORT_HANDOVER ",
13289 		"P2P_ASP_PROVISION ",
13290 		"P2P_ASP_PROVISION_RESP ",
13291 #ifdef CONFIG_AP
13292 		"STA ",
13293 		"STA-NEXT ",
13294 #endif /* CONFIG_AP */
13295 		NULL
13296 	};
13297 	int found = 0;
13298 	int i;
13299 
13300 	if (global->p2p_init_wpa_s == NULL)
13301 		return NULL;
13302 
13303 	for (i = 0; !found && cmd[i]; i++) {
13304 		if (os_strcmp(buf, cmd[i]) == 0)
13305 			found = 1;
13306 	}
13307 
13308 	for (i = 0; !found && prefix[i]; i++) {
13309 		if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
13310 			found = 1;
13311 	}
13312 
13313 	if (found)
13314 		return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
13315 							 buf, resp_len);
13316 #endif /* CONFIG_P2P */
13317 	return NULL;
13318 }
13319 
13320 
wpas_global_ctrl_iface_redir_wfd(struct wpa_global * global,char * buf,size_t * resp_len)13321 static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
13322 					       char *buf, size_t *resp_len)
13323 {
13324 #ifdef CONFIG_WIFI_DISPLAY
13325 	if (global->p2p_init_wpa_s == NULL)
13326 		return NULL;
13327 	if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
13328 	    os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
13329 		return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
13330 							 buf, resp_len);
13331 #endif /* CONFIG_WIFI_DISPLAY */
13332 	return NULL;
13333 }
13334 
13335 
wpas_global_ctrl_iface_redir(struct wpa_global * global,char * buf,size_t * resp_len)13336 static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
13337 					   char *buf, size_t *resp_len)
13338 {
13339 	char *ret;
13340 
13341 	ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
13342 	if (ret)
13343 		return ret;
13344 
13345 	ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
13346 	if (ret)
13347 		return ret;
13348 
13349 	return NULL;
13350 }
13351 
13352 
wpas_global_ctrl_iface_set(struct wpa_global * global,char * cmd)13353 static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
13354 {
13355 	char *value;
13356 
13357 	value = os_strchr(cmd, ' ');
13358 	if (value == NULL)
13359 		return -1;
13360 	*value++ = '\0';
13361 
13362 	wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
13363 
13364 #ifdef CONFIG_WIFI_DISPLAY
13365 	if (os_strcasecmp(cmd, "wifi_display") == 0) {
13366 		wifi_display_enable(global, !!atoi(value));
13367 		return 0;
13368 	}
13369 #endif /* CONFIG_WIFI_DISPLAY */
13370 
13371 	/* Restore cmd to its original value to allow redirection */
13372 	value[-1] = ' ';
13373 
13374 	return -1;
13375 }
13376 
13377 
wpas_global_ctrl_iface_dup_network(struct wpa_global * global,char * cmd)13378 static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
13379 					      char *cmd)
13380 {
13381 	struct wpa_supplicant *wpa_s[2]; /* src, dst */
13382 	char *p;
13383 	unsigned int i;
13384 
13385 	/* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
13386 	 * <variable name> */
13387 
13388 	for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
13389 		p = os_strchr(cmd, ' ');
13390 		if (p == NULL)
13391 			return -1;
13392 		*p = '\0';
13393 
13394 		wpa_s[i] = global->ifaces;
13395 		for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
13396 			if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
13397 				break;
13398 		}
13399 
13400 		if (!wpa_s[i]) {
13401 			wpa_printf(MSG_DEBUG,
13402 				   "CTRL_IFACE: Could not find iface=%s", cmd);
13403 			return -1;
13404 		}
13405 
13406 		cmd = p + 1;
13407 	}
13408 
13409 	return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
13410 }
13411 
13412 
13413 #ifndef CONFIG_NO_CONFIG_WRITE
wpas_global_ctrl_iface_save_config(struct wpa_global * global)13414 static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
13415 {
13416 	int ret = 0, saved = 0;
13417 	struct wpa_supplicant *wpa_s;
13418 
13419 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
13420 		if (!wpa_s->conf->update_config) {
13421 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
13422 			continue;
13423 		}
13424 
13425 		if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
13426 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
13427 			ret = 1;
13428 		} else {
13429 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
13430 			saved++;
13431 		}
13432 	}
13433 
13434 	if (!saved && !ret) {
13435 		wpa_dbg(wpa_s, MSG_DEBUG,
13436 			"CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
13437 		ret = 1;
13438 	}
13439 
13440 	return ret;
13441 }
13442 #endif /* CONFIG_NO_CONFIG_WRITE */
13443 
13444 
wpas_global_ctrl_iface_status(struct wpa_global * global,char * buf,size_t buflen)13445 static int wpas_global_ctrl_iface_status(struct wpa_global *global,
13446 					 char *buf, size_t buflen)
13447 {
13448 	char *pos, *end;
13449 	int ret;
13450 	struct wpa_supplicant *wpa_s;
13451 
13452 	pos = buf;
13453 	end = buf + buflen;
13454 
13455 #ifdef CONFIG_P2P
13456 	if (global->p2p && !global->p2p_disabled) {
13457 		ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
13458 				  "\n"
13459 				  "p2p_state=%s\n",
13460 				  MAC2STR(global->p2p_dev_addr),
13461 				  p2p_get_state_txt(global->p2p));
13462 		if (os_snprintf_error(end - pos, ret))
13463 			return pos - buf;
13464 		pos += ret;
13465 	} else if (global->p2p) {
13466 		ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
13467 		if (os_snprintf_error(end - pos, ret))
13468 			return pos - buf;
13469 		pos += ret;
13470 	}
13471 #endif /* CONFIG_P2P */
13472 
13473 #ifdef CONFIG_WIFI_DISPLAY
13474 	ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
13475 			  !!global->wifi_display);
13476 	if (os_snprintf_error(end - pos, ret))
13477 		return pos - buf;
13478 	pos += ret;
13479 #endif /* CONFIG_WIFI_DISPLAY */
13480 
13481 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
13482 		ret = os_snprintf(pos, end - pos, "ifname=%s\n"
13483 				  "address=" MACSTR "\n",
13484 				  wpa_s->ifname, MAC2STR(wpa_s->own_addr));
13485 		if (os_snprintf_error(end - pos, ret))
13486 			return pos - buf;
13487 		pos += ret;
13488 	}
13489 
13490 	return pos - buf;
13491 }
13492 
13493 
13494 #ifdef CONFIG_FST
13495 
wpas_global_ctrl_iface_fst_attach(struct wpa_global * global,char * cmd,char * buf,size_t reply_size)13496 static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
13497 					     char *cmd, char *buf,
13498 					     size_t reply_size)
13499 {
13500 	char ifname[IFNAMSIZ + 1];
13501 	struct fst_iface_cfg cfg;
13502 	struct wpa_supplicant *wpa_s;
13503 	struct fst_wpa_obj iface_obj;
13504 
13505 	if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
13506 		wpa_s = wpa_supplicant_get_iface(global, ifname);
13507 		if (wpa_s) {
13508 			if (wpa_s->fst) {
13509 				wpa_printf(MSG_INFO, "FST: Already attached");
13510 				return -1;
13511 			}
13512 			fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
13513 			wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
13514 						&iface_obj, &cfg);
13515 			if (wpa_s->fst)
13516 				return os_snprintf(buf, reply_size, "OK\n");
13517 		}
13518 	}
13519 
13520 	return -1;
13521 }
13522 
13523 
wpas_global_ctrl_iface_fst_detach(struct wpa_global * global,char * cmd,char * buf,size_t reply_size)13524 static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
13525 					     char *cmd, char *buf,
13526 					     size_t reply_size)
13527 {
13528 	char ifname[IFNAMSIZ + 1];
13529 	struct wpa_supplicant *wpa_s;
13530 
13531 	if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
13532 		wpa_s = wpa_supplicant_get_iface(global, ifname);
13533 		if (wpa_s) {
13534 			if (!fst_iface_detach(ifname)) {
13535 				wpa_s->fst = NULL;
13536 				return os_snprintf(buf, reply_size, "OK\n");
13537 			}
13538 		}
13539 	}
13540 
13541 	return -1;
13542 }
13543 
13544 #endif /* CONFIG_FST */
13545 
13546 
wpa_supplicant_global_ctrl_iface_process(struct wpa_global * global,char * buf,size_t * resp_len)13547 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
13548 						char *buf, size_t *resp_len)
13549 {
13550 	char *reply;
13551 	const int reply_size = 2048;
13552 	int reply_len;
13553 	int level = MSG_DEBUG;
13554 
13555 	if (os_strncmp(buf, "IFNAME=", 7) == 0) {
13556 		char *pos = os_strchr(buf + 7, ' ');
13557 		if (pos) {
13558 			*pos++ = '\0';
13559 			return wpas_global_ctrl_iface_ifname(global,
13560 							     buf + 7, pos,
13561 							     resp_len);
13562 		}
13563 	}
13564 
13565 	reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
13566 	if (reply)
13567 		return reply;
13568 
13569 	if (os_strcmp(buf, "PING") == 0)
13570 		level = MSG_EXCESSIVE;
13571 	wpa_hexdump_ascii(level, "RX global ctrl_iface",
13572 			  (const u8 *) buf, os_strlen(buf));
13573 
13574 	reply = os_malloc(reply_size);
13575 	if (reply == NULL) {
13576 		*resp_len = 1;
13577 		return NULL;
13578 	}
13579 
13580 	os_memcpy(reply, "OK\n", 3);
13581 	reply_len = 3;
13582 
13583 	if (os_strcmp(buf, "PING") == 0) {
13584 		os_memcpy(reply, "PONG\n", 5);
13585 		reply_len = 5;
13586 	} else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
13587 		if (wpa_supplicant_global_iface_add(global, buf + 14))
13588 			reply_len = -1;
13589 	} else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
13590 		if (wpa_supplicant_global_iface_remove(global, buf + 17))
13591 			reply_len = -1;
13592 	} else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
13593 		reply_len = wpa_supplicant_global_iface_list(
13594 			global, reply, reply_size);
13595 	} else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
13596 		reply_len = wpa_supplicant_global_iface_interfaces(
13597 			global, buf + 10, reply, reply_size);
13598 #ifdef CONFIG_FST
13599 	} else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
13600 		reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
13601 							      reply,
13602 							      reply_size);
13603 	} else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
13604 		reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
13605 							      reply,
13606 							      reply_size);
13607 	} else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
13608 		reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
13609 #endif /* CONFIG_FST */
13610 	} else if (os_strcmp(buf, "TERMINATE") == 0) {
13611 		wpa_supplicant_terminate_proc(global);
13612 	} else if (os_strcmp(buf, "SUSPEND") == 0) {
13613 		wpas_notify_suspend(global);
13614 	} else if (os_strcmp(buf, "RESUME") == 0) {
13615 		wpas_notify_resume(global);
13616 	} else if (os_strncmp(buf, "SET ", 4) == 0) {
13617 		if (wpas_global_ctrl_iface_set(global, buf + 4)) {
13618 #ifdef CONFIG_P2P
13619 			if (global->p2p_init_wpa_s) {
13620 				os_free(reply);
13621 				/* Check if P2P redirection would work for this
13622 				 * command. */
13623 				return wpa_supplicant_ctrl_iface_process(
13624 					global->p2p_init_wpa_s,
13625 					buf, resp_len);
13626 			}
13627 #endif /* CONFIG_P2P */
13628 			reply_len = -1;
13629 		}
13630 	} else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
13631 		if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
13632 			reply_len = -1;
13633 #ifndef CONFIG_NO_CONFIG_WRITE
13634 	} else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
13635 		if (wpas_global_ctrl_iface_save_config(global))
13636 			reply_len = -1;
13637 #endif /* CONFIG_NO_CONFIG_WRITE */
13638 	} else if (os_strcmp(buf, "STATUS") == 0) {
13639 		reply_len = wpas_global_ctrl_iface_status(global, reply,
13640 							  reply_size);
13641 #ifdef CONFIG_MODULE_TESTS
13642 	} else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
13643 		if (wpas_module_tests() < 0)
13644 			reply_len = -1;
13645 #endif /* CONFIG_MODULE_TESTS */
13646 	} else if (os_strncmp(buf, "RELOG", 5) == 0) {
13647 		if (wpa_debug_reopen_file() < 0)
13648 			reply_len = -1;
13649 	} else {
13650 		os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
13651 		reply_len = 16;
13652 	}
13653 
13654 	if (reply_len < 0) {
13655 		os_memcpy(reply, "FAIL\n", 5);
13656 		reply_len = 5;
13657 	}
13658 
13659 	*resp_len = reply_len;
13660 	return reply;
13661 }
13662