• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * WPA Supplicant / Control interface (shared code for all backends)
3  * Copyright (c) 2004-2020, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 #ifdef CONFIG_TESTING_OPTIONS
11 #include <netinet/ip.h>
12 #endif /* CONFIG_TESTING_OPTIONS */
13 
14 #include "utils/common.h"
15 #include "utils/eloop.h"
16 #include "utils/uuid.h"
17 #include "utils/module_tests.h"
18 #include "common/version.h"
19 #include "common/ieee802_11_defs.h"
20 #include "common/ieee802_11_common.h"
21 #include "common/wpa_ctrl.h"
22 #ifdef CONFIG_DPP
23 #include "common/dpp.h"
24 #endif /* CONFIG_DPP */
25 #include "common/ptksa_cache.h"
26 #include "crypto/tls.h"
27 #include "ap/hostapd.h"
28 #include "eap_peer/eap.h"
29 #include "eapol_supp/eapol_supp_sm.h"
30 #include "rsn_supp/wpa.h"
31 #include "rsn_supp/preauth.h"
32 #include "rsn_supp/pmksa_cache.h"
33 #include "l2_packet/l2_packet.h"
34 #include "wps/wps.h"
35 #include "fst/fst.h"
36 #include "fst/fst_ctrl_iface.h"
37 #include "config.h"
38 #include "wpa_supplicant_i.h"
39 #include "driver_i.h"
40 #include "wps_supplicant.h"
41 #include "ibss_rsn.h"
42 #include "wpas_glue.h"
43 #include "ap.h"
44 #include "p2p_supplicant.h"
45 #include "p2p/p2p.h"
46 #include "hs20_supplicant.h"
47 #include "wifi_display.h"
48 #include "notify.h"
49 #include "bss.h"
50 #include "scan.h"
51 #include "ctrl_iface.h"
52 #include "interworking.h"
53 #include "bssid_ignore.h"
54 #include "autoscan.h"
55 #include "wnm_sta.h"
56 #include "offchannel.h"
57 #include "drivers/driver.h"
58 #include "mesh.h"
59 #include "dpp_supplicant.h"
60 #include "sme.h"
61 #ifdef CONFIG_MAGICLINK
62 #include "wpa_magiclink.h"
63 #endif
64 
65 #ifdef __NetBSD__
66 #include <net/if_ether.h>
67 #elif !defined(__CYGWIN__) && !defined(CONFIG_NATIVE_WINDOWS)
68 #include <net/ethernet.h>
69 #endif
70 
71 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
72 					    char *buf, int len);
73 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
74 						  const char *input,
75 						  char *buf, int len);
76 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
77 					char *val);
78 
79 
set_bssid_filter(struct wpa_supplicant * wpa_s,char * val)80 static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
81 {
82 	char *pos;
83 	u8 addr[ETH_ALEN], *filter = NULL, *n;
84 	size_t count = 0;
85 
86 	pos = val;
87 	while (pos) {
88 		if (*pos == '\0')
89 			break;
90 		if (hwaddr_aton(pos, addr)) {
91 			os_free(filter);
92 			return -1;
93 		}
94 		n = os_realloc_array(filter, count + 1, ETH_ALEN);
95 		if (n == NULL) {
96 			os_free(filter);
97 			return -1;
98 		}
99 		filter = n;
100 		os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
101 		count++;
102 
103 		pos = os_strchr(pos, ' ');
104 		if (pos)
105 			pos++;
106 	}
107 
108 	wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
109 	os_free(wpa_s->bssid_filter);
110 	wpa_s->bssid_filter = filter;
111 	wpa_s->bssid_filter_count = count;
112 
113 	return 0;
114 }
115 
116 
set_disallow_aps(struct wpa_supplicant * wpa_s,char * val)117 static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
118 {
119 	char *pos;
120 	u8 addr[ETH_ALEN], *bssid = NULL, *n;
121 	struct wpa_ssid_value *ssid = NULL, *ns;
122 	size_t count = 0, ssid_count = 0;
123 	struct wpa_ssid *c;
124 
125 	/*
126 	 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
127 	 * SSID_SPEC ::= ssid <SSID_HEX>
128 	 * BSSID_SPEC ::= bssid <BSSID_HEX>
129 	 */
130 
131 	pos = val;
132 	while (pos) {
133 		if (*pos == '\0')
134 			break;
135 		if (os_strncmp(pos, "bssid ", 6) == 0) {
136 			int res;
137 			pos += 6;
138 			res = hwaddr_aton2(pos, addr);
139 			if (res < 0) {
140 				os_free(ssid);
141 				os_free(bssid);
142 				wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
143 					   "BSSID value '%s'", pos);
144 				return -1;
145 			}
146 			pos += res;
147 			n = os_realloc_array(bssid, count + 1, ETH_ALEN);
148 			if (n == NULL) {
149 				os_free(ssid);
150 				os_free(bssid);
151 				return -1;
152 			}
153 			bssid = n;
154 			os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
155 			count++;
156 		} else if (os_strncmp(pos, "ssid ", 5) == 0) {
157 			char *end;
158 			pos += 5;
159 
160 			end = pos;
161 			while (*end) {
162 				if (*end == '\0' || *end == ' ')
163 					break;
164 				end++;
165 			}
166 
167 			ns = os_realloc_array(ssid, ssid_count + 1,
168 					      sizeof(struct wpa_ssid_value));
169 			if (ns == NULL) {
170 				os_free(ssid);
171 				os_free(bssid);
172 				return -1;
173 			}
174 			ssid = ns;
175 
176 			if ((end - pos) & 0x01 ||
177 			    end - pos > 2 * SSID_MAX_LEN ||
178 			    hexstr2bin(pos, ssid[ssid_count].ssid,
179 				       (end - pos) / 2) < 0) {
180 				os_free(ssid);
181 				os_free(bssid);
182 				wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
183 					   "SSID value '%s'", pos);
184 				return -1;
185 			}
186 			ssid[ssid_count].ssid_len = (end - pos) / 2;
187 			wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
188 					  ssid[ssid_count].ssid,
189 					  ssid[ssid_count].ssid_len);
190 			ssid_count++;
191 			pos = end;
192 		} else {
193 			wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
194 				   "'%s'", pos);
195 			os_free(ssid);
196 			os_free(bssid);
197 			return -1;
198 		}
199 
200 		pos = os_strchr(pos, ' ');
201 		if (pos)
202 			pos++;
203 	}
204 
205 	wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
206 	os_free(wpa_s->disallow_aps_bssid);
207 	wpa_s->disallow_aps_bssid = bssid;
208 	wpa_s->disallow_aps_bssid_count = count;
209 
210 	wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
211 	os_free(wpa_s->disallow_aps_ssid);
212 	wpa_s->disallow_aps_ssid = ssid;
213 	wpa_s->disallow_aps_ssid_count = ssid_count;
214 
215 	if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
216 		return 0;
217 
218 	c = wpa_s->current_ssid;
219 	if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
220 		return 0;
221 
222 	if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
223 	    !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
224 		return 0;
225 
226 	wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
227 		   "because current AP was marked disallowed");
228 
229 #ifdef CONFIG_SME
230 	wpa_s->sme.prev_bssid_set = 0;
231 #endif /* CONFIG_SME */
232 	wpa_s->reassociate = 1;
233 	wpa_s->own_disconnect_req = 1;
234 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
235 	wpa_supplicant_req_scan(wpa_s, 0, 0);
236 
237 	return 0;
238 }
239 
240 
241 #ifndef CONFIG_NO_CONFIG_BLOBS
wpas_ctrl_set_blob(struct wpa_supplicant * wpa_s,char * pos)242 static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
243 {
244 	char *name = pos;
245 	struct wpa_config_blob *blob;
246 	size_t len;
247 
248 	pos = os_strchr(pos, ' ');
249 	if (pos == NULL)
250 		return -1;
251 	*pos++ = '\0';
252 	len = os_strlen(pos);
253 	if (len & 1)
254 		return -1;
255 
256 	wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
257 	blob = os_zalloc(sizeof(*blob));
258 	if (blob == NULL)
259 		return -1;
260 	blob->name = os_strdup(name);
261 	blob->data = os_malloc(len / 2);
262 	if (blob->name == NULL || blob->data == NULL) {
263 		wpa_config_free_blob(blob);
264 		return -1;
265 	}
266 
267 	if (hexstr2bin(pos, blob->data, len / 2) < 0) {
268 		wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
269 		wpa_config_free_blob(blob);
270 		return -1;
271 	}
272 	blob->len = len / 2;
273 
274 	wpa_config_set_blob(wpa_s->conf, blob);
275 
276 	return 0;
277 }
278 #endif /* CONFIG_NO_CONFIG_BLOBS */
279 
280 
wpas_ctrl_pno(struct wpa_supplicant * wpa_s,char * cmd)281 static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
282 {
283 	char *params;
284 	char *pos;
285 	int *freqs = NULL;
286 	int ret;
287 
288 	if (atoi(cmd)) {
289 		params = os_strchr(cmd, ' ');
290 		os_free(wpa_s->manual_sched_scan_freqs);
291 		if (params) {
292 			params++;
293 			pos = os_strstr(params, "freq=");
294 			if (pos)
295 				freqs = freq_range_to_channel_list(wpa_s,
296 								   pos + 5);
297 		}
298 		wpa_s->manual_sched_scan_freqs = freqs;
299 		ret = wpas_start_pno(wpa_s);
300 	} else {
301 		ret = wpas_stop_pno(wpa_s);
302 	}
303 	return ret;
304 }
305 
306 
wpas_ctrl_set_band(struct wpa_supplicant * wpa_s,char * bands)307 static int wpas_ctrl_set_band(struct wpa_supplicant *wpa_s, char *bands)
308 {
309 	union wpa_event_data event;
310 	u32 setband_mask = WPA_SETBAND_AUTO;
311 
312 	/*
313 	 * For example:
314 	 *  SET setband 2G,6G
315 	 *  SET setband 5G
316 	 *  SET setband AUTO
317 	 */
318 	if (!os_strstr(bands, "AUTO")) {
319 		if (os_strstr(bands, "5G"))
320 			setband_mask |= WPA_SETBAND_5G;
321 		if (os_strstr(bands, "6G"))
322 			setband_mask |= WPA_SETBAND_6G;
323 		if (os_strstr(bands, "2G"))
324 			setband_mask |= WPA_SETBAND_2G;
325 		if (setband_mask == WPA_SETBAND_AUTO)
326 			return -1;
327 	}
328 
329 	wpa_s->setband_mask = setband_mask;
330 	if (wpa_drv_setband(wpa_s, wpa_s->setband_mask) == 0) {
331 		os_memset(&event, 0, sizeof(event));
332 		event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
333 		event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
334 		wpa_supplicant_event(wpa_s, EVENT_CHANNEL_LIST_CHANGED, &event);
335 	}
336 
337 	return 0;
338 }
339 
340 
wpas_ctrl_iface_set_lci(struct wpa_supplicant * wpa_s,const char * cmd)341 static int wpas_ctrl_iface_set_lci(struct wpa_supplicant *wpa_s,
342 				   const char *cmd)
343 {
344 	struct wpabuf *lci;
345 
346 	if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
347 		wpabuf_free(wpa_s->lci);
348 		wpa_s->lci = NULL;
349 		return 0;
350 	}
351 
352 	lci = wpabuf_parse_bin(cmd);
353 	if (!lci)
354 		return -1;
355 
356 	if (os_get_reltime(&wpa_s->lci_time)) {
357 		wpabuf_free(lci);
358 		return -1;
359 	}
360 
361 	wpabuf_free(wpa_s->lci);
362 	wpa_s->lci = lci;
363 
364 	return 0;
365 }
366 
367 
368 static int
wpas_ctrl_set_relative_rssi(struct wpa_supplicant * wpa_s,const char * cmd)369 wpas_ctrl_set_relative_rssi(struct wpa_supplicant *wpa_s, const char *cmd)
370 {
371 	int relative_rssi;
372 
373 	if (os_strcmp(cmd, "disable") == 0) {
374 		wpa_s->srp.relative_rssi_set = 0;
375 		return 0;
376 	}
377 
378 	relative_rssi = atoi(cmd);
379 	if (relative_rssi < 0 || relative_rssi > 100)
380 		return -1;
381 	wpa_s->srp.relative_rssi = relative_rssi;
382 	wpa_s->srp.relative_rssi_set = 1;
383 	return 0;
384 }
385 
386 
wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant * wpa_s,const char * cmd)387 static int wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant *wpa_s,
388 					      const char *cmd)
389 {
390 	char *pos;
391 	int adjust_rssi;
392 
393 	/* <band>:adjust_value */
394 	pos = os_strchr(cmd, ':');
395 	if (!pos)
396 		return -1;
397 	pos++;
398 	adjust_rssi = atoi(pos);
399 	if (adjust_rssi < -100 || adjust_rssi > 100)
400 		return -1;
401 
402 	if (os_strncmp(cmd, "2G", 2) == 0)
403 		wpa_s->srp.relative_adjust_band = WPA_SETBAND_2G;
404 	else if (os_strncmp(cmd, "5G", 2) == 0)
405 		wpa_s->srp.relative_adjust_band = WPA_SETBAND_5G;
406 	else
407 		return -1;
408 
409 	wpa_s->srp.relative_adjust_rssi = adjust_rssi;
410 
411 	return 0;
412 }
413 
414 
wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant * wpa_s,const char * cmd)415 static int wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant *wpa_s,
416 				   const char *cmd)
417 {
418 	struct wpabuf *ric_ies;
419 
420 	if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
421 		wpabuf_free(wpa_s->ric_ies);
422 		wpa_s->ric_ies = NULL;
423 		return 0;
424 	}
425 
426 	ric_ies = wpabuf_parse_bin(cmd);
427 	if (!ric_ies)
428 		return -1;
429 
430 	wpabuf_free(wpa_s->ric_ies);
431 	wpa_s->ric_ies = ric_ies;
432 
433 	return 0;
434 }
435 
436 
437 #ifdef CONFIG_TESTING_OPTIONS
wpas_ctrl_iface_set_dso(struct wpa_supplicant * wpa_s,const char * val)438 static int wpas_ctrl_iface_set_dso(struct wpa_supplicant *wpa_s,
439 				   const char *val)
440 {
441 	u8 bssid[ETH_ALEN];
442 	const char *pos = val;
443 	struct driver_signal_override *dso = NULL, *tmp, parsed;
444 
445 	if (hwaddr_aton(pos, bssid))
446 		return -1;
447 	pos = os_strchr(pos, ' ');
448 
449 	dl_list_for_each(tmp, &wpa_s->drv_signal_override,
450 			 struct driver_signal_override, list) {
451 		if (os_memcmp(bssid, tmp->bssid, ETH_ALEN) == 0) {
452 			dso = tmp;
453 			break;
454 		}
455 	}
456 
457 	if (!pos) {
458 		/* Remove existing entry */
459 		if (dso) {
460 			dl_list_del(&dso->list);
461 			os_free(dso);
462 		}
463 		return 0;
464 	}
465 	pos++;
466 
467 	/* Update an existing entry or add a new one */
468 	os_memset(&parsed, 0, sizeof(parsed));
469 	if (sscanf(pos, "%d %d %d %d %d",
470 		   &parsed.si_current_signal,
471 		   &parsed.si_avg_signal,
472 		   &parsed.si_avg_beacon_signal,
473 		   &parsed.si_current_noise,
474 		   &parsed.scan_level) != 5)
475 		return -1;
476 
477 	if (!dso) {
478 		dso = os_zalloc(sizeof(*dso));
479 		if (!dso)
480 			return -1;
481 		os_memcpy(dso->bssid, bssid, ETH_ALEN);
482 		dl_list_add(&wpa_s->drv_signal_override, &dso->list);
483 	}
484 	dso->si_current_signal = parsed.si_current_signal;
485 	dso->si_avg_signal = parsed.si_avg_signal;
486 	dso->si_avg_beacon_signal = parsed.si_avg_beacon_signal;
487 	dso->si_current_noise = parsed.si_current_noise;
488 	dso->scan_level = parsed.scan_level;
489 
490 	return 0;
491 }
492 #endif /* CONFIG_TESTING_OPTIONS */
493 
494 
wpa_supplicant_ctrl_iface_set(struct wpa_supplicant * wpa_s,char * cmd)495 static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
496 					 char *cmd)
497 {
498 	char *value;
499 	int ret = 0;
500 
501 	value = os_strchr(cmd, ' ');
502 	if (value == NULL)
503 		return -1;
504 	*value++ = '\0';
505 
506 	wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
507 	if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
508 		eapol_sm_configure(wpa_s->eapol,
509 				   atoi(value), -1, -1, -1);
510 	} else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
511 		eapol_sm_configure(wpa_s->eapol,
512 				   -1, atoi(value), -1, -1);
513 	} else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
514 		eapol_sm_configure(wpa_s->eapol,
515 				   -1, -1, atoi(value), -1);
516 	} else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
517 		eapol_sm_configure(wpa_s->eapol,
518 				   -1, -1, -1, atoi(value));
519 #ifdef CONFIG_TESTING_OPTIONS
520 	} else if (os_strcasecmp(cmd, "EAPOL::portControl") == 0) {
521 		if (os_strcmp(value, "Auto") == 0)
522 			eapol_sm_notify_portControl(wpa_s->eapol, Auto);
523 		else if (os_strcmp(value, "ForceUnauthorized") == 0)
524 			eapol_sm_notify_portControl(wpa_s->eapol,
525 						    ForceUnauthorized);
526 		else if (os_strcmp(value, "ForceAuthorized") == 0)
527 			eapol_sm_notify_portControl(wpa_s->eapol,
528 						    ForceAuthorized);
529 		else
530 			ret = -1;
531 #endif /* CONFIG_TESTING_OPTIONS */
532 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
533 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
534 				     atoi(value))) {
535 			ret = -1;
536 		} else {
537 			value[-1] = '=';
538 			wpa_config_process_global(wpa_s->conf, cmd, -1);
539 		}
540 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
541 		   0) {
542 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
543 				     atoi(value))) {
544 			ret = -1;
545 		} else {
546 			value[-1] = '=';
547 			wpa_config_process_global(wpa_s->conf, cmd, -1);
548 		}
549 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
550 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
551 				     atoi(value))) {
552 			ret = -1;
553 		} else {
554 			value[-1] = '=';
555 			wpa_config_process_global(wpa_s->conf, cmd, -1);
556 		}
557 	} else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
558 		wpa_s->wps_fragment_size = atoi(value);
559 #ifdef CONFIG_WPS_TESTING
560 	} else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
561 		long int val;
562 		val = strtol(value, NULL, 0);
563 		if (val < 0 || val > 0xff) {
564 			ret = -1;
565 			wpa_printf(MSG_DEBUG, "WPS: Invalid "
566 				   "wps_version_number %ld", val);
567 		} else {
568 			wps_version_number = val;
569 			wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
570 				   "version %u.%u",
571 				   (wps_version_number & 0xf0) >> 4,
572 				   wps_version_number & 0x0f);
573 		}
574 	} else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) {
575 		wps_testing_stub_cred = atoi(value);
576 		wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d",
577 			   wps_testing_stub_cred);
578 	} else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
579 		wps_corrupt_pkhash = atoi(value);
580 		wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
581 			   wps_corrupt_pkhash);
582 	} else if (os_strcasecmp(cmd, "wps_force_auth_types") == 0) {
583 		if (value[0] == '\0') {
584 			wps_force_auth_types_in_use = 0;
585 		} else {
586 			wps_force_auth_types = strtol(value, NULL, 0);
587 			wps_force_auth_types_in_use = 1;
588 		}
589 	} else if (os_strcasecmp(cmd, "wps_force_encr_types") == 0) {
590 		if (value[0] == '\0') {
591 			wps_force_encr_types_in_use = 0;
592 		} else {
593 			wps_force_encr_types = strtol(value, NULL, 0);
594 			wps_force_encr_types_in_use = 1;
595 		}
596 #endif /* CONFIG_WPS_TESTING */
597 	} else if (os_strcasecmp(cmd, "ampdu") == 0) {
598 		if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
599 			ret = -1;
600 #ifdef CONFIG_TDLS
601 #ifdef CONFIG_TDLS_TESTING
602 	} else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
603 		tdls_testing = strtol(value, NULL, 0);
604 		wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
605 #endif /* CONFIG_TDLS_TESTING */
606 	} else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
607 		int disabled = atoi(value);
608 		wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
609 		if (disabled) {
610 			if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
611 				ret = -1;
612 		} else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
613 			ret = -1;
614 		wpa_tdls_enable(wpa_s->wpa, !disabled);
615 #endif /* CONFIG_TDLS */
616 	} else if (os_strcasecmp(cmd, "pno") == 0) {
617 		ret = wpas_ctrl_pno(wpa_s, value);
618 	} else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
619 		int disabled = atoi(value);
620 		if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
621 			ret = -1;
622 		else if (disabled)
623 			wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
624 	} else if (os_strcasecmp(cmd, "uapsd") == 0) {
625 		if (os_strcmp(value, "disable") == 0)
626 			wpa_s->set_sta_uapsd = 0;
627 		else {
628 			int be, bk, vi, vo;
629 			char *pos;
630 			/* format: BE,BK,VI,VO;max SP Length */
631 			be = atoi(value);
632 			pos = os_strchr(value, ',');
633 			if (pos == NULL)
634 				return -1;
635 			pos++;
636 			bk = atoi(pos);
637 			pos = os_strchr(pos, ',');
638 			if (pos == NULL)
639 				return -1;
640 			pos++;
641 			vi = atoi(pos);
642 			pos = os_strchr(pos, ',');
643 			if (pos == NULL)
644 				return -1;
645 			pos++;
646 			vo = atoi(pos);
647 			/* ignore max SP Length for now */
648 
649 			wpa_s->set_sta_uapsd = 1;
650 			wpa_s->sta_uapsd = 0;
651 			if (be)
652 				wpa_s->sta_uapsd |= BIT(0);
653 			if (bk)
654 				wpa_s->sta_uapsd |= BIT(1);
655 			if (vi)
656 				wpa_s->sta_uapsd |= BIT(2);
657 			if (vo)
658 				wpa_s->sta_uapsd |= BIT(3);
659 		}
660 	} else if (os_strcasecmp(cmd, "ps") == 0) {
661 		ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
662 #ifdef CONFIG_WIFI_DISPLAY
663 	} else if (os_strcasecmp(cmd, "wifi_display") == 0) {
664 		int enabled = !!atoi(value);
665 		if (enabled && !wpa_s->global->p2p)
666 			ret = -1;
667 		else
668 			wifi_display_enable(wpa_s->global, enabled);
669 #endif /* CONFIG_WIFI_DISPLAY */
670 	} else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
671 		ret = set_bssid_filter(wpa_s, value);
672 	} else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
673 		ret = set_disallow_aps(wpa_s, value);
674 	} else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
675 		wpa_s->no_keep_alive = !!atoi(value);
676 #ifdef CONFIG_DPP
677 	} else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
678 		os_free(wpa_s->dpp_configurator_params);
679 		wpa_s->dpp_configurator_params = os_strdup(value);
680 	} else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
681 		wpa_s->dpp_init_max_tries = atoi(value);
682 	} else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
683 		wpa_s->dpp_init_retry_time = atoi(value);
684 	} else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
685 		wpa_s->dpp_resp_wait_time = atoi(value);
686 	} else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
687 		wpa_s->dpp_resp_max_tries = atoi(value);
688 	} else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
689 		wpa_s->dpp_resp_retry_time = atoi(value);
690 #ifdef CONFIG_TESTING_OPTIONS
691 	} else if (os_strcasecmp(cmd, "dpp_pkex_own_mac_override") == 0) {
692 		if (hwaddr_aton(value, dpp_pkex_own_mac_override))
693 			ret = -1;
694 	} else if (os_strcasecmp(cmd, "dpp_pkex_peer_mac_override") == 0) {
695 		if (hwaddr_aton(value, dpp_pkex_peer_mac_override))
696 			ret = -1;
697 	} else if (os_strcasecmp(cmd, "dpp_pkex_ephemeral_key_override") == 0) {
698 		size_t hex_len = os_strlen(value);
699 
700 		if (hex_len >
701 		    2 * sizeof(dpp_pkex_ephemeral_key_override))
702 			ret = -1;
703 		else if (hexstr2bin(value, dpp_pkex_ephemeral_key_override,
704 				    hex_len / 2))
705 			ret = -1;
706 		else
707 			dpp_pkex_ephemeral_key_override_len = hex_len / 2;
708 	} else if (os_strcasecmp(cmd, "dpp_protocol_key_override") == 0) {
709 		size_t hex_len = os_strlen(value);
710 
711 		if (hex_len > 2 * sizeof(dpp_protocol_key_override))
712 			ret = -1;
713 		else if (hexstr2bin(value, dpp_protocol_key_override,
714 				    hex_len / 2))
715 			ret = -1;
716 		else
717 			dpp_protocol_key_override_len = hex_len / 2;
718 	} else if (os_strcasecmp(cmd, "dpp_nonce_override") == 0) {
719 		size_t hex_len = os_strlen(value);
720 
721 		if (hex_len > 2 * sizeof(dpp_nonce_override))
722 			ret = -1;
723 		else if (hexstr2bin(value, dpp_nonce_override, hex_len / 2))
724 			ret = -1;
725 		else
726 			dpp_nonce_override_len = hex_len / 2;
727 	} else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
728 		dpp_version_override = atoi(value);
729 #endif /* CONFIG_TESTING_OPTIONS */
730 #endif /* CONFIG_DPP */
731 #ifdef CONFIG_TESTING_OPTIONS
732 	} else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
733 		wpa_s->ext_mgmt_frame_handling = !!atoi(value);
734 	} else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
735 		wpa_s->ext_eapol_frame_io = !!atoi(value);
736 #ifdef CONFIG_AP
737 		if (wpa_s->ap_iface) {
738 			wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
739 				wpa_s->ext_eapol_frame_io;
740 		}
741 #endif /* CONFIG_AP */
742 	} else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
743 		wpa_s->extra_roc_dur = atoi(value);
744 	} else if (os_strcasecmp(cmd, "test_failure") == 0) {
745 		wpa_s->test_failure = atoi(value);
746 	} else if (os_strcasecmp(cmd, "p2p_go_csa_on_inv") == 0) {
747 		wpa_s->p2p_go_csa_on_inv = !!atoi(value);
748 	} else if (os_strcasecmp(cmd, "ignore_auth_resp") == 0) {
749 		wpa_s->ignore_auth_resp = !!atoi(value);
750 	} else if (os_strcasecmp(cmd, "ignore_assoc_disallow") == 0) {
751 		wpa_s->ignore_assoc_disallow = !!atoi(value);
752 		wpa_drv_ignore_assoc_disallow(wpa_s,
753 					      wpa_s->ignore_assoc_disallow);
754 	} else if (os_strcasecmp(cmd, "disable_sa_query") == 0) {
755 		wpa_s->disable_sa_query = !!atoi(value);
756 	} else if (os_strcasecmp(cmd, "ignore_sae_h2e_only") == 0) {
757 		wpa_s->ignore_sae_h2e_only = !!atoi(value);
758 	} else if (os_strcasecmp(cmd, "extra_sae_rejected_groups") == 0) {
759 		char *pos;
760 
761 		os_free(wpa_s->extra_sae_rejected_groups);
762 		wpa_s->extra_sae_rejected_groups = NULL;
763 		pos = value;
764 		while (pos && pos[0]) {
765 			int group;
766 
767 			group = atoi(pos);
768 			wpa_printf(MSG_DEBUG,
769 				   "TESTING: Extra rejection of SAE group %d",
770 				   group);
771 			if (group)
772 				int_array_add_unique(
773 					&wpa_s->extra_sae_rejected_groups,
774 					group);
775 			pos = os_strchr(pos, ' ');
776 			if (!pos)
777 				break;
778 			pos++;
779 		}
780 	} else if (os_strcasecmp(cmd, "ft_rsnxe_used") == 0) {
781 		wpa_s->ft_rsnxe_used = atoi(value);
782 	} else if (os_strcasecmp(cmd, "oci_freq_override_eapol") == 0) {
783 		wpa_s->oci_freq_override_eapol = atoi(value);
784 	} else if (os_strcasecmp(cmd, "oci_freq_override_saquery_req") == 0) {
785 		wpa_s->oci_freq_override_saquery_req = atoi(value);
786 	} else if (os_strcasecmp(cmd, "oci_freq_override_saquery_resp") == 0) {
787 		wpa_s->oci_freq_override_saquery_resp = atoi(value);
788 	} else if (os_strcasecmp(cmd, "oci_freq_override_eapol_g2") == 0) {
789 		wpa_s->oci_freq_override_eapol_g2 = atoi(value);
790 		/* Populate value to wpa_sm if already associated. */
791 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_EAPOL_G2,
792 				 wpa_s->oci_freq_override_eapol_g2);
793 	} else if (os_strcasecmp(cmd, "oci_freq_override_ft_assoc") == 0) {
794 		wpa_s->oci_freq_override_ft_assoc = atoi(value);
795 		/* Populate value to wpa_sm if already associated. */
796 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_FT_ASSOC,
797 				 wpa_s->oci_freq_override_ft_assoc);
798 	} else if (os_strcasecmp(cmd, "oci_freq_override_fils_assoc") == 0) {
799 		wpa_s->oci_freq_override_fils_assoc = atoi(value);
800 	} else if (os_strcasecmp(cmd, "oci_freq_override_wnm_sleep") == 0) {
801 		wpa_s->oci_freq_override_wnm_sleep = atoi(value);
802 	} else if (os_strcasecmp(cmd, "rsne_override_eapol") == 0) {
803 		wpabuf_free(wpa_s->rsne_override_eapol);
804 		if (os_strcmp(value, "NULL") == 0)
805 			wpa_s->rsne_override_eapol = NULL;
806 		else
807 			wpa_s->rsne_override_eapol = wpabuf_parse_bin(value);
808 	} else if (os_strcasecmp(cmd, "rsnxe_override_assoc") == 0) {
809 		wpabuf_free(wpa_s->rsnxe_override_assoc);
810 		if (os_strcmp(value, "NULL") == 0)
811 			wpa_s->rsnxe_override_assoc = NULL;
812 		else
813 			wpa_s->rsnxe_override_assoc = wpabuf_parse_bin(value);
814 	} else if (os_strcasecmp(cmd, "rsnxe_override_eapol") == 0) {
815 		wpabuf_free(wpa_s->rsnxe_override_eapol);
816 		if (os_strcmp(value, "NULL") == 0)
817 			wpa_s->rsnxe_override_eapol = NULL;
818 		else
819 			wpa_s->rsnxe_override_eapol = wpabuf_parse_bin(value);
820 	} else if (os_strcasecmp(cmd, "reject_btm_req_reason") == 0) {
821 		wpa_s->reject_btm_req_reason = atoi(value);
822 	} else if (os_strcasecmp(cmd, "get_pref_freq_list_override") == 0) {
823 		os_free(wpa_s->get_pref_freq_list_override);
824 		if (!value[0])
825 			wpa_s->get_pref_freq_list_override = NULL;
826 		else
827 			wpa_s->get_pref_freq_list_override = os_strdup(value);
828 	} else if (os_strcasecmp(cmd, "sae_commit_override") == 0) {
829 		wpabuf_free(wpa_s->sae_commit_override);
830 		if (value[0] == '\0')
831 			wpa_s->sae_commit_override = NULL;
832 		else
833 			wpa_s->sae_commit_override = wpabuf_parse_bin(value);
834 	} else if (os_strcasecmp(cmd, "driver_signal_override") == 0) {
835 		ret = wpas_ctrl_iface_set_dso(wpa_s, value);
836 	} else if (os_strcasecmp(cmd, "disable_scs_support") == 0) {
837 		wpa_s->disable_scs_support = !!atoi(value);
838 	} else if (os_strcasecmp(cmd, "disable_mscs_support") == 0) {
839 		wpa_s->disable_mscs_support = !!atoi(value);
840 #ifdef CONFIG_DPP
841 	} else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
842 		os_free(wpa_s->dpp_config_obj_override);
843 		if (value[0] == '\0')
844 			wpa_s->dpp_config_obj_override = NULL;
845 		else
846 			wpa_s->dpp_config_obj_override = os_strdup(value);
847 	} else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
848 		os_free(wpa_s->dpp_discovery_override);
849 		if (value[0] == '\0')
850 			wpa_s->dpp_discovery_override = NULL;
851 		else
852 			wpa_s->dpp_discovery_override = os_strdup(value);
853 	} else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
854 		os_free(wpa_s->dpp_groups_override);
855 		if (value[0] == '\0')
856 			wpa_s->dpp_groups_override = NULL;
857 		else
858 			wpa_s->dpp_groups_override = os_strdup(value);
859 	} else if (os_strcasecmp(cmd,
860 				 "dpp_ignore_netaccesskey_mismatch") == 0) {
861 		wpa_s->dpp_ignore_netaccesskey_mismatch = atoi(value);
862 	} else if (os_strcasecmp(cmd, "dpp_test") == 0) {
863 		dpp_test = atoi(value);
864 #endif /* CONFIG_DPP */
865 #endif /* CONFIG_TESTING_OPTIONS */
866 #ifdef CONFIG_FILS
867 	} else if (os_strcasecmp(cmd, "disable_fils") == 0) {
868 		wpa_s->disable_fils = !!atoi(value);
869 		wpa_drv_disable_fils(wpa_s, wpa_s->disable_fils);
870 		wpa_supplicant_set_default_scan_ies(wpa_s);
871 #endif /* CONFIG_FILS */
872 #ifndef CONFIG_NO_CONFIG_BLOBS
873 	} else if (os_strcmp(cmd, "blob") == 0) {
874 		ret = wpas_ctrl_set_blob(wpa_s, value);
875 #endif /* CONFIG_NO_CONFIG_BLOBS */
876 	} else if (os_strcasecmp(cmd, "setband") == 0) {
877 		ret = wpas_ctrl_set_band(wpa_s, value);
878 #ifdef CONFIG_MBO
879 	} else if (os_strcasecmp(cmd, "non_pref_chan") == 0) {
880 		ret = wpas_mbo_update_non_pref_chan(wpa_s, value);
881 		if (ret == 0) {
882 			value[-1] = '=';
883 			wpa_config_process_global(wpa_s->conf, cmd, -1);
884 		}
885 	} else if (os_strcasecmp(cmd, "mbo_cell_capa") == 0) {
886 		wpas_mbo_update_cell_capa(wpa_s, atoi(value));
887 	} else if (os_strcasecmp(cmd, "oce") == 0) {
888 		wpa_s->conf->oce = atoi(value);
889 		if (wpa_s->conf->oce) {
890 			if ((wpa_s->conf->oce & OCE_STA) &&
891 			    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA))
892 				wpa_s->enable_oce = OCE_STA;
893 
894 			if ((wpa_s->conf->oce & OCE_STA_CFON) &&
895 			    (wpa_s->drv_flags &
896 			     WPA_DRIVER_FLAGS_OCE_STA_CFON)) {
897 				/* TODO: Need to add STA-CFON support */
898 				wpa_printf(MSG_ERROR,
899 					   "OCE STA-CFON feature is not yet supported");
900 				return -1;
901 			}
902 		} else {
903 			wpa_s->enable_oce = 0;
904 		}
905 		wpa_supplicant_set_default_scan_ies(wpa_s);
906 #endif /* CONFIG_MBO */
907 	} else if (os_strcasecmp(cmd, "lci") == 0) {
908 		ret = wpas_ctrl_iface_set_lci(wpa_s, value);
909 	} else if (os_strcasecmp(cmd, "tdls_trigger_control") == 0) {
910 		ret = wpa_drv_set_tdls_mode(wpa_s, atoi(value));
911 	} else if (os_strcasecmp(cmd, "relative_rssi") == 0) {
912 		ret = wpas_ctrl_set_relative_rssi(wpa_s, value);
913 	} else if (os_strcasecmp(cmd, "relative_band_adjust") == 0) {
914 		ret = wpas_ctrl_set_relative_band_adjust(wpa_s, value);
915 	} else if (os_strcasecmp(cmd, "ric_ies") == 0) {
916 		ret = wpas_ctrl_iface_set_ric_ies(wpa_s, value);
917 	} else if (os_strcasecmp(cmd, "roaming") == 0) {
918 		ret = wpa_drv_roaming(wpa_s, atoi(value), NULL);
919 #ifdef CONFIG_WNM
920 	} else if (os_strcasecmp(cmd, "coloc_intf_elems") == 0) {
921 		struct wpabuf *elems;
922 
923 		elems = wpabuf_parse_bin(value);
924 		if (!elems)
925 			return -1;
926 		wnm_set_coloc_intf_elems(wpa_s, elems);
927 #endif /* CONFIG_WNM */
928 	} else if (os_strcasecmp(cmd, "enable_dscp_policy_capa") == 0) {
929 		wpa_s->enable_dscp_policy_capa = !!atoi(value);
930 	} else {
931 		value[-1] = '=';
932 		ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
933 		if (ret == 0)
934 			wpa_supplicant_update_config(wpa_s);
935 		else if (ret == 1)
936 			ret = 0;
937 	}
938 
939 	return ret;
940 }
941 
942 
wpa_supplicant_ctrl_iface_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)943 static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
944 					 char *cmd, char *buf, size_t buflen)
945 {
946 	int res = -1;
947 
948 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
949 
950 	if (os_strcmp(cmd, "version") == 0) {
951 		res = os_snprintf(buf, buflen, "%s", VERSION_STR);
952 	} else if (os_strcasecmp(cmd, "max_command_len") == 0) {
953 		res = os_snprintf(buf, buflen, "%u", CTRL_IFACE_MAX_LEN);
954 	} else if (os_strcasecmp(cmd, "country") == 0) {
955 		if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
956 			res = os_snprintf(buf, buflen, "%c%c",
957 					  wpa_s->conf->country[0],
958 					  wpa_s->conf->country[1]);
959 #ifdef CONFIG_WIFI_DISPLAY
960 	} else if (os_strcasecmp(cmd, "wifi_display") == 0) {
961 		int enabled;
962 		if (wpa_s->global->p2p == NULL ||
963 		    wpa_s->global->p2p_disabled)
964 			enabled = 0;
965 		else
966 			enabled = wpa_s->global->wifi_display;
967 		res = os_snprintf(buf, buflen, "%d", enabled);
968 #endif /* CONFIG_WIFI_DISPLAY */
969 #ifdef CONFIG_TESTING_GET_GTK
970 	} else if (os_strcmp(cmd, "gtk") == 0) {
971 		if (wpa_s->last_gtk_len == 0)
972 			return -1;
973 		res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
974 				       wpa_s->last_gtk_len);
975 		return res;
976 #endif /* CONFIG_TESTING_GET_GTK */
977 	} else if (os_strcmp(cmd, "tls_library") == 0) {
978 		res = tls_get_library_version(buf, buflen);
979 #ifdef CONFIG_TESTING_OPTIONS
980 	} else if (os_strcmp(cmd, "anonce") == 0) {
981 		return wpa_snprintf_hex(buf, buflen,
982 					wpa_sm_get_anonce(wpa_s->wpa),
983 					WPA_NONCE_LEN);
984 	} else if (os_strcasecmp(cmd, "last_tk_key_idx") == 0) {
985 		res = os_snprintf(buf, buflen, "%d", wpa_s->last_tk_key_idx);
986 #endif /* CONFIG_TESTING_OPTIONS */
987 	} else {
988 		res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
989 	}
990 
991 	if (os_snprintf_error(buflen, res))
992 		return -1;
993 	return res;
994 }
995 
996 
997 #ifdef IEEE8021X_EAPOL
wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant * wpa_s,char * addr)998 static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
999 					     char *addr)
1000 {
1001 	u8 bssid[ETH_ALEN];
1002 	struct wpa_ssid *ssid = wpa_s->current_ssid;
1003 
1004 	if (hwaddr_aton(addr, bssid)) {
1005 		wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
1006 			   "'%s'", addr);
1007 		return -1;
1008 	}
1009 
1010 	wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
1011 	rsn_preauth_deinit(wpa_s->wpa);
1012 	if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
1013 		return -1;
1014 
1015 	return 0;
1016 }
1017 #endif /* IEEE8021X_EAPOL */
1018 
1019 
1020 #ifdef CONFIG_TDLS
1021 
wpa_supplicant_ctrl_iface_tdls_discover(struct wpa_supplicant * wpa_s,char * addr)1022 static int wpa_supplicant_ctrl_iface_tdls_discover(
1023 	struct wpa_supplicant *wpa_s, char *addr)
1024 {
1025 	u8 peer[ETH_ALEN];
1026 	int ret;
1027 
1028 	if (hwaddr_aton(addr, peer)) {
1029 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
1030 			   "address '%s'", addr);
1031 		return -1;
1032 	}
1033 
1034 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
1035 		   MAC2STR(peer));
1036 
1037 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1038 		ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
1039 	else
1040 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
1041 
1042 	return ret;
1043 }
1044 
1045 
wpa_supplicant_ctrl_iface_tdls_setup(struct wpa_supplicant * wpa_s,char * addr)1046 static int wpa_supplicant_ctrl_iface_tdls_setup(
1047 	struct wpa_supplicant *wpa_s, char *addr)
1048 {
1049 	u8 peer[ETH_ALEN];
1050 	int ret;
1051 
1052 	if (hwaddr_aton(addr, peer)) {
1053 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
1054 			   "address '%s'", addr);
1055 		return -1;
1056 	}
1057 
1058 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
1059 		   MAC2STR(peer));
1060 
1061 	if ((wpa_s->conf->tdls_external_control) &&
1062 	    wpa_tdls_is_external_setup(wpa_s->wpa))
1063 		return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
1064 
1065 	wpa_tdls_remove(wpa_s->wpa, peer);
1066 
1067 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1068 		ret = wpa_tdls_start(wpa_s->wpa, peer);
1069 	else
1070 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
1071 
1072 	return ret;
1073 }
1074 
1075 
wpa_supplicant_ctrl_iface_tdls_teardown(struct wpa_supplicant * wpa_s,char * addr)1076 static int wpa_supplicant_ctrl_iface_tdls_teardown(
1077 	struct wpa_supplicant *wpa_s, char *addr)
1078 {
1079 	u8 peer[ETH_ALEN];
1080 	int ret;
1081 
1082 	if (os_strcmp(addr, "*") == 0) {
1083 		/* remove everyone */
1084 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
1085 		wpa_tdls_teardown_peers(wpa_s->wpa);
1086 		return 0;
1087 	}
1088 
1089 	if (hwaddr_aton(addr, peer)) {
1090 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
1091 			   "address '%s'", addr);
1092 		return -1;
1093 	}
1094 
1095 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
1096 		   MAC2STR(peer));
1097 
1098 	if ((wpa_s->conf->tdls_external_control) &&
1099 	    wpa_tdls_is_external_setup(wpa_s->wpa))
1100 		return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
1101 
1102 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1103 		ret = wpa_tdls_teardown_link(
1104 			wpa_s->wpa, peer,
1105 			WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
1106 	else
1107 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
1108 
1109 	return ret;
1110 }
1111 
1112 
ctrl_iface_get_capability_tdls(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)1113 static int ctrl_iface_get_capability_tdls(
1114 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1115 {
1116 	int ret;
1117 
1118 	ret = os_snprintf(buf, buflen, "%s\n",
1119 			  wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
1120 			  (wpa_s->drv_flags &
1121 			   WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
1122 			   "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
1123 	if (os_snprintf_error(buflen, ret))
1124 		return -1;
1125 	return ret;
1126 }
1127 
1128 
wpa_supplicant_ctrl_iface_tdls_chan_switch(struct wpa_supplicant * wpa_s,char * cmd)1129 static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
1130 	struct wpa_supplicant *wpa_s, char *cmd)
1131 {
1132 	u8 peer[ETH_ALEN];
1133 	struct hostapd_freq_params freq_params;
1134 	u8 oper_class;
1135 	char *pos, *end;
1136 
1137 	if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
1138 		wpa_printf(MSG_INFO,
1139 			   "tdls_chanswitch: Only supported with external setup");
1140 		return -1;
1141 	}
1142 
1143 	os_memset(&freq_params, 0, sizeof(freq_params));
1144 
1145 	pos = os_strchr(cmd, ' ');
1146 	if (pos == NULL)
1147 		return -1;
1148 	*pos++ = '\0';
1149 
1150 	oper_class = strtol(pos, &end, 10);
1151 	if (pos == end) {
1152 		wpa_printf(MSG_INFO,
1153 			   "tdls_chanswitch: Invalid op class provided");
1154 		return -1;
1155 	}
1156 
1157 	pos = end;
1158 	freq_params.freq = atoi(pos);
1159 	if (freq_params.freq == 0) {
1160 		wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
1161 		return -1;
1162 	}
1163 
1164 #define SET_FREQ_SETTING(str) \
1165 	do { \
1166 		const char *pos2 = os_strstr(pos, " " #str "="); \
1167 		if (pos2) { \
1168 			pos2 += sizeof(" " #str "=") - 1; \
1169 			freq_params.str = atoi(pos2); \
1170 		} \
1171 	} while (0)
1172 
1173 	SET_FREQ_SETTING(center_freq1);
1174 	SET_FREQ_SETTING(center_freq2);
1175 	SET_FREQ_SETTING(bandwidth);
1176 	SET_FREQ_SETTING(sec_channel_offset);
1177 #undef SET_FREQ_SETTING
1178 
1179 	freq_params.ht_enabled = !!os_strstr(pos, " ht");
1180 	freq_params.vht_enabled = !!os_strstr(pos, " vht");
1181 
1182 	if (hwaddr_aton(cmd, peer)) {
1183 		wpa_printf(MSG_DEBUG,
1184 			   "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
1185 			   cmd);
1186 		return -1;
1187 	}
1188 
1189 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
1190 		   " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
1191 		   MAC2STR(peer), oper_class, freq_params.freq,
1192 		   freq_params.center_freq1, freq_params.center_freq2,
1193 		   freq_params.bandwidth, freq_params.sec_channel_offset,
1194 		   freq_params.ht_enabled ? " HT" : "",
1195 		   freq_params.vht_enabled ? " VHT" : "");
1196 
1197 	return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
1198 					   &freq_params);
1199 }
1200 
1201 
wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(struct wpa_supplicant * wpa_s,char * cmd)1202 static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
1203 	struct wpa_supplicant *wpa_s, char *cmd)
1204 {
1205 	u8 peer[ETH_ALEN];
1206 
1207 	if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
1208 		wpa_printf(MSG_INFO,
1209 			   "tdls_chanswitch: Only supported with external setup");
1210 		return -1;
1211 	}
1212 
1213 	if (hwaddr_aton(cmd, peer)) {
1214 		wpa_printf(MSG_DEBUG,
1215 			   "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
1216 			   cmd);
1217 		return -1;
1218 	}
1219 
1220 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
1221 		   MAC2STR(peer));
1222 
1223 	return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
1224 }
1225 
1226 
wpa_supplicant_ctrl_iface_tdls_link_status(struct wpa_supplicant * wpa_s,const char * addr,char * buf,size_t buflen)1227 static int wpa_supplicant_ctrl_iface_tdls_link_status(
1228 	struct wpa_supplicant *wpa_s, const char *addr,
1229 	char *buf, size_t buflen)
1230 {
1231 	u8 peer[ETH_ALEN];
1232 	const char *tdls_status;
1233 	int ret;
1234 
1235 	if (hwaddr_aton(addr, peer)) {
1236 		wpa_printf(MSG_DEBUG,
1237 			   "CTRL_IFACE TDLS_LINK_STATUS: Invalid address '%s'",
1238 			   addr);
1239 		return -1;
1240 	}
1241 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS " MACSTR,
1242 		   MAC2STR(peer));
1243 
1244 	tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
1245 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS: %s", tdls_status);
1246 	ret = os_snprintf(buf, buflen, "TDLS link status: %s\n", tdls_status);
1247 	if (os_snprintf_error(buflen, ret))
1248 		return -1;
1249 
1250 	return ret;
1251 }
1252 
1253 #endif /* CONFIG_TDLS */
1254 
1255 
wmm_ac_ctrl_addts(struct wpa_supplicant * wpa_s,char * cmd)1256 static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
1257 {
1258 	char *token, *context = NULL;
1259 	struct wmm_ac_ts_setup_params params = {
1260 		.tsid = 0xff,
1261 		.direction = 0xff,
1262 	};
1263 
1264 	while ((token = str_token(cmd, " ", &context))) {
1265 		if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
1266 		    sscanf(token, "up=%i", &params.user_priority) == 1 ||
1267 		    sscanf(token, "nominal_msdu_size=%i",
1268 			   &params.nominal_msdu_size) == 1 ||
1269 		    sscanf(token, "mean_data_rate=%i",
1270 			   &params.mean_data_rate) == 1 ||
1271 		    sscanf(token, "min_phy_rate=%i",
1272 			   &params.minimum_phy_rate) == 1 ||
1273 		    sscanf(token, "sba=%i",
1274 			   &params.surplus_bandwidth_allowance) == 1)
1275 			continue;
1276 
1277 		if (os_strcasecmp(token, "downlink") == 0) {
1278 			params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
1279 		} else if (os_strcasecmp(token, "uplink") == 0) {
1280 			params.direction = WMM_TSPEC_DIRECTION_UPLINK;
1281 		} else if (os_strcasecmp(token, "bidi") == 0) {
1282 			params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
1283 		} else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
1284 			params.fixed_nominal_msdu = 1;
1285 		} else {
1286 			wpa_printf(MSG_DEBUG,
1287 				   "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
1288 				   token);
1289 			return -1;
1290 		}
1291 
1292 	}
1293 
1294 	return wpas_wmm_ac_addts(wpa_s, &params);
1295 }
1296 
1297 
wmm_ac_ctrl_delts(struct wpa_supplicant * wpa_s,char * cmd)1298 static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
1299 {
1300 	u8 tsid = atoi(cmd);
1301 
1302 	return wpas_wmm_ac_delts(wpa_s, tsid);
1303 }
1304 
1305 
1306 #ifdef CONFIG_IEEE80211R
wpa_supplicant_ctrl_iface_ft_ds(struct wpa_supplicant * wpa_s,char * addr)1307 static int wpa_supplicant_ctrl_iface_ft_ds(
1308 	struct wpa_supplicant *wpa_s, char *addr)
1309 {
1310 	u8 target_ap[ETH_ALEN];
1311 	struct wpa_bss *bss;
1312 	const u8 *mdie;
1313 
1314 	if (hwaddr_aton(addr, target_ap)) {
1315 		wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
1316 			   "address '%s'", addr);
1317 		return -1;
1318 	}
1319 
1320 	wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
1321 
1322 	bss = wpa_bss_get_bssid(wpa_s, target_ap);
1323 	if (bss)
1324 		mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1325 	else
1326 		mdie = NULL;
1327 
1328 	return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
1329 }
1330 #endif /* CONFIG_IEEE80211R */
1331 
1332 
1333 #ifdef CONFIG_WPS
wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant * wpa_s,char * cmd)1334 static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
1335 					     char *cmd)
1336 {
1337 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1338 #ifdef CONFIG_P2P
1339 	u8 p2p_dev_addr[ETH_ALEN];
1340 #endif /* CONFIG_P2P */
1341 #ifdef CONFIG_AP
1342 	u8 *_p2p_dev_addr = NULL;
1343 #endif /* CONFIG_AP */
1344 	char *pos;
1345 	int multi_ap = 0;
1346 
1347 	if (!cmd || os_strcmp(cmd, "any") == 0 ||
1348 	    os_strncmp(cmd, "any ", 4) == 0) {
1349 		_bssid = NULL;
1350 #ifdef CONFIG_P2P
1351 	} else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
1352 		if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
1353 			wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
1354 				   "P2P Device Address '%s'",
1355 				   cmd + 13);
1356 			return -1;
1357 		}
1358 		_p2p_dev_addr = p2p_dev_addr;
1359 #endif /* CONFIG_P2P */
1360 	} else if (os_strncmp(cmd, "multi_ap=", 9) == 0) {
1361 		_bssid = NULL;
1362 		multi_ap = atoi(cmd + 9);
1363 	} else if (hwaddr_aton(cmd, bssid)) {
1364 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
1365 			   cmd);
1366 		return -1;
1367 	}
1368 
1369 	if (cmd) {
1370 		pos = os_strstr(cmd, " multi_ap=");
1371 		if (pos) {
1372 			pos += 10;
1373 			multi_ap = atoi(pos);
1374 		}
1375 	}
1376 
1377 #ifdef CONFIG_AP
1378 	if (wpa_s->ap_iface)
1379 		return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
1380 #endif /* CONFIG_AP */
1381 
1382 	return wpas_wps_start_pbc(wpa_s, _bssid, 0, multi_ap);
1383 }
1384 
1385 
wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1386 static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
1387 					     char *cmd, char *buf,
1388 					     size_t buflen)
1389 {
1390 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1391 	char *pin;
1392 	int ret;
1393 
1394 	pin = os_strchr(cmd, ' ');
1395 	if (pin)
1396 		*pin++ = '\0';
1397 
1398 	if (os_strcmp(cmd, "any") == 0)
1399 		_bssid = NULL;
1400 	else if (os_strcmp(cmd, "get") == 0) {
1401 		if (wps_generate_pin((unsigned int *) &ret) < 0)
1402 			return -1;
1403 		goto done;
1404 	} else if (hwaddr_aton(cmd, bssid)) {
1405 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
1406 			   cmd);
1407 		return -1;
1408 	}
1409 
1410 #ifdef CONFIG_AP
1411 	if (wpa_s->ap_iface) {
1412 		int timeout = 0;
1413 		char *pos;
1414 
1415 		if (pin) {
1416 			pos = os_strchr(pin, ' ');
1417 			if (pos) {
1418 				*pos++ = '\0';
1419 				timeout = atoi(pos);
1420 			}
1421 		}
1422 
1423 		return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
1424 						 buf, buflen, timeout);
1425 	}
1426 #endif /* CONFIG_AP */
1427 
1428 	if (pin) {
1429 		ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
1430 					 DEV_PW_DEFAULT);
1431 		if (ret < 0)
1432 			return -1;
1433 		ret = os_snprintf(buf, buflen, "%s", pin);
1434 		if (os_snprintf_error(buflen, ret))
1435 			return -1;
1436 		return ret;
1437 	}
1438 
1439 	ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
1440 	if (ret < 0)
1441 		return -1;
1442 
1443 done:
1444 	/* Return the generated PIN */
1445 	ret = os_snprintf(buf, buflen, "%08d", ret);
1446 	if (os_snprintf_error(buflen, ret))
1447 		return -1;
1448 	return ret;
1449 }
1450 
1451 
wpa_supplicant_ctrl_iface_wps_check_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1452 static int wpa_supplicant_ctrl_iface_wps_check_pin(
1453 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1454 {
1455 	char pin[9];
1456 	size_t len;
1457 	char *pos;
1458 	int ret;
1459 
1460 	wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
1461 			      (u8 *) cmd, os_strlen(cmd));
1462 	for (pos = cmd, len = 0; *pos != '\0'; pos++) {
1463 		if (*pos < '0' || *pos > '9')
1464 			continue;
1465 		pin[len++] = *pos;
1466 		if (len == 9) {
1467 			wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
1468 			return -1;
1469 		}
1470 	}
1471 	if (len != 4 && len != 8) {
1472 		wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
1473 		return -1;
1474 	}
1475 	pin[len] = '\0';
1476 
1477 	if (len == 8) {
1478 		unsigned int pin_val;
1479 		pin_val = atoi(pin);
1480 		if (!wps_pin_valid(pin_val)) {
1481 			wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
1482 			ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
1483 			if (os_snprintf_error(buflen, ret))
1484 				return -1;
1485 			return ret;
1486 		}
1487 	}
1488 
1489 	ret = os_snprintf(buf, buflen, "%s", pin);
1490 	if (os_snprintf_error(buflen, ret))
1491 		return -1;
1492 
1493 	return ret;
1494 }
1495 
1496 
1497 #ifdef CONFIG_WPS_NFC
1498 
wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant * wpa_s,char * cmd)1499 static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
1500 					     char *cmd)
1501 {
1502 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1503 
1504 	if (cmd == NULL || cmd[0] == '\0')
1505 		_bssid = NULL;
1506 	else if (hwaddr_aton(cmd, bssid))
1507 		return -1;
1508 
1509 	return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
1510 				  0, 0);
1511 }
1512 
1513 
wpa_supplicant_ctrl_iface_wps_nfc_config_token(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1514 static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
1515 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1516 {
1517 	int ndef;
1518 	struct wpabuf *buf;
1519 	int res;
1520 	char *pos;
1521 
1522 	pos = os_strchr(cmd, ' ');
1523 	if (pos)
1524 		*pos++ = '\0';
1525 	if (os_strcmp(cmd, "WPS") == 0)
1526 		ndef = 0;
1527 	else if (os_strcmp(cmd, "NDEF") == 0)
1528 		ndef = 1;
1529 	else
1530 		return -1;
1531 
1532 	buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
1533 	if (buf == NULL)
1534 		return -1;
1535 
1536 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1537 					 wpabuf_len(buf));
1538 	reply[res++] = '\n';
1539 	reply[res] = '\0';
1540 
1541 	wpabuf_free(buf);
1542 
1543 	return res;
1544 }
1545 
1546 
wpa_supplicant_ctrl_iface_wps_nfc_token(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1547 static int wpa_supplicant_ctrl_iface_wps_nfc_token(
1548 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1549 {
1550 	int ndef;
1551 	struct wpabuf *buf;
1552 	int res;
1553 
1554 	if (os_strcmp(cmd, "WPS") == 0)
1555 		ndef = 0;
1556 	else if (os_strcmp(cmd, "NDEF") == 0)
1557 		ndef = 1;
1558 	else
1559 		return -1;
1560 
1561 	buf = wpas_wps_nfc_token(wpa_s, ndef);
1562 	if (buf == NULL)
1563 		return -1;
1564 
1565 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1566 					 wpabuf_len(buf));
1567 	reply[res++] = '\n';
1568 	reply[res] = '\0';
1569 
1570 	wpabuf_free(buf);
1571 
1572 	return res;
1573 }
1574 
1575 
wpa_supplicant_ctrl_iface_wps_nfc_tag_read(struct wpa_supplicant * wpa_s,char * pos)1576 static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
1577 	struct wpa_supplicant *wpa_s, char *pos)
1578 {
1579 	size_t len;
1580 	struct wpabuf *buf;
1581 	int ret;
1582 	char *freq;
1583 	int forced_freq = 0;
1584 
1585 	freq = strstr(pos, " freq=");
1586 	if (freq) {
1587 		*freq = '\0';
1588 		freq += 6;
1589 		forced_freq = atoi(freq);
1590 	}
1591 
1592 	len = os_strlen(pos);
1593 	if (len & 0x01)
1594 		return -1;
1595 	len /= 2;
1596 
1597 	buf = wpabuf_alloc(len);
1598 	if (buf == NULL)
1599 		return -1;
1600 	if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
1601 		wpabuf_free(buf);
1602 		return -1;
1603 	}
1604 
1605 	ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
1606 	wpabuf_free(buf);
1607 
1608 	return ret;
1609 }
1610 
1611 
wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef)1612 static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
1613 					      char *reply, size_t max_len,
1614 					      int ndef)
1615 {
1616 	struct wpabuf *buf;
1617 	int res;
1618 
1619 	buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
1620 	if (buf == NULL)
1621 		return -1;
1622 
1623 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1624 					 wpabuf_len(buf));
1625 	reply[res++] = '\n';
1626 	reply[res] = '\0';
1627 
1628 	wpabuf_free(buf);
1629 
1630 	return res;
1631 }
1632 
1633 
1634 #ifdef CONFIG_P2P
wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef)1635 static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
1636 					      char *reply, size_t max_len,
1637 					      int ndef)
1638 {
1639 	struct wpabuf *buf;
1640 	int res;
1641 
1642 	buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
1643 	if (buf == NULL) {
1644 		wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
1645 		return -1;
1646 	}
1647 
1648 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1649 					 wpabuf_len(buf));
1650 	reply[res++] = '\n';
1651 	reply[res] = '\0';
1652 
1653 	wpabuf_free(buf);
1654 
1655 	return res;
1656 }
1657 #endif /* CONFIG_P2P */
1658 
1659 
wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1660 static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
1661 					  char *cmd, char *reply,
1662 					  size_t max_len)
1663 {
1664 	char *pos;
1665 	int ndef;
1666 
1667 	pos = os_strchr(cmd, ' ');
1668 	if (pos == NULL)
1669 		return -1;
1670 	*pos++ = '\0';
1671 
1672 	if (os_strcmp(cmd, "WPS") == 0)
1673 		ndef = 0;
1674 	else if (os_strcmp(cmd, "NDEF") == 0)
1675 		ndef = 1;
1676 	else
1677 		return -1;
1678 
1679 	if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
1680 		if (!ndef)
1681 			return -1;
1682 		return wpas_ctrl_nfc_get_handover_req_wps(
1683 			wpa_s, reply, max_len, ndef);
1684 	}
1685 
1686 #ifdef CONFIG_P2P
1687 	if (os_strcmp(pos, "P2P-CR") == 0) {
1688 		return wpas_ctrl_nfc_get_handover_req_p2p(
1689 			wpa_s, reply, max_len, ndef);
1690 	}
1691 #endif /* CONFIG_P2P */
1692 
1693 	return -1;
1694 }
1695 
1696 
wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef,int cr,char * uuid)1697 static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
1698 					      char *reply, size_t max_len,
1699 					      int ndef, int cr, char *uuid)
1700 {
1701 	struct wpabuf *buf;
1702 	int res;
1703 
1704 	buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
1705 	if (buf == NULL)
1706 		return -1;
1707 
1708 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1709 					 wpabuf_len(buf));
1710 	reply[res++] = '\n';
1711 	reply[res] = '\0';
1712 
1713 	wpabuf_free(buf);
1714 
1715 	return res;
1716 }
1717 
1718 
1719 #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)1720 static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1721 					      char *reply, size_t max_len,
1722 					      int ndef, int tag)
1723 {
1724 	struct wpabuf *buf;
1725 	int res;
1726 
1727 	buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1728 	if (buf == NULL)
1729 		return -1;
1730 
1731 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1732 					 wpabuf_len(buf));
1733 	reply[res++] = '\n';
1734 	reply[res] = '\0';
1735 
1736 	wpabuf_free(buf);
1737 
1738 	return res;
1739 }
1740 #endif /* CONFIG_P2P */
1741 
1742 
wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1743 static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1744 					  char *cmd, char *reply,
1745 					  size_t max_len)
1746 {
1747 	char *pos, *pos2;
1748 	int ndef;
1749 
1750 	pos = os_strchr(cmd, ' ');
1751 	if (pos == NULL)
1752 		return -1;
1753 	*pos++ = '\0';
1754 
1755 	if (os_strcmp(cmd, "WPS") == 0)
1756 		ndef = 0;
1757 	else if (os_strcmp(cmd, "NDEF") == 0)
1758 		ndef = 1;
1759 	else
1760 		return -1;
1761 
1762 	pos2 = os_strchr(pos, ' ');
1763 	if (pos2)
1764 		*pos2++ = '\0';
1765 	if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
1766 		if (!ndef)
1767 			return -1;
1768 		return wpas_ctrl_nfc_get_handover_sel_wps(
1769 			wpa_s, reply, max_len, ndef,
1770 			os_strcmp(pos, "WPS-CR") == 0, pos2);
1771 	}
1772 
1773 #ifdef CONFIG_P2P
1774 	if (os_strcmp(pos, "P2P-CR") == 0) {
1775 		return wpas_ctrl_nfc_get_handover_sel_p2p(
1776 			wpa_s, reply, max_len, ndef, 0);
1777 	}
1778 
1779 	if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1780 		return wpas_ctrl_nfc_get_handover_sel_p2p(
1781 			wpa_s, reply, max_len, ndef, 1);
1782 	}
1783 #endif /* CONFIG_P2P */
1784 
1785 	return -1;
1786 }
1787 
1788 
wpas_ctrl_nfc_report_handover(struct wpa_supplicant * wpa_s,char * cmd)1789 static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1790 					 char *cmd)
1791 {
1792 	size_t len;
1793 	struct wpabuf *req, *sel;
1794 	int ret;
1795 	char *pos, *role, *type, *pos2;
1796 #ifdef CONFIG_P2P
1797 	char *freq;
1798 	int forced_freq = 0;
1799 
1800 	freq = strstr(cmd, " freq=");
1801 	if (freq) {
1802 		*freq = '\0';
1803 		freq += 6;
1804 		forced_freq = atoi(freq);
1805 	}
1806 #endif /* CONFIG_P2P */
1807 
1808 	role = cmd;
1809 	pos = os_strchr(role, ' ');
1810 	if (pos == NULL) {
1811 		wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
1812 		return -1;
1813 	}
1814 	*pos++ = '\0';
1815 
1816 	type = pos;
1817 	pos = os_strchr(type, ' ');
1818 	if (pos == NULL) {
1819 		wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
1820 		return -1;
1821 	}
1822 	*pos++ = '\0';
1823 
1824 	pos2 = os_strchr(pos, ' ');
1825 	if (pos2 == NULL) {
1826 		wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
1827 		return -1;
1828 	}
1829 	*pos2++ = '\0';
1830 
1831 	len = os_strlen(pos);
1832 	if (len & 0x01) {
1833 		wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
1834 		return -1;
1835 	}
1836 	len /= 2;
1837 
1838 	req = wpabuf_alloc(len);
1839 	if (req == NULL) {
1840 		wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
1841 		return -1;
1842 	}
1843 	if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
1844 		wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
1845 		wpabuf_free(req);
1846 		return -1;
1847 	}
1848 
1849 	len = os_strlen(pos2);
1850 	if (len & 0x01) {
1851 		wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
1852 		wpabuf_free(req);
1853 		return -1;
1854 	}
1855 	len /= 2;
1856 
1857 	sel = wpabuf_alloc(len);
1858 	if (sel == NULL) {
1859 		wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
1860 		wpabuf_free(req);
1861 		return -1;
1862 	}
1863 	if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
1864 		wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
1865 		wpabuf_free(req);
1866 		wpabuf_free(sel);
1867 		return -1;
1868 	}
1869 
1870 	wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1871 		   role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1872 
1873 	if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1874 		ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
1875 #ifdef CONFIG_AP
1876 	} else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1877 	{
1878 		ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
1879 		if (ret < 0)
1880 			ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
1881 #endif /* CONFIG_AP */
1882 #ifdef CONFIG_P2P
1883 	} else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1884 	{
1885 		ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
1886 	} else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1887 	{
1888 		ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1889 						   forced_freq);
1890 #endif /* CONFIG_P2P */
1891 	} else {
1892 		wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1893 			   "reported: role=%s type=%s", role, type);
1894 		ret = -1;
1895 	}
1896 	wpabuf_free(req);
1897 	wpabuf_free(sel);
1898 
1899 	if (ret)
1900 		wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1901 
1902 	return ret;
1903 }
1904 
1905 #endif /* CONFIG_WPS_NFC */
1906 
1907 
wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant * wpa_s,char * cmd)1908 static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1909 					     char *cmd)
1910 {
1911 	u8 bssid[ETH_ALEN];
1912 	char *pin;
1913 	char *new_ssid;
1914 	char *new_auth;
1915 	char *new_encr;
1916 	char *new_key;
1917 	struct wps_new_ap_settings ap;
1918 
1919 	pin = os_strchr(cmd, ' ');
1920 	if (pin == NULL)
1921 		return -1;
1922 	*pin++ = '\0';
1923 
1924 	if (hwaddr_aton(cmd, bssid)) {
1925 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1926 			   cmd);
1927 		return -1;
1928 	}
1929 
1930 	new_ssid = os_strchr(pin, ' ');
1931 	if (new_ssid == NULL)
1932 		return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1933 	*new_ssid++ = '\0';
1934 
1935 	new_auth = os_strchr(new_ssid, ' ');
1936 	if (new_auth == NULL)
1937 		return -1;
1938 	*new_auth++ = '\0';
1939 
1940 	new_encr = os_strchr(new_auth, ' ');
1941 	if (new_encr == NULL)
1942 		return -1;
1943 	*new_encr++ = '\0';
1944 
1945 	new_key = os_strchr(new_encr, ' ');
1946 	if (new_key == NULL)
1947 		return -1;
1948 	*new_key++ = '\0';
1949 
1950 	os_memset(&ap, 0, sizeof(ap));
1951 	ap.ssid_hex = new_ssid;
1952 	ap.auth = new_auth;
1953 	ap.encr = new_encr;
1954 	ap.key_hex = new_key;
1955 	return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1956 }
1957 
1958 
1959 #ifdef CONFIG_AP
wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1960 static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1961 						char *cmd, char *buf,
1962 						size_t buflen)
1963 {
1964 	int timeout = 300;
1965 	char *pos;
1966 	const char *pin_txt;
1967 
1968 	if (!wpa_s->ap_iface)
1969 		return -1;
1970 
1971 	pos = os_strchr(cmd, ' ');
1972 	if (pos)
1973 		*pos++ = '\0';
1974 
1975 	if (os_strcmp(cmd, "disable") == 0) {
1976 		wpas_wps_ap_pin_disable(wpa_s);
1977 		return os_snprintf(buf, buflen, "OK\n");
1978 	}
1979 
1980 	if (os_strcmp(cmd, "random") == 0) {
1981 		if (pos)
1982 			timeout = atoi(pos);
1983 		pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1984 		if (pin_txt == NULL)
1985 			return -1;
1986 		return os_snprintf(buf, buflen, "%s", pin_txt);
1987 	}
1988 
1989 	if (os_strcmp(cmd, "get") == 0) {
1990 		pin_txt = wpas_wps_ap_pin_get(wpa_s);
1991 		if (pin_txt == NULL)
1992 			return -1;
1993 		return os_snprintf(buf, buflen, "%s", pin_txt);
1994 	}
1995 
1996 	if (os_strcmp(cmd, "set") == 0) {
1997 		char *pin;
1998 		if (pos == NULL)
1999 			return -1;
2000 		pin = pos;
2001 		pos = os_strchr(pos, ' ');
2002 		if (pos) {
2003 			*pos++ = '\0';
2004 			timeout = atoi(pos);
2005 		}
2006 		if (os_strlen(pin) > buflen)
2007 			return -1;
2008 		if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
2009 			return -1;
2010 		return os_snprintf(buf, buflen, "%s", pin);
2011 	}
2012 
2013 	return -1;
2014 }
2015 #endif /* CONFIG_AP */
2016 
2017 
2018 #ifdef CONFIG_WPS_ER
wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant * wpa_s,char * cmd)2019 static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
2020 						char *cmd)
2021 {
2022 	char *uuid = cmd, *pin, *pos;
2023 	u8 addr_buf[ETH_ALEN], *addr = NULL;
2024 	pin = os_strchr(uuid, ' ');
2025 	if (pin == NULL)
2026 		return -1;
2027 	*pin++ = '\0';
2028 	pos = os_strchr(pin, ' ');
2029 	if (pos) {
2030 		*pos++ = '\0';
2031 		if (hwaddr_aton(pos, addr_buf) == 0)
2032 			addr = addr_buf;
2033 	}
2034 	return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
2035 }
2036 
2037 
wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant * wpa_s,char * cmd)2038 static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
2039 						  char *cmd)
2040 {
2041 	char *uuid = cmd, *pin;
2042 	pin = os_strchr(uuid, ' ');
2043 	if (pin == NULL)
2044 		return -1;
2045 	*pin++ = '\0';
2046 	return wpas_wps_er_learn(wpa_s, uuid, pin);
2047 }
2048 
2049 
wpa_supplicant_ctrl_iface_wps_er_set_config(struct wpa_supplicant * wpa_s,char * cmd)2050 static int wpa_supplicant_ctrl_iface_wps_er_set_config(
2051 	struct wpa_supplicant *wpa_s, char *cmd)
2052 {
2053 	char *uuid = cmd, *id;
2054 	id = os_strchr(uuid, ' ');
2055 	if (id == NULL)
2056 		return -1;
2057 	*id++ = '\0';
2058 	return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
2059 }
2060 
2061 
wpa_supplicant_ctrl_iface_wps_er_config(struct wpa_supplicant * wpa_s,char * cmd)2062 static int wpa_supplicant_ctrl_iface_wps_er_config(
2063 	struct wpa_supplicant *wpa_s, char *cmd)
2064 {
2065 	char *pin;
2066 	char *new_ssid;
2067 	char *new_auth;
2068 	char *new_encr;
2069 	char *new_key;
2070 	struct wps_new_ap_settings ap;
2071 
2072 	pin = os_strchr(cmd, ' ');
2073 	if (pin == NULL)
2074 		return -1;
2075 	*pin++ = '\0';
2076 
2077 	new_ssid = os_strchr(pin, ' ');
2078 	if (new_ssid == NULL)
2079 		return -1;
2080 	*new_ssid++ = '\0';
2081 
2082 	new_auth = os_strchr(new_ssid, ' ');
2083 	if (new_auth == NULL)
2084 		return -1;
2085 	*new_auth++ = '\0';
2086 
2087 	new_encr = os_strchr(new_auth, ' ');
2088 	if (new_encr == NULL)
2089 		return -1;
2090 	*new_encr++ = '\0';
2091 
2092 	new_key = os_strchr(new_encr, ' ');
2093 	if (new_key == NULL)
2094 		return -1;
2095 	*new_key++ = '\0';
2096 
2097 	os_memset(&ap, 0, sizeof(ap));
2098 	ap.ssid_hex = new_ssid;
2099 	ap.auth = new_auth;
2100 	ap.encr = new_encr;
2101 	ap.key_hex = new_key;
2102 	return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
2103 }
2104 
2105 
2106 #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)2107 static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
2108 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
2109 {
2110 	int ndef;
2111 	struct wpabuf *buf;
2112 	int res;
2113 	char *uuid;
2114 
2115 	uuid = os_strchr(cmd, ' ');
2116 	if (uuid == NULL)
2117 		return -1;
2118 	*uuid++ = '\0';
2119 
2120 	if (os_strcmp(cmd, "WPS") == 0)
2121 		ndef = 0;
2122 	else if (os_strcmp(cmd, "NDEF") == 0)
2123 		ndef = 1;
2124 	else
2125 		return -1;
2126 
2127 	buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
2128 	if (buf == NULL)
2129 		return -1;
2130 
2131 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
2132 					 wpabuf_len(buf));
2133 	reply[res++] = '\n';
2134 	reply[res] = '\0';
2135 
2136 	wpabuf_free(buf);
2137 
2138 	return res;
2139 }
2140 #endif /* CONFIG_WPS_NFC */
2141 #endif /* CONFIG_WPS_ER */
2142 
2143 #endif /* CONFIG_WPS */
2144 
2145 
2146 #ifdef CONFIG_IBSS_RSN
wpa_supplicant_ctrl_iface_ibss_rsn(struct wpa_supplicant * wpa_s,char * addr)2147 static int wpa_supplicant_ctrl_iface_ibss_rsn(
2148 	struct wpa_supplicant *wpa_s, char *addr)
2149 {
2150 	u8 peer[ETH_ALEN];
2151 
2152 	if (hwaddr_aton(addr, peer)) {
2153 		wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
2154 			   "address '%s'", addr);
2155 		return -1;
2156 	}
2157 
2158 	wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
2159 		   MAC2STR(peer));
2160 
2161 	return ibss_rsn_start(wpa_s->ibss_rsn, peer);
2162 }
2163 #endif /* CONFIG_IBSS_RSN */
2164 
2165 
wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant * wpa_s,char * rsp)2166 static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
2167 					      char *rsp)
2168 {
2169 #ifdef IEEE8021X_EAPOL
2170 	char *pos, *id_pos;
2171 	int id;
2172 	struct wpa_ssid *ssid;
2173 
2174 	pos = os_strchr(rsp, '-');
2175 	if (pos == NULL)
2176 		return -1;
2177 	*pos++ = '\0';
2178 	id_pos = pos;
2179 	pos = os_strchr(pos, ':');
2180 	if (pos == NULL)
2181 		return -1;
2182 	*pos++ = '\0';
2183 	id = atoi(id_pos);
2184 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
2185 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2186 			      (u8 *) pos, os_strlen(pos));
2187 
2188 	ssid = wpa_config_get_network(wpa_s->conf, id);
2189 	if (ssid == NULL) {
2190 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2191 			   "to update", id);
2192 		return -1;
2193 	}
2194 
2195 	return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
2196 							 pos);
2197 #else /* IEEE8021X_EAPOL */
2198 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
2199 	return -1;
2200 #endif /* IEEE8021X_EAPOL */
2201 }
2202 
2203 
wpa_supplicant_ctrl_iface_status(struct wpa_supplicant * wpa_s,const char * params,char * buf,size_t buflen)2204 static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
2205 					    const char *params,
2206 					    char *buf, size_t buflen)
2207 {
2208 	char *pos, *end, tmp[30];
2209 	int res, verbose, wps, ret;
2210 #ifdef CONFIG_HS20
2211 	const u8 *hs20;
2212 #endif /* CONFIG_HS20 */
2213 	const u8 *sess_id;
2214 	size_t sess_id_len;
2215 
2216 	if (os_strcmp(params, "-DRIVER") == 0)
2217 		return wpa_drv_status(wpa_s, buf, buflen);
2218 	verbose = os_strcmp(params, "-VERBOSE") == 0;
2219 	wps = os_strcmp(params, "-WPS") == 0;
2220 	pos = buf;
2221 	end = buf + buflen;
2222 	if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
2223 		struct wpa_ssid *ssid = wpa_s->current_ssid;
2224 		ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
2225 				  MAC2STR(wpa_s->bssid));
2226 		if (os_snprintf_error(end - pos, ret))
2227 			return pos - buf;
2228 		pos += ret;
2229 		ret = os_snprintf(pos, end - pos, "freq=%u\n",
2230 				  wpa_s->assoc_freq);
2231 		if (os_snprintf_error(end - pos, ret))
2232 			return pos - buf;
2233 		pos += ret;
2234 		if (ssid) {
2235 			u8 *_ssid = ssid->ssid;
2236 			size_t ssid_len = ssid->ssid_len;
2237 			u8 ssid_buf[SSID_MAX_LEN];
2238 			if (ssid_len == 0) {
2239 				int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
2240 				if (_res < 0)
2241 					ssid_len = 0;
2242 				else
2243 					ssid_len = _res;
2244 				_ssid = ssid_buf;
2245 			}
2246 			ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
2247 					  wpa_ssid_txt(_ssid, ssid_len),
2248 					  ssid->id);
2249 			if (os_snprintf_error(end - pos, ret))
2250 				return pos - buf;
2251 			pos += ret;
2252 
2253 			if (wps && ssid->passphrase &&
2254 			    wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
2255 			    (ssid->mode == WPAS_MODE_AP ||
2256 			     ssid->mode == WPAS_MODE_P2P_GO)) {
2257 				ret = os_snprintf(pos, end - pos,
2258 						  "passphrase=%s\n",
2259 						  ssid->passphrase);
2260 				if (os_snprintf_error(end - pos, ret))
2261 					return pos - buf;
2262 				pos += ret;
2263 			}
2264 			if (ssid->id_str) {
2265 				ret = os_snprintf(pos, end - pos,
2266 						  "id_str=%s\n",
2267 						  ssid->id_str);
2268 				if (os_snprintf_error(end - pos, ret))
2269 					return pos - buf;
2270 				pos += ret;
2271 			}
2272 
2273 			switch (ssid->mode) {
2274 			case WPAS_MODE_INFRA:
2275 				ret = os_snprintf(pos, end - pos,
2276 						  "mode=station\n");
2277 				break;
2278 			case WPAS_MODE_IBSS:
2279 				ret = os_snprintf(pos, end - pos,
2280 						  "mode=IBSS\n");
2281 				break;
2282 			case WPAS_MODE_AP:
2283 				ret = os_snprintf(pos, end - pos,
2284 						  "mode=AP\n");
2285 				break;
2286 			case WPAS_MODE_P2P_GO:
2287 				ret = os_snprintf(pos, end - pos,
2288 						  "mode=P2P GO\n");
2289 				break;
2290 			case WPAS_MODE_P2P_GROUP_FORMATION:
2291 				ret = os_snprintf(pos, end - pos,
2292 						  "mode=P2P GO - group "
2293 						  "formation\n");
2294 				break;
2295 			case WPAS_MODE_MESH:
2296 				ret = os_snprintf(pos, end - pos,
2297 						  "mode=mesh\n");
2298 				break;
2299 			default:
2300 				ret = 0;
2301 				break;
2302 			}
2303 			if (os_snprintf_error(end - pos, ret))
2304 				return pos - buf;
2305 			pos += ret;
2306 		}
2307 
2308 		if (wpa_s->connection_set &&
2309 		    (wpa_s->connection_ht || wpa_s->connection_vht ||
2310 		     wpa_s->connection_he)) {
2311 			ret = os_snprintf(pos, end - pos,
2312 					  "wifi_generation=%u\n",
2313 					  wpa_s->connection_he ? 6 :
2314 					  (wpa_s->connection_vht ? 5 : 4));
2315 			if (os_snprintf_error(end - pos, ret))
2316 				return pos - buf;
2317 			pos += ret;
2318 		}
2319 
2320 #ifdef CONFIG_AP
2321 		if (wpa_s->ap_iface) {
2322 			pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
2323 							    end - pos,
2324 							    verbose);
2325 		} else
2326 #endif /* CONFIG_AP */
2327 		pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
2328 	}
2329 #ifdef CONFIG_SME
2330 #ifdef CONFIG_SAE
2331 	if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
2332 #ifdef CONFIG_AP
2333 	    !wpa_s->ap_iface &&
2334 #endif /* CONFIG_AP */
2335 	    wpa_s->sme.sae.state == SAE_ACCEPTED) {
2336 		ret = os_snprintf(pos, end - pos, "sae_group=%d\n"
2337 				  "sae_h2e=%d\n"
2338 				  "sae_pk=%d\n",
2339 				  wpa_s->sme.sae.group,
2340 				  wpa_s->sme.sae.h2e,
2341 				  wpa_s->sme.sae.pk);
2342 		if (os_snprintf_error(end - pos, ret))
2343 			return pos - buf;
2344 		pos += ret;
2345 	}
2346 #endif /* CONFIG_SAE */
2347 #endif /* CONFIG_SME */
2348 	ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
2349 			  wpa_supplicant_state_txt(wpa_s->wpa_state));
2350 	if (os_snprintf_error(end - pos, ret))
2351 		return pos - buf;
2352 	pos += ret;
2353 
2354 	if (wpa_s->l2 &&
2355 	    l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
2356 		ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
2357 		if (os_snprintf_error(end - pos, ret))
2358 			return pos - buf;
2359 		pos += ret;
2360 	}
2361 
2362 #ifdef CONFIG_P2P
2363 	if (wpa_s->global->p2p) {
2364 		ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
2365 				  "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
2366 		if (os_snprintf_error(end - pos, ret))
2367 			return pos - buf;
2368 		pos += ret;
2369 	}
2370 #endif /* CONFIG_P2P */
2371 
2372 	ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
2373 			  MAC2STR(wpa_s->own_addr));
2374 	if (os_snprintf_error(end - pos, ret))
2375 		return pos - buf;
2376 	pos += ret;
2377 
2378 #ifdef CONFIG_HS20
2379 	if (wpa_s->current_bss &&
2380 	    (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2381 					  HS20_IE_VENDOR_TYPE)) &&
2382 	    wpa_s->wpa_proto == WPA_PROTO_RSN &&
2383 	    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2384 		int release = 1;
2385 		if (hs20[1] >= 5) {
2386 			u8 rel_num = (hs20[6] & 0xf0) >> 4;
2387 			release = rel_num + 1;
2388 		}
2389 		ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
2390 		if (os_snprintf_error(end - pos, ret))
2391 			return pos - buf;
2392 		pos += ret;
2393 	}
2394 
2395 	if (wpa_s->current_ssid) {
2396 		struct wpa_cred *cred;
2397 		char *type;
2398 
2399 		for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2400 			size_t i;
2401 
2402 			if (wpa_s->current_ssid->parent_cred != cred)
2403 				continue;
2404 
2405 			if (cred->provisioning_sp) {
2406 				ret = os_snprintf(pos, end - pos,
2407 						  "provisioning_sp=%s\n",
2408 						  cred->provisioning_sp);
2409 				if (os_snprintf_error(end - pos, ret))
2410 					return pos - buf;
2411 				pos += ret;
2412 			}
2413 
2414 			if (!cred->domain)
2415 				goto no_domain;
2416 
2417 			i = 0;
2418 			if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
2419 				struct wpabuf *names =
2420 					wpa_s->current_bss->anqp->domain_name;
2421 				for (i = 0; names && i < cred->num_domain; i++)
2422 				{
2423 					if (domain_name_list_contains(
2424 						    names, cred->domain[i], 1))
2425 						break;
2426 				}
2427 				if (i == cred->num_domain)
2428 					i = 0; /* show first entry by default */
2429 			}
2430 			ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
2431 					  cred->domain[i]);
2432 			if (os_snprintf_error(end - pos, ret))
2433 				return pos - buf;
2434 			pos += ret;
2435 
2436 		no_domain:
2437 			if (wpa_s->current_bss == NULL ||
2438 			    wpa_s->current_bss->anqp == NULL)
2439 				res = -1;
2440 			else
2441 				res = interworking_home_sp_cred(
2442 					wpa_s, cred,
2443 					wpa_s->current_bss->anqp->domain_name);
2444 			if (res > 0)
2445 				type = "home";
2446 			else if (res == 0)
2447 				type = "roaming";
2448 			else
2449 				type = "unknown";
2450 
2451 			ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
2452 			if (os_snprintf_error(end - pos, ret))
2453 				return pos - buf;
2454 			pos += ret;
2455 
2456 			break;
2457 		}
2458 	}
2459 #endif /* CONFIG_HS20 */
2460 
2461 	if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2462 	    wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2463 		res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
2464 					  verbose);
2465 		if (res >= 0)
2466 			pos += res;
2467 	}
2468 
2469 #ifdef CONFIG_MACSEC
2470 	res = ieee802_1x_kay_get_status(wpa_s->kay, pos, end - pos);
2471 	if (res > 0)
2472 		pos += res;
2473 #endif /* CONFIG_MACSEC */
2474 
2475 	sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
2476 	if (sess_id) {
2477 		char *start = pos;
2478 
2479 		ret = os_snprintf(pos, end - pos, "eap_session_id=");
2480 		if (os_snprintf_error(end - pos, ret))
2481 			return start - buf;
2482 		pos += ret;
2483 		ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
2484 		if (ret <= 0)
2485 			return start - buf;
2486 		pos += ret;
2487 		ret = os_snprintf(pos, end - pos, "\n");
2488 		if (os_snprintf_error(end - pos, ret))
2489 			return start - buf;
2490 		pos += ret;
2491 	}
2492 
2493 	res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
2494 	if (res >= 0)
2495 		pos += res;
2496 
2497 #ifdef CONFIG_WPS
2498 	{
2499 		char uuid_str[100];
2500 		uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
2501 		ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
2502 		if (os_snprintf_error(end - pos, ret))
2503 			return pos - buf;
2504 		pos += ret;
2505 	}
2506 #endif /* CONFIG_WPS */
2507 
2508 	if (wpa_s->ieee80211ac) {
2509 		ret = os_snprintf(pos, end - pos, "ieee80211ac=1\n");
2510 		if (os_snprintf_error(end - pos, ret))
2511 			return pos - buf;
2512 		pos += ret;
2513 	}
2514 
2515 #ifdef ANDROID
2516 	/*
2517 	 * Allow using the STATUS command with default behavior, say for debug,
2518 	 * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
2519 	 * events with STATUS-NO_EVENTS.
2520 	 */
2521 	if (os_strcmp(params, "-NO_EVENTS")) {
2522 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
2523 			     "id=%d state=%d BSSID=" MACSTR " SSID=%s",
2524 			     wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
2525 			     wpa_s->wpa_state,
2526 			     MAC2STR(wpa_s->bssid),
2527 			     wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
2528 			     wpa_ssid_txt(wpa_s->current_ssid->ssid,
2529 					  wpa_s->current_ssid->ssid_len) : "");
2530 		if (wpa_s->wpa_state == WPA_COMPLETED) {
2531 			struct wpa_ssid *ssid = wpa_s->current_ssid;
2532 			wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
2533 				     "- connection to " MACSTR
2534 				     " completed %s [id=%d id_str=%s]",
2535 				     MAC2STR(wpa_s->bssid), "(auth)",
2536 				     ssid ? ssid->id : -1,
2537 				     ssid && ssid->id_str ? ssid->id_str : "");
2538 		}
2539 	}
2540 #endif /* ANDROID */
2541 
2542 	return pos - buf;
2543 }
2544 
2545 
wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant * wpa_s,char * cmd)2546 static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
2547 					   char *cmd)
2548 {
2549 	char *pos;
2550 	int id;
2551 	struct wpa_ssid *ssid;
2552 	u8 bssid[ETH_ALEN];
2553 
2554 	/* cmd: "<network id> <BSSID>" */
2555 	pos = os_strchr(cmd, ' ');
2556 	if (pos == NULL)
2557 		return -1;
2558 	*pos++ = '\0';
2559 	id = atoi(cmd);
2560 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
2561 	if (hwaddr_aton(pos, bssid)) {
2562 		wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
2563 		return -1;
2564 	}
2565 
2566 	ssid = wpa_config_get_network(wpa_s->conf, id);
2567 	if (ssid == NULL) {
2568 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2569 			   "to update", id);
2570 		return -1;
2571 	}
2572 
2573 	os_memcpy(ssid->bssid, bssid, ETH_ALEN);
2574 	ssid->bssid_set = !is_zero_ether_addr(bssid);
2575 
2576 	return 0;
2577 }
2578 
2579 
wpa_supplicant_ctrl_iface_bssid_ignore(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2580 static int wpa_supplicant_ctrl_iface_bssid_ignore(struct wpa_supplicant *wpa_s,
2581 						  char *cmd, char *buf,
2582 						  size_t buflen)
2583 {
2584 	u8 bssid[ETH_ALEN];
2585 	struct wpa_bssid_ignore *e;
2586 	char *pos, *end;
2587 	int ret;
2588 
2589 	/* cmd: "BSSID_IGNORE [<BSSID>]" */
2590 	if (*cmd == '\0') {
2591 		pos = buf;
2592 		end = buf + buflen;
2593 		e = wpa_s->bssid_ignore;
2594 		while (e) {
2595 			ret = os_snprintf(pos, end - pos, MACSTR "\n",
2596 					  MAC2STR(e->bssid));
2597 			if (os_snprintf_error(end - pos, ret))
2598 				return pos - buf;
2599 			pos += ret;
2600 			e = e->next;
2601 		}
2602 		return pos - buf;
2603 	}
2604 
2605 	cmd++;
2606 	if (os_strncmp(cmd, "clear", 5) == 0) {
2607 		wpa_bssid_ignore_clear(wpa_s);
2608 		os_memcpy(buf, "OK\n", 3);
2609 		return 3;
2610 	}
2611 
2612 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: BSSID_IGNORE bssid='%s'", cmd);
2613 	if (hwaddr_aton(cmd, bssid)) {
2614 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
2615 		return -1;
2616 	}
2617 
2618 	/*
2619 	 * Add the BSSID twice, so its count will be 2, causing it to be
2620 	 * skipped when processing scan results.
2621 	 */
2622 	ret = wpa_bssid_ignore_add(wpa_s, bssid);
2623 	if (ret < 0)
2624 		return -1;
2625 	ret = wpa_bssid_ignore_add(wpa_s, bssid);
2626 	if (ret < 0)
2627 		return -1;
2628 	os_memcpy(buf, "OK\n", 3);
2629 	return 3;
2630 }
2631 
2632 
wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2633 static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
2634 					       char *cmd, char *buf,
2635 					       size_t buflen)
2636 {
2637 	char *pos, *end, *stamp;
2638 	int ret;
2639 
2640 	/* cmd: "LOG_LEVEL [<level>]" */
2641 	if (*cmd == '\0') {
2642 		pos = buf;
2643 		end = buf + buflen;
2644 		ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2645 				  "Timestamp: %d\n",
2646 				  debug_level_str(wpa_debug_level),
2647 				  wpa_debug_timestamp);
2648 		if (os_snprintf_error(end - pos, ret))
2649 			ret = 0;
2650 
2651 		return ret;
2652 	}
2653 
2654 	while (*cmd == ' ')
2655 		cmd++;
2656 
2657 	stamp = os_strchr(cmd, ' ');
2658 	if (stamp) {
2659 		*stamp++ = '\0';
2660 		while (*stamp == ' ') {
2661 			stamp++;
2662 		}
2663 	}
2664 
2665 	if (os_strlen(cmd)) {
2666 		int level = str_to_debug_level(cmd);
2667 		if (level < 0)
2668 			return -1;
2669 		wpa_debug_level = level;
2670 	}
2671 
2672 	if (stamp && os_strlen(stamp))
2673 		wpa_debug_timestamp = atoi(stamp);
2674 
2675 	os_memcpy(buf, "OK\n", 3);
2676 	return 3;
2677 }
2678 
2679 
wpa_supplicant_ctrl_iface_list_networks(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2680 static int wpa_supplicant_ctrl_iface_list_networks(
2681 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2682 {
2683 	char *pos, *end, *prev;
2684 	struct wpa_ssid *ssid;
2685 	int ret;
2686 
2687 	pos = buf;
2688 	end = buf + buflen;
2689 	ret = os_snprintf(pos, end - pos,
2690 			  "network id / ssid / bssid / flags\n");
2691 	if (os_snprintf_error(end - pos, ret))
2692 		return pos - buf;
2693 	pos += ret;
2694 
2695 	ssid = wpa_s->conf->ssid;
2696 
2697 	/* skip over ssids until we find next one */
2698 	if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
2699 		int last_id = atoi(cmd + 8);
2700 		if (last_id != -1) {
2701 			while (ssid != NULL && ssid->id <= last_id) {
2702 				ssid = ssid->next;
2703 			}
2704 		}
2705 	}
2706 
2707 	while (ssid) {
2708 		prev = pos;
2709 		ret = os_snprintf(pos, end - pos, "%d\t%s",
2710 				  ssid->id,
2711 				  wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2712 		if (os_snprintf_error(end - pos, ret))
2713 			return prev - buf;
2714 		pos += ret;
2715 		if (ssid->bssid_set) {
2716 			ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2717 					  MAC2STR(ssid->bssid));
2718 		} else {
2719 			ret = os_snprintf(pos, end - pos, "\tany");
2720 		}
2721 		if (os_snprintf_error(end - pos, ret))
2722 			return prev - buf;
2723 		pos += ret;
2724 		ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
2725 				  ssid == wpa_s->current_ssid ?
2726 				  "[CURRENT]" : "",
2727 				  ssid->disabled ? "[DISABLED]" : "",
2728 				  ssid->disabled_until.sec ?
2729 				  "[TEMP-DISABLED]" : "",
2730 				  ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2731 				  "");
2732 		if (os_snprintf_error(end - pos, ret))
2733 			return prev - buf;
2734 		pos += ret;
2735 		ret = os_snprintf(pos, end - pos, "\n");
2736 		if (os_snprintf_error(end - pos, ret))
2737 			return prev - buf;
2738 		pos += ret;
2739 
2740 		ssid = ssid->next;
2741 	}
2742 
2743 	return pos - buf;
2744 }
2745 
2746 
wpa_supplicant_cipher_txt(char * pos,char * end,int cipher)2747 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2748 {
2749 	int ret;
2750 	ret = os_snprintf(pos, end - pos, "-");
2751 	if (os_snprintf_error(end - pos, ret))
2752 		return pos;
2753 	pos += ret;
2754 	ret = wpa_write_ciphers(pos, end, cipher, "+");
2755 	if (ret < 0)
2756 		return pos;
2757 	pos += ret;
2758 	return pos;
2759 }
2760 
2761 
wpa_supplicant_ie_txt(char * pos,char * end,const char * proto,const u8 * ie,size_t ie_len)2762 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2763 				    const u8 *ie, size_t ie_len)
2764 {
2765 	struct wpa_ie_data data;
2766 	char *start;
2767 	int ret;
2768 
2769 	ret = os_snprintf(pos, end - pos, "[%s-", proto);
2770 	if (os_snprintf_error(end - pos, ret))
2771 		return pos;
2772 	pos += ret;
2773 
2774 	if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2775 		ret = os_snprintf(pos, end - pos, "?]");
2776 		if (os_snprintf_error(end - pos, ret))
2777 			return pos;
2778 		pos += ret;
2779 		return pos;
2780 	}
2781 
2782 	start = pos;
2783 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
2784 		ret = os_snprintf(pos, end - pos, "%sEAP",
2785 				  pos == start ? "" : "+");
2786 		if (os_snprintf_error(end - pos, ret))
2787 			return pos;
2788 		pos += ret;
2789 	}
2790 	if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
2791 		ret = os_snprintf(pos, end - pos, "%sPSK",
2792 				  pos == start ? "" : "+");
2793 		if (os_snprintf_error(end - pos, ret))
2794 			return pos;
2795 		pos += ret;
2796 	}
2797 	if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
2798 		ret = os_snprintf(pos, end - pos, "%sNone",
2799 				  pos == start ? "" : "+");
2800 		if (os_snprintf_error(end - pos, ret))
2801 			return pos;
2802 		pos += ret;
2803 	}
2804 	if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
2805 		ret = os_snprintf(pos, end - pos, "%sSAE",
2806 				  pos == start ? "" : "+");
2807 		if (os_snprintf_error(end - pos, ret))
2808 			return pos;
2809 		pos += ret;
2810 	}
2811 #ifdef CONFIG_IEEE80211R
2812 	if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2813 		ret = os_snprintf(pos, end - pos, "%sFT/EAP",
2814 				  pos == start ? "" : "+");
2815 		if (os_snprintf_error(end - pos, ret))
2816 			return pos;
2817 		pos += ret;
2818 	}
2819 	if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2820 		ret = os_snprintf(pos, end - pos, "%sFT/PSK",
2821 				  pos == start ? "" : "+");
2822 		if (os_snprintf_error(end - pos, ret))
2823 			return pos;
2824 		pos += ret;
2825 	}
2826 	if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
2827 		ret = os_snprintf(pos, end - pos, "%sFT/SAE",
2828 				  pos == start ? "" : "+");
2829 		if (os_snprintf_error(end - pos, ret))
2830 			return pos;
2831 		pos += ret;
2832 	}
2833 #endif /* CONFIG_IEEE80211R */
2834 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2835 		ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
2836 				  pos == start ? "" : "+");
2837 		if (os_snprintf_error(end - pos, ret))
2838 			return pos;
2839 		pos += ret;
2840 	}
2841 	if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2842 		ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
2843 				  pos == start ? "" : "+");
2844 		if (os_snprintf_error(end - pos, ret))
2845 			return pos;
2846 		pos += ret;
2847 	}
2848 
2849 #ifdef CONFIG_SUITEB
2850 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
2851 		ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
2852 				  pos == start ? "" : "+");
2853 		if (os_snprintf_error(end - pos, ret))
2854 			return pos;
2855 		pos += ret;
2856 	}
2857 #endif /* CONFIG_SUITEB */
2858 
2859 #ifdef CONFIG_SUITEB192
2860 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
2861 		ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
2862 				  pos == start ? "" : "+");
2863 		if (os_snprintf_error(end - pos, ret))
2864 			return pos;
2865 		pos += ret;
2866 	}
2867 #endif /* CONFIG_SUITEB192 */
2868 
2869 #ifdef CONFIG_FILS
2870 	if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
2871 		ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
2872 				  pos == start ? "" : "+");
2873 		if (os_snprintf_error(end - pos, ret))
2874 			return pos;
2875 		pos += ret;
2876 	}
2877 	if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
2878 		ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
2879 				  pos == start ? "" : "+");
2880 		if (os_snprintf_error(end - pos, ret))
2881 			return pos;
2882 		pos += ret;
2883 	}
2884 #ifdef CONFIG_IEEE80211R
2885 	if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
2886 		ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
2887 				  pos == start ? "" : "+");
2888 		if (os_snprintf_error(end - pos, ret))
2889 			return pos;
2890 		pos += ret;
2891 	}
2892 	if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
2893 		ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
2894 				  pos == start ? "" : "+");
2895 		if (os_snprintf_error(end - pos, ret))
2896 			return pos;
2897 		pos += ret;
2898 	}
2899 #endif /* CONFIG_IEEE80211R */
2900 #endif /* CONFIG_FILS */
2901 
2902 #ifdef CONFIG_OWE
2903 	if (data.key_mgmt & WPA_KEY_MGMT_OWE) {
2904 		ret = os_snprintf(pos, end - pos, "%sOWE",
2905 				  pos == start ? "" : "+");
2906 		if (os_snprintf_error(end - pos, ret))
2907 			return pos;
2908 		pos += ret;
2909 	}
2910 #endif /* CONFIG_OWE */
2911 
2912 #ifdef CONFIG_DPP
2913 	if (data.key_mgmt & WPA_KEY_MGMT_DPP) {
2914 		ret = os_snprintf(pos, end - pos, "%sDPP",
2915 				  pos == start ? "" : "+");
2916 		if (os_snprintf_error(end - pos, ret))
2917 			return pos;
2918 		pos += ret;
2919 	}
2920 #endif /* CONFIG_DPP */
2921 
2922 	if (data.key_mgmt & WPA_KEY_MGMT_OSEN) {
2923 		ret = os_snprintf(pos, end - pos, "%sOSEN",
2924 				  pos == start ? "" : "+");
2925 		if (os_snprintf_error(end - pos, ret))
2926 			return pos;
2927 		pos += ret;
2928 	}
2929 
2930 	pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2931 
2932 	if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2933 		ret = os_snprintf(pos, end - pos, "-preauth");
2934 		if (os_snprintf_error(end - pos, ret))
2935 			return pos;
2936 		pos += ret;
2937 	}
2938 
2939 	ret = os_snprintf(pos, end - pos, "]");
2940 	if (os_snprintf_error(end - pos, ret))
2941 		return pos;
2942 	pos += ret;
2943 
2944 	return pos;
2945 }
2946 
2947 
2948 #ifdef CONFIG_WPS
wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant * wpa_s,char * pos,char * end,struct wpabuf * wps_ie)2949 static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2950 					    char *pos, char *end,
2951 					    struct wpabuf *wps_ie)
2952 {
2953 	int ret;
2954 	const char *txt;
2955 
2956 	if (wps_ie == NULL)
2957 		return pos;
2958 	if (wps_is_selected_pbc_registrar(wps_ie))
2959 		txt = "[WPS-PBC]";
2960 	else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2961 		txt = "[WPS-AUTH]";
2962 	else if (wps_is_selected_pin_registrar(wps_ie))
2963 		txt = "[WPS-PIN]";
2964 	else
2965 		txt = "[WPS]";
2966 
2967 	ret = os_snprintf(pos, end - pos, "%s", txt);
2968 	if (!os_snprintf_error(end - pos, ret))
2969 		pos += ret;
2970 	wpabuf_free(wps_ie);
2971 	return pos;
2972 }
2973 #endif /* CONFIG_WPS */
2974 
2975 
wpa_supplicant_wps_ie_txt(struct wpa_supplicant * wpa_s,char * pos,char * end,const struct wpa_bss * bss)2976 static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2977 					char *pos, char *end,
2978 					const struct wpa_bss *bss)
2979 {
2980 #ifdef CONFIG_WPS
2981 	struct wpabuf *wps_ie;
2982 	wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2983 	return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2984 #else /* CONFIG_WPS */
2985 	return pos;
2986 #endif /* CONFIG_WPS */
2987 }
2988 
2989 
2990 /* 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)2991 static int wpa_supplicant_ctrl_iface_scan_result(
2992 	struct wpa_supplicant *wpa_s,
2993 	const struct wpa_bss *bss, char *buf, size_t buflen)
2994 {
2995 	char *pos, *end;
2996 	int ret;
2997 	const u8 *ie, *ie2, *osen_ie, *p2p, *mesh, *owe, *rsnxe;
2998 #ifdef CONFIG_OPEN_HARMONY_PATCH
2999 	const u8 *infoEle;
3000 #endif
3001 
3002 	mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
3003 	p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
3004 	if (!p2p)
3005 		p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
3006 	if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
3007 	    os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
3008 	    0)
3009 		return 0; /* Do not show P2P listen discovery results here */
3010 
3011 	pos = buf;
3012 	end = buf + buflen;
3013 
3014 	ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
3015 			  MAC2STR(bss->bssid), bss->freq, bss->level);
3016 	if (os_snprintf_error(end - pos, ret))
3017 		return -1;
3018 	pos += ret;
3019 	ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3020 	if (ie)
3021 		pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
3022 	ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3023 	if (ie2) {
3024 		pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
3025 					    ie2, 2 + ie2[1]);
3026 	}
3027 	rsnxe = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
3028 	if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_H2E)) {
3029 		ret = os_snprintf(pos, end - pos, "[SAE-H2E]");
3030 		if (os_snprintf_error(end - pos, ret))
3031 			return -1;
3032 		pos += ret;
3033 	}
3034 	if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_PK)) {
3035 		ret = os_snprintf(pos, end - pos, "[SAE-PK]");
3036 		if (os_snprintf_error(end - pos, ret))
3037 			return -1;
3038 		pos += ret;
3039 	}
3040 	osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
3041 	if (osen_ie)
3042 		pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
3043 					    osen_ie, 2 + osen_ie[1]);
3044 	owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
3045 	if (owe) {
3046 		ret = os_snprintf(pos, end - pos,
3047 				  ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
3048 		if (os_snprintf_error(end - pos, ret))
3049 			return -1;
3050 		pos += ret;
3051 	}
3052 	pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3053 	if (!ie && !ie2 && !osen_ie && (bss->caps & IEEE80211_CAP_PRIVACY)) {
3054 		ret = os_snprintf(pos, end - pos, "[WEP]");
3055 		if (os_snprintf_error(end - pos, ret))
3056 			return -1;
3057 		pos += ret;
3058 	}
3059 	if (mesh) {
3060 		ret = os_snprintf(pos, end - pos, "[MESH]");
3061 		if (os_snprintf_error(end - pos, ret))
3062 			return -1;
3063 		pos += ret;
3064 	}
3065 	if (bss_is_dmg(bss)) {
3066 		const char *s;
3067 
3068 		if (wpa_bss_get_ie_ext(bss, WLAN_EID_EXT_EDMG_OPERATION)) {
3069 			ret = os_snprintf(pos, end - pos, "[EDMG]");
3070 			if (os_snprintf_error(end - pos, ret))
3071 				return -1;
3072 			pos += ret;
3073 		}
3074 
3075 		ret = os_snprintf(pos, end - pos, "[DMG]");
3076 		if (os_snprintf_error(end - pos, ret))
3077 			return -1;
3078 		pos += ret;
3079 		switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
3080 		case IEEE80211_CAP_DMG_IBSS:
3081 			s = "[IBSS]";
3082 			break;
3083 		case IEEE80211_CAP_DMG_AP:
3084 			s = "[ESS]";
3085 			break;
3086 		case IEEE80211_CAP_DMG_PBSS:
3087 			s = "[PBSS]";
3088 			break;
3089 		default:
3090 			s = "";
3091 			break;
3092 		}
3093 		ret = os_snprintf(pos, end - pos, "%s", s);
3094 		if (os_snprintf_error(end - pos, ret))
3095 			return -1;
3096 		pos += ret;
3097 	} else {
3098 		if (bss->caps & IEEE80211_CAP_IBSS) {
3099 			ret = os_snprintf(pos, end - pos, "[IBSS]");
3100 			if (os_snprintf_error(end - pos, ret))
3101 				return -1;
3102 			pos += ret;
3103 		}
3104 		if (bss->caps & IEEE80211_CAP_ESS) {
3105 			ret = os_snprintf(pos, end - pos, "[ESS]");
3106 			if (os_snprintf_error(end - pos, ret))
3107 				return -1;
3108 			pos += ret;
3109 		}
3110 	}
3111 	if (p2p) {
3112 		ret = os_snprintf(pos, end - pos, "[P2P]");
3113 		if (os_snprintf_error(end - pos, ret))
3114 			return -1;
3115 		pos += ret;
3116 	}
3117 #ifdef CONFIG_HS20
3118 	if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
3119 		ret = os_snprintf(pos, end - pos, "[HS20]");
3120 		if (os_snprintf_error(end - pos, ret))
3121 			return -1;
3122 		pos += ret;
3123 	}
3124 #endif /* CONFIG_HS20 */
3125 #ifdef CONFIG_FILS
3126 	if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
3127 		ret = os_snprintf(pos, end - pos, "[FILS]");
3128 		if (os_snprintf_error(end - pos, ret))
3129 			return -1;
3130 		pos += ret;
3131 	}
3132 #endif /* CONFIG_FILS */
3133 #ifdef CONFIG_FST
3134 	if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
3135 		ret = os_snprintf(pos, end - pos, "[FST]");
3136 		if (os_snprintf_error(end - pos, ret))
3137 			return -1;
3138 		pos += ret;
3139 	}
3140 #endif /* CONFIG_FST */
3141 	if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_UTF_8_SSID)) {
3142 		ret = os_snprintf(pos, end - pos, "[UTF-8]");
3143 		if (os_snprintf_error(end - pos, ret))
3144 			return -1;
3145 		pos += ret;
3146 	}
3147 
3148 #ifdef CONFIG_OPEN_HARMONY_PATCH
3149 	ret = os_snprintf(pos, end - pos, "\t%s\t",
3150 			  wpa_ssid_txt(bss->ssid, bss->ssid_len));
3151 #else
3152 	ret = os_snprintf(pos, end - pos, "\t%s",
3153 			  wpa_ssid_txt(bss->ssid, bss->ssid_len));
3154 #endif
3155 	if (os_snprintf_error(end - pos, ret))
3156 		return -1;
3157 	pos += ret;
3158 
3159 #ifdef CONFIG_OPEN_HARMONY_PATCH
3160 	for (int j = 0; j < WLAN_EID_EXTENSION; j++) {
3161 		if ((j != WLAN_EID_VHT_OPERATION) && (j != WLAN_EID_HT_OPERATION) &&
3162 			(j != WLAN_EID_SUPPORTED_CHANNELS) && (j != WLAN_EID_COUNTRY)) {
3163 			continue;
3164 		}
3165 		infoEle = wpa_bss_get_ie(bss, j);
3166 		if (infoEle && infoEle[1] > 0) {
3167 			ret = os_snprintf(pos, end - pos, "[%d ", j);
3168 			if (os_snprintf_error(end - pos, ret))
3169 				return -1;
3170 			pos += ret;
3171 			for (u8 i = 0; i < infoEle[1]; i++) {
3172 				ret = os_snprintf(pos, end - pos, "%02x", infoEle[i + 2]);
3173 				if (os_snprintf_error(end - pos, ret))
3174 					return -1;
3175 				pos += ret;
3176 			}
3177 			ret = os_snprintf(pos, end - pos, "]");
3178 			if (os_snprintf_error(end - pos, ret))
3179 				return -1;
3180 			pos += ret;
3181 		}
3182 	}
3183 
3184 	infoEle = wpa_bss_get_ie(bss, WLAN_EID_EXTENSION);
3185 	if (infoEle) {
3186 		unsigned int len = infoEle[1];
3187 		if (len > 1 && infoEle[2] == WLAN_EID_EXT_HE_OPERATION) {
3188 			ret = os_snprintf(pos, end - pos, "[%d %d ",
3189 				WLAN_EID_EXTENSION, WLAN_EID_EXT_HE_OPERATION);
3190 			if (os_snprintf_error(end - pos, ret))
3191 				return -1;
3192 			pos += ret;
3193 			for (size_t i = 0; i < len; i++) {
3194 				ret = os_snprintf(pos, end - pos, "%02x", infoEle[i + 3]);
3195 				if (os_snprintf_error(end - pos, ret))
3196 					return -1;
3197 				pos += ret;
3198 			}
3199 			ret = os_snprintf(pos, end - pos, "]");
3200 			if (os_snprintf_error(end - pos, ret))
3201 				return -1;
3202 			pos += ret;
3203 		}
3204 	}
3205 #endif
3206 
3207 	ret = os_snprintf(pos, end - pos, "\n");
3208 	if (os_snprintf_error(end - pos, ret))
3209 		return -1;
3210 	pos += ret;
3211 
3212 	return pos - buf;
3213 }
3214 
3215 
wpa_supplicant_ctrl_iface_scan_results(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3216 static int wpa_supplicant_ctrl_iface_scan_results(
3217 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
3218 {
3219 	char *pos, *end;
3220 	struct wpa_bss *bss;
3221 	int ret;
3222 
3223 	pos = buf;
3224 	end = buf + buflen;
3225 #ifdef CONFIG_OPEN_HARMONY_PATCH
3226 	ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
3227 			  "flags / ssid / informationElements\n");
3228 #else
3229 	ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
3230 			  "flags / ssid\n");
3231 #endif
3232 	if (os_snprintf_error(end - pos, ret))
3233 		return pos - buf;
3234 	pos += ret;
3235 
3236 	dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
3237 		ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
3238 							    end - pos);
3239 		if (ret < 0 || ret >= end - pos)
3240 			return pos - buf;
3241 		pos += ret;
3242 	}
3243 
3244 	return pos - buf;
3245 }
3246 
3247 
3248 #ifdef CONFIG_MESH
3249 
wpa_supplicant_ctrl_iface_mesh_interface_add(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)3250 static int wpa_supplicant_ctrl_iface_mesh_interface_add(
3251 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
3252 {
3253 	char *pos, ifname[IFNAMSIZ + 1];
3254 
3255 	ifname[0] = '\0';
3256 
3257 	pos = os_strstr(cmd, "ifname=");
3258 	if (pos) {
3259 		pos += 7;
3260 		os_strlcpy(ifname, pos, sizeof(ifname));
3261 	}
3262 
3263 	if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
3264 		return -1;
3265 
3266 	os_strlcpy(reply, ifname, max_len);
3267 	return os_strlen(ifname);
3268 }
3269 
3270 
wpa_supplicant_ctrl_iface_mesh_group_add(struct wpa_supplicant * wpa_s,char * cmd)3271 static int wpa_supplicant_ctrl_iface_mesh_group_add(
3272 	struct wpa_supplicant *wpa_s, char *cmd)
3273 {
3274 	int id;
3275 	struct wpa_ssid *ssid;
3276 
3277 	id = atoi(cmd);
3278 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
3279 
3280 	ssid = wpa_config_get_network(wpa_s->conf, id);
3281 	if (ssid == NULL) {
3282 		wpa_printf(MSG_DEBUG,
3283 			   "CTRL_IFACE: Could not find network id=%d", id);
3284 		return -1;
3285 	}
3286 	if (ssid->mode != WPAS_MODE_MESH) {
3287 		wpa_printf(MSG_DEBUG,
3288 			   "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
3289 		return -1;
3290 	}
3291 	if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
3292 	    ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
3293 		wpa_printf(MSG_ERROR,
3294 			   "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
3295 		return -1;
3296 	}
3297 
3298 	/*
3299 	 * TODO: If necessary write our own group_add function,
3300 	 * for now we can reuse select_network
3301 	 */
3302 	wpa_supplicant_select_network(wpa_s, ssid);
3303 
3304 	return 0;
3305 }
3306 
3307 
wpa_supplicant_ctrl_iface_mesh_group_remove(struct wpa_supplicant * wpa_s,char * cmd)3308 static int wpa_supplicant_ctrl_iface_mesh_group_remove(
3309 	struct wpa_supplicant *wpa_s, char *cmd)
3310 {
3311 	struct wpa_supplicant *orig;
3312 	struct wpa_global *global;
3313 	int found = 0;
3314 
3315 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
3316 
3317 	global = wpa_s->global;
3318 	orig = wpa_s;
3319 
3320 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3321 		if (os_strcmp(wpa_s->ifname, cmd) == 0) {
3322 			found = 1;
3323 			break;
3324 		}
3325 	}
3326 	if (!found) {
3327 		wpa_printf(MSG_ERROR,
3328 			   "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
3329 			   cmd);
3330 		return -1;
3331 	}
3332 	if (wpa_s->mesh_if_created && wpa_s == orig) {
3333 		wpa_printf(MSG_ERROR,
3334 			   "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
3335 		return -1;
3336 	}
3337 
3338 	wpa_s->reassociate = 0;
3339 	wpa_s->disconnected = 1;
3340 	wpa_supplicant_cancel_sched_scan(wpa_s);
3341 	wpa_supplicant_cancel_scan(wpa_s);
3342 
3343 	/*
3344 	 * TODO: If necessary write our own group_remove function,
3345 	 * for now we can reuse deauthenticate
3346 	 */
3347 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3348 
3349 	if (wpa_s->mesh_if_created)
3350 		wpa_supplicant_remove_iface(global, wpa_s, 0);
3351 
3352 	return 0;
3353 }
3354 
3355 
wpa_supplicant_ctrl_iface_mesh_peer_remove(struct wpa_supplicant * wpa_s,char * cmd)3356 static int wpa_supplicant_ctrl_iface_mesh_peer_remove(
3357 	struct wpa_supplicant *wpa_s, char *cmd)
3358 {
3359 	u8 addr[ETH_ALEN];
3360 
3361 	if (hwaddr_aton(cmd, addr) < 0)
3362 		return -1;
3363 
3364 	return wpas_mesh_peer_remove(wpa_s, addr);
3365 }
3366 
3367 
wpa_supplicant_ctrl_iface_mesh_peer_add(struct wpa_supplicant * wpa_s,char * cmd)3368 static int wpa_supplicant_ctrl_iface_mesh_peer_add(
3369 	struct wpa_supplicant *wpa_s, char *cmd)
3370 {
3371 	u8 addr[ETH_ALEN];
3372 	int duration;
3373 	char *pos;
3374 
3375 	pos = os_strstr(cmd, " duration=");
3376 	if (pos) {
3377 		*pos = '\0';
3378 		duration = atoi(pos + 10);
3379 	} else {
3380 		duration = -1;
3381 	}
3382 
3383 	if (hwaddr_aton(cmd, addr))
3384 		return -1;
3385 
3386 	return wpas_mesh_peer_add(wpa_s, addr, duration);
3387 }
3388 
3389 
wpa_supplicant_ctrl_iface_mesh_link_probe(struct wpa_supplicant * wpa_s,char * cmd)3390 static int wpa_supplicant_ctrl_iface_mesh_link_probe(
3391 	struct wpa_supplicant *wpa_s, char *cmd)
3392 {
3393 	struct ether_header *eth;
3394 	u8 addr[ETH_ALEN];
3395 	u8 *buf;
3396 	char *pos;
3397 	size_t payload_len = 0, len;
3398 	int ret = -1;
3399 
3400 	if (hwaddr_aton(cmd, addr))
3401 		return -1;
3402 
3403 	pos = os_strstr(cmd, " payload=");
3404 	if (pos) {
3405 		pos = pos + 9;
3406 		payload_len = os_strlen(pos);
3407 		if (payload_len & 1)
3408 			return -1;
3409 
3410 		payload_len /= 2;
3411 	}
3412 
3413 	len = ETH_HLEN + payload_len;
3414 	buf = os_malloc(len);
3415 	if (!buf)
3416 		return -1;
3417 
3418 	eth = (struct ether_header *) buf;
3419 	os_memcpy(eth->ether_dhost, addr, ETH_ALEN);
3420 	os_memcpy(eth->ether_shost, wpa_s->own_addr, ETH_ALEN);
3421 	eth->ether_type = htons(ETH_P_802_3);
3422 
3423 	if (payload_len && hexstr2bin(pos, buf + ETH_HLEN, payload_len) < 0)
3424 		goto fail;
3425 
3426 	ret = wpa_drv_mesh_link_probe(wpa_s, addr, buf, len);
3427 fail:
3428 	os_free(buf);
3429 	return -ret;
3430 }
3431 
3432 #endif /* CONFIG_MESH */
3433 
3434 
wpa_supplicant_ctrl_iface_select_network(struct wpa_supplicant * wpa_s,char * cmd)3435 static int wpa_supplicant_ctrl_iface_select_network(
3436 	struct wpa_supplicant *wpa_s, char *cmd)
3437 {
3438 	int id;
3439 	struct wpa_ssid *ssid;
3440 	char *pos;
3441 
3442 	/* cmd: "<network id>" or "any" */
3443 	if (os_strncmp(cmd, "any", 3) == 0) {
3444 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
3445 		ssid = NULL;
3446 	} else {
3447 		id = atoi(cmd);
3448 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
3449 
3450 		ssid = wpa_config_get_network(wpa_s->conf, id);
3451 		if (ssid == NULL) {
3452 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3453 				   "network id=%d", id);
3454 			return -1;
3455 		}
3456 		if (ssid->disabled == 2) {
3457 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3458 				   "SELECT_NETWORK with persistent P2P group");
3459 			return -1;
3460 		}
3461 	}
3462 
3463 	pos = os_strstr(cmd, " freq=");
3464 	if (pos) {
3465 		int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
3466 		if (freqs) {
3467 			os_free(wpa_s->select_network_scan_freqs);
3468 			wpa_s->select_network_scan_freqs = freqs;
3469 		}
3470 	}
3471 
3472 	wpa_s->scan_min_time.sec = 0;
3473 	wpa_s->scan_min_time.usec = 0;
3474 	wpa_supplicant_select_network(wpa_s, ssid);
3475 
3476 	return 0;
3477 }
3478 
3479 
wpa_supplicant_ctrl_iface_enable_network(struct wpa_supplicant * wpa_s,char * cmd)3480 static int wpa_supplicant_ctrl_iface_enable_network(
3481 	struct wpa_supplicant *wpa_s, char *cmd)
3482 {
3483 	int id;
3484 	struct wpa_ssid *ssid;
3485 
3486 	/* cmd: "<network id>" or "all" */
3487 	if (os_strcmp(cmd, "all") == 0) {
3488 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
3489 		ssid = NULL;
3490 	} else {
3491 		id = atoi(cmd);
3492 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
3493 
3494 		ssid = wpa_config_get_network(wpa_s->conf, id);
3495 		if (ssid == NULL) {
3496 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3497 				   "network id=%d", id);
3498 			return -1;
3499 		}
3500 		if (ssid->disabled == 2) {
3501 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3502 				   "ENABLE_NETWORK with persistent P2P group");
3503 			return -1;
3504 		}
3505 
3506 		if (os_strstr(cmd, " no-connect")) {
3507 			ssid->disabled = 0;
3508 			return 0;
3509 		}
3510 	}
3511 	wpa_s->scan_min_time.sec = 0;
3512 	wpa_s->scan_min_time.usec = 0;
3513 	wpa_supplicant_enable_network(wpa_s, ssid);
3514 
3515 	return 0;
3516 }
3517 
3518 
wpa_supplicant_ctrl_iface_disable_network(struct wpa_supplicant * wpa_s,char * cmd)3519 static int wpa_supplicant_ctrl_iface_disable_network(
3520 	struct wpa_supplicant *wpa_s, char *cmd)
3521 {
3522 	int id;
3523 	struct wpa_ssid *ssid;
3524 
3525 	/* cmd: "<network id>" or "all" */
3526 	if (os_strcmp(cmd, "all") == 0) {
3527 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
3528 		ssid = NULL;
3529 	} else {
3530 		id = atoi(cmd);
3531 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
3532 
3533 		ssid = wpa_config_get_network(wpa_s->conf, id);
3534 		if (ssid == NULL) {
3535 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3536 				   "network id=%d", id);
3537 			return -1;
3538 		}
3539 		if (ssid->disabled == 2) {
3540 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3541 				   "DISABLE_NETWORK with persistent P2P "
3542 				   "group");
3543 			return -1;
3544 		}
3545 	}
3546 	wpa_supplicant_disable_network(wpa_s, ssid);
3547 
3548 	return 0;
3549 }
3550 
3551 
wpa_supplicant_ctrl_iface_add_network(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3552 static int wpa_supplicant_ctrl_iface_add_network(
3553 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
3554 {
3555 	struct wpa_ssid *ssid;
3556 	int ret;
3557 
3558 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
3559 
3560 	ssid = wpa_supplicant_add_network(wpa_s);
3561 	if (ssid == NULL)
3562 		return -1;
3563 
3564 	ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
3565 	if (os_snprintf_error(buflen, ret))
3566 		return -1;
3567 	return ret;
3568 }
3569 
3570 
wpa_supplicant_ctrl_iface_remove_network(struct wpa_supplicant * wpa_s,char * cmd)3571 static int wpa_supplicant_ctrl_iface_remove_network(
3572 	struct wpa_supplicant *wpa_s, char *cmd)
3573 {
3574 	int id;
3575 	int result;
3576 
3577 	/* cmd: "<network id>" or "all" */
3578 	if (os_strcmp(cmd, "all") == 0) {
3579 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
3580 		return wpa_supplicant_remove_all_networks(wpa_s);
3581 	}
3582 
3583 	id = atoi(cmd);
3584 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
3585 
3586 	result = wpa_supplicant_remove_network(wpa_s, id);
3587 	if (result == -1) {
3588 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3589 			   "id=%d", id);
3590 		return -1;
3591 	}
3592 	if (result == -2) {
3593 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
3594 			   "network id=%d", id);
3595 		return -1;
3596 	}
3597 	return 0;
3598 }
3599 
3600 
wpa_supplicant_ctrl_iface_update_network(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,char * name,char * value)3601 static int wpa_supplicant_ctrl_iface_update_network(
3602 	struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3603 	char *name, char *value)
3604 {
3605 	int ret;
3606 
3607 	ret = wpa_config_set(ssid, name, value, 0);
3608 	if (ret < 0) {
3609 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
3610 			   "variable '%s'", name);
3611 		return -1;
3612 	}
3613 	if (ret == 1)
3614 		return 0; /* No change to the previously configured value */
3615 
3616 #ifdef CONFIG_BGSCAN
3617 	if (os_strcmp(name, "bgscan") == 0) {
3618 		/*
3619 		 * Reset the bgscan parameters for the current network and
3620 		 * return. There's no need to flush caches for bgscan parameter
3621 		 * changes.
3622 		 */
3623 		if (wpa_s->current_ssid == ssid &&
3624 		    wpa_s->wpa_state == WPA_COMPLETED)
3625 			wpa_supplicant_reset_bgscan(wpa_s);
3626 		return 0;
3627 	}
3628 #endif /* CONFIG_BGSCAN */
3629 
3630 	if (os_strcmp(name, "bssid") != 0 &&
3631 	    os_strcmp(name, "bssid_hint") != 0 &&
3632 	    os_strcmp(name, "priority") != 0) {
3633 		wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
3634 
3635 		if (wpa_s->current_ssid == ssid ||
3636 		    wpa_s->current_ssid == NULL) {
3637 			/*
3638 			 * Invalidate the EAP session cache if anything in the
3639 			 * current or previously used configuration changes.
3640 			 */
3641 			eapol_sm_invalidate_cached_session(wpa_s->eapol);
3642 		}
3643 	}
3644 
3645 	if ((os_strcmp(name, "psk") == 0 &&
3646 	     value[0] == '"' && ssid->ssid_len) ||
3647 	    (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
3648 		wpa_config_update_psk(ssid);
3649 	else if (os_strcmp(name, "priority") == 0)
3650 		wpa_config_update_prio_list(wpa_s->conf);
3651 
3652 	return 0;
3653 }
3654 
3655 
wpa_supplicant_ctrl_iface_set_network(struct wpa_supplicant * wpa_s,char * cmd)3656 static int wpa_supplicant_ctrl_iface_set_network(
3657 	struct wpa_supplicant *wpa_s, char *cmd)
3658 {
3659 	int id, ret, prev_bssid_set, prev_disabled;
3660 	struct wpa_ssid *ssid;
3661 	char *name, *value;
3662 	u8 prev_bssid[ETH_ALEN];
3663 
3664 	/* cmd: "<network id> <variable name> <value>" */
3665 	name = os_strchr(cmd, ' ');
3666 	if (name == NULL)
3667 		return -1;
3668 	*name++ = '\0';
3669 
3670 	value = os_strchr(name, ' ');
3671 	if (value == NULL)
3672 		return -1;
3673 	*value++ = '\0';
3674 
3675 	id = atoi(cmd);
3676 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
3677 		   id, name);
3678 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3679 			      (u8 *) value, os_strlen(value));
3680 
3681 	ssid = wpa_config_get_network(wpa_s->conf, id);
3682 	if (ssid == NULL) {
3683 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3684 			   "id=%d", id);
3685 		return -1;
3686 	}
3687 
3688 	prev_bssid_set = ssid->bssid_set;
3689 	prev_disabled = ssid->disabled;
3690 	os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
3691 	ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
3692 						       value);
3693 	if (ret == 0 &&
3694 	    (ssid->bssid_set != prev_bssid_set ||
3695 	     os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
3696 		wpas_notify_network_bssid_set_changed(wpa_s, ssid);
3697 
3698 	if (prev_disabled != ssid->disabled &&
3699 	    (prev_disabled == 2 || ssid->disabled == 2))
3700 		wpas_notify_network_type_changed(wpa_s, ssid);
3701 
3702 	return ret;
3703 }
3704 
3705 
wpa_supplicant_ctrl_iface_get_network(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)3706 static int wpa_supplicant_ctrl_iface_get_network(
3707 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
3708 {
3709 	int id;
3710 	size_t res;
3711 	struct wpa_ssid *ssid;
3712 	char *name, *value;
3713 
3714 	/* cmd: "<network id> <variable name>" */
3715 	name = os_strchr(cmd, ' ');
3716 	if (name == NULL || buflen == 0)
3717 		return -1;
3718 	*name++ = '\0';
3719 
3720 	id = atoi(cmd);
3721 	wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
3722 		   id, name);
3723 
3724 	ssid = wpa_config_get_network(wpa_s->conf, id);
3725 	if (ssid == NULL) {
3726 		wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Could not find network "
3727 			   "id=%d", id);
3728 		return -1;
3729 	}
3730 
3731 	value = wpa_config_get_no_key(ssid, name);
3732 	if (value == NULL) {
3733 		wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Failed to get network "
3734 			   "variable '%s'", name);
3735 		return -1;
3736 	}
3737 
3738 	res = os_strlcpy(buf, value, buflen);
3739 	if (res >= buflen) {
3740 		os_free(value);
3741 		return -1;
3742 	}
3743 
3744 	os_free(value);
3745 
3746 	return res;
3747 }
3748 
3749 
wpa_supplicant_ctrl_iface_dup_network(struct wpa_supplicant * wpa_s,char * cmd,struct wpa_supplicant * dst_wpa_s)3750 static int wpa_supplicant_ctrl_iface_dup_network(
3751 	struct wpa_supplicant *wpa_s, char *cmd,
3752 	struct wpa_supplicant *dst_wpa_s)
3753 {
3754 	struct wpa_ssid *ssid_s, *ssid_d;
3755 	char *name, *id, *value;
3756 	int id_s, id_d, ret;
3757 
3758 	/* cmd: "<src network id> <dst network id> <variable name>" */
3759 	id = os_strchr(cmd, ' ');
3760 	if (id == NULL)
3761 		return -1;
3762 	*id++ = '\0';
3763 
3764 	name = os_strchr(id, ' ');
3765 	if (name == NULL)
3766 		return -1;
3767 	*name++ = '\0';
3768 
3769 	id_s = atoi(cmd);
3770 	id_d = atoi(id);
3771 
3772 	wpa_printf(MSG_DEBUG,
3773 		   "CTRL_IFACE: DUP_NETWORK ifname=%s->%s id=%d->%d name='%s'",
3774 		   wpa_s->ifname, dst_wpa_s->ifname, id_s, id_d, name);
3775 
3776 	ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
3777 	if (ssid_s == NULL) {
3778 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3779 			   "network id=%d", id_s);
3780 		return -1;
3781 	}
3782 
3783 	ssid_d = wpa_config_get_network(dst_wpa_s->conf, id_d);
3784 	if (ssid_d == NULL) {
3785 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3786 			   "network id=%d", id_d);
3787 		return -1;
3788 	}
3789 
3790 	value = wpa_config_get(ssid_s, name);
3791 	if (value == NULL) {
3792 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
3793 			   "variable '%s'", name);
3794 		return -1;
3795 	}
3796 
3797 	ret = wpa_supplicant_ctrl_iface_update_network(dst_wpa_s, ssid_d, name,
3798 						       value);
3799 
3800 	os_free(value);
3801 
3802 	return ret;
3803 }
3804 
3805 
wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3806 static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
3807 						char *buf, size_t buflen)
3808 {
3809 	char *pos, *end;
3810 	struct wpa_cred *cred;
3811 	int ret;
3812 
3813 	pos = buf;
3814 	end = buf + buflen;
3815 	ret = os_snprintf(pos, end - pos,
3816 			  "cred id / realm / username / domain / imsi\n");
3817 	if (os_snprintf_error(end - pos, ret))
3818 		return pos - buf;
3819 	pos += ret;
3820 
3821 	cred = wpa_s->conf->cred;
3822 	while (cred) {
3823 		ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
3824 				  cred->id, cred->realm ? cred->realm : "",
3825 				  cred->username ? cred->username : "",
3826 				  cred->domain ? cred->domain[0] : "",
3827 				  cred->imsi ? cred->imsi : "");
3828 		if (os_snprintf_error(end - pos, ret))
3829 			return pos - buf;
3830 		pos += ret;
3831 
3832 		cred = cred->next;
3833 	}
3834 
3835 	return pos - buf;
3836 }
3837 
3838 
wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3839 static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
3840 					      char *buf, size_t buflen)
3841 {
3842 	struct wpa_cred *cred;
3843 	int ret;
3844 
3845 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
3846 
3847 	cred = wpa_config_add_cred(wpa_s->conf);
3848 	if (cred == NULL)
3849 		return -1;
3850 
3851 	wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
3852 
3853 	ret = os_snprintf(buf, buflen, "%d\n", cred->id);
3854 	if (os_snprintf_error(buflen, ret))
3855 		return -1;
3856 	return ret;
3857 }
3858 
3859 
wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant * wpa_s,char * cmd)3860 static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
3861 						 char *cmd)
3862 {
3863 	int id;
3864 	struct wpa_cred *cred, *prev;
3865 
3866 	/* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
3867 	 * "provisioning_sp=<FQDN> */
3868 	if (os_strcmp(cmd, "all") == 0) {
3869 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
3870 		return wpas_remove_all_creds(wpa_s);
3871 	}
3872 
3873 	if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
3874 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
3875 			   cmd + 8);
3876 		cred = wpa_s->conf->cred;
3877 		while (cred) {
3878 			prev = cred;
3879 			cred = cred->next;
3880 			if (prev->domain) {
3881 				size_t i;
3882 				for (i = 0; i < prev->num_domain; i++) {
3883 					if (os_strcmp(prev->domain[i], cmd + 8)
3884 					    != 0)
3885 						continue;
3886 					wpas_remove_cred(wpa_s, prev);
3887 					break;
3888 				}
3889 			}
3890 		}
3891 		return 0;
3892 	}
3893 
3894 	if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
3895 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
3896 			   cmd + 16);
3897 		cred = wpa_s->conf->cred;
3898 		while (cred) {
3899 			prev = cred;
3900 			cred = cred->next;
3901 			if (prev->provisioning_sp &&
3902 			    os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
3903 				wpas_remove_cred(wpa_s, prev);
3904 		}
3905 		return 0;
3906 	}
3907 
3908 	id = atoi(cmd);
3909 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
3910 
3911 	cred = wpa_config_get_cred(wpa_s->conf, id);
3912 	return wpas_remove_cred(wpa_s, cred);
3913 }
3914 
3915 
wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant * wpa_s,char * cmd)3916 static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
3917 					      char *cmd)
3918 {
3919 	int id;
3920 	struct wpa_cred *cred;
3921 	char *name, *value;
3922 
3923 	/* cmd: "<cred id> <variable name> <value>" */
3924 	name = os_strchr(cmd, ' ');
3925 	if (name == NULL)
3926 		return -1;
3927 	*name++ = '\0';
3928 
3929 	value = os_strchr(name, ' ');
3930 	if (value == NULL)
3931 		return -1;
3932 	*value++ = '\0';
3933 
3934 	id = atoi(cmd);
3935 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
3936 		   id, name);
3937 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3938 			      (u8 *) value, os_strlen(value));
3939 
3940 	cred = wpa_config_get_cred(wpa_s->conf, id);
3941 	if (cred == NULL) {
3942 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3943 			   id);
3944 		return -1;
3945 	}
3946 
3947 	if (wpa_config_set_cred(cred, name, value, 0) < 0) {
3948 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
3949 			   "variable '%s'", name);
3950 		return -1;
3951 	}
3952 
3953 	wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
3954 
3955 	return 0;
3956 }
3957 
3958 
wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)3959 static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
3960 					      char *cmd, char *buf,
3961 					      size_t buflen)
3962 {
3963 	int id;
3964 	size_t res;
3965 	struct wpa_cred *cred;
3966 	char *name, *value;
3967 
3968 	/* cmd: "<cred id> <variable name>" */
3969 	name = os_strchr(cmd, ' ');
3970 	if (name == NULL)
3971 		return -1;
3972 	*name++ = '\0';
3973 
3974 	id = atoi(cmd);
3975 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
3976 		   id, name);
3977 
3978 	cred = wpa_config_get_cred(wpa_s->conf, id);
3979 	if (cred == NULL) {
3980 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3981 			   id);
3982 		return -1;
3983 	}
3984 
3985 	value = wpa_config_get_cred_no_key(cred, name);
3986 	if (value == NULL) {
3987 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
3988 			   name);
3989 		return -1;
3990 	}
3991 
3992 	res = os_strlcpy(buf, value, buflen);
3993 	if (res >= buflen) {
3994 		os_free(value);
3995 		return -1;
3996 	}
3997 
3998 	os_free(value);
3999 
4000 	return res;
4001 }
4002 
4003 
4004 #ifndef CONFIG_NO_CONFIG_WRITE
wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant * wpa_s)4005 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
4006 {
4007 	int ret;
4008 
4009 	if (!wpa_s->conf->update_config) {
4010 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
4011 			   "to update configuration (update_config=0)");
4012 		return -1;
4013 	}
4014 
4015 	ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
4016 	if (ret) {
4017 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
4018 			   "update configuration");
4019 	} else {
4020 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
4021 			   " updated");
4022 	}
4023 
4024 	return ret;
4025 }
4026 #endif /* CONFIG_NO_CONFIG_WRITE */
4027 
4028 
4029 struct cipher_info {
4030 	unsigned int capa;
4031 	const char *name;
4032 	int group_only;
4033 };
4034 
4035 static const struct cipher_info ciphers[] = {
4036 	{ WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
4037 	{ WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
4038 	{ WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
4039 	{ WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
4040 #ifndef CONFIG_NO_TKIP
4041 	{ WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
4042 #endif /* CONFIG_NO_TKIP */
4043 	{ WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
4044 #ifdef CONFIG_WEP
4045 	{ WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
4046 	{ WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
4047 #endif /* CONFIG_WEP */
4048 };
4049 
4050 static const struct cipher_info ciphers_group_mgmt[] = {
4051 	{ WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
4052 	{ WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
4053 	{ WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
4054 	{ WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
4055 };
4056 
4057 
ctrl_iface_get_capability_pairwise(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4058 static int ctrl_iface_get_capability_pairwise(int res, bool strict,
4059 					      struct wpa_driver_capa *capa,
4060 					      char *buf, size_t buflen)
4061 {
4062 	int ret;
4063 	char *pos, *end;
4064 	size_t len;
4065 	unsigned int i;
4066 
4067 	pos = buf;
4068 	end = pos + buflen;
4069 
4070 	if (res < 0) {
4071 		if (strict)
4072 			return 0;
4073 #ifdef CONFIG_NO_TKIP
4074 		len = os_strlcpy(buf, "CCMP NONE", buflen);
4075 #else /* CONFIG_NO_TKIP */
4076 		len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
4077 #endif /* CONFIG_NO_TKIP */
4078 		if (len >= buflen)
4079 			return -1;
4080 		return len;
4081 	}
4082 
4083 	for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
4084 		if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
4085 			ret = os_snprintf(pos, end - pos, "%s%s",
4086 					  pos == buf ? "" : " ",
4087 					  ciphers[i].name);
4088 			if (os_snprintf_error(end - pos, ret))
4089 				return pos - buf;
4090 			pos += ret;
4091 		}
4092 	}
4093 
4094 	return pos - buf;
4095 }
4096 
4097 
ctrl_iface_get_capability_group(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4098 static int ctrl_iface_get_capability_group(int res, bool strict,
4099 					   struct wpa_driver_capa *capa,
4100 					   char *buf, size_t buflen)
4101 {
4102 	int ret;
4103 	char *pos, *end;
4104 	size_t len;
4105 	unsigned int i;
4106 
4107 	pos = buf;
4108 	end = pos + buflen;
4109 
4110 	if (res < 0) {
4111 		if (strict)
4112 			return 0;
4113 #ifdef CONFIG_WEP
4114 #ifdef CONFIG_NO_TKIP
4115 		len = os_strlcpy(buf, "CCMP WEP104 WEP40", buflen);
4116 #else /* CONFIG_NO_TKIP */
4117 		len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
4118 #endif /* CONFIG_NO_TKIP */
4119 #else /* CONFIG_WEP */
4120 #ifdef CONFIG_NO_TKIP
4121 		len = os_strlcpy(buf, "CCMP", buflen);
4122 #else /* CONFIG_NO_TKIP */
4123 		len = os_strlcpy(buf, "CCMP TKIP", buflen);
4124 #endif /* CONFIG_NO_TKIP */
4125 #endif /* CONFIG_WEP */
4126 		if (len >= buflen)
4127 			return -1;
4128 		return len;
4129 	}
4130 
4131 	for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
4132 		if (capa->enc & ciphers[i].capa) {
4133 			ret = os_snprintf(pos, end - pos, "%s%s",
4134 					  pos == buf ? "" : " ",
4135 					  ciphers[i].name);
4136 			if (os_snprintf_error(end - pos, ret))
4137 				return pos - buf;
4138 			pos += ret;
4139 		}
4140 	}
4141 
4142 	return pos - buf;
4143 }
4144 
4145 
ctrl_iface_get_capability_group_mgmt(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4146 static int ctrl_iface_get_capability_group_mgmt(int res, bool strict,
4147 						struct wpa_driver_capa *capa,
4148 						char *buf, size_t buflen)
4149 {
4150 	int ret;
4151 	char *pos, *end;
4152 	unsigned int i;
4153 
4154 	pos = buf;
4155 	end = pos + buflen;
4156 
4157 	if (res < 0)
4158 		return 0;
4159 
4160 	for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
4161 		if (capa->enc & ciphers_group_mgmt[i].capa) {
4162 			ret = os_snprintf(pos, end - pos, "%s%s",
4163 					  pos == buf ? "" : " ",
4164 					  ciphers_group_mgmt[i].name);
4165 			if (os_snprintf_error(end - pos, ret))
4166 				return pos - buf;
4167 			pos += ret;
4168 		}
4169 	}
4170 
4171 	return pos - buf;
4172 }
4173 
4174 
iftype_str_to_index(const char * iftype_str)4175 static int iftype_str_to_index(const char *iftype_str)
4176 {
4177 	if (!iftype_str)
4178 		return WPA_IF_MAX;
4179 
4180 	if (os_strcmp(iftype_str, "STATION") == 0)
4181 		return WPA_IF_STATION;
4182 
4183 	if (os_strcmp(iftype_str, "AP_VLAN") == 0)
4184 		return WPA_IF_AP_VLAN;
4185 
4186 	if (os_strcmp(iftype_str, "AP") == 0)
4187 		return WPA_IF_AP_BSS;
4188 
4189 	if (os_strcmp(iftype_str, "P2P_GO") == 0)
4190 		return WPA_IF_P2P_GO;
4191 
4192 	if (os_strcmp(iftype_str, "P2P_CLIENT") == 0)
4193 		return WPA_IF_P2P_CLIENT;
4194 
4195 	if (os_strcmp(iftype_str, "P2P_DEVICE") == 0)
4196 		return WPA_IF_P2P_DEVICE;
4197 
4198 	if (os_strcmp(iftype_str, "MESH") == 0)
4199 		return WPA_IF_MESH;
4200 
4201 	if (os_strcmp(iftype_str, "IBSS") == 0)
4202 		return WPA_IF_IBSS;
4203 
4204 	if (os_strcmp(iftype_str, "NAN") == 0)
4205 		return WPA_IF_NAN;
4206 
4207 	return WPA_IF_MAX;
4208 }
4209 
4210 
ctrl_iface_get_capability_key_mgmt(int res,bool strict,struct wpa_driver_capa * capa,const char * iftype_str,char * buf,size_t buflen)4211 static int ctrl_iface_get_capability_key_mgmt(int res, bool strict,
4212 					      struct wpa_driver_capa *capa,
4213 					      const char *iftype_str,
4214 					      char *buf, size_t buflen)
4215 {
4216 	int ret;
4217 	unsigned int key_mgmt;
4218 	char *pos, *end;
4219 	size_t len;
4220 
4221 	pos = buf;
4222 	end = pos + buflen;
4223 
4224 	if (res < 0) {
4225 		if (strict)
4226 			return 0;
4227 		len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
4228 				 "NONE", buflen);
4229 		if (len >= buflen)
4230 			return -1;
4231 		return len;
4232 	}
4233 
4234 	if (iftype_str) {
4235 		enum wpa_driver_if_type iftype;
4236 
4237 		iftype = iftype_str_to_index(iftype_str);
4238 		if (iftype == WPA_IF_MAX)
4239 			return -1;
4240 		key_mgmt = capa->key_mgmt_iftype[iftype];
4241 	} else {
4242 		key_mgmt = capa->key_mgmt;
4243 	}
4244 
4245 	ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
4246 	if (os_snprintf_error(end - pos, ret))
4247 		return pos - buf;
4248 	pos += ret;
4249 
4250 	if (key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4251 			WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
4252 		ret = os_snprintf(pos, end - pos, " WPA-EAP");
4253 		if (os_snprintf_error(end - pos, ret))
4254 			return pos - buf;
4255 		pos += ret;
4256 	}
4257 
4258 	if (key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
4259 			WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
4260 		ret = os_snprintf(pos, end - pos, " WPA-PSK");
4261 		if (os_snprintf_error(end - pos, ret))
4262 			return pos - buf;
4263 		pos += ret;
4264 	}
4265 
4266 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
4267 		ret = os_snprintf(pos, end - pos, " WPA-NONE");
4268 		if (os_snprintf_error(end - pos, ret))
4269 			return pos - buf;
4270 		pos += ret;
4271 	}
4272 
4273 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WAPI_PSK) {
4274 		ret = os_snprintf(pos, end - pos, " WAPI-PSK");
4275 		if (os_snprintf_error(end - pos, ret))
4276 			return pos - buf;
4277 		pos += ret;
4278 	}
4279 
4280 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_TPK_HANDSHAKE) {
4281 		ret = os_snprintf(pos, end - pos, " TPK-HANDSHAKE");
4282 		if (os_snprintf_error(end - pos, ret))
4283 			return pos - buf;
4284 		pos += ret;
4285 	}
4286 
4287 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_CCKM) {
4288 		ret = os_snprintf(pos, end - pos, " CCKM");
4289 		if (os_snprintf_error(end - pos, ret))
4290 			return pos - buf;
4291 		pos += ret;
4292 	}
4293 
4294 #ifdef CONFIG_SUITEB
4295 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
4296 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
4297 		if (os_snprintf_error(end - pos, ret))
4298 			return pos - buf;
4299 		pos += ret;
4300 	}
4301 #endif /* CONFIG_SUITEB */
4302 #ifdef CONFIG_SUITEB192
4303 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
4304 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
4305 		if (os_snprintf_error(end - pos, ret))
4306 			return pos - buf;
4307 		pos += ret;
4308 	}
4309 #endif /* CONFIG_SUITEB192 */
4310 #ifdef CONFIG_OWE
4311 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OWE) {
4312 		ret = os_snprintf(pos, end - pos, " OWE");
4313 		if (os_snprintf_error(end - pos, ret))
4314 			return pos - buf;
4315 		pos += ret;
4316 	}
4317 #endif /* CONFIG_OWE */
4318 #ifdef CONFIG_DPP
4319 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_DPP) {
4320 		ret = os_snprintf(pos, end - pos, " DPP");
4321 		if (os_snprintf_error(end - pos, ret))
4322 			return pos - buf;
4323 		pos += ret;
4324 	}
4325 #endif /* CONFIG_DPP */
4326 #ifdef CONFIG_FILS
4327 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA256) {
4328 		ret = os_snprintf(pos, end - pos, " FILS-SHA256");
4329 		if (os_snprintf_error(end - pos, ret))
4330 			return pos - buf;
4331 		pos += ret;
4332 	}
4333 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA384) {
4334 		ret = os_snprintf(pos, end - pos, " FILS-SHA384");
4335 		if (os_snprintf_error(end - pos, ret))
4336 			return pos - buf;
4337 		pos += ret;
4338 	}
4339 #ifdef CONFIG_IEEE80211R
4340 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA256) {
4341 		ret = os_snprintf(pos, end - pos, " FT-FILS-SHA256");
4342 		if (os_snprintf_error(end - pos, ret))
4343 			return pos - buf;
4344 		pos += ret;
4345 	}
4346 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA384) {
4347 		ret = os_snprintf(pos, end - pos, " FT-FILS-SHA384");
4348 		if (os_snprintf_error(end - pos, ret))
4349 			return pos - buf;
4350 		pos += ret;
4351 	}
4352 #endif /* CONFIG_IEEE80211R */
4353 #endif /* CONFIG_FILS */
4354 #ifdef CONFIG_IEEE80211R
4355 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK) {
4356 		ret = os_snprintf(pos, end - pos, " FT-PSK");
4357 		if (os_snprintf_error(end - pos, ret))
4358 			return pos - buf;
4359 		pos += ret;
4360 	}
4361 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) {
4362 		ret = os_snprintf(pos, end - pos, " FT-EAP");
4363 		if (os_snprintf_error(end - pos, ret))
4364 			return pos - buf;
4365 		pos += ret;
4366 	}
4367 #ifdef CONFIG_SAE
4368 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_SAE) {
4369 		ret = os_snprintf(pos, end - pos, " FT-SAE");
4370 		if (os_snprintf_error(end - pos, ret))
4371 			return pos - buf;
4372 		pos += ret;
4373 	}
4374 #endif /* CONFIG_SAE */
4375 #ifdef CONFIG_SHA384
4376 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_802_1X_SHA384) {
4377 		ret = os_snprintf(pos, end - pos, " FT-EAP-SHA384");
4378 		if (os_snprintf_error(end - pos, ret))
4379 			return pos - buf;
4380 		pos += ret;
4381 	}
4382 #endif /* CONFIG_SHA384 */
4383 #endif /* CONFIG_IEEE80211R */
4384 #ifdef CONFIG_SAE
4385 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE) {
4386 		ret = os_snprintf(pos, end - pos, " SAE");
4387 		if (os_snprintf_error(end - pos, ret))
4388 			return pos - buf;
4389 		pos += ret;
4390 	}
4391 #endif /* CONFIG_SAE */
4392 #ifdef CONFIG_SHA256
4393 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_802_1X_SHA256) {
4394 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SHA256");
4395 		if (os_snprintf_error(end - pos, ret))
4396 			return pos - buf;
4397 		pos += ret;
4398 	}
4399 
4400 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_PSK_SHA256) {
4401 		ret = os_snprintf(pos, end - pos, " WPA-PSK-SHA256");
4402 		if (os_snprintf_error(end - pos, ret))
4403 			return pos - buf;
4404 		pos += ret;
4405 	}
4406 #endif /* CONFIG_SHA256 */
4407 #ifdef CONFIG_HS20
4408 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OSEN) {
4409 		ret = os_snprintf(pos, end - pos, " OSEN");
4410 		if (os_snprintf_error(end - pos, ret))
4411 			return pos - buf;
4412 		pos += ret;
4413 	}
4414 #endif /* CONFIG_HS20 */
4415 
4416 	return pos - buf;
4417 }
4418 
4419 
ctrl_iface_get_capability_proto(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4420 static int ctrl_iface_get_capability_proto(int res, bool strict,
4421 					   struct wpa_driver_capa *capa,
4422 					   char *buf, size_t buflen)
4423 {
4424 	int ret;
4425 	char *pos, *end;
4426 	size_t len;
4427 
4428 	pos = buf;
4429 	end = pos + buflen;
4430 
4431 	if (res < 0) {
4432 		if (strict)
4433 			return 0;
4434 		len = os_strlcpy(buf, "RSN WPA", buflen);
4435 		if (len >= buflen)
4436 			return -1;
4437 		return len;
4438 	}
4439 
4440 	if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
4441 			      WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
4442 		ret = os_snprintf(pos, end - pos, "%sRSN",
4443 				  pos == buf ? "" : " ");
4444 		if (os_snprintf_error(end - pos, ret))
4445 			return pos - buf;
4446 		pos += ret;
4447 	}
4448 
4449 	if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4450 			      WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
4451 		ret = os_snprintf(pos, end - pos, "%sWPA",
4452 				  pos == buf ? "" : " ");
4453 		if (os_snprintf_error(end - pos, ret))
4454 			return pos - buf;
4455 		pos += ret;
4456 	}
4457 
4458 	return pos - buf;
4459 }
4460 
4461 
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)4462 static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
4463 					      int res, bool strict,
4464 					      struct wpa_driver_capa *capa,
4465 					      char *buf, size_t buflen)
4466 {
4467 	int ret;
4468 	char *pos, *end;
4469 	size_t len;
4470 
4471 	pos = buf;
4472 	end = pos + buflen;
4473 
4474 	if (res < 0) {
4475 		if (strict)
4476 			return 0;
4477 		len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
4478 		if (len >= buflen)
4479 			return -1;
4480 		return len;
4481 	}
4482 
4483 	if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
4484 		ret = os_snprintf(pos, end - pos, "%sOPEN",
4485 				  pos == buf ? "" : " ");
4486 		if (os_snprintf_error(end - pos, ret))
4487 			return pos - buf;
4488 		pos += ret;
4489 	}
4490 
4491 	if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
4492 		ret = os_snprintf(pos, end - pos, "%sSHARED",
4493 				  pos == buf ? "" : " ");
4494 		if (os_snprintf_error(end - pos, ret))
4495 			return pos - buf;
4496 		pos += ret;
4497 	}
4498 
4499 	if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
4500 		ret = os_snprintf(pos, end - pos, "%sLEAP",
4501 				  pos == buf ? "" : " ");
4502 		if (os_snprintf_error(end - pos, ret))
4503 			return pos - buf;
4504 		pos += ret;
4505 	}
4506 
4507 #ifdef CONFIG_SAE
4508 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
4509 		ret = os_snprintf(pos, end - pos, "%sSAE",
4510 				  pos == buf ? "" : " ");
4511 		if (os_snprintf_error(end - pos, ret))
4512 			return pos - buf;
4513 		pos += ret;
4514 	}
4515 #endif /* CONFIG_SAE */
4516 
4517 #ifdef CONFIG_FILS
4518 	if (wpa_is_fils_supported(wpa_s)) {
4519 		ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITHOUT_PFS",
4520 				  pos == buf ? "" : " ");
4521 		if (os_snprintf_error(end - pos, ret))
4522 			return pos - buf;
4523 		pos += ret;
4524 	}
4525 
4526 #ifdef CONFIG_FILS_SK_PFS
4527 	if (wpa_is_fils_sk_pfs_supported(wpa_s)) {
4528 		ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITH_PFS",
4529 				  pos == buf ? "" : " ");
4530 		if (os_snprintf_error(end - pos, ret))
4531 			return pos - buf;
4532 		pos += ret;
4533 	}
4534 #endif /* CONFIG_FILS_SK_PFS */
4535 #endif /* CONFIG_FILS */
4536 
4537 #ifdef CONFIG_PASN
4538 	ret = os_snprintf(pos, end - pos, "%sPASN",
4539 			  pos == buf ? "" : " ");
4540 	if (os_snprintf_error(end - pos, ret))
4541 		return pos - buf;
4542 	pos += ret;
4543 
4544 #endif /* CONFIG_PASN */
4545 
4546 	return pos - buf;
4547 }
4548 
4549 
ctrl_iface_get_capability_modes(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4550 static int ctrl_iface_get_capability_modes(int res, bool strict,
4551 					   struct wpa_driver_capa *capa,
4552 					   char *buf, size_t buflen)
4553 {
4554 	int ret;
4555 	char *pos, *end;
4556 	size_t len;
4557 
4558 	pos = buf;
4559 	end = pos + buflen;
4560 
4561 	if (res < 0) {
4562 		if (strict)
4563 			return 0;
4564 		len = os_strlcpy(buf, "IBSS AP", buflen);
4565 		if (len >= buflen)
4566 			return -1;
4567 		return len;
4568 	}
4569 
4570 	if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
4571 		ret = os_snprintf(pos, end - pos, "%sIBSS",
4572 				  pos == buf ? "" : " ");
4573 		if (os_snprintf_error(end - pos, ret))
4574 			return pos - buf;
4575 		pos += ret;
4576 	}
4577 
4578 	if (capa->flags & WPA_DRIVER_FLAGS_AP) {
4579 		ret = os_snprintf(pos, end - pos, "%sAP",
4580 				  pos == buf ? "" : " ");
4581 		if (os_snprintf_error(end - pos, ret))
4582 			return pos - buf;
4583 		pos += ret;
4584 	}
4585 
4586 #ifdef CONFIG_MESH
4587 	if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
4588 		ret = os_snprintf(pos, end - pos, "%sMESH",
4589 				  pos == buf ? "" : " ");
4590 		if (os_snprintf_error(end - pos, ret))
4591 			return pos - buf;
4592 		pos += ret;
4593 	}
4594 #endif /* CONFIG_MESH */
4595 
4596 	return pos - buf;
4597 }
4598 
4599 
ctrl_iface_get_capability_channels(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)4600 static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
4601 					      char *buf, size_t buflen)
4602 {
4603 	struct hostapd_channel_data *chnl;
4604 	int ret, i, j;
4605 	char *pos, *end, *hmode;
4606 
4607 	pos = buf;
4608 	end = pos + buflen;
4609 
4610 	for (j = 0; j < wpa_s->hw.num_modes; j++) {
4611 		switch (wpa_s->hw.modes[j].mode) {
4612 		case HOSTAPD_MODE_IEEE80211B:
4613 			hmode = "B";
4614 			break;
4615 		case HOSTAPD_MODE_IEEE80211G:
4616 			hmode = "G";
4617 			break;
4618 		case HOSTAPD_MODE_IEEE80211A:
4619 			hmode = "A";
4620 			break;
4621 		case HOSTAPD_MODE_IEEE80211AD:
4622 			hmode = "AD";
4623 			break;
4624 		default:
4625 			continue;
4626 		}
4627 		ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
4628 		if (os_snprintf_error(end - pos, ret))
4629 			return pos - buf;
4630 		pos += ret;
4631 		chnl = wpa_s->hw.modes[j].channels;
4632 		for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
4633 			if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
4634 				continue;
4635 			ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
4636 			if (os_snprintf_error(end - pos, ret))
4637 				return pos - buf;
4638 			pos += ret;
4639 		}
4640 		ret = os_snprintf(pos, end - pos, "\n");
4641 		if (os_snprintf_error(end - pos, ret))
4642 			return pos - buf;
4643 		pos += ret;
4644 	}
4645 
4646 	return pos - buf;
4647 }
4648 
4649 
ctrl_iface_get_capability_freq(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)4650 static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
4651 					  char *buf, size_t buflen)
4652 {
4653 	struct hostapd_channel_data *chnl;
4654 	int ret, i, j;
4655 	char *pos, *end, *hmode;
4656 
4657 	pos = buf;
4658 	end = pos + buflen;
4659 
4660 	for (j = 0; j < wpa_s->hw.num_modes; j++) {
4661 		switch (wpa_s->hw.modes[j].mode) {
4662 		case HOSTAPD_MODE_IEEE80211B:
4663 			hmode = "B";
4664 			break;
4665 		case HOSTAPD_MODE_IEEE80211G:
4666 			hmode = "G";
4667 			break;
4668 		case HOSTAPD_MODE_IEEE80211A:
4669 			hmode = "A";
4670 			break;
4671 		case HOSTAPD_MODE_IEEE80211AD:
4672 			hmode = "AD";
4673 			break;
4674 		default:
4675 			continue;
4676 		}
4677 		ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
4678 				  hmode);
4679 		if (os_snprintf_error(end - pos, ret))
4680 			return pos - buf;
4681 		pos += ret;
4682 		chnl = wpa_s->hw.modes[j].channels;
4683 		for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
4684 			if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
4685 				continue;
4686 			ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
4687 					  chnl[i].chan, chnl[i].freq,
4688 					  chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
4689 					  " (NO_IR)" : "",
4690 					  chnl[i].flag & HOSTAPD_CHAN_RADAR ?
4691 					  " (DFS)" : "");
4692 
4693 			if (os_snprintf_error(end - pos, ret))
4694 				return pos - buf;
4695 			pos += ret;
4696 		}
4697 		ret = os_snprintf(pos, end - pos, "\n");
4698 		if (os_snprintf_error(end - pos, ret))
4699 			return pos - buf;
4700 		pos += ret;
4701 	}
4702 
4703 	return pos - buf;
4704 }
4705 
4706 
wpa_supplicant_ctrl_iface_get_capability(struct wpa_supplicant * wpa_s,const char * _field,char * buf,size_t buflen)4707 static int wpa_supplicant_ctrl_iface_get_capability(
4708 	struct wpa_supplicant *wpa_s, const char *_field, char *buf,
4709 	size_t buflen)
4710 {
4711 	struct wpa_driver_capa capa;
4712 	int res;
4713 	char *next_param, *curr_param, *iftype = NULL;
4714 	bool strict = false;
4715 	char field[50];
4716 	size_t len;
4717 
4718 	/* Determine whether or not strict checking was requested */
4719 	len = os_strlcpy(field, _field, sizeof(field));
4720 	if (len >= sizeof(field))
4721 		return -1;
4722 
4723 	next_param = os_strchr(field, ' ');
4724 	while (next_param) {
4725 		*next_param++ = '\0';
4726 		curr_param = next_param;
4727 		next_param = os_strchr(next_param, ' ');
4728 
4729 		if (next_param)
4730 			*next_param = '\0';
4731 
4732 		if (os_strcmp(curr_param, "strict") == 0)
4733 			strict = true;
4734 		else if (os_strncmp(curr_param, "iftype=", 7) == 0)
4735 			iftype = curr_param + 7;
4736 		else
4737 			return -1;
4738 	}
4739 
4740 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'%s%s%s",
4741 		   field, iftype ? " iftype=" : "", iftype ? iftype : "",
4742 		   strict ? " strict" : "");
4743 
4744 	if (os_strcmp(field, "eap") == 0) {
4745 		return eap_get_names(buf, buflen);
4746 	}
4747 
4748 	res = wpa_drv_get_capa(wpa_s, &capa);
4749 
4750 	if (os_strcmp(field, "pairwise") == 0)
4751 		return ctrl_iface_get_capability_pairwise(res, strict, &capa,
4752 							  buf, buflen);
4753 
4754 	if (os_strcmp(field, "group") == 0)
4755 		return ctrl_iface_get_capability_group(res, strict, &capa,
4756 						       buf, buflen);
4757 
4758 	if (os_strcmp(field, "group_mgmt") == 0)
4759 		return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
4760 							    buf, buflen);
4761 
4762 	if (os_strcmp(field, "key_mgmt") == 0)
4763 		return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
4764 							  iftype, buf, buflen);
4765 
4766 	if (os_strcmp(field, "proto") == 0)
4767 		return ctrl_iface_get_capability_proto(res, strict, &capa,
4768 						       buf, buflen);
4769 
4770 	if (os_strcmp(field, "auth_alg") == 0)
4771 		return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
4772 							  &capa, buf, buflen);
4773 
4774 	if (os_strcmp(field, "modes") == 0)
4775 		return ctrl_iface_get_capability_modes(res, strict, &capa,
4776 						       buf, buflen);
4777 
4778 	if (os_strcmp(field, "channels") == 0)
4779 		return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
4780 
4781 	if (os_strcmp(field, "freq") == 0)
4782 		return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
4783 
4784 #ifdef CONFIG_TDLS
4785 	if (os_strcmp(field, "tdls") == 0)
4786 		return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
4787 #endif /* CONFIG_TDLS */
4788 
4789 #ifdef CONFIG_ERP
4790 	if (os_strcmp(field, "erp") == 0) {
4791 		res = os_snprintf(buf, buflen, "ERP");
4792 		if (os_snprintf_error(buflen, res))
4793 			return -1;
4794 		return res;
4795 	}
4796 #endif /* CONFIG_EPR */
4797 
4798 #ifdef CONFIG_FIPS
4799 	if (os_strcmp(field, "fips") == 0) {
4800 		res = os_snprintf(buf, buflen, "FIPS");
4801 		if (os_snprintf_error(buflen, res))
4802 			return -1;
4803 		return res;
4804 	}
4805 #endif /* CONFIG_FIPS */
4806 
4807 #ifdef CONFIG_ACS
4808 	if (os_strcmp(field, "acs") == 0) {
4809 		res = os_snprintf(buf, buflen, "ACS");
4810 		if (os_snprintf_error(buflen, res))
4811 			return -1;
4812 		return res;
4813 	}
4814 #endif /* CONFIG_ACS */
4815 
4816 #ifdef CONFIG_FILS
4817 	if (os_strcmp(field, "fils") == 0) {
4818 #ifdef CONFIG_FILS_SK_PFS
4819 		if (wpa_is_fils_supported(wpa_s) &&
4820 		    wpa_is_fils_sk_pfs_supported(wpa_s)) {
4821 			res = os_snprintf(buf, buflen, "FILS FILS-SK-PFS");
4822 			if (os_snprintf_error(buflen, res))
4823 				return -1;
4824 			return res;
4825 		}
4826 #endif /* CONFIG_FILS_SK_PFS */
4827 
4828 		if (wpa_is_fils_supported(wpa_s)) {
4829 			res = os_snprintf(buf, buflen, "FILS");
4830 			if (os_snprintf_error(buflen, res))
4831 				return -1;
4832 			return res;
4833 		}
4834 	}
4835 #endif /* CONFIG_FILS */
4836 
4837 	if (os_strcmp(field, "multibss") == 0 && wpa_s->multi_bss_support) {
4838 		res = os_snprintf(buf, buflen, "MULTIBSS-STA");
4839 		if (os_snprintf_error(buflen, res))
4840 			return -1;
4841 		return res;
4842 	}
4843 
4844 #ifdef CONFIG_DPP
4845 	if (os_strcmp(field, "dpp") == 0) {
4846 #ifdef CONFIG_DPP3
4847 		res = os_snprintf(buf, buflen, "DPP=3");
4848 #elif defined(CONFIG_DPP2)
4849 		res = os_snprintf(buf, buflen, "DPP=2");
4850 #else /* CONFIG_DPP2 */
4851 		res = os_snprintf(buf, buflen, "DPP=1");
4852 #endif /* CONFIG_DPP2 */
4853 		if (os_snprintf_error(buflen, res))
4854 			return -1;
4855 		return res;
4856 	}
4857 #endif /* CONFIG_DPP */
4858 
4859 #ifdef CONFIG_SAE
4860 	if (os_strcmp(field, "sae") == 0 &&
4861 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE)) {
4862 #ifdef CONFIG_SAE_PK
4863 		res = os_snprintf(buf, buflen, "H2E PK");
4864 #else /* CONFIG_SAE_PK */
4865 		res = os_snprintf(buf, buflen, "H2E");
4866 #endif /* CONFIG_SAE_PK */
4867 		if (os_snprintf_error(buflen, res))
4868 			return -1;
4869 		return res;
4870 	}
4871 #endif /* CONFIG_SAE */
4872 
4873 #ifdef CONFIG_OCV
4874 	if (os_strcmp(field, "ocv") == 0) {
4875 		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
4876 		    (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_OCV))
4877 			res = os_snprintf(buf, buflen, "supported");
4878 		else
4879 			res = os_snprintf(buf, buflen, "not supported");
4880 		if (os_snprintf_error(buflen, res))
4881 			return -1;
4882 		return res;
4883 	}
4884 #endif /* CONFIG_OCV */
4885 
4886 	if (os_strcmp(field, "beacon_prot") == 0) {
4887 		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_BEACON_PROTECTION) ||
4888 		    (wpa_s->drv_flags2 &
4889 		     WPA_DRIVER_FLAGS2_BEACON_PROTECTION_CLIENT))
4890 			res = os_snprintf(buf, buflen, "supported");
4891 		else
4892 			res = os_snprintf(buf, buflen, "not supported");
4893 		if (os_snprintf_error(buflen, res))
4894 			return -1;
4895 		return res;
4896 	}
4897 
4898 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
4899 		   field);
4900 
4901 	return -1;
4902 }
4903 
4904 
4905 #ifdef CONFIG_INTERWORKING
anqp_add_hex(char * pos,char * end,const char * title,struct wpabuf * data)4906 static char * anqp_add_hex(char *pos, char *end, const char *title,
4907 			   struct wpabuf *data)
4908 {
4909 	char *start = pos;
4910 	size_t i;
4911 	int ret;
4912 	const u8 *d;
4913 
4914 	if (data == NULL)
4915 		return start;
4916 
4917 	ret = os_snprintf(pos, end - pos, "%s=", title);
4918 	if (os_snprintf_error(end - pos, ret))
4919 		return start;
4920 	pos += ret;
4921 
4922 	d = wpabuf_head_u8(data);
4923 	for (i = 0; i < wpabuf_len(data); i++) {
4924 		ret = os_snprintf(pos, end - pos, "%02x", *d++);
4925 		if (os_snprintf_error(end - pos, ret))
4926 			return start;
4927 		pos += ret;
4928 	}
4929 
4930 	ret = os_snprintf(pos, end - pos, "\n");
4931 	if (os_snprintf_error(end - pos, ret))
4932 		return start;
4933 	pos += ret;
4934 
4935 	return pos;
4936 }
4937 #endif /* CONFIG_INTERWORKING */
4938 
4939 
4940 #ifdef CONFIG_FILS
print_fils_indication(struct wpa_bss * bss,char * pos,char * end)4941 static int print_fils_indication(struct wpa_bss *bss, char *pos, char *end)
4942 {
4943 	char *start = pos;
4944 	const u8 *ie, *ie_end;
4945 	u16 info, realms;
4946 	int ret;
4947 
4948 	ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
4949 	if (!ie)
4950 		return 0;
4951 	ie_end = ie + 2 + ie[1];
4952 	ie += 2;
4953 	if (ie_end - ie < 2)
4954 		return -1;
4955 
4956 	info = WPA_GET_LE16(ie);
4957 	ie += 2;
4958 	ret = os_snprintf(pos, end - pos, "fils_info=%04x\n", info);
4959 	if (os_snprintf_error(end - pos, ret))
4960 		return 0;
4961 	pos += ret;
4962 
4963 	if (info & BIT(7)) {
4964 		/* Cache Identifier Included */
4965 		if (ie_end - ie < 2)
4966 			return -1;
4967 		ret = os_snprintf(pos, end - pos, "fils_cache_id=%02x%02x\n",
4968 				  ie[0], ie[1]);
4969 		if (os_snprintf_error(end - pos, ret))
4970 			return 0;
4971 		pos += ret;
4972 		ie += 2;
4973 	}
4974 
4975 	if (info & BIT(8)) {
4976 		/* HESSID Included */
4977 		if (ie_end - ie < ETH_ALEN)
4978 			return -1;
4979 		ret = os_snprintf(pos, end - pos, "fils_hessid=" MACSTR "\n",
4980 				  MAC2STR(ie));
4981 		if (os_snprintf_error(end - pos, ret))
4982 			return 0;
4983 		pos += ret;
4984 		ie += ETH_ALEN;
4985 	}
4986 
4987 	realms = (info & (BIT(3) | BIT(4) | BIT(5))) >> 3;
4988 	if (realms) {
4989 		if (ie_end - ie < realms * 2)
4990 			return -1;
4991 		ret = os_snprintf(pos, end - pos, "fils_realms=");
4992 		if (os_snprintf_error(end - pos, ret))
4993 			return 0;
4994 		pos += ret;
4995 
4996 		ret = wpa_snprintf_hex(pos, end - pos, ie, realms * 2);
4997 		if (ret <= 0)
4998 			return 0;
4999 		pos += ret;
5000 		ie += realms * 2;
5001 		ret = os_snprintf(pos, end - pos, "\n");
5002 		if (os_snprintf_error(end - pos, ret))
5003 			return 0;
5004 		pos += ret;
5005 	}
5006 
5007 	return pos - start;
5008 }
5009 #endif /* CONFIG_FILS */
5010 
5011 
print_bss_info(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,unsigned long mask,char * buf,size_t buflen)5012 static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5013 			  unsigned long mask, char *buf, size_t buflen)
5014 {
5015 	size_t i;
5016 	int ret;
5017 	char *pos, *end;
5018 	const u8 *ie, *ie2, *osen_ie, *mesh, *owe, *rsnxe;
5019 
5020 	pos = buf;
5021 	end = buf + buflen;
5022 
5023 	if (mask & WPA_BSS_MASK_ID) {
5024 		ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
5025 		if (os_snprintf_error(end - pos, ret))
5026 			return 0;
5027 		pos += ret;
5028 	}
5029 
5030 	if (mask & WPA_BSS_MASK_BSSID) {
5031 		ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
5032 				  MAC2STR(bss->bssid));
5033 		if (os_snprintf_error(end - pos, ret))
5034 			return 0;
5035 		pos += ret;
5036 	}
5037 
5038 	if (mask & WPA_BSS_MASK_FREQ) {
5039 		ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
5040 		if (os_snprintf_error(end - pos, ret))
5041 			return 0;
5042 		pos += ret;
5043 	}
5044 
5045 	if (mask & WPA_BSS_MASK_BEACON_INT) {
5046 		ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
5047 				  bss->beacon_int);
5048 		if (os_snprintf_error(end - pos, ret))
5049 			return 0;
5050 		pos += ret;
5051 	}
5052 
5053 	if (mask & WPA_BSS_MASK_CAPABILITIES) {
5054 		ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
5055 				  bss->caps);
5056 		if (os_snprintf_error(end - pos, ret))
5057 			return 0;
5058 		pos += ret;
5059 	}
5060 
5061 	if (mask & WPA_BSS_MASK_QUAL) {
5062 		ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
5063 		if (os_snprintf_error(end - pos, ret))
5064 			return 0;
5065 		pos += ret;
5066 	}
5067 
5068 	if (mask & WPA_BSS_MASK_NOISE) {
5069 		ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
5070 		if (os_snprintf_error(end - pos, ret))
5071 			return 0;
5072 		pos += ret;
5073 	}
5074 
5075 	if (mask & WPA_BSS_MASK_LEVEL) {
5076 		ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
5077 		if (os_snprintf_error(end - pos, ret))
5078 			return 0;
5079 		pos += ret;
5080 	}
5081 
5082 	if (mask & WPA_BSS_MASK_TSF) {
5083 		ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
5084 				  (unsigned long long) bss->tsf);
5085 		if (os_snprintf_error(end - pos, ret))
5086 			return 0;
5087 		pos += ret;
5088 	}
5089 
5090 	if (mask & WPA_BSS_MASK_AGE) {
5091 		struct os_reltime now;
5092 
5093 		os_get_reltime(&now);
5094 		ret = os_snprintf(pos, end - pos, "age=%d\n",
5095 				  (int) (now.sec - bss->last_update.sec));
5096 		if (os_snprintf_error(end - pos, ret))
5097 			return 0;
5098 		pos += ret;
5099 	}
5100 
5101 	if (mask & WPA_BSS_MASK_IE) {
5102 		ret = os_snprintf(pos, end - pos, "ie=");
5103 		if (os_snprintf_error(end - pos, ret))
5104 			return 0;
5105 		pos += ret;
5106 
5107 		ie = wpa_bss_ie_ptr(bss);
5108 		for (i = 0; i < bss->ie_len; i++) {
5109 			ret = os_snprintf(pos, end - pos, "%02x", *ie++);
5110 			if (os_snprintf_error(end - pos, ret))
5111 				return 0;
5112 			pos += ret;
5113 		}
5114 
5115 		ret = os_snprintf(pos, end - pos, "\n");
5116 		if (os_snprintf_error(end - pos, ret))
5117 			return 0;
5118 		pos += ret;
5119 	}
5120 
5121 	if (mask & WPA_BSS_MASK_FLAGS) {
5122 		ret = os_snprintf(pos, end - pos, "flags=");
5123 		if (os_snprintf_error(end - pos, ret))
5124 			return 0;
5125 		pos += ret;
5126 
5127 		mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
5128 
5129 		ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
5130 		if (ie)
5131 			pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
5132 						    2 + ie[1]);
5133 		ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
5134 		if (ie2)
5135 			pos = wpa_supplicant_ie_txt(pos, end,
5136 						    mesh ? "RSN" : "WPA2", ie2,
5137 						    2 + ie2[1]);
5138 		rsnxe = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
5139 		if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_H2E)) {
5140 			ret = os_snprintf(pos, end - pos, "[SAE-H2E]");
5141 			if (os_snprintf_error(end - pos, ret))
5142 				return -1;
5143 			pos += ret;
5144 		}
5145 		if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_PK)) {
5146 			ret = os_snprintf(pos, end - pos, "[SAE-PK]");
5147 			if (os_snprintf_error(end - pos, ret))
5148 				return -1;
5149 			pos += ret;
5150 		}
5151 		osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
5152 		if (osen_ie)
5153 			pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
5154 						    osen_ie, 2 + osen_ie[1]);
5155 		owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
5156 		if (owe) {
5157 			ret = os_snprintf(
5158 				pos, end - pos,
5159 				ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
5160 			if (os_snprintf_error(end - pos, ret))
5161 				return 0;
5162 			pos += ret;
5163 		}
5164 		pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
5165 		if (!ie && !ie2 && !osen_ie &&
5166 		    (bss->caps & IEEE80211_CAP_PRIVACY)) {
5167 			ret = os_snprintf(pos, end - pos, "[WEP]");
5168 			if (os_snprintf_error(end - pos, ret))
5169 				return 0;
5170 			pos += ret;
5171 		}
5172 
5173 		if (mesh) {
5174 			ret = os_snprintf(pos, end - pos, "[MESH]");
5175 			if (os_snprintf_error(end - pos, ret))
5176 				return 0;
5177 			pos += ret;
5178 		}
5179 
5180 		if (bss_is_dmg(bss)) {
5181 			const char *s;
5182 			ret = os_snprintf(pos, end - pos, "[DMG]");
5183 			if (os_snprintf_error(end - pos, ret))
5184 				return 0;
5185 			pos += ret;
5186 			switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
5187 			case IEEE80211_CAP_DMG_IBSS:
5188 				s = "[IBSS]";
5189 				break;
5190 			case IEEE80211_CAP_DMG_AP:
5191 				s = "[ESS]";
5192 				break;
5193 			case IEEE80211_CAP_DMG_PBSS:
5194 				s = "[PBSS]";
5195 				break;
5196 			default:
5197 				s = "";
5198 				break;
5199 			}
5200 			ret = os_snprintf(pos, end - pos, "%s", s);
5201 			if (os_snprintf_error(end - pos, ret))
5202 				return 0;
5203 			pos += ret;
5204 		} else {
5205 			if (bss->caps & IEEE80211_CAP_IBSS) {
5206 				ret = os_snprintf(pos, end - pos, "[IBSS]");
5207 				if (os_snprintf_error(end - pos, ret))
5208 					return 0;
5209 				pos += ret;
5210 			}
5211 			if (bss->caps & IEEE80211_CAP_ESS) {
5212 				ret = os_snprintf(pos, end - pos, "[ESS]");
5213 				if (os_snprintf_error(end - pos, ret))
5214 					return 0;
5215 				pos += ret;
5216 			}
5217 		}
5218 		if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
5219 		    wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
5220 			ret = os_snprintf(pos, end - pos, "[P2P]");
5221 			if (os_snprintf_error(end - pos, ret))
5222 				return 0;
5223 			pos += ret;
5224 		}
5225 #ifdef CONFIG_HS20
5226 		if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
5227 			ret = os_snprintf(pos, end - pos, "[HS20]");
5228 			if (os_snprintf_error(end - pos, ret))
5229 				return 0;
5230 			pos += ret;
5231 		}
5232 #endif /* CONFIG_HS20 */
5233 #ifdef CONFIG_FILS
5234 		if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
5235 			ret = os_snprintf(pos, end - pos, "[FILS]");
5236 			if (os_snprintf_error(end - pos, ret))
5237 				return 0;
5238 			pos += ret;
5239 		}
5240 #endif /* CONFIG_FILS */
5241 #ifdef CONFIG_FST
5242 		if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
5243 			ret = os_snprintf(pos, end - pos, "[FST]");
5244 			if (os_snprintf_error(end - pos, ret))
5245 				return 0;
5246 			pos += ret;
5247 		}
5248 #endif /* CONFIG_FST */
5249 		if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_UTF_8_SSID)) {
5250 			ret = os_snprintf(pos, end - pos, "[UTF-8]");
5251 			if (os_snprintf_error(end - pos, ret))
5252 				return 0;
5253 			pos += ret;
5254 		}
5255 
5256 		ret = os_snprintf(pos, end - pos, "\n");
5257 		if (os_snprintf_error(end - pos, ret))
5258 			return 0;
5259 		pos += ret;
5260 	}
5261 
5262 	if (mask & WPA_BSS_MASK_SSID) {
5263 		ret = os_snprintf(pos, end - pos, "ssid=%s\n",
5264 				  wpa_ssid_txt(bss->ssid, bss->ssid_len));
5265 		if (os_snprintf_error(end - pos, ret))
5266 			return 0;
5267 		pos += ret;
5268 	}
5269 
5270 #ifdef CONFIG_WPS
5271 	if (mask & WPA_BSS_MASK_WPS_SCAN) {
5272 		ie = wpa_bss_ie_ptr(bss);
5273 		ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
5274 		if (ret >= end - pos)
5275 			return 0;
5276 		if (ret > 0)
5277 			pos += ret;
5278 	}
5279 #endif /* CONFIG_WPS */
5280 
5281 #ifdef CONFIG_P2P
5282 	if (mask & WPA_BSS_MASK_P2P_SCAN) {
5283 		ie = wpa_bss_ie_ptr(bss);
5284 		ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
5285 		if (ret >= end - pos)
5286 			return 0;
5287 		if (ret > 0)
5288 			pos += ret;
5289 	}
5290 #endif /* CONFIG_P2P */
5291 
5292 #ifdef CONFIG_WIFI_DISPLAY
5293 	if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
5294 		struct wpabuf *wfd;
5295 
5296 		ie = wpa_bss_ie_ptr(bss);
5297 		wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
5298 						  WFD_IE_VENDOR_TYPE);
5299 		if (wfd) {
5300 			ret = os_snprintf(pos, end - pos, "wfd_subelems=");
5301 			if (os_snprintf_error(end - pos, ret)) {
5302 				wpabuf_free(wfd);
5303 				return 0;
5304 			}
5305 			pos += ret;
5306 
5307 			pos += wpa_snprintf_hex(pos, end - pos,
5308 						wpabuf_head(wfd),
5309 						wpabuf_len(wfd));
5310 			wpabuf_free(wfd);
5311 
5312 			ret = os_snprintf(pos, end - pos, "\n");
5313 			if (os_snprintf_error(end - pos, ret))
5314 				return 0;
5315 			pos += ret;
5316 		}
5317 	}
5318 #endif /* CONFIG_WIFI_DISPLAY */
5319 
5320 #ifdef CONFIG_INTERWORKING
5321 	if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
5322 		struct wpa_bss_anqp *anqp = bss->anqp;
5323 		struct wpa_bss_anqp_elem *elem;
5324 
5325 		pos = anqp_add_hex(pos, end, "anqp_capability_list",
5326 				   anqp->capability_list);
5327 		pos = anqp_add_hex(pos, end, "anqp_venue_name",
5328 				   anqp->venue_name);
5329 		pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
5330 				   anqp->network_auth_type);
5331 		pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
5332 				   anqp->roaming_consortium);
5333 		pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
5334 				   anqp->ip_addr_type_availability);
5335 		pos = anqp_add_hex(pos, end, "anqp_nai_realm",
5336 				   anqp->nai_realm);
5337 		pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
5338 		pos = anqp_add_hex(pos, end, "anqp_domain_name",
5339 				   anqp->domain_name);
5340 		pos = anqp_add_hex(pos, end, "anqp_fils_realm_info",
5341 				   anqp->fils_realm_info);
5342 #ifdef CONFIG_HS20
5343 		pos = anqp_add_hex(pos, end, "hs20_capability_list",
5344 				   anqp->hs20_capability_list);
5345 		pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
5346 				   anqp->hs20_operator_friendly_name);
5347 		pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
5348 				   anqp->hs20_wan_metrics);
5349 		pos = anqp_add_hex(pos, end, "hs20_connection_capability",
5350 				   anqp->hs20_connection_capability);
5351 		pos = anqp_add_hex(pos, end, "hs20_operating_class",
5352 				   anqp->hs20_operating_class);
5353 		pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
5354 				   anqp->hs20_osu_providers_list);
5355 		pos = anqp_add_hex(pos, end, "hs20_operator_icon_metadata",
5356 				   anqp->hs20_operator_icon_metadata);
5357 		pos = anqp_add_hex(pos, end, "hs20_osu_providers_nai_list",
5358 				   anqp->hs20_osu_providers_nai_list);
5359 #endif /* CONFIG_HS20 */
5360 
5361 		dl_list_for_each(elem, &anqp->anqp_elems,
5362 				 struct wpa_bss_anqp_elem, list) {
5363 			char title[20];
5364 
5365 			os_snprintf(title, sizeof(title), "anqp[%u]",
5366 				    elem->infoid);
5367 			pos = anqp_add_hex(pos, end, title, elem->payload);
5368 			if (elem->protected_response) {
5369 				ret = os_snprintf(pos, end - pos,
5370 						  "protected-anqp-info[%u]=1\n",
5371 						  elem->infoid);
5372 				if (os_snprintf_error(end - pos, ret))
5373 					return 0;
5374 				pos += ret;
5375 			}
5376 		}
5377 	}
5378 #endif /* CONFIG_INTERWORKING */
5379 
5380 #ifdef CONFIG_MESH
5381 	if (mask & WPA_BSS_MASK_MESH_SCAN) {
5382 		ie = wpa_bss_ie_ptr(bss);
5383 		ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
5384 		if (ret >= end - pos)
5385 			return 0;
5386 		if (ret > 0)
5387 			pos += ret;
5388 	}
5389 #endif /* CONFIG_MESH */
5390 
5391 	if (mask & WPA_BSS_MASK_SNR) {
5392 		ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
5393 		if (os_snprintf_error(end - pos, ret))
5394 			return 0;
5395 		pos += ret;
5396 	}
5397 
5398 	if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
5399 		ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
5400 				  bss->est_throughput);
5401 		if (os_snprintf_error(end - pos, ret))
5402 			return 0;
5403 		pos += ret;
5404 	}
5405 
5406 #ifdef CONFIG_FST
5407 	if (mask & WPA_BSS_MASK_FST) {
5408 		ret = fst_ctrl_iface_mb_info(bss->bssid, pos, end - pos);
5409 		if (ret < 0 || ret >= end - pos)
5410 			return 0;
5411 		pos += ret;
5412 	}
5413 #endif /* CONFIG_FST */
5414 
5415 	if (mask & WPA_BSS_MASK_UPDATE_IDX) {
5416 		ret = os_snprintf(pos, end - pos, "update_idx=%u\n",
5417 				  bss->last_update_idx);
5418 		if (os_snprintf_error(end - pos, ret))
5419 			return 0;
5420 		pos += ret;
5421 	}
5422 
5423 	if ((mask & WPA_BSS_MASK_BEACON_IE) && bss->beacon_ie_len) {
5424 		ret = os_snprintf(pos, end - pos, "beacon_ie=");
5425 		if (os_snprintf_error(end - pos, ret))
5426 			return 0;
5427 		pos += ret;
5428 
5429 		ie = wpa_bss_ie_ptr(bss);
5430 		ie += bss->ie_len;
5431 		for (i = 0; i < bss->beacon_ie_len; i++) {
5432 			ret = os_snprintf(pos, end - pos, "%02x", *ie++);
5433 			if (os_snprintf_error(end - pos, ret))
5434 				return 0;
5435 			pos += ret;
5436 		}
5437 
5438 		ret = os_snprintf(pos, end - pos, "\n");
5439 		if (os_snprintf_error(end - pos, ret))
5440 			return 0;
5441 		pos += ret;
5442 	}
5443 
5444 #ifdef CONFIG_FILS
5445 	if (mask & WPA_BSS_MASK_FILS_INDICATION) {
5446 		ret = print_fils_indication(bss, pos, end);
5447 		if (ret < 0)
5448 			return 0;
5449 		pos += ret;
5450 	}
5451 #endif /* CONFIG_FILS */
5452 
5453 	if (mask & WPA_BSS_MASK_DELIM) {
5454 		ret = os_snprintf(pos, end - pos, "====\n");
5455 		if (os_snprintf_error(end - pos, ret))
5456 			return 0;
5457 		pos += ret;
5458 	}
5459 
5460 	return pos - buf;
5461 }
5462 
5463 
wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)5464 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
5465 					 const char *cmd, char *buf,
5466 					 size_t buflen)
5467 {
5468 	u8 bssid[ETH_ALEN];
5469 	size_t i;
5470 	struct wpa_bss *bss;
5471 	struct wpa_bss *bsslast = NULL;
5472 	struct dl_list *next;
5473 	int ret = 0;
5474 	int len;
5475 	char *ctmp, *end = buf + buflen;
5476 	unsigned long mask = WPA_BSS_MASK_ALL;
5477 
5478 	if (os_strncmp(cmd, "RANGE=", 6) == 0) {
5479 		if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
5480 			bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
5481 					    list_id);
5482 			bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
5483 					       list_id);
5484 		} else { /* N1-N2 */
5485 			unsigned int id1, id2;
5486 
5487 			if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
5488 				wpa_printf(MSG_INFO, "Wrong BSS range "
5489 					   "format");
5490 				return 0;
5491 			}
5492 
5493 			if (*(cmd + 6) == '-')
5494 				id1 = 0;
5495 			else
5496 				id1 = atoi(cmd + 6);
5497 			ctmp++;
5498 			if (*ctmp >= '0' && *ctmp <= '9')
5499 				id2 = atoi(ctmp);
5500 			else
5501 				id2 = (unsigned int) -1;
5502 			bss = wpa_bss_get_id_range(wpa_s, id1, id2);
5503 			if (id2 == (unsigned int) -1)
5504 				bsslast = dl_list_last(&wpa_s->bss_id,
5505 						       struct wpa_bss,
5506 						       list_id);
5507 			else {
5508 				bsslast = wpa_bss_get_id(wpa_s, id2);
5509 				if (bsslast == NULL && bss && id2 > id1) {
5510 					struct wpa_bss *tmp = bss;
5511 					for (;;) {
5512 						next = tmp->list_id.next;
5513 						if (next == &wpa_s->bss_id)
5514 							break;
5515 						tmp = dl_list_entry(
5516 							next, struct wpa_bss,
5517 							list_id);
5518 						if (tmp->id > id2)
5519 							break;
5520 						bsslast = tmp;
5521 					}
5522 				}
5523 			}
5524 		}
5525 	} else if (os_strncmp(cmd, "FIRST", 5) == 0)
5526 		bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
5527 	else if (os_strncmp(cmd, "LAST", 4) == 0)
5528 		bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
5529 	else if (os_strncmp(cmd, "ID-", 3) == 0) {
5530 		i = atoi(cmd + 3);
5531 		bss = wpa_bss_get_id(wpa_s, i);
5532 	} else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
5533 		i = atoi(cmd + 5);
5534 		bss = wpa_bss_get_id(wpa_s, i);
5535 		if (bss) {
5536 			next = bss->list_id.next;
5537 			if (next == &wpa_s->bss_id)
5538 				bss = NULL;
5539 			else
5540 				bss = dl_list_entry(next, struct wpa_bss,
5541 						    list_id);
5542 		}
5543 	} else if (os_strncmp(cmd, "CURRENT", 7) == 0) {
5544 		bss = wpa_s->current_bss;
5545 #ifdef CONFIG_P2P
5546 	} else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
5547 		if (hwaddr_aton(cmd + 13, bssid) == 0)
5548 			bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
5549 		else
5550 			bss = NULL;
5551 #endif /* CONFIG_P2P */
5552 	} else if (hwaddr_aton(cmd, bssid) == 0)
5553 		bss = wpa_bss_get_bssid(wpa_s, bssid);
5554 	else {
5555 		struct wpa_bss *tmp;
5556 		i = atoi(cmd);
5557 		bss = NULL;
5558 		dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
5559 		{
5560 			if (i == 0) {
5561 				bss = tmp;
5562 				break;
5563 			}
5564 			i--;
5565 		}
5566 	}
5567 
5568 	if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
5569 		mask = strtoul(ctmp + 5, NULL, 0x10);
5570 		if (mask == 0)
5571 			mask = WPA_BSS_MASK_ALL;
5572 	}
5573 
5574 	if (bss == NULL)
5575 		return 0;
5576 
5577 	if (bsslast == NULL)
5578 		bsslast = bss;
5579 	do {
5580 		len = print_bss_info(wpa_s, bss, mask, buf, buflen);
5581 		ret += len;
5582 		buf += len;
5583 		buflen -= len;
5584 		if (bss == bsslast) {
5585 			if ((mask & WPA_BSS_MASK_DELIM) && len &&
5586 			    (bss == dl_list_last(&wpa_s->bss_id,
5587 						 struct wpa_bss, list_id))) {
5588 				int res;
5589 
5590 				res = os_snprintf(buf - 5, end - buf + 5,
5591 						  "####\n");
5592 				if (os_snprintf_error(end - buf + 5, res)) {
5593 					wpa_printf(MSG_DEBUG,
5594 						   "Could not add end delim");
5595 				}
5596 			}
5597 			break;
5598 		}
5599 		next = bss->list_id.next;
5600 		if (next == &wpa_s->bss_id)
5601 			break;
5602 		bss = dl_list_entry(next, struct wpa_bss, list_id);
5603 	} while (bss && len);
5604 
5605 	return ret;
5606 }
5607 
5608 
wpa_supplicant_ctrl_iface_ap_scan(struct wpa_supplicant * wpa_s,char * cmd)5609 static int wpa_supplicant_ctrl_iface_ap_scan(
5610 	struct wpa_supplicant *wpa_s, char *cmd)
5611 {
5612 	int ap_scan = atoi(cmd);
5613 	return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
5614 }
5615 
5616 
wpa_supplicant_ctrl_iface_scan_interval(struct wpa_supplicant * wpa_s,char * cmd)5617 static int wpa_supplicant_ctrl_iface_scan_interval(
5618 	struct wpa_supplicant *wpa_s, char *cmd)
5619 {
5620 	int scan_int = atoi(cmd);
5621 	return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
5622 }
5623 
5624 
wpa_supplicant_ctrl_iface_bss_expire_age(struct wpa_supplicant * wpa_s,char * cmd)5625 static int wpa_supplicant_ctrl_iface_bss_expire_age(
5626 	struct wpa_supplicant *wpa_s, char *cmd)
5627 {
5628 	int expire_age = atoi(cmd);
5629 	return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
5630 }
5631 
5632 
wpa_supplicant_ctrl_iface_bss_expire_count(struct wpa_supplicant * wpa_s,char * cmd)5633 static int wpa_supplicant_ctrl_iface_bss_expire_count(
5634 	struct wpa_supplicant *wpa_s, char *cmd)
5635 {
5636 	int expire_count = atoi(cmd);
5637 	return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
5638 }
5639 
5640 
wpa_supplicant_ctrl_iface_bss_flush(struct wpa_supplicant * wpa_s,char * cmd)5641 static void wpa_supplicant_ctrl_iface_bss_flush(
5642 	struct wpa_supplicant *wpa_s, char *cmd)
5643 {
5644 	int flush_age = atoi(cmd);
5645 
5646 	if (flush_age == 0)
5647 		wpa_bss_flush(wpa_s);
5648 	else
5649 		wpa_bss_flush_by_age(wpa_s, flush_age);
5650 }
5651 
5652 
5653 #ifdef CONFIG_TESTING_OPTIONS
wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant * wpa_s)5654 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
5655 {
5656 	wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
5657 	/* MLME-DELETEKEYS.request */
5658 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL,
5659 			0, KEY_FLAG_GROUP);
5660 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL,
5661 			0, KEY_FLAG_GROUP);
5662 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL,
5663 			0, KEY_FLAG_GROUP);
5664 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL,
5665 			0, KEY_FLAG_GROUP);
5666 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL,
5667 			0, KEY_FLAG_GROUP);
5668 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL,
5669 			0, KEY_FLAG_GROUP);
5670 
5671 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
5672 			0, KEY_FLAG_PAIRWISE);
5673 	if (wpa_sm_ext_key_id(wpa_s->wpa))
5674 		wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 1, 0,
5675 				NULL, 0, NULL, 0, KEY_FLAG_PAIRWISE);
5676 	/* MLME-SETPROTECTION.request(None) */
5677 	wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
5678 				   MLME_SETPROTECTION_PROTECT_TYPE_NONE,
5679 				   MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
5680 	wpa_sm_drop_sa(wpa_s->wpa);
5681 }
5682 #endif /* CONFIG_TESTING_OPTIONS */
5683 
5684 
wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant * wpa_s,char * addr)5685 static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
5686 					  char *addr)
5687 {
5688 #ifdef CONFIG_NO_SCAN_PROCESSING
5689 	return -1;
5690 #else /* CONFIG_NO_SCAN_PROCESSING */
5691 	u8 bssid[ETH_ALEN];
5692 	struct wpa_bss *bss;
5693 	struct wpa_ssid *ssid = wpa_s->current_ssid;
5694 	struct wpa_radio_work *already_connecting;
5695 
5696 	if (hwaddr_aton(addr, bssid)) {
5697 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
5698 			   "address '%s'", addr);
5699 		return -1;
5700 	}
5701 
5702 	wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
5703 
5704 	if (!ssid) {
5705 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
5706 			   "configuration known for the target AP");
5707 		return -1;
5708 	}
5709 
5710 	bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
5711 	if (!bss) {
5712 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
5713 			   "from BSS table");
5714 		return -1;
5715 	}
5716 
5717 	/*
5718 	 * TODO: Find best network configuration block from configuration to
5719 	 * allow roaming to other networks
5720 	 */
5721 
5722 	already_connecting = radio_work_pending(wpa_s, "sme-connect");
5723 	wpa_s->reassociate = 1;
5724 	wpa_supplicant_connect(wpa_s, bss, ssid);
5725 
5726 	/*
5727 	 * Indicate that an explicitly requested roam is in progress so scan
5728 	 * results that come in before the 'sme-connect' radio work gets
5729 	 * executed do not override the original connection attempt.
5730 	 */
5731 	if (!already_connecting && radio_work_pending(wpa_s, "sme-connect"))
5732 		wpa_s->roam_in_progress = true;
5733 
5734 	return 0;
5735 #endif /* CONFIG_NO_SCAN_PROCESSING */
5736 }
5737 
5738 
5739 #ifdef CONFIG_P2P
p2p_ctrl_find(struct wpa_supplicant * wpa_s,char * cmd)5740 static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
5741 {
5742 	unsigned int timeout = atoi(cmd);
5743 	enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
5744 	u8 dev_id[ETH_ALEN], *_dev_id = NULL;
5745 	u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
5746 	char *pos;
5747 	unsigned int search_delay;
5748 	const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
5749 	u8 seek_count = 0;
5750 	int freq = 0;
5751 	bool include_6ghz = false;
5752 
5753 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5754 		wpa_dbg(wpa_s, MSG_INFO,
5755 			"Reject P2P_FIND since interface is disabled");
5756 		return -1;
5757 	}
5758 
5759 	if (os_strstr(cmd, " include_6ghz"))
5760 		include_6ghz = true;
5761 	if (os_strstr(cmd, "type=social"))
5762 		type = P2P_FIND_ONLY_SOCIAL;
5763 	else if (os_strstr(cmd, "type=progressive"))
5764 		type = P2P_FIND_PROGRESSIVE;
5765 
5766 	pos = os_strstr(cmd, "dev_id=");
5767 	if (pos) {
5768 		pos += 7;
5769 		if (hwaddr_aton(pos, dev_id))
5770 			return -1;
5771 		_dev_id = dev_id;
5772 	}
5773 
5774 	pos = os_strstr(cmd, "dev_type=");
5775 	if (pos) {
5776 		pos += 9;
5777 		if (wps_dev_type_str2bin(pos, dev_type) < 0)
5778 			return -1;
5779 		_dev_type = dev_type;
5780 	}
5781 
5782 	pos = os_strstr(cmd, "delay=");
5783 	if (pos) {
5784 		pos += 6;
5785 		search_delay = atoi(pos);
5786 	} else
5787 		search_delay = wpas_p2p_search_delay(wpa_s);
5788 
5789 	pos = os_strstr(cmd, "freq=");
5790 	if (pos) {
5791 		pos += 5;
5792 		freq = atoi(pos);
5793 		if (freq <= 0)
5794 			return -1;
5795 	}
5796 
5797 	/* Must be searched for last, because it adds nul termination */
5798 	pos = os_strstr(cmd, " seek=");
5799 	if (pos)
5800 		pos += 6;
5801 	while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
5802 		char *term;
5803 
5804 		_seek[seek_count++] = pos;
5805 		seek = _seek;
5806 		term = os_strchr(pos, ' ');
5807 		if (!term)
5808 			break;
5809 		*term = '\0';
5810 		pos = os_strstr(term + 1, "seek=");
5811 		if (pos)
5812 			pos += 5;
5813 	}
5814 	if (seek_count > P2P_MAX_QUERY_HASH) {
5815 		seek[0] = NULL;
5816 		seek_count = 1;
5817 	}
5818 
5819 	return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
5820 			     _dev_id, search_delay, seek_count, seek, freq,
5821 			     include_6ghz);
5822 }
5823 
5824 
p2ps_ctrl_parse_cpt_priority(const char * pos,u8 * cpt)5825 static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
5826 {
5827 	const char *last = NULL;
5828 	const char *token;
5829 	long int token_len;
5830 	unsigned int i;
5831 
5832 	/* Expected predefined CPT names delimited by ':' */
5833 	for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
5834 		if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
5835 			wpa_printf(MSG_ERROR,
5836 				   "P2PS: CPT name list is too long, expected up to %d names",
5837 				   P2PS_FEATURE_CAPAB_CPT_MAX);
5838 			cpt[0] = 0;
5839 			return -1;
5840 		}
5841 
5842 		token_len = last - token;
5843 
5844 		if (token_len  == 3 &&
5845 		    os_memcmp(token, "UDP", token_len) == 0) {
5846 			cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
5847 		} else if (token_len == 3 &&
5848 			   os_memcmp(token, "MAC", token_len) == 0) {
5849 			cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
5850 		} else {
5851 			wpa_printf(MSG_ERROR,
5852 				   "P2PS: Unsupported CPT name '%s'", token);
5853 			cpt[0] = 0;
5854 			return -1;
5855 		}
5856 
5857 		if (isblank((unsigned char) *last)) {
5858 			i++;
5859 			break;
5860 		}
5861 	}
5862 	cpt[i] = 0;
5863 	return 0;
5864 }
5865 
5866 
p2p_parse_asp_provision_cmd(const char * cmd)5867 static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
5868 {
5869 	struct p2ps_provision *p2ps_prov;
5870 	char *pos;
5871 	size_t info_len = 0;
5872 	char *info = NULL;
5873 	u8 role = P2PS_SETUP_NONE;
5874 	long long unsigned val;
5875 	int i;
5876 
5877 	pos = os_strstr(cmd, "info=");
5878 	if (pos) {
5879 		pos += 5;
5880 		info_len = os_strlen(pos);
5881 
5882 		if (info_len) {
5883 			info = os_malloc(info_len + 1);
5884 			if (info) {
5885 				info_len = utf8_unescape(pos, info_len,
5886 							 info, info_len + 1);
5887 			} else
5888 				info_len = 0;
5889 		}
5890 	}
5891 
5892 	p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
5893 	if (p2ps_prov == NULL) {
5894 		os_free(info);
5895 		return NULL;
5896 	}
5897 
5898 	if (info) {
5899 		os_memcpy(p2ps_prov->info, info, info_len);
5900 		p2ps_prov->info[info_len] = '\0';
5901 		os_free(info);
5902 	}
5903 
5904 	pos = os_strstr(cmd, "status=");
5905 	if (pos)
5906 		p2ps_prov->status = atoi(pos + 7);
5907 	else
5908 		p2ps_prov->status = -1;
5909 
5910 	pos = os_strstr(cmd, "adv_id=");
5911 	if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
5912 		goto invalid_args;
5913 	p2ps_prov->adv_id = val;
5914 
5915 	pos = os_strstr(cmd, "method=");
5916 	if (pos)
5917 		p2ps_prov->method = strtol(pos + 7, NULL, 16);
5918 	else
5919 		p2ps_prov->method = 0;
5920 
5921 	pos = os_strstr(cmd, "session=");
5922 	if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
5923 		goto invalid_args;
5924 	p2ps_prov->session_id = val;
5925 
5926 	pos = os_strstr(cmd, "adv_mac=");
5927 	if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
5928 		goto invalid_args;
5929 
5930 	pos = os_strstr(cmd, "session_mac=");
5931 	if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
5932 		goto invalid_args;
5933 
5934 	pos = os_strstr(cmd, "cpt=");
5935 	if (pos) {
5936 		if (p2ps_ctrl_parse_cpt_priority(pos + 4,
5937 						 p2ps_prov->cpt_priority))
5938 			goto invalid_args;
5939 	} else {
5940 		p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
5941 	}
5942 
5943 	for (i = 0; p2ps_prov->cpt_priority[i]; i++)
5944 		p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
5945 
5946 	/* force conncap with tstCap (no validity checks) */
5947 	pos = os_strstr(cmd, "tstCap=");
5948 	if (pos) {
5949 		role = strtol(pos + 7, NULL, 16);
5950 	} else {
5951 		pos = os_strstr(cmd, "role=");
5952 		if (pos) {
5953 			role = strtol(pos + 5, NULL, 16);
5954 			if (role != P2PS_SETUP_CLIENT &&
5955 			    role != P2PS_SETUP_GROUP_OWNER)
5956 				role = P2PS_SETUP_NONE;
5957 		}
5958 	}
5959 	p2ps_prov->role = role;
5960 
5961 	return p2ps_prov;
5962 
5963 invalid_args:
5964 	os_free(p2ps_prov);
5965 	return NULL;
5966 }
5967 
5968 
p2p_ctrl_asp_provision_resp(struct wpa_supplicant * wpa_s,char * cmd)5969 static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
5970 {
5971 	u8 addr[ETH_ALEN];
5972 	struct p2ps_provision *p2ps_prov;
5973 	char *pos;
5974 
5975 	/* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
5976 
5977 	wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
5978 
5979 	if (hwaddr_aton(cmd, addr))
5980 		return -1;
5981 
5982 	pos = cmd + 17;
5983 	if (*pos != ' ')
5984 		return -1;
5985 
5986 	p2ps_prov = p2p_parse_asp_provision_cmd(pos);
5987 	if (!p2ps_prov)
5988 		return -1;
5989 
5990 	if (p2ps_prov->status < 0) {
5991 		os_free(p2ps_prov);
5992 		return -1;
5993 	}
5994 
5995 	return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
5996 				  p2ps_prov);
5997 }
5998 
5999 
p2p_ctrl_asp_provision(struct wpa_supplicant * wpa_s,char * cmd)6000 static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
6001 {
6002 	u8 addr[ETH_ALEN];
6003 	struct p2ps_provision *p2ps_prov;
6004 	char *pos;
6005 
6006 	/* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
6007 	 *        session=<ses_id> mac=<ses_mac> [info=<infodata>]
6008 	 */
6009 
6010 	wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
6011 	if (hwaddr_aton(cmd, addr))
6012 		return -1;
6013 
6014 	pos = cmd + 17;
6015 	if (*pos != ' ')
6016 		return -1;
6017 
6018 	p2ps_prov = p2p_parse_asp_provision_cmd(pos);
6019 	if (!p2ps_prov)
6020 		return -1;
6021 
6022 	p2ps_prov->pd_seeker = 1;
6023 
6024 	return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
6025 				  p2ps_prov);
6026 }
6027 
6028 
parse_freq(int chwidth,int freq2)6029 static int parse_freq(int chwidth, int freq2)
6030 {
6031 	if (freq2 < 0)
6032 		return -1;
6033 	if (freq2)
6034 		return CHANWIDTH_80P80MHZ;
6035 
6036 	switch (chwidth) {
6037 	case 0:
6038 	case 20:
6039 	case 40:
6040 		return CHANWIDTH_USE_HT;
6041 	case 80:
6042 		return CHANWIDTH_80MHZ;
6043 	case 160:
6044 		return CHANWIDTH_160MHZ;
6045 	default:
6046 		wpa_printf(MSG_DEBUG, "Unknown max oper bandwidth: %d",
6047 			   chwidth);
6048 		return -1;
6049 	}
6050 }
6051 
6052 
p2p_ctrl_connect(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)6053 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
6054 			    char *buf, size_t buflen)
6055 {
6056 	u8 addr[ETH_ALEN];
6057 	char *pos, *pos2;
6058 	char *pin = NULL;
6059 	enum p2p_wps_method wps_method;
6060 	int new_pin;
6061 	int ret;
6062 	int persistent_group, persistent_id = -1;
6063 	int join;
6064 	int auth;
6065 	int automatic;
6066 	int go_intent = -1;
6067 	int freq = 0;
6068 	int pd;
6069 	int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
6070 	int edmg;
6071 	u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
6072 	size_t group_ssid_len = 0;
6073 	int he;
6074 	bool allow_6ghz;
6075 
6076 	if (!wpa_s->global->p2p_init_wpa_s)
6077 		return -1;
6078 	if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
6079 		wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
6080 			wpa_s->global->p2p_init_wpa_s->ifname);
6081 		wpa_s = wpa_s->global->p2p_init_wpa_s;
6082 	}
6083 
6084 	/* <addr> <"pbc" | "pin" | PIN> [label|display|keypad|p2ps]
6085 	 * [persistent|persistent=<network id>]
6086 	 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
6087 	 * [ht40] [vht] [he] [edmg] [auto] [ssid=<hexdump>] */
6088 
6089 	if (hwaddr_aton(cmd, addr))
6090 		return -1;
6091 
6092 	pos = cmd + 17;
6093 	if (*pos != ' ')
6094 		return -1;
6095 	pos++;
6096 
6097 	persistent_group = os_strstr(pos, " persistent") != NULL;
6098 	pos2 = os_strstr(pos, " persistent=");
6099 	if (pos2) {
6100 		struct wpa_ssid *ssid;
6101 		persistent_id = atoi(pos2 + 12);
6102 		ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6103 		if (ssid == NULL || ssid->disabled != 2 ||
6104 		    ssid->mode != WPAS_MODE_P2P_GO) {
6105 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
6106 				   "SSID id=%d for persistent P2P group (GO)",
6107 				   persistent_id);
6108 			return -1;
6109 		}
6110 	}
6111 	join = os_strstr(pos, " join") != NULL;
6112 	allow_6ghz = os_strstr(pos, " allow_6ghz") != NULL;
6113 	auth = os_strstr(pos, " auth") != NULL;
6114 	automatic = os_strstr(pos, " auto") != NULL;
6115 	pd = os_strstr(pos, " provdisc") != NULL;
6116 	vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
6117 	ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
6118 		vht;
6119 	he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
6120 	edmg = (os_strstr(cmd, " edmg") != NULL) || wpa_s->conf->p2p_go_edmg;
6121 
6122 	pos2 = os_strstr(pos, " go_intent=");
6123 	if (pos2) {
6124 		pos2 += 11;
6125 		go_intent = atoi(pos2);
6126 		if (go_intent < 0 || go_intent > 15)
6127 			return -1;
6128 	}
6129 
6130 	pos2 = os_strstr(pos, " freq=");
6131 	if (pos2) {
6132 		pos2 += 6;
6133 		freq = atoi(pos2);
6134 		if (freq <= 0)
6135 			return -1;
6136 	}
6137 
6138 	pos2 = os_strstr(pos, " freq2=");
6139 	if (pos2)
6140 		freq2 = atoi(pos2 + 7);
6141 
6142 	pos2 = os_strstr(pos, " max_oper_chwidth=");
6143 	if (pos2)
6144 		chwidth = atoi(pos2 + 18);
6145 
6146 	max_oper_chwidth = parse_freq(chwidth, freq2);
6147 	if (max_oper_chwidth < 0)
6148 		return -1;
6149 
6150 	if (allow_6ghz && chwidth == 40)
6151 		max_oper_chwidth = CHANWIDTH_40MHZ_6GHZ;
6152 
6153 	pos2 = os_strstr(pos, " ssid=");
6154 	if (pos2) {
6155 		char *end;
6156 
6157 		pos2 += 6;
6158 		end = os_strchr(pos2, ' ');
6159 		if (!end)
6160 			group_ssid_len = os_strlen(pos2) / 2;
6161 		else
6162 			group_ssid_len = (end - pos2) / 2;
6163 		if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
6164 		    hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
6165 			return -1;
6166 		group_ssid = _group_ssid;
6167 	}
6168 
6169 	if (os_strncmp(pos, "pin", 3) == 0) {
6170 		/* Request random PIN (to be displayed) and enable the PIN */
6171 		wps_method = WPS_PIN_DISPLAY;
6172 	} else if (os_strncmp(pos, "pbc", 3) == 0) {
6173 		wps_method = WPS_PBC;
6174 	} else if (os_strstr(pos, "p2ps") != NULL) {
6175 		wps_method = WPS_P2PS;
6176 	} else {
6177 		pin = pos;
6178 		pos = os_strchr(pin, ' ');
6179 		wps_method = WPS_PIN_KEYPAD;
6180 		if (pos) {
6181 			*pos++ = '\0';
6182 			if (os_strncmp(pos, "display", 7) == 0)
6183 				wps_method = WPS_PIN_DISPLAY;
6184 		}
6185 		if (!wps_pin_str_valid(pin)) {
6186 			os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
6187 			return 17;
6188 		}
6189 	}
6190 
6191 	new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
6192 				   persistent_group, automatic, join,
6193 				   auth, go_intent, freq, freq2, persistent_id,
6194 				   pd, ht40, vht, max_oper_chwidth, he, edmg,
6195 				   group_ssid, group_ssid_len, allow_6ghz);
6196 	if (new_pin == -2) {
6197 		os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
6198 		return 25;
6199 	}
6200 	if (new_pin == -3) {
6201 		os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
6202 		return 25;
6203 	}
6204 	if (new_pin < 0)
6205 		return -1;
6206 	if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
6207 		ret = os_snprintf(buf, buflen, "%08d", new_pin);
6208 		if (os_snprintf_error(buflen, ret))
6209 			return -1;
6210 		return ret;
6211 	}
6212 
6213 	os_memcpy(buf, "OK\n", 3);
6214 	return 3;
6215 }
6216 
6217 
p2p_ctrl_listen(struct wpa_supplicant * wpa_s,char * cmd)6218 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
6219 {
6220 	unsigned int timeout = atoi(cmd);
6221 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6222 		wpa_dbg(wpa_s, MSG_INFO,
6223 			"Reject P2P_LISTEN since interface is disabled");
6224 		return -1;
6225 	}
6226 	return wpas_p2p_listen(wpa_s, timeout);
6227 }
6228 
6229 
p2p_ctrl_prov_disc(struct wpa_supplicant * wpa_s,char * cmd)6230 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
6231 {
6232 	u8 addr[ETH_ALEN];
6233 	char *pos;
6234 	enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
6235 
6236 	/* <addr> <config method> [join|auto] */
6237 
6238 	if (hwaddr_aton(cmd, addr))
6239 		return -1;
6240 
6241 	pos = cmd + 17;
6242 	if (*pos != ' ')
6243 		return -1;
6244 	pos++;
6245 
6246 	if (os_strstr(pos, " join") != NULL)
6247 		use = WPAS_P2P_PD_FOR_JOIN;
6248 	else if (os_strstr(pos, " auto") != NULL)
6249 		use = WPAS_P2P_PD_AUTO;
6250 
6251 	return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
6252 }
6253 
6254 
p2p_get_passphrase(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)6255 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
6256 			      size_t buflen)
6257 {
6258 	struct wpa_ssid *ssid = wpa_s->current_ssid;
6259 
6260 	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
6261 	    ssid->passphrase == NULL)
6262 		return -1;
6263 
6264 	os_strlcpy(buf, ssid->passphrase, buflen);
6265 	return os_strlen(buf);
6266 }
6267 
6268 
p2p_ctrl_serv_disc_req(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)6269 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
6270 				  char *buf, size_t buflen)
6271 {
6272 	u64 ref;
6273 	int res;
6274 	u8 dst_buf[ETH_ALEN], *dst;
6275 	struct wpabuf *tlvs;
6276 	char *pos;
6277 	size_t len;
6278 
6279 	if (hwaddr_aton(cmd, dst_buf))
6280 		return -1;
6281 	dst = dst_buf;
6282 	if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
6283 	    dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
6284 		dst = NULL;
6285 	pos = cmd + 17;
6286 	if (*pos != ' ')
6287 		return -1;
6288 	pos++;
6289 
6290 	if (os_strncmp(pos, "upnp ", 5) == 0) {
6291 		u8 version;
6292 		pos += 5;
6293 		if (hexstr2bin(pos, &version, 1) < 0)
6294 			return -1;
6295 		pos += 2;
6296 		if (*pos != ' ')
6297 			return -1;
6298 		pos++;
6299 		ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
6300 #ifdef CONFIG_WIFI_DISPLAY
6301 	} else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
6302 		ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
6303 #endif /* CONFIG_WIFI_DISPLAY */
6304 	} else if (os_strncmp(pos, "asp ", 4) == 0) {
6305 		char *svc_str;
6306 		char *svc_info = NULL;
6307 		u32 id;
6308 
6309 		pos += 4;
6310 		if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
6311 			return -1;
6312 
6313 		pos = os_strchr(pos, ' ');
6314 		if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
6315 			return -1;
6316 
6317 		svc_str = pos + 1;
6318 
6319 		pos = os_strchr(svc_str, ' ');
6320 
6321 		if (pos)
6322 			*pos++ = '\0';
6323 
6324 		/* All remaining data is the svc_info string */
6325 		if (pos && pos[0] && pos[0] != ' ') {
6326 			len = os_strlen(pos);
6327 
6328 			/* Unescape in place */
6329 			len = utf8_unescape(pos, len, pos, len);
6330 			if (len > 0xff)
6331 				return -1;
6332 
6333 			svc_info = pos;
6334 		}
6335 
6336 		ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
6337 					      svc_str, svc_info);
6338 	} else {
6339 		len = os_strlen(pos);
6340 		if (len & 1)
6341 			return -1;
6342 		len /= 2;
6343 		tlvs = wpabuf_alloc(len);
6344 		if (tlvs == NULL)
6345 			return -1;
6346 		if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
6347 			wpabuf_free(tlvs);
6348 			return -1;
6349 		}
6350 
6351 		ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
6352 		wpabuf_free(tlvs);
6353 	}
6354 	if (ref == 0)
6355 		return -1;
6356 	res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
6357 	if (os_snprintf_error(buflen, res))
6358 		return -1;
6359 	return res;
6360 }
6361 
6362 
p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant * wpa_s,char * cmd)6363 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
6364 					 char *cmd)
6365 {
6366 	long long unsigned val;
6367 	u64 req;
6368 	if (sscanf(cmd, "%llx", &val) != 1)
6369 		return -1;
6370 	req = val;
6371 	return wpas_p2p_sd_cancel_request(wpa_s, req);
6372 }
6373 
6374 
p2p_ctrl_serv_disc_resp(struct wpa_supplicant * wpa_s,char * cmd)6375 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
6376 {
6377 	int freq;
6378 	u8 dst[ETH_ALEN];
6379 	u8 dialog_token;
6380 	struct wpabuf *resp_tlvs;
6381 	char *pos, *pos2;
6382 	size_t len;
6383 
6384 	pos = os_strchr(cmd, ' ');
6385 	if (pos == NULL)
6386 		return -1;
6387 	*pos++ = '\0';
6388 	freq = atoi(cmd);
6389 	if (freq == 0)
6390 		return -1;
6391 
6392 	if (hwaddr_aton(pos, dst))
6393 		return -1;
6394 	pos += 17;
6395 	if (*pos != ' ')
6396 		return -1;
6397 	pos++;
6398 
6399 	pos2 = os_strchr(pos, ' ');
6400 	if (pos2 == NULL)
6401 		return -1;
6402 	*pos2++ = '\0';
6403 	dialog_token = atoi(pos);
6404 
6405 	len = os_strlen(pos2);
6406 	if (len & 1)
6407 		return -1;
6408 	len /= 2;
6409 	resp_tlvs = wpabuf_alloc(len);
6410 	if (resp_tlvs == NULL)
6411 		return -1;
6412 	if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
6413 		wpabuf_free(resp_tlvs);
6414 		return -1;
6415 	}
6416 
6417 	wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
6418 	wpabuf_free(resp_tlvs);
6419 	return 0;
6420 }
6421 
6422 
p2p_ctrl_serv_disc_external(struct wpa_supplicant * wpa_s,char * cmd)6423 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
6424 				       char *cmd)
6425 {
6426 	if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
6427 		return -1;
6428 	wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
6429 	return 0;
6430 }
6431 
6432 
p2p_ctrl_service_add_bonjour(struct wpa_supplicant * wpa_s,char * cmd)6433 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
6434 					char *cmd)
6435 {
6436 	char *pos;
6437 	size_t len;
6438 	struct wpabuf *query, *resp;
6439 
6440 	pos = os_strchr(cmd, ' ');
6441 	if (pos == NULL)
6442 		return -1;
6443 	*pos++ = '\0';
6444 
6445 	len = os_strlen(cmd);
6446 	if (len & 1)
6447 		return -1;
6448 	len /= 2;
6449 	query = wpabuf_alloc(len);
6450 	if (query == NULL)
6451 		return -1;
6452 	if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
6453 		wpabuf_free(query);
6454 		return -1;
6455 	}
6456 
6457 	len = os_strlen(pos);
6458 	if (len & 1) {
6459 		wpabuf_free(query);
6460 		return -1;
6461 	}
6462 	len /= 2;
6463 	resp = wpabuf_alloc(len);
6464 	if (resp == NULL) {
6465 		wpabuf_free(query);
6466 		return -1;
6467 	}
6468 	if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
6469 		wpabuf_free(query);
6470 		wpabuf_free(resp);
6471 		return -1;
6472 	}
6473 
6474 	if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
6475 		wpabuf_free(query);
6476 		wpabuf_free(resp);
6477 		return -1;
6478 	}
6479 	return 0;
6480 }
6481 
6482 
p2p_ctrl_service_add_upnp(struct wpa_supplicant * wpa_s,char * cmd)6483 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
6484 {
6485 	char *pos;
6486 	u8 version;
6487 
6488 	pos = os_strchr(cmd, ' ');
6489 	if (pos == NULL)
6490 		return -1;
6491 	*pos++ = '\0';
6492 
6493 	if (hexstr2bin(cmd, &version, 1) < 0)
6494 		return -1;
6495 
6496 	return wpas_p2p_service_add_upnp(wpa_s, version, pos);
6497 }
6498 
6499 
p2p_ctrl_service_add_asp(struct wpa_supplicant * wpa_s,u8 replace,char * cmd)6500 static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
6501 				    u8 replace, char *cmd)
6502 {
6503 	char *pos;
6504 	char *adv_str;
6505 	u32 auto_accept, adv_id, svc_state, config_methods;
6506 	char *svc_info = NULL;
6507 	char *cpt_prio_str;
6508 	u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
6509 
6510 	pos = os_strchr(cmd, ' ');
6511 	if (pos == NULL)
6512 		return -1;
6513 	*pos++ = '\0';
6514 
6515 	/* Auto-Accept value is mandatory, and must be one of the
6516 	 * single values (0, 1, 2, 4) */
6517 	auto_accept = atoi(cmd);
6518 	switch (auto_accept) {
6519 	case P2PS_SETUP_NONE: /* No auto-accept */
6520 	case P2PS_SETUP_NEW:
6521 	case P2PS_SETUP_CLIENT:
6522 	case P2PS_SETUP_GROUP_OWNER:
6523 		break;
6524 	default:
6525 		return -1;
6526 	}
6527 
6528 	/* Advertisement ID is mandatory */
6529 	cmd = pos;
6530 	pos = os_strchr(cmd, ' ');
6531 	if (pos == NULL)
6532 		return -1;
6533 	*pos++ = '\0';
6534 
6535 	/* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
6536 	if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
6537 		return -1;
6538 
6539 	/* Only allow replacements if exist, and adds if not */
6540 	if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
6541 		if (!replace)
6542 			return -1;
6543 	} else {
6544 		if (replace)
6545 			return -1;
6546 	}
6547 
6548 	/* svc_state between 0 - 0xff is mandatory */
6549 	if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
6550 		return -1;
6551 
6552 	pos = os_strchr(pos, ' ');
6553 	if (pos == NULL)
6554 		return -1;
6555 
6556 	/* config_methods is mandatory */
6557 	pos++;
6558 	if (sscanf(pos, "%x", &config_methods) != 1)
6559 		return -1;
6560 
6561 	if (!(config_methods &
6562 	      (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
6563 		return -1;
6564 
6565 	pos = os_strchr(pos, ' ');
6566 	if (pos == NULL)
6567 		return -1;
6568 
6569 	pos++;
6570 	adv_str = pos;
6571 
6572 	/* Advertisement string is mandatory */
6573 	if (!pos[0] || pos[0] == ' ')
6574 		return -1;
6575 
6576 	/* Terminate svc string */
6577 	pos = os_strchr(pos, ' ');
6578 	if (pos != NULL)
6579 		*pos++ = '\0';
6580 
6581 	cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
6582 	if (cpt_prio_str) {
6583 		pos = os_strchr(pos, ' ');
6584 		if (pos != NULL)
6585 			*pos++ = '\0';
6586 
6587 		if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
6588 			return -1;
6589 	} else {
6590 		cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
6591 		cpt_prio[1] = 0;
6592 	}
6593 
6594 	/* Service and Response Information are optional */
6595 	if (pos && pos[0]) {
6596 		size_t len;
6597 
6598 		/* Note the bare ' included, which cannot exist legally
6599 		 * in unescaped string. */
6600 		svc_info = os_strstr(pos, "svc_info='");
6601 
6602 		if (svc_info) {
6603 			svc_info += 9;
6604 			len = os_strlen(svc_info);
6605 			utf8_unescape(svc_info, len, svc_info, len);
6606 		}
6607 	}
6608 
6609 	return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
6610 					(u8) svc_state, (u16) config_methods,
6611 					svc_info, cpt_prio);
6612 }
6613 
6614 
p2p_ctrl_service_add(struct wpa_supplicant * wpa_s,char * cmd)6615 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
6616 {
6617 	char *pos;
6618 
6619 	pos = os_strchr(cmd, ' ');
6620 	if (pos == NULL)
6621 		return -1;
6622 	*pos++ = '\0';
6623 
6624 	if (os_strcmp(cmd, "bonjour") == 0)
6625 		return p2p_ctrl_service_add_bonjour(wpa_s, pos);
6626 	if (os_strcmp(cmd, "upnp") == 0)
6627 		return p2p_ctrl_service_add_upnp(wpa_s, pos);
6628 	if (os_strcmp(cmd, "asp") == 0)
6629 		return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
6630 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6631 	return -1;
6632 }
6633 
6634 
p2p_ctrl_service_del_bonjour(struct wpa_supplicant * wpa_s,char * cmd)6635 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
6636 					char *cmd)
6637 {
6638 	size_t len;
6639 	struct wpabuf *query;
6640 	int ret;
6641 
6642 	len = os_strlen(cmd);
6643 	if (len & 1)
6644 		return -1;
6645 	len /= 2;
6646 	query = wpabuf_alloc(len);
6647 	if (query == NULL)
6648 		return -1;
6649 	if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
6650 		wpabuf_free(query);
6651 		return -1;
6652 	}
6653 
6654 	ret = wpas_p2p_service_del_bonjour(wpa_s, query);
6655 	wpabuf_free(query);
6656 	return ret;
6657 }
6658 
6659 
p2p_ctrl_service_del_upnp(struct wpa_supplicant * wpa_s,char * cmd)6660 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
6661 {
6662 	char *pos;
6663 	u8 version;
6664 
6665 	pos = os_strchr(cmd, ' ');
6666 	if (pos == NULL)
6667 		return -1;
6668 	*pos++ = '\0';
6669 
6670 	if (hexstr2bin(cmd, &version, 1) < 0)
6671 		return -1;
6672 
6673 	return wpas_p2p_service_del_upnp(wpa_s, version, pos);
6674 }
6675 
6676 
p2p_ctrl_service_del_asp(struct wpa_supplicant * wpa_s,char * cmd)6677 static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
6678 {
6679 	u32 adv_id;
6680 
6681 	if (os_strcmp(cmd, "all") == 0) {
6682 		wpas_p2p_service_flush_asp(wpa_s);
6683 		return 0;
6684 	}
6685 
6686 	if (sscanf(cmd, "%x", &adv_id) != 1)
6687 		return -1;
6688 
6689 	return wpas_p2p_service_del_asp(wpa_s, adv_id);
6690 }
6691 
6692 
p2p_ctrl_service_del(struct wpa_supplicant * wpa_s,char * cmd)6693 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
6694 {
6695 	char *pos;
6696 
6697 	pos = os_strchr(cmd, ' ');
6698 	if (pos == NULL)
6699 		return -1;
6700 	*pos++ = '\0';
6701 
6702 	if (os_strcmp(cmd, "bonjour") == 0)
6703 		return p2p_ctrl_service_del_bonjour(wpa_s, pos);
6704 	if (os_strcmp(cmd, "upnp") == 0)
6705 		return p2p_ctrl_service_del_upnp(wpa_s, pos);
6706 	if (os_strcmp(cmd, "asp") == 0)
6707 		return p2p_ctrl_service_del_asp(wpa_s, pos);
6708 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6709 	return -1;
6710 }
6711 
6712 
p2p_ctrl_service_replace(struct wpa_supplicant * wpa_s,char * cmd)6713 static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
6714 {
6715 	char *pos;
6716 
6717 	pos = os_strchr(cmd, ' ');
6718 	if (pos == NULL)
6719 		return -1;
6720 	*pos++ = '\0';
6721 
6722 	if (os_strcmp(cmd, "asp") == 0)
6723 		return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
6724 
6725 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6726 	return -1;
6727 }
6728 
6729 
p2p_ctrl_reject(struct wpa_supplicant * wpa_s,char * cmd)6730 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
6731 {
6732 	u8 addr[ETH_ALEN];
6733 
6734 	/* <addr> */
6735 
6736 	if (hwaddr_aton(cmd, addr))
6737 		return -1;
6738 
6739 	return wpas_p2p_reject(wpa_s, addr);
6740 }
6741 
6742 
p2p_ctrl_invite_persistent(struct wpa_supplicant * wpa_s,char * cmd)6743 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
6744 {
6745 	char *pos;
6746 	int id;
6747 	struct wpa_ssid *ssid;
6748 	u8 *_peer = NULL, peer[ETH_ALEN];
6749 	int freq = 0, pref_freq = 0;
6750 	int ht40, vht, he, max_oper_chwidth, chwidth = 0, freq2 = 0;
6751 	int edmg;
6752 	bool allow_6ghz;
6753 
6754 	id = atoi(cmd);
6755 	pos = os_strstr(cmd, " peer=");
6756 	if (pos) {
6757 		pos += 6;
6758 		if (hwaddr_aton(pos, peer))
6759 			return -1;
6760 		_peer = peer;
6761 	}
6762 	ssid = wpa_config_get_network(wpa_s->conf, id);
6763 	if (ssid == NULL || ssid->disabled != 2) {
6764 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
6765 			   "for persistent P2P group",
6766 			   id);
6767 		return -1;
6768 	}
6769 
6770 	pos = os_strstr(cmd, " freq=");
6771 	if (pos) {
6772 		pos += 6;
6773 		freq = atoi(pos);
6774 		if (freq <= 0)
6775 			return -1;
6776 	}
6777 
6778 	pos = os_strstr(cmd, " pref=");
6779 	if (pos) {
6780 		pos += 6;
6781 		pref_freq = atoi(pos);
6782 		if (pref_freq <= 0)
6783 			return -1;
6784 	}
6785 
6786 	vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
6787 	ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
6788 		vht;
6789 	he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
6790 	edmg = (os_strstr(cmd, " edmg") != NULL) || wpa_s->conf->p2p_go_edmg;
6791 
6792 	pos = os_strstr(cmd, "freq2=");
6793 	if (pos)
6794 		freq2 = atoi(pos + 6);
6795 
6796 	pos = os_strstr(cmd, " max_oper_chwidth=");
6797 	if (pos)
6798 		chwidth = atoi(pos + 18);
6799 
6800 	max_oper_chwidth = parse_freq(chwidth, freq2);
6801 	if (max_oper_chwidth < 0)
6802 		return -1;
6803 
6804 	allow_6ghz = os_strstr(cmd, " allow_6ghz") != NULL;
6805 
6806 	if (allow_6ghz && chwidth == 40)
6807 		max_oper_chwidth = CHANWIDTH_40MHZ_6GHZ;
6808 
6809 	return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
6810 			       max_oper_chwidth, pref_freq, he, edmg,
6811 			       allow_6ghz);
6812 }
6813 
6814 
p2p_ctrl_invite_group(struct wpa_supplicant * wpa_s,char * cmd)6815 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
6816 {
6817 	char *pos;
6818 	u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
6819 	bool allow_6ghz;
6820 
6821 	pos = os_strstr(cmd, " peer=");
6822 	if (!pos)
6823 		return -1;
6824 
6825 	*pos = '\0';
6826 	pos += 6;
6827 	if (hwaddr_aton(pos, peer)) {
6828 		wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
6829 		return -1;
6830 	}
6831 
6832 	allow_6ghz = os_strstr(pos, " allow_6ghz") != NULL;
6833 
6834 	pos = os_strstr(pos, " go_dev_addr=");
6835 	if (pos) {
6836 		pos += 13;
6837 		if (hwaddr_aton(pos, go_dev_addr)) {
6838 			wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
6839 				   pos);
6840 			return -1;
6841 		}
6842 		go_dev = go_dev_addr;
6843 	}
6844 
6845 	return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev, allow_6ghz);
6846 }
6847 
6848 
p2p_ctrl_invite(struct wpa_supplicant * wpa_s,char * cmd)6849 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
6850 {
6851 	if (os_strncmp(cmd, "persistent=", 11) == 0)
6852 		return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
6853 	if (os_strncmp(cmd, "group=", 6) == 0)
6854 		return p2p_ctrl_invite_group(wpa_s, cmd + 6);
6855 
6856 	return -1;
6857 }
6858 
6859 
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)6860 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
6861 					 int id, int freq, int vht_center_freq2,
6862 					 int ht40, int vht, int vht_chwidth,
6863 					 int he, int edmg, bool allow_6ghz)
6864 {
6865 	struct wpa_ssid *ssid;
6866 
6867 	ssid = wpa_config_get_network(wpa_s->conf, id);
6868 	if (ssid == NULL || ssid->disabled != 2) {
6869 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
6870 			   "for persistent P2P group",
6871 			   id);
6872 		return -1;
6873 	}
6874 
6875 	return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq,
6876 					     vht_center_freq2, 0, ht40, vht,
6877 					     vht_chwidth, he, edmg,
6878 					     NULL, 0, 0, allow_6ghz);
6879 }
6880 
6881 
p2p_ctrl_group_add(struct wpa_supplicant * wpa_s,char * cmd)6882 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
6883 {
6884 	int freq = 0, persistent = 0, group_id = -1;
6885 	bool allow_6ghz = false;
6886 	int vht = wpa_s->conf->p2p_go_vht;
6887 	int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
6888 	int he = wpa_s->conf->p2p_go_he;
6889 	int edmg = wpa_s->conf->p2p_go_edmg;
6890 	int max_oper_chwidth, chwidth = 0, freq2 = 0;
6891 	char *token, *context = NULL;
6892 #ifdef CONFIG_ACS
6893 	int acs = 0;
6894 #endif /* CONFIG_ACS */
6895 
6896 	while ((token = str_token(cmd, " ", &context))) {
6897 		if (sscanf(token, "freq2=%d", &freq2) == 1 ||
6898 		    sscanf(token, "persistent=%d", &group_id) == 1 ||
6899 		    sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1) {
6900 			continue;
6901 #ifdef CONFIG_ACS
6902 		} else if (os_strcmp(token, "freq=acs") == 0) {
6903 			acs = 1;
6904 #endif /* CONFIG_ACS */
6905 		} else if (sscanf(token, "freq=%d", &freq) == 1) {
6906 			continue;
6907 		} else if (os_strcmp(token, "ht40") == 0) {
6908 			ht40 = 1;
6909 		} else if (os_strcmp(token, "vht") == 0) {
6910 			vht = 1;
6911 			ht40 = 1;
6912 		} else if (os_strcmp(token, "he") == 0) {
6913 			he = 1;
6914 		} else if (os_strcmp(token, "edmg") == 0) {
6915 			edmg = 1;
6916 		} else if (os_strcmp(token, "persistent") == 0) {
6917 			persistent = 1;
6918 		} else if (os_strcmp(token, "allow_6ghz") == 0) {
6919 			allow_6ghz = true;
6920 		} else {
6921 			wpa_printf(MSG_DEBUG,
6922 				   "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
6923 				   token);
6924 			return -1;
6925 		}
6926 	}
6927 
6928 #ifdef CONFIG_ACS
6929 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
6930 	    (acs || freq == 2 || freq == 5)) {
6931 		if (freq == 2 && wpa_s->best_24_freq <= 0) {
6932 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211G;
6933 			wpa_s->p2p_go_do_acs = 1;
6934 			freq = 0;
6935 		} else if (freq == 5 && wpa_s->best_5_freq <= 0) {
6936 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211A;
6937 			wpa_s->p2p_go_do_acs = 1;
6938 			freq = 0;
6939 		} else {
6940 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211ANY;
6941 			wpa_s->p2p_go_do_acs = 1;
6942 		}
6943 	} else {
6944 		wpa_s->p2p_go_do_acs = 0;
6945 	}
6946 #endif /* CONFIG_ACS */
6947 
6948 	max_oper_chwidth = parse_freq(chwidth, freq2);
6949 	if (max_oper_chwidth < 0)
6950 		return -1;
6951 
6952 	if (allow_6ghz && chwidth == 40)
6953 		max_oper_chwidth = CHANWIDTH_40MHZ_6GHZ;
6954 
6955 	/* Allow DFS to be used for Autonomous GO */
6956 	wpa_s->p2p_go_allow_dfs = !!(wpa_s->drv_flags &
6957 				     WPA_DRIVER_FLAGS_DFS_OFFLOAD);
6958 
6959 	if (group_id >= 0)
6960 		return p2p_ctrl_group_add_persistent(wpa_s, group_id,
6961 						     freq, freq2, ht40, vht,
6962 						     max_oper_chwidth, he,
6963 						     edmg, allow_6ghz);
6964 
6965 	return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
6966 				  max_oper_chwidth, he, edmg, allow_6ghz);
6967 }
6968 
6969 
p2p_ctrl_group_member(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)6970 static int p2p_ctrl_group_member(struct wpa_supplicant *wpa_s, const char *cmd,
6971 				 char *buf, size_t buflen)
6972 {
6973 	u8 dev_addr[ETH_ALEN];
6974 	struct wpa_ssid *ssid;
6975 	int res;
6976 	const u8 *iaddr;
6977 
6978 	ssid = wpa_s->current_ssid;
6979 	if (!wpa_s->global->p2p || !ssid || ssid->mode != WPAS_MODE_P2P_GO ||
6980 	    hwaddr_aton(cmd, dev_addr))
6981 		return -1;
6982 
6983 	iaddr = p2p_group_get_client_interface_addr(wpa_s->p2p_group, dev_addr);
6984 	if (!iaddr)
6985 		return -1;
6986 	res = os_snprintf(buf, buflen, MACSTR, MAC2STR(iaddr));
6987 	if (os_snprintf_error(buflen, res))
6988 		return -1;
6989 	return res;
6990 }
6991 
6992 
wpas_find_p2p_dev_addr_bss(struct wpa_global * global,const u8 * p2p_dev_addr)6993 static int wpas_find_p2p_dev_addr_bss(struct wpa_global *global,
6994 				      const u8 *p2p_dev_addr)
6995 {
6996 	struct wpa_supplicant *wpa_s;
6997 
6998 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6999 		if (wpa_bss_get_p2p_dev_addr(wpa_s, p2p_dev_addr))
7000 			return 1;
7001 	}
7002 
7003 	return 0;
7004 }
7005 
7006 
p2p_ctrl_peer(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)7007 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
7008 			 char *buf, size_t buflen)
7009 {
7010 	u8 addr[ETH_ALEN], *addr_ptr, group_capab;
7011 	int next, res;
7012 	const struct p2p_peer_info *info;
7013 	char *pos, *end;
7014 	char devtype[WPS_DEV_TYPE_BUFSIZE];
7015 	struct wpa_ssid *ssid;
7016 	size_t i;
7017 
7018 	if (!wpa_s->global->p2p)
7019 		return -1;
7020 
7021 	if (os_strcmp(cmd, "FIRST") == 0) {
7022 		addr_ptr = NULL;
7023 		next = 0;
7024 	} else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
7025 		if (hwaddr_aton(cmd + 5, addr) < 0)
7026 			return -1;
7027 		addr_ptr = addr;
7028 		next = 1;
7029 	} else {
7030 		if (hwaddr_aton(cmd, addr) < 0)
7031 			return -1;
7032 		addr_ptr = addr;
7033 		next = 0;
7034 	}
7035 
7036 	info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
7037 	if (info == NULL)
7038 		return -1;
7039 	group_capab = info->group_capab;
7040 
7041 	if (group_capab &&
7042 	    !wpas_find_p2p_dev_addr_bss(wpa_s->global, info->p2p_device_addr)) {
7043 		wpa_printf(MSG_DEBUG,
7044 			   "P2P: Could not find any BSS with p2p_dev_addr "
7045 			   MACSTR ", hence override group_capab from 0x%x to 0",
7046 			   MAC2STR(info->p2p_device_addr), group_capab);
7047 		group_capab = 0;
7048 	}
7049 
7050 	pos = buf;
7051 	end = buf + buflen;
7052 
7053 	res = os_snprintf(pos, end - pos, MACSTR "\n"
7054 			  "pri_dev_type=%s\n"
7055 			  "device_name=%s\n"
7056 			  "manufacturer=%s\n"
7057 			  "model_name=%s\n"
7058 			  "model_number=%s\n"
7059 			  "serial_number=%s\n"
7060 			  "config_methods=0x%x\n"
7061 			  "dev_capab=0x%x\n"
7062 			  "group_capab=0x%x\n"
7063 			  "level=%d\n",
7064 			  MAC2STR(info->p2p_device_addr),
7065 			  wps_dev_type_bin2str(info->pri_dev_type,
7066 					       devtype, sizeof(devtype)),
7067 			  info->device_name,
7068 			  info->manufacturer,
7069 			  info->model_name,
7070 			  info->model_number,
7071 			  info->serial_number,
7072 			  info->config_methods,
7073 			  info->dev_capab,
7074 			  group_capab,
7075 			  info->level);
7076 	if (os_snprintf_error(end - pos, res))
7077 		return pos - buf;
7078 	pos += res;
7079 
7080 	for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
7081 	{
7082 		const u8 *t;
7083 		t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
7084 		res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
7085 				  wps_dev_type_bin2str(t, devtype,
7086 						       sizeof(devtype)));
7087 		if (os_snprintf_error(end - pos, res))
7088 			return pos - buf;
7089 		pos += res;
7090 	}
7091 
7092 	ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
7093 	if (ssid) {
7094 		res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
7095 		if (os_snprintf_error(end - pos, res))
7096 			return pos - buf;
7097 		pos += res;
7098 	}
7099 
7100 	res = p2p_get_peer_info_txt(info, pos, end - pos);
7101 	if (res < 0)
7102 		return pos - buf;
7103 	pos += res;
7104 
7105 	if (info->vendor_elems) {
7106 		res = os_snprintf(pos, end - pos, "vendor_elems=");
7107 		if (os_snprintf_error(end - pos, res))
7108 			return pos - buf;
7109 		pos += res;
7110 
7111 		pos += wpa_snprintf_hex(pos, end - pos,
7112 					wpabuf_head(info->vendor_elems),
7113 					wpabuf_len(info->vendor_elems));
7114 
7115 		res = os_snprintf(pos, end - pos, "\n");
7116 		if (os_snprintf_error(end - pos, res))
7117 			return pos - buf;
7118 		pos += res;
7119 	}
7120 
7121 	return pos - buf;
7122 }
7123 
7124 
p2p_ctrl_disallow_freq(struct wpa_supplicant * wpa_s,const char * param)7125 static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
7126 				  const char *param)
7127 {
7128 	unsigned int i;
7129 
7130 	if (wpa_s->global->p2p == NULL)
7131 		return -1;
7132 
7133 	if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
7134 		return -1;
7135 
7136 	for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
7137 		struct wpa_freq_range *freq;
7138 		freq = &wpa_s->global->p2p_disallow_freq.range[i];
7139 		wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
7140 			   freq->min, freq->max);
7141 	}
7142 
7143 	wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
7144 	return 0;
7145 }
7146 
7147 
p2p_ctrl_set(struct wpa_supplicant * wpa_s,char * cmd)7148 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
7149 {
7150 	char *param;
7151 
7152 	if (wpa_s->global->p2p == NULL)
7153 		return -1;
7154 
7155 	param = os_strchr(cmd, ' ');
7156 	if (param == NULL)
7157 		return -1;
7158 	*param++ = '\0';
7159 
7160 	if (os_strcmp(cmd, "discoverability") == 0) {
7161 		p2p_set_client_discoverability(wpa_s->global->p2p,
7162 					       atoi(param));
7163 		return 0;
7164 	}
7165 
7166 	if (os_strcmp(cmd, "managed") == 0) {
7167 		p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
7168 		return 0;
7169 	}
7170 
7171 	if (os_strcmp(cmd, "listen_channel") == 0) {
7172 		char *pos;
7173 		u8 channel, op_class;
7174 
7175 		channel = atoi(param);
7176 		pos = os_strchr(param, ' ');
7177 		op_class = pos ? atoi(pos) : 81;
7178 
7179 		return p2p_set_listen_channel(wpa_s->global->p2p, op_class,
7180 					      channel, 1);
7181 	}
7182 
7183 	if (os_strcmp(cmd, "ssid_postfix") == 0) {
7184 		return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
7185 					    os_strlen(param));
7186 	}
7187 
7188 	if (os_strcmp(cmd, "noa") == 0) {
7189 		char *pos;
7190 		int count, start, duration;
7191 		/* GO NoA parameters: count,start_offset(ms),duration(ms) */
7192 		count = atoi(param);
7193 		pos = os_strchr(param, ',');
7194 		if (pos == NULL)
7195 			return -1;
7196 		pos++;
7197 		start = atoi(pos);
7198 		pos = os_strchr(pos, ',');
7199 		if (pos == NULL)
7200 			return -1;
7201 		pos++;
7202 		duration = atoi(pos);
7203 		if (count < 0 || count > 255 || start < 0 || duration < 0)
7204 			return -1;
7205 		if (count == 0 && duration > 0)
7206 			return -1;
7207 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
7208 			   "start=%d duration=%d", count, start, duration);
7209 		return wpas_p2p_set_noa(wpa_s, count, start, duration);
7210 	}
7211 
7212 	if (os_strcmp(cmd, "ps") == 0)
7213 		return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
7214 
7215 	if (os_strcmp(cmd, "oppps") == 0)
7216 		return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
7217 
7218 	if (os_strcmp(cmd, "ctwindow") == 0)
7219 		return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
7220 
7221 	if (os_strcmp(cmd, "disabled") == 0) {
7222 		wpa_s->global->p2p_disabled = atoi(param);
7223 		wpa_printf(MSG_DEBUG, "P2P functionality %s",
7224 			   wpa_s->global->p2p_disabled ?
7225 			   "disabled" : "enabled");
7226 		if (wpa_s->global->p2p_disabled) {
7227 			wpas_p2p_stop_find(wpa_s);
7228 			os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7229 			p2p_flush(wpa_s->global->p2p);
7230 		}
7231 		return 0;
7232 	}
7233 
7234 	if (os_strcmp(cmd, "conc_pref") == 0) {
7235 		if (os_strcmp(param, "sta") == 0)
7236 			wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
7237 		else if (os_strcmp(param, "p2p") == 0)
7238 			wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
7239 		else {
7240 			wpa_printf(MSG_INFO, "Invalid conc_pref value");
7241 			return -1;
7242 		}
7243 		wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
7244 			   "%s", param);
7245 		return 0;
7246 	}
7247 
7248 	if (os_strcmp(cmd, "force_long_sd") == 0) {
7249 		wpa_s->force_long_sd = atoi(param);
7250 		return 0;
7251 	}
7252 
7253 	if (os_strcmp(cmd, "peer_filter") == 0) {
7254 		u8 addr[ETH_ALEN];
7255 		if (hwaddr_aton(param, addr))
7256 			return -1;
7257 		p2p_set_peer_filter(wpa_s->global->p2p, addr);
7258 		return 0;
7259 	}
7260 
7261 	if (os_strcmp(cmd, "cross_connect") == 0)
7262 		return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
7263 
7264 	if (os_strcmp(cmd, "go_apsd") == 0) {
7265 		if (os_strcmp(param, "disable") == 0)
7266 			wpa_s->set_ap_uapsd = 0;
7267 		else {
7268 			wpa_s->set_ap_uapsd = 1;
7269 			wpa_s->ap_uapsd = atoi(param);
7270 		}
7271 		return 0;
7272 	}
7273 
7274 	if (os_strcmp(cmd, "client_apsd") == 0) {
7275 		if (os_strcmp(param, "disable") == 0)
7276 			wpa_s->set_sta_uapsd = 0;
7277 		else {
7278 			int be, bk, vi, vo;
7279 			char *pos;
7280 			/* format: BE,BK,VI,VO;max SP Length */
7281 			be = atoi(param);
7282 			pos = os_strchr(param, ',');
7283 			if (pos == NULL)
7284 				return -1;
7285 			pos++;
7286 			bk = atoi(pos);
7287 			pos = os_strchr(pos, ',');
7288 			if (pos == NULL)
7289 				return -1;
7290 			pos++;
7291 			vi = atoi(pos);
7292 			pos = os_strchr(pos, ',');
7293 			if (pos == NULL)
7294 				return -1;
7295 			pos++;
7296 			vo = atoi(pos);
7297 			/* ignore max SP Length for now */
7298 
7299 			wpa_s->set_sta_uapsd = 1;
7300 			wpa_s->sta_uapsd = 0;
7301 			if (be)
7302 				wpa_s->sta_uapsd |= BIT(0);
7303 			if (bk)
7304 				wpa_s->sta_uapsd |= BIT(1);
7305 			if (vi)
7306 				wpa_s->sta_uapsd |= BIT(2);
7307 			if (vo)
7308 				wpa_s->sta_uapsd |= BIT(3);
7309 		}
7310 		return 0;
7311 	}
7312 
7313 	if (os_strcmp(cmd, "disallow_freq") == 0)
7314 		return p2p_ctrl_disallow_freq(wpa_s, param);
7315 
7316 	if (os_strcmp(cmd, "disc_int") == 0) {
7317 		int min_disc_int, max_disc_int, max_disc_tu;
7318 		char *pos;
7319 
7320 		pos = param;
7321 
7322 		min_disc_int = atoi(pos);
7323 		pos = os_strchr(pos, ' ');
7324 		if (pos == NULL)
7325 			return -1;
7326 		*pos++ = '\0';
7327 
7328 		max_disc_int = atoi(pos);
7329 		pos = os_strchr(pos, ' ');
7330 		if (pos == NULL)
7331 			return -1;
7332 		*pos++ = '\0';
7333 
7334 		max_disc_tu = atoi(pos);
7335 
7336 		return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
7337 					max_disc_int, max_disc_tu);
7338 	}
7339 
7340 	if (os_strcmp(cmd, "per_sta_psk") == 0) {
7341 		wpa_s->global->p2p_per_sta_psk = !!atoi(param);
7342 		return 0;
7343 	}
7344 
7345 #ifdef CONFIG_WPS_NFC
7346 	if (os_strcmp(cmd, "nfc_tag") == 0)
7347 		return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
7348 #endif /* CONFIG_WPS_NFC */
7349 
7350 	if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
7351 		wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
7352 		return 0;
7353 	}
7354 
7355 	if (os_strcmp(cmd, "override_pref_op_chan") == 0) {
7356 		int op_class, chan;
7357 
7358 		op_class = atoi(param);
7359 		param = os_strchr(param, ':');
7360 		if (!param)
7361 			return -1;
7362 		param++;
7363 		chan = atoi(param);
7364 		p2p_set_override_pref_op_chan(wpa_s->global->p2p, op_class,
7365 					      chan);
7366 		return 0;
7367 	}
7368 
7369 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
7370 		   cmd);
7371 
7372 	return -1;
7373 }
7374 
7375 
p2p_ctrl_flush(struct wpa_supplicant * wpa_s)7376 static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
7377 {
7378 	os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7379 	wpa_s->force_long_sd = 0;
7380 
7381 #ifdef CONFIG_TESTING_OPTIONS
7382 	os_free(wpa_s->get_pref_freq_list_override);
7383 	wpa_s->get_pref_freq_list_override = NULL;
7384 #endif /* CONFIG_TESTING_OPTIONS */
7385 
7386 	wpas_p2p_stop_find(wpa_s);
7387 	wpa_s->parent->p2ps_method_config_any = 0;
7388 	if (wpa_s->global->p2p)
7389 		p2p_flush(wpa_s->global->p2p);
7390 }
7391 
7392 
p2p_ctrl_presence_req(struct wpa_supplicant * wpa_s,char * cmd)7393 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
7394 {
7395 	char *pos, *pos2;
7396 	unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
7397 
7398 	if (cmd[0]) {
7399 		pos = os_strchr(cmd, ' ');
7400 		if (pos == NULL)
7401 			return -1;
7402 		*pos++ = '\0';
7403 		dur1 = atoi(cmd);
7404 
7405 		pos2 = os_strchr(pos, ' ');
7406 		if (pos2)
7407 			*pos2++ = '\0';
7408 		int1 = atoi(pos);
7409 	} else
7410 		pos2 = NULL;
7411 
7412 	if (pos2) {
7413 		pos = os_strchr(pos2, ' ');
7414 		if (pos == NULL)
7415 			return -1;
7416 		*pos++ = '\0';
7417 		dur2 = atoi(pos2);
7418 		int2 = atoi(pos);
7419 	}
7420 
7421 	return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
7422 }
7423 
7424 
p2p_ctrl_ext_listen(struct wpa_supplicant * wpa_s,char * cmd)7425 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
7426 {
7427 	char *pos;
7428 	unsigned int period = 0, interval = 0;
7429 
7430 	if (cmd[0]) {
7431 		pos = os_strchr(cmd, ' ');
7432 		if (pos == NULL)
7433 			return -1;
7434 		*pos++ = '\0';
7435 		period = atoi(cmd);
7436 		interval = atoi(pos);
7437 	}
7438 
7439 	return wpas_p2p_ext_listen(wpa_s, period, interval);
7440 }
7441 
7442 
p2p_ctrl_remove_client(struct wpa_supplicant * wpa_s,const char * cmd)7443 static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
7444 {
7445 	const char *pos;
7446 	u8 peer[ETH_ALEN];
7447 	int iface_addr = 0;
7448 
7449 	pos = cmd;
7450 	if (os_strncmp(pos, "iface=", 6) == 0) {
7451 		iface_addr = 1;
7452 		pos += 6;
7453 	}
7454 	if (hwaddr_aton(pos, peer))
7455 		return -1;
7456 
7457 	wpas_p2p_remove_client(wpa_s, peer, iface_addr);
7458 	return 0;
7459 }
7460 
7461 
p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant * wpa_s,char * cmd)7462 static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
7463 {
7464 	int freq = 0, period = 0, interval = 0, count = 0;
7465 
7466 	if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
7467 	{
7468 		wpa_printf(MSG_DEBUG,
7469 			   "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
7470 		return -1;
7471 	}
7472 
7473 	return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
7474 }
7475 
7476 #endif /* CONFIG_P2P */
7477 
7478 
freq_range_to_channel_list(struct wpa_supplicant * wpa_s,char * val)7479 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
7480 {
7481 	struct wpa_freq_range_list ranges;
7482 	int *freqs = NULL;
7483 	struct hostapd_hw_modes *mode;
7484 	u16 i;
7485 
7486 	if (wpa_s->hw.modes == NULL)
7487 		return NULL;
7488 
7489 	os_memset(&ranges, 0, sizeof(ranges));
7490 	if (freq_range_list_parse(&ranges, val) < 0)
7491 		return NULL;
7492 
7493 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
7494 		int j;
7495 
7496 		mode = &wpa_s->hw.modes[i];
7497 		for (j = 0; j < mode->num_channels; j++) {
7498 			unsigned int freq;
7499 
7500 			if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
7501 				continue;
7502 
7503 			freq = mode->channels[j].freq;
7504 			if (!freq_range_list_includes(&ranges, freq))
7505 				continue;
7506 
7507 			int_array_add_unique(&freqs, freq);
7508 		}
7509 	}
7510 
7511 	os_free(ranges.range);
7512 	return freqs;
7513 }
7514 
7515 
7516 #ifdef CONFIG_INTERWORKING
7517 
ctrl_interworking_select(struct wpa_supplicant * wpa_s,char * param)7518 static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
7519 {
7520 	int auto_sel = 0;
7521 	int *freqs = NULL;
7522 
7523 	if (param) {
7524 		char *pos;
7525 
7526 		auto_sel = os_strstr(param, "auto") != NULL;
7527 
7528 		pos = os_strstr(param, "freq=");
7529 		if (pos) {
7530 			freqs = freq_range_to_channel_list(wpa_s, pos + 5);
7531 			if (freqs == NULL)
7532 				return -1;
7533 		}
7534 
7535 	}
7536 
7537 	return interworking_select(wpa_s, auto_sel, freqs);
7538 }
7539 
7540 
ctrl_interworking_connect(struct wpa_supplicant * wpa_s,char * dst,int only_add)7541 static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
7542 				     int only_add)
7543 {
7544 	u8 bssid[ETH_ALEN];
7545 	struct wpa_bss *bss;
7546 
7547 	if (hwaddr_aton(dst, bssid)) {
7548 		wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
7549 		return -1;
7550 	}
7551 
7552 	bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
7553 	if (bss == NULL) {
7554 		wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
7555 			   MAC2STR(bssid));
7556 		return -1;
7557 	}
7558 
7559 	if (bss->ssid_len == 0) {
7560 		int found = 0;
7561 
7562 		wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
7563 			   " does not have SSID information", MAC2STR(bssid));
7564 
7565 		dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
7566 					 list) {
7567 			if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
7568 			    bss->ssid_len > 0) {
7569 				found = 1;
7570 				break;
7571 			}
7572 		}
7573 
7574 		if (!found)
7575 			return -1;
7576 		wpa_printf(MSG_DEBUG,
7577 			   "Found another matching BSS entry with SSID");
7578 	}
7579 
7580 	return interworking_connect(wpa_s, bss, only_add);
7581 }
7582 
7583 
get_anqp(struct wpa_supplicant * wpa_s,char * dst)7584 static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
7585 {
7586 	u8 dst_addr[ETH_ALEN];
7587 	int used, freq = 0;
7588 	char *pos;
7589 #define MAX_ANQP_INFO_ID 100
7590 	u16 id[MAX_ANQP_INFO_ID];
7591 	size_t num_id = 0;
7592 	u32 subtypes = 0;
7593 	u32 mbo_subtypes = 0;
7594 
7595 	used = hwaddr_aton2(dst, dst_addr);
7596 	if (used < 0)
7597 		return -1;
7598 	pos = dst + used;
7599 	if (*pos == ' ')
7600 		pos++;
7601 
7602 	if (os_strncmp(pos, "freq=", 5) == 0) {
7603 		freq = atoi(pos + 5);
7604 		pos = os_strchr(pos, ' ');
7605 		if (!pos)
7606 			return -1;
7607 		pos++;
7608 	}
7609 
7610 	while (num_id < MAX_ANQP_INFO_ID) {
7611 		if (os_strncmp(pos, "hs20:", 5) == 0) {
7612 #ifdef CONFIG_HS20
7613 			int num = atoi(pos + 5);
7614 			if (num <= 0 || num > 31)
7615 				return -1;
7616 			subtypes |= BIT(num);
7617 #else /* CONFIG_HS20 */
7618 			return -1;
7619 #endif /* CONFIG_HS20 */
7620 		} else if (os_strncmp(pos, "mbo:", 4) == 0) {
7621 #ifdef CONFIG_MBO
7622 			int num = atoi(pos + 4);
7623 
7624 			if (num <= 0 || num > MAX_MBO_ANQP_SUBTYPE)
7625 				return -1;
7626 			mbo_subtypes |= BIT(num);
7627 #else /* CONFIG_MBO */
7628 			return -1;
7629 #endif /* CONFIG_MBO */
7630 		} else {
7631 			id[num_id] = atoi(pos);
7632 			if (id[num_id])
7633 				num_id++;
7634 		}
7635 		pos = os_strchr(pos + 1, ',');
7636 		if (pos == NULL)
7637 			break;
7638 		pos++;
7639 	}
7640 
7641 	if (num_id == 0 && !subtypes && !mbo_subtypes)
7642 		return -1;
7643 
7644 	return anqp_send_req(wpa_s, dst_addr, freq, id, num_id, subtypes,
7645 			     mbo_subtypes);
7646 }
7647 
7648 
gas_request(struct wpa_supplicant * wpa_s,char * cmd)7649 static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
7650 {
7651 	u8 dst_addr[ETH_ALEN];
7652 	struct wpabuf *advproto, *query = NULL;
7653 	int used, ret = -1;
7654 	char *pos, *end;
7655 	size_t len;
7656 
7657 	used = hwaddr_aton2(cmd, dst_addr);
7658 	if (used < 0)
7659 		return -1;
7660 
7661 	pos = cmd + used;
7662 	while (*pos == ' ')
7663 		pos++;
7664 
7665 	/* Advertisement Protocol ID */
7666 	end = os_strchr(pos, ' ');
7667 	if (end)
7668 		len = end - pos;
7669 	else
7670 		len = os_strlen(pos);
7671 	if (len & 0x01)
7672 		return -1;
7673 	len /= 2;
7674 	if (len == 0)
7675 		return -1;
7676 	advproto = wpabuf_alloc(len);
7677 	if (advproto == NULL)
7678 		return -1;
7679 	if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
7680 		goto fail;
7681 
7682 	if (end) {
7683 		/* Optional Query Request */
7684 		pos = end + 1;
7685 		while (*pos == ' ')
7686 			pos++;
7687 
7688 		len = os_strlen(pos);
7689 		if (len) {
7690 			if (len & 0x01)
7691 				goto fail;
7692 			len /= 2;
7693 			if (len == 0)
7694 				goto fail;
7695 			query = wpabuf_alloc(len);
7696 			if (query == NULL)
7697 				goto fail;
7698 			if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
7699 				goto fail;
7700 		}
7701 	}
7702 
7703 	ret = gas_send_request(wpa_s, dst_addr, advproto, query);
7704 
7705 fail:
7706 	wpabuf_free(advproto);
7707 	wpabuf_free(query);
7708 
7709 	return ret;
7710 }
7711 
7712 
gas_response_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)7713 static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
7714 			    size_t buflen)
7715 {
7716 	u8 addr[ETH_ALEN];
7717 	int dialog_token;
7718 	int used;
7719 	char *pos;
7720 	size_t resp_len, start, requested_len;
7721 	struct wpabuf *resp;
7722 	int ret;
7723 
7724 	used = hwaddr_aton2(cmd, addr);
7725 	if (used < 0)
7726 		return -1;
7727 
7728 	pos = cmd + used;
7729 	while (*pos == ' ')
7730 		pos++;
7731 	dialog_token = atoi(pos);
7732 
7733 	if (wpa_s->last_gas_resp &&
7734 	    os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
7735 	    dialog_token == wpa_s->last_gas_dialog_token)
7736 		resp = wpa_s->last_gas_resp;
7737 	else if (wpa_s->prev_gas_resp &&
7738 		 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
7739 		 dialog_token == wpa_s->prev_gas_dialog_token)
7740 		resp = wpa_s->prev_gas_resp;
7741 	else
7742 		return -1;
7743 
7744 	resp_len = wpabuf_len(resp);
7745 	start = 0;
7746 	requested_len = resp_len;
7747 
7748 	pos = os_strchr(pos, ' ');
7749 	if (pos) {
7750 		start = atoi(pos);
7751 		if (start > resp_len)
7752 			return os_snprintf(buf, buflen, "FAIL-Invalid range");
7753 		pos = os_strchr(pos, ',');
7754 		if (pos == NULL)
7755 			return -1;
7756 		pos++;
7757 		requested_len = atoi(pos);
7758 		if (start + requested_len > resp_len)
7759 			return os_snprintf(buf, buflen, "FAIL-Invalid range");
7760 	}
7761 
7762 	if (requested_len * 2 + 1 > buflen)
7763 		return os_snprintf(buf, buflen, "FAIL-Too long response");
7764 
7765 	ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
7766 			       requested_len);
7767 
7768 	if (start + requested_len == resp_len) {
7769 		/*
7770 		 * Free memory by dropping the response after it has been
7771 		 * fetched.
7772 		 */
7773 		if (resp == wpa_s->prev_gas_resp) {
7774 			wpabuf_free(wpa_s->prev_gas_resp);
7775 			wpa_s->prev_gas_resp = NULL;
7776 		} else {
7777 			wpabuf_free(wpa_s->last_gas_resp);
7778 			wpa_s->last_gas_resp = NULL;
7779 		}
7780 	}
7781 
7782 	return ret;
7783 }
7784 #endif /* CONFIG_INTERWORKING */
7785 
7786 
7787 #ifdef CONFIG_HS20
7788 
get_hs20_anqp(struct wpa_supplicant * wpa_s,char * dst)7789 static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
7790 {
7791 	u8 dst_addr[ETH_ALEN];
7792 	int used;
7793 	char *pos;
7794 	u32 subtypes = 0;
7795 
7796 	used = hwaddr_aton2(dst, dst_addr);
7797 	if (used < 0)
7798 		return -1;
7799 	pos = dst + used;
7800 	if (*pos == ' ')
7801 		pos++;
7802 	for (;;) {
7803 		int num = atoi(pos);
7804 		if (num <= 0 || num > 31)
7805 			return -1;
7806 		subtypes |= BIT(num);
7807 		pos = os_strchr(pos + 1, ',');
7808 		if (pos == NULL)
7809 			break;
7810 		pos++;
7811 	}
7812 
7813 	if (subtypes == 0)
7814 		return -1;
7815 
7816 	return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
7817 }
7818 
7819 
hs20_nai_home_realm_list(struct wpa_supplicant * wpa_s,const u8 * addr,const char * realm)7820 static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
7821 				    const u8 *addr, const char *realm)
7822 {
7823 	u8 *buf;
7824 	size_t rlen, len;
7825 	int ret;
7826 
7827 	rlen = os_strlen(realm);
7828 	len = 3 + rlen;
7829 	buf = os_malloc(len);
7830 	if (buf == NULL)
7831 		return -1;
7832 	buf[0] = 1; /* NAI Home Realm Count */
7833 	buf[1] = 0; /* Formatted in accordance with RFC 4282 */
7834 	buf[2] = rlen;
7835 	os_memcpy(buf + 3, realm, rlen);
7836 
7837 	ret = hs20_anqp_send_req(wpa_s, addr,
7838 				 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
7839 				 buf, len, 0);
7840 
7841 	os_free(buf);
7842 
7843 	return ret;
7844 }
7845 
7846 
hs20_get_nai_home_realm_list(struct wpa_supplicant * wpa_s,char * dst)7847 static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
7848 					char *dst)
7849 {
7850 	struct wpa_cred *cred = wpa_s->conf->cred;
7851 	u8 dst_addr[ETH_ALEN];
7852 	int used;
7853 	u8 *buf;
7854 	size_t len;
7855 	int ret;
7856 
7857 	used = hwaddr_aton2(dst, dst_addr);
7858 	if (used < 0)
7859 		return -1;
7860 
7861 	while (dst[used] == ' ')
7862 		used++;
7863 	if (os_strncmp(dst + used, "realm=", 6) == 0)
7864 		return hs20_nai_home_realm_list(wpa_s, dst_addr,
7865 						dst + used + 6);
7866 
7867 	len = os_strlen(dst + used);
7868 
7869 	if (len == 0 && cred && cred->realm)
7870 		return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
7871 
7872 	if (len & 1)
7873 		return -1;
7874 	len /= 2;
7875 	buf = os_malloc(len);
7876 	if (buf == NULL)
7877 		return -1;
7878 	if (hexstr2bin(dst + used, buf, len) < 0) {
7879 		os_free(buf);
7880 		return -1;
7881 	}
7882 
7883 	ret = hs20_anqp_send_req(wpa_s, dst_addr,
7884 				 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
7885 				 buf, len, 0);
7886 	os_free(buf);
7887 
7888 	return ret;
7889 }
7890 
7891 
get_hs20_icon(struct wpa_supplicant * wpa_s,char * cmd,char * reply,int buflen)7892 static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
7893 			 int buflen)
7894 {
7895 	u8 dst_addr[ETH_ALEN];
7896 	int used;
7897 	char *ctx = NULL, *icon, *poffset, *psize;
7898 
7899 	used = hwaddr_aton2(cmd, dst_addr);
7900 	if (used < 0)
7901 		return -1;
7902 	cmd += used;
7903 
7904 	icon = str_token(cmd, " ", &ctx);
7905 	poffset = str_token(cmd, " ", &ctx);
7906 	psize = str_token(cmd, " ", &ctx);
7907 	if (!icon || !poffset || !psize)
7908 		return -1;
7909 
7910 	wpa_s->fetch_osu_icon_in_progress = 0;
7911 	return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
7912 			     reply, buflen);
7913 }
7914 
7915 
del_hs20_icon(struct wpa_supplicant * wpa_s,char * cmd)7916 static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
7917 {
7918 	u8 dst_addr[ETH_ALEN];
7919 	int used;
7920 	char *icon;
7921 
7922 	if (!cmd[0])
7923 		return hs20_del_icon(wpa_s, NULL, NULL);
7924 
7925 	used = hwaddr_aton2(cmd, dst_addr);
7926 	if (used < 0)
7927 		return -1;
7928 
7929 	while (cmd[used] == ' ')
7930 		used++;
7931 	icon = cmd[used] ? &cmd[used] : NULL;
7932 
7933 	return hs20_del_icon(wpa_s, dst_addr, icon);
7934 }
7935 
7936 
hs20_icon_request(struct wpa_supplicant * wpa_s,char * cmd,int inmem)7937 static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
7938 {
7939 	u8 dst_addr[ETH_ALEN];
7940 	int used;
7941 	char *icon;
7942 
7943 	used = hwaddr_aton2(cmd, dst_addr);
7944 	if (used < 0)
7945 		return -1;
7946 
7947 	while (cmd[used] == ' ')
7948 		used++;
7949 	icon = &cmd[used];
7950 
7951 	wpa_s->fetch_osu_icon_in_progress = 0;
7952 	return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
7953 				  (u8 *) icon, os_strlen(icon), inmem);
7954 }
7955 
7956 #endif /* CONFIG_HS20 */
7957 
7958 
7959 #ifdef CONFIG_AUTOSCAN
7960 
wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant * wpa_s,char * cmd)7961 static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
7962 					      char *cmd)
7963 {
7964 	enum wpa_states state = wpa_s->wpa_state;
7965 	char *new_params = NULL;
7966 
7967 	if (os_strlen(cmd) > 0) {
7968 		new_params = os_strdup(cmd);
7969 		if (new_params == NULL)
7970 			return -1;
7971 	}
7972 
7973 	os_free(wpa_s->conf->autoscan);
7974 	wpa_s->conf->autoscan = new_params;
7975 
7976 	if (wpa_s->conf->autoscan == NULL)
7977 		autoscan_deinit(wpa_s);
7978 	else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
7979 		autoscan_init(wpa_s, 1);
7980 	else if (state == WPA_SCANNING)
7981 		wpa_supplicant_reinit_autoscan(wpa_s);
7982 	else
7983 		wpa_printf(MSG_DEBUG, "No autoscan update in state %s",
7984 			   wpa_supplicant_state_txt(state));
7985 
7986 	return 0;
7987 }
7988 
7989 #endif /* CONFIG_AUTOSCAN */
7990 
7991 
7992 #ifdef CONFIG_WNM
7993 
wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant * wpa_s,char * cmd)7994 static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
7995 {
7996 	int enter;
7997 	int intval = 0;
7998 	char *pos;
7999 	int ret;
8000 	struct wpabuf *tfs_req = NULL;
8001 
8002 	if (os_strncmp(cmd, "enter", 5) == 0)
8003 		enter = 1;
8004 	else if (os_strncmp(cmd, "exit", 4) == 0)
8005 		enter = 0;
8006 	else
8007 		return -1;
8008 
8009 	pos = os_strstr(cmd, " interval=");
8010 	if (pos)
8011 		intval = atoi(pos + 10);
8012 
8013 	pos = os_strstr(cmd, " tfs_req=");
8014 	if (pos) {
8015 		char *end;
8016 		size_t len;
8017 		pos += 9;
8018 		end = os_strchr(pos, ' ');
8019 		if (end)
8020 			len = end - pos;
8021 		else
8022 			len = os_strlen(pos);
8023 		if (len & 1)
8024 			return -1;
8025 		len /= 2;
8026 		tfs_req = wpabuf_alloc(len);
8027 		if (tfs_req == NULL)
8028 			return -1;
8029 		if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
8030 			wpabuf_free(tfs_req);
8031 			return -1;
8032 		}
8033 	}
8034 
8035 	ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
8036 					   WNM_SLEEP_MODE_EXIT, intval,
8037 					   tfs_req);
8038 	wpabuf_free(tfs_req);
8039 
8040 	return ret;
8041 }
8042 
8043 
wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant * wpa_s,char * cmd)8044 static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
8045 {
8046 	int query_reason, list = 0;
8047 	char *btm_candidates = NULL;
8048 
8049 	query_reason = atoi(cmd);
8050 
8051 	cmd = os_strchr(cmd, ' ');
8052 	if (cmd) {
8053 		if (os_strncmp(cmd, " list", 5) == 0)
8054 			list = 1;
8055 		else
8056 			btm_candidates = cmd;
8057 	}
8058 
8059 	wpa_printf(MSG_DEBUG,
8060 		   "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
8061 		   query_reason, list ? " candidate list" : "");
8062 
8063 	return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason,
8064 						  btm_candidates,
8065 						  list);
8066 }
8067 
8068 
wpas_ctrl_iface_coloc_intf_report(struct wpa_supplicant * wpa_s,char * cmd)8069 static int wpas_ctrl_iface_coloc_intf_report(struct wpa_supplicant *wpa_s,
8070 					     char *cmd)
8071 {
8072 	struct wpabuf *elems;
8073 	int ret;
8074 
8075 	elems = wpabuf_parse_bin(cmd);
8076 	if (!elems)
8077 		return -1;
8078 
8079 	ret = wnm_send_coloc_intf_report(wpa_s, 0, elems);
8080 	wpabuf_free(elems);
8081 	return ret;
8082 }
8083 
8084 #endif /* CONFIG_WNM */
8085 
8086 
wpa_supplicant_signal_poll(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8087 static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
8088 				      size_t buflen)
8089 {
8090 	struct wpa_signal_info si;
8091 	int ret;
8092 	char *pos, *end;
8093 
8094 	ret = wpa_drv_signal_poll(wpa_s, &si);
8095 	if (ret)
8096 		return -1;
8097 
8098 	pos = buf;
8099 	end = buf + buflen;
8100 
8101 	ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
8102 			  "NOISE=%d\nFREQUENCY=%u\n",
8103 			  si.current_signal, si.current_txrate / 1000,
8104 			  si.current_noise, si.frequency);
8105 	if (os_snprintf_error(end - pos, ret))
8106 		return -1;
8107 	pos += ret;
8108 
8109 	if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
8110 		ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
8111 				  channel_width_to_string(si.chanwidth));
8112 		if (os_snprintf_error(end - pos, ret))
8113 			return -1;
8114 		pos += ret;
8115 	}
8116 
8117 	if (si.center_frq1 > 0) {
8118 		ret = os_snprintf(pos, end - pos, "CENTER_FRQ1=%d\n",
8119 				  si.center_frq1);
8120 		if (os_snprintf_error(end - pos, ret))
8121 			return -1;
8122 		pos += ret;
8123 	}
8124 
8125 	if (si.center_frq2 > 0) {
8126 		ret = os_snprintf(pos, end - pos, "CENTER_FRQ2=%d\n",
8127 				  si.center_frq2);
8128 		if (os_snprintf_error(end - pos, ret))
8129 			return -1;
8130 		pos += ret;
8131 	}
8132 
8133 	if (si.avg_signal) {
8134 		ret = os_snprintf(pos, end - pos,
8135 				  "AVG_RSSI=%d\n", si.avg_signal);
8136 		if (os_snprintf_error(end - pos, ret))
8137 			return -1;
8138 		pos += ret;
8139 	}
8140 
8141 	if (si.avg_beacon_signal) {
8142 		ret = os_snprintf(pos, end - pos,
8143 				  "AVG_BEACON_RSSI=%d\n", si.avg_beacon_signal);
8144 		if (os_snprintf_error(end - pos, ret))
8145 			return -1;
8146 		pos += ret;
8147 	}
8148 
8149 	return pos - buf;
8150 }
8151 
8152 
wpas_ctrl_iface_signal_monitor(struct wpa_supplicant * wpa_s,const char * cmd)8153 static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
8154 					  const char *cmd)
8155 {
8156 	const char *pos;
8157 	int threshold = 0;
8158 	int hysteresis = 0;
8159 
8160 	if (wpa_s->bgscan && wpa_s->bgscan_priv) {
8161 		wpa_printf(MSG_DEBUG,
8162 			   "Reject SIGNAL_MONITOR command - bgscan is active");
8163 		return -1;
8164 	}
8165 	pos = os_strstr(cmd, "THRESHOLD=");
8166 	if (pos)
8167 		threshold = atoi(pos + 10);
8168 	pos = os_strstr(cmd, "HYSTERESIS=");
8169 	if (pos)
8170 		hysteresis = atoi(pos + 11);
8171 	return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
8172 }
8173 
8174 
8175 #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,unsigned int * freq_list)8176 int wpas_ctrl_iface_get_pref_freq_list_override(struct wpa_supplicant *wpa_s,
8177 						enum wpa_driver_if_type if_type,
8178 						unsigned int *num,
8179 						unsigned int *freq_list)
8180 {
8181 	char *pos = wpa_s->get_pref_freq_list_override;
8182 	char *end;
8183 	unsigned int count = 0;
8184 
8185 	/* Override string format:
8186 	 *  <if_type1>:<freq1>,<freq2>,... <if_type2>:... */
8187 
8188 	while (pos) {
8189 		if (atoi(pos) == (int) if_type)
8190 			break;
8191 		pos = os_strchr(pos, ' ');
8192 		if (pos)
8193 			pos++;
8194 	}
8195 	if (!pos)
8196 		return -1;
8197 	pos = os_strchr(pos, ':');
8198 	if (!pos)
8199 		return -1;
8200 	pos++;
8201 	end = os_strchr(pos, ' ');
8202 	while (pos && (!end || pos < end) && count < *num) {
8203 		freq_list[count++] = atoi(pos);
8204 		pos = os_strchr(pos, ',');
8205 		if (pos)
8206 			pos++;
8207 	}
8208 
8209 	*num = count;
8210 	return 0;
8211 }
8212 #endif /* CONFIG_TESTING_OPTIONS */
8213 
8214 
wpas_ctrl_iface_get_pref_freq_list(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8215 static int wpas_ctrl_iface_get_pref_freq_list(
8216 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
8217 {
8218 	unsigned int freq_list[100], num = 100, i;
8219 	int ret;
8220 	enum wpa_driver_if_type iface_type;
8221 	char *pos, *end;
8222 
8223 	pos = buf;
8224 	end = buf + buflen;
8225 
8226 	/* buf: "<interface_type>" */
8227 	if (os_strcmp(cmd, "STATION") == 0)
8228 		iface_type = WPA_IF_STATION;
8229 	else if (os_strcmp(cmd, "AP") == 0)
8230 		iface_type = WPA_IF_AP_BSS;
8231 	else if (os_strcmp(cmd, "P2P_GO") == 0)
8232 		iface_type = WPA_IF_P2P_GO;
8233 	else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
8234 		iface_type = WPA_IF_P2P_CLIENT;
8235 	else if (os_strcmp(cmd, "IBSS") == 0)
8236 		iface_type = WPA_IF_IBSS;
8237 	else if (os_strcmp(cmd, "TDLS") == 0)
8238 		iface_type = WPA_IF_TDLS;
8239 	else
8240 		return -1;
8241 
8242 	wpa_printf(MSG_DEBUG,
8243 		   "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
8244 		   iface_type, cmd);
8245 
8246 	ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
8247 	if (ret)
8248 		return -1;
8249 
8250 	for (i = 0; i < num; i++) {
8251 		ret = os_snprintf(pos, end - pos, "%s%u",
8252 				  i > 0 ? "," : "", freq_list[i]);
8253 		if (os_snprintf_error(end - pos, ret))
8254 			return -1;
8255 		pos += ret;
8256 	}
8257 
8258 	return pos - buf;
8259 }
8260 
8261 
wpas_ctrl_iface_driver_flags(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8262 static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
8263 					char *buf, size_t buflen)
8264 {
8265 	int ret, i;
8266 	char *pos, *end;
8267 
8268 	ret = os_snprintf(buf, buflen, "%016llX:\n",
8269 			  (long long unsigned) wpa_s->drv_flags);
8270 	if (os_snprintf_error(buflen, ret))
8271 		return -1;
8272 
8273 	pos = buf + ret;
8274 	end = buf + buflen;
8275 
8276 	for (i = 0; i < 64; i++) {
8277 		if (wpa_s->drv_flags & (1LLU << i)) {
8278 			ret = os_snprintf(pos, end - pos, "%s\n",
8279 					  driver_flag_to_string(1LLU << i));
8280 			if (os_snprintf_error(end - pos, ret))
8281 				return -1;
8282 			pos += ret;
8283 		}
8284 	}
8285 
8286 	return pos - buf;
8287 }
8288 
8289 
wpas_ctrl_iface_driver_flags2(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8290 static int wpas_ctrl_iface_driver_flags2(struct wpa_supplicant *wpa_s,
8291 					 char *buf, size_t buflen)
8292 {
8293 	int ret, i;
8294 	char *pos, *end;
8295 
8296 	ret = os_snprintf(buf, buflen, "%016llX:\n",
8297 			  (long long unsigned) wpa_s->drv_flags2);
8298 	if (os_snprintf_error(buflen, ret))
8299 		return -1;
8300 
8301 	pos = buf + ret;
8302 	end = buf + buflen;
8303 
8304 	for (i = 0; i < 64; i++) {
8305 		if (wpa_s->drv_flags2 & (1LLU << i)) {
8306 			ret = os_snprintf(pos, end - pos, "%s\n",
8307 					  driver_flag2_to_string(1LLU << i));
8308 			if (os_snprintf_error(end - pos, ret))
8309 				return -1;
8310 			pos += ret;
8311 		}
8312 	}
8313 
8314 	return pos - buf;
8315 }
8316 
8317 
wpa_supplicant_pktcnt_poll(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8318 static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
8319 				      size_t buflen)
8320 {
8321 	struct hostap_sta_driver_data sta;
8322 	int ret;
8323 
8324 	ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
8325 	if (ret)
8326 		return -1;
8327 
8328 	ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
8329 			  sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
8330 	if (os_snprintf_error(buflen, ret))
8331 		return -1;
8332 	return ret;
8333 }
8334 
8335 
8336 #if defined(ANDROID) || defined(CONFIG_DRIVER_NL80211_HISI)
wpa_supplicant_driver_cmd(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8337 static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
8338 				     char *buf, size_t buflen)
8339 {
8340 	int ret;
8341 	size_t len = buflen;
8342 
8343 #ifdef CONFIG_OPEN_HARMONY_PATCH
8344 	len = buflen > 4096 ? 4096 : buflen;
8345 #endif
8346 	ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, len);
8347 	if (ret == 0) {
8348 		if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
8349 			struct p2p_data *p2p = wpa_s->global->p2p;
8350 			if (p2p) {
8351 				char country[3];
8352 				country[0] = cmd[8];
8353 				country[1] = cmd[9];
8354 				country[2] = 0x04;
8355 				p2p_set_country(p2p, country);
8356 			}
8357 		}
8358 		ret = os_snprintf(buf, buflen, "%s\n", "OK");
8359 		if (os_snprintf_error(buflen, ret))
8360 			ret = -1;
8361 	}
8362 	return ret;
8363 }
8364 #endif /* ANDROID || CONFIG_DRIVER_NL80211_HISI */
8365 
8366 
wpa_supplicant_vendor_cmd(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8367 static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
8368 				     char *buf, size_t buflen)
8369 {
8370 	int ret;
8371 	char *pos, *temp = NULL;
8372 	u8 *data = NULL;
8373 	unsigned int vendor_id, subcmd;
8374 	enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
8375 	struct wpabuf *reply;
8376 	size_t data_len = 0;
8377 
8378 	/**
8379 	 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
8380 	 * [nested=<0|1>]
8381 	 */
8382 	vendor_id = strtoul(cmd, &pos, 16);
8383 	if (!isblank((unsigned char) *pos))
8384 		return -EINVAL;
8385 
8386 	subcmd = strtoul(pos, &pos, 10);
8387 
8388 	if (*pos != '\0') {
8389 		if (!isblank((unsigned char) *pos++))
8390 			return -EINVAL;
8391 
8392 		temp = os_strchr(pos, ' ');
8393 		data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
8394 	}
8395 
8396 	if (data_len) {
8397 		data_len /= 2;
8398 		data = os_malloc(data_len);
8399 		if (!data)
8400 			return -1;
8401 
8402 		if (hexstr2bin(pos, data, data_len)) {
8403 			wpa_printf(MSG_DEBUG,
8404 				   "Vendor command: wrong parameter format");
8405 			os_free(data);
8406 			return -EINVAL;
8407 		}
8408 	}
8409 
8410 	pos = os_strstr(cmd, "nested=");
8411 	if (pos)
8412 		nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
8413 			NESTED_ATTR_NOT_USED;
8414 
8415 	reply = wpabuf_alloc((buflen - 1) / 2);
8416 	if (!reply) {
8417 		os_free(data);
8418 		return -1;
8419 	}
8420 
8421 	ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
8422 				 nested_attr_flag, reply);
8423 
8424 	if (ret == 0)
8425 		ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
8426 				       wpabuf_len(reply));
8427 
8428 	wpabuf_free(reply);
8429 	os_free(data);
8430 
8431 	return ret;
8432 }
8433 
8434 
wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant * wpa_s)8435 static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
8436 {
8437 #ifdef CONFIG_P2P
8438 	struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
8439 		wpa_s->global->p2p_init_wpa_s : wpa_s;
8440 #endif /* CONFIG_P2P */
8441 
8442 	wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
8443 
8444 	if (wpas_abort_ongoing_scan(wpa_s) == 0)
8445 		wpa_s->ignore_post_flush_scan_res = 1;
8446 
8447 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
8448 		/*
8449 		 * Avoid possible auto connect re-connection on getting
8450 		 * disconnected due to state flush.
8451 		 */
8452 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
8453 	}
8454 
8455 #ifdef CONFIG_P2P
8456 	wpas_p2p_group_remove(p2p_wpa_s, "*");
8457 	wpas_p2p_cancel(p2p_wpa_s);
8458 	p2p_ctrl_flush(p2p_wpa_s);
8459 	wpas_p2p_service_flush(p2p_wpa_s);
8460 	p2p_wpa_s->global->p2p_disabled = 0;
8461 	p2p_wpa_s->global->p2p_per_sta_psk = 0;
8462 	p2p_wpa_s->conf->num_sec_device_types = 0;
8463 	p2p_wpa_s->p2p_disable_ip_addr_req = 0;
8464 	os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
8465 	p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
8466 	p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
8467 	p2p_wpa_s->global->pending_p2ps_group = 0;
8468 	p2p_wpa_s->global->pending_p2ps_group_freq = 0;
8469 #endif /* CONFIG_P2P */
8470 
8471 #ifdef CONFIG_WPS_TESTING
8472 	wps_version_number = 0x20;
8473 	wps_testing_stub_cred = 0;
8474 	wps_corrupt_pkhash = 0;
8475 	wps_force_auth_types_in_use = 0;
8476 	wps_force_encr_types_in_use = 0;
8477 #endif /* CONFIG_WPS_TESTING */
8478 #ifdef CONFIG_WPS
8479 	wpa_s->wps_fragment_size = 0;
8480 	wpas_wps_cancel(wpa_s);
8481 	wps_registrar_flush(wpa_s->wps->registrar);
8482 #endif /* CONFIG_WPS */
8483 	wpa_s->after_wps = 0;
8484 	wpa_s->known_wps_freq = 0;
8485 
8486 #ifdef CONFIG_DPP
8487 	wpas_dpp_deinit(wpa_s);
8488 	wpa_s->dpp_init_max_tries = 0;
8489 	wpa_s->dpp_init_retry_time = 0;
8490 	wpa_s->dpp_resp_wait_time = 0;
8491 	wpa_s->dpp_resp_max_tries = 0;
8492 	wpa_s->dpp_resp_retry_time = 0;
8493 #ifdef CONFIG_DPP2
8494 	wpas_dpp_chirp_stop(wpa_s);
8495 	wpa_s->dpp_pfs_fallback = 0;
8496 #endif /* CONFIG_DPP2 */
8497 #ifdef CONFIG_TESTING_OPTIONS
8498 	os_memset(dpp_pkex_own_mac_override, 0, ETH_ALEN);
8499 	os_memset(dpp_pkex_peer_mac_override, 0, ETH_ALEN);
8500 	dpp_pkex_ephemeral_key_override_len = 0;
8501 	dpp_protocol_key_override_len = 0;
8502 	dpp_nonce_override_len = 0;
8503 #ifdef CONFIG_DPP3
8504 	dpp_version_override = 3;
8505 #elif defined(CONFIG_DPP2)
8506 	dpp_version_override = 2;
8507 #else /* CONFIG_DPP2 */
8508 	dpp_version_override = 1;
8509 #endif /* CONFIG_DPP2 */
8510 #endif /* CONFIG_TESTING_OPTIONS */
8511 #endif /* CONFIG_DPP */
8512 
8513 #ifdef CONFIG_TDLS
8514 #ifdef CONFIG_TDLS_TESTING
8515 	tdls_testing = 0;
8516 #endif /* CONFIG_TDLS_TESTING */
8517 	wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
8518 	wpa_tdls_enable(wpa_s->wpa, 1);
8519 #endif /* CONFIG_TDLS */
8520 
8521 	eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
8522 	wpa_supplicant_stop_countermeasures(wpa_s, NULL);
8523 	wpa_s->last_michael_mic_error.sec = 0;
8524 
8525 	wpa_s->no_keep_alive = 0;
8526 	wpa_s->own_disconnect_req = 0;
8527 	wpa_s->own_reconnect_req = 0;
8528 	wpa_s->deny_ptk0_rekey = 0;
8529 
8530 	os_free(wpa_s->disallow_aps_bssid);
8531 	wpa_s->disallow_aps_bssid = NULL;
8532 	wpa_s->disallow_aps_bssid_count = 0;
8533 	os_free(wpa_s->disallow_aps_ssid);
8534 	wpa_s->disallow_aps_ssid = NULL;
8535 	wpa_s->disallow_aps_ssid_count = 0;
8536 
8537 	wpa_s->set_sta_uapsd = 0;
8538 	wpa_s->sta_uapsd = 0;
8539 
8540 	wpa_s->consecutive_conn_failures = 0;
8541 
8542 	wpa_drv_radio_disable(wpa_s, 0);
8543 	wpa_bssid_ignore_clear(wpa_s);
8544 	wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
8545 	wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
8546 	wpa_config_flush_blobs(wpa_s->conf);
8547 	wpa_s->conf->auto_interworking = 0;
8548 	wpa_s->conf->okc = 0;
8549 
8550 	ptksa_cache_flush(wpa_s->ptksa, NULL, WPA_CIPHER_NONE);
8551 	wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
8552 	rsn_preauth_deinit(wpa_s->wpa);
8553 
8554 	wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
8555 	wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
8556 	wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
8557 	eapol_sm_notify_logoff(wpa_s->eapol, false);
8558 
8559 	radio_remove_works(wpa_s, NULL, 1);
8560 	wpa_s->ext_work_in_progress = 0;
8561 
8562 	wpa_s->next_ssid = NULL;
8563 
8564 #ifdef CONFIG_INTERWORKING
8565 #ifdef CONFIG_HS20
8566 	hs20_cancel_fetch_osu(wpa_s);
8567 	hs20_del_icon(wpa_s, NULL, NULL);
8568 #endif /* CONFIG_HS20 */
8569 #endif /* CONFIG_INTERWORKING */
8570 
8571 	wpa_s->ext_mgmt_frame_handling = 0;
8572 	wpa_s->ext_eapol_frame_io = 0;
8573 #ifdef CONFIG_TESTING_OPTIONS
8574 	wpa_s->extra_roc_dur = 0;
8575 	wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
8576 	wpa_s->p2p_go_csa_on_inv = 0;
8577 	wpa_s->ignore_auth_resp = 0;
8578 	wpa_s->ignore_assoc_disallow = 0;
8579 	wpa_s->disable_sa_query = 0;
8580 	wpa_s->testing_resend_assoc = 0;
8581 	wpa_s->ignore_sae_h2e_only = 0;
8582 	wpa_s->ft_rsnxe_used = 0;
8583 	wpa_s->reject_btm_req_reason = 0;
8584 	wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
8585 	os_free(wpa_s->get_pref_freq_list_override);
8586 	wpa_s->get_pref_freq_list_override = NULL;
8587 	wpabuf_free(wpa_s->sae_commit_override);
8588 	wpa_s->sae_commit_override = NULL;
8589 	os_free(wpa_s->extra_sae_rejected_groups);
8590 	wpa_s->extra_sae_rejected_groups = NULL;
8591 	wpabuf_free(wpa_s->rsne_override_eapol);
8592 	wpa_s->rsne_override_eapol = NULL;
8593 	wpabuf_free(wpa_s->rsnxe_override_assoc);
8594 	wpa_s->rsnxe_override_assoc = NULL;
8595 	wpabuf_free(wpa_s->rsnxe_override_eapol);
8596 	wpa_s->rsnxe_override_eapol = NULL;
8597 	wpas_clear_driver_signal_override(wpa_s);
8598 	wpa_s->disable_scs_support = 0;
8599 	wpa_s->disable_mscs_support = 0;
8600 	wpa_s->enable_dscp_policy_capa = 0;
8601 	wpa_s->oci_freq_override_eapol = 0;
8602 	wpa_s->oci_freq_override_saquery_req = 0;
8603 	wpa_s->oci_freq_override_saquery_resp = 0;
8604 	wpa_s->oci_freq_override_eapol_g2 = 0;
8605 	wpa_s->oci_freq_override_ft_assoc = 0;
8606 	wpa_s->oci_freq_override_fils_assoc = 0;
8607 	wpa_s->oci_freq_override_wnm_sleep = 0;
8608 #ifdef CONFIG_DPP
8609 	os_free(wpa_s->dpp_config_obj_override);
8610 	wpa_s->dpp_config_obj_override = NULL;
8611 	os_free(wpa_s->dpp_discovery_override);
8612 	wpa_s->dpp_discovery_override = NULL;
8613 	os_free(wpa_s->dpp_groups_override);
8614 	wpa_s->dpp_groups_override = NULL;
8615 	dpp_test = DPP_TEST_DISABLED;
8616 #endif /* CONFIG_DPP */
8617 #endif /* CONFIG_TESTING_OPTIONS */
8618 
8619 	wpa_s->disconnected = 0;
8620 	os_free(wpa_s->next_scan_freqs);
8621 	wpa_s->next_scan_freqs = NULL;
8622 	os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
8623 	wpa_s->next_scan_bssid_wildcard_ssid = 0;
8624 	os_free(wpa_s->select_network_scan_freqs);
8625 	wpa_s->select_network_scan_freqs = NULL;
8626 	os_memset(&wpa_s->robust_av, 0, sizeof(struct robust_av_data));
8627 
8628 	wpa_bss_flush(wpa_s);
8629 	if (!dl_list_empty(&wpa_s->bss)) {
8630 		wpa_printf(MSG_DEBUG,
8631 			   "BSS table not empty after flush: %u entries, current_bss=%p bssid="
8632 			   MACSTR " pending_bssid=" MACSTR,
8633 			   dl_list_len(&wpa_s->bss), wpa_s->current_bss,
8634 			   MAC2STR(wpa_s->bssid),
8635 			   MAC2STR(wpa_s->pending_bssid));
8636 	}
8637 
8638 	eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
8639 	wpa_s->wnmsleep_used = 0;
8640 
8641 #ifdef CONFIG_SME
8642 	wpa_s->sme.last_unprot_disconnect.sec = 0;
8643 	wpa_s->sme.auth_alg = 0;
8644 #endif /* CONFIG_SME */
8645 
8646 	wpabuf_free(wpa_s->ric_ies);
8647 	wpa_s->ric_ies = NULL;
8648 
8649 	wpa_supplicant_update_channel_list(wpa_s, NULL);
8650 
8651 	free_bss_tmp_disallowed(wpa_s);
8652 
8653 	os_memset(&wpa_s->robust_av, 0, sizeof(struct robust_av_data));
8654 
8655 #ifdef CONFIG_PASN
8656 	wpas_pasn_auth_stop(wpa_s);
8657 #endif /* CONFIG_PASN */
8658 
8659 	if (wpa_s->mac_addr_changed && wpa_s->conf->mac_addr == 0)
8660 		wpas_restore_permanent_mac_addr(wpa_s);
8661 }
8662 
8663 
wpas_ctrl_radio_work_show(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8664 static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
8665 				     char *buf, size_t buflen)
8666 {
8667 	struct wpa_radio_work *work;
8668 	char *pos, *end;
8669 	struct os_reltime now, diff;
8670 
8671 	pos = buf;
8672 	end = buf + buflen;
8673 
8674 	os_get_reltime(&now);
8675 
8676 	dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
8677 	{
8678 		int ret;
8679 
8680 		os_reltime_sub(&now, &work->time, &diff);
8681 		ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
8682 				  work->type, work->wpa_s->ifname, work->freq,
8683 				  work->started, diff.sec, diff.usec);
8684 		if (os_snprintf_error(end - pos, ret))
8685 			break;
8686 		pos += ret;
8687 	}
8688 
8689 	return pos - buf;
8690 }
8691 
8692 
wpas_ctrl_radio_work_timeout(void * eloop_ctx,void * timeout_ctx)8693 static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
8694 {
8695 	struct wpa_radio_work *work = eloop_ctx;
8696 	struct wpa_external_work *ework = work->ctx;
8697 
8698 	wpa_dbg(work->wpa_s, MSG_DEBUG,
8699 		"Timing out external radio work %u (%s)",
8700 		ework->id, work->type);
8701 	wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
8702 	work->wpa_s->ext_work_in_progress = 0;
8703 	radio_work_done(work);
8704 	os_free(ework);
8705 }
8706 
8707 
wpas_ctrl_radio_work_cb(struct wpa_radio_work * work,int deinit)8708 static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
8709 {
8710 	struct wpa_external_work *ework = work->ctx;
8711 
8712 	if (deinit) {
8713 		if (work->started)
8714 			eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
8715 					     work, NULL);
8716 
8717 		/*
8718 		 * work->type points to a buffer in ework, so need to replace
8719 		 * that here with a fixed string to avoid use of freed memory
8720 		 * in debug prints.
8721 		 */
8722 		work->type = "freed-ext-work";
8723 		work->ctx = NULL;
8724 		os_free(ework);
8725 		return;
8726 	}
8727 
8728 	wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
8729 		ework->id, ework->type);
8730 	wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
8731 	work->wpa_s->ext_work_in_progress = 1;
8732 	if (!ework->timeout)
8733 		ework->timeout = 10;
8734 	eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
8735 			       work, NULL);
8736 }
8737 
8738 
wpas_ctrl_radio_work_add(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8739 static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
8740 				    char *buf, size_t buflen)
8741 {
8742 	struct wpa_external_work *ework;
8743 	char *pos, *pos2;
8744 	size_t type_len;
8745 	int ret;
8746 	unsigned int freq = 0;
8747 
8748 	/* format: <name> [freq=<MHz>] [timeout=<seconds>] */
8749 
8750 	ework = os_zalloc(sizeof(*ework));
8751 	if (ework == NULL)
8752 		return -1;
8753 
8754 	pos = os_strchr(cmd, ' ');
8755 	if (pos) {
8756 		type_len = pos - cmd;
8757 		pos++;
8758 
8759 		pos2 = os_strstr(pos, "freq=");
8760 		if (pos2)
8761 			freq = atoi(pos2 + 5);
8762 
8763 		pos2 = os_strstr(pos, "timeout=");
8764 		if (pos2)
8765 			ework->timeout = atoi(pos2 + 8);
8766 	} else {
8767 		type_len = os_strlen(cmd);
8768 	}
8769 	if (4 + type_len >= sizeof(ework->type))
8770 		type_len = sizeof(ework->type) - 4 - 1;
8771 	os_strlcpy(ework->type, "ext:", sizeof(ework->type));
8772 	os_memcpy(ework->type + 4, cmd, type_len);
8773 	ework->type[4 + type_len] = '\0';
8774 
8775 	wpa_s->ext_work_id++;
8776 	if (wpa_s->ext_work_id == 0)
8777 		wpa_s->ext_work_id++;
8778 	ework->id = wpa_s->ext_work_id;
8779 
8780 	if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
8781 			   ework) < 0) {
8782 		os_free(ework);
8783 		return -1;
8784 	}
8785 
8786 	ret = os_snprintf(buf, buflen, "%u", ework->id);
8787 	if (os_snprintf_error(buflen, ret))
8788 		return -1;
8789 	return ret;
8790 }
8791 
8792 
wpas_ctrl_radio_work_done(struct wpa_supplicant * wpa_s,char * cmd)8793 static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
8794 {
8795 	struct wpa_radio_work *work;
8796 	unsigned int id = atoi(cmd);
8797 
8798 	dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
8799 	{
8800 		struct wpa_external_work *ework;
8801 
8802 		if (os_strncmp(work->type, "ext:", 4) != 0)
8803 			continue;
8804 		ework = work->ctx;
8805 		if (id && ework->id != id)
8806 			continue;
8807 		wpa_dbg(wpa_s, MSG_DEBUG,
8808 			"Completed external radio work %u (%s)",
8809 			ework->id, ework->type);
8810 		eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
8811 		wpa_s->ext_work_in_progress = 0;
8812 		radio_work_done(work);
8813 		os_free(ework);
8814 		return 3; /* "OK\n" */
8815 	}
8816 
8817 	return -1;
8818 }
8819 
8820 
wpas_ctrl_radio_work(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8821 static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
8822 				char *buf, size_t buflen)
8823 {
8824 	if (os_strcmp(cmd, "show") == 0)
8825 		return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
8826 	if (os_strncmp(cmd, "add ", 4) == 0)
8827 		return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
8828 	if (os_strncmp(cmd, "done ", 5) == 0)
8829 		return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
8830 	return -1;
8831 }
8832 
8833 
wpas_ctrl_radio_work_flush(struct wpa_supplicant * wpa_s)8834 void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
8835 {
8836 	struct wpa_radio_work *work, *tmp;
8837 
8838 	if (!wpa_s || !wpa_s->radio)
8839 		return;
8840 
8841 	dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
8842 			      struct wpa_radio_work, list) {
8843 		struct wpa_external_work *ework;
8844 
8845 		if (os_strncmp(work->type, "ext:", 4) != 0)
8846 			continue;
8847 		ework = work->ctx;
8848 		wpa_dbg(wpa_s, MSG_DEBUG,
8849 			"Flushing%s external radio work %u (%s)",
8850 			work->started ? " started" : "", ework->id,
8851 			ework->type);
8852 		if (work->started)
8853 			eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
8854 					     work, NULL);
8855 		radio_work_done(work);
8856 		os_free(ework);
8857 	}
8858 }
8859 
8860 
wpas_ctrl_eapol_response(void * eloop_ctx,void * timeout_ctx)8861 static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
8862 {
8863 	struct wpa_supplicant *wpa_s = eloop_ctx;
8864 	eapol_sm_notify_ctrl_response(wpa_s->eapol);
8865 }
8866 
8867 
scan_id_list_parse(struct wpa_supplicant * wpa_s,const char * value,unsigned int * scan_id_count,int scan_id[])8868 static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
8869 			      unsigned int *scan_id_count, int scan_id[])
8870 {
8871 	const char *pos = value;
8872 
8873 	while (pos) {
8874 		if (*pos == ' ' || *pos == '\0')
8875 			break;
8876 		if (*scan_id_count == MAX_SCAN_ID)
8877 			return -1;
8878 		scan_id[(*scan_id_count)++] = atoi(pos);
8879 		pos = os_strchr(pos, ',');
8880 		if (pos)
8881 			pos++;
8882 	}
8883 
8884 	return 0;
8885 }
8886 
8887 
wpas_ctrl_scan(struct wpa_supplicant * wpa_s,char * params,char * reply,int reply_size,int * reply_len)8888 static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
8889 			   char *reply, int reply_size, int *reply_len)
8890 {
8891 	char *pos;
8892 	unsigned int manual_scan_passive = 0;
8893 	unsigned int manual_scan_use_id = 0;
8894 	unsigned int manual_scan_only_new = 0;
8895 	unsigned int scan_only = 0;
8896 	unsigned int scan_id_count = 0;
8897 	int scan_id[MAX_SCAN_ID];
8898 	void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
8899 				 struct wpa_scan_results *scan_res);
8900 	int *manual_scan_freqs = NULL;
8901 	struct wpa_ssid_value *ssid = NULL, *ns;
8902 	unsigned int ssid_count = 0;
8903 
8904 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
8905 		*reply_len = -1;
8906 		return;
8907 	}
8908 
8909 	if (radio_work_pending(wpa_s, "scan")) {
8910 		wpa_printf(MSG_DEBUG,
8911 			   "Pending scan scheduled - reject new request");
8912 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
8913 		return;
8914 	}
8915 
8916 #ifdef CONFIG_INTERWORKING
8917 	if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
8918 		wpa_printf(MSG_DEBUG,
8919 			   "Interworking select in progress - reject new scan");
8920 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
8921 		return;
8922 	}
8923 #endif /* CONFIG_INTERWORKING */
8924 
8925 	if (params) {
8926 		if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
8927 			scan_only = 1;
8928 
8929 		pos = os_strstr(params, "freq=");
8930 		if (pos) {
8931 			manual_scan_freqs = freq_range_to_channel_list(wpa_s,
8932 								       pos + 5);
8933 			if (manual_scan_freqs == NULL) {
8934 				*reply_len = -1;
8935 				goto done;
8936 			}
8937 		}
8938 
8939 		pos = os_strstr(params, "passive=");
8940 		if (pos)
8941 			manual_scan_passive = !!atoi(pos + 8);
8942 
8943 		pos = os_strstr(params, "use_id=");
8944 		if (pos)
8945 			manual_scan_use_id = atoi(pos + 7);
8946 
8947 		pos = os_strstr(params, "only_new=1");
8948 		if (pos)
8949 			manual_scan_only_new = 1;
8950 
8951 		pos = os_strstr(params, "scan_id=");
8952 		if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
8953 					      scan_id) < 0) {
8954 			*reply_len = -1;
8955 			goto done;
8956 		}
8957 
8958 		pos = os_strstr(params, "bssid=");
8959 		if (pos) {
8960 			u8 bssid[ETH_ALEN];
8961 
8962 			pos += 6;
8963 			if (hwaddr_aton(pos, bssid)) {
8964 				wpa_printf(MSG_ERROR, "Invalid BSSID %s", pos);
8965 				*reply_len = -1;
8966 				goto done;
8967 			}
8968 			os_memcpy(wpa_s->next_scan_bssid, bssid, ETH_ALEN);
8969 
8970 			wpa_s->next_scan_bssid_wildcard_ssid =
8971 				os_strstr(params, "wildcard_ssid=1") != NULL;
8972 		}
8973 
8974 		pos = params;
8975 		while (pos && *pos != '\0') {
8976 			if (os_strncmp(pos, "ssid ", 5) == 0) {
8977 				char *end;
8978 
8979 				pos += 5;
8980 				end = pos;
8981 				while (*end) {
8982 					if (*end == '\0' || *end == ' ')
8983 						break;
8984 					end++;
8985 				}
8986 
8987 				ns = os_realloc_array(
8988 					ssid, ssid_count + 1,
8989 					sizeof(struct wpa_ssid_value));
8990 				if (ns == NULL) {
8991 					*reply_len = -1;
8992 					goto done;
8993 				}
8994 				ssid = ns;
8995 
8996 				if ((end - pos) & 0x01 ||
8997 				    end - pos > 2 * SSID_MAX_LEN ||
8998 				    hexstr2bin(pos, ssid[ssid_count].ssid,
8999 					       (end - pos) / 2) < 0) {
9000 					wpa_printf(MSG_DEBUG,
9001 						   "Invalid SSID value '%s'",
9002 						   pos);
9003 					*reply_len = -1;
9004 					goto done;
9005 				}
9006 				ssid[ssid_count].ssid_len = (end - pos) / 2;
9007 				wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
9008 						  ssid[ssid_count].ssid,
9009 						  ssid[ssid_count].ssid_len);
9010 				ssid_count++;
9011 				pos = end;
9012 			}
9013 
9014 			pos = os_strchr(pos, ' ');
9015 			if (pos)
9016 				pos++;
9017 		}
9018 	}
9019 
9020 	wpa_s->num_ssids_from_scan_req = ssid_count;
9021 	os_free(wpa_s->ssids_from_scan_req);
9022 	if (ssid_count) {
9023 		wpa_s->ssids_from_scan_req = ssid;
9024 		ssid = NULL;
9025 	} else {
9026 		wpa_s->ssids_from_scan_req = NULL;
9027 	}
9028 
9029 	if (scan_only)
9030 		scan_res_handler = scan_only_handler;
9031 	else if (wpa_s->scan_res_handler == scan_only_handler)
9032 		scan_res_handler = NULL;
9033 	else
9034 		scan_res_handler = wpa_s->scan_res_handler;
9035 
9036 	if (!wpa_s->sched_scanning && !wpa_s->scanning &&
9037 	    ((wpa_s->wpa_state <= WPA_SCANNING) ||
9038 	     (wpa_s->wpa_state == WPA_COMPLETED))) {
9039 		wpa_s->manual_scan_passive = manual_scan_passive;
9040 		wpa_s->manual_scan_use_id = manual_scan_use_id;
9041 		wpa_s->manual_scan_only_new = manual_scan_only_new;
9042 		wpa_s->scan_id_count = scan_id_count;
9043 		os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
9044 		wpa_s->scan_res_handler = scan_res_handler;
9045 		os_free(wpa_s->manual_scan_freqs);
9046 		wpa_s->manual_scan_freqs = manual_scan_freqs;
9047 		manual_scan_freqs = NULL;
9048 
9049 		wpa_s->normal_scans = 0;
9050 		wpa_s->scan_req = MANUAL_SCAN_REQ;
9051 		wpa_s->after_wps = 0;
9052 		wpa_s->known_wps_freq = 0;
9053 		wpa_supplicant_req_scan(wpa_s, 0, 0);
9054 		if (wpa_s->manual_scan_use_id) {
9055 			wpa_s->manual_scan_id++;
9056 			wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
9057 				wpa_s->manual_scan_id);
9058 			*reply_len = os_snprintf(reply, reply_size, "%u\n",
9059 						 wpa_s->manual_scan_id);
9060 		}
9061 	} else if (wpa_s->sched_scanning) {
9062 		wpa_s->manual_scan_passive = manual_scan_passive;
9063 		wpa_s->manual_scan_use_id = manual_scan_use_id;
9064 		wpa_s->manual_scan_only_new = manual_scan_only_new;
9065 		wpa_s->scan_id_count = scan_id_count;
9066 		os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
9067 		wpa_s->scan_res_handler = scan_res_handler;
9068 		os_free(wpa_s->manual_scan_freqs);
9069 		wpa_s->manual_scan_freqs = manual_scan_freqs;
9070 		manual_scan_freqs = NULL;
9071 
9072 		wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
9073 		wpa_supplicant_cancel_sched_scan(wpa_s);
9074 		wpa_s->scan_req = MANUAL_SCAN_REQ;
9075 		wpa_supplicant_req_scan(wpa_s, 0, 0);
9076 		if (wpa_s->manual_scan_use_id) {
9077 			wpa_s->manual_scan_id++;
9078 			*reply_len = os_snprintf(reply, reply_size, "%u\n",
9079 						 wpa_s->manual_scan_id);
9080 			wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
9081 				wpa_s->manual_scan_id);
9082 		}
9083 	} else {
9084 		wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
9085 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
9086 	}
9087 
9088 done:
9089 	os_free(manual_scan_freqs);
9090 	os_free(ssid);
9091 }
9092 
9093 
9094 #ifdef CONFIG_TESTING_OPTIONS
9095 
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)9096 static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
9097 				       unsigned int freq, const u8 *dst,
9098 				       const u8 *src, const u8 *bssid,
9099 				       const u8 *data, size_t data_len,
9100 				       enum offchannel_send_action_result
9101 				       result)
9102 {
9103 	wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
9104 		" src=" MACSTR " bssid=" MACSTR " result=%s",
9105 		freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
9106 		result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
9107 		"SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
9108 			     "NO_ACK" : "FAILED"));
9109 }
9110 
9111 
wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant * wpa_s,char * cmd)9112 static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
9113 {
9114 	char *pos, *param;
9115 	size_t len;
9116 	u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
9117 	int res, used;
9118 	int freq = 0, no_cck = 0, wait_time = 0;
9119 
9120 	/* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
9121 	 *    <action=Action frame payload> */
9122 
9123 	wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
9124 
9125 	pos = cmd;
9126 	used = hwaddr_aton2(pos, da);
9127 	if (used < 0)
9128 		return -1;
9129 	pos += used;
9130 	while (*pos == ' ')
9131 		pos++;
9132 	used = hwaddr_aton2(pos, bssid);
9133 	if (used < 0)
9134 		return -1;
9135 	pos += used;
9136 
9137 	param = os_strstr(pos, " freq=");
9138 	if (param) {
9139 		param += 6;
9140 		freq = atoi(param);
9141 	}
9142 
9143 	param = os_strstr(pos, " no_cck=");
9144 	if (param) {
9145 		param += 8;
9146 		no_cck = atoi(param);
9147 	}
9148 
9149 	param = os_strstr(pos, " wait_time=");
9150 	if (param) {
9151 		param += 11;
9152 		wait_time = atoi(param);
9153 	}
9154 
9155 	param = os_strstr(pos, " action=");
9156 	if (param == NULL)
9157 		return -1;
9158 	param += 8;
9159 
9160 	len = os_strlen(param);
9161 	if (len & 1)
9162 		return -1;
9163 	len /= 2;
9164 
9165 	buf = os_malloc(len);
9166 	if (buf == NULL)
9167 		return -1;
9168 
9169 	if (hexstr2bin(param, buf, len) < 0) {
9170 		os_free(buf);
9171 		return -1;
9172 	}
9173 
9174 	res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
9175 				     buf, len, wait_time,
9176 				     wpas_ctrl_iface_mgmt_tx_cb, no_cck);
9177 	os_free(buf);
9178 	return res;
9179 }
9180 
9181 
wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant * wpa_s)9182 static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
9183 {
9184 	wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
9185 	offchannel_send_action_done(wpa_s);
9186 }
9187 
9188 
wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant * wpa_s,char * cmd)9189 static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
9190 					   char *cmd)
9191 {
9192 	char *pos, *param;
9193 	size_t len;
9194 	u8 *buf;
9195 	int freq = 0, datarate = 0, ssi_signal = 0;
9196 	union wpa_event_data event;
9197 
9198 	if (!wpa_s->ext_mgmt_frame_handling)
9199 		return -1;
9200 
9201 	/* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
9202 
9203 	wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
9204 
9205 	pos = cmd;
9206 	param = os_strstr(pos, "freq=");
9207 	if (param) {
9208 		param += 5;
9209 		freq = atoi(param);
9210 	}
9211 
9212 	param = os_strstr(pos, " datarate=");
9213 	if (param) {
9214 		param += 10;
9215 		datarate = atoi(param);
9216 	}
9217 
9218 	param = os_strstr(pos, " ssi_signal=");
9219 	if (param) {
9220 		param += 12;
9221 		ssi_signal = atoi(param);
9222 	}
9223 
9224 	param = os_strstr(pos, " frame=");
9225 	if (param == NULL)
9226 		return -1;
9227 	param += 7;
9228 
9229 	len = os_strlen(param);
9230 	if (len & 1)
9231 		return -1;
9232 	len /= 2;
9233 
9234 	buf = os_malloc(len);
9235 	if (buf == NULL)
9236 		return -1;
9237 
9238 	if (hexstr2bin(param, buf, len) < 0) {
9239 		os_free(buf);
9240 		return -1;
9241 	}
9242 
9243 	os_memset(&event, 0, sizeof(event));
9244 	event.rx_mgmt.freq = freq;
9245 	event.rx_mgmt.frame = buf;
9246 	event.rx_mgmt.frame_len = len;
9247 	event.rx_mgmt.ssi_signal = ssi_signal;
9248 	event.rx_mgmt.datarate = datarate;
9249 	wpa_s->ext_mgmt_frame_handling = 0;
9250 	wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
9251 	wpa_s->ext_mgmt_frame_handling = 1;
9252 
9253 	os_free(buf);
9254 
9255 	return 0;
9256 }
9257 
9258 
wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant * wpa_s,char * param)9259 static int wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant *wpa_s,
9260 					   char *param)
9261 {
9262 	struct wpa_scan_res *res;
9263 	struct os_reltime now;
9264 	char *pos, *end;
9265 	int ret = -1;
9266 
9267 	if (!param)
9268 		return -1;
9269 
9270 	if (os_strcmp(param, "START") == 0) {
9271 		wpa_bss_update_start(wpa_s);
9272 		return 0;
9273 	}
9274 
9275 	if (os_strcmp(param, "END") == 0) {
9276 		wpa_bss_update_end(wpa_s, NULL, 1);
9277 		return 0;
9278 	}
9279 
9280 	if (os_strncmp(param, "BSS ", 4) != 0)
9281 		return -1;
9282 	param += 3;
9283 
9284 	res = os_zalloc(sizeof(*res) + os_strlen(param) / 2);
9285 	if (!res)
9286 		return -1;
9287 
9288 	pos = os_strstr(param, " flags=");
9289 	if (pos)
9290 		res->flags = strtol(pos + 7, NULL, 16);
9291 
9292 	pos = os_strstr(param, " bssid=");
9293 	if (pos && hwaddr_aton(pos + 7, res->bssid))
9294 		goto fail;
9295 
9296 	pos = os_strstr(param, " freq=");
9297 	if (pos)
9298 		res->freq = atoi(pos + 6);
9299 
9300 	pos = os_strstr(param, " beacon_int=");
9301 	if (pos)
9302 		res->beacon_int = atoi(pos + 12);
9303 
9304 	pos = os_strstr(param, " caps=");
9305 	if (pos)
9306 		res->caps = strtol(pos + 6, NULL, 16);
9307 
9308 	pos = os_strstr(param, " qual=");
9309 	if (pos)
9310 		res->qual = atoi(pos + 6);
9311 
9312 	pos = os_strstr(param, " noise=");
9313 	if (pos)
9314 		res->noise = atoi(pos + 7);
9315 
9316 	pos = os_strstr(param, " level=");
9317 	if (pos)
9318 		res->level = atoi(pos + 7);
9319 
9320 	pos = os_strstr(param, " tsf=");
9321 	if (pos)
9322 		res->tsf = strtoll(pos + 5, NULL, 16);
9323 
9324 	pos = os_strstr(param, " age=");
9325 	if (pos)
9326 		res->age = atoi(pos + 5);
9327 
9328 	pos = os_strstr(param, " est_throughput=");
9329 	if (pos)
9330 		res->est_throughput = atoi(pos + 16);
9331 
9332 	pos = os_strstr(param, " snr=");
9333 	if (pos)
9334 		res->snr = atoi(pos + 5);
9335 
9336 	pos = os_strstr(param, " parent_tsf=");
9337 	if (pos)
9338 		res->parent_tsf = strtoll(pos + 7, NULL, 16);
9339 
9340 	pos = os_strstr(param, " tsf_bssid=");
9341 	if (pos && hwaddr_aton(pos + 11, res->tsf_bssid))
9342 		goto fail;
9343 
9344 	pos = os_strstr(param, " ie=");
9345 	if (pos) {
9346 		pos += 4;
9347 		end = os_strchr(pos, ' ');
9348 		if (!end)
9349 			end = pos + os_strlen(pos);
9350 		res->ie_len = (end - pos) / 2;
9351 		if (hexstr2bin(pos, (u8 *) (res + 1), res->ie_len))
9352 			goto fail;
9353 	}
9354 
9355 	pos = os_strstr(param, " beacon_ie=");
9356 	if (pos) {
9357 		pos += 11;
9358 		end = os_strchr(pos, ' ');
9359 		if (!end)
9360 			end = pos + os_strlen(pos);
9361 		res->beacon_ie_len = (end - pos) / 2;
9362 		if (hexstr2bin(pos, ((u8 *) (res + 1)) + res->ie_len,
9363 			       res->beacon_ie_len))
9364 			goto fail;
9365 	}
9366 
9367 	os_get_reltime(&now);
9368 	wpa_bss_update_scan_res(wpa_s, res, &now);
9369 	ret = 0;
9370 fail:
9371 	os_free(res);
9372 
9373 	return ret;
9374 }
9375 
9376 
wpas_ctrl_iface_driver_event_assoc(struct wpa_supplicant * wpa_s,char * param)9377 static int wpas_ctrl_iface_driver_event_assoc(struct wpa_supplicant *wpa_s,
9378 					      char *param)
9379 {
9380 	union wpa_event_data event;
9381 	struct assoc_info *ai;
9382 	char *ctx = NULL;
9383 	int ret = -1;
9384 	struct wpabuf *req_ies = NULL;
9385 	struct wpabuf *resp_ies = NULL;
9386 	struct wpabuf *resp_frame = NULL;
9387 	struct wpabuf *beacon_ies = NULL;
9388 	struct wpabuf *key_replay_ctr = NULL;
9389 	struct wpabuf *ptk_kck = NULL;
9390 	struct wpabuf *ptk_kek = NULL;
9391 	struct wpabuf *fils_pmk = NULL;
9392 	char *str, *pos;
9393 	u8 addr[ETH_ALEN];
9394 	u8 fils_pmkid[PMKID_LEN];
9395 
9396 	os_memset(&event, 0, sizeof(event));
9397 	ai = &event.assoc_info;
9398 
9399 	while ((str = str_token(param, " ", &ctx))) {
9400 		pos = os_strchr(str, '=');
9401 		if (!pos)
9402 			goto fail;
9403 		*pos++ = '\0';
9404 
9405 		if (os_strcmp(str, "reassoc") == 0) {
9406 			ai->reassoc = atoi(pos);
9407 		} else if (os_strcmp(str, "req_ies") == 0) {
9408 			wpabuf_free(req_ies);
9409 			req_ies = wpabuf_parse_bin(pos);
9410 			if (!req_ies)
9411 				goto fail;
9412 			ai->req_ies = wpabuf_head(req_ies);
9413 			ai->req_ies_len = wpabuf_len(req_ies);
9414 		} else if (os_strcmp(str, "resp_ies") == 0) {
9415 			wpabuf_free(resp_ies);
9416 			resp_ies = wpabuf_parse_bin(pos);
9417 			if (!resp_ies)
9418 				goto fail;
9419 			ai->resp_ies = wpabuf_head(resp_ies);
9420 			ai->resp_ies_len = wpabuf_len(resp_ies);
9421 		} else if (os_strcmp(str, "resp_frame") == 0) {
9422 			wpabuf_free(resp_frame);
9423 			resp_frame = wpabuf_parse_bin(pos);
9424 			if (!resp_frame)
9425 				goto fail;
9426 			ai->resp_frame = wpabuf_head(resp_frame);
9427 			ai->resp_frame_len = wpabuf_len(resp_frame);
9428 		} else if (os_strcmp(str, "beacon_ies") == 0) {
9429 			wpabuf_free(beacon_ies);
9430 			beacon_ies = wpabuf_parse_bin(pos);
9431 			if (!beacon_ies)
9432 				goto fail;
9433 			ai->beacon_ies = wpabuf_head(beacon_ies);
9434 			ai->beacon_ies_len = wpabuf_len(beacon_ies);
9435 		} else if (os_strcmp(str, "freq") == 0) {
9436 			ai->freq = atoi(pos);
9437 		} else if (os_strcmp(str, "wmm::info_bitmap") == 0) {
9438 			ai->wmm_params.info_bitmap = atoi(pos);
9439 		} else if (os_strcmp(str, "wmm::uapsd_queues") == 0) {
9440 			ai->wmm_params.uapsd_queues = atoi(pos);
9441 		} else if (os_strcmp(str, "addr") == 0) {
9442 			if (hwaddr_aton(pos, addr))
9443 				goto fail;
9444 			ai->addr = addr;
9445 		} else if (os_strcmp(str, "authorized") == 0) {
9446 			ai->authorized = atoi(pos);
9447 		} else if (os_strcmp(str, "key_replay_ctr") == 0) {
9448 			wpabuf_free(key_replay_ctr);
9449 			key_replay_ctr = wpabuf_parse_bin(pos);
9450 			if (!key_replay_ctr)
9451 				goto fail;
9452 			ai->key_replay_ctr = wpabuf_head(key_replay_ctr);
9453 			ai->key_replay_ctr_len = wpabuf_len(key_replay_ctr);
9454 		} else if (os_strcmp(str, "ptk_kck") == 0) {
9455 			wpabuf_free(ptk_kck);
9456 			ptk_kck = wpabuf_parse_bin(pos);
9457 			if (!ptk_kck)
9458 				goto fail;
9459 			ai->ptk_kck = wpabuf_head(ptk_kck);
9460 			ai->ptk_kck_len = wpabuf_len(ptk_kck);
9461 		} else if (os_strcmp(str, "ptk_kek") == 0) {
9462 			wpabuf_free(ptk_kek);
9463 			ptk_kek = wpabuf_parse_bin(pos);
9464 			if (!ptk_kek)
9465 				goto fail;
9466 			ai->ptk_kek = wpabuf_head(ptk_kek);
9467 			ai->ptk_kek_len = wpabuf_len(ptk_kek);
9468 		} else if (os_strcmp(str, "subnet_status") == 0) {
9469 			ai->subnet_status = atoi(pos);
9470 		} else if (os_strcmp(str, "fils_erp_next_seq_num") == 0) {
9471 			ai->fils_erp_next_seq_num = atoi(pos);
9472 		} else if (os_strcmp(str, "fils_pmk") == 0) {
9473 			wpabuf_free(fils_pmk);
9474 			fils_pmk = wpabuf_parse_bin(pos);
9475 			if (!fils_pmk)
9476 				goto fail;
9477 			ai->fils_pmk = wpabuf_head(fils_pmk);
9478 			ai->fils_pmk_len = wpabuf_len(fils_pmk);
9479 		} else if (os_strcmp(str, "fils_pmkid") == 0) {
9480 			if (hexstr2bin(pos, fils_pmkid, PMKID_LEN) < 0)
9481 				goto fail;
9482 			ai->fils_pmkid = fils_pmkid;
9483 		} else {
9484 			goto fail;
9485 		}
9486 	}
9487 
9488 	wpa_supplicant_event(wpa_s, EVENT_ASSOC, &event);
9489 	ret = 0;
9490 fail:
9491 	wpabuf_free(req_ies);
9492 	wpabuf_free(resp_ies);
9493 	wpabuf_free(resp_frame);
9494 	wpabuf_free(beacon_ies);
9495 	wpabuf_free(key_replay_ctr);
9496 	wpabuf_free(ptk_kck);
9497 	wpabuf_free(ptk_kek);
9498 	wpabuf_free(fils_pmk);
9499 	return ret;
9500 }
9501 
9502 
wpas_ctrl_iface_driver_event(struct wpa_supplicant * wpa_s,char * cmd)9503 static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
9504 {
9505 	char *pos, *param;
9506 	union wpa_event_data event;
9507 	enum wpa_event_type ev;
9508 
9509 	/* <event name> [parameters..] */
9510 
9511 	wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
9512 
9513 	pos = cmd;
9514 	param = os_strchr(pos, ' ');
9515 	if (param)
9516 		*param++ = '\0';
9517 
9518 	os_memset(&event, 0, sizeof(event));
9519 
9520 	if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
9521 		ev = EVENT_INTERFACE_ENABLED;
9522 	} else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
9523 		ev = EVENT_INTERFACE_DISABLED;
9524 	} else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
9525 		ev = EVENT_AVOID_FREQUENCIES;
9526 		if (param == NULL)
9527 			param = "";
9528 		if (freq_range_list_parse(&event.freq_range, param) < 0)
9529 			return -1;
9530 		wpa_supplicant_event(wpa_s, ev, &event);
9531 		os_free(event.freq_range.range);
9532 		return 0;
9533 	} else if (os_strcmp(cmd, "SCAN_RES") == 0) {
9534 		return wpas_ctrl_iface_driver_scan_res(wpa_s, param);
9535 	} else if (os_strcmp(cmd, "ASSOC") == 0) {
9536 		return wpas_ctrl_iface_driver_event_assoc(wpa_s, param);
9537 	} else {
9538 		wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
9539 			cmd);
9540 		return -1;
9541 	}
9542 
9543 	wpa_supplicant_event(wpa_s, ev, &event);
9544 
9545 	return 0;
9546 }
9547 
9548 
wpas_ctrl_iface_eapol_rx(struct wpa_supplicant * wpa_s,char * cmd)9549 static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
9550 {
9551 	char *pos;
9552 	u8 src[ETH_ALEN], *buf;
9553 	int used;
9554 	size_t len;
9555 
9556 	wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
9557 
9558 	pos = cmd;
9559 	used = hwaddr_aton2(pos, src);
9560 	if (used < 0)
9561 		return -1;
9562 	pos += used;
9563 	while (*pos == ' ')
9564 		pos++;
9565 
9566 	len = os_strlen(pos);
9567 	if (len & 1)
9568 		return -1;
9569 	len /= 2;
9570 
9571 	buf = os_malloc(len);
9572 	if (buf == NULL)
9573 		return -1;
9574 
9575 	if (hexstr2bin(pos, buf, len) < 0) {
9576 		os_free(buf);
9577 		return -1;
9578 	}
9579 
9580 	wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
9581 	os_free(buf);
9582 
9583 	return 0;
9584 }
9585 
9586 
wpas_ctrl_iface_eapol_tx(struct wpa_supplicant * wpa_s,char * cmd)9587 static int wpas_ctrl_iface_eapol_tx(struct wpa_supplicant *wpa_s, char *cmd)
9588 {
9589 	char *pos;
9590 	u8 dst[ETH_ALEN], *buf;
9591 	int used, ret;
9592 	size_t len;
9593 	unsigned int prev;
9594 
9595 	wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
9596 
9597 	pos = cmd;
9598 	used = hwaddr_aton2(pos, dst);
9599 	if (used < 0)
9600 		return -1;
9601 	pos += used;
9602 	while (*pos == ' ')
9603 		pos++;
9604 
9605 	len = os_strlen(pos);
9606 	if (len & 1)
9607 		return -1;
9608 	len /= 2;
9609 
9610 	buf = os_malloc(len);
9611 	if (!buf || hexstr2bin(pos, buf, len) < 0) {
9612 		os_free(buf);
9613 		return -1;
9614 	}
9615 
9616 	prev = wpa_s->ext_eapol_frame_io;
9617 	wpa_s->ext_eapol_frame_io = 0;
9618 	ret = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, buf, len);
9619 	wpa_s->ext_eapol_frame_io = prev;
9620 	os_free(buf);
9621 
9622 	return ret;
9623 }
9624 
9625 
ipv4_hdr_checksum(const void * buf,size_t len)9626 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
9627 {
9628 	size_t i;
9629 	u32 sum = 0;
9630 	const u16 *pos = buf;
9631 
9632 	for (i = 0; i < len / 2; i++)
9633 		sum += *pos++;
9634 
9635 	while (sum >> 16)
9636 		sum = (sum & 0xffff) + (sum >> 16);
9637 
9638 	return sum ^ 0xffff;
9639 }
9640 
9641 
9642 #define HWSIM_PACKETLEN 1500
9643 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
9644 
wpas_data_test_rx(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)9645 static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
9646 			      size_t len)
9647 {
9648 	struct wpa_supplicant *wpa_s = ctx;
9649 	const struct ether_header *eth;
9650 	struct ip ip;
9651 	const u8 *pos;
9652 	unsigned int i;
9653 	char extra[30];
9654 
9655 	if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
9656 		wpa_printf(MSG_DEBUG,
9657 			   "test data: RX - ignore unexpected length %d",
9658 			   (int) len);
9659 		return;
9660 	}
9661 
9662 	eth = (const struct ether_header *) buf;
9663 	os_memcpy(&ip, eth + 1, sizeof(ip));
9664 	pos = &buf[sizeof(*eth) + sizeof(ip)];
9665 
9666 	if (ip.ip_hl != 5 || ip.ip_v != 4 || ntohs(ip.ip_len) > HWSIM_IP_LEN) {
9667 		wpa_printf(MSG_DEBUG,
9668 			   "test data: RX - ignore unexpected IP header");
9669 		return;
9670 	}
9671 
9672 	for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
9673 		if (*pos != (u8) i) {
9674 			wpa_printf(MSG_DEBUG,
9675 				   "test data: RX - ignore mismatching payload");
9676 			return;
9677 		}
9678 		pos++;
9679 	}
9680 	extra[0] = '\0';
9681 	if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
9682 		os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
9683 	wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
9684 		MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
9685 }
9686 
9687 
wpas_ctrl_iface_data_test_config(struct wpa_supplicant * wpa_s,char * cmd)9688 static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
9689 					    char *cmd)
9690 {
9691 	int enabled = atoi(cmd);
9692 	char *pos;
9693 	const char *ifname;
9694 
9695 	if (!enabled) {
9696 		if (wpa_s->l2_test) {
9697 			l2_packet_deinit(wpa_s->l2_test);
9698 			wpa_s->l2_test = NULL;
9699 			wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
9700 		}
9701 		return 0;
9702 	}
9703 
9704 	if (wpa_s->l2_test)
9705 		return 0;
9706 
9707 	pos = os_strstr(cmd, " ifname=");
9708 	if (pos)
9709 		ifname = pos + 8;
9710 	else
9711 		ifname = wpa_s->ifname;
9712 
9713 	wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
9714 					ETHERTYPE_IP, wpas_data_test_rx,
9715 					wpa_s, 1);
9716 	if (wpa_s->l2_test == NULL)
9717 		return -1;
9718 
9719 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
9720 
9721 	return 0;
9722 }
9723 
9724 
wpas_ctrl_iface_data_test_tx(struct wpa_supplicant * wpa_s,char * cmd)9725 static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
9726 {
9727 	u8 dst[ETH_ALEN], src[ETH_ALEN];
9728 	char *pos, *pos2;
9729 	int used;
9730 	long int val;
9731 	u8 tos;
9732 	u8 buf[2 + HWSIM_PACKETLEN];
9733 	struct ether_header *eth;
9734 	struct ip *ip;
9735 	u8 *dpos;
9736 	unsigned int i;
9737 	size_t send_len = HWSIM_IP_LEN;
9738 
9739 	if (wpa_s->l2_test == NULL)
9740 		return -1;
9741 
9742 	/* format: <dst> <src> <tos> [len=<length>] */
9743 
9744 	pos = cmd;
9745 	used = hwaddr_aton2(pos, dst);
9746 	if (used < 0)
9747 		return -1;
9748 	pos += used;
9749 	while (*pos == ' ')
9750 		pos++;
9751 	used = hwaddr_aton2(pos, src);
9752 	if (used < 0)
9753 		return -1;
9754 	pos += used;
9755 
9756 	val = strtol(pos, &pos2, 0);
9757 	if (val < 0 || val > 0xff)
9758 		return -1;
9759 	tos = val;
9760 
9761 	pos = os_strstr(pos2, " len=");
9762 	if (pos) {
9763 		i = atoi(pos + 5);
9764 		if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
9765 			return -1;
9766 		send_len = i;
9767 	}
9768 
9769 	eth = (struct ether_header *) &buf[2];
9770 	os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
9771 	os_memcpy(eth->ether_shost, src, ETH_ALEN);
9772 	eth->ether_type = htons(ETHERTYPE_IP);
9773 	ip = (struct ip *) (eth + 1);
9774 	os_memset(ip, 0, sizeof(*ip));
9775 	ip->ip_hl = 5;
9776 	ip->ip_v = 4;
9777 	ip->ip_ttl = 64;
9778 	ip->ip_tos = tos;
9779 	ip->ip_len = htons(send_len);
9780 	ip->ip_p = 1;
9781 	ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
9782 	ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
9783 	ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
9784 	dpos = (u8 *) (ip + 1);
9785 	for (i = 0; i < send_len - sizeof(*ip); i++)
9786 		*dpos++ = i;
9787 
9788 	if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
9789 			   sizeof(struct ether_header) + send_len) < 0)
9790 		return -1;
9791 
9792 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
9793 		" tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
9794 
9795 	return 0;
9796 }
9797 
9798 
wpas_ctrl_iface_data_test_frame(struct wpa_supplicant * wpa_s,char * cmd)9799 static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
9800 					   char *cmd)
9801 {
9802 	u8 *buf;
9803 	struct ether_header *eth;
9804 	struct l2_packet_data *l2 = NULL;
9805 	size_t len;
9806 	u16 ethertype;
9807 	int res = -1;
9808 
9809 	len = os_strlen(cmd);
9810 	if (len & 1 || len < ETH_HLEN * 2)
9811 		return -1;
9812 	len /= 2;
9813 
9814 	buf = os_malloc(len);
9815 	if (buf == NULL)
9816 		return -1;
9817 
9818 	if (hexstr2bin(cmd, buf, len) < 0)
9819 		goto done;
9820 
9821 	eth = (struct ether_header *) buf;
9822 	ethertype = ntohs(eth->ether_type);
9823 
9824 	l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
9825 			    wpas_data_test_rx, wpa_s, 1);
9826 	if (l2 == NULL)
9827 		goto done;
9828 
9829 	res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
9830 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
9831 done:
9832 	if (l2)
9833 		l2_packet_deinit(l2);
9834 	os_free(buf);
9835 
9836 	return res < 0 ? -1 : 0;
9837 }
9838 
9839 
wpas_ctrl_test_alloc_fail(struct wpa_supplicant * wpa_s,char * cmd)9840 static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
9841 {
9842 #ifdef WPA_TRACE_BFD
9843 	char *pos;
9844 
9845 	wpa_trace_fail_after = atoi(cmd);
9846 	pos = os_strchr(cmd, ':');
9847 	if (pos) {
9848 		pos++;
9849 		os_strlcpy(wpa_trace_fail_func, pos,
9850 			   sizeof(wpa_trace_fail_func));
9851 	} else {
9852 		wpa_trace_fail_after = 0;
9853 	}
9854 	return 0;
9855 #else /* WPA_TRACE_BFD */
9856 	return -1;
9857 #endif /* WPA_TRACE_BFD */
9858 }
9859 
9860 
wpas_ctrl_get_alloc_fail(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)9861 static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
9862 				    char *buf, size_t buflen)
9863 {
9864 #ifdef WPA_TRACE_BFD
9865 	return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
9866 			   wpa_trace_fail_func);
9867 #else /* WPA_TRACE_BFD */
9868 	return -1;
9869 #endif /* WPA_TRACE_BFD */
9870 }
9871 
9872 
wpas_ctrl_test_fail(struct wpa_supplicant * wpa_s,char * cmd)9873 static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd)
9874 {
9875 #ifdef WPA_TRACE_BFD
9876 	char *pos;
9877 
9878 	wpa_trace_test_fail_after = atoi(cmd);
9879 	pos = os_strchr(cmd, ':');
9880 	if (pos) {
9881 		pos++;
9882 		os_strlcpy(wpa_trace_test_fail_func, pos,
9883 			   sizeof(wpa_trace_test_fail_func));
9884 	} else {
9885 		wpa_trace_test_fail_after = 0;
9886 	}
9887 	return 0;
9888 #else /* WPA_TRACE_BFD */
9889 	return -1;
9890 #endif /* WPA_TRACE_BFD */
9891 }
9892 
9893 
wpas_ctrl_get_fail(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)9894 static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s,
9895 				    char *buf, size_t buflen)
9896 {
9897 #ifdef WPA_TRACE_BFD
9898 	return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
9899 			   wpa_trace_test_fail_func);
9900 #else /* WPA_TRACE_BFD */
9901 	return -1;
9902 #endif /* WPA_TRACE_BFD */
9903 }
9904 
9905 
wpas_ctrl_event_test_cb(void * eloop_ctx,void * timeout_ctx)9906 static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
9907 {
9908 	struct wpa_supplicant *wpa_s = eloop_ctx;
9909 	int i, count = (intptr_t) timeout_ctx;
9910 
9911 	wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
9912 		   count);
9913 	for (i = 0; i < count; i++) {
9914 		wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
9915 			     i + 1, count);
9916 	}
9917 }
9918 
9919 
wpas_ctrl_event_test(struct wpa_supplicant * wpa_s,const char * cmd)9920 static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
9921 {
9922 	int count;
9923 
9924 	count = atoi(cmd);
9925 	if (count <= 0)
9926 		return -1;
9927 
9928 	return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
9929 				      (void *) (intptr_t) count);
9930 }
9931 
9932 
wpas_ctrl_test_assoc_ie(struct wpa_supplicant * wpa_s,const char * cmd)9933 static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
9934 				   const char *cmd)
9935 {
9936 	struct wpabuf *buf;
9937 	size_t len;
9938 
9939 	len = os_strlen(cmd);
9940 	if (len & 1)
9941 		return -1;
9942 	len /= 2;
9943 
9944 	if (len == 0) {
9945 		buf = NULL;
9946 	} else {
9947 		buf = wpabuf_alloc(len);
9948 		if (buf == NULL)
9949 			return -1;
9950 
9951 		if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
9952 			wpabuf_free(buf);
9953 			return -1;
9954 		}
9955 	}
9956 
9957 	wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
9958 	return 0;
9959 }
9960 
9961 
wpas_ctrl_reset_pn(struct wpa_supplicant * wpa_s)9962 static int wpas_ctrl_reset_pn(struct wpa_supplicant *wpa_s)
9963 {
9964 	u8 zero[WPA_TK_MAX_LEN];
9965 
9966 	if (wpa_s->last_tk_alg == WPA_ALG_NONE)
9967 		return -1;
9968 
9969 	wpa_printf(MSG_INFO, "TESTING: Reset PN");
9970 	os_memset(zero, 0, sizeof(zero));
9971 
9972 	/* First, use a zero key to avoid any possible duplicate key avoidance
9973 	 * in the driver. */
9974 	if (wpa_drv_set_key(wpa_s, wpa_s->last_tk_alg, wpa_s->last_tk_addr,
9975 			    wpa_s->last_tk_key_idx, 1, zero, 6,
9976 			    zero, wpa_s->last_tk_len,
9977 			    KEY_FLAG_PAIRWISE_RX_TX) < 0)
9978 		return -1;
9979 
9980 	/* Set the previously configured key to reset its TSC/RSC */
9981 	return wpa_drv_set_key(wpa_s, wpa_s->last_tk_alg, wpa_s->last_tk_addr,
9982 			       wpa_s->last_tk_key_idx, 1, zero, 6,
9983 			       wpa_s->last_tk, wpa_s->last_tk_len,
9984 			       KEY_FLAG_PAIRWISE_RX_TX);
9985 }
9986 
9987 
wpas_ctrl_key_request(struct wpa_supplicant * wpa_s,const char * cmd)9988 static int wpas_ctrl_key_request(struct wpa_supplicant *wpa_s, const char *cmd)
9989 {
9990 	const char *pos = cmd;
9991 	int error, pairwise;
9992 
9993 	error = atoi(pos);
9994 	pos = os_strchr(pos, ' ');
9995 	if (!pos)
9996 		return -1;
9997 	pairwise = atoi(pos);
9998 	wpa_sm_key_request(wpa_s->wpa, error, pairwise);
9999 	return 0;
10000 }
10001 
10002 
wpas_ctrl_resend_assoc(struct wpa_supplicant * wpa_s)10003 static int wpas_ctrl_resend_assoc(struct wpa_supplicant *wpa_s)
10004 {
10005 #ifdef CONFIG_SME
10006 	struct wpa_driver_associate_params params;
10007 	int ret;
10008 
10009 	os_memset(&params, 0, sizeof(params));
10010 	params.bssid = wpa_s->bssid;
10011 	params.ssid = wpa_s->sme.ssid;
10012 	params.ssid_len = wpa_s->sme.ssid_len;
10013 	params.freq.freq = wpa_s->sme.freq;
10014 	if (wpa_s->last_assoc_req_wpa_ie) {
10015 		params.wpa_ie = wpabuf_head(wpa_s->last_assoc_req_wpa_ie);
10016 		params.wpa_ie_len = wpabuf_len(wpa_s->last_assoc_req_wpa_ie);
10017 	}
10018 	params.pairwise_suite = wpa_s->pairwise_cipher;
10019 	params.group_suite = wpa_s->group_cipher;
10020 	params.mgmt_group_suite = wpa_s->mgmt_group_cipher;
10021 	params.key_mgmt_suite = wpa_s->key_mgmt;
10022 	params.wpa_proto = wpa_s->wpa_proto;
10023 	params.mgmt_frame_protection = wpa_s->sme.mfp;
10024 	params.rrm_used = wpa_s->rrm.rrm_used;
10025 	if (wpa_s->sme.prev_bssid_set)
10026 		params.prev_bssid = wpa_s->sme.prev_bssid;
10027 	wpa_printf(MSG_INFO, "TESTING: Resend association request");
10028 	ret = wpa_drv_associate(wpa_s, &params);
10029 	wpa_s->testing_resend_assoc = 1;
10030 	return ret;
10031 #else /* CONFIG_SME */
10032 	return -1;
10033 #endif /* CONFIG_SME */
10034 }
10035 
10036 
wpas_ctrl_iface_send_twt_setup(struct wpa_supplicant * wpa_s,const char * cmd)10037 static int wpas_ctrl_iface_send_twt_setup(struct wpa_supplicant *wpa_s,
10038 					  const char *cmd)
10039 {
10040 	u8 dtok = 1;
10041 	int exponent = 10;
10042 	int mantissa = 8192;
10043 	u8 min_twt = 255;
10044 	unsigned long long twt = 0;
10045 	bool requestor = true;
10046 	int setup_cmd = 0;
10047 	bool trigger = true;
10048 	bool implicit = true;
10049 	bool flow_type = true;
10050 	int flow_id = 0;
10051 	bool protection = false;
10052 	u8 twt_channel = 0;
10053 	u8 control = BIT(4); /* Control field (IEEE P802.11ax/D8.0 Figure
10054 			      * 9-687): B4 = TWT Information Frame Disabled */
10055 	const char *tok_s;
10056 
10057 	tok_s = os_strstr(cmd, " dialog=");
10058 	if (tok_s)
10059 		dtok = atoi(tok_s + os_strlen(" dialog="));
10060 
10061 	tok_s = os_strstr(cmd, " exponent=");
10062 	if (tok_s)
10063 		exponent = atoi(tok_s + os_strlen(" exponent="));
10064 
10065 	tok_s = os_strstr(cmd, " mantissa=");
10066 	if (tok_s)
10067 		mantissa = atoi(tok_s + os_strlen(" mantissa="));
10068 
10069 	tok_s = os_strstr(cmd, " min_twt=");
10070 	if (tok_s)
10071 		min_twt = atoi(tok_s + os_strlen(" min_twt="));
10072 
10073 	tok_s = os_strstr(cmd, " setup_cmd=");
10074 	if (tok_s)
10075 		setup_cmd = atoi(tok_s + os_strlen(" setup_cmd="));
10076 
10077 	tok_s = os_strstr(cmd, " twt=");
10078 	if (tok_s)
10079 		sscanf(tok_s + os_strlen(" twt="), "%llu", &twt);
10080 
10081 	tok_s = os_strstr(cmd, " requestor=");
10082 	if (tok_s)
10083 		requestor = atoi(tok_s + os_strlen(" requestor="));
10084 
10085 	tok_s = os_strstr(cmd, " trigger=");
10086 	if (tok_s)
10087 		trigger = atoi(tok_s + os_strlen(" trigger="));
10088 
10089 	tok_s = os_strstr(cmd, " implicit=");
10090 	if (tok_s)
10091 		implicit = atoi(tok_s + os_strlen(" implicit="));
10092 
10093 	tok_s = os_strstr(cmd, " flow_type=");
10094 	if (tok_s)
10095 		flow_type = atoi(tok_s + os_strlen(" flow_type="));
10096 
10097 	tok_s = os_strstr(cmd, " flow_id=");
10098 	if (tok_s)
10099 		flow_id = atoi(tok_s + os_strlen(" flow_id="));
10100 
10101 	tok_s = os_strstr(cmd, " protection=");
10102 	if (tok_s)
10103 		protection = atoi(tok_s + os_strlen(" protection="));
10104 
10105 	tok_s = os_strstr(cmd, " twt_channel=");
10106 	if (tok_s)
10107 		twt_channel = atoi(tok_s + os_strlen(" twt_channel="));
10108 
10109 	tok_s = os_strstr(cmd, " control=");
10110 	if (tok_s)
10111 		control = atoi(tok_s + os_strlen(" control="));
10112 
10113 	return wpas_twt_send_setup(wpa_s, dtok, exponent, mantissa, min_twt,
10114 				   setup_cmd, twt, requestor, trigger, implicit,
10115 				   flow_type, flow_id, protection, twt_channel,
10116 				   control);
10117 }
10118 
10119 
wpas_ctrl_iface_send_twt_teardown(struct wpa_supplicant * wpa_s,const char * cmd)10120 static int wpas_ctrl_iface_send_twt_teardown(struct wpa_supplicant *wpa_s,
10121 					     const char *cmd)
10122 {
10123 	u8 flags = 0x1;
10124 	const char *tok_s;
10125 
10126 	tok_s = os_strstr(cmd, " flags=");
10127 	if (tok_s)
10128 		flags = atoi(tok_s + os_strlen(" flags="));
10129 
10130 	return wpas_twt_send_teardown(wpa_s, flags);
10131 }
10132 
10133 #endif /* CONFIG_TESTING_OPTIONS */
10134 
10135 
wpas_ctrl_vendor_elem_add(struct wpa_supplicant * wpa_s,char * cmd)10136 static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
10137 {
10138 	char *pos = cmd;
10139 	int frame;
10140 	size_t len;
10141 	struct wpabuf *buf;
10142 	struct ieee802_11_elems elems;
10143 
10144 	frame = atoi(pos);
10145 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
10146 		return -1;
10147 	wpa_s = wpas_vendor_elem(wpa_s, frame);
10148 
10149 	pos = os_strchr(pos, ' ');
10150 	if (pos == NULL)
10151 		return -1;
10152 	pos++;
10153 
10154 	len = os_strlen(pos);
10155 	if (len == 0)
10156 		return 0;
10157 	if (len & 1)
10158 		return -1;
10159 	len /= 2;
10160 
10161 	buf = wpabuf_alloc(len);
10162 	if (buf == NULL)
10163 		return -1;
10164 
10165 	if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
10166 		wpabuf_free(buf);
10167 		return -1;
10168 	}
10169 
10170 	if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
10171 	    ParseFailed) {
10172 		wpabuf_free(buf);
10173 		return -1;
10174 	}
10175 
10176 	if (wpa_s->vendor_elem[frame] == NULL) {
10177 		wpa_s->vendor_elem[frame] = buf;
10178 		goto update_ies;
10179 	}
10180 
10181 	if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
10182 		wpabuf_free(buf);
10183 		return -1;
10184 	}
10185 
10186 	wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
10187 	wpabuf_free(buf);
10188 
10189 update_ies:
10190 	wpas_vendor_elem_update(wpa_s);
10191 
10192 	if (frame == VENDOR_ELEM_PROBE_REQ ||
10193 	    frame == VENDOR_ELEM_PROBE_REQ_P2P)
10194 		wpa_supplicant_set_default_scan_ies(wpa_s);
10195 
10196 	return 0;
10197 }
10198 
10199 
wpas_ctrl_vendor_elem_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)10200 static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
10201 				     char *buf, size_t buflen)
10202 {
10203 	int frame = atoi(cmd);
10204 
10205 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
10206 		return -1;
10207 	wpa_s = wpas_vendor_elem(wpa_s, frame);
10208 
10209 	if (wpa_s->vendor_elem[frame] == NULL)
10210 		return 0;
10211 
10212 	return wpa_snprintf_hex(buf, buflen,
10213 				wpabuf_head_u8(wpa_s->vendor_elem[frame]),
10214 				wpabuf_len(wpa_s->vendor_elem[frame]));
10215 }
10216 
10217 
wpas_ctrl_vendor_elem_remove(struct wpa_supplicant * wpa_s,char * cmd)10218 static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
10219 {
10220 	char *pos = cmd;
10221 	int frame;
10222 	size_t len;
10223 	u8 *buf;
10224 	struct ieee802_11_elems elems;
10225 	int res;
10226 
10227 	frame = atoi(pos);
10228 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
10229 		return -1;
10230 	wpa_s = wpas_vendor_elem(wpa_s, frame);
10231 
10232 	pos = os_strchr(pos, ' ');
10233 	if (pos == NULL)
10234 		return -1;
10235 	pos++;
10236 
10237 	if (*pos == '*') {
10238 		wpabuf_free(wpa_s->vendor_elem[frame]);
10239 		wpa_s->vendor_elem[frame] = NULL;
10240 		wpas_vendor_elem_update(wpa_s);
10241 		return 0;
10242 	}
10243 
10244 	if (wpa_s->vendor_elem[frame] == NULL)
10245 		return -1;
10246 
10247 	len = os_strlen(pos);
10248 	if (len == 0)
10249 		return 0;
10250 	if (len & 1)
10251 		return -1;
10252 	len /= 2;
10253 
10254 	buf = os_malloc(len);
10255 	if (buf == NULL)
10256 		return -1;
10257 
10258 	if (hexstr2bin(pos, buf, len) < 0) {
10259 		os_free(buf);
10260 		return -1;
10261 	}
10262 
10263 	if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
10264 		os_free(buf);
10265 		return -1;
10266 	}
10267 
10268 	res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
10269 	os_free(buf);
10270 	return res;
10271 }
10272 
10273 
wpas_ctrl_neighbor_rep_cb(void * ctx,struct wpabuf * neighbor_rep)10274 static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
10275 {
10276 	struct wpa_supplicant *wpa_s = ctx;
10277 	size_t len;
10278 	const u8 *data;
10279 
10280 	/*
10281 	 * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
10282 	 * BSSID[6]
10283 	 * BSSID Information[4]
10284 	 * Operating Class[1]
10285 	 * Channel Number[1]
10286 	 * PHY Type[1]
10287 	 * Optional Subelements[variable]
10288 	 */
10289 #define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
10290 
10291 	if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
10292 		wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
10293 		goto out;
10294 	}
10295 
10296 	data = wpabuf_head_u8(neighbor_rep);
10297 	len = wpabuf_len(neighbor_rep);
10298 
10299 	while (len >= 2 + NR_IE_MIN_LEN) {
10300 		const u8 *nr;
10301 		char lci[256 * 2 + 1];
10302 		char civic[256 * 2 + 1];
10303 		u8 nr_len = data[1];
10304 		const u8 *pos = data, *end;
10305 
10306 		if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
10307 		    nr_len < NR_IE_MIN_LEN) {
10308 			wpa_dbg(wpa_s, MSG_DEBUG,
10309 				"CTRL: Invalid Neighbor Report element: id=%u len=%u",
10310 				data[0], nr_len);
10311 			goto out;
10312 		}
10313 
10314 		if (2U + nr_len > len) {
10315 			wpa_dbg(wpa_s, MSG_DEBUG,
10316 				"CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
10317 				data[0], len, nr_len);
10318 			goto out;
10319 		}
10320 		pos += 2;
10321 		end = pos + nr_len;
10322 
10323 		nr = pos;
10324 		pos += NR_IE_MIN_LEN;
10325 
10326 		lci[0] = '\0';
10327 		civic[0] = '\0';
10328 		while (end - pos > 2) {
10329 			u8 s_id, s_len;
10330 
10331 			s_id = *pos++;
10332 			s_len = *pos++;
10333 			if (s_len > end - pos)
10334 				goto out;
10335 			if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
10336 				/* Measurement Token[1] */
10337 				/* Measurement Report Mode[1] */
10338 				/* Measurement Type[1] */
10339 				/* Measurement Report[variable] */
10340 				switch (pos[2]) {
10341 				case MEASURE_TYPE_LCI:
10342 					if (lci[0])
10343 						break;
10344 					wpa_snprintf_hex(lci, sizeof(lci),
10345 							 pos, s_len);
10346 					break;
10347 				case MEASURE_TYPE_LOCATION_CIVIC:
10348 					if (civic[0])
10349 						break;
10350 					wpa_snprintf_hex(civic, sizeof(civic),
10351 							 pos, s_len);
10352 					break;
10353 				}
10354 			}
10355 
10356 			pos += s_len;
10357 		}
10358 
10359 		wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
10360 			"bssid=" MACSTR
10361 			" info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
10362 			MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
10363 			nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
10364 			nr[ETH_ALEN + 6],
10365 			lci[0] ? " lci=" : "", lci,
10366 			civic[0] ? " civic=" : "", civic);
10367 
10368 		data = end;
10369 		len -= 2 + nr_len;
10370 	}
10371 
10372 out:
10373 	wpabuf_free(neighbor_rep);
10374 }
10375 
10376 
wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant * wpa_s,char * cmd)10377 static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
10378 					     char *cmd)
10379 {
10380 	struct wpa_ssid_value ssid, *ssid_p = NULL;
10381 	int ret, lci = 0, civic = 0;
10382 	char *ssid_s;
10383 
10384 	ssid_s = os_strstr(cmd, "ssid=");
10385 	if (ssid_s) {
10386 		if (ssid_parse(ssid_s + 5, &ssid)) {
10387 			wpa_msg(wpa_s, MSG_INFO,
10388 				"CTRL: Send Neighbor Report: bad SSID");
10389 			return -1;
10390 		}
10391 
10392 		ssid_p = &ssid;
10393 
10394 		/*
10395 		 * Move cmd after the SSID text that may include "lci" or
10396 		 * "civic".
10397 		 */
10398 		cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
10399 		if (cmd)
10400 			cmd++;
10401 
10402 	}
10403 
10404 	if (cmd && os_strstr(cmd, "lci"))
10405 		lci = 1;
10406 
10407 	if (cmd && os_strstr(cmd, "civic"))
10408 		civic = 1;
10409 
10410 	ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
10411 						 wpas_ctrl_neighbor_rep_cb,
10412 						 wpa_s);
10413 
10414 	return ret;
10415 }
10416 
10417 
wpas_ctrl_iface_erp_flush(struct wpa_supplicant * wpa_s)10418 static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
10419 {
10420 	eapol_sm_erp_flush(wpa_s->eapol);
10421 	return 0;
10422 }
10423 
10424 
wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant * wpa_s,char * cmd)10425 static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
10426 					 char *cmd)
10427 {
10428 	char *token, *context = NULL;
10429 	unsigned int enable = ~0, type = 0;
10430 	u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
10431 	u8 *addr = NULL, *mask = NULL;
10432 
10433 	while ((token = str_token(cmd, " ", &context))) {
10434 		if (os_strcasecmp(token, "scan") == 0) {
10435 			type |= MAC_ADDR_RAND_SCAN;
10436 		} else if (os_strcasecmp(token, "sched") == 0) {
10437 			type |= MAC_ADDR_RAND_SCHED_SCAN;
10438 		} else if (os_strcasecmp(token, "pno") == 0) {
10439 			type |= MAC_ADDR_RAND_PNO;
10440 		} else if (os_strcasecmp(token, "all") == 0) {
10441 			type = wpa_s->mac_addr_rand_supported;
10442 		} else if (os_strncasecmp(token, "enable=", 7) == 0) {
10443 			enable = atoi(token + 7);
10444 		} else if (os_strncasecmp(token, "addr=", 5) == 0) {
10445 			addr = _addr;
10446 			if (hwaddr_aton(token + 5, addr)) {
10447 				wpa_printf(MSG_INFO,
10448 					   "CTRL: Invalid MAC address: %s",
10449 					   token);
10450 				return -1;
10451 			}
10452 		} else if (os_strncasecmp(token, "mask=", 5) == 0) {
10453 			mask = _mask;
10454 			if (hwaddr_aton(token + 5, mask)) {
10455 				wpa_printf(MSG_INFO,
10456 					   "CTRL: Invalid MAC address mask: %s",
10457 					   token);
10458 				return -1;
10459 			}
10460 		} else {
10461 			wpa_printf(MSG_INFO,
10462 				   "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
10463 				   token);
10464 			return -1;
10465 		}
10466 	}
10467 
10468 	if (!type) {
10469 		wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
10470 		return -1;
10471 	}
10472 
10473 	if (enable > 1) {
10474 		wpa_printf(MSG_INFO,
10475 			   "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
10476 		return -1;
10477 	}
10478 
10479 	if (!enable)
10480 		return wpas_disable_mac_addr_randomization(wpa_s, type);
10481 
10482 	return wpas_enable_mac_addr_randomization(wpa_s, type, addr, mask);
10483 }
10484 
10485 
wpas_ctrl_iface_pmksa(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)10486 static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
10487 				 char *buf, size_t buflen)
10488 {
10489 	size_t reply_len;
10490 
10491 	reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
10492 #ifdef CONFIG_AP
10493 	reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
10494 					      buflen - reply_len);
10495 #endif /* CONFIG_AP */
10496 	return reply_len;
10497 }
10498 
10499 
wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant * wpa_s)10500 static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
10501 {
10502 	ptksa_cache_flush(wpa_s->ptksa, NULL, WPA_CIPHER_NONE);
10503 	wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
10504 #ifdef CONFIG_AP
10505 	wpas_ap_pmksa_cache_flush(wpa_s);
10506 #endif /* CONFIG_AP */
10507 }
10508 
10509 
10510 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
10511 
wpas_ctrl_iface_pmksa_get(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)10512 static int wpas_ctrl_iface_pmksa_get(struct wpa_supplicant *wpa_s,
10513 				     const char *cmd, char *buf, size_t buflen)
10514 {
10515 	struct rsn_pmksa_cache_entry *entry;
10516 	struct wpa_ssid *ssid;
10517 	char *pos, *pos2, *end;
10518 	int ret;
10519 	struct os_reltime now;
10520 
10521 	ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
10522 	if (!ssid)
10523 		return -1;
10524 
10525 	pos = buf;
10526 	end = buf + buflen;
10527 
10528 	os_get_reltime(&now);
10529 
10530 	/*
10531 	 * Entry format:
10532 	 * <BSSID> <PMKID> <PMK> <reauth_time in seconds>
10533 	 * <expiration in seconds> <akmp> <opportunistic>
10534 	 * [FILS Cache Identifier]
10535 	 */
10536 
10537 	for (entry = wpa_sm_pmksa_cache_head(wpa_s->wpa); entry;
10538 	     entry = entry->next) {
10539 		if (entry->network_ctx != ssid)
10540 			continue;
10541 
10542 		pos2 = pos;
10543 		ret = os_snprintf(pos2, end - pos2, MACSTR " ",
10544 				  MAC2STR(entry->aa));
10545 		if (os_snprintf_error(end - pos2, ret))
10546 			break;
10547 		pos2 += ret;
10548 
10549 		pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmkid,
10550 					 PMKID_LEN);
10551 
10552 		ret = os_snprintf(pos2, end - pos2, " ");
10553 		if (os_snprintf_error(end - pos2, ret))
10554 			break;
10555 		pos2 += ret;
10556 
10557 		pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmk,
10558 					 entry->pmk_len);
10559 
10560 		ret = os_snprintf(pos2, end - pos2, " %d %d %d %d",
10561 				  (int) (entry->reauth_time - now.sec),
10562 				  (int) (entry->expiration - now.sec),
10563 				  entry->akmp,
10564 				  entry->opportunistic);
10565 		if (os_snprintf_error(end - pos2, ret))
10566 			break;
10567 		pos2 += ret;
10568 
10569 		if (entry->fils_cache_id_set) {
10570 			ret = os_snprintf(pos2, end - pos2, " %02x%02x",
10571 					  entry->fils_cache_id[0],
10572 					  entry->fils_cache_id[1]);
10573 			if (os_snprintf_error(end - pos2, ret))
10574 				break;
10575 			pos2 += ret;
10576 		}
10577 
10578 		ret = os_snprintf(pos2, end - pos2, "\n");
10579 		if (os_snprintf_error(end - pos2, ret))
10580 			break;
10581 		pos2 += ret;
10582 
10583 		pos = pos2;
10584 	}
10585 
10586 	return pos - buf;
10587 }
10588 
10589 
wpas_ctrl_iface_pmksa_add(struct wpa_supplicant * wpa_s,char * cmd)10590 static int wpas_ctrl_iface_pmksa_add(struct wpa_supplicant *wpa_s,
10591 				     char *cmd)
10592 {
10593 	struct rsn_pmksa_cache_entry *entry;
10594 	struct wpa_ssid *ssid;
10595 	char *pos, *pos2;
10596 	int ret = -1;
10597 	struct os_reltime now;
10598 	int reauth_time = 0, expiration = 0, i;
10599 
10600 	/*
10601 	 * Entry format:
10602 	 * <network_id> <BSSID> <PMKID> <PMK> <reauth_time in seconds>
10603 	 * <expiration in seconds> <akmp> <opportunistic>
10604 	 * [FILS Cache Identifier]
10605 	 */
10606 
10607 	ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
10608 	if (!ssid)
10609 		return -1;
10610 
10611 	pos = os_strchr(cmd, ' ');
10612 	if (!pos)
10613 		return -1;
10614 	pos++;
10615 
10616 	entry = os_zalloc(sizeof(*entry));
10617 	if (!entry)
10618 		return -1;
10619 
10620 	if (hwaddr_aton(pos, entry->aa))
10621 		goto fail;
10622 
10623 	pos = os_strchr(pos, ' ');
10624 	if (!pos)
10625 		goto fail;
10626 	pos++;
10627 
10628 	if (hexstr2bin(pos, entry->pmkid, PMKID_LEN) < 0)
10629 		goto fail;
10630 
10631 	pos = os_strchr(pos, ' ');
10632 	if (!pos)
10633 		goto fail;
10634 	pos++;
10635 
10636 	pos2 = os_strchr(pos, ' ');
10637 	if (!pos2)
10638 		goto fail;
10639 	entry->pmk_len = (pos2 - pos) / 2;
10640 	if (entry->pmk_len < PMK_LEN || entry->pmk_len > PMK_LEN_MAX ||
10641 	    hexstr2bin(pos, entry->pmk, entry->pmk_len) < 0)
10642 		goto fail;
10643 
10644 	pos = os_strchr(pos, ' ');
10645 	if (!pos)
10646 		goto fail;
10647 	pos++;
10648 
10649 	if (sscanf(pos, "%d %d %d %d", &reauth_time, &expiration,
10650 		   &entry->akmp, &entry->opportunistic) != 4)
10651 		goto fail;
10652 	if (reauth_time > expiration)
10653 		goto fail;
10654 	for (i = 0; i < 4; i++) {
10655 		pos = os_strchr(pos, ' ');
10656 		if (!pos) {
10657 			if (i < 3)
10658 				goto fail;
10659 			break;
10660 		}
10661 		pos++;
10662 	}
10663 	if (pos) {
10664 		if (hexstr2bin(pos, entry->fils_cache_id,
10665 			       FILS_CACHE_ID_LEN) < 0)
10666 			goto fail;
10667 		entry->fils_cache_id_set = 1;
10668 	}
10669 	os_get_reltime(&now);
10670 	entry->expiration = now.sec + expiration;
10671 	entry->reauth_time = now.sec + reauth_time;
10672 
10673 	entry->network_ctx = ssid;
10674 
10675 	entry->external = true;
10676 
10677 	wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
10678 	entry = NULL;
10679 	ret = 0;
10680 fail:
10681 	os_free(entry);
10682 	return ret;
10683 }
10684 
10685 
10686 #ifdef CONFIG_MESH
10687 
wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)10688 static int wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant *wpa_s,
10689 					  const char *cmd, char *buf,
10690 					  size_t buflen)
10691 {
10692 	u8 spa[ETH_ALEN];
10693 
10694 	if (!wpa_s->ifmsh)
10695 		return -1;
10696 
10697 	if (os_strcasecmp(cmd, "any") == 0)
10698 		return wpas_ap_pmksa_cache_list_mesh(wpa_s, NULL, buf, buflen);
10699 
10700 	if (hwaddr_aton(cmd, spa))
10701 		return -1;
10702 
10703 	return wpas_ap_pmksa_cache_list_mesh(wpa_s, spa, buf, buflen);
10704 }
10705 
10706 
wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant * wpa_s,char * cmd)10707 static int wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant *wpa_s,
10708 					  char *cmd)
10709 {
10710 	/*
10711 	 * We do not check mesh interface existence because PMKSA should be
10712 	 * stored before wpa_s->ifmsh creation to suppress commit message
10713 	 * creation.
10714 	 */
10715 	return wpas_ap_pmksa_cache_add_external(wpa_s, cmd);
10716 }
10717 
10718 #endif /* CONFIG_MESH */
10719 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
10720 
10721 
10722 #ifdef CONFIG_FILS
wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant * wpa_s,const char * cmd)10723 static int wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant *wpa_s,
10724 					    const char *cmd)
10725 {
10726 	struct fils_hlp_req *req;
10727 	const char *pos;
10728 
10729 	/* format: <dst> <packet starting from ethertype> */
10730 
10731 	req = os_zalloc(sizeof(*req));
10732 	if (!req)
10733 		return -1;
10734 
10735 	if (hwaddr_aton(cmd, req->dst))
10736 		goto fail;
10737 
10738 	pos = os_strchr(cmd, ' ');
10739 	if (!pos)
10740 		goto fail;
10741 	pos++;
10742 	req->pkt = wpabuf_parse_bin(pos);
10743 	if (!req->pkt)
10744 		goto fail;
10745 
10746 	dl_list_add_tail(&wpa_s->fils_hlp_req, &req->list);
10747 	return 0;
10748 fail:
10749 	wpabuf_free(req->pkt);
10750 	os_free(req);
10751 	return -1;
10752 }
10753 #endif /* CONFIG_FILS */
10754 
10755 
wpas_ctrl_cmd_debug_level(const char * cmd)10756 static int wpas_ctrl_cmd_debug_level(const char *cmd)
10757 {
10758 	if (os_strcmp(cmd, "PING") == 0 ||
10759 	    os_strncmp(cmd, "BSS ", 4) == 0 ||
10760 	    os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
10761 	    os_strncmp(cmd, "STATUS", 6) == 0 ||
10762 	    os_strncmp(cmd, "STA ", 4) == 0 ||
10763 	    os_strncmp(cmd, "STA-", 4) == 0)
10764 		return MSG_EXCESSIVE;
10765 	return MSG_DEBUG;
10766 }
10767 
10768 
wpas_ctrl_iface_configure_mscs(struct wpa_supplicant * wpa_s,const char * cmd)10769 static int wpas_ctrl_iface_configure_mscs(struct wpa_supplicant *wpa_s,
10770 					  const char *cmd)
10771 {
10772 	size_t frame_classifier_len;
10773 	const char *pos, *end;
10774 	struct robust_av_data *robust_av = &wpa_s->robust_av;
10775 	int val;
10776 
10777 	/*
10778 	 * format:
10779 	 * <add|remove|change> [up_bitmap=<hex byte>] [up_limit=<integer>]
10780 	 * [stream_timeout=<in TUs>] [frame_classifier=<hex bytes>]
10781 	 */
10782 	os_memset(robust_av, 0, sizeof(struct robust_av_data));
10783 	if (os_strncmp(cmd, "add ", 4) == 0) {
10784 		robust_av->request_type = SCS_REQ_ADD;
10785 	} else if (os_strcmp(cmd, "remove") == 0) {
10786 		robust_av->request_type = SCS_REQ_REMOVE;
10787 		robust_av->valid_config = false;
10788 		return wpas_send_mscs_req(wpa_s);
10789 	} else if (os_strncmp(cmd, "change ", 7) == 0) {
10790 		robust_av->request_type = SCS_REQ_CHANGE;
10791 	} else {
10792 		return -1;
10793 	}
10794 
10795 	pos = os_strstr(cmd, "up_bitmap=");
10796 	if (!pos)
10797 		return -1;
10798 
10799 	val = hex2byte(pos + 10);
10800 	if (val < 0)
10801 		return -1;
10802 	robust_av->up_bitmap = val;
10803 
10804 	pos = os_strstr(cmd, "up_limit=");
10805 	if (!pos)
10806 		return -1;
10807 
10808 	robust_av->up_limit = atoi(pos + 9);
10809 
10810 	pos = os_strstr(cmd, "stream_timeout=");
10811 	if (!pos)
10812 		return -1;
10813 
10814 	robust_av->stream_timeout = atoi(pos + 15);
10815 	if (robust_av->stream_timeout == 0)
10816 		return -1;
10817 
10818 	pos = os_strstr(cmd, "frame_classifier=");
10819 	if (!pos)
10820 		return -1;
10821 
10822 	pos += 17;
10823 	end = os_strchr(pos, ' ');
10824 	if (!end)
10825 		end = pos + os_strlen(pos);
10826 
10827 	frame_classifier_len = (end - pos) / 2;
10828 	if (frame_classifier_len > sizeof(robust_av->frame_classifier) ||
10829 	    hexstr2bin(pos, robust_av->frame_classifier, frame_classifier_len))
10830 		return -1;
10831 
10832 	robust_av->frame_classifier_len = frame_classifier_len;
10833 	robust_av->valid_config = true;
10834 
10835 	return wpas_send_mscs_req(wpa_s);
10836 }
10837 
10838 
10839 #ifdef CONFIG_PASN
wpas_ctrl_iface_pasn_start(struct wpa_supplicant * wpa_s,char * cmd)10840 static int wpas_ctrl_iface_pasn_start(struct wpa_supplicant *wpa_s, char *cmd)
10841 {
10842 	char *token, *context = NULL;
10843 	u8 bssid[ETH_ALEN];
10844 	int akmp = -1, cipher = -1, got_bssid = 0;
10845 	u16 group = 0xFFFF;
10846 	u8 *comeback = NULL;
10847 	size_t comeback_len = 0;
10848 	int id = 0, ret = -1;
10849 
10850 	/*
10851 	 * Entry format: bssid=<BSSID> akmp=<AKMP> cipher=<CIPHER> group=<group>
10852 	 *    [comeback=<hexdump>]
10853 	 */
10854 	while ((token = str_token(cmd, " ", &context))) {
10855 		if (os_strncmp(token, "bssid=", 6) == 0) {
10856 			if (hwaddr_aton(token + 6, bssid))
10857 				goto out;
10858 			got_bssid = 1;
10859 		} else if (os_strcmp(token, "akmp=PASN") == 0) {
10860 			akmp = WPA_KEY_MGMT_PASN;
10861 #ifdef CONFIG_IEEE80211R
10862 		} else if (os_strcmp(token, "akmp=FT-PSK") == 0) {
10863 			akmp = WPA_KEY_MGMT_FT_PSK;
10864 		} else if (os_strcmp(token, "akmp=FT-EAP-SHA384") == 0) {
10865 			akmp = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
10866 		} else if (os_strcmp(token, "akmp=FT-EAP") == 0) {
10867 			akmp = WPA_KEY_MGMT_FT_IEEE8021X;
10868 #endif /* CONFIG_IEEE80211R */
10869 #ifdef CONFIG_SAE
10870 		} else if (os_strcmp(token, "akmp=SAE") == 0) {
10871 			akmp = WPA_KEY_MGMT_SAE;
10872 #endif /* CONFIG_SAE */
10873 #ifdef CONFIG_FILS
10874 		} else if (os_strcmp(token, "akmp=FILS-SHA256") == 0) {
10875 			akmp = WPA_KEY_MGMT_FILS_SHA256;
10876 		} else if (os_strcmp(token, "akmp=FILS-SHA384") == 0) {
10877 			akmp = WPA_KEY_MGMT_FILS_SHA384;
10878 #endif /* CONFIG_FILS */
10879 		} else if (os_strcmp(token, "cipher=CCMP-256") == 0) {
10880 			cipher = WPA_CIPHER_CCMP_256;
10881 		} else if (os_strcmp(token, "cipher=GCMP-256") == 0) {
10882 			cipher = WPA_CIPHER_GCMP_256;
10883 		} else if (os_strcmp(token, "cipher=CCMP") == 0) {
10884 			cipher = WPA_CIPHER_CCMP;
10885 		} else if (os_strcmp(token, "cipher=GCMP") == 0) {
10886 			cipher = WPA_CIPHER_GCMP;
10887 		} else if (os_strncmp(token, "group=", 6) == 0) {
10888 			group = atoi(token + 6);
10889 		} else if (os_strncmp(token, "nid=", 4) == 0) {
10890 			id = atoi(token + 4);
10891 		} else if (os_strncmp(token, "comeback=", 9) == 0) {
10892 			comeback_len = os_strlen(token + 9);
10893 			if (comeback || !comeback_len || comeback_len % 2)
10894 				goto out;
10895 
10896 			comeback_len /= 2;
10897 			comeback = os_malloc(comeback_len);
10898 			if (!comeback ||
10899 			    hexstr2bin(token + 9, comeback, comeback_len))
10900 				goto out;
10901 		} else {
10902 			wpa_printf(MSG_DEBUG,
10903 				   "CTRL: PASN Invalid parameter: '%s'",
10904 				   token);
10905 			goto out;
10906 		}
10907 	}
10908 
10909 	if (!got_bssid || akmp == -1 || cipher == -1 || group == 0xFFFF) {
10910 		wpa_printf(MSG_DEBUG,"CTRL: PASN missing parameter");
10911 		goto out;
10912 	}
10913 
10914 	ret = wpas_pasn_auth_start(wpa_s, bssid, akmp, cipher, group, id,
10915 				   comeback, comeback_len);
10916 out:
10917 	os_free(comeback);
10918 	return ret;
10919 }
10920 
10921 
wpas_ctrl_iface_pasn_deauthenticate(struct wpa_supplicant * wpa_s,const char * cmd)10922 static int wpas_ctrl_iface_pasn_deauthenticate(struct wpa_supplicant *wpa_s,
10923 					       const char *cmd)
10924 {
10925 	u8 bssid[ETH_ALEN];
10926 
10927 	if (os_strncmp(cmd, "bssid=", 6) != 0 || hwaddr_aton(cmd + 6, bssid)) {
10928 		wpa_printf(MSG_DEBUG,
10929 			   "CTRL: PASN_DEAUTH without valid BSSID");
10930 		return -1;
10931 	}
10932 
10933 	return wpas_pasn_deauthenticate(wpa_s, bssid);
10934 }
10935 
10936 #endif /* CONFIG_PASN */
10937 
10938 
set_type4_frame_classifier(const char * cmd,struct type4_params * param)10939 static int set_type4_frame_classifier(const char *cmd,
10940 				      struct type4_params *param)
10941 {
10942 	const char *pos, *end;
10943 	u8 classifier_mask = 0;
10944 	int ret;
10945 	char addr[INET6_ADDRSTRLEN];
10946 	size_t alen;
10947 
10948 	if (os_strstr(cmd, "ip_version=ipv4")) {
10949 		param->ip_version = IPV4;
10950 	} else if (os_strstr(cmd, "ip_version=ipv6")) {
10951 		param->ip_version = IPV6;
10952 	} else {
10953 		wpa_printf(MSG_ERROR, "IP version missing/invalid");
10954 		return -1;
10955 	}
10956 
10957 	classifier_mask |= BIT(0);
10958 
10959 	pos = os_strstr(cmd, "src_ip=");
10960 	if (pos) {
10961 		pos += 7;
10962 		end = os_strchr(pos, ' ');
10963 		if (!end)
10964 			end = pos + os_strlen(pos);
10965 
10966 		alen = end - pos;
10967 		if (alen >= INET6_ADDRSTRLEN)
10968 			return -1;
10969 		os_memcpy(addr, pos, alen);
10970 		addr[alen] = '\0';
10971 		if (param->ip_version == IPV4)
10972 			ret = inet_pton(AF_INET, addr,
10973 					&param->ip_params.v4.src_ip);
10974 		else
10975 			ret = inet_pton(AF_INET6, addr,
10976 					&param->ip_params.v6.src_ip);
10977 
10978 		if (ret != 1) {
10979 			wpa_printf(MSG_ERROR,
10980 				   "Error converting src IP address to binary ret=%d",
10981 				   ret);
10982 			return -1;
10983 		}
10984 
10985 		classifier_mask |= BIT(1);
10986 	}
10987 
10988 	pos = os_strstr(cmd, "dst_ip=");
10989 	if (pos) {
10990 		pos += 7;
10991 		end = os_strchr(pos, ' ');
10992 		if (!end)
10993 			end = pos + os_strlen(pos);
10994 
10995 		alen = end - pos;
10996 		if (alen >= INET6_ADDRSTRLEN)
10997 			return -1;
10998 		os_memcpy(addr, pos, alen);
10999 		addr[alen] = '\0';
11000 		if (param->ip_version == IPV4)
11001 			ret = inet_pton(AF_INET, addr,
11002 					&param->ip_params.v4.dst_ip);
11003 		else
11004 			ret = inet_pton(AF_INET6, addr,
11005 					&param->ip_params.v6.dst_ip);
11006 
11007 		if (ret != 1) {
11008 			wpa_printf(MSG_ERROR,
11009 				   "Error converting dst IP address to binary ret=%d",
11010 				   ret);
11011 			return -1;
11012 		}
11013 
11014 		classifier_mask |= BIT(2);
11015 	}
11016 
11017 	pos = os_strstr(cmd, "src_port=");
11018 	if (pos && atoi(pos + 9) > 0) {
11019 		if (param->ip_version == IPV4)
11020 			param->ip_params.v4.src_port = atoi(pos + 9);
11021 		else
11022 			param->ip_params.v6.src_port = atoi(pos + 9);
11023 		classifier_mask |= BIT(3);
11024 	}
11025 
11026 	pos = os_strstr(cmd, "dst_port=");
11027 	if (pos && atoi(pos + 9) > 0) {
11028 		if (param->ip_version == IPV4)
11029 			param->ip_params.v4.dst_port = atoi(pos + 9);
11030 		else
11031 			param->ip_params.v6.dst_port = atoi(pos + 9);
11032 		classifier_mask |= BIT(4);
11033 	}
11034 
11035 	pos = os_strstr(cmd, "dscp=");
11036 	if (pos && atoi(pos + 5) > 0) {
11037 		if (param->ip_version == IPV4)
11038 			param->ip_params.v4.dscp = atoi(pos + 5);
11039 		else
11040 			param->ip_params.v6.dscp = atoi(pos + 5);
11041 		classifier_mask |= BIT(5);
11042 	}
11043 
11044 	if (param->ip_version == IPV4) {
11045 		pos = os_strstr(cmd, "protocol=");
11046 		if (pos) {
11047 			if (os_strstr(pos, "udp")) {
11048 				param->ip_params.v4.protocol = 17;
11049 			} else if (os_strstr(pos, "tcp")) {
11050 				param->ip_params.v4.protocol = 6;
11051 			} else if (os_strstr(pos, "esp")) {
11052 				param->ip_params.v4.protocol = 50;
11053 			} else {
11054 				wpa_printf(MSG_ERROR, "Invalid protocol");
11055 				return -1;
11056 			}
11057 			classifier_mask |= BIT(6);
11058 		}
11059 	} else {
11060 		pos = os_strstr(cmd, "next_header=");
11061 		if (pos) {
11062 			if (os_strstr(pos, "udp")) {
11063 				param->ip_params.v6.next_header = 17;
11064 			} else if (os_strstr(pos, "tcp")) {
11065 				param->ip_params.v6.next_header = 6;
11066 			} else if (os_strstr(pos, "esp")) {
11067 				param->ip_params.v6.next_header = 50;
11068 			} else {
11069 				wpa_printf(MSG_ERROR, "Invalid next header");
11070 				return -1;
11071 			}
11072 
11073 			classifier_mask |= BIT(6);
11074 		}
11075 
11076 		pos = os_strstr(cmd, "flow_label=");
11077 		if (pos) {
11078 			pos += 11;
11079 			end = os_strchr(pos, ' ');
11080 			if (!end)
11081 				end = pos + os_strlen(pos);
11082 
11083 			if (end - pos != 6 ||
11084 			    hexstr2bin(pos, param->ip_params.v6.flow_label,
11085 				       3) ||
11086 			    param->ip_params.v6.flow_label[0] > 0x0F) {
11087 				wpa_printf(MSG_ERROR, "Invalid flow label");
11088 				return -1;
11089 			}
11090 
11091 			classifier_mask |= BIT(7);
11092 		}
11093 	}
11094 
11095 	param->classifier_mask = classifier_mask;
11096 	return 0;
11097 }
11098 
11099 
set_type10_frame_classifier(const char * cmd,struct type10_params * param)11100 static int set_type10_frame_classifier(const char *cmd,
11101 				       struct type10_params *param)
11102 {
11103 	const char *pos, *end;
11104 	size_t filter_len;
11105 
11106 	pos = os_strstr(cmd, "prot_instance=");
11107 	if (!pos) {
11108 		wpa_printf(MSG_ERROR, "Protocol instance missing");
11109 		return -1;
11110 	}
11111 	param->prot_instance = atoi(pos + 14);
11112 
11113 	pos = os_strstr(cmd, "prot_number=");
11114 	if (!pos) {
11115 		wpa_printf(MSG_ERROR, "Protocol number missing");
11116 		return -1;
11117 	}
11118 	if (os_strstr(pos, "udp")) {
11119 		param->prot_number = 17;
11120 	} else if (os_strstr(pos, "tcp")) {
11121 		param->prot_number = 6;
11122 	} else if (os_strstr(pos, "esp")) {
11123 		param->prot_number = 50;
11124 	} else {
11125 		wpa_printf(MSG_ERROR, "Invalid protocol number");
11126 		return -1;
11127 	}
11128 
11129 	pos = os_strstr(cmd, "filter_value=");
11130 	if (!pos) {
11131 		wpa_printf(MSG_ERROR,
11132 			   "Classifier parameter filter_value missing");
11133 		return -1;
11134 	}
11135 
11136 	pos += 13;
11137 	end = os_strchr(pos, ' ');
11138 	if (!end)
11139 		end = pos + os_strlen(pos);
11140 
11141 	filter_len = (end - pos) / 2;
11142 	param->filter_value = os_malloc(filter_len);
11143 	if (!param->filter_value)
11144 		return -1;
11145 
11146 	if (hexstr2bin(pos, param->filter_value, filter_len)) {
11147 		wpa_printf(MSG_ERROR, "Invalid filter_value %s", pos);
11148 		goto free;
11149 	}
11150 
11151 	pos = os_strstr(cmd, "filter_mask=");
11152 	if (!pos) {
11153 		wpa_printf(MSG_ERROR,
11154 			   "Classifier parameter filter_mask missing");
11155 		goto free;
11156 	}
11157 
11158 	pos += 12;
11159 	end = os_strchr(pos, ' ');
11160 	if (!end)
11161 		end = pos + os_strlen(pos);
11162 
11163 	if (filter_len != (size_t) (end - pos) / 2) {
11164 		wpa_printf(MSG_ERROR,
11165 			   "Filter mask length mismatch expected=%zu received=%zu",
11166 			   filter_len, (size_t) (end - pos) / 2);
11167 		goto free;
11168 	}
11169 
11170 	param->filter_mask = os_malloc(filter_len);
11171 	if (!param->filter_mask)
11172 		goto free;
11173 
11174 	if (hexstr2bin(pos, param->filter_mask, filter_len)) {
11175 		wpa_printf(MSG_ERROR, "Invalid filter mask %s", pos);
11176 		os_free(param->filter_mask);
11177 		param->filter_mask = NULL;
11178 		goto free;
11179 	}
11180 
11181 	param->filter_len = filter_len;
11182 	return 0;
11183 free:
11184 	os_free(param->filter_value);
11185 	param->filter_value = NULL;
11186 	return -1;
11187 }
11188 
11189 
scs_parse_type4(struct tclas_element * elem,const char * pos)11190 static int scs_parse_type4(struct tclas_element *elem, const char *pos)
11191 {
11192 	struct type4_params type4_param = { 0 };
11193 
11194 	if (set_type4_frame_classifier(pos, &type4_param) == -1) {
11195 		wpa_printf(MSG_ERROR, "Failed to set frame_classifier 4");
11196 		return -1;
11197 	}
11198 
11199 	os_memcpy(&elem->frame_classifier.type4_param,
11200 		  &type4_param, sizeof(struct type4_params));
11201 	return 0;
11202 }
11203 
11204 
scs_parse_type10(struct tclas_element * elem,const char * pos)11205 static int scs_parse_type10(struct tclas_element *elem, const char *pos)
11206 {
11207 	struct type10_params type10_param = { 0 };
11208 
11209 	if (set_type10_frame_classifier(pos, &type10_param) == -1) {
11210 		wpa_printf(MSG_ERROR, "Failed to set frame_classifier 10");
11211 		return -1;
11212 	}
11213 
11214 	os_memcpy(&elem->frame_classifier.type10_param,
11215 		  &type10_param, sizeof(struct type10_params));
11216 	return 0;
11217 }
11218 
11219 
wpas_ctrl_iface_configure_scs(struct wpa_supplicant * wpa_s,char * cmd)11220 static int wpas_ctrl_iface_configure_scs(struct wpa_supplicant *wpa_s,
11221 					 char *cmd)
11222 {
11223 	char *pos1, *pos;
11224 	struct scs_robust_av_data *scs_data = &wpa_s->scs_robust_av_req;
11225 	struct scs_desc_elem desc_elem = { 0 };
11226 	int val;
11227 	unsigned int num_scs_desc = 0;
11228 
11229 	if (wpa_s->ongoing_scs_req) {
11230 		wpa_printf(MSG_ERROR, "%s: SCS Request already in queue",
11231 			   __func__);
11232 		return -1;
11233 	}
11234 
11235 	/**
11236 	 * format:
11237 	 * [scs_id=<decimal number>] <add|remove|change> [scs_up=<0-7>]
11238 	 * [classifier_type=<4|10>]
11239 	 * [classifier params based on classifier type]
11240 	 * [tclas_processing=<0|1>] [scs_id=<decimal number>] ...
11241 	 */
11242 	pos1 = os_strstr(cmd, "scs_id=");
11243 	if (!pos1) {
11244 		wpa_printf(MSG_ERROR, "SCSID not present");
11245 		return -1;
11246 	}
11247 
11248 	free_up_scs_desc(scs_data);
11249 
11250 	while (pos1) {
11251 		struct scs_desc_elem *n1;
11252 		struct active_scs_elem *active_scs_desc;
11253 		char *next_scs_desc;
11254 		unsigned int num_tclas_elem = 0;
11255 		bool scsid_active = false;
11256 
11257 		desc_elem.scs_id = atoi(pos1 + 7);
11258 		pos1 += 7;
11259 
11260 		next_scs_desc = os_strstr(pos1, "scs_id=");
11261 		if (next_scs_desc) {
11262 			char temp[20];
11263 
11264 			os_snprintf(temp, sizeof(temp), "scs_id=%d ",
11265 				    desc_elem.scs_id);
11266 			if (os_strstr(next_scs_desc, temp)) {
11267 				wpa_printf(MSG_ERROR,
11268 					   "Multiple SCS descriptors configured with same SCSID(=%d)",
11269 					   desc_elem.scs_id);
11270 				goto free_scs_desc;
11271 			}
11272 			pos1[next_scs_desc - pos1 - 1] = '\0';
11273 		}
11274 
11275 		dl_list_for_each(active_scs_desc, &wpa_s->active_scs_ids,
11276 				 struct active_scs_elem, list) {
11277 			if (desc_elem.scs_id == active_scs_desc->scs_id) {
11278 				scsid_active = true;
11279 				break;
11280 			}
11281 		}
11282 
11283 		if (os_strstr(pos1, "add ")) {
11284 			desc_elem.request_type = SCS_REQ_ADD;
11285 			if (scsid_active) {
11286 				wpa_printf(MSG_ERROR, "SCSID %d already active",
11287 					   desc_elem.scs_id);
11288 				return -1;
11289 			}
11290 		} else if (os_strstr(pos1, "remove")) {
11291 			desc_elem.request_type = SCS_REQ_REMOVE;
11292 			if (!scsid_active) {
11293 				wpa_printf(MSG_ERROR, "SCSID %d not active",
11294 					   desc_elem.scs_id);
11295 				return -1;
11296 			}
11297 			goto scs_desc_end;
11298 		} else if (os_strstr(pos1, "change ")) {
11299 			desc_elem.request_type = SCS_REQ_CHANGE;
11300 			if (!scsid_active) {
11301 				wpa_printf(MSG_ERROR, "SCSID %d not active",
11302 					   desc_elem.scs_id);
11303 				return -1;
11304 			}
11305 		} else {
11306 			wpa_printf(MSG_ERROR, "SCS Request type invalid");
11307 			goto free_scs_desc;
11308 		}
11309 
11310 		pos1 = os_strstr(pos1, "scs_up=");
11311 		if (!pos1) {
11312 			wpa_printf(MSG_ERROR,
11313 				   "Intra-Access user priority not present");
11314 			goto free_scs_desc;
11315 		}
11316 
11317 		val = atoi(pos1 + 7);
11318 		if (val < 0 || val > 7) {
11319 			wpa_printf(MSG_ERROR,
11320 				   "Intra-Access user priority invalid %d",
11321 				   val);
11322 			goto free_scs_desc;
11323 		}
11324 
11325 		desc_elem.intra_access_priority = val;
11326 		desc_elem.scs_up_avail = true;
11327 
11328 		pos = os_strstr(pos1, "classifier_type=");
11329 		if (!pos) {
11330 			wpa_printf(MSG_ERROR, "classifier type empty");
11331 			goto free_scs_desc;
11332 		}
11333 
11334 		while (pos) {
11335 			struct tclas_element elem = { 0 }, *n;
11336 			char *next_tclas_elem;
11337 
11338 			val = atoi(pos + 16);
11339 			if (val != 4 && val != 10) {
11340 				wpa_printf(MSG_ERROR,
11341 					   "classifier type invalid %d", val);
11342 				goto free_scs_desc;
11343 			}
11344 
11345 			elem.classifier_type = val;
11346 			pos += 16;
11347 
11348 			next_tclas_elem = os_strstr(pos, "classifier_type=");
11349 			if (next_tclas_elem) {
11350 				pos1 = next_tclas_elem;
11351 				pos[next_tclas_elem - pos - 1] = '\0';
11352 			}
11353 
11354 			switch (val) {
11355 			case 4:
11356 				if (scs_parse_type4(&elem, pos) < 0)
11357 					goto free_scs_desc;
11358 				break;
11359 			case 10:
11360 				if (scs_parse_type10(&elem, pos) < 0)
11361 					goto free_scs_desc;
11362 				break;
11363 			}
11364 
11365 			n = os_realloc(desc_elem.tclas_elems,
11366 				       (num_tclas_elem + 1) * sizeof(elem));
11367 			if (!n)
11368 				goto free_scs_desc;
11369 
11370 			desc_elem.tclas_elems = n;
11371 			os_memcpy((u8 *) desc_elem.tclas_elems +
11372 				  num_tclas_elem * sizeof(elem),
11373 				  &elem, sizeof(elem));
11374 			num_tclas_elem++;
11375 			desc_elem.num_tclas_elem = num_tclas_elem;
11376 			pos = next_tclas_elem;
11377 		}
11378 
11379 		if (desc_elem.num_tclas_elem > 1) {
11380 			pos1 = os_strstr(pos1, "tclas_processing=");
11381 			if (!pos1) {
11382 				wpa_printf(MSG_ERROR, "tclas_processing empty");
11383 				goto free_scs_desc;
11384 			}
11385 
11386 			val = atoi(pos1 + 17);
11387 			if (val != 0 && val != 1) {
11388 				wpa_printf(MSG_ERROR,
11389 					   "tclas_processing invalid");
11390 				goto free_scs_desc;
11391 			}
11392 
11393 			desc_elem.tclas_processing = val;
11394 		}
11395 
11396 scs_desc_end:
11397 		n1 = os_realloc(scs_data->scs_desc_elems, (num_scs_desc + 1) *
11398 				sizeof(struct scs_desc_elem));
11399 		if (!n1)
11400 			goto free_scs_desc;
11401 
11402 		scs_data->scs_desc_elems = n1;
11403 		os_memcpy((u8 *) scs_data->scs_desc_elems + num_scs_desc *
11404 			  sizeof(desc_elem), &desc_elem, sizeof(desc_elem));
11405 		num_scs_desc++;
11406 		scs_data->num_scs_desc = num_scs_desc;
11407 		pos1 = next_scs_desc;
11408 		os_memset(&desc_elem, 0, sizeof(desc_elem));
11409 	}
11410 
11411 	return wpas_send_scs_req(wpa_s);
11412 
11413 free_scs_desc:
11414 	free_up_tclas_elem(&desc_elem);
11415 	free_up_scs_desc(scs_data);
11416 	return -1;
11417 }
11418 
11419 
wpas_ctrl_iface_send_dscp_resp(struct wpa_supplicant * wpa_s,const char * cmd)11420 static int wpas_ctrl_iface_send_dscp_resp(struct wpa_supplicant *wpa_s,
11421 					  const char *cmd)
11422 {
11423 	char *pos;
11424 	struct dscp_policy_status *policy = NULL, *n;
11425 	int num_policies = 0, ret = -1;
11426 	struct dscp_resp_data resp_data;
11427 
11428 	/*
11429 	 * format:
11430 	 * <[reset]>/<[solicited] [policy_id=1 status=0...]> [more]
11431 	 */
11432 
11433 	os_memset(&resp_data, 0, sizeof(resp_data));
11434 
11435 	resp_data.more = os_strstr(cmd, "more") != NULL;
11436 
11437 	if (os_strstr(cmd, "reset")) {
11438 		resp_data.reset = true;
11439 		resp_data.solicited = false;
11440 		goto send_resp;
11441 	}
11442 
11443 	resp_data.solicited = os_strstr(cmd, "solicited") != NULL;
11444 
11445 	pos = os_strstr(cmd, "policy_id=");
11446 	while (pos) {
11447 		n = os_realloc(policy, (num_policies + 1) * sizeof(*policy));
11448 		if (!n)
11449 			goto fail;
11450 
11451 		policy = n;
11452 		pos += 10;
11453 		policy[num_policies].id = atoi(pos);
11454 		if (policy[num_policies].id == 0) {
11455 			wpa_printf(MSG_ERROR, "DSCP: Invalid policy id");
11456 			goto fail;
11457 		}
11458 
11459 		pos = os_strstr(pos, "status=");
11460 		if (!pos) {
11461 			wpa_printf(MSG_ERROR,
11462 				   "DSCP: Status is not found for a policy");
11463 			goto fail;
11464 		}
11465 
11466 		pos += 7;
11467 		policy[num_policies].status = atoi(pos);
11468 		num_policies++;
11469 
11470 		pos = os_strstr(pos, "policy_id");
11471 	}
11472 
11473 	resp_data.policy = policy;
11474 	resp_data.num_policies = num_policies;
11475 send_resp:
11476 	ret = wpas_send_dscp_response(wpa_s, &resp_data);
11477 	if (ret)
11478 		wpa_printf(MSG_ERROR, "DSCP: Failed to send DSCP response");
11479 fail:
11480 	os_free(policy);
11481 	return ret;
11482 }
11483 
11484 
wpas_ctrl_iface_send_dscp_query(struct wpa_supplicant * wpa_s,const char * cmd)11485 static int wpas_ctrl_iface_send_dscp_query(struct wpa_supplicant *wpa_s,
11486 					   const char *cmd)
11487 {
11488 	char *pos;
11489 
11490 	/*
11491 	 * format:
11492 	 * Wildcard DSCP query
11493 	 * <wildcard>
11494 	 *
11495 	 * DSCP query with a domain name attribute:
11496 	 * [domain_name=<string>]
11497 	 */
11498 
11499 	if (os_strstr(cmd, "wildcard")) {
11500 		wpa_printf(MSG_DEBUG, "QM: Send wildcard DSCP policy query");
11501 		return wpas_send_dscp_query(wpa_s, NULL, 0);
11502 	}
11503 
11504 	pos = os_strstr(cmd, "domain_name=");
11505 	if (!pos || !os_strlen(pos + 12)) {
11506 		wpa_printf(MSG_ERROR, "QM: Domain name not preset");
11507 		return -1;
11508 	}
11509 
11510 	return wpas_send_dscp_query(wpa_s, pos + 12, os_strlen(pos + 12));
11511 }
11512 
11513 
wpa_supplicant_ctrl_iface_process(struct wpa_supplicant * wpa_s,char * buf,size_t * resp_len)11514 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
11515 					 char *buf, size_t *resp_len)
11516 {
11517 	char *reply;
11518 #ifdef CONFIG_OPEN_HARMONY_PATCH
11519 	const int reply_size = 4096 * 10;
11520 #else
11521 	const int reply_size = 4096;
11522 #endif
11523 	int reply_len;
11524 
11525 	if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
11526 	    os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
11527 	    os_strncmp(buf, "PMKSA_ADD ", 10) == 0 ||
11528 	    os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
11529 		if (wpa_debug_show_keys)
11530 			wpa_dbg(wpa_s, MSG_DEBUG,
11531 				"Control interface command '%s'", buf);
11532 		else
11533 			wpa_dbg(wpa_s, MSG_DEBUG,
11534 				"Control interface command '%s [REMOVED]'",
11535 				os_strncmp(buf, WPA_CTRL_RSP,
11536 					   os_strlen(WPA_CTRL_RSP)) == 0 ?
11537 				WPA_CTRL_RSP :
11538 				(os_strncmp(buf, "SET_NETWORK ", 12) == 0 ?
11539 				 "SET_NETWORK" : "key-add"));
11540 	} else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
11541 		   os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
11542 		wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
11543 				      (const u8 *) buf, os_strlen(buf));
11544 	} else {
11545 		int level = wpas_ctrl_cmd_debug_level(buf);
11546 		wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
11547 	}
11548 
11549 	reply = os_malloc(reply_size);
11550 	if (reply == NULL) {
11551 		*resp_len = 1;
11552 		return NULL;
11553 	}
11554 
11555 	os_memcpy(reply, "OK\n", 3);
11556 	reply_len = 3;
11557 
11558 	if (os_strcmp(buf, "PING") == 0) {
11559 		os_memcpy(reply, "PONG\n", 5);
11560 		reply_len = 5;
11561 	} else if (os_strcmp(buf, "IFNAME") == 0) {
11562 		reply_len = os_strlen(wpa_s->ifname);
11563 		os_memcpy(reply, wpa_s->ifname, reply_len);
11564 	} else if (os_strncmp(buf, "RELOG", 5) == 0) {
11565 		if (wpa_debug_reopen_file() < 0)
11566 			reply_len = -1;
11567 	} else if (os_strncmp(buf, "NOTE ", 5) == 0) {
11568 		wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
11569 	} else if (os_strcmp(buf, "MIB") == 0) {
11570 		reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
11571 		if (reply_len >= 0) {
11572 			reply_len += eapol_sm_get_mib(wpa_s->eapol,
11573 						      reply + reply_len,
11574 						      reply_size - reply_len);
11575 #ifdef CONFIG_MACSEC
11576 			reply_len += ieee802_1x_kay_get_mib(
11577 				wpa_s->kay, reply + reply_len,
11578 				reply_size - reply_len);
11579 #endif /* CONFIG_MACSEC */
11580 		}
11581 	} else if (os_strncmp(buf, "STATUS", 6) == 0) {
11582 		reply_len = wpa_supplicant_ctrl_iface_status(
11583 			wpa_s, buf + 6, reply, reply_size);
11584 	} else if (os_strcmp(buf, "PMKSA") == 0) {
11585 		reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
11586 	} else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
11587 		wpas_ctrl_iface_pmksa_flush(wpa_s);
11588 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
11589 	} else if (os_strncmp(buf, "PMKSA_GET ", 10) == 0) {
11590 		reply_len = wpas_ctrl_iface_pmksa_get(wpa_s, buf + 10,
11591 						      reply, reply_size);
11592 	} else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
11593 		if (wpas_ctrl_iface_pmksa_add(wpa_s, buf + 10) < 0)
11594 			reply_len = -1;
11595 #ifdef CONFIG_MESH
11596 	} else if (os_strncmp(buf, "MESH_PMKSA_GET ", 15) == 0) {
11597 		reply_len = wpas_ctrl_iface_mesh_pmksa_get(wpa_s, buf + 15,
11598 							   reply, reply_size);
11599 	} else if (os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
11600 		if (wpas_ctrl_iface_mesh_pmksa_add(wpa_s, buf + 15) < 0)
11601 			reply_len = -1;
11602 #endif /* CONFIG_MESH */
11603 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
11604 	} else if (os_strncmp(buf, "SET ", 4) == 0) {
11605 		if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
11606 			reply_len = -1;
11607 	} else if (os_strncmp(buf, "DUMP", 4) == 0) {
11608 		reply_len = wpa_config_dump_values(wpa_s->conf,
11609 						   reply, reply_size);
11610 	} else if (os_strncmp(buf, "GET ", 4) == 0) {
11611 		reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
11612 							  reply, reply_size);
11613 	} else if (os_strcmp(buf, "LOGON") == 0) {
11614 		eapol_sm_notify_logoff(wpa_s->eapol, false);
11615 	} else if (os_strcmp(buf, "LOGOFF") == 0) {
11616 		eapol_sm_notify_logoff(wpa_s->eapol, true);
11617 	} else if (os_strcmp(buf, "REASSOCIATE") == 0) {
11618 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
11619 			reply_len = -1;
11620 		else
11621 			wpas_request_connection(wpa_s);
11622 	} else if (os_strcmp(buf, "REATTACH") == 0) {
11623 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
11624 		    !wpa_s->current_ssid)
11625 			reply_len = -1;
11626 		else {
11627 			wpa_s->reattach = 1;
11628 			wpas_request_connection(wpa_s);
11629 		}
11630 	} else if (os_strcmp(buf, "RECONNECT") == 0) {
11631 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
11632 			reply_len = -1;
11633 		else if (wpa_s->disconnected)
11634 			wpas_request_connection(wpa_s);
11635 #ifdef IEEE8021X_EAPOL
11636 	} else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
11637 		if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
11638 			reply_len = -1;
11639 #endif /* IEEE8021X_EAPOL */
11640 #ifdef CONFIG_IEEE80211R
11641 	} else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
11642 		if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
11643 			reply_len = -1;
11644 #endif /* CONFIG_IEEE80211R */
11645 #ifdef CONFIG_WPS
11646 	} else if (os_strcmp(buf, "WPS_PBC") == 0) {
11647 		int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
11648 		if (res == -2) {
11649 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
11650 			reply_len = 17;
11651 		} else if (res)
11652 			reply_len = -1;
11653 	} else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
11654 		int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
11655 		if (res == -2) {
11656 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
11657 			reply_len = 17;
11658 		} else if (res)
11659 			reply_len = -1;
11660 	} else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
11661 		reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
11662 							      reply,
11663 							      reply_size);
11664 	} else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
11665 		reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
11666 			wpa_s, buf + 14, reply, reply_size);
11667 	} else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
11668 		if (wpas_wps_cancel(wpa_s))
11669 			reply_len = -1;
11670 #ifdef CONFIG_WPS_NFC
11671 	} else if (os_strcmp(buf, "WPS_NFC") == 0) {
11672 		if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
11673 			reply_len = -1;
11674 	} else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
11675 		if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
11676 			reply_len = -1;
11677 	} else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
11678 		reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
11679 			wpa_s, buf + 21, reply, reply_size);
11680 	} else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
11681 		reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
11682 			wpa_s, buf + 14, reply, reply_size);
11683 	} else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
11684 		if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
11685 							       buf + 17))
11686 			reply_len = -1;
11687 	} else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
11688 		reply_len = wpas_ctrl_nfc_get_handover_req(
11689 			wpa_s, buf + 21, reply, reply_size);
11690 	} else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
11691 		reply_len = wpas_ctrl_nfc_get_handover_sel(
11692 			wpa_s, buf + 21, reply, reply_size);
11693 	} else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
11694 		if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
11695 			reply_len = -1;
11696 #endif /* CONFIG_WPS_NFC */
11697 	} else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
11698 		if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
11699 			reply_len = -1;
11700 #ifdef CONFIG_AP
11701 	} else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
11702 		reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
11703 			wpa_s, buf + 11, reply, reply_size);
11704 #endif /* CONFIG_AP */
11705 #ifdef CONFIG_WPS_ER
11706 	} else if (os_strcmp(buf, "WPS_ER_START") == 0) {
11707 		if (wpas_wps_er_start(wpa_s, NULL))
11708 			reply_len = -1;
11709 	} else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
11710 		if (wpas_wps_er_start(wpa_s, buf + 13))
11711 			reply_len = -1;
11712 	} else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
11713 		wpas_wps_er_stop(wpa_s);
11714 	} else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
11715 		if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
11716 			reply_len = -1;
11717 	} else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
11718 		int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
11719 		if (ret == -2) {
11720 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
11721 			reply_len = 17;
11722 		} else if (ret == -3) {
11723 			os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
11724 			reply_len = 18;
11725 		} else if (ret == -4) {
11726 			os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
11727 			reply_len = 20;
11728 		} else if (ret)
11729 			reply_len = -1;
11730 	} else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
11731 		if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
11732 			reply_len = -1;
11733 	} else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
11734 		if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
11735 								buf + 18))
11736 			reply_len = -1;
11737 	} else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
11738 		if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
11739 			reply_len = -1;
11740 #ifdef CONFIG_WPS_NFC
11741 	} else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
11742 		reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
11743 			wpa_s, buf + 24, reply, reply_size);
11744 #endif /* CONFIG_WPS_NFC */
11745 #endif /* CONFIG_WPS_ER */
11746 #endif /* CONFIG_WPS */
11747 #ifdef CONFIG_IBSS_RSN
11748 	} else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
11749 		if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
11750 			reply_len = -1;
11751 #endif /* CONFIG_IBSS_RSN */
11752 #ifdef CONFIG_MESH
11753 	} else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
11754 		reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
11755 			wpa_s, buf + 19, reply, reply_size);
11756 	} else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
11757 		reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
11758 			wpa_s, "", reply, reply_size);
11759 	} else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
11760 		if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
11761 			reply_len = -1;
11762 	} else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
11763 		if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
11764 								buf + 18))
11765 			reply_len = -1;
11766 	} else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
11767 		if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
11768 			reply_len = -1;
11769 	} else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
11770 		if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
11771 			reply_len = -1;
11772 	} else if (os_strncmp(buf, "MESH_LINK_PROBE ", 16) == 0) {
11773 		if (wpa_supplicant_ctrl_iface_mesh_link_probe(wpa_s, buf + 16))
11774 			reply_len = -1;
11775 #endif /* CONFIG_MESH */
11776 #ifdef CONFIG_P2P
11777 	} else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
11778 		if (p2p_ctrl_find(wpa_s, buf + 8))
11779 			reply_len = -1;
11780 	} else if (os_strcmp(buf, "P2P_FIND") == 0) {
11781 		if (p2p_ctrl_find(wpa_s, ""))
11782 			reply_len = -1;
11783 	} else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
11784 		wpas_p2p_stop_find(wpa_s);
11785 	} else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
11786 		if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
11787 			reply_len = -1;
11788 	} else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
11789 		if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
11790 			reply_len = -1;
11791 #ifdef CONFIG_MAGICLINK
11792 	} else if (os_strncmp(buf, "MAGICLINK ",
11793 			      os_strlen("MAGICLINK ")) == 0) {
11794 		wpa_dbg(wpa_s, MSG_ERROR, "magiclink cmd in");
11795 		if (hw_magiclink_p2p_ctrl_connect(wpa_s,
11796 			      buf + os_strlen("MAGICLINK ")))
11797 			reply_len = -1;
11798 #endif /* CONFIG_MAGICLINK */
11799 	} else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
11800 		reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
11801 					     reply_size);
11802 	} else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
11803 		if (p2p_ctrl_listen(wpa_s, buf + 11))
11804 			reply_len = -1;
11805 	} else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
11806 		if (p2p_ctrl_listen(wpa_s, ""))
11807 			reply_len = -1;
11808 	} else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
11809 		if (wpas_p2p_group_remove(wpa_s, buf + 17))
11810 			reply_len = -1;
11811 	} else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
11812 		if (p2p_ctrl_group_add(wpa_s, ""))
11813 			reply_len = -1;
11814 	} else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
11815 		if (p2p_ctrl_group_add(wpa_s, buf + 14))
11816 			reply_len = -1;
11817 	} else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
11818 		reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
11819 						  reply_size);
11820 	} else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
11821 		if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
11822 			reply_len = -1;
11823 	} else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
11824 		reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
11825 	} else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
11826 		reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
11827 						   reply_size);
11828 	} else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
11829 		if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
11830 			reply_len = -1;
11831 	} else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
11832 		if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
11833 			reply_len = -1;
11834 	} else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
11835 		wpas_p2p_sd_service_update(wpa_s);
11836 	} else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
11837 		if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
11838 			reply_len = -1;
11839 	} else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
11840 		wpas_p2p_service_flush(wpa_s);
11841 	} else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
11842 		if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
11843 			reply_len = -1;
11844 	} else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
11845 		if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
11846 			reply_len = -1;
11847 	} else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
11848 		if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
11849 			reply_len = -1;
11850 	} else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
11851 		if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
11852 			reply_len = -1;
11853 	} else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
11854 		if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
11855 			reply_len = -1;
11856 	} else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
11857 		reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
11858 					      reply_size);
11859 	} else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
11860 		if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
11861 			reply_len = -1;
11862 	} else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
11863 		p2p_ctrl_flush(wpa_s);
11864 	} else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
11865 		if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
11866 			reply_len = -1;
11867 	} else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
11868 		if (wpas_p2p_cancel(wpa_s))
11869 			reply_len = -1;
11870 	} else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
11871 		if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
11872 			reply_len = -1;
11873 	} else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
11874 		if (p2p_ctrl_presence_req(wpa_s, "") < 0)
11875 			reply_len = -1;
11876 	} else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
11877 		if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
11878 			reply_len = -1;
11879 	} else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
11880 		if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
11881 			reply_len = -1;
11882 	} else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
11883 		if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
11884 			reply_len = -1;
11885 	} else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
11886 		if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
11887 			reply_len = -1;
11888 	} else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
11889 		if (wpas_p2p_lo_stop(wpa_s))
11890 			reply_len = -1;
11891 #endif /* CONFIG_P2P */
11892 #ifdef CONFIG_WIFI_DISPLAY
11893 	} else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
11894 		if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
11895 			reply_len = -1;
11896 	} else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
11897 		reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
11898 						     reply, reply_size);
11899 #endif /* CONFIG_WIFI_DISPLAY */
11900 #ifdef CONFIG_INTERWORKING
11901 	} else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
11902 		if (interworking_fetch_anqp(wpa_s) < 0)
11903 			reply_len = -1;
11904 	} else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
11905 		interworking_stop_fetch_anqp(wpa_s);
11906 	} else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
11907 		if (ctrl_interworking_select(wpa_s, NULL) < 0)
11908 			reply_len = -1;
11909 	} else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
11910 		if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
11911 			reply_len = -1;
11912 	} else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
11913 		if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
11914 			reply_len = -1;
11915 	} else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
11916 		int id;
11917 
11918 		id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
11919 		if (id < 0)
11920 			reply_len = -1;
11921 		else {
11922 			reply_len = os_snprintf(reply, reply_size, "%d\n", id);
11923 			if (os_snprintf_error(reply_size, reply_len))
11924 				reply_len = -1;
11925 		}
11926 	} else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
11927 		if (get_anqp(wpa_s, buf + 9) < 0)
11928 			reply_len = -1;
11929 	} else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
11930 		if (gas_request(wpa_s, buf + 12) < 0)
11931 			reply_len = -1;
11932 	} else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
11933 		reply_len = gas_response_get(wpa_s, buf + 17, reply,
11934 					     reply_size);
11935 #endif /* CONFIG_INTERWORKING */
11936 #ifdef CONFIG_HS20
11937 	} else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
11938 		if (get_hs20_anqp(wpa_s, buf + 14) < 0)
11939 			reply_len = -1;
11940 	} else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
11941 		if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
11942 			reply_len = -1;
11943 	} else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
11944 		if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
11945 			reply_len = -1;
11946 	} else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
11947 		if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
11948 			reply_len = -1;
11949 	} else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
11950 		reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
11951 	} else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
11952 		if (del_hs20_icon(wpa_s, buf + 14) < 0)
11953 			reply_len = -1;
11954 	} else if (os_strcmp(buf, "FETCH_OSU") == 0) {
11955 		if (hs20_fetch_osu(wpa_s, 0) < 0)
11956 			reply_len = -1;
11957 	} else if (os_strcmp(buf, "FETCH_OSU no-scan") == 0) {
11958 		if (hs20_fetch_osu(wpa_s, 1) < 0)
11959 			reply_len = -1;
11960 	} else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
11961 		hs20_cancel_fetch_osu(wpa_s);
11962 #endif /* CONFIG_HS20 */
11963 	} else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
11964 	{
11965 		if (wpa_supplicant_ctrl_iface_ctrl_rsp(
11966 			    wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
11967 			reply_len = -1;
11968 		else {
11969 			/*
11970 			 * Notify response from timeout to allow the control
11971 			 * interface response to be sent first.
11972 			 */
11973 			eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
11974 					       wpa_s, NULL);
11975 		}
11976 	} else if (os_strcmp(buf, "RECONFIGURE") == 0) {
11977 		if (wpa_supplicant_reload_configuration(wpa_s))
11978 			reply_len = -1;
11979 #ifdef CONFIG_DRIVER_HDF
11980 	} else if (os_strcmp(buf, "TERMINATE_WITH_RESET_DRIVER") == 0) {
11981 	    wpa_supplicant_terminate_with_reset_driver(wpa_s->global);
11982 #endif
11983 	} else if (os_strcmp(buf, "TERMINATE") == 0) {
11984 		wpa_supplicant_terminate_proc(wpa_s->global);
11985 	} else if (os_strncmp(buf, "BSSID ", 6) == 0) {
11986 		if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
11987 			reply_len = -1;
11988 	} else if (os_strncmp(buf, "BSSID_IGNORE", 12) == 0) {
11989 		reply_len = wpa_supplicant_ctrl_iface_bssid_ignore(
11990 			wpa_s, buf + 12, reply, reply_size);
11991 	} else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
11992 		/* deprecated backwards compatibility alias for BSSID_IGNORE */
11993 		reply_len = wpa_supplicant_ctrl_iface_bssid_ignore(
11994 			wpa_s, buf + 9, reply, reply_size);
11995 	} else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
11996 		reply_len = wpa_supplicant_ctrl_iface_log_level(
11997 			wpa_s, buf + 9, reply, reply_size);
11998 	} else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
11999 		reply_len = wpa_supplicant_ctrl_iface_list_networks(
12000 			wpa_s, buf + 14, reply, reply_size);
12001 	} else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
12002 		reply_len = wpa_supplicant_ctrl_iface_list_networks(
12003 			wpa_s, NULL, reply, reply_size);
12004 	} else if (os_strcmp(buf, "DISCONNECT") == 0) {
12005 		wpas_request_disconnection(wpa_s);
12006 	} else if (os_strcmp(buf, "SCAN") == 0) {
12007 		wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
12008 	} else if (os_strncmp(buf, "SCAN ", 5) == 0) {
12009 		wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
12010 	} else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
12011 		reply_len = wpa_supplicant_ctrl_iface_scan_results(
12012 			wpa_s, reply, reply_size);
12013 	} else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
12014 		if (wpas_abort_ongoing_scan(wpa_s) < 0)
12015 			reply_len = -1;
12016 	} else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
12017 		if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
12018 			reply_len = -1;
12019 	} else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
12020 		if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
12021 			reply_len = -1;
12022 	} else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
12023 		if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
12024 			reply_len = -1;
12025 	} else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
12026 		reply_len = wpa_supplicant_ctrl_iface_add_network(
12027 			wpa_s, reply, reply_size);
12028 	} else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
12029 		if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
12030 			reply_len = -1;
12031 	} else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
12032 		if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
12033 			reply_len = -1;
12034 	} else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
12035 		reply_len = wpa_supplicant_ctrl_iface_get_network(
12036 			wpa_s, buf + 12, reply, reply_size);
12037 	} else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
12038 		if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
12039 							  wpa_s))
12040 			reply_len = -1;
12041 	} else if (os_strcmp(buf, "LIST_CREDS") == 0) {
12042 		reply_len = wpa_supplicant_ctrl_iface_list_creds(
12043 			wpa_s, reply, reply_size);
12044 	} else if (os_strcmp(buf, "ADD_CRED") == 0) {
12045 		reply_len = wpa_supplicant_ctrl_iface_add_cred(
12046 			wpa_s, reply, reply_size);
12047 	} else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
12048 		if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
12049 			reply_len = -1;
12050 	} else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
12051 		if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
12052 			reply_len = -1;
12053 	} else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
12054 		reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
12055 							       reply,
12056 							       reply_size);
12057 #ifndef CONFIG_NO_CONFIG_WRITE
12058 	} else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
12059 		if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
12060 			reply_len = -1;
12061 #endif /* CONFIG_NO_CONFIG_WRITE */
12062 	} else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
12063 		reply_len = wpa_supplicant_ctrl_iface_get_capability(
12064 			wpa_s, buf + 15, reply, reply_size);
12065 	} else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
12066 		if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
12067 			reply_len = -1;
12068 	} else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
12069 		if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
12070 			reply_len = -1;
12071 	} else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
12072 		reply_len = wpa_supplicant_global_iface_list(
12073 			wpa_s->global, reply, reply_size);
12074 	} else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
12075 		reply_len = wpa_supplicant_global_iface_interfaces(
12076 			wpa_s->global, buf + 10, reply, reply_size);
12077 	} else if (os_strncmp(buf, "BSS ", 4) == 0) {
12078 		reply_len = wpa_supplicant_ctrl_iface_bss(
12079 			wpa_s, buf + 4, reply, reply_size);
12080 #ifdef CONFIG_AP
12081 	} else if (os_strcmp(buf, "STA-FIRST") == 0) {
12082 		reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
12083 	} else if (os_strncmp(buf, "STA ", 4) == 0) {
12084 		reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
12085 					      reply_size);
12086 	} else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
12087 		reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
12088 						   reply_size);
12089 	} else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
12090 		if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
12091 			reply_len = -1;
12092 	} else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
12093 		if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
12094 			reply_len = -1;
12095 	} else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
12096 		if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
12097 			reply_len = -1;
12098 	} else if (os_strcmp(buf, "STOP_AP") == 0) {
12099 		if (wpas_ap_stop_ap(wpa_s))
12100 			reply_len = -1;
12101 	} else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
12102 		if (wpas_ap_update_beacon(wpa_s))
12103 			reply_len = -1;
12104 #endif /* CONFIG_AP */
12105 	} else if (os_strcmp(buf, "SUSPEND") == 0) {
12106 		wpas_notify_suspend(wpa_s->global);
12107 	} else if (os_strcmp(buf, "RESUME") == 0) {
12108 		wpas_notify_resume(wpa_s->global);
12109 #ifdef CONFIG_TESTING_OPTIONS
12110 	} else if (os_strcmp(buf, "DROP_SA") == 0) {
12111 		wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
12112 #endif /* CONFIG_TESTING_OPTIONS */
12113 	} else if (os_strncmp(buf, "ROAM ", 5) == 0) {
12114 		if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
12115 			reply_len = -1;
12116 	} else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
12117 		wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
12118 	} else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
12119 		if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
12120 			reply_len = -1;
12121 	} else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
12122 		if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
12123 							       buf + 17))
12124 			reply_len = -1;
12125 	} else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
12126 		wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
12127 #ifdef CONFIG_TDLS
12128 	} else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
12129 		if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
12130 			reply_len = -1;
12131 	} else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
12132 		if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
12133 			reply_len = -1;
12134 	} else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
12135 		if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
12136 			reply_len = -1;
12137 	} else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
12138 		if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
12139 							       buf + 17))
12140 			reply_len = -1;
12141 	} else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
12142 		if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
12143 								      buf + 24))
12144 			reply_len = -1;
12145 	} else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
12146 		reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
12147 			wpa_s, buf + 17, reply, reply_size);
12148 #endif /* CONFIG_TDLS */
12149 	} else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
12150 		reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
12151 	} else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
12152 		if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
12153 			reply_len = -1;
12154 	} else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
12155 		if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
12156 			reply_len = -1;
12157 	} else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
12158 		reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
12159 						       reply_size);
12160 	} else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
12161 		if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
12162 			reply_len = -1;
12163 	} else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
12164 		reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
12165 						       reply_size);
12166 #ifdef CONFIG_AUTOSCAN
12167 	} else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
12168 		if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
12169 			reply_len = -1;
12170 #endif /* CONFIG_AUTOSCAN */
12171 	} else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
12172 		reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
12173 							 reply_size);
12174 	} else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
12175 		reply_len = wpas_ctrl_iface_driver_flags2(wpa_s, reply,
12176 							  reply_size);
12177 #if defined(ANDROID) || defined(CONFIG_DRIVER_NL80211_HISI)
12178 	} else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
12179 		reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
12180 						      reply_size);
12181 #endif /* ANDROID || CONFIG_DRIVER_NL80211_HISI */
12182 	} else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
12183 		reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
12184 						      reply_size);
12185 	} else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
12186 		pmksa_cache_clear_current(wpa_s->wpa);
12187 		eapol_sm_request_reauth(wpa_s->eapol);
12188 #ifdef CONFIG_WNM
12189 	} else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
12190 		if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
12191 			reply_len = -1;
12192 	} else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
12193 		if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
12194 				reply_len = -1;
12195 	} else if (os_strncmp(buf, "COLOC_INTF_REPORT ", 18) == 0) {
12196 		if (wpas_ctrl_iface_coloc_intf_report(wpa_s, buf + 18))
12197 			reply_len = -1;
12198 #endif /* CONFIG_WNM */
12199 	} else if (os_strcmp(buf, "FLUSH") == 0) {
12200 		wpa_supplicant_ctrl_iface_flush(wpa_s);
12201 	} else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
12202 		reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
12203 						 reply_size);
12204 #ifdef CONFIG_TESTING_OPTIONS
12205 	} else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
12206 		if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
12207 			reply_len = -1;
12208 	} else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
12209 		wpas_ctrl_iface_mgmt_tx_done(wpa_s);
12210 	} else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
12211 		if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
12212 			reply_len = -1;
12213 	} else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
12214 		if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
12215 			reply_len = -1;
12216 	} else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
12217 		if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
12218 			reply_len = -1;
12219 	} else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
12220 		if (wpas_ctrl_iface_eapol_tx(wpa_s, buf + 9) < 0)
12221 			reply_len = -1;
12222 	} else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
12223 		if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
12224 			reply_len = -1;
12225 	} else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
12226 		if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
12227 			reply_len = -1;
12228 	} else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
12229 		if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
12230 			reply_len = -1;
12231 	} else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
12232 		if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
12233 			reply_len = -1;
12234 	} else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
12235 		reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
12236 	} else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
12237 		if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0)
12238 			reply_len = -1;
12239 	} else if (os_strcmp(buf, "GET_FAIL") == 0) {
12240 		reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size);
12241 	} else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
12242 		if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
12243 			reply_len = -1;
12244 	} else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
12245 		if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
12246 			reply_len = -1;
12247 	} else if (os_strcmp(buf, "RESET_PN") == 0) {
12248 		if (wpas_ctrl_reset_pn(wpa_s) < 0)
12249 			reply_len = -1;
12250 	} else if (os_strncmp(buf, "KEY_REQUEST ", 12) == 0) {
12251 		if (wpas_ctrl_key_request(wpa_s, buf + 12) < 0)
12252 			reply_len = -1;
12253 	} else if (os_strcmp(buf, "RESEND_ASSOC") == 0) {
12254 		if (wpas_ctrl_resend_assoc(wpa_s) < 0)
12255 			reply_len = -1;
12256 	} else if (os_strcmp(buf, "UNPROT_DEAUTH") == 0) {
12257 		sme_event_unprot_disconnect(
12258 			wpa_s, wpa_s->bssid, NULL,
12259 			WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
12260 	} else if (os_strncmp(buf, "TWT_SETUP ", 10) == 0) {
12261 		if (wpas_ctrl_iface_send_twt_setup(wpa_s, buf + 9))
12262 			reply_len = -1;
12263 	} else if (os_strcmp(buf, "TWT_SETUP") == 0) {
12264 		if (wpas_ctrl_iface_send_twt_setup(wpa_s, ""))
12265 			reply_len = -1;
12266 	} else if (os_strncmp(buf, "TWT_TEARDOWN ", 13) == 0) {
12267 		if (wpas_ctrl_iface_send_twt_teardown(wpa_s, buf + 12))
12268 			reply_len = -1;
12269 	} else if (os_strcmp(buf, "TWT_TEARDOWN") == 0) {
12270 		if (wpas_ctrl_iface_send_twt_teardown(wpa_s, ""))
12271 			reply_len = -1;
12272 #endif /* CONFIG_TESTING_OPTIONS */
12273 	} else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
12274 		if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
12275 			reply_len = -1;
12276 	} else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
12277 		reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
12278 						      reply_size);
12279 	} else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
12280 		if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
12281 			reply_len = -1;
12282 	} else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
12283 		if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
12284 			reply_len = -1;
12285 	} else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
12286 		wpas_ctrl_iface_erp_flush(wpa_s);
12287 	} else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
12288 		if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
12289 			reply_len = -1;
12290 	} else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
12291 		reply_len = wpas_ctrl_iface_get_pref_freq_list(
12292 			wpa_s, buf + 19, reply, reply_size);
12293 #ifdef CONFIG_FILS
12294 	} else if (os_strncmp(buf, "FILS_HLP_REQ_ADD ", 17) == 0) {
12295 		if (wpas_ctrl_iface_fils_hlp_req_add(wpa_s, buf + 17))
12296 			reply_len = -1;
12297 	} else if (os_strcmp(buf, "FILS_HLP_REQ_FLUSH") == 0) {
12298 		wpas_flush_fils_hlp_req(wpa_s);
12299 #endif /* CONFIG_FILS */
12300 #ifdef CONFIG_DPP
12301 	} else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
12302 		int res;
12303 
12304 		res = wpas_dpp_qr_code(wpa_s, buf + 12);
12305 		if (res < 0) {
12306 			reply_len = -1;
12307 		} else {
12308 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12309 			if (os_snprintf_error(reply_size, reply_len))
12310 				reply_len = -1;
12311 		}
12312 	} else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
12313 		int res;
12314 
12315 		res = wpas_dpp_nfc_uri(wpa_s, buf + 12);
12316 		if (res < 0) {
12317 			reply_len = -1;
12318 		} else {
12319 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12320 			if (os_snprintf_error(reply_size, reply_len))
12321 				reply_len = -1;
12322 		}
12323 	} else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
12324 		int res;
12325 
12326 		res = wpas_dpp_nfc_handover_req(wpa_s, buf + 20);
12327 		if (res < 0) {
12328 			reply_len = -1;
12329 		} else {
12330 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12331 			if (os_snprintf_error(reply_size, reply_len))
12332 				reply_len = -1;
12333 		}
12334 	} else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
12335 		int res;
12336 
12337 		res = wpas_dpp_nfc_handover_sel(wpa_s, buf + 20);
12338 		if (res < 0) {
12339 			reply_len = -1;
12340 		} else {
12341 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12342 			if (os_snprintf_error(reply_size, reply_len))
12343 				reply_len = -1;
12344 		}
12345 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
12346 		int res;
12347 
12348 		res = dpp_bootstrap_gen(wpa_s->dpp, buf + 18);
12349 		if (res < 0) {
12350 			reply_len = -1;
12351 		} else {
12352 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12353 			if (os_snprintf_error(reply_size, reply_len))
12354 				reply_len = -1;
12355 		}
12356 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
12357 		if (dpp_bootstrap_remove(wpa_s->dpp, buf + 21) < 0)
12358 			reply_len = -1;
12359 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
12360 		const char *uri;
12361 
12362 		uri = dpp_bootstrap_get_uri(wpa_s->dpp, atoi(buf + 22));
12363 		if (!uri) {
12364 			reply_len = -1;
12365 		} else {
12366 			reply_len = os_snprintf(reply, reply_size, "%s", uri);
12367 			if (os_snprintf_error(reply_size, reply_len))
12368 				reply_len = -1;
12369 		}
12370 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
12371 		reply_len = dpp_bootstrap_info(wpa_s->dpp, atoi(buf + 19),
12372 					       reply, reply_size);
12373 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
12374 		if (dpp_bootstrap_set(wpa_s->dpp, atoi(buf + 18),
12375 				      os_strchr(buf + 18, ' ')) < 0)
12376 			reply_len = -1;
12377 	} else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
12378 		if (wpas_dpp_auth_init(wpa_s, buf + 13) < 0)
12379 			reply_len = -1;
12380 	} else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
12381 		if (wpas_dpp_listen(wpa_s, buf + 11) < 0)
12382 			reply_len = -1;
12383 	} else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
12384 		wpas_dpp_stop(wpa_s);
12385 		wpas_dpp_listen_stop(wpa_s);
12386 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
12387 		int res;
12388 
12389 		res = dpp_configurator_add(wpa_s->dpp, buf + 20);
12390 		if (res < 0) {
12391 			reply_len = -1;
12392 		} else {
12393 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12394 			if (os_snprintf_error(reply_size, reply_len))
12395 				reply_len = -1;
12396 		}
12397 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
12398 		if (dpp_configurator_remove(wpa_s->dpp, buf + 24) < 0)
12399 			reply_len = -1;
12400 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
12401 		if (wpas_dpp_configurator_sign(wpa_s, buf + 21) < 0)
12402 			reply_len = -1;
12403 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
12404 		reply_len = dpp_configurator_get_key_id(wpa_s->dpp,
12405 							atoi(buf + 25),
12406 							reply, reply_size);
12407 	} else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
12408 		int res;
12409 
12410 		res = wpas_dpp_pkex_add(wpa_s, buf + 12);
12411 		if (res < 0) {
12412 			reply_len = -1;
12413 		} else {
12414 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12415 			if (os_snprintf_error(reply_size, reply_len))
12416 				reply_len = -1;
12417 		}
12418 	} else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
12419 		if (wpas_dpp_pkex_remove(wpa_s, buf + 16) < 0)
12420 			reply_len = -1;
12421 #ifdef CONFIG_DPP2
12422 	} else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
12423 		if (wpas_dpp_controller_start(wpa_s, buf + 20) < 0)
12424 			reply_len = -1;
12425 	} else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
12426 		if (wpas_dpp_controller_start(wpa_s, NULL) < 0)
12427 			reply_len = -1;
12428 	} else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
12429 		dpp_controller_stop(wpa_s->dpp);
12430 	} else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
12431 		if (wpas_dpp_chirp(wpa_s, buf + 9) < 0)
12432 			reply_len = -1;
12433 	} else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
12434 		wpas_dpp_chirp_stop(wpa_s);
12435 	} else if (os_strncmp(buf, "DPP_RECONFIG ", 13) == 0) {
12436 		if (wpas_dpp_reconfig(wpa_s, buf + 13) < 0)
12437 			reply_len = -1;
12438 	} else if (os_strncmp(buf, "DPP_CA_SET ", 11) == 0) {
12439 		if (wpas_dpp_ca_set(wpa_s, buf + 10) < 0)
12440 			reply_len = -1;
12441 #endif /* CONFIG_DPP2 */
12442 #endif /* CONFIG_DPP */
12443 	} else if (os_strncmp(buf, "MSCS ", 5) == 0) {
12444 		if (wpas_ctrl_iface_configure_mscs(wpa_s, buf + 5))
12445 			reply_len = -1;
12446 #ifdef CONFIG_PASN
12447 	} else if (os_strncmp(buf, "PASN_START ", 11) == 0) {
12448 		if (wpas_ctrl_iface_pasn_start(wpa_s, buf + 11) < 0)
12449 			reply_len = -1;
12450 	} else if (os_strcmp(buf, "PASN_STOP") == 0) {
12451 		wpas_pasn_auth_stop(wpa_s);
12452 	} else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
12453 		reply_len = ptksa_cache_list(wpa_s->ptksa, reply, reply_size);
12454 	} else if (os_strncmp(buf, "PASN_DEAUTH ", 12) == 0) {
12455 		if (wpas_ctrl_iface_pasn_deauthenticate(wpa_s, buf + 12) < 0)
12456 			reply_len = -1;
12457 #endif /* CONFIG_PASN */
12458 	} else if (os_strncmp(buf, "SCS ", 4) == 0) {
12459 		if (wpas_ctrl_iface_configure_scs(wpa_s, buf + 4))
12460 			reply_len = -1;
12461 	} else if (os_strncmp(buf, "DSCP_RESP ", 10) == 0) {
12462 		if (wpas_ctrl_iface_send_dscp_resp(wpa_s, buf + 10))
12463 			reply_len = -1;
12464 	} else if (os_strncmp(buf, "DSCP_QUERY ", 11) == 0) {
12465 		if (wpas_ctrl_iface_send_dscp_query(wpa_s, buf + 11))
12466 			reply_len = -1;
12467 	} else {
12468 		os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
12469 		reply_len = 16;
12470 	}
12471 
12472 	if (reply_len < 0) {
12473 		os_memcpy(reply, "FAIL\n", 5);
12474 		reply_len = 5;
12475 	}
12476 
12477 	*resp_len = reply_len;
12478 	return reply;
12479 }
12480 
12481 
wpa_supplicant_global_iface_add(struct wpa_global * global,char * cmd)12482 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
12483 					   char *cmd)
12484 {
12485 	struct wpa_interface iface;
12486 	char *pos, *extra;
12487 	struct wpa_supplicant *wpa_s;
12488 	unsigned int create_iface = 0;
12489 	u8 mac_addr[ETH_ALEN];
12490 	enum wpa_driver_if_type type = WPA_IF_STATION;
12491 
12492 	/*
12493 	 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
12494 	 * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
12495 	 */
12496 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
12497 
12498 	os_memset(&iface, 0, sizeof(iface));
12499 
12500 	do {
12501 		iface.ifname = pos = cmd;
12502 		pos = os_strchr(pos, '\t');
12503 		if (pos)
12504 			*pos++ = '\0';
12505 		if (iface.ifname[0] == '\0')
12506 			return -1;
12507 		if (pos == NULL)
12508 			break;
12509 
12510 		iface.confname = pos;
12511 		pos = os_strchr(pos, '\t');
12512 		if (pos)
12513 			*pos++ = '\0';
12514 		if (iface.confname[0] == '\0')
12515 			iface.confname = NULL;
12516 		if (pos == NULL)
12517 			break;
12518 
12519 		iface.driver = pos;
12520 		pos = os_strchr(pos, '\t');
12521 		if (pos)
12522 			*pos++ = '\0';
12523 		if (iface.driver[0] == '\0')
12524 			iface.driver = NULL;
12525 		if (pos == NULL)
12526 			break;
12527 
12528 		iface.ctrl_interface = pos;
12529 		pos = os_strchr(pos, '\t');
12530 		if (pos)
12531 			*pos++ = '\0';
12532 		if (iface.ctrl_interface[0] == '\0')
12533 			iface.ctrl_interface = NULL;
12534 		if (pos == NULL)
12535 			break;
12536 
12537 		iface.driver_param = pos;
12538 		pos = os_strchr(pos, '\t');
12539 		if (pos)
12540 			*pos++ = '\0';
12541 		if (iface.driver_param[0] == '\0')
12542 			iface.driver_param = NULL;
12543 		if (pos == NULL)
12544 			break;
12545 
12546 		iface.bridge_ifname = pos;
12547 		pos = os_strchr(pos, '\t');
12548 		if (pos)
12549 			*pos++ = '\0';
12550 		if (iface.bridge_ifname[0] == '\0')
12551 			iface.bridge_ifname = NULL;
12552 		if (pos == NULL)
12553 			break;
12554 
12555 		extra = pos;
12556 		pos = os_strchr(pos, '\t');
12557 		if (pos)
12558 			*pos++ = '\0';
12559 		if (!extra[0])
12560 			break;
12561 
12562 		if (os_strcmp(extra, "create") == 0) {
12563 			create_iface = 1;
12564 			if (!pos)
12565 				break;
12566 
12567 			if (os_strcmp(pos, "sta") == 0) {
12568 				type = WPA_IF_STATION;
12569 			} else if (os_strcmp(pos, "ap") == 0) {
12570 				type = WPA_IF_AP_BSS;
12571 			} else {
12572 				wpa_printf(MSG_DEBUG,
12573 					   "INTERFACE_ADD unsupported interface type: '%s'",
12574 					   pos);
12575 				return -1;
12576 			}
12577 		} else {
12578 			wpa_printf(MSG_DEBUG,
12579 				   "INTERFACE_ADD unsupported extra parameter: '%s'",
12580 				   extra);
12581 			return -1;
12582 		}
12583 	} while (0);
12584 
12585 	if (create_iface) {
12586 		wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
12587 			   iface.ifname);
12588 		if (!global->ifaces)
12589 			return -1;
12590 		if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
12591 				   NULL, NULL, NULL, mac_addr, NULL) < 0) {
12592 			wpa_printf(MSG_ERROR,
12593 				   "CTRL_IFACE interface creation failed");
12594 			return -1;
12595 		}
12596 
12597 		wpa_printf(MSG_DEBUG,
12598 			   "CTRL_IFACE interface '%s' created with MAC addr: "
12599 			   MACSTR, iface.ifname, MAC2STR(mac_addr));
12600 	}
12601 
12602 	if (wpa_supplicant_get_iface(global, iface.ifname))
12603 		goto fail;
12604 
12605 	wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
12606 	if (!wpa_s)
12607 		goto fail;
12608 	wpa_s->added_vif = create_iface;
12609 	return 0;
12610 
12611 fail:
12612 	if (create_iface)
12613 		wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
12614 	return -1;
12615 }
12616 
12617 
wpa_supplicant_global_iface_remove(struct wpa_global * global,char * cmd)12618 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
12619 					      char *cmd)
12620 {
12621 	struct wpa_supplicant *wpa_s;
12622 	int ret;
12623 	unsigned int delete_iface;
12624 
12625 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
12626 
12627 	wpa_s = wpa_supplicant_get_iface(global, cmd);
12628 	if (wpa_s == NULL)
12629 		return -1;
12630 	delete_iface = wpa_s->added_vif;
12631 	ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
12632 	if (!ret && delete_iface) {
12633 		wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
12634 			   cmd);
12635 		ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
12636 	}
12637 	return ret;
12638 }
12639 
12640 
wpa_free_iface_info(struct wpa_interface_info * iface)12641 static void wpa_free_iface_info(struct wpa_interface_info *iface)
12642 {
12643 	struct wpa_interface_info *prev;
12644 
12645 	while (iface) {
12646 		prev = iface;
12647 		iface = iface->next;
12648 
12649 		os_free(prev->ifname);
12650 		os_free(prev->desc);
12651 		os_free(prev);
12652 	}
12653 }
12654 
12655 
wpa_supplicant_global_iface_list(struct wpa_global * global,char * buf,int len)12656 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
12657 					    char *buf, int len)
12658 {
12659 	int i, res;
12660 	struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
12661 	char *pos, *end;
12662 
12663 	for (i = 0; wpa_drivers[i]; i++) {
12664 		const struct wpa_driver_ops *drv = wpa_drivers[i];
12665 		if (drv->get_interfaces == NULL)
12666 			continue;
12667 		tmp = drv->get_interfaces(global->drv_priv[i]);
12668 		if (tmp == NULL)
12669 			continue;
12670 
12671 		if (last == NULL)
12672 			iface = last = tmp;
12673 		else
12674 			last->next = tmp;
12675 		while (last->next)
12676 			last = last->next;
12677 	}
12678 
12679 	pos = buf;
12680 	end = buf + len;
12681 	for (tmp = iface; tmp; tmp = tmp->next) {
12682 		res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
12683 				  tmp->drv_name, tmp->ifname,
12684 				  tmp->desc ? tmp->desc : "");
12685 		if (os_snprintf_error(end - pos, res)) {
12686 			*pos = '\0';
12687 			break;
12688 		}
12689 		pos += res;
12690 	}
12691 
12692 	wpa_free_iface_info(iface);
12693 
12694 	return pos - buf;
12695 }
12696 
12697 
wpa_supplicant_global_iface_interfaces(struct wpa_global * global,const char * input,char * buf,int len)12698 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
12699 						  const char *input,
12700 						  char *buf, int len)
12701 {
12702 	int res;
12703 	char *pos, *end;
12704 	struct wpa_supplicant *wpa_s;
12705 	int show_ctrl = 0;
12706 
12707 	if (input)
12708 		show_ctrl = !!os_strstr(input, "ctrl");
12709 
12710 	wpa_s = global->ifaces;
12711 	pos = buf;
12712 	end = buf + len;
12713 
12714 	while (wpa_s) {
12715 		if (show_ctrl)
12716 			res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
12717 					  wpa_s->ifname,
12718 					  wpa_s->conf->ctrl_interface ?
12719 					  wpa_s->conf->ctrl_interface : "N/A");
12720 		else
12721 			res = os_snprintf(pos, end - pos, "%s\n",
12722 					  wpa_s->ifname);
12723 
12724 		if (os_snprintf_error(end - pos, res)) {
12725 			*pos = '\0';
12726 			break;
12727 		}
12728 		pos += res;
12729 		wpa_s = wpa_s->next;
12730 	}
12731 	return pos - buf;
12732 }
12733 
12734 
wpas_global_ctrl_iface_ifname(struct wpa_global * global,const char * ifname,char * cmd,size_t * resp_len)12735 static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
12736 					    const char *ifname,
12737 					    char *cmd, size_t *resp_len)
12738 {
12739 	struct wpa_supplicant *wpa_s;
12740 
12741 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
12742 		if (os_strcmp(ifname, wpa_s->ifname) == 0)
12743 			break;
12744 	}
12745 
12746 	if (wpa_s == NULL) {
12747 		char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
12748 		if (resp)
12749 			*resp_len = os_strlen(resp);
12750 		else
12751 			*resp_len = 1;
12752 		return resp;
12753 	}
12754 
12755 	return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
12756 }
12757 
12758 
wpas_global_ctrl_iface_redir_p2p(struct wpa_global * global,char * buf,size_t * resp_len)12759 static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
12760 					       char *buf, size_t *resp_len)
12761 {
12762 #ifdef CONFIG_P2P
12763 	static const char * cmd[] = {
12764 		"LIST_NETWORKS",
12765 		"P2P_FIND",
12766 		"P2P_STOP_FIND",
12767 		"P2P_LISTEN",
12768 		"P2P_GROUP_ADD",
12769 		"P2P_GET_PASSPHRASE",
12770 		"P2P_SERVICE_UPDATE",
12771 		"P2P_SERVICE_FLUSH",
12772 		"P2P_FLUSH",
12773 		"P2P_CANCEL",
12774 		"P2P_PRESENCE_REQ",
12775 		"P2P_EXT_LISTEN",
12776 #ifdef CONFIG_AP
12777 		"STA-FIRST",
12778 #endif /* CONFIG_AP */
12779 		NULL
12780 	};
12781 	static const char * prefix[] = {
12782 #ifdef ANDROID
12783 		"DRIVER ",
12784 #endif /* ANDROID */
12785 		"GET_CAPABILITY ",
12786 		"GET_NETWORK ",
12787 		"REMOVE_NETWORK ",
12788 		"P2P_FIND ",
12789 		"P2P_CONNECT ",
12790 		"P2P_LISTEN ",
12791 		"P2P_GROUP_REMOVE ",
12792 		"P2P_GROUP_ADD ",
12793 		"P2P_GROUP_MEMBER ",
12794 		"P2P_PROV_DISC ",
12795 		"P2P_SERV_DISC_REQ ",
12796 		"P2P_SERV_DISC_CANCEL_REQ ",
12797 		"P2P_SERV_DISC_RESP ",
12798 		"P2P_SERV_DISC_EXTERNAL ",
12799 		"P2P_SERVICE_ADD ",
12800 		"P2P_SERVICE_DEL ",
12801 		"P2P_SERVICE_REP ",
12802 		"P2P_REJECT ",
12803 		"P2P_INVITE ",
12804 		"P2P_PEER ",
12805 		"P2P_SET ",
12806 		"P2P_UNAUTHORIZE ",
12807 		"P2P_PRESENCE_REQ ",
12808 		"P2P_EXT_LISTEN ",
12809 		"P2P_REMOVE_CLIENT ",
12810 		"WPS_NFC_TOKEN ",
12811 		"WPS_NFC_TAG_READ ",
12812 		"NFC_GET_HANDOVER_SEL ",
12813 		"NFC_GET_HANDOVER_REQ ",
12814 		"NFC_REPORT_HANDOVER ",
12815 		"P2P_ASP_PROVISION ",
12816 		"P2P_ASP_PROVISION_RESP ",
12817 #ifdef CONFIG_AP
12818 		"STA ",
12819 		"STA-NEXT ",
12820 #endif /* CONFIG_AP */
12821 #ifdef CONFIG_MAGICLINK
12822 		"MAGICLINK ",
12823 #endif /* CONFIG_MAGICLINK */
12824 		NULL
12825 	};
12826 	int found = 0;
12827 	int i;
12828 
12829 	if (global->p2p_init_wpa_s == NULL)
12830 		return NULL;
12831 
12832 	for (i = 0; !found && cmd[i]; i++) {
12833 		if (os_strcmp(buf, cmd[i]) == 0)
12834 			found = 1;
12835 	}
12836 
12837 	for (i = 0; !found && prefix[i]; i++) {
12838 		if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
12839 			found = 1;
12840 	}
12841 
12842 	if (found)
12843 		return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
12844 							 buf, resp_len);
12845 #endif /* CONFIG_P2P */
12846 	return NULL;
12847 }
12848 
12849 
wpas_global_ctrl_iface_redir_wfd(struct wpa_global * global,char * buf,size_t * resp_len)12850 static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
12851 					       char *buf, size_t *resp_len)
12852 {
12853 #ifdef CONFIG_WIFI_DISPLAY
12854 	if (global->p2p_init_wpa_s == NULL)
12855 		return NULL;
12856 	if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
12857 	    os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
12858 		return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
12859 							 buf, resp_len);
12860 #endif /* CONFIG_WIFI_DISPLAY */
12861 	return NULL;
12862 }
12863 
12864 
wpas_global_ctrl_iface_redir(struct wpa_global * global,char * buf,size_t * resp_len)12865 static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
12866 					   char *buf, size_t *resp_len)
12867 {
12868 	char *ret;
12869 
12870 	ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
12871 	if (ret)
12872 		return ret;
12873 
12874 	ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
12875 	if (ret)
12876 		return ret;
12877 
12878 	return NULL;
12879 }
12880 
12881 
wpas_global_ctrl_iface_set(struct wpa_global * global,char * cmd)12882 static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
12883 {
12884 	char *value;
12885 
12886 	value = os_strchr(cmd, ' ');
12887 	if (value == NULL)
12888 		return -1;
12889 	*value++ = '\0';
12890 
12891 	wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
12892 
12893 #ifdef CONFIG_WIFI_DISPLAY
12894 	if (os_strcasecmp(cmd, "wifi_display") == 0) {
12895 		wifi_display_enable(global, !!atoi(value));
12896 		return 0;
12897 	}
12898 #endif /* CONFIG_WIFI_DISPLAY */
12899 
12900 	/* Restore cmd to its original value to allow redirection */
12901 	value[-1] = ' ';
12902 
12903 	return -1;
12904 }
12905 
12906 
wpas_global_ctrl_iface_dup_network(struct wpa_global * global,char * cmd)12907 static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
12908 					      char *cmd)
12909 {
12910 	struct wpa_supplicant *wpa_s[2]; /* src, dst */
12911 	char *p;
12912 	unsigned int i;
12913 
12914 	/* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
12915 	 * <variable name> */
12916 
12917 	for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
12918 		p = os_strchr(cmd, ' ');
12919 		if (p == NULL)
12920 			return -1;
12921 		*p = '\0';
12922 
12923 		wpa_s[i] = global->ifaces;
12924 		for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
12925 			if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
12926 				break;
12927 		}
12928 
12929 		if (!wpa_s[i]) {
12930 			wpa_printf(MSG_DEBUG,
12931 				   "CTRL_IFACE: Could not find iface=%s", cmd);
12932 			return -1;
12933 		}
12934 
12935 		cmd = p + 1;
12936 	}
12937 
12938 	return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
12939 }
12940 
12941 
12942 #ifndef CONFIG_NO_CONFIG_WRITE
wpas_global_ctrl_iface_save_config(struct wpa_global * global)12943 static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
12944 {
12945 	int ret = 0, saved = 0;
12946 	struct wpa_supplicant *wpa_s;
12947 
12948 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
12949 		if (!wpa_s->conf->update_config) {
12950 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
12951 			continue;
12952 		}
12953 
12954 		if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
12955 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
12956 			ret = 1;
12957 		} else {
12958 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
12959 			saved++;
12960 		}
12961 	}
12962 
12963 	if (!saved && !ret) {
12964 		wpa_dbg(wpa_s, MSG_DEBUG,
12965 			"CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
12966 		ret = 1;
12967 	}
12968 
12969 	return ret;
12970 }
12971 #endif /* CONFIG_NO_CONFIG_WRITE */
12972 
12973 
wpas_global_ctrl_iface_status(struct wpa_global * global,char * buf,size_t buflen)12974 static int wpas_global_ctrl_iface_status(struct wpa_global *global,
12975 					 char *buf, size_t buflen)
12976 {
12977 	char *pos, *end;
12978 	int ret;
12979 	struct wpa_supplicant *wpa_s;
12980 
12981 	pos = buf;
12982 	end = buf + buflen;
12983 
12984 #ifdef CONFIG_P2P
12985 	if (global->p2p && !global->p2p_disabled) {
12986 		ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
12987 				  "\n"
12988 				  "p2p_state=%s\n",
12989 				  MAC2STR(global->p2p_dev_addr),
12990 				  p2p_get_state_txt(global->p2p));
12991 		if (os_snprintf_error(end - pos, ret))
12992 			return pos - buf;
12993 		pos += ret;
12994 	} else if (global->p2p) {
12995 		ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
12996 		if (os_snprintf_error(end - pos, ret))
12997 			return pos - buf;
12998 		pos += ret;
12999 	}
13000 #endif /* CONFIG_P2P */
13001 
13002 #ifdef CONFIG_WIFI_DISPLAY
13003 	ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
13004 			  !!global->wifi_display);
13005 	if (os_snprintf_error(end - pos, ret))
13006 		return pos - buf;
13007 	pos += ret;
13008 #endif /* CONFIG_WIFI_DISPLAY */
13009 
13010 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
13011 		ret = os_snprintf(pos, end - pos, "ifname=%s\n"
13012 				  "address=" MACSTR "\n",
13013 				  wpa_s->ifname, MAC2STR(wpa_s->own_addr));
13014 		if (os_snprintf_error(end - pos, ret))
13015 			return pos - buf;
13016 		pos += ret;
13017 	}
13018 
13019 	return pos - buf;
13020 }
13021 
13022 
13023 #ifdef CONFIG_FST
13024 
wpas_global_ctrl_iface_fst_attach(struct wpa_global * global,char * cmd,char * buf,size_t reply_size)13025 static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
13026 					     char *cmd, char *buf,
13027 					     size_t reply_size)
13028 {
13029 	char ifname[IFNAMSIZ + 1];
13030 	struct fst_iface_cfg cfg;
13031 	struct wpa_supplicant *wpa_s;
13032 	struct fst_wpa_obj iface_obj;
13033 
13034 	if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
13035 		wpa_s = wpa_supplicant_get_iface(global, ifname);
13036 		if (wpa_s) {
13037 			if (wpa_s->fst) {
13038 				wpa_printf(MSG_INFO, "FST: Already attached");
13039 				return -1;
13040 			}
13041 			fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
13042 			wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
13043 						&iface_obj, &cfg);
13044 			if (wpa_s->fst)
13045 				return os_snprintf(buf, reply_size, "OK\n");
13046 		}
13047 	}
13048 
13049 	return -1;
13050 }
13051 
13052 
wpas_global_ctrl_iface_fst_detach(struct wpa_global * global,char * cmd,char * buf,size_t reply_size)13053 static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
13054 					     char *cmd, char *buf,
13055 					     size_t reply_size)
13056 {
13057 	char ifname[IFNAMSIZ + 1];
13058 	struct wpa_supplicant *wpa_s;
13059 
13060 	if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
13061 		wpa_s = wpa_supplicant_get_iface(global, ifname);
13062 		if (wpa_s) {
13063 			if (!fst_iface_detach(ifname)) {
13064 				wpa_s->fst = NULL;
13065 				return os_snprintf(buf, reply_size, "OK\n");
13066 			}
13067 		}
13068 	}
13069 
13070 	return -1;
13071 }
13072 
13073 #endif /* CONFIG_FST */
13074 
13075 #ifdef CONFIG_MAGICLINK
hw_magiclink_ctrl_iface_update_network(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,char * name,char * value)13076 int hw_magiclink_ctrl_iface_update_network(
13077 	struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
13078 	char *name, char *value)
13079 {
13080 	return wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name, value);
13081 }
13082 #endif /* CONFIG_MAGICLINK */
13083 
wpa_supplicant_global_ctrl_iface_process(struct wpa_global * global,char * buf,size_t * resp_len)13084 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
13085 						char *buf, size_t *resp_len)
13086 {
13087 	char *reply;
13088 	const int reply_size = 2048;
13089 	int reply_len;
13090 	int level = MSG_DEBUG;
13091 
13092 	if (os_strncmp(buf, "IFNAME=", 7) == 0) {
13093 		char *pos = os_strchr(buf + 7, ' ');
13094 		if (pos) {
13095 			*pos++ = '\0';
13096 			return wpas_global_ctrl_iface_ifname(global,
13097 							     buf + 7, pos,
13098 							     resp_len);
13099 		}
13100 	}
13101 
13102 	reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
13103 	if (reply)
13104 		return reply;
13105 
13106 	if (os_strcmp(buf, "PING") == 0)
13107 		level = MSG_EXCESSIVE;
13108 	wpa_hexdump_ascii(level, "RX global ctrl_iface",
13109 			  (const u8 *) buf, os_strlen(buf));
13110 
13111 	reply = os_malloc(reply_size);
13112 	if (reply == NULL) {
13113 		*resp_len = 1;
13114 		return NULL;
13115 	}
13116 
13117 	os_memcpy(reply, "OK\n", 3);
13118 	reply_len = 3;
13119 
13120 	if (os_strcmp(buf, "PING") == 0) {
13121 		os_memcpy(reply, "PONG\n", 5);
13122 		reply_len = 5;
13123 	} else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
13124 		if (wpa_supplicant_global_iface_add(global, buf + 14))
13125 			reply_len = -1;
13126 	} else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
13127 		if (wpa_supplicant_global_iface_remove(global, buf + 17))
13128 			reply_len = -1;
13129 	} else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
13130 		reply_len = wpa_supplicant_global_iface_list(
13131 			global, reply, reply_size);
13132 	} else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
13133 		reply_len = wpa_supplicant_global_iface_interfaces(
13134 			global, buf + 10, reply, reply_size);
13135 #ifdef CONFIG_FST
13136 	} else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
13137 		reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
13138 							      reply,
13139 							      reply_size);
13140 	} else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
13141 		reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
13142 							      reply,
13143 							      reply_size);
13144 	} else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
13145 		reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
13146 #endif /* CONFIG_FST */
13147 	} else if (os_strcmp(buf, "TERMINATE") == 0) {
13148 		wpa_supplicant_terminate_proc(global);
13149 	} else if (os_strcmp(buf, "SUSPEND") == 0) {
13150 		wpas_notify_suspend(global);
13151 	} else if (os_strcmp(buf, "RESUME") == 0) {
13152 		wpas_notify_resume(global);
13153 	} else if (os_strncmp(buf, "SET ", 4) == 0) {
13154 		if (wpas_global_ctrl_iface_set(global, buf + 4)) {
13155 #ifdef CONFIG_P2P
13156 			if (global->p2p_init_wpa_s) {
13157 				os_free(reply);
13158 				/* Check if P2P redirection would work for this
13159 				 * command. */
13160 				return wpa_supplicant_ctrl_iface_process(
13161 					global->p2p_init_wpa_s,
13162 					buf, resp_len);
13163 			}
13164 #endif /* CONFIG_P2P */
13165 			reply_len = -1;
13166 		}
13167 	} else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
13168 		if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
13169 			reply_len = -1;
13170 #ifndef CONFIG_NO_CONFIG_WRITE
13171 	} else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
13172 		if (wpas_global_ctrl_iface_save_config(global))
13173 			reply_len = -1;
13174 #endif /* CONFIG_NO_CONFIG_WRITE */
13175 	} else if (os_strcmp(buf, "STATUS") == 0) {
13176 		reply_len = wpas_global_ctrl_iface_status(global, reply,
13177 							  reply_size);
13178 #ifdef CONFIG_MODULE_TESTS
13179 	} else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
13180 		if (wpas_module_tests() < 0)
13181 			reply_len = -1;
13182 #endif /* CONFIG_MODULE_TESTS */
13183 	} else if (os_strncmp(buf, "RELOG", 5) == 0) {
13184 		if (wpa_debug_reopen_file() < 0)
13185 			reply_len = -1;
13186 	} else {
13187 		os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
13188 		reply_len = 16;
13189 	}
13190 
13191 	if (reply_len < 0) {
13192 		os_memcpy(reply, "FAIL\n", 5);
13193 		reply_len = 5;
13194 	}
13195 
13196 	*resp_len = reply_len;
13197 	return reply;
13198 }
13199