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