xref: /freebsd/contrib/wpa/wpa_supplicant/ctrl_iface.c (revision 71e72c9e91c4b8007a4292e09669e8b549c29e97)
1 /*
2  * WPA Supplicant / Control interface (shared code for all backends)
3  * Copyright (c) 2004-2024, 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 "utils/trace.h"
19 #include "common/version.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/ieee802_11_common.h"
22 #include "common/wpa_ctrl.h"
23 #ifdef CONFIG_DPP
24 #include "common/dpp.h"
25 #endif /* CONFIG_DPP */
26 #include "common/nan_de.h"
27 #include "common/ptksa_cache.h"
28 #include "common/proximity_ranging.h"
29 #include "crypto/tls.h"
30 #include "ap/hostapd.h"
31 #include "eap_peer/eap.h"
32 #include "eapol_supp/eapol_supp_sm.h"
33 #include "rsn_supp/wpa.h"
34 #include "rsn_supp/preauth.h"
35 #include "rsn_supp/pmksa_cache.h"
36 #include "l2_packet/l2_packet.h"
37 #include "wps/wps.h"
38 #include "fst/fst.h"
39 #include "fst/fst_ctrl_iface.h"
40 #include "config.h"
41 #include "wpa_supplicant_i.h"
42 #include "driver_i.h"
43 #include "wps_supplicant.h"
44 #include "ibss_rsn.h"
45 #include "wpas_glue.h"
46 #include "ap.h"
47 #include "p2p_supplicant.h"
48 #include "p2p/p2p.h"
49 #include "hs20_supplicant.h"
50 #include "wifi_display.h"
51 #include "notify.h"
52 #include "bss.h"
53 #include "scan.h"
54 #include "ctrl_iface.h"
55 #include "interworking.h"
56 #include "bssid_ignore.h"
57 #include "autoscan.h"
58 #include "wnm_sta.h"
59 #include "offchannel.h"
60 #include "drivers/driver.h"
61 #include "mesh.h"
62 #include "dpp_supplicant.h"
63 #include "sme.h"
64 #include "pr_supplicant.h"
65 #include "nan_supplicant.h"
66 
67 #ifdef __NetBSD__
68 #include <net/if_ether.h>
69 #elif !defined(__CYGWIN__) && !defined(CONFIG_NATIVE_WINDOWS)
70 #include <net/ethernet.h>
71 #endif
72 
73 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
74 					    char *buf, int len);
75 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
76 						  const char *input,
77 						  char *buf, int len);
78 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
79 					char *val);
80 
81 
set_bssid_filter(struct wpa_supplicant * wpa_s,char * val)82 static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
83 {
84 	char *pos;
85 	u8 addr[ETH_ALEN], *filter = NULL, *n;
86 	size_t count = 0;
87 
88 	pos = val;
89 	while (pos) {
90 		if (*pos == '\0')
91 			break;
92 		if (hwaddr_aton(pos, addr)) {
93 			os_free(filter);
94 			return -1;
95 		}
96 		n = os_realloc_array(filter, count + 1, ETH_ALEN);
97 		if (n == NULL) {
98 			os_free(filter);
99 			return -1;
100 		}
101 		filter = n;
102 		os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
103 		count++;
104 
105 		pos = os_strchr(pos, ' ');
106 		if (pos)
107 			pos++;
108 	}
109 
110 	wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
111 	os_free(wpa_s->bssid_filter);
112 	wpa_s->bssid_filter = filter;
113 	wpa_s->bssid_filter_count = count;
114 
115 	return 0;
116 }
117 
118 
set_disallow_aps(struct wpa_supplicant * wpa_s,char * val)119 static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
120 {
121 	char *pos;
122 	u8 addr[ETH_ALEN], *bssid = NULL, *n;
123 	struct wpa_ssid_value *ssid = NULL, *ns;
124 	size_t count = 0, ssid_count = 0;
125 	struct wpa_ssid *c;
126 
127 	/*
128 	 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
129 	 * SSID_SPEC ::= ssid <SSID_HEX>
130 	 * BSSID_SPEC ::= bssid <BSSID_HEX>
131 	 */
132 
133 	pos = val;
134 	while (pos) {
135 		if (*pos == '\0')
136 			break;
137 		if (os_strncmp(pos, "bssid ", 6) == 0) {
138 			int res;
139 			pos += 6;
140 			res = hwaddr_aton2(pos, addr);
141 			if (res < 0) {
142 				os_free(ssid);
143 				os_free(bssid);
144 				wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
145 					   "BSSID value '%s'", pos);
146 				return -1;
147 			}
148 			pos += res;
149 			n = os_realloc_array(bssid, count + 1, ETH_ALEN);
150 			if (n == NULL) {
151 				os_free(ssid);
152 				os_free(bssid);
153 				return -1;
154 			}
155 			bssid = n;
156 			os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
157 			count++;
158 		} else if (os_strncmp(pos, "ssid ", 5) == 0) {
159 			char *end;
160 			pos += 5;
161 
162 			end = pos;
163 			while (*end) {
164 				if (*end == '\0' || *end == ' ')
165 					break;
166 				end++;
167 			}
168 
169 			ns = os_realloc_array(ssid, ssid_count + 1,
170 					      sizeof(struct wpa_ssid_value));
171 			if (ns == NULL) {
172 				os_free(ssid);
173 				os_free(bssid);
174 				return -1;
175 			}
176 			ssid = ns;
177 
178 			if ((end - pos) & 0x01 ||
179 			    end - pos > 2 * SSID_MAX_LEN ||
180 			    hexstr2bin(pos, ssid[ssid_count].ssid,
181 				       (end - pos) / 2) < 0) {
182 				os_free(ssid);
183 				os_free(bssid);
184 				wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
185 					   "SSID value '%s'", pos);
186 				return -1;
187 			}
188 			ssid[ssid_count].ssid_len = (end - pos) / 2;
189 			wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
190 					  ssid[ssid_count].ssid,
191 					  ssid[ssid_count].ssid_len);
192 			ssid_count++;
193 			pos = end;
194 		} else {
195 			wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
196 				   "'%s'", pos);
197 			os_free(ssid);
198 			os_free(bssid);
199 			return -1;
200 		}
201 
202 		pos = os_strchr(pos, ' ');
203 		if (pos)
204 			pos++;
205 	}
206 
207 	wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
208 	os_free(wpa_s->disallow_aps_bssid);
209 	wpa_s->disallow_aps_bssid = bssid;
210 	wpa_s->disallow_aps_bssid_count = count;
211 
212 	wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
213 	os_free(wpa_s->disallow_aps_ssid);
214 	wpa_s->disallow_aps_ssid = ssid;
215 	wpa_s->disallow_aps_ssid_count = ssid_count;
216 
217 	if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
218 		return 0;
219 
220 	c = wpa_s->current_ssid;
221 	if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
222 		return 0;
223 
224 	if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
225 	    !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
226 		return 0;
227 
228 	wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
229 		   "because current AP was marked disallowed");
230 
231 #ifdef CONFIG_SME
232 	wpa_s->sme.prev_bssid_set = 0;
233 #endif /* CONFIG_SME */
234 	wpa_s->reassociate = 1;
235 	wpa_s->own_disconnect_req = 1;
236 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
237 	wpa_supplicant_req_scan(wpa_s, 0, 0);
238 
239 	return 0;
240 }
241 
242 
243 #ifndef CONFIG_NO_CONFIG_BLOBS
wpas_ctrl_set_blob(struct wpa_supplicant * wpa_s,char * pos)244 static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
245 {
246 	char *name = pos;
247 	struct wpa_config_blob *blob;
248 	size_t len;
249 
250 	pos = os_strchr(pos, ' ');
251 	if (pos == NULL)
252 		return -1;
253 	*pos++ = '\0';
254 	len = os_strlen(pos);
255 	if (len & 1)
256 		return -1;
257 
258 	wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
259 	blob = os_zalloc(sizeof(*blob));
260 	if (blob == NULL)
261 		return -1;
262 	blob->name = os_strdup(name);
263 	blob->data = os_malloc(len / 2);
264 	if (blob->name == NULL || blob->data == NULL) {
265 		wpa_config_free_blob(blob);
266 		return -1;
267 	}
268 
269 	if (hexstr2bin(pos, blob->data, len / 2) < 0) {
270 		wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
271 		wpa_config_free_blob(blob);
272 		return -1;
273 	}
274 	blob->len = len / 2;
275 
276 	wpa_config_set_blob(wpa_s->conf, blob);
277 
278 	return 0;
279 }
280 #endif /* CONFIG_NO_CONFIG_BLOBS */
281 
282 
wpas_ctrl_pno(struct wpa_supplicant * wpa_s,char * cmd)283 static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
284 {
285 	char *params;
286 	char *pos;
287 	int *freqs = NULL;
288 	int ret;
289 
290 	if (atoi(cmd)) {
291 		params = os_strchr(cmd, ' ');
292 		os_free(wpa_s->manual_sched_scan_freqs);
293 		if (params) {
294 			params++;
295 			pos = os_strstr(params, "freq=");
296 			if (pos)
297 				freqs = freq_range_to_channel_list(wpa_s,
298 								   pos + 5);
299 		}
300 		wpa_s->manual_sched_scan_freqs = freqs;
301 		ret = wpas_start_pno(wpa_s);
302 	} else {
303 		ret = wpas_stop_pno(wpa_s);
304 	}
305 	return ret;
306 }
307 
308 
wpas_ctrl_set_band(struct wpa_supplicant * wpa_s,char * bands)309 static int wpas_ctrl_set_band(struct wpa_supplicant *wpa_s, char *bands)
310 {
311 	union wpa_event_data event;
312 	u32 setband_mask = WPA_SETBAND_AUTO;
313 
314 	/*
315 	 * For example:
316 	 *  SET setband 2G,6G
317 	 *  SET setband 5G
318 	 *  SET setband AUTO
319 	 */
320 	if (!os_strstr(bands, "AUTO")) {
321 		if (os_strstr(bands, "5G"))
322 			setband_mask |= WPA_SETBAND_5G;
323 		if (os_strstr(bands, "6G"))
324 			setband_mask |= WPA_SETBAND_6G;
325 		if (os_strstr(bands, "2G"))
326 			setband_mask |= WPA_SETBAND_2G;
327 		if (setband_mask == WPA_SETBAND_AUTO)
328 			return -1;
329 	}
330 
331 	wpa_s->setband_mask = setband_mask;
332 	if (wpa_drv_setband(wpa_s, wpa_s->setband_mask) == 0) {
333 		os_memset(&event, 0, sizeof(event));
334 		event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
335 		event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
336 		wpa_supplicant_event(wpa_s, EVENT_CHANNEL_LIST_CHANGED, &event);
337 	}
338 
339 	return 0;
340 }
341 
342 
wpas_ctrl_iface_set_lci(struct wpa_supplicant * wpa_s,const char * cmd)343 static int wpas_ctrl_iface_set_lci(struct wpa_supplicant *wpa_s,
344 				   const char *cmd)
345 {
346 	struct wpabuf *lci;
347 
348 	if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
349 		wpabuf_free(wpa_s->lci);
350 		wpa_s->lci = NULL;
351 		return 0;
352 	}
353 
354 	lci = wpabuf_parse_bin(cmd);
355 	if (!lci)
356 		return -1;
357 
358 	if (os_get_reltime(&wpa_s->lci_time)) {
359 		wpabuf_free(lci);
360 		return -1;
361 	}
362 
363 	wpabuf_free(wpa_s->lci);
364 	wpa_s->lci = lci;
365 
366 	return 0;
367 }
368 
369 
370 static int
wpas_ctrl_set_relative_rssi(struct wpa_supplicant * wpa_s,const char * cmd)371 wpas_ctrl_set_relative_rssi(struct wpa_supplicant *wpa_s, const char *cmd)
372 {
373 	int relative_rssi;
374 
375 	if (os_strcmp(cmd, "disable") == 0) {
376 		wpa_s->srp.relative_rssi_set = 0;
377 		return 0;
378 	}
379 
380 	relative_rssi = atoi(cmd);
381 	if (relative_rssi < 0 || relative_rssi > 100)
382 		return -1;
383 	wpa_s->srp.relative_rssi = relative_rssi;
384 	wpa_s->srp.relative_rssi_set = 1;
385 	return 0;
386 }
387 
388 
wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant * wpa_s,const char * cmd)389 static int wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant *wpa_s,
390 					      const char *cmd)
391 {
392 	char *pos;
393 	int adjust_rssi;
394 
395 	/* <band>:adjust_value */
396 	pos = os_strchr(cmd, ':');
397 	if (!pos)
398 		return -1;
399 	pos++;
400 	adjust_rssi = atoi(pos);
401 	if (adjust_rssi < -100 || adjust_rssi > 100)
402 		return -1;
403 
404 	if (os_strncmp(cmd, "2G", 2) == 0)
405 		wpa_s->srp.relative_adjust_band = WPA_SETBAND_2G;
406 	else if (os_strncmp(cmd, "5G", 2) == 0)
407 		wpa_s->srp.relative_adjust_band = WPA_SETBAND_5G;
408 	else
409 		return -1;
410 
411 	wpa_s->srp.relative_adjust_rssi = adjust_rssi;
412 
413 	return 0;
414 }
415 
416 
wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant * wpa_s,const char * cmd)417 static int wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant *wpa_s,
418 				   const char *cmd)
419 {
420 	struct wpabuf *ric_ies;
421 
422 	if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
423 		wpabuf_free(wpa_s->ric_ies);
424 		wpa_s->ric_ies = NULL;
425 		return 0;
426 	}
427 
428 	ric_ies = wpabuf_parse_bin(cmd);
429 	if (!ric_ies)
430 		return -1;
431 
432 	wpabuf_free(wpa_s->ric_ies);
433 	wpa_s->ric_ies = ric_ies;
434 
435 	return 0;
436 }
437 
438 
439 #ifdef CONFIG_TESTING_OPTIONS
wpas_ctrl_iface_set_dso(struct wpa_supplicant * wpa_s,const char * val)440 static int wpas_ctrl_iface_set_dso(struct wpa_supplicant *wpa_s,
441 				   const char *val)
442 {
443 	u8 bssid[ETH_ALEN];
444 	const char *pos = val;
445 	struct driver_signal_override *dso = NULL, *tmp, parsed;
446 
447 	if (hwaddr_aton(pos, bssid))
448 		return -1;
449 	pos = os_strchr(pos, ' ');
450 
451 	dl_list_for_each(tmp, &wpa_s->drv_signal_override,
452 			 struct driver_signal_override, list) {
453 		if (ether_addr_equal(bssid, tmp->bssid)) {
454 			dso = tmp;
455 			break;
456 		}
457 	}
458 
459 	if (!pos) {
460 		/* Remove existing entry */
461 		if (dso) {
462 			dl_list_del(&dso->list);
463 			os_free(dso);
464 		}
465 		return 0;
466 	}
467 	pos++;
468 
469 	/* Update an existing entry or add a new one */
470 	os_memset(&parsed, 0, sizeof(parsed));
471 	if (sscanf(pos, "%d %d %d %d %d",
472 		   &parsed.si_current_signal,
473 		   &parsed.si_avg_signal,
474 		   &parsed.si_avg_beacon_signal,
475 		   &parsed.si_current_noise,
476 		   &parsed.scan_level) != 5)
477 		return -1;
478 
479 	if (!dso) {
480 		dso = os_zalloc(sizeof(*dso));
481 		if (!dso)
482 			return -1;
483 		os_memcpy(dso->bssid, bssid, ETH_ALEN);
484 		dl_list_add(&wpa_s->drv_signal_override, &dso->list);
485 	}
486 	dso->si_current_signal = parsed.si_current_signal;
487 	dso->si_avg_signal = parsed.si_avg_signal;
488 	dso->si_avg_beacon_signal = parsed.si_avg_beacon_signal;
489 	dso->si_current_noise = parsed.si_current_noise;
490 	dso->scan_level = parsed.scan_level;
491 
492 	return 0;
493 }
494 #endif /* CONFIG_TESTING_OPTIONS */
495 
496 
wpa_supplicant_ctrl_iface_set(struct wpa_supplicant * wpa_s,char * cmd)497 static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
498 					 char *cmd)
499 {
500 	char *value;
501 	int ret = 0;
502 
503 	value = os_strchr(cmd, ' ');
504 	if (value == NULL)
505 		return -1;
506 	*value++ = '\0';
507 
508 	wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
509 	if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
510 		eapol_sm_configure(wpa_s->eapol,
511 				   atoi(value), -1, -1, -1);
512 	} else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
513 		eapol_sm_configure(wpa_s->eapol,
514 				   -1, atoi(value), -1, -1);
515 	} else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
516 		eapol_sm_configure(wpa_s->eapol,
517 				   -1, -1, atoi(value), -1);
518 	} else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
519 		eapol_sm_configure(wpa_s->eapol,
520 				   -1, -1, -1, atoi(value));
521 #ifdef CONFIG_TESTING_OPTIONS
522 	} else if (os_strcasecmp(cmd, "EAPOL::portControl") == 0) {
523 		if (os_strcmp(value, "Auto") == 0)
524 			eapol_sm_notify_portControl(wpa_s->eapol, Auto);
525 		else if (os_strcmp(value, "ForceUnauthorized") == 0)
526 			eapol_sm_notify_portControl(wpa_s->eapol,
527 						    ForceUnauthorized);
528 		else if (os_strcmp(value, "ForceAuthorized") == 0)
529 			eapol_sm_notify_portControl(wpa_s->eapol,
530 						    ForceAuthorized);
531 		else
532 			ret = -1;
533 #endif /* CONFIG_TESTING_OPTIONS */
534 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
535 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
536 				     atoi(value))) {
537 			ret = -1;
538 		} else {
539 			value[-1] = '=';
540 			wpa_config_process_global(wpa_s->conf, cmd, -1,
541 						  true);
542 		}
543 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
544 		   0) {
545 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
546 				     atoi(value))) {
547 			ret = -1;
548 		} else {
549 			value[-1] = '=';
550 			wpa_config_process_global(wpa_s->conf, cmd, -1,
551 						  true);
552 		}
553 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
554 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
555 				     atoi(value))) {
556 			ret = -1;
557 		} else {
558 			value[-1] = '=';
559 			wpa_config_process_global(wpa_s->conf, cmd, -1,
560 						  true);
561 		}
562 	} else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
563 		wpa_s->wps_fragment_size = atoi(value);
564 #ifdef CONFIG_WPS_TESTING
565 	} else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
566 		long int val;
567 		val = strtol(value, NULL, 0);
568 		if (val < 0 || val > 0xff) {
569 			ret = -1;
570 			wpa_printf(MSG_DEBUG, "WPS: Invalid "
571 				   "wps_version_number %ld", val);
572 		} else {
573 			wps_version_number = val;
574 			wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
575 				   "version %u.%u",
576 				   (wps_version_number & 0xf0) >> 4,
577 				   wps_version_number & 0x0f);
578 		}
579 	} else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) {
580 		wps_testing_stub_cred = atoi(value);
581 		wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d",
582 			   wps_testing_stub_cred);
583 	} else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
584 		wps_corrupt_pkhash = atoi(value);
585 		wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
586 			   wps_corrupt_pkhash);
587 	} else if (os_strcasecmp(cmd, "wps_force_auth_types") == 0) {
588 		if (value[0] == '\0') {
589 			wps_force_auth_types_in_use = 0;
590 		} else {
591 			wps_force_auth_types = strtol(value, NULL, 0);
592 			wps_force_auth_types_in_use = 1;
593 		}
594 	} else if (os_strcasecmp(cmd, "wps_force_encr_types") == 0) {
595 		if (value[0] == '\0') {
596 			wps_force_encr_types_in_use = 0;
597 		} else {
598 			wps_force_encr_types = strtol(value, NULL, 0);
599 			wps_force_encr_types_in_use = 1;
600 		}
601 #endif /* CONFIG_WPS_TESTING */
602 	} else if (os_strcasecmp(cmd, "ampdu") == 0) {
603 		if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
604 			ret = -1;
605 #ifdef CONFIG_TDLS
606 #ifdef CONFIG_TDLS_TESTING
607 	} else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
608 		tdls_testing = strtol(value, NULL, 0);
609 		wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
610 #endif /* CONFIG_TDLS_TESTING */
611 	} else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
612 		int disabled = atoi(value);
613 		wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
614 		if (disabled) {
615 			if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
616 				ret = -1;
617 		} else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
618 			ret = -1;
619 		wpa_tdls_enable(wpa_s->wpa, !disabled);
620 #endif /* CONFIG_TDLS */
621 	} else if (os_strcasecmp(cmd, "pno") == 0) {
622 		ret = wpas_ctrl_pno(wpa_s, value);
623 	} else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
624 		int disabled = atoi(value);
625 		if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
626 			ret = -1;
627 		else if (disabled)
628 			wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
629 	} else if (os_strcasecmp(cmd, "uapsd") == 0) {
630 		if (os_strcmp(value, "disable") == 0)
631 			wpa_s->set_sta_uapsd = 0;
632 		else {
633 			int be, bk, vi, vo;
634 			char *pos;
635 			/* format: BE,BK,VI,VO;max SP Length */
636 			be = atoi(value);
637 			pos = os_strchr(value, ',');
638 			if (pos == NULL)
639 				return -1;
640 			pos++;
641 			bk = atoi(pos);
642 			pos = os_strchr(pos, ',');
643 			if (pos == NULL)
644 				return -1;
645 			pos++;
646 			vi = atoi(pos);
647 			pos = os_strchr(pos, ',');
648 			if (pos == NULL)
649 				return -1;
650 			pos++;
651 			vo = atoi(pos);
652 			/* ignore max SP Length for now */
653 
654 			wpa_s->set_sta_uapsd = 1;
655 			wpa_s->sta_uapsd = 0;
656 			if (be)
657 				wpa_s->sta_uapsd |= BIT(0);
658 			if (bk)
659 				wpa_s->sta_uapsd |= BIT(1);
660 			if (vi)
661 				wpa_s->sta_uapsd |= BIT(2);
662 			if (vo)
663 				wpa_s->sta_uapsd |= BIT(3);
664 		}
665 	} else if (os_strcasecmp(cmd, "ps") == 0) {
666 		ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
667 #ifdef CONFIG_WIFI_DISPLAY
668 	} else if (os_strcasecmp(cmd, "wifi_display") == 0) {
669 		int enabled = !!atoi(value);
670 		if (enabled && !wpa_s->global->p2p)
671 			ret = -1;
672 		else
673 			wifi_display_enable(wpa_s->global, enabled);
674 #endif /* CONFIG_WIFI_DISPLAY */
675 	} else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
676 		ret = set_bssid_filter(wpa_s, value);
677 	} else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
678 		ret = set_disallow_aps(wpa_s, value);
679 	} else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
680 		wpa_s->no_keep_alive = !!atoi(value);
681 #ifdef CONFIG_DPP
682 	} else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
683 		os_free(wpa_s->dpp_configurator_params);
684 		wpa_s->dpp_configurator_params = os_strdup(value);
685 #ifdef CONFIG_DPP2
686 		dpp_controller_set_params(wpa_s->dpp, value);
687 #endif /* CONFIG_DPP2 */
688 	} else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
689 		wpa_s->dpp_init_max_tries = atoi(value);
690 	} else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
691 		wpa_s->dpp_init_retry_time = atoi(value);
692 	} else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
693 		wpa_s->dpp_resp_wait_time = atoi(value);
694 	} else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
695 		wpa_s->dpp_resp_max_tries = atoi(value);
696 	} else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
697 		wpa_s->dpp_resp_retry_time = atoi(value);
698 #ifdef CONFIG_TESTING_OPTIONS
699 	} else if (os_strcasecmp(cmd, "dpp_pkex_own_mac_override") == 0) {
700 		if (hwaddr_aton(value, dpp_pkex_own_mac_override))
701 			ret = -1;
702 	} else if (os_strcasecmp(cmd, "dpp_pkex_peer_mac_override") == 0) {
703 		if (hwaddr_aton(value, dpp_pkex_peer_mac_override))
704 			ret = -1;
705 	} else if (os_strcasecmp(cmd, "dpp_pkex_ephemeral_key_override") == 0) {
706 		size_t hex_len = os_strlen(value);
707 
708 		if (hex_len >
709 		    2 * sizeof(dpp_pkex_ephemeral_key_override))
710 			ret = -1;
711 		else if (hexstr2bin(value, dpp_pkex_ephemeral_key_override,
712 				    hex_len / 2))
713 			ret = -1;
714 		else
715 			dpp_pkex_ephemeral_key_override_len = hex_len / 2;
716 	} else if (os_strcasecmp(cmd, "dpp_protocol_key_override") == 0) {
717 		size_t hex_len = os_strlen(value);
718 
719 		if (hex_len > 2 * sizeof(dpp_protocol_key_override))
720 			ret = -1;
721 		else if (hexstr2bin(value, dpp_protocol_key_override,
722 				    hex_len / 2))
723 			ret = -1;
724 		else
725 			dpp_protocol_key_override_len = hex_len / 2;
726 	} else if (os_strcasecmp(cmd, "dpp_nonce_override") == 0) {
727 		size_t hex_len = os_strlen(value);
728 
729 		if (hex_len > 2 * sizeof(dpp_nonce_override))
730 			ret = -1;
731 		else if (hexstr2bin(value, dpp_nonce_override, hex_len / 2))
732 			ret = -1;
733 		else
734 			dpp_nonce_override_len = hex_len / 2;
735 	} else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
736 		dpp_version_override = atoi(value);
737 #endif /* CONFIG_TESTING_OPTIONS */
738 #endif /* CONFIG_DPP */
739 #ifdef CONFIG_TESTING_OPTIONS
740 	} else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
741 		wpa_s->ext_mgmt_frame_handling = !!atoi(value);
742 	} else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
743 		wpa_s->ext_eapol_frame_io = !!atoi(value);
744 #ifdef CONFIG_AP
745 		if (wpa_s->ap_iface) {
746 			wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
747 				wpa_s->ext_eapol_frame_io;
748 		}
749 #endif /* CONFIG_AP */
750 	} else if (os_strcasecmp(cmd, "encrypt_eapol_m2") == 0) {
751 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_ENCRYPT_EAPOL_M2,
752 				 !!atoi(value));
753 	} else if (os_strcasecmp(cmd, "encrypt_eapol_m4") == 0) {
754 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_ENCRYPT_EAPOL_M4,
755 				 !!atoi(value));
756 	} else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
757 		wpa_s->extra_roc_dur = atoi(value);
758 	} else if (os_strcasecmp(cmd, "test_failure") == 0) {
759 		wpa_s->test_failure = atoi(value);
760 	} else if (os_strcasecmp(cmd, "p2p_go_csa_on_inv") == 0) {
761 		wpa_s->p2p_go_csa_on_inv = !!atoi(value);
762 	} else if (os_strcasecmp(cmd, "ignore_auth_resp") == 0) {
763 		wpa_s->ignore_auth_resp = !!atoi(value);
764 	} else if (os_strcasecmp(cmd, "ignore_assoc_disallow") == 0) {
765 		wpa_s->ignore_assoc_disallow = !!atoi(value);
766 		wpa_drv_ignore_assoc_disallow(wpa_s,
767 					      wpa_s->ignore_assoc_disallow);
768 	} else if (os_strcasecmp(cmd, "disable_sa_query") == 0) {
769 		wpa_s->disable_sa_query = !!atoi(value);
770 	} else if (os_strcasecmp(cmd, "ignore_sae_h2e_only") == 0) {
771 		wpa_s->ignore_sae_h2e_only = !!atoi(value);
772 	} else if (os_strcasecmp(cmd, "extra_sae_rejected_groups") == 0) {
773 		char *pos;
774 
775 		os_free(wpa_s->extra_sae_rejected_groups);
776 		wpa_s->extra_sae_rejected_groups = NULL;
777 		pos = value;
778 		while (pos && pos[0]) {
779 			int group;
780 
781 			group = atoi(pos);
782 			wpa_printf(MSG_DEBUG,
783 				   "TESTING: Extra rejection of SAE group %d",
784 				   group);
785 			if (group)
786 				int_array_add_unique(
787 					&wpa_s->extra_sae_rejected_groups,
788 					group);
789 			pos = os_strchr(pos, ' ');
790 			if (!pos)
791 				break;
792 			pos++;
793 		}
794 	} else if (os_strcasecmp(cmd, "ft_rsnxe_used") == 0) {
795 		wpa_s->ft_rsnxe_used = atoi(value);
796 	} else if (os_strcasecmp(cmd, "oci_freq_override_eapol") == 0) {
797 		wpa_s->oci_freq_override_eapol = atoi(value);
798 	} else if (os_strcasecmp(cmd, "oci_freq_override_saquery_req") == 0) {
799 		wpa_s->oci_freq_override_saquery_req = atoi(value);
800 	} else if (os_strcasecmp(cmd, "oci_freq_override_saquery_resp") == 0) {
801 		wpa_s->oci_freq_override_saquery_resp = atoi(value);
802 	} else if (os_strcasecmp(cmd, "oci_freq_override_eapol_g2") == 0) {
803 		wpa_s->oci_freq_override_eapol_g2 = atoi(value);
804 		/* Populate value to wpa_sm if already associated. */
805 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_EAPOL_G2,
806 				 wpa_s->oci_freq_override_eapol_g2);
807 	} else if (os_strcasecmp(cmd, "oci_freq_override_ft_assoc") == 0) {
808 		wpa_s->oci_freq_override_ft_assoc = atoi(value);
809 		/* Populate value to wpa_sm if already associated. */
810 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_FT_ASSOC,
811 				 wpa_s->oci_freq_override_ft_assoc);
812 	} else if (os_strcasecmp(cmd, "oci_freq_override_fils_assoc") == 0) {
813 		wpa_s->oci_freq_override_fils_assoc = atoi(value);
814 	} else if (os_strcasecmp(cmd, "oci_freq_override_wnm_sleep") == 0) {
815 		wpa_s->oci_freq_override_wnm_sleep = atoi(value);
816 	} else if (os_strcasecmp(cmd, "rsne_override_eapol") == 0) {
817 		wpabuf_free(wpa_s->rsne_override_eapol);
818 		if (os_strcmp(value, "NULL") == 0)
819 			wpa_s->rsne_override_eapol = NULL;
820 		else
821 			wpa_s->rsne_override_eapol = wpabuf_parse_bin(value);
822 	} else if (os_strcasecmp(cmd, "rsnxe_override_assoc") == 0) {
823 		wpabuf_free(wpa_s->rsnxe_override_assoc);
824 		if (os_strcmp(value, "NULL") == 0)
825 			wpa_s->rsnxe_override_assoc = NULL;
826 		else
827 			wpa_s->rsnxe_override_assoc = wpabuf_parse_bin(value);
828 	} else if (os_strcasecmp(cmd, "rsnxe_override_eapol") == 0) {
829 		wpabuf_free(wpa_s->rsnxe_override_eapol);
830 		if (os_strcmp(value, "NULL") == 0)
831 			wpa_s->rsnxe_override_eapol = NULL;
832 		else
833 			wpa_s->rsnxe_override_eapol = wpabuf_parse_bin(value);
834 	} else if (os_strcasecmp(cmd, "link_ies") == 0) {
835 		int link_id = atoi(value);
836 		char *pos;
837 
838 		if (link_id < 0 || link_id >= MAX_NUM_MLD_LINKS)
839 			return -1;
840 
841 		pos = os_strchr(value, ':');
842 		if (!pos)
843 			return -1;
844 		pos++;
845 
846 		wpabuf_free(wpa_s->link_ies[link_id]);
847 		if (os_strcmp(value, "NULL") == 0)
848 			wpa_s->link_ies[link_id] = NULL;
849 		else
850 			wpa_s->link_ies[link_id] = wpabuf_parse_bin(pos);
851 	} else if (os_strcasecmp(cmd, "reject_btm_req_reason") == 0) {
852 		wpa_s->reject_btm_req_reason = atoi(value);
853 	} else if (os_strcasecmp(cmd, "get_pref_freq_list_override") == 0) {
854 		os_free(wpa_s->get_pref_freq_list_override);
855 		if (!value[0])
856 			wpa_s->get_pref_freq_list_override = NULL;
857 		else
858 			wpa_s->get_pref_freq_list_override = os_strdup(value);
859 	} else if (os_strcasecmp(cmd, "sae_commit_override") == 0) {
860 		wpabuf_free(wpa_s->sae_commit_override);
861 		if (value[0] == '\0')
862 			wpa_s->sae_commit_override = NULL;
863 		else
864 			wpa_s->sae_commit_override = wpabuf_parse_bin(value);
865 	} else if (os_strcasecmp(cmd, "driver_signal_override") == 0) {
866 		ret = wpas_ctrl_iface_set_dso(wpa_s, value);
867 #ifndef CONFIG_NO_ROBUST_AV
868 	} else if (os_strcasecmp(cmd, "disable_scs_support") == 0) {
869 		wpa_s->disable_scs_support = !!atoi(value);
870 	} else if (os_strcasecmp(cmd, "disable_mscs_support") == 0) {
871 		wpa_s->disable_mscs_support = !!atoi(value);
872 #endif /* CONFIG_NO_ROBUST_AV */
873 	} else if (os_strcasecmp(cmd, "disable_eapol_g2_tx") == 0) {
874 		wpa_s->disable_eapol_g2_tx = !!atoi(value);
875 		/* Populate value to wpa_sm if already associated. */
876 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_DISABLE_EAPOL_G2_TX,
877 				 wpa_s->disable_eapol_g2_tx);
878 	} else if (os_strcasecmp(cmd, "test_assoc_comeback_type") == 0) {
879 		wpa_s->test_assoc_comeback_type = atoi(value);
880 #ifdef CONFIG_DPP
881 	} else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
882 		os_free(wpa_s->dpp_config_obj_override);
883 		if (value[0] == '\0')
884 			wpa_s->dpp_config_obj_override = NULL;
885 		else
886 			wpa_s->dpp_config_obj_override = os_strdup(value);
887 	} else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
888 		os_free(wpa_s->dpp_discovery_override);
889 		if (value[0] == '\0')
890 			wpa_s->dpp_discovery_override = NULL;
891 		else
892 			wpa_s->dpp_discovery_override = os_strdup(value);
893 	} else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
894 		os_free(wpa_s->dpp_groups_override);
895 		if (value[0] == '\0')
896 			wpa_s->dpp_groups_override = NULL;
897 		else
898 			wpa_s->dpp_groups_override = os_strdup(value);
899 	} else if (os_strcasecmp(cmd,
900 				 "dpp_ignore_netaccesskey_mismatch") == 0) {
901 		wpa_s->dpp_ignore_netaccesskey_mismatch = atoi(value);
902 	} else if (os_strcasecmp(cmd, "dpp_discard_public_action") == 0) {
903 		wpa_s->dpp_discard_public_action = atoi(value);
904 	} else if (os_strcasecmp(cmd, "dpp_test") == 0) {
905 		dpp_test = atoi(value);
906 #endif /* CONFIG_DPP */
907 	} else if (os_strcasecmp(cmd, "eapol_2_key_info_set_mask") == 0) {
908 		wpa_s->eapol_2_key_info_set_mask = strtoul(value, NULL, 0x10);
909 #endif /* CONFIG_TESTING_OPTIONS */
910 #ifdef CONFIG_FILS
911 	} else if (os_strcasecmp(cmd, "disable_fils") == 0) {
912 		wpa_s->disable_fils = !!atoi(value);
913 		wpa_drv_disable_fils(wpa_s, wpa_s->disable_fils);
914 		wpa_supplicant_set_default_scan_ies(wpa_s);
915 #endif /* CONFIG_FILS */
916 #ifndef CONFIG_NO_CONFIG_BLOBS
917 	} else if (os_strcmp(cmd, "blob") == 0) {
918 		ret = wpas_ctrl_set_blob(wpa_s, value);
919 #endif /* CONFIG_NO_CONFIG_BLOBS */
920 	} else if (os_strcasecmp(cmd, "setband") == 0) {
921 		ret = wpas_ctrl_set_band(wpa_s, value);
922 #ifdef CONFIG_MBO
923 	} else if (os_strcasecmp(cmd, "non_pref_chan") == 0) {
924 		ret = wpas_mbo_update_non_pref_chan(wpa_s, value);
925 		if (ret == 0) {
926 			value[-1] = '=';
927 			wpa_config_process_global(wpa_s->conf, cmd, -1,
928 						  true);
929 		}
930 	} else if (os_strcasecmp(cmd, "mbo_cell_capa") == 0) {
931 		int val = atoi(value);
932 
933 		if (val < MBO_CELL_CAPA_AVAILABLE ||
934 		    val > MBO_CELL_CAPA_NOT_SUPPORTED)
935 			return -1;
936 
937 		wpas_mbo_update_cell_capa(wpa_s, val);
938 	} else if (os_strcasecmp(cmd, "oce") == 0) {
939 		int val = atoi(value);
940 
941 		if (val < 0 || val > 3)
942 			return -1;
943 
944 		wpa_s->conf->oce = val;
945 		if (wpa_s->conf->oce) {
946 			if ((wpa_s->conf->oce & OCE_STA) &&
947 			    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA))
948 				wpa_s->enable_oce = OCE_STA;
949 
950 			if ((wpa_s->conf->oce & OCE_STA_CFON) &&
951 			    (wpa_s->drv_flags &
952 			     WPA_DRIVER_FLAGS_OCE_STA_CFON)) {
953 				/* TODO: Need to add STA-CFON support */
954 				wpa_printf(MSG_ERROR,
955 					   "OCE STA-CFON feature is not yet supported");
956 				return -1;
957 			}
958 		} else {
959 			wpa_s->enable_oce = 0;
960 		}
961 		wpa_supplicant_set_default_scan_ies(wpa_s);
962 #endif /* CONFIG_MBO */
963 	} else if (os_strcasecmp(cmd, "lci") == 0) {
964 		ret = wpas_ctrl_iface_set_lci(wpa_s, value);
965 	} else if (os_strcasecmp(cmd, "tdls_trigger_control") == 0) {
966 		ret = wpa_drv_set_tdls_mode(wpa_s, atoi(value));
967 	} else if (os_strcasecmp(cmd, "relative_rssi") == 0) {
968 		ret = wpas_ctrl_set_relative_rssi(wpa_s, value);
969 	} else if (os_strcasecmp(cmd, "relative_band_adjust") == 0) {
970 		ret = wpas_ctrl_set_relative_band_adjust(wpa_s, value);
971 	} else if (os_strcasecmp(cmd, "ric_ies") == 0) {
972 		ret = wpas_ctrl_iface_set_ric_ies(wpa_s, value);
973 	} else if (os_strcasecmp(cmd, "roaming") == 0) {
974 		wpa_s->sta_roaming_disabled = atoi(value) ? false : true;
975 		ret = wpa_drv_roaming(wpa_s, atoi(value), NULL);
976 #ifdef CONFIG_WNM
977 	} else if (os_strcasecmp(cmd, "coloc_intf_elems") == 0) {
978 		struct wpabuf *elems;
979 
980 		elems = wpabuf_parse_bin(value);
981 		if (!elems)
982 			return -1;
983 		wnm_set_coloc_intf_elems(wpa_s, elems);
984 #endif /* CONFIG_WNM */
985 #ifndef CONFIG_NO_ROBUST_AV
986 	} else if (os_strcasecmp(cmd, "enable_dscp_policy_capa") == 0) {
987 		wpa_s->enable_dscp_policy_capa = !!atoi(value);
988 #endif /* CONFIG_NO_ROBUST_AV */
989 #ifdef CONFIG_PASN
990 	} else if (os_strcasecmp(cmd, "urnm_mfpr") == 0) {
991 		wpa_s->disable_urnm_mfpr = !atoi(value);
992 	} else if (os_strcasecmp(cmd, "urnm_mfpr_x20") == 0) {
993 		wpa_s->urnm_mfpr_x20 = !!atoi(value);
994 #endif /* CONFIG_PASN */
995 	} else {
996 		value[-1] = '=';
997 		ret = wpa_config_process_global(
998 			wpa_s->conf, cmd, -1,
999 			wpa_s->global->params.show_details);
1000 		if (ret == 0)
1001 			wpa_supplicant_update_config(wpa_s);
1002 		else if (ret == 1)
1003 			ret = 0;
1004 	}
1005 
1006 	return ret;
1007 }
1008 
1009 
wpa_supplicant_ctrl_iface_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1010 static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
1011 					 char *cmd, char *buf, size_t buflen)
1012 {
1013 	int res = -1;
1014 
1015 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1016 
1017 	if (os_strcmp(cmd, "version") == 0) {
1018 		res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1019 	} else if (os_strcasecmp(cmd, "max_command_len") == 0) {
1020 		res = os_snprintf(buf, buflen, "%u", CTRL_IFACE_MAX_LEN);
1021 	} else if (os_strcasecmp(cmd, "country") == 0) {
1022 		if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
1023 			res = os_snprintf(buf, buflen, "%c%c",
1024 					  wpa_s->conf->country[0],
1025 					  wpa_s->conf->country[1]);
1026 #ifdef CONFIG_WIFI_DISPLAY
1027 	} else if (os_strcasecmp(cmd, "wifi_display") == 0) {
1028 		int enabled;
1029 		if (wpa_s->global->p2p == NULL ||
1030 		    wpa_s->global->p2p_disabled)
1031 			enabled = 0;
1032 		else
1033 			enabled = wpa_s->global->wifi_display;
1034 		res = os_snprintf(buf, buflen, "%d", enabled);
1035 #endif /* CONFIG_WIFI_DISPLAY */
1036 #ifdef CONFIG_TESTING_GET_GTK
1037 	} else if (os_strcmp(cmd, "gtk") == 0) {
1038 		if (wpa_s->last_gtk_len == 0)
1039 			return -1;
1040 		res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
1041 				       wpa_s->last_gtk_len);
1042 		return res;
1043 #endif /* CONFIG_TESTING_GET_GTK */
1044 	} else if (os_strcmp(cmd, "tls_library") == 0) {
1045 		res = tls_get_library_version(buf, buflen);
1046 #ifdef CONFIG_TESTING_OPTIONS
1047 	} else if (os_strcmp(cmd, "anonce") == 0) {
1048 		return wpa_snprintf_hex(buf, buflen,
1049 					wpa_sm_get_anonce(wpa_s->wpa),
1050 					WPA_NONCE_LEN);
1051 	} else if (os_strcasecmp(cmd, "last_tk_key_idx") == 0) {
1052 		res = os_snprintf(buf, buflen, "%d", wpa_s->last_tk_key_idx);
1053 #endif /* CONFIG_TESTING_OPTIONS */
1054 	} else {
1055 		res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
1056 	}
1057 
1058 	if (os_snprintf_error(buflen, res))
1059 		return -1;
1060 	return res;
1061 }
1062 
1063 
1064 #ifdef IEEE8021X_EAPOL
wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant * wpa_s,char * addr)1065 static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
1066 					     char *addr)
1067 {
1068 	u8 bssid[ETH_ALEN];
1069 	struct wpa_ssid *ssid = wpa_s->current_ssid;
1070 
1071 	if (hwaddr_aton(addr, bssid)) {
1072 		wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
1073 			   "'%s'", addr);
1074 		return -1;
1075 	}
1076 
1077 	wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
1078 	rsn_preauth_deinit(wpa_s->wpa);
1079 	if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
1080 		return -1;
1081 
1082 	return 0;
1083 }
1084 #endif /* IEEE8021X_EAPOL */
1085 
1086 
1087 #ifdef CONFIG_TDLS
1088 
wpa_supplicant_ctrl_iface_tdls_discover(struct wpa_supplicant * wpa_s,char * addr)1089 static int wpa_supplicant_ctrl_iface_tdls_discover(
1090 	struct wpa_supplicant *wpa_s, char *addr)
1091 {
1092 	u8 peer[ETH_ALEN];
1093 	int ret;
1094 
1095 	if (hwaddr_aton(addr, peer)) {
1096 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
1097 			   "address '%s'", addr);
1098 		return -1;
1099 	}
1100 
1101 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
1102 		   MAC2STR(peer));
1103 
1104 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1105 		ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
1106 	else
1107 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
1108 
1109 	return ret;
1110 }
1111 
1112 
wpa_supplicant_ctrl_iface_tdls_setup(struct wpa_supplicant * wpa_s,char * addr)1113 static int wpa_supplicant_ctrl_iface_tdls_setup(
1114 	struct wpa_supplicant *wpa_s, char *addr)
1115 {
1116 	u8 peer[ETH_ALEN];
1117 	int ret;
1118 
1119 	if (hwaddr_aton(addr, peer)) {
1120 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
1121 			   "address '%s'", addr);
1122 		return -1;
1123 	}
1124 
1125 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
1126 		   MAC2STR(peer));
1127 
1128 	if ((wpa_s->conf->tdls_external_control) &&
1129 	    wpa_tdls_is_external_setup(wpa_s->wpa))
1130 		return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
1131 
1132 	wpa_tdls_remove(wpa_s->wpa, peer);
1133 
1134 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1135 		ret = wpa_tdls_start(wpa_s->wpa, peer);
1136 	else
1137 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
1138 
1139 	return ret;
1140 }
1141 
1142 
wpa_supplicant_ctrl_iface_tdls_teardown(struct wpa_supplicant * wpa_s,char * addr)1143 static int wpa_supplicant_ctrl_iface_tdls_teardown(
1144 	struct wpa_supplicant *wpa_s, char *addr)
1145 {
1146 	u8 peer[ETH_ALEN];
1147 	int ret;
1148 
1149 	if (os_strcmp(addr, "*") == 0) {
1150 		/* remove everyone */
1151 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
1152 		wpa_tdls_teardown_peers(wpa_s->wpa);
1153 		return 0;
1154 	}
1155 
1156 	if (hwaddr_aton(addr, peer)) {
1157 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
1158 			   "address '%s'", addr);
1159 		return -1;
1160 	}
1161 
1162 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
1163 		   MAC2STR(peer));
1164 
1165 	if ((wpa_s->conf->tdls_external_control) &&
1166 	    wpa_tdls_is_external_setup(wpa_s->wpa))
1167 		return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
1168 
1169 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1170 		ret = wpa_tdls_teardown_link(
1171 			wpa_s->wpa, peer,
1172 			WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
1173 	else
1174 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
1175 
1176 	return ret;
1177 }
1178 
1179 
ctrl_iface_get_capability_tdls(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)1180 static int ctrl_iface_get_capability_tdls(
1181 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1182 {
1183 	int ret;
1184 
1185 	ret = os_snprintf(buf, buflen, "%s\n",
1186 			  wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
1187 			  (wpa_s->drv_flags &
1188 			   WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
1189 			   "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
1190 	if (os_snprintf_error(buflen, ret))
1191 		return -1;
1192 	return ret;
1193 }
1194 
1195 
wpa_supplicant_ctrl_iface_tdls_chan_switch(struct wpa_supplicant * wpa_s,char * cmd)1196 static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
1197 	struct wpa_supplicant *wpa_s, char *cmd)
1198 {
1199 	u8 peer[ETH_ALEN];
1200 	struct hostapd_freq_params freq_params;
1201 	u8 oper_class;
1202 	char *pos, *end;
1203 
1204 	if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
1205 		wpa_printf(MSG_INFO,
1206 			   "tdls_chanswitch: Only supported with external setup");
1207 		return -1;
1208 	}
1209 
1210 	os_memset(&freq_params, 0, sizeof(freq_params));
1211 
1212 	pos = os_strchr(cmd, ' ');
1213 	if (pos == NULL)
1214 		return -1;
1215 	*pos++ = '\0';
1216 
1217 	oper_class = strtol(pos, &end, 10);
1218 	if (pos == end) {
1219 		wpa_printf(MSG_INFO,
1220 			   "tdls_chanswitch: Invalid op class provided");
1221 		return -1;
1222 	}
1223 
1224 	pos = end;
1225 	freq_params.freq = atoi(pos);
1226 	if (freq_params.freq == 0) {
1227 		wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
1228 		return -1;
1229 	}
1230 
1231 #define SET_FREQ_SETTING(str) \
1232 	do { \
1233 		const char *pos2 = os_strstr(pos, " " #str "="); \
1234 		if (pos2) { \
1235 			pos2 += sizeof(" " #str "=") - 1; \
1236 			freq_params.str = atoi(pos2); \
1237 		} \
1238 	} while (0)
1239 
1240 	SET_FREQ_SETTING(center_freq1);
1241 	SET_FREQ_SETTING(center_freq2);
1242 	SET_FREQ_SETTING(bandwidth);
1243 	SET_FREQ_SETTING(sec_channel_offset);
1244 #undef SET_FREQ_SETTING
1245 
1246 	freq_params.ht_enabled = !!os_strstr(pos, " ht");
1247 	freq_params.vht_enabled = !!os_strstr(pos, " vht");
1248 
1249 	if (hwaddr_aton(cmd, peer)) {
1250 		wpa_printf(MSG_DEBUG,
1251 			   "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
1252 			   cmd);
1253 		return -1;
1254 	}
1255 
1256 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
1257 		   " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
1258 		   MAC2STR(peer), oper_class, freq_params.freq,
1259 		   freq_params.center_freq1, freq_params.center_freq2,
1260 		   freq_params.bandwidth, freq_params.sec_channel_offset,
1261 		   freq_params.ht_enabled ? " HT" : "",
1262 		   freq_params.vht_enabled ? " VHT" : "");
1263 
1264 	return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
1265 					   &freq_params);
1266 }
1267 
1268 
wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(struct wpa_supplicant * wpa_s,char * cmd)1269 static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
1270 	struct wpa_supplicant *wpa_s, char *cmd)
1271 {
1272 	u8 peer[ETH_ALEN];
1273 
1274 	if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
1275 		wpa_printf(MSG_INFO,
1276 			   "tdls_chanswitch: Only supported with external setup");
1277 		return -1;
1278 	}
1279 
1280 	if (hwaddr_aton(cmd, peer)) {
1281 		wpa_printf(MSG_DEBUG,
1282 			   "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
1283 			   cmd);
1284 		return -1;
1285 	}
1286 
1287 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
1288 		   MAC2STR(peer));
1289 
1290 	return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
1291 }
1292 
1293 
wpa_supplicant_ctrl_iface_tdls_link_status(struct wpa_supplicant * wpa_s,const char * addr,char * buf,size_t buflen)1294 static int wpa_supplicant_ctrl_iface_tdls_link_status(
1295 	struct wpa_supplicant *wpa_s, const char *addr,
1296 	char *buf, size_t buflen)
1297 {
1298 	u8 peer[ETH_ALEN];
1299 	const char *tdls_status;
1300 	int ret;
1301 
1302 	if (hwaddr_aton(addr, peer)) {
1303 		wpa_printf(MSG_DEBUG,
1304 			   "CTRL_IFACE TDLS_LINK_STATUS: Invalid address '%s'",
1305 			   addr);
1306 		return -1;
1307 	}
1308 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS " MACSTR,
1309 		   MAC2STR(peer));
1310 
1311 	tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
1312 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS: %s", tdls_status);
1313 	ret = os_snprintf(buf, buflen, "TDLS link status: %s\n", tdls_status);
1314 	if (os_snprintf_error(buflen, ret))
1315 		return -1;
1316 
1317 	return ret;
1318 }
1319 
1320 #endif /* CONFIG_TDLS */
1321 
1322 
1323 #ifndef CONFIG_NO_WMM_AC
1324 
wmm_ac_ctrl_addts(struct wpa_supplicant * wpa_s,char * cmd)1325 static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
1326 {
1327 	char *token, *context = NULL;
1328 	struct wmm_ac_ts_setup_params params = {
1329 		.tsid = 0xff,
1330 		.direction = 0xff,
1331 	};
1332 
1333 	while ((token = str_token(cmd, " ", &context))) {
1334 		if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
1335 		    sscanf(token, "up=%i", &params.user_priority) == 1 ||
1336 		    sscanf(token, "nominal_msdu_size=%i",
1337 			   &params.nominal_msdu_size) == 1 ||
1338 		    sscanf(token, "mean_data_rate=%i",
1339 			   &params.mean_data_rate) == 1 ||
1340 		    sscanf(token, "min_phy_rate=%i",
1341 			   &params.minimum_phy_rate) == 1 ||
1342 		    sscanf(token, "sba=%i",
1343 			   &params.surplus_bandwidth_allowance) == 1)
1344 			continue;
1345 
1346 		if (os_strcasecmp(token, "downlink") == 0) {
1347 			params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
1348 		} else if (os_strcasecmp(token, "uplink") == 0) {
1349 			params.direction = WMM_TSPEC_DIRECTION_UPLINK;
1350 		} else if (os_strcasecmp(token, "bidi") == 0) {
1351 			params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
1352 		} else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
1353 			params.fixed_nominal_msdu = 1;
1354 		} else {
1355 			wpa_printf(MSG_DEBUG,
1356 				   "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
1357 				   token);
1358 			return -1;
1359 		}
1360 
1361 	}
1362 
1363 	return wpas_wmm_ac_addts(wpa_s, &params);
1364 }
1365 
1366 
wmm_ac_ctrl_delts(struct wpa_supplicant * wpa_s,char * cmd)1367 static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
1368 {
1369 	u8 tsid = atoi(cmd);
1370 
1371 	return wpas_wmm_ac_delts(wpa_s, tsid);
1372 }
1373 
1374 #endif /* CONFIG_NO_WMM_AC */
1375 
1376 
1377 #ifdef CONFIG_IEEE80211R
wpa_supplicant_ctrl_iface_ft_ds(struct wpa_supplicant * wpa_s,char * addr)1378 static int wpa_supplicant_ctrl_iface_ft_ds(
1379 	struct wpa_supplicant *wpa_s, char *addr)
1380 {
1381 	u8 target_ap[ETH_ALEN];
1382 	struct wpa_bss *bss;
1383 	const u8 *mdie;
1384 	bool force = os_strstr(addr, " force") != NULL;
1385 
1386 	if (hwaddr_aton(addr, target_ap)) {
1387 		wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
1388 			   "address '%s'", addr);
1389 		return -1;
1390 	}
1391 
1392 	wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
1393 
1394 	bss = wpa_bss_get_bssid(wpa_s, target_ap);
1395 	if (bss)
1396 		mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1397 	else
1398 		mdie = NULL;
1399 
1400 	return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie, force);
1401 }
1402 #endif /* CONFIG_IEEE80211R */
1403 
1404 
1405 #ifdef CONFIG_WPS
wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant * wpa_s,char * cmd)1406 static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
1407 					     char *cmd)
1408 {
1409 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1410 #ifdef CONFIG_P2P
1411 	u8 p2p_dev_addr[ETH_ALEN];
1412 #endif /* CONFIG_P2P */
1413 #ifdef CONFIG_AP
1414 	u8 *_p2p_dev_addr = NULL;
1415 #endif /* CONFIG_AP */
1416 	char *pos;
1417 	int multi_ap = 0;
1418 
1419 	if (!cmd || os_strcmp(cmd, "any") == 0 ||
1420 	    os_strncmp(cmd, "any ", 4) == 0) {
1421 		_bssid = NULL;
1422 #ifdef CONFIG_P2P
1423 	} else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
1424 		if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
1425 			wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
1426 				   "P2P Device Address '%s'",
1427 				   cmd + 13);
1428 			return -1;
1429 		}
1430 		_p2p_dev_addr = p2p_dev_addr;
1431 #endif /* CONFIG_P2P */
1432 	} else if (os_strncmp(cmd, "multi_ap=", 9) == 0) {
1433 		_bssid = NULL;
1434 		multi_ap = atoi(cmd + 9);
1435 	} else if (hwaddr_aton(cmd, bssid)) {
1436 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
1437 			   cmd);
1438 		return -1;
1439 	}
1440 
1441 	if (cmd) {
1442 		pos = os_strstr(cmd, " multi_ap=");
1443 		if (pos) {
1444 			pos += 10;
1445 			multi_ap = atoi(pos);
1446 		}
1447 	}
1448 
1449 #ifdef CONFIG_AP
1450 	if (wpa_s->ap_iface)
1451 		return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
1452 #endif /* CONFIG_AP */
1453 
1454 	return wpas_wps_start_pbc(wpa_s, _bssid, 0, multi_ap);
1455 }
1456 
1457 
wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1458 static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
1459 					     char *cmd, char *buf,
1460 					     size_t buflen)
1461 {
1462 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1463 	char *pin;
1464 	int ret;
1465 
1466 	pin = os_strchr(cmd, ' ');
1467 	if (pin)
1468 		*pin++ = '\0';
1469 
1470 	if (os_strcmp(cmd, "any") == 0)
1471 		_bssid = NULL;
1472 	else if (os_strcmp(cmd, "get") == 0) {
1473 		if (wps_generate_pin((unsigned int *) &ret) < 0)
1474 			return -1;
1475 		goto done;
1476 	} else if (hwaddr_aton(cmd, bssid)) {
1477 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
1478 			   cmd);
1479 		return -1;
1480 	}
1481 
1482 #ifdef CONFIG_AP
1483 	if (wpa_s->ap_iface) {
1484 		int timeout = 0;
1485 		char *pos;
1486 
1487 		if (pin) {
1488 			pos = os_strchr(pin, ' ');
1489 			if (pos) {
1490 				*pos++ = '\0';
1491 				timeout = atoi(pos);
1492 			}
1493 		}
1494 
1495 		return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
1496 						 buf, buflen, timeout);
1497 	}
1498 #endif /* CONFIG_AP */
1499 
1500 	if (pin) {
1501 		ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
1502 					 DEV_PW_DEFAULT);
1503 		if (ret < 0)
1504 			return -1;
1505 		ret = os_snprintf(buf, buflen, "%s", pin);
1506 		if (os_snprintf_error(buflen, ret))
1507 			return -1;
1508 		return ret;
1509 	}
1510 
1511 	ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
1512 	if (ret < 0)
1513 		return -1;
1514 
1515 done:
1516 	/* Return the generated PIN */
1517 	ret = os_snprintf(buf, buflen, "%08d", ret);
1518 	if (os_snprintf_error(buflen, ret))
1519 		return -1;
1520 	return ret;
1521 }
1522 
1523 
wpa_supplicant_ctrl_iface_wps_check_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1524 static int wpa_supplicant_ctrl_iface_wps_check_pin(
1525 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1526 {
1527 	char pin[9];
1528 	size_t len;
1529 	char *pos;
1530 	int ret;
1531 
1532 	wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
1533 			      (u8 *) cmd, os_strlen(cmd));
1534 	for (pos = cmd, len = 0; *pos != '\0'; pos++) {
1535 		if (*pos < '0' || *pos > '9')
1536 			continue;
1537 		pin[len++] = *pos;
1538 		if (len == 9) {
1539 			wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
1540 			return -1;
1541 		}
1542 	}
1543 	if (len != 4 && len != 8) {
1544 		wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
1545 		return -1;
1546 	}
1547 	pin[len] = '\0';
1548 
1549 	if (len == 8) {
1550 		unsigned int pin_val;
1551 		pin_val = atoi(pin);
1552 		if (!wps_pin_valid(pin_val)) {
1553 			wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
1554 			ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
1555 			if (os_snprintf_error(buflen, ret))
1556 				return -1;
1557 			return ret;
1558 		}
1559 	}
1560 
1561 	ret = os_snprintf(buf, buflen, "%s", pin);
1562 	if (os_snprintf_error(buflen, ret))
1563 		return -1;
1564 
1565 	return ret;
1566 }
1567 
1568 
1569 #ifdef CONFIG_WPS_NFC
1570 
wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant * wpa_s,char * cmd)1571 static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
1572 					     char *cmd)
1573 {
1574 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1575 
1576 	if (cmd == NULL || cmd[0] == '\0')
1577 		_bssid = NULL;
1578 	else if (hwaddr_aton(cmd, bssid))
1579 		return -1;
1580 
1581 	return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
1582 				  0, 0);
1583 }
1584 
1585 
wpa_supplicant_ctrl_iface_wps_nfc_config_token(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1586 static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
1587 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1588 {
1589 	int ndef;
1590 	struct wpabuf *buf;
1591 	int res;
1592 	char *pos;
1593 
1594 	pos = os_strchr(cmd, ' ');
1595 	if (pos)
1596 		*pos++ = '\0';
1597 	if (os_strcmp(cmd, "WPS") == 0)
1598 		ndef = 0;
1599 	else if (os_strcmp(cmd, "NDEF") == 0)
1600 		ndef = 1;
1601 	else
1602 		return -1;
1603 
1604 	buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
1605 	if (buf == NULL)
1606 		return -1;
1607 
1608 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1609 					 wpabuf_len(buf));
1610 	reply[res++] = '\n';
1611 	reply[res] = '\0';
1612 
1613 	wpabuf_free(buf);
1614 
1615 	return res;
1616 }
1617 
1618 
wpa_supplicant_ctrl_iface_wps_nfc_token(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1619 static int wpa_supplicant_ctrl_iface_wps_nfc_token(
1620 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1621 {
1622 	int ndef;
1623 	struct wpabuf *buf;
1624 	int res;
1625 
1626 	if (os_strcmp(cmd, "WPS") == 0)
1627 		ndef = 0;
1628 	else if (os_strcmp(cmd, "NDEF") == 0)
1629 		ndef = 1;
1630 	else
1631 		return -1;
1632 
1633 	buf = wpas_wps_nfc_token(wpa_s, ndef);
1634 	if (buf == NULL)
1635 		return -1;
1636 
1637 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1638 					 wpabuf_len(buf));
1639 	reply[res++] = '\n';
1640 	reply[res] = '\0';
1641 
1642 	wpabuf_free(buf);
1643 
1644 	return res;
1645 }
1646 
1647 
wpa_supplicant_ctrl_iface_wps_nfc_tag_read(struct wpa_supplicant * wpa_s,char * pos)1648 static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
1649 	struct wpa_supplicant *wpa_s, char *pos)
1650 {
1651 	size_t len;
1652 	struct wpabuf *buf;
1653 	int ret;
1654 	char *freq;
1655 	int forced_freq = 0;
1656 
1657 	freq = strstr(pos, " freq=");
1658 	if (freq) {
1659 		*freq = '\0';
1660 		freq += 6;
1661 		forced_freq = atoi(freq);
1662 	}
1663 
1664 	len = os_strlen(pos);
1665 	if (len & 0x01)
1666 		return -1;
1667 	len /= 2;
1668 
1669 	buf = wpabuf_alloc(len);
1670 	if (buf == NULL)
1671 		return -1;
1672 	if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
1673 		wpabuf_free(buf);
1674 		return -1;
1675 	}
1676 
1677 	ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
1678 	wpabuf_free(buf);
1679 
1680 	return ret;
1681 }
1682 
1683 
wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef)1684 static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
1685 					      char *reply, size_t max_len,
1686 					      int ndef)
1687 {
1688 	struct wpabuf *buf;
1689 	int res;
1690 
1691 	buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
1692 	if (buf == NULL)
1693 		return -1;
1694 
1695 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1696 					 wpabuf_len(buf));
1697 	reply[res++] = '\n';
1698 	reply[res] = '\0';
1699 
1700 	wpabuf_free(buf);
1701 
1702 	return res;
1703 }
1704 
1705 
1706 #ifdef CONFIG_P2P
wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef)1707 static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
1708 					      char *reply, size_t max_len,
1709 					      int ndef)
1710 {
1711 	struct wpabuf *buf;
1712 	int res;
1713 
1714 	buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
1715 	if (buf == NULL) {
1716 		wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
1717 		return -1;
1718 	}
1719 
1720 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1721 					 wpabuf_len(buf));
1722 	reply[res++] = '\n';
1723 	reply[res] = '\0';
1724 
1725 	wpabuf_free(buf);
1726 
1727 	return res;
1728 }
1729 #endif /* CONFIG_P2P */
1730 
1731 
wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1732 static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
1733 					  char *cmd, char *reply,
1734 					  size_t max_len)
1735 {
1736 	char *pos;
1737 	int ndef;
1738 
1739 	pos = os_strchr(cmd, ' ');
1740 	if (pos == NULL)
1741 		return -1;
1742 	*pos++ = '\0';
1743 
1744 	if (os_strcmp(cmd, "WPS") == 0)
1745 		ndef = 0;
1746 	else if (os_strcmp(cmd, "NDEF") == 0)
1747 		ndef = 1;
1748 	else
1749 		return -1;
1750 
1751 	if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
1752 		if (!ndef)
1753 			return -1;
1754 		return wpas_ctrl_nfc_get_handover_req_wps(
1755 			wpa_s, reply, max_len, ndef);
1756 	}
1757 
1758 #ifdef CONFIG_P2P
1759 	if (os_strcmp(pos, "P2P-CR") == 0) {
1760 		return wpas_ctrl_nfc_get_handover_req_p2p(
1761 			wpa_s, reply, max_len, ndef);
1762 	}
1763 #endif /* CONFIG_P2P */
1764 
1765 	return -1;
1766 }
1767 
1768 
wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef,int cr,char * uuid)1769 static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
1770 					      char *reply, size_t max_len,
1771 					      int ndef, int cr, char *uuid)
1772 {
1773 	struct wpabuf *buf;
1774 	int res;
1775 
1776 	buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
1777 	if (buf == NULL)
1778 		return -1;
1779 
1780 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1781 					 wpabuf_len(buf));
1782 	reply[res++] = '\n';
1783 	reply[res] = '\0';
1784 
1785 	wpabuf_free(buf);
1786 
1787 	return res;
1788 }
1789 
1790 
1791 #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)1792 static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1793 					      char *reply, size_t max_len,
1794 					      int ndef, int tag)
1795 {
1796 	struct wpabuf *buf;
1797 	int res;
1798 
1799 	buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1800 	if (buf == NULL)
1801 		return -1;
1802 
1803 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1804 					 wpabuf_len(buf));
1805 	reply[res++] = '\n';
1806 	reply[res] = '\0';
1807 
1808 	wpabuf_free(buf);
1809 
1810 	return res;
1811 }
1812 #endif /* CONFIG_P2P */
1813 
1814 
wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1815 static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1816 					  char *cmd, char *reply,
1817 					  size_t max_len)
1818 {
1819 	char *pos, *pos2;
1820 	int ndef;
1821 
1822 	pos = os_strchr(cmd, ' ');
1823 	if (pos == NULL)
1824 		return -1;
1825 	*pos++ = '\0';
1826 
1827 	if (os_strcmp(cmd, "WPS") == 0)
1828 		ndef = 0;
1829 	else if (os_strcmp(cmd, "NDEF") == 0)
1830 		ndef = 1;
1831 	else
1832 		return -1;
1833 
1834 	pos2 = os_strchr(pos, ' ');
1835 	if (pos2)
1836 		*pos2++ = '\0';
1837 	if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
1838 		if (!ndef)
1839 			return -1;
1840 		return wpas_ctrl_nfc_get_handover_sel_wps(
1841 			wpa_s, reply, max_len, ndef,
1842 			os_strcmp(pos, "WPS-CR") == 0, pos2);
1843 	}
1844 
1845 #ifdef CONFIG_P2P
1846 	if (os_strcmp(pos, "P2P-CR") == 0) {
1847 		return wpas_ctrl_nfc_get_handover_sel_p2p(
1848 			wpa_s, reply, max_len, ndef, 0);
1849 	}
1850 
1851 	if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1852 		return wpas_ctrl_nfc_get_handover_sel_p2p(
1853 			wpa_s, reply, max_len, ndef, 1);
1854 	}
1855 #endif /* CONFIG_P2P */
1856 
1857 	return -1;
1858 }
1859 
1860 
wpas_ctrl_nfc_report_handover(struct wpa_supplicant * wpa_s,char * cmd)1861 static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1862 					 char *cmd)
1863 {
1864 	size_t len;
1865 	struct wpabuf *req, *sel;
1866 	int ret;
1867 	char *pos, *role, *type, *pos2;
1868 #ifdef CONFIG_P2P
1869 	char *freq;
1870 	int forced_freq = 0;
1871 
1872 	freq = strstr(cmd, " freq=");
1873 	if (freq) {
1874 		*freq = '\0';
1875 		freq += 6;
1876 		forced_freq = atoi(freq);
1877 	}
1878 #endif /* CONFIG_P2P */
1879 
1880 	role = cmd;
1881 	pos = os_strchr(role, ' ');
1882 	if (pos == NULL) {
1883 		wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
1884 		return -1;
1885 	}
1886 	*pos++ = '\0';
1887 
1888 	type = pos;
1889 	pos = os_strchr(type, ' ');
1890 	if (pos == NULL) {
1891 		wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
1892 		return -1;
1893 	}
1894 	*pos++ = '\0';
1895 
1896 	pos2 = os_strchr(pos, ' ');
1897 	if (pos2 == NULL) {
1898 		wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
1899 		return -1;
1900 	}
1901 	*pos2++ = '\0';
1902 
1903 	len = os_strlen(pos);
1904 	if (len & 0x01) {
1905 		wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
1906 		return -1;
1907 	}
1908 	len /= 2;
1909 
1910 	req = wpabuf_alloc(len);
1911 	if (req == NULL) {
1912 		wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
1913 		return -1;
1914 	}
1915 	if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
1916 		wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
1917 		wpabuf_free(req);
1918 		return -1;
1919 	}
1920 
1921 	len = os_strlen(pos2);
1922 	if (len & 0x01) {
1923 		wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
1924 		wpabuf_free(req);
1925 		return -1;
1926 	}
1927 	len /= 2;
1928 
1929 	sel = wpabuf_alloc(len);
1930 	if (sel == NULL) {
1931 		wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
1932 		wpabuf_free(req);
1933 		return -1;
1934 	}
1935 	if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
1936 		wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
1937 		wpabuf_free(req);
1938 		wpabuf_free(sel);
1939 		return -1;
1940 	}
1941 
1942 	wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1943 		   role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1944 
1945 	if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1946 		ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
1947 #ifdef CONFIG_AP
1948 	} else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1949 	{
1950 		ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
1951 		if (ret < 0)
1952 			ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
1953 #endif /* CONFIG_AP */
1954 #ifdef CONFIG_P2P
1955 	} else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1956 	{
1957 		ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
1958 	} else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1959 	{
1960 		ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1961 						   forced_freq);
1962 #endif /* CONFIG_P2P */
1963 	} else {
1964 		wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1965 			   "reported: role=%s type=%s", role, type);
1966 		ret = -1;
1967 	}
1968 	wpabuf_free(req);
1969 	wpabuf_free(sel);
1970 
1971 	if (ret)
1972 		wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1973 
1974 	return ret;
1975 }
1976 
1977 #endif /* CONFIG_WPS_NFC */
1978 
1979 
wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant * wpa_s,char * cmd)1980 static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1981 					     char *cmd)
1982 {
1983 	u8 bssid[ETH_ALEN];
1984 	char *pin;
1985 	char *new_ssid;
1986 	char *new_auth;
1987 	char *new_encr;
1988 	char *new_key;
1989 	struct wps_new_ap_settings ap;
1990 
1991 	pin = os_strchr(cmd, ' ');
1992 	if (pin == NULL)
1993 		return -1;
1994 	*pin++ = '\0';
1995 
1996 	if (hwaddr_aton(cmd, bssid)) {
1997 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1998 			   cmd);
1999 		return -1;
2000 	}
2001 
2002 	new_ssid = os_strchr(pin, ' ');
2003 	if (new_ssid == NULL)
2004 		return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
2005 	*new_ssid++ = '\0';
2006 
2007 	new_auth = os_strchr(new_ssid, ' ');
2008 	if (new_auth == NULL)
2009 		return -1;
2010 	*new_auth++ = '\0';
2011 
2012 	new_encr = os_strchr(new_auth, ' ');
2013 	if (new_encr == NULL)
2014 		return -1;
2015 	*new_encr++ = '\0';
2016 
2017 	new_key = os_strchr(new_encr, ' ');
2018 	if (new_key == NULL)
2019 		return -1;
2020 	*new_key++ = '\0';
2021 
2022 	os_memset(&ap, 0, sizeof(ap));
2023 	ap.ssid_hex = new_ssid;
2024 	ap.auth = new_auth;
2025 	ap.encr = new_encr;
2026 	ap.key_hex = new_key;
2027 	return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
2028 }
2029 
2030 
2031 #ifdef CONFIG_AP
wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2032 static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
2033 						char *cmd, char *buf,
2034 						size_t buflen)
2035 {
2036 	int timeout = 300;
2037 	char *pos;
2038 	const char *pin_txt;
2039 
2040 	if (!wpa_s->ap_iface)
2041 		return -1;
2042 
2043 	pos = os_strchr(cmd, ' ');
2044 	if (pos)
2045 		*pos++ = '\0';
2046 
2047 	if (os_strcmp(cmd, "disable") == 0) {
2048 		wpas_wps_ap_pin_disable(wpa_s);
2049 		return os_snprintf(buf, buflen, "OK\n");
2050 	}
2051 
2052 	if (os_strcmp(cmd, "random") == 0) {
2053 		if (pos)
2054 			timeout = atoi(pos);
2055 		pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
2056 		if (pin_txt == NULL)
2057 			return -1;
2058 		return os_snprintf(buf, buflen, "%s", pin_txt);
2059 	}
2060 
2061 	if (os_strcmp(cmd, "get") == 0) {
2062 		pin_txt = wpas_wps_ap_pin_get(wpa_s);
2063 		if (pin_txt == NULL)
2064 			return -1;
2065 		return os_snprintf(buf, buflen, "%s", pin_txt);
2066 	}
2067 
2068 	if (os_strcmp(cmd, "set") == 0) {
2069 		char *pin;
2070 		if (pos == NULL)
2071 			return -1;
2072 		pin = pos;
2073 		pos = os_strchr(pos, ' ');
2074 		if (pos) {
2075 			*pos++ = '\0';
2076 			timeout = atoi(pos);
2077 		}
2078 		if (os_strlen(pin) > buflen)
2079 			return -1;
2080 		if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
2081 			return -1;
2082 		return os_snprintf(buf, buflen, "%s", pin);
2083 	}
2084 
2085 	return -1;
2086 }
2087 #endif /* CONFIG_AP */
2088 
2089 
2090 #ifdef CONFIG_WPS_ER
wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant * wpa_s,char * cmd)2091 static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
2092 						char *cmd)
2093 {
2094 	char *uuid = cmd, *pin, *pos;
2095 	u8 addr_buf[ETH_ALEN], *addr = NULL;
2096 	pin = os_strchr(uuid, ' ');
2097 	if (pin == NULL)
2098 		return -1;
2099 	*pin++ = '\0';
2100 	pos = os_strchr(pin, ' ');
2101 	if (pos) {
2102 		*pos++ = '\0';
2103 		if (hwaddr_aton(pos, addr_buf) == 0)
2104 			addr = addr_buf;
2105 	}
2106 	return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
2107 }
2108 
2109 
wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant * wpa_s,char * cmd)2110 static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
2111 						  char *cmd)
2112 {
2113 	char *uuid = cmd, *pin;
2114 	pin = os_strchr(uuid, ' ');
2115 	if (pin == NULL)
2116 		return -1;
2117 	*pin++ = '\0';
2118 	return wpas_wps_er_learn(wpa_s, uuid, pin);
2119 }
2120 
2121 
wpa_supplicant_ctrl_iface_wps_er_set_config(struct wpa_supplicant * wpa_s,char * cmd)2122 static int wpa_supplicant_ctrl_iface_wps_er_set_config(
2123 	struct wpa_supplicant *wpa_s, char *cmd)
2124 {
2125 	char *uuid = cmd, *id;
2126 	id = os_strchr(uuid, ' ');
2127 	if (id == NULL)
2128 		return -1;
2129 	*id++ = '\0';
2130 	return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
2131 }
2132 
2133 
wpa_supplicant_ctrl_iface_wps_er_config(struct wpa_supplicant * wpa_s,char * cmd)2134 static int wpa_supplicant_ctrl_iface_wps_er_config(
2135 	struct wpa_supplicant *wpa_s, char *cmd)
2136 {
2137 	char *pin;
2138 	char *new_ssid;
2139 	char *new_auth;
2140 	char *new_encr;
2141 	char *new_key;
2142 	struct wps_new_ap_settings ap;
2143 
2144 	pin = os_strchr(cmd, ' ');
2145 	if (pin == NULL)
2146 		return -1;
2147 	*pin++ = '\0';
2148 
2149 	new_ssid = os_strchr(pin, ' ');
2150 	if (new_ssid == NULL)
2151 		return -1;
2152 	*new_ssid++ = '\0';
2153 
2154 	new_auth = os_strchr(new_ssid, ' ');
2155 	if (new_auth == NULL)
2156 		return -1;
2157 	*new_auth++ = '\0';
2158 
2159 	new_encr = os_strchr(new_auth, ' ');
2160 	if (new_encr == NULL)
2161 		return -1;
2162 	*new_encr++ = '\0';
2163 
2164 	new_key = os_strchr(new_encr, ' ');
2165 	if (new_key == NULL)
2166 		return -1;
2167 	*new_key++ = '\0';
2168 
2169 	os_memset(&ap, 0, sizeof(ap));
2170 	ap.ssid_hex = new_ssid;
2171 	ap.auth = new_auth;
2172 	ap.encr = new_encr;
2173 	ap.key_hex = new_key;
2174 	return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
2175 }
2176 
2177 
2178 #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)2179 static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
2180 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
2181 {
2182 	int ndef;
2183 	struct wpabuf *buf;
2184 	int res;
2185 	char *uuid;
2186 
2187 	uuid = os_strchr(cmd, ' ');
2188 	if (uuid == NULL)
2189 		return -1;
2190 	*uuid++ = '\0';
2191 
2192 	if (os_strcmp(cmd, "WPS") == 0)
2193 		ndef = 0;
2194 	else if (os_strcmp(cmd, "NDEF") == 0)
2195 		ndef = 1;
2196 	else
2197 		return -1;
2198 
2199 	buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
2200 	if (buf == NULL)
2201 		return -1;
2202 
2203 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
2204 					 wpabuf_len(buf));
2205 	reply[res++] = '\n';
2206 	reply[res] = '\0';
2207 
2208 	wpabuf_free(buf);
2209 
2210 	return res;
2211 }
2212 #endif /* CONFIG_WPS_NFC */
2213 #endif /* CONFIG_WPS_ER */
2214 
2215 #endif /* CONFIG_WPS */
2216 
2217 
2218 #ifdef CONFIG_IBSS_RSN
wpa_supplicant_ctrl_iface_ibss_rsn(struct wpa_supplicant * wpa_s,char * addr)2219 static int wpa_supplicant_ctrl_iface_ibss_rsn(
2220 	struct wpa_supplicant *wpa_s, char *addr)
2221 {
2222 	u8 peer[ETH_ALEN];
2223 
2224 	if (hwaddr_aton(addr, peer)) {
2225 		wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
2226 			   "address '%s'", addr);
2227 		return -1;
2228 	}
2229 
2230 	wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
2231 		   MAC2STR(peer));
2232 
2233 	return ibss_rsn_start(wpa_s->ibss_rsn, peer);
2234 }
2235 #endif /* CONFIG_IBSS_RSN */
2236 
2237 
wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant * wpa_s,char * rsp)2238 static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
2239 					      char *rsp)
2240 {
2241 #ifdef IEEE8021X_EAPOL
2242 	char *pos, *id_pos;
2243 	int id;
2244 	struct wpa_ssid *ssid;
2245 
2246 	pos = os_strchr(rsp, '-');
2247 	if (pos == NULL)
2248 		return -1;
2249 	*pos++ = '\0';
2250 	id_pos = pos;
2251 	pos = os_strchr(pos, ':');
2252 	if (pos == NULL)
2253 		return -1;
2254 	*pos++ = '\0';
2255 	id = atoi(id_pos);
2256 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
2257 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2258 			      (u8 *) pos, os_strlen(pos));
2259 
2260 	ssid = wpa_config_get_network(wpa_s->conf, id);
2261 	if (ssid == NULL) {
2262 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2263 			   "to update", id);
2264 		return -1;
2265 	}
2266 
2267 	return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
2268 							 pos);
2269 #else /* IEEE8021X_EAPOL */
2270 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
2271 	return -1;
2272 #endif /* IEEE8021X_EAPOL */
2273 }
2274 
2275 
wpa_supplicant_ctrl_iface_status(struct wpa_supplicant * wpa_s,const char * params,char * buf,size_t buflen)2276 static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
2277 					    const char *params,
2278 					    char *buf, size_t buflen)
2279 {
2280 	char *pos, *end, tmp[30];
2281 	int res, verbose, wps, ret;
2282 #ifdef CONFIG_HS20
2283 	const u8 *hs20;
2284 #endif /* CONFIG_HS20 */
2285 	const u8 *sess_id;
2286 	size_t sess_id_len;
2287 
2288 	if (os_strcmp(params, "-DRIVER") == 0)
2289 		return wpa_drv_status(wpa_s, buf, buflen);
2290 	verbose = os_strcmp(params, "-VERBOSE") == 0;
2291 	wps = os_strcmp(params, "-WPS") == 0;
2292 	pos = buf;
2293 	end = buf + buflen;
2294 	if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
2295 		struct wpa_ssid *ssid = wpa_s->current_ssid;
2296 		ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
2297 				  MAC2STR(wpa_s->bssid));
2298 		if (os_snprintf_error(end - pos, ret))
2299 			return pos - buf;
2300 		pos += ret;
2301 		ret = os_snprintf(pos, end - pos, "freq=%u\n",
2302 				  wpa_s->assoc_freq);
2303 		if (os_snprintf_error(end - pos, ret))
2304 			return pos - buf;
2305 		pos += ret;
2306 		if (ssid) {
2307 			u8 *_ssid = ssid->ssid;
2308 			size_t ssid_len = ssid->ssid_len;
2309 			u8 ssid_buf[SSID_MAX_LEN];
2310 			if (ssid_len == 0) {
2311 				int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
2312 				if (_res < 0)
2313 					ssid_len = 0;
2314 				else
2315 					ssid_len = _res;
2316 				_ssid = ssid_buf;
2317 			}
2318 			ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
2319 					  wpa_ssid_txt(_ssid, ssid_len),
2320 					  ssid->id);
2321 			if (os_snprintf_error(end - pos, ret))
2322 				return pos - buf;
2323 			pos += ret;
2324 
2325 			if (wps && ssid->passphrase &&
2326 			    wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
2327 			    (ssid->mode == WPAS_MODE_AP ||
2328 			     ssid->mode == WPAS_MODE_P2P_GO)) {
2329 				ret = os_snprintf(pos, end - pos,
2330 						  "passphrase=%s\n",
2331 						  ssid->passphrase);
2332 				if (os_snprintf_error(end - pos, ret))
2333 					return pos - buf;
2334 				pos += ret;
2335 			}
2336 			if (ssid->id_str) {
2337 				ret = os_snprintf(pos, end - pos,
2338 						  "id_str=%s\n",
2339 						  ssid->id_str);
2340 				if (os_snprintf_error(end - pos, ret))
2341 					return pos - buf;
2342 				pos += ret;
2343 			}
2344 
2345 			switch (ssid->mode) {
2346 			case WPAS_MODE_INFRA:
2347 				ret = os_snprintf(pos, end - pos,
2348 						  "mode=station\n");
2349 				break;
2350 			case WPAS_MODE_IBSS:
2351 				ret = os_snprintf(pos, end - pos,
2352 						  "mode=IBSS\n");
2353 				break;
2354 			case WPAS_MODE_AP:
2355 				ret = os_snprintf(pos, end - pos,
2356 						  "mode=AP\n");
2357 				break;
2358 			case WPAS_MODE_P2P_GO:
2359 				ret = os_snprintf(pos, end - pos,
2360 						  "mode=P2P GO\n");
2361 				break;
2362 			case WPAS_MODE_P2P_GROUP_FORMATION:
2363 				ret = os_snprintf(pos, end - pos,
2364 						  "mode=P2P GO - group "
2365 						  "formation\n");
2366 				break;
2367 			case WPAS_MODE_MESH:
2368 				ret = os_snprintf(pos, end - pos,
2369 						  "mode=mesh\n");
2370 				break;
2371 			default:
2372 				ret = 0;
2373 				break;
2374 			}
2375 			if (os_snprintf_error(end - pos, ret))
2376 				return pos - buf;
2377 			pos += ret;
2378 		}
2379 
2380 		if (wpa_s->connection_set &&
2381 		    (wpa_s->connection_ht || wpa_s->connection_vht ||
2382 		     wpa_s->connection_he || wpa_s->connection_eht)) {
2383 			ret = os_snprintf(pos, end - pos,
2384 					  "wifi_generation=%u\n",
2385 					  wpa_s->connection_eht ? 7 :
2386 					  (wpa_s->connection_he ? 6 :
2387 					   (wpa_s->connection_vht ? 5 : 4)));
2388 			if (os_snprintf_error(end - pos, ret))
2389 				return pos - buf;
2390 			pos += ret;
2391 		}
2392 		if (wpa_s->connection_set) {
2393 			ret = os_snprintf(
2394 				pos, end - pos,
2395 				"max_nss_rx=%u\n"
2396 				"max_nss_tx=%u\n"
2397 				"channel_width=%u\n",
2398 				wpa_s->connection_max_nss_rx,
2399 				wpa_s->connection_max_nss_tx,
2400 				channel_width_to_int(
2401 					wpa_s->connection_channel_bandwidth));
2402 			if (os_snprintf_error(end - pos, ret))
2403 				return pos - buf;
2404 			pos += ret;
2405 		}
2406 
2407 #ifdef CONFIG_AP
2408 		if (wpa_s->ap_iface) {
2409 			pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
2410 							    end - pos,
2411 							    verbose);
2412 		} else
2413 #endif /* CONFIG_AP */
2414 		pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
2415 	}
2416 #ifdef CONFIG_SME
2417 #ifdef CONFIG_SAE
2418 	if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
2419 #ifdef CONFIG_AP
2420 	    !wpa_s->ap_iface &&
2421 #endif /* CONFIG_AP */
2422 	    wpa_s->sme.sae.state == SAE_ACCEPTED) {
2423 		ret = os_snprintf(pos, end - pos, "sae_group=%d\n"
2424 				  "sae_h2e=%d\n"
2425 				  "sae_pk=%d\n",
2426 				  wpa_s->sme.sae.group,
2427 				  wpa_s->sme.sae.h2e,
2428 				  wpa_s->sme.sae.pk);
2429 		if (os_snprintf_error(end - pos, ret))
2430 			return pos - buf;
2431 		pos += ret;
2432 	}
2433 #endif /* CONFIG_SAE */
2434 #endif /* CONFIG_SME */
2435 	ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
2436 			  wpa_supplicant_state_txt(wpa_s->wpa_state));
2437 	if (os_snprintf_error(end - pos, ret))
2438 		return pos - buf;
2439 	pos += ret;
2440 
2441 	if (wpa_s->l2 &&
2442 	    l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
2443 		ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
2444 		if (os_snprintf_error(end - pos, ret))
2445 			return pos - buf;
2446 		pos += ret;
2447 	}
2448 
2449 #ifdef CONFIG_P2P
2450 	if (wpa_s->global->p2p) {
2451 		ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
2452 				  "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
2453 		if (os_snprintf_error(end - pos, ret))
2454 			return pos - buf;
2455 		pos += ret;
2456 	}
2457 #endif /* CONFIG_P2P */
2458 
2459 	ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
2460 			  MAC2STR(wpa_s->own_addr));
2461 	if (os_snprintf_error(end - pos, ret))
2462 		return pos - buf;
2463 	pos += ret;
2464 
2465 	if (wpa_s->valid_links) {
2466 		ret = os_snprintf(pos, end - pos, "ap_mld_addr=" MACSTR "\n",
2467 				  MAC2STR(wpa_s->ap_mld_addr));
2468 		if (os_snprintf_error(end - pos, ret))
2469 			return pos - buf;
2470 		pos += ret;
2471 	}
2472 
2473 #ifdef CONFIG_HS20
2474 	if (wpa_s->current_bss &&
2475 	    (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2476 					  HS20_IE_VENDOR_TYPE)) &&
2477 	    wpa_s->wpa_proto == WPA_PROTO_RSN &&
2478 	    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2479 		int release = 1;
2480 		if (hs20[1] >= 5) {
2481 			u8 rel_num = (hs20[6] & 0xf0) >> 4;
2482 			release = rel_num + 1;
2483 		}
2484 		ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
2485 		if (os_snprintf_error(end - pos, ret))
2486 			return pos - buf;
2487 		pos += ret;
2488 	}
2489 
2490 	if (wpa_s->current_ssid) {
2491 		struct wpa_cred *cred;
2492 		char *type;
2493 
2494 		for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2495 			size_t i;
2496 
2497 			if (wpa_s->current_ssid->parent_cred != cred)
2498 				continue;
2499 
2500 			if (cred->provisioning_sp) {
2501 				ret = os_snprintf(pos, end - pos,
2502 						  "provisioning_sp=%s\n",
2503 						  cred->provisioning_sp);
2504 				if (os_snprintf_error(end - pos, ret))
2505 					return pos - buf;
2506 				pos += ret;
2507 			}
2508 
2509 			if (!cred->domain)
2510 				goto no_domain;
2511 
2512 			i = 0;
2513 			if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
2514 				struct wpabuf *names =
2515 					wpa_s->current_bss->anqp->domain_name;
2516 				for (i = 0; names && i < cred->num_domain; i++)
2517 				{
2518 					if (domain_name_list_contains(
2519 						    names, cred->domain[i], 1))
2520 						break;
2521 				}
2522 				if (i == cred->num_domain)
2523 					i = 0; /* show first entry by default */
2524 			}
2525 			ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
2526 					  cred->domain[i]);
2527 			if (os_snprintf_error(end - pos, ret))
2528 				return pos - buf;
2529 			pos += ret;
2530 
2531 		no_domain:
2532 			if (wpa_s->current_bss == NULL ||
2533 			    wpa_s->current_bss->anqp == NULL)
2534 				res = -1;
2535 			else
2536 				res = interworking_home_sp_cred(
2537 					wpa_s, cred,
2538 					wpa_s->current_bss->anqp->domain_name);
2539 			if (res > 0)
2540 				type = "home";
2541 			else if (res == 0)
2542 				type = "roaming";
2543 			else
2544 				type = "unknown";
2545 
2546 			ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
2547 			if (os_snprintf_error(end - pos, ret))
2548 				return pos - buf;
2549 			pos += ret;
2550 
2551 			break;
2552 		}
2553 	}
2554 #endif /* CONFIG_HS20 */
2555 
2556 	if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2557 	    wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2558 		res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
2559 					  verbose);
2560 		if (res >= 0)
2561 			pos += res;
2562 	}
2563 
2564 #ifdef CONFIG_MACSEC
2565 	res = ieee802_1x_kay_get_status(wpa_s->kay, pos, end - pos);
2566 	if (res > 0)
2567 		pos += res;
2568 #endif /* CONFIG_MACSEC */
2569 
2570 	sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
2571 	if (sess_id) {
2572 		char *start = pos;
2573 
2574 		ret = os_snprintf(pos, end - pos, "eap_session_id=");
2575 		if (os_snprintf_error(end - pos, ret))
2576 			return start - buf;
2577 		pos += ret;
2578 		ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
2579 		if (ret <= 0)
2580 			return start - buf;
2581 		pos += ret;
2582 		ret = os_snprintf(pos, end - pos, "\n");
2583 		if (os_snprintf_error(end - pos, ret))
2584 			return start - buf;
2585 		pos += ret;
2586 	}
2587 
2588 	res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
2589 	if (res >= 0)
2590 		pos += res;
2591 
2592 #ifdef CONFIG_WPS
2593 	{
2594 		char uuid_str[100];
2595 		uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
2596 		ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
2597 		if (os_snprintf_error(end - pos, ret))
2598 			return pos - buf;
2599 		pos += ret;
2600 	}
2601 #endif /* CONFIG_WPS */
2602 
2603 	if (wpa_s->ieee80211ac) {
2604 		ret = os_snprintf(pos, end - pos, "ieee80211ac=1\n");
2605 		if (os_snprintf_error(end - pos, ret))
2606 			return pos - buf;
2607 		pos += ret;
2608 	}
2609 
2610 #ifdef CONFIG_SME
2611 	if (wpa_s->sme.bss_max_idle_period) {
2612 		ret = os_snprintf(pos, end - pos, "bss_max_idle_period=%d\n",
2613 				  wpa_s->sme.bss_max_idle_period);
2614 		if (os_snprintf_error(end - pos, ret))
2615 			return pos - buf;
2616 		pos += ret;
2617 	}
2618 #endif /* CONFIG_SME */
2619 
2620 	if (wpa_s->ssid_verified) {
2621 		ret = os_snprintf(pos, end - pos, "ssid_verified=1\n");
2622 		if (os_snprintf_error(end - pos, ret))
2623 			return pos - buf;
2624 		pos += ret;
2625 	}
2626 
2627 	if (wpa_s->bigtk_set) {
2628 		ret = os_snprintf(pos, end - pos, "bigtk_set=1\n");
2629 		if (os_snprintf_error(end - pos, ret))
2630 			return pos - buf;
2631 		pos += ret;
2632 	}
2633 
2634 #ifdef ANDROID
2635 	/*
2636 	 * Allow using the STATUS command with default behavior, say for debug,
2637 	 * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
2638 	 * events with STATUS-NO_EVENTS.
2639 	 */
2640 	if (os_strcmp(params, "-NO_EVENTS")) {
2641 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
2642 			     "id=%d state=%d BSSID=" MACSTR " SSID=%s",
2643 			     wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
2644 			     wpa_s->wpa_state,
2645 			     MAC2STR(wpa_s->bssid),
2646 			     wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
2647 			     wpa_ssid_txt(wpa_s->current_ssid->ssid,
2648 					  wpa_s->current_ssid->ssid_len) : "");
2649 		if (wpa_s->wpa_state == WPA_COMPLETED) {
2650 			struct wpa_ssid *ssid = wpa_s->current_ssid;
2651 			char mld_addr[50];
2652 
2653 			mld_addr[0] = '\0';
2654 			if (wpa_s->valid_links)
2655 				os_snprintf(mld_addr, sizeof(mld_addr),
2656 					    " ap_mld_addr=" MACSTR,
2657 					    MAC2STR(wpa_s->ap_mld_addr));
2658 
2659 			wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
2660 				     "- connection to " MACSTR
2661 				     " completed %s [id=%d id_str=%s]%s",
2662 				     MAC2STR(wpa_s->bssid), "(auth)",
2663 				     ssid ? ssid->id : -1,
2664 				     ssid && ssid->id_str ? ssid->id_str : "",
2665 				     mld_addr);
2666 		}
2667 	}
2668 #endif /* ANDROID */
2669 
2670 	return pos - buf;
2671 }
2672 
2673 
wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant * wpa_s,char * cmd)2674 static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
2675 					   char *cmd)
2676 {
2677 	char *pos;
2678 	int id;
2679 	struct wpa_ssid *ssid;
2680 	u8 bssid[ETH_ALEN];
2681 
2682 	/* cmd: "<network id> <BSSID>" */
2683 	pos = os_strchr(cmd, ' ');
2684 	if (pos == NULL)
2685 		return -1;
2686 	*pos++ = '\0';
2687 	id = atoi(cmd);
2688 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
2689 	if (hwaddr_aton(pos, bssid)) {
2690 		wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
2691 		return -1;
2692 	}
2693 
2694 	ssid = wpa_config_get_network(wpa_s->conf, id);
2695 	if (ssid == NULL) {
2696 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2697 			   "to update", id);
2698 		return -1;
2699 	}
2700 
2701 	os_memcpy(ssid->bssid, bssid, ETH_ALEN);
2702 	ssid->bssid_set = !is_zero_ether_addr(bssid);
2703 
2704 	return 0;
2705 }
2706 
2707 
wpa_supplicant_ctrl_iface_bssid_ignore(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2708 static int wpa_supplicant_ctrl_iface_bssid_ignore(struct wpa_supplicant *wpa_s,
2709 						  char *cmd, char *buf,
2710 						  size_t buflen)
2711 {
2712 	u8 bssid[ETH_ALEN];
2713 	struct wpa_bssid_ignore *e;
2714 	char *pos, *end;
2715 	int ret;
2716 
2717 	/* cmd: "BSSID_IGNORE [<BSSID>]" */
2718 	if (*cmd == '\0') {
2719 		pos = buf;
2720 		end = buf + buflen;
2721 		e = wpa_s->bssid_ignore;
2722 		while (e) {
2723 			ret = os_snprintf(pos, end - pos, MACSTR "\n",
2724 					  MAC2STR(e->bssid));
2725 			if (os_snprintf_error(end - pos, ret))
2726 				return pos - buf;
2727 			pos += ret;
2728 			e = e->next;
2729 		}
2730 		return pos - buf;
2731 	}
2732 
2733 	cmd++;
2734 	if (os_strncmp(cmd, "clear", 5) == 0) {
2735 		wpa_bssid_ignore_clear(wpa_s);
2736 		os_memcpy(buf, "OK\n", 3);
2737 		return 3;
2738 	}
2739 
2740 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: BSSID_IGNORE bssid='%s'", cmd);
2741 	if (hwaddr_aton(cmd, bssid)) {
2742 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
2743 		return -1;
2744 	}
2745 
2746 	/*
2747 	 * Add the BSSID twice, so its count will be 2, causing it to be
2748 	 * skipped when processing scan results.
2749 	 */
2750 	ret = wpa_bssid_ignore_add(wpa_s, bssid);
2751 	if (ret < 0)
2752 		return -1;
2753 	ret = wpa_bssid_ignore_add(wpa_s, bssid);
2754 	if (ret < 0)
2755 		return -1;
2756 	os_memcpy(buf, "OK\n", 3);
2757 	return 3;
2758 }
2759 
2760 
wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2761 static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
2762 					       char *cmd, char *buf,
2763 					       size_t buflen)
2764 {
2765 	char *pos, *end, *stamp;
2766 	int ret;
2767 
2768 	/* cmd: "LOG_LEVEL [<level>]" */
2769 	if (*cmd == '\0') {
2770 		pos = buf;
2771 		end = buf + buflen;
2772 		ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2773 				  "Timestamp: %d\n",
2774 				  debug_level_str(wpa_debug_level),
2775 				  wpa_debug_timestamp);
2776 		if (os_snprintf_error(end - pos, ret))
2777 			ret = 0;
2778 
2779 		return ret;
2780 	}
2781 
2782 	while (*cmd == ' ')
2783 		cmd++;
2784 
2785 	stamp = os_strchr(cmd, ' ');
2786 	if (stamp) {
2787 		*stamp++ = '\0';
2788 		while (*stamp == ' ') {
2789 			stamp++;
2790 		}
2791 	}
2792 
2793 	if (os_strlen(cmd)) {
2794 		int level = str_to_debug_level(cmd);
2795 		if (level < 0)
2796 			return -1;
2797 		wpa_debug_level = level;
2798 	}
2799 
2800 	if (stamp && os_strlen(stamp))
2801 		wpa_debug_timestamp = atoi(stamp);
2802 
2803 	os_memcpy(buf, "OK\n", 3);
2804 	return 3;
2805 }
2806 
2807 
wpa_supplicant_ctrl_iface_list_networks(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2808 static int wpa_supplicant_ctrl_iface_list_networks(
2809 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2810 {
2811 	char *pos, *end, *prev;
2812 	struct wpa_ssid *ssid;
2813 	int ret;
2814 
2815 	pos = buf;
2816 	end = buf + buflen;
2817 	ret = os_snprintf(pos, end - pos,
2818 			  "network id / ssid / bssid / flags\n");
2819 	if (os_snprintf_error(end - pos, ret))
2820 		return pos - buf;
2821 	pos += ret;
2822 
2823 	ssid = wpa_s->conf->ssid;
2824 
2825 	/* skip over ssids until we find next one */
2826 	if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
2827 		int last_id = atoi(cmd + 8);
2828 		if (last_id != -1) {
2829 			while (ssid != NULL && ssid->id <= last_id) {
2830 				ssid = ssid->next;
2831 			}
2832 		}
2833 	}
2834 
2835 	while (ssid) {
2836 		prev = pos;
2837 		ret = os_snprintf(pos, end - pos, "%d\t%s",
2838 				  ssid->id,
2839 				  wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2840 		if (os_snprintf_error(end - pos, ret))
2841 			return prev - buf;
2842 		pos += ret;
2843 		if (ssid->bssid_set) {
2844 			ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2845 					  MAC2STR(ssid->bssid));
2846 		} else {
2847 			ret = os_snprintf(pos, end - pos, "\tany");
2848 		}
2849 		if (os_snprintf_error(end - pos, ret))
2850 			return prev - buf;
2851 		pos += ret;
2852 		ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
2853 				  ssid == wpa_s->current_ssid ?
2854 				  "[CURRENT]" : "",
2855 				  ssid->disabled ? "[DISABLED]" : "",
2856 				  ssid->disabled_until.sec ?
2857 				  "[TEMP-DISABLED]" : "",
2858 				  ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2859 				  "");
2860 		if (os_snprintf_error(end - pos, ret))
2861 			return prev - buf;
2862 		pos += ret;
2863 		ret = os_snprintf(pos, end - pos, "\n");
2864 		if (os_snprintf_error(end - pos, ret))
2865 			return prev - buf;
2866 		pos += ret;
2867 
2868 		ssid = ssid->next;
2869 	}
2870 
2871 	return pos - buf;
2872 }
2873 
2874 
wpa_supplicant_cipher_txt(char * pos,char * end,int cipher)2875 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2876 {
2877 	int ret;
2878 	ret = os_snprintf(pos, end - pos, "-");
2879 	if (os_snprintf_error(end - pos, ret))
2880 		return pos;
2881 	pos += ret;
2882 	ret = wpa_write_ciphers(pos, end, cipher, "+");
2883 	if (ret < 0)
2884 		return pos;
2885 	pos += ret;
2886 	return pos;
2887 }
2888 
2889 
wpa_supplicant_ie_txt(char * pos,char * end,const char * proto,const u8 * ie,size_t ie_len)2890 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2891 				    const u8 *ie, size_t ie_len)
2892 {
2893 	struct wpa_ie_data data;
2894 	char *start;
2895 	int ret;
2896 
2897 	ret = os_snprintf(pos, end - pos, "[%s-", proto);
2898 	if (os_snprintf_error(end - pos, ret))
2899 		return pos;
2900 	pos += ret;
2901 
2902 	if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2903 		ret = os_snprintf(pos, end - pos, "?]");
2904 		if (os_snprintf_error(end - pos, ret))
2905 			return pos;
2906 		pos += ret;
2907 		return pos;
2908 	}
2909 
2910 	start = pos;
2911 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
2912 		ret = os_snprintf(pos, end - pos, "%sEAP",
2913 				  pos == start ? "" : "+");
2914 		if (os_snprintf_error(end - pos, ret))
2915 			return pos;
2916 		pos += ret;
2917 	}
2918 	if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
2919 		ret = os_snprintf(pos, end - pos, "%sPSK",
2920 				  pos == start ? "" : "+");
2921 		if (os_snprintf_error(end - pos, ret))
2922 			return pos;
2923 		pos += ret;
2924 	}
2925 	if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
2926 		ret = os_snprintf(pos, end - pos, "%sNone",
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_SAE) {
2933 		ret = os_snprintf(pos, end - pos, "%sSAE",
2934 				  pos == start ? "" : "+");
2935 		if (os_snprintf_error(end - pos, ret))
2936 			return pos;
2937 		pos += ret;
2938 	}
2939 	if (data.key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
2940 		ret = os_snprintf(pos, end - pos, "%sSAE-EXT-KEY",
2941 				  pos == start ? "" : "+");
2942 		if (os_snprintf_error(end - pos, ret))
2943 			return pos;
2944 		pos += ret;
2945 	}
2946 #ifdef CONFIG_IEEE80211R
2947 	if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2948 		ret = os_snprintf(pos, end - pos, "%sFT/EAP",
2949 				  pos == start ? "" : "+");
2950 		if (os_snprintf_error(end - pos, ret))
2951 			return pos;
2952 		pos += ret;
2953 	}
2954 	if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2955 		ret = os_snprintf(pos, end - pos, "%sFT/PSK",
2956 				  pos == start ? "" : "+");
2957 		if (os_snprintf_error(end - pos, ret))
2958 			return pos;
2959 		pos += ret;
2960 	}
2961 	if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
2962 		ret = os_snprintf(pos, end - pos, "%sFT/SAE",
2963 				  pos == start ? "" : "+");
2964 		if (os_snprintf_error(end - pos, ret))
2965 			return pos;
2966 		pos += ret;
2967 	}
2968 	if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
2969 		ret = os_snprintf(pos, end - pos, "%sFT/SAE-EXT-KEY",
2970 				  pos == start ? "" : "+");
2971 		if (os_snprintf_error(end - pos, ret))
2972 			return pos;
2973 		pos += ret;
2974 	}
2975 #endif /* CONFIG_IEEE80211R */
2976 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2977 		ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
2978 				  pos == start ? "" : "+");
2979 		if (os_snprintf_error(end - pos, ret))
2980 			return pos;
2981 		pos += ret;
2982 	}
2983 	if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2984 		ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
2985 				  pos == start ? "" : "+");
2986 		if (os_snprintf_error(end - pos, ret))
2987 			return pos;
2988 		pos += ret;
2989 	}
2990 
2991 #ifdef CONFIG_SUITEB
2992 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
2993 		ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
2994 				  pos == start ? "" : "+");
2995 		if (os_snprintf_error(end - pos, ret))
2996 			return pos;
2997 		pos += ret;
2998 	}
2999 #endif /* CONFIG_SUITEB */
3000 
3001 #ifdef CONFIG_SUITEB192
3002 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
3003 		ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
3004 				  pos == start ? "" : "+");
3005 		if (os_snprintf_error(end - pos, ret))
3006 			return pos;
3007 		pos += ret;
3008 	}
3009 #endif /* CONFIG_SUITEB192 */
3010 
3011 #ifdef CONFIG_FILS
3012 	if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
3013 		ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
3014 				  pos == start ? "" : "+");
3015 		if (os_snprintf_error(end - pos, ret))
3016 			return pos;
3017 		pos += ret;
3018 	}
3019 	if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
3020 		ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
3021 				  pos == start ? "" : "+");
3022 		if (os_snprintf_error(end - pos, ret))
3023 			return pos;
3024 		pos += ret;
3025 	}
3026 #ifdef CONFIG_IEEE80211R
3027 	if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
3028 		ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
3029 				  pos == start ? "" : "+");
3030 		if (os_snprintf_error(end - pos, ret))
3031 			return pos;
3032 		pos += ret;
3033 	}
3034 	if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
3035 		ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
3036 				  pos == start ? "" : "+");
3037 		if (os_snprintf_error(end - pos, ret))
3038 			return pos;
3039 		pos += ret;
3040 	}
3041 #endif /* CONFIG_IEEE80211R */
3042 #endif /* CONFIG_FILS */
3043 
3044 #ifdef CONFIG_OWE
3045 	if (data.key_mgmt & WPA_KEY_MGMT_OWE) {
3046 		ret = os_snprintf(pos, end - pos, "%sOWE",
3047 				  pos == start ? "" : "+");
3048 		if (os_snprintf_error(end - pos, ret))
3049 			return pos;
3050 		pos += ret;
3051 	}
3052 #endif /* CONFIG_OWE */
3053 
3054 #ifdef CONFIG_DPP
3055 	if (data.key_mgmt & WPA_KEY_MGMT_DPP) {
3056 		ret = os_snprintf(pos, end - pos, "%sDPP",
3057 				  pos == start ? "" : "+");
3058 		if (os_snprintf_error(end - pos, ret))
3059 			return pos;
3060 		pos += ret;
3061 	}
3062 #endif /* CONFIG_DPP */
3063 
3064 #ifdef CONFIG_SHA384
3065 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA384) {
3066 		ret = os_snprintf(pos, end - pos, "%sEAP-SHA384",
3067 				  pos == start ? "" : "+");
3068 		if (os_snprintf_error(end - pos, ret))
3069 			return pos;
3070 		pos += ret;
3071 	}
3072 #endif /* CONFIG_SHA384 */
3073 
3074 	pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
3075 
3076 	if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
3077 		ret = os_snprintf(pos, end - pos, "-preauth");
3078 		if (os_snprintf_error(end - pos, ret))
3079 			return pos;
3080 		pos += ret;
3081 	}
3082 
3083 	ret = os_snprintf(pos, end - pos, "]");
3084 	if (os_snprintf_error(end - pos, ret))
3085 		return pos;
3086 	pos += ret;
3087 
3088 	return pos;
3089 }
3090 
3091 
3092 #ifdef CONFIG_WPS
wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant * wpa_s,char * pos,char * end,struct wpabuf * wps_ie)3093 static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
3094 					    char *pos, char *end,
3095 					    struct wpabuf *wps_ie)
3096 {
3097 	int ret;
3098 	const char *txt;
3099 
3100 	if (wps_ie == NULL)
3101 		return pos;
3102 	if (wps_is_selected_pbc_registrar(wps_ie))
3103 		txt = "[WPS-PBC]";
3104 	else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
3105 		txt = "[WPS-AUTH]";
3106 	else if (wps_is_selected_pin_registrar(wps_ie))
3107 		txt = "[WPS-PIN]";
3108 	else
3109 		txt = "[WPS]";
3110 
3111 	ret = os_snprintf(pos, end - pos, "%s", txt);
3112 	if (!os_snprintf_error(end - pos, ret))
3113 		pos += ret;
3114 	wpabuf_free(wps_ie);
3115 	return pos;
3116 }
3117 #endif /* CONFIG_WPS */
3118 
3119 
wpa_supplicant_wps_ie_txt(struct wpa_supplicant * wpa_s,char * pos,char * end,const struct wpa_bss * bss)3120 static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
3121 					char *pos, char *end,
3122 					const struct wpa_bss *bss)
3123 {
3124 #ifdef CONFIG_WPS
3125 	struct wpabuf *wps_ie;
3126 	wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
3127 	return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
3128 #else /* CONFIG_WPS */
3129 	return pos;
3130 #endif /* CONFIG_WPS */
3131 }
3132 
3133 
3134 /* 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)3135 static int wpa_supplicant_ctrl_iface_scan_result(
3136 	struct wpa_supplicant *wpa_s,
3137 	const struct wpa_bss *bss, char *buf, size_t buflen)
3138 {
3139 	char *pos, *end;
3140 	int ret;
3141 	const u8 *ie, *ie2, *p2p, *mesh, *owe, *rsnxe;
3142 
3143 	mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
3144 	p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
3145 	if (!p2p)
3146 		p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
3147 	if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
3148 	    os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
3149 	    0)
3150 		return 0; /* Do not show P2P listen discovery results here */
3151 
3152 	pos = buf;
3153 	end = buf + buflen;
3154 
3155 	ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
3156 			  MAC2STR(bss->bssid), bss->freq, bss->level);
3157 	if (os_snprintf_error(end - pos, ret))
3158 		return -1;
3159 	pos += ret;
3160 	ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3161 	if (ie)
3162 		pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
3163 	ie2 = wpa_bss_get_rsne(wpa_s, bss, NULL, false);
3164 	if (ie2) {
3165 		pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
3166 					    ie2, 2 + ie2[1]);
3167 	}
3168 	rsnxe = wpa_bss_get_rsnxe(wpa_s, bss, NULL, false);
3169 	if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_H2E)) {
3170 		ret = os_snprintf(pos, end - pos, "[SAE-H2E]");
3171 		if (os_snprintf_error(end - pos, ret))
3172 			return -1;
3173 		pos += ret;
3174 	}
3175 	if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_PK)) {
3176 		ret = os_snprintf(pos, end - pos, "[SAE-PK]");
3177 		if (os_snprintf_error(end - pos, ret))
3178 			return -1;
3179 		pos += ret;
3180 	}
3181 	owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
3182 	if (owe) {
3183 		ret = os_snprintf(pos, end - pos,
3184 				  ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
3185 		if (os_snprintf_error(end - pos, ret))
3186 			return -1;
3187 		pos += ret;
3188 	}
3189 	pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3190 	if (!ie && !ie2 && (bss->caps & IEEE80211_CAP_PRIVACY)) {
3191 		ret = os_snprintf(pos, end - pos, "[WEP]");
3192 		if (os_snprintf_error(end - pos, ret))
3193 			return -1;
3194 		pos += ret;
3195 	}
3196 	if (mesh) {
3197 		ret = os_snprintf(pos, end - pos, "[MESH]");
3198 		if (os_snprintf_error(end - pos, ret))
3199 			return -1;
3200 		pos += ret;
3201 	}
3202 	if (bss_is_dmg(bss)) {
3203 		const char *s;
3204 
3205 		if (wpa_bss_get_ie_ext(bss, WLAN_EID_EXT_EDMG_OPERATION)) {
3206 			ret = os_snprintf(pos, end - pos, "[EDMG]");
3207 			if (os_snprintf_error(end - pos, ret))
3208 				return -1;
3209 			pos += ret;
3210 		}
3211 
3212 		ret = os_snprintf(pos, end - pos, "[DMG]");
3213 		if (os_snprintf_error(end - pos, ret))
3214 			return -1;
3215 		pos += ret;
3216 		switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
3217 		case IEEE80211_CAP_DMG_IBSS:
3218 			s = "[IBSS]";
3219 			break;
3220 		case IEEE80211_CAP_DMG_AP:
3221 			s = "[ESS]";
3222 			break;
3223 		case IEEE80211_CAP_DMG_PBSS:
3224 			s = "[PBSS]";
3225 			break;
3226 		default:
3227 			s = "";
3228 			break;
3229 		}
3230 		ret = os_snprintf(pos, end - pos, "%s", s);
3231 		if (os_snprintf_error(end - pos, ret))
3232 			return -1;
3233 		pos += ret;
3234 	} else {
3235 		if (bss->caps & IEEE80211_CAP_IBSS) {
3236 			ret = os_snprintf(pos, end - pos, "[IBSS]");
3237 			if (os_snprintf_error(end - pos, ret))
3238 				return -1;
3239 			pos += ret;
3240 		}
3241 		if (bss->caps & IEEE80211_CAP_ESS) {
3242 			ret = os_snprintf(pos, end - pos, "[ESS]");
3243 			if (os_snprintf_error(end - pos, ret))
3244 				return -1;
3245 			pos += ret;
3246 		}
3247 	}
3248 	if (p2p) {
3249 		ret = os_snprintf(pos, end - pos, "[P2P]");
3250 		if (os_snprintf_error(end - pos, ret))
3251 			return -1;
3252 		pos += ret;
3253 	}
3254 #ifdef CONFIG_HS20
3255 	if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
3256 		ret = os_snprintf(pos, end - pos, "[HS20]");
3257 		if (os_snprintf_error(end - pos, ret))
3258 			return -1;
3259 		pos += ret;
3260 	}
3261 #endif /* CONFIG_HS20 */
3262 #ifdef CONFIG_FILS
3263 	if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
3264 		ret = os_snprintf(pos, end - pos, "[FILS]");
3265 		if (os_snprintf_error(end - pos, ret))
3266 			return -1;
3267 		pos += ret;
3268 	}
3269 #endif /* CONFIG_FILS */
3270 #ifdef CONFIG_FST
3271 	if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
3272 		ret = os_snprintf(pos, end - pos, "[FST]");
3273 		if (os_snprintf_error(end - pos, ret))
3274 			return -1;
3275 		pos += ret;
3276 	}
3277 #endif /* CONFIG_FST */
3278 	if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_UTF_8_SSID)) {
3279 		ret = os_snprintf(pos, end - pos, "[UTF-8]");
3280 		if (os_snprintf_error(end - pos, ret))
3281 			return -1;
3282 		pos += ret;
3283 	}
3284 
3285 	ret = os_snprintf(pos, end - pos, "\t%s",
3286 			  wpa_ssid_txt(bss->ssid, bss->ssid_len));
3287 	if (os_snprintf_error(end - pos, ret))
3288 		return -1;
3289 	pos += ret;
3290 
3291 	ret = os_snprintf(pos, end - pos, "\n");
3292 	if (os_snprintf_error(end - pos, ret))
3293 		return -1;
3294 	pos += ret;
3295 
3296 	return pos - buf;
3297 }
3298 
3299 
wpa_supplicant_ctrl_iface_scan_results(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3300 static int wpa_supplicant_ctrl_iface_scan_results(
3301 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
3302 {
3303 	char *pos, *end;
3304 	struct wpa_bss *bss;
3305 	int ret;
3306 
3307 	pos = buf;
3308 	end = buf + buflen;
3309 	ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
3310 			  "flags / ssid\n");
3311 	if (os_snprintf_error(end - pos, ret))
3312 		return pos - buf;
3313 	pos += ret;
3314 
3315 	dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
3316 		ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
3317 							    end - pos);
3318 		if (ret < 0 || ret >= end - pos)
3319 			return pos - buf;
3320 		pos += ret;
3321 	}
3322 
3323 	return pos - buf;
3324 }
3325 
3326 
3327 #ifdef CONFIG_MESH
3328 
wpa_supplicant_ctrl_iface_mesh_interface_add(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)3329 static int wpa_supplicant_ctrl_iface_mesh_interface_add(
3330 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
3331 {
3332 	char *pos, ifname[IFNAMSIZ + 1];
3333 
3334 	ifname[0] = '\0';
3335 
3336 	pos = os_strstr(cmd, "ifname=");
3337 	if (pos) {
3338 		pos += 7;
3339 		os_strlcpy(ifname, pos, sizeof(ifname));
3340 	}
3341 
3342 	if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
3343 		return -1;
3344 
3345 	os_strlcpy(reply, ifname, max_len);
3346 	return os_strlen(ifname);
3347 }
3348 
3349 
wpa_supplicant_ctrl_iface_mesh_group_add(struct wpa_supplicant * wpa_s,char * cmd)3350 static int wpa_supplicant_ctrl_iface_mesh_group_add(
3351 	struct wpa_supplicant *wpa_s, char *cmd)
3352 {
3353 	int id;
3354 	struct wpa_ssid *ssid;
3355 
3356 	id = atoi(cmd);
3357 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
3358 
3359 	ssid = wpa_config_get_network(wpa_s->conf, id);
3360 	if (ssid == NULL) {
3361 		wpa_printf(MSG_DEBUG,
3362 			   "CTRL_IFACE: Could not find network id=%d", id);
3363 		return -1;
3364 	}
3365 	if (ssid->mode != WPAS_MODE_MESH) {
3366 		wpa_printf(MSG_DEBUG,
3367 			   "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
3368 		return -1;
3369 	}
3370 	if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
3371 	    ssid->key_mgmt != WPA_KEY_MGMT_SAE &&
3372 	    ssid->key_mgmt != WPA_KEY_MGMT_SAE_EXT_KEY) {
3373 		wpa_printf(MSG_ERROR,
3374 			   "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
3375 		return -1;
3376 	}
3377 
3378 	/*
3379 	 * TODO: If necessary write our own group_add function,
3380 	 * for now we can reuse select_network
3381 	 */
3382 	wpa_supplicant_select_network(wpa_s, ssid);
3383 
3384 	return 0;
3385 }
3386 
3387 
wpa_supplicant_ctrl_iface_mesh_group_remove(struct wpa_supplicant * wpa_s,char * cmd)3388 static int wpa_supplicant_ctrl_iface_mesh_group_remove(
3389 	struct wpa_supplicant *wpa_s, char *cmd)
3390 {
3391 	struct wpa_supplicant *orig;
3392 	struct wpa_global *global;
3393 	int found = 0;
3394 
3395 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
3396 
3397 	global = wpa_s->global;
3398 	orig = wpa_s;
3399 
3400 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3401 		if (os_strcmp(wpa_s->ifname, cmd) == 0) {
3402 			found = 1;
3403 			break;
3404 		}
3405 	}
3406 	if (!found) {
3407 		wpa_printf(MSG_ERROR,
3408 			   "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
3409 			   cmd);
3410 		return -1;
3411 	}
3412 	if (wpa_s->mesh_if_created && wpa_s == orig) {
3413 		wpa_printf(MSG_ERROR,
3414 			   "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
3415 		return -1;
3416 	}
3417 
3418 	wpa_s->reassociate = 0;
3419 	wpa_s->disconnected = 1;
3420 	wpa_supplicant_cancel_sched_scan(wpa_s);
3421 	wpa_supplicant_cancel_scan(wpa_s);
3422 
3423 	/*
3424 	 * TODO: If necessary write our own group_remove function,
3425 	 * for now we can reuse deauthenticate
3426 	 */
3427 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3428 
3429 	if (wpa_s->mesh_if_created)
3430 		wpa_supplicant_remove_iface(global, wpa_s, 0);
3431 
3432 	return 0;
3433 }
3434 
3435 
wpa_supplicant_ctrl_iface_mesh_peer_remove(struct wpa_supplicant * wpa_s,char * cmd)3436 static int wpa_supplicant_ctrl_iface_mesh_peer_remove(
3437 	struct wpa_supplicant *wpa_s, char *cmd)
3438 {
3439 	u8 addr[ETH_ALEN];
3440 
3441 	if (hwaddr_aton(cmd, addr) < 0)
3442 		return -1;
3443 
3444 	return wpas_mesh_peer_remove(wpa_s, addr);
3445 }
3446 
3447 
wpa_supplicant_ctrl_iface_mesh_peer_add(struct wpa_supplicant * wpa_s,char * cmd)3448 static int wpa_supplicant_ctrl_iface_mesh_peer_add(
3449 	struct wpa_supplicant *wpa_s, char *cmd)
3450 {
3451 	u8 addr[ETH_ALEN];
3452 	int duration;
3453 	char *pos;
3454 
3455 	pos = os_strstr(cmd, " duration=");
3456 	if (pos) {
3457 		*pos = '\0';
3458 		duration = atoi(pos + 10);
3459 	} else {
3460 		duration = -1;
3461 	}
3462 
3463 	if (hwaddr_aton(cmd, addr))
3464 		return -1;
3465 
3466 	return wpas_mesh_peer_add(wpa_s, addr, duration);
3467 }
3468 
3469 
wpa_supplicant_ctrl_iface_mesh_link_probe(struct wpa_supplicant * wpa_s,char * cmd)3470 static int wpa_supplicant_ctrl_iface_mesh_link_probe(
3471 	struct wpa_supplicant *wpa_s, char *cmd)
3472 {
3473 	struct ether_header *eth;
3474 	u8 addr[ETH_ALEN];
3475 	u8 *buf;
3476 	char *pos;
3477 	size_t payload_len = 0, len;
3478 	int ret = -1;
3479 
3480 	if (hwaddr_aton(cmd, addr))
3481 		return -1;
3482 
3483 	pos = os_strstr(cmd, " payload=");
3484 	if (pos) {
3485 		pos = pos + 9;
3486 		payload_len = os_strlen(pos);
3487 		if (payload_len & 1)
3488 			return -1;
3489 
3490 		payload_len /= 2;
3491 	}
3492 
3493 	len = ETH_HLEN + payload_len;
3494 	buf = os_malloc(len);
3495 	if (!buf)
3496 		return -1;
3497 
3498 	eth = (struct ether_header *) buf;
3499 	os_memcpy(eth->ether_dhost, addr, ETH_ALEN);
3500 	os_memcpy(eth->ether_shost, wpa_s->own_addr, ETH_ALEN);
3501 	eth->ether_type = htons(ETH_P_802_3);
3502 
3503 	if (payload_len && hexstr2bin(pos, buf + ETH_HLEN, payload_len) < 0)
3504 		goto fail;
3505 
3506 	ret = wpa_drv_mesh_link_probe(wpa_s, addr, buf, len);
3507 fail:
3508 	os_free(buf);
3509 	return -ret;
3510 }
3511 
3512 #endif /* CONFIG_MESH */
3513 
3514 
wpa_supplicant_ctrl_iface_select_network(struct wpa_supplicant * wpa_s,char * cmd)3515 static int wpa_supplicant_ctrl_iface_select_network(
3516 	struct wpa_supplicant *wpa_s, char *cmd)
3517 {
3518 	int id;
3519 	struct wpa_ssid *ssid;
3520 	char *pos;
3521 
3522 	/* cmd: "<network id>" or "any" */
3523 	if (os_strncmp(cmd, "any", 3) == 0) {
3524 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
3525 		ssid = NULL;
3526 	} else {
3527 		id = atoi(cmd);
3528 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
3529 
3530 		ssid = wpa_config_get_network(wpa_s->conf, id);
3531 		if (ssid == NULL) {
3532 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3533 				   "network id=%d", id);
3534 			return -1;
3535 		}
3536 		if (ssid->disabled == 2) {
3537 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3538 				   "SELECT_NETWORK with persistent P2P group");
3539 			return -1;
3540 		}
3541 	}
3542 
3543 	pos = os_strstr(cmd, " freq=");
3544 	if (pos) {
3545 		int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
3546 		if (freqs) {
3547 			os_free(wpa_s->select_network_scan_freqs);
3548 			wpa_s->select_network_scan_freqs = freqs;
3549 		}
3550 	}
3551 
3552 	wpa_s->scan_min_time.sec = 0;
3553 	wpa_s->scan_min_time.usec = 0;
3554 	wpa_supplicant_select_network(wpa_s, ssid);
3555 
3556 	return 0;
3557 }
3558 
3559 
wpa_supplicant_ctrl_iface_enable_network(struct wpa_supplicant * wpa_s,char * cmd)3560 static int wpa_supplicant_ctrl_iface_enable_network(
3561 	struct wpa_supplicant *wpa_s, char *cmd)
3562 {
3563 	int id;
3564 	struct wpa_ssid *ssid;
3565 
3566 	/* cmd: "<network id>" or "all" */
3567 	if (os_strcmp(cmd, "all") == 0) {
3568 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
3569 		ssid = NULL;
3570 	} else {
3571 		id = atoi(cmd);
3572 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
3573 
3574 		ssid = wpa_config_get_network(wpa_s->conf, id);
3575 		if (ssid == NULL) {
3576 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3577 				   "network id=%d", id);
3578 			return -1;
3579 		}
3580 		if (ssid->disabled == 2) {
3581 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3582 				   "ENABLE_NETWORK with persistent P2P group");
3583 			return -1;
3584 		}
3585 
3586 		if (os_strstr(cmd, " no-connect")) {
3587 			ssid->disabled = 0;
3588 			return 0;
3589 		}
3590 	}
3591 	wpa_s->scan_min_time.sec = 0;
3592 	wpa_s->scan_min_time.usec = 0;
3593 	wpa_supplicant_enable_network(wpa_s, ssid);
3594 
3595 	return 0;
3596 }
3597 
3598 
wpa_supplicant_ctrl_iface_disable_network(struct wpa_supplicant * wpa_s,char * cmd)3599 static int wpa_supplicant_ctrl_iface_disable_network(
3600 	struct wpa_supplicant *wpa_s, char *cmd)
3601 {
3602 	int id;
3603 	struct wpa_ssid *ssid;
3604 
3605 	/* cmd: "<network id>" or "all" */
3606 	if (os_strcmp(cmd, "all") == 0) {
3607 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
3608 		ssid = NULL;
3609 	} else {
3610 		id = atoi(cmd);
3611 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
3612 
3613 		ssid = wpa_config_get_network(wpa_s->conf, id);
3614 		if (ssid == NULL) {
3615 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3616 				   "network id=%d", id);
3617 			return -1;
3618 		}
3619 		if (ssid->disabled == 2) {
3620 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3621 				   "DISABLE_NETWORK with persistent P2P "
3622 				   "group");
3623 			return -1;
3624 		}
3625 	}
3626 	wpa_supplicant_disable_network(wpa_s, ssid);
3627 
3628 	return 0;
3629 }
3630 
3631 
wpa_supplicant_ctrl_iface_add_network(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3632 static int wpa_supplicant_ctrl_iface_add_network(
3633 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
3634 {
3635 	struct wpa_ssid *ssid;
3636 	int ret;
3637 
3638 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
3639 
3640 	ssid = wpa_supplicant_add_network(wpa_s);
3641 	if (ssid == NULL)
3642 		return -1;
3643 
3644 	ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
3645 	if (os_snprintf_error(buflen, ret))
3646 		return -1;
3647 	return ret;
3648 }
3649 
3650 
wpa_supplicant_ctrl_iface_remove_network(struct wpa_supplicant * wpa_s,char * cmd)3651 static int wpa_supplicant_ctrl_iface_remove_network(
3652 	struct wpa_supplicant *wpa_s, char *cmd)
3653 {
3654 	int id;
3655 	int result;
3656 
3657 	/* cmd: "<network id>" or "all" */
3658 	if (os_strcmp(cmd, "all") == 0) {
3659 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
3660 		return wpa_supplicant_remove_all_networks(wpa_s);
3661 	}
3662 
3663 	id = atoi(cmd);
3664 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
3665 
3666 	result = wpa_supplicant_remove_network(wpa_s, id);
3667 	if (result == -1) {
3668 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3669 			   "id=%d", id);
3670 		return -1;
3671 	}
3672 	if (result == -2) {
3673 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
3674 			   "network id=%d", id);
3675 		return -1;
3676 	}
3677 	return 0;
3678 }
3679 
3680 
wpa_supplicant_ctrl_iface_update_network(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,char * name,char * value)3681 static int wpa_supplicant_ctrl_iface_update_network(
3682 	struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3683 	char *name, char *value)
3684 {
3685 	int ret;
3686 
3687 	ret = wpa_config_set(ssid, name, value, 0);
3688 	if (ret < 0) {
3689 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
3690 			   "variable '%s'", name);
3691 		return -1;
3692 	}
3693 	if (ret == 1)
3694 		return 0; /* No change to the previously configured value */
3695 
3696 #ifdef CONFIG_BGSCAN
3697 	if (os_strcmp(name, "bgscan") == 0) {
3698 		/*
3699 		 * Reset the bgscan parameters for the current network and
3700 		 * return. There's no need to flush caches for bgscan parameter
3701 		 * changes.
3702 		 */
3703 		if (wpa_s->current_ssid == ssid &&
3704 		    wpa_s->wpa_state == WPA_COMPLETED)
3705 			wpa_supplicant_reset_bgscan(wpa_s);
3706 		return 0;
3707 	}
3708 #endif /* CONFIG_BGSCAN */
3709 
3710 	if (os_strcmp(name, "bssid") != 0 &&
3711 	    os_strncmp(name, "bssid_", 6) != 0 &&
3712 	    os_strcmp(name, "scan_freq") != 0 &&
3713 	    os_strcmp(name, "priority") != 0) {
3714 		wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
3715 
3716 		if (wpa_s->current_ssid == ssid ||
3717 		    wpa_s->current_ssid == NULL) {
3718 			/*
3719 			 * Invalidate the EAP session cache if anything in the
3720 			 * current or previously used configuration changes.
3721 			 */
3722 			eapol_sm_invalidate_cached_session(wpa_s->eapol);
3723 		}
3724 	}
3725 
3726 	if ((os_strcmp(name, "psk") == 0 &&
3727 	     value[0] == '"' && ssid->ssid_len) ||
3728 	    (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
3729 		wpa_config_update_psk(ssid);
3730 	else if (os_strcmp(name, "priority") == 0)
3731 		wpa_config_update_prio_list(wpa_s->conf);
3732 
3733 	return 0;
3734 }
3735 
3736 
wpa_supplicant_ctrl_iface_set_network(struct wpa_supplicant * wpa_s,char * cmd)3737 static int wpa_supplicant_ctrl_iface_set_network(
3738 	struct wpa_supplicant *wpa_s, char *cmd)
3739 {
3740 	int id, ret, prev_bssid_set, prev_disabled;
3741 	struct wpa_ssid *ssid;
3742 	char *name, *value;
3743 	u8 prev_bssid[ETH_ALEN];
3744 
3745 	/* cmd: "<network id> <variable name> <value>" */
3746 	name = os_strchr(cmd, ' ');
3747 	if (name == NULL)
3748 		return -1;
3749 	*name++ = '\0';
3750 
3751 	value = os_strchr(name, ' ');
3752 	if (value == NULL)
3753 		return -1;
3754 	*value++ = '\0';
3755 
3756 	id = atoi(cmd);
3757 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
3758 		   id, name);
3759 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3760 			      (u8 *) value, os_strlen(value));
3761 
3762 	ssid = wpa_config_get_network(wpa_s->conf, id);
3763 	if (ssid == NULL) {
3764 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3765 			   "id=%d", id);
3766 		return -1;
3767 	}
3768 
3769 	prev_bssid_set = ssid->bssid_set;
3770 	prev_disabled = ssid->disabled;
3771 	os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
3772 	ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
3773 						       value);
3774 	if (ret == 0 &&
3775 	    (ssid->bssid_set != prev_bssid_set ||
3776 	     !ether_addr_equal(ssid->bssid, prev_bssid)))
3777 		wpas_notify_network_bssid_set_changed(wpa_s, ssid);
3778 
3779 	if (prev_disabled != ssid->disabled &&
3780 	    (prev_disabled == 2 || ssid->disabled == 2))
3781 		wpas_notify_network_type_changed(wpa_s, ssid);
3782 
3783 	return ret;
3784 }
3785 
3786 
wpa_supplicant_ctrl_iface_get_network(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)3787 static int wpa_supplicant_ctrl_iface_get_network(
3788 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
3789 {
3790 	int id;
3791 	size_t res;
3792 	struct wpa_ssid *ssid;
3793 	char *name, *value;
3794 
3795 	/* cmd: "<network id> <variable name>" */
3796 	name = os_strchr(cmd, ' ');
3797 	if (name == NULL || buflen == 0)
3798 		return -1;
3799 	*name++ = '\0';
3800 
3801 	id = atoi(cmd);
3802 	wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
3803 		   id, name);
3804 
3805 	ssid = wpa_config_get_network(wpa_s->conf, id);
3806 	if (ssid == NULL) {
3807 		wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Could not find network "
3808 			   "id=%d", id);
3809 		return -1;
3810 	}
3811 
3812 	value = wpa_config_get_no_key(ssid, name);
3813 	if (value == NULL) {
3814 		wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Failed to get network "
3815 			   "variable '%s'", name);
3816 		return -1;
3817 	}
3818 
3819 	res = os_strlcpy(buf, value, buflen);
3820 	if (res >= buflen) {
3821 		os_free(value);
3822 		return -1;
3823 	}
3824 
3825 	os_free(value);
3826 
3827 	return res;
3828 }
3829 
3830 
wpa_supplicant_ctrl_iface_dup_network(struct wpa_supplicant * wpa_s,char * cmd,struct wpa_supplicant * dst_wpa_s)3831 static int wpa_supplicant_ctrl_iface_dup_network(
3832 	struct wpa_supplicant *wpa_s, char *cmd,
3833 	struct wpa_supplicant *dst_wpa_s)
3834 {
3835 	struct wpa_ssid *ssid_s, *ssid_d;
3836 	char *name, *id, *value;
3837 	int id_s, id_d, ret;
3838 
3839 	/* cmd: "<src network id> <dst network id> <variable name>" */
3840 	id = os_strchr(cmd, ' ');
3841 	if (id == NULL)
3842 		return -1;
3843 	*id++ = '\0';
3844 
3845 	name = os_strchr(id, ' ');
3846 	if (name == NULL)
3847 		return -1;
3848 	*name++ = '\0';
3849 
3850 	id_s = atoi(cmd);
3851 	id_d = atoi(id);
3852 
3853 	wpa_printf(MSG_DEBUG,
3854 		   "CTRL_IFACE: DUP_NETWORK ifname=%s->%s id=%d->%d name='%s'",
3855 		   wpa_s->ifname, dst_wpa_s->ifname, id_s, id_d, name);
3856 
3857 	ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
3858 	if (ssid_s == NULL) {
3859 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3860 			   "network id=%d", id_s);
3861 		return -1;
3862 	}
3863 
3864 	ssid_d = wpa_config_get_network(dst_wpa_s->conf, id_d);
3865 	if (ssid_d == NULL) {
3866 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3867 			   "network id=%d", id_d);
3868 		return -1;
3869 	}
3870 
3871 	value = wpa_config_get(ssid_s, name);
3872 	if (value == NULL) {
3873 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
3874 			   "variable '%s'", name);
3875 		return -1;
3876 	}
3877 
3878 	ret = wpa_supplicant_ctrl_iface_update_network(dst_wpa_s, ssid_d, name,
3879 						       value);
3880 
3881 	os_free(value);
3882 
3883 	return ret;
3884 }
3885 
3886 
wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3887 static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
3888 						char *buf, size_t buflen)
3889 {
3890 	char *pos, *end;
3891 	struct wpa_cred *cred;
3892 	int ret;
3893 
3894 	pos = buf;
3895 	end = buf + buflen;
3896 	ret = os_snprintf(pos, end - pos,
3897 			  "cred id / realm / username / domain / imsi\n");
3898 	if (os_snprintf_error(end - pos, ret))
3899 		return pos - buf;
3900 	pos += ret;
3901 
3902 	cred = wpa_s->conf->cred;
3903 	while (cred) {
3904 		ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
3905 				  cred->id, cred->realm ? cred->realm : "",
3906 				  cred->username ? cred->username : "",
3907 				  cred->domain ? cred->domain[0] : "",
3908 				  cred->imsi ? cred->imsi : "");
3909 		if (os_snprintf_error(end - pos, ret))
3910 			return pos - buf;
3911 		pos += ret;
3912 
3913 		cred = cred->next;
3914 	}
3915 
3916 	return pos - buf;
3917 }
3918 
3919 
wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3920 static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
3921 					      char *buf, size_t buflen)
3922 {
3923 	struct wpa_cred *cred;
3924 	int ret;
3925 
3926 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
3927 
3928 	cred = wpa_config_add_cred(wpa_s->conf);
3929 	if (cred == NULL)
3930 		return -1;
3931 
3932 	wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
3933 
3934 	ret = os_snprintf(buf, buflen, "%d\n", cred->id);
3935 	if (os_snprintf_error(buflen, ret))
3936 		return -1;
3937 	return ret;
3938 }
3939 
3940 
wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant * wpa_s,char * cmd)3941 static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
3942 						 char *cmd)
3943 {
3944 	int id;
3945 	struct wpa_cred *cred, *prev;
3946 
3947 	/* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
3948 	 * "provisioning_sp=<FQDN> */
3949 	if (os_strcmp(cmd, "all") == 0) {
3950 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
3951 		return wpas_remove_all_creds(wpa_s);
3952 	}
3953 
3954 	if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
3955 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
3956 			   cmd + 8);
3957 		cred = wpa_s->conf->cred;
3958 		while (cred) {
3959 			prev = cred;
3960 			cred = cred->next;
3961 			if (prev->domain) {
3962 				size_t i;
3963 				for (i = 0; i < prev->num_domain; i++) {
3964 					if (os_strcmp(prev->domain[i], cmd + 8)
3965 					    != 0)
3966 						continue;
3967 					wpas_remove_cred(wpa_s, prev);
3968 					break;
3969 				}
3970 			}
3971 		}
3972 		return 0;
3973 	}
3974 
3975 	if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
3976 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
3977 			   cmd + 16);
3978 		cred = wpa_s->conf->cred;
3979 		while (cred) {
3980 			prev = cred;
3981 			cred = cred->next;
3982 			if (prev->provisioning_sp &&
3983 			    os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
3984 				wpas_remove_cred(wpa_s, prev);
3985 		}
3986 		return 0;
3987 	}
3988 
3989 	id = atoi(cmd);
3990 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
3991 
3992 	cred = wpa_config_get_cred(wpa_s->conf, id);
3993 	return wpas_remove_cred(wpa_s, cred);
3994 }
3995 
3996 
wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant * wpa_s,char * cmd)3997 static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
3998 					      char *cmd)
3999 {
4000 	int id;
4001 	struct wpa_cred *cred;
4002 	char *name, *value;
4003 
4004 	/* cmd: "<cred id> <variable name> <value>" */
4005 	name = os_strchr(cmd, ' ');
4006 	if (name == NULL)
4007 		return -1;
4008 	*name++ = '\0';
4009 
4010 	value = os_strchr(name, ' ');
4011 	if (value == NULL)
4012 		return -1;
4013 	*value++ = '\0';
4014 
4015 	id = atoi(cmd);
4016 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
4017 		   id, name);
4018 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
4019 			      (u8 *) value, os_strlen(value));
4020 
4021 	cred = wpa_config_get_cred(wpa_s->conf, id);
4022 	if (cred == NULL) {
4023 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
4024 			   id);
4025 		return -1;
4026 	}
4027 
4028 	if (wpa_config_set_cred(cred, name, value, 0) < 0) {
4029 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
4030 			   "variable '%s'", name);
4031 		return -1;
4032 	}
4033 
4034 	wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
4035 
4036 	return 0;
4037 }
4038 
4039 
wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)4040 static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
4041 					      char *cmd, char *buf,
4042 					      size_t buflen)
4043 {
4044 	int id;
4045 	size_t res;
4046 	struct wpa_cred *cred;
4047 	char *name, *value;
4048 
4049 	/* cmd: "<cred id> <variable name>" */
4050 	name = os_strchr(cmd, ' ');
4051 	if (name == NULL)
4052 		return -1;
4053 	*name++ = '\0';
4054 
4055 	id = atoi(cmd);
4056 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
4057 		   id, name);
4058 
4059 	cred = wpa_config_get_cred(wpa_s->conf, id);
4060 	if (cred == NULL) {
4061 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
4062 			   id);
4063 		return -1;
4064 	}
4065 
4066 	value = wpa_config_get_cred_no_key(cred, name);
4067 	if (value == NULL) {
4068 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
4069 			   name);
4070 		return -1;
4071 	}
4072 
4073 	res = os_strlcpy(buf, value, buflen);
4074 	if (res >= buflen) {
4075 		os_free(value);
4076 		return -1;
4077 	}
4078 
4079 	os_free(value);
4080 
4081 	return res;
4082 }
4083 
4084 
4085 #ifndef CONFIG_NO_CONFIG_WRITE
wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant * wpa_s)4086 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
4087 {
4088 	int ret;
4089 
4090 	if (!wpa_s->conf->update_config) {
4091 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
4092 			   "to update configuration (update_config=0)");
4093 		return -1;
4094 	}
4095 
4096 	ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
4097 	if (ret) {
4098 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
4099 			   "update configuration");
4100 	} else {
4101 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
4102 			   " updated");
4103 	}
4104 
4105 	return ret;
4106 }
4107 #endif /* CONFIG_NO_CONFIG_WRITE */
4108 
4109 
4110 struct cipher_info {
4111 	unsigned int capa;
4112 	const char *name;
4113 	int group_only;
4114 };
4115 
4116 static const struct cipher_info ciphers[] = {
4117 	{ WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
4118 	{ WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
4119 	{ WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
4120 	{ WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
4121 #ifndef CONFIG_NO_TKIP
4122 	{ WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
4123 #endif /* CONFIG_NO_TKIP */
4124 	{ WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
4125 #ifdef CONFIG_WEP
4126 	{ WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
4127 	{ WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
4128 #endif /* CONFIG_WEP */
4129 };
4130 
4131 static const struct cipher_info ciphers_group_mgmt[] = {
4132 	{ WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
4133 	{ WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
4134 	{ WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
4135 	{ WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
4136 };
4137 
4138 
ctrl_iface_get_capability_pairwise(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4139 static int ctrl_iface_get_capability_pairwise(int res, bool strict,
4140 					      struct wpa_driver_capa *capa,
4141 					      char *buf, size_t buflen)
4142 {
4143 	int ret;
4144 	char *pos, *end;
4145 	size_t len;
4146 	unsigned int i;
4147 
4148 	pos = buf;
4149 	end = pos + buflen;
4150 
4151 	if (res < 0) {
4152 		if (strict)
4153 			return 0;
4154 #ifdef CONFIG_NO_TKIP
4155 		len = os_strlcpy(buf, "CCMP NONE", buflen);
4156 #else /* CONFIG_NO_TKIP */
4157 		len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
4158 #endif /* CONFIG_NO_TKIP */
4159 		if (len >= buflen)
4160 			return -1;
4161 		return len;
4162 	}
4163 
4164 	for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
4165 		if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
4166 			ret = os_snprintf(pos, end - pos, "%s%s",
4167 					  pos == buf ? "" : " ",
4168 					  ciphers[i].name);
4169 			if (os_snprintf_error(end - pos, ret))
4170 				return pos - buf;
4171 			pos += ret;
4172 		}
4173 	}
4174 
4175 	return pos - buf;
4176 }
4177 
4178 
ctrl_iface_get_capability_group(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4179 static int ctrl_iface_get_capability_group(int res, bool strict,
4180 					   struct wpa_driver_capa *capa,
4181 					   char *buf, size_t buflen)
4182 {
4183 	int ret;
4184 	char *pos, *end;
4185 	size_t len;
4186 	unsigned int i;
4187 
4188 	pos = buf;
4189 	end = pos + buflen;
4190 
4191 	if (res < 0) {
4192 		if (strict)
4193 			return 0;
4194 #ifdef CONFIG_WEP
4195 #ifdef CONFIG_NO_TKIP
4196 		len = os_strlcpy(buf, "CCMP WEP104 WEP40", buflen);
4197 #else /* CONFIG_NO_TKIP */
4198 		len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
4199 #endif /* CONFIG_NO_TKIP */
4200 #else /* CONFIG_WEP */
4201 #ifdef CONFIG_NO_TKIP
4202 		len = os_strlcpy(buf, "CCMP", buflen);
4203 #else /* CONFIG_NO_TKIP */
4204 		len = os_strlcpy(buf, "CCMP TKIP", buflen);
4205 #endif /* CONFIG_NO_TKIP */
4206 #endif /* CONFIG_WEP */
4207 		if (len >= buflen)
4208 			return -1;
4209 		return len;
4210 	}
4211 
4212 	for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
4213 		if (capa->enc & ciphers[i].capa) {
4214 			ret = os_snprintf(pos, end - pos, "%s%s",
4215 					  pos == buf ? "" : " ",
4216 					  ciphers[i].name);
4217 			if (os_snprintf_error(end - pos, ret))
4218 				return pos - buf;
4219 			pos += ret;
4220 		}
4221 	}
4222 
4223 	return pos - buf;
4224 }
4225 
4226 
ctrl_iface_get_capability_group_mgmt(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4227 static int ctrl_iface_get_capability_group_mgmt(int res, bool strict,
4228 						struct wpa_driver_capa *capa,
4229 						char *buf, size_t buflen)
4230 {
4231 	int ret;
4232 	char *pos, *end;
4233 	unsigned int i;
4234 
4235 	pos = buf;
4236 	end = pos + buflen;
4237 
4238 	if (res < 0)
4239 		return 0;
4240 
4241 	for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
4242 		if (capa->enc & ciphers_group_mgmt[i].capa) {
4243 			ret = os_snprintf(pos, end - pos, "%s%s",
4244 					  pos == buf ? "" : " ",
4245 					  ciphers_group_mgmt[i].name);
4246 			if (os_snprintf_error(end - pos, ret))
4247 				return pos - buf;
4248 			pos += ret;
4249 		}
4250 	}
4251 
4252 	return pos - buf;
4253 }
4254 
4255 
iftype_str_to_index(const char * iftype_str)4256 static int iftype_str_to_index(const char *iftype_str)
4257 {
4258 	if (!iftype_str)
4259 		return WPA_IF_MAX;
4260 
4261 	if (os_strcmp(iftype_str, "STATION") == 0)
4262 		return WPA_IF_STATION;
4263 
4264 	if (os_strcmp(iftype_str, "AP_VLAN") == 0)
4265 		return WPA_IF_AP_VLAN;
4266 
4267 	if (os_strcmp(iftype_str, "AP") == 0)
4268 		return WPA_IF_AP_BSS;
4269 
4270 	if (os_strcmp(iftype_str, "P2P_GO") == 0)
4271 		return WPA_IF_P2P_GO;
4272 
4273 	if (os_strcmp(iftype_str, "P2P_CLIENT") == 0)
4274 		return WPA_IF_P2P_CLIENT;
4275 
4276 	if (os_strcmp(iftype_str, "P2P_DEVICE") == 0)
4277 		return WPA_IF_P2P_DEVICE;
4278 
4279 	if (os_strcmp(iftype_str, "MESH") == 0)
4280 		return WPA_IF_MESH;
4281 
4282 	if (os_strcmp(iftype_str, "IBSS") == 0)
4283 		return WPA_IF_IBSS;
4284 
4285 	if (os_strcmp(iftype_str, "NAN") == 0)
4286 		return WPA_IF_NAN;
4287 
4288 	if (os_strcmp(iftype_str, "NAN_DATA") == 0)
4289 		return WPA_IF_NAN_DATA;
4290 
4291 	return WPA_IF_MAX;
4292 }
4293 
4294 
ctrl_iface_get_capability_key_mgmt(int res,bool strict,struct wpa_driver_capa * capa,const char * iftype_str,char * buf,size_t buflen)4295 static int ctrl_iface_get_capability_key_mgmt(int res, bool strict,
4296 					      struct wpa_driver_capa *capa,
4297 					      const char *iftype_str,
4298 					      char *buf, size_t buflen)
4299 {
4300 	int ret;
4301 	unsigned int key_mgmt;
4302 	char *pos, *end;
4303 	size_t len;
4304 
4305 	pos = buf;
4306 	end = pos + buflen;
4307 
4308 	if (res < 0) {
4309 		if (strict)
4310 			return 0;
4311 		len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
4312 				 "NONE", buflen);
4313 		if (len >= buflen)
4314 			return -1;
4315 		return len;
4316 	}
4317 
4318 	if (iftype_str) {
4319 		enum wpa_driver_if_type iftype;
4320 
4321 		iftype = iftype_str_to_index(iftype_str);
4322 		if (iftype == WPA_IF_MAX)
4323 			return -1;
4324 		key_mgmt = capa->key_mgmt_iftype[iftype];
4325 	} else {
4326 		key_mgmt = capa->key_mgmt;
4327 	}
4328 
4329 	ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
4330 	if (os_snprintf_error(end - pos, ret))
4331 		return pos - buf;
4332 	pos += ret;
4333 
4334 	if (key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4335 			WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
4336 		ret = os_snprintf(pos, end - pos, " WPA-EAP");
4337 		if (os_snprintf_error(end - pos, ret))
4338 			return pos - buf;
4339 		pos += ret;
4340 	}
4341 
4342 	if (key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
4343 			WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
4344 		ret = os_snprintf(pos, end - pos, " WPA-PSK");
4345 		if (os_snprintf_error(end - pos, ret))
4346 			return pos - buf;
4347 		pos += ret;
4348 	}
4349 
4350 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
4351 		ret = os_snprintf(pos, end - pos, " WPA-NONE");
4352 		if (os_snprintf_error(end - pos, ret))
4353 			return pos - buf;
4354 		pos += ret;
4355 	}
4356 
4357 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WAPI_PSK) {
4358 		ret = os_snprintf(pos, end - pos, " WAPI-PSK");
4359 		if (os_snprintf_error(end - pos, ret))
4360 			return pos - buf;
4361 		pos += ret;
4362 	}
4363 
4364 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_TPK_HANDSHAKE) {
4365 		ret = os_snprintf(pos, end - pos, " TPK-HANDSHAKE");
4366 		if (os_snprintf_error(end - pos, ret))
4367 			return pos - buf;
4368 		pos += ret;
4369 	}
4370 
4371 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_CCKM) {
4372 		ret = os_snprintf(pos, end - pos, " CCKM");
4373 		if (os_snprintf_error(end - pos, ret))
4374 			return pos - buf;
4375 		pos += ret;
4376 	}
4377 
4378 #ifdef CONFIG_SUITEB
4379 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
4380 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
4381 		if (os_snprintf_error(end - pos, ret))
4382 			return pos - buf;
4383 		pos += ret;
4384 	}
4385 #endif /* CONFIG_SUITEB */
4386 #ifdef CONFIG_SUITEB192
4387 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
4388 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
4389 		if (os_snprintf_error(end - pos, ret))
4390 			return pos - buf;
4391 		pos += ret;
4392 	}
4393 #endif /* CONFIG_SUITEB192 */
4394 #ifdef CONFIG_OWE
4395 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OWE) {
4396 		ret = os_snprintf(pos, end - pos, " OWE");
4397 		if (os_snprintf_error(end - pos, ret))
4398 			return pos - buf;
4399 		pos += ret;
4400 	}
4401 #endif /* CONFIG_OWE */
4402 #ifdef CONFIG_DPP
4403 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_DPP) {
4404 		ret = os_snprintf(pos, end - pos, " DPP");
4405 		if (os_snprintf_error(end - pos, ret))
4406 			return pos - buf;
4407 		pos += ret;
4408 	}
4409 #endif /* CONFIG_DPP */
4410 #ifdef CONFIG_FILS
4411 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA256) {
4412 		ret = os_snprintf(pos, end - pos, " FILS-SHA256");
4413 		if (os_snprintf_error(end - pos, ret))
4414 			return pos - buf;
4415 		pos += ret;
4416 	}
4417 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA384) {
4418 		ret = os_snprintf(pos, end - pos, " FILS-SHA384");
4419 		if (os_snprintf_error(end - pos, ret))
4420 			return pos - buf;
4421 		pos += ret;
4422 	}
4423 #ifdef CONFIG_IEEE80211R
4424 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA256) {
4425 		ret = os_snprintf(pos, end - pos, " FT-FILS-SHA256");
4426 		if (os_snprintf_error(end - pos, ret))
4427 			return pos - buf;
4428 		pos += ret;
4429 	}
4430 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA384) {
4431 		ret = os_snprintf(pos, end - pos, " FT-FILS-SHA384");
4432 		if (os_snprintf_error(end - pos, ret))
4433 			return pos - buf;
4434 		pos += ret;
4435 	}
4436 #endif /* CONFIG_IEEE80211R */
4437 #endif /* CONFIG_FILS */
4438 #ifdef CONFIG_IEEE80211R
4439 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK) {
4440 		ret = os_snprintf(pos, end - pos, " FT-PSK");
4441 		if (os_snprintf_error(end - pos, ret))
4442 			return pos - buf;
4443 		pos += ret;
4444 	}
4445 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) {
4446 		ret = os_snprintf(pos, end - pos, " FT-EAP");
4447 		if (os_snprintf_error(end - pos, ret))
4448 			return pos - buf;
4449 		pos += ret;
4450 	}
4451 #ifdef CONFIG_SAE
4452 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_SAE) {
4453 		ret = os_snprintf(pos, end - pos, " FT-SAE");
4454 		if (os_snprintf_error(end - pos, ret))
4455 			return pos - buf;
4456 		pos += ret;
4457 	}
4458 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_SAE_EXT_KEY) {
4459 		ret = os_snprintf(pos, end - pos, " FT-SAE-EXT-KEY");
4460 		if (os_snprintf_error(end - pos, ret))
4461 			return pos - buf;
4462 		pos += ret;
4463 	}
4464 #endif /* CONFIG_SAE */
4465 #ifdef CONFIG_SHA384
4466 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_802_1X_SHA384) {
4467 		ret = os_snprintf(pos, end - pos, " FT-EAP-SHA384");
4468 		if (os_snprintf_error(end - pos, ret))
4469 			return pos - buf;
4470 		pos += ret;
4471 	}
4472 #endif /* CONFIG_SHA384 */
4473 #endif /* CONFIG_IEEE80211R */
4474 #ifdef CONFIG_SAE
4475 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE) {
4476 		ret = os_snprintf(pos, end - pos, " SAE");
4477 		if (os_snprintf_error(end - pos, ret))
4478 			return pos - buf;
4479 		pos += ret;
4480 	}
4481 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE_EXT_KEY) {
4482 		ret = os_snprintf(pos, end - pos, " SAE-EXT-KEY");
4483 		if (os_snprintf_error(end - pos, ret))
4484 			return pos - buf;
4485 		pos += ret;
4486 	}
4487 #endif /* CONFIG_SAE */
4488 #ifdef CONFIG_SHA256
4489 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_802_1X_SHA256) {
4490 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SHA256");
4491 		if (os_snprintf_error(end - pos, ret))
4492 			return pos - buf;
4493 		pos += ret;
4494 	}
4495 
4496 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_PSK_SHA256) {
4497 		ret = os_snprintf(pos, end - pos, " WPA-PSK-SHA256");
4498 		if (os_snprintf_error(end - pos, ret))
4499 			return pos - buf;
4500 		pos += ret;
4501 	}
4502 #endif /* CONFIG_SHA256 */
4503 
4504 	return pos - buf;
4505 }
4506 
4507 
ctrl_iface_get_capability_proto(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4508 static int ctrl_iface_get_capability_proto(int res, bool strict,
4509 					   struct wpa_driver_capa *capa,
4510 					   char *buf, size_t buflen)
4511 {
4512 	int ret;
4513 	char *pos, *end;
4514 	size_t len;
4515 
4516 	pos = buf;
4517 	end = pos + buflen;
4518 
4519 	if (res < 0) {
4520 		if (strict)
4521 			return 0;
4522 		len = os_strlcpy(buf, "RSN WPA", buflen);
4523 		if (len >= buflen)
4524 			return -1;
4525 		return len;
4526 	}
4527 
4528 	if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
4529 			      WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
4530 		ret = os_snprintf(pos, end - pos, "%sRSN",
4531 				  pos == buf ? "" : " ");
4532 		if (os_snprintf_error(end - pos, ret))
4533 			return pos - buf;
4534 		pos += ret;
4535 	}
4536 
4537 	if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4538 			      WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
4539 		ret = os_snprintf(pos, end - pos, "%sWPA",
4540 				  pos == buf ? "" : " ");
4541 		if (os_snprintf_error(end - pos, ret))
4542 			return pos - buf;
4543 		pos += ret;
4544 	}
4545 
4546 	return pos - buf;
4547 }
4548 
4549 
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)4550 static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
4551 					      int res, bool strict,
4552 					      struct wpa_driver_capa *capa,
4553 					      char *buf, size_t buflen)
4554 {
4555 	int ret;
4556 	char *pos, *end;
4557 	size_t len;
4558 
4559 	pos = buf;
4560 	end = pos + buflen;
4561 
4562 	if (res < 0) {
4563 		if (strict)
4564 			return 0;
4565 		len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
4566 		if (len >= buflen)
4567 			return -1;
4568 		return len;
4569 	}
4570 
4571 	if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
4572 		ret = os_snprintf(pos, end - pos, "%sOPEN",
4573 				  pos == buf ? "" : " ");
4574 		if (os_snprintf_error(end - pos, ret))
4575 			return pos - buf;
4576 		pos += ret;
4577 	}
4578 
4579 	if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
4580 		ret = os_snprintf(pos, end - pos, "%sSHARED",
4581 				  pos == buf ? "" : " ");
4582 		if (os_snprintf_error(end - pos, ret))
4583 			return pos - buf;
4584 		pos += ret;
4585 	}
4586 
4587 	if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
4588 		ret = os_snprintf(pos, end - pos, "%sLEAP",
4589 				  pos == buf ? "" : " ");
4590 		if (os_snprintf_error(end - pos, ret))
4591 			return pos - buf;
4592 		pos += ret;
4593 	}
4594 
4595 #ifdef CONFIG_SAE
4596 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
4597 		ret = os_snprintf(pos, end - pos, "%sSAE",
4598 				  pos == buf ? "" : " ");
4599 		if (os_snprintf_error(end - pos, ret))
4600 			return pos - buf;
4601 		pos += ret;
4602 	}
4603 #endif /* CONFIG_SAE */
4604 
4605 #ifdef CONFIG_FILS
4606 	if (wpa_is_fils_supported(wpa_s)) {
4607 		ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITHOUT_PFS",
4608 				  pos == buf ? "" : " ");
4609 		if (os_snprintf_error(end - pos, ret))
4610 			return pos - buf;
4611 		pos += ret;
4612 	}
4613 
4614 #ifdef CONFIG_FILS_SK_PFS
4615 	if (wpa_is_fils_sk_pfs_supported(wpa_s)) {
4616 		ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITH_PFS",
4617 				  pos == buf ? "" : " ");
4618 		if (os_snprintf_error(end - pos, ret))
4619 			return pos - buf;
4620 		pos += ret;
4621 	}
4622 #endif /* CONFIG_FILS_SK_PFS */
4623 #endif /* CONFIG_FILS */
4624 
4625 #ifdef CONFIG_PASN
4626 	ret = os_snprintf(pos, end - pos, "%sPASN",
4627 			  pos == buf ? "" : " ");
4628 	if (os_snprintf_error(end - pos, ret))
4629 		return pos - buf;
4630 	pos += ret;
4631 
4632 #endif /* CONFIG_PASN */
4633 
4634 #if defined(CONFIG_ENC_ASSOC) && defined(CONFIG_PASN) && defined(CONFIG_SAE)
4635 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) &&
4636 	    (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_EPPKE) &&
4637 	    (wpa_s->drv_flags2 &
4638 	     WPA_DRIVER_FLAGS2_ASSOCIATION_FRAME_ENCRYPTION)) {
4639 		ret = os_snprintf(pos, end - pos, "%sEPPKE",
4640 				  pos == buf ? "" : " ");
4641 		if (os_snprintf_error(end - pos, ret))
4642 			return pos - buf;
4643 		pos += ret;
4644 	}
4645 #endif /* CONFIG_ENC_ASSOC && CONFIG_PASN && CONFIG_SAE */
4646 
4647 	return pos - buf;
4648 }
4649 
4650 
ctrl_iface_get_capability_modes(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4651 static int ctrl_iface_get_capability_modes(int res, bool strict,
4652 					   struct wpa_driver_capa *capa,
4653 					   char *buf, size_t buflen)
4654 {
4655 	int ret;
4656 	char *pos, *end;
4657 	size_t len;
4658 
4659 	pos = buf;
4660 	end = pos + buflen;
4661 
4662 	if (res < 0) {
4663 		if (strict)
4664 			return 0;
4665 		len = os_strlcpy(buf, "IBSS AP", buflen);
4666 		if (len >= buflen)
4667 			return -1;
4668 		return len;
4669 	}
4670 
4671 	if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
4672 		ret = os_snprintf(pos, end - pos, "%sIBSS",
4673 				  pos == buf ? "" : " ");
4674 		if (os_snprintf_error(end - pos, ret))
4675 			return pos - buf;
4676 		pos += ret;
4677 	}
4678 
4679 	if (capa->flags & WPA_DRIVER_FLAGS_AP) {
4680 		ret = os_snprintf(pos, end - pos, "%sAP",
4681 				  pos == buf ? "" : " ");
4682 		if (os_snprintf_error(end - pos, ret))
4683 			return pos - buf;
4684 		pos += ret;
4685 	}
4686 
4687 #ifdef CONFIG_MESH
4688 	if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
4689 		ret = os_snprintf(pos, end - pos, "%sMESH",
4690 				  pos == buf ? "" : " ");
4691 		if (os_snprintf_error(end - pos, ret))
4692 			return pos - buf;
4693 		pos += ret;
4694 	}
4695 #endif /* CONFIG_MESH */
4696 
4697 	return pos - buf;
4698 }
4699 
4700 
ctrl_iface_get_capability_channels(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)4701 static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
4702 					      char *buf, size_t buflen)
4703 {
4704 	struct hostapd_channel_data *chnl;
4705 	int ret, i, j;
4706 	char *pos, *end, *hmode;
4707 
4708 	pos = buf;
4709 	end = pos + buflen;
4710 
4711 	for (j = 0; j < wpa_s->hw.num_modes; j++) {
4712 		switch (wpa_s->hw.modes[j].mode) {
4713 		case HOSTAPD_MODE_IEEE80211B:
4714 			hmode = "B";
4715 			break;
4716 		case HOSTAPD_MODE_IEEE80211G:
4717 			hmode = "G";
4718 			break;
4719 		case HOSTAPD_MODE_IEEE80211A:
4720 			hmode = "A";
4721 			break;
4722 		case HOSTAPD_MODE_IEEE80211AD:
4723 			hmode = "AD";
4724 			break;
4725 		default:
4726 			continue;
4727 		}
4728 		ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
4729 		if (os_snprintf_error(end - pos, ret))
4730 			return pos - buf;
4731 		pos += ret;
4732 		chnl = wpa_s->hw.modes[j].channels;
4733 		for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
4734 			if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
4735 				continue;
4736 			ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
4737 			if (os_snprintf_error(end - pos, ret))
4738 				return pos - buf;
4739 			pos += ret;
4740 		}
4741 		ret = os_snprintf(pos, end - pos, "\n");
4742 		if (os_snprintf_error(end - pos, ret))
4743 			return pos - buf;
4744 		pos += ret;
4745 	}
4746 
4747 	return pos - buf;
4748 }
4749 
4750 
ctrl_iface_get_capability_freq(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)4751 static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
4752 					  char *buf, size_t buflen)
4753 {
4754 	struct hostapd_channel_data *chnl;
4755 	int ret, i, j;
4756 	char *pos, *end, *hmode;
4757 
4758 	pos = buf;
4759 	end = pos + buflen;
4760 
4761 	for (j = 0; j < wpa_s->hw.num_modes; j++) {
4762 		switch (wpa_s->hw.modes[j].mode) {
4763 		case HOSTAPD_MODE_IEEE80211B:
4764 			hmode = "B";
4765 			break;
4766 		case HOSTAPD_MODE_IEEE80211G:
4767 			hmode = "G";
4768 			break;
4769 		case HOSTAPD_MODE_IEEE80211A:
4770 			hmode = "A";
4771 			break;
4772 		case HOSTAPD_MODE_IEEE80211AD:
4773 			hmode = "AD";
4774 			break;
4775 		default:
4776 			continue;
4777 		}
4778 		ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
4779 				  hmode);
4780 		if (os_snprintf_error(end - pos, ret))
4781 			return pos - buf;
4782 		pos += ret;
4783 		chnl = wpa_s->hw.modes[j].channels;
4784 		for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
4785 			if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
4786 				continue;
4787 			ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
4788 					  chnl[i].chan, chnl[i].freq,
4789 					  chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
4790 					  " (NO_IR)" : "",
4791 					  chnl[i].flag & HOSTAPD_CHAN_RADAR ?
4792 					  " (DFS)" : "");
4793 
4794 			if (os_snprintf_error(end - pos, ret))
4795 				return pos - buf;
4796 			pos += ret;
4797 		}
4798 		ret = os_snprintf(pos, end - pos, "\n");
4799 		if (os_snprintf_error(end - pos, ret))
4800 			return pos - buf;
4801 		pos += ret;
4802 	}
4803 
4804 	return pos - buf;
4805 }
4806 
4807 
wpa_supplicant_ctrl_iface_get_capability(struct wpa_supplicant * wpa_s,const char * _field,char * buf,size_t buflen)4808 static int wpa_supplicant_ctrl_iface_get_capability(
4809 	struct wpa_supplicant *wpa_s, const char *_field, char *buf,
4810 	size_t buflen)
4811 {
4812 	struct wpa_driver_capa capa;
4813 	int res;
4814 	char *next_param, *curr_param, *iftype = NULL;
4815 	bool strict = false;
4816 	char field[50];
4817 	size_t len;
4818 
4819 	/* Determine whether or not strict checking was requested */
4820 	len = os_strlcpy(field, _field, sizeof(field));
4821 	if (len >= sizeof(field))
4822 		return -1;
4823 
4824 	next_param = os_strchr(field, ' ');
4825 	while (next_param) {
4826 		*next_param++ = '\0';
4827 		curr_param = next_param;
4828 		next_param = os_strchr(next_param, ' ');
4829 
4830 		if (next_param)
4831 			*next_param = '\0';
4832 
4833 		if (os_strcmp(curr_param, "strict") == 0)
4834 			strict = true;
4835 		else if (os_strncmp(curr_param, "iftype=", 7) == 0)
4836 			iftype = curr_param + 7;
4837 		else
4838 			return -1;
4839 	}
4840 
4841 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'%s%s%s",
4842 		   field, iftype ? " iftype=" : "", iftype ? iftype : "",
4843 		   strict ? " strict" : "");
4844 
4845 	if (os_strcmp(field, "eap") == 0) {
4846 		return eap_get_names(buf, buflen);
4847 	}
4848 
4849 	res = wpa_drv_get_capa(wpa_s, &capa);
4850 
4851 	if (os_strcmp(field, "pairwise") == 0)
4852 		return ctrl_iface_get_capability_pairwise(res, strict, &capa,
4853 							  buf, buflen);
4854 
4855 	if (os_strcmp(field, "group") == 0)
4856 		return ctrl_iface_get_capability_group(res, strict, &capa,
4857 						       buf, buflen);
4858 
4859 	if (os_strcmp(field, "group_mgmt") == 0)
4860 		return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
4861 							    buf, buflen);
4862 
4863 	if (os_strcmp(field, "key_mgmt") == 0)
4864 		return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
4865 							  iftype, buf, buflen);
4866 
4867 	if (os_strcmp(field, "proto") == 0)
4868 		return ctrl_iface_get_capability_proto(res, strict, &capa,
4869 						       buf, buflen);
4870 
4871 	if (os_strcmp(field, "auth_alg") == 0)
4872 		return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
4873 							  &capa, buf, buflen);
4874 
4875 	if (os_strcmp(field, "modes") == 0)
4876 		return ctrl_iface_get_capability_modes(res, strict, &capa,
4877 						       buf, buflen);
4878 
4879 	if (os_strcmp(field, "channels") == 0)
4880 		return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
4881 
4882 	if (os_strcmp(field, "freq") == 0)
4883 		return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
4884 
4885 #ifdef CONFIG_TDLS
4886 	if (os_strcmp(field, "tdls") == 0)
4887 		return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
4888 #endif /* CONFIG_TDLS */
4889 
4890 #ifdef CONFIG_ERP
4891 	if (os_strcmp(field, "erp") == 0) {
4892 		res = os_snprintf(buf, buflen, "ERP");
4893 		if (os_snprintf_error(buflen, res))
4894 			return -1;
4895 		return res;
4896 	}
4897 #endif /* CONFIG_EPR */
4898 
4899 #ifdef CONFIG_FIPS
4900 	if (os_strcmp(field, "fips") == 0) {
4901 		res = os_snprintf(buf, buflen, "FIPS");
4902 		if (os_snprintf_error(buflen, res))
4903 			return -1;
4904 		return res;
4905 	}
4906 #endif /* CONFIG_FIPS */
4907 
4908 #ifdef CONFIG_ACS
4909 	if (os_strcmp(field, "acs") == 0) {
4910 		res = os_snprintf(buf, buflen, "ACS");
4911 		if (os_snprintf_error(buflen, res))
4912 			return -1;
4913 		return res;
4914 	}
4915 #endif /* CONFIG_ACS */
4916 
4917 #ifdef CONFIG_FILS
4918 	if (os_strcmp(field, "fils") == 0) {
4919 #ifdef CONFIG_FILS_SK_PFS
4920 		if (wpa_is_fils_supported(wpa_s) &&
4921 		    wpa_is_fils_sk_pfs_supported(wpa_s)) {
4922 			res = os_snprintf(buf, buflen, "FILS FILS-SK-PFS");
4923 			if (os_snprintf_error(buflen, res))
4924 				return -1;
4925 			return res;
4926 		}
4927 #endif /* CONFIG_FILS_SK_PFS */
4928 
4929 		if (wpa_is_fils_supported(wpa_s)) {
4930 			res = os_snprintf(buf, buflen, "FILS");
4931 			if (os_snprintf_error(buflen, res))
4932 				return -1;
4933 			return res;
4934 		}
4935 	}
4936 #endif /* CONFIG_FILS */
4937 
4938 	if (os_strcmp(field, "multibss") == 0 && wpa_s->multi_bss_support) {
4939 		res = os_snprintf(buf, buflen, "MULTIBSS-STA");
4940 		if (os_snprintf_error(buflen, res))
4941 			return -1;
4942 		return res;
4943 	}
4944 
4945 #ifdef CONFIG_DPP
4946 	if (os_strcmp(field, "dpp") == 0) {
4947 #ifdef CONFIG_DPP3
4948 		res = os_snprintf(buf, buflen, "DPP=3");
4949 #elif defined(CONFIG_DPP2)
4950 		res = os_snprintf(buf, buflen, "DPP=2");
4951 #else /* CONFIG_DPP2 */
4952 		res = os_snprintf(buf, buflen, "DPP=1");
4953 #endif /* CONFIG_DPP2 */
4954 		if (os_snprintf_error(buflen, res))
4955 			return -1;
4956 		return res;
4957 	}
4958 #endif /* CONFIG_DPP */
4959 
4960 	if (os_strcmp(field, "nan") == 0) {
4961 		char *pos = buf;
4962 
4963 #ifdef CONFIG_NAN_USD
4964 		res = os_snprintf(pos, buflen, "USD");
4965 		if (os_snprintf_error(buflen, res))
4966 			return -1;
4967 
4968 		pos += res;
4969 		buflen -= res;
4970 #endif /* CONFIG_NAN_USD */
4971 #ifdef CONFIG_NAN
4972 		if ((wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_SUPPORT_NAN)) {
4973 			res = os_snprintf(pos, buflen, "%sNAN",
4974 					  pos == buf ? "" : " ");
4975 			if (os_snprintf_error(buflen, res))
4976 				return -1;
4977 
4978 			pos += res;
4979 			buflen -= res;
4980 		}
4981 #endif /* CONFIG_NAN */
4982 		return pos - buf;
4983 	}
4984 
4985 #ifdef CONFIG_SAE
4986 	if (os_strcmp(field, "sae") == 0 &&
4987 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE)) {
4988 #ifdef CONFIG_SAE_PK
4989 		res = os_snprintf(buf, buflen, "H2E PK");
4990 #else /* CONFIG_SAE_PK */
4991 		res = os_snprintf(buf, buflen, "H2E");
4992 #endif /* CONFIG_SAE_PK */
4993 		if (os_snprintf_error(buflen, res))
4994 			return -1;
4995 		return res;
4996 	}
4997 #endif /* CONFIG_SAE */
4998 
4999 #ifdef CONFIG_OCV
5000 	if (os_strcmp(field, "ocv") == 0) {
5001 		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
5002 		    (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_OCV))
5003 			res = os_snprintf(buf, buflen, "supported");
5004 		else
5005 			res = os_snprintf(buf, buflen, "not supported");
5006 		if (os_snprintf_error(buflen, res))
5007 			return -1;
5008 		return res;
5009 	}
5010 #endif /* CONFIG_OCV */
5011 
5012 	if (os_strcmp(field, "beacon_prot") == 0) {
5013 		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_BEACON_PROTECTION) ||
5014 		    (wpa_s->drv_flags2 &
5015 		     WPA_DRIVER_FLAGS2_BEACON_PROTECTION_CLIENT))
5016 			res = os_snprintf(buf, buflen, "supported");
5017 		else
5018 			res = os_snprintf(buf, buflen, "not supported");
5019 		if (os_snprintf_error(buflen, res))
5020 			return -1;
5021 		return res;
5022 	}
5023 
5024 #ifdef CONFIG_P2P
5025 	if (os_strcmp(field, "p2p2") == 0) {
5026 		if (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_P2P_FEATURE_V2)
5027 			res = os_snprintf(buf, buflen, "supported");
5028 		else
5029 			res = os_snprintf(buf, buflen, "not supported");
5030 		if (os_snprintf_error(buflen, res))
5031 			return -1;
5032 		return res;
5033 	}
5034 
5035 	if (os_strcmp(field, "pcc_mode") == 0) {
5036 		if (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_P2P_FEATURE_PCC_MODE)
5037 			res = os_snprintf(buf, buflen, "supported");
5038 		else
5039 			res = os_snprintf(buf, buflen, "not supported");
5040 		if (os_snprintf_error(buflen, res))
5041 			return -1;
5042 		return res;
5043 	}
5044 #endif /* CONFIG_P2P */
5045 
5046 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
5047 		   field);
5048 
5049 	return -1;
5050 }
5051 
5052 
5053 #ifdef CONFIG_INTERWORKING
anqp_add_hex(char * pos,char * end,const char * title,struct wpabuf * data)5054 static char * anqp_add_hex(char *pos, char *end, const char *title,
5055 			   struct wpabuf *data)
5056 {
5057 	char *start = pos;
5058 	size_t i;
5059 	int ret;
5060 	const u8 *d;
5061 
5062 	if (data == NULL)
5063 		return start;
5064 
5065 	ret = os_snprintf(pos, end - pos, "%s=", title);
5066 	if (os_snprintf_error(end - pos, ret))
5067 		return start;
5068 	pos += ret;
5069 
5070 	d = wpabuf_head_u8(data);
5071 	for (i = 0; i < wpabuf_len(data); i++) {
5072 		ret = os_snprintf(pos, end - pos, "%02x", *d++);
5073 		if (os_snprintf_error(end - pos, ret))
5074 			return start;
5075 		pos += ret;
5076 	}
5077 
5078 	ret = os_snprintf(pos, end - pos, "\n");
5079 	if (os_snprintf_error(end - pos, ret))
5080 		return start;
5081 	pos += ret;
5082 
5083 	return pos;
5084 }
5085 #endif /* CONFIG_INTERWORKING */
5086 
5087 
5088 #ifdef CONFIG_FILS
print_fils_indication(struct wpa_bss * bss,char * pos,char * end)5089 static int print_fils_indication(struct wpa_bss *bss, char *pos, char *end)
5090 {
5091 	char *start = pos;
5092 	const u8 *ie, *ie_end;
5093 	u16 info, realms;
5094 	int ret;
5095 
5096 	ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
5097 	if (!ie)
5098 		return 0;
5099 	ie_end = ie + 2 + ie[1];
5100 	ie += 2;
5101 	if (ie_end - ie < 2)
5102 		return 0;
5103 
5104 	info = WPA_GET_LE16(ie);
5105 	ie += 2;
5106 	ret = os_snprintf(pos, end - pos, "fils_info=%04x\n", info);
5107 	if (os_snprintf_error(end - pos, ret))
5108 		return 0;
5109 	pos += ret;
5110 
5111 	if (info & BIT(7)) {
5112 		/* Cache Identifier Included */
5113 		if (ie_end - ie < 2)
5114 			return 0;
5115 		ret = os_snprintf(pos, end - pos, "fils_cache_id=%02x%02x\n",
5116 				  ie[0], ie[1]);
5117 		if (os_snprintf_error(end - pos, ret))
5118 			return 0;
5119 		pos += ret;
5120 		ie += 2;
5121 	}
5122 
5123 	if (info & BIT(8)) {
5124 		/* HESSID Included */
5125 		if (ie_end - ie < ETH_ALEN)
5126 			return 0;
5127 		ret = os_snprintf(pos, end - pos, "fils_hessid=" MACSTR "\n",
5128 				  MAC2STR(ie));
5129 		if (os_snprintf_error(end - pos, ret))
5130 			return 0;
5131 		pos += ret;
5132 		ie += ETH_ALEN;
5133 	}
5134 
5135 	realms = (info & (BIT(3) | BIT(4) | BIT(5))) >> 3;
5136 	if (realms) {
5137 		if (ie_end - ie < realms * 2)
5138 			return 0;
5139 		ret = os_snprintf(pos, end - pos, "fils_realms=");
5140 		if (os_snprintf_error(end - pos, ret))
5141 			return 0;
5142 		pos += ret;
5143 
5144 		ret = wpa_snprintf_hex(pos, end - pos, ie, realms * 2);
5145 		if (ret <= 0)
5146 			return 0;
5147 		pos += ret;
5148 		ie += realms * 2;
5149 		ret = os_snprintf(pos, end - pos, "\n");
5150 		if (os_snprintf_error(end - pos, ret))
5151 			return 0;
5152 		pos += ret;
5153 	}
5154 
5155 	return pos - start;
5156 }
5157 #endif /* CONFIG_FILS */
5158 
5159 
print_rnr(const u8 * ie,char * pos,char * end)5160 static int print_rnr(const u8 *ie, char *pos, char *end)
5161 {
5162 	char *start = pos;
5163 	const u8 *ie_end;
5164 	unsigned int n = 0;
5165 	int ret;
5166 
5167 	ie_end = ie + 2 + ie[1];
5168 	ie += 2;
5169 
5170 	while (ie < ie_end) {
5171 		const struct ieee80211_neighbor_ap_info *info =
5172 			(const struct ieee80211_neighbor_ap_info *) ie;
5173 		const u8 *tbtt_start;
5174 		size_t left = ie_end - ie;
5175 		unsigned int count;
5176 		unsigned int i;
5177 
5178 		if (left < sizeof(struct ieee80211_neighbor_ap_info))
5179 			return 0;
5180 
5181 		left -= sizeof(struct ieee80211_neighbor_ap_info);
5182 		count = RNR_TBTT_INFO_COUNT_VAL(info->tbtt_info_hdr) + 1;
5183 		if (left < count * info->tbtt_info_len)
5184 			return 0;
5185 
5186 		ie += sizeof(struct ieee80211_neighbor_ap_info);
5187 		tbtt_start = ie;
5188 
5189 		for (i = 0; i < count; i++) {
5190 			ie = tbtt_start + i * info->tbtt_info_len;
5191 
5192 			ret = os_snprintf(pos, end - pos,
5193 					  "ap_info[%u]: tbtt_info[%u]: hdr=0x%x, len=%u, op_c=%u, channel=%u, ",
5194 					  n, i, info->tbtt_info_hdr,
5195 					  info->tbtt_info_len,
5196 					  info->op_class, info->channel);
5197 			if (os_snprintf_error(end - pos, ret))
5198 				return 0;
5199 			pos += ret;
5200 
5201 			if (info->tbtt_info_len >= 1) {
5202 				ret = os_snprintf(pos, end - pos,
5203 						  "tbtt_offset=%u, ", *ie);
5204 				if (os_snprintf_error(end - pos, ret))
5205 					return 0;
5206 
5207 				ie++;
5208 				pos += ret;
5209 			}
5210 
5211 			if (info->tbtt_info_len >= 7) {
5212 				ret = os_snprintf(pos, end - pos,
5213 						  "bssid=" MACSTR ", ",
5214 						  MAC2STR(ie));
5215 				if (os_snprintf_error(end - pos, ret))
5216 					return 0;
5217 
5218 				ie += ETH_ALEN;
5219 				pos += ret;
5220 			}
5221 
5222 			if (info->tbtt_info_len >= 11) {
5223 				ret = os_snprintf(pos, end - pos,
5224 						  "short SSID=0x%x, ",
5225 						  WPA_GET_LE32(ie));
5226 				if (os_snprintf_error(end - pos, ret))
5227 					return 0;
5228 
5229 				ie += 4;
5230 				pos += ret;
5231 			}
5232 
5233 			if (info->tbtt_info_len >= 12) {
5234 				ret = os_snprintf(pos, end - pos,
5235 						  "bss_params=0x%x, ", *ie);
5236 				if (os_snprintf_error(end - pos, ret))
5237 					return 0;
5238 
5239 				ie++;
5240 				pos += ret;
5241 			}
5242 
5243 			if (info->tbtt_info_len >= 13) {
5244 				ret = os_snprintf(pos, end - pos,
5245 						  "PSD=0x%x, ", *ie);
5246 				if (os_snprintf_error(end - pos, ret))
5247 					return 0;
5248 
5249 				ie++;
5250 				pos += ret;
5251 			}
5252 
5253 			if (info->tbtt_info_len >= 16) {
5254 				ret = os_snprintf(pos, end - pos,
5255 						  "mld ID=%u, link ID=%u",
5256 						  *ie, *(ie + 1) & 0xF);
5257 				if (os_snprintf_error(end - pos, ret))
5258 					return 0;
5259 
5260 				ie += 3;
5261 				pos += ret;
5262 			}
5263 
5264 			ret = os_snprintf(pos, end - pos, "\n");
5265 			if (os_snprintf_error(end - pos, ret))
5266 				return 0;
5267 			pos += ret;
5268 		}
5269 
5270 		ie = tbtt_start + count * info->tbtt_info_len;
5271 
5272 		n++;
5273 	}
5274 
5275 	return pos - start;
5276 }
5277 
5278 
print_ml(struct wpa_bss * bss,char * pos,char * end)5279 static int print_ml(struct wpa_bss *bss, char *pos, char *end)
5280 {
5281 	const struct ieee80211_eht_ml *ml;
5282 	char *start = pos;
5283 	const u8 *ie, *ie_end;
5284 	u16 ml_control;
5285 	u8 common_info_length;
5286 	int ret;
5287 
5288 	ie = get_ml_ie(wpa_bss_ie_ptr(bss), bss->ie_len,
5289 		       MULTI_LINK_CONTROL_TYPE_BASIC);
5290 	if (!ie)
5291 		return 0;
5292 
5293 	ie_end = ie + 2 + ie[1];
5294 	ie += 3;
5295 	ml = (const struct ieee80211_eht_ml *) ie;
5296 
5297 	/* control + common info length + MLD MAC Address */
5298 	if (ie_end - ie < 2 + 1 + ETH_ALEN)
5299 		return 0;
5300 
5301 	ml_control = le_to_host16(ml->ml_control);
5302 
5303 	common_info_length = *(ie + 2);
5304 	ret = os_snprintf(pos, end - pos,
5305 			  "multi-link: control=0x%x, common info len=%u",
5306 			  ml_control, common_info_length);
5307 	if (os_snprintf_error(end - pos, ret))
5308 		return 0;
5309 	pos += ret;
5310 
5311 	ie += 2;
5312 	if (ie_end - ie < common_info_length)
5313 		return 0;
5314 
5315 	ie++;
5316 	common_info_length--;
5317 
5318 	if (common_info_length < ETH_ALEN)
5319 		return 0;
5320 
5321 	ret = os_snprintf(pos, end - pos, ", MLD addr=" MACSTR, MAC2STR(ie));
5322 	if (os_snprintf_error(end - pos, ret))
5323 		return 0;
5324 	pos += ret;
5325 
5326 	ie += ETH_ALEN;
5327 	common_info_length -= ETH_ALEN;
5328 
5329 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_LINK_ID) {
5330 		if (common_info_length < 1)
5331 			return 0;
5332 
5333 		ret = os_snprintf(pos, end - pos, ", link ID=%u", *ie & 0x0f);
5334 		if (os_snprintf_error(end - pos, ret))
5335 			return 0;
5336 		pos += ret;
5337 		ie++;
5338 		common_info_length--;
5339 	}
5340 
5341 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_BSS_PARAM_CH_COUNT) {
5342 		if (common_info_length < 1)
5343 			return 0;
5344 
5345 		ret = os_snprintf(pos, end - pos,
5346 				  ", BSS change parameters=0x%x", *ie);
5347 		if (os_snprintf_error(end - pos, ret))
5348 			return 0;
5349 		pos += ret;
5350 		ie++;
5351 		common_info_length--;
5352 	}
5353 
5354 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_MSD_INFO) {
5355 		if (common_info_length < 2)
5356 			return 0;
5357 
5358 		ret = os_snprintf(pos, end - pos, ", MSD Info=0x%x",
5359 				  WPA_GET_LE16(ie));
5360 		if (os_snprintf_error(end - pos, ret))
5361 			return 0;
5362 		pos += ret;
5363 		ie += 2;
5364 		common_info_length -= 2;
5365 	}
5366 
5367 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_EML_CAPA) {
5368 		if (common_info_length < 2)
5369 			return 0;
5370 
5371 		ret = os_snprintf(pos, end - pos, ", EML capabilities=0x%x",
5372 				  WPA_GET_LE16(ie));
5373 		if (os_snprintf_error(end - pos, ret))
5374 			return 0;
5375 		pos += ret;
5376 		ie += 2;
5377 		common_info_length -= 2;
5378 	}
5379 
5380 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_MLD_CAPA) {
5381 		if (common_info_length < 2)
5382 			return 0;
5383 
5384 		ret = os_snprintf(pos, end - pos, ", MLD capabilities=0x%x",
5385 				  WPA_GET_LE16(ie));
5386 		if (os_snprintf_error(end - pos, ret))
5387 			return 0;
5388 		pos += ret;
5389 		ie += 2;
5390 		common_info_length -= 2;
5391 	}
5392 
5393 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_AP_MLD_ID) {
5394 		if (common_info_length < 1)
5395 			return 0;
5396 
5397 		ret = os_snprintf(pos, end - pos, ", MLD ID=0x%x", *ie);
5398 		if (os_snprintf_error(end - pos, ret))
5399 			return 0;
5400 		pos += ret;
5401 		ie += 1;
5402 		common_info_length--;
5403 	}
5404 
5405 	ret = os_snprintf(pos, end - pos, "\n");
5406 	if (os_snprintf_error(end - pos, ret))
5407 		return 0;
5408 	pos += ret;
5409 
5410 	return pos - start;
5411 }
5412 
5413 
print_bss_info(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,unsigned long mask,char * buf,size_t buflen)5414 static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5415 			  unsigned long mask, char *buf, size_t buflen)
5416 {
5417 	size_t i;
5418 	int ret;
5419 	char *pos, *end;
5420 	const u8 *ie, *ie2, *mesh, *owe, *rsnxe;
5421 
5422 	pos = buf;
5423 	end = buf + buflen;
5424 
5425 	if (mask & WPA_BSS_MASK_ID) {
5426 		ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
5427 		if (os_snprintf_error(end - pos, ret))
5428 			return 0;
5429 		pos += ret;
5430 	}
5431 
5432 	if (mask & WPA_BSS_MASK_BSSID) {
5433 		ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
5434 				  MAC2STR(bss->bssid));
5435 		if (os_snprintf_error(end - pos, ret))
5436 			return 0;
5437 		pos += ret;
5438 	}
5439 
5440 	if (mask & WPA_BSS_MASK_FREQ) {
5441 		ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
5442 		if (os_snprintf_error(end - pos, ret))
5443 			return 0;
5444 		pos += ret;
5445 	}
5446 
5447 	if (mask & WPA_BSS_MASK_BEACON_INT) {
5448 		ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
5449 				  bss->beacon_int);
5450 		if (os_snprintf_error(end - pos, ret))
5451 			return 0;
5452 		pos += ret;
5453 	}
5454 
5455 	if (mask & WPA_BSS_MASK_CAPABILITIES) {
5456 		ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
5457 				  bss->caps);
5458 		if (os_snprintf_error(end - pos, ret))
5459 			return 0;
5460 		pos += ret;
5461 	}
5462 
5463 	if (mask & WPA_BSS_MASK_QUAL) {
5464 		ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
5465 		if (os_snprintf_error(end - pos, ret))
5466 			return 0;
5467 		pos += ret;
5468 	}
5469 
5470 	if (mask & WPA_BSS_MASK_NOISE) {
5471 		ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
5472 		if (os_snprintf_error(end - pos, ret))
5473 			return 0;
5474 		pos += ret;
5475 	}
5476 
5477 	if (mask & WPA_BSS_MASK_LEVEL) {
5478 		ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
5479 		if (os_snprintf_error(end - pos, ret))
5480 			return 0;
5481 		pos += ret;
5482 	}
5483 
5484 	if (mask & WPA_BSS_MASK_TSF) {
5485 		ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
5486 				  (unsigned long long) bss->tsf);
5487 		if (os_snprintf_error(end - pos, ret))
5488 			return 0;
5489 		pos += ret;
5490 	}
5491 
5492 	if (mask & WPA_BSS_MASK_AGE) {
5493 		struct os_reltime now;
5494 
5495 		os_get_reltime(&now);
5496 		ret = os_snprintf(pos, end - pos, "age=%d\n",
5497 				  (int) (now.sec - bss->last_update.sec));
5498 		if (os_snprintf_error(end - pos, ret))
5499 			return 0;
5500 		pos += ret;
5501 	}
5502 
5503 	if (mask & WPA_BSS_MASK_IE) {
5504 		ret = os_snprintf(pos, end - pos, "ie=");
5505 		if (os_snprintf_error(end - pos, ret))
5506 			return 0;
5507 		pos += ret;
5508 
5509 		ie = wpa_bss_ie_ptr(bss);
5510 		for (i = 0; i < bss->ie_len; i++) {
5511 			ret = os_snprintf(pos, end - pos, "%02x", *ie++);
5512 			if (os_snprintf_error(end - pos, ret))
5513 				return 0;
5514 			pos += ret;
5515 		}
5516 
5517 		ret = os_snprintf(pos, end - pos, "\n");
5518 		if (os_snprintf_error(end - pos, ret))
5519 			return 0;
5520 		pos += ret;
5521 	}
5522 
5523 	if (mask & WPA_BSS_MASK_FLAGS) {
5524 		ret = os_snprintf(pos, end - pos, "flags=");
5525 		if (os_snprintf_error(end - pos, ret))
5526 			return 0;
5527 		pos += ret;
5528 
5529 		mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
5530 
5531 		ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
5532 		if (ie)
5533 			pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
5534 						    2 + ie[1]);
5535 		ie2 = wpa_bss_get_rsne(wpa_s, bss, NULL, false);
5536 		if (ie2)
5537 			pos = wpa_supplicant_ie_txt(pos, end,
5538 						    mesh ? "RSN" : "WPA2", ie2,
5539 						    2 + ie2[1]);
5540 		rsnxe = wpa_bss_get_rsnxe(wpa_s, bss, NULL, false);
5541 		if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_H2E)) {
5542 			ret = os_snprintf(pos, end - pos, "[SAE-H2E]");
5543 			if (os_snprintf_error(end - pos, ret))
5544 				return 0;
5545 			pos += ret;
5546 		}
5547 		if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_PK)) {
5548 			ret = os_snprintf(pos, end - pos, "[SAE-PK]");
5549 			if (os_snprintf_error(end - pos, ret))
5550 				return 0;
5551 			pos += ret;
5552 		}
5553 		owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
5554 		if (owe) {
5555 			ret = os_snprintf(
5556 				pos, end - pos,
5557 				ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
5558 			if (os_snprintf_error(end - pos, ret))
5559 				return 0;
5560 			pos += ret;
5561 		}
5562 		pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
5563 		if (!ie && !ie2 &&
5564 		    (bss->caps & IEEE80211_CAP_PRIVACY)) {
5565 			ret = os_snprintf(pos, end - pos, "[WEP]");
5566 			if (os_snprintf_error(end - pos, ret))
5567 				return 0;
5568 			pos += ret;
5569 		}
5570 
5571 		if (mesh) {
5572 			ret = os_snprintf(pos, end - pos, "[MESH]");
5573 			if (os_snprintf_error(end - pos, ret))
5574 				return 0;
5575 			pos += ret;
5576 		}
5577 
5578 		if (bss_is_dmg(bss)) {
5579 			const char *s;
5580 			ret = os_snprintf(pos, end - pos, "[DMG]");
5581 			if (os_snprintf_error(end - pos, ret))
5582 				return 0;
5583 			pos += ret;
5584 			switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
5585 			case IEEE80211_CAP_DMG_IBSS:
5586 				s = "[IBSS]";
5587 				break;
5588 			case IEEE80211_CAP_DMG_AP:
5589 				s = "[ESS]";
5590 				break;
5591 			case IEEE80211_CAP_DMG_PBSS:
5592 				s = "[PBSS]";
5593 				break;
5594 			default:
5595 				s = "";
5596 				break;
5597 			}
5598 			ret = os_snprintf(pos, end - pos, "%s", s);
5599 			if (os_snprintf_error(end - pos, ret))
5600 				return 0;
5601 			pos += ret;
5602 		} else {
5603 			if (bss->caps & IEEE80211_CAP_IBSS) {
5604 				ret = os_snprintf(pos, end - pos, "[IBSS]");
5605 				if (os_snprintf_error(end - pos, ret))
5606 					return 0;
5607 				pos += ret;
5608 			}
5609 			if (bss->caps & IEEE80211_CAP_ESS) {
5610 				ret = os_snprintf(pos, end - pos, "[ESS]");
5611 				if (os_snprintf_error(end - pos, ret))
5612 					return 0;
5613 				pos += ret;
5614 			}
5615 		}
5616 		if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
5617 		    wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
5618 			ret = os_snprintf(pos, end - pos, "[P2P]");
5619 			if (os_snprintf_error(end - pos, ret))
5620 				return 0;
5621 			pos += ret;
5622 		}
5623 #ifdef CONFIG_HS20
5624 		if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
5625 			ret = os_snprintf(pos, end - pos, "[HS20]");
5626 			if (os_snprintf_error(end - pos, ret))
5627 				return 0;
5628 			pos += ret;
5629 		}
5630 #endif /* CONFIG_HS20 */
5631 #ifdef CONFIG_FILS
5632 		if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
5633 			ret = os_snprintf(pos, end - pos, "[FILS]");
5634 			if (os_snprintf_error(end - pos, ret))
5635 				return 0;
5636 			pos += ret;
5637 		}
5638 #endif /* CONFIG_FILS */
5639 #ifdef CONFIG_FST
5640 		if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
5641 			ret = os_snprintf(pos, end - pos, "[FST]");
5642 			if (os_snprintf_error(end - pos, ret))
5643 				return 0;
5644 			pos += ret;
5645 		}
5646 #endif /* CONFIG_FST */
5647 		if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_UTF_8_SSID)) {
5648 			ret = os_snprintf(pos, end - pos, "[UTF-8]");
5649 			if (os_snprintf_error(end - pos, ret))
5650 				return 0;
5651 			pos += ret;
5652 		}
5653 
5654 		ret = os_snprintf(pos, end - pos, "\n");
5655 		if (os_snprintf_error(end - pos, ret))
5656 			return 0;
5657 		pos += ret;
5658 	}
5659 
5660 	if (mask & WPA_BSS_MASK_SSID) {
5661 		ret = os_snprintf(pos, end - pos, "ssid=%s\n",
5662 				  wpa_ssid_txt(bss->ssid, bss->ssid_len));
5663 		if (os_snprintf_error(end - pos, ret))
5664 			return 0;
5665 		pos += ret;
5666 	}
5667 
5668 #ifdef CONFIG_WPS
5669 	if (mask & WPA_BSS_MASK_WPS_SCAN) {
5670 		ie = wpa_bss_ie_ptr(bss);
5671 		ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
5672 		if (ret >= end - pos)
5673 			return 0;
5674 		if (ret > 0)
5675 			pos += ret;
5676 	}
5677 #endif /* CONFIG_WPS */
5678 
5679 #ifdef CONFIG_P2P
5680 	if (mask & WPA_BSS_MASK_P2P_SCAN) {
5681 		ie = wpa_bss_ie_ptr(bss);
5682 		ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
5683 		if (ret >= end - pos)
5684 			return 0;
5685 		if (ret > 0)
5686 			pos += ret;
5687 	}
5688 #endif /* CONFIG_P2P */
5689 
5690 #ifdef CONFIG_WIFI_DISPLAY
5691 	if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
5692 		struct wpabuf *wfd;
5693 
5694 		ie = wpa_bss_ie_ptr(bss);
5695 		wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
5696 						  WFD_IE_VENDOR_TYPE);
5697 		if (wfd) {
5698 			ret = os_snprintf(pos, end - pos, "wfd_subelems=");
5699 			if (os_snprintf_error(end - pos, ret)) {
5700 				wpabuf_free(wfd);
5701 				return 0;
5702 			}
5703 			pos += ret;
5704 
5705 			pos += wpa_snprintf_hex(pos, end - pos,
5706 						wpabuf_head(wfd),
5707 						wpabuf_len(wfd));
5708 			wpabuf_free(wfd);
5709 
5710 			ret = os_snprintf(pos, end - pos, "\n");
5711 			if (os_snprintf_error(end - pos, ret))
5712 				return 0;
5713 			pos += ret;
5714 		}
5715 	}
5716 #endif /* CONFIG_WIFI_DISPLAY */
5717 
5718 #ifdef CONFIG_INTERWORKING
5719 	if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
5720 		struct wpa_bss_anqp *anqp = bss->anqp;
5721 		struct wpa_bss_anqp_elem *elem;
5722 
5723 		pos = anqp_add_hex(pos, end, "anqp_capability_list",
5724 				   anqp->capability_list);
5725 		pos = anqp_add_hex(pos, end, "anqp_venue_name",
5726 				   anqp->venue_name);
5727 		pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
5728 				   anqp->network_auth_type);
5729 		pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
5730 				   anqp->roaming_consortium);
5731 		pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
5732 				   anqp->ip_addr_type_availability);
5733 		pos = anqp_add_hex(pos, end, "anqp_nai_realm",
5734 				   anqp->nai_realm);
5735 		pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
5736 		pos = anqp_add_hex(pos, end, "anqp_domain_name",
5737 				   anqp->domain_name);
5738 		pos = anqp_add_hex(pos, end, "anqp_fils_realm_info",
5739 				   anqp->fils_realm_info);
5740 #ifdef CONFIG_HS20
5741 		pos = anqp_add_hex(pos, end, "hs20_capability_list",
5742 				   anqp->hs20_capability_list);
5743 		pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
5744 				   anqp->hs20_operator_friendly_name);
5745 		pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
5746 				   anqp->hs20_wan_metrics);
5747 		pos = anqp_add_hex(pos, end, "hs20_connection_capability",
5748 				   anqp->hs20_connection_capability);
5749 		pos = anqp_add_hex(pos, end, "hs20_operating_class",
5750 				   anqp->hs20_operating_class);
5751 #endif /* CONFIG_HS20 */
5752 
5753 		dl_list_for_each(elem, &anqp->anqp_elems,
5754 				 struct wpa_bss_anqp_elem, list) {
5755 			char title[20];
5756 
5757 			os_snprintf(title, sizeof(title), "anqp[%u]",
5758 				    elem->infoid);
5759 			pos = anqp_add_hex(pos, end, title, elem->payload);
5760 			if (elem->protected_response) {
5761 				ret = os_snprintf(pos, end - pos,
5762 						  "protected-anqp-info[%u]=1\n",
5763 						  elem->infoid);
5764 				if (os_snprintf_error(end - pos, ret))
5765 					return 0;
5766 				pos += ret;
5767 			}
5768 		}
5769 	}
5770 #endif /* CONFIG_INTERWORKING */
5771 
5772 #ifdef CONFIG_MESH
5773 	if (mask & WPA_BSS_MASK_MESH_SCAN) {
5774 		ie = wpa_bss_ie_ptr(bss);
5775 		ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
5776 		if (ret >= end - pos)
5777 			return 0;
5778 		if (ret > 0)
5779 			pos += ret;
5780 	}
5781 #endif /* CONFIG_MESH */
5782 
5783 	if (mask & WPA_BSS_MASK_SNR) {
5784 		ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
5785 		if (os_snprintf_error(end - pos, ret))
5786 			return 0;
5787 		pos += ret;
5788 	}
5789 
5790 	if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
5791 		ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
5792 				  bss->est_throughput);
5793 		if (os_snprintf_error(end - pos, ret))
5794 			return 0;
5795 		pos += ret;
5796 	}
5797 
5798 #ifdef CONFIG_FST
5799 	if (mask & WPA_BSS_MASK_FST) {
5800 		ret = fst_ctrl_iface_mb_info(bss->bssid, pos, end - pos);
5801 		if (ret < 0 || ret >= end - pos)
5802 			return 0;
5803 		pos += ret;
5804 	}
5805 #endif /* CONFIG_FST */
5806 
5807 	if (mask & WPA_BSS_MASK_UPDATE_IDX) {
5808 		ret = os_snprintf(pos, end - pos, "update_idx=%u\n",
5809 				  bss->last_update_idx);
5810 		if (os_snprintf_error(end - pos, ret))
5811 			return 0;
5812 		pos += ret;
5813 	}
5814 
5815 	if ((mask & WPA_BSS_MASK_BEACON_IE) && bss->beacon_ie_len) {
5816 		ret = os_snprintf(pos, end - pos, "beacon_ie=");
5817 		if (os_snprintf_error(end - pos, ret))
5818 			return 0;
5819 		pos += ret;
5820 
5821 		ie = wpa_bss_ie_ptr(bss);
5822 		ie += bss->ie_len;
5823 		for (i = 0; i < bss->beacon_ie_len; i++) {
5824 			ret = os_snprintf(pos, end - pos, "%02x", *ie++);
5825 			if (os_snprintf_error(end - pos, ret))
5826 				return 0;
5827 			pos += ret;
5828 		}
5829 
5830 		ret = os_snprintf(pos, end - pos, "\n");
5831 		if (os_snprintf_error(end - pos, ret))
5832 			return 0;
5833 		pos += ret;
5834 	}
5835 
5836 #ifdef CONFIG_FILS
5837 	if (mask & WPA_BSS_MASK_FILS_INDICATION) {
5838 		ret = print_fils_indication(bss, pos, end);
5839 		pos += ret;
5840 	}
5841 #endif /* CONFIG_FILS */
5842 
5843 	if (!is_zero_ether_addr(bss->mld_addr) &&
5844 	    (mask & WPA_BSS_MASK_AP_MLD_ADDR)) {
5845 		ret = os_snprintf(pos, end - pos,
5846 				  "ap_mld_addr=" MACSTR "\n",
5847 				  MAC2STR(bss->mld_addr));
5848 		if (os_snprintf_error(end - pos, ret))
5849 			return 0;
5850 		pos += ret;
5851 	}
5852 
5853 	if (mask & WPA_BSS_MASK_RNR) {
5854 		size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
5855 		const struct element *elem;
5856 
5857 		for_each_element_id(elem, WLAN_EID_REDUCED_NEIGHBOR_REPORT,
5858 				    wpa_bss_ie_ptr(bss), ies_len)
5859 			pos += print_rnr((const u8 *) elem, pos, end);
5860 	}
5861 
5862 	if (mask & WPA_BSS_MASK_ML)
5863 		pos += print_ml(bss, pos, end);
5864 
5865 	if (mask & WPA_BSS_MASK_DELIM) {
5866 		ret = os_snprintf(pos, end - pos, "====\n");
5867 		if (os_snprintf_error(end - pos, ret))
5868 			return 0;
5869 		pos += ret;
5870 	}
5871 
5872 	return pos - buf;
5873 }
5874 
5875 
wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)5876 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
5877 					 const char *cmd, char *buf,
5878 					 size_t buflen)
5879 {
5880 	u8 bssid[ETH_ALEN];
5881 	size_t i;
5882 	struct wpa_bss *bss;
5883 	struct wpa_bss *bsslast = NULL;
5884 	struct dl_list *next;
5885 	int ret = 0;
5886 	int len;
5887 	char *ctmp, *end = buf + buflen;
5888 	unsigned long mask = WPA_BSS_MASK_ALL;
5889 
5890 	if (os_strncmp(cmd, "RANGE=", 6) == 0) {
5891 		if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
5892 			bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
5893 					    list_id);
5894 			bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
5895 					       list_id);
5896 		} else { /* N1-N2 */
5897 			unsigned int id1, id2;
5898 
5899 			if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
5900 				wpa_printf(MSG_INFO, "Wrong BSS range "
5901 					   "format");
5902 				return 0;
5903 			}
5904 
5905 			if (*(cmd + 6) == '-')
5906 				id1 = 0;
5907 			else
5908 				id1 = atoi(cmd + 6);
5909 			ctmp++;
5910 			if (*ctmp >= '0' && *ctmp <= '9')
5911 				id2 = atoi(ctmp);
5912 			else
5913 				id2 = (unsigned int) -1;
5914 			bss = wpa_bss_get_id_range(wpa_s, id1, id2);
5915 			if (id2 == (unsigned int) -1)
5916 				bsslast = dl_list_last(&wpa_s->bss_id,
5917 						       struct wpa_bss,
5918 						       list_id);
5919 			else {
5920 				bsslast = wpa_bss_get_id(wpa_s, id2);
5921 				if (bsslast == NULL && bss && id2 > id1) {
5922 					struct wpa_bss *tmp = bss;
5923 					for (;;) {
5924 						next = tmp->list_id.next;
5925 						if (next == &wpa_s->bss_id)
5926 							break;
5927 						tmp = dl_list_entry(
5928 							next, struct wpa_bss,
5929 							list_id);
5930 						if (tmp->id > id2)
5931 							break;
5932 						bsslast = tmp;
5933 					}
5934 				}
5935 			}
5936 		}
5937 	} else if (os_strncmp(cmd, "FIRST", 5) == 0)
5938 		bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
5939 	else if (os_strncmp(cmd, "LAST", 4) == 0)
5940 		bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
5941 	else if (os_strncmp(cmd, "ID-", 3) == 0) {
5942 		i = atoi(cmd + 3);
5943 		bss = wpa_bss_get_id(wpa_s, i);
5944 	} else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
5945 		i = atoi(cmd + 5);
5946 		bss = wpa_bss_get_id(wpa_s, i);
5947 		if (bss) {
5948 			next = bss->list_id.next;
5949 			if (next == &wpa_s->bss_id)
5950 				bss = NULL;
5951 			else
5952 				bss = dl_list_entry(next, struct wpa_bss,
5953 						    list_id);
5954 		}
5955 	} else if (os_strncmp(cmd, "CURRENT", 7) == 0) {
5956 		bss = wpa_s->current_bss;
5957 #ifdef CONFIG_P2P
5958 	} else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
5959 		if (hwaddr_aton(cmd + 13, bssid) == 0)
5960 			bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
5961 		else
5962 			bss = NULL;
5963 #endif /* CONFIG_P2P */
5964 	} else if (hwaddr_aton(cmd, bssid) == 0)
5965 		bss = wpa_bss_get_bssid(wpa_s, bssid);
5966 	else {
5967 		struct wpa_bss *tmp;
5968 		i = atoi(cmd);
5969 		bss = NULL;
5970 		dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
5971 		{
5972 			if (i == 0) {
5973 				bss = tmp;
5974 				break;
5975 			}
5976 			i--;
5977 		}
5978 	}
5979 
5980 	if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
5981 		mask = strtoul(ctmp + 5, NULL, 0x10);
5982 		if (mask == 0)
5983 			mask = WPA_BSS_MASK_ALL;
5984 	}
5985 
5986 	if (bss == NULL)
5987 		return 0;
5988 
5989 	if (bsslast == NULL)
5990 		bsslast = bss;
5991 	do {
5992 		len = print_bss_info(wpa_s, bss, mask, buf, buflen);
5993 		ret += len;
5994 		buf += len;
5995 		buflen -= len;
5996 		if (bss == bsslast) {
5997 			if ((mask & WPA_BSS_MASK_DELIM) && len &&
5998 			    (bss == dl_list_last(&wpa_s->bss_id,
5999 						 struct wpa_bss, list_id))) {
6000 				int res;
6001 
6002 				res = os_snprintf(buf - 5, end - buf + 5,
6003 						  "####\n");
6004 				if (os_snprintf_error(end - buf + 5, res)) {
6005 					wpa_printf(MSG_DEBUG,
6006 						   "Could not add end delim");
6007 				}
6008 			}
6009 			break;
6010 		}
6011 		next = bss->list_id.next;
6012 		if (next == &wpa_s->bss_id)
6013 			break;
6014 		bss = dl_list_entry(next, struct wpa_bss, list_id);
6015 	} while (bss && len);
6016 
6017 	return ret;
6018 }
6019 
6020 
wpa_supplicant_ctrl_iface_ap_scan(struct wpa_supplicant * wpa_s,char * cmd)6021 static int wpa_supplicant_ctrl_iface_ap_scan(
6022 	struct wpa_supplicant *wpa_s, char *cmd)
6023 {
6024 	int ap_scan = atoi(cmd);
6025 	return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
6026 }
6027 
6028 
wpa_supplicant_ctrl_iface_scan_interval(struct wpa_supplicant * wpa_s,char * cmd)6029 static int wpa_supplicant_ctrl_iface_scan_interval(
6030 	struct wpa_supplicant *wpa_s, char *cmd)
6031 {
6032 	int scan_int = atoi(cmd);
6033 	return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
6034 }
6035 
6036 
wpa_supplicant_ctrl_iface_bss_expire_age(struct wpa_supplicant * wpa_s,char * cmd)6037 static int wpa_supplicant_ctrl_iface_bss_expire_age(
6038 	struct wpa_supplicant *wpa_s, char *cmd)
6039 {
6040 	int expire_age = atoi(cmd);
6041 	return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
6042 }
6043 
6044 
wpa_supplicant_ctrl_iface_bss_expire_count(struct wpa_supplicant * wpa_s,char * cmd)6045 static int wpa_supplicant_ctrl_iface_bss_expire_count(
6046 	struct wpa_supplicant *wpa_s, char *cmd)
6047 {
6048 	int expire_count = atoi(cmd);
6049 	return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
6050 }
6051 
6052 
wpa_supplicant_ctrl_iface_bss_flush(struct wpa_supplicant * wpa_s,char * cmd)6053 static void wpa_supplicant_ctrl_iface_bss_flush(
6054 	struct wpa_supplicant *wpa_s, char *cmd)
6055 {
6056 	int flush_age = atoi(cmd);
6057 
6058 	if (flush_age == 0)
6059 		wpa_bss_flush(wpa_s);
6060 	else
6061 		wpa_bss_flush_by_age(wpa_s, flush_age);
6062 }
6063 
6064 
6065 #ifdef CONFIG_TESTING_OPTIONS
wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant * wpa_s)6066 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
6067 {
6068 	wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
6069 	/* MLME-DELETEKEYS.request */
6070 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL,
6071 			0, KEY_FLAG_GROUP);
6072 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL,
6073 			0, KEY_FLAG_GROUP);
6074 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL,
6075 			0, KEY_FLAG_GROUP);
6076 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL,
6077 			0, KEY_FLAG_GROUP);
6078 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL,
6079 			0, KEY_FLAG_GROUP);
6080 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL,
6081 			0, KEY_FLAG_GROUP);
6082 
6083 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0,
6084 			NULL, 0, KEY_FLAG_PAIRWISE);
6085 	if (wpa_sm_ext_key_id(wpa_s->wpa))
6086 		wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, wpa_s->bssid, 1, 0,
6087 				NULL, 0, NULL, 0, KEY_FLAG_PAIRWISE);
6088 	/* MLME-SETPROTECTION.request(None) */
6089 	wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
6090 				   MLME_SETPROTECTION_PROTECT_TYPE_NONE,
6091 				   MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
6092 	wpa_sm_drop_sa(wpa_s->wpa);
6093 }
6094 #endif /* CONFIG_TESTING_OPTIONS */
6095 
6096 
wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant * wpa_s,char * addr)6097 static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
6098 					  char *addr)
6099 {
6100 #ifdef CONFIG_NO_SCAN_PROCESSING
6101 	return -1;
6102 #else /* CONFIG_NO_SCAN_PROCESSING */
6103 	u8 bssid[ETH_ALEN];
6104 	struct wpa_bss *bss;
6105 	struct wpa_ssid *ssid = wpa_s->current_ssid;
6106 	struct wpa_radio_work *already_connecting;
6107 
6108 	if (hwaddr_aton(addr, bssid)) {
6109 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
6110 			   "address '%s'", addr);
6111 		return -1;
6112 	}
6113 
6114 	wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
6115 
6116 	if (!ssid) {
6117 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
6118 			   "configuration known for the target AP");
6119 		return -1;
6120 	}
6121 
6122 	bss = wpa_bss_get_connection(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
6123 	if (!bss) {
6124 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
6125 			   "from BSS table");
6126 		return -1;
6127 	}
6128 
6129 	/*
6130 	 * TODO: Find best network configuration block from configuration to
6131 	 * allow roaming to other networks
6132 	 */
6133 
6134 	already_connecting = radio_work_pending(wpa_s, "sme-connect");
6135 	wpa_s->reassociate = 1;
6136 	wpa_supplicant_connect(wpa_s, bss, ssid);
6137 
6138 	/*
6139 	 * Indicate that an explicitly requested roam is in progress so scan
6140 	 * results that come in before the 'sme-connect' radio work gets
6141 	 * executed do not override the original connection attempt.
6142 	 */
6143 	if (!already_connecting && radio_work_pending(wpa_s, "sme-connect"))
6144 		wpa_s->roam_in_progress = true;
6145 
6146 	return 0;
6147 #endif /* CONFIG_NO_SCAN_PROCESSING */
6148 }
6149 
6150 
6151 #ifdef CONFIG_P2P
p2p_ctrl_find(struct wpa_supplicant * wpa_s,char * cmd)6152 static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
6153 {
6154 	unsigned int timeout = atoi(cmd);
6155 	enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
6156 	u8 dev_id[ETH_ALEN], *_dev_id = NULL;
6157 	u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
6158 	char *pos;
6159 	unsigned int search_delay;
6160 	const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
6161 	u8 seek_count = 0;
6162 	int freq = 0;
6163 	bool include_6ghz = false;
6164 
6165 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6166 		wpa_dbg(wpa_s, MSG_INFO,
6167 			"Reject P2P_FIND since interface is disabled");
6168 		return -1;
6169 	}
6170 
6171 	if (os_strstr(cmd, " include_6ghz"))
6172 		include_6ghz = true;
6173 	if (os_strstr(cmd, "type=social"))
6174 		type = P2P_FIND_ONLY_SOCIAL;
6175 	else if (os_strstr(cmd, "type=progressive"))
6176 		type = P2P_FIND_PROGRESSIVE;
6177 
6178 	pos = os_strstr(cmd, "dev_id=");
6179 	if (pos) {
6180 		pos += 7;
6181 		if (hwaddr_aton(pos, dev_id))
6182 			return -1;
6183 		_dev_id = dev_id;
6184 	}
6185 
6186 	pos = os_strstr(cmd, "dev_type=");
6187 	if (pos) {
6188 		pos += 9;
6189 		if (wps_dev_type_str2bin(pos, dev_type) < 0)
6190 			return -1;
6191 		_dev_type = dev_type;
6192 	}
6193 
6194 	pos = os_strstr(cmd, "delay=");
6195 	if (pos) {
6196 		pos += 6;
6197 		search_delay = atoi(pos);
6198 	} else
6199 		search_delay = wpas_p2p_search_delay(wpa_s);
6200 
6201 	pos = os_strstr(cmd, "freq=");
6202 	if (pos) {
6203 		pos += 5;
6204 		freq = atoi(pos);
6205 		if (freq <= 0)
6206 			return -1;
6207 	}
6208 
6209 	/* Must be searched for last, because it adds nul termination */
6210 	pos = os_strstr(cmd, " seek=");
6211 	if (pos)
6212 		pos += 6;
6213 	while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
6214 		char *term;
6215 
6216 		_seek[seek_count++] = pos;
6217 		seek = _seek;
6218 		term = os_strchr(pos, ' ');
6219 		if (!term)
6220 			break;
6221 		*term = '\0';
6222 		pos = os_strstr(term + 1, "seek=");
6223 		if (pos)
6224 			pos += 5;
6225 	}
6226 	if (seek_count > P2P_MAX_QUERY_HASH) {
6227 		seek[0] = NULL;
6228 		seek_count = 1;
6229 	}
6230 
6231 	return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
6232 			     _dev_id, search_delay, seek_count, seek, freq,
6233 			     include_6ghz);
6234 }
6235 
6236 
p2ps_ctrl_parse_cpt_priority(const char * pos,u8 * cpt)6237 static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
6238 {
6239 	const char *last = NULL;
6240 	const char *token;
6241 	long int token_len;
6242 	unsigned int i;
6243 
6244 	/* Expected predefined CPT names delimited by ':' */
6245 	for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
6246 		if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
6247 			wpa_printf(MSG_ERROR,
6248 				   "P2PS: CPT name list is too long, expected up to %d names",
6249 				   P2PS_FEATURE_CAPAB_CPT_MAX);
6250 			cpt[0] = 0;
6251 			return -1;
6252 		}
6253 
6254 		token_len = last - token;
6255 
6256 		if (token_len  == 3 &&
6257 		    os_memcmp(token, "UDP", token_len) == 0) {
6258 			cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
6259 		} else if (token_len == 3 &&
6260 			   os_memcmp(token, "MAC", token_len) == 0) {
6261 			cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
6262 		} else {
6263 			wpa_printf(MSG_ERROR,
6264 				   "P2PS: Unsupported CPT name '%s'", token);
6265 			cpt[0] = 0;
6266 			return -1;
6267 		}
6268 
6269 		if (isblank((unsigned char) *last)) {
6270 			i++;
6271 			break;
6272 		}
6273 	}
6274 	cpt[i] = 0;
6275 	return 0;
6276 }
6277 
6278 
p2p_parse_asp_provision_cmd(const char * cmd)6279 static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
6280 {
6281 	struct p2ps_provision *p2ps_prov;
6282 	char *pos;
6283 	size_t info_len = 0;
6284 	char *info = NULL;
6285 	u8 role = P2PS_SETUP_NONE;
6286 	long long unsigned val;
6287 	int i;
6288 
6289 	pos = os_strstr(cmd, "info=");
6290 	if (pos) {
6291 		pos += 5;
6292 		info_len = os_strlen(pos);
6293 
6294 		if (info_len) {
6295 			info = os_malloc(info_len + 1);
6296 			if (info) {
6297 				info_len = utf8_unescape(pos, info_len,
6298 							 info, info_len + 1);
6299 			} else
6300 				info_len = 0;
6301 		}
6302 	}
6303 
6304 	p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
6305 	if (p2ps_prov == NULL) {
6306 		os_free(info);
6307 		return NULL;
6308 	}
6309 
6310 	if (info) {
6311 		os_memcpy(p2ps_prov->info, info, info_len);
6312 		p2ps_prov->info[info_len] = '\0';
6313 		os_free(info);
6314 	}
6315 
6316 	pos = os_strstr(cmd, "status=");
6317 	if (pos)
6318 		p2ps_prov->status = atoi(pos + 7);
6319 	else
6320 		p2ps_prov->status = -1;
6321 
6322 	pos = os_strstr(cmd, "adv_id=");
6323 	if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
6324 		goto invalid_args;
6325 	p2ps_prov->adv_id = val;
6326 
6327 	pos = os_strstr(cmd, "method=");
6328 	if (pos)
6329 		p2ps_prov->method = strtol(pos + 7, NULL, 16);
6330 	else
6331 		p2ps_prov->method = 0;
6332 
6333 	pos = os_strstr(cmd, "session=");
6334 	if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
6335 		goto invalid_args;
6336 	p2ps_prov->session_id = val;
6337 
6338 	pos = os_strstr(cmd, "adv_mac=");
6339 	if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
6340 		goto invalid_args;
6341 
6342 	pos = os_strstr(cmd, "session_mac=");
6343 	if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
6344 		goto invalid_args;
6345 
6346 	pos = os_strstr(cmd, "cpt=");
6347 	if (pos) {
6348 		if (p2ps_ctrl_parse_cpt_priority(pos + 4,
6349 						 p2ps_prov->cpt_priority))
6350 			goto invalid_args;
6351 	} else {
6352 		p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
6353 	}
6354 
6355 	for (i = 0; p2ps_prov->cpt_priority[i]; i++)
6356 		p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
6357 
6358 	/* force conncap with tstCap (no validity checks) */
6359 	pos = os_strstr(cmd, "tstCap=");
6360 	if (pos) {
6361 		role = strtol(pos + 7, NULL, 16);
6362 	} else {
6363 		pos = os_strstr(cmd, "role=");
6364 		if (pos) {
6365 			role = strtol(pos + 5, NULL, 16);
6366 			if (role != P2PS_SETUP_CLIENT &&
6367 			    role != P2PS_SETUP_GROUP_OWNER)
6368 				role = P2PS_SETUP_NONE;
6369 		}
6370 	}
6371 	p2ps_prov->role = role;
6372 
6373 	return p2ps_prov;
6374 
6375 invalid_args:
6376 	os_free(p2ps_prov);
6377 	return NULL;
6378 }
6379 
6380 
p2p_ctrl_asp_provision_resp(struct wpa_supplicant * wpa_s,char * cmd)6381 static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
6382 {
6383 	u8 addr[ETH_ALEN];
6384 	struct p2ps_provision *p2ps_prov;
6385 	char *pos;
6386 
6387 	/* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
6388 
6389 	wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
6390 
6391 	if (hwaddr_aton(cmd, addr))
6392 		return -1;
6393 
6394 	pos = cmd + 17;
6395 	if (*pos != ' ')
6396 		return -1;
6397 
6398 	p2ps_prov = p2p_parse_asp_provision_cmd(pos);
6399 	if (!p2ps_prov)
6400 		return -1;
6401 
6402 	if (p2ps_prov->status < 0) {
6403 		os_free(p2ps_prov);
6404 		return -1;
6405 	}
6406 
6407 	return wpas_p2p_prov_disc(wpa_s, addr, NULL, 0, WPAS_P2P_PD_FOR_ASP,
6408 				  p2ps_prov);
6409 }
6410 
6411 
p2p_ctrl_asp_provision(struct wpa_supplicant * wpa_s,char * cmd)6412 static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
6413 {
6414 	u8 addr[ETH_ALEN];
6415 	struct p2ps_provision *p2ps_prov;
6416 	char *pos;
6417 
6418 	/* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
6419 	 *        session=<ses_id> mac=<ses_mac> [info=<infodata>]
6420 	 */
6421 
6422 	wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
6423 	if (hwaddr_aton(cmd, addr))
6424 		return -1;
6425 
6426 	pos = cmd + 17;
6427 	if (*pos != ' ')
6428 		return -1;
6429 
6430 	p2ps_prov = p2p_parse_asp_provision_cmd(pos);
6431 	if (!p2ps_prov)
6432 		return -1;
6433 
6434 	p2ps_prov->pd_seeker = 1;
6435 
6436 	return wpas_p2p_prov_disc(wpa_s, addr, NULL, 0, WPAS_P2P_PD_FOR_ASP,
6437 				  p2ps_prov);
6438 }
6439 
6440 
bootstrap_pwd_required(u16 bootstrap)6441 static bool bootstrap_pwd_required(u16 bootstrap)
6442 {
6443 	switch (bootstrap) {
6444 	case P2P_PBMA_OPPORTUNISTIC:
6445 		return false;
6446 	case P2P_PBMA_PIN_CODE_DISPLAY:
6447 	case P2P_PBMA_PASSPHRASE_DISPLAY:
6448 	case P2P_PBMA_QR_DISPLAY:
6449 	case P2P_PBMA_NFC_TAG:
6450 	case P2P_PBMA_PIN_CODE_KEYPAD:
6451 	case P2P_PBMA_PASSPHRASE_KEYPAD:
6452 	case P2P_PBMA_QR_SCAN:
6453 	case P2P_PBMA_NFC_READER:
6454 		return true;
6455 	case P2P_PBMA_SERVICE_MANAGED:
6456 	case P2P_PBMA_HANDSHAKE_SKIP:
6457 		return false;
6458 	}
6459 
6460 	return false;
6461 }
6462 
6463 
p2p_ctrl_connect(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)6464 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
6465 			    char *buf, size_t buflen)
6466 {
6467 	u8 addr[ETH_ALEN];
6468 	char *pos, *pos2;
6469 	char *pin = NULL;
6470 	enum p2p_wps_method wps_method;
6471 	int new_pin;
6472 	int ret;
6473 	int persistent_group, persistent_id = -1;
6474 	int join;
6475 	int auth;
6476 	int automatic;
6477 	int go_intent = -1;
6478 	int freq = 0;
6479 	int pd;
6480 	int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
6481 	int edmg;
6482 	u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
6483 	size_t group_ssid_len = 0;
6484 	int he;
6485 	bool allow_6ghz;
6486 	bool p2p2, skip_prov;
6487 	u16 bootstrap = 0;
6488 	const char *password = NULL;
6489 	char *token, *context = NULL;
6490 
6491 	if (!wpa_s->global->p2p_init_wpa_s)
6492 		return -1;
6493 	if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
6494 		wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
6495 			wpa_s->global->p2p_init_wpa_s->ifname);
6496 		wpa_s = wpa_s->global->p2p_init_wpa_s;
6497 	}
6498 
6499 	/* <addr> <"pbc" | "pin" | "pair" | PIN> [label|display|keypad|p2ps]
6500 	 * [persistent|persistent=<network id>]
6501 	 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
6502 	 * [ht40] [vht] [he] [edmg] [auto] [ssid=<hexdump>]
6503 	 * [p2p2] [skip_prov] [bstrapmethod=<value>] [password=<string>]
6504 	 */
6505 
6506 	if (hwaddr_aton(cmd, addr))
6507 		return -1;
6508 
6509 	pos = cmd + 17;
6510 	if (*pos != ' ')
6511 		return -1;
6512 	pos++;
6513 
6514 	persistent_group = os_strstr(pos, " persistent") != NULL;
6515 	pos2 = os_strstr(pos, " persistent=");
6516 	if (pos2) {
6517 		struct wpa_ssid *ssid;
6518 		persistent_id = atoi(pos2 + 12);
6519 		ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6520 		if (ssid == NULL || ssid->disabled != 2 ||
6521 		    ssid->mode != WPAS_MODE_P2P_GO) {
6522 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
6523 				   "SSID id=%d for persistent P2P group (GO)",
6524 				   persistent_id);
6525 			return -1;
6526 		}
6527 	}
6528 	join = os_strstr(pos, " join") != NULL;
6529 	allow_6ghz = os_strstr(pos, " allow_6ghz") != NULL;
6530 	auth = os_strstr(pos, " auth") != NULL;
6531 	automatic = os_strstr(pos, " auto") != NULL;
6532 	pd = os_strstr(pos, " provdisc") != NULL;
6533 	vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
6534 	ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
6535 		vht;
6536 	he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
6537 	edmg = (os_strstr(cmd, " edmg") != NULL) || wpa_s->conf->p2p_go_edmg;
6538 	p2p2 = os_strstr(pos, " p2p2") != NULL;
6539 	skip_prov = os_strstr(pos, " skip_prov") != NULL;
6540 
6541 	pos2 = os_strstr(pos, " go_intent=");
6542 	if (pos2) {
6543 		pos2 += 11;
6544 		go_intent = atoi(pos2);
6545 		if (go_intent < 0 || go_intent > 15)
6546 			return -1;
6547 	}
6548 
6549 	pos2 = os_strstr(pos, " freq=");
6550 	if (pos2) {
6551 		pos2 += 6;
6552 		freq = atoi(pos2);
6553 		if (freq <= 0)
6554 			return -1;
6555 	}
6556 
6557 	pos2 = os_strstr(pos, " freq2=");
6558 	if (pos2)
6559 		freq2 = atoi(pos2 + 7);
6560 
6561 	pos2 = os_strstr(pos, " max_oper_chwidth=");
6562 	if (pos2)
6563 		chwidth = atoi(pos2 + 18);
6564 
6565 	max_oper_chwidth = chwidth_freq2_to_ch_width(chwidth, freq2);
6566 	if (max_oper_chwidth < 0)
6567 		return -1;
6568 
6569 	if (allow_6ghz && chwidth == 40)
6570 		max_oper_chwidth = CONF_OPER_CHWIDTH_40MHZ_6GHZ;
6571 
6572 	pos2 = os_strstr(pos, " ssid=");
6573 	if (pos2) {
6574 		char *end;
6575 
6576 		pos2 += 6;
6577 		end = os_strchr(pos2, ' ');
6578 		if (!end)
6579 			group_ssid_len = os_strlen(pos2) / 2;
6580 		else
6581 			group_ssid_len = (end - pos2) / 2;
6582 		if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
6583 		    hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
6584 			return -1;
6585 		group_ssid = _group_ssid;
6586 	}
6587 
6588 	if (os_strncmp(pos, "pin", 3) == 0) {
6589 		/* Request random PIN (to be displayed) and enable the PIN */
6590 		wps_method = WPS_PIN_DISPLAY;
6591 	} else if (os_strncmp(pos, "pbc", 3) == 0) {
6592 		wps_method = WPS_PBC;
6593 	} else if (os_strstr(pos, "p2ps") != NULL) {
6594 		wps_method = WPS_P2PS;
6595 	} else if (os_strncmp(pos, "pair", 4) == 0 && p2p2) {
6596 		wps_method = WPS_NOT_READY;
6597 	} else {
6598 		pin = pos;
6599 		pos = os_strchr(pin, ' ');
6600 		wps_method = WPS_PIN_KEYPAD;
6601 		if (pos) {
6602 			*pos++ = '\0';
6603 			if (os_strncmp(pos, "display", 7) == 0)
6604 				wps_method = WPS_PIN_DISPLAY;
6605 		}
6606 		if (!wps_pin_str_valid(pin)) {
6607 			os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
6608 			return 17;
6609 		}
6610 	}
6611 
6612 	pos2 = os_strstr(pos, "bstrapmethod=");
6613 	if (pos2) {
6614 		pos2 += 13;
6615 		bootstrap = atoi(pos2);
6616 	}
6617 
6618 	while ((token = str_token(pos, " ", &context))) {
6619 		if (os_strncmp(token, "password=", 9) == 0) {
6620 			password = token + 9;
6621 			continue;
6622 		}
6623 	}
6624 
6625 	if (bootstrap_pwd_required(bootstrap) && !password) {
6626 		wpa_printf(MSG_DEBUG,
6627 			   "CTRL_IFACE: This P2P2 bootstrap method requires password");
6628 		return -1;
6629 	}
6630 
6631 	new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
6632 				   persistent_group, automatic, join,
6633 				   auth, go_intent, freq, freq2, persistent_id,
6634 				   pd, ht40, vht, max_oper_chwidth, he, edmg,
6635 				   group_ssid, group_ssid_len, allow_6ghz, p2p2,
6636 				   bootstrap, password, skip_prov);
6637 	if (new_pin == -2) {
6638 		os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
6639 		return 25;
6640 	}
6641 	if (new_pin == -3) {
6642 		os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
6643 		return 25;
6644 	}
6645 	if (new_pin < 0)
6646 		return -1;
6647 	if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
6648 		ret = os_snprintf(buf, buflen, "%08d", new_pin);
6649 		if (os_snprintf_error(buflen, ret))
6650 			return -1;
6651 		return ret;
6652 	}
6653 
6654 	os_memcpy(buf, "OK\n", 3);
6655 	return 3;
6656 }
6657 
6658 
p2p_ctrl_listen(struct wpa_supplicant * wpa_s,char * cmd)6659 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
6660 {
6661 	unsigned int timeout = atoi(cmd);
6662 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6663 		wpa_dbg(wpa_s, MSG_INFO,
6664 			"Reject P2P_LISTEN since interface is disabled");
6665 		return -1;
6666 	}
6667 	return wpas_p2p_listen(wpa_s, timeout);
6668 }
6669 
6670 
p2p_ctrl_prov_disc(struct wpa_supplicant * wpa_s,char * cmd)6671 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
6672 {
6673 	u8 addr[ETH_ALEN];
6674 	char *pos, *pos2;
6675 	enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
6676 	u16 bootstrap = 0;
6677 
6678 	/* <addr> <config method> [join|auto] [bstrapmethod=<value>] */
6679 
6680 	if (hwaddr_aton(cmd, addr))
6681 		return -1;
6682 
6683 	pos = cmd + 17;
6684 	if (*pos != ' ')
6685 		return -1;
6686 	pos++;
6687 
6688 	if (os_strstr(pos, " join") != NULL)
6689 		use = WPAS_P2P_PD_FOR_JOIN;
6690 	else if (os_strstr(pos, " auto") != NULL)
6691 		use = WPAS_P2P_PD_AUTO;
6692 
6693 	pos2 = os_strstr(pos, "bstrapmethod=");
6694 	if (pos2) {
6695 		pos2 += 13;
6696 		bootstrap = atoi(pos2);
6697 	}
6698 
6699 	return wpas_p2p_prov_disc(wpa_s, addr, pos, bootstrap, use, NULL);
6700 }
6701 
6702 
p2p_get_passphrase(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)6703 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
6704 			      size_t buflen)
6705 {
6706 	struct wpa_ssid *ssid = wpa_s->current_ssid;
6707 
6708 	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
6709 	    ssid->passphrase == NULL)
6710 		return -1;
6711 
6712 	os_strlcpy(buf, ssid->passphrase, buflen);
6713 	return os_strlen(buf);
6714 }
6715 
6716 
p2p_ctrl_validate_dira(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)6717 static int p2p_ctrl_validate_dira(struct wpa_supplicant *wpa_s, char *cmd,
6718 				  char *buf, size_t buflen)
6719 {
6720 	char *pos, *pos2;
6721 	u8 addr[ETH_ALEN];
6722 	u8 nonce[DEVICE_IDENTITY_NONCE_LEN];
6723 	u8 tag[DEVICE_IDENTITY_TAG_LEN];
6724 	int id;
6725 	int res;
6726 
6727 	if (hwaddr_aton(cmd, addr))
6728 		return -1;
6729 
6730 	pos = cmd + 17;
6731 	if (*pos != ' ')
6732 		return -1;
6733 
6734 	pos2 = os_strstr(pos, "nonce=");
6735 	if (pos2) {
6736 		pos2 += 6;
6737 		if (hexstr2bin(pos2, nonce, sizeof(nonce)) < 0)
6738 			return -1;
6739 	} else {
6740 		return -1;
6741 	}
6742 
6743 	pos2 = os_strstr(pos, "tag=");
6744 	if (pos2) {
6745 		pos2 += 4;
6746 		if (hexstr2bin(pos2, tag, sizeof(tag)) < 0)
6747 			return -1;
6748 	} else {
6749 		return -1;
6750 	}
6751 
6752 	id = wpas_p2p_validate_dira(wpa_s, addr, 0, nonce, tag);
6753 	if (id <= 0)
6754 		return -1;
6755 
6756 	res = os_snprintf(buf, buflen, "%u", id);
6757 	if (os_snprintf_error(buflen, res))
6758 		return -1;
6759 	return res;
6760 }
6761 
6762 
p2p_ctrl_serv_disc_req(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)6763 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
6764 				  char *buf, size_t buflen)
6765 {
6766 	u64 ref;
6767 	int res;
6768 	u8 dst_buf[ETH_ALEN], *dst;
6769 	struct wpabuf *tlvs;
6770 	char *pos;
6771 	size_t len;
6772 
6773 	if (hwaddr_aton(cmd, dst_buf))
6774 		return -1;
6775 	dst = dst_buf;
6776 	if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
6777 	    dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
6778 		dst = NULL;
6779 	pos = cmd + 17;
6780 	if (*pos != ' ')
6781 		return -1;
6782 	pos++;
6783 
6784 	if (os_strncmp(pos, "upnp ", 5) == 0) {
6785 		u8 version;
6786 		pos += 5;
6787 		if (hexstr2bin(pos, &version, 1) < 0)
6788 			return -1;
6789 		pos += 2;
6790 		if (*pos != ' ')
6791 			return -1;
6792 		pos++;
6793 		ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
6794 #ifdef CONFIG_WIFI_DISPLAY
6795 	} else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
6796 		ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
6797 #endif /* CONFIG_WIFI_DISPLAY */
6798 	} else if (os_strncmp(pos, "asp ", 4) == 0) {
6799 		char *svc_str;
6800 		char *svc_info = NULL;
6801 		u32 id;
6802 
6803 		pos += 4;
6804 		if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
6805 			return -1;
6806 
6807 		pos = os_strchr(pos, ' ');
6808 		if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
6809 			return -1;
6810 
6811 		svc_str = pos + 1;
6812 
6813 		pos = os_strchr(svc_str, ' ');
6814 
6815 		if (pos)
6816 			*pos++ = '\0';
6817 
6818 		/* All remaining data is the svc_info string */
6819 		if (pos && pos[0] && pos[0] != ' ') {
6820 			len = os_strlen(pos);
6821 
6822 			/* Unescape in place */
6823 			len = utf8_unescape(pos, len, pos, len);
6824 			if (len > 0xff)
6825 				return -1;
6826 
6827 			svc_info = pos;
6828 		}
6829 
6830 		ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
6831 					      svc_str, svc_info);
6832 	} else {
6833 		len = os_strlen(pos);
6834 		if (len & 1)
6835 			return -1;
6836 		len /= 2;
6837 		tlvs = wpabuf_alloc(len);
6838 		if (tlvs == NULL)
6839 			return -1;
6840 		if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
6841 			wpabuf_free(tlvs);
6842 			return -1;
6843 		}
6844 
6845 		ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
6846 		wpabuf_free(tlvs);
6847 	}
6848 	if (ref == 0)
6849 		return -1;
6850 	res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
6851 	if (os_snprintf_error(buflen, res))
6852 		return -1;
6853 	return res;
6854 }
6855 
6856 
p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant * wpa_s,char * cmd)6857 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
6858 					 char *cmd)
6859 {
6860 	long long unsigned val;
6861 	u64 req;
6862 	if (sscanf(cmd, "%llx", &val) != 1)
6863 		return -1;
6864 	req = val;
6865 	return wpas_p2p_sd_cancel_request(wpa_s, req);
6866 }
6867 
6868 
p2p_ctrl_serv_disc_resp(struct wpa_supplicant * wpa_s,char * cmd)6869 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
6870 {
6871 	int freq;
6872 	u8 dst[ETH_ALEN];
6873 	u8 dialog_token;
6874 	struct wpabuf *resp_tlvs;
6875 	char *pos, *pos2;
6876 	size_t len;
6877 
6878 	pos = os_strchr(cmd, ' ');
6879 	if (pos == NULL)
6880 		return -1;
6881 	*pos++ = '\0';
6882 	freq = atoi(cmd);
6883 	if (freq == 0)
6884 		return -1;
6885 
6886 	if (hwaddr_aton(pos, dst))
6887 		return -1;
6888 	pos += 17;
6889 	if (*pos != ' ')
6890 		return -1;
6891 	pos++;
6892 
6893 	pos2 = os_strchr(pos, ' ');
6894 	if (pos2 == NULL)
6895 		return -1;
6896 	*pos2++ = '\0';
6897 	dialog_token = atoi(pos);
6898 
6899 	len = os_strlen(pos2);
6900 	if (len & 1)
6901 		return -1;
6902 	len /= 2;
6903 	resp_tlvs = wpabuf_alloc(len);
6904 	if (resp_tlvs == NULL)
6905 		return -1;
6906 	if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
6907 		wpabuf_free(resp_tlvs);
6908 		return -1;
6909 	}
6910 
6911 	wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
6912 	wpabuf_free(resp_tlvs);
6913 	return 0;
6914 }
6915 
6916 
p2p_ctrl_serv_disc_external(struct wpa_supplicant * wpa_s,char * cmd)6917 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
6918 				       char *cmd)
6919 {
6920 	if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
6921 		return -1;
6922 	wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
6923 	return 0;
6924 }
6925 
6926 
p2p_ctrl_service_add_bonjour(struct wpa_supplicant * wpa_s,char * cmd)6927 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
6928 					char *cmd)
6929 {
6930 	char *pos;
6931 	size_t len;
6932 	struct wpabuf *query, *resp;
6933 	int ret;
6934 
6935 	pos = os_strchr(cmd, ' ');
6936 	if (pos == NULL)
6937 		return -1;
6938 	*pos++ = '\0';
6939 
6940 	len = os_strlen(cmd);
6941 	if (len & 1)
6942 		return -1;
6943 	len /= 2;
6944 	query = wpabuf_alloc(len);
6945 	if (query == NULL)
6946 		return -1;
6947 	ret = hexstr2bin(cmd, wpabuf_put(query, len), len);
6948 	if (ret < 0)
6949 		goto err_query;
6950 	ret = -1;
6951 	len = os_strlen(pos);
6952 	if (len & 1)
6953 		goto err_query;
6954 	len /= 2;
6955 	resp = wpabuf_alloc(len);
6956 	if (!resp)
6957 		goto err_query;
6958 	ret = hexstr2bin(pos, wpabuf_put(resp, len), len);
6959 	if (ret < 0)
6960 		goto err_resp;
6961 
6962 	ret = wpas_p2p_service_add_bonjour(wpa_s, query, resp);
6963 
6964 err_resp:
6965 	wpabuf_free(resp);
6966 err_query:
6967 	wpabuf_free(query);
6968 	return ret;
6969 }
6970 
6971 
p2p_ctrl_service_add_upnp(struct wpa_supplicant * wpa_s,char * cmd)6972 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
6973 {
6974 	char *pos;
6975 	u8 version;
6976 
6977 	pos = os_strchr(cmd, ' ');
6978 	if (pos == NULL)
6979 		return -1;
6980 	*pos++ = '\0';
6981 
6982 	if (hexstr2bin(cmd, &version, 1) < 0)
6983 		return -1;
6984 
6985 	return wpas_p2p_service_add_upnp(wpa_s, version, pos);
6986 }
6987 
6988 
p2p_ctrl_service_add_asp(struct wpa_supplicant * wpa_s,u8 replace,char * cmd)6989 static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
6990 				    u8 replace, char *cmd)
6991 {
6992 	char *pos;
6993 	char *adv_str;
6994 	u32 auto_accept, adv_id, svc_state, config_methods;
6995 	char *svc_info = NULL;
6996 	char *cpt_prio_str;
6997 	u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
6998 
6999 	pos = os_strchr(cmd, ' ');
7000 	if (pos == NULL)
7001 		return -1;
7002 	*pos++ = '\0';
7003 
7004 	/* Auto-Accept value is mandatory, and must be one of the
7005 	 * single values (0, 1, 2, 4) */
7006 	auto_accept = atoi(cmd);
7007 	switch (auto_accept) {
7008 	case P2PS_SETUP_NONE: /* No auto-accept */
7009 	case P2PS_SETUP_NEW:
7010 	case P2PS_SETUP_CLIENT:
7011 	case P2PS_SETUP_GROUP_OWNER:
7012 		break;
7013 	default:
7014 		return -1;
7015 	}
7016 
7017 	/* Advertisement ID is mandatory */
7018 	cmd = pos;
7019 	pos = os_strchr(cmd, ' ');
7020 	if (pos == NULL)
7021 		return -1;
7022 	*pos++ = '\0';
7023 
7024 	/* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
7025 	if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
7026 		return -1;
7027 
7028 	/* Only allow replacements if exist, and adds if not */
7029 	if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
7030 		if (!replace)
7031 			return -1;
7032 	} else {
7033 		if (replace)
7034 			return -1;
7035 	}
7036 
7037 	/* svc_state between 0 - 0xff is mandatory */
7038 	if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
7039 		return -1;
7040 
7041 	pos = os_strchr(pos, ' ');
7042 	if (pos == NULL)
7043 		return -1;
7044 
7045 	/* config_methods is mandatory */
7046 	pos++;
7047 	if (sscanf(pos, "%x", &config_methods) != 1)
7048 		return -1;
7049 
7050 	if (!(config_methods &
7051 	      (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
7052 		return -1;
7053 
7054 	pos = os_strchr(pos, ' ');
7055 	if (pos == NULL)
7056 		return -1;
7057 
7058 	pos++;
7059 	adv_str = pos;
7060 
7061 	/* Advertisement string is mandatory */
7062 	if (!pos[0] || pos[0] == ' ')
7063 		return -1;
7064 
7065 	/* Terminate svc string */
7066 	pos = os_strchr(pos, ' ');
7067 	if (pos != NULL)
7068 		*pos++ = '\0';
7069 
7070 	cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
7071 	if (cpt_prio_str) {
7072 		pos = os_strchr(pos, ' ');
7073 		if (pos != NULL)
7074 			*pos++ = '\0';
7075 
7076 		if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
7077 			return -1;
7078 	} else {
7079 		cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
7080 		cpt_prio[1] = 0;
7081 	}
7082 
7083 	/* Service and Response Information are optional */
7084 	if (pos && pos[0]) {
7085 		size_t len;
7086 
7087 		/* Note the bare ' included, which cannot exist legally
7088 		 * in unescaped string. */
7089 		svc_info = os_strstr(pos, "svc_info='");
7090 
7091 		if (svc_info) {
7092 			svc_info += 9;
7093 			len = os_strlen(svc_info);
7094 			utf8_unescape(svc_info, len, svc_info, len);
7095 		}
7096 	}
7097 
7098 	return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
7099 					(u8) svc_state, (u16) config_methods,
7100 					svc_info, cpt_prio);
7101 }
7102 
7103 
p2p_ctrl_service_add(struct wpa_supplicant * wpa_s,char * cmd)7104 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
7105 {
7106 	char *pos;
7107 
7108 	pos = os_strchr(cmd, ' ');
7109 	if (pos == NULL)
7110 		return -1;
7111 	*pos++ = '\0';
7112 
7113 	if (os_strcmp(cmd, "bonjour") == 0)
7114 		return p2p_ctrl_service_add_bonjour(wpa_s, pos);
7115 	if (os_strcmp(cmd, "upnp") == 0)
7116 		return p2p_ctrl_service_add_upnp(wpa_s, pos);
7117 	if (os_strcmp(cmd, "asp") == 0)
7118 		return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
7119 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
7120 	return -1;
7121 }
7122 
7123 
p2p_ctrl_service_del_bonjour(struct wpa_supplicant * wpa_s,char * cmd)7124 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
7125 					char *cmd)
7126 {
7127 	size_t len;
7128 	struct wpabuf *query;
7129 	int ret;
7130 
7131 	len = os_strlen(cmd);
7132 	if (len & 1)
7133 		return -1;
7134 	len /= 2;
7135 	query = wpabuf_alloc(len);
7136 	if (query == NULL)
7137 		return -1;
7138 	if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
7139 		wpabuf_free(query);
7140 		return -1;
7141 	}
7142 
7143 	ret = wpas_p2p_service_del_bonjour(wpa_s, query);
7144 	wpabuf_free(query);
7145 	return ret;
7146 }
7147 
7148 
p2p_ctrl_service_del_upnp(struct wpa_supplicant * wpa_s,char * cmd)7149 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
7150 {
7151 	char *pos;
7152 	u8 version;
7153 
7154 	pos = os_strchr(cmd, ' ');
7155 	if (pos == NULL)
7156 		return -1;
7157 	*pos++ = '\0';
7158 
7159 	if (hexstr2bin(cmd, &version, 1) < 0)
7160 		return -1;
7161 
7162 	return wpas_p2p_service_del_upnp(wpa_s, version, pos);
7163 }
7164 
7165 
p2p_ctrl_service_del_asp(struct wpa_supplicant * wpa_s,char * cmd)7166 static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
7167 {
7168 	u32 adv_id;
7169 
7170 	if (os_strcmp(cmd, "all") == 0) {
7171 		wpas_p2p_service_flush_asp(wpa_s);
7172 		return 0;
7173 	}
7174 
7175 	if (sscanf(cmd, "%x", &adv_id) != 1)
7176 		return -1;
7177 
7178 	return wpas_p2p_service_del_asp(wpa_s, adv_id);
7179 }
7180 
7181 
p2p_ctrl_service_del(struct wpa_supplicant * wpa_s,char * cmd)7182 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
7183 {
7184 	char *pos;
7185 
7186 	pos = os_strchr(cmd, ' ');
7187 	if (pos == NULL)
7188 		return -1;
7189 	*pos++ = '\0';
7190 
7191 	if (os_strcmp(cmd, "bonjour") == 0)
7192 		return p2p_ctrl_service_del_bonjour(wpa_s, pos);
7193 	if (os_strcmp(cmd, "upnp") == 0)
7194 		return p2p_ctrl_service_del_upnp(wpa_s, pos);
7195 	if (os_strcmp(cmd, "asp") == 0)
7196 		return p2p_ctrl_service_del_asp(wpa_s, pos);
7197 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
7198 	return -1;
7199 }
7200 
7201 
p2p_ctrl_service_replace(struct wpa_supplicant * wpa_s,char * cmd)7202 static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
7203 {
7204 	char *pos;
7205 
7206 	pos = os_strchr(cmd, ' ');
7207 	if (pos == NULL)
7208 		return -1;
7209 	*pos++ = '\0';
7210 
7211 	if (os_strcmp(cmd, "asp") == 0)
7212 		return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
7213 
7214 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
7215 	return -1;
7216 }
7217 
7218 
p2p_ctrl_reject(struct wpa_supplicant * wpa_s,char * cmd)7219 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
7220 {
7221 	u8 addr[ETH_ALEN];
7222 
7223 	/* <addr> */
7224 
7225 	if (hwaddr_aton(cmd, addr))
7226 		return -1;
7227 
7228 	return wpas_p2p_reject(wpa_s, addr);
7229 }
7230 
7231 
p2p_ctrl_invite_persistent(struct wpa_supplicant * wpa_s,char * cmd)7232 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
7233 {
7234 	char *pos;
7235 	int id;
7236 	struct wpa_ssid *ssid = NULL;
7237 	u8 *_peer = NULL, peer[ETH_ALEN];
7238 	int freq = 0, pref_freq = 0;
7239 	int ht40, vht, he, max_oper_chwidth, chwidth = 0, freq2 = 0;
7240 	int edmg;
7241 	bool allow_6ghz;
7242 	bool p2p2;
7243 
7244 	p2p2 = os_strstr(cmd, " p2p2") != NULL;
7245 
7246 	pos = os_strstr(cmd, " peer=");
7247 	if (pos) {
7248 		pos += 6;
7249 		if (hwaddr_aton(pos, peer))
7250 			return -1;
7251 		_peer = peer;
7252 	}
7253 
7254 	if (os_strncmp(cmd, "persistent=", 11) == 0) {
7255 		id = atoi(cmd + 11);
7256 		ssid = wpa_config_get_network(wpa_s->conf, id);
7257 		if (!ssid || ssid->disabled != 2) {
7258 			wpa_printf(MSG_DEBUG,
7259 				   "CTRL_IFACE: Could not find SSID id=%d for persistent P2P group",
7260 				   id);
7261 			return -1;
7262 		}
7263 	} else if (p2p2 && !_peer) {
7264 		wpa_printf(MSG_DEBUG,
7265 			   "CTRL_IFACE: Could not find peer for persistent P2P group");
7266 		return -1;
7267 	}
7268 
7269 	pos = os_strstr(cmd, " freq=");
7270 	if (pos) {
7271 		pos += 6;
7272 		freq = atoi(pos);
7273 		if (freq <= 0)
7274 			return -1;
7275 	}
7276 
7277 	pos = os_strstr(cmd, " pref=");
7278 	if (pos) {
7279 		pos += 6;
7280 		pref_freq = atoi(pos);
7281 		if (pref_freq <= 0)
7282 			return -1;
7283 	}
7284 
7285 	vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
7286 	ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
7287 		vht;
7288 	he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
7289 	edmg = (os_strstr(cmd, " edmg") != NULL) || wpa_s->conf->p2p_go_edmg;
7290 
7291 	pos = os_strstr(cmd, "freq2=");
7292 	if (pos)
7293 		freq2 = atoi(pos + 6);
7294 
7295 	pos = os_strstr(cmd, " max_oper_chwidth=");
7296 	if (pos)
7297 		chwidth = atoi(pos + 18);
7298 
7299 	max_oper_chwidth = chwidth_freq2_to_ch_width(chwidth, freq2);
7300 	if (max_oper_chwidth < 0)
7301 		return -1;
7302 
7303 	allow_6ghz = os_strstr(cmd, " allow_6ghz") != NULL;
7304 
7305 	if (allow_6ghz && chwidth == 40)
7306 		max_oper_chwidth = CONF_OPER_CHWIDTH_40MHZ_6GHZ;
7307 
7308 	return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
7309 			       max_oper_chwidth, pref_freq, he, edmg,
7310 			       allow_6ghz, p2p2);
7311 }
7312 
7313 
p2p_ctrl_invite_group(struct wpa_supplicant * wpa_s,char * cmd)7314 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
7315 {
7316 	char *pos;
7317 	u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
7318 	bool allow_6ghz;
7319 
7320 	pos = os_strstr(cmd, " peer=");
7321 	if (!pos)
7322 		return -1;
7323 
7324 	*pos = '\0';
7325 	pos += 6;
7326 	if (hwaddr_aton(pos, peer)) {
7327 		wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
7328 		return -1;
7329 	}
7330 
7331 	allow_6ghz = os_strstr(pos, " allow_6ghz") != NULL;
7332 
7333 	pos = os_strstr(pos, " go_dev_addr=");
7334 	if (pos) {
7335 		pos += 13;
7336 		if (hwaddr_aton(pos, go_dev_addr)) {
7337 			wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
7338 				   pos);
7339 			return -1;
7340 		}
7341 		go_dev = go_dev_addr;
7342 	}
7343 
7344 	return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev, allow_6ghz);
7345 }
7346 
7347 
p2p_ctrl_invite(struct wpa_supplicant * wpa_s,char * cmd)7348 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
7349 {
7350 	if (os_strncmp(cmd, "persistent", 10) == 0)
7351 		return p2p_ctrl_invite_persistent(wpa_s, cmd);
7352 	if (os_strncmp(cmd, "group=", 6) == 0)
7353 		return p2p_ctrl_invite_group(wpa_s, cmd + 6);
7354 
7355 	return -1;
7356 }
7357 
7358 
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,bool p2p2,enum wpa_p2p_mode p2p_mode)7359 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
7360 					 int id, int freq, int vht_center_freq2,
7361 					 int ht40, int vht, int vht_chwidth,
7362 					 int he, int edmg, bool allow_6ghz,
7363 					 const u8 *go_bssid, bool p2p2,
7364 					 enum wpa_p2p_mode p2p_mode)
7365 {
7366 	struct wpa_ssid *ssid;
7367 	bool join = false;
7368 
7369 	ssid = wpa_config_get_network(wpa_s->conf, id);
7370 	if (ssid == NULL || ssid->disabled != 2) {
7371 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
7372 			   "for persistent P2P group",
7373 			   id);
7374 		return -1;
7375 	}
7376 
7377 	if (p2p2) {
7378 		wpa_s->p2p2 = p2p2;
7379 		wpa_s->p2p_mode = p2p_mode;
7380 		join = true;
7381 	}
7382 	return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, freq,
7383 					     vht_center_freq2, ht40, vht,
7384 					     vht_chwidth, he, edmg,
7385 					     NULL, 0, 0, allow_6ghz, 0,
7386 					     go_bssid, NULL, NULL, NULL, 0,
7387 					     join, false);
7388 }
7389 
7390 
p2p_ctrl_group_add(struct wpa_supplicant * wpa_s,char * cmd)7391 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
7392 {
7393 	int freq = 0, persistent = 0, group_id = -1;
7394 	bool p2p2 = false;
7395 	int p2pmode = WPA_P2P_MODE_WFD_R1;
7396 	enum wpa_p2p_mode p2p_mode;
7397 	bool allow_6ghz = false;
7398 	int vht = wpa_s->conf->p2p_go_vht;
7399 	int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
7400 	int he = wpa_s->conf->p2p_go_he;
7401 	int edmg = wpa_s->conf->p2p_go_edmg;
7402 	int max_oper_chwidth, chwidth = 0, freq2 = 0;
7403 	char *token, *context = NULL;
7404 	u8 go_bssid_buf[ETH_ALEN], *go_bssid = NULL;
7405 #ifdef CONFIG_ACS
7406 	int acs = 0;
7407 #endif /* CONFIG_ACS */
7408 
7409 	while ((token = str_token(cmd, " ", &context))) {
7410 		if (sscanf(token, "freq2=%d", &freq2) == 1 ||
7411 		    sscanf(token, "persistent=%d", &group_id) == 1 ||
7412 		    sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1 ||
7413 		    sscanf(token, "p2pmode=%d", &p2pmode) == 1) {
7414 			continue;
7415 #ifdef CONFIG_ACS
7416 		} else if (os_strcmp(token, "freq=acs") == 0) {
7417 			acs = 1;
7418 #endif /* CONFIG_ACS */
7419 		} else if (sscanf(token, "freq=%d", &freq) == 1) {
7420 			continue;
7421 		} else if (os_strcmp(token, "ht40") == 0) {
7422 			ht40 = 1;
7423 		} else if (os_strcmp(token, "vht") == 0) {
7424 			vht = 1;
7425 			ht40 = 1;
7426 		} else if (os_strcmp(token, "he") == 0) {
7427 			he = 1;
7428 		} else if (os_strcmp(token, "edmg") == 0) {
7429 			edmg = 1;
7430 		} else if (os_strcmp(token, "persistent") == 0) {
7431 			persistent = 1;
7432 		} else if (os_strcmp(token, "allow_6ghz") == 0) {
7433 			allow_6ghz = true;
7434 		} else if (os_strcmp(token, "p2p2") == 0) {
7435 			p2p2 = true;
7436 		} else if (os_strncmp(token, "go_bssid=", 9) == 0) {
7437 			if (hwaddr_aton(token + 9, go_bssid_buf))
7438 				return -1;
7439 			go_bssid = go_bssid_buf;
7440 		} else {
7441 			wpa_printf(MSG_DEBUG,
7442 				   "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
7443 				   token);
7444 			return -1;
7445 		}
7446 	}
7447 
7448 #ifdef CONFIG_ACS
7449 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
7450 	    (freq == 2 || freq == 5)) {
7451 		unsigned int res, size = P2P_MAX_PREF_CHANNELS;
7452 		struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7453 
7454 		acs = 1;
7455 		res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
7456 						 &size, pref_freq_list);
7457 		if (!res && size > 0)
7458 			acs = 0;
7459 	}
7460 
7461 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) && acs) {
7462 		if (freq == 2 && wpa_s->best_24_freq <= 0) {
7463 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211G;
7464 			wpa_s->p2p_go_do_acs = 1;
7465 			freq = 0;
7466 		} else if (freq == 5 && wpa_s->best_5_freq <= 0) {
7467 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211A;
7468 			wpa_s->p2p_go_do_acs = 1;
7469 			freq = 0;
7470 		} else {
7471 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211ANY;
7472 			wpa_s->p2p_go_do_acs = 1;
7473 		}
7474 	} else {
7475 		wpa_s->p2p_go_do_acs = 0;
7476 	}
7477 #endif /* CONFIG_ACS */
7478 
7479 	max_oper_chwidth = chwidth_freq2_to_ch_width(chwidth, freq2);
7480 	if (max_oper_chwidth < 0)
7481 		return -1;
7482 
7483 	if (allow_6ghz && chwidth == 40)
7484 		max_oper_chwidth = CONF_OPER_CHWIDTH_40MHZ_6GHZ;
7485 
7486 	/* Allow DFS to be used for Autonomous GO */
7487 	wpa_s->p2p_go_allow_dfs = !!(wpa_s->drv_flags &
7488 				     WPA_DRIVER_FLAGS_DFS_OFFLOAD);
7489 
7490 	if (p2pmode < WPA_P2P_MODE_WFD_R1 || p2pmode > WPA_P2P_MODE_WFD_PCC)
7491 		return -1;
7492 	p2p_mode = p2pmode;
7493 
7494 	if (group_id >= 0)
7495 		return p2p_ctrl_group_add_persistent(wpa_s, group_id,
7496 						     freq, freq2, ht40, vht,
7497 						     max_oper_chwidth, he,
7498 						     edmg, allow_6ghz,
7499 						     go_bssid, p2p2,
7500 						     p2p_mode);
7501 
7502 	return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
7503 				  max_oper_chwidth, he, edmg, allow_6ghz, p2p2,
7504 				  p2p_mode);
7505 }
7506 
7507 
p2p_ctrl_group_member(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)7508 static int p2p_ctrl_group_member(struct wpa_supplicant *wpa_s, const char *cmd,
7509 				 char *buf, size_t buflen)
7510 {
7511 	u8 dev_addr[ETH_ALEN];
7512 	struct wpa_ssid *ssid;
7513 	int res;
7514 	const u8 *iaddr;
7515 
7516 	ssid = wpa_s->current_ssid;
7517 	if (!wpa_s->global->p2p || !ssid || ssid->mode != WPAS_MODE_P2P_GO ||
7518 	    hwaddr_aton(cmd, dev_addr))
7519 		return -1;
7520 
7521 	iaddr = p2p_group_get_client_interface_addr(wpa_s->p2p_group, dev_addr);
7522 	if (!iaddr)
7523 		return -1;
7524 	res = os_snprintf(buf, buflen, MACSTR, MAC2STR(iaddr));
7525 	if (os_snprintf_error(buflen, res))
7526 		return -1;
7527 	return res;
7528 }
7529 
7530 
wpas_find_p2p_dev_addr_bss(struct wpa_global * global,const u8 * p2p_dev_addr)7531 static int wpas_find_p2p_dev_addr_bss(struct wpa_global *global,
7532 				      const u8 *p2p_dev_addr)
7533 {
7534 	struct wpa_supplicant *wpa_s;
7535 
7536 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7537 		if (wpa_bss_get_p2p_dev_addr(wpa_s, p2p_dev_addr))
7538 			return 1;
7539 	}
7540 
7541 	return 0;
7542 }
7543 
7544 
p2p_ctrl_peer(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)7545 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
7546 			 char *buf, size_t buflen)
7547 {
7548 	u8 addr[ETH_ALEN], *addr_ptr, group_capab;
7549 	int next, res;
7550 	const struct p2p_peer_info *info;
7551 	char *pos, *end;
7552 	char devtype[WPS_DEV_TYPE_BUFSIZE];
7553 	struct wpa_ssid *ssid;
7554 	size_t i;
7555 
7556 	if (!wpa_s->global->p2p)
7557 		return -1;
7558 
7559 	if (os_strcmp(cmd, "FIRST") == 0) {
7560 		addr_ptr = NULL;
7561 		next = 0;
7562 	} else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
7563 		if (hwaddr_aton(cmd + 5, addr) < 0)
7564 			return -1;
7565 		addr_ptr = addr;
7566 		next = 1;
7567 	} else {
7568 		if (hwaddr_aton(cmd, addr) < 0)
7569 			return -1;
7570 		addr_ptr = addr;
7571 		next = 0;
7572 	}
7573 
7574 	info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
7575 	if (info == NULL)
7576 		return -1;
7577 	group_capab = info->group_capab;
7578 
7579 	if (group_capab &&
7580 	    !wpas_find_p2p_dev_addr_bss(wpa_s->global, info->p2p_device_addr)) {
7581 		wpa_printf(MSG_DEBUG,
7582 			   "P2P: Could not find any BSS with p2p_dev_addr "
7583 			   MACSTR ", hence override group_capab from 0x%x to 0",
7584 			   MAC2STR(info->p2p_device_addr), group_capab);
7585 		group_capab = 0;
7586 	}
7587 
7588 	pos = buf;
7589 	end = buf + buflen;
7590 
7591 	res = os_snprintf(pos, end - pos, MACSTR "\n"
7592 			  "pri_dev_type=%s\n"
7593 			  "device_name=%s\n"
7594 			  "manufacturer=%s\n"
7595 			  "model_name=%s\n"
7596 			  "model_number=%s\n"
7597 			  "serial_number=%s\n"
7598 			  "config_methods=0x%x\n"
7599 			  "dev_capab=0x%x\n"
7600 			  "group_capab=0x%x\n"
7601 			  "level=%d\n",
7602 			  MAC2STR(info->p2p_device_addr),
7603 			  wps_dev_type_bin2str(info->pri_dev_type,
7604 					       devtype, sizeof(devtype)),
7605 			  info->device_name,
7606 			  info->manufacturer,
7607 			  info->model_name,
7608 			  info->model_number,
7609 			  info->serial_number,
7610 			  info->config_methods,
7611 			  info->dev_capab,
7612 			  group_capab,
7613 			  info->level);
7614 	if (os_snprintf_error(end - pos, res))
7615 		return pos - buf;
7616 	pos += res;
7617 
7618 	for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
7619 	{
7620 		const u8 *t;
7621 		t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
7622 		res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
7623 				  wps_dev_type_bin2str(t, devtype,
7624 						       sizeof(devtype)));
7625 		if (os_snprintf_error(end - pos, res))
7626 			return pos - buf;
7627 		pos += res;
7628 	}
7629 
7630 	ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
7631 	if (ssid) {
7632 		res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
7633 		if (os_snprintf_error(end - pos, res))
7634 			return pos - buf;
7635 		pos += res;
7636 	}
7637 
7638 	res = p2p_get_peer_info_txt(info, pos, end - pos);
7639 	if (res < 0)
7640 		return pos - buf;
7641 	pos += res;
7642 
7643 	if (info->vendor_elems) {
7644 		res = os_snprintf(pos, end - pos, "vendor_elems=");
7645 		if (os_snprintf_error(end - pos, res))
7646 			return pos - buf;
7647 		pos += res;
7648 
7649 		pos += wpa_snprintf_hex(pos, end - pos,
7650 					wpabuf_head(info->vendor_elems),
7651 					wpabuf_len(info->vendor_elems));
7652 
7653 		res = os_snprintf(pos, end - pos, "\n");
7654 		if (os_snprintf_error(end - pos, res))
7655 			return pos - buf;
7656 		pos += res;
7657 	}
7658 
7659 	return pos - buf;
7660 }
7661 
7662 
p2p_ctrl_disallow_freq(struct wpa_supplicant * wpa_s,const char * param)7663 static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
7664 				  const char *param)
7665 {
7666 	unsigned int i;
7667 
7668 	if (wpa_s->global->p2p == NULL)
7669 		return -1;
7670 
7671 	if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
7672 		return -1;
7673 
7674 	for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
7675 		struct wpa_freq_range *freq;
7676 		freq = &wpa_s->global->p2p_disallow_freq.range[i];
7677 		wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
7678 			   freq->min, freq->max);
7679 	}
7680 
7681 	wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
7682 	return 0;
7683 }
7684 
7685 
p2p_ctrl_set(struct wpa_supplicant * wpa_s,char * cmd)7686 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
7687 {
7688 	char *param;
7689 
7690 	if (wpa_s->global->p2p == NULL)
7691 		return -1;
7692 
7693 	param = os_strchr(cmd, ' ');
7694 	if (param == NULL)
7695 		return -1;
7696 	*param++ = '\0';
7697 
7698 	if (os_strcmp(cmd, "discoverability") == 0) {
7699 		p2p_set_client_discoverability(wpa_s->global->p2p,
7700 					       atoi(param));
7701 		return 0;
7702 	}
7703 
7704 	if (os_strcmp(cmd, "managed") == 0) {
7705 		p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
7706 		return 0;
7707 	}
7708 
7709 	if (os_strcmp(cmd, "listen_channel") == 0) {
7710 		char *pos;
7711 		u8 channel, op_class;
7712 
7713 		channel = atoi(param);
7714 		pos = os_strchr(param, ' ');
7715 		op_class = pos ? atoi(pos) : 81;
7716 
7717 		return p2p_set_listen_channel(wpa_s->global->p2p, op_class,
7718 					      channel, 1);
7719 	}
7720 
7721 	if (os_strcmp(cmd, "ssid_postfix") == 0) {
7722 		return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
7723 					    os_strlen(param));
7724 	}
7725 
7726 	if (os_strcmp(cmd, "noa") == 0) {
7727 		char *pos;
7728 		int count, start, duration;
7729 		/* GO NoA parameters: count,start_offset(ms),duration(ms) */
7730 		count = atoi(param);
7731 		pos = os_strchr(param, ',');
7732 		if (pos == NULL)
7733 			return -1;
7734 		pos++;
7735 		start = atoi(pos);
7736 		pos = os_strchr(pos, ',');
7737 		if (pos == NULL)
7738 			return -1;
7739 		pos++;
7740 		duration = atoi(pos);
7741 		if (count < 0 || count > 255 || start < 0 || duration < 0)
7742 			return -1;
7743 		if (count == 0 && duration > 0)
7744 			return -1;
7745 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
7746 			   "start=%d duration=%d", count, start, duration);
7747 		return wpas_p2p_set_noa(wpa_s, count, start, duration);
7748 	}
7749 
7750 	if (os_strcmp(cmd, "ps") == 0)
7751 		return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
7752 
7753 	if (os_strcmp(cmd, "oppps") == 0)
7754 		return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
7755 
7756 	if (os_strcmp(cmd, "ctwindow") == 0)
7757 		return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
7758 
7759 	if (os_strcmp(cmd, "disabled") == 0) {
7760 		wpa_s->global->p2p_disabled = atoi(param);
7761 		wpa_printf(MSG_DEBUG, "P2P functionality %s",
7762 			   wpa_s->global->p2p_disabled ?
7763 			   "disabled" : "enabled");
7764 		if (wpa_s->global->p2p_disabled) {
7765 			wpas_p2p_stop_find(wpa_s);
7766 			os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7767 			p2p_flush(wpa_s->global->p2p);
7768 		}
7769 		return 0;
7770 	}
7771 
7772 	if (os_strcmp(cmd, "conc_pref") == 0) {
7773 		if (os_strcmp(param, "sta") == 0)
7774 			wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
7775 		else if (os_strcmp(param, "p2p") == 0)
7776 			wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
7777 		else {
7778 			wpa_printf(MSG_INFO, "Invalid conc_pref value");
7779 			return -1;
7780 		}
7781 		wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
7782 			   "%s", param);
7783 		return 0;
7784 	}
7785 
7786 	if (os_strcmp(cmd, "force_long_sd") == 0) {
7787 		wpa_s->force_long_sd = atoi(param);
7788 		return 0;
7789 	}
7790 
7791 	if (os_strcmp(cmd, "peer_filter") == 0) {
7792 		u8 addr[ETH_ALEN];
7793 		if (hwaddr_aton(param, addr))
7794 			return -1;
7795 		p2p_set_peer_filter(wpa_s->global->p2p, addr);
7796 		return 0;
7797 	}
7798 
7799 	if (os_strcmp(cmd, "cross_connect") == 0)
7800 		return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
7801 
7802 	if (os_strcmp(cmd, "go_apsd") == 0) {
7803 		if (os_strcmp(param, "disable") == 0)
7804 			wpa_s->set_ap_uapsd = 0;
7805 		else {
7806 			wpa_s->set_ap_uapsd = 1;
7807 			wpa_s->ap_uapsd = atoi(param);
7808 		}
7809 		return 0;
7810 	}
7811 
7812 	if (os_strcmp(cmd, "client_apsd") == 0) {
7813 		if (os_strcmp(param, "disable") == 0)
7814 			wpa_s->set_sta_uapsd = 0;
7815 		else {
7816 			int be, bk, vi, vo;
7817 			char *pos;
7818 			/* format: BE,BK,VI,VO;max SP Length */
7819 			be = atoi(param);
7820 			pos = os_strchr(param, ',');
7821 			if (pos == NULL)
7822 				return -1;
7823 			pos++;
7824 			bk = atoi(pos);
7825 			pos = os_strchr(pos, ',');
7826 			if (pos == NULL)
7827 				return -1;
7828 			pos++;
7829 			vi = atoi(pos);
7830 			pos = os_strchr(pos, ',');
7831 			if (pos == NULL)
7832 				return -1;
7833 			pos++;
7834 			vo = atoi(pos);
7835 			/* ignore max SP Length for now */
7836 
7837 			wpa_s->set_sta_uapsd = 1;
7838 			wpa_s->sta_uapsd = 0;
7839 			if (be)
7840 				wpa_s->sta_uapsd |= BIT(0);
7841 			if (bk)
7842 				wpa_s->sta_uapsd |= BIT(1);
7843 			if (vi)
7844 				wpa_s->sta_uapsd |= BIT(2);
7845 			if (vo)
7846 				wpa_s->sta_uapsd |= BIT(3);
7847 		}
7848 		return 0;
7849 	}
7850 
7851 	if (os_strcmp(cmd, "disallow_freq") == 0)
7852 		return p2p_ctrl_disallow_freq(wpa_s, param);
7853 
7854 	if (os_strcmp(cmd, "disc_int") == 0) {
7855 		int min_disc_int, max_disc_int, max_disc_tu;
7856 		char *pos;
7857 
7858 		pos = param;
7859 
7860 		min_disc_int = atoi(pos);
7861 		pos = os_strchr(pos, ' ');
7862 		if (pos == NULL)
7863 			return -1;
7864 		*pos++ = '\0';
7865 
7866 		max_disc_int = atoi(pos);
7867 		pos = os_strchr(pos, ' ');
7868 		if (pos == NULL)
7869 			return -1;
7870 		*pos++ = '\0';
7871 
7872 		max_disc_tu = atoi(pos);
7873 
7874 		return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
7875 					max_disc_int, max_disc_tu);
7876 	}
7877 
7878 	if (os_strcmp(cmd, "per_sta_psk") == 0) {
7879 		wpa_s->global->p2p_per_sta_psk = !!atoi(param);
7880 		return 0;
7881 	}
7882 
7883 #ifdef CONFIG_WPS_NFC
7884 	if (os_strcmp(cmd, "nfc_tag") == 0)
7885 		return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
7886 #endif /* CONFIG_WPS_NFC */
7887 
7888 	if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
7889 		wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
7890 		return 0;
7891 	}
7892 
7893 	if (os_strcmp(cmd, "override_pref_op_chan") == 0) {
7894 		int op_class, chan;
7895 
7896 		op_class = atoi(param);
7897 		param = os_strchr(param, ':');
7898 		if (!param)
7899 			return -1;
7900 		param++;
7901 		chan = atoi(param);
7902 		p2p_set_override_pref_op_chan(wpa_s->global->p2p, op_class,
7903 					      chan);
7904 		return 0;
7905 	}
7906 
7907 	if (os_strcmp(cmd, "supported_bootstrapmethods") == 0) {
7908 		p2p_set_bootstrapmethods(wpa_s->global->p2p, atoi(param));
7909 		return 0;
7910 	}
7911 
7912 	if (os_strcmp(cmd, "pasn_type") == 0) {
7913 		p2p_set_pasn_type(wpa_s->global->p2p, atoi(param));
7914 		return 0;
7915 	}
7916 
7917 	if (os_strcmp(cmd, "comeback_after") == 0) {
7918 		p2p_set_comeback_after(wpa_s->global->p2p, atoi(param));
7919 		return 0;
7920 	}
7921 
7922 	if (os_strcmp(cmd, "reginfo") == 0) {
7923 		p2p_set_reg_info(wpa_s->global->p2p, atoi(param));
7924 		return 0;
7925 	}
7926 
7927 	if (os_strcmp(cmd, "twt_power_mgmt") == 0) {
7928 		p2p_set_twt_power_mgmt(wpa_s->global->p2p, atoi(param));
7929 		return 0;
7930 	}
7931 
7932 	if (os_strcmp(cmd, "chan_switch_req_enable") == 0) {
7933 		p2p_set_chan_switch_req_enable(wpa_s->global->p2p, atoi(param));
7934 		return 0;
7935 	}
7936 
7937 	if (os_strcmp(cmd, "inv_oper_freq") == 0) {
7938 		p2p_set_invitation_op_freq(wpa_s->global->p2p, atoi(param));
7939 		return 0;
7940 	}
7941 
7942 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
7943 		   cmd);
7944 
7945 	return -1;
7946 }
7947 
7948 
p2p_ctrl_flush(struct wpa_supplicant * wpa_s)7949 static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
7950 {
7951 	os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7952 	wpa_s->force_long_sd = 0;
7953 
7954 #ifdef CONFIG_TESTING_OPTIONS
7955 	os_free(wpa_s->get_pref_freq_list_override);
7956 	wpa_s->get_pref_freq_list_override = NULL;
7957 	if (wpa_s->global->p2p)
7958 		p2p_set_invitation_op_freq(wpa_s->global->p2p, -1);
7959 #endif /* CONFIG_TESTING_OPTIONS */
7960 
7961 	wpas_p2p_stop_find(wpa_s);
7962 	wpa_s->parent->p2ps_method_config_any = 0;
7963 	if (wpa_s->global->p2p)
7964 		p2p_flush(wpa_s->global->p2p);
7965 
7966 	wpa_s->p2p2 = false;
7967 }
7968 
7969 
p2p_ctrl_presence_req(struct wpa_supplicant * wpa_s,char * cmd)7970 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
7971 {
7972 	char *pos, *pos2;
7973 	unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
7974 
7975 	if (cmd[0]) {
7976 		pos = os_strchr(cmd, ' ');
7977 		if (pos == NULL)
7978 			return -1;
7979 		*pos++ = '\0';
7980 		dur1 = atoi(cmd);
7981 
7982 		pos2 = os_strchr(pos, ' ');
7983 		if (pos2)
7984 			*pos2++ = '\0';
7985 		int1 = atoi(pos);
7986 	} else
7987 		pos2 = NULL;
7988 
7989 	if (pos2) {
7990 		pos = os_strchr(pos2, ' ');
7991 		if (pos == NULL)
7992 			return -1;
7993 		*pos++ = '\0';
7994 		dur2 = atoi(pos2);
7995 		int2 = atoi(pos);
7996 	}
7997 
7998 	return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
7999 }
8000 
8001 
p2p_ctrl_ext_listen(struct wpa_supplicant * wpa_s,char * cmd)8002 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
8003 {
8004 	char *pos;
8005 	unsigned int period = 0, interval = 0;
8006 
8007 	if (cmd[0]) {
8008 		pos = os_strchr(cmd, ' ');
8009 		if (pos == NULL)
8010 			return -1;
8011 		*pos++ = '\0';
8012 		period = atoi(cmd);
8013 		interval = atoi(pos);
8014 	}
8015 
8016 	return wpas_p2p_ext_listen(wpa_s, period, interval);
8017 }
8018 
8019 
p2p_ctrl_remove_client(struct wpa_supplicant * wpa_s,const char * cmd)8020 static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
8021 {
8022 	const char *pos;
8023 	u8 peer[ETH_ALEN];
8024 	int iface_addr = 0;
8025 
8026 	pos = cmd;
8027 	if (os_strncmp(pos, "iface=", 6) == 0) {
8028 		iface_addr = 1;
8029 		pos += 6;
8030 	}
8031 	if (hwaddr_aton(pos, peer))
8032 		return -1;
8033 
8034 	wpas_p2p_remove_client(wpa_s, peer, iface_addr);
8035 	return 0;
8036 }
8037 
8038 
p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant * wpa_s,char * cmd)8039 static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
8040 {
8041 	int freq = 0, period = 0, interval = 0, count = 0;
8042 
8043 	if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
8044 	{
8045 		wpa_printf(MSG_DEBUG,
8046 			   "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
8047 		return -1;
8048 	}
8049 
8050 	return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
8051 }
8052 
8053 
8054 #ifdef CONFIG_TESTING_OPTIONS
p2p_ctrl_pmk_get(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8055 static int p2p_ctrl_pmk_get(struct wpa_supplicant *wpa_s, char *buf,
8056 			    size_t buflen)
8057 {
8058 	const u8 *pmk;
8059 	size_t pmk_len;
8060 
8061 	/* Return the PMK from the first identity entry. This assumes test
8062 	 * cases to remove all indentities at the beginning so that only one
8063 	 * entry is available. */
8064 	if (!wpa_s->conf->identity || !wpa_s->conf->identity->pmk)
8065 		return -1;
8066 
8067 	pmk_len = wpabuf_len(wpa_s->conf->identity->pmk);
8068 	pmk = wpabuf_head(wpa_s->conf->identity->pmk);
8069 
8070 	return wpa_snprintf_hex(buf, buflen, pmk, pmk_len);
8071 }
8072 #endif /* CONFIG_TESTING_OPTIONS */
8073 #endif /* CONFIG_P2P */
8074 
8075 
freq_range_to_channel_list(struct wpa_supplicant * wpa_s,char * val)8076 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
8077 {
8078 	struct wpa_freq_range_list ranges;
8079 	int *freqs = NULL;
8080 	struct hostapd_hw_modes *mode;
8081 	u16 i;
8082 
8083 	if (wpa_s->hw.modes == NULL)
8084 		return NULL;
8085 
8086 	os_memset(&ranges, 0, sizeof(ranges));
8087 	if (freq_range_list_parse(&ranges, val) < 0)
8088 		return NULL;
8089 
8090 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
8091 		int j;
8092 
8093 		mode = &wpa_s->hw.modes[i];
8094 		for (j = 0; j < mode->num_channels; j++) {
8095 			unsigned int freq;
8096 
8097 			if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
8098 				continue;
8099 
8100 			freq = mode->channels[j].freq;
8101 			if (!freq_range_list_includes(&ranges, freq))
8102 				continue;
8103 
8104 			int_array_add_unique(&freqs, freq);
8105 		}
8106 	}
8107 
8108 	os_free(ranges.range);
8109 	return freqs;
8110 }
8111 
8112 
8113 #ifdef CONFIG_INTERWORKING
8114 
ctrl_interworking_select(struct wpa_supplicant * wpa_s,char * param)8115 static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
8116 {
8117 	int auto_sel = 0;
8118 	int *freqs = NULL;
8119 
8120 	if (param) {
8121 		char *pos;
8122 
8123 		auto_sel = os_strstr(param, "auto") != NULL;
8124 
8125 		pos = os_strstr(param, "freq=");
8126 		if (pos) {
8127 			freqs = freq_range_to_channel_list(wpa_s, pos + 5);
8128 			if (freqs == NULL)
8129 				return -1;
8130 		}
8131 
8132 	}
8133 
8134 	return interworking_select(wpa_s, auto_sel, freqs);
8135 }
8136 
8137 
ctrl_interworking_connect(struct wpa_supplicant * wpa_s,char * dst,int only_add)8138 static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
8139 				     int only_add)
8140 {
8141 	u8 bssid[ETH_ALEN];
8142 	struct wpa_bss *bss;
8143 
8144 	if (hwaddr_aton(dst, bssid)) {
8145 		wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
8146 		return -1;
8147 	}
8148 
8149 	bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
8150 	if (bss == NULL) {
8151 		wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
8152 			   MAC2STR(bssid));
8153 		return -1;
8154 	}
8155 
8156 	if (bss->ssid_len == 0) {
8157 		int found = 0;
8158 
8159 		wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
8160 			   " does not have SSID information", MAC2STR(bssid));
8161 
8162 		dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
8163 					 list) {
8164 			if (ether_addr_equal(bss->bssid, bssid) &&
8165 			    bss->ssid_len > 0) {
8166 				found = 1;
8167 				break;
8168 			}
8169 		}
8170 
8171 		if (!found)
8172 			return -1;
8173 		wpa_printf(MSG_DEBUG,
8174 			   "Found another matching BSS entry with SSID");
8175 	}
8176 
8177 	return interworking_connect(wpa_s, bss, only_add);
8178 }
8179 
8180 
get_anqp(struct wpa_supplicant * wpa_s,char * dst)8181 static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
8182 {
8183 	u8 dst_addr[ETH_ALEN];
8184 	int used, freq = 0;
8185 	char *pos;
8186 #define MAX_ANQP_INFO_ID 100
8187 	u16 id[MAX_ANQP_INFO_ID];
8188 	size_t num_id = 0;
8189 	u32 subtypes = 0;
8190 	u32 mbo_subtypes = 0;
8191 
8192 	used = hwaddr_aton2(dst, dst_addr);
8193 	if (used < 0)
8194 		return -1;
8195 	pos = dst + used;
8196 	if (*pos == ' ')
8197 		pos++;
8198 
8199 	if (os_strncmp(pos, "freq=", 5) == 0) {
8200 		freq = atoi(pos + 5);
8201 		pos = os_strchr(pos, ' ');
8202 		if (!pos)
8203 			return -1;
8204 		pos++;
8205 	}
8206 
8207 	while (num_id < MAX_ANQP_INFO_ID) {
8208 		if (os_strncmp(pos, "hs20:", 5) == 0) {
8209 #ifdef CONFIG_HS20
8210 			int num = atoi(pos + 5);
8211 			if (num <= 0 || num > 31)
8212 				return -1;
8213 			subtypes |= BIT(num);
8214 #else /* CONFIG_HS20 */
8215 			return -1;
8216 #endif /* CONFIG_HS20 */
8217 		} else if (os_strncmp(pos, "mbo:", 4) == 0) {
8218 #ifdef CONFIG_MBO
8219 			int num = atoi(pos + 4);
8220 
8221 			if (num <= 0 || num > MAX_MBO_ANQP_SUBTYPE)
8222 				return -1;
8223 			mbo_subtypes |= BIT(num);
8224 #else /* CONFIG_MBO */
8225 			return -1;
8226 #endif /* CONFIG_MBO */
8227 		} else {
8228 			id[num_id] = atoi(pos);
8229 			if (id[num_id])
8230 				num_id++;
8231 		}
8232 		pos = os_strchr(pos + 1, ',');
8233 		if (pos == NULL)
8234 			break;
8235 		pos++;
8236 	}
8237 
8238 	if (num_id == 0 && !subtypes && !mbo_subtypes)
8239 		return -1;
8240 
8241 	return anqp_send_req(wpa_s, dst_addr, freq, id, num_id, subtypes,
8242 			     mbo_subtypes);
8243 }
8244 
8245 
gas_request(struct wpa_supplicant * wpa_s,char * cmd)8246 static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
8247 {
8248 	u8 dst_addr[ETH_ALEN];
8249 	struct wpabuf *advproto, *query = NULL;
8250 	int used, ret = -1;
8251 	char *pos, *end;
8252 	size_t len;
8253 
8254 	used = hwaddr_aton2(cmd, dst_addr);
8255 	if (used < 0)
8256 		return -1;
8257 
8258 	pos = cmd + used;
8259 	while (*pos == ' ')
8260 		pos++;
8261 
8262 	/* Advertisement Protocol ID */
8263 	end = os_strchr(pos, ' ');
8264 	if (end)
8265 		len = end - pos;
8266 	else
8267 		len = os_strlen(pos);
8268 	if (len & 0x01)
8269 		return -1;
8270 	len /= 2;
8271 	if (len == 0)
8272 		return -1;
8273 	advproto = wpabuf_alloc(len);
8274 	if (advproto == NULL)
8275 		return -1;
8276 	if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
8277 		goto fail;
8278 
8279 	if (end) {
8280 		/* Optional Query Request */
8281 		pos = end + 1;
8282 		while (*pos == ' ')
8283 			pos++;
8284 
8285 		len = os_strlen(pos);
8286 		if (len) {
8287 			if (len & 0x01)
8288 				goto fail;
8289 			len /= 2;
8290 			if (len == 0)
8291 				goto fail;
8292 			query = wpabuf_alloc(len);
8293 			if (query == NULL)
8294 				goto fail;
8295 			if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
8296 				goto fail;
8297 		}
8298 	}
8299 
8300 	ret = gas_send_request(wpa_s, dst_addr, advproto, query);
8301 
8302 fail:
8303 	wpabuf_free(advproto);
8304 	wpabuf_free(query);
8305 
8306 	return ret;
8307 }
8308 
8309 
gas_response_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8310 static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
8311 			    size_t buflen)
8312 {
8313 	u8 addr[ETH_ALEN];
8314 	int dialog_token;
8315 	int used;
8316 	char *pos;
8317 	size_t resp_len, start, requested_len;
8318 	struct wpabuf *resp;
8319 	int ret;
8320 
8321 	used = hwaddr_aton2(cmd, addr);
8322 	if (used < 0)
8323 		return -1;
8324 
8325 	pos = cmd + used;
8326 	while (*pos == ' ')
8327 		pos++;
8328 	dialog_token = atoi(pos);
8329 
8330 	if (wpa_s->last_gas_resp &&
8331 	    ether_addr_equal(addr, wpa_s->last_gas_addr) &&
8332 	    dialog_token == wpa_s->last_gas_dialog_token)
8333 		resp = wpa_s->last_gas_resp;
8334 	else if (wpa_s->prev_gas_resp &&
8335 		 ether_addr_equal(addr, wpa_s->prev_gas_addr) &&
8336 		 dialog_token == wpa_s->prev_gas_dialog_token)
8337 		resp = wpa_s->prev_gas_resp;
8338 	else
8339 		return -1;
8340 
8341 	resp_len = wpabuf_len(resp);
8342 	start = 0;
8343 	requested_len = resp_len;
8344 
8345 	pos = os_strchr(pos, ' ');
8346 	if (pos) {
8347 		start = atoi(pos);
8348 		if (start > resp_len)
8349 			return os_snprintf(buf, buflen, "FAIL-Invalid range");
8350 		pos = os_strchr(pos, ',');
8351 		if (pos == NULL)
8352 			return -1;
8353 		pos++;
8354 		requested_len = atoi(pos);
8355 		if (start + requested_len > resp_len)
8356 			return os_snprintf(buf, buflen, "FAIL-Invalid range");
8357 	}
8358 
8359 	if (requested_len * 2 + 1 > buflen)
8360 		return os_snprintf(buf, buflen, "FAIL-Too long response");
8361 
8362 	ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
8363 			       requested_len);
8364 
8365 	if (start + requested_len == resp_len) {
8366 		/*
8367 		 * Free memory by dropping the response after it has been
8368 		 * fetched.
8369 		 */
8370 		if (resp == wpa_s->prev_gas_resp) {
8371 			wpabuf_free(wpa_s->prev_gas_resp);
8372 			wpa_s->prev_gas_resp = NULL;
8373 		} else {
8374 			wpabuf_free(wpa_s->last_gas_resp);
8375 			wpa_s->last_gas_resp = NULL;
8376 		}
8377 	}
8378 
8379 	return ret;
8380 }
8381 #endif /* CONFIG_INTERWORKING */
8382 
8383 
8384 #ifdef CONFIG_HS20
8385 
get_hs20_anqp(struct wpa_supplicant * wpa_s,char * dst)8386 static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
8387 {
8388 	u8 dst_addr[ETH_ALEN];
8389 	int used;
8390 	char *pos;
8391 	u32 subtypes = 0;
8392 
8393 	used = hwaddr_aton2(dst, dst_addr);
8394 	if (used < 0)
8395 		return -1;
8396 	pos = dst + used;
8397 	if (*pos == ' ')
8398 		pos++;
8399 	for (;;) {
8400 		int num = atoi(pos);
8401 		if (num <= 0 || num > 31)
8402 			return -1;
8403 		subtypes |= BIT(num);
8404 		pos = os_strchr(pos + 1, ',');
8405 		if (pos == NULL)
8406 			break;
8407 		pos++;
8408 	}
8409 
8410 	if (subtypes == 0)
8411 		return -1;
8412 
8413 	return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
8414 }
8415 
8416 
hs20_nai_home_realm_list(struct wpa_supplicant * wpa_s,const u8 * addr,const char * realm)8417 static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
8418 				    const u8 *addr, const char *realm)
8419 {
8420 	u8 *buf;
8421 	size_t rlen, len;
8422 	int ret;
8423 
8424 	rlen = os_strlen(realm);
8425 	len = 3 + rlen;
8426 	buf = os_malloc(len);
8427 	if (buf == NULL)
8428 		return -1;
8429 	buf[0] = 1; /* NAI Home Realm Count */
8430 	buf[1] = 0; /* Formatted in accordance with RFC 4282 */
8431 	buf[2] = rlen;
8432 	os_memcpy(buf + 3, realm, rlen);
8433 
8434 	ret = hs20_anqp_send_req(wpa_s, addr,
8435 				 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
8436 				 buf, len);
8437 
8438 	os_free(buf);
8439 
8440 	return ret;
8441 }
8442 
8443 
hs20_get_nai_home_realm_list(struct wpa_supplicant * wpa_s,char * dst)8444 static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
8445 					char *dst)
8446 {
8447 	struct wpa_cred *cred = wpa_s->conf->cred;
8448 	u8 dst_addr[ETH_ALEN];
8449 	int used;
8450 	u8 *buf;
8451 	size_t len;
8452 	int ret;
8453 
8454 	used = hwaddr_aton2(dst, dst_addr);
8455 	if (used < 0)
8456 		return -1;
8457 
8458 	while (dst[used] == ' ')
8459 		used++;
8460 	if (os_strncmp(dst + used, "realm=", 6) == 0)
8461 		return hs20_nai_home_realm_list(wpa_s, dst_addr,
8462 						dst + used + 6);
8463 
8464 	len = os_strlen(dst + used);
8465 
8466 	if (len == 0 && cred && cred->realm)
8467 		return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
8468 
8469 	if (len & 1)
8470 		return -1;
8471 	len /= 2;
8472 	buf = os_malloc(len);
8473 	if (buf == NULL)
8474 		return -1;
8475 	if (hexstr2bin(dst + used, buf, len) < 0) {
8476 		os_free(buf);
8477 		return -1;
8478 	}
8479 
8480 	ret = hs20_anqp_send_req(wpa_s, dst_addr,
8481 				 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
8482 				 buf, len);
8483 	os_free(buf);
8484 
8485 	return ret;
8486 }
8487 
8488 #endif /* CONFIG_HS20 */
8489 
8490 
8491 #ifdef CONFIG_AUTOSCAN
8492 
wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant * wpa_s,char * cmd)8493 static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
8494 					      char *cmd)
8495 {
8496 	enum wpa_states state = wpa_s->wpa_state;
8497 	char *new_params = NULL;
8498 
8499 	if (os_strlen(cmd) > 0) {
8500 		new_params = os_strdup(cmd);
8501 		if (new_params == NULL)
8502 			return -1;
8503 	}
8504 
8505 	os_free(wpa_s->conf->autoscan);
8506 	wpa_s->conf->autoscan = new_params;
8507 
8508 	if (wpa_s->conf->autoscan == NULL)
8509 		autoscan_deinit(wpa_s);
8510 	else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
8511 		autoscan_init(wpa_s, 1);
8512 	else if (state == WPA_SCANNING)
8513 		wpa_supplicant_reinit_autoscan(wpa_s);
8514 	else
8515 		wpa_printf(MSG_DEBUG, "No autoscan update in state %s",
8516 			   wpa_supplicant_state_txt(state));
8517 
8518 	return 0;
8519 }
8520 
8521 #endif /* CONFIG_AUTOSCAN */
8522 
8523 
8524 #ifdef CONFIG_WNM
8525 
wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant * wpa_s,char * cmd)8526 static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
8527 {
8528 	int enter;
8529 	int intval = 0;
8530 	char *pos;
8531 	int ret;
8532 	struct wpabuf *tfs_req = NULL;
8533 
8534 	if (os_strncmp(cmd, "enter", 5) == 0)
8535 		enter = 1;
8536 	else if (os_strncmp(cmd, "exit", 4) == 0)
8537 		enter = 0;
8538 	else
8539 		return -1;
8540 
8541 	pos = os_strstr(cmd, " interval=");
8542 	if (pos)
8543 		intval = atoi(pos + 10);
8544 
8545 	pos = os_strstr(cmd, " tfs_req=");
8546 	if (pos) {
8547 		char *end;
8548 		size_t len;
8549 		pos += 9;
8550 		end = os_strchr(pos, ' ');
8551 		if (end)
8552 			len = end - pos;
8553 		else
8554 			len = os_strlen(pos);
8555 		if (len & 1)
8556 			return -1;
8557 		len /= 2;
8558 		tfs_req = wpabuf_alloc(len);
8559 		if (tfs_req == NULL)
8560 			return -1;
8561 		if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
8562 			wpabuf_free(tfs_req);
8563 			return -1;
8564 		}
8565 	}
8566 
8567 	ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
8568 					   WNM_SLEEP_MODE_EXIT, intval,
8569 					   tfs_req);
8570 	wpabuf_free(tfs_req);
8571 
8572 	return ret;
8573 }
8574 
8575 
wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant * wpa_s,char * cmd)8576 static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
8577 {
8578 	int query_reason, list = 0;
8579 	char *btm_candidates = NULL;
8580 
8581 	query_reason = atoi(cmd);
8582 
8583 	cmd = os_strchr(cmd, ' ');
8584 	if (cmd) {
8585 		if (os_strncmp(cmd, " list", 5) == 0)
8586 			list = 1;
8587 		else
8588 			btm_candidates = cmd;
8589 	}
8590 
8591 	wpa_printf(MSG_DEBUG,
8592 		   "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
8593 		   query_reason, list ? " candidate list" : "");
8594 
8595 	return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason,
8596 						  btm_candidates,
8597 						  list);
8598 }
8599 
8600 
wpas_ctrl_iface_coloc_intf_report(struct wpa_supplicant * wpa_s,char * cmd)8601 static int wpas_ctrl_iface_coloc_intf_report(struct wpa_supplicant *wpa_s,
8602 					     char *cmd)
8603 {
8604 	struct wpabuf *elems;
8605 	int ret;
8606 
8607 	elems = wpabuf_parse_bin(cmd);
8608 	if (!elems)
8609 		return -1;
8610 
8611 	ret = wnm_send_coloc_intf_report(wpa_s, 0, elems);
8612 	wpabuf_free(elems);
8613 	return ret;
8614 }
8615 
8616 #endif /* CONFIG_WNM */
8617 
8618 
wpa_supplicant_signal_poll(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8619 static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
8620 				      size_t buflen)
8621 {
8622 	struct wpa_signal_info si;
8623 	int ret;
8624 	char *pos, *end;
8625 
8626 	ret = wpa_drv_signal_poll(wpa_s, &si);
8627 	if (ret)
8628 		return -1;
8629 
8630 	pos = buf;
8631 	end = buf + buflen;
8632 
8633 	ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%lu\n"
8634 			  "NOISE=%d\nFREQUENCY=%u\n",
8635 			  si.data.signal, si.data.current_tx_rate / 1000,
8636 			  si.current_noise, si.frequency);
8637 	if (os_snprintf_error(end - pos, ret))
8638 		return -1;
8639 	pos += ret;
8640 
8641 	if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
8642 		ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
8643 				  channel_width_to_string(si.chanwidth));
8644 		if (os_snprintf_error(end - pos, ret))
8645 			return -1;
8646 		pos += ret;
8647 	}
8648 
8649 	if (si.center_frq1 > 0) {
8650 		ret = os_snprintf(pos, end - pos, "CENTER_FRQ1=%d\n",
8651 				  si.center_frq1);
8652 		if (os_snprintf_error(end - pos, ret))
8653 			return -1;
8654 		pos += ret;
8655 	}
8656 
8657 	if (si.center_frq2 > 0) {
8658 		ret = os_snprintf(pos, end - pos, "CENTER_FRQ2=%d\n",
8659 				  si.center_frq2);
8660 		if (os_snprintf_error(end - pos, ret))
8661 			return -1;
8662 		pos += ret;
8663 	}
8664 
8665 	if (si.data.avg_signal) {
8666 		ret = os_snprintf(pos, end - pos,
8667 				  "AVG_RSSI=%d\n", si.data.avg_signal);
8668 		if (os_snprintf_error(end - pos, ret))
8669 			return -1;
8670 		pos += ret;
8671 	}
8672 
8673 	if (si.data.avg_beacon_signal) {
8674 		ret = os_snprintf(pos, end - pos,
8675 				  "AVG_BEACON_RSSI=%d\n",
8676 				  si.data.avg_beacon_signal);
8677 		if (os_snprintf_error(end - pos, ret))
8678 			return -1;
8679 		pos += ret;
8680 	}
8681 
8682 	return pos - buf;
8683 }
8684 
8685 
wpas_ctrl_iface_signal_monitor(struct wpa_supplicant * wpa_s,const char * cmd)8686 static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
8687 					  const char *cmd)
8688 {
8689 	const char *pos;
8690 	int threshold = 0;
8691 	int hysteresis = 0;
8692 
8693 	if (wpa_s->bgscan && wpa_s->bgscan_priv) {
8694 		wpa_printf(MSG_DEBUG,
8695 			   "Reject SIGNAL_MONITOR command - bgscan is active");
8696 		return -1;
8697 	}
8698 	pos = os_strstr(cmd, "THRESHOLD=");
8699 	if (pos)
8700 		threshold = atoi(pos + 10);
8701 	pos = os_strstr(cmd, "HYSTERESIS=");
8702 	if (pos)
8703 		hysteresis = atoi(pos + 11);
8704 	return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
8705 }
8706 
8707 
8708 #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)8709 int wpas_ctrl_iface_get_pref_freq_list_override(struct wpa_supplicant *wpa_s,
8710 						enum wpa_driver_if_type if_type,
8711 						unsigned int *num,
8712 						struct weighted_pcl *freq_list)
8713 {
8714 	char *pos = wpa_s->get_pref_freq_list_override;
8715 	char *end;
8716 	unsigned int count = 0;
8717 
8718 	/* Override string format:
8719 	 *  <if_type1>:<freq1>,<freq2>,... <if_type2>:... */
8720 
8721 	while (pos) {
8722 		if (atoi(pos) == (int) if_type)
8723 			break;
8724 		pos = os_strchr(pos, ' ');
8725 		if (pos)
8726 			pos++;
8727 	}
8728 	if (!pos)
8729 		return -1;
8730 	pos = os_strchr(pos, ':');
8731 	if (!pos)
8732 		return -1;
8733 	pos++;
8734 	end = os_strchr(pos, ' ');
8735 	while (pos && (!end || pos < end) && count < *num) {
8736 		freq_list[count].freq = atoi(pos);
8737 		freq_list[count++].flag = WEIGHTED_PCL_GO | WEIGHTED_PCL_CLI;
8738 		pos = os_strchr(pos, ',');
8739 		if (pos)
8740 			pos++;
8741 	}
8742 
8743 	*num = count;
8744 	return 0;
8745 }
8746 #endif /* CONFIG_TESTING_OPTIONS */
8747 
8748 
wpas_ctrl_iface_get_pref_freq_list(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8749 static int wpas_ctrl_iface_get_pref_freq_list(
8750 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
8751 {
8752 	unsigned int num = 100, i;
8753 	int ret;
8754 	enum wpa_driver_if_type iface_type;
8755 	char *pos, *end;
8756 	struct weighted_pcl freq_list[100];
8757 
8758 	pos = buf;
8759 	end = buf + buflen;
8760 
8761 	/* buf: "<interface_type>" */
8762 	if (os_strcmp(cmd, "STATION") == 0)
8763 		iface_type = WPA_IF_STATION;
8764 	else if (os_strcmp(cmd, "AP") == 0)
8765 		iface_type = WPA_IF_AP_BSS;
8766 	else if (os_strcmp(cmd, "P2P_GO") == 0)
8767 		iface_type = WPA_IF_P2P_GO;
8768 	else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
8769 		iface_type = WPA_IF_P2P_CLIENT;
8770 	else if (os_strcmp(cmd, "IBSS") == 0)
8771 		iface_type = WPA_IF_IBSS;
8772 	else if (os_strcmp(cmd, "TDLS") == 0)
8773 		iface_type = WPA_IF_TDLS;
8774 	else
8775 		return -1;
8776 
8777 	wpa_printf(MSG_DEBUG,
8778 		   "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
8779 		   iface_type, cmd);
8780 
8781 	ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
8782 	if (ret)
8783 		return -1;
8784 
8785 	for (i = 0; i < num; i++) {
8786 		ret = os_snprintf(pos, end - pos, "%s%u",
8787 				  i > 0 ? "," : "", freq_list[i].freq);
8788 		if (os_snprintf_error(end - pos, ret))
8789 			return -1;
8790 		pos += ret;
8791 	}
8792 
8793 	return pos - buf;
8794 }
8795 
8796 
wpas_ctrl_iface_driver_flags(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8797 static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
8798 					char *buf, size_t buflen)
8799 {
8800 	int ret, i;
8801 	char *pos, *end;
8802 
8803 	ret = os_snprintf(buf, buflen, "%016llX:\n",
8804 			  (long long unsigned) wpa_s->drv_flags);
8805 	if (os_snprintf_error(buflen, ret))
8806 		return -1;
8807 
8808 	pos = buf + ret;
8809 	end = buf + buflen;
8810 
8811 	for (i = 0; i < 64; i++) {
8812 		if (wpa_s->drv_flags & (1LLU << i)) {
8813 			ret = os_snprintf(pos, end - pos, "%s\n",
8814 					  driver_flag_to_string(1LLU << i));
8815 			if (os_snprintf_error(end - pos, ret))
8816 				return -1;
8817 			pos += ret;
8818 		}
8819 	}
8820 
8821 	return pos - buf;
8822 }
8823 
8824 
wpas_ctrl_iface_driver_flags2(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8825 static int wpas_ctrl_iface_driver_flags2(struct wpa_supplicant *wpa_s,
8826 					 char *buf, size_t buflen)
8827 {
8828 	int ret, i;
8829 	char *pos, *end;
8830 
8831 	ret = os_snprintf(buf, buflen, "%016llX:\n",
8832 			  (long long unsigned) wpa_s->drv_flags2);
8833 	if (os_snprintf_error(buflen, ret))
8834 		return -1;
8835 
8836 	pos = buf + ret;
8837 	end = buf + buflen;
8838 
8839 	for (i = 0; i < 64; i++) {
8840 		if (wpa_s->drv_flags2 & (1LLU << i)) {
8841 			ret = os_snprintf(pos, end - pos, "%s\n",
8842 					  driver_flag2_to_string(1LLU << i));
8843 			if (os_snprintf_error(end - pos, ret))
8844 				return -1;
8845 			pos += ret;
8846 		}
8847 	}
8848 
8849 	return pos - buf;
8850 }
8851 
8852 
wpa_supplicant_pktcnt_poll(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8853 static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
8854 				      size_t buflen)
8855 {
8856 	struct hostap_sta_driver_data sta;
8857 	int ret;
8858 
8859 	ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
8860 	if (ret)
8861 		return -1;
8862 
8863 	ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
8864 			  sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
8865 	if (os_snprintf_error(buflen, ret))
8866 		return -1;
8867 	return ret;
8868 }
8869 
8870 
8871 #ifdef ANDROID
wpa_supplicant_driver_cmd(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8872 static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
8873 				     char *buf, size_t buflen)
8874 {
8875 	int ret;
8876 
8877 	ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
8878 	if (ret == 0) {
8879 #ifdef CONFIG_P2P
8880 		if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
8881 			struct p2p_data *p2p = wpa_s->global->p2p;
8882 			if (p2p) {
8883 				char country[3];
8884 				country[0] = cmd[8];
8885 				country[1] = cmd[9];
8886 				country[2] = 0x04;
8887 				p2p_set_country(p2p, country);
8888 			}
8889 		}
8890 #endif /* CONFIG_P2P */
8891 		ret = os_snprintf(buf, buflen, "%s\n", "OK");
8892 		if (os_snprintf_error(buflen, ret))
8893 			ret = -1;
8894 	}
8895 	return ret;
8896 }
8897 #endif /* ANDROID */
8898 
8899 
wpa_supplicant_vendor_cmd(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8900 static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
8901 				     char *buf, size_t buflen)
8902 {
8903 	int ret;
8904 	char *pos, *temp = NULL;
8905 	u8 *data = NULL;
8906 	unsigned int vendor_id, subcmd;
8907 	enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
8908 	struct wpabuf *reply;
8909 	size_t data_len = 0;
8910 
8911 	/**
8912 	 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
8913 	 * [nested=<0|1>]
8914 	 */
8915 	vendor_id = strtoul(cmd, &pos, 16);
8916 	if (!isblank((unsigned char) *pos))
8917 		return -EINVAL;
8918 
8919 	subcmd = strtoul(pos, &pos, 10);
8920 
8921 	if (*pos != '\0') {
8922 		if (!isblank((unsigned char) *pos++))
8923 			return -EINVAL;
8924 
8925 		temp = os_strchr(pos, ' ');
8926 		data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
8927 	}
8928 
8929 	if (data_len) {
8930 		data_len /= 2;
8931 		data = os_malloc(data_len);
8932 		if (!data)
8933 			return -1;
8934 
8935 		if (hexstr2bin(pos, data, data_len)) {
8936 			wpa_printf(MSG_DEBUG,
8937 				   "Vendor command: wrong parameter format");
8938 			os_free(data);
8939 			return -EINVAL;
8940 		}
8941 	}
8942 
8943 	pos = os_strstr(cmd, "nested=");
8944 	if (pos)
8945 		nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
8946 			NESTED_ATTR_NOT_USED;
8947 
8948 	reply = wpabuf_alloc((buflen - 1) / 2);
8949 	if (!reply) {
8950 		os_free(data);
8951 		return -1;
8952 	}
8953 
8954 	ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
8955 				 nested_attr_flag, reply);
8956 
8957 	if (ret == 0)
8958 		ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
8959 				       wpabuf_len(reply));
8960 
8961 	wpabuf_free(reply);
8962 	os_free(data);
8963 
8964 	return ret;
8965 }
8966 
8967 
wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant * wpa_s)8968 static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
8969 {
8970 #ifdef CONFIG_P2P
8971 	struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
8972 		wpa_s->global->p2p_init_wpa_s : wpa_s;
8973 #endif /* CONFIG_P2P */
8974 
8975 	wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
8976 
8977 	if (wpas_abort_ongoing_scan(wpa_s) == 0)
8978 		wpa_s->ignore_post_flush_scan_res = 1;
8979 
8980 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
8981 		/*
8982 		 * Avoid possible auto connect re-connection on getting
8983 		 * disconnected due to state flush.
8984 		 */
8985 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
8986 	}
8987 
8988 #ifdef CONFIG_P2P
8989 	wpas_p2p_group_remove(p2p_wpa_s, "*");
8990 	wpas_p2p_cancel(p2p_wpa_s);
8991 	p2p_ctrl_flush(p2p_wpa_s);
8992 	wpas_p2p_service_flush(p2p_wpa_s);
8993 	p2p_wpa_s->global->p2p_disabled = 0;
8994 	p2p_wpa_s->global->p2p_per_sta_psk = 0;
8995 	p2p_wpa_s->conf->num_sec_device_types = 0;
8996 	p2p_wpa_s->p2p_disable_ip_addr_req = 0;
8997 	os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
8998 	p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
8999 	p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
9000 	p2p_wpa_s->global->pending_p2ps_group = 0;
9001 	p2p_wpa_s->global->pending_p2ps_group_freq = 0;
9002 #endif /* CONFIG_P2P */
9003 
9004 #ifdef CONFIG_WPS_TESTING
9005 	wps_version_number = 0x20;
9006 	wps_testing_stub_cred = 0;
9007 	wps_corrupt_pkhash = 0;
9008 	wps_force_auth_types_in_use = 0;
9009 	wps_force_encr_types_in_use = 0;
9010 #endif /* CONFIG_WPS_TESTING */
9011 #ifdef CONFIG_WPS
9012 	wpa_s->wps_fragment_size = 0;
9013 	wpas_wps_cancel(wpa_s);
9014 	wps_registrar_flush(wpa_s->wps->registrar);
9015 #endif /* CONFIG_WPS */
9016 	wpa_s->after_wps = 0;
9017 	wpa_s->known_wps_freq = 0;
9018 
9019 #ifdef CONFIG_DPP
9020 	wpas_dpp_deinit(wpa_s);
9021 	wpa_s->dpp_init_max_tries = 0;
9022 	wpa_s->dpp_init_retry_time = 0;
9023 	wpa_s->dpp_resp_wait_time = 0;
9024 	wpa_s->dpp_resp_max_tries = 0;
9025 	wpa_s->dpp_resp_retry_time = 0;
9026 #ifdef CONFIG_DPP2
9027 	wpas_dpp_chirp_stop(wpa_s);
9028 	wpa_s->dpp_pfs_fallback = 0;
9029 #endif /* CONFIG_DPP2 */
9030 #ifdef CONFIG_DPP3
9031 	{
9032 		int i;
9033 
9034 		for (i = 0; i < DPP_PB_INFO_COUNT; i++) {
9035 			struct dpp_pb_info *info;
9036 
9037 			info = &wpa_s->dpp_pb[i];
9038 			info->rx_time.sec = 0;
9039 			info->rx_time.usec = 0;
9040 		}
9041 	}
9042 #endif /* CONFIG_DPP3 */
9043 #ifdef CONFIG_TESTING_OPTIONS
9044 	os_memset(dpp_pkex_own_mac_override, 0, ETH_ALEN);
9045 	os_memset(dpp_pkex_peer_mac_override, 0, ETH_ALEN);
9046 	dpp_pkex_ephemeral_key_override_len = 0;
9047 	dpp_protocol_key_override_len = 0;
9048 	dpp_nonce_override_len = 0;
9049 #ifdef CONFIG_DPP3
9050 	dpp_version_override = 3;
9051 #elif defined(CONFIG_DPP2)
9052 	dpp_version_override = 2;
9053 #else /* CONFIG_DPP2 */
9054 	dpp_version_override = 1;
9055 #endif /* CONFIG_DPP2 */
9056 #endif /* CONFIG_TESTING_OPTIONS */
9057 #endif /* CONFIG_DPP */
9058 
9059 #ifdef CONFIG_TDLS
9060 #ifdef CONFIG_TDLS_TESTING
9061 	tdls_testing = 0;
9062 #endif /* CONFIG_TDLS_TESTING */
9063 	wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
9064 	wpa_tdls_enable(wpa_s->wpa, 1);
9065 #endif /* CONFIG_TDLS */
9066 
9067 	eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
9068 	wpa_supplicant_stop_countermeasures(wpa_s, NULL);
9069 	wpa_s->last_michael_mic_error.sec = 0;
9070 
9071 	wpa_s->no_keep_alive = 0;
9072 	wpa_s->own_disconnect_req = 0;
9073 	wpa_s->own_reconnect_req = 0;
9074 	wpa_s->deny_ptk0_rekey = 0;
9075 
9076 	os_free(wpa_s->disallow_aps_bssid);
9077 	wpa_s->disallow_aps_bssid = NULL;
9078 	wpa_s->disallow_aps_bssid_count = 0;
9079 	os_free(wpa_s->disallow_aps_ssid);
9080 	wpa_s->disallow_aps_ssid = NULL;
9081 	wpa_s->disallow_aps_ssid_count = 0;
9082 
9083 	wpa_s->set_sta_uapsd = 0;
9084 	wpa_s->sta_uapsd = 0;
9085 
9086 	wpa_s->consecutive_conn_failures = 0;
9087 
9088 	wpa_drv_radio_disable(wpa_s, 0);
9089 	wpa_bssid_ignore_clear(wpa_s);
9090 	wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
9091 	wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
9092 	wpa_config_flush_blobs(wpa_s->conf);
9093 	wpa_s->conf->auto_interworking = 0;
9094 	wpa_s->conf->okc = 0;
9095 
9096 	ptksa_cache_flush(wpa_s->ptksa, NULL, WPA_CIPHER_NONE);
9097 	wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
9098 	rsn_preauth_deinit(wpa_s->wpa);
9099 
9100 	wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
9101 	wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
9102 	wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
9103 	eapol_sm_notify_logoff(wpa_s->eapol, false);
9104 
9105 	radio_remove_works(wpa_s, NULL, 1);
9106 	wpa_s->ext_work_in_progress = 0;
9107 
9108 	wpa_s->next_ssid = NULL;
9109 
9110 	wnm_btm_reset(wpa_s);
9111 
9112 	wpa_s->ext_mgmt_frame_handling = 0;
9113 	wpa_s->ext_eapol_frame_io = 0;
9114 #ifdef CONFIG_TESTING_OPTIONS
9115 	wpa_s->extra_roc_dur = 0;
9116 	wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
9117 	wpa_s->p2p_go_csa_on_inv = 0;
9118 	wpa_s->ignore_auth_resp = 0;
9119 	wpa_s->ignore_assoc_disallow = 0;
9120 	wpa_s->disable_sa_query = 0;
9121 	wpa_s->testing_resend_assoc = 0;
9122 	wpa_s->ignore_sae_h2e_only = 0;
9123 	wpa_s->ft_rsnxe_used = 0;
9124 	wpa_s->reject_btm_req_reason = 0;
9125 	wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
9126 	wpa_sm_set_test_eapol_m2_elems(wpa_s->wpa, NULL);
9127 	wpa_sm_set_test_eapol_m4_elems(wpa_s->wpa, NULL);
9128 	wpa_sm_set_test_rsnxe_data(wpa_s->wpa, NULL, NULL);
9129 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_ENCRYPT_EAPOL_M2, 0);
9130 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_ENCRYPT_EAPOL_M4, 0);
9131 	os_free(wpa_s->get_pref_freq_list_override);
9132 	wpa_s->get_pref_freq_list_override = NULL;
9133 	wpabuf_free(wpa_s->sae_commit_override);
9134 	wpa_s->sae_commit_override = NULL;
9135 	os_free(wpa_s->extra_sae_rejected_groups);
9136 	wpa_s->extra_sae_rejected_groups = NULL;
9137 	wpabuf_free(wpa_s->rsne_override_eapol);
9138 	wpa_s->rsne_override_eapol = NULL;
9139 	wpabuf_free(wpa_s->rsnxe_override_assoc);
9140 	wpa_s->rsnxe_override_assoc = NULL;
9141 	wpabuf_free(wpa_s->rsnxe_override_eapol);
9142 	wpa_s->rsnxe_override_eapol = NULL;
9143 	wpas_clear_driver_signal_override(wpa_s);
9144 	{
9145 		int i;
9146 
9147 		for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
9148 			wpabuf_free(wpa_s->link_ies[i]);
9149 			wpa_s->link_ies[i] = NULL;
9150 		}
9151 	}
9152 #ifndef CONFIG_NO_ROBUST_AV
9153 	wpa_s->disable_scs_support = 0;
9154 	wpa_s->disable_mscs_support = 0;
9155 	wpa_s->enable_dscp_policy_capa = 0;
9156 #endif /* CONFIG_NO_ROBUST_AV */
9157 	wpa_s->oci_freq_override_eapol = 0;
9158 	wpa_s->oci_freq_override_saquery_req = 0;
9159 	wpa_s->oci_freq_override_saquery_resp = 0;
9160 	wpa_s->oci_freq_override_eapol_g2 = 0;
9161 	wpa_s->oci_freq_override_ft_assoc = 0;
9162 	wpa_s->oci_freq_override_fils_assoc = 0;
9163 	wpa_s->oci_freq_override_wnm_sleep = 0;
9164 	wpa_s->disable_eapol_g2_tx = 0;
9165 	wpa_s->eapol_2_key_info_set_mask = 0;
9166 	wpa_s->test_assoc_comeback_type = -1;
9167 #ifdef CONFIG_DPP
9168 	os_free(wpa_s->dpp_config_obj_override);
9169 	wpa_s->dpp_config_obj_override = NULL;
9170 	os_free(wpa_s->dpp_discovery_override);
9171 	wpa_s->dpp_discovery_override = NULL;
9172 	os_free(wpa_s->dpp_groups_override);
9173 	wpa_s->dpp_groups_override = NULL;
9174 	wpa_s->dpp_ignore_netaccesskey_mismatch = 0;
9175 	wpa_s->dpp_discard_public_action = 0;
9176 	dpp_test = DPP_TEST_DISABLED;
9177 #endif /* CONFIG_DPP */
9178 #endif /* CONFIG_TESTING_OPTIONS */
9179 
9180 	wpa_s->disconnected = 0;
9181 	os_free(wpa_s->next_scan_freqs);
9182 	wpa_s->next_scan_freqs = NULL;
9183 	os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
9184 	wpa_s->next_scan_bssid_wildcard_ssid = 0;
9185 	os_free(wpa_s->select_network_scan_freqs);
9186 	wpa_s->select_network_scan_freqs = NULL;
9187 #ifndef CONFIG_NO_ROBUST_AV
9188 	os_memset(&wpa_s->robust_av, 0, sizeof(struct robust_av_data));
9189 #endif /* CONFIG_NO_ROBUST_AV */
9190 
9191 	wpa_bss_flush(wpa_s);
9192 	if (!dl_list_empty(&wpa_s->bss)) {
9193 		wpa_printf(MSG_DEBUG,
9194 			   "BSS table not empty after flush: %u entries, current_bss=%p bssid="
9195 			   MACSTR " pending_bssid=" MACSTR,
9196 			   dl_list_len(&wpa_s->bss), wpa_s->current_bss,
9197 			   MAC2STR(wpa_s->bssid),
9198 			   MAC2STR(wpa_s->pending_bssid));
9199 	}
9200 
9201 	eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
9202 	wpa_s->wnmsleep_used = 0;
9203 
9204 #ifdef CONFIG_SME
9205 	wpa_s->sme.last_unprot_disconnect.sec = 0;
9206 	wpa_s->sme.auth_alg = 0;
9207 #endif /* CONFIG_SME */
9208 
9209 	wpabuf_free(wpa_s->ric_ies);
9210 	wpa_s->ric_ies = NULL;
9211 
9212 	wpa_supplicant_update_channel_list(wpa_s, NULL);
9213 
9214 	free_bss_tmp_disallowed(wpa_s);
9215 
9216 #ifndef CONFIG_NO_ROBUST_AV
9217 	os_memset(&wpa_s->robust_av, 0, sizeof(struct robust_av_data));
9218 #endif /* CONFIG_NO_ROBUST_AV */
9219 
9220 #ifdef CONFIG_PASN
9221 	wpas_pasn_auth_stop(wpa_s);
9222 #endif /* CONFIG_PASN */
9223 
9224 	if (wpa_s->mac_addr_changed && wpa_s->conf->mac_addr == 0)
9225 		wpas_restore_permanent_mac_addr(wpa_s);
9226 
9227 	wpa_s->conf->ignore_old_scan_res = 0;
9228 
9229 	wpas_nan_de_flush(wpa_s);
9230 
9231 	wpas_pr_flush(wpa_s);
9232 }
9233 
9234 
wpas_ctrl_radio_work_show(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)9235 static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
9236 				     char *buf, size_t buflen)
9237 {
9238 	struct wpa_radio_work *work;
9239 	char *pos, *end;
9240 	struct os_reltime now, diff;
9241 
9242 	pos = buf;
9243 	end = buf + buflen;
9244 
9245 	os_get_reltime(&now);
9246 
9247 	dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
9248 	{
9249 		int ret;
9250 
9251 		os_reltime_sub(&now, &work->time, &diff);
9252 		ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
9253 				  work->type, work->wpa_s->ifname, work->freq,
9254 				  work->started, diff.sec, diff.usec);
9255 		if (os_snprintf_error(end - pos, ret))
9256 			break;
9257 		pos += ret;
9258 	}
9259 
9260 	return pos - buf;
9261 }
9262 
9263 
wpas_ctrl_radio_work_timeout(void * eloop_ctx,void * timeout_ctx)9264 static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
9265 {
9266 	struct wpa_radio_work *work = eloop_ctx;
9267 	struct wpa_external_work *ework = work->ctx;
9268 
9269 	wpa_dbg(work->wpa_s, MSG_DEBUG,
9270 		"Timing out external radio work %u (%s)",
9271 		ework->id, work->type);
9272 	wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
9273 	work->wpa_s->ext_work_in_progress = 0;
9274 	radio_work_done(work);
9275 	os_free(ework);
9276 }
9277 
9278 
wpas_ctrl_radio_work_cb(struct wpa_radio_work * work,int deinit)9279 static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
9280 {
9281 	struct wpa_external_work *ework = work->ctx;
9282 
9283 	if (deinit) {
9284 		if (work->started)
9285 			eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
9286 					     work, NULL);
9287 
9288 		/*
9289 		 * work->type points to a buffer in ework, so need to replace
9290 		 * that here with a fixed string to avoid use of freed memory
9291 		 * in debug prints.
9292 		 */
9293 		work->type = "freed-ext-work";
9294 		work->ctx = NULL;
9295 		os_free(ework);
9296 		return;
9297 	}
9298 
9299 	wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
9300 		ework->id, ework->type);
9301 	wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
9302 	work->wpa_s->ext_work_in_progress = 1;
9303 	if (!ework->timeout)
9304 		ework->timeout = 10;
9305 	eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
9306 			       work, NULL);
9307 }
9308 
9309 
wpas_ctrl_radio_work_add(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)9310 static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
9311 				    char *buf, size_t buflen)
9312 {
9313 	struct wpa_external_work *ework;
9314 	char *pos, *pos2;
9315 	size_t type_len;
9316 	int ret;
9317 	unsigned int freq = 0;
9318 
9319 	/* format: <name> [freq=<MHz>] [timeout=<seconds>] */
9320 
9321 	ework = os_zalloc(sizeof(*ework));
9322 	if (ework == NULL)
9323 		return -1;
9324 
9325 	pos = os_strchr(cmd, ' ');
9326 	if (pos) {
9327 		type_len = pos - cmd;
9328 		pos++;
9329 
9330 		pos2 = os_strstr(pos, "freq=");
9331 		if (pos2)
9332 			freq = atoi(pos2 + 5);
9333 
9334 		pos2 = os_strstr(pos, "timeout=");
9335 		if (pos2)
9336 			ework->timeout = atoi(pos2 + 8);
9337 	} else {
9338 		type_len = os_strlen(cmd);
9339 	}
9340 	if (4 + type_len >= sizeof(ework->type))
9341 		type_len = sizeof(ework->type) - 4 - 1;
9342 	os_strlcpy(ework->type, "ext:", sizeof(ework->type));
9343 	os_memcpy(ework->type + 4, cmd, type_len);
9344 	ework->type[4 + type_len] = '\0';
9345 
9346 	wpa_s->ext_work_id++;
9347 	if (wpa_s->ext_work_id == 0)
9348 		wpa_s->ext_work_id++;
9349 	ework->id = wpa_s->ext_work_id;
9350 
9351 	if (!radio_add_work(wpa_s, freq, ework->type, 0,
9352 			    wpas_ctrl_radio_work_cb, ework)) {
9353 		os_free(ework);
9354 		return -1;
9355 	}
9356 
9357 	ret = os_snprintf(buf, buflen, "%u", ework->id);
9358 	if (os_snprintf_error(buflen, ret))
9359 		return -1;
9360 	return ret;
9361 }
9362 
9363 
wpas_ctrl_radio_work_done(struct wpa_supplicant * wpa_s,char * cmd)9364 static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
9365 {
9366 	struct wpa_radio_work *work;
9367 	unsigned int id = atoi(cmd);
9368 
9369 	dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
9370 	{
9371 		struct wpa_external_work *ework;
9372 
9373 		if (os_strncmp(work->type, "ext:", 4) != 0)
9374 			continue;
9375 		ework = work->ctx;
9376 		if (id && ework->id != id)
9377 			continue;
9378 		wpa_dbg(wpa_s, MSG_DEBUG,
9379 			"Completed external radio work %u (%s)",
9380 			ework->id, ework->type);
9381 		eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
9382 		wpa_s->ext_work_in_progress = 0;
9383 		radio_work_done(work);
9384 		os_free(ework);
9385 		return 3; /* "OK\n" */
9386 	}
9387 
9388 	return -1;
9389 }
9390 
9391 
wpas_ctrl_radio_work(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)9392 static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
9393 				char *buf, size_t buflen)
9394 {
9395 	if (os_strcmp(cmd, "show") == 0)
9396 		return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
9397 	if (os_strncmp(cmd, "add ", 4) == 0)
9398 		return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
9399 	if (os_strncmp(cmd, "done ", 5) == 0)
9400 		return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
9401 	return -1;
9402 }
9403 
9404 
wpas_ctrl_radio_work_flush(struct wpa_supplicant * wpa_s)9405 void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
9406 {
9407 	struct wpa_radio_work *work, *tmp;
9408 
9409 	if (!wpa_s || !wpa_s->radio)
9410 		return;
9411 
9412 	dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
9413 			      struct wpa_radio_work, list) {
9414 		struct wpa_external_work *ework;
9415 
9416 		if (os_strncmp(work->type, "ext:", 4) != 0)
9417 			continue;
9418 		ework = work->ctx;
9419 		wpa_dbg(wpa_s, MSG_DEBUG,
9420 			"Flushing%s external radio work %u (%s)",
9421 			work->started ? " started" : "", ework->id,
9422 			ework->type);
9423 		if (work->started)
9424 			eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
9425 					     work, NULL);
9426 		radio_work_done(work);
9427 		os_free(ework);
9428 	}
9429 }
9430 
9431 
wpas_ctrl_eapol_response(void * eloop_ctx,void * timeout_ctx)9432 static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
9433 {
9434 	struct wpa_supplicant *wpa_s = eloop_ctx;
9435 	eapol_sm_notify_ctrl_response(wpa_s->eapol);
9436 }
9437 
9438 
scan_id_list_parse(struct wpa_supplicant * wpa_s,const char * value,unsigned int * scan_id_count,int scan_id[])9439 static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
9440 			      unsigned int *scan_id_count, int scan_id[])
9441 {
9442 	const char *pos = value;
9443 
9444 	while (pos) {
9445 		if (*pos == ' ' || *pos == '\0')
9446 			break;
9447 		if (*scan_id_count == MAX_SCAN_ID)
9448 			return -1;
9449 		scan_id[(*scan_id_count)++] = atoi(pos);
9450 		pos = os_strchr(pos, ',');
9451 		if (pos)
9452 			pos++;
9453 	}
9454 
9455 	return 0;
9456 }
9457 
9458 
wpas_ctrl_scan(struct wpa_supplicant * wpa_s,char * params,char * reply,int reply_size,int * reply_len)9459 static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
9460 			   char *reply, int reply_size, int *reply_len)
9461 {
9462 	char *pos;
9463 	unsigned int manual_scan_passive = 0;
9464 	unsigned int manual_scan_use_id = 0;
9465 	unsigned int manual_scan_only_new = 0;
9466 	unsigned int scan_only = 0;
9467 	unsigned int scan_id_count = 0;
9468 	unsigned int manual_non_coloc_6ghz = 0;
9469 	int scan_id[MAX_SCAN_ID];
9470 	void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
9471 				 struct wpa_scan_results *scan_res);
9472 	int *manual_scan_freqs = NULL;
9473 	struct wpa_ssid_value *ssid = NULL, *ns;
9474 	unsigned int ssid_count = 0;
9475 
9476 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
9477 		*reply_len = -1;
9478 		return;
9479 	}
9480 
9481 	if (radio_work_pending(wpa_s, "scan")) {
9482 		wpa_printf(MSG_DEBUG,
9483 			   "Pending scan scheduled - reject new request");
9484 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
9485 		return;
9486 	}
9487 
9488 #ifdef CONFIG_INTERWORKING
9489 	if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
9490 		wpa_printf(MSG_DEBUG,
9491 			   "Interworking select in progress - reject new scan");
9492 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
9493 		return;
9494 	}
9495 #endif /* CONFIG_INTERWORKING */
9496 
9497 	if (params) {
9498 		if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
9499 			scan_only = 1;
9500 
9501 		pos = os_strstr(params, "freq=");
9502 		if (pos) {
9503 			manual_scan_freqs = freq_range_to_channel_list(wpa_s,
9504 								       pos + 5);
9505 			if (manual_scan_freqs == NULL) {
9506 				*reply_len = -1;
9507 				goto done;
9508 			}
9509 		}
9510 
9511 		pos = os_strstr(params, "passive=");
9512 		if (pos)
9513 			manual_scan_passive = !!atoi(pos + 8);
9514 
9515 		pos = os_strstr(params, "use_id=");
9516 		if (pos)
9517 			manual_scan_use_id = atoi(pos + 7);
9518 
9519 		pos = os_strstr(params, "only_new=1");
9520 		if (pos)
9521 			manual_scan_only_new = 1;
9522 
9523 		pos = os_strstr(params, "scan_id=");
9524 		if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
9525 					      scan_id) < 0) {
9526 			*reply_len = -1;
9527 			goto done;
9528 		}
9529 
9530 		pos = os_strstr(params, "bssid=");
9531 		if (pos) {
9532 			u8 bssid[ETH_ALEN];
9533 
9534 			pos += 6;
9535 			if (hwaddr_aton(pos, bssid)) {
9536 				wpa_printf(MSG_ERROR, "Invalid BSSID %s", pos);
9537 				*reply_len = -1;
9538 				goto done;
9539 			}
9540 			os_memcpy(wpa_s->next_scan_bssid, bssid, ETH_ALEN);
9541 
9542 			wpa_s->next_scan_bssid_wildcard_ssid =
9543 				os_strstr(params, "wildcard_ssid=1") != NULL;
9544 		}
9545 
9546 		pos = os_strstr(params, "non_coloc_6ghz=");
9547 		if (pos)
9548 			manual_non_coloc_6ghz = !!atoi(pos + 15);
9549 
9550 		pos = params;
9551 		while (pos && *pos != '\0') {
9552 			if (os_strncmp(pos, "ssid ", 5) == 0) {
9553 				char *end;
9554 
9555 				pos += 5;
9556 				end = pos;
9557 				while (*end) {
9558 					if (*end == '\0' || *end == ' ')
9559 						break;
9560 					end++;
9561 				}
9562 
9563 				ns = os_realloc_array(
9564 					ssid, ssid_count + 1,
9565 					sizeof(struct wpa_ssid_value));
9566 				if (ns == NULL) {
9567 					*reply_len = -1;
9568 					goto done;
9569 				}
9570 				ssid = ns;
9571 
9572 				if ((end - pos) & 0x01 ||
9573 				    end - pos > 2 * SSID_MAX_LEN ||
9574 				    hexstr2bin(pos, ssid[ssid_count].ssid,
9575 					       (end - pos) / 2) < 0) {
9576 					wpa_printf(MSG_DEBUG,
9577 						   "Invalid SSID value '%s'",
9578 						   pos);
9579 					*reply_len = -1;
9580 					goto done;
9581 				}
9582 				ssid[ssid_count].ssid_len = (end - pos) / 2;
9583 				wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
9584 						  ssid[ssid_count].ssid,
9585 						  ssid[ssid_count].ssid_len);
9586 				ssid_count++;
9587 				pos = end;
9588 			}
9589 
9590 			pos = os_strchr(pos, ' ');
9591 			if (pos)
9592 				pos++;
9593 		}
9594 	}
9595 
9596 	wpa_s->num_ssids_from_scan_req = ssid_count;
9597 	os_free(wpa_s->ssids_from_scan_req);
9598 	if (ssid_count) {
9599 		wpa_s->ssids_from_scan_req = ssid;
9600 		ssid = NULL;
9601 	} else {
9602 		wpa_s->ssids_from_scan_req = NULL;
9603 	}
9604 
9605 	if (scan_only)
9606 		scan_res_handler = scan_only_handler;
9607 	else if (wpa_s->scan_res_handler == scan_only_handler)
9608 		scan_res_handler = NULL;
9609 	else
9610 		scan_res_handler = wpa_s->scan_res_handler;
9611 
9612 	if (!wpa_s->sched_scanning && !wpa_s->scanning &&
9613 	    ((wpa_s->wpa_state <= WPA_SCANNING) ||
9614 	     (wpa_s->wpa_state == WPA_COMPLETED))) {
9615 		wpa_s->manual_scan_passive = manual_scan_passive;
9616 		wpa_s->manual_scan_use_id = manual_scan_use_id;
9617 		wpa_s->manual_scan_only_new = manual_scan_only_new;
9618 		wpa_s->scan_id_count = scan_id_count;
9619 		wpa_s->manual_non_coloc_6ghz = manual_non_coloc_6ghz;
9620 		os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
9621 		wpa_s->scan_res_handler = scan_res_handler;
9622 		os_free(wpa_s->manual_scan_freqs);
9623 		wpa_s->manual_scan_freqs = manual_scan_freqs;
9624 		manual_scan_freqs = NULL;
9625 
9626 		wpa_s->normal_scans = 0;
9627 		wpa_s->scan_req = MANUAL_SCAN_REQ;
9628 		wpa_s->after_wps = 0;
9629 		wpa_s->known_wps_freq = 0;
9630 		wpa_supplicant_req_scan(wpa_s, 0, 0);
9631 		if (wpa_s->manual_scan_use_id) {
9632 			wpa_s->manual_scan_id++;
9633 			wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
9634 				wpa_s->manual_scan_id);
9635 			*reply_len = os_snprintf(reply, reply_size, "%u\n",
9636 						 wpa_s->manual_scan_id);
9637 		}
9638 	} else if (wpa_s->sched_scanning) {
9639 		wpa_s->manual_scan_passive = manual_scan_passive;
9640 		wpa_s->manual_scan_use_id = manual_scan_use_id;
9641 		wpa_s->manual_scan_only_new = manual_scan_only_new;
9642 		wpa_s->scan_id_count = scan_id_count;
9643 		wpa_s->manual_non_coloc_6ghz = manual_non_coloc_6ghz;
9644 		os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
9645 		wpa_s->scan_res_handler = scan_res_handler;
9646 		os_free(wpa_s->manual_scan_freqs);
9647 		wpa_s->manual_scan_freqs = manual_scan_freqs;
9648 		manual_scan_freqs = NULL;
9649 
9650 		wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
9651 		wpa_supplicant_cancel_sched_scan(wpa_s);
9652 		wpa_s->scan_req = MANUAL_SCAN_REQ;
9653 		wpa_supplicant_req_scan(wpa_s, 0, 0);
9654 		if (wpa_s->manual_scan_use_id) {
9655 			wpa_s->manual_scan_id++;
9656 			*reply_len = os_snprintf(reply, reply_size, "%u\n",
9657 						 wpa_s->manual_scan_id);
9658 			wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
9659 				wpa_s->manual_scan_id);
9660 		}
9661 	} else {
9662 		wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
9663 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
9664 	}
9665 
9666 done:
9667 	os_free(manual_scan_freqs);
9668 	os_free(ssid);
9669 }
9670 
9671 
9672 #ifdef CONFIG_TESTING_OPTIONS
9673 
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)9674 static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
9675 				       unsigned int freq, const u8 *dst,
9676 				       const u8 *src, const u8 *bssid,
9677 				       const u8 *data, size_t data_len,
9678 				       enum offchannel_send_action_result
9679 				       result)
9680 {
9681 	wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
9682 		" src=" MACSTR " bssid=" MACSTR " result=%s",
9683 		freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
9684 		result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
9685 		"SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
9686 			     "NO_ACK" : "FAILED"));
9687 }
9688 
9689 
wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant * wpa_s,char * cmd)9690 static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
9691 {
9692 	char *pos, *param;
9693 	size_t len;
9694 	u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
9695 	int res, used;
9696 	int freq = 0, no_cck = 0, wait_time = 0;
9697 
9698 	/* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
9699 	 *    <action=Action frame payload> */
9700 
9701 	wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
9702 
9703 	pos = cmd;
9704 	used = hwaddr_aton2(pos, da);
9705 	if (used < 0)
9706 		return -1;
9707 	pos += used;
9708 	while (*pos == ' ')
9709 		pos++;
9710 	used = hwaddr_aton2(pos, bssid);
9711 	if (used < 0)
9712 		return -1;
9713 	pos += used;
9714 
9715 	param = os_strstr(pos, " freq=");
9716 	if (param) {
9717 		param += 6;
9718 		freq = atoi(param);
9719 	}
9720 
9721 	param = os_strstr(pos, " no_cck=");
9722 	if (param) {
9723 		param += 8;
9724 		no_cck = atoi(param);
9725 	}
9726 
9727 	param = os_strstr(pos, " wait_time=");
9728 	if (param) {
9729 		param += 11;
9730 		wait_time = atoi(param);
9731 	}
9732 
9733 	param = os_strstr(pos, " action=");
9734 	if (param == NULL)
9735 		return -1;
9736 	param += 8;
9737 
9738 	len = os_strlen(param);
9739 	if (len & 1)
9740 		return -1;
9741 	len /= 2;
9742 
9743 	buf = os_malloc(len);
9744 	if (buf == NULL)
9745 		return -1;
9746 
9747 	if (hexstr2bin(param, buf, len) < 0) {
9748 		os_free(buf);
9749 		return -1;
9750 	}
9751 
9752 	res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
9753 				     buf, len, wait_time,
9754 				     wpas_ctrl_iface_mgmt_tx_cb, no_cck);
9755 	os_free(buf);
9756 	return res;
9757 }
9758 
9759 
wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant * wpa_s)9760 static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
9761 {
9762 	wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
9763 	offchannel_send_action_done(wpa_s);
9764 }
9765 
9766 
wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant * wpa_s,char * cmd)9767 static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
9768 					   char *cmd)
9769 {
9770 	char *pos, *param;
9771 	size_t len;
9772 	u8 *buf;
9773 	int freq = 0, datarate = 0, ssi_signal = 0;
9774 	union wpa_event_data event;
9775 
9776 	if (!wpa_s->ext_mgmt_frame_handling)
9777 		return -1;
9778 
9779 	/* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
9780 
9781 	wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
9782 
9783 	pos = cmd;
9784 	param = os_strstr(pos, "freq=");
9785 	if (param) {
9786 		param += 5;
9787 		freq = atoi(param);
9788 	}
9789 
9790 	param = os_strstr(pos, " datarate=");
9791 	if (param) {
9792 		param += 10;
9793 		datarate = atoi(param);
9794 	}
9795 
9796 	param = os_strstr(pos, " ssi_signal=");
9797 	if (param) {
9798 		param += 12;
9799 		ssi_signal = atoi(param);
9800 	}
9801 
9802 	param = os_strstr(pos, " frame=");
9803 	if (param == NULL)
9804 		return -1;
9805 	param += 7;
9806 
9807 	len = os_strlen(param);
9808 	if (len & 1)
9809 		return -1;
9810 	len /= 2;
9811 
9812 	buf = os_malloc(len);
9813 	if (buf == NULL)
9814 		return -1;
9815 
9816 	if (hexstr2bin(param, buf, len) < 0) {
9817 		os_free(buf);
9818 		return -1;
9819 	}
9820 
9821 	os_memset(&event, 0, sizeof(event));
9822 	event.rx_mgmt.freq = freq;
9823 	event.rx_mgmt.frame = buf;
9824 	event.rx_mgmt.frame_len = len;
9825 	event.rx_mgmt.ssi_signal = ssi_signal;
9826 	event.rx_mgmt.datarate = datarate;
9827 	wpa_s->ext_mgmt_frame_handling = 0;
9828 	wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
9829 	wpa_s->ext_mgmt_frame_handling = 1;
9830 
9831 	os_free(buf);
9832 
9833 	return 0;
9834 }
9835 
9836 
wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant * wpa_s,char * param)9837 static int wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant *wpa_s,
9838 					   char *param)
9839 {
9840 	struct wpa_scan_res *res;
9841 	struct os_reltime now;
9842 	char *pos, *end;
9843 	int ret = -1;
9844 
9845 	if (!param)
9846 		return -1;
9847 
9848 	if (os_strcmp(param, "START") == 0) {
9849 		wpa_bss_update_start(wpa_s);
9850 		return 0;
9851 	}
9852 
9853 	if (os_strcmp(param, "END") == 0) {
9854 		wpa_bss_update_end(wpa_s, NULL, 1);
9855 		return 0;
9856 	}
9857 
9858 	if (os_strncmp(param, "BSS ", 4) != 0)
9859 		return -1;
9860 	param += 3;
9861 
9862 	res = os_zalloc(sizeof(*res) + os_strlen(param) / 2);
9863 	if (!res)
9864 		return -1;
9865 
9866 	pos = os_strstr(param, " flags=");
9867 	if (pos)
9868 		res->flags = strtol(pos + 7, NULL, 16);
9869 
9870 	pos = os_strstr(param, " bssid=");
9871 	if (pos && hwaddr_aton(pos + 7, res->bssid))
9872 		goto fail;
9873 
9874 	pos = os_strstr(param, " freq=");
9875 	if (pos)
9876 		res->freq = atoi(pos + 6);
9877 
9878 	pos = os_strstr(param, " beacon_int=");
9879 	if (pos)
9880 		res->beacon_int = atoi(pos + 12);
9881 
9882 	pos = os_strstr(param, " caps=");
9883 	if (pos)
9884 		res->caps = strtol(pos + 6, NULL, 16);
9885 
9886 	pos = os_strstr(param, " qual=");
9887 	if (pos)
9888 		res->qual = atoi(pos + 6);
9889 
9890 	pos = os_strstr(param, " noise=");
9891 	if (pos)
9892 		res->noise = atoi(pos + 7);
9893 
9894 	pos = os_strstr(param, " level=");
9895 	if (pos)
9896 		res->level = atoi(pos + 7);
9897 
9898 	pos = os_strstr(param, " tsf=");
9899 	if (pos)
9900 		res->tsf = strtoll(pos + 5, NULL, 16);
9901 
9902 	pos = os_strstr(param, " age=");
9903 	if (pos)
9904 		res->age = atoi(pos + 5);
9905 
9906 	pos = os_strstr(param, " est_throughput=");
9907 	if (pos)
9908 		res->est_throughput = atoi(pos + 16);
9909 
9910 	pos = os_strstr(param, " snr=");
9911 	if (pos)
9912 		res->snr = atoi(pos + 5);
9913 
9914 	pos = os_strstr(param, " parent_tsf=");
9915 	if (pos)
9916 		res->parent_tsf = strtoll(pos + 7, NULL, 16);
9917 
9918 	pos = os_strstr(param, " tsf_bssid=");
9919 	if (pos && hwaddr_aton(pos + 11, res->tsf_bssid))
9920 		goto fail;
9921 
9922 	pos = os_strstr(param, " ie=");
9923 	if (pos) {
9924 		pos += 4;
9925 		end = os_strchr(pos, ' ');
9926 		if (!end)
9927 			end = pos + os_strlen(pos);
9928 		res->ie_len = (end - pos) / 2;
9929 		if (hexstr2bin(pos, (u8 *) (res + 1), res->ie_len))
9930 			goto fail;
9931 	}
9932 
9933 	pos = os_strstr(param, " beacon_ie=");
9934 	if (pos) {
9935 		pos += 11;
9936 		end = os_strchr(pos, ' ');
9937 		if (!end)
9938 			end = pos + os_strlen(pos);
9939 		res->beacon_ie_len = (end - pos) / 2;
9940 		if (hexstr2bin(pos, ((u8 *) (res + 1)) + res->ie_len,
9941 			       res->beacon_ie_len))
9942 			goto fail;
9943 	}
9944 
9945 	os_get_reltime(&now);
9946 	wpa_bss_update_scan_res(wpa_s, res, &now);
9947 	ret = 0;
9948 fail:
9949 	os_free(res);
9950 
9951 	return ret;
9952 }
9953 
9954 
wpas_ctrl_iface_driver_event_assoc(struct wpa_supplicant * wpa_s,char * param)9955 static int wpas_ctrl_iface_driver_event_assoc(struct wpa_supplicant *wpa_s,
9956 					      char *param)
9957 {
9958 	union wpa_event_data event;
9959 	struct assoc_info *ai;
9960 	char *ctx = NULL;
9961 	int ret = -1;
9962 	struct wpabuf *req_ies = NULL;
9963 	struct wpabuf *resp_ies = NULL;
9964 	struct wpabuf *resp_frame = NULL;
9965 	struct wpabuf *beacon_ies = NULL;
9966 	struct wpabuf *key_replay_ctr = NULL;
9967 	struct wpabuf *ptk_kck = NULL;
9968 	struct wpabuf *ptk_kek = NULL;
9969 	struct wpabuf *fils_pmk = NULL;
9970 	char *str, *pos;
9971 	u8 addr[ETH_ALEN];
9972 	u8 fils_pmkid[PMKID_LEN];
9973 
9974 	os_memset(&event, 0, sizeof(event));
9975 	ai = &event.assoc_info;
9976 
9977 	while ((str = str_token(param, " ", &ctx))) {
9978 		pos = os_strchr(str, '=');
9979 		if (!pos)
9980 			goto fail;
9981 		*pos++ = '\0';
9982 
9983 		if (os_strcmp(str, "reassoc") == 0) {
9984 			ai->reassoc = atoi(pos);
9985 		} else if (os_strcmp(str, "req_ies") == 0) {
9986 			wpabuf_free(req_ies);
9987 			req_ies = wpabuf_parse_bin(pos);
9988 			if (!req_ies)
9989 				goto fail;
9990 			ai->req_ies = wpabuf_head(req_ies);
9991 			ai->req_ies_len = wpabuf_len(req_ies);
9992 		} else if (os_strcmp(str, "resp_ies") == 0) {
9993 			wpabuf_free(resp_ies);
9994 			resp_ies = wpabuf_parse_bin(pos);
9995 			if (!resp_ies)
9996 				goto fail;
9997 			ai->resp_ies = wpabuf_head(resp_ies);
9998 			ai->resp_ies_len = wpabuf_len(resp_ies);
9999 		} else if (os_strcmp(str, "resp_frame") == 0) {
10000 			wpabuf_free(resp_frame);
10001 			resp_frame = wpabuf_parse_bin(pos);
10002 			if (!resp_frame)
10003 				goto fail;
10004 			ai->resp_frame = wpabuf_head(resp_frame);
10005 			ai->resp_frame_len = wpabuf_len(resp_frame);
10006 		} else if (os_strcmp(str, "beacon_ies") == 0) {
10007 			wpabuf_free(beacon_ies);
10008 			beacon_ies = wpabuf_parse_bin(pos);
10009 			if (!beacon_ies)
10010 				goto fail;
10011 			ai->beacon_ies = wpabuf_head(beacon_ies);
10012 			ai->beacon_ies_len = wpabuf_len(beacon_ies);
10013 		} else if (os_strcmp(str, "freq") == 0) {
10014 			ai->freq = atoi(pos);
10015 		} else if (os_strcmp(str, "wmm::info_bitmap") == 0) {
10016 			ai->wmm_params.info_bitmap = atoi(pos);
10017 		} else if (os_strcmp(str, "wmm::uapsd_queues") == 0) {
10018 			ai->wmm_params.uapsd_queues = atoi(pos);
10019 		} else if (os_strcmp(str, "addr") == 0) {
10020 			if (hwaddr_aton(pos, addr))
10021 				goto fail;
10022 			ai->addr = addr;
10023 		} else if (os_strcmp(str, "authorized") == 0) {
10024 			ai->authorized = atoi(pos);
10025 		} else if (os_strcmp(str, "key_replay_ctr") == 0) {
10026 			wpabuf_free(key_replay_ctr);
10027 			key_replay_ctr = wpabuf_parse_bin(pos);
10028 			if (!key_replay_ctr)
10029 				goto fail;
10030 			ai->key_replay_ctr = wpabuf_head(key_replay_ctr);
10031 			ai->key_replay_ctr_len = wpabuf_len(key_replay_ctr);
10032 		} else if (os_strcmp(str, "ptk_kck") == 0) {
10033 			wpabuf_free(ptk_kck);
10034 			ptk_kck = wpabuf_parse_bin(pos);
10035 			if (!ptk_kck)
10036 				goto fail;
10037 			ai->ptk_kck = wpabuf_head(ptk_kck);
10038 			ai->ptk_kck_len = wpabuf_len(ptk_kck);
10039 		} else if (os_strcmp(str, "ptk_kek") == 0) {
10040 			wpabuf_free(ptk_kek);
10041 			ptk_kek = wpabuf_parse_bin(pos);
10042 			if (!ptk_kek)
10043 				goto fail;
10044 			ai->ptk_kek = wpabuf_head(ptk_kek);
10045 			ai->ptk_kek_len = wpabuf_len(ptk_kek);
10046 		} else if (os_strcmp(str, "subnet_status") == 0) {
10047 			ai->subnet_status = atoi(pos);
10048 		} else if (os_strcmp(str, "fils_erp_next_seq_num") == 0) {
10049 			ai->fils_erp_next_seq_num = atoi(pos);
10050 		} else if (os_strcmp(str, "fils_pmk") == 0) {
10051 			wpabuf_free(fils_pmk);
10052 			fils_pmk = wpabuf_parse_bin(pos);
10053 			if (!fils_pmk)
10054 				goto fail;
10055 			ai->fils_pmk = wpabuf_head(fils_pmk);
10056 			ai->fils_pmk_len = wpabuf_len(fils_pmk);
10057 		} else if (os_strcmp(str, "fils_pmkid") == 0) {
10058 			if (hexstr2bin(pos, fils_pmkid, PMKID_LEN) < 0)
10059 				goto fail;
10060 			ai->fils_pmkid = fils_pmkid;
10061 		} else {
10062 			goto fail;
10063 		}
10064 	}
10065 
10066 	wpa_supplicant_event(wpa_s, EVENT_ASSOC, &event);
10067 	ret = 0;
10068 fail:
10069 	wpabuf_free(req_ies);
10070 	wpabuf_free(resp_ies);
10071 	wpabuf_free(resp_frame);
10072 	wpabuf_free(beacon_ies);
10073 	wpabuf_free(key_replay_ctr);
10074 	wpabuf_free(ptk_kck);
10075 	wpabuf_free(ptk_kek);
10076 	wpabuf_free(fils_pmk);
10077 	return ret;
10078 }
10079 
10080 
wpas_ctrl_iface_driver_event(struct wpa_supplicant * wpa_s,char * cmd)10081 static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
10082 {
10083 	char *pos, *param;
10084 	union wpa_event_data event;
10085 	enum wpa_event_type ev;
10086 
10087 	/* <event name> [parameters..] */
10088 
10089 	wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
10090 
10091 	pos = cmd;
10092 	param = os_strchr(pos, ' ');
10093 	if (param)
10094 		*param++ = '\0';
10095 
10096 	os_memset(&event, 0, sizeof(event));
10097 
10098 	if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
10099 		ev = EVENT_INTERFACE_ENABLED;
10100 	} else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
10101 		ev = EVENT_INTERFACE_DISABLED;
10102 	} else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
10103 		ev = EVENT_AVOID_FREQUENCIES;
10104 		if (param == NULL)
10105 			param = "";
10106 		if (freq_range_list_parse(&event.freq_range, param) < 0)
10107 			return -1;
10108 		wpa_supplicant_event(wpa_s, ev, &event);
10109 		os_free(event.freq_range.range);
10110 		return 0;
10111 	} else if (os_strcmp(cmd, "SCAN_RES") == 0) {
10112 		return wpas_ctrl_iface_driver_scan_res(wpa_s, param);
10113 	} else if (os_strcmp(cmd, "ASSOC") == 0) {
10114 		return wpas_ctrl_iface_driver_event_assoc(wpa_s, param);
10115 	} else {
10116 		wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
10117 			cmd);
10118 		return -1;
10119 	}
10120 
10121 	wpa_supplicant_event(wpa_s, ev, &event);
10122 
10123 	return 0;
10124 }
10125 
10126 
wpas_ctrl_iface_eapol_rx(struct wpa_supplicant * wpa_s,char * cmd)10127 static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
10128 {
10129 	char *pos;
10130 	u8 src[ETH_ALEN], *buf;
10131 	int used;
10132 	size_t len;
10133 
10134 	wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
10135 
10136 	pos = cmd;
10137 	used = hwaddr_aton2(pos, src);
10138 	if (used < 0)
10139 		return -1;
10140 	pos += used;
10141 	while (*pos == ' ')
10142 		pos++;
10143 
10144 	len = os_strlen(pos);
10145 	if (len & 1)
10146 		return -1;
10147 	len /= 2;
10148 
10149 	buf = os_malloc(len);
10150 	if (buf == NULL)
10151 		return -1;
10152 
10153 	if (hexstr2bin(pos, buf, len) < 0) {
10154 		os_free(buf);
10155 		return -1;
10156 	}
10157 
10158 	wpa_supplicant_rx_eapol(wpa_s, src, buf, len, FRAME_ENCRYPTION_UNKNOWN);
10159 	os_free(buf);
10160 
10161 	return 0;
10162 }
10163 
10164 
wpas_ctrl_iface_eapol_tx(struct wpa_supplicant * wpa_s,char * cmd)10165 static int wpas_ctrl_iface_eapol_tx(struct wpa_supplicant *wpa_s, char *cmd)
10166 {
10167 	char *pos;
10168 	u8 dst[ETH_ALEN], *buf;
10169 	int used, ret;
10170 	size_t len;
10171 	unsigned int prev;
10172 
10173 	wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
10174 
10175 	pos = cmd;
10176 	used = hwaddr_aton2(pos, dst);
10177 	if (used < 0)
10178 		return -1;
10179 	pos += used;
10180 	while (*pos == ' ')
10181 		pos++;
10182 
10183 	len = os_strlen(pos);
10184 	if (len & 1)
10185 		return -1;
10186 	len /= 2;
10187 
10188 	buf = os_malloc(len);
10189 	if (!buf || hexstr2bin(pos, buf, len) < 0) {
10190 		os_free(buf);
10191 		return -1;
10192 	}
10193 
10194 	prev = wpa_s->ext_eapol_frame_io;
10195 	wpa_s->ext_eapol_frame_io = 0;
10196 	ret = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, buf, len);
10197 	wpa_s->ext_eapol_frame_io = prev;
10198 	os_free(buf);
10199 
10200 	return ret;
10201 }
10202 
10203 
ipv4_hdr_checksum(const void * buf,size_t len)10204 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
10205 {
10206 	size_t i;
10207 	u32 sum = 0;
10208 	const u16 *pos = buf;
10209 
10210 	for (i = 0; i < len / 2; i++)
10211 		sum += *pos++;
10212 
10213 	while (sum >> 16)
10214 		sum = (sum & 0xffff) + (sum >> 16);
10215 
10216 	return sum ^ 0xffff;
10217 }
10218 
10219 
10220 #define HWSIM_PACKETLEN 1500
10221 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
10222 
wpas_data_test_rx(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)10223 static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
10224 			      size_t len)
10225 {
10226 	struct wpa_supplicant *wpa_s = ctx;
10227 	const struct ether_header *eth;
10228 	struct ip ip;
10229 	const u8 *pos;
10230 	unsigned int i;
10231 	char extra[30];
10232 
10233 	if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
10234 		wpa_printf(MSG_DEBUG,
10235 			   "test data: RX - ignore unexpected length %d",
10236 			   (int) len);
10237 		return;
10238 	}
10239 
10240 	eth = (const struct ether_header *) buf;
10241 	os_memcpy(&ip, eth + 1, sizeof(ip));
10242 	pos = &buf[sizeof(*eth) + sizeof(ip)];
10243 
10244 	if (ip.ip_hl != 5 || ip.ip_v != 4 || ntohs(ip.ip_len) > HWSIM_IP_LEN) {
10245 		wpa_printf(MSG_DEBUG,
10246 			   "test data: RX - ignore unexpected IP header");
10247 		return;
10248 	}
10249 
10250 	for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
10251 		if (*pos != (u8) i) {
10252 			wpa_printf(MSG_DEBUG,
10253 				   "test data: RX - ignore mismatching payload");
10254 			return;
10255 		}
10256 		pos++;
10257 	}
10258 	extra[0] = '\0';
10259 	if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
10260 		os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
10261 	wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
10262 		MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
10263 }
10264 
10265 
wpas_ctrl_iface_data_test_config(struct wpa_supplicant * wpa_s,char * cmd)10266 static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
10267 					    char *cmd)
10268 {
10269 	int enabled = atoi(cmd);
10270 	char *pos;
10271 	const char *ifname;
10272 
10273 	if (!enabled) {
10274 		if (wpa_s->l2_test) {
10275 			l2_packet_deinit(wpa_s->l2_test);
10276 			wpa_s->l2_test = NULL;
10277 			wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
10278 		}
10279 		return 0;
10280 	}
10281 
10282 	if (wpa_s->l2_test)
10283 		return 0;
10284 
10285 	pos = os_strstr(cmd, " ifname=");
10286 	if (pos)
10287 		ifname = pos + 8;
10288 	else
10289 		ifname = wpa_s->ifname;
10290 
10291 	wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
10292 					ETHERTYPE_IP, wpas_data_test_rx,
10293 					wpa_s, 1);
10294 	if (wpa_s->l2_test == NULL)
10295 		return -1;
10296 
10297 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
10298 
10299 	return 0;
10300 }
10301 
10302 
wpas_ctrl_iface_data_test_tx(struct wpa_supplicant * wpa_s,char * cmd)10303 static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
10304 {
10305 	u8 dst[ETH_ALEN], src[ETH_ALEN];
10306 	char *pos, *pos2;
10307 	int used;
10308 	long int val;
10309 	u8 tos;
10310 	u8 buf[2 + HWSIM_PACKETLEN];
10311 	struct ether_header *eth;
10312 	struct ip *ip;
10313 	u8 *dpos;
10314 	unsigned int i;
10315 	size_t send_len = HWSIM_IP_LEN;
10316 
10317 	if (wpa_s->l2_test == NULL)
10318 		return -1;
10319 
10320 	/* format: <dst> <src> <tos> [len=<length>] */
10321 
10322 	pos = cmd;
10323 	used = hwaddr_aton2(pos, dst);
10324 	if (used < 0)
10325 		return -1;
10326 	pos += used;
10327 	while (*pos == ' ')
10328 		pos++;
10329 	used = hwaddr_aton2(pos, src);
10330 	if (used < 0)
10331 		return -1;
10332 	pos += used;
10333 
10334 	val = strtol(pos, &pos2, 0);
10335 	if (val < 0 || val > 0xff)
10336 		return -1;
10337 	tos = val;
10338 
10339 	pos = os_strstr(pos2, " len=");
10340 	if (pos) {
10341 		i = atoi(pos + 5);
10342 		if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
10343 			return -1;
10344 		send_len = i;
10345 	}
10346 
10347 	eth = (struct ether_header *) &buf[2];
10348 	os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
10349 	os_memcpy(eth->ether_shost, src, ETH_ALEN);
10350 	eth->ether_type = htons(ETHERTYPE_IP);
10351 	ip = (struct ip *) (eth + 1);
10352 	os_memset(ip, 0, sizeof(*ip));
10353 	ip->ip_hl = 5;
10354 	ip->ip_v = 4;
10355 	ip->ip_ttl = 64;
10356 	ip->ip_tos = tos;
10357 	ip->ip_len = htons(send_len);
10358 	ip->ip_p = 1;
10359 	ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
10360 	ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
10361 	ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
10362 	dpos = (u8 *) (ip + 1);
10363 	for (i = 0; i < send_len - sizeof(*ip); i++)
10364 		*dpos++ = i;
10365 
10366 	if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
10367 			   sizeof(struct ether_header) + send_len) < 0)
10368 		return -1;
10369 
10370 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
10371 		" tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
10372 
10373 	return 0;
10374 }
10375 
10376 
wpas_ctrl_iface_data_test_frame(struct wpa_supplicant * wpa_s,char * cmd)10377 static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
10378 					   char *cmd)
10379 {
10380 	u8 *buf;
10381 	struct ether_header *eth;
10382 	struct l2_packet_data *l2 = NULL;
10383 	size_t len;
10384 	u16 ethertype;
10385 	int res = -1;
10386 
10387 	len = os_strlen(cmd);
10388 	if (len & 1 || len < ETH_HLEN * 2)
10389 		return -1;
10390 	len /= 2;
10391 
10392 	buf = os_malloc(len);
10393 	if (buf == NULL)
10394 		return -1;
10395 
10396 	if (hexstr2bin(cmd, buf, len) < 0)
10397 		goto done;
10398 
10399 	eth = (struct ether_header *) buf;
10400 	ethertype = ntohs(eth->ether_type);
10401 
10402 	l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
10403 			    wpas_data_test_rx, wpa_s, 1);
10404 	if (l2 == NULL)
10405 		goto done;
10406 
10407 	res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
10408 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
10409 done:
10410 	if (l2)
10411 		l2_packet_deinit(l2);
10412 	os_free(buf);
10413 
10414 	return res < 0 ? -1 : 0;
10415 }
10416 
10417 
wpas_ctrl_event_test_cb(void * eloop_ctx,void * timeout_ctx)10418 static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
10419 {
10420 	struct wpa_supplicant *wpa_s = eloop_ctx;
10421 	int i, count = (intptr_t) timeout_ctx;
10422 
10423 	wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
10424 		   count);
10425 	for (i = 0; i < count; i++) {
10426 		wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
10427 			     i + 1, count);
10428 	}
10429 }
10430 
10431 
wpas_ctrl_event_test(struct wpa_supplicant * wpa_s,const char * cmd)10432 static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
10433 {
10434 	int count;
10435 
10436 	count = atoi(cmd);
10437 	if (count <= 0)
10438 		return -1;
10439 
10440 	return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
10441 				      (void *) (intptr_t) count);
10442 }
10443 
10444 
wpas_get_hex_buf(const char * val,struct wpabuf ** ret)10445 static int wpas_get_hex_buf(const char *val, struct wpabuf **ret)
10446 {
10447 	struct wpabuf *buf;
10448 	size_t len;
10449 
10450 	len = os_strlen(val);
10451 	if (len & 1)
10452 		return -1;
10453 	len /= 2;
10454 
10455 	if (len == 0) {
10456 		buf = NULL;
10457 	} else {
10458 		buf = wpabuf_alloc(len);
10459 		if (!buf)
10460 			return -1;
10461 
10462 		if (hexstr2bin(val, wpabuf_put(buf, len), len) < 0) {
10463 			wpabuf_free(buf);
10464 			return -1;
10465 		}
10466 	}
10467 
10468 	*ret = buf;
10469 	return 0;
10470 }
10471 
10472 
wpas_ctrl_test_assoc_ie(struct wpa_supplicant * wpa_s,const char * cmd)10473 static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
10474 				   const char *cmd)
10475 {
10476 	struct wpabuf *buf;
10477 
10478 	if (wpas_get_hex_buf(cmd, &buf) < 0)
10479 		return -1;
10480 	wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
10481 	return 0;
10482 }
10483 
10484 
wpas_ctrl_test_eapol_m2_elems(struct wpa_supplicant * wpa_s,const char * cmd)10485 static int wpas_ctrl_test_eapol_m2_elems(struct wpa_supplicant *wpa_s,
10486 					 const char *cmd)
10487 {
10488 	struct wpabuf *buf;
10489 
10490 	if (wpas_get_hex_buf(cmd, &buf) < 0)
10491 		return -1;
10492 	wpa_sm_set_test_eapol_m2_elems(wpa_s->wpa, buf);
10493 	return 0;
10494 }
10495 
10496 
wpas_ctrl_test_eapol_m4_elems(struct wpa_supplicant * wpa_s,const char * cmd)10497 static int wpas_ctrl_test_eapol_m4_elems(struct wpa_supplicant *wpa_s,
10498 					 const char *cmd)
10499 {
10500 	struct wpabuf *buf;
10501 
10502 	if (wpas_get_hex_buf(cmd, &buf) < 0)
10503 		return -1;
10504 	wpa_sm_set_test_eapol_m4_elems(wpa_s->wpa, buf);
10505 	return 0;
10506 }
10507 
10508 
wpas_ctrl_test_rsnxe_data(struct wpa_supplicant * wpa_s,const char * cmd)10509 static int wpas_ctrl_test_rsnxe_data(struct wpa_supplicant *wpa_s,
10510 				     const char *cmd)
10511 {
10512 	struct wpabuf *data = NULL, *mask = NULL;
10513 	char *pos;
10514 
10515 	pos = os_strchr(cmd, ' ');
10516 	if (!pos)
10517 		return -1;
10518 	*pos++ = '\0';
10519 
10520 	if (wpas_get_hex_buf(cmd, &data) < 0 ||
10521 	    wpas_get_hex_buf(pos, &mask) < 0 ||
10522 	    wpa_sm_set_test_rsnxe_data(wpa_s->wpa, data, mask) < 0) {
10523 		wpabuf_free(data);
10524 		wpabuf_free(mask);
10525 		return -1;
10526 	}
10527 
10528 	return 0;
10529 }
10530 
10531 
wpas_ctrl_reset_pn(struct wpa_supplicant * wpa_s)10532 static int wpas_ctrl_reset_pn(struct wpa_supplicant *wpa_s)
10533 {
10534 	u8 zero[WPA_TK_MAX_LEN];
10535 
10536 	if (wpa_s->last_tk_alg == WPA_ALG_NONE)
10537 		return -1;
10538 
10539 	wpa_printf(MSG_INFO, "TESTING: Reset PN");
10540 	os_memset(zero, 0, sizeof(zero));
10541 
10542 	/* First, use a zero key to avoid any possible duplicate key avoidance
10543 	 * in the driver. */
10544 	if (wpa_drv_set_key(wpa_s, -1, wpa_s->last_tk_alg, wpa_s->last_tk_addr,
10545 			    wpa_s->last_tk_key_idx, 1, zero, 6,
10546 			    zero, wpa_s->last_tk_len,
10547 			    KEY_FLAG_PAIRWISE_RX_TX) < 0)
10548 		return -1;
10549 
10550 	/* Set the previously configured key to reset its TSC/RSC */
10551 	return wpa_drv_set_key(wpa_s, -1, wpa_s->last_tk_alg,
10552 			       wpa_s->last_tk_addr,
10553 			       wpa_s->last_tk_key_idx, 1, zero, 6,
10554 			       wpa_s->last_tk, wpa_s->last_tk_len,
10555 			       KEY_FLAG_PAIRWISE_RX_TX);
10556 }
10557 
10558 
wpas_ctrl_key_request(struct wpa_supplicant * wpa_s,const char * cmd)10559 static int wpas_ctrl_key_request(struct wpa_supplicant *wpa_s, const char *cmd)
10560 {
10561 	const char *pos = cmd;
10562 	int error, pairwise;
10563 
10564 	error = atoi(pos);
10565 	pos = os_strchr(pos, ' ');
10566 	if (!pos)
10567 		return -1;
10568 	pairwise = atoi(pos);
10569 	wpa_sm_key_request(wpa_s->wpa, error, pairwise);
10570 	return 0;
10571 }
10572 
10573 
wpas_ctrl_resend_assoc(struct wpa_supplicant * wpa_s)10574 static int wpas_ctrl_resend_assoc(struct wpa_supplicant *wpa_s)
10575 {
10576 #ifdef CONFIG_SME
10577 	struct wpa_driver_associate_params params;
10578 	int ret;
10579 
10580 	os_memset(&params, 0, sizeof(params));
10581 	params.bssid = wpa_s->bssid;
10582 	params.ssid = wpa_s->sme.ssid;
10583 	params.ssid_len = wpa_s->sme.ssid_len;
10584 	params.freq.freq = wpa_s->sme.freq;
10585 	if (wpa_s->last_assoc_req_wpa_ie) {
10586 		params.wpa_ie = wpabuf_head(wpa_s->last_assoc_req_wpa_ie);
10587 		params.wpa_ie_len = wpabuf_len(wpa_s->last_assoc_req_wpa_ie);
10588 	}
10589 	params.pairwise_suite = wpa_s->pairwise_cipher;
10590 	params.group_suite = wpa_s->group_cipher;
10591 	params.mgmt_group_suite = wpa_s->mgmt_group_cipher;
10592 	params.key_mgmt_suite = wpa_s->key_mgmt;
10593 	params.wpa_proto = wpa_s->wpa_proto;
10594 	params.mgmt_frame_protection = wpa_s->sme.mfp;
10595 	params.spp_amsdu = wpa_s->sme.spp_amsdu;
10596 	params.rrm_used = wpa_s->rrm.rrm_used;
10597 	if (wpa_s->sme.prev_bssid_set)
10598 		params.prev_bssid = wpa_s->sme.prev_bssid;
10599 	wpa_printf(MSG_INFO, "TESTING: Resend association request");
10600 	ret = wpa_drv_associate(wpa_s, &params);
10601 	wpa_s->testing_resend_assoc = 1;
10602 	return ret;
10603 #else /* CONFIG_SME */
10604 	return -1;
10605 #endif /* CONFIG_SME */
10606 }
10607 
10608 
wpas_ctrl_iface_send_twt_setup(struct wpa_supplicant * wpa_s,const char * cmd)10609 static int wpas_ctrl_iface_send_twt_setup(struct wpa_supplicant *wpa_s,
10610 					  const char *cmd)
10611 {
10612 	u8 dtok = 1;
10613 	int exponent = 10;
10614 	int mantissa = 8192;
10615 	u8 min_twt = 255;
10616 	unsigned long long twt = 0;
10617 	bool requestor = true;
10618 	int setup_cmd = 0;
10619 	bool trigger = true;
10620 	bool implicit = true;
10621 	bool flow_type = true;
10622 	int flow_id = 0;
10623 	bool protection = false;
10624 	u8 twt_channel = 0;
10625 	u8 control = BIT(4); /* Control field (IEEE Std 802.11ax-2021,
10626 			      * Figure 9-687 - Control field format):
10627 			      * B4 = TWT Information Frame Disabled */
10628 	const char *tok_s;
10629 
10630 	tok_s = os_strstr(cmd, " dialog=");
10631 	if (tok_s)
10632 		dtok = atoi(tok_s + os_strlen(" dialog="));
10633 
10634 	tok_s = os_strstr(cmd, " exponent=");
10635 	if (tok_s)
10636 		exponent = atoi(tok_s + os_strlen(" exponent="));
10637 
10638 	tok_s = os_strstr(cmd, " mantissa=");
10639 	if (tok_s)
10640 		mantissa = atoi(tok_s + os_strlen(" mantissa="));
10641 
10642 	tok_s = os_strstr(cmd, " min_twt=");
10643 	if (tok_s)
10644 		min_twt = atoi(tok_s + os_strlen(" min_twt="));
10645 
10646 	tok_s = os_strstr(cmd, " setup_cmd=");
10647 	if (tok_s)
10648 		setup_cmd = atoi(tok_s + os_strlen(" setup_cmd="));
10649 
10650 	tok_s = os_strstr(cmd, " twt=");
10651 	if (tok_s &&
10652 	    sscanf(tok_s + os_strlen(" twt="), "%llu", &twt) != 1)
10653 		return -1;
10654 
10655 	tok_s = os_strstr(cmd, " requestor=");
10656 	if (tok_s)
10657 		requestor = atoi(tok_s + os_strlen(" requestor="));
10658 
10659 	tok_s = os_strstr(cmd, " trigger=");
10660 	if (tok_s)
10661 		trigger = atoi(tok_s + os_strlen(" trigger="));
10662 
10663 	tok_s = os_strstr(cmd, " implicit=");
10664 	if (tok_s)
10665 		implicit = atoi(tok_s + os_strlen(" implicit="));
10666 
10667 	tok_s = os_strstr(cmd, " flow_type=");
10668 	if (tok_s)
10669 		flow_type = atoi(tok_s + os_strlen(" flow_type="));
10670 
10671 	tok_s = os_strstr(cmd, " flow_id=");
10672 	if (tok_s)
10673 		flow_id = atoi(tok_s + os_strlen(" flow_id="));
10674 
10675 	tok_s = os_strstr(cmd, " protection=");
10676 	if (tok_s)
10677 		protection = atoi(tok_s + os_strlen(" protection="));
10678 
10679 	tok_s = os_strstr(cmd, " twt_channel=");
10680 	if (tok_s)
10681 		twt_channel = atoi(tok_s + os_strlen(" twt_channel="));
10682 
10683 	tok_s = os_strstr(cmd, " control=");
10684 	if (tok_s)
10685 		control = atoi(tok_s + os_strlen(" control="));
10686 
10687 	return wpas_twt_send_setup(wpa_s, dtok, exponent, mantissa, min_twt,
10688 				   setup_cmd, twt, requestor, trigger, implicit,
10689 				   flow_type, flow_id, protection, twt_channel,
10690 				   control);
10691 }
10692 
10693 
wpas_ctrl_iface_send_twt_teardown(struct wpa_supplicant * wpa_s,const char * cmd)10694 static int wpas_ctrl_iface_send_twt_teardown(struct wpa_supplicant *wpa_s,
10695 					     const char *cmd)
10696 {
10697 	u8 flags = 0x1;
10698 	const char *tok_s;
10699 
10700 	tok_s = os_strstr(cmd, " flags=");
10701 	if (tok_s)
10702 		flags = atoi(tok_s + os_strlen(" flags="));
10703 
10704 	return wpas_twt_send_teardown(wpa_s, flags);
10705 }
10706 
10707 
wpas_ctrl_iface_get_tk(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)10708 static int wpas_ctrl_iface_get_tk(struct wpa_supplicant *wpa_s, char *buf,
10709 				  size_t buflen)
10710 {
10711 	u8 tk[WPA_TK_MAX_LEN];
10712 	size_t tk_len = 0;
10713 	int ret;
10714 
10715 	if (wpa_sm_get_cached_tk(wpa_s->wpa, tk, &tk_len) < 0) {
10716 		ret = os_snprintf(buf, buflen, "FAIL");
10717 	} else {
10718 		ret = wpa_snprintf_hex(buf, buflen, tk, tk_len);
10719 		forced_memzero(tk, sizeof(tk));
10720 	}
10721 
10722 	return ret;
10723 }
10724 
10725 #endif /* CONFIG_TESTING_OPTIONS */
10726 
10727 
wpas_ctrl_vendor_elem_add(struct wpa_supplicant * wpa_s,char * cmd)10728 static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
10729 {
10730 	char *pos = cmd;
10731 	int frame;
10732 	size_t len;
10733 	struct wpabuf *buf;
10734 	struct ieee802_11_elems elems;
10735 
10736 	frame = atoi(pos);
10737 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
10738 		return -1;
10739 	wpa_s = wpas_vendor_elem(wpa_s, frame);
10740 
10741 	pos = os_strchr(pos, ' ');
10742 	if (pos == NULL)
10743 		return -1;
10744 	pos++;
10745 
10746 	len = os_strlen(pos);
10747 	if (len == 0)
10748 		return 0;
10749 	if (len & 1)
10750 		return -1;
10751 	len /= 2;
10752 
10753 	buf = wpabuf_alloc(len);
10754 	if (buf == NULL)
10755 		return -1;
10756 
10757 	if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
10758 		wpabuf_free(buf);
10759 		return -1;
10760 	}
10761 
10762 	if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
10763 	    ParseFailed) {
10764 		wpabuf_free(buf);
10765 		return -1;
10766 	}
10767 
10768 	if (wpa_s->vendor_elem[frame] == NULL) {
10769 		wpa_s->vendor_elem[frame] = buf;
10770 		goto update_ies;
10771 	}
10772 
10773 	if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
10774 		wpabuf_free(buf);
10775 		return -1;
10776 	}
10777 
10778 	wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
10779 	wpabuf_free(buf);
10780 
10781 update_ies:
10782 	wpas_vendor_elem_update(wpa_s);
10783 
10784 	if (frame == VENDOR_ELEM_PROBE_REQ ||
10785 	    frame == VENDOR_ELEM_PROBE_REQ_P2P)
10786 		wpa_supplicant_set_default_scan_ies(wpa_s);
10787 
10788 	return 0;
10789 }
10790 
10791 
wpas_ctrl_vendor_elem_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)10792 static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
10793 				     char *buf, size_t buflen)
10794 {
10795 	int frame = atoi(cmd);
10796 
10797 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
10798 		return -1;
10799 	wpa_s = wpas_vendor_elem(wpa_s, frame);
10800 
10801 	if (wpa_s->vendor_elem[frame] == NULL)
10802 		return 0;
10803 
10804 	return wpa_snprintf_hex(buf, buflen,
10805 				wpabuf_head_u8(wpa_s->vendor_elem[frame]),
10806 				wpabuf_len(wpa_s->vendor_elem[frame]));
10807 }
10808 
10809 
wpas_ctrl_vendor_elem_remove(struct wpa_supplicant * wpa_s,char * cmd)10810 static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
10811 {
10812 	char *pos = cmd;
10813 	int frame;
10814 	size_t len;
10815 	u8 *buf;
10816 	struct ieee802_11_elems elems;
10817 	int res;
10818 
10819 	frame = atoi(pos);
10820 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
10821 		return -1;
10822 	wpa_s = wpas_vendor_elem(wpa_s, frame);
10823 
10824 	pos = os_strchr(pos, ' ');
10825 	if (pos == NULL)
10826 		return -1;
10827 	pos++;
10828 
10829 	if (*pos == '*') {
10830 		wpabuf_free(wpa_s->vendor_elem[frame]);
10831 		wpa_s->vendor_elem[frame] = NULL;
10832 		wpas_vendor_elem_update(wpa_s);
10833 		return 0;
10834 	}
10835 
10836 	if (wpa_s->vendor_elem[frame] == NULL)
10837 		return -1;
10838 
10839 	len = os_strlen(pos);
10840 	if (len == 0)
10841 		return 0;
10842 	if (len & 1)
10843 		return -1;
10844 	len /= 2;
10845 
10846 	buf = os_malloc(len);
10847 	if (buf == NULL)
10848 		return -1;
10849 
10850 	if (hexstr2bin(pos, buf, len) < 0) {
10851 		os_free(buf);
10852 		return -1;
10853 	}
10854 
10855 	if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
10856 		os_free(buf);
10857 		return -1;
10858 	}
10859 
10860 	res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
10861 	os_free(buf);
10862 	return res;
10863 }
10864 
10865 
10866 #ifndef CONFIG_NO_RRM
10867 
wpas_ctrl_neighbor_rep_cb(void * ctx,struct wpabuf * neighbor_rep)10868 static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
10869 {
10870 	struct wpa_supplicant *wpa_s = ctx;
10871 	size_t len;
10872 	const u8 *data;
10873 
10874 	/*
10875 	 * Neighbor Report element (IEEE Std 802.11-2024, 9.4.2.35)
10876 	 * BSSID[6]
10877 	 * BSSID Information[4]
10878 	 * Operating Class[1]
10879 	 * Channel Number[1]
10880 	 * PHY Type[1]
10881 	 * Optional Subelements[variable]
10882 	 */
10883 #define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
10884 
10885 	if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
10886 		wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
10887 		goto out;
10888 	}
10889 
10890 	data = wpabuf_head_u8(neighbor_rep);
10891 	len = wpabuf_len(neighbor_rep);
10892 
10893 	while (len >= 2 + NR_IE_MIN_LEN) {
10894 		const u8 *nr;
10895 		char lci[256 * 2 + 1];
10896 		char civic[256 * 2 + 1];
10897 		u8 nr_len = data[1];
10898 		const u8 *pos = data, *end;
10899 
10900 		if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
10901 		    nr_len < NR_IE_MIN_LEN) {
10902 			wpa_dbg(wpa_s, MSG_DEBUG,
10903 				"CTRL: Invalid Neighbor Report element: id=%u len=%u",
10904 				data[0], nr_len);
10905 			goto out;
10906 		}
10907 
10908 		if (2U + nr_len > len) {
10909 			wpa_dbg(wpa_s, MSG_DEBUG,
10910 				"CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
10911 				data[0], len, nr_len);
10912 			goto out;
10913 		}
10914 		pos += 2;
10915 		end = pos + nr_len;
10916 
10917 		nr = pos;
10918 		pos += NR_IE_MIN_LEN;
10919 
10920 		lci[0] = '\0';
10921 		civic[0] = '\0';
10922 		while (end - pos > 2) {
10923 			u8 s_id, s_len;
10924 
10925 			s_id = *pos++;
10926 			s_len = *pos++;
10927 			if (s_len > end - pos)
10928 				goto out;
10929 			if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
10930 				/* Measurement Token[1] */
10931 				/* Measurement Report Mode[1] */
10932 				/* Measurement Type[1] */
10933 				/* Measurement Report[variable] */
10934 				switch (pos[2]) {
10935 				case MEASURE_TYPE_LCI:
10936 					if (lci[0])
10937 						break;
10938 					wpa_snprintf_hex(lci, sizeof(lci),
10939 							 pos, s_len);
10940 					break;
10941 				case MEASURE_TYPE_LOCATION_CIVIC:
10942 					if (civic[0])
10943 						break;
10944 					wpa_snprintf_hex(civic, sizeof(civic),
10945 							 pos, s_len);
10946 					break;
10947 				}
10948 			}
10949 
10950 			pos += s_len;
10951 		}
10952 
10953 		wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
10954 			"bssid=" MACSTR
10955 			" info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
10956 			MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
10957 			nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
10958 			nr[ETH_ALEN + 6],
10959 			lci[0] ? " lci=" : "", lci,
10960 			civic[0] ? " civic=" : "", civic);
10961 
10962 		data = end;
10963 		len -= 2 + nr_len;
10964 	}
10965 
10966 out:
10967 	wpabuf_free(neighbor_rep);
10968 }
10969 
10970 
wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant * wpa_s,char * cmd)10971 static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
10972 					     char *cmd)
10973 {
10974 	struct wpa_ssid_value ssid, *ssid_p = NULL;
10975 	int ret, lci = 0, civic = 0;
10976 	char *ssid_s;
10977 
10978 	ssid_s = os_strstr(cmd, "ssid=");
10979 	if (ssid_s) {
10980 		if (ssid_parse(ssid_s + 5, &ssid)) {
10981 			wpa_msg(wpa_s, MSG_INFO,
10982 				"CTRL: Send Neighbor Report: bad SSID");
10983 			return -1;
10984 		}
10985 
10986 		ssid_p = &ssid;
10987 
10988 		/*
10989 		 * Move cmd after the SSID text that may include "lci" or
10990 		 * "civic".
10991 		 */
10992 		cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
10993 		if (cmd)
10994 			cmd++;
10995 
10996 	}
10997 
10998 	if (cmd && os_strstr(cmd, "lci"))
10999 		lci = 1;
11000 
11001 	if (cmd && os_strstr(cmd, "civic"))
11002 		civic = 1;
11003 
11004 	ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
11005 						 wpas_ctrl_neighbor_rep_cb,
11006 						 wpa_s);
11007 
11008 	return ret;
11009 }
11010 
11011 #endif /* CONFIG_NO_RRM */
11012 
11013 
wpas_ctrl_iface_erp_flush(struct wpa_supplicant * wpa_s)11014 static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
11015 {
11016 	eapol_sm_erp_flush(wpa_s->eapol);
11017 	return 0;
11018 }
11019 
11020 
wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant * wpa_s,char * cmd)11021 static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
11022 					 char *cmd)
11023 {
11024 	char *token, *context = NULL;
11025 	unsigned int enable = ~0, type = 0;
11026 	u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
11027 	u8 *addr = NULL, *mask = NULL;
11028 
11029 	while ((token = str_token(cmd, " ", &context))) {
11030 		if (os_strcasecmp(token, "scan") == 0) {
11031 			type |= MAC_ADDR_RAND_SCAN;
11032 		} else if (os_strcasecmp(token, "sched") == 0) {
11033 			type |= MAC_ADDR_RAND_SCHED_SCAN;
11034 		} else if (os_strcasecmp(token, "pno") == 0) {
11035 			type |= MAC_ADDR_RAND_PNO;
11036 		} else if (os_strcasecmp(token, "all") == 0) {
11037 			type = wpa_s->mac_addr_rand_supported;
11038 		} else if (os_strncasecmp(token, "enable=", 7) == 0) {
11039 			enable = atoi(token + 7);
11040 		} else if (os_strncasecmp(token, "addr=", 5) == 0) {
11041 			addr = _addr;
11042 			if (hwaddr_aton(token + 5, addr)) {
11043 				wpa_printf(MSG_INFO,
11044 					   "CTRL: Invalid MAC address: %s",
11045 					   token);
11046 				return -1;
11047 			}
11048 		} else if (os_strncasecmp(token, "mask=", 5) == 0) {
11049 			mask = _mask;
11050 			if (hwaddr_aton(token + 5, mask)) {
11051 				wpa_printf(MSG_INFO,
11052 					   "CTRL: Invalid MAC address mask: %s",
11053 					   token);
11054 				return -1;
11055 			}
11056 		} else {
11057 			wpa_printf(MSG_INFO,
11058 				   "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
11059 				   token);
11060 			return -1;
11061 		}
11062 	}
11063 
11064 	if (!type) {
11065 		wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
11066 		return -1;
11067 	}
11068 
11069 	if (enable > 1) {
11070 		wpa_printf(MSG_INFO,
11071 			   "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
11072 		return -1;
11073 	}
11074 
11075 	if (!enable)
11076 		return wpas_disable_mac_addr_randomization(wpa_s, type);
11077 
11078 	return wpas_enable_mac_addr_randomization(wpa_s, type, addr, mask);
11079 }
11080 
11081 
wpas_ctrl_iface_pmksa(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)11082 static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
11083 				 char *buf, size_t buflen)
11084 {
11085 	size_t reply_len;
11086 
11087 	reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
11088 #ifdef CONFIG_AP
11089 	reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
11090 					      buflen - reply_len);
11091 #endif /* CONFIG_AP */
11092 	return reply_len;
11093 }
11094 
11095 
wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant * wpa_s)11096 static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
11097 {
11098 	ptksa_cache_flush(wpa_s->ptksa, NULL, WPA_CIPHER_NONE);
11099 	wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
11100 #ifdef CONFIG_AP
11101 	wpas_ap_pmksa_cache_flush(wpa_s);
11102 #endif /* CONFIG_AP */
11103 }
11104 
11105 #ifdef CONFIG_P2P
11106 #ifdef CONFIG_PASN
11107 
11108 #ifdef CONFIG_TESTING_OPTIONS
p2p_ctrl_get_pasn_ptk(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)11109 static int p2p_ctrl_get_pasn_ptk(struct wpa_supplicant *wpa_s, char *buf,
11110 				 size_t buflen)
11111 {
11112 	const u8 *ptk;
11113 	size_t ptk_len;
11114 
11115 	if (wpas_p2p_get_pasn_ptk(wpa_s, &ptk, &ptk_len))
11116 		return -1;
11117 	return wpa_snprintf_hex(buf, buflen, ptk, ptk_len);
11118 }
11119 #endif /* CONFIG_TESTING_OPTIONS */
11120 
11121 #endif /* CONFIG_PASN */
11122 #endif /* CONFIG_P2P */
11123 
11124 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
11125 
wpas_ctrl_iface_pmksa_get(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)11126 static int wpas_ctrl_iface_pmksa_get(struct wpa_supplicant *wpa_s,
11127 				     const char *cmd, char *buf, size_t buflen)
11128 {
11129 	struct rsn_pmksa_cache_entry *entry;
11130 	struct wpa_ssid *ssid;
11131 	char *pos, *pos2, *end;
11132 	int ret;
11133 	struct os_reltime now;
11134 
11135 	ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
11136 	if (!ssid)
11137 		return -1;
11138 
11139 	pos = buf;
11140 	end = buf + buflen;
11141 
11142 	os_get_reltime(&now);
11143 
11144 	/*
11145 	 * Entry format:
11146 	 * <BSSID> <PMKID> <PMK> <reauth_time in seconds>
11147 	 * <expiration in seconds> <akmp> <opportunistic>
11148 	 * [FILS Cache Identifier]
11149 	 */
11150 
11151 	for (entry = wpa_sm_pmksa_cache_head(wpa_s->wpa); entry;
11152 	     entry = entry->next) {
11153 		if (entry->network_ctx != ssid)
11154 			continue;
11155 
11156 		pos2 = pos;
11157 		ret = os_snprintf(pos2, end - pos2, MACSTR " ",
11158 				  MAC2STR(entry->aa));
11159 		if (os_snprintf_error(end - pos2, ret))
11160 			break;
11161 		pos2 += ret;
11162 
11163 		pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmkid,
11164 					 PMKID_LEN);
11165 
11166 		ret = os_snprintf(pos2, end - pos2, " ");
11167 		if (os_snprintf_error(end - pos2, ret))
11168 			break;
11169 		pos2 += ret;
11170 
11171 		pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmk,
11172 					 entry->pmk_len);
11173 
11174 		ret = os_snprintf(pos2, end - pos2, " %d %d %d %d",
11175 				  (int) (entry->reauth_time - now.sec),
11176 				  (int) (entry->expiration - now.sec),
11177 				  entry->akmp,
11178 				  entry->opportunistic);
11179 		if (os_snprintf_error(end - pos2, ret))
11180 			break;
11181 		pos2 += ret;
11182 
11183 		if (entry->fils_cache_id_set) {
11184 			ret = os_snprintf(pos2, end - pos2, " %02x%02x",
11185 					  entry->fils_cache_id[0],
11186 					  entry->fils_cache_id[1]);
11187 			if (os_snprintf_error(end - pos2, ret))
11188 				break;
11189 			pos2 += ret;
11190 		}
11191 
11192 		ret = os_snprintf(pos2, end - pos2, "\n");
11193 		if (os_snprintf_error(end - pos2, ret))
11194 			break;
11195 		pos2 += ret;
11196 
11197 		pos = pos2;
11198 	}
11199 
11200 	return pos - buf;
11201 }
11202 
11203 
wpas_ctrl_iface_pmksa_add(struct wpa_supplicant * wpa_s,char * cmd)11204 static int wpas_ctrl_iface_pmksa_add(struct wpa_supplicant *wpa_s,
11205 				     char *cmd)
11206 {
11207 	struct rsn_pmksa_cache_entry *entry;
11208 	struct wpa_ssid *ssid;
11209 	char *pos, *pos2;
11210 	int ret = -1;
11211 	struct os_reltime now;
11212 	int reauth_time = 0, expiration = 0, i;
11213 
11214 	/*
11215 	 * Entry format:
11216 	 * <network_id> <BSSID> <PMKID> <PMK> <reauth_time in seconds>
11217 	 * <expiration in seconds> <akmp> <opportunistic>
11218 	 * [FILS Cache Identifier]
11219 	 */
11220 
11221 	ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
11222 	if (!ssid)
11223 		return -1;
11224 
11225 	pos = os_strchr(cmd, ' ');
11226 	if (!pos)
11227 		return -1;
11228 	pos++;
11229 
11230 	entry = os_zalloc(sizeof(*entry));
11231 	if (!entry)
11232 		return -1;
11233 
11234 	if (hwaddr_aton(pos, entry->aa))
11235 		goto fail;
11236 
11237 	pos = os_strchr(pos, ' ');
11238 	if (!pos)
11239 		goto fail;
11240 	pos++;
11241 
11242 	if (hexstr2bin(pos, entry->pmkid, PMKID_LEN) < 0)
11243 		goto fail;
11244 
11245 	pos = os_strchr(pos, ' ');
11246 	if (!pos)
11247 		goto fail;
11248 	pos++;
11249 
11250 	pos2 = os_strchr(pos, ' ');
11251 	if (!pos2)
11252 		goto fail;
11253 	entry->pmk_len = (pos2 - pos) / 2;
11254 	if (entry->pmk_len < PMK_LEN || entry->pmk_len > PMK_LEN_MAX ||
11255 	    hexstr2bin(pos, entry->pmk, entry->pmk_len) < 0)
11256 		goto fail;
11257 
11258 	pos = os_strchr(pos, ' ');
11259 	if (!pos)
11260 		goto fail;
11261 	pos++;
11262 
11263 	if (sscanf(pos, "%d %d %d %d", &reauth_time, &expiration,
11264 		   &entry->akmp, &entry->opportunistic) != 4)
11265 		goto fail;
11266 	if (reauth_time > expiration)
11267 		goto fail;
11268 	for (i = 0; i < 4; i++) {
11269 		pos = os_strchr(pos, ' ');
11270 		if (!pos) {
11271 			if (i < 3)
11272 				goto fail;
11273 			break;
11274 		}
11275 		pos++;
11276 	}
11277 	if (pos) {
11278 		if (hexstr2bin(pos, entry->fils_cache_id,
11279 			       FILS_CACHE_ID_LEN) < 0)
11280 			goto fail;
11281 		entry->fils_cache_id_set = 1;
11282 	}
11283 	os_get_reltime(&now);
11284 	entry->expiration = now.sec + expiration;
11285 	entry->reauth_time = now.sec + reauth_time;
11286 
11287 	entry->network_ctx = ssid;
11288 	os_memcpy(entry->spa, wpa_s->own_addr, ETH_ALEN);
11289 
11290 	entry->external = true;
11291 
11292 	wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
11293 	entry = NULL;
11294 	ret = 0;
11295 fail:
11296 	os_free(entry);
11297 	return ret;
11298 }
11299 
11300 
11301 #ifdef CONFIG_MESH
11302 
wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)11303 static int wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant *wpa_s,
11304 					  const char *cmd, char *buf,
11305 					  size_t buflen)
11306 {
11307 	u8 spa[ETH_ALEN];
11308 
11309 	if (!wpa_s->ifmsh)
11310 		return -1;
11311 
11312 	if (os_strcasecmp(cmd, "any") == 0)
11313 		return wpas_ap_pmksa_cache_list_mesh(wpa_s, NULL, buf, buflen);
11314 
11315 	if (hwaddr_aton(cmd, spa))
11316 		return -1;
11317 
11318 	return wpas_ap_pmksa_cache_list_mesh(wpa_s, spa, buf, buflen);
11319 }
11320 
11321 
wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant * wpa_s,char * cmd)11322 static int wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant *wpa_s,
11323 					  char *cmd)
11324 {
11325 	/*
11326 	 * We do not check mesh interface existence because PMKSA should be
11327 	 * stored before wpa_s->ifmsh creation to suppress commit message
11328 	 * creation.
11329 	 */
11330 	return wpas_ap_pmksa_cache_add_external(wpa_s, cmd);
11331 }
11332 
11333 #endif /* CONFIG_MESH */
11334 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
11335 
11336 
11337 #ifdef CONFIG_FILS
wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant * wpa_s,const char * cmd)11338 static int wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant *wpa_s,
11339 					    const char *cmd)
11340 {
11341 	struct fils_hlp_req *req;
11342 	const char *pos;
11343 
11344 	/* format: <dst> <packet starting from ethertype> */
11345 
11346 	req = os_zalloc(sizeof(*req));
11347 	if (!req)
11348 		return -1;
11349 
11350 	if (hwaddr_aton(cmd, req->dst))
11351 		goto fail;
11352 
11353 	pos = os_strchr(cmd, ' ');
11354 	if (!pos)
11355 		goto fail;
11356 	pos++;
11357 	req->pkt = wpabuf_parse_bin(pos);
11358 	if (!req->pkt)
11359 		goto fail;
11360 
11361 	dl_list_add_tail(&wpa_s->fils_hlp_req, &req->list);
11362 	return 0;
11363 fail:
11364 	wpabuf_free(req->pkt);
11365 	os_free(req);
11366 	return -1;
11367 }
11368 #endif /* CONFIG_FILS */
11369 
11370 
wpas_ctrl_cmd_debug_level(const char * cmd)11371 int wpas_ctrl_cmd_debug_level(const char *cmd)
11372 {
11373 	if (os_strcmp(cmd, "PING") == 0 ||
11374 	    os_strncmp(cmd, "BSS ", 4) == 0 ||
11375 	    os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
11376 	    os_strncmp(cmd, "STATUS", 6) == 0 ||
11377 	    os_strncmp(cmd, "STA ", 4) == 0 ||
11378 	    os_strncmp(cmd, "STA-", 4) == 0)
11379 		return MSG_EXCESSIVE;
11380 	return MSG_DEBUG;
11381 }
11382 
11383 
11384 #ifndef CONFIG_NO_ROBUST_AV
wpas_ctrl_iface_configure_mscs(struct wpa_supplicant * wpa_s,const char * cmd)11385 static int wpas_ctrl_iface_configure_mscs(struct wpa_supplicant *wpa_s,
11386 					  const char *cmd)
11387 {
11388 	size_t frame_classifier_len;
11389 	const char *pos, *end;
11390 	struct robust_av_data *robust_av = &wpa_s->robust_av;
11391 	int val;
11392 
11393 	/*
11394 	 * format:
11395 	 * <add|remove|change> [up_bitmap=<hex byte>] [up_limit=<integer>]
11396 	 * [stream_timeout=<in TUs>] [frame_classifier=<hex bytes>]
11397 	 */
11398 	os_memset(robust_av, 0, sizeof(struct robust_av_data));
11399 	if (os_strncmp(cmd, "add ", 4) == 0) {
11400 		robust_av->request_type = SCS_REQ_ADD;
11401 	} else if (os_strcmp(cmd, "remove") == 0) {
11402 		robust_av->request_type = SCS_REQ_REMOVE;
11403 		robust_av->valid_config = false;
11404 		return wpas_send_mscs_req(wpa_s);
11405 	} else if (os_strncmp(cmd, "change ", 7) == 0) {
11406 		robust_av->request_type = SCS_REQ_CHANGE;
11407 	} else {
11408 		return -1;
11409 	}
11410 
11411 	pos = os_strstr(cmd, "up_bitmap=");
11412 	if (!pos)
11413 		return -1;
11414 
11415 	val = hex2byte(pos + 10);
11416 	if (val < 0)
11417 		return -1;
11418 	robust_av->up_bitmap = val;
11419 
11420 	pos = os_strstr(cmd, "up_limit=");
11421 	if (!pos)
11422 		return -1;
11423 
11424 	robust_av->up_limit = atoi(pos + 9);
11425 
11426 	pos = os_strstr(cmd, "stream_timeout=");
11427 	if (!pos)
11428 		return -1;
11429 
11430 	robust_av->stream_timeout = atoi(pos + 15);
11431 	if (robust_av->stream_timeout == 0)
11432 		return -1;
11433 
11434 	pos = os_strstr(cmd, "frame_classifier=");
11435 	if (!pos)
11436 		return -1;
11437 
11438 	pos += 17;
11439 	end = os_strchr(pos, ' ');
11440 	if (!end)
11441 		end = pos + os_strlen(pos);
11442 
11443 	frame_classifier_len = (end - pos) / 2;
11444 	if (frame_classifier_len > sizeof(robust_av->frame_classifier) ||
11445 	    hexstr2bin(pos, robust_av->frame_classifier, frame_classifier_len))
11446 		return -1;
11447 
11448 	robust_av->frame_classifier_len = frame_classifier_len;
11449 	robust_av->valid_config = true;
11450 
11451 	return wpas_send_mscs_req(wpa_s);
11452 }
11453 #endif /* CONFIG_NO_ROBUST_AV */
11454 
11455 
11456 #ifdef CONFIG_PASN
wpas_ctrl_iface_pasn_start(struct wpa_supplicant * wpa_s,char * cmd)11457 static int wpas_ctrl_iface_pasn_start(struct wpa_supplicant *wpa_s, char *cmd)
11458 {
11459 	char *token, *context = NULL;
11460 	u8 bssid[ETH_ALEN];
11461 	int akmp = -1, cipher = -1, got_bssid = 0;
11462 	u16 group = 0xFFFF;
11463 	u8 *comeback = NULL;
11464 	size_t comeback_len = 0;
11465 	int id = 0, ret = -1;
11466 
11467 	/*
11468 	 * Entry format: bssid=<BSSID> akmp=<AKMP> cipher=<CIPHER> group=<group>
11469 	 *    [comeback=<hexdump>]
11470 	 */
11471 	while ((token = str_token(cmd, " ", &context))) {
11472 		if (os_strncmp(token, "bssid=", 6) == 0) {
11473 			if (hwaddr_aton(token + 6, bssid))
11474 				goto out;
11475 			got_bssid = 1;
11476 		} else if (os_strcmp(token, "akmp=PASN") == 0) {
11477 			akmp = WPA_KEY_MGMT_PASN;
11478 #ifdef CONFIG_IEEE80211R
11479 		} else if (os_strcmp(token, "akmp=FT-PSK") == 0) {
11480 			akmp = WPA_KEY_MGMT_FT_PSK;
11481 		} else if (os_strcmp(token, "akmp=FT-EAP-SHA384") == 0) {
11482 			akmp = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
11483 		} else if (os_strcmp(token, "akmp=FT-EAP") == 0) {
11484 			akmp = WPA_KEY_MGMT_FT_IEEE8021X;
11485 #endif /* CONFIG_IEEE80211R */
11486 #ifdef CONFIG_SAE
11487 		} else if (os_strcmp(token, "akmp=SAE") == 0) {
11488 			akmp = WPA_KEY_MGMT_SAE;
11489 		} else if (os_strcmp(token, "akmp=SAE-EXT-KEY") == 0) {
11490 			akmp = WPA_KEY_MGMT_SAE_EXT_KEY;
11491 #endif /* CONFIG_SAE */
11492 #ifdef CONFIG_FILS
11493 		} else if (os_strcmp(token, "akmp=FILS-SHA256") == 0) {
11494 			akmp = WPA_KEY_MGMT_FILS_SHA256;
11495 		} else if (os_strcmp(token, "akmp=FILS-SHA384") == 0) {
11496 			akmp = WPA_KEY_MGMT_FILS_SHA384;
11497 #endif /* CONFIG_FILS */
11498 		} else if (os_strcmp(token, "cipher=CCMP-256") == 0) {
11499 			cipher = WPA_CIPHER_CCMP_256;
11500 		} else if (os_strcmp(token, "cipher=GCMP-256") == 0) {
11501 			cipher = WPA_CIPHER_GCMP_256;
11502 		} else if (os_strcmp(token, "cipher=CCMP") == 0) {
11503 			cipher = WPA_CIPHER_CCMP;
11504 		} else if (os_strcmp(token, "cipher=GCMP") == 0) {
11505 			cipher = WPA_CIPHER_GCMP;
11506 		} else if (os_strncmp(token, "group=", 6) == 0) {
11507 			group = atoi(token + 6);
11508 		} else if (os_strncmp(token, "nid=", 4) == 0) {
11509 			id = atoi(token + 4);
11510 		} else if (os_strncmp(token, "comeback=", 9) == 0) {
11511 			comeback_len = os_strlen(token + 9);
11512 			if (comeback || !comeback_len || comeback_len % 2)
11513 				goto out;
11514 
11515 			comeback_len /= 2;
11516 			comeback = os_malloc(comeback_len);
11517 			if (!comeback ||
11518 			    hexstr2bin(token + 9, comeback, comeback_len))
11519 				goto out;
11520 		} else {
11521 			wpa_printf(MSG_DEBUG,
11522 				   "CTRL: PASN Invalid parameter: '%s'",
11523 				   token);
11524 			goto out;
11525 		}
11526 	}
11527 
11528 	if (!got_bssid || akmp == -1 || cipher == -1 || group == 0xFFFF) {
11529 		wpa_printf(MSG_DEBUG,"CTRL: PASN missing parameter");
11530 		goto out;
11531 	}
11532 
11533 	ret = wpas_pasn_auth_start(wpa_s, wpa_s->own_addr, bssid, akmp, cipher,
11534 				   group, id, comeback, comeback_len,
11535 				   WLAN_AUTH_PASN, 0, 0, 0, NULL, false);
11536 out:
11537 	os_free(comeback);
11538 	return ret;
11539 }
11540 
11541 
wpas_ctrl_iface_pasn_deauthenticate(struct wpa_supplicant * wpa_s,const char * cmd)11542 static int wpas_ctrl_iface_pasn_deauthenticate(struct wpa_supplicant *wpa_s,
11543 					       const char *cmd)
11544 {
11545 	u8 bssid[ETH_ALEN];
11546 
11547 	if (os_strncmp(cmd, "bssid=", 6) != 0 || hwaddr_aton(cmd + 6, bssid)) {
11548 		wpa_printf(MSG_DEBUG,
11549 			   "CTRL: PASN_DEAUTH without valid BSSID");
11550 		return -1;
11551 	}
11552 
11553 	return wpas_pasn_deauthenticate(wpa_s, wpa_s->own_addr, bssid);
11554 }
11555 
11556 
11557 #ifdef CONFIG_PR
11558 
wpas_ctrl_iface_pr_pasn_start(struct wpa_supplicant * wpa_s,char * cmd)11559 static int wpas_ctrl_iface_pr_pasn_start(struct wpa_supplicant *wpa_s,
11560 					 char *cmd)
11561 {
11562 	char *token, *context = NULL;
11563 	struct pr_pasn_ranging_params params;
11564 	bool got_addr = false;
11565 	int interval_time = 0;
11566 
11567 	os_memset(&params, 0, sizeof(params));
11568 	params.action = PR_PASN_AND_RANGING;
11569 
11570 	while ((token = str_token(cmd, " ", &context))) {
11571 		if (os_strncmp(token, "addr=", 5) == 0) {
11572 			if (hwaddr_aton(token + 5, params.peer_addr))
11573 				return -1;
11574 			got_addr = true;
11575 		} else if (os_strcmp(token, "role=ISTA") == 0) {
11576 			params.ranging_role |= PR_ISTA_SUPPORT;
11577 		} else if (os_strcmp(token, "role=RSTA") == 0) {
11578 			params.ranging_role |= PR_RSTA_SUPPORT;
11579 		} else if (os_strcmp(token, "ranging_type=EDCA") == 0) {
11580 			params.ranging_type |= PR_EDCA_BASED_RANGING;
11581 		} else if (os_strcmp(token, "ranging_type=NTB-OPEN-PHY") == 0) {
11582 			params.ranging_type |= PR_NTB_OPEN_BASED_RANGING;
11583 		} else if (os_strcmp(token, "ranging_type=NTB-SEC-PHY") == 0) {
11584 			params.ranging_type |= PR_NTB_SECURE_LTF_BASED_RANGING;
11585 		} else if (os_strncmp(token, "freq=", 5) == 0) {
11586 			params.freq = atoi(token + 5);
11587 		} else if (os_strncmp(token, "auth=", 5) == 0) {
11588 			params.auth_mode = atoi(token + 5);
11589 		} else if (os_strncmp(token, "forced_pr_freq=", 15) == 0) {
11590 			params.forced_pr_freq = atoi(token + 15);
11591 		} else if (os_strncmp(token, "num_bursts_exp=", 15) == 0) {
11592 			params.num_bursts_exp = atoi(token + 15);
11593 		} else if (os_strncmp(token, "ftmr_retries=", 13) == 0) {
11594 			params.ftmr_retries = atoi(token + 13);
11595 		} else if (os_strncmp(token, "burst_duration=", 15) == 0) {
11596 			params.burst_duration = atoi(token + 15);
11597 		} else if (os_strncmp(token, "ftms_per_burst=", 15) == 0) {
11598 			params.ftms_per_burst = atoi(token + 15);
11599 		} else if (os_strncmp(token, "interval_time=", 14) == 0) {
11600 			interval_time = atoi(token + 14);
11601 		} else if (os_strncmp(token, "min_time_between_meas=", 22) ==
11602 			   0) {
11603 			params.min_time_between_measurements = atoi(token + 22);
11604 		} else if (os_strncmp(token, "max_time_between_meas=", 22) ==
11605 			   0) {
11606 			params.max_time_between_measurements = atoi(token + 22);
11607 		} else if (os_strncmp(token, "availability_window=", 20) == 0) {
11608 			params.availability_window = atoi(token + 20);
11609 		} else if (os_strcmp(token, "request_lci=1") == 0) {
11610 			params.request_lci = true;
11611 		} else if (os_strcmp(token, "request_civicloc=1") == 0) {
11612 			params.request_civicloc = true;
11613 		} else if (os_strncmp(token, "continuous_session_time=", 24) ==
11614 			   0) {
11615 			params.continuous_ranging_session_time =
11616 				atoi(token + 24);
11617 		} else if (os_strncmp(token, "src_addr=", 9) == 0) {
11618 			if (hwaddr_aton(token + 9, params.src_addr))
11619 				return -1;
11620 		} else if (os_strcmp(token, "pasn_role=INITIATOR") == 0) {
11621 			params.pasn_role = PR_ROLE_PASN_INITIATOR;
11622 		} else if (os_strcmp(token, "pasn_role=RESPONDER") == 0) {
11623 			params.pasn_role = PR_ROLE_PASN_RESPONDER;
11624 		} else if (os_strcmp(token, "lmr_feedback=1") == 0) {
11625 			params.lmr_feedback = true;
11626 		} else if (os_strncmp(token, "ingress_threshold=", 18) == 0) {
11627 			params.ingress_threshold = atoll(token + 18);
11628 		} else if (os_strncmp(token, "egress_threshold=", 17) == 0) {
11629 			params.egress_threshold = atoll(token + 17);
11630 		} else if (os_strcmp(token, "pd_suppress_results=1") == 0) {
11631 			params.pr_suppress_results = true;
11632 		} else if (os_strncmp(token, "password=", 9) == 0) {
11633 			size_t pwd_len = os_strlen(token + 9);
11634 
11635 			if (pwd_len == 0 ||
11636 			    pwd_len >= sizeof(params.password)) {
11637 				wpa_printf(MSG_INFO,
11638 					   "CTRL: PR_PASN_START invalid password length %zu",
11639 					   pwd_len);
11640 				return -1;
11641 			}
11642 			os_strlcpy(params.password, token + 9,
11643 				   sizeof(params.password));
11644 			params.password_valid = true;
11645 		} else if (os_strncmp(token, "pmk=", 4) == 0) {
11646 			size_t pmk_len = os_strlen(token + 4) / 2;
11647 
11648 			if (pmk_len != 32 && pmk_len != 48 && pmk_len != 64) {
11649 				wpa_printf(MSG_INFO,
11650 					   "CTRL: PR_PASN_START invalid PMK length %zu",
11651 					   pmk_len);
11652 				return -1;
11653 			}
11654 			if (hexstr2bin(token + 4, params.pmk, pmk_len)) {
11655 				wpa_printf(MSG_INFO,
11656 					   "CTRL: PR_PASN_START invalid PMK");
11657 				return -1;
11658 			}
11659 			params.pmk_len = pmk_len;
11660 		} else {
11661 			wpa_printf(MSG_DEBUG,
11662 				   "CTRL: PR_PASN_START invalid parameter: '%s'",
11663 				   token);
11664 			return -1;
11665 		}
11666 	}
11667 
11668 	/*
11669 	 * Map interval_time to the appropriate protocol-specific field:
11670 	 * - EDCA: burst_period
11671 	 * - NTB: nominal_time (nominal time between measurements)
11672 	 */
11673 	if (interval_time > 0) {
11674 		if (params.ranging_type & PR_EDCA_BASED_RANGING)
11675 			params.burst_period = interval_time;
11676 		else if (params.ranging_type &
11677 			 (PR_NTB_OPEN_BASED_RANGING |
11678 			  PR_NTB_SECURE_LTF_BASED_RANGING))
11679 			params.nominal_time = interval_time;
11680 	}
11681 
11682 	if (!got_addr || params.ranging_type == 0 || params.ranging_role == 0 ||
11683 	    params.freq == 0) {
11684 		wpa_printf(MSG_DEBUG,
11685 			   "CTRL: PR_PASN_START missing parameter");
11686 		return -1;
11687 	}
11688 
11689 	/* pmk and password are only valid for authenticated modes */
11690 	if (params.auth_mode == PR_PASN_AUTH_MODE_PASN &&
11691 	    (params.pmk_len > 0 || params.password_valid)) {
11692 		wpa_printf(MSG_INFO,
11693 			   "CTRL: PR_PASN_START pmk/password not applicable for unauthenticated mode");
11694 		return -1;
11695 	}
11696 
11697 	wpa_printf(MSG_DEBUG,
11698 		   "CTRL: PR_PASN_START params: ranging type=0x%x, role=0x%x,"
11699 		   " auth_mode=%d, freq=%d, addr=" MACSTR " src_addr=" MACSTR " pasn_role=%d",
11700 		   params.ranging_type, params.ranging_role, params.auth_mode,
11701 		   params.freq, MAC2STR(params.peer_addr),
11702 		   MAC2STR(params.src_addr), params.pasn_role);
11703 
11704 	return wpas_pr_pasn_trigger(wpa_s, &params);
11705 }
11706 
11707 
wpas_ctrl_iface_pr_set_dik_ctx(struct wpa_supplicant * wpa_s,char * cmd)11708 static int wpas_ctrl_iface_pr_set_dik_ctx(struct wpa_supplicant *wpa_s,
11709 					  char *cmd)
11710 {
11711 	int ret = -1;
11712 	bool own = false;
11713 	char *token, *context = NULL;
11714 	const char *password = NULL;
11715 	const u8 *pmk = NULL, *dik = NULL;
11716 	struct wpabuf *pmk_buf = NULL, *dik_buf = NULL;
11717 	size_t pmk_len;
11718 
11719 	while ((token = str_token(cmd, " ", &context))) {
11720 		if (os_strcmp(token, "self") == 0) {
11721 			own = true;
11722 			continue;
11723 		}
11724 
11725 		if (os_strncmp(token, "dik=", 4) == 0) {
11726 			dik_buf = wpabuf_parse_bin(token + 4);
11727 			if (!dik_buf)
11728 				goto fail;
11729 			dik = wpabuf_head_u8(dik_buf);
11730 			continue;
11731 		}
11732 
11733 		if (os_strncmp(token, "password=", 9) == 0) {
11734 			password = token + 9;
11735 			continue;
11736 		}
11737 
11738 		if (os_strncmp(token, "pmk=", 4) == 0) {
11739 			pmk_buf = wpabuf_parse_bin(token + 4);
11740 			if (!pmk_buf)
11741 				goto fail;
11742 			pmk = wpabuf_head_u8(pmk_buf);
11743 			pmk_len = wpabuf_len(pmk_buf);
11744 			continue;
11745 		}
11746 	}
11747 
11748 	if (!dik)
11749 		goto fail;
11750 
11751 	wpas_pr_set_dev_ik(wpa_s, dik, password, pmk, pmk_len, own);
11752 	ret = 0;
11753 fail:
11754 	wpabuf_clear_free(dik_buf);
11755 	wpabuf_clear_free(pmk_buf);
11756 	return ret;
11757 }
11758 
11759 #endif /* CONFIG_PR */
11760 
11761 
11762 #ifdef CONFIG_TESTING_OPTIONS
wpas_ctrl_iface_pasn_driver(struct wpa_supplicant * wpa_s,char * cmd)11763 static int wpas_ctrl_iface_pasn_driver(struct wpa_supplicant *wpa_s, char *cmd)
11764 {
11765 	char *token, *context = NULL;
11766 	u8 bssid[ETH_ALEN];
11767 	u8 *comeback = NULL;
11768 	size_t comeback_len = 0;
11769 	union wpa_event_data event;
11770 	unsigned int i;
11771 	struct pasn_peer *peer = NULL;
11772 	int ret = -1;
11773 
11774 	os_memset(&event, 0, sizeof(event));
11775 
11776 	/*
11777 	 * Entry format :
11778 	 *    <PASN_ACTION> <PEER_1_DETAILS> <PEER_2_DETAILS>
11779 	 *    .......... <PEER_N_DETAILS>
11780 	 */
11781 
11782 	if (os_strncmp(cmd, "auth ", 5) == 0)
11783 		event.pasn_auth.action = PASN_ACTION_AUTH;
11784 	else if (os_strncmp(cmd, "del ", 4) == 0)
11785 		event.pasn_auth.action =
11786 			PASN_ACTION_DELETE_SECURE_RANGING_CONTEXT;
11787 	else
11788 		return -1;
11789 
11790 	cmd = os_strchr(cmd, ' ');
11791 	if (!cmd)
11792 		return -1;
11793 
11794 	/*
11795 	 * Entry format for peer details:
11796 	 *    bssid=<BSSID> akmp=<AKMP> cipher=<CIPHER> group=<group>
11797 	 *    nid=<network_id> [comeback=<hexdump>] password=<PASSWORD>
11798 	 */
11799 	while ((token = str_token(cmd, " ", &context))) {
11800 		if (os_strncmp(token, "bssid=", 6) == 0) {
11801 			if (hwaddr_aton(token + 6, bssid))
11802 				goto out;
11803 
11804 			if (event.pasn_auth.num_peers == WPAS_MAX_PASN_PEERS)
11805 				goto out;
11806 			peer = &event.pasn_auth.peer[event.pasn_auth.num_peers];
11807 			os_memcpy(peer->own_addr, wpa_s->own_addr, ETH_ALEN);
11808 			os_memcpy(peer->peer_addr, bssid, ETH_ALEN);
11809 			event.pasn_auth.num_peers++;
11810 			continue;
11811 		}
11812 
11813 		if (!peer) {
11814 			wpa_printf(MSG_INFO,
11815 				   "CTRL: PASN: No peer (bssid) specified before parameter '%s'",
11816 				   token);
11817 			goto out;
11818 		}
11819 
11820 		if (os_strcmp(token, "akmp=PASN") == 0) {
11821 			peer->akmp = WPA_KEY_MGMT_PASN;
11822 #ifdef CONFIG_IEEE80211R
11823 		} else if (os_strcmp(token, "akmp=FT-PSK") == 0) {
11824 			peer->akmp = WPA_KEY_MGMT_FT_PSK;
11825 		} else if (os_strcmp(token, "akmp=FT-EAP-SHA384") == 0) {
11826 			peer->akmp = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
11827 		} else if (os_strcmp(token, "akmp=FT-EAP") == 0) {
11828 			peer->akmp = WPA_KEY_MGMT_FT_IEEE8021X;
11829 #endif /* CONFIG_IEEE80211R */
11830 #ifdef CONFIG_SAE
11831 		} else if (os_strcmp(token, "akmp=SAE") == 0) {
11832 			peer->akmp = WPA_KEY_MGMT_SAE;
11833 			wpa_printf(MSG_ERROR, "PASN: SAE akmp picked");
11834 		} else if (os_strcmp(token, "akmp=SAE-EXT-KEY") == 0) {
11835 			peer->akmp = WPA_KEY_MGMT_SAE_EXT_KEY;
11836 #endif /* CONFIG_SAE */
11837 #ifdef CONFIG_FILS
11838 		} else if (os_strcmp(token, "akmp=FILS-SHA256") == 0) {
11839 			peer->akmp = WPA_KEY_MGMT_FILS_SHA256;
11840 		} else if (os_strcmp(token, "akmp=FILS-SHA384") == 0) {
11841 			peer->akmp = WPA_KEY_MGMT_FILS_SHA384;
11842 #endif /* CONFIG_FILS */
11843 		} else if (os_strncmp(token, "cipher=", 7) == 0) {
11844 			peer->cipher = wpa_parse_cipher(token + 7);
11845 		} else if (os_strncmp(token, "group=", 6) == 0) {
11846 			peer->group = atoi(token + 6);
11847 		} else if (os_strncmp(token, "nid=", 4) == 0) {
11848 			peer->network_id = atoi(token + 4);
11849 		} else if (os_strncmp(token, "comeback=", 9) == 0) {
11850 			comeback_len = os_strlen(token + 9);
11851 			if (comeback || !comeback_len || comeback_len % 2)
11852 				goto out;
11853 
11854 			comeback_len /= 2;
11855 			peer->comeback = os_malloc(comeback_len);
11856 			if (!peer->comeback ||
11857 			    hexstr2bin(token + 9, peer->comeback, comeback_len))
11858 				goto out;
11859 			peer->comeback_len = comeback_len;
11860 		} else if (os_strncmp(token, "password=", 9) == 0) {
11861 			peer->password = os_strdup(token + 9);
11862 			if (!peer->password)
11863 				goto out;
11864 		} else {
11865 			wpa_printf(MSG_DEBUG,
11866 				   "CTRL: PASN Invalid parameter: '%s'",
11867 				   token);
11868 			goto out;
11869 		}
11870 	}
11871 
11872 	wpa_supplicant_event(wpa_s, EVENT_PASN_AUTH, &event);
11873 
11874 	ret = 0;
11875 
11876 out:
11877 	for (i = 0; i < event.pasn_auth.num_peers; i++) {
11878 		peer = &event.pasn_auth.peer[i];
11879 		os_free(peer->password);
11880 		os_free(peer->comeback);
11881 	}
11882 
11883 	return ret;
11884 }
11885 
11886 #endif /* CONFIG_TESTING_OPTIONS */
11887 
11888 #endif /* CONFIG_PASN */
11889 
11890 
11891 #ifndef CONFIG_NO_ROBUST_AV
11892 
set_type4_frame_classifier(const char * cmd,struct type4_params * param)11893 static int set_type4_frame_classifier(const char *cmd,
11894 				      struct type4_params *param)
11895 {
11896 	const char *pos, *end;
11897 	u8 classifier_mask = 0;
11898 	int ret;
11899 	char addr[INET6_ADDRSTRLEN];
11900 	size_t alen;
11901 
11902 	if (os_strstr(cmd, "ip_version=ipv4")) {
11903 		param->ip_version = IPV4;
11904 	} else if (os_strstr(cmd, "ip_version=ipv6")) {
11905 		param->ip_version = IPV6;
11906 	} else {
11907 		wpa_printf(MSG_ERROR, "IP version missing/invalid");
11908 		return -1;
11909 	}
11910 
11911 	classifier_mask |= BIT(0);
11912 
11913 	pos = os_strstr(cmd, "src_ip=");
11914 	if (pos) {
11915 		pos += 7;
11916 		end = os_strchr(pos, ' ');
11917 		if (!end)
11918 			end = pos + os_strlen(pos);
11919 
11920 		alen = end - pos;
11921 		if (alen >= INET6_ADDRSTRLEN)
11922 			return -1;
11923 		os_memcpy(addr, pos, alen);
11924 		addr[alen] = '\0';
11925 		if (param->ip_version == IPV4)
11926 			ret = inet_pton(AF_INET, addr,
11927 					&param->ip_params.v4.src_ip);
11928 		else
11929 			ret = inet_pton(AF_INET6, addr,
11930 					&param->ip_params.v6.src_ip);
11931 
11932 		if (ret != 1) {
11933 			wpa_printf(MSG_ERROR,
11934 				   "Error converting src IP address to binary ret=%d",
11935 				   ret);
11936 			return -1;
11937 		}
11938 
11939 		classifier_mask |= BIT(1);
11940 	}
11941 
11942 	pos = os_strstr(cmd, "dst_ip=");
11943 	if (pos) {
11944 		pos += 7;
11945 		end = os_strchr(pos, ' ');
11946 		if (!end)
11947 			end = pos + os_strlen(pos);
11948 
11949 		alen = end - pos;
11950 		if (alen >= INET6_ADDRSTRLEN)
11951 			return -1;
11952 		os_memcpy(addr, pos, alen);
11953 		addr[alen] = '\0';
11954 		if (param->ip_version == IPV4)
11955 			ret = inet_pton(AF_INET, addr,
11956 					&param->ip_params.v4.dst_ip);
11957 		else
11958 			ret = inet_pton(AF_INET6, addr,
11959 					&param->ip_params.v6.dst_ip);
11960 
11961 		if (ret != 1) {
11962 			wpa_printf(MSG_ERROR,
11963 				   "Error converting dst IP address to binary ret=%d",
11964 				   ret);
11965 			return -1;
11966 		}
11967 
11968 		classifier_mask |= BIT(2);
11969 	}
11970 
11971 	pos = os_strstr(cmd, "src_port=");
11972 	if (pos && atoi(pos + 9) > 0) {
11973 		if (param->ip_version == IPV4)
11974 			param->ip_params.v4.src_port = atoi(pos + 9);
11975 		else
11976 			param->ip_params.v6.src_port = atoi(pos + 9);
11977 		classifier_mask |= BIT(3);
11978 	}
11979 
11980 	pos = os_strstr(cmd, "dst_port=");
11981 	if (pos && atoi(pos + 9) > 0) {
11982 		if (param->ip_version == IPV4)
11983 			param->ip_params.v4.dst_port = atoi(pos + 9);
11984 		else
11985 			param->ip_params.v6.dst_port = atoi(pos + 9);
11986 		classifier_mask |= BIT(4);
11987 	}
11988 
11989 	pos = os_strstr(cmd, "dscp=");
11990 	if (pos && atoi(pos + 5) > 0) {
11991 		if (param->ip_version == IPV4)
11992 			param->ip_params.v4.dscp = atoi(pos + 5);
11993 		else
11994 			param->ip_params.v6.dscp = atoi(pos + 5);
11995 		classifier_mask |= BIT(5);
11996 	}
11997 
11998 	if (param->ip_version == IPV4) {
11999 		pos = os_strstr(cmd, "protocol=");
12000 		if (pos) {
12001 			if (os_strstr(pos, "udp")) {
12002 				param->ip_params.v4.protocol = 17;
12003 			} else if (os_strstr(pos, "tcp")) {
12004 				param->ip_params.v4.protocol = 6;
12005 			} else if (os_strstr(pos, "esp")) {
12006 				param->ip_params.v4.protocol = 50;
12007 			} else {
12008 				wpa_printf(MSG_ERROR, "Invalid protocol");
12009 				return -1;
12010 			}
12011 			classifier_mask |= BIT(6);
12012 		}
12013 	} else {
12014 		pos = os_strstr(cmd, "next_header=");
12015 		if (pos) {
12016 			if (os_strstr(pos, "udp")) {
12017 				param->ip_params.v6.next_header = 17;
12018 			} else if (os_strstr(pos, "tcp")) {
12019 				param->ip_params.v6.next_header = 6;
12020 			} else if (os_strstr(pos, "esp")) {
12021 				param->ip_params.v6.next_header = 50;
12022 			} else {
12023 				wpa_printf(MSG_ERROR, "Invalid next header");
12024 				return -1;
12025 			}
12026 
12027 			classifier_mask |= BIT(6);
12028 		}
12029 
12030 		pos = os_strstr(cmd, "flow_label=");
12031 		if (pos) {
12032 			pos += 11;
12033 			end = os_strchr(pos, ' ');
12034 			if (!end)
12035 				end = pos + os_strlen(pos);
12036 
12037 			if (end - pos != 6 ||
12038 			    hexstr2bin(pos, param->ip_params.v6.flow_label,
12039 				       3) ||
12040 			    param->ip_params.v6.flow_label[0] > 0x0F) {
12041 				wpa_printf(MSG_ERROR, "Invalid flow label");
12042 				return -1;
12043 			}
12044 
12045 			classifier_mask |= BIT(7);
12046 		}
12047 	}
12048 
12049 	param->classifier_mask = classifier_mask;
12050 	return 0;
12051 }
12052 
12053 
set_type10_frame_classifier(const char * cmd,struct type10_params * param)12054 static int set_type10_frame_classifier(const char *cmd,
12055 				       struct type10_params *param)
12056 {
12057 	const char *pos, *end;
12058 	size_t filter_len;
12059 
12060 	pos = os_strstr(cmd, "prot_instance=");
12061 	if (!pos) {
12062 		wpa_printf(MSG_ERROR, "Protocol instance missing");
12063 		return -1;
12064 	}
12065 	param->prot_instance = atoi(pos + 14);
12066 
12067 	pos = os_strstr(cmd, "prot_number=");
12068 	if (!pos) {
12069 		wpa_printf(MSG_ERROR, "Protocol number missing");
12070 		return -1;
12071 	}
12072 	if (os_strstr(pos, "udp")) {
12073 		param->prot_number = 17;
12074 	} else if (os_strstr(pos, "tcp")) {
12075 		param->prot_number = 6;
12076 	} else if (os_strstr(pos, "esp")) {
12077 		param->prot_number = 50;
12078 	} else {
12079 		wpa_printf(MSG_ERROR, "Invalid protocol number");
12080 		return -1;
12081 	}
12082 
12083 	pos = os_strstr(cmd, "filter_value=");
12084 	if (!pos) {
12085 		wpa_printf(MSG_ERROR,
12086 			   "Classifier parameter filter_value missing");
12087 		return -1;
12088 	}
12089 
12090 	pos += 13;
12091 	end = os_strchr(pos, ' ');
12092 	if (!end)
12093 		end = pos + os_strlen(pos);
12094 
12095 	filter_len = (end - pos) / 2;
12096 	param->filter_value = os_malloc(filter_len);
12097 	if (!param->filter_value)
12098 		return -1;
12099 
12100 	if (hexstr2bin(pos, param->filter_value, filter_len)) {
12101 		wpa_printf(MSG_ERROR, "Invalid filter_value %s", pos);
12102 		goto free;
12103 	}
12104 
12105 	pos = os_strstr(cmd, "filter_mask=");
12106 	if (!pos) {
12107 		wpa_printf(MSG_ERROR,
12108 			   "Classifier parameter filter_mask missing");
12109 		goto free;
12110 	}
12111 
12112 	pos += 12;
12113 	end = os_strchr(pos, ' ');
12114 	if (!end)
12115 		end = pos + os_strlen(pos);
12116 
12117 	if (filter_len != (size_t) (end - pos) / 2) {
12118 		wpa_printf(MSG_ERROR,
12119 			   "Filter mask length mismatch expected=%zu received=%zu",
12120 			   filter_len, (size_t) (end - pos) / 2);
12121 		goto free;
12122 	}
12123 
12124 	param->filter_mask = os_malloc(filter_len);
12125 	if (!param->filter_mask)
12126 		goto free;
12127 
12128 	if (hexstr2bin(pos, param->filter_mask, filter_len)) {
12129 		wpa_printf(MSG_ERROR, "Invalid filter mask %s", pos);
12130 		os_free(param->filter_mask);
12131 		param->filter_mask = NULL;
12132 		goto free;
12133 	}
12134 
12135 	param->filter_len = filter_len;
12136 	return 0;
12137 free:
12138 	os_free(param->filter_value);
12139 	param->filter_value = NULL;
12140 	return -1;
12141 }
12142 
12143 
scs_parse_type4(struct tclas_element * elem,const char * pos)12144 static int scs_parse_type4(struct tclas_element *elem, const char *pos)
12145 {
12146 	struct type4_params type4_param = { 0 };
12147 
12148 	if (set_type4_frame_classifier(pos, &type4_param) == -1) {
12149 		wpa_printf(MSG_ERROR, "Failed to set frame_classifier 4");
12150 		return -1;
12151 	}
12152 
12153 	os_memcpy(&elem->frame_classifier.type4_param,
12154 		  &type4_param, sizeof(struct type4_params));
12155 	return 0;
12156 }
12157 
12158 
scs_parse_type10(struct tclas_element * elem,const char * pos)12159 static int scs_parse_type10(struct tclas_element *elem, const char *pos)
12160 {
12161 	struct type10_params type10_param = { 0 };
12162 
12163 	if (set_type10_frame_classifier(pos, &type10_param) == -1) {
12164 		wpa_printf(MSG_ERROR, "Failed to set frame_classifier 10");
12165 		return -1;
12166 	}
12167 
12168 	os_memcpy(&elem->frame_classifier.type10_param,
12169 		  &type10_param, sizeof(struct type10_params));
12170 	return 0;
12171 }
12172 
12173 
wpas_ctrl_iface_configure_scs(struct wpa_supplicant * wpa_s,char * cmd)12174 static int wpas_ctrl_iface_configure_scs(struct wpa_supplicant *wpa_s,
12175 					 char *cmd)
12176 {
12177 	char *pos1, *pos;
12178 	struct scs_robust_av_data *scs_data = &wpa_s->scs_robust_av_req;
12179 	struct scs_desc_elem desc_elem = { 0 };
12180 	int val;
12181 	unsigned int num_scs_desc = 0;
12182 
12183 	if (wpa_s->ongoing_scs_req) {
12184 		wpa_printf(MSG_ERROR, "%s: SCS Request already in queue",
12185 			   __func__);
12186 		return -1;
12187 	}
12188 
12189 	/**
12190 	 * format:
12191 	 * [scs_id=<decimal number>] <add|remove|change> [scs_up=<0-7>]
12192 	 * [classifier_type=<4|10>]
12193 	 * [classifier params based on classifier type]
12194 	 * [tclas_processing=<0|1>]
12195 	 * [qos_characteristics] <up/down/direct> [min_si=<decimal number>]
12196 	 * [max_si=<decimal number>] [min_data_rate=<decimal number>]
12197 	 * [delay_bound=<decimal number>] [max_msdu=<decimal number>]
12198 	 * [service_start_time=<decimal number>]
12199 	 * [service_start_time_link_id=<decimal number>]
12200 	 * [mean_data_rate=<decimal number>] [burst_size=<decimal number>]
12201 	 * [msdu_lifetime=<decimal number>]
12202 	 * [msdu_delivery_info=<decimal number>] [medium_time=<decimal number>]
12203 	 * [scs_id=<decimal number>] ...
12204 	 */
12205 	pos1 = os_strstr(cmd, "scs_id=");
12206 	if (!pos1) {
12207 		wpa_printf(MSG_ERROR, "SCSID not present");
12208 		return -1;
12209 	}
12210 
12211 	free_up_scs_desc(scs_data);
12212 
12213 	while (pos1) {
12214 		struct scs_desc_elem *n1;
12215 		struct active_scs_elem *active_scs_desc;
12216 		char *next_scs_desc, *pos2;
12217 		unsigned int num_tclas_elem = 0;
12218 		bool scsid_active = false, tclas_present = false;
12219 		struct qos_characteristics *qos_elem = &desc_elem.qos_char_elem;
12220 
12221 		desc_elem.scs_id = atoi(pos1 + 7);
12222 		pos1 += 7;
12223 
12224 		next_scs_desc = os_strstr(pos1, "scs_id=");
12225 		if (next_scs_desc) {
12226 			char temp[20];
12227 
12228 			os_snprintf(temp, sizeof(temp), "scs_id=%d ",
12229 				    desc_elem.scs_id);
12230 			if (os_strstr(next_scs_desc, temp)) {
12231 				wpa_printf(MSG_ERROR,
12232 					   "Multiple SCS descriptors configured with same SCSID(=%d)",
12233 					   desc_elem.scs_id);
12234 				goto free_scs_desc;
12235 			}
12236 			pos1[next_scs_desc - pos1 - 1] = '\0';
12237 		}
12238 
12239 		dl_list_for_each(active_scs_desc, &wpa_s->active_scs_ids,
12240 				 struct active_scs_elem, list) {
12241 			if (desc_elem.scs_id == active_scs_desc->scs_id) {
12242 				scsid_active = true;
12243 				break;
12244 			}
12245 		}
12246 
12247 		if (os_strstr(pos1, "add ")) {
12248 			desc_elem.request_type = SCS_REQ_ADD;
12249 			if (scsid_active) {
12250 				wpa_printf(MSG_ERROR, "SCSID %d already active",
12251 					   desc_elem.scs_id);
12252 				return -1;
12253 			}
12254 		} else if (os_strstr(pos1, "remove")) {
12255 			desc_elem.request_type = SCS_REQ_REMOVE;
12256 			if (!scsid_active) {
12257 				wpa_printf(MSG_ERROR, "SCSID %d not active",
12258 					   desc_elem.scs_id);
12259 				return -1;
12260 			}
12261 			goto scs_desc_end;
12262 		} else if (os_strstr(pos1, "change ")) {
12263 			desc_elem.request_type = SCS_REQ_CHANGE;
12264 			if (!scsid_active) {
12265 				wpa_printf(MSG_ERROR, "SCSID %d not active",
12266 					   desc_elem.scs_id);
12267 				return -1;
12268 			}
12269 		} else {
12270 			wpa_printf(MSG_ERROR, "SCS Request type invalid");
12271 			goto free_scs_desc;
12272 		}
12273 
12274 		pos1 = os_strstr(pos1, "scs_up=");
12275 		if (!pos1) {
12276 			wpa_printf(MSG_ERROR,
12277 				   "Intra-Access user priority not present");
12278 			goto free_scs_desc;
12279 		}
12280 
12281 		val = atoi(pos1 + 7);
12282 		if (val < 0 || val > 7) {
12283 			wpa_printf(MSG_ERROR,
12284 				   "Intra-Access user priority invalid %d",
12285 				   val);
12286 			goto free_scs_desc;
12287 		}
12288 
12289 		desc_elem.intra_access_priority = val;
12290 		desc_elem.scs_up_avail = true;
12291 
12292 		pos = os_strstr(pos1, "classifier_type=");
12293 		if (!pos) {
12294 			wpa_printf(MSG_ERROR, "classifier type empty");
12295 			goto qos_characteristics;
12296 		}
12297 		tclas_present = true;
12298 
12299 		while (pos) {
12300 			struct tclas_element elem = { 0 }, *n;
12301 			char *next_tclas_elem;
12302 
12303 			val = atoi(pos + 16);
12304 			if (val != 4 && val != 10) {
12305 				wpa_printf(MSG_ERROR,
12306 					   "classifier type invalid %d", val);
12307 				goto free_scs_desc;
12308 			}
12309 
12310 			elem.classifier_type = val;
12311 			pos += 16;
12312 
12313 			next_tclas_elem = os_strstr(pos, "classifier_type=");
12314 			if (next_tclas_elem) {
12315 				pos1 = next_tclas_elem;
12316 				pos[next_tclas_elem - pos - 1] = '\0';
12317 			}
12318 
12319 			switch (val) {
12320 			case 4:
12321 				if (scs_parse_type4(&elem, pos) < 0)
12322 					goto free_scs_desc;
12323 				break;
12324 			case 10:
12325 				if (scs_parse_type10(&elem, pos) < 0)
12326 					goto free_scs_desc;
12327 				break;
12328 			}
12329 
12330 			n = os_realloc(desc_elem.tclas_elems,
12331 				       (num_tclas_elem + 1) * sizeof(elem));
12332 			if (!n)
12333 				goto free_scs_desc;
12334 
12335 			desc_elem.tclas_elems = n;
12336 			os_memcpy((u8 *) desc_elem.tclas_elems +
12337 				  num_tclas_elem * sizeof(elem),
12338 				  &elem, sizeof(elem));
12339 			num_tclas_elem++;
12340 			desc_elem.num_tclas_elem = num_tclas_elem;
12341 			pos = next_tclas_elem;
12342 		}
12343 
12344 		if (desc_elem.num_tclas_elem > 1) {
12345 			pos1 = os_strstr(pos1, "tclas_processing=");
12346 			if (!pos1) {
12347 				wpa_printf(MSG_ERROR, "tclas_processing empty");
12348 				goto free_scs_desc;
12349 			}
12350 
12351 			val = atoi(pos1 + 17);
12352 			if (val != 0 && val != 1) {
12353 				wpa_printf(MSG_ERROR,
12354 					   "tclas_processing invalid");
12355 				goto free_scs_desc;
12356 			}
12357 
12358 			desc_elem.tclas_processing = val;
12359 		}
12360 
12361 	qos_characteristics:
12362 		pos1 = os_strstr(pos1, "qos_characteristics");
12363 		if (!pos1 && !tclas_present)
12364 			goto free_scs_desc;
12365 		if (!pos1)
12366 			goto scs_desc_end;
12367 
12368 		qos_elem->available = true;
12369 		if (os_strstr(pos1, "up ")) {
12370 			qos_elem->direction = SCS_DIRECTION_UP;
12371 			if (tclas_present) {
12372 				wpa_printf(MSG_ERROR,
12373 					   "TCLAS with direction:UP not allowed");
12374 				goto free_scs_desc;
12375 			}
12376 		} else if (os_strstr(pos1, "down ")) {
12377 			qos_elem->direction = SCS_DIRECTION_DOWN;
12378 		} else if (os_strstr(pos1, "direct ")) {
12379 			qos_elem->direction = SCS_DIRECTION_DIRECT;
12380 		}
12381 
12382 		pos1 = os_strstr(pos1, "min_si=");
12383 		if (!pos1) {
12384 			wpa_printf(MSG_ERROR, "Min SI is required");
12385 			goto free_scs_desc;
12386 		}
12387 		qos_elem->min_si = atoi(pos1 + 7);
12388 
12389 		pos1 = os_strstr(pos1, "max_si=");
12390 		if (!pos1) {
12391 			wpa_printf(MSG_ERROR, "Max SI is required");
12392 			goto free_scs_desc;
12393 		}
12394 		qos_elem->max_si = atoi(pos1 + 7);
12395 
12396 		if (qos_elem->min_si && qos_elem->max_si &&
12397 		    qos_elem->max_si < qos_elem->min_si) {
12398 			wpa_printf(MSG_ERROR, "Invalid Max SI");
12399 			goto free_scs_desc;
12400 		}
12401 
12402 		pos1 = os_strstr(pos1, "min_data_rate=");
12403 		if (!pos1) {
12404 			wpa_printf(MSG_ERROR, "Min data rate is required");
12405 			goto free_scs_desc;
12406 		}
12407 		qos_elem->min_data_rate = atoi(pos1 + 14);
12408 
12409 		pos1 = os_strstr(pos1, "delay_bound=");
12410 		if (!pos1) {
12411 			wpa_printf(MSG_ERROR, "Delay Bound is required");
12412 			goto free_scs_desc;
12413 		}
12414 		qos_elem->delay_bound = atoi(pos1 + 12);
12415 
12416 		if (qos_elem->min_data_rate >= BIT(24) ||
12417 		    qos_elem->delay_bound >= BIT(24)) {
12418 			wpa_printf(MSG_ERROR,
12419 				   "Invalid min_data_rate or delay_bound");
12420 			goto free_scs_desc;
12421 		}
12422 
12423 		pos2 = os_strstr(pos1, "max_msdu=");
12424 		if (pos2) {
12425 			qos_elem->max_msdu_size = atoi(pos2 + 9);
12426 			qos_elem->mask |= SCS_QOS_BIT_MAX_MSDU_SIZE;
12427 		}
12428 
12429 		pos2 = os_strstr(pos1, "service_start_time=");
12430 		if (pos2) {
12431 			qos_elem->service_start_time = atoi(pos2 + 19);
12432 			qos_elem->mask |= SCS_QOS_BIT_SERVICE_START_TIME;
12433 		}
12434 
12435 		pos2 = os_strstr(pos1, "service_start_time_link_id=");
12436 		if (pos2) {
12437 			qos_elem->service_start_time_link_id = atoi(pos2 + 27);
12438 			qos_elem->mask |= SCS_QOS_BIT_SERVICE_START_TIME_LINKID;
12439 		}
12440 
12441 		pos2 = os_strstr(pos1, "mean_data_rate=");
12442 		if (pos2) {
12443 			qos_elem->mean_data_rate = atoi(pos2 + 15);
12444 			qos_elem->mask |= SCS_QOS_BIT_MEAN_DATA_RATE;
12445 		}
12446 
12447 		pos2 = os_strstr(pos1, "burst_size=");
12448 		if (pos2) {
12449 			qos_elem->burst_size = atoi(pos2 + 11);
12450 			qos_elem->mask |=
12451 				SCS_QOS_BIT_DELAYED_BOUNDED_BURST_SIZE;
12452 		}
12453 
12454 		pos2 = os_strstr(pos1, "msdu_lifetime=");
12455 		if (pos2) {
12456 			qos_elem->msdu_lifetime = atoi(pos2 + 14);
12457 			qos_elem->mask |= SCS_QOS_BIT_MSDU_LIFETIME;
12458 		}
12459 
12460 		pos2 = os_strstr(pos1, "msdu_delivery_info=");
12461 		if (pos2) {
12462 			qos_elem->msdu_delivery_info = atoi(pos2 + 19);
12463 			qos_elem->mask |= SCS_QOS_BIT_MSDU_DELIVERY_INFO;
12464 		}
12465 
12466 		pos2 = os_strstr(pos1, "medium_time=");
12467 		if (pos2) {
12468 			qos_elem->medium_time = atoi(pos2 + 12);
12469 			qos_elem->mask |= SCS_QOS_BIT_MEDIUM_TIME;
12470 		}
12471 
12472 scs_desc_end:
12473 		n1 = os_realloc(scs_data->scs_desc_elems, (num_scs_desc + 1) *
12474 				sizeof(struct scs_desc_elem));
12475 		if (!n1)
12476 			goto free_scs_desc;
12477 
12478 		scs_data->scs_desc_elems = n1;
12479 		os_memcpy((u8 *) scs_data->scs_desc_elems + num_scs_desc *
12480 			  sizeof(desc_elem), &desc_elem, sizeof(desc_elem));
12481 		num_scs_desc++;
12482 		scs_data->num_scs_desc = num_scs_desc;
12483 		pos1 = next_scs_desc;
12484 		os_memset(&desc_elem, 0, sizeof(desc_elem));
12485 	}
12486 
12487 	return wpas_send_scs_req(wpa_s);
12488 
12489 free_scs_desc:
12490 	free_up_tclas_elem(&desc_elem);
12491 	free_up_scs_desc(scs_data);
12492 	return -1;
12493 }
12494 
12495 
wpas_ctrl_iface_send_dscp_resp(struct wpa_supplicant * wpa_s,const char * cmd)12496 static int wpas_ctrl_iface_send_dscp_resp(struct wpa_supplicant *wpa_s,
12497 					  const char *cmd)
12498 {
12499 	char *pos;
12500 	struct dscp_policy_status *policy = NULL, *n;
12501 	int num_policies = 0, ret = -1;
12502 	struct dscp_resp_data resp_data;
12503 
12504 	/*
12505 	 * format:
12506 	 * <[reset]>/<[solicited] [policy_id=1 status=0...]> [more]
12507 	 */
12508 
12509 	os_memset(&resp_data, 0, sizeof(resp_data));
12510 
12511 	resp_data.more = os_strstr(cmd, "more") != NULL;
12512 
12513 	if (os_strstr(cmd, "reset")) {
12514 		resp_data.reset = true;
12515 		resp_data.solicited = false;
12516 		goto send_resp;
12517 	}
12518 
12519 	resp_data.solicited = os_strstr(cmd, "solicited") != NULL;
12520 
12521 	pos = os_strstr(cmd, "policy_id=");
12522 	while (pos) {
12523 		n = os_realloc(policy, (num_policies + 1) * sizeof(*policy));
12524 		if (!n)
12525 			goto fail;
12526 
12527 		policy = n;
12528 		pos += 10;
12529 		policy[num_policies].id = atoi(pos);
12530 		if (policy[num_policies].id == 0) {
12531 			wpa_printf(MSG_ERROR, "DSCP: Invalid policy id");
12532 			goto fail;
12533 		}
12534 
12535 		pos = os_strstr(pos, "status=");
12536 		if (!pos) {
12537 			wpa_printf(MSG_ERROR,
12538 				   "DSCP: Status is not found for a policy");
12539 			goto fail;
12540 		}
12541 
12542 		pos += 7;
12543 		policy[num_policies].status = atoi(pos);
12544 		num_policies++;
12545 
12546 		pos = os_strstr(pos, "policy_id");
12547 	}
12548 
12549 	resp_data.policy = policy;
12550 	resp_data.num_policies = num_policies;
12551 send_resp:
12552 	ret = wpas_send_dscp_response(wpa_s, &resp_data);
12553 	if (ret)
12554 		wpa_printf(MSG_ERROR, "DSCP: Failed to send DSCP response");
12555 fail:
12556 	os_free(policy);
12557 	return ret;
12558 }
12559 
12560 
wpas_ctrl_iface_send_dscp_query(struct wpa_supplicant * wpa_s,const char * cmd)12561 static int wpas_ctrl_iface_send_dscp_query(struct wpa_supplicant *wpa_s,
12562 					   const char *cmd)
12563 {
12564 	char *pos;
12565 
12566 	/*
12567 	 * format:
12568 	 * Wildcard DSCP query
12569 	 * <wildcard>
12570 	 *
12571 	 * DSCP query with a domain name attribute:
12572 	 * [domain_name=<string>]
12573 	 */
12574 
12575 	if (os_strstr(cmd, "wildcard")) {
12576 		wpa_printf(MSG_DEBUG, "QM: Send wildcard DSCP policy query");
12577 		return wpas_send_dscp_query(wpa_s, NULL, 0);
12578 	}
12579 
12580 	pos = os_strstr(cmd, "domain_name=");
12581 	if (!pos || !os_strlen(pos + 12)) {
12582 		wpa_printf(MSG_ERROR, "QM: Domain name not preset");
12583 		return -1;
12584 	}
12585 
12586 	return wpas_send_dscp_query(wpa_s, pos + 12, os_strlen(pos + 12));
12587 }
12588 
12589 #endif /* CONFIG_NO_ROBUST_AV */
12590 
12591 
wpas_ctrl_iface_mlo_signal_poll(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)12592 static int wpas_ctrl_iface_mlo_signal_poll(struct wpa_supplicant *wpa_s,
12593 					   char *buf, size_t buflen)
12594 {
12595 	int ret, i;
12596 	char *pos, *end;
12597 	struct wpa_mlo_signal_info mlo_si;
12598 
12599 	if (!wpa_s->valid_links)
12600 		return -1;
12601 
12602 	ret = wpa_drv_mlo_signal_poll(wpa_s, &mlo_si);
12603 	if (ret)
12604 		return -1;
12605 
12606 	pos = buf;
12607 	end = buf + buflen;
12608 
12609 	for_each_link(mlo_si.valid_links, i) {
12610 		ret = os_snprintf(pos, end - pos,
12611 				  "LINK_ID=%d\nRSSI=%d\nLINKSPEED=%lu\n"
12612 				  "NOISE=%d\nFREQUENCY=%u\n",
12613 				  i, mlo_si.links[i].data.signal,
12614 				  mlo_si.links[i].data.current_tx_rate / 1000,
12615 				  mlo_si.links[i].current_noise,
12616 				  mlo_si.links[i].frequency);
12617 		if (os_snprintf_error(end - pos, ret))
12618 			return -1;
12619 		pos += ret;
12620 
12621 		if (mlo_si.links[i].chanwidth != CHAN_WIDTH_UNKNOWN) {
12622 			ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
12623 					  channel_width_to_string(
12624 						  mlo_si.links[i].chanwidth));
12625 			if (os_snprintf_error(end - pos, ret))
12626 				return -1;
12627 			pos += ret;
12628 		}
12629 
12630 		if (mlo_si.links[i].center_frq1 > 0) {
12631 			ret = os_snprintf(pos, end - pos, "CENTER_FRQ1=%d\n",
12632 					  mlo_si.links[i].center_frq1);
12633 			if (os_snprintf_error(end - pos, ret))
12634 				return -1;
12635 			pos += ret;
12636 		}
12637 
12638 		if (mlo_si.links[i].center_frq2 > 0) {
12639 			ret = os_snprintf(pos, end - pos, "CENTER_FRQ2=%d\n",
12640 					  mlo_si.links[i].center_frq2);
12641 			if (os_snprintf_error(end - pos, ret))
12642 				return -1;
12643 			pos += ret;
12644 		}
12645 
12646 		if (mlo_si.links[i].data.avg_signal) {
12647 			ret = os_snprintf(pos, end - pos,
12648 					  "AVG_RSSI=%d\n",
12649 					  mlo_si.links[i].data.avg_signal);
12650 			if (os_snprintf_error(end - pos, ret))
12651 				return -1;
12652 			pos += ret;
12653 		}
12654 
12655 		if (mlo_si.links[i].data.avg_beacon_signal) {
12656 			ret = os_snprintf(
12657 				pos, end - pos, "AVG_BEACON_RSSI=%d\n",
12658 				mlo_si.links[i].data.avg_beacon_signal);
12659 			if (os_snprintf_error(end - pos, ret))
12660 				return -1;
12661 			pos += ret;
12662 		}
12663 	}
12664 
12665 	return pos - buf;
12666 }
12667 
12668 
12669 static int
wpa_supplicant_ctrl_iface_setup_link_reconfig(struct wpa_supplicant * wpa_s,const char * cmd)12670 wpa_supplicant_ctrl_iface_setup_link_reconfig(struct wpa_supplicant *wpa_s,
12671 					      const char *cmd)
12672 {
12673 	const char *pos, *add_pos, *dlt_pos;
12674 	struct wpa_mlo_reconfig_info info;
12675 	int link_id;
12676 
12677 	add_pos = os_strstr(cmd, "add=");
12678 	dlt_pos = os_strstr(cmd, "delete=");
12679 
12680 	if (!add_pos && !dlt_pos) {
12681 		wpa_printf(MSG_INFO, "No add or delete links info");
12682 		return -1;
12683 	}
12684 
12685 	if (!wpa_s->current_bss) {
12686 		wpa_printf(MSG_INFO, "%s: Not connected", __func__);
12687 		return -1;
12688 	}
12689 
12690 	info.add_links = 0;
12691 	info.delete_links = 0;
12692 
12693 	if (add_pos) {
12694 		pos = add_pos + 4;
12695 
12696 		do {
12697 			link_id = atoi(pos);
12698 			if (link_id < 0 || link_id >=  MAX_NUM_MLD_LINKS)
12699 				return -1;
12700 
12701 			if (wpa_s->current_bss->valid_links & BIT(link_id)) {
12702 				info.add_links |= BIT(link_id);
12703 				os_memcpy(info.add_link_bssid[link_id],
12704 					  wpa_s->current_bss->mld_links[link_id].bssid,
12705 					  ETH_ALEN);
12706 				info.add_link_freq[link_id] =
12707 					wpa_s->current_bss->mld_links[link_id].freq;
12708 			} else {
12709 				wpa_printf(MSG_INFO,
12710 					   "%s: add link info not present",
12711 					   __func__);
12712 				return -1;
12713 			}
12714 
12715 			pos = os_strchr(pos, ' ');
12716 			if (pos)
12717 				pos++;
12718 		} while (pos && pos != dlt_pos);
12719 	}
12720 
12721 	if (dlt_pos) {
12722 		pos = dlt_pos + 7;
12723 
12724 		do {
12725 			link_id = atoi(pos);
12726 			if (link_id < 0 || link_id >=  MAX_NUM_MLD_LINKS)
12727 				return -1;
12728 
12729 			if (wpa_s->valid_links & BIT(link_id))
12730 				info.delete_links |= BIT(link_id);
12731 			else {
12732 				wpa_printf(MSG_INFO,
12733 					   "%s: not a valid delete link",
12734 					   __func__);
12735 				return -1;
12736 			}
12737 
12738 			pos = os_strchr(pos, ' ');
12739 			if (pos)
12740 				pos++;
12741 		} while (pos && pos != add_pos);
12742 	}
12743 
12744 	return wpa_drv_setup_link_reconfig(wpa_s, &info);
12745 }
12746 
12747 
wpas_ctrl_iface_mlo_status(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)12748 static int wpas_ctrl_iface_mlo_status(struct wpa_supplicant *wpa_s,
12749 				      char *buf, size_t buflen)
12750 {
12751 	int ret, i;
12752 	char *pos, *end;
12753 
12754 	if (!wpa_s->valid_links)
12755 		return -1;
12756 
12757 	pos = buf;
12758 	end = buf + buflen;
12759 
12760 	for_each_link(wpa_s->valid_links, i) {
12761 		ret = os_snprintf(pos, end - pos, "link_id=%d\nfreq=%u\n"
12762 				  "ap_link_addr=" MACSTR
12763 				  "\nsta_link_addr=" MACSTR "\n",
12764 				  i, wpa_s->links[i].freq,
12765 				  MAC2STR(wpa_s->links[i].bssid),
12766 				  MAC2STR(wpa_s->links[i].addr));
12767 		if (os_snprintf_error(end - pos, ret))
12768 			return pos - buf;
12769 		pos += ret;
12770 
12771 		if (wpa_s->connection_set) {
12772 			ret = os_snprintf(
12773 				pos, end - pos,
12774 				"max_nss_rx=%u\n"
12775 				"max_nss_tx=%u\n"
12776 				"channel_width=%u\n",
12777 				wpa_s->links[i].max_nss_rx,
12778 				wpa_s->links[i].max_nss_tx,
12779 				channel_width_to_int(
12780 					wpa_s->links[i].channel_bandwidth));
12781 			if (os_snprintf_error(end - pos, ret))
12782 				return pos - buf;
12783 			pos += ret;
12784 		}
12785 	}
12786 
12787 	return pos - buf;
12788 }
12789 
12790 
12791 #ifdef CONFIG_TESTING_OPTIONS
wpas_ctrl_ml_probe(struct wpa_supplicant * wpa_s,char * cmd)12792 static int wpas_ctrl_ml_probe(struct wpa_supplicant *wpa_s, char *cmd)
12793 {
12794 	char *token, *context = NULL;
12795 	u8 bssid[ETH_ALEN];
12796 	int mld_id = -1, link_id = -1;
12797 	struct wpa_bss *bss;
12798 	int *freqs;
12799 
12800 	os_memset(bssid, 0, sizeof(bssid));
12801 
12802 	while ((token = str_token(cmd, " ", &context))) {
12803 		if (os_strncmp(token, "bssid=", 6) == 0) {
12804 			if (hwaddr_aton(token + 6, bssid))
12805 				return -1;
12806 		} else if (os_strncmp(token, "mld_id=", 7) == 0) {
12807 			mld_id = atoi(token + 7);
12808 		} else if (os_strncmp(token, "link_id=", 8) == 0) {
12809 			link_id = atoi(token + 8);
12810 		}
12811 	}
12812 
12813 	if (is_zero_ether_addr(bssid)) {
12814 		wpa_printf(MSG_DEBUG,
12815 			   "MLD: Failed parsing ML probe request arguments");
12816 		return -1;
12817 	}
12818 
12819 	bss = wpa_bss_get_bssid(wpa_s, bssid);
12820 	if (!bss) {
12821 		wpa_printf(MSG_DEBUG,
12822 			   "MLD: Unknown BSS for " MACSTR, MAC2STR(bssid));
12823 		return -1;
12824 	}
12825 
12826 	if (wpa_s->sched_scanning || wpa_s->scanning ||
12827 	    (wpa_s->wpa_state > WPA_SCANNING &&
12828 	     wpa_s->wpa_state != WPA_COMPLETED)) {
12829 		wpa_printf(MSG_DEBUG,
12830 			   "MLO: Ongoing scan: Reject ML probe request");
12831 		return -1;
12832 	}
12833 
12834 	freqs = os_malloc(sizeof(int) * 2);
12835 	if (!freqs)
12836 		return -1;
12837 
12838 	freqs[0] = bss->freq;
12839 	freqs[1] = 0;
12840 
12841 	wpa_s->manual_scan_passive = 0;
12842 	wpa_s->manual_scan_use_id = 0;
12843 	wpa_s->manual_scan_only_new = 0;
12844 	wpa_s->scan_id_count = 0;
12845 	wpa_s->scan_res_handler = scan_only_handler;
12846 	os_free(wpa_s->manual_scan_freqs);
12847 	wpa_s->manual_scan_freqs = freqs;
12848 
12849 	os_memcpy(wpa_s->ml_probe_bssid, bssid, ETH_ALEN);
12850 	wpa_s->ml_probe_mld_id = mld_id;
12851 	if (link_id >= 0)
12852 		wpa_s->ml_probe_links = BIT(link_id);
12853 
12854 	wpa_s->normal_scans = 0;
12855 	wpa_s->scan_req = MANUAL_SCAN_REQ;
12856 	wpa_s->after_wps = 0;
12857 	wpa_s->known_wps_freq = 0;
12858 	wpa_supplicant_req_scan(wpa_s, 0, 0);
12859 
12860 	return 0;
12861 }
12862 #endif /* CONFIG_TESTING_OPTIONS */
12863 
12864 
12865 #if defined(CONFIG_NAN) || defined(CONFIG_NAN_USD)
12866 
wpas_nan_gtk_cs_supported(const int * cipher_list)12867 static bool wpas_nan_gtk_cs_supported(const int *cipher_list)
12868 {
12869 	unsigned int i;
12870 
12871 	for (i = 0; cipher_list && cipher_list[i]; i++) {
12872 		if (cipher_list[i] == NAN_CS_GTK_CCMP_128 ||
12873 		    cipher_list[i] == NAN_CS_GTK_GCMP_256)
12874 			return true;
12875 	}
12876 
12877 	return false;
12878 }
12879 
12880 
wpas_ctrl_nan_publish(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)12881 static int wpas_ctrl_nan_publish(struct wpa_supplicant *wpa_s, char *cmd,
12882 				 char *buf, size_t buflen)
12883 {
12884 	char *token, *context = NULL;
12885 	int publish_id;
12886 	struct nan_publish_params params;
12887 	const char *service_name = NULL;
12888 	struct wpabuf *ssi = NULL;
12889 	int ret = -1;
12890 	enum nan_service_protocol_type srv_proto_type = 0;
12891 	int *freq_list = NULL;
12892 	int *cipher_list = NULL;
12893 	u8 nd_pmk[PMK_LEN];
12894 	bool p2p = false;
12895 	u8 forced_addr[ETH_ALEN];
12896 	bool security_required_set = false;
12897 
12898 	os_memset(&params, 0, sizeof(params));
12899 	/* USD shall use both solicited and unsolicited transmissions */
12900 	params.unsolicited = true;
12901 	params.solicited = true;
12902 	/* USD shall require FSD without GAS */
12903 	params.fsd = true;
12904 	params.freq = NAN_USD_DEFAULT_FREQ;
12905 
12906 	while ((token = str_token(cmd, " ", &context))) {
12907 		if (os_strncmp(token, "service_name=", 13) == 0) {
12908 			service_name = token + 13;
12909 			continue;
12910 		}
12911 
12912 		if (os_strncmp(token, "ttl=", 4) == 0) {
12913 			params.ttl = atoi(token + 4);
12914 			continue;
12915 		}
12916 
12917 		if (os_strncmp(token, "freq=", 5) == 0) {
12918 			params.freq = atoi(token + 5);
12919 			continue;
12920 		}
12921 
12922 		if (os_strncmp(token, "freq_list=", 10) == 0) {
12923 			char *pos = token + 10;
12924 
12925 			if (os_strcmp(pos, "all") == 0) {
12926 				os_free(freq_list);
12927 				freq_list = wpas_nan_usd_all_freqs(wpa_s);
12928 				params.freq_list = freq_list;
12929 				continue;
12930 			}
12931 
12932 			while (pos && pos[0]) {
12933 				int_array_add_unique(&freq_list, atoi(pos));
12934 				pos = os_strchr(pos, ',');
12935 				if (pos)
12936 					pos++;
12937 			}
12938 
12939 			params.freq_list = freq_list;
12940 			continue;
12941 		}
12942 
12943 		if (os_strncmp(token, "srv_proto_type=", 15) == 0) {
12944 			srv_proto_type = atoi(token + 15);
12945 			continue;
12946 		}
12947 
12948 		if (os_strncmp(token, "ssi=", 4) == 0) {
12949 			if (ssi)
12950 				goto fail;
12951 			ssi = wpabuf_parse_bin(token + 4);
12952 			if (!ssi)
12953 				goto fail;
12954 			continue;
12955 		}
12956 
12957 		if (os_strcmp(token, "p2p=1") == 0) {
12958 			p2p = true;
12959 			continue;
12960 		}
12961 
12962 		if (os_strcmp(token, "solicited=0") == 0) {
12963 			params.solicited = false;
12964 			continue;
12965 		}
12966 
12967 		if (os_strcmp(token, "unsolicited=0") == 0) {
12968 			params.unsolicited = false;
12969 			continue;
12970 		}
12971 
12972 		if (os_strcmp(token, "fsd=0") == 0) {
12973 			params.fsd = false;
12974 			continue;
12975 		}
12976 
12977 		if (os_strcmp(token, "pr=1") == 0) {
12978 			params.proximity_ranging = true;
12979 			continue;
12980 		}
12981 
12982 		if (os_strcmp(token, "sync=1") == 0) {
12983 			params.sync = true;
12984 			continue;
12985 		}
12986 
12987 		if (os_strncmp(token, "match_filter_tx=", 16) == 0) {
12988 			params.match_filter_tx = token + 16;
12989 			continue;
12990 		}
12991 
12992 		if (os_strncmp(token, "match_filter_rx=", 16) == 0) {
12993 			params.match_filter_rx = token + 16;
12994 			continue;
12995 		}
12996 
12997 		if (os_strcmp(token, "close_proximity=1") == 0) {
12998 			params.close_proximity = true;
12999 			continue;
13000 		}
13001 
13002 		if (os_strncmp(token, "pbm=", 4) == 0) {
13003 			params.pbm = strtol(token + 4, NULL, 0);
13004 			continue;
13005 		}
13006 
13007 		if (os_strncmp(token, "cipher_suites=", 14) == 0) {
13008 			char *pos = token + 14;
13009 
13010 			while (pos && pos[0]) {
13011 				int_array_add_unique(&cipher_list, atoi(pos));
13012 				pos = os_strchr(pos, ',');
13013 				if (pos)
13014 					pos++;
13015 			}
13016 
13017 			params.cipher_suites_list = cipher_list;
13018 			continue;
13019 		}
13020 
13021 		if (os_strncmp(token, "nd_pmk=", 7) == 0) {
13022 			if (params.nd_pmk) {
13023 				wpa_printf(MSG_INFO,
13024 					   "CTRL: Duplicate nd_pmk parameter");
13025 				goto fail;
13026 			}
13027 
13028 			if (hexstr2bin(token + 7, nd_pmk, PMK_LEN) < 0) {
13029 				wpa_printf(MSG_INFO,
13030 					   "CTRL: Invalid nd_pmk value");
13031 				goto fail;
13032 			}
13033 
13034 			params.nd_pmk = nd_pmk;
13035 			continue;
13036 		}
13037 
13038 		if (os_strcmp(token, "gtk_required=1") == 0) {
13039 			params.gtk_required = true;
13040 			continue;
13041 		}
13042 
13043 		if (os_strcmp(token, "data_path=1") == 0) {
13044 			params.data_path = true;
13045 			continue;
13046 		}
13047 
13048 		if (os_strncmp(token, "forced_addr=", 12) == 0) {
13049 			if (hwaddr_aton(token + 12, forced_addr)) {
13050 				wpa_printf(MSG_INFO,
13051 					   "CTRL: Invalid forced_addr");
13052 				goto fail;
13053 			}
13054 			params.forced_addr = forced_addr;
13055 			continue;
13056 		}
13057 
13058 		if (os_strcmp(token, "security_required=0") == 0) {
13059 			params.security_required = false;
13060 			security_required_set = true;
13061 			continue;
13062 		}
13063 
13064 		wpa_printf(MSG_INFO, "CTRL: Invalid NAN_PUBLISH parameter: %s",
13065 			   token);
13066 		goto fail;
13067 	}
13068 
13069 	/* Default security_required to true when cipher suites are specified */
13070 	if (params.cipher_suites_list && !security_required_set)
13071 		params.security_required = true;
13072 
13073 	if (params.gtk_required &&
13074 	    !wpas_nan_gtk_cs_supported(params.cipher_suites_list)) {
13075 		wpa_printf(MSG_INFO,
13076 			   "CTRL: GTK required but no GTK cipher suite configured");
13077 		goto fail;
13078 	}
13079 
13080 	publish_id = wpas_nan_publish(wpa_s, service_name, srv_proto_type,
13081 				      ssi, &params, p2p);
13082 	if (publish_id > 0)
13083 		ret = os_snprintf(buf, buflen, "%d", publish_id);
13084 fail:
13085 	if (params.nd_pmk)
13086 		forced_memzero(nd_pmk, PMK_LEN);
13087 
13088 	wpabuf_free(ssi);
13089 	os_free(freq_list);
13090 	os_free(cipher_list);
13091 	return ret;
13092 }
13093 
13094 
wpas_ctrl_nan_cancel_publish(struct wpa_supplicant * wpa_s,char * cmd)13095 static int wpas_ctrl_nan_cancel_publish(struct wpa_supplicant *wpa_s,
13096 					char *cmd)
13097 {
13098 	char *token, *context = NULL;
13099 	int publish_id = 0;
13100 
13101 	while ((token = str_token(cmd, " ", &context))) {
13102 		if (sscanf(token, "publish_id=%i", &publish_id) == 1)
13103 			continue;
13104 		wpa_printf(MSG_INFO,
13105 			   "CTRL: Invalid NAN_CANCEL_PUBLISH parameter: %s",
13106 			   token);
13107 		return -1;
13108 	}
13109 
13110 	if (publish_id <= 0) {
13111 		wpa_printf(MSG_INFO,
13112 			   "CTRL: Invalid or missing NAN_CANCEL_PUBLISH publish_id");
13113 		return -1;
13114 	}
13115 
13116 	wpas_nan_cancel_publish(wpa_s, publish_id);
13117 	return 0;
13118 }
13119 
13120 
wpas_ctrl_nan_update_publish(struct wpa_supplicant * wpa_s,char * cmd)13121 static int wpas_ctrl_nan_update_publish(struct wpa_supplicant *wpa_s,
13122 					char *cmd)
13123 {
13124 	char *token, *context = NULL;
13125 	int publish_id = 0;
13126 	struct wpabuf *ssi = NULL;
13127 	int ret = -1;
13128 
13129 	while ((token = str_token(cmd, " ", &context))) {
13130 		if (sscanf(token, "publish_id=%i", &publish_id) == 1)
13131 			continue;
13132 		if (os_strncmp(token, "ssi=", 4) == 0) {
13133 			if (ssi)
13134 				goto fail;
13135 			ssi = wpabuf_parse_bin(token + 4);
13136 			if (!ssi)
13137 				goto fail;
13138 			continue;
13139 		}
13140 		wpa_printf(MSG_INFO,
13141 			   "CTRL: Invalid NAN_UPDATE_PUBLISH parameter: %s",
13142 			   token);
13143 		goto fail;
13144 	}
13145 
13146 	if (publish_id <= 0) {
13147 		wpa_printf(MSG_INFO,
13148 			   "CTRL: Invalid or missing NAN_UPDATE_PUBLISH publish_id");
13149 		goto fail;
13150 	}
13151 
13152 	ret = wpas_nan_update_publish(wpa_s, publish_id, ssi);
13153 fail:
13154 	wpabuf_free(ssi);
13155 	return ret;
13156 }
13157 
13158 
wpas_ctrl_nan_publish_stop_listen(struct wpa_supplicant * wpa_s,char * cmd)13159 static int wpas_ctrl_nan_publish_stop_listen(struct wpa_supplicant *wpa_s,
13160 					     char *cmd)
13161 {
13162 	char *token, *context = NULL;
13163 	int publish_id = 0;
13164 
13165 	while ((token = str_token(cmd, " ", &context))) {
13166 		if (sscanf(token, "id=%i", &publish_id) == 1)
13167 			continue;
13168 		wpa_printf(MSG_INFO,
13169 			   "CTRL: Invalid NAN_PUBLISH_STOP_LISTEN parameter: %s",
13170 			   token);
13171 		return -1;
13172 	}
13173 
13174 	if (publish_id <= 0) {
13175 		wpa_printf(MSG_INFO,
13176 			   "CTRL: Invalid or missing NAN_PUBLISH_STOP_LISTEN publish_id");
13177 		return -1;
13178 	}
13179 
13180 	return wpas_nan_usd_publish_stop_listen(wpa_s, publish_id);
13181 }
13182 
13183 
wpas_ctrl_nan_subscribe(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)13184 static int wpas_ctrl_nan_subscribe(struct wpa_supplicant *wpa_s, char *cmd,
13185 				   char *buf, size_t buflen)
13186 {
13187 	char *token, *context = NULL;
13188 	int subscribe_id;
13189 	struct nan_subscribe_params params;
13190 	const char *service_name = NULL;
13191 	struct wpabuf *ssi = NULL;
13192 	int ret = -1;
13193 	enum nan_service_protocol_type srv_proto_type = 0;
13194 	int *freq_list = NULL;
13195 	bool p2p = false;
13196 	u8 forced_addr[ETH_ALEN];
13197 
13198 	os_memset(&params, 0, sizeof(params));
13199 	params.freq = NAN_USD_DEFAULT_FREQ;
13200 
13201 	while ((token = str_token(cmd, " ", &context))) {
13202 		if (os_strncmp(token, "service_name=", 13) == 0) {
13203 			service_name = token + 13;
13204 			continue;
13205 		}
13206 
13207 		if (os_strcmp(token, "active=1") == 0) {
13208 			params.active = true;
13209 			continue;
13210 		}
13211 
13212 		if (os_strncmp(token, "ttl=", 4) == 0) {
13213 			params.ttl = atoi(token + 4);
13214 			continue;
13215 		}
13216 
13217 		if (os_strncmp(token, "freq=", 5) == 0) {
13218 			params.freq = atoi(token + 5);
13219 			continue;
13220 		}
13221 
13222 		if (os_strncmp(token, "freq_list=", 10) == 0) {
13223 			char *pos = token + 10;
13224 
13225 			if (os_strcmp(pos, "all") == 0) {
13226 				os_free(freq_list);
13227 				freq_list = wpas_nan_usd_all_freqs(wpa_s);
13228 				params.freq_list = freq_list;
13229 				continue;
13230 			}
13231 
13232 			while (pos && pos[0]) {
13233 				int_array_add_unique(&freq_list, atoi(pos));
13234 				pos = os_strchr(pos, ',');
13235 				if (pos)
13236 					pos++;
13237 			}
13238 
13239 			params.freq_list = freq_list;
13240 			continue;
13241 		}
13242 
13243 		if (os_strncmp(token, "srv_proto_type=", 15) == 0) {
13244 			srv_proto_type = atoi(token + 15);
13245 			continue;
13246 		}
13247 
13248 		if (os_strncmp(token, "ssi=", 4) == 0) {
13249 			if (ssi)
13250 				goto fail;
13251 			ssi = wpabuf_parse_bin(token + 4);
13252 			if (!ssi)
13253 				goto fail;
13254 			continue;
13255 		}
13256 
13257 		if (os_strcmp(token, "p2p=1") == 0) {
13258 			p2p = true;
13259 			continue;
13260 		}
13261 
13262 		if (os_strcmp(token, "pr=1") == 0) {
13263 			params.proximity_ranging = true;
13264 			continue;
13265 		}
13266 
13267 		if (os_strcmp(token, "sync=1") == 0) {
13268 			params.sync = true;
13269 			continue;
13270 		}
13271 
13272 		if (os_strncmp(token, "match_filter_tx=", 16) == 0) {
13273 			params.match_filter_tx = token + 16;
13274 			continue;
13275 		}
13276 
13277 		if (os_strncmp(token, "match_filter_rx=", 16) == 0) {
13278 			params.match_filter_rx = token + 16;
13279 			continue;
13280 		}
13281 
13282 		if (os_strncmp(token, "srf_include=", 12) == 0) {
13283 			params.srf_include = !!atoi(token + 12);
13284 			continue;
13285 		}
13286 
13287 		if (os_strncmp(token, "srf_mac_list=", 13) == 0) {
13288 			params.srf_mac_list = token + 13;
13289 			continue;
13290 		}
13291 
13292 		if (os_strncmp(token, "srf_bf_len=", 11) == 0) {
13293 			params.srf_bf_len = atoi(token + 11);
13294 			continue;
13295 		}
13296 
13297 		if (os_strncmp(token, "srf_bf_idx=", 11) == 0) {
13298 			params.srf_bf_idx = atoi(token + 11);
13299 			continue;
13300 		}
13301 
13302 		if (os_strcmp(token, "close_proximity=1") == 0) {
13303 			params.close_proximity = true;
13304 			continue;
13305 		}
13306 
13307 		if (os_strncmp(token, "pbm=", 4) == 0) {
13308 			params.pbm = strtol(token + 4, NULL, 0);
13309 			continue;
13310 		}
13311 
13312 		if (os_strncmp(token, "forced_addr=", 12) == 0) {
13313 			if (hwaddr_aton(token + 12, forced_addr)) {
13314 				wpa_printf(MSG_INFO,
13315 					   "CTRL: Invalid forced_addr");
13316 				goto fail;
13317 			}
13318 			params.forced_addr = forced_addr;
13319 			continue;
13320 		}
13321 
13322 		wpa_printf(MSG_INFO,
13323 			   "CTRL: Invalid NAN_SUBSCRIBE parameter: %s",
13324 			   token);
13325 		goto fail;
13326 	}
13327 
13328 	subscribe_id = wpas_nan_subscribe(wpa_s, service_name,
13329 					  srv_proto_type, ssi,
13330 					  &params, p2p);
13331 	if (subscribe_id > 0)
13332 		ret = os_snprintf(buf, buflen, "%d", subscribe_id);
13333 fail:
13334 	wpabuf_free(ssi);
13335 	os_free(freq_list);
13336 	return ret;
13337 }
13338 
13339 
wpas_ctrl_nan_cancel_subscribe(struct wpa_supplicant * wpa_s,char * cmd)13340 static int wpas_ctrl_nan_cancel_subscribe(struct wpa_supplicant *wpa_s,
13341 					  char *cmd)
13342 {
13343 	char *token, *context = NULL;
13344 	int subscribe_id = 0;
13345 
13346 	while ((token = str_token(cmd, " ", &context))) {
13347 		if (sscanf(token, "subscribe_id=%i", &subscribe_id) == 1)
13348 			continue;
13349 		wpa_printf(MSG_INFO,
13350 			   "CTRL: Invalid NAN_CANCEL_SUBSCRIBE parameter: %s",
13351 			   token);
13352 		return -1;
13353 	}
13354 
13355 	if (subscribe_id <= 0) {
13356 		wpa_printf(MSG_INFO,
13357 			   "CTRL: Invalid or missing NAN_CANCEL_SUBSCRIBE subscribe_id");
13358 		return -1;
13359 	}
13360 
13361 	wpas_nan_cancel_subscribe(wpa_s, subscribe_id);
13362 	return 0;
13363 }
13364 
13365 
wpas_ctrl_nan_subscribe_stop_listen(struct wpa_supplicant * wpa_s,char * cmd)13366 static int wpas_ctrl_nan_subscribe_stop_listen(struct wpa_supplicant *wpa_s,
13367 					       char *cmd)
13368 {
13369 	char *token, *context = NULL;
13370 	int subscribe_id = 0;
13371 
13372 	while ((token = str_token(cmd, " ", &context))) {
13373 		if (sscanf(token, "id=%i", &subscribe_id) == 1)
13374 			continue;
13375 		wpa_printf(MSG_INFO,
13376 			   "CTRL: Invalid NAN_SUBSCRIBE_STOP_LISTEN parameter: %s",
13377 			   token);
13378 		return -1;
13379 	}
13380 
13381 	if (subscribe_id <= 0) {
13382 		wpa_printf(MSG_INFO,
13383 			   "CTRL: Invalid or missing NAN_SUBSCRIBE_STOP_LISTEN subscribe_id");
13384 		return -1;
13385 	}
13386 
13387 	return wpas_nan_usd_subscribe_stop_listen(wpa_s, subscribe_id);
13388 }
13389 
13390 
wpas_ctrl_nan_transmit(struct wpa_supplicant * wpa_s,char * cmd)13391 static int wpas_ctrl_nan_transmit(struct wpa_supplicant *wpa_s, char *cmd)
13392 {
13393 	char *token, *context = NULL;
13394 	int handle = 0;
13395 	int req_instance_id = 0;
13396 	struct wpabuf *ssi = NULL;
13397 	u8 peer_addr[ETH_ALEN];
13398 	int ret = -1;
13399 	u32 cookie = 0;
13400 	u32 *cookie_ptr = NULL;
13401 
13402 	os_memset(peer_addr, 0, ETH_ALEN);
13403 
13404 	while ((token = str_token(cmd, " ", &context))) {
13405 		if (sscanf(token, "handle=%i", &handle) == 1)
13406 			continue;
13407 
13408 		if (sscanf(token, "req_instance_id=%i", &req_instance_id) == 1)
13409 			continue;
13410 
13411 		if (os_strncmp(token, "address=", 8) == 0) {
13412 			if (hwaddr_aton(token + 8, peer_addr) < 0)
13413 				return -1;
13414 			continue;
13415 		}
13416 
13417 		if (os_strncmp(token, "ssi=", 4) == 0) {
13418 			if (ssi)
13419 				goto fail;
13420 			ssi = wpabuf_parse_bin(token + 4);
13421 			if (!ssi)
13422 				goto fail;
13423 			continue;
13424 		}
13425 
13426 		if (os_strncmp(token, "cookie=", 7) == 0) {
13427 			cookie = strtoul(token + 7, NULL, 0);
13428 			if (!cookie) {
13429 				wpa_printf(MSG_INFO,
13430 					   "CTRL: Invalid cookie value: %s",
13431 					   token + 7);
13432 				goto fail;
13433 			}
13434 
13435 			cookie_ptr = &cookie;
13436 			continue;
13437 		}
13438 
13439 		wpa_printf(MSG_INFO,
13440 			   "CTRL: Invalid NAN_TRANSMIT parameter: %s",
13441 			   token);
13442 		goto fail;
13443 	}
13444 
13445 	if (handle <= 0) {
13446 		wpa_printf(MSG_INFO,
13447 			   "CTRL: Invalid or missing NAN_TRANSMIT handle");
13448 		goto fail;
13449 	}
13450 
13451 	if (is_zero_ether_addr(peer_addr)) {
13452 		wpa_printf(MSG_INFO,
13453 			   "CTRL: Invalid or missing NAN_TRANSMIT address");
13454 		goto fail;
13455 	}
13456 
13457 	ret = wpas_nan_transmit(wpa_s, handle, ssi, NULL, peer_addr,
13458 				req_instance_id, cookie_ptr);
13459 fail:
13460 	wpabuf_free(ssi);
13461 	return ret;
13462 }
13463 
13464 
wpas_ctrl_nan_unpause_publish(struct wpa_supplicant * wpa_s,char * cmd)13465 static int wpas_ctrl_nan_unpause_publish(struct wpa_supplicant *wpa_s,
13466 					 char *cmd)
13467 {
13468 	char *token, *context = NULL;
13469 	int publish_id = 0;
13470 	int peer_instance_id = 0;
13471 	u8 peer_addr[ETH_ALEN];
13472 
13473 	os_memset(peer_addr, 0, ETH_ALEN);
13474 
13475 	while ((token = str_token(cmd, " ", &context))) {
13476 		if (sscanf(token, "publish_id=%i", &publish_id) == 1)
13477 			continue;
13478 
13479 		if (sscanf(token, "peer_instance_id=%i",
13480 			   &peer_instance_id) == 1)
13481 			continue;
13482 
13483 		if (os_strncmp(token, "peer=", 5) == 0) {
13484 			if (hwaddr_aton(token + 5, peer_addr) < 0)
13485 				return -1;
13486 			continue;
13487 		}
13488 
13489 		wpa_printf(MSG_INFO,
13490 			   "CTRL: Invalid NAN_UNPAUSE_PUBLISH parameter: %s",
13491 			   token);
13492 		return -1;
13493 	}
13494 
13495 	if (publish_id <= 0) {
13496 		wpa_printf(MSG_INFO,
13497 			   "CTRL: Invalid or missing NAN_UNPAUSE_PUBLIH publish_id");
13498 		return -1;
13499 	}
13500 
13501 	if (is_zero_ether_addr(peer_addr)) {
13502 		wpa_printf(MSG_INFO,
13503 			   "CTRL: Invalid or missing NAN_UNPAUSE_PUBLISH address");
13504 		return -1;
13505 	}
13506 
13507 	return wpas_nan_usd_unpause_publish(wpa_s, publish_id, peer_instance_id,
13508 					    peer_addr);
13509 }
13510 
13511 #endif /* CONFIG_NAN || CONFIG_NAN_USD */
13512 
13513 
wpa_supplicant_ctrl_iface_process(struct wpa_supplicant * wpa_s,char * buf,size_t * resp_len)13514 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
13515 					 char *buf, size_t *resp_len)
13516 {
13517 	char *reply;
13518 	const int reply_size = 4096;
13519 	int reply_len;
13520 
13521 	if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
13522 	    os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
13523 	    os_strncmp(buf, "PMKSA_ADD ", 10) == 0 ||
13524 	    os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
13525 		if (wpa_debug_show_keys)
13526 			wpa_dbg(wpa_s, MSG_DEBUG,
13527 				"Control interface command '%s'", buf);
13528 		else
13529 			wpa_dbg(wpa_s, MSG_DEBUG,
13530 				"Control interface command '%s [REMOVED]'",
13531 				os_strncmp(buf, WPA_CTRL_RSP,
13532 					   os_strlen(WPA_CTRL_RSP)) == 0 ?
13533 				WPA_CTRL_RSP :
13534 				(os_strncmp(buf, "SET_NETWORK ", 12) == 0 ?
13535 				 "SET_NETWORK" : "key-add"));
13536 	} else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
13537 		   os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
13538 		wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
13539 				      (const u8 *) buf, os_strlen(buf));
13540 	} else {
13541 		int level = wpas_ctrl_cmd_debug_level(buf);
13542 		wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
13543 	}
13544 
13545 	reply = os_malloc(reply_size);
13546 	if (reply == NULL) {
13547 		*resp_len = 1;
13548 		return NULL;
13549 	}
13550 
13551 	os_memcpy(reply, "OK\n", 3);
13552 	reply_len = 3;
13553 
13554 	if (os_strcmp(buf, "PING") == 0) {
13555 		os_memcpy(reply, "PONG\n", 5);
13556 		reply_len = 5;
13557 	} else if (os_strcmp(buf, "IFNAME") == 0) {
13558 		reply_len = os_strlen(wpa_s->ifname);
13559 		os_memcpy(reply, wpa_s->ifname, reply_len);
13560 	} else if (os_strncmp(buf, "RELOG", 5) == 0) {
13561 		if (wpa_debug_reopen_file() < 0)
13562 			reply_len = -1;
13563 	} else if (os_strncmp(buf, "NOTE ", 5) == 0) {
13564 		wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
13565 		wpa_trace_set_context(buf + 5);
13566 	} else if (os_strcmp(buf, "MIB") == 0) {
13567 		reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
13568 		if (reply_len >= 0) {
13569 			reply_len += eapol_sm_get_mib(wpa_s->eapol,
13570 						      reply + reply_len,
13571 						      reply_size - reply_len);
13572 #ifdef CONFIG_MACSEC
13573 			reply_len += ieee802_1x_kay_get_mib(
13574 				wpa_s->kay, reply + reply_len,
13575 				reply_size - reply_len);
13576 #endif /* CONFIG_MACSEC */
13577 		}
13578 	} else if (os_strncmp(buf, "STATUS", 6) == 0) {
13579 		reply_len = wpa_supplicant_ctrl_iface_status(
13580 			wpa_s, buf + 6, reply, reply_size);
13581 	} else if (os_strcmp(buf, "PMKSA") == 0) {
13582 		reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
13583 	} else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
13584 		wpas_ctrl_iface_pmksa_flush(wpa_s);
13585 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
13586 	} else if (os_strncmp(buf, "PMKSA_GET ", 10) == 0) {
13587 		reply_len = wpas_ctrl_iface_pmksa_get(wpa_s, buf + 10,
13588 						      reply, reply_size);
13589 	} else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
13590 		if (wpas_ctrl_iface_pmksa_add(wpa_s, buf + 10) < 0)
13591 			reply_len = -1;
13592 #ifdef CONFIG_MESH
13593 	} else if (os_strncmp(buf, "MESH_PMKSA_GET ", 15) == 0) {
13594 		reply_len = wpas_ctrl_iface_mesh_pmksa_get(wpa_s, buf + 15,
13595 							   reply, reply_size);
13596 	} else if (os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
13597 		if (wpas_ctrl_iface_mesh_pmksa_add(wpa_s, buf + 15) < 0)
13598 			reply_len = -1;
13599 #endif /* CONFIG_MESH */
13600 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
13601 	} else if (os_strncmp(buf, "SET ", 4) == 0) {
13602 		if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
13603 			reply_len = -1;
13604 	} else if (os_strncmp(buf, "DUMP", 4) == 0) {
13605 		reply_len = wpa_config_dump_values(wpa_s->conf,
13606 						   reply, reply_size);
13607 	} else if (os_strncmp(buf, "GET ", 4) == 0) {
13608 		reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
13609 							  reply, reply_size);
13610 	} else if (os_strcmp(buf, "LOGON") == 0) {
13611 		eapol_sm_notify_logoff(wpa_s->eapol, false);
13612 	} else if (os_strcmp(buf, "LOGOFF") == 0) {
13613 		eapol_sm_notify_logoff(wpa_s->eapol, true);
13614 	} else if (os_strcmp(buf, "REASSOCIATE") == 0) {
13615 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
13616 			reply_len = -1;
13617 		else
13618 			wpas_request_connection(wpa_s);
13619 	} else if (os_strcmp(buf, "REATTACH") == 0) {
13620 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
13621 		    !wpa_s->current_ssid)
13622 			reply_len = -1;
13623 		else {
13624 			wpa_s->reattach = 1;
13625 			wpas_request_connection(wpa_s);
13626 		}
13627 	} else if (os_strcmp(buf, "RECONNECT") == 0) {
13628 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
13629 			reply_len = -1;
13630 		else if (wpa_s->disconnected)
13631 			wpas_request_connection(wpa_s);
13632 #ifdef IEEE8021X_EAPOL
13633 	} else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
13634 		if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
13635 			reply_len = -1;
13636 #endif /* IEEE8021X_EAPOL */
13637 #ifdef CONFIG_IEEE80211R
13638 	} else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
13639 		if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
13640 			reply_len = -1;
13641 #endif /* CONFIG_IEEE80211R */
13642 #ifdef CONFIG_WPS
13643 	} else if (os_strcmp(buf, "WPS_PBC") == 0) {
13644 		int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
13645 		if (res == -2) {
13646 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
13647 			reply_len = 17;
13648 		} else if (res)
13649 			reply_len = -1;
13650 	} else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
13651 		int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
13652 		if (res == -2) {
13653 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
13654 			reply_len = 17;
13655 		} else if (res)
13656 			reply_len = -1;
13657 	} else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
13658 		reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
13659 							      reply,
13660 							      reply_size);
13661 	} else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
13662 		reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
13663 			wpa_s, buf + 14, reply, reply_size);
13664 	} else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
13665 		if (wpas_wps_cancel(wpa_s))
13666 			reply_len = -1;
13667 #ifdef CONFIG_WPS_NFC
13668 	} else if (os_strcmp(buf, "WPS_NFC") == 0) {
13669 		if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
13670 			reply_len = -1;
13671 	} else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
13672 		if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
13673 			reply_len = -1;
13674 	} else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
13675 		reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
13676 			wpa_s, buf + 21, reply, reply_size);
13677 	} else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
13678 		reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
13679 			wpa_s, buf + 14, reply, reply_size);
13680 	} else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
13681 		if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
13682 							       buf + 17))
13683 			reply_len = -1;
13684 	} else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
13685 		reply_len = wpas_ctrl_nfc_get_handover_req(
13686 			wpa_s, buf + 21, reply, reply_size);
13687 	} else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
13688 		reply_len = wpas_ctrl_nfc_get_handover_sel(
13689 			wpa_s, buf + 21, reply, reply_size);
13690 	} else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
13691 		if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
13692 			reply_len = -1;
13693 #endif /* CONFIG_WPS_NFC */
13694 	} else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
13695 		if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
13696 			reply_len = -1;
13697 #ifdef CONFIG_AP
13698 	} else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
13699 		reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
13700 			wpa_s, buf + 11, reply, reply_size);
13701 #endif /* CONFIG_AP */
13702 #ifdef CONFIG_WPS_ER
13703 	} else if (os_strcmp(buf, "WPS_ER_START") == 0) {
13704 		if (wpas_wps_er_start(wpa_s, NULL))
13705 			reply_len = -1;
13706 	} else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
13707 		if (wpas_wps_er_start(wpa_s, buf + 13))
13708 			reply_len = -1;
13709 	} else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
13710 		wpas_wps_er_stop(wpa_s);
13711 	} else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
13712 		if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
13713 			reply_len = -1;
13714 	} else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
13715 		int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
13716 		if (ret == -2) {
13717 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
13718 			reply_len = 17;
13719 		} else if (ret == -3) {
13720 			os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
13721 			reply_len = 18;
13722 		} else if (ret == -4) {
13723 			os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
13724 			reply_len = 20;
13725 		} else if (ret)
13726 			reply_len = -1;
13727 	} else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
13728 		if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
13729 			reply_len = -1;
13730 	} else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
13731 		if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
13732 								buf + 18))
13733 			reply_len = -1;
13734 	} else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
13735 		if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
13736 			reply_len = -1;
13737 #ifdef CONFIG_WPS_NFC
13738 	} else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
13739 		reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
13740 			wpa_s, buf + 24, reply, reply_size);
13741 #endif /* CONFIG_WPS_NFC */
13742 #endif /* CONFIG_WPS_ER */
13743 #endif /* CONFIG_WPS */
13744 #ifdef CONFIG_IBSS_RSN
13745 	} else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
13746 		if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
13747 			reply_len = -1;
13748 #endif /* CONFIG_IBSS_RSN */
13749 #ifdef CONFIG_MESH
13750 	} else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
13751 		reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
13752 			wpa_s, buf + 19, reply, reply_size);
13753 	} else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
13754 		reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
13755 			wpa_s, "", reply, reply_size);
13756 	} else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
13757 		if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
13758 			reply_len = -1;
13759 	} else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
13760 		if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
13761 								buf + 18))
13762 			reply_len = -1;
13763 	} else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
13764 		if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
13765 			reply_len = -1;
13766 	} else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
13767 		if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
13768 			reply_len = -1;
13769 	} else if (os_strncmp(buf, "MESH_LINK_PROBE ", 16) == 0) {
13770 		if (wpa_supplicant_ctrl_iface_mesh_link_probe(wpa_s, buf + 16))
13771 			reply_len = -1;
13772 #endif /* CONFIG_MESH */
13773 #ifdef CONFIG_P2P
13774 	} else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
13775 		if (p2p_ctrl_find(wpa_s, buf + 8))
13776 			reply_len = -1;
13777 	} else if (os_strcmp(buf, "P2P_FIND") == 0) {
13778 		if (p2p_ctrl_find(wpa_s, ""))
13779 			reply_len = -1;
13780 	} else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
13781 		wpas_p2p_stop_find(wpa_s);
13782 	} else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
13783 		if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
13784 			reply_len = -1;
13785 	} else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
13786 		if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
13787 			reply_len = -1;
13788 	} else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
13789 		reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
13790 					     reply_size);
13791 	} else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
13792 		if (p2p_ctrl_listen(wpa_s, buf + 11))
13793 			reply_len = -1;
13794 	} else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
13795 		if (p2p_ctrl_listen(wpa_s, ""))
13796 			reply_len = -1;
13797 	} else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
13798 		if (wpas_p2p_group_remove(wpa_s, buf + 17))
13799 			reply_len = -1;
13800 	} else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
13801 		if (p2p_ctrl_group_add(wpa_s, ""))
13802 			reply_len = -1;
13803 	} else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
13804 		if (p2p_ctrl_group_add(wpa_s, buf + 14))
13805 			reply_len = -1;
13806 	} else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
13807 		reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
13808 						  reply_size);
13809 	} else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
13810 		if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
13811 			reply_len = -1;
13812 	} else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
13813 		reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
13814 	} else if (os_strcmp(buf, "P2P_GET_DIRA") == 0) {
13815 		reply_len = wpas_p2p_get_dira(wpa_s, reply, reply_size);
13816 	} else if (os_strncmp(buf, "P2P_VALIDATE_DIRA ", 18) == 0) {
13817 		reply_len = p2p_ctrl_validate_dira(wpa_s, buf + 18,
13818 						   reply, reply_size);
13819 #ifdef CONFIG_PASN
13820 #ifdef CONFIG_TESTING_OPTIONS
13821 	} else if (os_strcmp(buf, "P2P_GET_PASNPTK") == 0) {
13822 		reply_len = p2p_ctrl_get_pasn_ptk(wpa_s, reply, reply_size);
13823 #endif /* CONFIG_TESTING_OPTIONS */
13824 #endif /* CONFIG_PASN */
13825 	} else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
13826 		reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
13827 						   reply_size);
13828 	} else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
13829 		if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
13830 			reply_len = -1;
13831 	} else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
13832 		if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
13833 			reply_len = -1;
13834 	} else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
13835 		wpas_p2p_sd_service_update(wpa_s);
13836 	} else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
13837 		if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
13838 			reply_len = -1;
13839 	} else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
13840 		wpas_p2p_service_flush(wpa_s);
13841 	} else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
13842 		if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
13843 			reply_len = -1;
13844 	} else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
13845 		if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
13846 			reply_len = -1;
13847 	} else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
13848 		if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
13849 			reply_len = -1;
13850 	} else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
13851 		if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
13852 			reply_len = -1;
13853 	} else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
13854 		if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
13855 			reply_len = -1;
13856 	} else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
13857 		reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
13858 					      reply_size);
13859 	} else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
13860 		if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
13861 			reply_len = -1;
13862 	} else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
13863 		p2p_ctrl_flush(wpa_s);
13864 	} else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
13865 		if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
13866 			reply_len = -1;
13867 	} else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
13868 		if (wpas_p2p_cancel(wpa_s))
13869 			reply_len = -1;
13870 	} else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
13871 		if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
13872 			reply_len = -1;
13873 	} else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
13874 		if (p2p_ctrl_presence_req(wpa_s, "") < 0)
13875 			reply_len = -1;
13876 	} else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
13877 		if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
13878 			reply_len = -1;
13879 	} else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
13880 		if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
13881 			reply_len = -1;
13882 	} else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
13883 		if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
13884 			reply_len = -1;
13885 	} else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
13886 		if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
13887 			reply_len = -1;
13888 	} else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
13889 		if (wpas_p2p_lo_stop(wpa_s))
13890 			reply_len = -1;
13891 	} else if (os_strcmp(buf, "P2P_REMOVE_IDENTITY") == 0) {
13892 		if (wpas_p2p_remove_all_identity(wpa_s))
13893 			reply_len = -1;
13894 #ifdef CONFIG_TESTING_OPTIONS
13895 	} else if (os_strncmp(buf, "P2P_PMK_GET", 12) == 0) {
13896 		reply_len = p2p_ctrl_pmk_get(wpa_s, reply, reply_size);
13897 #endif /* CONFIG_TESTING_OPTIONS */
13898 #endif /* CONFIG_P2P */
13899 #ifdef CONFIG_WIFI_DISPLAY
13900 	} else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
13901 		if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
13902 			reply_len = -1;
13903 	} else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
13904 		reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
13905 						     reply, reply_size);
13906 #endif /* CONFIG_WIFI_DISPLAY */
13907 #ifdef CONFIG_INTERWORKING
13908 	} else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
13909 		if (interworking_fetch_anqp(wpa_s) < 0)
13910 			reply_len = -1;
13911 	} else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
13912 		interworking_stop_fetch_anqp(wpa_s);
13913 	} else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
13914 		if (ctrl_interworking_select(wpa_s, NULL) < 0)
13915 			reply_len = -1;
13916 	} else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
13917 		if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
13918 			reply_len = -1;
13919 	} else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
13920 		if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
13921 			reply_len = -1;
13922 	} else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
13923 		int id;
13924 
13925 		id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
13926 		if (id < 0)
13927 			reply_len = -1;
13928 		else {
13929 			reply_len = os_snprintf(reply, reply_size, "%d\n", id);
13930 			if (os_snprintf_error(reply_size, reply_len))
13931 				reply_len = -1;
13932 		}
13933 	} else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
13934 		if (get_anqp(wpa_s, buf + 9) < 0)
13935 			reply_len = -1;
13936 	} else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
13937 		if (gas_request(wpa_s, buf + 12) < 0)
13938 			reply_len = -1;
13939 	} else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
13940 		reply_len = gas_response_get(wpa_s, buf + 17, reply,
13941 					     reply_size);
13942 #endif /* CONFIG_INTERWORKING */
13943 #ifdef CONFIG_HS20
13944 	} else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
13945 		if (get_hs20_anqp(wpa_s, buf + 14) < 0)
13946 			reply_len = -1;
13947 	} else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
13948 		if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
13949 			reply_len = -1;
13950 #endif /* CONFIG_HS20 */
13951 	} else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
13952 	{
13953 		if (wpa_supplicant_ctrl_iface_ctrl_rsp(
13954 			    wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
13955 			reply_len = -1;
13956 		else {
13957 			/*
13958 			 * Notify response from timeout to allow the control
13959 			 * interface response to be sent first.
13960 			 */
13961 			eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
13962 					       wpa_s, NULL);
13963 		}
13964 	} else if (os_strcmp(buf, "RECONFIGURE") == 0) {
13965 		if (wpa_supplicant_reload_configuration(wpa_s))
13966 			reply_len = -1;
13967 	} else if (os_strcmp(buf, "TERMINATE") == 0) {
13968 		wpa_supplicant_terminate_proc(wpa_s->global);
13969 	} else if (os_strncmp(buf, "BSSID ", 6) == 0) {
13970 		if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
13971 			reply_len = -1;
13972 	} else if (os_strncmp(buf, "BSSID_IGNORE", 12) == 0) {
13973 		reply_len = wpa_supplicant_ctrl_iface_bssid_ignore(
13974 			wpa_s, buf + 12, reply, reply_size);
13975 	} else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
13976 		/* deprecated backwards compatibility alias for BSSID_IGNORE */
13977 		reply_len = wpa_supplicant_ctrl_iface_bssid_ignore(
13978 			wpa_s, buf + 9, reply, reply_size);
13979 	} else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
13980 		reply_len = wpa_supplicant_ctrl_iface_log_level(
13981 			wpa_s, buf + 9, reply, reply_size);
13982 	} else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
13983 		reply_len = wpa_supplicant_ctrl_iface_list_networks(
13984 			wpa_s, buf + 14, reply, reply_size);
13985 	} else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
13986 		reply_len = wpa_supplicant_ctrl_iface_list_networks(
13987 			wpa_s, NULL, reply, reply_size);
13988 	} else if (os_strcmp(buf, "DISCONNECT") == 0) {
13989 		wpas_request_disconnection(wpa_s);
13990 	} else if (os_strcmp(buf, "SCAN") == 0) {
13991 		wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
13992 	} else if (os_strncmp(buf, "SCAN ", 5) == 0) {
13993 		wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
13994 	} else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
13995 		reply_len = wpa_supplicant_ctrl_iface_scan_results(
13996 			wpa_s, reply, reply_size);
13997 	} else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
13998 		if (wpas_abort_ongoing_scan(wpa_s) < 0)
13999 			reply_len = -1;
14000 	} else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
14001 		if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
14002 			reply_len = -1;
14003 	} else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
14004 		if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
14005 			reply_len = -1;
14006 	} else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
14007 		if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
14008 			reply_len = -1;
14009 	} else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
14010 		reply_len = wpa_supplicant_ctrl_iface_add_network(
14011 			wpa_s, reply, reply_size);
14012 	} else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
14013 		if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
14014 			reply_len = -1;
14015 	} else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
14016 		if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
14017 			reply_len = -1;
14018 	} else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
14019 		reply_len = wpa_supplicant_ctrl_iface_get_network(
14020 			wpa_s, buf + 12, reply, reply_size);
14021 	} else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
14022 		if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
14023 							  wpa_s))
14024 			reply_len = -1;
14025 	} else if (os_strcmp(buf, "LIST_CREDS") == 0) {
14026 		reply_len = wpa_supplicant_ctrl_iface_list_creds(
14027 			wpa_s, reply, reply_size);
14028 	} else if (os_strcmp(buf, "ADD_CRED") == 0) {
14029 		reply_len = wpa_supplicant_ctrl_iface_add_cred(
14030 			wpa_s, reply, reply_size);
14031 	} else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
14032 		if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
14033 			reply_len = -1;
14034 	} else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
14035 		if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
14036 			reply_len = -1;
14037 	} else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
14038 		reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
14039 							       reply,
14040 							       reply_size);
14041 #ifndef CONFIG_NO_CONFIG_WRITE
14042 	} else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
14043 		if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
14044 			reply_len = -1;
14045 #endif /* CONFIG_NO_CONFIG_WRITE */
14046 	} else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
14047 		reply_len = wpa_supplicant_ctrl_iface_get_capability(
14048 			wpa_s, buf + 15, reply, reply_size);
14049 	} else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
14050 		if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
14051 			reply_len = -1;
14052 	} else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
14053 		if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
14054 			reply_len = -1;
14055 	} else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
14056 		reply_len = wpa_supplicant_global_iface_list(
14057 			wpa_s->global, reply, reply_size);
14058 	} else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
14059 		reply_len = wpa_supplicant_global_iface_interfaces(
14060 			wpa_s->global, buf + 10, reply, reply_size);
14061 	} else if (os_strncmp(buf, "BSS ", 4) == 0) {
14062 		reply_len = wpa_supplicant_ctrl_iface_bss(
14063 			wpa_s, buf + 4, reply, reply_size);
14064 #ifdef CONFIG_AP
14065 	} else if (os_strcmp(buf, "STA-FIRST") == 0) {
14066 		reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
14067 	} else if (os_strncmp(buf, "STA ", 4) == 0) {
14068 		reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
14069 					      reply_size);
14070 	} else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
14071 		reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
14072 						   reply_size);
14073 	} else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
14074 		if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
14075 			reply_len = -1;
14076 	} else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
14077 		if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
14078 			reply_len = -1;
14079 	} else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
14080 		if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
14081 			reply_len = -1;
14082 	} else if (os_strcmp(buf, "STOP_AP") == 0) {
14083 		if (wpas_ap_stop_ap(wpa_s))
14084 			reply_len = -1;
14085 	} else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
14086 		if (wpas_ap_update_beacon(wpa_s))
14087 			reply_len = -1;
14088 	} else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
14089 		if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
14090 			if (ap_ctrl_iface_acl_add_mac(wpa_s,
14091 						      DENY_UNLESS_ACCEPTED,
14092 						      buf + 19) ||
14093 			    ap_ctrl_iface_set_acl(wpa_s))
14094 				reply_len = -1;
14095 		} else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
14096 			if (ap_ctrl_iface_acl_del_mac(wpa_s,
14097 						      DENY_UNLESS_ACCEPTED,
14098 						      buf + 19) ||
14099 			    ap_ctrl_iface_set_acl(wpa_s) ||
14100 			    ap_ctrl_iface_disassoc_accept_mac(wpa_s))
14101 				reply_len = -1;
14102 		} else if (os_strcmp(buf + 11, "SHOW") == 0) {
14103 			reply_len = ap_ctrl_iface_acl_show_mac(
14104 				wpa_s, DENY_UNLESS_ACCEPTED,
14105 				reply, reply_size);
14106 		} else if (os_strcmp(buf + 11, "CLEAR") == 0) {
14107 			ap_ctrl_iface_acl_clear_list(wpa_s,
14108 						     DENY_UNLESS_ACCEPTED);
14109 			if (ap_ctrl_iface_set_acl(wpa_s) ||
14110 			    ap_ctrl_iface_disassoc_accept_mac(wpa_s))
14111 				reply_len = -1;
14112 		} else {
14113 			reply_len = -1;
14114 		}
14115 	} else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
14116 		if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
14117 			if (ap_ctrl_iface_acl_add_mac(wpa_s,
14118 						      ACCEPT_UNLESS_DENIED,
14119 						      buf + 17) ||
14120 			    ap_ctrl_iface_set_acl(wpa_s) ||
14121 			    ap_ctrl_iface_disassoc_deny_mac(wpa_s))
14122 				reply_len = -1;
14123 		} else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
14124 			if (ap_ctrl_iface_acl_del_mac(wpa_s,
14125 						      ACCEPT_UNLESS_DENIED,
14126 						      buf + 17) ||
14127 			    ap_ctrl_iface_set_acl(wpa_s))
14128 				reply_len = -1;
14129 		} else if (os_strcmp(buf + 9, "SHOW") == 0) {
14130 			reply_len = ap_ctrl_iface_acl_show_mac(
14131 				wpa_s, ACCEPT_UNLESS_DENIED, reply, reply_size);
14132 		} else if (os_strcmp(buf + 9, "CLEAR") == 0) {
14133 			ap_ctrl_iface_acl_clear_list(wpa_s,
14134 						     ACCEPT_UNLESS_DENIED);
14135 			if (ap_ctrl_iface_set_acl(wpa_s))
14136 				reply_len = -1;
14137 		} else {
14138 			reply_len = -1;
14139 		}
14140 #endif /* CONFIG_AP */
14141 	} else if (os_strcmp(buf, "SUSPEND") == 0) {
14142 		wpas_notify_suspend(wpa_s->global);
14143 	} else if (os_strcmp(buf, "RESUME") == 0) {
14144 		wpas_notify_resume(wpa_s->global);
14145 #ifdef CONFIG_TESTING_OPTIONS
14146 	} else if (os_strcmp(buf, "DROP_SA") == 0) {
14147 		wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
14148 #endif /* CONFIG_TESTING_OPTIONS */
14149 	} else if (os_strncmp(buf, "ROAM ", 5) == 0) {
14150 		if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
14151 			reply_len = -1;
14152 	} else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
14153 		wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
14154 	} else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
14155 		if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
14156 			reply_len = -1;
14157 	} else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
14158 		if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
14159 							       buf + 17))
14160 			reply_len = -1;
14161 	} else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
14162 		wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
14163 #ifdef CONFIG_TDLS
14164 	} else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
14165 		if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
14166 			reply_len = -1;
14167 	} else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
14168 		if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
14169 			reply_len = -1;
14170 	} else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
14171 		if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
14172 			reply_len = -1;
14173 	} else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
14174 		if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
14175 							       buf + 17))
14176 			reply_len = -1;
14177 	} else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
14178 		if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
14179 								      buf + 24))
14180 			reply_len = -1;
14181 	} else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
14182 		reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
14183 			wpa_s, buf + 17, reply, reply_size);
14184 #endif /* CONFIG_TDLS */
14185 #ifndef CONFIG_NO_WMM_AC
14186 	} else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
14187 		reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
14188 	} else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
14189 		if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
14190 			reply_len = -1;
14191 	} else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
14192 		if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
14193 			reply_len = -1;
14194 #endif /* CONFIG_NO_WMM_AC */
14195 	} else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
14196 		reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
14197 						       reply_size);
14198 	} else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
14199 		if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
14200 			reply_len = -1;
14201 	} else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
14202 		reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
14203 						       reply_size);
14204 #ifdef CONFIG_AUTOSCAN
14205 	} else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
14206 		if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
14207 			reply_len = -1;
14208 #endif /* CONFIG_AUTOSCAN */
14209 	} else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
14210 		reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
14211 							 reply_size);
14212 	} else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
14213 		reply_len = wpas_ctrl_iface_driver_flags2(wpa_s, reply,
14214 							  reply_size);
14215 #ifdef ANDROID
14216 	} else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
14217 		reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
14218 						      reply_size);
14219 #endif /* ANDROID */
14220 	} else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
14221 		reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
14222 						      reply_size);
14223 	} else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
14224 		pmksa_cache_clear_current(wpa_s->wpa);
14225 		eapol_sm_request_reauth(wpa_s->eapol);
14226 #ifdef CONFIG_WNM
14227 	} else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
14228 		if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
14229 			reply_len = -1;
14230 	} else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
14231 		if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
14232 				reply_len = -1;
14233 	} else if (os_strncmp(buf, "COLOC_INTF_REPORT ", 18) == 0) {
14234 		if (wpas_ctrl_iface_coloc_intf_report(wpa_s, buf + 18))
14235 			reply_len = -1;
14236 #endif /* CONFIG_WNM */
14237 #ifdef CONFIG_WNM_AP
14238 	} else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
14239 		if (ap_ctrl_iface_disassoc_imminent(wpa_s, buf + 18))
14240 			reply_len = -1;
14241 	} else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
14242 		if (ap_ctrl_iface_ess_disassoc(wpa_s, buf + 13))
14243 			reply_len = -1;
14244 	} else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
14245 		if (ap_ctrl_iface_bss_tm_req(wpa_s, buf + 11))
14246 			reply_len = -1;
14247 #endif /* CONFIG_WNM_AP */
14248 	} else if (os_strcmp(buf, "FLUSH") == 0) {
14249 		wpa_supplicant_ctrl_iface_flush(wpa_s);
14250 	} else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
14251 		reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
14252 						 reply_size);
14253 #ifdef CONFIG_TESTING_OPTIONS
14254 	} else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
14255 		if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
14256 			reply_len = -1;
14257 	} else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
14258 		wpas_ctrl_iface_mgmt_tx_done(wpa_s);
14259 	} else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
14260 		if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
14261 			reply_len = -1;
14262 	} else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
14263 		if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
14264 			reply_len = -1;
14265 	} else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
14266 		if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
14267 			reply_len = -1;
14268 	} else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
14269 		if (wpas_ctrl_iface_eapol_tx(wpa_s, buf + 9) < 0)
14270 			reply_len = -1;
14271 	} else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
14272 		if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
14273 			reply_len = -1;
14274 	} else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
14275 		if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
14276 			reply_len = -1;
14277 	} else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
14278 		if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
14279 			reply_len = -1;
14280 	} else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
14281 		if (testing_set_fail_pattern(true, buf + 16) < 0)
14282 			reply_len = -1;
14283 	} else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
14284 		reply_len = testing_get_fail_pattern(true, reply, reply_size);
14285 	} else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
14286 		if (testing_set_fail_pattern(false, buf + 10) < 0)
14287 			reply_len = -1;
14288 	} else if (os_strcmp(buf, "GET_FAIL") == 0) {
14289 		reply_len = testing_get_fail_pattern(false, reply, reply_size);
14290 	} else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
14291 		if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
14292 			reply_len = -1;
14293 	} else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
14294 		if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
14295 			reply_len = -1;
14296 	} else if (os_strncmp(buf, "TEST_EAPOL_M2_ELEMS ", 20) == 0) {
14297 		if (wpas_ctrl_test_eapol_m2_elems(wpa_s, buf + 20) < 0)
14298 			reply_len = -1;
14299 	} else if (os_strncmp(buf, "TEST_EAPOL_M4_ELEMS ", 20) == 0) {
14300 		if (wpas_ctrl_test_eapol_m4_elems(wpa_s, buf + 20) < 0)
14301 			reply_len = -1;
14302 	} else if (os_strcmp(buf, "RESET_PN") == 0) {
14303 		if (wpas_ctrl_reset_pn(wpa_s) < 0)
14304 			reply_len = -1;
14305 	} else if (os_strncmp(buf, "KEY_REQUEST ", 12) == 0) {
14306 		if (wpas_ctrl_key_request(wpa_s, buf + 12) < 0)
14307 			reply_len = -1;
14308 	} else if (os_strcmp(buf, "RESEND_ASSOC") == 0) {
14309 		if (wpas_ctrl_resend_assoc(wpa_s) < 0)
14310 			reply_len = -1;
14311 	} else if (os_strcmp(buf, "UNPROT_DEAUTH") == 0) {
14312 		sme_event_unprot_disconnect(
14313 			wpa_s, wpa_s->bssid, NULL,
14314 			WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
14315 	} else if (os_strncmp(buf, "TWT_SETUP ", 10) == 0) {
14316 		if (wpas_ctrl_iface_send_twt_setup(wpa_s, buf + 9))
14317 			reply_len = -1;
14318 	} else if (os_strcmp(buf, "TWT_SETUP") == 0) {
14319 		if (wpas_ctrl_iface_send_twt_setup(wpa_s, ""))
14320 			reply_len = -1;
14321 	} else if (os_strncmp(buf, "TWT_TEARDOWN ", 13) == 0) {
14322 		if (wpas_ctrl_iface_send_twt_teardown(wpa_s, buf + 12))
14323 			reply_len = -1;
14324 	} else if (os_strcmp(buf, "TWT_TEARDOWN") == 0) {
14325 		if (wpas_ctrl_iface_send_twt_teardown(wpa_s, ""))
14326 			reply_len = -1;
14327 	} else if (os_strncmp(buf, "ML_PROBE_REQ ", 13) == 0) {
14328 		if (wpas_ctrl_ml_probe(wpa_s, buf + 13))
14329 			reply_len = -1;
14330 	} else if (os_strncmp(buf, "TEST_RSNXE_DATA ", 16) == 0) {
14331 		if (wpas_ctrl_test_rsnxe_data(wpa_s, buf + 16) < 0)
14332 			reply_len = -1;
14333 	} else if (os_strcmp(buf, "GET_TK") == 0) {
14334 		reply_len = wpas_ctrl_iface_get_tk(wpa_s, reply, reply_size);
14335 #endif /* CONFIG_TESTING_OPTIONS */
14336 	} else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
14337 		if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
14338 			reply_len = -1;
14339 	} else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
14340 		reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
14341 						      reply_size);
14342 	} else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
14343 		if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
14344 			reply_len = -1;
14345 #ifndef CONFIG_NO_RRM
14346 	} else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
14347 		if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
14348 			reply_len = -1;
14349 #endif /* CONFIG_NO_RRM */
14350 	} else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
14351 		wpas_ctrl_iface_erp_flush(wpa_s);
14352 	} else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
14353 		if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
14354 			reply_len = -1;
14355 	} else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
14356 		reply_len = wpas_ctrl_iface_get_pref_freq_list(
14357 			wpa_s, buf + 19, reply, reply_size);
14358 #ifdef CONFIG_FILS
14359 	} else if (os_strncmp(buf, "FILS_HLP_REQ_ADD ", 17) == 0) {
14360 		if (wpas_ctrl_iface_fils_hlp_req_add(wpa_s, buf + 17))
14361 			reply_len = -1;
14362 	} else if (os_strcmp(buf, "FILS_HLP_REQ_FLUSH") == 0) {
14363 		wpas_flush_fils_hlp_req(wpa_s);
14364 #endif /* CONFIG_FILS */
14365 #ifdef CONFIG_DPP
14366 	} else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
14367 		int res;
14368 
14369 		res = wpas_dpp_qr_code(wpa_s, buf + 12);
14370 		if (res < 0) {
14371 			reply_len = -1;
14372 		} else {
14373 			reply_len = os_snprintf(reply, reply_size, "%d", res);
14374 			if (os_snprintf_error(reply_size, reply_len))
14375 				reply_len = -1;
14376 		}
14377 	} else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
14378 		int res;
14379 
14380 		res = wpas_dpp_nfc_uri(wpa_s, buf + 12);
14381 		if (res < 0) {
14382 			reply_len = -1;
14383 		} else {
14384 			reply_len = os_snprintf(reply, reply_size, "%d", res);
14385 			if (os_snprintf_error(reply_size, reply_len))
14386 				reply_len = -1;
14387 		}
14388 	} else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
14389 		int res;
14390 
14391 		res = wpas_dpp_nfc_handover_req(wpa_s, buf + 20);
14392 		if (res < 0) {
14393 			reply_len = -1;
14394 		} else {
14395 			reply_len = os_snprintf(reply, reply_size, "%d", res);
14396 			if (os_snprintf_error(reply_size, reply_len))
14397 				reply_len = -1;
14398 		}
14399 	} else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
14400 		int res;
14401 
14402 		res = wpas_dpp_nfc_handover_sel(wpa_s, buf + 20);
14403 		if (res < 0) {
14404 			reply_len = -1;
14405 		} else {
14406 			reply_len = os_snprintf(reply, reply_size, "%d", res);
14407 			if (os_snprintf_error(reply_size, reply_len))
14408 				reply_len = -1;
14409 		}
14410 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
14411 		int res;
14412 
14413 		res = dpp_bootstrap_gen(wpa_s->dpp, buf + 18);
14414 		if (res < 0) {
14415 			reply_len = -1;
14416 		} else {
14417 			reply_len = os_snprintf(reply, reply_size, "%d", res);
14418 			if (os_snprintf_error(reply_size, reply_len))
14419 				reply_len = -1;
14420 		}
14421 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
14422 		if (dpp_bootstrap_remove(wpa_s->dpp, buf + 21) < 0)
14423 			reply_len = -1;
14424 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
14425 		const char *uri;
14426 
14427 		uri = dpp_bootstrap_get_uri(wpa_s->dpp, atoi(buf + 22));
14428 		if (!uri) {
14429 			reply_len = -1;
14430 		} else {
14431 			reply_len = os_snprintf(reply, reply_size, "%s", uri);
14432 			if (os_snprintf_error(reply_size, reply_len))
14433 				reply_len = -1;
14434 		}
14435 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
14436 		reply_len = dpp_bootstrap_info(wpa_s->dpp, atoi(buf + 19),
14437 					       reply, reply_size);
14438 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
14439 		if (dpp_bootstrap_set(wpa_s->dpp, atoi(buf + 18),
14440 				      os_strchr(buf + 18, ' ')) < 0)
14441 			reply_len = -1;
14442 	} else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
14443 		if (wpas_dpp_auth_init(wpa_s, buf + 13) < 0)
14444 			reply_len = -1;
14445 	} else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
14446 		if (wpas_dpp_listen(wpa_s, buf + 11) < 0)
14447 			reply_len = -1;
14448 	} else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
14449 		wpas_dpp_stop(wpa_s);
14450 		wpas_dpp_listen_stop(wpa_s);
14451 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
14452 		int res;
14453 
14454 		res = dpp_configurator_add(wpa_s->dpp, buf + 20);
14455 		if (res < 0) {
14456 			reply_len = -1;
14457 		} else {
14458 			reply_len = os_snprintf(reply, reply_size, "%d", res);
14459 			if (os_snprintf_error(reply_size, reply_len))
14460 				reply_len = -1;
14461 		}
14462 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_SET ", 21) == 0) {
14463 		if (dpp_configurator_set(wpa_s->dpp, buf + 20) < 0)
14464 			reply_len = -1;
14465 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
14466 		if (dpp_configurator_remove(wpa_s->dpp, buf + 24) < 0)
14467 			reply_len = -1;
14468 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
14469 		if (wpas_dpp_configurator_sign(wpa_s, buf + 21) < 0)
14470 			reply_len = -1;
14471 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
14472 		reply_len = dpp_configurator_get_key_id(wpa_s->dpp,
14473 							atoi(buf + 25),
14474 							reply, reply_size);
14475 	} else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
14476 		int res;
14477 
14478 		res = wpas_dpp_pkex_add(wpa_s, buf + 12);
14479 		if (res < 0) {
14480 			reply_len = -1;
14481 		} else {
14482 			reply_len = os_snprintf(reply, reply_size, "%d", res);
14483 			if (os_snprintf_error(reply_size, reply_len))
14484 				reply_len = -1;
14485 		}
14486 	} else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
14487 		if (wpas_dpp_pkex_remove(wpa_s, buf + 16) < 0)
14488 			reply_len = -1;
14489 	} else if (os_strncmp(buf, "DPP_CONF_SET ", 13) == 0) {
14490 		if (wpas_dpp_conf_set(wpa_s, buf + 12) < 0)
14491 			reply_len = -1;
14492 #ifdef CONFIG_DPP2
14493 	} else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
14494 		if (wpas_dpp_controller_start(wpa_s, buf + 20) < 0)
14495 			reply_len = -1;
14496 	} else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
14497 		if (wpas_dpp_controller_start(wpa_s, NULL) < 0)
14498 			reply_len = -1;
14499 	} else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
14500 		dpp_controller_stop(wpa_s->dpp);
14501 	} else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
14502 		if (wpas_dpp_chirp(wpa_s, buf + 9) < 0)
14503 			reply_len = -1;
14504 	} else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
14505 		wpas_dpp_chirp_stop(wpa_s);
14506 	} else if (os_strncmp(buf, "DPP_RECONFIG ", 13) == 0) {
14507 		if (wpas_dpp_reconfig(wpa_s, buf + 13) < 0)
14508 			reply_len = -1;
14509 	} else if (os_strncmp(buf, "DPP_CA_SET ", 11) == 0) {
14510 		if (wpas_dpp_ca_set(wpa_s, buf + 10) < 0)
14511 			reply_len = -1;
14512 #endif /* CONFIG_DPP2 */
14513 #ifdef CONFIG_DPP3
14514 	} else if (os_strcmp(buf, "DPP_PUSH_BUTTON") == 0) {
14515 		if (wpas_dpp_push_button(wpa_s, NULL) < 0)
14516 			reply_len = -1;
14517 	} else if (os_strncmp(buf, "DPP_PUSH_BUTTON ", 16) == 0) {
14518 		if (wpas_dpp_push_button(wpa_s, buf + 15) < 0)
14519 			reply_len = -1;
14520 #endif /* CONFIG_DPP3 */
14521 #endif /* CONFIG_DPP */
14522 #if defined(CONFIG_NAN) || defined(CONFIG_NAN_USD)
14523 	} else if (os_strncmp(buf, "NAN_PUBLISH ", 12) == 0) {
14524 		reply_len = wpas_ctrl_nan_publish(wpa_s, buf + 12, reply,
14525 						  reply_size);
14526 	} else if (os_strncmp(buf, "NAN_CANCEL_PUBLISH ", 19) == 0) {
14527 		if (wpas_ctrl_nan_cancel_publish(wpa_s, buf + 19) < 0)
14528 			reply_len = -1;
14529 	} else if (os_strncmp(buf, "NAN_UPDATE_PUBLISH ", 19) == 0) {
14530 		if (wpas_ctrl_nan_update_publish(wpa_s, buf + 19) < 0)
14531 			reply_len = -1;
14532 	} else if (os_strncmp(buf, "NAN_PUBLISH_STOP_LISTEN ", 24) == 0) {
14533 		if (wpas_ctrl_nan_publish_stop_listen(wpa_s, buf + 24) < 0)
14534 			reply_len = -1;
14535 	} else if (os_strncmp(buf, "NAN_SUBSCRIBE ", 14) == 0) {
14536 		reply_len = wpas_ctrl_nan_subscribe(wpa_s, buf + 14, reply,
14537 						    reply_size);
14538 	} else if (os_strncmp(buf, "NAN_CANCEL_SUBSCRIBE ", 21) == 0) {
14539 		if (wpas_ctrl_nan_cancel_subscribe(wpa_s, buf + 21) < 0)
14540 			reply_len = -1;
14541 	} else if (os_strncmp(buf, "NAN_SUBSCRIBE_STOP_LISTEN ", 26) == 0) {
14542 		if (wpas_ctrl_nan_subscribe_stop_listen(wpa_s, buf + 26) < 0)
14543 			reply_len = -1;
14544 	} else if (os_strncmp(buf, "NAN_TRANSMIT ", 13) == 0) {
14545 		if (wpas_ctrl_nan_transmit(wpa_s, buf + 13) < 0)
14546 			reply_len = -1;
14547 	} else if (os_strncmp(buf, "NAN_UNPAUSE_PUBLISH ", 20) == 0) {
14548 		if (wpas_ctrl_nan_unpause_publish(wpa_s, buf + 20) < 0)
14549 			reply_len = -1;
14550 	} else if (os_strcmp(buf, "NAN_FLUSH") == 0) {
14551 		wpas_nan_de_flush(wpa_s);
14552 #endif /* CONFIG_NAN || CONFIG_NAN_USD */
14553 #ifdef CONFIG_PASN
14554 	} else if (os_strncmp(buf, "PASN_START ", 11) == 0) {
14555 		if (wpas_ctrl_iface_pasn_start(wpa_s, buf + 11) < 0)
14556 			reply_len = -1;
14557 	} else if (os_strcmp(buf, "PASN_STOP") == 0) {
14558 		wpas_pasn_auth_stop(wpa_s);
14559 	} else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
14560 		reply_len = ptksa_cache_list(wpa_s->ptksa, reply, reply_size);
14561 	} else if (os_strncmp(buf, "PASN_DEAUTH ", 12) == 0) {
14562 		if (wpas_ctrl_iface_pasn_deauthenticate(wpa_s, buf + 12) < 0)
14563 			reply_len = -1;
14564 #ifdef CONFIG_PR
14565 	} else if (os_strncmp(buf, "PR_PASN_START ", 14) == 0) {
14566 		if (wpas_ctrl_iface_pr_pasn_start(wpa_s, buf + 14) < 0)
14567 			reply_len = -1;
14568 	} else if (os_strncmp(buf, "PR_SET_DIK_CONTEXT ", 19) == 0) {
14569 		if (wpas_ctrl_iface_pr_set_dik_ctx(wpa_s, buf + 19) < 0)
14570 			reply_len = -1;
14571 	} else if (os_strcmp(buf, "PR_CLEAR_DIK_CONTEXT") == 0) {
14572 		wpas_pr_clear_dev_iks(wpa_s);
14573 	} else if (os_strcmp(buf, "PR_RANGING_ABORT") == 0) {
14574 		wpas_pr_abort_ranging(wpa_s);
14575 #endif /* CONFIG_PR */
14576 #ifdef CONFIG_TESTING_OPTIONS
14577 	} else if (os_strncmp(buf, "PASN_DRIVER ", 12) == 0) {
14578 		if (wpas_ctrl_iface_pasn_driver(wpa_s, buf + 12) < 0)
14579 			reply_len = -1;
14580 #endif /* CONFIG_TESTING_OPTIONS */
14581 #endif /* CONFIG_PASN */
14582 #ifndef CONFIG_NO_ROBUST_AV
14583 	} else if (os_strncmp(buf, "MSCS ", 5) == 0) {
14584 		if (wpas_ctrl_iface_configure_mscs(wpa_s, buf + 5))
14585 			reply_len = -1;
14586 	} else if (os_strncmp(buf, "SCS ", 4) == 0) {
14587 		if (wpas_ctrl_iface_configure_scs(wpa_s, buf + 4))
14588 			reply_len = -1;
14589 	} else if (os_strncmp(buf, "DSCP_RESP ", 10) == 0) {
14590 		if (wpas_ctrl_iface_send_dscp_resp(wpa_s, buf + 10))
14591 			reply_len = -1;
14592 	} else if (os_strncmp(buf, "DSCP_QUERY ", 11) == 0) {
14593 		if (wpas_ctrl_iface_send_dscp_query(wpa_s, buf + 11))
14594 			reply_len = -1;
14595 #endif /* CONFIG_NO_ROBUST_AV */
14596 	} else if (os_strncmp(buf, "SETUP_LINK_RECONFIG", 19) == 0) {
14597 		if (wpa_supplicant_ctrl_iface_setup_link_reconfig(wpa_s,
14598 								  buf + 19) < 0)
14599 			reply_len = -1;
14600 	} else if (os_strcmp(buf, "MLO_STATUS") == 0) {
14601 		reply_len = wpas_ctrl_iface_mlo_status(wpa_s, reply,
14602 						       reply_size);
14603 	} else if (os_strcmp(buf, "MLO_SIGNAL_POLL") == 0) {
14604 		reply_len = wpas_ctrl_iface_mlo_signal_poll(wpa_s, reply,
14605 							    reply_size);
14606 	} else if (os_strcmp(buf, "NEW_RANDOM_MAC_ADDRESS") == 0) {
14607 		enum wpas_mac_addr_style mac_addr_style =
14608 			wpa_s->conf->preassoc_mac_addr;
14609 
14610 		wpa_s->conf->preassoc_mac_addr = WPAS_MAC_ADDR_STYLE_RANDOM;
14611 		if (wpas_update_random_addr_disassoc(wpa_s) != 1)
14612 			reply_len = -1;
14613 		wpa_s->conf->preassoc_mac_addr = mac_addr_style;
14614 #ifdef CONFIG_NAN
14615 	} else if (os_strncmp(buf, "NAN_START", 9) == 0) {
14616 		if (wpas_nan_start(wpa_s) < 0)
14617 			reply_len = -1;
14618 	} else if (os_strncmp(buf, "NAN_STOP", 8) == 0) {
14619 		if (wpas_nan_stop(wpa_s) < 0)
14620 			reply_len = -1;
14621 	} else if (os_strncmp(buf, "NAN_SET ", 8) == 0) {
14622 		if (wpas_nan_set(wpa_s, buf + 8) < 0)
14623 			reply_len = -1;
14624 	} else if (os_strncmp(buf, "NAN_UPDATE_CONF", 15) == 0) {
14625 		if (wpas_nan_update_conf(wpa_s) < 0)
14626 			reply_len = -1;
14627 	} else if (os_strncmp(buf, "NAN_SCHED_CONFIG_MAP ", 21) == 0) {
14628 		if (wpas_nan_sched_config_map(wpa_s, buf + 21) < 0)
14629 			reply_len = -1;
14630 	} else if (os_strncmp(buf, "NAN_NDP_REQUEST ", 16) == 0) {
14631 		if (wpas_nan_ndp_request(wpa_s, buf + 16) < 0)
14632 			reply_len = -1;
14633 	} else if (os_strncmp(buf, "NAN_NDP_RESPONSE ", 17) == 0) {
14634 		if (wpas_nan_ndp_response(wpa_s, buf + 17) < 0)
14635 			reply_len = -1;
14636 	} else if (os_strncmp(buf, "NAN_NDP_TERMINATE ", 18) == 0) {
14637 		if (wpas_nan_ndp_terminate(wpa_s, buf + 18) < 0)
14638 			reply_len = -1;
14639 	} else if (os_strncmp(buf, "NAN_PEER_INFO ", 14) == 0) {
14640 		reply_len = wpas_nan_peer_info(wpa_s, buf + 14, reply,
14641 					       reply_size);
14642 	} else if (os_strcmp(buf, "NAN_STATUS") == 0) {
14643 		reply_len = wpas_nan_status(wpa_s, reply, reply_size);
14644 	} else if (os_strncmp(buf, "NAN_BOOTSTRAP ", 14) == 0) {
14645 		if (wpas_nan_bootstrap_request(wpa_s, buf + 14) < 0)
14646 			reply_len = -1;
14647 	} else if (os_strncmp(buf, "NAN_BOOTSTRAP_RESET ", 20) == 0) {
14648 		if (wpas_nan_bootstrap_reset(wpa_s, buf + 20) < 0)
14649 			reply_len = -1;
14650 #ifdef CONFIG_PASN
14651 	} else if (os_strncmp(buf, "NAN_PAIR ", 9) == 0) {
14652 		if (wpas_nan_pairing_start(wpa_s, buf + 9) < 0)
14653 			reply_len = -1;
14654 	} else if (os_strncmp(buf, "NAN_PAIR_ABORT ", 15) == 0) {
14655 		if (wpas_nan_pairing_abort(wpa_s, buf + 15) < 0)
14656 			reply_len = -1;
14657 #endif /* CONFIG_PASN */
14658 #endif /* CONFIG_NAN */
14659 	} else {
14660 		os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
14661 		reply_len = 16;
14662 	}
14663 
14664 	if (reply_len < 0) {
14665 		os_memcpy(reply, "FAIL\n", 5);
14666 		reply_len = 5;
14667 	}
14668 
14669 	*resp_len = reply_len;
14670 	return reply;
14671 }
14672 
14673 
wpa_supplicant_global_iface_add(struct wpa_global * global,char * cmd)14674 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
14675 					   char *cmd)
14676 {
14677 	struct wpa_interface iface;
14678 	char *pos, *extra;
14679 	struct wpa_supplicant *wpa_s;
14680 	unsigned int create_iface = 0;
14681 	u8 mac_addr[ETH_ALEN];
14682 	const u8 *addr_ptr = NULL;
14683 	enum wpa_driver_if_type type = WPA_IF_STATION;
14684 
14685 	/*
14686 	 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
14687 	 * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>][TAB<addr>]]
14688 	 */
14689 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
14690 
14691 	os_memset(&iface, 0, sizeof(iface));
14692 
14693 	do {
14694 		iface.ifname = pos = cmd;
14695 		pos = os_strchr(pos, '\t');
14696 		if (pos)
14697 			*pos++ = '\0';
14698 		if (iface.ifname[0] == '\0')
14699 			return -1;
14700 		if (pos == NULL)
14701 			break;
14702 
14703 		iface.confname = pos;
14704 		pos = os_strchr(pos, '\t');
14705 		if (pos)
14706 			*pos++ = '\0';
14707 		if (iface.confname[0] == '\0')
14708 			iface.confname = NULL;
14709 		if (pos == NULL)
14710 			break;
14711 
14712 		iface.driver = pos;
14713 		pos = os_strchr(pos, '\t');
14714 		if (pos)
14715 			*pos++ = '\0';
14716 		if (iface.driver[0] == '\0')
14717 			iface.driver = NULL;
14718 		if (pos == NULL)
14719 			break;
14720 
14721 		iface.ctrl_interface = pos;
14722 		pos = os_strchr(pos, '\t');
14723 		if (pos)
14724 			*pos++ = '\0';
14725 		if (iface.ctrl_interface[0] == '\0')
14726 			iface.ctrl_interface = NULL;
14727 		if (pos == NULL)
14728 			break;
14729 
14730 		iface.driver_param = pos;
14731 		pos = os_strchr(pos, '\t');
14732 		if (pos)
14733 			*pos++ = '\0';
14734 		if (iface.driver_param[0] == '\0')
14735 			iface.driver_param = NULL;
14736 		if (pos == NULL)
14737 			break;
14738 
14739 		iface.bridge_ifname = pos;
14740 		pos = os_strchr(pos, '\t');
14741 		if (pos)
14742 			*pos++ = '\0';
14743 		if (iface.bridge_ifname[0] == '\0')
14744 			iface.bridge_ifname = NULL;
14745 		if (pos == NULL)
14746 			break;
14747 
14748 		extra = pos;
14749 		pos = os_strchr(pos, '\t');
14750 		if (pos)
14751 			*pos++ = '\0';
14752 		if (!extra[0])
14753 			break;
14754 
14755 		if (os_strcmp(extra, "create") == 0) {
14756 			create_iface = 1;
14757 			if (!pos)
14758 				break;
14759 
14760 			extra = os_strchr(pos, '\t');
14761 			if (extra)
14762 				*extra++ = '\0';
14763 
14764 			if (os_strcmp(pos, "sta") == 0) {
14765 				type = WPA_IF_STATION;
14766 			} else if (os_strcmp(pos, "ap") == 0) {
14767 				type = WPA_IF_AP_BSS;
14768 			} else if (os_strcmp(pos, "nan") == 0) {
14769 				type = WPA_IF_NAN;
14770 				iface.nan_mgmt = true;
14771 			} else if (os_strcmp(pos, "nan_data") == 0) {
14772 				type = WPA_IF_NAN_DATA;
14773 				iface.nan_data = true;
14774 			} else {
14775 				wpa_printf(MSG_DEBUG,
14776 					   "INTERFACE_ADD unsupported interface type: '%s'",
14777 					   pos);
14778 				return -1;
14779 			}
14780 
14781 			pos = extra;
14782 			if (!pos || !pos[0])
14783 				break;
14784 
14785 			if (hwaddr_aton(pos, mac_addr)) {
14786 				wpa_printf(MSG_DEBUG,
14787 					   "INTERFACE_ADD invalid MAC address: '%s'",
14788 					   pos);
14789 				return -1;
14790 			}
14791 
14792 			addr_ptr = mac_addr;
14793 		} else {
14794 			wpa_printf(MSG_DEBUG,
14795 				   "INTERFACE_ADD unsupported extra parameter: '%s'",
14796 				   extra);
14797 			return -1;
14798 		}
14799 	} while (0);
14800 
14801 	if (create_iface) {
14802 		wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
14803 			   iface.ifname);
14804 		if (!global->ifaces)
14805 			return -1;
14806 		if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
14807 				   addr_ptr, NULL, NULL, mac_addr, NULL) < 0) {
14808 			wpa_printf(MSG_ERROR,
14809 				   "CTRL_IFACE interface creation failed");
14810 			return -1;
14811 		}
14812 
14813 		wpa_printf(MSG_DEBUG,
14814 			   "CTRL_IFACE interface '%s' created with MAC addr: "
14815 			   MACSTR, iface.ifname, MAC2STR(mac_addr));
14816 	}
14817 
14818 	if (wpa_supplicant_get_iface(global, iface.ifname))
14819 		goto fail;
14820 
14821 	wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
14822 	if (!wpa_s)
14823 		goto fail;
14824 	wpa_s->added_vif = create_iface;
14825 	return 0;
14826 
14827 fail:
14828 	if (create_iface) {
14829 		/* wpa_supplicant does not create multi-BSS AP, so collapse to
14830 		 * WPA_IF_STATION to avoid unwanted clean up in the driver. */
14831 		wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
14832 	}
14833 	return -1;
14834 }
14835 
14836 
wpa_supplicant_global_iface_remove(struct wpa_global * global,char * cmd)14837 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
14838 					      char *cmd)
14839 {
14840 	struct wpa_supplicant *wpa_s;
14841 	int ret;
14842 	unsigned int delete_iface;
14843 
14844 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
14845 
14846 	wpa_s = wpa_supplicant_get_iface(global, cmd);
14847 	if (wpa_s == NULL)
14848 		return -1;
14849 	delete_iface = wpa_s->added_vif;
14850 	ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
14851 	if (!ret && delete_iface) {
14852 		wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
14853 			   cmd);
14854 		/* wpa_supplicant does not create multi-BSS AP, so collapse to
14855 		 * WPA_IF_STATION to avoid unwanted clean up in the driver. */
14856 		ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
14857 	}
14858 	return ret;
14859 }
14860 
14861 
wpa_free_iface_info(struct wpa_interface_info * iface)14862 static void wpa_free_iface_info(struct wpa_interface_info *iface)
14863 {
14864 	struct wpa_interface_info *prev;
14865 
14866 	while (iface) {
14867 		prev = iface;
14868 		iface = iface->next;
14869 
14870 		os_free(prev->ifname);
14871 		os_free(prev->desc);
14872 		os_free(prev);
14873 	}
14874 }
14875 
14876 
wpa_supplicant_global_iface_list(struct wpa_global * global,char * buf,int len)14877 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
14878 					    char *buf, int len)
14879 {
14880 	int i, res;
14881 	struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
14882 	char *pos, *end;
14883 
14884 	for (i = 0; wpa_drivers[i]; i++) {
14885 		const struct wpa_driver_ops *drv = wpa_drivers[i];
14886 		if (drv->get_interfaces == NULL)
14887 			continue;
14888 		tmp = drv->get_interfaces(global->drv_priv[i]);
14889 		if (tmp == NULL)
14890 			continue;
14891 
14892 		if (last == NULL)
14893 			iface = last = tmp;
14894 		else
14895 			last->next = tmp;
14896 		while (last->next)
14897 			last = last->next;
14898 	}
14899 
14900 	pos = buf;
14901 	end = buf + len;
14902 	for (tmp = iface; tmp; tmp = tmp->next) {
14903 		res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
14904 				  tmp->drv_name, tmp->ifname,
14905 				  tmp->desc ? tmp->desc : "");
14906 		if (os_snprintf_error(end - pos, res)) {
14907 			*pos = '\0';
14908 			break;
14909 		}
14910 		pos += res;
14911 	}
14912 
14913 	wpa_free_iface_info(iface);
14914 
14915 	return pos - buf;
14916 }
14917 
14918 
wpa_supplicant_global_iface_interfaces(struct wpa_global * global,const char * input,char * buf,int len)14919 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
14920 						  const char *input,
14921 						  char *buf, int len)
14922 {
14923 	int res;
14924 	char *pos, *end;
14925 	struct wpa_supplicant *wpa_s;
14926 	int show_ctrl = 0;
14927 
14928 	if (input)
14929 		show_ctrl = !!os_strstr(input, "ctrl");
14930 
14931 	wpa_s = global->ifaces;
14932 	pos = buf;
14933 	end = buf + len;
14934 
14935 	while (wpa_s) {
14936 		if (show_ctrl)
14937 			res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
14938 					  wpa_s->ifname,
14939 					  wpa_s->conf->ctrl_interface ?
14940 					  wpa_s->conf->ctrl_interface : "N/A");
14941 		else
14942 			res = os_snprintf(pos, end - pos, "%s\n",
14943 					  wpa_s->ifname);
14944 
14945 		if (os_snprintf_error(end - pos, res)) {
14946 			*pos = '\0';
14947 			break;
14948 		}
14949 		pos += res;
14950 		wpa_s = wpa_s->next;
14951 	}
14952 	return pos - buf;
14953 }
14954 
14955 
wpas_global_ctrl_iface_ifname(struct wpa_global * global,const char * ifname,char * cmd,size_t * resp_len)14956 static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
14957 					    const char *ifname,
14958 					    char *cmd, size_t *resp_len)
14959 {
14960 	struct wpa_supplicant *wpa_s;
14961 
14962 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
14963 		if (os_strcmp(ifname, wpa_s->ifname) == 0)
14964 			break;
14965 	}
14966 
14967 	if (wpa_s == NULL) {
14968 		char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
14969 		if (resp)
14970 			*resp_len = os_strlen(resp);
14971 		else
14972 			*resp_len = 1;
14973 		return resp;
14974 	}
14975 
14976 	return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
14977 }
14978 
14979 
wpas_global_ctrl_iface_redir_p2p(struct wpa_global * global,char * buf,size_t * resp_len)14980 static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
14981 					       char *buf, size_t *resp_len)
14982 {
14983 #ifdef CONFIG_P2P
14984 	static const char * cmd[] = {
14985 		"LIST_NETWORKS",
14986 		"P2P_FIND",
14987 		"P2P_STOP_FIND",
14988 		"P2P_LISTEN",
14989 		"P2P_GROUP_ADD",
14990 		"P2P_GET_PASSPHRASE",
14991 		"P2P_SERVICE_UPDATE",
14992 		"P2P_SERVICE_FLUSH",
14993 		"P2P_FLUSH",
14994 		"P2P_CANCEL",
14995 		"P2P_PRESENCE_REQ",
14996 		"P2P_EXT_LISTEN",
14997 #ifdef CONFIG_AP
14998 		"STA-FIRST",
14999 #endif /* CONFIG_AP */
15000 		"P2P_GET_DIRA",
15001 		"P2P_VALIDATE_DIRA",
15002 		NULL
15003 	};
15004 	static const char * prefix[] = {
15005 #ifdef ANDROID
15006 		"DRIVER ",
15007 #endif /* ANDROID */
15008 		"GET_CAPABILITY ",
15009 		"GET_NETWORK ",
15010 		"REMOVE_NETWORK ",
15011 		"P2P_FIND ",
15012 		"P2P_CONNECT ",
15013 		"P2P_LISTEN ",
15014 		"P2P_GROUP_REMOVE ",
15015 		"P2P_GROUP_ADD ",
15016 		"P2P_GROUP_MEMBER ",
15017 		"P2P_PROV_DISC ",
15018 		"P2P_SERV_DISC_REQ ",
15019 		"P2P_SERV_DISC_CANCEL_REQ ",
15020 		"P2P_SERV_DISC_RESP ",
15021 		"P2P_SERV_DISC_EXTERNAL ",
15022 		"P2P_SERVICE_ADD ",
15023 		"P2P_SERVICE_DEL ",
15024 		"P2P_SERVICE_REP ",
15025 		"P2P_REJECT ",
15026 		"P2P_INVITE ",
15027 		"P2P_PEER ",
15028 		"P2P_SET ",
15029 		"P2P_UNAUTHORIZE ",
15030 		"P2P_PRESENCE_REQ ",
15031 		"P2P_EXT_LISTEN ",
15032 		"P2P_REMOVE_CLIENT ",
15033 		"WPS_NFC_TOKEN ",
15034 		"WPS_NFC_TAG_READ ",
15035 		"NFC_GET_HANDOVER_SEL ",
15036 		"NFC_GET_HANDOVER_REQ ",
15037 		"NFC_REPORT_HANDOVER ",
15038 		"P2P_ASP_PROVISION ",
15039 		"P2P_ASP_PROVISION_RESP ",
15040 		"NAN_",
15041 #ifdef CONFIG_AP
15042 		"STA ",
15043 		"STA-NEXT ",
15044 #endif /* CONFIG_AP */
15045 		NULL
15046 	};
15047 	int found = 0;
15048 	int i;
15049 
15050 	if (global->p2p_init_wpa_s == NULL)
15051 		return NULL;
15052 
15053 	for (i = 0; !found && cmd[i]; i++) {
15054 		if (os_strcmp(buf, cmd[i]) == 0)
15055 			found = 1;
15056 	}
15057 
15058 	for (i = 0; !found && prefix[i]; i++) {
15059 		if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
15060 			found = 1;
15061 	}
15062 
15063 	if (found)
15064 		return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
15065 							 buf, resp_len);
15066 #endif /* CONFIG_P2P */
15067 	return NULL;
15068 }
15069 
15070 
wpas_global_ctrl_iface_redir_wfd(struct wpa_global * global,char * buf,size_t * resp_len)15071 static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
15072 					       char *buf, size_t *resp_len)
15073 {
15074 #ifdef CONFIG_WIFI_DISPLAY
15075 	if (global->p2p_init_wpa_s == NULL)
15076 		return NULL;
15077 	if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
15078 	    os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
15079 		return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
15080 							 buf, resp_len);
15081 #endif /* CONFIG_WIFI_DISPLAY */
15082 	return NULL;
15083 }
15084 
15085 
wpas_global_ctrl_iface_redir(struct wpa_global * global,char * buf,size_t * resp_len)15086 static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
15087 					   char *buf, size_t *resp_len)
15088 {
15089 	char *ret;
15090 
15091 	ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
15092 	if (ret)
15093 		return ret;
15094 
15095 	ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
15096 	if (ret)
15097 		return ret;
15098 
15099 	return NULL;
15100 }
15101 
15102 
wpas_global_ctrl_iface_set(struct wpa_global * global,char * cmd)15103 static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
15104 {
15105 	char *value;
15106 
15107 	value = os_strchr(cmd, ' ');
15108 	if (value == NULL)
15109 		return -1;
15110 	*value++ = '\0';
15111 
15112 	wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
15113 
15114 #ifdef CONFIG_WIFI_DISPLAY
15115 	if (os_strcasecmp(cmd, "wifi_display") == 0) {
15116 		wifi_display_enable(global, !!atoi(value));
15117 		return 0;
15118 	}
15119 #endif /* CONFIG_WIFI_DISPLAY */
15120 
15121 	/* Restore cmd to its original value to allow redirection */
15122 	value[-1] = ' ';
15123 
15124 	return -1;
15125 }
15126 
15127 
wpas_global_ctrl_iface_dup_network(struct wpa_global * global,char * cmd)15128 static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
15129 					      char *cmd)
15130 {
15131 	struct wpa_supplicant *wpa_s[2]; /* src, dst */
15132 	char *p;
15133 	unsigned int i;
15134 
15135 	/* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
15136 	 * <variable name> */
15137 
15138 	for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
15139 		p = os_strchr(cmd, ' ');
15140 		if (p == NULL)
15141 			return -1;
15142 		*p = '\0';
15143 
15144 		wpa_s[i] = global->ifaces;
15145 		for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
15146 			if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
15147 				break;
15148 		}
15149 
15150 		if (!wpa_s[i]) {
15151 			wpa_printf(MSG_DEBUG,
15152 				   "CTRL_IFACE: Could not find iface=%s", cmd);
15153 			return -1;
15154 		}
15155 
15156 		cmd = p + 1;
15157 	}
15158 
15159 	return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
15160 }
15161 
15162 
15163 #ifndef CONFIG_NO_CONFIG_WRITE
wpas_global_ctrl_iface_save_config(struct wpa_global * global)15164 static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
15165 {
15166 	int ret = 0, saved = 0;
15167 	struct wpa_supplicant *wpa_s;
15168 
15169 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
15170 		if (!wpa_s->conf->update_config) {
15171 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
15172 			continue;
15173 		}
15174 
15175 		if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
15176 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
15177 			ret = 1;
15178 		} else {
15179 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
15180 			saved++;
15181 		}
15182 	}
15183 
15184 	if (!saved && !ret) {
15185 		wpa_dbg(wpa_s, MSG_DEBUG,
15186 			"CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
15187 		ret = 1;
15188 	}
15189 
15190 	return ret;
15191 }
15192 #endif /* CONFIG_NO_CONFIG_WRITE */
15193 
15194 
wpas_global_ctrl_iface_status(struct wpa_global * global,char * buf,size_t buflen)15195 static int wpas_global_ctrl_iface_status(struct wpa_global *global,
15196 					 char *buf, size_t buflen)
15197 {
15198 	char *pos, *end;
15199 	int ret;
15200 	struct wpa_supplicant *wpa_s;
15201 
15202 	pos = buf;
15203 	end = buf + buflen;
15204 
15205 #ifdef CONFIG_P2P
15206 	if (global->p2p && !global->p2p_disabled) {
15207 		ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
15208 				  "\n"
15209 				  "p2p_state=%s\n",
15210 				  MAC2STR(global->p2p_dev_addr),
15211 				  p2p_get_state_txt(global->p2p));
15212 		if (os_snprintf_error(end - pos, ret))
15213 			return pos - buf;
15214 		pos += ret;
15215 	} else if (global->p2p) {
15216 		ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
15217 		if (os_snprintf_error(end - pos, ret))
15218 			return pos - buf;
15219 		pos += ret;
15220 	}
15221 #endif /* CONFIG_P2P */
15222 
15223 #ifdef CONFIG_WIFI_DISPLAY
15224 	ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
15225 			  !!global->wifi_display);
15226 	if (os_snprintf_error(end - pos, ret))
15227 		return pos - buf;
15228 	pos += ret;
15229 #endif /* CONFIG_WIFI_DISPLAY */
15230 
15231 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
15232 		ret = os_snprintf(pos, end - pos, "ifname=%s\n"
15233 				  "address=" MACSTR "\n",
15234 				  wpa_s->ifname, MAC2STR(wpa_s->own_addr));
15235 		if (os_snprintf_error(end - pos, ret))
15236 			return pos - buf;
15237 		pos += ret;
15238 	}
15239 
15240 	return pos - buf;
15241 }
15242 
15243 
15244 #ifdef CONFIG_FST
15245 
wpas_global_ctrl_iface_fst_attach(struct wpa_global * global,char * cmd,char * buf,size_t reply_size)15246 static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
15247 					     char *cmd, char *buf,
15248 					     size_t reply_size)
15249 {
15250 	char ifname[IFNAMSIZ + 1];
15251 	struct fst_iface_cfg cfg;
15252 	struct wpa_supplicant *wpa_s;
15253 	struct fst_wpa_obj iface_obj;
15254 
15255 	if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
15256 		wpa_s = wpa_supplicant_get_iface(global, ifname);
15257 		if (wpa_s) {
15258 			if (wpa_s->fst) {
15259 				wpa_printf(MSG_INFO, "FST: Already attached");
15260 				return -1;
15261 			}
15262 			fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
15263 			wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
15264 						&iface_obj, &cfg);
15265 			if (wpa_s->fst)
15266 				return os_snprintf(buf, reply_size, "OK\n");
15267 		}
15268 	}
15269 
15270 	return -1;
15271 }
15272 
15273 
wpas_global_ctrl_iface_fst_detach(struct wpa_global * global,char * cmd,char * buf,size_t reply_size)15274 static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
15275 					     char *cmd, char *buf,
15276 					     size_t reply_size)
15277 {
15278 	char ifname[IFNAMSIZ + 1];
15279 	struct wpa_supplicant *wpa_s;
15280 
15281 	if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
15282 		wpa_s = wpa_supplicant_get_iface(global, ifname);
15283 		if (wpa_s) {
15284 			if (!fst_iface_detach(ifname)) {
15285 				wpa_s->fst = NULL;
15286 				return os_snprintf(buf, reply_size, "OK\n");
15287 			}
15288 		}
15289 	}
15290 
15291 	return -1;
15292 }
15293 
15294 #endif /* CONFIG_FST */
15295 
15296 
wpa_supplicant_global_ctrl_iface_process(struct wpa_global * global,char * buf,size_t * resp_len)15297 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
15298 						char *buf, size_t *resp_len)
15299 {
15300 	char *reply;
15301 	const int reply_size = 2048;
15302 	int reply_len;
15303 	int level = MSG_DEBUG;
15304 
15305 	if (os_strncmp(buf, "IFNAME=", 7) == 0) {
15306 		char *pos = os_strchr(buf + 7, ' ');
15307 		if (pos) {
15308 			*pos++ = '\0';
15309 			return wpas_global_ctrl_iface_ifname(global,
15310 							     buf + 7, pos,
15311 							     resp_len);
15312 		}
15313 	}
15314 
15315 	reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
15316 	if (reply)
15317 		return reply;
15318 
15319 	if (os_strcmp(buf, "PING") == 0)
15320 		level = MSG_EXCESSIVE;
15321 	wpa_hexdump_ascii(level, "RX global ctrl_iface",
15322 			  (const u8 *) buf, os_strlen(buf));
15323 
15324 	reply = os_malloc(reply_size);
15325 	if (reply == NULL) {
15326 		*resp_len = 1;
15327 		return NULL;
15328 	}
15329 
15330 	os_memcpy(reply, "OK\n", 3);
15331 	reply_len = 3;
15332 
15333 	if (os_strcmp(buf, "PING") == 0) {
15334 		os_memcpy(reply, "PONG\n", 5);
15335 		reply_len = 5;
15336 	} else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
15337 		if (wpa_supplicant_global_iface_add(global, buf + 14))
15338 			reply_len = -1;
15339 	} else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
15340 		if (wpa_supplicant_global_iface_remove(global, buf + 17))
15341 			reply_len = -1;
15342 	} else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
15343 		reply_len = wpa_supplicant_global_iface_list(
15344 			global, reply, reply_size);
15345 	} else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
15346 		reply_len = wpa_supplicant_global_iface_interfaces(
15347 			global, buf + 10, reply, reply_size);
15348 #ifdef CONFIG_FST
15349 	} else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
15350 		reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
15351 							      reply,
15352 							      reply_size);
15353 	} else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
15354 		reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
15355 							      reply,
15356 							      reply_size);
15357 	} else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
15358 		reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
15359 #endif /* CONFIG_FST */
15360 	} else if (os_strcmp(buf, "TERMINATE") == 0) {
15361 		wpa_supplicant_terminate_proc(global);
15362 	} else if (os_strcmp(buf, "SUSPEND") == 0) {
15363 		wpas_notify_suspend(global);
15364 	} else if (os_strcmp(buf, "RESUME") == 0) {
15365 		wpas_notify_resume(global);
15366 	} else if (os_strncmp(buf, "SET ", 4) == 0) {
15367 		if (wpas_global_ctrl_iface_set(global, buf + 4)) {
15368 #ifdef CONFIG_P2P
15369 			if (global->p2p_init_wpa_s) {
15370 				os_free(reply);
15371 				/* Check if P2P redirection would work for this
15372 				 * command. */
15373 				return wpa_supplicant_ctrl_iface_process(
15374 					global->p2p_init_wpa_s,
15375 					buf, resp_len);
15376 			}
15377 #endif /* CONFIG_P2P */
15378 			reply_len = -1;
15379 		}
15380 	} else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
15381 		if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
15382 			reply_len = -1;
15383 #ifndef CONFIG_NO_CONFIG_WRITE
15384 	} else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
15385 		if (wpas_global_ctrl_iface_save_config(global))
15386 			reply_len = -1;
15387 #endif /* CONFIG_NO_CONFIG_WRITE */
15388 	} else if (os_strcmp(buf, "STATUS") == 0) {
15389 		reply_len = wpas_global_ctrl_iface_status(global, reply,
15390 							  reply_size);
15391 #ifdef CONFIG_MODULE_TESTS
15392 	} else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
15393 		if (wpas_module_tests() < 0)
15394 			reply_len = -1;
15395 #endif /* CONFIG_MODULE_TESTS */
15396 	} else if (os_strncmp(buf, "RELOG", 5) == 0) {
15397 		if (wpa_debug_reopen_file() < 0)
15398 			reply_len = -1;
15399 	} else if (os_strncmp(buf, "NOTE ", 5) == 0) {
15400 		wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
15401 		wpa_trace_set_context(buf + 5);
15402 	} else {
15403 		os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
15404 		reply_len = 16;
15405 	}
15406 
15407 	if (reply_len < 0) {
15408 		os_memcpy(reply, "FAIL\n", 5);
15409 		reply_len = 5;
15410 	}
15411 
15412 	*resp_len = reply_len;
15413 	return reply;
15414 }
15415