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