xref: /aosp_15_r20/external/wpa_supplicant_8/wpa_supplicant/ctrl_iface.c (revision 03f9172ca588f91df233974f4258bab95191f931)
1 /*
2  * WPA Supplicant / Control interface (shared code for all backends)
3  * Copyright (c) 2004-2024, Jouni Malinen <[email protected]>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 #ifdef CONFIG_TESTING_OPTIONS
11 #include <netinet/ip.h>
12 #endif /* CONFIG_TESTING_OPTIONS */
13 
14 #include "utils/common.h"
15 #include "utils/eloop.h"
16 #include "utils/uuid.h"
17 #include "utils/module_tests.h"
18 #include "common/version.h"
19 #include "common/ieee802_11_defs.h"
20 #include "common/ieee802_11_common.h"
21 #include "common/wpa_ctrl.h"
22 #ifdef CONFIG_DPP
23 #include "common/dpp.h"
24 #endif /* CONFIG_DPP */
25 #include "common/nan_de.h"
26 #include "common/ptksa_cache.h"
27 #include "crypto/tls.h"
28 #include "ap/hostapd.h"
29 #include "eap_peer/eap.h"
30 #include "eapol_supp/eapol_supp_sm.h"
31 #include "rsn_supp/wpa.h"
32 #include "rsn_supp/preauth.h"
33 #include "rsn_supp/pmksa_cache.h"
34 #include "l2_packet/l2_packet.h"
35 #include "wps/wps.h"
36 #include "fst/fst.h"
37 #include "fst/fst_ctrl_iface.h"
38 #include "config.h"
39 #include "wpa_supplicant_i.h"
40 #include "driver_i.h"
41 #include "wps_supplicant.h"
42 #include "ibss_rsn.h"
43 #include "wpas_glue.h"
44 #include "ap.h"
45 #include "p2p_supplicant.h"
46 #include "p2p/p2p.h"
47 #include "hs20_supplicant.h"
48 #include "wifi_display.h"
49 #include "notify.h"
50 #include "bss.h"
51 #include "scan.h"
52 #include "ctrl_iface.h"
53 #include "interworking.h"
54 #include "bssid_ignore.h"
55 #include "autoscan.h"
56 #include "wnm_sta.h"
57 #include "offchannel.h"
58 #include "drivers/driver.h"
59 #include "mesh.h"
60 #include "dpp_supplicant.h"
61 #include "sme.h"
62 #include "nan_usd.h"
63 
64 #ifdef __NetBSD__
65 #include <net/if_ether.h>
66 #elif !defined(__CYGWIN__) && !defined(CONFIG_NATIVE_WINDOWS)
67 #include <net/ethernet.h>
68 #endif
69 
70 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
71 					    char *buf, int len);
72 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
73 						  const char *input,
74 						  char *buf, int len);
75 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
76 					char *val);
77 
78 
set_bssid_filter(struct wpa_supplicant * wpa_s,char * val)79 static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
80 {
81 	char *pos;
82 	u8 addr[ETH_ALEN], *filter = NULL, *n;
83 	size_t count = 0;
84 
85 	pos = val;
86 	while (pos) {
87 		if (*pos == '\0')
88 			break;
89 		if (hwaddr_aton(pos, addr)) {
90 			os_free(filter);
91 			return -1;
92 		}
93 		n = os_realloc_array(filter, count + 1, ETH_ALEN);
94 		if (n == NULL) {
95 			os_free(filter);
96 			return -1;
97 		}
98 		filter = n;
99 		os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
100 		count++;
101 
102 		pos = os_strchr(pos, ' ');
103 		if (pos)
104 			pos++;
105 	}
106 
107 	wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
108 	os_free(wpa_s->bssid_filter);
109 	wpa_s->bssid_filter = filter;
110 	wpa_s->bssid_filter_count = count;
111 
112 	return 0;
113 }
114 
115 
set_disallow_aps(struct wpa_supplicant * wpa_s,char * val)116 static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
117 {
118 	char *pos;
119 	u8 addr[ETH_ALEN], *bssid = NULL, *n;
120 	struct wpa_ssid_value *ssid = NULL, *ns;
121 	size_t count = 0, ssid_count = 0;
122 	struct wpa_ssid *c;
123 
124 	/*
125 	 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
126 	 * SSID_SPEC ::= ssid <SSID_HEX>
127 	 * BSSID_SPEC ::= bssid <BSSID_HEX>
128 	 */
129 
130 	pos = val;
131 	while (pos) {
132 		if (*pos == '\0')
133 			break;
134 		if (os_strncmp(pos, "bssid ", 6) == 0) {
135 			int res;
136 			pos += 6;
137 			res = hwaddr_aton2(pos, addr);
138 			if (res < 0) {
139 				os_free(ssid);
140 				os_free(bssid);
141 				wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
142 					   "BSSID value '%s'", pos);
143 				return -1;
144 			}
145 			pos += res;
146 			n = os_realloc_array(bssid, count + 1, ETH_ALEN);
147 			if (n == NULL) {
148 				os_free(ssid);
149 				os_free(bssid);
150 				return -1;
151 			}
152 			bssid = n;
153 			os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
154 			count++;
155 		} else if (os_strncmp(pos, "ssid ", 5) == 0) {
156 			char *end;
157 			pos += 5;
158 
159 			end = pos;
160 			while (*end) {
161 				if (*end == '\0' || *end == ' ')
162 					break;
163 				end++;
164 			}
165 
166 			ns = os_realloc_array(ssid, ssid_count + 1,
167 					      sizeof(struct wpa_ssid_value));
168 			if (ns == NULL) {
169 				os_free(ssid);
170 				os_free(bssid);
171 				return -1;
172 			}
173 			ssid = ns;
174 
175 			if ((end - pos) & 0x01 ||
176 			    end - pos > 2 * SSID_MAX_LEN ||
177 			    hexstr2bin(pos, ssid[ssid_count].ssid,
178 				       (end - pos) / 2) < 0) {
179 				os_free(ssid);
180 				os_free(bssid);
181 				wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
182 					   "SSID value '%s'", pos);
183 				return -1;
184 			}
185 			ssid[ssid_count].ssid_len = (end - pos) / 2;
186 			wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
187 					  ssid[ssid_count].ssid,
188 					  ssid[ssid_count].ssid_len);
189 			ssid_count++;
190 			pos = end;
191 		} else {
192 			wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
193 				   "'%s'", pos);
194 			os_free(ssid);
195 			os_free(bssid);
196 			return -1;
197 		}
198 
199 		pos = os_strchr(pos, ' ');
200 		if (pos)
201 			pos++;
202 	}
203 
204 	wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
205 	os_free(wpa_s->disallow_aps_bssid);
206 	wpa_s->disallow_aps_bssid = bssid;
207 	wpa_s->disallow_aps_bssid_count = count;
208 
209 	wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
210 	os_free(wpa_s->disallow_aps_ssid);
211 	wpa_s->disallow_aps_ssid = ssid;
212 	wpa_s->disallow_aps_ssid_count = ssid_count;
213 
214 	if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
215 		return 0;
216 
217 	c = wpa_s->current_ssid;
218 	if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
219 		return 0;
220 
221 	if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
222 	    !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
223 		return 0;
224 
225 	wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
226 		   "because current AP was marked disallowed");
227 
228 #ifdef CONFIG_SME
229 	wpa_s->sme.prev_bssid_set = 0;
230 #endif /* CONFIG_SME */
231 	wpa_s->reassociate = 1;
232 	wpa_s->own_disconnect_req = 1;
233 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
234 	wpa_supplicant_req_scan(wpa_s, 0, 0);
235 
236 	return 0;
237 }
238 
239 
240 #ifndef CONFIG_NO_CONFIG_BLOBS
wpas_ctrl_set_blob(struct wpa_supplicant * wpa_s,char * pos)241 static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
242 {
243 	char *name = pos;
244 	struct wpa_config_blob *blob;
245 	size_t len;
246 
247 	pos = os_strchr(pos, ' ');
248 	if (pos == NULL)
249 		return -1;
250 	*pos++ = '\0';
251 	len = os_strlen(pos);
252 	if (len & 1)
253 		return -1;
254 
255 	wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
256 	blob = os_zalloc(sizeof(*blob));
257 	if (blob == NULL)
258 		return -1;
259 	blob->name = os_strdup(name);
260 	blob->data = os_malloc(len / 2);
261 	if (blob->name == NULL || blob->data == NULL) {
262 		wpa_config_free_blob(blob);
263 		return -1;
264 	}
265 
266 	if (hexstr2bin(pos, blob->data, len / 2) < 0) {
267 		wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
268 		wpa_config_free_blob(blob);
269 		return -1;
270 	}
271 	blob->len = len / 2;
272 
273 	wpa_config_set_blob(wpa_s->conf, blob);
274 
275 	return 0;
276 }
277 #endif /* CONFIG_NO_CONFIG_BLOBS */
278 
279 
wpas_ctrl_pno(struct wpa_supplicant * wpa_s,char * cmd)280 static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
281 {
282 	char *params;
283 	char *pos;
284 	int *freqs = NULL;
285 	int ret;
286 
287 	if (atoi(cmd)) {
288 		params = os_strchr(cmd, ' ');
289 		os_free(wpa_s->manual_sched_scan_freqs);
290 		if (params) {
291 			params++;
292 			pos = os_strstr(params, "freq=");
293 			if (pos)
294 				freqs = freq_range_to_channel_list(wpa_s,
295 								   pos + 5);
296 		}
297 		wpa_s->manual_sched_scan_freqs = freqs;
298 		ret = wpas_start_pno(wpa_s);
299 	} else {
300 		ret = wpas_stop_pno(wpa_s);
301 	}
302 	return ret;
303 }
304 
305 
wpas_ctrl_set_band(struct wpa_supplicant * wpa_s,char * bands)306 static int wpas_ctrl_set_band(struct wpa_supplicant *wpa_s, char *bands)
307 {
308 	union wpa_event_data event;
309 	u32 setband_mask = WPA_SETBAND_AUTO;
310 
311 	/*
312 	 * For example:
313 	 *  SET setband 2G,6G
314 	 *  SET setband 5G
315 	 *  SET setband AUTO
316 	 */
317 	if (!os_strstr(bands, "AUTO")) {
318 		if (os_strstr(bands, "5G"))
319 			setband_mask |= WPA_SETBAND_5G;
320 		if (os_strstr(bands, "6G"))
321 			setband_mask |= WPA_SETBAND_6G;
322 		if (os_strstr(bands, "2G"))
323 			setband_mask |= WPA_SETBAND_2G;
324 		if (setband_mask == WPA_SETBAND_AUTO)
325 			return -1;
326 	}
327 
328 	wpa_s->setband_mask = setband_mask;
329 	if (wpa_drv_setband(wpa_s, wpa_s->setband_mask) == 0) {
330 		os_memset(&event, 0, sizeof(event));
331 		event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
332 		event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
333 		wpa_supplicant_event(wpa_s, EVENT_CHANNEL_LIST_CHANGED, &event);
334 	}
335 
336 	return 0;
337 }
338 
339 
wpas_ctrl_iface_set_lci(struct wpa_supplicant * wpa_s,const char * cmd)340 static int wpas_ctrl_iface_set_lci(struct wpa_supplicant *wpa_s,
341 				   const char *cmd)
342 {
343 	struct wpabuf *lci;
344 
345 	if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
346 		wpabuf_free(wpa_s->lci);
347 		wpa_s->lci = NULL;
348 		return 0;
349 	}
350 
351 	lci = wpabuf_parse_bin(cmd);
352 	if (!lci)
353 		return -1;
354 
355 	if (os_get_reltime(&wpa_s->lci_time)) {
356 		wpabuf_free(lci);
357 		return -1;
358 	}
359 
360 	wpabuf_free(wpa_s->lci);
361 	wpa_s->lci = lci;
362 
363 	return 0;
364 }
365 
366 
367 static int
wpas_ctrl_set_relative_rssi(struct wpa_supplicant * wpa_s,const char * cmd)368 wpas_ctrl_set_relative_rssi(struct wpa_supplicant *wpa_s, const char *cmd)
369 {
370 	int relative_rssi;
371 
372 	if (os_strcmp(cmd, "disable") == 0) {
373 		wpa_s->srp.relative_rssi_set = 0;
374 		return 0;
375 	}
376 
377 	relative_rssi = atoi(cmd);
378 	if (relative_rssi < 0 || relative_rssi > 100)
379 		return -1;
380 	wpa_s->srp.relative_rssi = relative_rssi;
381 	wpa_s->srp.relative_rssi_set = 1;
382 	return 0;
383 }
384 
385 
wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant * wpa_s,const char * cmd)386 static int wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant *wpa_s,
387 					      const char *cmd)
388 {
389 	char *pos;
390 	int adjust_rssi;
391 
392 	/* <band>:adjust_value */
393 	pos = os_strchr(cmd, ':');
394 	if (!pos)
395 		return -1;
396 	pos++;
397 	adjust_rssi = atoi(pos);
398 	if (adjust_rssi < -100 || adjust_rssi > 100)
399 		return -1;
400 
401 	if (os_strncmp(cmd, "2G", 2) == 0)
402 		wpa_s->srp.relative_adjust_band = WPA_SETBAND_2G;
403 	else if (os_strncmp(cmd, "5G", 2) == 0)
404 		wpa_s->srp.relative_adjust_band = WPA_SETBAND_5G;
405 	else
406 		return -1;
407 
408 	wpa_s->srp.relative_adjust_rssi = adjust_rssi;
409 
410 	return 0;
411 }
412 
413 
wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant * wpa_s,const char * cmd)414 static int wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant *wpa_s,
415 				   const char *cmd)
416 {
417 	struct wpabuf *ric_ies;
418 
419 	if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
420 		wpabuf_free(wpa_s->ric_ies);
421 		wpa_s->ric_ies = NULL;
422 		return 0;
423 	}
424 
425 	ric_ies = wpabuf_parse_bin(cmd);
426 	if (!ric_ies)
427 		return -1;
428 
429 	wpabuf_free(wpa_s->ric_ies);
430 	wpa_s->ric_ies = ric_ies;
431 
432 	return 0;
433 }
434 
435 
436 #ifdef CONFIG_TESTING_OPTIONS
wpas_ctrl_iface_set_dso(struct wpa_supplicant * wpa_s,const char * val)437 static int wpas_ctrl_iface_set_dso(struct wpa_supplicant *wpa_s,
438 				   const char *val)
439 {
440 	u8 bssid[ETH_ALEN];
441 	const char *pos = val;
442 	struct driver_signal_override *dso = NULL, *tmp, parsed;
443 
444 	if (hwaddr_aton(pos, bssid))
445 		return -1;
446 	pos = os_strchr(pos, ' ');
447 
448 	dl_list_for_each(tmp, &wpa_s->drv_signal_override,
449 			 struct driver_signal_override, list) {
450 		if (ether_addr_equal(bssid, tmp->bssid)) {
451 			dso = tmp;
452 			break;
453 		}
454 	}
455 
456 	if (!pos) {
457 		/* Remove existing entry */
458 		if (dso) {
459 			dl_list_del(&dso->list);
460 			os_free(dso);
461 		}
462 		return 0;
463 	}
464 	pos++;
465 
466 	/* Update an existing entry or add a new one */
467 	os_memset(&parsed, 0, sizeof(parsed));
468 	if (sscanf(pos, "%d %d %d %d %d",
469 		   &parsed.si_current_signal,
470 		   &parsed.si_avg_signal,
471 		   &parsed.si_avg_beacon_signal,
472 		   &parsed.si_current_noise,
473 		   &parsed.scan_level) != 5)
474 		return -1;
475 
476 	if (!dso) {
477 		dso = os_zalloc(sizeof(*dso));
478 		if (!dso)
479 			return -1;
480 		os_memcpy(dso->bssid, bssid, ETH_ALEN);
481 		dl_list_add(&wpa_s->drv_signal_override, &dso->list);
482 	}
483 	dso->si_current_signal = parsed.si_current_signal;
484 	dso->si_avg_signal = parsed.si_avg_signal;
485 	dso->si_avg_beacon_signal = parsed.si_avg_beacon_signal;
486 	dso->si_current_noise = parsed.si_current_noise;
487 	dso->scan_level = parsed.scan_level;
488 
489 	return 0;
490 }
491 #endif /* CONFIG_TESTING_OPTIONS */
492 
493 
wpa_supplicant_ctrl_iface_set(struct wpa_supplicant * wpa_s,char * cmd)494 static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
495 					 char *cmd)
496 {
497 	char *value;
498 	int ret = 0;
499 
500 	value = os_strchr(cmd, ' ');
501 	if (value == NULL)
502 		return -1;
503 	*value++ = '\0';
504 
505 	wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
506 	if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
507 		eapol_sm_configure(wpa_s->eapol,
508 				   atoi(value), -1, -1, -1);
509 	} else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
510 		eapol_sm_configure(wpa_s->eapol,
511 				   -1, atoi(value), -1, -1);
512 	} else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
513 		eapol_sm_configure(wpa_s->eapol,
514 				   -1, -1, atoi(value), -1);
515 	} else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
516 		eapol_sm_configure(wpa_s->eapol,
517 				   -1, -1, -1, atoi(value));
518 #ifdef CONFIG_TESTING_OPTIONS
519 	} else if (os_strcasecmp(cmd, "EAPOL::portControl") == 0) {
520 		if (os_strcmp(value, "Auto") == 0)
521 			eapol_sm_notify_portControl(wpa_s->eapol, Auto);
522 		else if (os_strcmp(value, "ForceUnauthorized") == 0)
523 			eapol_sm_notify_portControl(wpa_s->eapol,
524 						    ForceUnauthorized);
525 		else if (os_strcmp(value, "ForceAuthorized") == 0)
526 			eapol_sm_notify_portControl(wpa_s->eapol,
527 						    ForceAuthorized);
528 		else
529 			ret = -1;
530 #endif /* CONFIG_TESTING_OPTIONS */
531 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
532 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
533 				     atoi(value))) {
534 			ret = -1;
535 		} else {
536 			value[-1] = '=';
537 			wpa_config_process_global(wpa_s->conf, cmd, -1);
538 		}
539 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
540 		   0) {
541 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
542 				     atoi(value))) {
543 			ret = -1;
544 		} else {
545 			value[-1] = '=';
546 			wpa_config_process_global(wpa_s->conf, cmd, -1);
547 		}
548 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
549 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
550 				     atoi(value))) {
551 			ret = -1;
552 		} else {
553 			value[-1] = '=';
554 			wpa_config_process_global(wpa_s->conf, cmd, -1);
555 		}
556 	} else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
557 		wpa_s->wps_fragment_size = atoi(value);
558 #ifdef CONFIG_WPS_TESTING
559 	} else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
560 		long int val;
561 		val = strtol(value, NULL, 0);
562 		if (val < 0 || val > 0xff) {
563 			ret = -1;
564 			wpa_printf(MSG_DEBUG, "WPS: Invalid "
565 				   "wps_version_number %ld", val);
566 		} else {
567 			wps_version_number = val;
568 			wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
569 				   "version %u.%u",
570 				   (wps_version_number & 0xf0) >> 4,
571 				   wps_version_number & 0x0f);
572 		}
573 	} else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) {
574 		wps_testing_stub_cred = atoi(value);
575 		wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d",
576 			   wps_testing_stub_cred);
577 	} else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
578 		wps_corrupt_pkhash = atoi(value);
579 		wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
580 			   wps_corrupt_pkhash);
581 	} else if (os_strcasecmp(cmd, "wps_force_auth_types") == 0) {
582 		if (value[0] == '\0') {
583 			wps_force_auth_types_in_use = 0;
584 		} else {
585 			wps_force_auth_types = strtol(value, NULL, 0);
586 			wps_force_auth_types_in_use = 1;
587 		}
588 	} else if (os_strcasecmp(cmd, "wps_force_encr_types") == 0) {
589 		if (value[0] == '\0') {
590 			wps_force_encr_types_in_use = 0;
591 		} else {
592 			wps_force_encr_types = strtol(value, NULL, 0);
593 			wps_force_encr_types_in_use = 1;
594 		}
595 #endif /* CONFIG_WPS_TESTING */
596 	} else if (os_strcasecmp(cmd, "ampdu") == 0) {
597 		if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
598 			ret = -1;
599 #ifdef CONFIG_TDLS
600 #ifdef CONFIG_TDLS_TESTING
601 	} else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
602 		tdls_testing = strtol(value, NULL, 0);
603 		wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
604 #endif /* CONFIG_TDLS_TESTING */
605 	} else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
606 		int disabled = atoi(value);
607 		wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
608 		if (disabled) {
609 			if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
610 				ret = -1;
611 		} else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
612 			ret = -1;
613 		wpa_tdls_enable(wpa_s->wpa, !disabled);
614 #endif /* CONFIG_TDLS */
615 	} else if (os_strcasecmp(cmd, "pno") == 0) {
616 		ret = wpas_ctrl_pno(wpa_s, value);
617 	} else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
618 		int disabled = atoi(value);
619 		if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
620 			ret = -1;
621 		else if (disabled)
622 			wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
623 	} else if (os_strcasecmp(cmd, "uapsd") == 0) {
624 		if (os_strcmp(value, "disable") == 0)
625 			wpa_s->set_sta_uapsd = 0;
626 		else {
627 			int be, bk, vi, vo;
628 			char *pos;
629 			/* format: BE,BK,VI,VO;max SP Length */
630 			be = atoi(value);
631 			pos = os_strchr(value, ',');
632 			if (pos == NULL)
633 				return -1;
634 			pos++;
635 			bk = atoi(pos);
636 			pos = os_strchr(pos, ',');
637 			if (pos == NULL)
638 				return -1;
639 			pos++;
640 			vi = atoi(pos);
641 			pos = os_strchr(pos, ',');
642 			if (pos == NULL)
643 				return -1;
644 			pos++;
645 			vo = atoi(pos);
646 			/* ignore max SP Length for now */
647 
648 			wpa_s->set_sta_uapsd = 1;
649 			wpa_s->sta_uapsd = 0;
650 			if (be)
651 				wpa_s->sta_uapsd |= BIT(0);
652 			if (bk)
653 				wpa_s->sta_uapsd |= BIT(1);
654 			if (vi)
655 				wpa_s->sta_uapsd |= BIT(2);
656 			if (vo)
657 				wpa_s->sta_uapsd |= BIT(3);
658 		}
659 	} else if (os_strcasecmp(cmd, "ps") == 0) {
660 		ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
661 #ifdef CONFIG_WIFI_DISPLAY
662 	} else if (os_strcasecmp(cmd, "wifi_display") == 0) {
663 		int enabled = !!atoi(value);
664 		if (enabled && !wpa_s->global->p2p)
665 			ret = -1;
666 		else
667 			wifi_display_enable(wpa_s->global, enabled);
668 #endif /* CONFIG_WIFI_DISPLAY */
669 	} else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
670 		ret = set_bssid_filter(wpa_s, value);
671 	} else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
672 		ret = set_disallow_aps(wpa_s, value);
673 	} else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
674 		wpa_s->no_keep_alive = !!atoi(value);
675 #ifdef CONFIG_DPP
676 	} else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
677 		os_free(wpa_s->dpp_configurator_params);
678 		wpa_s->dpp_configurator_params = os_strdup(value);
679 #ifdef CONFIG_DPP2
680 		dpp_controller_set_params(wpa_s->dpp, value);
681 #endif /* CONFIG_DPP2 */
682 	} else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
683 		wpa_s->dpp_init_max_tries = atoi(value);
684 	} else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
685 		wpa_s->dpp_init_retry_time = atoi(value);
686 	} else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
687 		wpa_s->dpp_resp_wait_time = atoi(value);
688 	} else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
689 		wpa_s->dpp_resp_max_tries = atoi(value);
690 	} else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
691 		wpa_s->dpp_resp_retry_time = atoi(value);
692 #ifdef CONFIG_TESTING_OPTIONS
693 	} else if (os_strcasecmp(cmd, "dpp_pkex_own_mac_override") == 0) {
694 		if (hwaddr_aton(value, dpp_pkex_own_mac_override))
695 			ret = -1;
696 	} else if (os_strcasecmp(cmd, "dpp_pkex_peer_mac_override") == 0) {
697 		if (hwaddr_aton(value, dpp_pkex_peer_mac_override))
698 			ret = -1;
699 	} else if (os_strcasecmp(cmd, "dpp_pkex_ephemeral_key_override") == 0) {
700 		size_t hex_len = os_strlen(value);
701 
702 		if (hex_len >
703 		    2 * sizeof(dpp_pkex_ephemeral_key_override))
704 			ret = -1;
705 		else if (hexstr2bin(value, dpp_pkex_ephemeral_key_override,
706 				    hex_len / 2))
707 			ret = -1;
708 		else
709 			dpp_pkex_ephemeral_key_override_len = hex_len / 2;
710 	} else if (os_strcasecmp(cmd, "dpp_protocol_key_override") == 0) {
711 		size_t hex_len = os_strlen(value);
712 
713 		if (hex_len > 2 * sizeof(dpp_protocol_key_override))
714 			ret = -1;
715 		else if (hexstr2bin(value, dpp_protocol_key_override,
716 				    hex_len / 2))
717 			ret = -1;
718 		else
719 			dpp_protocol_key_override_len = hex_len / 2;
720 	} else if (os_strcasecmp(cmd, "dpp_nonce_override") == 0) {
721 		size_t hex_len = os_strlen(value);
722 
723 		if (hex_len > 2 * sizeof(dpp_nonce_override))
724 			ret = -1;
725 		else if (hexstr2bin(value, dpp_nonce_override, hex_len / 2))
726 			ret = -1;
727 		else
728 			dpp_nonce_override_len = hex_len / 2;
729 	} else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
730 		dpp_version_override = atoi(value);
731 #endif /* CONFIG_TESTING_OPTIONS */
732 #endif /* CONFIG_DPP */
733 #ifdef CONFIG_TESTING_OPTIONS
734 	} else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
735 		wpa_s->ext_mgmt_frame_handling = !!atoi(value);
736 	} else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
737 		wpa_s->ext_eapol_frame_io = !!atoi(value);
738 #ifdef CONFIG_AP
739 		if (wpa_s->ap_iface) {
740 			wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
741 				wpa_s->ext_eapol_frame_io;
742 		}
743 #endif /* CONFIG_AP */
744 	} else if (os_strcasecmp(cmd, "encrypt_eapol_m2") == 0) {
745 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_ENCRYPT_EAPOL_M2,
746 				 !!atoi(value));
747 	} else if (os_strcasecmp(cmd, "encrypt_eapol_m4") == 0) {
748 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_ENCRYPT_EAPOL_M4,
749 				 !!atoi(value));
750 	} else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
751 		wpa_s->extra_roc_dur = atoi(value);
752 	} else if (os_strcasecmp(cmd, "test_failure") == 0) {
753 		wpa_s->test_failure = atoi(value);
754 	} else if (os_strcasecmp(cmd, "p2p_go_csa_on_inv") == 0) {
755 		wpa_s->p2p_go_csa_on_inv = !!atoi(value);
756 	} else if (os_strcasecmp(cmd, "ignore_auth_resp") == 0) {
757 		wpa_s->ignore_auth_resp = !!atoi(value);
758 	} else if (os_strcasecmp(cmd, "ignore_assoc_disallow") == 0) {
759 		wpa_s->ignore_assoc_disallow = !!atoi(value);
760 		wpa_drv_ignore_assoc_disallow(wpa_s,
761 					      wpa_s->ignore_assoc_disallow);
762 	} else if (os_strcasecmp(cmd, "disable_sa_query") == 0) {
763 		wpa_s->disable_sa_query = !!atoi(value);
764 	} else if (os_strcasecmp(cmd, "ignore_sae_h2e_only") == 0) {
765 		wpa_s->ignore_sae_h2e_only = !!atoi(value);
766 	} else if (os_strcasecmp(cmd, "extra_sae_rejected_groups") == 0) {
767 		char *pos;
768 
769 		os_free(wpa_s->extra_sae_rejected_groups);
770 		wpa_s->extra_sae_rejected_groups = NULL;
771 		pos = value;
772 		while (pos && pos[0]) {
773 			int group;
774 
775 			group = atoi(pos);
776 			wpa_printf(MSG_DEBUG,
777 				   "TESTING: Extra rejection of SAE group %d",
778 				   group);
779 			if (group)
780 				int_array_add_unique(
781 					&wpa_s->extra_sae_rejected_groups,
782 					group);
783 			pos = os_strchr(pos, ' ');
784 			if (!pos)
785 				break;
786 			pos++;
787 		}
788 	} else if (os_strcasecmp(cmd, "ft_rsnxe_used") == 0) {
789 		wpa_s->ft_rsnxe_used = atoi(value);
790 	} else if (os_strcasecmp(cmd, "oci_freq_override_eapol") == 0) {
791 		wpa_s->oci_freq_override_eapol = atoi(value);
792 	} else if (os_strcasecmp(cmd, "oci_freq_override_saquery_req") == 0) {
793 		wpa_s->oci_freq_override_saquery_req = atoi(value);
794 	} else if (os_strcasecmp(cmd, "oci_freq_override_saquery_resp") == 0) {
795 		wpa_s->oci_freq_override_saquery_resp = atoi(value);
796 	} else if (os_strcasecmp(cmd, "oci_freq_override_eapol_g2") == 0) {
797 		wpa_s->oci_freq_override_eapol_g2 = atoi(value);
798 		/* Populate value to wpa_sm if already associated. */
799 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_EAPOL_G2,
800 				 wpa_s->oci_freq_override_eapol_g2);
801 	} else if (os_strcasecmp(cmd, "oci_freq_override_ft_assoc") == 0) {
802 		wpa_s->oci_freq_override_ft_assoc = atoi(value);
803 		/* Populate value to wpa_sm if already associated. */
804 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_FT_ASSOC,
805 				 wpa_s->oci_freq_override_ft_assoc);
806 	} else if (os_strcasecmp(cmd, "oci_freq_override_fils_assoc") == 0) {
807 		wpa_s->oci_freq_override_fils_assoc = atoi(value);
808 	} else if (os_strcasecmp(cmd, "oci_freq_override_wnm_sleep") == 0) {
809 		wpa_s->oci_freq_override_wnm_sleep = atoi(value);
810 	} else if (os_strcasecmp(cmd, "rsne_override_eapol") == 0) {
811 		wpabuf_free(wpa_s->rsne_override_eapol);
812 		if (os_strcmp(value, "NULL") == 0)
813 			wpa_s->rsne_override_eapol = NULL;
814 		else
815 			wpa_s->rsne_override_eapol = wpabuf_parse_bin(value);
816 	} else if (os_strcasecmp(cmd, "rsnxe_override_assoc") == 0) {
817 		wpabuf_free(wpa_s->rsnxe_override_assoc);
818 		if (os_strcmp(value, "NULL") == 0)
819 			wpa_s->rsnxe_override_assoc = NULL;
820 		else
821 			wpa_s->rsnxe_override_assoc = wpabuf_parse_bin(value);
822 	} else if (os_strcasecmp(cmd, "rsnxe_override_eapol") == 0) {
823 		wpabuf_free(wpa_s->rsnxe_override_eapol);
824 		if (os_strcmp(value, "NULL") == 0)
825 			wpa_s->rsnxe_override_eapol = NULL;
826 		else
827 			wpa_s->rsnxe_override_eapol = wpabuf_parse_bin(value);
828 	} else if (os_strcasecmp(cmd, "reject_btm_req_reason") == 0) {
829 		wpa_s->reject_btm_req_reason = atoi(value);
830 	} else if (os_strcasecmp(cmd, "get_pref_freq_list_override") == 0) {
831 		os_free(wpa_s->get_pref_freq_list_override);
832 		if (!value[0])
833 			wpa_s->get_pref_freq_list_override = NULL;
834 		else
835 			wpa_s->get_pref_freq_list_override = os_strdup(value);
836 	} else if (os_strcasecmp(cmd, "sae_commit_override") == 0) {
837 		wpabuf_free(wpa_s->sae_commit_override);
838 		if (value[0] == '\0')
839 			wpa_s->sae_commit_override = NULL;
840 		else
841 			wpa_s->sae_commit_override = wpabuf_parse_bin(value);
842 	} else if (os_strcasecmp(cmd, "driver_signal_override") == 0) {
843 		ret = wpas_ctrl_iface_set_dso(wpa_s, value);
844 #ifndef CONFIG_NO_ROBUST_AV
845 	} else if (os_strcasecmp(cmd, "disable_scs_support") == 0) {
846 		wpa_s->disable_scs_support = !!atoi(value);
847 	} else if (os_strcasecmp(cmd, "disable_mscs_support") == 0) {
848 		wpa_s->disable_mscs_support = !!atoi(value);
849 #endif /* CONFIG_NO_ROBUST_AV */
850 	} else if (os_strcasecmp(cmd, "disable_eapol_g2_tx") == 0) {
851 		wpa_s->disable_eapol_g2_tx = !!atoi(value);
852 		/* Populate value to wpa_sm if already associated. */
853 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_DISABLE_EAPOL_G2_TX,
854 				 wpa_s->disable_eapol_g2_tx);
855 	} else if (os_strcasecmp(cmd, "test_assoc_comeback_type") == 0) {
856 		wpa_s->test_assoc_comeback_type = atoi(value);
857 #ifdef CONFIG_DPP
858 	} else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
859 		os_free(wpa_s->dpp_config_obj_override);
860 		if (value[0] == '\0')
861 			wpa_s->dpp_config_obj_override = NULL;
862 		else
863 			wpa_s->dpp_config_obj_override = os_strdup(value);
864 	} else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
865 		os_free(wpa_s->dpp_discovery_override);
866 		if (value[0] == '\0')
867 			wpa_s->dpp_discovery_override = NULL;
868 		else
869 			wpa_s->dpp_discovery_override = os_strdup(value);
870 	} else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
871 		os_free(wpa_s->dpp_groups_override);
872 		if (value[0] == '\0')
873 			wpa_s->dpp_groups_override = NULL;
874 		else
875 			wpa_s->dpp_groups_override = os_strdup(value);
876 	} else if (os_strcasecmp(cmd,
877 				 "dpp_ignore_netaccesskey_mismatch") == 0) {
878 		wpa_s->dpp_ignore_netaccesskey_mismatch = atoi(value);
879 	} else if (os_strcasecmp(cmd, "dpp_discard_public_action") == 0) {
880 		wpa_s->dpp_discard_public_action = atoi(value);
881 	} else if (os_strcasecmp(cmd, "dpp_test") == 0) {
882 		dpp_test = atoi(value);
883 #endif /* CONFIG_DPP */
884 #endif /* CONFIG_TESTING_OPTIONS */
885 #ifdef CONFIG_FILS
886 	} else if (os_strcasecmp(cmd, "disable_fils") == 0) {
887 		wpa_s->disable_fils = !!atoi(value);
888 		wpa_drv_disable_fils(wpa_s, wpa_s->disable_fils);
889 		wpa_supplicant_set_default_scan_ies(wpa_s);
890 #endif /* CONFIG_FILS */
891 #ifndef CONFIG_NO_CONFIG_BLOBS
892 	} else if (os_strcmp(cmd, "blob") == 0) {
893 		ret = wpas_ctrl_set_blob(wpa_s, value);
894 #endif /* CONFIG_NO_CONFIG_BLOBS */
895 	} else if (os_strcasecmp(cmd, "setband") == 0) {
896 		ret = wpas_ctrl_set_band(wpa_s, value);
897 #ifdef CONFIG_MBO
898 	} else if (os_strcasecmp(cmd, "non_pref_chan") == 0) {
899 		ret = wpas_mbo_update_non_pref_chan(wpa_s, value);
900 		if (ret == 0) {
901 			value[-1] = '=';
902 			wpa_config_process_global(wpa_s->conf, cmd, -1);
903 		}
904 	} else if (os_strcasecmp(cmd, "mbo_cell_capa") == 0) {
905 		int val = atoi(value);
906 
907 		if (val < MBO_CELL_CAPA_AVAILABLE ||
908 		    val > MBO_CELL_CAPA_NOT_SUPPORTED)
909 			return -1;
910 
911 		wpas_mbo_update_cell_capa(wpa_s, val);
912 	} else if (os_strcasecmp(cmd, "oce") == 0) {
913 		int val = atoi(value);
914 
915 		if (val < 0 || val > 3)
916 			return -1;
917 
918 		wpa_s->conf->oce = val;
919 		if (wpa_s->conf->oce) {
920 			if ((wpa_s->conf->oce & OCE_STA) &&
921 			    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA))
922 				wpa_s->enable_oce = OCE_STA;
923 
924 			if ((wpa_s->conf->oce & OCE_STA_CFON) &&
925 			    (wpa_s->drv_flags &
926 			     WPA_DRIVER_FLAGS_OCE_STA_CFON)) {
927 				/* TODO: Need to add STA-CFON support */
928 				wpa_printf(MSG_ERROR,
929 					   "OCE STA-CFON feature is not yet supported");
930 				return -1;
931 			}
932 		} else {
933 			wpa_s->enable_oce = 0;
934 		}
935 		wpa_supplicant_set_default_scan_ies(wpa_s);
936 #endif /* CONFIG_MBO */
937 	} else if (os_strcasecmp(cmd, "lci") == 0) {
938 		ret = wpas_ctrl_iface_set_lci(wpa_s, value);
939 	} else if (os_strcasecmp(cmd, "tdls_trigger_control") == 0) {
940 		ret = wpa_drv_set_tdls_mode(wpa_s, atoi(value));
941 	} else if (os_strcasecmp(cmd, "relative_rssi") == 0) {
942 		ret = wpas_ctrl_set_relative_rssi(wpa_s, value);
943 	} else if (os_strcasecmp(cmd, "relative_band_adjust") == 0) {
944 		ret = wpas_ctrl_set_relative_band_adjust(wpa_s, value);
945 	} else if (os_strcasecmp(cmd, "ric_ies") == 0) {
946 		ret = wpas_ctrl_iface_set_ric_ies(wpa_s, value);
947 	} else if (os_strcasecmp(cmd, "roaming") == 0) {
948 		ret = wpa_drv_roaming(wpa_s, atoi(value), NULL);
949 #ifdef CONFIG_WNM
950 	} else if (os_strcasecmp(cmd, "coloc_intf_elems") == 0) {
951 		struct wpabuf *elems;
952 
953 		elems = wpabuf_parse_bin(value);
954 		if (!elems)
955 			return -1;
956 		wnm_set_coloc_intf_elems(wpa_s, elems);
957 #endif /* CONFIG_WNM */
958 #ifndef CONFIG_NO_ROBUST_AV
959 	} else if (os_strcasecmp(cmd, "enable_dscp_policy_capa") == 0) {
960 		wpa_s->enable_dscp_policy_capa = !!atoi(value);
961 #endif /* CONFIG_NO_ROBUST_AV */
962 	} else {
963 		value[-1] = '=';
964 		ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
965 		if (ret == 0)
966 			wpa_supplicant_update_config(wpa_s);
967 		else if (ret == 1)
968 			ret = 0;
969 	}
970 
971 	return ret;
972 }
973 
974 
wpa_supplicant_ctrl_iface_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)975 static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
976 					 char *cmd, char *buf, size_t buflen)
977 {
978 	int res = -1;
979 
980 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
981 
982 	if (os_strcmp(cmd, "version") == 0) {
983 		res = os_snprintf(buf, buflen, "%s", VERSION_STR);
984 	} else if (os_strcasecmp(cmd, "max_command_len") == 0) {
985 		res = os_snprintf(buf, buflen, "%u", CTRL_IFACE_MAX_LEN);
986 	} else if (os_strcasecmp(cmd, "country") == 0) {
987 		if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
988 			res = os_snprintf(buf, buflen, "%c%c",
989 					  wpa_s->conf->country[0],
990 					  wpa_s->conf->country[1]);
991 #ifdef CONFIG_WIFI_DISPLAY
992 	} else if (os_strcasecmp(cmd, "wifi_display") == 0) {
993 		int enabled;
994 		if (wpa_s->global->p2p == NULL ||
995 		    wpa_s->global->p2p_disabled)
996 			enabled = 0;
997 		else
998 			enabled = wpa_s->global->wifi_display;
999 		res = os_snprintf(buf, buflen, "%d", enabled);
1000 #endif /* CONFIG_WIFI_DISPLAY */
1001 #ifdef CONFIG_TESTING_GET_GTK
1002 	} else if (os_strcmp(cmd, "gtk") == 0) {
1003 		if (wpa_s->last_gtk_len == 0)
1004 			return -1;
1005 		res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
1006 				       wpa_s->last_gtk_len);
1007 		return res;
1008 #endif /* CONFIG_TESTING_GET_GTK */
1009 	} else if (os_strcmp(cmd, "tls_library") == 0) {
1010 		res = tls_get_library_version(buf, buflen);
1011 #ifdef CONFIG_TESTING_OPTIONS
1012 	} else if (os_strcmp(cmd, "anonce") == 0) {
1013 		return wpa_snprintf_hex(buf, buflen,
1014 					wpa_sm_get_anonce(wpa_s->wpa),
1015 					WPA_NONCE_LEN);
1016 	} else if (os_strcasecmp(cmd, "last_tk_key_idx") == 0) {
1017 		res = os_snprintf(buf, buflen, "%d", wpa_s->last_tk_key_idx);
1018 #endif /* CONFIG_TESTING_OPTIONS */
1019 	} else {
1020 		res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
1021 	}
1022 
1023 	if (os_snprintf_error(buflen, res))
1024 		return -1;
1025 	return res;
1026 }
1027 
1028 
1029 #ifdef IEEE8021X_EAPOL
wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant * wpa_s,char * addr)1030 static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
1031 					     char *addr)
1032 {
1033 	u8 bssid[ETH_ALEN];
1034 	struct wpa_ssid *ssid = wpa_s->current_ssid;
1035 
1036 	if (hwaddr_aton(addr, bssid)) {
1037 		wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
1038 			   "'%s'", addr);
1039 		return -1;
1040 	}
1041 
1042 	wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
1043 	rsn_preauth_deinit(wpa_s->wpa);
1044 	if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
1045 		return -1;
1046 
1047 	return 0;
1048 }
1049 #endif /* IEEE8021X_EAPOL */
1050 
1051 
1052 #ifdef CONFIG_TDLS
1053 
wpa_supplicant_ctrl_iface_tdls_discover(struct wpa_supplicant * wpa_s,char * addr)1054 static int wpa_supplicant_ctrl_iface_tdls_discover(
1055 	struct wpa_supplicant *wpa_s, char *addr)
1056 {
1057 	u8 peer[ETH_ALEN];
1058 	int ret;
1059 
1060 	if (hwaddr_aton(addr, peer)) {
1061 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
1062 			   "address '%s'", addr);
1063 		return -1;
1064 	}
1065 
1066 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
1067 		   MAC2STR(peer));
1068 
1069 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1070 		ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
1071 	else
1072 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
1073 
1074 	return ret;
1075 }
1076 
1077 
wpa_supplicant_ctrl_iface_tdls_setup(struct wpa_supplicant * wpa_s,char * addr)1078 static int wpa_supplicant_ctrl_iface_tdls_setup(
1079 	struct wpa_supplicant *wpa_s, char *addr)
1080 {
1081 	u8 peer[ETH_ALEN];
1082 	int ret;
1083 
1084 	if (hwaddr_aton(addr, peer)) {
1085 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
1086 			   "address '%s'", addr);
1087 		return -1;
1088 	}
1089 
1090 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
1091 		   MAC2STR(peer));
1092 
1093 	if ((wpa_s->conf->tdls_external_control) &&
1094 	    wpa_tdls_is_external_setup(wpa_s->wpa))
1095 		return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
1096 
1097 	wpa_tdls_remove(wpa_s->wpa, peer);
1098 
1099 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1100 		ret = wpa_tdls_start(wpa_s->wpa, peer);
1101 	else
1102 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
1103 
1104 	return ret;
1105 }
1106 
1107 
wpa_supplicant_ctrl_iface_tdls_teardown(struct wpa_supplicant * wpa_s,char * addr)1108 static int wpa_supplicant_ctrl_iface_tdls_teardown(
1109 	struct wpa_supplicant *wpa_s, char *addr)
1110 {
1111 	u8 peer[ETH_ALEN];
1112 	int ret;
1113 
1114 	if (os_strcmp(addr, "*") == 0) {
1115 		/* remove everyone */
1116 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
1117 		wpa_tdls_teardown_peers(wpa_s->wpa);
1118 		return 0;
1119 	}
1120 
1121 	if (hwaddr_aton(addr, peer)) {
1122 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
1123 			   "address '%s'", addr);
1124 		return -1;
1125 	}
1126 
1127 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
1128 		   MAC2STR(peer));
1129 
1130 	if ((wpa_s->conf->tdls_external_control) &&
1131 	    wpa_tdls_is_external_setup(wpa_s->wpa))
1132 		return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
1133 
1134 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1135 		ret = wpa_tdls_teardown_link(
1136 			wpa_s->wpa, peer,
1137 			WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
1138 	else
1139 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
1140 
1141 	return ret;
1142 }
1143 
1144 
ctrl_iface_get_capability_tdls(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)1145 static int ctrl_iface_get_capability_tdls(
1146 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1147 {
1148 	int ret;
1149 
1150 	ret = os_snprintf(buf, buflen, "%s\n",
1151 			  wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
1152 			  (wpa_s->drv_flags &
1153 			   WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
1154 			   "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
1155 	if (os_snprintf_error(buflen, ret))
1156 		return -1;
1157 	return ret;
1158 }
1159 
1160 
wpa_supplicant_ctrl_iface_tdls_chan_switch(struct wpa_supplicant * wpa_s,char * cmd)1161 static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
1162 	struct wpa_supplicant *wpa_s, char *cmd)
1163 {
1164 	u8 peer[ETH_ALEN];
1165 	struct hostapd_freq_params freq_params;
1166 	u8 oper_class;
1167 	char *pos, *end;
1168 
1169 	if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
1170 		wpa_printf(MSG_INFO,
1171 			   "tdls_chanswitch: Only supported with external setup");
1172 		return -1;
1173 	}
1174 
1175 	os_memset(&freq_params, 0, sizeof(freq_params));
1176 
1177 	pos = os_strchr(cmd, ' ');
1178 	if (pos == NULL)
1179 		return -1;
1180 	*pos++ = '\0';
1181 
1182 	oper_class = strtol(pos, &end, 10);
1183 	if (pos == end) {
1184 		wpa_printf(MSG_INFO,
1185 			   "tdls_chanswitch: Invalid op class provided");
1186 		return -1;
1187 	}
1188 
1189 	pos = end;
1190 	freq_params.freq = atoi(pos);
1191 	if (freq_params.freq == 0) {
1192 		wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
1193 		return -1;
1194 	}
1195 
1196 #define SET_FREQ_SETTING(str) \
1197 	do { \
1198 		const char *pos2 = os_strstr(pos, " " #str "="); \
1199 		if (pos2) { \
1200 			pos2 += sizeof(" " #str "=") - 1; \
1201 			freq_params.str = atoi(pos2); \
1202 		} \
1203 	} while (0)
1204 
1205 	SET_FREQ_SETTING(center_freq1);
1206 	SET_FREQ_SETTING(center_freq2);
1207 	SET_FREQ_SETTING(bandwidth);
1208 	SET_FREQ_SETTING(sec_channel_offset);
1209 #undef SET_FREQ_SETTING
1210 
1211 	freq_params.ht_enabled = !!os_strstr(pos, " ht");
1212 	freq_params.vht_enabled = !!os_strstr(pos, " vht");
1213 
1214 	if (hwaddr_aton(cmd, peer)) {
1215 		wpa_printf(MSG_DEBUG,
1216 			   "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
1217 			   cmd);
1218 		return -1;
1219 	}
1220 
1221 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
1222 		   " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
1223 		   MAC2STR(peer), oper_class, freq_params.freq,
1224 		   freq_params.center_freq1, freq_params.center_freq2,
1225 		   freq_params.bandwidth, freq_params.sec_channel_offset,
1226 		   freq_params.ht_enabled ? " HT" : "",
1227 		   freq_params.vht_enabled ? " VHT" : "");
1228 
1229 	return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
1230 					   &freq_params);
1231 }
1232 
1233 
wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(struct wpa_supplicant * wpa_s,char * cmd)1234 static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
1235 	struct wpa_supplicant *wpa_s, char *cmd)
1236 {
1237 	u8 peer[ETH_ALEN];
1238 
1239 	if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
1240 		wpa_printf(MSG_INFO,
1241 			   "tdls_chanswitch: Only supported with external setup");
1242 		return -1;
1243 	}
1244 
1245 	if (hwaddr_aton(cmd, peer)) {
1246 		wpa_printf(MSG_DEBUG,
1247 			   "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
1248 			   cmd);
1249 		return -1;
1250 	}
1251 
1252 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
1253 		   MAC2STR(peer));
1254 
1255 	return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
1256 }
1257 
1258 
wpa_supplicant_ctrl_iface_tdls_link_status(struct wpa_supplicant * wpa_s,const char * addr,char * buf,size_t buflen)1259 static int wpa_supplicant_ctrl_iface_tdls_link_status(
1260 	struct wpa_supplicant *wpa_s, const char *addr,
1261 	char *buf, size_t buflen)
1262 {
1263 	u8 peer[ETH_ALEN];
1264 	const char *tdls_status;
1265 	int ret;
1266 
1267 	if (hwaddr_aton(addr, peer)) {
1268 		wpa_printf(MSG_DEBUG,
1269 			   "CTRL_IFACE TDLS_LINK_STATUS: Invalid address '%s'",
1270 			   addr);
1271 		return -1;
1272 	}
1273 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS " MACSTR,
1274 		   MAC2STR(peer));
1275 
1276 	tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
1277 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS: %s", tdls_status);
1278 	ret = os_snprintf(buf, buflen, "TDLS link status: %s\n", tdls_status);
1279 	if (os_snprintf_error(buflen, ret))
1280 		return -1;
1281 
1282 	return ret;
1283 }
1284 
1285 #endif /* CONFIG_TDLS */
1286 
1287 
1288 #ifndef CONFIG_NO_WMM_AC
1289 
wmm_ac_ctrl_addts(struct wpa_supplicant * wpa_s,char * cmd)1290 static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
1291 {
1292 	char *token, *context = NULL;
1293 	struct wmm_ac_ts_setup_params params = {
1294 		.tsid = 0xff,
1295 		.direction = 0xff,
1296 	};
1297 
1298 	while ((token = str_token(cmd, " ", &context))) {
1299 		if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
1300 		    sscanf(token, "up=%i", &params.user_priority) == 1 ||
1301 		    sscanf(token, "nominal_msdu_size=%i",
1302 			   &params.nominal_msdu_size) == 1 ||
1303 		    sscanf(token, "mean_data_rate=%i",
1304 			   &params.mean_data_rate) == 1 ||
1305 		    sscanf(token, "min_phy_rate=%i",
1306 			   &params.minimum_phy_rate) == 1 ||
1307 		    sscanf(token, "sba=%i",
1308 			   &params.surplus_bandwidth_allowance) == 1)
1309 			continue;
1310 
1311 		if (os_strcasecmp(token, "downlink") == 0) {
1312 			params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
1313 		} else if (os_strcasecmp(token, "uplink") == 0) {
1314 			params.direction = WMM_TSPEC_DIRECTION_UPLINK;
1315 		} else if (os_strcasecmp(token, "bidi") == 0) {
1316 			params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
1317 		} else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
1318 			params.fixed_nominal_msdu = 1;
1319 		} else {
1320 			wpa_printf(MSG_DEBUG,
1321 				   "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
1322 				   token);
1323 			return -1;
1324 		}
1325 
1326 	}
1327 
1328 	return wpas_wmm_ac_addts(wpa_s, &params);
1329 }
1330 
1331 
wmm_ac_ctrl_delts(struct wpa_supplicant * wpa_s,char * cmd)1332 static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
1333 {
1334 	u8 tsid = atoi(cmd);
1335 
1336 	return wpas_wmm_ac_delts(wpa_s, tsid);
1337 }
1338 
1339 #endif /* CONFIG_NO_WMM_AC */
1340 
1341 
1342 #ifdef CONFIG_IEEE80211R
wpa_supplicant_ctrl_iface_ft_ds(struct wpa_supplicant * wpa_s,char * addr)1343 static int wpa_supplicant_ctrl_iface_ft_ds(
1344 	struct wpa_supplicant *wpa_s, char *addr)
1345 {
1346 	u8 target_ap[ETH_ALEN];
1347 	struct wpa_bss *bss;
1348 	const u8 *mdie;
1349 	bool force = os_strstr(addr, " force") != NULL;
1350 
1351 	if (hwaddr_aton(addr, target_ap)) {
1352 		wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
1353 			   "address '%s'", addr);
1354 		return -1;
1355 	}
1356 
1357 	wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
1358 
1359 	bss = wpa_bss_get_bssid(wpa_s, target_ap);
1360 	if (bss)
1361 		mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1362 	else
1363 		mdie = NULL;
1364 
1365 	return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie, force);
1366 }
1367 #endif /* CONFIG_IEEE80211R */
1368 
1369 
1370 #ifdef CONFIG_WPS
wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant * wpa_s,char * cmd)1371 static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
1372 					     char *cmd)
1373 {
1374 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1375 #ifdef CONFIG_P2P
1376 	u8 p2p_dev_addr[ETH_ALEN];
1377 #endif /* CONFIG_P2P */
1378 #ifdef CONFIG_AP
1379 	u8 *_p2p_dev_addr = NULL;
1380 #endif /* CONFIG_AP */
1381 	char *pos;
1382 	int multi_ap = 0;
1383 
1384 	if (!cmd || os_strcmp(cmd, "any") == 0 ||
1385 	    os_strncmp(cmd, "any ", 4) == 0) {
1386 		_bssid = NULL;
1387 #ifdef CONFIG_P2P
1388 	} else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
1389 		if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
1390 			wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
1391 				   "P2P Device Address '%s'",
1392 				   cmd + 13);
1393 			return -1;
1394 		}
1395 		_p2p_dev_addr = p2p_dev_addr;
1396 #endif /* CONFIG_P2P */
1397 	} else if (os_strncmp(cmd, "multi_ap=", 9) == 0) {
1398 		_bssid = NULL;
1399 		multi_ap = atoi(cmd + 9);
1400 	} else if (hwaddr_aton(cmd, bssid)) {
1401 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
1402 			   cmd);
1403 		return -1;
1404 	}
1405 
1406 	if (cmd) {
1407 		pos = os_strstr(cmd, " multi_ap=");
1408 		if (pos) {
1409 			pos += 10;
1410 			multi_ap = atoi(pos);
1411 		}
1412 	}
1413 
1414 #ifdef CONFIG_AP
1415 	if (wpa_s->ap_iface)
1416 		return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
1417 #endif /* CONFIG_AP */
1418 
1419 	return wpas_wps_start_pbc(wpa_s, _bssid, 0, multi_ap);
1420 }
1421 
1422 
wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1423 static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
1424 					     char *cmd, char *buf,
1425 					     size_t buflen)
1426 {
1427 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1428 	char *pin;
1429 	int ret;
1430 
1431 	pin = os_strchr(cmd, ' ');
1432 	if (pin)
1433 		*pin++ = '\0';
1434 
1435 	if (os_strcmp(cmd, "any") == 0)
1436 		_bssid = NULL;
1437 	else if (os_strcmp(cmd, "get") == 0) {
1438 		if (wps_generate_pin((unsigned int *) &ret) < 0)
1439 			return -1;
1440 		goto done;
1441 	} else if (hwaddr_aton(cmd, bssid)) {
1442 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
1443 			   cmd);
1444 		return -1;
1445 	}
1446 
1447 #ifdef CONFIG_AP
1448 	if (wpa_s->ap_iface) {
1449 		int timeout = 0;
1450 		char *pos;
1451 
1452 		if (pin) {
1453 			pos = os_strchr(pin, ' ');
1454 			if (pos) {
1455 				*pos++ = '\0';
1456 				timeout = atoi(pos);
1457 			}
1458 		}
1459 
1460 		return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
1461 						 buf, buflen, timeout);
1462 	}
1463 #endif /* CONFIG_AP */
1464 
1465 	if (pin) {
1466 		ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
1467 					 DEV_PW_DEFAULT);
1468 		if (ret < 0)
1469 			return -1;
1470 		ret = os_snprintf(buf, buflen, "%s", pin);
1471 		if (os_snprintf_error(buflen, ret))
1472 			return -1;
1473 		return ret;
1474 	}
1475 
1476 	ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
1477 	if (ret < 0)
1478 		return -1;
1479 
1480 done:
1481 	/* Return the generated PIN */
1482 	ret = os_snprintf(buf, buflen, "%08d", ret);
1483 	if (os_snprintf_error(buflen, ret))
1484 		return -1;
1485 	return ret;
1486 }
1487 
1488 
wpa_supplicant_ctrl_iface_wps_check_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1489 static int wpa_supplicant_ctrl_iface_wps_check_pin(
1490 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1491 {
1492 	char pin[9];
1493 	size_t len;
1494 	char *pos;
1495 	int ret;
1496 
1497 	wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
1498 			      (u8 *) cmd, os_strlen(cmd));
1499 	for (pos = cmd, len = 0; *pos != '\0'; pos++) {
1500 		if (*pos < '0' || *pos > '9')
1501 			continue;
1502 		pin[len++] = *pos;
1503 		if (len == 9) {
1504 			wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
1505 			return -1;
1506 		}
1507 	}
1508 	if (len != 4 && len != 8) {
1509 		wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
1510 		return -1;
1511 	}
1512 	pin[len] = '\0';
1513 
1514 	if (len == 8) {
1515 		unsigned int pin_val;
1516 		pin_val = atoi(pin);
1517 		if (!wps_pin_valid(pin_val)) {
1518 			wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
1519 			ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
1520 			if (os_snprintf_error(buflen, ret))
1521 				return -1;
1522 			return ret;
1523 		}
1524 	}
1525 
1526 	ret = os_snprintf(buf, buflen, "%s", pin);
1527 	if (os_snprintf_error(buflen, ret))
1528 		return -1;
1529 
1530 	return ret;
1531 }
1532 
1533 
1534 #ifdef CONFIG_WPS_NFC
1535 
wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant * wpa_s,char * cmd)1536 static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
1537 					     char *cmd)
1538 {
1539 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1540 
1541 	if (cmd == NULL || cmd[0] == '\0')
1542 		_bssid = NULL;
1543 	else if (hwaddr_aton(cmd, bssid))
1544 		return -1;
1545 
1546 	return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
1547 				  0, 0);
1548 }
1549 
1550 
wpa_supplicant_ctrl_iface_wps_nfc_config_token(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1551 static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
1552 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1553 {
1554 	int ndef;
1555 	struct wpabuf *buf;
1556 	int res;
1557 	char *pos;
1558 
1559 	pos = os_strchr(cmd, ' ');
1560 	if (pos)
1561 		*pos++ = '\0';
1562 	if (os_strcmp(cmd, "WPS") == 0)
1563 		ndef = 0;
1564 	else if (os_strcmp(cmd, "NDEF") == 0)
1565 		ndef = 1;
1566 	else
1567 		return -1;
1568 
1569 	buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
1570 	if (buf == NULL)
1571 		return -1;
1572 
1573 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1574 					 wpabuf_len(buf));
1575 	reply[res++] = '\n';
1576 	reply[res] = '\0';
1577 
1578 	wpabuf_free(buf);
1579 
1580 	return res;
1581 }
1582 
1583 
wpa_supplicant_ctrl_iface_wps_nfc_token(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1584 static int wpa_supplicant_ctrl_iface_wps_nfc_token(
1585 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1586 {
1587 	int ndef;
1588 	struct wpabuf *buf;
1589 	int res;
1590 
1591 	if (os_strcmp(cmd, "WPS") == 0)
1592 		ndef = 0;
1593 	else if (os_strcmp(cmd, "NDEF") == 0)
1594 		ndef = 1;
1595 	else
1596 		return -1;
1597 
1598 	buf = wpas_wps_nfc_token(wpa_s, ndef);
1599 	if (buf == NULL)
1600 		return -1;
1601 
1602 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1603 					 wpabuf_len(buf));
1604 	reply[res++] = '\n';
1605 	reply[res] = '\0';
1606 
1607 	wpabuf_free(buf);
1608 
1609 	return res;
1610 }
1611 
1612 
wpa_supplicant_ctrl_iface_wps_nfc_tag_read(struct wpa_supplicant * wpa_s,char * pos)1613 static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
1614 	struct wpa_supplicant *wpa_s, char *pos)
1615 {
1616 	size_t len;
1617 	struct wpabuf *buf;
1618 	int ret;
1619 	char *freq;
1620 	int forced_freq = 0;
1621 
1622 	freq = strstr(pos, " freq=");
1623 	if (freq) {
1624 		*freq = '\0';
1625 		freq += 6;
1626 		forced_freq = atoi(freq);
1627 	}
1628 
1629 	len = os_strlen(pos);
1630 	if (len & 0x01)
1631 		return -1;
1632 	len /= 2;
1633 
1634 	buf = wpabuf_alloc(len);
1635 	if (buf == NULL)
1636 		return -1;
1637 	if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
1638 		wpabuf_free(buf);
1639 		return -1;
1640 	}
1641 
1642 	ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
1643 	wpabuf_free(buf);
1644 
1645 	return ret;
1646 }
1647 
1648 
wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef)1649 static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
1650 					      char *reply, size_t max_len,
1651 					      int ndef)
1652 {
1653 	struct wpabuf *buf;
1654 	int res;
1655 
1656 	buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
1657 	if (buf == NULL)
1658 		return -1;
1659 
1660 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1661 					 wpabuf_len(buf));
1662 	reply[res++] = '\n';
1663 	reply[res] = '\0';
1664 
1665 	wpabuf_free(buf);
1666 
1667 	return res;
1668 }
1669 
1670 
1671 #ifdef CONFIG_P2P
wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef)1672 static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
1673 					      char *reply, size_t max_len,
1674 					      int ndef)
1675 {
1676 	struct wpabuf *buf;
1677 	int res;
1678 
1679 	buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
1680 	if (buf == NULL) {
1681 		wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
1682 		return -1;
1683 	}
1684 
1685 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1686 					 wpabuf_len(buf));
1687 	reply[res++] = '\n';
1688 	reply[res] = '\0';
1689 
1690 	wpabuf_free(buf);
1691 
1692 	return res;
1693 }
1694 #endif /* CONFIG_P2P */
1695 
1696 
wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1697 static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
1698 					  char *cmd, char *reply,
1699 					  size_t max_len)
1700 {
1701 	char *pos;
1702 	int ndef;
1703 
1704 	pos = os_strchr(cmd, ' ');
1705 	if (pos == NULL)
1706 		return -1;
1707 	*pos++ = '\0';
1708 
1709 	if (os_strcmp(cmd, "WPS") == 0)
1710 		ndef = 0;
1711 	else if (os_strcmp(cmd, "NDEF") == 0)
1712 		ndef = 1;
1713 	else
1714 		return -1;
1715 
1716 	if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
1717 		if (!ndef)
1718 			return -1;
1719 		return wpas_ctrl_nfc_get_handover_req_wps(
1720 			wpa_s, reply, max_len, ndef);
1721 	}
1722 
1723 #ifdef CONFIG_P2P
1724 	if (os_strcmp(pos, "P2P-CR") == 0) {
1725 		return wpas_ctrl_nfc_get_handover_req_p2p(
1726 			wpa_s, reply, max_len, ndef);
1727 	}
1728 #endif /* CONFIG_P2P */
1729 
1730 	return -1;
1731 }
1732 
1733 
wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef,int cr,char * uuid)1734 static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
1735 					      char *reply, size_t max_len,
1736 					      int ndef, int cr, char *uuid)
1737 {
1738 	struct wpabuf *buf;
1739 	int res;
1740 
1741 	buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
1742 	if (buf == NULL)
1743 		return -1;
1744 
1745 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1746 					 wpabuf_len(buf));
1747 	reply[res++] = '\n';
1748 	reply[res] = '\0';
1749 
1750 	wpabuf_free(buf);
1751 
1752 	return res;
1753 }
1754 
1755 
1756 #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)1757 static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1758 					      char *reply, size_t max_len,
1759 					      int ndef, int tag)
1760 {
1761 	struct wpabuf *buf;
1762 	int res;
1763 
1764 	buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1765 	if (buf == NULL)
1766 		return -1;
1767 
1768 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1769 					 wpabuf_len(buf));
1770 	reply[res++] = '\n';
1771 	reply[res] = '\0';
1772 
1773 	wpabuf_free(buf);
1774 
1775 	return res;
1776 }
1777 #endif /* CONFIG_P2P */
1778 
1779 
wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1780 static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1781 					  char *cmd, char *reply,
1782 					  size_t max_len)
1783 {
1784 	char *pos, *pos2;
1785 	int ndef;
1786 
1787 	pos = os_strchr(cmd, ' ');
1788 	if (pos == NULL)
1789 		return -1;
1790 	*pos++ = '\0';
1791 
1792 	if (os_strcmp(cmd, "WPS") == 0)
1793 		ndef = 0;
1794 	else if (os_strcmp(cmd, "NDEF") == 0)
1795 		ndef = 1;
1796 	else
1797 		return -1;
1798 
1799 	pos2 = os_strchr(pos, ' ');
1800 	if (pos2)
1801 		*pos2++ = '\0';
1802 	if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
1803 		if (!ndef)
1804 			return -1;
1805 		return wpas_ctrl_nfc_get_handover_sel_wps(
1806 			wpa_s, reply, max_len, ndef,
1807 			os_strcmp(pos, "WPS-CR") == 0, pos2);
1808 	}
1809 
1810 #ifdef CONFIG_P2P
1811 	if (os_strcmp(pos, "P2P-CR") == 0) {
1812 		return wpas_ctrl_nfc_get_handover_sel_p2p(
1813 			wpa_s, reply, max_len, ndef, 0);
1814 	}
1815 
1816 	if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1817 		return wpas_ctrl_nfc_get_handover_sel_p2p(
1818 			wpa_s, reply, max_len, ndef, 1);
1819 	}
1820 #endif /* CONFIG_P2P */
1821 
1822 	return -1;
1823 }
1824 
1825 
wpas_ctrl_nfc_report_handover(struct wpa_supplicant * wpa_s,char * cmd)1826 static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1827 					 char *cmd)
1828 {
1829 	size_t len;
1830 	struct wpabuf *req, *sel;
1831 	int ret;
1832 	char *pos, *role, *type, *pos2;
1833 #ifdef CONFIG_P2P
1834 	char *freq;
1835 	int forced_freq = 0;
1836 
1837 	freq = strstr(cmd, " freq=");
1838 	if (freq) {
1839 		*freq = '\0';
1840 		freq += 6;
1841 		forced_freq = atoi(freq);
1842 	}
1843 #endif /* CONFIG_P2P */
1844 
1845 	role = cmd;
1846 	pos = os_strchr(role, ' ');
1847 	if (pos == NULL) {
1848 		wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
1849 		return -1;
1850 	}
1851 	*pos++ = '\0';
1852 
1853 	type = pos;
1854 	pos = os_strchr(type, ' ');
1855 	if (pos == NULL) {
1856 		wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
1857 		return -1;
1858 	}
1859 	*pos++ = '\0';
1860 
1861 	pos2 = os_strchr(pos, ' ');
1862 	if (pos2 == NULL) {
1863 		wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
1864 		return -1;
1865 	}
1866 	*pos2++ = '\0';
1867 
1868 	len = os_strlen(pos);
1869 	if (len & 0x01) {
1870 		wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
1871 		return -1;
1872 	}
1873 	len /= 2;
1874 
1875 	req = wpabuf_alloc(len);
1876 	if (req == NULL) {
1877 		wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
1878 		return -1;
1879 	}
1880 	if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
1881 		wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
1882 		wpabuf_free(req);
1883 		return -1;
1884 	}
1885 
1886 	len = os_strlen(pos2);
1887 	if (len & 0x01) {
1888 		wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
1889 		wpabuf_free(req);
1890 		return -1;
1891 	}
1892 	len /= 2;
1893 
1894 	sel = wpabuf_alloc(len);
1895 	if (sel == NULL) {
1896 		wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
1897 		wpabuf_free(req);
1898 		return -1;
1899 	}
1900 	if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
1901 		wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
1902 		wpabuf_free(req);
1903 		wpabuf_free(sel);
1904 		return -1;
1905 	}
1906 
1907 	wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1908 		   role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1909 
1910 	if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1911 		ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
1912 #ifdef CONFIG_AP
1913 	} else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1914 	{
1915 		ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
1916 		if (ret < 0)
1917 			ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
1918 #endif /* CONFIG_AP */
1919 #ifdef CONFIG_P2P
1920 	} else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1921 	{
1922 		ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
1923 	} else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1924 	{
1925 		ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1926 						   forced_freq);
1927 #endif /* CONFIG_P2P */
1928 	} else {
1929 		wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1930 			   "reported: role=%s type=%s", role, type);
1931 		ret = -1;
1932 	}
1933 	wpabuf_free(req);
1934 	wpabuf_free(sel);
1935 
1936 	if (ret)
1937 		wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1938 
1939 	return ret;
1940 }
1941 
1942 #endif /* CONFIG_WPS_NFC */
1943 
1944 
wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant * wpa_s,char * cmd)1945 static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1946 					     char *cmd)
1947 {
1948 	u8 bssid[ETH_ALEN];
1949 	char *pin;
1950 	char *new_ssid;
1951 	char *new_auth;
1952 	char *new_encr;
1953 	char *new_key;
1954 	struct wps_new_ap_settings ap;
1955 
1956 	pin = os_strchr(cmd, ' ');
1957 	if (pin == NULL)
1958 		return -1;
1959 	*pin++ = '\0';
1960 
1961 	if (hwaddr_aton(cmd, bssid)) {
1962 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1963 			   cmd);
1964 		return -1;
1965 	}
1966 
1967 	new_ssid = os_strchr(pin, ' ');
1968 	if (new_ssid == NULL)
1969 		return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1970 	*new_ssid++ = '\0';
1971 
1972 	new_auth = os_strchr(new_ssid, ' ');
1973 	if (new_auth == NULL)
1974 		return -1;
1975 	*new_auth++ = '\0';
1976 
1977 	new_encr = os_strchr(new_auth, ' ');
1978 	if (new_encr == NULL)
1979 		return -1;
1980 	*new_encr++ = '\0';
1981 
1982 	new_key = os_strchr(new_encr, ' ');
1983 	if (new_key == NULL)
1984 		return -1;
1985 	*new_key++ = '\0';
1986 
1987 	os_memset(&ap, 0, sizeof(ap));
1988 	ap.ssid_hex = new_ssid;
1989 	ap.auth = new_auth;
1990 	ap.encr = new_encr;
1991 	ap.key_hex = new_key;
1992 	return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1993 }
1994 
1995 
1996 #ifdef CONFIG_AP
wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1997 static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1998 						char *cmd, char *buf,
1999 						size_t buflen)
2000 {
2001 	int timeout = 300;
2002 	char *pos;
2003 	const char *pin_txt;
2004 
2005 	if (!wpa_s->ap_iface)
2006 		return -1;
2007 
2008 	pos = os_strchr(cmd, ' ');
2009 	if (pos)
2010 		*pos++ = '\0';
2011 
2012 	if (os_strcmp(cmd, "disable") == 0) {
2013 		wpas_wps_ap_pin_disable(wpa_s);
2014 		return os_snprintf(buf, buflen, "OK\n");
2015 	}
2016 
2017 	if (os_strcmp(cmd, "random") == 0) {
2018 		if (pos)
2019 			timeout = atoi(pos);
2020 		pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
2021 		if (pin_txt == NULL)
2022 			return -1;
2023 		return os_snprintf(buf, buflen, "%s", pin_txt);
2024 	}
2025 
2026 	if (os_strcmp(cmd, "get") == 0) {
2027 		pin_txt = wpas_wps_ap_pin_get(wpa_s);
2028 		if (pin_txt == NULL)
2029 			return -1;
2030 		return os_snprintf(buf, buflen, "%s", pin_txt);
2031 	}
2032 
2033 	if (os_strcmp(cmd, "set") == 0) {
2034 		char *pin;
2035 		if (pos == NULL)
2036 			return -1;
2037 		pin = pos;
2038 		pos = os_strchr(pos, ' ');
2039 		if (pos) {
2040 			*pos++ = '\0';
2041 			timeout = atoi(pos);
2042 		}
2043 		if (os_strlen(pin) > buflen)
2044 			return -1;
2045 		if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
2046 			return -1;
2047 		return os_snprintf(buf, buflen, "%s", pin);
2048 	}
2049 
2050 	return -1;
2051 }
2052 #endif /* CONFIG_AP */
2053 
2054 
2055 #ifdef CONFIG_WPS_ER
wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant * wpa_s,char * cmd)2056 static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
2057 						char *cmd)
2058 {
2059 	char *uuid = cmd, *pin, *pos;
2060 	u8 addr_buf[ETH_ALEN], *addr = NULL;
2061 	pin = os_strchr(uuid, ' ');
2062 	if (pin == NULL)
2063 		return -1;
2064 	*pin++ = '\0';
2065 	pos = os_strchr(pin, ' ');
2066 	if (pos) {
2067 		*pos++ = '\0';
2068 		if (hwaddr_aton(pos, addr_buf) == 0)
2069 			addr = addr_buf;
2070 	}
2071 	return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
2072 }
2073 
2074 
wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant * wpa_s,char * cmd)2075 static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
2076 						  char *cmd)
2077 {
2078 	char *uuid = cmd, *pin;
2079 	pin = os_strchr(uuid, ' ');
2080 	if (pin == NULL)
2081 		return -1;
2082 	*pin++ = '\0';
2083 	return wpas_wps_er_learn(wpa_s, uuid, pin);
2084 }
2085 
2086 
wpa_supplicant_ctrl_iface_wps_er_set_config(struct wpa_supplicant * wpa_s,char * cmd)2087 static int wpa_supplicant_ctrl_iface_wps_er_set_config(
2088 	struct wpa_supplicant *wpa_s, char *cmd)
2089 {
2090 	char *uuid = cmd, *id;
2091 	id = os_strchr(uuid, ' ');
2092 	if (id == NULL)
2093 		return -1;
2094 	*id++ = '\0';
2095 	return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
2096 }
2097 
2098 
wpa_supplicant_ctrl_iface_wps_er_config(struct wpa_supplicant * wpa_s,char * cmd)2099 static int wpa_supplicant_ctrl_iface_wps_er_config(
2100 	struct wpa_supplicant *wpa_s, char *cmd)
2101 {
2102 	char *pin;
2103 	char *new_ssid;
2104 	char *new_auth;
2105 	char *new_encr;
2106 	char *new_key;
2107 	struct wps_new_ap_settings ap;
2108 
2109 	pin = os_strchr(cmd, ' ');
2110 	if (pin == NULL)
2111 		return -1;
2112 	*pin++ = '\0';
2113 
2114 	new_ssid = os_strchr(pin, ' ');
2115 	if (new_ssid == NULL)
2116 		return -1;
2117 	*new_ssid++ = '\0';
2118 
2119 	new_auth = os_strchr(new_ssid, ' ');
2120 	if (new_auth == NULL)
2121 		return -1;
2122 	*new_auth++ = '\0';
2123 
2124 	new_encr = os_strchr(new_auth, ' ');
2125 	if (new_encr == NULL)
2126 		return -1;
2127 	*new_encr++ = '\0';
2128 
2129 	new_key = os_strchr(new_encr, ' ');
2130 	if (new_key == NULL)
2131 		return -1;
2132 	*new_key++ = '\0';
2133 
2134 	os_memset(&ap, 0, sizeof(ap));
2135 	ap.ssid_hex = new_ssid;
2136 	ap.auth = new_auth;
2137 	ap.encr = new_encr;
2138 	ap.key_hex = new_key;
2139 	return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
2140 }
2141 
2142 
2143 #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)2144 static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
2145 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
2146 {
2147 	int ndef;
2148 	struct wpabuf *buf;
2149 	int res;
2150 	char *uuid;
2151 
2152 	uuid = os_strchr(cmd, ' ');
2153 	if (uuid == NULL)
2154 		return -1;
2155 	*uuid++ = '\0';
2156 
2157 	if (os_strcmp(cmd, "WPS") == 0)
2158 		ndef = 0;
2159 	else if (os_strcmp(cmd, "NDEF") == 0)
2160 		ndef = 1;
2161 	else
2162 		return -1;
2163 
2164 	buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
2165 	if (buf == NULL)
2166 		return -1;
2167 
2168 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
2169 					 wpabuf_len(buf));
2170 	reply[res++] = '\n';
2171 	reply[res] = '\0';
2172 
2173 	wpabuf_free(buf);
2174 
2175 	return res;
2176 }
2177 #endif /* CONFIG_WPS_NFC */
2178 #endif /* CONFIG_WPS_ER */
2179 
2180 #endif /* CONFIG_WPS */
2181 
2182 
2183 #ifdef CONFIG_IBSS_RSN
wpa_supplicant_ctrl_iface_ibss_rsn(struct wpa_supplicant * wpa_s,char * addr)2184 static int wpa_supplicant_ctrl_iface_ibss_rsn(
2185 	struct wpa_supplicant *wpa_s, char *addr)
2186 {
2187 	u8 peer[ETH_ALEN];
2188 
2189 	if (hwaddr_aton(addr, peer)) {
2190 		wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
2191 			   "address '%s'", addr);
2192 		return -1;
2193 	}
2194 
2195 	wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
2196 		   MAC2STR(peer));
2197 
2198 	return ibss_rsn_start(wpa_s->ibss_rsn, peer);
2199 }
2200 #endif /* CONFIG_IBSS_RSN */
2201 
2202 
wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant * wpa_s,char * rsp)2203 static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
2204 					      char *rsp)
2205 {
2206 #ifdef IEEE8021X_EAPOL
2207 	char *pos, *id_pos;
2208 	int id;
2209 	struct wpa_ssid *ssid;
2210 
2211 	pos = os_strchr(rsp, '-');
2212 	if (pos == NULL)
2213 		return -1;
2214 	*pos++ = '\0';
2215 	id_pos = pos;
2216 	pos = os_strchr(pos, ':');
2217 	if (pos == NULL)
2218 		return -1;
2219 	*pos++ = '\0';
2220 	id = atoi(id_pos);
2221 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
2222 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2223 			      (u8 *) pos, os_strlen(pos));
2224 
2225 	ssid = wpa_config_get_network(wpa_s->conf, id);
2226 	if (ssid == NULL) {
2227 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2228 			   "to update", id);
2229 		return -1;
2230 	}
2231 
2232 	return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
2233 							 pos);
2234 #else /* IEEE8021X_EAPOL */
2235 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
2236 	return -1;
2237 #endif /* IEEE8021X_EAPOL */
2238 }
2239 
2240 
wpa_supplicant_ctrl_iface_status(struct wpa_supplicant * wpa_s,const char * params,char * buf,size_t buflen)2241 static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
2242 					    const char *params,
2243 					    char *buf, size_t buflen)
2244 {
2245 	char *pos, *end, tmp[30];
2246 	int res, verbose, wps, ret;
2247 #ifdef CONFIG_HS20
2248 	const u8 *hs20;
2249 #endif /* CONFIG_HS20 */
2250 	const u8 *sess_id;
2251 	size_t sess_id_len;
2252 
2253 	if (os_strcmp(params, "-DRIVER") == 0)
2254 		return wpa_drv_status(wpa_s, buf, buflen);
2255 	verbose = os_strcmp(params, "-VERBOSE") == 0;
2256 	wps = os_strcmp(params, "-WPS") == 0;
2257 	pos = buf;
2258 	end = buf + buflen;
2259 	if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
2260 		struct wpa_ssid *ssid = wpa_s->current_ssid;
2261 		ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
2262 				  MAC2STR(wpa_s->bssid));
2263 		if (os_snprintf_error(end - pos, ret))
2264 			return pos - buf;
2265 		pos += ret;
2266 		ret = os_snprintf(pos, end - pos, "freq=%u\n",
2267 				  wpa_s->assoc_freq);
2268 		if (os_snprintf_error(end - pos, ret))
2269 			return pos - buf;
2270 		pos += ret;
2271 		if (ssid) {
2272 			u8 *_ssid = ssid->ssid;
2273 			size_t ssid_len = ssid->ssid_len;
2274 			u8 ssid_buf[SSID_MAX_LEN];
2275 			if (ssid_len == 0) {
2276 				int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
2277 				if (_res < 0)
2278 					ssid_len = 0;
2279 				else
2280 					ssid_len = _res;
2281 				_ssid = ssid_buf;
2282 			}
2283 			ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
2284 					  wpa_ssid_txt(_ssid, ssid_len),
2285 					  ssid->id);
2286 			if (os_snprintf_error(end - pos, ret))
2287 				return pos - buf;
2288 			pos += ret;
2289 
2290 			if (wps && ssid->passphrase &&
2291 			    wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
2292 			    (ssid->mode == WPAS_MODE_AP ||
2293 			     ssid->mode == WPAS_MODE_P2P_GO)) {
2294 				ret = os_snprintf(pos, end - pos,
2295 						  "passphrase=%s\n",
2296 						  ssid->passphrase);
2297 				if (os_snprintf_error(end - pos, ret))
2298 					return pos - buf;
2299 				pos += ret;
2300 			}
2301 			if (ssid->id_str) {
2302 				ret = os_snprintf(pos, end - pos,
2303 						  "id_str=%s\n",
2304 						  ssid->id_str);
2305 				if (os_snprintf_error(end - pos, ret))
2306 					return pos - buf;
2307 				pos += ret;
2308 			}
2309 
2310 			switch (ssid->mode) {
2311 			case WPAS_MODE_INFRA:
2312 				ret = os_snprintf(pos, end - pos,
2313 						  "mode=station\n");
2314 				break;
2315 			case WPAS_MODE_IBSS:
2316 				ret = os_snprintf(pos, end - pos,
2317 						  "mode=IBSS\n");
2318 				break;
2319 			case WPAS_MODE_AP:
2320 				ret = os_snprintf(pos, end - pos,
2321 						  "mode=AP\n");
2322 				break;
2323 			case WPAS_MODE_P2P_GO:
2324 				ret = os_snprintf(pos, end - pos,
2325 						  "mode=P2P GO\n");
2326 				break;
2327 			case WPAS_MODE_P2P_GROUP_FORMATION:
2328 				ret = os_snprintf(pos, end - pos,
2329 						  "mode=P2P GO - group "
2330 						  "formation\n");
2331 				break;
2332 			case WPAS_MODE_MESH:
2333 				ret = os_snprintf(pos, end - pos,
2334 						  "mode=mesh\n");
2335 				break;
2336 			default:
2337 				ret = 0;
2338 				break;
2339 			}
2340 			if (os_snprintf_error(end - pos, ret))
2341 				return pos - buf;
2342 			pos += ret;
2343 		}
2344 
2345 		if (wpa_s->connection_set &&
2346 		    (wpa_s->connection_ht || wpa_s->connection_vht ||
2347 		     wpa_s->connection_he || wpa_s->connection_eht)) {
2348 			ret = os_snprintf(pos, end - pos,
2349 					  "wifi_generation=%u\n",
2350 					  wpa_s->connection_eht ? 7 :
2351 					  (wpa_s->connection_he ? 6 :
2352 					   (wpa_s->connection_vht ? 5 : 4)));
2353 			if (os_snprintf_error(end - pos, ret))
2354 				return pos - buf;
2355 			pos += ret;
2356 		}
2357 
2358 #ifdef CONFIG_AP
2359 		if (wpa_s->ap_iface) {
2360 			pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
2361 							    end - pos,
2362 							    verbose);
2363 		} else
2364 #endif /* CONFIG_AP */
2365 		pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
2366 	}
2367 #ifdef CONFIG_SME
2368 #ifdef CONFIG_SAE
2369 	if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
2370 #ifdef CONFIG_AP
2371 	    !wpa_s->ap_iface &&
2372 #endif /* CONFIG_AP */
2373 	    wpa_s->sme.sae.state == SAE_ACCEPTED) {
2374 		ret = os_snprintf(pos, end - pos, "sae_group=%d\n"
2375 				  "sae_h2e=%d\n"
2376 				  "sae_pk=%d\n",
2377 				  wpa_s->sme.sae.group,
2378 				  wpa_s->sme.sae.h2e,
2379 				  wpa_s->sme.sae.pk);
2380 		if (os_snprintf_error(end - pos, ret))
2381 			return pos - buf;
2382 		pos += ret;
2383 	}
2384 #endif /* CONFIG_SAE */
2385 #endif /* CONFIG_SME */
2386 	ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
2387 			  wpa_supplicant_state_txt(wpa_s->wpa_state));
2388 	if (os_snprintf_error(end - pos, ret))
2389 		return pos - buf;
2390 	pos += ret;
2391 
2392 	if (wpa_s->l2 &&
2393 	    l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
2394 		ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
2395 		if (os_snprintf_error(end - pos, ret))
2396 			return pos - buf;
2397 		pos += ret;
2398 	}
2399 
2400 #ifdef CONFIG_P2P
2401 	if (wpa_s->global->p2p) {
2402 		ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
2403 				  "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
2404 		if (os_snprintf_error(end - pos, ret))
2405 			return pos - buf;
2406 		pos += ret;
2407 	}
2408 #endif /* CONFIG_P2P */
2409 
2410 	ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
2411 			  MAC2STR(wpa_s->own_addr));
2412 	if (os_snprintf_error(end - pos, ret))
2413 		return pos - buf;
2414 	pos += ret;
2415 
2416 	if (wpa_s->valid_links) {
2417 		ret = os_snprintf(pos, end - pos, "ap_mld_addr=" MACSTR "\n",
2418 				  MAC2STR(wpa_s->ap_mld_addr));
2419 		if (os_snprintf_error(end - pos, ret))
2420 			return pos - buf;
2421 		pos += ret;
2422 	}
2423 
2424 #ifdef CONFIG_HS20
2425 	if (wpa_s->current_bss &&
2426 	    (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2427 					  HS20_IE_VENDOR_TYPE)) &&
2428 	    wpa_s->wpa_proto == WPA_PROTO_RSN &&
2429 	    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2430 		int release = 1;
2431 		if (hs20[1] >= 5) {
2432 			u8 rel_num = (hs20[6] & 0xf0) >> 4;
2433 			release = rel_num + 1;
2434 		}
2435 		ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
2436 		if (os_snprintf_error(end - pos, ret))
2437 			return pos - buf;
2438 		pos += ret;
2439 	}
2440 
2441 	if (wpa_s->current_ssid) {
2442 		struct wpa_cred *cred;
2443 		char *type;
2444 
2445 		for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2446 			size_t i;
2447 
2448 			if (wpa_s->current_ssid->parent_cred != cred)
2449 				continue;
2450 
2451 			if (cred->provisioning_sp) {
2452 				ret = os_snprintf(pos, end - pos,
2453 						  "provisioning_sp=%s\n",
2454 						  cred->provisioning_sp);
2455 				if (os_snprintf_error(end - pos, ret))
2456 					return pos - buf;
2457 				pos += ret;
2458 			}
2459 
2460 			if (!cred->domain)
2461 				goto no_domain;
2462 
2463 			i = 0;
2464 			if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
2465 				struct wpabuf *names =
2466 					wpa_s->current_bss->anqp->domain_name;
2467 				for (i = 0; names && i < cred->num_domain; i++)
2468 				{
2469 					if (domain_name_list_contains(
2470 						    names, cred->domain[i], 1))
2471 						break;
2472 				}
2473 				if (i == cred->num_domain)
2474 					i = 0; /* show first entry by default */
2475 			}
2476 			ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
2477 					  cred->domain[i]);
2478 			if (os_snprintf_error(end - pos, ret))
2479 				return pos - buf;
2480 			pos += ret;
2481 
2482 		no_domain:
2483 			if (wpa_s->current_bss == NULL ||
2484 			    wpa_s->current_bss->anqp == NULL)
2485 				res = -1;
2486 			else
2487 				res = interworking_home_sp_cred(
2488 					wpa_s, cred,
2489 					wpa_s->current_bss->anqp->domain_name);
2490 			if (res > 0)
2491 				type = "home";
2492 			else if (res == 0)
2493 				type = "roaming";
2494 			else
2495 				type = "unknown";
2496 
2497 			ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
2498 			if (os_snprintf_error(end - pos, ret))
2499 				return pos - buf;
2500 			pos += ret;
2501 
2502 			break;
2503 		}
2504 	}
2505 #endif /* CONFIG_HS20 */
2506 
2507 	if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2508 	    wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2509 		res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
2510 					  verbose);
2511 		if (res >= 0)
2512 			pos += res;
2513 	}
2514 
2515 #ifdef CONFIG_MACSEC
2516 	res = ieee802_1x_kay_get_status(wpa_s->kay, pos, end - pos);
2517 	if (res > 0)
2518 		pos += res;
2519 #endif /* CONFIG_MACSEC */
2520 
2521 	sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
2522 	if (sess_id) {
2523 		char *start = pos;
2524 
2525 		ret = os_snprintf(pos, end - pos, "eap_session_id=");
2526 		if (os_snprintf_error(end - pos, ret))
2527 			return start - buf;
2528 		pos += ret;
2529 		ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
2530 		if (ret <= 0)
2531 			return start - buf;
2532 		pos += ret;
2533 		ret = os_snprintf(pos, end - pos, "\n");
2534 		if (os_snprintf_error(end - pos, ret))
2535 			return start - buf;
2536 		pos += ret;
2537 	}
2538 
2539 	res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
2540 	if (res >= 0)
2541 		pos += res;
2542 
2543 #ifdef CONFIG_WPS
2544 	{
2545 		char uuid_str[100];
2546 		uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
2547 		ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
2548 		if (os_snprintf_error(end - pos, ret))
2549 			return pos - buf;
2550 		pos += ret;
2551 	}
2552 #endif /* CONFIG_WPS */
2553 
2554 	if (wpa_s->ieee80211ac) {
2555 		ret = os_snprintf(pos, end - pos, "ieee80211ac=1\n");
2556 		if (os_snprintf_error(end - pos, ret))
2557 			return pos - buf;
2558 		pos += ret;
2559 	}
2560 
2561 #ifdef CONFIG_SME
2562 	if (wpa_s->sme.bss_max_idle_period) {
2563 		ret = os_snprintf(pos, end - pos, "bss_max_idle_period=%d\n",
2564 				  wpa_s->sme.bss_max_idle_period);
2565 		if (os_snprintf_error(end - pos, ret))
2566 			return pos - buf;
2567 		pos += ret;
2568 	}
2569 #endif /* CONFIG_SME */
2570 
2571 	if (wpa_s->ssid_verified) {
2572 		ret = os_snprintf(pos, end - pos, "ssid_verified=1\n");
2573 		if (os_snprintf_error(end - pos, ret))
2574 			return pos - buf;
2575 		pos += ret;
2576 	}
2577 
2578 	if (wpa_s->bigtk_set) {
2579 		ret = os_snprintf(pos, end - pos, "bigtk_set=1\n");
2580 		if (os_snprintf_error(end - pos, ret))
2581 			return pos - buf;
2582 		pos += ret;
2583 	}
2584 
2585 #ifdef ANDROID
2586 	/*
2587 	 * Allow using the STATUS command with default behavior, say for debug,
2588 	 * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
2589 	 * events with STATUS-NO_EVENTS.
2590 	 */
2591 	if (os_strcmp(params, "-NO_EVENTS")) {
2592 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
2593 			     "id=%d state=%d BSSID=" MACSTR " SSID=%s",
2594 			     wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
2595 			     wpa_s->wpa_state,
2596 			     MAC2STR(wpa_s->bssid),
2597 			     wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
2598 			     wpa_ssid_txt(wpa_s->current_ssid->ssid,
2599 					  wpa_s->current_ssid->ssid_len) : "");
2600 		if (wpa_s->wpa_state == WPA_COMPLETED) {
2601 			struct wpa_ssid *ssid = wpa_s->current_ssid;
2602 			char mld_addr[50];
2603 
2604 			mld_addr[0] = '\0';
2605 			if (wpa_s->valid_links)
2606 				os_snprintf(mld_addr, sizeof(mld_addr),
2607 					    " ap_mld_addr=" MACSTR,
2608 					    MAC2STR(wpa_s->ap_mld_addr));
2609 
2610 			wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
2611 				     "- connection to " MACSTR
2612 				     " completed %s [id=%d id_str=%s]%s",
2613 				     MAC2STR(wpa_s->bssid), "(auth)",
2614 				     ssid ? ssid->id : -1,
2615 				     ssid && ssid->id_str ? ssid->id_str : "",
2616 				     mld_addr);
2617 		}
2618 	}
2619 #endif /* ANDROID */
2620 
2621 	return pos - buf;
2622 }
2623 
2624 
wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant * wpa_s,char * cmd)2625 static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
2626 					   char *cmd)
2627 {
2628 	char *pos;
2629 	int id;
2630 	struct wpa_ssid *ssid;
2631 	u8 bssid[ETH_ALEN];
2632 
2633 	/* cmd: "<network id> <BSSID>" */
2634 	pos = os_strchr(cmd, ' ');
2635 	if (pos == NULL)
2636 		return -1;
2637 	*pos++ = '\0';
2638 	id = atoi(cmd);
2639 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
2640 	if (hwaddr_aton(pos, bssid)) {
2641 		wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
2642 		return -1;
2643 	}
2644 
2645 	ssid = wpa_config_get_network(wpa_s->conf, id);
2646 	if (ssid == NULL) {
2647 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2648 			   "to update", id);
2649 		return -1;
2650 	}
2651 
2652 	os_memcpy(ssid->bssid, bssid, ETH_ALEN);
2653 	ssid->bssid_set = !is_zero_ether_addr(bssid);
2654 
2655 	return 0;
2656 }
2657 
2658 
wpa_supplicant_ctrl_iface_bssid_ignore(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2659 static int wpa_supplicant_ctrl_iface_bssid_ignore(struct wpa_supplicant *wpa_s,
2660 						  char *cmd, char *buf,
2661 						  size_t buflen)
2662 {
2663 	u8 bssid[ETH_ALEN];
2664 	struct wpa_bssid_ignore *e;
2665 	char *pos, *end;
2666 	int ret;
2667 
2668 	/* cmd: "BSSID_IGNORE [<BSSID>]" */
2669 	if (*cmd == '\0') {
2670 		pos = buf;
2671 		end = buf + buflen;
2672 		e = wpa_s->bssid_ignore;
2673 		while (e) {
2674 			ret = os_snprintf(pos, end - pos, MACSTR "\n",
2675 					  MAC2STR(e->bssid));
2676 			if (os_snprintf_error(end - pos, ret))
2677 				return pos - buf;
2678 			pos += ret;
2679 			e = e->next;
2680 		}
2681 		return pos - buf;
2682 	}
2683 
2684 	cmd++;
2685 	if (os_strncmp(cmd, "clear", 5) == 0) {
2686 		wpa_bssid_ignore_clear(wpa_s);
2687 		os_memcpy(buf, "OK\n", 3);
2688 		return 3;
2689 	}
2690 
2691 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: BSSID_IGNORE bssid='%s'", cmd);
2692 	if (hwaddr_aton(cmd, bssid)) {
2693 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
2694 		return -1;
2695 	}
2696 
2697 	/*
2698 	 * Add the BSSID twice, so its count will be 2, causing it to be
2699 	 * skipped when processing scan results.
2700 	 */
2701 	ret = wpa_bssid_ignore_add(wpa_s, bssid);
2702 	if (ret < 0)
2703 		return -1;
2704 	ret = wpa_bssid_ignore_add(wpa_s, bssid);
2705 	if (ret < 0)
2706 		return -1;
2707 	os_memcpy(buf, "OK\n", 3);
2708 	return 3;
2709 }
2710 
2711 
wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2712 static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
2713 					       char *cmd, char *buf,
2714 					       size_t buflen)
2715 {
2716 	char *pos, *end, *stamp;
2717 	int ret;
2718 
2719 	/* cmd: "LOG_LEVEL [<level>]" */
2720 	if (*cmd == '\0') {
2721 		pos = buf;
2722 		end = buf + buflen;
2723 		ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2724 				  "Timestamp: %d\n",
2725 				  debug_level_str(wpa_debug_level),
2726 				  wpa_debug_timestamp);
2727 		if (os_snprintf_error(end - pos, ret))
2728 			ret = 0;
2729 
2730 		return ret;
2731 	}
2732 
2733 	while (*cmd == ' ')
2734 		cmd++;
2735 
2736 	stamp = os_strchr(cmd, ' ');
2737 	if (stamp) {
2738 		*stamp++ = '\0';
2739 		while (*stamp == ' ') {
2740 			stamp++;
2741 		}
2742 	}
2743 
2744 	if (os_strlen(cmd)) {
2745 		int level = str_to_debug_level(cmd);
2746 		if (level < 0)
2747 			return -1;
2748 		wpa_debug_level = level;
2749 	}
2750 
2751 	if (stamp && os_strlen(stamp))
2752 		wpa_debug_timestamp = atoi(stamp);
2753 
2754 	os_memcpy(buf, "OK\n", 3);
2755 	return 3;
2756 }
2757 
2758 
wpa_supplicant_ctrl_iface_list_networks(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2759 static int wpa_supplicant_ctrl_iface_list_networks(
2760 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2761 {
2762 	char *pos, *end, *prev;
2763 	struct wpa_ssid *ssid;
2764 	int ret;
2765 
2766 	pos = buf;
2767 	end = buf + buflen;
2768 	ret = os_snprintf(pos, end - pos,
2769 			  "network id / ssid / bssid / flags\n");
2770 	if (os_snprintf_error(end - pos, ret))
2771 		return pos - buf;
2772 	pos += ret;
2773 
2774 	ssid = wpa_s->conf->ssid;
2775 
2776 	/* skip over ssids until we find next one */
2777 	if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
2778 		int last_id = atoi(cmd + 8);
2779 		if (last_id != -1) {
2780 			while (ssid != NULL && ssid->id <= last_id) {
2781 				ssid = ssid->next;
2782 			}
2783 		}
2784 	}
2785 
2786 	while (ssid) {
2787 		prev = pos;
2788 		ret = os_snprintf(pos, end - pos, "%d\t%s",
2789 				  ssid->id,
2790 				  wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2791 		if (os_snprintf_error(end - pos, ret))
2792 			return prev - buf;
2793 		pos += ret;
2794 		if (ssid->bssid_set) {
2795 			ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2796 					  MAC2STR(ssid->bssid));
2797 		} else {
2798 			ret = os_snprintf(pos, end - pos, "\tany");
2799 		}
2800 		if (os_snprintf_error(end - pos, ret))
2801 			return prev - buf;
2802 		pos += ret;
2803 		ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
2804 				  ssid == wpa_s->current_ssid ?
2805 				  "[CURRENT]" : "",
2806 				  ssid->disabled ? "[DISABLED]" : "",
2807 				  ssid->disabled_until.sec ?
2808 				  "[TEMP-DISABLED]" : "",
2809 				  ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2810 				  "");
2811 		if (os_snprintf_error(end - pos, ret))
2812 			return prev - buf;
2813 		pos += ret;
2814 		ret = os_snprintf(pos, end - pos, "\n");
2815 		if (os_snprintf_error(end - pos, ret))
2816 			return prev - buf;
2817 		pos += ret;
2818 
2819 		ssid = ssid->next;
2820 	}
2821 
2822 	return pos - buf;
2823 }
2824 
2825 
wpa_supplicant_cipher_txt(char * pos,char * end,int cipher)2826 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2827 {
2828 	int ret;
2829 	ret = os_snprintf(pos, end - pos, "-");
2830 	if (os_snprintf_error(end - pos, ret))
2831 		return pos;
2832 	pos += ret;
2833 	ret = wpa_write_ciphers(pos, end, cipher, "+");
2834 	if (ret < 0)
2835 		return pos;
2836 	pos += ret;
2837 	return pos;
2838 }
2839 
2840 
wpa_supplicant_ie_txt(char * pos,char * end,const char * proto,const u8 * ie,size_t ie_len)2841 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2842 				    const u8 *ie, size_t ie_len)
2843 {
2844 	struct wpa_ie_data data;
2845 	char *start;
2846 	int ret;
2847 
2848 	ret = os_snprintf(pos, end - pos, "[%s-", proto);
2849 	if (os_snprintf_error(end - pos, ret))
2850 		return pos;
2851 	pos += ret;
2852 
2853 	if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2854 		ret = os_snprintf(pos, end - pos, "?]");
2855 		if (os_snprintf_error(end - pos, ret))
2856 			return pos;
2857 		pos += ret;
2858 		return pos;
2859 	}
2860 
2861 	start = pos;
2862 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
2863 		ret = os_snprintf(pos, end - pos, "%sEAP",
2864 				  pos == start ? "" : "+");
2865 		if (os_snprintf_error(end - pos, ret))
2866 			return pos;
2867 		pos += ret;
2868 	}
2869 	if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
2870 		ret = os_snprintf(pos, end - pos, "%sPSK",
2871 				  pos == start ? "" : "+");
2872 		if (os_snprintf_error(end - pos, ret))
2873 			return pos;
2874 		pos += ret;
2875 	}
2876 	if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
2877 		ret = os_snprintf(pos, end - pos, "%sNone",
2878 				  pos == start ? "" : "+");
2879 		if (os_snprintf_error(end - pos, ret))
2880 			return pos;
2881 		pos += ret;
2882 	}
2883 	if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
2884 		ret = os_snprintf(pos, end - pos, "%sSAE",
2885 				  pos == start ? "" : "+");
2886 		if (os_snprintf_error(end - pos, ret))
2887 			return pos;
2888 		pos += ret;
2889 	}
2890 	if (data.key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
2891 		ret = os_snprintf(pos, end - pos, "%sSAE-EXT-KEY",
2892 				  pos == start ? "" : "+");
2893 		if (os_snprintf_error(end - pos, ret))
2894 			return pos;
2895 		pos += ret;
2896 	}
2897 #ifdef CONFIG_IEEE80211R
2898 	if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2899 		ret = os_snprintf(pos, end - pos, "%sFT/EAP",
2900 				  pos == start ? "" : "+");
2901 		if (os_snprintf_error(end - pos, ret))
2902 			return pos;
2903 		pos += ret;
2904 	}
2905 	if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2906 		ret = os_snprintf(pos, end - pos, "%sFT/PSK",
2907 				  pos == start ? "" : "+");
2908 		if (os_snprintf_error(end - pos, ret))
2909 			return pos;
2910 		pos += ret;
2911 	}
2912 	if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
2913 		ret = os_snprintf(pos, end - pos, "%sFT/SAE",
2914 				  pos == start ? "" : "+");
2915 		if (os_snprintf_error(end - pos, ret))
2916 			return pos;
2917 		pos += ret;
2918 	}
2919 	if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
2920 		ret = os_snprintf(pos, end - pos, "%sFT/SAE-EXT-KEY",
2921 				  pos == start ? "" : "+");
2922 		if (os_snprintf_error(end - pos, ret))
2923 			return pos;
2924 		pos += ret;
2925 	}
2926 #endif /* CONFIG_IEEE80211R */
2927 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2928 		ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
2929 				  pos == start ? "" : "+");
2930 		if (os_snprintf_error(end - pos, ret))
2931 			return pos;
2932 		pos += ret;
2933 	}
2934 	if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2935 		ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
2936 				  pos == start ? "" : "+");
2937 		if (os_snprintf_error(end - pos, ret))
2938 			return pos;
2939 		pos += ret;
2940 	}
2941 
2942 #ifdef CONFIG_SUITEB
2943 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
2944 		ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
2945 				  pos == start ? "" : "+");
2946 		if (os_snprintf_error(end - pos, ret))
2947 			return pos;
2948 		pos += ret;
2949 	}
2950 #endif /* CONFIG_SUITEB */
2951 
2952 #ifdef CONFIG_SUITEB192
2953 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
2954 		ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
2955 				  pos == start ? "" : "+");
2956 		if (os_snprintf_error(end - pos, ret))
2957 			return pos;
2958 		pos += ret;
2959 	}
2960 #endif /* CONFIG_SUITEB192 */
2961 
2962 #ifdef CONFIG_FILS
2963 	if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
2964 		ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
2965 				  pos == start ? "" : "+");
2966 		if (os_snprintf_error(end - pos, ret))
2967 			return pos;
2968 		pos += ret;
2969 	}
2970 	if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
2971 		ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
2972 				  pos == start ? "" : "+");
2973 		if (os_snprintf_error(end - pos, ret))
2974 			return pos;
2975 		pos += ret;
2976 	}
2977 #ifdef CONFIG_IEEE80211R
2978 	if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
2979 		ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
2980 				  pos == start ? "" : "+");
2981 		if (os_snprintf_error(end - pos, ret))
2982 			return pos;
2983 		pos += ret;
2984 	}
2985 	if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
2986 		ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
2987 				  pos == start ? "" : "+");
2988 		if (os_snprintf_error(end - pos, ret))
2989 			return pos;
2990 		pos += ret;
2991 	}
2992 #endif /* CONFIG_IEEE80211R */
2993 #endif /* CONFIG_FILS */
2994 
2995 #ifdef CONFIG_OWE
2996 	if (data.key_mgmt & WPA_KEY_MGMT_OWE) {
2997 		ret = os_snprintf(pos, end - pos, "%sOWE",
2998 				  pos == start ? "" : "+");
2999 		if (os_snprintf_error(end - pos, ret))
3000 			return pos;
3001 		pos += ret;
3002 	}
3003 #endif /* CONFIG_OWE */
3004 
3005 #ifdef CONFIG_DPP
3006 	if (data.key_mgmt & WPA_KEY_MGMT_DPP) {
3007 		ret = os_snprintf(pos, end - pos, "%sDPP",
3008 				  pos == start ? "" : "+");
3009 		if (os_snprintf_error(end - pos, ret))
3010 			return pos;
3011 		pos += ret;
3012 	}
3013 #endif /* CONFIG_DPP */
3014 
3015 	if (data.key_mgmt & WPA_KEY_MGMT_OSEN) {
3016 		ret = os_snprintf(pos, end - pos, "%sOSEN",
3017 				  pos == start ? "" : "+");
3018 		if (os_snprintf_error(end - pos, ret))
3019 			return pos;
3020 		pos += ret;
3021 	}
3022 
3023 #ifdef CONFIG_SHA384
3024 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA384) {
3025 		ret = os_snprintf(pos, end - pos, "%sEAP-SHA384",
3026 				  pos == start ? "" : "+");
3027 		if (os_snprintf_error(end - pos, ret))
3028 			return pos;
3029 		pos += ret;
3030 	}
3031 #endif /* CONFIG_SHA384 */
3032 
3033 	pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
3034 
3035 	if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
3036 		ret = os_snprintf(pos, end - pos, "-preauth");
3037 		if (os_snprintf_error(end - pos, ret))
3038 			return pos;
3039 		pos += ret;
3040 	}
3041 
3042 	ret = os_snprintf(pos, end - pos, "]");
3043 	if (os_snprintf_error(end - pos, ret))
3044 		return pos;
3045 	pos += ret;
3046 
3047 	return pos;
3048 }
3049 
3050 
3051 #ifdef CONFIG_WPS
wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant * wpa_s,char * pos,char * end,struct wpabuf * wps_ie)3052 static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
3053 					    char *pos, char *end,
3054 					    struct wpabuf *wps_ie)
3055 {
3056 	int ret;
3057 	const char *txt;
3058 
3059 	if (wps_ie == NULL)
3060 		return pos;
3061 	if (wps_is_selected_pbc_registrar(wps_ie))
3062 		txt = "[WPS-PBC]";
3063 	else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
3064 		txt = "[WPS-AUTH]";
3065 	else if (wps_is_selected_pin_registrar(wps_ie))
3066 		txt = "[WPS-PIN]";
3067 	else
3068 		txt = "[WPS]";
3069 
3070 	ret = os_snprintf(pos, end - pos, "%s", txt);
3071 	if (!os_snprintf_error(end - pos, ret))
3072 		pos += ret;
3073 	wpabuf_free(wps_ie);
3074 	return pos;
3075 }
3076 #endif /* CONFIG_WPS */
3077 
3078 
wpa_supplicant_wps_ie_txt(struct wpa_supplicant * wpa_s,char * pos,char * end,const struct wpa_bss * bss)3079 static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
3080 					char *pos, char *end,
3081 					const struct wpa_bss *bss)
3082 {
3083 #ifdef CONFIG_WPS
3084 	struct wpabuf *wps_ie;
3085 	wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
3086 	return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
3087 #else /* CONFIG_WPS */
3088 	return pos;
3089 #endif /* CONFIG_WPS */
3090 }
3091 
3092 
3093 /* 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)3094 static int wpa_supplicant_ctrl_iface_scan_result(
3095 	struct wpa_supplicant *wpa_s,
3096 	const struct wpa_bss *bss, char *buf, size_t buflen)
3097 {
3098 	char *pos, *end;
3099 	int ret;
3100 	const u8 *ie, *ie2, *osen_ie, *p2p, *mesh, *owe, *rsnxe;
3101 
3102 	mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
3103 	p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
3104 	if (!p2p)
3105 		p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
3106 	if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
3107 	    os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
3108 	    0)
3109 		return 0; /* Do not show P2P listen discovery results here */
3110 
3111 	pos = buf;
3112 	end = buf + buflen;
3113 
3114 	ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
3115 			  MAC2STR(bss->bssid), bss->freq, bss->level);
3116 	if (os_snprintf_error(end - pos, ret))
3117 		return -1;
3118 	pos += ret;
3119 	ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3120 	if (ie)
3121 		pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
3122 	ie2 = wpa_bss_get_rsne(wpa_s, bss, NULL, false);
3123 	if (ie2) {
3124 		pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
3125 					    ie2, 2 + ie2[1]);
3126 	}
3127 	rsnxe = wpa_bss_get_rsnxe(wpa_s, bss, NULL, false);
3128 	if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_H2E)) {
3129 		ret = os_snprintf(pos, end - pos, "[SAE-H2E]");
3130 		if (os_snprintf_error(end - pos, ret))
3131 			return -1;
3132 		pos += ret;
3133 	}
3134 	if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_PK)) {
3135 		ret = os_snprintf(pos, end - pos, "[SAE-PK]");
3136 		if (os_snprintf_error(end - pos, ret))
3137 			return -1;
3138 		pos += ret;
3139 	}
3140 	osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
3141 	if (osen_ie)
3142 		pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
3143 					    osen_ie, 2 + osen_ie[1]);
3144 	owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
3145 	if (owe) {
3146 		ret = os_snprintf(pos, end - pos,
3147 				  ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
3148 		if (os_snprintf_error(end - pos, ret))
3149 			return -1;
3150 		pos += ret;
3151 	}
3152 	pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3153 	if (!ie && !ie2 && !osen_ie && (bss->caps & IEEE80211_CAP_PRIVACY)) {
3154 		ret = os_snprintf(pos, end - pos, "[WEP]");
3155 		if (os_snprintf_error(end - pos, ret))
3156 			return -1;
3157 		pos += ret;
3158 	}
3159 	if (mesh) {
3160 		ret = os_snprintf(pos, end - pos, "[MESH]");
3161 		if (os_snprintf_error(end - pos, ret))
3162 			return -1;
3163 		pos += ret;
3164 	}
3165 	if (bss_is_dmg(bss)) {
3166 		const char *s;
3167 
3168 		if (wpa_bss_get_ie_ext(bss, WLAN_EID_EXT_EDMG_OPERATION)) {
3169 			ret = os_snprintf(pos, end - pos, "[EDMG]");
3170 			if (os_snprintf_error(end - pos, ret))
3171 				return -1;
3172 			pos += ret;
3173 		}
3174 
3175 		ret = os_snprintf(pos, end - pos, "[DMG]");
3176 		if (os_snprintf_error(end - pos, ret))
3177 			return -1;
3178 		pos += ret;
3179 		switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
3180 		case IEEE80211_CAP_DMG_IBSS:
3181 			s = "[IBSS]";
3182 			break;
3183 		case IEEE80211_CAP_DMG_AP:
3184 			s = "[ESS]";
3185 			break;
3186 		case IEEE80211_CAP_DMG_PBSS:
3187 			s = "[PBSS]";
3188 			break;
3189 		default:
3190 			s = "";
3191 			break;
3192 		}
3193 		ret = os_snprintf(pos, end - pos, "%s", s);
3194 		if (os_snprintf_error(end - pos, ret))
3195 			return -1;
3196 		pos += ret;
3197 	} else {
3198 		if (bss->caps & IEEE80211_CAP_IBSS) {
3199 			ret = os_snprintf(pos, end - pos, "[IBSS]");
3200 			if (os_snprintf_error(end - pos, ret))
3201 				return -1;
3202 			pos += ret;
3203 		}
3204 		if (bss->caps & IEEE80211_CAP_ESS) {
3205 			ret = os_snprintf(pos, end - pos, "[ESS]");
3206 			if (os_snprintf_error(end - pos, ret))
3207 				return -1;
3208 			pos += ret;
3209 		}
3210 	}
3211 	if (p2p) {
3212 		ret = os_snprintf(pos, end - pos, "[P2P]");
3213 		if (os_snprintf_error(end - pos, ret))
3214 			return -1;
3215 		pos += ret;
3216 	}
3217 #ifdef CONFIG_HS20
3218 	if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
3219 		ret = os_snprintf(pos, end - pos, "[HS20]");
3220 		if (os_snprintf_error(end - pos, ret))
3221 			return -1;
3222 		pos += ret;
3223 	}
3224 #endif /* CONFIG_HS20 */
3225 #ifdef CONFIG_FILS
3226 	if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
3227 		ret = os_snprintf(pos, end - pos, "[FILS]");
3228 		if (os_snprintf_error(end - pos, ret))
3229 			return -1;
3230 		pos += ret;
3231 	}
3232 #endif /* CONFIG_FILS */
3233 #ifdef CONFIG_FST
3234 	if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
3235 		ret = os_snprintf(pos, end - pos, "[FST]");
3236 		if (os_snprintf_error(end - pos, ret))
3237 			return -1;
3238 		pos += ret;
3239 	}
3240 #endif /* CONFIG_FST */
3241 	if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_UTF_8_SSID)) {
3242 		ret = os_snprintf(pos, end - pos, "[UTF-8]");
3243 		if (os_snprintf_error(end - pos, ret))
3244 			return -1;
3245 		pos += ret;
3246 	}
3247 
3248 	ret = os_snprintf(pos, end - pos, "\t%s",
3249 			  wpa_ssid_txt(bss->ssid, bss->ssid_len));
3250 	if (os_snprintf_error(end - pos, ret))
3251 		return -1;
3252 	pos += ret;
3253 
3254 	ret = os_snprintf(pos, end - pos, "\n");
3255 	if (os_snprintf_error(end - pos, ret))
3256 		return -1;
3257 	pos += ret;
3258 
3259 	return pos - buf;
3260 }
3261 
3262 
wpa_supplicant_ctrl_iface_scan_results(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3263 static int wpa_supplicant_ctrl_iface_scan_results(
3264 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
3265 {
3266 	char *pos, *end;
3267 	struct wpa_bss *bss;
3268 	int ret;
3269 
3270 	pos = buf;
3271 	end = buf + buflen;
3272 	ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
3273 			  "flags / ssid\n");
3274 	if (os_snprintf_error(end - pos, ret))
3275 		return pos - buf;
3276 	pos += ret;
3277 
3278 	dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
3279 		ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
3280 							    end - pos);
3281 		if (ret < 0 || ret >= end - pos)
3282 			return pos - buf;
3283 		pos += ret;
3284 	}
3285 
3286 	return pos - buf;
3287 }
3288 
3289 
3290 #ifdef CONFIG_MESH
3291 
wpa_supplicant_ctrl_iface_mesh_interface_add(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)3292 static int wpa_supplicant_ctrl_iface_mesh_interface_add(
3293 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
3294 {
3295 	char *pos, ifname[IFNAMSIZ + 1];
3296 
3297 	ifname[0] = '\0';
3298 
3299 	pos = os_strstr(cmd, "ifname=");
3300 	if (pos) {
3301 		pos += 7;
3302 		os_strlcpy(ifname, pos, sizeof(ifname));
3303 	}
3304 
3305 	if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
3306 		return -1;
3307 
3308 	os_strlcpy(reply, ifname, max_len);
3309 	return os_strlen(ifname);
3310 }
3311 
3312 
wpa_supplicant_ctrl_iface_mesh_group_add(struct wpa_supplicant * wpa_s,char * cmd)3313 static int wpa_supplicant_ctrl_iface_mesh_group_add(
3314 	struct wpa_supplicant *wpa_s, char *cmd)
3315 {
3316 	int id;
3317 	struct wpa_ssid *ssid;
3318 
3319 	id = atoi(cmd);
3320 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
3321 
3322 	ssid = wpa_config_get_network(wpa_s->conf, id);
3323 	if (ssid == NULL) {
3324 		wpa_printf(MSG_DEBUG,
3325 			   "CTRL_IFACE: Could not find network id=%d", id);
3326 		return -1;
3327 	}
3328 	if (ssid->mode != WPAS_MODE_MESH) {
3329 		wpa_printf(MSG_DEBUG,
3330 			   "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
3331 		return -1;
3332 	}
3333 	if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
3334 	    ssid->key_mgmt != WPA_KEY_MGMT_SAE &&
3335 	    ssid->key_mgmt != WPA_KEY_MGMT_SAE_EXT_KEY) {
3336 		wpa_printf(MSG_ERROR,
3337 			   "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
3338 		return -1;
3339 	}
3340 
3341 	/*
3342 	 * TODO: If necessary write our own group_add function,
3343 	 * for now we can reuse select_network
3344 	 */
3345 	wpa_supplicant_select_network(wpa_s, ssid);
3346 
3347 	return 0;
3348 }
3349 
3350 
wpa_supplicant_ctrl_iface_mesh_group_remove(struct wpa_supplicant * wpa_s,char * cmd)3351 static int wpa_supplicant_ctrl_iface_mesh_group_remove(
3352 	struct wpa_supplicant *wpa_s, char *cmd)
3353 {
3354 	struct wpa_supplicant *orig;
3355 	struct wpa_global *global;
3356 	int found = 0;
3357 
3358 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
3359 
3360 	global = wpa_s->global;
3361 	orig = wpa_s;
3362 
3363 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3364 		if (os_strcmp(wpa_s->ifname, cmd) == 0) {
3365 			found = 1;
3366 			break;
3367 		}
3368 	}
3369 	if (!found) {
3370 		wpa_printf(MSG_ERROR,
3371 			   "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
3372 			   cmd);
3373 		return -1;
3374 	}
3375 	if (wpa_s->mesh_if_created && wpa_s == orig) {
3376 		wpa_printf(MSG_ERROR,
3377 			   "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
3378 		return -1;
3379 	}
3380 
3381 	wpa_s->reassociate = 0;
3382 	wpa_s->disconnected = 1;
3383 	wpa_supplicant_cancel_sched_scan(wpa_s);
3384 	wpa_supplicant_cancel_scan(wpa_s);
3385 
3386 	/*
3387 	 * TODO: If necessary write our own group_remove function,
3388 	 * for now we can reuse deauthenticate
3389 	 */
3390 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3391 
3392 	if (wpa_s->mesh_if_created)
3393 		wpa_supplicant_remove_iface(global, wpa_s, 0);
3394 
3395 	return 0;
3396 }
3397 
3398 
wpa_supplicant_ctrl_iface_mesh_peer_remove(struct wpa_supplicant * wpa_s,char * cmd)3399 static int wpa_supplicant_ctrl_iface_mesh_peer_remove(
3400 	struct wpa_supplicant *wpa_s, char *cmd)
3401 {
3402 	u8 addr[ETH_ALEN];
3403 
3404 	if (hwaddr_aton(cmd, addr) < 0)
3405 		return -1;
3406 
3407 	return wpas_mesh_peer_remove(wpa_s, addr);
3408 }
3409 
3410 
wpa_supplicant_ctrl_iface_mesh_peer_add(struct wpa_supplicant * wpa_s,char * cmd)3411 static int wpa_supplicant_ctrl_iface_mesh_peer_add(
3412 	struct wpa_supplicant *wpa_s, char *cmd)
3413 {
3414 	u8 addr[ETH_ALEN];
3415 	int duration;
3416 	char *pos;
3417 
3418 	pos = os_strstr(cmd, " duration=");
3419 	if (pos) {
3420 		*pos = '\0';
3421 		duration = atoi(pos + 10);
3422 	} else {
3423 		duration = -1;
3424 	}
3425 
3426 	if (hwaddr_aton(cmd, addr))
3427 		return -1;
3428 
3429 	return wpas_mesh_peer_add(wpa_s, addr, duration);
3430 }
3431 
3432 
wpa_supplicant_ctrl_iface_mesh_link_probe(struct wpa_supplicant * wpa_s,char * cmd)3433 static int wpa_supplicant_ctrl_iface_mesh_link_probe(
3434 	struct wpa_supplicant *wpa_s, char *cmd)
3435 {
3436 	struct ether_header *eth;
3437 	u8 addr[ETH_ALEN];
3438 	u8 *buf;
3439 	char *pos;
3440 	size_t payload_len = 0, len;
3441 	int ret = -1;
3442 
3443 	if (hwaddr_aton(cmd, addr))
3444 		return -1;
3445 
3446 	pos = os_strstr(cmd, " payload=");
3447 	if (pos) {
3448 		pos = pos + 9;
3449 		payload_len = os_strlen(pos);
3450 		if (payload_len & 1)
3451 			return -1;
3452 
3453 		payload_len /= 2;
3454 	}
3455 
3456 	len = ETH_HLEN + payload_len;
3457 	buf = os_malloc(len);
3458 	if (!buf)
3459 		return -1;
3460 
3461 	eth = (struct ether_header *) buf;
3462 	os_memcpy(eth->ether_dhost, addr, ETH_ALEN);
3463 	os_memcpy(eth->ether_shost, wpa_s->own_addr, ETH_ALEN);
3464 	eth->ether_type = htons(ETH_P_802_3);
3465 
3466 	if (payload_len && hexstr2bin(pos, buf + ETH_HLEN, payload_len) < 0)
3467 		goto fail;
3468 
3469 	ret = wpa_drv_mesh_link_probe(wpa_s, addr, buf, len);
3470 fail:
3471 	os_free(buf);
3472 	return -ret;
3473 }
3474 
3475 #endif /* CONFIG_MESH */
3476 
3477 
wpa_supplicant_ctrl_iface_select_network(struct wpa_supplicant * wpa_s,char * cmd)3478 static int wpa_supplicant_ctrl_iface_select_network(
3479 	struct wpa_supplicant *wpa_s, char *cmd)
3480 {
3481 	int id;
3482 	struct wpa_ssid *ssid;
3483 	char *pos;
3484 
3485 	/* cmd: "<network id>" or "any" */
3486 	if (os_strncmp(cmd, "any", 3) == 0) {
3487 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
3488 		ssid = NULL;
3489 	} else {
3490 		id = atoi(cmd);
3491 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
3492 
3493 		ssid = wpa_config_get_network(wpa_s->conf, id);
3494 		if (ssid == NULL) {
3495 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3496 				   "network id=%d", id);
3497 			return -1;
3498 		}
3499 		if (ssid->disabled == 2) {
3500 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3501 				   "SELECT_NETWORK with persistent P2P group");
3502 			return -1;
3503 		}
3504 	}
3505 
3506 	pos = os_strstr(cmd, " freq=");
3507 	if (pos) {
3508 		int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
3509 		if (freqs) {
3510 			os_free(wpa_s->select_network_scan_freqs);
3511 			wpa_s->select_network_scan_freqs = freqs;
3512 		}
3513 	}
3514 
3515 	wpa_s->scan_min_time.sec = 0;
3516 	wpa_s->scan_min_time.usec = 0;
3517 	wpa_supplicant_select_network(wpa_s, ssid);
3518 
3519 	return 0;
3520 }
3521 
3522 
wpa_supplicant_ctrl_iface_enable_network(struct wpa_supplicant * wpa_s,char * cmd)3523 static int wpa_supplicant_ctrl_iface_enable_network(
3524 	struct wpa_supplicant *wpa_s, char *cmd)
3525 {
3526 	int id;
3527 	struct wpa_ssid *ssid;
3528 
3529 	/* cmd: "<network id>" or "all" */
3530 	if (os_strcmp(cmd, "all") == 0) {
3531 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
3532 		ssid = NULL;
3533 	} else {
3534 		id = atoi(cmd);
3535 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
3536 
3537 		ssid = wpa_config_get_network(wpa_s->conf, id);
3538 		if (ssid == NULL) {
3539 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3540 				   "network id=%d", id);
3541 			return -1;
3542 		}
3543 		if (ssid->disabled == 2) {
3544 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3545 				   "ENABLE_NETWORK with persistent P2P group");
3546 			return -1;
3547 		}
3548 
3549 		if (os_strstr(cmd, " no-connect")) {
3550 			ssid->disabled = 0;
3551 			return 0;
3552 		}
3553 	}
3554 	wpa_s->scan_min_time.sec = 0;
3555 	wpa_s->scan_min_time.usec = 0;
3556 	wpa_supplicant_enable_network(wpa_s, ssid);
3557 
3558 	return 0;
3559 }
3560 
3561 
wpa_supplicant_ctrl_iface_disable_network(struct wpa_supplicant * wpa_s,char * cmd)3562 static int wpa_supplicant_ctrl_iface_disable_network(
3563 	struct wpa_supplicant *wpa_s, char *cmd)
3564 {
3565 	int id;
3566 	struct wpa_ssid *ssid;
3567 
3568 	/* cmd: "<network id>" or "all" */
3569 	if (os_strcmp(cmd, "all") == 0) {
3570 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
3571 		ssid = NULL;
3572 	} else {
3573 		id = atoi(cmd);
3574 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
3575 
3576 		ssid = wpa_config_get_network(wpa_s->conf, id);
3577 		if (ssid == NULL) {
3578 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3579 				   "network id=%d", id);
3580 			return -1;
3581 		}
3582 		if (ssid->disabled == 2) {
3583 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3584 				   "DISABLE_NETWORK with persistent P2P "
3585 				   "group");
3586 			return -1;
3587 		}
3588 	}
3589 	wpa_supplicant_disable_network(wpa_s, ssid);
3590 
3591 	return 0;
3592 }
3593 
3594 
wpa_supplicant_ctrl_iface_add_network(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3595 static int wpa_supplicant_ctrl_iface_add_network(
3596 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
3597 {
3598 	struct wpa_ssid *ssid;
3599 	int ret;
3600 
3601 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
3602 
3603 	ssid = wpa_supplicant_add_network(wpa_s);
3604 	if (ssid == NULL)
3605 		return -1;
3606 
3607 	ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
3608 	if (os_snprintf_error(buflen, ret))
3609 		return -1;
3610 	return ret;
3611 }
3612 
3613 
wpa_supplicant_ctrl_iface_remove_network(struct wpa_supplicant * wpa_s,char * cmd)3614 static int wpa_supplicant_ctrl_iface_remove_network(
3615 	struct wpa_supplicant *wpa_s, char *cmd)
3616 {
3617 	int id;
3618 	int result;
3619 
3620 	/* cmd: "<network id>" or "all" */
3621 	if (os_strcmp(cmd, "all") == 0) {
3622 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
3623 		return wpa_supplicant_remove_all_networks(wpa_s);
3624 	}
3625 
3626 	id = atoi(cmd);
3627 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
3628 
3629 	result = wpa_supplicant_remove_network(wpa_s, id);
3630 	if (result == -1) {
3631 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3632 			   "id=%d", id);
3633 		return -1;
3634 	}
3635 	if (result == -2) {
3636 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
3637 			   "network id=%d", id);
3638 		return -1;
3639 	}
3640 	return 0;
3641 }
3642 
3643 
wpa_supplicant_ctrl_iface_update_network(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,char * name,char * value)3644 static int wpa_supplicant_ctrl_iface_update_network(
3645 	struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3646 	char *name, char *value)
3647 {
3648 	int ret;
3649 
3650 	ret = wpa_config_set(ssid, name, value, 0);
3651 	if (ret < 0) {
3652 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
3653 			   "variable '%s'", name);
3654 		return -1;
3655 	}
3656 	if (ret == 1)
3657 		return 0; /* No change to the previously configured value */
3658 
3659 #ifdef CONFIG_BGSCAN
3660 	if (os_strcmp(name, "bgscan") == 0) {
3661 		/*
3662 		 * Reset the bgscan parameters for the current network and
3663 		 * return. There's no need to flush caches for bgscan parameter
3664 		 * changes.
3665 		 */
3666 		if (wpa_s->current_ssid == ssid &&
3667 		    wpa_s->wpa_state == WPA_COMPLETED)
3668 			wpa_supplicant_reset_bgscan(wpa_s);
3669 		return 0;
3670 	}
3671 #endif /* CONFIG_BGSCAN */
3672 
3673 	if (os_strcmp(name, "bssid") != 0 &&
3674 	    os_strncmp(name, "bssid_", 6) != 0 &&
3675 	    os_strcmp(name, "scan_freq") != 0 &&
3676 	    os_strcmp(name, "priority") != 0) {
3677 		wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
3678 
3679 		if (wpa_s->current_ssid == ssid ||
3680 		    wpa_s->current_ssid == NULL) {
3681 			/*
3682 			 * Invalidate the EAP session cache if anything in the
3683 			 * current or previously used configuration changes.
3684 			 */
3685 			eapol_sm_invalidate_cached_session(wpa_s->eapol);
3686 		}
3687 	}
3688 
3689 	if ((os_strcmp(name, "psk") == 0 &&
3690 	     value[0] == '"' && ssid->ssid_len) ||
3691 	    (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
3692 		wpa_config_update_psk(ssid);
3693 	else if (os_strcmp(name, "priority") == 0)
3694 		wpa_config_update_prio_list(wpa_s->conf);
3695 
3696 	return 0;
3697 }
3698 
3699 
wpa_supplicant_ctrl_iface_set_network(struct wpa_supplicant * wpa_s,char * cmd)3700 static int wpa_supplicant_ctrl_iface_set_network(
3701 	struct wpa_supplicant *wpa_s, char *cmd)
3702 {
3703 	int id, ret, prev_bssid_set, prev_disabled;
3704 	struct wpa_ssid *ssid;
3705 	char *name, *value;
3706 	u8 prev_bssid[ETH_ALEN];
3707 
3708 	/* cmd: "<network id> <variable name> <value>" */
3709 	name = os_strchr(cmd, ' ');
3710 	if (name == NULL)
3711 		return -1;
3712 	*name++ = '\0';
3713 
3714 	value = os_strchr(name, ' ');
3715 	if (value == NULL)
3716 		return -1;
3717 	*value++ = '\0';
3718 
3719 	id = atoi(cmd);
3720 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
3721 		   id, name);
3722 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3723 			      (u8 *) value, os_strlen(value));
3724 
3725 	ssid = wpa_config_get_network(wpa_s->conf, id);
3726 	if (ssid == NULL) {
3727 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3728 			   "id=%d", id);
3729 		return -1;
3730 	}
3731 
3732 	prev_bssid_set = ssid->bssid_set;
3733 	prev_disabled = ssid->disabled;
3734 	os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
3735 	ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
3736 						       value);
3737 	if (ret == 0 &&
3738 	    (ssid->bssid_set != prev_bssid_set ||
3739 	     !ether_addr_equal(ssid->bssid, prev_bssid)))
3740 		wpas_notify_network_bssid_set_changed(wpa_s, ssid);
3741 
3742 	if (prev_disabled != ssid->disabled &&
3743 	    (prev_disabled == 2 || ssid->disabled == 2))
3744 		wpas_notify_network_type_changed(wpa_s, ssid);
3745 
3746 	return ret;
3747 }
3748 
3749 
wpa_supplicant_ctrl_iface_get_network(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)3750 static int wpa_supplicant_ctrl_iface_get_network(
3751 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
3752 {
3753 	int id;
3754 	size_t res;
3755 	struct wpa_ssid *ssid;
3756 	char *name, *value;
3757 
3758 	/* cmd: "<network id> <variable name>" */
3759 	name = os_strchr(cmd, ' ');
3760 	if (name == NULL || buflen == 0)
3761 		return -1;
3762 	*name++ = '\0';
3763 
3764 	id = atoi(cmd);
3765 	wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
3766 		   id, name);
3767 
3768 	ssid = wpa_config_get_network(wpa_s->conf, id);
3769 	if (ssid == NULL) {
3770 		wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Could not find network "
3771 			   "id=%d", id);
3772 		return -1;
3773 	}
3774 
3775 	value = wpa_config_get_no_key(ssid, name);
3776 	if (value == NULL) {
3777 		wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Failed to get network "
3778 			   "variable '%s'", name);
3779 		return -1;
3780 	}
3781 
3782 	res = os_strlcpy(buf, value, buflen);
3783 	if (res >= buflen) {
3784 		os_free(value);
3785 		return -1;
3786 	}
3787 
3788 	os_free(value);
3789 
3790 	return res;
3791 }
3792 
3793 
wpa_supplicant_ctrl_iface_dup_network(struct wpa_supplicant * wpa_s,char * cmd,struct wpa_supplicant * dst_wpa_s)3794 static int wpa_supplicant_ctrl_iface_dup_network(
3795 	struct wpa_supplicant *wpa_s, char *cmd,
3796 	struct wpa_supplicant *dst_wpa_s)
3797 {
3798 	struct wpa_ssid *ssid_s, *ssid_d;
3799 	char *name, *id, *value;
3800 	int id_s, id_d, ret;
3801 
3802 	/* cmd: "<src network id> <dst network id> <variable name>" */
3803 	id = os_strchr(cmd, ' ');
3804 	if (id == NULL)
3805 		return -1;
3806 	*id++ = '\0';
3807 
3808 	name = os_strchr(id, ' ');
3809 	if (name == NULL)
3810 		return -1;
3811 	*name++ = '\0';
3812 
3813 	id_s = atoi(cmd);
3814 	id_d = atoi(id);
3815 
3816 	wpa_printf(MSG_DEBUG,
3817 		   "CTRL_IFACE: DUP_NETWORK ifname=%s->%s id=%d->%d name='%s'",
3818 		   wpa_s->ifname, dst_wpa_s->ifname, id_s, id_d, name);
3819 
3820 	ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
3821 	if (ssid_s == NULL) {
3822 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3823 			   "network id=%d", id_s);
3824 		return -1;
3825 	}
3826 
3827 	ssid_d = wpa_config_get_network(dst_wpa_s->conf, id_d);
3828 	if (ssid_d == NULL) {
3829 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3830 			   "network id=%d", id_d);
3831 		return -1;
3832 	}
3833 
3834 	value = wpa_config_get(ssid_s, name);
3835 	if (value == NULL) {
3836 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
3837 			   "variable '%s'", name);
3838 		return -1;
3839 	}
3840 
3841 	ret = wpa_supplicant_ctrl_iface_update_network(dst_wpa_s, ssid_d, name,
3842 						       value);
3843 
3844 	os_free(value);
3845 
3846 	return ret;
3847 }
3848 
3849 
wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3850 static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
3851 						char *buf, size_t buflen)
3852 {
3853 	char *pos, *end;
3854 	struct wpa_cred *cred;
3855 	int ret;
3856 
3857 	pos = buf;
3858 	end = buf + buflen;
3859 	ret = os_snprintf(pos, end - pos,
3860 			  "cred id / realm / username / domain / imsi\n");
3861 	if (os_snprintf_error(end - pos, ret))
3862 		return pos - buf;
3863 	pos += ret;
3864 
3865 	cred = wpa_s->conf->cred;
3866 	while (cred) {
3867 		ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
3868 				  cred->id, cred->realm ? cred->realm : "",
3869 				  cred->username ? cred->username : "",
3870 				  cred->domain ? cred->domain[0] : "",
3871 				  cred->imsi ? cred->imsi : "");
3872 		if (os_snprintf_error(end - pos, ret))
3873 			return pos - buf;
3874 		pos += ret;
3875 
3876 		cred = cred->next;
3877 	}
3878 
3879 	return pos - buf;
3880 }
3881 
3882 
wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3883 static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
3884 					      char *buf, size_t buflen)
3885 {
3886 	struct wpa_cred *cred;
3887 	int ret;
3888 
3889 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
3890 
3891 	cred = wpa_config_add_cred(wpa_s->conf);
3892 	if (cred == NULL)
3893 		return -1;
3894 
3895 	wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
3896 
3897 	ret = os_snprintf(buf, buflen, "%d\n", cred->id);
3898 	if (os_snprintf_error(buflen, ret))
3899 		return -1;
3900 	return ret;
3901 }
3902 
3903 
wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant * wpa_s,char * cmd)3904 static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
3905 						 char *cmd)
3906 {
3907 	int id;
3908 	struct wpa_cred *cred, *prev;
3909 
3910 	/* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
3911 	 * "provisioning_sp=<FQDN> */
3912 	if (os_strcmp(cmd, "all") == 0) {
3913 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
3914 		return wpas_remove_all_creds(wpa_s);
3915 	}
3916 
3917 	if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
3918 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
3919 			   cmd + 8);
3920 		cred = wpa_s->conf->cred;
3921 		while (cred) {
3922 			prev = cred;
3923 			cred = cred->next;
3924 			if (prev->domain) {
3925 				size_t i;
3926 				for (i = 0; i < prev->num_domain; i++) {
3927 					if (os_strcmp(prev->domain[i], cmd + 8)
3928 					    != 0)
3929 						continue;
3930 					wpas_remove_cred(wpa_s, prev);
3931 					break;
3932 				}
3933 			}
3934 		}
3935 		return 0;
3936 	}
3937 
3938 	if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
3939 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
3940 			   cmd + 16);
3941 		cred = wpa_s->conf->cred;
3942 		while (cred) {
3943 			prev = cred;
3944 			cred = cred->next;
3945 			if (prev->provisioning_sp &&
3946 			    os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
3947 				wpas_remove_cred(wpa_s, prev);
3948 		}
3949 		return 0;
3950 	}
3951 
3952 	id = atoi(cmd);
3953 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
3954 
3955 	cred = wpa_config_get_cred(wpa_s->conf, id);
3956 	return wpas_remove_cred(wpa_s, cred);
3957 }
3958 
3959 
wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant * wpa_s,char * cmd)3960 static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
3961 					      char *cmd)
3962 {
3963 	int id;
3964 	struct wpa_cred *cred;
3965 	char *name, *value;
3966 
3967 	/* cmd: "<cred id> <variable name> <value>" */
3968 	name = os_strchr(cmd, ' ');
3969 	if (name == NULL)
3970 		return -1;
3971 	*name++ = '\0';
3972 
3973 	value = os_strchr(name, ' ');
3974 	if (value == NULL)
3975 		return -1;
3976 	*value++ = '\0';
3977 
3978 	id = atoi(cmd);
3979 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
3980 		   id, name);
3981 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3982 			      (u8 *) value, os_strlen(value));
3983 
3984 	cred = wpa_config_get_cred(wpa_s->conf, id);
3985 	if (cred == NULL) {
3986 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3987 			   id);
3988 		return -1;
3989 	}
3990 
3991 	if (wpa_config_set_cred(cred, name, value, 0) < 0) {
3992 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
3993 			   "variable '%s'", name);
3994 		return -1;
3995 	}
3996 
3997 	wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
3998 
3999 	return 0;
4000 }
4001 
4002 
wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)4003 static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
4004 					      char *cmd, char *buf,
4005 					      size_t buflen)
4006 {
4007 	int id;
4008 	size_t res;
4009 	struct wpa_cred *cred;
4010 	char *name, *value;
4011 
4012 	/* cmd: "<cred id> <variable name>" */
4013 	name = os_strchr(cmd, ' ');
4014 	if (name == NULL)
4015 		return -1;
4016 	*name++ = '\0';
4017 
4018 	id = atoi(cmd);
4019 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
4020 		   id, name);
4021 
4022 	cred = wpa_config_get_cred(wpa_s->conf, id);
4023 	if (cred == NULL) {
4024 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
4025 			   id);
4026 		return -1;
4027 	}
4028 
4029 	value = wpa_config_get_cred_no_key(cred, name);
4030 	if (value == NULL) {
4031 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
4032 			   name);
4033 		return -1;
4034 	}
4035 
4036 	res = os_strlcpy(buf, value, buflen);
4037 	if (res >= buflen) {
4038 		os_free(value);
4039 		return -1;
4040 	}
4041 
4042 	os_free(value);
4043 
4044 	return res;
4045 }
4046 
4047 
4048 #ifndef CONFIG_NO_CONFIG_WRITE
wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant * wpa_s)4049 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
4050 {
4051 	int ret;
4052 
4053 	if (!wpa_s->conf->update_config) {
4054 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
4055 			   "to update configuration (update_config=0)");
4056 		return -1;
4057 	}
4058 
4059 	ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
4060 	if (ret) {
4061 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
4062 			   "update configuration");
4063 	} else {
4064 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
4065 			   " updated");
4066 	}
4067 
4068 	return ret;
4069 }
4070 #endif /* CONFIG_NO_CONFIG_WRITE */
4071 
4072 
4073 struct cipher_info {
4074 	unsigned int capa;
4075 	const char *name;
4076 	int group_only;
4077 };
4078 
4079 static const struct cipher_info ciphers[] = {
4080 	{ WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
4081 	{ WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
4082 	{ WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
4083 	{ WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
4084 #ifndef CONFIG_NO_TKIP
4085 	{ WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
4086 #endif /* CONFIG_NO_TKIP */
4087 	{ WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
4088 #ifdef CONFIG_WEP
4089 	{ WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
4090 	{ WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
4091 #endif /* CONFIG_WEP */
4092 };
4093 
4094 static const struct cipher_info ciphers_group_mgmt[] = {
4095 	{ WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
4096 	{ WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
4097 	{ WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
4098 	{ WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
4099 };
4100 
4101 
ctrl_iface_get_capability_pairwise(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4102 static int ctrl_iface_get_capability_pairwise(int res, bool strict,
4103 					      struct wpa_driver_capa *capa,
4104 					      char *buf, size_t buflen)
4105 {
4106 	int ret;
4107 	char *pos, *end;
4108 	size_t len;
4109 	unsigned int i;
4110 
4111 	pos = buf;
4112 	end = pos + buflen;
4113 
4114 	if (res < 0) {
4115 		if (strict)
4116 			return 0;
4117 #ifdef CONFIG_NO_TKIP
4118 		len = os_strlcpy(buf, "CCMP NONE", buflen);
4119 #else /* CONFIG_NO_TKIP */
4120 		len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
4121 #endif /* CONFIG_NO_TKIP */
4122 		if (len >= buflen)
4123 			return -1;
4124 		return len;
4125 	}
4126 
4127 	for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
4128 		if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
4129 			ret = os_snprintf(pos, end - pos, "%s%s",
4130 					  pos == buf ? "" : " ",
4131 					  ciphers[i].name);
4132 			if (os_snprintf_error(end - pos, ret))
4133 				return pos - buf;
4134 			pos += ret;
4135 		}
4136 	}
4137 
4138 	return pos - buf;
4139 }
4140 
4141 
ctrl_iface_get_capability_group(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4142 static int ctrl_iface_get_capability_group(int res, bool strict,
4143 					   struct wpa_driver_capa *capa,
4144 					   char *buf, size_t buflen)
4145 {
4146 	int ret;
4147 	char *pos, *end;
4148 	size_t len;
4149 	unsigned int i;
4150 
4151 	pos = buf;
4152 	end = pos + buflen;
4153 
4154 	if (res < 0) {
4155 		if (strict)
4156 			return 0;
4157 #ifdef CONFIG_WEP
4158 #ifdef CONFIG_NO_TKIP
4159 		len = os_strlcpy(buf, "CCMP WEP104 WEP40", buflen);
4160 #else /* CONFIG_NO_TKIP */
4161 		len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
4162 #endif /* CONFIG_NO_TKIP */
4163 #else /* CONFIG_WEP */
4164 #ifdef CONFIG_NO_TKIP
4165 		len = os_strlcpy(buf, "CCMP", buflen);
4166 #else /* CONFIG_NO_TKIP */
4167 		len = os_strlcpy(buf, "CCMP TKIP", buflen);
4168 #endif /* CONFIG_NO_TKIP */
4169 #endif /* CONFIG_WEP */
4170 		if (len >= buflen)
4171 			return -1;
4172 		return len;
4173 	}
4174 
4175 	for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
4176 		if (capa->enc & ciphers[i].capa) {
4177 			ret = os_snprintf(pos, end - pos, "%s%s",
4178 					  pos == buf ? "" : " ",
4179 					  ciphers[i].name);
4180 			if (os_snprintf_error(end - pos, ret))
4181 				return pos - buf;
4182 			pos += ret;
4183 		}
4184 	}
4185 
4186 	return pos - buf;
4187 }
4188 
4189 
ctrl_iface_get_capability_group_mgmt(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4190 static int ctrl_iface_get_capability_group_mgmt(int res, bool strict,
4191 						struct wpa_driver_capa *capa,
4192 						char *buf, size_t buflen)
4193 {
4194 	int ret;
4195 	char *pos, *end;
4196 	unsigned int i;
4197 
4198 	pos = buf;
4199 	end = pos + buflen;
4200 
4201 	if (res < 0)
4202 		return 0;
4203 
4204 	for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
4205 		if (capa->enc & ciphers_group_mgmt[i].capa) {
4206 			ret = os_snprintf(pos, end - pos, "%s%s",
4207 					  pos == buf ? "" : " ",
4208 					  ciphers_group_mgmt[i].name);
4209 			if (os_snprintf_error(end - pos, ret))
4210 				return pos - buf;
4211 			pos += ret;
4212 		}
4213 	}
4214 
4215 	return pos - buf;
4216 }
4217 
4218 
iftype_str_to_index(const char * iftype_str)4219 static int iftype_str_to_index(const char *iftype_str)
4220 {
4221 	if (!iftype_str)
4222 		return WPA_IF_MAX;
4223 
4224 	if (os_strcmp(iftype_str, "STATION") == 0)
4225 		return WPA_IF_STATION;
4226 
4227 	if (os_strcmp(iftype_str, "AP_VLAN") == 0)
4228 		return WPA_IF_AP_VLAN;
4229 
4230 	if (os_strcmp(iftype_str, "AP") == 0)
4231 		return WPA_IF_AP_BSS;
4232 
4233 	if (os_strcmp(iftype_str, "P2P_GO") == 0)
4234 		return WPA_IF_P2P_GO;
4235 
4236 	if (os_strcmp(iftype_str, "P2P_CLIENT") == 0)
4237 		return WPA_IF_P2P_CLIENT;
4238 
4239 	if (os_strcmp(iftype_str, "P2P_DEVICE") == 0)
4240 		return WPA_IF_P2P_DEVICE;
4241 
4242 	if (os_strcmp(iftype_str, "MESH") == 0)
4243 		return WPA_IF_MESH;
4244 
4245 	if (os_strcmp(iftype_str, "IBSS") == 0)
4246 		return WPA_IF_IBSS;
4247 
4248 	if (os_strcmp(iftype_str, "NAN") == 0)
4249 		return WPA_IF_NAN;
4250 
4251 	return WPA_IF_MAX;
4252 }
4253 
4254 
ctrl_iface_get_capability_key_mgmt(int res,bool strict,struct wpa_driver_capa * capa,const char * iftype_str,char * buf,size_t buflen)4255 static int ctrl_iface_get_capability_key_mgmt(int res, bool strict,
4256 					      struct wpa_driver_capa *capa,
4257 					      const char *iftype_str,
4258 					      char *buf, size_t buflen)
4259 {
4260 	int ret;
4261 	unsigned int key_mgmt;
4262 	char *pos, *end;
4263 	size_t len;
4264 
4265 	pos = buf;
4266 	end = pos + buflen;
4267 
4268 	if (res < 0) {
4269 		if (strict)
4270 			return 0;
4271 		len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
4272 				 "NONE", buflen);
4273 		if (len >= buflen)
4274 			return -1;
4275 		return len;
4276 	}
4277 
4278 	if (iftype_str) {
4279 		enum wpa_driver_if_type iftype;
4280 
4281 		iftype = iftype_str_to_index(iftype_str);
4282 		if (iftype == WPA_IF_MAX)
4283 			return -1;
4284 		key_mgmt = capa->key_mgmt_iftype[iftype];
4285 	} else {
4286 		key_mgmt = capa->key_mgmt;
4287 	}
4288 
4289 	ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
4290 	if (os_snprintf_error(end - pos, ret))
4291 		return pos - buf;
4292 	pos += ret;
4293 
4294 	if (key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4295 			WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
4296 		ret = os_snprintf(pos, end - pos, " WPA-EAP");
4297 		if (os_snprintf_error(end - pos, ret))
4298 			return pos - buf;
4299 		pos += ret;
4300 	}
4301 
4302 	if (key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
4303 			WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
4304 		ret = os_snprintf(pos, end - pos, " WPA-PSK");
4305 		if (os_snprintf_error(end - pos, ret))
4306 			return pos - buf;
4307 		pos += ret;
4308 	}
4309 
4310 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
4311 		ret = os_snprintf(pos, end - pos, " WPA-NONE");
4312 		if (os_snprintf_error(end - pos, ret))
4313 			return pos - buf;
4314 		pos += ret;
4315 	}
4316 
4317 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WAPI_PSK) {
4318 		ret = os_snprintf(pos, end - pos, " WAPI-PSK");
4319 		if (os_snprintf_error(end - pos, ret))
4320 			return pos - buf;
4321 		pos += ret;
4322 	}
4323 
4324 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_TPK_HANDSHAKE) {
4325 		ret = os_snprintf(pos, end - pos, " TPK-HANDSHAKE");
4326 		if (os_snprintf_error(end - pos, ret))
4327 			return pos - buf;
4328 		pos += ret;
4329 	}
4330 
4331 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_CCKM) {
4332 		ret = os_snprintf(pos, end - pos, " CCKM");
4333 		if (os_snprintf_error(end - pos, ret))
4334 			return pos - buf;
4335 		pos += ret;
4336 	}
4337 
4338 #ifdef CONFIG_SUITEB
4339 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
4340 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
4341 		if (os_snprintf_error(end - pos, ret))
4342 			return pos - buf;
4343 		pos += ret;
4344 	}
4345 #endif /* CONFIG_SUITEB */
4346 #ifdef CONFIG_SUITEB192
4347 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
4348 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
4349 		if (os_snprintf_error(end - pos, ret))
4350 			return pos - buf;
4351 		pos += ret;
4352 	}
4353 #endif /* CONFIG_SUITEB192 */
4354 #ifdef CONFIG_OWE
4355 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OWE) {
4356 		ret = os_snprintf(pos, end - pos, " OWE");
4357 		if (os_snprintf_error(end - pos, ret))
4358 			return pos - buf;
4359 		pos += ret;
4360 	}
4361 #endif /* CONFIG_OWE */
4362 #ifdef CONFIG_DPP
4363 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_DPP) {
4364 		ret = os_snprintf(pos, end - pos, " DPP");
4365 		if (os_snprintf_error(end - pos, ret))
4366 			return pos - buf;
4367 		pos += ret;
4368 	}
4369 #endif /* CONFIG_DPP */
4370 #ifdef CONFIG_FILS
4371 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA256) {
4372 		ret = os_snprintf(pos, end - pos, " FILS-SHA256");
4373 		if (os_snprintf_error(end - pos, ret))
4374 			return pos - buf;
4375 		pos += ret;
4376 	}
4377 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA384) {
4378 		ret = os_snprintf(pos, end - pos, " FILS-SHA384");
4379 		if (os_snprintf_error(end - pos, ret))
4380 			return pos - buf;
4381 		pos += ret;
4382 	}
4383 #ifdef CONFIG_IEEE80211R
4384 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA256) {
4385 		ret = os_snprintf(pos, end - pos, " FT-FILS-SHA256");
4386 		if (os_snprintf_error(end - pos, ret))
4387 			return pos - buf;
4388 		pos += ret;
4389 	}
4390 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA384) {
4391 		ret = os_snprintf(pos, end - pos, " FT-FILS-SHA384");
4392 		if (os_snprintf_error(end - pos, ret))
4393 			return pos - buf;
4394 		pos += ret;
4395 	}
4396 #endif /* CONFIG_IEEE80211R */
4397 #endif /* CONFIG_FILS */
4398 #ifdef CONFIG_IEEE80211R
4399 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK) {
4400 		ret = os_snprintf(pos, end - pos, " FT-PSK");
4401 		if (os_snprintf_error(end - pos, ret))
4402 			return pos - buf;
4403 		pos += ret;
4404 	}
4405 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) {
4406 		ret = os_snprintf(pos, end - pos, " FT-EAP");
4407 		if (os_snprintf_error(end - pos, ret))
4408 			return pos - buf;
4409 		pos += ret;
4410 	}
4411 #ifdef CONFIG_SAE
4412 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_SAE) {
4413 		ret = os_snprintf(pos, end - pos, " FT-SAE");
4414 		if (os_snprintf_error(end - pos, ret))
4415 			return pos - buf;
4416 		pos += ret;
4417 	}
4418 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_SAE_EXT_KEY) {
4419 		ret = os_snprintf(pos, end - pos, " FT-SAE-EXT-KEY");
4420 		if (os_snprintf_error(end - pos, ret))
4421 			return pos - buf;
4422 		pos += ret;
4423 	}
4424 #endif /* CONFIG_SAE */
4425 #ifdef CONFIG_SHA384
4426 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_802_1X_SHA384) {
4427 		ret = os_snprintf(pos, end - pos, " FT-EAP-SHA384");
4428 		if (os_snprintf_error(end - pos, ret))
4429 			return pos - buf;
4430 		pos += ret;
4431 	}
4432 #endif /* CONFIG_SHA384 */
4433 #endif /* CONFIG_IEEE80211R */
4434 #ifdef CONFIG_SAE
4435 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE) {
4436 		ret = os_snprintf(pos, end - pos, " SAE");
4437 		if (os_snprintf_error(end - pos, ret))
4438 			return pos - buf;
4439 		pos += ret;
4440 	}
4441 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE_EXT_KEY) {
4442 		ret = os_snprintf(pos, end - pos, " SAE-EXT-KEY");
4443 		if (os_snprintf_error(end - pos, ret))
4444 			return pos - buf;
4445 		pos += ret;
4446 	}
4447 #endif /* CONFIG_SAE */
4448 #ifdef CONFIG_SHA256
4449 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_802_1X_SHA256) {
4450 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SHA256");
4451 		if (os_snprintf_error(end - pos, ret))
4452 			return pos - buf;
4453 		pos += ret;
4454 	}
4455 
4456 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_PSK_SHA256) {
4457 		ret = os_snprintf(pos, end - pos, " WPA-PSK-SHA256");
4458 		if (os_snprintf_error(end - pos, ret))
4459 			return pos - buf;
4460 		pos += ret;
4461 	}
4462 #endif /* CONFIG_SHA256 */
4463 #ifdef CONFIG_HS20
4464 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OSEN) {
4465 		ret = os_snprintf(pos, end - pos, " OSEN");
4466 		if (os_snprintf_error(end - pos, ret))
4467 			return pos - buf;
4468 		pos += ret;
4469 	}
4470 #endif /* CONFIG_HS20 */
4471 
4472 	return pos - buf;
4473 }
4474 
4475 
ctrl_iface_get_capability_proto(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4476 static int ctrl_iface_get_capability_proto(int res, bool strict,
4477 					   struct wpa_driver_capa *capa,
4478 					   char *buf, size_t buflen)
4479 {
4480 	int ret;
4481 	char *pos, *end;
4482 	size_t len;
4483 
4484 	pos = buf;
4485 	end = pos + buflen;
4486 
4487 	if (res < 0) {
4488 		if (strict)
4489 			return 0;
4490 		len = os_strlcpy(buf, "RSN WPA", buflen);
4491 		if (len >= buflen)
4492 			return -1;
4493 		return len;
4494 	}
4495 
4496 	if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
4497 			      WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
4498 		ret = os_snprintf(pos, end - pos, "%sRSN",
4499 				  pos == buf ? "" : " ");
4500 		if (os_snprintf_error(end - pos, ret))
4501 			return pos - buf;
4502 		pos += ret;
4503 	}
4504 
4505 	if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4506 			      WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
4507 		ret = os_snprintf(pos, end - pos, "%sWPA",
4508 				  pos == buf ? "" : " ");
4509 		if (os_snprintf_error(end - pos, ret))
4510 			return pos - buf;
4511 		pos += ret;
4512 	}
4513 
4514 	return pos - buf;
4515 }
4516 
4517 
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)4518 static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
4519 					      int res, bool strict,
4520 					      struct wpa_driver_capa *capa,
4521 					      char *buf, size_t buflen)
4522 {
4523 	int ret;
4524 	char *pos, *end;
4525 	size_t len;
4526 
4527 	pos = buf;
4528 	end = pos + buflen;
4529 
4530 	if (res < 0) {
4531 		if (strict)
4532 			return 0;
4533 		len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
4534 		if (len >= buflen)
4535 			return -1;
4536 		return len;
4537 	}
4538 
4539 	if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
4540 		ret = os_snprintf(pos, end - pos, "%sOPEN",
4541 				  pos == buf ? "" : " ");
4542 		if (os_snprintf_error(end - pos, ret))
4543 			return pos - buf;
4544 		pos += ret;
4545 	}
4546 
4547 	if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
4548 		ret = os_snprintf(pos, end - pos, "%sSHARED",
4549 				  pos == buf ? "" : " ");
4550 		if (os_snprintf_error(end - pos, ret))
4551 			return pos - buf;
4552 		pos += ret;
4553 	}
4554 
4555 	if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
4556 		ret = os_snprintf(pos, end - pos, "%sLEAP",
4557 				  pos == buf ? "" : " ");
4558 		if (os_snprintf_error(end - pos, ret))
4559 			return pos - buf;
4560 		pos += ret;
4561 	}
4562 
4563 #ifdef CONFIG_SAE
4564 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
4565 		ret = os_snprintf(pos, end - pos, "%sSAE",
4566 				  pos == buf ? "" : " ");
4567 		if (os_snprintf_error(end - pos, ret))
4568 			return pos - buf;
4569 		pos += ret;
4570 	}
4571 #endif /* CONFIG_SAE */
4572 
4573 #ifdef CONFIG_FILS
4574 	if (wpa_is_fils_supported(wpa_s)) {
4575 		ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITHOUT_PFS",
4576 				  pos == buf ? "" : " ");
4577 		if (os_snprintf_error(end - pos, ret))
4578 			return pos - buf;
4579 		pos += ret;
4580 	}
4581 
4582 #ifdef CONFIG_FILS_SK_PFS
4583 	if (wpa_is_fils_sk_pfs_supported(wpa_s)) {
4584 		ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITH_PFS",
4585 				  pos == buf ? "" : " ");
4586 		if (os_snprintf_error(end - pos, ret))
4587 			return pos - buf;
4588 		pos += ret;
4589 	}
4590 #endif /* CONFIG_FILS_SK_PFS */
4591 #endif /* CONFIG_FILS */
4592 
4593 #ifdef CONFIG_PASN
4594 	ret = os_snprintf(pos, end - pos, "%sPASN",
4595 			  pos == buf ? "" : " ");
4596 	if (os_snprintf_error(end - pos, ret))
4597 		return pos - buf;
4598 	pos += ret;
4599 
4600 #endif /* CONFIG_PASN */
4601 
4602 	return pos - buf;
4603 }
4604 
4605 
ctrl_iface_get_capability_modes(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4606 static int ctrl_iface_get_capability_modes(int res, bool strict,
4607 					   struct wpa_driver_capa *capa,
4608 					   char *buf, size_t buflen)
4609 {
4610 	int ret;
4611 	char *pos, *end;
4612 	size_t len;
4613 
4614 	pos = buf;
4615 	end = pos + buflen;
4616 
4617 	if (res < 0) {
4618 		if (strict)
4619 			return 0;
4620 		len = os_strlcpy(buf, "IBSS AP", buflen);
4621 		if (len >= buflen)
4622 			return -1;
4623 		return len;
4624 	}
4625 
4626 	if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
4627 		ret = os_snprintf(pos, end - pos, "%sIBSS",
4628 				  pos == buf ? "" : " ");
4629 		if (os_snprintf_error(end - pos, ret))
4630 			return pos - buf;
4631 		pos += ret;
4632 	}
4633 
4634 	if (capa->flags & WPA_DRIVER_FLAGS_AP) {
4635 		ret = os_snprintf(pos, end - pos, "%sAP",
4636 				  pos == buf ? "" : " ");
4637 		if (os_snprintf_error(end - pos, ret))
4638 			return pos - buf;
4639 		pos += ret;
4640 	}
4641 
4642 #ifdef CONFIG_MESH
4643 	if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
4644 		ret = os_snprintf(pos, end - pos, "%sMESH",
4645 				  pos == buf ? "" : " ");
4646 		if (os_snprintf_error(end - pos, ret))
4647 			return pos - buf;
4648 		pos += ret;
4649 	}
4650 #endif /* CONFIG_MESH */
4651 
4652 	return pos - buf;
4653 }
4654 
4655 
ctrl_iface_get_capability_channels(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)4656 static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
4657 					      char *buf, size_t buflen)
4658 {
4659 	struct hostapd_channel_data *chnl;
4660 	int ret, i, j;
4661 	char *pos, *end, *hmode;
4662 
4663 	pos = buf;
4664 	end = pos + buflen;
4665 
4666 	for (j = 0; j < wpa_s->hw.num_modes; j++) {
4667 		switch (wpa_s->hw.modes[j].mode) {
4668 		case HOSTAPD_MODE_IEEE80211B:
4669 			hmode = "B";
4670 			break;
4671 		case HOSTAPD_MODE_IEEE80211G:
4672 			hmode = "G";
4673 			break;
4674 		case HOSTAPD_MODE_IEEE80211A:
4675 			hmode = "A";
4676 			break;
4677 		case HOSTAPD_MODE_IEEE80211AD:
4678 			hmode = "AD";
4679 			break;
4680 		default:
4681 			continue;
4682 		}
4683 		ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
4684 		if (os_snprintf_error(end - pos, ret))
4685 			return pos - buf;
4686 		pos += ret;
4687 		chnl = wpa_s->hw.modes[j].channels;
4688 		for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
4689 			if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
4690 				continue;
4691 			ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
4692 			if (os_snprintf_error(end - pos, ret))
4693 				return pos - buf;
4694 			pos += ret;
4695 		}
4696 		ret = os_snprintf(pos, end - pos, "\n");
4697 		if (os_snprintf_error(end - pos, ret))
4698 			return pos - buf;
4699 		pos += ret;
4700 	}
4701 
4702 	return pos - buf;
4703 }
4704 
4705 
ctrl_iface_get_capability_freq(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)4706 static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
4707 					  char *buf, size_t buflen)
4708 {
4709 	struct hostapd_channel_data *chnl;
4710 	int ret, i, j;
4711 	char *pos, *end, *hmode;
4712 
4713 	pos = buf;
4714 	end = pos + buflen;
4715 
4716 	for (j = 0; j < wpa_s->hw.num_modes; j++) {
4717 		switch (wpa_s->hw.modes[j].mode) {
4718 		case HOSTAPD_MODE_IEEE80211B:
4719 			hmode = "B";
4720 			break;
4721 		case HOSTAPD_MODE_IEEE80211G:
4722 			hmode = "G";
4723 			break;
4724 		case HOSTAPD_MODE_IEEE80211A:
4725 			hmode = "A";
4726 			break;
4727 		case HOSTAPD_MODE_IEEE80211AD:
4728 			hmode = "AD";
4729 			break;
4730 		default:
4731 			continue;
4732 		}
4733 		ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
4734 				  hmode);
4735 		if (os_snprintf_error(end - pos, ret))
4736 			return pos - buf;
4737 		pos += ret;
4738 		chnl = wpa_s->hw.modes[j].channels;
4739 		for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
4740 			if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
4741 				continue;
4742 			ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
4743 					  chnl[i].chan, chnl[i].freq,
4744 					  chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
4745 					  " (NO_IR)" : "",
4746 					  chnl[i].flag & HOSTAPD_CHAN_RADAR ?
4747 					  " (DFS)" : "");
4748 
4749 			if (os_snprintf_error(end - pos, ret))
4750 				return pos - buf;
4751 			pos += ret;
4752 		}
4753 		ret = os_snprintf(pos, end - pos, "\n");
4754 		if (os_snprintf_error(end - pos, ret))
4755 			return pos - buf;
4756 		pos += ret;
4757 	}
4758 
4759 	return pos - buf;
4760 }
4761 
4762 
wpa_supplicant_ctrl_iface_get_capability(struct wpa_supplicant * wpa_s,const char * _field,char * buf,size_t buflen)4763 static int wpa_supplicant_ctrl_iface_get_capability(
4764 	struct wpa_supplicant *wpa_s, const char *_field, char *buf,
4765 	size_t buflen)
4766 {
4767 	struct wpa_driver_capa capa;
4768 	int res;
4769 	char *next_param, *curr_param, *iftype = NULL;
4770 	bool strict = false;
4771 	char field[50];
4772 	size_t len;
4773 
4774 	/* Determine whether or not strict checking was requested */
4775 	len = os_strlcpy(field, _field, sizeof(field));
4776 	if (len >= sizeof(field))
4777 		return -1;
4778 
4779 	next_param = os_strchr(field, ' ');
4780 	while (next_param) {
4781 		*next_param++ = '\0';
4782 		curr_param = next_param;
4783 		next_param = os_strchr(next_param, ' ');
4784 
4785 		if (next_param)
4786 			*next_param = '\0';
4787 
4788 		if (os_strcmp(curr_param, "strict") == 0)
4789 			strict = true;
4790 		else if (os_strncmp(curr_param, "iftype=", 7) == 0)
4791 			iftype = curr_param + 7;
4792 		else
4793 			return -1;
4794 	}
4795 
4796 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'%s%s%s",
4797 		   field, iftype ? " iftype=" : "", iftype ? iftype : "",
4798 		   strict ? " strict" : "");
4799 
4800 	if (os_strcmp(field, "eap") == 0) {
4801 		return eap_get_names(buf, buflen);
4802 	}
4803 
4804 	res = wpa_drv_get_capa(wpa_s, &capa);
4805 
4806 	if (os_strcmp(field, "pairwise") == 0)
4807 		return ctrl_iface_get_capability_pairwise(res, strict, &capa,
4808 							  buf, buflen);
4809 
4810 	if (os_strcmp(field, "group") == 0)
4811 		return ctrl_iface_get_capability_group(res, strict, &capa,
4812 						       buf, buflen);
4813 
4814 	if (os_strcmp(field, "group_mgmt") == 0)
4815 		return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
4816 							    buf, buflen);
4817 
4818 	if (os_strcmp(field, "key_mgmt") == 0)
4819 		return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
4820 							  iftype, buf, buflen);
4821 
4822 	if (os_strcmp(field, "proto") == 0)
4823 		return ctrl_iface_get_capability_proto(res, strict, &capa,
4824 						       buf, buflen);
4825 
4826 	if (os_strcmp(field, "auth_alg") == 0)
4827 		return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
4828 							  &capa, buf, buflen);
4829 
4830 	if (os_strcmp(field, "modes") == 0)
4831 		return ctrl_iface_get_capability_modes(res, strict, &capa,
4832 						       buf, buflen);
4833 
4834 	if (os_strcmp(field, "channels") == 0)
4835 		return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
4836 
4837 	if (os_strcmp(field, "freq") == 0)
4838 		return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
4839 
4840 #ifdef CONFIG_TDLS
4841 	if (os_strcmp(field, "tdls") == 0)
4842 		return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
4843 #endif /* CONFIG_TDLS */
4844 
4845 #ifdef CONFIG_ERP
4846 	if (os_strcmp(field, "erp") == 0) {
4847 		res = os_snprintf(buf, buflen, "ERP");
4848 		if (os_snprintf_error(buflen, res))
4849 			return -1;
4850 		return res;
4851 	}
4852 #endif /* CONFIG_EPR */
4853 
4854 #ifdef CONFIG_FIPS
4855 	if (os_strcmp(field, "fips") == 0) {
4856 		res = os_snprintf(buf, buflen, "FIPS");
4857 		if (os_snprintf_error(buflen, res))
4858 			return -1;
4859 		return res;
4860 	}
4861 #endif /* CONFIG_FIPS */
4862 
4863 #ifdef CONFIG_ACS
4864 	if (os_strcmp(field, "acs") == 0) {
4865 		res = os_snprintf(buf, buflen, "ACS");
4866 		if (os_snprintf_error(buflen, res))
4867 			return -1;
4868 		return res;
4869 	}
4870 #endif /* CONFIG_ACS */
4871 
4872 #ifdef CONFIG_FILS
4873 	if (os_strcmp(field, "fils") == 0) {
4874 #ifdef CONFIG_FILS_SK_PFS
4875 		if (wpa_is_fils_supported(wpa_s) &&
4876 		    wpa_is_fils_sk_pfs_supported(wpa_s)) {
4877 			res = os_snprintf(buf, buflen, "FILS FILS-SK-PFS");
4878 			if (os_snprintf_error(buflen, res))
4879 				return -1;
4880 			return res;
4881 		}
4882 #endif /* CONFIG_FILS_SK_PFS */
4883 
4884 		if (wpa_is_fils_supported(wpa_s)) {
4885 			res = os_snprintf(buf, buflen, "FILS");
4886 			if (os_snprintf_error(buflen, res))
4887 				return -1;
4888 			return res;
4889 		}
4890 	}
4891 #endif /* CONFIG_FILS */
4892 
4893 	if (os_strcmp(field, "multibss") == 0 && wpa_s->multi_bss_support) {
4894 		res = os_snprintf(buf, buflen, "MULTIBSS-STA");
4895 		if (os_snprintf_error(buflen, res))
4896 			return -1;
4897 		return res;
4898 	}
4899 
4900 #ifdef CONFIG_DPP
4901 	if (os_strcmp(field, "dpp") == 0) {
4902 #ifdef CONFIG_DPP3
4903 		res = os_snprintf(buf, buflen, "DPP=3");
4904 #elif defined(CONFIG_DPP2)
4905 		res = os_snprintf(buf, buflen, "DPP=2");
4906 #else /* CONFIG_DPP2 */
4907 		res = os_snprintf(buf, buflen, "DPP=1");
4908 #endif /* CONFIG_DPP2 */
4909 		if (os_snprintf_error(buflen, res))
4910 			return -1;
4911 		return res;
4912 	}
4913 #endif /* CONFIG_DPP */
4914 
4915 #ifdef CONFIG_NAN_USD
4916 	if (os_strcmp(field, "nan") == 0) {
4917 		res = os_snprintf(buf, buflen, "USD");
4918 		if (os_snprintf_error(buflen, res))
4919 			return -1;
4920 		return res;
4921 	}
4922 #endif /* CONFIG_NAN_USD */
4923 
4924 #ifdef CONFIG_SAE
4925 	if (os_strcmp(field, "sae") == 0 &&
4926 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE)) {
4927 #ifdef CONFIG_SAE_PK
4928 		res = os_snprintf(buf, buflen, "H2E PK");
4929 #else /* CONFIG_SAE_PK */
4930 		res = os_snprintf(buf, buflen, "H2E");
4931 #endif /* CONFIG_SAE_PK */
4932 		if (os_snprintf_error(buflen, res))
4933 			return -1;
4934 		return res;
4935 	}
4936 #endif /* CONFIG_SAE */
4937 
4938 #ifdef CONFIG_OCV
4939 	if (os_strcmp(field, "ocv") == 0) {
4940 		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
4941 		    (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_OCV))
4942 			res = os_snprintf(buf, buflen, "supported");
4943 		else
4944 			res = os_snprintf(buf, buflen, "not supported");
4945 		if (os_snprintf_error(buflen, res))
4946 			return -1;
4947 		return res;
4948 	}
4949 #endif /* CONFIG_OCV */
4950 
4951 	if (os_strcmp(field, "beacon_prot") == 0) {
4952 		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_BEACON_PROTECTION) ||
4953 		    (wpa_s->drv_flags2 &
4954 		     WPA_DRIVER_FLAGS2_BEACON_PROTECTION_CLIENT))
4955 			res = os_snprintf(buf, buflen, "supported");
4956 		else
4957 			res = os_snprintf(buf, buflen, "not supported");
4958 		if (os_snprintf_error(buflen, res))
4959 			return -1;
4960 		return res;
4961 	}
4962 
4963 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
4964 		   field);
4965 
4966 	return -1;
4967 }
4968 
4969 
4970 #ifdef CONFIG_INTERWORKING
anqp_add_hex(char * pos,char * end,const char * title,struct wpabuf * data)4971 static char * anqp_add_hex(char *pos, char *end, const char *title,
4972 			   struct wpabuf *data)
4973 {
4974 	char *start = pos;
4975 	size_t i;
4976 	int ret;
4977 	const u8 *d;
4978 
4979 	if (data == NULL)
4980 		return start;
4981 
4982 	ret = os_snprintf(pos, end - pos, "%s=", title);
4983 	if (os_snprintf_error(end - pos, ret))
4984 		return start;
4985 	pos += ret;
4986 
4987 	d = wpabuf_head_u8(data);
4988 	for (i = 0; i < wpabuf_len(data); i++) {
4989 		ret = os_snprintf(pos, end - pos, "%02x", *d++);
4990 		if (os_snprintf_error(end - pos, ret))
4991 			return start;
4992 		pos += ret;
4993 	}
4994 
4995 	ret = os_snprintf(pos, end - pos, "\n");
4996 	if (os_snprintf_error(end - pos, ret))
4997 		return start;
4998 	pos += ret;
4999 
5000 	return pos;
5001 }
5002 #endif /* CONFIG_INTERWORKING */
5003 
5004 
5005 #ifdef CONFIG_FILS
print_fils_indication(struct wpa_bss * bss,char * pos,char * end)5006 static int print_fils_indication(struct wpa_bss *bss, char *pos, char *end)
5007 {
5008 	char *start = pos;
5009 	const u8 *ie, *ie_end;
5010 	u16 info, realms;
5011 	int ret;
5012 
5013 	ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
5014 	if (!ie)
5015 		return 0;
5016 	ie_end = ie + 2 + ie[1];
5017 	ie += 2;
5018 	if (ie_end - ie < 2)
5019 		return 0;
5020 
5021 	info = WPA_GET_LE16(ie);
5022 	ie += 2;
5023 	ret = os_snprintf(pos, end - pos, "fils_info=%04x\n", info);
5024 	if (os_snprintf_error(end - pos, ret))
5025 		return 0;
5026 	pos += ret;
5027 
5028 	if (info & BIT(7)) {
5029 		/* Cache Identifier Included */
5030 		if (ie_end - ie < 2)
5031 			return 0;
5032 		ret = os_snprintf(pos, end - pos, "fils_cache_id=%02x%02x\n",
5033 				  ie[0], ie[1]);
5034 		if (os_snprintf_error(end - pos, ret))
5035 			return 0;
5036 		pos += ret;
5037 		ie += 2;
5038 	}
5039 
5040 	if (info & BIT(8)) {
5041 		/* HESSID Included */
5042 		if (ie_end - ie < ETH_ALEN)
5043 			return 0;
5044 		ret = os_snprintf(pos, end - pos, "fils_hessid=" MACSTR "\n",
5045 				  MAC2STR(ie));
5046 		if (os_snprintf_error(end - pos, ret))
5047 			return 0;
5048 		pos += ret;
5049 		ie += ETH_ALEN;
5050 	}
5051 
5052 	realms = (info & (BIT(3) | BIT(4) | BIT(5))) >> 3;
5053 	if (realms) {
5054 		if (ie_end - ie < realms * 2)
5055 			return 0;
5056 		ret = os_snprintf(pos, end - pos, "fils_realms=");
5057 		if (os_snprintf_error(end - pos, ret))
5058 			return 0;
5059 		pos += ret;
5060 
5061 		ret = wpa_snprintf_hex(pos, end - pos, ie, realms * 2);
5062 		if (ret <= 0)
5063 			return 0;
5064 		pos += ret;
5065 		ie += realms * 2;
5066 		ret = os_snprintf(pos, end - pos, "\n");
5067 		if (os_snprintf_error(end - pos, ret))
5068 			return 0;
5069 		pos += ret;
5070 	}
5071 
5072 	return pos - start;
5073 }
5074 #endif /* CONFIG_FILS */
5075 
5076 
print_rnr(struct wpa_bss * bss,char * pos,char * end)5077 static int print_rnr(struct wpa_bss *bss, char *pos, char *end)
5078 {
5079 	char *start = pos;
5080 	const u8 *ie, *ie_end;
5081 	unsigned int n = 0;
5082 	int ret;
5083 
5084 	ie = wpa_bss_get_ie(bss, WLAN_EID_REDUCED_NEIGHBOR_REPORT);
5085 	if (!ie)
5086 		return 0;
5087 
5088 	ie_end = ie + 2 + ie[1];
5089 	ie += 2;
5090 
5091 	while (ie < ie_end) {
5092 		const struct ieee80211_neighbor_ap_info *info =
5093 			(const struct ieee80211_neighbor_ap_info *) ie;
5094 		const u8 *tbtt_start;
5095 		size_t left = ie_end - ie;
5096 
5097 		if (left < sizeof(struct ieee80211_neighbor_ap_info))
5098 			return 0;
5099 
5100 		left -= sizeof(struct ieee80211_neighbor_ap_info);
5101 		if (left < info->tbtt_info_len)
5102 			return 0;
5103 
5104 		ret = os_snprintf(pos, end - pos,
5105 				  "ap_info[%u]: tbtt_info: hdr=0x%x, len=%u, op_c=%u, channel=%u, ",
5106 				  n, *ie, info->tbtt_info_len,
5107 				  info->op_class, info->channel);
5108 		if (os_snprintf_error(end - pos, ret))
5109 			return 0;
5110 		pos += ret;
5111 
5112 		ie += sizeof(struct ieee80211_neighbor_ap_info);
5113 		tbtt_start = ie;
5114 		if (info->tbtt_info_len >= 1) {
5115 			ret = os_snprintf(pos, end - pos,
5116 					  "tbtt_offset=%u, ", *ie);
5117 			if (os_snprintf_error(end - pos, ret))
5118 				return 0;
5119 
5120 			ie++;
5121 			pos += ret;
5122 		}
5123 
5124 		if (info->tbtt_info_len >= 7) {
5125 			ret = os_snprintf(pos, end - pos,
5126 					  "bssid=" MACSTR ", ",
5127 					  MAC2STR(ie));
5128 			if (os_snprintf_error(end - pos, ret))
5129 				return 0;
5130 
5131 			ie += ETH_ALEN;
5132 			pos += ret;
5133 		}
5134 
5135 		if (info->tbtt_info_len >= 11) {
5136 			ret = os_snprintf(pos, end - pos,
5137 					  "short SSID=0x%x, ",
5138 					  WPA_GET_LE32(ie));
5139 			if (os_snprintf_error(end - pos, ret))
5140 				return 0;
5141 
5142 			ie += 4;
5143 			pos += ret;
5144 		}
5145 
5146 		if (info->tbtt_info_len >= 12) {
5147 			ret = os_snprintf(pos, end - pos,
5148 					  "bss_params=0x%x, ", *ie);
5149 			if (os_snprintf_error(end - pos, ret))
5150 				return 0;
5151 
5152 			ie++;
5153 			pos += ret;
5154 		}
5155 
5156 		if (info->tbtt_info_len >= 13) {
5157 			ret = os_snprintf(pos, end - pos,
5158 					  "PSD=0x%x, ", *ie);
5159 			if (os_snprintf_error(end - pos, ret))
5160 				return 0;
5161 
5162 			ie++;
5163 			pos += ret;
5164 		}
5165 
5166 		if (info->tbtt_info_len >= 16) {
5167 			ret = os_snprintf(pos, end - pos,
5168 					  "mld ID=%u, link ID=%u",
5169 					  *ie, *(ie + 1) & 0xF);
5170 			if (os_snprintf_error(end - pos, ret))
5171 				return 0;
5172 
5173 			ie += 3;
5174 			pos += ret;
5175 		}
5176 
5177 		ie = tbtt_start + info->tbtt_info_len;
5178 
5179 		ret = os_snprintf(pos, end - pos, "\n");
5180 		if (os_snprintf_error(end - pos, ret))
5181 			return 0;
5182 		pos += ret;
5183 
5184 		n++;
5185 	}
5186 
5187 	return pos - start;
5188 }
5189 
5190 
print_ml(struct wpa_bss * bss,char * pos,char * end)5191 static int print_ml(struct wpa_bss *bss, char *pos, char *end)
5192 {
5193 	const struct ieee80211_eht_ml *ml;
5194 	char *start = pos;
5195 	const u8 *ie, *ie_end;
5196 	u16 ml_control;
5197 	u8 common_info_length;
5198 	int ret;
5199 
5200 	ie = get_ml_ie(wpa_bss_ie_ptr(bss), bss->ie_len,
5201 		       MULTI_LINK_CONTROL_TYPE_BASIC);
5202 	if (!ie)
5203 		return 0;
5204 
5205 	ie_end = ie + 2 + ie[1];
5206 	ie += 3;
5207 	ml = (const struct ieee80211_eht_ml *) ie;
5208 
5209 	/* control + common info length + MLD MAC Address */
5210 	if (ie_end - ie < 2 + 1 + ETH_ALEN)
5211 		return 0;
5212 
5213 	ml_control = le_to_host16(ml->ml_control);
5214 
5215 	common_info_length = *(ie + 2);
5216 	ret = os_snprintf(pos, end - pos,
5217 			  "multi-link: control=0x%x, common info len=%u",
5218 			  ml_control, common_info_length);
5219 	if (os_snprintf_error(end - pos, ret))
5220 		return 0;
5221 	pos += ret;
5222 
5223 	ie += 2;
5224 	if (ie_end - ie < common_info_length)
5225 		return 0;
5226 
5227 	ie++;
5228 	common_info_length--;
5229 
5230 	if (common_info_length < ETH_ALEN)
5231 		return 0;
5232 
5233 	ret = os_snprintf(pos, end - pos, ", MLD addr=" MACSTR, MAC2STR(ie));
5234 	if (os_snprintf_error(end - pos, ret))
5235 		return 0;
5236 	pos += ret;
5237 
5238 	ie += ETH_ALEN;
5239 	common_info_length -= ETH_ALEN;
5240 
5241 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_LINK_ID) {
5242 		if (common_info_length < 1)
5243 			return 0;
5244 
5245 		ret = os_snprintf(pos, end - pos, ", link ID=%u", *ie & 0x0f);
5246 		if (os_snprintf_error(end - pos, ret))
5247 			return 0;
5248 		pos += ret;
5249 		ie++;
5250 		common_info_length--;
5251 	}
5252 
5253 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_BSS_PARAM_CH_COUNT) {
5254 		if (common_info_length < 1)
5255 			return 0;
5256 
5257 		ret = os_snprintf(pos, end - pos,
5258 				  ", BSS change parameters=0x%x", *ie);
5259 		if (os_snprintf_error(end - pos, ret))
5260 			return 0;
5261 		pos += ret;
5262 		ie++;
5263 		common_info_length--;
5264 	}
5265 
5266 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_MSD_INFO) {
5267 		if (common_info_length < 2)
5268 			return 0;
5269 
5270 		ret = os_snprintf(pos, end - pos, ", MSD Info=0x%x",
5271 				  WPA_GET_LE16(ie));
5272 		if (os_snprintf_error(end - pos, ret))
5273 			return 0;
5274 		pos += ret;
5275 		ie += 2;
5276 		common_info_length -= 2;
5277 	}
5278 
5279 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_EML_CAPA) {
5280 		if (common_info_length < 2)
5281 			return 0;
5282 
5283 		ret = os_snprintf(pos, end - pos, ", EML capabilities=0x%x",
5284 				  WPA_GET_LE16(ie));
5285 		if (os_snprintf_error(end - pos, ret))
5286 			return 0;
5287 		pos += ret;
5288 		ie += 2;
5289 		common_info_length -= 2;
5290 	}
5291 
5292 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_MLD_CAPA) {
5293 		if (common_info_length < 2)
5294 			return 0;
5295 
5296 		ret = os_snprintf(pos, end - pos, ", MLD capabilities=0x%x",
5297 				  WPA_GET_LE16(ie));
5298 		if (os_snprintf_error(end - pos, ret))
5299 			return 0;
5300 		pos += ret;
5301 		ie += 2;
5302 		common_info_length -= 2;
5303 	}
5304 
5305 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_AP_MLD_ID) {
5306 		if (common_info_length < 1)
5307 			return 0;
5308 
5309 		ret = os_snprintf(pos, end - pos, ", MLD ID=0x%x", *ie);
5310 		if (os_snprintf_error(end - pos, ret))
5311 			return 0;
5312 		pos += ret;
5313 		ie += 1;
5314 		common_info_length--;
5315 	}
5316 
5317 	ret = os_snprintf(pos, end - pos, "\n");
5318 	if (os_snprintf_error(end - pos, ret))
5319 		return 0;
5320 	pos += ret;
5321 
5322 	return pos - start;
5323 }
5324 
5325 
print_bss_info(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,unsigned long mask,char * buf,size_t buflen)5326 static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5327 			  unsigned long mask, char *buf, size_t buflen)
5328 {
5329 	size_t i;
5330 	int ret;
5331 	char *pos, *end;
5332 	const u8 *ie, *ie2, *osen_ie, *mesh, *owe, *rsnxe;
5333 
5334 	pos = buf;
5335 	end = buf + buflen;
5336 
5337 	if (mask & WPA_BSS_MASK_ID) {
5338 		ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
5339 		if (os_snprintf_error(end - pos, ret))
5340 			return 0;
5341 		pos += ret;
5342 	}
5343 
5344 	if (mask & WPA_BSS_MASK_BSSID) {
5345 		ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
5346 				  MAC2STR(bss->bssid));
5347 		if (os_snprintf_error(end - pos, ret))
5348 			return 0;
5349 		pos += ret;
5350 	}
5351 
5352 	if (mask & WPA_BSS_MASK_FREQ) {
5353 		ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
5354 		if (os_snprintf_error(end - pos, ret))
5355 			return 0;
5356 		pos += ret;
5357 	}
5358 
5359 	if (mask & WPA_BSS_MASK_BEACON_INT) {
5360 		ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
5361 				  bss->beacon_int);
5362 		if (os_snprintf_error(end - pos, ret))
5363 			return 0;
5364 		pos += ret;
5365 	}
5366 
5367 	if (mask & WPA_BSS_MASK_CAPABILITIES) {
5368 		ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
5369 				  bss->caps);
5370 		if (os_snprintf_error(end - pos, ret))
5371 			return 0;
5372 		pos += ret;
5373 	}
5374 
5375 	if (mask & WPA_BSS_MASK_QUAL) {
5376 		ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
5377 		if (os_snprintf_error(end - pos, ret))
5378 			return 0;
5379 		pos += ret;
5380 	}
5381 
5382 	if (mask & WPA_BSS_MASK_NOISE) {
5383 		ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
5384 		if (os_snprintf_error(end - pos, ret))
5385 			return 0;
5386 		pos += ret;
5387 	}
5388 
5389 	if (mask & WPA_BSS_MASK_LEVEL) {
5390 		ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
5391 		if (os_snprintf_error(end - pos, ret))
5392 			return 0;
5393 		pos += ret;
5394 	}
5395 
5396 	if (mask & WPA_BSS_MASK_TSF) {
5397 		ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
5398 				  (unsigned long long) bss->tsf);
5399 		if (os_snprintf_error(end - pos, ret))
5400 			return 0;
5401 		pos += ret;
5402 	}
5403 
5404 	if (mask & WPA_BSS_MASK_AGE) {
5405 		struct os_reltime now;
5406 
5407 		os_get_reltime(&now);
5408 		ret = os_snprintf(pos, end - pos, "age=%d\n",
5409 				  (int) (now.sec - bss->last_update.sec));
5410 		if (os_snprintf_error(end - pos, ret))
5411 			return 0;
5412 		pos += ret;
5413 	}
5414 
5415 	if (mask & WPA_BSS_MASK_IE) {
5416 		ret = os_snprintf(pos, end - pos, "ie=");
5417 		if (os_snprintf_error(end - pos, ret))
5418 			return 0;
5419 		pos += ret;
5420 
5421 		ie = wpa_bss_ie_ptr(bss);
5422 		for (i = 0; i < bss->ie_len; i++) {
5423 			ret = os_snprintf(pos, end - pos, "%02x", *ie++);
5424 			if (os_snprintf_error(end - pos, ret))
5425 				return 0;
5426 			pos += ret;
5427 		}
5428 
5429 		ret = os_snprintf(pos, end - pos, "\n");
5430 		if (os_snprintf_error(end - pos, ret))
5431 			return 0;
5432 		pos += ret;
5433 	}
5434 
5435 	if (mask & WPA_BSS_MASK_FLAGS) {
5436 		ret = os_snprintf(pos, end - pos, "flags=");
5437 		if (os_snprintf_error(end - pos, ret))
5438 			return 0;
5439 		pos += ret;
5440 
5441 		mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
5442 
5443 		ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
5444 		if (ie)
5445 			pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
5446 						    2 + ie[1]);
5447 		ie2 = wpa_bss_get_rsne(wpa_s, bss, NULL, false);
5448 		if (ie2)
5449 			pos = wpa_supplicant_ie_txt(pos, end,
5450 						    mesh ? "RSN" : "WPA2", ie2,
5451 						    2 + ie2[1]);
5452 		rsnxe = wpa_bss_get_rsnxe(wpa_s, bss, NULL, false);
5453 		if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_H2E)) {
5454 			ret = os_snprintf(pos, end - pos, "[SAE-H2E]");
5455 			if (os_snprintf_error(end - pos, ret))
5456 				return 0;
5457 			pos += ret;
5458 		}
5459 		if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_PK)) {
5460 			ret = os_snprintf(pos, end - pos, "[SAE-PK]");
5461 			if (os_snprintf_error(end - pos, ret))
5462 				return 0;
5463 			pos += ret;
5464 		}
5465 		osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
5466 		if (osen_ie)
5467 			pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
5468 						    osen_ie, 2 + osen_ie[1]);
5469 		owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
5470 		if (owe) {
5471 			ret = os_snprintf(
5472 				pos, end - pos,
5473 				ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
5474 			if (os_snprintf_error(end - pos, ret))
5475 				return 0;
5476 			pos += ret;
5477 		}
5478 		pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
5479 		if (!ie && !ie2 && !osen_ie &&
5480 		    (bss->caps & IEEE80211_CAP_PRIVACY)) {
5481 			ret = os_snprintf(pos, end - pos, "[WEP]");
5482 			if (os_snprintf_error(end - pos, ret))
5483 				return 0;
5484 			pos += ret;
5485 		}
5486 
5487 		if (mesh) {
5488 			ret = os_snprintf(pos, end - pos, "[MESH]");
5489 			if (os_snprintf_error(end - pos, ret))
5490 				return 0;
5491 			pos += ret;
5492 		}
5493 
5494 		if (bss_is_dmg(bss)) {
5495 			const char *s;
5496 			ret = os_snprintf(pos, end - pos, "[DMG]");
5497 			if (os_snprintf_error(end - pos, ret))
5498 				return 0;
5499 			pos += ret;
5500 			switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
5501 			case IEEE80211_CAP_DMG_IBSS:
5502 				s = "[IBSS]";
5503 				break;
5504 			case IEEE80211_CAP_DMG_AP:
5505 				s = "[ESS]";
5506 				break;
5507 			case IEEE80211_CAP_DMG_PBSS:
5508 				s = "[PBSS]";
5509 				break;
5510 			default:
5511 				s = "";
5512 				break;
5513 			}
5514 			ret = os_snprintf(pos, end - pos, "%s", s);
5515 			if (os_snprintf_error(end - pos, ret))
5516 				return 0;
5517 			pos += ret;
5518 		} else {
5519 			if (bss->caps & IEEE80211_CAP_IBSS) {
5520 				ret = os_snprintf(pos, end - pos, "[IBSS]");
5521 				if (os_snprintf_error(end - pos, ret))
5522 					return 0;
5523 				pos += ret;
5524 			}
5525 			if (bss->caps & IEEE80211_CAP_ESS) {
5526 				ret = os_snprintf(pos, end - pos, "[ESS]");
5527 				if (os_snprintf_error(end - pos, ret))
5528 					return 0;
5529 				pos += ret;
5530 			}
5531 		}
5532 		if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
5533 		    wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
5534 			ret = os_snprintf(pos, end - pos, "[P2P]");
5535 			if (os_snprintf_error(end - pos, ret))
5536 				return 0;
5537 			pos += ret;
5538 		}
5539 #ifdef CONFIG_HS20
5540 		if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
5541 			ret = os_snprintf(pos, end - pos, "[HS20]");
5542 			if (os_snprintf_error(end - pos, ret))
5543 				return 0;
5544 			pos += ret;
5545 		}
5546 #endif /* CONFIG_HS20 */
5547 #ifdef CONFIG_FILS
5548 		if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
5549 			ret = os_snprintf(pos, end - pos, "[FILS]");
5550 			if (os_snprintf_error(end - pos, ret))
5551 				return 0;
5552 			pos += ret;
5553 		}
5554 #endif /* CONFIG_FILS */
5555 #ifdef CONFIG_FST
5556 		if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
5557 			ret = os_snprintf(pos, end - pos, "[FST]");
5558 			if (os_snprintf_error(end - pos, ret))
5559 				return 0;
5560 			pos += ret;
5561 		}
5562 #endif /* CONFIG_FST */
5563 		if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_UTF_8_SSID)) {
5564 			ret = os_snprintf(pos, end - pos, "[UTF-8]");
5565 			if (os_snprintf_error(end - pos, ret))
5566 				return 0;
5567 			pos += ret;
5568 		}
5569 
5570 		ret = os_snprintf(pos, end - pos, "\n");
5571 		if (os_snprintf_error(end - pos, ret))
5572 			return 0;
5573 		pos += ret;
5574 	}
5575 
5576 	if (mask & WPA_BSS_MASK_SSID) {
5577 		ret = os_snprintf(pos, end - pos, "ssid=%s\n",
5578 				  wpa_ssid_txt(bss->ssid, bss->ssid_len));
5579 		if (os_snprintf_error(end - pos, ret))
5580 			return 0;
5581 		pos += ret;
5582 	}
5583 
5584 #ifdef CONFIG_WPS
5585 	if (mask & WPA_BSS_MASK_WPS_SCAN) {
5586 		ie = wpa_bss_ie_ptr(bss);
5587 		ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
5588 		if (ret >= end - pos)
5589 			return 0;
5590 		if (ret > 0)
5591 			pos += ret;
5592 	}
5593 #endif /* CONFIG_WPS */
5594 
5595 #ifdef CONFIG_P2P
5596 	if (mask & WPA_BSS_MASK_P2P_SCAN) {
5597 		ie = wpa_bss_ie_ptr(bss);
5598 		ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
5599 		if (ret >= end - pos)
5600 			return 0;
5601 		if (ret > 0)
5602 			pos += ret;
5603 	}
5604 #endif /* CONFIG_P2P */
5605 
5606 #ifdef CONFIG_WIFI_DISPLAY
5607 	if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
5608 		struct wpabuf *wfd;
5609 
5610 		ie = wpa_bss_ie_ptr(bss);
5611 		wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
5612 						  WFD_IE_VENDOR_TYPE);
5613 		if (wfd) {
5614 			ret = os_snprintf(pos, end - pos, "wfd_subelems=");
5615 			if (os_snprintf_error(end - pos, ret)) {
5616 				wpabuf_free(wfd);
5617 				return 0;
5618 			}
5619 			pos += ret;
5620 
5621 			pos += wpa_snprintf_hex(pos, end - pos,
5622 						wpabuf_head(wfd),
5623 						wpabuf_len(wfd));
5624 			wpabuf_free(wfd);
5625 
5626 			ret = os_snprintf(pos, end - pos, "\n");
5627 			if (os_snprintf_error(end - pos, ret))
5628 				return 0;
5629 			pos += ret;
5630 		}
5631 	}
5632 #endif /* CONFIG_WIFI_DISPLAY */
5633 
5634 #ifdef CONFIG_INTERWORKING
5635 	if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
5636 		struct wpa_bss_anqp *anqp = bss->anqp;
5637 		struct wpa_bss_anqp_elem *elem;
5638 
5639 		pos = anqp_add_hex(pos, end, "anqp_capability_list",
5640 				   anqp->capability_list);
5641 		pos = anqp_add_hex(pos, end, "anqp_venue_name",
5642 				   anqp->venue_name);
5643 		pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
5644 				   anqp->network_auth_type);
5645 		pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
5646 				   anqp->roaming_consortium);
5647 		pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
5648 				   anqp->ip_addr_type_availability);
5649 		pos = anqp_add_hex(pos, end, "anqp_nai_realm",
5650 				   anqp->nai_realm);
5651 		pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
5652 		pos = anqp_add_hex(pos, end, "anqp_domain_name",
5653 				   anqp->domain_name);
5654 		pos = anqp_add_hex(pos, end, "anqp_fils_realm_info",
5655 				   anqp->fils_realm_info);
5656 #ifdef CONFIG_HS20
5657 		pos = anqp_add_hex(pos, end, "hs20_capability_list",
5658 				   anqp->hs20_capability_list);
5659 		pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
5660 				   anqp->hs20_operator_friendly_name);
5661 		pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
5662 				   anqp->hs20_wan_metrics);
5663 		pos = anqp_add_hex(pos, end, "hs20_connection_capability",
5664 				   anqp->hs20_connection_capability);
5665 		pos = anqp_add_hex(pos, end, "hs20_operating_class",
5666 				   anqp->hs20_operating_class);
5667 		pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
5668 				   anqp->hs20_osu_providers_list);
5669 		pos = anqp_add_hex(pos, end, "hs20_operator_icon_metadata",
5670 				   anqp->hs20_operator_icon_metadata);
5671 		pos = anqp_add_hex(pos, end, "hs20_osu_providers_nai_list",
5672 				   anqp->hs20_osu_providers_nai_list);
5673 #endif /* CONFIG_HS20 */
5674 
5675 		dl_list_for_each(elem, &anqp->anqp_elems,
5676 				 struct wpa_bss_anqp_elem, list) {
5677 			char title[20];
5678 
5679 			os_snprintf(title, sizeof(title), "anqp[%u]",
5680 				    elem->infoid);
5681 			pos = anqp_add_hex(pos, end, title, elem->payload);
5682 			if (elem->protected_response) {
5683 				ret = os_snprintf(pos, end - pos,
5684 						  "protected-anqp-info[%u]=1\n",
5685 						  elem->infoid);
5686 				if (os_snprintf_error(end - pos, ret))
5687 					return 0;
5688 				pos += ret;
5689 			}
5690 		}
5691 	}
5692 #endif /* CONFIG_INTERWORKING */
5693 
5694 #ifdef CONFIG_MESH
5695 	if (mask & WPA_BSS_MASK_MESH_SCAN) {
5696 		ie = wpa_bss_ie_ptr(bss);
5697 		ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
5698 		if (ret >= end - pos)
5699 			return 0;
5700 		if (ret > 0)
5701 			pos += ret;
5702 	}
5703 #endif /* CONFIG_MESH */
5704 
5705 	if (mask & WPA_BSS_MASK_SNR) {
5706 		ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
5707 		if (os_snprintf_error(end - pos, ret))
5708 			return 0;
5709 		pos += ret;
5710 	}
5711 
5712 	if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
5713 		ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
5714 				  bss->est_throughput);
5715 		if (os_snprintf_error(end - pos, ret))
5716 			return 0;
5717 		pos += ret;
5718 	}
5719 
5720 #ifdef CONFIG_FST
5721 	if (mask & WPA_BSS_MASK_FST) {
5722 		ret = fst_ctrl_iface_mb_info(bss->bssid, pos, end - pos);
5723 		if (ret < 0 || ret >= end - pos)
5724 			return 0;
5725 		pos += ret;
5726 	}
5727 #endif /* CONFIG_FST */
5728 
5729 	if (mask & WPA_BSS_MASK_UPDATE_IDX) {
5730 		ret = os_snprintf(pos, end - pos, "update_idx=%u\n",
5731 				  bss->last_update_idx);
5732 		if (os_snprintf_error(end - pos, ret))
5733 			return 0;
5734 		pos += ret;
5735 	}
5736 
5737 	if ((mask & WPA_BSS_MASK_BEACON_IE) && bss->beacon_ie_len) {
5738 		ret = os_snprintf(pos, end - pos, "beacon_ie=");
5739 		if (os_snprintf_error(end - pos, ret))
5740 			return 0;
5741 		pos += ret;
5742 
5743 		ie = wpa_bss_ie_ptr(bss);
5744 		ie += bss->ie_len;
5745 		for (i = 0; i < bss->beacon_ie_len; i++) {
5746 			ret = os_snprintf(pos, end - pos, "%02x", *ie++);
5747 			if (os_snprintf_error(end - pos, ret))
5748 				return 0;
5749 			pos += ret;
5750 		}
5751 
5752 		ret = os_snprintf(pos, end - pos, "\n");
5753 		if (os_snprintf_error(end - pos, ret))
5754 			return 0;
5755 		pos += ret;
5756 	}
5757 
5758 #ifdef CONFIG_FILS
5759 	if (mask & WPA_BSS_MASK_FILS_INDICATION) {
5760 		ret = print_fils_indication(bss, pos, end);
5761 		pos += ret;
5762 	}
5763 #endif /* CONFIG_FILS */
5764 
5765 	if (!is_zero_ether_addr(bss->mld_addr) &&
5766 	    (mask & WPA_BSS_MASK_AP_MLD_ADDR)) {
5767 		ret = os_snprintf(pos, end - pos,
5768 				  "ap_mld_addr=" MACSTR "\n",
5769 				  MAC2STR(bss->mld_addr));
5770 		if (os_snprintf_error(end - pos, ret))
5771 			return 0;
5772 		pos += ret;
5773 	}
5774 
5775 	if (mask & WPA_BSS_MASK_RNR)
5776 		pos += print_rnr(bss, pos, end);
5777 
5778 	if (mask & WPA_BSS_MASK_ML)
5779 		pos += print_ml(bss, pos, end);
5780 
5781 	if (mask & WPA_BSS_MASK_DELIM) {
5782 		ret = os_snprintf(pos, end - pos, "====\n");
5783 		if (os_snprintf_error(end - pos, ret))
5784 			return 0;
5785 		pos += ret;
5786 	}
5787 
5788 	return pos - buf;
5789 }
5790 
5791 
wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)5792 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
5793 					 const char *cmd, char *buf,
5794 					 size_t buflen)
5795 {
5796 	u8 bssid[ETH_ALEN];
5797 	size_t i;
5798 	struct wpa_bss *bss;
5799 	struct wpa_bss *bsslast = NULL;
5800 	struct dl_list *next;
5801 	int ret = 0;
5802 	int len;
5803 	char *ctmp, *end = buf + buflen;
5804 	unsigned long mask = WPA_BSS_MASK_ALL;
5805 
5806 	if (os_strncmp(cmd, "RANGE=", 6) == 0) {
5807 		if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
5808 			bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
5809 					    list_id);
5810 			bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
5811 					       list_id);
5812 		} else { /* N1-N2 */
5813 			unsigned int id1, id2;
5814 
5815 			if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
5816 				wpa_printf(MSG_INFO, "Wrong BSS range "
5817 					   "format");
5818 				return 0;
5819 			}
5820 
5821 			if (*(cmd + 6) == '-')
5822 				id1 = 0;
5823 			else
5824 				id1 = atoi(cmd + 6);
5825 			ctmp++;
5826 			if (*ctmp >= '0' && *ctmp <= '9')
5827 				id2 = atoi(ctmp);
5828 			else
5829 				id2 = (unsigned int) -1;
5830 			bss = wpa_bss_get_id_range(wpa_s, id1, id2);
5831 			if (id2 == (unsigned int) -1)
5832 				bsslast = dl_list_last(&wpa_s->bss_id,
5833 						       struct wpa_bss,
5834 						       list_id);
5835 			else {
5836 				bsslast = wpa_bss_get_id(wpa_s, id2);
5837 				if (bsslast == NULL && bss && id2 > id1) {
5838 					struct wpa_bss *tmp = bss;
5839 					for (;;) {
5840 						next = tmp->list_id.next;
5841 						if (next == &wpa_s->bss_id)
5842 							break;
5843 						tmp = dl_list_entry(
5844 							next, struct wpa_bss,
5845 							list_id);
5846 						if (tmp->id > id2)
5847 							break;
5848 						bsslast = tmp;
5849 					}
5850 				}
5851 			}
5852 		}
5853 	} else if (os_strncmp(cmd, "FIRST", 5) == 0)
5854 		bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
5855 	else if (os_strncmp(cmd, "LAST", 4) == 0)
5856 		bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
5857 	else if (os_strncmp(cmd, "ID-", 3) == 0) {
5858 		i = atoi(cmd + 3);
5859 		bss = wpa_bss_get_id(wpa_s, i);
5860 	} else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
5861 		i = atoi(cmd + 5);
5862 		bss = wpa_bss_get_id(wpa_s, i);
5863 		if (bss) {
5864 			next = bss->list_id.next;
5865 			if (next == &wpa_s->bss_id)
5866 				bss = NULL;
5867 			else
5868 				bss = dl_list_entry(next, struct wpa_bss,
5869 						    list_id);
5870 		}
5871 	} else if (os_strncmp(cmd, "CURRENT", 7) == 0) {
5872 		bss = wpa_s->current_bss;
5873 #ifdef CONFIG_P2P
5874 	} else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
5875 		if (hwaddr_aton(cmd + 13, bssid) == 0)
5876 			bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
5877 		else
5878 			bss = NULL;
5879 #endif /* CONFIG_P2P */
5880 	} else if (hwaddr_aton(cmd, bssid) == 0)
5881 		bss = wpa_bss_get_bssid(wpa_s, bssid);
5882 	else {
5883 		struct wpa_bss *tmp;
5884 		i = atoi(cmd);
5885 		bss = NULL;
5886 		dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
5887 		{
5888 			if (i == 0) {
5889 				bss = tmp;
5890 				break;
5891 			}
5892 			i--;
5893 		}
5894 	}
5895 
5896 	if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
5897 		mask = strtoul(ctmp + 5, NULL, 0x10);
5898 		if (mask == 0)
5899 			mask = WPA_BSS_MASK_ALL;
5900 	}
5901 
5902 	if (bss == NULL)
5903 		return 0;
5904 
5905 	if (bsslast == NULL)
5906 		bsslast = bss;
5907 	do {
5908 		len = print_bss_info(wpa_s, bss, mask, buf, buflen);
5909 		ret += len;
5910 		buf += len;
5911 		buflen -= len;
5912 		if (bss == bsslast) {
5913 			if ((mask & WPA_BSS_MASK_DELIM) && len &&
5914 			    (bss == dl_list_last(&wpa_s->bss_id,
5915 						 struct wpa_bss, list_id))) {
5916 				int res;
5917 
5918 				res = os_snprintf(buf - 5, end - buf + 5,
5919 						  "####\n");
5920 				if (os_snprintf_error(end - buf + 5, res)) {
5921 					wpa_printf(MSG_DEBUG,
5922 						   "Could not add end delim");
5923 				}
5924 			}
5925 			break;
5926 		}
5927 		next = bss->list_id.next;
5928 		if (next == &wpa_s->bss_id)
5929 			break;
5930 		bss = dl_list_entry(next, struct wpa_bss, list_id);
5931 	} while (bss && len);
5932 
5933 	return ret;
5934 }
5935 
5936 
wpa_supplicant_ctrl_iface_ap_scan(struct wpa_supplicant * wpa_s,char * cmd)5937 static int wpa_supplicant_ctrl_iface_ap_scan(
5938 	struct wpa_supplicant *wpa_s, char *cmd)
5939 {
5940 	int ap_scan = atoi(cmd);
5941 	return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
5942 }
5943 
5944 
wpa_supplicant_ctrl_iface_scan_interval(struct wpa_supplicant * wpa_s,char * cmd)5945 static int wpa_supplicant_ctrl_iface_scan_interval(
5946 	struct wpa_supplicant *wpa_s, char *cmd)
5947 {
5948 	int scan_int = atoi(cmd);
5949 	return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
5950 }
5951 
5952 
wpa_supplicant_ctrl_iface_bss_expire_age(struct wpa_supplicant * wpa_s,char * cmd)5953 static int wpa_supplicant_ctrl_iface_bss_expire_age(
5954 	struct wpa_supplicant *wpa_s, char *cmd)
5955 {
5956 	int expire_age = atoi(cmd);
5957 	return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
5958 }
5959 
5960 
wpa_supplicant_ctrl_iface_bss_expire_count(struct wpa_supplicant * wpa_s,char * cmd)5961 static int wpa_supplicant_ctrl_iface_bss_expire_count(
5962 	struct wpa_supplicant *wpa_s, char *cmd)
5963 {
5964 	int expire_count = atoi(cmd);
5965 	return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
5966 }
5967 
5968 
wpa_supplicant_ctrl_iface_bss_flush(struct wpa_supplicant * wpa_s,char * cmd)5969 static void wpa_supplicant_ctrl_iface_bss_flush(
5970 	struct wpa_supplicant *wpa_s, char *cmd)
5971 {
5972 	int flush_age = atoi(cmd);
5973 
5974 	if (flush_age == 0)
5975 		wpa_bss_flush(wpa_s);
5976 	else
5977 		wpa_bss_flush_by_age(wpa_s, flush_age);
5978 }
5979 
5980 
5981 #ifdef CONFIG_TESTING_OPTIONS
wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant * wpa_s)5982 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
5983 {
5984 	wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
5985 	/* MLME-DELETEKEYS.request */
5986 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL,
5987 			0, KEY_FLAG_GROUP);
5988 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL,
5989 			0, KEY_FLAG_GROUP);
5990 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL,
5991 			0, KEY_FLAG_GROUP);
5992 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL,
5993 			0, KEY_FLAG_GROUP);
5994 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL,
5995 			0, KEY_FLAG_GROUP);
5996 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL,
5997 			0, KEY_FLAG_GROUP);
5998 
5999 	wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0,
6000 			NULL, 0, KEY_FLAG_PAIRWISE);
6001 	if (wpa_sm_ext_key_id(wpa_s->wpa))
6002 		wpa_drv_set_key(wpa_s, -1, WPA_ALG_NONE, wpa_s->bssid, 1, 0,
6003 				NULL, 0, NULL, 0, KEY_FLAG_PAIRWISE);
6004 	/* MLME-SETPROTECTION.request(None) */
6005 	wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
6006 				   MLME_SETPROTECTION_PROTECT_TYPE_NONE,
6007 				   MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
6008 	wpa_sm_drop_sa(wpa_s->wpa);
6009 }
6010 #endif /* CONFIG_TESTING_OPTIONS */
6011 
6012 
wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant * wpa_s,char * addr)6013 static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
6014 					  char *addr)
6015 {
6016 #ifdef CONFIG_NO_SCAN_PROCESSING
6017 	return -1;
6018 #else /* CONFIG_NO_SCAN_PROCESSING */
6019 	u8 bssid[ETH_ALEN];
6020 	struct wpa_bss *bss;
6021 	struct wpa_ssid *ssid = wpa_s->current_ssid;
6022 	struct wpa_radio_work *already_connecting;
6023 
6024 	if (hwaddr_aton(addr, bssid)) {
6025 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
6026 			   "address '%s'", addr);
6027 		return -1;
6028 	}
6029 
6030 	wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
6031 
6032 	if (!ssid) {
6033 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
6034 			   "configuration known for the target AP");
6035 		return -1;
6036 	}
6037 
6038 	bss = wpa_bss_get_connection(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
6039 	if (!bss) {
6040 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
6041 			   "from BSS table");
6042 		return -1;
6043 	}
6044 
6045 	/*
6046 	 * TODO: Find best network configuration block from configuration to
6047 	 * allow roaming to other networks
6048 	 */
6049 
6050 	already_connecting = radio_work_pending(wpa_s, "sme-connect");
6051 	wpa_s->reassociate = 1;
6052 	wpa_supplicant_connect(wpa_s, bss, ssid);
6053 
6054 	/*
6055 	 * Indicate that an explicitly requested roam is in progress so scan
6056 	 * results that come in before the 'sme-connect' radio work gets
6057 	 * executed do not override the original connection attempt.
6058 	 */
6059 	if (!already_connecting && radio_work_pending(wpa_s, "sme-connect"))
6060 		wpa_s->roam_in_progress = true;
6061 
6062 	return 0;
6063 #endif /* CONFIG_NO_SCAN_PROCESSING */
6064 }
6065 
6066 
6067 #ifdef CONFIG_P2P
p2p_ctrl_find(struct wpa_supplicant * wpa_s,char * cmd)6068 static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
6069 {
6070 	unsigned int timeout = atoi(cmd);
6071 	enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
6072 	u8 dev_id[ETH_ALEN], *_dev_id = NULL;
6073 	u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
6074 	char *pos;
6075 	unsigned int search_delay;
6076 	const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
6077 	u8 seek_count = 0;
6078 	int freq = 0;
6079 	bool include_6ghz = false;
6080 
6081 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6082 		wpa_dbg(wpa_s, MSG_INFO,
6083 			"Reject P2P_FIND since interface is disabled");
6084 		return -1;
6085 	}
6086 
6087 	if (os_strstr(cmd, " include_6ghz"))
6088 		include_6ghz = true;
6089 	if (os_strstr(cmd, "type=social"))
6090 		type = P2P_FIND_ONLY_SOCIAL;
6091 	else if (os_strstr(cmd, "type=progressive"))
6092 		type = P2P_FIND_PROGRESSIVE;
6093 
6094 	pos = os_strstr(cmd, "dev_id=");
6095 	if (pos) {
6096 		pos += 7;
6097 		if (hwaddr_aton(pos, dev_id))
6098 			return -1;
6099 		_dev_id = dev_id;
6100 	}
6101 
6102 	pos = os_strstr(cmd, "dev_type=");
6103 	if (pos) {
6104 		pos += 9;
6105 		if (wps_dev_type_str2bin(pos, dev_type) < 0)
6106 			return -1;
6107 		_dev_type = dev_type;
6108 	}
6109 
6110 	pos = os_strstr(cmd, "delay=");
6111 	if (pos) {
6112 		pos += 6;
6113 		search_delay = atoi(pos);
6114 	} else
6115 		search_delay = wpas_p2p_search_delay(wpa_s);
6116 
6117 	pos = os_strstr(cmd, "freq=");
6118 	if (pos) {
6119 		pos += 5;
6120 		freq = atoi(pos);
6121 		if (freq <= 0)
6122 			return -1;
6123 	}
6124 
6125 	/* Must be searched for last, because it adds nul termination */
6126 	pos = os_strstr(cmd, " seek=");
6127 	if (pos)
6128 		pos += 6;
6129 	while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
6130 		char *term;
6131 
6132 		_seek[seek_count++] = pos;
6133 		seek = _seek;
6134 		term = os_strchr(pos, ' ');
6135 		if (!term)
6136 			break;
6137 		*term = '\0';
6138 		pos = os_strstr(term + 1, "seek=");
6139 		if (pos)
6140 			pos += 5;
6141 	}
6142 	if (seek_count > P2P_MAX_QUERY_HASH) {
6143 		seek[0] = NULL;
6144 		seek_count = 1;
6145 	}
6146 
6147 	return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
6148 			     _dev_id, search_delay, seek_count, seek, freq,
6149 			     include_6ghz);
6150 }
6151 
6152 
p2ps_ctrl_parse_cpt_priority(const char * pos,u8 * cpt)6153 static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
6154 {
6155 	const char *last = NULL;
6156 	const char *token;
6157 	long int token_len;
6158 	unsigned int i;
6159 
6160 	/* Expected predefined CPT names delimited by ':' */
6161 	for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
6162 		if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
6163 			wpa_printf(MSG_ERROR,
6164 				   "P2PS: CPT name list is too long, expected up to %d names",
6165 				   P2PS_FEATURE_CAPAB_CPT_MAX);
6166 			cpt[0] = 0;
6167 			return -1;
6168 		}
6169 
6170 		token_len = last - token;
6171 
6172 		if (token_len  == 3 &&
6173 		    os_memcmp(token, "UDP", token_len) == 0) {
6174 			cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
6175 		} else if (token_len == 3 &&
6176 			   os_memcmp(token, "MAC", token_len) == 0) {
6177 			cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
6178 		} else {
6179 			wpa_printf(MSG_ERROR,
6180 				   "P2PS: Unsupported CPT name '%s'", token);
6181 			cpt[0] = 0;
6182 			return -1;
6183 		}
6184 
6185 		if (isblank((unsigned char) *last)) {
6186 			i++;
6187 			break;
6188 		}
6189 	}
6190 	cpt[i] = 0;
6191 	return 0;
6192 }
6193 
6194 
p2p_parse_asp_provision_cmd(const char * cmd)6195 static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
6196 {
6197 	struct p2ps_provision *p2ps_prov;
6198 	char *pos;
6199 	size_t info_len = 0;
6200 	char *info = NULL;
6201 	u8 role = P2PS_SETUP_NONE;
6202 	long long unsigned val;
6203 	int i;
6204 
6205 	pos = os_strstr(cmd, "info=");
6206 	if (pos) {
6207 		pos += 5;
6208 		info_len = os_strlen(pos);
6209 
6210 		if (info_len) {
6211 			info = os_malloc(info_len + 1);
6212 			if (info) {
6213 				info_len = utf8_unescape(pos, info_len,
6214 							 info, info_len + 1);
6215 			} else
6216 				info_len = 0;
6217 		}
6218 	}
6219 
6220 	p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
6221 	if (p2ps_prov == NULL) {
6222 		os_free(info);
6223 		return NULL;
6224 	}
6225 
6226 	if (info) {
6227 		os_memcpy(p2ps_prov->info, info, info_len);
6228 		p2ps_prov->info[info_len] = '\0';
6229 		os_free(info);
6230 	}
6231 
6232 	pos = os_strstr(cmd, "status=");
6233 	if (pos)
6234 		p2ps_prov->status = atoi(pos + 7);
6235 	else
6236 		p2ps_prov->status = -1;
6237 
6238 	pos = os_strstr(cmd, "adv_id=");
6239 	if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
6240 		goto invalid_args;
6241 	p2ps_prov->adv_id = val;
6242 
6243 	pos = os_strstr(cmd, "method=");
6244 	if (pos)
6245 		p2ps_prov->method = strtol(pos + 7, NULL, 16);
6246 	else
6247 		p2ps_prov->method = 0;
6248 
6249 	pos = os_strstr(cmd, "session=");
6250 	if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
6251 		goto invalid_args;
6252 	p2ps_prov->session_id = val;
6253 
6254 	pos = os_strstr(cmd, "adv_mac=");
6255 	if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
6256 		goto invalid_args;
6257 
6258 	pos = os_strstr(cmd, "session_mac=");
6259 	if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
6260 		goto invalid_args;
6261 
6262 	pos = os_strstr(cmd, "cpt=");
6263 	if (pos) {
6264 		if (p2ps_ctrl_parse_cpt_priority(pos + 4,
6265 						 p2ps_prov->cpt_priority))
6266 			goto invalid_args;
6267 	} else {
6268 		p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
6269 	}
6270 
6271 	for (i = 0; p2ps_prov->cpt_priority[i]; i++)
6272 		p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
6273 
6274 	/* force conncap with tstCap (no validity checks) */
6275 	pos = os_strstr(cmd, "tstCap=");
6276 	if (pos) {
6277 		role = strtol(pos + 7, NULL, 16);
6278 	} else {
6279 		pos = os_strstr(cmd, "role=");
6280 		if (pos) {
6281 			role = strtol(pos + 5, NULL, 16);
6282 			if (role != P2PS_SETUP_CLIENT &&
6283 			    role != P2PS_SETUP_GROUP_OWNER)
6284 				role = P2PS_SETUP_NONE;
6285 		}
6286 	}
6287 	p2ps_prov->role = role;
6288 
6289 	return p2ps_prov;
6290 
6291 invalid_args:
6292 	os_free(p2ps_prov);
6293 	return NULL;
6294 }
6295 
6296 
p2p_ctrl_asp_provision_resp(struct wpa_supplicant * wpa_s,char * cmd)6297 static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
6298 {
6299 	u8 addr[ETH_ALEN];
6300 	struct p2ps_provision *p2ps_prov;
6301 	char *pos;
6302 
6303 	/* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
6304 
6305 	wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
6306 
6307 	if (hwaddr_aton(cmd, addr))
6308 		return -1;
6309 
6310 	pos = cmd + 17;
6311 	if (*pos != ' ')
6312 		return -1;
6313 
6314 	p2ps_prov = p2p_parse_asp_provision_cmd(pos);
6315 	if (!p2ps_prov)
6316 		return -1;
6317 
6318 	if (p2ps_prov->status < 0) {
6319 		os_free(p2ps_prov);
6320 		return -1;
6321 	}
6322 
6323 	return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
6324 				  p2ps_prov);
6325 }
6326 
6327 
p2p_ctrl_asp_provision(struct wpa_supplicant * wpa_s,char * cmd)6328 static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
6329 {
6330 	u8 addr[ETH_ALEN];
6331 	struct p2ps_provision *p2ps_prov;
6332 	char *pos;
6333 
6334 	/* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
6335 	 *        session=<ses_id> mac=<ses_mac> [info=<infodata>]
6336 	 */
6337 
6338 	wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
6339 	if (hwaddr_aton(cmd, addr))
6340 		return -1;
6341 
6342 	pos = cmd + 17;
6343 	if (*pos != ' ')
6344 		return -1;
6345 
6346 	p2ps_prov = p2p_parse_asp_provision_cmd(pos);
6347 	if (!p2ps_prov)
6348 		return -1;
6349 
6350 	p2ps_prov->pd_seeker = 1;
6351 
6352 	return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
6353 				  p2ps_prov);
6354 }
6355 
6356 
p2p_ctrl_connect(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)6357 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
6358 			    char *buf, size_t buflen)
6359 {
6360 	u8 addr[ETH_ALEN];
6361 	char *pos, *pos2;
6362 	char *pin = NULL;
6363 	enum p2p_wps_method wps_method;
6364 	int new_pin;
6365 	int ret;
6366 	int persistent_group, persistent_id = -1;
6367 	int join;
6368 	int auth;
6369 	int automatic;
6370 	int go_intent = -1;
6371 	int freq = 0;
6372 	int pd;
6373 	int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
6374 	int edmg;
6375 	u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
6376 	size_t group_ssid_len = 0;
6377 	int he;
6378 	bool allow_6ghz;
6379 	bool p2p2;
6380 	u16 bootstrap = 0;
6381 	const char *password = NULL;
6382 	char *token, *context = NULL;
6383 
6384 	if (!wpa_s->global->p2p_init_wpa_s)
6385 		return -1;
6386 	if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
6387 		wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
6388 			wpa_s->global->p2p_init_wpa_s->ifname);
6389 		wpa_s = wpa_s->global->p2p_init_wpa_s;
6390 	}
6391 
6392 	/* <addr> <"pbc" | "pin" | "pair" | PIN> [label|display|keypad|p2ps]
6393 	 * [persistent|persistent=<network id>]
6394 	 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
6395 	 * [ht40] [vht] [he] [edmg] [auto] [ssid=<hexdump>]
6396 	 * [p2p2] [bstrapmethod=<value>] [password=<string>]
6397 	 */
6398 
6399 	if (hwaddr_aton(cmd, addr))
6400 		return -1;
6401 
6402 	pos = cmd + 17;
6403 	if (*pos != ' ')
6404 		return -1;
6405 	pos++;
6406 
6407 	persistent_group = os_strstr(pos, " persistent") != NULL;
6408 	pos2 = os_strstr(pos, " persistent=");
6409 	if (pos2) {
6410 		struct wpa_ssid *ssid;
6411 		persistent_id = atoi(pos2 + 12);
6412 		ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6413 		if (ssid == NULL || ssid->disabled != 2 ||
6414 		    ssid->mode != WPAS_MODE_P2P_GO) {
6415 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
6416 				   "SSID id=%d for persistent P2P group (GO)",
6417 				   persistent_id);
6418 			return -1;
6419 		}
6420 	}
6421 	join = os_strstr(pos, " join") != NULL;
6422 	allow_6ghz = os_strstr(pos, " allow_6ghz") != NULL;
6423 	auth = os_strstr(pos, " auth") != NULL;
6424 	automatic = os_strstr(pos, " auto") != NULL;
6425 	pd = os_strstr(pos, " provdisc") != NULL;
6426 	vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
6427 	ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
6428 		vht;
6429 	he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
6430 	edmg = (os_strstr(cmd, " edmg") != NULL) || wpa_s->conf->p2p_go_edmg;
6431 	p2p2 = os_strstr(pos, " p2p2") != NULL;
6432 
6433 	pos2 = os_strstr(pos, " go_intent=");
6434 	if (pos2) {
6435 		pos2 += 11;
6436 		go_intent = atoi(pos2);
6437 		if (go_intent < 0 || go_intent > 15)
6438 			return -1;
6439 	}
6440 
6441 	pos2 = os_strstr(pos, " freq=");
6442 	if (pos2) {
6443 		pos2 += 6;
6444 		freq = atoi(pos2);
6445 		if (freq <= 0)
6446 			return -1;
6447 	}
6448 
6449 	pos2 = os_strstr(pos, " freq2=");
6450 	if (pos2)
6451 		freq2 = atoi(pos2 + 7);
6452 
6453 	pos2 = os_strstr(pos, " max_oper_chwidth=");
6454 	if (pos2)
6455 		chwidth = atoi(pos2 + 18);
6456 
6457 	max_oper_chwidth = chwidth_freq2_to_ch_width(chwidth, freq2);
6458 	if (max_oper_chwidth < 0)
6459 		return -1;
6460 
6461 	if (allow_6ghz && chwidth == 40)
6462 		max_oper_chwidth = CONF_OPER_CHWIDTH_40MHZ_6GHZ;
6463 
6464 	pos2 = os_strstr(pos, " ssid=");
6465 	if (pos2) {
6466 		char *end;
6467 
6468 		pos2 += 6;
6469 		end = os_strchr(pos2, ' ');
6470 		if (!end)
6471 			group_ssid_len = os_strlen(pos2) / 2;
6472 		else
6473 			group_ssid_len = (end - pos2) / 2;
6474 		if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
6475 		    hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
6476 			return -1;
6477 		group_ssid = _group_ssid;
6478 	}
6479 
6480 	if (os_strncmp(pos, "pin", 3) == 0) {
6481 		/* Request random PIN (to be displayed) and enable the PIN */
6482 		wps_method = WPS_PIN_DISPLAY;
6483 	} else if (os_strncmp(pos, "pbc", 3) == 0) {
6484 		wps_method = WPS_PBC;
6485 	} else if (os_strstr(pos, "p2ps") != NULL) {
6486 		wps_method = WPS_P2PS;
6487 	} else if (os_strncmp(pos, "pair", 4) == 0 && p2p2) {
6488 		wps_method = WPS_NOT_READY;
6489 	} else {
6490 		pin = pos;
6491 		pos = os_strchr(pin, ' ');
6492 		wps_method = WPS_PIN_KEYPAD;
6493 		if (pos) {
6494 			*pos++ = '\0';
6495 			if (os_strncmp(pos, "display", 7) == 0)
6496 				wps_method = WPS_PIN_DISPLAY;
6497 		}
6498 		if (!wps_pin_str_valid(pin)) {
6499 			os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
6500 			return 17;
6501 		}
6502 	}
6503 
6504 	pos2 = os_strstr(pos, "bstrapmethod=");
6505 	if (pos2) {
6506 		pos2 += 13;
6507 		bootstrap = atoi(pos2);
6508 		pd = true;
6509 	}
6510 
6511 	while ((token = str_token(pos, " ", &context))) {
6512 		if (os_strncmp(token, "password=", 9) == 0) {
6513 			password = token + 9;
6514 			continue;
6515 		}
6516 	}
6517 
6518 	new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
6519 				   persistent_group, automatic, join,
6520 				   auth, go_intent, freq, freq2, persistent_id,
6521 				   pd, ht40, vht, max_oper_chwidth, he, edmg,
6522 				   group_ssid, group_ssid_len, allow_6ghz, p2p2,
6523 				   bootstrap, password);
6524 	if (new_pin == -2) {
6525 		os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
6526 		return 25;
6527 	}
6528 	if (new_pin == -3) {
6529 		os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
6530 		return 25;
6531 	}
6532 	if (new_pin < 0)
6533 		return -1;
6534 	if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
6535 		ret = os_snprintf(buf, buflen, "%08d", new_pin);
6536 		if (os_snprintf_error(buflen, ret))
6537 			return -1;
6538 		return ret;
6539 	}
6540 
6541 	os_memcpy(buf, "OK\n", 3);
6542 	return 3;
6543 }
6544 
6545 
p2p_ctrl_listen(struct wpa_supplicant * wpa_s,char * cmd)6546 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
6547 {
6548 	unsigned int timeout = atoi(cmd);
6549 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6550 		wpa_dbg(wpa_s, MSG_INFO,
6551 			"Reject P2P_LISTEN since interface is disabled");
6552 		return -1;
6553 	}
6554 	return wpas_p2p_listen(wpa_s, timeout);
6555 }
6556 
6557 
p2p_ctrl_prov_disc(struct wpa_supplicant * wpa_s,char * cmd)6558 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
6559 {
6560 	u8 addr[ETH_ALEN];
6561 	char *pos;
6562 	enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
6563 
6564 	/* <addr> <config method> [join|auto] */
6565 
6566 	if (hwaddr_aton(cmd, addr))
6567 		return -1;
6568 
6569 	pos = cmd + 17;
6570 	if (*pos != ' ')
6571 		return -1;
6572 	pos++;
6573 
6574 	if (os_strstr(pos, " join") != NULL)
6575 		use = WPAS_P2P_PD_FOR_JOIN;
6576 	else if (os_strstr(pos, " auto") != NULL)
6577 		use = WPAS_P2P_PD_AUTO;
6578 
6579 	return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
6580 }
6581 
6582 
p2p_get_passphrase(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)6583 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
6584 			      size_t buflen)
6585 {
6586 	struct wpa_ssid *ssid = wpa_s->current_ssid;
6587 
6588 	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
6589 	    ssid->passphrase == NULL)
6590 		return -1;
6591 
6592 	os_strlcpy(buf, ssid->passphrase, buflen);
6593 	return os_strlen(buf);
6594 }
6595 
6596 
p2p_ctrl_serv_disc_req(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)6597 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
6598 				  char *buf, size_t buflen)
6599 {
6600 	u64 ref;
6601 	int res;
6602 	u8 dst_buf[ETH_ALEN], *dst;
6603 	struct wpabuf *tlvs;
6604 	char *pos;
6605 	size_t len;
6606 
6607 	if (hwaddr_aton(cmd, dst_buf))
6608 		return -1;
6609 	dst = dst_buf;
6610 	if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
6611 	    dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
6612 		dst = NULL;
6613 	pos = cmd + 17;
6614 	if (*pos != ' ')
6615 		return -1;
6616 	pos++;
6617 
6618 	if (os_strncmp(pos, "upnp ", 5) == 0) {
6619 		u8 version;
6620 		pos += 5;
6621 		if (hexstr2bin(pos, &version, 1) < 0)
6622 			return -1;
6623 		pos += 2;
6624 		if (*pos != ' ')
6625 			return -1;
6626 		pos++;
6627 		ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
6628 #ifdef CONFIG_WIFI_DISPLAY
6629 	} else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
6630 		ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
6631 #endif /* CONFIG_WIFI_DISPLAY */
6632 	} else if (os_strncmp(pos, "asp ", 4) == 0) {
6633 		char *svc_str;
6634 		char *svc_info = NULL;
6635 		u32 id;
6636 
6637 		pos += 4;
6638 		if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
6639 			return -1;
6640 
6641 		pos = os_strchr(pos, ' ');
6642 		if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
6643 			return -1;
6644 
6645 		svc_str = pos + 1;
6646 
6647 		pos = os_strchr(svc_str, ' ');
6648 
6649 		if (pos)
6650 			*pos++ = '\0';
6651 
6652 		/* All remaining data is the svc_info string */
6653 		if (pos && pos[0] && pos[0] != ' ') {
6654 			len = os_strlen(pos);
6655 
6656 			/* Unescape in place */
6657 			len = utf8_unescape(pos, len, pos, len);
6658 			if (len > 0xff)
6659 				return -1;
6660 
6661 			svc_info = pos;
6662 		}
6663 
6664 		ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
6665 					      svc_str, svc_info);
6666 	} else {
6667 		len = os_strlen(pos);
6668 		if (len & 1)
6669 			return -1;
6670 		len /= 2;
6671 		tlvs = wpabuf_alloc(len);
6672 		if (tlvs == NULL)
6673 			return -1;
6674 		if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
6675 			wpabuf_free(tlvs);
6676 			return -1;
6677 		}
6678 
6679 		ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
6680 		wpabuf_free(tlvs);
6681 	}
6682 	if (ref == 0)
6683 		return -1;
6684 	res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
6685 	if (os_snprintf_error(buflen, res))
6686 		return -1;
6687 	return res;
6688 }
6689 
6690 
p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant * wpa_s,char * cmd)6691 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
6692 					 char *cmd)
6693 {
6694 	long long unsigned val;
6695 	u64 req;
6696 	if (sscanf(cmd, "%llx", &val) != 1)
6697 		return -1;
6698 	req = val;
6699 	return wpas_p2p_sd_cancel_request(wpa_s, req);
6700 }
6701 
6702 
p2p_ctrl_serv_disc_resp(struct wpa_supplicant * wpa_s,char * cmd)6703 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
6704 {
6705 	int freq;
6706 	u8 dst[ETH_ALEN];
6707 	u8 dialog_token;
6708 	struct wpabuf *resp_tlvs;
6709 	char *pos, *pos2;
6710 	size_t len;
6711 
6712 	pos = os_strchr(cmd, ' ');
6713 	if (pos == NULL)
6714 		return -1;
6715 	*pos++ = '\0';
6716 	freq = atoi(cmd);
6717 	if (freq == 0)
6718 		return -1;
6719 
6720 	if (hwaddr_aton(pos, dst))
6721 		return -1;
6722 	pos += 17;
6723 	if (*pos != ' ')
6724 		return -1;
6725 	pos++;
6726 
6727 	pos2 = os_strchr(pos, ' ');
6728 	if (pos2 == NULL)
6729 		return -1;
6730 	*pos2++ = '\0';
6731 	dialog_token = atoi(pos);
6732 
6733 	len = os_strlen(pos2);
6734 	if (len & 1)
6735 		return -1;
6736 	len /= 2;
6737 	resp_tlvs = wpabuf_alloc(len);
6738 	if (resp_tlvs == NULL)
6739 		return -1;
6740 	if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
6741 		wpabuf_free(resp_tlvs);
6742 		return -1;
6743 	}
6744 
6745 	wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
6746 	wpabuf_free(resp_tlvs);
6747 	return 0;
6748 }
6749 
6750 
p2p_ctrl_serv_disc_external(struct wpa_supplicant * wpa_s,char * cmd)6751 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
6752 				       char *cmd)
6753 {
6754 	if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
6755 		return -1;
6756 	wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
6757 	return 0;
6758 }
6759 
6760 
p2p_ctrl_service_add_bonjour(struct wpa_supplicant * wpa_s,char * cmd)6761 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
6762 					char *cmd)
6763 {
6764 	char *pos;
6765 	size_t len;
6766 	struct wpabuf *query, *resp;
6767 	int ret;
6768 
6769 	pos = os_strchr(cmd, ' ');
6770 	if (pos == NULL)
6771 		return -1;
6772 	*pos++ = '\0';
6773 
6774 	len = os_strlen(cmd);
6775 	if (len & 1)
6776 		return -1;
6777 	len /= 2;
6778 	query = wpabuf_alloc(len);
6779 	if (query == NULL)
6780 		return -1;
6781 	ret = hexstr2bin(cmd, wpabuf_put(query, len), len);
6782 	if (ret < 0)
6783 		goto err_query;
6784 	ret = -1;
6785 	len = os_strlen(pos);
6786 	if (len & 1)
6787 		goto err_query;
6788 	len /= 2;
6789 	resp = wpabuf_alloc(len);
6790 	if (!resp)
6791 		goto err_query;
6792 	ret = hexstr2bin(pos, wpabuf_put(resp, len), len);
6793 	if (ret < 0)
6794 		goto err_resp;
6795 
6796 	ret = wpas_p2p_service_add_bonjour(wpa_s, query, resp);
6797 
6798 err_resp:
6799 	wpabuf_free(resp);
6800 err_query:
6801 	wpabuf_free(query);
6802 	return ret;
6803 }
6804 
6805 
p2p_ctrl_service_add_upnp(struct wpa_supplicant * wpa_s,char * cmd)6806 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
6807 {
6808 	char *pos;
6809 	u8 version;
6810 
6811 	pos = os_strchr(cmd, ' ');
6812 	if (pos == NULL)
6813 		return -1;
6814 	*pos++ = '\0';
6815 
6816 	if (hexstr2bin(cmd, &version, 1) < 0)
6817 		return -1;
6818 
6819 	return wpas_p2p_service_add_upnp(wpa_s, version, pos);
6820 }
6821 
6822 
p2p_ctrl_service_add_asp(struct wpa_supplicant * wpa_s,u8 replace,char * cmd)6823 static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
6824 				    u8 replace, char *cmd)
6825 {
6826 	char *pos;
6827 	char *adv_str;
6828 	u32 auto_accept, adv_id, svc_state, config_methods;
6829 	char *svc_info = NULL;
6830 	char *cpt_prio_str;
6831 	u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
6832 
6833 	pos = os_strchr(cmd, ' ');
6834 	if (pos == NULL)
6835 		return -1;
6836 	*pos++ = '\0';
6837 
6838 	/* Auto-Accept value is mandatory, and must be one of the
6839 	 * single values (0, 1, 2, 4) */
6840 	auto_accept = atoi(cmd);
6841 	switch (auto_accept) {
6842 	case P2PS_SETUP_NONE: /* No auto-accept */
6843 	case P2PS_SETUP_NEW:
6844 	case P2PS_SETUP_CLIENT:
6845 	case P2PS_SETUP_GROUP_OWNER:
6846 		break;
6847 	default:
6848 		return -1;
6849 	}
6850 
6851 	/* Advertisement ID is mandatory */
6852 	cmd = pos;
6853 	pos = os_strchr(cmd, ' ');
6854 	if (pos == NULL)
6855 		return -1;
6856 	*pos++ = '\0';
6857 
6858 	/* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
6859 	if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
6860 		return -1;
6861 
6862 	/* Only allow replacements if exist, and adds if not */
6863 	if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
6864 		if (!replace)
6865 			return -1;
6866 	} else {
6867 		if (replace)
6868 			return -1;
6869 	}
6870 
6871 	/* svc_state between 0 - 0xff is mandatory */
6872 	if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
6873 		return -1;
6874 
6875 	pos = os_strchr(pos, ' ');
6876 	if (pos == NULL)
6877 		return -1;
6878 
6879 	/* config_methods is mandatory */
6880 	pos++;
6881 	if (sscanf(pos, "%x", &config_methods) != 1)
6882 		return -1;
6883 
6884 	if (!(config_methods &
6885 	      (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
6886 		return -1;
6887 
6888 	pos = os_strchr(pos, ' ');
6889 	if (pos == NULL)
6890 		return -1;
6891 
6892 	pos++;
6893 	adv_str = pos;
6894 
6895 	/* Advertisement string is mandatory */
6896 	if (!pos[0] || pos[0] == ' ')
6897 		return -1;
6898 
6899 	/* Terminate svc string */
6900 	pos = os_strchr(pos, ' ');
6901 	if (pos != NULL)
6902 		*pos++ = '\0';
6903 
6904 	cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
6905 	if (cpt_prio_str) {
6906 		pos = os_strchr(pos, ' ');
6907 		if (pos != NULL)
6908 			*pos++ = '\0';
6909 
6910 		if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
6911 			return -1;
6912 	} else {
6913 		cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
6914 		cpt_prio[1] = 0;
6915 	}
6916 
6917 	/* Service and Response Information are optional */
6918 	if (pos && pos[0]) {
6919 		size_t len;
6920 
6921 		/* Note the bare ' included, which cannot exist legally
6922 		 * in unescaped string. */
6923 		svc_info = os_strstr(pos, "svc_info='");
6924 
6925 		if (svc_info) {
6926 			svc_info += 9;
6927 			len = os_strlen(svc_info);
6928 			utf8_unescape(svc_info, len, svc_info, len);
6929 		}
6930 	}
6931 
6932 	return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
6933 					(u8) svc_state, (u16) config_methods,
6934 					svc_info, cpt_prio);
6935 }
6936 
6937 
p2p_ctrl_service_add(struct wpa_supplicant * wpa_s,char * cmd)6938 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
6939 {
6940 	char *pos;
6941 
6942 	pos = os_strchr(cmd, ' ');
6943 	if (pos == NULL)
6944 		return -1;
6945 	*pos++ = '\0';
6946 
6947 	if (os_strcmp(cmd, "bonjour") == 0)
6948 		return p2p_ctrl_service_add_bonjour(wpa_s, pos);
6949 	if (os_strcmp(cmd, "upnp") == 0)
6950 		return p2p_ctrl_service_add_upnp(wpa_s, pos);
6951 	if (os_strcmp(cmd, "asp") == 0)
6952 		return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
6953 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6954 	return -1;
6955 }
6956 
6957 
p2p_ctrl_service_del_bonjour(struct wpa_supplicant * wpa_s,char * cmd)6958 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
6959 					char *cmd)
6960 {
6961 	size_t len;
6962 	struct wpabuf *query;
6963 	int ret;
6964 
6965 	len = os_strlen(cmd);
6966 	if (len & 1)
6967 		return -1;
6968 	len /= 2;
6969 	query = wpabuf_alloc(len);
6970 	if (query == NULL)
6971 		return -1;
6972 	if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
6973 		wpabuf_free(query);
6974 		return -1;
6975 	}
6976 
6977 	ret = wpas_p2p_service_del_bonjour(wpa_s, query);
6978 	wpabuf_free(query);
6979 	return ret;
6980 }
6981 
6982 
p2p_ctrl_service_del_upnp(struct wpa_supplicant * wpa_s,char * cmd)6983 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
6984 {
6985 	char *pos;
6986 	u8 version;
6987 
6988 	pos = os_strchr(cmd, ' ');
6989 	if (pos == NULL)
6990 		return -1;
6991 	*pos++ = '\0';
6992 
6993 	if (hexstr2bin(cmd, &version, 1) < 0)
6994 		return -1;
6995 
6996 	return wpas_p2p_service_del_upnp(wpa_s, version, pos);
6997 }
6998 
6999 
p2p_ctrl_service_del_asp(struct wpa_supplicant * wpa_s,char * cmd)7000 static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
7001 {
7002 	u32 adv_id;
7003 
7004 	if (os_strcmp(cmd, "all") == 0) {
7005 		wpas_p2p_service_flush_asp(wpa_s);
7006 		return 0;
7007 	}
7008 
7009 	if (sscanf(cmd, "%x", &adv_id) != 1)
7010 		return -1;
7011 
7012 	return wpas_p2p_service_del_asp(wpa_s, adv_id);
7013 }
7014 
7015 
p2p_ctrl_service_del(struct wpa_supplicant * wpa_s,char * cmd)7016 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
7017 {
7018 	char *pos;
7019 
7020 	pos = os_strchr(cmd, ' ');
7021 	if (pos == NULL)
7022 		return -1;
7023 	*pos++ = '\0';
7024 
7025 	if (os_strcmp(cmd, "bonjour") == 0)
7026 		return p2p_ctrl_service_del_bonjour(wpa_s, pos);
7027 	if (os_strcmp(cmd, "upnp") == 0)
7028 		return p2p_ctrl_service_del_upnp(wpa_s, pos);
7029 	if (os_strcmp(cmd, "asp") == 0)
7030 		return p2p_ctrl_service_del_asp(wpa_s, pos);
7031 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
7032 	return -1;
7033 }
7034 
7035 
p2p_ctrl_service_replace(struct wpa_supplicant * wpa_s,char * cmd)7036 static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
7037 {
7038 	char *pos;
7039 
7040 	pos = os_strchr(cmd, ' ');
7041 	if (pos == NULL)
7042 		return -1;
7043 	*pos++ = '\0';
7044 
7045 	if (os_strcmp(cmd, "asp") == 0)
7046 		return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
7047 
7048 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
7049 	return -1;
7050 }
7051 
7052 
p2p_ctrl_reject(struct wpa_supplicant * wpa_s,char * cmd)7053 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
7054 {
7055 	u8 addr[ETH_ALEN];
7056 
7057 	/* <addr> */
7058 
7059 	if (hwaddr_aton(cmd, addr))
7060 		return -1;
7061 
7062 	return wpas_p2p_reject(wpa_s, addr);
7063 }
7064 
7065 
p2p_ctrl_invite_persistent(struct wpa_supplicant * wpa_s,char * cmd)7066 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
7067 {
7068 	char *pos;
7069 	int id;
7070 	struct wpa_ssid *ssid;
7071 	u8 *_peer = NULL, peer[ETH_ALEN];
7072 	int freq = 0, pref_freq = 0;
7073 	int ht40, vht, he, max_oper_chwidth, chwidth = 0, freq2 = 0;
7074 	int edmg;
7075 	bool allow_6ghz;
7076 
7077 	id = atoi(cmd);
7078 	pos = os_strstr(cmd, " peer=");
7079 	if (pos) {
7080 		pos += 6;
7081 		if (hwaddr_aton(pos, peer))
7082 			return -1;
7083 		_peer = peer;
7084 	}
7085 	ssid = wpa_config_get_network(wpa_s->conf, id);
7086 	if (ssid == NULL || ssid->disabled != 2) {
7087 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
7088 			   "for persistent P2P group",
7089 			   id);
7090 		return -1;
7091 	}
7092 
7093 	pos = os_strstr(cmd, " freq=");
7094 	if (pos) {
7095 		pos += 6;
7096 		freq = atoi(pos);
7097 		if (freq <= 0)
7098 			return -1;
7099 	}
7100 
7101 	pos = os_strstr(cmd, " pref=");
7102 	if (pos) {
7103 		pos += 6;
7104 		pref_freq = atoi(pos);
7105 		if (pref_freq <= 0)
7106 			return -1;
7107 	}
7108 
7109 	vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
7110 	ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
7111 		vht;
7112 	he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
7113 	edmg = (os_strstr(cmd, " edmg") != NULL) || wpa_s->conf->p2p_go_edmg;
7114 
7115 	pos = os_strstr(cmd, "freq2=");
7116 	if (pos)
7117 		freq2 = atoi(pos + 6);
7118 
7119 	pos = os_strstr(cmd, " max_oper_chwidth=");
7120 	if (pos)
7121 		chwidth = atoi(pos + 18);
7122 
7123 	max_oper_chwidth = chwidth_freq2_to_ch_width(chwidth, freq2);
7124 	if (max_oper_chwidth < 0)
7125 		return -1;
7126 
7127 	allow_6ghz = os_strstr(cmd, " allow_6ghz") != NULL;
7128 
7129 	if (allow_6ghz && chwidth == 40)
7130 		max_oper_chwidth = CONF_OPER_CHWIDTH_40MHZ_6GHZ;
7131 
7132 	return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
7133 			       max_oper_chwidth, pref_freq, he, edmg,
7134 			       allow_6ghz);
7135 }
7136 
7137 
p2p_ctrl_invite_group(struct wpa_supplicant * wpa_s,char * cmd)7138 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
7139 {
7140 	char *pos;
7141 	u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
7142 	bool allow_6ghz;
7143 
7144 	pos = os_strstr(cmd, " peer=");
7145 	if (!pos)
7146 		return -1;
7147 
7148 	*pos = '\0';
7149 	pos += 6;
7150 	if (hwaddr_aton(pos, peer)) {
7151 		wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
7152 		return -1;
7153 	}
7154 
7155 	allow_6ghz = os_strstr(pos, " allow_6ghz") != NULL;
7156 
7157 	pos = os_strstr(pos, " go_dev_addr=");
7158 	if (pos) {
7159 		pos += 13;
7160 		if (hwaddr_aton(pos, go_dev_addr)) {
7161 			wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
7162 				   pos);
7163 			return -1;
7164 		}
7165 		go_dev = go_dev_addr;
7166 	}
7167 
7168 	return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev, allow_6ghz);
7169 }
7170 
7171 
p2p_ctrl_invite(struct wpa_supplicant * wpa_s,char * cmd)7172 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
7173 {
7174 	if (os_strncmp(cmd, "persistent=", 11) == 0)
7175 		return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
7176 	if (os_strncmp(cmd, "group=", 6) == 0)
7177 		return p2p_ctrl_invite_group(wpa_s, cmd + 6);
7178 
7179 	return -1;
7180 }
7181 
7182 
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)7183 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
7184 					 int id, int freq, int vht_center_freq2,
7185 					 int ht40, int vht, int vht_chwidth,
7186 					 int he, int edmg, bool allow_6ghz,
7187 					 const u8 *go_bssid)
7188 {
7189 	struct wpa_ssid *ssid;
7190 
7191 	ssid = wpa_config_get_network(wpa_s->conf, id);
7192 	if (ssid == NULL || ssid->disabled != 2) {
7193 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
7194 			   "for persistent P2P group",
7195 			   id);
7196 		return -1;
7197 	}
7198 
7199 	return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, freq,
7200 					     vht_center_freq2, ht40, vht,
7201 					     vht_chwidth, he, edmg,
7202 					     NULL, 0, 0, allow_6ghz, 0,
7203 					     go_bssid);
7204 }
7205 
7206 
p2p_ctrl_group_add(struct wpa_supplicant * wpa_s,char * cmd)7207 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
7208 {
7209 	int freq = 0, persistent = 0, group_id = -1;
7210 	bool allow_6ghz = false;
7211 	int vht = wpa_s->conf->p2p_go_vht;
7212 	int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
7213 	int he = wpa_s->conf->p2p_go_he;
7214 	int edmg = wpa_s->conf->p2p_go_edmg;
7215 	int max_oper_chwidth, chwidth = 0, freq2 = 0;
7216 	char *token, *context = NULL;
7217 	u8 go_bssid_buf[ETH_ALEN], *go_bssid = NULL;
7218 #ifdef CONFIG_ACS
7219 	int acs = 0;
7220 #endif /* CONFIG_ACS */
7221 
7222 	while ((token = str_token(cmd, " ", &context))) {
7223 		if (sscanf(token, "freq2=%d", &freq2) == 1 ||
7224 		    sscanf(token, "persistent=%d", &group_id) == 1 ||
7225 		    sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1) {
7226 			continue;
7227 #ifdef CONFIG_ACS
7228 		} else if (os_strcmp(token, "freq=acs") == 0) {
7229 			acs = 1;
7230 #endif /* CONFIG_ACS */
7231 		} else if (sscanf(token, "freq=%d", &freq) == 1) {
7232 			continue;
7233 		} else if (os_strcmp(token, "ht40") == 0) {
7234 			ht40 = 1;
7235 		} else if (os_strcmp(token, "vht") == 0) {
7236 			vht = 1;
7237 			ht40 = 1;
7238 		} else if (os_strcmp(token, "he") == 0) {
7239 			he = 1;
7240 		} else if (os_strcmp(token, "edmg") == 0) {
7241 			edmg = 1;
7242 		} else if (os_strcmp(token, "persistent") == 0) {
7243 			persistent = 1;
7244 		} else if (os_strcmp(token, "allow_6ghz") == 0) {
7245 			allow_6ghz = true;
7246 		} else if (os_strncmp(token, "go_bssid=", 9) == 0) {
7247 			if (hwaddr_aton(token + 9, go_bssid_buf))
7248 				return -1;
7249 			go_bssid = go_bssid_buf;
7250 		} else {
7251 			wpa_printf(MSG_DEBUG,
7252 				   "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
7253 				   token);
7254 			return -1;
7255 		}
7256 	}
7257 
7258 #ifdef CONFIG_ACS
7259 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
7260 	    (acs || freq == 2 || freq == 5)) {
7261 		if (freq == 2 && wpa_s->best_24_freq <= 0) {
7262 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211G;
7263 			wpa_s->p2p_go_do_acs = 1;
7264 			freq = 0;
7265 		} else if (freq == 5 && wpa_s->best_5_freq <= 0) {
7266 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211A;
7267 			wpa_s->p2p_go_do_acs = 1;
7268 			freq = 0;
7269 		} else {
7270 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211ANY;
7271 			wpa_s->p2p_go_do_acs = 1;
7272 		}
7273 	} else {
7274 		wpa_s->p2p_go_do_acs = 0;
7275 	}
7276 #endif /* CONFIG_ACS */
7277 
7278 	max_oper_chwidth = chwidth_freq2_to_ch_width(chwidth, freq2);
7279 	if (max_oper_chwidth < 0)
7280 		return -1;
7281 
7282 	if (allow_6ghz && chwidth == 40)
7283 		max_oper_chwidth = CONF_OPER_CHWIDTH_40MHZ_6GHZ;
7284 
7285 	/* Allow DFS to be used for Autonomous GO */
7286 	wpa_s->p2p_go_allow_dfs = !!(wpa_s->drv_flags &
7287 				     WPA_DRIVER_FLAGS_DFS_OFFLOAD);
7288 
7289 	if (group_id >= 0)
7290 		return p2p_ctrl_group_add_persistent(wpa_s, group_id,
7291 						     freq, freq2, ht40, vht,
7292 						     max_oper_chwidth, he,
7293 						     edmg, allow_6ghz,
7294 						     go_bssid);
7295 
7296 	return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
7297 				  max_oper_chwidth, he, edmg, allow_6ghz);
7298 }
7299 
7300 
p2p_ctrl_group_member(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)7301 static int p2p_ctrl_group_member(struct wpa_supplicant *wpa_s, const char *cmd,
7302 				 char *buf, size_t buflen)
7303 {
7304 	u8 dev_addr[ETH_ALEN];
7305 	struct wpa_ssid *ssid;
7306 	int res;
7307 	const u8 *iaddr;
7308 
7309 	ssid = wpa_s->current_ssid;
7310 	if (!wpa_s->global->p2p || !ssid || ssid->mode != WPAS_MODE_P2P_GO ||
7311 	    hwaddr_aton(cmd, dev_addr))
7312 		return -1;
7313 
7314 	iaddr = p2p_group_get_client_interface_addr(wpa_s->p2p_group, dev_addr);
7315 	if (!iaddr)
7316 		return -1;
7317 	res = os_snprintf(buf, buflen, MACSTR, MAC2STR(iaddr));
7318 	if (os_snprintf_error(buflen, res))
7319 		return -1;
7320 	return res;
7321 }
7322 
7323 
wpas_find_p2p_dev_addr_bss(struct wpa_global * global,const u8 * p2p_dev_addr)7324 static int wpas_find_p2p_dev_addr_bss(struct wpa_global *global,
7325 				      const u8 *p2p_dev_addr)
7326 {
7327 	struct wpa_supplicant *wpa_s;
7328 
7329 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7330 		if (wpa_bss_get_p2p_dev_addr(wpa_s, p2p_dev_addr))
7331 			return 1;
7332 	}
7333 
7334 	return 0;
7335 }
7336 
7337 
p2p_ctrl_peer(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)7338 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
7339 			 char *buf, size_t buflen)
7340 {
7341 	u8 addr[ETH_ALEN], *addr_ptr, group_capab;
7342 	int next, res;
7343 	const struct p2p_peer_info *info;
7344 	char *pos, *end;
7345 	char devtype[WPS_DEV_TYPE_BUFSIZE];
7346 	struct wpa_ssid *ssid;
7347 	size_t i;
7348 
7349 	if (!wpa_s->global->p2p)
7350 		return -1;
7351 
7352 	if (os_strcmp(cmd, "FIRST") == 0) {
7353 		addr_ptr = NULL;
7354 		next = 0;
7355 	} else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
7356 		if (hwaddr_aton(cmd + 5, addr) < 0)
7357 			return -1;
7358 		addr_ptr = addr;
7359 		next = 1;
7360 	} else {
7361 		if (hwaddr_aton(cmd, addr) < 0)
7362 			return -1;
7363 		addr_ptr = addr;
7364 		next = 0;
7365 	}
7366 
7367 	info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
7368 	if (info == NULL)
7369 		return -1;
7370 	group_capab = info->group_capab;
7371 
7372 	if (group_capab &&
7373 	    !wpas_find_p2p_dev_addr_bss(wpa_s->global, info->p2p_device_addr)) {
7374 		wpa_printf(MSG_DEBUG,
7375 			   "P2P: Could not find any BSS with p2p_dev_addr "
7376 			   MACSTR ", hence override group_capab from 0x%x to 0",
7377 			   MAC2STR(info->p2p_device_addr), group_capab);
7378 		group_capab = 0;
7379 	}
7380 
7381 	pos = buf;
7382 	end = buf + buflen;
7383 
7384 	res = os_snprintf(pos, end - pos, MACSTR "\n"
7385 			  "pri_dev_type=%s\n"
7386 			  "device_name=%s\n"
7387 			  "manufacturer=%s\n"
7388 			  "model_name=%s\n"
7389 			  "model_number=%s\n"
7390 			  "serial_number=%s\n"
7391 			  "config_methods=0x%x\n"
7392 			  "dev_capab=0x%x\n"
7393 			  "group_capab=0x%x\n"
7394 			  "level=%d\n",
7395 			  MAC2STR(info->p2p_device_addr),
7396 			  wps_dev_type_bin2str(info->pri_dev_type,
7397 					       devtype, sizeof(devtype)),
7398 			  info->device_name,
7399 			  info->manufacturer,
7400 			  info->model_name,
7401 			  info->model_number,
7402 			  info->serial_number,
7403 			  info->config_methods,
7404 			  info->dev_capab,
7405 			  group_capab,
7406 			  info->level);
7407 	if (os_snprintf_error(end - pos, res))
7408 		return pos - buf;
7409 	pos += res;
7410 
7411 	for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
7412 	{
7413 		const u8 *t;
7414 		t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
7415 		res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
7416 				  wps_dev_type_bin2str(t, devtype,
7417 						       sizeof(devtype)));
7418 		if (os_snprintf_error(end - pos, res))
7419 			return pos - buf;
7420 		pos += res;
7421 	}
7422 
7423 	ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
7424 	if (ssid) {
7425 		res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
7426 		if (os_snprintf_error(end - pos, res))
7427 			return pos - buf;
7428 		pos += res;
7429 	}
7430 
7431 	res = p2p_get_peer_info_txt(info, pos, end - pos);
7432 	if (res < 0)
7433 		return pos - buf;
7434 	pos += res;
7435 
7436 	if (info->vendor_elems) {
7437 		res = os_snprintf(pos, end - pos, "vendor_elems=");
7438 		if (os_snprintf_error(end - pos, res))
7439 			return pos - buf;
7440 		pos += res;
7441 
7442 		pos += wpa_snprintf_hex(pos, end - pos,
7443 					wpabuf_head(info->vendor_elems),
7444 					wpabuf_len(info->vendor_elems));
7445 
7446 		res = os_snprintf(pos, end - pos, "\n");
7447 		if (os_snprintf_error(end - pos, res))
7448 			return pos - buf;
7449 		pos += res;
7450 	}
7451 
7452 	return pos - buf;
7453 }
7454 
7455 
p2p_ctrl_disallow_freq(struct wpa_supplicant * wpa_s,const char * param)7456 static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
7457 				  const char *param)
7458 {
7459 	unsigned int i;
7460 
7461 	if (wpa_s->global->p2p == NULL)
7462 		return -1;
7463 
7464 	if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
7465 		return -1;
7466 
7467 	for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
7468 		struct wpa_freq_range *freq;
7469 		freq = &wpa_s->global->p2p_disallow_freq.range[i];
7470 		wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
7471 			   freq->min, freq->max);
7472 	}
7473 
7474 	wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
7475 	return 0;
7476 }
7477 
7478 
p2p_ctrl_set(struct wpa_supplicant * wpa_s,char * cmd)7479 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
7480 {
7481 	char *param;
7482 
7483 	if (wpa_s->global->p2p == NULL)
7484 		return -1;
7485 
7486 	param = os_strchr(cmd, ' ');
7487 	if (param == NULL)
7488 		return -1;
7489 	*param++ = '\0';
7490 
7491 	if (os_strcmp(cmd, "discoverability") == 0) {
7492 		p2p_set_client_discoverability(wpa_s->global->p2p,
7493 					       atoi(param));
7494 		return 0;
7495 	}
7496 
7497 	if (os_strcmp(cmd, "managed") == 0) {
7498 		p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
7499 		return 0;
7500 	}
7501 
7502 	if (os_strcmp(cmd, "listen_channel") == 0) {
7503 		char *pos;
7504 		u8 channel, op_class;
7505 
7506 		channel = atoi(param);
7507 		pos = os_strchr(param, ' ');
7508 		op_class = pos ? atoi(pos) : 81;
7509 
7510 		return p2p_set_listen_channel(wpa_s->global->p2p, op_class,
7511 					      channel, 1);
7512 	}
7513 
7514 	if (os_strcmp(cmd, "ssid_postfix") == 0) {
7515 		return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
7516 					    os_strlen(param));
7517 	}
7518 
7519 	if (os_strcmp(cmd, "noa") == 0) {
7520 		char *pos;
7521 		int count, start, duration;
7522 		/* GO NoA parameters: count,start_offset(ms),duration(ms) */
7523 		count = atoi(param);
7524 		pos = os_strchr(param, ',');
7525 		if (pos == NULL)
7526 			return -1;
7527 		pos++;
7528 		start = atoi(pos);
7529 		pos = os_strchr(pos, ',');
7530 		if (pos == NULL)
7531 			return -1;
7532 		pos++;
7533 		duration = atoi(pos);
7534 		if (count < 0 || count > 255 || start < 0 || duration < 0)
7535 			return -1;
7536 		if (count == 0 && duration > 0)
7537 			return -1;
7538 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
7539 			   "start=%d duration=%d", count, start, duration);
7540 		return wpas_p2p_set_noa(wpa_s, count, start, duration);
7541 	}
7542 
7543 	if (os_strcmp(cmd, "ps") == 0)
7544 		return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
7545 
7546 	if (os_strcmp(cmd, "oppps") == 0)
7547 		return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
7548 
7549 	if (os_strcmp(cmd, "ctwindow") == 0)
7550 		return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
7551 
7552 	if (os_strcmp(cmd, "disabled") == 0) {
7553 		wpa_s->global->p2p_disabled = atoi(param);
7554 		wpa_printf(MSG_DEBUG, "P2P functionality %s",
7555 			   wpa_s->global->p2p_disabled ?
7556 			   "disabled" : "enabled");
7557 		if (wpa_s->global->p2p_disabled) {
7558 			wpas_p2p_stop_find(wpa_s);
7559 			os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7560 			p2p_flush(wpa_s->global->p2p);
7561 		}
7562 		return 0;
7563 	}
7564 
7565 	if (os_strcmp(cmd, "conc_pref") == 0) {
7566 		if (os_strcmp(param, "sta") == 0)
7567 			wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
7568 		else if (os_strcmp(param, "p2p") == 0)
7569 			wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
7570 		else {
7571 			wpa_printf(MSG_INFO, "Invalid conc_pref value");
7572 			return -1;
7573 		}
7574 		wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
7575 			   "%s", param);
7576 		return 0;
7577 	}
7578 
7579 	if (os_strcmp(cmd, "force_long_sd") == 0) {
7580 		wpa_s->force_long_sd = atoi(param);
7581 		return 0;
7582 	}
7583 
7584 	if (os_strcmp(cmd, "peer_filter") == 0) {
7585 		u8 addr[ETH_ALEN];
7586 		if (hwaddr_aton(param, addr))
7587 			return -1;
7588 		p2p_set_peer_filter(wpa_s->global->p2p, addr);
7589 		return 0;
7590 	}
7591 
7592 	if (os_strcmp(cmd, "cross_connect") == 0)
7593 		return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
7594 
7595 	if (os_strcmp(cmd, "go_apsd") == 0) {
7596 		if (os_strcmp(param, "disable") == 0)
7597 			wpa_s->set_ap_uapsd = 0;
7598 		else {
7599 			wpa_s->set_ap_uapsd = 1;
7600 			wpa_s->ap_uapsd = atoi(param);
7601 		}
7602 		return 0;
7603 	}
7604 
7605 	if (os_strcmp(cmd, "client_apsd") == 0) {
7606 		if (os_strcmp(param, "disable") == 0)
7607 			wpa_s->set_sta_uapsd = 0;
7608 		else {
7609 			int be, bk, vi, vo;
7610 			char *pos;
7611 			/* format: BE,BK,VI,VO;max SP Length */
7612 			be = atoi(param);
7613 			pos = os_strchr(param, ',');
7614 			if (pos == NULL)
7615 				return -1;
7616 			pos++;
7617 			bk = atoi(pos);
7618 			pos = os_strchr(pos, ',');
7619 			if (pos == NULL)
7620 				return -1;
7621 			pos++;
7622 			vi = atoi(pos);
7623 			pos = os_strchr(pos, ',');
7624 			if (pos == NULL)
7625 				return -1;
7626 			pos++;
7627 			vo = atoi(pos);
7628 			/* ignore max SP Length for now */
7629 
7630 			wpa_s->set_sta_uapsd = 1;
7631 			wpa_s->sta_uapsd = 0;
7632 			if (be)
7633 				wpa_s->sta_uapsd |= BIT(0);
7634 			if (bk)
7635 				wpa_s->sta_uapsd |= BIT(1);
7636 			if (vi)
7637 				wpa_s->sta_uapsd |= BIT(2);
7638 			if (vo)
7639 				wpa_s->sta_uapsd |= BIT(3);
7640 		}
7641 		return 0;
7642 	}
7643 
7644 	if (os_strcmp(cmd, "disallow_freq") == 0)
7645 		return p2p_ctrl_disallow_freq(wpa_s, param);
7646 
7647 	if (os_strcmp(cmd, "disc_int") == 0) {
7648 		int min_disc_int, max_disc_int, max_disc_tu;
7649 		char *pos;
7650 
7651 		pos = param;
7652 
7653 		min_disc_int = atoi(pos);
7654 		pos = os_strchr(pos, ' ');
7655 		if (pos == NULL)
7656 			return -1;
7657 		*pos++ = '\0';
7658 
7659 		max_disc_int = atoi(pos);
7660 		pos = os_strchr(pos, ' ');
7661 		if (pos == NULL)
7662 			return -1;
7663 		*pos++ = '\0';
7664 
7665 		max_disc_tu = atoi(pos);
7666 
7667 		return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
7668 					max_disc_int, max_disc_tu);
7669 	}
7670 
7671 	if (os_strcmp(cmd, "per_sta_psk") == 0) {
7672 		wpa_s->global->p2p_per_sta_psk = !!atoi(param);
7673 		return 0;
7674 	}
7675 
7676 #ifdef CONFIG_WPS_NFC
7677 	if (os_strcmp(cmd, "nfc_tag") == 0)
7678 		return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
7679 #endif /* CONFIG_WPS_NFC */
7680 
7681 	if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
7682 		wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
7683 		return 0;
7684 	}
7685 
7686 	if (os_strcmp(cmd, "override_pref_op_chan") == 0) {
7687 		int op_class, chan;
7688 
7689 		op_class = atoi(param);
7690 		param = os_strchr(param, ':');
7691 		if (!param)
7692 			return -1;
7693 		param++;
7694 		chan = atoi(param);
7695 		p2p_set_override_pref_op_chan(wpa_s->global->p2p, op_class,
7696 					      chan);
7697 		return 0;
7698 	}
7699 
7700 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
7701 		   cmd);
7702 
7703 	return -1;
7704 }
7705 
7706 
p2p_ctrl_flush(struct wpa_supplicant * wpa_s)7707 static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
7708 {
7709 	os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7710 	wpa_s->force_long_sd = 0;
7711 
7712 #ifdef CONFIG_TESTING_OPTIONS
7713 	os_free(wpa_s->get_pref_freq_list_override);
7714 	wpa_s->get_pref_freq_list_override = NULL;
7715 #endif /* CONFIG_TESTING_OPTIONS */
7716 
7717 	wpas_p2p_stop_find(wpa_s);
7718 	wpa_s->parent->p2ps_method_config_any = 0;
7719 	if (wpa_s->global->p2p)
7720 		p2p_flush(wpa_s->global->p2p);
7721 }
7722 
7723 
p2p_ctrl_presence_req(struct wpa_supplicant * wpa_s,char * cmd)7724 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
7725 {
7726 	char *pos, *pos2;
7727 	unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
7728 
7729 	if (cmd[0]) {
7730 		pos = os_strchr(cmd, ' ');
7731 		if (pos == NULL)
7732 			return -1;
7733 		*pos++ = '\0';
7734 		dur1 = atoi(cmd);
7735 
7736 		pos2 = os_strchr(pos, ' ');
7737 		if (pos2)
7738 			*pos2++ = '\0';
7739 		int1 = atoi(pos);
7740 	} else
7741 		pos2 = NULL;
7742 
7743 	if (pos2) {
7744 		pos = os_strchr(pos2, ' ');
7745 		if (pos == NULL)
7746 			return -1;
7747 		*pos++ = '\0';
7748 		dur2 = atoi(pos2);
7749 		int2 = atoi(pos);
7750 	}
7751 
7752 	return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
7753 }
7754 
7755 
p2p_ctrl_ext_listen(struct wpa_supplicant * wpa_s,char * cmd)7756 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
7757 {
7758 	char *pos;
7759 	unsigned int period = 0, interval = 0;
7760 
7761 	if (cmd[0]) {
7762 		pos = os_strchr(cmd, ' ');
7763 		if (pos == NULL)
7764 			return -1;
7765 		*pos++ = '\0';
7766 		period = atoi(cmd);
7767 		interval = atoi(pos);
7768 	}
7769 
7770 	return wpas_p2p_ext_listen(wpa_s, period, interval);
7771 }
7772 
7773 
p2p_ctrl_remove_client(struct wpa_supplicant * wpa_s,const char * cmd)7774 static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
7775 {
7776 	const char *pos;
7777 	u8 peer[ETH_ALEN];
7778 	int iface_addr = 0;
7779 
7780 	pos = cmd;
7781 	if (os_strncmp(pos, "iface=", 6) == 0) {
7782 		iface_addr = 1;
7783 		pos += 6;
7784 	}
7785 	if (hwaddr_aton(pos, peer))
7786 		return -1;
7787 
7788 	wpas_p2p_remove_client(wpa_s, peer, iface_addr);
7789 	return 0;
7790 }
7791 
7792 
p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant * wpa_s,char * cmd)7793 static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
7794 {
7795 	int freq = 0, period = 0, interval = 0, count = 0;
7796 
7797 	if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
7798 	{
7799 		wpa_printf(MSG_DEBUG,
7800 			   "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
7801 		return -1;
7802 	}
7803 
7804 	return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
7805 }
7806 
7807 #endif /* CONFIG_P2P */
7808 
7809 
freq_range_to_channel_list(struct wpa_supplicant * wpa_s,char * val)7810 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
7811 {
7812 	struct wpa_freq_range_list ranges;
7813 	int *freqs = NULL;
7814 	struct hostapd_hw_modes *mode;
7815 	u16 i;
7816 
7817 	if (wpa_s->hw.modes == NULL)
7818 		return NULL;
7819 
7820 	os_memset(&ranges, 0, sizeof(ranges));
7821 	if (freq_range_list_parse(&ranges, val) < 0)
7822 		return NULL;
7823 
7824 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
7825 		int j;
7826 
7827 		mode = &wpa_s->hw.modes[i];
7828 		for (j = 0; j < mode->num_channels; j++) {
7829 			unsigned int freq;
7830 
7831 			if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
7832 				continue;
7833 
7834 			freq = mode->channels[j].freq;
7835 			if (!freq_range_list_includes(&ranges, freq))
7836 				continue;
7837 
7838 			int_array_add_unique(&freqs, freq);
7839 		}
7840 	}
7841 
7842 	os_free(ranges.range);
7843 	return freqs;
7844 }
7845 
7846 
7847 #ifdef CONFIG_INTERWORKING
7848 
ctrl_interworking_select(struct wpa_supplicant * wpa_s,char * param)7849 static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
7850 {
7851 	int auto_sel = 0;
7852 	int *freqs = NULL;
7853 
7854 	if (param) {
7855 		char *pos;
7856 
7857 		auto_sel = os_strstr(param, "auto") != NULL;
7858 
7859 		pos = os_strstr(param, "freq=");
7860 		if (pos) {
7861 			freqs = freq_range_to_channel_list(wpa_s, pos + 5);
7862 			if (freqs == NULL)
7863 				return -1;
7864 		}
7865 
7866 	}
7867 
7868 	return interworking_select(wpa_s, auto_sel, freqs);
7869 }
7870 
7871 
ctrl_interworking_connect(struct wpa_supplicant * wpa_s,char * dst,int only_add)7872 static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
7873 				     int only_add)
7874 {
7875 	u8 bssid[ETH_ALEN];
7876 	struct wpa_bss *bss;
7877 
7878 	if (hwaddr_aton(dst, bssid)) {
7879 		wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
7880 		return -1;
7881 	}
7882 
7883 	bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
7884 	if (bss == NULL) {
7885 		wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
7886 			   MAC2STR(bssid));
7887 		return -1;
7888 	}
7889 
7890 	if (bss->ssid_len == 0) {
7891 		int found = 0;
7892 
7893 		wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
7894 			   " does not have SSID information", MAC2STR(bssid));
7895 
7896 		dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
7897 					 list) {
7898 			if (ether_addr_equal(bss->bssid, bssid) &&
7899 			    bss->ssid_len > 0) {
7900 				found = 1;
7901 				break;
7902 			}
7903 		}
7904 
7905 		if (!found)
7906 			return -1;
7907 		wpa_printf(MSG_DEBUG,
7908 			   "Found another matching BSS entry with SSID");
7909 	}
7910 
7911 	return interworking_connect(wpa_s, bss, only_add);
7912 }
7913 
7914 
get_anqp(struct wpa_supplicant * wpa_s,char * dst)7915 static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
7916 {
7917 	u8 dst_addr[ETH_ALEN];
7918 	int used, freq = 0;
7919 	char *pos;
7920 #define MAX_ANQP_INFO_ID 100
7921 	u16 id[MAX_ANQP_INFO_ID];
7922 	size_t num_id = 0;
7923 	u32 subtypes = 0;
7924 	u32 mbo_subtypes = 0;
7925 
7926 	used = hwaddr_aton2(dst, dst_addr);
7927 	if (used < 0)
7928 		return -1;
7929 	pos = dst + used;
7930 	if (*pos == ' ')
7931 		pos++;
7932 
7933 	if (os_strncmp(pos, "freq=", 5) == 0) {
7934 		freq = atoi(pos + 5);
7935 		pos = os_strchr(pos, ' ');
7936 		if (!pos)
7937 			return -1;
7938 		pos++;
7939 	}
7940 
7941 	while (num_id < MAX_ANQP_INFO_ID) {
7942 		if (os_strncmp(pos, "hs20:", 5) == 0) {
7943 #ifdef CONFIG_HS20
7944 			int num = atoi(pos + 5);
7945 			if (num <= 0 || num > 31)
7946 				return -1;
7947 			subtypes |= BIT(num);
7948 #else /* CONFIG_HS20 */
7949 			return -1;
7950 #endif /* CONFIG_HS20 */
7951 		} else if (os_strncmp(pos, "mbo:", 4) == 0) {
7952 #ifdef CONFIG_MBO
7953 			int num = atoi(pos + 4);
7954 
7955 			if (num <= 0 || num > MAX_MBO_ANQP_SUBTYPE)
7956 				return -1;
7957 			mbo_subtypes |= BIT(num);
7958 #else /* CONFIG_MBO */
7959 			return -1;
7960 #endif /* CONFIG_MBO */
7961 		} else {
7962 			id[num_id] = atoi(pos);
7963 			if (id[num_id])
7964 				num_id++;
7965 		}
7966 		pos = os_strchr(pos + 1, ',');
7967 		if (pos == NULL)
7968 			break;
7969 		pos++;
7970 	}
7971 
7972 	if (num_id == 0 && !subtypes && !mbo_subtypes)
7973 		return -1;
7974 
7975 	return anqp_send_req(wpa_s, dst_addr, freq, id, num_id, subtypes,
7976 			     mbo_subtypes);
7977 }
7978 
7979 
gas_request(struct wpa_supplicant * wpa_s,char * cmd)7980 static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
7981 {
7982 	u8 dst_addr[ETH_ALEN];
7983 	struct wpabuf *advproto, *query = NULL;
7984 	int used, ret = -1;
7985 	char *pos, *end;
7986 	size_t len;
7987 
7988 	used = hwaddr_aton2(cmd, dst_addr);
7989 	if (used < 0)
7990 		return -1;
7991 
7992 	pos = cmd + used;
7993 	while (*pos == ' ')
7994 		pos++;
7995 
7996 	/* Advertisement Protocol ID */
7997 	end = os_strchr(pos, ' ');
7998 	if (end)
7999 		len = end - pos;
8000 	else
8001 		len = os_strlen(pos);
8002 	if (len & 0x01)
8003 		return -1;
8004 	len /= 2;
8005 	if (len == 0)
8006 		return -1;
8007 	advproto = wpabuf_alloc(len);
8008 	if (advproto == NULL)
8009 		return -1;
8010 	if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
8011 		goto fail;
8012 
8013 	if (end) {
8014 		/* Optional Query Request */
8015 		pos = end + 1;
8016 		while (*pos == ' ')
8017 			pos++;
8018 
8019 		len = os_strlen(pos);
8020 		if (len) {
8021 			if (len & 0x01)
8022 				goto fail;
8023 			len /= 2;
8024 			if (len == 0)
8025 				goto fail;
8026 			query = wpabuf_alloc(len);
8027 			if (query == NULL)
8028 				goto fail;
8029 			if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
8030 				goto fail;
8031 		}
8032 	}
8033 
8034 	ret = gas_send_request(wpa_s, dst_addr, advproto, query);
8035 
8036 fail:
8037 	wpabuf_free(advproto);
8038 	wpabuf_free(query);
8039 
8040 	return ret;
8041 }
8042 
8043 
gas_response_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8044 static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
8045 			    size_t buflen)
8046 {
8047 	u8 addr[ETH_ALEN];
8048 	int dialog_token;
8049 	int used;
8050 	char *pos;
8051 	size_t resp_len, start, requested_len;
8052 	struct wpabuf *resp;
8053 	int ret;
8054 
8055 	used = hwaddr_aton2(cmd, addr);
8056 	if (used < 0)
8057 		return -1;
8058 
8059 	pos = cmd + used;
8060 	while (*pos == ' ')
8061 		pos++;
8062 	dialog_token = atoi(pos);
8063 
8064 	if (wpa_s->last_gas_resp &&
8065 	    ether_addr_equal(addr, wpa_s->last_gas_addr) &&
8066 	    dialog_token == wpa_s->last_gas_dialog_token)
8067 		resp = wpa_s->last_gas_resp;
8068 	else if (wpa_s->prev_gas_resp &&
8069 		 ether_addr_equal(addr, wpa_s->prev_gas_addr) &&
8070 		 dialog_token == wpa_s->prev_gas_dialog_token)
8071 		resp = wpa_s->prev_gas_resp;
8072 	else
8073 		return -1;
8074 
8075 	resp_len = wpabuf_len(resp);
8076 	start = 0;
8077 	requested_len = resp_len;
8078 
8079 	pos = os_strchr(pos, ' ');
8080 	if (pos) {
8081 		start = atoi(pos);
8082 		if (start > resp_len)
8083 			return os_snprintf(buf, buflen, "FAIL-Invalid range");
8084 		pos = os_strchr(pos, ',');
8085 		if (pos == NULL)
8086 			return -1;
8087 		pos++;
8088 		requested_len = atoi(pos);
8089 		if (start + requested_len > resp_len)
8090 			return os_snprintf(buf, buflen, "FAIL-Invalid range");
8091 	}
8092 
8093 	if (requested_len * 2 + 1 > buflen)
8094 		return os_snprintf(buf, buflen, "FAIL-Too long response");
8095 
8096 	ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
8097 			       requested_len);
8098 
8099 	if (start + requested_len == resp_len) {
8100 		/*
8101 		 * Free memory by dropping the response after it has been
8102 		 * fetched.
8103 		 */
8104 		if (resp == wpa_s->prev_gas_resp) {
8105 			wpabuf_free(wpa_s->prev_gas_resp);
8106 			wpa_s->prev_gas_resp = NULL;
8107 		} else {
8108 			wpabuf_free(wpa_s->last_gas_resp);
8109 			wpa_s->last_gas_resp = NULL;
8110 		}
8111 	}
8112 
8113 	return ret;
8114 }
8115 #endif /* CONFIG_INTERWORKING */
8116 
8117 
8118 #ifdef CONFIG_HS20
8119 
get_hs20_anqp(struct wpa_supplicant * wpa_s,char * dst)8120 static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
8121 {
8122 	u8 dst_addr[ETH_ALEN];
8123 	int used;
8124 	char *pos;
8125 	u32 subtypes = 0;
8126 
8127 	used = hwaddr_aton2(dst, dst_addr);
8128 	if (used < 0)
8129 		return -1;
8130 	pos = dst + used;
8131 	if (*pos == ' ')
8132 		pos++;
8133 	for (;;) {
8134 		int num = atoi(pos);
8135 		if (num <= 0 || num > 31)
8136 			return -1;
8137 		subtypes |= BIT(num);
8138 		pos = os_strchr(pos + 1, ',');
8139 		if (pos == NULL)
8140 			break;
8141 		pos++;
8142 	}
8143 
8144 	if (subtypes == 0)
8145 		return -1;
8146 
8147 	return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
8148 }
8149 
8150 
hs20_nai_home_realm_list(struct wpa_supplicant * wpa_s,const u8 * addr,const char * realm)8151 static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
8152 				    const u8 *addr, const char *realm)
8153 {
8154 	u8 *buf;
8155 	size_t rlen, len;
8156 	int ret;
8157 
8158 	rlen = os_strlen(realm);
8159 	len = 3 + rlen;
8160 	buf = os_malloc(len);
8161 	if (buf == NULL)
8162 		return -1;
8163 	buf[0] = 1; /* NAI Home Realm Count */
8164 	buf[1] = 0; /* Formatted in accordance with RFC 4282 */
8165 	buf[2] = rlen;
8166 	os_memcpy(buf + 3, realm, rlen);
8167 
8168 	ret = hs20_anqp_send_req(wpa_s, addr,
8169 				 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
8170 				 buf, len, 0);
8171 
8172 	os_free(buf);
8173 
8174 	return ret;
8175 }
8176 
8177 
hs20_get_nai_home_realm_list(struct wpa_supplicant * wpa_s,char * dst)8178 static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
8179 					char *dst)
8180 {
8181 	struct wpa_cred *cred = wpa_s->conf->cred;
8182 	u8 dst_addr[ETH_ALEN];
8183 	int used;
8184 	u8 *buf;
8185 	size_t len;
8186 	int ret;
8187 
8188 	used = hwaddr_aton2(dst, dst_addr);
8189 	if (used < 0)
8190 		return -1;
8191 
8192 	while (dst[used] == ' ')
8193 		used++;
8194 	if (os_strncmp(dst + used, "realm=", 6) == 0)
8195 		return hs20_nai_home_realm_list(wpa_s, dst_addr,
8196 						dst + used + 6);
8197 
8198 	len = os_strlen(dst + used);
8199 
8200 	if (len == 0 && cred && cred->realm)
8201 		return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
8202 
8203 	if (len & 1)
8204 		return -1;
8205 	len /= 2;
8206 	buf = os_malloc(len);
8207 	if (buf == NULL)
8208 		return -1;
8209 	if (hexstr2bin(dst + used, buf, len) < 0) {
8210 		os_free(buf);
8211 		return -1;
8212 	}
8213 
8214 	ret = hs20_anqp_send_req(wpa_s, dst_addr,
8215 				 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
8216 				 buf, len, 0);
8217 	os_free(buf);
8218 
8219 	return ret;
8220 }
8221 
8222 
get_hs20_icon(struct wpa_supplicant * wpa_s,char * cmd,char * reply,int buflen)8223 static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
8224 			 int buflen)
8225 {
8226 	u8 dst_addr[ETH_ALEN];
8227 	int used;
8228 	char *ctx = NULL, *icon, *poffset, *psize;
8229 
8230 	used = hwaddr_aton2(cmd, dst_addr);
8231 	if (used < 0)
8232 		return -1;
8233 	cmd += used;
8234 
8235 	icon = str_token(cmd, " ", &ctx);
8236 	poffset = str_token(cmd, " ", &ctx);
8237 	psize = str_token(cmd, " ", &ctx);
8238 	if (!icon || !poffset || !psize)
8239 		return -1;
8240 
8241 	wpa_s->fetch_osu_icon_in_progress = 0;
8242 	return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
8243 			     reply, buflen);
8244 }
8245 
8246 
del_hs20_icon(struct wpa_supplicant * wpa_s,char * cmd)8247 static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
8248 {
8249 	u8 dst_addr[ETH_ALEN];
8250 	int used;
8251 	char *icon;
8252 
8253 	if (!cmd[0])
8254 		return hs20_del_icon(wpa_s, NULL, NULL);
8255 
8256 	used = hwaddr_aton2(cmd, dst_addr);
8257 	if (used < 0)
8258 		return -1;
8259 
8260 	while (cmd[used] == ' ')
8261 		used++;
8262 	icon = cmd[used] ? &cmd[used] : NULL;
8263 
8264 	return hs20_del_icon(wpa_s, dst_addr, icon);
8265 }
8266 
8267 
hs20_icon_request(struct wpa_supplicant * wpa_s,char * cmd,int inmem)8268 static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
8269 {
8270 	u8 dst_addr[ETH_ALEN];
8271 	int used;
8272 	char *icon;
8273 
8274 	used = hwaddr_aton2(cmd, dst_addr);
8275 	if (used < 0)
8276 		return -1;
8277 
8278 	while (cmd[used] == ' ')
8279 		used++;
8280 	icon = &cmd[used];
8281 
8282 	wpa_s->fetch_osu_icon_in_progress = 0;
8283 	return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
8284 				  (u8 *) icon, os_strlen(icon), inmem);
8285 }
8286 
8287 #endif /* CONFIG_HS20 */
8288 
8289 
8290 #ifdef CONFIG_AUTOSCAN
8291 
wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant * wpa_s,char * cmd)8292 static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
8293 					      char *cmd)
8294 {
8295 	enum wpa_states state = wpa_s->wpa_state;
8296 	char *new_params = NULL;
8297 
8298 	if (os_strlen(cmd) > 0) {
8299 		new_params = os_strdup(cmd);
8300 		if (new_params == NULL)
8301 			return -1;
8302 	}
8303 
8304 	os_free(wpa_s->conf->autoscan);
8305 	wpa_s->conf->autoscan = new_params;
8306 
8307 	if (wpa_s->conf->autoscan == NULL)
8308 		autoscan_deinit(wpa_s);
8309 	else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
8310 		autoscan_init(wpa_s, 1);
8311 	else if (state == WPA_SCANNING)
8312 		wpa_supplicant_reinit_autoscan(wpa_s);
8313 	else
8314 		wpa_printf(MSG_DEBUG, "No autoscan update in state %s",
8315 			   wpa_supplicant_state_txt(state));
8316 
8317 	return 0;
8318 }
8319 
8320 #endif /* CONFIG_AUTOSCAN */
8321 
8322 
8323 #ifdef CONFIG_WNM
8324 
wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant * wpa_s,char * cmd)8325 static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
8326 {
8327 	int enter;
8328 	int intval = 0;
8329 	char *pos;
8330 	int ret;
8331 	struct wpabuf *tfs_req = NULL;
8332 
8333 	if (os_strncmp(cmd, "enter", 5) == 0)
8334 		enter = 1;
8335 	else if (os_strncmp(cmd, "exit", 4) == 0)
8336 		enter = 0;
8337 	else
8338 		return -1;
8339 
8340 	pos = os_strstr(cmd, " interval=");
8341 	if (pos)
8342 		intval = atoi(pos + 10);
8343 
8344 	pos = os_strstr(cmd, " tfs_req=");
8345 	if (pos) {
8346 		char *end;
8347 		size_t len;
8348 		pos += 9;
8349 		end = os_strchr(pos, ' ');
8350 		if (end)
8351 			len = end - pos;
8352 		else
8353 			len = os_strlen(pos);
8354 		if (len & 1)
8355 			return -1;
8356 		len /= 2;
8357 		tfs_req = wpabuf_alloc(len);
8358 		if (tfs_req == NULL)
8359 			return -1;
8360 		if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
8361 			wpabuf_free(tfs_req);
8362 			return -1;
8363 		}
8364 	}
8365 
8366 	ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
8367 					   WNM_SLEEP_MODE_EXIT, intval,
8368 					   tfs_req);
8369 	wpabuf_free(tfs_req);
8370 
8371 	return ret;
8372 }
8373 
8374 
wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant * wpa_s,char * cmd)8375 static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
8376 {
8377 	int query_reason, list = 0;
8378 	char *btm_candidates = NULL;
8379 
8380 	query_reason = atoi(cmd);
8381 
8382 	cmd = os_strchr(cmd, ' ');
8383 	if (cmd) {
8384 		if (os_strncmp(cmd, " list", 5) == 0)
8385 			list = 1;
8386 		else
8387 			btm_candidates = cmd;
8388 	}
8389 
8390 	wpa_printf(MSG_DEBUG,
8391 		   "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
8392 		   query_reason, list ? " candidate list" : "");
8393 
8394 	return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason,
8395 						  btm_candidates,
8396 						  list);
8397 }
8398 
8399 
wpas_ctrl_iface_coloc_intf_report(struct wpa_supplicant * wpa_s,char * cmd)8400 static int wpas_ctrl_iface_coloc_intf_report(struct wpa_supplicant *wpa_s,
8401 					     char *cmd)
8402 {
8403 	struct wpabuf *elems;
8404 	int ret;
8405 
8406 	elems = wpabuf_parse_bin(cmd);
8407 	if (!elems)
8408 		return -1;
8409 
8410 	ret = wnm_send_coloc_intf_report(wpa_s, 0, elems);
8411 	wpabuf_free(elems);
8412 	return ret;
8413 }
8414 
8415 #endif /* CONFIG_WNM */
8416 
8417 
wpa_supplicant_signal_poll(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8418 static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
8419 				      size_t buflen)
8420 {
8421 	struct wpa_signal_info si;
8422 	int ret;
8423 	char *pos, *end;
8424 
8425 	ret = wpa_drv_signal_poll(wpa_s, &si);
8426 	if (ret)
8427 		return -1;
8428 
8429 	pos = buf;
8430 	end = buf + buflen;
8431 
8432 	ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%lu\n"
8433 			  "NOISE=%d\nFREQUENCY=%u\n",
8434 			  si.data.signal, si.data.current_tx_rate / 1000,
8435 			  si.current_noise, si.frequency);
8436 	if (os_snprintf_error(end - pos, ret))
8437 		return -1;
8438 	pos += ret;
8439 
8440 	if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
8441 		ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
8442 				  channel_width_to_string(si.chanwidth));
8443 		if (os_snprintf_error(end - pos, ret))
8444 			return -1;
8445 		pos += ret;
8446 	}
8447 
8448 	if (si.center_frq1 > 0) {
8449 		ret = os_snprintf(pos, end - pos, "CENTER_FRQ1=%d\n",
8450 				  si.center_frq1);
8451 		if (os_snprintf_error(end - pos, ret))
8452 			return -1;
8453 		pos += ret;
8454 	}
8455 
8456 	if (si.center_frq2 > 0) {
8457 		ret = os_snprintf(pos, end - pos, "CENTER_FRQ2=%d\n",
8458 				  si.center_frq2);
8459 		if (os_snprintf_error(end - pos, ret))
8460 			return -1;
8461 		pos += ret;
8462 	}
8463 
8464 	if (si.data.avg_signal) {
8465 		ret = os_snprintf(pos, end - pos,
8466 				  "AVG_RSSI=%d\n", si.data.avg_signal);
8467 		if (os_snprintf_error(end - pos, ret))
8468 			return -1;
8469 		pos += ret;
8470 	}
8471 
8472 	if (si.data.avg_beacon_signal) {
8473 		ret = os_snprintf(pos, end - pos,
8474 				  "AVG_BEACON_RSSI=%d\n",
8475 				  si.data.avg_beacon_signal);
8476 		if (os_snprintf_error(end - pos, ret))
8477 			return -1;
8478 		pos += ret;
8479 	}
8480 
8481 	return pos - buf;
8482 }
8483 
8484 
wpas_ctrl_iface_signal_monitor(struct wpa_supplicant * wpa_s,const char * cmd)8485 static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
8486 					  const char *cmd)
8487 {
8488 	const char *pos;
8489 	int threshold = 0;
8490 	int hysteresis = 0;
8491 
8492 	if (wpa_s->bgscan && wpa_s->bgscan_priv) {
8493 		wpa_printf(MSG_DEBUG,
8494 			   "Reject SIGNAL_MONITOR command - bgscan is active");
8495 		return -1;
8496 	}
8497 	pos = os_strstr(cmd, "THRESHOLD=");
8498 	if (pos)
8499 		threshold = atoi(pos + 10);
8500 	pos = os_strstr(cmd, "HYSTERESIS=");
8501 	if (pos)
8502 		hysteresis = atoi(pos + 11);
8503 	return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
8504 }
8505 
8506 
8507 #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)8508 int wpas_ctrl_iface_get_pref_freq_list_override(struct wpa_supplicant *wpa_s,
8509 						enum wpa_driver_if_type if_type,
8510 						unsigned int *num,
8511 						struct weighted_pcl *freq_list)
8512 {
8513 	char *pos = wpa_s->get_pref_freq_list_override;
8514 	char *end;
8515 	unsigned int count = 0;
8516 
8517 	/* Override string format:
8518 	 *  <if_type1>:<freq1>,<freq2>,... <if_type2>:... */
8519 
8520 	while (pos) {
8521 		if (atoi(pos) == (int) if_type)
8522 			break;
8523 		pos = os_strchr(pos, ' ');
8524 		if (pos)
8525 			pos++;
8526 	}
8527 	if (!pos)
8528 		return -1;
8529 	pos = os_strchr(pos, ':');
8530 	if (!pos)
8531 		return -1;
8532 	pos++;
8533 	end = os_strchr(pos, ' ');
8534 	while (pos && (!end || pos < end) && count < *num) {
8535 		freq_list[count].freq = atoi(pos);
8536 		freq_list[count++].flag = WEIGHTED_PCL_GO | WEIGHTED_PCL_CLI;
8537 		pos = os_strchr(pos, ',');
8538 		if (pos)
8539 			pos++;
8540 	}
8541 
8542 	*num = count;
8543 	return 0;
8544 }
8545 #endif /* CONFIG_TESTING_OPTIONS */
8546 
8547 
wpas_ctrl_iface_get_pref_freq_list(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8548 static int wpas_ctrl_iface_get_pref_freq_list(
8549 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
8550 {
8551 	unsigned int num = 100, i;
8552 	int ret;
8553 	enum wpa_driver_if_type iface_type;
8554 	char *pos, *end;
8555 	struct weighted_pcl freq_list[100];
8556 
8557 	pos = buf;
8558 	end = buf + buflen;
8559 
8560 	/* buf: "<interface_type>" */
8561 	if (os_strcmp(cmd, "STATION") == 0)
8562 		iface_type = WPA_IF_STATION;
8563 	else if (os_strcmp(cmd, "AP") == 0)
8564 		iface_type = WPA_IF_AP_BSS;
8565 	else if (os_strcmp(cmd, "P2P_GO") == 0)
8566 		iface_type = WPA_IF_P2P_GO;
8567 	else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
8568 		iface_type = WPA_IF_P2P_CLIENT;
8569 	else if (os_strcmp(cmd, "IBSS") == 0)
8570 		iface_type = WPA_IF_IBSS;
8571 	else if (os_strcmp(cmd, "TDLS") == 0)
8572 		iface_type = WPA_IF_TDLS;
8573 	else
8574 		return -1;
8575 
8576 	wpa_printf(MSG_DEBUG,
8577 		   "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
8578 		   iface_type, cmd);
8579 
8580 	ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
8581 	if (ret)
8582 		return -1;
8583 
8584 	for (i = 0; i < num; i++) {
8585 		ret = os_snprintf(pos, end - pos, "%s%u",
8586 				  i > 0 ? "," : "", freq_list[i].freq);
8587 		if (os_snprintf_error(end - pos, ret))
8588 			return -1;
8589 		pos += ret;
8590 	}
8591 
8592 	return pos - buf;
8593 }
8594 
8595 
wpas_ctrl_iface_driver_flags(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8596 static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
8597 					char *buf, size_t buflen)
8598 {
8599 	int ret, i;
8600 	char *pos, *end;
8601 
8602 	ret = os_snprintf(buf, buflen, "%016llX:\n",
8603 			  (long long unsigned) wpa_s->drv_flags);
8604 	if (os_snprintf_error(buflen, ret))
8605 		return -1;
8606 
8607 	pos = buf + ret;
8608 	end = buf + buflen;
8609 
8610 	for (i = 0; i < 64; i++) {
8611 		if (wpa_s->drv_flags & (1LLU << i)) {
8612 			ret = os_snprintf(pos, end - pos, "%s\n",
8613 					  driver_flag_to_string(1LLU << i));
8614 			if (os_snprintf_error(end - pos, ret))
8615 				return -1;
8616 			pos += ret;
8617 		}
8618 	}
8619 
8620 	return pos - buf;
8621 }
8622 
8623 
wpas_ctrl_iface_driver_flags2(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8624 static int wpas_ctrl_iface_driver_flags2(struct wpa_supplicant *wpa_s,
8625 					 char *buf, size_t buflen)
8626 {
8627 	int ret, i;
8628 	char *pos, *end;
8629 
8630 	ret = os_snprintf(buf, buflen, "%016llX:\n",
8631 			  (long long unsigned) wpa_s->drv_flags2);
8632 	if (os_snprintf_error(buflen, ret))
8633 		return -1;
8634 
8635 	pos = buf + ret;
8636 	end = buf + buflen;
8637 
8638 	for (i = 0; i < 64; i++) {
8639 		if (wpa_s->drv_flags2 & (1LLU << i)) {
8640 			ret = os_snprintf(pos, end - pos, "%s\n",
8641 					  driver_flag2_to_string(1LLU << i));
8642 			if (os_snprintf_error(end - pos, ret))
8643 				return -1;
8644 			pos += ret;
8645 		}
8646 	}
8647 
8648 	return pos - buf;
8649 }
8650 
8651 
wpa_supplicant_pktcnt_poll(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8652 static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
8653 				      size_t buflen)
8654 {
8655 	struct hostap_sta_driver_data sta;
8656 	int ret;
8657 
8658 	ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
8659 	if (ret)
8660 		return -1;
8661 
8662 	ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
8663 			  sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
8664 	if (os_snprintf_error(buflen, ret))
8665 		return -1;
8666 	return ret;
8667 }
8668 
8669 
8670 #ifdef ANDROID
wpa_supplicant_driver_cmd(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8671 static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
8672 				     char *buf, size_t buflen)
8673 {
8674 	int ret;
8675 
8676 	ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
8677 #ifdef CONFIG_P2P
8678 	if (ret == 0) {
8679 		if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
8680 			struct p2p_data *p2p = wpa_s->global->p2p;
8681 			if (p2p) {
8682 				char country[3];
8683 				country[0] = cmd[8];
8684 				country[1] = cmd[9];
8685 				country[2] = 0x04;
8686 				p2p_set_country(p2p, country);
8687 			}
8688 		}
8689 		ret = os_snprintf(buf, buflen, "%s\n", "OK");
8690 		if (os_snprintf_error(buflen, ret))
8691 			ret = -1;
8692 	}
8693 #endif /* CONFIG_P2P */
8694 	return ret;
8695 }
8696 #endif /* ANDROID */
8697 
8698 
wpa_supplicant_vendor_cmd(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8699 static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
8700 				     char *buf, size_t buflen)
8701 {
8702 	int ret;
8703 	char *pos, *temp = NULL;
8704 	u8 *data = NULL;
8705 	unsigned int vendor_id, subcmd;
8706 	enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
8707 	struct wpabuf *reply;
8708 	size_t data_len = 0;
8709 
8710 	/**
8711 	 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
8712 	 * [nested=<0|1>]
8713 	 */
8714 	vendor_id = strtoul(cmd, &pos, 16);
8715 	if (!isblank((unsigned char) *pos))
8716 		return -EINVAL;
8717 
8718 	subcmd = strtoul(pos, &pos, 10);
8719 
8720 	if (*pos != '\0') {
8721 		if (!isblank((unsigned char) *pos++))
8722 			return -EINVAL;
8723 
8724 		temp = os_strchr(pos, ' ');
8725 		data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
8726 	}
8727 
8728 	if (data_len) {
8729 		data_len /= 2;
8730 		data = os_malloc(data_len);
8731 		if (!data)
8732 			return -1;
8733 
8734 		if (hexstr2bin(pos, data, data_len)) {
8735 			wpa_printf(MSG_DEBUG,
8736 				   "Vendor command: wrong parameter format");
8737 			os_free(data);
8738 			return -EINVAL;
8739 		}
8740 	}
8741 
8742 	pos = os_strstr(cmd, "nested=");
8743 	if (pos)
8744 		nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
8745 			NESTED_ATTR_NOT_USED;
8746 
8747 	reply = wpabuf_alloc((buflen - 1) / 2);
8748 	if (!reply) {
8749 		os_free(data);
8750 		return -1;
8751 	}
8752 
8753 	ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
8754 				 nested_attr_flag, reply);
8755 
8756 	if (ret == 0)
8757 		ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
8758 				       wpabuf_len(reply));
8759 
8760 	wpabuf_free(reply);
8761 	os_free(data);
8762 
8763 	return ret;
8764 }
8765 
8766 
wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant * wpa_s)8767 static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
8768 {
8769 #ifdef CONFIG_P2P
8770 	struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
8771 		wpa_s->global->p2p_init_wpa_s : wpa_s;
8772 #endif /* CONFIG_P2P */
8773 
8774 	wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
8775 
8776 	if (wpas_abort_ongoing_scan(wpa_s) == 0)
8777 		wpa_s->ignore_post_flush_scan_res = 1;
8778 
8779 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
8780 		/*
8781 		 * Avoid possible auto connect re-connection on getting
8782 		 * disconnected due to state flush.
8783 		 */
8784 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
8785 	}
8786 
8787 #ifdef CONFIG_P2P
8788 	wpas_p2p_group_remove(p2p_wpa_s, "*");
8789 	wpas_p2p_cancel(p2p_wpa_s);
8790 	p2p_ctrl_flush(p2p_wpa_s);
8791 	wpas_p2p_service_flush(p2p_wpa_s);
8792 	p2p_wpa_s->global->p2p_disabled = 0;
8793 	p2p_wpa_s->global->p2p_per_sta_psk = 0;
8794 	p2p_wpa_s->conf->num_sec_device_types = 0;
8795 	p2p_wpa_s->p2p_disable_ip_addr_req = 0;
8796 	os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
8797 	p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
8798 	p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
8799 	p2p_wpa_s->global->pending_p2ps_group = 0;
8800 	p2p_wpa_s->global->pending_p2ps_group_freq = 0;
8801 #endif /* CONFIG_P2P */
8802 
8803 #ifdef CONFIG_WPS_TESTING
8804 	wps_version_number = 0x20;
8805 	wps_testing_stub_cred = 0;
8806 	wps_corrupt_pkhash = 0;
8807 	wps_force_auth_types_in_use = 0;
8808 	wps_force_encr_types_in_use = 0;
8809 #endif /* CONFIG_WPS_TESTING */
8810 #ifdef CONFIG_WPS
8811 	wpa_s->wps_fragment_size = 0;
8812 	wpas_wps_cancel(wpa_s);
8813 	wps_registrar_flush(wpa_s->wps->registrar);
8814 #endif /* CONFIG_WPS */
8815 	wpa_s->after_wps = 0;
8816 	wpa_s->known_wps_freq = 0;
8817 
8818 #ifdef CONFIG_DPP
8819 	wpas_dpp_deinit(wpa_s);
8820 	wpa_s->dpp_init_max_tries = 0;
8821 	wpa_s->dpp_init_retry_time = 0;
8822 	wpa_s->dpp_resp_wait_time = 0;
8823 	wpa_s->dpp_resp_max_tries = 0;
8824 	wpa_s->dpp_resp_retry_time = 0;
8825 #ifdef CONFIG_DPP2
8826 	wpas_dpp_chirp_stop(wpa_s);
8827 	wpa_s->dpp_pfs_fallback = 0;
8828 #endif /* CONFIG_DPP2 */
8829 #ifdef CONFIG_DPP3
8830 	{
8831 		int i;
8832 
8833 		for (i = 0; i < DPP_PB_INFO_COUNT; i++) {
8834 			struct dpp_pb_info *info;
8835 
8836 			info = &wpa_s->dpp_pb[i];
8837 			info->rx_time.sec = 0;
8838 			info->rx_time.usec = 0;
8839 		}
8840 	}
8841 #endif /* CONFIG_DPP3 */
8842 #ifdef CONFIG_TESTING_OPTIONS
8843 	os_memset(dpp_pkex_own_mac_override, 0, ETH_ALEN);
8844 	os_memset(dpp_pkex_peer_mac_override, 0, ETH_ALEN);
8845 	dpp_pkex_ephemeral_key_override_len = 0;
8846 	dpp_protocol_key_override_len = 0;
8847 	dpp_nonce_override_len = 0;
8848 #ifdef CONFIG_DPP3
8849 	dpp_version_override = 3;
8850 #elif defined(CONFIG_DPP2)
8851 	dpp_version_override = 2;
8852 #else /* CONFIG_DPP2 */
8853 	dpp_version_override = 1;
8854 #endif /* CONFIG_DPP2 */
8855 #endif /* CONFIG_TESTING_OPTIONS */
8856 #endif /* CONFIG_DPP */
8857 
8858 #ifdef CONFIG_TDLS
8859 #ifdef CONFIG_TDLS_TESTING
8860 	tdls_testing = 0;
8861 #endif /* CONFIG_TDLS_TESTING */
8862 	wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
8863 	wpa_tdls_enable(wpa_s->wpa, 1);
8864 #endif /* CONFIG_TDLS */
8865 
8866 	eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
8867 	wpa_supplicant_stop_countermeasures(wpa_s, NULL);
8868 	wpa_s->last_michael_mic_error.sec = 0;
8869 
8870 	wpa_s->no_keep_alive = 0;
8871 	wpa_s->own_disconnect_req = 0;
8872 	wpa_s->own_reconnect_req = 0;
8873 	wpa_s->deny_ptk0_rekey = 0;
8874 
8875 	os_free(wpa_s->disallow_aps_bssid);
8876 	wpa_s->disallow_aps_bssid = NULL;
8877 	wpa_s->disallow_aps_bssid_count = 0;
8878 	os_free(wpa_s->disallow_aps_ssid);
8879 	wpa_s->disallow_aps_ssid = NULL;
8880 	wpa_s->disallow_aps_ssid_count = 0;
8881 
8882 	wpa_s->set_sta_uapsd = 0;
8883 	wpa_s->sta_uapsd = 0;
8884 
8885 	wpa_s->consecutive_conn_failures = 0;
8886 
8887 	wpa_drv_radio_disable(wpa_s, 0);
8888 	wpa_bssid_ignore_clear(wpa_s);
8889 	wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
8890 	wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
8891 	wpa_config_flush_blobs(wpa_s->conf);
8892 	wpa_s->conf->auto_interworking = 0;
8893 	wpa_s->conf->okc = 0;
8894 
8895 	ptksa_cache_flush(wpa_s->ptksa, NULL, WPA_CIPHER_NONE);
8896 	wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
8897 	rsn_preauth_deinit(wpa_s->wpa);
8898 
8899 	wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
8900 	wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
8901 	wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
8902 	eapol_sm_notify_logoff(wpa_s->eapol, false);
8903 
8904 	radio_remove_works(wpa_s, NULL, 1);
8905 	wpa_s->ext_work_in_progress = 0;
8906 
8907 	wpa_s->next_ssid = NULL;
8908 
8909 	wnm_btm_reset(wpa_s);
8910 
8911 #ifdef CONFIG_INTERWORKING
8912 #ifdef CONFIG_HS20
8913 	hs20_cancel_fetch_osu(wpa_s);
8914 	hs20_del_icon(wpa_s, NULL, NULL);
8915 #endif /* CONFIG_HS20 */
8916 #endif /* CONFIG_INTERWORKING */
8917 
8918 	wpa_s->ext_mgmt_frame_handling = 0;
8919 	wpa_s->ext_eapol_frame_io = 0;
8920 #ifdef CONFIG_TESTING_OPTIONS
8921 	wpa_s->extra_roc_dur = 0;
8922 	wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
8923 	wpa_s->p2p_go_csa_on_inv = 0;
8924 	wpa_s->ignore_auth_resp = 0;
8925 	wpa_s->ignore_assoc_disallow = 0;
8926 	wpa_s->disable_sa_query = 0;
8927 	wpa_s->testing_resend_assoc = 0;
8928 	wpa_s->ignore_sae_h2e_only = 0;
8929 	wpa_s->ft_rsnxe_used = 0;
8930 	wpa_s->reject_btm_req_reason = 0;
8931 	wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
8932 	wpa_sm_set_test_eapol_m2_elems(wpa_s->wpa, NULL);
8933 	wpa_sm_set_test_eapol_m4_elems(wpa_s->wpa, NULL);
8934 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_ENCRYPT_EAPOL_M2, 0);
8935 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_ENCRYPT_EAPOL_M4, 0);
8936 	os_free(wpa_s->get_pref_freq_list_override);
8937 	wpa_s->get_pref_freq_list_override = NULL;
8938 	wpabuf_free(wpa_s->sae_commit_override);
8939 	wpa_s->sae_commit_override = NULL;
8940 	os_free(wpa_s->extra_sae_rejected_groups);
8941 	wpa_s->extra_sae_rejected_groups = NULL;
8942 	wpabuf_free(wpa_s->rsne_override_eapol);
8943 	wpa_s->rsne_override_eapol = NULL;
8944 	wpabuf_free(wpa_s->rsnxe_override_assoc);
8945 	wpa_s->rsnxe_override_assoc = NULL;
8946 	wpabuf_free(wpa_s->rsnxe_override_eapol);
8947 	wpa_s->rsnxe_override_eapol = NULL;
8948 	wpas_clear_driver_signal_override(wpa_s);
8949 #ifndef CONFIG_NO_ROBUST_AV
8950 	wpa_s->disable_scs_support = 0;
8951 	wpa_s->disable_mscs_support = 0;
8952 	wpa_s->enable_dscp_policy_capa = 0;
8953 #endif /* CONFIG_NO_ROBUST_AV */
8954 	wpa_s->oci_freq_override_eapol = 0;
8955 	wpa_s->oci_freq_override_saquery_req = 0;
8956 	wpa_s->oci_freq_override_saquery_resp = 0;
8957 	wpa_s->oci_freq_override_eapol_g2 = 0;
8958 	wpa_s->oci_freq_override_ft_assoc = 0;
8959 	wpa_s->oci_freq_override_fils_assoc = 0;
8960 	wpa_s->oci_freq_override_wnm_sleep = 0;
8961 	wpa_s->disable_eapol_g2_tx = 0;
8962 	wpa_s->test_assoc_comeback_type = -1;
8963 #ifdef CONFIG_DPP
8964 	os_free(wpa_s->dpp_config_obj_override);
8965 	wpa_s->dpp_config_obj_override = NULL;
8966 	os_free(wpa_s->dpp_discovery_override);
8967 	wpa_s->dpp_discovery_override = NULL;
8968 	os_free(wpa_s->dpp_groups_override);
8969 	wpa_s->dpp_groups_override = NULL;
8970 	wpa_s->dpp_ignore_netaccesskey_mismatch = 0;
8971 	wpa_s->dpp_discard_public_action = 0;
8972 	dpp_test = DPP_TEST_DISABLED;
8973 #endif /* CONFIG_DPP */
8974 #endif /* CONFIG_TESTING_OPTIONS */
8975 
8976 	wpa_s->disconnected = 0;
8977 	os_free(wpa_s->next_scan_freqs);
8978 	wpa_s->next_scan_freqs = NULL;
8979 	os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
8980 	wpa_s->next_scan_bssid_wildcard_ssid = 0;
8981 	os_free(wpa_s->select_network_scan_freqs);
8982 	wpa_s->select_network_scan_freqs = NULL;
8983 #ifndef CONFIG_NO_ROBUST_AV
8984 	os_memset(&wpa_s->robust_av, 0, sizeof(struct robust_av_data));
8985 #endif /* CONFIG_NO_ROBUST_AV */
8986 
8987 	wpa_bss_flush(wpa_s);
8988 	if (!dl_list_empty(&wpa_s->bss)) {
8989 		wpa_printf(MSG_DEBUG,
8990 			   "BSS table not empty after flush: %u entries, current_bss=%p bssid="
8991 			   MACSTR " pending_bssid=" MACSTR,
8992 			   dl_list_len(&wpa_s->bss), wpa_s->current_bss,
8993 			   MAC2STR(wpa_s->bssid),
8994 			   MAC2STR(wpa_s->pending_bssid));
8995 	}
8996 
8997 	eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
8998 	wpa_s->wnmsleep_used = 0;
8999 
9000 #ifdef CONFIG_SME
9001 	wpa_s->sme.last_unprot_disconnect.sec = 0;
9002 	wpa_s->sme.auth_alg = 0;
9003 #endif /* CONFIG_SME */
9004 
9005 	wpabuf_free(wpa_s->ric_ies);
9006 	wpa_s->ric_ies = NULL;
9007 
9008 	wpa_supplicant_update_channel_list(wpa_s, NULL);
9009 
9010 	free_bss_tmp_disallowed(wpa_s);
9011 
9012 #ifndef CONFIG_NO_ROBUST_AV
9013 	os_memset(&wpa_s->robust_av, 0, sizeof(struct robust_av_data));
9014 #endif /* CONFIG_NO_ROBUST_AV */
9015 
9016 #ifdef CONFIG_PASN
9017 	wpas_pasn_auth_stop(wpa_s);
9018 #endif /* CONFIG_PASN */
9019 
9020 	if (wpa_s->mac_addr_changed && wpa_s->conf->mac_addr == 0)
9021 		wpas_restore_permanent_mac_addr(wpa_s);
9022 
9023 	wpa_s->conf->ignore_old_scan_res = 0;
9024 
9025 #ifdef CONFIG_NAN_USD
9026 	wpas_nan_usd_flush(wpa_s);
9027 #endif /* CONFIG_NAN_USD */
9028 }
9029 
9030 
wpas_ctrl_radio_work_show(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)9031 static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
9032 				     char *buf, size_t buflen)
9033 {
9034 	struct wpa_radio_work *work;
9035 	char *pos, *end;
9036 	struct os_reltime now, diff;
9037 
9038 	pos = buf;
9039 	end = buf + buflen;
9040 
9041 	os_get_reltime(&now);
9042 
9043 	dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
9044 	{
9045 		int ret;
9046 
9047 		os_reltime_sub(&now, &work->time, &diff);
9048 		ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
9049 				  work->type, work->wpa_s->ifname, work->freq,
9050 				  work->started, diff.sec, diff.usec);
9051 		if (os_snprintf_error(end - pos, ret))
9052 			break;
9053 		pos += ret;
9054 	}
9055 
9056 	return pos - buf;
9057 }
9058 
9059 
wpas_ctrl_radio_work_timeout(void * eloop_ctx,void * timeout_ctx)9060 static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
9061 {
9062 	struct wpa_radio_work *work = eloop_ctx;
9063 	struct wpa_external_work *ework = work->ctx;
9064 
9065 	wpa_dbg(work->wpa_s, MSG_DEBUG,
9066 		"Timing out external radio work %u (%s)",
9067 		ework->id, work->type);
9068 	wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
9069 	work->wpa_s->ext_work_in_progress = 0;
9070 	radio_work_done(work);
9071 	os_free(ework);
9072 }
9073 
9074 
wpas_ctrl_radio_work_cb(struct wpa_radio_work * work,int deinit)9075 static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
9076 {
9077 	struct wpa_external_work *ework = work->ctx;
9078 
9079 	if (deinit) {
9080 		if (work->started)
9081 			eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
9082 					     work, NULL);
9083 
9084 		/*
9085 		 * work->type points to a buffer in ework, so need to replace
9086 		 * that here with a fixed string to avoid use of freed memory
9087 		 * in debug prints.
9088 		 */
9089 		work->type = "freed-ext-work";
9090 		work->ctx = NULL;
9091 		os_free(ework);
9092 		return;
9093 	}
9094 
9095 	wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
9096 		ework->id, ework->type);
9097 	wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
9098 	work->wpa_s->ext_work_in_progress = 1;
9099 	if (!ework->timeout)
9100 		ework->timeout = 10;
9101 	eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
9102 			       work, NULL);
9103 }
9104 
9105 
wpas_ctrl_radio_work_add(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)9106 static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
9107 				    char *buf, size_t buflen)
9108 {
9109 	struct wpa_external_work *ework;
9110 	char *pos, *pos2;
9111 	size_t type_len;
9112 	int ret;
9113 	unsigned int freq = 0;
9114 
9115 	/* format: <name> [freq=<MHz>] [timeout=<seconds>] */
9116 
9117 	ework = os_zalloc(sizeof(*ework));
9118 	if (ework == NULL)
9119 		return -1;
9120 
9121 	pos = os_strchr(cmd, ' ');
9122 	if (pos) {
9123 		type_len = pos - cmd;
9124 		pos++;
9125 
9126 		pos2 = os_strstr(pos, "freq=");
9127 		if (pos2)
9128 			freq = atoi(pos2 + 5);
9129 
9130 		pos2 = os_strstr(pos, "timeout=");
9131 		if (pos2)
9132 			ework->timeout = atoi(pos2 + 8);
9133 	} else {
9134 		type_len = os_strlen(cmd);
9135 	}
9136 	if (4 + type_len >= sizeof(ework->type))
9137 		type_len = sizeof(ework->type) - 4 - 1;
9138 	os_strlcpy(ework->type, "ext:", sizeof(ework->type));
9139 	os_memcpy(ework->type + 4, cmd, type_len);
9140 	ework->type[4 + type_len] = '\0';
9141 
9142 	wpa_s->ext_work_id++;
9143 	if (wpa_s->ext_work_id == 0)
9144 		wpa_s->ext_work_id++;
9145 	ework->id = wpa_s->ext_work_id;
9146 
9147 	if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
9148 			   ework) < 0) {
9149 		os_free(ework);
9150 		return -1;
9151 	}
9152 
9153 	ret = os_snprintf(buf, buflen, "%u", ework->id);
9154 	if (os_snprintf_error(buflen, ret))
9155 		return -1;
9156 	return ret;
9157 }
9158 
9159 
wpas_ctrl_radio_work_done(struct wpa_supplicant * wpa_s,char * cmd)9160 static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
9161 {
9162 	struct wpa_radio_work *work;
9163 	unsigned int id = atoi(cmd);
9164 
9165 	dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
9166 	{
9167 		struct wpa_external_work *ework;
9168 
9169 		if (os_strncmp(work->type, "ext:", 4) != 0)
9170 			continue;
9171 		ework = work->ctx;
9172 		if (id && ework->id != id)
9173 			continue;
9174 		wpa_dbg(wpa_s, MSG_DEBUG,
9175 			"Completed external radio work %u (%s)",
9176 			ework->id, ework->type);
9177 		eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
9178 		wpa_s->ext_work_in_progress = 0;
9179 		radio_work_done(work);
9180 		os_free(ework);
9181 		return 3; /* "OK\n" */
9182 	}
9183 
9184 	return -1;
9185 }
9186 
9187 
wpas_ctrl_radio_work(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)9188 static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
9189 				char *buf, size_t buflen)
9190 {
9191 	if (os_strcmp(cmd, "show") == 0)
9192 		return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
9193 	if (os_strncmp(cmd, "add ", 4) == 0)
9194 		return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
9195 	if (os_strncmp(cmd, "done ", 5) == 0)
9196 		return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
9197 	return -1;
9198 }
9199 
9200 
wpas_ctrl_radio_work_flush(struct wpa_supplicant * wpa_s)9201 void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
9202 {
9203 	struct wpa_radio_work *work, *tmp;
9204 
9205 	if (!wpa_s || !wpa_s->radio)
9206 		return;
9207 
9208 	dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
9209 			      struct wpa_radio_work, list) {
9210 		struct wpa_external_work *ework;
9211 
9212 		if (os_strncmp(work->type, "ext:", 4) != 0)
9213 			continue;
9214 		ework = work->ctx;
9215 		wpa_dbg(wpa_s, MSG_DEBUG,
9216 			"Flushing%s external radio work %u (%s)",
9217 			work->started ? " started" : "", ework->id,
9218 			ework->type);
9219 		if (work->started)
9220 			eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
9221 					     work, NULL);
9222 		radio_work_done(work);
9223 		os_free(ework);
9224 	}
9225 }
9226 
9227 
wpas_ctrl_eapol_response(void * eloop_ctx,void * timeout_ctx)9228 static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
9229 {
9230 	struct wpa_supplicant *wpa_s = eloop_ctx;
9231 	eapol_sm_notify_ctrl_response(wpa_s->eapol);
9232 }
9233 
9234 
scan_id_list_parse(struct wpa_supplicant * wpa_s,const char * value,unsigned int * scan_id_count,int scan_id[])9235 static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
9236 			      unsigned int *scan_id_count, int scan_id[])
9237 {
9238 	const char *pos = value;
9239 
9240 	while (pos) {
9241 		if (*pos == ' ' || *pos == '\0')
9242 			break;
9243 		if (*scan_id_count == MAX_SCAN_ID)
9244 			return -1;
9245 		scan_id[(*scan_id_count)++] = atoi(pos);
9246 		pos = os_strchr(pos, ',');
9247 		if (pos)
9248 			pos++;
9249 	}
9250 
9251 	return 0;
9252 }
9253 
9254 
wpas_ctrl_scan(struct wpa_supplicant * wpa_s,char * params,char * reply,int reply_size,int * reply_len)9255 static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
9256 			   char *reply, int reply_size, int *reply_len)
9257 {
9258 	char *pos;
9259 	unsigned int manual_scan_passive = 0;
9260 	unsigned int manual_scan_use_id = 0;
9261 	unsigned int manual_scan_only_new = 0;
9262 	unsigned int scan_only = 0;
9263 	unsigned int scan_id_count = 0;
9264 	unsigned int manual_non_coloc_6ghz = 0;
9265 	int scan_id[MAX_SCAN_ID];
9266 	void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
9267 				 struct wpa_scan_results *scan_res);
9268 	int *manual_scan_freqs = NULL;
9269 	struct wpa_ssid_value *ssid = NULL, *ns;
9270 	unsigned int ssid_count = 0;
9271 
9272 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
9273 		*reply_len = -1;
9274 		return;
9275 	}
9276 
9277 	if (radio_work_pending(wpa_s, "scan")) {
9278 		wpa_printf(MSG_DEBUG,
9279 			   "Pending scan scheduled - reject new request");
9280 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
9281 		return;
9282 	}
9283 
9284 #ifdef CONFIG_INTERWORKING
9285 	if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
9286 		wpa_printf(MSG_DEBUG,
9287 			   "Interworking select in progress - reject new scan");
9288 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
9289 		return;
9290 	}
9291 #endif /* CONFIG_INTERWORKING */
9292 
9293 	if (params) {
9294 		if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
9295 			scan_only = 1;
9296 
9297 		pos = os_strstr(params, "freq=");
9298 		if (pos) {
9299 			manual_scan_freqs = freq_range_to_channel_list(wpa_s,
9300 								       pos + 5);
9301 			if (manual_scan_freqs == NULL) {
9302 				*reply_len = -1;
9303 				goto done;
9304 			}
9305 		}
9306 
9307 		pos = os_strstr(params, "passive=");
9308 		if (pos)
9309 			manual_scan_passive = !!atoi(pos + 8);
9310 
9311 		pos = os_strstr(params, "use_id=");
9312 		if (pos)
9313 			manual_scan_use_id = atoi(pos + 7);
9314 
9315 		pos = os_strstr(params, "only_new=1");
9316 		if (pos)
9317 			manual_scan_only_new = 1;
9318 
9319 		pos = os_strstr(params, "scan_id=");
9320 		if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
9321 					      scan_id) < 0) {
9322 			*reply_len = -1;
9323 			goto done;
9324 		}
9325 
9326 		pos = os_strstr(params, "bssid=");
9327 		if (pos) {
9328 			u8 bssid[ETH_ALEN];
9329 
9330 			pos += 6;
9331 			if (hwaddr_aton(pos, bssid)) {
9332 				wpa_printf(MSG_ERROR, "Invalid BSSID %s", pos);
9333 				*reply_len = -1;
9334 				goto done;
9335 			}
9336 			os_memcpy(wpa_s->next_scan_bssid, bssid, ETH_ALEN);
9337 
9338 			wpa_s->next_scan_bssid_wildcard_ssid =
9339 				os_strstr(params, "wildcard_ssid=1") != NULL;
9340 		}
9341 
9342 		pos = os_strstr(params, "non_coloc_6ghz=");
9343 		if (pos)
9344 			manual_non_coloc_6ghz = !!atoi(pos + 15);
9345 
9346 		pos = params;
9347 		while (pos && *pos != '\0') {
9348 			if (os_strncmp(pos, "ssid ", 5) == 0) {
9349 				char *end;
9350 
9351 				pos += 5;
9352 				end = pos;
9353 				while (*end) {
9354 					if (*end == '\0' || *end == ' ')
9355 						break;
9356 					end++;
9357 				}
9358 
9359 				ns = os_realloc_array(
9360 					ssid, ssid_count + 1,
9361 					sizeof(struct wpa_ssid_value));
9362 				if (ns == NULL) {
9363 					*reply_len = -1;
9364 					goto done;
9365 				}
9366 				ssid = ns;
9367 
9368 				if ((end - pos) & 0x01 ||
9369 				    end - pos > 2 * SSID_MAX_LEN ||
9370 				    hexstr2bin(pos, ssid[ssid_count].ssid,
9371 					       (end - pos) / 2) < 0) {
9372 					wpa_printf(MSG_DEBUG,
9373 						   "Invalid SSID value '%s'",
9374 						   pos);
9375 					*reply_len = -1;
9376 					goto done;
9377 				}
9378 				ssid[ssid_count].ssid_len = (end - pos) / 2;
9379 				wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
9380 						  ssid[ssid_count].ssid,
9381 						  ssid[ssid_count].ssid_len);
9382 				ssid_count++;
9383 				pos = end;
9384 			}
9385 
9386 			pos = os_strchr(pos, ' ');
9387 			if (pos)
9388 				pos++;
9389 		}
9390 	}
9391 
9392 	wpa_s->num_ssids_from_scan_req = ssid_count;
9393 	os_free(wpa_s->ssids_from_scan_req);
9394 	if (ssid_count) {
9395 		wpa_s->ssids_from_scan_req = ssid;
9396 		ssid = NULL;
9397 	} else {
9398 		wpa_s->ssids_from_scan_req = NULL;
9399 	}
9400 
9401 	if (scan_only)
9402 		scan_res_handler = scan_only_handler;
9403 	else if (wpa_s->scan_res_handler == scan_only_handler)
9404 		scan_res_handler = NULL;
9405 	else
9406 		scan_res_handler = wpa_s->scan_res_handler;
9407 
9408 	if (!wpa_s->sched_scanning && !wpa_s->scanning &&
9409 	    ((wpa_s->wpa_state <= WPA_SCANNING) ||
9410 	     (wpa_s->wpa_state == WPA_COMPLETED))) {
9411 		wpa_s->manual_scan_passive = manual_scan_passive;
9412 		wpa_s->manual_scan_use_id = manual_scan_use_id;
9413 		wpa_s->manual_scan_only_new = manual_scan_only_new;
9414 		wpa_s->scan_id_count = scan_id_count;
9415 		wpa_s->manual_non_coloc_6ghz = manual_non_coloc_6ghz;
9416 		os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
9417 		wpa_s->scan_res_handler = scan_res_handler;
9418 		os_free(wpa_s->manual_scan_freqs);
9419 		wpa_s->manual_scan_freqs = manual_scan_freqs;
9420 		manual_scan_freqs = NULL;
9421 
9422 		wpa_s->normal_scans = 0;
9423 		wpa_s->scan_req = MANUAL_SCAN_REQ;
9424 		wpa_s->after_wps = 0;
9425 		wpa_s->known_wps_freq = 0;
9426 		wpa_supplicant_req_scan(wpa_s, 0, 0);
9427 		if (wpa_s->manual_scan_use_id) {
9428 			wpa_s->manual_scan_id++;
9429 			wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
9430 				wpa_s->manual_scan_id);
9431 			*reply_len = os_snprintf(reply, reply_size, "%u\n",
9432 						 wpa_s->manual_scan_id);
9433 		}
9434 	} else if (wpa_s->sched_scanning) {
9435 		wpa_s->manual_scan_passive = manual_scan_passive;
9436 		wpa_s->manual_scan_use_id = manual_scan_use_id;
9437 		wpa_s->manual_scan_only_new = manual_scan_only_new;
9438 		wpa_s->scan_id_count = scan_id_count;
9439 		wpa_s->manual_non_coloc_6ghz = manual_non_coloc_6ghz;
9440 		os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
9441 		wpa_s->scan_res_handler = scan_res_handler;
9442 		os_free(wpa_s->manual_scan_freqs);
9443 		wpa_s->manual_scan_freqs = manual_scan_freqs;
9444 		manual_scan_freqs = NULL;
9445 
9446 		wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
9447 		wpa_supplicant_cancel_sched_scan(wpa_s);
9448 		wpa_s->scan_req = MANUAL_SCAN_REQ;
9449 		wpa_supplicant_req_scan(wpa_s, 0, 0);
9450 		if (wpa_s->manual_scan_use_id) {
9451 			wpa_s->manual_scan_id++;
9452 			*reply_len = os_snprintf(reply, reply_size, "%u\n",
9453 						 wpa_s->manual_scan_id);
9454 			wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
9455 				wpa_s->manual_scan_id);
9456 		}
9457 	} else {
9458 		wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
9459 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
9460 	}
9461 
9462 done:
9463 	os_free(manual_scan_freqs);
9464 	os_free(ssid);
9465 }
9466 
9467 
9468 #ifdef CONFIG_TESTING_OPTIONS
9469 
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)9470 static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
9471 				       unsigned int freq, const u8 *dst,
9472 				       const u8 *src, const u8 *bssid,
9473 				       const u8 *data, size_t data_len,
9474 				       enum offchannel_send_action_result
9475 				       result)
9476 {
9477 	wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
9478 		" src=" MACSTR " bssid=" MACSTR " result=%s",
9479 		freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
9480 		result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
9481 		"SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
9482 			     "NO_ACK" : "FAILED"));
9483 }
9484 
9485 
wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant * wpa_s,char * cmd)9486 static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
9487 {
9488 	char *pos, *param;
9489 	size_t len;
9490 	u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
9491 	int res, used;
9492 	int freq = 0, no_cck = 0, wait_time = 0;
9493 
9494 	/* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
9495 	 *    <action=Action frame payload> */
9496 
9497 	wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
9498 
9499 	pos = cmd;
9500 	used = hwaddr_aton2(pos, da);
9501 	if (used < 0)
9502 		return -1;
9503 	pos += used;
9504 	while (*pos == ' ')
9505 		pos++;
9506 	used = hwaddr_aton2(pos, bssid);
9507 	if (used < 0)
9508 		return -1;
9509 	pos += used;
9510 
9511 	param = os_strstr(pos, " freq=");
9512 	if (param) {
9513 		param += 6;
9514 		freq = atoi(param);
9515 	}
9516 
9517 	param = os_strstr(pos, " no_cck=");
9518 	if (param) {
9519 		param += 8;
9520 		no_cck = atoi(param);
9521 	}
9522 
9523 	param = os_strstr(pos, " wait_time=");
9524 	if (param) {
9525 		param += 11;
9526 		wait_time = atoi(param);
9527 	}
9528 
9529 	param = os_strstr(pos, " action=");
9530 	if (param == NULL)
9531 		return -1;
9532 	param += 8;
9533 
9534 	len = os_strlen(param);
9535 	if (len & 1)
9536 		return -1;
9537 	len /= 2;
9538 
9539 	buf = os_malloc(len);
9540 	if (buf == NULL)
9541 		return -1;
9542 
9543 	if (hexstr2bin(param, buf, len) < 0) {
9544 		os_free(buf);
9545 		return -1;
9546 	}
9547 
9548 	res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
9549 				     buf, len, wait_time,
9550 				     wpas_ctrl_iface_mgmt_tx_cb, no_cck);
9551 	os_free(buf);
9552 	return res;
9553 }
9554 
9555 
wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant * wpa_s)9556 static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
9557 {
9558 	wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
9559 	offchannel_send_action_done(wpa_s);
9560 }
9561 
9562 
wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant * wpa_s,char * cmd)9563 static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
9564 					   char *cmd)
9565 {
9566 	char *pos, *param;
9567 	size_t len;
9568 	u8 *buf;
9569 	int freq = 0, datarate = 0, ssi_signal = 0;
9570 	union wpa_event_data event;
9571 
9572 	if (!wpa_s->ext_mgmt_frame_handling)
9573 		return -1;
9574 
9575 	/* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
9576 
9577 	wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
9578 
9579 	pos = cmd;
9580 	param = os_strstr(pos, "freq=");
9581 	if (param) {
9582 		param += 5;
9583 		freq = atoi(param);
9584 	}
9585 
9586 	param = os_strstr(pos, " datarate=");
9587 	if (param) {
9588 		param += 10;
9589 		datarate = atoi(param);
9590 	}
9591 
9592 	param = os_strstr(pos, " ssi_signal=");
9593 	if (param) {
9594 		param += 12;
9595 		ssi_signal = atoi(param);
9596 	}
9597 
9598 	param = os_strstr(pos, " frame=");
9599 	if (param == NULL)
9600 		return -1;
9601 	param += 7;
9602 
9603 	len = os_strlen(param);
9604 	if (len & 1)
9605 		return -1;
9606 	len /= 2;
9607 
9608 	buf = os_malloc(len);
9609 	if (buf == NULL)
9610 		return -1;
9611 
9612 	if (hexstr2bin(param, buf, len) < 0) {
9613 		os_free(buf);
9614 		return -1;
9615 	}
9616 
9617 	os_memset(&event, 0, sizeof(event));
9618 	event.rx_mgmt.freq = freq;
9619 	event.rx_mgmt.frame = buf;
9620 	event.rx_mgmt.frame_len = len;
9621 	event.rx_mgmt.ssi_signal = ssi_signal;
9622 	event.rx_mgmt.datarate = datarate;
9623 	wpa_s->ext_mgmt_frame_handling = 0;
9624 	wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
9625 	wpa_s->ext_mgmt_frame_handling = 1;
9626 
9627 	os_free(buf);
9628 
9629 	return 0;
9630 }
9631 
9632 
wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant * wpa_s,char * param)9633 static int wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant *wpa_s,
9634 					   char *param)
9635 {
9636 	struct wpa_scan_res *res;
9637 	struct os_reltime now;
9638 	char *pos, *end;
9639 	int ret = -1;
9640 
9641 	if (!param)
9642 		return -1;
9643 
9644 	if (os_strcmp(param, "START") == 0) {
9645 		wpa_bss_update_start(wpa_s);
9646 		return 0;
9647 	}
9648 
9649 	if (os_strcmp(param, "END") == 0) {
9650 		wpa_bss_update_end(wpa_s, NULL, 1);
9651 		return 0;
9652 	}
9653 
9654 	if (os_strncmp(param, "BSS ", 4) != 0)
9655 		return -1;
9656 	param += 3;
9657 
9658 	res = os_zalloc(sizeof(*res) + os_strlen(param) / 2);
9659 	if (!res)
9660 		return -1;
9661 
9662 	pos = os_strstr(param, " flags=");
9663 	if (pos)
9664 		res->flags = strtol(pos + 7, NULL, 16);
9665 
9666 	pos = os_strstr(param, " bssid=");
9667 	if (pos && hwaddr_aton(pos + 7, res->bssid))
9668 		goto fail;
9669 
9670 	pos = os_strstr(param, " freq=");
9671 	if (pos)
9672 		res->freq = atoi(pos + 6);
9673 
9674 	pos = os_strstr(param, " beacon_int=");
9675 	if (pos)
9676 		res->beacon_int = atoi(pos + 12);
9677 
9678 	pos = os_strstr(param, " caps=");
9679 	if (pos)
9680 		res->caps = strtol(pos + 6, NULL, 16);
9681 
9682 	pos = os_strstr(param, " qual=");
9683 	if (pos)
9684 		res->qual = atoi(pos + 6);
9685 
9686 	pos = os_strstr(param, " noise=");
9687 	if (pos)
9688 		res->noise = atoi(pos + 7);
9689 
9690 	pos = os_strstr(param, " level=");
9691 	if (pos)
9692 		res->level = atoi(pos + 7);
9693 
9694 	pos = os_strstr(param, " tsf=");
9695 	if (pos)
9696 		res->tsf = strtoll(pos + 5, NULL, 16);
9697 
9698 	pos = os_strstr(param, " age=");
9699 	if (pos)
9700 		res->age = atoi(pos + 5);
9701 
9702 	pos = os_strstr(param, " est_throughput=");
9703 	if (pos)
9704 		res->est_throughput = atoi(pos + 16);
9705 
9706 	pos = os_strstr(param, " snr=");
9707 	if (pos)
9708 		res->snr = atoi(pos + 5);
9709 
9710 	pos = os_strstr(param, " parent_tsf=");
9711 	if (pos)
9712 		res->parent_tsf = strtoll(pos + 7, NULL, 16);
9713 
9714 	pos = os_strstr(param, " tsf_bssid=");
9715 	if (pos && hwaddr_aton(pos + 11, res->tsf_bssid))
9716 		goto fail;
9717 
9718 	pos = os_strstr(param, " ie=");
9719 	if (pos) {
9720 		pos += 4;
9721 		end = os_strchr(pos, ' ');
9722 		if (!end)
9723 			end = pos + os_strlen(pos);
9724 		res->ie_len = (end - pos) / 2;
9725 		if (hexstr2bin(pos, (u8 *) (res + 1), res->ie_len))
9726 			goto fail;
9727 	}
9728 
9729 	pos = os_strstr(param, " beacon_ie=");
9730 	if (pos) {
9731 		pos += 11;
9732 		end = os_strchr(pos, ' ');
9733 		if (!end)
9734 			end = pos + os_strlen(pos);
9735 		res->beacon_ie_len = (end - pos) / 2;
9736 		if (hexstr2bin(pos, ((u8 *) (res + 1)) + res->ie_len,
9737 			       res->beacon_ie_len))
9738 			goto fail;
9739 	}
9740 
9741 	os_get_reltime(&now);
9742 	wpa_bss_update_scan_res(wpa_s, res, &now);
9743 	ret = 0;
9744 fail:
9745 	os_free(res);
9746 
9747 	return ret;
9748 }
9749 
9750 
wpas_ctrl_iface_driver_event_assoc(struct wpa_supplicant * wpa_s,char * param)9751 static int wpas_ctrl_iface_driver_event_assoc(struct wpa_supplicant *wpa_s,
9752 					      char *param)
9753 {
9754 	union wpa_event_data event;
9755 	struct assoc_info *ai;
9756 	char *ctx = NULL;
9757 	int ret = -1;
9758 	struct wpabuf *req_ies = NULL;
9759 	struct wpabuf *resp_ies = NULL;
9760 	struct wpabuf *resp_frame = NULL;
9761 	struct wpabuf *beacon_ies = NULL;
9762 	struct wpabuf *key_replay_ctr = NULL;
9763 	struct wpabuf *ptk_kck = NULL;
9764 	struct wpabuf *ptk_kek = NULL;
9765 	struct wpabuf *fils_pmk = NULL;
9766 	char *str, *pos;
9767 	u8 addr[ETH_ALEN];
9768 	u8 fils_pmkid[PMKID_LEN];
9769 
9770 	os_memset(&event, 0, sizeof(event));
9771 	ai = &event.assoc_info;
9772 
9773 	while ((str = str_token(param, " ", &ctx))) {
9774 		pos = os_strchr(str, '=');
9775 		if (!pos)
9776 			goto fail;
9777 		*pos++ = '\0';
9778 
9779 		if (os_strcmp(str, "reassoc") == 0) {
9780 			ai->reassoc = atoi(pos);
9781 		} else if (os_strcmp(str, "req_ies") == 0) {
9782 			wpabuf_free(req_ies);
9783 			req_ies = wpabuf_parse_bin(pos);
9784 			if (!req_ies)
9785 				goto fail;
9786 			ai->req_ies = wpabuf_head(req_ies);
9787 			ai->req_ies_len = wpabuf_len(req_ies);
9788 		} else if (os_strcmp(str, "resp_ies") == 0) {
9789 			wpabuf_free(resp_ies);
9790 			resp_ies = wpabuf_parse_bin(pos);
9791 			if (!resp_ies)
9792 				goto fail;
9793 			ai->resp_ies = wpabuf_head(resp_ies);
9794 			ai->resp_ies_len = wpabuf_len(resp_ies);
9795 		} else if (os_strcmp(str, "resp_frame") == 0) {
9796 			wpabuf_free(resp_frame);
9797 			resp_frame = wpabuf_parse_bin(pos);
9798 			if (!resp_frame)
9799 				goto fail;
9800 			ai->resp_frame = wpabuf_head(resp_frame);
9801 			ai->resp_frame_len = wpabuf_len(resp_frame);
9802 		} else if (os_strcmp(str, "beacon_ies") == 0) {
9803 			wpabuf_free(beacon_ies);
9804 			beacon_ies = wpabuf_parse_bin(pos);
9805 			if (!beacon_ies)
9806 				goto fail;
9807 			ai->beacon_ies = wpabuf_head(beacon_ies);
9808 			ai->beacon_ies_len = wpabuf_len(beacon_ies);
9809 		} else if (os_strcmp(str, "freq") == 0) {
9810 			ai->freq = atoi(pos);
9811 		} else if (os_strcmp(str, "wmm::info_bitmap") == 0) {
9812 			ai->wmm_params.info_bitmap = atoi(pos);
9813 		} else if (os_strcmp(str, "wmm::uapsd_queues") == 0) {
9814 			ai->wmm_params.uapsd_queues = atoi(pos);
9815 		} else if (os_strcmp(str, "addr") == 0) {
9816 			if (hwaddr_aton(pos, addr))
9817 				goto fail;
9818 			ai->addr = addr;
9819 		} else if (os_strcmp(str, "authorized") == 0) {
9820 			ai->authorized = atoi(pos);
9821 		} else if (os_strcmp(str, "key_replay_ctr") == 0) {
9822 			wpabuf_free(key_replay_ctr);
9823 			key_replay_ctr = wpabuf_parse_bin(pos);
9824 			if (!key_replay_ctr)
9825 				goto fail;
9826 			ai->key_replay_ctr = wpabuf_head(key_replay_ctr);
9827 			ai->key_replay_ctr_len = wpabuf_len(key_replay_ctr);
9828 		} else if (os_strcmp(str, "ptk_kck") == 0) {
9829 			wpabuf_free(ptk_kck);
9830 			ptk_kck = wpabuf_parse_bin(pos);
9831 			if (!ptk_kck)
9832 				goto fail;
9833 			ai->ptk_kck = wpabuf_head(ptk_kck);
9834 			ai->ptk_kck_len = wpabuf_len(ptk_kck);
9835 		} else if (os_strcmp(str, "ptk_kek") == 0) {
9836 			wpabuf_free(ptk_kek);
9837 			ptk_kek = wpabuf_parse_bin(pos);
9838 			if (!ptk_kek)
9839 				goto fail;
9840 			ai->ptk_kek = wpabuf_head(ptk_kek);
9841 			ai->ptk_kek_len = wpabuf_len(ptk_kek);
9842 		} else if (os_strcmp(str, "subnet_status") == 0) {
9843 			ai->subnet_status = atoi(pos);
9844 		} else if (os_strcmp(str, "fils_erp_next_seq_num") == 0) {
9845 			ai->fils_erp_next_seq_num = atoi(pos);
9846 		} else if (os_strcmp(str, "fils_pmk") == 0) {
9847 			wpabuf_free(fils_pmk);
9848 			fils_pmk = wpabuf_parse_bin(pos);
9849 			if (!fils_pmk)
9850 				goto fail;
9851 			ai->fils_pmk = wpabuf_head(fils_pmk);
9852 			ai->fils_pmk_len = wpabuf_len(fils_pmk);
9853 		} else if (os_strcmp(str, "fils_pmkid") == 0) {
9854 			if (hexstr2bin(pos, fils_pmkid, PMKID_LEN) < 0)
9855 				goto fail;
9856 			ai->fils_pmkid = fils_pmkid;
9857 		} else {
9858 			goto fail;
9859 		}
9860 	}
9861 
9862 	wpa_supplicant_event(wpa_s, EVENT_ASSOC, &event);
9863 	ret = 0;
9864 fail:
9865 	wpabuf_free(req_ies);
9866 	wpabuf_free(resp_ies);
9867 	wpabuf_free(resp_frame);
9868 	wpabuf_free(beacon_ies);
9869 	wpabuf_free(key_replay_ctr);
9870 	wpabuf_free(ptk_kck);
9871 	wpabuf_free(ptk_kek);
9872 	wpabuf_free(fils_pmk);
9873 	return ret;
9874 }
9875 
9876 
wpas_ctrl_iface_driver_event(struct wpa_supplicant * wpa_s,char * cmd)9877 static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
9878 {
9879 	char *pos, *param;
9880 	union wpa_event_data event;
9881 	enum wpa_event_type ev;
9882 
9883 	/* <event name> [parameters..] */
9884 
9885 	wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
9886 
9887 	pos = cmd;
9888 	param = os_strchr(pos, ' ');
9889 	if (param)
9890 		*param++ = '\0';
9891 
9892 	os_memset(&event, 0, sizeof(event));
9893 
9894 	if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
9895 		ev = EVENT_INTERFACE_ENABLED;
9896 	} else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
9897 		ev = EVENT_INTERFACE_DISABLED;
9898 	} else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
9899 		ev = EVENT_AVOID_FREQUENCIES;
9900 		if (param == NULL)
9901 			param = "";
9902 		if (freq_range_list_parse(&event.freq_range, param) < 0)
9903 			return -1;
9904 		wpa_supplicant_event(wpa_s, ev, &event);
9905 		os_free(event.freq_range.range);
9906 		return 0;
9907 	} else if (os_strcmp(cmd, "SCAN_RES") == 0) {
9908 		return wpas_ctrl_iface_driver_scan_res(wpa_s, param);
9909 	} else if (os_strcmp(cmd, "ASSOC") == 0) {
9910 		return wpas_ctrl_iface_driver_event_assoc(wpa_s, param);
9911 	} else {
9912 		wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
9913 			cmd);
9914 		return -1;
9915 	}
9916 
9917 	wpa_supplicant_event(wpa_s, ev, &event);
9918 
9919 	return 0;
9920 }
9921 
9922 
wpas_ctrl_iface_eapol_rx(struct wpa_supplicant * wpa_s,char * cmd)9923 static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
9924 {
9925 	char *pos;
9926 	u8 src[ETH_ALEN], *buf;
9927 	int used;
9928 	size_t len;
9929 
9930 	wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
9931 
9932 	pos = cmd;
9933 	used = hwaddr_aton2(pos, src);
9934 	if (used < 0)
9935 		return -1;
9936 	pos += used;
9937 	while (*pos == ' ')
9938 		pos++;
9939 
9940 	len = os_strlen(pos);
9941 	if (len & 1)
9942 		return -1;
9943 	len /= 2;
9944 
9945 	buf = os_malloc(len);
9946 	if (buf == NULL)
9947 		return -1;
9948 
9949 	if (hexstr2bin(pos, buf, len) < 0) {
9950 		os_free(buf);
9951 		return -1;
9952 	}
9953 
9954 	wpa_supplicant_rx_eapol(wpa_s, src, buf, len, FRAME_ENCRYPTION_UNKNOWN);
9955 	os_free(buf);
9956 
9957 	return 0;
9958 }
9959 
9960 
wpas_ctrl_iface_eapol_tx(struct wpa_supplicant * wpa_s,char * cmd)9961 static int wpas_ctrl_iface_eapol_tx(struct wpa_supplicant *wpa_s, char *cmd)
9962 {
9963 	char *pos;
9964 	u8 dst[ETH_ALEN], *buf;
9965 	int used, ret;
9966 	size_t len;
9967 	unsigned int prev;
9968 
9969 	wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
9970 
9971 	pos = cmd;
9972 	used = hwaddr_aton2(pos, dst);
9973 	if (used < 0)
9974 		return -1;
9975 	pos += used;
9976 	while (*pos == ' ')
9977 		pos++;
9978 
9979 	len = os_strlen(pos);
9980 	if (len & 1)
9981 		return -1;
9982 	len /= 2;
9983 
9984 	buf = os_malloc(len);
9985 	if (!buf || hexstr2bin(pos, buf, len) < 0) {
9986 		os_free(buf);
9987 		return -1;
9988 	}
9989 
9990 	prev = wpa_s->ext_eapol_frame_io;
9991 	wpa_s->ext_eapol_frame_io = 0;
9992 	ret = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, buf, len);
9993 	wpa_s->ext_eapol_frame_io = prev;
9994 	os_free(buf);
9995 
9996 	return ret;
9997 }
9998 
9999 
ipv4_hdr_checksum(const void * buf,size_t len)10000 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
10001 {
10002 	size_t i;
10003 	u32 sum = 0;
10004 	const u16 *pos = buf;
10005 
10006 	for (i = 0; i < len / 2; i++)
10007 		sum += *pos++;
10008 
10009 	while (sum >> 16)
10010 		sum = (sum & 0xffff) + (sum >> 16);
10011 
10012 	return sum ^ 0xffff;
10013 }
10014 
10015 
10016 #define HWSIM_PACKETLEN 1500
10017 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
10018 
wpas_data_test_rx(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)10019 static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
10020 			      size_t len)
10021 {
10022 	struct wpa_supplicant *wpa_s = ctx;
10023 	const struct ether_header *eth;
10024 	struct ip ip;
10025 	const u8 *pos;
10026 	unsigned int i;
10027 	char extra[30];
10028 
10029 	if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
10030 		wpa_printf(MSG_DEBUG,
10031 			   "test data: RX - ignore unexpected length %d",
10032 			   (int) len);
10033 		return;
10034 	}
10035 
10036 	eth = (const struct ether_header *) buf;
10037 	os_memcpy(&ip, eth + 1, sizeof(ip));
10038 	pos = &buf[sizeof(*eth) + sizeof(ip)];
10039 
10040 	if (ip.ip_hl != 5 || ip.ip_v != 4 || ntohs(ip.ip_len) > HWSIM_IP_LEN) {
10041 		wpa_printf(MSG_DEBUG,
10042 			   "test data: RX - ignore unexpected IP header");
10043 		return;
10044 	}
10045 
10046 	for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
10047 		if (*pos != (u8) i) {
10048 			wpa_printf(MSG_DEBUG,
10049 				   "test data: RX - ignore mismatching payload");
10050 			return;
10051 		}
10052 		pos++;
10053 	}
10054 	extra[0] = '\0';
10055 	if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
10056 		os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
10057 	wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
10058 		MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
10059 }
10060 
10061 
wpas_ctrl_iface_data_test_config(struct wpa_supplicant * wpa_s,char * cmd)10062 static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
10063 					    char *cmd)
10064 {
10065 	int enabled = atoi(cmd);
10066 	char *pos;
10067 	const char *ifname;
10068 
10069 	if (!enabled) {
10070 		if (wpa_s->l2_test) {
10071 			l2_packet_deinit(wpa_s->l2_test);
10072 			wpa_s->l2_test = NULL;
10073 			wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
10074 		}
10075 		return 0;
10076 	}
10077 
10078 	if (wpa_s->l2_test)
10079 		return 0;
10080 
10081 	pos = os_strstr(cmd, " ifname=");
10082 	if (pos)
10083 		ifname = pos + 8;
10084 	else
10085 		ifname = wpa_s->ifname;
10086 
10087 	wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
10088 					ETHERTYPE_IP, wpas_data_test_rx,
10089 					wpa_s, 1);
10090 	if (wpa_s->l2_test == NULL)
10091 		return -1;
10092 
10093 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
10094 
10095 	return 0;
10096 }
10097 
10098 
wpas_ctrl_iface_data_test_tx(struct wpa_supplicant * wpa_s,char * cmd)10099 static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
10100 {
10101 	u8 dst[ETH_ALEN], src[ETH_ALEN];
10102 	char *pos, *pos2;
10103 	int used;
10104 	long int val;
10105 	u8 tos;
10106 	u8 buf[2 + HWSIM_PACKETLEN];
10107 	struct ether_header *eth;
10108 	struct ip *ip;
10109 	u8 *dpos;
10110 	unsigned int i;
10111 	size_t send_len = HWSIM_IP_LEN;
10112 
10113 	if (wpa_s->l2_test == NULL)
10114 		return -1;
10115 
10116 	/* format: <dst> <src> <tos> [len=<length>] */
10117 
10118 	pos = cmd;
10119 	used = hwaddr_aton2(pos, dst);
10120 	if (used < 0)
10121 		return -1;
10122 	pos += used;
10123 	while (*pos == ' ')
10124 		pos++;
10125 	used = hwaddr_aton2(pos, src);
10126 	if (used < 0)
10127 		return -1;
10128 	pos += used;
10129 
10130 	val = strtol(pos, &pos2, 0);
10131 	if (val < 0 || val > 0xff)
10132 		return -1;
10133 	tos = val;
10134 
10135 	pos = os_strstr(pos2, " len=");
10136 	if (pos) {
10137 		i = atoi(pos + 5);
10138 		if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
10139 			return -1;
10140 		send_len = i;
10141 	}
10142 
10143 	eth = (struct ether_header *) &buf[2];
10144 	os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
10145 	os_memcpy(eth->ether_shost, src, ETH_ALEN);
10146 	eth->ether_type = htons(ETHERTYPE_IP);
10147 	ip = (struct ip *) (eth + 1);
10148 	os_memset(ip, 0, sizeof(*ip));
10149 	ip->ip_hl = 5;
10150 	ip->ip_v = 4;
10151 	ip->ip_ttl = 64;
10152 	ip->ip_tos = tos;
10153 	ip->ip_len = htons(send_len);
10154 	ip->ip_p = 1;
10155 	ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
10156 	ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
10157 	ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
10158 	dpos = (u8 *) (ip + 1);
10159 	for (i = 0; i < send_len - sizeof(*ip); i++)
10160 		*dpos++ = i;
10161 
10162 	if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
10163 			   sizeof(struct ether_header) + send_len) < 0)
10164 		return -1;
10165 
10166 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
10167 		" tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
10168 
10169 	return 0;
10170 }
10171 
10172 
wpas_ctrl_iface_data_test_frame(struct wpa_supplicant * wpa_s,char * cmd)10173 static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
10174 					   char *cmd)
10175 {
10176 	u8 *buf;
10177 	struct ether_header *eth;
10178 	struct l2_packet_data *l2 = NULL;
10179 	size_t len;
10180 	u16 ethertype;
10181 	int res = -1;
10182 
10183 	len = os_strlen(cmd);
10184 	if (len & 1 || len < ETH_HLEN * 2)
10185 		return -1;
10186 	len /= 2;
10187 
10188 	buf = os_malloc(len);
10189 	if (buf == NULL)
10190 		return -1;
10191 
10192 	if (hexstr2bin(cmd, buf, len) < 0)
10193 		goto done;
10194 
10195 	eth = (struct ether_header *) buf;
10196 	ethertype = ntohs(eth->ether_type);
10197 
10198 	l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
10199 			    wpas_data_test_rx, wpa_s, 1);
10200 	if (l2 == NULL)
10201 		goto done;
10202 
10203 	res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
10204 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
10205 done:
10206 	if (l2)
10207 		l2_packet_deinit(l2);
10208 	os_free(buf);
10209 
10210 	return res < 0 ? -1 : 0;
10211 }
10212 
10213 
wpas_ctrl_event_test_cb(void * eloop_ctx,void * timeout_ctx)10214 static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
10215 {
10216 	struct wpa_supplicant *wpa_s = eloop_ctx;
10217 	int i, count = (intptr_t) timeout_ctx;
10218 
10219 	wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
10220 		   count);
10221 	for (i = 0; i < count; i++) {
10222 		wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
10223 			     i + 1, count);
10224 	}
10225 }
10226 
10227 
wpas_ctrl_event_test(struct wpa_supplicant * wpa_s,const char * cmd)10228 static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
10229 {
10230 	int count;
10231 
10232 	count = atoi(cmd);
10233 	if (count <= 0)
10234 		return -1;
10235 
10236 	return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
10237 				      (void *) (intptr_t) count);
10238 }
10239 
10240 
wpas_get_hex_buf(const char * val,struct wpabuf ** ret)10241 static int wpas_get_hex_buf(const char *val, struct wpabuf **ret)
10242 {
10243 	struct wpabuf *buf;
10244 	size_t len;
10245 
10246 	len = os_strlen(val);
10247 	if (len & 1)
10248 		return -1;
10249 	len /= 2;
10250 
10251 	if (len == 0) {
10252 		buf = NULL;
10253 	} else {
10254 		buf = wpabuf_alloc(len);
10255 		if (!buf)
10256 			return -1;
10257 
10258 		if (hexstr2bin(val, wpabuf_put(buf, len), len) < 0) {
10259 			wpabuf_free(buf);
10260 			return -1;
10261 		}
10262 	}
10263 
10264 	*ret = buf;
10265 	return 0;
10266 }
10267 
10268 
wpas_ctrl_test_assoc_ie(struct wpa_supplicant * wpa_s,const char * cmd)10269 static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
10270 				   const char *cmd)
10271 {
10272 	struct wpabuf *buf;
10273 
10274 	if (wpas_get_hex_buf(cmd, &buf) < 0)
10275 		return -1;
10276 	wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
10277 	return 0;
10278 }
10279 
10280 
wpas_ctrl_test_eapol_m2_elems(struct wpa_supplicant * wpa_s,const char * cmd)10281 static int wpas_ctrl_test_eapol_m2_elems(struct wpa_supplicant *wpa_s,
10282 					 const char *cmd)
10283 {
10284 	struct wpabuf *buf;
10285 
10286 	if (wpas_get_hex_buf(cmd, &buf) < 0)
10287 		return -1;
10288 	wpa_sm_set_test_eapol_m2_elems(wpa_s->wpa, buf);
10289 	return 0;
10290 }
10291 
10292 
wpas_ctrl_test_eapol_m4_elems(struct wpa_supplicant * wpa_s,const char * cmd)10293 static int wpas_ctrl_test_eapol_m4_elems(struct wpa_supplicant *wpa_s,
10294 					 const char *cmd)
10295 {
10296 	struct wpabuf *buf;
10297 
10298 	if (wpas_get_hex_buf(cmd, &buf) < 0)
10299 		return -1;
10300 	wpa_sm_set_test_eapol_m4_elems(wpa_s->wpa, buf);
10301 	return 0;
10302 }
10303 
10304 
wpas_ctrl_reset_pn(struct wpa_supplicant * wpa_s)10305 static int wpas_ctrl_reset_pn(struct wpa_supplicant *wpa_s)
10306 {
10307 	u8 zero[WPA_TK_MAX_LEN];
10308 
10309 	if (wpa_s->last_tk_alg == WPA_ALG_NONE)
10310 		return -1;
10311 
10312 	wpa_printf(MSG_INFO, "TESTING: Reset PN");
10313 	os_memset(zero, 0, sizeof(zero));
10314 
10315 	/* First, use a zero key to avoid any possible duplicate key avoidance
10316 	 * in the driver. */
10317 	if (wpa_drv_set_key(wpa_s, -1, wpa_s->last_tk_alg, wpa_s->last_tk_addr,
10318 			    wpa_s->last_tk_key_idx, 1, zero, 6,
10319 			    zero, wpa_s->last_tk_len,
10320 			    KEY_FLAG_PAIRWISE_RX_TX) < 0)
10321 		return -1;
10322 
10323 	/* Set the previously configured key to reset its TSC/RSC */
10324 	return wpa_drv_set_key(wpa_s, -1, wpa_s->last_tk_alg,
10325 			       wpa_s->last_tk_addr,
10326 			       wpa_s->last_tk_key_idx, 1, zero, 6,
10327 			       wpa_s->last_tk, wpa_s->last_tk_len,
10328 			       KEY_FLAG_PAIRWISE_RX_TX);
10329 }
10330 
10331 
wpas_ctrl_key_request(struct wpa_supplicant * wpa_s,const char * cmd)10332 static int wpas_ctrl_key_request(struct wpa_supplicant *wpa_s, const char *cmd)
10333 {
10334 	const char *pos = cmd;
10335 	int error, pairwise;
10336 
10337 	error = atoi(pos);
10338 	pos = os_strchr(pos, ' ');
10339 	if (!pos)
10340 		return -1;
10341 	pairwise = atoi(pos);
10342 	wpa_sm_key_request(wpa_s->wpa, error, pairwise);
10343 	return 0;
10344 }
10345 
10346 
wpas_ctrl_resend_assoc(struct wpa_supplicant * wpa_s)10347 static int wpas_ctrl_resend_assoc(struct wpa_supplicant *wpa_s)
10348 {
10349 #ifdef CONFIG_SME
10350 	struct wpa_driver_associate_params params;
10351 	int ret;
10352 
10353 	os_memset(&params, 0, sizeof(params));
10354 	params.bssid = wpa_s->bssid;
10355 	params.ssid = wpa_s->sme.ssid;
10356 	params.ssid_len = wpa_s->sme.ssid_len;
10357 	params.freq.freq = wpa_s->sme.freq;
10358 	if (wpa_s->last_assoc_req_wpa_ie) {
10359 		params.wpa_ie = wpabuf_head(wpa_s->last_assoc_req_wpa_ie);
10360 		params.wpa_ie_len = wpabuf_len(wpa_s->last_assoc_req_wpa_ie);
10361 	}
10362 	params.pairwise_suite = wpa_s->pairwise_cipher;
10363 	params.group_suite = wpa_s->group_cipher;
10364 	params.mgmt_group_suite = wpa_s->mgmt_group_cipher;
10365 	params.key_mgmt_suite = wpa_s->key_mgmt;
10366 	params.wpa_proto = wpa_s->wpa_proto;
10367 	params.mgmt_frame_protection = wpa_s->sme.mfp;
10368 	params.rrm_used = wpa_s->rrm.rrm_used;
10369 	if (wpa_s->sme.prev_bssid_set)
10370 		params.prev_bssid = wpa_s->sme.prev_bssid;
10371 	wpa_printf(MSG_INFO, "TESTING: Resend association request");
10372 	ret = wpa_drv_associate(wpa_s, &params);
10373 	wpa_s->testing_resend_assoc = 1;
10374 	return ret;
10375 #else /* CONFIG_SME */
10376 	return -1;
10377 #endif /* CONFIG_SME */
10378 }
10379 
10380 
wpas_ctrl_iface_send_twt_setup(struct wpa_supplicant * wpa_s,const char * cmd)10381 static int wpas_ctrl_iface_send_twt_setup(struct wpa_supplicant *wpa_s,
10382 					  const char *cmd)
10383 {
10384 	u8 dtok = 1;
10385 	int exponent = 10;
10386 	int mantissa = 8192;
10387 	u8 min_twt = 255;
10388 	unsigned long long twt = 0;
10389 	bool requestor = true;
10390 	int setup_cmd = 0;
10391 	bool trigger = true;
10392 	bool implicit = true;
10393 	bool flow_type = true;
10394 	int flow_id = 0;
10395 	bool protection = false;
10396 	u8 twt_channel = 0;
10397 	u8 control = BIT(4); /* Control field (IEEE Std 802.11ax-2021,
10398 			      * Figure 9-687 - Control field format):
10399 			      * B4 = TWT Information Frame Disabled */
10400 	const char *tok_s;
10401 
10402 	tok_s = os_strstr(cmd, " dialog=");
10403 	if (tok_s)
10404 		dtok = atoi(tok_s + os_strlen(" dialog="));
10405 
10406 	tok_s = os_strstr(cmd, " exponent=");
10407 	if (tok_s)
10408 		exponent = atoi(tok_s + os_strlen(" exponent="));
10409 
10410 	tok_s = os_strstr(cmd, " mantissa=");
10411 	if (tok_s)
10412 		mantissa = atoi(tok_s + os_strlen(" mantissa="));
10413 
10414 	tok_s = os_strstr(cmd, " min_twt=");
10415 	if (tok_s)
10416 		min_twt = atoi(tok_s + os_strlen(" min_twt="));
10417 
10418 	tok_s = os_strstr(cmd, " setup_cmd=");
10419 	if (tok_s)
10420 		setup_cmd = atoi(tok_s + os_strlen(" setup_cmd="));
10421 
10422 	tok_s = os_strstr(cmd, " twt=");
10423 	if (tok_s &&
10424 	    sscanf(tok_s + os_strlen(" twt="), "%llu", &twt) != 1)
10425 		return -1;
10426 
10427 	tok_s = os_strstr(cmd, " requestor=");
10428 	if (tok_s)
10429 		requestor = atoi(tok_s + os_strlen(" requestor="));
10430 
10431 	tok_s = os_strstr(cmd, " trigger=");
10432 	if (tok_s)
10433 		trigger = atoi(tok_s + os_strlen(" trigger="));
10434 
10435 	tok_s = os_strstr(cmd, " implicit=");
10436 	if (tok_s)
10437 		implicit = atoi(tok_s + os_strlen(" implicit="));
10438 
10439 	tok_s = os_strstr(cmd, " flow_type=");
10440 	if (tok_s)
10441 		flow_type = atoi(tok_s + os_strlen(" flow_type="));
10442 
10443 	tok_s = os_strstr(cmd, " flow_id=");
10444 	if (tok_s)
10445 		flow_id = atoi(tok_s + os_strlen(" flow_id="));
10446 
10447 	tok_s = os_strstr(cmd, " protection=");
10448 	if (tok_s)
10449 		protection = atoi(tok_s + os_strlen(" protection="));
10450 
10451 	tok_s = os_strstr(cmd, " twt_channel=");
10452 	if (tok_s)
10453 		twt_channel = atoi(tok_s + os_strlen(" twt_channel="));
10454 
10455 	tok_s = os_strstr(cmd, " control=");
10456 	if (tok_s)
10457 		control = atoi(tok_s + os_strlen(" control="));
10458 
10459 	return wpas_twt_send_setup(wpa_s, dtok, exponent, mantissa, min_twt,
10460 				   setup_cmd, twt, requestor, trigger, implicit,
10461 				   flow_type, flow_id, protection, twt_channel,
10462 				   control);
10463 }
10464 
10465 
wpas_ctrl_iface_send_twt_teardown(struct wpa_supplicant * wpa_s,const char * cmd)10466 static int wpas_ctrl_iface_send_twt_teardown(struct wpa_supplicant *wpa_s,
10467 					     const char *cmd)
10468 {
10469 	u8 flags = 0x1;
10470 	const char *tok_s;
10471 
10472 	tok_s = os_strstr(cmd, " flags=");
10473 	if (tok_s)
10474 		flags = atoi(tok_s + os_strlen(" flags="));
10475 
10476 	return wpas_twt_send_teardown(wpa_s, flags);
10477 }
10478 
10479 #endif /* CONFIG_TESTING_OPTIONS */
10480 
10481 
wpas_ctrl_vendor_elem_add(struct wpa_supplicant * wpa_s,char * cmd)10482 static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
10483 {
10484 	char *pos = cmd;
10485 	int frame;
10486 	size_t len;
10487 	struct wpabuf *buf;
10488 	struct ieee802_11_elems elems;
10489 
10490 	frame = atoi(pos);
10491 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
10492 		return -1;
10493 	wpa_s = wpas_vendor_elem(wpa_s, frame);
10494 
10495 	pos = os_strchr(pos, ' ');
10496 	if (pos == NULL)
10497 		return -1;
10498 	pos++;
10499 
10500 	len = os_strlen(pos);
10501 	if (len == 0)
10502 		return 0;
10503 	if (len & 1)
10504 		return -1;
10505 	len /= 2;
10506 
10507 	buf = wpabuf_alloc(len);
10508 	if (buf == NULL)
10509 		return -1;
10510 
10511 	if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
10512 		wpabuf_free(buf);
10513 		return -1;
10514 	}
10515 
10516 	if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
10517 	    ParseFailed) {
10518 		wpabuf_free(buf);
10519 		return -1;
10520 	}
10521 
10522 	if (wpa_s->vendor_elem[frame] == NULL) {
10523 		wpa_s->vendor_elem[frame] = buf;
10524 		goto update_ies;
10525 	}
10526 
10527 	if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
10528 		wpabuf_free(buf);
10529 		return -1;
10530 	}
10531 
10532 	wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
10533 	wpabuf_free(buf);
10534 
10535 update_ies:
10536 	wpas_vendor_elem_update(wpa_s);
10537 
10538 	if (frame == VENDOR_ELEM_PROBE_REQ ||
10539 	    frame == VENDOR_ELEM_PROBE_REQ_P2P)
10540 		wpa_supplicant_set_default_scan_ies(wpa_s);
10541 
10542 	return 0;
10543 }
10544 
10545 
wpas_ctrl_vendor_elem_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)10546 static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
10547 				     char *buf, size_t buflen)
10548 {
10549 	int frame = atoi(cmd);
10550 
10551 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
10552 		return -1;
10553 	wpa_s = wpas_vendor_elem(wpa_s, frame);
10554 
10555 	if (wpa_s->vendor_elem[frame] == NULL)
10556 		return 0;
10557 
10558 	return wpa_snprintf_hex(buf, buflen,
10559 				wpabuf_head_u8(wpa_s->vendor_elem[frame]),
10560 				wpabuf_len(wpa_s->vendor_elem[frame]));
10561 }
10562 
10563 
wpas_ctrl_vendor_elem_remove(struct wpa_supplicant * wpa_s,char * cmd)10564 static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
10565 {
10566 	char *pos = cmd;
10567 	int frame;
10568 	size_t len;
10569 	u8 *buf;
10570 	struct ieee802_11_elems elems;
10571 	int res;
10572 
10573 	frame = atoi(pos);
10574 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
10575 		return -1;
10576 	wpa_s = wpas_vendor_elem(wpa_s, frame);
10577 
10578 	pos = os_strchr(pos, ' ');
10579 	if (pos == NULL)
10580 		return -1;
10581 	pos++;
10582 
10583 	if (*pos == '*') {
10584 		wpabuf_free(wpa_s->vendor_elem[frame]);
10585 		wpa_s->vendor_elem[frame] = NULL;
10586 		wpas_vendor_elem_update(wpa_s);
10587 		return 0;
10588 	}
10589 
10590 	if (wpa_s->vendor_elem[frame] == NULL)
10591 		return -1;
10592 
10593 	len = os_strlen(pos);
10594 	if (len == 0)
10595 		return 0;
10596 	if (len & 1)
10597 		return -1;
10598 	len /= 2;
10599 
10600 	buf = os_malloc(len);
10601 	if (buf == NULL)
10602 		return -1;
10603 
10604 	if (hexstr2bin(pos, buf, len) < 0) {
10605 		os_free(buf);
10606 		return -1;
10607 	}
10608 
10609 	if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
10610 		os_free(buf);
10611 		return -1;
10612 	}
10613 
10614 	res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
10615 	os_free(buf);
10616 	return res;
10617 }
10618 
10619 
10620 #ifndef CONFIG_NO_RRM
10621 
wpas_ctrl_neighbor_rep_cb(void * ctx,struct wpabuf * neighbor_rep)10622 static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
10623 {
10624 	struct wpa_supplicant *wpa_s = ctx;
10625 	size_t len;
10626 	const u8 *data;
10627 
10628 	/*
10629 	 * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
10630 	 * BSSID[6]
10631 	 * BSSID Information[4]
10632 	 * Operating Class[1]
10633 	 * Channel Number[1]
10634 	 * PHY Type[1]
10635 	 * Optional Subelements[variable]
10636 	 */
10637 #define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
10638 
10639 	if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
10640 		wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
10641 		goto out;
10642 	}
10643 
10644 	data = wpabuf_head_u8(neighbor_rep);
10645 	len = wpabuf_len(neighbor_rep);
10646 
10647 	while (len >= 2 + NR_IE_MIN_LEN) {
10648 		const u8 *nr;
10649 		char lci[256 * 2 + 1];
10650 		char civic[256 * 2 + 1];
10651 		u8 nr_len = data[1];
10652 		const u8 *pos = data, *end;
10653 
10654 		if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
10655 		    nr_len < NR_IE_MIN_LEN) {
10656 			wpa_dbg(wpa_s, MSG_DEBUG,
10657 				"CTRL: Invalid Neighbor Report element: id=%u len=%u",
10658 				data[0], nr_len);
10659 			goto out;
10660 		}
10661 
10662 		if (2U + nr_len > len) {
10663 			wpa_dbg(wpa_s, MSG_DEBUG,
10664 				"CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
10665 				data[0], len, nr_len);
10666 			goto out;
10667 		}
10668 		pos += 2;
10669 		end = pos + nr_len;
10670 
10671 		nr = pos;
10672 		pos += NR_IE_MIN_LEN;
10673 
10674 		lci[0] = '\0';
10675 		civic[0] = '\0';
10676 		while (end - pos > 2) {
10677 			u8 s_id, s_len;
10678 
10679 			s_id = *pos++;
10680 			s_len = *pos++;
10681 			if (s_len > end - pos)
10682 				goto out;
10683 			if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
10684 				/* Measurement Token[1] */
10685 				/* Measurement Report Mode[1] */
10686 				/* Measurement Type[1] */
10687 				/* Measurement Report[variable] */
10688 				switch (pos[2]) {
10689 				case MEASURE_TYPE_LCI:
10690 					if (lci[0])
10691 						break;
10692 					wpa_snprintf_hex(lci, sizeof(lci),
10693 							 pos, s_len);
10694 					break;
10695 				case MEASURE_TYPE_LOCATION_CIVIC:
10696 					if (civic[0])
10697 						break;
10698 					wpa_snprintf_hex(civic, sizeof(civic),
10699 							 pos, s_len);
10700 					break;
10701 				}
10702 			}
10703 
10704 			pos += s_len;
10705 		}
10706 
10707 		wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
10708 			"bssid=" MACSTR
10709 			" info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
10710 			MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
10711 			nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
10712 			nr[ETH_ALEN + 6],
10713 			lci[0] ? " lci=" : "", lci,
10714 			civic[0] ? " civic=" : "", civic);
10715 
10716 		data = end;
10717 		len -= 2 + nr_len;
10718 	}
10719 
10720 out:
10721 	wpabuf_free(neighbor_rep);
10722 }
10723 
10724 
wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant * wpa_s,char * cmd)10725 static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
10726 					     char *cmd)
10727 {
10728 	struct wpa_ssid_value ssid, *ssid_p = NULL;
10729 	int ret, lci = 0, civic = 0;
10730 	char *ssid_s;
10731 
10732 	ssid_s = os_strstr(cmd, "ssid=");
10733 	if (ssid_s) {
10734 		if (ssid_parse(ssid_s + 5, &ssid)) {
10735 			wpa_msg(wpa_s, MSG_INFO,
10736 				"CTRL: Send Neighbor Report: bad SSID");
10737 			return -1;
10738 		}
10739 
10740 		ssid_p = &ssid;
10741 
10742 		/*
10743 		 * Move cmd after the SSID text that may include "lci" or
10744 		 * "civic".
10745 		 */
10746 		cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
10747 		if (cmd)
10748 			cmd++;
10749 
10750 	}
10751 
10752 	if (cmd && os_strstr(cmd, "lci"))
10753 		lci = 1;
10754 
10755 	if (cmd && os_strstr(cmd, "civic"))
10756 		civic = 1;
10757 
10758 	ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
10759 						 wpas_ctrl_neighbor_rep_cb,
10760 						 wpa_s);
10761 
10762 	return ret;
10763 }
10764 
10765 #endif /* CONFIG_NO_RRM */
10766 
10767 
wpas_ctrl_iface_erp_flush(struct wpa_supplicant * wpa_s)10768 static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
10769 {
10770 	eapol_sm_erp_flush(wpa_s->eapol);
10771 	return 0;
10772 }
10773 
10774 
wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant * wpa_s,char * cmd)10775 static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
10776 					 char *cmd)
10777 {
10778 	char *token, *context = NULL;
10779 	unsigned int enable = ~0, type = 0;
10780 	u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
10781 	u8 *addr = NULL, *mask = NULL;
10782 
10783 	while ((token = str_token(cmd, " ", &context))) {
10784 		if (os_strcasecmp(token, "scan") == 0) {
10785 			type |= MAC_ADDR_RAND_SCAN;
10786 		} else if (os_strcasecmp(token, "sched") == 0) {
10787 			type |= MAC_ADDR_RAND_SCHED_SCAN;
10788 		} else if (os_strcasecmp(token, "pno") == 0) {
10789 			type |= MAC_ADDR_RAND_PNO;
10790 		} else if (os_strcasecmp(token, "all") == 0) {
10791 			type = wpa_s->mac_addr_rand_supported;
10792 		} else if (os_strncasecmp(token, "enable=", 7) == 0) {
10793 			enable = atoi(token + 7);
10794 		} else if (os_strncasecmp(token, "addr=", 5) == 0) {
10795 			addr = _addr;
10796 			if (hwaddr_aton(token + 5, addr)) {
10797 				wpa_printf(MSG_INFO,
10798 					   "CTRL: Invalid MAC address: %s",
10799 					   token);
10800 				return -1;
10801 			}
10802 		} else if (os_strncasecmp(token, "mask=", 5) == 0) {
10803 			mask = _mask;
10804 			if (hwaddr_aton(token + 5, mask)) {
10805 				wpa_printf(MSG_INFO,
10806 					   "CTRL: Invalid MAC address mask: %s",
10807 					   token);
10808 				return -1;
10809 			}
10810 		} else {
10811 			wpa_printf(MSG_INFO,
10812 				   "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
10813 				   token);
10814 			return -1;
10815 		}
10816 	}
10817 
10818 	if (!type) {
10819 		wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
10820 		return -1;
10821 	}
10822 
10823 	if (enable > 1) {
10824 		wpa_printf(MSG_INFO,
10825 			   "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
10826 		return -1;
10827 	}
10828 
10829 	if (!enable)
10830 		return wpas_disable_mac_addr_randomization(wpa_s, type);
10831 
10832 	return wpas_enable_mac_addr_randomization(wpa_s, type, addr, mask);
10833 }
10834 
10835 
wpas_ctrl_iface_pmksa(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)10836 static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
10837 				 char *buf, size_t buflen)
10838 {
10839 	size_t reply_len;
10840 
10841 	reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
10842 #ifdef CONFIG_AP
10843 	reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
10844 					      buflen - reply_len);
10845 #endif /* CONFIG_AP */
10846 	return reply_len;
10847 }
10848 
10849 
wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant * wpa_s)10850 static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
10851 {
10852 	ptksa_cache_flush(wpa_s->ptksa, NULL, WPA_CIPHER_NONE);
10853 	wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
10854 #ifdef CONFIG_AP
10855 	wpas_ap_pmksa_cache_flush(wpa_s);
10856 #endif /* CONFIG_AP */
10857 }
10858 
10859 
10860 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
10861 
wpas_ctrl_iface_pmksa_get(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)10862 static int wpas_ctrl_iface_pmksa_get(struct wpa_supplicant *wpa_s,
10863 				     const char *cmd, char *buf, size_t buflen)
10864 {
10865 	struct rsn_pmksa_cache_entry *entry;
10866 	struct wpa_ssid *ssid;
10867 	char *pos, *pos2, *end;
10868 	int ret;
10869 	struct os_reltime now;
10870 
10871 	ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
10872 	if (!ssid)
10873 		return -1;
10874 
10875 	pos = buf;
10876 	end = buf + buflen;
10877 
10878 	os_get_reltime(&now);
10879 
10880 	/*
10881 	 * Entry format:
10882 	 * <BSSID> <PMKID> <PMK> <reauth_time in seconds>
10883 	 * <expiration in seconds> <akmp> <opportunistic>
10884 	 * [FILS Cache Identifier]
10885 	 */
10886 
10887 	for (entry = wpa_sm_pmksa_cache_head(wpa_s->wpa); entry;
10888 	     entry = entry->next) {
10889 		if (entry->network_ctx != ssid)
10890 			continue;
10891 
10892 		pos2 = pos;
10893 		ret = os_snprintf(pos2, end - pos2, MACSTR " ",
10894 				  MAC2STR(entry->aa));
10895 		if (os_snprintf_error(end - pos2, ret))
10896 			break;
10897 		pos2 += ret;
10898 
10899 		pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmkid,
10900 					 PMKID_LEN);
10901 
10902 		ret = os_snprintf(pos2, end - pos2, " ");
10903 		if (os_snprintf_error(end - pos2, ret))
10904 			break;
10905 		pos2 += ret;
10906 
10907 		pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmk,
10908 					 entry->pmk_len);
10909 
10910 		ret = os_snprintf(pos2, end - pos2, " %d %d %d %d",
10911 				  (int) (entry->reauth_time - now.sec),
10912 				  (int) (entry->expiration - now.sec),
10913 				  entry->akmp,
10914 				  entry->opportunistic);
10915 		if (os_snprintf_error(end - pos2, ret))
10916 			break;
10917 		pos2 += ret;
10918 
10919 		if (entry->fils_cache_id_set) {
10920 			ret = os_snprintf(pos2, end - pos2, " %02x%02x",
10921 					  entry->fils_cache_id[0],
10922 					  entry->fils_cache_id[1]);
10923 			if (os_snprintf_error(end - pos2, ret))
10924 				break;
10925 			pos2 += ret;
10926 		}
10927 
10928 		ret = os_snprintf(pos2, end - pos2, "\n");
10929 		if (os_snprintf_error(end - pos2, ret))
10930 			break;
10931 		pos2 += ret;
10932 
10933 		pos = pos2;
10934 	}
10935 
10936 	return pos - buf;
10937 }
10938 
10939 
wpas_ctrl_iface_pmksa_add(struct wpa_supplicant * wpa_s,char * cmd)10940 static int wpas_ctrl_iface_pmksa_add(struct wpa_supplicant *wpa_s,
10941 				     char *cmd)
10942 {
10943 	struct rsn_pmksa_cache_entry *entry;
10944 	struct wpa_ssid *ssid;
10945 	char *pos, *pos2;
10946 	int ret = -1;
10947 	struct os_reltime now;
10948 	int reauth_time = 0, expiration = 0, i;
10949 
10950 	/*
10951 	 * Entry format:
10952 	 * <network_id> <BSSID> <PMKID> <PMK> <reauth_time in seconds>
10953 	 * <expiration in seconds> <akmp> <opportunistic>
10954 	 * [FILS Cache Identifier]
10955 	 */
10956 
10957 	ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
10958 	if (!ssid)
10959 		return -1;
10960 
10961 	pos = os_strchr(cmd, ' ');
10962 	if (!pos)
10963 		return -1;
10964 	pos++;
10965 
10966 	entry = os_zalloc(sizeof(*entry));
10967 	if (!entry)
10968 		return -1;
10969 
10970 	if (hwaddr_aton(pos, entry->aa))
10971 		goto fail;
10972 
10973 	pos = os_strchr(pos, ' ');
10974 	if (!pos)
10975 		goto fail;
10976 	pos++;
10977 
10978 	if (hexstr2bin(pos, entry->pmkid, PMKID_LEN) < 0)
10979 		goto fail;
10980 
10981 	pos = os_strchr(pos, ' ');
10982 	if (!pos)
10983 		goto fail;
10984 	pos++;
10985 
10986 	pos2 = os_strchr(pos, ' ');
10987 	if (!pos2)
10988 		goto fail;
10989 	entry->pmk_len = (pos2 - pos) / 2;
10990 	if (entry->pmk_len < PMK_LEN || entry->pmk_len > PMK_LEN_MAX ||
10991 	    hexstr2bin(pos, entry->pmk, entry->pmk_len) < 0)
10992 		goto fail;
10993 
10994 	pos = os_strchr(pos, ' ');
10995 	if (!pos)
10996 		goto fail;
10997 	pos++;
10998 
10999 	if (sscanf(pos, "%d %d %d %d", &reauth_time, &expiration,
11000 		   &entry->akmp, &entry->opportunistic) != 4)
11001 		goto fail;
11002 	if (reauth_time > expiration)
11003 		goto fail;
11004 	for (i = 0; i < 4; i++) {
11005 		pos = os_strchr(pos, ' ');
11006 		if (!pos) {
11007 			if (i < 3)
11008 				goto fail;
11009 			break;
11010 		}
11011 		pos++;
11012 	}
11013 	if (pos) {
11014 		if (hexstr2bin(pos, entry->fils_cache_id,
11015 			       FILS_CACHE_ID_LEN) < 0)
11016 			goto fail;
11017 		entry->fils_cache_id_set = 1;
11018 	}
11019 	os_get_reltime(&now);
11020 	entry->expiration = now.sec + expiration;
11021 	entry->reauth_time = now.sec + reauth_time;
11022 
11023 	entry->network_ctx = ssid;
11024 	os_memcpy(entry->spa, wpa_s->own_addr, ETH_ALEN);
11025 
11026 	entry->external = true;
11027 
11028 	wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
11029 	entry = NULL;
11030 	ret = 0;
11031 fail:
11032 	os_free(entry);
11033 	return ret;
11034 }
11035 
11036 
11037 #ifdef CONFIG_MESH
11038 
wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)11039 static int wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant *wpa_s,
11040 					  const char *cmd, char *buf,
11041 					  size_t buflen)
11042 {
11043 	u8 spa[ETH_ALEN];
11044 
11045 	if (!wpa_s->ifmsh)
11046 		return -1;
11047 
11048 	if (os_strcasecmp(cmd, "any") == 0)
11049 		return wpas_ap_pmksa_cache_list_mesh(wpa_s, NULL, buf, buflen);
11050 
11051 	if (hwaddr_aton(cmd, spa))
11052 		return -1;
11053 
11054 	return wpas_ap_pmksa_cache_list_mesh(wpa_s, spa, buf, buflen);
11055 }
11056 
11057 
wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant * wpa_s,char * cmd)11058 static int wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant *wpa_s,
11059 					  char *cmd)
11060 {
11061 	/*
11062 	 * We do not check mesh interface existence because PMKSA should be
11063 	 * stored before wpa_s->ifmsh creation to suppress commit message
11064 	 * creation.
11065 	 */
11066 	return wpas_ap_pmksa_cache_add_external(wpa_s, cmd);
11067 }
11068 
11069 #endif /* CONFIG_MESH */
11070 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
11071 
11072 
11073 #ifdef CONFIG_FILS
wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant * wpa_s,const char * cmd)11074 static int wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant *wpa_s,
11075 					    const char *cmd)
11076 {
11077 	struct fils_hlp_req *req;
11078 	const char *pos;
11079 
11080 	/* format: <dst> <packet starting from ethertype> */
11081 
11082 	req = os_zalloc(sizeof(*req));
11083 	if (!req)
11084 		return -1;
11085 
11086 	if (hwaddr_aton(cmd, req->dst))
11087 		goto fail;
11088 
11089 	pos = os_strchr(cmd, ' ');
11090 	if (!pos)
11091 		goto fail;
11092 	pos++;
11093 	req->pkt = wpabuf_parse_bin(pos);
11094 	if (!req->pkt)
11095 		goto fail;
11096 
11097 	dl_list_add_tail(&wpa_s->fils_hlp_req, &req->list);
11098 	return 0;
11099 fail:
11100 	wpabuf_free(req->pkt);
11101 	os_free(req);
11102 	return -1;
11103 }
11104 #endif /* CONFIG_FILS */
11105 
11106 
wpas_ctrl_cmd_debug_level(const char * cmd)11107 int wpas_ctrl_cmd_debug_level(const char *cmd)
11108 {
11109 	if (os_strcmp(cmd, "PING") == 0 ||
11110 	    os_strncmp(cmd, "BSS ", 4) == 0 ||
11111 	    os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
11112 	    os_strncmp(cmd, "STATUS", 6) == 0 ||
11113 	    os_strncmp(cmd, "STA ", 4) == 0 ||
11114 	    os_strncmp(cmd, "STA-", 4) == 0)
11115 		return MSG_EXCESSIVE;
11116 	return MSG_DEBUG;
11117 }
11118 
11119 
11120 #ifndef CONFIG_NO_ROBUST_AV
wpas_ctrl_iface_configure_mscs(struct wpa_supplicant * wpa_s,const char * cmd)11121 static int wpas_ctrl_iface_configure_mscs(struct wpa_supplicant *wpa_s,
11122 					  const char *cmd)
11123 {
11124 	size_t frame_classifier_len;
11125 	const char *pos, *end;
11126 	struct robust_av_data *robust_av = &wpa_s->robust_av;
11127 	int val;
11128 
11129 	/*
11130 	 * format:
11131 	 * <add|remove|change> [up_bitmap=<hex byte>] [up_limit=<integer>]
11132 	 * [stream_timeout=<in TUs>] [frame_classifier=<hex bytes>]
11133 	 */
11134 	os_memset(robust_av, 0, sizeof(struct robust_av_data));
11135 	if (os_strncmp(cmd, "add ", 4) == 0) {
11136 		robust_av->request_type = SCS_REQ_ADD;
11137 	} else if (os_strcmp(cmd, "remove") == 0) {
11138 		robust_av->request_type = SCS_REQ_REMOVE;
11139 		robust_av->valid_config = false;
11140 		return wpas_send_mscs_req(wpa_s);
11141 	} else if (os_strncmp(cmd, "change ", 7) == 0) {
11142 		robust_av->request_type = SCS_REQ_CHANGE;
11143 	} else {
11144 		return -1;
11145 	}
11146 
11147 	pos = os_strstr(cmd, "up_bitmap=");
11148 	if (!pos)
11149 		return -1;
11150 
11151 	val = hex2byte(pos + 10);
11152 	if (val < 0)
11153 		return -1;
11154 	robust_av->up_bitmap = val;
11155 
11156 	pos = os_strstr(cmd, "up_limit=");
11157 	if (!pos)
11158 		return -1;
11159 
11160 	robust_av->up_limit = atoi(pos + 9);
11161 
11162 	pos = os_strstr(cmd, "stream_timeout=");
11163 	if (!pos)
11164 		return -1;
11165 
11166 	robust_av->stream_timeout = atoi(pos + 15);
11167 	if (robust_av->stream_timeout == 0)
11168 		return -1;
11169 
11170 	pos = os_strstr(cmd, "frame_classifier=");
11171 	if (!pos)
11172 		return -1;
11173 
11174 	pos += 17;
11175 	end = os_strchr(pos, ' ');
11176 	if (!end)
11177 		end = pos + os_strlen(pos);
11178 
11179 	frame_classifier_len = (end - pos) / 2;
11180 	if (frame_classifier_len > sizeof(robust_av->frame_classifier) ||
11181 	    hexstr2bin(pos, robust_av->frame_classifier, frame_classifier_len))
11182 		return -1;
11183 
11184 	robust_av->frame_classifier_len = frame_classifier_len;
11185 	robust_av->valid_config = true;
11186 
11187 	return wpas_send_mscs_req(wpa_s);
11188 }
11189 #endif /* CONFIG_NO_ROBUST_AV */
11190 
11191 
11192 #ifdef CONFIG_PASN
wpas_ctrl_iface_pasn_start(struct wpa_supplicant * wpa_s,char * cmd)11193 static int wpas_ctrl_iface_pasn_start(struct wpa_supplicant *wpa_s, char *cmd)
11194 {
11195 	char *token, *context = NULL;
11196 	u8 bssid[ETH_ALEN];
11197 	int akmp = -1, cipher = -1, got_bssid = 0;
11198 	u16 group = 0xFFFF;
11199 	u8 *comeback = NULL;
11200 	size_t comeback_len = 0;
11201 	int id = 0, ret = -1;
11202 
11203 	/*
11204 	 * Entry format: bssid=<BSSID> akmp=<AKMP> cipher=<CIPHER> group=<group>
11205 	 *    [comeback=<hexdump>]
11206 	 */
11207 	while ((token = str_token(cmd, " ", &context))) {
11208 		if (os_strncmp(token, "bssid=", 6) == 0) {
11209 			if (hwaddr_aton(token + 6, bssid))
11210 				goto out;
11211 			got_bssid = 1;
11212 		} else if (os_strcmp(token, "akmp=PASN") == 0) {
11213 			akmp = WPA_KEY_MGMT_PASN;
11214 #ifdef CONFIG_IEEE80211R
11215 		} else if (os_strcmp(token, "akmp=FT-PSK") == 0) {
11216 			akmp = WPA_KEY_MGMT_FT_PSK;
11217 		} else if (os_strcmp(token, "akmp=FT-EAP-SHA384") == 0) {
11218 			akmp = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
11219 		} else if (os_strcmp(token, "akmp=FT-EAP") == 0) {
11220 			akmp = WPA_KEY_MGMT_FT_IEEE8021X;
11221 #endif /* CONFIG_IEEE80211R */
11222 #ifdef CONFIG_SAE
11223 		} else if (os_strcmp(token, "akmp=SAE") == 0) {
11224 			akmp = WPA_KEY_MGMT_SAE;
11225 		} else if (os_strcmp(token, "akmp=SAE-EXT-KEY") == 0) {
11226 			akmp = WPA_KEY_MGMT_SAE_EXT_KEY;
11227 #endif /* CONFIG_SAE */
11228 #ifdef CONFIG_FILS
11229 		} else if (os_strcmp(token, "akmp=FILS-SHA256") == 0) {
11230 			akmp = WPA_KEY_MGMT_FILS_SHA256;
11231 		} else if (os_strcmp(token, "akmp=FILS-SHA384") == 0) {
11232 			akmp = WPA_KEY_MGMT_FILS_SHA384;
11233 #endif /* CONFIG_FILS */
11234 		} else if (os_strcmp(token, "cipher=CCMP-256") == 0) {
11235 			cipher = WPA_CIPHER_CCMP_256;
11236 		} else if (os_strcmp(token, "cipher=GCMP-256") == 0) {
11237 			cipher = WPA_CIPHER_GCMP_256;
11238 		} else if (os_strcmp(token, "cipher=CCMP") == 0) {
11239 			cipher = WPA_CIPHER_CCMP;
11240 		} else if (os_strcmp(token, "cipher=GCMP") == 0) {
11241 			cipher = WPA_CIPHER_GCMP;
11242 		} else if (os_strncmp(token, "group=", 6) == 0) {
11243 			group = atoi(token + 6);
11244 		} else if (os_strncmp(token, "nid=", 4) == 0) {
11245 			id = atoi(token + 4);
11246 		} else if (os_strncmp(token, "comeback=", 9) == 0) {
11247 			comeback_len = os_strlen(token + 9);
11248 			if (comeback || !comeback_len || comeback_len % 2)
11249 				goto out;
11250 
11251 			comeback_len /= 2;
11252 			comeback = os_malloc(comeback_len);
11253 			if (!comeback ||
11254 			    hexstr2bin(token + 9, comeback, comeback_len))
11255 				goto out;
11256 		} else {
11257 			wpa_printf(MSG_DEBUG,
11258 				   "CTRL: PASN Invalid parameter: '%s'",
11259 				   token);
11260 			goto out;
11261 		}
11262 	}
11263 
11264 	if (!got_bssid || akmp == -1 || cipher == -1 || group == 0xFFFF) {
11265 		wpa_printf(MSG_DEBUG,"CTRL: PASN missing parameter");
11266 		goto out;
11267 	}
11268 
11269 	ret = wpas_pasn_auth_start(wpa_s, wpa_s->own_addr, bssid, akmp, cipher,
11270 				   group, id, comeback, comeback_len);
11271 out:
11272 	os_free(comeback);
11273 	return ret;
11274 }
11275 
11276 
wpas_ctrl_iface_pasn_deauthenticate(struct wpa_supplicant * wpa_s,const char * cmd)11277 static int wpas_ctrl_iface_pasn_deauthenticate(struct wpa_supplicant *wpa_s,
11278 					       const char *cmd)
11279 {
11280 	u8 bssid[ETH_ALEN];
11281 
11282 	if (os_strncmp(cmd, "bssid=", 6) != 0 || hwaddr_aton(cmd + 6, bssid)) {
11283 		wpa_printf(MSG_DEBUG,
11284 			   "CTRL: PASN_DEAUTH without valid BSSID");
11285 		return -1;
11286 	}
11287 
11288 	return wpas_pasn_deauthenticate(wpa_s, wpa_s->own_addr, bssid);
11289 }
11290 
11291 
11292 #ifdef CONFIG_TESTING_OPTIONS
wpas_ctrl_iface_pasn_driver(struct wpa_supplicant * wpa_s,const char * cmd)11293 static int wpas_ctrl_iface_pasn_driver(struct wpa_supplicant *wpa_s,
11294 				       const char *cmd)
11295 {
11296 	union wpa_event_data event;
11297 	const char *pos = cmd;
11298 	u8 addr[ETH_ALEN];
11299 
11300 	os_memset(&event, 0, sizeof(event));
11301 
11302 	if (os_strncmp(pos, "auth ", 5) == 0)
11303 		event.pasn_auth.action = PASN_ACTION_AUTH;
11304 	else if (os_strncmp(pos, "del ", 4) == 0)
11305 		event.pasn_auth.action =
11306 			PASN_ACTION_DELETE_SECURE_RANGING_CONTEXT;
11307 	else
11308 		return -1;
11309 
11310 	pos = os_strchr(pos, ' ');
11311 	if (!pos)
11312 		return -1;
11313 	pos++;
11314 	while (hwaddr_aton(pos, addr) == 0) {
11315 		struct pasn_peer *peer;
11316 
11317 		if (event.pasn_auth.num_peers == WPAS_MAX_PASN_PEERS)
11318 			return -1;
11319 		peer = &event.pasn_auth.peer[event.pasn_auth.num_peers];
11320 		os_memcpy(peer->own_addr, wpa_s->own_addr, ETH_ALEN);
11321 		os_memcpy(peer->peer_addr, addr, ETH_ALEN);
11322 		event.pasn_auth.num_peers++;
11323 
11324 		pos = os_strchr(pos, ' ');
11325 		if (!pos)
11326 			break;
11327 		pos++;
11328 	}
11329 
11330 	wpa_supplicant_event(wpa_s, EVENT_PASN_AUTH, &event);
11331 	return 0;
11332 }
11333 #endif /* CONFIG_TESTING_OPTIONS */
11334 
11335 #endif /* CONFIG_PASN */
11336 
11337 
11338 #ifndef CONFIG_NO_ROBUST_AV
11339 
set_type4_frame_classifier(const char * cmd,struct type4_params * param)11340 static int set_type4_frame_classifier(const char *cmd,
11341 				      struct type4_params *param)
11342 {
11343 	const char *pos, *end;
11344 	u8 classifier_mask = 0;
11345 	int ret;
11346 	char addr[INET6_ADDRSTRLEN];
11347 	size_t alen;
11348 
11349 	if (os_strstr(cmd, "ip_version=ipv4")) {
11350 		param->ip_version = IPV4;
11351 	} else if (os_strstr(cmd, "ip_version=ipv6")) {
11352 		param->ip_version = IPV6;
11353 	} else {
11354 		wpa_printf(MSG_ERROR, "IP version missing/invalid");
11355 		return -1;
11356 	}
11357 
11358 	classifier_mask |= BIT(0);
11359 
11360 	pos = os_strstr(cmd, "src_ip=");
11361 	if (pos) {
11362 		pos += 7;
11363 		end = os_strchr(pos, ' ');
11364 		if (!end)
11365 			end = pos + os_strlen(pos);
11366 
11367 		alen = end - pos;
11368 		if (alen >= INET6_ADDRSTRLEN)
11369 			return -1;
11370 		os_memcpy(addr, pos, alen);
11371 		addr[alen] = '\0';
11372 		if (param->ip_version == IPV4)
11373 			ret = inet_pton(AF_INET, addr,
11374 					&param->ip_params.v4.src_ip);
11375 		else
11376 			ret = inet_pton(AF_INET6, addr,
11377 					&param->ip_params.v6.src_ip);
11378 
11379 		if (ret != 1) {
11380 			wpa_printf(MSG_ERROR,
11381 				   "Error converting src IP address to binary ret=%d",
11382 				   ret);
11383 			return -1;
11384 		}
11385 
11386 		classifier_mask |= BIT(1);
11387 	}
11388 
11389 	pos = os_strstr(cmd, "dst_ip=");
11390 	if (pos) {
11391 		pos += 7;
11392 		end = os_strchr(pos, ' ');
11393 		if (!end)
11394 			end = pos + os_strlen(pos);
11395 
11396 		alen = end - pos;
11397 		if (alen >= INET6_ADDRSTRLEN)
11398 			return -1;
11399 		os_memcpy(addr, pos, alen);
11400 		addr[alen] = '\0';
11401 		if (param->ip_version == IPV4)
11402 			ret = inet_pton(AF_INET, addr,
11403 					&param->ip_params.v4.dst_ip);
11404 		else
11405 			ret = inet_pton(AF_INET6, addr,
11406 					&param->ip_params.v6.dst_ip);
11407 
11408 		if (ret != 1) {
11409 			wpa_printf(MSG_ERROR,
11410 				   "Error converting dst IP address to binary ret=%d",
11411 				   ret);
11412 			return -1;
11413 		}
11414 
11415 		classifier_mask |= BIT(2);
11416 	}
11417 
11418 	pos = os_strstr(cmd, "src_port=");
11419 	if (pos && atoi(pos + 9) > 0) {
11420 		if (param->ip_version == IPV4)
11421 			param->ip_params.v4.src_port = atoi(pos + 9);
11422 		else
11423 			param->ip_params.v6.src_port = atoi(pos + 9);
11424 		classifier_mask |= BIT(3);
11425 	}
11426 
11427 	pos = os_strstr(cmd, "dst_port=");
11428 	if (pos && atoi(pos + 9) > 0) {
11429 		if (param->ip_version == IPV4)
11430 			param->ip_params.v4.dst_port = atoi(pos + 9);
11431 		else
11432 			param->ip_params.v6.dst_port = atoi(pos + 9);
11433 		classifier_mask |= BIT(4);
11434 	}
11435 
11436 	pos = os_strstr(cmd, "dscp=");
11437 	if (pos && atoi(pos + 5) > 0) {
11438 		if (param->ip_version == IPV4)
11439 			param->ip_params.v4.dscp = atoi(pos + 5);
11440 		else
11441 			param->ip_params.v6.dscp = atoi(pos + 5);
11442 		classifier_mask |= BIT(5);
11443 	}
11444 
11445 	if (param->ip_version == IPV4) {
11446 		pos = os_strstr(cmd, "protocol=");
11447 		if (pos) {
11448 			if (os_strstr(pos, "udp")) {
11449 				param->ip_params.v4.protocol = 17;
11450 			} else if (os_strstr(pos, "tcp")) {
11451 				param->ip_params.v4.protocol = 6;
11452 			} else if (os_strstr(pos, "esp")) {
11453 				param->ip_params.v4.protocol = 50;
11454 			} else {
11455 				wpa_printf(MSG_ERROR, "Invalid protocol");
11456 				return -1;
11457 			}
11458 			classifier_mask |= BIT(6);
11459 		}
11460 	} else {
11461 		pos = os_strstr(cmd, "next_header=");
11462 		if (pos) {
11463 			if (os_strstr(pos, "udp")) {
11464 				param->ip_params.v6.next_header = 17;
11465 			} else if (os_strstr(pos, "tcp")) {
11466 				param->ip_params.v6.next_header = 6;
11467 			} else if (os_strstr(pos, "esp")) {
11468 				param->ip_params.v6.next_header = 50;
11469 			} else {
11470 				wpa_printf(MSG_ERROR, "Invalid next header");
11471 				return -1;
11472 			}
11473 
11474 			classifier_mask |= BIT(6);
11475 		}
11476 
11477 		pos = os_strstr(cmd, "flow_label=");
11478 		if (pos) {
11479 			pos += 11;
11480 			end = os_strchr(pos, ' ');
11481 			if (!end)
11482 				end = pos + os_strlen(pos);
11483 
11484 			if (end - pos != 6 ||
11485 			    hexstr2bin(pos, param->ip_params.v6.flow_label,
11486 				       3) ||
11487 			    param->ip_params.v6.flow_label[0] > 0x0F) {
11488 				wpa_printf(MSG_ERROR, "Invalid flow label");
11489 				return -1;
11490 			}
11491 
11492 			classifier_mask |= BIT(7);
11493 		}
11494 	}
11495 
11496 	param->classifier_mask = classifier_mask;
11497 	return 0;
11498 }
11499 
11500 
set_type10_frame_classifier(const char * cmd,struct type10_params * param)11501 static int set_type10_frame_classifier(const char *cmd,
11502 				       struct type10_params *param)
11503 {
11504 	const char *pos, *end;
11505 	size_t filter_len;
11506 
11507 	pos = os_strstr(cmd, "prot_instance=");
11508 	if (!pos) {
11509 		wpa_printf(MSG_ERROR, "Protocol instance missing");
11510 		return -1;
11511 	}
11512 	param->prot_instance = atoi(pos + 14);
11513 
11514 	pos = os_strstr(cmd, "prot_number=");
11515 	if (!pos) {
11516 		wpa_printf(MSG_ERROR, "Protocol number missing");
11517 		return -1;
11518 	}
11519 	if (os_strstr(pos, "udp")) {
11520 		param->prot_number = 17;
11521 	} else if (os_strstr(pos, "tcp")) {
11522 		param->prot_number = 6;
11523 	} else if (os_strstr(pos, "esp")) {
11524 		param->prot_number = 50;
11525 	} else {
11526 		wpa_printf(MSG_ERROR, "Invalid protocol number");
11527 		return -1;
11528 	}
11529 
11530 	pos = os_strstr(cmd, "filter_value=");
11531 	if (!pos) {
11532 		wpa_printf(MSG_ERROR,
11533 			   "Classifier parameter filter_value missing");
11534 		return -1;
11535 	}
11536 
11537 	pos += 13;
11538 	end = os_strchr(pos, ' ');
11539 	if (!end)
11540 		end = pos + os_strlen(pos);
11541 
11542 	filter_len = (end - pos) / 2;
11543 	param->filter_value = os_malloc(filter_len);
11544 	if (!param->filter_value)
11545 		return -1;
11546 
11547 	if (hexstr2bin(pos, param->filter_value, filter_len)) {
11548 		wpa_printf(MSG_ERROR, "Invalid filter_value %s", pos);
11549 		goto free;
11550 	}
11551 
11552 	pos = os_strstr(cmd, "filter_mask=");
11553 	if (!pos) {
11554 		wpa_printf(MSG_ERROR,
11555 			   "Classifier parameter filter_mask missing");
11556 		goto free;
11557 	}
11558 
11559 	pos += 12;
11560 	end = os_strchr(pos, ' ');
11561 	if (!end)
11562 		end = pos + os_strlen(pos);
11563 
11564 	if (filter_len != (size_t) (end - pos) / 2) {
11565 		wpa_printf(MSG_ERROR,
11566 			   "Filter mask length mismatch expected=%zu received=%zu",
11567 			   filter_len, (size_t) (end - pos) / 2);
11568 		goto free;
11569 	}
11570 
11571 	param->filter_mask = os_malloc(filter_len);
11572 	if (!param->filter_mask)
11573 		goto free;
11574 
11575 	if (hexstr2bin(pos, param->filter_mask, filter_len)) {
11576 		wpa_printf(MSG_ERROR, "Invalid filter mask %s", pos);
11577 		os_free(param->filter_mask);
11578 		param->filter_mask = NULL;
11579 		goto free;
11580 	}
11581 
11582 	param->filter_len = filter_len;
11583 	return 0;
11584 free:
11585 	os_free(param->filter_value);
11586 	param->filter_value = NULL;
11587 	return -1;
11588 }
11589 
11590 
scs_parse_type4(struct tclas_element * elem,const char * pos)11591 static int scs_parse_type4(struct tclas_element *elem, const char *pos)
11592 {
11593 	struct type4_params type4_param = { 0 };
11594 
11595 	if (set_type4_frame_classifier(pos, &type4_param) == -1) {
11596 		wpa_printf(MSG_ERROR, "Failed to set frame_classifier 4");
11597 		return -1;
11598 	}
11599 
11600 	os_memcpy(&elem->frame_classifier.type4_param,
11601 		  &type4_param, sizeof(struct type4_params));
11602 	return 0;
11603 }
11604 
11605 
scs_parse_type10(struct tclas_element * elem,const char * pos)11606 static int scs_parse_type10(struct tclas_element *elem, const char *pos)
11607 {
11608 	struct type10_params type10_param = { 0 };
11609 
11610 	if (set_type10_frame_classifier(pos, &type10_param) == -1) {
11611 		wpa_printf(MSG_ERROR, "Failed to set frame_classifier 10");
11612 		return -1;
11613 	}
11614 
11615 	os_memcpy(&elem->frame_classifier.type10_param,
11616 		  &type10_param, sizeof(struct type10_params));
11617 	return 0;
11618 }
11619 
11620 
wpas_ctrl_iface_configure_scs(struct wpa_supplicant * wpa_s,char * cmd)11621 static int wpas_ctrl_iface_configure_scs(struct wpa_supplicant *wpa_s,
11622 					 char *cmd)
11623 {
11624 	char *pos1, *pos;
11625 	struct scs_robust_av_data *scs_data = &wpa_s->scs_robust_av_req;
11626 	struct scs_desc_elem desc_elem = { 0 };
11627 	int val;
11628 	unsigned int num_scs_desc = 0;
11629 
11630 	if (wpa_s->ongoing_scs_req) {
11631 		wpa_printf(MSG_ERROR, "%s: SCS Request already in queue",
11632 			   __func__);
11633 		return -1;
11634 	}
11635 
11636 	/**
11637 	 * format:
11638 	 * [scs_id=<decimal number>] <add|remove|change> [scs_up=<0-7>]
11639 	 * [classifier_type=<4|10>]
11640 	 * [classifier params based on classifier type]
11641 	 * [tclas_processing=<0|1>]
11642 	 * [qos_characteristics] <up/down/direct> [min_si=<decimal number>]
11643 	 * [max_si=<decimal number>] [min_data_rate=<decimal number>]
11644 	 * [delay_bound=<decimal number>] [max_msdu=<decimal number>]
11645 	 * [service_start_time=<decimal number>]
11646 	 * [service_start_time_link_id=<decimal number>]
11647 	 * [mean_data_rate=<decimal number>] [burst_size=<decimal number>]
11648 	 * [msdu_lifetime=<decimal number>]
11649 	 * [msdu_delivery_info=<decimal number>] [medium_time=<decimal number>]
11650 	 * [scs_id=<decimal number>] ...
11651 	 */
11652 	pos1 = os_strstr(cmd, "scs_id=");
11653 	if (!pos1) {
11654 		wpa_printf(MSG_ERROR, "SCSID not present");
11655 		return -1;
11656 	}
11657 
11658 	free_up_scs_desc(scs_data);
11659 
11660 	while (pos1) {
11661 		struct scs_desc_elem *n1;
11662 		struct active_scs_elem *active_scs_desc;
11663 		char *next_scs_desc, *pos2;
11664 		unsigned int num_tclas_elem = 0;
11665 		bool scsid_active = false, tclas_present = false;
11666 		struct qos_characteristics *qos_elem = &desc_elem.qos_char_elem;
11667 
11668 		desc_elem.scs_id = atoi(pos1 + 7);
11669 		pos1 += 7;
11670 
11671 		next_scs_desc = os_strstr(pos1, "scs_id=");
11672 		if (next_scs_desc) {
11673 			char temp[20];
11674 
11675 			os_snprintf(temp, sizeof(temp), "scs_id=%d ",
11676 				    desc_elem.scs_id);
11677 			if (os_strstr(next_scs_desc, temp)) {
11678 				wpa_printf(MSG_ERROR,
11679 					   "Multiple SCS descriptors configured with same SCSID(=%d)",
11680 					   desc_elem.scs_id);
11681 				goto free_scs_desc;
11682 			}
11683 			pos1[next_scs_desc - pos1 - 1] = '\0';
11684 		}
11685 
11686 		dl_list_for_each(active_scs_desc, &wpa_s->active_scs_ids,
11687 				 struct active_scs_elem, list) {
11688 			if (desc_elem.scs_id == active_scs_desc->scs_id) {
11689 				scsid_active = true;
11690 				break;
11691 			}
11692 		}
11693 
11694 		if (os_strstr(pos1, "add ")) {
11695 			desc_elem.request_type = SCS_REQ_ADD;
11696 			if (scsid_active) {
11697 				wpa_printf(MSG_ERROR, "SCSID %d already active",
11698 					   desc_elem.scs_id);
11699 				return -1;
11700 			}
11701 		} else if (os_strstr(pos1, "remove")) {
11702 			desc_elem.request_type = SCS_REQ_REMOVE;
11703 			if (!scsid_active) {
11704 				wpa_printf(MSG_ERROR, "SCSID %d not active",
11705 					   desc_elem.scs_id);
11706 				return -1;
11707 			}
11708 			goto scs_desc_end;
11709 		} else if (os_strstr(pos1, "change ")) {
11710 			desc_elem.request_type = SCS_REQ_CHANGE;
11711 			if (!scsid_active) {
11712 				wpa_printf(MSG_ERROR, "SCSID %d not active",
11713 					   desc_elem.scs_id);
11714 				return -1;
11715 			}
11716 		} else {
11717 			wpa_printf(MSG_ERROR, "SCS Request type invalid");
11718 			goto free_scs_desc;
11719 		}
11720 
11721 		pos1 = os_strstr(pos1, "scs_up=");
11722 		if (!pos1) {
11723 			wpa_printf(MSG_ERROR,
11724 				   "Intra-Access user priority not present");
11725 			goto free_scs_desc;
11726 		}
11727 
11728 		val = atoi(pos1 + 7);
11729 		if (val < 0 || val > 7) {
11730 			wpa_printf(MSG_ERROR,
11731 				   "Intra-Access user priority invalid %d",
11732 				   val);
11733 			goto free_scs_desc;
11734 		}
11735 
11736 		desc_elem.intra_access_priority = val;
11737 		desc_elem.scs_up_avail = true;
11738 
11739 		pos = os_strstr(pos1, "classifier_type=");
11740 		if (!pos) {
11741 			wpa_printf(MSG_ERROR, "classifier type empty");
11742 			goto qos_characteristics;
11743 		}
11744 		tclas_present = true;
11745 
11746 		while (pos) {
11747 			struct tclas_element elem = { 0 }, *n;
11748 			char *next_tclas_elem;
11749 
11750 			val = atoi(pos + 16);
11751 			if (val != 4 && val != 10) {
11752 				wpa_printf(MSG_ERROR,
11753 					   "classifier type invalid %d", val);
11754 				goto free_scs_desc;
11755 			}
11756 
11757 			elem.classifier_type = val;
11758 			pos += 16;
11759 
11760 			next_tclas_elem = os_strstr(pos, "classifier_type=");
11761 			if (next_tclas_elem) {
11762 				pos1 = next_tclas_elem;
11763 				pos[next_tclas_elem - pos - 1] = '\0';
11764 			}
11765 
11766 			switch (val) {
11767 			case 4:
11768 				if (scs_parse_type4(&elem, pos) < 0)
11769 					goto free_scs_desc;
11770 				break;
11771 			case 10:
11772 				if (scs_parse_type10(&elem, pos) < 0)
11773 					goto free_scs_desc;
11774 				break;
11775 			}
11776 
11777 			n = os_realloc(desc_elem.tclas_elems,
11778 				       (num_tclas_elem + 1) * sizeof(elem));
11779 			if (!n)
11780 				goto free_scs_desc;
11781 
11782 			desc_elem.tclas_elems = n;
11783 			os_memcpy((u8 *) desc_elem.tclas_elems +
11784 				  num_tclas_elem * sizeof(elem),
11785 				  &elem, sizeof(elem));
11786 			num_tclas_elem++;
11787 			desc_elem.num_tclas_elem = num_tclas_elem;
11788 			pos = next_tclas_elem;
11789 		}
11790 
11791 		if (desc_elem.num_tclas_elem > 1) {
11792 			pos1 = os_strstr(pos1, "tclas_processing=");
11793 			if (!pos1) {
11794 				wpa_printf(MSG_ERROR, "tclas_processing empty");
11795 				goto free_scs_desc;
11796 			}
11797 
11798 			val = atoi(pos1 + 17);
11799 			if (val != 0 && val != 1) {
11800 				wpa_printf(MSG_ERROR,
11801 					   "tclas_processing invalid");
11802 				goto free_scs_desc;
11803 			}
11804 
11805 			desc_elem.tclas_processing = val;
11806 		}
11807 
11808 	qos_characteristics:
11809 		pos1 = os_strstr(pos1, "qos_characteristics");
11810 		if (!pos1 && !tclas_present)
11811 			goto free_scs_desc;
11812 		if (!pos1)
11813 			goto scs_desc_end;
11814 
11815 		qos_elem->available = true;
11816 		if (os_strstr(pos1, "up ")) {
11817 			qos_elem->direction = SCS_DIRECTION_UP;
11818 			if (tclas_present) {
11819 				wpa_printf(MSG_ERROR,
11820 					   "TCLAS with direction:UP not allowed");
11821 				goto free_scs_desc;
11822 			}
11823 		} else if (os_strstr(pos1, "down ")) {
11824 			qos_elem->direction = SCS_DIRECTION_DOWN;
11825 		} else if (os_strstr(pos1, "direct ")) {
11826 			qos_elem->direction = SCS_DIRECTION_DIRECT;
11827 		}
11828 
11829 		pos1 = os_strstr(pos1, "min_si=");
11830 		if (!pos1) {
11831 			wpa_printf(MSG_ERROR, "Min SI is required");
11832 			goto free_scs_desc;
11833 		}
11834 		qos_elem->min_si = atoi(pos1 + 7);
11835 
11836 		pos1 = os_strstr(pos1, "max_si=");
11837 		if (!pos1) {
11838 			wpa_printf(MSG_ERROR, "Max SI is required");
11839 			goto free_scs_desc;
11840 		}
11841 		qos_elem->max_si = atoi(pos1 + 7);
11842 
11843 		if (qos_elem->min_si && qos_elem->max_si &&
11844 		    qos_elem->max_si < qos_elem->min_si) {
11845 			wpa_printf(MSG_ERROR, "Invalid Max SI");
11846 			goto free_scs_desc;
11847 		}
11848 
11849 		pos1 = os_strstr(pos1, "min_data_rate=");
11850 		if (!pos1) {
11851 			wpa_printf(MSG_ERROR, "Min data rate is required");
11852 			goto free_scs_desc;
11853 		}
11854 		qos_elem->min_data_rate = atoi(pos1 + 14);
11855 
11856 		pos1 = os_strstr(pos1, "delay_bound=");
11857 		if (!pos1) {
11858 			wpa_printf(MSG_ERROR, "Delay Bound is required");
11859 			goto free_scs_desc;
11860 		}
11861 		qos_elem->delay_bound = atoi(pos1 + 12);
11862 
11863 		if (qos_elem->min_data_rate >= BIT(24) ||
11864 		    qos_elem->delay_bound >= BIT(24)) {
11865 			wpa_printf(MSG_ERROR,
11866 				   "Invalid min_data_rate or delay_bound");
11867 			goto free_scs_desc;
11868 		}
11869 
11870 		pos2 = os_strstr(pos1, "max_msdu=");
11871 		if (pos2) {
11872 			qos_elem->max_msdu_size = atoi(pos2 + 9);
11873 			qos_elem->mask |= SCS_QOS_BIT_MAX_MSDU_SIZE;
11874 		}
11875 
11876 		pos2 = os_strstr(pos1, "service_start_time=");
11877 		if (pos2) {
11878 			qos_elem->service_start_time = atoi(pos2 + 19);
11879 			qos_elem->mask |= SCS_QOS_BIT_SERVICE_START_TIME;
11880 		}
11881 
11882 		pos2 = os_strstr(pos1, "service_start_time_link_id=");
11883 		if (pos2) {
11884 			qos_elem->service_start_time_link_id = atoi(pos2 + 27);
11885 			qos_elem->mask |= SCS_QOS_BIT_SERVICE_START_TIME_LINKID;
11886 		}
11887 
11888 		pos2 = os_strstr(pos1, "mean_data_rate=");
11889 		if (pos2) {
11890 			qos_elem->mean_data_rate = atoi(pos2 + 15);
11891 			qos_elem->mask |= SCS_QOS_BIT_MEAN_DATA_RATE;
11892 		}
11893 
11894 		pos2 = os_strstr(pos1, "burst_size=");
11895 		if (pos2) {
11896 			qos_elem->burst_size = atoi(pos2 + 11);
11897 			qos_elem->mask |=
11898 				SCS_QOS_BIT_DELAYED_BOUNDED_BURST_SIZE;
11899 		}
11900 
11901 		pos2 = os_strstr(pos1, "msdu_lifetime=");
11902 		if (pos2) {
11903 			qos_elem->msdu_lifetime = atoi(pos2 + 14);
11904 			qos_elem->mask |= SCS_QOS_BIT_MSDU_LIFETIME;
11905 		}
11906 
11907 		pos2 = os_strstr(pos1, "msdu_delivery_info=");
11908 		if (pos2) {
11909 			qos_elem->msdu_delivery_info = atoi(pos2 + 19);
11910 			qos_elem->mask |= SCS_QOS_BIT_MSDU_DELIVERY_INFO;
11911 		}
11912 
11913 		pos2 = os_strstr(pos1, "medium_time=");
11914 		if (pos2) {
11915 			qos_elem->medium_time = atoi(pos2 + 12);
11916 			qos_elem->mask |= SCS_QOS_BIT_MEDIUM_TIME;
11917 		}
11918 
11919 scs_desc_end:
11920 		n1 = os_realloc(scs_data->scs_desc_elems, (num_scs_desc + 1) *
11921 				sizeof(struct scs_desc_elem));
11922 		if (!n1)
11923 			goto free_scs_desc;
11924 
11925 		scs_data->scs_desc_elems = n1;
11926 		os_memcpy((u8 *) scs_data->scs_desc_elems + num_scs_desc *
11927 			  sizeof(desc_elem), &desc_elem, sizeof(desc_elem));
11928 		num_scs_desc++;
11929 		scs_data->num_scs_desc = num_scs_desc;
11930 		pos1 = next_scs_desc;
11931 		os_memset(&desc_elem, 0, sizeof(desc_elem));
11932 	}
11933 
11934 	return wpas_send_scs_req(wpa_s);
11935 
11936 free_scs_desc:
11937 	free_up_tclas_elem(&desc_elem);
11938 	free_up_scs_desc(scs_data);
11939 	return -1;
11940 }
11941 
11942 
wpas_ctrl_iface_send_dscp_resp(struct wpa_supplicant * wpa_s,const char * cmd)11943 static int wpas_ctrl_iface_send_dscp_resp(struct wpa_supplicant *wpa_s,
11944 					  const char *cmd)
11945 {
11946 	char *pos;
11947 	struct dscp_policy_status *policy = NULL, *n;
11948 	int num_policies = 0, ret = -1;
11949 	struct dscp_resp_data resp_data;
11950 
11951 	/*
11952 	 * format:
11953 	 * <[reset]>/<[solicited] [policy_id=1 status=0...]> [more]
11954 	 */
11955 
11956 	os_memset(&resp_data, 0, sizeof(resp_data));
11957 
11958 	resp_data.more = os_strstr(cmd, "more") != NULL;
11959 
11960 	if (os_strstr(cmd, "reset")) {
11961 		resp_data.reset = true;
11962 		resp_data.solicited = false;
11963 		goto send_resp;
11964 	}
11965 
11966 	resp_data.solicited = os_strstr(cmd, "solicited") != NULL;
11967 
11968 	pos = os_strstr(cmd, "policy_id=");
11969 	while (pos) {
11970 		n = os_realloc(policy, (num_policies + 1) * sizeof(*policy));
11971 		if (!n)
11972 			goto fail;
11973 
11974 		policy = n;
11975 		pos += 10;
11976 		policy[num_policies].id = atoi(pos);
11977 		if (policy[num_policies].id == 0) {
11978 			wpa_printf(MSG_ERROR, "DSCP: Invalid policy id");
11979 			goto fail;
11980 		}
11981 
11982 		pos = os_strstr(pos, "status=");
11983 		if (!pos) {
11984 			wpa_printf(MSG_ERROR,
11985 				   "DSCP: Status is not found for a policy");
11986 			goto fail;
11987 		}
11988 
11989 		pos += 7;
11990 		policy[num_policies].status = atoi(pos);
11991 		num_policies++;
11992 
11993 		pos = os_strstr(pos, "policy_id");
11994 	}
11995 
11996 	resp_data.policy = policy;
11997 	resp_data.num_policies = num_policies;
11998 send_resp:
11999 	ret = wpas_send_dscp_response(wpa_s, &resp_data);
12000 	if (ret)
12001 		wpa_printf(MSG_ERROR, "DSCP: Failed to send DSCP response");
12002 fail:
12003 	os_free(policy);
12004 	return ret;
12005 }
12006 
12007 
wpas_ctrl_iface_send_dscp_query(struct wpa_supplicant * wpa_s,const char * cmd)12008 static int wpas_ctrl_iface_send_dscp_query(struct wpa_supplicant *wpa_s,
12009 					   const char *cmd)
12010 {
12011 	char *pos;
12012 
12013 	/*
12014 	 * format:
12015 	 * Wildcard DSCP query
12016 	 * <wildcard>
12017 	 *
12018 	 * DSCP query with a domain name attribute:
12019 	 * [domain_name=<string>]
12020 	 */
12021 
12022 	if (os_strstr(cmd, "wildcard")) {
12023 		wpa_printf(MSG_DEBUG, "QM: Send wildcard DSCP policy query");
12024 		return wpas_send_dscp_query(wpa_s, NULL, 0);
12025 	}
12026 
12027 	pos = os_strstr(cmd, "domain_name=");
12028 	if (!pos || !os_strlen(pos + 12)) {
12029 		wpa_printf(MSG_ERROR, "QM: Domain name not preset");
12030 		return -1;
12031 	}
12032 
12033 	return wpas_send_dscp_query(wpa_s, pos + 12, os_strlen(pos + 12));
12034 }
12035 
12036 #endif /* CONFIG_NO_ROBUST_AV */
12037 
12038 
wpas_ctrl_iface_mlo_signal_poll(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)12039 static int wpas_ctrl_iface_mlo_signal_poll(struct wpa_supplicant *wpa_s,
12040 					   char *buf, size_t buflen)
12041 {
12042 	int ret, i;
12043 	char *pos, *end;
12044 	struct wpa_mlo_signal_info mlo_si;
12045 
12046 	if (!wpa_s->valid_links)
12047 		return -1;
12048 
12049 	ret = wpa_drv_mlo_signal_poll(wpa_s, &mlo_si);
12050 	if (ret)
12051 		return -1;
12052 
12053 	pos = buf;
12054 	end = buf + buflen;
12055 
12056 	for_each_link(mlo_si.valid_links, i) {
12057 		ret = os_snprintf(pos, end - pos,
12058 				  "LINK_ID=%d\nRSSI=%d\nLINKSPEED=%lu\n"
12059 				  "NOISE=%d\nFREQUENCY=%u\n",
12060 				  i, mlo_si.links[i].data.signal,
12061 				  mlo_si.links[i].data.current_tx_rate / 1000,
12062 				  mlo_si.links[i].current_noise,
12063 				  mlo_si.links[i].frequency);
12064 		if (os_snprintf_error(end - pos, ret))
12065 			return -1;
12066 		pos += ret;
12067 
12068 		if (mlo_si.links[i].chanwidth != CHAN_WIDTH_UNKNOWN) {
12069 			ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
12070 					  channel_width_to_string(
12071 						  mlo_si.links[i].chanwidth));
12072 			if (os_snprintf_error(end - pos, ret))
12073 				return -1;
12074 			pos += ret;
12075 		}
12076 
12077 		if (mlo_si.links[i].center_frq1 > 0) {
12078 			ret = os_snprintf(pos, end - pos, "CENTER_FRQ1=%d\n",
12079 					  mlo_si.links[i].center_frq1);
12080 			if (os_snprintf_error(end - pos, ret))
12081 				return -1;
12082 			pos += ret;
12083 		}
12084 
12085 		if (mlo_si.links[i].center_frq2 > 0) {
12086 			ret = os_snprintf(pos, end - pos, "CENTER_FRQ2=%d\n",
12087 					  mlo_si.links[i].center_frq2);
12088 			if (os_snprintf_error(end - pos, ret))
12089 				return -1;
12090 			pos += ret;
12091 		}
12092 
12093 		if (mlo_si.links[i].data.avg_signal) {
12094 			ret = os_snprintf(pos, end - pos,
12095 					  "AVG_RSSI=%d\n",
12096 					  mlo_si.links[i].data.avg_signal);
12097 			if (os_snprintf_error(end - pos, ret))
12098 				return -1;
12099 			pos += ret;
12100 		}
12101 
12102 		if (mlo_si.links[i].data.avg_beacon_signal) {
12103 			ret = os_snprintf(
12104 				pos, end - pos, "AVG_BEACON_RSSI=%d\n",
12105 				mlo_si.links[i].data.avg_beacon_signal);
12106 			if (os_snprintf_error(end - pos, ret))
12107 				return -1;
12108 			pos += ret;
12109 		}
12110 	}
12111 
12112 	return pos - buf;
12113 }
12114 
12115 
wpas_ctrl_iface_mlo_status(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)12116 static int wpas_ctrl_iface_mlo_status(struct wpa_supplicant *wpa_s,
12117 				      char *buf, size_t buflen)
12118 {
12119 	int ret, i;
12120 	char *pos, *end;
12121 
12122 	if (!wpa_s->valid_links)
12123 		return -1;
12124 
12125 	pos = buf;
12126 	end = buf + buflen;
12127 
12128 	for_each_link(wpa_s->valid_links, i) {
12129 		ret = os_snprintf(pos, end - pos, "link_id=%d\nfreq=%u\n"
12130 				  "ap_link_addr=" MACSTR
12131 				  "\nsta_link_addr=" MACSTR "\n",
12132 				  i, wpa_s->links[i].freq,
12133 				  MAC2STR(wpa_s->links[i].bssid),
12134 				  MAC2STR(wpa_s->links[i].addr));
12135 		if (os_snprintf_error(end - pos, ret))
12136 			return pos - buf;
12137 		pos += ret;
12138 	}
12139 
12140 	return pos - buf;
12141 }
12142 
12143 
12144 #ifdef CONFIG_TESTING_OPTIONS
wpas_ctrl_ml_probe(struct wpa_supplicant * wpa_s,char * cmd)12145 static int wpas_ctrl_ml_probe(struct wpa_supplicant *wpa_s, char *cmd)
12146 {
12147 	char *token, *context = NULL;
12148 	u8 bssid[ETH_ALEN];
12149 	int mld_id = -1, link_id = -1;
12150 	struct wpa_bss *bss;
12151 	int *freqs;
12152 
12153 	os_memset(bssid, 0, sizeof(bssid));
12154 
12155 	while ((token = str_token(cmd, " ", &context))) {
12156 		if (os_strncmp(token, "bssid=", 6) == 0) {
12157 			if (hwaddr_aton(token + 6, bssid))
12158 				return -1;
12159 		} else if (os_strncmp(token, "mld_id=", 7) == 0) {
12160 			mld_id = atoi(token + 7);
12161 		} else if (os_strncmp(token, "link_id=", 8) == 0) {
12162 			link_id = atoi(token + 8);
12163 		}
12164 	}
12165 
12166 	if (is_zero_ether_addr(bssid)) {
12167 		wpa_printf(MSG_DEBUG,
12168 			   "MLD: Failed parsing ML probe request arguments");
12169 		return -1;
12170 	}
12171 
12172 	bss = wpa_bss_get_bssid(wpa_s, bssid);
12173 	if (!bss) {
12174 		wpa_printf(MSG_DEBUG,
12175 			   "MLD: Unknown BSS for " MACSTR, MAC2STR(bssid));
12176 		return -1;
12177 	}
12178 
12179 	if (wpa_s->sched_scanning || wpa_s->scanning ||
12180 	    (wpa_s->wpa_state > WPA_SCANNING &&
12181 	     wpa_s->wpa_state != WPA_COMPLETED)) {
12182 		wpa_printf(MSG_DEBUG,
12183 			   "MLO: Ongoing scan: Reject ML probe request");
12184 		return -1;
12185 	}
12186 
12187 	freqs = os_malloc(sizeof(int) * 2);
12188 	if (!freqs)
12189 		return -1;
12190 
12191 	freqs[0] = bss->freq;
12192 	freqs[1] = 0;
12193 
12194 	wpa_s->manual_scan_passive = 0;
12195 	wpa_s->manual_scan_use_id = 0;
12196 	wpa_s->manual_scan_only_new = 0;
12197 	wpa_s->scan_id_count = 0;
12198 	wpa_s->scan_res_handler = scan_only_handler;
12199 	os_free(wpa_s->manual_scan_freqs);
12200 	wpa_s->manual_scan_freqs = freqs;
12201 
12202 	os_memcpy(wpa_s->ml_probe_bssid, bssid, ETH_ALEN);
12203 	wpa_s->ml_probe_mld_id = mld_id;
12204 	if (link_id >= 0)
12205 		wpa_s->ml_probe_links = BIT(link_id);
12206 
12207 	wpa_s->normal_scans = 0;
12208 	wpa_s->scan_req = MANUAL_SCAN_REQ;
12209 	wpa_s->after_wps = 0;
12210 	wpa_s->known_wps_freq = 0;
12211 	wpa_supplicant_req_scan(wpa_s, 0, 0);
12212 
12213 	return 0;
12214 }
12215 #endif /* CONFIG_TESTING_OPTIONS */
12216 
12217 
12218 #ifdef CONFIG_NAN_USD
12219 
wpas_ctrl_nan_publish(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)12220 static int wpas_ctrl_nan_publish(struct wpa_supplicant *wpa_s, char *cmd,
12221 				 char *buf, size_t buflen)
12222 {
12223 	char *token, *context = NULL;
12224 	int publish_id;
12225 	struct nan_publish_params params;
12226 	const char *service_name = NULL;
12227 	struct wpabuf *ssi = NULL;
12228 	int ret = -1;
12229 	enum nan_service_protocol_type srv_proto_type = 0;
12230 	int *freq_list = NULL;
12231 	bool p2p = false;
12232 
12233 	os_memset(&params, 0, sizeof(params));
12234 	/* USD shall use both solicited and unsolicited transmissions */
12235 	params.unsolicited = true;
12236 	params.solicited = true;
12237 	/* USD shall require FSD without GAS */
12238 	params.fsd = true;
12239 	params.freq = NAN_USD_DEFAULT_FREQ;
12240 
12241 	while ((token = str_token(cmd, " ", &context))) {
12242 		if (os_strncmp(token, "service_name=", 13) == 0) {
12243 			service_name = token + 13;
12244 			continue;
12245 		}
12246 
12247 		if (os_strncmp(token, "ttl=", 4) == 0) {
12248 			params.ttl = atoi(token + 4);
12249 			continue;
12250 		}
12251 
12252 		if (os_strncmp(token, "freq=", 5) == 0) {
12253 			params.freq = atoi(token + 5);
12254 			continue;
12255 		}
12256 
12257 		if (os_strncmp(token, "freq_list=", 10) == 0) {
12258 			char *pos = token + 10;
12259 
12260 			if (os_strcmp(pos, "all") == 0) {
12261 				os_free(freq_list);
12262 				freq_list = wpas_nan_usd_all_freqs(wpa_s);
12263 				params.freq_list = freq_list;
12264 				continue;
12265 			}
12266 
12267 			while (pos && pos[0]) {
12268 				int_array_add_unique(&freq_list, atoi(pos));
12269 				pos = os_strchr(pos, ',');
12270 				if (pos)
12271 					pos++;
12272 			}
12273 
12274 			params.freq_list = freq_list;
12275 			continue;
12276 		}
12277 
12278 		if (os_strncmp(token, "srv_proto_type=", 15) == 0) {
12279 			srv_proto_type = atoi(token + 15);
12280 			continue;
12281 		}
12282 
12283 		if (os_strncmp(token, "ssi=", 4) == 0) {
12284 			if (ssi)
12285 				goto fail;
12286 			ssi = wpabuf_parse_bin(token + 4);
12287 			if (!ssi)
12288 				goto fail;
12289 			continue;
12290 		}
12291 
12292 		if (os_strcmp(token, "p2p=1") == 0) {
12293 			p2p = true;
12294 			continue;
12295 		}
12296 
12297 		if (os_strcmp(token, "solicited=0") == 0) {
12298 			params.solicited = false;
12299 			continue;
12300 		}
12301 
12302 		if (os_strcmp(token, "unsolicited=0") == 0) {
12303 			params.unsolicited = false;
12304 			continue;
12305 		}
12306 
12307 		if (os_strcmp(token, "fsd=0") == 0) {
12308 			params.fsd = false;
12309 			continue;
12310 		}
12311 
12312 		wpa_printf(MSG_INFO, "CTRL: Invalid NAN_PUBLISH parameter: %s",
12313 			   token);
12314 		goto fail;
12315 	}
12316 
12317 	publish_id = wpas_nan_usd_publish(wpa_s, service_name, srv_proto_type,
12318 					  ssi, &params, p2p);
12319 	if (publish_id > 0)
12320 		ret = os_snprintf(buf, buflen, "%d", publish_id);
12321 fail:
12322 	wpabuf_free(ssi);
12323 	os_free(freq_list);
12324 	return ret;
12325 }
12326 
12327 
wpas_ctrl_nan_cancel_publish(struct wpa_supplicant * wpa_s,char * cmd)12328 static int wpas_ctrl_nan_cancel_publish(struct wpa_supplicant *wpa_s,
12329 					char *cmd)
12330 {
12331 	char *token, *context = NULL;
12332 	int publish_id = 0;
12333 
12334 	while ((token = str_token(cmd, " ", &context))) {
12335 		if (sscanf(token, "publish_id=%i", &publish_id) == 1)
12336 			continue;
12337 		wpa_printf(MSG_INFO,
12338 			   "CTRL: Invalid NAN_CANCEL_PUBLISH parameter: %s",
12339 			   token);
12340 		return -1;
12341 	}
12342 
12343 	if (publish_id <= 0) {
12344 		wpa_printf(MSG_INFO,
12345 			   "CTRL: Invalid or missing NAN_CANCEL_PUBLISH publish_id");
12346 		return -1;
12347 	}
12348 
12349 	wpas_nan_usd_cancel_publish(wpa_s, publish_id);
12350 	return 0;
12351 }
12352 
12353 
wpas_ctrl_nan_update_publish(struct wpa_supplicant * wpa_s,char * cmd)12354 static int wpas_ctrl_nan_update_publish(struct wpa_supplicant *wpa_s,
12355 					char *cmd)
12356 {
12357 	char *token, *context = NULL;
12358 	int publish_id = 0;
12359 	struct wpabuf *ssi = NULL;
12360 	int ret = -1;
12361 
12362 	while ((token = str_token(cmd, " ", &context))) {
12363 		if (sscanf(token, "publish_id=%i", &publish_id) == 1)
12364 			continue;
12365 		if (os_strncmp(token, "ssi=", 4) == 0) {
12366 			if (ssi)
12367 				goto fail;
12368 			ssi = wpabuf_parse_bin(token + 4);
12369 			if (!ssi)
12370 				goto fail;
12371 			continue;
12372 		}
12373 		wpa_printf(MSG_INFO,
12374 			   "CTRL: Invalid NAN_UPDATE_PUBLISH parameter: %s",
12375 			   token);
12376 		goto fail;
12377 	}
12378 
12379 	if (publish_id <= 0) {
12380 		wpa_printf(MSG_INFO,
12381 			   "CTRL: Invalid or missing NAN_UPDATE_PUBLISH publish_id");
12382 		goto fail;
12383 	}
12384 
12385 	ret = wpas_nan_usd_update_publish(wpa_s, publish_id, ssi);
12386 fail:
12387 	wpabuf_free(ssi);
12388 	return ret;
12389 }
12390 
12391 
wpas_ctrl_nan_subscribe(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)12392 static int wpas_ctrl_nan_subscribe(struct wpa_supplicant *wpa_s, char *cmd,
12393 				   char *buf, size_t buflen)
12394 {
12395 	char *token, *context = NULL;
12396 	int subscribe_id;
12397 	struct nan_subscribe_params params;
12398 	const char *service_name = NULL;
12399 	struct wpabuf *ssi = NULL;
12400 	int ret = -1;
12401 	enum nan_service_protocol_type srv_proto_type = 0;
12402 	int *freq_list = NULL;
12403 	bool p2p = false;
12404 
12405 	os_memset(&params, 0, sizeof(params));
12406 	params.freq = NAN_USD_DEFAULT_FREQ;
12407 
12408 	while ((token = str_token(cmd, " ", &context))) {
12409 		if (os_strncmp(token, "service_name=", 13) == 0) {
12410 			service_name = token + 13;
12411 			continue;
12412 		}
12413 
12414 		if (os_strcmp(token, "active=1") == 0) {
12415 			params.active = true;
12416 			continue;
12417 		}
12418 
12419 		if (os_strncmp(token, "ttl=", 4) == 0) {
12420 			params.ttl = atoi(token + 4);
12421 			continue;
12422 		}
12423 
12424 		if (os_strncmp(token, "freq=", 5) == 0) {
12425 			params.freq = atoi(token + 5);
12426 			continue;
12427 		}
12428 
12429 		if (os_strncmp(token, "freq_list=", 10) == 0) {
12430 			char *pos = token + 10;
12431 
12432 			if (os_strcmp(pos, "all") == 0) {
12433 				os_free(freq_list);
12434 				freq_list = wpas_nan_usd_all_freqs(wpa_s);
12435 				params.freq_list = freq_list;
12436 				continue;
12437 			}
12438 
12439 			while (pos && pos[0]) {
12440 				int_array_add_unique(&freq_list, atoi(pos));
12441 				pos = os_strchr(pos, ',');
12442 				if (pos)
12443 					pos++;
12444 			}
12445 
12446 			params.freq_list = freq_list;
12447 			continue;
12448 		}
12449 
12450 		if (os_strncmp(token, "srv_proto_type=", 15) == 0) {
12451 			srv_proto_type = atoi(token + 15);
12452 			continue;
12453 		}
12454 
12455 		if (os_strncmp(token, "ssi=", 4) == 0) {
12456 			if (ssi)
12457 				goto fail;
12458 			ssi = wpabuf_parse_bin(token + 4);
12459 			if (!ssi)
12460 				goto fail;
12461 			continue;
12462 		}
12463 
12464 		if (os_strcmp(token, "p2p=1") == 0) {
12465 			p2p = true;
12466 			continue;
12467 		}
12468 
12469 		wpa_printf(MSG_INFO,
12470 			   "CTRL: Invalid NAN_SUBSCRIBE parameter: %s",
12471 			   token);
12472 		goto fail;
12473 	}
12474 
12475 	subscribe_id = wpas_nan_usd_subscribe(wpa_s, service_name,
12476 					      srv_proto_type, ssi,
12477 					      &params, p2p);
12478 	if (subscribe_id > 0)
12479 		ret = os_snprintf(buf, buflen, "%d", subscribe_id);
12480 fail:
12481 	wpabuf_free(ssi);
12482 	os_free(freq_list);
12483 	return ret;
12484 }
12485 
12486 
wpas_ctrl_nan_cancel_subscribe(struct wpa_supplicant * wpa_s,char * cmd)12487 static int wpas_ctrl_nan_cancel_subscribe(struct wpa_supplicant *wpa_s,
12488 					  char *cmd)
12489 {
12490 	char *token, *context = NULL;
12491 	int subscribe_id = 0;
12492 
12493 	while ((token = str_token(cmd, " ", &context))) {
12494 		if (sscanf(token, "subscribe_id=%i", &subscribe_id) == 1)
12495 			continue;
12496 		wpa_printf(MSG_INFO,
12497 			   "CTRL: Invalid NAN_CANCEL_SUBSCRIBE parameter: %s",
12498 			   token);
12499 		return -1;
12500 	}
12501 
12502 	if (subscribe_id <= 0) {
12503 		wpa_printf(MSG_INFO,
12504 			   "CTRL: Invalid or missing NAN_CANCEL_SUBSCRIBE subscribe_id");
12505 		return -1;
12506 	}
12507 
12508 	wpas_nan_usd_cancel_subscribe(wpa_s, subscribe_id);
12509 	return 0;
12510 }
12511 
12512 
wpas_ctrl_nan_transmit(struct wpa_supplicant * wpa_s,char * cmd)12513 static int wpas_ctrl_nan_transmit(struct wpa_supplicant *wpa_s, char *cmd)
12514 {
12515 	char *token, *context = NULL;
12516 	int handle = 0;
12517 	int req_instance_id = 0;
12518 	struct wpabuf *ssi = NULL;
12519 	u8 peer_addr[ETH_ALEN];
12520 	int ret = -1;
12521 
12522 	os_memset(peer_addr, 0, ETH_ALEN);
12523 
12524 	while ((token = str_token(cmd, " ", &context))) {
12525 		if (sscanf(token, "handle=%i", &handle) == 1)
12526 			continue;
12527 
12528 		if (sscanf(token, "req_instance_id=%i", &req_instance_id) == 1)
12529 			continue;
12530 
12531 		if (os_strncmp(token, "address=", 8) == 0) {
12532 			if (hwaddr_aton(token + 8, peer_addr) < 0)
12533 				return -1;
12534 			continue;
12535 		}
12536 
12537 		if (os_strncmp(token, "ssi=", 4) == 0) {
12538 			if (ssi)
12539 				goto fail;
12540 			ssi = wpabuf_parse_bin(token + 4);
12541 			if (!ssi)
12542 				goto fail;
12543 			continue;
12544 		}
12545 
12546 		wpa_printf(MSG_INFO,
12547 			   "CTRL: Invalid NAN_TRANSMIT parameter: %s",
12548 			   token);
12549 		goto fail;
12550 	}
12551 
12552 	if (handle <= 0) {
12553 		wpa_printf(MSG_INFO,
12554 			   "CTRL: Invalid or missing NAN_TRANSMIT handle");
12555 		goto fail;
12556 	}
12557 
12558 	if (is_zero_ether_addr(peer_addr)) {
12559 		wpa_printf(MSG_INFO,
12560 			   "CTRL: Invalid or missing NAN_TRANSMIT address");
12561 		goto fail;
12562 	}
12563 
12564 	ret = wpas_nan_usd_transmit(wpa_s, handle, ssi, NULL, peer_addr,
12565 				    req_instance_id);
12566 fail:
12567 	wpabuf_free(ssi);
12568 	return ret;
12569 }
12570 
12571 #endif /* CONFIG_NAN_USD */
12572 
12573 
wpa_supplicant_ctrl_iface_process(struct wpa_supplicant * wpa_s,char * buf,size_t * resp_len)12574 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
12575 					 char *buf, size_t *resp_len)
12576 {
12577 	char *reply;
12578 	const int reply_size = 4096;
12579 	int reply_len;
12580 
12581 	if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
12582 	    os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
12583 	    os_strncmp(buf, "PMKSA_ADD ", 10) == 0 ||
12584 	    os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
12585 		if (wpa_debug_show_keys)
12586 			wpa_dbg(wpa_s, MSG_DEBUG,
12587 				"Control interface command '%s'", buf);
12588 		else
12589 			wpa_dbg(wpa_s, MSG_DEBUG,
12590 				"Control interface command '%s [REMOVED]'",
12591 				os_strncmp(buf, WPA_CTRL_RSP,
12592 					   os_strlen(WPA_CTRL_RSP)) == 0 ?
12593 				WPA_CTRL_RSP :
12594 				(os_strncmp(buf, "SET_NETWORK ", 12) == 0 ?
12595 				 "SET_NETWORK" : "key-add"));
12596 	} else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
12597 		   os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
12598 		wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
12599 				      (const u8 *) buf, os_strlen(buf));
12600 	} else {
12601 		int level = wpas_ctrl_cmd_debug_level(buf);
12602 		wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
12603 	}
12604 
12605 	reply = os_malloc(reply_size);
12606 	if (reply == NULL) {
12607 		*resp_len = 1;
12608 		return NULL;
12609 	}
12610 
12611 	os_memcpy(reply, "OK\n", 3);
12612 	reply_len = 3;
12613 
12614 	if (os_strcmp(buf, "PING") == 0) {
12615 		os_memcpy(reply, "PONG\n", 5);
12616 		reply_len = 5;
12617 	} else if (os_strcmp(buf, "IFNAME") == 0) {
12618 		reply_len = os_strlen(wpa_s->ifname);
12619 		os_memcpy(reply, wpa_s->ifname, reply_len);
12620 	} else if (os_strncmp(buf, "RELOG", 5) == 0) {
12621 		if (wpa_debug_reopen_file() < 0)
12622 			reply_len = -1;
12623 	} else if (os_strncmp(buf, "NOTE ", 5) == 0) {
12624 		wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
12625 	} else if (os_strcmp(buf, "MIB") == 0) {
12626 		reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
12627 		if (reply_len >= 0) {
12628 			reply_len += eapol_sm_get_mib(wpa_s->eapol,
12629 						      reply + reply_len,
12630 						      reply_size - reply_len);
12631 #ifdef CONFIG_MACSEC
12632 			reply_len += ieee802_1x_kay_get_mib(
12633 				wpa_s->kay, reply + reply_len,
12634 				reply_size - reply_len);
12635 #endif /* CONFIG_MACSEC */
12636 		}
12637 	} else if (os_strncmp(buf, "STATUS", 6) == 0) {
12638 		reply_len = wpa_supplicant_ctrl_iface_status(
12639 			wpa_s, buf + 6, reply, reply_size);
12640 	} else if (os_strcmp(buf, "PMKSA") == 0) {
12641 		reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
12642 	} else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
12643 		wpas_ctrl_iface_pmksa_flush(wpa_s);
12644 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
12645 	} else if (os_strncmp(buf, "PMKSA_GET ", 10) == 0) {
12646 		reply_len = wpas_ctrl_iface_pmksa_get(wpa_s, buf + 10,
12647 						      reply, reply_size);
12648 	} else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
12649 		if (wpas_ctrl_iface_pmksa_add(wpa_s, buf + 10) < 0)
12650 			reply_len = -1;
12651 #ifdef CONFIG_MESH
12652 	} else if (os_strncmp(buf, "MESH_PMKSA_GET ", 15) == 0) {
12653 		reply_len = wpas_ctrl_iface_mesh_pmksa_get(wpa_s, buf + 15,
12654 							   reply, reply_size);
12655 	} else if (os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
12656 		if (wpas_ctrl_iface_mesh_pmksa_add(wpa_s, buf + 15) < 0)
12657 			reply_len = -1;
12658 #endif /* CONFIG_MESH */
12659 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
12660 	} else if (os_strncmp(buf, "SET ", 4) == 0) {
12661 		if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
12662 			reply_len = -1;
12663 	} else if (os_strncmp(buf, "DUMP", 4) == 0) {
12664 		reply_len = wpa_config_dump_values(wpa_s->conf,
12665 						   reply, reply_size);
12666 	} else if (os_strncmp(buf, "GET ", 4) == 0) {
12667 		reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
12668 							  reply, reply_size);
12669 	} else if (os_strcmp(buf, "LOGON") == 0) {
12670 		eapol_sm_notify_logoff(wpa_s->eapol, false);
12671 	} else if (os_strcmp(buf, "LOGOFF") == 0) {
12672 		eapol_sm_notify_logoff(wpa_s->eapol, true);
12673 	} else if (os_strcmp(buf, "REASSOCIATE") == 0) {
12674 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
12675 			reply_len = -1;
12676 		else
12677 			wpas_request_connection(wpa_s);
12678 	} else if (os_strcmp(buf, "REATTACH") == 0) {
12679 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
12680 		    !wpa_s->current_ssid)
12681 			reply_len = -1;
12682 		else {
12683 			wpa_s->reattach = 1;
12684 			wpas_request_connection(wpa_s);
12685 		}
12686 	} else if (os_strcmp(buf, "RECONNECT") == 0) {
12687 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
12688 			reply_len = -1;
12689 		else if (wpa_s->disconnected)
12690 			wpas_request_connection(wpa_s);
12691 #ifdef IEEE8021X_EAPOL
12692 	} else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
12693 		if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
12694 			reply_len = -1;
12695 #endif /* IEEE8021X_EAPOL */
12696 #ifdef CONFIG_IEEE80211R
12697 	} else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
12698 		if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
12699 			reply_len = -1;
12700 #endif /* CONFIG_IEEE80211R */
12701 #ifdef CONFIG_WPS
12702 	} else if (os_strcmp(buf, "WPS_PBC") == 0) {
12703 		int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
12704 		if (res == -2) {
12705 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
12706 			reply_len = 17;
12707 		} else if (res)
12708 			reply_len = -1;
12709 	} else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
12710 		int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
12711 		if (res == -2) {
12712 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
12713 			reply_len = 17;
12714 		} else if (res)
12715 			reply_len = -1;
12716 	} else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
12717 		reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
12718 							      reply,
12719 							      reply_size);
12720 	} else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
12721 		reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
12722 			wpa_s, buf + 14, reply, reply_size);
12723 	} else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
12724 		if (wpas_wps_cancel(wpa_s))
12725 			reply_len = -1;
12726 #ifdef CONFIG_WPS_NFC
12727 	} else if (os_strcmp(buf, "WPS_NFC") == 0) {
12728 		if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
12729 			reply_len = -1;
12730 	} else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
12731 		if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
12732 			reply_len = -1;
12733 	} else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
12734 		reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
12735 			wpa_s, buf + 21, reply, reply_size);
12736 	} else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
12737 		reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
12738 			wpa_s, buf + 14, reply, reply_size);
12739 	} else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
12740 		if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
12741 							       buf + 17))
12742 			reply_len = -1;
12743 	} else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
12744 		reply_len = wpas_ctrl_nfc_get_handover_req(
12745 			wpa_s, buf + 21, reply, reply_size);
12746 	} else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
12747 		reply_len = wpas_ctrl_nfc_get_handover_sel(
12748 			wpa_s, buf + 21, reply, reply_size);
12749 	} else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
12750 		if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
12751 			reply_len = -1;
12752 #endif /* CONFIG_WPS_NFC */
12753 	} else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
12754 		if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
12755 			reply_len = -1;
12756 #ifdef CONFIG_AP
12757 	} else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
12758 		reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
12759 			wpa_s, buf + 11, reply, reply_size);
12760 #endif /* CONFIG_AP */
12761 #ifdef CONFIG_WPS_ER
12762 	} else if (os_strcmp(buf, "WPS_ER_START") == 0) {
12763 		if (wpas_wps_er_start(wpa_s, NULL))
12764 			reply_len = -1;
12765 	} else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
12766 		if (wpas_wps_er_start(wpa_s, buf + 13))
12767 			reply_len = -1;
12768 	} else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
12769 		wpas_wps_er_stop(wpa_s);
12770 	} else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
12771 		if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
12772 			reply_len = -1;
12773 	} else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
12774 		int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
12775 		if (ret == -2) {
12776 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
12777 			reply_len = 17;
12778 		} else if (ret == -3) {
12779 			os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
12780 			reply_len = 18;
12781 		} else if (ret == -4) {
12782 			os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
12783 			reply_len = 20;
12784 		} else if (ret)
12785 			reply_len = -1;
12786 	} else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
12787 		if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
12788 			reply_len = -1;
12789 	} else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
12790 		if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
12791 								buf + 18))
12792 			reply_len = -1;
12793 	} else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
12794 		if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
12795 			reply_len = -1;
12796 #ifdef CONFIG_WPS_NFC
12797 	} else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
12798 		reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
12799 			wpa_s, buf + 24, reply, reply_size);
12800 #endif /* CONFIG_WPS_NFC */
12801 #endif /* CONFIG_WPS_ER */
12802 #endif /* CONFIG_WPS */
12803 #ifdef CONFIG_IBSS_RSN
12804 	} else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
12805 		if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
12806 			reply_len = -1;
12807 #endif /* CONFIG_IBSS_RSN */
12808 #ifdef CONFIG_MESH
12809 	} else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
12810 		reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
12811 			wpa_s, buf + 19, reply, reply_size);
12812 	} else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
12813 		reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
12814 			wpa_s, "", reply, reply_size);
12815 	} else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
12816 		if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
12817 			reply_len = -1;
12818 	} else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
12819 		if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
12820 								buf + 18))
12821 			reply_len = -1;
12822 	} else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
12823 		if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
12824 			reply_len = -1;
12825 	} else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
12826 		if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
12827 			reply_len = -1;
12828 	} else if (os_strncmp(buf, "MESH_LINK_PROBE ", 16) == 0) {
12829 		if (wpa_supplicant_ctrl_iface_mesh_link_probe(wpa_s, buf + 16))
12830 			reply_len = -1;
12831 #endif /* CONFIG_MESH */
12832 #ifdef CONFIG_P2P
12833 	} else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
12834 		if (p2p_ctrl_find(wpa_s, buf + 8))
12835 			reply_len = -1;
12836 	} else if (os_strcmp(buf, "P2P_FIND") == 0) {
12837 		if (p2p_ctrl_find(wpa_s, ""))
12838 			reply_len = -1;
12839 	} else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
12840 		wpas_p2p_stop_find(wpa_s);
12841 	} else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
12842 		if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
12843 			reply_len = -1;
12844 	} else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
12845 		if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
12846 			reply_len = -1;
12847 	} else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
12848 		reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
12849 					     reply_size);
12850 	} else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
12851 		if (p2p_ctrl_listen(wpa_s, buf + 11))
12852 			reply_len = -1;
12853 	} else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
12854 		if (p2p_ctrl_listen(wpa_s, ""))
12855 			reply_len = -1;
12856 	} else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
12857 		if (wpas_p2p_group_remove(wpa_s, buf + 17))
12858 			reply_len = -1;
12859 	} else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
12860 		if (p2p_ctrl_group_add(wpa_s, ""))
12861 			reply_len = -1;
12862 	} else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
12863 		if (p2p_ctrl_group_add(wpa_s, buf + 14))
12864 			reply_len = -1;
12865 	} else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
12866 		reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
12867 						  reply_size);
12868 	} else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
12869 		if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
12870 			reply_len = -1;
12871 	} else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
12872 		reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
12873 	} else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
12874 		reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
12875 						   reply_size);
12876 	} else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
12877 		if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
12878 			reply_len = -1;
12879 	} else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
12880 		if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
12881 			reply_len = -1;
12882 	} else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
12883 		wpas_p2p_sd_service_update(wpa_s);
12884 	} else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
12885 		if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
12886 			reply_len = -1;
12887 	} else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
12888 		wpas_p2p_service_flush(wpa_s);
12889 	} else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
12890 		if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
12891 			reply_len = -1;
12892 	} else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
12893 		if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
12894 			reply_len = -1;
12895 	} else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
12896 		if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
12897 			reply_len = -1;
12898 	} else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
12899 		if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
12900 			reply_len = -1;
12901 	} else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
12902 		if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
12903 			reply_len = -1;
12904 	} else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
12905 		reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
12906 					      reply_size);
12907 	} else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
12908 		if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
12909 			reply_len = -1;
12910 	} else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
12911 		p2p_ctrl_flush(wpa_s);
12912 	} else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
12913 		if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
12914 			reply_len = -1;
12915 	} else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
12916 		if (wpas_p2p_cancel(wpa_s))
12917 			reply_len = -1;
12918 	} else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
12919 		if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
12920 			reply_len = -1;
12921 	} else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
12922 		if (p2p_ctrl_presence_req(wpa_s, "") < 0)
12923 			reply_len = -1;
12924 	} else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
12925 		if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
12926 			reply_len = -1;
12927 	} else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
12928 		if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
12929 			reply_len = -1;
12930 	} else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
12931 		if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
12932 			reply_len = -1;
12933 	} else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
12934 		if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
12935 			reply_len = -1;
12936 	} else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
12937 		if (wpas_p2p_lo_stop(wpa_s))
12938 			reply_len = -1;
12939 #endif /* CONFIG_P2P */
12940 #ifdef CONFIG_WIFI_DISPLAY
12941 	} else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
12942 		if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
12943 			reply_len = -1;
12944 	} else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
12945 		reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
12946 						     reply, reply_size);
12947 #endif /* CONFIG_WIFI_DISPLAY */
12948 #ifdef CONFIG_INTERWORKING
12949 	} else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
12950 		if (interworking_fetch_anqp(wpa_s) < 0)
12951 			reply_len = -1;
12952 	} else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
12953 		interworking_stop_fetch_anqp(wpa_s);
12954 	} else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
12955 		if (ctrl_interworking_select(wpa_s, NULL) < 0)
12956 			reply_len = -1;
12957 	} else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
12958 		if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
12959 			reply_len = -1;
12960 	} else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
12961 		if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
12962 			reply_len = -1;
12963 	} else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
12964 		int id;
12965 
12966 		id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
12967 		if (id < 0)
12968 			reply_len = -1;
12969 		else {
12970 			reply_len = os_snprintf(reply, reply_size, "%d\n", id);
12971 			if (os_snprintf_error(reply_size, reply_len))
12972 				reply_len = -1;
12973 		}
12974 	} else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
12975 		if (get_anqp(wpa_s, buf + 9) < 0)
12976 			reply_len = -1;
12977 	} else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
12978 		if (gas_request(wpa_s, buf + 12) < 0)
12979 			reply_len = -1;
12980 	} else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
12981 		reply_len = gas_response_get(wpa_s, buf + 17, reply,
12982 					     reply_size);
12983 #endif /* CONFIG_INTERWORKING */
12984 #ifdef CONFIG_HS20
12985 	} else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
12986 		if (get_hs20_anqp(wpa_s, buf + 14) < 0)
12987 			reply_len = -1;
12988 	} else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
12989 		if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
12990 			reply_len = -1;
12991 	} else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
12992 		if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
12993 			reply_len = -1;
12994 	} else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
12995 		if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
12996 			reply_len = -1;
12997 	} else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
12998 		reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
12999 	} else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
13000 		if (del_hs20_icon(wpa_s, buf + 14) < 0)
13001 			reply_len = -1;
13002 	} else if (os_strcmp(buf, "FETCH_OSU") == 0) {
13003 		if (hs20_fetch_osu(wpa_s, 0) < 0)
13004 			reply_len = -1;
13005 	} else if (os_strcmp(buf, "FETCH_OSU no-scan") == 0) {
13006 		if (hs20_fetch_osu(wpa_s, 1) < 0)
13007 			reply_len = -1;
13008 	} else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
13009 		hs20_cancel_fetch_osu(wpa_s);
13010 #endif /* CONFIG_HS20 */
13011 	} else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
13012 	{
13013 		if (wpa_supplicant_ctrl_iface_ctrl_rsp(
13014 			    wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
13015 			reply_len = -1;
13016 		else {
13017 			/*
13018 			 * Notify response from timeout to allow the control
13019 			 * interface response to be sent first.
13020 			 */
13021 			eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
13022 					       wpa_s, NULL);
13023 		}
13024 	} else if (os_strcmp(buf, "RECONFIGURE") == 0) {
13025 		if (wpa_supplicant_reload_configuration(wpa_s))
13026 			reply_len = -1;
13027 	} else if (os_strcmp(buf, "TERMINATE") == 0) {
13028 		wpa_supplicant_terminate_proc(wpa_s->global);
13029 	} else if (os_strncmp(buf, "BSSID ", 6) == 0) {
13030 		if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
13031 			reply_len = -1;
13032 	} else if (os_strncmp(buf, "BSSID_IGNORE", 12) == 0) {
13033 		reply_len = wpa_supplicant_ctrl_iface_bssid_ignore(
13034 			wpa_s, buf + 12, reply, reply_size);
13035 	} else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
13036 		/* deprecated backwards compatibility alias for BSSID_IGNORE */
13037 		reply_len = wpa_supplicant_ctrl_iface_bssid_ignore(
13038 			wpa_s, buf + 9, reply, reply_size);
13039 	} else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
13040 		reply_len = wpa_supplicant_ctrl_iface_log_level(
13041 			wpa_s, buf + 9, reply, reply_size);
13042 	} else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
13043 		reply_len = wpa_supplicant_ctrl_iface_list_networks(
13044 			wpa_s, buf + 14, reply, reply_size);
13045 	} else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
13046 		reply_len = wpa_supplicant_ctrl_iface_list_networks(
13047 			wpa_s, NULL, reply, reply_size);
13048 	} else if (os_strcmp(buf, "DISCONNECT") == 0) {
13049 		wpas_request_disconnection(wpa_s);
13050 	} else if (os_strcmp(buf, "SCAN") == 0) {
13051 		wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
13052 	} else if (os_strncmp(buf, "SCAN ", 5) == 0) {
13053 		wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
13054 	} else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
13055 		reply_len = wpa_supplicant_ctrl_iface_scan_results(
13056 			wpa_s, reply, reply_size);
13057 	} else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
13058 		if (wpas_abort_ongoing_scan(wpa_s) < 0)
13059 			reply_len = -1;
13060 	} else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
13061 		if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
13062 			reply_len = -1;
13063 	} else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
13064 		if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
13065 			reply_len = -1;
13066 	} else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
13067 		if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
13068 			reply_len = -1;
13069 	} else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
13070 		reply_len = wpa_supplicant_ctrl_iface_add_network(
13071 			wpa_s, reply, reply_size);
13072 	} else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
13073 		if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
13074 			reply_len = -1;
13075 	} else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
13076 		if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
13077 			reply_len = -1;
13078 	} else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
13079 		reply_len = wpa_supplicant_ctrl_iface_get_network(
13080 			wpa_s, buf + 12, reply, reply_size);
13081 	} else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
13082 		if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
13083 							  wpa_s))
13084 			reply_len = -1;
13085 	} else if (os_strcmp(buf, "LIST_CREDS") == 0) {
13086 		reply_len = wpa_supplicant_ctrl_iface_list_creds(
13087 			wpa_s, reply, reply_size);
13088 	} else if (os_strcmp(buf, "ADD_CRED") == 0) {
13089 		reply_len = wpa_supplicant_ctrl_iface_add_cred(
13090 			wpa_s, reply, reply_size);
13091 	} else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
13092 		if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
13093 			reply_len = -1;
13094 	} else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
13095 		if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
13096 			reply_len = -1;
13097 	} else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
13098 		reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
13099 							       reply,
13100 							       reply_size);
13101 #ifndef CONFIG_NO_CONFIG_WRITE
13102 	} else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
13103 		if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
13104 			reply_len = -1;
13105 #endif /* CONFIG_NO_CONFIG_WRITE */
13106 	} else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
13107 		reply_len = wpa_supplicant_ctrl_iface_get_capability(
13108 			wpa_s, buf + 15, reply, reply_size);
13109 	} else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
13110 		if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
13111 			reply_len = -1;
13112 	} else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
13113 		if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
13114 			reply_len = -1;
13115 	} else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
13116 		reply_len = wpa_supplicant_global_iface_list(
13117 			wpa_s->global, reply, reply_size);
13118 	} else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
13119 		reply_len = wpa_supplicant_global_iface_interfaces(
13120 			wpa_s->global, buf + 10, reply, reply_size);
13121 	} else if (os_strncmp(buf, "BSS ", 4) == 0) {
13122 		reply_len = wpa_supplicant_ctrl_iface_bss(
13123 			wpa_s, buf + 4, reply, reply_size);
13124 #ifdef CONFIG_AP
13125 	} else if (os_strcmp(buf, "STA-FIRST") == 0) {
13126 		reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
13127 	} else if (os_strncmp(buf, "STA ", 4) == 0) {
13128 		reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
13129 					      reply_size);
13130 	} else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
13131 		reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
13132 						   reply_size);
13133 	} else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
13134 		if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
13135 			reply_len = -1;
13136 	} else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
13137 		if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
13138 			reply_len = -1;
13139 	} else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
13140 		if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
13141 			reply_len = -1;
13142 	} else if (os_strcmp(buf, "STOP_AP") == 0) {
13143 		if (wpas_ap_stop_ap(wpa_s))
13144 			reply_len = -1;
13145 	} else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
13146 		if (wpas_ap_update_beacon(wpa_s))
13147 			reply_len = -1;
13148 	} else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
13149 		if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
13150 			if (ap_ctrl_iface_acl_add_mac(wpa_s,
13151 						      DENY_UNLESS_ACCEPTED,
13152 						      buf + 19) ||
13153 			    ap_ctrl_iface_set_acl(wpa_s))
13154 				reply_len = -1;
13155 		} else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
13156 			if (ap_ctrl_iface_acl_del_mac(wpa_s,
13157 						      DENY_UNLESS_ACCEPTED,
13158 						      buf + 19) ||
13159 			    ap_ctrl_iface_set_acl(wpa_s) ||
13160 			    ap_ctrl_iface_disassoc_accept_mac(wpa_s))
13161 				reply_len = -1;
13162 		} else if (os_strcmp(buf + 11, "SHOW") == 0) {
13163 			reply_len = ap_ctrl_iface_acl_show_mac(
13164 				wpa_s, DENY_UNLESS_ACCEPTED,
13165 				reply, reply_size);
13166 		} else if (os_strcmp(buf + 11, "CLEAR") == 0) {
13167 			ap_ctrl_iface_acl_clear_list(wpa_s,
13168 						     DENY_UNLESS_ACCEPTED);
13169 			if (ap_ctrl_iface_set_acl(wpa_s) ||
13170 			    ap_ctrl_iface_disassoc_accept_mac(wpa_s))
13171 				reply_len = -1;
13172 		} else {
13173 			reply_len = -1;
13174 		}
13175 	} else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
13176 		if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
13177 			if (ap_ctrl_iface_acl_add_mac(wpa_s,
13178 						      ACCEPT_UNLESS_DENIED,
13179 						      buf + 17) ||
13180 			    ap_ctrl_iface_set_acl(wpa_s) ||
13181 			    ap_ctrl_iface_disassoc_deny_mac(wpa_s))
13182 				reply_len = -1;
13183 		} else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
13184 			if (ap_ctrl_iface_acl_del_mac(wpa_s,
13185 						      ACCEPT_UNLESS_DENIED,
13186 						      buf + 17) ||
13187 			    ap_ctrl_iface_set_acl(wpa_s))
13188 				reply_len = -1;
13189 		} else if (os_strcmp(buf + 9, "SHOW") == 0) {
13190 			reply_len = ap_ctrl_iface_acl_show_mac(
13191 				wpa_s, ACCEPT_UNLESS_DENIED, reply, reply_size);
13192 		} else if (os_strcmp(buf + 9, "CLEAR") == 0) {
13193 			ap_ctrl_iface_acl_clear_list(wpa_s,
13194 						     ACCEPT_UNLESS_DENIED);
13195 			if (ap_ctrl_iface_set_acl(wpa_s))
13196 				reply_len = -1;
13197 		} else {
13198 			reply_len = -1;
13199 		}
13200 #endif /* CONFIG_AP */
13201 	} else if (os_strcmp(buf, "SUSPEND") == 0) {
13202 		wpas_notify_suspend(wpa_s->global);
13203 	} else if (os_strcmp(buf, "RESUME") == 0) {
13204 		wpas_notify_resume(wpa_s->global);
13205 #ifdef CONFIG_TESTING_OPTIONS
13206 	} else if (os_strcmp(buf, "DROP_SA") == 0) {
13207 		wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
13208 #endif /* CONFIG_TESTING_OPTIONS */
13209 	} else if (os_strncmp(buf, "ROAM ", 5) == 0) {
13210 		if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
13211 			reply_len = -1;
13212 	} else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
13213 		wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
13214 	} else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
13215 		if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
13216 			reply_len = -1;
13217 	} else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
13218 		if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
13219 							       buf + 17))
13220 			reply_len = -1;
13221 	} else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
13222 		wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
13223 #ifdef CONFIG_TDLS
13224 	} else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
13225 		if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
13226 			reply_len = -1;
13227 	} else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
13228 		if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
13229 			reply_len = -1;
13230 	} else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
13231 		if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
13232 			reply_len = -1;
13233 	} else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
13234 		if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
13235 							       buf + 17))
13236 			reply_len = -1;
13237 	} else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
13238 		if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
13239 								      buf + 24))
13240 			reply_len = -1;
13241 	} else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
13242 		reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
13243 			wpa_s, buf + 17, reply, reply_size);
13244 #endif /* CONFIG_TDLS */
13245 #ifndef CONFIG_NO_WMM_AC
13246 	} else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
13247 		reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
13248 	} else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
13249 		if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
13250 			reply_len = -1;
13251 	} else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
13252 		if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
13253 			reply_len = -1;
13254 #endif /* CONFIG_NO_WMM_AC */
13255 	} else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
13256 		reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
13257 						       reply_size);
13258 	} else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
13259 		if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
13260 			reply_len = -1;
13261 	} else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
13262 		reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
13263 						       reply_size);
13264 #ifdef CONFIG_AUTOSCAN
13265 	} else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
13266 		if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
13267 			reply_len = -1;
13268 #endif /* CONFIG_AUTOSCAN */
13269 	} else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
13270 		reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
13271 							 reply_size);
13272 	} else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
13273 		reply_len = wpas_ctrl_iface_driver_flags2(wpa_s, reply,
13274 							  reply_size);
13275 #ifdef ANDROID
13276 	} else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
13277 		reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
13278 						      reply_size);
13279 #endif /* ANDROID */
13280 	} else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
13281 		reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
13282 						      reply_size);
13283 	} else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
13284 		pmksa_cache_clear_current(wpa_s->wpa);
13285 		eapol_sm_request_reauth(wpa_s->eapol);
13286 #ifdef CONFIG_WNM
13287 	} else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
13288 		if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
13289 			reply_len = -1;
13290 	} else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
13291 		if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
13292 				reply_len = -1;
13293 	} else if (os_strncmp(buf, "COLOC_INTF_REPORT ", 18) == 0) {
13294 		if (wpas_ctrl_iface_coloc_intf_report(wpa_s, buf + 18))
13295 			reply_len = -1;
13296 #endif /* CONFIG_WNM */
13297 #ifdef CONFIG_WNM_AP
13298 	} else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
13299 		if (ap_ctrl_iface_disassoc_imminent(wpa_s, buf + 18))
13300 			reply_len = -1;
13301 	} else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
13302 		if (ap_ctrl_iface_ess_disassoc(wpa_s, buf + 13))
13303 			reply_len = -1;
13304 	} else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
13305 		if (ap_ctrl_iface_bss_tm_req(wpa_s, buf + 11))
13306 			reply_len = -1;
13307 #endif /* CONFIG_WNM_AP */
13308 	} else if (os_strcmp(buf, "FLUSH") == 0) {
13309 		wpa_supplicant_ctrl_iface_flush(wpa_s);
13310 	} else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
13311 		reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
13312 						 reply_size);
13313 #ifdef CONFIG_TESTING_OPTIONS
13314 	} else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
13315 		if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
13316 			reply_len = -1;
13317 	} else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
13318 		wpas_ctrl_iface_mgmt_tx_done(wpa_s);
13319 	} else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
13320 		if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
13321 			reply_len = -1;
13322 	} else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
13323 		if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
13324 			reply_len = -1;
13325 	} else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
13326 		if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
13327 			reply_len = -1;
13328 	} else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
13329 		if (wpas_ctrl_iface_eapol_tx(wpa_s, buf + 9) < 0)
13330 			reply_len = -1;
13331 	} else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
13332 		if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
13333 			reply_len = -1;
13334 	} else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
13335 		if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
13336 			reply_len = -1;
13337 	} else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
13338 		if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
13339 			reply_len = -1;
13340 	} else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
13341 		if (testing_set_fail_pattern(true, buf + 16) < 0)
13342 			reply_len = -1;
13343 	} else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
13344 		reply_len = testing_get_fail_pattern(true, reply, reply_size);
13345 	} else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
13346 		if (testing_set_fail_pattern(false, buf + 10) < 0)
13347 			reply_len = -1;
13348 	} else if (os_strcmp(buf, "GET_FAIL") == 0) {
13349 		reply_len = testing_get_fail_pattern(false, reply, reply_size);
13350 	} else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
13351 		if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
13352 			reply_len = -1;
13353 	} else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
13354 		if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
13355 			reply_len = -1;
13356 	} else if (os_strncmp(buf, "TEST_EAPOL_M2_ELEMS ", 20) == 0) {
13357 		if (wpas_ctrl_test_eapol_m2_elems(wpa_s, buf + 20) < 0)
13358 			reply_len = -1;
13359 	} else if (os_strncmp(buf, "TEST_EAPOL_M4_ELEMS ", 20) == 0) {
13360 		if (wpas_ctrl_test_eapol_m4_elems(wpa_s, buf + 20) < 0)
13361 			reply_len = -1;
13362 	} else if (os_strcmp(buf, "RESET_PN") == 0) {
13363 		if (wpas_ctrl_reset_pn(wpa_s) < 0)
13364 			reply_len = -1;
13365 	} else if (os_strncmp(buf, "KEY_REQUEST ", 12) == 0) {
13366 		if (wpas_ctrl_key_request(wpa_s, buf + 12) < 0)
13367 			reply_len = -1;
13368 	} else if (os_strcmp(buf, "RESEND_ASSOC") == 0) {
13369 		if (wpas_ctrl_resend_assoc(wpa_s) < 0)
13370 			reply_len = -1;
13371 	} else if (os_strcmp(buf, "UNPROT_DEAUTH") == 0) {
13372 		sme_event_unprot_disconnect(
13373 			wpa_s, wpa_s->bssid, NULL,
13374 			WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
13375 	} else if (os_strncmp(buf, "TWT_SETUP ", 10) == 0) {
13376 		if (wpas_ctrl_iface_send_twt_setup(wpa_s, buf + 9))
13377 			reply_len = -1;
13378 	} else if (os_strcmp(buf, "TWT_SETUP") == 0) {
13379 		if (wpas_ctrl_iface_send_twt_setup(wpa_s, ""))
13380 			reply_len = -1;
13381 	} else if (os_strncmp(buf, "TWT_TEARDOWN ", 13) == 0) {
13382 		if (wpas_ctrl_iface_send_twt_teardown(wpa_s, buf + 12))
13383 			reply_len = -1;
13384 	} else if (os_strcmp(buf, "TWT_TEARDOWN") == 0) {
13385 		if (wpas_ctrl_iface_send_twt_teardown(wpa_s, ""))
13386 			reply_len = -1;
13387 	} else if (os_strncmp(buf, "ML_PROBE_REQ ", 13) == 0) {
13388 		if (wpas_ctrl_ml_probe(wpa_s, buf + 13))
13389 			reply_len = -1;
13390 #endif /* CONFIG_TESTING_OPTIONS */
13391 	} else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
13392 		if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
13393 			reply_len = -1;
13394 	} else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
13395 		reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
13396 						      reply_size);
13397 	} else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
13398 		if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
13399 			reply_len = -1;
13400 #ifndef CONFIG_NO_RRM
13401 	} else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
13402 		if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
13403 			reply_len = -1;
13404 #endif /* CONFIG_NO_RRM */
13405 	} else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
13406 		wpas_ctrl_iface_erp_flush(wpa_s);
13407 	} else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
13408 		if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
13409 			reply_len = -1;
13410 	} else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
13411 		reply_len = wpas_ctrl_iface_get_pref_freq_list(
13412 			wpa_s, buf + 19, reply, reply_size);
13413 #ifdef CONFIG_FILS
13414 	} else if (os_strncmp(buf, "FILS_HLP_REQ_ADD ", 17) == 0) {
13415 		if (wpas_ctrl_iface_fils_hlp_req_add(wpa_s, buf + 17))
13416 			reply_len = -1;
13417 	} else if (os_strcmp(buf, "FILS_HLP_REQ_FLUSH") == 0) {
13418 		wpas_flush_fils_hlp_req(wpa_s);
13419 #endif /* CONFIG_FILS */
13420 #ifdef CONFIG_DPP
13421 	} else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
13422 		int res;
13423 
13424 		res = wpas_dpp_qr_code(wpa_s, buf + 12);
13425 		if (res < 0) {
13426 			reply_len = -1;
13427 		} else {
13428 			reply_len = os_snprintf(reply, reply_size, "%d", res);
13429 			if (os_snprintf_error(reply_size, reply_len))
13430 				reply_len = -1;
13431 		}
13432 	} else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
13433 		int res;
13434 
13435 		res = wpas_dpp_nfc_uri(wpa_s, buf + 12);
13436 		if (res < 0) {
13437 			reply_len = -1;
13438 		} else {
13439 			reply_len = os_snprintf(reply, reply_size, "%d", res);
13440 			if (os_snprintf_error(reply_size, reply_len))
13441 				reply_len = -1;
13442 		}
13443 	} else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
13444 		int res;
13445 
13446 		res = wpas_dpp_nfc_handover_req(wpa_s, buf + 20);
13447 		if (res < 0) {
13448 			reply_len = -1;
13449 		} else {
13450 			reply_len = os_snprintf(reply, reply_size, "%d", res);
13451 			if (os_snprintf_error(reply_size, reply_len))
13452 				reply_len = -1;
13453 		}
13454 	} else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
13455 		int res;
13456 
13457 		res = wpas_dpp_nfc_handover_sel(wpa_s, buf + 20);
13458 		if (res < 0) {
13459 			reply_len = -1;
13460 		} else {
13461 			reply_len = os_snprintf(reply, reply_size, "%d", res);
13462 			if (os_snprintf_error(reply_size, reply_len))
13463 				reply_len = -1;
13464 		}
13465 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
13466 		int res;
13467 
13468 		res = dpp_bootstrap_gen(wpa_s->dpp, buf + 18);
13469 		if (res < 0) {
13470 			reply_len = -1;
13471 		} else {
13472 			reply_len = os_snprintf(reply, reply_size, "%d", res);
13473 			if (os_snprintf_error(reply_size, reply_len))
13474 				reply_len = -1;
13475 		}
13476 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
13477 		if (dpp_bootstrap_remove(wpa_s->dpp, buf + 21) < 0)
13478 			reply_len = -1;
13479 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
13480 		const char *uri;
13481 
13482 		uri = dpp_bootstrap_get_uri(wpa_s->dpp, atoi(buf + 22));
13483 		if (!uri) {
13484 			reply_len = -1;
13485 		} else {
13486 			reply_len = os_snprintf(reply, reply_size, "%s", uri);
13487 			if (os_snprintf_error(reply_size, reply_len))
13488 				reply_len = -1;
13489 		}
13490 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
13491 		reply_len = dpp_bootstrap_info(wpa_s->dpp, atoi(buf + 19),
13492 					       reply, reply_size);
13493 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
13494 		if (dpp_bootstrap_set(wpa_s->dpp, atoi(buf + 18),
13495 				      os_strchr(buf + 18, ' ')) < 0)
13496 			reply_len = -1;
13497 	} else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
13498 		if (wpas_dpp_auth_init(wpa_s, buf + 13) < 0)
13499 			reply_len = -1;
13500 	} else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
13501 		if (wpas_dpp_listen(wpa_s, buf + 11) < 0)
13502 			reply_len = -1;
13503 	} else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
13504 		wpas_dpp_stop(wpa_s);
13505 		wpas_dpp_listen_stop(wpa_s);
13506 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
13507 		int res;
13508 
13509 		res = dpp_configurator_add(wpa_s->dpp, buf + 20);
13510 		if (res < 0) {
13511 			reply_len = -1;
13512 		} else {
13513 			reply_len = os_snprintf(reply, reply_size, "%d", res);
13514 			if (os_snprintf_error(reply_size, reply_len))
13515 				reply_len = -1;
13516 		}
13517 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_SET ", 21) == 0) {
13518 		if (dpp_configurator_set(wpa_s->dpp, buf + 20) < 0)
13519 			reply_len = -1;
13520 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
13521 		if (dpp_configurator_remove(wpa_s->dpp, buf + 24) < 0)
13522 			reply_len = -1;
13523 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
13524 		if (wpas_dpp_configurator_sign(wpa_s, buf + 21) < 0)
13525 			reply_len = -1;
13526 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
13527 		reply_len = dpp_configurator_get_key_id(wpa_s->dpp,
13528 							atoi(buf + 25),
13529 							reply, reply_size);
13530 	} else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
13531 		int res;
13532 
13533 		res = wpas_dpp_pkex_add(wpa_s, buf + 12);
13534 		if (res < 0) {
13535 			reply_len = -1;
13536 		} else {
13537 			reply_len = os_snprintf(reply, reply_size, "%d", res);
13538 			if (os_snprintf_error(reply_size, reply_len))
13539 				reply_len = -1;
13540 		}
13541 	} else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
13542 		if (wpas_dpp_pkex_remove(wpa_s, buf + 16) < 0)
13543 			reply_len = -1;
13544 	} else if (os_strncmp(buf, "DPP_CONF_SET ", 13) == 0) {
13545 		if (wpas_dpp_conf_set(wpa_s, buf + 12) < 0)
13546 			reply_len = -1;
13547 #ifdef CONFIG_DPP2
13548 	} else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
13549 		if (wpas_dpp_controller_start(wpa_s, buf + 20) < 0)
13550 			reply_len = -1;
13551 	} else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
13552 		if (wpas_dpp_controller_start(wpa_s, NULL) < 0)
13553 			reply_len = -1;
13554 	} else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
13555 		dpp_controller_stop(wpa_s->dpp);
13556 	} else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
13557 		if (wpas_dpp_chirp(wpa_s, buf + 9) < 0)
13558 			reply_len = -1;
13559 	} else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
13560 		wpas_dpp_chirp_stop(wpa_s);
13561 	} else if (os_strncmp(buf, "DPP_RECONFIG ", 13) == 0) {
13562 		if (wpas_dpp_reconfig(wpa_s, buf + 13) < 0)
13563 			reply_len = -1;
13564 	} else if (os_strncmp(buf, "DPP_CA_SET ", 11) == 0) {
13565 		if (wpas_dpp_ca_set(wpa_s, buf + 10) < 0)
13566 			reply_len = -1;
13567 #endif /* CONFIG_DPP2 */
13568 #ifdef CONFIG_DPP3
13569 	} else if (os_strcmp(buf, "DPP_PUSH_BUTTON") == 0) {
13570 		if (wpas_dpp_push_button(wpa_s, NULL) < 0)
13571 			reply_len = -1;
13572 	} else if (os_strncmp(buf, "DPP_PUSH_BUTTON ", 16) == 0) {
13573 		if (wpas_dpp_push_button(wpa_s, buf + 15) < 0)
13574 			reply_len = -1;
13575 #endif /* CONFIG_DPP3 */
13576 #endif /* CONFIG_DPP */
13577 #ifdef CONFIG_NAN_USD
13578 	} else if (os_strncmp(buf, "NAN_PUBLISH ", 12) == 0) {
13579 		reply_len = wpas_ctrl_nan_publish(wpa_s, buf + 12, reply,
13580 						  reply_size);
13581 	} else if (os_strncmp(buf, "NAN_CANCEL_PUBLISH ", 19) == 0) {
13582 		if (wpas_ctrl_nan_cancel_publish(wpa_s, buf + 19) < 0)
13583 			reply_len = -1;
13584 	} else if (os_strncmp(buf, "NAN_UPDATE_PUBLISH ", 19) == 0) {
13585 		if (wpas_ctrl_nan_update_publish(wpa_s, buf + 19) < 0)
13586 			reply_len = -1;
13587 	} else if (os_strncmp(buf, "NAN_SUBSCRIBE ", 14) == 0) {
13588 		reply_len = wpas_ctrl_nan_subscribe(wpa_s, buf + 14, reply,
13589 						    reply_size);
13590 	} else if (os_strncmp(buf, "NAN_CANCEL_SUBSCRIBE ", 21) == 0) {
13591 		if (wpas_ctrl_nan_cancel_subscribe(wpa_s, buf + 21) < 0)
13592 			reply_len = -1;
13593 	} else if (os_strncmp(buf, "NAN_TRANSMIT ", 13) == 0) {
13594 		if (wpas_ctrl_nan_transmit(wpa_s, buf + 13) < 0)
13595 			reply_len = -1;
13596 #endif /* CONFIG_NAN_USD */
13597 #ifdef CONFIG_PASN
13598 	} else if (os_strncmp(buf, "PASN_START ", 11) == 0) {
13599 		if (wpas_ctrl_iface_pasn_start(wpa_s, buf + 11) < 0)
13600 			reply_len = -1;
13601 	} else if (os_strcmp(buf, "PASN_STOP") == 0) {
13602 		wpas_pasn_auth_stop(wpa_s);
13603 	} else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
13604 		reply_len = ptksa_cache_list(wpa_s->ptksa, reply, reply_size);
13605 	} else if (os_strncmp(buf, "PASN_DEAUTH ", 12) == 0) {
13606 		if (wpas_ctrl_iface_pasn_deauthenticate(wpa_s, buf + 12) < 0)
13607 			reply_len = -1;
13608 #ifdef CONFIG_TESTING_OPTIONS
13609 	} else if (os_strncmp(buf, "PASN_DRIVER ", 12) == 0) {
13610 		if (wpas_ctrl_iface_pasn_driver(wpa_s, buf + 12) < 0)
13611 			reply_len = -1;
13612 #endif /* CONFIG_TESTING_OPTIONS */
13613 #endif /* CONFIG_PASN */
13614 #ifndef CONFIG_NO_ROBUST_AV
13615 	} else if (os_strncmp(buf, "MSCS ", 5) == 0) {
13616 		if (wpas_ctrl_iface_configure_mscs(wpa_s, buf + 5))
13617 			reply_len = -1;
13618 	} else if (os_strncmp(buf, "SCS ", 4) == 0) {
13619 		if (wpas_ctrl_iface_configure_scs(wpa_s, buf + 4))
13620 			reply_len = -1;
13621 	} else if (os_strncmp(buf, "DSCP_RESP ", 10) == 0) {
13622 		if (wpas_ctrl_iface_send_dscp_resp(wpa_s, buf + 10))
13623 			reply_len = -1;
13624 	} else if (os_strncmp(buf, "DSCP_QUERY ", 11) == 0) {
13625 		if (wpas_ctrl_iface_send_dscp_query(wpa_s, buf + 11))
13626 			reply_len = -1;
13627 #endif /* CONFIG_NO_ROBUST_AV */
13628 	} else if (os_strcmp(buf, "MLO_STATUS") == 0) {
13629 		reply_len = wpas_ctrl_iface_mlo_status(wpa_s, reply,
13630 						       reply_size);
13631 	} else if (os_strcmp(buf, "MLO_SIGNAL_POLL") == 0) {
13632 		reply_len = wpas_ctrl_iface_mlo_signal_poll(wpa_s, reply,
13633 							    reply_size);
13634 	} else {
13635 		os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
13636 		reply_len = 16;
13637 	}
13638 
13639 	if (reply_len < 0) {
13640 		os_memcpy(reply, "FAIL\n", 5);
13641 		reply_len = 5;
13642 	}
13643 
13644 	*resp_len = reply_len;
13645 	return reply;
13646 }
13647 
13648 
wpa_supplicant_global_iface_add(struct wpa_global * global,char * cmd)13649 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
13650 					   char *cmd)
13651 {
13652 	struct wpa_interface iface;
13653 	char *pos, *extra;
13654 	struct wpa_supplicant *wpa_s;
13655 	unsigned int create_iface = 0;
13656 	u8 mac_addr[ETH_ALEN];
13657 	enum wpa_driver_if_type type = WPA_IF_STATION;
13658 
13659 	/*
13660 	 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
13661 	 * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
13662 	 */
13663 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
13664 
13665 	os_memset(&iface, 0, sizeof(iface));
13666 
13667 	do {
13668 		iface.ifname = pos = cmd;
13669 		pos = os_strchr(pos, '\t');
13670 		if (pos)
13671 			*pos++ = '\0';
13672 		if (iface.ifname[0] == '\0')
13673 			return -1;
13674 		if (pos == NULL)
13675 			break;
13676 
13677 		iface.confname = pos;
13678 		pos = os_strchr(pos, '\t');
13679 		if (pos)
13680 			*pos++ = '\0';
13681 		if (iface.confname[0] == '\0')
13682 			iface.confname = NULL;
13683 		if (pos == NULL)
13684 			break;
13685 
13686 		iface.driver = pos;
13687 		pos = os_strchr(pos, '\t');
13688 		if (pos)
13689 			*pos++ = '\0';
13690 		if (iface.driver[0] == '\0')
13691 			iface.driver = NULL;
13692 		if (pos == NULL)
13693 			break;
13694 
13695 		iface.ctrl_interface = pos;
13696 		pos = os_strchr(pos, '\t');
13697 		if (pos)
13698 			*pos++ = '\0';
13699 		if (iface.ctrl_interface[0] == '\0')
13700 			iface.ctrl_interface = NULL;
13701 		if (pos == NULL)
13702 			break;
13703 
13704 		iface.driver_param = pos;
13705 		pos = os_strchr(pos, '\t');
13706 		if (pos)
13707 			*pos++ = '\0';
13708 		if (iface.driver_param[0] == '\0')
13709 			iface.driver_param = NULL;
13710 		if (pos == NULL)
13711 			break;
13712 
13713 		iface.bridge_ifname = pos;
13714 		pos = os_strchr(pos, '\t');
13715 		if (pos)
13716 			*pos++ = '\0';
13717 		if (iface.bridge_ifname[0] == '\0')
13718 			iface.bridge_ifname = NULL;
13719 		if (pos == NULL)
13720 			break;
13721 
13722 		extra = pos;
13723 		pos = os_strchr(pos, '\t');
13724 		if (pos)
13725 			*pos++ = '\0';
13726 		if (!extra[0])
13727 			break;
13728 
13729 		if (os_strcmp(extra, "create") == 0) {
13730 			create_iface = 1;
13731 			if (!pos)
13732 				break;
13733 
13734 			if (os_strcmp(pos, "sta") == 0) {
13735 				type = WPA_IF_STATION;
13736 			} else if (os_strcmp(pos, "ap") == 0) {
13737 				type = WPA_IF_AP_BSS;
13738 			} else {
13739 				wpa_printf(MSG_DEBUG,
13740 					   "INTERFACE_ADD unsupported interface type: '%s'",
13741 					   pos);
13742 				return -1;
13743 			}
13744 		} else {
13745 			wpa_printf(MSG_DEBUG,
13746 				   "INTERFACE_ADD unsupported extra parameter: '%s'",
13747 				   extra);
13748 			return -1;
13749 		}
13750 	} while (0);
13751 
13752 	if (create_iface) {
13753 		wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
13754 			   iface.ifname);
13755 		if (!global->ifaces)
13756 			return -1;
13757 		if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
13758 				   NULL, NULL, NULL, mac_addr, NULL) < 0) {
13759 			wpa_printf(MSG_ERROR,
13760 				   "CTRL_IFACE interface creation failed");
13761 			return -1;
13762 		}
13763 
13764 		wpa_printf(MSG_DEBUG,
13765 			   "CTRL_IFACE interface '%s' created with MAC addr: "
13766 			   MACSTR, iface.ifname, MAC2STR(mac_addr));
13767 	}
13768 
13769 	if (wpa_supplicant_get_iface(global, iface.ifname))
13770 		goto fail;
13771 
13772 	wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
13773 	if (!wpa_s)
13774 		goto fail;
13775 	wpa_s->added_vif = create_iface;
13776 	return 0;
13777 
13778 fail:
13779 	if (create_iface) {
13780 		/* wpa_supplicant does not create multi-BSS AP, so collapse to
13781 		 * WPA_IF_STATION to avoid unwanted clean up in the driver. */
13782 		wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
13783 	}
13784 	return -1;
13785 }
13786 
13787 
wpa_supplicant_global_iface_remove(struct wpa_global * global,char * cmd)13788 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
13789 					      char *cmd)
13790 {
13791 	struct wpa_supplicant *wpa_s;
13792 	int ret;
13793 	unsigned int delete_iface;
13794 
13795 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
13796 
13797 	wpa_s = wpa_supplicant_get_iface(global, cmd);
13798 	if (wpa_s == NULL)
13799 		return -1;
13800 	delete_iface = wpa_s->added_vif;
13801 	ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
13802 	if (!ret && delete_iface) {
13803 		wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
13804 			   cmd);
13805 		/* wpa_supplicant does not create multi-BSS AP, so collapse to
13806 		 * WPA_IF_STATION to avoid unwanted clean up in the driver. */
13807 		ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
13808 	}
13809 	return ret;
13810 }
13811 
13812 
wpa_free_iface_info(struct wpa_interface_info * iface)13813 static void wpa_free_iface_info(struct wpa_interface_info *iface)
13814 {
13815 	struct wpa_interface_info *prev;
13816 
13817 	while (iface) {
13818 		prev = iface;
13819 		iface = iface->next;
13820 
13821 		os_free(prev->ifname);
13822 		os_free(prev->desc);
13823 		os_free(prev);
13824 	}
13825 }
13826 
13827 
wpa_supplicant_global_iface_list(struct wpa_global * global,char * buf,int len)13828 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
13829 					    char *buf, int len)
13830 {
13831 	int i, res;
13832 	struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
13833 	char *pos, *end;
13834 
13835 	for (i = 0; wpa_drivers[i]; i++) {
13836 		const struct wpa_driver_ops *drv = wpa_drivers[i];
13837 		if (drv->get_interfaces == NULL)
13838 			continue;
13839 		tmp = drv->get_interfaces(global->drv_priv[i]);
13840 		if (tmp == NULL)
13841 			continue;
13842 
13843 		if (last == NULL)
13844 			iface = last = tmp;
13845 		else
13846 			last->next = tmp;
13847 		while (last->next)
13848 			last = last->next;
13849 	}
13850 
13851 	pos = buf;
13852 	end = buf + len;
13853 	for (tmp = iface; tmp; tmp = tmp->next) {
13854 		res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
13855 				  tmp->drv_name, tmp->ifname,
13856 				  tmp->desc ? tmp->desc : "");
13857 		if (os_snprintf_error(end - pos, res)) {
13858 			*pos = '\0';
13859 			break;
13860 		}
13861 		pos += res;
13862 	}
13863 
13864 	wpa_free_iface_info(iface);
13865 
13866 	return pos - buf;
13867 }
13868 
13869 
wpa_supplicant_global_iface_interfaces(struct wpa_global * global,const char * input,char * buf,int len)13870 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
13871 						  const char *input,
13872 						  char *buf, int len)
13873 {
13874 	int res;
13875 	char *pos, *end;
13876 	struct wpa_supplicant *wpa_s;
13877 	int show_ctrl = 0;
13878 
13879 	if (input)
13880 		show_ctrl = !!os_strstr(input, "ctrl");
13881 
13882 	wpa_s = global->ifaces;
13883 	pos = buf;
13884 	end = buf + len;
13885 
13886 	while (wpa_s) {
13887 		if (show_ctrl)
13888 			res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
13889 					  wpa_s->ifname,
13890 					  wpa_s->conf->ctrl_interface ?
13891 					  wpa_s->conf->ctrl_interface : "N/A");
13892 		else
13893 			res = os_snprintf(pos, end - pos, "%s\n",
13894 					  wpa_s->ifname);
13895 
13896 		if (os_snprintf_error(end - pos, res)) {
13897 			*pos = '\0';
13898 			break;
13899 		}
13900 		pos += res;
13901 		wpa_s = wpa_s->next;
13902 	}
13903 	return pos - buf;
13904 }
13905 
13906 
wpas_global_ctrl_iface_ifname(struct wpa_global * global,const char * ifname,char * cmd,size_t * resp_len)13907 static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
13908 					    const char *ifname,
13909 					    char *cmd, size_t *resp_len)
13910 {
13911 	struct wpa_supplicant *wpa_s;
13912 
13913 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
13914 		if (os_strcmp(ifname, wpa_s->ifname) == 0)
13915 			break;
13916 	}
13917 
13918 	if (wpa_s == NULL) {
13919 		char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
13920 		if (resp)
13921 			*resp_len = os_strlen(resp);
13922 		else
13923 			*resp_len = 1;
13924 		return resp;
13925 	}
13926 
13927 	return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
13928 }
13929 
13930 
wpas_global_ctrl_iface_redir_p2p(struct wpa_global * global,char * buf,size_t * resp_len)13931 static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
13932 					       char *buf, size_t *resp_len)
13933 {
13934 #ifdef CONFIG_P2P
13935 	static const char * cmd[] = {
13936 		"LIST_NETWORKS",
13937 		"P2P_FIND",
13938 		"P2P_STOP_FIND",
13939 		"P2P_LISTEN",
13940 		"P2P_GROUP_ADD",
13941 		"P2P_GET_PASSPHRASE",
13942 		"P2P_SERVICE_UPDATE",
13943 		"P2P_SERVICE_FLUSH",
13944 		"P2P_FLUSH",
13945 		"P2P_CANCEL",
13946 		"P2P_PRESENCE_REQ",
13947 		"P2P_EXT_LISTEN",
13948 #ifdef CONFIG_AP
13949 		"STA-FIRST",
13950 #endif /* CONFIG_AP */
13951 		NULL
13952 	};
13953 	static const char * prefix[] = {
13954 #ifdef ANDROID
13955 		"DRIVER ",
13956 #endif /* ANDROID */
13957 		"GET_CAPABILITY ",
13958 		"GET_NETWORK ",
13959 		"REMOVE_NETWORK ",
13960 		"P2P_FIND ",
13961 		"P2P_CONNECT ",
13962 		"P2P_LISTEN ",
13963 		"P2P_GROUP_REMOVE ",
13964 		"P2P_GROUP_ADD ",
13965 		"P2P_GROUP_MEMBER ",
13966 		"P2P_PROV_DISC ",
13967 		"P2P_SERV_DISC_REQ ",
13968 		"P2P_SERV_DISC_CANCEL_REQ ",
13969 		"P2P_SERV_DISC_RESP ",
13970 		"P2P_SERV_DISC_EXTERNAL ",
13971 		"P2P_SERVICE_ADD ",
13972 		"P2P_SERVICE_DEL ",
13973 		"P2P_SERVICE_REP ",
13974 		"P2P_REJECT ",
13975 		"P2P_INVITE ",
13976 		"P2P_PEER ",
13977 		"P2P_SET ",
13978 		"P2P_UNAUTHORIZE ",
13979 		"P2P_PRESENCE_REQ ",
13980 		"P2P_EXT_LISTEN ",
13981 		"P2P_REMOVE_CLIENT ",
13982 		"WPS_NFC_TOKEN ",
13983 		"WPS_NFC_TAG_READ ",
13984 		"NFC_GET_HANDOVER_SEL ",
13985 		"NFC_GET_HANDOVER_REQ ",
13986 		"NFC_REPORT_HANDOVER ",
13987 		"P2P_ASP_PROVISION ",
13988 		"P2P_ASP_PROVISION_RESP ",
13989 #ifdef CONFIG_AP
13990 		"STA ",
13991 		"STA-NEXT ",
13992 #endif /* CONFIG_AP */
13993 		NULL
13994 	};
13995 	int found = 0;
13996 	int i;
13997 
13998 	if (global->p2p_init_wpa_s == NULL)
13999 		return NULL;
14000 
14001 	for (i = 0; !found && cmd[i]; i++) {
14002 		if (os_strcmp(buf, cmd[i]) == 0)
14003 			found = 1;
14004 	}
14005 
14006 	for (i = 0; !found && prefix[i]; i++) {
14007 		if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
14008 			found = 1;
14009 	}
14010 
14011 	if (found)
14012 		return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
14013 							 buf, resp_len);
14014 #endif /* CONFIG_P2P */
14015 	return NULL;
14016 }
14017 
14018 
wpas_global_ctrl_iface_redir_wfd(struct wpa_global * global,char * buf,size_t * resp_len)14019 static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
14020 					       char *buf, size_t *resp_len)
14021 {
14022 #ifdef CONFIG_WIFI_DISPLAY
14023 	if (global->p2p_init_wpa_s == NULL)
14024 		return NULL;
14025 	if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
14026 	    os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
14027 		return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
14028 							 buf, resp_len);
14029 #endif /* CONFIG_WIFI_DISPLAY */
14030 	return NULL;
14031 }
14032 
14033 
wpas_global_ctrl_iface_redir(struct wpa_global * global,char * buf,size_t * resp_len)14034 static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
14035 					   char *buf, size_t *resp_len)
14036 {
14037 	char *ret;
14038 
14039 	ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
14040 	if (ret)
14041 		return ret;
14042 
14043 	ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
14044 	if (ret)
14045 		return ret;
14046 
14047 	return NULL;
14048 }
14049 
14050 
wpas_global_ctrl_iface_set(struct wpa_global * global,char * cmd)14051 static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
14052 {
14053 	char *value;
14054 
14055 	value = os_strchr(cmd, ' ');
14056 	if (value == NULL)
14057 		return -1;
14058 	*value++ = '\0';
14059 
14060 	wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
14061 
14062 #ifdef CONFIG_WIFI_DISPLAY
14063 	if (os_strcasecmp(cmd, "wifi_display") == 0) {
14064 		wifi_display_enable(global, !!atoi(value));
14065 		return 0;
14066 	}
14067 #endif /* CONFIG_WIFI_DISPLAY */
14068 
14069 	/* Restore cmd to its original value to allow redirection */
14070 	value[-1] = ' ';
14071 
14072 	return -1;
14073 }
14074 
14075 
wpas_global_ctrl_iface_dup_network(struct wpa_global * global,char * cmd)14076 static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
14077 					      char *cmd)
14078 {
14079 	struct wpa_supplicant *wpa_s[2]; /* src, dst */
14080 	char *p;
14081 	unsigned int i;
14082 
14083 	/* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
14084 	 * <variable name> */
14085 
14086 	for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
14087 		p = os_strchr(cmd, ' ');
14088 		if (p == NULL)
14089 			return -1;
14090 		*p = '\0';
14091 
14092 		wpa_s[i] = global->ifaces;
14093 		for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
14094 			if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
14095 				break;
14096 		}
14097 
14098 		if (!wpa_s[i]) {
14099 			wpa_printf(MSG_DEBUG,
14100 				   "CTRL_IFACE: Could not find iface=%s", cmd);
14101 			return -1;
14102 		}
14103 
14104 		cmd = p + 1;
14105 	}
14106 
14107 	return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
14108 }
14109 
14110 
14111 #ifndef CONFIG_NO_CONFIG_WRITE
wpas_global_ctrl_iface_save_config(struct wpa_global * global)14112 static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
14113 {
14114 	int ret = 0, saved = 0;
14115 	struct wpa_supplicant *wpa_s;
14116 
14117 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
14118 		if (!wpa_s->conf->update_config) {
14119 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
14120 			continue;
14121 		}
14122 
14123 		if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
14124 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
14125 			ret = 1;
14126 		} else {
14127 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
14128 			saved++;
14129 		}
14130 	}
14131 
14132 	if (!saved && !ret) {
14133 		wpa_dbg(wpa_s, MSG_DEBUG,
14134 			"CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
14135 		ret = 1;
14136 	}
14137 
14138 	return ret;
14139 }
14140 #endif /* CONFIG_NO_CONFIG_WRITE */
14141 
14142 
wpas_global_ctrl_iface_status(struct wpa_global * global,char * buf,size_t buflen)14143 static int wpas_global_ctrl_iface_status(struct wpa_global *global,
14144 					 char *buf, size_t buflen)
14145 {
14146 	char *pos, *end;
14147 	int ret;
14148 	struct wpa_supplicant *wpa_s;
14149 
14150 	pos = buf;
14151 	end = buf + buflen;
14152 
14153 #ifdef CONFIG_P2P
14154 	if (global->p2p && !global->p2p_disabled) {
14155 		ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
14156 				  "\n"
14157 				  "p2p_state=%s\n",
14158 				  MAC2STR(global->p2p_dev_addr),
14159 				  p2p_get_state_txt(global->p2p));
14160 		if (os_snprintf_error(end - pos, ret))
14161 			return pos - buf;
14162 		pos += ret;
14163 	} else if (global->p2p) {
14164 		ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
14165 		if (os_snprintf_error(end - pos, ret))
14166 			return pos - buf;
14167 		pos += ret;
14168 	}
14169 #endif /* CONFIG_P2P */
14170 
14171 #ifdef CONFIG_WIFI_DISPLAY
14172 	ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
14173 			  !!global->wifi_display);
14174 	if (os_snprintf_error(end - pos, ret))
14175 		return pos - buf;
14176 	pos += ret;
14177 #endif /* CONFIG_WIFI_DISPLAY */
14178 
14179 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
14180 		ret = os_snprintf(pos, end - pos, "ifname=%s\n"
14181 				  "address=" MACSTR "\n",
14182 				  wpa_s->ifname, MAC2STR(wpa_s->own_addr));
14183 		if (os_snprintf_error(end - pos, ret))
14184 			return pos - buf;
14185 		pos += ret;
14186 	}
14187 
14188 	return pos - buf;
14189 }
14190 
14191 
14192 #ifdef CONFIG_FST
14193 
wpas_global_ctrl_iface_fst_attach(struct wpa_global * global,char * cmd,char * buf,size_t reply_size)14194 static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
14195 					     char *cmd, char *buf,
14196 					     size_t reply_size)
14197 {
14198 	char ifname[IFNAMSIZ + 1];
14199 	struct fst_iface_cfg cfg;
14200 	struct wpa_supplicant *wpa_s;
14201 	struct fst_wpa_obj iface_obj;
14202 
14203 	if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
14204 		wpa_s = wpa_supplicant_get_iface(global, ifname);
14205 		if (wpa_s) {
14206 			if (wpa_s->fst) {
14207 				wpa_printf(MSG_INFO, "FST: Already attached");
14208 				return -1;
14209 			}
14210 			fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
14211 			wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
14212 						&iface_obj, &cfg);
14213 			if (wpa_s->fst)
14214 				return os_snprintf(buf, reply_size, "OK\n");
14215 		}
14216 	}
14217 
14218 	return -1;
14219 }
14220 
14221 
wpas_global_ctrl_iface_fst_detach(struct wpa_global * global,char * cmd,char * buf,size_t reply_size)14222 static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
14223 					     char *cmd, char *buf,
14224 					     size_t reply_size)
14225 {
14226 	char ifname[IFNAMSIZ + 1];
14227 	struct wpa_supplicant *wpa_s;
14228 
14229 	if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
14230 		wpa_s = wpa_supplicant_get_iface(global, ifname);
14231 		if (wpa_s) {
14232 			if (!fst_iface_detach(ifname)) {
14233 				wpa_s->fst = NULL;
14234 				return os_snprintf(buf, reply_size, "OK\n");
14235 			}
14236 		}
14237 	}
14238 
14239 	return -1;
14240 }
14241 
14242 #endif /* CONFIG_FST */
14243 
14244 
wpa_supplicant_global_ctrl_iface_process(struct wpa_global * global,char * buf,size_t * resp_len)14245 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
14246 						char *buf, size_t *resp_len)
14247 {
14248 	char *reply;
14249 	const int reply_size = 2048;
14250 	int reply_len;
14251 	int level = MSG_DEBUG;
14252 
14253 	if (os_strncmp(buf, "IFNAME=", 7) == 0) {
14254 		char *pos = os_strchr(buf + 7, ' ');
14255 		if (pos) {
14256 			*pos++ = '\0';
14257 			return wpas_global_ctrl_iface_ifname(global,
14258 							     buf + 7, pos,
14259 							     resp_len);
14260 		}
14261 	}
14262 
14263 	reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
14264 	if (reply)
14265 		return reply;
14266 
14267 	if (os_strcmp(buf, "PING") == 0)
14268 		level = MSG_EXCESSIVE;
14269 	wpa_hexdump_ascii(level, "RX global ctrl_iface",
14270 			  (const u8 *) buf, os_strlen(buf));
14271 
14272 	reply = os_malloc(reply_size);
14273 	if (reply == NULL) {
14274 		*resp_len = 1;
14275 		return NULL;
14276 	}
14277 
14278 	os_memcpy(reply, "OK\n", 3);
14279 	reply_len = 3;
14280 
14281 	if (os_strcmp(buf, "PING") == 0) {
14282 		os_memcpy(reply, "PONG\n", 5);
14283 		reply_len = 5;
14284 	} else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
14285 		if (wpa_supplicant_global_iface_add(global, buf + 14))
14286 			reply_len = -1;
14287 	} else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
14288 		if (wpa_supplicant_global_iface_remove(global, buf + 17))
14289 			reply_len = -1;
14290 	} else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
14291 		reply_len = wpa_supplicant_global_iface_list(
14292 			global, reply, reply_size);
14293 	} else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
14294 		reply_len = wpa_supplicant_global_iface_interfaces(
14295 			global, buf + 10, reply, reply_size);
14296 #ifdef CONFIG_FST
14297 	} else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
14298 		reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
14299 							      reply,
14300 							      reply_size);
14301 	} else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
14302 		reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
14303 							      reply,
14304 							      reply_size);
14305 	} else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
14306 		reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
14307 #endif /* CONFIG_FST */
14308 	} else if (os_strcmp(buf, "TERMINATE") == 0) {
14309 		wpa_supplicant_terminate_proc(global);
14310 	} else if (os_strcmp(buf, "SUSPEND") == 0) {
14311 		wpas_notify_suspend(global);
14312 	} else if (os_strcmp(buf, "RESUME") == 0) {
14313 		wpas_notify_resume(global);
14314 	} else if (os_strncmp(buf, "SET ", 4) == 0) {
14315 		if (wpas_global_ctrl_iface_set(global, buf + 4)) {
14316 #ifdef CONFIG_P2P
14317 			if (global->p2p_init_wpa_s) {
14318 				os_free(reply);
14319 				/* Check if P2P redirection would work for this
14320 				 * command. */
14321 				return wpa_supplicant_ctrl_iface_process(
14322 					global->p2p_init_wpa_s,
14323 					buf, resp_len);
14324 			}
14325 #endif /* CONFIG_P2P */
14326 			reply_len = -1;
14327 		}
14328 	} else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
14329 		if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
14330 			reply_len = -1;
14331 #ifndef CONFIG_NO_CONFIG_WRITE
14332 	} else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
14333 		if (wpas_global_ctrl_iface_save_config(global))
14334 			reply_len = -1;
14335 #endif /* CONFIG_NO_CONFIG_WRITE */
14336 	} else if (os_strcmp(buf, "STATUS") == 0) {
14337 		reply_len = wpas_global_ctrl_iface_status(global, reply,
14338 							  reply_size);
14339 #ifdef CONFIG_MODULE_TESTS
14340 	} else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
14341 		if (wpas_module_tests() < 0)
14342 			reply_len = -1;
14343 #endif /* CONFIG_MODULE_TESTS */
14344 	} else if (os_strncmp(buf, "RELOG", 5) == 0) {
14345 		if (wpa_debug_reopen_file() < 0)
14346 			reply_len = -1;
14347 	} else {
14348 		os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
14349 		reply_len = 16;
14350 	}
14351 
14352 	if (reply_len < 0) {
14353 		os_memcpy(reply, "FAIL\n", 5);
14354 		reply_len = 5;
14355 	}
14356 
14357 	*resp_len = reply_len;
14358 	return reply;
14359 }
14360