1 /*
2  * WPA Supplicant / Control interface (shared code for all backends)
3  * Copyright (c) 2004-2020, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 #ifdef CONFIG_TESTING_OPTIONS
11 #include <netinet/ip.h>
12 #endif /* CONFIG_TESTING_OPTIONS */
13 
14 #include "utils/common.h"
15 #include "utils/eloop.h"
16 #include "utils/uuid.h"
17 #include "utils/module_tests.h"
18 #include "common/version.h"
19 #include "common/ieee802_11_defs.h"
20 #include "common/ieee802_11_common.h"
21 #include "common/wpa_ctrl.h"
22 #ifdef CONFIG_DPP
23 #include "common/dpp.h"
24 #endif /* CONFIG_DPP */
25 #include "common/ptksa_cache.h"
26 #include "crypto/tls.h"
27 #include "ap/hostapd.h"
28 #include "eap_peer/eap.h"
29 #include "eapol_supp/eapol_supp_sm.h"
30 #include "rsn_supp/wpa.h"
31 #include "rsn_supp/preauth.h"
32 #include "rsn_supp/pmksa_cache.h"
33 #include "l2_packet/l2_packet.h"
34 #include "wps/wps.h"
35 #include "fst/fst.h"
36 #include "fst/fst_ctrl_iface.h"
37 #include "config.h"
38 #include "wpa_supplicant_i.h"
39 #include "driver_i.h"
40 #include "wps_supplicant.h"
41 #include "ibss_rsn.h"
42 #include "wpas_glue.h"
43 #include "ap.h"
44 #include "p2p_supplicant.h"
45 #include "p2p/p2p.h"
46 #include "hs20_supplicant.h"
47 #include "wifi_display.h"
48 #include "notify.h"
49 #include "bss.h"
50 #include "scan.h"
51 #include "ctrl_iface.h"
52 #include "interworking.h"
53 #include "bssid_ignore.h"
54 #include "autoscan.h"
55 #include "wnm_sta.h"
56 #include "offchannel.h"
57 #include "drivers/driver.h"
58 #include "mesh.h"
59 #include "dpp_supplicant.h"
60 #include "sme.h"
61 
62 #ifdef __NetBSD__
63 #include <net/if_ether.h>
64 #elif defined(__ZEPHYR__)
65 #include <zephyr/net/ethernet.h>
66 #elif !defined(__CYGWIN__) && !defined(CONFIG_NATIVE_WINDOWS)
67 #include <net/ethernet.h>
68 #endif
69 
70 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
71 					    char *buf, int len);
72 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
73 						  const char *input,
74 						  char *buf, int len);
75 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
76 					char *val);
77 
78 
set_bssid_filter(struct wpa_supplicant * wpa_s,char * val)79 static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
80 {
81 	char *pos;
82 	u8 addr[ETH_ALEN], *filter = NULL, *n;
83 	size_t count = 0;
84 
85 	pos = val;
86 	while (pos) {
87 		if (*pos == '\0')
88 			break;
89 		if (hwaddr_aton(pos, addr)) {
90 			os_free(filter);
91 			return -1;
92 		}
93 		n = os_realloc_array(filter, count + 1, ETH_ALEN);
94 		if (n == NULL) {
95 			os_free(filter);
96 			return -1;
97 		}
98 		filter = n;
99 		os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
100 		count++;
101 
102 		pos = os_strchr(pos, ' ');
103 		if (pos)
104 			pos++;
105 	}
106 
107 	wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
108 	os_free(wpa_s->bssid_filter);
109 	wpa_s->bssid_filter = filter;
110 	wpa_s->bssid_filter_count = count;
111 
112 	return 0;
113 }
114 
115 
set_disallow_aps(struct wpa_supplicant * wpa_s,char * val)116 static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
117 {
118 	char *pos;
119 	u8 addr[ETH_ALEN], *bssid = NULL, *n;
120 	struct wpa_ssid_value *ssid = NULL, *ns;
121 	size_t count = 0, ssid_count = 0;
122 	struct wpa_ssid *c;
123 
124 	/*
125 	 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
126 	 * SSID_SPEC ::= ssid <SSID_HEX>
127 	 * BSSID_SPEC ::= bssid <BSSID_HEX>
128 	 */
129 
130 	pos = val;
131 	while (pos) {
132 		if (*pos == '\0')
133 			break;
134 		if (os_strncmp(pos, "bssid ", 6) == 0) {
135 			int res;
136 			pos += 6;
137 			res = hwaddr_aton2(pos, addr);
138 			if (res < 0) {
139 				os_free(ssid);
140 				os_free(bssid);
141 				wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
142 					   "BSSID value '%s'", pos);
143 				return -1;
144 			}
145 			pos += res;
146 			n = os_realloc_array(bssid, count + 1, ETH_ALEN);
147 			if (n == NULL) {
148 				os_free(ssid);
149 				os_free(bssid);
150 				return -1;
151 			}
152 			bssid = n;
153 			os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
154 			count++;
155 		} else if (os_strncmp(pos, "ssid ", 5) == 0) {
156 			char *end;
157 			pos += 5;
158 
159 			end = pos;
160 			while (*end) {
161 				if (*end == '\0' || *end == ' ')
162 					break;
163 				end++;
164 			}
165 
166 			ns = os_realloc_array(ssid, ssid_count + 1,
167 					      sizeof(struct wpa_ssid_value));
168 			if (ns == NULL) {
169 				os_free(ssid);
170 				os_free(bssid);
171 				return -1;
172 			}
173 			ssid = ns;
174 
175 			if ((end - pos) & 0x01 ||
176 			    end - pos > 2 * SSID_MAX_LEN ||
177 			    hexstr2bin(pos, ssid[ssid_count].ssid,
178 				       (end - pos) / 2) < 0) {
179 				os_free(ssid);
180 				os_free(bssid);
181 				wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
182 					   "SSID value '%s'", pos);
183 				return -1;
184 			}
185 			ssid[ssid_count].ssid_len = (end - pos) / 2;
186 			wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
187 					  ssid[ssid_count].ssid,
188 					  ssid[ssid_count].ssid_len);
189 			ssid_count++;
190 			pos = end;
191 		} else {
192 			wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
193 				   "'%s'", pos);
194 			os_free(ssid);
195 			os_free(bssid);
196 			return -1;
197 		}
198 
199 		pos = os_strchr(pos, ' ');
200 		if (pos)
201 			pos++;
202 	}
203 
204 	wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
205 	os_free(wpa_s->disallow_aps_bssid);
206 	wpa_s->disallow_aps_bssid = bssid;
207 	wpa_s->disallow_aps_bssid_count = count;
208 
209 	wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
210 	os_free(wpa_s->disallow_aps_ssid);
211 	wpa_s->disallow_aps_ssid = ssid;
212 	wpa_s->disallow_aps_ssid_count = ssid_count;
213 
214 	if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
215 		return 0;
216 
217 	c = wpa_s->current_ssid;
218 	if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
219 		return 0;
220 
221 	if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
222 	    !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
223 		return 0;
224 
225 	wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
226 		   "because current AP was marked disallowed");
227 
228 #ifdef CONFIG_SME
229 	wpa_s->sme.prev_bssid_set = 0;
230 #endif /* CONFIG_SME */
231 	wpa_s->reassociate = 1;
232 	wpa_s->own_disconnect_req = 1;
233 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
234 	wpa_supplicant_req_scan(wpa_s, 0, 0);
235 
236 	return 0;
237 }
238 
239 
240 #ifndef CONFIG_NO_CONFIG_BLOBS
wpas_ctrl_set_blob(struct wpa_supplicant * wpa_s,char * pos)241 static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
242 {
243 	char *name = pos;
244 	struct wpa_config_blob *blob;
245 	size_t len;
246 
247 	pos = os_strchr(pos, ' ');
248 	if (pos == NULL)
249 		return -1;
250 	*pos++ = '\0';
251 	len = os_strlen(pos);
252 	if (len & 1)
253 		return -1;
254 
255 	wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
256 	blob = os_zalloc(sizeof(*blob));
257 	if (blob == NULL)
258 		return -1;
259 	blob->name = os_strdup(name);
260 	blob->data = os_malloc(len / 2);
261 	if (blob->name == NULL || blob->data == NULL) {
262 		wpa_config_free_blob(blob);
263 		return -1;
264 	}
265 
266 	if (hexstr2bin(pos, blob->data, len / 2) < 0) {
267 		wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
268 		wpa_config_free_blob(blob);
269 		return -1;
270 	}
271 	blob->len = len / 2;
272 
273 	wpa_config_set_blob(wpa_s->conf, blob);
274 
275 	return 0;
276 }
277 #endif /* CONFIG_NO_CONFIG_BLOBS */
278 
279 
wpas_ctrl_pno(struct wpa_supplicant * wpa_s,char * cmd)280 static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
281 {
282 	char *params;
283 	char *pos;
284 	int *freqs = NULL;
285 	int ret;
286 
287 	if (atoi(cmd)) {
288 		params = os_strchr(cmd, ' ');
289 		os_free(wpa_s->manual_sched_scan_freqs);
290 		if (params) {
291 			params++;
292 			pos = os_strstr(params, "freq=");
293 			if (pos)
294 				freqs = freq_range_to_channel_list(wpa_s,
295 								   pos + 5);
296 		}
297 		wpa_s->manual_sched_scan_freqs = freqs;
298 		ret = wpas_start_pno(wpa_s);
299 	} else {
300 		ret = wpas_stop_pno(wpa_s);
301 	}
302 	return ret;
303 }
304 
305 
wpas_ctrl_set_band(struct wpa_supplicant * wpa_s,char * bands)306 static int wpas_ctrl_set_band(struct wpa_supplicant *wpa_s, char *bands)
307 {
308 	union wpa_event_data event;
309 	u32 setband_mask = WPA_SETBAND_AUTO;
310 
311 	/*
312 	 * For example:
313 	 *  SET setband 2G,6G
314 	 *  SET setband 5G
315 	 *  SET setband AUTO
316 	 */
317 	if (!os_strstr(bands, "AUTO")) {
318 		if (os_strstr(bands, "5G"))
319 			setband_mask |= WPA_SETBAND_5G;
320 		if (os_strstr(bands, "6G"))
321 			setband_mask |= WPA_SETBAND_6G;
322 		if (os_strstr(bands, "2G"))
323 			setband_mask |= WPA_SETBAND_2G;
324 		if (setband_mask == WPA_SETBAND_AUTO)
325 			return -1;
326 	}
327 
328 	wpa_s->setband_mask = setband_mask;
329 	if (wpa_drv_setband(wpa_s, wpa_s->setband_mask) == 0) {
330 		os_memset(&event, 0, sizeof(event));
331 		event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
332 		event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
333 		wpa_supplicant_event(wpa_s, EVENT_CHANNEL_LIST_CHANGED, &event);
334 	}
335 
336 	return 0;
337 }
338 
339 
wpas_ctrl_iface_set_lci(struct wpa_supplicant * wpa_s,const char * cmd)340 static int wpas_ctrl_iface_set_lci(struct wpa_supplicant *wpa_s,
341 				   const char *cmd)
342 {
343 	struct wpabuf *lci;
344 
345 	if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
346 		wpabuf_free(wpa_s->lci);
347 		wpa_s->lci = NULL;
348 		return 0;
349 	}
350 
351 	lci = wpabuf_parse_bin(cmd);
352 	if (!lci)
353 		return -1;
354 
355 	if (os_get_reltime(&wpa_s->lci_time)) {
356 		wpabuf_free(lci);
357 		return -1;
358 	}
359 
360 	wpabuf_free(wpa_s->lci);
361 	wpa_s->lci = lci;
362 
363 	return 0;
364 }
365 
366 
367 static int
wpas_ctrl_set_relative_rssi(struct wpa_supplicant * wpa_s,const char * cmd)368 wpas_ctrl_set_relative_rssi(struct wpa_supplicant *wpa_s, const char *cmd)
369 {
370 	int relative_rssi;
371 
372 	if (os_strcmp(cmd, "disable") == 0) {
373 		wpa_s->srp.relative_rssi_set = 0;
374 		return 0;
375 	}
376 
377 	relative_rssi = atoi(cmd);
378 	if (relative_rssi < 0 || relative_rssi > 100)
379 		return -1;
380 	wpa_s->srp.relative_rssi = relative_rssi;
381 	wpa_s->srp.relative_rssi_set = 1;
382 	return 0;
383 }
384 
385 
wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant * wpa_s,const char * cmd)386 static int wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant *wpa_s,
387 					      const char *cmd)
388 {
389 	char *pos;
390 	int adjust_rssi;
391 
392 	/* <band>:adjust_value */
393 	pos = os_strchr(cmd, ':');
394 	if (!pos)
395 		return -1;
396 	pos++;
397 	adjust_rssi = atoi(pos);
398 	if (adjust_rssi < -100 || adjust_rssi > 100)
399 		return -1;
400 
401 	if (os_strncmp(cmd, "2G", 2) == 0)
402 		wpa_s->srp.relative_adjust_band = WPA_SETBAND_2G;
403 	else if (os_strncmp(cmd, "5G", 2) == 0)
404 		wpa_s->srp.relative_adjust_band = WPA_SETBAND_5G;
405 	else
406 		return -1;
407 
408 	wpa_s->srp.relative_adjust_rssi = adjust_rssi;
409 
410 	return 0;
411 }
412 
413 
wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant * wpa_s,const char * cmd)414 static int wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant *wpa_s,
415 				   const char *cmd)
416 {
417 	struct wpabuf *ric_ies;
418 
419 	if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
420 		wpabuf_free(wpa_s->ric_ies);
421 		wpa_s->ric_ies = NULL;
422 		return 0;
423 	}
424 
425 	ric_ies = wpabuf_parse_bin(cmd);
426 	if (!ric_ies)
427 		return -1;
428 
429 	wpabuf_free(wpa_s->ric_ies);
430 	wpa_s->ric_ies = ric_ies;
431 
432 	return 0;
433 }
434 
435 
436 #ifdef CONFIG_TESTING_OPTIONS
wpas_ctrl_iface_set_dso(struct wpa_supplicant * wpa_s,const char * val)437 static int wpas_ctrl_iface_set_dso(struct wpa_supplicant *wpa_s,
438 				   const char *val)
439 {
440 	u8 bssid[ETH_ALEN];
441 	const char *pos = val;
442 	struct driver_signal_override *dso = NULL, *tmp, parsed;
443 
444 	if (hwaddr_aton(pos, bssid))
445 		return -1;
446 	pos = os_strchr(pos, ' ');
447 
448 	dl_list_for_each(tmp, &wpa_s->drv_signal_override,
449 			 struct driver_signal_override, list) {
450 		if (os_memcmp(bssid, tmp->bssid, ETH_ALEN) == 0) {
451 			dso = tmp;
452 			break;
453 		}
454 	}
455 
456 	if (!pos) {
457 		/* Remove existing entry */
458 		if (dso) {
459 			dl_list_del(&dso->list);
460 			os_free(dso);
461 		}
462 		return 0;
463 	}
464 	pos++;
465 
466 	/* Update an existing entry or add a new one */
467 	os_memset(&parsed, 0, sizeof(parsed));
468 	if (sscanf(pos, "%d %d %d %d %d",
469 		   &parsed.si_current_signal,
470 		   &parsed.si_avg_signal,
471 		   &parsed.si_avg_beacon_signal,
472 		   &parsed.si_current_noise,
473 		   &parsed.scan_level) != 5)
474 		return -1;
475 
476 	if (!dso) {
477 		dso = os_zalloc(sizeof(*dso));
478 		if (!dso)
479 			return -1;
480 		os_memcpy(dso->bssid, bssid, ETH_ALEN);
481 		dl_list_add(&wpa_s->drv_signal_override, &dso->list);
482 	}
483 	dso->si_current_signal = parsed.si_current_signal;
484 	dso->si_avg_signal = parsed.si_avg_signal;
485 	dso->si_avg_beacon_signal = parsed.si_avg_beacon_signal;
486 	dso->si_current_noise = parsed.si_current_noise;
487 	dso->scan_level = parsed.scan_level;
488 
489 	return 0;
490 }
491 #endif /* CONFIG_TESTING_OPTIONS */
492 
493 
wpa_supplicant_ctrl_iface_set(struct wpa_supplicant * wpa_s,char * cmd)494 static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
495 					 char *cmd)
496 {
497 	char *value;
498 	int ret = 0;
499 
500 	value = os_strchr(cmd, ' ');
501 	if (value == NULL)
502 		return -1;
503 	*value++ = '\0';
504 
505 	wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
506 	if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
507 		eapol_sm_configure(wpa_s->eapol,
508 				   atoi(value), -1, -1, -1);
509 	} else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
510 		eapol_sm_configure(wpa_s->eapol,
511 				   -1, atoi(value), -1, -1);
512 	} else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
513 		eapol_sm_configure(wpa_s->eapol,
514 				   -1, -1, atoi(value), -1);
515 	} else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
516 		eapol_sm_configure(wpa_s->eapol,
517 				   -1, -1, -1, atoi(value));
518 #ifdef CONFIG_TESTING_OPTIONS
519 	} else if (os_strcasecmp(cmd, "EAPOL::portControl") == 0) {
520 		if (os_strcmp(value, "Auto") == 0)
521 			eapol_sm_notify_portControl(wpa_s->eapol, Auto);
522 		else if (os_strcmp(value, "ForceUnauthorized") == 0)
523 			eapol_sm_notify_portControl(wpa_s->eapol,
524 						    ForceUnauthorized);
525 		else if (os_strcmp(value, "ForceAuthorized") == 0)
526 			eapol_sm_notify_portControl(wpa_s->eapol,
527 						    ForceAuthorized);
528 		else
529 			ret = -1;
530 #endif /* CONFIG_TESTING_OPTIONS */
531 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
532 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
533 				     atoi(value))) {
534 			ret = -1;
535 		} else {
536 			value[-1] = '=';
537 			wpa_config_process_global(wpa_s->conf, cmd, -1);
538 		}
539 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
540 		   0) {
541 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
542 				     atoi(value))) {
543 			ret = -1;
544 		} else {
545 			value[-1] = '=';
546 			wpa_config_process_global(wpa_s->conf, cmd, -1);
547 		}
548 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
549 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
550 				     atoi(value))) {
551 			ret = -1;
552 		} else {
553 			value[-1] = '=';
554 			wpa_config_process_global(wpa_s->conf, cmd, -1);
555 		}
556 	} else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
557 		wpa_s->wps_fragment_size = atoi(value);
558 #ifdef CONFIG_WPS_TESTING
559 	} else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
560 		long int val;
561 		val = strtol(value, NULL, 0);
562 		if (val < 0 || val > 0xff) {
563 			ret = -1;
564 			wpa_printf(MSG_DEBUG, "WPS: Invalid "
565 				   "wps_version_number %ld", val);
566 		} else {
567 			wps_version_number = val;
568 			wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
569 				   "version %u.%u",
570 				   (wps_version_number & 0xf0) >> 4,
571 				   wps_version_number & 0x0f);
572 		}
573 	} else if (os_strcasecmp(cmd, "wps_testing_stub_cred") == 0) {
574 		wps_testing_stub_cred = atoi(value);
575 		wpa_printf(MSG_DEBUG, "WPS: Testing - stub_cred=%d",
576 			   wps_testing_stub_cred);
577 	} else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
578 		wps_corrupt_pkhash = atoi(value);
579 		wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
580 			   wps_corrupt_pkhash);
581 	} else if (os_strcasecmp(cmd, "wps_force_auth_types") == 0) {
582 		if (value[0] == '\0') {
583 			wps_force_auth_types_in_use = 0;
584 		} else {
585 			wps_force_auth_types = strtol(value, NULL, 0);
586 			wps_force_auth_types_in_use = 1;
587 		}
588 	} else if (os_strcasecmp(cmd, "wps_force_encr_types") == 0) {
589 		if (value[0] == '\0') {
590 			wps_force_encr_types_in_use = 0;
591 		} else {
592 			wps_force_encr_types = strtol(value, NULL, 0);
593 			wps_force_encr_types_in_use = 1;
594 		}
595 #endif /* CONFIG_WPS_TESTING */
596 	} else if (os_strcasecmp(cmd, "ampdu") == 0) {
597 		if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
598 			ret = -1;
599 #ifdef CONFIG_TDLS
600 #ifdef CONFIG_TDLS_TESTING
601 	} else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
602 		tdls_testing = strtol(value, NULL, 0);
603 		wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
604 #endif /* CONFIG_TDLS_TESTING */
605 	} else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
606 		int disabled = atoi(value);
607 		wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
608 		if (disabled) {
609 			if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
610 				ret = -1;
611 		} else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
612 			ret = -1;
613 		wpa_tdls_enable(wpa_s->wpa, !disabled);
614 #endif /* CONFIG_TDLS */
615 	} else if (os_strcasecmp(cmd, "pno") == 0) {
616 		ret = wpas_ctrl_pno(wpa_s, value);
617 	} else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
618 		int disabled = atoi(value);
619 		if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
620 			ret = -1;
621 		else if (disabled)
622 			wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
623 	} else if (os_strcasecmp(cmd, "uapsd") == 0) {
624 		if (os_strcmp(value, "disable") == 0)
625 			wpa_s->set_sta_uapsd = 0;
626 		else {
627 			int be, bk, vi, vo;
628 			char *pos;
629 			/* format: BE,BK,VI,VO;max SP Length */
630 			be = atoi(value);
631 			pos = os_strchr(value, ',');
632 			if (pos == NULL)
633 				return -1;
634 			pos++;
635 			bk = atoi(pos);
636 			pos = os_strchr(pos, ',');
637 			if (pos == NULL)
638 				return -1;
639 			pos++;
640 			vi = atoi(pos);
641 			pos = os_strchr(pos, ',');
642 			if (pos == NULL)
643 				return -1;
644 			pos++;
645 			vo = atoi(pos);
646 			/* ignore max SP Length for now */
647 
648 			wpa_s->set_sta_uapsd = 1;
649 			wpa_s->sta_uapsd = 0;
650 			if (be)
651 				wpa_s->sta_uapsd |= BIT(0);
652 			if (bk)
653 				wpa_s->sta_uapsd |= BIT(1);
654 			if (vi)
655 				wpa_s->sta_uapsd |= BIT(2);
656 			if (vo)
657 				wpa_s->sta_uapsd |= BIT(3);
658 		}
659 	} else if (os_strcasecmp(cmd, "ps") == 0) {
660 		ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
661 #ifdef CONFIG_WIFI_DISPLAY
662 	} else if (os_strcasecmp(cmd, "wifi_display") == 0) {
663 		int enabled = !!atoi(value);
664 		if (enabled && !wpa_s->global->p2p)
665 			ret = -1;
666 		else
667 			wifi_display_enable(wpa_s->global, enabled);
668 #endif /* CONFIG_WIFI_DISPLAY */
669 	} else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
670 		ret = set_bssid_filter(wpa_s, value);
671 	} else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
672 		ret = set_disallow_aps(wpa_s, value);
673 	} else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
674 		wpa_s->no_keep_alive = !!atoi(value);
675 #ifdef CONFIG_DPP
676 	} else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
677 		os_free(wpa_s->dpp_configurator_params);
678 		wpa_s->dpp_configurator_params = os_strdup(value);
679 	} else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
680 		wpa_s->dpp_init_max_tries = atoi(value);
681 	} else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
682 		wpa_s->dpp_init_retry_time = atoi(value);
683 	} else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
684 		wpa_s->dpp_resp_wait_time = atoi(value);
685 	} else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
686 		wpa_s->dpp_resp_max_tries = atoi(value);
687 	} else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
688 		wpa_s->dpp_resp_retry_time = atoi(value);
689 #ifdef CONFIG_TESTING_OPTIONS
690 	} else if (os_strcasecmp(cmd, "dpp_pkex_own_mac_override") == 0) {
691 		if (hwaddr_aton(value, dpp_pkex_own_mac_override))
692 			ret = -1;
693 	} else if (os_strcasecmp(cmd, "dpp_pkex_peer_mac_override") == 0) {
694 		if (hwaddr_aton(value, dpp_pkex_peer_mac_override))
695 			ret = -1;
696 	} else if (os_strcasecmp(cmd, "dpp_pkex_ephemeral_key_override") == 0) {
697 		size_t hex_len = os_strlen(value);
698 
699 		if (hex_len >
700 		    2 * sizeof(dpp_pkex_ephemeral_key_override))
701 			ret = -1;
702 		else if (hexstr2bin(value, dpp_pkex_ephemeral_key_override,
703 				    hex_len / 2))
704 			ret = -1;
705 		else
706 			dpp_pkex_ephemeral_key_override_len = hex_len / 2;
707 	} else if (os_strcasecmp(cmd, "dpp_protocol_key_override") == 0) {
708 		size_t hex_len = os_strlen(value);
709 
710 		if (hex_len > 2 * sizeof(dpp_protocol_key_override))
711 			ret = -1;
712 		else if (hexstr2bin(value, dpp_protocol_key_override,
713 				    hex_len / 2))
714 			ret = -1;
715 		else
716 			dpp_protocol_key_override_len = hex_len / 2;
717 	} else if (os_strcasecmp(cmd, "dpp_nonce_override") == 0) {
718 		size_t hex_len = os_strlen(value);
719 
720 		if (hex_len > 2 * sizeof(dpp_nonce_override))
721 			ret = -1;
722 		else if (hexstr2bin(value, dpp_nonce_override, hex_len / 2))
723 			ret = -1;
724 		else
725 			dpp_nonce_override_len = hex_len / 2;
726 	} else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
727 		dpp_version_override = atoi(value);
728 #endif /* CONFIG_TESTING_OPTIONS */
729 #endif /* CONFIG_DPP */
730 #ifdef CONFIG_TESTING_OPTIONS
731 	} else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
732 		wpa_s->ext_mgmt_frame_handling = !!atoi(value);
733 	} else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
734 		wpa_s->ext_eapol_frame_io = !!atoi(value);
735 #ifdef CONFIG_AP
736 		if (wpa_s->ap_iface) {
737 			wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
738 				wpa_s->ext_eapol_frame_io;
739 		}
740 #endif /* CONFIG_AP */
741 	} else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
742 		wpa_s->extra_roc_dur = atoi(value);
743 	} else if (os_strcasecmp(cmd, "test_failure") == 0) {
744 		wpa_s->test_failure = atoi(value);
745 	} else if (os_strcasecmp(cmd, "p2p_go_csa_on_inv") == 0) {
746 		wpa_s->p2p_go_csa_on_inv = !!atoi(value);
747 	} else if (os_strcasecmp(cmd, "ignore_auth_resp") == 0) {
748 		wpa_s->ignore_auth_resp = !!atoi(value);
749 	} else if (os_strcasecmp(cmd, "ignore_assoc_disallow") == 0) {
750 		wpa_s->ignore_assoc_disallow = !!atoi(value);
751 		wpa_drv_ignore_assoc_disallow(wpa_s,
752 					      wpa_s->ignore_assoc_disallow);
753 	} else if (os_strcasecmp(cmd, "disable_sa_query") == 0) {
754 		wpa_s->disable_sa_query = !!atoi(value);
755 	} else if (os_strcasecmp(cmd, "ignore_sae_h2e_only") == 0) {
756 		wpa_s->ignore_sae_h2e_only = !!atoi(value);
757 	} else if (os_strcasecmp(cmd, "extra_sae_rejected_groups") == 0) {
758 		char *pos;
759 
760 		os_free(wpa_s->extra_sae_rejected_groups);
761 		wpa_s->extra_sae_rejected_groups = NULL;
762 		pos = value;
763 		while (pos && pos[0]) {
764 			int group;
765 
766 			group = atoi(pos);
767 			wpa_printf(MSG_DEBUG,
768 				   "TESTING: Extra rejection of SAE group %d",
769 				   group);
770 			if (group)
771 				int_array_add_unique(
772 					&wpa_s->extra_sae_rejected_groups,
773 					group);
774 			pos = os_strchr(pos, ' ');
775 			if (!pos)
776 				break;
777 			pos++;
778 		}
779 	} else if (os_strcasecmp(cmd, "ft_rsnxe_used") == 0) {
780 		wpa_s->ft_rsnxe_used = atoi(value);
781 	} else if (os_strcasecmp(cmd, "oci_freq_override_eapol") == 0) {
782 		wpa_s->oci_freq_override_eapol = atoi(value);
783 	} else if (os_strcasecmp(cmd, "oci_freq_override_saquery_req") == 0) {
784 		wpa_s->oci_freq_override_saquery_req = atoi(value);
785 	} else if (os_strcasecmp(cmd, "oci_freq_override_saquery_resp") == 0) {
786 		wpa_s->oci_freq_override_saquery_resp = atoi(value);
787 	} else if (os_strcasecmp(cmd, "oci_freq_override_eapol_g2") == 0) {
788 		wpa_s->oci_freq_override_eapol_g2 = atoi(value);
789 		/* Populate value to wpa_sm if already associated. */
790 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_EAPOL_G2,
791 				 wpa_s->oci_freq_override_eapol_g2);
792 	} else if (os_strcasecmp(cmd, "oci_freq_override_ft_assoc") == 0) {
793 		wpa_s->oci_freq_override_ft_assoc = atoi(value);
794 		/* Populate value to wpa_sm if already associated. */
795 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCI_FREQ_FT_ASSOC,
796 				 wpa_s->oci_freq_override_ft_assoc);
797 	} else if (os_strcasecmp(cmd, "oci_freq_override_fils_assoc") == 0) {
798 		wpa_s->oci_freq_override_fils_assoc = atoi(value);
799 	} else if (os_strcasecmp(cmd, "oci_freq_override_wnm_sleep") == 0) {
800 		wpa_s->oci_freq_override_wnm_sleep = atoi(value);
801 	} else if (os_strcasecmp(cmd, "rsne_override_eapol") == 0) {
802 		wpabuf_free(wpa_s->rsne_override_eapol);
803 		if (os_strcmp(value, "NULL") == 0)
804 			wpa_s->rsne_override_eapol = NULL;
805 		else
806 			wpa_s->rsne_override_eapol = wpabuf_parse_bin(value);
807 	} else if (os_strcasecmp(cmd, "rsnxe_override_assoc") == 0) {
808 		wpabuf_free(wpa_s->rsnxe_override_assoc);
809 		if (os_strcmp(value, "NULL") == 0)
810 			wpa_s->rsnxe_override_assoc = NULL;
811 		else
812 			wpa_s->rsnxe_override_assoc = wpabuf_parse_bin(value);
813 	} else if (os_strcasecmp(cmd, "rsnxe_override_eapol") == 0) {
814 		wpabuf_free(wpa_s->rsnxe_override_eapol);
815 		if (os_strcmp(value, "NULL") == 0)
816 			wpa_s->rsnxe_override_eapol = NULL;
817 		else
818 			wpa_s->rsnxe_override_eapol = wpabuf_parse_bin(value);
819 	} else if (os_strcasecmp(cmd, "reject_btm_req_reason") == 0) {
820 		wpa_s->reject_btm_req_reason = atoi(value);
821 	} else if (os_strcasecmp(cmd, "get_pref_freq_list_override") == 0) {
822 		os_free(wpa_s->get_pref_freq_list_override);
823 		if (!value[0])
824 			wpa_s->get_pref_freq_list_override = NULL;
825 		else
826 			wpa_s->get_pref_freq_list_override = os_strdup(value);
827 	} else if (os_strcasecmp(cmd, "sae_commit_override") == 0) {
828 		wpabuf_free(wpa_s->sae_commit_override);
829 		if (value[0] == '\0')
830 			wpa_s->sae_commit_override = NULL;
831 		else
832 			wpa_s->sae_commit_override = wpabuf_parse_bin(value);
833 	} else if (os_strcasecmp(cmd, "driver_signal_override") == 0) {
834 		ret = wpas_ctrl_iface_set_dso(wpa_s, value);
835 	} else if (os_strcasecmp(cmd, "disable_scs_support") == 0) {
836 		wpa_s->disable_scs_support = !!atoi(value);
837 	} else if (os_strcasecmp(cmd, "disable_mscs_support") == 0) {
838 		wpa_s->disable_mscs_support = !!atoi(value);
839 #ifdef CONFIG_DPP
840 	} else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
841 		os_free(wpa_s->dpp_config_obj_override);
842 		if (value[0] == '\0')
843 			wpa_s->dpp_config_obj_override = NULL;
844 		else
845 			wpa_s->dpp_config_obj_override = os_strdup(value);
846 	} else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
847 		os_free(wpa_s->dpp_discovery_override);
848 		if (value[0] == '\0')
849 			wpa_s->dpp_discovery_override = NULL;
850 		else
851 			wpa_s->dpp_discovery_override = os_strdup(value);
852 	} else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
853 		os_free(wpa_s->dpp_groups_override);
854 		if (value[0] == '\0')
855 			wpa_s->dpp_groups_override = NULL;
856 		else
857 			wpa_s->dpp_groups_override = os_strdup(value);
858 	} else if (os_strcasecmp(cmd,
859 				 "dpp_ignore_netaccesskey_mismatch") == 0) {
860 		wpa_s->dpp_ignore_netaccesskey_mismatch = atoi(value);
861 	} else if (os_strcasecmp(cmd, "dpp_test") == 0) {
862 		dpp_test = atoi(value);
863 #endif /* CONFIG_DPP */
864 #endif /* CONFIG_TESTING_OPTIONS */
865 #ifdef CONFIG_FILS
866 	} else if (os_strcasecmp(cmd, "disable_fils") == 0) {
867 		wpa_s->disable_fils = !!atoi(value);
868 		wpa_drv_disable_fils(wpa_s, wpa_s->disable_fils);
869 		wpa_supplicant_set_default_scan_ies(wpa_s);
870 #endif /* CONFIG_FILS */
871 #ifndef CONFIG_NO_CONFIG_BLOBS
872 	} else if (os_strcmp(cmd, "blob") == 0) {
873 		ret = wpas_ctrl_set_blob(wpa_s, value);
874 #endif /* CONFIG_NO_CONFIG_BLOBS */
875 	} else if (os_strcasecmp(cmd, "setband") == 0) {
876 		ret = wpas_ctrl_set_band(wpa_s, value);
877 #ifdef CONFIG_MBO
878 	} else if (os_strcasecmp(cmd, "non_pref_chan") == 0) {
879 		ret = wpas_mbo_update_non_pref_chan(wpa_s, value);
880 		if (ret == 0) {
881 			value[-1] = '=';
882 			wpa_config_process_global(wpa_s->conf, cmd, -1);
883 		}
884 	} else if (os_strcasecmp(cmd, "mbo_cell_capa") == 0) {
885 		wpas_mbo_update_cell_capa(wpa_s, atoi(value));
886 	} else if (os_strcasecmp(cmd, "oce") == 0) {
887 		wpa_s->conf->oce = atoi(value);
888 		if (wpa_s->conf->oce) {
889 			if ((wpa_s->conf->oce & OCE_STA) &&
890 			    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA))
891 				wpa_s->enable_oce = OCE_STA;
892 
893 			if ((wpa_s->conf->oce & OCE_STA_CFON) &&
894 			    (wpa_s->drv_flags &
895 			     WPA_DRIVER_FLAGS_OCE_STA_CFON)) {
896 				/* TODO: Need to add STA-CFON support */
897 				wpa_printf(MSG_ERROR,
898 					   "OCE STA-CFON feature is not yet supported");
899 				return -1;
900 			}
901 		} else {
902 			wpa_s->enable_oce = 0;
903 		}
904 		wpa_supplicant_set_default_scan_ies(wpa_s);
905 #endif /* CONFIG_MBO */
906 	} else if (os_strcasecmp(cmd, "lci") == 0) {
907 		ret = wpas_ctrl_iface_set_lci(wpa_s, value);
908 	} else if (os_strcasecmp(cmd, "tdls_trigger_control") == 0) {
909 		ret = wpa_drv_set_tdls_mode(wpa_s, atoi(value));
910 	} else if (os_strcasecmp(cmd, "relative_rssi") == 0) {
911 		ret = wpas_ctrl_set_relative_rssi(wpa_s, value);
912 	} else if (os_strcasecmp(cmd, "relative_band_adjust") == 0) {
913 		ret = wpas_ctrl_set_relative_band_adjust(wpa_s, value);
914 	} else if (os_strcasecmp(cmd, "ric_ies") == 0) {
915 		ret = wpas_ctrl_iface_set_ric_ies(wpa_s, value);
916 	} else if (os_strcasecmp(cmd, "roaming") == 0) {
917 		ret = wpa_drv_roaming(wpa_s, atoi(value), NULL);
918 #ifdef CONFIG_WNM
919 	} else if (os_strcasecmp(cmd, "coloc_intf_elems") == 0) {
920 		struct wpabuf *elems;
921 
922 		elems = wpabuf_parse_bin(value);
923 		if (!elems)
924 			return -1;
925 		wnm_set_coloc_intf_elems(wpa_s, elems);
926 #endif /* CONFIG_WNM */
927 	} else if (os_strcasecmp(cmd, "enable_dscp_policy_capa") == 0) {
928 		wpa_s->enable_dscp_policy_capa = !!atoi(value);
929 	} else {
930 		value[-1] = '=';
931 		ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
932 		if (ret == 0)
933 			wpa_supplicant_update_config(wpa_s);
934 		else if (ret == 1)
935 			ret = 0;
936 	}
937 
938 	return ret;
939 }
940 
941 
wpa_supplicant_ctrl_iface_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)942 static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
943 					 char *cmd, char *buf, size_t buflen)
944 {
945 	int res = -1;
946 
947 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
948 
949 	if (os_strcmp(cmd, "version") == 0) {
950 		res = os_snprintf(buf, buflen, "%s", VERSION_STR);
951 	} else if (os_strcasecmp(cmd, "max_command_len") == 0) {
952 		res = os_snprintf(buf, buflen, "%u", CTRL_IFACE_MAX_LEN);
953 	} else if (os_strcasecmp(cmd, "country") == 0) {
954 		if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
955 			res = os_snprintf(buf, buflen, "%c%c",
956 					  wpa_s->conf->country[0],
957 					  wpa_s->conf->country[1]);
958 #ifdef CONFIG_WIFI_DISPLAY
959 	} else if (os_strcasecmp(cmd, "wifi_display") == 0) {
960 		int enabled;
961 		if (wpa_s->global->p2p == NULL ||
962 		    wpa_s->global->p2p_disabled)
963 			enabled = 0;
964 		else
965 			enabled = wpa_s->global->wifi_display;
966 		res = os_snprintf(buf, buflen, "%d", enabled);
967 #endif /* CONFIG_WIFI_DISPLAY */
968 #ifdef CONFIG_TESTING_GET_GTK
969 	} else if (os_strcmp(cmd, "gtk") == 0) {
970 		if (wpa_s->last_gtk_len == 0)
971 			return -1;
972 		res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
973 				       wpa_s->last_gtk_len);
974 		return res;
975 #endif /* CONFIG_TESTING_GET_GTK */
976 	} else if (os_strcmp(cmd, "tls_library") == 0) {
977 		res = tls_get_library_version(buf, buflen);
978 #ifdef CONFIG_TESTING_OPTIONS
979 	} else if (os_strcmp(cmd, "anonce") == 0) {
980 		return wpa_snprintf_hex(buf, buflen,
981 					wpa_sm_get_anonce(wpa_s->wpa),
982 					WPA_NONCE_LEN);
983 	} else if (os_strcasecmp(cmd, "last_tk_key_idx") == 0) {
984 		res = os_snprintf(buf, buflen, "%d", wpa_s->last_tk_key_idx);
985 #endif /* CONFIG_TESTING_OPTIONS */
986 	} else {
987 		res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
988 	}
989 
990 	if (os_snprintf_error(buflen, res))
991 		return -1;
992 	return res;
993 }
994 
995 
996 #ifdef IEEE8021X_EAPOL
wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant * wpa_s,char * addr)997 static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
998 					     char *addr)
999 {
1000 	u8 bssid[ETH_ALEN];
1001 	struct wpa_ssid *ssid = wpa_s->current_ssid;
1002 
1003 	if (hwaddr_aton(addr, bssid)) {
1004 		wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
1005 			   "'%s'", addr);
1006 		return -1;
1007 	}
1008 
1009 	wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
1010 	rsn_preauth_deinit(wpa_s->wpa);
1011 	if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
1012 		return -1;
1013 
1014 	return 0;
1015 }
1016 #endif /* IEEE8021X_EAPOL */
1017 
1018 
1019 #ifdef CONFIG_TDLS
1020 
wpa_supplicant_ctrl_iface_tdls_discover(struct wpa_supplicant * wpa_s,char * addr)1021 static int wpa_supplicant_ctrl_iface_tdls_discover(
1022 	struct wpa_supplicant *wpa_s, char *addr)
1023 {
1024 	u8 peer[ETH_ALEN];
1025 	int ret;
1026 
1027 	if (hwaddr_aton(addr, peer)) {
1028 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
1029 			   "address '%s'", addr);
1030 		return -1;
1031 	}
1032 
1033 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
1034 		   MAC2STR(peer));
1035 
1036 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1037 		ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
1038 	else
1039 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
1040 
1041 	return ret;
1042 }
1043 
1044 
wpa_supplicant_ctrl_iface_tdls_setup(struct wpa_supplicant * wpa_s,char * addr)1045 static int wpa_supplicant_ctrl_iface_tdls_setup(
1046 	struct wpa_supplicant *wpa_s, char *addr)
1047 {
1048 	u8 peer[ETH_ALEN];
1049 	int ret;
1050 
1051 	if (hwaddr_aton(addr, peer)) {
1052 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
1053 			   "address '%s'", addr);
1054 		return -1;
1055 	}
1056 
1057 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
1058 		   MAC2STR(peer));
1059 
1060 	if ((wpa_s->conf->tdls_external_control) &&
1061 	    wpa_tdls_is_external_setup(wpa_s->wpa))
1062 		return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
1063 
1064 	wpa_tdls_remove(wpa_s->wpa, peer);
1065 
1066 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1067 		ret = wpa_tdls_start(wpa_s->wpa, peer);
1068 	else
1069 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
1070 
1071 	return ret;
1072 }
1073 
1074 
wpa_supplicant_ctrl_iface_tdls_teardown(struct wpa_supplicant * wpa_s,char * addr)1075 static int wpa_supplicant_ctrl_iface_tdls_teardown(
1076 	struct wpa_supplicant *wpa_s, char *addr)
1077 {
1078 	u8 peer[ETH_ALEN];
1079 	int ret;
1080 
1081 	if (os_strcmp(addr, "*") == 0) {
1082 		/* remove everyone */
1083 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
1084 		wpa_tdls_teardown_peers(wpa_s->wpa);
1085 		return 0;
1086 	}
1087 
1088 	if (hwaddr_aton(addr, peer)) {
1089 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
1090 			   "address '%s'", addr);
1091 		return -1;
1092 	}
1093 
1094 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
1095 		   MAC2STR(peer));
1096 
1097 	if ((wpa_s->conf->tdls_external_control) &&
1098 	    wpa_tdls_is_external_setup(wpa_s->wpa))
1099 		return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
1100 
1101 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1102 		ret = wpa_tdls_teardown_link(
1103 			wpa_s->wpa, peer,
1104 			WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
1105 	else
1106 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
1107 
1108 	return ret;
1109 }
1110 
1111 
ctrl_iface_get_capability_tdls(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)1112 static int ctrl_iface_get_capability_tdls(
1113 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1114 {
1115 	int ret;
1116 
1117 	ret = os_snprintf(buf, buflen, "%s\n",
1118 			  wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
1119 			  (wpa_s->drv_flags &
1120 			   WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
1121 			   "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
1122 	if (os_snprintf_error(buflen, ret))
1123 		return -1;
1124 	return ret;
1125 }
1126 
1127 
wpa_supplicant_ctrl_iface_tdls_chan_switch(struct wpa_supplicant * wpa_s,char * cmd)1128 static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
1129 	struct wpa_supplicant *wpa_s, char *cmd)
1130 {
1131 	u8 peer[ETH_ALEN];
1132 	struct hostapd_freq_params freq_params;
1133 	u8 oper_class;
1134 	char *pos, *end;
1135 
1136 	if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
1137 		wpa_printf(MSG_INFO,
1138 			   "tdls_chanswitch: Only supported with external setup");
1139 		return -1;
1140 	}
1141 
1142 	os_memset(&freq_params, 0, sizeof(freq_params));
1143 
1144 	pos = os_strchr(cmd, ' ');
1145 	if (pos == NULL)
1146 		return -1;
1147 	*pos++ = '\0';
1148 
1149 	oper_class = strtol(pos, &end, 10);
1150 	if (pos == end) {
1151 		wpa_printf(MSG_INFO,
1152 			   "tdls_chanswitch: Invalid op class provided");
1153 		return -1;
1154 	}
1155 
1156 	pos = end;
1157 	freq_params.freq = atoi(pos);
1158 	if (freq_params.freq == 0) {
1159 		wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
1160 		return -1;
1161 	}
1162 
1163 #define SET_FREQ_SETTING(str) \
1164 	do { \
1165 		const char *pos2 = os_strstr(pos, " " #str "="); \
1166 		if (pos2) { \
1167 			pos2 += sizeof(" " #str "=") - 1; \
1168 			freq_params.str = atoi(pos2); \
1169 		} \
1170 	} while (0)
1171 
1172 	SET_FREQ_SETTING(center_freq1);
1173 	SET_FREQ_SETTING(center_freq2);
1174 	SET_FREQ_SETTING(bandwidth);
1175 	SET_FREQ_SETTING(sec_channel_offset);
1176 #undef SET_FREQ_SETTING
1177 
1178 	freq_params.ht_enabled = !!os_strstr(pos, " ht");
1179 	freq_params.vht_enabled = !!os_strstr(pos, " vht");
1180 
1181 	if (hwaddr_aton(cmd, peer)) {
1182 		wpa_printf(MSG_DEBUG,
1183 			   "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
1184 			   cmd);
1185 		return -1;
1186 	}
1187 
1188 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
1189 		   " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
1190 		   MAC2STR(peer), oper_class, freq_params.freq,
1191 		   freq_params.center_freq1, freq_params.center_freq2,
1192 		   freq_params.bandwidth, freq_params.sec_channel_offset,
1193 		   freq_params.ht_enabled ? " HT" : "",
1194 		   freq_params.vht_enabled ? " VHT" : "");
1195 
1196 	return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
1197 					   &freq_params);
1198 }
1199 
1200 
wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(struct wpa_supplicant * wpa_s,char * cmd)1201 static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
1202 	struct wpa_supplicant *wpa_s, char *cmd)
1203 {
1204 	u8 peer[ETH_ALEN];
1205 
1206 	if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
1207 		wpa_printf(MSG_INFO,
1208 			   "tdls_chanswitch: Only supported with external setup");
1209 		return -1;
1210 	}
1211 
1212 	if (hwaddr_aton(cmd, peer)) {
1213 		wpa_printf(MSG_DEBUG,
1214 			   "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
1215 			   cmd);
1216 		return -1;
1217 	}
1218 
1219 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
1220 		   MAC2STR(peer));
1221 
1222 	return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
1223 }
1224 
1225 
wpa_supplicant_ctrl_iface_tdls_link_status(struct wpa_supplicant * wpa_s,const char * addr,char * buf,size_t buflen)1226 static int wpa_supplicant_ctrl_iface_tdls_link_status(
1227 	struct wpa_supplicant *wpa_s, const char *addr,
1228 	char *buf, size_t buflen)
1229 {
1230 	u8 peer[ETH_ALEN];
1231 	const char *tdls_status;
1232 	int ret;
1233 
1234 	if (hwaddr_aton(addr, peer)) {
1235 		wpa_printf(MSG_DEBUG,
1236 			   "CTRL_IFACE TDLS_LINK_STATUS: Invalid address '%s'",
1237 			   addr);
1238 		return -1;
1239 	}
1240 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS " MACSTR,
1241 		   MAC2STR(peer));
1242 
1243 	tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
1244 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS: %s", tdls_status);
1245 	ret = os_snprintf(buf, buflen, "TDLS link status: %s\n", tdls_status);
1246 	if (os_snprintf_error(buflen, ret))
1247 		return -1;
1248 
1249 	return ret;
1250 }
1251 
1252 #endif /* CONFIG_TDLS */
1253 
1254 
wmm_ac_ctrl_addts(struct wpa_supplicant * wpa_s,char * cmd)1255 static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
1256 {
1257 	char *token, *context = NULL;
1258 	struct wmm_ac_ts_setup_params params = {
1259 		.tsid = 0xff,
1260 		.direction = 0xff,
1261 	};
1262 
1263 	while ((token = str_token(cmd, " ", &context))) {
1264 		if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
1265 		    sscanf(token, "up=%i", &params.user_priority) == 1 ||
1266 		    sscanf(token, "nominal_msdu_size=%i",
1267 			   &params.nominal_msdu_size) == 1 ||
1268 		    sscanf(token, "mean_data_rate=%i",
1269 			   &params.mean_data_rate) == 1 ||
1270 		    sscanf(token, "min_phy_rate=%i",
1271 			   &params.minimum_phy_rate) == 1 ||
1272 		    sscanf(token, "sba=%i",
1273 			   &params.surplus_bandwidth_allowance) == 1)
1274 			continue;
1275 
1276 		if (os_strcasecmp(token, "downlink") == 0) {
1277 			params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
1278 		} else if (os_strcasecmp(token, "uplink") == 0) {
1279 			params.direction = WMM_TSPEC_DIRECTION_UPLINK;
1280 		} else if (os_strcasecmp(token, "bidi") == 0) {
1281 			params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
1282 		} else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
1283 			params.fixed_nominal_msdu = 1;
1284 		} else {
1285 			wpa_printf(MSG_DEBUG,
1286 				   "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
1287 				   token);
1288 			return -1;
1289 		}
1290 
1291 	}
1292 
1293 	return wpas_wmm_ac_addts(wpa_s, &params);
1294 }
1295 
1296 
wmm_ac_ctrl_delts(struct wpa_supplicant * wpa_s,char * cmd)1297 static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
1298 {
1299 	u8 tsid = atoi(cmd);
1300 
1301 	return wpas_wmm_ac_delts(wpa_s, tsid);
1302 }
1303 
1304 
1305 #ifdef CONFIG_IEEE80211R
wpa_supplicant_ctrl_iface_ft_ds(struct wpa_supplicant * wpa_s,char * addr)1306 static int wpa_supplicant_ctrl_iface_ft_ds(
1307 	struct wpa_supplicant *wpa_s, char *addr)
1308 {
1309 	u8 target_ap[ETH_ALEN];
1310 	struct wpa_bss *bss;
1311 	const u8 *mdie;
1312 
1313 	if (hwaddr_aton(addr, target_ap)) {
1314 		wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
1315 			   "address '%s'", addr);
1316 		return -1;
1317 	}
1318 
1319 	wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
1320 
1321 	bss = wpa_bss_get_bssid(wpa_s, target_ap);
1322 	if (bss)
1323 		mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1324 	else
1325 		mdie = NULL;
1326 
1327 	return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
1328 }
1329 #endif /* CONFIG_IEEE80211R */
1330 
1331 
1332 #ifdef CONFIG_WPS
wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant * wpa_s,char * cmd)1333 static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
1334 					     char *cmd)
1335 {
1336 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1337 #ifdef CONFIG_P2P
1338 	u8 p2p_dev_addr[ETH_ALEN];
1339 #endif /* CONFIG_P2P */
1340 #ifdef CONFIG_AP
1341 	u8 *_p2p_dev_addr = NULL;
1342 #endif /* CONFIG_AP */
1343 	char *pos;
1344 	int multi_ap = 0;
1345 
1346 	if (!cmd || os_strcmp(cmd, "any") == 0 ||
1347 	    os_strncmp(cmd, "any ", 4) == 0) {
1348 		_bssid = NULL;
1349 #ifdef CONFIG_P2P
1350 	} else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
1351 		if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
1352 			wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
1353 				   "P2P Device Address '%s'",
1354 				   cmd + 13);
1355 			return -1;
1356 		}
1357 		_p2p_dev_addr = p2p_dev_addr;
1358 #endif /* CONFIG_P2P */
1359 	} else if (os_strncmp(cmd, "multi_ap=", 9) == 0) {
1360 		_bssid = NULL;
1361 		multi_ap = atoi(cmd + 9);
1362 	} else if (hwaddr_aton(cmd, bssid)) {
1363 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
1364 			   cmd);
1365 		return -1;
1366 	}
1367 
1368 	if (cmd) {
1369 		pos = os_strstr(cmd, " multi_ap=");
1370 		if (pos) {
1371 			pos += 10;
1372 			multi_ap = atoi(pos);
1373 		}
1374 	}
1375 
1376 #ifdef CONFIG_AP
1377 	if (wpa_s->ap_iface)
1378 		return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
1379 #endif /* CONFIG_AP */
1380 
1381 	return wpas_wps_start_pbc(wpa_s, _bssid, 0, multi_ap);
1382 }
1383 
1384 
wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1385 static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
1386 					     char *cmd, char *buf,
1387 					     size_t buflen)
1388 {
1389 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1390 	char *pin;
1391 	int ret;
1392 
1393 	pin = os_strchr(cmd, ' ');
1394 	if (pin)
1395 		*pin++ = '\0';
1396 
1397 	if (os_strcmp(cmd, "any") == 0)
1398 		_bssid = NULL;
1399 	else if (os_strcmp(cmd, "get") == 0) {
1400 		if (wps_generate_pin((unsigned int *) &ret) < 0)
1401 			return -1;
1402 		goto done;
1403 	} else if (hwaddr_aton(cmd, bssid)) {
1404 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
1405 			   cmd);
1406 		return -1;
1407 	}
1408 
1409 #ifdef CONFIG_AP
1410 	if (wpa_s->ap_iface) {
1411 		int timeout = 0;
1412 		char *pos;
1413 
1414 		if (pin) {
1415 			pos = os_strchr(pin, ' ');
1416 			if (pos) {
1417 				*pos++ = '\0';
1418 				timeout = atoi(pos);
1419 			}
1420 		}
1421 
1422 		return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
1423 						 buf, buflen, timeout);
1424 	}
1425 #endif /* CONFIG_AP */
1426 
1427 	if (pin) {
1428 		ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
1429 					 DEV_PW_DEFAULT);
1430 		if (ret < 0)
1431 			return -1;
1432 		ret = os_snprintf(buf, buflen, "%s", pin);
1433 		if (os_snprintf_error(buflen, ret))
1434 			return -1;
1435 		return ret;
1436 	}
1437 
1438 	ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
1439 	if (ret < 0)
1440 		return -1;
1441 
1442 done:
1443 	/* Return the generated PIN */
1444 	ret = os_snprintf(buf, buflen, "%08d", ret);
1445 	if (os_snprintf_error(buflen, ret))
1446 		return -1;
1447 	return ret;
1448 }
1449 
1450 
wpa_supplicant_ctrl_iface_wps_check_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1451 static int wpa_supplicant_ctrl_iface_wps_check_pin(
1452 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1453 {
1454 	char pin[9];
1455 	size_t len;
1456 	char *pos;
1457 	int ret;
1458 
1459 	wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
1460 			      (u8 *) cmd, os_strlen(cmd));
1461 	for (pos = cmd, len = 0; *pos != '\0'; pos++) {
1462 		if (*pos < '0' || *pos > '9')
1463 			continue;
1464 		pin[len++] = *pos;
1465 		if (len == 9) {
1466 			wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
1467 			return -1;
1468 		}
1469 	}
1470 	if (len != 4 && len != 8) {
1471 		wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
1472 		return -1;
1473 	}
1474 	pin[len] = '\0';
1475 
1476 	if (len == 8) {
1477 		unsigned int pin_val;
1478 		pin_val = atoi(pin);
1479 		if (!wps_pin_valid(pin_val)) {
1480 			wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
1481 			ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
1482 			if (os_snprintf_error(buflen, ret))
1483 				return -1;
1484 			return ret;
1485 		}
1486 	}
1487 
1488 	ret = os_snprintf(buf, buflen, "%s", pin);
1489 	if (os_snprintf_error(buflen, ret))
1490 		return -1;
1491 
1492 	return ret;
1493 }
1494 
1495 
1496 #ifdef CONFIG_WPS_NFC
1497 
wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant * wpa_s,char * cmd)1498 static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
1499 					     char *cmd)
1500 {
1501 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1502 
1503 	if (cmd == NULL || cmd[0] == '\0')
1504 		_bssid = NULL;
1505 	else if (hwaddr_aton(cmd, bssid))
1506 		return -1;
1507 
1508 	return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
1509 				  0, 0);
1510 }
1511 
1512 
wpa_supplicant_ctrl_iface_wps_nfc_config_token(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1513 static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
1514 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1515 {
1516 	int ndef;
1517 	struct wpabuf *buf;
1518 	int res;
1519 	char *pos;
1520 
1521 	pos = os_strchr(cmd, ' ');
1522 	if (pos)
1523 		*pos++ = '\0';
1524 	if (os_strcmp(cmd, "WPS") == 0)
1525 		ndef = 0;
1526 	else if (os_strcmp(cmd, "NDEF") == 0)
1527 		ndef = 1;
1528 	else
1529 		return -1;
1530 
1531 	buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
1532 	if (buf == NULL)
1533 		return -1;
1534 
1535 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1536 					 wpabuf_len(buf));
1537 	reply[res++] = '\n';
1538 	reply[res] = '\0';
1539 
1540 	wpabuf_free(buf);
1541 
1542 	return res;
1543 }
1544 
1545 
wpa_supplicant_ctrl_iface_wps_nfc_token(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1546 static int wpa_supplicant_ctrl_iface_wps_nfc_token(
1547 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1548 {
1549 	int ndef;
1550 	struct wpabuf *buf;
1551 	int res;
1552 
1553 	if (os_strcmp(cmd, "WPS") == 0)
1554 		ndef = 0;
1555 	else if (os_strcmp(cmd, "NDEF") == 0)
1556 		ndef = 1;
1557 	else
1558 		return -1;
1559 
1560 	buf = wpas_wps_nfc_token(wpa_s, ndef);
1561 	if (buf == NULL)
1562 		return -1;
1563 
1564 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1565 					 wpabuf_len(buf));
1566 	reply[res++] = '\n';
1567 	reply[res] = '\0';
1568 
1569 	wpabuf_free(buf);
1570 
1571 	return res;
1572 }
1573 
1574 
wpa_supplicant_ctrl_iface_wps_nfc_tag_read(struct wpa_supplicant * wpa_s,char * pos)1575 static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
1576 	struct wpa_supplicant *wpa_s, char *pos)
1577 {
1578 	size_t len;
1579 	struct wpabuf *buf;
1580 	int ret;
1581 	char *freq;
1582 	int forced_freq = 0;
1583 
1584 	freq = strstr(pos, " freq=");
1585 	if (freq) {
1586 		*freq = '\0';
1587 		freq += 6;
1588 		forced_freq = atoi(freq);
1589 	}
1590 
1591 	len = os_strlen(pos);
1592 	if (len & 0x01)
1593 		return -1;
1594 	len /= 2;
1595 
1596 	buf = wpabuf_alloc(len);
1597 	if (buf == NULL)
1598 		return -1;
1599 	if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
1600 		wpabuf_free(buf);
1601 		return -1;
1602 	}
1603 
1604 	ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
1605 	wpabuf_free(buf);
1606 
1607 	return ret;
1608 }
1609 
1610 
wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef)1611 static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
1612 					      char *reply, size_t max_len,
1613 					      int ndef)
1614 {
1615 	struct wpabuf *buf;
1616 	int res;
1617 
1618 	buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
1619 	if (buf == NULL)
1620 		return -1;
1621 
1622 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1623 					 wpabuf_len(buf));
1624 	reply[res++] = '\n';
1625 	reply[res] = '\0';
1626 
1627 	wpabuf_free(buf);
1628 
1629 	return res;
1630 }
1631 
1632 
1633 #ifdef CONFIG_P2P
wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef)1634 static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
1635 					      char *reply, size_t max_len,
1636 					      int ndef)
1637 {
1638 	struct wpabuf *buf;
1639 	int res;
1640 
1641 	buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
1642 	if (buf == NULL) {
1643 		wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
1644 		return -1;
1645 	}
1646 
1647 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1648 					 wpabuf_len(buf));
1649 	reply[res++] = '\n';
1650 	reply[res] = '\0';
1651 
1652 	wpabuf_free(buf);
1653 
1654 	return res;
1655 }
1656 #endif /* CONFIG_P2P */
1657 
1658 
wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1659 static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
1660 					  char *cmd, char *reply,
1661 					  size_t max_len)
1662 {
1663 	char *pos;
1664 	int ndef;
1665 
1666 	pos = os_strchr(cmd, ' ');
1667 	if (pos == NULL)
1668 		return -1;
1669 	*pos++ = '\0';
1670 
1671 	if (os_strcmp(cmd, "WPS") == 0)
1672 		ndef = 0;
1673 	else if (os_strcmp(cmd, "NDEF") == 0)
1674 		ndef = 1;
1675 	else
1676 		return -1;
1677 
1678 	if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
1679 		if (!ndef)
1680 			return -1;
1681 		return wpas_ctrl_nfc_get_handover_req_wps(
1682 			wpa_s, reply, max_len, ndef);
1683 	}
1684 
1685 #ifdef CONFIG_P2P
1686 	if (os_strcmp(pos, "P2P-CR") == 0) {
1687 		return wpas_ctrl_nfc_get_handover_req_p2p(
1688 			wpa_s, reply, max_len, ndef);
1689 	}
1690 #endif /* CONFIG_P2P */
1691 
1692 	return -1;
1693 }
1694 
1695 
wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef,int cr,char * uuid)1696 static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
1697 					      char *reply, size_t max_len,
1698 					      int ndef, int cr, char *uuid)
1699 {
1700 	struct wpabuf *buf;
1701 	int res;
1702 
1703 	buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
1704 	if (buf == NULL)
1705 		return -1;
1706 
1707 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1708 					 wpabuf_len(buf));
1709 	reply[res++] = '\n';
1710 	reply[res] = '\0';
1711 
1712 	wpabuf_free(buf);
1713 
1714 	return res;
1715 }
1716 
1717 
1718 #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)1719 static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1720 					      char *reply, size_t max_len,
1721 					      int ndef, int tag)
1722 {
1723 	struct wpabuf *buf;
1724 	int res;
1725 
1726 	buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1727 	if (buf == NULL)
1728 		return -1;
1729 
1730 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1731 					 wpabuf_len(buf));
1732 	reply[res++] = '\n';
1733 	reply[res] = '\0';
1734 
1735 	wpabuf_free(buf);
1736 
1737 	return res;
1738 }
1739 #endif /* CONFIG_P2P */
1740 
1741 
wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1742 static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1743 					  char *cmd, char *reply,
1744 					  size_t max_len)
1745 {
1746 	char *pos, *pos2;
1747 	int ndef;
1748 
1749 	pos = os_strchr(cmd, ' ');
1750 	if (pos == NULL)
1751 		return -1;
1752 	*pos++ = '\0';
1753 
1754 	if (os_strcmp(cmd, "WPS") == 0)
1755 		ndef = 0;
1756 	else if (os_strcmp(cmd, "NDEF") == 0)
1757 		ndef = 1;
1758 	else
1759 		return -1;
1760 
1761 	pos2 = os_strchr(pos, ' ');
1762 	if (pos2)
1763 		*pos2++ = '\0';
1764 	if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
1765 		if (!ndef)
1766 			return -1;
1767 		return wpas_ctrl_nfc_get_handover_sel_wps(
1768 			wpa_s, reply, max_len, ndef,
1769 			os_strcmp(pos, "WPS-CR") == 0, pos2);
1770 	}
1771 
1772 #ifdef CONFIG_P2P
1773 	if (os_strcmp(pos, "P2P-CR") == 0) {
1774 		return wpas_ctrl_nfc_get_handover_sel_p2p(
1775 			wpa_s, reply, max_len, ndef, 0);
1776 	}
1777 
1778 	if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1779 		return wpas_ctrl_nfc_get_handover_sel_p2p(
1780 			wpa_s, reply, max_len, ndef, 1);
1781 	}
1782 #endif /* CONFIG_P2P */
1783 
1784 	return -1;
1785 }
1786 
1787 
wpas_ctrl_nfc_report_handover(struct wpa_supplicant * wpa_s,char * cmd)1788 static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1789 					 char *cmd)
1790 {
1791 	size_t len;
1792 	struct wpabuf *req, *sel;
1793 	int ret;
1794 	char *pos, *role, *type, *pos2;
1795 #ifdef CONFIG_P2P
1796 	char *freq;
1797 	int forced_freq = 0;
1798 
1799 	freq = strstr(cmd, " freq=");
1800 	if (freq) {
1801 		*freq = '\0';
1802 		freq += 6;
1803 		forced_freq = atoi(freq);
1804 	}
1805 #endif /* CONFIG_P2P */
1806 
1807 	role = cmd;
1808 	pos = os_strchr(role, ' ');
1809 	if (pos == NULL) {
1810 		wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
1811 		return -1;
1812 	}
1813 	*pos++ = '\0';
1814 
1815 	type = pos;
1816 	pos = os_strchr(type, ' ');
1817 	if (pos == NULL) {
1818 		wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
1819 		return -1;
1820 	}
1821 	*pos++ = '\0';
1822 
1823 	pos2 = os_strchr(pos, ' ');
1824 	if (pos2 == NULL) {
1825 		wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
1826 		return -1;
1827 	}
1828 	*pos2++ = '\0';
1829 
1830 	len = os_strlen(pos);
1831 	if (len & 0x01) {
1832 		wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
1833 		return -1;
1834 	}
1835 	len /= 2;
1836 
1837 	req = wpabuf_alloc(len);
1838 	if (req == NULL) {
1839 		wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
1840 		return -1;
1841 	}
1842 	if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
1843 		wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
1844 		wpabuf_free(req);
1845 		return -1;
1846 	}
1847 
1848 	len = os_strlen(pos2);
1849 	if (len & 0x01) {
1850 		wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
1851 		wpabuf_free(req);
1852 		return -1;
1853 	}
1854 	len /= 2;
1855 
1856 	sel = wpabuf_alloc(len);
1857 	if (sel == NULL) {
1858 		wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
1859 		wpabuf_free(req);
1860 		return -1;
1861 	}
1862 	if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
1863 		wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
1864 		wpabuf_free(req);
1865 		wpabuf_free(sel);
1866 		return -1;
1867 	}
1868 
1869 	wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1870 		   role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1871 
1872 	if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1873 		ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
1874 #ifdef CONFIG_AP
1875 	} else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1876 	{
1877 		ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
1878 		if (ret < 0)
1879 			ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
1880 #endif /* CONFIG_AP */
1881 #ifdef CONFIG_P2P
1882 	} else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1883 	{
1884 		ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
1885 	} else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1886 	{
1887 		ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1888 						   forced_freq);
1889 #endif /* CONFIG_P2P */
1890 	} else {
1891 		wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1892 			   "reported: role=%s type=%s", role, type);
1893 		ret = -1;
1894 	}
1895 	wpabuf_free(req);
1896 	wpabuf_free(sel);
1897 
1898 	if (ret)
1899 		wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1900 
1901 	return ret;
1902 }
1903 
1904 #endif /* CONFIG_WPS_NFC */
1905 
1906 
wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant * wpa_s,char * cmd)1907 static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1908 					     char *cmd)
1909 {
1910 	u8 bssid[ETH_ALEN];
1911 	char *pin;
1912 	char *new_ssid;
1913 	char *new_auth;
1914 	char *new_encr;
1915 	char *new_key;
1916 	struct wps_new_ap_settings ap;
1917 
1918 	pin = os_strchr(cmd, ' ');
1919 	if (pin == NULL)
1920 		return -1;
1921 	*pin++ = '\0';
1922 
1923 	if (hwaddr_aton(cmd, bssid)) {
1924 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1925 			   cmd);
1926 		return -1;
1927 	}
1928 
1929 	new_ssid = os_strchr(pin, ' ');
1930 	if (new_ssid == NULL)
1931 		return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1932 	*new_ssid++ = '\0';
1933 
1934 	new_auth = os_strchr(new_ssid, ' ');
1935 	if (new_auth == NULL)
1936 		return -1;
1937 	*new_auth++ = '\0';
1938 
1939 	new_encr = os_strchr(new_auth, ' ');
1940 	if (new_encr == NULL)
1941 		return -1;
1942 	*new_encr++ = '\0';
1943 
1944 	new_key = os_strchr(new_encr, ' ');
1945 	if (new_key == NULL)
1946 		return -1;
1947 	*new_key++ = '\0';
1948 
1949 	os_memset(&ap, 0, sizeof(ap));
1950 	ap.ssid_hex = new_ssid;
1951 	ap.auth = new_auth;
1952 	ap.encr = new_encr;
1953 	ap.key_hex = new_key;
1954 	return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1955 }
1956 
1957 
1958 #ifdef CONFIG_AP
wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1959 static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1960 						char *cmd, char *buf,
1961 						size_t buflen)
1962 {
1963 	int timeout = 300;
1964 	char *pos;
1965 	const char *pin_txt;
1966 
1967 	if (!wpa_s->ap_iface)
1968 		return -1;
1969 
1970 	pos = os_strchr(cmd, ' ');
1971 	if (pos)
1972 		*pos++ = '\0';
1973 
1974 	if (os_strcmp(cmd, "disable") == 0) {
1975 		wpas_wps_ap_pin_disable(wpa_s);
1976 		return os_snprintf(buf, buflen, "OK\n");
1977 	}
1978 
1979 	if (os_strcmp(cmd, "random") == 0) {
1980 		if (pos)
1981 			timeout = atoi(pos);
1982 		pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1983 		if (pin_txt == NULL)
1984 			return -1;
1985 		return os_snprintf(buf, buflen, "%s", pin_txt);
1986 	}
1987 
1988 	if (os_strcmp(cmd, "get") == 0) {
1989 		pin_txt = wpas_wps_ap_pin_get(wpa_s);
1990 		if (pin_txt == NULL)
1991 			return -1;
1992 		return os_snprintf(buf, buflen, "%s", pin_txt);
1993 	}
1994 
1995 	if (os_strcmp(cmd, "set") == 0) {
1996 		char *pin;
1997 		if (pos == NULL)
1998 			return -1;
1999 		pin = pos;
2000 		pos = os_strchr(pos, ' ');
2001 		if (pos) {
2002 			*pos++ = '\0';
2003 			timeout = atoi(pos);
2004 		}
2005 		if (os_strlen(pin) > buflen)
2006 			return -1;
2007 		if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
2008 			return -1;
2009 		return os_snprintf(buf, buflen, "%s", pin);
2010 	}
2011 
2012 	return -1;
2013 }
2014 #endif /* CONFIG_AP */
2015 
2016 
2017 #ifdef CONFIG_WPS_ER
wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant * wpa_s,char * cmd)2018 static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
2019 						char *cmd)
2020 {
2021 	char *uuid = cmd, *pin, *pos;
2022 	u8 addr_buf[ETH_ALEN], *addr = NULL;
2023 	pin = os_strchr(uuid, ' ');
2024 	if (pin == NULL)
2025 		return -1;
2026 	*pin++ = '\0';
2027 	pos = os_strchr(pin, ' ');
2028 	if (pos) {
2029 		*pos++ = '\0';
2030 		if (hwaddr_aton(pos, addr_buf) == 0)
2031 			addr = addr_buf;
2032 	}
2033 	return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
2034 }
2035 
2036 
wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant * wpa_s,char * cmd)2037 static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
2038 						  char *cmd)
2039 {
2040 	char *uuid = cmd, *pin;
2041 	pin = os_strchr(uuid, ' ');
2042 	if (pin == NULL)
2043 		return -1;
2044 	*pin++ = '\0';
2045 	return wpas_wps_er_learn(wpa_s, uuid, pin);
2046 }
2047 
2048 
wpa_supplicant_ctrl_iface_wps_er_set_config(struct wpa_supplicant * wpa_s,char * cmd)2049 static int wpa_supplicant_ctrl_iface_wps_er_set_config(
2050 	struct wpa_supplicant *wpa_s, char *cmd)
2051 {
2052 	char *uuid = cmd, *id;
2053 	id = os_strchr(uuid, ' ');
2054 	if (id == NULL)
2055 		return -1;
2056 	*id++ = '\0';
2057 	return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
2058 }
2059 
2060 
wpa_supplicant_ctrl_iface_wps_er_config(struct wpa_supplicant * wpa_s,char * cmd)2061 static int wpa_supplicant_ctrl_iface_wps_er_config(
2062 	struct wpa_supplicant *wpa_s, char *cmd)
2063 {
2064 	char *pin;
2065 	char *new_ssid;
2066 	char *new_auth;
2067 	char *new_encr;
2068 	char *new_key;
2069 	struct wps_new_ap_settings ap;
2070 
2071 	pin = os_strchr(cmd, ' ');
2072 	if (pin == NULL)
2073 		return -1;
2074 	*pin++ = '\0';
2075 
2076 	new_ssid = os_strchr(pin, ' ');
2077 	if (new_ssid == NULL)
2078 		return -1;
2079 	*new_ssid++ = '\0';
2080 
2081 	new_auth = os_strchr(new_ssid, ' ');
2082 	if (new_auth == NULL)
2083 		return -1;
2084 	*new_auth++ = '\0';
2085 
2086 	new_encr = os_strchr(new_auth, ' ');
2087 	if (new_encr == NULL)
2088 		return -1;
2089 	*new_encr++ = '\0';
2090 
2091 	new_key = os_strchr(new_encr, ' ');
2092 	if (new_key == NULL)
2093 		return -1;
2094 	*new_key++ = '\0';
2095 
2096 	os_memset(&ap, 0, sizeof(ap));
2097 	ap.ssid_hex = new_ssid;
2098 	ap.auth = new_auth;
2099 	ap.encr = new_encr;
2100 	ap.key_hex = new_key;
2101 	return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
2102 }
2103 
2104 
2105 #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)2106 static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
2107 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
2108 {
2109 	int ndef;
2110 	struct wpabuf *buf;
2111 	int res;
2112 	char *uuid;
2113 
2114 	uuid = os_strchr(cmd, ' ');
2115 	if (uuid == NULL)
2116 		return -1;
2117 	*uuid++ = '\0';
2118 
2119 	if (os_strcmp(cmd, "WPS") == 0)
2120 		ndef = 0;
2121 	else if (os_strcmp(cmd, "NDEF") == 0)
2122 		ndef = 1;
2123 	else
2124 		return -1;
2125 
2126 	buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
2127 	if (buf == NULL)
2128 		return -1;
2129 
2130 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
2131 					 wpabuf_len(buf));
2132 	reply[res++] = '\n';
2133 	reply[res] = '\0';
2134 
2135 	wpabuf_free(buf);
2136 
2137 	return res;
2138 }
2139 #endif /* CONFIG_WPS_NFC */
2140 #endif /* CONFIG_WPS_ER */
2141 
2142 #endif /* CONFIG_WPS */
2143 
2144 
2145 #ifdef CONFIG_IBSS_RSN
wpa_supplicant_ctrl_iface_ibss_rsn(struct wpa_supplicant * wpa_s,char * addr)2146 static int wpa_supplicant_ctrl_iface_ibss_rsn(
2147 	struct wpa_supplicant *wpa_s, char *addr)
2148 {
2149 	u8 peer[ETH_ALEN];
2150 
2151 	if (hwaddr_aton(addr, peer)) {
2152 		wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
2153 			   "address '%s'", addr);
2154 		return -1;
2155 	}
2156 
2157 	wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
2158 		   MAC2STR(peer));
2159 
2160 	return ibss_rsn_start(wpa_s->ibss_rsn, peer);
2161 }
2162 #endif /* CONFIG_IBSS_RSN */
2163 
2164 
wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant * wpa_s,char * rsp)2165 static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
2166 					      char *rsp)
2167 {
2168 #ifdef IEEE8021X_EAPOL
2169 	char *pos, *id_pos;
2170 	int id;
2171 	struct wpa_ssid *ssid;
2172 
2173 	pos = os_strchr(rsp, '-');
2174 	if (pos == NULL)
2175 		return -1;
2176 	*pos++ = '\0';
2177 	id_pos = pos;
2178 	pos = os_strchr(pos, ':');
2179 	if (pos == NULL)
2180 		return -1;
2181 	*pos++ = '\0';
2182 	id = atoi(id_pos);
2183 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
2184 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2185 			      (u8 *) pos, os_strlen(pos));
2186 
2187 	ssid = wpa_config_get_network(wpa_s->conf, id);
2188 	if (ssid == NULL) {
2189 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2190 			   "to update", id);
2191 		return -1;
2192 	}
2193 
2194 	return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
2195 							 pos);
2196 #else /* IEEE8021X_EAPOL */
2197 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
2198 	return -1;
2199 #endif /* IEEE8021X_EAPOL */
2200 }
2201 
2202 
wpa_supplicant_ctrl_iface_status(struct wpa_supplicant * wpa_s,const char * params,char * buf,size_t buflen)2203 static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
2204 					    const char *params,
2205 					    char *buf, size_t buflen)
2206 {
2207 	char *pos, *end, tmp[30];
2208 	int res, verbose, wps, ret;
2209 #ifdef CONFIG_HS20
2210 	const u8 *hs20;
2211 #endif /* CONFIG_HS20 */
2212 	const u8 *sess_id;
2213 	size_t sess_id_len;
2214 
2215 	if (os_strcmp(params, "-DRIVER") == 0)
2216 		return wpa_drv_status(wpa_s, buf, buflen);
2217 	verbose = os_strcmp(params, "-VERBOSE") == 0;
2218 	wps = os_strcmp(params, "-WPS") == 0;
2219 	pos = buf;
2220 	end = buf + buflen;
2221 	if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
2222 		struct wpa_ssid *ssid = wpa_s->current_ssid;
2223 		ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
2224 				  MAC2STR(wpa_s->bssid));
2225 		if (os_snprintf_error(end - pos, ret))
2226 			return pos - buf;
2227 		pos += ret;
2228 		ret = os_snprintf(pos, end - pos, "freq=%u\n",
2229 				  wpa_s->assoc_freq);
2230 		if (os_snprintf_error(end - pos, ret))
2231 			return pos - buf;
2232 		pos += ret;
2233 		if (ssid) {
2234 			u8 *_ssid = ssid->ssid;
2235 			size_t ssid_len = ssid->ssid_len;
2236 			u8 ssid_buf[SSID_MAX_LEN];
2237 			if (ssid_len == 0) {
2238 				int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
2239 				if (_res < 0)
2240 					ssid_len = 0;
2241 				else
2242 					ssid_len = _res;
2243 				_ssid = ssid_buf;
2244 			}
2245 			ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
2246 					  wpa_ssid_txt(_ssid, ssid_len),
2247 					  ssid->id);
2248 			if (os_snprintf_error(end - pos, ret))
2249 				return pos - buf;
2250 			pos += ret;
2251 
2252 			if (wps && ssid->passphrase &&
2253 			    wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
2254 			    (ssid->mode == WPAS_MODE_AP ||
2255 			     ssid->mode == WPAS_MODE_P2P_GO)) {
2256 				ret = os_snprintf(pos, end - pos,
2257 						  "passphrase=%s\n",
2258 						  ssid->passphrase);
2259 				if (os_snprintf_error(end - pos, ret))
2260 					return pos - buf;
2261 				pos += ret;
2262 			}
2263 			if (ssid->id_str) {
2264 				ret = os_snprintf(pos, end - pos,
2265 						  "id_str=%s\n",
2266 						  ssid->id_str);
2267 				if (os_snprintf_error(end - pos, ret))
2268 					return pos - buf;
2269 				pos += ret;
2270 			}
2271 
2272 			switch (ssid->mode) {
2273 			case WPAS_MODE_INFRA:
2274 				ret = os_snprintf(pos, end - pos,
2275 						  "mode=station\n");
2276 				break;
2277 			case WPAS_MODE_IBSS:
2278 				ret = os_snprintf(pos, end - pos,
2279 						  "mode=IBSS\n");
2280 				break;
2281 			case WPAS_MODE_AP:
2282 				ret = os_snprintf(pos, end - pos,
2283 						  "mode=AP\n");
2284 				break;
2285 			case WPAS_MODE_P2P_GO:
2286 				ret = os_snprintf(pos, end - pos,
2287 						  "mode=P2P GO\n");
2288 				break;
2289 			case WPAS_MODE_P2P_GROUP_FORMATION:
2290 				ret = os_snprintf(pos, end - pos,
2291 						  "mode=P2P GO - group "
2292 						  "formation\n");
2293 				break;
2294 			case WPAS_MODE_MESH:
2295 				ret = os_snprintf(pos, end - pos,
2296 						  "mode=mesh\n");
2297 				break;
2298 			default:
2299 				ret = 0;
2300 				break;
2301 			}
2302 			if (os_snprintf_error(end - pos, ret))
2303 				return pos - buf;
2304 			pos += ret;
2305 		}
2306 
2307 		if (wpa_s->connection_set &&
2308 		    (wpa_s->connection_ht || wpa_s->connection_vht ||
2309 		     wpa_s->connection_he)) {
2310 			ret = os_snprintf(pos, end - pos,
2311 					  "wifi_generation=%u\n",
2312 					  wpa_s->connection_he ? 6 :
2313 					  (wpa_s->connection_vht ? 5 : 4));
2314 			if (os_snprintf_error(end - pos, ret))
2315 				return pos - buf;
2316 			pos += ret;
2317 		}
2318 
2319 #ifdef CONFIG_AP
2320 		if (wpa_s->ap_iface) {
2321 			pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
2322 							    end - pos,
2323 							    verbose);
2324 		} else
2325 #endif /* CONFIG_AP */
2326 		pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
2327 	}
2328 #ifdef CONFIG_SME
2329 #ifdef CONFIG_SAE
2330 	if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
2331 #ifdef CONFIG_AP
2332 	    !wpa_s->ap_iface &&
2333 #endif /* CONFIG_AP */
2334 	    wpa_s->sme.sae.state == SAE_ACCEPTED) {
2335 		ret = os_snprintf(pos, end - pos, "sae_group=%d\n"
2336 				  "sae_h2e=%d\n"
2337 				  "sae_pk=%d\n",
2338 				  wpa_s->sme.sae.group,
2339 				  wpa_s->sme.sae.h2e,
2340 				  wpa_s->sme.sae.pk);
2341 		if (os_snprintf_error(end - pos, ret))
2342 			return pos - buf;
2343 		pos += ret;
2344 	}
2345 #endif /* CONFIG_SAE */
2346 #endif /* CONFIG_SME */
2347 	ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
2348 			  wpa_supplicant_state_txt(wpa_s->wpa_state));
2349 	if (os_snprintf_error(end - pos, ret))
2350 		return pos - buf;
2351 	pos += ret;
2352 
2353 	if (wpa_s->l2 &&
2354 	    l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
2355 		ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
2356 		if (os_snprintf_error(end - pos, ret))
2357 			return pos - buf;
2358 		pos += ret;
2359 	}
2360 
2361 #ifdef CONFIG_P2P
2362 	if (wpa_s->global->p2p) {
2363 		ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
2364 				  "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
2365 		if (os_snprintf_error(end - pos, ret))
2366 			return pos - buf;
2367 		pos += ret;
2368 	}
2369 #endif /* CONFIG_P2P */
2370 
2371 	ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
2372 			  MAC2STR(wpa_s->own_addr));
2373 	if (os_snprintf_error(end - pos, ret))
2374 		return pos - buf;
2375 	pos += ret;
2376 
2377 #ifdef CONFIG_HS20
2378 	if (wpa_s->current_bss &&
2379 	    (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2380 					  HS20_IE_VENDOR_TYPE)) &&
2381 	    wpa_s->wpa_proto == WPA_PROTO_RSN &&
2382 	    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2383 		int release = 1;
2384 		if (hs20[1] >= 5) {
2385 			u8 rel_num = (hs20[6] & 0xf0) >> 4;
2386 			release = rel_num + 1;
2387 		}
2388 		ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
2389 		if (os_snprintf_error(end - pos, ret))
2390 			return pos - buf;
2391 		pos += ret;
2392 	}
2393 
2394 	if (wpa_s->current_ssid) {
2395 		struct wpa_cred *cred;
2396 		char *type;
2397 
2398 		for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2399 			size_t i;
2400 
2401 			if (wpa_s->current_ssid->parent_cred != cred)
2402 				continue;
2403 
2404 			if (cred->provisioning_sp) {
2405 				ret = os_snprintf(pos, end - pos,
2406 						  "provisioning_sp=%s\n",
2407 						  cred->provisioning_sp);
2408 				if (os_snprintf_error(end - pos, ret))
2409 					return pos - buf;
2410 				pos += ret;
2411 			}
2412 
2413 			if (!cred->domain)
2414 				goto no_domain;
2415 
2416 			i = 0;
2417 			if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
2418 				struct wpabuf *names =
2419 					wpa_s->current_bss->anqp->domain_name;
2420 				for (i = 0; names && i < cred->num_domain; i++)
2421 				{
2422 					if (domain_name_list_contains(
2423 						    names, cred->domain[i], 1))
2424 						break;
2425 				}
2426 				if (i == cred->num_domain)
2427 					i = 0; /* show first entry by default */
2428 			}
2429 			ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
2430 					  cred->domain[i]);
2431 			if (os_snprintf_error(end - pos, ret))
2432 				return pos - buf;
2433 			pos += ret;
2434 
2435 		no_domain:
2436 			if (wpa_s->current_bss == NULL ||
2437 			    wpa_s->current_bss->anqp == NULL)
2438 				res = -1;
2439 			else
2440 				res = interworking_home_sp_cred(
2441 					wpa_s, cred,
2442 					wpa_s->current_bss->anqp->domain_name);
2443 			if (res > 0)
2444 				type = "home";
2445 			else if (res == 0)
2446 				type = "roaming";
2447 			else
2448 				type = "unknown";
2449 
2450 			ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
2451 			if (os_snprintf_error(end - pos, ret))
2452 				return pos - buf;
2453 			pos += ret;
2454 
2455 			break;
2456 		}
2457 	}
2458 #endif /* CONFIG_HS20 */
2459 
2460 	if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2461 	    wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2462 		res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
2463 					  verbose);
2464 		if (res >= 0)
2465 			pos += res;
2466 	}
2467 
2468 #ifdef CONFIG_MACSEC
2469 	res = ieee802_1x_kay_get_status(wpa_s->kay, pos, end - pos);
2470 	if (res > 0)
2471 		pos += res;
2472 #endif /* CONFIG_MACSEC */
2473 
2474 	sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
2475 	if (sess_id) {
2476 		char *start = pos;
2477 
2478 		ret = os_snprintf(pos, end - pos, "eap_session_id=");
2479 		if (os_snprintf_error(end - pos, ret))
2480 			return start - buf;
2481 		pos += ret;
2482 		ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
2483 		if (ret <= 0)
2484 			return start - buf;
2485 		pos += ret;
2486 		ret = os_snprintf(pos, end - pos, "\n");
2487 		if (os_snprintf_error(end - pos, ret))
2488 			return start - buf;
2489 		pos += ret;
2490 	}
2491 
2492 	res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
2493 	if (res >= 0)
2494 		pos += res;
2495 
2496 #ifdef CONFIG_WPS
2497 	{
2498 		char uuid_str[100];
2499 		uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
2500 		ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
2501 		if (os_snprintf_error(end - pos, ret))
2502 			return pos - buf;
2503 		pos += ret;
2504 	}
2505 #endif /* CONFIG_WPS */
2506 
2507 	if (wpa_s->ieee80211ac) {
2508 		ret = os_snprintf(pos, end - pos, "ieee80211ac=1\n");
2509 		if (os_snprintf_error(end - pos, ret))
2510 			return pos - buf;
2511 		pos += ret;
2512 	}
2513 
2514 #ifdef ANDROID
2515 	/*
2516 	 * Allow using the STATUS command with default behavior, say for debug,
2517 	 * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
2518 	 * events with STATUS-NO_EVENTS.
2519 	 */
2520 	if (os_strcmp(params, "-NO_EVENTS")) {
2521 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
2522 			     "id=%d state=%d BSSID=" MACSTR " SSID=%s",
2523 			     wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
2524 			     wpa_s->wpa_state,
2525 			     MAC2STR(wpa_s->bssid),
2526 			     wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
2527 			     wpa_ssid_txt(wpa_s->current_ssid->ssid,
2528 					  wpa_s->current_ssid->ssid_len) : "");
2529 		if (wpa_s->wpa_state == WPA_COMPLETED) {
2530 			struct wpa_ssid *ssid = wpa_s->current_ssid;
2531 			wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
2532 				     "- connection to " MACSTR
2533 				     " completed %s [id=%d id_str=%s]",
2534 				     MAC2STR(wpa_s->bssid), "(auth)",
2535 				     ssid ? ssid->id : -1,
2536 				     ssid && ssid->id_str ? ssid->id_str : "");
2537 		}
2538 	}
2539 #endif /* ANDROID */
2540 
2541 	return pos - buf;
2542 }
2543 
2544 
wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant * wpa_s,char * cmd)2545 static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
2546 					   char *cmd)
2547 {
2548 	char *pos;
2549 	int id;
2550 	struct wpa_ssid *ssid;
2551 	u8 bssid[ETH_ALEN];
2552 
2553 	/* cmd: "<network id> <BSSID>" */
2554 	pos = os_strchr(cmd, ' ');
2555 	if (pos == NULL)
2556 		return -1;
2557 	*pos++ = '\0';
2558 	id = atoi(cmd);
2559 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
2560 	if (hwaddr_aton(pos, bssid)) {
2561 		wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
2562 		return -1;
2563 	}
2564 
2565 	ssid = wpa_config_get_network(wpa_s->conf, id);
2566 	if (ssid == NULL) {
2567 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2568 			   "to update", id);
2569 		return -1;
2570 	}
2571 
2572 	os_memcpy(ssid->bssid, bssid, ETH_ALEN);
2573 	ssid->bssid_set = !is_zero_ether_addr(bssid);
2574 
2575 	return 0;
2576 }
2577 
2578 
wpa_supplicant_ctrl_iface_bssid_ignore(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2579 static int wpa_supplicant_ctrl_iface_bssid_ignore(struct wpa_supplicant *wpa_s,
2580 						  char *cmd, char *buf,
2581 						  size_t buflen)
2582 {
2583 	u8 bssid[ETH_ALEN];
2584 	struct wpa_bssid_ignore *e;
2585 	char *pos, *end;
2586 	int ret;
2587 
2588 	/* cmd: "BSSID_IGNORE [<BSSID>]" */
2589 	if (*cmd == '\0') {
2590 		pos = buf;
2591 		end = buf + buflen;
2592 		e = wpa_s->bssid_ignore;
2593 		while (e) {
2594 			ret = os_snprintf(pos, end - pos, MACSTR "\n",
2595 					  MAC2STR(e->bssid));
2596 			if (os_snprintf_error(end - pos, ret))
2597 				return pos - buf;
2598 			pos += ret;
2599 			e = e->next;
2600 		}
2601 		return pos - buf;
2602 	}
2603 
2604 	cmd++;
2605 	if (os_strncmp(cmd, "clear", 5) == 0) {
2606 		wpa_bssid_ignore_clear(wpa_s);
2607 		os_memcpy(buf, "OK\n", 3);
2608 		return 3;
2609 	}
2610 
2611 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: BSSID_IGNORE bssid='%s'", cmd);
2612 	if (hwaddr_aton(cmd, bssid)) {
2613 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
2614 		return -1;
2615 	}
2616 
2617 	/*
2618 	 * Add the BSSID twice, so its count will be 2, causing it to be
2619 	 * skipped when processing scan results.
2620 	 */
2621 	ret = wpa_bssid_ignore_add(wpa_s, bssid);
2622 	if (ret < 0)
2623 		return -1;
2624 	ret = wpa_bssid_ignore_add(wpa_s, bssid);
2625 	if (ret < 0)
2626 		return -1;
2627 	os_memcpy(buf, "OK\n", 3);
2628 	return 3;
2629 }
2630 
2631 
wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2632 static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
2633 					       char *cmd, char *buf,
2634 					       size_t buflen)
2635 {
2636 	char *pos, *end, *stamp;
2637 	int ret;
2638 
2639 	/* cmd: "LOG_LEVEL [<level>]" */
2640 	if (*cmd == '\0') {
2641 		pos = buf;
2642 		end = buf + buflen;
2643 		ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2644 				  "Timestamp: %d\n",
2645 				  debug_level_str(wpa_debug_level),
2646 				  wpa_debug_timestamp);
2647 		if (os_snprintf_error(end - pos, ret))
2648 			ret = 0;
2649 
2650 		return ret;
2651 	}
2652 
2653 	while (*cmd == ' ')
2654 		cmd++;
2655 
2656 	stamp = os_strchr(cmd, ' ');
2657 	if (stamp) {
2658 		*stamp++ = '\0';
2659 		while (*stamp == ' ') {
2660 			stamp++;
2661 		}
2662 	}
2663 
2664 	if (os_strlen(cmd)) {
2665 		int level = str_to_debug_level(cmd);
2666 		if (level < 0)
2667 			return -1;
2668 		wpa_debug_level = level;
2669 	}
2670 
2671 	if (stamp && os_strlen(stamp))
2672 		wpa_debug_timestamp = atoi(stamp);
2673 
2674 	os_memcpy(buf, "OK\n", 3);
2675 	return 3;
2676 }
2677 
2678 
wpa_supplicant_ctrl_iface_list_networks(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2679 static int wpa_supplicant_ctrl_iface_list_networks(
2680 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2681 {
2682 	char *pos, *end, *prev;
2683 	struct wpa_ssid *ssid;
2684 	int ret;
2685 
2686 	pos = buf;
2687 	end = buf + buflen;
2688 	ret = os_snprintf(pos, end - pos,
2689 			  "network id / ssid / bssid / flags\n");
2690 	if (os_snprintf_error(end - pos, ret))
2691 		return pos - buf;
2692 	pos += ret;
2693 
2694 	ssid = wpa_s->conf->ssid;
2695 
2696 	/* skip over ssids until we find next one */
2697 	if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
2698 		int last_id = atoi(cmd + 8);
2699 		if (last_id != -1) {
2700 			while (ssid != NULL && ssid->id <= last_id) {
2701 				ssid = ssid->next;
2702 			}
2703 		}
2704 	}
2705 
2706 	while (ssid) {
2707 		prev = pos;
2708 		ret = os_snprintf(pos, end - pos, "%d\t%s",
2709 				  ssid->id,
2710 				  wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2711 		if (os_snprintf_error(end - pos, ret))
2712 			return prev - buf;
2713 		pos += ret;
2714 		if (ssid->bssid_set) {
2715 			ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2716 					  MAC2STR(ssid->bssid));
2717 		} else {
2718 			ret = os_snprintf(pos, end - pos, "\tany");
2719 		}
2720 		if (os_snprintf_error(end - pos, ret))
2721 			return prev - buf;
2722 		pos += ret;
2723 		ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
2724 				  ssid == wpa_s->current_ssid ?
2725 				  "[CURRENT]" : "",
2726 				  ssid->disabled ? "[DISABLED]" : "",
2727 				  ssid->disabled_until.sec ?
2728 				  "[TEMP-DISABLED]" : "",
2729 				  ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2730 				  "");
2731 		if (os_snprintf_error(end - pos, ret))
2732 			return prev - buf;
2733 		pos += ret;
2734 		ret = os_snprintf(pos, end - pos, "\n");
2735 		if (os_snprintf_error(end - pos, ret))
2736 			return prev - buf;
2737 		pos += ret;
2738 
2739 		ssid = ssid->next;
2740 	}
2741 
2742 	return pos - buf;
2743 }
2744 
2745 
wpa_supplicant_cipher_txt(char * pos,char * end,int cipher)2746 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2747 {
2748 	int ret;
2749 	ret = os_snprintf(pos, end - pos, "-");
2750 	if (os_snprintf_error(end - pos, ret))
2751 		return pos;
2752 	pos += ret;
2753 	ret = wpa_write_ciphers(pos, end, cipher, "+");
2754 	if (ret < 0)
2755 		return pos;
2756 	pos += ret;
2757 	return pos;
2758 }
2759 
2760 
wpa_supplicant_ie_txt(char * pos,char * end,const char * proto,const u8 * ie,size_t ie_len)2761 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2762 				    const u8 *ie, size_t ie_len)
2763 {
2764 	struct wpa_ie_data data;
2765 	char *start;
2766 	int ret;
2767 
2768 	ret = os_snprintf(pos, end - pos, "[%s-", proto);
2769 	if (os_snprintf_error(end - pos, ret))
2770 		return pos;
2771 	pos += ret;
2772 
2773 	if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2774 		ret = os_snprintf(pos, end - pos, "?]");
2775 		if (os_snprintf_error(end - pos, ret))
2776 			return pos;
2777 		pos += ret;
2778 		return pos;
2779 	}
2780 
2781 	start = pos;
2782 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
2783 		ret = os_snprintf(pos, end - pos, "%sEAP",
2784 				  pos == start ? "" : "+");
2785 		if (os_snprintf_error(end - pos, ret))
2786 			return pos;
2787 		pos += ret;
2788 	}
2789 	if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
2790 		ret = os_snprintf(pos, end - pos, "%sPSK",
2791 				  pos == start ? "" : "+");
2792 		if (os_snprintf_error(end - pos, ret))
2793 			return pos;
2794 		pos += ret;
2795 	}
2796 	if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
2797 		ret = os_snprintf(pos, end - pos, "%sNone",
2798 				  pos == start ? "" : "+");
2799 		if (os_snprintf_error(end - pos, ret))
2800 			return pos;
2801 		pos += ret;
2802 	}
2803 	if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
2804 		ret = os_snprintf(pos, end - pos, "%sSAE",
2805 				  pos == start ? "" : "+");
2806 		if (os_snprintf_error(end - pos, ret))
2807 			return pos;
2808 		pos += ret;
2809 	}
2810 #ifdef CONFIG_IEEE80211R
2811 	if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2812 		ret = os_snprintf(pos, end - pos, "%sFT/EAP",
2813 				  pos == start ? "" : "+");
2814 		if (os_snprintf_error(end - pos, ret))
2815 			return pos;
2816 		pos += ret;
2817 	}
2818 	if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2819 		ret = os_snprintf(pos, end - pos, "%sFT/PSK",
2820 				  pos == start ? "" : "+");
2821 		if (os_snprintf_error(end - pos, ret))
2822 			return pos;
2823 		pos += ret;
2824 	}
2825 	if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
2826 		ret = os_snprintf(pos, end - pos, "%sFT/SAE",
2827 				  pos == start ? "" : "+");
2828 		if (os_snprintf_error(end - pos, ret))
2829 			return pos;
2830 		pos += ret;
2831 	}
2832 #endif /* CONFIG_IEEE80211R */
2833 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2834 		ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
2835 				  pos == start ? "" : "+");
2836 		if (os_snprintf_error(end - pos, ret))
2837 			return pos;
2838 		pos += ret;
2839 	}
2840 	if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2841 		ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
2842 				  pos == start ? "" : "+");
2843 		if (os_snprintf_error(end - pos, ret))
2844 			return pos;
2845 		pos += ret;
2846 	}
2847 
2848 #ifdef CONFIG_SUITEB
2849 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
2850 		ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
2851 				  pos == start ? "" : "+");
2852 		if (os_snprintf_error(end - pos, ret))
2853 			return pos;
2854 		pos += ret;
2855 	}
2856 #endif /* CONFIG_SUITEB */
2857 
2858 #ifdef CONFIG_SUITEB192
2859 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
2860 		ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
2861 				  pos == start ? "" : "+");
2862 		if (os_snprintf_error(end - pos, ret))
2863 			return pos;
2864 		pos += ret;
2865 	}
2866 #endif /* CONFIG_SUITEB192 */
2867 
2868 #ifdef CONFIG_FILS
2869 	if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
2870 		ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
2871 				  pos == start ? "" : "+");
2872 		if (os_snprintf_error(end - pos, ret))
2873 			return pos;
2874 		pos += ret;
2875 	}
2876 	if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
2877 		ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
2878 				  pos == start ? "" : "+");
2879 		if (os_snprintf_error(end - pos, ret))
2880 			return pos;
2881 		pos += ret;
2882 	}
2883 #ifdef CONFIG_IEEE80211R
2884 	if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
2885 		ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
2886 				  pos == start ? "" : "+");
2887 		if (os_snprintf_error(end - pos, ret))
2888 			return pos;
2889 		pos += ret;
2890 	}
2891 	if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
2892 		ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
2893 				  pos == start ? "" : "+");
2894 		if (os_snprintf_error(end - pos, ret))
2895 			return pos;
2896 		pos += ret;
2897 	}
2898 #endif /* CONFIG_IEEE80211R */
2899 #endif /* CONFIG_FILS */
2900 
2901 #ifdef CONFIG_OWE
2902 	if (data.key_mgmt & WPA_KEY_MGMT_OWE) {
2903 		ret = os_snprintf(pos, end - pos, "%sOWE",
2904 				  pos == start ? "" : "+");
2905 		if (os_snprintf_error(end - pos, ret))
2906 			return pos;
2907 		pos += ret;
2908 	}
2909 #endif /* CONFIG_OWE */
2910 
2911 #ifdef CONFIG_DPP
2912 	if (data.key_mgmt & WPA_KEY_MGMT_DPP) {
2913 		ret = os_snprintf(pos, end - pos, "%sDPP",
2914 				  pos == start ? "" : "+");
2915 		if (os_snprintf_error(end - pos, ret))
2916 			return pos;
2917 		pos += ret;
2918 	}
2919 #endif /* CONFIG_DPP */
2920 
2921 	if (data.key_mgmt & WPA_KEY_MGMT_OSEN) {
2922 		ret = os_snprintf(pos, end - pos, "%sOSEN",
2923 				  pos == start ? "" : "+");
2924 		if (os_snprintf_error(end - pos, ret))
2925 			return pos;
2926 		pos += ret;
2927 	}
2928 
2929 	pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2930 
2931 	if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2932 		ret = os_snprintf(pos, end - pos, "-preauth");
2933 		if (os_snprintf_error(end - pos, ret))
2934 			return pos;
2935 		pos += ret;
2936 	}
2937 
2938 	ret = os_snprintf(pos, end - pos, "]");
2939 	if (os_snprintf_error(end - pos, ret))
2940 		return pos;
2941 	pos += ret;
2942 
2943 	return pos;
2944 }
2945 
2946 
2947 #ifdef CONFIG_WPS
wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant * wpa_s,char * pos,char * end,struct wpabuf * wps_ie)2948 static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2949 					    char *pos, char *end,
2950 					    struct wpabuf *wps_ie)
2951 {
2952 	int ret;
2953 	const char *txt;
2954 
2955 	if (wps_ie == NULL)
2956 		return pos;
2957 	if (wps_is_selected_pbc_registrar(wps_ie))
2958 		txt = "[WPS-PBC]";
2959 	else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2960 		txt = "[WPS-AUTH]";
2961 	else if (wps_is_selected_pin_registrar(wps_ie))
2962 		txt = "[WPS-PIN]";
2963 	else
2964 		txt = "[WPS]";
2965 
2966 	ret = os_snprintf(pos, end - pos, "%s", txt);
2967 	if (!os_snprintf_error(end - pos, ret))
2968 		pos += ret;
2969 	wpabuf_free(wps_ie);
2970 	return pos;
2971 }
2972 #endif /* CONFIG_WPS */
2973 
2974 
wpa_supplicant_wps_ie_txt(struct wpa_supplicant * wpa_s,char * pos,char * end,const struct wpa_bss * bss)2975 static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2976 					char *pos, char *end,
2977 					const struct wpa_bss *bss)
2978 {
2979 #ifdef CONFIG_WPS
2980 	struct wpabuf *wps_ie;
2981 	wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2982 	return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2983 #else /* CONFIG_WPS */
2984 	return pos;
2985 #endif /* CONFIG_WPS */
2986 }
2987 
2988 
2989 /* 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)2990 static int wpa_supplicant_ctrl_iface_scan_result(
2991 	struct wpa_supplicant *wpa_s,
2992 	const struct wpa_bss *bss, char *buf, size_t buflen)
2993 {
2994 	char *pos, *end;
2995 	int ret;
2996 	const u8 *ie, *ie2, *osen_ie, *p2p, *mesh, *owe, *rsnxe;
2997 
2998 	mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
2999 	p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
3000 	if (!p2p)
3001 		p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
3002 	if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
3003 	    os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
3004 	    0)
3005 		return 0; /* Do not show P2P listen discovery results here */
3006 
3007 	pos = buf;
3008 	end = buf + buflen;
3009 
3010 	ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
3011 			  MAC2STR(bss->bssid), bss->freq, bss->level);
3012 	if (os_snprintf_error(end - pos, ret))
3013 		return -1;
3014 	pos += ret;
3015 	ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
3016 	if (ie)
3017 		pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
3018 	ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
3019 	if (ie2) {
3020 		pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
3021 					    ie2, 2 + ie2[1]);
3022 	}
3023 	rsnxe = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
3024 	if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_H2E)) {
3025 		ret = os_snprintf(pos, end - pos, "[SAE-H2E]");
3026 		if (os_snprintf_error(end - pos, ret))
3027 			return -1;
3028 		pos += ret;
3029 	}
3030 	if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_PK)) {
3031 		ret = os_snprintf(pos, end - pos, "[SAE-PK]");
3032 		if (os_snprintf_error(end - pos, ret))
3033 			return -1;
3034 		pos += ret;
3035 	}
3036 	osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
3037 	if (osen_ie)
3038 		pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
3039 					    osen_ie, 2 + osen_ie[1]);
3040 	owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
3041 	if (owe) {
3042 		ret = os_snprintf(pos, end - pos,
3043 				  ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
3044 		if (os_snprintf_error(end - pos, ret))
3045 			return -1;
3046 		pos += ret;
3047 	}
3048 	pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
3049 	if (!ie && !ie2 && !osen_ie && (bss->caps & IEEE80211_CAP_PRIVACY)) {
3050 		ret = os_snprintf(pos, end - pos, "[WEP]");
3051 		if (os_snprintf_error(end - pos, ret))
3052 			return -1;
3053 		pos += ret;
3054 	}
3055 	if (mesh) {
3056 		ret = os_snprintf(pos, end - pos, "[MESH]");
3057 		if (os_snprintf_error(end - pos, ret))
3058 			return -1;
3059 		pos += ret;
3060 	}
3061 	if (bss_is_dmg(bss)) {
3062 		const char *s;
3063 
3064 		if (wpa_bss_get_ie_ext(bss, WLAN_EID_EXT_EDMG_OPERATION)) {
3065 			ret = os_snprintf(pos, end - pos, "[EDMG]");
3066 			if (os_snprintf_error(end - pos, ret))
3067 				return -1;
3068 			pos += ret;
3069 		}
3070 
3071 		ret = os_snprintf(pos, end - pos, "[DMG]");
3072 		if (os_snprintf_error(end - pos, ret))
3073 			return -1;
3074 		pos += ret;
3075 		switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
3076 		case IEEE80211_CAP_DMG_IBSS:
3077 			s = "[IBSS]";
3078 			break;
3079 		case IEEE80211_CAP_DMG_AP:
3080 			s = "[ESS]";
3081 			break;
3082 		case IEEE80211_CAP_DMG_PBSS:
3083 			s = "[PBSS]";
3084 			break;
3085 		default:
3086 			s = "";
3087 			break;
3088 		}
3089 		ret = os_snprintf(pos, end - pos, "%s", s);
3090 		if (os_snprintf_error(end - pos, ret))
3091 			return -1;
3092 		pos += ret;
3093 	} else {
3094 		if (bss->caps & IEEE80211_CAP_IBSS) {
3095 			ret = os_snprintf(pos, end - pos, "[IBSS]");
3096 			if (os_snprintf_error(end - pos, ret))
3097 				return -1;
3098 			pos += ret;
3099 		}
3100 		if (bss->caps & IEEE80211_CAP_ESS) {
3101 			ret = os_snprintf(pos, end - pos, "[ESS]");
3102 			if (os_snprintf_error(end - pos, ret))
3103 				return -1;
3104 			pos += ret;
3105 		}
3106 	}
3107 	if (p2p) {
3108 		ret = os_snprintf(pos, end - pos, "[P2P]");
3109 		if (os_snprintf_error(end - pos, ret))
3110 			return -1;
3111 		pos += ret;
3112 	}
3113 #ifdef CONFIG_HS20
3114 	if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
3115 		ret = os_snprintf(pos, end - pos, "[HS20]");
3116 		if (os_snprintf_error(end - pos, ret))
3117 			return -1;
3118 		pos += ret;
3119 	}
3120 #endif /* CONFIG_HS20 */
3121 #ifdef CONFIG_FILS
3122 	if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
3123 		ret = os_snprintf(pos, end - pos, "[FILS]");
3124 		if (os_snprintf_error(end - pos, ret))
3125 			return -1;
3126 		pos += ret;
3127 	}
3128 #endif /* CONFIG_FILS */
3129 #ifdef CONFIG_FST
3130 	if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
3131 		ret = os_snprintf(pos, end - pos, "[FST]");
3132 		if (os_snprintf_error(end - pos, ret))
3133 			return -1;
3134 		pos += ret;
3135 	}
3136 #endif /* CONFIG_FST */
3137 	if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_UTF_8_SSID)) {
3138 		ret = os_snprintf(pos, end - pos, "[UTF-8]");
3139 		if (os_snprintf_error(end - pos, ret))
3140 			return -1;
3141 		pos += ret;
3142 	}
3143 
3144 	ret = os_snprintf(pos, end - pos, "\t%s",
3145 			  wpa_ssid_txt(bss->ssid, bss->ssid_len));
3146 	if (os_snprintf_error(end - pos, ret))
3147 		return -1;
3148 	pos += ret;
3149 
3150 	ret = os_snprintf(pos, end - pos, "\n");
3151 	if (os_snprintf_error(end - pos, ret))
3152 		return -1;
3153 	pos += ret;
3154 
3155 	return pos - buf;
3156 }
3157 
3158 
wpa_supplicant_ctrl_iface_scan_results(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3159 static int wpa_supplicant_ctrl_iface_scan_results(
3160 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
3161 {
3162 	char *pos, *end;
3163 	struct wpa_bss *bss;
3164 	int ret;
3165 
3166 	pos = buf;
3167 	end = buf + buflen;
3168 	ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
3169 			  "flags / ssid\n");
3170 	if (os_snprintf_error(end - pos, ret))
3171 		return pos - buf;
3172 	pos += ret;
3173 
3174 	dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
3175 		ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
3176 							    end - pos);
3177 		if (ret < 0 || ret >= end - pos)
3178 			return pos - buf;
3179 		pos += ret;
3180 	}
3181 
3182 	return pos - buf;
3183 }
3184 
3185 
3186 #ifdef CONFIG_MESH
3187 
wpa_supplicant_ctrl_iface_mesh_interface_add(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)3188 static int wpa_supplicant_ctrl_iface_mesh_interface_add(
3189 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
3190 {
3191 	char *pos, ifname[IFNAMSIZ + 1];
3192 
3193 	ifname[0] = '\0';
3194 
3195 	pos = os_strstr(cmd, "ifname=");
3196 	if (pos) {
3197 		pos += 7;
3198 		os_strlcpy(ifname, pos, sizeof(ifname));
3199 	}
3200 
3201 	if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
3202 		return -1;
3203 
3204 	os_strlcpy(reply, ifname, max_len);
3205 	return os_strlen(ifname);
3206 }
3207 
3208 
wpa_supplicant_ctrl_iface_mesh_group_add(struct wpa_supplicant * wpa_s,char * cmd)3209 static int wpa_supplicant_ctrl_iface_mesh_group_add(
3210 	struct wpa_supplicant *wpa_s, char *cmd)
3211 {
3212 	int id;
3213 	struct wpa_ssid *ssid;
3214 
3215 	id = atoi(cmd);
3216 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
3217 
3218 	ssid = wpa_config_get_network(wpa_s->conf, id);
3219 	if (ssid == NULL) {
3220 		wpa_printf(MSG_DEBUG,
3221 			   "CTRL_IFACE: Could not find network id=%d", id);
3222 		return -1;
3223 	}
3224 	if (ssid->mode != WPAS_MODE_MESH) {
3225 		wpa_printf(MSG_DEBUG,
3226 			   "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
3227 		return -1;
3228 	}
3229 	if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
3230 	    ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
3231 		wpa_printf(MSG_ERROR,
3232 			   "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
3233 		return -1;
3234 	}
3235 
3236 	/*
3237 	 * TODO: If necessary write our own group_add function,
3238 	 * for now we can reuse select_network
3239 	 */
3240 	wpa_supplicant_select_network(wpa_s, ssid);
3241 
3242 	return 0;
3243 }
3244 
3245 
wpa_supplicant_ctrl_iface_mesh_group_remove(struct wpa_supplicant * wpa_s,char * cmd)3246 static int wpa_supplicant_ctrl_iface_mesh_group_remove(
3247 	struct wpa_supplicant *wpa_s, char *cmd)
3248 {
3249 	struct wpa_supplicant *orig;
3250 	struct wpa_global *global;
3251 	int found = 0;
3252 
3253 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
3254 
3255 	global = wpa_s->global;
3256 	orig = wpa_s;
3257 
3258 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3259 		if (os_strcmp(wpa_s->ifname, cmd) == 0) {
3260 			found = 1;
3261 			break;
3262 		}
3263 	}
3264 	if (!found) {
3265 		wpa_printf(MSG_ERROR,
3266 			   "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
3267 			   cmd);
3268 		return -1;
3269 	}
3270 	if (wpa_s->mesh_if_created && wpa_s == orig) {
3271 		wpa_printf(MSG_ERROR,
3272 			   "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
3273 		return -1;
3274 	}
3275 
3276 	wpa_s->reassociate = 0;
3277 	wpa_s->disconnected = 1;
3278 	wpa_supplicant_cancel_sched_scan(wpa_s);
3279 	wpa_supplicant_cancel_scan(wpa_s);
3280 
3281 	/*
3282 	 * TODO: If necessary write our own group_remove function,
3283 	 * for now we can reuse deauthenticate
3284 	 */
3285 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3286 
3287 	if (wpa_s->mesh_if_created)
3288 		wpa_supplicant_remove_iface(global, wpa_s, 0);
3289 
3290 	return 0;
3291 }
3292 
3293 
wpa_supplicant_ctrl_iface_mesh_peer_remove(struct wpa_supplicant * wpa_s,char * cmd)3294 static int wpa_supplicant_ctrl_iface_mesh_peer_remove(
3295 	struct wpa_supplicant *wpa_s, char *cmd)
3296 {
3297 	u8 addr[ETH_ALEN];
3298 
3299 	if (hwaddr_aton(cmd, addr) < 0)
3300 		return -1;
3301 
3302 	return wpas_mesh_peer_remove(wpa_s, addr);
3303 }
3304 
3305 
wpa_supplicant_ctrl_iface_mesh_peer_add(struct wpa_supplicant * wpa_s,char * cmd)3306 static int wpa_supplicant_ctrl_iface_mesh_peer_add(
3307 	struct wpa_supplicant *wpa_s, char *cmd)
3308 {
3309 	u8 addr[ETH_ALEN];
3310 	int duration;
3311 	char *pos;
3312 
3313 	pos = os_strstr(cmd, " duration=");
3314 	if (pos) {
3315 		*pos = '\0';
3316 		duration = atoi(pos + 10);
3317 	} else {
3318 		duration = -1;
3319 	}
3320 
3321 	if (hwaddr_aton(cmd, addr))
3322 		return -1;
3323 
3324 	return wpas_mesh_peer_add(wpa_s, addr, duration);
3325 }
3326 
3327 
wpa_supplicant_ctrl_iface_mesh_link_probe(struct wpa_supplicant * wpa_s,char * cmd)3328 static int wpa_supplicant_ctrl_iface_mesh_link_probe(
3329 	struct wpa_supplicant *wpa_s, char *cmd)
3330 {
3331 	struct ether_header *eth;
3332 	u8 addr[ETH_ALEN];
3333 	u8 *buf;
3334 	char *pos;
3335 	size_t payload_len = 0, len;
3336 	int ret = -1;
3337 
3338 	if (hwaddr_aton(cmd, addr))
3339 		return -1;
3340 
3341 	pos = os_strstr(cmd, " payload=");
3342 	if (pos) {
3343 		pos = pos + 9;
3344 		payload_len = os_strlen(pos);
3345 		if (payload_len & 1)
3346 			return -1;
3347 
3348 		payload_len /= 2;
3349 	}
3350 
3351 	len = ETH_HLEN + payload_len;
3352 	buf = os_malloc(len);
3353 	if (!buf)
3354 		return -1;
3355 
3356 	eth = (struct ether_header *) buf;
3357 	os_memcpy(eth->ether_dhost, addr, ETH_ALEN);
3358 	os_memcpy(eth->ether_shost, wpa_s->own_addr, ETH_ALEN);
3359 	eth->ether_type = htons(ETH_P_802_3);
3360 
3361 	if (payload_len && hexstr2bin(pos, buf + ETH_HLEN, payload_len) < 0)
3362 		goto fail;
3363 
3364 	ret = wpa_drv_mesh_link_probe(wpa_s, addr, buf, len);
3365 fail:
3366 	os_free(buf);
3367 	return -ret;
3368 }
3369 
3370 #endif /* CONFIG_MESH */
3371 
3372 
wpa_supplicant_ctrl_iface_select_network(struct wpa_supplicant * wpa_s,char * cmd)3373 static int wpa_supplicant_ctrl_iface_select_network(
3374 	struct wpa_supplicant *wpa_s, char *cmd)
3375 {
3376 	int id;
3377 	struct wpa_ssid *ssid;
3378 	char *pos;
3379 
3380 	/* cmd: "<network id>" or "any" */
3381 	if (os_strncmp(cmd, "any", 3) == 0) {
3382 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
3383 		ssid = NULL;
3384 	} else {
3385 		id = atoi(cmd);
3386 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
3387 
3388 		ssid = wpa_config_get_network(wpa_s->conf, id);
3389 		if (ssid == NULL) {
3390 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3391 				   "network id=%d", id);
3392 			return -1;
3393 		}
3394 		if (ssid->disabled == 2) {
3395 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3396 				   "SELECT_NETWORK with persistent P2P group");
3397 			return -1;
3398 		}
3399 	}
3400 
3401 	pos = os_strstr(cmd, " freq=");
3402 	if (pos) {
3403 		int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
3404 		if (freqs) {
3405 			os_free(wpa_s->select_network_scan_freqs);
3406 			wpa_s->select_network_scan_freqs = freqs;
3407 		}
3408 	}
3409 
3410 	wpa_s->scan_min_time.sec = 0;
3411 	wpa_s->scan_min_time.usec = 0;
3412 	wpa_supplicant_select_network(wpa_s, ssid);
3413 
3414 	return 0;
3415 }
3416 
3417 
wpa_supplicant_ctrl_iface_enable_network(struct wpa_supplicant * wpa_s,char * cmd)3418 static int wpa_supplicant_ctrl_iface_enable_network(
3419 	struct wpa_supplicant *wpa_s, char *cmd)
3420 {
3421 	int id;
3422 	struct wpa_ssid *ssid;
3423 
3424 	/* cmd: "<network id>" or "all" */
3425 	if (os_strcmp(cmd, "all") == 0) {
3426 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
3427 		ssid = NULL;
3428 	} else {
3429 		id = atoi(cmd);
3430 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
3431 
3432 		ssid = wpa_config_get_network(wpa_s->conf, id);
3433 		if (ssid == NULL) {
3434 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3435 				   "network id=%d", id);
3436 			return -1;
3437 		}
3438 		if (ssid->disabled == 2) {
3439 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3440 				   "ENABLE_NETWORK with persistent P2P group");
3441 			return -1;
3442 		}
3443 
3444 		if (os_strstr(cmd, " no-connect")) {
3445 			ssid->disabled = 0;
3446 			return 0;
3447 		}
3448 	}
3449 	wpa_s->scan_min_time.sec = 0;
3450 	wpa_s->scan_min_time.usec = 0;
3451 	wpa_supplicant_enable_network(wpa_s, ssid);
3452 
3453 	return 0;
3454 }
3455 
3456 
wpa_supplicant_ctrl_iface_disable_network(struct wpa_supplicant * wpa_s,char * cmd)3457 static int wpa_supplicant_ctrl_iface_disable_network(
3458 	struct wpa_supplicant *wpa_s, char *cmd)
3459 {
3460 	int id;
3461 	struct wpa_ssid *ssid;
3462 
3463 	/* cmd: "<network id>" or "all" */
3464 	if (os_strcmp(cmd, "all") == 0) {
3465 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
3466 		ssid = NULL;
3467 	} else {
3468 		id = atoi(cmd);
3469 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
3470 
3471 		ssid = wpa_config_get_network(wpa_s->conf, id);
3472 		if (ssid == NULL) {
3473 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3474 				   "network id=%d", id);
3475 			return -1;
3476 		}
3477 		if (ssid->disabled == 2) {
3478 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3479 				   "DISABLE_NETWORK with persistent P2P "
3480 				   "group");
3481 			return -1;
3482 		}
3483 	}
3484 	wpa_supplicant_disable_network(wpa_s, ssid);
3485 
3486 	return 0;
3487 }
3488 
3489 
wpa_supplicant_ctrl_iface_add_network(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3490 static int wpa_supplicant_ctrl_iface_add_network(
3491 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
3492 {
3493 	struct wpa_ssid *ssid;
3494 	int ret;
3495 
3496 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
3497 
3498 	ssid = wpa_supplicant_add_network(wpa_s);
3499 	if (ssid == NULL)
3500 		return -1;
3501 
3502 	ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
3503 	if (os_snprintf_error(buflen, ret))
3504 		return -1;
3505 	return ret;
3506 }
3507 
3508 
wpa_supplicant_ctrl_iface_remove_network(struct wpa_supplicant * wpa_s,char * cmd)3509 static int wpa_supplicant_ctrl_iface_remove_network(
3510 	struct wpa_supplicant *wpa_s, char *cmd)
3511 {
3512 	int id;
3513 	int result;
3514 
3515 	/* cmd: "<network id>" or "all" */
3516 	if (os_strcmp(cmd, "all") == 0) {
3517 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
3518 		return wpa_supplicant_remove_all_networks(wpa_s);
3519 	}
3520 
3521 	id = atoi(cmd);
3522 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
3523 
3524 	result = wpa_supplicant_remove_network(wpa_s, id);
3525 	if (result == -1) {
3526 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3527 			   "id=%d", id);
3528 		return -1;
3529 	}
3530 	if (result == -2) {
3531 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
3532 			   "network id=%d", id);
3533 		return -1;
3534 	}
3535 	return 0;
3536 }
3537 
3538 
wpa_supplicant_ctrl_iface_update_network(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,char * name,char * value)3539 static int wpa_supplicant_ctrl_iface_update_network(
3540 	struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3541 	char *name, char *value)
3542 {
3543 	int ret;
3544 
3545 	ret = wpa_config_set(ssid, name, value, 0);
3546 	if (ret < 0) {
3547 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
3548 			   "variable '%s'", name);
3549 		return -1;
3550 	}
3551 	if (ret == 1)
3552 		return 0; /* No change to the previously configured value */
3553 
3554 #ifdef CONFIG_BGSCAN
3555 	if (os_strcmp(name, "bgscan") == 0) {
3556 		/*
3557 		 * Reset the bgscan parameters for the current network and
3558 		 * return. There's no need to flush caches for bgscan parameter
3559 		 * changes.
3560 		 */
3561 		if (wpa_s->current_ssid == ssid &&
3562 		    wpa_s->wpa_state == WPA_COMPLETED)
3563 			wpa_supplicant_reset_bgscan(wpa_s);
3564 		return 0;
3565 	}
3566 #endif /* CONFIG_BGSCAN */
3567 
3568 	if (os_strcmp(name, "bssid") != 0 &&
3569 	    os_strcmp(name, "bssid_hint") != 0 &&
3570 	    os_strcmp(name, "priority") != 0) {
3571 		wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
3572 
3573 		if (wpa_s->current_ssid == ssid ||
3574 		    wpa_s->current_ssid == NULL) {
3575 			/*
3576 			 * Invalidate the EAP session cache if anything in the
3577 			 * current or previously used configuration changes.
3578 			 */
3579 			eapol_sm_invalidate_cached_session(wpa_s->eapol);
3580 		}
3581 	}
3582 
3583 	if ((os_strcmp(name, "psk") == 0 &&
3584 	     value[0] == '"' && ssid->ssid_len) ||
3585 	    (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
3586 		wpa_config_update_psk(ssid);
3587 	else if (os_strcmp(name, "priority") == 0)
3588 		wpa_config_update_prio_list(wpa_s->conf);
3589 
3590 	return 0;
3591 }
3592 
3593 
wpa_supplicant_ctrl_iface_set_network(struct wpa_supplicant * wpa_s,char * cmd)3594 static int wpa_supplicant_ctrl_iface_set_network(
3595 	struct wpa_supplicant *wpa_s, char *cmd)
3596 {
3597 	int id, ret, prev_bssid_set, prev_disabled;
3598 	struct wpa_ssid *ssid;
3599 	char *name, *value;
3600 	u8 prev_bssid[ETH_ALEN];
3601 
3602 	/* cmd: "<network id> <variable name> <value>" */
3603 	name = os_strchr(cmd, ' ');
3604 	if (name == NULL)
3605 		return -1;
3606 	*name++ = '\0';
3607 
3608 	value = os_strchr(name, ' ');
3609 	if (value == NULL)
3610 		return -1;
3611 	*value++ = '\0';
3612 
3613 	id = atoi(cmd);
3614 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
3615 		   id, name);
3616 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3617 			      (u8 *) value, os_strlen(value));
3618 
3619 	ssid = wpa_config_get_network(wpa_s->conf, id);
3620 	if (ssid == NULL) {
3621 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3622 			   "id=%d", id);
3623 		return -1;
3624 	}
3625 
3626 	prev_bssid_set = ssid->bssid_set;
3627 	prev_disabled = ssid->disabled;
3628 	os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
3629 	ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
3630 						       value);
3631 	if (ret == 0 &&
3632 	    (ssid->bssid_set != prev_bssid_set ||
3633 	     os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
3634 		wpas_notify_network_bssid_set_changed(wpa_s, ssid);
3635 
3636 	if (prev_disabled != ssid->disabled &&
3637 	    (prev_disabled == 2 || ssid->disabled == 2))
3638 		wpas_notify_network_type_changed(wpa_s, ssid);
3639 
3640 	return ret;
3641 }
3642 
3643 
wpa_supplicant_ctrl_iface_get_network(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)3644 static int wpa_supplicant_ctrl_iface_get_network(
3645 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
3646 {
3647 	int id;
3648 	size_t res;
3649 	struct wpa_ssid *ssid;
3650 	char *name, *value;
3651 
3652 	/* cmd: "<network id> <variable name>" */
3653 	name = os_strchr(cmd, ' ');
3654 	if (name == NULL || buflen == 0)
3655 		return -1;
3656 	*name++ = '\0';
3657 
3658 	id = atoi(cmd);
3659 	wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
3660 		   id, name);
3661 
3662 	ssid = wpa_config_get_network(wpa_s->conf, id);
3663 	if (ssid == NULL) {
3664 		wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Could not find network "
3665 			   "id=%d", id);
3666 		return -1;
3667 	}
3668 
3669 	value = wpa_config_get_no_key(ssid, name);
3670 	if (value == NULL) {
3671 		wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Failed to get network "
3672 			   "variable '%s'", name);
3673 		return -1;
3674 	}
3675 
3676 	res = os_strlcpy(buf, value, buflen);
3677 	if (res >= buflen) {
3678 		os_free(value);
3679 		return -1;
3680 	}
3681 
3682 	os_free(value);
3683 
3684 	return res;
3685 }
3686 
3687 
wpa_supplicant_ctrl_iface_dup_network(struct wpa_supplicant * wpa_s,char * cmd,struct wpa_supplicant * dst_wpa_s)3688 static int wpa_supplicant_ctrl_iface_dup_network(
3689 	struct wpa_supplicant *wpa_s, char *cmd,
3690 	struct wpa_supplicant *dst_wpa_s)
3691 {
3692 	struct wpa_ssid *ssid_s, *ssid_d;
3693 	char *name, *id, *value;
3694 	int id_s, id_d, ret;
3695 
3696 	/* cmd: "<src network id> <dst network id> <variable name>" */
3697 	id = os_strchr(cmd, ' ');
3698 	if (id == NULL)
3699 		return -1;
3700 	*id++ = '\0';
3701 
3702 	name = os_strchr(id, ' ');
3703 	if (name == NULL)
3704 		return -1;
3705 	*name++ = '\0';
3706 
3707 	id_s = atoi(cmd);
3708 	id_d = atoi(id);
3709 
3710 	wpa_printf(MSG_DEBUG,
3711 		   "CTRL_IFACE: DUP_NETWORK ifname=%s->%s id=%d->%d name='%s'",
3712 		   wpa_s->ifname, dst_wpa_s->ifname, id_s, id_d, name);
3713 
3714 	ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
3715 	if (ssid_s == NULL) {
3716 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3717 			   "network id=%d", id_s);
3718 		return -1;
3719 	}
3720 
3721 	ssid_d = wpa_config_get_network(dst_wpa_s->conf, id_d);
3722 	if (ssid_d == NULL) {
3723 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3724 			   "network id=%d", id_d);
3725 		return -1;
3726 	}
3727 
3728 	value = wpa_config_get(ssid_s, name);
3729 	if (value == NULL) {
3730 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
3731 			   "variable '%s'", name);
3732 		return -1;
3733 	}
3734 
3735 	ret = wpa_supplicant_ctrl_iface_update_network(dst_wpa_s, ssid_d, name,
3736 						       value);
3737 
3738 	os_free(value);
3739 
3740 	return ret;
3741 }
3742 
3743 
wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3744 static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
3745 						char *buf, size_t buflen)
3746 {
3747 	char *pos, *end;
3748 	struct wpa_cred *cred;
3749 	int ret;
3750 
3751 	pos = buf;
3752 	end = buf + buflen;
3753 	ret = os_snprintf(pos, end - pos,
3754 			  "cred id / realm / username / domain / imsi\n");
3755 	if (os_snprintf_error(end - pos, ret))
3756 		return pos - buf;
3757 	pos += ret;
3758 
3759 	cred = wpa_s->conf->cred;
3760 	while (cred) {
3761 		ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
3762 				  cred->id, cred->realm ? cred->realm : "",
3763 				  cred->username ? cred->username : "",
3764 				  cred->domain ? cred->domain[0] : "",
3765 				  cred->imsi ? cred->imsi : "");
3766 		if (os_snprintf_error(end - pos, ret))
3767 			return pos - buf;
3768 		pos += ret;
3769 
3770 		cred = cred->next;
3771 	}
3772 
3773 	return pos - buf;
3774 }
3775 
3776 
wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3777 static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
3778 					      char *buf, size_t buflen)
3779 {
3780 	struct wpa_cred *cred;
3781 	int ret;
3782 
3783 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
3784 
3785 	cred = wpa_config_add_cred(wpa_s->conf);
3786 	if (cred == NULL)
3787 		return -1;
3788 
3789 	wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
3790 
3791 	ret = os_snprintf(buf, buflen, "%d\n", cred->id);
3792 	if (os_snprintf_error(buflen, ret))
3793 		return -1;
3794 	return ret;
3795 }
3796 
3797 
wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant * wpa_s,char * cmd)3798 static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
3799 						 char *cmd)
3800 {
3801 	int id;
3802 	struct wpa_cred *cred, *prev;
3803 
3804 	/* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
3805 	 * "provisioning_sp=<FQDN> */
3806 	if (os_strcmp(cmd, "all") == 0) {
3807 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
3808 		return wpas_remove_all_creds(wpa_s);
3809 	}
3810 
3811 	if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
3812 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
3813 			   cmd + 8);
3814 		cred = wpa_s->conf->cred;
3815 		while (cred) {
3816 			prev = cred;
3817 			cred = cred->next;
3818 			if (prev->domain) {
3819 				size_t i;
3820 				for (i = 0; i < prev->num_domain; i++) {
3821 					if (os_strcmp(prev->domain[i], cmd + 8)
3822 					    != 0)
3823 						continue;
3824 					wpas_remove_cred(wpa_s, prev);
3825 					break;
3826 				}
3827 			}
3828 		}
3829 		return 0;
3830 	}
3831 
3832 	if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
3833 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
3834 			   cmd + 16);
3835 		cred = wpa_s->conf->cred;
3836 		while (cred) {
3837 			prev = cred;
3838 			cred = cred->next;
3839 			if (prev->provisioning_sp &&
3840 			    os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
3841 				wpas_remove_cred(wpa_s, prev);
3842 		}
3843 		return 0;
3844 	}
3845 
3846 	id = atoi(cmd);
3847 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
3848 
3849 	cred = wpa_config_get_cred(wpa_s->conf, id);
3850 	return wpas_remove_cred(wpa_s, cred);
3851 }
3852 
3853 
wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant * wpa_s,char * cmd)3854 static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
3855 					      char *cmd)
3856 {
3857 	int id;
3858 	struct wpa_cred *cred;
3859 	char *name, *value;
3860 
3861 	/* cmd: "<cred id> <variable name> <value>" */
3862 	name = os_strchr(cmd, ' ');
3863 	if (name == NULL)
3864 		return -1;
3865 	*name++ = '\0';
3866 
3867 	value = os_strchr(name, ' ');
3868 	if (value == NULL)
3869 		return -1;
3870 	*value++ = '\0';
3871 
3872 	id = atoi(cmd);
3873 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
3874 		   id, name);
3875 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3876 			      (u8 *) value, os_strlen(value));
3877 
3878 	cred = wpa_config_get_cred(wpa_s->conf, id);
3879 	if (cred == NULL) {
3880 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3881 			   id);
3882 		return -1;
3883 	}
3884 
3885 	if (wpa_config_set_cred(cred, name, value, 0) < 0) {
3886 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
3887 			   "variable '%s'", name);
3888 		return -1;
3889 	}
3890 
3891 	wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
3892 
3893 	return 0;
3894 }
3895 
3896 
wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)3897 static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
3898 					      char *cmd, char *buf,
3899 					      size_t buflen)
3900 {
3901 	int id;
3902 	size_t res;
3903 	struct wpa_cred *cred;
3904 	char *name, *value;
3905 
3906 	/* cmd: "<cred id> <variable name>" */
3907 	name = os_strchr(cmd, ' ');
3908 	if (name == NULL)
3909 		return -1;
3910 	*name++ = '\0';
3911 
3912 	id = atoi(cmd);
3913 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
3914 		   id, name);
3915 
3916 	cred = wpa_config_get_cred(wpa_s->conf, id);
3917 	if (cred == NULL) {
3918 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3919 			   id);
3920 		return -1;
3921 	}
3922 
3923 	value = wpa_config_get_cred_no_key(cred, name);
3924 	if (value == NULL) {
3925 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
3926 			   name);
3927 		return -1;
3928 	}
3929 
3930 	res = os_strlcpy(buf, value, buflen);
3931 	if (res >= buflen) {
3932 		os_free(value);
3933 		return -1;
3934 	}
3935 
3936 	os_free(value);
3937 
3938 	return res;
3939 }
3940 
3941 
3942 #ifndef CONFIG_NO_CONFIG_WRITE
wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant * wpa_s)3943 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
3944 {
3945 	int ret;
3946 
3947 	if (!wpa_s->conf->update_config) {
3948 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
3949 			   "to update configuration (update_config=0)");
3950 		return -1;
3951 	}
3952 
3953 	ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
3954 	if (ret) {
3955 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
3956 			   "update configuration");
3957 	} else {
3958 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
3959 			   " updated");
3960 	}
3961 
3962 	return ret;
3963 }
3964 #endif /* CONFIG_NO_CONFIG_WRITE */
3965 
3966 
3967 struct cipher_info {
3968 	unsigned int capa;
3969 	const char *name;
3970 	int group_only;
3971 };
3972 
3973 static const struct cipher_info ciphers[] = {
3974 	{ WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
3975 	{ WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
3976 	{ WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
3977 	{ WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
3978 #ifndef CONFIG_NO_TKIP
3979 	{ WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
3980 #endif /* CONFIG_NO_TKIP */
3981 	{ WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
3982 #ifdef CONFIG_WEP
3983 	{ WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
3984 	{ WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
3985 #endif /* CONFIG_WEP */
3986 };
3987 
3988 static const struct cipher_info ciphers_group_mgmt[] = {
3989 	{ WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
3990 	{ WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
3991 	{ WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
3992 	{ WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
3993 };
3994 
3995 
ctrl_iface_get_capability_pairwise(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)3996 static int ctrl_iface_get_capability_pairwise(int res, bool strict,
3997 					      struct wpa_driver_capa *capa,
3998 					      char *buf, size_t buflen)
3999 {
4000 	int ret;
4001 	char *pos, *end;
4002 	size_t len;
4003 	unsigned int i;
4004 
4005 	pos = buf;
4006 	end = pos + buflen;
4007 
4008 	if (res < 0) {
4009 		if (strict)
4010 			return 0;
4011 #ifdef CONFIG_NO_TKIP
4012 		len = os_strlcpy(buf, "CCMP NONE", buflen);
4013 #else /* CONFIG_NO_TKIP */
4014 		len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
4015 #endif /* CONFIG_NO_TKIP */
4016 		if (len >= buflen)
4017 			return -1;
4018 		return len;
4019 	}
4020 
4021 	for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
4022 		if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
4023 			ret = os_snprintf(pos, end - pos, "%s%s",
4024 					  pos == buf ? "" : " ",
4025 					  ciphers[i].name);
4026 			if (os_snprintf_error(end - pos, ret))
4027 				return pos - buf;
4028 			pos += ret;
4029 		}
4030 	}
4031 
4032 	return pos - buf;
4033 }
4034 
4035 
ctrl_iface_get_capability_group(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4036 static int ctrl_iface_get_capability_group(int res, bool strict,
4037 					   struct wpa_driver_capa *capa,
4038 					   char *buf, size_t buflen)
4039 {
4040 	int ret;
4041 	char *pos, *end;
4042 	size_t len;
4043 	unsigned int i;
4044 
4045 	pos = buf;
4046 	end = pos + buflen;
4047 
4048 	if (res < 0) {
4049 		if (strict)
4050 			return 0;
4051 #ifdef CONFIG_WEP
4052 #ifdef CONFIG_NO_TKIP
4053 		len = os_strlcpy(buf, "CCMP WEP104 WEP40", buflen);
4054 #else /* CONFIG_NO_TKIP */
4055 		len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
4056 #endif /* CONFIG_NO_TKIP */
4057 #else /* CONFIG_WEP */
4058 #ifdef CONFIG_NO_TKIP
4059 		len = os_strlcpy(buf, "CCMP", buflen);
4060 #else /* CONFIG_NO_TKIP */
4061 		len = os_strlcpy(buf, "CCMP TKIP", buflen);
4062 #endif /* CONFIG_NO_TKIP */
4063 #endif /* CONFIG_WEP */
4064 		if (len >= buflen)
4065 			return -1;
4066 		return len;
4067 	}
4068 
4069 	for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
4070 		if (capa->enc & ciphers[i].capa) {
4071 			ret = os_snprintf(pos, end - pos, "%s%s",
4072 					  pos == buf ? "" : " ",
4073 					  ciphers[i].name);
4074 			if (os_snprintf_error(end - pos, ret))
4075 				return pos - buf;
4076 			pos += ret;
4077 		}
4078 	}
4079 
4080 	return pos - buf;
4081 }
4082 
4083 
ctrl_iface_get_capability_group_mgmt(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4084 static int ctrl_iface_get_capability_group_mgmt(int res, bool strict,
4085 						struct wpa_driver_capa *capa,
4086 						char *buf, size_t buflen)
4087 {
4088 	int ret;
4089 	char *pos, *end;
4090 	unsigned int i;
4091 
4092 	pos = buf;
4093 	end = pos + buflen;
4094 
4095 	if (res < 0)
4096 		return 0;
4097 
4098 	for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
4099 		if (capa->enc & ciphers_group_mgmt[i].capa) {
4100 			ret = os_snprintf(pos, end - pos, "%s%s",
4101 					  pos == buf ? "" : " ",
4102 					  ciphers_group_mgmt[i].name);
4103 			if (os_snprintf_error(end - pos, ret))
4104 				return pos - buf;
4105 			pos += ret;
4106 		}
4107 	}
4108 
4109 	return pos - buf;
4110 }
4111 
4112 
iftype_str_to_index(const char * iftype_str)4113 static int iftype_str_to_index(const char *iftype_str)
4114 {
4115 	if (!iftype_str)
4116 		return WPA_IF_MAX;
4117 
4118 	if (os_strcmp(iftype_str, "STATION") == 0)
4119 		return WPA_IF_STATION;
4120 
4121 	if (os_strcmp(iftype_str, "AP_VLAN") == 0)
4122 		return WPA_IF_AP_VLAN;
4123 
4124 	if (os_strcmp(iftype_str, "AP") == 0)
4125 		return WPA_IF_AP_BSS;
4126 
4127 	if (os_strcmp(iftype_str, "P2P_GO") == 0)
4128 		return WPA_IF_P2P_GO;
4129 
4130 	if (os_strcmp(iftype_str, "P2P_CLIENT") == 0)
4131 		return WPA_IF_P2P_CLIENT;
4132 
4133 	if (os_strcmp(iftype_str, "P2P_DEVICE") == 0)
4134 		return WPA_IF_P2P_DEVICE;
4135 
4136 	if (os_strcmp(iftype_str, "MESH") == 0)
4137 		return WPA_IF_MESH;
4138 
4139 	if (os_strcmp(iftype_str, "IBSS") == 0)
4140 		return WPA_IF_IBSS;
4141 
4142 	if (os_strcmp(iftype_str, "NAN") == 0)
4143 		return WPA_IF_NAN;
4144 
4145 	return WPA_IF_MAX;
4146 }
4147 
4148 
ctrl_iface_get_capability_key_mgmt(int res,bool strict,struct wpa_driver_capa * capa,const char * iftype_str,char * buf,size_t buflen)4149 static int ctrl_iface_get_capability_key_mgmt(int res, bool strict,
4150 					      struct wpa_driver_capa *capa,
4151 					      const char *iftype_str,
4152 					      char *buf, size_t buflen)
4153 {
4154 	int ret;
4155 	unsigned int key_mgmt;
4156 	char *pos, *end;
4157 	size_t len;
4158 
4159 	pos = buf;
4160 	end = pos + buflen;
4161 
4162 	if (res < 0) {
4163 		if (strict)
4164 			return 0;
4165 		len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
4166 				 "NONE", buflen);
4167 		if (len >= buflen)
4168 			return -1;
4169 		return len;
4170 	}
4171 
4172 	if (iftype_str) {
4173 		enum wpa_driver_if_type iftype;
4174 
4175 		iftype = iftype_str_to_index(iftype_str);
4176 		if (iftype == WPA_IF_MAX)
4177 			return -1;
4178 		key_mgmt = capa->key_mgmt_iftype[iftype];
4179 	} else {
4180 		key_mgmt = capa->key_mgmt;
4181 	}
4182 
4183 	ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
4184 	if (os_snprintf_error(end - pos, ret))
4185 		return pos - buf;
4186 	pos += ret;
4187 
4188 	if (key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4189 			WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
4190 		ret = os_snprintf(pos, end - pos, " WPA-EAP");
4191 		if (os_snprintf_error(end - pos, ret))
4192 			return pos - buf;
4193 		pos += ret;
4194 	}
4195 
4196 	if (key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
4197 			WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
4198 		ret = os_snprintf(pos, end - pos, " WPA-PSK");
4199 		if (os_snprintf_error(end - pos, ret))
4200 			return pos - buf;
4201 		pos += ret;
4202 	}
4203 
4204 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
4205 		ret = os_snprintf(pos, end - pos, " WPA-NONE");
4206 		if (os_snprintf_error(end - pos, ret))
4207 			return pos - buf;
4208 		pos += ret;
4209 	}
4210 
4211 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WAPI_PSK) {
4212 		ret = os_snprintf(pos, end - pos, " WAPI-PSK");
4213 		if (os_snprintf_error(end - pos, ret))
4214 			return pos - buf;
4215 		pos += ret;
4216 	}
4217 
4218 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_TPK_HANDSHAKE) {
4219 		ret = os_snprintf(pos, end - pos, " TPK-HANDSHAKE");
4220 		if (os_snprintf_error(end - pos, ret))
4221 			return pos - buf;
4222 		pos += ret;
4223 	}
4224 
4225 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_CCKM) {
4226 		ret = os_snprintf(pos, end - pos, " CCKM");
4227 		if (os_snprintf_error(end - pos, ret))
4228 			return pos - buf;
4229 		pos += ret;
4230 	}
4231 
4232 #ifdef CONFIG_SUITEB
4233 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
4234 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
4235 		if (os_snprintf_error(end - pos, ret))
4236 			return pos - buf;
4237 		pos += ret;
4238 	}
4239 #endif /* CONFIG_SUITEB */
4240 #ifdef CONFIG_SUITEB192
4241 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
4242 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
4243 		if (os_snprintf_error(end - pos, ret))
4244 			return pos - buf;
4245 		pos += ret;
4246 	}
4247 #endif /* CONFIG_SUITEB192 */
4248 #ifdef CONFIG_OWE
4249 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OWE) {
4250 		ret = os_snprintf(pos, end - pos, " OWE");
4251 		if (os_snprintf_error(end - pos, ret))
4252 			return pos - buf;
4253 		pos += ret;
4254 	}
4255 #endif /* CONFIG_OWE */
4256 #ifdef CONFIG_DPP
4257 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_DPP) {
4258 		ret = os_snprintf(pos, end - pos, " DPP");
4259 		if (os_snprintf_error(end - pos, ret))
4260 			return pos - buf;
4261 		pos += ret;
4262 	}
4263 #endif /* CONFIG_DPP */
4264 #ifdef CONFIG_FILS
4265 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA256) {
4266 		ret = os_snprintf(pos, end - pos, " FILS-SHA256");
4267 		if (os_snprintf_error(end - pos, ret))
4268 			return pos - buf;
4269 		pos += ret;
4270 	}
4271 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA384) {
4272 		ret = os_snprintf(pos, end - pos, " FILS-SHA384");
4273 		if (os_snprintf_error(end - pos, ret))
4274 			return pos - buf;
4275 		pos += ret;
4276 	}
4277 #ifdef CONFIG_IEEE80211R
4278 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA256) {
4279 		ret = os_snprintf(pos, end - pos, " FT-FILS-SHA256");
4280 		if (os_snprintf_error(end - pos, ret))
4281 			return pos - buf;
4282 		pos += ret;
4283 	}
4284 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA384) {
4285 		ret = os_snprintf(pos, end - pos, " FT-FILS-SHA384");
4286 		if (os_snprintf_error(end - pos, ret))
4287 			return pos - buf;
4288 		pos += ret;
4289 	}
4290 #endif /* CONFIG_IEEE80211R */
4291 #endif /* CONFIG_FILS */
4292 #ifdef CONFIG_IEEE80211R
4293 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK) {
4294 		ret = os_snprintf(pos, end - pos, " FT-PSK");
4295 		if (os_snprintf_error(end - pos, ret))
4296 			return pos - buf;
4297 		pos += ret;
4298 	}
4299 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) {
4300 		ret = os_snprintf(pos, end - pos, " FT-EAP");
4301 		if (os_snprintf_error(end - pos, ret))
4302 			return pos - buf;
4303 		pos += ret;
4304 	}
4305 #ifdef CONFIG_SAE
4306 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_SAE) {
4307 		ret = os_snprintf(pos, end - pos, " FT-SAE");
4308 		if (os_snprintf_error(end - pos, ret))
4309 			return pos - buf;
4310 		pos += ret;
4311 	}
4312 #endif /* CONFIG_SAE */
4313 #ifdef CONFIG_SHA384
4314 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_802_1X_SHA384) {
4315 		ret = os_snprintf(pos, end - pos, " FT-EAP-SHA384");
4316 		if (os_snprintf_error(end - pos, ret))
4317 			return pos - buf;
4318 		pos += ret;
4319 	}
4320 #endif /* CONFIG_SHA384 */
4321 #endif /* CONFIG_IEEE80211R */
4322 #ifdef CONFIG_SAE
4323 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE) {
4324 		ret = os_snprintf(pos, end - pos, " SAE");
4325 		if (os_snprintf_error(end - pos, ret))
4326 			return pos - buf;
4327 		pos += ret;
4328 	}
4329 #endif /* CONFIG_SAE */
4330 #ifdef CONFIG_SHA256
4331 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_802_1X_SHA256) {
4332 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SHA256");
4333 		if (os_snprintf_error(end - pos, ret))
4334 			return pos - buf;
4335 		pos += ret;
4336 	}
4337 
4338 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_PSK_SHA256) {
4339 		ret = os_snprintf(pos, end - pos, " WPA-PSK-SHA256");
4340 		if (os_snprintf_error(end - pos, ret))
4341 			return pos - buf;
4342 		pos += ret;
4343 	}
4344 #endif /* CONFIG_SHA256 */
4345 #ifdef CONFIG_HS20
4346 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OSEN) {
4347 		ret = os_snprintf(pos, end - pos, " OSEN");
4348 		if (os_snprintf_error(end - pos, ret))
4349 			return pos - buf;
4350 		pos += ret;
4351 	}
4352 #endif /* CONFIG_HS20 */
4353 
4354 	return pos - buf;
4355 }
4356 
4357 
ctrl_iface_get_capability_proto(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4358 static int ctrl_iface_get_capability_proto(int res, bool strict,
4359 					   struct wpa_driver_capa *capa,
4360 					   char *buf, size_t buflen)
4361 {
4362 	int ret;
4363 	char *pos, *end;
4364 	size_t len;
4365 
4366 	pos = buf;
4367 	end = pos + buflen;
4368 
4369 	if (res < 0) {
4370 		if (strict)
4371 			return 0;
4372 		len = os_strlcpy(buf, "RSN WPA", buflen);
4373 		if (len >= buflen)
4374 			return -1;
4375 		return len;
4376 	}
4377 
4378 	if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
4379 			      WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
4380 		ret = os_snprintf(pos, end - pos, "%sRSN",
4381 				  pos == buf ? "" : " ");
4382 		if (os_snprintf_error(end - pos, ret))
4383 			return pos - buf;
4384 		pos += ret;
4385 	}
4386 
4387 	if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4388 			      WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
4389 		ret = os_snprintf(pos, end - pos, "%sWPA",
4390 				  pos == buf ? "" : " ");
4391 		if (os_snprintf_error(end - pos, ret))
4392 			return pos - buf;
4393 		pos += ret;
4394 	}
4395 
4396 	return pos - buf;
4397 }
4398 
4399 
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)4400 static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
4401 					      int res, bool strict,
4402 					      struct wpa_driver_capa *capa,
4403 					      char *buf, size_t buflen)
4404 {
4405 	int ret;
4406 	char *pos, *end;
4407 	size_t len;
4408 
4409 	pos = buf;
4410 	end = pos + buflen;
4411 
4412 	if (res < 0) {
4413 		if (strict)
4414 			return 0;
4415 		len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
4416 		if (len >= buflen)
4417 			return -1;
4418 		return len;
4419 	}
4420 
4421 	if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
4422 		ret = os_snprintf(pos, end - pos, "%sOPEN",
4423 				  pos == buf ? "" : " ");
4424 		if (os_snprintf_error(end - pos, ret))
4425 			return pos - buf;
4426 		pos += ret;
4427 	}
4428 
4429 	if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
4430 		ret = os_snprintf(pos, end - pos, "%sSHARED",
4431 				  pos == buf ? "" : " ");
4432 		if (os_snprintf_error(end - pos, ret))
4433 			return pos - buf;
4434 		pos += ret;
4435 	}
4436 
4437 	if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
4438 		ret = os_snprintf(pos, end - pos, "%sLEAP",
4439 				  pos == buf ? "" : " ");
4440 		if (os_snprintf_error(end - pos, ret))
4441 			return pos - buf;
4442 		pos += ret;
4443 	}
4444 
4445 #ifdef CONFIG_SAE
4446 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
4447 		ret = os_snprintf(pos, end - pos, "%sSAE",
4448 				  pos == buf ? "" : " ");
4449 		if (os_snprintf_error(end - pos, ret))
4450 			return pos - buf;
4451 		pos += ret;
4452 	}
4453 #endif /* CONFIG_SAE */
4454 
4455 #ifdef CONFIG_FILS
4456 	if (wpa_is_fils_supported(wpa_s)) {
4457 		ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITHOUT_PFS",
4458 				  pos == buf ? "" : " ");
4459 		if (os_snprintf_error(end - pos, ret))
4460 			return pos - buf;
4461 		pos += ret;
4462 	}
4463 
4464 #ifdef CONFIG_FILS_SK_PFS
4465 	if (wpa_is_fils_sk_pfs_supported(wpa_s)) {
4466 		ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITH_PFS",
4467 				  pos == buf ? "" : " ");
4468 		if (os_snprintf_error(end - pos, ret))
4469 			return pos - buf;
4470 		pos += ret;
4471 	}
4472 #endif /* CONFIG_FILS_SK_PFS */
4473 #endif /* CONFIG_FILS */
4474 
4475 #ifdef CONFIG_PASN
4476 	ret = os_snprintf(pos, end - pos, "%sPASN",
4477 			  pos == buf ? "" : " ");
4478 	if (os_snprintf_error(end - pos, ret))
4479 		return pos - buf;
4480 	pos += ret;
4481 
4482 #endif /* CONFIG_PASN */
4483 
4484 	return pos - buf;
4485 }
4486 
4487 
ctrl_iface_get_capability_modes(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4488 static int ctrl_iface_get_capability_modes(int res, bool strict,
4489 					   struct wpa_driver_capa *capa,
4490 					   char *buf, size_t buflen)
4491 {
4492 	int ret;
4493 	char *pos, *end;
4494 	size_t len;
4495 
4496 	pos = buf;
4497 	end = pos + buflen;
4498 
4499 	if (res < 0) {
4500 		if (strict)
4501 			return 0;
4502 		len = os_strlcpy(buf, "IBSS AP", buflen);
4503 		if (len >= buflen)
4504 			return -1;
4505 		return len;
4506 	}
4507 
4508 	if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
4509 		ret = os_snprintf(pos, end - pos, "%sIBSS",
4510 				  pos == buf ? "" : " ");
4511 		if (os_snprintf_error(end - pos, ret))
4512 			return pos - buf;
4513 		pos += ret;
4514 	}
4515 
4516 	if (capa->flags & WPA_DRIVER_FLAGS_AP) {
4517 		ret = os_snprintf(pos, end - pos, "%sAP",
4518 				  pos == buf ? "" : " ");
4519 		if (os_snprintf_error(end - pos, ret))
4520 			return pos - buf;
4521 		pos += ret;
4522 	}
4523 
4524 #ifdef CONFIG_MESH
4525 	if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
4526 		ret = os_snprintf(pos, end - pos, "%sMESH",
4527 				  pos == buf ? "" : " ");
4528 		if (os_snprintf_error(end - pos, ret))
4529 			return pos - buf;
4530 		pos += ret;
4531 	}
4532 #endif /* CONFIG_MESH */
4533 
4534 	return pos - buf;
4535 }
4536 
4537 
ctrl_iface_get_capability_channels(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)4538 static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
4539 					      char *buf, size_t buflen)
4540 {
4541 	struct hostapd_channel_data *chnl;
4542 	int ret, i, j;
4543 	char *pos, *end, *hmode;
4544 
4545 	pos = buf;
4546 	end = pos + buflen;
4547 
4548 	for (j = 0; j < wpa_s->hw.num_modes; j++) {
4549 		switch (wpa_s->hw.modes[j].mode) {
4550 		case HOSTAPD_MODE_IEEE80211B:
4551 			hmode = "B";
4552 			break;
4553 		case HOSTAPD_MODE_IEEE80211G:
4554 			hmode = "G";
4555 			break;
4556 		case HOSTAPD_MODE_IEEE80211A:
4557 			hmode = "A";
4558 			break;
4559 		case HOSTAPD_MODE_IEEE80211AD:
4560 			hmode = "AD";
4561 			break;
4562 		default:
4563 			continue;
4564 		}
4565 		ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
4566 		if (os_snprintf_error(end - pos, ret))
4567 			return pos - buf;
4568 		pos += ret;
4569 		chnl = wpa_s->hw.modes[j].channels;
4570 		for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
4571 			if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
4572 				continue;
4573 			ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
4574 			if (os_snprintf_error(end - pos, ret))
4575 				return pos - buf;
4576 			pos += ret;
4577 		}
4578 		ret = os_snprintf(pos, end - pos, "\n");
4579 		if (os_snprintf_error(end - pos, ret))
4580 			return pos - buf;
4581 		pos += ret;
4582 	}
4583 
4584 	return pos - buf;
4585 }
4586 
4587 
ctrl_iface_get_capability_freq(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)4588 static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
4589 					  char *buf, size_t buflen)
4590 {
4591 	struct hostapd_channel_data *chnl;
4592 	int ret, i, j;
4593 	char *pos, *end, *hmode;
4594 
4595 	pos = buf;
4596 	end = pos + buflen;
4597 
4598 	for (j = 0; j < wpa_s->hw.num_modes; j++) {
4599 		switch (wpa_s->hw.modes[j].mode) {
4600 		case HOSTAPD_MODE_IEEE80211B:
4601 			hmode = "B";
4602 			break;
4603 		case HOSTAPD_MODE_IEEE80211G:
4604 			hmode = "G";
4605 			break;
4606 		case HOSTAPD_MODE_IEEE80211A:
4607 			hmode = "A";
4608 			break;
4609 		case HOSTAPD_MODE_IEEE80211AD:
4610 			hmode = "AD";
4611 			break;
4612 		default:
4613 			continue;
4614 		}
4615 		ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
4616 				  hmode);
4617 		if (os_snprintf_error(end - pos, ret))
4618 			return pos - buf;
4619 		pos += ret;
4620 		chnl = wpa_s->hw.modes[j].channels;
4621 		for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
4622 			if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
4623 				continue;
4624 			ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
4625 					  chnl[i].chan, chnl[i].freq,
4626 					  chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
4627 					  " (NO_IR)" : "",
4628 					  chnl[i].flag & HOSTAPD_CHAN_RADAR ?
4629 					  " (DFS)" : "");
4630 
4631 			if (os_snprintf_error(end - pos, ret))
4632 				return pos - buf;
4633 			pos += ret;
4634 		}
4635 		ret = os_snprintf(pos, end - pos, "\n");
4636 		if (os_snprintf_error(end - pos, ret))
4637 			return pos - buf;
4638 		pos += ret;
4639 	}
4640 
4641 	return pos - buf;
4642 }
4643 
4644 
wpa_supplicant_ctrl_iface_get_capability(struct wpa_supplicant * wpa_s,const char * _field,char * buf,size_t buflen)4645 static int wpa_supplicant_ctrl_iface_get_capability(
4646 	struct wpa_supplicant *wpa_s, const char *_field, char *buf,
4647 	size_t buflen)
4648 {
4649 	struct wpa_driver_capa capa;
4650 	int res;
4651 	char *next_param, *curr_param, *iftype = NULL;
4652 	bool strict = false;
4653 	char field[50];
4654 	size_t len;
4655 
4656 	/* Determine whether or not strict checking was requested */
4657 	len = os_strlcpy(field, _field, sizeof(field));
4658 	if (len >= sizeof(field))
4659 		return -1;
4660 
4661 	next_param = os_strchr(field, ' ');
4662 	while (next_param) {
4663 		*next_param++ = '\0';
4664 		curr_param = next_param;
4665 		next_param = os_strchr(next_param, ' ');
4666 
4667 		if (next_param)
4668 			*next_param = '\0';
4669 
4670 		if (os_strcmp(curr_param, "strict") == 0)
4671 			strict = true;
4672 		else if (os_strncmp(curr_param, "iftype=", 7) == 0)
4673 			iftype = curr_param + 7;
4674 		else
4675 			return -1;
4676 	}
4677 
4678 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'%s%s%s",
4679 		   field, iftype ? " iftype=" : "", iftype ? iftype : "",
4680 		   strict ? " strict" : "");
4681 
4682 	if (os_strcmp(field, "eap") == 0) {
4683 		return eap_get_names(buf, buflen);
4684 	}
4685 
4686 	res = wpa_drv_get_capa(wpa_s, &capa);
4687 
4688 	if (os_strcmp(field, "pairwise") == 0)
4689 		return ctrl_iface_get_capability_pairwise(res, strict, &capa,
4690 							  buf, buflen);
4691 
4692 	if (os_strcmp(field, "group") == 0)
4693 		return ctrl_iface_get_capability_group(res, strict, &capa,
4694 						       buf, buflen);
4695 
4696 	if (os_strcmp(field, "group_mgmt") == 0)
4697 		return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
4698 							    buf, buflen);
4699 
4700 	if (os_strcmp(field, "key_mgmt") == 0)
4701 		return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
4702 							  iftype, buf, buflen);
4703 
4704 	if (os_strcmp(field, "proto") == 0)
4705 		return ctrl_iface_get_capability_proto(res, strict, &capa,
4706 						       buf, buflen);
4707 
4708 	if (os_strcmp(field, "auth_alg") == 0)
4709 		return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
4710 							  &capa, buf, buflen);
4711 
4712 	if (os_strcmp(field, "modes") == 0)
4713 		return ctrl_iface_get_capability_modes(res, strict, &capa,
4714 						       buf, buflen);
4715 
4716 	if (os_strcmp(field, "channels") == 0)
4717 		return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
4718 
4719 	if (os_strcmp(field, "freq") == 0)
4720 		return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
4721 
4722 #ifdef CONFIG_TDLS
4723 	if (os_strcmp(field, "tdls") == 0)
4724 		return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
4725 #endif /* CONFIG_TDLS */
4726 
4727 #ifdef CONFIG_ERP
4728 	if (os_strcmp(field, "erp") == 0) {
4729 		res = os_snprintf(buf, buflen, "ERP");
4730 		if (os_snprintf_error(buflen, res))
4731 			return -1;
4732 		return res;
4733 	}
4734 #endif /* CONFIG_EPR */
4735 
4736 #ifdef CONFIG_FIPS
4737 	if (os_strcmp(field, "fips") == 0) {
4738 		res = os_snprintf(buf, buflen, "FIPS");
4739 		if (os_snprintf_error(buflen, res))
4740 			return -1;
4741 		return res;
4742 	}
4743 #endif /* CONFIG_FIPS */
4744 
4745 #ifdef CONFIG_ACS
4746 	if (os_strcmp(field, "acs") == 0) {
4747 		res = os_snprintf(buf, buflen, "ACS");
4748 		if (os_snprintf_error(buflen, res))
4749 			return -1;
4750 		return res;
4751 	}
4752 #endif /* CONFIG_ACS */
4753 
4754 #ifdef CONFIG_FILS
4755 	if (os_strcmp(field, "fils") == 0) {
4756 #ifdef CONFIG_FILS_SK_PFS
4757 		if (wpa_is_fils_supported(wpa_s) &&
4758 		    wpa_is_fils_sk_pfs_supported(wpa_s)) {
4759 			res = os_snprintf(buf, buflen, "FILS FILS-SK-PFS");
4760 			if (os_snprintf_error(buflen, res))
4761 				return -1;
4762 			return res;
4763 		}
4764 #endif /* CONFIG_FILS_SK_PFS */
4765 
4766 		if (wpa_is_fils_supported(wpa_s)) {
4767 			res = os_snprintf(buf, buflen, "FILS");
4768 			if (os_snprintf_error(buflen, res))
4769 				return -1;
4770 			return res;
4771 		}
4772 	}
4773 #endif /* CONFIG_FILS */
4774 
4775 	if (os_strcmp(field, "multibss") == 0 && wpa_s->multi_bss_support) {
4776 		res = os_snprintf(buf, buflen, "MULTIBSS-STA");
4777 		if (os_snprintf_error(buflen, res))
4778 			return -1;
4779 		return res;
4780 	}
4781 
4782 #ifdef CONFIG_DPP
4783 	if (os_strcmp(field, "dpp") == 0) {
4784 #ifdef CONFIG_DPP3
4785 		res = os_snprintf(buf, buflen, "DPP=3");
4786 #elif defined(CONFIG_DPP2)
4787 		res = os_snprintf(buf, buflen, "DPP=2");
4788 #else /* CONFIG_DPP2 */
4789 		res = os_snprintf(buf, buflen, "DPP=1");
4790 #endif /* CONFIG_DPP2 */
4791 		if (os_snprintf_error(buflen, res))
4792 			return -1;
4793 		return res;
4794 	}
4795 #endif /* CONFIG_DPP */
4796 
4797 #ifdef CONFIG_SAE
4798 	if (os_strcmp(field, "sae") == 0 &&
4799 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE)) {
4800 #ifdef CONFIG_SAE_PK
4801 		res = os_snprintf(buf, buflen, "H2E PK");
4802 #else /* CONFIG_SAE_PK */
4803 		res = os_snprintf(buf, buflen, "H2E");
4804 #endif /* CONFIG_SAE_PK */
4805 		if (os_snprintf_error(buflen, res))
4806 			return -1;
4807 		return res;
4808 	}
4809 #endif /* CONFIG_SAE */
4810 
4811 #ifdef CONFIG_OCV
4812 	if (os_strcmp(field, "ocv") == 0) {
4813 		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
4814 		    (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_OCV))
4815 			res = os_snprintf(buf, buflen, "supported");
4816 		else
4817 			res = os_snprintf(buf, buflen, "not supported");
4818 		if (os_snprintf_error(buflen, res))
4819 			return -1;
4820 		return res;
4821 	}
4822 #endif /* CONFIG_OCV */
4823 
4824 	if (os_strcmp(field, "beacon_prot") == 0) {
4825 		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_BEACON_PROTECTION) ||
4826 		    (wpa_s->drv_flags2 &
4827 		     WPA_DRIVER_FLAGS2_BEACON_PROTECTION_CLIENT))
4828 			res = os_snprintf(buf, buflen, "supported");
4829 		else
4830 			res = os_snprintf(buf, buflen, "not supported");
4831 		if (os_snprintf_error(buflen, res))
4832 			return -1;
4833 		return res;
4834 	}
4835 
4836 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
4837 		   field);
4838 
4839 	return -1;
4840 }
4841 
4842 
4843 #ifdef CONFIG_INTERWORKING
anqp_add_hex(char * pos,char * end,const char * title,struct wpabuf * data)4844 static char * anqp_add_hex(char *pos, char *end, const char *title,
4845 			   struct wpabuf *data)
4846 {
4847 	char *start = pos;
4848 	size_t i;
4849 	int ret;
4850 	const u8 *d;
4851 
4852 	if (data == NULL)
4853 		return start;
4854 
4855 	ret = os_snprintf(pos, end - pos, "%s=", title);
4856 	if (os_snprintf_error(end - pos, ret))
4857 		return start;
4858 	pos += ret;
4859 
4860 	d = wpabuf_head_u8(data);
4861 	for (i = 0; i < wpabuf_len(data); i++) {
4862 		ret = os_snprintf(pos, end - pos, "%02x", *d++);
4863 		if (os_snprintf_error(end - pos, ret))
4864 			return start;
4865 		pos += ret;
4866 	}
4867 
4868 	ret = os_snprintf(pos, end - pos, "\n");
4869 	if (os_snprintf_error(end - pos, ret))
4870 		return start;
4871 	pos += ret;
4872 
4873 	return pos;
4874 }
4875 #endif /* CONFIG_INTERWORKING */
4876 
4877 
4878 #ifdef CONFIG_FILS
print_fils_indication(struct wpa_bss * bss,char * pos,char * end)4879 static int print_fils_indication(struct wpa_bss *bss, char *pos, char *end)
4880 {
4881 	char *start = pos;
4882 	const u8 *ie, *ie_end;
4883 	u16 info, realms;
4884 	int ret;
4885 
4886 	ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
4887 	if (!ie)
4888 		return 0;
4889 	ie_end = ie + 2 + ie[1];
4890 	ie += 2;
4891 	if (ie_end - ie < 2)
4892 		return -1;
4893 
4894 	info = WPA_GET_LE16(ie);
4895 	ie += 2;
4896 	ret = os_snprintf(pos, end - pos, "fils_info=%04x\n", info);
4897 	if (os_snprintf_error(end - pos, ret))
4898 		return 0;
4899 	pos += ret;
4900 
4901 	if (info & BIT(7)) {
4902 		/* Cache Identifier Included */
4903 		if (ie_end - ie < 2)
4904 			return -1;
4905 		ret = os_snprintf(pos, end - pos, "fils_cache_id=%02x%02x\n",
4906 				  ie[0], ie[1]);
4907 		if (os_snprintf_error(end - pos, ret))
4908 			return 0;
4909 		pos += ret;
4910 		ie += 2;
4911 	}
4912 
4913 	if (info & BIT(8)) {
4914 		/* HESSID Included */
4915 		if (ie_end - ie < ETH_ALEN)
4916 			return -1;
4917 		ret = os_snprintf(pos, end - pos, "fils_hessid=" MACSTR "\n",
4918 				  MAC2STR(ie));
4919 		if (os_snprintf_error(end - pos, ret))
4920 			return 0;
4921 		pos += ret;
4922 		ie += ETH_ALEN;
4923 	}
4924 
4925 	realms = (info & (BIT(3) | BIT(4) | BIT(5))) >> 3;
4926 	if (realms) {
4927 		if (ie_end - ie < realms * 2)
4928 			return -1;
4929 		ret = os_snprintf(pos, end - pos, "fils_realms=");
4930 		if (os_snprintf_error(end - pos, ret))
4931 			return 0;
4932 		pos += ret;
4933 
4934 		ret = wpa_snprintf_hex(pos, end - pos, ie, realms * 2);
4935 		if (ret <= 0)
4936 			return 0;
4937 		pos += ret;
4938 		ie += realms * 2;
4939 		ret = os_snprintf(pos, end - pos, "\n");
4940 		if (os_snprintf_error(end - pos, ret))
4941 			return 0;
4942 		pos += ret;
4943 	}
4944 
4945 	return pos - start;
4946 }
4947 #endif /* CONFIG_FILS */
4948 
4949 
print_bss_info(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,unsigned long mask,char * buf,size_t buflen)4950 static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4951 			  unsigned long mask, char *buf, size_t buflen)
4952 {
4953 	size_t i;
4954 	int ret;
4955 	char *pos, *end;
4956 	const u8 *ie, *ie2, *osen_ie, *mesh, *owe, *rsnxe;
4957 
4958 	pos = buf;
4959 	end = buf + buflen;
4960 
4961 	if (mask & WPA_BSS_MASK_ID) {
4962 		ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
4963 		if (os_snprintf_error(end - pos, ret))
4964 			return 0;
4965 		pos += ret;
4966 	}
4967 
4968 	if (mask & WPA_BSS_MASK_BSSID) {
4969 		ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
4970 				  MAC2STR(bss->bssid));
4971 		if (os_snprintf_error(end - pos, ret))
4972 			return 0;
4973 		pos += ret;
4974 	}
4975 
4976 	if (mask & WPA_BSS_MASK_FREQ) {
4977 		ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
4978 		if (os_snprintf_error(end - pos, ret))
4979 			return 0;
4980 		pos += ret;
4981 	}
4982 
4983 	if (mask & WPA_BSS_MASK_BEACON_INT) {
4984 		ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
4985 				  bss->beacon_int);
4986 		if (os_snprintf_error(end - pos, ret))
4987 			return 0;
4988 		pos += ret;
4989 	}
4990 
4991 	if (mask & WPA_BSS_MASK_CAPABILITIES) {
4992 		ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
4993 				  bss->caps);
4994 		if (os_snprintf_error(end - pos, ret))
4995 			return 0;
4996 		pos += ret;
4997 	}
4998 
4999 	if (mask & WPA_BSS_MASK_QUAL) {
5000 		ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
5001 		if (os_snprintf_error(end - pos, ret))
5002 			return 0;
5003 		pos += ret;
5004 	}
5005 
5006 	if (mask & WPA_BSS_MASK_NOISE) {
5007 		ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
5008 		if (os_snprintf_error(end - pos, ret))
5009 			return 0;
5010 		pos += ret;
5011 	}
5012 
5013 	if (mask & WPA_BSS_MASK_LEVEL) {
5014 		ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
5015 		if (os_snprintf_error(end - pos, ret))
5016 			return 0;
5017 		pos += ret;
5018 	}
5019 
5020 	if (mask & WPA_BSS_MASK_TSF) {
5021 		ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
5022 				  (unsigned long long) bss->tsf);
5023 		if (os_snprintf_error(end - pos, ret))
5024 			return 0;
5025 		pos += ret;
5026 	}
5027 
5028 	if (mask & WPA_BSS_MASK_AGE) {
5029 		struct os_reltime now;
5030 
5031 		os_get_reltime(&now);
5032 		ret = os_snprintf(pos, end - pos, "age=%d\n",
5033 				  (int) (now.sec - bss->last_update.sec));
5034 		if (os_snprintf_error(end - pos, ret))
5035 			return 0;
5036 		pos += ret;
5037 	}
5038 
5039 	if (mask & WPA_BSS_MASK_IE) {
5040 		ret = os_snprintf(pos, end - pos, "ie=");
5041 		if (os_snprintf_error(end - pos, ret))
5042 			return 0;
5043 		pos += ret;
5044 
5045 		ie = wpa_bss_ie_ptr(bss);
5046 		for (i = 0; i < bss->ie_len; i++) {
5047 			ret = os_snprintf(pos, end - pos, "%02x", *ie++);
5048 			if (os_snprintf_error(end - pos, ret))
5049 				return 0;
5050 			pos += ret;
5051 		}
5052 
5053 		ret = os_snprintf(pos, end - pos, "\n");
5054 		if (os_snprintf_error(end - pos, ret))
5055 			return 0;
5056 		pos += ret;
5057 	}
5058 
5059 	if (mask & WPA_BSS_MASK_FLAGS) {
5060 		ret = os_snprintf(pos, end - pos, "flags=");
5061 		if (os_snprintf_error(end - pos, ret))
5062 			return 0;
5063 		pos += ret;
5064 
5065 		mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
5066 
5067 		ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
5068 		if (ie)
5069 			pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
5070 						    2 + ie[1]);
5071 		ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
5072 		if (ie2)
5073 			pos = wpa_supplicant_ie_txt(pos, end,
5074 						    mesh ? "RSN" : "WPA2", ie2,
5075 						    2 + ie2[1]);
5076 		rsnxe = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
5077 		if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_H2E)) {
5078 			ret = os_snprintf(pos, end - pos, "[SAE-H2E]");
5079 			if (os_snprintf_error(end - pos, ret))
5080 				return -1;
5081 			pos += ret;
5082 		}
5083 		if (ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SAE_PK)) {
5084 			ret = os_snprintf(pos, end - pos, "[SAE-PK]");
5085 			if (os_snprintf_error(end - pos, ret))
5086 				return -1;
5087 			pos += ret;
5088 		}
5089 		osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
5090 		if (osen_ie)
5091 			pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
5092 						    osen_ie, 2 + osen_ie[1]);
5093 		owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
5094 		if (owe) {
5095 			ret = os_snprintf(
5096 				pos, end - pos,
5097 				ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
5098 			if (os_snprintf_error(end - pos, ret))
5099 				return 0;
5100 			pos += ret;
5101 		}
5102 		pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
5103 		if (!ie && !ie2 && !osen_ie &&
5104 		    (bss->caps & IEEE80211_CAP_PRIVACY)) {
5105 			ret = os_snprintf(pos, end - pos, "[WEP]");
5106 			if (os_snprintf_error(end - pos, ret))
5107 				return 0;
5108 			pos += ret;
5109 		}
5110 
5111 		if (mesh) {
5112 			ret = os_snprintf(pos, end - pos, "[MESH]");
5113 			if (os_snprintf_error(end - pos, ret))
5114 				return 0;
5115 			pos += ret;
5116 		}
5117 
5118 		if (bss_is_dmg(bss)) {
5119 			const char *s;
5120 			ret = os_snprintf(pos, end - pos, "[DMG]");
5121 			if (os_snprintf_error(end - pos, ret))
5122 				return 0;
5123 			pos += ret;
5124 			switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
5125 			case IEEE80211_CAP_DMG_IBSS:
5126 				s = "[IBSS]";
5127 				break;
5128 			case IEEE80211_CAP_DMG_AP:
5129 				s = "[ESS]";
5130 				break;
5131 			case IEEE80211_CAP_DMG_PBSS:
5132 				s = "[PBSS]";
5133 				break;
5134 			default:
5135 				s = "";
5136 				break;
5137 			}
5138 			ret = os_snprintf(pos, end - pos, "%s", s);
5139 			if (os_snprintf_error(end - pos, ret))
5140 				return 0;
5141 			pos += ret;
5142 		} else {
5143 			if (bss->caps & IEEE80211_CAP_IBSS) {
5144 				ret = os_snprintf(pos, end - pos, "[IBSS]");
5145 				if (os_snprintf_error(end - pos, ret))
5146 					return 0;
5147 				pos += ret;
5148 			}
5149 			if (bss->caps & IEEE80211_CAP_ESS) {
5150 				ret = os_snprintf(pos, end - pos, "[ESS]");
5151 				if (os_snprintf_error(end - pos, ret))
5152 					return 0;
5153 				pos += ret;
5154 			}
5155 		}
5156 		if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
5157 		    wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
5158 			ret = os_snprintf(pos, end - pos, "[P2P]");
5159 			if (os_snprintf_error(end - pos, ret))
5160 				return 0;
5161 			pos += ret;
5162 		}
5163 #ifdef CONFIG_HS20
5164 		if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
5165 			ret = os_snprintf(pos, end - pos, "[HS20]");
5166 			if (os_snprintf_error(end - pos, ret))
5167 				return 0;
5168 			pos += ret;
5169 		}
5170 #endif /* CONFIG_HS20 */
5171 #ifdef CONFIG_FILS
5172 		if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
5173 			ret = os_snprintf(pos, end - pos, "[FILS]");
5174 			if (os_snprintf_error(end - pos, ret))
5175 				return 0;
5176 			pos += ret;
5177 		}
5178 #endif /* CONFIG_FILS */
5179 #ifdef CONFIG_FST
5180 		if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
5181 			ret = os_snprintf(pos, end - pos, "[FST]");
5182 			if (os_snprintf_error(end - pos, ret))
5183 				return 0;
5184 			pos += ret;
5185 		}
5186 #endif /* CONFIG_FST */
5187 		if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_UTF_8_SSID)) {
5188 			ret = os_snprintf(pos, end - pos, "[UTF-8]");
5189 			if (os_snprintf_error(end - pos, ret))
5190 				return 0;
5191 			pos += ret;
5192 		}
5193 
5194 		ret = os_snprintf(pos, end - pos, "\n");
5195 		if (os_snprintf_error(end - pos, ret))
5196 			return 0;
5197 		pos += ret;
5198 	}
5199 
5200 	if (mask & WPA_BSS_MASK_SSID) {
5201 		ret = os_snprintf(pos, end - pos, "ssid=%s\n",
5202 				  wpa_ssid_txt(bss->ssid, bss->ssid_len));
5203 		if (os_snprintf_error(end - pos, ret))
5204 			return 0;
5205 		pos += ret;
5206 	}
5207 
5208 #ifdef CONFIG_WPS
5209 	if (mask & WPA_BSS_MASK_WPS_SCAN) {
5210 		ie = wpa_bss_ie_ptr(bss);
5211 		ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
5212 		if (ret >= end - pos)
5213 			return 0;
5214 		if (ret > 0)
5215 			pos += ret;
5216 	}
5217 #endif /* CONFIG_WPS */
5218 
5219 #ifdef CONFIG_P2P
5220 	if (mask & WPA_BSS_MASK_P2P_SCAN) {
5221 		ie = wpa_bss_ie_ptr(bss);
5222 		ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
5223 		if (ret >= end - pos)
5224 			return 0;
5225 		if (ret > 0)
5226 			pos += ret;
5227 	}
5228 #endif /* CONFIG_P2P */
5229 
5230 #ifdef CONFIG_WIFI_DISPLAY
5231 	if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
5232 		struct wpabuf *wfd;
5233 
5234 		ie = wpa_bss_ie_ptr(bss);
5235 		wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
5236 						  WFD_IE_VENDOR_TYPE);
5237 		if (wfd) {
5238 			ret = os_snprintf(pos, end - pos, "wfd_subelems=");
5239 			if (os_snprintf_error(end - pos, ret)) {
5240 				wpabuf_free(wfd);
5241 				return 0;
5242 			}
5243 			pos += ret;
5244 
5245 			pos += wpa_snprintf_hex(pos, end - pos,
5246 						wpabuf_head(wfd),
5247 						wpabuf_len(wfd));
5248 			wpabuf_free(wfd);
5249 
5250 			ret = os_snprintf(pos, end - pos, "\n");
5251 			if (os_snprintf_error(end - pos, ret))
5252 				return 0;
5253 			pos += ret;
5254 		}
5255 	}
5256 #endif /* CONFIG_WIFI_DISPLAY */
5257 
5258 #ifdef CONFIG_INTERWORKING
5259 	if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
5260 		struct wpa_bss_anqp *anqp = bss->anqp;
5261 		struct wpa_bss_anqp_elem *elem;
5262 
5263 		pos = anqp_add_hex(pos, end, "anqp_capability_list",
5264 				   anqp->capability_list);
5265 		pos = anqp_add_hex(pos, end, "anqp_venue_name",
5266 				   anqp->venue_name);
5267 		pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
5268 				   anqp->network_auth_type);
5269 		pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
5270 				   anqp->roaming_consortium);
5271 		pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
5272 				   anqp->ip_addr_type_availability);
5273 		pos = anqp_add_hex(pos, end, "anqp_nai_realm",
5274 				   anqp->nai_realm);
5275 		pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
5276 		pos = anqp_add_hex(pos, end, "anqp_domain_name",
5277 				   anqp->domain_name);
5278 		pos = anqp_add_hex(pos, end, "anqp_fils_realm_info",
5279 				   anqp->fils_realm_info);
5280 #ifdef CONFIG_HS20
5281 		pos = anqp_add_hex(pos, end, "hs20_capability_list",
5282 				   anqp->hs20_capability_list);
5283 		pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
5284 				   anqp->hs20_operator_friendly_name);
5285 		pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
5286 				   anqp->hs20_wan_metrics);
5287 		pos = anqp_add_hex(pos, end, "hs20_connection_capability",
5288 				   anqp->hs20_connection_capability);
5289 		pos = anqp_add_hex(pos, end, "hs20_operating_class",
5290 				   anqp->hs20_operating_class);
5291 		pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
5292 				   anqp->hs20_osu_providers_list);
5293 		pos = anqp_add_hex(pos, end, "hs20_operator_icon_metadata",
5294 				   anqp->hs20_operator_icon_metadata);
5295 		pos = anqp_add_hex(pos, end, "hs20_osu_providers_nai_list",
5296 				   anqp->hs20_osu_providers_nai_list);
5297 #endif /* CONFIG_HS20 */
5298 
5299 		dl_list_for_each(elem, &anqp->anqp_elems,
5300 				 struct wpa_bss_anqp_elem, list) {
5301 			char title[20];
5302 
5303 			os_snprintf(title, sizeof(title), "anqp[%u]",
5304 				    elem->infoid);
5305 			pos = anqp_add_hex(pos, end, title, elem->payload);
5306 			if (elem->protected_response) {
5307 				ret = os_snprintf(pos, end - pos,
5308 						  "protected-anqp-info[%u]=1\n",
5309 						  elem->infoid);
5310 				if (os_snprintf_error(end - pos, ret))
5311 					return 0;
5312 				pos += ret;
5313 			}
5314 		}
5315 	}
5316 #endif /* CONFIG_INTERWORKING */
5317 
5318 #ifdef CONFIG_MESH
5319 	if (mask & WPA_BSS_MASK_MESH_SCAN) {
5320 		ie = wpa_bss_ie_ptr(bss);
5321 		ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
5322 		if (ret >= end - pos)
5323 			return 0;
5324 		if (ret > 0)
5325 			pos += ret;
5326 	}
5327 #endif /* CONFIG_MESH */
5328 
5329 	if (mask & WPA_BSS_MASK_SNR) {
5330 		ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
5331 		if (os_snprintf_error(end - pos, ret))
5332 			return 0;
5333 		pos += ret;
5334 	}
5335 
5336 	if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
5337 		ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
5338 				  bss->est_throughput);
5339 		if (os_snprintf_error(end - pos, ret))
5340 			return 0;
5341 		pos += ret;
5342 	}
5343 
5344 #ifdef CONFIG_FST
5345 	if (mask & WPA_BSS_MASK_FST) {
5346 		ret = fst_ctrl_iface_mb_info(bss->bssid, pos, end - pos);
5347 		if (ret < 0 || ret >= end - pos)
5348 			return 0;
5349 		pos += ret;
5350 	}
5351 #endif /* CONFIG_FST */
5352 
5353 	if (mask & WPA_BSS_MASK_UPDATE_IDX) {
5354 		ret = os_snprintf(pos, end - pos, "update_idx=%u\n",
5355 				  bss->last_update_idx);
5356 		if (os_snprintf_error(end - pos, ret))
5357 			return 0;
5358 		pos += ret;
5359 	}
5360 
5361 	if ((mask & WPA_BSS_MASK_BEACON_IE) && bss->beacon_ie_len) {
5362 		ret = os_snprintf(pos, end - pos, "beacon_ie=");
5363 		if (os_snprintf_error(end - pos, ret))
5364 			return 0;
5365 		pos += ret;
5366 
5367 		ie = wpa_bss_ie_ptr(bss);
5368 		ie += bss->ie_len;
5369 		for (i = 0; i < bss->beacon_ie_len; i++) {
5370 			ret = os_snprintf(pos, end - pos, "%02x", *ie++);
5371 			if (os_snprintf_error(end - pos, ret))
5372 				return 0;
5373 			pos += ret;
5374 		}
5375 
5376 		ret = os_snprintf(pos, end - pos, "\n");
5377 		if (os_snprintf_error(end - pos, ret))
5378 			return 0;
5379 		pos += ret;
5380 	}
5381 
5382 #ifdef CONFIG_FILS
5383 	if (mask & WPA_BSS_MASK_FILS_INDICATION) {
5384 		ret = print_fils_indication(bss, pos, end);
5385 		if (ret < 0)
5386 			return 0;
5387 		pos += ret;
5388 	}
5389 #endif /* CONFIG_FILS */
5390 
5391 	if (mask & WPA_BSS_MASK_DELIM) {
5392 		ret = os_snprintf(pos, end - pos, "====\n");
5393 		if (os_snprintf_error(end - pos, ret))
5394 			return 0;
5395 		pos += ret;
5396 	}
5397 
5398 	return pos - buf;
5399 }
5400 
5401 
wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)5402 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
5403 					 const char *cmd, char *buf,
5404 					 size_t buflen)
5405 {
5406 	u8 bssid[ETH_ALEN];
5407 	size_t i;
5408 	struct wpa_bss *bss;
5409 	struct wpa_bss *bsslast = NULL;
5410 	struct dl_list *next;
5411 	int ret = 0;
5412 	int len;
5413 	char *ctmp, *end = buf + buflen;
5414 	unsigned long mask = WPA_BSS_MASK_ALL;
5415 
5416 	if (os_strncmp(cmd, "RANGE=", 6) == 0) {
5417 		if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
5418 			bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
5419 					    list_id);
5420 			bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
5421 					       list_id);
5422 		} else { /* N1-N2 */
5423 			unsigned int id1, id2;
5424 
5425 			if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
5426 				wpa_printf(MSG_INFO, "Wrong BSS range "
5427 					   "format");
5428 				return 0;
5429 			}
5430 
5431 			if (*(cmd + 6) == '-')
5432 				id1 = 0;
5433 			else
5434 				id1 = atoi(cmd + 6);
5435 			ctmp++;
5436 			if (*ctmp >= '0' && *ctmp <= '9')
5437 				id2 = atoi(ctmp);
5438 			else
5439 				id2 = (unsigned int) -1;
5440 			bss = wpa_bss_get_id_range(wpa_s, id1, id2);
5441 			if (id2 == (unsigned int) -1)
5442 				bsslast = dl_list_last(&wpa_s->bss_id,
5443 						       struct wpa_bss,
5444 						       list_id);
5445 			else {
5446 				bsslast = wpa_bss_get_id(wpa_s, id2);
5447 				if (bsslast == NULL && bss && id2 > id1) {
5448 					struct wpa_bss *tmp = bss;
5449 					for (;;) {
5450 						next = tmp->list_id.next;
5451 						if (next == &wpa_s->bss_id)
5452 							break;
5453 						tmp = dl_list_entry(
5454 							next, struct wpa_bss,
5455 							list_id);
5456 						if (tmp->id > id2)
5457 							break;
5458 						bsslast = tmp;
5459 					}
5460 				}
5461 			}
5462 		}
5463 	} else if (os_strncmp(cmd, "FIRST", 5) == 0)
5464 		bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
5465 	else if (os_strncmp(cmd, "LAST", 4) == 0)
5466 		bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
5467 	else if (os_strncmp(cmd, "ID-", 3) == 0) {
5468 		i = atoi(cmd + 3);
5469 		bss = wpa_bss_get_id(wpa_s, i);
5470 	} else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
5471 		i = atoi(cmd + 5);
5472 		bss = wpa_bss_get_id(wpa_s, i);
5473 		if (bss) {
5474 			next = bss->list_id.next;
5475 			if (next == &wpa_s->bss_id)
5476 				bss = NULL;
5477 			else
5478 				bss = dl_list_entry(next, struct wpa_bss,
5479 						    list_id);
5480 		}
5481 	} else if (os_strncmp(cmd, "CURRENT", 7) == 0) {
5482 		bss = wpa_s->current_bss;
5483 #ifdef CONFIG_P2P
5484 	} else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
5485 		if (hwaddr_aton(cmd + 13, bssid) == 0)
5486 			bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
5487 		else
5488 			bss = NULL;
5489 #endif /* CONFIG_P2P */
5490 	} else if (hwaddr_aton(cmd, bssid) == 0)
5491 		bss = wpa_bss_get_bssid(wpa_s, bssid);
5492 	else {
5493 		struct wpa_bss *tmp;
5494 		i = atoi(cmd);
5495 		bss = NULL;
5496 		dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
5497 		{
5498 			if (i == 0) {
5499 				bss = tmp;
5500 				break;
5501 			}
5502 			i--;
5503 		}
5504 	}
5505 
5506 	if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
5507 		mask = strtoul(ctmp + 5, NULL, 0x10);
5508 		if (mask == 0)
5509 			mask = WPA_BSS_MASK_ALL;
5510 	}
5511 
5512 	if (bss == NULL)
5513 		return 0;
5514 
5515 	if (bsslast == NULL)
5516 		bsslast = bss;
5517 	do {
5518 		len = print_bss_info(wpa_s, bss, mask, buf, buflen);
5519 		ret += len;
5520 		buf += len;
5521 		buflen -= len;
5522 		if (bss == bsslast) {
5523 			if ((mask & WPA_BSS_MASK_DELIM) && len &&
5524 			    (bss == dl_list_last(&wpa_s->bss_id,
5525 						 struct wpa_bss, list_id))) {
5526 				int res;
5527 
5528 				res = os_snprintf(buf - 5, end - buf + 5,
5529 						  "####\n");
5530 				if (os_snprintf_error(end - buf + 5, res)) {
5531 					wpa_printf(MSG_DEBUG,
5532 						   "Could not add end delim");
5533 				}
5534 			}
5535 			break;
5536 		}
5537 		next = bss->list_id.next;
5538 		if (next == &wpa_s->bss_id)
5539 			break;
5540 		bss = dl_list_entry(next, struct wpa_bss, list_id);
5541 	} while (bss && len);
5542 
5543 	return ret;
5544 }
5545 
5546 
wpa_supplicant_ctrl_iface_ap_scan(struct wpa_supplicant * wpa_s,char * cmd)5547 static int wpa_supplicant_ctrl_iface_ap_scan(
5548 	struct wpa_supplicant *wpa_s, char *cmd)
5549 {
5550 	int ap_scan = atoi(cmd);
5551 	return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
5552 }
5553 
5554 
wpa_supplicant_ctrl_iface_scan_interval(struct wpa_supplicant * wpa_s,char * cmd)5555 static int wpa_supplicant_ctrl_iface_scan_interval(
5556 	struct wpa_supplicant *wpa_s, char *cmd)
5557 {
5558 	int scan_int = atoi(cmd);
5559 	return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
5560 }
5561 
5562 
wpa_supplicant_ctrl_iface_bss_expire_age(struct wpa_supplicant * wpa_s,char * cmd)5563 static int wpa_supplicant_ctrl_iface_bss_expire_age(
5564 	struct wpa_supplicant *wpa_s, char *cmd)
5565 {
5566 	int expire_age = atoi(cmd);
5567 	return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
5568 }
5569 
5570 
wpa_supplicant_ctrl_iface_bss_expire_count(struct wpa_supplicant * wpa_s,char * cmd)5571 static int wpa_supplicant_ctrl_iface_bss_expire_count(
5572 	struct wpa_supplicant *wpa_s, char *cmd)
5573 {
5574 	int expire_count = atoi(cmd);
5575 	return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
5576 }
5577 
5578 
wpa_supplicant_ctrl_iface_bss_flush(struct wpa_supplicant * wpa_s,char * cmd)5579 static void wpa_supplicant_ctrl_iface_bss_flush(
5580 	struct wpa_supplicant *wpa_s, char *cmd)
5581 {
5582 	int flush_age = atoi(cmd);
5583 
5584 	if (flush_age == 0)
5585 		wpa_bss_flush(wpa_s);
5586 	else
5587 		wpa_bss_flush_by_age(wpa_s, flush_age);
5588 }
5589 
5590 
5591 #ifdef CONFIG_TESTING_OPTIONS
wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant * wpa_s)5592 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
5593 {
5594 	wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
5595 	/* MLME-DELETEKEYS.request */
5596 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL,
5597 			0, KEY_FLAG_GROUP);
5598 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL,
5599 			0, KEY_FLAG_GROUP);
5600 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL,
5601 			0, KEY_FLAG_GROUP);
5602 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL,
5603 			0, KEY_FLAG_GROUP);
5604 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL,
5605 			0, KEY_FLAG_GROUP);
5606 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL,
5607 			0, KEY_FLAG_GROUP);
5608 
5609 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
5610 			0, KEY_FLAG_PAIRWISE);
5611 	if (wpa_sm_ext_key_id(wpa_s->wpa))
5612 		wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 1, 0,
5613 				NULL, 0, NULL, 0, KEY_FLAG_PAIRWISE);
5614 	/* MLME-SETPROTECTION.request(None) */
5615 	wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
5616 				   MLME_SETPROTECTION_PROTECT_TYPE_NONE,
5617 				   MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
5618 	wpa_sm_drop_sa(wpa_s->wpa);
5619 }
5620 #endif /* CONFIG_TESTING_OPTIONS */
5621 
5622 
wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant * wpa_s,char * addr)5623 static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
5624 					  char *addr)
5625 {
5626 #ifdef CONFIG_NO_SCAN_PROCESSING
5627 	return -1;
5628 #else /* CONFIG_NO_SCAN_PROCESSING */
5629 	u8 bssid[ETH_ALEN];
5630 	struct wpa_bss *bss;
5631 	struct wpa_ssid *ssid = wpa_s->current_ssid;
5632 	struct wpa_radio_work *already_connecting;
5633 
5634 	if (hwaddr_aton(addr, bssid)) {
5635 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
5636 			   "address '%s'", addr);
5637 		return -1;
5638 	}
5639 
5640 	wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
5641 
5642 	if (!ssid) {
5643 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
5644 			   "configuration known for the target AP");
5645 		return -1;
5646 	}
5647 
5648 	bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
5649 	if (!bss) {
5650 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
5651 			   "from BSS table");
5652 		return -1;
5653 	}
5654 
5655 	/*
5656 	 * TODO: Find best network configuration block from configuration to
5657 	 * allow roaming to other networks
5658 	 */
5659 
5660 	already_connecting = radio_work_pending(wpa_s, "sme-connect");
5661 	wpa_s->reassociate = 1;
5662 	wpa_supplicant_connect(wpa_s, bss, ssid);
5663 
5664 	/*
5665 	 * Indicate that an explicitly requested roam is in progress so scan
5666 	 * results that come in before the 'sme-connect' radio work gets
5667 	 * executed do not override the original connection attempt.
5668 	 */
5669 	if (!already_connecting && radio_work_pending(wpa_s, "sme-connect"))
5670 		wpa_s->roam_in_progress = true;
5671 
5672 	return 0;
5673 #endif /* CONFIG_NO_SCAN_PROCESSING */
5674 }
5675 
5676 
5677 #ifdef CONFIG_P2P
p2p_ctrl_find(struct wpa_supplicant * wpa_s,char * cmd)5678 static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
5679 {
5680 	unsigned int timeout = atoi(cmd);
5681 	enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
5682 	u8 dev_id[ETH_ALEN], *_dev_id = NULL;
5683 	u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
5684 	char *pos;
5685 	unsigned int search_delay;
5686 	const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
5687 	u8 seek_count = 0;
5688 	int freq = 0;
5689 	bool include_6ghz = false;
5690 
5691 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5692 		wpa_dbg(wpa_s, MSG_INFO,
5693 			"Reject P2P_FIND since interface is disabled");
5694 		return -1;
5695 	}
5696 
5697 	if (os_strstr(cmd, " include_6ghz"))
5698 		include_6ghz = true;
5699 	if (os_strstr(cmd, "type=social"))
5700 		type = P2P_FIND_ONLY_SOCIAL;
5701 	else if (os_strstr(cmd, "type=progressive"))
5702 		type = P2P_FIND_PROGRESSIVE;
5703 
5704 	pos = os_strstr(cmd, "dev_id=");
5705 	if (pos) {
5706 		pos += 7;
5707 		if (hwaddr_aton(pos, dev_id))
5708 			return -1;
5709 		_dev_id = dev_id;
5710 	}
5711 
5712 	pos = os_strstr(cmd, "dev_type=");
5713 	if (pos) {
5714 		pos += 9;
5715 		if (wps_dev_type_str2bin(pos, dev_type) < 0)
5716 			return -1;
5717 		_dev_type = dev_type;
5718 	}
5719 
5720 	pos = os_strstr(cmd, "delay=");
5721 	if (pos) {
5722 		pos += 6;
5723 		search_delay = atoi(pos);
5724 	} else
5725 		search_delay = wpas_p2p_search_delay(wpa_s);
5726 
5727 	pos = os_strstr(cmd, "freq=");
5728 	if (pos) {
5729 		pos += 5;
5730 		freq = atoi(pos);
5731 		if (freq <= 0)
5732 			return -1;
5733 	}
5734 
5735 	/* Must be searched for last, because it adds nul termination */
5736 	pos = os_strstr(cmd, " seek=");
5737 	if (pos)
5738 		pos += 6;
5739 	while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
5740 		char *term;
5741 
5742 		_seek[seek_count++] = pos;
5743 		seek = _seek;
5744 		term = os_strchr(pos, ' ');
5745 		if (!term)
5746 			break;
5747 		*term = '\0';
5748 		pos = os_strstr(term + 1, "seek=");
5749 		if (pos)
5750 			pos += 5;
5751 	}
5752 	if (seek_count > P2P_MAX_QUERY_HASH) {
5753 		seek[0] = NULL;
5754 		seek_count = 1;
5755 	}
5756 
5757 	return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
5758 			     _dev_id, search_delay, seek_count, seek, freq,
5759 			     include_6ghz);
5760 }
5761 
5762 
p2ps_ctrl_parse_cpt_priority(const char * pos,u8 * cpt)5763 static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
5764 {
5765 	const char *last = NULL;
5766 	const char *token;
5767 	long int token_len;
5768 	unsigned int i;
5769 
5770 	/* Expected predefined CPT names delimited by ':' */
5771 	for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
5772 		if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
5773 			wpa_printf(MSG_ERROR,
5774 				   "P2PS: CPT name list is too long, expected up to %d names",
5775 				   P2PS_FEATURE_CAPAB_CPT_MAX);
5776 			cpt[0] = 0;
5777 			return -1;
5778 		}
5779 
5780 		token_len = last - token;
5781 
5782 		if (token_len  == 3 &&
5783 		    os_memcmp(token, "UDP", token_len) == 0) {
5784 			cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
5785 		} else if (token_len == 3 &&
5786 			   os_memcmp(token, "MAC", token_len) == 0) {
5787 			cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
5788 		} else {
5789 			wpa_printf(MSG_ERROR,
5790 				   "P2PS: Unsupported CPT name '%s'", token);
5791 			cpt[0] = 0;
5792 			return -1;
5793 		}
5794 
5795 		if (isblank((unsigned char) *last)) {
5796 			i++;
5797 			break;
5798 		}
5799 	}
5800 	cpt[i] = 0;
5801 	return 0;
5802 }
5803 
5804 
p2p_parse_asp_provision_cmd(const char * cmd)5805 static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
5806 {
5807 	struct p2ps_provision *p2ps_prov;
5808 	char *pos;
5809 	size_t info_len = 0;
5810 	char *info = NULL;
5811 	u8 role = P2PS_SETUP_NONE;
5812 	long long unsigned val;
5813 	int i;
5814 
5815 	pos = os_strstr(cmd, "info=");
5816 	if (pos) {
5817 		pos += 5;
5818 		info_len = os_strlen(pos);
5819 
5820 		if (info_len) {
5821 			info = os_malloc(info_len + 1);
5822 			if (info) {
5823 				info_len = utf8_unescape(pos, info_len,
5824 							 info, info_len + 1);
5825 			} else
5826 				info_len = 0;
5827 		}
5828 	}
5829 
5830 	p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
5831 	if (p2ps_prov == NULL) {
5832 		os_free(info);
5833 		return NULL;
5834 	}
5835 
5836 	if (info) {
5837 		os_memcpy(p2ps_prov->info, info, info_len);
5838 		p2ps_prov->info[info_len] = '\0';
5839 		os_free(info);
5840 	}
5841 
5842 	pos = os_strstr(cmd, "status=");
5843 	if (pos)
5844 		p2ps_prov->status = atoi(pos + 7);
5845 	else
5846 		p2ps_prov->status = -1;
5847 
5848 	pos = os_strstr(cmd, "adv_id=");
5849 	if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
5850 		goto invalid_args;
5851 	p2ps_prov->adv_id = val;
5852 
5853 	pos = os_strstr(cmd, "method=");
5854 	if (pos)
5855 		p2ps_prov->method = strtol(pos + 7, NULL, 16);
5856 	else
5857 		p2ps_prov->method = 0;
5858 
5859 	pos = os_strstr(cmd, "session=");
5860 	if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
5861 		goto invalid_args;
5862 	p2ps_prov->session_id = val;
5863 
5864 	pos = os_strstr(cmd, "adv_mac=");
5865 	if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
5866 		goto invalid_args;
5867 
5868 	pos = os_strstr(cmd, "session_mac=");
5869 	if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
5870 		goto invalid_args;
5871 
5872 	pos = os_strstr(cmd, "cpt=");
5873 	if (pos) {
5874 		if (p2ps_ctrl_parse_cpt_priority(pos + 4,
5875 						 p2ps_prov->cpt_priority))
5876 			goto invalid_args;
5877 	} else {
5878 		p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
5879 	}
5880 
5881 	for (i = 0; p2ps_prov->cpt_priority[i]; i++)
5882 		p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
5883 
5884 	/* force conncap with tstCap (no validity checks) */
5885 	pos = os_strstr(cmd, "tstCap=");
5886 	if (pos) {
5887 		role = strtol(pos + 7, NULL, 16);
5888 	} else {
5889 		pos = os_strstr(cmd, "role=");
5890 		if (pos) {
5891 			role = strtol(pos + 5, NULL, 16);
5892 			if (role != P2PS_SETUP_CLIENT &&
5893 			    role != P2PS_SETUP_GROUP_OWNER)
5894 				role = P2PS_SETUP_NONE;
5895 		}
5896 	}
5897 	p2ps_prov->role = role;
5898 
5899 	return p2ps_prov;
5900 
5901 invalid_args:
5902 	os_free(p2ps_prov);
5903 	return NULL;
5904 }
5905 
5906 
p2p_ctrl_asp_provision_resp(struct wpa_supplicant * wpa_s,char * cmd)5907 static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
5908 {
5909 	u8 addr[ETH_ALEN];
5910 	struct p2ps_provision *p2ps_prov;
5911 	char *pos;
5912 
5913 	/* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
5914 
5915 	wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
5916 
5917 	if (hwaddr_aton(cmd, addr))
5918 		return -1;
5919 
5920 	pos = cmd + 17;
5921 	if (*pos != ' ')
5922 		return -1;
5923 
5924 	p2ps_prov = p2p_parse_asp_provision_cmd(pos);
5925 	if (!p2ps_prov)
5926 		return -1;
5927 
5928 	if (p2ps_prov->status < 0) {
5929 		os_free(p2ps_prov);
5930 		return -1;
5931 	}
5932 
5933 	return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
5934 				  p2ps_prov);
5935 }
5936 
5937 
p2p_ctrl_asp_provision(struct wpa_supplicant * wpa_s,char * cmd)5938 static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
5939 {
5940 	u8 addr[ETH_ALEN];
5941 	struct p2ps_provision *p2ps_prov;
5942 	char *pos;
5943 
5944 	/* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
5945 	 *        session=<ses_id> mac=<ses_mac> [info=<infodata>]
5946 	 */
5947 
5948 	wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
5949 	if (hwaddr_aton(cmd, addr))
5950 		return -1;
5951 
5952 	pos = cmd + 17;
5953 	if (*pos != ' ')
5954 		return -1;
5955 
5956 	p2ps_prov = p2p_parse_asp_provision_cmd(pos);
5957 	if (!p2ps_prov)
5958 		return -1;
5959 
5960 	p2ps_prov->pd_seeker = 1;
5961 
5962 	return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
5963 				  p2ps_prov);
5964 }
5965 
5966 
parse_freq(int chwidth,int freq2)5967 static int parse_freq(int chwidth, int freq2)
5968 {
5969 	if (freq2 < 0)
5970 		return -1;
5971 	if (freq2)
5972 		return CHANWIDTH_80P80MHZ;
5973 
5974 	switch (chwidth) {
5975 	case 0:
5976 	case 20:
5977 	case 40:
5978 		return CHANWIDTH_USE_HT;
5979 	case 80:
5980 		return CHANWIDTH_80MHZ;
5981 	case 160:
5982 		return CHANWIDTH_160MHZ;
5983 	default:
5984 		wpa_printf(MSG_DEBUG, "Unknown max oper bandwidth: %d",
5985 			   chwidth);
5986 		return -1;
5987 	}
5988 }
5989 
5990 
p2p_ctrl_connect(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)5991 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
5992 			    char *buf, size_t buflen)
5993 {
5994 	u8 addr[ETH_ALEN];
5995 	char *pos, *pos2;
5996 	char *pin = NULL;
5997 	enum p2p_wps_method wps_method;
5998 	int new_pin;
5999 	int ret;
6000 	int persistent_group, persistent_id = -1;
6001 	int join;
6002 	int auth;
6003 	int automatic;
6004 	int go_intent = -1;
6005 	int freq = 0;
6006 	int pd;
6007 	int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
6008 	int edmg;
6009 	u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
6010 	size_t group_ssid_len = 0;
6011 	int he;
6012 	bool allow_6ghz;
6013 
6014 	if (!wpa_s->global->p2p_init_wpa_s)
6015 		return -1;
6016 	if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
6017 		wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
6018 			wpa_s->global->p2p_init_wpa_s->ifname);
6019 		wpa_s = wpa_s->global->p2p_init_wpa_s;
6020 	}
6021 
6022 	/* <addr> <"pbc" | "pin" | PIN> [label|display|keypad|p2ps]
6023 	 * [persistent|persistent=<network id>]
6024 	 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
6025 	 * [ht40] [vht] [he] [edmg] [auto] [ssid=<hexdump>] */
6026 
6027 	if (hwaddr_aton(cmd, addr))
6028 		return -1;
6029 
6030 	pos = cmd + 17;
6031 	if (*pos != ' ')
6032 		return -1;
6033 	pos++;
6034 
6035 	persistent_group = os_strstr(pos, " persistent") != NULL;
6036 	pos2 = os_strstr(pos, " persistent=");
6037 	if (pos2) {
6038 		struct wpa_ssid *ssid;
6039 		persistent_id = atoi(pos2 + 12);
6040 		ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6041 		if (ssid == NULL || ssid->disabled != 2 ||
6042 		    ssid->mode != WPAS_MODE_P2P_GO) {
6043 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
6044 				   "SSID id=%d for persistent P2P group (GO)",
6045 				   persistent_id);
6046 			return -1;
6047 		}
6048 	}
6049 	join = os_strstr(pos, " join") != NULL;
6050 	allow_6ghz = os_strstr(pos, " allow_6ghz") != NULL;
6051 	auth = os_strstr(pos, " auth") != NULL;
6052 	automatic = os_strstr(pos, " auto") != NULL;
6053 	pd = os_strstr(pos, " provdisc") != NULL;
6054 	vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
6055 	ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
6056 		vht;
6057 	he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
6058 	edmg = (os_strstr(cmd, " edmg") != NULL) || wpa_s->conf->p2p_go_edmg;
6059 
6060 	pos2 = os_strstr(pos, " go_intent=");
6061 	if (pos2) {
6062 		pos2 += 11;
6063 		go_intent = atoi(pos2);
6064 		if (go_intent < 0 || go_intent > 15)
6065 			return -1;
6066 	}
6067 
6068 	pos2 = os_strstr(pos, " freq=");
6069 	if (pos2) {
6070 		pos2 += 6;
6071 		freq = atoi(pos2);
6072 		if (freq <= 0)
6073 			return -1;
6074 	}
6075 
6076 	pos2 = os_strstr(pos, " freq2=");
6077 	if (pos2)
6078 		freq2 = atoi(pos2 + 7);
6079 
6080 	pos2 = os_strstr(pos, " max_oper_chwidth=");
6081 	if (pos2)
6082 		chwidth = atoi(pos2 + 18);
6083 
6084 	max_oper_chwidth = parse_freq(chwidth, freq2);
6085 	if (max_oper_chwidth < 0)
6086 		return -1;
6087 
6088 	if (allow_6ghz && chwidth == 40)
6089 		max_oper_chwidth = CHANWIDTH_40MHZ_6GHZ;
6090 
6091 	pos2 = os_strstr(pos, " ssid=");
6092 	if (pos2) {
6093 		char *end;
6094 
6095 		pos2 += 6;
6096 		end = os_strchr(pos2, ' ');
6097 		if (!end)
6098 			group_ssid_len = os_strlen(pos2) / 2;
6099 		else
6100 			group_ssid_len = (end - pos2) / 2;
6101 		if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
6102 		    hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
6103 			return -1;
6104 		group_ssid = _group_ssid;
6105 	}
6106 
6107 	if (os_strncmp(pos, "pin", 3) == 0) {
6108 		/* Request random PIN (to be displayed) and enable the PIN */
6109 		wps_method = WPS_PIN_DISPLAY;
6110 	} else if (os_strncmp(pos, "pbc", 3) == 0) {
6111 		wps_method = WPS_PBC;
6112 	} else if (os_strstr(pos, "p2ps") != NULL) {
6113 		wps_method = WPS_P2PS;
6114 	} else {
6115 		pin = pos;
6116 		pos = os_strchr(pin, ' ');
6117 		wps_method = WPS_PIN_KEYPAD;
6118 		if (pos) {
6119 			*pos++ = '\0';
6120 			if (os_strncmp(pos, "display", 7) == 0)
6121 				wps_method = WPS_PIN_DISPLAY;
6122 		}
6123 		if (!wps_pin_str_valid(pin)) {
6124 			os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
6125 			return 17;
6126 		}
6127 	}
6128 
6129 	new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
6130 				   persistent_group, automatic, join,
6131 				   auth, go_intent, freq, freq2, persistent_id,
6132 				   pd, ht40, vht, max_oper_chwidth, he, edmg,
6133 				   group_ssid, group_ssid_len, allow_6ghz);
6134 	if (new_pin == -2) {
6135 		os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
6136 		return 25;
6137 	}
6138 	if (new_pin == -3) {
6139 		os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
6140 		return 25;
6141 	}
6142 	if (new_pin < 0)
6143 		return -1;
6144 	if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
6145 		ret = os_snprintf(buf, buflen, "%08d", new_pin);
6146 		if (os_snprintf_error(buflen, ret))
6147 			return -1;
6148 		return ret;
6149 	}
6150 
6151 	os_memcpy(buf, "OK\n", 3);
6152 	return 3;
6153 }
6154 
6155 
p2p_ctrl_listen(struct wpa_supplicant * wpa_s,char * cmd)6156 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
6157 {
6158 	unsigned int timeout = atoi(cmd);
6159 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6160 		wpa_dbg(wpa_s, MSG_INFO,
6161 			"Reject P2P_LISTEN since interface is disabled");
6162 		return -1;
6163 	}
6164 	return wpas_p2p_listen(wpa_s, timeout);
6165 }
6166 
6167 
p2p_ctrl_prov_disc(struct wpa_supplicant * wpa_s,char * cmd)6168 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
6169 {
6170 	u8 addr[ETH_ALEN];
6171 	char *pos;
6172 	enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
6173 
6174 	/* <addr> <config method> [join|auto] */
6175 
6176 	if (hwaddr_aton(cmd, addr))
6177 		return -1;
6178 
6179 	pos = cmd + 17;
6180 	if (*pos != ' ')
6181 		return -1;
6182 	pos++;
6183 
6184 	if (os_strstr(pos, " join") != NULL)
6185 		use = WPAS_P2P_PD_FOR_JOIN;
6186 	else if (os_strstr(pos, " auto") != NULL)
6187 		use = WPAS_P2P_PD_AUTO;
6188 
6189 	return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
6190 }
6191 
6192 
p2p_get_passphrase(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)6193 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
6194 			      size_t buflen)
6195 {
6196 	struct wpa_ssid *ssid = wpa_s->current_ssid;
6197 
6198 	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
6199 	    ssid->passphrase == NULL)
6200 		return -1;
6201 
6202 	os_strlcpy(buf, ssid->passphrase, buflen);
6203 	return os_strlen(buf);
6204 }
6205 
6206 
p2p_ctrl_serv_disc_req(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)6207 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
6208 				  char *buf, size_t buflen)
6209 {
6210 	u64 ref;
6211 	int res;
6212 	u8 dst_buf[ETH_ALEN], *dst;
6213 	struct wpabuf *tlvs;
6214 	char *pos;
6215 	size_t len;
6216 
6217 	if (hwaddr_aton(cmd, dst_buf))
6218 		return -1;
6219 	dst = dst_buf;
6220 	if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
6221 	    dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
6222 		dst = NULL;
6223 	pos = cmd + 17;
6224 	if (*pos != ' ')
6225 		return -1;
6226 	pos++;
6227 
6228 	if (os_strncmp(pos, "upnp ", 5) == 0) {
6229 		u8 version;
6230 		pos += 5;
6231 		if (hexstr2bin(pos, &version, 1) < 0)
6232 			return -1;
6233 		pos += 2;
6234 		if (*pos != ' ')
6235 			return -1;
6236 		pos++;
6237 		ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
6238 #ifdef CONFIG_WIFI_DISPLAY
6239 	} else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
6240 		ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
6241 #endif /* CONFIG_WIFI_DISPLAY */
6242 	} else if (os_strncmp(pos, "asp ", 4) == 0) {
6243 		char *svc_str;
6244 		char *svc_info = NULL;
6245 		u32 id;
6246 
6247 		pos += 4;
6248 		if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
6249 			return -1;
6250 
6251 		pos = os_strchr(pos, ' ');
6252 		if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
6253 			return -1;
6254 
6255 		svc_str = pos + 1;
6256 
6257 		pos = os_strchr(svc_str, ' ');
6258 
6259 		if (pos)
6260 			*pos++ = '\0';
6261 
6262 		/* All remaining data is the svc_info string */
6263 		if (pos && pos[0] && pos[0] != ' ') {
6264 			len = os_strlen(pos);
6265 
6266 			/* Unescape in place */
6267 			len = utf8_unescape(pos, len, pos, len);
6268 			if (len > 0xff)
6269 				return -1;
6270 
6271 			svc_info = pos;
6272 		}
6273 
6274 		ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
6275 					      svc_str, svc_info);
6276 	} else {
6277 		len = os_strlen(pos);
6278 		if (len & 1)
6279 			return -1;
6280 		len /= 2;
6281 		tlvs = wpabuf_alloc(len);
6282 		if (tlvs == NULL)
6283 			return -1;
6284 		if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
6285 			wpabuf_free(tlvs);
6286 			return -1;
6287 		}
6288 
6289 		ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
6290 		wpabuf_free(tlvs);
6291 	}
6292 	if (ref == 0)
6293 		return -1;
6294 	res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
6295 	if (os_snprintf_error(buflen, res))
6296 		return -1;
6297 	return res;
6298 }
6299 
6300 
p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant * wpa_s,char * cmd)6301 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
6302 					 char *cmd)
6303 {
6304 	long long unsigned val;
6305 	u64 req;
6306 	if (sscanf(cmd, "%llx", &val) != 1)
6307 		return -1;
6308 	req = val;
6309 	return wpas_p2p_sd_cancel_request(wpa_s, req);
6310 }
6311 
6312 
p2p_ctrl_serv_disc_resp(struct wpa_supplicant * wpa_s,char * cmd)6313 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
6314 {
6315 	int freq;
6316 	u8 dst[ETH_ALEN];
6317 	u8 dialog_token;
6318 	struct wpabuf *resp_tlvs;
6319 	char *pos, *pos2;
6320 	size_t len;
6321 
6322 	pos = os_strchr(cmd, ' ');
6323 	if (pos == NULL)
6324 		return -1;
6325 	*pos++ = '\0';
6326 	freq = atoi(cmd);
6327 	if (freq == 0)
6328 		return -1;
6329 
6330 	if (hwaddr_aton(pos, dst))
6331 		return -1;
6332 	pos += 17;
6333 	if (*pos != ' ')
6334 		return -1;
6335 	pos++;
6336 
6337 	pos2 = os_strchr(pos, ' ');
6338 	if (pos2 == NULL)
6339 		return -1;
6340 	*pos2++ = '\0';
6341 	dialog_token = atoi(pos);
6342 
6343 	len = os_strlen(pos2);
6344 	if (len & 1)
6345 		return -1;
6346 	len /= 2;
6347 	resp_tlvs = wpabuf_alloc(len);
6348 	if (resp_tlvs == NULL)
6349 		return -1;
6350 	if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
6351 		wpabuf_free(resp_tlvs);
6352 		return -1;
6353 	}
6354 
6355 	wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
6356 	wpabuf_free(resp_tlvs);
6357 	return 0;
6358 }
6359 
6360 
p2p_ctrl_serv_disc_external(struct wpa_supplicant * wpa_s,char * cmd)6361 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
6362 				       char *cmd)
6363 {
6364 	if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
6365 		return -1;
6366 	wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
6367 	return 0;
6368 }
6369 
6370 
p2p_ctrl_service_add_bonjour(struct wpa_supplicant * wpa_s,char * cmd)6371 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
6372 					char *cmd)
6373 {
6374 	char *pos;
6375 	size_t len;
6376 	struct wpabuf *query, *resp;
6377 
6378 	pos = os_strchr(cmd, ' ');
6379 	if (pos == NULL)
6380 		return -1;
6381 	*pos++ = '\0';
6382 
6383 	len = os_strlen(cmd);
6384 	if (len & 1)
6385 		return -1;
6386 	len /= 2;
6387 	query = wpabuf_alloc(len);
6388 	if (query == NULL)
6389 		return -1;
6390 	if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
6391 		wpabuf_free(query);
6392 		return -1;
6393 	}
6394 
6395 	len = os_strlen(pos);
6396 	if (len & 1) {
6397 		wpabuf_free(query);
6398 		return -1;
6399 	}
6400 	len /= 2;
6401 	resp = wpabuf_alloc(len);
6402 	if (resp == NULL) {
6403 		wpabuf_free(query);
6404 		return -1;
6405 	}
6406 	if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
6407 		wpabuf_free(query);
6408 		wpabuf_free(resp);
6409 		return -1;
6410 	}
6411 
6412 	if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
6413 		wpabuf_free(query);
6414 		wpabuf_free(resp);
6415 		return -1;
6416 	}
6417 	return 0;
6418 }
6419 
6420 
p2p_ctrl_service_add_upnp(struct wpa_supplicant * wpa_s,char * cmd)6421 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
6422 {
6423 	char *pos;
6424 	u8 version;
6425 
6426 	pos = os_strchr(cmd, ' ');
6427 	if (pos == NULL)
6428 		return -1;
6429 	*pos++ = '\0';
6430 
6431 	if (hexstr2bin(cmd, &version, 1) < 0)
6432 		return -1;
6433 
6434 	return wpas_p2p_service_add_upnp(wpa_s, version, pos);
6435 }
6436 
6437 
p2p_ctrl_service_add_asp(struct wpa_supplicant * wpa_s,u8 replace,char * cmd)6438 static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
6439 				    u8 replace, char *cmd)
6440 {
6441 	char *pos;
6442 	char *adv_str;
6443 	u32 auto_accept, adv_id, svc_state, config_methods;
6444 	char *svc_info = NULL;
6445 	char *cpt_prio_str;
6446 	u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
6447 
6448 	pos = os_strchr(cmd, ' ');
6449 	if (pos == NULL)
6450 		return -1;
6451 	*pos++ = '\0';
6452 
6453 	/* Auto-Accept value is mandatory, and must be one of the
6454 	 * single values (0, 1, 2, 4) */
6455 	auto_accept = atoi(cmd);
6456 	switch (auto_accept) {
6457 	case P2PS_SETUP_NONE: /* No auto-accept */
6458 	case P2PS_SETUP_NEW:
6459 	case P2PS_SETUP_CLIENT:
6460 	case P2PS_SETUP_GROUP_OWNER:
6461 		break;
6462 	default:
6463 		return -1;
6464 	}
6465 
6466 	/* Advertisement ID is mandatory */
6467 	cmd = pos;
6468 	pos = os_strchr(cmd, ' ');
6469 	if (pos == NULL)
6470 		return -1;
6471 	*pos++ = '\0';
6472 
6473 	/* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
6474 	if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
6475 		return -1;
6476 
6477 	/* Only allow replacements if exist, and adds if not */
6478 	if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
6479 		if (!replace)
6480 			return -1;
6481 	} else {
6482 		if (replace)
6483 			return -1;
6484 	}
6485 
6486 	/* svc_state between 0 - 0xff is mandatory */
6487 	if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
6488 		return -1;
6489 
6490 	pos = os_strchr(pos, ' ');
6491 	if (pos == NULL)
6492 		return -1;
6493 
6494 	/* config_methods is mandatory */
6495 	pos++;
6496 	if (sscanf(pos, "%x", &config_methods) != 1)
6497 		return -1;
6498 
6499 	if (!(config_methods &
6500 	      (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
6501 		return -1;
6502 
6503 	pos = os_strchr(pos, ' ');
6504 	if (pos == NULL)
6505 		return -1;
6506 
6507 	pos++;
6508 	adv_str = pos;
6509 
6510 	/* Advertisement string is mandatory */
6511 	if (!pos[0] || pos[0] == ' ')
6512 		return -1;
6513 
6514 	/* Terminate svc string */
6515 	pos = os_strchr(pos, ' ');
6516 	if (pos != NULL)
6517 		*pos++ = '\0';
6518 
6519 	cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
6520 	if (cpt_prio_str) {
6521 		pos = os_strchr(pos, ' ');
6522 		if (pos != NULL)
6523 			*pos++ = '\0';
6524 
6525 		if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
6526 			return -1;
6527 	} else {
6528 		cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
6529 		cpt_prio[1] = 0;
6530 	}
6531 
6532 	/* Service and Response Information are optional */
6533 	if (pos && pos[0]) {
6534 		size_t len;
6535 
6536 		/* Note the bare ' included, which cannot exist legally
6537 		 * in unescaped string. */
6538 		svc_info = os_strstr(pos, "svc_info='");
6539 
6540 		if (svc_info) {
6541 			svc_info += 9;
6542 			len = os_strlen(svc_info);
6543 			utf8_unescape(svc_info, len, svc_info, len);
6544 		}
6545 	}
6546 
6547 	return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
6548 					(u8) svc_state, (u16) config_methods,
6549 					svc_info, cpt_prio);
6550 }
6551 
6552 
p2p_ctrl_service_add(struct wpa_supplicant * wpa_s,char * cmd)6553 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
6554 {
6555 	char *pos;
6556 
6557 	pos = os_strchr(cmd, ' ');
6558 	if (pos == NULL)
6559 		return -1;
6560 	*pos++ = '\0';
6561 
6562 	if (os_strcmp(cmd, "bonjour") == 0)
6563 		return p2p_ctrl_service_add_bonjour(wpa_s, pos);
6564 	if (os_strcmp(cmd, "upnp") == 0)
6565 		return p2p_ctrl_service_add_upnp(wpa_s, pos);
6566 	if (os_strcmp(cmd, "asp") == 0)
6567 		return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
6568 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6569 	return -1;
6570 }
6571 
6572 
p2p_ctrl_service_del_bonjour(struct wpa_supplicant * wpa_s,char * cmd)6573 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
6574 					char *cmd)
6575 {
6576 	size_t len;
6577 	struct wpabuf *query;
6578 	int ret;
6579 
6580 	len = os_strlen(cmd);
6581 	if (len & 1)
6582 		return -1;
6583 	len /= 2;
6584 	query = wpabuf_alloc(len);
6585 	if (query == NULL)
6586 		return -1;
6587 	if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
6588 		wpabuf_free(query);
6589 		return -1;
6590 	}
6591 
6592 	ret = wpas_p2p_service_del_bonjour(wpa_s, query);
6593 	wpabuf_free(query);
6594 	return ret;
6595 }
6596 
6597 
p2p_ctrl_service_del_upnp(struct wpa_supplicant * wpa_s,char * cmd)6598 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
6599 {
6600 	char *pos;
6601 	u8 version;
6602 
6603 	pos = os_strchr(cmd, ' ');
6604 	if (pos == NULL)
6605 		return -1;
6606 	*pos++ = '\0';
6607 
6608 	if (hexstr2bin(cmd, &version, 1) < 0)
6609 		return -1;
6610 
6611 	return wpas_p2p_service_del_upnp(wpa_s, version, pos);
6612 }
6613 
6614 
p2p_ctrl_service_del_asp(struct wpa_supplicant * wpa_s,char * cmd)6615 static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
6616 {
6617 	u32 adv_id;
6618 
6619 	if (os_strcmp(cmd, "all") == 0) {
6620 		wpas_p2p_service_flush_asp(wpa_s);
6621 		return 0;
6622 	}
6623 
6624 	if (sscanf(cmd, "%x", &adv_id) != 1)
6625 		return -1;
6626 
6627 	return wpas_p2p_service_del_asp(wpa_s, adv_id);
6628 }
6629 
6630 
p2p_ctrl_service_del(struct wpa_supplicant * wpa_s,char * cmd)6631 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
6632 {
6633 	char *pos;
6634 
6635 	pos = os_strchr(cmd, ' ');
6636 	if (pos == NULL)
6637 		return -1;
6638 	*pos++ = '\0';
6639 
6640 	if (os_strcmp(cmd, "bonjour") == 0)
6641 		return p2p_ctrl_service_del_bonjour(wpa_s, pos);
6642 	if (os_strcmp(cmd, "upnp") == 0)
6643 		return p2p_ctrl_service_del_upnp(wpa_s, pos);
6644 	if (os_strcmp(cmd, "asp") == 0)
6645 		return p2p_ctrl_service_del_asp(wpa_s, pos);
6646 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6647 	return -1;
6648 }
6649 
6650 
p2p_ctrl_service_replace(struct wpa_supplicant * wpa_s,char * cmd)6651 static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
6652 {
6653 	char *pos;
6654 
6655 	pos = os_strchr(cmd, ' ');
6656 	if (pos == NULL)
6657 		return -1;
6658 	*pos++ = '\0';
6659 
6660 	if (os_strcmp(cmd, "asp") == 0)
6661 		return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
6662 
6663 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6664 	return -1;
6665 }
6666 
6667 
p2p_ctrl_reject(struct wpa_supplicant * wpa_s,char * cmd)6668 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
6669 {
6670 	u8 addr[ETH_ALEN];
6671 
6672 	/* <addr> */
6673 
6674 	if (hwaddr_aton(cmd, addr))
6675 		return -1;
6676 
6677 	return wpas_p2p_reject(wpa_s, addr);
6678 }
6679 
6680 
p2p_ctrl_invite_persistent(struct wpa_supplicant * wpa_s,char * cmd)6681 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
6682 {
6683 	char *pos;
6684 	int id;
6685 	struct wpa_ssid *ssid;
6686 	u8 *_peer = NULL, peer[ETH_ALEN];
6687 	int freq = 0, pref_freq = 0;
6688 	int ht40, vht, he, max_oper_chwidth, chwidth = 0, freq2 = 0;
6689 	int edmg;
6690 	bool allow_6ghz;
6691 
6692 	id = atoi(cmd);
6693 	pos = os_strstr(cmd, " peer=");
6694 	if (pos) {
6695 		pos += 6;
6696 		if (hwaddr_aton(pos, peer))
6697 			return -1;
6698 		_peer = peer;
6699 	}
6700 	ssid = wpa_config_get_network(wpa_s->conf, id);
6701 	if (ssid == NULL || ssid->disabled != 2) {
6702 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
6703 			   "for persistent P2P group",
6704 			   id);
6705 		return -1;
6706 	}
6707 
6708 	pos = os_strstr(cmd, " freq=");
6709 	if (pos) {
6710 		pos += 6;
6711 		freq = atoi(pos);
6712 		if (freq <= 0)
6713 			return -1;
6714 	}
6715 
6716 	pos = os_strstr(cmd, " pref=");
6717 	if (pos) {
6718 		pos += 6;
6719 		pref_freq = atoi(pos);
6720 		if (pref_freq <= 0)
6721 			return -1;
6722 	}
6723 
6724 	vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
6725 	ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
6726 		vht;
6727 	he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
6728 	edmg = (os_strstr(cmd, " edmg") != NULL) || wpa_s->conf->p2p_go_edmg;
6729 
6730 	pos = os_strstr(cmd, "freq2=");
6731 	if (pos)
6732 		freq2 = atoi(pos + 6);
6733 
6734 	pos = os_strstr(cmd, " max_oper_chwidth=");
6735 	if (pos)
6736 		chwidth = atoi(pos + 18);
6737 
6738 	max_oper_chwidth = parse_freq(chwidth, freq2);
6739 	if (max_oper_chwidth < 0)
6740 		return -1;
6741 
6742 	allow_6ghz = os_strstr(cmd, " allow_6ghz") != NULL;
6743 
6744 	if (allow_6ghz && chwidth == 40)
6745 		max_oper_chwidth = CHANWIDTH_40MHZ_6GHZ;
6746 
6747 	return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
6748 			       max_oper_chwidth, pref_freq, he, edmg,
6749 			       allow_6ghz);
6750 }
6751 
6752 
p2p_ctrl_invite_group(struct wpa_supplicant * wpa_s,char * cmd)6753 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
6754 {
6755 	char *pos;
6756 	u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
6757 	bool allow_6ghz;
6758 
6759 	pos = os_strstr(cmd, " peer=");
6760 	if (!pos)
6761 		return -1;
6762 
6763 	*pos = '\0';
6764 	pos += 6;
6765 	if (hwaddr_aton(pos, peer)) {
6766 		wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
6767 		return -1;
6768 	}
6769 
6770 	allow_6ghz = os_strstr(pos, " allow_6ghz") != NULL;
6771 
6772 	pos = os_strstr(pos, " go_dev_addr=");
6773 	if (pos) {
6774 		pos += 13;
6775 		if (hwaddr_aton(pos, go_dev_addr)) {
6776 			wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
6777 				   pos);
6778 			return -1;
6779 		}
6780 		go_dev = go_dev_addr;
6781 	}
6782 
6783 	return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev, allow_6ghz);
6784 }
6785 
6786 
p2p_ctrl_invite(struct wpa_supplicant * wpa_s,char * cmd)6787 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
6788 {
6789 	if (os_strncmp(cmd, "persistent=", 11) == 0)
6790 		return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
6791 	if (os_strncmp(cmd, "group=", 6) == 0)
6792 		return p2p_ctrl_invite_group(wpa_s, cmd + 6);
6793 
6794 	return -1;
6795 }
6796 
6797 
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)6798 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
6799 					 int id, int freq, int vht_center_freq2,
6800 					 int ht40, int vht, int vht_chwidth,
6801 					 int he, int edmg, bool allow_6ghz)
6802 {
6803 	struct wpa_ssid *ssid;
6804 
6805 	ssid = wpa_config_get_network(wpa_s->conf, id);
6806 	if (ssid == NULL || ssid->disabled != 2) {
6807 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
6808 			   "for persistent P2P group",
6809 			   id);
6810 		return -1;
6811 	}
6812 
6813 	return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq,
6814 					     vht_center_freq2, 0, ht40, vht,
6815 					     vht_chwidth, he, edmg,
6816 					     NULL, 0, 0, allow_6ghz);
6817 }
6818 
6819 
p2p_ctrl_group_add(struct wpa_supplicant * wpa_s,char * cmd)6820 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
6821 {
6822 	int freq = 0, persistent = 0, group_id = -1;
6823 	bool allow_6ghz = false;
6824 	int vht = wpa_s->conf->p2p_go_vht;
6825 	int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
6826 	int he = wpa_s->conf->p2p_go_he;
6827 	int edmg = wpa_s->conf->p2p_go_edmg;
6828 	int max_oper_chwidth, chwidth = 0, freq2 = 0;
6829 	char *token, *context = NULL;
6830 #ifdef CONFIG_ACS
6831 	int acs = 0;
6832 #endif /* CONFIG_ACS */
6833 
6834 	while ((token = str_token(cmd, " ", &context))) {
6835 		if (sscanf(token, "freq2=%d", &freq2) == 1 ||
6836 		    sscanf(token, "persistent=%d", &group_id) == 1 ||
6837 		    sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1) {
6838 			continue;
6839 #ifdef CONFIG_ACS
6840 		} else if (os_strcmp(token, "freq=acs") == 0) {
6841 			acs = 1;
6842 #endif /* CONFIG_ACS */
6843 		} else if (sscanf(token, "freq=%d", &freq) == 1) {
6844 			continue;
6845 		} else if (os_strcmp(token, "ht40") == 0) {
6846 			ht40 = 1;
6847 		} else if (os_strcmp(token, "vht") == 0) {
6848 			vht = 1;
6849 			ht40 = 1;
6850 		} else if (os_strcmp(token, "he") == 0) {
6851 			he = 1;
6852 		} else if (os_strcmp(token, "edmg") == 0) {
6853 			edmg = 1;
6854 		} else if (os_strcmp(token, "persistent") == 0) {
6855 			persistent = 1;
6856 		} else if (os_strcmp(token, "allow_6ghz") == 0) {
6857 			allow_6ghz = true;
6858 		} else {
6859 			wpa_printf(MSG_DEBUG,
6860 				   "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
6861 				   token);
6862 			return -1;
6863 		}
6864 	}
6865 
6866 #ifdef CONFIG_ACS
6867 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
6868 	    (acs || freq == 2 || freq == 5)) {
6869 		if (freq == 2 && wpa_s->best_24_freq <= 0) {
6870 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211G;
6871 			wpa_s->p2p_go_do_acs = 1;
6872 			freq = 0;
6873 		} else if (freq == 5 && wpa_s->best_5_freq <= 0) {
6874 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211A;
6875 			wpa_s->p2p_go_do_acs = 1;
6876 			freq = 0;
6877 		} else {
6878 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211ANY;
6879 			wpa_s->p2p_go_do_acs = 1;
6880 		}
6881 	} else {
6882 		wpa_s->p2p_go_do_acs = 0;
6883 	}
6884 #endif /* CONFIG_ACS */
6885 
6886 	max_oper_chwidth = parse_freq(chwidth, freq2);
6887 	if (max_oper_chwidth < 0)
6888 		return -1;
6889 
6890 	if (allow_6ghz && chwidth == 40)
6891 		max_oper_chwidth = CHANWIDTH_40MHZ_6GHZ;
6892 
6893 	/* Allow DFS to be used for Autonomous GO */
6894 	wpa_s->p2p_go_allow_dfs = !!(wpa_s->drv_flags &
6895 				     WPA_DRIVER_FLAGS_DFS_OFFLOAD);
6896 
6897 	if (group_id >= 0)
6898 		return p2p_ctrl_group_add_persistent(wpa_s, group_id,
6899 						     freq, freq2, ht40, vht,
6900 						     max_oper_chwidth, he,
6901 						     edmg, allow_6ghz);
6902 
6903 	return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
6904 				  max_oper_chwidth, he, edmg, allow_6ghz);
6905 }
6906 
6907 
p2p_ctrl_group_member(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)6908 static int p2p_ctrl_group_member(struct wpa_supplicant *wpa_s, const char *cmd,
6909 				 char *buf, size_t buflen)
6910 {
6911 	u8 dev_addr[ETH_ALEN];
6912 	struct wpa_ssid *ssid;
6913 	int res;
6914 	const u8 *iaddr;
6915 
6916 	ssid = wpa_s->current_ssid;
6917 	if (!wpa_s->global->p2p || !ssid || ssid->mode != WPAS_MODE_P2P_GO ||
6918 	    hwaddr_aton(cmd, dev_addr))
6919 		return -1;
6920 
6921 	iaddr = p2p_group_get_client_interface_addr(wpa_s->p2p_group, dev_addr);
6922 	if (!iaddr)
6923 		return -1;
6924 	res = os_snprintf(buf, buflen, MACSTR, MAC2STR(iaddr));
6925 	if (os_snprintf_error(buflen, res))
6926 		return -1;
6927 	return res;
6928 }
6929 
6930 
wpas_find_p2p_dev_addr_bss(struct wpa_global * global,const u8 * p2p_dev_addr)6931 static int wpas_find_p2p_dev_addr_bss(struct wpa_global *global,
6932 				      const u8 *p2p_dev_addr)
6933 {
6934 	struct wpa_supplicant *wpa_s;
6935 
6936 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6937 		if (wpa_bss_get_p2p_dev_addr(wpa_s, p2p_dev_addr))
6938 			return 1;
6939 	}
6940 
6941 	return 0;
6942 }
6943 
6944 
p2p_ctrl_peer(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)6945 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
6946 			 char *buf, size_t buflen)
6947 {
6948 	u8 addr[ETH_ALEN], *addr_ptr, group_capab;
6949 	int next, res;
6950 	const struct p2p_peer_info *info;
6951 	char *pos, *end;
6952 	char devtype[WPS_DEV_TYPE_BUFSIZE];
6953 	struct wpa_ssid *ssid;
6954 	size_t i;
6955 
6956 	if (!wpa_s->global->p2p)
6957 		return -1;
6958 
6959 	if (os_strcmp(cmd, "FIRST") == 0) {
6960 		addr_ptr = NULL;
6961 		next = 0;
6962 	} else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
6963 		if (hwaddr_aton(cmd + 5, addr) < 0)
6964 			return -1;
6965 		addr_ptr = addr;
6966 		next = 1;
6967 	} else {
6968 		if (hwaddr_aton(cmd, addr) < 0)
6969 			return -1;
6970 		addr_ptr = addr;
6971 		next = 0;
6972 	}
6973 
6974 	info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
6975 	if (info == NULL)
6976 		return -1;
6977 	group_capab = info->group_capab;
6978 
6979 	if (group_capab &&
6980 	    !wpas_find_p2p_dev_addr_bss(wpa_s->global, info->p2p_device_addr)) {
6981 		wpa_printf(MSG_DEBUG,
6982 			   "P2P: Could not find any BSS with p2p_dev_addr "
6983 			   MACSTR ", hence override group_capab from 0x%x to 0",
6984 			   MAC2STR(info->p2p_device_addr), group_capab);
6985 		group_capab = 0;
6986 	}
6987 
6988 	pos = buf;
6989 	end = buf + buflen;
6990 
6991 	res = os_snprintf(pos, end - pos, MACSTR "\n"
6992 			  "pri_dev_type=%s\n"
6993 			  "device_name=%s\n"
6994 			  "manufacturer=%s\n"
6995 			  "model_name=%s\n"
6996 			  "model_number=%s\n"
6997 			  "serial_number=%s\n"
6998 			  "config_methods=0x%x\n"
6999 			  "dev_capab=0x%x\n"
7000 			  "group_capab=0x%x\n"
7001 			  "level=%d\n",
7002 			  MAC2STR(info->p2p_device_addr),
7003 			  wps_dev_type_bin2str(info->pri_dev_type,
7004 					       devtype, sizeof(devtype)),
7005 			  info->device_name,
7006 			  info->manufacturer,
7007 			  info->model_name,
7008 			  info->model_number,
7009 			  info->serial_number,
7010 			  info->config_methods,
7011 			  info->dev_capab,
7012 			  group_capab,
7013 			  info->level);
7014 	if (os_snprintf_error(end - pos, res))
7015 		return pos - buf;
7016 	pos += res;
7017 
7018 	for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
7019 	{
7020 		const u8 *t;
7021 		t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
7022 		res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
7023 				  wps_dev_type_bin2str(t, devtype,
7024 						       sizeof(devtype)));
7025 		if (os_snprintf_error(end - pos, res))
7026 			return pos - buf;
7027 		pos += res;
7028 	}
7029 
7030 	ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
7031 	if (ssid) {
7032 		res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
7033 		if (os_snprintf_error(end - pos, res))
7034 			return pos - buf;
7035 		pos += res;
7036 	}
7037 
7038 	res = p2p_get_peer_info_txt(info, pos, end - pos);
7039 	if (res < 0)
7040 		return pos - buf;
7041 	pos += res;
7042 
7043 	if (info->vendor_elems) {
7044 		res = os_snprintf(pos, end - pos, "vendor_elems=");
7045 		if (os_snprintf_error(end - pos, res))
7046 			return pos - buf;
7047 		pos += res;
7048 
7049 		pos += wpa_snprintf_hex(pos, end - pos,
7050 					wpabuf_head(info->vendor_elems),
7051 					wpabuf_len(info->vendor_elems));
7052 
7053 		res = os_snprintf(pos, end - pos, "\n");
7054 		if (os_snprintf_error(end - pos, res))
7055 			return pos - buf;
7056 		pos += res;
7057 	}
7058 
7059 	return pos - buf;
7060 }
7061 
7062 
p2p_ctrl_disallow_freq(struct wpa_supplicant * wpa_s,const char * param)7063 static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
7064 				  const char *param)
7065 {
7066 	unsigned int i;
7067 
7068 	if (wpa_s->global->p2p == NULL)
7069 		return -1;
7070 
7071 	if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
7072 		return -1;
7073 
7074 	for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
7075 		struct wpa_freq_range *freq;
7076 		freq = &wpa_s->global->p2p_disallow_freq.range[i];
7077 		wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
7078 			   freq->min, freq->max);
7079 	}
7080 
7081 	wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
7082 	return 0;
7083 }
7084 
7085 
p2p_ctrl_set(struct wpa_supplicant * wpa_s,char * cmd)7086 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
7087 {
7088 	char *param;
7089 
7090 	if (wpa_s->global->p2p == NULL)
7091 		return -1;
7092 
7093 	param = os_strchr(cmd, ' ');
7094 	if (param == NULL)
7095 		return -1;
7096 	*param++ = '\0';
7097 
7098 	if (os_strcmp(cmd, "discoverability") == 0) {
7099 		p2p_set_client_discoverability(wpa_s->global->p2p,
7100 					       atoi(param));
7101 		return 0;
7102 	}
7103 
7104 	if (os_strcmp(cmd, "managed") == 0) {
7105 		p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
7106 		return 0;
7107 	}
7108 
7109 	if (os_strcmp(cmd, "listen_channel") == 0) {
7110 		char *pos;
7111 		u8 channel, op_class;
7112 
7113 		channel = atoi(param);
7114 		pos = os_strchr(param, ' ');
7115 		op_class = pos ? atoi(pos) : 81;
7116 
7117 		return p2p_set_listen_channel(wpa_s->global->p2p, op_class,
7118 					      channel, 1);
7119 	}
7120 
7121 	if (os_strcmp(cmd, "ssid_postfix") == 0) {
7122 		return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
7123 					    os_strlen(param));
7124 	}
7125 
7126 	if (os_strcmp(cmd, "noa") == 0) {
7127 		char *pos;
7128 		int count, start, duration;
7129 		/* GO NoA parameters: count,start_offset(ms),duration(ms) */
7130 		count = atoi(param);
7131 		pos = os_strchr(param, ',');
7132 		if (pos == NULL)
7133 			return -1;
7134 		pos++;
7135 		start = atoi(pos);
7136 		pos = os_strchr(pos, ',');
7137 		if (pos == NULL)
7138 			return -1;
7139 		pos++;
7140 		duration = atoi(pos);
7141 		if (count < 0 || count > 255 || start < 0 || duration < 0)
7142 			return -1;
7143 		if (count == 0 && duration > 0)
7144 			return -1;
7145 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
7146 			   "start=%d duration=%d", count, start, duration);
7147 		return wpas_p2p_set_noa(wpa_s, count, start, duration);
7148 	}
7149 
7150 	if (os_strcmp(cmd, "ps") == 0)
7151 		return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
7152 
7153 	if (os_strcmp(cmd, "oppps") == 0)
7154 		return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
7155 
7156 	if (os_strcmp(cmd, "ctwindow") == 0)
7157 		return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
7158 
7159 	if (os_strcmp(cmd, "disabled") == 0) {
7160 		wpa_s->global->p2p_disabled = atoi(param);
7161 		wpa_printf(MSG_DEBUG, "P2P functionality %s",
7162 			   wpa_s->global->p2p_disabled ?
7163 			   "disabled" : "enabled");
7164 		if (wpa_s->global->p2p_disabled) {
7165 			wpas_p2p_stop_find(wpa_s);
7166 			os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7167 			p2p_flush(wpa_s->global->p2p);
7168 		}
7169 		return 0;
7170 	}
7171 
7172 	if (os_strcmp(cmd, "conc_pref") == 0) {
7173 		if (os_strcmp(param, "sta") == 0)
7174 			wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
7175 		else if (os_strcmp(param, "p2p") == 0)
7176 			wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
7177 		else {
7178 			wpa_printf(MSG_INFO, "Invalid conc_pref value");
7179 			return -1;
7180 		}
7181 		wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
7182 			   "%s", param);
7183 		return 0;
7184 	}
7185 
7186 	if (os_strcmp(cmd, "force_long_sd") == 0) {
7187 		wpa_s->force_long_sd = atoi(param);
7188 		return 0;
7189 	}
7190 
7191 	if (os_strcmp(cmd, "peer_filter") == 0) {
7192 		u8 addr[ETH_ALEN];
7193 		if (hwaddr_aton(param, addr))
7194 			return -1;
7195 		p2p_set_peer_filter(wpa_s->global->p2p, addr);
7196 		return 0;
7197 	}
7198 
7199 	if (os_strcmp(cmd, "cross_connect") == 0)
7200 		return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
7201 
7202 	if (os_strcmp(cmd, "go_apsd") == 0) {
7203 		if (os_strcmp(param, "disable") == 0)
7204 			wpa_s->set_ap_uapsd = 0;
7205 		else {
7206 			wpa_s->set_ap_uapsd = 1;
7207 			wpa_s->ap_uapsd = atoi(param);
7208 		}
7209 		return 0;
7210 	}
7211 
7212 	if (os_strcmp(cmd, "client_apsd") == 0) {
7213 		if (os_strcmp(param, "disable") == 0)
7214 			wpa_s->set_sta_uapsd = 0;
7215 		else {
7216 			int be, bk, vi, vo;
7217 			char *pos;
7218 			/* format: BE,BK,VI,VO;max SP Length */
7219 			be = atoi(param);
7220 			pos = os_strchr(param, ',');
7221 			if (pos == NULL)
7222 				return -1;
7223 			pos++;
7224 			bk = atoi(pos);
7225 			pos = os_strchr(pos, ',');
7226 			if (pos == NULL)
7227 				return -1;
7228 			pos++;
7229 			vi = atoi(pos);
7230 			pos = os_strchr(pos, ',');
7231 			if (pos == NULL)
7232 				return -1;
7233 			pos++;
7234 			vo = atoi(pos);
7235 			/* ignore max SP Length for now */
7236 
7237 			wpa_s->set_sta_uapsd = 1;
7238 			wpa_s->sta_uapsd = 0;
7239 			if (be)
7240 				wpa_s->sta_uapsd |= BIT(0);
7241 			if (bk)
7242 				wpa_s->sta_uapsd |= BIT(1);
7243 			if (vi)
7244 				wpa_s->sta_uapsd |= BIT(2);
7245 			if (vo)
7246 				wpa_s->sta_uapsd |= BIT(3);
7247 		}
7248 		return 0;
7249 	}
7250 
7251 	if (os_strcmp(cmd, "disallow_freq") == 0)
7252 		return p2p_ctrl_disallow_freq(wpa_s, param);
7253 
7254 	if (os_strcmp(cmd, "disc_int") == 0) {
7255 		int min_disc_int, max_disc_int, max_disc_tu;
7256 		char *pos;
7257 
7258 		pos = param;
7259 
7260 		min_disc_int = atoi(pos);
7261 		pos = os_strchr(pos, ' ');
7262 		if (pos == NULL)
7263 			return -1;
7264 		*pos++ = '\0';
7265 
7266 		max_disc_int = atoi(pos);
7267 		pos = os_strchr(pos, ' ');
7268 		if (pos == NULL)
7269 			return -1;
7270 		*pos++ = '\0';
7271 
7272 		max_disc_tu = atoi(pos);
7273 
7274 		return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
7275 					max_disc_int, max_disc_tu);
7276 	}
7277 
7278 	if (os_strcmp(cmd, "per_sta_psk") == 0) {
7279 		wpa_s->global->p2p_per_sta_psk = !!atoi(param);
7280 		return 0;
7281 	}
7282 
7283 #ifdef CONFIG_WPS_NFC
7284 	if (os_strcmp(cmd, "nfc_tag") == 0)
7285 		return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
7286 #endif /* CONFIG_WPS_NFC */
7287 
7288 	if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
7289 		wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
7290 		return 0;
7291 	}
7292 
7293 	if (os_strcmp(cmd, "override_pref_op_chan") == 0) {
7294 		int op_class, chan;
7295 
7296 		op_class = atoi(param);
7297 		param = os_strchr(param, ':');
7298 		if (!param)
7299 			return -1;
7300 		param++;
7301 		chan = atoi(param);
7302 		p2p_set_override_pref_op_chan(wpa_s->global->p2p, op_class,
7303 					      chan);
7304 		return 0;
7305 	}
7306 
7307 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
7308 		   cmd);
7309 
7310 	return -1;
7311 }
7312 
7313 
p2p_ctrl_flush(struct wpa_supplicant * wpa_s)7314 static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
7315 {
7316 	os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7317 	wpa_s->force_long_sd = 0;
7318 
7319 #ifdef CONFIG_TESTING_OPTIONS
7320 	os_free(wpa_s->get_pref_freq_list_override);
7321 	wpa_s->get_pref_freq_list_override = NULL;
7322 #endif /* CONFIG_TESTING_OPTIONS */
7323 
7324 	wpas_p2p_stop_find(wpa_s);
7325 	wpa_s->parent->p2ps_method_config_any = 0;
7326 	if (wpa_s->global->p2p)
7327 		p2p_flush(wpa_s->global->p2p);
7328 }
7329 
7330 
p2p_ctrl_presence_req(struct wpa_supplicant * wpa_s,char * cmd)7331 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
7332 {
7333 	char *pos, *pos2;
7334 	unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
7335 
7336 	if (cmd[0]) {
7337 		pos = os_strchr(cmd, ' ');
7338 		if (pos == NULL)
7339 			return -1;
7340 		*pos++ = '\0';
7341 		dur1 = atoi(cmd);
7342 
7343 		pos2 = os_strchr(pos, ' ');
7344 		if (pos2)
7345 			*pos2++ = '\0';
7346 		int1 = atoi(pos);
7347 	} else
7348 		pos2 = NULL;
7349 
7350 	if (pos2) {
7351 		pos = os_strchr(pos2, ' ');
7352 		if (pos == NULL)
7353 			return -1;
7354 		*pos++ = '\0';
7355 		dur2 = atoi(pos2);
7356 		int2 = atoi(pos);
7357 	}
7358 
7359 	return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
7360 }
7361 
7362 
p2p_ctrl_ext_listen(struct wpa_supplicant * wpa_s,char * cmd)7363 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
7364 {
7365 	char *pos;
7366 	unsigned int period = 0, interval = 0;
7367 
7368 	if (cmd[0]) {
7369 		pos = os_strchr(cmd, ' ');
7370 		if (pos == NULL)
7371 			return -1;
7372 		*pos++ = '\0';
7373 		period = atoi(cmd);
7374 		interval = atoi(pos);
7375 	}
7376 
7377 	return wpas_p2p_ext_listen(wpa_s, period, interval);
7378 }
7379 
7380 
p2p_ctrl_remove_client(struct wpa_supplicant * wpa_s,const char * cmd)7381 static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
7382 {
7383 	const char *pos;
7384 	u8 peer[ETH_ALEN];
7385 	int iface_addr = 0;
7386 
7387 	pos = cmd;
7388 	if (os_strncmp(pos, "iface=", 6) == 0) {
7389 		iface_addr = 1;
7390 		pos += 6;
7391 	}
7392 	if (hwaddr_aton(pos, peer))
7393 		return -1;
7394 
7395 	wpas_p2p_remove_client(wpa_s, peer, iface_addr);
7396 	return 0;
7397 }
7398 
7399 
p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant * wpa_s,char * cmd)7400 static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
7401 {
7402 	int freq = 0, period = 0, interval = 0, count = 0;
7403 
7404 	if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
7405 	{
7406 		wpa_printf(MSG_DEBUG,
7407 			   "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
7408 		return -1;
7409 	}
7410 
7411 	return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
7412 }
7413 
7414 #endif /* CONFIG_P2P */
7415 
7416 
freq_range_to_channel_list(struct wpa_supplicant * wpa_s,char * val)7417 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
7418 {
7419 	struct wpa_freq_range_list ranges;
7420 	int *freqs = NULL;
7421 	struct hostapd_hw_modes *mode;
7422 	u16 i;
7423 
7424 	if (wpa_s->hw.modes == NULL)
7425 		return NULL;
7426 
7427 	os_memset(&ranges, 0, sizeof(ranges));
7428 	if (freq_range_list_parse(&ranges, val) < 0)
7429 		return NULL;
7430 
7431 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
7432 		int j;
7433 
7434 		mode = &wpa_s->hw.modes[i];
7435 		for (j = 0; j < mode->num_channels; j++) {
7436 			unsigned int freq;
7437 
7438 			if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
7439 				continue;
7440 
7441 			freq = mode->channels[j].freq;
7442 			if (!freq_range_list_includes(&ranges, freq))
7443 				continue;
7444 
7445 			int_array_add_unique(&freqs, freq);
7446 		}
7447 	}
7448 
7449 	os_free(ranges.range);
7450 	return freqs;
7451 }
7452 
7453 
7454 #ifdef CONFIG_INTERWORKING
7455 
ctrl_interworking_select(struct wpa_supplicant * wpa_s,char * param)7456 static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
7457 {
7458 	int auto_sel = 0;
7459 	int *freqs = NULL;
7460 
7461 	if (param) {
7462 		char *pos;
7463 
7464 		auto_sel = os_strstr(param, "auto") != NULL;
7465 
7466 		pos = os_strstr(param, "freq=");
7467 		if (pos) {
7468 			freqs = freq_range_to_channel_list(wpa_s, pos + 5);
7469 			if (freqs == NULL)
7470 				return -1;
7471 		}
7472 
7473 	}
7474 
7475 	return interworking_select(wpa_s, auto_sel, freqs);
7476 }
7477 
7478 
ctrl_interworking_connect(struct wpa_supplicant * wpa_s,char * dst,int only_add)7479 static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
7480 				     int only_add)
7481 {
7482 	u8 bssid[ETH_ALEN];
7483 	struct wpa_bss *bss;
7484 
7485 	if (hwaddr_aton(dst, bssid)) {
7486 		wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
7487 		return -1;
7488 	}
7489 
7490 	bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
7491 	if (bss == NULL) {
7492 		wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
7493 			   MAC2STR(bssid));
7494 		return -1;
7495 	}
7496 
7497 	if (bss->ssid_len == 0) {
7498 		int found = 0;
7499 
7500 		wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
7501 			   " does not have SSID information", MAC2STR(bssid));
7502 
7503 		dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
7504 					 list) {
7505 			if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
7506 			    bss->ssid_len > 0) {
7507 				found = 1;
7508 				break;
7509 			}
7510 		}
7511 
7512 		if (!found)
7513 			return -1;
7514 		wpa_printf(MSG_DEBUG,
7515 			   "Found another matching BSS entry with SSID");
7516 	}
7517 
7518 	return interworking_connect(wpa_s, bss, only_add);
7519 }
7520 
7521 
get_anqp(struct wpa_supplicant * wpa_s,char * dst)7522 static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
7523 {
7524 	u8 dst_addr[ETH_ALEN];
7525 	int used, freq = 0;
7526 	char *pos;
7527 #define MAX_ANQP_INFO_ID 100
7528 	u16 id[MAX_ANQP_INFO_ID];
7529 	size_t num_id = 0;
7530 	u32 subtypes = 0;
7531 	u32 mbo_subtypes = 0;
7532 
7533 	used = hwaddr_aton2(dst, dst_addr);
7534 	if (used < 0)
7535 		return -1;
7536 	pos = dst + used;
7537 	if (*pos == ' ')
7538 		pos++;
7539 
7540 	if (os_strncmp(pos, "freq=", 5) == 0) {
7541 		freq = atoi(pos + 5);
7542 		pos = os_strchr(pos, ' ');
7543 		if (!pos)
7544 			return -1;
7545 		pos++;
7546 	}
7547 
7548 	while (num_id < MAX_ANQP_INFO_ID) {
7549 		if (os_strncmp(pos, "hs20:", 5) == 0) {
7550 #ifdef CONFIG_HS20
7551 			int num = atoi(pos + 5);
7552 			if (num <= 0 || num > 31)
7553 				return -1;
7554 			subtypes |= BIT(num);
7555 #else /* CONFIG_HS20 */
7556 			return -1;
7557 #endif /* CONFIG_HS20 */
7558 		} else if (os_strncmp(pos, "mbo:", 4) == 0) {
7559 #ifdef CONFIG_MBO
7560 			int num = atoi(pos + 4);
7561 
7562 			if (num <= 0 || num > MAX_MBO_ANQP_SUBTYPE)
7563 				return -1;
7564 			mbo_subtypes |= BIT(num);
7565 #else /* CONFIG_MBO */
7566 			return -1;
7567 #endif /* CONFIG_MBO */
7568 		} else {
7569 			id[num_id] = atoi(pos);
7570 			if (id[num_id])
7571 				num_id++;
7572 		}
7573 		pos = os_strchr(pos + 1, ',');
7574 		if (pos == NULL)
7575 			break;
7576 		pos++;
7577 	}
7578 
7579 	if (num_id == 0 && !subtypes && !mbo_subtypes)
7580 		return -1;
7581 
7582 	return anqp_send_req(wpa_s, dst_addr, freq, id, num_id, subtypes,
7583 			     mbo_subtypes);
7584 }
7585 
7586 
gas_request(struct wpa_supplicant * wpa_s,char * cmd)7587 static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
7588 {
7589 	u8 dst_addr[ETH_ALEN];
7590 	struct wpabuf *advproto, *query = NULL;
7591 	int used, ret = -1;
7592 	char *pos, *end;
7593 	size_t len;
7594 
7595 	used = hwaddr_aton2(cmd, dst_addr);
7596 	if (used < 0)
7597 		return -1;
7598 
7599 	pos = cmd + used;
7600 	while (*pos == ' ')
7601 		pos++;
7602 
7603 	/* Advertisement Protocol ID */
7604 	end = os_strchr(pos, ' ');
7605 	if (end)
7606 		len = end - pos;
7607 	else
7608 		len = os_strlen(pos);
7609 	if (len & 0x01)
7610 		return -1;
7611 	len /= 2;
7612 	if (len == 0)
7613 		return -1;
7614 	advproto = wpabuf_alloc(len);
7615 	if (advproto == NULL)
7616 		return -1;
7617 	if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
7618 		goto fail;
7619 
7620 	if (end) {
7621 		/* Optional Query Request */
7622 		pos = end + 1;
7623 		while (*pos == ' ')
7624 			pos++;
7625 
7626 		len = os_strlen(pos);
7627 		if (len) {
7628 			if (len & 0x01)
7629 				goto fail;
7630 			len /= 2;
7631 			if (len == 0)
7632 				goto fail;
7633 			query = wpabuf_alloc(len);
7634 			if (query == NULL)
7635 				goto fail;
7636 			if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
7637 				goto fail;
7638 		}
7639 	}
7640 
7641 	ret = gas_send_request(wpa_s, dst_addr, advproto, query);
7642 
7643 fail:
7644 	wpabuf_free(advproto);
7645 	wpabuf_free(query);
7646 
7647 	return ret;
7648 }
7649 
7650 
gas_response_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)7651 static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
7652 			    size_t buflen)
7653 {
7654 	u8 addr[ETH_ALEN];
7655 	int dialog_token;
7656 	int used;
7657 	char *pos;
7658 	size_t resp_len, start, requested_len;
7659 	struct wpabuf *resp;
7660 	int ret;
7661 
7662 	used = hwaddr_aton2(cmd, addr);
7663 	if (used < 0)
7664 		return -1;
7665 
7666 	pos = cmd + used;
7667 	while (*pos == ' ')
7668 		pos++;
7669 	dialog_token = atoi(pos);
7670 
7671 	if (wpa_s->last_gas_resp &&
7672 	    os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
7673 	    dialog_token == wpa_s->last_gas_dialog_token)
7674 		resp = wpa_s->last_gas_resp;
7675 	else if (wpa_s->prev_gas_resp &&
7676 		 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
7677 		 dialog_token == wpa_s->prev_gas_dialog_token)
7678 		resp = wpa_s->prev_gas_resp;
7679 	else
7680 		return -1;
7681 
7682 	resp_len = wpabuf_len(resp);
7683 	start = 0;
7684 	requested_len = resp_len;
7685 
7686 	pos = os_strchr(pos, ' ');
7687 	if (pos) {
7688 		start = atoi(pos);
7689 		if (start > resp_len)
7690 			return os_snprintf(buf, buflen, "FAIL-Invalid range");
7691 		pos = os_strchr(pos, ',');
7692 		if (pos == NULL)
7693 			return -1;
7694 		pos++;
7695 		requested_len = atoi(pos);
7696 		if (start + requested_len > resp_len)
7697 			return os_snprintf(buf, buflen, "FAIL-Invalid range");
7698 	}
7699 
7700 	if (requested_len * 2 + 1 > buflen)
7701 		return os_snprintf(buf, buflen, "FAIL-Too long response");
7702 
7703 	ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
7704 			       requested_len);
7705 
7706 	if (start + requested_len == resp_len) {
7707 		/*
7708 		 * Free memory by dropping the response after it has been
7709 		 * fetched.
7710 		 */
7711 		if (resp == wpa_s->prev_gas_resp) {
7712 			wpabuf_free(wpa_s->prev_gas_resp);
7713 			wpa_s->prev_gas_resp = NULL;
7714 		} else {
7715 			wpabuf_free(wpa_s->last_gas_resp);
7716 			wpa_s->last_gas_resp = NULL;
7717 		}
7718 	}
7719 
7720 	return ret;
7721 }
7722 #endif /* CONFIG_INTERWORKING */
7723 
7724 
7725 #ifdef CONFIG_HS20
7726 
get_hs20_anqp(struct wpa_supplicant * wpa_s,char * dst)7727 static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
7728 {
7729 	u8 dst_addr[ETH_ALEN];
7730 	int used;
7731 	char *pos;
7732 	u32 subtypes = 0;
7733 
7734 	used = hwaddr_aton2(dst, dst_addr);
7735 	if (used < 0)
7736 		return -1;
7737 	pos = dst + used;
7738 	if (*pos == ' ')
7739 		pos++;
7740 	for (;;) {
7741 		int num = atoi(pos);
7742 		if (num <= 0 || num > 31)
7743 			return -1;
7744 		subtypes |= BIT(num);
7745 		pos = os_strchr(pos + 1, ',');
7746 		if (pos == NULL)
7747 			break;
7748 		pos++;
7749 	}
7750 
7751 	if (subtypes == 0)
7752 		return -1;
7753 
7754 	return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
7755 }
7756 
7757 
hs20_nai_home_realm_list(struct wpa_supplicant * wpa_s,const u8 * addr,const char * realm)7758 static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
7759 				    const u8 *addr, const char *realm)
7760 {
7761 	u8 *buf;
7762 	size_t rlen, len;
7763 	int ret;
7764 
7765 	rlen = os_strlen(realm);
7766 	len = 3 + rlen;
7767 	buf = os_malloc(len);
7768 	if (buf == NULL)
7769 		return -1;
7770 	buf[0] = 1; /* NAI Home Realm Count */
7771 	buf[1] = 0; /* Formatted in accordance with RFC 4282 */
7772 	buf[2] = rlen;
7773 	os_memcpy(buf + 3, realm, rlen);
7774 
7775 	ret = hs20_anqp_send_req(wpa_s, addr,
7776 				 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
7777 				 buf, len, 0);
7778 
7779 	os_free(buf);
7780 
7781 	return ret;
7782 }
7783 
7784 
hs20_get_nai_home_realm_list(struct wpa_supplicant * wpa_s,char * dst)7785 static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
7786 					char *dst)
7787 {
7788 	struct wpa_cred *cred = wpa_s->conf->cred;
7789 	u8 dst_addr[ETH_ALEN];
7790 	int used;
7791 	u8 *buf;
7792 	size_t len;
7793 	int ret;
7794 
7795 	used = hwaddr_aton2(dst, dst_addr);
7796 	if (used < 0)
7797 		return -1;
7798 
7799 	while (dst[used] == ' ')
7800 		used++;
7801 	if (os_strncmp(dst + used, "realm=", 6) == 0)
7802 		return hs20_nai_home_realm_list(wpa_s, dst_addr,
7803 						dst + used + 6);
7804 
7805 	len = os_strlen(dst + used);
7806 
7807 	if (len == 0 && cred && cred->realm)
7808 		return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
7809 
7810 	if (len & 1)
7811 		return -1;
7812 	len /= 2;
7813 	buf = os_malloc(len);
7814 	if (buf == NULL)
7815 		return -1;
7816 	if (hexstr2bin(dst + used, buf, len) < 0) {
7817 		os_free(buf);
7818 		return -1;
7819 	}
7820 
7821 	ret = hs20_anqp_send_req(wpa_s, dst_addr,
7822 				 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
7823 				 buf, len, 0);
7824 	os_free(buf);
7825 
7826 	return ret;
7827 }
7828 
7829 
get_hs20_icon(struct wpa_supplicant * wpa_s,char * cmd,char * reply,int buflen)7830 static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
7831 			 int buflen)
7832 {
7833 	u8 dst_addr[ETH_ALEN];
7834 	int used;
7835 	char *ctx = NULL, *icon, *poffset, *psize;
7836 
7837 	used = hwaddr_aton2(cmd, dst_addr);
7838 	if (used < 0)
7839 		return -1;
7840 	cmd += used;
7841 
7842 	icon = str_token(cmd, " ", &ctx);
7843 	poffset = str_token(cmd, " ", &ctx);
7844 	psize = str_token(cmd, " ", &ctx);
7845 	if (!icon || !poffset || !psize)
7846 		return -1;
7847 
7848 	wpa_s->fetch_osu_icon_in_progress = 0;
7849 	return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
7850 			     reply, buflen);
7851 }
7852 
7853 
del_hs20_icon(struct wpa_supplicant * wpa_s,char * cmd)7854 static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
7855 {
7856 	u8 dst_addr[ETH_ALEN];
7857 	int used;
7858 	char *icon;
7859 
7860 	if (!cmd[0])
7861 		return hs20_del_icon(wpa_s, NULL, NULL);
7862 
7863 	used = hwaddr_aton2(cmd, dst_addr);
7864 	if (used < 0)
7865 		return -1;
7866 
7867 	while (cmd[used] == ' ')
7868 		used++;
7869 	icon = cmd[used] ? &cmd[used] : NULL;
7870 
7871 	return hs20_del_icon(wpa_s, dst_addr, icon);
7872 }
7873 
7874 
hs20_icon_request(struct wpa_supplicant * wpa_s,char * cmd,int inmem)7875 static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
7876 {
7877 	u8 dst_addr[ETH_ALEN];
7878 	int used;
7879 	char *icon;
7880 
7881 	used = hwaddr_aton2(cmd, dst_addr);
7882 	if (used < 0)
7883 		return -1;
7884 
7885 	while (cmd[used] == ' ')
7886 		used++;
7887 	icon = &cmd[used];
7888 
7889 	wpa_s->fetch_osu_icon_in_progress = 0;
7890 	return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
7891 				  (u8 *) icon, os_strlen(icon), inmem);
7892 }
7893 
7894 #endif /* CONFIG_HS20 */
7895 
7896 
7897 #ifdef CONFIG_AUTOSCAN
7898 
wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant * wpa_s,char * cmd)7899 static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
7900 					      char *cmd)
7901 {
7902 	enum wpa_states state = wpa_s->wpa_state;
7903 	char *new_params = NULL;
7904 
7905 	if (os_strlen(cmd) > 0) {
7906 		new_params = os_strdup(cmd);
7907 		if (new_params == NULL)
7908 			return -1;
7909 	}
7910 
7911 	os_free(wpa_s->conf->autoscan);
7912 	wpa_s->conf->autoscan = new_params;
7913 
7914 	if (wpa_s->conf->autoscan == NULL)
7915 		autoscan_deinit(wpa_s);
7916 	else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
7917 		autoscan_init(wpa_s, 1);
7918 	else if (state == WPA_SCANNING)
7919 		wpa_supplicant_reinit_autoscan(wpa_s);
7920 	else
7921 		wpa_printf(MSG_DEBUG, "No autoscan update in state %s",
7922 			   wpa_supplicant_state_txt(state));
7923 
7924 	return 0;
7925 }
7926 
7927 #endif /* CONFIG_AUTOSCAN */
7928 
7929 
7930 #ifdef CONFIG_WNM
7931 
wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant * wpa_s,char * cmd)7932 static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
7933 {
7934 	int enter;
7935 	int intval = 0;
7936 	char *pos;
7937 	int ret;
7938 	struct wpabuf *tfs_req = NULL;
7939 
7940 	if (os_strncmp(cmd, "enter", 5) == 0)
7941 		enter = 1;
7942 	else if (os_strncmp(cmd, "exit", 4) == 0)
7943 		enter = 0;
7944 	else
7945 		return -1;
7946 
7947 	pos = os_strstr(cmd, " interval=");
7948 	if (pos)
7949 		intval = atoi(pos + 10);
7950 
7951 	pos = os_strstr(cmd, " tfs_req=");
7952 	if (pos) {
7953 		char *end;
7954 		size_t len;
7955 		pos += 9;
7956 		end = os_strchr(pos, ' ');
7957 		if (end)
7958 			len = end - pos;
7959 		else
7960 			len = os_strlen(pos);
7961 		if (len & 1)
7962 			return -1;
7963 		len /= 2;
7964 		tfs_req = wpabuf_alloc(len);
7965 		if (tfs_req == NULL)
7966 			return -1;
7967 		if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
7968 			wpabuf_free(tfs_req);
7969 			return -1;
7970 		}
7971 	}
7972 
7973 	ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
7974 					   WNM_SLEEP_MODE_EXIT, intval,
7975 					   tfs_req);
7976 	wpabuf_free(tfs_req);
7977 
7978 	return ret;
7979 }
7980 
7981 
wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant * wpa_s,char * cmd)7982 static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
7983 {
7984 	int query_reason, list = 0;
7985 	char *btm_candidates = NULL;
7986 
7987 	query_reason = atoi(cmd);
7988 
7989 	cmd = os_strchr(cmd, ' ');
7990 	if (cmd) {
7991 		if (os_strncmp(cmd, " list", 5) == 0)
7992 			list = 1;
7993 		else
7994 			btm_candidates = cmd;
7995 	}
7996 
7997 	wpa_printf(MSG_DEBUG,
7998 		   "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
7999 		   query_reason, list ? " candidate list" : "");
8000 
8001 	return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason,
8002 						  btm_candidates,
8003 						  list);
8004 }
8005 
8006 
wpas_ctrl_iface_coloc_intf_report(struct wpa_supplicant * wpa_s,char * cmd)8007 static int wpas_ctrl_iface_coloc_intf_report(struct wpa_supplicant *wpa_s,
8008 					     char *cmd)
8009 {
8010 	struct wpabuf *elems;
8011 	int ret;
8012 
8013 	elems = wpabuf_parse_bin(cmd);
8014 	if (!elems)
8015 		return -1;
8016 
8017 	ret = wnm_send_coloc_intf_report(wpa_s, 0, elems);
8018 	wpabuf_free(elems);
8019 	return ret;
8020 }
8021 
8022 #endif /* CONFIG_WNM */
8023 
8024 
wpa_supplicant_signal_poll(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8025 static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
8026 				      size_t buflen)
8027 {
8028 	struct wpa_signal_info si;
8029 	int ret;
8030 	char *pos, *end;
8031 
8032 	ret = wpa_drv_signal_poll(wpa_s, &si);
8033 	if (ret)
8034 		return -1;
8035 
8036 	pos = buf;
8037 	end = buf + buflen;
8038 
8039 	ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
8040 			  "NOISE=%d\nFREQUENCY=%u\n",
8041 			  si.current_signal, si.current_txrate / 1000,
8042 			  si.current_noise, si.frequency);
8043 	if (os_snprintf_error(end - pos, ret))
8044 		return -1;
8045 	pos += ret;
8046 
8047 	if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
8048 		ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
8049 				  channel_width_to_string(si.chanwidth));
8050 		if (os_snprintf_error(end - pos, ret))
8051 			return -1;
8052 		pos += ret;
8053 	}
8054 
8055 	if (si.center_frq1 > 0) {
8056 		ret = os_snprintf(pos, end - pos, "CENTER_FRQ1=%d\n",
8057 				  si.center_frq1);
8058 		if (os_snprintf_error(end - pos, ret))
8059 			return -1;
8060 		pos += ret;
8061 	}
8062 
8063 	if (si.center_frq2 > 0) {
8064 		ret = os_snprintf(pos, end - pos, "CENTER_FRQ2=%d\n",
8065 				  si.center_frq2);
8066 		if (os_snprintf_error(end - pos, ret))
8067 			return -1;
8068 		pos += ret;
8069 	}
8070 
8071 	if (si.avg_signal) {
8072 		ret = os_snprintf(pos, end - pos,
8073 				  "AVG_RSSI=%d\n", si.avg_signal);
8074 		if (os_snprintf_error(end - pos, ret))
8075 			return -1;
8076 		pos += ret;
8077 	}
8078 
8079 	if (si.avg_beacon_signal) {
8080 		ret = os_snprintf(pos, end - pos,
8081 				  "AVG_BEACON_RSSI=%d\n", si.avg_beacon_signal);
8082 		if (os_snprintf_error(end - pos, ret))
8083 			return -1;
8084 		pos += ret;
8085 	}
8086 
8087 	return pos - buf;
8088 }
8089 
8090 
wpas_ctrl_iface_signal_monitor(struct wpa_supplicant * wpa_s,const char * cmd)8091 static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
8092 					  const char *cmd)
8093 {
8094 	const char *pos;
8095 	int threshold = 0;
8096 	int hysteresis = 0;
8097 
8098 	if (wpa_s->bgscan && wpa_s->bgscan_priv) {
8099 		wpa_printf(MSG_DEBUG,
8100 			   "Reject SIGNAL_MONITOR command - bgscan is active");
8101 		return -1;
8102 	}
8103 	pos = os_strstr(cmd, "THRESHOLD=");
8104 	if (pos)
8105 		threshold = atoi(pos + 10);
8106 	pos = os_strstr(cmd, "HYSTERESIS=");
8107 	if (pos)
8108 		hysteresis = atoi(pos + 11);
8109 	return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
8110 }
8111 
8112 
8113 #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)8114 int wpas_ctrl_iface_get_pref_freq_list_override(struct wpa_supplicant *wpa_s,
8115 						enum wpa_driver_if_type if_type,
8116 						unsigned int *num,
8117 						unsigned int *freq_list)
8118 {
8119 	char *pos = wpa_s->get_pref_freq_list_override;
8120 	char *end;
8121 	unsigned int count = 0;
8122 
8123 	/* Override string format:
8124 	 *  <if_type1>:<freq1>,<freq2>,... <if_type2>:... */
8125 
8126 	while (pos) {
8127 		if (atoi(pos) == (int) if_type)
8128 			break;
8129 		pos = os_strchr(pos, ' ');
8130 		if (pos)
8131 			pos++;
8132 	}
8133 	if (!pos)
8134 		return -1;
8135 	pos = os_strchr(pos, ':');
8136 	if (!pos)
8137 		return -1;
8138 	pos++;
8139 	end = os_strchr(pos, ' ');
8140 	while (pos && (!end || pos < end) && count < *num) {
8141 		freq_list[count++] = atoi(pos);
8142 		pos = os_strchr(pos, ',');
8143 		if (pos)
8144 			pos++;
8145 	}
8146 
8147 	*num = count;
8148 	return 0;
8149 }
8150 #endif /* CONFIG_TESTING_OPTIONS */
8151 
8152 
wpas_ctrl_iface_get_pref_freq_list(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8153 static int wpas_ctrl_iface_get_pref_freq_list(
8154 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
8155 {
8156 	unsigned int freq_list[100], num = 100, i;
8157 	int ret;
8158 	enum wpa_driver_if_type iface_type;
8159 	char *pos, *end;
8160 
8161 	pos = buf;
8162 	end = buf + buflen;
8163 
8164 	/* buf: "<interface_type>" */
8165 	if (os_strcmp(cmd, "STATION") == 0)
8166 		iface_type = WPA_IF_STATION;
8167 	else if (os_strcmp(cmd, "AP") == 0)
8168 		iface_type = WPA_IF_AP_BSS;
8169 	else if (os_strcmp(cmd, "P2P_GO") == 0)
8170 		iface_type = WPA_IF_P2P_GO;
8171 	else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
8172 		iface_type = WPA_IF_P2P_CLIENT;
8173 	else if (os_strcmp(cmd, "IBSS") == 0)
8174 		iface_type = WPA_IF_IBSS;
8175 	else if (os_strcmp(cmd, "TDLS") == 0)
8176 		iface_type = WPA_IF_TDLS;
8177 	else
8178 		return -1;
8179 
8180 	wpa_printf(MSG_DEBUG,
8181 		   "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
8182 		   iface_type, cmd);
8183 
8184 	ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
8185 	if (ret)
8186 		return -1;
8187 
8188 	for (i = 0; i < num; i++) {
8189 		ret = os_snprintf(pos, end - pos, "%s%u",
8190 				  i > 0 ? "," : "", freq_list[i]);
8191 		if (os_snprintf_error(end - pos, ret))
8192 			return -1;
8193 		pos += ret;
8194 	}
8195 
8196 	return pos - buf;
8197 }
8198 
8199 
wpas_ctrl_iface_driver_flags(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8200 static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
8201 					char *buf, size_t buflen)
8202 {
8203 	int ret, i;
8204 	char *pos, *end;
8205 
8206 	ret = os_snprintf(buf, buflen, "%016llX:\n",
8207 			  (long long unsigned) wpa_s->drv_flags);
8208 	if (os_snprintf_error(buflen, ret))
8209 		return -1;
8210 
8211 	pos = buf + ret;
8212 	end = buf + buflen;
8213 
8214 	for (i = 0; i < 64; i++) {
8215 		if (wpa_s->drv_flags & (1LLU << i)) {
8216 			ret = os_snprintf(pos, end - pos, "%s\n",
8217 					  driver_flag_to_string(1LLU << i));
8218 			if (os_snprintf_error(end - pos, ret))
8219 				return -1;
8220 			pos += ret;
8221 		}
8222 	}
8223 
8224 	return pos - buf;
8225 }
8226 
8227 
wpas_ctrl_iface_driver_flags2(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8228 static int wpas_ctrl_iface_driver_flags2(struct wpa_supplicant *wpa_s,
8229 					 char *buf, size_t buflen)
8230 {
8231 	int ret, i;
8232 	char *pos, *end;
8233 
8234 	ret = os_snprintf(buf, buflen, "%016llX:\n",
8235 			  (long long unsigned) wpa_s->drv_flags2);
8236 	if (os_snprintf_error(buflen, ret))
8237 		return -1;
8238 
8239 	pos = buf + ret;
8240 	end = buf + buflen;
8241 
8242 	for (i = 0; i < 64; i++) {
8243 		if (wpa_s->drv_flags2 & (1LLU << i)) {
8244 			ret = os_snprintf(pos, end - pos, "%s\n",
8245 					  driver_flag2_to_string(1LLU << i));
8246 			if (os_snprintf_error(end - pos, ret))
8247 				return -1;
8248 			pos += ret;
8249 		}
8250 	}
8251 
8252 	return pos - buf;
8253 }
8254 
8255 
wpa_supplicant_pktcnt_poll(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8256 static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
8257 				      size_t buflen)
8258 {
8259 	struct hostap_sta_driver_data sta;
8260 	int ret;
8261 
8262 	ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
8263 	if (ret)
8264 		return -1;
8265 
8266 	ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
8267 			  sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
8268 	if (os_snprintf_error(buflen, ret))
8269 		return -1;
8270 	return ret;
8271 }
8272 
8273 
8274 #ifdef ANDROID
wpa_supplicant_driver_cmd(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8275 static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
8276 				     char *buf, size_t buflen)
8277 {
8278 	int ret;
8279 
8280 	ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
8281 	if (ret == 0) {
8282 		if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
8283 			struct p2p_data *p2p = wpa_s->global->p2p;
8284 			if (p2p) {
8285 				char country[3];
8286 				country[0] = cmd[8];
8287 				country[1] = cmd[9];
8288 				country[2] = 0x04;
8289 				p2p_set_country(p2p, country);
8290 			}
8291 		}
8292 		ret = os_snprintf(buf, buflen, "%s\n", "OK");
8293 		if (os_snprintf_error(buflen, ret))
8294 			ret = -1;
8295 	}
8296 	return ret;
8297 }
8298 #endif /* ANDROID */
8299 
8300 
wpa_supplicant_vendor_cmd(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8301 static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
8302 				     char *buf, size_t buflen)
8303 {
8304 	int ret;
8305 	char *pos, *temp = NULL;
8306 	u8 *data = NULL;
8307 	unsigned int vendor_id, subcmd;
8308 	enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
8309 	struct wpabuf *reply;
8310 	size_t data_len = 0;
8311 
8312 	/**
8313 	 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
8314 	 * [nested=<0|1>]
8315 	 */
8316 	vendor_id = strtoul(cmd, &pos, 16);
8317 	if (!isblank((unsigned char) *pos))
8318 		return -EINVAL;
8319 
8320 	subcmd = strtoul(pos, &pos, 10);
8321 
8322 	if (*pos != '\0') {
8323 		if (!isblank((unsigned char) *pos++))
8324 			return -EINVAL;
8325 
8326 		temp = os_strchr(pos, ' ');
8327 		data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
8328 	}
8329 
8330 	if (data_len) {
8331 		data_len /= 2;
8332 		data = os_malloc(data_len);
8333 		if (!data)
8334 			return -1;
8335 
8336 		if (hexstr2bin(pos, data, data_len)) {
8337 			wpa_printf(MSG_DEBUG,
8338 				   "Vendor command: wrong parameter format");
8339 			os_free(data);
8340 			return -EINVAL;
8341 		}
8342 	}
8343 
8344 	pos = os_strstr(cmd, "nested=");
8345 	if (pos)
8346 		nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
8347 			NESTED_ATTR_NOT_USED;
8348 
8349 	reply = wpabuf_alloc((buflen - 1) / 2);
8350 	if (!reply) {
8351 		os_free(data);
8352 		return -1;
8353 	}
8354 
8355 	ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
8356 				 nested_attr_flag, reply);
8357 
8358 	if (ret == 0)
8359 		ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
8360 				       wpabuf_len(reply));
8361 
8362 	wpabuf_free(reply);
8363 	os_free(data);
8364 
8365 	return ret;
8366 }
8367 
8368 
wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant * wpa_s)8369 static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
8370 {
8371 #ifdef CONFIG_P2P
8372 	struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
8373 		wpa_s->global->p2p_init_wpa_s : wpa_s;
8374 #endif /* CONFIG_P2P */
8375 
8376 	wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
8377 
8378 	if (wpas_abort_ongoing_scan(wpa_s) == 0)
8379 		wpa_s->ignore_post_flush_scan_res = 1;
8380 
8381 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
8382 		/*
8383 		 * Avoid possible auto connect re-connection on getting
8384 		 * disconnected due to state flush.
8385 		 */
8386 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
8387 	}
8388 
8389 #ifdef CONFIG_P2P
8390 	wpas_p2p_group_remove(p2p_wpa_s, "*");
8391 	wpas_p2p_cancel(p2p_wpa_s);
8392 	p2p_ctrl_flush(p2p_wpa_s);
8393 	wpas_p2p_service_flush(p2p_wpa_s);
8394 	p2p_wpa_s->global->p2p_disabled = 0;
8395 	p2p_wpa_s->global->p2p_per_sta_psk = 0;
8396 	p2p_wpa_s->conf->num_sec_device_types = 0;
8397 	p2p_wpa_s->p2p_disable_ip_addr_req = 0;
8398 	os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
8399 	p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
8400 	p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
8401 	p2p_wpa_s->global->pending_p2ps_group = 0;
8402 	p2p_wpa_s->global->pending_p2ps_group_freq = 0;
8403 #endif /* CONFIG_P2P */
8404 
8405 #ifdef CONFIG_WPS_TESTING
8406 	wps_version_number = 0x20;
8407 	wps_testing_stub_cred = 0;
8408 	wps_corrupt_pkhash = 0;
8409 	wps_force_auth_types_in_use = 0;
8410 	wps_force_encr_types_in_use = 0;
8411 #endif /* CONFIG_WPS_TESTING */
8412 #ifdef CONFIG_WPS
8413 	wpa_s->wps_fragment_size = 0;
8414 	wpas_wps_cancel(wpa_s);
8415 	wps_registrar_flush(wpa_s->wps->registrar);
8416 #endif /* CONFIG_WPS */
8417 	wpa_s->after_wps = 0;
8418 	wpa_s->known_wps_freq = 0;
8419 
8420 #ifdef CONFIG_DPP
8421 	wpas_dpp_deinit(wpa_s);
8422 	wpa_s->dpp_init_max_tries = 0;
8423 	wpa_s->dpp_init_retry_time = 0;
8424 	wpa_s->dpp_resp_wait_time = 0;
8425 	wpa_s->dpp_resp_max_tries = 0;
8426 	wpa_s->dpp_resp_retry_time = 0;
8427 #ifdef CONFIG_DPP2
8428 	wpas_dpp_chirp_stop(wpa_s);
8429 	wpa_s->dpp_pfs_fallback = 0;
8430 #endif /* CONFIG_DPP2 */
8431 #ifdef CONFIG_TESTING_OPTIONS
8432 	os_memset(dpp_pkex_own_mac_override, 0, ETH_ALEN);
8433 	os_memset(dpp_pkex_peer_mac_override, 0, ETH_ALEN);
8434 	dpp_pkex_ephemeral_key_override_len = 0;
8435 	dpp_protocol_key_override_len = 0;
8436 	dpp_nonce_override_len = 0;
8437 #ifdef CONFIG_DPP3
8438 	dpp_version_override = 3;
8439 #elif defined(CONFIG_DPP2)
8440 	dpp_version_override = 2;
8441 #else /* CONFIG_DPP2 */
8442 	dpp_version_override = 1;
8443 #endif /* CONFIG_DPP2 */
8444 #endif /* CONFIG_TESTING_OPTIONS */
8445 #endif /* CONFIG_DPP */
8446 
8447 #ifdef CONFIG_TDLS
8448 #ifdef CONFIG_TDLS_TESTING
8449 	tdls_testing = 0;
8450 #endif /* CONFIG_TDLS_TESTING */
8451 	wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
8452 	wpa_tdls_enable(wpa_s->wpa, 1);
8453 #endif /* CONFIG_TDLS */
8454 
8455 	eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
8456 	wpa_supplicant_stop_countermeasures(wpa_s, NULL);
8457 	wpa_s->last_michael_mic_error.sec = 0;
8458 
8459 	wpa_s->no_keep_alive = 0;
8460 	wpa_s->own_disconnect_req = 0;
8461 	wpa_s->own_reconnect_req = 0;
8462 	wpa_s->deny_ptk0_rekey = 0;
8463 
8464 	os_free(wpa_s->disallow_aps_bssid);
8465 	wpa_s->disallow_aps_bssid = NULL;
8466 	wpa_s->disallow_aps_bssid_count = 0;
8467 	os_free(wpa_s->disallow_aps_ssid);
8468 	wpa_s->disallow_aps_ssid = NULL;
8469 	wpa_s->disallow_aps_ssid_count = 0;
8470 
8471 	wpa_s->set_sta_uapsd = 0;
8472 	wpa_s->sta_uapsd = 0;
8473 
8474 	wpa_s->consecutive_conn_failures = 0;
8475 
8476 	wpa_drv_radio_disable(wpa_s, 0);
8477 	wpa_bssid_ignore_clear(wpa_s);
8478 	wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
8479 	wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
8480 	wpa_config_flush_blobs(wpa_s->conf);
8481 	wpa_s->conf->auto_interworking = 0;
8482 	wpa_s->conf->okc = 0;
8483 
8484 	ptksa_cache_flush(wpa_s->ptksa, NULL, WPA_CIPHER_NONE);
8485 	wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
8486 	rsn_preauth_deinit(wpa_s->wpa);
8487 
8488 	wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
8489 	wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
8490 	wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
8491 	eapol_sm_notify_logoff(wpa_s->eapol, false);
8492 
8493 	radio_remove_works(wpa_s, NULL, 1);
8494 	wpa_s->ext_work_in_progress = 0;
8495 
8496 	wpa_s->next_ssid = NULL;
8497 
8498 #ifdef CONFIG_INTERWORKING
8499 #ifdef CONFIG_HS20
8500 	hs20_cancel_fetch_osu(wpa_s);
8501 	hs20_del_icon(wpa_s, NULL, NULL);
8502 #endif /* CONFIG_HS20 */
8503 #endif /* CONFIG_INTERWORKING */
8504 
8505 	wpa_s->ext_mgmt_frame_handling = 0;
8506 	wpa_s->ext_eapol_frame_io = 0;
8507 #ifdef CONFIG_TESTING_OPTIONS
8508 	wpa_s->extra_roc_dur = 0;
8509 	wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
8510 	wpa_s->p2p_go_csa_on_inv = 0;
8511 	wpa_s->ignore_auth_resp = 0;
8512 	wpa_s->ignore_assoc_disallow = 0;
8513 	wpa_s->disable_sa_query = 0;
8514 	wpa_s->testing_resend_assoc = 0;
8515 	wpa_s->ignore_sae_h2e_only = 0;
8516 	wpa_s->ft_rsnxe_used = 0;
8517 	wpa_s->reject_btm_req_reason = 0;
8518 	wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
8519 	os_free(wpa_s->get_pref_freq_list_override);
8520 	wpa_s->get_pref_freq_list_override = NULL;
8521 	wpabuf_free(wpa_s->sae_commit_override);
8522 	wpa_s->sae_commit_override = NULL;
8523 	os_free(wpa_s->extra_sae_rejected_groups);
8524 	wpa_s->extra_sae_rejected_groups = NULL;
8525 	wpabuf_free(wpa_s->rsne_override_eapol);
8526 	wpa_s->rsne_override_eapol = NULL;
8527 	wpabuf_free(wpa_s->rsnxe_override_assoc);
8528 	wpa_s->rsnxe_override_assoc = NULL;
8529 	wpabuf_free(wpa_s->rsnxe_override_eapol);
8530 	wpa_s->rsnxe_override_eapol = NULL;
8531 	wpas_clear_driver_signal_override(wpa_s);
8532 	wpa_s->disable_scs_support = 0;
8533 	wpa_s->disable_mscs_support = 0;
8534 	wpa_s->enable_dscp_policy_capa = 0;
8535 	wpa_s->oci_freq_override_eapol = 0;
8536 	wpa_s->oci_freq_override_saquery_req = 0;
8537 	wpa_s->oci_freq_override_saquery_resp = 0;
8538 	wpa_s->oci_freq_override_eapol_g2 = 0;
8539 	wpa_s->oci_freq_override_ft_assoc = 0;
8540 	wpa_s->oci_freq_override_fils_assoc = 0;
8541 	wpa_s->oci_freq_override_wnm_sleep = 0;
8542 #ifdef CONFIG_DPP
8543 	os_free(wpa_s->dpp_config_obj_override);
8544 	wpa_s->dpp_config_obj_override = NULL;
8545 	os_free(wpa_s->dpp_discovery_override);
8546 	wpa_s->dpp_discovery_override = NULL;
8547 	os_free(wpa_s->dpp_groups_override);
8548 	wpa_s->dpp_groups_override = NULL;
8549 	dpp_test = DPP_TEST_DISABLED;
8550 #endif /* CONFIG_DPP */
8551 #endif /* CONFIG_TESTING_OPTIONS */
8552 
8553 	wpa_s->disconnected = 0;
8554 	os_free(wpa_s->next_scan_freqs);
8555 	wpa_s->next_scan_freqs = NULL;
8556 	os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
8557 	wpa_s->next_scan_bssid_wildcard_ssid = 0;
8558 	os_free(wpa_s->select_network_scan_freqs);
8559 	wpa_s->select_network_scan_freqs = NULL;
8560 	os_memset(&wpa_s->robust_av, 0, sizeof(struct robust_av_data));
8561 
8562 	wpa_bss_flush(wpa_s);
8563 	if (!dl_list_empty(&wpa_s->bss)) {
8564 		wpa_printf(MSG_DEBUG,
8565 			   "BSS table not empty after flush: %u entries, current_bss=%p bssid="
8566 			   MACSTR " pending_bssid=" MACSTR,
8567 			   dl_list_len(&wpa_s->bss), wpa_s->current_bss,
8568 			   MAC2STR(wpa_s->bssid),
8569 			   MAC2STR(wpa_s->pending_bssid));
8570 	}
8571 
8572 	eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
8573 	wpa_s->wnmsleep_used = 0;
8574 
8575 #ifdef CONFIG_SME
8576 	wpa_s->sme.last_unprot_disconnect.sec = 0;
8577 	wpa_s->sme.auth_alg = 0;
8578 #endif /* CONFIG_SME */
8579 
8580 	wpabuf_free(wpa_s->ric_ies);
8581 	wpa_s->ric_ies = NULL;
8582 
8583 	wpa_supplicant_update_channel_list(wpa_s, NULL);
8584 
8585 	free_bss_tmp_disallowed(wpa_s);
8586 
8587 	os_memset(&wpa_s->robust_av, 0, sizeof(struct robust_av_data));
8588 
8589 #ifdef CONFIG_PASN
8590 	wpas_pasn_auth_stop(wpa_s);
8591 #endif /* CONFIG_PASN */
8592 
8593 	if (wpa_s->mac_addr_changed && wpa_s->conf->mac_addr == 0)
8594 		wpas_restore_permanent_mac_addr(wpa_s);
8595 
8596 	wpa_s->conf->ignore_old_scan_res = 0;
8597 }
8598 
8599 
wpas_ctrl_radio_work_show(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8600 static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
8601 				     char *buf, size_t buflen)
8602 {
8603 	struct wpa_radio_work *work;
8604 	char *pos, *end;
8605 	struct os_reltime now, diff;
8606 
8607 	pos = buf;
8608 	end = buf + buflen;
8609 
8610 	os_get_reltime(&now);
8611 
8612 	dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
8613 	{
8614 		int ret;
8615 
8616 		os_reltime_sub(&now, &work->time, &diff);
8617 		ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%lld.%06lld\n",
8618 				  work->type, work->wpa_s->ifname, work->freq,
8619 				  work->started, diff.sec, diff.usec);
8620 		if (os_snprintf_error(end - pos, ret))
8621 			break;
8622 		pos += ret;
8623 	}
8624 
8625 	return pos - buf;
8626 }
8627 
8628 
wpas_ctrl_radio_work_timeout(void * eloop_ctx,void * timeout_ctx)8629 static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
8630 {
8631 	struct wpa_radio_work *work = eloop_ctx;
8632 	struct wpa_external_work *ework = work->ctx;
8633 
8634 	wpa_dbg(work->wpa_s, MSG_DEBUG,
8635 		"Timing out external radio work %u (%s)",
8636 		ework->id, work->type);
8637 	wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
8638 	work->wpa_s->ext_work_in_progress = 0;
8639 	radio_work_done(work);
8640 	os_free(ework);
8641 }
8642 
8643 
wpas_ctrl_radio_work_cb(struct wpa_radio_work * work,int deinit)8644 static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
8645 {
8646 	struct wpa_external_work *ework = work->ctx;
8647 
8648 	if (deinit) {
8649 		if (work->started)
8650 			eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
8651 					     work, NULL);
8652 
8653 		/*
8654 		 * work->type points to a buffer in ework, so need to replace
8655 		 * that here with a fixed string to avoid use of freed memory
8656 		 * in debug prints.
8657 		 */
8658 		work->type = "freed-ext-work";
8659 		work->ctx = NULL;
8660 		os_free(ework);
8661 		return;
8662 	}
8663 
8664 	wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
8665 		ework->id, ework->type);
8666 	wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
8667 	work->wpa_s->ext_work_in_progress = 1;
8668 	if (!ework->timeout)
8669 		ework->timeout = 10;
8670 	eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
8671 			       work, NULL);
8672 }
8673 
8674 
wpas_ctrl_radio_work_add(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8675 static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
8676 				    char *buf, size_t buflen)
8677 {
8678 	struct wpa_external_work *ework;
8679 	char *pos, *pos2;
8680 	size_t type_len;
8681 	int ret;
8682 	unsigned int freq = 0;
8683 
8684 	/* format: <name> [freq=<MHz>] [timeout=<seconds>] */
8685 
8686 	ework = os_zalloc(sizeof(*ework));
8687 	if (ework == NULL)
8688 		return -1;
8689 
8690 	pos = os_strchr(cmd, ' ');
8691 	if (pos) {
8692 		type_len = pos - cmd;
8693 		pos++;
8694 
8695 		pos2 = os_strstr(pos, "freq=");
8696 		if (pos2)
8697 			freq = atoi(pos2 + 5);
8698 
8699 		pos2 = os_strstr(pos, "timeout=");
8700 		if (pos2)
8701 			ework->timeout = atoi(pos2 + 8);
8702 	} else {
8703 		type_len = os_strlen(cmd);
8704 	}
8705 	if (4 + type_len >= sizeof(ework->type))
8706 		type_len = sizeof(ework->type) - 4 - 1;
8707 	os_strlcpy(ework->type, "ext:", sizeof(ework->type));
8708 	os_memcpy(ework->type + 4, cmd, type_len);
8709 	ework->type[4 + type_len] = '\0';
8710 
8711 	wpa_s->ext_work_id++;
8712 	if (wpa_s->ext_work_id == 0)
8713 		wpa_s->ext_work_id++;
8714 	ework->id = wpa_s->ext_work_id;
8715 
8716 	if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
8717 			   ework) < 0) {
8718 		os_free(ework);
8719 		return -1;
8720 	}
8721 
8722 	ret = os_snprintf(buf, buflen, "%u", ework->id);
8723 	if (os_snprintf_error(buflen, ret))
8724 		return -1;
8725 	return ret;
8726 }
8727 
8728 
wpas_ctrl_radio_work_done(struct wpa_supplicant * wpa_s,char * cmd)8729 static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
8730 {
8731 	struct wpa_radio_work *work;
8732 	unsigned int id = atoi(cmd);
8733 
8734 	dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
8735 	{
8736 		struct wpa_external_work *ework;
8737 
8738 		if (os_strncmp(work->type, "ext:", 4) != 0)
8739 			continue;
8740 		ework = work->ctx;
8741 		if (id && ework->id != id)
8742 			continue;
8743 		wpa_dbg(wpa_s, MSG_DEBUG,
8744 			"Completed external radio work %u (%s)",
8745 			ework->id, ework->type);
8746 		eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
8747 		wpa_s->ext_work_in_progress = 0;
8748 		radio_work_done(work);
8749 		os_free(ework);
8750 		return 3; /* "OK\n" */
8751 	}
8752 
8753 	return -1;
8754 }
8755 
8756 
wpas_ctrl_radio_work(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8757 static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
8758 				char *buf, size_t buflen)
8759 {
8760 	if (os_strcmp(cmd, "show") == 0)
8761 		return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
8762 	if (os_strncmp(cmd, "add ", 4) == 0)
8763 		return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
8764 	if (os_strncmp(cmd, "done ", 5) == 0)
8765 		return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
8766 	return -1;
8767 }
8768 
8769 
wpas_ctrl_radio_work_flush(struct wpa_supplicant * wpa_s)8770 void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
8771 {
8772 	struct wpa_radio_work *work, *tmp;
8773 
8774 	if (!wpa_s || !wpa_s->radio)
8775 		return;
8776 
8777 	dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
8778 			      struct wpa_radio_work, list) {
8779 		struct wpa_external_work *ework;
8780 
8781 		if (os_strncmp(work->type, "ext:", 4) != 0)
8782 			continue;
8783 		ework = work->ctx;
8784 		wpa_dbg(wpa_s, MSG_DEBUG,
8785 			"Flushing%s external radio work %u (%s)",
8786 			work->started ? " started" : "", ework->id,
8787 			ework->type);
8788 		if (work->started)
8789 			eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
8790 					     work, NULL);
8791 		radio_work_done(work);
8792 		os_free(ework);
8793 	}
8794 }
8795 
8796 
wpas_ctrl_eapol_response(void * eloop_ctx,void * timeout_ctx)8797 static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
8798 {
8799 	struct wpa_supplicant *wpa_s = eloop_ctx;
8800 	eapol_sm_notify_ctrl_response(wpa_s->eapol);
8801 }
8802 
8803 
scan_id_list_parse(struct wpa_supplicant * wpa_s,const char * value,unsigned int * scan_id_count,int scan_id[])8804 static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
8805 			      unsigned int *scan_id_count, int scan_id[])
8806 {
8807 	const char *pos = value;
8808 
8809 	while (pos) {
8810 		if (*pos == ' ' || *pos == '\0')
8811 			break;
8812 		if (*scan_id_count == MAX_SCAN_ID)
8813 			return -1;
8814 		scan_id[(*scan_id_count)++] = atoi(pos);
8815 		pos = os_strchr(pos, ',');
8816 		if (pos)
8817 			pos++;
8818 	}
8819 
8820 	return 0;
8821 }
8822 
8823 
wpas_ctrl_scan(struct wpa_supplicant * wpa_s,char * params,char * reply,int reply_size,int * reply_len)8824 static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
8825 			   char *reply, int reply_size, int *reply_len)
8826 {
8827 	char *pos;
8828 	unsigned int manual_scan_passive = 0;
8829 	unsigned int manual_scan_use_id = 0;
8830 	unsigned int manual_scan_only_new = 0;
8831 	unsigned int scan_only = 0;
8832 	unsigned int scan_id_count = 0;
8833 	int scan_id[MAX_SCAN_ID];
8834 	void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
8835 				 struct wpa_scan_results *scan_res);
8836 	int *manual_scan_freqs = NULL;
8837 	struct wpa_ssid_value *ssid = NULL, *ns;
8838 	unsigned int ssid_count = 0;
8839 
8840 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
8841 		*reply_len = -1;
8842 		return;
8843 	}
8844 
8845 	if (radio_work_pending(wpa_s, "scan")) {
8846 		wpa_printf(MSG_DEBUG,
8847 			   "Pending scan scheduled - reject new request");
8848 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
8849 		return;
8850 	}
8851 
8852 #ifdef CONFIG_INTERWORKING
8853 	if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
8854 		wpa_printf(MSG_DEBUG,
8855 			   "Interworking select in progress - reject new scan");
8856 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
8857 		return;
8858 	}
8859 #endif /* CONFIG_INTERWORKING */
8860 
8861 	if (params) {
8862 		if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
8863 			scan_only = 1;
8864 
8865 		pos = os_strstr(params, "freq=");
8866 		if (pos) {
8867 			manual_scan_freqs = freq_range_to_channel_list(wpa_s,
8868 								       pos + 5);
8869 			if (manual_scan_freqs == NULL) {
8870 				*reply_len = -1;
8871 				goto done;
8872 			}
8873 		}
8874 
8875 		pos = os_strstr(params, "passive=");
8876 		if (pos)
8877 			manual_scan_passive = !!atoi(pos + 8);
8878 
8879 		pos = os_strstr(params, "use_id=");
8880 		if (pos)
8881 			manual_scan_use_id = atoi(pos + 7);
8882 
8883 		pos = os_strstr(params, "only_new=1");
8884 		if (pos)
8885 			manual_scan_only_new = 1;
8886 
8887 		pos = os_strstr(params, "scan_id=");
8888 		if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
8889 					      scan_id) < 0) {
8890 			*reply_len = -1;
8891 			goto done;
8892 		}
8893 
8894 		pos = os_strstr(params, "bssid=");
8895 		if (pos) {
8896 			u8 bssid[ETH_ALEN];
8897 
8898 			pos += 6;
8899 			if (hwaddr_aton(pos, bssid)) {
8900 				wpa_printf(MSG_ERROR, "Invalid BSSID %s", pos);
8901 				*reply_len = -1;
8902 				goto done;
8903 			}
8904 			os_memcpy(wpa_s->next_scan_bssid, bssid, ETH_ALEN);
8905 
8906 			wpa_s->next_scan_bssid_wildcard_ssid =
8907 				os_strstr(params, "wildcard_ssid=1") != NULL;
8908 		}
8909 
8910 		pos = params;
8911 		while (pos && *pos != '\0') {
8912 			if (os_strncmp(pos, "ssid ", 5) == 0) {
8913 				char *end;
8914 
8915 				pos += 5;
8916 				end = pos;
8917 				while (*end) {
8918 					if (*end == '\0' || *end == ' ')
8919 						break;
8920 					end++;
8921 				}
8922 
8923 				ns = os_realloc_array(
8924 					ssid, ssid_count + 1,
8925 					sizeof(struct wpa_ssid_value));
8926 				if (ns == NULL) {
8927 					*reply_len = -1;
8928 					goto done;
8929 				}
8930 				ssid = ns;
8931 
8932 				if ((end - pos) & 0x01 ||
8933 				    end - pos > 2 * SSID_MAX_LEN ||
8934 				    hexstr2bin(pos, ssid[ssid_count].ssid,
8935 					       (end - pos) / 2) < 0) {
8936 					wpa_printf(MSG_DEBUG,
8937 						   "Invalid SSID value '%s'",
8938 						   pos);
8939 					*reply_len = -1;
8940 					goto done;
8941 				}
8942 				ssid[ssid_count].ssid_len = (end - pos) / 2;
8943 				wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
8944 						  ssid[ssid_count].ssid,
8945 						  ssid[ssid_count].ssid_len);
8946 				ssid_count++;
8947 				pos = end;
8948 			}
8949 
8950 			pos = os_strchr(pos, ' ');
8951 			if (pos)
8952 				pos++;
8953 		}
8954 	}
8955 
8956 	wpa_s->num_ssids_from_scan_req = ssid_count;
8957 	os_free(wpa_s->ssids_from_scan_req);
8958 	if (ssid_count) {
8959 		wpa_s->ssids_from_scan_req = ssid;
8960 		ssid = NULL;
8961 	} else {
8962 		wpa_s->ssids_from_scan_req = NULL;
8963 	}
8964 
8965 	if (scan_only)
8966 		scan_res_handler = scan_only_handler;
8967 	else if (wpa_s->scan_res_handler == scan_only_handler)
8968 		scan_res_handler = NULL;
8969 	else
8970 		scan_res_handler = wpa_s->scan_res_handler;
8971 
8972 	if (!wpa_s->sched_scanning && !wpa_s->scanning &&
8973 	    ((wpa_s->wpa_state <= WPA_SCANNING) ||
8974 	     (wpa_s->wpa_state == WPA_COMPLETED))) {
8975 		wpa_s->manual_scan_passive = manual_scan_passive;
8976 		wpa_s->manual_scan_use_id = manual_scan_use_id;
8977 		wpa_s->manual_scan_only_new = manual_scan_only_new;
8978 		wpa_s->scan_id_count = scan_id_count;
8979 		os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
8980 		wpa_s->scan_res_handler = scan_res_handler;
8981 		os_free(wpa_s->manual_scan_freqs);
8982 		wpa_s->manual_scan_freqs = manual_scan_freqs;
8983 		manual_scan_freqs = NULL;
8984 
8985 		wpa_s->normal_scans = 0;
8986 		wpa_s->scan_req = MANUAL_SCAN_REQ;
8987 		wpa_s->after_wps = 0;
8988 		wpa_s->known_wps_freq = 0;
8989 		wpa_supplicant_req_scan(wpa_s, 0, 0);
8990 		if (wpa_s->manual_scan_use_id) {
8991 			wpa_s->manual_scan_id++;
8992 			wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
8993 				wpa_s->manual_scan_id);
8994 			*reply_len = os_snprintf(reply, reply_size, "%u\n",
8995 						 wpa_s->manual_scan_id);
8996 		}
8997 	} else if (wpa_s->sched_scanning) {
8998 		wpa_s->manual_scan_passive = manual_scan_passive;
8999 		wpa_s->manual_scan_use_id = manual_scan_use_id;
9000 		wpa_s->manual_scan_only_new = manual_scan_only_new;
9001 		wpa_s->scan_id_count = scan_id_count;
9002 		os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
9003 		wpa_s->scan_res_handler = scan_res_handler;
9004 		os_free(wpa_s->manual_scan_freqs);
9005 		wpa_s->manual_scan_freqs = manual_scan_freqs;
9006 		manual_scan_freqs = NULL;
9007 
9008 		wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
9009 		wpa_supplicant_cancel_sched_scan(wpa_s);
9010 		wpa_s->scan_req = MANUAL_SCAN_REQ;
9011 		wpa_supplicant_req_scan(wpa_s, 0, 0);
9012 		if (wpa_s->manual_scan_use_id) {
9013 			wpa_s->manual_scan_id++;
9014 			*reply_len = os_snprintf(reply, reply_size, "%u\n",
9015 						 wpa_s->manual_scan_id);
9016 			wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
9017 				wpa_s->manual_scan_id);
9018 		}
9019 	} else {
9020 		wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
9021 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
9022 	}
9023 
9024 done:
9025 	os_free(manual_scan_freqs);
9026 	os_free(ssid);
9027 }
9028 
9029 
9030 #ifdef CONFIG_TESTING_OPTIONS
9031 
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)9032 static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
9033 				       unsigned int freq, const u8 *dst,
9034 				       const u8 *src, const u8 *bssid,
9035 				       const u8 *data, size_t data_len,
9036 				       enum offchannel_send_action_result
9037 				       result)
9038 {
9039 	wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
9040 		" src=" MACSTR " bssid=" MACSTR " result=%s",
9041 		freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
9042 		result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
9043 		"SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
9044 			     "NO_ACK" : "FAILED"));
9045 }
9046 
9047 
wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant * wpa_s,char * cmd)9048 static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
9049 {
9050 	char *pos, *param;
9051 	size_t len;
9052 	u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
9053 	int res, used;
9054 	int freq = 0, no_cck = 0, wait_time = 0;
9055 
9056 	/* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
9057 	 *    <action=Action frame payload> */
9058 
9059 	wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
9060 
9061 	pos = cmd;
9062 	used = hwaddr_aton2(pos, da);
9063 	if (used < 0)
9064 		return -1;
9065 	pos += used;
9066 	while (*pos == ' ')
9067 		pos++;
9068 	used = hwaddr_aton2(pos, bssid);
9069 	if (used < 0)
9070 		return -1;
9071 	pos += used;
9072 
9073 	param = os_strstr(pos, " freq=");
9074 	if (param) {
9075 		param += 6;
9076 		freq = atoi(param);
9077 	}
9078 
9079 	param = os_strstr(pos, " no_cck=");
9080 	if (param) {
9081 		param += 8;
9082 		no_cck = atoi(param);
9083 	}
9084 
9085 	param = os_strstr(pos, " wait_time=");
9086 	if (param) {
9087 		param += 11;
9088 		wait_time = atoi(param);
9089 	}
9090 
9091 	param = os_strstr(pos, " action=");
9092 	if (param == NULL)
9093 		return -1;
9094 	param += 8;
9095 
9096 	len = os_strlen(param);
9097 	if (len & 1)
9098 		return -1;
9099 	len /= 2;
9100 
9101 	buf = os_malloc(len);
9102 	if (buf == NULL)
9103 		return -1;
9104 
9105 	if (hexstr2bin(param, buf, len) < 0) {
9106 		os_free(buf);
9107 		return -1;
9108 	}
9109 
9110 	res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
9111 				     buf, len, wait_time,
9112 				     wpas_ctrl_iface_mgmt_tx_cb, no_cck);
9113 	os_free(buf);
9114 	return res;
9115 }
9116 
9117 
wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant * wpa_s)9118 static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
9119 {
9120 	wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
9121 	offchannel_send_action_done(wpa_s);
9122 }
9123 
9124 
wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant * wpa_s,char * cmd)9125 static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
9126 					   char *cmd)
9127 {
9128 	char *pos, *param;
9129 	size_t len;
9130 	u8 *buf;
9131 	int freq = 0, datarate = 0, ssi_signal = 0;
9132 	union wpa_event_data event;
9133 
9134 	if (!wpa_s->ext_mgmt_frame_handling)
9135 		return -1;
9136 
9137 	/* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
9138 
9139 	wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
9140 
9141 	pos = cmd;
9142 	param = os_strstr(pos, "freq=");
9143 	if (param) {
9144 		param += 5;
9145 		freq = atoi(param);
9146 	}
9147 
9148 	param = os_strstr(pos, " datarate=");
9149 	if (param) {
9150 		param += 10;
9151 		datarate = atoi(param);
9152 	}
9153 
9154 	param = os_strstr(pos, " ssi_signal=");
9155 	if (param) {
9156 		param += 12;
9157 		ssi_signal = atoi(param);
9158 	}
9159 
9160 	param = os_strstr(pos, " frame=");
9161 	if (param == NULL)
9162 		return -1;
9163 	param += 7;
9164 
9165 	len = os_strlen(param);
9166 	if (len & 1)
9167 		return -1;
9168 	len /= 2;
9169 
9170 	buf = os_malloc(len);
9171 	if (buf == NULL)
9172 		return -1;
9173 
9174 	if (hexstr2bin(param, buf, len) < 0) {
9175 		os_free(buf);
9176 		return -1;
9177 	}
9178 
9179 	os_memset(&event, 0, sizeof(event));
9180 	event.rx_mgmt.freq = freq;
9181 	event.rx_mgmt.frame = buf;
9182 	event.rx_mgmt.frame_len = len;
9183 	event.rx_mgmt.ssi_signal = ssi_signal;
9184 	event.rx_mgmt.datarate = datarate;
9185 	wpa_s->ext_mgmt_frame_handling = 0;
9186 	wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
9187 	wpa_s->ext_mgmt_frame_handling = 1;
9188 
9189 	os_free(buf);
9190 
9191 	return 0;
9192 }
9193 
9194 
wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant * wpa_s,char * param)9195 static int wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant *wpa_s,
9196 					   char *param)
9197 {
9198 	struct wpa_scan_res *res;
9199 	struct os_reltime now;
9200 	char *pos, *end;
9201 	int ret = -1;
9202 
9203 	if (!param)
9204 		return -1;
9205 
9206 	if (os_strcmp(param, "START") == 0) {
9207 		wpa_bss_update_start(wpa_s);
9208 		return 0;
9209 	}
9210 
9211 	if (os_strcmp(param, "END") == 0) {
9212 		wpa_bss_update_end(wpa_s, NULL, 1);
9213 		return 0;
9214 	}
9215 
9216 	if (os_strncmp(param, "BSS ", 4) != 0)
9217 		return -1;
9218 	param += 3;
9219 
9220 	res = os_zalloc(sizeof(*res) + os_strlen(param) / 2);
9221 	if (!res)
9222 		return -1;
9223 
9224 	pos = os_strstr(param, " flags=");
9225 	if (pos)
9226 		res->flags = strtol(pos + 7, NULL, 16);
9227 
9228 	pos = os_strstr(param, " bssid=");
9229 	if (pos && hwaddr_aton(pos + 7, res->bssid))
9230 		goto fail;
9231 
9232 	pos = os_strstr(param, " freq=");
9233 	if (pos)
9234 		res->freq = atoi(pos + 6);
9235 
9236 	pos = os_strstr(param, " beacon_int=");
9237 	if (pos)
9238 		res->beacon_int = atoi(pos + 12);
9239 
9240 	pos = os_strstr(param, " caps=");
9241 	if (pos)
9242 		res->caps = strtol(pos + 6, NULL, 16);
9243 
9244 	pos = os_strstr(param, " qual=");
9245 	if (pos)
9246 		res->qual = atoi(pos + 6);
9247 
9248 	pos = os_strstr(param, " noise=");
9249 	if (pos)
9250 		res->noise = atoi(pos + 7);
9251 
9252 	pos = os_strstr(param, " level=");
9253 	if (pos)
9254 		res->level = atoi(pos + 7);
9255 
9256 	pos = os_strstr(param, " tsf=");
9257 	if (pos)
9258 		res->tsf = strtoll(pos + 5, NULL, 16);
9259 
9260 	pos = os_strstr(param, " age=");
9261 	if (pos)
9262 		res->age = atoi(pos + 5);
9263 
9264 	pos = os_strstr(param, " est_throughput=");
9265 	if (pos)
9266 		res->est_throughput = atoi(pos + 16);
9267 
9268 	pos = os_strstr(param, " snr=");
9269 	if (pos)
9270 		res->snr = atoi(pos + 5);
9271 
9272 	pos = os_strstr(param, " parent_tsf=");
9273 	if (pos)
9274 		res->parent_tsf = strtoll(pos + 7, NULL, 16);
9275 
9276 	pos = os_strstr(param, " tsf_bssid=");
9277 	if (pos && hwaddr_aton(pos + 11, res->tsf_bssid))
9278 		goto fail;
9279 
9280 	pos = os_strstr(param, " ie=");
9281 	if (pos) {
9282 		pos += 4;
9283 		end = os_strchr(pos, ' ');
9284 		if (!end)
9285 			end = pos + os_strlen(pos);
9286 		res->ie_len = (end - pos) / 2;
9287 		if (hexstr2bin(pos, (u8 *) (res + 1), res->ie_len))
9288 			goto fail;
9289 	}
9290 
9291 	pos = os_strstr(param, " beacon_ie=");
9292 	if (pos) {
9293 		pos += 11;
9294 		end = os_strchr(pos, ' ');
9295 		if (!end)
9296 			end = pos + os_strlen(pos);
9297 		res->beacon_ie_len = (end - pos) / 2;
9298 		if (hexstr2bin(pos, ((u8 *) (res + 1)) + res->ie_len,
9299 			       res->beacon_ie_len))
9300 			goto fail;
9301 	}
9302 
9303 	os_get_reltime(&now);
9304 	wpa_bss_update_scan_res(wpa_s, res, &now);
9305 	ret = 0;
9306 fail:
9307 	os_free(res);
9308 
9309 	return ret;
9310 }
9311 
9312 
wpas_ctrl_iface_driver_event_assoc(struct wpa_supplicant * wpa_s,char * param)9313 static int wpas_ctrl_iface_driver_event_assoc(struct wpa_supplicant *wpa_s,
9314 					      char *param)
9315 {
9316 	union wpa_event_data event;
9317 	struct assoc_info *ai;
9318 	char *ctx = NULL;
9319 	int ret = -1;
9320 	struct wpabuf *req_ies = NULL;
9321 	struct wpabuf *resp_ies = NULL;
9322 	struct wpabuf *resp_frame = NULL;
9323 	struct wpabuf *beacon_ies = NULL;
9324 	struct wpabuf *key_replay_ctr = NULL;
9325 	struct wpabuf *ptk_kck = NULL;
9326 	struct wpabuf *ptk_kek = NULL;
9327 	struct wpabuf *fils_pmk = NULL;
9328 	char *str, *pos;
9329 	u8 addr[ETH_ALEN];
9330 	u8 fils_pmkid[PMKID_LEN];
9331 
9332 	os_memset(&event, 0, sizeof(event));
9333 	ai = &event.assoc_info;
9334 
9335 	while ((str = str_token(param, " ", &ctx))) {
9336 		pos = os_strchr(str, '=');
9337 		if (!pos)
9338 			goto fail;
9339 		*pos++ = '\0';
9340 
9341 		if (os_strcmp(str, "reassoc") == 0) {
9342 			ai->reassoc = atoi(pos);
9343 		} else if (os_strcmp(str, "req_ies") == 0) {
9344 			wpabuf_free(req_ies);
9345 			req_ies = wpabuf_parse_bin(pos);
9346 			if (!req_ies)
9347 				goto fail;
9348 			ai->req_ies = wpabuf_head(req_ies);
9349 			ai->req_ies_len = wpabuf_len(req_ies);
9350 		} else if (os_strcmp(str, "resp_ies") == 0) {
9351 			wpabuf_free(resp_ies);
9352 			resp_ies = wpabuf_parse_bin(pos);
9353 			if (!resp_ies)
9354 				goto fail;
9355 			ai->resp_ies = wpabuf_head(resp_ies);
9356 			ai->resp_ies_len = wpabuf_len(resp_ies);
9357 		} else if (os_strcmp(str, "resp_frame") == 0) {
9358 			wpabuf_free(resp_frame);
9359 			resp_frame = wpabuf_parse_bin(pos);
9360 			if (!resp_frame)
9361 				goto fail;
9362 			ai->resp_frame = wpabuf_head(resp_frame);
9363 			ai->resp_frame_len = wpabuf_len(resp_frame);
9364 		} else if (os_strcmp(str, "beacon_ies") == 0) {
9365 			wpabuf_free(beacon_ies);
9366 			beacon_ies = wpabuf_parse_bin(pos);
9367 			if (!beacon_ies)
9368 				goto fail;
9369 			ai->beacon_ies = wpabuf_head(beacon_ies);
9370 			ai->beacon_ies_len = wpabuf_len(beacon_ies);
9371 		} else if (os_strcmp(str, "freq") == 0) {
9372 			ai->freq = atoi(pos);
9373 		} else if (os_strcmp(str, "wmm::info_bitmap") == 0) {
9374 			ai->wmm_params.info_bitmap = atoi(pos);
9375 		} else if (os_strcmp(str, "wmm::uapsd_queues") == 0) {
9376 			ai->wmm_params.uapsd_queues = atoi(pos);
9377 		} else if (os_strcmp(str, "addr") == 0) {
9378 			if (hwaddr_aton(pos, addr))
9379 				goto fail;
9380 			ai->addr = addr;
9381 		} else if (os_strcmp(str, "authorized") == 0) {
9382 			ai->authorized = atoi(pos);
9383 		} else if (os_strcmp(str, "key_replay_ctr") == 0) {
9384 			wpabuf_free(key_replay_ctr);
9385 			key_replay_ctr = wpabuf_parse_bin(pos);
9386 			if (!key_replay_ctr)
9387 				goto fail;
9388 			ai->key_replay_ctr = wpabuf_head(key_replay_ctr);
9389 			ai->key_replay_ctr_len = wpabuf_len(key_replay_ctr);
9390 		} else if (os_strcmp(str, "ptk_kck") == 0) {
9391 			wpabuf_free(ptk_kck);
9392 			ptk_kck = wpabuf_parse_bin(pos);
9393 			if (!ptk_kck)
9394 				goto fail;
9395 			ai->ptk_kck = wpabuf_head(ptk_kck);
9396 			ai->ptk_kck_len = wpabuf_len(ptk_kck);
9397 		} else if (os_strcmp(str, "ptk_kek") == 0) {
9398 			wpabuf_free(ptk_kek);
9399 			ptk_kek = wpabuf_parse_bin(pos);
9400 			if (!ptk_kek)
9401 				goto fail;
9402 			ai->ptk_kek = wpabuf_head(ptk_kek);
9403 			ai->ptk_kek_len = wpabuf_len(ptk_kek);
9404 		} else if (os_strcmp(str, "subnet_status") == 0) {
9405 			ai->subnet_status = atoi(pos);
9406 		} else if (os_strcmp(str, "fils_erp_next_seq_num") == 0) {
9407 			ai->fils_erp_next_seq_num = atoi(pos);
9408 		} else if (os_strcmp(str, "fils_pmk") == 0) {
9409 			wpabuf_free(fils_pmk);
9410 			fils_pmk = wpabuf_parse_bin(pos);
9411 			if (!fils_pmk)
9412 				goto fail;
9413 			ai->fils_pmk = wpabuf_head(fils_pmk);
9414 			ai->fils_pmk_len = wpabuf_len(fils_pmk);
9415 		} else if (os_strcmp(str, "fils_pmkid") == 0) {
9416 			if (hexstr2bin(pos, fils_pmkid, PMKID_LEN) < 0)
9417 				goto fail;
9418 			ai->fils_pmkid = fils_pmkid;
9419 		} else {
9420 			goto fail;
9421 		}
9422 	}
9423 
9424 	wpa_supplicant_event(wpa_s, EVENT_ASSOC, &event);
9425 	ret = 0;
9426 fail:
9427 	wpabuf_free(req_ies);
9428 	wpabuf_free(resp_ies);
9429 	wpabuf_free(resp_frame);
9430 	wpabuf_free(beacon_ies);
9431 	wpabuf_free(key_replay_ctr);
9432 	wpabuf_free(ptk_kck);
9433 	wpabuf_free(ptk_kek);
9434 	wpabuf_free(fils_pmk);
9435 	return ret;
9436 }
9437 
9438 
wpas_ctrl_iface_driver_event(struct wpa_supplicant * wpa_s,char * cmd)9439 static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
9440 {
9441 	char *pos, *param;
9442 	union wpa_event_data event;
9443 	enum wpa_event_type ev;
9444 
9445 	/* <event name> [parameters..] */
9446 
9447 	wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
9448 
9449 	pos = cmd;
9450 	param = os_strchr(pos, ' ');
9451 	if (param)
9452 		*param++ = '\0';
9453 
9454 	os_memset(&event, 0, sizeof(event));
9455 
9456 	if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
9457 		ev = EVENT_INTERFACE_ENABLED;
9458 	} else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
9459 		ev = EVENT_INTERFACE_DISABLED;
9460 	} else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
9461 		ev = EVENT_AVOID_FREQUENCIES;
9462 		if (param == NULL)
9463 			param = "";
9464 		if (freq_range_list_parse(&event.freq_range, param) < 0)
9465 			return -1;
9466 		wpa_supplicant_event(wpa_s, ev, &event);
9467 		os_free(event.freq_range.range);
9468 		return 0;
9469 	} else if (os_strcmp(cmd, "SCAN_RES") == 0) {
9470 		return wpas_ctrl_iface_driver_scan_res(wpa_s, param);
9471 	} else if (os_strcmp(cmd, "ASSOC") == 0) {
9472 		return wpas_ctrl_iface_driver_event_assoc(wpa_s, param);
9473 	} else {
9474 		wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
9475 			cmd);
9476 		return -1;
9477 	}
9478 
9479 	wpa_supplicant_event(wpa_s, ev, &event);
9480 
9481 	return 0;
9482 }
9483 
9484 
wpas_ctrl_iface_eapol_rx(struct wpa_supplicant * wpa_s,char * cmd)9485 static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
9486 {
9487 	char *pos;
9488 	u8 src[ETH_ALEN], *buf;
9489 	int used;
9490 	size_t len;
9491 
9492 	wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
9493 
9494 	pos = cmd;
9495 	used = hwaddr_aton2(pos, src);
9496 	if (used < 0)
9497 		return -1;
9498 	pos += used;
9499 	while (*pos == ' ')
9500 		pos++;
9501 
9502 	len = os_strlen(pos);
9503 	if (len & 1)
9504 		return -1;
9505 	len /= 2;
9506 
9507 	buf = os_malloc(len);
9508 	if (buf == NULL)
9509 		return -1;
9510 
9511 	if (hexstr2bin(pos, buf, len) < 0) {
9512 		os_free(buf);
9513 		return -1;
9514 	}
9515 
9516 	wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
9517 	os_free(buf);
9518 
9519 	return 0;
9520 }
9521 
9522 
wpas_ctrl_iface_eapol_tx(struct wpa_supplicant * wpa_s,char * cmd)9523 static int wpas_ctrl_iface_eapol_tx(struct wpa_supplicant *wpa_s, char *cmd)
9524 {
9525 	char *pos;
9526 	u8 dst[ETH_ALEN], *buf;
9527 	int used, ret;
9528 	size_t len;
9529 	unsigned int prev;
9530 
9531 	wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
9532 
9533 	pos = cmd;
9534 	used = hwaddr_aton2(pos, dst);
9535 	if (used < 0)
9536 		return -1;
9537 	pos += used;
9538 	while (*pos == ' ')
9539 		pos++;
9540 
9541 	len = os_strlen(pos);
9542 	if (len & 1)
9543 		return -1;
9544 	len /= 2;
9545 
9546 	buf = os_malloc(len);
9547 	if (!buf || hexstr2bin(pos, buf, len) < 0) {
9548 		os_free(buf);
9549 		return -1;
9550 	}
9551 
9552 	prev = wpa_s->ext_eapol_frame_io;
9553 	wpa_s->ext_eapol_frame_io = 0;
9554 	ret = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, buf, len);
9555 	wpa_s->ext_eapol_frame_io = prev;
9556 	os_free(buf);
9557 
9558 	return ret;
9559 }
9560 
9561 
ipv4_hdr_checksum(const void * buf,size_t len)9562 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
9563 {
9564 	size_t i;
9565 	u32 sum = 0;
9566 	const u16 *pos = buf;
9567 
9568 	for (i = 0; i < len / 2; i++)
9569 		sum += *pos++;
9570 
9571 	while (sum >> 16)
9572 		sum = (sum & 0xffff) + (sum >> 16);
9573 
9574 	return sum ^ 0xffff;
9575 }
9576 
9577 
9578 #define HWSIM_PACKETLEN 1500
9579 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
9580 
wpas_data_test_rx(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)9581 static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
9582 			      size_t len)
9583 {
9584 	struct wpa_supplicant *wpa_s = ctx;
9585 	const struct ether_header *eth;
9586 	struct ip ip;
9587 	const u8 *pos;
9588 	unsigned int i;
9589 	char extra[30];
9590 
9591 	if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
9592 		wpa_printf(MSG_DEBUG,
9593 			   "test data: RX - ignore unexpected length %d",
9594 			   (int) len);
9595 		return;
9596 	}
9597 
9598 	eth = (const struct ether_header *) buf;
9599 	os_memcpy(&ip, eth + 1, sizeof(ip));
9600 	pos = &buf[sizeof(*eth) + sizeof(ip)];
9601 
9602 	if (ip.ip_hl != 5 || ip.ip_v != 4 || ntohs(ip.ip_len) > HWSIM_IP_LEN) {
9603 		wpa_printf(MSG_DEBUG,
9604 			   "test data: RX - ignore unexpected IP header");
9605 		return;
9606 	}
9607 
9608 	for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
9609 		if (*pos != (u8) i) {
9610 			wpa_printf(MSG_DEBUG,
9611 				   "test data: RX - ignore mismatching payload");
9612 			return;
9613 		}
9614 		pos++;
9615 	}
9616 	extra[0] = '\0';
9617 	if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
9618 		os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
9619 	wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
9620 		MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
9621 }
9622 
9623 
wpas_ctrl_iface_data_test_config(struct wpa_supplicant * wpa_s,char * cmd)9624 static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
9625 					    char *cmd)
9626 {
9627 	int enabled = atoi(cmd);
9628 	char *pos;
9629 	const char *ifname;
9630 
9631 	if (!enabled) {
9632 		if (wpa_s->l2_test) {
9633 			l2_packet_deinit(wpa_s->l2_test);
9634 			wpa_s->l2_test = NULL;
9635 			wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
9636 		}
9637 		return 0;
9638 	}
9639 
9640 	if (wpa_s->l2_test)
9641 		return 0;
9642 
9643 	pos = os_strstr(cmd, " ifname=");
9644 	if (pos)
9645 		ifname = pos + 8;
9646 	else
9647 		ifname = wpa_s->ifname;
9648 
9649 	wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
9650 					ETHERTYPE_IP, wpas_data_test_rx,
9651 					wpa_s, 1);
9652 	if (wpa_s->l2_test == NULL)
9653 		return -1;
9654 
9655 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
9656 
9657 	return 0;
9658 }
9659 
9660 
wpas_ctrl_iface_data_test_tx(struct wpa_supplicant * wpa_s,char * cmd)9661 static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
9662 {
9663 	u8 dst[ETH_ALEN], src[ETH_ALEN];
9664 	char *pos, *pos2;
9665 	int used;
9666 	long int val;
9667 	u8 tos;
9668 	u8 buf[2 + HWSIM_PACKETLEN];
9669 	struct ether_header *eth;
9670 	struct ip *ip;
9671 	u8 *dpos;
9672 	unsigned int i;
9673 	size_t send_len = HWSIM_IP_LEN;
9674 
9675 	if (wpa_s->l2_test == NULL)
9676 		return -1;
9677 
9678 	/* format: <dst> <src> <tos> [len=<length>] */
9679 
9680 	pos = cmd;
9681 	used = hwaddr_aton2(pos, dst);
9682 	if (used < 0)
9683 		return -1;
9684 	pos += used;
9685 	while (*pos == ' ')
9686 		pos++;
9687 	used = hwaddr_aton2(pos, src);
9688 	if (used < 0)
9689 		return -1;
9690 	pos += used;
9691 
9692 	val = strtol(pos, &pos2, 0);
9693 	if (val < 0 || val > 0xff)
9694 		return -1;
9695 	tos = val;
9696 
9697 	pos = os_strstr(pos2, " len=");
9698 	if (pos) {
9699 		i = atoi(pos + 5);
9700 		if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
9701 			return -1;
9702 		send_len = i;
9703 	}
9704 
9705 	eth = (struct ether_header *) &buf[2];
9706 	os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
9707 	os_memcpy(eth->ether_shost, src, ETH_ALEN);
9708 	eth->ether_type = htons(ETHERTYPE_IP);
9709 	ip = (struct ip *) (eth + 1);
9710 	os_memset(ip, 0, sizeof(*ip));
9711 	ip->ip_hl = 5;
9712 	ip->ip_v = 4;
9713 	ip->ip_ttl = 64;
9714 	ip->ip_tos = tos;
9715 	ip->ip_len = htons(send_len);
9716 	ip->ip_p = 1;
9717 	ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
9718 	ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
9719 	ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
9720 	dpos = (u8 *) (ip + 1);
9721 	for (i = 0; i < send_len - sizeof(*ip); i++)
9722 		*dpos++ = i;
9723 
9724 	if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
9725 			   sizeof(struct ether_header) + send_len) < 0)
9726 		return -1;
9727 
9728 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
9729 		" tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
9730 
9731 	return 0;
9732 }
9733 
9734 
wpas_ctrl_iface_data_test_frame(struct wpa_supplicant * wpa_s,char * cmd)9735 static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
9736 					   char *cmd)
9737 {
9738 	u8 *buf;
9739 	struct ether_header *eth;
9740 	struct l2_packet_data *l2 = NULL;
9741 	size_t len;
9742 	u16 ethertype;
9743 	int res = -1;
9744 
9745 	len = os_strlen(cmd);
9746 	if (len & 1 || len < ETH_HLEN * 2)
9747 		return -1;
9748 	len /= 2;
9749 
9750 	buf = os_malloc(len);
9751 	if (buf == NULL)
9752 		return -1;
9753 
9754 	if (hexstr2bin(cmd, buf, len) < 0)
9755 		goto done;
9756 
9757 	eth = (struct ether_header *) buf;
9758 	ethertype = ntohs(eth->ether_type);
9759 
9760 	l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
9761 			    wpas_data_test_rx, wpa_s, 1);
9762 	if (l2 == NULL)
9763 		goto done;
9764 
9765 	res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
9766 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
9767 done:
9768 	if (l2)
9769 		l2_packet_deinit(l2);
9770 	os_free(buf);
9771 
9772 	return res < 0 ? -1 : 0;
9773 }
9774 
9775 
wpas_ctrl_test_alloc_fail(struct wpa_supplicant * wpa_s,char * cmd)9776 static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
9777 {
9778 #ifdef WPA_TRACE_BFD
9779 	char *pos;
9780 
9781 	wpa_trace_fail_after = atoi(cmd);
9782 	pos = os_strchr(cmd, ':');
9783 	if (pos) {
9784 		pos++;
9785 		os_strlcpy(wpa_trace_fail_func, pos,
9786 			   sizeof(wpa_trace_fail_func));
9787 	} else {
9788 		wpa_trace_fail_after = 0;
9789 	}
9790 	return 0;
9791 #else /* WPA_TRACE_BFD */
9792 	return -1;
9793 #endif /* WPA_TRACE_BFD */
9794 }
9795 
9796 
wpas_ctrl_get_alloc_fail(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)9797 static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
9798 				    char *buf, size_t buflen)
9799 {
9800 #ifdef WPA_TRACE_BFD
9801 	return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
9802 			   wpa_trace_fail_func);
9803 #else /* WPA_TRACE_BFD */
9804 	return -1;
9805 #endif /* WPA_TRACE_BFD */
9806 }
9807 
9808 
wpas_ctrl_test_fail(struct wpa_supplicant * wpa_s,char * cmd)9809 static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd)
9810 {
9811 #ifdef WPA_TRACE_BFD
9812 	char *pos;
9813 
9814 	wpa_trace_test_fail_after = atoi(cmd);
9815 	pos = os_strchr(cmd, ':');
9816 	if (pos) {
9817 		pos++;
9818 		os_strlcpy(wpa_trace_test_fail_func, pos,
9819 			   sizeof(wpa_trace_test_fail_func));
9820 	} else {
9821 		wpa_trace_test_fail_after = 0;
9822 	}
9823 	return 0;
9824 #else /* WPA_TRACE_BFD */
9825 	return -1;
9826 #endif /* WPA_TRACE_BFD */
9827 }
9828 
9829 
wpas_ctrl_get_fail(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)9830 static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s,
9831 				    char *buf, size_t buflen)
9832 {
9833 #ifdef WPA_TRACE_BFD
9834 	return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
9835 			   wpa_trace_test_fail_func);
9836 #else /* WPA_TRACE_BFD */
9837 	return -1;
9838 #endif /* WPA_TRACE_BFD */
9839 }
9840 
9841 
wpas_ctrl_event_test_cb(void * eloop_ctx,void * timeout_ctx)9842 static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
9843 {
9844 	struct wpa_supplicant *wpa_s = eloop_ctx;
9845 	int i, count = (intptr_t) timeout_ctx;
9846 
9847 	wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
9848 		   count);
9849 	for (i = 0; i < count; i++) {
9850 		wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
9851 			     i + 1, count);
9852 	}
9853 }
9854 
9855 
wpas_ctrl_event_test(struct wpa_supplicant * wpa_s,const char * cmd)9856 static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
9857 {
9858 	int count;
9859 
9860 	count = atoi(cmd);
9861 	if (count <= 0)
9862 		return -1;
9863 
9864 	return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
9865 				      (void *) (intptr_t) count);
9866 }
9867 
9868 
wpas_ctrl_test_assoc_ie(struct wpa_supplicant * wpa_s,const char * cmd)9869 static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
9870 				   const char *cmd)
9871 {
9872 	struct wpabuf *buf;
9873 	size_t len;
9874 
9875 	len = os_strlen(cmd);
9876 	if (len & 1)
9877 		return -1;
9878 	len /= 2;
9879 
9880 	if (len == 0) {
9881 		buf = NULL;
9882 	} else {
9883 		buf = wpabuf_alloc(len);
9884 		if (buf == NULL)
9885 			return -1;
9886 
9887 		if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
9888 			wpabuf_free(buf);
9889 			return -1;
9890 		}
9891 	}
9892 
9893 	wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
9894 	return 0;
9895 }
9896 
9897 
wpas_ctrl_reset_pn(struct wpa_supplicant * wpa_s)9898 static int wpas_ctrl_reset_pn(struct wpa_supplicant *wpa_s)
9899 {
9900 	u8 zero[WPA_TK_MAX_LEN];
9901 
9902 	if (wpa_s->last_tk_alg == WPA_ALG_NONE)
9903 		return -1;
9904 
9905 	wpa_printf(MSG_INFO, "TESTING: Reset PN");
9906 	os_memset(zero, 0, sizeof(zero));
9907 
9908 	/* First, use a zero key to avoid any possible duplicate key avoidance
9909 	 * in the driver. */
9910 	if (wpa_drv_set_key(wpa_s, wpa_s->last_tk_alg, wpa_s->last_tk_addr,
9911 			    wpa_s->last_tk_key_idx, 1, zero, 6,
9912 			    zero, wpa_s->last_tk_len,
9913 			    KEY_FLAG_PAIRWISE_RX_TX) < 0)
9914 		return -1;
9915 
9916 	/* Set the previously configured key to reset its TSC/RSC */
9917 	return wpa_drv_set_key(wpa_s, wpa_s->last_tk_alg, wpa_s->last_tk_addr,
9918 			       wpa_s->last_tk_key_idx, 1, zero, 6,
9919 			       wpa_s->last_tk, wpa_s->last_tk_len,
9920 			       KEY_FLAG_PAIRWISE_RX_TX);
9921 }
9922 
9923 
wpas_ctrl_key_request(struct wpa_supplicant * wpa_s,const char * cmd)9924 static int wpas_ctrl_key_request(struct wpa_supplicant *wpa_s, const char *cmd)
9925 {
9926 	const char *pos = cmd;
9927 	int error, pairwise;
9928 
9929 	error = atoi(pos);
9930 	pos = os_strchr(pos, ' ');
9931 	if (!pos)
9932 		return -1;
9933 	pairwise = atoi(pos);
9934 	wpa_sm_key_request(wpa_s->wpa, error, pairwise);
9935 	return 0;
9936 }
9937 
9938 
wpas_ctrl_resend_assoc(struct wpa_supplicant * wpa_s)9939 static int wpas_ctrl_resend_assoc(struct wpa_supplicant *wpa_s)
9940 {
9941 #ifdef CONFIG_SME
9942 	struct wpa_driver_associate_params params;
9943 	int ret;
9944 
9945 	os_memset(&params, 0, sizeof(params));
9946 	params.bssid = wpa_s->bssid;
9947 	params.ssid = wpa_s->sme.ssid;
9948 	params.ssid_len = wpa_s->sme.ssid_len;
9949 	params.freq.freq = wpa_s->sme.freq;
9950 	if (wpa_s->last_assoc_req_wpa_ie) {
9951 		params.wpa_ie = wpabuf_head(wpa_s->last_assoc_req_wpa_ie);
9952 		params.wpa_ie_len = wpabuf_len(wpa_s->last_assoc_req_wpa_ie);
9953 	}
9954 	params.pairwise_suite = wpa_s->pairwise_cipher;
9955 	params.group_suite = wpa_s->group_cipher;
9956 	params.mgmt_group_suite = wpa_s->mgmt_group_cipher;
9957 	params.key_mgmt_suite = wpa_s->key_mgmt;
9958 	params.wpa_proto = wpa_s->wpa_proto;
9959 	params.mgmt_frame_protection = wpa_s->sme.mfp;
9960 	params.rrm_used = wpa_s->rrm.rrm_used;
9961 	if (wpa_s->sme.prev_bssid_set)
9962 		params.prev_bssid = wpa_s->sme.prev_bssid;
9963 	wpa_printf(MSG_INFO, "TESTING: Resend association request");
9964 	ret = wpa_drv_associate(wpa_s, &params);
9965 	wpa_s->testing_resend_assoc = 1;
9966 	return ret;
9967 #else /* CONFIG_SME */
9968 	return -1;
9969 #endif /* CONFIG_SME */
9970 }
9971 
9972 
wpas_ctrl_iface_send_twt_setup(struct wpa_supplicant * wpa_s,const char * cmd)9973 static int wpas_ctrl_iface_send_twt_setup(struct wpa_supplicant *wpa_s,
9974 					  const char *cmd)
9975 {
9976 	u8 dtok = 1;
9977 	int exponent = 10;
9978 	int mantissa = 8192;
9979 	u8 min_twt = 255;
9980 	unsigned long long twt = 0;
9981 	bool requestor = true;
9982 	int setup_cmd = 0;
9983 	bool trigger = true;
9984 	bool implicit = true;
9985 	bool flow_type = true;
9986 	int flow_id = 0;
9987 	bool protection = false;
9988 	u8 twt_channel = 0;
9989 	u8 control = BIT(4); /* Control field (IEEE P802.11ax/D8.0 Figure
9990 			      * 9-687): B4 = TWT Information Frame Disabled */
9991 	const char *tok_s;
9992 
9993 	tok_s = os_strstr(cmd, " dialog=");
9994 	if (tok_s)
9995 		dtok = atoi(tok_s + os_strlen(" dialog="));
9996 
9997 	tok_s = os_strstr(cmd, " exponent=");
9998 	if (tok_s)
9999 		exponent = atoi(tok_s + os_strlen(" exponent="));
10000 
10001 	tok_s = os_strstr(cmd, " mantissa=");
10002 	if (tok_s)
10003 		mantissa = atoi(tok_s + os_strlen(" mantissa="));
10004 
10005 	tok_s = os_strstr(cmd, " min_twt=");
10006 	if (tok_s)
10007 		min_twt = atoi(tok_s + os_strlen(" min_twt="));
10008 
10009 	tok_s = os_strstr(cmd, " setup_cmd=");
10010 	if (tok_s)
10011 		setup_cmd = atoi(tok_s + os_strlen(" setup_cmd="));
10012 
10013 	tok_s = os_strstr(cmd, " twt=");
10014 	if (tok_s)
10015 		sscanf(tok_s + os_strlen(" twt="), "%llu", &twt);
10016 
10017 	tok_s = os_strstr(cmd, " requestor=");
10018 	if (tok_s)
10019 		requestor = atoi(tok_s + os_strlen(" requestor="));
10020 
10021 	tok_s = os_strstr(cmd, " trigger=");
10022 	if (tok_s)
10023 		trigger = atoi(tok_s + os_strlen(" trigger="));
10024 
10025 	tok_s = os_strstr(cmd, " implicit=");
10026 	if (tok_s)
10027 		implicit = atoi(tok_s + os_strlen(" implicit="));
10028 
10029 	tok_s = os_strstr(cmd, " flow_type=");
10030 	if (tok_s)
10031 		flow_type = atoi(tok_s + os_strlen(" flow_type="));
10032 
10033 	tok_s = os_strstr(cmd, " flow_id=");
10034 	if (tok_s)
10035 		flow_id = atoi(tok_s + os_strlen(" flow_id="));
10036 
10037 	tok_s = os_strstr(cmd, " protection=");
10038 	if (tok_s)
10039 		protection = atoi(tok_s + os_strlen(" protection="));
10040 
10041 	tok_s = os_strstr(cmd, " twt_channel=");
10042 	if (tok_s)
10043 		twt_channel = atoi(tok_s + os_strlen(" twt_channel="));
10044 
10045 	tok_s = os_strstr(cmd, " control=");
10046 	if (tok_s)
10047 		control = atoi(tok_s + os_strlen(" control="));
10048 
10049 	return wpas_twt_send_setup(wpa_s, dtok, exponent, mantissa, min_twt,
10050 				   setup_cmd, twt, requestor, trigger, implicit,
10051 				   flow_type, flow_id, protection, twt_channel,
10052 				   control);
10053 }
10054 
10055 
wpas_ctrl_iface_send_twt_teardown(struct wpa_supplicant * wpa_s,const char * cmd)10056 static int wpas_ctrl_iface_send_twt_teardown(struct wpa_supplicant *wpa_s,
10057 					     const char *cmd)
10058 {
10059 	u8 flags = 0x1;
10060 	const char *tok_s;
10061 
10062 	tok_s = os_strstr(cmd, " flags=");
10063 	if (tok_s)
10064 		flags = atoi(tok_s + os_strlen(" flags="));
10065 
10066 	return wpas_twt_send_teardown(wpa_s, flags);
10067 }
10068 
10069 #endif /* CONFIG_TESTING_OPTIONS */
10070 
10071 
wpas_ctrl_vendor_elem_add(struct wpa_supplicant * wpa_s,char * cmd)10072 static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
10073 {
10074 	char *pos = cmd;
10075 	int frame;
10076 	size_t len;
10077 	struct wpabuf *buf;
10078 	struct ieee802_11_elems elems;
10079 
10080 	frame = atoi(pos);
10081 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
10082 		return -1;
10083 	wpa_s = wpas_vendor_elem(wpa_s, frame);
10084 
10085 	pos = os_strchr(pos, ' ');
10086 	if (pos == NULL)
10087 		return -1;
10088 	pos++;
10089 
10090 	len = os_strlen(pos);
10091 	if (len == 0)
10092 		return 0;
10093 	if (len & 1)
10094 		return -1;
10095 	len /= 2;
10096 
10097 	buf = wpabuf_alloc(len);
10098 	if (buf == NULL)
10099 		return -1;
10100 
10101 	if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
10102 		wpabuf_free(buf);
10103 		return -1;
10104 	}
10105 
10106 	if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
10107 	    ParseFailed) {
10108 		wpabuf_free(buf);
10109 		return -1;
10110 	}
10111 
10112 	if (wpa_s->vendor_elem[frame] == NULL) {
10113 		wpa_s->vendor_elem[frame] = buf;
10114 		goto update_ies;
10115 	}
10116 
10117 	if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
10118 		wpabuf_free(buf);
10119 		return -1;
10120 	}
10121 
10122 	wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
10123 	wpabuf_free(buf);
10124 
10125 update_ies:
10126 	wpas_vendor_elem_update(wpa_s);
10127 
10128 	if (frame == VENDOR_ELEM_PROBE_REQ ||
10129 	    frame == VENDOR_ELEM_PROBE_REQ_P2P)
10130 		wpa_supplicant_set_default_scan_ies(wpa_s);
10131 
10132 	return 0;
10133 }
10134 
10135 
wpas_ctrl_vendor_elem_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)10136 static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
10137 				     char *buf, size_t buflen)
10138 {
10139 	int frame = atoi(cmd);
10140 
10141 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
10142 		return -1;
10143 	wpa_s = wpas_vendor_elem(wpa_s, frame);
10144 
10145 	if (wpa_s->vendor_elem[frame] == NULL)
10146 		return 0;
10147 
10148 	return wpa_snprintf_hex(buf, buflen,
10149 				wpabuf_head_u8(wpa_s->vendor_elem[frame]),
10150 				wpabuf_len(wpa_s->vendor_elem[frame]));
10151 }
10152 
10153 
wpas_ctrl_vendor_elem_remove(struct wpa_supplicant * wpa_s,char * cmd)10154 static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
10155 {
10156 	char *pos = cmd;
10157 	int frame;
10158 	size_t len;
10159 	u8 *buf;
10160 	struct ieee802_11_elems elems;
10161 	int res;
10162 
10163 	frame = atoi(pos);
10164 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
10165 		return -1;
10166 	wpa_s = wpas_vendor_elem(wpa_s, frame);
10167 
10168 	pos = os_strchr(pos, ' ');
10169 	if (pos == NULL)
10170 		return -1;
10171 	pos++;
10172 
10173 	if (*pos == '*') {
10174 		wpabuf_free(wpa_s->vendor_elem[frame]);
10175 		wpa_s->vendor_elem[frame] = NULL;
10176 		wpas_vendor_elem_update(wpa_s);
10177 		return 0;
10178 	}
10179 
10180 	if (wpa_s->vendor_elem[frame] == NULL)
10181 		return -1;
10182 
10183 	len = os_strlen(pos);
10184 	if (len == 0)
10185 		return 0;
10186 	if (len & 1)
10187 		return -1;
10188 	len /= 2;
10189 
10190 	buf = os_malloc(len);
10191 	if (buf == NULL)
10192 		return -1;
10193 
10194 	if (hexstr2bin(pos, buf, len) < 0) {
10195 		os_free(buf);
10196 		return -1;
10197 	}
10198 
10199 	if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
10200 		os_free(buf);
10201 		return -1;
10202 	}
10203 
10204 	res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
10205 	os_free(buf);
10206 	return res;
10207 }
10208 
10209 
wpas_ctrl_neighbor_rep_cb(void * ctx,struct wpabuf * neighbor_rep)10210 static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
10211 {
10212 	struct wpa_supplicant *wpa_s = ctx;
10213 	size_t len;
10214 	const u8 *data;
10215 
10216 	/*
10217 	 * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
10218 	 * BSSID[6]
10219 	 * BSSID Information[4]
10220 	 * Operating Class[1]
10221 	 * Channel Number[1]
10222 	 * PHY Type[1]
10223 	 * Optional Subelements[variable]
10224 	 */
10225 #define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
10226 
10227 	if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
10228 		wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
10229 		goto out;
10230 	}
10231 
10232 	data = wpabuf_head_u8(neighbor_rep);
10233 	len = wpabuf_len(neighbor_rep);
10234 
10235 	while (len >= 2 + NR_IE_MIN_LEN) {
10236 		const u8 *nr;
10237 		char lci[256 * 2 + 1];
10238 		char civic[256 * 2 + 1];
10239 		u8 nr_len = data[1];
10240 		const u8 *pos = data, *end;
10241 
10242 		if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
10243 		    nr_len < NR_IE_MIN_LEN) {
10244 			wpa_dbg(wpa_s, MSG_DEBUG,
10245 				"CTRL: Invalid Neighbor Report element: id=%u len=%u",
10246 				data[0], nr_len);
10247 			goto out;
10248 		}
10249 
10250 		if (2U + nr_len > len) {
10251 			wpa_dbg(wpa_s, MSG_DEBUG,
10252 				"CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
10253 				data[0], len, nr_len);
10254 			goto out;
10255 		}
10256 		pos += 2;
10257 		end = pos + nr_len;
10258 
10259 		nr = pos;
10260 		pos += NR_IE_MIN_LEN;
10261 
10262 		lci[0] = '\0';
10263 		civic[0] = '\0';
10264 		while (end - pos > 2) {
10265 			u8 s_id, s_len;
10266 
10267 			s_id = *pos++;
10268 			s_len = *pos++;
10269 			if (s_len > end - pos)
10270 				goto out;
10271 			if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
10272 				/* Measurement Token[1] */
10273 				/* Measurement Report Mode[1] */
10274 				/* Measurement Type[1] */
10275 				/* Measurement Report[variable] */
10276 				switch (pos[2]) {
10277 				case MEASURE_TYPE_LCI:
10278 					if (lci[0])
10279 						break;
10280 					wpa_snprintf_hex(lci, sizeof(lci),
10281 							 pos, s_len);
10282 					break;
10283 				case MEASURE_TYPE_LOCATION_CIVIC:
10284 					if (civic[0])
10285 						break;
10286 					wpa_snprintf_hex(civic, sizeof(civic),
10287 							 pos, s_len);
10288 					break;
10289 				}
10290 			}
10291 
10292 			pos += s_len;
10293 		}
10294 
10295 		wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
10296 			"bssid=" MACSTR
10297 			" info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
10298 			MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
10299 			nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
10300 			nr[ETH_ALEN + 6],
10301 			lci[0] ? " lci=" : "", lci,
10302 			civic[0] ? " civic=" : "", civic);
10303 
10304 		data = end;
10305 		len -= 2 + nr_len;
10306 	}
10307 
10308 out:
10309 	wpabuf_free(neighbor_rep);
10310 }
10311 
10312 
wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant * wpa_s,char * cmd)10313 static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
10314 					     char *cmd)
10315 {
10316 	struct wpa_ssid_value ssid, *ssid_p = NULL;
10317 	int ret, lci = 0, civic = 0;
10318 	char *ssid_s;
10319 
10320 	ssid_s = os_strstr(cmd, "ssid=");
10321 	if (ssid_s) {
10322 		if (ssid_parse(ssid_s + 5, &ssid)) {
10323 			wpa_msg(wpa_s, MSG_INFO,
10324 				"CTRL: Send Neighbor Report: bad SSID");
10325 			return -1;
10326 		}
10327 
10328 		ssid_p = &ssid;
10329 
10330 		/*
10331 		 * Move cmd after the SSID text that may include "lci" or
10332 		 * "civic".
10333 		 */
10334 		cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
10335 		if (cmd)
10336 			cmd++;
10337 
10338 	}
10339 
10340 	if (cmd && os_strstr(cmd, "lci"))
10341 		lci = 1;
10342 
10343 	if (cmd && os_strstr(cmd, "civic"))
10344 		civic = 1;
10345 
10346 	ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
10347 						 wpas_ctrl_neighbor_rep_cb,
10348 						 wpa_s);
10349 
10350 	return ret;
10351 }
10352 
10353 
wpas_ctrl_iface_erp_flush(struct wpa_supplicant * wpa_s)10354 static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
10355 {
10356 	eapol_sm_erp_flush(wpa_s->eapol);
10357 	return 0;
10358 }
10359 
10360 
wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant * wpa_s,char * cmd)10361 static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
10362 					 char *cmd)
10363 {
10364 	char *token, *context = NULL;
10365 	unsigned int enable = ~0, type = 0;
10366 	u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
10367 	u8 *addr = NULL, *mask = NULL;
10368 
10369 	while ((token = str_token(cmd, " ", &context))) {
10370 		if (os_strcasecmp(token, "scan") == 0) {
10371 			type |= MAC_ADDR_RAND_SCAN;
10372 		} else if (os_strcasecmp(token, "sched") == 0) {
10373 			type |= MAC_ADDR_RAND_SCHED_SCAN;
10374 		} else if (os_strcasecmp(token, "pno") == 0) {
10375 			type |= MAC_ADDR_RAND_PNO;
10376 		} else if (os_strcasecmp(token, "all") == 0) {
10377 			type = wpa_s->mac_addr_rand_supported;
10378 		} else if (os_strncasecmp(token, "enable=", 7) == 0) {
10379 			enable = atoi(token + 7);
10380 		} else if (os_strncasecmp(token, "addr=", 5) == 0) {
10381 			addr = _addr;
10382 			if (hwaddr_aton(token + 5, addr)) {
10383 				wpa_printf(MSG_INFO,
10384 					   "CTRL: Invalid MAC address: %s",
10385 					   token);
10386 				return -1;
10387 			}
10388 		} else if (os_strncasecmp(token, "mask=", 5) == 0) {
10389 			mask = _mask;
10390 			if (hwaddr_aton(token + 5, mask)) {
10391 				wpa_printf(MSG_INFO,
10392 					   "CTRL: Invalid MAC address mask: %s",
10393 					   token);
10394 				return -1;
10395 			}
10396 		} else {
10397 			wpa_printf(MSG_INFO,
10398 				   "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
10399 				   token);
10400 			return -1;
10401 		}
10402 	}
10403 
10404 	if (!type) {
10405 		wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
10406 		return -1;
10407 	}
10408 
10409 	if (enable > 1) {
10410 		wpa_printf(MSG_INFO,
10411 			   "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
10412 		return -1;
10413 	}
10414 
10415 	if (!enable)
10416 		return wpas_disable_mac_addr_randomization(wpa_s, type);
10417 
10418 	return wpas_enable_mac_addr_randomization(wpa_s, type, addr, mask);
10419 }
10420 
10421 
wpas_ctrl_iface_pmksa(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)10422 static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
10423 				 char *buf, size_t buflen)
10424 {
10425 	size_t reply_len;
10426 
10427 	reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
10428 #ifdef CONFIG_AP
10429 	reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
10430 					      buflen - reply_len);
10431 #endif /* CONFIG_AP */
10432 	return reply_len;
10433 }
10434 
10435 
wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant * wpa_s)10436 static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
10437 {
10438 	ptksa_cache_flush(wpa_s->ptksa, NULL, WPA_CIPHER_NONE);
10439 	wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
10440 #ifdef CONFIG_AP
10441 	wpas_ap_pmksa_cache_flush(wpa_s);
10442 #endif /* CONFIG_AP */
10443 }
10444 
10445 
10446 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
10447 
wpas_ctrl_iface_pmksa_get(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)10448 static int wpas_ctrl_iface_pmksa_get(struct wpa_supplicant *wpa_s,
10449 				     const char *cmd, char *buf, size_t buflen)
10450 {
10451 	struct rsn_pmksa_cache_entry *entry;
10452 	struct wpa_ssid *ssid;
10453 	char *pos, *pos2, *end;
10454 	int ret;
10455 	struct os_reltime now;
10456 
10457 	ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
10458 	if (!ssid)
10459 		return -1;
10460 
10461 	pos = buf;
10462 	end = buf + buflen;
10463 
10464 	os_get_reltime(&now);
10465 
10466 	/*
10467 	 * Entry format:
10468 	 * <BSSID> <PMKID> <PMK> <reauth_time in seconds>
10469 	 * <expiration in seconds> <akmp> <opportunistic>
10470 	 * [FILS Cache Identifier]
10471 	 */
10472 
10473 	for (entry = wpa_sm_pmksa_cache_head(wpa_s->wpa); entry;
10474 	     entry = entry->next) {
10475 		if (entry->network_ctx != ssid)
10476 			continue;
10477 
10478 		pos2 = pos;
10479 		ret = os_snprintf(pos2, end - pos2, MACSTR " ",
10480 				  MAC2STR(entry->aa));
10481 		if (os_snprintf_error(end - pos2, ret))
10482 			break;
10483 		pos2 += ret;
10484 
10485 		pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmkid,
10486 					 PMKID_LEN);
10487 
10488 		ret = os_snprintf(pos2, end - pos2, " ");
10489 		if (os_snprintf_error(end - pos2, ret))
10490 			break;
10491 		pos2 += ret;
10492 
10493 		pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmk,
10494 					 entry->pmk_len);
10495 
10496 		ret = os_snprintf(pos2, end - pos2, " %d %d %d %d",
10497 				  (int) (entry->reauth_time - now.sec),
10498 				  (int) (entry->expiration - now.sec),
10499 				  entry->akmp,
10500 				  entry->opportunistic);
10501 		if (os_snprintf_error(end - pos2, ret))
10502 			break;
10503 		pos2 += ret;
10504 
10505 		if (entry->fils_cache_id_set) {
10506 			ret = os_snprintf(pos2, end - pos2, " %02x%02x",
10507 					  entry->fils_cache_id[0],
10508 					  entry->fils_cache_id[1]);
10509 			if (os_snprintf_error(end - pos2, ret))
10510 				break;
10511 			pos2 += ret;
10512 		}
10513 
10514 		ret = os_snprintf(pos2, end - pos2, "\n");
10515 		if (os_snprintf_error(end - pos2, ret))
10516 			break;
10517 		pos2 += ret;
10518 
10519 		pos = pos2;
10520 	}
10521 
10522 	return pos - buf;
10523 }
10524 
10525 
wpas_ctrl_iface_pmksa_add(struct wpa_supplicant * wpa_s,char * cmd)10526 static int wpas_ctrl_iface_pmksa_add(struct wpa_supplicant *wpa_s,
10527 				     char *cmd)
10528 {
10529 	struct rsn_pmksa_cache_entry *entry;
10530 	struct wpa_ssid *ssid;
10531 	char *pos, *pos2;
10532 	int ret = -1;
10533 	struct os_reltime now;
10534 	int reauth_time = 0, expiration = 0, i;
10535 
10536 	/*
10537 	 * Entry format:
10538 	 * <network_id> <BSSID> <PMKID> <PMK> <reauth_time in seconds>
10539 	 * <expiration in seconds> <akmp> <opportunistic>
10540 	 * [FILS Cache Identifier]
10541 	 */
10542 
10543 	ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
10544 	if (!ssid)
10545 		return -1;
10546 
10547 	pos = os_strchr(cmd, ' ');
10548 	if (!pos)
10549 		return -1;
10550 	pos++;
10551 
10552 	entry = os_zalloc(sizeof(*entry));
10553 	if (!entry)
10554 		return -1;
10555 
10556 	if (hwaddr_aton(pos, entry->aa))
10557 		goto fail;
10558 
10559 	pos = os_strchr(pos, ' ');
10560 	if (!pos)
10561 		goto fail;
10562 	pos++;
10563 
10564 	if (hexstr2bin(pos, entry->pmkid, PMKID_LEN) < 0)
10565 		goto fail;
10566 
10567 	pos = os_strchr(pos, ' ');
10568 	if (!pos)
10569 		goto fail;
10570 	pos++;
10571 
10572 	pos2 = os_strchr(pos, ' ');
10573 	if (!pos2)
10574 		goto fail;
10575 	entry->pmk_len = (pos2 - pos) / 2;
10576 	if (entry->pmk_len < PMK_LEN || entry->pmk_len > PMK_LEN_MAX ||
10577 	    hexstr2bin(pos, entry->pmk, entry->pmk_len) < 0)
10578 		goto fail;
10579 
10580 	pos = os_strchr(pos, ' ');
10581 	if (!pos)
10582 		goto fail;
10583 	pos++;
10584 
10585 	if (sscanf(pos, "%d %d %d %d", &reauth_time, &expiration,
10586 		   &entry->akmp, &entry->opportunistic) != 4)
10587 		goto fail;
10588 	if (reauth_time > expiration)
10589 		goto fail;
10590 	for (i = 0; i < 4; i++) {
10591 		pos = os_strchr(pos, ' ');
10592 		if (!pos) {
10593 			if (i < 3)
10594 				goto fail;
10595 			break;
10596 		}
10597 		pos++;
10598 	}
10599 	if (pos) {
10600 		if (hexstr2bin(pos, entry->fils_cache_id,
10601 			       FILS_CACHE_ID_LEN) < 0)
10602 			goto fail;
10603 		entry->fils_cache_id_set = 1;
10604 	}
10605 	os_get_reltime(&now);
10606 	entry->expiration = now.sec + expiration;
10607 	entry->reauth_time = now.sec + reauth_time;
10608 
10609 	entry->network_ctx = ssid;
10610 
10611 	entry->external = true;
10612 
10613 	wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
10614 	entry = NULL;
10615 	ret = 0;
10616 fail:
10617 	os_free(entry);
10618 	return ret;
10619 }
10620 
10621 
10622 #ifdef CONFIG_MESH
10623 
wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)10624 static int wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant *wpa_s,
10625 					  const char *cmd, char *buf,
10626 					  size_t buflen)
10627 {
10628 	u8 spa[ETH_ALEN];
10629 
10630 	if (!wpa_s->ifmsh)
10631 		return -1;
10632 
10633 	if (os_strcasecmp(cmd, "any") == 0)
10634 		return wpas_ap_pmksa_cache_list_mesh(wpa_s, NULL, buf, buflen);
10635 
10636 	if (hwaddr_aton(cmd, spa))
10637 		return -1;
10638 
10639 	return wpas_ap_pmksa_cache_list_mesh(wpa_s, spa, buf, buflen);
10640 }
10641 
10642 
wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant * wpa_s,char * cmd)10643 static int wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant *wpa_s,
10644 					  char *cmd)
10645 {
10646 	/*
10647 	 * We do not check mesh interface existence because PMKSA should be
10648 	 * stored before wpa_s->ifmsh creation to suppress commit message
10649 	 * creation.
10650 	 */
10651 	return wpas_ap_pmksa_cache_add_external(wpa_s, cmd);
10652 }
10653 
10654 #endif /* CONFIG_MESH */
10655 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
10656 
10657 
10658 #ifdef CONFIG_FILS
wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant * wpa_s,const char * cmd)10659 static int wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant *wpa_s,
10660 					    const char *cmd)
10661 {
10662 	struct fils_hlp_req *req;
10663 	const char *pos;
10664 
10665 	/* format: <dst> <packet starting from ethertype> */
10666 
10667 	req = os_zalloc(sizeof(*req));
10668 	if (!req)
10669 		return -1;
10670 
10671 	if (hwaddr_aton(cmd, req->dst))
10672 		goto fail;
10673 
10674 	pos = os_strchr(cmd, ' ');
10675 	if (!pos)
10676 		goto fail;
10677 	pos++;
10678 	req->pkt = wpabuf_parse_bin(pos);
10679 	if (!req->pkt)
10680 		goto fail;
10681 
10682 	dl_list_add_tail(&wpa_s->fils_hlp_req, &req->list);
10683 	return 0;
10684 fail:
10685 	wpabuf_free(req->pkt);
10686 	os_free(req);
10687 	return -1;
10688 }
10689 #endif /* CONFIG_FILS */
10690 
10691 
wpas_ctrl_cmd_debug_level(const char * cmd)10692 static int wpas_ctrl_cmd_debug_level(const char *cmd)
10693 {
10694 	if (os_strcmp(cmd, "PING") == 0 ||
10695 	    os_strncmp(cmd, "BSS ", 4) == 0 ||
10696 	    os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
10697 	    os_strncmp(cmd, "STATUS", 6) == 0 ||
10698 	    os_strncmp(cmd, "STA ", 4) == 0 ||
10699 	    os_strncmp(cmd, "STA-", 4) == 0)
10700 		return MSG_EXCESSIVE;
10701 	return MSG_DEBUG;
10702 }
10703 
10704 
wpas_ctrl_iface_configure_mscs(struct wpa_supplicant * wpa_s,const char * cmd)10705 static int wpas_ctrl_iface_configure_mscs(struct wpa_supplicant *wpa_s,
10706 					  const char *cmd)
10707 {
10708 	size_t frame_classifier_len;
10709 	const char *pos, *end;
10710 	struct robust_av_data *robust_av = &wpa_s->robust_av;
10711 	int val;
10712 
10713 	/*
10714 	 * format:
10715 	 * <add|remove|change> [up_bitmap=<hex byte>] [up_limit=<integer>]
10716 	 * [stream_timeout=<in TUs>] [frame_classifier=<hex bytes>]
10717 	 */
10718 	os_memset(robust_av, 0, sizeof(struct robust_av_data));
10719 	if (os_strncmp(cmd, "add ", 4) == 0) {
10720 		robust_av->request_type = SCS_REQ_ADD;
10721 	} else if (os_strcmp(cmd, "remove") == 0) {
10722 		robust_av->request_type = SCS_REQ_REMOVE;
10723 		robust_av->valid_config = false;
10724 		return wpas_send_mscs_req(wpa_s);
10725 	} else if (os_strncmp(cmd, "change ", 7) == 0) {
10726 		robust_av->request_type = SCS_REQ_CHANGE;
10727 	} else {
10728 		return -1;
10729 	}
10730 
10731 	pos = os_strstr(cmd, "up_bitmap=");
10732 	if (!pos)
10733 		return -1;
10734 
10735 	val = hex2byte(pos + 10);
10736 	if (val < 0)
10737 		return -1;
10738 	robust_av->up_bitmap = val;
10739 
10740 	pos = os_strstr(cmd, "up_limit=");
10741 	if (!pos)
10742 		return -1;
10743 
10744 	robust_av->up_limit = atoi(pos + 9);
10745 
10746 	pos = os_strstr(cmd, "stream_timeout=");
10747 	if (!pos)
10748 		return -1;
10749 
10750 	robust_av->stream_timeout = atoi(pos + 15);
10751 	if (robust_av->stream_timeout == 0)
10752 		return -1;
10753 
10754 	pos = os_strstr(cmd, "frame_classifier=");
10755 	if (!pos)
10756 		return -1;
10757 
10758 	pos += 17;
10759 	end = os_strchr(pos, ' ');
10760 	if (!end)
10761 		end = pos + os_strlen(pos);
10762 
10763 	frame_classifier_len = (end - pos) / 2;
10764 	if (frame_classifier_len > sizeof(robust_av->frame_classifier) ||
10765 	    hexstr2bin(pos, robust_av->frame_classifier, frame_classifier_len))
10766 		return -1;
10767 
10768 	robust_av->frame_classifier_len = frame_classifier_len;
10769 	robust_av->valid_config = true;
10770 
10771 	return wpas_send_mscs_req(wpa_s);
10772 }
10773 
10774 
10775 #ifdef CONFIG_PASN
wpas_ctrl_iface_pasn_start(struct wpa_supplicant * wpa_s,char * cmd)10776 static int wpas_ctrl_iface_pasn_start(struct wpa_supplicant *wpa_s, char *cmd)
10777 {
10778 	char *token, *context = NULL;
10779 	u8 bssid[ETH_ALEN];
10780 	int akmp = -1, cipher = -1, got_bssid = 0;
10781 	u16 group = 0xFFFF;
10782 	u8 *comeback = NULL;
10783 	size_t comeback_len = 0;
10784 	int id = 0, ret = -1;
10785 
10786 	/*
10787 	 * Entry format: bssid=<BSSID> akmp=<AKMP> cipher=<CIPHER> group=<group>
10788 	 *    [comeback=<hexdump>]
10789 	 */
10790 	while ((token = str_token(cmd, " ", &context))) {
10791 		if (os_strncmp(token, "bssid=", 6) == 0) {
10792 			if (hwaddr_aton(token + 6, bssid))
10793 				goto out;
10794 			got_bssid = 1;
10795 		} else if (os_strcmp(token, "akmp=PASN") == 0) {
10796 			akmp = WPA_KEY_MGMT_PASN;
10797 #ifdef CONFIG_IEEE80211R
10798 		} else if (os_strcmp(token, "akmp=FT-PSK") == 0) {
10799 			akmp = WPA_KEY_MGMT_FT_PSK;
10800 		} else if (os_strcmp(token, "akmp=FT-EAP-SHA384") == 0) {
10801 			akmp = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
10802 		} else if (os_strcmp(token, "akmp=FT-EAP") == 0) {
10803 			akmp = WPA_KEY_MGMT_FT_IEEE8021X;
10804 #endif /* CONFIG_IEEE80211R */
10805 #ifdef CONFIG_SAE
10806 		} else if (os_strcmp(token, "akmp=SAE") == 0) {
10807 			akmp = WPA_KEY_MGMT_SAE;
10808 #endif /* CONFIG_SAE */
10809 #ifdef CONFIG_FILS
10810 		} else if (os_strcmp(token, "akmp=FILS-SHA256") == 0) {
10811 			akmp = WPA_KEY_MGMT_FILS_SHA256;
10812 		} else if (os_strcmp(token, "akmp=FILS-SHA384") == 0) {
10813 			akmp = WPA_KEY_MGMT_FILS_SHA384;
10814 #endif /* CONFIG_FILS */
10815 		} else if (os_strcmp(token, "cipher=CCMP-256") == 0) {
10816 			cipher = WPA_CIPHER_CCMP_256;
10817 		} else if (os_strcmp(token, "cipher=GCMP-256") == 0) {
10818 			cipher = WPA_CIPHER_GCMP_256;
10819 		} else if (os_strcmp(token, "cipher=CCMP") == 0) {
10820 			cipher = WPA_CIPHER_CCMP;
10821 		} else if (os_strcmp(token, "cipher=GCMP") == 0) {
10822 			cipher = WPA_CIPHER_GCMP;
10823 		} else if (os_strncmp(token, "group=", 6) == 0) {
10824 			group = atoi(token + 6);
10825 		} else if (os_strncmp(token, "nid=", 4) == 0) {
10826 			id = atoi(token + 4);
10827 		} else if (os_strncmp(token, "comeback=", 9) == 0) {
10828 			comeback_len = os_strlen(token + 9);
10829 			if (comeback || !comeback_len || comeback_len % 2)
10830 				goto out;
10831 
10832 			comeback_len /= 2;
10833 			comeback = os_malloc(comeback_len);
10834 			if (!comeback ||
10835 			    hexstr2bin(token + 9, comeback, comeback_len))
10836 				goto out;
10837 		} else {
10838 			wpa_printf(MSG_DEBUG,
10839 				   "CTRL: PASN Invalid parameter: '%s'",
10840 				   token);
10841 			goto out;
10842 		}
10843 	}
10844 
10845 	if (!got_bssid || akmp == -1 || cipher == -1 || group == 0xFFFF) {
10846 		wpa_printf(MSG_DEBUG,"CTRL: PASN missing parameter");
10847 		goto out;
10848 	}
10849 
10850 	ret = wpas_pasn_auth_start(wpa_s, bssid, akmp, cipher, group, id,
10851 				   comeback, comeback_len);
10852 out:
10853 	os_free(comeback);
10854 	return ret;
10855 }
10856 
10857 
wpas_ctrl_iface_pasn_deauthenticate(struct wpa_supplicant * wpa_s,const char * cmd)10858 static int wpas_ctrl_iface_pasn_deauthenticate(struct wpa_supplicant *wpa_s,
10859 					       const char *cmd)
10860 {
10861 	u8 bssid[ETH_ALEN];
10862 
10863 	if (os_strncmp(cmd, "bssid=", 6) != 0 || hwaddr_aton(cmd + 6, bssid)) {
10864 		wpa_printf(MSG_DEBUG,
10865 			   "CTRL: PASN_DEAUTH without valid BSSID");
10866 		return -1;
10867 	}
10868 
10869 	return wpas_pasn_deauthenticate(wpa_s, bssid);
10870 }
10871 
10872 #endif /* CONFIG_PASN */
10873 
10874 
set_type4_frame_classifier(const char * cmd,struct type4_params * param)10875 static int set_type4_frame_classifier(const char *cmd,
10876 				      struct type4_params *param)
10877 {
10878 	const char *pos, *end;
10879 	u8 classifier_mask = 0;
10880 	int ret;
10881 	char addr[INET6_ADDRSTRLEN];
10882 	size_t alen;
10883 
10884 	if (os_strstr(cmd, "ip_version=ipv4")) {
10885 		param->ip_version = IPV4;
10886 	} else if (os_strstr(cmd, "ip_version=ipv6")) {
10887 		param->ip_version = IPV6;
10888 	} else {
10889 		wpa_printf(MSG_ERROR, "IP version missing/invalid");
10890 		return -1;
10891 	}
10892 
10893 	classifier_mask |= BIT(0);
10894 
10895 	pos = os_strstr(cmd, "src_ip=");
10896 	if (pos) {
10897 		pos += 7;
10898 		end = os_strchr(pos, ' ');
10899 		if (!end)
10900 			end = pos + os_strlen(pos);
10901 
10902 		alen = end - pos;
10903 		if (alen >= INET6_ADDRSTRLEN)
10904 			return -1;
10905 		os_memcpy(addr, pos, alen);
10906 		addr[alen] = '\0';
10907 		if (param->ip_version == IPV4)
10908 			ret = inet_pton(AF_INET, addr,
10909 					&param->ip_params.v4.src_ip);
10910 		else
10911 			ret = inet_pton(AF_INET6, addr,
10912 					&param->ip_params.v6.src_ip);
10913 
10914 		if (ret != 1) {
10915 			wpa_printf(MSG_ERROR,
10916 				   "Error converting src IP address to binary ret=%d",
10917 				   ret);
10918 			return -1;
10919 		}
10920 
10921 		classifier_mask |= BIT(1);
10922 	}
10923 
10924 	pos = os_strstr(cmd, "dst_ip=");
10925 	if (pos) {
10926 		pos += 7;
10927 		end = os_strchr(pos, ' ');
10928 		if (!end)
10929 			end = pos + os_strlen(pos);
10930 
10931 		alen = end - pos;
10932 		if (alen >= INET6_ADDRSTRLEN)
10933 			return -1;
10934 		os_memcpy(addr, pos, alen);
10935 		addr[alen] = '\0';
10936 		if (param->ip_version == IPV4)
10937 			ret = inet_pton(AF_INET, addr,
10938 					&param->ip_params.v4.dst_ip);
10939 		else
10940 			ret = inet_pton(AF_INET6, addr,
10941 					&param->ip_params.v6.dst_ip);
10942 
10943 		if (ret != 1) {
10944 			wpa_printf(MSG_ERROR,
10945 				   "Error converting dst IP address to binary ret=%d",
10946 				   ret);
10947 			return -1;
10948 		}
10949 
10950 		classifier_mask |= BIT(2);
10951 	}
10952 
10953 	pos = os_strstr(cmd, "src_port=");
10954 	if (pos && atoi(pos + 9) > 0) {
10955 		if (param->ip_version == IPV4)
10956 			param->ip_params.v4.src_port = atoi(pos + 9);
10957 		else
10958 			param->ip_params.v6.src_port = atoi(pos + 9);
10959 		classifier_mask |= BIT(3);
10960 	}
10961 
10962 	pos = os_strstr(cmd, "dst_port=");
10963 	if (pos && atoi(pos + 9) > 0) {
10964 		if (param->ip_version == IPV4)
10965 			param->ip_params.v4.dst_port = atoi(pos + 9);
10966 		else
10967 			param->ip_params.v6.dst_port = atoi(pos + 9);
10968 		classifier_mask |= BIT(4);
10969 	}
10970 
10971 	pos = os_strstr(cmd, "dscp=");
10972 	if (pos && atoi(pos + 5) > 0) {
10973 		if (param->ip_version == IPV4)
10974 			param->ip_params.v4.dscp = atoi(pos + 5);
10975 		else
10976 			param->ip_params.v6.dscp = atoi(pos + 5);
10977 		classifier_mask |= BIT(5);
10978 	}
10979 
10980 	if (param->ip_version == IPV4) {
10981 		pos = os_strstr(cmd, "protocol=");
10982 		if (pos) {
10983 			if (os_strstr(pos, "udp")) {
10984 				param->ip_params.v4.protocol = 17;
10985 			} else if (os_strstr(pos, "tcp")) {
10986 				param->ip_params.v4.protocol = 6;
10987 			} else if (os_strstr(pos, "esp")) {
10988 				param->ip_params.v4.protocol = 50;
10989 			} else {
10990 				wpa_printf(MSG_ERROR, "Invalid protocol");
10991 				return -1;
10992 			}
10993 			classifier_mask |= BIT(6);
10994 		}
10995 	} else {
10996 		pos = os_strstr(cmd, "next_header=");
10997 		if (pos) {
10998 			if (os_strstr(pos, "udp")) {
10999 				param->ip_params.v6.next_header = 17;
11000 			} else if (os_strstr(pos, "tcp")) {
11001 				param->ip_params.v6.next_header = 6;
11002 			} else if (os_strstr(pos, "esp")) {
11003 				param->ip_params.v6.next_header = 50;
11004 			} else {
11005 				wpa_printf(MSG_ERROR, "Invalid next header");
11006 				return -1;
11007 			}
11008 
11009 			classifier_mask |= BIT(6);
11010 		}
11011 
11012 		pos = os_strstr(cmd, "flow_label=");
11013 		if (pos) {
11014 			pos += 11;
11015 			end = os_strchr(pos, ' ');
11016 			if (!end)
11017 				end = pos + os_strlen(pos);
11018 
11019 			if (end - pos != 6 ||
11020 			    hexstr2bin(pos, param->ip_params.v6.flow_label,
11021 				       3) ||
11022 			    param->ip_params.v6.flow_label[0] > 0x0F) {
11023 				wpa_printf(MSG_ERROR, "Invalid flow label");
11024 				return -1;
11025 			}
11026 
11027 			classifier_mask |= BIT(7);
11028 		}
11029 	}
11030 
11031 	param->classifier_mask = classifier_mask;
11032 	return 0;
11033 }
11034 
11035 
set_type10_frame_classifier(const char * cmd,struct type10_params * param)11036 static int set_type10_frame_classifier(const char *cmd,
11037 				       struct type10_params *param)
11038 {
11039 	const char *pos, *end;
11040 	size_t filter_len;
11041 
11042 	pos = os_strstr(cmd, "prot_instance=");
11043 	if (!pos) {
11044 		wpa_printf(MSG_ERROR, "Protocol instance missing");
11045 		return -1;
11046 	}
11047 	param->prot_instance = atoi(pos + 14);
11048 
11049 	pos = os_strstr(cmd, "prot_number=");
11050 	if (!pos) {
11051 		wpa_printf(MSG_ERROR, "Protocol number missing");
11052 		return -1;
11053 	}
11054 	if (os_strstr(pos, "udp")) {
11055 		param->prot_number = 17;
11056 	} else if (os_strstr(pos, "tcp")) {
11057 		param->prot_number = 6;
11058 	} else if (os_strstr(pos, "esp")) {
11059 		param->prot_number = 50;
11060 	} else {
11061 		wpa_printf(MSG_ERROR, "Invalid protocol number");
11062 		return -1;
11063 	}
11064 
11065 	pos = os_strstr(cmd, "filter_value=");
11066 	if (!pos) {
11067 		wpa_printf(MSG_ERROR,
11068 			   "Classifier parameter filter_value missing");
11069 		return -1;
11070 	}
11071 
11072 	pos += 13;
11073 	end = os_strchr(pos, ' ');
11074 	if (!end)
11075 		end = pos + os_strlen(pos);
11076 
11077 	filter_len = (end - pos) / 2;
11078 	param->filter_value = os_malloc(filter_len);
11079 	if (!param->filter_value)
11080 		return -1;
11081 
11082 	if (hexstr2bin(pos, param->filter_value, filter_len)) {
11083 		wpa_printf(MSG_ERROR, "Invalid filter_value %s", pos);
11084 		goto free;
11085 	}
11086 
11087 	pos = os_strstr(cmd, "filter_mask=");
11088 	if (!pos) {
11089 		wpa_printf(MSG_ERROR,
11090 			   "Classifier parameter filter_mask missing");
11091 		goto free;
11092 	}
11093 
11094 	pos += 12;
11095 	end = os_strchr(pos, ' ');
11096 	if (!end)
11097 		end = pos + os_strlen(pos);
11098 
11099 	if (filter_len != (size_t) (end - pos) / 2) {
11100 		wpa_printf(MSG_ERROR,
11101 			   "Filter mask length mismatch expected=%zu received=%zu",
11102 			   filter_len, (size_t) (end - pos) / 2);
11103 		goto free;
11104 	}
11105 
11106 	param->filter_mask = os_malloc(filter_len);
11107 	if (!param->filter_mask)
11108 		goto free;
11109 
11110 	if (hexstr2bin(pos, param->filter_mask, filter_len)) {
11111 		wpa_printf(MSG_ERROR, "Invalid filter mask %s", pos);
11112 		os_free(param->filter_mask);
11113 		param->filter_mask = NULL;
11114 		goto free;
11115 	}
11116 
11117 	param->filter_len = filter_len;
11118 	return 0;
11119 free:
11120 	os_free(param->filter_value);
11121 	param->filter_value = NULL;
11122 	return -1;
11123 }
11124 
11125 
scs_parse_type4(struct tclas_element * elem,const char * pos)11126 static int scs_parse_type4(struct tclas_element *elem, const char *pos)
11127 {
11128 	struct type4_params type4_param = { 0 };
11129 
11130 	if (set_type4_frame_classifier(pos, &type4_param) == -1) {
11131 		wpa_printf(MSG_ERROR, "Failed to set frame_classifier 4");
11132 		return -1;
11133 	}
11134 
11135 	os_memcpy(&elem->frame_classifier.type4_param,
11136 		  &type4_param, sizeof(struct type4_params));
11137 	return 0;
11138 }
11139 
11140 
scs_parse_type10(struct tclas_element * elem,const char * pos)11141 static int scs_parse_type10(struct tclas_element *elem, const char *pos)
11142 {
11143 	struct type10_params type10_param = { 0 };
11144 
11145 	if (set_type10_frame_classifier(pos, &type10_param) == -1) {
11146 		wpa_printf(MSG_ERROR, "Failed to set frame_classifier 10");
11147 		return -1;
11148 	}
11149 
11150 	os_memcpy(&elem->frame_classifier.type10_param,
11151 		  &type10_param, sizeof(struct type10_params));
11152 	return 0;
11153 }
11154 
11155 
wpas_ctrl_iface_configure_scs(struct wpa_supplicant * wpa_s,char * cmd)11156 static int wpas_ctrl_iface_configure_scs(struct wpa_supplicant *wpa_s,
11157 					 char *cmd)
11158 {
11159 	char *pos1, *pos;
11160 	struct scs_robust_av_data *scs_data = &wpa_s->scs_robust_av_req;
11161 	struct scs_desc_elem desc_elem = { 0 };
11162 	int val;
11163 	unsigned int num_scs_desc = 0;
11164 
11165 	if (wpa_s->ongoing_scs_req) {
11166 		wpa_printf(MSG_ERROR, "%s: SCS Request already in queue",
11167 			   __func__);
11168 		return -1;
11169 	}
11170 
11171 	/**
11172 	 * format:
11173 	 * [scs_id=<decimal number>] <add|remove|change> [scs_up=<0-7>]
11174 	 * [classifier_type=<4|10>]
11175 	 * [classifier params based on classifier type]
11176 	 * [tclas_processing=<0|1>] [scs_id=<decimal number>] ...
11177 	 */
11178 	pos1 = os_strstr(cmd, "scs_id=");
11179 	if (!pos1) {
11180 		wpa_printf(MSG_ERROR, "SCSID not present");
11181 		return -1;
11182 	}
11183 
11184 	free_up_scs_desc(scs_data);
11185 
11186 	while (pos1) {
11187 		struct scs_desc_elem *n1;
11188 		struct active_scs_elem *active_scs_desc;
11189 		char *next_scs_desc;
11190 		unsigned int num_tclas_elem = 0;
11191 		bool scsid_active = false;
11192 
11193 		desc_elem.scs_id = atoi(pos1 + 7);
11194 		pos1 += 7;
11195 
11196 		next_scs_desc = os_strstr(pos1, "scs_id=");
11197 		if (next_scs_desc) {
11198 			char temp[20];
11199 
11200 			os_snprintf(temp, sizeof(temp), "scs_id=%d ",
11201 				    desc_elem.scs_id);
11202 			if (os_strstr(next_scs_desc, temp)) {
11203 				wpa_printf(MSG_ERROR,
11204 					   "Multiple SCS descriptors configured with same SCSID(=%d)",
11205 					   desc_elem.scs_id);
11206 				goto free_scs_desc;
11207 			}
11208 			pos1[next_scs_desc - pos1 - 1] = '\0';
11209 		}
11210 
11211 		dl_list_for_each(active_scs_desc, &wpa_s->active_scs_ids,
11212 				 struct active_scs_elem, list) {
11213 			if (desc_elem.scs_id == active_scs_desc->scs_id) {
11214 				scsid_active = true;
11215 				break;
11216 			}
11217 		}
11218 
11219 		if (os_strstr(pos1, "add ")) {
11220 			desc_elem.request_type = SCS_REQ_ADD;
11221 			if (scsid_active) {
11222 				wpa_printf(MSG_ERROR, "SCSID %d already active",
11223 					   desc_elem.scs_id);
11224 				return -1;
11225 			}
11226 		} else if (os_strstr(pos1, "remove")) {
11227 			desc_elem.request_type = SCS_REQ_REMOVE;
11228 			if (!scsid_active) {
11229 				wpa_printf(MSG_ERROR, "SCSID %d not active",
11230 					   desc_elem.scs_id);
11231 				return -1;
11232 			}
11233 			goto scs_desc_end;
11234 		} else if (os_strstr(pos1, "change ")) {
11235 			desc_elem.request_type = SCS_REQ_CHANGE;
11236 			if (!scsid_active) {
11237 				wpa_printf(MSG_ERROR, "SCSID %d not active",
11238 					   desc_elem.scs_id);
11239 				return -1;
11240 			}
11241 		} else {
11242 			wpa_printf(MSG_ERROR, "SCS Request type invalid");
11243 			goto free_scs_desc;
11244 		}
11245 
11246 		pos1 = os_strstr(pos1, "scs_up=");
11247 		if (!pos1) {
11248 			wpa_printf(MSG_ERROR,
11249 				   "Intra-Access user priority not present");
11250 			goto free_scs_desc;
11251 		}
11252 
11253 		val = atoi(pos1 + 7);
11254 		if (val < 0 || val > 7) {
11255 			wpa_printf(MSG_ERROR,
11256 				   "Intra-Access user priority invalid %d",
11257 				   val);
11258 			goto free_scs_desc;
11259 		}
11260 
11261 		desc_elem.intra_access_priority = val;
11262 		desc_elem.scs_up_avail = true;
11263 
11264 		pos = os_strstr(pos1, "classifier_type=");
11265 		if (!pos) {
11266 			wpa_printf(MSG_ERROR, "classifier type empty");
11267 			goto free_scs_desc;
11268 		}
11269 
11270 		while (pos) {
11271 			struct tclas_element elem = { 0 }, *n;
11272 			char *next_tclas_elem;
11273 
11274 			val = atoi(pos + 16);
11275 			if (val != 4 && val != 10) {
11276 				wpa_printf(MSG_ERROR,
11277 					   "classifier type invalid %d", val);
11278 				goto free_scs_desc;
11279 			}
11280 
11281 			elem.classifier_type = val;
11282 			pos += 16;
11283 
11284 			next_tclas_elem = os_strstr(pos, "classifier_type=");
11285 			if (next_tclas_elem) {
11286 				pos1 = next_tclas_elem;
11287 				pos[next_tclas_elem - pos - 1] = '\0';
11288 			}
11289 
11290 			switch (val) {
11291 			case 4:
11292 				if (scs_parse_type4(&elem, pos) < 0)
11293 					goto free_scs_desc;
11294 				break;
11295 			case 10:
11296 				if (scs_parse_type10(&elem, pos) < 0)
11297 					goto free_scs_desc;
11298 				break;
11299 			}
11300 
11301 			n = os_realloc(desc_elem.tclas_elems,
11302 				       (num_tclas_elem + 1) * sizeof(elem));
11303 			if (!n)
11304 				goto free_scs_desc;
11305 
11306 			desc_elem.tclas_elems = n;
11307 			os_memcpy((u8 *) desc_elem.tclas_elems +
11308 				  num_tclas_elem * sizeof(elem),
11309 				  &elem, sizeof(elem));
11310 			num_tclas_elem++;
11311 			desc_elem.num_tclas_elem = num_tclas_elem;
11312 			pos = next_tclas_elem;
11313 		}
11314 
11315 		if (desc_elem.num_tclas_elem > 1) {
11316 			pos1 = os_strstr(pos1, "tclas_processing=");
11317 			if (!pos1) {
11318 				wpa_printf(MSG_ERROR, "tclas_processing empty");
11319 				goto free_scs_desc;
11320 			}
11321 
11322 			val = atoi(pos1 + 17);
11323 			if (val != 0 && val != 1) {
11324 				wpa_printf(MSG_ERROR,
11325 					   "tclas_processing invalid");
11326 				goto free_scs_desc;
11327 			}
11328 
11329 			desc_elem.tclas_processing = val;
11330 		}
11331 
11332 scs_desc_end:
11333 		n1 = os_realloc(scs_data->scs_desc_elems, (num_scs_desc + 1) *
11334 				sizeof(struct scs_desc_elem));
11335 		if (!n1)
11336 			goto free_scs_desc;
11337 
11338 		scs_data->scs_desc_elems = n1;
11339 		os_memcpy((u8 *) scs_data->scs_desc_elems + num_scs_desc *
11340 			  sizeof(desc_elem), &desc_elem, sizeof(desc_elem));
11341 		num_scs_desc++;
11342 		scs_data->num_scs_desc = num_scs_desc;
11343 		pos1 = next_scs_desc;
11344 		os_memset(&desc_elem, 0, sizeof(desc_elem));
11345 	}
11346 
11347 	return wpas_send_scs_req(wpa_s);
11348 
11349 free_scs_desc:
11350 	free_up_tclas_elem(&desc_elem);
11351 	free_up_scs_desc(scs_data);
11352 	return -1;
11353 }
11354 
11355 
wpas_ctrl_iface_send_dscp_resp(struct wpa_supplicant * wpa_s,const char * cmd)11356 static int wpas_ctrl_iface_send_dscp_resp(struct wpa_supplicant *wpa_s,
11357 					  const char *cmd)
11358 {
11359 	char *pos;
11360 	struct dscp_policy_status *policy = NULL, *n;
11361 	int num_policies = 0, ret = -1;
11362 	struct dscp_resp_data resp_data;
11363 
11364 	/*
11365 	 * format:
11366 	 * <[reset]>/<[solicited] [policy_id=1 status=0...]> [more]
11367 	 */
11368 
11369 	os_memset(&resp_data, 0, sizeof(resp_data));
11370 
11371 	resp_data.more = os_strstr(cmd, "more") != NULL;
11372 
11373 	if (os_strstr(cmd, "reset")) {
11374 		resp_data.reset = true;
11375 		resp_data.solicited = false;
11376 		goto send_resp;
11377 	}
11378 
11379 	resp_data.solicited = os_strstr(cmd, "solicited") != NULL;
11380 
11381 	pos = os_strstr(cmd, "policy_id=");
11382 	while (pos) {
11383 		n = os_realloc(policy, (num_policies + 1) * sizeof(*policy));
11384 		if (!n)
11385 			goto fail;
11386 
11387 		policy = n;
11388 		pos += 10;
11389 		policy[num_policies].id = atoi(pos);
11390 		if (policy[num_policies].id == 0) {
11391 			wpa_printf(MSG_ERROR, "DSCP: Invalid policy id");
11392 			goto fail;
11393 		}
11394 
11395 		pos = os_strstr(pos, "status=");
11396 		if (!pos) {
11397 			wpa_printf(MSG_ERROR,
11398 				   "DSCP: Status is not found for a policy");
11399 			goto fail;
11400 		}
11401 
11402 		pos += 7;
11403 		policy[num_policies].status = atoi(pos);
11404 		num_policies++;
11405 
11406 		pos = os_strstr(pos, "policy_id");
11407 	}
11408 
11409 	resp_data.policy = policy;
11410 	resp_data.num_policies = num_policies;
11411 send_resp:
11412 	ret = wpas_send_dscp_response(wpa_s, &resp_data);
11413 	if (ret)
11414 		wpa_printf(MSG_ERROR, "DSCP: Failed to send DSCP response");
11415 fail:
11416 	os_free(policy);
11417 	return ret;
11418 }
11419 
11420 
wpas_ctrl_iface_send_dscp_query(struct wpa_supplicant * wpa_s,const char * cmd)11421 static int wpas_ctrl_iface_send_dscp_query(struct wpa_supplicant *wpa_s,
11422 					   const char *cmd)
11423 {
11424 	char *pos;
11425 
11426 	/*
11427 	 * format:
11428 	 * Wildcard DSCP query
11429 	 * <wildcard>
11430 	 *
11431 	 * DSCP query with a domain name attribute:
11432 	 * [domain_name=<string>]
11433 	 */
11434 
11435 	if (os_strstr(cmd, "wildcard")) {
11436 		wpa_printf(MSG_DEBUG, "QM: Send wildcard DSCP policy query");
11437 		return wpas_send_dscp_query(wpa_s, NULL, 0);
11438 	}
11439 
11440 	pos = os_strstr(cmd, "domain_name=");
11441 	if (!pos || !os_strlen(pos + 12)) {
11442 		wpa_printf(MSG_ERROR, "QM: Domain name not preset");
11443 		return -1;
11444 	}
11445 
11446 	return wpas_send_dscp_query(wpa_s, pos + 12, os_strlen(pos + 12));
11447 }
11448 
11449 
wpa_supplicant_ctrl_iface_process(struct wpa_supplicant * wpa_s,char * buf,size_t * resp_len)11450 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
11451 					 char *buf, size_t *resp_len)
11452 {
11453 	char *reply;
11454 	const int reply_size = 4096;
11455 	int reply_len;
11456 
11457 	if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
11458 	    os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
11459 	    os_strncmp(buf, "PMKSA_ADD ", 10) == 0 ||
11460 	    os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
11461 		if (wpa_debug_show_keys)
11462 			wpa_dbg(wpa_s, MSG_DEBUG,
11463 				"Control interface command '%s'", buf);
11464 		else
11465 			wpa_dbg(wpa_s, MSG_DEBUG,
11466 				"Control interface command '%s [REMOVED]'",
11467 				os_strncmp(buf, WPA_CTRL_RSP,
11468 					   os_strlen(WPA_CTRL_RSP)) == 0 ?
11469 				WPA_CTRL_RSP :
11470 				(os_strncmp(buf, "SET_NETWORK ", 12) == 0 ?
11471 				 "SET_NETWORK" : "key-add"));
11472 	} else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
11473 		   os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
11474 		wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
11475 				      (const u8 *) buf, os_strlen(buf));
11476 	} else {
11477 		int level = wpas_ctrl_cmd_debug_level(buf);
11478 		wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
11479 	}
11480 
11481 	reply = os_malloc(reply_size);
11482 	if (reply == NULL) {
11483 		*resp_len = 1;
11484 		return NULL;
11485 	}
11486 
11487 	os_memcpy(reply, "OK\n", 3);
11488 	reply_len = 3;
11489 
11490 	if (os_strcmp(buf, "PING") == 0) {
11491 		os_memcpy(reply, "PONG\n", 5);
11492 		reply_len = 5;
11493 	} else if (os_strcmp(buf, "IFNAME") == 0) {
11494 		reply_len = os_strlen(wpa_s->ifname);
11495 		os_memcpy(reply, wpa_s->ifname, reply_len);
11496 	} else if (os_strncmp(buf, "RELOG", 5) == 0) {
11497 		if (wpa_debug_reopen_file() < 0)
11498 			reply_len = -1;
11499 	} else if (os_strncmp(buf, "NOTE ", 5) == 0) {
11500 		wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
11501 	} else if (os_strcmp(buf, "MIB") == 0) {
11502 		reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
11503 		if (reply_len >= 0) {
11504 			reply_len += eapol_sm_get_mib(wpa_s->eapol,
11505 						      reply + reply_len,
11506 						      reply_size - reply_len);
11507 #ifdef CONFIG_MACSEC
11508 			reply_len += ieee802_1x_kay_get_mib(
11509 				wpa_s->kay, reply + reply_len,
11510 				reply_size - reply_len);
11511 #endif /* CONFIG_MACSEC */
11512 		}
11513 	} else if (os_strncmp(buf, "STATUS", 6) == 0) {
11514 		reply_len = wpa_supplicant_ctrl_iface_status(
11515 			wpa_s, buf + 6, reply, reply_size);
11516 	} else if (os_strcmp(buf, "PMKSA") == 0) {
11517 		reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
11518 	} else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
11519 		wpas_ctrl_iface_pmksa_flush(wpa_s);
11520 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
11521 	} else if (os_strncmp(buf, "PMKSA_GET ", 10) == 0) {
11522 		reply_len = wpas_ctrl_iface_pmksa_get(wpa_s, buf + 10,
11523 						      reply, reply_size);
11524 	} else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
11525 		if (wpas_ctrl_iface_pmksa_add(wpa_s, buf + 10) < 0)
11526 			reply_len = -1;
11527 #ifdef CONFIG_MESH
11528 	} else if (os_strncmp(buf, "MESH_PMKSA_GET ", 15) == 0) {
11529 		reply_len = wpas_ctrl_iface_mesh_pmksa_get(wpa_s, buf + 15,
11530 							   reply, reply_size);
11531 	} else if (os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
11532 		if (wpas_ctrl_iface_mesh_pmksa_add(wpa_s, buf + 15) < 0)
11533 			reply_len = -1;
11534 #endif /* CONFIG_MESH */
11535 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
11536 	} else if (os_strncmp(buf, "SET ", 4) == 0) {
11537 		if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
11538 			reply_len = -1;
11539 	} else if (os_strncmp(buf, "DUMP", 4) == 0) {
11540 		reply_len = wpa_config_dump_values(wpa_s->conf,
11541 						   reply, reply_size);
11542 	} else if (os_strncmp(buf, "GET ", 4) == 0) {
11543 		reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
11544 							  reply, reply_size);
11545 	} else if (os_strcmp(buf, "LOGON") == 0) {
11546 		eapol_sm_notify_logoff(wpa_s->eapol, false);
11547 	} else if (os_strcmp(buf, "LOGOFF") == 0) {
11548 		eapol_sm_notify_logoff(wpa_s->eapol, true);
11549 	} else if (os_strcmp(buf, "REASSOCIATE") == 0) {
11550 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
11551 			reply_len = -1;
11552 		else
11553 			wpas_request_connection(wpa_s);
11554 	} else if (os_strcmp(buf, "REATTACH") == 0) {
11555 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
11556 		    !wpa_s->current_ssid)
11557 			reply_len = -1;
11558 		else {
11559 			wpa_s->reattach = 1;
11560 			wpas_request_connection(wpa_s);
11561 		}
11562 	} else if (os_strcmp(buf, "RECONNECT") == 0) {
11563 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
11564 			reply_len = -1;
11565 		else if (wpa_s->disconnected)
11566 			wpas_request_connection(wpa_s);
11567 #ifdef IEEE8021X_EAPOL
11568 	} else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
11569 		if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
11570 			reply_len = -1;
11571 #endif /* IEEE8021X_EAPOL */
11572 #ifdef CONFIG_IEEE80211R
11573 	} else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
11574 		if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
11575 			reply_len = -1;
11576 #endif /* CONFIG_IEEE80211R */
11577 #ifdef CONFIG_WPS
11578 	} else if (os_strcmp(buf, "WPS_PBC") == 0) {
11579 		int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
11580 		if (res == -2) {
11581 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
11582 			reply_len = 17;
11583 		} else if (res)
11584 			reply_len = -1;
11585 	} else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
11586 		int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
11587 		if (res == -2) {
11588 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
11589 			reply_len = 17;
11590 		} else if (res)
11591 			reply_len = -1;
11592 	} else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
11593 		reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
11594 							      reply,
11595 							      reply_size);
11596 	} else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
11597 		reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
11598 			wpa_s, buf + 14, reply, reply_size);
11599 	} else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
11600 		if (wpas_wps_cancel(wpa_s))
11601 			reply_len = -1;
11602 #ifdef CONFIG_WPS_NFC
11603 	} else if (os_strcmp(buf, "WPS_NFC") == 0) {
11604 		if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
11605 			reply_len = -1;
11606 	} else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
11607 		if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
11608 			reply_len = -1;
11609 	} else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
11610 		reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
11611 			wpa_s, buf + 21, reply, reply_size);
11612 	} else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
11613 		reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
11614 			wpa_s, buf + 14, reply, reply_size);
11615 	} else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
11616 		if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
11617 							       buf + 17))
11618 			reply_len = -1;
11619 	} else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
11620 		reply_len = wpas_ctrl_nfc_get_handover_req(
11621 			wpa_s, buf + 21, reply, reply_size);
11622 	} else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
11623 		reply_len = wpas_ctrl_nfc_get_handover_sel(
11624 			wpa_s, buf + 21, reply, reply_size);
11625 	} else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
11626 		if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
11627 			reply_len = -1;
11628 #endif /* CONFIG_WPS_NFC */
11629 	} else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
11630 		if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
11631 			reply_len = -1;
11632 #ifdef CONFIG_AP
11633 	} else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
11634 		reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
11635 			wpa_s, buf + 11, reply, reply_size);
11636 #endif /* CONFIG_AP */
11637 #ifdef CONFIG_WPS_ER
11638 	} else if (os_strcmp(buf, "WPS_ER_START") == 0) {
11639 		if (wpas_wps_er_start(wpa_s, NULL))
11640 			reply_len = -1;
11641 	} else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
11642 		if (wpas_wps_er_start(wpa_s, buf + 13))
11643 			reply_len = -1;
11644 	} else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
11645 		wpas_wps_er_stop(wpa_s);
11646 	} else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
11647 		if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
11648 			reply_len = -1;
11649 	} else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
11650 		int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
11651 		if (ret == -2) {
11652 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
11653 			reply_len = 17;
11654 		} else if (ret == -3) {
11655 			os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
11656 			reply_len = 18;
11657 		} else if (ret == -4) {
11658 			os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
11659 			reply_len = 20;
11660 		} else if (ret)
11661 			reply_len = -1;
11662 	} else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
11663 		if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
11664 			reply_len = -1;
11665 	} else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
11666 		if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
11667 								buf + 18))
11668 			reply_len = -1;
11669 	} else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
11670 		if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
11671 			reply_len = -1;
11672 #ifdef CONFIG_WPS_NFC
11673 	} else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
11674 		reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
11675 			wpa_s, buf + 24, reply, reply_size);
11676 #endif /* CONFIG_WPS_NFC */
11677 #endif /* CONFIG_WPS_ER */
11678 #endif /* CONFIG_WPS */
11679 #ifdef CONFIG_IBSS_RSN
11680 	} else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
11681 		if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
11682 			reply_len = -1;
11683 #endif /* CONFIG_IBSS_RSN */
11684 #ifdef CONFIG_MESH
11685 	} else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
11686 		reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
11687 			wpa_s, buf + 19, reply, reply_size);
11688 	} else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
11689 		reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
11690 			wpa_s, "", reply, reply_size);
11691 	} else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
11692 		if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
11693 			reply_len = -1;
11694 	} else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
11695 		if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
11696 								buf + 18))
11697 			reply_len = -1;
11698 	} else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
11699 		if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
11700 			reply_len = -1;
11701 	} else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
11702 		if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
11703 			reply_len = -1;
11704 	} else if (os_strncmp(buf, "MESH_LINK_PROBE ", 16) == 0) {
11705 		if (wpa_supplicant_ctrl_iface_mesh_link_probe(wpa_s, buf + 16))
11706 			reply_len = -1;
11707 #endif /* CONFIG_MESH */
11708 #ifdef CONFIG_P2P
11709 	} else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
11710 		if (p2p_ctrl_find(wpa_s, buf + 8))
11711 			reply_len = -1;
11712 	} else if (os_strcmp(buf, "P2P_FIND") == 0) {
11713 		if (p2p_ctrl_find(wpa_s, ""))
11714 			reply_len = -1;
11715 	} else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
11716 		wpas_p2p_stop_find(wpa_s);
11717 	} else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
11718 		if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
11719 			reply_len = -1;
11720 	} else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
11721 		if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
11722 			reply_len = -1;
11723 	} else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
11724 		reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
11725 					     reply_size);
11726 	} else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
11727 		if (p2p_ctrl_listen(wpa_s, buf + 11))
11728 			reply_len = -1;
11729 	} else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
11730 		if (p2p_ctrl_listen(wpa_s, ""))
11731 			reply_len = -1;
11732 	} else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
11733 		if (wpas_p2p_group_remove(wpa_s, buf + 17))
11734 			reply_len = -1;
11735 	} else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
11736 		if (p2p_ctrl_group_add(wpa_s, ""))
11737 			reply_len = -1;
11738 	} else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
11739 		if (p2p_ctrl_group_add(wpa_s, buf + 14))
11740 			reply_len = -1;
11741 	} else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
11742 		reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
11743 						  reply_size);
11744 	} else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
11745 		if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
11746 			reply_len = -1;
11747 	} else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
11748 		reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
11749 	} else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
11750 		reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
11751 						   reply_size);
11752 	} else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
11753 		if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
11754 			reply_len = -1;
11755 	} else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
11756 		if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
11757 			reply_len = -1;
11758 	} else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
11759 		wpas_p2p_sd_service_update(wpa_s);
11760 	} else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
11761 		if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
11762 			reply_len = -1;
11763 	} else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
11764 		wpas_p2p_service_flush(wpa_s);
11765 	} else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
11766 		if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
11767 			reply_len = -1;
11768 	} else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
11769 		if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
11770 			reply_len = -1;
11771 	} else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
11772 		if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
11773 			reply_len = -1;
11774 	} else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
11775 		if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
11776 			reply_len = -1;
11777 	} else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
11778 		if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
11779 			reply_len = -1;
11780 	} else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
11781 		reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
11782 					      reply_size);
11783 	} else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
11784 		if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
11785 			reply_len = -1;
11786 	} else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
11787 		p2p_ctrl_flush(wpa_s);
11788 	} else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
11789 		if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
11790 			reply_len = -1;
11791 	} else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
11792 		if (wpas_p2p_cancel(wpa_s))
11793 			reply_len = -1;
11794 	} else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
11795 		if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
11796 			reply_len = -1;
11797 	} else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
11798 		if (p2p_ctrl_presence_req(wpa_s, "") < 0)
11799 			reply_len = -1;
11800 	} else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
11801 		if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
11802 			reply_len = -1;
11803 	} else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
11804 		if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
11805 			reply_len = -1;
11806 	} else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
11807 		if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
11808 			reply_len = -1;
11809 	} else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
11810 		if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
11811 			reply_len = -1;
11812 	} else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
11813 		if (wpas_p2p_lo_stop(wpa_s))
11814 			reply_len = -1;
11815 #endif /* CONFIG_P2P */
11816 #ifdef CONFIG_WIFI_DISPLAY
11817 	} else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
11818 		if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
11819 			reply_len = -1;
11820 	} else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
11821 		reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
11822 						     reply, reply_size);
11823 #endif /* CONFIG_WIFI_DISPLAY */
11824 #ifdef CONFIG_INTERWORKING
11825 	} else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
11826 		if (interworking_fetch_anqp(wpa_s) < 0)
11827 			reply_len = -1;
11828 	} else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
11829 		interworking_stop_fetch_anqp(wpa_s);
11830 	} else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
11831 		if (ctrl_interworking_select(wpa_s, NULL) < 0)
11832 			reply_len = -1;
11833 	} else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
11834 		if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
11835 			reply_len = -1;
11836 	} else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
11837 		if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
11838 			reply_len = -1;
11839 	} else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
11840 		int id;
11841 
11842 		id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
11843 		if (id < 0)
11844 			reply_len = -1;
11845 		else {
11846 			reply_len = os_snprintf(reply, reply_size, "%d\n", id);
11847 			if (os_snprintf_error(reply_size, reply_len))
11848 				reply_len = -1;
11849 		}
11850 	} else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
11851 		if (get_anqp(wpa_s, buf + 9) < 0)
11852 			reply_len = -1;
11853 	} else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
11854 		if (gas_request(wpa_s, buf + 12) < 0)
11855 			reply_len = -1;
11856 	} else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
11857 		reply_len = gas_response_get(wpa_s, buf + 17, reply,
11858 					     reply_size);
11859 #endif /* CONFIG_INTERWORKING */
11860 #ifdef CONFIG_HS20
11861 	} else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
11862 		if (get_hs20_anqp(wpa_s, buf + 14) < 0)
11863 			reply_len = -1;
11864 	} else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
11865 		if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
11866 			reply_len = -1;
11867 	} else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
11868 		if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
11869 			reply_len = -1;
11870 	} else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
11871 		if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
11872 			reply_len = -1;
11873 	} else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
11874 		reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
11875 	} else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
11876 		if (del_hs20_icon(wpa_s, buf + 14) < 0)
11877 			reply_len = -1;
11878 	} else if (os_strcmp(buf, "FETCH_OSU") == 0) {
11879 		if (hs20_fetch_osu(wpa_s, 0) < 0)
11880 			reply_len = -1;
11881 	} else if (os_strcmp(buf, "FETCH_OSU no-scan") == 0) {
11882 		if (hs20_fetch_osu(wpa_s, 1) < 0)
11883 			reply_len = -1;
11884 	} else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
11885 		hs20_cancel_fetch_osu(wpa_s);
11886 #endif /* CONFIG_HS20 */
11887 	} else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
11888 	{
11889 		if (wpa_supplicant_ctrl_iface_ctrl_rsp(
11890 			    wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
11891 			reply_len = -1;
11892 		else {
11893 			/*
11894 			 * Notify response from timeout to allow the control
11895 			 * interface response to be sent first.
11896 			 */
11897 			eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
11898 					       wpa_s, NULL);
11899 		}
11900 	} else if (os_strcmp(buf, "RECONFIGURE") == 0) {
11901 		if (wpa_supplicant_reload_configuration(wpa_s))
11902 			reply_len = -1;
11903 	} else if (os_strcmp(buf, "TERMINATE") == 0) {
11904 		wpa_supplicant_terminate_proc(wpa_s->global);
11905 	} else if (os_strncmp(buf, "BSSID ", 6) == 0) {
11906 		if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
11907 			reply_len = -1;
11908 	} else if (os_strncmp(buf, "BSSID_IGNORE", 12) == 0) {
11909 		reply_len = wpa_supplicant_ctrl_iface_bssid_ignore(
11910 			wpa_s, buf + 12, reply, reply_size);
11911 	} else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
11912 		/* deprecated backwards compatibility alias for BSSID_IGNORE */
11913 		reply_len = wpa_supplicant_ctrl_iface_bssid_ignore(
11914 			wpa_s, buf + 9, reply, reply_size);
11915 	} else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
11916 		reply_len = wpa_supplicant_ctrl_iface_log_level(
11917 			wpa_s, buf + 9, reply, reply_size);
11918 	} else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
11919 		reply_len = wpa_supplicant_ctrl_iface_list_networks(
11920 			wpa_s, buf + 14, reply, reply_size);
11921 	} else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
11922 		reply_len = wpa_supplicant_ctrl_iface_list_networks(
11923 			wpa_s, NULL, reply, reply_size);
11924 	} else if (os_strcmp(buf, "DISCONNECT") == 0) {
11925 		wpas_request_disconnection(wpa_s);
11926 	} else if (os_strcmp(buf, "SCAN") == 0) {
11927 		wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
11928 	} else if (os_strncmp(buf, "SCAN ", 5) == 0) {
11929 		wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
11930 	} else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
11931 		reply_len = wpa_supplicant_ctrl_iface_scan_results(
11932 			wpa_s, reply, reply_size);
11933 	} else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
11934 		if (wpas_abort_ongoing_scan(wpa_s) < 0)
11935 			reply_len = -1;
11936 	} else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
11937 		if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
11938 			reply_len = -1;
11939 	} else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
11940 		if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
11941 			reply_len = -1;
11942 	} else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
11943 		if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
11944 			reply_len = -1;
11945 	} else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
11946 		reply_len = wpa_supplicant_ctrl_iface_add_network(
11947 			wpa_s, reply, reply_size);
11948 	} else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
11949 		if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
11950 			reply_len = -1;
11951 	} else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
11952 		if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
11953 			reply_len = -1;
11954 	} else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
11955 		reply_len = wpa_supplicant_ctrl_iface_get_network(
11956 			wpa_s, buf + 12, reply, reply_size);
11957 	} else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
11958 		if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
11959 							  wpa_s))
11960 			reply_len = -1;
11961 	} else if (os_strcmp(buf, "LIST_CREDS") == 0) {
11962 		reply_len = wpa_supplicant_ctrl_iface_list_creds(
11963 			wpa_s, reply, reply_size);
11964 	} else if (os_strcmp(buf, "ADD_CRED") == 0) {
11965 		reply_len = wpa_supplicant_ctrl_iface_add_cred(
11966 			wpa_s, reply, reply_size);
11967 	} else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
11968 		if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
11969 			reply_len = -1;
11970 	} else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
11971 		if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
11972 			reply_len = -1;
11973 	} else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
11974 		reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
11975 							       reply,
11976 							       reply_size);
11977 #ifndef CONFIG_NO_CONFIG_WRITE
11978 	} else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
11979 		if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
11980 			reply_len = -1;
11981 #endif /* CONFIG_NO_CONFIG_WRITE */
11982 	} else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
11983 		reply_len = wpa_supplicant_ctrl_iface_get_capability(
11984 			wpa_s, buf + 15, reply, reply_size);
11985 	} else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
11986 		if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
11987 			reply_len = -1;
11988 	} else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
11989 		if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
11990 			reply_len = -1;
11991 	} else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
11992 		reply_len = wpa_supplicant_global_iface_list(
11993 			wpa_s->global, reply, reply_size);
11994 	} else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
11995 		reply_len = wpa_supplicant_global_iface_interfaces(
11996 			wpa_s->global, buf + 10, reply, reply_size);
11997 	} else if (os_strncmp(buf, "BSS ", 4) == 0) {
11998 		reply_len = wpa_supplicant_ctrl_iface_bss(
11999 			wpa_s, buf + 4, reply, reply_size);
12000 #ifdef CONFIG_AP
12001 	} else if (os_strcmp(buf, "STA-FIRST") == 0) {
12002 		reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
12003 	} else if (os_strncmp(buf, "STA ", 4) == 0) {
12004 		reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
12005 					      reply_size);
12006 	} else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
12007 		reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
12008 						   reply_size);
12009 	} else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
12010 		if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
12011 			reply_len = -1;
12012 	} else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
12013 		if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
12014 			reply_len = -1;
12015 	} else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
12016 		if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
12017 			reply_len = -1;
12018 	} else if (os_strcmp(buf, "STOP_AP") == 0) {
12019 		if (wpas_ap_stop_ap(wpa_s))
12020 			reply_len = -1;
12021 	} else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
12022 		if (wpas_ap_update_beacon(wpa_s))
12023 			reply_len = -1;
12024 #endif /* CONFIG_AP */
12025 	} else if (os_strcmp(buf, "SUSPEND") == 0) {
12026 		wpas_notify_suspend(wpa_s->global);
12027 	} else if (os_strcmp(buf, "RESUME") == 0) {
12028 		wpas_notify_resume(wpa_s->global);
12029 #ifdef CONFIG_TESTING_OPTIONS
12030 	} else if (os_strcmp(buf, "DROP_SA") == 0) {
12031 		wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
12032 #endif /* CONFIG_TESTING_OPTIONS */
12033 	} else if (os_strncmp(buf, "ROAM ", 5) == 0) {
12034 		if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
12035 			reply_len = -1;
12036 	} else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
12037 		wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
12038 	} else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
12039 		if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
12040 			reply_len = -1;
12041 	} else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
12042 		if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
12043 							       buf + 17))
12044 			reply_len = -1;
12045 	} else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
12046 		wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
12047 #ifdef CONFIG_TDLS
12048 	} else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
12049 		if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
12050 			reply_len = -1;
12051 	} else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
12052 		if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
12053 			reply_len = -1;
12054 	} else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
12055 		if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
12056 			reply_len = -1;
12057 	} else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
12058 		if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
12059 							       buf + 17))
12060 			reply_len = -1;
12061 	} else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
12062 		if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
12063 								      buf + 24))
12064 			reply_len = -1;
12065 	} else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
12066 		reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
12067 			wpa_s, buf + 17, reply, reply_size);
12068 #endif /* CONFIG_TDLS */
12069 	} else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
12070 		reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
12071 	} else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
12072 		if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
12073 			reply_len = -1;
12074 	} else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
12075 		if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
12076 			reply_len = -1;
12077 	} else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
12078 		reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
12079 						       reply_size);
12080 	} else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
12081 		if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
12082 			reply_len = -1;
12083 	} else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
12084 		reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
12085 						       reply_size);
12086 #ifdef CONFIG_AUTOSCAN
12087 	} else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
12088 		if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
12089 			reply_len = -1;
12090 #endif /* CONFIG_AUTOSCAN */
12091 	} else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
12092 		reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
12093 							 reply_size);
12094 	} else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
12095 		reply_len = wpas_ctrl_iface_driver_flags2(wpa_s, reply,
12096 							  reply_size);
12097 #ifdef ANDROID
12098 	} else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
12099 		reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
12100 						      reply_size);
12101 #endif /* ANDROID */
12102 	} else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
12103 		reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
12104 						      reply_size);
12105 	} else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
12106 		pmksa_cache_clear_current(wpa_s->wpa);
12107 		eapol_sm_request_reauth(wpa_s->eapol);
12108 #ifdef CONFIG_WNM
12109 	} else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
12110 		if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
12111 			reply_len = -1;
12112 	} else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
12113 		if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
12114 				reply_len = -1;
12115 	} else if (os_strncmp(buf, "COLOC_INTF_REPORT ", 18) == 0) {
12116 		if (wpas_ctrl_iface_coloc_intf_report(wpa_s, buf + 18))
12117 			reply_len = -1;
12118 #endif /* CONFIG_WNM */
12119 	} else if (os_strcmp(buf, "FLUSH") == 0) {
12120 		wpa_supplicant_ctrl_iface_flush(wpa_s);
12121 	} else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
12122 		reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
12123 						 reply_size);
12124 #ifdef CONFIG_TESTING_OPTIONS
12125 	} else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
12126 		if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
12127 			reply_len = -1;
12128 	} else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
12129 		wpas_ctrl_iface_mgmt_tx_done(wpa_s);
12130 	} else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
12131 		if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
12132 			reply_len = -1;
12133 	} else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
12134 		if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
12135 			reply_len = -1;
12136 	} else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
12137 		if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
12138 			reply_len = -1;
12139 	} else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
12140 		if (wpas_ctrl_iface_eapol_tx(wpa_s, buf + 9) < 0)
12141 			reply_len = -1;
12142 	} else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
12143 		if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
12144 			reply_len = -1;
12145 	} else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
12146 		if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
12147 			reply_len = -1;
12148 	} else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
12149 		if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
12150 			reply_len = -1;
12151 	} else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
12152 		if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
12153 			reply_len = -1;
12154 	} else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
12155 		reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
12156 	} else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
12157 		if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0)
12158 			reply_len = -1;
12159 	} else if (os_strcmp(buf, "GET_FAIL") == 0) {
12160 		reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size);
12161 	} else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
12162 		if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
12163 			reply_len = -1;
12164 	} else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
12165 		if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
12166 			reply_len = -1;
12167 	} else if (os_strcmp(buf, "RESET_PN") == 0) {
12168 		if (wpas_ctrl_reset_pn(wpa_s) < 0)
12169 			reply_len = -1;
12170 	} else if (os_strncmp(buf, "KEY_REQUEST ", 12) == 0) {
12171 		if (wpas_ctrl_key_request(wpa_s, buf + 12) < 0)
12172 			reply_len = -1;
12173 	} else if (os_strcmp(buf, "RESEND_ASSOC") == 0) {
12174 		if (wpas_ctrl_resend_assoc(wpa_s) < 0)
12175 			reply_len = -1;
12176 	} else if (os_strcmp(buf, "UNPROT_DEAUTH") == 0) {
12177 		sme_event_unprot_disconnect(
12178 			wpa_s, wpa_s->bssid, NULL,
12179 			WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
12180 	} else if (os_strncmp(buf, "TWT_SETUP ", 10) == 0) {
12181 		if (wpas_ctrl_iface_send_twt_setup(wpa_s, buf + 9))
12182 			reply_len = -1;
12183 	} else if (os_strcmp(buf, "TWT_SETUP") == 0) {
12184 		if (wpas_ctrl_iface_send_twt_setup(wpa_s, ""))
12185 			reply_len = -1;
12186 	} else if (os_strncmp(buf, "TWT_TEARDOWN ", 13) == 0) {
12187 		if (wpas_ctrl_iface_send_twt_teardown(wpa_s, buf + 12))
12188 			reply_len = -1;
12189 	} else if (os_strcmp(buf, "TWT_TEARDOWN") == 0) {
12190 		if (wpas_ctrl_iface_send_twt_teardown(wpa_s, ""))
12191 			reply_len = -1;
12192 #endif /* CONFIG_TESTING_OPTIONS */
12193 	} else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
12194 		if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
12195 			reply_len = -1;
12196 	} else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
12197 		reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
12198 						      reply_size);
12199 	} else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
12200 		if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
12201 			reply_len = -1;
12202 	} else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
12203 		if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
12204 			reply_len = -1;
12205 	} else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
12206 		wpas_ctrl_iface_erp_flush(wpa_s);
12207 	} else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
12208 		if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
12209 			reply_len = -1;
12210 	} else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
12211 		reply_len = wpas_ctrl_iface_get_pref_freq_list(
12212 			wpa_s, buf + 19, reply, reply_size);
12213 #ifdef CONFIG_FILS
12214 	} else if (os_strncmp(buf, "FILS_HLP_REQ_ADD ", 17) == 0) {
12215 		if (wpas_ctrl_iface_fils_hlp_req_add(wpa_s, buf + 17))
12216 			reply_len = -1;
12217 	} else if (os_strcmp(buf, "FILS_HLP_REQ_FLUSH") == 0) {
12218 		wpas_flush_fils_hlp_req(wpa_s);
12219 #endif /* CONFIG_FILS */
12220 #ifdef CONFIG_DPP
12221 	} else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
12222 		int res;
12223 
12224 		res = wpas_dpp_qr_code(wpa_s, buf + 12);
12225 		if (res < 0) {
12226 			reply_len = -1;
12227 		} else {
12228 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12229 			if (os_snprintf_error(reply_size, reply_len))
12230 				reply_len = -1;
12231 		}
12232 	} else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
12233 		int res;
12234 
12235 		res = wpas_dpp_nfc_uri(wpa_s, buf + 12);
12236 		if (res < 0) {
12237 			reply_len = -1;
12238 		} else {
12239 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12240 			if (os_snprintf_error(reply_size, reply_len))
12241 				reply_len = -1;
12242 		}
12243 	} else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
12244 		int res;
12245 
12246 		res = wpas_dpp_nfc_handover_req(wpa_s, buf + 20);
12247 		if (res < 0) {
12248 			reply_len = -1;
12249 		} else {
12250 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12251 			if (os_snprintf_error(reply_size, reply_len))
12252 				reply_len = -1;
12253 		}
12254 	} else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
12255 		int res;
12256 
12257 		res = wpas_dpp_nfc_handover_sel(wpa_s, buf + 20);
12258 		if (res < 0) {
12259 			reply_len = -1;
12260 		} else {
12261 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12262 			if (os_snprintf_error(reply_size, reply_len))
12263 				reply_len = -1;
12264 		}
12265 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
12266 		int res;
12267 
12268 		res = dpp_bootstrap_gen(wpa_s->dpp, buf + 18);
12269 		if (res < 0) {
12270 			reply_len = -1;
12271 		} else {
12272 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12273 			if (os_snprintf_error(reply_size, reply_len))
12274 				reply_len = -1;
12275 		}
12276 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
12277 		if (dpp_bootstrap_remove(wpa_s->dpp, buf + 21) < 0)
12278 			reply_len = -1;
12279 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
12280 		const char *uri;
12281 
12282 		uri = dpp_bootstrap_get_uri(wpa_s->dpp, atoi(buf + 22));
12283 		if (!uri) {
12284 			reply_len = -1;
12285 		} else {
12286 			reply_len = os_snprintf(reply, reply_size, "%s", uri);
12287 			if (os_snprintf_error(reply_size, reply_len))
12288 				reply_len = -1;
12289 		}
12290 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
12291 		reply_len = dpp_bootstrap_info(wpa_s->dpp, atoi(buf + 19),
12292 					       reply, reply_size);
12293 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
12294 		if (dpp_bootstrap_set(wpa_s->dpp, atoi(buf + 18),
12295 				      os_strchr(buf + 18, ' ')) < 0)
12296 			reply_len = -1;
12297 	} else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
12298 		if (wpas_dpp_auth_init(wpa_s, buf + 13) < 0)
12299 			reply_len = -1;
12300 	} else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
12301 		if (wpas_dpp_listen(wpa_s, buf + 11) < 0)
12302 			reply_len = -1;
12303 	} else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
12304 		wpas_dpp_stop(wpa_s);
12305 		wpas_dpp_listen_stop(wpa_s);
12306 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
12307 		int res;
12308 
12309 		res = dpp_configurator_add(wpa_s->dpp, buf + 20);
12310 		if (res < 0) {
12311 			reply_len = -1;
12312 		} else {
12313 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12314 			if (os_snprintf_error(reply_size, reply_len))
12315 				reply_len = -1;
12316 		}
12317 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_SET ", 21) == 0) {
12318 		if (dpp_configurator_set(wpa_s->dpp, buf + 20) < 0)
12319 			reply_len = -1;
12320 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
12321 		if (dpp_configurator_remove(wpa_s->dpp, buf + 24) < 0)
12322 			reply_len = -1;
12323 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
12324 		if (wpas_dpp_configurator_sign(wpa_s, buf + 21) < 0)
12325 			reply_len = -1;
12326 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
12327 		reply_len = dpp_configurator_get_key_id(wpa_s->dpp,
12328 							atoi(buf + 25),
12329 							reply, reply_size);
12330 	} else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
12331 		int res;
12332 
12333 		res = wpas_dpp_pkex_add(wpa_s, buf + 12);
12334 		if (res < 0) {
12335 			reply_len = -1;
12336 		} else {
12337 			reply_len = os_snprintf(reply, reply_size, "%d", res);
12338 			if (os_snprintf_error(reply_size, reply_len))
12339 				reply_len = -1;
12340 		}
12341 	} else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
12342 		if (wpas_dpp_pkex_remove(wpa_s, buf + 16) < 0)
12343 			reply_len = -1;
12344 	} else if (os_strncmp(buf, "DPP_CONF_SET ", 13) == 0) {
12345 		if (wpas_dpp_conf_set(wpa_s, buf + 12) < 0)
12346 			reply_len = -1;
12347 #ifdef CONFIG_DPP2
12348 	} else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
12349 		if (wpas_dpp_controller_start(wpa_s, buf + 20) < 0)
12350 			reply_len = -1;
12351 	} else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
12352 		if (wpas_dpp_controller_start(wpa_s, NULL) < 0)
12353 			reply_len = -1;
12354 	} else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
12355 		dpp_controller_stop(wpa_s->dpp);
12356 	} else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
12357 		if (wpas_dpp_chirp(wpa_s, buf + 9) < 0)
12358 			reply_len = -1;
12359 	} else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
12360 		wpas_dpp_chirp_stop(wpa_s);
12361 	} else if (os_strncmp(buf, "DPP_RECONFIG ", 13) == 0) {
12362 		if (wpas_dpp_reconfig(wpa_s, buf + 13) < 0)
12363 			reply_len = -1;
12364 	} else if (os_strncmp(buf, "DPP_CA_SET ", 11) == 0) {
12365 		if (wpas_dpp_ca_set(wpa_s, buf + 10) < 0)
12366 			reply_len = -1;
12367 #endif /* CONFIG_DPP2 */
12368 #endif /* CONFIG_DPP */
12369 	} else if (os_strncmp(buf, "MSCS ", 5) == 0) {
12370 		if (wpas_ctrl_iface_configure_mscs(wpa_s, buf + 5))
12371 			reply_len = -1;
12372 #ifdef CONFIG_PASN
12373 	} else if (os_strncmp(buf, "PASN_START ", 11) == 0) {
12374 		if (wpas_ctrl_iface_pasn_start(wpa_s, buf + 11) < 0)
12375 			reply_len = -1;
12376 	} else if (os_strcmp(buf, "PASN_STOP") == 0) {
12377 		wpas_pasn_auth_stop(wpa_s);
12378 	} else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
12379 		reply_len = ptksa_cache_list(wpa_s->ptksa, reply, reply_size);
12380 	} else if (os_strncmp(buf, "PASN_DEAUTH ", 12) == 0) {
12381 		if (wpas_ctrl_iface_pasn_deauthenticate(wpa_s, buf + 12) < 0)
12382 			reply_len = -1;
12383 #endif /* CONFIG_PASN */
12384 	} else if (os_strncmp(buf, "SCS ", 4) == 0) {
12385 		if (wpas_ctrl_iface_configure_scs(wpa_s, buf + 4))
12386 			reply_len = -1;
12387 	} else if (os_strncmp(buf, "DSCP_RESP ", 10) == 0) {
12388 		if (wpas_ctrl_iface_send_dscp_resp(wpa_s, buf + 10))
12389 			reply_len = -1;
12390 	} else if (os_strncmp(buf, "DSCP_QUERY ", 11) == 0) {
12391 		if (wpas_ctrl_iface_send_dscp_query(wpa_s, buf + 11))
12392 			reply_len = -1;
12393 	} else {
12394 		os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
12395 		reply_len = 16;
12396 	}
12397 
12398 	if (reply_len < 0) {
12399 		os_memcpy(reply, "FAIL\n", 5);
12400 		reply_len = 5;
12401 	}
12402 
12403 	*resp_len = reply_len;
12404 	return reply;
12405 }
12406 
12407 
wpa_supplicant_global_iface_add(struct wpa_global * global,char * cmd)12408 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
12409 					   char *cmd)
12410 {
12411 	struct wpa_interface iface;
12412 	char *pos, *extra;
12413 	struct wpa_supplicant *wpa_s;
12414 	unsigned int create_iface = 0;
12415 	u8 mac_addr[ETH_ALEN];
12416 	enum wpa_driver_if_type type = WPA_IF_STATION;
12417 
12418 	/*
12419 	 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
12420 	 * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
12421 	 */
12422 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
12423 
12424 	os_memset(&iface, 0, sizeof(iface));
12425 
12426 	do {
12427 		iface.ifname = pos = cmd;
12428 		pos = os_strchr(pos, '\t');
12429 		if (pos)
12430 			*pos++ = '\0';
12431 		if (iface.ifname[0] == '\0')
12432 			return -1;
12433 		if (pos == NULL)
12434 			break;
12435 
12436 		iface.confname = pos;
12437 		pos = os_strchr(pos, '\t');
12438 		if (pos)
12439 			*pos++ = '\0';
12440 		if (iface.confname[0] == '\0')
12441 			iface.confname = NULL;
12442 		if (pos == NULL)
12443 			break;
12444 
12445 		iface.driver = pos;
12446 		pos = os_strchr(pos, '\t');
12447 		if (pos)
12448 			*pos++ = '\0';
12449 		if (iface.driver[0] == '\0')
12450 			iface.driver = NULL;
12451 		if (pos == NULL)
12452 			break;
12453 
12454 		iface.ctrl_interface = pos;
12455 		pos = os_strchr(pos, '\t');
12456 		if (pos)
12457 			*pos++ = '\0';
12458 		if (iface.ctrl_interface[0] == '\0')
12459 			iface.ctrl_interface = NULL;
12460 		if (pos == NULL)
12461 			break;
12462 
12463 		iface.driver_param = pos;
12464 		pos = os_strchr(pos, '\t');
12465 		if (pos)
12466 			*pos++ = '\0';
12467 		if (iface.driver_param[0] == '\0')
12468 			iface.driver_param = NULL;
12469 		if (pos == NULL)
12470 			break;
12471 
12472 		iface.bridge_ifname = pos;
12473 		pos = os_strchr(pos, '\t');
12474 		if (pos)
12475 			*pos++ = '\0';
12476 		if (iface.bridge_ifname[0] == '\0')
12477 			iface.bridge_ifname = NULL;
12478 		if (pos == NULL)
12479 			break;
12480 
12481 		extra = pos;
12482 		pos = os_strchr(pos, '\t');
12483 		if (pos)
12484 			*pos++ = '\0';
12485 		if (!extra[0])
12486 			break;
12487 
12488 		if (os_strcmp(extra, "create") == 0) {
12489 			create_iface = 1;
12490 			if (!pos)
12491 				break;
12492 
12493 			if (os_strcmp(pos, "sta") == 0) {
12494 				type = WPA_IF_STATION;
12495 			} else if (os_strcmp(pos, "ap") == 0) {
12496 				type = WPA_IF_AP_BSS;
12497 			} else {
12498 				wpa_printf(MSG_DEBUG,
12499 					   "INTERFACE_ADD unsupported interface type: '%s'",
12500 					   pos);
12501 				return -1;
12502 			}
12503 		} else {
12504 			wpa_printf(MSG_DEBUG,
12505 				   "INTERFACE_ADD unsupported extra parameter: '%s'",
12506 				   extra);
12507 			return -1;
12508 		}
12509 	} while (0);
12510 
12511 	if (create_iface) {
12512 		wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
12513 			   iface.ifname);
12514 		if (!global->ifaces)
12515 			return -1;
12516 		if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
12517 				   NULL, NULL, NULL, mac_addr, NULL) < 0) {
12518 			wpa_printf(MSG_ERROR,
12519 				   "CTRL_IFACE interface creation failed");
12520 			return -1;
12521 		}
12522 
12523 		wpa_printf(MSG_DEBUG,
12524 			   "CTRL_IFACE interface '%s' created with MAC addr: "
12525 			   MACSTR, iface.ifname, MAC2STR(mac_addr));
12526 	}
12527 
12528 	if (wpa_supplicant_get_iface(global, iface.ifname))
12529 		goto fail;
12530 
12531 	wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
12532 	if (!wpa_s)
12533 		goto fail;
12534 	wpa_s->added_vif = create_iface;
12535 	return 0;
12536 
12537 fail:
12538 	if (create_iface)
12539 		wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
12540 	return -1;
12541 }
12542 
12543 
wpa_supplicant_global_iface_remove(struct wpa_global * global,char * cmd)12544 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
12545 					      char *cmd)
12546 {
12547 	struct wpa_supplicant *wpa_s;
12548 	int ret;
12549 	unsigned int delete_iface;
12550 
12551 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
12552 
12553 	wpa_s = wpa_supplicant_get_iface(global, cmd);
12554 	if (wpa_s == NULL)
12555 		return -1;
12556 	delete_iface = wpa_s->added_vif;
12557 	ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
12558 	if (!ret && delete_iface) {
12559 		wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
12560 			   cmd);
12561 		ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
12562 	}
12563 	return ret;
12564 }
12565 
12566 
wpa_free_iface_info(struct wpa_interface_info * iface)12567 static void wpa_free_iface_info(struct wpa_interface_info *iface)
12568 {
12569 	struct wpa_interface_info *prev;
12570 
12571 	while (iface) {
12572 		prev = iface;
12573 		iface = iface->next;
12574 
12575 		os_free(prev->ifname);
12576 		os_free(prev->desc);
12577 		os_free(prev);
12578 	}
12579 }
12580 
12581 
wpa_supplicant_global_iface_list(struct wpa_global * global,char * buf,int len)12582 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
12583 					    char *buf, int len)
12584 {
12585 	int i, res;
12586 	struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
12587 	char *pos, *end;
12588 
12589 	for (i = 0; wpa_drivers[i]; i++) {
12590 		const struct wpa_driver_ops *drv = wpa_drivers[i];
12591 		if (drv->get_interfaces == NULL)
12592 			continue;
12593 		tmp = drv->get_interfaces(global->drv_priv[i]);
12594 		if (tmp == NULL)
12595 			continue;
12596 
12597 		if (last == NULL)
12598 			iface = last = tmp;
12599 		else
12600 			last->next = tmp;
12601 		while (last->next)
12602 			last = last->next;
12603 	}
12604 
12605 	pos = buf;
12606 	end = buf + len;
12607 	for (tmp = iface; tmp; tmp = tmp->next) {
12608 		res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
12609 				  tmp->drv_name, tmp->ifname,
12610 				  tmp->desc ? tmp->desc : "");
12611 		if (os_snprintf_error(end - pos, res)) {
12612 			*pos = '\0';
12613 			break;
12614 		}
12615 		pos += res;
12616 	}
12617 
12618 	wpa_free_iface_info(iface);
12619 
12620 	return pos - buf;
12621 }
12622 
12623 
wpa_supplicant_global_iface_interfaces(struct wpa_global * global,const char * input,char * buf,int len)12624 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
12625 						  const char *input,
12626 						  char *buf, int len)
12627 {
12628 	int res;
12629 	char *pos, *end;
12630 	struct wpa_supplicant *wpa_s;
12631 	int show_ctrl = 0;
12632 
12633 	if (input)
12634 		show_ctrl = !!os_strstr(input, "ctrl");
12635 
12636 	wpa_s = global->ifaces;
12637 	pos = buf;
12638 	end = buf + len;
12639 
12640 	while (wpa_s) {
12641 		if (show_ctrl)
12642 			res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
12643 					  wpa_s->ifname,
12644 					  wpa_s->conf->ctrl_interface ?
12645 					  wpa_s->conf->ctrl_interface : "N/A");
12646 		else
12647 			res = os_snprintf(pos, end - pos, "%s\n",
12648 					  wpa_s->ifname);
12649 
12650 		if (os_snprintf_error(end - pos, res)) {
12651 			*pos = '\0';
12652 			break;
12653 		}
12654 		pos += res;
12655 		wpa_s = wpa_s->next;
12656 	}
12657 	return pos - buf;
12658 }
12659 
12660 
wpas_global_ctrl_iface_ifname(struct wpa_global * global,const char * ifname,char * cmd,size_t * resp_len)12661 static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
12662 					    const char *ifname,
12663 					    char *cmd, size_t *resp_len)
12664 {
12665 	struct wpa_supplicant *wpa_s;
12666 
12667 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
12668 		if (os_strcmp(ifname, wpa_s->ifname) == 0)
12669 			break;
12670 	}
12671 
12672 	if (wpa_s == NULL) {
12673 		char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
12674 		if (resp)
12675 			*resp_len = os_strlen(resp);
12676 		else
12677 			*resp_len = 1;
12678 		return resp;
12679 	}
12680 
12681 	return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
12682 }
12683 
12684 
wpas_global_ctrl_iface_redir_p2p(struct wpa_global * global,char * buf,size_t * resp_len)12685 static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
12686 					       char *buf, size_t *resp_len)
12687 {
12688 #ifdef CONFIG_P2P
12689 	static const char * cmd[] = {
12690 		"LIST_NETWORKS",
12691 		"P2P_FIND",
12692 		"P2P_STOP_FIND",
12693 		"P2P_LISTEN",
12694 		"P2P_GROUP_ADD",
12695 		"P2P_GET_PASSPHRASE",
12696 		"P2P_SERVICE_UPDATE",
12697 		"P2P_SERVICE_FLUSH",
12698 		"P2P_FLUSH",
12699 		"P2P_CANCEL",
12700 		"P2P_PRESENCE_REQ",
12701 		"P2P_EXT_LISTEN",
12702 #ifdef CONFIG_AP
12703 		"STA-FIRST",
12704 #endif /* CONFIG_AP */
12705 		NULL
12706 	};
12707 	static const char * prefix[] = {
12708 #ifdef ANDROID
12709 		"DRIVER ",
12710 #endif /* ANDROID */
12711 		"GET_CAPABILITY ",
12712 		"GET_NETWORK ",
12713 		"REMOVE_NETWORK ",
12714 		"P2P_FIND ",
12715 		"P2P_CONNECT ",
12716 		"P2P_LISTEN ",
12717 		"P2P_GROUP_REMOVE ",
12718 		"P2P_GROUP_ADD ",
12719 		"P2P_GROUP_MEMBER ",
12720 		"P2P_PROV_DISC ",
12721 		"P2P_SERV_DISC_REQ ",
12722 		"P2P_SERV_DISC_CANCEL_REQ ",
12723 		"P2P_SERV_DISC_RESP ",
12724 		"P2P_SERV_DISC_EXTERNAL ",
12725 		"P2P_SERVICE_ADD ",
12726 		"P2P_SERVICE_DEL ",
12727 		"P2P_SERVICE_REP ",
12728 		"P2P_REJECT ",
12729 		"P2P_INVITE ",
12730 		"P2P_PEER ",
12731 		"P2P_SET ",
12732 		"P2P_UNAUTHORIZE ",
12733 		"P2P_PRESENCE_REQ ",
12734 		"P2P_EXT_LISTEN ",
12735 		"P2P_REMOVE_CLIENT ",
12736 		"WPS_NFC_TOKEN ",
12737 		"WPS_NFC_TAG_READ ",
12738 		"NFC_GET_HANDOVER_SEL ",
12739 		"NFC_GET_HANDOVER_REQ ",
12740 		"NFC_REPORT_HANDOVER ",
12741 		"P2P_ASP_PROVISION ",
12742 		"P2P_ASP_PROVISION_RESP ",
12743 #ifdef CONFIG_AP
12744 		"STA ",
12745 		"STA-NEXT ",
12746 #endif /* CONFIG_AP */
12747 		NULL
12748 	};
12749 	int found = 0;
12750 	int i;
12751 
12752 	if (global->p2p_init_wpa_s == NULL)
12753 		return NULL;
12754 
12755 	for (i = 0; !found && cmd[i]; i++) {
12756 		if (os_strcmp(buf, cmd[i]) == 0)
12757 			found = 1;
12758 	}
12759 
12760 	for (i = 0; !found && prefix[i]; i++) {
12761 		if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
12762 			found = 1;
12763 	}
12764 
12765 	if (found)
12766 		return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
12767 							 buf, resp_len);
12768 #endif /* CONFIG_P2P */
12769 	return NULL;
12770 }
12771 
12772 
wpas_global_ctrl_iface_redir_wfd(struct wpa_global * global,char * buf,size_t * resp_len)12773 static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
12774 					       char *buf, size_t *resp_len)
12775 {
12776 #ifdef CONFIG_WIFI_DISPLAY
12777 	if (global->p2p_init_wpa_s == NULL)
12778 		return NULL;
12779 	if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
12780 	    os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
12781 		return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
12782 							 buf, resp_len);
12783 #endif /* CONFIG_WIFI_DISPLAY */
12784 	return NULL;
12785 }
12786 
12787 
wpas_global_ctrl_iface_redir(struct wpa_global * global,char * buf,size_t * resp_len)12788 static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
12789 					   char *buf, size_t *resp_len)
12790 {
12791 	char *ret;
12792 
12793 	ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
12794 	if (ret)
12795 		return ret;
12796 
12797 	ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
12798 	if (ret)
12799 		return ret;
12800 
12801 	return NULL;
12802 }
12803 
12804 
wpas_global_ctrl_iface_set(struct wpa_global * global,char * cmd)12805 static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
12806 {
12807 	char *value;
12808 
12809 	value = os_strchr(cmd, ' ');
12810 	if (value == NULL)
12811 		return -1;
12812 	*value++ = '\0';
12813 
12814 	wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
12815 
12816 #ifdef CONFIG_WIFI_DISPLAY
12817 	if (os_strcasecmp(cmd, "wifi_display") == 0) {
12818 		wifi_display_enable(global, !!atoi(value));
12819 		return 0;
12820 	}
12821 #endif /* CONFIG_WIFI_DISPLAY */
12822 
12823 	/* Restore cmd to its original value to allow redirection */
12824 	value[-1] = ' ';
12825 
12826 	return -1;
12827 }
12828 
12829 
wpas_global_ctrl_iface_dup_network(struct wpa_global * global,char * cmd)12830 static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
12831 					      char *cmd)
12832 {
12833 	struct wpa_supplicant *wpa_s[2]; /* src, dst */
12834 	char *p;
12835 	unsigned int i;
12836 
12837 	/* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
12838 	 * <variable name> */
12839 
12840 	for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
12841 		p = os_strchr(cmd, ' ');
12842 		if (p == NULL)
12843 			return -1;
12844 		*p = '\0';
12845 
12846 		wpa_s[i] = global->ifaces;
12847 		for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
12848 			if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
12849 				break;
12850 		}
12851 
12852 		if (!wpa_s[i]) {
12853 			wpa_printf(MSG_DEBUG,
12854 				   "CTRL_IFACE: Could not find iface=%s", cmd);
12855 			return -1;
12856 		}
12857 
12858 		cmd = p + 1;
12859 	}
12860 
12861 	return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
12862 }
12863 
12864 
12865 #ifndef CONFIG_NO_CONFIG_WRITE
wpas_global_ctrl_iface_save_config(struct wpa_global * global)12866 static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
12867 {
12868 	int ret = 0, saved = 0;
12869 	struct wpa_supplicant *wpa_s;
12870 
12871 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
12872 		if (!wpa_s->conf->update_config) {
12873 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
12874 			continue;
12875 		}
12876 
12877 		if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
12878 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
12879 			ret = 1;
12880 		} else {
12881 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
12882 			saved++;
12883 		}
12884 	}
12885 
12886 	if (!saved && !ret) {
12887 		wpa_dbg(wpa_s, MSG_DEBUG,
12888 			"CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
12889 		ret = 1;
12890 	}
12891 
12892 	return ret;
12893 }
12894 #endif /* CONFIG_NO_CONFIG_WRITE */
12895 
12896 
wpas_global_ctrl_iface_status(struct wpa_global * global,char * buf,size_t buflen)12897 static int wpas_global_ctrl_iface_status(struct wpa_global *global,
12898 					 char *buf, size_t buflen)
12899 {
12900 	char *pos, *end;
12901 	int ret;
12902 	struct wpa_supplicant *wpa_s;
12903 
12904 	pos = buf;
12905 	end = buf + buflen;
12906 
12907 #ifdef CONFIG_P2P
12908 	if (global->p2p && !global->p2p_disabled) {
12909 		ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
12910 				  "\n"
12911 				  "p2p_state=%s\n",
12912 				  MAC2STR(global->p2p_dev_addr),
12913 				  p2p_get_state_txt(global->p2p));
12914 		if (os_snprintf_error(end - pos, ret))
12915 			return pos - buf;
12916 		pos += ret;
12917 	} else if (global->p2p) {
12918 		ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
12919 		if (os_snprintf_error(end - pos, ret))
12920 			return pos - buf;
12921 		pos += ret;
12922 	}
12923 #endif /* CONFIG_P2P */
12924 
12925 #ifdef CONFIG_WIFI_DISPLAY
12926 	ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
12927 			  !!global->wifi_display);
12928 	if (os_snprintf_error(end - pos, ret))
12929 		return pos - buf;
12930 	pos += ret;
12931 #endif /* CONFIG_WIFI_DISPLAY */
12932 
12933 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
12934 		ret = os_snprintf(pos, end - pos, "ifname=%s\n"
12935 				  "address=" MACSTR "\n",
12936 				  wpa_s->ifname, MAC2STR(wpa_s->own_addr));
12937 		if (os_snprintf_error(end - pos, ret))
12938 			return pos - buf;
12939 		pos += ret;
12940 	}
12941 
12942 	return pos - buf;
12943 }
12944 
12945 
12946 #ifdef CONFIG_FST
12947 
wpas_global_ctrl_iface_fst_attach(struct wpa_global * global,char * cmd,char * buf,size_t reply_size)12948 static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
12949 					     char *cmd, char *buf,
12950 					     size_t reply_size)
12951 {
12952 	char ifname[IFNAMSIZ + 1];
12953 	struct fst_iface_cfg cfg;
12954 	struct wpa_supplicant *wpa_s;
12955 	struct fst_wpa_obj iface_obj;
12956 
12957 	if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
12958 		wpa_s = wpa_supplicant_get_iface(global, ifname);
12959 		if (wpa_s) {
12960 			if (wpa_s->fst) {
12961 				wpa_printf(MSG_INFO, "FST: Already attached");
12962 				return -1;
12963 			}
12964 			fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
12965 			wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
12966 						&iface_obj, &cfg);
12967 			if (wpa_s->fst)
12968 				return os_snprintf(buf, reply_size, "OK\n");
12969 		}
12970 	}
12971 
12972 	return -1;
12973 }
12974 
12975 
wpas_global_ctrl_iface_fst_detach(struct wpa_global * global,char * cmd,char * buf,size_t reply_size)12976 static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
12977 					     char *cmd, char *buf,
12978 					     size_t reply_size)
12979 {
12980 	char ifname[IFNAMSIZ + 1];
12981 	struct wpa_supplicant *wpa_s;
12982 
12983 	if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
12984 		wpa_s = wpa_supplicant_get_iface(global, ifname);
12985 		if (wpa_s) {
12986 			if (!fst_iface_detach(ifname)) {
12987 				wpa_s->fst = NULL;
12988 				return os_snprintf(buf, reply_size, "OK\n");
12989 			}
12990 		}
12991 	}
12992 
12993 	return -1;
12994 }
12995 
12996 #endif /* CONFIG_FST */
12997 
12998 
wpa_supplicant_global_ctrl_iface_process(struct wpa_global * global,char * buf,size_t * resp_len)12999 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
13000 						char *buf, size_t *resp_len)
13001 {
13002 	char *reply;
13003 	const int reply_size = 2048;
13004 	int reply_len;
13005 	int level = MSG_DEBUG;
13006 
13007 	if (os_strncmp(buf, "IFNAME=", 7) == 0) {
13008 		char *pos = os_strchr(buf + 7, ' ');
13009 		if (pos) {
13010 			*pos++ = '\0';
13011 			return wpas_global_ctrl_iface_ifname(global,
13012 							     buf + 7, pos,
13013 							     resp_len);
13014 		}
13015 	}
13016 
13017 	reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
13018 	if (reply)
13019 		return reply;
13020 
13021 	if (os_strcmp(buf, "PING") == 0)
13022 		level = MSG_EXCESSIVE;
13023 	wpa_hexdump_ascii(level, "RX global ctrl_iface",
13024 			  (const u8 *) buf, os_strlen(buf));
13025 
13026 	reply = os_malloc(reply_size);
13027 	if (reply == NULL) {
13028 		*resp_len = 1;
13029 		return NULL;
13030 	}
13031 
13032 	os_memcpy(reply, "OK\n", 3);
13033 	reply_len = 3;
13034 
13035 	if (os_strcmp(buf, "PING") == 0) {
13036 		os_memcpy(reply, "PONG\n", 5);
13037 		reply_len = 5;
13038 	} else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
13039 		if (wpa_supplicant_global_iface_add(global, buf + 14))
13040 			reply_len = -1;
13041 	} else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
13042 		if (wpa_supplicant_global_iface_remove(global, buf + 17))
13043 			reply_len = -1;
13044 	} else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
13045 		reply_len = wpa_supplicant_global_iface_list(
13046 			global, reply, reply_size);
13047 	} else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
13048 		reply_len = wpa_supplicant_global_iface_interfaces(
13049 			global, buf + 10, reply, reply_size);
13050 #ifdef CONFIG_FST
13051 	} else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
13052 		reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
13053 							      reply,
13054 							      reply_size);
13055 	} else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
13056 		reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
13057 							      reply,
13058 							      reply_size);
13059 	} else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
13060 		reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
13061 #endif /* CONFIG_FST */
13062 	} else if (os_strcmp(buf, "TERMINATE") == 0) {
13063 		wpa_supplicant_terminate_proc(global);
13064 	} else if (os_strcmp(buf, "SUSPEND") == 0) {
13065 		wpas_notify_suspend(global);
13066 	} else if (os_strcmp(buf, "RESUME") == 0) {
13067 		wpas_notify_resume(global);
13068 	} else if (os_strncmp(buf, "SET ", 4) == 0) {
13069 		if (wpas_global_ctrl_iface_set(global, buf + 4)) {
13070 #ifdef CONFIG_P2P
13071 			if (global->p2p_init_wpa_s) {
13072 				os_free(reply);
13073 				/* Check if P2P redirection would work for this
13074 				 * command. */
13075 				return wpa_supplicant_ctrl_iface_process(
13076 					global->p2p_init_wpa_s,
13077 					buf, resp_len);
13078 			}
13079 #endif /* CONFIG_P2P */
13080 			reply_len = -1;
13081 		}
13082 	} else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
13083 		if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
13084 			reply_len = -1;
13085 #ifndef CONFIG_NO_CONFIG_WRITE
13086 	} else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
13087 		if (wpas_global_ctrl_iface_save_config(global))
13088 			reply_len = -1;
13089 #endif /* CONFIG_NO_CONFIG_WRITE */
13090 	} else if (os_strcmp(buf, "STATUS") == 0) {
13091 		reply_len = wpas_global_ctrl_iface_status(global, reply,
13092 							  reply_size);
13093 #ifdef CONFIG_MODULE_TESTS
13094 	} else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
13095 		if (wpas_module_tests() < 0)
13096 			reply_len = -1;
13097 #endif /* CONFIG_MODULE_TESTS */
13098 	} else if (os_strncmp(buf, "RELOG", 5) == 0) {
13099 		if (wpa_debug_reopen_file() < 0)
13100 			reply_len = -1;
13101 	} else {
13102 		os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
13103 		reply_len = 16;
13104 	}
13105 
13106 	if (reply_len < 0) {
13107 		os_memcpy(reply, "FAIL\n", 5);
13108 		reply_len = 5;
13109 	}
13110 
13111 	*resp_len = reply_len;
13112 	return reply;
13113 }
13114