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