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