xref: /freebsd/contrib/wpa/hostapd/ctrl_iface.c (revision 53b70c86)
1 /*
2  * hostapd / UNIX domain socket -based control interface
3  * Copyright (c) 2004-2018, 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 
11 #ifndef CONFIG_NATIVE_WINDOWS
12 
13 #ifdef CONFIG_TESTING_OPTIONS
14 #ifdef __NetBSD__
15 #include <net/if_ether.h>
16 #else
17 #include <net/ethernet.h>
18 #endif
19 #include <netinet/ip.h>
20 #endif /* CONFIG_TESTING_OPTIONS */
21 
22 #include <sys/un.h>
23 #include <sys/stat.h>
24 #include <stddef.h>
25 
26 #ifdef CONFIG_CTRL_IFACE_UDP
27 #include <netdb.h>
28 #endif /* CONFIG_CTRL_IFACE_UDP */
29 
30 #include "utils/common.h"
31 #include "utils/eloop.h"
32 #include "utils/module_tests.h"
33 #include "common/version.h"
34 #include "common/ieee802_11_defs.h"
35 #include "common/ctrl_iface_common.h"
36 #ifdef CONFIG_DPP
37 #include "common/dpp.h"
38 #endif /* CONFIG_DPP */
39 #include "common/wpa_ctrl.h"
40 #include "common/ptksa_cache.h"
41 #include "crypto/tls.h"
42 #include "drivers/driver.h"
43 #include "eapol_auth/eapol_auth_sm.h"
44 #include "radius/radius_client.h"
45 #include "radius/radius_server.h"
46 #include "l2_packet/l2_packet.h"
47 #include "ap/hostapd.h"
48 #include "ap/ap_config.h"
49 #include "ap/ieee802_1x.h"
50 #include "ap/wpa_auth.h"
51 #include "ap/pmksa_cache_auth.h"
52 #include "ap/ieee802_11.h"
53 #include "ap/sta_info.h"
54 #include "ap/wps_hostapd.h"
55 #include "ap/ctrl_iface_ap.h"
56 #include "ap/ap_drv_ops.h"
57 #include "ap/hs20.h"
58 #include "ap/wnm_ap.h"
59 #include "ap/wpa_auth.h"
60 #include "ap/beacon.h"
61 #include "ap/neighbor_db.h"
62 #include "ap/rrm.h"
63 #include "ap/dpp_hostapd.h"
64 #include "ap/dfs.h"
65 #include "wps/wps_defs.h"
66 #include "wps/wps.h"
67 #include "fst/fst_ctrl_iface.h"
68 #include "config_file.h"
69 #include "ctrl_iface.h"
70 
71 
72 #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
73 
74 #ifdef CONFIG_CTRL_IFACE_UDP
75 #define HOSTAPD_CTRL_IFACE_PORT		8877
76 #define HOSTAPD_CTRL_IFACE_PORT_LIMIT	50
77 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT		8878
78 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT	50
79 #endif /* CONFIG_CTRL_IFACE_UDP */
80 
81 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
82 				    enum wpa_msg_type type,
83 				    const char *buf, size_t len);
84 
85 
86 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
87 				     struct sockaddr_storage *from,
88 				     socklen_t fromlen, const char *input)
89 {
90 	return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen, input);
91 }
92 
93 
94 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
95 				     struct sockaddr_storage *from,
96 				     socklen_t fromlen)
97 {
98 	return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
99 }
100 
101 
102 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
103 				    struct sockaddr_storage *from,
104 				    socklen_t fromlen,
105 				    char *level)
106 {
107 	return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
108 }
109 
110 
111 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
112 				      const char *txtaddr)
113 {
114 	u8 addr[ETH_ALEN];
115 	struct sta_info *sta;
116 
117 	wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
118 
119 	if (hwaddr_aton(txtaddr, addr))
120 		return -1;
121 
122 	sta = ap_get_sta(hapd, addr);
123 	if (sta)
124 		return 0;
125 
126 	wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
127 		   "notification", MAC2STR(addr));
128 	sta = ap_sta_add(hapd, addr);
129 	if (sta == NULL)
130 		return -1;
131 
132 	hostapd_new_assoc_sta(hapd, sta, 0);
133 	return 0;
134 }
135 
136 
137 #ifdef NEED_AP_MLME
138 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
139 				       const char *txtaddr)
140 {
141 	u8 addr[ETH_ALEN];
142 	u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
143 
144 	wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
145 
146 	if (hwaddr_aton(txtaddr, addr) ||
147 	    os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
148 		return -1;
149 
150 	ieee802_11_send_sa_query_req(hapd, addr, trans_id);
151 
152 	return 0;
153 }
154 #endif /* NEED_AP_MLME */
155 
156 
157 #ifdef CONFIG_WPS
158 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
159 {
160 	char *pin = os_strchr(txt, ' ');
161 	char *timeout_txt;
162 	int timeout;
163 	u8 addr_buf[ETH_ALEN], *addr = NULL;
164 	char *pos;
165 
166 	if (pin == NULL)
167 		return -1;
168 	*pin++ = '\0';
169 
170 	timeout_txt = os_strchr(pin, ' ');
171 	if (timeout_txt) {
172 		*timeout_txt++ = '\0';
173 		timeout = atoi(timeout_txt);
174 		pos = os_strchr(timeout_txt, ' ');
175 		if (pos) {
176 			*pos++ = '\0';
177 			if (hwaddr_aton(pos, addr_buf) == 0)
178 				addr = addr_buf;
179 		}
180 	} else
181 		timeout = 0;
182 
183 	return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
184 }
185 
186 
187 static int hostapd_ctrl_iface_wps_check_pin(
188 	struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
189 {
190 	char pin[9];
191 	size_t len;
192 	char *pos;
193 	int ret;
194 
195 	wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
196 			      (u8 *) cmd, os_strlen(cmd));
197 	for (pos = cmd, len = 0; *pos != '\0'; pos++) {
198 		if (*pos < '0' || *pos > '9')
199 			continue;
200 		pin[len++] = *pos;
201 		if (len == 9) {
202 			wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
203 			return -1;
204 		}
205 	}
206 	if (len != 4 && len != 8) {
207 		wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
208 		return -1;
209 	}
210 	pin[len] = '\0';
211 
212 	if (len == 8) {
213 		unsigned int pin_val;
214 		pin_val = atoi(pin);
215 		if (!wps_pin_valid(pin_val)) {
216 			wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
217 			ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
218 			if (os_snprintf_error(buflen, ret))
219 				return -1;
220 			return ret;
221 		}
222 	}
223 
224 	ret = os_snprintf(buf, buflen, "%s", pin);
225 	if (os_snprintf_error(buflen, ret))
226 		return -1;
227 
228 	return ret;
229 }
230 
231 
232 #ifdef CONFIG_WPS_NFC
233 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
234 					       char *pos)
235 {
236 	size_t len;
237 	struct wpabuf *buf;
238 	int ret;
239 
240 	len = os_strlen(pos);
241 	if (len & 0x01)
242 		return -1;
243 	len /= 2;
244 
245 	buf = wpabuf_alloc(len);
246 	if (buf == NULL)
247 		return -1;
248 	if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
249 		wpabuf_free(buf);
250 		return -1;
251 	}
252 
253 	ret = hostapd_wps_nfc_tag_read(hapd, buf);
254 	wpabuf_free(buf);
255 
256 	return ret;
257 }
258 
259 
260 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
261 						   char *cmd, char *reply,
262 						   size_t max_len)
263 {
264 	int ndef;
265 	struct wpabuf *buf;
266 	int res;
267 
268 	if (os_strcmp(cmd, "WPS") == 0)
269 		ndef = 0;
270 	else if (os_strcmp(cmd, "NDEF") == 0)
271 		ndef = 1;
272 	else
273 		return -1;
274 
275 	buf = hostapd_wps_nfc_config_token(hapd, ndef);
276 	if (buf == NULL)
277 		return -1;
278 
279 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
280 					 wpabuf_len(buf));
281 	reply[res++] = '\n';
282 	reply[res] = '\0';
283 
284 	wpabuf_free(buf);
285 
286 	return res;
287 }
288 
289 
290 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
291 						char *reply, size_t max_len,
292 						int ndef)
293 {
294 	struct wpabuf *buf;
295 	int res;
296 
297 	buf = hostapd_wps_nfc_token_gen(hapd, ndef);
298 	if (buf == NULL)
299 		return -1;
300 
301 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
302 					 wpabuf_len(buf));
303 	reply[res++] = '\n';
304 	reply[res] = '\0';
305 
306 	wpabuf_free(buf);
307 
308 	return res;
309 }
310 
311 
312 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
313 					    char *cmd, char *reply,
314 					    size_t max_len)
315 {
316 	if (os_strcmp(cmd, "WPS") == 0)
317 		return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
318 							    max_len, 0);
319 
320 	if (os_strcmp(cmd, "NDEF") == 0)
321 		return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
322 							    max_len, 1);
323 
324 	if (os_strcmp(cmd, "enable") == 0)
325 		return hostapd_wps_nfc_token_enable(hapd);
326 
327 	if (os_strcmp(cmd, "disable") == 0) {
328 		hostapd_wps_nfc_token_disable(hapd);
329 		return 0;
330 	}
331 
332 	return -1;
333 }
334 
335 
336 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
337 						   char *cmd, char *reply,
338 						   size_t max_len)
339 {
340 	struct wpabuf *buf;
341 	int res;
342 	char *pos;
343 	int ndef;
344 
345 	pos = os_strchr(cmd, ' ');
346 	if (pos == NULL)
347 		return -1;
348 	*pos++ = '\0';
349 
350 	if (os_strcmp(cmd, "WPS") == 0)
351 		ndef = 0;
352 	else if (os_strcmp(cmd, "NDEF") == 0)
353 		ndef = 1;
354 	else
355 		return -1;
356 
357 	if (os_strcmp(pos, "WPS-CR") == 0)
358 		buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
359 	else
360 		buf = NULL;
361 	if (buf == NULL)
362 		return -1;
363 
364 	res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
365 					 wpabuf_len(buf));
366 	reply[res++] = '\n';
367 	reply[res] = '\0';
368 
369 	wpabuf_free(buf);
370 
371 	return res;
372 }
373 
374 
375 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
376 						  char *cmd)
377 {
378 	size_t len;
379 	struct wpabuf *req, *sel;
380 	int ret;
381 	char *pos, *role, *type, *pos2;
382 
383 	role = cmd;
384 	pos = os_strchr(role, ' ');
385 	if (pos == NULL)
386 		return -1;
387 	*pos++ = '\0';
388 
389 	type = pos;
390 	pos = os_strchr(type, ' ');
391 	if (pos == NULL)
392 		return -1;
393 	*pos++ = '\0';
394 
395 	pos2 = os_strchr(pos, ' ');
396 	if (pos2 == NULL)
397 		return -1;
398 	*pos2++ = '\0';
399 
400 	len = os_strlen(pos);
401 	if (len & 0x01)
402 		return -1;
403 	len /= 2;
404 
405 	req = wpabuf_alloc(len);
406 	if (req == NULL)
407 		return -1;
408 	if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
409 		wpabuf_free(req);
410 		return -1;
411 	}
412 
413 	len = os_strlen(pos2);
414 	if (len & 0x01) {
415 		wpabuf_free(req);
416 		return -1;
417 	}
418 	len /= 2;
419 
420 	sel = wpabuf_alloc(len);
421 	if (sel == NULL) {
422 		wpabuf_free(req);
423 		return -1;
424 	}
425 	if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
426 		wpabuf_free(req);
427 		wpabuf_free(sel);
428 		return -1;
429 	}
430 
431 	if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
432 		ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
433 	} else {
434 		wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
435 			   "reported: role=%s type=%s", role, type);
436 		ret = -1;
437 	}
438 	wpabuf_free(req);
439 	wpabuf_free(sel);
440 
441 	return ret;
442 }
443 
444 #endif /* CONFIG_WPS_NFC */
445 
446 
447 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
448 					 char *buf, size_t buflen)
449 {
450 	int timeout = 300;
451 	char *pos;
452 	const char *pin_txt;
453 
454 	pos = os_strchr(txt, ' ');
455 	if (pos)
456 		*pos++ = '\0';
457 
458 	if (os_strcmp(txt, "disable") == 0) {
459 		hostapd_wps_ap_pin_disable(hapd);
460 		return os_snprintf(buf, buflen, "OK\n");
461 	}
462 
463 	if (os_strcmp(txt, "random") == 0) {
464 		if (pos)
465 			timeout = atoi(pos);
466 		pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
467 		if (pin_txt == NULL)
468 			return -1;
469 		return os_snprintf(buf, buflen, "%s", pin_txt);
470 	}
471 
472 	if (os_strcmp(txt, "get") == 0) {
473 		pin_txt = hostapd_wps_ap_pin_get(hapd);
474 		if (pin_txt == NULL)
475 			return -1;
476 		return os_snprintf(buf, buflen, "%s", pin_txt);
477 	}
478 
479 	if (os_strcmp(txt, "set") == 0) {
480 		char *pin;
481 		if (pos == NULL)
482 			return -1;
483 		pin = pos;
484 		pos = os_strchr(pos, ' ');
485 		if (pos) {
486 			*pos++ = '\0';
487 			timeout = atoi(pos);
488 		}
489 		if (os_strlen(pin) > buflen)
490 			return -1;
491 		if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
492 			return -1;
493 		return os_snprintf(buf, buflen, "%s", pin);
494 	}
495 
496 	return -1;
497 }
498 
499 
500 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
501 {
502 	char *pos;
503 	char *ssid, *auth, *encr = NULL, *key = NULL;
504 
505 	ssid = txt;
506 	pos = os_strchr(txt, ' ');
507 	if (!pos)
508 		return -1;
509 	*pos++ = '\0';
510 
511 	auth = pos;
512 	pos = os_strchr(pos, ' ');
513 	if (pos) {
514 		*pos++ = '\0';
515 		encr = pos;
516 		pos = os_strchr(pos, ' ');
517 		if (pos) {
518 			*pos++ = '\0';
519 			key = pos;
520 		}
521 	}
522 
523 	return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
524 }
525 
526 
527 static const char * pbc_status_str(enum pbc_status status)
528 {
529 	switch (status) {
530 	case WPS_PBC_STATUS_DISABLE:
531 		return "Disabled";
532 	case WPS_PBC_STATUS_ACTIVE:
533 		return "Active";
534 	case WPS_PBC_STATUS_TIMEOUT:
535 		return "Timed-out";
536 	case WPS_PBC_STATUS_OVERLAP:
537 		return "Overlap";
538 	default:
539 		return "Unknown";
540 	}
541 }
542 
543 
544 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
545 					     char *buf, size_t buflen)
546 {
547 	int ret;
548 	char *pos, *end;
549 
550 	pos = buf;
551 	end = buf + buflen;
552 
553 	ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
554 			  pbc_status_str(hapd->wps_stats.pbc_status));
555 
556 	if (os_snprintf_error(end - pos, ret))
557 		return pos - buf;
558 	pos += ret;
559 
560 	ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
561 			  (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
562 			   "Success":
563 			   (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
564 			    "Failed" : "None")));
565 
566 	if (os_snprintf_error(end - pos, ret))
567 		return pos - buf;
568 	pos += ret;
569 
570 	/* If status == Failure - Add possible Reasons */
571 	if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
572 	   hapd->wps_stats.failure_reason > 0) {
573 		ret = os_snprintf(pos, end - pos,
574 				  "Failure Reason: %s\n",
575 				  wps_ei_str(hapd->wps_stats.failure_reason));
576 
577 		if (os_snprintf_error(end - pos, ret))
578 			return pos - buf;
579 		pos += ret;
580 	}
581 
582 	if (hapd->wps_stats.status) {
583 		ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
584 				  MAC2STR(hapd->wps_stats.peer_addr));
585 
586 		if (os_snprintf_error(end - pos, ret))
587 			return pos - buf;
588 		pos += ret;
589 	}
590 
591 	return pos - buf;
592 }
593 
594 #endif /* CONFIG_WPS */
595 
596 #ifdef CONFIG_HS20
597 
598 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
599 					     const char *cmd)
600 {
601 	u8 addr[ETH_ALEN];
602 	const char *url;
603 
604 	if (hwaddr_aton(cmd, addr))
605 		return -1;
606 	url = cmd + 17;
607 	if (*url == '\0') {
608 		url = NULL;
609 	} else {
610 		if (*url != ' ')
611 			return -1;
612 		url++;
613 		if (*url == '\0')
614 			url = NULL;
615 	}
616 
617 	return hs20_send_wnm_notification(hapd, addr, 1, url);
618 }
619 
620 
621 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
622 					      const char *cmd)
623 {
624 	u8 addr[ETH_ALEN];
625 	int code, reauth_delay, ret;
626 	const char *pos;
627 	size_t url_len;
628 	struct wpabuf *req;
629 
630 	/* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
631 	if (hwaddr_aton(cmd, addr))
632 		return -1;
633 
634 	pos = os_strchr(cmd, ' ');
635 	if (pos == NULL)
636 		return -1;
637 	pos++;
638 	code = atoi(pos);
639 
640 	pos = os_strchr(pos, ' ');
641 	if (pos == NULL)
642 		return -1;
643 	pos++;
644 	reauth_delay = atoi(pos);
645 
646 	url_len = 0;
647 	pos = os_strchr(pos, ' ');
648 	if (pos) {
649 		pos++;
650 		url_len = os_strlen(pos);
651 	}
652 
653 	req = wpabuf_alloc(4 + url_len);
654 	if (req == NULL)
655 		return -1;
656 	wpabuf_put_u8(req, code);
657 	wpabuf_put_le16(req, reauth_delay);
658 	wpabuf_put_u8(req, url_len);
659 	if (pos)
660 		wpabuf_put_data(req, pos, url_len);
661 
662 	wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
663 		   " to indicate imminent deauthentication (code=%d "
664 		   "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
665 	ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
666 	wpabuf_free(req);
667 	return ret;
668 }
669 
670 #endif /* CONFIG_HS20 */
671 
672 
673 #ifdef CONFIG_INTERWORKING
674 
675 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
676 					      const char *cmd)
677 {
678 	u8 qos_map_set[16 + 2 * 21], count = 0;
679 	const char *pos = cmd;
680 	int val, ret;
681 
682 	for (;;) {
683 		if (count == sizeof(qos_map_set)) {
684 			wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
685 			return -1;
686 		}
687 
688 		val = atoi(pos);
689 		if (val < 0 || val > 255) {
690 			wpa_printf(MSG_INFO, "Invalid QoS Map Set");
691 			return -1;
692 		}
693 
694 		qos_map_set[count++] = val;
695 		pos = os_strchr(pos, ',');
696 		if (!pos)
697 			break;
698 		pos++;
699 	}
700 
701 	if (count < 16 || count & 1) {
702 		wpa_printf(MSG_INFO, "Invalid QoS Map Set");
703 		return -1;
704 	}
705 
706 	ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
707 	if (ret) {
708 		wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
709 		return -1;
710 	}
711 
712 	os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
713 	hapd->conf->qos_map_set_len = count;
714 
715 	return 0;
716 }
717 
718 
719 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
720 						const char *cmd)
721 {
722 	u8 addr[ETH_ALEN];
723 	struct sta_info *sta;
724 	struct wpabuf *buf;
725 	u8 *qos_map_set = hapd->conf->qos_map_set;
726 	u8 qos_map_set_len = hapd->conf->qos_map_set_len;
727 	int ret;
728 
729 	if (!qos_map_set_len) {
730 		wpa_printf(MSG_INFO, "QoS Map Set is not set");
731 		return -1;
732 	}
733 
734 	if (hwaddr_aton(cmd, addr))
735 		return -1;
736 
737 	sta = ap_get_sta(hapd, addr);
738 	if (sta == NULL) {
739 		wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
740 			   "for QoS Map Configuration message",
741 			   MAC2STR(addr));
742 		return -1;
743 	}
744 
745 	if (!sta->qos_map_enabled) {
746 		wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
747 			   "support for QoS Map", MAC2STR(addr));
748 		return -1;
749 	}
750 
751 	buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
752 	if (buf == NULL)
753 		return -1;
754 
755 	wpabuf_put_u8(buf, WLAN_ACTION_QOS);
756 	wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
757 
758 	/* QoS Map Set Element */
759 	wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
760 	wpabuf_put_u8(buf, qos_map_set_len);
761 	wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
762 
763 	ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
764 				      wpabuf_head(buf), wpabuf_len(buf));
765 	wpabuf_free(buf);
766 
767 	return ret;
768 }
769 
770 #endif /* CONFIG_INTERWORKING */
771 
772 
773 #ifdef CONFIG_WNM_AP
774 
775 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
776 						const char *cmd)
777 {
778 	u8 addr[ETH_ALEN];
779 	int disassoc_timer;
780 	struct sta_info *sta;
781 
782 	if (hwaddr_aton(cmd, addr))
783 		return -1;
784 	if (cmd[17] != ' ')
785 		return -1;
786 	disassoc_timer = atoi(cmd + 17);
787 
788 	sta = ap_get_sta(hapd, addr);
789 	if (sta == NULL) {
790 		wpa_printf(MSG_DEBUG, "Station " MACSTR
791 			   " not found for disassociation imminent message",
792 			   MAC2STR(addr));
793 		return -1;
794 	}
795 
796 	return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
797 }
798 
799 
800 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
801 					   const char *cmd)
802 {
803 	u8 addr[ETH_ALEN];
804 	const char *url, *timerstr;
805 	int disassoc_timer;
806 	struct sta_info *sta;
807 
808 	if (hwaddr_aton(cmd, addr))
809 		return -1;
810 
811 	sta = ap_get_sta(hapd, addr);
812 	if (sta == NULL) {
813 		wpa_printf(MSG_DEBUG, "Station " MACSTR
814 			   " not found for ESS disassociation imminent message",
815 			   MAC2STR(addr));
816 		return -1;
817 	}
818 
819 	timerstr = cmd + 17;
820 	if (*timerstr != ' ')
821 		return -1;
822 	timerstr++;
823 	disassoc_timer = atoi(timerstr);
824 	if (disassoc_timer < 0 || disassoc_timer > 65535)
825 		return -1;
826 
827 	url = os_strchr(timerstr, ' ');
828 	if (url == NULL)
829 		return -1;
830 	url++;
831 
832 	return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
833 }
834 
835 
836 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
837 					 const char *cmd)
838 {
839 	u8 addr[ETH_ALEN];
840 	const char *pos, *end;
841 	int disassoc_timer = 0;
842 	struct sta_info *sta;
843 	u8 req_mode = 0, valid_int = 0x01;
844 	u8 bss_term_dur[12];
845 	char *url = NULL;
846 	int ret;
847 	u8 nei_rep[1000];
848 	int nei_len;
849 	u8 mbo[10];
850 	size_t mbo_len = 0;
851 
852 	if (hwaddr_aton(cmd, addr)) {
853 		wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
854 		return -1;
855 	}
856 
857 	sta = ap_get_sta(hapd, addr);
858 	if (sta == NULL) {
859 		wpa_printf(MSG_DEBUG, "Station " MACSTR
860 			   " not found for BSS TM Request message",
861 			   MAC2STR(addr));
862 		return -1;
863 	}
864 
865 	pos = os_strstr(cmd, " disassoc_timer=");
866 	if (pos) {
867 		pos += 16;
868 		disassoc_timer = atoi(pos);
869 		if (disassoc_timer < 0 || disassoc_timer > 65535) {
870 			wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
871 			return -1;
872 		}
873 	}
874 
875 	pos = os_strstr(cmd, " valid_int=");
876 	if (pos) {
877 		pos += 11;
878 		valid_int = atoi(pos);
879 	}
880 
881 	pos = os_strstr(cmd, " bss_term=");
882 	if (pos) {
883 		pos += 10;
884 		req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
885 		/* TODO: TSF configurable/learnable */
886 		bss_term_dur[0] = 4; /* Subelement ID */
887 		bss_term_dur[1] = 10; /* Length */
888 		os_memset(&bss_term_dur[2], 0, 8);
889 		end = os_strchr(pos, ',');
890 		if (end == NULL) {
891 			wpa_printf(MSG_DEBUG, "Invalid bss_term data");
892 			return -1;
893 		}
894 		end++;
895 		WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
896 	}
897 
898 	nei_len = ieee802_11_parse_candidate_list(cmd, nei_rep,
899 						  sizeof(nei_rep));
900 	if (nei_len < 0)
901 		return -1;
902 
903 	pos = os_strstr(cmd, " url=");
904 	if (pos) {
905 		size_t len;
906 		pos += 5;
907 		end = os_strchr(pos, ' ');
908 		if (end)
909 			len = end - pos;
910 		else
911 			len = os_strlen(pos);
912 		url = os_malloc(len + 1);
913 		if (url == NULL)
914 			return -1;
915 		os_memcpy(url, pos, len);
916 		url[len] = '\0';
917 		req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
918 	}
919 
920 	if (os_strstr(cmd, " pref=1"))
921 		req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
922 	if (os_strstr(cmd, " abridged=1"))
923 		req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
924 	if (os_strstr(cmd, " disassoc_imminent=1"))
925 		req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
926 
927 #ifdef CONFIG_MBO
928 	pos = os_strstr(cmd, "mbo=");
929 	if (pos) {
930 		unsigned int mbo_reason, cell_pref, reassoc_delay;
931 		u8 *mbo_pos = mbo;
932 
933 		ret = sscanf(pos, "mbo=%u:%u:%u", &mbo_reason,
934 			     &reassoc_delay, &cell_pref);
935 		if (ret != 3) {
936 			wpa_printf(MSG_DEBUG,
937 				   "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
938 			ret = -1;
939 			goto fail;
940 		}
941 
942 		if (mbo_reason > MBO_TRANSITION_REASON_PREMIUM_AP) {
943 			wpa_printf(MSG_DEBUG,
944 				   "Invalid MBO transition reason code %u",
945 				   mbo_reason);
946 			ret = -1;
947 			goto fail;
948 		}
949 
950 		/* Valid values for Cellular preference are: 0, 1, 255 */
951 		if (cell_pref != 0 && cell_pref != 1 && cell_pref != 255) {
952 			wpa_printf(MSG_DEBUG,
953 				   "Invalid MBO cellular capability %u",
954 				   cell_pref);
955 			ret = -1;
956 			goto fail;
957 		}
958 
959 		if (reassoc_delay > 65535 ||
960 		    (reassoc_delay &&
961 		     !(req_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT))) {
962 			wpa_printf(MSG_DEBUG,
963 				   "MBO: Assoc retry delay is only valid in disassoc imminent mode");
964 			ret = -1;
965 			goto fail;
966 		}
967 
968 		*mbo_pos++ = MBO_ATTR_ID_TRANSITION_REASON;
969 		*mbo_pos++ = 1;
970 		*mbo_pos++ = mbo_reason;
971 		*mbo_pos++ = MBO_ATTR_ID_CELL_DATA_PREF;
972 		*mbo_pos++ = 1;
973 		*mbo_pos++ = cell_pref;
974 
975 		if (reassoc_delay) {
976 			*mbo_pos++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY;
977 			*mbo_pos++ = 2;
978 			WPA_PUT_LE16(mbo_pos, reassoc_delay);
979 			mbo_pos += 2;
980 		}
981 
982 		mbo_len = mbo_pos - mbo;
983 	}
984 #endif /* CONFIG_MBO */
985 
986 	ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
987 				  valid_int, bss_term_dur, url,
988 				  nei_len ? nei_rep : NULL, nei_len,
989 				  mbo_len ? mbo : NULL, mbo_len);
990 #ifdef CONFIG_MBO
991 fail:
992 #endif /* CONFIG_MBO */
993 	os_free(url);
994 	return ret;
995 }
996 
997 
998 static int hostapd_ctrl_iface_coloc_intf_req(struct hostapd_data *hapd,
999 					     const char *cmd)
1000 {
1001 	u8 addr[ETH_ALEN];
1002 	struct sta_info *sta;
1003 	const char *pos;
1004 	unsigned int auto_report, timeout;
1005 
1006 	if (hwaddr_aton(cmd, addr)) {
1007 		wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
1008 		return -1;
1009 	}
1010 
1011 	sta = ap_get_sta(hapd, addr);
1012 	if (!sta) {
1013 		wpa_printf(MSG_DEBUG, "Station " MACSTR
1014 			   " not found for Collocated Interference Request",
1015 			   MAC2STR(addr));
1016 		return -1;
1017 	}
1018 
1019 	pos = cmd + 17;
1020 	if (*pos != ' ')
1021 		return -1;
1022 	pos++;
1023 	auto_report = atoi(pos);
1024 	pos = os_strchr(pos, ' ');
1025 	if (!pos)
1026 		return -1;
1027 	pos++;
1028 	timeout = atoi(pos);
1029 
1030 	return wnm_send_coloc_intf_req(hapd, sta, auto_report, timeout);
1031 }
1032 
1033 #endif /* CONFIG_WNM_AP */
1034 
1035 
1036 static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
1037 					   char *buf, size_t buflen)
1038 {
1039 	int ret = 0;
1040 	char *pos, *end;
1041 
1042 	pos = buf;
1043 	end = buf + buflen;
1044 
1045 	WPA_ASSERT(hapd->conf->wpa_key_mgmt);
1046 
1047 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1048 		ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1049 		if (os_snprintf_error(end - pos, ret))
1050 			return pos - buf;
1051 		pos += ret;
1052 	}
1053 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1054 		ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1055 		if (os_snprintf_error(end - pos, ret))
1056 			return pos - buf;
1057 		pos += ret;
1058 	}
1059 #ifdef CONFIG_IEEE80211R_AP
1060 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1061 		ret = os_snprintf(pos, end - pos, "FT-PSK ");
1062 		if (os_snprintf_error(end - pos, ret))
1063 			return pos - buf;
1064 		pos += ret;
1065 	}
1066 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1067 		ret = os_snprintf(pos, end - pos, "FT-EAP ");
1068 		if (os_snprintf_error(end - pos, ret))
1069 			return pos - buf;
1070 		pos += ret;
1071 	}
1072 #ifdef CONFIG_SHA384
1073 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
1074 		ret = os_snprintf(pos, end - pos, "FT-EAP-SHA384 ");
1075 		if (os_snprintf_error(end - pos, ret))
1076 			return pos - buf;
1077 		pos += ret;
1078 	}
1079 #endif /* CONFIG_SHA384 */
1080 #ifdef CONFIG_SAE
1081 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1082 		ret = os_snprintf(pos, end - pos, "FT-SAE ");
1083 		if (os_snprintf_error(end - pos, ret))
1084 			return pos - buf;
1085 		pos += ret;
1086 	}
1087 #endif /* CONFIG_SAE */
1088 #ifdef CONFIG_FILS
1089 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1090 		ret = os_snprintf(pos, end - pos, "FT-FILS-SHA256 ");
1091 		if (os_snprintf_error(end - pos, ret))
1092 			return pos - buf;
1093 		pos += ret;
1094 	}
1095 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1096 		ret = os_snprintf(pos, end - pos, "FT-FILS-SHA384 ");
1097 		if (os_snprintf_error(end - pos, ret))
1098 			return pos - buf;
1099 		pos += ret;
1100 	}
1101 #endif /* CONFIG_FILS */
1102 #endif /* CONFIG_IEEE80211R_AP */
1103 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1104 		ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1105 		if (os_snprintf_error(end - pos, ret))
1106 			return pos - buf;
1107 		pos += ret;
1108 	}
1109 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1110 		ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1111 		if (os_snprintf_error(end - pos, ret))
1112 			return pos - buf;
1113 		pos += ret;
1114 	}
1115 #ifdef CONFIG_SAE
1116 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1117 		ret = os_snprintf(pos, end - pos, "SAE ");
1118 		if (os_snprintf_error(end - pos, ret))
1119 			return pos - buf;
1120 		pos += ret;
1121 	}
1122 #endif /* CONFIG_SAE */
1123 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1124 		ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1125 		if (os_snprintf_error(end - pos, ret))
1126 			return pos - buf;
1127 		pos += ret;
1128 	}
1129 	if (hapd->conf->wpa_key_mgmt &
1130 	    WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1131 		ret = os_snprintf(pos, end - pos,
1132 				  "WPA-EAP-SUITE-B-192 ");
1133 		if (os_snprintf_error(end - pos, ret))
1134 			return pos - buf;
1135 		pos += ret;
1136 	}
1137 #ifdef CONFIG_FILS
1138 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
1139 		ret = os_snprintf(pos, end - pos, "FILS-SHA256 ");
1140 		if (os_snprintf_error(end - pos, ret))
1141 			return pos - buf;
1142 		pos += ret;
1143 	}
1144 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1145 		ret = os_snprintf(pos, end - pos, "FILS-SHA384 ");
1146 		if (os_snprintf_error(end - pos, ret))
1147 			return pos - buf;
1148 		pos += ret;
1149 	}
1150 #endif /* CONFIG_FILS */
1151 
1152 #ifdef CONFIG_OWE
1153 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) {
1154 		ret = os_snprintf(pos, end - pos, "OWE ");
1155 		if (os_snprintf_error(end - pos, ret))
1156 			return pos - buf;
1157 		pos += ret;
1158 	}
1159 #endif /* CONFIG_OWE */
1160 
1161 #ifdef CONFIG_DPP
1162 	if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) {
1163 		ret = os_snprintf(pos, end - pos, "DPP ");
1164 		if (os_snprintf_error(end - pos, ret))
1165 			return pos - buf;
1166 		pos += ret;
1167 	}
1168 #endif /* CONFIG_DPP */
1169 
1170 	if (pos > buf && *(pos - 1) == ' ') {
1171 		*(pos - 1) = '\0';
1172 		pos--;
1173 	}
1174 
1175 	return pos - buf;
1176 }
1177 
1178 
1179 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1180 					 char *buf, size_t buflen)
1181 {
1182 	int ret;
1183 	char *pos, *end;
1184 
1185 	pos = buf;
1186 	end = buf + buflen;
1187 
1188 	ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1189 			  "ssid=%s\n",
1190 			  MAC2STR(hapd->own_addr),
1191 			  wpa_ssid_txt(hapd->conf->ssid.ssid,
1192 				       hapd->conf->ssid.ssid_len));
1193 	if (os_snprintf_error(end - pos, ret))
1194 		return pos - buf;
1195 	pos += ret;
1196 
1197 #ifdef CONFIG_WPS
1198 	ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1199 			  hapd->conf->wps_state == 0 ? "disabled" :
1200 			  (hapd->conf->wps_state == 1 ? "not configured" :
1201 			   "configured"));
1202 	if (os_snprintf_error(end - pos, ret))
1203 		return pos - buf;
1204 	pos += ret;
1205 
1206 	if (hapd->conf->wps_state && hapd->conf->wpa &&
1207 	    hapd->conf->ssid.wpa_passphrase) {
1208 		ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1209 				  hapd->conf->ssid.wpa_passphrase);
1210 		if (os_snprintf_error(end - pos, ret))
1211 			return pos - buf;
1212 		pos += ret;
1213 	}
1214 
1215 	if (hapd->conf->wps_state && hapd->conf->wpa &&
1216 	    hapd->conf->ssid.wpa_psk &&
1217 	    hapd->conf->ssid.wpa_psk->group) {
1218 		char hex[PMK_LEN * 2 + 1];
1219 		wpa_snprintf_hex(hex, sizeof(hex),
1220 				 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1221 		ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
1222 		if (os_snprintf_error(end - pos, ret))
1223 			return pos - buf;
1224 		pos += ret;
1225 	}
1226 
1227 	if (hapd->conf->multi_ap) {
1228 		struct hostapd_ssid *ssid = &hapd->conf->multi_ap_backhaul_ssid;
1229 
1230 		ret = os_snprintf(pos, end - pos, "multi_ap=%d\n",
1231 				  hapd->conf->multi_ap);
1232 		if (os_snprintf_error(end - pos, ret))
1233 			return pos - buf;
1234 		pos += ret;
1235 
1236 		if (ssid->ssid_len) {
1237 			ret = os_snprintf(pos, end - pos,
1238 					  "multi_ap_backhaul_ssid=%s\n",
1239 					  wpa_ssid_txt(ssid->ssid,
1240 						       ssid->ssid_len));
1241 			if (os_snprintf_error(end - pos, ret))
1242 				return pos - buf;
1243 			pos += ret;
1244 		}
1245 
1246 		if (hapd->conf->wps_state && hapd->conf->wpa &&
1247 			ssid->wpa_passphrase) {
1248 			ret = os_snprintf(pos, end - pos,
1249 					  "multi_ap_backhaul_wpa_passphrase=%s\n",
1250 					  ssid->wpa_passphrase);
1251 			if (os_snprintf_error(end - pos, ret))
1252 				return pos - buf;
1253 			pos += ret;
1254 		}
1255 
1256 		if (hapd->conf->wps_state && hapd->conf->wpa &&
1257 		    ssid->wpa_psk &&
1258 		    ssid->wpa_psk->group) {
1259 			char hex[PMK_LEN * 2 + 1];
1260 
1261 			wpa_snprintf_hex(hex, sizeof(hex), ssid->wpa_psk->psk,
1262 					 PMK_LEN);
1263 			ret = os_snprintf(pos, end - pos,
1264 					  "multi_ap_backhaul_wpa_psk=%s\n",
1265 					  hex);
1266 			forced_memzero(hex, sizeof(hex));
1267 			if (os_snprintf_error(end - pos, ret))
1268 				return pos - buf;
1269 			pos += ret;
1270 		}
1271 	}
1272 #endif /* CONFIG_WPS */
1273 
1274 	if (hapd->conf->wpa) {
1275 		ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1276 		if (os_snprintf_error(end - pos, ret))
1277 			return pos - buf;
1278 		pos += ret;
1279 	}
1280 
1281 	if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1282 		ret = os_snprintf(pos, end - pos, "key_mgmt=");
1283 		if (os_snprintf_error(end - pos, ret))
1284 			return pos - buf;
1285 		pos += ret;
1286 
1287 		pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
1288 
1289 		ret = os_snprintf(pos, end - pos, "\n");
1290 		if (os_snprintf_error(end - pos, ret))
1291 			return pos - buf;
1292 		pos += ret;
1293 	}
1294 
1295 	if (hapd->conf->wpa) {
1296 		ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1297 				  wpa_cipher_txt(hapd->conf->wpa_group));
1298 		if (os_snprintf_error(end - pos, ret))
1299 			return pos - buf;
1300 		pos += ret;
1301 	}
1302 
1303 	if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1304 		ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
1305 		if (os_snprintf_error(end - pos, ret))
1306 			return pos - buf;
1307 		pos += ret;
1308 
1309 		ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1310 					" ");
1311 		if (ret < 0)
1312 			return pos - buf;
1313 		pos += ret;
1314 
1315 		ret = os_snprintf(pos, end - pos, "\n");
1316 		if (os_snprintf_error(end - pos, ret))
1317 			return pos - buf;
1318 		pos += ret;
1319 	}
1320 
1321 	if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1322 		ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
1323 		if (os_snprintf_error(end - pos, ret))
1324 			return pos - buf;
1325 		pos += ret;
1326 
1327 		ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
1328 					" ");
1329 		if (ret < 0)
1330 			return pos - buf;
1331 		pos += ret;
1332 
1333 		ret = os_snprintf(pos, end - pos, "\n");
1334 		if (os_snprintf_error(end - pos, ret))
1335 			return pos - buf;
1336 		pos += ret;
1337 	}
1338 
1339 	if (hapd->conf->wpa && hapd->conf->wpa_deny_ptk0_rekey) {
1340 		ret = os_snprintf(pos, end - pos, "wpa_deny_ptk0_rekey=%d\n",
1341 				  hapd->conf->wpa_deny_ptk0_rekey);
1342 		if (os_snprintf_error(end - pos, ret))
1343 			return pos - buf;
1344 		pos += ret;
1345 	}
1346 
1347 	if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->extended_key_id) {
1348 		ret = os_snprintf(pos, end - pos, "extended_key_id=%d\n",
1349 				  hapd->conf->extended_key_id);
1350 		if (os_snprintf_error(end - pos, ret))
1351 			return pos - buf;
1352 		pos += ret;
1353 	}
1354 
1355 	return pos - buf;
1356 }
1357 
1358 
1359 static void hostapd_disassoc_accept_mac(struct hostapd_data *hapd)
1360 {
1361 	struct sta_info *sta;
1362 	struct vlan_description vlan_id;
1363 
1364 	if (hapd->conf->macaddr_acl != DENY_UNLESS_ACCEPTED)
1365 		return;
1366 
1367 	for (sta = hapd->sta_list; sta; sta = sta->next) {
1368 		if (!hostapd_maclist_found(hapd->conf->accept_mac,
1369 					   hapd->conf->num_accept_mac,
1370 					   sta->addr, &vlan_id) ||
1371 		    (vlan_id.notempty &&
1372 		     vlan_compare(&vlan_id, sta->vlan_desc)))
1373 			ap_sta_disconnect(hapd, sta, sta->addr,
1374 					  WLAN_REASON_UNSPECIFIED);
1375 	}
1376 }
1377 
1378 
1379 static void hostapd_disassoc_deny_mac(struct hostapd_data *hapd)
1380 {
1381 	struct sta_info *sta;
1382 	struct vlan_description vlan_id;
1383 
1384 	for (sta = hapd->sta_list; sta; sta = sta->next) {
1385 		if (hostapd_maclist_found(hapd->conf->deny_mac,
1386 					  hapd->conf->num_deny_mac, sta->addr,
1387 					  &vlan_id) &&
1388 		    (!vlan_id.notempty ||
1389 		     !vlan_compare(&vlan_id, sta->vlan_desc)))
1390 			ap_sta_disconnect(hapd, sta, sta->addr,
1391 					  WLAN_REASON_UNSPECIFIED);
1392 	}
1393 }
1394 
1395 
1396 static int hostapd_ctrl_iface_set_band(struct hostapd_data *hapd,
1397 				       const char *bands)
1398 {
1399 	union wpa_event_data event;
1400 	u32 setband_mask = WPA_SETBAND_AUTO;
1401 
1402 	/*
1403 	 * For example:
1404 	 *  SET setband 2G,6G
1405 	 *  SET setband 5G
1406 	 *  SET setband AUTO
1407 	 */
1408 	if (!os_strstr(bands, "AUTO")) {
1409 		if (os_strstr(bands, "5G"))
1410 			setband_mask |= WPA_SETBAND_5G;
1411 		if (os_strstr(bands, "6G"))
1412 			setband_mask |= WPA_SETBAND_6G;
1413 		if (os_strstr(bands, "2G"))
1414 			setband_mask |= WPA_SETBAND_2G;
1415 		if (setband_mask == WPA_SETBAND_AUTO)
1416 			return -1;
1417 	}
1418 
1419 	if (hostapd_drv_set_band(hapd, setband_mask) == 0) {
1420 		os_memset(&event, 0, sizeof(event));
1421 		event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
1422 		event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
1423 		wpa_supplicant_event(hapd, EVENT_CHANNEL_LIST_CHANGED, &event);
1424 	}
1425 
1426 	return 0;
1427 }
1428 
1429 
1430 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1431 {
1432 	char *value;
1433 	int ret = 0;
1434 
1435 	value = os_strchr(cmd, ' ');
1436 	if (value == NULL)
1437 		return -1;
1438 	*value++ = '\0';
1439 
1440 	wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1441 	if (0) {
1442 #ifdef CONFIG_WPS_TESTING
1443 	} else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1444 		long int val;
1445 		val = strtol(value, NULL, 0);
1446 		if (val < 0 || val > 0xff) {
1447 			ret = -1;
1448 			wpa_printf(MSG_DEBUG, "WPS: Invalid "
1449 				   "wps_version_number %ld", val);
1450 		} else {
1451 			wps_version_number = val;
1452 			wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1453 				   "version %u.%u",
1454 				   (wps_version_number & 0xf0) >> 4,
1455 				   wps_version_number & 0x0f);
1456 			hostapd_wps_update_ie(hapd);
1457 		}
1458 	} else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1459 		wps_testing_dummy_cred = atoi(value);
1460 		wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1461 			   wps_testing_dummy_cred);
1462 	} else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1463 		wps_corrupt_pkhash = atoi(value);
1464 		wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1465 			   wps_corrupt_pkhash);
1466 #endif /* CONFIG_WPS_TESTING */
1467 #ifdef CONFIG_TESTING_OPTIONS
1468 	} else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1469 		hapd->ext_mgmt_frame_handling = atoi(value);
1470 	} else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1471 		hapd->ext_eapol_frame_io = atoi(value);
1472 	} else if (os_strcasecmp(cmd, "force_backlog_bytes") == 0) {
1473 		hapd->force_backlog_bytes = atoi(value);
1474 #ifdef CONFIG_DPP
1475 	} else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
1476 		os_free(hapd->dpp_config_obj_override);
1477 		hapd->dpp_config_obj_override = os_strdup(value);
1478 	} else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
1479 		os_free(hapd->dpp_discovery_override);
1480 		hapd->dpp_discovery_override = os_strdup(value);
1481 	} else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
1482 		os_free(hapd->dpp_groups_override);
1483 		hapd->dpp_groups_override = os_strdup(value);
1484 	} else if (os_strcasecmp(cmd,
1485 				 "dpp_ignore_netaccesskey_mismatch") == 0) {
1486 		hapd->dpp_ignore_netaccesskey_mismatch = atoi(value);
1487 	} else if (os_strcasecmp(cmd, "dpp_test") == 0) {
1488 		dpp_test = atoi(value);
1489 	} else if (os_strcasecmp(cmd, "dpp_version_override") == 0) {
1490 		dpp_version_override = atoi(value);
1491 #endif /* CONFIG_DPP */
1492 #endif /* CONFIG_TESTING_OPTIONS */
1493 #ifdef CONFIG_MBO
1494 	} else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1495 		int val;
1496 
1497 		if (!hapd->conf->mbo_enabled)
1498 			return -1;
1499 
1500 		val = atoi(value);
1501 		if (val < 0 || val > 1)
1502 			return -1;
1503 
1504 		hapd->mbo_assoc_disallow = val;
1505 		ieee802_11_update_beacons(hapd->iface);
1506 
1507 		/*
1508 		 * TODO: Need to configure drivers that do AP MLME offload with
1509 		 * disallowing station logic.
1510 		 */
1511 #endif /* CONFIG_MBO */
1512 #ifdef CONFIG_DPP
1513 	} else if (os_strcasecmp(cmd, "dpp_configurator_params") == 0) {
1514 		os_free(hapd->dpp_configurator_params);
1515 		hapd->dpp_configurator_params = os_strdup(value);
1516 	} else if (os_strcasecmp(cmd, "dpp_init_max_tries") == 0) {
1517 		hapd->dpp_init_max_tries = atoi(value);
1518 	} else if (os_strcasecmp(cmd, "dpp_init_retry_time") == 0) {
1519 		hapd->dpp_init_retry_time = atoi(value);
1520 	} else if (os_strcasecmp(cmd, "dpp_resp_wait_time") == 0) {
1521 		hapd->dpp_resp_wait_time = atoi(value);
1522 	} else if (os_strcasecmp(cmd, "dpp_resp_max_tries") == 0) {
1523 		hapd->dpp_resp_max_tries = atoi(value);
1524 	} else if (os_strcasecmp(cmd, "dpp_resp_retry_time") == 0) {
1525 		hapd->dpp_resp_retry_time = atoi(value);
1526 #endif /* CONFIG_DPP */
1527 	} else if (os_strcasecmp(cmd, "setband") == 0) {
1528 		ret = hostapd_ctrl_iface_set_band(hapd, value);
1529 	} else {
1530 		ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1531 		if (ret)
1532 			return ret;
1533 
1534 		if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1535 			hostapd_disassoc_deny_mac(hapd);
1536 		} else if (os_strcasecmp(cmd, "accept_mac_file") == 0) {
1537 			hostapd_disassoc_accept_mac(hapd);
1538 		} else if (os_strncmp(cmd, "wme_ac_", 7) == 0 ||
1539 			   os_strncmp(cmd, "wmm_ac_", 7) == 0) {
1540 			hapd->parameter_set_count++;
1541 			if (ieee802_11_update_beacons(hapd->iface))
1542 				wpa_printf(MSG_DEBUG,
1543 					   "Failed to update beacons with WMM parameters");
1544 		} else if (os_strcmp(cmd, "wpa_passphrase") == 0 ||
1545 			   os_strcmp(cmd, "sae_password") == 0 ||
1546 			   os_strcmp(cmd, "sae_pwe") == 0) {
1547 			if (hapd->started)
1548 				hostapd_setup_sae_pt(hapd->conf);
1549 		} else if (os_strcasecmp(cmd, "transition_disable") == 0) {
1550 			wpa_auth_set_transition_disable(hapd->wpa_auth,
1551 							hapd->conf->transition_disable);
1552 		}
1553 
1554 #ifdef CONFIG_TESTING_OPTIONS
1555 		if (os_strcmp(cmd, "ft_rsnxe_used") == 0)
1556 			wpa_auth_set_ft_rsnxe_used(hapd->wpa_auth,
1557 						   hapd->conf->ft_rsnxe_used);
1558 		else if (os_strcmp(cmd, "oci_freq_override_eapol_m3") == 0)
1559 			wpa_auth_set_ocv_override_freq(
1560 				hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_M3,
1561 				atoi(value));
1562 		else if (os_strcmp(cmd, "oci_freq_override_eapol_g1") == 0)
1563 			wpa_auth_set_ocv_override_freq(
1564 				hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_EAPOL_G1,
1565 				atoi(value));
1566 		else if (os_strcmp(cmd, "oci_freq_override_ft_assoc") == 0)
1567 			wpa_auth_set_ocv_override_freq(
1568 				hapd->wpa_auth, WPA_AUTH_OCV_OVERRIDE_FT_ASSOC,
1569 				atoi(value));
1570 		else if (os_strcmp(cmd, "oci_freq_override_fils_assoc") == 0)
1571 			wpa_auth_set_ocv_override_freq(
1572 				hapd->wpa_auth,
1573 				WPA_AUTH_OCV_OVERRIDE_FILS_ASSOC, atoi(value));
1574 #endif /* CONFIG_TESTING_OPTIONS */
1575 	}
1576 
1577 	return ret;
1578 }
1579 
1580 
1581 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1582 				  char *buf, size_t buflen)
1583 {
1584 	int res;
1585 
1586 	wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1587 
1588 	if (os_strcmp(cmd, "version") == 0) {
1589 		res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1590 		if (os_snprintf_error(buflen, res))
1591 			return -1;
1592 		return res;
1593 	} else if (os_strcmp(cmd, "tls_library") == 0) {
1594 		res = tls_get_library_version(buf, buflen);
1595 		if (os_snprintf_error(buflen, res))
1596 			return -1;
1597 		return res;
1598 	}
1599 
1600 	return -1;
1601 }
1602 
1603 
1604 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1605 {
1606 	if (hostapd_enable_iface(iface) < 0) {
1607 		wpa_printf(MSG_ERROR, "Enabling of interface failed");
1608 		return -1;
1609 	}
1610 	return 0;
1611 }
1612 
1613 
1614 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1615 {
1616 	if (hostapd_reload_iface(iface) < 0) {
1617 		wpa_printf(MSG_ERROR, "Reloading of interface failed");
1618 		return -1;
1619 	}
1620 	return 0;
1621 }
1622 
1623 
1624 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1625 {
1626 	if (hostapd_disable_iface(iface) < 0) {
1627 		wpa_printf(MSG_ERROR, "Disabling of interface failed");
1628 		return -1;
1629 	}
1630 	return 0;
1631 }
1632 
1633 
1634 static int
1635 hostapd_ctrl_iface_kick_mismatch_psk_sta_iter(struct hostapd_data *hapd,
1636 					      struct sta_info *sta, void *ctx)
1637 {
1638 	struct hostapd_wpa_psk *psk;
1639 	const u8 *pmk;
1640 	int pmk_len;
1641 	int pmk_match;
1642 	int sta_match;
1643 	int bss_match;
1644 	int reason;
1645 
1646 	pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1647 
1648 	for (psk = hapd->conf->ssid.wpa_psk; pmk && psk; psk = psk->next) {
1649 		pmk_match = PMK_LEN == pmk_len &&
1650 			os_memcmp(psk->psk, pmk, pmk_len) == 0;
1651 		sta_match = psk->group == 0 &&
1652 			os_memcmp(sta->addr, psk->addr, ETH_ALEN) == 0;
1653 		bss_match = psk->group == 1;
1654 
1655 		if (pmk_match && (sta_match || bss_match))
1656 			return 0;
1657 	}
1658 
1659 	wpa_printf(MSG_INFO, "STA " MACSTR
1660 		   " PSK/passphrase no longer valid - disconnect",
1661 		   MAC2STR(sta->addr));
1662 	reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1663 	hostapd_drv_sta_deauth(hapd, sta->addr, reason);
1664 	ap_sta_deauthenticate(hapd, sta, reason);
1665 
1666 	return 0;
1667 }
1668 
1669 
1670 static int hostapd_ctrl_iface_reload_wpa_psk(struct hostapd_data *hapd)
1671 {
1672 	struct hostapd_bss_config *conf = hapd->conf;
1673 	int err;
1674 
1675 	hostapd_config_clear_wpa_psk(&conf->ssid.wpa_psk);
1676 
1677 	err = hostapd_setup_wpa_psk(conf);
1678 	if (err < 0) {
1679 		wpa_printf(MSG_ERROR, "Reloading WPA-PSK passwords failed: %d",
1680 			   err);
1681 		return -1;
1682 	}
1683 
1684 	ap_for_each_sta(hapd, hostapd_ctrl_iface_kick_mismatch_psk_sta_iter,
1685 			NULL);
1686 
1687 	return 0;
1688 }
1689 
1690 
1691 #ifdef CONFIG_TESTING_OPTIONS
1692 
1693 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1694 {
1695 	union wpa_event_data data;
1696 	char *pos, *param;
1697 	enum wpa_event_type event;
1698 
1699 	wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1700 
1701 	os_memset(&data, 0, sizeof(data));
1702 
1703 	param = os_strchr(cmd, ' ');
1704 	if (param == NULL)
1705 		return -1;
1706 	*param++ = '\0';
1707 
1708 	if (os_strcmp(cmd, "DETECTED") == 0)
1709 		event = EVENT_DFS_RADAR_DETECTED;
1710 	else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1711 		event = EVENT_DFS_CAC_FINISHED;
1712 	else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1713 		event = EVENT_DFS_CAC_ABORTED;
1714 	else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1715 		event = EVENT_DFS_NOP_FINISHED;
1716 	else {
1717 		wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1718 			   cmd);
1719 		return -1;
1720 	}
1721 
1722 	pos = os_strstr(param, "freq=");
1723 	if (pos)
1724 		data.dfs_event.freq = atoi(pos + 5);
1725 
1726 	pos = os_strstr(param, "ht_enabled=1");
1727 	if (pos)
1728 		data.dfs_event.ht_enabled = 1;
1729 
1730 	pos = os_strstr(param, "chan_offset=");
1731 	if (pos)
1732 		data.dfs_event.chan_offset = atoi(pos + 12);
1733 
1734 	pos = os_strstr(param, "chan_width=");
1735 	if (pos)
1736 		data.dfs_event.chan_width = atoi(pos + 11);
1737 
1738 	pos = os_strstr(param, "cf1=");
1739 	if (pos)
1740 		data.dfs_event.cf1 = atoi(pos + 4);
1741 
1742 	pos = os_strstr(param, "cf2=");
1743 	if (pos)
1744 		data.dfs_event.cf2 = atoi(pos + 4);
1745 
1746 	wpa_supplicant_event(hapd, event, &data);
1747 
1748 	return 0;
1749 }
1750 
1751 
1752 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1753 {
1754 	size_t len;
1755 	u8 *buf;
1756 	int res;
1757 
1758 	wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1759 
1760 	len = os_strlen(cmd);
1761 	if (len & 1)
1762 		return -1;
1763 	len /= 2;
1764 
1765 	buf = os_malloc(len);
1766 	if (buf == NULL)
1767 		return -1;
1768 
1769 	if (hexstr2bin(cmd, buf, len) < 0) {
1770 		os_free(buf);
1771 		return -1;
1772 	}
1773 
1774 	res = hostapd_drv_send_mlme(hapd, buf, len, 0, NULL, 0, 0);
1775 	os_free(buf);
1776 	return res;
1777 }
1778 
1779 
1780 static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data *hapd,
1781 						     char *cmd)
1782 {
1783 	char *pos, *param;
1784 	size_t len;
1785 	u8 *buf;
1786 	int stype = 0, ok = 0;
1787 	union wpa_event_data event;
1788 
1789 	if (!hapd->ext_mgmt_frame_handling)
1790 		return -1;
1791 
1792 	/* stype=<val> ok=<0/1> buf=<frame hexdump> */
1793 
1794 	wpa_printf(MSG_DEBUG, "External MGMT TX status process: %s", cmd);
1795 
1796 	pos = cmd;
1797 	param = os_strstr(pos, "stype=");
1798 	if (param) {
1799 		param += 6;
1800 		stype = atoi(param);
1801 	}
1802 
1803 	param = os_strstr(pos, " ok=");
1804 	if (param) {
1805 		param += 4;
1806 		ok = atoi(param);
1807 	}
1808 
1809 	param = os_strstr(pos, " buf=");
1810 	if (!param)
1811 		return -1;
1812 	param += 5;
1813 
1814 	len = os_strlen(param);
1815 	if (len & 1)
1816 		return -1;
1817 	len /= 2;
1818 
1819 	buf = os_malloc(len);
1820 	if (!buf || hexstr2bin(param, buf, len) < 0) {
1821 		os_free(buf);
1822 		return -1;
1823 	}
1824 
1825 	os_memset(&event, 0, sizeof(event));
1826 	event.tx_status.type = WLAN_FC_TYPE_MGMT;
1827 	event.tx_status.data = buf;
1828 	event.tx_status.data_len = len;
1829 	event.tx_status.stype = stype;
1830 	event.tx_status.ack = ok;
1831 	hapd->ext_mgmt_frame_handling = 0;
1832 	wpa_supplicant_event(hapd, EVENT_TX_STATUS, &event);
1833 	hapd->ext_mgmt_frame_handling = 1;
1834 
1835 	os_free(buf);
1836 
1837 	return 0;
1838 }
1839 
1840 
1841 static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data *hapd,
1842 					      char *cmd)
1843 {
1844 	char *pos, *param;
1845 	size_t len;
1846 	u8 *buf;
1847 	int freq = 0, datarate = 0, ssi_signal = 0;
1848 	union wpa_event_data event;
1849 
1850 	if (!hapd->ext_mgmt_frame_handling)
1851 		return -1;
1852 
1853 	/* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1854 
1855 	wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
1856 
1857 	pos = cmd;
1858 	param = os_strstr(pos, "freq=");
1859 	if (param) {
1860 		param += 5;
1861 		freq = atoi(param);
1862 	}
1863 
1864 	param = os_strstr(pos, " datarate=");
1865 	if (param) {
1866 		param += 10;
1867 		datarate = atoi(param);
1868 	}
1869 
1870 	param = os_strstr(pos, " ssi_signal=");
1871 	if (param) {
1872 		param += 12;
1873 		ssi_signal = atoi(param);
1874 	}
1875 
1876 	param = os_strstr(pos, " frame=");
1877 	if (param == NULL)
1878 		return -1;
1879 	param += 7;
1880 
1881 	len = os_strlen(param);
1882 	if (len & 1)
1883 		return -1;
1884 	len /= 2;
1885 
1886 	buf = os_malloc(len);
1887 	if (buf == NULL)
1888 		return -1;
1889 
1890 	if (hexstr2bin(param, buf, len) < 0) {
1891 		os_free(buf);
1892 		return -1;
1893 	}
1894 
1895 	os_memset(&event, 0, sizeof(event));
1896 	event.rx_mgmt.freq = freq;
1897 	event.rx_mgmt.frame = buf;
1898 	event.rx_mgmt.frame_len = len;
1899 	event.rx_mgmt.ssi_signal = ssi_signal;
1900 	event.rx_mgmt.datarate = datarate;
1901 	hapd->ext_mgmt_frame_handling = 0;
1902 	wpa_supplicant_event(hapd, EVENT_RX_MGMT, &event);
1903 	hapd->ext_mgmt_frame_handling = 1;
1904 
1905 	os_free(buf);
1906 
1907 	return 0;
1908 }
1909 
1910 
1911 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1912 {
1913 	char *pos;
1914 	u8 src[ETH_ALEN], *buf;
1915 	int used;
1916 	size_t len;
1917 
1918 	wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1919 
1920 	pos = cmd;
1921 	used = hwaddr_aton2(pos, src);
1922 	if (used < 0)
1923 		return -1;
1924 	pos += used;
1925 	while (*pos == ' ')
1926 		pos++;
1927 
1928 	len = os_strlen(pos);
1929 	if (len & 1)
1930 		return -1;
1931 	len /= 2;
1932 
1933 	buf = os_malloc(len);
1934 	if (buf == NULL)
1935 		return -1;
1936 
1937 	if (hexstr2bin(pos, buf, len) < 0) {
1938 		os_free(buf);
1939 		return -1;
1940 	}
1941 
1942 	ieee802_1x_receive(hapd, src, buf, len);
1943 	os_free(buf);
1944 
1945 	return 0;
1946 }
1947 
1948 
1949 static int hostapd_ctrl_iface_eapol_tx(struct hostapd_data *hapd, char *cmd)
1950 {
1951 	char *pos, *pos2;
1952 	u8 dst[ETH_ALEN], *buf;
1953 	int used, ret;
1954 	size_t len;
1955 	unsigned int prev;
1956 	int encrypt = 0;
1957 
1958 	wpa_printf(MSG_DEBUG, "External EAPOL TX: %s", cmd);
1959 
1960 	pos = cmd;
1961 	used = hwaddr_aton2(pos, dst);
1962 	if (used < 0)
1963 		return -1;
1964 	pos += used;
1965 	while (*pos == ' ')
1966 		pos++;
1967 
1968 	pos2 = os_strchr(pos, ' ');
1969 	if (pos2) {
1970 		len = pos2 - pos;
1971 		encrypt = os_strstr(pos2, "encrypt=1") != NULL;
1972 	} else {
1973 		len = os_strlen(pos);
1974 	}
1975 	if (len & 1)
1976 		return -1;
1977 	len /= 2;
1978 
1979 	buf = os_malloc(len);
1980 	if (!buf || hexstr2bin(pos, buf, len) < 0) {
1981 		os_free(buf);
1982 		return -1;
1983 	}
1984 
1985 	prev = hapd->ext_eapol_frame_io;
1986 	hapd->ext_eapol_frame_io = 0;
1987 	ret = hostapd_wpa_auth_send_eapol(hapd, dst, buf, len, encrypt);
1988 	hapd->ext_eapol_frame_io = prev;
1989 	os_free(buf);
1990 
1991 	return ret;
1992 }
1993 
1994 
1995 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1996 {
1997 	size_t i;
1998 	u32 sum = 0;
1999 	const u16 *pos = buf;
2000 
2001 	for (i = 0; i < len / 2; i++)
2002 		sum += *pos++;
2003 
2004 	while (sum >> 16)
2005 		sum = (sum & 0xffff) + (sum >> 16);
2006 
2007 	return sum ^ 0xffff;
2008 }
2009 
2010 
2011 #define HWSIM_PACKETLEN 1500
2012 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
2013 
2014 static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
2015 				 size_t len)
2016 {
2017 	struct hostapd_data *hapd = ctx;
2018 	const struct ether_header *eth;
2019 	struct ip ip;
2020 	const u8 *pos;
2021 	unsigned int i;
2022 	char extra[30];
2023 
2024 	if (len < sizeof(*eth) + sizeof(ip) || len > HWSIM_PACKETLEN) {
2025 		wpa_printf(MSG_DEBUG,
2026 			   "test data: RX - ignore unexpected length %d",
2027 			   (int) len);
2028 		return;
2029 	}
2030 
2031 	eth = (const struct ether_header *) buf;
2032 	os_memcpy(&ip, eth + 1, sizeof(ip));
2033 	pos = &buf[sizeof(*eth) + sizeof(ip)];
2034 
2035 	if (ip.ip_hl != 5 || ip.ip_v != 4 ||
2036 	    ntohs(ip.ip_len) > HWSIM_IP_LEN) {
2037 		wpa_printf(MSG_DEBUG,
2038 			   "test data: RX - ignore unexpected IP header");
2039 		return;
2040 	}
2041 
2042 	for (i = 0; i < ntohs(ip.ip_len) - sizeof(ip); i++) {
2043 		if (*pos != (u8) i) {
2044 			wpa_printf(MSG_DEBUG,
2045 				   "test data: RX - ignore mismatching payload");
2046 			return;
2047 		}
2048 		pos++;
2049 	}
2050 
2051 	extra[0] = '\0';
2052 	if (ntohs(ip.ip_len) != HWSIM_IP_LEN)
2053 		os_snprintf(extra, sizeof(extra), " len=%d", ntohs(ip.ip_len));
2054 	wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR "%s",
2055 		MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost), extra);
2056 }
2057 
2058 
2059 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
2060 					       char *cmd)
2061 {
2062 	int enabled = atoi(cmd);
2063 	char *pos;
2064 	const char *ifname;
2065 
2066 	if (!enabled) {
2067 		if (hapd->l2_test) {
2068 			l2_packet_deinit(hapd->l2_test);
2069 			hapd->l2_test = NULL;
2070 			wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
2071 				"test data: Disabled");
2072 		}
2073 		return 0;
2074 	}
2075 
2076 	if (hapd->l2_test)
2077 		return 0;
2078 
2079 	pos = os_strstr(cmd, " ifname=");
2080 	if (pos)
2081 		ifname = pos + 8;
2082 	else
2083 		ifname = hapd->conf->iface;
2084 
2085 	hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
2086 					ETHERTYPE_IP, hostapd_data_test_rx,
2087 					hapd, 1);
2088 	if (hapd->l2_test == NULL)
2089 		return -1;
2090 
2091 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
2092 
2093 	return 0;
2094 }
2095 
2096 
2097 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
2098 {
2099 	u8 dst[ETH_ALEN], src[ETH_ALEN];
2100 	char *pos, *pos2;
2101 	int used;
2102 	long int val;
2103 	u8 tos;
2104 	u8 buf[2 + HWSIM_PACKETLEN];
2105 	struct ether_header *eth;
2106 	struct ip *ip;
2107 	u8 *dpos;
2108 	unsigned int i;
2109 	size_t send_len = HWSIM_IP_LEN;
2110 
2111 	if (hapd->l2_test == NULL)
2112 		return -1;
2113 
2114 	/* format: <dst> <src> <tos> [len=<length>] */
2115 
2116 	pos = cmd;
2117 	used = hwaddr_aton2(pos, dst);
2118 	if (used < 0)
2119 		return -1;
2120 	pos += used;
2121 	while (*pos == ' ')
2122 		pos++;
2123 	used = hwaddr_aton2(pos, src);
2124 	if (used < 0)
2125 		return -1;
2126 	pos += used;
2127 
2128 	val = strtol(pos, &pos2, 0);
2129 	if (val < 0 || val > 0xff)
2130 		return -1;
2131 	tos = val;
2132 
2133 	pos = os_strstr(pos2, " len=");
2134 	if (pos) {
2135 		i = atoi(pos + 5);
2136 		if (i < sizeof(*ip) || i > HWSIM_IP_LEN)
2137 			return -1;
2138 		send_len = i;
2139 	}
2140 
2141 	eth = (struct ether_header *) &buf[2];
2142 	os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
2143 	os_memcpy(eth->ether_shost, src, ETH_ALEN);
2144 	eth->ether_type = htons(ETHERTYPE_IP);
2145 	ip = (struct ip *) (eth + 1);
2146 	os_memset(ip, 0, sizeof(*ip));
2147 	ip->ip_hl = 5;
2148 	ip->ip_v = 4;
2149 	ip->ip_ttl = 64;
2150 	ip->ip_tos = tos;
2151 	ip->ip_len = htons(send_len);
2152 	ip->ip_p = 1;
2153 	ip->ip_src.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
2154 	ip->ip_dst.s_addr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
2155 	ip->ip_sum = ipv4_hdr_checksum(ip, sizeof(*ip));
2156 	dpos = (u8 *) (ip + 1);
2157 	for (i = 0; i < send_len - sizeof(*ip); i++)
2158 		*dpos++ = i;
2159 
2160 	if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
2161 			   sizeof(struct ether_header) + send_len) < 0)
2162 		return -1;
2163 
2164 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
2165 		" src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
2166 
2167 	return 0;
2168 }
2169 
2170 
2171 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
2172 					      char *cmd)
2173 {
2174 	u8 *buf;
2175 	struct ether_header *eth;
2176 	struct l2_packet_data *l2 = NULL;
2177 	size_t len;
2178 	u16 ethertype;
2179 	int res = -1;
2180 	const char *ifname = hapd->conf->iface;
2181 
2182 	if (os_strncmp(cmd, "ifname=", 7) == 0) {
2183 		cmd += 7;
2184 		ifname = cmd;
2185 		cmd = os_strchr(cmd, ' ');
2186 		if (cmd == NULL)
2187 			return -1;
2188 		*cmd++ = '\0';
2189 	}
2190 
2191 	len = os_strlen(cmd);
2192 	if (len & 1 || len < ETH_HLEN * 2)
2193 		return -1;
2194 	len /= 2;
2195 
2196 	buf = os_malloc(len);
2197 	if (buf == NULL)
2198 		return -1;
2199 
2200 	if (hexstr2bin(cmd, buf, len) < 0)
2201 		goto done;
2202 
2203 	eth = (struct ether_header *) buf;
2204 	ethertype = ntohs(eth->ether_type);
2205 
2206 	l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
2207 			    hostapd_data_test_rx, hapd, 1);
2208 	if (l2 == NULL)
2209 		goto done;
2210 
2211 	res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
2212 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
2213 done:
2214 	if (l2)
2215 		l2_packet_deinit(l2);
2216 	os_free(buf);
2217 
2218 	return res < 0 ? -1 : 0;
2219 }
2220 
2221 
2222 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
2223 {
2224 #ifdef WPA_TRACE_BFD
2225 	char *pos;
2226 
2227 	wpa_trace_fail_after = atoi(cmd);
2228 	pos = os_strchr(cmd, ':');
2229 	if (pos) {
2230 		pos++;
2231 		os_strlcpy(wpa_trace_fail_func, pos,
2232 			   sizeof(wpa_trace_fail_func));
2233 	} else {
2234 		wpa_trace_fail_after = 0;
2235 	}
2236 
2237 	return 0;
2238 #else /* WPA_TRACE_BFD */
2239 	return -1;
2240 #endif /* WPA_TRACE_BFD */
2241 }
2242 
2243 
2244 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
2245 				       char *buf, size_t buflen)
2246 {
2247 #ifdef WPA_TRACE_BFD
2248 	return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
2249 			   wpa_trace_fail_func);
2250 #else /* WPA_TRACE_BFD */
2251 	return -1;
2252 #endif /* WPA_TRACE_BFD */
2253 }
2254 
2255 
2256 static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
2257 {
2258 #ifdef WPA_TRACE_BFD
2259 	char *pos;
2260 
2261 	wpa_trace_test_fail_after = atoi(cmd);
2262 	pos = os_strchr(cmd, ':');
2263 	if (pos) {
2264 		pos++;
2265 		os_strlcpy(wpa_trace_test_fail_func, pos,
2266 			   sizeof(wpa_trace_test_fail_func));
2267 	} else {
2268 		wpa_trace_test_fail_after = 0;
2269 	}
2270 
2271 	return 0;
2272 #else /* WPA_TRACE_BFD */
2273 	return -1;
2274 #endif /* WPA_TRACE_BFD */
2275 }
2276 
2277 
2278 static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
2279 				 char *buf, size_t buflen)
2280 {
2281 #ifdef WPA_TRACE_BFD
2282 	return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
2283 			   wpa_trace_test_fail_func);
2284 #else /* WPA_TRACE_BFD */
2285 	return -1;
2286 #endif /* WPA_TRACE_BFD */
2287 }
2288 
2289 
2290 static int hostapd_ctrl_reset_pn(struct hostapd_data *hapd, const char *cmd)
2291 {
2292 	struct sta_info *sta;
2293 	u8 addr[ETH_ALEN];
2294 	u8 zero[WPA_TK_MAX_LEN];
2295 
2296 	os_memset(zero, 0, sizeof(zero));
2297 
2298 	if (hwaddr_aton(cmd, addr))
2299 		return -1;
2300 
2301 	if (is_broadcast_ether_addr(addr) && os_strstr(cmd, " BIGTK")) {
2302 		if (hapd->last_bigtk_alg == WPA_ALG_NONE)
2303 			return -1;
2304 
2305 		wpa_printf(MSG_INFO, "TESTING: Reset BIPN for BIGTK");
2306 
2307 		/* First, use a zero key to avoid any possible duplicate key
2308 		 * avoidance in the driver. */
2309 		if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2310 					hapd->last_bigtk_alg,
2311 					broadcast_ether_addr,
2312 					hapd->last_bigtk_key_idx, 0, 1, NULL, 0,
2313 					zero, hapd->last_bigtk_len,
2314 					KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2315 			return -1;
2316 
2317 		/* Set the previously configured key to reset its TSC */
2318 		return hostapd_drv_set_key(hapd->conf->iface, hapd,
2319 					   hapd->last_bigtk_alg,
2320 					   broadcast_ether_addr,
2321 					   hapd->last_bigtk_key_idx, 0, 1, NULL,
2322 					   0, hapd->last_bigtk,
2323 					   hapd->last_bigtk_len,
2324 					   KEY_FLAG_GROUP_TX_DEFAULT);
2325 	}
2326 
2327 	if (is_broadcast_ether_addr(addr) && os_strstr(cmd, "IGTK")) {
2328 		if (hapd->last_igtk_alg == WPA_ALG_NONE)
2329 			return -1;
2330 
2331 		wpa_printf(MSG_INFO, "TESTING: Reset IPN for IGTK");
2332 
2333 		/* First, use a zero key to avoid any possible duplicate key
2334 		 * avoidance in the driver. */
2335 		if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2336 					hapd->last_igtk_alg,
2337 					broadcast_ether_addr,
2338 					hapd->last_igtk_key_idx, 0, 1, NULL, 0,
2339 					zero, hapd->last_igtk_len,
2340 					KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2341 			return -1;
2342 
2343 		/* Set the previously configured key to reset its TSC */
2344 		return hostapd_drv_set_key(hapd->conf->iface, hapd,
2345 					   hapd->last_igtk_alg,
2346 					   broadcast_ether_addr,
2347 					   hapd->last_igtk_key_idx, 0, 1, NULL,
2348 					   0, hapd->last_igtk,
2349 					   hapd->last_igtk_len,
2350 					   KEY_FLAG_GROUP_TX_DEFAULT);
2351 	}
2352 
2353 	if (is_broadcast_ether_addr(addr)) {
2354 		if (hapd->last_gtk_alg == WPA_ALG_NONE)
2355 			return -1;
2356 
2357 		wpa_printf(MSG_INFO, "TESTING: Reset PN for GTK");
2358 
2359 		/* First, use a zero key to avoid any possible duplicate key
2360 		 * avoidance in the driver. */
2361 		if (hostapd_drv_set_key(hapd->conf->iface, hapd,
2362 					hapd->last_gtk_alg,
2363 					broadcast_ether_addr,
2364 					hapd->last_gtk_key_idx, 0, 1, NULL, 0,
2365 					zero, hapd->last_gtk_len,
2366 					KEY_FLAG_GROUP_TX_DEFAULT) < 0)
2367 			return -1;
2368 
2369 		/* Set the previously configured key to reset its TSC */
2370 		return hostapd_drv_set_key(hapd->conf->iface, hapd,
2371 					   hapd->last_gtk_alg,
2372 					   broadcast_ether_addr,
2373 					   hapd->last_gtk_key_idx, 0, 1, NULL,
2374 					   0, hapd->last_gtk,
2375 					   hapd->last_gtk_len,
2376 					   KEY_FLAG_GROUP_TX_DEFAULT);
2377 	}
2378 
2379 	sta = ap_get_sta(hapd, addr);
2380 	if (!sta)
2381 		return -1;
2382 
2383 	if (sta->last_tk_alg == WPA_ALG_NONE)
2384 		return -1;
2385 
2386 	wpa_printf(MSG_INFO, "TESTING: Reset PN for " MACSTR,
2387 		   MAC2STR(sta->addr));
2388 
2389 	/* First, use a zero key to avoid any possible duplicate key avoidance
2390 	 * in the driver. */
2391 	if (hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
2392 				sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2393 				zero, sta->last_tk_len,
2394 				KEY_FLAG_PAIRWISE_RX_TX) < 0)
2395 		return -1;
2396 
2397 	/* Set the previously configured key to reset its TSC/RSC */
2398 	return hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
2399 				   sta->addr, sta->last_tk_key_idx, 0, 1, NULL,
2400 				   0, sta->last_tk, sta->last_tk_len,
2401 				   KEY_FLAG_PAIRWISE_RX_TX);
2402 }
2403 
2404 
2405 static int hostapd_ctrl_set_key(struct hostapd_data *hapd, const char *cmd)
2406 {
2407 	u8 addr[ETH_ALEN];
2408 	const char *pos = cmd;
2409 	enum wpa_alg alg;
2410 	enum key_flag key_flag;
2411 	int idx, set_tx;
2412 	u8 seq[6], key[WPA_TK_MAX_LEN];
2413 	size_t key_len;
2414 
2415 	/* parameters: alg addr idx set_tx seq key key_flag */
2416 
2417 	alg = atoi(pos);
2418 	pos = os_strchr(pos, ' ');
2419 	if (!pos)
2420 		return -1;
2421 	pos++;
2422 	if (hwaddr_aton(pos, addr))
2423 		return -1;
2424 	pos += 17;
2425 	if (*pos != ' ')
2426 		return -1;
2427 	pos++;
2428 	idx = atoi(pos);
2429 	pos = os_strchr(pos, ' ');
2430 	if (!pos)
2431 		return -1;
2432 	pos++;
2433 	set_tx = atoi(pos);
2434 	pos = os_strchr(pos, ' ');
2435 	if (!pos)
2436 		return -1;
2437 	pos++;
2438 	if (hexstr2bin(pos, seq, sizeof(seq)) < 0)
2439 		return -1;
2440 	pos += 2 * 6;
2441 	if (*pos != ' ')
2442 		return -1;
2443 	pos++;
2444 	if (!os_strchr(pos, ' '))
2445 		return -1;
2446 	key_len = (os_strchr(pos, ' ') - pos) / 2;
2447 	if (hexstr2bin(pos, key, key_len) < 0)
2448 		return -1;
2449 	pos += 2 * key_len;
2450 	if (*pos != ' ')
2451 		return -1;
2452 
2453 	pos++;
2454 	key_flag = atoi(pos);
2455 	pos = os_strchr(pos, ' ');
2456 	if (pos)
2457 		return -1;
2458 
2459 	wpa_printf(MSG_INFO, "TESTING: Set key");
2460 	return hostapd_drv_set_key(hapd->conf->iface, hapd, alg, addr, idx, 0,
2461 				   set_tx, seq, 6, key, key_len, key_flag);
2462 }
2463 
2464 
2465 static void restore_tk(void *ctx1, void *ctx2)
2466 {
2467 	struct hostapd_data *hapd = ctx1;
2468 	struct sta_info *sta = ctx2;
2469 
2470 	wpa_printf(MSG_INFO, "TESTING: Restore TK for " MACSTR,
2471 		   MAC2STR(sta->addr));
2472 	/* This does not really restore the TSC properly, so this will result
2473 	 * in replay protection issues for now since there is no clean way of
2474 	 * preventing encryption of a single EAPOL frame. */
2475 	hostapd_drv_set_key(hapd->conf->iface, hapd, sta->last_tk_alg,
2476 			    sta->addr, sta->last_tk_key_idx, 0, 1, NULL, 0,
2477 			    sta->last_tk, sta->last_tk_len,
2478 			    KEY_FLAG_PAIRWISE_RX_TX);
2479 }
2480 
2481 
2482 static int hostapd_ctrl_resend_m1(struct hostapd_data *hapd, const char *cmd)
2483 {
2484 	struct sta_info *sta;
2485 	u8 addr[ETH_ALEN];
2486 	int plain = os_strstr(cmd, "plaintext") != NULL;
2487 
2488 	if (hwaddr_aton(cmd, addr))
2489 		return -1;
2490 
2491 	sta = ap_get_sta(hapd, addr);
2492 	if (!sta || !sta->wpa_sm)
2493 		return -1;
2494 
2495 	if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2496 		plain = 0; /* no need for special processing */
2497 	if (plain) {
2498 		wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2499 			   MAC2STR(sta->addr));
2500 		hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
2501 				    sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2502 				    0, NULL, 0, KEY_FLAG_PAIRWISE);
2503 	}
2504 
2505 	wpa_printf(MSG_INFO, "TESTING: Send M1 to " MACSTR, MAC2STR(sta->addr));
2506 	return wpa_auth_resend_m1(sta->wpa_sm,
2507 				  os_strstr(cmd, "change-anonce") != NULL,
2508 				  plain ? restore_tk : NULL, hapd, sta);
2509 }
2510 
2511 
2512 static int hostapd_ctrl_resend_m3(struct hostapd_data *hapd, const char *cmd)
2513 {
2514 	struct sta_info *sta;
2515 	u8 addr[ETH_ALEN];
2516 	int plain = os_strstr(cmd, "plaintext") != NULL;
2517 
2518 	if (hwaddr_aton(cmd, addr))
2519 		return -1;
2520 
2521 	sta = ap_get_sta(hapd, addr);
2522 	if (!sta || !sta->wpa_sm)
2523 		return -1;
2524 
2525 	if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2526 		plain = 0; /* no need for special processing */
2527 	if (plain) {
2528 		wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2529 			   MAC2STR(sta->addr));
2530 		hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
2531 				    sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2532 				    0, NULL, 0, KEY_FLAG_PAIRWISE);
2533 	}
2534 
2535 	wpa_printf(MSG_INFO, "TESTING: Send M3 to " MACSTR, MAC2STR(sta->addr));
2536 	return wpa_auth_resend_m3(sta->wpa_sm,
2537 				  plain ? restore_tk : NULL, hapd, sta);
2538 }
2539 
2540 
2541 static int hostapd_ctrl_resend_group_m1(struct hostapd_data *hapd,
2542 					const char *cmd)
2543 {
2544 	struct sta_info *sta;
2545 	u8 addr[ETH_ALEN];
2546 	int plain = os_strstr(cmd, "plaintext") != NULL;
2547 
2548 	if (hwaddr_aton(cmd, addr))
2549 		return -1;
2550 
2551 	sta = ap_get_sta(hapd, addr);
2552 	if (!sta || !sta->wpa_sm)
2553 		return -1;
2554 
2555 	if (plain && sta->last_tk_alg == WPA_ALG_NONE)
2556 		plain = 0; /* no need for special processing */
2557 	if (plain) {
2558 		wpa_printf(MSG_INFO, "TESTING: Clear TK for " MACSTR,
2559 			   MAC2STR(sta->addr));
2560 		hostapd_drv_set_key(hapd->conf->iface, hapd, WPA_ALG_NONE,
2561 				    sta->addr, sta->last_tk_key_idx, 0, 0, NULL,
2562 				    0, NULL, 0, KEY_FLAG_PAIRWISE);
2563 	}
2564 
2565 	wpa_printf(MSG_INFO,
2566 		   "TESTING: Send group M1 for the same GTK and zero RSC to "
2567 		   MACSTR, MAC2STR(sta->addr));
2568 	return wpa_auth_resend_group_m1(sta->wpa_sm,
2569 					plain ? restore_tk : NULL, hapd, sta);
2570 }
2571 
2572 
2573 static int hostapd_ctrl_rekey_ptk(struct hostapd_data *hapd, const char *cmd)
2574 {
2575 	struct sta_info *sta;
2576 	u8 addr[ETH_ALEN];
2577 
2578 	if (hwaddr_aton(cmd, addr))
2579 		return -1;
2580 
2581 	sta = ap_get_sta(hapd, addr);
2582 	if (!sta || !sta->wpa_sm)
2583 		return -1;
2584 
2585 	return wpa_auth_rekey_ptk(hapd->wpa_auth, sta->wpa_sm);
2586 }
2587 
2588 
2589 static int hostapd_ctrl_get_pmksa_pmk(struct hostapd_data *hapd, const u8 *addr,
2590 				      char *buf, size_t buflen)
2591 {
2592 	struct rsn_pmksa_cache_entry *pmksa;
2593 
2594 	pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, addr, NULL);
2595 	if (!pmksa)
2596 		return -1;
2597 
2598 	return wpa_snprintf_hex(buf, buflen, pmksa->pmk, pmksa->pmk_len);
2599 }
2600 
2601 
2602 static int hostapd_ctrl_get_pmk(struct hostapd_data *hapd, const char *cmd,
2603 				char *buf, size_t buflen)
2604 {
2605 	struct sta_info *sta;
2606 	u8 addr[ETH_ALEN];
2607 	const u8 *pmk;
2608 	int pmk_len;
2609 
2610 	if (hwaddr_aton(cmd, addr))
2611 		return -1;
2612 
2613 	sta = ap_get_sta(hapd, addr);
2614 	if (!sta || !sta->wpa_sm) {
2615 		wpa_printf(MSG_DEBUG, "No STA WPA state machine for " MACSTR,
2616 			   MAC2STR(addr));
2617 		return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
2618 	}
2619 	pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
2620 	if (!pmk || !pmk_len) {
2621 		wpa_printf(MSG_DEBUG, "No PMK stored for " MACSTR,
2622 			   MAC2STR(addr));
2623 		return hostapd_ctrl_get_pmksa_pmk(hapd, addr, buf, buflen);
2624 	}
2625 
2626 	return wpa_snprintf_hex(buf, buflen, pmk, pmk_len);
2627 }
2628 
2629 
2630 static int hostapd_ctrl_register_frame(struct hostapd_data *hapd,
2631 				       const char *cmd)
2632 {
2633 	u16 type;
2634 	char *pos, *end;
2635 	u8 match[10];
2636 	size_t match_len;
2637 	bool multicast = false;
2638 
2639 	type = strtol(cmd, &pos, 16);
2640 	if (*pos != ' ')
2641 		return -1;
2642 	pos++;
2643 	end = os_strchr(pos, ' ');
2644 	if (end) {
2645 		match_len = end - pos;
2646 		multicast = os_strstr(end, "multicast") != NULL;
2647 	} else {
2648 		match_len = os_strlen(pos) / 2;
2649 	}
2650 	if (hexstr2bin(pos, match, match_len))
2651 		return -1;
2652 
2653 	return hostapd_drv_register_frame(hapd, type, match, match_len,
2654 					  multicast);
2655 }
2656 
2657 #endif /* CONFIG_TESTING_OPTIONS */
2658 
2659 
2660 #ifdef NEED_AP_MLME
2661 static int hostapd_ctrl_check_freq_params(struct hostapd_freq_params *params)
2662 {
2663 	switch (params->bandwidth) {
2664 	case 0:
2665 		/* bandwidth not specified: use 20 MHz by default */
2666 		/* fall-through */
2667 	case 20:
2668 		if (params->center_freq1 &&
2669 		    params->center_freq1 != params->freq)
2670 			return -1;
2671 
2672 		if (params->center_freq2 || params->sec_channel_offset)
2673 			return -1;
2674 		break;
2675 	case 40:
2676 		if (params->center_freq2 || !params->sec_channel_offset)
2677 			return -1;
2678 
2679 		if (!params->center_freq1)
2680 			break;
2681 		switch (params->sec_channel_offset) {
2682 		case 1:
2683 			if (params->freq + 10 != params->center_freq1)
2684 				return -1;
2685 			break;
2686 		case -1:
2687 			if (params->freq - 10 != params->center_freq1)
2688 				return -1;
2689 			break;
2690 		default:
2691 			return -1;
2692 		}
2693 		break;
2694 	case 80:
2695 		if (!params->center_freq1 || !params->sec_channel_offset)
2696 			return 1;
2697 
2698 		switch (params->sec_channel_offset) {
2699 		case 1:
2700 			if (params->freq - 10 != params->center_freq1 &&
2701 			    params->freq + 30 != params->center_freq1)
2702 				return 1;
2703 			break;
2704 		case -1:
2705 			if (params->freq + 10 != params->center_freq1 &&
2706 			    params->freq - 30 != params->center_freq1)
2707 				return -1;
2708 			break;
2709 		default:
2710 			return -1;
2711 		}
2712 
2713 		/* Adjacent and overlapped are not allowed for 80+80 */
2714 		if (params->center_freq2 &&
2715 		    params->center_freq1 - params->center_freq2 <= 80 &&
2716 		    params->center_freq2 - params->center_freq1 <= 80)
2717 			return 1;
2718 		break;
2719 	case 160:
2720 		if (!params->center_freq1 || params->center_freq2 ||
2721 		    !params->sec_channel_offset)
2722 			return -1;
2723 
2724 		switch (params->sec_channel_offset) {
2725 		case 1:
2726 			if (params->freq + 70 != params->center_freq1 &&
2727 			    params->freq + 30 != params->center_freq1 &&
2728 			    params->freq - 10 != params->center_freq1 &&
2729 			    params->freq - 50 != params->center_freq1)
2730 				return -1;
2731 			break;
2732 		case -1:
2733 			if (params->freq + 50 != params->center_freq1 &&
2734 			    params->freq + 10 != params->center_freq1 &&
2735 			    params->freq - 30 != params->center_freq1 &&
2736 			    params->freq - 70 != params->center_freq1)
2737 				return -1;
2738 			break;
2739 		default:
2740 			return -1;
2741 		}
2742 		break;
2743 	default:
2744 		return -1;
2745 	}
2746 
2747 	return 0;
2748 }
2749 #endif /* NEED_AP_MLME */
2750 
2751 
2752 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
2753 					  char *pos)
2754 {
2755 #ifdef NEED_AP_MLME
2756 	struct csa_settings settings;
2757 	int ret;
2758 	int dfs_range = 0;
2759 	unsigned int i;
2760 	int bandwidth;
2761 	u8 chan;
2762 
2763 	ret = hostapd_parse_csa_settings(pos, &settings);
2764 	if (ret)
2765 		return ret;
2766 
2767 	ret = hostapd_ctrl_check_freq_params(&settings.freq_params);
2768 	if (ret) {
2769 		wpa_printf(MSG_INFO,
2770 			   "chanswitch: invalid frequency settings provided");
2771 		return ret;
2772 	}
2773 
2774 	switch (settings.freq_params.bandwidth) {
2775 	case 40:
2776 		bandwidth = CHAN_WIDTH_40;
2777 		break;
2778 	case 80:
2779 		if (settings.freq_params.center_freq2)
2780 			bandwidth = CHAN_WIDTH_80P80;
2781 		else
2782 			bandwidth = CHAN_WIDTH_80;
2783 		break;
2784 	case 160:
2785 		bandwidth = CHAN_WIDTH_160;
2786 		break;
2787 	default:
2788 		bandwidth = CHAN_WIDTH_20;
2789 		break;
2790 	}
2791 
2792 	if (settings.freq_params.center_freq1)
2793 		dfs_range += hostapd_is_dfs_overlap(
2794 			iface, bandwidth, settings.freq_params.center_freq1);
2795 	else
2796 		dfs_range += hostapd_is_dfs_overlap(
2797 			iface, bandwidth, settings.freq_params.freq);
2798 
2799 	if (settings.freq_params.center_freq2)
2800 		dfs_range += hostapd_is_dfs_overlap(
2801 			iface, bandwidth, settings.freq_params.center_freq2);
2802 
2803 	if (dfs_range) {
2804 		ret = ieee80211_freq_to_chan(settings.freq_params.freq, &chan);
2805 		if (ret == NUM_HOSTAPD_MODES) {
2806 			wpa_printf(MSG_ERROR,
2807 				   "Failed to get channel for (freq=%d, sec_channel_offset=%d, bw=%d)",
2808 				   settings.freq_params.freq,
2809 				   settings.freq_params.sec_channel_offset,
2810 				   settings.freq_params.bandwidth);
2811 			return -1;
2812 		}
2813 
2814 		settings.freq_params.channel = chan;
2815 
2816 		wpa_printf(MSG_DEBUG,
2817 			   "DFS/CAC to (channel=%u, freq=%d, sec_channel_offset=%d, bw=%d, center_freq1=%d)",
2818 			   settings.freq_params.channel,
2819 			   settings.freq_params.freq,
2820 			   settings.freq_params.sec_channel_offset,
2821 			   settings.freq_params.bandwidth,
2822 			   settings.freq_params.center_freq1);
2823 
2824 		/* Perform CAC and switch channel */
2825 		hostapd_switch_channel_fallback(iface, &settings.freq_params);
2826 		return 0;
2827 	}
2828 
2829 	for (i = 0; i < iface->num_bss; i++) {
2830 
2831 		/* Save CHAN_SWITCH VHT and HE config */
2832 		hostapd_chan_switch_config(iface->bss[i],
2833 					   &settings.freq_params);
2834 
2835 		ret = hostapd_switch_channel(iface->bss[i], &settings);
2836 		if (ret) {
2837 			/* FIX: What do we do if CSA fails in the middle of
2838 			 * submitting multi-BSS CSA requests? */
2839 			return ret;
2840 		}
2841 	}
2842 
2843 	return 0;
2844 #else /* NEED_AP_MLME */
2845 	return -1;
2846 #endif /* NEED_AP_MLME */
2847 }
2848 
2849 
2850 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
2851 				  int reply_size, const char *param)
2852 {
2853 #ifdef RADIUS_SERVER
2854 	if (os_strcmp(param, "radius_server") == 0) {
2855 		return radius_server_get_mib(hapd->radius_srv, reply,
2856 					     reply_size);
2857 	}
2858 #endif /* RADIUS_SERVER */
2859 	return -1;
2860 }
2861 
2862 
2863 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
2864 				     char *buf, size_t buflen)
2865 {
2866 	int ret;
2867 	char *pos, *temp = NULL;
2868 	u8 *data = NULL;
2869 	unsigned int vendor_id, subcmd;
2870 	enum nested_attr nested_attr_flag = NESTED_ATTR_UNSPECIFIED;
2871 	struct wpabuf *reply;
2872 	size_t data_len = 0;
2873 
2874 	/**
2875 	 * cmd: <vendor id> <subcommand id> [<hex formatted data>]
2876 	 * [nested=<0|1>]
2877 	 */
2878 	vendor_id = strtoul(cmd, &pos, 16);
2879 	if (!isblank((unsigned char) *pos))
2880 		return -EINVAL;
2881 
2882 	subcmd = strtoul(pos, &pos, 10);
2883 
2884 	if (*pos != '\0') {
2885 		if (!isblank((unsigned char) *pos++))
2886 			return -EINVAL;
2887 
2888 		temp = os_strchr(pos, ' ');
2889 		data_len = temp ? (size_t) (temp - pos) : os_strlen(pos);
2890 	}
2891 
2892 	if (data_len) {
2893 		data_len /= 2;
2894 		data = os_malloc(data_len);
2895 		if (!data)
2896 			return -ENOBUFS;
2897 
2898 		if (hexstr2bin(pos, data, data_len)) {
2899 			wpa_printf(MSG_DEBUG,
2900 				   "Vendor command: wrong parameter format");
2901 			os_free(data);
2902 			return -EINVAL;
2903 		}
2904 	}
2905 
2906 	pos = os_strstr(cmd, "nested=");
2907 	if (pos)
2908 		nested_attr_flag = atoi(pos + 7) ? NESTED_ATTR_USED :
2909 			NESTED_ATTR_NOT_USED;
2910 
2911 	reply = wpabuf_alloc((buflen - 1) / 2);
2912 	if (!reply) {
2913 		os_free(data);
2914 		return -ENOBUFS;
2915 	}
2916 
2917 	ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
2918 				     nested_attr_flag, reply);
2919 
2920 	if (ret == 0)
2921 		ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
2922 				       wpabuf_len(reply));
2923 
2924 	wpabuf_free(reply);
2925 	os_free(data);
2926 
2927 	return ret;
2928 }
2929 
2930 
2931 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
2932 					   const char *cmd)
2933 {
2934 	u8 addr[ETH_ALEN];
2935 	struct sta_info *sta;
2936 
2937 	if (hwaddr_aton(cmd, addr))
2938 		return -1;
2939 
2940 	sta = ap_get_sta(hapd, addr);
2941 	if (!sta || !sta->eapol_sm)
2942 		return -1;
2943 
2944 	eapol_auth_reauthenticate(sta->eapol_sm);
2945 	return 0;
2946 }
2947 
2948 
2949 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
2950 {
2951 	u8 addr[ETH_ALEN];
2952 	struct sta_info *sta;
2953 	char *pos = cmd, *param;
2954 
2955 	if (hwaddr_aton(pos, addr) || pos[17] != ' ')
2956 		return -1;
2957 	pos += 18;
2958 	param = pos;
2959 	pos = os_strchr(pos, ' ');
2960 	if (!pos)
2961 		return -1;
2962 	*pos++ = '\0';
2963 
2964 	sta = ap_get_sta(hapd, addr);
2965 	if (!sta || !sta->eapol_sm)
2966 		return -1;
2967 
2968 	return eapol_auth_set_conf(sta->eapol_sm, param, pos);
2969 }
2970 
2971 
2972 static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
2973 					char *buf, size_t buflen)
2974 {
2975 	char *pos, *end, *stamp;
2976 	int ret;
2977 
2978 	/* cmd: "LOG_LEVEL [<level>]" */
2979 	if (*cmd == '\0') {
2980 		pos = buf;
2981 		end = buf + buflen;
2982 		ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2983 				  "Timestamp: %d\n",
2984 				  debug_level_str(wpa_debug_level),
2985 				  wpa_debug_timestamp);
2986 		if (os_snprintf_error(end - pos, ret))
2987 			ret = 0;
2988 
2989 		return ret;
2990 	}
2991 
2992 	while (*cmd == ' ')
2993 		cmd++;
2994 
2995 	stamp = os_strchr(cmd, ' ');
2996 	if (stamp) {
2997 		*stamp++ = '\0';
2998 		while (*stamp == ' ') {
2999 			stamp++;
3000 		}
3001 	}
3002 
3003 	if (os_strlen(cmd)) {
3004 		int level = str_to_debug_level(cmd);
3005 		if (level < 0)
3006 			return -1;
3007 		wpa_debug_level = level;
3008 	}
3009 
3010 	if (stamp && os_strlen(stamp))
3011 		wpa_debug_timestamp = atoi(stamp);
3012 
3013 	os_memcpy(buf, "OK\n", 3);
3014 	return 3;
3015 }
3016 
3017 
3018 #ifdef NEED_AP_MLME
3019 static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
3020 					     char *buf, size_t buflen)
3021 {
3022 	struct hostapd_iface *iface = hapd->iface;
3023 	char *pos, *end;
3024 	struct hostapd_sta_info *info;
3025 	struct os_reltime now;
3026 
3027 	if (!iface->num_sta_seen)
3028 		return 0;
3029 
3030 	sta_track_expire(iface, 0);
3031 
3032 	pos = buf;
3033 	end = buf + buflen;
3034 
3035 	os_get_reltime(&now);
3036 	dl_list_for_each_reverse(info, &iface->sta_seen,
3037 				 struct hostapd_sta_info, list) {
3038 		struct os_reltime age;
3039 		int ret;
3040 
3041 		os_reltime_sub(&now, &info->last_seen, &age);
3042 		ret = os_snprintf(pos, end - pos, MACSTR " %u %d\n",
3043 				  MAC2STR(info->addr), (unsigned int) age.sec,
3044 				  info->ssi_signal);
3045 		if (os_snprintf_error(end - pos, ret))
3046 			break;
3047 		pos += ret;
3048 	}
3049 
3050 	return pos - buf;
3051 }
3052 #endif /* NEED_AP_MLME */
3053 
3054 
3055 static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
3056 				      const char *cmd)
3057 {
3058 	u8 addr[ETH_ALEN];
3059 
3060 	if (hwaddr_aton(cmd, addr)) {
3061 		wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
3062 		return -1;
3063 	}
3064 
3065 	return hostapd_send_lci_req(hapd, addr);
3066 }
3067 
3068 
3069 static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
3070 {
3071 	u8 addr[ETH_ALEN];
3072 	char *token, *context = NULL;
3073 	int random_interval, min_ap;
3074 	u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
3075 	unsigned int n_responders;
3076 
3077 	token = str_token(cmd, " ", &context);
3078 	if (!token || hwaddr_aton(token, addr)) {
3079 		wpa_printf(MSG_INFO,
3080 			   "CTRL: REQ_RANGE - Bad destination address");
3081 		return -1;
3082 	}
3083 
3084 	token = str_token(cmd, " ", &context);
3085 	if (!token)
3086 		return -1;
3087 
3088 	random_interval = atoi(token);
3089 	if (random_interval < 0 || random_interval > 0xffff)
3090 		return -1;
3091 
3092 	token = str_token(cmd, " ", &context);
3093 	if (!token)
3094 		return -1;
3095 
3096 	min_ap = atoi(token);
3097 	if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
3098 		return -1;
3099 
3100 	n_responders = 0;
3101 	while ((token = str_token(cmd, " ", &context))) {
3102 		if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
3103 			wpa_printf(MSG_INFO,
3104 				   "CTRL: REQ_RANGE: Too many responders");
3105 			return -1;
3106 		}
3107 
3108 		if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
3109 			wpa_printf(MSG_INFO,
3110 				   "CTRL: REQ_RANGE: Bad responder address");
3111 			return -1;
3112 		}
3113 
3114 		n_responders++;
3115 	}
3116 
3117 	if (!n_responders) {
3118 		wpa_printf(MSG_INFO,
3119 			   "CTRL: REQ_RANGE - No FTM responder address");
3120 		return -1;
3121 	}
3122 
3123 	return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
3124 				      responders, n_responders);
3125 }
3126 
3127 
3128 static int hostapd_ctrl_iface_req_beacon(struct hostapd_data *hapd,
3129 					 const char *cmd, char *reply,
3130 					 size_t reply_size)
3131 {
3132 	u8 addr[ETH_ALEN];
3133 	const char *pos;
3134 	struct wpabuf *req;
3135 	int ret;
3136 	u8 req_mode = 0;
3137 
3138 	if (hwaddr_aton(cmd, addr))
3139 		return -1;
3140 	pos = os_strchr(cmd, ' ');
3141 	if (!pos)
3142 		return -1;
3143 	pos++;
3144 	if (os_strncmp(pos, "req_mode=", 9) == 0) {
3145 		int val = hex2byte(pos + 9);
3146 
3147 		if (val < 0)
3148 			return -1;
3149 		req_mode = val;
3150 		pos += 11;
3151 		pos = os_strchr(pos, ' ');
3152 		if (!pos)
3153 			return -1;
3154 		pos++;
3155 	}
3156 	req = wpabuf_parse_bin(pos);
3157 	if (!req)
3158 		return -1;
3159 
3160 	ret = hostapd_send_beacon_req(hapd, addr, req_mode, req);
3161 	wpabuf_free(req);
3162 	if (ret >= 0)
3163 		ret = os_snprintf(reply, reply_size, "%d", ret);
3164 	return ret;
3165 }
3166 
3167 
3168 static int hostapd_ctrl_iface_show_neighbor(struct hostapd_data *hapd,
3169 					    char *buf, size_t buflen)
3170 {
3171 	if (!(hapd->conf->radio_measurements[0] &
3172 	      WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3173 		wpa_printf(MSG_ERROR,
3174 			   "CTRL: SHOW_NEIGHBOR: Neighbor report is not enabled");
3175 		return -1;
3176 	}
3177 
3178 	return hostapd_neighbor_show(hapd, buf, buflen);
3179 }
3180 
3181 
3182 static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
3183 {
3184 	struct wpa_ssid_value ssid;
3185 	u8 bssid[ETH_ALEN];
3186 	struct wpabuf *nr, *lci = NULL, *civic = NULL;
3187 	int stationary = 0;
3188 	char *tmp;
3189 	int ret;
3190 
3191 	if (!(hapd->conf->radio_measurements[0] &
3192 	      WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
3193 		wpa_printf(MSG_ERROR,
3194 			   "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
3195 		return -1;
3196 	}
3197 
3198 	if (hwaddr_aton(buf, bssid)) {
3199 		wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
3200 		return -1;
3201 	}
3202 
3203 	tmp = os_strstr(buf, "ssid=");
3204 	if (!tmp || ssid_parse(tmp + 5, &ssid)) {
3205 		wpa_printf(MSG_ERROR,
3206 			   "CTRL: SET_NEIGHBOR: Bad or missing SSID");
3207 		return -1;
3208 	}
3209 	buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
3210 	if (!buf)
3211 		return -1;
3212 
3213 	tmp = os_strstr(buf, "nr=");
3214 	if (!tmp) {
3215 		wpa_printf(MSG_ERROR,
3216 			   "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
3217 		return -1;
3218 	}
3219 
3220 	buf = os_strchr(tmp, ' ');
3221 	if (buf)
3222 		*buf++ = '\0';
3223 
3224 	nr = wpabuf_parse_bin(tmp + 3);
3225 	if (!nr) {
3226 		wpa_printf(MSG_ERROR,
3227 			   "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
3228 		return -1;
3229 	}
3230 
3231 	if (!buf)
3232 		goto set;
3233 
3234 	tmp = os_strstr(buf, "lci=");
3235 	if (tmp) {
3236 		buf = os_strchr(tmp, ' ');
3237 		if (buf)
3238 			*buf++ = '\0';
3239 		lci = wpabuf_parse_bin(tmp + 4);
3240 		if (!lci) {
3241 			wpa_printf(MSG_ERROR,
3242 				   "CTRL: SET_NEIGHBOR: Bad LCI subelement");
3243 			wpabuf_free(nr);
3244 			return -1;
3245 		}
3246 	}
3247 
3248 	if (!buf)
3249 		goto set;
3250 
3251 	tmp = os_strstr(buf, "civic=");
3252 	if (tmp) {
3253 		buf = os_strchr(tmp, ' ');
3254 		if (buf)
3255 			*buf++ = '\0';
3256 		civic = wpabuf_parse_bin(tmp + 6);
3257 		if (!civic) {
3258 			wpa_printf(MSG_ERROR,
3259 				   "CTRL: SET_NEIGHBOR: Bad civic subelement");
3260 			wpabuf_free(nr);
3261 			wpabuf_free(lci);
3262 			return -1;
3263 		}
3264 	}
3265 
3266 	if (!buf)
3267 		goto set;
3268 
3269 	if (os_strstr(buf, "stat"))
3270 		stationary = 1;
3271 
3272 set:
3273 	ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic,
3274 				   stationary);
3275 
3276 	wpabuf_free(nr);
3277 	wpabuf_free(lci);
3278 	wpabuf_free(civic);
3279 
3280 	return ret;
3281 }
3282 
3283 
3284 static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
3285 					      char *buf)
3286 {
3287 	struct wpa_ssid_value ssid;
3288 	struct wpa_ssid_value *ssidp = NULL;
3289 	u8 bssid[ETH_ALEN];
3290 	char *tmp;
3291 
3292 	if (hwaddr_aton(buf, bssid)) {
3293 		wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
3294 		return -1;
3295 	}
3296 
3297 	tmp = os_strstr(buf, "ssid=");
3298 	if (tmp) {
3299 		ssidp = &ssid;
3300 		if (ssid_parse(tmp + 5, &ssid)) {
3301 			wpa_printf(MSG_ERROR,
3302 				   "CTRL: REMOVE_NEIGHBOR: Bad SSID");
3303 			return -1;
3304 		}
3305 	}
3306 
3307 	return hostapd_neighbor_remove(hapd, bssid, ssidp);
3308 }
3309 
3310 
3311 static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
3312 				     size_t buflen)
3313 {
3314 	int ret, i;
3315 	char *pos, *end;
3316 
3317 	ret = os_snprintf(buf, buflen, "%016llX:\n",
3318 			  (long long unsigned) iface->drv_flags);
3319 	if (os_snprintf_error(buflen, ret))
3320 		return -1;
3321 
3322 	pos = buf + ret;
3323 	end = buf + buflen;
3324 
3325 	for (i = 0; i < 64; i++) {
3326 		if (iface->drv_flags & (1LLU << i)) {
3327 			ret = os_snprintf(pos, end - pos, "%s\n",
3328 					  driver_flag_to_string(1LLU << i));
3329 			if (os_snprintf_error(end - pos, ret))
3330 				return -1;
3331 			pos += ret;
3332 		}
3333 	}
3334 
3335 	return pos - buf;
3336 }
3337 
3338 
3339 static int hostapd_ctrl_driver_flags2(struct hostapd_iface *iface, char *buf,
3340 				      size_t buflen)
3341 {
3342 	int ret, i;
3343 	char *pos, *end;
3344 
3345 	ret = os_snprintf(buf, buflen, "%016llX:\n",
3346 			  (long long unsigned) iface->drv_flags2);
3347 	if (os_snprintf_error(buflen, ret))
3348 		return -1;
3349 
3350 	pos = buf + ret;
3351 	end = buf + buflen;
3352 
3353 	for (i = 0; i < 64; i++) {
3354 		if (iface->drv_flags2 & (1LLU << i)) {
3355 			ret = os_snprintf(pos, end - pos, "%s\n",
3356 					  driver_flag2_to_string(1LLU << i));
3357 			if (os_snprintf_error(end - pos, ret))
3358 				return -1;
3359 			pos += ret;
3360 		}
3361 	}
3362 
3363 	return pos - buf;
3364 }
3365 
3366 
3367 static int hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry **acl, int *num,
3368 					  const char *txtaddr)
3369 {
3370 	u8 addr[ETH_ALEN];
3371 	struct vlan_description vlan_id;
3372 
3373 	if (!(*num))
3374 		return 0;
3375 
3376 	if (hwaddr_aton(txtaddr, addr))
3377 		return -1;
3378 
3379 	if (hostapd_maclist_found(*acl, *num, addr, &vlan_id))
3380 		hostapd_remove_acl_mac(acl, num, addr);
3381 
3382 	return 0;
3383 }
3384 
3385 
3386 static void hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry **acl,
3387 					      int *num)
3388 {
3389 	while (*num)
3390 		hostapd_remove_acl_mac(acl, num, (*acl)[0].addr);
3391 }
3392 
3393 
3394 static int hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry *acl, int num,
3395 					   char *buf, size_t buflen)
3396 {
3397 	int i = 0, len = 0, ret = 0;
3398 
3399 	if (!acl)
3400 		return 0;
3401 
3402 	while (i < num) {
3403 		ret = os_snprintf(buf + len, buflen - len,
3404 				  MACSTR " VLAN_ID=%d\n",
3405 				  MAC2STR(acl[i].addr),
3406 				  acl[i].vlan_id.untagged);
3407 		if (ret < 0 || (size_t) ret >= buflen - len)
3408 			return len;
3409 		i++;
3410 		len += ret;
3411 	}
3412 	return len;
3413 }
3414 
3415 
3416 static int hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry **acl, int *num,
3417 					  const char *cmd)
3418 {
3419 	u8 addr[ETH_ALEN];
3420 	struct vlan_description vlan_id;
3421 	int ret = 0, vlanid = 0;
3422 	const char *pos;
3423 
3424 	if (hwaddr_aton(cmd, addr))
3425 		return -1;
3426 
3427 	pos = os_strstr(cmd, "VLAN_ID=");
3428 	if (pos)
3429 		vlanid = atoi(pos + 8);
3430 
3431 	if (!hostapd_maclist_found(*acl, *num, addr, &vlan_id)) {
3432 		ret = hostapd_add_acl_maclist(acl, num, vlanid, addr);
3433 		if (ret != -1 && *acl)
3434 			qsort(*acl, *num, sizeof(**acl), hostapd_acl_comp);
3435 	}
3436 
3437 	return ret < 0 ? -1 : 0;
3438 }
3439 
3440 
3441 static int hostapd_ctrl_iface_get_capability(struct hostapd_data *hapd,
3442 					     const char *field, char *buf,
3443 					     size_t buflen)
3444 {
3445 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);
3446 
3447 #ifdef CONFIG_DPP
3448 	if (os_strcmp(field, "dpp") == 0) {
3449 		int res;
3450 
3451 #ifdef CONFIG_DPP2
3452 		res = os_snprintf(buf, buflen, "DPP=2");
3453 #else /* CONFIG_DPP2 */
3454 		res = os_snprintf(buf, buflen, "DPP=1");
3455 #endif /* CONFIG_DPP2 */
3456 		if (os_snprintf_error(buflen, res))
3457 			return -1;
3458 		return res;
3459 	}
3460 #endif /* CONFIG_DPP */
3461 
3462 	wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
3463 		   field);
3464 
3465 	return -1;
3466 }
3467 
3468 
3469 #ifdef ANDROID
3470 static int hostapd_ctrl_iface_driver_cmd(struct hostapd_data *hapd, char *cmd,
3471 					 char *buf, size_t buflen)
3472 {
3473 	int ret;
3474 
3475 	ret = hostapd_drv_driver_cmd(hapd, cmd, buf, buflen);
3476 	if (ret == 0) {
3477 		ret = os_snprintf(buf, buflen, "%s\n", "OK");
3478 		if (os_snprintf_error(buflen, ret))
3479 			ret = -1;
3480 	}
3481 	return ret;
3482 }
3483 #endif /* ANDROID */
3484 
3485 
3486 static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
3487 					      char *buf, char *reply,
3488 					      int reply_size,
3489 					      struct sockaddr_storage *from,
3490 					      socklen_t fromlen)
3491 {
3492 	int reply_len, res;
3493 
3494 	os_memcpy(reply, "OK\n", 3);
3495 	reply_len = 3;
3496 
3497 	if (os_strcmp(buf, "PING") == 0) {
3498 		os_memcpy(reply, "PONG\n", 5);
3499 		reply_len = 5;
3500 	} else if (os_strncmp(buf, "RELOG", 5) == 0) {
3501 		if (wpa_debug_reopen_file() < 0)
3502 			reply_len = -1;
3503 	} else if (os_strncmp(buf, "NOTE ", 5) == 0) {
3504 		wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
3505 	} else if (os_strcmp(buf, "STATUS") == 0) {
3506 		reply_len = hostapd_ctrl_iface_status(hapd, reply,
3507 						      reply_size);
3508 	} else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
3509 		reply_len = hostapd_drv_status(hapd, reply, reply_size);
3510 	} else if (os_strcmp(buf, "MIB") == 0) {
3511 		reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
3512 		if (reply_len >= 0) {
3513 			res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
3514 					  reply_size - reply_len);
3515 			if (res < 0)
3516 				reply_len = -1;
3517 			else
3518 				reply_len += res;
3519 		}
3520 		if (reply_len >= 0) {
3521 			res = ieee802_1x_get_mib(hapd, reply + reply_len,
3522 						 reply_size - reply_len);
3523 			if (res < 0)
3524 				reply_len = -1;
3525 			else
3526 				reply_len += res;
3527 		}
3528 #ifndef CONFIG_NO_RADIUS
3529 		if (reply_len >= 0) {
3530 			res = radius_client_get_mib(hapd->radius,
3531 						    reply + reply_len,
3532 						    reply_size - reply_len);
3533 			if (res < 0)
3534 				reply_len = -1;
3535 			else
3536 				reply_len += res;
3537 		}
3538 #endif /* CONFIG_NO_RADIUS */
3539 	} else if (os_strncmp(buf, "MIB ", 4) == 0) {
3540 		reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
3541 						   buf + 4);
3542 	} else if (os_strcmp(buf, "STA-FIRST") == 0) {
3543 		reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
3544 							 reply_size);
3545 	} else if (os_strncmp(buf, "STA ", 4) == 0) {
3546 		reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
3547 						   reply_size);
3548 	} else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
3549 		reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
3550 							reply_size);
3551 	} else if (os_strcmp(buf, "ATTACH") == 0) {
3552 		if (hostapd_ctrl_iface_attach(hapd, from, fromlen, NULL))
3553 			reply_len = -1;
3554 	} else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
3555 		if (hostapd_ctrl_iface_attach(hapd, from, fromlen, buf + 7))
3556 			reply_len = -1;
3557 	} else if (os_strcmp(buf, "DETACH") == 0) {
3558 		if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
3559 			reply_len = -1;
3560 	} else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
3561 		if (hostapd_ctrl_iface_level(hapd, from, fromlen,
3562 						    buf + 6))
3563 			reply_len = -1;
3564 	} else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
3565 		if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
3566 			reply_len = -1;
3567 	} else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
3568 		if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
3569 			reply_len = -1;
3570 	} else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
3571 		if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
3572 			reply_len = -1;
3573 #ifdef CONFIG_TAXONOMY
3574 	} else if (os_strncmp(buf, "SIGNATURE ", 10) == 0) {
3575 		reply_len = hostapd_ctrl_iface_signature(hapd, buf + 10,
3576 							 reply, reply_size);
3577 #endif /* CONFIG_TAXONOMY */
3578 	} else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
3579 		if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
3580 			reply_len = -1;
3581 	} else if (os_strcmp(buf, "STOP_AP") == 0) {
3582 		if (hostapd_ctrl_iface_stop_ap(hapd))
3583 			reply_len = -1;
3584 #ifdef NEED_AP_MLME
3585 	} else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
3586 		if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
3587 			reply_len = -1;
3588 #endif /* NEED_AP_MLME */
3589 #ifdef CONFIG_WPS
3590 	} else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
3591 		if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
3592 			reply_len = -1;
3593 	} else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
3594 		reply_len = hostapd_ctrl_iface_wps_check_pin(
3595 			hapd, buf + 14, reply, reply_size);
3596 	} else if (os_strcmp(buf, "WPS_PBC") == 0) {
3597 		if (hostapd_wps_button_pushed(hapd, NULL))
3598 			reply_len = -1;
3599 	} else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
3600 		if (hostapd_wps_cancel(hapd))
3601 			reply_len = -1;
3602 	} else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
3603 		reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
3604 							  reply, reply_size);
3605 	} else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
3606 		if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
3607 			reply_len = -1;
3608 	} else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
3609 		reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
3610 							      reply_size);
3611 #ifdef CONFIG_WPS_NFC
3612 	} else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
3613 		if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
3614 			reply_len = -1;
3615 	} else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
3616 		reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
3617 			hapd, buf + 21, reply, reply_size);
3618 	} else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
3619 		reply_len = hostapd_ctrl_iface_wps_nfc_token(
3620 			hapd, buf + 14, reply, reply_size);
3621 	} else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
3622 		reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
3623 			hapd, buf + 21, reply, reply_size);
3624 	} else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
3625 		if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
3626 			reply_len = -1;
3627 #endif /* CONFIG_WPS_NFC */
3628 #endif /* CONFIG_WPS */
3629 #ifdef CONFIG_INTERWORKING
3630 	} else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
3631 		if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
3632 			reply_len = -1;
3633 	} else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
3634 		if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
3635 			reply_len = -1;
3636 #endif /* CONFIG_INTERWORKING */
3637 #ifdef CONFIG_HS20
3638 	} else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
3639 		if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
3640 			reply_len = -1;
3641 	} else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
3642 		if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
3643 			reply_len = -1;
3644 #endif /* CONFIG_HS20 */
3645 #ifdef CONFIG_WNM_AP
3646 	} else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
3647 		if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
3648 			reply_len = -1;
3649 	} else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
3650 		if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
3651 			reply_len = -1;
3652 	} else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
3653 		if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
3654 			reply_len = -1;
3655 	} else if (os_strncmp(buf, "COLOC_INTF_REQ ", 15) == 0) {
3656 		if (hostapd_ctrl_iface_coloc_intf_req(hapd, buf + 15))
3657 			reply_len = -1;
3658 #endif /* CONFIG_WNM_AP */
3659 	} else if (os_strcmp(buf, "GET_CONFIG") == 0) {
3660 		reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
3661 							  reply_size);
3662 	} else if (os_strncmp(buf, "SET ", 4) == 0) {
3663 		if (hostapd_ctrl_iface_set(hapd, buf + 4))
3664 			reply_len = -1;
3665 	} else if (os_strncmp(buf, "GET ", 4) == 0) {
3666 		reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
3667 						   reply_size);
3668 	} else if (os_strncmp(buf, "ENABLE", 6) == 0) {
3669 		if (hostapd_ctrl_iface_enable(hapd->iface))
3670 			reply_len = -1;
3671 	} else if (os_strcmp(buf, "RELOAD_WPA_PSK") == 0) {
3672 		if (hostapd_ctrl_iface_reload_wpa_psk(hapd))
3673 			reply_len = -1;
3674 	} else if (os_strncmp(buf, "RELOAD", 6) == 0) {
3675 		if (hostapd_ctrl_iface_reload(hapd->iface))
3676 			reply_len = -1;
3677 	} else if (os_strncmp(buf, "DISABLE", 7) == 0) {
3678 		if (hostapd_ctrl_iface_disable(hapd->iface))
3679 			reply_len = -1;
3680 	} else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
3681 		if (ieee802_11_set_beacon(hapd))
3682 			reply_len = -1;
3683 #ifdef CONFIG_TESTING_OPTIONS
3684 	} else if (os_strncmp(buf, "RADAR ", 6) == 0) {
3685 		if (hostapd_ctrl_iface_radar(hapd, buf + 6))
3686 			reply_len = -1;
3687 	} else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
3688 		if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
3689 			reply_len = -1;
3690 	} else if (os_strncmp(buf, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
3691 		if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd,
3692 							      buf + 23) < 0)
3693 			reply_len = -1;
3694 	} else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
3695 		if (hostapd_ctrl_iface_mgmt_rx_process(hapd, buf + 16) < 0)
3696 			reply_len = -1;
3697 	} else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
3698 		if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
3699 			reply_len = -1;
3700 	} else if (os_strncmp(buf, "EAPOL_TX ", 9) == 0) {
3701 		if (hostapd_ctrl_iface_eapol_tx(hapd, buf + 9) < 0)
3702 			reply_len = -1;
3703 	} else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
3704 		if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
3705 			reply_len = -1;
3706 	} else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
3707 		if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
3708 			reply_len = -1;
3709 	} else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
3710 		if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
3711 			reply_len = -1;
3712 	} else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
3713 		if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
3714 			reply_len = -1;
3715 	} else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
3716 		reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
3717 							reply_size);
3718 	} else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
3719 		if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
3720 			reply_len = -1;
3721 	} else if (os_strcmp(buf, "GET_FAIL") == 0) {
3722 		reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
3723 	} else if (os_strncmp(buf, "RESET_PN ", 9) == 0) {
3724 		if (hostapd_ctrl_reset_pn(hapd, buf + 9) < 0)
3725 			reply_len = -1;
3726 	} else if (os_strncmp(buf, "SET_KEY ", 8) == 0) {
3727 		if (hostapd_ctrl_set_key(hapd, buf + 8) < 0)
3728 			reply_len = -1;
3729 	} else if (os_strncmp(buf, "RESEND_M1 ", 10) == 0) {
3730 		if (hostapd_ctrl_resend_m1(hapd, buf + 10) < 0)
3731 			reply_len = -1;
3732 	} else if (os_strncmp(buf, "RESEND_M3 ", 10) == 0) {
3733 		if (hostapd_ctrl_resend_m3(hapd, buf + 10) < 0)
3734 			reply_len = -1;
3735 	} else if (os_strncmp(buf, "RESEND_GROUP_M1 ", 16) == 0) {
3736 		if (hostapd_ctrl_resend_group_m1(hapd, buf + 16) < 0)
3737 			reply_len = -1;
3738 	} else if (os_strncmp(buf, "REKEY_PTK ", 10) == 0) {
3739 		if (hostapd_ctrl_rekey_ptk(hapd, buf + 10) < 0)
3740 			reply_len = -1;
3741 	} else if (os_strcmp(buf, "REKEY_GTK") == 0) {
3742 		if (wpa_auth_rekey_gtk(hapd->wpa_auth) < 0)
3743 			reply_len = -1;
3744 	} else if (os_strncmp(buf, "GET_PMK ", 8) == 0) {
3745 		reply_len = hostapd_ctrl_get_pmk(hapd, buf + 8, reply,
3746 						 reply_size);
3747 	} else if (os_strncmp(buf, "REGISTER_FRAME ", 15) == 0) {
3748 		if (hostapd_ctrl_register_frame(hapd, buf + 16) < 0)
3749 			reply_len = -1;
3750 #endif /* CONFIG_TESTING_OPTIONS */
3751 	} else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
3752 		if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
3753 			reply_len = -1;
3754 	} else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
3755 		reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
3756 						      reply_size);
3757 	} else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
3758 		ieee802_1x_erp_flush(hapd);
3759 #ifdef RADIUS_SERVER
3760 		radius_server_erp_flush(hapd->radius_srv);
3761 #endif /* RADIUS_SERVER */
3762 	} else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
3763 		if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
3764 			reply_len = -1;
3765 	} else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
3766 		if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
3767 			reply_len = -1;
3768 	} else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
3769 		reply_len = hostapd_ctrl_iface_log_level(
3770 			hapd, buf + 9, reply, reply_size);
3771 #ifdef NEED_AP_MLME
3772 	} else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
3773 		reply_len = hostapd_ctrl_iface_track_sta_list(
3774 			hapd, reply, reply_size);
3775 #endif /* NEED_AP_MLME */
3776 	} else if (os_strcmp(buf, "PMKSA") == 0) {
3777 		reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
3778 							  reply_size);
3779 	} else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
3780 		hostapd_ctrl_iface_pmksa_flush(hapd);
3781 	} else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
3782 		if (hostapd_ctrl_iface_pmksa_add(hapd, buf + 10) < 0)
3783 			reply_len = -1;
3784 	} else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
3785 		if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
3786 			reply_len = -1;
3787 	} else if (os_strcmp(buf, "SHOW_NEIGHBOR") == 0) {
3788 		reply_len = hostapd_ctrl_iface_show_neighbor(hapd, reply,
3789 							     reply_size);
3790 	} else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
3791 		if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
3792 			reply_len = -1;
3793 	} else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
3794 		if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
3795 			reply_len = -1;
3796 	} else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
3797 		if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
3798 			reply_len = -1;
3799 	} else if (os_strncmp(buf, "REQ_BEACON ", 11) == 0) {
3800 		reply_len = hostapd_ctrl_iface_req_beacon(hapd, buf + 11,
3801 							  reply, reply_size);
3802 	} else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
3803 		reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
3804 						      reply_size);
3805 	} else if (os_strcmp(buf, "DRIVER_FLAGS2") == 0) {
3806 		reply_len = hostapd_ctrl_driver_flags2(hapd->iface, reply,
3807 						       reply_size);
3808 	} else if (os_strcmp(buf, "TERMINATE") == 0) {
3809 		eloop_terminate();
3810 	} else if (os_strncmp(buf, "ACCEPT_ACL ", 11) == 0) {
3811 		if (os_strncmp(buf + 11, "ADD_MAC ", 8) == 0) {
3812 			if (hostapd_ctrl_iface_acl_add_mac(
3813 				    &hapd->conf->accept_mac,
3814 				    &hapd->conf->num_accept_mac, buf + 19))
3815 				reply_len = -1;
3816 		} else if (os_strncmp((buf + 11), "DEL_MAC ", 8) == 0) {
3817 			if (!hostapd_ctrl_iface_acl_del_mac(
3818 				    &hapd->conf->accept_mac,
3819 				    &hapd->conf->num_accept_mac, buf + 19))
3820 				hostapd_disassoc_accept_mac(hapd);
3821 			else
3822 				reply_len = -1;
3823 		} else if (os_strcmp(buf + 11, "SHOW") == 0) {
3824 			reply_len = hostapd_ctrl_iface_acl_show_mac(
3825 				hapd->conf->accept_mac,
3826 				hapd->conf->num_accept_mac, reply, reply_size);
3827 		} else if (os_strcmp(buf + 11, "CLEAR") == 0) {
3828 			hostapd_ctrl_iface_acl_clear_list(
3829 				&hapd->conf->accept_mac,
3830 				&hapd->conf->num_accept_mac);
3831 			hostapd_disassoc_accept_mac(hapd);
3832 		}
3833 	} else if (os_strncmp(buf, "DENY_ACL ", 9) == 0) {
3834 		if (os_strncmp(buf + 9, "ADD_MAC ", 8) == 0) {
3835 			if (!hostapd_ctrl_iface_acl_add_mac(
3836 				    &hapd->conf->deny_mac,
3837 				    &hapd->conf->num_deny_mac, buf + 17))
3838 				hostapd_disassoc_deny_mac(hapd);
3839 			else
3840 				reply_len = -1;
3841 		} else if (os_strncmp(buf + 9, "DEL_MAC ", 8) == 0) {
3842 			if (hostapd_ctrl_iface_acl_del_mac(
3843 				    &hapd->conf->deny_mac,
3844 				    &hapd->conf->num_deny_mac, buf + 17))
3845 				reply_len = -1;
3846 		} else if (os_strcmp(buf + 9, "SHOW") == 0) {
3847 			reply_len = hostapd_ctrl_iface_acl_show_mac(
3848 				hapd->conf->deny_mac,
3849 				hapd->conf->num_deny_mac, reply, reply_size);
3850 		} else if (os_strcmp(buf + 9, "CLEAR") == 0) {
3851 			hostapd_ctrl_iface_acl_clear_list(
3852 				&hapd->conf->deny_mac,
3853 				&hapd->conf->num_deny_mac);
3854 		}
3855 #ifdef CONFIG_DPP
3856 	} else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
3857 		res = hostapd_dpp_qr_code(hapd, buf + 12);
3858 		if (res < 0) {
3859 			reply_len = -1;
3860 		} else {
3861 			reply_len = os_snprintf(reply, reply_size, "%d", res);
3862 			if (os_snprintf_error(reply_size, reply_len))
3863 				reply_len = -1;
3864 		}
3865 	} else if (os_strncmp(buf, "DPP_NFC_URI ", 12) == 0) {
3866 		res = hostapd_dpp_nfc_uri(hapd, buf + 12);
3867 		if (res < 0) {
3868 			reply_len = -1;
3869 		} else {
3870 			reply_len = os_snprintf(reply, reply_size, "%d", res);
3871 			if (os_snprintf_error(reply_size, reply_len))
3872 				reply_len = -1;
3873 		}
3874 	} else if (os_strncmp(buf, "DPP_NFC_HANDOVER_REQ ", 21) == 0) {
3875 		res = hostapd_dpp_nfc_handover_req(hapd, buf + 20);
3876 		if (res < 0) {
3877 			reply_len = -1;
3878 		} else {
3879 			reply_len = os_snprintf(reply, reply_size, "%d", res);
3880 			if (os_snprintf_error(reply_size, reply_len))
3881 				reply_len = -1;
3882 		}
3883 	} else if (os_strncmp(buf, "DPP_NFC_HANDOVER_SEL ", 21) == 0) {
3884 		res = hostapd_dpp_nfc_handover_sel(hapd, buf + 20);
3885 		if (res < 0) {
3886 			reply_len = -1;
3887 		} else {
3888 			reply_len = os_snprintf(reply, reply_size, "%d", res);
3889 			if (os_snprintf_error(reply_size, reply_len))
3890 				reply_len = -1;
3891 		}
3892 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
3893 		res = dpp_bootstrap_gen(hapd->iface->interfaces->dpp, buf + 18);
3894 		if (res < 0) {
3895 			reply_len = -1;
3896 		} else {
3897 			reply_len = os_snprintf(reply, reply_size, "%d", res);
3898 			if (os_snprintf_error(reply_size, reply_len))
3899 				reply_len = -1;
3900 		}
3901 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
3902 		if (dpp_bootstrap_remove(hapd->iface->interfaces->dpp,
3903 					 buf + 21) < 0)
3904 			reply_len = -1;
3905 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
3906 		const char *uri;
3907 
3908 		uri = dpp_bootstrap_get_uri(hapd->iface->interfaces->dpp,
3909 					    atoi(buf + 22));
3910 		if (!uri) {
3911 			reply_len = -1;
3912 		} else {
3913 			reply_len = os_snprintf(reply, reply_size, "%s", uri);
3914 			if (os_snprintf_error(reply_size, reply_len))
3915 				reply_len = -1;
3916 		}
3917 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
3918 		reply_len = dpp_bootstrap_info(hapd->iface->interfaces->dpp,
3919 					       atoi(buf + 19),
3920 			reply, reply_size);
3921 	} else if (os_strncmp(buf, "DPP_BOOTSTRAP_SET ", 18) == 0) {
3922 		if (dpp_bootstrap_set(hapd->iface->interfaces->dpp,
3923 				      atoi(buf + 18),
3924 				      os_strchr(buf + 18, ' ')) < 0)
3925 			reply_len = -1;
3926 	} else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
3927 		if (hostapd_dpp_auth_init(hapd, buf + 13) < 0)
3928 			reply_len = -1;
3929 	} else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
3930 		if (hostapd_dpp_listen(hapd, buf + 11) < 0)
3931 			reply_len = -1;
3932 	} else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
3933 		hostapd_dpp_stop(hapd);
3934 		hostapd_dpp_listen_stop(hapd);
3935 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
3936 		res = dpp_configurator_add(hapd->iface->interfaces->dpp,
3937 					   buf + 20);
3938 		if (res < 0) {
3939 			reply_len = -1;
3940 		} else {
3941 			reply_len = os_snprintf(reply, reply_size, "%d", res);
3942 			if (os_snprintf_error(reply_size, reply_len))
3943 				reply_len = -1;
3944 		}
3945 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
3946 		if (dpp_configurator_remove(hapd->iface->interfaces->dpp,
3947 					    buf + 24) < 0)
3948 			reply_len = -1;
3949 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
3950 		if (hostapd_dpp_configurator_sign(hapd, buf + 21) < 0)
3951 			reply_len = -1;
3952 	} else if (os_strncmp(buf, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
3953 		reply_len = dpp_configurator_get_key_id(
3954 			hapd->iface->interfaces->dpp,
3955 			atoi(buf + 25),
3956 			reply, reply_size);
3957 	} else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
3958 		res = hostapd_dpp_pkex_add(hapd, buf + 12);
3959 		if (res < 0) {
3960 			reply_len = -1;
3961 		} else {
3962 			reply_len = os_snprintf(reply, reply_size, "%d", res);
3963 			if (os_snprintf_error(reply_size, reply_len))
3964 				reply_len = -1;
3965 		}
3966 	} else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
3967 		if (hostapd_dpp_pkex_remove(hapd, buf + 16) < 0)
3968 			reply_len = -1;
3969 #ifdef CONFIG_DPP2
3970 	} else if (os_strncmp(buf, "DPP_CONTROLLER_START ", 21) == 0) {
3971 		if (hostapd_dpp_controller_start(hapd, buf + 20) < 0)
3972 			reply_len = -1;
3973 	} else if (os_strcmp(buf, "DPP_CONTROLLER_START") == 0) {
3974 		if (hostapd_dpp_controller_start(hapd, NULL) < 0)
3975 			reply_len = -1;
3976 	} else if (os_strcmp(buf, "DPP_CONTROLLER_STOP") == 0) {
3977 		dpp_controller_stop(hapd->iface->interfaces->dpp);
3978 	} else if (os_strncmp(buf, "DPP_CHIRP ", 10) == 0) {
3979 		if (hostapd_dpp_chirp(hapd, buf + 9) < 0)
3980 			reply_len = -1;
3981 	} else if (os_strcmp(buf, "DPP_STOP_CHIRP") == 0) {
3982 		hostapd_dpp_chirp_stop(hapd);
3983 #endif /* CONFIG_DPP2 */
3984 #endif /* CONFIG_DPP */
3985 #ifdef RADIUS_SERVER
3986 	} else if (os_strncmp(buf, "DAC_REQUEST ", 12) == 0) {
3987 		if (radius_server_dac_request(hapd->radius_srv, buf + 12) < 0)
3988 			reply_len = -1;
3989 #endif /* RADIUS_SERVER */
3990 	} else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
3991 		reply_len = hostapd_ctrl_iface_get_capability(
3992 			hapd, buf + 15, reply, reply_size);
3993 #ifdef CONFIG_PASN
3994 	} else if (os_strcmp(buf, "PTKSA_CACHE_LIST") == 0) {
3995 		reply_len = ptksa_cache_list(hapd->ptksa, reply, reply_size);
3996 #endif /* CONFIG_PASN */
3997 #ifdef ANDROID
3998 	} else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
3999 		reply_len = hostapd_ctrl_iface_driver_cmd(hapd, buf + 7, reply,
4000 							  reply_size);
4001 #endif /* ANDROID */
4002 	} else {
4003 		os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
4004 		reply_len = 16;
4005 	}
4006 
4007 	if (reply_len < 0) {
4008 		os_memcpy(reply, "FAIL\n", 5);
4009 		reply_len = 5;
4010 	}
4011 
4012 	return reply_len;
4013 }
4014 
4015 
4016 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
4017 				       void *sock_ctx)
4018 {
4019 	struct hostapd_data *hapd = eloop_ctx;
4020 	char buf[4096];
4021 	int res;
4022 	struct sockaddr_storage from;
4023 	socklen_t fromlen = sizeof(from);
4024 	char *reply, *pos = buf;
4025 	const int reply_size = 4096;
4026 	int reply_len;
4027 	int level = MSG_DEBUG;
4028 #ifdef CONFIG_CTRL_IFACE_UDP
4029 	unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
4030 #endif /* CONFIG_CTRL_IFACE_UDP */
4031 
4032 	res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
4033 		       (struct sockaddr *) &from, &fromlen);
4034 	if (res < 0) {
4035 		wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4036 			   strerror(errno));
4037 		return;
4038 	}
4039 	buf[res] = '\0';
4040 
4041 	reply = os_malloc(reply_size);
4042 	if (reply == NULL) {
4043 		if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4044 			   fromlen) < 0) {
4045 			wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4046 				   strerror(errno));
4047 		}
4048 		return;
4049 	}
4050 
4051 #ifdef CONFIG_CTRL_IFACE_UDP
4052 	if (os_strcmp(buf, "GET_COOKIE") == 0) {
4053 		os_memcpy(reply, "COOKIE=", 7);
4054 		wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4055 				 hapd->ctrl_iface_cookie,
4056 				 CTRL_IFACE_COOKIE_LEN);
4057 		reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
4058 		goto done;
4059 	}
4060 
4061 	if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
4062 	    hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
4063 		wpa_printf(MSG_DEBUG,
4064 			   "CTRL: No cookie in the request - drop request");
4065 		os_free(reply);
4066 		return;
4067 	}
4068 
4069 	if (os_memcmp(hapd->ctrl_iface_cookie, lcookie,
4070 		      CTRL_IFACE_COOKIE_LEN) != 0) {
4071 		wpa_printf(MSG_DEBUG,
4072 			   "CTRL: Invalid cookie in the request - drop request");
4073 		os_free(reply);
4074 		return;
4075 	}
4076 
4077 	pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN;
4078 	while (*pos == ' ')
4079 		pos++;
4080 #endif /* CONFIG_CTRL_IFACE_UDP */
4081 
4082 	if (os_strcmp(pos, "PING") == 0)
4083 		level = MSG_EXCESSIVE;
4084 	wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
4085 
4086 	reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
4087 						       reply, reply_size,
4088 						       &from, fromlen);
4089 
4090 #ifdef CONFIG_CTRL_IFACE_UDP
4091 done:
4092 #endif /* CONFIG_CTRL_IFACE_UDP */
4093 	if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4094 		   fromlen) < 0) {
4095 		wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4096 			   strerror(errno));
4097 	}
4098 	os_free(reply);
4099 }
4100 
4101 
4102 #ifndef CONFIG_CTRL_IFACE_UDP
4103 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
4104 {
4105 	char *buf;
4106 	size_t len;
4107 
4108 	if (hapd->conf->ctrl_interface == NULL)
4109 		return NULL;
4110 
4111 	len = os_strlen(hapd->conf->ctrl_interface) +
4112 		os_strlen(hapd->conf->iface) + 2;
4113 	buf = os_malloc(len);
4114 	if (buf == NULL)
4115 		return NULL;
4116 
4117 	os_snprintf(buf, len, "%s/%s",
4118 		    hapd->conf->ctrl_interface, hapd->conf->iface);
4119 	buf[len - 1] = '\0';
4120 	return buf;
4121 }
4122 #endif /* CONFIG_CTRL_IFACE_UDP */
4123 
4124 
4125 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
4126 				      enum wpa_msg_type type,
4127 				      const char *txt, size_t len)
4128 {
4129 	struct hostapd_data *hapd = ctx;
4130 	if (hapd == NULL)
4131 		return;
4132 	hostapd_ctrl_iface_send(hapd, level, type, txt, len);
4133 }
4134 
4135 
4136 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
4137 {
4138 #ifdef CONFIG_CTRL_IFACE_UDP
4139 	int port = HOSTAPD_CTRL_IFACE_PORT;
4140 	char p[32] = { 0 };
4141 	char port_str[40], *tmp;
4142 	char *pos;
4143 	struct addrinfo hints = { 0 }, *res, *saveres;
4144 	int n;
4145 
4146 	if (hapd->ctrl_sock > -1) {
4147 		wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4148 		return 0;
4149 	}
4150 
4151 	if (hapd->conf->ctrl_interface == NULL)
4152 		return 0;
4153 
4154 	pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
4155 	if (pos) {
4156 		pos += 4;
4157 		port = atoi(pos);
4158 		if (port <= 0) {
4159 			wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
4160 			goto fail;
4161 		}
4162 	}
4163 
4164 	dl_list_init(&hapd->ctrl_dst);
4165 	hapd->ctrl_sock = -1;
4166 	os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
4167 
4168 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4169 	hints.ai_flags = AI_PASSIVE;
4170 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4171 
4172 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4173 	hints.ai_family = AF_INET6;
4174 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4175 	hints.ai_family = AF_INET;
4176 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4177 	hints.ai_socktype = SOCK_DGRAM;
4178 
4179 try_again:
4180 	os_snprintf(p, sizeof(p), "%d", port);
4181 	n = getaddrinfo(NULL, p, &hints, &res);
4182 	if (n) {
4183 		wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4184 		goto fail;
4185 	}
4186 
4187 	saveres = res;
4188 	hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
4189 				 res->ai_protocol);
4190 	if (hapd->ctrl_sock < 0) {
4191 		wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4192 		goto fail;
4193 	}
4194 
4195 	if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
4196 		port--;
4197 		if ((HOSTAPD_CTRL_IFACE_PORT - port) <
4198 		    HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
4199 			goto try_again;
4200 		wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4201 		goto fail;
4202 	}
4203 
4204 	freeaddrinfo(saveres);
4205 
4206 	os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
4207 	tmp = os_strdup(port_str);
4208 	if (tmp) {
4209 		os_free(hapd->conf->ctrl_interface);
4210 		hapd->conf->ctrl_interface = tmp;
4211 	}
4212 	wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
4213 
4214 	if (eloop_register_read_sock(hapd->ctrl_sock,
4215 				     hostapd_ctrl_iface_receive, hapd, NULL) <
4216 	    0) {
4217 		hostapd_ctrl_iface_deinit(hapd);
4218 		return -1;
4219 	}
4220 
4221 	hapd->msg_ctx = hapd;
4222 	wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4223 
4224 	return 0;
4225 
4226 fail:
4227 	if (hapd->ctrl_sock >= 0)
4228 		close(hapd->ctrl_sock);
4229 	return -1;
4230 #else /* CONFIG_CTRL_IFACE_UDP */
4231 	struct sockaddr_un addr;
4232 	int s = -1;
4233 	char *fname = NULL;
4234 
4235 	if (hapd->ctrl_sock > -1) {
4236 		wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4237 		return 0;
4238 	}
4239 
4240 	dl_list_init(&hapd->ctrl_dst);
4241 
4242 	if (hapd->conf->ctrl_interface == NULL)
4243 		return 0;
4244 
4245 	if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4246 		if (errno == EEXIST) {
4247 			wpa_printf(MSG_DEBUG, "Using existing control "
4248 				   "interface directory.");
4249 		} else {
4250 			wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
4251 				   strerror(errno));
4252 			goto fail;
4253 		}
4254 	}
4255 
4256 	if (hapd->conf->ctrl_interface_gid_set &&
4257 	    lchown(hapd->conf->ctrl_interface, -1,
4258 		   hapd->conf->ctrl_interface_gid) < 0) {
4259 		wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
4260 			   strerror(errno));
4261 		return -1;
4262 	}
4263 
4264 	if (!hapd->conf->ctrl_interface_gid_set &&
4265 	    hapd->iface->interfaces->ctrl_iface_group &&
4266 	    lchown(hapd->conf->ctrl_interface, -1,
4267 		   hapd->iface->interfaces->ctrl_iface_group) < 0) {
4268 		wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
4269 			   strerror(errno));
4270 		return -1;
4271 	}
4272 
4273 #ifdef ANDROID
4274 	/*
4275 	 * Android is using umask 0077 which would leave the control interface
4276 	 * directory without group access. This breaks things since Wi-Fi
4277 	 * framework assumes that this directory can be accessed by other
4278 	 * applications in the wifi group. Fix this by adding group access even
4279 	 * if umask value would prevent this.
4280 	 */
4281 	if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
4282 		wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
4283 			   strerror(errno));
4284 		/* Try to continue anyway */
4285 	}
4286 #endif /* ANDROID */
4287 
4288 	if (os_strlen(hapd->conf->ctrl_interface) + 1 +
4289 	    os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
4290 		goto fail;
4291 
4292 	s = socket(PF_UNIX, SOCK_DGRAM, 0);
4293 	if (s < 0) {
4294 		wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
4295 		goto fail;
4296 	}
4297 
4298 	os_memset(&addr, 0, sizeof(addr));
4299 #ifdef __FreeBSD__
4300 	addr.sun_len = sizeof(addr);
4301 #endif /* __FreeBSD__ */
4302 	addr.sun_family = AF_UNIX;
4303 	fname = hostapd_ctrl_iface_path(hapd);
4304 	if (fname == NULL)
4305 		goto fail;
4306 	os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
4307 	if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4308 		wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
4309 			   strerror(errno));
4310 		if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
4311 			wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
4312 				   " allow connections - assuming it was left"
4313 				   "over from forced program termination");
4314 			if (unlink(fname) < 0) {
4315 				wpa_printf(MSG_ERROR,
4316 					   "Could not unlink existing ctrl_iface socket '%s': %s",
4317 					   fname, strerror(errno));
4318 				goto fail;
4319 			}
4320 			if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
4321 			    0) {
4322 				wpa_printf(MSG_ERROR,
4323 					   "hostapd-ctrl-iface: bind(PF_UNIX): %s",
4324 					   strerror(errno));
4325 				goto fail;
4326 			}
4327 			wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
4328 				   "ctrl_iface socket '%s'", fname);
4329 		} else {
4330 			wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
4331 				   "be in use - cannot override it");
4332 			wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
4333 				   "not used anymore", fname);
4334 			os_free(fname);
4335 			fname = NULL;
4336 			goto fail;
4337 		}
4338 	}
4339 
4340 	if (hapd->conf->ctrl_interface_gid_set &&
4341 	    lchown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
4342 		wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
4343 			   strerror(errno));
4344 		goto fail;
4345 	}
4346 
4347 	if (!hapd->conf->ctrl_interface_gid_set &&
4348 	    hapd->iface->interfaces->ctrl_iface_group &&
4349 	    lchown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
4350 		wpa_printf(MSG_ERROR, "lchown[ctrl_interface/ifname]: %s",
4351 			   strerror(errno));
4352 		goto fail;
4353 	}
4354 
4355 	if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
4356 		wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
4357 			   strerror(errno));
4358 		goto fail;
4359 	}
4360 	os_free(fname);
4361 
4362 	hapd->ctrl_sock = s;
4363 	if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
4364 				     NULL) < 0) {
4365 		hostapd_ctrl_iface_deinit(hapd);
4366 		return -1;
4367 	}
4368 	hapd->msg_ctx = hapd;
4369 	wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
4370 
4371 	return 0;
4372 
4373 fail:
4374 	if (s >= 0)
4375 		close(s);
4376 	if (fname) {
4377 		unlink(fname);
4378 		os_free(fname);
4379 	}
4380 	return -1;
4381 #endif /* CONFIG_CTRL_IFACE_UDP */
4382 }
4383 
4384 
4385 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
4386 {
4387 	struct wpa_ctrl_dst *dst, *prev;
4388 
4389 	if (hapd->ctrl_sock > -1) {
4390 #ifndef CONFIG_CTRL_IFACE_UDP
4391 		char *fname;
4392 #endif /* !CONFIG_CTRL_IFACE_UDP */
4393 
4394 		eloop_unregister_read_sock(hapd->ctrl_sock);
4395 		close(hapd->ctrl_sock);
4396 		hapd->ctrl_sock = -1;
4397 #ifndef CONFIG_CTRL_IFACE_UDP
4398 		fname = hostapd_ctrl_iface_path(hapd);
4399 		if (fname)
4400 			unlink(fname);
4401 		os_free(fname);
4402 
4403 		if (hapd->conf->ctrl_interface &&
4404 		    rmdir(hapd->conf->ctrl_interface) < 0) {
4405 			if (errno == ENOTEMPTY) {
4406 				wpa_printf(MSG_DEBUG, "Control interface "
4407 					   "directory not empty - leaving it "
4408 					   "behind");
4409 			} else {
4410 				wpa_printf(MSG_ERROR,
4411 					   "rmdir[ctrl_interface=%s]: %s",
4412 					   hapd->conf->ctrl_interface,
4413 					   strerror(errno));
4414 			}
4415 		}
4416 #endif /* !CONFIG_CTRL_IFACE_UDP */
4417 	}
4418 
4419 	dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
4420 			      list)
4421 		os_free(dst);
4422 
4423 #ifdef CONFIG_TESTING_OPTIONS
4424 	l2_packet_deinit(hapd->l2_test);
4425 	hapd->l2_test = NULL;
4426 #endif /* CONFIG_TESTING_OPTIONS */
4427 }
4428 
4429 
4430 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
4431 				  char *buf)
4432 {
4433 	if (hostapd_add_iface(interfaces, buf) < 0) {
4434 		wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
4435 		return -1;
4436 	}
4437 	return 0;
4438 }
4439 
4440 
4441 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
4442 				     char *buf)
4443 {
4444 	if (hostapd_remove_iface(interfaces, buf) < 0) {
4445 		wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
4446 		return -1;
4447 	}
4448 	return 0;
4449 }
4450 
4451 
4452 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
4453 					    struct sockaddr_storage *from,
4454 					    socklen_t fromlen, char *input)
4455 {
4456 	return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen,
4457 				 input);
4458 }
4459 
4460 
4461 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
4462 					    struct sockaddr_storage *from,
4463 					    socklen_t fromlen)
4464 {
4465 	return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
4466 }
4467 
4468 
4469 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
4470 {
4471 #ifdef CONFIG_WPS_TESTING
4472 	wps_version_number = 0x20;
4473 	wps_testing_dummy_cred = 0;
4474 	wps_corrupt_pkhash = 0;
4475 #endif /* CONFIG_WPS_TESTING */
4476 
4477 #ifdef CONFIG_TESTING_OPTIONS
4478 #ifdef CONFIG_DPP
4479 	dpp_test = DPP_TEST_DISABLED;
4480 #ifdef CONFIG_DPP2
4481 	dpp_version_override = 2;
4482 #else /* CONFIG_DPP2 */
4483 	dpp_version_override = 1;
4484 #endif /* CONFIG_DPP2 */
4485 #endif /* CONFIG_DPP */
4486 #endif /* CONFIG_TESTING_OPTIONS */
4487 
4488 #ifdef CONFIG_DPP
4489 	dpp_global_clear(interfaces->dpp);
4490 #endif /* CONFIG_DPP */
4491 }
4492 
4493 
4494 #ifdef CONFIG_FST
4495 
4496 static int
4497 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
4498 				     const char *cmd)
4499 {
4500 	char ifname[IFNAMSIZ + 1];
4501 	struct fst_iface_cfg cfg;
4502 	struct hostapd_data *hapd;
4503 	struct fst_wpa_obj iface_obj;
4504 
4505 	if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
4506 		hapd = hostapd_get_iface(interfaces, ifname);
4507 		if (hapd) {
4508 			if (hapd->iface->fst) {
4509 				wpa_printf(MSG_INFO, "FST: Already attached");
4510 				return -1;
4511 			}
4512 			fst_hostapd_fill_iface_obj(hapd, &iface_obj);
4513 			hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
4514 						      &iface_obj, &cfg);
4515 			if (hapd->iface->fst)
4516 				return 0;
4517 		}
4518 	}
4519 
4520 	return -EINVAL;
4521 }
4522 
4523 
4524 static int
4525 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
4526 				     const char *cmd)
4527 {
4528 	char ifname[IFNAMSIZ + 1];
4529 	struct hostapd_data * hapd;
4530 
4531 	if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
4532 		hapd = hostapd_get_iface(interfaces, ifname);
4533 		if (hapd) {
4534 			if (!fst_iface_detach(ifname)) {
4535 				hapd->iface->fst = NULL;
4536 				hapd->iface->fst_ies = NULL;
4537 				return 0;
4538 			}
4539 		}
4540 	}
4541 
4542 	return -EINVAL;
4543 }
4544 
4545 #endif /* CONFIG_FST */
4546 
4547 
4548 static struct hostapd_data *
4549 hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
4550 			    const char *ifname)
4551 {
4552 	size_t i, j;
4553 
4554 	for (i = 0; i < interfaces->count; i++) {
4555 		struct hostapd_iface *iface = interfaces->iface[i];
4556 
4557 		for (j = 0; j < iface->num_bss; j++) {
4558 			struct hostapd_data *hapd;
4559 
4560 			hapd = iface->bss[j];
4561 			if (os_strcmp(ifname, hapd->conf->iface) == 0)
4562 				return hapd;
4563 		}
4564 	}
4565 
4566 	return NULL;
4567 }
4568 
4569 
4570 static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
4571 					struct hostapd_data *dst_hapd,
4572 					const char *param)
4573 {
4574 	int res;
4575 	char *value;
4576 
4577 	value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4578 	if (!value) {
4579 		wpa_printf(MSG_ERROR,
4580 			   "DUP: cannot allocate buffer to stringify %s",
4581 			   param);
4582 		goto error_return;
4583 	}
4584 
4585 	if (os_strcmp(param, "wpa") == 0) {
4586 		os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
4587 			    src_hapd->conf->wpa);
4588 	} else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
4589 		   src_hapd->conf->wpa_key_mgmt) {
4590 		res = hostapd_ctrl_iface_get_key_mgmt(
4591 			src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
4592 		if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
4593 			goto error_stringify;
4594 	} else if (os_strcmp(param, "wpa_pairwise") == 0 &&
4595 		   src_hapd->conf->wpa_pairwise) {
4596 		res = wpa_write_ciphers(value,
4597 					value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4598 					src_hapd->conf->wpa_pairwise, " ");
4599 		if (res < 0)
4600 			goto error_stringify;
4601 	} else if (os_strcmp(param, "rsn_pairwise") == 0 &&
4602 		   src_hapd->conf->rsn_pairwise) {
4603 		res = wpa_write_ciphers(value,
4604 					value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4605 					src_hapd->conf->rsn_pairwise, " ");
4606 		if (res < 0)
4607 			goto error_stringify;
4608 	} else if (os_strcmp(param, "wpa_passphrase") == 0 &&
4609 		   src_hapd->conf->ssid.wpa_passphrase) {
4610 		os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
4611 			    src_hapd->conf->ssid.wpa_passphrase);
4612 	} else if (os_strcmp(param, "wpa_psk") == 0 &&
4613 		   src_hapd->conf->ssid.wpa_psk_set) {
4614 		wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
4615 			src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
4616 	} else {
4617 		wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
4618 		goto error_return;
4619 	}
4620 
4621 	res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
4622 	os_free(value);
4623 	return res;
4624 
4625 error_stringify:
4626 	wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
4627 error_return:
4628 	os_free(value);
4629 	return -1;
4630 }
4631 
4632 
4633 static int
4634 hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
4635 				     const char *input,
4636 				     char *reply, int reply_size)
4637 {
4638 	size_t i, j;
4639 	int res;
4640 	char *pos, *end;
4641 	struct hostapd_iface *iface;
4642 	int show_ctrl = 0;
4643 
4644 	if (input)
4645 		show_ctrl = !!os_strstr(input, "ctrl");
4646 
4647 	pos = reply;
4648 	end = reply + reply_size;
4649 
4650 	for (i = 0; i < interfaces->count; i++) {
4651 		iface = interfaces->iface[i];
4652 
4653 		for (j = 0; j < iface->num_bss; j++) {
4654 			struct hostapd_bss_config *conf;
4655 
4656 			conf = iface->conf->bss[j];
4657 			if (show_ctrl)
4658 				res = os_snprintf(pos, end - pos,
4659 						  "%s ctrl_iface=%s\n",
4660 						  conf->iface,
4661 						  conf->ctrl_interface ?
4662 						  conf->ctrl_interface : "N/A");
4663 			else
4664 				res = os_snprintf(pos, end - pos, "%s\n",
4665 						  conf->iface);
4666 			if (os_snprintf_error(end - pos, res)) {
4667 				*pos = '\0';
4668 				return pos - reply;
4669 			}
4670 			pos += res;
4671 		}
4672 	}
4673 
4674 	return pos - reply;
4675 }
4676 
4677 
4678 static int
4679 hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
4680 				      char *cmd)
4681 {
4682 	char *p_start = cmd, *p_end;
4683 	struct hostapd_data *src_hapd, *dst_hapd;
4684 
4685 	/* cmd: "<src ifname> <dst ifname> <variable name> */
4686 
4687 	p_end = os_strchr(p_start, ' ');
4688 	if (!p_end) {
4689 		wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
4690 			   cmd);
4691 		return -1;
4692 	}
4693 
4694 	*p_end = '\0';
4695 	src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4696 	if (!src_hapd) {
4697 		wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
4698 			   p_start);
4699 		return -1;
4700 	}
4701 
4702 	p_start = p_end + 1;
4703 	p_end = os_strchr(p_start, ' ');
4704 	if (!p_end) {
4705 		wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
4706 			   cmd);
4707 		return -1;
4708 	}
4709 
4710 	*p_end = '\0';
4711 	dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
4712 	if (!dst_hapd) {
4713 		wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
4714 			   p_start);
4715 		return -1;
4716 	}
4717 
4718 	p_start = p_end + 1;
4719 	return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
4720 }
4721 
4722 
4723 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
4724 					    const char *ifname,
4725 					    char *buf, char *reply,
4726 					    int reply_size,
4727 					    struct sockaddr_storage *from,
4728 					    socklen_t fromlen)
4729 {
4730 	struct hostapd_data *hapd;
4731 
4732 	hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
4733 	if (hapd == NULL) {
4734 		int res;
4735 
4736 		res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
4737 		if (os_snprintf_error(reply_size, res))
4738 			return -1;
4739 		return res;
4740 	}
4741 
4742 	return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
4743 						  from, fromlen);
4744 }
4745 
4746 
4747 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
4748 					      void *sock_ctx)
4749 {
4750 	struct hapd_interfaces *interfaces = eloop_ctx;
4751 	char buffer[256], *buf = buffer;
4752 	int res;
4753 	struct sockaddr_storage from;
4754 	socklen_t fromlen = sizeof(from);
4755 	char *reply;
4756 	int reply_len;
4757 	const int reply_size = 4096;
4758 #ifdef CONFIG_CTRL_IFACE_UDP
4759 	unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
4760 #endif /* CONFIG_CTRL_IFACE_UDP */
4761 
4762 	res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
4763 		       (struct sockaddr *) &from, &fromlen);
4764 	if (res < 0) {
4765 		wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
4766 			   strerror(errno));
4767 		return;
4768 	}
4769 	buf[res] = '\0';
4770 	wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
4771 
4772 	reply = os_malloc(reply_size);
4773 	if (reply == NULL) {
4774 		if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
4775 			   fromlen) < 0) {
4776 			wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4777 				   strerror(errno));
4778 		}
4779 		return;
4780 	}
4781 
4782 	os_memcpy(reply, "OK\n", 3);
4783 	reply_len = 3;
4784 
4785 #ifdef CONFIG_CTRL_IFACE_UDP
4786 	if (os_strcmp(buf, "GET_COOKIE") == 0) {
4787 		os_memcpy(reply, "COOKIE=", 7);
4788 		wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
4789 				 interfaces->ctrl_iface_cookie,
4790 				 CTRL_IFACE_COOKIE_LEN);
4791 		reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
4792 		goto send_reply;
4793 	}
4794 
4795 	if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
4796 	    hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
4797 		wpa_printf(MSG_DEBUG,
4798 			   "CTRL: No cookie in the request - drop request");
4799 		os_free(reply);
4800 		return;
4801 	}
4802 
4803 	if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie,
4804 		      CTRL_IFACE_COOKIE_LEN) != 0) {
4805 		wpa_printf(MSG_DEBUG,
4806 			   "CTRL: Invalid cookie in the request - drop request");
4807 		os_free(reply);
4808 		return;
4809 	}
4810 
4811 	buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN;
4812 	while (*buf == ' ')
4813 		buf++;
4814 #endif /* CONFIG_CTRL_IFACE_UDP */
4815 
4816 	if (os_strncmp(buf, "IFNAME=", 7) == 0) {
4817 		char *pos = os_strchr(buf + 7, ' ');
4818 
4819 		if (pos) {
4820 			*pos++ = '\0';
4821 			reply_len = hostapd_global_ctrl_iface_ifname(
4822 				interfaces, buf + 7, pos, reply, reply_size,
4823 				&from, fromlen);
4824 			goto send_reply;
4825 		}
4826 	}
4827 
4828 	if (os_strcmp(buf, "PING") == 0) {
4829 		os_memcpy(reply, "PONG\n", 5);
4830 		reply_len = 5;
4831 	} else if (os_strncmp(buf, "RELOG", 5) == 0) {
4832 		if (wpa_debug_reopen_file() < 0)
4833 			reply_len = -1;
4834 	} else if (os_strcmp(buf, "FLUSH") == 0) {
4835 		hostapd_ctrl_iface_flush(interfaces);
4836 	} else if (os_strncmp(buf, "ADD ", 4) == 0) {
4837 		if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
4838 			reply_len = -1;
4839 	} else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
4840 		if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
4841 			reply_len = -1;
4842 	} else if (os_strcmp(buf, "ATTACH") == 0) {
4843 		if (hostapd_global_ctrl_iface_attach(interfaces, &from,
4844 						     fromlen, NULL))
4845 			reply_len = -1;
4846 	} else if (os_strncmp(buf, "ATTACH ", 7) == 0) {
4847 		if (hostapd_global_ctrl_iface_attach(interfaces, &from,
4848 						     fromlen, buf + 7))
4849 			reply_len = -1;
4850 	} else if (os_strcmp(buf, "DETACH") == 0) {
4851 		if (hostapd_global_ctrl_iface_detach(interfaces, &from,
4852 			fromlen))
4853 			reply_len = -1;
4854 #ifdef CONFIG_MODULE_TESTS
4855 	} else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
4856 		if (hapd_module_tests() < 0)
4857 			reply_len = -1;
4858 #endif /* CONFIG_MODULE_TESTS */
4859 #ifdef CONFIG_FST
4860 	} else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
4861 		if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
4862 			reply_len = os_snprintf(reply, reply_size, "OK\n");
4863 		else
4864 			reply_len = -1;
4865 	} else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
4866 		if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
4867 			reply_len = os_snprintf(reply, reply_size, "OK\n");
4868 		else
4869 			reply_len = -1;
4870 	} else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
4871 		reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
4872 #endif /* CONFIG_FST */
4873 	} else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
4874 		if (!hostapd_global_ctrl_iface_dup_network(interfaces,
4875 							   buf + 12))
4876 			reply_len = os_snprintf(reply, reply_size, "OK\n");
4877 		else
4878 			reply_len = -1;
4879 	} else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
4880 		reply_len = hostapd_global_ctrl_iface_interfaces(
4881 			interfaces, buf + 10, reply, sizeof(buffer));
4882 	} else if (os_strcmp(buf, "TERMINATE") == 0) {
4883 		eloop_terminate();
4884 	} else {
4885 		wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
4886 			   "ignored");
4887 		reply_len = -1;
4888 	}
4889 
4890 send_reply:
4891 	if (reply_len < 0) {
4892 		os_memcpy(reply, "FAIL\n", 5);
4893 		reply_len = 5;
4894 	}
4895 
4896 	if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
4897 		   fromlen) < 0) {
4898 		wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
4899 			   strerror(errno));
4900 	}
4901 	os_free(reply);
4902 }
4903 
4904 
4905 #ifndef CONFIG_CTRL_IFACE_UDP
4906 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
4907 {
4908 	char *buf;
4909 	size_t len;
4910 
4911 	if (interface->global_iface_path == NULL)
4912 		return NULL;
4913 
4914 	len = os_strlen(interface->global_iface_path) +
4915 		os_strlen(interface->global_iface_name) + 2;
4916 	buf = os_malloc(len);
4917 	if (buf == NULL)
4918 		return NULL;
4919 
4920 	os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
4921 		    interface->global_iface_name);
4922 	buf[len - 1] = '\0';
4923 	return buf;
4924 }
4925 #endif /* CONFIG_CTRL_IFACE_UDP */
4926 
4927 
4928 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
4929 {
4930 #ifdef CONFIG_CTRL_IFACE_UDP
4931 	int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
4932 	char p[32] = { 0 };
4933 	char *pos;
4934 	struct addrinfo hints = { 0 }, *res, *saveres;
4935 	int n;
4936 
4937 	if (interface->global_ctrl_sock > -1) {
4938 		wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
4939 		return 0;
4940 	}
4941 
4942 	if (interface->global_iface_path == NULL)
4943 		return 0;
4944 
4945 	pos = os_strstr(interface->global_iface_path, "udp:");
4946 	if (pos) {
4947 		pos += 4;
4948 		port = atoi(pos);
4949 		if (port <= 0) {
4950 			wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
4951 			goto fail;
4952 		}
4953 	}
4954 
4955 	os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
4956 
4957 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4958 	hints.ai_flags = AI_PASSIVE;
4959 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4960 
4961 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4962 	hints.ai_family = AF_INET6;
4963 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4964 	hints.ai_family = AF_INET;
4965 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4966 	hints.ai_socktype = SOCK_DGRAM;
4967 
4968 try_again:
4969 	os_snprintf(p, sizeof(p), "%d", port);
4970 	n = getaddrinfo(NULL, p, &hints, &res);
4971 	if (n) {
4972 		wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
4973 		goto fail;
4974 	}
4975 
4976 	saveres = res;
4977 	interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
4978 					     res->ai_protocol);
4979 	if (interface->global_ctrl_sock < 0) {
4980 		wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
4981 		goto fail;
4982 	}
4983 
4984 	if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
4985 	    0) {
4986 		port++;
4987 		if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
4988 		    HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
4989 			goto try_again;
4990 		wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
4991 		goto fail;
4992 	}
4993 
4994 	freeaddrinfo(saveres);
4995 
4996 	wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
4997 
4998 	if (eloop_register_read_sock(interface->global_ctrl_sock,
4999 				     hostapd_global_ctrl_iface_receive,
5000 				     interface, NULL) < 0) {
5001 		hostapd_global_ctrl_iface_deinit(interface);
5002 		return -1;
5003 	}
5004 
5005 	wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
5006 
5007 	return 0;
5008 
5009 fail:
5010 	if (interface->global_ctrl_sock >= 0)
5011 		close(interface->global_ctrl_sock);
5012 	return -1;
5013 #else /* CONFIG_CTRL_IFACE_UDP */
5014 	struct sockaddr_un addr;
5015 	int s = -1;
5016 	char *fname = NULL;
5017 
5018 	if (interface->global_iface_path == NULL) {
5019 		wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
5020 		return 0;
5021 	}
5022 
5023 	if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
5024 		if (errno == EEXIST) {
5025 			wpa_printf(MSG_DEBUG, "Using existing control "
5026 				   "interface directory.");
5027 		} else {
5028 			wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
5029 				   strerror(errno));
5030 			goto fail;
5031 		}
5032 	} else if (interface->ctrl_iface_group &&
5033 		   lchown(interface->global_iface_path, -1,
5034 			  interface->ctrl_iface_group) < 0) {
5035 		wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
5036 			   strerror(errno));
5037 		goto fail;
5038 	}
5039 
5040 	if (os_strlen(interface->global_iface_path) + 1 +
5041 	    os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
5042 		goto fail;
5043 
5044 	s = socket(PF_UNIX, SOCK_DGRAM, 0);
5045 	if (s < 0) {
5046 		wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
5047 		goto fail;
5048 	}
5049 
5050 	os_memset(&addr, 0, sizeof(addr));
5051 #ifdef __FreeBSD__
5052 	addr.sun_len = sizeof(addr);
5053 #endif /* __FreeBSD__ */
5054 	addr.sun_family = AF_UNIX;
5055 	fname = hostapd_global_ctrl_iface_path(interface);
5056 	if (fname == NULL)
5057 		goto fail;
5058 	os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
5059 	if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5060 		wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
5061 			   strerror(errno));
5062 		if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
5063 			wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
5064 				   " allow connections - assuming it was left"
5065 				   "over from forced program termination");
5066 			if (unlink(fname) < 0) {
5067 				wpa_printf(MSG_ERROR,
5068 					   "Could not unlink existing ctrl_iface socket '%s': %s",
5069 					   fname, strerror(errno));
5070 				goto fail;
5071 			}
5072 			if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
5073 			    0) {
5074 				wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
5075 					   strerror(errno));
5076 				goto fail;
5077 			}
5078 			wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
5079 				   "ctrl_iface socket '%s'", fname);
5080 		} else {
5081 			wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
5082 				   "be in use - cannot override it");
5083 			wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
5084 				   "not used anymore", fname);
5085 			os_free(fname);
5086 			fname = NULL;
5087 			goto fail;
5088 		}
5089 	}
5090 
5091 	if (interface->ctrl_iface_group &&
5092 	    lchown(fname, -1, interface->ctrl_iface_group) < 0) {
5093 		wpa_printf(MSG_ERROR, "lchown[ctrl_interface]: %s",
5094 			   strerror(errno));
5095 		goto fail;
5096 	}
5097 
5098 	if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
5099 		wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
5100 			   strerror(errno));
5101 		goto fail;
5102 	}
5103 	os_free(fname);
5104 
5105 	interface->global_ctrl_sock = s;
5106 	eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
5107 				 interface, NULL);
5108 
5109 	wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
5110 
5111 	return 0;
5112 
5113 fail:
5114 	if (s >= 0)
5115 		close(s);
5116 	if (fname) {
5117 		unlink(fname);
5118 		os_free(fname);
5119 	}
5120 	return -1;
5121 #endif /* CONFIG_CTRL_IFACE_UDP */
5122 }
5123 
5124 
5125 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
5126 {
5127 #ifndef CONFIG_CTRL_IFACE_UDP
5128 	char *fname = NULL;
5129 #endif /* CONFIG_CTRL_IFACE_UDP */
5130 	struct wpa_ctrl_dst *dst, *prev;
5131 
5132 	if (interfaces->global_ctrl_sock > -1) {
5133 		eloop_unregister_read_sock(interfaces->global_ctrl_sock);
5134 		close(interfaces->global_ctrl_sock);
5135 		interfaces->global_ctrl_sock = -1;
5136 #ifndef CONFIG_CTRL_IFACE_UDP
5137 		fname = hostapd_global_ctrl_iface_path(interfaces);
5138 		if (fname) {
5139 			unlink(fname);
5140 			os_free(fname);
5141 		}
5142 
5143 		if (interfaces->global_iface_path &&
5144 		    rmdir(interfaces->global_iface_path) < 0) {
5145 			if (errno == ENOTEMPTY) {
5146 				wpa_printf(MSG_DEBUG, "Control interface "
5147 					   "directory not empty - leaving it "
5148 					   "behind");
5149 			} else {
5150 				wpa_printf(MSG_ERROR,
5151 					   "rmdir[ctrl_interface=%s]: %s",
5152 					   interfaces->global_iface_path,
5153 					   strerror(errno));
5154 			}
5155 		}
5156 #endif /* CONFIG_CTRL_IFACE_UDP */
5157 	}
5158 
5159 	os_free(interfaces->global_iface_path);
5160 	interfaces->global_iface_path = NULL;
5161 
5162 	dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
5163 			      struct wpa_ctrl_dst, list)
5164 		os_free(dst);
5165 }
5166 
5167 
5168 static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst *dst,
5169 					    const char *buf)
5170 {
5171 	/* Enable Probe Request events based on explicit request.
5172 	 * Other events are enabled by default.
5173 	 */
5174 	if (str_starts(buf, RX_PROBE_REQUEST))
5175 		return !!(dst->events & WPA_EVENT_RX_PROBE_REQUEST);
5176 	return 1;
5177 }
5178 
5179 
5180 static void hostapd_ctrl_iface_send_internal(int sock, struct dl_list *ctrl_dst,
5181 					     const char *ifname, int level,
5182 					     const char *buf, size_t len)
5183 {
5184 	struct wpa_ctrl_dst *dst, *next;
5185 	struct msghdr msg;
5186 	int idx, res;
5187 	struct iovec io[5];
5188 	char levelstr[10];
5189 
5190 	if (sock < 0 || dl_list_empty(ctrl_dst))
5191 		return;
5192 
5193 	res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
5194 	if (os_snprintf_error(sizeof(levelstr), res))
5195 		return;
5196 	idx = 0;
5197 	if (ifname) {
5198 		io[idx].iov_base = "IFNAME=";
5199 		io[idx].iov_len = 7;
5200 		idx++;
5201 		io[idx].iov_base = (char *) ifname;
5202 		io[idx].iov_len = os_strlen(ifname);
5203 		idx++;
5204 		io[idx].iov_base = " ";
5205 		io[idx].iov_len = 1;
5206 		idx++;
5207 	}
5208 	io[idx].iov_base = levelstr;
5209 	io[idx].iov_len = os_strlen(levelstr);
5210 	idx++;
5211 	io[idx].iov_base = (char *) buf;
5212 	io[idx].iov_len = len;
5213 	idx++;
5214 	os_memset(&msg, 0, sizeof(msg));
5215 	msg.msg_iov = io;
5216 	msg.msg_iovlen = idx;
5217 
5218 	idx = 0;
5219 	dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
5220 		if ((level >= dst->debug_level) &&
5221 		     hostapd_ctrl_check_event_enabled(dst, buf)) {
5222 			sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
5223 				       &dst->addr, dst->addrlen);
5224 			msg.msg_name = &dst->addr;
5225 			msg.msg_namelen = dst->addrlen;
5226 			if (sendmsg(sock, &msg, 0) < 0) {
5227 				int _errno = errno;
5228 				wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
5229 					   "%d - %s",
5230 					   idx, errno, strerror(errno));
5231 				dst->errors++;
5232 				if (dst->errors > 10 || _errno == ENOENT) {
5233 					ctrl_iface_detach(ctrl_dst,
5234 							  &dst->addr,
5235 							  dst->addrlen);
5236 				}
5237 			} else
5238 				dst->errors = 0;
5239 		}
5240 		idx++;
5241 	}
5242 }
5243 
5244 
5245 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
5246 				    enum wpa_msg_type type,
5247 				    const char *buf, size_t len)
5248 {
5249 	if (type != WPA_MSG_NO_GLOBAL) {
5250 		hostapd_ctrl_iface_send_internal(
5251 			hapd->iface->interfaces->global_ctrl_sock,
5252 			&hapd->iface->interfaces->global_ctrl_dst,
5253 			type != WPA_MSG_PER_INTERFACE ?
5254 			NULL : hapd->conf->iface,
5255 			level, buf, len);
5256 	}
5257 
5258 	if (type != WPA_MSG_ONLY_GLOBAL) {
5259 		hostapd_ctrl_iface_send_internal(
5260 			hapd->ctrl_sock, &hapd->ctrl_dst,
5261 			NULL, level, buf, len);
5262 	}
5263 }
5264 
5265 #endif /* CONFIG_NATIVE_WINDOWS */
5266