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