• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * WPA Supplicant / Control interface (shared code for all backends)
3  * Copyright (c) 2004-2020, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 #ifdef CONFIG_TESTING_OPTIONS
11 #include <netinet/ip.h>
12 #endif /* CONFIG_TESTING_OPTIONS */
13 
14 #include "utils/common.h"
15 #include "utils/eloop.h"
16 #include "utils/uuid.h"
17 #include "utils/module_tests.h"
18 #include "common/version.h"
19 #include "common/ieee802_11_defs.h"
20 #include "common/ieee802_11_common.h"
21 #include "common/wpa_ctrl.h"
22 #ifdef CONFIG_DPP
23 #include "common/dpp.h"
24 #endif /* CONFIG_DPP */
25 #include "crypto/tls.h"
26 #include "ap/hostapd.h"
27 #include "eap_peer/eap.h"
28 #include "eapol_supp/eapol_supp_sm.h"
29 #include "rsn_supp/wpa.h"
30 #include "rsn_supp/preauth.h"
31 #include "rsn_supp/pmksa_cache.h"
32 #include "l2_packet/l2_packet.h"
33 #include "wps/wps.h"
34 #include "fst/fst.h"
35 #include "fst/fst_ctrl_iface.h"
36 #include "config.h"
37 #include "wpa_supplicant_i.h"
38 #include "driver_i.h"
39 #include "wps_supplicant.h"
40 #include "ibss_rsn.h"
41 #include "ap.h"
42 #include "p2p_supplicant.h"
43 #include "p2p/p2p.h"
44 #include "hs20_supplicant.h"
45 #include "wifi_display.h"
46 #include "notify.h"
47 #include "bss.h"
48 #include "scan.h"
49 #include "ctrl_iface.h"
50 #include "interworking.h"
51 #include "blacklist.h"
52 #include "autoscan.h"
53 #include "wnm_sta.h"
54 #include "offchannel.h"
55 #include "drivers/driver.h"
56 #include "mesh.h"
57 #include "dpp_supplicant.h"
58 #include "sme.h"
59 
60 #ifdef __NetBSD__
61 #include <net/if_ether.h>
62 #elif !defined(__CYGWIN__) && !defined(CONFIG_NATIVE_WINDOWS)
63 #include <net/ethernet.h>
64 #endif
65 
66 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
67 					    char *buf, int len);
68 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
69 						  const char *input,
70 						  char *buf, int len);
71 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
72 					char *val);
73 
74 
set_bssid_filter(struct wpa_supplicant * wpa_s,char * val)75 static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
76 {
77 	char *pos;
78 	u8 addr[ETH_ALEN], *filter = NULL, *n;
79 	size_t count = 0;
80 
81 	pos = val;
82 	while (pos) {
83 		if (*pos == '\0')
84 			break;
85 		if (hwaddr_aton(pos, addr)) {
86 			os_free(filter);
87 			return -1;
88 		}
89 		n = os_realloc_array(filter, count + 1, ETH_ALEN);
90 		if (n == NULL) {
91 			os_free(filter);
92 			return -1;
93 		}
94 		filter = n;
95 		os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
96 		count++;
97 
98 		pos = os_strchr(pos, ' ');
99 		if (pos)
100 			pos++;
101 	}
102 
103 	wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
104 	os_free(wpa_s->bssid_filter);
105 	wpa_s->bssid_filter = filter;
106 	wpa_s->bssid_filter_count = count;
107 
108 	return 0;
109 }
110 
111 
set_disallow_aps(struct wpa_supplicant * wpa_s,char * val)112 static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
113 {
114 	char *pos;
115 	u8 addr[ETH_ALEN], *bssid = NULL, *n;
116 	struct wpa_ssid_value *ssid = NULL, *ns;
117 	size_t count = 0, ssid_count = 0;
118 	struct wpa_ssid *c;
119 
120 	/*
121 	 * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
122 	 * SSID_SPEC ::= ssid <SSID_HEX>
123 	 * BSSID_SPEC ::= bssid <BSSID_HEX>
124 	 */
125 
126 	pos = val;
127 	while (pos) {
128 		if (*pos == '\0')
129 			break;
130 		if (os_strncmp(pos, "bssid ", 6) == 0) {
131 			int res;
132 			pos += 6;
133 			res = hwaddr_aton2(pos, addr);
134 			if (res < 0) {
135 				os_free(ssid);
136 				os_free(bssid);
137 				wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
138 					   "BSSID value '%s'", pos);
139 				return -1;
140 			}
141 			pos += res;
142 			n = os_realloc_array(bssid, count + 1, ETH_ALEN);
143 			if (n == NULL) {
144 				os_free(ssid);
145 				os_free(bssid);
146 				return -1;
147 			}
148 			bssid = n;
149 			os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
150 			count++;
151 		} else if (os_strncmp(pos, "ssid ", 5) == 0) {
152 			char *end;
153 			pos += 5;
154 
155 			end = pos;
156 			while (*end) {
157 				if (*end == '\0' || *end == ' ')
158 					break;
159 				end++;
160 			}
161 
162 			ns = os_realloc_array(ssid, ssid_count + 1,
163 					      sizeof(struct wpa_ssid_value));
164 			if (ns == NULL) {
165 				os_free(ssid);
166 				os_free(bssid);
167 				return -1;
168 			}
169 			ssid = ns;
170 
171 			if ((end - pos) & 0x01 ||
172 			    end - pos > 2 * SSID_MAX_LEN ||
173 			    hexstr2bin(pos, ssid[ssid_count].ssid,
174 				       (end - pos) / 2) < 0) {
175 				os_free(ssid);
176 				os_free(bssid);
177 				wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
178 					   "SSID value '%s'", pos);
179 				return -1;
180 			}
181 			ssid[ssid_count].ssid_len = (end - pos) / 2;
182 			wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
183 					  ssid[ssid_count].ssid,
184 					  ssid[ssid_count].ssid_len);
185 			ssid_count++;
186 			pos = end;
187 		} else {
188 			wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
189 				   "'%s'", pos);
190 			os_free(ssid);
191 			os_free(bssid);
192 			return -1;
193 		}
194 
195 		pos = os_strchr(pos, ' ');
196 		if (pos)
197 			pos++;
198 	}
199 
200 	wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
201 	os_free(wpa_s->disallow_aps_bssid);
202 	wpa_s->disallow_aps_bssid = bssid;
203 	wpa_s->disallow_aps_bssid_count = count;
204 
205 	wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
206 	os_free(wpa_s->disallow_aps_ssid);
207 	wpa_s->disallow_aps_ssid = ssid;
208 	wpa_s->disallow_aps_ssid_count = ssid_count;
209 
210 	if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
211 		return 0;
212 
213 	c = wpa_s->current_ssid;
214 	if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
215 		return 0;
216 
217 	if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
218 	    !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
219 		return 0;
220 
221 	wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
222 		   "because current AP was marked disallowed");
223 
224 #ifdef CONFIG_SME
225 	wpa_s->sme.prev_bssid_set = 0;
226 #endif /* CONFIG_SME */
227 	wpa_s->reassociate = 1;
228 	wpa_s->own_disconnect_req = 1;
229 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
230 	wpa_supplicant_req_scan(wpa_s, 0, 0);
231 
232 	return 0;
233 }
234 
235 
236 #ifndef CONFIG_NO_CONFIG_BLOBS
wpas_ctrl_set_blob(struct wpa_supplicant * wpa_s,char * pos)237 static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
238 {
239 	char *name = pos;
240 	struct wpa_config_blob *blob;
241 	size_t len;
242 
243 	pos = os_strchr(pos, ' ');
244 	if (pos == NULL)
245 		return -1;
246 	*pos++ = '\0';
247 	len = os_strlen(pos);
248 	if (len & 1)
249 		return -1;
250 
251 	wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
252 	blob = os_zalloc(sizeof(*blob));
253 	if (blob == NULL)
254 		return -1;
255 	blob->name = os_strdup(name);
256 	blob->data = os_malloc(len / 2);
257 	if (blob->name == NULL || blob->data == NULL) {
258 		wpa_config_free_blob(blob);
259 		return -1;
260 	}
261 
262 	if (hexstr2bin(pos, blob->data, len / 2) < 0) {
263 		wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
264 		wpa_config_free_blob(blob);
265 		return -1;
266 	}
267 	blob->len = len / 2;
268 
269 	wpa_config_set_blob(wpa_s->conf, blob);
270 
271 	return 0;
272 }
273 #endif /* CONFIG_NO_CONFIG_BLOBS */
274 
275 
wpas_ctrl_pno(struct wpa_supplicant * wpa_s,char * cmd)276 static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
277 {
278 	char *params;
279 	char *pos;
280 	int *freqs = NULL;
281 	int ret;
282 
283 	if (atoi(cmd)) {
284 		params = os_strchr(cmd, ' ');
285 		os_free(wpa_s->manual_sched_scan_freqs);
286 		if (params) {
287 			params++;
288 			pos = os_strstr(params, "freq=");
289 			if (pos)
290 				freqs = freq_range_to_channel_list(wpa_s,
291 								   pos + 5);
292 		}
293 		wpa_s->manual_sched_scan_freqs = freqs;
294 		ret = wpas_start_pno(wpa_s);
295 	} else {
296 		ret = wpas_stop_pno(wpa_s);
297 	}
298 	return ret;
299 }
300 
301 
wpas_ctrl_set_band(struct wpa_supplicant * wpa_s,char * band)302 static int wpas_ctrl_set_band(struct wpa_supplicant *wpa_s, char *band)
303 {
304 	union wpa_event_data event;
305 
306 	if (os_strcmp(band, "AUTO") == 0)
307 		wpa_s->setband = WPA_SETBAND_AUTO;
308 	else if (os_strcmp(band, "5G") == 0)
309 		wpa_s->setband = WPA_SETBAND_5G;
310 	else if (os_strcmp(band, "2G") == 0)
311 		wpa_s->setband = WPA_SETBAND_2G;
312 	else
313 		return -1;
314 
315 	if (wpa_drv_setband(wpa_s, wpa_s->setband) == 0) {
316 		os_memset(&event, 0, sizeof(event));
317 		event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
318 		event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
319 		wpa_supplicant_event(wpa_s, EVENT_CHANNEL_LIST_CHANGED, &event);
320 	}
321 
322 	return 0;
323 }
324 
325 
wpas_ctrl_iface_set_lci(struct wpa_supplicant * wpa_s,const char * cmd)326 static int wpas_ctrl_iface_set_lci(struct wpa_supplicant *wpa_s,
327 				   const char *cmd)
328 {
329 	struct wpabuf *lci;
330 
331 	if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
332 		wpabuf_free(wpa_s->lci);
333 		wpa_s->lci = NULL;
334 		return 0;
335 	}
336 
337 	lci = wpabuf_parse_bin(cmd);
338 	if (!lci)
339 		return -1;
340 
341 	if (os_get_reltime(&wpa_s->lci_time)) {
342 		wpabuf_free(lci);
343 		return -1;
344 	}
345 
346 	wpabuf_free(wpa_s->lci);
347 	wpa_s->lci = lci;
348 
349 	return 0;
350 }
351 
352 
353 static int
wpas_ctrl_set_relative_rssi(struct wpa_supplicant * wpa_s,const char * cmd)354 wpas_ctrl_set_relative_rssi(struct wpa_supplicant *wpa_s, const char *cmd)
355 {
356 	int relative_rssi;
357 
358 	if (os_strcmp(cmd, "disable") == 0) {
359 		wpa_s->srp.relative_rssi_set = 0;
360 		return 0;
361 	}
362 
363 	relative_rssi = atoi(cmd);
364 	if (relative_rssi < 0 || relative_rssi > 100)
365 		return -1;
366 	wpa_s->srp.relative_rssi = relative_rssi;
367 	wpa_s->srp.relative_rssi_set = 1;
368 	return 0;
369 }
370 
371 
wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant * wpa_s,const char * cmd)372 static int wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant *wpa_s,
373 					      const char *cmd)
374 {
375 	char *pos;
376 	int adjust_rssi;
377 
378 	/* <band>:adjust_value */
379 	pos = os_strchr(cmd, ':');
380 	if (!pos)
381 		return -1;
382 	pos++;
383 	adjust_rssi = atoi(pos);
384 	if (adjust_rssi < -100 || adjust_rssi > 100)
385 		return -1;
386 
387 	if (os_strncmp(cmd, "2G", 2) == 0)
388 		wpa_s->srp.relative_adjust_band = WPA_SETBAND_2G;
389 	else if (os_strncmp(cmd, "5G", 2) == 0)
390 		wpa_s->srp.relative_adjust_band = WPA_SETBAND_5G;
391 	else
392 		return -1;
393 
394 	wpa_s->srp.relative_adjust_rssi = adjust_rssi;
395 
396 	return 0;
397 }
398 
399 
wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant * wpa_s,const char * cmd)400 static int wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant *wpa_s,
401 				   const char *cmd)
402 {
403 	struct wpabuf *ric_ies;
404 
405 	if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
406 		wpabuf_free(wpa_s->ric_ies);
407 		wpa_s->ric_ies = NULL;
408 		return 0;
409 	}
410 
411 	ric_ies = wpabuf_parse_bin(cmd);
412 	if (!ric_ies)
413 		return -1;
414 
415 	wpabuf_free(wpa_s->ric_ies);
416 	wpa_s->ric_ies = ric_ies;
417 
418 	return 0;
419 }
420 
421 
422 #ifdef CONFIG_TESTING_OPTIONS
wpas_ctrl_iface_set_dso(struct wpa_supplicant * wpa_s,const char * val)423 static int wpas_ctrl_iface_set_dso(struct wpa_supplicant *wpa_s,
424 				   const char *val)
425 {
426 	u8 bssid[ETH_ALEN];
427 	const char *pos = val;
428 	struct driver_signal_override *dso = NULL, *tmp, parsed;
429 
430 	if (hwaddr_aton(pos, bssid))
431 		return -1;
432 	pos = os_strchr(pos, ' ');
433 
434 	dl_list_for_each(tmp, &wpa_s->drv_signal_override,
435 			 struct driver_signal_override, list) {
436 		if (os_memcmp(bssid, tmp->bssid, ETH_ALEN) == 0) {
437 			dso = tmp;
438 			break;
439 		}
440 	}
441 
442 	if (!pos) {
443 		/* Remove existing entry */
444 		if (dso) {
445 			dl_list_del(&dso->list);
446 			os_free(dso);
447 		}
448 		return 0;
449 	}
450 	pos++;
451 
452 	/* Update an existing entry or add a new one */
453 	os_memset(&parsed, 0, sizeof(parsed));
454 	if (sscanf(pos, "%d %d %d %d %d",
455 		   &parsed.si_current_signal,
456 		   &parsed.si_avg_signal,
457 		   &parsed.si_avg_beacon_signal,
458 		   &parsed.si_current_noise,
459 		   &parsed.scan_level) != 5)
460 		return -1;
461 
462 	if (!dso) {
463 		dso = os_zalloc(sizeof(*dso));
464 		if (!dso)
465 			return -1;
466 		os_memcpy(dso->bssid, bssid, ETH_ALEN);
467 		dl_list_add(&wpa_s->drv_signal_override, &dso->list);
468 	}
469 	dso->si_current_signal = parsed.si_current_signal;
470 	dso->si_avg_signal = parsed.si_avg_signal;
471 	dso->si_avg_beacon_signal = parsed.si_avg_beacon_signal;
472 	dso->si_current_noise = parsed.si_current_noise;
473 	dso->scan_level = parsed.scan_level;
474 
475 	return 0;
476 }
477 #endif /* CONFIG_TESTING_OPTIONS */
478 
479 
wpa_supplicant_ctrl_iface_set(struct wpa_supplicant * wpa_s,char * cmd)480 static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
481 					 char *cmd)
482 {
483 	char *value;
484 	int ret = 0;
485 
486 	value = os_strchr(cmd, ' ');
487 	if (value == NULL)
488 		return -1;
489 	*value++ = '\0';
490 
491 	wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
492 	if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
493 		eapol_sm_configure(wpa_s->eapol,
494 				   atoi(value), -1, -1, -1);
495 	} else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
496 		eapol_sm_configure(wpa_s->eapol,
497 				   -1, atoi(value), -1, -1);
498 	} else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
499 		eapol_sm_configure(wpa_s->eapol,
500 				   -1, -1, atoi(value), -1);
501 	} else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
502 		eapol_sm_configure(wpa_s->eapol,
503 				   -1, -1, -1, atoi(value));
504 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
505 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
506 				     atoi(value))) {
507 			ret = -1;
508 		} else {
509 			value[-1] = '=';
510 			wpa_config_process_global(wpa_s->conf, cmd, -1);
511 		}
512 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
513 		   0) {
514 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
515 				     atoi(value))) {
516 			ret = -1;
517 		} else {
518 			value[-1] = '=';
519 			wpa_config_process_global(wpa_s->conf, cmd, -1);
520 		}
521 	} else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
522 		if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
523 				     atoi(value))) {
524 			ret = -1;
525 		} else {
526 			value[-1] = '=';
527 			wpa_config_process_global(wpa_s->conf, cmd, -1);
528 		}
529 	} else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
530 		wpa_s->wps_fragment_size = atoi(value);
531 #ifdef CONFIG_WPS_TESTING
532 	} else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
533 		long int val;
534 		val = strtol(value, NULL, 0);
535 		if (val < 0 || val > 0xff) {
536 			ret = -1;
537 			wpa_printf(MSG_DEBUG, "WPS: Invalid "
538 				   "wps_version_number %ld", val);
539 		} else {
540 			wps_version_number = val;
541 			wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
542 				   "version %u.%u",
543 				   (wps_version_number & 0xf0) >> 4,
544 				   wps_version_number & 0x0f);
545 		}
546 	} else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
547 		wps_testing_dummy_cred = atoi(value);
548 		wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
549 			   wps_testing_dummy_cred);
550 	} else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
551 		wps_corrupt_pkhash = atoi(value);
552 		wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
553 			   wps_corrupt_pkhash);
554 	} else if (os_strcasecmp(cmd, "wps_force_auth_types") == 0) {
555 		if (value[0] == '\0') {
556 			wps_force_auth_types_in_use = 0;
557 		} else {
558 			wps_force_auth_types = strtol(value, NULL, 0);
559 			wps_force_auth_types_in_use = 1;
560 		}
561 	} else if (os_strcasecmp(cmd, "wps_force_encr_types") == 0) {
562 		if (value[0] == '\0') {
563 			wps_force_encr_types_in_use = 0;
564 		} else {
565 			wps_force_encr_types = strtol(value, NULL, 0);
566 			wps_force_encr_types_in_use = 1;
567 		}
568 #endif /* CONFIG_WPS_TESTING */
569 	} else if (os_strcasecmp(cmd, "ampdu") == 0) {
570 		if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
571 			ret = -1;
572 #ifdef CONFIG_TDLS
573 #ifdef CONFIG_TDLS_TESTING
574 	} else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
575 		tdls_testing = strtol(value, NULL, 0);
576 		wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
577 #endif /* CONFIG_TDLS_TESTING */
578 	} else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
579 		int disabled = atoi(value);
580 		wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
581 		if (disabled) {
582 			if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
583 				ret = -1;
584 		} else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
585 			ret = -1;
586 		wpa_tdls_enable(wpa_s->wpa, !disabled);
587 #endif /* CONFIG_TDLS */
588 	} else if (os_strcasecmp(cmd, "pno") == 0) {
589 		ret = wpas_ctrl_pno(wpa_s, value);
590 	} else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
591 		int disabled = atoi(value);
592 		if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
593 			ret = -1;
594 		else if (disabled)
595 			wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
596 	} else if (os_strcasecmp(cmd, "uapsd") == 0) {
597 		if (os_strcmp(value, "disable") == 0)
598 			wpa_s->set_sta_uapsd = 0;
599 		else {
600 			int be, bk, vi, vo;
601 			char *pos;
602 			/* format: BE,BK,VI,VO;max SP Length */
603 			be = atoi(value);
604 			pos = os_strchr(value, ',');
605 			if (pos == NULL)
606 				return -1;
607 			pos++;
608 			bk = atoi(pos);
609 			pos = os_strchr(pos, ',');
610 			if (pos == NULL)
611 				return -1;
612 			pos++;
613 			vi = atoi(pos);
614 			pos = os_strchr(pos, ',');
615 			if (pos == NULL)
616 				return -1;
617 			pos++;
618 			vo = atoi(pos);
619 			/* ignore max SP Length for now */
620 
621 			wpa_s->set_sta_uapsd = 1;
622 			wpa_s->sta_uapsd = 0;
623 			if (be)
624 				wpa_s->sta_uapsd |= BIT(0);
625 			if (bk)
626 				wpa_s->sta_uapsd |= BIT(1);
627 			if (vi)
628 				wpa_s->sta_uapsd |= BIT(2);
629 			if (vo)
630 				wpa_s->sta_uapsd |= BIT(3);
631 		}
632 	} else if (os_strcasecmp(cmd, "ps") == 0) {
633 		ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
634 #ifdef CONFIG_WIFI_DISPLAY
635 	} else if (os_strcasecmp(cmd, "wifi_display") == 0) {
636 		int enabled = !!atoi(value);
637 		if (enabled && !wpa_s->global->p2p)
638 			ret = -1;
639 		else
640 			wifi_display_enable(wpa_s->global, enabled);
641 #endif /* CONFIG_WIFI_DISPLAY */
642 	} else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
643 		ret = set_bssid_filter(wpa_s, value);
644 	} else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
645 		ret = set_disallow_aps(wpa_s, value);
646 	} else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
647 		wpa_s->no_keep_alive = !!atoi(value);
648 #ifdef CONFIG_DPP
649 	} else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
650 		os_free(wpa_s->dpp_configurator_params);
651 		wpa_s->dpp_configurator_params = os_strdup(value);
652 	} else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
653 		wpa_s->dpp_init_max_tries = atoi(value);
654 	} else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
655 		wpa_s->dpp_init_retry_time = atoi(value);
656 	} else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
657 		wpa_s->dpp_resp_wait_time = atoi(value);
658 	} else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
659 		wpa_s->dpp_resp_max_tries = atoi(value);
660 	} else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
661 		wpa_s->dpp_resp_retry_time = atoi(value);
662 #ifdef CONFIG_TESTING_OPTIONS
663 	} else if (os_strcasecmp(cmd, "dpp_pkex_own_mac_override") == 0) {
664 		if (hwaddr_aton(value, dpp_pkex_own_mac_override))
665 			ret = -1;
666 	} else if (os_strcasecmp(cmd, "dpp_pkex_peer_mac_override") == 0) {
667 		if (hwaddr_aton(value, dpp_pkex_peer_mac_override))
668 			ret = -1;
669 	} else if (os_strcasecmp(cmd, "dpp_pkex_ephemeral_key_override") == 0) {
670 		size_t hex_len = os_strlen(value);
671 
672 		if (hex_len >
673 		    2 * sizeof(dpp_pkex_ephemeral_key_override))
674 			ret = -1;
675 		else if (hexstr2bin(value, dpp_pkex_ephemeral_key_override,
676 				    hex_len / 2))
677 			ret = -1;
678 		else
679 			dpp_pkex_ephemeral_key_override_len = hex_len / 2;
680 	} else if (os_strcasecmp(cmd, "dpp_protocol_key_override") == 0) {
681 		size_t hex_len = os_strlen(value);
682 
683 		if (hex_len > 2 * sizeof(dpp_protocol_key_override))
684 			ret = -1;
685 		else if (hexstr2bin(value, dpp_protocol_key_override,
686 				    hex_len / 2))
687 			ret = -1;
688 		else
689 			dpp_protocol_key_override_len = hex_len / 2;
690 	} else if (os_strcasecmp(cmd, "dpp_nonce_override") == 0) {
691 		size_t hex_len = os_strlen(value);
692 
693 		if (hex_len > 2 * sizeof(dpp_nonce_override))
694 			ret = -1;
695 		else if (hexstr2bin(value, dpp_nonce_override, hex_len / 2))
696 			ret = -1;
697 		else
698 			dpp_nonce_override_len = hex_len / 2;
699 	} else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
700 		dpp_version_override = atoi(value);
701 #endif /* CONFIG_TESTING_OPTIONS */
702 #endif /* CONFIG_DPP */
703 #ifdef CONFIG_TESTING_OPTIONS
704 	} else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
705 		wpa_s->ext_mgmt_frame_handling = !!atoi(value);
706 	} else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
707 		wpa_s->ext_eapol_frame_io = !!atoi(value);
708 #ifdef CONFIG_AP
709 		if (wpa_s->ap_iface) {
710 			wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
711 				wpa_s->ext_eapol_frame_io;
712 		}
713 #endif /* CONFIG_AP */
714 	} else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
715 		wpa_s->extra_roc_dur = atoi(value);
716 	} else if (os_strcasecmp(cmd, "test_failure") == 0) {
717 		wpa_s->test_failure = atoi(value);
718 	} else if (os_strcasecmp(cmd, "p2p_go_csa_on_inv") == 0) {
719 		wpa_s->p2p_go_csa_on_inv = !!atoi(value);
720 	} else if (os_strcasecmp(cmd, "ignore_auth_resp") == 0) {
721 		wpa_s->ignore_auth_resp = !!atoi(value);
722 	} else if (os_strcasecmp(cmd, "ignore_assoc_disallow") == 0) {
723 		wpa_s->ignore_assoc_disallow = !!atoi(value);
724 		wpa_drv_ignore_assoc_disallow(wpa_s,
725 					      wpa_s->ignore_assoc_disallow);
726 	} else if (os_strcasecmp(cmd, "disable_sa_query") == 0) {
727 		wpa_s->disable_sa_query = !!atoi(value);
728 	} else if (os_strcasecmp(cmd, "ignore_sae_h2e_only") == 0) {
729 		wpa_s->ignore_sae_h2e_only = !!atoi(value);
730 	} else if (os_strcasecmp(cmd, "extra_sae_rejected_groups") == 0) {
731 		char *pos;
732 
733 		os_free(wpa_s->extra_sae_rejected_groups);
734 		wpa_s->extra_sae_rejected_groups = NULL;
735 		pos = value;
736 		while (pos && pos[0]) {
737 			int group;
738 
739 			group = atoi(pos);
740 			wpa_printf(MSG_DEBUG,
741 				   "TESTING: Extra rejection of SAE group %d",
742 				   group);
743 			if (group)
744 				int_array_add_unique(
745 					&wpa_s->extra_sae_rejected_groups,
746 					group);
747 			pos = os_strchr(pos, ' ');
748 			if (!pos)
749 				break;
750 			pos++;
751 		}
752 	} else if (os_strcasecmp(cmd, "ft_rsnxe_used") == 0) {
753 		wpa_s->ft_rsnxe_used = atoi(value);
754 	} else if (os_strcasecmp(cmd, "rsne_override_eapol") == 0) {
755 		wpabuf_free(wpa_s->rsne_override_eapol);
756 		if (os_strcmp(value, "NULL") == 0)
757 			wpa_s->rsne_override_eapol = NULL;
758 		else
759 			wpa_s->rsne_override_eapol = wpabuf_parse_bin(value);
760 	} else if (os_strcasecmp(cmd, "rsnxe_override_assoc") == 0) {
761 		wpabuf_free(wpa_s->rsnxe_override_assoc);
762 		if (os_strcmp(value, "NULL") == 0)
763 			wpa_s->rsnxe_override_assoc = NULL;
764 		else
765 			wpa_s->rsnxe_override_assoc = wpabuf_parse_bin(value);
766 	} else if (os_strcasecmp(cmd, "rsnxe_override_eapol") == 0) {
767 		wpabuf_free(wpa_s->rsnxe_override_eapol);
768 		if (os_strcmp(value, "NULL") == 0)
769 			wpa_s->rsnxe_override_eapol = NULL;
770 		else
771 			wpa_s->rsnxe_override_eapol = wpabuf_parse_bin(value);
772 	} else if (os_strcasecmp(cmd, "reject_btm_req_reason") == 0) {
773 		wpa_s->reject_btm_req_reason = atoi(value);
774 	} else if (os_strcasecmp(cmd, "get_pref_freq_list_override") == 0) {
775 		os_free(wpa_s->get_pref_freq_list_override);
776 		if (!value[0])
777 			wpa_s->get_pref_freq_list_override = NULL;
778 		else
779 			wpa_s->get_pref_freq_list_override = os_strdup(value);
780 	} else if (os_strcasecmp(cmd, "sae_commit_override") == 0) {
781 		wpabuf_free(wpa_s->sae_commit_override);
782 		if (value[0] == '\0')
783 			wpa_s->sae_commit_override = NULL;
784 		else
785 			wpa_s->sae_commit_override = wpabuf_parse_bin(value);
786 	} else if (os_strcasecmp(cmd, "driver_signal_override") == 0) {
787 		ret = wpas_ctrl_iface_set_dso(wpa_s, value);
788 #ifdef CONFIG_DPP
789 	} else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
790 		os_free(wpa_s->dpp_config_obj_override);
791 		if (value[0] == '\0')
792 			wpa_s->dpp_config_obj_override = NULL;
793 		else
794 			wpa_s->dpp_config_obj_override = os_strdup(value);
795 	} else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
796 		os_free(wpa_s->dpp_discovery_override);
797 		if (value[0] == '\0')
798 			wpa_s->dpp_discovery_override = NULL;
799 		else
800 			wpa_s->dpp_discovery_override = os_strdup(value);
801 	} else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
802 		os_free(wpa_s->dpp_groups_override);
803 		if (value[0] == '\0')
804 			wpa_s->dpp_groups_override = NULL;
805 		else
806 			wpa_s->dpp_groups_override = os_strdup(value);
807 	} else if (os_strcasecmp(cmd,
808 				 "dpp_ignore_netaccesskey_mismatch") == 0) {
809 		wpa_s->dpp_ignore_netaccesskey_mismatch = atoi(value);
810 	} else if (os_strcasecmp(cmd, "dpp_test") == 0) {
811 		dpp_test = atoi(value);
812 #endif /* CONFIG_DPP */
813 #endif /* CONFIG_TESTING_OPTIONS */
814 #ifdef CONFIG_FILS
815 	} else if (os_strcasecmp(cmd, "disable_fils") == 0) {
816 		wpa_s->disable_fils = !!atoi(value);
817 		wpa_drv_disable_fils(wpa_s, wpa_s->disable_fils);
818 		wpa_supplicant_set_default_scan_ies(wpa_s);
819 #endif /* CONFIG_FILS */
820 #ifndef CONFIG_NO_CONFIG_BLOBS
821 	} else if (os_strcmp(cmd, "blob") == 0) {
822 		ret = wpas_ctrl_set_blob(wpa_s, value);
823 #endif /* CONFIG_NO_CONFIG_BLOBS */
824 	} else if (os_strcasecmp(cmd, "setband") == 0) {
825 		ret = wpas_ctrl_set_band(wpa_s, value);
826 #ifdef CONFIG_MBO
827 	} else if (os_strcasecmp(cmd, "non_pref_chan") == 0) {
828 		ret = wpas_mbo_update_non_pref_chan(wpa_s, value);
829 		if (ret == 0) {
830 			value[-1] = '=';
831 			wpa_config_process_global(wpa_s->conf, cmd, -1);
832 		}
833 	} else if (os_strcasecmp(cmd, "mbo_cell_capa") == 0) {
834 		wpas_mbo_update_cell_capa(wpa_s, atoi(value));
835 	} else if (os_strcasecmp(cmd, "oce") == 0) {
836 		wpa_s->conf->oce = atoi(value);
837 		if (wpa_s->conf->oce) {
838 			if ((wpa_s->conf->oce & OCE_STA) &&
839 			    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_OCE_STA))
840 				wpa_s->enable_oce = OCE_STA;
841 
842 			if ((wpa_s->conf->oce & OCE_STA_CFON) &&
843 			    (wpa_s->drv_flags &
844 			     WPA_DRIVER_FLAGS_OCE_STA_CFON)) {
845 				/* TODO: Need to add STA-CFON support */
846 				wpa_printf(MSG_ERROR,
847 					   "OCE STA-CFON feature is not yet supported");
848 				return -1;
849 			}
850 		} else {
851 			wpa_s->enable_oce = 0;
852 		}
853 		wpa_supplicant_set_default_scan_ies(wpa_s);
854 #endif /* CONFIG_MBO */
855 	} else if (os_strcasecmp(cmd, "lci") == 0) {
856 		ret = wpas_ctrl_iface_set_lci(wpa_s, value);
857 	} else if (os_strcasecmp(cmd, "tdls_trigger_control") == 0) {
858 		ret = wpa_drv_set_tdls_mode(wpa_s, atoi(value));
859 	} else if (os_strcasecmp(cmd, "relative_rssi") == 0) {
860 		ret = wpas_ctrl_set_relative_rssi(wpa_s, value);
861 	} else if (os_strcasecmp(cmd, "relative_band_adjust") == 0) {
862 		ret = wpas_ctrl_set_relative_band_adjust(wpa_s, value);
863 	} else if (os_strcasecmp(cmd, "ric_ies") == 0) {
864 		ret = wpas_ctrl_iface_set_ric_ies(wpa_s, value);
865 	} else if (os_strcasecmp(cmd, "roaming") == 0) {
866 		ret = wpa_drv_roaming(wpa_s, atoi(value), NULL);
867 #ifdef CONFIG_WNM
868 	} else if (os_strcasecmp(cmd, "coloc_intf_elems") == 0) {
869 		struct wpabuf *elems;
870 
871 		elems = wpabuf_parse_bin(value);
872 		if (!elems)
873 			return -1;
874 		wnm_set_coloc_intf_elems(wpa_s, elems);
875 #endif /* CONFIG_WNM */
876 	} else {
877 		value[-1] = '=';
878 		ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
879 		if (ret == 0)
880 			wpa_supplicant_update_config(wpa_s);
881 	}
882 
883 	return ret;
884 }
885 
886 
wpa_supplicant_ctrl_iface_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)887 static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
888 					 char *cmd, char *buf, size_t buflen)
889 {
890 	int res = -1;
891 
892 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
893 
894 	if (os_strcmp(cmd, "version") == 0) {
895 		res = os_snprintf(buf, buflen, "%s", VERSION_STR);
896 	} else if (os_strcasecmp(cmd, "max_command_len") == 0) {
897 		res = os_snprintf(buf, buflen, "%u", CTRL_IFACE_MAX_LEN);
898 	} else if (os_strcasecmp(cmd, "country") == 0) {
899 		if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
900 			res = os_snprintf(buf, buflen, "%c%c",
901 					  wpa_s->conf->country[0],
902 					  wpa_s->conf->country[1]);
903 #ifdef CONFIG_WIFI_DISPLAY
904 	} else if (os_strcasecmp(cmd, "wifi_display") == 0) {
905 		int enabled;
906 		if (wpa_s->global->p2p == NULL ||
907 		    wpa_s->global->p2p_disabled)
908 			enabled = 0;
909 		else
910 			enabled = wpa_s->global->wifi_display;
911 		res = os_snprintf(buf, buflen, "%d", enabled);
912 #endif /* CONFIG_WIFI_DISPLAY */
913 #ifdef CONFIG_TESTING_GET_GTK
914 	} else if (os_strcmp(cmd, "gtk") == 0) {
915 		if (wpa_s->last_gtk_len == 0)
916 			return -1;
917 		res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
918 				       wpa_s->last_gtk_len);
919 		return res;
920 #endif /* CONFIG_TESTING_GET_GTK */
921 	} else if (os_strcmp(cmd, "tls_library") == 0) {
922 		res = tls_get_library_version(buf, buflen);
923 #ifdef CONFIG_TESTING_OPTIONS
924 	} else if (os_strcmp(cmd, "anonce") == 0) {
925 		return wpa_snprintf_hex(buf, buflen,
926 					wpa_sm_get_anonce(wpa_s->wpa),
927 					WPA_NONCE_LEN);
928 	} else if (os_strcasecmp(cmd, "last_tk_key_idx") == 0) {
929 		res = os_snprintf(buf, buflen, "%d", wpa_s->last_tk_key_idx);
930 #endif /* CONFIG_TESTING_OPTIONS */
931 	} else {
932 		res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
933 	}
934 
935 	if (os_snprintf_error(buflen, res))
936 		return -1;
937 	return res;
938 }
939 
940 
941 #ifdef IEEE8021X_EAPOL
wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant * wpa_s,char * addr)942 static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
943 					     char *addr)
944 {
945 	u8 bssid[ETH_ALEN];
946 	struct wpa_ssid *ssid = wpa_s->current_ssid;
947 
948 	if (hwaddr_aton(addr, bssid)) {
949 		wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
950 			   "'%s'", addr);
951 		return -1;
952 	}
953 
954 	wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
955 	rsn_preauth_deinit(wpa_s->wpa);
956 	if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
957 		return -1;
958 
959 	return 0;
960 }
961 #endif /* IEEE8021X_EAPOL */
962 
963 
964 #ifdef CONFIG_TDLS
965 
wpa_supplicant_ctrl_iface_tdls_discover(struct wpa_supplicant * wpa_s,char * addr)966 static int wpa_supplicant_ctrl_iface_tdls_discover(
967 	struct wpa_supplicant *wpa_s, char *addr)
968 {
969 	u8 peer[ETH_ALEN];
970 	int ret;
971 
972 	if (hwaddr_aton(addr, peer)) {
973 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
974 			   "address '%s'", addr);
975 		return -1;
976 	}
977 
978 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
979 		   MAC2STR(peer));
980 
981 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
982 		ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
983 	else
984 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
985 
986 	return ret;
987 }
988 
989 
wpa_supplicant_ctrl_iface_tdls_setup(struct wpa_supplicant * wpa_s,char * addr)990 static int wpa_supplicant_ctrl_iface_tdls_setup(
991 	struct wpa_supplicant *wpa_s, char *addr)
992 {
993 	u8 peer[ETH_ALEN];
994 	int ret;
995 
996 	if (hwaddr_aton(addr, peer)) {
997 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
998 			   "address '%s'", addr);
999 		return -1;
1000 	}
1001 
1002 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
1003 		   MAC2STR(peer));
1004 
1005 	if ((wpa_s->conf->tdls_external_control) &&
1006 	    wpa_tdls_is_external_setup(wpa_s->wpa))
1007 		return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
1008 
1009 	wpa_tdls_remove(wpa_s->wpa, peer);
1010 
1011 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1012 		ret = wpa_tdls_start(wpa_s->wpa, peer);
1013 	else
1014 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
1015 
1016 	return ret;
1017 }
1018 
1019 
wpa_supplicant_ctrl_iface_tdls_teardown(struct wpa_supplicant * wpa_s,char * addr)1020 static int wpa_supplicant_ctrl_iface_tdls_teardown(
1021 	struct wpa_supplicant *wpa_s, char *addr)
1022 {
1023 	u8 peer[ETH_ALEN];
1024 	int ret;
1025 
1026 	if (os_strcmp(addr, "*") == 0) {
1027 		/* remove everyone */
1028 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
1029 		wpa_tdls_teardown_peers(wpa_s->wpa);
1030 		return 0;
1031 	}
1032 
1033 	if (hwaddr_aton(addr, peer)) {
1034 		wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
1035 			   "address '%s'", addr);
1036 		return -1;
1037 	}
1038 
1039 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
1040 		   MAC2STR(peer));
1041 
1042 	if ((wpa_s->conf->tdls_external_control) &&
1043 	    wpa_tdls_is_external_setup(wpa_s->wpa))
1044 		return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
1045 
1046 	if (wpa_tdls_is_external_setup(wpa_s->wpa))
1047 		ret = wpa_tdls_teardown_link(
1048 			wpa_s->wpa, peer,
1049 			WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
1050 	else
1051 		ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
1052 
1053 	return ret;
1054 }
1055 
1056 
ctrl_iface_get_capability_tdls(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)1057 static int ctrl_iface_get_capability_tdls(
1058 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
1059 {
1060 	int ret;
1061 
1062 	ret = os_snprintf(buf, buflen, "%s\n",
1063 			  wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
1064 			  (wpa_s->drv_flags &
1065 			   WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
1066 			   "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
1067 	if (os_snprintf_error(buflen, ret))
1068 		return -1;
1069 	return ret;
1070 }
1071 
1072 
wpa_supplicant_ctrl_iface_tdls_chan_switch(struct wpa_supplicant * wpa_s,char * cmd)1073 static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
1074 	struct wpa_supplicant *wpa_s, char *cmd)
1075 {
1076 	u8 peer[ETH_ALEN];
1077 	struct hostapd_freq_params freq_params;
1078 	u8 oper_class;
1079 	char *pos, *end;
1080 
1081 	if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
1082 		wpa_printf(MSG_INFO,
1083 			   "tdls_chanswitch: Only supported with external setup");
1084 		return -1;
1085 	}
1086 
1087 	os_memset(&freq_params, 0, sizeof(freq_params));
1088 
1089 	pos = os_strchr(cmd, ' ');
1090 	if (pos == NULL)
1091 		return -1;
1092 	*pos++ = '\0';
1093 
1094 	oper_class = strtol(pos, &end, 10);
1095 	if (pos == end) {
1096 		wpa_printf(MSG_INFO,
1097 			   "tdls_chanswitch: Invalid op class provided");
1098 		return -1;
1099 	}
1100 
1101 	pos = end;
1102 	freq_params.freq = atoi(pos);
1103 	if (freq_params.freq == 0) {
1104 		wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
1105 		return -1;
1106 	}
1107 
1108 #define SET_FREQ_SETTING(str) \
1109 	do { \
1110 		const char *pos2 = os_strstr(pos, " " #str "="); \
1111 		if (pos2) { \
1112 			pos2 += sizeof(" " #str "=") - 1; \
1113 			freq_params.str = atoi(pos2); \
1114 		} \
1115 	} while (0)
1116 
1117 	SET_FREQ_SETTING(center_freq1);
1118 	SET_FREQ_SETTING(center_freq2);
1119 	SET_FREQ_SETTING(bandwidth);
1120 	SET_FREQ_SETTING(sec_channel_offset);
1121 #undef SET_FREQ_SETTING
1122 
1123 	freq_params.ht_enabled = !!os_strstr(pos, " ht");
1124 	freq_params.vht_enabled = !!os_strstr(pos, " vht");
1125 
1126 	if (hwaddr_aton(cmd, peer)) {
1127 		wpa_printf(MSG_DEBUG,
1128 			   "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
1129 			   cmd);
1130 		return -1;
1131 	}
1132 
1133 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
1134 		   " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
1135 		   MAC2STR(peer), oper_class, freq_params.freq,
1136 		   freq_params.center_freq1, freq_params.center_freq2,
1137 		   freq_params.bandwidth, freq_params.sec_channel_offset,
1138 		   freq_params.ht_enabled ? " HT" : "",
1139 		   freq_params.vht_enabled ? " VHT" : "");
1140 
1141 	return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
1142 					   &freq_params);
1143 }
1144 
1145 
wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(struct wpa_supplicant * wpa_s,char * cmd)1146 static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
1147 	struct wpa_supplicant *wpa_s, char *cmd)
1148 {
1149 	u8 peer[ETH_ALEN];
1150 
1151 	if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
1152 		wpa_printf(MSG_INFO,
1153 			   "tdls_chanswitch: Only supported with external setup");
1154 		return -1;
1155 	}
1156 
1157 	if (hwaddr_aton(cmd, peer)) {
1158 		wpa_printf(MSG_DEBUG,
1159 			   "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
1160 			   cmd);
1161 		return -1;
1162 	}
1163 
1164 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
1165 		   MAC2STR(peer));
1166 
1167 	return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
1168 }
1169 
1170 
wpa_supplicant_ctrl_iface_tdls_link_status(struct wpa_supplicant * wpa_s,const char * addr,char * buf,size_t buflen)1171 static int wpa_supplicant_ctrl_iface_tdls_link_status(
1172 	struct wpa_supplicant *wpa_s, const char *addr,
1173 	char *buf, size_t buflen)
1174 {
1175 	u8 peer[ETH_ALEN];
1176 	const char *tdls_status;
1177 	int ret;
1178 
1179 	if (hwaddr_aton(addr, peer)) {
1180 		wpa_printf(MSG_DEBUG,
1181 			   "CTRL_IFACE TDLS_LINK_STATUS: Invalid address '%s'",
1182 			   addr);
1183 		return -1;
1184 	}
1185 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS " MACSTR,
1186 		   MAC2STR(peer));
1187 
1188 	tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
1189 	wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS: %s", tdls_status);
1190 	ret = os_snprintf(buf, buflen, "TDLS link status: %s\n", tdls_status);
1191 	if (os_snprintf_error(buflen, ret))
1192 		return -1;
1193 
1194 	return ret;
1195 }
1196 
1197 #endif /* CONFIG_TDLS */
1198 
1199 
wmm_ac_ctrl_addts(struct wpa_supplicant * wpa_s,char * cmd)1200 static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
1201 {
1202 	char *token, *context = NULL;
1203 	struct wmm_ac_ts_setup_params params = {
1204 		.tsid = 0xff,
1205 		.direction = 0xff,
1206 	};
1207 
1208 	while ((token = str_token(cmd, " ", &context))) {
1209 		if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
1210 		    sscanf(token, "up=%i", &params.user_priority) == 1 ||
1211 		    sscanf(token, "nominal_msdu_size=%i",
1212 			   &params.nominal_msdu_size) == 1 ||
1213 		    sscanf(token, "mean_data_rate=%i",
1214 			   &params.mean_data_rate) == 1 ||
1215 		    sscanf(token, "min_phy_rate=%i",
1216 			   &params.minimum_phy_rate) == 1 ||
1217 		    sscanf(token, "sba=%i",
1218 			   &params.surplus_bandwidth_allowance) == 1)
1219 			continue;
1220 
1221 		if (os_strcasecmp(token, "downlink") == 0) {
1222 			params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
1223 		} else if (os_strcasecmp(token, "uplink") == 0) {
1224 			params.direction = WMM_TSPEC_DIRECTION_UPLINK;
1225 		} else if (os_strcasecmp(token, "bidi") == 0) {
1226 			params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
1227 		} else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
1228 			params.fixed_nominal_msdu = 1;
1229 		} else {
1230 			wpa_printf(MSG_DEBUG,
1231 				   "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
1232 				   token);
1233 			return -1;
1234 		}
1235 
1236 	}
1237 
1238 	return wpas_wmm_ac_addts(wpa_s, &params);
1239 }
1240 
1241 
wmm_ac_ctrl_delts(struct wpa_supplicant * wpa_s,char * cmd)1242 static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
1243 {
1244 	u8 tsid = atoi(cmd);
1245 
1246 	return wpas_wmm_ac_delts(wpa_s, tsid);
1247 }
1248 
1249 
1250 #ifdef CONFIG_IEEE80211R
wpa_supplicant_ctrl_iface_ft_ds(struct wpa_supplicant * wpa_s,char * addr)1251 static int wpa_supplicant_ctrl_iface_ft_ds(
1252 	struct wpa_supplicant *wpa_s, char *addr)
1253 {
1254 	u8 target_ap[ETH_ALEN];
1255 	struct wpa_bss *bss;
1256 	const u8 *mdie;
1257 
1258 	if (hwaddr_aton(addr, target_ap)) {
1259 		wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
1260 			   "address '%s'", addr);
1261 		return -1;
1262 	}
1263 
1264 	wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
1265 
1266 	bss = wpa_bss_get_bssid(wpa_s, target_ap);
1267 	if (bss)
1268 		mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1269 	else
1270 		mdie = NULL;
1271 
1272 	return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
1273 }
1274 #endif /* CONFIG_IEEE80211R */
1275 
1276 
1277 #ifdef CONFIG_WPS
wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant * wpa_s,char * cmd)1278 static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
1279 					     char *cmd)
1280 {
1281 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1282 #ifdef CONFIG_P2P
1283 	u8 p2p_dev_addr[ETH_ALEN];
1284 #endif /* CONFIG_P2P */
1285 #ifdef CONFIG_AP
1286 	u8 *_p2p_dev_addr = NULL;
1287 #endif /* CONFIG_AP */
1288 	char *pos;
1289 	int multi_ap = 0;
1290 
1291 	if (!cmd || os_strcmp(cmd, "any") == 0 ||
1292 	    os_strncmp(cmd, "any ", 4) == 0) {
1293 		_bssid = NULL;
1294 #ifdef CONFIG_P2P
1295 	} else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
1296 		if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
1297 			wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
1298 				   "P2P Device Address '%s'",
1299 				   cmd + 13);
1300 			return -1;
1301 		}
1302 		_p2p_dev_addr = p2p_dev_addr;
1303 #endif /* CONFIG_P2P */
1304 	} else if (os_strncmp(cmd, "multi_ap=", 9) == 0) {
1305 		_bssid = NULL;
1306 		multi_ap = atoi(cmd + 9);
1307 	} else if (hwaddr_aton(cmd, bssid)) {
1308 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
1309 			   cmd);
1310 		return -1;
1311 	}
1312 
1313 	if (cmd) {
1314 		pos = os_strstr(cmd, " multi_ap=");
1315 		if (pos) {
1316 			pos += 10;
1317 			multi_ap = atoi(pos);
1318 		}
1319 	}
1320 
1321 #ifdef CONFIG_AP
1322 	if (wpa_s->ap_iface)
1323 		return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
1324 #endif /* CONFIG_AP */
1325 
1326 	return wpas_wps_start_pbc(wpa_s, _bssid, 0, multi_ap);
1327 }
1328 
1329 
wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1330 static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
1331 					     char *cmd, char *buf,
1332 					     size_t buflen)
1333 {
1334 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1335 	char *pin;
1336 	int ret;
1337 
1338 	pin = os_strchr(cmd, ' ');
1339 	if (pin)
1340 		*pin++ = '\0';
1341 
1342 	if (os_strcmp(cmd, "any") == 0)
1343 		_bssid = NULL;
1344 	else if (os_strcmp(cmd, "get") == 0) {
1345 		if (wps_generate_pin((unsigned int *) &ret) < 0)
1346 			return -1;
1347 		goto done;
1348 	} else if (hwaddr_aton(cmd, bssid)) {
1349 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
1350 			   cmd);
1351 		return -1;
1352 	}
1353 
1354 #ifdef CONFIG_AP
1355 	if (wpa_s->ap_iface) {
1356 		int timeout = 0;
1357 		char *pos;
1358 
1359 		if (pin) {
1360 			pos = os_strchr(pin, ' ');
1361 			if (pos) {
1362 				*pos++ = '\0';
1363 				timeout = atoi(pos);
1364 			}
1365 		}
1366 
1367 		return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
1368 						 buf, buflen, timeout);
1369 	}
1370 #endif /* CONFIG_AP */
1371 
1372 	if (pin) {
1373 		ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
1374 					 DEV_PW_DEFAULT);
1375 		if (ret < 0)
1376 			return -1;
1377 		ret = os_snprintf(buf, buflen, "%s", pin);
1378 		if (os_snprintf_error(buflen, ret))
1379 			return -1;
1380 		return ret;
1381 	}
1382 
1383 	ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
1384 	if (ret < 0)
1385 		return -1;
1386 
1387 done:
1388 	/* Return the generated PIN */
1389 	ret = os_snprintf(buf, buflen, "%08d", ret);
1390 	if (os_snprintf_error(buflen, ret))
1391 		return -1;
1392 	return ret;
1393 }
1394 
1395 
wpa_supplicant_ctrl_iface_wps_check_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1396 static int wpa_supplicant_ctrl_iface_wps_check_pin(
1397 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
1398 {
1399 	char pin[9];
1400 	size_t len;
1401 	char *pos;
1402 	int ret;
1403 
1404 	wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
1405 			      (u8 *) cmd, os_strlen(cmd));
1406 	for (pos = cmd, len = 0; *pos != '\0'; pos++) {
1407 		if (*pos < '0' || *pos > '9')
1408 			continue;
1409 		pin[len++] = *pos;
1410 		if (len == 9) {
1411 			wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
1412 			return -1;
1413 		}
1414 	}
1415 	if (len != 4 && len != 8) {
1416 		wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
1417 		return -1;
1418 	}
1419 	pin[len] = '\0';
1420 
1421 	if (len == 8) {
1422 		unsigned int pin_val;
1423 		pin_val = atoi(pin);
1424 		if (!wps_pin_valid(pin_val)) {
1425 			wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
1426 			ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
1427 			if (os_snprintf_error(buflen, ret))
1428 				return -1;
1429 			return ret;
1430 		}
1431 	}
1432 
1433 	ret = os_snprintf(buf, buflen, "%s", pin);
1434 	if (os_snprintf_error(buflen, ret))
1435 		return -1;
1436 
1437 	return ret;
1438 }
1439 
1440 
1441 #ifdef CONFIG_WPS_NFC
1442 
wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant * wpa_s,char * cmd)1443 static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
1444 					     char *cmd)
1445 {
1446 	u8 bssid[ETH_ALEN], *_bssid = bssid;
1447 
1448 	if (cmd == NULL || cmd[0] == '\0')
1449 		_bssid = NULL;
1450 	else if (hwaddr_aton(cmd, bssid))
1451 		return -1;
1452 
1453 	return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
1454 				  0, 0);
1455 }
1456 
1457 
wpa_supplicant_ctrl_iface_wps_nfc_config_token(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1458 static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
1459 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1460 {
1461 	int ndef;
1462 	struct wpabuf *buf;
1463 	int res;
1464 	char *pos;
1465 
1466 	pos = os_strchr(cmd, ' ');
1467 	if (pos)
1468 		*pos++ = '\0';
1469 	if (os_strcmp(cmd, "WPS") == 0)
1470 		ndef = 0;
1471 	else if (os_strcmp(cmd, "NDEF") == 0)
1472 		ndef = 1;
1473 	else
1474 		return -1;
1475 
1476 	buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
1477 	if (buf == NULL)
1478 		return -1;
1479 
1480 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1481 					 wpabuf_len(buf));
1482 	reply[res++] = '\n';
1483 	reply[res] = '\0';
1484 
1485 	wpabuf_free(buf);
1486 
1487 	return res;
1488 }
1489 
1490 
wpa_supplicant_ctrl_iface_wps_nfc_token(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1491 static int wpa_supplicant_ctrl_iface_wps_nfc_token(
1492 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
1493 {
1494 	int ndef;
1495 	struct wpabuf *buf;
1496 	int res;
1497 
1498 	if (os_strcmp(cmd, "WPS") == 0)
1499 		ndef = 0;
1500 	else if (os_strcmp(cmd, "NDEF") == 0)
1501 		ndef = 1;
1502 	else
1503 		return -1;
1504 
1505 	buf = wpas_wps_nfc_token(wpa_s, ndef);
1506 	if (buf == NULL)
1507 		return -1;
1508 
1509 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1510 					 wpabuf_len(buf));
1511 	reply[res++] = '\n';
1512 	reply[res] = '\0';
1513 
1514 	wpabuf_free(buf);
1515 
1516 	return res;
1517 }
1518 
1519 
wpa_supplicant_ctrl_iface_wps_nfc_tag_read(struct wpa_supplicant * wpa_s,char * pos)1520 static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
1521 	struct wpa_supplicant *wpa_s, char *pos)
1522 {
1523 	size_t len;
1524 	struct wpabuf *buf;
1525 	int ret;
1526 	char *freq;
1527 	int forced_freq = 0;
1528 
1529 	freq = strstr(pos, " freq=");
1530 	if (freq) {
1531 		*freq = '\0';
1532 		freq += 6;
1533 		forced_freq = atoi(freq);
1534 	}
1535 
1536 	len = os_strlen(pos);
1537 	if (len & 0x01)
1538 		return -1;
1539 	len /= 2;
1540 
1541 	buf = wpabuf_alloc(len);
1542 	if (buf == NULL)
1543 		return -1;
1544 	if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
1545 		wpabuf_free(buf);
1546 		return -1;
1547 	}
1548 
1549 	ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
1550 	wpabuf_free(buf);
1551 
1552 	return ret;
1553 }
1554 
1555 
wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef)1556 static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
1557 					      char *reply, size_t max_len,
1558 					      int ndef)
1559 {
1560 	struct wpabuf *buf;
1561 	int res;
1562 
1563 	buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
1564 	if (buf == NULL)
1565 		return -1;
1566 
1567 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1568 					 wpabuf_len(buf));
1569 	reply[res++] = '\n';
1570 	reply[res] = '\0';
1571 
1572 	wpabuf_free(buf);
1573 
1574 	return res;
1575 }
1576 
1577 
1578 #ifdef CONFIG_P2P
wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef)1579 static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
1580 					      char *reply, size_t max_len,
1581 					      int ndef)
1582 {
1583 	struct wpabuf *buf;
1584 	int res;
1585 
1586 	buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
1587 	if (buf == NULL) {
1588 		wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
1589 		return -1;
1590 	}
1591 
1592 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1593 					 wpabuf_len(buf));
1594 	reply[res++] = '\n';
1595 	reply[res] = '\0';
1596 
1597 	wpabuf_free(buf);
1598 
1599 	return res;
1600 }
1601 #endif /* CONFIG_P2P */
1602 
1603 
wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1604 static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
1605 					  char *cmd, char *reply,
1606 					  size_t max_len)
1607 {
1608 	char *pos;
1609 	int ndef;
1610 
1611 	pos = os_strchr(cmd, ' ');
1612 	if (pos == NULL)
1613 		return -1;
1614 	*pos++ = '\0';
1615 
1616 	if (os_strcmp(cmd, "WPS") == 0)
1617 		ndef = 0;
1618 	else if (os_strcmp(cmd, "NDEF") == 0)
1619 		ndef = 1;
1620 	else
1621 		return -1;
1622 
1623 	if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
1624 		if (!ndef)
1625 			return -1;
1626 		return wpas_ctrl_nfc_get_handover_req_wps(
1627 			wpa_s, reply, max_len, ndef);
1628 	}
1629 
1630 #ifdef CONFIG_P2P
1631 	if (os_strcmp(pos, "P2P-CR") == 0) {
1632 		return wpas_ctrl_nfc_get_handover_req_p2p(
1633 			wpa_s, reply, max_len, ndef);
1634 	}
1635 #endif /* CONFIG_P2P */
1636 
1637 	return -1;
1638 }
1639 
1640 
wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant * wpa_s,char * reply,size_t max_len,int ndef,int cr,char * uuid)1641 static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
1642 					      char *reply, size_t max_len,
1643 					      int ndef, int cr, char *uuid)
1644 {
1645 	struct wpabuf *buf;
1646 	int res;
1647 
1648 	buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
1649 	if (buf == NULL)
1650 		return -1;
1651 
1652 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1653 					 wpabuf_len(buf));
1654 	reply[res++] = '\n';
1655 	reply[res] = '\0';
1656 
1657 	wpabuf_free(buf);
1658 
1659 	return res;
1660 }
1661 
1662 
1663 #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)1664 static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
1665 					      char *reply, size_t max_len,
1666 					      int ndef, int tag)
1667 {
1668 	struct wpabuf *buf;
1669 	int res;
1670 
1671 	buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
1672 	if (buf == NULL)
1673 		return -1;
1674 
1675 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
1676 					 wpabuf_len(buf));
1677 	reply[res++] = '\n';
1678 	reply[res] = '\0';
1679 
1680 	wpabuf_free(buf);
1681 
1682 	return res;
1683 }
1684 #endif /* CONFIG_P2P */
1685 
1686 
wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)1687 static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
1688 					  char *cmd, char *reply,
1689 					  size_t max_len)
1690 {
1691 	char *pos, *pos2;
1692 	int ndef;
1693 
1694 	pos = os_strchr(cmd, ' ');
1695 	if (pos == NULL)
1696 		return -1;
1697 	*pos++ = '\0';
1698 
1699 	if (os_strcmp(cmd, "WPS") == 0)
1700 		ndef = 0;
1701 	else if (os_strcmp(cmd, "NDEF") == 0)
1702 		ndef = 1;
1703 	else
1704 		return -1;
1705 
1706 	pos2 = os_strchr(pos, ' ');
1707 	if (pos2)
1708 		*pos2++ = '\0';
1709 	if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
1710 		if (!ndef)
1711 			return -1;
1712 		return wpas_ctrl_nfc_get_handover_sel_wps(
1713 			wpa_s, reply, max_len, ndef,
1714 			os_strcmp(pos, "WPS-CR") == 0, pos2);
1715 	}
1716 
1717 #ifdef CONFIG_P2P
1718 	if (os_strcmp(pos, "P2P-CR") == 0) {
1719 		return wpas_ctrl_nfc_get_handover_sel_p2p(
1720 			wpa_s, reply, max_len, ndef, 0);
1721 	}
1722 
1723 	if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
1724 		return wpas_ctrl_nfc_get_handover_sel_p2p(
1725 			wpa_s, reply, max_len, ndef, 1);
1726 	}
1727 #endif /* CONFIG_P2P */
1728 
1729 	return -1;
1730 }
1731 
1732 
wpas_ctrl_nfc_report_handover(struct wpa_supplicant * wpa_s,char * cmd)1733 static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
1734 					 char *cmd)
1735 {
1736 	size_t len;
1737 	struct wpabuf *req, *sel;
1738 	int ret;
1739 	char *pos, *role, *type, *pos2;
1740 #ifdef CONFIG_P2P
1741 	char *freq;
1742 	int forced_freq = 0;
1743 
1744 	freq = strstr(cmd, " freq=");
1745 	if (freq) {
1746 		*freq = '\0';
1747 		freq += 6;
1748 		forced_freq = atoi(freq);
1749 	}
1750 #endif /* CONFIG_P2P */
1751 
1752 	role = cmd;
1753 	pos = os_strchr(role, ' ');
1754 	if (pos == NULL) {
1755 		wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
1756 		return -1;
1757 	}
1758 	*pos++ = '\0';
1759 
1760 	type = pos;
1761 	pos = os_strchr(type, ' ');
1762 	if (pos == NULL) {
1763 		wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
1764 		return -1;
1765 	}
1766 	*pos++ = '\0';
1767 
1768 	pos2 = os_strchr(pos, ' ');
1769 	if (pos2 == NULL) {
1770 		wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
1771 		return -1;
1772 	}
1773 	*pos2++ = '\0';
1774 
1775 	len = os_strlen(pos);
1776 	if (len & 0x01) {
1777 		wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
1778 		return -1;
1779 	}
1780 	len /= 2;
1781 
1782 	req = wpabuf_alloc(len);
1783 	if (req == NULL) {
1784 		wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
1785 		return -1;
1786 	}
1787 	if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
1788 		wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
1789 		wpabuf_free(req);
1790 		return -1;
1791 	}
1792 
1793 	len = os_strlen(pos2);
1794 	if (len & 0x01) {
1795 		wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
1796 		wpabuf_free(req);
1797 		return -1;
1798 	}
1799 	len /= 2;
1800 
1801 	sel = wpabuf_alloc(len);
1802 	if (sel == NULL) {
1803 		wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
1804 		wpabuf_free(req);
1805 		return -1;
1806 	}
1807 	if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
1808 		wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
1809 		wpabuf_free(req);
1810 		wpabuf_free(sel);
1811 		return -1;
1812 	}
1813 
1814 	wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
1815 		   role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
1816 
1817 	if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
1818 		ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
1819 #ifdef CONFIG_AP
1820 	} else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
1821 	{
1822 		ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
1823 		if (ret < 0)
1824 			ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
1825 #endif /* CONFIG_AP */
1826 #ifdef CONFIG_P2P
1827 	} else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
1828 	{
1829 		ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
1830 	} else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
1831 	{
1832 		ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
1833 						   forced_freq);
1834 #endif /* CONFIG_P2P */
1835 	} else {
1836 		wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
1837 			   "reported: role=%s type=%s", role, type);
1838 		ret = -1;
1839 	}
1840 	wpabuf_free(req);
1841 	wpabuf_free(sel);
1842 
1843 	if (ret)
1844 		wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
1845 
1846 	return ret;
1847 }
1848 
1849 #endif /* CONFIG_WPS_NFC */
1850 
1851 
wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant * wpa_s,char * cmd)1852 static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
1853 					     char *cmd)
1854 {
1855 	u8 bssid[ETH_ALEN];
1856 	char *pin;
1857 	char *new_ssid;
1858 	char *new_auth;
1859 	char *new_encr;
1860 	char *new_key;
1861 	struct wps_new_ap_settings ap;
1862 
1863 	pin = os_strchr(cmd, ' ');
1864 	if (pin == NULL)
1865 		return -1;
1866 	*pin++ = '\0';
1867 
1868 	if (hwaddr_aton(cmd, bssid)) {
1869 		wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
1870 			   cmd);
1871 		return -1;
1872 	}
1873 
1874 	new_ssid = os_strchr(pin, ' ');
1875 	if (new_ssid == NULL)
1876 		return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
1877 	*new_ssid++ = '\0';
1878 
1879 	new_auth = os_strchr(new_ssid, ' ');
1880 	if (new_auth == NULL)
1881 		return -1;
1882 	*new_auth++ = '\0';
1883 
1884 	new_encr = os_strchr(new_auth, ' ');
1885 	if (new_encr == NULL)
1886 		return -1;
1887 	*new_encr++ = '\0';
1888 
1889 	new_key = os_strchr(new_encr, ' ');
1890 	if (new_key == NULL)
1891 		return -1;
1892 	*new_key++ = '\0';
1893 
1894 	os_memset(&ap, 0, sizeof(ap));
1895 	ap.ssid_hex = new_ssid;
1896 	ap.auth = new_auth;
1897 	ap.encr = new_encr;
1898 	ap.key_hex = new_key;
1899 	return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
1900 }
1901 
1902 
1903 #ifdef CONFIG_AP
wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)1904 static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
1905 						char *cmd, char *buf,
1906 						size_t buflen)
1907 {
1908 	int timeout = 300;
1909 	char *pos;
1910 	const char *pin_txt;
1911 
1912 	if (!wpa_s->ap_iface)
1913 		return -1;
1914 
1915 	pos = os_strchr(cmd, ' ');
1916 	if (pos)
1917 		*pos++ = '\0';
1918 
1919 	if (os_strcmp(cmd, "disable") == 0) {
1920 		wpas_wps_ap_pin_disable(wpa_s);
1921 		return os_snprintf(buf, buflen, "OK\n");
1922 	}
1923 
1924 	if (os_strcmp(cmd, "random") == 0) {
1925 		if (pos)
1926 			timeout = atoi(pos);
1927 		pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
1928 		if (pin_txt == NULL)
1929 			return -1;
1930 		return os_snprintf(buf, buflen, "%s", pin_txt);
1931 	}
1932 
1933 	if (os_strcmp(cmd, "get") == 0) {
1934 		pin_txt = wpas_wps_ap_pin_get(wpa_s);
1935 		if (pin_txt == NULL)
1936 			return -1;
1937 		return os_snprintf(buf, buflen, "%s", pin_txt);
1938 	}
1939 
1940 	if (os_strcmp(cmd, "set") == 0) {
1941 		char *pin;
1942 		if (pos == NULL)
1943 			return -1;
1944 		pin = pos;
1945 		pos = os_strchr(pos, ' ');
1946 		if (pos) {
1947 			*pos++ = '\0';
1948 			timeout = atoi(pos);
1949 		}
1950 		if (os_strlen(pin) > buflen)
1951 			return -1;
1952 		if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
1953 			return -1;
1954 		return os_snprintf(buf, buflen, "%s", pin);
1955 	}
1956 
1957 	return -1;
1958 }
1959 #endif /* CONFIG_AP */
1960 
1961 
1962 #ifdef CONFIG_WPS_ER
wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant * wpa_s,char * cmd)1963 static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
1964 						char *cmd)
1965 {
1966 	char *uuid = cmd, *pin, *pos;
1967 	u8 addr_buf[ETH_ALEN], *addr = NULL;
1968 	pin = os_strchr(uuid, ' ');
1969 	if (pin == NULL)
1970 		return -1;
1971 	*pin++ = '\0';
1972 	pos = os_strchr(pin, ' ');
1973 	if (pos) {
1974 		*pos++ = '\0';
1975 		if (hwaddr_aton(pos, addr_buf) == 0)
1976 			addr = addr_buf;
1977 	}
1978 	return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
1979 }
1980 
1981 
wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant * wpa_s,char * cmd)1982 static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
1983 						  char *cmd)
1984 {
1985 	char *uuid = cmd, *pin;
1986 	pin = os_strchr(uuid, ' ');
1987 	if (pin == NULL)
1988 		return -1;
1989 	*pin++ = '\0';
1990 	return wpas_wps_er_learn(wpa_s, uuid, pin);
1991 }
1992 
1993 
wpa_supplicant_ctrl_iface_wps_er_set_config(struct wpa_supplicant * wpa_s,char * cmd)1994 static int wpa_supplicant_ctrl_iface_wps_er_set_config(
1995 	struct wpa_supplicant *wpa_s, char *cmd)
1996 {
1997 	char *uuid = cmd, *id;
1998 	id = os_strchr(uuid, ' ');
1999 	if (id == NULL)
2000 		return -1;
2001 	*id++ = '\0';
2002 	return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
2003 }
2004 
2005 
wpa_supplicant_ctrl_iface_wps_er_config(struct wpa_supplicant * wpa_s,char * cmd)2006 static int wpa_supplicant_ctrl_iface_wps_er_config(
2007 	struct wpa_supplicant *wpa_s, char *cmd)
2008 {
2009 	char *pin;
2010 	char *new_ssid;
2011 	char *new_auth;
2012 	char *new_encr;
2013 	char *new_key;
2014 	struct wps_new_ap_settings ap;
2015 
2016 	pin = os_strchr(cmd, ' ');
2017 	if (pin == NULL)
2018 		return -1;
2019 	*pin++ = '\0';
2020 
2021 	new_ssid = os_strchr(pin, ' ');
2022 	if (new_ssid == NULL)
2023 		return -1;
2024 	*new_ssid++ = '\0';
2025 
2026 	new_auth = os_strchr(new_ssid, ' ');
2027 	if (new_auth == NULL)
2028 		return -1;
2029 	*new_auth++ = '\0';
2030 
2031 	new_encr = os_strchr(new_auth, ' ');
2032 	if (new_encr == NULL)
2033 		return -1;
2034 	*new_encr++ = '\0';
2035 
2036 	new_key = os_strchr(new_encr, ' ');
2037 	if (new_key == NULL)
2038 		return -1;
2039 	*new_key++ = '\0';
2040 
2041 	os_memset(&ap, 0, sizeof(ap));
2042 	ap.ssid_hex = new_ssid;
2043 	ap.auth = new_auth;
2044 	ap.encr = new_encr;
2045 	ap.key_hex = new_key;
2046 	return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
2047 }
2048 
2049 
2050 #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)2051 static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
2052 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
2053 {
2054 	int ndef;
2055 	struct wpabuf *buf;
2056 	int res;
2057 	char *uuid;
2058 
2059 	uuid = os_strchr(cmd, ' ');
2060 	if (uuid == NULL)
2061 		return -1;
2062 	*uuid++ = '\0';
2063 
2064 	if (os_strcmp(cmd, "WPS") == 0)
2065 		ndef = 0;
2066 	else if (os_strcmp(cmd, "NDEF") == 0)
2067 		ndef = 1;
2068 	else
2069 		return -1;
2070 
2071 	buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
2072 	if (buf == NULL)
2073 		return -1;
2074 
2075 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
2076 					 wpabuf_len(buf));
2077 	reply[res++] = '\n';
2078 	reply[res] = '\0';
2079 
2080 	wpabuf_free(buf);
2081 
2082 	return res;
2083 }
2084 #endif /* CONFIG_WPS_NFC */
2085 #endif /* CONFIG_WPS_ER */
2086 
2087 #endif /* CONFIG_WPS */
2088 
2089 
2090 #ifdef CONFIG_IBSS_RSN
wpa_supplicant_ctrl_iface_ibss_rsn(struct wpa_supplicant * wpa_s,char * addr)2091 static int wpa_supplicant_ctrl_iface_ibss_rsn(
2092 	struct wpa_supplicant *wpa_s, char *addr)
2093 {
2094 	u8 peer[ETH_ALEN];
2095 
2096 	if (hwaddr_aton(addr, peer)) {
2097 		wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
2098 			   "address '%s'", addr);
2099 		return -1;
2100 	}
2101 
2102 	wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
2103 		   MAC2STR(peer));
2104 
2105 	return ibss_rsn_start(wpa_s->ibss_rsn, peer);
2106 }
2107 #endif /* CONFIG_IBSS_RSN */
2108 
2109 
wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant * wpa_s,char * rsp)2110 static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
2111 					      char *rsp)
2112 {
2113 #ifdef IEEE8021X_EAPOL
2114 	char *pos, *id_pos;
2115 	int id;
2116 	struct wpa_ssid *ssid;
2117 
2118 	pos = os_strchr(rsp, '-');
2119 	if (pos == NULL)
2120 		return -1;
2121 	*pos++ = '\0';
2122 	id_pos = pos;
2123 	pos = os_strchr(pos, ':');
2124 	if (pos == NULL)
2125 		return -1;
2126 	*pos++ = '\0';
2127 	id = atoi(id_pos);
2128 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
2129 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
2130 			      (u8 *) pos, os_strlen(pos));
2131 
2132 	ssid = wpa_config_get_network(wpa_s->conf, id);
2133 	if (ssid == NULL) {
2134 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2135 			   "to update", id);
2136 		return -1;
2137 	}
2138 
2139 	return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
2140 							 pos);
2141 #else /* IEEE8021X_EAPOL */
2142 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
2143 	return -1;
2144 #endif /* IEEE8021X_EAPOL */
2145 }
2146 
2147 
wpa_supplicant_ctrl_iface_status(struct wpa_supplicant * wpa_s,const char * params,char * buf,size_t buflen)2148 static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
2149 					    const char *params,
2150 					    char *buf, size_t buflen)
2151 {
2152 	char *pos, *end, tmp[30];
2153 	int res, verbose, wps, ret;
2154 #ifdef CONFIG_HS20
2155 	const u8 *hs20;
2156 #endif /* CONFIG_HS20 */
2157 	const u8 *sess_id;
2158 	size_t sess_id_len;
2159 
2160 	if (os_strcmp(params, "-DRIVER") == 0)
2161 		return wpa_drv_status(wpa_s, buf, buflen);
2162 	verbose = os_strcmp(params, "-VERBOSE") == 0;
2163 	wps = os_strcmp(params, "-WPS") == 0;
2164 	pos = buf;
2165 	end = buf + buflen;
2166 	if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
2167 		struct wpa_ssid *ssid = wpa_s->current_ssid;
2168 		ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
2169 				  MAC2STR(wpa_s->bssid));
2170 		if (os_snprintf_error(end - pos, ret))
2171 			return pos - buf;
2172 		pos += ret;
2173 		ret = os_snprintf(pos, end - pos, "freq=%u\n",
2174 				  wpa_s->assoc_freq);
2175 		if (os_snprintf_error(end - pos, ret))
2176 			return pos - buf;
2177 		pos += ret;
2178 		if (ssid) {
2179 			u8 *_ssid = ssid->ssid;
2180 			size_t ssid_len = ssid->ssid_len;
2181 			u8 ssid_buf[SSID_MAX_LEN];
2182 			if (ssid_len == 0) {
2183 				int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
2184 				if (_res < 0)
2185 					ssid_len = 0;
2186 				else
2187 					ssid_len = _res;
2188 				_ssid = ssid_buf;
2189 			}
2190 			ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
2191 					  wpa_ssid_txt(_ssid, ssid_len),
2192 					  ssid->id);
2193 			if (os_snprintf_error(end - pos, ret))
2194 				return pos - buf;
2195 			pos += ret;
2196 
2197 			if (wps && ssid->passphrase &&
2198 			    wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
2199 			    (ssid->mode == WPAS_MODE_AP ||
2200 			     ssid->mode == WPAS_MODE_P2P_GO)) {
2201 				ret = os_snprintf(pos, end - pos,
2202 						  "passphrase=%s\n",
2203 						  ssid->passphrase);
2204 				if (os_snprintf_error(end - pos, ret))
2205 					return pos - buf;
2206 				pos += ret;
2207 			}
2208 			if (ssid->id_str) {
2209 				ret = os_snprintf(pos, end - pos,
2210 						  "id_str=%s\n",
2211 						  ssid->id_str);
2212 				if (os_snprintf_error(end - pos, ret))
2213 					return pos - buf;
2214 				pos += ret;
2215 			}
2216 
2217 			switch (ssid->mode) {
2218 			case WPAS_MODE_INFRA:
2219 				ret = os_snprintf(pos, end - pos,
2220 						  "mode=station\n");
2221 				break;
2222 			case WPAS_MODE_IBSS:
2223 				ret = os_snprintf(pos, end - pos,
2224 						  "mode=IBSS\n");
2225 				break;
2226 			case WPAS_MODE_AP:
2227 				ret = os_snprintf(pos, end - pos,
2228 						  "mode=AP\n");
2229 				break;
2230 			case WPAS_MODE_P2P_GO:
2231 				ret = os_snprintf(pos, end - pos,
2232 						  "mode=P2P GO\n");
2233 				break;
2234 			case WPAS_MODE_P2P_GROUP_FORMATION:
2235 				ret = os_snprintf(pos, end - pos,
2236 						  "mode=P2P GO - group "
2237 						  "formation\n");
2238 				break;
2239 			case WPAS_MODE_MESH:
2240 				ret = os_snprintf(pos, end - pos,
2241 						  "mode=mesh\n");
2242 				break;
2243 			default:
2244 				ret = 0;
2245 				break;
2246 			}
2247 			if (os_snprintf_error(end - pos, ret))
2248 				return pos - buf;
2249 			pos += ret;
2250 		}
2251 
2252 		if (wpa_s->connection_set &&
2253 		    (wpa_s->connection_ht || wpa_s->connection_vht ||
2254 		     wpa_s->connection_he)) {
2255 			ret = os_snprintf(pos, end - pos,
2256 					  "wifi_generation=%u\n",
2257 					  wpa_s->connection_he ? 6 :
2258 					  (wpa_s->connection_vht ? 5 : 4));
2259 			if (os_snprintf_error(end - pos, ret))
2260 				return pos - buf;
2261 			pos += ret;
2262 		}
2263 
2264 #ifdef CONFIG_AP
2265 		if (wpa_s->ap_iface) {
2266 			pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
2267 							    end - pos,
2268 							    verbose);
2269 		} else
2270 #endif /* CONFIG_AP */
2271 		pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
2272 	}
2273 #ifdef CONFIG_SME
2274 #ifdef CONFIG_SAE
2275 	if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
2276 #ifdef CONFIG_AP
2277 	    !wpa_s->ap_iface &&
2278 #endif /* CONFIG_AP */
2279 	    wpa_s->sme.sae.state == SAE_ACCEPTED) {
2280 		ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
2281 				  wpa_s->sme.sae.group);
2282 		if (os_snprintf_error(end - pos, ret))
2283 			return pos - buf;
2284 		pos += ret;
2285 	}
2286 #endif /* CONFIG_SAE */
2287 #endif /* CONFIG_SME */
2288 	ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
2289 			  wpa_supplicant_state_txt(wpa_s->wpa_state));
2290 	if (os_snprintf_error(end - pos, ret))
2291 		return pos - buf;
2292 	pos += ret;
2293 
2294 	if (wpa_s->l2 &&
2295 	    l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
2296 		ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
2297 		if (os_snprintf_error(end - pos, ret))
2298 			return pos - buf;
2299 		pos += ret;
2300 	}
2301 
2302 #ifdef CONFIG_P2P
2303 	if (wpa_s->global->p2p) {
2304 		ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
2305 				  "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
2306 		if (os_snprintf_error(end - pos, ret))
2307 			return pos - buf;
2308 		pos += ret;
2309 	}
2310 #endif /* CONFIG_P2P */
2311 
2312 	ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
2313 			  MAC2STR(wpa_s->own_addr));
2314 	if (os_snprintf_error(end - pos, ret))
2315 		return pos - buf;
2316 	pos += ret;
2317 
2318 #ifdef CONFIG_HS20
2319 	if (wpa_s->current_bss &&
2320 	    (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2321 					  HS20_IE_VENDOR_TYPE)) &&
2322 	    wpa_s->wpa_proto == WPA_PROTO_RSN &&
2323 	    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2324 		int release = 1;
2325 		if (hs20[1] >= 5) {
2326 			u8 rel_num = (hs20[6] & 0xf0) >> 4;
2327 			release = rel_num + 1;
2328 		}
2329 		ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
2330 		if (os_snprintf_error(end - pos, ret))
2331 			return pos - buf;
2332 		pos += ret;
2333 	}
2334 
2335 	if (wpa_s->current_ssid) {
2336 		struct wpa_cred *cred;
2337 		char *type;
2338 
2339 		for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
2340 			size_t i;
2341 
2342 			if (wpa_s->current_ssid->parent_cred != cred)
2343 				continue;
2344 
2345 			if (cred->provisioning_sp) {
2346 				ret = os_snprintf(pos, end - pos,
2347 						  "provisioning_sp=%s\n",
2348 						  cred->provisioning_sp);
2349 				if (os_snprintf_error(end - pos, ret))
2350 					return pos - buf;
2351 				pos += ret;
2352 			}
2353 
2354 			if (!cred->domain)
2355 				goto no_domain;
2356 
2357 			i = 0;
2358 			if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
2359 				struct wpabuf *names =
2360 					wpa_s->current_bss->anqp->domain_name;
2361 				for (i = 0; names && i < cred->num_domain; i++)
2362 				{
2363 					if (domain_name_list_contains(
2364 						    names, cred->domain[i], 1))
2365 						break;
2366 				}
2367 				if (i == cred->num_domain)
2368 					i = 0; /* show first entry by default */
2369 			}
2370 			ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
2371 					  cred->domain[i]);
2372 			if (os_snprintf_error(end - pos, ret))
2373 				return pos - buf;
2374 			pos += ret;
2375 
2376 		no_domain:
2377 			if (wpa_s->current_bss == NULL ||
2378 			    wpa_s->current_bss->anqp == NULL)
2379 				res = -1;
2380 			else
2381 				res = interworking_home_sp_cred(
2382 					wpa_s, cred,
2383 					wpa_s->current_bss->anqp->domain_name);
2384 			if (res > 0)
2385 				type = "home";
2386 			else if (res == 0)
2387 				type = "roaming";
2388 			else
2389 				type = "unknown";
2390 
2391 			ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
2392 			if (os_snprintf_error(end - pos, ret))
2393 				return pos - buf;
2394 			pos += ret;
2395 
2396 			break;
2397 		}
2398 	}
2399 #endif /* CONFIG_HS20 */
2400 
2401 	if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2402 	    wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2403 		res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
2404 					  verbose);
2405 		if (res >= 0)
2406 			pos += res;
2407 	}
2408 
2409 #ifdef CONFIG_MACSEC
2410 	res = ieee802_1x_kay_get_status(wpa_s->kay, pos, end - pos);
2411 	if (res > 0)
2412 		pos += res;
2413 #endif /* CONFIG_MACSEC */
2414 
2415 	sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
2416 	if (sess_id) {
2417 		char *start = pos;
2418 
2419 		ret = os_snprintf(pos, end - pos, "eap_session_id=");
2420 		if (os_snprintf_error(end - pos, ret))
2421 			return start - buf;
2422 		pos += ret;
2423 		ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
2424 		if (ret <= 0)
2425 			return start - buf;
2426 		pos += ret;
2427 		ret = os_snprintf(pos, end - pos, "\n");
2428 		if (os_snprintf_error(end - pos, ret))
2429 			return start - buf;
2430 		pos += ret;
2431 	}
2432 
2433 	res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
2434 	if (res >= 0)
2435 		pos += res;
2436 
2437 #ifdef CONFIG_WPS
2438 	{
2439 		char uuid_str[100];
2440 		uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
2441 		ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
2442 		if (os_snprintf_error(end - pos, ret))
2443 			return pos - buf;
2444 		pos += ret;
2445 	}
2446 #endif /* CONFIG_WPS */
2447 
2448 	if (wpa_s->ieee80211ac) {
2449 		ret = os_snprintf(pos, end - pos, "ieee80211ac=1\n");
2450 		if (os_snprintf_error(end - pos, ret))
2451 			return pos - buf;
2452 		pos += ret;
2453 	}
2454 
2455 #ifdef ANDROID
2456 	/*
2457 	 * Allow using the STATUS command with default behavior, say for debug,
2458 	 * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
2459 	 * events with STATUS-NO_EVENTS.
2460 	 */
2461 	if (os_strcmp(params, "-NO_EVENTS")) {
2462 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
2463 			     "id=%d state=%d BSSID=" MACSTR " SSID=%s",
2464 			     wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
2465 			     wpa_s->wpa_state,
2466 			     MAC2STR(wpa_s->bssid),
2467 			     wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
2468 			     wpa_ssid_txt(wpa_s->current_ssid->ssid,
2469 					  wpa_s->current_ssid->ssid_len) : "");
2470 		if (wpa_s->wpa_state == WPA_COMPLETED) {
2471 			struct wpa_ssid *ssid = wpa_s->current_ssid;
2472 			wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
2473 				     "- connection to " MACSTR
2474 				     " completed %s [id=%d id_str=%s]",
2475 				     MAC2STR(wpa_s->bssid), "(auth)",
2476 				     ssid ? ssid->id : -1,
2477 				     ssid && ssid->id_str ? ssid->id_str : "");
2478 		}
2479 	}
2480 #endif /* ANDROID */
2481 
2482 	return pos - buf;
2483 }
2484 
2485 
wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant * wpa_s,char * cmd)2486 static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
2487 					   char *cmd)
2488 {
2489 	char *pos;
2490 	int id;
2491 	struct wpa_ssid *ssid;
2492 	u8 bssid[ETH_ALEN];
2493 
2494 	/* cmd: "<network id> <BSSID>" */
2495 	pos = os_strchr(cmd, ' ');
2496 	if (pos == NULL)
2497 		return -1;
2498 	*pos++ = '\0';
2499 	id = atoi(cmd);
2500 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
2501 	if (hwaddr_aton(pos, bssid)) {
2502 		wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
2503 		return -1;
2504 	}
2505 
2506 	ssid = wpa_config_get_network(wpa_s->conf, id);
2507 	if (ssid == NULL) {
2508 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
2509 			   "to update", id);
2510 		return -1;
2511 	}
2512 
2513 	os_memcpy(ssid->bssid, bssid, ETH_ALEN);
2514 	ssid->bssid_set = !is_zero_ether_addr(bssid);
2515 
2516 	return 0;
2517 }
2518 
2519 
wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2520 static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
2521 					       char *cmd, char *buf,
2522 					       size_t buflen)
2523 {
2524 	u8 bssid[ETH_ALEN];
2525 	struct wpa_blacklist *e;
2526 	char *pos, *end;
2527 	int ret;
2528 
2529 	/* cmd: "BLACKLIST [<BSSID>]" */
2530 	if (*cmd == '\0') {
2531 		pos = buf;
2532 		end = buf + buflen;
2533 		e = wpa_s->blacklist;
2534 		while (e) {
2535 			ret = os_snprintf(pos, end - pos, MACSTR "\n",
2536 					  MAC2STR(e->bssid));
2537 			if (os_snprintf_error(end - pos, ret))
2538 				return pos - buf;
2539 			pos += ret;
2540 			e = e->next;
2541 		}
2542 		return pos - buf;
2543 	}
2544 
2545 	cmd++;
2546 	if (os_strncmp(cmd, "clear", 5) == 0) {
2547 		wpa_blacklist_clear(wpa_s);
2548 		os_memcpy(buf, "OK\n", 3);
2549 		return 3;
2550 	}
2551 
2552 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
2553 	if (hwaddr_aton(cmd, bssid)) {
2554 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
2555 		return -1;
2556 	}
2557 
2558 	/*
2559 	 * Add the BSSID twice, so its count will be 2, causing it to be
2560 	 * skipped when processing scan results.
2561 	 */
2562 	ret = wpa_blacklist_add(wpa_s, bssid);
2563 	if (ret < 0)
2564 		return -1;
2565 	ret = wpa_blacklist_add(wpa_s, bssid);
2566 	if (ret < 0)
2567 		return -1;
2568 	os_memcpy(buf, "OK\n", 3);
2569 	return 3;
2570 }
2571 
2572 
wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2573 static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
2574 					       char *cmd, char *buf,
2575 					       size_t buflen)
2576 {
2577 	char *pos, *end, *stamp;
2578 	int ret;
2579 
2580 	/* cmd: "LOG_LEVEL [<level>]" */
2581 	if (*cmd == '\0') {
2582 		pos = buf;
2583 		end = buf + buflen;
2584 		ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2585 				  "Timestamp: %d\n",
2586 				  debug_level_str(wpa_debug_level),
2587 				  wpa_debug_timestamp);
2588 		if (os_snprintf_error(end - pos, ret))
2589 			ret = 0;
2590 
2591 		return ret;
2592 	}
2593 
2594 	while (*cmd == ' ')
2595 		cmd++;
2596 
2597 	stamp = os_strchr(cmd, ' ');
2598 	if (stamp) {
2599 		*stamp++ = '\0';
2600 		while (*stamp == ' ') {
2601 			stamp++;
2602 		}
2603 	}
2604 
2605 	if (os_strlen(cmd)) {
2606 		int level = str_to_debug_level(cmd);
2607 		if (level < 0)
2608 			return -1;
2609 		wpa_debug_level = level;
2610 	}
2611 
2612 	if (stamp && os_strlen(stamp))
2613 		wpa_debug_timestamp = atoi(stamp);
2614 
2615 	os_memcpy(buf, "OK\n", 3);
2616 	return 3;
2617 }
2618 
2619 
wpa_supplicant_ctrl_iface_list_networks(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)2620 static int wpa_supplicant_ctrl_iface_list_networks(
2621 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
2622 {
2623 	char *pos, *end, *prev;
2624 	struct wpa_ssid *ssid;
2625 	int ret;
2626 
2627 	pos = buf;
2628 	end = buf + buflen;
2629 	ret = os_snprintf(pos, end - pos,
2630 			  "network id / ssid / bssid / flags\n");
2631 	if (os_snprintf_error(end - pos, ret))
2632 		return pos - buf;
2633 	pos += ret;
2634 
2635 	ssid = wpa_s->conf->ssid;
2636 
2637 	/* skip over ssids until we find next one */
2638 	if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
2639 		int last_id = atoi(cmd + 8);
2640 		if (last_id != -1) {
2641 			while (ssid != NULL && ssid->id <= last_id) {
2642 				ssid = ssid->next;
2643 			}
2644 		}
2645 	}
2646 
2647 	while (ssid) {
2648 		prev = pos;
2649 		ret = os_snprintf(pos, end - pos, "%d\t%s",
2650 				  ssid->id,
2651 				  wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2652 		if (os_snprintf_error(end - pos, ret))
2653 			return prev - buf;
2654 		pos += ret;
2655 		if (ssid->bssid_set) {
2656 			ret = os_snprintf(pos, end - pos, "\t" MACSTR,
2657 					  MAC2STR(ssid->bssid));
2658 		} else {
2659 			ret = os_snprintf(pos, end - pos, "\tany");
2660 		}
2661 		if (os_snprintf_error(end - pos, ret))
2662 			return prev - buf;
2663 		pos += ret;
2664 		ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
2665 				  ssid == wpa_s->current_ssid ?
2666 				  "[CURRENT]" : "",
2667 				  ssid->disabled ? "[DISABLED]" : "",
2668 				  ssid->disabled_until.sec ?
2669 				  "[TEMP-DISABLED]" : "",
2670 				  ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
2671 				  "");
2672 		if (os_snprintf_error(end - pos, ret))
2673 			return prev - buf;
2674 		pos += ret;
2675 		ret = os_snprintf(pos, end - pos, "\n");
2676 		if (os_snprintf_error(end - pos, ret))
2677 			return prev - buf;
2678 		pos += ret;
2679 
2680 		ssid = ssid->next;
2681 	}
2682 
2683 	return pos - buf;
2684 }
2685 
2686 
wpa_supplicant_cipher_txt(char * pos,char * end,int cipher)2687 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
2688 {
2689 	int ret;
2690 	ret = os_snprintf(pos, end - pos, "-");
2691 	if (os_snprintf_error(end - pos, ret))
2692 		return pos;
2693 	pos += ret;
2694 	ret = wpa_write_ciphers(pos, end, cipher, "+");
2695 	if (ret < 0)
2696 		return pos;
2697 	pos += ret;
2698 	return pos;
2699 }
2700 
2701 
wpa_supplicant_ie_txt(char * pos,char * end,const char * proto,const u8 * ie,size_t ie_len)2702 static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
2703 				    const u8 *ie, size_t ie_len)
2704 {
2705 	struct wpa_ie_data data;
2706 	char *start;
2707 	int ret;
2708 
2709 	ret = os_snprintf(pos, end - pos, "[%s-", proto);
2710 	if (os_snprintf_error(end - pos, ret))
2711 		return pos;
2712 	pos += ret;
2713 
2714 	if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
2715 		ret = os_snprintf(pos, end - pos, "?]");
2716 		if (os_snprintf_error(end - pos, ret))
2717 			return pos;
2718 		pos += ret;
2719 		return pos;
2720 	}
2721 
2722 	start = pos;
2723 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
2724 		ret = os_snprintf(pos, end - pos, "%sEAP",
2725 				  pos == start ? "" : "+");
2726 		if (os_snprintf_error(end - pos, ret))
2727 			return pos;
2728 		pos += ret;
2729 	}
2730 	if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
2731 		ret = os_snprintf(pos, end - pos, "%sPSK",
2732 				  pos == start ? "" : "+");
2733 		if (os_snprintf_error(end - pos, ret))
2734 			return pos;
2735 		pos += ret;
2736 	}
2737 	if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
2738 		ret = os_snprintf(pos, end - pos, "%sNone",
2739 				  pos == start ? "" : "+");
2740 		if (os_snprintf_error(end - pos, ret))
2741 			return pos;
2742 		pos += ret;
2743 	}
2744 	if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
2745 		ret = os_snprintf(pos, end - pos, "%sSAE",
2746 				  pos == start ? "" : "+");
2747 		if (os_snprintf_error(end - pos, ret))
2748 			return pos;
2749 		pos += ret;
2750 	}
2751 #ifdef CONFIG_IEEE80211R
2752 	if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
2753 		ret = os_snprintf(pos, end - pos, "%sFT/EAP",
2754 				  pos == start ? "" : "+");
2755 		if (os_snprintf_error(end - pos, ret))
2756 			return pos;
2757 		pos += ret;
2758 	}
2759 	if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
2760 		ret = os_snprintf(pos, end - pos, "%sFT/PSK",
2761 				  pos == start ? "" : "+");
2762 		if (os_snprintf_error(end - pos, ret))
2763 			return pos;
2764 		pos += ret;
2765 	}
2766 	if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
2767 		ret = os_snprintf(pos, end - pos, "%sFT/SAE",
2768 				  pos == start ? "" : "+");
2769 		if (os_snprintf_error(end - pos, ret))
2770 			return pos;
2771 		pos += ret;
2772 	}
2773 #endif /* CONFIG_IEEE80211R */
2774 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
2775 		ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
2776 				  pos == start ? "" : "+");
2777 		if (os_snprintf_error(end - pos, ret))
2778 			return pos;
2779 		pos += ret;
2780 	}
2781 	if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
2782 		ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
2783 				  pos == start ? "" : "+");
2784 		if (os_snprintf_error(end - pos, ret))
2785 			return pos;
2786 		pos += ret;
2787 	}
2788 
2789 #ifdef CONFIG_SUITEB
2790 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
2791 		ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
2792 				  pos == start ? "" : "+");
2793 		if (os_snprintf_error(end - pos, ret))
2794 			return pos;
2795 		pos += ret;
2796 	}
2797 #endif /* CONFIG_SUITEB */
2798 
2799 #ifdef CONFIG_SUITEB192
2800 	if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
2801 		ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
2802 				  pos == start ? "" : "+");
2803 		if (os_snprintf_error(end - pos, ret))
2804 			return pos;
2805 		pos += ret;
2806 	}
2807 #endif /* CONFIG_SUITEB192 */
2808 
2809 #ifdef CONFIG_FILS
2810 	if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
2811 		ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
2812 				  pos == start ? "" : "+");
2813 		if (os_snprintf_error(end - pos, ret))
2814 			return pos;
2815 		pos += ret;
2816 	}
2817 	if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
2818 		ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
2819 				  pos == start ? "" : "+");
2820 		if (os_snprintf_error(end - pos, ret))
2821 			return pos;
2822 		pos += ret;
2823 	}
2824 #ifdef CONFIG_IEEE80211R
2825 	if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
2826 		ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
2827 				  pos == start ? "" : "+");
2828 		if (os_snprintf_error(end - pos, ret))
2829 			return pos;
2830 		pos += ret;
2831 	}
2832 	if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
2833 		ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
2834 				  pos == start ? "" : "+");
2835 		if (os_snprintf_error(end - pos, ret))
2836 			return pos;
2837 		pos += ret;
2838 	}
2839 #endif /* CONFIG_IEEE80211R */
2840 #endif /* CONFIG_FILS */
2841 
2842 #ifdef CONFIG_OWE
2843 	if (data.key_mgmt & WPA_KEY_MGMT_OWE) {
2844 		ret = os_snprintf(pos, end - pos, "%sOWE",
2845 				  pos == start ? "" : "+");
2846 		if (os_snprintf_error(end - pos, ret))
2847 			return pos;
2848 		pos += ret;
2849 	}
2850 #endif /* CONFIG_OWE */
2851 
2852 #ifdef CONFIG_DPP
2853 	if (data.key_mgmt & WPA_KEY_MGMT_DPP) {
2854 		ret = os_snprintf(pos, end - pos, "%sDPP",
2855 				  pos == start ? "" : "+");
2856 		if (os_snprintf_error(end - pos, ret))
2857 			return pos;
2858 		pos += ret;
2859 	}
2860 #endif /* CONFIG_DPP */
2861 
2862 	if (data.key_mgmt & WPA_KEY_MGMT_OSEN) {
2863 		ret = os_snprintf(pos, end - pos, "%sOSEN",
2864 				  pos == start ? "" : "+");
2865 		if (os_snprintf_error(end - pos, ret))
2866 			return pos;
2867 		pos += ret;
2868 	}
2869 
2870 	pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
2871 
2872 	if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
2873 		ret = os_snprintf(pos, end - pos, "-preauth");
2874 		if (os_snprintf_error(end - pos, ret))
2875 			return pos;
2876 		pos += ret;
2877 	}
2878 
2879 	ret = os_snprintf(pos, end - pos, "]");
2880 	if (os_snprintf_error(end - pos, ret))
2881 		return pos;
2882 	pos += ret;
2883 
2884 	return pos;
2885 }
2886 
2887 
2888 #ifdef CONFIG_WPS
wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant * wpa_s,char * pos,char * end,struct wpabuf * wps_ie)2889 static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
2890 					    char *pos, char *end,
2891 					    struct wpabuf *wps_ie)
2892 {
2893 	int ret;
2894 	const char *txt;
2895 
2896 	if (wps_ie == NULL)
2897 		return pos;
2898 	if (wps_is_selected_pbc_registrar(wps_ie))
2899 		txt = "[WPS-PBC]";
2900 	else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
2901 		txt = "[WPS-AUTH]";
2902 	else if (wps_is_selected_pin_registrar(wps_ie))
2903 		txt = "[WPS-PIN]";
2904 	else
2905 		txt = "[WPS]";
2906 
2907 	ret = os_snprintf(pos, end - pos, "%s", txt);
2908 	if (!os_snprintf_error(end - pos, ret))
2909 		pos += ret;
2910 	wpabuf_free(wps_ie);
2911 	return pos;
2912 }
2913 #endif /* CONFIG_WPS */
2914 
2915 
wpa_supplicant_wps_ie_txt(struct wpa_supplicant * wpa_s,char * pos,char * end,const struct wpa_bss * bss)2916 static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
2917 					char *pos, char *end,
2918 					const struct wpa_bss *bss)
2919 {
2920 #ifdef CONFIG_WPS
2921 	struct wpabuf *wps_ie;
2922 	wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
2923 	return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
2924 #else /* CONFIG_WPS */
2925 	return pos;
2926 #endif /* CONFIG_WPS */
2927 }
2928 
2929 
2930 /* 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)2931 static int wpa_supplicant_ctrl_iface_scan_result(
2932 	struct wpa_supplicant *wpa_s,
2933 	const struct wpa_bss *bss, char *buf, size_t buflen)
2934 {
2935 	char *pos, *end;
2936 	int ret;
2937 	const u8 *ie, *ie2, *osen_ie, *p2p, *mesh, *owe;
2938 
2939 	mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
2940 	p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
2941 	if (!p2p)
2942 		p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
2943 	if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
2944 	    os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
2945 	    0)
2946 		return 0; /* Do not show P2P listen discovery results here */
2947 
2948 	pos = buf;
2949 	end = buf + buflen;
2950 
2951 	ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
2952 			  MAC2STR(bss->bssid), bss->freq, bss->level);
2953 	if (os_snprintf_error(end - pos, ret))
2954 		return -1;
2955 	pos += ret;
2956 	ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
2957 	if (ie)
2958 		pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
2959 	ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2960 	if (ie2) {
2961 		pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
2962 					    ie2, 2 + ie2[1]);
2963 	}
2964 	osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
2965 	if (osen_ie)
2966 		pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
2967 					    osen_ie, 2 + osen_ie[1]);
2968 	owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
2969 	if (owe) {
2970 		ret = os_snprintf(pos, end - pos,
2971 				  ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
2972 		if (os_snprintf_error(end - pos, ret))
2973 			return -1;
2974 		pos += ret;
2975 	}
2976 	pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
2977 	if (!ie && !ie2 && !osen_ie && (bss->caps & IEEE80211_CAP_PRIVACY)) {
2978 		ret = os_snprintf(pos, end - pos, "[WEP]");
2979 		if (os_snprintf_error(end - pos, ret))
2980 			return -1;
2981 		pos += ret;
2982 	}
2983 	if (mesh) {
2984 		ret = os_snprintf(pos, end - pos, "[MESH]");
2985 		if (os_snprintf_error(end - pos, ret))
2986 			return -1;
2987 		pos += ret;
2988 	}
2989 	if (bss_is_dmg(bss)) {
2990 		const char *s;
2991 
2992 		if (get_ie_ext((const u8 *) (bss + 1), bss->ie_len,
2993 			       WLAN_EID_EXT_EDMG_OPERATION)) {
2994 			ret = os_snprintf(pos, end - pos, "[EDMG]");
2995 			if (os_snprintf_error(end - pos, ret))
2996 				return -1;
2997 			pos += ret;
2998 		}
2999 
3000 		ret = os_snprintf(pos, end - pos, "[DMG]");
3001 		if (os_snprintf_error(end - pos, ret))
3002 			return -1;
3003 		pos += ret;
3004 		switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
3005 		case IEEE80211_CAP_DMG_IBSS:
3006 			s = "[IBSS]";
3007 			break;
3008 		case IEEE80211_CAP_DMG_AP:
3009 			s = "[ESS]";
3010 			break;
3011 		case IEEE80211_CAP_DMG_PBSS:
3012 			s = "[PBSS]";
3013 			break;
3014 		default:
3015 			s = "";
3016 			break;
3017 		}
3018 		ret = os_snprintf(pos, end - pos, "%s", s);
3019 		if (os_snprintf_error(end - pos, ret))
3020 			return -1;
3021 		pos += ret;
3022 	} else {
3023 		if (bss->caps & IEEE80211_CAP_IBSS) {
3024 			ret = os_snprintf(pos, end - pos, "[IBSS]");
3025 			if (os_snprintf_error(end - pos, ret))
3026 				return -1;
3027 			pos += ret;
3028 		}
3029 		if (bss->caps & IEEE80211_CAP_ESS) {
3030 			ret = os_snprintf(pos, end - pos, "[ESS]");
3031 			if (os_snprintf_error(end - pos, ret))
3032 				return -1;
3033 			pos += ret;
3034 		}
3035 	}
3036 	if (p2p) {
3037 		ret = os_snprintf(pos, end - pos, "[P2P]");
3038 		if (os_snprintf_error(end - pos, ret))
3039 			return -1;
3040 		pos += ret;
3041 	}
3042 #ifdef CONFIG_HS20
3043 	if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
3044 		ret = os_snprintf(pos, end - pos, "[HS20]");
3045 		if (os_snprintf_error(end - pos, ret))
3046 			return -1;
3047 		pos += ret;
3048 	}
3049 #endif /* CONFIG_HS20 */
3050 #ifdef CONFIG_FILS
3051 	if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
3052 		ret = os_snprintf(pos, end - pos, "[FILS]");
3053 		if (os_snprintf_error(end - pos, ret))
3054 			return -1;
3055 		pos += ret;
3056 	}
3057 #endif /* CONFIG_FILS */
3058 #ifdef CONFIG_FST
3059 	if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
3060 		ret = os_snprintf(pos, end - pos, "[FST]");
3061 		if (os_snprintf_error(end - pos, ret))
3062 			return -1;
3063 		pos += ret;
3064 	}
3065 #endif /* CONFIG_FST */
3066 	if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_UTF_8_SSID)) {
3067 		ret = os_snprintf(pos, end - pos, "[UTF-8]");
3068 		if (os_snprintf_error(end - pos, ret))
3069 			return -1;
3070 		pos += ret;
3071 	}
3072 
3073 	ret = os_snprintf(pos, end - pos, "\t%s",
3074 			  wpa_ssid_txt(bss->ssid, bss->ssid_len));
3075 	if (os_snprintf_error(end - pos, ret))
3076 		return -1;
3077 	pos += ret;
3078 
3079 	ret = os_snprintf(pos, end - pos, "\n");
3080 	if (os_snprintf_error(end - pos, ret))
3081 		return -1;
3082 	pos += ret;
3083 
3084 	return pos - buf;
3085 }
3086 
3087 
wpa_supplicant_ctrl_iface_scan_results(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3088 static int wpa_supplicant_ctrl_iface_scan_results(
3089 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
3090 {
3091 	char *pos, *end;
3092 	struct wpa_bss *bss;
3093 	int ret;
3094 
3095 	pos = buf;
3096 	end = buf + buflen;
3097 	ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
3098 			  "flags / ssid\n");
3099 	if (os_snprintf_error(end - pos, ret))
3100 		return pos - buf;
3101 	pos += ret;
3102 
3103 	dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
3104 		ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
3105 							    end - pos);
3106 		if (ret < 0 || ret >= end - pos)
3107 			return pos - buf;
3108 		pos += ret;
3109 	}
3110 
3111 	return pos - buf;
3112 }
3113 
3114 
3115 #ifdef CONFIG_MESH
3116 
wpa_supplicant_ctrl_iface_mesh_interface_add(struct wpa_supplicant * wpa_s,char * cmd,char * reply,size_t max_len)3117 static int wpa_supplicant_ctrl_iface_mesh_interface_add(
3118 	struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
3119 {
3120 	char *pos, ifname[IFNAMSIZ + 1];
3121 
3122 	ifname[0] = '\0';
3123 
3124 	pos = os_strstr(cmd, "ifname=");
3125 	if (pos) {
3126 		pos += 7;
3127 		os_strlcpy(ifname, pos, sizeof(ifname));
3128 	}
3129 
3130 	if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
3131 		return -1;
3132 
3133 	os_strlcpy(reply, ifname, max_len);
3134 	return os_strlen(ifname);
3135 }
3136 
3137 
wpa_supplicant_ctrl_iface_mesh_group_add(struct wpa_supplicant * wpa_s,char * cmd)3138 static int wpa_supplicant_ctrl_iface_mesh_group_add(
3139 	struct wpa_supplicant *wpa_s, char *cmd)
3140 {
3141 	int id;
3142 	struct wpa_ssid *ssid;
3143 
3144 	id = atoi(cmd);
3145 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
3146 
3147 	ssid = wpa_config_get_network(wpa_s->conf, id);
3148 	if (ssid == NULL) {
3149 		wpa_printf(MSG_DEBUG,
3150 			   "CTRL_IFACE: Could not find network id=%d", id);
3151 		return -1;
3152 	}
3153 	if (ssid->mode != WPAS_MODE_MESH) {
3154 		wpa_printf(MSG_DEBUG,
3155 			   "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
3156 		return -1;
3157 	}
3158 	if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
3159 	    ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
3160 		wpa_printf(MSG_ERROR,
3161 			   "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
3162 		return -1;
3163 	}
3164 
3165 	/*
3166 	 * TODO: If necessary write our own group_add function,
3167 	 * for now we can reuse select_network
3168 	 */
3169 	wpa_supplicant_select_network(wpa_s, ssid);
3170 
3171 	return 0;
3172 }
3173 
3174 
wpa_supplicant_ctrl_iface_mesh_group_remove(struct wpa_supplicant * wpa_s,char * cmd)3175 static int wpa_supplicant_ctrl_iface_mesh_group_remove(
3176 	struct wpa_supplicant *wpa_s, char *cmd)
3177 {
3178 	struct wpa_supplicant *orig;
3179 	struct wpa_global *global;
3180 	int found = 0;
3181 
3182 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
3183 
3184 	global = wpa_s->global;
3185 	orig = wpa_s;
3186 
3187 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3188 		if (os_strcmp(wpa_s->ifname, cmd) == 0) {
3189 			found = 1;
3190 			break;
3191 		}
3192 	}
3193 	if (!found) {
3194 		wpa_printf(MSG_ERROR,
3195 			   "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
3196 			   cmd);
3197 		return -1;
3198 	}
3199 	if (wpa_s->mesh_if_created && wpa_s == orig) {
3200 		wpa_printf(MSG_ERROR,
3201 			   "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
3202 		return -1;
3203 	}
3204 
3205 	wpa_s->reassociate = 0;
3206 	wpa_s->disconnected = 1;
3207 	wpa_supplicant_cancel_sched_scan(wpa_s);
3208 	wpa_supplicant_cancel_scan(wpa_s);
3209 
3210 	/*
3211 	 * TODO: If necessary write our own group_remove function,
3212 	 * for now we can reuse deauthenticate
3213 	 */
3214 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3215 
3216 	if (wpa_s->mesh_if_created)
3217 		wpa_supplicant_remove_iface(global, wpa_s, 0);
3218 
3219 	return 0;
3220 }
3221 
3222 
wpa_supplicant_ctrl_iface_mesh_peer_remove(struct wpa_supplicant * wpa_s,char * cmd)3223 static int wpa_supplicant_ctrl_iface_mesh_peer_remove(
3224 	struct wpa_supplicant *wpa_s, char *cmd)
3225 {
3226 	u8 addr[ETH_ALEN];
3227 
3228 	if (hwaddr_aton(cmd, addr) < 0)
3229 		return -1;
3230 
3231 	return wpas_mesh_peer_remove(wpa_s, addr);
3232 }
3233 
3234 
wpa_supplicant_ctrl_iface_mesh_peer_add(struct wpa_supplicant * wpa_s,char * cmd)3235 static int wpa_supplicant_ctrl_iface_mesh_peer_add(
3236 	struct wpa_supplicant *wpa_s, char *cmd)
3237 {
3238 	u8 addr[ETH_ALEN];
3239 	int duration;
3240 	char *pos;
3241 
3242 	pos = os_strstr(cmd, " duration=");
3243 	if (pos) {
3244 		*pos = '\0';
3245 		duration = atoi(pos + 10);
3246 	} else {
3247 		duration = -1;
3248 	}
3249 
3250 	if (hwaddr_aton(cmd, addr))
3251 		return -1;
3252 
3253 	return wpas_mesh_peer_add(wpa_s, addr, duration);
3254 }
3255 
3256 
wpa_supplicant_ctrl_iface_mesh_link_probe(struct wpa_supplicant * wpa_s,char * cmd)3257 static int wpa_supplicant_ctrl_iface_mesh_link_probe(
3258 	struct wpa_supplicant *wpa_s, char *cmd)
3259 {
3260 	struct ether_header *eth;
3261 	u8 addr[ETH_ALEN];
3262 	u8 *buf;
3263 	char *pos;
3264 	size_t payload_len = 0, len;
3265 	int ret = -1;
3266 
3267 	if (hwaddr_aton(cmd, addr))
3268 		return -1;
3269 
3270 	pos = os_strstr(cmd, " payload=");
3271 	if (pos) {
3272 		pos = pos + 9;
3273 		payload_len = os_strlen(pos);
3274 		if (payload_len & 1)
3275 			return -1;
3276 
3277 		payload_len /= 2;
3278 	}
3279 
3280 	len = ETH_HLEN + payload_len;
3281 	buf = os_malloc(len);
3282 	if (!buf)
3283 		return -1;
3284 
3285 	eth = (struct ether_header *) buf;
3286 	os_memcpy(eth->ether_dhost, addr, ETH_ALEN);
3287 	os_memcpy(eth->ether_shost, wpa_s->own_addr, ETH_ALEN);
3288 	eth->ether_type = htons(ETH_P_802_3);
3289 
3290 	if (payload_len && hexstr2bin(pos, buf + ETH_HLEN, payload_len) < 0)
3291 		goto fail;
3292 
3293 	ret = wpa_drv_mesh_link_probe(wpa_s, addr, buf, len);
3294 fail:
3295 	os_free(buf);
3296 	return -ret;
3297 }
3298 
3299 #endif /* CONFIG_MESH */
3300 
3301 
wpa_supplicant_ctrl_iface_select_network(struct wpa_supplicant * wpa_s,char * cmd)3302 static int wpa_supplicant_ctrl_iface_select_network(
3303 	struct wpa_supplicant *wpa_s, char *cmd)
3304 {
3305 	int id;
3306 	struct wpa_ssid *ssid;
3307 	char *pos;
3308 
3309 	/* cmd: "<network id>" or "any" */
3310 	if (os_strncmp(cmd, "any", 3) == 0) {
3311 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
3312 		ssid = NULL;
3313 	} else {
3314 		id = atoi(cmd);
3315 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
3316 
3317 		ssid = wpa_config_get_network(wpa_s->conf, id);
3318 		if (ssid == NULL) {
3319 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3320 				   "network id=%d", id);
3321 			return -1;
3322 		}
3323 		if (ssid->disabled == 2) {
3324 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3325 				   "SELECT_NETWORK with persistent P2P group");
3326 			return -1;
3327 		}
3328 	}
3329 
3330 	pos = os_strstr(cmd, " freq=");
3331 	if (pos) {
3332 		int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
3333 		if (freqs) {
3334 			os_free(wpa_s->select_network_scan_freqs);
3335 			wpa_s->select_network_scan_freqs = freqs;
3336 		}
3337 	}
3338 
3339 	wpa_s->scan_min_time.sec = 0;
3340 	wpa_s->scan_min_time.usec = 0;
3341 	wpa_supplicant_select_network(wpa_s, ssid);
3342 
3343 	return 0;
3344 }
3345 
3346 
wpa_supplicant_ctrl_iface_enable_network(struct wpa_supplicant * wpa_s,char * cmd)3347 static int wpa_supplicant_ctrl_iface_enable_network(
3348 	struct wpa_supplicant *wpa_s, char *cmd)
3349 {
3350 	int id;
3351 	struct wpa_ssid *ssid;
3352 
3353 	/* cmd: "<network id>" or "all" */
3354 	if (os_strcmp(cmd, "all") == 0) {
3355 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
3356 		ssid = NULL;
3357 	} else {
3358 		id = atoi(cmd);
3359 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
3360 
3361 		ssid = wpa_config_get_network(wpa_s->conf, id);
3362 		if (ssid == NULL) {
3363 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3364 				   "network id=%d", id);
3365 			return -1;
3366 		}
3367 		if (ssid->disabled == 2) {
3368 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3369 				   "ENABLE_NETWORK with persistent P2P group");
3370 			return -1;
3371 		}
3372 
3373 		if (os_strstr(cmd, " no-connect")) {
3374 			ssid->disabled = 0;
3375 			return 0;
3376 		}
3377 	}
3378 	wpa_s->scan_min_time.sec = 0;
3379 	wpa_s->scan_min_time.usec = 0;
3380 	wpa_supplicant_enable_network(wpa_s, ssid);
3381 
3382 	return 0;
3383 }
3384 
3385 
wpa_supplicant_ctrl_iface_disable_network(struct wpa_supplicant * wpa_s,char * cmd)3386 static int wpa_supplicant_ctrl_iface_disable_network(
3387 	struct wpa_supplicant *wpa_s, char *cmd)
3388 {
3389 	int id;
3390 	struct wpa_ssid *ssid;
3391 
3392 	/* cmd: "<network id>" or "all" */
3393 	if (os_strcmp(cmd, "all") == 0) {
3394 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
3395 		ssid = NULL;
3396 	} else {
3397 		id = atoi(cmd);
3398 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
3399 
3400 		ssid = wpa_config_get_network(wpa_s->conf, id);
3401 		if (ssid == NULL) {
3402 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3403 				   "network id=%d", id);
3404 			return -1;
3405 		}
3406 		if (ssid->disabled == 2) {
3407 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
3408 				   "DISABLE_NETWORK with persistent P2P "
3409 				   "group");
3410 			return -1;
3411 		}
3412 	}
3413 	wpa_supplicant_disable_network(wpa_s, ssid);
3414 
3415 	return 0;
3416 }
3417 
3418 
wpa_supplicant_ctrl_iface_add_network(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3419 static int wpa_supplicant_ctrl_iface_add_network(
3420 	struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
3421 {
3422 	struct wpa_ssid *ssid;
3423 	int ret;
3424 
3425 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
3426 
3427 	ssid = wpa_supplicant_add_network(wpa_s);
3428 	if (ssid == NULL)
3429 		return -1;
3430 
3431 	ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
3432 	if (os_snprintf_error(buflen, ret))
3433 		return -1;
3434 	return ret;
3435 }
3436 
3437 
wpa_supplicant_ctrl_iface_remove_network(struct wpa_supplicant * wpa_s,char * cmd)3438 static int wpa_supplicant_ctrl_iface_remove_network(
3439 	struct wpa_supplicant *wpa_s, char *cmd)
3440 {
3441 	int id;
3442 	struct wpa_ssid *ssid;
3443 	int result;
3444 
3445 	/* cmd: "<network id>" or "all" */
3446 	if (os_strcmp(cmd, "all") == 0) {
3447 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
3448 		if (wpa_s->sched_scanning)
3449 			wpa_supplicant_cancel_sched_scan(wpa_s);
3450 
3451 		eapol_sm_invalidate_cached_session(wpa_s->eapol);
3452 		if (wpa_s->current_ssid) {
3453 #ifdef CONFIG_SME
3454 			wpa_s->sme.prev_bssid_set = 0;
3455 #endif /* CONFIG_SME */
3456 			wpa_sm_set_config(wpa_s->wpa, NULL);
3457 			eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
3458 			if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3459 				wpa_s->own_disconnect_req = 1;
3460 			wpa_supplicant_deauthenticate(
3461 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3462 		}
3463 		ssid = wpa_s->conf->ssid;
3464 		while (ssid) {
3465 			struct wpa_ssid *remove_ssid = ssid;
3466 			id = ssid->id;
3467 			ssid = ssid->next;
3468 			if (wpa_s->last_ssid == remove_ssid)
3469 				wpa_s->last_ssid = NULL;
3470 			wpas_notify_network_removed(wpa_s, remove_ssid);
3471 			wpa_config_remove_network(wpa_s->conf, id);
3472 		}
3473 		return 0;
3474 	}
3475 
3476 	id = atoi(cmd);
3477 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
3478 
3479 	result = wpa_supplicant_remove_network(wpa_s, id);
3480 	if (result == -1) {
3481 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3482 			   "id=%d", id);
3483 		return -1;
3484 	}
3485 	if (result == -2) {
3486 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
3487 			   "network id=%d", id);
3488 		return -1;
3489 	}
3490 	return 0;
3491 }
3492 
3493 
wpa_supplicant_ctrl_iface_update_network(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,char * name,char * value)3494 static int wpa_supplicant_ctrl_iface_update_network(
3495 	struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3496 	char *name, char *value)
3497 {
3498 	int ret;
3499 
3500 	ret = wpa_config_set(ssid, name, value, 0);
3501 	if (ret < 0) {
3502 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
3503 			   "variable '%s'", name);
3504 		return -1;
3505 	}
3506 	if (ret == 1)
3507 		return 0; /* No change to the previously configured value */
3508 
3509 	if (os_strcmp(name, "bssid") != 0 &&
3510 	    os_strcmp(name, "bssid_hint") != 0 &&
3511 	    os_strcmp(name, "priority") != 0) {
3512 		wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
3513 
3514 		if (wpa_s->current_ssid == ssid ||
3515 		    wpa_s->current_ssid == NULL) {
3516 			/*
3517 			 * Invalidate the EAP session cache if anything in the
3518 			 * current or previously used configuration changes.
3519 			 */
3520 			eapol_sm_invalidate_cached_session(wpa_s->eapol);
3521 		}
3522 	}
3523 
3524 	if ((os_strcmp(name, "psk") == 0 &&
3525 	     value[0] == '"' && ssid->ssid_len) ||
3526 	    (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
3527 		wpa_config_update_psk(ssid);
3528 	else if (os_strcmp(name, "priority") == 0)
3529 		wpa_config_update_prio_list(wpa_s->conf);
3530 
3531 	return 0;
3532 }
3533 
3534 
wpa_supplicant_ctrl_iface_set_network(struct wpa_supplicant * wpa_s,char * cmd)3535 static int wpa_supplicant_ctrl_iface_set_network(
3536 	struct wpa_supplicant *wpa_s, char *cmd)
3537 {
3538 	int id, ret, prev_bssid_set, prev_disabled;
3539 	struct wpa_ssid *ssid;
3540 	char *name, *value;
3541 	u8 prev_bssid[ETH_ALEN];
3542 
3543 	/* cmd: "<network id> <variable name> <value>" */
3544 	name = os_strchr(cmd, ' ');
3545 	if (name == NULL)
3546 		return -1;
3547 	*name++ = '\0';
3548 
3549 	value = os_strchr(name, ' ');
3550 	if (value == NULL)
3551 		return -1;
3552 	*value++ = '\0';
3553 
3554 	id = atoi(cmd);
3555 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
3556 		   id, name);
3557 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3558 			      (u8 *) value, os_strlen(value));
3559 
3560 	ssid = wpa_config_get_network(wpa_s->conf, id);
3561 	if (ssid == NULL) {
3562 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
3563 			   "id=%d", id);
3564 		return -1;
3565 	}
3566 
3567 	prev_bssid_set = ssid->bssid_set;
3568 	prev_disabled = ssid->disabled;
3569 	os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
3570 	ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
3571 						       value);
3572 	if (ret == 0 &&
3573 	    (ssid->bssid_set != prev_bssid_set ||
3574 	     os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
3575 		wpas_notify_network_bssid_set_changed(wpa_s, ssid);
3576 
3577 	if (prev_disabled != ssid->disabled &&
3578 	    (prev_disabled == 2 || ssid->disabled == 2))
3579 		wpas_notify_network_type_changed(wpa_s, ssid);
3580 
3581 	return ret;
3582 }
3583 
3584 
wpa_supplicant_ctrl_iface_get_network(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)3585 static int wpa_supplicant_ctrl_iface_get_network(
3586 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
3587 {
3588 	int id;
3589 	size_t res;
3590 	struct wpa_ssid *ssid;
3591 	char *name, *value;
3592 
3593 	/* cmd: "<network id> <variable name>" */
3594 	name = os_strchr(cmd, ' ');
3595 	if (name == NULL || buflen == 0)
3596 		return -1;
3597 	*name++ = '\0';
3598 
3599 	id = atoi(cmd);
3600 	wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
3601 		   id, name);
3602 
3603 	ssid = wpa_config_get_network(wpa_s->conf, id);
3604 	if (ssid == NULL) {
3605 		wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Could not find network "
3606 			   "id=%d", id);
3607 		return -1;
3608 	}
3609 
3610 	value = wpa_config_get_no_key(ssid, name);
3611 	if (value == NULL) {
3612 		wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Failed to get network "
3613 			   "variable '%s'", name);
3614 		return -1;
3615 	}
3616 
3617 	res = os_strlcpy(buf, value, buflen);
3618 	if (res >= buflen) {
3619 		os_free(value);
3620 		return -1;
3621 	}
3622 
3623 	os_free(value);
3624 
3625 	return res;
3626 }
3627 
3628 
wpa_supplicant_ctrl_iface_dup_network(struct wpa_supplicant * wpa_s,char * cmd,struct wpa_supplicant * dst_wpa_s)3629 static int wpa_supplicant_ctrl_iface_dup_network(
3630 	struct wpa_supplicant *wpa_s, char *cmd,
3631 	struct wpa_supplicant *dst_wpa_s)
3632 {
3633 	struct wpa_ssid *ssid_s, *ssid_d;
3634 	char *name, *id, *value;
3635 	int id_s, id_d, ret;
3636 
3637 	/* cmd: "<src network id> <dst network id> <variable name>" */
3638 	id = os_strchr(cmd, ' ');
3639 	if (id == NULL)
3640 		return -1;
3641 	*id++ = '\0';
3642 
3643 	name = os_strchr(id, ' ');
3644 	if (name == NULL)
3645 		return -1;
3646 	*name++ = '\0';
3647 
3648 	id_s = atoi(cmd);
3649 	id_d = atoi(id);
3650 
3651 	wpa_printf(MSG_DEBUG,
3652 		   "CTRL_IFACE: DUP_NETWORK ifname=%s->%s id=%d->%d name='%s'",
3653 		   wpa_s->ifname, dst_wpa_s->ifname, id_s, id_d, name);
3654 
3655 	ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
3656 	if (ssid_s == NULL) {
3657 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3658 			   "network id=%d", id_s);
3659 		return -1;
3660 	}
3661 
3662 	ssid_d = wpa_config_get_network(dst_wpa_s->conf, id_d);
3663 	if (ssid_d == NULL) {
3664 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
3665 			   "network id=%d", id_d);
3666 		return -1;
3667 	}
3668 
3669 	value = wpa_config_get(ssid_s, name);
3670 	if (value == NULL) {
3671 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
3672 			   "variable '%s'", name);
3673 		return -1;
3674 	}
3675 
3676 	ret = wpa_supplicant_ctrl_iface_update_network(dst_wpa_s, ssid_d, name,
3677 						       value);
3678 
3679 	os_free(value);
3680 
3681 	return ret;
3682 }
3683 
3684 
wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3685 static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
3686 						char *buf, size_t buflen)
3687 {
3688 	char *pos, *end;
3689 	struct wpa_cred *cred;
3690 	int ret;
3691 
3692 	pos = buf;
3693 	end = buf + buflen;
3694 	ret = os_snprintf(pos, end - pos,
3695 			  "cred id / realm / username / domain / imsi\n");
3696 	if (os_snprintf_error(end - pos, ret))
3697 		return pos - buf;
3698 	pos += ret;
3699 
3700 	cred = wpa_s->conf->cred;
3701 	while (cred) {
3702 		ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
3703 				  cred->id, cred->realm ? cred->realm : "",
3704 				  cred->username ? cred->username : "",
3705 				  cred->domain ? cred->domain[0] : "",
3706 				  cred->imsi ? cred->imsi : "");
3707 		if (os_snprintf_error(end - pos, ret))
3708 			return pos - buf;
3709 		pos += ret;
3710 
3711 		cred = cred->next;
3712 	}
3713 
3714 	return pos - buf;
3715 }
3716 
3717 
wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)3718 static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
3719 					      char *buf, size_t buflen)
3720 {
3721 	struct wpa_cred *cred;
3722 	int ret;
3723 
3724 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
3725 
3726 	cred = wpa_config_add_cred(wpa_s->conf);
3727 	if (cred == NULL)
3728 		return -1;
3729 
3730 	wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
3731 
3732 	ret = os_snprintf(buf, buflen, "%d\n", cred->id);
3733 	if (os_snprintf_error(buflen, ret))
3734 		return -1;
3735 	return ret;
3736 }
3737 
3738 
wpas_ctrl_remove_cred(struct wpa_supplicant * wpa_s,struct wpa_cred * cred)3739 static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
3740 				 struct wpa_cred *cred)
3741 {
3742 	struct wpa_ssid *ssid;
3743 	char str[20];
3744 	int id;
3745 
3746 	if (cred == NULL) {
3747 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3748 		return -1;
3749 	}
3750 
3751 	id = cred->id;
3752 	if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
3753 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
3754 		return -1;
3755 	}
3756 
3757 	wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
3758 
3759 	/* Remove any network entry created based on the removed credential */
3760 	ssid = wpa_s->conf->ssid;
3761 	while (ssid) {
3762 		if (ssid->parent_cred == cred) {
3763 			int res;
3764 
3765 			wpa_printf(MSG_DEBUG, "Remove network id %d since it "
3766 				   "used the removed credential", ssid->id);
3767 			res = os_snprintf(str, sizeof(str), "%d", ssid->id);
3768 			if (os_snprintf_error(sizeof(str), res))
3769 				str[sizeof(str) - 1] = '\0';
3770 			ssid = ssid->next;
3771 			wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
3772 		} else
3773 			ssid = ssid->next;
3774 	}
3775 
3776 	return 0;
3777 }
3778 
3779 
wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant * wpa_s,char * cmd)3780 static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
3781 						 char *cmd)
3782 {
3783 	int id;
3784 	struct wpa_cred *cred, *prev;
3785 
3786 	/* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
3787 	 * "provisioning_sp=<FQDN> */
3788 	if (os_strcmp(cmd, "all") == 0) {
3789 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
3790 		cred = wpa_s->conf->cred;
3791 		while (cred) {
3792 			prev = cred;
3793 			cred = cred->next;
3794 			wpas_ctrl_remove_cred(wpa_s, prev);
3795 		}
3796 		return 0;
3797 	}
3798 
3799 	if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
3800 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
3801 			   cmd + 8);
3802 		cred = wpa_s->conf->cred;
3803 		while (cred) {
3804 			prev = cred;
3805 			cred = cred->next;
3806 			if (prev->domain) {
3807 				size_t i;
3808 				for (i = 0; i < prev->num_domain; i++) {
3809 					if (os_strcmp(prev->domain[i], cmd + 8)
3810 					    != 0)
3811 						continue;
3812 					wpas_ctrl_remove_cred(wpa_s, prev);
3813 					break;
3814 				}
3815 			}
3816 		}
3817 		return 0;
3818 	}
3819 
3820 	if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
3821 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
3822 			   cmd + 16);
3823 		cred = wpa_s->conf->cred;
3824 		while (cred) {
3825 			prev = cred;
3826 			cred = cred->next;
3827 			if (prev->provisioning_sp &&
3828 			    os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
3829 				wpas_ctrl_remove_cred(wpa_s, prev);
3830 		}
3831 		return 0;
3832 	}
3833 
3834 	id = atoi(cmd);
3835 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
3836 
3837 	cred = wpa_config_get_cred(wpa_s->conf, id);
3838 	return wpas_ctrl_remove_cred(wpa_s, cred);
3839 }
3840 
3841 
wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant * wpa_s,char * cmd)3842 static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
3843 					      char *cmd)
3844 {
3845 	int id;
3846 	struct wpa_cred *cred;
3847 	char *name, *value;
3848 
3849 	/* cmd: "<cred id> <variable name> <value>" */
3850 	name = os_strchr(cmd, ' ');
3851 	if (name == NULL)
3852 		return -1;
3853 	*name++ = '\0';
3854 
3855 	value = os_strchr(name, ' ');
3856 	if (value == NULL)
3857 		return -1;
3858 	*value++ = '\0';
3859 
3860 	id = atoi(cmd);
3861 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
3862 		   id, name);
3863 	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
3864 			      (u8 *) value, os_strlen(value));
3865 
3866 	cred = wpa_config_get_cred(wpa_s->conf, id);
3867 	if (cred == NULL) {
3868 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3869 			   id);
3870 		return -1;
3871 	}
3872 
3873 	if (wpa_config_set_cred(cred, name, value, 0) < 0) {
3874 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
3875 			   "variable '%s'", name);
3876 		return -1;
3877 	}
3878 
3879 	wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
3880 
3881 	return 0;
3882 }
3883 
3884 
wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)3885 static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
3886 					      char *cmd, char *buf,
3887 					      size_t buflen)
3888 {
3889 	int id;
3890 	size_t res;
3891 	struct wpa_cred *cred;
3892 	char *name, *value;
3893 
3894 	/* cmd: "<cred id> <variable name>" */
3895 	name = os_strchr(cmd, ' ');
3896 	if (name == NULL)
3897 		return -1;
3898 	*name++ = '\0';
3899 
3900 	id = atoi(cmd);
3901 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
3902 		   id, name);
3903 
3904 	cred = wpa_config_get_cred(wpa_s->conf, id);
3905 	if (cred == NULL) {
3906 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
3907 			   id);
3908 		return -1;
3909 	}
3910 
3911 	value = wpa_config_get_cred_no_key(cred, name);
3912 	if (value == NULL) {
3913 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
3914 			   name);
3915 		return -1;
3916 	}
3917 
3918 	res = os_strlcpy(buf, value, buflen);
3919 	if (res >= buflen) {
3920 		os_free(value);
3921 		return -1;
3922 	}
3923 
3924 	os_free(value);
3925 
3926 	return res;
3927 }
3928 
3929 
3930 #ifndef CONFIG_NO_CONFIG_WRITE
wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant * wpa_s)3931 static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
3932 {
3933 	int ret;
3934 
3935 	if (!wpa_s->conf->update_config) {
3936 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
3937 			   "to update configuration (update_config=0)");
3938 		return -1;
3939 	}
3940 
3941 	ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
3942 	if (ret) {
3943 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
3944 			   "update configuration");
3945 	} else {
3946 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
3947 			   " updated");
3948 	}
3949 
3950 	return ret;
3951 }
3952 #endif /* CONFIG_NO_CONFIG_WRITE */
3953 
3954 
3955 struct cipher_info {
3956 	unsigned int capa;
3957 	const char *name;
3958 	int group_only;
3959 };
3960 
3961 static const struct cipher_info ciphers[] = {
3962 	{ WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
3963 	{ WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
3964 	{ WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
3965 	{ WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
3966 #ifndef CONFIG_NO_TKIP
3967 	{ WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
3968 #endif /* CONFIG_NO_TKIP */
3969 	{ WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
3970 #ifdef CONFIG_WEP
3971 	{ WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
3972 	{ WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
3973 #endif /* CONFIG_WEP */
3974 };
3975 
3976 static const struct cipher_info ciphers_group_mgmt[] = {
3977 	{ WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
3978 	{ WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
3979 	{ WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
3980 	{ WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
3981 };
3982 
3983 
ctrl_iface_get_capability_pairwise(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)3984 static int ctrl_iface_get_capability_pairwise(int res, bool strict,
3985 					      struct wpa_driver_capa *capa,
3986 					      char *buf, size_t buflen)
3987 {
3988 	int ret;
3989 	char *pos, *end;
3990 	size_t len;
3991 	unsigned int i;
3992 
3993 	pos = buf;
3994 	end = pos + buflen;
3995 
3996 	if (res < 0) {
3997 		if (strict)
3998 			return 0;
3999 #ifdef CONFIG_NO_TKIP
4000 		len = os_strlcpy(buf, "CCMP NONE", buflen);
4001 #else /* CONFIG_NO_TKIP */
4002 		len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
4003 #endif /* CONFIG_NO_TKIP */
4004 		if (len >= buflen)
4005 			return -1;
4006 		return len;
4007 	}
4008 
4009 	for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
4010 		if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
4011 			ret = os_snprintf(pos, end - pos, "%s%s",
4012 					  pos == buf ? "" : " ",
4013 					  ciphers[i].name);
4014 			if (os_snprintf_error(end - pos, ret))
4015 				return pos - buf;
4016 			pos += ret;
4017 		}
4018 	}
4019 
4020 	return pos - buf;
4021 }
4022 
4023 
ctrl_iface_get_capability_group(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4024 static int ctrl_iface_get_capability_group(int res, bool strict,
4025 					   struct wpa_driver_capa *capa,
4026 					   char *buf, size_t buflen)
4027 {
4028 	int ret;
4029 	char *pos, *end;
4030 	size_t len;
4031 	unsigned int i;
4032 
4033 	pos = buf;
4034 	end = pos + buflen;
4035 
4036 	if (res < 0) {
4037 		if (strict)
4038 			return 0;
4039 #ifdef CONFIG_WEP
4040 #ifdef CONFIG_NO_TKIP
4041 		len = os_strlcpy(buf, "CCMP WEP104 WEP40", buflen);
4042 #else /* CONFIG_NO_TKIP */
4043 		len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
4044 #endif /* CONFIG_NO_TKIP */
4045 #else /* CONFIG_WEP */
4046 #ifdef CONFIG_NO_TKIP
4047 		len = os_strlcpy(buf, "CCMP", buflen);
4048 #else /* CONFIG_NO_TKIP */
4049 		len = os_strlcpy(buf, "CCMP TKIP", buflen);
4050 #endif /* CONFIG_NO_TKIP */
4051 #endif /* CONFIG_WEP */
4052 		if (len >= buflen)
4053 			return -1;
4054 		return len;
4055 	}
4056 
4057 	for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
4058 		if (capa->enc & ciphers[i].capa) {
4059 			ret = os_snprintf(pos, end - pos, "%s%s",
4060 					  pos == buf ? "" : " ",
4061 					  ciphers[i].name);
4062 			if (os_snprintf_error(end - pos, ret))
4063 				return pos - buf;
4064 			pos += ret;
4065 		}
4066 	}
4067 
4068 	return pos - buf;
4069 }
4070 
4071 
ctrl_iface_get_capability_group_mgmt(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4072 static int ctrl_iface_get_capability_group_mgmt(int res, bool strict,
4073 						struct wpa_driver_capa *capa,
4074 						char *buf, size_t buflen)
4075 {
4076 	int ret;
4077 	char *pos, *end;
4078 	unsigned int i;
4079 
4080 	pos = buf;
4081 	end = pos + buflen;
4082 
4083 	if (res < 0)
4084 		return 0;
4085 
4086 	for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
4087 		if (capa->enc & ciphers_group_mgmt[i].capa) {
4088 			ret = os_snprintf(pos, end - pos, "%s%s",
4089 					  pos == buf ? "" : " ",
4090 					  ciphers_group_mgmt[i].name);
4091 			if (os_snprintf_error(end - pos, ret))
4092 				return pos - buf;
4093 			pos += ret;
4094 		}
4095 	}
4096 
4097 	return pos - buf;
4098 }
4099 
4100 
iftype_str_to_index(const char * iftype_str)4101 static int iftype_str_to_index(const char *iftype_str)
4102 {
4103 	if (!iftype_str)
4104 		return WPA_IF_MAX;
4105 
4106 	if (os_strcmp(iftype_str, "STATION") == 0)
4107 		return WPA_IF_STATION;
4108 
4109 	if (os_strcmp(iftype_str, "AP_VLAN") == 0)
4110 		return WPA_IF_AP_VLAN;
4111 
4112 	if (os_strcmp(iftype_str, "AP") == 0)
4113 		return WPA_IF_AP_BSS;
4114 
4115 	if (os_strcmp(iftype_str, "P2P_GO") == 0)
4116 		return WPA_IF_P2P_GO;
4117 
4118 	if (os_strcmp(iftype_str, "P2P_CLIENT") == 0)
4119 		return WPA_IF_P2P_CLIENT;
4120 
4121 	if (os_strcmp(iftype_str, "P2P_DEVICE") == 0)
4122 		return WPA_IF_P2P_DEVICE;
4123 
4124 	if (os_strcmp(iftype_str, "MESH") == 0)
4125 		return WPA_IF_MESH;
4126 
4127 	if (os_strcmp(iftype_str, "IBSS") == 0)
4128 		return WPA_IF_IBSS;
4129 
4130 	if (os_strcmp(iftype_str, "NAN") == 0)
4131 		return WPA_IF_NAN;
4132 
4133 	return WPA_IF_MAX;
4134 }
4135 
4136 
ctrl_iface_get_capability_key_mgmt(int res,bool strict,struct wpa_driver_capa * capa,const char * iftype_str,char * buf,size_t buflen)4137 static int ctrl_iface_get_capability_key_mgmt(int res, bool strict,
4138 					      struct wpa_driver_capa *capa,
4139 					      const char *iftype_str,
4140 					      char *buf, size_t buflen)
4141 {
4142 	int ret;
4143 	unsigned int key_mgmt;
4144 	char *pos, *end;
4145 	size_t len;
4146 
4147 	pos = buf;
4148 	end = pos + buflen;
4149 
4150 	if (res < 0) {
4151 		if (strict)
4152 			return 0;
4153 		len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
4154 				 "NONE", buflen);
4155 		if (len >= buflen)
4156 			return -1;
4157 		return len;
4158 	}
4159 
4160 	if (iftype_str) {
4161 		enum wpa_driver_if_type iftype;
4162 
4163 		iftype = iftype_str_to_index(iftype_str);
4164 		if (iftype == WPA_IF_MAX)
4165 			return -1;
4166 		key_mgmt = capa->key_mgmt_iftype[iftype];
4167 	} else {
4168 		key_mgmt = capa->key_mgmt;
4169 	}
4170 
4171 	ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
4172 	if (os_snprintf_error(end - pos, ret))
4173 		return pos - buf;
4174 	pos += ret;
4175 
4176 	if (key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4177 			WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
4178 		ret = os_snprintf(pos, end - pos, " WPA-EAP");
4179 		if (os_snprintf_error(end - pos, ret))
4180 			return pos - buf;
4181 		pos += ret;
4182 	}
4183 
4184 	if (key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
4185 			WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
4186 		ret = os_snprintf(pos, end - pos, " WPA-PSK");
4187 		if (os_snprintf_error(end - pos, ret))
4188 			return pos - buf;
4189 		pos += ret;
4190 	}
4191 
4192 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
4193 		ret = os_snprintf(pos, end - pos, " WPA-NONE");
4194 		if (os_snprintf_error(end - pos, ret))
4195 			return pos - buf;
4196 		pos += ret;
4197 	}
4198 
4199 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WAPI_PSK) {
4200 		ret = os_snprintf(pos, end - pos, " WAPI-PSK");
4201 		if (os_snprintf_error(end - pos, ret))
4202 			return pos - buf;
4203 		pos += ret;
4204 	}
4205 
4206 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_TPK_HANDSHAKE) {
4207 		ret = os_snprintf(pos, end - pos, " TPK-HANDSHAKE");
4208 		if (os_snprintf_error(end - pos, ret))
4209 			return pos - buf;
4210 		pos += ret;
4211 	}
4212 
4213 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_CCKM) {
4214 		ret = os_snprintf(pos, end - pos, " CCKM");
4215 		if (os_snprintf_error(end - pos, ret))
4216 			return pos - buf;
4217 		pos += ret;
4218 	}
4219 
4220 #ifdef CONFIG_SUITEB
4221 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
4222 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
4223 		if (os_snprintf_error(end - pos, ret))
4224 			return pos - buf;
4225 		pos += ret;
4226 	}
4227 #endif /* CONFIG_SUITEB */
4228 #ifdef CONFIG_SUITEB192
4229 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
4230 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
4231 		if (os_snprintf_error(end - pos, ret))
4232 			return pos - buf;
4233 		pos += ret;
4234 	}
4235 #endif /* CONFIG_SUITEB192 */
4236 #ifdef CONFIG_OWE
4237 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OWE) {
4238 		ret = os_snprintf(pos, end - pos, " OWE");
4239 		if (os_snprintf_error(end - pos, ret))
4240 			return pos - buf;
4241 		pos += ret;
4242 	}
4243 #endif /* CONFIG_OWE */
4244 #ifdef CONFIG_DPP
4245 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_DPP) {
4246 		ret = os_snprintf(pos, end - pos, " DPP");
4247 		if (os_snprintf_error(end - pos, ret))
4248 			return pos - buf;
4249 		pos += ret;
4250 	}
4251 #endif /* CONFIG_DPP */
4252 #ifdef CONFIG_FILS
4253 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA256) {
4254 		ret = os_snprintf(pos, end - pos, " FILS-SHA256");
4255 		if (os_snprintf_error(end - pos, ret))
4256 			return pos - buf;
4257 		pos += ret;
4258 	}
4259 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA384) {
4260 		ret = os_snprintf(pos, end - pos, " FILS-SHA384");
4261 		if (os_snprintf_error(end - pos, ret))
4262 			return pos - buf;
4263 		pos += ret;
4264 	}
4265 #ifdef CONFIG_IEEE80211R
4266 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA256) {
4267 		ret = os_snprintf(pos, end - pos, " FT-FILS-SHA256");
4268 		if (os_snprintf_error(end - pos, ret))
4269 			return pos - buf;
4270 		pos += ret;
4271 	}
4272 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA384) {
4273 		ret = os_snprintf(pos, end - pos, " FT-FILS-SHA384");
4274 		if (os_snprintf_error(end - pos, ret))
4275 			return pos - buf;
4276 		pos += ret;
4277 	}
4278 #endif /* CONFIG_IEEE80211R */
4279 #endif /* CONFIG_FILS */
4280 #ifdef CONFIG_IEEE80211R
4281 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK) {
4282 		ret = os_snprintf(pos, end - pos, " FT-PSK");
4283 		if (os_snprintf_error(end - pos, ret))
4284 			return pos - buf;
4285 		pos += ret;
4286 	}
4287 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT) {
4288 		ret = os_snprintf(pos, end - pos, " FT-EAP");
4289 		if (os_snprintf_error(end - pos, ret))
4290 			return pos - buf;
4291 		pos += ret;
4292 	}
4293 #ifdef CONFIG_SAE
4294 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_SAE) {
4295 		ret = os_snprintf(pos, end - pos, " FT-SAE");
4296 		if (os_snprintf_error(end - pos, ret))
4297 			return pos - buf;
4298 		pos += ret;
4299 	}
4300 #endif /* CONFIG_SAE */
4301 #ifdef CONFIG_SHA384
4302 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_802_1X_SHA384) {
4303 		ret = os_snprintf(pos, end - pos, " FT-EAP-SHA384");
4304 		if (os_snprintf_error(end - pos, ret))
4305 			return pos - buf;
4306 		pos += ret;
4307 	}
4308 #endif /* CONFIG_SHA384 */
4309 #endif /* CONFIG_IEEE80211R */
4310 #ifdef CONFIG_SAE
4311 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE) {
4312 		ret = os_snprintf(pos, end - pos, " SAE");
4313 		if (os_snprintf_error(end - pos, ret))
4314 			return pos - buf;
4315 		pos += ret;
4316 	}
4317 #endif /* CONFIG_SAE */
4318 #ifdef CONFIG_SHA256
4319 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_802_1X_SHA256) {
4320 		ret = os_snprintf(pos, end - pos, " WPA-EAP-SHA256");
4321 		if (os_snprintf_error(end - pos, ret))
4322 			return pos - buf;
4323 		pos += ret;
4324 	}
4325 
4326 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_PSK_SHA256) {
4327 		ret = os_snprintf(pos, end - pos, " WPA-PSK-SHA256");
4328 		if (os_snprintf_error(end - pos, ret))
4329 			return pos - buf;
4330 		pos += ret;
4331 	}
4332 #endif /* CONFIG_SHA256 */
4333 #ifdef CONFIG_HS20
4334 	if (key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OSEN) {
4335 		ret = os_snprintf(pos, end - pos, " OSEN");
4336 		if (os_snprintf_error(end - pos, ret))
4337 			return pos - buf;
4338 		pos += ret;
4339 	}
4340 #endif /* CONFIG_HS20 */
4341 
4342 	return pos - buf;
4343 }
4344 
4345 
ctrl_iface_get_capability_proto(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4346 static int ctrl_iface_get_capability_proto(int res, bool strict,
4347 					   struct wpa_driver_capa *capa,
4348 					   char *buf, size_t buflen)
4349 {
4350 	int ret;
4351 	char *pos, *end;
4352 	size_t len;
4353 
4354 	pos = buf;
4355 	end = pos + buflen;
4356 
4357 	if (res < 0) {
4358 		if (strict)
4359 			return 0;
4360 		len = os_strlcpy(buf, "RSN WPA", buflen);
4361 		if (len >= buflen)
4362 			return -1;
4363 		return len;
4364 	}
4365 
4366 	if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
4367 			      WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
4368 		ret = os_snprintf(pos, end - pos, "%sRSN",
4369 				  pos == buf ? "" : " ");
4370 		if (os_snprintf_error(end - pos, ret))
4371 			return pos - buf;
4372 		pos += ret;
4373 	}
4374 
4375 	if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
4376 			      WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
4377 		ret = os_snprintf(pos, end - pos, "%sWPA",
4378 				  pos == buf ? "" : " ");
4379 		if (os_snprintf_error(end - pos, ret))
4380 			return pos - buf;
4381 		pos += ret;
4382 	}
4383 
4384 	return pos - buf;
4385 }
4386 
4387 
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)4388 static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
4389 					      int res, bool strict,
4390 					      struct wpa_driver_capa *capa,
4391 					      char *buf, size_t buflen)
4392 {
4393 	int ret;
4394 	char *pos, *end;
4395 	size_t len;
4396 
4397 	pos = buf;
4398 	end = pos + buflen;
4399 
4400 	if (res < 0) {
4401 		if (strict)
4402 			return 0;
4403 		len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
4404 		if (len >= buflen)
4405 			return -1;
4406 		return len;
4407 	}
4408 
4409 	if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
4410 		ret = os_snprintf(pos, end - pos, "%sOPEN",
4411 				  pos == buf ? "" : " ");
4412 		if (os_snprintf_error(end - pos, ret))
4413 			return pos - buf;
4414 		pos += ret;
4415 	}
4416 
4417 	if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
4418 		ret = os_snprintf(pos, end - pos, "%sSHARED",
4419 				  pos == buf ? "" : " ");
4420 		if (os_snprintf_error(end - pos, ret))
4421 			return pos - buf;
4422 		pos += ret;
4423 	}
4424 
4425 	if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
4426 		ret = os_snprintf(pos, end - pos, "%sLEAP",
4427 				  pos == buf ? "" : " ");
4428 		if (os_snprintf_error(end - pos, ret))
4429 			return pos - buf;
4430 		pos += ret;
4431 	}
4432 
4433 #ifdef CONFIG_SAE
4434 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
4435 		ret = os_snprintf(pos, end - pos, "%sSAE",
4436 				  pos == buf ? "" : " ");
4437 		if (os_snprintf_error(end - pos, ret))
4438 			return pos - buf;
4439 		pos += ret;
4440 	}
4441 #endif /* CONFIG_SAE */
4442 
4443 #ifdef CONFIG_FILS
4444 	if (wpa_is_fils_supported(wpa_s)) {
4445 		ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITHOUT_PFS",
4446 				  pos == buf ? "" : " ");
4447 		if (os_snprintf_error(end - pos, ret))
4448 			return pos - buf;
4449 		pos += ret;
4450 	}
4451 
4452 #ifdef CONFIG_FILS_SK_PFS
4453 	if (wpa_is_fils_sk_pfs_supported(wpa_s)) {
4454 		ret = os_snprintf(pos, end - pos, "%sFILS_SK_WITH_PFS",
4455 				  pos == buf ? "" : " ");
4456 		if (os_snprintf_error(end - pos, ret))
4457 			return pos - buf;
4458 		pos += ret;
4459 	}
4460 #endif /* CONFIG_FILS_SK_PFS */
4461 #endif /* CONFIG_FILS */
4462 
4463 	return pos - buf;
4464 }
4465 
4466 
ctrl_iface_get_capability_modes(int res,bool strict,struct wpa_driver_capa * capa,char * buf,size_t buflen)4467 static int ctrl_iface_get_capability_modes(int res, bool strict,
4468 					   struct wpa_driver_capa *capa,
4469 					   char *buf, size_t buflen)
4470 {
4471 	int ret;
4472 	char *pos, *end;
4473 	size_t len;
4474 
4475 	pos = buf;
4476 	end = pos + buflen;
4477 
4478 	if (res < 0) {
4479 		if (strict)
4480 			return 0;
4481 		len = os_strlcpy(buf, "IBSS AP", buflen);
4482 		if (len >= buflen)
4483 			return -1;
4484 		return len;
4485 	}
4486 
4487 	if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
4488 		ret = os_snprintf(pos, end - pos, "%sIBSS",
4489 				  pos == buf ? "" : " ");
4490 		if (os_snprintf_error(end - pos, ret))
4491 			return pos - buf;
4492 		pos += ret;
4493 	}
4494 
4495 	if (capa->flags & WPA_DRIVER_FLAGS_AP) {
4496 		ret = os_snprintf(pos, end - pos, "%sAP",
4497 				  pos == buf ? "" : " ");
4498 		if (os_snprintf_error(end - pos, ret))
4499 			return pos - buf;
4500 		pos += ret;
4501 	}
4502 
4503 #ifdef CONFIG_MESH
4504 	if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
4505 		ret = os_snprintf(pos, end - pos, "%sMESH",
4506 				  pos == buf ? "" : " ");
4507 		if (os_snprintf_error(end - pos, ret))
4508 			return pos - buf;
4509 		pos += ret;
4510 	}
4511 #endif /* CONFIG_MESH */
4512 
4513 	return pos - buf;
4514 }
4515 
4516 
ctrl_iface_get_capability_channels(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)4517 static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
4518 					      char *buf, size_t buflen)
4519 {
4520 	struct hostapd_channel_data *chnl;
4521 	int ret, i, j;
4522 	char *pos, *end, *hmode;
4523 
4524 	pos = buf;
4525 	end = pos + buflen;
4526 
4527 	for (j = 0; j < wpa_s->hw.num_modes; j++) {
4528 		switch (wpa_s->hw.modes[j].mode) {
4529 		case HOSTAPD_MODE_IEEE80211B:
4530 			hmode = "B";
4531 			break;
4532 		case HOSTAPD_MODE_IEEE80211G:
4533 			hmode = "G";
4534 			break;
4535 		case HOSTAPD_MODE_IEEE80211A:
4536 			hmode = "A";
4537 			break;
4538 		case HOSTAPD_MODE_IEEE80211AD:
4539 			hmode = "AD";
4540 			break;
4541 		default:
4542 			continue;
4543 		}
4544 		ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
4545 		if (os_snprintf_error(end - pos, ret))
4546 			return pos - buf;
4547 		pos += ret;
4548 		chnl = wpa_s->hw.modes[j].channels;
4549 		for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
4550 			if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
4551 				continue;
4552 			ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
4553 			if (os_snprintf_error(end - pos, ret))
4554 				return pos - buf;
4555 			pos += ret;
4556 		}
4557 		ret = os_snprintf(pos, end - pos, "\n");
4558 		if (os_snprintf_error(end - pos, ret))
4559 			return pos - buf;
4560 		pos += ret;
4561 	}
4562 
4563 	return pos - buf;
4564 }
4565 
4566 
ctrl_iface_get_capability_freq(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)4567 static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
4568 					  char *buf, size_t buflen)
4569 {
4570 	struct hostapd_channel_data *chnl;
4571 	int ret, i, j;
4572 	char *pos, *end, *hmode;
4573 
4574 	pos = buf;
4575 	end = pos + buflen;
4576 
4577 	for (j = 0; j < wpa_s->hw.num_modes; j++) {
4578 		switch (wpa_s->hw.modes[j].mode) {
4579 		case HOSTAPD_MODE_IEEE80211B:
4580 			hmode = "B";
4581 			break;
4582 		case HOSTAPD_MODE_IEEE80211G:
4583 			hmode = "G";
4584 			break;
4585 		case HOSTAPD_MODE_IEEE80211A:
4586 			hmode = "A";
4587 			break;
4588 		case HOSTAPD_MODE_IEEE80211AD:
4589 			hmode = "AD";
4590 			break;
4591 		default:
4592 			continue;
4593 		}
4594 		ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
4595 				  hmode);
4596 		if (os_snprintf_error(end - pos, ret))
4597 			return pos - buf;
4598 		pos += ret;
4599 		chnl = wpa_s->hw.modes[j].channels;
4600 		for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
4601 			if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
4602 				continue;
4603 			ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
4604 					  chnl[i].chan, chnl[i].freq,
4605 					  chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
4606 					  " (NO_IR)" : "",
4607 					  chnl[i].flag & HOSTAPD_CHAN_RADAR ?
4608 					  " (DFS)" : "");
4609 
4610 			if (os_snprintf_error(end - pos, ret))
4611 				return pos - buf;
4612 			pos += ret;
4613 		}
4614 		ret = os_snprintf(pos, end - pos, "\n");
4615 		if (os_snprintf_error(end - pos, ret))
4616 			return pos - buf;
4617 		pos += ret;
4618 	}
4619 
4620 	return pos - buf;
4621 }
4622 
4623 
wpa_supplicant_ctrl_iface_get_capability(struct wpa_supplicant * wpa_s,const char * _field,char * buf,size_t buflen)4624 static int wpa_supplicant_ctrl_iface_get_capability(
4625 	struct wpa_supplicant *wpa_s, const char *_field, char *buf,
4626 	size_t buflen)
4627 {
4628 	struct wpa_driver_capa capa;
4629 	int res;
4630 	char *next_param, *curr_param, *iftype = NULL;
4631 	bool strict = false;
4632 	char field[50];
4633 	size_t len;
4634 
4635 	/* Determine whether or not strict checking was requested */
4636 	len = os_strlcpy(field, _field, sizeof(field));
4637 	if (len >= sizeof(field))
4638 		return -1;
4639 
4640 	next_param = os_strchr(field, ' ');
4641 	while (next_param) {
4642 		*next_param++ = '\0';
4643 		curr_param = next_param;
4644 		next_param = os_strchr(next_param, ' ');
4645 
4646 		if (next_param)
4647 			*next_param = '\0';
4648 
4649 		if (os_strcmp(curr_param, "strict") == 0)
4650 			strict = true;
4651 		else if (os_strncmp(curr_param, "iftype=", 7) == 0)
4652 			iftype = curr_param + 7;
4653 		else
4654 			return -1;
4655 	}
4656 
4657 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'%s%s%s",
4658 		   field, iftype ? " iftype=" : "", iftype ? iftype : "",
4659 		   strict ? " strict" : "");
4660 
4661 	if (os_strcmp(field, "eap") == 0) {
4662 		return eap_get_names(buf, buflen);
4663 	}
4664 
4665 	res = wpa_drv_get_capa(wpa_s, &capa);
4666 
4667 	if (os_strcmp(field, "pairwise") == 0)
4668 		return ctrl_iface_get_capability_pairwise(res, strict, &capa,
4669 							  buf, buflen);
4670 
4671 	if (os_strcmp(field, "group") == 0)
4672 		return ctrl_iface_get_capability_group(res, strict, &capa,
4673 						       buf, buflen);
4674 
4675 	if (os_strcmp(field, "group_mgmt") == 0)
4676 		return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
4677 							    buf, buflen);
4678 
4679 	if (os_strcmp(field, "key_mgmt") == 0)
4680 		return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
4681 							  iftype, buf, buflen);
4682 
4683 	if (os_strcmp(field, "proto") == 0)
4684 		return ctrl_iface_get_capability_proto(res, strict, &capa,
4685 						       buf, buflen);
4686 
4687 	if (os_strcmp(field, "auth_alg") == 0)
4688 		return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
4689 							  &capa, buf, buflen);
4690 
4691 	if (os_strcmp(field, "modes") == 0)
4692 		return ctrl_iface_get_capability_modes(res, strict, &capa,
4693 						       buf, buflen);
4694 
4695 	if (os_strcmp(field, "channels") == 0)
4696 		return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
4697 
4698 	if (os_strcmp(field, "freq") == 0)
4699 		return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
4700 
4701 #ifdef CONFIG_TDLS
4702 	if (os_strcmp(field, "tdls") == 0)
4703 		return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
4704 #endif /* CONFIG_TDLS */
4705 
4706 #ifdef CONFIG_ERP
4707 	if (os_strcmp(field, "erp") == 0) {
4708 		res = os_snprintf(buf, buflen, "ERP");
4709 		if (os_snprintf_error(buflen, res))
4710 			return -1;
4711 		return res;
4712 	}
4713 #endif /* CONFIG_EPR */
4714 
4715 #ifdef CONFIG_FIPS
4716 	if (os_strcmp(field, "fips") == 0) {
4717 		res = os_snprintf(buf, buflen, "FIPS");
4718 		if (os_snprintf_error(buflen, res))
4719 			return -1;
4720 		return res;
4721 	}
4722 #endif /* CONFIG_FIPS */
4723 
4724 #ifdef CONFIG_ACS
4725 	if (os_strcmp(field, "acs") == 0) {
4726 		res = os_snprintf(buf, buflen, "ACS");
4727 		if (os_snprintf_error(buflen, res))
4728 			return -1;
4729 		return res;
4730 	}
4731 #endif /* CONFIG_ACS */
4732 
4733 #ifdef CONFIG_FILS
4734 	if (os_strcmp(field, "fils") == 0) {
4735 #ifdef CONFIG_FILS_SK_PFS
4736 		if (wpa_is_fils_supported(wpa_s) &&
4737 		    wpa_is_fils_sk_pfs_supported(wpa_s)) {
4738 			res = os_snprintf(buf, buflen, "FILS FILS-SK-PFS");
4739 			if (os_snprintf_error(buflen, res))
4740 				return -1;
4741 			return res;
4742 		}
4743 #endif /* CONFIG_FILS_SK_PFS */
4744 
4745 		if (wpa_is_fils_supported(wpa_s)) {
4746 			res = os_snprintf(buf, buflen, "FILS");
4747 			if (os_snprintf_error(buflen, res))
4748 				return -1;
4749 			return res;
4750 		}
4751 	}
4752 #endif /* CONFIG_FILS */
4753 
4754 	if (os_strcmp(field, "multibss") == 0 && wpa_s->multi_bss_support) {
4755 		res = os_snprintf(buf, buflen, "MULTIBSS-STA");
4756 		if (os_snprintf_error(buflen, res))
4757 			return -1;
4758 		return res;
4759 	}
4760 
4761 #ifdef CONFIG_DPP
4762 	if (os_strcmp(field, "dpp") == 0) {
4763 #ifdef CONFIG_DPP2
4764 		res = os_snprintf(buf, buflen, "DPP=2");
4765 #else /* CONFIG_DPP2 */
4766 		res = os_snprintf(buf, buflen, "DPP=1");
4767 #endif /* CONFIG_DPP2 */
4768 		if (os_snprintf_error(buflen, res))
4769 			return -1;
4770 		return res;
4771 	}
4772 #endif /* CONFIG_DPP */
4773 
4774 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
4775 		   field);
4776 
4777 	return -1;
4778 }
4779 
4780 
4781 #ifdef CONFIG_INTERWORKING
anqp_add_hex(char * pos,char * end,const char * title,struct wpabuf * data)4782 static char * anqp_add_hex(char *pos, char *end, const char *title,
4783 			   struct wpabuf *data)
4784 {
4785 	char *start = pos;
4786 	size_t i;
4787 	int ret;
4788 	const u8 *d;
4789 
4790 	if (data == NULL)
4791 		return start;
4792 
4793 	ret = os_snprintf(pos, end - pos, "%s=", title);
4794 	if (os_snprintf_error(end - pos, ret))
4795 		return start;
4796 	pos += ret;
4797 
4798 	d = wpabuf_head_u8(data);
4799 	for (i = 0; i < wpabuf_len(data); i++) {
4800 		ret = os_snprintf(pos, end - pos, "%02x", *d++);
4801 		if (os_snprintf_error(end - pos, ret))
4802 			return start;
4803 		pos += ret;
4804 	}
4805 
4806 	ret = os_snprintf(pos, end - pos, "\n");
4807 	if (os_snprintf_error(end - pos, ret))
4808 		return start;
4809 	pos += ret;
4810 
4811 	return pos;
4812 }
4813 #endif /* CONFIG_INTERWORKING */
4814 
4815 
4816 #ifdef CONFIG_FILS
print_fils_indication(struct wpa_bss * bss,char * pos,char * end)4817 static int print_fils_indication(struct wpa_bss *bss, char *pos, char *end)
4818 {
4819 	char *start = pos;
4820 	const u8 *ie, *ie_end;
4821 	u16 info, realms;
4822 	int ret;
4823 
4824 	ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
4825 	if (!ie)
4826 		return 0;
4827 	ie_end = ie + 2 + ie[1];
4828 	ie += 2;
4829 	if (ie_end - ie < 2)
4830 		return -1;
4831 
4832 	info = WPA_GET_LE16(ie);
4833 	ie += 2;
4834 	ret = os_snprintf(pos, end - pos, "fils_info=%04x\n", info);
4835 	if (os_snprintf_error(end - pos, ret))
4836 		return 0;
4837 	pos += ret;
4838 
4839 	if (info & BIT(7)) {
4840 		/* Cache Identifier Included */
4841 		if (ie_end - ie < 2)
4842 			return -1;
4843 		ret = os_snprintf(pos, end - pos, "fils_cache_id=%02x%02x\n",
4844 				  ie[0], ie[1]);
4845 		if (os_snprintf_error(end - pos, ret))
4846 			return 0;
4847 		pos += ret;
4848 		ie += 2;
4849 	}
4850 
4851 	if (info & BIT(8)) {
4852 		/* HESSID Included */
4853 		if (ie_end - ie < ETH_ALEN)
4854 			return -1;
4855 		ret = os_snprintf(pos, end - pos, "fils_hessid=" MACSTR "\n",
4856 				  MAC2STR(ie));
4857 		if (os_snprintf_error(end - pos, ret))
4858 			return 0;
4859 		pos += ret;
4860 		ie += ETH_ALEN;
4861 	}
4862 
4863 	realms = (info & (BIT(3) | BIT(4) | BIT(5))) >> 3;
4864 	if (realms) {
4865 		if (ie_end - ie < realms * 2)
4866 			return -1;
4867 		ret = os_snprintf(pos, end - pos, "fils_realms=");
4868 		if (os_snprintf_error(end - pos, ret))
4869 			return 0;
4870 		pos += ret;
4871 
4872 		ret = wpa_snprintf_hex(pos, end - pos, ie, realms * 2);
4873 		if (ret <= 0)
4874 			return 0;
4875 		pos += ret;
4876 		ie += realms * 2;
4877 		ret = os_snprintf(pos, end - pos, "\n");
4878 		if (os_snprintf_error(end - pos, ret))
4879 			return 0;
4880 		pos += ret;
4881 	}
4882 
4883 	return pos - start;
4884 }
4885 #endif /* CONFIG_FILS */
4886 
4887 
print_bss_info(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,unsigned long mask,char * buf,size_t buflen)4888 static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4889 			  unsigned long mask, char *buf, size_t buflen)
4890 {
4891 	size_t i;
4892 	int ret;
4893 	char *pos, *end;
4894 	const u8 *ie, *ie2, *osen_ie, *mesh, *owe;
4895 
4896 	pos = buf;
4897 	end = buf + buflen;
4898 
4899 	if (mask & WPA_BSS_MASK_ID) {
4900 		ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
4901 		if (os_snprintf_error(end - pos, ret))
4902 			return 0;
4903 		pos += ret;
4904 	}
4905 
4906 	if (mask & WPA_BSS_MASK_BSSID) {
4907 		ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
4908 				  MAC2STR(bss->bssid));
4909 		if (os_snprintf_error(end - pos, ret))
4910 			return 0;
4911 		pos += ret;
4912 	}
4913 
4914 	if (mask & WPA_BSS_MASK_FREQ) {
4915 		ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
4916 		if (os_snprintf_error(end - pos, ret))
4917 			return 0;
4918 		pos += ret;
4919 	}
4920 
4921 	if (mask & WPA_BSS_MASK_BEACON_INT) {
4922 		ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
4923 				  bss->beacon_int);
4924 		if (os_snprintf_error(end - pos, ret))
4925 			return 0;
4926 		pos += ret;
4927 	}
4928 
4929 	if (mask & WPA_BSS_MASK_CAPABILITIES) {
4930 		ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
4931 				  bss->caps);
4932 		if (os_snprintf_error(end - pos, ret))
4933 			return 0;
4934 		pos += ret;
4935 	}
4936 
4937 	if (mask & WPA_BSS_MASK_QUAL) {
4938 		ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
4939 		if (os_snprintf_error(end - pos, ret))
4940 			return 0;
4941 		pos += ret;
4942 	}
4943 
4944 	if (mask & WPA_BSS_MASK_NOISE) {
4945 		ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
4946 		if (os_snprintf_error(end - pos, ret))
4947 			return 0;
4948 		pos += ret;
4949 	}
4950 
4951 	if (mask & WPA_BSS_MASK_LEVEL) {
4952 		ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
4953 		if (os_snprintf_error(end - pos, ret))
4954 			return 0;
4955 		pos += ret;
4956 	}
4957 
4958 	if (mask & WPA_BSS_MASK_TSF) {
4959 		ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
4960 				  (unsigned long long) bss->tsf);
4961 		if (os_snprintf_error(end - pos, ret))
4962 			return 0;
4963 		pos += ret;
4964 	}
4965 
4966 	if (mask & WPA_BSS_MASK_AGE) {
4967 		struct os_reltime now;
4968 
4969 		os_get_reltime(&now);
4970 		ret = os_snprintf(pos, end - pos, "age=%d\n",
4971 				  (int) (now.sec - bss->last_update.sec));
4972 		if (os_snprintf_error(end - pos, ret))
4973 			return 0;
4974 		pos += ret;
4975 	}
4976 
4977 	if (mask & WPA_BSS_MASK_IE) {
4978 		ret = os_snprintf(pos, end - pos, "ie=");
4979 		if (os_snprintf_error(end - pos, ret))
4980 			return 0;
4981 		pos += ret;
4982 
4983 		ie = (const u8 *) (bss + 1);
4984 		for (i = 0; i < bss->ie_len; i++) {
4985 			ret = os_snprintf(pos, end - pos, "%02x", *ie++);
4986 			if (os_snprintf_error(end - pos, ret))
4987 				return 0;
4988 			pos += ret;
4989 		}
4990 
4991 		ret = os_snprintf(pos, end - pos, "\n");
4992 		if (os_snprintf_error(end - pos, ret))
4993 			return 0;
4994 		pos += ret;
4995 	}
4996 
4997 	if (mask & WPA_BSS_MASK_FLAGS) {
4998 		ret = os_snprintf(pos, end - pos, "flags=");
4999 		if (os_snprintf_error(end - pos, ret))
5000 			return 0;
5001 		pos += ret;
5002 
5003 		mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
5004 
5005 		ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
5006 		if (ie)
5007 			pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
5008 						    2 + ie[1]);
5009 		ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
5010 		if (ie2)
5011 			pos = wpa_supplicant_ie_txt(pos, end,
5012 						    mesh ? "RSN" : "WPA2", ie2,
5013 						    2 + ie2[1]);
5014 		osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
5015 		if (osen_ie)
5016 			pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
5017 						    osen_ie, 2 + osen_ie[1]);
5018 		owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
5019 		if (owe) {
5020 			ret = os_snprintf(
5021 				pos, end - pos,
5022 				ie2 ? "[OWE-TRANS]" : "[OWE-TRANS-OPEN]");
5023 			if (os_snprintf_error(end - pos, ret))
5024 				return 0;
5025 			pos += ret;
5026 		}
5027 		pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
5028 		if (!ie && !ie2 && !osen_ie &&
5029 		    (bss->caps & IEEE80211_CAP_PRIVACY)) {
5030 			ret = os_snprintf(pos, end - pos, "[WEP]");
5031 			if (os_snprintf_error(end - pos, ret))
5032 				return 0;
5033 			pos += ret;
5034 		}
5035 
5036 		if (mesh) {
5037 			ret = os_snprintf(pos, end - pos, "[MESH]");
5038 			if (os_snprintf_error(end - pos, ret))
5039 				return 0;
5040 			pos += ret;
5041 		}
5042 
5043 		if (bss_is_dmg(bss)) {
5044 			const char *s;
5045 			ret = os_snprintf(pos, end - pos, "[DMG]");
5046 			if (os_snprintf_error(end - pos, ret))
5047 				return 0;
5048 			pos += ret;
5049 			switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
5050 			case IEEE80211_CAP_DMG_IBSS:
5051 				s = "[IBSS]";
5052 				break;
5053 			case IEEE80211_CAP_DMG_AP:
5054 				s = "[ESS]";
5055 				break;
5056 			case IEEE80211_CAP_DMG_PBSS:
5057 				s = "[PBSS]";
5058 				break;
5059 			default:
5060 				s = "";
5061 				break;
5062 			}
5063 			ret = os_snprintf(pos, end - pos, "%s", s);
5064 			if (os_snprintf_error(end - pos, ret))
5065 				return 0;
5066 			pos += ret;
5067 		} else {
5068 			if (bss->caps & IEEE80211_CAP_IBSS) {
5069 				ret = os_snprintf(pos, end - pos, "[IBSS]");
5070 				if (os_snprintf_error(end - pos, ret))
5071 					return 0;
5072 				pos += ret;
5073 			}
5074 			if (bss->caps & IEEE80211_CAP_ESS) {
5075 				ret = os_snprintf(pos, end - pos, "[ESS]");
5076 				if (os_snprintf_error(end - pos, ret))
5077 					return 0;
5078 				pos += ret;
5079 			}
5080 		}
5081 		if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
5082 		    wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
5083 			ret = os_snprintf(pos, end - pos, "[P2P]");
5084 			if (os_snprintf_error(end - pos, ret))
5085 				return 0;
5086 			pos += ret;
5087 		}
5088 #ifdef CONFIG_HS20
5089 		if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
5090 			ret = os_snprintf(pos, end - pos, "[HS20]");
5091 			if (os_snprintf_error(end - pos, ret))
5092 				return 0;
5093 			pos += ret;
5094 		}
5095 #endif /* CONFIG_HS20 */
5096 #ifdef CONFIG_FILS
5097 		if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
5098 			ret = os_snprintf(pos, end - pos, "[FILS]");
5099 			if (os_snprintf_error(end - pos, ret))
5100 				return 0;
5101 			pos += ret;
5102 		}
5103 #endif /* CONFIG_FILS */
5104 #ifdef CONFIG_FST
5105 		if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
5106 			ret = os_snprintf(pos, end - pos, "[FST]");
5107 			if (os_snprintf_error(end - pos, ret))
5108 				return 0;
5109 			pos += ret;
5110 		}
5111 #endif /* CONFIG_FST */
5112 		if (wpa_bss_ext_capab(bss, WLAN_EXT_CAPAB_UTF_8_SSID)) {
5113 			ret = os_snprintf(pos, end - pos, "[UTF-8]");
5114 			if (os_snprintf_error(end - pos, ret))
5115 				return 0;
5116 			pos += ret;
5117 		}
5118 
5119 		ret = os_snprintf(pos, end - pos, "\n");
5120 		if (os_snprintf_error(end - pos, ret))
5121 			return 0;
5122 		pos += ret;
5123 	}
5124 
5125 	if (mask & WPA_BSS_MASK_SSID) {
5126 		ret = os_snprintf(pos, end - pos, "ssid=%s\n",
5127 				  wpa_ssid_txt(bss->ssid, bss->ssid_len));
5128 		if (os_snprintf_error(end - pos, ret))
5129 			return 0;
5130 		pos += ret;
5131 	}
5132 
5133 #ifdef CONFIG_WPS
5134 	if (mask & WPA_BSS_MASK_WPS_SCAN) {
5135 		ie = (const u8 *) (bss + 1);
5136 		ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
5137 		if (ret >= end - pos)
5138 			return 0;
5139 		if (ret > 0)
5140 			pos += ret;
5141 	}
5142 #endif /* CONFIG_WPS */
5143 
5144 #ifdef CONFIG_P2P
5145 	if (mask & WPA_BSS_MASK_P2P_SCAN) {
5146 		ie = (const u8 *) (bss + 1);
5147 		ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
5148 		if (ret >= end - pos)
5149 			return 0;
5150 		if (ret > 0)
5151 			pos += ret;
5152 	}
5153 #endif /* CONFIG_P2P */
5154 
5155 #ifdef CONFIG_WIFI_DISPLAY
5156 	if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
5157 		struct wpabuf *wfd;
5158 		ie = (const u8 *) (bss + 1);
5159 		wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
5160 						  WFD_IE_VENDOR_TYPE);
5161 		if (wfd) {
5162 			ret = os_snprintf(pos, end - pos, "wfd_subelems=");
5163 			if (os_snprintf_error(end - pos, ret)) {
5164 				wpabuf_free(wfd);
5165 				return 0;
5166 			}
5167 			pos += ret;
5168 
5169 			pos += wpa_snprintf_hex(pos, end - pos,
5170 						wpabuf_head(wfd),
5171 						wpabuf_len(wfd));
5172 			wpabuf_free(wfd);
5173 
5174 			ret = os_snprintf(pos, end - pos, "\n");
5175 			if (os_snprintf_error(end - pos, ret))
5176 				return 0;
5177 			pos += ret;
5178 		}
5179 	}
5180 #endif /* CONFIG_WIFI_DISPLAY */
5181 
5182 #ifdef CONFIG_INTERWORKING
5183 	if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
5184 		struct wpa_bss_anqp *anqp = bss->anqp;
5185 		struct wpa_bss_anqp_elem *elem;
5186 
5187 		pos = anqp_add_hex(pos, end, "anqp_capability_list",
5188 				   anqp->capability_list);
5189 		pos = anqp_add_hex(pos, end, "anqp_venue_name",
5190 				   anqp->venue_name);
5191 		pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
5192 				   anqp->network_auth_type);
5193 		pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
5194 				   anqp->roaming_consortium);
5195 		pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
5196 				   anqp->ip_addr_type_availability);
5197 		pos = anqp_add_hex(pos, end, "anqp_nai_realm",
5198 				   anqp->nai_realm);
5199 		pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
5200 		pos = anqp_add_hex(pos, end, "anqp_domain_name",
5201 				   anqp->domain_name);
5202 		pos = anqp_add_hex(pos, end, "anqp_fils_realm_info",
5203 				   anqp->fils_realm_info);
5204 #ifdef CONFIG_HS20
5205 		pos = anqp_add_hex(pos, end, "hs20_capability_list",
5206 				   anqp->hs20_capability_list);
5207 		pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
5208 				   anqp->hs20_operator_friendly_name);
5209 		pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
5210 				   anqp->hs20_wan_metrics);
5211 		pos = anqp_add_hex(pos, end, "hs20_connection_capability",
5212 				   anqp->hs20_connection_capability);
5213 		pos = anqp_add_hex(pos, end, "hs20_operating_class",
5214 				   anqp->hs20_operating_class);
5215 		pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
5216 				   anqp->hs20_osu_providers_list);
5217 		pos = anqp_add_hex(pos, end, "hs20_operator_icon_metadata",
5218 				   anqp->hs20_operator_icon_metadata);
5219 		pos = anqp_add_hex(pos, end, "hs20_osu_providers_nai_list",
5220 				   anqp->hs20_osu_providers_nai_list);
5221 #endif /* CONFIG_HS20 */
5222 
5223 		dl_list_for_each(elem, &anqp->anqp_elems,
5224 				 struct wpa_bss_anqp_elem, list) {
5225 			char title[20];
5226 
5227 			os_snprintf(title, sizeof(title), "anqp[%u]",
5228 				    elem->infoid);
5229 			pos = anqp_add_hex(pos, end, title, elem->payload);
5230 		}
5231 	}
5232 #endif /* CONFIG_INTERWORKING */
5233 
5234 #ifdef CONFIG_MESH
5235 	if (mask & WPA_BSS_MASK_MESH_SCAN) {
5236 		ie = (const u8 *) (bss + 1);
5237 		ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
5238 		if (ret >= end - pos)
5239 			return 0;
5240 		if (ret > 0)
5241 			pos += ret;
5242 	}
5243 #endif /* CONFIG_MESH */
5244 
5245 	if (mask & WPA_BSS_MASK_SNR) {
5246 		ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
5247 		if (os_snprintf_error(end - pos, ret))
5248 			return 0;
5249 		pos += ret;
5250 	}
5251 
5252 	if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
5253 		ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
5254 				  bss->est_throughput);
5255 		if (os_snprintf_error(end - pos, ret))
5256 			return 0;
5257 		pos += ret;
5258 	}
5259 
5260 #ifdef CONFIG_FST
5261 	if (mask & WPA_BSS_MASK_FST) {
5262 		ret = fst_ctrl_iface_mb_info(bss->bssid, pos, end - pos);
5263 		if (ret < 0 || ret >= end - pos)
5264 			return 0;
5265 		pos += ret;
5266 	}
5267 #endif /* CONFIG_FST */
5268 
5269 	if (mask & WPA_BSS_MASK_UPDATE_IDX) {
5270 		ret = os_snprintf(pos, end - pos, "update_idx=%u\n",
5271 				  bss->last_update_idx);
5272 		if (os_snprintf_error(end - pos, ret))
5273 			return 0;
5274 		pos += ret;
5275 	}
5276 
5277 	if ((mask & WPA_BSS_MASK_BEACON_IE) && bss->beacon_ie_len) {
5278 		ret = os_snprintf(pos, end - pos, "beacon_ie=");
5279 		if (os_snprintf_error(end - pos, ret))
5280 			return 0;
5281 		pos += ret;
5282 
5283 		ie = (const u8 *) (bss + 1);
5284 		ie += bss->ie_len;
5285 		for (i = 0; i < bss->beacon_ie_len; i++) {
5286 			ret = os_snprintf(pos, end - pos, "%02x", *ie++);
5287 			if (os_snprintf_error(end - pos, ret))
5288 				return 0;
5289 			pos += ret;
5290 		}
5291 
5292 		ret = os_snprintf(pos, end - pos, "\n");
5293 		if (os_snprintf_error(end - pos, ret))
5294 			return 0;
5295 		pos += ret;
5296 	}
5297 
5298 #ifdef CONFIG_FILS
5299 	if (mask & WPA_BSS_MASK_FILS_INDICATION) {
5300 		ret = print_fils_indication(bss, pos, end);
5301 		if (ret < 0)
5302 			return 0;
5303 		pos += ret;
5304 	}
5305 #endif /* CONFIG_FILS */
5306 
5307 	if (mask & WPA_BSS_MASK_DELIM) {
5308 		ret = os_snprintf(pos, end - pos, "====\n");
5309 		if (os_snprintf_error(end - pos, ret))
5310 			return 0;
5311 		pos += ret;
5312 	}
5313 
5314 	return pos - buf;
5315 }
5316 
5317 
wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)5318 static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
5319 					 const char *cmd, char *buf,
5320 					 size_t buflen)
5321 {
5322 	u8 bssid[ETH_ALEN];
5323 	size_t i;
5324 	struct wpa_bss *bss;
5325 	struct wpa_bss *bsslast = NULL;
5326 	struct dl_list *next;
5327 	int ret = 0;
5328 	int len;
5329 	char *ctmp, *end = buf + buflen;
5330 	unsigned long mask = WPA_BSS_MASK_ALL;
5331 
5332 	if (os_strncmp(cmd, "RANGE=", 6) == 0) {
5333 		if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
5334 			bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
5335 					    list_id);
5336 			bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
5337 					       list_id);
5338 		} else { /* N1-N2 */
5339 			unsigned int id1, id2;
5340 
5341 			if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
5342 				wpa_printf(MSG_INFO, "Wrong BSS range "
5343 					   "format");
5344 				return 0;
5345 			}
5346 
5347 			if (*(cmd + 6) == '-')
5348 				id1 = 0;
5349 			else
5350 				id1 = atoi(cmd + 6);
5351 			ctmp++;
5352 			if (*ctmp >= '0' && *ctmp <= '9')
5353 				id2 = atoi(ctmp);
5354 			else
5355 				id2 = (unsigned int) -1;
5356 			bss = wpa_bss_get_id_range(wpa_s, id1, id2);
5357 			if (id2 == (unsigned int) -1)
5358 				bsslast = dl_list_last(&wpa_s->bss_id,
5359 						       struct wpa_bss,
5360 						       list_id);
5361 			else {
5362 				bsslast = wpa_bss_get_id(wpa_s, id2);
5363 				if (bsslast == NULL && bss && id2 > id1) {
5364 					struct wpa_bss *tmp = bss;
5365 					for (;;) {
5366 						next = tmp->list_id.next;
5367 						if (next == &wpa_s->bss_id)
5368 							break;
5369 						tmp = dl_list_entry(
5370 							next, struct wpa_bss,
5371 							list_id);
5372 						if (tmp->id > id2)
5373 							break;
5374 						bsslast = tmp;
5375 					}
5376 				}
5377 			}
5378 		}
5379 	} else if (os_strncmp(cmd, "FIRST", 5) == 0)
5380 		bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
5381 	else if (os_strncmp(cmd, "LAST", 4) == 0)
5382 		bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
5383 	else if (os_strncmp(cmd, "ID-", 3) == 0) {
5384 		i = atoi(cmd + 3);
5385 		bss = wpa_bss_get_id(wpa_s, i);
5386 	} else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
5387 		i = atoi(cmd + 5);
5388 		bss = wpa_bss_get_id(wpa_s, i);
5389 		if (bss) {
5390 			next = bss->list_id.next;
5391 			if (next == &wpa_s->bss_id)
5392 				bss = NULL;
5393 			else
5394 				bss = dl_list_entry(next, struct wpa_bss,
5395 						    list_id);
5396 		}
5397 	} else if (os_strncmp(cmd, "CURRENT", 7) == 0) {
5398 		bss = wpa_s->current_bss;
5399 #ifdef CONFIG_P2P
5400 	} else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
5401 		if (hwaddr_aton(cmd + 13, bssid) == 0)
5402 			bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
5403 		else
5404 			bss = NULL;
5405 #endif /* CONFIG_P2P */
5406 	} else if (hwaddr_aton(cmd, bssid) == 0)
5407 		bss = wpa_bss_get_bssid(wpa_s, bssid);
5408 	else {
5409 		struct wpa_bss *tmp;
5410 		i = atoi(cmd);
5411 		bss = NULL;
5412 		dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
5413 		{
5414 			if (i == 0) {
5415 				bss = tmp;
5416 				break;
5417 			}
5418 			i--;
5419 		}
5420 	}
5421 
5422 	if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
5423 		mask = strtoul(ctmp + 5, NULL, 0x10);
5424 		if (mask == 0)
5425 			mask = WPA_BSS_MASK_ALL;
5426 	}
5427 
5428 	if (bss == NULL)
5429 		return 0;
5430 
5431 	if (bsslast == NULL)
5432 		bsslast = bss;
5433 	do {
5434 		len = print_bss_info(wpa_s, bss, mask, buf, buflen);
5435 		ret += len;
5436 		buf += len;
5437 		buflen -= len;
5438 		if (bss == bsslast) {
5439 			if ((mask & WPA_BSS_MASK_DELIM) && len &&
5440 			    (bss == dl_list_last(&wpa_s->bss_id,
5441 						 struct wpa_bss, list_id))) {
5442 				int res;
5443 
5444 				res = os_snprintf(buf - 5, end - buf + 5,
5445 						  "####\n");
5446 				if (os_snprintf_error(end - buf + 5, res)) {
5447 					wpa_printf(MSG_DEBUG,
5448 						   "Could not add end delim");
5449 				}
5450 			}
5451 			break;
5452 		}
5453 		next = bss->list_id.next;
5454 		if (next == &wpa_s->bss_id)
5455 			break;
5456 		bss = dl_list_entry(next, struct wpa_bss, list_id);
5457 	} while (bss && len);
5458 
5459 	return ret;
5460 }
5461 
5462 
wpa_supplicant_ctrl_iface_ap_scan(struct wpa_supplicant * wpa_s,char * cmd)5463 static int wpa_supplicant_ctrl_iface_ap_scan(
5464 	struct wpa_supplicant *wpa_s, char *cmd)
5465 {
5466 	int ap_scan = atoi(cmd);
5467 	return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
5468 }
5469 
5470 
wpa_supplicant_ctrl_iface_scan_interval(struct wpa_supplicant * wpa_s,char * cmd)5471 static int wpa_supplicant_ctrl_iface_scan_interval(
5472 	struct wpa_supplicant *wpa_s, char *cmd)
5473 {
5474 	int scan_int = atoi(cmd);
5475 	return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
5476 }
5477 
5478 
wpa_supplicant_ctrl_iface_bss_expire_age(struct wpa_supplicant * wpa_s,char * cmd)5479 static int wpa_supplicant_ctrl_iface_bss_expire_age(
5480 	struct wpa_supplicant *wpa_s, char *cmd)
5481 {
5482 	int expire_age = atoi(cmd);
5483 	return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
5484 }
5485 
5486 
wpa_supplicant_ctrl_iface_bss_expire_count(struct wpa_supplicant * wpa_s,char * cmd)5487 static int wpa_supplicant_ctrl_iface_bss_expire_count(
5488 	struct wpa_supplicant *wpa_s, char *cmd)
5489 {
5490 	int expire_count = atoi(cmd);
5491 	return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
5492 }
5493 
5494 
wpa_supplicant_ctrl_iface_bss_flush(struct wpa_supplicant * wpa_s,char * cmd)5495 static void wpa_supplicant_ctrl_iface_bss_flush(
5496 	struct wpa_supplicant *wpa_s, char *cmd)
5497 {
5498 	int flush_age = atoi(cmd);
5499 
5500 	if (flush_age == 0)
5501 		wpa_bss_flush(wpa_s);
5502 	else
5503 		wpa_bss_flush_by_age(wpa_s, flush_age);
5504 }
5505 
5506 
5507 #ifdef CONFIG_TESTING_OPTIONS
wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant * wpa_s)5508 static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
5509 {
5510 	wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
5511 	/* MLME-DELETEKEYS.request */
5512 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL,
5513 			0, KEY_FLAG_GROUP);
5514 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL,
5515 			0, KEY_FLAG_GROUP);
5516 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL,
5517 			0, KEY_FLAG_GROUP);
5518 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL,
5519 			0, KEY_FLAG_GROUP);
5520 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL,
5521 			0, KEY_FLAG_GROUP);
5522 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL,
5523 			0, KEY_FLAG_GROUP);
5524 
5525 	wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
5526 			0, KEY_FLAG_PAIRWISE);
5527 	if (wpa_sm_ext_key_id(wpa_s->wpa))
5528 		wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 1, 0,
5529 				NULL, 0, NULL, 0, KEY_FLAG_PAIRWISE);
5530 	/* MLME-SETPROTECTION.request(None) */
5531 	wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
5532 				   MLME_SETPROTECTION_PROTECT_TYPE_NONE,
5533 				   MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
5534 	wpa_sm_drop_sa(wpa_s->wpa);
5535 }
5536 #endif /* CONFIG_TESTING_OPTIONS */
5537 
5538 
wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant * wpa_s,char * addr)5539 static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
5540 					  char *addr)
5541 {
5542 #ifdef CONFIG_NO_SCAN_PROCESSING
5543 	return -1;
5544 #else /* CONFIG_NO_SCAN_PROCESSING */
5545 	u8 bssid[ETH_ALEN];
5546 	struct wpa_bss *bss;
5547 	struct wpa_ssid *ssid = wpa_s->current_ssid;
5548 
5549 	if (hwaddr_aton(addr, bssid)) {
5550 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
5551 			   "address '%s'", addr);
5552 		return -1;
5553 	}
5554 
5555 	wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
5556 
5557 	if (!ssid) {
5558 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
5559 			   "configuration known for the target AP");
5560 		return -1;
5561 	}
5562 
5563 	bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
5564 	if (!bss) {
5565 		wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
5566 			   "from BSS table");
5567 		return -1;
5568 	}
5569 
5570 	/*
5571 	 * TODO: Find best network configuration block from configuration to
5572 	 * allow roaming to other networks
5573 	 */
5574 
5575 	wpa_s->reassociate = 1;
5576 	wpa_supplicant_connect(wpa_s, bss, ssid);
5577 
5578 	return 0;
5579 #endif /* CONFIG_NO_SCAN_PROCESSING */
5580 }
5581 
5582 
5583 #ifdef CONFIG_P2P
p2p_ctrl_find(struct wpa_supplicant * wpa_s,char * cmd)5584 static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
5585 {
5586 	unsigned int timeout = atoi(cmd);
5587 	enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
5588 	u8 dev_id[ETH_ALEN], *_dev_id = NULL;
5589 	u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
5590 	char *pos;
5591 	unsigned int search_delay;
5592 	const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
5593 	u8 seek_count = 0;
5594 	int freq = 0;
5595 
5596 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
5597 		wpa_dbg(wpa_s, MSG_INFO,
5598 			"Reject P2P_FIND since interface is disabled");
5599 		return -1;
5600 	}
5601 	if (os_strstr(cmd, "type=social"))
5602 		type = P2P_FIND_ONLY_SOCIAL;
5603 	else if (os_strstr(cmd, "type=progressive"))
5604 		type = P2P_FIND_PROGRESSIVE;
5605 
5606 	pos = os_strstr(cmd, "dev_id=");
5607 	if (pos) {
5608 		pos += 7;
5609 		if (hwaddr_aton(pos, dev_id))
5610 			return -1;
5611 		_dev_id = dev_id;
5612 	}
5613 
5614 	pos = os_strstr(cmd, "dev_type=");
5615 	if (pos) {
5616 		pos += 9;
5617 		if (wps_dev_type_str2bin(pos, dev_type) < 0)
5618 			return -1;
5619 		_dev_type = dev_type;
5620 	}
5621 
5622 	pos = os_strstr(cmd, "delay=");
5623 	if (pos) {
5624 		pos += 6;
5625 		search_delay = atoi(pos);
5626 	} else
5627 		search_delay = wpas_p2p_search_delay(wpa_s);
5628 
5629 	pos = os_strstr(cmd, "freq=");
5630 	if (pos) {
5631 		pos += 5;
5632 		freq = atoi(pos);
5633 		if (freq <= 0)
5634 			return -1;
5635 	}
5636 
5637 	/* Must be searched for last, because it adds nul termination */
5638 	pos = os_strstr(cmd, " seek=");
5639 	if (pos)
5640 		pos += 6;
5641 	while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
5642 		char *term;
5643 
5644 		_seek[seek_count++] = pos;
5645 		seek = _seek;
5646 		term = os_strchr(pos, ' ');
5647 		if (!term)
5648 			break;
5649 		*term = '\0';
5650 		pos = os_strstr(term + 1, "seek=");
5651 		if (pos)
5652 			pos += 5;
5653 	}
5654 	if (seek_count > P2P_MAX_QUERY_HASH) {
5655 		seek[0] = NULL;
5656 		seek_count = 1;
5657 	}
5658 
5659 	return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
5660 			     _dev_id, search_delay, seek_count, seek, freq);
5661 }
5662 
5663 
p2ps_ctrl_parse_cpt_priority(const char * pos,u8 * cpt)5664 static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
5665 {
5666 	const char *last = NULL;
5667 	const char *token;
5668 	long int token_len;
5669 	unsigned int i;
5670 
5671 	/* Expected predefined CPT names delimited by ':' */
5672 	for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
5673 		if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
5674 			wpa_printf(MSG_ERROR,
5675 				   "P2PS: CPT name list is too long, expected up to %d names",
5676 				   P2PS_FEATURE_CAPAB_CPT_MAX);
5677 			cpt[0] = 0;
5678 			return -1;
5679 		}
5680 
5681 		token_len = last - token;
5682 
5683 		if (token_len  == 3 &&
5684 		    os_memcmp(token, "UDP", token_len) == 0) {
5685 			cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
5686 		} else if (token_len == 3 &&
5687 			   os_memcmp(token, "MAC", token_len) == 0) {
5688 			cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
5689 		} else {
5690 			wpa_printf(MSG_ERROR,
5691 				   "P2PS: Unsupported CPT name '%s'", token);
5692 			cpt[0] = 0;
5693 			return -1;
5694 		}
5695 
5696 		if (isblank((unsigned char) *last)) {
5697 			i++;
5698 			break;
5699 		}
5700 	}
5701 	cpt[i] = 0;
5702 	return 0;
5703 }
5704 
5705 
p2p_parse_asp_provision_cmd(const char * cmd)5706 static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
5707 {
5708 	struct p2ps_provision *p2ps_prov;
5709 	char *pos;
5710 	size_t info_len = 0;
5711 	char *info = NULL;
5712 	u8 role = P2PS_SETUP_NONE;
5713 	long long unsigned val;
5714 	int i;
5715 
5716 	pos = os_strstr(cmd, "info=");
5717 	if (pos) {
5718 		pos += 5;
5719 		info_len = os_strlen(pos);
5720 
5721 		if (info_len) {
5722 			info = os_malloc(info_len + 1);
5723 			if (info) {
5724 				info_len = utf8_unescape(pos, info_len,
5725 							 info, info_len + 1);
5726 			} else
5727 				info_len = 0;
5728 		}
5729 	}
5730 
5731 	p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
5732 	if (p2ps_prov == NULL) {
5733 		os_free(info);
5734 		return NULL;
5735 	}
5736 
5737 	if (info) {
5738 		os_memcpy(p2ps_prov->info, info, info_len);
5739 		p2ps_prov->info[info_len] = '\0';
5740 		os_free(info);
5741 	}
5742 
5743 	pos = os_strstr(cmd, "status=");
5744 	if (pos)
5745 		p2ps_prov->status = atoi(pos + 7);
5746 	else
5747 		p2ps_prov->status = -1;
5748 
5749 	pos = os_strstr(cmd, "adv_id=");
5750 	if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
5751 		goto invalid_args;
5752 	p2ps_prov->adv_id = val;
5753 
5754 	pos = os_strstr(cmd, "method=");
5755 	if (pos)
5756 		p2ps_prov->method = strtol(pos + 7, NULL, 16);
5757 	else
5758 		p2ps_prov->method = 0;
5759 
5760 	pos = os_strstr(cmd, "session=");
5761 	if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
5762 		goto invalid_args;
5763 	p2ps_prov->session_id = val;
5764 
5765 	pos = os_strstr(cmd, "adv_mac=");
5766 	if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
5767 		goto invalid_args;
5768 
5769 	pos = os_strstr(cmd, "session_mac=");
5770 	if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
5771 		goto invalid_args;
5772 
5773 	pos = os_strstr(cmd, "cpt=");
5774 	if (pos) {
5775 		if (p2ps_ctrl_parse_cpt_priority(pos + 4,
5776 						 p2ps_prov->cpt_priority))
5777 			goto invalid_args;
5778 	} else {
5779 		p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
5780 	}
5781 
5782 	for (i = 0; p2ps_prov->cpt_priority[i]; i++)
5783 		p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
5784 
5785 	/* force conncap with tstCap (no sanity checks) */
5786 	pos = os_strstr(cmd, "tstCap=");
5787 	if (pos) {
5788 		role = strtol(pos + 7, NULL, 16);
5789 	} else {
5790 		pos = os_strstr(cmd, "role=");
5791 		if (pos) {
5792 			role = strtol(pos + 5, NULL, 16);
5793 			if (role != P2PS_SETUP_CLIENT &&
5794 			    role != P2PS_SETUP_GROUP_OWNER)
5795 				role = P2PS_SETUP_NONE;
5796 		}
5797 	}
5798 	p2ps_prov->role = role;
5799 
5800 	return p2ps_prov;
5801 
5802 invalid_args:
5803 	os_free(p2ps_prov);
5804 	return NULL;
5805 }
5806 
5807 
p2p_ctrl_asp_provision_resp(struct wpa_supplicant * wpa_s,char * cmd)5808 static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
5809 {
5810 	u8 addr[ETH_ALEN];
5811 	struct p2ps_provision *p2ps_prov;
5812 	char *pos;
5813 
5814 	/* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
5815 
5816 	wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
5817 
5818 	if (hwaddr_aton(cmd, addr))
5819 		return -1;
5820 
5821 	pos = cmd + 17;
5822 	if (*pos != ' ')
5823 		return -1;
5824 
5825 	p2ps_prov = p2p_parse_asp_provision_cmd(pos);
5826 	if (!p2ps_prov)
5827 		return -1;
5828 
5829 	if (p2ps_prov->status < 0) {
5830 		os_free(p2ps_prov);
5831 		return -1;
5832 	}
5833 
5834 	return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
5835 				  p2ps_prov);
5836 }
5837 
5838 
p2p_ctrl_asp_provision(struct wpa_supplicant * wpa_s,char * cmd)5839 static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
5840 {
5841 	u8 addr[ETH_ALEN];
5842 	struct p2ps_provision *p2ps_prov;
5843 	char *pos;
5844 
5845 	/* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
5846 	 *        session=<ses_id> mac=<ses_mac> [info=<infodata>]
5847 	 */
5848 
5849 	wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
5850 	if (hwaddr_aton(cmd, addr))
5851 		return -1;
5852 
5853 	pos = cmd + 17;
5854 	if (*pos != ' ')
5855 		return -1;
5856 
5857 	p2ps_prov = p2p_parse_asp_provision_cmd(pos);
5858 	if (!p2ps_prov)
5859 		return -1;
5860 
5861 	p2ps_prov->pd_seeker = 1;
5862 
5863 	return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
5864 				  p2ps_prov);
5865 }
5866 
5867 
parse_freq(int chwidth,int freq2)5868 static int parse_freq(int chwidth, int freq2)
5869 {
5870 	if (freq2 < 0)
5871 		return -1;
5872 	if (freq2)
5873 		return CHANWIDTH_80P80MHZ;
5874 
5875 	switch (chwidth) {
5876 	case 0:
5877 	case 20:
5878 	case 40:
5879 		return CHANWIDTH_USE_HT;
5880 	case 80:
5881 		return CHANWIDTH_80MHZ;
5882 	case 160:
5883 		return CHANWIDTH_160MHZ;
5884 	default:
5885 		wpa_printf(MSG_DEBUG, "Unknown max oper bandwidth: %d",
5886 			   chwidth);
5887 		return -1;
5888 	}
5889 }
5890 
5891 
p2p_ctrl_connect(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)5892 static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
5893 			    char *buf, size_t buflen)
5894 {
5895 	u8 addr[ETH_ALEN];
5896 	char *pos, *pos2;
5897 	char *pin = NULL;
5898 	enum p2p_wps_method wps_method;
5899 	int new_pin;
5900 	int ret;
5901 	int persistent_group, persistent_id = -1;
5902 	int join;
5903 	int auth;
5904 	int automatic;
5905 	int go_intent = -1;
5906 	int freq = 0;
5907 	int pd;
5908 	int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
5909 	int edmg;
5910 	u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
5911 	size_t group_ssid_len = 0;
5912 	int he;
5913 
5914 	if (!wpa_s->global->p2p_init_wpa_s)
5915 		return -1;
5916 	if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
5917 		wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
5918 			wpa_s->global->p2p_init_wpa_s->ifname);
5919 		wpa_s = wpa_s->global->p2p_init_wpa_s;
5920 	}
5921 
5922 	/* <addr> <"pbc" | "pin" | PIN> [label|display|keypad|p2ps]
5923 	 * [persistent|persistent=<network id>]
5924 	 * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
5925 	 * [ht40] [vht] [he] [edmg] [auto] [ssid=<hexdump>] */
5926 
5927 	if (hwaddr_aton(cmd, addr))
5928 		return -1;
5929 
5930 	pos = cmd + 17;
5931 	if (*pos != ' ')
5932 		return -1;
5933 	pos++;
5934 
5935 	persistent_group = os_strstr(pos, " persistent") != NULL;
5936 	pos2 = os_strstr(pos, " persistent=");
5937 	if (pos2) {
5938 		struct wpa_ssid *ssid;
5939 		persistent_id = atoi(pos2 + 12);
5940 		ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5941 		if (ssid == NULL || ssid->disabled != 2 ||
5942 		    ssid->mode != WPAS_MODE_P2P_GO) {
5943 			wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
5944 				   "SSID id=%d for persistent P2P group (GO)",
5945 				   persistent_id);
5946 			return -1;
5947 		}
5948 	}
5949 	join = os_strstr(pos, " join") != NULL;
5950 	auth = os_strstr(pos, " auth") != NULL;
5951 	automatic = os_strstr(pos, " auto") != NULL;
5952 	pd = os_strstr(pos, " provdisc") != NULL;
5953 	vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
5954 	ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
5955 		vht;
5956 	he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
5957 	edmg = (os_strstr(cmd, " edmg") != NULL) || wpa_s->conf->p2p_go_edmg;
5958 
5959 	pos2 = os_strstr(pos, " go_intent=");
5960 	if (pos2) {
5961 		pos2 += 11;
5962 		go_intent = atoi(pos2);
5963 		if (go_intent < 0 || go_intent > 15)
5964 			return -1;
5965 	}
5966 
5967 	pos2 = os_strstr(pos, " freq=");
5968 	if (pos2) {
5969 		pos2 += 6;
5970 		freq = atoi(pos2);
5971 		if (freq <= 0)
5972 			return -1;
5973 	}
5974 
5975 	pos2 = os_strstr(pos, " freq2=");
5976 	if (pos2)
5977 		freq2 = atoi(pos2 + 7);
5978 
5979 	pos2 = os_strstr(pos, " max_oper_chwidth=");
5980 	if (pos2)
5981 		chwidth = atoi(pos2 + 18);
5982 
5983 	max_oper_chwidth = parse_freq(chwidth, freq2);
5984 	if (max_oper_chwidth < 0)
5985 		return -1;
5986 
5987 	pos2 = os_strstr(pos, " ssid=");
5988 	if (pos2) {
5989 		char *end;
5990 
5991 		pos2 += 6;
5992 		end = os_strchr(pos2, ' ');
5993 		if (!end)
5994 			group_ssid_len = os_strlen(pos2) / 2;
5995 		else
5996 			group_ssid_len = (end - pos2) / 2;
5997 		if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
5998 		    hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
5999 			return -1;
6000 		group_ssid = _group_ssid;
6001 	}
6002 
6003 	if (os_strncmp(pos, "pin", 3) == 0) {
6004 		/* Request random PIN (to be displayed) and enable the PIN */
6005 		wps_method = WPS_PIN_DISPLAY;
6006 	} else if (os_strncmp(pos, "pbc", 3) == 0) {
6007 		wps_method = WPS_PBC;
6008 	} else if (os_strstr(pos, "p2ps") != NULL) {
6009 		wps_method = WPS_P2PS;
6010 	} else {
6011 		pin = pos;
6012 		pos = os_strchr(pin, ' ');
6013 		wps_method = WPS_PIN_KEYPAD;
6014 		if (pos) {
6015 			*pos++ = '\0';
6016 			if (os_strncmp(pos, "display", 7) == 0)
6017 				wps_method = WPS_PIN_DISPLAY;
6018 		}
6019 		if (!wps_pin_str_valid(pin)) {
6020 			os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
6021 			return 17;
6022 		}
6023 	}
6024 
6025 	new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
6026 				   persistent_group, automatic, join,
6027 				   auth, go_intent, freq, freq2, persistent_id,
6028 				   pd, ht40, vht, max_oper_chwidth, he, edmg,
6029 				   group_ssid, group_ssid_len);
6030 	if (new_pin == -2) {
6031 		os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
6032 		return 25;
6033 	}
6034 	if (new_pin == -3) {
6035 		os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
6036 		return 25;
6037 	}
6038 	if (new_pin < 0)
6039 		return -1;
6040 	if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
6041 		ret = os_snprintf(buf, buflen, "%08d", new_pin);
6042 		if (os_snprintf_error(buflen, ret))
6043 			return -1;
6044 		return ret;
6045 	}
6046 
6047 	os_memcpy(buf, "OK\n", 3);
6048 	return 3;
6049 }
6050 
6051 
p2p_ctrl_listen(struct wpa_supplicant * wpa_s,char * cmd)6052 static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
6053 {
6054 	unsigned int timeout = atoi(cmd);
6055 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
6056 		wpa_dbg(wpa_s, MSG_INFO,
6057 			"Reject P2P_LISTEN since interface is disabled");
6058 		return -1;
6059 	}
6060 	return wpas_p2p_listen(wpa_s, timeout);
6061 }
6062 
6063 
p2p_ctrl_prov_disc(struct wpa_supplicant * wpa_s,char * cmd)6064 static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
6065 {
6066 	u8 addr[ETH_ALEN];
6067 	char *pos;
6068 	enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
6069 
6070 	/* <addr> <config method> [join|auto] */
6071 
6072 	if (hwaddr_aton(cmd, addr))
6073 		return -1;
6074 
6075 	pos = cmd + 17;
6076 	if (*pos != ' ')
6077 		return -1;
6078 	pos++;
6079 
6080 	if (os_strstr(pos, " join") != NULL)
6081 		use = WPAS_P2P_PD_FOR_JOIN;
6082 	else if (os_strstr(pos, " auto") != NULL)
6083 		use = WPAS_P2P_PD_AUTO;
6084 
6085 	return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
6086 }
6087 
6088 
p2p_get_passphrase(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)6089 static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
6090 			      size_t buflen)
6091 {
6092 	struct wpa_ssid *ssid = wpa_s->current_ssid;
6093 
6094 	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
6095 	    ssid->passphrase == NULL)
6096 		return -1;
6097 
6098 	os_strlcpy(buf, ssid->passphrase, buflen);
6099 	return os_strlen(buf);
6100 }
6101 
6102 
p2p_ctrl_serv_disc_req(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)6103 static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
6104 				  char *buf, size_t buflen)
6105 {
6106 	u64 ref;
6107 	int res;
6108 	u8 dst_buf[ETH_ALEN], *dst;
6109 	struct wpabuf *tlvs;
6110 	char *pos;
6111 	size_t len;
6112 
6113 	if (hwaddr_aton(cmd, dst_buf))
6114 		return -1;
6115 	dst = dst_buf;
6116 	if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
6117 	    dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
6118 		dst = NULL;
6119 	pos = cmd + 17;
6120 	if (*pos != ' ')
6121 		return -1;
6122 	pos++;
6123 
6124 	if (os_strncmp(pos, "upnp ", 5) == 0) {
6125 		u8 version;
6126 		pos += 5;
6127 		if (hexstr2bin(pos, &version, 1) < 0)
6128 			return -1;
6129 		pos += 2;
6130 		if (*pos != ' ')
6131 			return -1;
6132 		pos++;
6133 		ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
6134 #ifdef CONFIG_WIFI_DISPLAY
6135 	} else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
6136 		ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
6137 #endif /* CONFIG_WIFI_DISPLAY */
6138 	} else if (os_strncmp(pos, "asp ", 4) == 0) {
6139 		char *svc_str;
6140 		char *svc_info = NULL;
6141 		u32 id;
6142 
6143 		pos += 4;
6144 		if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
6145 			return -1;
6146 
6147 		pos = os_strchr(pos, ' ');
6148 		if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
6149 			return -1;
6150 
6151 		svc_str = pos + 1;
6152 
6153 		pos = os_strchr(svc_str, ' ');
6154 
6155 		if (pos)
6156 			*pos++ = '\0';
6157 
6158 		/* All remaining data is the svc_info string */
6159 		if (pos && pos[0] && pos[0] != ' ') {
6160 			len = os_strlen(pos);
6161 
6162 			/* Unescape in place */
6163 			len = utf8_unescape(pos, len, pos, len);
6164 			if (len > 0xff)
6165 				return -1;
6166 
6167 			svc_info = pos;
6168 		}
6169 
6170 		ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
6171 					      svc_str, svc_info);
6172 	} else {
6173 		len = os_strlen(pos);
6174 		if (len & 1)
6175 			return -1;
6176 		len /= 2;
6177 		tlvs = wpabuf_alloc(len);
6178 		if (tlvs == NULL)
6179 			return -1;
6180 		if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
6181 			wpabuf_free(tlvs);
6182 			return -1;
6183 		}
6184 
6185 		ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
6186 		wpabuf_free(tlvs);
6187 	}
6188 	if (ref == 0)
6189 		return -1;
6190 	res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
6191 	if (os_snprintf_error(buflen, res))
6192 		return -1;
6193 	return res;
6194 }
6195 
6196 
p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant * wpa_s,char * cmd)6197 static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
6198 					 char *cmd)
6199 {
6200 	long long unsigned val;
6201 	u64 req;
6202 	if (sscanf(cmd, "%llx", &val) != 1)
6203 		return -1;
6204 	req = val;
6205 	return wpas_p2p_sd_cancel_request(wpa_s, req);
6206 }
6207 
6208 
p2p_ctrl_serv_disc_resp(struct wpa_supplicant * wpa_s,char * cmd)6209 static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
6210 {
6211 	int freq;
6212 	u8 dst[ETH_ALEN];
6213 	u8 dialog_token;
6214 	struct wpabuf *resp_tlvs;
6215 	char *pos, *pos2;
6216 	size_t len;
6217 
6218 	pos = os_strchr(cmd, ' ');
6219 	if (pos == NULL)
6220 		return -1;
6221 	*pos++ = '\0';
6222 	freq = atoi(cmd);
6223 	if (freq == 0)
6224 		return -1;
6225 
6226 	if (hwaddr_aton(pos, dst))
6227 		return -1;
6228 	pos += 17;
6229 	if (*pos != ' ')
6230 		return -1;
6231 	pos++;
6232 
6233 	pos2 = os_strchr(pos, ' ');
6234 	if (pos2 == NULL)
6235 		return -1;
6236 	*pos2++ = '\0';
6237 	dialog_token = atoi(pos);
6238 
6239 	len = os_strlen(pos2);
6240 	if (len & 1)
6241 		return -1;
6242 	len /= 2;
6243 	resp_tlvs = wpabuf_alloc(len);
6244 	if (resp_tlvs == NULL)
6245 		return -1;
6246 	if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
6247 		wpabuf_free(resp_tlvs);
6248 		return -1;
6249 	}
6250 
6251 	wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
6252 	wpabuf_free(resp_tlvs);
6253 	return 0;
6254 }
6255 
6256 
p2p_ctrl_serv_disc_external(struct wpa_supplicant * wpa_s,char * cmd)6257 static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
6258 				       char *cmd)
6259 {
6260 	if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
6261 		return -1;
6262 	wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
6263 	return 0;
6264 }
6265 
6266 
p2p_ctrl_service_add_bonjour(struct wpa_supplicant * wpa_s,char * cmd)6267 static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
6268 					char *cmd)
6269 {
6270 	char *pos;
6271 	size_t len;
6272 	struct wpabuf *query, *resp;
6273 
6274 	pos = os_strchr(cmd, ' ');
6275 	if (pos == NULL)
6276 		return -1;
6277 	*pos++ = '\0';
6278 
6279 	len = os_strlen(cmd);
6280 	if (len & 1)
6281 		return -1;
6282 	len /= 2;
6283 	query = wpabuf_alloc(len);
6284 	if (query == NULL)
6285 		return -1;
6286 	if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
6287 		wpabuf_free(query);
6288 		return -1;
6289 	}
6290 
6291 	len = os_strlen(pos);
6292 	if (len & 1) {
6293 		wpabuf_free(query);
6294 		return -1;
6295 	}
6296 	len /= 2;
6297 	resp = wpabuf_alloc(len);
6298 	if (resp == NULL) {
6299 		wpabuf_free(query);
6300 		return -1;
6301 	}
6302 	if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
6303 		wpabuf_free(query);
6304 		wpabuf_free(resp);
6305 		return -1;
6306 	}
6307 
6308 	if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
6309 		wpabuf_free(query);
6310 		wpabuf_free(resp);
6311 		return -1;
6312 	}
6313 	return 0;
6314 }
6315 
6316 
p2p_ctrl_service_add_upnp(struct wpa_supplicant * wpa_s,char * cmd)6317 static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
6318 {
6319 	char *pos;
6320 	u8 version;
6321 
6322 	pos = os_strchr(cmd, ' ');
6323 	if (pos == NULL)
6324 		return -1;
6325 	*pos++ = '\0';
6326 
6327 	if (hexstr2bin(cmd, &version, 1) < 0)
6328 		return -1;
6329 
6330 	return wpas_p2p_service_add_upnp(wpa_s, version, pos);
6331 }
6332 
6333 
p2p_ctrl_service_add_asp(struct wpa_supplicant * wpa_s,u8 replace,char * cmd)6334 static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
6335 				    u8 replace, char *cmd)
6336 {
6337 	char *pos;
6338 	char *adv_str;
6339 	u32 auto_accept, adv_id, svc_state, config_methods;
6340 	char *svc_info = NULL;
6341 	char *cpt_prio_str;
6342 	u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
6343 
6344 	pos = os_strchr(cmd, ' ');
6345 	if (pos == NULL)
6346 		return -1;
6347 	*pos++ = '\0';
6348 
6349 	/* Auto-Accept value is mandatory, and must be one of the
6350 	 * single values (0, 1, 2, 4) */
6351 	auto_accept = atoi(cmd);
6352 	switch (auto_accept) {
6353 	case P2PS_SETUP_NONE: /* No auto-accept */
6354 	case P2PS_SETUP_NEW:
6355 	case P2PS_SETUP_CLIENT:
6356 	case P2PS_SETUP_GROUP_OWNER:
6357 		break;
6358 	default:
6359 		return -1;
6360 	}
6361 
6362 	/* Advertisement ID is mandatory */
6363 	cmd = pos;
6364 	pos = os_strchr(cmd, ' ');
6365 	if (pos == NULL)
6366 		return -1;
6367 	*pos++ = '\0';
6368 
6369 	/* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
6370 	if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
6371 		return -1;
6372 
6373 	/* Only allow replacements if exist, and adds if not */
6374 	if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
6375 		if (!replace)
6376 			return -1;
6377 	} else {
6378 		if (replace)
6379 			return -1;
6380 	}
6381 
6382 	/* svc_state between 0 - 0xff is mandatory */
6383 	if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
6384 		return -1;
6385 
6386 	pos = os_strchr(pos, ' ');
6387 	if (pos == NULL)
6388 		return -1;
6389 
6390 	/* config_methods is mandatory */
6391 	pos++;
6392 	if (sscanf(pos, "%x", &config_methods) != 1)
6393 		return -1;
6394 
6395 	if (!(config_methods &
6396 	      (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
6397 		return -1;
6398 
6399 	pos = os_strchr(pos, ' ');
6400 	if (pos == NULL)
6401 		return -1;
6402 
6403 	pos++;
6404 	adv_str = pos;
6405 
6406 	/* Advertisement string is mandatory */
6407 	if (!pos[0] || pos[0] == ' ')
6408 		return -1;
6409 
6410 	/* Terminate svc string */
6411 	pos = os_strchr(pos, ' ');
6412 	if (pos != NULL)
6413 		*pos++ = '\0';
6414 
6415 	cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
6416 	if (cpt_prio_str) {
6417 		pos = os_strchr(pos, ' ');
6418 		if (pos != NULL)
6419 			*pos++ = '\0';
6420 
6421 		if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
6422 			return -1;
6423 	} else {
6424 		cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
6425 		cpt_prio[1] = 0;
6426 	}
6427 
6428 	/* Service and Response Information are optional */
6429 	if (pos && pos[0]) {
6430 		size_t len;
6431 
6432 		/* Note the bare ' included, which cannot exist legally
6433 		 * in unescaped string. */
6434 		svc_info = os_strstr(pos, "svc_info='");
6435 
6436 		if (svc_info) {
6437 			svc_info += 9;
6438 			len = os_strlen(svc_info);
6439 			utf8_unescape(svc_info, len, svc_info, len);
6440 		}
6441 	}
6442 
6443 	return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
6444 					(u8) svc_state, (u16) config_methods,
6445 					svc_info, cpt_prio);
6446 }
6447 
6448 
p2p_ctrl_service_add(struct wpa_supplicant * wpa_s,char * cmd)6449 static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
6450 {
6451 	char *pos;
6452 
6453 	pos = os_strchr(cmd, ' ');
6454 	if (pos == NULL)
6455 		return -1;
6456 	*pos++ = '\0';
6457 
6458 	if (os_strcmp(cmd, "bonjour") == 0)
6459 		return p2p_ctrl_service_add_bonjour(wpa_s, pos);
6460 	if (os_strcmp(cmd, "upnp") == 0)
6461 		return p2p_ctrl_service_add_upnp(wpa_s, pos);
6462 	if (os_strcmp(cmd, "asp") == 0)
6463 		return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
6464 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6465 	return -1;
6466 }
6467 
6468 
p2p_ctrl_service_del_bonjour(struct wpa_supplicant * wpa_s,char * cmd)6469 static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
6470 					char *cmd)
6471 {
6472 	size_t len;
6473 	struct wpabuf *query;
6474 	int ret;
6475 
6476 	len = os_strlen(cmd);
6477 	if (len & 1)
6478 		return -1;
6479 	len /= 2;
6480 	query = wpabuf_alloc(len);
6481 	if (query == NULL)
6482 		return -1;
6483 	if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
6484 		wpabuf_free(query);
6485 		return -1;
6486 	}
6487 
6488 	ret = wpas_p2p_service_del_bonjour(wpa_s, query);
6489 	wpabuf_free(query);
6490 	return ret;
6491 }
6492 
6493 
p2p_ctrl_service_del_upnp(struct wpa_supplicant * wpa_s,char * cmd)6494 static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
6495 {
6496 	char *pos;
6497 	u8 version;
6498 
6499 	pos = os_strchr(cmd, ' ');
6500 	if (pos == NULL)
6501 		return -1;
6502 	*pos++ = '\0';
6503 
6504 	if (hexstr2bin(cmd, &version, 1) < 0)
6505 		return -1;
6506 
6507 	return wpas_p2p_service_del_upnp(wpa_s, version, pos);
6508 }
6509 
6510 
p2p_ctrl_service_del_asp(struct wpa_supplicant * wpa_s,char * cmd)6511 static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
6512 {
6513 	u32 adv_id;
6514 
6515 	if (os_strcmp(cmd, "all") == 0) {
6516 		wpas_p2p_service_flush_asp(wpa_s);
6517 		return 0;
6518 	}
6519 
6520 	if (sscanf(cmd, "%x", &adv_id) != 1)
6521 		return -1;
6522 
6523 	return wpas_p2p_service_del_asp(wpa_s, adv_id);
6524 }
6525 
6526 
p2p_ctrl_service_del(struct wpa_supplicant * wpa_s,char * cmd)6527 static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
6528 {
6529 	char *pos;
6530 
6531 	pos = os_strchr(cmd, ' ');
6532 	if (pos == NULL)
6533 		return -1;
6534 	*pos++ = '\0';
6535 
6536 	if (os_strcmp(cmd, "bonjour") == 0)
6537 		return p2p_ctrl_service_del_bonjour(wpa_s, pos);
6538 	if (os_strcmp(cmd, "upnp") == 0)
6539 		return p2p_ctrl_service_del_upnp(wpa_s, pos);
6540 	if (os_strcmp(cmd, "asp") == 0)
6541 		return p2p_ctrl_service_del_asp(wpa_s, pos);
6542 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6543 	return -1;
6544 }
6545 
6546 
p2p_ctrl_service_replace(struct wpa_supplicant * wpa_s,char * cmd)6547 static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
6548 {
6549 	char *pos;
6550 
6551 	pos = os_strchr(cmd, ' ');
6552 	if (pos == NULL)
6553 		return -1;
6554 	*pos++ = '\0';
6555 
6556 	if (os_strcmp(cmd, "asp") == 0)
6557 		return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
6558 
6559 	wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
6560 	return -1;
6561 }
6562 
6563 
p2p_ctrl_reject(struct wpa_supplicant * wpa_s,char * cmd)6564 static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
6565 {
6566 	u8 addr[ETH_ALEN];
6567 
6568 	/* <addr> */
6569 
6570 	if (hwaddr_aton(cmd, addr))
6571 		return -1;
6572 
6573 	return wpas_p2p_reject(wpa_s, addr);
6574 }
6575 
6576 
p2p_ctrl_invite_persistent(struct wpa_supplicant * wpa_s,char * cmd)6577 static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
6578 {
6579 	char *pos;
6580 	int id;
6581 	struct wpa_ssid *ssid;
6582 	u8 *_peer = NULL, peer[ETH_ALEN];
6583 	int freq = 0, pref_freq = 0;
6584 	int ht40, vht, he, max_oper_chwidth, chwidth = 0, freq2 = 0;
6585 	int edmg;
6586 
6587 	id = atoi(cmd);
6588 	pos = os_strstr(cmd, " peer=");
6589 	if (pos) {
6590 		pos += 6;
6591 		if (hwaddr_aton(pos, peer))
6592 			return -1;
6593 		_peer = peer;
6594 	}
6595 	ssid = wpa_config_get_network(wpa_s->conf, id);
6596 	if (ssid == NULL || ssid->disabled != 2) {
6597 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
6598 			   "for persistent P2P group",
6599 			   id);
6600 		return -1;
6601 	}
6602 
6603 	pos = os_strstr(cmd, " freq=");
6604 	if (pos) {
6605 		pos += 6;
6606 		freq = atoi(pos);
6607 		if (freq <= 0)
6608 			return -1;
6609 	}
6610 
6611 	pos = os_strstr(cmd, " pref=");
6612 	if (pos) {
6613 		pos += 6;
6614 		pref_freq = atoi(pos);
6615 		if (pref_freq <= 0)
6616 			return -1;
6617 	}
6618 
6619 	vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
6620 	ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
6621 		vht;
6622 	he = (os_strstr(cmd, " he") != NULL) || wpa_s->conf->p2p_go_he;
6623 	edmg = (os_strstr(cmd, " edmg") != NULL) || wpa_s->conf->p2p_go_edmg;
6624 
6625 	pos = os_strstr(cmd, "freq2=");
6626 	if (pos)
6627 		freq2 = atoi(pos + 6);
6628 
6629 	pos = os_strstr(cmd, " max_oper_chwidth=");
6630 	if (pos)
6631 		chwidth = atoi(pos + 18);
6632 
6633 	max_oper_chwidth = parse_freq(chwidth, freq2);
6634 	if (max_oper_chwidth < 0)
6635 		return -1;
6636 
6637 	return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
6638 			       max_oper_chwidth, pref_freq, he, edmg);
6639 }
6640 
6641 
p2p_ctrl_invite_group(struct wpa_supplicant * wpa_s,char * cmd)6642 static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
6643 {
6644 	char *pos;
6645 	u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
6646 
6647 	pos = os_strstr(cmd, " peer=");
6648 	if (!pos)
6649 		return -1;
6650 
6651 	*pos = '\0';
6652 	pos += 6;
6653 	if (hwaddr_aton(pos, peer)) {
6654 		wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
6655 		return -1;
6656 	}
6657 
6658 	pos = os_strstr(pos, " go_dev_addr=");
6659 	if (pos) {
6660 		pos += 13;
6661 		if (hwaddr_aton(pos, go_dev_addr)) {
6662 			wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
6663 				   pos);
6664 			return -1;
6665 		}
6666 		go_dev = go_dev_addr;
6667 	}
6668 
6669 	return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
6670 }
6671 
6672 
p2p_ctrl_invite(struct wpa_supplicant * wpa_s,char * cmd)6673 static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
6674 {
6675 	if (os_strncmp(cmd, "persistent=", 11) == 0)
6676 		return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
6677 	if (os_strncmp(cmd, "group=", 6) == 0)
6678 		return p2p_ctrl_invite_group(wpa_s, cmd + 6);
6679 
6680 	return -1;
6681 }
6682 
6683 
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)6684 static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
6685 					 int id, int freq, int vht_center_freq2,
6686 					 int ht40, int vht, int vht_chwidth,
6687 					 int he, int edmg)
6688 {
6689 	struct wpa_ssid *ssid;
6690 
6691 	ssid = wpa_config_get_network(wpa_s->conf, id);
6692 	if (ssid == NULL || ssid->disabled != 2) {
6693 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
6694 			   "for persistent P2P group",
6695 			   id);
6696 		return -1;
6697 	}
6698 
6699 	return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq,
6700 					     vht_center_freq2, 0, ht40, vht,
6701 					     vht_chwidth, he, edmg,
6702 					     NULL, 0, 0);
6703 }
6704 
6705 
p2p_ctrl_group_add(struct wpa_supplicant * wpa_s,char * cmd)6706 static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
6707 {
6708 	int freq = 0, persistent = 0, group_id = -1;
6709 	int vht = wpa_s->conf->p2p_go_vht;
6710 	int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
6711 	int he = wpa_s->conf->p2p_go_he;
6712 	int edmg = wpa_s->conf->p2p_go_edmg;
6713 	int max_oper_chwidth, chwidth = 0, freq2 = 0;
6714 	char *token, *context = NULL;
6715 #ifdef CONFIG_ACS
6716 	int acs = 0;
6717 #endif /* CONFIG_ACS */
6718 
6719 	while ((token = str_token(cmd, " ", &context))) {
6720 		if (sscanf(token, "freq2=%d", &freq2) == 1 ||
6721 		    sscanf(token, "persistent=%d", &group_id) == 1 ||
6722 		    sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1) {
6723 			continue;
6724 #ifdef CONFIG_ACS
6725 		} else if (os_strcmp(token, "freq=acs") == 0) {
6726 			acs = 1;
6727 #endif /* CONFIG_ACS */
6728 		} else if (sscanf(token, "freq=%d", &freq) == 1) {
6729 			continue;
6730 		} else if (os_strcmp(token, "ht40") == 0) {
6731 			ht40 = 1;
6732 		} else if (os_strcmp(token, "vht") == 0) {
6733 			vht = 1;
6734 			ht40 = 1;
6735 		} else if (os_strcmp(token, "he") == 0) {
6736 			he = 1;
6737 		} else if (os_strcmp(token, "edmg") == 0) {
6738 			edmg = 1;
6739 		} else if (os_strcmp(token, "persistent") == 0) {
6740 			persistent = 1;
6741 		} else {
6742 			wpa_printf(MSG_DEBUG,
6743 				   "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
6744 				   token);
6745 			return -1;
6746 		}
6747 	}
6748 
6749 #ifdef CONFIG_ACS
6750 	if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_ACS_OFFLOAD) &&
6751 	    (acs || freq == 2 || freq == 5)) {
6752 		if (freq == 2 && wpa_s->best_24_freq <= 0) {
6753 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211G;
6754 			wpa_s->p2p_go_do_acs = 1;
6755 			freq = 0;
6756 		} else if (freq == 5 && wpa_s->best_5_freq <= 0) {
6757 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211A;
6758 			wpa_s->p2p_go_do_acs = 1;
6759 			freq = 0;
6760 		} else {
6761 			wpa_s->p2p_go_acs_band = HOSTAPD_MODE_IEEE80211ANY;
6762 			wpa_s->p2p_go_do_acs = 1;
6763 		}
6764 	} else {
6765 		wpa_s->p2p_go_do_acs = 0;
6766 	}
6767 #endif /* CONFIG_ACS */
6768 
6769 	max_oper_chwidth = parse_freq(chwidth, freq2);
6770 	if (max_oper_chwidth < 0)
6771 		return -1;
6772 
6773 	if (group_id >= 0)
6774 		return p2p_ctrl_group_add_persistent(wpa_s, group_id,
6775 						     freq, freq2, ht40, vht,
6776 						     max_oper_chwidth, he,
6777 						     edmg);
6778 
6779 	return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
6780 				  max_oper_chwidth, he, edmg);
6781 }
6782 
6783 
p2p_ctrl_group_member(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)6784 static int p2p_ctrl_group_member(struct wpa_supplicant *wpa_s, const char *cmd,
6785 				 char *buf, size_t buflen)
6786 {
6787 	u8 dev_addr[ETH_ALEN];
6788 	struct wpa_ssid *ssid;
6789 	int res;
6790 	const u8 *iaddr;
6791 
6792 	ssid = wpa_s->current_ssid;
6793 	if (!wpa_s->global->p2p || !ssid || ssid->mode != WPAS_MODE_P2P_GO ||
6794 	    hwaddr_aton(cmd, dev_addr))
6795 		return -1;
6796 
6797 	iaddr = p2p_group_get_client_interface_addr(wpa_s->p2p_group, dev_addr);
6798 	if (!iaddr)
6799 		return -1;
6800 	res = os_snprintf(buf, buflen, MACSTR, MAC2STR(iaddr));
6801 	if (os_snprintf_error(buflen, res))
6802 		return -1;
6803 	return res;
6804 }
6805 
6806 
wpas_find_p2p_dev_addr_bss(struct wpa_global * global,const u8 * p2p_dev_addr)6807 static int wpas_find_p2p_dev_addr_bss(struct wpa_global *global,
6808 				      const u8 *p2p_dev_addr)
6809 {
6810 	struct wpa_supplicant *wpa_s;
6811 
6812 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6813 		if (wpa_bss_get_p2p_dev_addr(wpa_s, p2p_dev_addr))
6814 			return 1;
6815 	}
6816 
6817 	return 0;
6818 }
6819 
6820 
p2p_ctrl_peer(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)6821 static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
6822 			 char *buf, size_t buflen)
6823 {
6824 	u8 addr[ETH_ALEN], *addr_ptr, group_capab;
6825 	int next, res;
6826 	const struct p2p_peer_info *info;
6827 	char *pos, *end;
6828 	char devtype[WPS_DEV_TYPE_BUFSIZE];
6829 	struct wpa_ssid *ssid;
6830 	size_t i;
6831 
6832 	if (!wpa_s->global->p2p)
6833 		return -1;
6834 
6835 	if (os_strcmp(cmd, "FIRST") == 0) {
6836 		addr_ptr = NULL;
6837 		next = 0;
6838 	} else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
6839 		if (hwaddr_aton(cmd + 5, addr) < 0)
6840 			return -1;
6841 		addr_ptr = addr;
6842 		next = 1;
6843 	} else {
6844 		if (hwaddr_aton(cmd, addr) < 0)
6845 			return -1;
6846 		addr_ptr = addr;
6847 		next = 0;
6848 	}
6849 
6850 	info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
6851 	if (info == NULL)
6852 		return -1;
6853 	group_capab = info->group_capab;
6854 
6855 	if (group_capab &&
6856 	    !wpas_find_p2p_dev_addr_bss(wpa_s->global, info->p2p_device_addr)) {
6857 		wpa_printf(MSG_DEBUG,
6858 			   "P2P: Could not find any BSS with p2p_dev_addr "
6859 			   MACSTR ", hence override group_capab from 0x%x to 0",
6860 			   MAC2STR(info->p2p_device_addr), group_capab);
6861 		group_capab = 0;
6862 	}
6863 
6864 	pos = buf;
6865 	end = buf + buflen;
6866 
6867 	res = os_snprintf(pos, end - pos, MACSTR "\n"
6868 			  "pri_dev_type=%s\n"
6869 			  "device_name=%s\n"
6870 			  "manufacturer=%s\n"
6871 			  "model_name=%s\n"
6872 			  "model_number=%s\n"
6873 			  "serial_number=%s\n"
6874 			  "config_methods=0x%x\n"
6875 			  "dev_capab=0x%x\n"
6876 			  "group_capab=0x%x\n"
6877 			  "level=%d\n",
6878 			  MAC2STR(info->p2p_device_addr),
6879 			  wps_dev_type_bin2str(info->pri_dev_type,
6880 					       devtype, sizeof(devtype)),
6881 			  info->device_name,
6882 			  info->manufacturer,
6883 			  info->model_name,
6884 			  info->model_number,
6885 			  info->serial_number,
6886 			  info->config_methods,
6887 			  info->dev_capab,
6888 			  group_capab,
6889 			  info->level);
6890 	if (os_snprintf_error(end - pos, res))
6891 		return pos - buf;
6892 	pos += res;
6893 
6894 	for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
6895 	{
6896 		const u8 *t;
6897 		t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
6898 		res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
6899 				  wps_dev_type_bin2str(t, devtype,
6900 						       sizeof(devtype)));
6901 		if (os_snprintf_error(end - pos, res))
6902 			return pos - buf;
6903 		pos += res;
6904 	}
6905 
6906 	ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
6907 	if (ssid) {
6908 		res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
6909 		if (os_snprintf_error(end - pos, res))
6910 			return pos - buf;
6911 		pos += res;
6912 	}
6913 
6914 	res = p2p_get_peer_info_txt(info, pos, end - pos);
6915 	if (res < 0)
6916 		return pos - buf;
6917 	pos += res;
6918 
6919 	if (info->vendor_elems) {
6920 		res = os_snprintf(pos, end - pos, "vendor_elems=");
6921 		if (os_snprintf_error(end - pos, res))
6922 			return pos - buf;
6923 		pos += res;
6924 
6925 		pos += wpa_snprintf_hex(pos, end - pos,
6926 					wpabuf_head(info->vendor_elems),
6927 					wpabuf_len(info->vendor_elems));
6928 
6929 		res = os_snprintf(pos, end - pos, "\n");
6930 		if (os_snprintf_error(end - pos, res))
6931 			return pos - buf;
6932 		pos += res;
6933 	}
6934 
6935 	return pos - buf;
6936 }
6937 
6938 
p2p_ctrl_disallow_freq(struct wpa_supplicant * wpa_s,const char * param)6939 static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
6940 				  const char *param)
6941 {
6942 	unsigned int i;
6943 
6944 	if (wpa_s->global->p2p == NULL)
6945 		return -1;
6946 
6947 	if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
6948 		return -1;
6949 
6950 	for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
6951 		struct wpa_freq_range *freq;
6952 		freq = &wpa_s->global->p2p_disallow_freq.range[i];
6953 		wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
6954 			   freq->min, freq->max);
6955 	}
6956 
6957 	wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
6958 	return 0;
6959 }
6960 
6961 
p2p_ctrl_set(struct wpa_supplicant * wpa_s,char * cmd)6962 static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
6963 {
6964 	char *param;
6965 
6966 	if (wpa_s->global->p2p == NULL)
6967 		return -1;
6968 
6969 	param = os_strchr(cmd, ' ');
6970 	if (param == NULL)
6971 		return -1;
6972 	*param++ = '\0';
6973 
6974 	if (os_strcmp(cmd, "discoverability") == 0) {
6975 		p2p_set_client_discoverability(wpa_s->global->p2p,
6976 					       atoi(param));
6977 		return 0;
6978 	}
6979 
6980 	if (os_strcmp(cmd, "managed") == 0) {
6981 		p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
6982 		return 0;
6983 	}
6984 
6985 	if (os_strcmp(cmd, "listen_channel") == 0) {
6986 		char *pos;
6987 		u8 channel, op_class;
6988 
6989 		channel = atoi(param);
6990 		pos = os_strchr(param, ' ');
6991 		op_class = pos ? atoi(pos) : 81;
6992 
6993 		return p2p_set_listen_channel(wpa_s->global->p2p, op_class,
6994 					      channel, 1);
6995 	}
6996 
6997 	if (os_strcmp(cmd, "ssid_postfix") == 0) {
6998 		return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
6999 					    os_strlen(param));
7000 	}
7001 
7002 	if (os_strcmp(cmd, "noa") == 0) {
7003 		char *pos;
7004 		int count, start, duration;
7005 		/* GO NoA parameters: count,start_offset(ms),duration(ms) */
7006 		count = atoi(param);
7007 		pos = os_strchr(param, ',');
7008 		if (pos == NULL)
7009 			return -1;
7010 		pos++;
7011 		start = atoi(pos);
7012 		pos = os_strchr(pos, ',');
7013 		if (pos == NULL)
7014 			return -1;
7015 		pos++;
7016 		duration = atoi(pos);
7017 		if (count < 0 || count > 255 || start < 0 || duration < 0)
7018 			return -1;
7019 		if (count == 0 && duration > 0)
7020 			return -1;
7021 		wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
7022 			   "start=%d duration=%d", count, start, duration);
7023 		return wpas_p2p_set_noa(wpa_s, count, start, duration);
7024 	}
7025 
7026 	if (os_strcmp(cmd, "ps") == 0)
7027 		return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
7028 
7029 	if (os_strcmp(cmd, "oppps") == 0)
7030 		return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
7031 
7032 	if (os_strcmp(cmd, "ctwindow") == 0)
7033 		return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
7034 
7035 	if (os_strcmp(cmd, "disabled") == 0) {
7036 		wpa_s->global->p2p_disabled = atoi(param);
7037 		wpa_printf(MSG_DEBUG, "P2P functionality %s",
7038 			   wpa_s->global->p2p_disabled ?
7039 			   "disabled" : "enabled");
7040 		if (wpa_s->global->p2p_disabled) {
7041 			wpas_p2p_stop_find(wpa_s);
7042 			os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7043 			p2p_flush(wpa_s->global->p2p);
7044 		}
7045 		return 0;
7046 	}
7047 
7048 	if (os_strcmp(cmd, "conc_pref") == 0) {
7049 		if (os_strcmp(param, "sta") == 0)
7050 			wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
7051 		else if (os_strcmp(param, "p2p") == 0)
7052 			wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
7053 		else {
7054 			wpa_printf(MSG_INFO, "Invalid conc_pref value");
7055 			return -1;
7056 		}
7057 		wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
7058 			   "%s", param);
7059 		return 0;
7060 	}
7061 
7062 	if (os_strcmp(cmd, "force_long_sd") == 0) {
7063 		wpa_s->force_long_sd = atoi(param);
7064 		return 0;
7065 	}
7066 
7067 	if (os_strcmp(cmd, "peer_filter") == 0) {
7068 		u8 addr[ETH_ALEN];
7069 		if (hwaddr_aton(param, addr))
7070 			return -1;
7071 		p2p_set_peer_filter(wpa_s->global->p2p, addr);
7072 		return 0;
7073 	}
7074 
7075 	if (os_strcmp(cmd, "cross_connect") == 0)
7076 		return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
7077 
7078 	if (os_strcmp(cmd, "go_apsd") == 0) {
7079 		if (os_strcmp(param, "disable") == 0)
7080 			wpa_s->set_ap_uapsd = 0;
7081 		else {
7082 			wpa_s->set_ap_uapsd = 1;
7083 			wpa_s->ap_uapsd = atoi(param);
7084 		}
7085 		return 0;
7086 	}
7087 
7088 	if (os_strcmp(cmd, "client_apsd") == 0) {
7089 		if (os_strcmp(param, "disable") == 0)
7090 			wpa_s->set_sta_uapsd = 0;
7091 		else {
7092 			int be, bk, vi, vo;
7093 			char *pos;
7094 			/* format: BE,BK,VI,VO;max SP Length */
7095 			be = atoi(param);
7096 			pos = os_strchr(param, ',');
7097 			if (pos == NULL)
7098 				return -1;
7099 			pos++;
7100 			bk = atoi(pos);
7101 			pos = os_strchr(pos, ',');
7102 			if (pos == NULL)
7103 				return -1;
7104 			pos++;
7105 			vi = atoi(pos);
7106 			pos = os_strchr(pos, ',');
7107 			if (pos == NULL)
7108 				return -1;
7109 			pos++;
7110 			vo = atoi(pos);
7111 			/* ignore max SP Length for now */
7112 
7113 			wpa_s->set_sta_uapsd = 1;
7114 			wpa_s->sta_uapsd = 0;
7115 			if (be)
7116 				wpa_s->sta_uapsd |= BIT(0);
7117 			if (bk)
7118 				wpa_s->sta_uapsd |= BIT(1);
7119 			if (vi)
7120 				wpa_s->sta_uapsd |= BIT(2);
7121 			if (vo)
7122 				wpa_s->sta_uapsd |= BIT(3);
7123 		}
7124 		return 0;
7125 	}
7126 
7127 	if (os_strcmp(cmd, "disallow_freq") == 0)
7128 		return p2p_ctrl_disallow_freq(wpa_s, param);
7129 
7130 	if (os_strcmp(cmd, "disc_int") == 0) {
7131 		int min_disc_int, max_disc_int, max_disc_tu;
7132 		char *pos;
7133 
7134 		pos = param;
7135 
7136 		min_disc_int = atoi(pos);
7137 		pos = os_strchr(pos, ' ');
7138 		if (pos == NULL)
7139 			return -1;
7140 		*pos++ = '\0';
7141 
7142 		max_disc_int = atoi(pos);
7143 		pos = os_strchr(pos, ' ');
7144 		if (pos == NULL)
7145 			return -1;
7146 		*pos++ = '\0';
7147 
7148 		max_disc_tu = atoi(pos);
7149 
7150 		return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
7151 					max_disc_int, max_disc_tu);
7152 	}
7153 
7154 	if (os_strcmp(cmd, "per_sta_psk") == 0) {
7155 		wpa_s->global->p2p_per_sta_psk = !!atoi(param);
7156 		return 0;
7157 	}
7158 
7159 #ifdef CONFIG_WPS_NFC
7160 	if (os_strcmp(cmd, "nfc_tag") == 0)
7161 		return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
7162 #endif /* CONFIG_WPS_NFC */
7163 
7164 	if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
7165 		wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
7166 		return 0;
7167 	}
7168 
7169 	if (os_strcmp(cmd, "override_pref_op_chan") == 0) {
7170 		int op_class, chan;
7171 
7172 		op_class = atoi(param);
7173 		param = os_strchr(param, ':');
7174 		if (!param)
7175 			return -1;
7176 		param++;
7177 		chan = atoi(param);
7178 		p2p_set_override_pref_op_chan(wpa_s->global->p2p, op_class,
7179 					      chan);
7180 		return 0;
7181 	}
7182 
7183 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
7184 		   cmd);
7185 
7186 	return -1;
7187 }
7188 
7189 
p2p_ctrl_flush(struct wpa_supplicant * wpa_s)7190 static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
7191 {
7192 	os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
7193 	wpa_s->force_long_sd = 0;
7194 
7195 #ifdef CONFIG_TESTING_OPTIONS
7196 	os_free(wpa_s->get_pref_freq_list_override);
7197 	wpa_s->get_pref_freq_list_override = NULL;
7198 #endif /* CONFIG_TESTING_OPTIONS */
7199 
7200 	wpas_p2p_stop_find(wpa_s);
7201 	wpa_s->parent->p2ps_method_config_any = 0;
7202 	if (wpa_s->global->p2p)
7203 		p2p_flush(wpa_s->global->p2p);
7204 }
7205 
7206 
p2p_ctrl_presence_req(struct wpa_supplicant * wpa_s,char * cmd)7207 static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
7208 {
7209 	char *pos, *pos2;
7210 	unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
7211 
7212 	if (cmd[0]) {
7213 		pos = os_strchr(cmd, ' ');
7214 		if (pos == NULL)
7215 			return -1;
7216 		*pos++ = '\0';
7217 		dur1 = atoi(cmd);
7218 
7219 		pos2 = os_strchr(pos, ' ');
7220 		if (pos2)
7221 			*pos2++ = '\0';
7222 		int1 = atoi(pos);
7223 	} else
7224 		pos2 = NULL;
7225 
7226 	if (pos2) {
7227 		pos = os_strchr(pos2, ' ');
7228 		if (pos == NULL)
7229 			return -1;
7230 		*pos++ = '\0';
7231 		dur2 = atoi(pos2);
7232 		int2 = atoi(pos);
7233 	}
7234 
7235 	return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
7236 }
7237 
7238 
p2p_ctrl_ext_listen(struct wpa_supplicant * wpa_s,char * cmd)7239 static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
7240 {
7241 	char *pos;
7242 	unsigned int period = 0, interval = 0;
7243 
7244 	if (cmd[0]) {
7245 		pos = os_strchr(cmd, ' ');
7246 		if (pos == NULL)
7247 			return -1;
7248 		*pos++ = '\0';
7249 		period = atoi(cmd);
7250 		interval = atoi(pos);
7251 	}
7252 
7253 	return wpas_p2p_ext_listen(wpa_s, period, interval);
7254 }
7255 
7256 
p2p_ctrl_remove_client(struct wpa_supplicant * wpa_s,const char * cmd)7257 static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
7258 {
7259 	const char *pos;
7260 	u8 peer[ETH_ALEN];
7261 	int iface_addr = 0;
7262 
7263 	pos = cmd;
7264 	if (os_strncmp(pos, "iface=", 6) == 0) {
7265 		iface_addr = 1;
7266 		pos += 6;
7267 	}
7268 	if (hwaddr_aton(pos, peer))
7269 		return -1;
7270 
7271 	wpas_p2p_remove_client(wpa_s, peer, iface_addr);
7272 	return 0;
7273 }
7274 
7275 
p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant * wpa_s,char * cmd)7276 static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
7277 {
7278 	int freq = 0, period = 0, interval = 0, count = 0;
7279 
7280 	if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
7281 	{
7282 		wpa_printf(MSG_DEBUG,
7283 			   "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
7284 		return -1;
7285 	}
7286 
7287 	return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
7288 }
7289 
7290 #endif /* CONFIG_P2P */
7291 
7292 
freq_range_to_channel_list(struct wpa_supplicant * wpa_s,char * val)7293 static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
7294 {
7295 	struct wpa_freq_range_list ranges;
7296 	int *freqs = NULL;
7297 	struct hostapd_hw_modes *mode;
7298 	u16 i;
7299 
7300 	if (wpa_s->hw.modes == NULL)
7301 		return NULL;
7302 
7303 	os_memset(&ranges, 0, sizeof(ranges));
7304 	if (freq_range_list_parse(&ranges, val) < 0)
7305 		return NULL;
7306 
7307 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
7308 		int j;
7309 
7310 		mode = &wpa_s->hw.modes[i];
7311 		for (j = 0; j < mode->num_channels; j++) {
7312 			unsigned int freq;
7313 
7314 			if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
7315 				continue;
7316 
7317 			freq = mode->channels[j].freq;
7318 			if (!freq_range_list_includes(&ranges, freq))
7319 				continue;
7320 
7321 			int_array_add_unique(&freqs, freq);
7322 		}
7323 	}
7324 
7325 	os_free(ranges.range);
7326 	return freqs;
7327 }
7328 
7329 
7330 #ifdef CONFIG_INTERWORKING
7331 
ctrl_interworking_select(struct wpa_supplicant * wpa_s,char * param)7332 static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
7333 {
7334 	int auto_sel = 0;
7335 	int *freqs = NULL;
7336 
7337 	if (param) {
7338 		char *pos;
7339 
7340 		auto_sel = os_strstr(param, "auto") != NULL;
7341 
7342 		pos = os_strstr(param, "freq=");
7343 		if (pos) {
7344 			freqs = freq_range_to_channel_list(wpa_s, pos + 5);
7345 			if (freqs == NULL)
7346 				return -1;
7347 		}
7348 
7349 	}
7350 
7351 	return interworking_select(wpa_s, auto_sel, freqs);
7352 }
7353 
7354 
ctrl_interworking_connect(struct wpa_supplicant * wpa_s,char * dst,int only_add)7355 static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
7356 				     int only_add)
7357 {
7358 	u8 bssid[ETH_ALEN];
7359 	struct wpa_bss *bss;
7360 
7361 	if (hwaddr_aton(dst, bssid)) {
7362 		wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
7363 		return -1;
7364 	}
7365 
7366 	bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
7367 	if (bss == NULL) {
7368 		wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
7369 			   MAC2STR(bssid));
7370 		return -1;
7371 	}
7372 
7373 	if (bss->ssid_len == 0) {
7374 		int found = 0;
7375 
7376 		wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
7377 			   " does not have SSID information", MAC2STR(bssid));
7378 
7379 		dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
7380 					 list) {
7381 			if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
7382 			    bss->ssid_len > 0) {
7383 				found = 1;
7384 				break;
7385 			}
7386 		}
7387 
7388 		if (!found)
7389 			return -1;
7390 		wpa_printf(MSG_DEBUG,
7391 			   "Found another matching BSS entry with SSID");
7392 	}
7393 
7394 	return interworking_connect(wpa_s, bss, only_add);
7395 }
7396 
7397 
get_anqp(struct wpa_supplicant * wpa_s,char * dst)7398 static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
7399 {
7400 	u8 dst_addr[ETH_ALEN];
7401 	int used;
7402 	char *pos;
7403 #define MAX_ANQP_INFO_ID 100
7404 	u16 id[MAX_ANQP_INFO_ID];
7405 	size_t num_id = 0;
7406 	u32 subtypes = 0;
7407 	u32 mbo_subtypes = 0;
7408 
7409 	used = hwaddr_aton2(dst, dst_addr);
7410 	if (used < 0)
7411 		return -1;
7412 	pos = dst + used;
7413 	if (*pos == ' ')
7414 		pos++;
7415 	while (num_id < MAX_ANQP_INFO_ID) {
7416 		if (os_strncmp(pos, "hs20:", 5) == 0) {
7417 #ifdef CONFIG_HS20
7418 			int num = atoi(pos + 5);
7419 			if (num <= 0 || num > 31)
7420 				return -1;
7421 			subtypes |= BIT(num);
7422 #else /* CONFIG_HS20 */
7423 			return -1;
7424 #endif /* CONFIG_HS20 */
7425 		} else if (os_strncmp(pos, "mbo:", 4) == 0) {
7426 #ifdef CONFIG_MBO
7427 			int num = atoi(pos + 4);
7428 
7429 			if (num <= 0 || num > MAX_MBO_ANQP_SUBTYPE)
7430 				return -1;
7431 			mbo_subtypes |= BIT(num);
7432 #else /* CONFIG_MBO */
7433 			return -1;
7434 #endif /* CONFIG_MBO */
7435 		} else {
7436 			id[num_id] = atoi(pos);
7437 			if (id[num_id])
7438 				num_id++;
7439 		}
7440 		pos = os_strchr(pos + 1, ',');
7441 		if (pos == NULL)
7442 			break;
7443 		pos++;
7444 	}
7445 
7446 	if (num_id == 0 && !subtypes && !mbo_subtypes)
7447 		return -1;
7448 
7449 	return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes,
7450 			     mbo_subtypes);
7451 }
7452 
7453 
gas_request(struct wpa_supplicant * wpa_s,char * cmd)7454 static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
7455 {
7456 	u8 dst_addr[ETH_ALEN];
7457 	struct wpabuf *advproto, *query = NULL;
7458 	int used, ret = -1;
7459 	char *pos, *end;
7460 	size_t len;
7461 
7462 	used = hwaddr_aton2(cmd, dst_addr);
7463 	if (used < 0)
7464 		return -1;
7465 
7466 	pos = cmd + used;
7467 	while (*pos == ' ')
7468 		pos++;
7469 
7470 	/* Advertisement Protocol ID */
7471 	end = os_strchr(pos, ' ');
7472 	if (end)
7473 		len = end - pos;
7474 	else
7475 		len = os_strlen(pos);
7476 	if (len & 0x01)
7477 		return -1;
7478 	len /= 2;
7479 	if (len == 0)
7480 		return -1;
7481 	advproto = wpabuf_alloc(len);
7482 	if (advproto == NULL)
7483 		return -1;
7484 	if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
7485 		goto fail;
7486 
7487 	if (end) {
7488 		/* Optional Query Request */
7489 		pos = end + 1;
7490 		while (*pos == ' ')
7491 			pos++;
7492 
7493 		len = os_strlen(pos);
7494 		if (len) {
7495 			if (len & 0x01)
7496 				goto fail;
7497 			len /= 2;
7498 			if (len == 0)
7499 				goto fail;
7500 			query = wpabuf_alloc(len);
7501 			if (query == NULL)
7502 				goto fail;
7503 			if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
7504 				goto fail;
7505 		}
7506 	}
7507 
7508 	ret = gas_send_request(wpa_s, dst_addr, advproto, query);
7509 
7510 fail:
7511 	wpabuf_free(advproto);
7512 	wpabuf_free(query);
7513 
7514 	return ret;
7515 }
7516 
7517 
gas_response_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)7518 static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
7519 			    size_t buflen)
7520 {
7521 	u8 addr[ETH_ALEN];
7522 	int dialog_token;
7523 	int used;
7524 	char *pos;
7525 	size_t resp_len, start, requested_len;
7526 	struct wpabuf *resp;
7527 	int ret;
7528 
7529 	used = hwaddr_aton2(cmd, addr);
7530 	if (used < 0)
7531 		return -1;
7532 
7533 	pos = cmd + used;
7534 	while (*pos == ' ')
7535 		pos++;
7536 	dialog_token = atoi(pos);
7537 
7538 	if (wpa_s->last_gas_resp &&
7539 	    os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
7540 	    dialog_token == wpa_s->last_gas_dialog_token)
7541 		resp = wpa_s->last_gas_resp;
7542 	else if (wpa_s->prev_gas_resp &&
7543 		 os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
7544 		 dialog_token == wpa_s->prev_gas_dialog_token)
7545 		resp = wpa_s->prev_gas_resp;
7546 	else
7547 		return -1;
7548 
7549 	resp_len = wpabuf_len(resp);
7550 	start = 0;
7551 	requested_len = resp_len;
7552 
7553 	pos = os_strchr(pos, ' ');
7554 	if (pos) {
7555 		start = atoi(pos);
7556 		if (start > resp_len)
7557 			return os_snprintf(buf, buflen, "FAIL-Invalid range");
7558 		pos = os_strchr(pos, ',');
7559 		if (pos == NULL)
7560 			return -1;
7561 		pos++;
7562 		requested_len = atoi(pos);
7563 		if (start + requested_len > resp_len)
7564 			return os_snprintf(buf, buflen, "FAIL-Invalid range");
7565 	}
7566 
7567 	if (requested_len * 2 + 1 > buflen)
7568 		return os_snprintf(buf, buflen, "FAIL-Too long response");
7569 
7570 	ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
7571 			       requested_len);
7572 
7573 	if (start + requested_len == resp_len) {
7574 		/*
7575 		 * Free memory by dropping the response after it has been
7576 		 * fetched.
7577 		 */
7578 		if (resp == wpa_s->prev_gas_resp) {
7579 			wpabuf_free(wpa_s->prev_gas_resp);
7580 			wpa_s->prev_gas_resp = NULL;
7581 		} else {
7582 			wpabuf_free(wpa_s->last_gas_resp);
7583 			wpa_s->last_gas_resp = NULL;
7584 		}
7585 	}
7586 
7587 	return ret;
7588 }
7589 #endif /* CONFIG_INTERWORKING */
7590 
7591 
7592 #ifdef CONFIG_HS20
7593 
get_hs20_anqp(struct wpa_supplicant * wpa_s,char * dst)7594 static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
7595 {
7596 	u8 dst_addr[ETH_ALEN];
7597 	int used;
7598 	char *pos;
7599 	u32 subtypes = 0;
7600 
7601 	used = hwaddr_aton2(dst, dst_addr);
7602 	if (used < 0)
7603 		return -1;
7604 	pos = dst + used;
7605 	if (*pos == ' ')
7606 		pos++;
7607 	for (;;) {
7608 		int num = atoi(pos);
7609 		if (num <= 0 || num > 31)
7610 			return -1;
7611 		subtypes |= BIT(num);
7612 		pos = os_strchr(pos + 1, ',');
7613 		if (pos == NULL)
7614 			break;
7615 		pos++;
7616 	}
7617 
7618 	if (subtypes == 0)
7619 		return -1;
7620 
7621 	return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
7622 }
7623 
7624 
hs20_nai_home_realm_list(struct wpa_supplicant * wpa_s,const u8 * addr,const char * realm)7625 static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
7626 				    const u8 *addr, const char *realm)
7627 {
7628 	u8 *buf;
7629 	size_t rlen, len;
7630 	int ret;
7631 
7632 	rlen = os_strlen(realm);
7633 	len = 3 + rlen;
7634 	buf = os_malloc(len);
7635 	if (buf == NULL)
7636 		return -1;
7637 	buf[0] = 1; /* NAI Home Realm Count */
7638 	buf[1] = 0; /* Formatted in accordance with RFC 4282 */
7639 	buf[2] = rlen;
7640 	os_memcpy(buf + 3, realm, rlen);
7641 
7642 	ret = hs20_anqp_send_req(wpa_s, addr,
7643 				 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
7644 				 buf, len, 0);
7645 
7646 	os_free(buf);
7647 
7648 	return ret;
7649 }
7650 
7651 
hs20_get_nai_home_realm_list(struct wpa_supplicant * wpa_s,char * dst)7652 static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
7653 					char *dst)
7654 {
7655 	struct wpa_cred *cred = wpa_s->conf->cred;
7656 	u8 dst_addr[ETH_ALEN];
7657 	int used;
7658 	u8 *buf;
7659 	size_t len;
7660 	int ret;
7661 
7662 	used = hwaddr_aton2(dst, dst_addr);
7663 	if (used < 0)
7664 		return -1;
7665 
7666 	while (dst[used] == ' ')
7667 		used++;
7668 	if (os_strncmp(dst + used, "realm=", 6) == 0)
7669 		return hs20_nai_home_realm_list(wpa_s, dst_addr,
7670 						dst + used + 6);
7671 
7672 	len = os_strlen(dst + used);
7673 
7674 	if (len == 0 && cred && cred->realm)
7675 		return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
7676 
7677 	if (len & 1)
7678 		return -1;
7679 	len /= 2;
7680 	buf = os_malloc(len);
7681 	if (buf == NULL)
7682 		return -1;
7683 	if (hexstr2bin(dst + used, buf, len) < 0) {
7684 		os_free(buf);
7685 		return -1;
7686 	}
7687 
7688 	ret = hs20_anqp_send_req(wpa_s, dst_addr,
7689 				 BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
7690 				 buf, len, 0);
7691 	os_free(buf);
7692 
7693 	return ret;
7694 }
7695 
7696 
get_hs20_icon(struct wpa_supplicant * wpa_s,char * cmd,char * reply,int buflen)7697 static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
7698 			 int buflen)
7699 {
7700 	u8 dst_addr[ETH_ALEN];
7701 	int used;
7702 	char *ctx = NULL, *icon, *poffset, *psize;
7703 
7704 	used = hwaddr_aton2(cmd, dst_addr);
7705 	if (used < 0)
7706 		return -1;
7707 	cmd += used;
7708 
7709 	icon = str_token(cmd, " ", &ctx);
7710 	poffset = str_token(cmd, " ", &ctx);
7711 	psize = str_token(cmd, " ", &ctx);
7712 	if (!icon || !poffset || !psize)
7713 		return -1;
7714 
7715 	wpa_s->fetch_osu_icon_in_progress = 0;
7716 	return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
7717 			     reply, buflen);
7718 }
7719 
7720 
del_hs20_icon(struct wpa_supplicant * wpa_s,char * cmd)7721 static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
7722 {
7723 	u8 dst_addr[ETH_ALEN];
7724 	int used;
7725 	char *icon;
7726 
7727 	if (!cmd[0])
7728 		return hs20_del_icon(wpa_s, NULL, NULL);
7729 
7730 	used = hwaddr_aton2(cmd, dst_addr);
7731 	if (used < 0)
7732 		return -1;
7733 
7734 	while (cmd[used] == ' ')
7735 		used++;
7736 	icon = cmd[used] ? &cmd[used] : NULL;
7737 
7738 	return hs20_del_icon(wpa_s, dst_addr, icon);
7739 }
7740 
7741 
hs20_icon_request(struct wpa_supplicant * wpa_s,char * cmd,int inmem)7742 static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
7743 {
7744 	u8 dst_addr[ETH_ALEN];
7745 	int used;
7746 	char *icon;
7747 
7748 	used = hwaddr_aton2(cmd, dst_addr);
7749 	if (used < 0)
7750 		return -1;
7751 
7752 	while (cmd[used] == ' ')
7753 		used++;
7754 	icon = &cmd[used];
7755 
7756 	wpa_s->fetch_osu_icon_in_progress = 0;
7757 	return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
7758 				  (u8 *) icon, os_strlen(icon), inmem);
7759 }
7760 
7761 #endif /* CONFIG_HS20 */
7762 
7763 
7764 #ifdef CONFIG_AUTOSCAN
7765 
wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant * wpa_s,char * cmd)7766 static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
7767 					      char *cmd)
7768 {
7769 	enum wpa_states state = wpa_s->wpa_state;
7770 	char *new_params = NULL;
7771 
7772 	if (os_strlen(cmd) > 0) {
7773 		new_params = os_strdup(cmd);
7774 		if (new_params == NULL)
7775 			return -1;
7776 	}
7777 
7778 	os_free(wpa_s->conf->autoscan);
7779 	wpa_s->conf->autoscan = new_params;
7780 
7781 	if (wpa_s->conf->autoscan == NULL)
7782 		autoscan_deinit(wpa_s);
7783 	else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
7784 		autoscan_init(wpa_s, 1);
7785 	else if (state == WPA_SCANNING)
7786 		wpa_supplicant_reinit_autoscan(wpa_s);
7787 	else
7788 		wpa_printf(MSG_DEBUG, "No autoscan update in state %s",
7789 			   wpa_supplicant_state_txt(state));
7790 
7791 	return 0;
7792 }
7793 
7794 #endif /* CONFIG_AUTOSCAN */
7795 
7796 
7797 #ifdef CONFIG_WNM
7798 
wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant * wpa_s,char * cmd)7799 static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
7800 {
7801 	int enter;
7802 	int intval = 0;
7803 	char *pos;
7804 	int ret;
7805 	struct wpabuf *tfs_req = NULL;
7806 
7807 	if (os_strncmp(cmd, "enter", 5) == 0)
7808 		enter = 1;
7809 	else if (os_strncmp(cmd, "exit", 4) == 0)
7810 		enter = 0;
7811 	else
7812 		return -1;
7813 
7814 	pos = os_strstr(cmd, " interval=");
7815 	if (pos)
7816 		intval = atoi(pos + 10);
7817 
7818 	pos = os_strstr(cmd, " tfs_req=");
7819 	if (pos) {
7820 		char *end;
7821 		size_t len;
7822 		pos += 9;
7823 		end = os_strchr(pos, ' ');
7824 		if (end)
7825 			len = end - pos;
7826 		else
7827 			len = os_strlen(pos);
7828 		if (len & 1)
7829 			return -1;
7830 		len /= 2;
7831 		tfs_req = wpabuf_alloc(len);
7832 		if (tfs_req == NULL)
7833 			return -1;
7834 		if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
7835 			wpabuf_free(tfs_req);
7836 			return -1;
7837 		}
7838 	}
7839 
7840 	ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
7841 					   WNM_SLEEP_MODE_EXIT, intval,
7842 					   tfs_req);
7843 	wpabuf_free(tfs_req);
7844 
7845 	return ret;
7846 }
7847 
7848 
wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant * wpa_s,char * cmd)7849 static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
7850 {
7851 	int query_reason, list = 0;
7852 	char *btm_candidates = NULL;
7853 
7854 	query_reason = atoi(cmd);
7855 
7856 	cmd = os_strchr(cmd, ' ');
7857 	if (cmd) {
7858 		if (os_strncmp(cmd, " list", 5) == 0)
7859 			list = 1;
7860 		else
7861 			btm_candidates = cmd;
7862 	}
7863 
7864 	wpa_printf(MSG_DEBUG,
7865 		   "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
7866 		   query_reason, list ? " candidate list" : "");
7867 
7868 	return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason,
7869 						  btm_candidates,
7870 						  list);
7871 }
7872 
7873 
wpas_ctrl_iface_coloc_intf_report(struct wpa_supplicant * wpa_s,char * cmd)7874 static int wpas_ctrl_iface_coloc_intf_report(struct wpa_supplicant *wpa_s,
7875 					     char *cmd)
7876 {
7877 	struct wpabuf *elems;
7878 	int ret;
7879 
7880 	elems = wpabuf_parse_bin(cmd);
7881 	if (!elems)
7882 		return -1;
7883 
7884 	ret = wnm_send_coloc_intf_report(wpa_s, 0, elems);
7885 	wpabuf_free(elems);
7886 	return ret;
7887 }
7888 
7889 #endif /* CONFIG_WNM */
7890 
7891 
wpa_supplicant_signal_poll(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)7892 static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
7893 				      size_t buflen)
7894 {
7895 	struct wpa_signal_info si;
7896 	int ret;
7897 	char *pos, *end;
7898 
7899 	ret = wpa_drv_signal_poll(wpa_s, &si);
7900 	if (ret)
7901 		return -1;
7902 
7903 	pos = buf;
7904 	end = buf + buflen;
7905 
7906 	ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
7907 			  "NOISE=%d\nFREQUENCY=%u\n",
7908 			  si.current_signal, si.current_txrate / 1000,
7909 			  si.current_noise, si.frequency);
7910 	if (os_snprintf_error(end - pos, ret))
7911 		return -1;
7912 	pos += ret;
7913 
7914 	if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
7915 		ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
7916 				  channel_width_to_string(si.chanwidth));
7917 		if (os_snprintf_error(end - pos, ret))
7918 			return -1;
7919 		pos += ret;
7920 	}
7921 
7922 	if (si.center_frq1 > 0) {
7923 		ret = os_snprintf(pos, end - pos, "CENTER_FRQ1=%d\n",
7924 				  si.center_frq1);
7925 		if (os_snprintf_error(end - pos, ret))
7926 			return -1;
7927 		pos += ret;
7928 	}
7929 
7930 	if (si.center_frq2 > 0) {
7931 		ret = os_snprintf(pos, end - pos, "CENTER_FRQ2=%d\n",
7932 				  si.center_frq2);
7933 		if (os_snprintf_error(end - pos, ret))
7934 			return -1;
7935 		pos += ret;
7936 	}
7937 
7938 	if (si.avg_signal) {
7939 		ret = os_snprintf(pos, end - pos,
7940 				  "AVG_RSSI=%d\n", si.avg_signal);
7941 		if (os_snprintf_error(end - pos, ret))
7942 			return -1;
7943 		pos += ret;
7944 	}
7945 
7946 	if (si.avg_beacon_signal) {
7947 		ret = os_snprintf(pos, end - pos,
7948 				  "AVG_BEACON_RSSI=%d\n", si.avg_beacon_signal);
7949 		if (os_snprintf_error(end - pos, ret))
7950 			return -1;
7951 		pos += ret;
7952 	}
7953 
7954 	return pos - buf;
7955 }
7956 
7957 
wpas_ctrl_iface_signal_monitor(struct wpa_supplicant * wpa_s,const char * cmd)7958 static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
7959 					  const char *cmd)
7960 {
7961 	const char *pos;
7962 	int threshold = 0;
7963 	int hysteresis = 0;
7964 
7965 	if (wpa_s->bgscan && wpa_s->bgscan_priv) {
7966 		wpa_printf(MSG_DEBUG,
7967 			   "Reject SIGNAL_MONITOR command - bgscan is active");
7968 		return -1;
7969 	}
7970 	pos = os_strstr(cmd, "THRESHOLD=");
7971 	if (pos)
7972 		threshold = atoi(pos + 10);
7973 	pos = os_strstr(cmd, "HYSTERESIS=");
7974 	if (pos)
7975 		hysteresis = atoi(pos + 11);
7976 	return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
7977 }
7978 
7979 
7980 #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)7981 int wpas_ctrl_iface_get_pref_freq_list_override(struct wpa_supplicant *wpa_s,
7982 						enum wpa_driver_if_type if_type,
7983 						unsigned int *num,
7984 						unsigned int *freq_list)
7985 {
7986 	char *pos = wpa_s->get_pref_freq_list_override;
7987 	char *end;
7988 	unsigned int count = 0;
7989 
7990 	/* Override string format:
7991 	 *  <if_type1>:<freq1>,<freq2>,... <if_type2>:... */
7992 
7993 	while (pos) {
7994 		if (atoi(pos) == (int) if_type)
7995 			break;
7996 		pos = os_strchr(pos, ' ');
7997 		if (pos)
7998 			pos++;
7999 	}
8000 	if (!pos)
8001 		return -1;
8002 	pos = os_strchr(pos, ':');
8003 	if (!pos)
8004 		return -1;
8005 	pos++;
8006 	end = os_strchr(pos, ' ');
8007 	while (pos && (!end || pos < end) && count < *num) {
8008 		freq_list[count++] = atoi(pos);
8009 		pos = os_strchr(pos, ',');
8010 		if (pos)
8011 			pos++;
8012 	}
8013 
8014 	*num = count;
8015 	return 0;
8016 }
8017 #endif /* CONFIG_TESTING_OPTIONS */
8018 
8019 
wpas_ctrl_iface_get_pref_freq_list(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8020 static int wpas_ctrl_iface_get_pref_freq_list(
8021 	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
8022 {
8023 	unsigned int freq_list[100], num = 100, i;
8024 	int ret;
8025 	enum wpa_driver_if_type iface_type;
8026 	char *pos, *end;
8027 
8028 	pos = buf;
8029 	end = buf + buflen;
8030 
8031 	/* buf: "<interface_type>" */
8032 	if (os_strcmp(cmd, "STATION") == 0)
8033 		iface_type = WPA_IF_STATION;
8034 	else if (os_strcmp(cmd, "AP") == 0)
8035 		iface_type = WPA_IF_AP_BSS;
8036 	else if (os_strcmp(cmd, "P2P_GO") == 0)
8037 		iface_type = WPA_IF_P2P_GO;
8038 	else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
8039 		iface_type = WPA_IF_P2P_CLIENT;
8040 	else if (os_strcmp(cmd, "IBSS") == 0)
8041 		iface_type = WPA_IF_IBSS;
8042 	else if (os_strcmp(cmd, "TDLS") == 0)
8043 		iface_type = WPA_IF_TDLS;
8044 	else
8045 		return -1;
8046 
8047 	wpa_printf(MSG_DEBUG,
8048 		   "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
8049 		   iface_type, cmd);
8050 
8051 	ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
8052 	if (ret)
8053 		return -1;
8054 
8055 	for (i = 0; i < num; i++) {
8056 		ret = os_snprintf(pos, end - pos, "%s%u",
8057 				  i > 0 ? "," : "", freq_list[i]);
8058 		if (os_snprintf_error(end - pos, ret))
8059 			return -1;
8060 		pos += ret;
8061 	}
8062 
8063 	return pos - buf;
8064 }
8065 
8066 
wpas_ctrl_iface_driver_flags(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8067 static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
8068 					char *buf, size_t buflen)
8069 {
8070 	int ret, i;
8071 	char *pos, *end;
8072 
8073 	ret = os_snprintf(buf, buflen, "%016llX:\n",
8074 			  (long long unsigned) wpa_s->drv_flags);
8075 	if (os_snprintf_error(buflen, ret))
8076 		return -1;
8077 
8078 	pos = buf + ret;
8079 	end = buf + buflen;
8080 
8081 	for (i = 0; i < 64; i++) {
8082 		if (wpa_s->drv_flags & (1LLU << i)) {
8083 			ret = os_snprintf(pos, end - pos, "%s\n",
8084 					  driver_flag_to_string(1LLU << i));
8085 			if (os_snprintf_error(end - pos, ret))
8086 				return -1;
8087 			pos += ret;
8088 		}
8089 	}
8090 
8091 	return pos - buf;
8092 }
8093 
8094 
wpas_ctrl_iface_driver_flags2(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8095 static int wpas_ctrl_iface_driver_flags2(struct wpa_supplicant *wpa_s,
8096 					 char *buf, size_t buflen)
8097 {
8098 	int ret, i;
8099 	char *pos, *end;
8100 
8101 	ret = os_snprintf(buf, buflen, "%016llX:\n",
8102 			  (long long unsigned) wpa_s->drv_flags2);
8103 	if (os_snprintf_error(buflen, ret))
8104 		return -1;
8105 
8106 	pos = buf + ret;
8107 	end = buf + buflen;
8108 
8109 	for (i = 0; i < 64; i++) {
8110 		if (wpa_s->drv_flags2 & (1LLU << i)) {
8111 			ret = os_snprintf(pos, end - pos, "%s\n",
8112 					  driver_flag2_to_string(1LLU << i));
8113 			if (os_snprintf_error(end - pos, ret))
8114 				return -1;
8115 			pos += ret;
8116 		}
8117 	}
8118 
8119 	return pos - buf;
8120 }
8121 
8122 
wpa_supplicant_pktcnt_poll(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8123 static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
8124 				      size_t buflen)
8125 {
8126 	struct hostap_sta_driver_data sta;
8127 	int ret;
8128 
8129 	ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
8130 	if (ret)
8131 		return -1;
8132 
8133 	ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
8134 			  sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
8135 	if (os_snprintf_error(buflen, ret))
8136 		return -1;
8137 	return ret;
8138 }
8139 
8140 
8141 #ifdef ANDROID
wpa_supplicant_driver_cmd(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8142 static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
8143 				     char *buf, size_t buflen)
8144 {
8145 	int ret;
8146 
8147 	ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
8148 	if (ret == 0) {
8149 		if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
8150 			struct p2p_data *p2p = wpa_s->global->p2p;
8151 			if (p2p) {
8152 				char country[3];
8153 				country[0] = cmd[8];
8154 				country[1] = cmd[9];
8155 				country[2] = 0x04;
8156 				p2p_set_country(p2p, country);
8157 			}
8158 		}
8159 		ret = os_snprintf(buf, buflen, "%s\n", "OK");
8160 		if (os_snprintf_error(buflen, ret))
8161 			ret = -1;
8162 	}
8163 	return ret;
8164 }
8165 #endif /* ANDROID */
8166 
8167 
wpa_supplicant_vendor_cmd(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8168 static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
8169 				     char *buf, size_t buflen)
8170 {
8171 	int ret;
8172 	char *pos;
8173 	u8 *data = NULL;
8174 	unsigned int vendor_id, subcmd;
8175 	struct wpabuf *reply;
8176 	size_t data_len = 0;
8177 
8178 	/* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
8179 	vendor_id = strtoul(cmd, &pos, 16);
8180 	if (!isblank((unsigned char) *pos))
8181 		return -EINVAL;
8182 
8183 	subcmd = strtoul(pos, &pos, 10);
8184 
8185 	if (*pos != '\0') {
8186 		if (!isblank((unsigned char) *pos++))
8187 			return -EINVAL;
8188 		data_len = os_strlen(pos);
8189 	}
8190 
8191 	if (data_len) {
8192 		data_len /= 2;
8193 		data = os_malloc(data_len);
8194 		if (!data)
8195 			return -1;
8196 
8197 		if (hexstr2bin(pos, data, data_len)) {
8198 			wpa_printf(MSG_DEBUG,
8199 				   "Vendor command: wrong parameter format");
8200 			os_free(data);
8201 			return -EINVAL;
8202 		}
8203 	}
8204 
8205 	reply = wpabuf_alloc((buflen - 1) / 2);
8206 	if (!reply) {
8207 		os_free(data);
8208 		return -1;
8209 	}
8210 
8211 	ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
8212 				 reply);
8213 
8214 	if (ret == 0)
8215 		ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
8216 				       wpabuf_len(reply));
8217 
8218 	wpabuf_free(reply);
8219 	os_free(data);
8220 
8221 	return ret;
8222 }
8223 
8224 
wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant * wpa_s)8225 static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
8226 {
8227 #ifdef CONFIG_P2P
8228 	struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
8229 		wpa_s->global->p2p_init_wpa_s : wpa_s;
8230 #endif /* CONFIG_P2P */
8231 
8232 	wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
8233 
8234 	if (wpas_abort_ongoing_scan(wpa_s) == 0)
8235 		wpa_s->ignore_post_flush_scan_res = 1;
8236 
8237 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
8238 		/*
8239 		 * Avoid possible auto connect re-connection on getting
8240 		 * disconnected due to state flush.
8241 		 */
8242 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
8243 	}
8244 
8245 #ifdef CONFIG_P2P
8246 	wpas_p2p_group_remove(p2p_wpa_s, "*");
8247 	wpas_p2p_cancel(p2p_wpa_s);
8248 	p2p_ctrl_flush(p2p_wpa_s);
8249 	wpas_p2p_service_flush(p2p_wpa_s);
8250 	p2p_wpa_s->global->p2p_disabled = 0;
8251 	p2p_wpa_s->global->p2p_per_sta_psk = 0;
8252 	p2p_wpa_s->conf->num_sec_device_types = 0;
8253 	p2p_wpa_s->p2p_disable_ip_addr_req = 0;
8254 	os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
8255 	p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
8256 	p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
8257 	p2p_wpa_s->global->pending_p2ps_group = 0;
8258 	p2p_wpa_s->global->pending_p2ps_group_freq = 0;
8259 #endif /* CONFIG_P2P */
8260 
8261 #ifdef CONFIG_WPS_TESTING
8262 	wps_version_number = 0x20;
8263 	wps_testing_dummy_cred = 0;
8264 	wps_corrupt_pkhash = 0;
8265 	wps_force_auth_types_in_use = 0;
8266 	wps_force_encr_types_in_use = 0;
8267 #endif /* CONFIG_WPS_TESTING */
8268 #ifdef CONFIG_WPS
8269 	wpa_s->wps_fragment_size = 0;
8270 	wpas_wps_cancel(wpa_s);
8271 	wps_registrar_flush(wpa_s->wps->registrar);
8272 #endif /* CONFIG_WPS */
8273 	wpa_s->after_wps = 0;
8274 	wpa_s->known_wps_freq = 0;
8275 
8276 #ifdef CONFIG_DPP
8277 	wpas_dpp_deinit(wpa_s);
8278 	wpa_s->dpp_init_max_tries = 0;
8279 	wpa_s->dpp_init_retry_time = 0;
8280 	wpa_s->dpp_resp_wait_time = 0;
8281 	wpa_s->dpp_resp_max_tries = 0;
8282 	wpa_s->dpp_resp_retry_time = 0;
8283 #ifdef CONFIG_DPP2
8284 	wpas_dpp_chirp_stop(wpa_s);
8285 	wpa_s->dpp_pfs_fallback = 0;
8286 #endif /* CONFIG_DPP2 */
8287 #ifdef CONFIG_TESTING_OPTIONS
8288 	os_memset(dpp_pkex_own_mac_override, 0, ETH_ALEN);
8289 	os_memset(dpp_pkex_peer_mac_override, 0, ETH_ALEN);
8290 	dpp_pkex_ephemeral_key_override_len = 0;
8291 	dpp_protocol_key_override_len = 0;
8292 	dpp_nonce_override_len = 0;
8293 #ifdef CONFIG_DPP2
8294 	dpp_version_override = 2;
8295 #else /* CONFIG_DPP2 */
8296 	dpp_version_override = 1;
8297 #endif /* CONFIG_DPP2 */
8298 #endif /* CONFIG_TESTING_OPTIONS */
8299 #endif /* CONFIG_DPP */
8300 
8301 #ifdef CONFIG_TDLS
8302 #ifdef CONFIG_TDLS_TESTING
8303 	tdls_testing = 0;
8304 #endif /* CONFIG_TDLS_TESTING */
8305 	wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
8306 	wpa_tdls_enable(wpa_s->wpa, 1);
8307 #endif /* CONFIG_TDLS */
8308 
8309 	eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
8310 	wpa_supplicant_stop_countermeasures(wpa_s, NULL);
8311 	wpa_s->last_michael_mic_error.sec = 0;
8312 
8313 	wpa_s->no_keep_alive = 0;
8314 	wpa_s->own_disconnect_req = 0;
8315 	wpa_s->own_reconnect_req = 0;
8316 	wpa_s->deny_ptk0_rekey = 0;
8317 
8318 	os_free(wpa_s->disallow_aps_bssid);
8319 	wpa_s->disallow_aps_bssid = NULL;
8320 	wpa_s->disallow_aps_bssid_count = 0;
8321 	os_free(wpa_s->disallow_aps_ssid);
8322 	wpa_s->disallow_aps_ssid = NULL;
8323 	wpa_s->disallow_aps_ssid_count = 0;
8324 
8325 	wpa_s->set_sta_uapsd = 0;
8326 	wpa_s->sta_uapsd = 0;
8327 
8328 	wpa_drv_radio_disable(wpa_s, 0);
8329 	wpa_blacklist_clear(wpa_s);
8330 	wpa_s->extra_blacklist_count = 0;
8331 	wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
8332 	wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
8333 	wpa_config_flush_blobs(wpa_s->conf);
8334 	wpa_s->conf->auto_interworking = 0;
8335 	wpa_s->conf->okc = 0;
8336 
8337 	wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
8338 	rsn_preauth_deinit(wpa_s->wpa);
8339 
8340 	wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
8341 	wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
8342 	wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
8343 	eapol_sm_notify_logoff(wpa_s->eapol, false);
8344 
8345 	radio_remove_works(wpa_s, NULL, 1);
8346 	wpa_s->ext_work_in_progress = 0;
8347 
8348 	wpa_s->next_ssid = NULL;
8349 
8350 #ifdef CONFIG_INTERWORKING
8351 #ifdef CONFIG_HS20
8352 	hs20_cancel_fetch_osu(wpa_s);
8353 	hs20_del_icon(wpa_s, NULL, NULL);
8354 #endif /* CONFIG_HS20 */
8355 #endif /* CONFIG_INTERWORKING */
8356 
8357 	wpa_s->ext_mgmt_frame_handling = 0;
8358 	wpa_s->ext_eapol_frame_io = 0;
8359 #ifdef CONFIG_TESTING_OPTIONS
8360 	wpa_s->extra_roc_dur = 0;
8361 	wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
8362 	wpa_s->p2p_go_csa_on_inv = 0;
8363 	wpa_s->ignore_auth_resp = 0;
8364 	wpa_s->ignore_assoc_disallow = 0;
8365 	wpa_s->disable_sa_query = 0;
8366 	wpa_s->testing_resend_assoc = 0;
8367 	wpa_s->ignore_sae_h2e_only = 0;
8368 	wpa_s->ft_rsnxe_used = 0;
8369 	wpa_s->reject_btm_req_reason = 0;
8370 	wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
8371 	os_free(wpa_s->get_pref_freq_list_override);
8372 	wpa_s->get_pref_freq_list_override = NULL;
8373 	wpabuf_free(wpa_s->sae_commit_override);
8374 	wpa_s->sae_commit_override = NULL;
8375 	os_free(wpa_s->extra_sae_rejected_groups);
8376 	wpa_s->extra_sae_rejected_groups = NULL;
8377 	wpabuf_free(wpa_s->rsne_override_eapol);
8378 	wpa_s->rsne_override_eapol = NULL;
8379 	wpabuf_free(wpa_s->rsnxe_override_assoc);
8380 	wpa_s->rsnxe_override_assoc = NULL;
8381 	wpabuf_free(wpa_s->rsnxe_override_eapol);
8382 	wpa_s->rsnxe_override_eapol = NULL;
8383 	wpas_clear_driver_signal_override(wpa_s);
8384 #ifdef CONFIG_DPP
8385 	os_free(wpa_s->dpp_config_obj_override);
8386 	wpa_s->dpp_config_obj_override = NULL;
8387 	os_free(wpa_s->dpp_discovery_override);
8388 	wpa_s->dpp_discovery_override = NULL;
8389 	os_free(wpa_s->dpp_groups_override);
8390 	wpa_s->dpp_groups_override = NULL;
8391 	dpp_test = DPP_TEST_DISABLED;
8392 #endif /* CONFIG_DPP */
8393 #endif /* CONFIG_TESTING_OPTIONS */
8394 
8395 	wpa_s->disconnected = 0;
8396 	os_free(wpa_s->next_scan_freqs);
8397 	wpa_s->next_scan_freqs = NULL;
8398 	os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN);
8399 	wpa_s->next_scan_bssid_wildcard_ssid = 0;
8400 	os_free(wpa_s->select_network_scan_freqs);
8401 	wpa_s->select_network_scan_freqs = NULL;
8402 
8403 	wpa_bss_flush(wpa_s);
8404 	if (!dl_list_empty(&wpa_s->bss)) {
8405 		wpa_printf(MSG_DEBUG,
8406 			   "BSS table not empty after flush: %u entries, current_bss=%p bssid="
8407 			   MACSTR " pending_bssid=" MACSTR,
8408 			   dl_list_len(&wpa_s->bss), wpa_s->current_bss,
8409 			   MAC2STR(wpa_s->bssid),
8410 			   MAC2STR(wpa_s->pending_bssid));
8411 	}
8412 
8413 	eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
8414 	wpa_s->wnmsleep_used = 0;
8415 
8416 #ifdef CONFIG_SME
8417 	wpa_s->sme.last_unprot_disconnect.sec = 0;
8418 	wpa_s->sme.auth_alg = 0;
8419 #endif /* CONFIG_SME */
8420 
8421 	wpabuf_free(wpa_s->ric_ies);
8422 	wpa_s->ric_ies = NULL;
8423 
8424 	wpa_supplicant_update_channel_list(wpa_s, NULL);
8425 
8426 	free_bss_tmp_disallowed(wpa_s);
8427 }
8428 
8429 
wpas_ctrl_radio_work_show(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)8430 static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
8431 				     char *buf, size_t buflen)
8432 {
8433 	struct wpa_radio_work *work;
8434 	char *pos, *end;
8435 	struct os_reltime now, diff;
8436 
8437 	pos = buf;
8438 	end = buf + buflen;
8439 
8440 	os_get_reltime(&now);
8441 
8442 	dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
8443 	{
8444 		int ret;
8445 
8446 		os_reltime_sub(&now, &work->time, &diff);
8447 		ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
8448 				  work->type, work->wpa_s->ifname, work->freq,
8449 				  work->started, diff.sec, diff.usec);
8450 		if (os_snprintf_error(end - pos, ret))
8451 			break;
8452 		pos += ret;
8453 	}
8454 
8455 	return pos - buf;
8456 }
8457 
8458 
wpas_ctrl_radio_work_timeout(void * eloop_ctx,void * timeout_ctx)8459 static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
8460 {
8461 	struct wpa_radio_work *work = eloop_ctx;
8462 	struct wpa_external_work *ework = work->ctx;
8463 
8464 	wpa_dbg(work->wpa_s, MSG_DEBUG,
8465 		"Timing out external radio work %u (%s)",
8466 		ework->id, work->type);
8467 	wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
8468 	work->wpa_s->ext_work_in_progress = 0;
8469 	radio_work_done(work);
8470 	os_free(ework);
8471 }
8472 
8473 
wpas_ctrl_radio_work_cb(struct wpa_radio_work * work,int deinit)8474 static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
8475 {
8476 	struct wpa_external_work *ework = work->ctx;
8477 
8478 	if (deinit) {
8479 		if (work->started)
8480 			eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
8481 					     work, NULL);
8482 
8483 		/*
8484 		 * work->type points to a buffer in ework, so need to replace
8485 		 * that here with a fixed string to avoid use of freed memory
8486 		 * in debug prints.
8487 		 */
8488 		work->type = "freed-ext-work";
8489 		work->ctx = NULL;
8490 		os_free(ework);
8491 		return;
8492 	}
8493 
8494 	wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
8495 		ework->id, ework->type);
8496 	wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
8497 	work->wpa_s->ext_work_in_progress = 1;
8498 	if (!ework->timeout)
8499 		ework->timeout = 10;
8500 	eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
8501 			       work, NULL);
8502 }
8503 
8504 
wpas_ctrl_radio_work_add(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8505 static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
8506 				    char *buf, size_t buflen)
8507 {
8508 	struct wpa_external_work *ework;
8509 	char *pos, *pos2;
8510 	size_t type_len;
8511 	int ret;
8512 	unsigned int freq = 0;
8513 
8514 	/* format: <name> [freq=<MHz>] [timeout=<seconds>] */
8515 
8516 	ework = os_zalloc(sizeof(*ework));
8517 	if (ework == NULL)
8518 		return -1;
8519 
8520 	pos = os_strchr(cmd, ' ');
8521 	if (pos) {
8522 		type_len = pos - cmd;
8523 		pos++;
8524 
8525 		pos2 = os_strstr(pos, "freq=");
8526 		if (pos2)
8527 			freq = atoi(pos2 + 5);
8528 
8529 		pos2 = os_strstr(pos, "timeout=");
8530 		if (pos2)
8531 			ework->timeout = atoi(pos2 + 8);
8532 	} else {
8533 		type_len = os_strlen(cmd);
8534 	}
8535 	if (4 + type_len >= sizeof(ework->type))
8536 		type_len = sizeof(ework->type) - 4 - 1;
8537 	os_strlcpy(ework->type, "ext:", sizeof(ework->type));
8538 	os_memcpy(ework->type + 4, cmd, type_len);
8539 	ework->type[4 + type_len] = '\0';
8540 
8541 	wpa_s->ext_work_id++;
8542 	if (wpa_s->ext_work_id == 0)
8543 		wpa_s->ext_work_id++;
8544 	ework->id = wpa_s->ext_work_id;
8545 
8546 	if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
8547 			   ework) < 0) {
8548 		os_free(ework);
8549 		return -1;
8550 	}
8551 
8552 	ret = os_snprintf(buf, buflen, "%u", ework->id);
8553 	if (os_snprintf_error(buflen, ret))
8554 		return -1;
8555 	return ret;
8556 }
8557 
8558 
wpas_ctrl_radio_work_done(struct wpa_supplicant * wpa_s,char * cmd)8559 static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
8560 {
8561 	struct wpa_radio_work *work;
8562 	unsigned int id = atoi(cmd);
8563 
8564 	dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
8565 	{
8566 		struct wpa_external_work *ework;
8567 
8568 		if (os_strncmp(work->type, "ext:", 4) != 0)
8569 			continue;
8570 		ework = work->ctx;
8571 		if (id && ework->id != id)
8572 			continue;
8573 		wpa_dbg(wpa_s, MSG_DEBUG,
8574 			"Completed external radio work %u (%s)",
8575 			ework->id, ework->type);
8576 		eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
8577 		wpa_s->ext_work_in_progress = 0;
8578 		radio_work_done(work);
8579 		os_free(ework);
8580 		return 3; /* "OK\n" */
8581 	}
8582 
8583 	return -1;
8584 }
8585 
8586 
wpas_ctrl_radio_work(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)8587 static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
8588 				char *buf, size_t buflen)
8589 {
8590 	if (os_strcmp(cmd, "show") == 0)
8591 		return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
8592 	if (os_strncmp(cmd, "add ", 4) == 0)
8593 		return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
8594 	if (os_strncmp(cmd, "done ", 5) == 0)
8595 		return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
8596 	return -1;
8597 }
8598 
8599 
wpas_ctrl_radio_work_flush(struct wpa_supplicant * wpa_s)8600 void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
8601 {
8602 	struct wpa_radio_work *work, *tmp;
8603 
8604 	if (!wpa_s || !wpa_s->radio)
8605 		return;
8606 
8607 	dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
8608 			      struct wpa_radio_work, list) {
8609 		struct wpa_external_work *ework;
8610 
8611 		if (os_strncmp(work->type, "ext:", 4) != 0)
8612 			continue;
8613 		ework = work->ctx;
8614 		wpa_dbg(wpa_s, MSG_DEBUG,
8615 			"Flushing%s external radio work %u (%s)",
8616 			work->started ? " started" : "", ework->id,
8617 			ework->type);
8618 		if (work->started)
8619 			eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
8620 					     work, NULL);
8621 		radio_work_done(work);
8622 		os_free(ework);
8623 	}
8624 }
8625 
8626 
wpas_ctrl_eapol_response(void * eloop_ctx,void * timeout_ctx)8627 static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
8628 {
8629 	struct wpa_supplicant *wpa_s = eloop_ctx;
8630 	eapol_sm_notify_ctrl_response(wpa_s->eapol);
8631 }
8632 
8633 
scan_id_list_parse(struct wpa_supplicant * wpa_s,const char * value,unsigned int * scan_id_count,int scan_id[])8634 static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
8635 			      unsigned int *scan_id_count, int scan_id[])
8636 {
8637 	const char *pos = value;
8638 
8639 	while (pos) {
8640 		if (*pos == ' ' || *pos == '\0')
8641 			break;
8642 		if (*scan_id_count == MAX_SCAN_ID)
8643 			return -1;
8644 		scan_id[(*scan_id_count)++] = atoi(pos);
8645 		pos = os_strchr(pos, ',');
8646 		if (pos)
8647 			pos++;
8648 	}
8649 
8650 	return 0;
8651 }
8652 
8653 
wpas_ctrl_scan(struct wpa_supplicant * wpa_s,char * params,char * reply,int reply_size,int * reply_len)8654 static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
8655 			   char *reply, int reply_size, int *reply_len)
8656 {
8657 	char *pos;
8658 	unsigned int manual_scan_passive = 0;
8659 	unsigned int manual_scan_use_id = 0;
8660 	unsigned int manual_scan_only_new = 0;
8661 	unsigned int scan_only = 0;
8662 	unsigned int scan_id_count = 0;
8663 	int scan_id[MAX_SCAN_ID];
8664 	void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
8665 				 struct wpa_scan_results *scan_res);
8666 	int *manual_scan_freqs = NULL;
8667 	struct wpa_ssid_value *ssid = NULL, *ns;
8668 	unsigned int ssid_count = 0;
8669 
8670 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
8671 		*reply_len = -1;
8672 		return;
8673 	}
8674 
8675 	if (radio_work_pending(wpa_s, "scan")) {
8676 		wpa_printf(MSG_DEBUG,
8677 			   "Pending scan scheduled - reject new request");
8678 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
8679 		return;
8680 	}
8681 
8682 #ifdef CONFIG_INTERWORKING
8683 	if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
8684 		wpa_printf(MSG_DEBUG,
8685 			   "Interworking select in progress - reject new scan");
8686 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
8687 		return;
8688 	}
8689 #endif /* CONFIG_INTERWORKING */
8690 
8691 	if (params) {
8692 		if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
8693 			scan_only = 1;
8694 
8695 		pos = os_strstr(params, "freq=");
8696 		if (pos) {
8697 			manual_scan_freqs = freq_range_to_channel_list(wpa_s,
8698 								       pos + 5);
8699 			if (manual_scan_freqs == NULL) {
8700 				*reply_len = -1;
8701 				goto done;
8702 			}
8703 		}
8704 
8705 		pos = os_strstr(params, "passive=");
8706 		if (pos)
8707 			manual_scan_passive = !!atoi(pos + 8);
8708 
8709 		pos = os_strstr(params, "use_id=");
8710 		if (pos)
8711 			manual_scan_use_id = atoi(pos + 7);
8712 
8713 		pos = os_strstr(params, "only_new=1");
8714 		if (pos)
8715 			manual_scan_only_new = 1;
8716 
8717 		pos = os_strstr(params, "scan_id=");
8718 		if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
8719 					      scan_id) < 0) {
8720 			*reply_len = -1;
8721 			goto done;
8722 		}
8723 
8724 		pos = os_strstr(params, "bssid=");
8725 		if (pos) {
8726 			u8 bssid[ETH_ALEN];
8727 
8728 			pos += 6;
8729 			if (hwaddr_aton(pos, bssid)) {
8730 				wpa_printf(MSG_ERROR, "Invalid BSSID %s", pos);
8731 				*reply_len = -1;
8732 				goto done;
8733 			}
8734 			os_memcpy(wpa_s->next_scan_bssid, bssid, ETH_ALEN);
8735 
8736 			wpa_s->next_scan_bssid_wildcard_ssid =
8737 				os_strstr(params, "wildcard_ssid=1") != NULL;
8738 		}
8739 
8740 		pos = params;
8741 		while (pos && *pos != '\0') {
8742 			if (os_strncmp(pos, "ssid ", 5) == 0) {
8743 				char *end;
8744 
8745 				pos += 5;
8746 				end = pos;
8747 				while (*end) {
8748 					if (*end == '\0' || *end == ' ')
8749 						break;
8750 					end++;
8751 				}
8752 
8753 				ns = os_realloc_array(
8754 					ssid, ssid_count + 1,
8755 					sizeof(struct wpa_ssid_value));
8756 				if (ns == NULL) {
8757 					*reply_len = -1;
8758 					goto done;
8759 				}
8760 				ssid = ns;
8761 
8762 				if ((end - pos) & 0x01 ||
8763 				    end - pos > 2 * SSID_MAX_LEN ||
8764 				    hexstr2bin(pos, ssid[ssid_count].ssid,
8765 					       (end - pos) / 2) < 0) {
8766 					wpa_printf(MSG_DEBUG,
8767 						   "Invalid SSID value '%s'",
8768 						   pos);
8769 					*reply_len = -1;
8770 					goto done;
8771 				}
8772 				ssid[ssid_count].ssid_len = (end - pos) / 2;
8773 				wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
8774 						  ssid[ssid_count].ssid,
8775 						  ssid[ssid_count].ssid_len);
8776 				ssid_count++;
8777 				pos = end;
8778 			}
8779 
8780 			pos = os_strchr(pos, ' ');
8781 			if (pos)
8782 				pos++;
8783 		}
8784 	}
8785 
8786 	wpa_s->num_ssids_from_scan_req = ssid_count;
8787 	os_free(wpa_s->ssids_from_scan_req);
8788 	if (ssid_count) {
8789 		wpa_s->ssids_from_scan_req = ssid;
8790 		ssid = NULL;
8791 	} else {
8792 		wpa_s->ssids_from_scan_req = NULL;
8793 	}
8794 
8795 	if (scan_only)
8796 		scan_res_handler = scan_only_handler;
8797 	else if (wpa_s->scan_res_handler == scan_only_handler)
8798 		scan_res_handler = NULL;
8799 	else
8800 		scan_res_handler = wpa_s->scan_res_handler;
8801 
8802 	if (!wpa_s->sched_scanning && !wpa_s->scanning &&
8803 	    ((wpa_s->wpa_state <= WPA_SCANNING) ||
8804 	     (wpa_s->wpa_state == WPA_COMPLETED))) {
8805 		wpa_s->manual_scan_passive = manual_scan_passive;
8806 		wpa_s->manual_scan_use_id = manual_scan_use_id;
8807 		wpa_s->manual_scan_only_new = manual_scan_only_new;
8808 		wpa_s->scan_id_count = scan_id_count;
8809 		os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
8810 		wpa_s->scan_res_handler = scan_res_handler;
8811 		os_free(wpa_s->manual_scan_freqs);
8812 		wpa_s->manual_scan_freqs = manual_scan_freqs;
8813 		manual_scan_freqs = NULL;
8814 
8815 		wpa_s->normal_scans = 0;
8816 		wpa_s->scan_req = MANUAL_SCAN_REQ;
8817 		wpa_s->after_wps = 0;
8818 		wpa_s->known_wps_freq = 0;
8819 		wpa_supplicant_req_scan(wpa_s, 0, 0);
8820 		if (wpa_s->manual_scan_use_id) {
8821 			wpa_s->manual_scan_id++;
8822 			wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
8823 				wpa_s->manual_scan_id);
8824 			*reply_len = os_snprintf(reply, reply_size, "%u\n",
8825 						 wpa_s->manual_scan_id);
8826 		}
8827 	} else if (wpa_s->sched_scanning) {
8828 		wpa_s->manual_scan_passive = manual_scan_passive;
8829 		wpa_s->manual_scan_use_id = manual_scan_use_id;
8830 		wpa_s->manual_scan_only_new = manual_scan_only_new;
8831 		wpa_s->scan_id_count = scan_id_count;
8832 		os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
8833 		wpa_s->scan_res_handler = scan_res_handler;
8834 		os_free(wpa_s->manual_scan_freqs);
8835 		wpa_s->manual_scan_freqs = manual_scan_freqs;
8836 		manual_scan_freqs = NULL;
8837 
8838 		wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
8839 		wpa_supplicant_cancel_sched_scan(wpa_s);
8840 		wpa_s->scan_req = MANUAL_SCAN_REQ;
8841 		wpa_supplicant_req_scan(wpa_s, 0, 0);
8842 		if (wpa_s->manual_scan_use_id) {
8843 			wpa_s->manual_scan_id++;
8844 			*reply_len = os_snprintf(reply, reply_size, "%u\n",
8845 						 wpa_s->manual_scan_id);
8846 			wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
8847 				wpa_s->manual_scan_id);
8848 		}
8849 	} else {
8850 		wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
8851 		*reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
8852 	}
8853 
8854 done:
8855 	os_free(manual_scan_freqs);
8856 	os_free(ssid);
8857 }
8858 
8859 
8860 #ifdef CONFIG_TESTING_OPTIONS
8861 
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)8862 static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
8863 				       unsigned int freq, const u8 *dst,
8864 				       const u8 *src, const u8 *bssid,
8865 				       const u8 *data, size_t data_len,
8866 				       enum offchannel_send_action_result
8867 				       result)
8868 {
8869 	wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
8870 		" src=" MACSTR " bssid=" MACSTR " result=%s",
8871 		freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
8872 		result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
8873 		"SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
8874 			     "NO_ACK" : "FAILED"));
8875 }
8876 
8877 
wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant * wpa_s,char * cmd)8878 static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
8879 {
8880 	char *pos, *param;
8881 	size_t len;
8882 	u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
8883 	int res, used;
8884 	int freq = 0, no_cck = 0, wait_time = 0;
8885 
8886 	/* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
8887 	 *    <action=Action frame payload> */
8888 
8889 	wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
8890 
8891 	pos = cmd;
8892 	used = hwaddr_aton2(pos, da);
8893 	if (used < 0)
8894 		return -1;
8895 	pos += used;
8896 	while (*pos == ' ')
8897 		pos++;
8898 	used = hwaddr_aton2(pos, bssid);
8899 	if (used < 0)
8900 		return -1;
8901 	pos += used;
8902 
8903 	param = os_strstr(pos, " freq=");
8904 	if (param) {
8905 		param += 6;
8906 		freq = atoi(param);
8907 	}
8908 
8909 	param = os_strstr(pos, " no_cck=");
8910 	if (param) {
8911 		param += 8;
8912 		no_cck = atoi(param);
8913 	}
8914 
8915 	param = os_strstr(pos, " wait_time=");
8916 	if (param) {
8917 		param += 11;
8918 		wait_time = atoi(param);
8919 	}
8920 
8921 	param = os_strstr(pos, " action=");
8922 	if (param == NULL)
8923 		return -1;
8924 	param += 8;
8925 
8926 	len = os_strlen(param);
8927 	if (len & 1)
8928 		return -1;
8929 	len /= 2;
8930 
8931 	buf = os_malloc(len);
8932 	if (buf == NULL)
8933 		return -1;
8934 
8935 	if (hexstr2bin(param, buf, len) < 0) {
8936 		os_free(buf);
8937 		return -1;
8938 	}
8939 
8940 	res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
8941 				     buf, len, wait_time,
8942 				     wpas_ctrl_iface_mgmt_tx_cb, no_cck);
8943 	os_free(buf);
8944 	return res;
8945 }
8946 
8947 
wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant * wpa_s)8948 static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
8949 {
8950 	wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
8951 	offchannel_send_action_done(wpa_s);
8952 }
8953 
8954 
wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant * wpa_s,char * cmd)8955 static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
8956 					   char *cmd)
8957 {
8958 	char *pos, *param;
8959 	size_t len;
8960 	u8 *buf;
8961 	int freq = 0, datarate = 0, ssi_signal = 0;
8962 	union wpa_event_data event;
8963 
8964 	if (!wpa_s->ext_mgmt_frame_handling)
8965 		return -1;
8966 
8967 	/* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
8968 
8969 	wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
8970 
8971 	pos = cmd;
8972 	param = os_strstr(pos, "freq=");
8973 	if (param) {
8974 		param += 5;
8975 		freq = atoi(param);
8976 	}
8977 
8978 	param = os_strstr(pos, " datarate=");
8979 	if (param) {
8980 		param += 10;
8981 		datarate = atoi(param);
8982 	}
8983 
8984 	param = os_strstr(pos, " ssi_signal=");
8985 	if (param) {
8986 		param += 12;
8987 		ssi_signal = atoi(param);
8988 	}
8989 
8990 	param = os_strstr(pos, " frame=");
8991 	if (param == NULL)
8992 		return -1;
8993 	param += 7;
8994 
8995 	len = os_strlen(param);
8996 	if (len & 1)
8997 		return -1;
8998 	len /= 2;
8999 
9000 	buf = os_malloc(len);
9001 	if (buf == NULL)
9002 		return -1;
9003 
9004 	if (hexstr2bin(param, buf, len) < 0) {
9005 		os_free(buf);
9006 		return -1;
9007 	}
9008 
9009 	os_memset(&event, 0, sizeof(event));
9010 	event.rx_mgmt.freq = freq;
9011 	event.rx_mgmt.frame = buf;
9012 	event.rx_mgmt.frame_len = len;
9013 	event.rx_mgmt.ssi_signal = ssi_signal;
9014 	event.rx_mgmt.datarate = datarate;
9015 	wpa_s->ext_mgmt_frame_handling = 0;
9016 	wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
9017 	wpa_s->ext_mgmt_frame_handling = 1;
9018 
9019 	os_free(buf);
9020 
9021 	return 0;
9022 }
9023 
9024 
wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant * wpa_s,char * param)9025 static int wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant *wpa_s,
9026 					   char *param)
9027 {
9028 	struct wpa_scan_res *res;
9029 	struct os_reltime now;
9030 	char *pos, *end;
9031 	int ret = -1;
9032 
9033 	if (!param)
9034 		return -1;
9035 
9036 	if (os_strcmp(param, "START") == 0) {
9037 		wpa_bss_update_start(wpa_s);
9038 		return 0;
9039 	}
9040 
9041 	if (os_strcmp(param, "END") == 0) {
9042 		wpa_bss_update_end(wpa_s, NULL, 1);
9043 		return 0;
9044 	}
9045 
9046 	if (os_strncmp(param, "BSS ", 4) != 0)
9047 		return -1;
9048 	param += 3;
9049 
9050 	res = os_zalloc(sizeof(*res) + os_strlen(param) / 2);
9051 	if (!res)
9052 		return -1;
9053 
9054 	pos = os_strstr(param, " flags=");
9055 	if (pos)
9056 		res->flags = strtol(pos + 7, NULL, 16);
9057 
9058 	pos = os_strstr(param, " bssid=");
9059 	if (pos && hwaddr_aton(pos + 7, res->bssid))
9060 		goto fail;
9061 
9062 	pos = os_strstr(param, " freq=");
9063 	if (pos)
9064 		res->freq = atoi(pos + 6);
9065 
9066 	pos = os_strstr(param, " beacon_int=");
9067 	if (pos)
9068 		res->beacon_int = atoi(pos + 12);
9069 
9070 	pos = os_strstr(param, " caps=");
9071 	if (pos)
9072 		res->caps = strtol(pos + 6, NULL, 16);
9073 
9074 	pos = os_strstr(param, " qual=");
9075 	if (pos)
9076 		res->qual = atoi(pos + 6);
9077 
9078 	pos = os_strstr(param, " noise=");
9079 	if (pos)
9080 		res->noise = atoi(pos + 7);
9081 
9082 	pos = os_strstr(param, " level=");
9083 	if (pos)
9084 		res->level = atoi(pos + 7);
9085 
9086 	pos = os_strstr(param, " tsf=");
9087 	if (pos)
9088 		res->tsf = strtoll(pos + 5, NULL, 16);
9089 
9090 	pos = os_strstr(param, " age=");
9091 	if (pos)
9092 		res->age = atoi(pos + 5);
9093 
9094 	pos = os_strstr(param, " est_throughput=");
9095 	if (pos)
9096 		res->est_throughput = atoi(pos + 16);
9097 
9098 	pos = os_strstr(param, " snr=");
9099 	if (pos)
9100 		res->snr = atoi(pos + 5);
9101 
9102 	pos = os_strstr(param, " parent_tsf=");
9103 	if (pos)
9104 		res->parent_tsf = strtoll(pos + 7, NULL, 16);
9105 
9106 	pos = os_strstr(param, " tsf_bssid=");
9107 	if (pos && hwaddr_aton(pos + 11, res->tsf_bssid))
9108 		goto fail;
9109 
9110 	pos = os_strstr(param, " ie=");
9111 	if (pos) {
9112 		pos += 4;
9113 		end = os_strchr(pos, ' ');
9114 		if (!end)
9115 			end = pos + os_strlen(pos);
9116 		res->ie_len = (end - pos) / 2;
9117 		if (hexstr2bin(pos, (u8 *) (res + 1), res->ie_len))
9118 			goto fail;
9119 	}
9120 
9121 	pos = os_strstr(param, " beacon_ie=");
9122 	if (pos) {
9123 		pos += 11;
9124 		end = os_strchr(pos, ' ');
9125 		if (!end)
9126 			end = pos + os_strlen(pos);
9127 		res->beacon_ie_len = (end - pos) / 2;
9128 		if (hexstr2bin(pos, ((u8 *) (res + 1)) + res->ie_len,
9129 			       res->beacon_ie_len))
9130 			goto fail;
9131 	}
9132 
9133 	os_get_reltime(&now);
9134 	wpa_bss_update_scan_res(wpa_s, res, &now);
9135 	ret = 0;
9136 fail:
9137 	os_free(res);
9138 
9139 	return ret;
9140 }
9141 
9142 
wpas_ctrl_iface_driver_event(struct wpa_supplicant * wpa_s,char * cmd)9143 static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
9144 {
9145 	char *pos, *param;
9146 	union wpa_event_data event;
9147 	enum wpa_event_type ev;
9148 
9149 	/* <event name> [parameters..] */
9150 
9151 	wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
9152 
9153 	pos = cmd;
9154 	param = os_strchr(pos, ' ');
9155 	if (param)
9156 		*param++ = '\0';
9157 
9158 	os_memset(&event, 0, sizeof(event));
9159 
9160 	if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
9161 		ev = EVENT_INTERFACE_ENABLED;
9162 	} else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
9163 		ev = EVENT_INTERFACE_DISABLED;
9164 	} else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
9165 		ev = EVENT_AVOID_FREQUENCIES;
9166 		if (param == NULL)
9167 			param = "";
9168 		if (freq_range_list_parse(&event.freq_range, param) < 0)
9169 			return -1;
9170 		wpa_supplicant_event(wpa_s, ev, &event);
9171 		os_free(event.freq_range.range);
9172 		return 0;
9173 	} else if (os_strcmp(cmd, "SCAN_RES") == 0) {
9174 		return wpas_ctrl_iface_driver_scan_res(wpa_s, param);
9175 	} else {
9176 		wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
9177 			cmd);
9178 		return -1;
9179 	}
9180 
9181 	wpa_supplicant_event(wpa_s, ev, &event);
9182 
9183 	return 0;
9184 }
9185 
9186 
wpas_ctrl_iface_eapol_rx(struct wpa_supplicant * wpa_s,char * cmd)9187 static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
9188 {
9189 	char *pos;
9190 	u8 src[ETH_ALEN], *buf;
9191 	int used;
9192 	size_t len;
9193 
9194 	wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
9195 
9196 	pos = cmd;
9197 	used = hwaddr_aton2(pos, src);
9198 	if (used < 0)
9199 		return -1;
9200 	pos += used;
9201 	while (*pos == ' ')
9202 		pos++;
9203 
9204 	len = os_strlen(pos);
9205 	if (len & 1)
9206 		return -1;
9207 	len /= 2;
9208 
9209 	buf = os_malloc(len);
9210 	if (buf == NULL)
9211 		return -1;
9212 
9213 	if (hexstr2bin(pos, buf, len) < 0) {
9214 		os_free(buf);
9215 		return -1;
9216 	}
9217 
9218 	wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
9219 	os_free(buf);
9220 
9221 	return 0;
9222 }
9223 
9224 
ipv4_hdr_checksum(const void * buf,size_t len)9225 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
9226 {
9227 	size_t i;
9228 	u32 sum = 0;
9229 	const u16 *pos = buf;
9230 
9231 	for (i = 0; i < len / 2; i++)
9232 		sum += *pos++;
9233 
9234 	while (sum >> 16)
9235 		sum = (sum & 0xffff) + (sum >> 16);
9236 
9237 	return sum ^ 0xffff;
9238 }
9239 
9240 
9241 #define HWSIM_PACKETLEN 1500
9242 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
9243 
wpas_data_test_rx(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)9244 static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
9245 			      size_t len)
9246 {
9247 	struct wpa_supplicant *wpa_s = ctx;
9248 	const struct ether_header *eth;
9249 	struct ip ip;
9250 	const u8 *pos;
9251 	unsigned int i;
9252 	char extra[30];
9253 
9254 	if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
9255 		wpa_printf(MSG_DEBUG,
9256 			   "test data: RX - ignore unexpected length %d",
9257 			   (int) len);
9258 		return;
9259 	}
9260 
9261 	eth = (const struct ether_header *) buf;
9262 	os_memcpy(&ip, eth + 1, sizeof(ip));
9263 	pos = &buf[sizeof(*eth) + sizeof(ip)];
9264 
9265 	if (ip.ip_hl != 5 || ip.ip_v != 4 || ntohs(ip.ip_len) > HWSIM_IP_LEN) {
9266 		wpa_printf(MSG_DEBUG,
9267 			   "test data: RX - ignore unexpect IP header");
9268 		return;
9269 	}
9270 
9271 	for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
9272 		if (*pos != (u8) i) {
9273 			wpa_printf(MSG_DEBUG,
9274 				   "test data: RX - ignore mismatching payload");
9275 			return;
9276 		}
9277 		pos++;
9278 	}
9279 	extra[0] = '\0';
9280 	if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
9281 		os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
9282 	wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
9283 		MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
9284 }
9285 
9286 
wpas_ctrl_iface_data_test_config(struct wpa_supplicant * wpa_s,char * cmd)9287 static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
9288 					    char *cmd)
9289 {
9290 	int enabled = atoi(cmd);
9291 	char *pos;
9292 	const char *ifname;
9293 
9294 	if (!enabled) {
9295 		if (wpa_s->l2_test) {
9296 			l2_packet_deinit(wpa_s->l2_test);
9297 			wpa_s->l2_test = NULL;
9298 			wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
9299 		}
9300 		return 0;
9301 	}
9302 
9303 	if (wpa_s->l2_test)
9304 		return 0;
9305 
9306 	pos = os_strstr(cmd, " ifname=");
9307 	if (pos)
9308 		ifname = pos + 8;
9309 	else
9310 		ifname = wpa_s->ifname;
9311 
9312 	wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
9313 					ETHERTYPE_IP, wpas_data_test_rx,
9314 					wpa_s, 1);
9315 	if (wpa_s->l2_test == NULL)
9316 		return -1;
9317 
9318 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
9319 
9320 	return 0;
9321 }
9322 
9323 
wpas_ctrl_iface_data_test_tx(struct wpa_supplicant * wpa_s,char * cmd)9324 static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
9325 {
9326 	u8 dst[ETH_ALEN], src[ETH_ALEN];
9327 	char *pos, *pos2;
9328 	int used;
9329 	long int val;
9330 	u8 tos;
9331 	u8 buf[2 + HWSIM_PACKETLEN];
9332 	struct ether_header *eth;
9333 	struct ip *ip;
9334 	u8 *dpos;
9335 	unsigned int i;
9336 	size_t send_len = HWSIM_IP_LEN;
9337 
9338 	if (wpa_s->l2_test == NULL)
9339 		return -1;
9340 
9341 	/* format: <dst> <src> <tos> [len=<length>] */
9342 
9343 	pos = cmd;
9344 	used = hwaddr_aton2(pos, dst);
9345 	if (used < 0)
9346 		return -1;
9347 	pos += used;
9348 	while (*pos == ' ')
9349 		pos++;
9350 	used = hwaddr_aton2(pos, src);
9351 	if (used < 0)
9352 		return -1;
9353 	pos += used;
9354 
9355 	val = strtol(pos, &pos2, 0);
9356 	if (val < 0 || val > 0xff)
9357 		return -1;
9358 	tos = val;
9359 
9360 	pos = os_strstr(pos2, " len=");
9361 	if (pos) {
9362 		i = atoi(pos + 5);
9363 		if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
9364 			return -1;
9365 		send_len = i;
9366 	}
9367 
9368 	eth = (struct ether_header *) &buf[2];
9369 	os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
9370 	os_memcpy(eth->ether_shost, src, ETH_ALEN);
9371 	eth->ether_type = htons(ETHERTYPE_IP);
9372 	ip = (struct ip *) (eth + 1);
9373 	os_memset(ip, 0, sizeof(*ip));
9374 	ip->ip_hl = 5;
9375 	ip->ip_v = 4;
9376 	ip->ip_ttl = 64;
9377 	ip->ip_tos = tos;
9378 	ip->ip_len = htons(send_len);
9379 	ip->ip_p = 1;
9380 	ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
9381 	ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
9382 	ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
9383 	dpos = (u8 *) (ip + 1);
9384 	for (i = 0; i < send_len - sizeof(*ip); i++)
9385 		*dpos++ = i;
9386 
9387 	if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
9388 			   sizeof(struct ether_header) + send_len) < 0)
9389 		return -1;
9390 
9391 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
9392 		" tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
9393 
9394 	return 0;
9395 }
9396 
9397 
wpas_ctrl_iface_data_test_frame(struct wpa_supplicant * wpa_s,char * cmd)9398 static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
9399 					   char *cmd)
9400 {
9401 	u8 *buf;
9402 	struct ether_header *eth;
9403 	struct l2_packet_data *l2 = NULL;
9404 	size_t len;
9405 	u16 ethertype;
9406 	int res = -1;
9407 
9408 	len = os_strlen(cmd);
9409 	if (len & 1 || len < ETH_HLEN * 2)
9410 		return -1;
9411 	len /= 2;
9412 
9413 	buf = os_malloc(len);
9414 	if (buf == NULL)
9415 		return -1;
9416 
9417 	if (hexstr2bin(cmd, buf, len) < 0)
9418 		goto done;
9419 
9420 	eth = (struct ether_header *) buf;
9421 	ethertype = ntohs(eth->ether_type);
9422 
9423 	l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
9424 			    wpas_data_test_rx, wpa_s, 1);
9425 	if (l2 == NULL)
9426 		goto done;
9427 
9428 	res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
9429 	wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
9430 done:
9431 	if (l2)
9432 		l2_packet_deinit(l2);
9433 	os_free(buf);
9434 
9435 	return res < 0 ? -1 : 0;
9436 }
9437 
9438 
wpas_ctrl_test_alloc_fail(struct wpa_supplicant * wpa_s,char * cmd)9439 static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
9440 {
9441 #ifdef WPA_TRACE_BFD
9442 	char *pos;
9443 
9444 	wpa_trace_fail_after = atoi(cmd);
9445 	pos = os_strchr(cmd, ':');
9446 	if (pos) {
9447 		pos++;
9448 		os_strlcpy(wpa_trace_fail_func, pos,
9449 			   sizeof(wpa_trace_fail_func));
9450 	} else {
9451 		wpa_trace_fail_after = 0;
9452 	}
9453 	return 0;
9454 #else /* WPA_TRACE_BFD */
9455 	return -1;
9456 #endif /* WPA_TRACE_BFD */
9457 }
9458 
9459 
wpas_ctrl_get_alloc_fail(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)9460 static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
9461 				    char *buf, size_t buflen)
9462 {
9463 #ifdef WPA_TRACE_BFD
9464 	return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
9465 			   wpa_trace_fail_func);
9466 #else /* WPA_TRACE_BFD */
9467 	return -1;
9468 #endif /* WPA_TRACE_BFD */
9469 }
9470 
9471 
wpas_ctrl_test_fail(struct wpa_supplicant * wpa_s,char * cmd)9472 static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd)
9473 {
9474 #ifdef WPA_TRACE_BFD
9475 	char *pos;
9476 
9477 	wpa_trace_test_fail_after = atoi(cmd);
9478 	pos = os_strchr(cmd, ':');
9479 	if (pos) {
9480 		pos++;
9481 		os_strlcpy(wpa_trace_test_fail_func, pos,
9482 			   sizeof(wpa_trace_test_fail_func));
9483 	} else {
9484 		wpa_trace_test_fail_after = 0;
9485 	}
9486 	return 0;
9487 #else /* WPA_TRACE_BFD */
9488 	return -1;
9489 #endif /* WPA_TRACE_BFD */
9490 }
9491 
9492 
wpas_ctrl_get_fail(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)9493 static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s,
9494 				    char *buf, size_t buflen)
9495 {
9496 #ifdef WPA_TRACE_BFD
9497 	return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
9498 			   wpa_trace_test_fail_func);
9499 #else /* WPA_TRACE_BFD */
9500 	return -1;
9501 #endif /* WPA_TRACE_BFD */
9502 }
9503 
9504 
wpas_ctrl_event_test_cb(void * eloop_ctx,void * timeout_ctx)9505 static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
9506 {
9507 	struct wpa_supplicant *wpa_s = eloop_ctx;
9508 	int i, count = (intptr_t) timeout_ctx;
9509 
9510 	wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
9511 		   count);
9512 	for (i = 0; i < count; i++) {
9513 		wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
9514 			     i + 1, count);
9515 	}
9516 }
9517 
9518 
wpas_ctrl_event_test(struct wpa_supplicant * wpa_s,const char * cmd)9519 static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
9520 {
9521 	int count;
9522 
9523 	count = atoi(cmd);
9524 	if (count <= 0)
9525 		return -1;
9526 
9527 	return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
9528 				      (void *) (intptr_t) count);
9529 }
9530 
9531 
wpas_ctrl_test_assoc_ie(struct wpa_supplicant * wpa_s,const char * cmd)9532 static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
9533 				   const char *cmd)
9534 {
9535 	struct wpabuf *buf;
9536 	size_t len;
9537 
9538 	len = os_strlen(cmd);
9539 	if (len & 1)
9540 		return -1;
9541 	len /= 2;
9542 
9543 	if (len == 0) {
9544 		buf = NULL;
9545 	} else {
9546 		buf = wpabuf_alloc(len);
9547 		if (buf == NULL)
9548 			return -1;
9549 
9550 		if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
9551 			wpabuf_free(buf);
9552 			return -1;
9553 		}
9554 	}
9555 
9556 	wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
9557 	return 0;
9558 }
9559 
9560 
wpas_ctrl_reset_pn(struct wpa_supplicant * wpa_s)9561 static int wpas_ctrl_reset_pn(struct wpa_supplicant *wpa_s)
9562 {
9563 	u8 zero[WPA_TK_MAX_LEN];
9564 
9565 	if (wpa_s->last_tk_alg == WPA_ALG_NONE)
9566 		return -1;
9567 
9568 	wpa_printf(MSG_INFO, "TESTING: Reset PN");
9569 	os_memset(zero, 0, sizeof(zero));
9570 
9571 	/* First, use a zero key to avoid any possible duplicate key avoidance
9572 	 * in the driver. */
9573 	if (wpa_drv_set_key(wpa_s, wpa_s->last_tk_alg, wpa_s->last_tk_addr,
9574 			    wpa_s->last_tk_key_idx, 1, zero, 6,
9575 			    zero, wpa_s->last_tk_len,
9576 			    KEY_FLAG_PAIRWISE_RX_TX) < 0)
9577 		return -1;
9578 
9579 	/* Set the previously configured key to reset its TSC/RSC */
9580 	return wpa_drv_set_key(wpa_s, wpa_s->last_tk_alg, wpa_s->last_tk_addr,
9581 			       wpa_s->last_tk_key_idx, 1, zero, 6,
9582 			       wpa_s->last_tk, wpa_s->last_tk_len,
9583 			       KEY_FLAG_PAIRWISE_RX_TX);
9584 }
9585 
9586 
wpas_ctrl_key_request(struct wpa_supplicant * wpa_s,const char * cmd)9587 static int wpas_ctrl_key_request(struct wpa_supplicant *wpa_s, const char *cmd)
9588 {
9589 	const char *pos = cmd;
9590 	int error, pairwise;
9591 
9592 	error = atoi(pos);
9593 	pos = os_strchr(pos, ' ');
9594 	if (!pos)
9595 		return -1;
9596 	pairwise = atoi(pos);
9597 	wpa_sm_key_request(wpa_s->wpa, error, pairwise);
9598 	return 0;
9599 }
9600 
9601 
wpas_ctrl_resend_assoc(struct wpa_supplicant * wpa_s)9602 static int wpas_ctrl_resend_assoc(struct wpa_supplicant *wpa_s)
9603 {
9604 #ifdef CONFIG_SME
9605 	struct wpa_driver_associate_params params;
9606 	int ret;
9607 
9608 	os_memset(&params, 0, sizeof(params));
9609 	params.bssid = wpa_s->bssid;
9610 	params.ssid = wpa_s->sme.ssid;
9611 	params.ssid_len = wpa_s->sme.ssid_len;
9612 	params.freq.freq = wpa_s->sme.freq;
9613 	if (wpa_s->last_assoc_req_wpa_ie) {
9614 		params.wpa_ie = wpabuf_head(wpa_s->last_assoc_req_wpa_ie);
9615 		params.wpa_ie_len = wpabuf_len(wpa_s->last_assoc_req_wpa_ie);
9616 	}
9617 	params.pairwise_suite = wpa_s->pairwise_cipher;
9618 	params.group_suite = wpa_s->group_cipher;
9619 	params.mgmt_group_suite = wpa_s->mgmt_group_cipher;
9620 	params.key_mgmt_suite = wpa_s->key_mgmt;
9621 	params.wpa_proto = wpa_s->wpa_proto;
9622 	params.mgmt_frame_protection = wpa_s->sme.mfp;
9623 	params.rrm_used = wpa_s->rrm.rrm_used;
9624 	if (wpa_s->sme.prev_bssid_set)
9625 		params.prev_bssid = wpa_s->sme.prev_bssid;
9626 	wpa_printf(MSG_INFO, "TESTING: Resend association request");
9627 	ret = wpa_drv_associate(wpa_s, &params);
9628 	wpa_s->testing_resend_assoc = 1;
9629 	return ret;
9630 #else /* CONFIG_SME */
9631 	return -1;
9632 #endif /* CONFIG_SME */
9633 }
9634 
9635 #endif /* CONFIG_TESTING_OPTIONS */
9636 
9637 
wpas_ctrl_vendor_elem_add(struct wpa_supplicant * wpa_s,char * cmd)9638 static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
9639 {
9640 	char *pos = cmd;
9641 	int frame;
9642 	size_t len;
9643 	struct wpabuf *buf;
9644 	struct ieee802_11_elems elems;
9645 
9646 	frame = atoi(pos);
9647 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
9648 		return -1;
9649 	wpa_s = wpas_vendor_elem(wpa_s, frame);
9650 
9651 	pos = os_strchr(pos, ' ');
9652 	if (pos == NULL)
9653 		return -1;
9654 	pos++;
9655 
9656 	len = os_strlen(pos);
9657 	if (len == 0)
9658 		return 0;
9659 	if (len & 1)
9660 		return -1;
9661 	len /= 2;
9662 
9663 	buf = wpabuf_alloc(len);
9664 	if (buf == NULL)
9665 		return -1;
9666 
9667 	if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
9668 		wpabuf_free(buf);
9669 		return -1;
9670 	}
9671 
9672 	if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
9673 	    ParseFailed) {
9674 		wpabuf_free(buf);
9675 		return -1;
9676 	}
9677 
9678 	if (wpa_s->vendor_elem[frame] == NULL) {
9679 		wpa_s->vendor_elem[frame] = buf;
9680 		wpas_vendor_elem_update(wpa_s);
9681 		return 0;
9682 	}
9683 
9684 	if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
9685 		wpabuf_free(buf);
9686 		return -1;
9687 	}
9688 
9689 	wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
9690 	wpabuf_free(buf);
9691 	wpas_vendor_elem_update(wpa_s);
9692 
9693 	return 0;
9694 }
9695 
9696 
wpas_ctrl_vendor_elem_get(struct wpa_supplicant * wpa_s,char * cmd,char * buf,size_t buflen)9697 static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
9698 				     char *buf, size_t buflen)
9699 {
9700 	int frame = atoi(cmd);
9701 
9702 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
9703 		return -1;
9704 	wpa_s = wpas_vendor_elem(wpa_s, frame);
9705 
9706 	if (wpa_s->vendor_elem[frame] == NULL)
9707 		return 0;
9708 
9709 	return wpa_snprintf_hex(buf, buflen,
9710 				wpabuf_head_u8(wpa_s->vendor_elem[frame]),
9711 				wpabuf_len(wpa_s->vendor_elem[frame]));
9712 }
9713 
9714 
wpas_ctrl_vendor_elem_remove(struct wpa_supplicant * wpa_s,char * cmd)9715 static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
9716 {
9717 	char *pos = cmd;
9718 	int frame;
9719 	size_t len;
9720 	u8 *buf;
9721 	struct ieee802_11_elems elems;
9722 	int res;
9723 
9724 	frame = atoi(pos);
9725 	if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
9726 		return -1;
9727 	wpa_s = wpas_vendor_elem(wpa_s, frame);
9728 
9729 	pos = os_strchr(pos, ' ');
9730 	if (pos == NULL)
9731 		return -1;
9732 	pos++;
9733 
9734 	if (*pos == '*') {
9735 		wpabuf_free(wpa_s->vendor_elem[frame]);
9736 		wpa_s->vendor_elem[frame] = NULL;
9737 		wpas_vendor_elem_update(wpa_s);
9738 		return 0;
9739 	}
9740 
9741 	if (wpa_s->vendor_elem[frame] == NULL)
9742 		return -1;
9743 
9744 	len = os_strlen(pos);
9745 	if (len == 0)
9746 		return 0;
9747 	if (len & 1)
9748 		return -1;
9749 	len /= 2;
9750 
9751 	buf = os_malloc(len);
9752 	if (buf == NULL)
9753 		return -1;
9754 
9755 	if (hexstr2bin(pos, buf, len) < 0) {
9756 		os_free(buf);
9757 		return -1;
9758 	}
9759 
9760 	if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
9761 		os_free(buf);
9762 		return -1;
9763 	}
9764 
9765 	res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
9766 	os_free(buf);
9767 	return res;
9768 }
9769 
9770 
wpas_ctrl_neighbor_rep_cb(void * ctx,struct wpabuf * neighbor_rep)9771 static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
9772 {
9773 	struct wpa_supplicant *wpa_s = ctx;
9774 	size_t len;
9775 	const u8 *data;
9776 
9777 	/*
9778 	 * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
9779 	 * BSSID[6]
9780 	 * BSSID Information[4]
9781 	 * Operating Class[1]
9782 	 * Channel Number[1]
9783 	 * PHY Type[1]
9784 	 * Optional Subelements[variable]
9785 	 */
9786 #define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
9787 
9788 	if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
9789 		wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
9790 		goto out;
9791 	}
9792 
9793 	data = wpabuf_head_u8(neighbor_rep);
9794 	len = wpabuf_len(neighbor_rep);
9795 
9796 	while (len >= 2 + NR_IE_MIN_LEN) {
9797 		const u8 *nr;
9798 		char lci[256 * 2 + 1];
9799 		char civic[256 * 2 + 1];
9800 		u8 nr_len = data[1];
9801 		const u8 *pos = data, *end;
9802 
9803 		if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
9804 		    nr_len < NR_IE_MIN_LEN) {
9805 			wpa_dbg(wpa_s, MSG_DEBUG,
9806 				"CTRL: Invalid Neighbor Report element: id=%u len=%u",
9807 				data[0], nr_len);
9808 			goto out;
9809 		}
9810 
9811 		if (2U + nr_len > len) {
9812 			wpa_dbg(wpa_s, MSG_DEBUG,
9813 				"CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
9814 				data[0], len, nr_len);
9815 			goto out;
9816 		}
9817 		pos += 2;
9818 		end = pos + nr_len;
9819 
9820 		nr = pos;
9821 		pos += NR_IE_MIN_LEN;
9822 
9823 		lci[0] = '\0';
9824 		civic[0] = '\0';
9825 		while (end - pos > 2) {
9826 			u8 s_id, s_len;
9827 
9828 			s_id = *pos++;
9829 			s_len = *pos++;
9830 			if (s_len > end - pos)
9831 				goto out;
9832 			if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
9833 				/* Measurement Token[1] */
9834 				/* Measurement Report Mode[1] */
9835 				/* Measurement Type[1] */
9836 				/* Measurement Report[variable] */
9837 				switch (pos[2]) {
9838 				case MEASURE_TYPE_LCI:
9839 					if (lci[0])
9840 						break;
9841 					wpa_snprintf_hex(lci, sizeof(lci),
9842 							 pos, s_len);
9843 					break;
9844 				case MEASURE_TYPE_LOCATION_CIVIC:
9845 					if (civic[0])
9846 						break;
9847 					wpa_snprintf_hex(civic, sizeof(civic),
9848 							 pos, s_len);
9849 					break;
9850 				}
9851 			}
9852 
9853 			pos += s_len;
9854 		}
9855 
9856 		wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
9857 			"bssid=" MACSTR
9858 			" info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
9859 			MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
9860 			nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
9861 			nr[ETH_ALEN + 6],
9862 			lci[0] ? " lci=" : "", lci,
9863 			civic[0] ? " civic=" : "", civic);
9864 
9865 		data = end;
9866 		len -= 2 + nr_len;
9867 	}
9868 
9869 out:
9870 	wpabuf_free(neighbor_rep);
9871 }
9872 
9873 
wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant * wpa_s,char * cmd)9874 static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
9875 					     char *cmd)
9876 {
9877 	struct wpa_ssid_value ssid, *ssid_p = NULL;
9878 	int ret, lci = 0, civic = 0;
9879 	char *ssid_s;
9880 
9881 	ssid_s = os_strstr(cmd, "ssid=");
9882 	if (ssid_s) {
9883 		if (ssid_parse(ssid_s + 5, &ssid)) {
9884 			wpa_msg(wpa_s, MSG_INFO,
9885 				"CTRL: Send Neighbor Report: bad SSID");
9886 			return -1;
9887 		}
9888 
9889 		ssid_p = &ssid;
9890 
9891 		/*
9892 		 * Move cmd after the SSID text that may include "lci" or
9893 		 * "civic".
9894 		 */
9895 		cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
9896 		if (cmd)
9897 			cmd++;
9898 
9899 	}
9900 
9901 	if (cmd && os_strstr(cmd, "lci"))
9902 		lci = 1;
9903 
9904 	if (cmd && os_strstr(cmd, "civic"))
9905 		civic = 1;
9906 
9907 	ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
9908 						 wpas_ctrl_neighbor_rep_cb,
9909 						 wpa_s);
9910 
9911 	return ret;
9912 }
9913 
9914 
wpas_ctrl_iface_erp_flush(struct wpa_supplicant * wpa_s)9915 static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
9916 {
9917 	eapol_sm_erp_flush(wpa_s->eapol);
9918 	return 0;
9919 }
9920 
9921 
wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant * wpa_s,char * cmd)9922 static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
9923 					 char *cmd)
9924 {
9925 	char *token, *context = NULL;
9926 	unsigned int enable = ~0, type = 0;
9927 	u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
9928 	u8 *addr = NULL, *mask = NULL;
9929 
9930 	while ((token = str_token(cmd, " ", &context))) {
9931 		if (os_strcasecmp(token, "scan") == 0) {
9932 			type |= MAC_ADDR_RAND_SCAN;
9933 		} else if (os_strcasecmp(token, "sched") == 0) {
9934 			type |= MAC_ADDR_RAND_SCHED_SCAN;
9935 		} else if (os_strcasecmp(token, "pno") == 0) {
9936 			type |= MAC_ADDR_RAND_PNO;
9937 		} else if (os_strcasecmp(token, "all") == 0) {
9938 			type = wpa_s->mac_addr_rand_supported;
9939 		} else if (os_strncasecmp(token, "enable=", 7) == 0) {
9940 			enable = atoi(token + 7);
9941 		} else if (os_strncasecmp(token, "addr=", 5) == 0) {
9942 			addr = _addr;
9943 			if (hwaddr_aton(token + 5, addr)) {
9944 				wpa_printf(MSG_INFO,
9945 					   "CTRL: Invalid MAC address: %s",
9946 					   token);
9947 				return -1;
9948 			}
9949 		} else if (os_strncasecmp(token, "mask=", 5) == 0) {
9950 			mask = _mask;
9951 			if (hwaddr_aton(token + 5, mask)) {
9952 				wpa_printf(MSG_INFO,
9953 					   "CTRL: Invalid MAC address mask: %s",
9954 					   token);
9955 				return -1;
9956 			}
9957 		} else {
9958 			wpa_printf(MSG_INFO,
9959 				   "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
9960 				   token);
9961 			return -1;
9962 		}
9963 	}
9964 
9965 	if (!type) {
9966 		wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
9967 		return -1;
9968 	}
9969 
9970 	if (enable > 1) {
9971 		wpa_printf(MSG_INFO,
9972 			   "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
9973 		return -1;
9974 	}
9975 
9976 	if (!enable)
9977 		return wpas_disable_mac_addr_randomization(wpa_s, type);
9978 
9979 	return wpas_enable_mac_addr_randomization(wpa_s, type, addr, mask);
9980 }
9981 
9982 
wpas_ctrl_iface_pmksa(struct wpa_supplicant * wpa_s,char * buf,size_t buflen)9983 static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
9984 				 char *buf, size_t buflen)
9985 {
9986 	size_t reply_len;
9987 
9988 	reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
9989 #ifdef CONFIG_AP
9990 	reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
9991 					      buflen - reply_len);
9992 #endif /* CONFIG_AP */
9993 	return reply_len;
9994 }
9995 
9996 
wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant * wpa_s)9997 static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
9998 {
9999 	wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
10000 #ifdef CONFIG_AP
10001 	wpas_ap_pmksa_cache_flush(wpa_s);
10002 #endif /* CONFIG_AP */
10003 }
10004 
10005 
10006 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
10007 
wpas_ctrl_iface_pmksa_get(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)10008 static int wpas_ctrl_iface_pmksa_get(struct wpa_supplicant *wpa_s,
10009 				     const char *cmd, char *buf, size_t buflen)
10010 {
10011 	struct rsn_pmksa_cache_entry *entry;
10012 	struct wpa_ssid *ssid;
10013 	char *pos, *pos2, *end;
10014 	int ret;
10015 	struct os_reltime now;
10016 
10017 	ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
10018 	if (!ssid)
10019 		return -1;
10020 
10021 	pos = buf;
10022 	end = buf + buflen;
10023 
10024 	os_get_reltime(&now);
10025 
10026 	/*
10027 	 * Entry format:
10028 	 * <BSSID> <PMKID> <PMK> <reauth_time in seconds>
10029 	 * <expiration in seconds> <akmp> <opportunistic>
10030 	 * [FILS Cache Identifier]
10031 	 */
10032 
10033 	for (entry = wpa_sm_pmksa_cache_head(wpa_s->wpa); entry;
10034 	     entry = entry->next) {
10035 		if (entry->network_ctx != ssid)
10036 			continue;
10037 
10038 		pos2 = pos;
10039 		ret = os_snprintf(pos2, end - pos2, MACSTR " ",
10040 				  MAC2STR(entry->aa));
10041 		if (os_snprintf_error(end - pos2, ret))
10042 			break;
10043 		pos2 += ret;
10044 
10045 		pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmkid,
10046 					 PMKID_LEN);
10047 
10048 		ret = os_snprintf(pos2, end - pos2, " ");
10049 		if (os_snprintf_error(end - pos2, ret))
10050 			break;
10051 		pos2 += ret;
10052 
10053 		pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmk,
10054 					 entry->pmk_len);
10055 
10056 		ret = os_snprintf(pos2, end - pos2, " %d %d %d %d",
10057 				  (int) (entry->reauth_time - now.sec),
10058 				  (int) (entry->expiration - now.sec),
10059 				  entry->akmp,
10060 				  entry->opportunistic);
10061 		if (os_snprintf_error(end - pos2, ret))
10062 			break;
10063 		pos2 += ret;
10064 
10065 		if (entry->fils_cache_id_set) {
10066 			ret = os_snprintf(pos2, end - pos2, " %02x%02x",
10067 					  entry->fils_cache_id[0],
10068 					  entry->fils_cache_id[1]);
10069 			if (os_snprintf_error(end - pos2, ret))
10070 				break;
10071 			pos2 += ret;
10072 		}
10073 
10074 		ret = os_snprintf(pos2, end - pos2, "\n");
10075 		if (os_snprintf_error(end - pos2, ret))
10076 			break;
10077 		pos2 += ret;
10078 
10079 		pos = pos2;
10080 	}
10081 
10082 	return pos - buf;
10083 }
10084 
10085 
wpas_ctrl_iface_pmksa_add(struct wpa_supplicant * wpa_s,char * cmd)10086 static int wpas_ctrl_iface_pmksa_add(struct wpa_supplicant *wpa_s,
10087 				     char *cmd)
10088 {
10089 	struct rsn_pmksa_cache_entry *entry;
10090 	struct wpa_ssid *ssid;
10091 	char *pos, *pos2;
10092 	int ret = -1;
10093 	struct os_reltime now;
10094 	int reauth_time = 0, expiration = 0, i;
10095 
10096 	/*
10097 	 * Entry format:
10098 	 * <network_id> <BSSID> <PMKID> <PMK> <reauth_time in seconds>
10099 	 * <expiration in seconds> <akmp> <opportunistic>
10100 	 * [FILS Cache Identifier]
10101 	 */
10102 
10103 	ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
10104 	if (!ssid)
10105 		return -1;
10106 
10107 	pos = os_strchr(cmd, ' ');
10108 	if (!pos)
10109 		return -1;
10110 	pos++;
10111 
10112 	entry = os_zalloc(sizeof(*entry));
10113 	if (!entry)
10114 		return -1;
10115 
10116 	if (hwaddr_aton(pos, entry->aa))
10117 		goto fail;
10118 
10119 	pos = os_strchr(pos, ' ');
10120 	if (!pos)
10121 		goto fail;
10122 	pos++;
10123 
10124 	if (hexstr2bin(pos, entry->pmkid, PMKID_LEN) < 0)
10125 		goto fail;
10126 
10127 	pos = os_strchr(pos, ' ');
10128 	if (!pos)
10129 		goto fail;
10130 	pos++;
10131 
10132 	pos2 = os_strchr(pos, ' ');
10133 	if (!pos2)
10134 		goto fail;
10135 	entry->pmk_len = (pos2 - pos) / 2;
10136 	if (entry->pmk_len < PMK_LEN || entry->pmk_len > PMK_LEN_MAX ||
10137 	    hexstr2bin(pos, entry->pmk, entry->pmk_len) < 0)
10138 		goto fail;
10139 
10140 	pos = os_strchr(pos, ' ');
10141 	if (!pos)
10142 		goto fail;
10143 	pos++;
10144 
10145 	if (sscanf(pos, "%d %d %d %d", &reauth_time, &expiration,
10146 		   &entry->akmp, &entry->opportunistic) != 4)
10147 		goto fail;
10148 	for (i = 0; i < 4; i++) {
10149 		pos = os_strchr(pos, ' ');
10150 		if (!pos) {
10151 			if (i < 3)
10152 				goto fail;
10153 			break;
10154 		}
10155 		pos++;
10156 	}
10157 	if (pos) {
10158 		if (hexstr2bin(pos, entry->fils_cache_id,
10159 			       FILS_CACHE_ID_LEN) < 0)
10160 			goto fail;
10161 		entry->fils_cache_id_set = 1;
10162 	}
10163 	os_get_reltime(&now);
10164 	entry->expiration = now.sec + expiration;
10165 	entry->reauth_time = now.sec + reauth_time;
10166 
10167 	entry->network_ctx = ssid;
10168 
10169 	wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
10170 	entry = NULL;
10171 	ret = 0;
10172 fail:
10173 	os_free(entry);
10174 	return ret;
10175 }
10176 
10177 
10178 #ifdef CONFIG_MESH
10179 
wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant * wpa_s,const char * cmd,char * buf,size_t buflen)10180 static int wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant *wpa_s,
10181 					  const char *cmd, char *buf,
10182 					  size_t buflen)
10183 {
10184 	u8 spa[ETH_ALEN];
10185 
10186 	if (!wpa_s->ifmsh)
10187 		return -1;
10188 
10189 	if (os_strcasecmp(cmd, "any") == 0)
10190 		return wpas_ap_pmksa_cache_list_mesh(wpa_s, NULL, buf, buflen);
10191 
10192 	if (hwaddr_aton(cmd, spa))
10193 		return -1;
10194 
10195 	return wpas_ap_pmksa_cache_list_mesh(wpa_s, spa, buf, buflen);
10196 }
10197 
10198 
wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant * wpa_s,char * cmd)10199 static int wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant *wpa_s,
10200 					  char *cmd)
10201 {
10202 	/*
10203 	 * We do not check mesh interface existance because PMKSA should be
10204 	 * stored before wpa_s->ifmsh creation to suppress commit message
10205 	 * creation.
10206 	 */
10207 	return wpas_ap_pmksa_cache_add_external(wpa_s, cmd);
10208 }
10209 
10210 #endif /* CONFIG_MESH */
10211 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
10212 
10213 
10214 #ifdef CONFIG_FILS
wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant * wpa_s,const char * cmd)10215 static int wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant *wpa_s,
10216 					    const char *cmd)
10217 {
10218 	struct fils_hlp_req *req;
10219 	const char *pos;
10220 
10221 	/* format: <dst> <packet starting from ethertype> */
10222 
10223 	req = os_zalloc(sizeof(*req));
10224 	if (!req)
10225 		return -1;
10226 
10227 	if (hwaddr_aton(cmd, req->dst))
10228 		goto fail;
10229 
10230 	pos = os_strchr(cmd, ' ');
10231 	if (!pos)
10232 		goto fail;
10233 	pos++;
10234 	req->pkt = wpabuf_parse_bin(pos);
10235 	if (!req->pkt)
10236 		goto fail;
10237 
10238 	dl_list_add_tail(&wpa_s->fils_hlp_req, &req->list);
10239 	return 0;
10240 fail:
10241 	wpabuf_free(req->pkt);
10242 	os_free(req);
10243 	return -1;
10244 }
10245 #endif /* CONFIG_FILS */
10246 
10247 
wpas_ctrl_cmd_debug_level(const char * cmd)10248 static int wpas_ctrl_cmd_debug_level(const char *cmd)
10249 {
10250 	if (os_strcmp(cmd, "PING") == 0 ||
10251 	    os_strncmp(cmd, "BSS ", 4) == 0 ||
10252 	    os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
10253 	    os_strncmp(cmd, "STATUS", 6) == 0 ||
10254 	    os_strncmp(cmd, "STA ", 4) == 0 ||
10255 	    os_strncmp(cmd, "STA-", 4) == 0)
10256 		return MSG_EXCESSIVE;
10257 	return MSG_DEBUG;
10258 }
10259 
10260 
wpa_supplicant_ctrl_iface_process(struct wpa_supplicant * wpa_s,char * buf,size_t * resp_len)10261 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
10262 					 char *buf, size_t *resp_len)
10263 {
10264 	char *reply;
10265 	const int reply_size = 4096;
10266 	int reply_len;
10267 
10268 	if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
10269 	    os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
10270 	    os_strncmp(buf, "PMKSA_ADD ", 10) == 0 ||
10271 	    os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
10272 		if (wpa_debug_show_keys)
10273 			wpa_dbg(wpa_s, MSG_DEBUG,
10274 				"Control interface command '%s'", buf);
10275 		else
10276 			wpa_dbg(wpa_s, MSG_DEBUG,
10277 				"Control interface command '%s [REMOVED]'",
10278 				os_strncmp(buf, WPA_CTRL_RSP,
10279 					   os_strlen(WPA_CTRL_RSP)) == 0 ?
10280 				WPA_CTRL_RSP :
10281 				(os_strncmp(buf, "SET_NETWORK ", 12) == 0 ?
10282 				 "SET_NETWORK" : "key-add"));
10283 	} else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
10284 		   os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
10285 		wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
10286 				      (const u8 *) buf, os_strlen(buf));
10287 	} else {
10288 		int level = wpas_ctrl_cmd_debug_level(buf);
10289 		wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
10290 	}
10291 
10292 	reply = os_malloc(reply_size);
10293 	if (reply == NULL) {
10294 		*resp_len = 1;
10295 		return NULL;
10296 	}
10297 
10298 	os_memcpy(reply, "OK\n", 3);
10299 	reply_len = 3;
10300 
10301 	if (os_strcmp(buf, "PING") == 0) {
10302 		os_memcpy(reply, "PONG\n", 5);
10303 		reply_len = 5;
10304 	} else if (os_strcmp(buf, "IFNAME") == 0) {
10305 		reply_len = os_strlen(wpa_s->ifname);
10306 		os_memcpy(reply, wpa_s->ifname, reply_len);
10307 	} else if (os_strncmp(buf, "RELOG", 5) == 0) {
10308 		if (wpa_debug_reopen_file() < 0)
10309 			reply_len = -1;
10310 	} else if (os_strncmp(buf, "NOTE ", 5) == 0) {
10311 		wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
10312 	} else if (os_strcmp(buf, "MIB") == 0) {
10313 		reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
10314 		if (reply_len >= 0) {
10315 			reply_len += eapol_sm_get_mib(wpa_s->eapol,
10316 						      reply + reply_len,
10317 						      reply_size - reply_len);
10318 #ifdef CONFIG_MACSEC
10319 			reply_len += ieee802_1x_kay_get_mib(
10320 				wpa_s->kay, reply + reply_len,
10321 				reply_size - reply_len);
10322 #endif /* CONFIG_MACSEC */
10323 		}
10324 	} else if (os_strncmp(buf, "STATUS", 6) == 0) {
10325 		reply_len = wpa_supplicant_ctrl_iface_status(
10326 			wpa_s, buf + 6, reply, reply_size);
10327 	} else if (os_strcmp(buf, "PMKSA") == 0) {
10328 		reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
10329 	} else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
10330 		wpas_ctrl_iface_pmksa_flush(wpa_s);
10331 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
10332 	} else if (os_strncmp(buf, "PMKSA_GET ", 10) == 0) {
10333 		reply_len = wpas_ctrl_iface_pmksa_get(wpa_s, buf + 10,
10334 						      reply, reply_size);
10335 	} else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
10336 		if (wpas_ctrl_iface_pmksa_add(wpa_s, buf + 10) < 0)
10337 			reply_len = -1;
10338 #ifdef CONFIG_MESH
10339 	} else if (os_strncmp(buf, "MESH_PMKSA_GET ", 15) == 0) {
10340 		reply_len = wpas_ctrl_iface_mesh_pmksa_get(wpa_s, buf + 15,
10341 							   reply, reply_size);
10342 	} else if (os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
10343 		if (wpas_ctrl_iface_mesh_pmksa_add(wpa_s, buf + 15) < 0)
10344 			reply_len = -1;
10345 #endif /* CONFIG_MESH */
10346 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
10347 	} else if (os_strncmp(buf, "SET ", 4) == 0) {
10348 		if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
10349 			reply_len = -1;
10350 	} else if (os_strncmp(buf, "DUMP", 4) == 0) {
10351 		reply_len = wpa_config_dump_values(wpa_s->conf,
10352 						   reply, reply_size);
10353 	} else if (os_strncmp(buf, "GET ", 4) == 0) {
10354 		reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
10355 							  reply, reply_size);
10356 	} else if (os_strcmp(buf, "LOGON") == 0) {
10357 		eapol_sm_notify_logoff(wpa_s->eapol, false);
10358 	} else if (os_strcmp(buf, "LOGOFF") == 0) {
10359 		eapol_sm_notify_logoff(wpa_s->eapol, true);
10360 	} else if (os_strcmp(buf, "REASSOCIATE") == 0) {
10361 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
10362 			reply_len = -1;
10363 		else
10364 			wpas_request_connection(wpa_s);
10365 	} else if (os_strcmp(buf, "REATTACH") == 0) {
10366 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
10367 		    !wpa_s->current_ssid)
10368 			reply_len = -1;
10369 		else {
10370 			wpa_s->reattach = 1;
10371 			wpas_request_connection(wpa_s);
10372 		}
10373 	} else if (os_strcmp(buf, "RECONNECT") == 0) {
10374 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
10375 			reply_len = -1;
10376 		else if (wpa_s->disconnected)
10377 			wpas_request_connection(wpa_s);
10378 #ifdef IEEE8021X_EAPOL
10379 	} else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
10380 		if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
10381 			reply_len = -1;
10382 #endif /* IEEE8021X_EAPOL */
10383 #ifdef CONFIG_IEEE80211R
10384 	} else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
10385 		if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
10386 			reply_len = -1;
10387 #endif /* CONFIG_IEEE80211R */
10388 #ifdef CONFIG_WPS
10389 	} else if (os_strcmp(buf, "WPS_PBC") == 0) {
10390 		int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
10391 		if (res == -2) {
10392 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
10393 			reply_len = 17;
10394 		} else if (res)
10395 			reply_len = -1;
10396 	} else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
10397 		int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
10398 		if (res == -2) {
10399 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
10400 			reply_len = 17;
10401 		} else if (res)
10402 			reply_len = -1;
10403 	} else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
10404 		reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
10405 							      reply,
10406 							      reply_size);
10407 	} else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
10408 		reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
10409 			wpa_s, buf + 14, reply, reply_size);
10410 	} else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
10411 		if (wpas_wps_cancel(wpa_s))
10412 			reply_len = -1;
10413 #ifdef CONFIG_WPS_NFC
10414 	} else if (os_strcmp(buf, "WPS_NFC") == 0) {
10415 		if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
10416 			reply_len = -1;
10417 	} else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
10418 		if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
10419 			reply_len = -1;
10420 	} else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
10421 		reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
10422 			wpa_s, buf + 21, reply, reply_size);
10423 	} else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
10424 		reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
10425 			wpa_s, buf + 14, reply, reply_size);
10426 	} else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
10427 		if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
10428 							       buf + 17))
10429 			reply_len = -1;
10430 	} else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
10431 		reply_len = wpas_ctrl_nfc_get_handover_req(
10432 			wpa_s, buf + 21, reply, reply_size);
10433 	} else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
10434 		reply_len = wpas_ctrl_nfc_get_handover_sel(
10435 			wpa_s, buf + 21, reply, reply_size);
10436 	} else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
10437 		if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
10438 			reply_len = -1;
10439 #endif /* CONFIG_WPS_NFC */
10440 	} else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
10441 		if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
10442 			reply_len = -1;
10443 #ifdef CONFIG_AP
10444 	} else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
10445 		reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
10446 			wpa_s, buf + 11, reply, reply_size);
10447 #endif /* CONFIG_AP */
10448 #ifdef CONFIG_WPS_ER
10449 	} else if (os_strcmp(buf, "WPS_ER_START") == 0) {
10450 		if (wpas_wps_er_start(wpa_s, NULL))
10451 			reply_len = -1;
10452 	} else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
10453 		if (wpas_wps_er_start(wpa_s, buf + 13))
10454 			reply_len = -1;
10455 	} else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
10456 		wpas_wps_er_stop(wpa_s);
10457 	} else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
10458 		if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
10459 			reply_len = -1;
10460 	} else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
10461 		int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
10462 		if (ret == -2) {
10463 			os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
10464 			reply_len = 17;
10465 		} else if (ret == -3) {
10466 			os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
10467 			reply_len = 18;
10468 		} else if (ret == -4) {
10469 			os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
10470 			reply_len = 20;
10471 		} else if (ret)
10472 			reply_len = -1;
10473 	} else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
10474 		if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
10475 			reply_len = -1;
10476 	} else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
10477 		if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
10478 								buf + 18))
10479 			reply_len = -1;
10480 	} else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
10481 		if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
10482 			reply_len = -1;
10483 #ifdef CONFIG_WPS_NFC
10484 	} else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
10485 		reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
10486 			wpa_s, buf + 24, reply, reply_size);
10487 #endif /* CONFIG_WPS_NFC */
10488 #endif /* CONFIG_WPS_ER */
10489 #endif /* CONFIG_WPS */
10490 #ifdef CONFIG_IBSS_RSN
10491 	} else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
10492 		if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
10493 			reply_len = -1;
10494 #endif /* CONFIG_IBSS_RSN */
10495 #ifdef CONFIG_MESH
10496 	} else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
10497 		reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
10498 			wpa_s, buf + 19, reply, reply_size);
10499 	} else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
10500 		reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
10501 			wpa_s, "", reply, reply_size);
10502 	} else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
10503 		if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
10504 			reply_len = -1;
10505 	} else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
10506 		if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
10507 								buf + 18))
10508 			reply_len = -1;
10509 	} else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
10510 		if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
10511 			reply_len = -1;
10512 	} else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
10513 		if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
10514 			reply_len = -1;
10515 	} else if (os_strncmp(buf, "MESH_LINK_PROBE ", 16) == 0) {
10516 		if (wpa_supplicant_ctrl_iface_mesh_link_probe(wpa_s, buf + 16))
10517 			reply_len = -1;
10518 #endif /* CONFIG_MESH */
10519 #ifdef CONFIG_P2P
10520 	} else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
10521 		if (p2p_ctrl_find(wpa_s, buf + 8))
10522 			reply_len = -1;
10523 	} else if (os_strcmp(buf, "P2P_FIND") == 0) {
10524 		if (p2p_ctrl_find(wpa_s, ""))
10525 			reply_len = -1;
10526 	} else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
10527 		wpas_p2p_stop_find(wpa_s);
10528 	} else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
10529 		if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
10530 			reply_len = -1;
10531 	} else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
10532 		if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
10533 			reply_len = -1;
10534 	} else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
10535 		reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
10536 					     reply_size);
10537 	} else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
10538 		if (p2p_ctrl_listen(wpa_s, buf + 11))
10539 			reply_len = -1;
10540 	} else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
10541 		if (p2p_ctrl_listen(wpa_s, ""))
10542 			reply_len = -1;
10543 	} else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
10544 		if (wpas_p2p_group_remove(wpa_s, buf + 17))
10545 			reply_len = -1;
10546 	} else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
10547 		if (p2p_ctrl_group_add(wpa_s, ""))
10548 			reply_len = -1;
10549 	} else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
10550 		if (p2p_ctrl_group_add(wpa_s, buf + 14))
10551 			reply_len = -1;
10552 	} else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
10553 		reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
10554 						  reply_size);
10555 	} else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
10556 		if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
10557 			reply_len = -1;
10558 	} else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
10559 		reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
10560 	} else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
10561 		reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
10562 						   reply_size);
10563 	} else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
10564 		if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
10565 			reply_len = -1;
10566 	} else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
10567 		if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
10568 			reply_len = -1;
10569 	} else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
10570 		wpas_p2p_sd_service_update(wpa_s);
10571 	} else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
10572 		if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
10573 			reply_len = -1;
10574 	} else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
10575 		wpas_p2p_service_flush(wpa_s);
10576 	} else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
10577 		if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
10578 			reply_len = -1;
10579 	} else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
10580 		if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
10581 			reply_len = -1;
10582 	} else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
10583 		if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
10584 			reply_len = -1;
10585 	} else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
10586 		if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
10587 			reply_len = -1;
10588 	} else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
10589 		if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
10590 			reply_len = -1;
10591 	} else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
10592 		reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
10593 					      reply_size);
10594 	} else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
10595 		if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
10596 			reply_len = -1;
10597 	} else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
10598 		p2p_ctrl_flush(wpa_s);
10599 	} else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
10600 		if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
10601 			reply_len = -1;
10602 	} else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
10603 		if (wpas_p2p_cancel(wpa_s))
10604 			reply_len = -1;
10605 	} else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
10606 		if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
10607 			reply_len = -1;
10608 	} else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
10609 		if (p2p_ctrl_presence_req(wpa_s, "") < 0)
10610 			reply_len = -1;
10611 	} else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
10612 		if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
10613 			reply_len = -1;
10614 	} else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
10615 		if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
10616 			reply_len = -1;
10617 	} else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
10618 		if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
10619 			reply_len = -1;
10620 	} else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
10621 		if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
10622 			reply_len = -1;
10623 	} else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
10624 		if (wpas_p2p_lo_stop(wpa_s))
10625 			reply_len = -1;
10626 #endif /* CONFIG_P2P */
10627 #ifdef CONFIG_WIFI_DISPLAY
10628 	} else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
10629 		if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
10630 			reply_len = -1;
10631 	} else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
10632 		reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
10633 						     reply, reply_size);
10634 #endif /* CONFIG_WIFI_DISPLAY */
10635 #ifdef CONFIG_INTERWORKING
10636 	} else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
10637 		if (interworking_fetch_anqp(wpa_s) < 0)
10638 			reply_len = -1;
10639 	} else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
10640 		interworking_stop_fetch_anqp(wpa_s);
10641 	} else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
10642 		if (ctrl_interworking_select(wpa_s, NULL) < 0)
10643 			reply_len = -1;
10644 	} else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
10645 		if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
10646 			reply_len = -1;
10647 	} else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
10648 		if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
10649 			reply_len = -1;
10650 	} else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
10651 		int id;
10652 
10653 		id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
10654 		if (id < 0)
10655 			reply_len = -1;
10656 		else {
10657 			reply_len = os_snprintf(reply, reply_size, "%d\n", id);
10658 			if (os_snprintf_error(reply_size, reply_len))
10659 				reply_len = -1;
10660 		}
10661 	} else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
10662 		if (get_anqp(wpa_s, buf + 9) < 0)
10663 			reply_len = -1;
10664 	} else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
10665 		if (gas_request(wpa_s, buf + 12) < 0)
10666 			reply_len = -1;
10667 	} else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
10668 		reply_len = gas_response_get(wpa_s, buf + 17, reply,
10669 					     reply_size);
10670 #endif /* CONFIG_INTERWORKING */
10671 #ifdef CONFIG_HS20
10672 	} else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
10673 		if (get_hs20_anqp(wpa_s, buf + 14) < 0)
10674 			reply_len = -1;
10675 	} else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
10676 		if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
10677 			reply_len = -1;
10678 	} else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
10679 		if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
10680 			reply_len = -1;
10681 	} else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
10682 		if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
10683 			reply_len = -1;
10684 	} else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
10685 		reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
10686 	} else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
10687 		if (del_hs20_icon(wpa_s, buf + 14) < 0)
10688 			reply_len = -1;
10689 	} else if (os_strcmp(buf, "FETCH_OSU") == 0) {
10690 		if (hs20_fetch_osu(wpa_s, 0) < 0)
10691 			reply_len = -1;
10692 	} else if (os_strcmp(buf, "FETCH_OSU no-scan") == 0) {
10693 		if (hs20_fetch_osu(wpa_s, 1) < 0)
10694 			reply_len = -1;
10695 	} else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
10696 		hs20_cancel_fetch_osu(wpa_s);
10697 #endif /* CONFIG_HS20 */
10698 	} else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
10699 	{
10700 		if (wpa_supplicant_ctrl_iface_ctrl_rsp(
10701 			    wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
10702 			reply_len = -1;
10703 		else {
10704 			/*
10705 			 * Notify response from timeout to allow the control
10706 			 * interface response to be sent first.
10707 			 */
10708 			eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
10709 					       wpa_s, NULL);
10710 		}
10711 	} else if (os_strcmp(buf, "RECONFIGURE") == 0) {
10712 		if (wpa_supplicant_reload_configuration(wpa_s))
10713 			reply_len = -1;
10714 	} else if (os_strcmp(buf, "TERMINATE") == 0) {
10715 		wpa_supplicant_terminate_proc(wpa_s->global);
10716 	} else if (os_strncmp(buf, "BSSID ", 6) == 0) {
10717 		if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
10718 			reply_len = -1;
10719 	} else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
10720 		reply_len = wpa_supplicant_ctrl_iface_blacklist(
10721 			wpa_s, buf + 9, reply, reply_size);
10722 	} else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
10723 		reply_len = wpa_supplicant_ctrl_iface_log_level(
10724 			wpa_s, buf + 9, reply, reply_size);
10725 	} else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
10726 		reply_len = wpa_supplicant_ctrl_iface_list_networks(
10727 			wpa_s, buf + 14, reply, reply_size);
10728 	} else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
10729 		reply_len = wpa_supplicant_ctrl_iface_list_networks(
10730 			wpa_s, NULL, reply, reply_size);
10731 	} else if (os_strcmp(buf, "DISCONNECT") == 0) {
10732 		wpas_request_disconnection(wpa_s);
10733 	} else if (os_strcmp(buf, "SCAN") == 0) {
10734 		wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
10735 	} else if (os_strncmp(buf, "SCAN ", 5) == 0) {
10736 		wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
10737 	} else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
10738 		reply_len = wpa_supplicant_ctrl_iface_scan_results(
10739 			wpa_s, reply, reply_size);
10740 	} else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
10741 		if (wpas_abort_ongoing_scan(wpa_s) < 0)
10742 			reply_len = -1;
10743 	} else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
10744 		if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
10745 			reply_len = -1;
10746 	} else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
10747 		if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
10748 			reply_len = -1;
10749 	} else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
10750 		if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
10751 			reply_len = -1;
10752 	} else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
10753 		reply_len = wpa_supplicant_ctrl_iface_add_network(
10754 			wpa_s, reply, reply_size);
10755 	} else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
10756 		if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
10757 			reply_len = -1;
10758 	} else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
10759 		if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
10760 			reply_len = -1;
10761 	} else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
10762 		reply_len = wpa_supplicant_ctrl_iface_get_network(
10763 			wpa_s, buf + 12, reply, reply_size);
10764 	} else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
10765 		if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
10766 							  wpa_s))
10767 			reply_len = -1;
10768 	} else if (os_strcmp(buf, "LIST_CREDS") == 0) {
10769 		reply_len = wpa_supplicant_ctrl_iface_list_creds(
10770 			wpa_s, reply, reply_size);
10771 	} else if (os_strcmp(buf, "ADD_CRED") == 0) {
10772 		reply_len = wpa_supplicant_ctrl_iface_add_cred(
10773 			wpa_s, reply, reply_size);
10774 	} else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
10775 		if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
10776 			reply_len = -1;
10777 	} else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
10778 		if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
10779 			reply_len = -1;
10780 	} else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
10781 		reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
10782 							       reply,
10783 							       reply_size);
10784 #ifndef CONFIG_NO_CONFIG_WRITE
10785 	} else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
10786 		if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
10787 			reply_len = -1;
10788 #endif /* CONFIG_NO_CONFIG_WRITE */
10789 	} else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
10790 		reply_len = wpa_supplicant_ctrl_iface_get_capability(
10791 			wpa_s, buf + 15, reply, reply_size);
10792 	} else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
10793 		if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
10794 			reply_len = -1;
10795 	} else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
10796 		if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
10797 			reply_len = -1;
10798 	} else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
10799 		reply_len = wpa_supplicant_global_iface_list(
10800 			wpa_s->global, reply, reply_size);
10801 	} else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
10802 		reply_len = wpa_supplicant_global_iface_interfaces(
10803 			wpa_s->global, buf + 10, reply, reply_size);
10804 	} else if (os_strncmp(buf, "BSS ", 4) == 0) {
10805 		reply_len = wpa_supplicant_ctrl_iface_bss(
10806 			wpa_s, buf + 4, reply, reply_size);
10807 #ifdef CONFIG_AP
10808 	} else if (os_strcmp(buf, "STA-FIRST") == 0) {
10809 		reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
10810 	} else if (os_strncmp(buf, "STA ", 4) == 0) {
10811 		reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
10812 					      reply_size);
10813 	} else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
10814 		reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
10815 						   reply_size);
10816 	} else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
10817 		if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
10818 			reply_len = -1;
10819 	} else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
10820 		if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
10821 			reply_len = -1;
10822 	} else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
10823 		if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
10824 			reply_len = -1;
10825 	} else if (os_strcmp(buf, "STOP_AP") == 0) {
10826 		if (wpas_ap_stop_ap(wpa_s))
10827 			reply_len = -1;
10828 #endif /* CONFIG_AP */
10829 	} else if (os_strcmp(buf, "SUSPEND") == 0) {
10830 		wpas_notify_suspend(wpa_s->global);
10831 	} else if (os_strcmp(buf, "RESUME") == 0) {
10832 		wpas_notify_resume(wpa_s->global);
10833 #ifdef CONFIG_TESTING_OPTIONS
10834 	} else if (os_strcmp(buf, "DROP_SA") == 0) {
10835 		wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
10836 #endif /* CONFIG_TESTING_OPTIONS */
10837 	} else if (os_strncmp(buf, "ROAM ", 5) == 0) {
10838 		if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
10839 			reply_len = -1;
10840 	} else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
10841 		wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
10842 	} else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
10843 		if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
10844 			reply_len = -1;
10845 	} else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
10846 		if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
10847 							       buf + 17))
10848 			reply_len = -1;
10849 	} else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
10850 		wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
10851 #ifdef CONFIG_TDLS
10852 	} else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
10853 		if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
10854 			reply_len = -1;
10855 	} else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
10856 		if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
10857 			reply_len = -1;
10858 	} else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
10859 		if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
10860 			reply_len = -1;
10861 	} else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
10862 		if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
10863 							       buf + 17))
10864 			reply_len = -1;
10865 	} else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
10866 		if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
10867 								      buf + 24))
10868 			reply_len = -1;
10869 	} else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
10870 		reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
10871 			wpa_s, buf + 17, reply, reply_size);
10872 #endif /* CONFIG_TDLS */
10873 	} else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
10874 		reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
10875 	} else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
10876 		if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
10877 			reply_len = -1;
10878 	} else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
10879 		if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
10880 			reply_len = -1;
10881 	} else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
10882 		reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
10883 						       reply_size);
10884 	} else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
10885 		if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
10886 			reply_len = -1;
10887 	} else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
10888 		reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
10889 						       reply_size);
10890 #ifdef CONFIG_AUTOSCAN
10891 	} else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
10892 		if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
10893 			reply_len = -1;
10894 #endif /* CONFIG_AUTOSCAN */
10895 	} else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
10896 		reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
10897 							 reply_size);
10898 	} else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
10899 		reply_len = wpas_ctrl_iface_driver_flags2(wpa_s, reply,
10900 							  reply_size);
10901 #ifdef ANDROID
10902 	} else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
10903 		reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
10904 						      reply_size);
10905 #endif /* ANDROID */
10906 	} else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
10907 		reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
10908 						      reply_size);
10909 	} else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
10910 		pmksa_cache_clear_current(wpa_s->wpa);
10911 		eapol_sm_request_reauth(wpa_s->eapol);
10912 #ifdef CONFIG_WNM
10913 	} else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
10914 		if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
10915 			reply_len = -1;
10916 	} else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
10917 		if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
10918 				reply_len = -1;
10919 	} else if (os_strncmp(buf, "COLOC_INTF_REPORT ", 18) == 0) {
10920 		if (wpas_ctrl_iface_coloc_intf_report(wpa_s, buf + 18))
10921 			reply_len = -1;
10922 #endif /* CONFIG_WNM */
10923 	} else if (os_strcmp(buf, "FLUSH") == 0) {
10924 		wpa_supplicant_ctrl_iface_flush(wpa_s);
10925 	} else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
10926 		reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
10927 						 reply_size);
10928 #ifdef CONFIG_TESTING_OPTIONS
10929 	} else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
10930 		if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
10931 			reply_len = -1;
10932 	} else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
10933 		wpas_ctrl_iface_mgmt_tx_done(wpa_s);
10934 	} else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
10935 		if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
10936 			reply_len = -1;
10937 	} else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
10938 		if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
10939 			reply_len = -1;
10940 	} else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
10941 		if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
10942 			reply_len = -1;
10943 	} else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
10944 		if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
10945 			reply_len = -1;
10946 	} else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
10947 		if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
10948 			reply_len = -1;
10949 	} else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
10950 		if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
10951 			reply_len = -1;
10952 	} else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
10953 		if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
10954 			reply_len = -1;
10955 	} else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
10956 		reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
10957 	} else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
10958 		if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0)
10959 			reply_len = -1;
10960 	} else if (os_strcmp(buf, "GET_FAIL") == 0) {
10961 		reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size);
10962 	} else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
10963 		if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
10964 			reply_len = -1;
10965 	} else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
10966 		if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
10967 			reply_len = -1;
10968 	} else if (os_strcmp(buf, "RESET_PN") == 0) {
10969 		if (wpas_ctrl_reset_pn(wpa_s) < 0)
10970 			reply_len = -1;
10971 	} else if (os_strncmp(buf, "KEY_REQUEST ", 12) == 0) {
10972 		if (wpas_ctrl_key_request(wpa_s, buf + 12) < 0)
10973 			reply_len = -1;
10974 	} else if (os_strcmp(buf, "RESEND_ASSOC") == 0) {
10975 		if (wpas_ctrl_resend_assoc(wpa_s) < 0)
10976 			reply_len = -1;
10977 	} else if (os_strcmp(buf, "UNPROT_DEAUTH") == 0) {
10978 		sme_event_unprot_disconnect(
10979 			wpa_s, wpa_s->bssid, NULL,
10980 			WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
10981 #endif /* CONFIG_TESTING_OPTIONS */
10982 	} else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
10983 		if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
10984 			reply_len = -1;
10985 	} else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
10986 		reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
10987 						      reply_size);
10988 	} else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
10989 		if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
10990 			reply_len = -1;
10991 	} else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
10992 		if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
10993 			reply_len = -1;
10994 	} else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
10995 		wpas_ctrl_iface_erp_flush(wpa_s);
10996 	} else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
10997 		if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
10998 			reply_len = -1;
10999 	} else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
11000 		reply_len = wpas_ctrl_iface_get_pref_freq_list(
11001 			wpa_s, buf + 19, reply, reply_size);
11002 #ifdef CONFIG_FILS
11003 	} else if (os_strncmp(buf, "FILS_HLP_REQ_ADD ", 17) == 0) {
11004 		if (wpas_ctrl_iface_fils_hlp_req_add(wpa_s, buf + 17))
11005 			reply_len = -1;
11006 	} else if (os_strcmp(buf, "FILS_HLP_REQ_FLUSH") == 0) {
11007 		wpas_flush_fils_hlp_req(wpa_s);
11008 #endif /* CONFIG_FILS */
11009 #ifdef CONFIG_DPP
11010 	} else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
11011 		int res;
11012 
11013 		res = wpas_dpp_qr_code(wpa_s, buf + 12);
11014 		if (res < 0) {
11015 			reply_len = -1;
11016 		} else {
11017 			reply_len = os_snprintf(reply, reply_size, "%d", res);
11018 			if (os_snprintf_error(reply_size, reply_len))
11019 				reply_len = -1;
11020 		}
11021 	} else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
11022 		int res;
11023 
11024 		res = wpas_dpp_nfc_uri(wpa_s, buf + 12);
11025 		if (res < 0) {
11026 			reply_len = -1;
11027 		} else {
11028 			reply_len = os_snprintf(reply, reply_size, "%d", res);
11029 			if (os_snprintf_error(reply_size, reply_len))
11030 				reply_len = -1;
11031 		}
11032 	} else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
11033 		int res;
11034 
11035 		res = wpas_dpp_nfc_handover_req(wpa_s, buf + 20);
11036 		if (res < 0) {
11037 			reply_len = -1;
11038 		} else {
11039 			reply_len = os_snprintf(reply, reply_size, "%d", res);
11040 			if (os_snprintf_error(reply_size, reply_len))
11041 				reply_len = -1;
11042 		}
11043 	} else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
11044 		int res;
11045 
11046 		res = wpas_dpp_nfc_handover_sel(wpa_s, buf + 20);
11047 		if (res < 0) {
11048 			reply_len = -1;
11049 		} else {
11050 			reply_len = os_snprintf(reply, reply_size, "%d", res);
11051 			if (os_snprintf_error(reply_size, reply_len))
11052 				reply_len = -1;
11053 		}
11054 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
11055 		int res;
11056 
11057 		res = dpp_bootstrap_gen(wpa_s->dpp, buf + 18);
11058 		if (res < 0) {
11059 			reply_len = -1;
11060 		} else {
11061 			reply_len = os_snprintf(reply, reply_size, "%d", res);
11062 			if (os_snprintf_error(reply_size, reply_len))
11063 				reply_len = -1;
11064 		}
11065 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
11066 		if (dpp_bootstrap_remove(wpa_s->dpp, buf + 21) < 0)
11067 			reply_len = -1;
11068 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
11069 		const char *uri;
11070 
11071 		uri = dpp_bootstrap_get_uri(wpa_s->dpp, atoi(buf + 22));
11072 		if (!uri) {
11073 			reply_len = -1;
11074 		} else {
11075 			reply_len = os_snprintf(reply, reply_size, "%s", uri);
11076 			if (os_snprintf_error(reply_size, reply_len))
11077 				reply_len = -1;
11078 		}
11079 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
11080 		reply_len = dpp_bootstrap_info(wpa_s->dpp, atoi(buf + 19),
11081 					       reply, reply_size);
11082 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
11083 		if (dpp_bootstrap_set(wpa_s->dpp, atoi(buf + 18),
11084 				      os_strchr(buf + 18, ' ')) < 0)
11085 			reply_len = -1;
11086 	} else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
11087 		if (wpas_dpp_auth_init(wpa_s, buf + 13) < 0)
11088 			reply_len = -1;
11089 	} else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
11090 		if (wpas_dpp_listen(wpa_s, buf + 11) < 0)
11091 			reply_len = -1;
11092 	} else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
11093 		wpas_dpp_stop(wpa_s);
11094 		wpas_dpp_listen_stop(wpa_s);
11095 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
11096 		int res;
11097 
11098 		res = dpp_configurator_add(wpa_s->dpp, buf + 20);
11099 		if (res < 0) {
11100 			reply_len = -1;
11101 		} else {
11102 			reply_len = os_snprintf(reply, reply_size, "%d", res);
11103 			if (os_snprintf_error(reply_size, reply_len))
11104 				reply_len = -1;
11105 		}
11106 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
11107 		if (dpp_configurator_remove(wpa_s->dpp, buf + 24) < 0)
11108 			reply_len = -1;
11109 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
11110 		if (wpas_dpp_configurator_sign(wpa_s, buf + 21) < 0)
11111 			reply_len = -1;
11112 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
11113 		reply_len = dpp_configurator_get_key_id(wpa_s->dpp,
11114 							atoi(buf + 25),
11115 							reply, reply_size);
11116 	} else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
11117 		int res;
11118 
11119 		res = wpas_dpp_pkex_add(wpa_s, buf + 12);
11120 		if (res < 0) {
11121 			reply_len = -1;
11122 		} else {
11123 			reply_len = os_snprintf(reply, reply_size, "%d", res);
11124 			if (os_snprintf_error(reply_size, reply_len))
11125 				reply_len = -1;
11126 		}
11127 	} else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
11128 		if (wpas_dpp_pkex_remove(wpa_s, buf + 16) < 0)
11129 			reply_len = -1;
11130 #ifdef CONFIG_DPP2
11131 	} else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
11132 		if (wpas_dpp_controller_start(wpa_s, buf + 20) < 0)
11133 			reply_len = -1;
11134 	} else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
11135 		if (wpas_dpp_controller_start(wpa_s, NULL) < 0)
11136 			reply_len = -1;
11137 	} else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
11138 		dpp_controller_stop(wpa_s->dpp);
11139 	} else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
11140 		if (wpas_dpp_chirp(wpa_s, buf + 9) < 0)
11141 			reply_len = -1;
11142 	} else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
11143 		wpas_dpp_chirp_stop(wpa_s);
11144 	} else if (os_strncmp(buf, "DPP_RECONFIG ", 13) == 0) {
11145 		struct wpa_ssid *ssid;
11146 
11147 		ssid = wpa_config_get_network(wpa_s->conf, atoi(buf + 13));
11148 		if (!ssid || wpas_dpp_reconfig(wpa_s, ssid) < 0)
11149 			reply_len = -1;
11150 #endif /* CONFIG_DPP2 */
11151 #endif /* CONFIG_DPP */
11152 	} else {
11153 		os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
11154 		reply_len = 16;
11155 	}
11156 
11157 	if (reply_len < 0) {
11158 		os_memcpy(reply, "FAIL\n", 5);
11159 		reply_len = 5;
11160 	}
11161 
11162 	*resp_len = reply_len;
11163 	return reply;
11164 }
11165 
11166 
wpa_supplicant_global_iface_add(struct wpa_global * global,char * cmd)11167 static int wpa_supplicant_global_iface_add(struct wpa_global *global,
11168 					   char *cmd)
11169 {
11170 	struct wpa_interface iface;
11171 	char *pos, *extra;
11172 	struct wpa_supplicant *wpa_s;
11173 	unsigned int create_iface = 0;
11174 	u8 mac_addr[ETH_ALEN];
11175 	enum wpa_driver_if_type type = WPA_IF_STATION;
11176 
11177 	/*
11178 	 * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
11179 	 * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
11180 	 */
11181 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
11182 
11183 	os_memset(&iface, 0, sizeof(iface));
11184 
11185 	do {
11186 		iface.ifname = pos = cmd;
11187 		pos = os_strchr(pos, '\t');
11188 		if (pos)
11189 			*pos++ = '\0';
11190 		if (iface.ifname[0] == '\0')
11191 			return -1;
11192 		if (pos == NULL)
11193 			break;
11194 
11195 		iface.confname = pos;
11196 		pos = os_strchr(pos, '\t');
11197 		if (pos)
11198 			*pos++ = '\0';
11199 		if (iface.confname[0] == '\0')
11200 			iface.confname = NULL;
11201 		if (pos == NULL)
11202 			break;
11203 
11204 		iface.driver = pos;
11205 		pos = os_strchr(pos, '\t');
11206 		if (pos)
11207 			*pos++ = '\0';
11208 		if (iface.driver[0] == '\0')
11209 			iface.driver = NULL;
11210 		if (pos == NULL)
11211 			break;
11212 
11213 		iface.ctrl_interface = pos;
11214 		pos = os_strchr(pos, '\t');
11215 		if (pos)
11216 			*pos++ = '\0';
11217 		if (iface.ctrl_interface[0] == '\0')
11218 			iface.ctrl_interface = NULL;
11219 		if (pos == NULL)
11220 			break;
11221 
11222 		iface.driver_param = pos;
11223 		pos = os_strchr(pos, '\t');
11224 		if (pos)
11225 			*pos++ = '\0';
11226 		if (iface.driver_param[0] == '\0')
11227 			iface.driver_param = NULL;
11228 		if (pos == NULL)
11229 			break;
11230 
11231 		iface.bridge_ifname = pos;
11232 		pos = os_strchr(pos, '\t');
11233 		if (pos)
11234 			*pos++ = '\0';
11235 		if (iface.bridge_ifname[0] == '\0')
11236 			iface.bridge_ifname = NULL;
11237 		if (pos == NULL)
11238 			break;
11239 
11240 		extra = pos;
11241 		pos = os_strchr(pos, '\t');
11242 		if (pos)
11243 			*pos++ = '\0';
11244 		if (!extra[0])
11245 			break;
11246 
11247 		if (os_strcmp(extra, "create") == 0) {
11248 			create_iface = 1;
11249 			if (!pos)
11250 				break;
11251 
11252 			if (os_strcmp(pos, "sta") == 0) {
11253 				type = WPA_IF_STATION;
11254 			} else if (os_strcmp(pos, "ap") == 0) {
11255 				type = WPA_IF_AP_BSS;
11256 			} else {
11257 				wpa_printf(MSG_DEBUG,
11258 					   "INTERFACE_ADD unsupported interface type: '%s'",
11259 					   pos);
11260 				return -1;
11261 			}
11262 		} else {
11263 			wpa_printf(MSG_DEBUG,
11264 				   "INTERFACE_ADD unsupported extra parameter: '%s'",
11265 				   extra);
11266 			return -1;
11267 		}
11268 	} while (0);
11269 
11270 	if (create_iface) {
11271 		wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
11272 			   iface.ifname);
11273 		if (!global->ifaces)
11274 			return -1;
11275 		if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
11276 				   NULL, NULL, NULL, mac_addr, NULL) < 0) {
11277 			wpa_printf(MSG_ERROR,
11278 				   "CTRL_IFACE interface creation failed");
11279 			return -1;
11280 		}
11281 
11282 		wpa_printf(MSG_DEBUG,
11283 			   "CTRL_IFACE interface '%s' created with MAC addr: "
11284 			   MACSTR, iface.ifname, MAC2STR(mac_addr));
11285 	}
11286 
11287 	if (wpa_supplicant_get_iface(global, iface.ifname))
11288 		goto fail;
11289 
11290 	wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
11291 	if (!wpa_s)
11292 		goto fail;
11293 	wpa_s->added_vif = create_iface;
11294 	return 0;
11295 
11296 fail:
11297 	if (create_iface)
11298 		wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
11299 	return -1;
11300 }
11301 
11302 
wpa_supplicant_global_iface_remove(struct wpa_global * global,char * cmd)11303 static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
11304 					      char *cmd)
11305 {
11306 	struct wpa_supplicant *wpa_s;
11307 	int ret;
11308 	unsigned int delete_iface;
11309 
11310 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
11311 
11312 	wpa_s = wpa_supplicant_get_iface(global, cmd);
11313 	if (wpa_s == NULL)
11314 		return -1;
11315 	delete_iface = wpa_s->added_vif;
11316 	ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
11317 	if (!ret && delete_iface) {
11318 		wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
11319 			   cmd);
11320 		ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
11321 	}
11322 	return ret;
11323 }
11324 
11325 
wpa_free_iface_info(struct wpa_interface_info * iface)11326 static void wpa_free_iface_info(struct wpa_interface_info *iface)
11327 {
11328 	struct wpa_interface_info *prev;
11329 
11330 	while (iface) {
11331 		prev = iface;
11332 		iface = iface->next;
11333 
11334 		os_free(prev->ifname);
11335 		os_free(prev->desc);
11336 		os_free(prev);
11337 	}
11338 }
11339 
11340 
wpa_supplicant_global_iface_list(struct wpa_global * global,char * buf,int len)11341 static int wpa_supplicant_global_iface_list(struct wpa_global *global,
11342 					    char *buf, int len)
11343 {
11344 	int i, res;
11345 	struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
11346 	char *pos, *end;
11347 
11348 	for (i = 0; wpa_drivers[i]; i++) {
11349 		const struct wpa_driver_ops *drv = wpa_drivers[i];
11350 		if (drv->get_interfaces == NULL)
11351 			continue;
11352 		tmp = drv->get_interfaces(global->drv_priv[i]);
11353 		if (tmp == NULL)
11354 			continue;
11355 
11356 		if (last == NULL)
11357 			iface = last = tmp;
11358 		else
11359 			last->next = tmp;
11360 		while (last->next)
11361 			last = last->next;
11362 	}
11363 
11364 	pos = buf;
11365 	end = buf + len;
11366 	for (tmp = iface; tmp; tmp = tmp->next) {
11367 		res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
11368 				  tmp->drv_name, tmp->ifname,
11369 				  tmp->desc ? tmp->desc : "");
11370 		if (os_snprintf_error(end - pos, res)) {
11371 			*pos = '\0';
11372 			break;
11373 		}
11374 		pos += res;
11375 	}
11376 
11377 	wpa_free_iface_info(iface);
11378 
11379 	return pos - buf;
11380 }
11381 
11382 
wpa_supplicant_global_iface_interfaces(struct wpa_global * global,const char * input,char * buf,int len)11383 static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
11384 						  const char *input,
11385 						  char *buf, int len)
11386 {
11387 	int res;
11388 	char *pos, *end;
11389 	struct wpa_supplicant *wpa_s;
11390 	int show_ctrl = 0;
11391 
11392 	if (input)
11393 		show_ctrl = !!os_strstr(input, "ctrl");
11394 
11395 	wpa_s = global->ifaces;
11396 	pos = buf;
11397 	end = buf + len;
11398 
11399 	while (wpa_s) {
11400 		if (show_ctrl)
11401 			res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
11402 					  wpa_s->ifname,
11403 					  wpa_s->conf->ctrl_interface ?
11404 					  wpa_s->conf->ctrl_interface : "N/A");
11405 		else
11406 			res = os_snprintf(pos, end - pos, "%s\n",
11407 					  wpa_s->ifname);
11408 
11409 		if (os_snprintf_error(end - pos, res)) {
11410 			*pos = '\0';
11411 			break;
11412 		}
11413 		pos += res;
11414 		wpa_s = wpa_s->next;
11415 	}
11416 	return pos - buf;
11417 }
11418 
11419 
wpas_global_ctrl_iface_ifname(struct wpa_global * global,const char * ifname,char * cmd,size_t * resp_len)11420 static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
11421 					    const char *ifname,
11422 					    char *cmd, size_t *resp_len)
11423 {
11424 	struct wpa_supplicant *wpa_s;
11425 
11426 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
11427 		if (os_strcmp(ifname, wpa_s->ifname) == 0)
11428 			break;
11429 	}
11430 
11431 	if (wpa_s == NULL) {
11432 		char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
11433 		if (resp)
11434 			*resp_len = os_strlen(resp);
11435 		else
11436 			*resp_len = 1;
11437 		return resp;
11438 	}
11439 
11440 	return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
11441 }
11442 
11443 
wpas_global_ctrl_iface_redir_p2p(struct wpa_global * global,char * buf,size_t * resp_len)11444 static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
11445 					       char *buf, size_t *resp_len)
11446 {
11447 #ifdef CONFIG_P2P
11448 	static const char * cmd[] = {
11449 		"LIST_NETWORKS",
11450 		"P2P_FIND",
11451 		"P2P_STOP_FIND",
11452 		"P2P_LISTEN",
11453 		"P2P_GROUP_ADD",
11454 		"P2P_GET_PASSPHRASE",
11455 		"P2P_SERVICE_UPDATE",
11456 		"P2P_SERVICE_FLUSH",
11457 		"P2P_FLUSH",
11458 		"P2P_CANCEL",
11459 		"P2P_PRESENCE_REQ",
11460 		"P2P_EXT_LISTEN",
11461 #ifdef CONFIG_AP
11462 		"STA-FIRST",
11463 #endif /* CONFIG_AP */
11464 		NULL
11465 	};
11466 	static const char * prefix[] = {
11467 #ifdef ANDROID
11468 		"DRIVER ",
11469 #endif /* ANDROID */
11470 		"GET_CAPABILITY ",
11471 		"GET_NETWORK ",
11472 		"REMOVE_NETWORK ",
11473 		"P2P_FIND ",
11474 		"P2P_CONNECT ",
11475 		"P2P_LISTEN ",
11476 		"P2P_GROUP_REMOVE ",
11477 		"P2P_GROUP_ADD ",
11478 		"P2P_GROUP_MEMBER ",
11479 		"P2P_PROV_DISC ",
11480 		"P2P_SERV_DISC_REQ ",
11481 		"P2P_SERV_DISC_CANCEL_REQ ",
11482 		"P2P_SERV_DISC_RESP ",
11483 		"P2P_SERV_DISC_EXTERNAL ",
11484 		"P2P_SERVICE_ADD ",
11485 		"P2P_SERVICE_DEL ",
11486 		"P2P_SERVICE_REP ",
11487 		"P2P_REJECT ",
11488 		"P2P_INVITE ",
11489 		"P2P_PEER ",
11490 		"P2P_SET ",
11491 		"P2P_UNAUTHORIZE ",
11492 		"P2P_PRESENCE_REQ ",
11493 		"P2P_EXT_LISTEN ",
11494 		"P2P_REMOVE_CLIENT ",
11495 		"WPS_NFC_TOKEN ",
11496 		"WPS_NFC_TAG_READ ",
11497 		"NFC_GET_HANDOVER_SEL ",
11498 		"NFC_GET_HANDOVER_REQ ",
11499 		"NFC_REPORT_HANDOVER ",
11500 		"P2P_ASP_PROVISION ",
11501 		"P2P_ASP_PROVISION_RESP ",
11502 #ifdef CONFIG_AP
11503 		"STA ",
11504 		"STA-NEXT ",
11505 #endif /* CONFIG_AP */
11506 		NULL
11507 	};
11508 	int found = 0;
11509 	int i;
11510 
11511 	if (global->p2p_init_wpa_s == NULL)
11512 		return NULL;
11513 
11514 	for (i = 0; !found && cmd[i]; i++) {
11515 		if (os_strcmp(buf, cmd[i]) == 0)
11516 			found = 1;
11517 	}
11518 
11519 	for (i = 0; !found && prefix[i]; i++) {
11520 		if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
11521 			found = 1;
11522 	}
11523 
11524 	if (found)
11525 		return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
11526 							 buf, resp_len);
11527 #endif /* CONFIG_P2P */
11528 	return NULL;
11529 }
11530 
11531 
wpas_global_ctrl_iface_redir_wfd(struct wpa_global * global,char * buf,size_t * resp_len)11532 static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
11533 					       char *buf, size_t *resp_len)
11534 {
11535 #ifdef CONFIG_WIFI_DISPLAY
11536 	if (global->p2p_init_wpa_s == NULL)
11537 		return NULL;
11538 	if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
11539 	    os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
11540 		return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
11541 							 buf, resp_len);
11542 #endif /* CONFIG_WIFI_DISPLAY */
11543 	return NULL;
11544 }
11545 
11546 
wpas_global_ctrl_iface_redir(struct wpa_global * global,char * buf,size_t * resp_len)11547 static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
11548 					   char *buf, size_t *resp_len)
11549 {
11550 	char *ret;
11551 
11552 	ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
11553 	if (ret)
11554 		return ret;
11555 
11556 	ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
11557 	if (ret)
11558 		return ret;
11559 
11560 	return NULL;
11561 }
11562 
11563 
wpas_global_ctrl_iface_set(struct wpa_global * global,char * cmd)11564 static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
11565 {
11566 	char *value;
11567 
11568 	value = os_strchr(cmd, ' ');
11569 	if (value == NULL)
11570 		return -1;
11571 	*value++ = '\0';
11572 
11573 	wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
11574 
11575 #ifdef CONFIG_WIFI_DISPLAY
11576 	if (os_strcasecmp(cmd, "wifi_display") == 0) {
11577 		wifi_display_enable(global, !!atoi(value));
11578 		return 0;
11579 	}
11580 #endif /* CONFIG_WIFI_DISPLAY */
11581 
11582 	/* Restore cmd to its original value to allow redirection */
11583 	value[-1] = ' ';
11584 
11585 	return -1;
11586 }
11587 
11588 
wpas_global_ctrl_iface_dup_network(struct wpa_global * global,char * cmd)11589 static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
11590 					      char *cmd)
11591 {
11592 	struct wpa_supplicant *wpa_s[2]; /* src, dst */
11593 	char *p;
11594 	unsigned int i;
11595 
11596 	/* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
11597 	 * <variable name> */
11598 
11599 	for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
11600 		p = os_strchr(cmd, ' ');
11601 		if (p == NULL)
11602 			return -1;
11603 		*p = '\0';
11604 
11605 		wpa_s[i] = global->ifaces;
11606 		for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
11607 			if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
11608 				break;
11609 		}
11610 
11611 		if (!wpa_s[i]) {
11612 			wpa_printf(MSG_DEBUG,
11613 				   "CTRL_IFACE: Could not find iface=%s", cmd);
11614 			return -1;
11615 		}
11616 
11617 		cmd = p + 1;
11618 	}
11619 
11620 	return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
11621 }
11622 
11623 
11624 #ifndef CONFIG_NO_CONFIG_WRITE
wpas_global_ctrl_iface_save_config(struct wpa_global * global)11625 static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
11626 {
11627 	int ret = 0, saved = 0;
11628 	struct wpa_supplicant *wpa_s;
11629 
11630 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
11631 		if (!wpa_s->conf->update_config) {
11632 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
11633 			continue;
11634 		}
11635 
11636 		if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
11637 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
11638 			ret = 1;
11639 		} else {
11640 			wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
11641 			saved++;
11642 		}
11643 	}
11644 
11645 	if (!saved && !ret) {
11646 		wpa_dbg(wpa_s, MSG_DEBUG,
11647 			"CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
11648 		ret = 1;
11649 	}
11650 
11651 	return ret;
11652 }
11653 #endif /* CONFIG_NO_CONFIG_WRITE */
11654 
11655 
wpas_global_ctrl_iface_status(struct wpa_global * global,char * buf,size_t buflen)11656 static int wpas_global_ctrl_iface_status(struct wpa_global *global,
11657 					 char *buf, size_t buflen)
11658 {
11659 	char *pos, *end;
11660 	int ret;
11661 	struct wpa_supplicant *wpa_s;
11662 
11663 	pos = buf;
11664 	end = buf + buflen;
11665 
11666 #ifdef CONFIG_P2P
11667 	if (global->p2p && !global->p2p_disabled) {
11668 		ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
11669 				  "\n"
11670 				  "p2p_state=%s\n",
11671 				  MAC2STR(global->p2p_dev_addr),
11672 				  p2p_get_state_txt(global->p2p));
11673 		if (os_snprintf_error(end - pos, ret))
11674 			return pos - buf;
11675 		pos += ret;
11676 	} else if (global->p2p) {
11677 		ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
11678 		if (os_snprintf_error(end - pos, ret))
11679 			return pos - buf;
11680 		pos += ret;
11681 	}
11682 #endif /* CONFIG_P2P */
11683 
11684 #ifdef CONFIG_WIFI_DISPLAY
11685 	ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
11686 			  !!global->wifi_display);
11687 	if (os_snprintf_error(end - pos, ret))
11688 		return pos - buf;
11689 	pos += ret;
11690 #endif /* CONFIG_WIFI_DISPLAY */
11691 
11692 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
11693 		ret = os_snprintf(pos, end - pos, "ifname=%s\n"
11694 				  "address=" MACSTR "\n",
11695 				  wpa_s->ifname, MAC2STR(wpa_s->own_addr));
11696 		if (os_snprintf_error(end - pos, ret))
11697 			return pos - buf;
11698 		pos += ret;
11699 	}
11700 
11701 	return pos - buf;
11702 }
11703 
11704 
11705 #ifdef CONFIG_FST
11706 
wpas_global_ctrl_iface_fst_attach(struct wpa_global * global,char * cmd,char * buf,size_t reply_size)11707 static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
11708 					     char *cmd, char *buf,
11709 					     size_t reply_size)
11710 {
11711 	char ifname[IFNAMSIZ + 1];
11712 	struct fst_iface_cfg cfg;
11713 	struct wpa_supplicant *wpa_s;
11714 	struct fst_wpa_obj iface_obj;
11715 
11716 	if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
11717 		wpa_s = wpa_supplicant_get_iface(global, ifname);
11718 		if (wpa_s) {
11719 			if (wpa_s->fst) {
11720 				wpa_printf(MSG_INFO, "FST: Already attached");
11721 				return -1;
11722 			}
11723 			fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
11724 			wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
11725 						&iface_obj, &cfg);
11726 			if (wpa_s->fst)
11727 				return os_snprintf(buf, reply_size, "OK\n");
11728 		}
11729 	}
11730 
11731 	return -1;
11732 }
11733 
11734 
wpas_global_ctrl_iface_fst_detach(struct wpa_global * global,char * cmd,char * buf,size_t reply_size)11735 static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
11736 					     char *cmd, char *buf,
11737 					     size_t reply_size)
11738 {
11739 	char ifname[IFNAMSIZ + 1];
11740 	struct wpa_supplicant *wpa_s;
11741 
11742 	if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
11743 		wpa_s = wpa_supplicant_get_iface(global, ifname);
11744 		if (wpa_s) {
11745 			if (!fst_iface_detach(ifname)) {
11746 				wpa_s->fst = NULL;
11747 				return os_snprintf(buf, reply_size, "OK\n");
11748 			}
11749 		}
11750 	}
11751 
11752 	return -1;
11753 }
11754 
11755 #endif /* CONFIG_FST */
11756 
11757 
wpa_supplicant_global_ctrl_iface_process(struct wpa_global * global,char * buf,size_t * resp_len)11758 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
11759 						char *buf, size_t *resp_len)
11760 {
11761 	char *reply;
11762 	const int reply_size = 2048;
11763 	int reply_len;
11764 	int level = MSG_DEBUG;
11765 
11766 	if (os_strncmp(buf, "IFNAME=", 7) == 0) {
11767 		char *pos = os_strchr(buf + 7, ' ');
11768 		if (pos) {
11769 			*pos++ = '\0';
11770 			return wpas_global_ctrl_iface_ifname(global,
11771 							     buf + 7, pos,
11772 							     resp_len);
11773 		}
11774 	}
11775 
11776 	reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
11777 	if (reply)
11778 		return reply;
11779 
11780 	if (os_strcmp(buf, "PING") == 0)
11781 		level = MSG_EXCESSIVE;
11782 	wpa_hexdump_ascii(level, "RX global ctrl_iface",
11783 			  (const u8 *) buf, os_strlen(buf));
11784 
11785 	reply = os_malloc(reply_size);
11786 	if (reply == NULL) {
11787 		*resp_len = 1;
11788 		return NULL;
11789 	}
11790 
11791 	os_memcpy(reply, "OK\n", 3);
11792 	reply_len = 3;
11793 
11794 	if (os_strcmp(buf, "PING") == 0) {
11795 		os_memcpy(reply, "PONG\n", 5);
11796 		reply_len = 5;
11797 	} else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
11798 		if (wpa_supplicant_global_iface_add(global, buf + 14))
11799 			reply_len = -1;
11800 	} else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
11801 		if (wpa_supplicant_global_iface_remove(global, buf + 17))
11802 			reply_len = -1;
11803 	} else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
11804 		reply_len = wpa_supplicant_global_iface_list(
11805 			global, reply, reply_size);
11806 	} else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
11807 		reply_len = wpa_supplicant_global_iface_interfaces(
11808 			global, buf + 10, reply, reply_size);
11809 #ifdef CONFIG_FST
11810 	} else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
11811 		reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
11812 							      reply,
11813 							      reply_size);
11814 	} else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
11815 		reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
11816 							      reply,
11817 							      reply_size);
11818 	} else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
11819 		reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
11820 #endif /* CONFIG_FST */
11821 	} else if (os_strcmp(buf, "TERMINATE") == 0) {
11822 		wpa_supplicant_terminate_proc(global);
11823 	} else if (os_strcmp(buf, "SUSPEND") == 0) {
11824 		wpas_notify_suspend(global);
11825 	} else if (os_strcmp(buf, "RESUME") == 0) {
11826 		wpas_notify_resume(global);
11827 	} else if (os_strncmp(buf, "SET ", 4) == 0) {
11828 		if (wpas_global_ctrl_iface_set(global, buf + 4)) {
11829 #ifdef CONFIG_P2P
11830 			if (global->p2p_init_wpa_s) {
11831 				os_free(reply);
11832 				/* Check if P2P redirection would work for this
11833 				 * command. */
11834 				return wpa_supplicant_ctrl_iface_process(
11835 					global->p2p_init_wpa_s,
11836 					buf, resp_len);
11837 			}
11838 #endif /* CONFIG_P2P */
11839 			reply_len = -1;
11840 		}
11841 	} else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
11842 		if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
11843 			reply_len = -1;
11844 #ifndef CONFIG_NO_CONFIG_WRITE
11845 	} else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
11846 		if (wpas_global_ctrl_iface_save_config(global))
11847 			reply_len = -1;
11848 #endif /* CONFIG_NO_CONFIG_WRITE */
11849 	} else if (os_strcmp(buf, "STATUS") == 0) {
11850 		reply_len = wpas_global_ctrl_iface_status(global, reply,
11851 							  reply_size);
11852 #ifdef CONFIG_MODULE_TESTS
11853 	} else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
11854 		if (wpas_module_tests() < 0)
11855 			reply_len = -1;
11856 #endif /* CONFIG_MODULE_TESTS */
11857 	} else if (os_strncmp(buf, "RELOG", 5) == 0) {
11858 		if (wpa_debug_reopen_file() < 0)
11859 			reply_len = -1;
11860 	} else {
11861 		os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
11862 		reply_len = 16;
11863 	}
11864 
11865 	if (reply_len < 0) {
11866 		os_memcpy(reply, "FAIL\n", 5);
11867 		reply_len = 5;
11868 	}
11869 
11870 	*resp_len = reply_len;
11871 	return reply;
11872 }
11873