2 * hostapd / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #ifndef CONFIG_NATIVE_WINDOWS
13 #ifdef CONFIG_TESTING_OPTIONS
14 #include <net/ethernet.h>
15 #include <netinet/ip.h>
16 #endif /* CONFIG_TESTING_OPTIONS */
22 #ifdef CONFIG_CTRL_IFACE_UDP
24 #endif /* CONFIG_CTRL_IFACE_UDP */
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 #include "common/dpp.h"
33 #include "common/wpa_ctrl.h"
34 #include "crypto/tls.h"
35 #include "drivers/driver.h"
36 #include "eapol_auth/eapol_auth_sm.h"
37 #include "radius/radius_client.h"
38 #include "radius/radius_server.h"
39 #include "l2_packet/l2_packet.h"
40 #include "ap/hostapd.h"
41 #include "ap/ap_config.h"
42 #include "ap/ieee802_1x.h"
43 #include "ap/wpa_auth.h"
44 #include "ap/ieee802_11.h"
45 #include "ap/sta_info.h"
46 #include "ap/wps_hostapd.h"
47 #include "ap/ctrl_iface_ap.h"
48 #include "ap/ap_drv_ops.h"
50 #include "ap/wnm_ap.h"
51 #include "ap/wpa_auth.h"
52 #include "ap/beacon.h"
53 #include "ap/neighbor_db.h"
55 #include "ap/dpp_hostapd.h"
56 #include "wps/wps_defs.h"
58 #include "fst/fst_ctrl_iface.h"
59 #include "config_file.h"
60 #include "ctrl_iface.h"
63 #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
65 #ifdef CONFIG_CTRL_IFACE_UDP
67 static unsigned char cookie
[COOKIE_LEN
];
68 static unsigned char gcookie
[COOKIE_LEN
];
69 #define HOSTAPD_CTRL_IFACE_PORT 8877
70 #define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
71 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
72 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
73 #endif /* CONFIG_CTRL_IFACE_UDP */
75 static void hostapd_ctrl_iface_send(struct hostapd_data
*hapd
, int level
,
76 enum wpa_msg_type type
,
77 const char *buf
, size_t len
);
80 static int hostapd_ctrl_iface_attach(struct hostapd_data
*hapd
,
81 struct sockaddr_storage
*from
,
82 socklen_t fromlen
, const char *input
)
84 return ctrl_iface_attach(&hapd
->ctrl_dst
, from
, fromlen
, input
);
88 static int hostapd_ctrl_iface_detach(struct hostapd_data
*hapd
,
89 struct sockaddr_storage
*from
,
92 return ctrl_iface_detach(&hapd
->ctrl_dst
, from
, fromlen
);
96 static int hostapd_ctrl_iface_level(struct hostapd_data
*hapd
,
97 struct sockaddr_storage
*from
,
101 return ctrl_iface_level(&hapd
->ctrl_dst
, from
, fromlen
, level
);
105 static int hostapd_ctrl_iface_new_sta(struct hostapd_data
*hapd
,
109 struct sta_info
*sta
;
111 wpa_printf(MSG_DEBUG
, "CTRL_IFACE NEW_STA %s", txtaddr
);
113 if (hwaddr_aton(txtaddr
, addr
))
116 sta
= ap_get_sta(hapd
, addr
);
120 wpa_printf(MSG_DEBUG
, "Add new STA " MACSTR
" based on ctrl_iface "
121 "notification", MAC2STR(addr
));
122 sta
= ap_sta_add(hapd
, addr
);
126 hostapd_new_assoc_sta(hapd
, sta
, 0);
131 #ifdef CONFIG_IEEE80211W
133 static int hostapd_ctrl_iface_sa_query(struct hostapd_data
*hapd
,
137 u8 trans_id
[WLAN_SA_QUERY_TR_ID_LEN
];
139 wpa_printf(MSG_DEBUG
, "CTRL_IFACE SA_QUERY %s", txtaddr
);
141 if (hwaddr_aton(txtaddr
, addr
) ||
142 os_get_random(trans_id
, WLAN_SA_QUERY_TR_ID_LEN
) < 0)
145 ieee802_11_send_sa_query_req(hapd
, addr
, trans_id
);
149 #endif /* NEED_AP_MLME */
150 #endif /* CONFIG_IEEE80211W */
154 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data
*hapd
, char *txt
)
156 char *pin
= os_strchr(txt
, ' ');
159 u8 addr_buf
[ETH_ALEN
], *addr
= NULL
;
166 timeout_txt
= os_strchr(pin
, ' ');
168 *timeout_txt
++ = '\0';
169 timeout
= atoi(timeout_txt
);
170 pos
= os_strchr(timeout_txt
, ' ');
173 if (hwaddr_aton(pos
, addr_buf
) == 0)
179 return hostapd_wps_add_pin(hapd
, addr
, txt
, pin
, timeout
);
183 static int hostapd_ctrl_iface_wps_check_pin(
184 struct hostapd_data
*hapd
, char *cmd
, char *buf
, size_t buflen
)
191 wpa_hexdump_ascii_key(MSG_DEBUG
, "WPS_CHECK_PIN",
192 (u8
*) cmd
, os_strlen(cmd
));
193 for (pos
= cmd
, len
= 0; *pos
!= '\0'; pos
++) {
194 if (*pos
< '0' || *pos
> '9')
198 wpa_printf(MSG_DEBUG
, "WPS: Too long PIN");
202 if (len
!= 4 && len
!= 8) {
203 wpa_printf(MSG_DEBUG
, "WPS: Invalid PIN length %d", (int) len
);
209 unsigned int pin_val
;
211 if (!wps_pin_valid(pin_val
)) {
212 wpa_printf(MSG_DEBUG
, "WPS: Invalid checksum digit");
213 ret
= os_snprintf(buf
, buflen
, "FAIL-CHECKSUM\n");
214 if (os_snprintf_error(buflen
, ret
))
220 ret
= os_snprintf(buf
, buflen
, "%s", pin
);
221 if (os_snprintf_error(buflen
, ret
))
228 #ifdef CONFIG_WPS_NFC
229 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data
*hapd
,
236 len
= os_strlen(pos
);
241 buf
= wpabuf_alloc(len
);
244 if (hexstr2bin(pos
, wpabuf_put(buf
, len
), len
) < 0) {
249 ret
= hostapd_wps_nfc_tag_read(hapd
, buf
);
256 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data
*hapd
,
257 char *cmd
, char *reply
,
264 if (os_strcmp(cmd
, "WPS") == 0)
266 else if (os_strcmp(cmd
, "NDEF") == 0)
271 buf
= hostapd_wps_nfc_config_token(hapd
, ndef
);
275 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
286 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data
*hapd
,
287 char *reply
, size_t max_len
,
293 buf
= hostapd_wps_nfc_token_gen(hapd
, ndef
);
297 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
308 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data
*hapd
,
309 char *cmd
, char *reply
,
312 if (os_strcmp(cmd
, "WPS") == 0)
313 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd
, reply
,
316 if (os_strcmp(cmd
, "NDEF") == 0)
317 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd
, reply
,
320 if (os_strcmp(cmd
, "enable") == 0)
321 return hostapd_wps_nfc_token_enable(hapd
);
323 if (os_strcmp(cmd
, "disable") == 0) {
324 hostapd_wps_nfc_token_disable(hapd
);
332 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data
*hapd
,
333 char *cmd
, char *reply
,
341 pos
= os_strchr(cmd
, ' ');
346 if (os_strcmp(cmd
, "WPS") == 0)
348 else if (os_strcmp(cmd
, "NDEF") == 0)
353 if (os_strcmp(pos
, "WPS-CR") == 0)
354 buf
= hostapd_wps_nfc_hs_cr(hapd
, ndef
);
360 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
371 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data
*hapd
,
375 struct wpabuf
*req
, *sel
;
377 char *pos
, *role
, *type
, *pos2
;
380 pos
= os_strchr(role
, ' ');
386 pos
= os_strchr(type
, ' ');
391 pos2
= os_strchr(pos
, ' ');
396 len
= os_strlen(pos
);
401 req
= wpabuf_alloc(len
);
404 if (hexstr2bin(pos
, wpabuf_put(req
, len
), len
) < 0) {
409 len
= os_strlen(pos2
);
416 sel
= wpabuf_alloc(len
);
421 if (hexstr2bin(pos2
, wpabuf_put(sel
, len
), len
) < 0) {
427 if (os_strcmp(role
, "RESP") == 0 && os_strcmp(type
, "WPS") == 0) {
428 ret
= hostapd_wps_nfc_report_handover(hapd
, req
, sel
);
430 wpa_printf(MSG_DEBUG
, "NFC: Unsupported connection handover "
431 "reported: role=%s type=%s", role
, type
);
440 #endif /* CONFIG_WPS_NFC */
443 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data
*hapd
, char *txt
,
444 char *buf
, size_t buflen
)
450 pos
= os_strchr(txt
, ' ');
454 if (os_strcmp(txt
, "disable") == 0) {
455 hostapd_wps_ap_pin_disable(hapd
);
456 return os_snprintf(buf
, buflen
, "OK\n");
459 if (os_strcmp(txt
, "random") == 0) {
462 pin_txt
= hostapd_wps_ap_pin_random(hapd
, timeout
);
465 return os_snprintf(buf
, buflen
, "%s", pin_txt
);
468 if (os_strcmp(txt
, "get") == 0) {
469 pin_txt
= hostapd_wps_ap_pin_get(hapd
);
472 return os_snprintf(buf
, buflen
, "%s", pin_txt
);
475 if (os_strcmp(txt
, "set") == 0) {
480 pos
= os_strchr(pos
, ' ');
485 if (os_strlen(pin
) > buflen
)
487 if (hostapd_wps_ap_pin_set(hapd
, pin
, timeout
) < 0)
489 return os_snprintf(buf
, buflen
, "%s", pin
);
496 static int hostapd_ctrl_iface_wps_config(struct hostapd_data
*hapd
, char *txt
)
499 char *ssid
, *auth
, *encr
= NULL
, *key
= NULL
;
502 pos
= os_strchr(txt
, ' ');
508 pos
= os_strchr(pos
, ' ');
512 pos
= os_strchr(pos
, ' ');
519 return hostapd_wps_config_ap(hapd
, ssid
, auth
, encr
, key
);
523 static const char * pbc_status_str(enum pbc_status status
)
526 case WPS_PBC_STATUS_DISABLE
:
528 case WPS_PBC_STATUS_ACTIVE
:
530 case WPS_PBC_STATUS_TIMEOUT
:
532 case WPS_PBC_STATUS_OVERLAP
:
540 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data
*hapd
,
541 char *buf
, size_t buflen
)
549 ret
= os_snprintf(pos
, end
- pos
, "PBC Status: %s\n",
550 pbc_status_str(hapd
->wps_stats
.pbc_status
));
552 if (os_snprintf_error(end
- pos
, ret
))
556 ret
= os_snprintf(pos
, end
- pos
, "Last WPS result: %s\n",
557 (hapd
->wps_stats
.status
== WPS_STATUS_SUCCESS
?
559 (hapd
->wps_stats
.status
== WPS_STATUS_FAILURE
?
560 "Failed" : "None")));
562 if (os_snprintf_error(end
- pos
, ret
))
566 /* If status == Failure - Add possible Reasons */
567 if(hapd
->wps_stats
.status
== WPS_STATUS_FAILURE
&&
568 hapd
->wps_stats
.failure_reason
> 0) {
569 ret
= os_snprintf(pos
, end
- pos
,
570 "Failure Reason: %s\n",
571 wps_ei_str(hapd
->wps_stats
.failure_reason
));
573 if (os_snprintf_error(end
- pos
, ret
))
578 if (hapd
->wps_stats
.status
) {
579 ret
= os_snprintf(pos
, end
- pos
, "Peer Address: " MACSTR
"\n",
580 MAC2STR(hapd
->wps_stats
.peer_addr
));
582 if (os_snprintf_error(end
- pos
, ret
))
590 #endif /* CONFIG_WPS */
594 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data
*hapd
,
600 if (hwaddr_aton(cmd
, addr
))
613 return hs20_send_wnm_notification(hapd
, addr
, 1, url
);
617 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data
*hapd
,
621 int code
, reauth_delay
, ret
;
626 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
627 if (hwaddr_aton(cmd
, addr
))
630 pos
= os_strchr(cmd
, ' ');
636 pos
= os_strchr(pos
, ' ');
640 reauth_delay
= atoi(pos
);
643 pos
= os_strchr(pos
, ' ');
646 url_len
= os_strlen(pos
);
649 req
= wpabuf_alloc(4 + url_len
);
652 wpabuf_put_u8(req
, code
);
653 wpabuf_put_le16(req
, reauth_delay
);
654 wpabuf_put_u8(req
, url_len
);
656 wpabuf_put_data(req
, pos
, url_len
);
658 wpa_printf(MSG_DEBUG
, "HS 2.0: Send WNM-Notification to " MACSTR
659 " to indicate imminent deauthentication (code=%d "
660 "reauth_delay=%d)", MAC2STR(addr
), code
, reauth_delay
);
661 ret
= hs20_send_wnm_notification_deauth_req(hapd
, addr
, req
);
666 #endif /* CONFIG_HS20 */
669 #ifdef CONFIG_INTERWORKING
671 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data
*hapd
,
674 u8 qos_map_set
[16 + 2 * 21], count
= 0;
675 const char *pos
= cmd
;
679 if (count
== sizeof(qos_map_set
)) {
680 wpa_printf(MSG_ERROR
, "Too many qos_map_set parameters");
685 if (val
< 0 || val
> 255) {
686 wpa_printf(MSG_INFO
, "Invalid QoS Map Set");
690 qos_map_set
[count
++] = val
;
691 pos
= os_strchr(pos
, ',');
697 if (count
< 16 || count
& 1) {
698 wpa_printf(MSG_INFO
, "Invalid QoS Map Set");
702 ret
= hostapd_drv_set_qos_map(hapd
, qos_map_set
, count
);
704 wpa_printf(MSG_INFO
, "Failed to set QoS Map Set");
708 os_memcpy(hapd
->conf
->qos_map_set
, qos_map_set
, count
);
709 hapd
->conf
->qos_map_set_len
= count
;
715 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data
*hapd
,
719 struct sta_info
*sta
;
721 u8
*qos_map_set
= hapd
->conf
->qos_map_set
;
722 u8 qos_map_set_len
= hapd
->conf
->qos_map_set_len
;
725 if (!qos_map_set_len
) {
726 wpa_printf(MSG_INFO
, "QoS Map Set is not set");
730 if (hwaddr_aton(cmd
, addr
))
733 sta
= ap_get_sta(hapd
, addr
);
735 wpa_printf(MSG_DEBUG
, "Station " MACSTR
" not found "
736 "for QoS Map Configuration message",
741 if (!sta
->qos_map_enabled
) {
742 wpa_printf(MSG_DEBUG
, "Station " MACSTR
" did not indicate "
743 "support for QoS Map", MAC2STR(addr
));
747 buf
= wpabuf_alloc(2 + 2 + qos_map_set_len
);
751 wpabuf_put_u8(buf
, WLAN_ACTION_QOS
);
752 wpabuf_put_u8(buf
, QOS_QOS_MAP_CONFIG
);
754 /* QoS Map Set Element */
755 wpabuf_put_u8(buf
, WLAN_EID_QOS_MAP_SET
);
756 wpabuf_put_u8(buf
, qos_map_set_len
);
757 wpabuf_put_data(buf
, qos_map_set
, qos_map_set_len
);
759 ret
= hostapd_drv_send_action(hapd
, hapd
->iface
->freq
, 0, addr
,
760 wpabuf_head(buf
), wpabuf_len(buf
));
766 #endif /* CONFIG_INTERWORKING */
771 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data
*hapd
,
776 struct sta_info
*sta
;
778 if (hwaddr_aton(cmd
, addr
))
782 disassoc_timer
= atoi(cmd
+ 17);
784 sta
= ap_get_sta(hapd
, addr
);
786 wpa_printf(MSG_DEBUG
, "Station " MACSTR
787 " not found for disassociation imminent message",
792 return wnm_send_disassoc_imminent(hapd
, sta
, disassoc_timer
);
796 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data
*hapd
,
800 const char *url
, *timerstr
;
802 struct sta_info
*sta
;
804 if (hwaddr_aton(cmd
, addr
))
807 sta
= ap_get_sta(hapd
, addr
);
809 wpa_printf(MSG_DEBUG
, "Station " MACSTR
810 " not found for ESS disassociation imminent message",
816 if (*timerstr
!= ' ')
819 disassoc_timer
= atoi(timerstr
);
820 if (disassoc_timer
< 0 || disassoc_timer
> 65535)
823 url
= os_strchr(timerstr
, ' ');
828 return wnm_send_ess_disassoc_imminent(hapd
, sta
, url
, disassoc_timer
);
832 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data
*hapd
,
836 const char *pos
, *end
;
837 int disassoc_timer
= 0;
838 struct sta_info
*sta
;
839 u8 req_mode
= 0, valid_int
= 0x01;
848 if (hwaddr_aton(cmd
, addr
)) {
849 wpa_printf(MSG_DEBUG
, "Invalid STA MAC address");
853 sta
= ap_get_sta(hapd
, addr
);
855 wpa_printf(MSG_DEBUG
, "Station " MACSTR
856 " not found for BSS TM Request message",
861 pos
= os_strstr(cmd
, " disassoc_timer=");
864 disassoc_timer
= atoi(pos
);
865 if (disassoc_timer
< 0 || disassoc_timer
> 65535) {
866 wpa_printf(MSG_DEBUG
, "Invalid disassoc_timer");
871 pos
= os_strstr(cmd
, " valid_int=");
874 valid_int
= atoi(pos
);
877 pos
= os_strstr(cmd
, " bss_term=");
880 req_mode
|= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED
;
881 /* TODO: TSF configurable/learnable */
882 bss_term_dur
[0] = 4; /* Subelement ID */
883 bss_term_dur
[1] = 10; /* Length */
884 os_memset(bss_term_dur
, 2, 8);
885 end
= os_strchr(pos
, ',');
887 wpa_printf(MSG_DEBUG
, "Invalid bss_term data");
891 WPA_PUT_LE16(&bss_term_dur
[10], atoi(end
));
894 nei_len
= ieee802_11_parse_candidate_list(cmd
, nei_rep
,
899 pos
= os_strstr(cmd
, " url=");
903 end
= os_strchr(pos
, ' ');
907 len
= os_strlen(pos
);
908 url
= os_malloc(len
+ 1);
911 os_memcpy(url
, pos
, len
);
913 req_mode
|= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT
;
916 if (os_strstr(cmd
, " pref=1"))
917 req_mode
|= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED
;
918 if (os_strstr(cmd
, " abridged=1"))
919 req_mode
|= WNM_BSS_TM_REQ_ABRIDGED
;
920 if (os_strstr(cmd
, " disassoc_imminent=1"))
921 req_mode
|= WNM_BSS_TM_REQ_DISASSOC_IMMINENT
;
924 pos
= os_strstr(cmd
, "mbo=");
926 unsigned int mbo_reason
, cell_pref
, reassoc_delay
;
929 ret
= sscanf(pos
, "mbo=%u:%u:%u", &mbo_reason
,
930 &reassoc_delay
, &cell_pref
);
932 wpa_printf(MSG_DEBUG
,
933 "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
938 if (mbo_reason
> MBO_TRANSITION_REASON_PREMIUM_AP
) {
939 wpa_printf(MSG_DEBUG
,
940 "Invalid MBO transition reason code %u",
946 /* Valid values for Cellular preference are: 0, 1, 255 */
947 if (cell_pref
!= 0 && cell_pref
!= 1 && cell_pref
!= 255) {
948 wpa_printf(MSG_DEBUG
,
949 "Invalid MBO cellular capability %u",
955 if (reassoc_delay
> 65535 ||
957 !(req_mode
& WNM_BSS_TM_REQ_DISASSOC_IMMINENT
))) {
958 wpa_printf(MSG_DEBUG
,
959 "MBO: Assoc retry delay is only valid in disassoc imminent mode");
964 *mbo_pos
++ = MBO_ATTR_ID_TRANSITION_REASON
;
966 *mbo_pos
++ = mbo_reason
;
967 *mbo_pos
++ = MBO_ATTR_ID_CELL_DATA_PREF
;
969 *mbo_pos
++ = cell_pref
;
972 *mbo_pos
++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY
;
974 WPA_PUT_LE16(mbo_pos
, reassoc_delay
);
978 mbo_len
= mbo_pos
- mbo
;
980 #endif /* CONFIG_MBO */
982 ret
= wnm_send_bss_tm_req(hapd
, sta
, req_mode
, disassoc_timer
,
983 valid_int
, bss_term_dur
, url
,
984 nei_len
? nei_rep
: NULL
, nei_len
,
985 mbo_len
? mbo
: NULL
, mbo_len
);
988 #endif /* CONFIG_MBO */
993 #endif /* CONFIG_WNM_AP */
996 static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data
*hapd
,
997 char *buf
, size_t buflen
)
1005 WPA_ASSERT(hapd
->conf
->wpa_key_mgmt
);
1007 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_PSK
) {
1008 ret
= os_snprintf(pos
, end
- pos
, "WPA-PSK ");
1009 if (os_snprintf_error(end
- pos
, ret
))
1013 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X
) {
1014 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP ");
1015 if (os_snprintf_error(end
- pos
, ret
))
1019 #ifdef CONFIG_IEEE80211R_AP
1020 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_PSK
) {
1021 ret
= os_snprintf(pos
, end
- pos
, "FT-PSK ");
1022 if (os_snprintf_error(end
- pos
, ret
))
1026 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_IEEE8021X
) {
1027 ret
= os_snprintf(pos
, end
- pos
, "FT-EAP ");
1028 if (os_snprintf_error(end
- pos
, ret
))
1033 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_SAE
) {
1034 ret
= os_snprintf(pos
, end
- pos
, "FT-SAE ");
1035 if (os_snprintf_error(end
- pos
, ret
))
1039 #endif /* CONFIG_SAE */
1041 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_FILS_SHA256
) {
1042 ret
= os_snprintf(pos
, end
- pos
, "FT-FILS-SHA256 ");
1043 if (os_snprintf_error(end
- pos
, ret
))
1047 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_FILS_SHA384
) {
1048 ret
= os_snprintf(pos
, end
- pos
, "FT-FILS-SHA384 ");
1049 if (os_snprintf_error(end
- pos
, ret
))
1053 #endif /* CONFIG_FILS */
1054 #endif /* CONFIG_IEEE80211R_AP */
1055 #ifdef CONFIG_IEEE80211W
1056 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_PSK_SHA256
) {
1057 ret
= os_snprintf(pos
, end
- pos
, "WPA-PSK-SHA256 ");
1058 if (os_snprintf_error(end
- pos
, ret
))
1062 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X_SHA256
) {
1063 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP-SHA256 ");
1064 if (os_snprintf_error(end
- pos
, ret
))
1068 #endif /* CONFIG_IEEE80211W */
1070 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_SAE
) {
1071 ret
= os_snprintf(pos
, end
- pos
, "SAE ");
1072 if (os_snprintf_error(end
- pos
, ret
))
1076 #endif /* CONFIG_SAE */
1077 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X_SUITE_B
) {
1078 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP-SUITE-B ");
1079 if (os_snprintf_error(end
- pos
, ret
))
1083 if (hapd
->conf
->wpa_key_mgmt
&
1084 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192
) {
1085 ret
= os_snprintf(pos
, end
- pos
,
1086 "WPA-EAP-SUITE-B-192 ");
1087 if (os_snprintf_error(end
- pos
, ret
))
1092 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FILS_SHA256
) {
1093 ret
= os_snprintf(pos
, end
- pos
, "FILS-SHA256 ");
1094 if (os_snprintf_error(end
- pos
, ret
))
1098 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FILS_SHA384
) {
1099 ret
= os_snprintf(pos
, end
- pos
, "FILS-SHA384 ");
1100 if (os_snprintf_error(end
- pos
, ret
))
1104 #endif /* CONFIG_FILS */
1107 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_OWE
) {
1108 ret
= os_snprintf(pos
, end
- pos
, "OWE ");
1109 if (os_snprintf_error(end
- pos
, ret
))
1113 #endif /* CONFIG_OWE */
1116 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_DPP
) {
1117 ret
= os_snprintf(pos
, end
- pos
, "DPP ");
1118 if (os_snprintf_error(end
- pos
, ret
))
1122 #endif /* CONFIG_DPP */
1124 if (pos
> buf
&& *(pos
- 1) == ' ') {
1133 static int hostapd_ctrl_iface_get_config(struct hostapd_data
*hapd
,
1134 char *buf
, size_t buflen
)
1142 ret
= os_snprintf(pos
, end
- pos
, "bssid=" MACSTR
"\n"
1144 MAC2STR(hapd
->own_addr
),
1145 wpa_ssid_txt(hapd
->conf
->ssid
.ssid
,
1146 hapd
->conf
->ssid
.ssid_len
));
1147 if (os_snprintf_error(end
- pos
, ret
))
1152 ret
= os_snprintf(pos
, end
- pos
, "wps_state=%s\n",
1153 hapd
->conf
->wps_state
== 0 ? "disabled" :
1154 (hapd
->conf
->wps_state
== 1 ? "not configured" :
1156 if (os_snprintf_error(end
- pos
, ret
))
1160 if (hapd
->conf
->wps_state
&& hapd
->conf
->wpa
&&
1161 hapd
->conf
->ssid
.wpa_passphrase
) {
1162 ret
= os_snprintf(pos
, end
- pos
, "passphrase=%s\n",
1163 hapd
->conf
->ssid
.wpa_passphrase
);
1164 if (os_snprintf_error(end
- pos
, ret
))
1169 if (hapd
->conf
->wps_state
&& hapd
->conf
->wpa
&&
1170 hapd
->conf
->ssid
.wpa_psk
&&
1171 hapd
->conf
->ssid
.wpa_psk
->group
) {
1172 char hex
[PMK_LEN
* 2 + 1];
1173 wpa_snprintf_hex(hex
, sizeof(hex
),
1174 hapd
->conf
->ssid
.wpa_psk
->psk
, PMK_LEN
);
1175 ret
= os_snprintf(pos
, end
- pos
, "psk=%s\n", hex
);
1176 if (os_snprintf_error(end
- pos
, ret
))
1180 #endif /* CONFIG_WPS */
1182 if (hapd
->conf
->wpa
) {
1183 ret
= os_snprintf(pos
, end
- pos
, "wpa=%d\n", hapd
->conf
->wpa
);
1184 if (os_snprintf_error(end
- pos
, ret
))
1189 if (hapd
->conf
->wpa
&& hapd
->conf
->wpa_key_mgmt
) {
1190 ret
= os_snprintf(pos
, end
- pos
, "key_mgmt=");
1191 if (os_snprintf_error(end
- pos
, ret
))
1195 pos
+= hostapd_ctrl_iface_get_key_mgmt(hapd
, pos
, end
- pos
);
1197 ret
= os_snprintf(pos
, end
- pos
, "\n");
1198 if (os_snprintf_error(end
- pos
, ret
))
1203 if (hapd
->conf
->wpa
) {
1204 ret
= os_snprintf(pos
, end
- pos
, "group_cipher=%s\n",
1205 wpa_cipher_txt(hapd
->conf
->wpa_group
));
1206 if (os_snprintf_error(end
- pos
, ret
))
1211 if ((hapd
->conf
->wpa
& WPA_PROTO_RSN
) && hapd
->conf
->rsn_pairwise
) {
1212 ret
= os_snprintf(pos
, end
- pos
, "rsn_pairwise_cipher=");
1213 if (os_snprintf_error(end
- pos
, ret
))
1217 ret
= wpa_write_ciphers(pos
, end
, hapd
->conf
->rsn_pairwise
,
1223 ret
= os_snprintf(pos
, end
- pos
, "\n");
1224 if (os_snprintf_error(end
- pos
, ret
))
1229 if ((hapd
->conf
->wpa
& WPA_PROTO_WPA
) && hapd
->conf
->wpa_pairwise
) {
1230 ret
= os_snprintf(pos
, end
- pos
, "wpa_pairwise_cipher=");
1231 if (os_snprintf_error(end
- pos
, ret
))
1235 ret
= wpa_write_ciphers(pos
, end
, hapd
->conf
->wpa_pairwise
,
1241 ret
= os_snprintf(pos
, end
- pos
, "\n");
1242 if (os_snprintf_error(end
- pos
, ret
))
1251 static int hostapd_ctrl_iface_set(struct hostapd_data
*hapd
, char *cmd
)
1256 value
= os_strchr(cmd
, ' ');
1261 wpa_printf(MSG_DEBUG
, "CTRL_IFACE SET '%s'='%s'", cmd
, value
);
1263 #ifdef CONFIG_WPS_TESTING
1264 } else if (os_strcasecmp(cmd
, "wps_version_number") == 0) {
1266 val
= strtol(value
, NULL
, 0);
1267 if (val
< 0 || val
> 0xff) {
1269 wpa_printf(MSG_DEBUG
, "WPS: Invalid "
1270 "wps_version_number %ld", val
);
1272 wps_version_number
= val
;
1273 wpa_printf(MSG_DEBUG
, "WPS: Testing - force WPS "
1275 (wps_version_number
& 0xf0) >> 4,
1276 wps_version_number
& 0x0f);
1277 hostapd_wps_update_ie(hapd
);
1279 } else if (os_strcasecmp(cmd
, "wps_testing_dummy_cred") == 0) {
1280 wps_testing_dummy_cred
= atoi(value
);
1281 wpa_printf(MSG_DEBUG
, "WPS: Testing - dummy_cred=%d",
1282 wps_testing_dummy_cred
);
1283 } else if (os_strcasecmp(cmd
, "wps_corrupt_pkhash") == 0) {
1284 wps_corrupt_pkhash
= atoi(value
);
1285 wpa_printf(MSG_DEBUG
, "WPS: Testing - wps_corrupt_pkhash=%d",
1286 wps_corrupt_pkhash
);
1287 #endif /* CONFIG_WPS_TESTING */
1288 #ifdef CONFIG_TESTING_OPTIONS
1289 } else if (os_strcasecmp(cmd
, "ext_mgmt_frame_handling") == 0) {
1290 hapd
->ext_mgmt_frame_handling
= atoi(value
);
1291 } else if (os_strcasecmp(cmd
, "ext_eapol_frame_io") == 0) {
1292 hapd
->ext_eapol_frame_io
= atoi(value
);
1294 } else if (os_strcasecmp(cmd
, "dpp_config_obj_override") == 0) {
1295 os_free(hapd
->dpp_config_obj_override
);
1296 hapd
->dpp_config_obj_override
= os_strdup(value
);
1297 } else if (os_strcasecmp(cmd
, "dpp_discovery_override") == 0) {
1298 os_free(hapd
->dpp_discovery_override
);
1299 hapd
->dpp_discovery_override
= os_strdup(value
);
1300 } else if (os_strcasecmp(cmd
, "dpp_groups_override") == 0) {
1301 os_free(hapd
->dpp_groups_override
);
1302 hapd
->dpp_groups_override
= os_strdup(value
);
1303 } else if (os_strcasecmp(cmd
,
1304 "dpp_ignore_netaccesskey_mismatch") == 0) {
1305 hapd
->dpp_ignore_netaccesskey_mismatch
= atoi(value
);
1306 } else if (os_strcasecmp(cmd
, "dpp_test") == 0) {
1307 dpp_test
= atoi(value
);
1308 #endif /* CONFIG_DPP */
1309 #endif /* CONFIG_TESTING_OPTIONS */
1311 } else if (os_strcasecmp(cmd
, "mbo_assoc_disallow") == 0) {
1314 if (!hapd
->conf
->mbo_enabled
)
1318 if (val
< 0 || val
> 1)
1321 hapd
->mbo_assoc_disallow
= val
;
1322 ieee802_11_update_beacons(hapd
->iface
);
1325 * TODO: Need to configure drivers that do AP MLME offload with
1326 * disallowing station logic.
1328 #endif /* CONFIG_MBO */
1330 } else if (os_strcasecmp(cmd
, "dpp_configurator_params") == 0) {
1331 os_free(hapd
->dpp_configurator_params
);
1332 hapd
->dpp_configurator_params
= os_strdup(value
);
1333 #endif /* CONFIG_DPP */
1335 struct sta_info
*sta
;
1336 struct vlan_description vlan_id
;
1338 ret
= hostapd_set_iface(hapd
->iconf
, hapd
->conf
, cmd
, value
);
1342 if (os_strcasecmp(cmd
, "deny_mac_file") == 0) {
1343 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
1344 if (hostapd_maclist_found(
1345 hapd
->conf
->deny_mac
,
1346 hapd
->conf
->num_deny_mac
, sta
->addr
,
1348 (!vlan_id
.notempty
||
1349 !vlan_compare(&vlan_id
, sta
->vlan_desc
)))
1351 hapd
, sta
, sta
->addr
,
1352 WLAN_REASON_UNSPECIFIED
);
1354 } else if (hapd
->conf
->macaddr_acl
== DENY_UNLESS_ACCEPTED
&&
1355 os_strcasecmp(cmd
, "accept_mac_file") == 0) {
1356 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
1357 if (!hostapd_maclist_found(
1358 hapd
->conf
->accept_mac
,
1359 hapd
->conf
->num_accept_mac
,
1360 sta
->addr
, &vlan_id
) ||
1361 (vlan_id
.notempty
&&
1362 vlan_compare(&vlan_id
, sta
->vlan_desc
)))
1364 hapd
, sta
, sta
->addr
,
1365 WLAN_REASON_UNSPECIFIED
);
1374 static int hostapd_ctrl_iface_get(struct hostapd_data
*hapd
, char *cmd
,
1375 char *buf
, size_t buflen
)
1379 wpa_printf(MSG_DEBUG
, "CTRL_IFACE GET '%s'", cmd
);
1381 if (os_strcmp(cmd
, "version") == 0) {
1382 res
= os_snprintf(buf
, buflen
, "%s", VERSION_STR
);
1383 if (os_snprintf_error(buflen
, res
))
1386 } else if (os_strcmp(cmd
, "tls_library") == 0) {
1387 res
= tls_get_library_version(buf
, buflen
);
1388 if (os_snprintf_error(buflen
, res
))
1397 static int hostapd_ctrl_iface_enable(struct hostapd_iface
*iface
)
1399 if (hostapd_enable_iface(iface
) < 0) {
1400 wpa_printf(MSG_ERROR
, "Enabling of interface failed");
1407 static int hostapd_ctrl_iface_reload(struct hostapd_iface
*iface
)
1409 if (hostapd_reload_iface(iface
) < 0) {
1410 wpa_printf(MSG_ERROR
, "Reloading of interface failed");
1417 static int hostapd_ctrl_iface_disable(struct hostapd_iface
*iface
)
1419 if (hostapd_disable_iface(iface
) < 0) {
1420 wpa_printf(MSG_ERROR
, "Disabling of interface failed");
1427 #ifdef CONFIG_TESTING_OPTIONS
1429 static int hostapd_ctrl_iface_radar(struct hostapd_data
*hapd
, char *cmd
)
1431 union wpa_event_data data
;
1433 enum wpa_event_type event
;
1435 wpa_printf(MSG_DEBUG
, "RADAR TEST: %s", cmd
);
1437 os_memset(&data
, 0, sizeof(data
));
1439 param
= os_strchr(cmd
, ' ');
1444 if (os_strcmp(cmd
, "DETECTED") == 0)
1445 event
= EVENT_DFS_RADAR_DETECTED
;
1446 else if (os_strcmp(cmd
, "CAC-FINISHED") == 0)
1447 event
= EVENT_DFS_CAC_FINISHED
;
1448 else if (os_strcmp(cmd
, "CAC-ABORTED") == 0)
1449 event
= EVENT_DFS_CAC_ABORTED
;
1450 else if (os_strcmp(cmd
, "NOP-FINISHED") == 0)
1451 event
= EVENT_DFS_NOP_FINISHED
;
1453 wpa_printf(MSG_DEBUG
, "Unsupported RADAR test command: %s",
1458 pos
= os_strstr(param
, "freq=");
1460 data
.dfs_event
.freq
= atoi(pos
+ 5);
1462 pos
= os_strstr(param
, "ht_enabled=1");
1464 data
.dfs_event
.ht_enabled
= 1;
1466 pos
= os_strstr(param
, "chan_offset=");
1468 data
.dfs_event
.chan_offset
= atoi(pos
+ 12);
1470 pos
= os_strstr(param
, "chan_width=");
1472 data
.dfs_event
.chan_width
= atoi(pos
+ 11);
1474 pos
= os_strstr(param
, "cf1=");
1476 data
.dfs_event
.cf1
= atoi(pos
+ 4);
1478 pos
= os_strstr(param
, "cf2=");
1480 data
.dfs_event
.cf2
= atoi(pos
+ 4);
1482 wpa_supplicant_event(hapd
, event
, &data
);
1488 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data
*hapd
, char *cmd
)
1494 wpa_printf(MSG_DEBUG
, "External MGMT TX: %s", cmd
);
1496 len
= os_strlen(cmd
);
1501 buf
= os_malloc(len
);
1505 if (hexstr2bin(cmd
, buf
, len
) < 0) {
1510 res
= hostapd_drv_send_mlme(hapd
, buf
, len
, 0);
1516 static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data
*hapd
,
1522 int stype
= 0, ok
= 0;
1523 union wpa_event_data event
;
1525 if (!hapd
->ext_mgmt_frame_handling
)
1528 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1530 wpa_printf(MSG_DEBUG
, "External MGMT TX status process: %s", cmd
);
1533 param
= os_strstr(pos
, "stype=");
1536 stype
= atoi(param
);
1539 param
= os_strstr(pos
, " ok=");
1545 param
= os_strstr(pos
, " buf=");
1550 len
= os_strlen(param
);
1555 buf
= os_malloc(len
);
1556 if (!buf
|| hexstr2bin(param
, buf
, len
) < 0) {
1561 os_memset(&event
, 0, sizeof(event
));
1562 event
.tx_status
.type
= WLAN_FC_TYPE_MGMT
;
1563 event
.tx_status
.data
= buf
;
1564 event
.tx_status
.data_len
= len
;
1565 event
.tx_status
.stype
= stype
;
1566 event
.tx_status
.ack
= ok
;
1567 hapd
->ext_mgmt_frame_handling
= 0;
1568 wpa_supplicant_event(hapd
, EVENT_TX_STATUS
, &event
);
1569 hapd
->ext_mgmt_frame_handling
= 1;
1577 static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data
*hapd
,
1583 int freq
= 0, datarate
= 0, ssi_signal
= 0;
1584 union wpa_event_data event
;
1586 if (!hapd
->ext_mgmt_frame_handling
)
1589 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1591 wpa_printf(MSG_DEBUG
, "External MGMT RX process: %s", cmd
);
1594 param
= os_strstr(pos
, "freq=");
1600 param
= os_strstr(pos
, " datarate=");
1603 datarate
= atoi(param
);
1606 param
= os_strstr(pos
, " ssi_signal=");
1609 ssi_signal
= atoi(param
);
1612 param
= os_strstr(pos
, " frame=");
1617 len
= os_strlen(param
);
1622 buf
= os_malloc(len
);
1626 if (hexstr2bin(param
, buf
, len
) < 0) {
1631 os_memset(&event
, 0, sizeof(event
));
1632 event
.rx_mgmt
.freq
= freq
;
1633 event
.rx_mgmt
.frame
= buf
;
1634 event
.rx_mgmt
.frame_len
= len
;
1635 event
.rx_mgmt
.ssi_signal
= ssi_signal
;
1636 event
.rx_mgmt
.datarate
= datarate
;
1637 hapd
->ext_mgmt_frame_handling
= 0;
1638 wpa_supplicant_event(hapd
, EVENT_RX_MGMT
, &event
);
1639 hapd
->ext_mgmt_frame_handling
= 1;
1647 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data
*hapd
, char *cmd
)
1650 u8 src
[ETH_ALEN
], *buf
;
1654 wpa_printf(MSG_DEBUG
, "External EAPOL RX: %s", cmd
);
1657 used
= hwaddr_aton2(pos
, src
);
1664 len
= os_strlen(pos
);
1669 buf
= os_malloc(len
);
1673 if (hexstr2bin(pos
, buf
, len
) < 0) {
1678 ieee802_1x_receive(hapd
, src
, buf
, len
);
1685 static u16
ipv4_hdr_checksum(const void *buf
, size_t len
)
1689 const u16
*pos
= buf
;
1691 for (i
= 0; i
< len
/ 2; i
++)
1695 sum
= (sum
& 0xffff) + (sum
>> 16);
1697 return sum
^ 0xffff;
1701 #define HWSIM_PACKETLEN 1500
1702 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1704 static void hostapd_data_test_rx(void *ctx
, const u8
*src_addr
, const u8
*buf
,
1707 struct hostapd_data
*hapd
= ctx
;
1708 const struct ether_header
*eth
;
1713 if (len
!= HWSIM_PACKETLEN
)
1716 eth
= (const struct ether_header
*) buf
;
1717 os_memcpy(&ip
, eth
+ 1, sizeof(ip
));
1718 pos
= &buf
[sizeof(*eth
) + sizeof(ip
)];
1720 if (ip
.ihl
!= 5 || ip
.version
!= 4 ||
1721 ntohs(ip
.tot_len
) != HWSIM_IP_LEN
)
1724 for (i
= 0; i
< HWSIM_IP_LEN
- sizeof(ip
); i
++) {
1730 wpa_msg(hapd
->msg_ctx
, MSG_INFO
, "DATA-TEST-RX " MACSTR
" " MACSTR
,
1731 MAC2STR(eth
->ether_dhost
), MAC2STR(eth
->ether_shost
));
1735 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data
*hapd
,
1738 int enabled
= atoi(cmd
);
1743 if (hapd
->l2_test
) {
1744 l2_packet_deinit(hapd
->l2_test
);
1745 hapd
->l2_test
= NULL
;
1746 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
,
1747 "test data: Disabled");
1755 pos
= os_strstr(cmd
, " ifname=");
1759 ifname
= hapd
->conf
->iface
;
1761 hapd
->l2_test
= l2_packet_init(ifname
, hapd
->own_addr
,
1762 ETHERTYPE_IP
, hostapd_data_test_rx
,
1764 if (hapd
->l2_test
== NULL
)
1767 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: Enabled");
1773 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data
*hapd
, char *cmd
)
1775 u8 dst
[ETH_ALEN
], src
[ETH_ALEN
];
1780 u8 buf
[2 + HWSIM_PACKETLEN
];
1781 struct ether_header
*eth
;
1786 if (hapd
->l2_test
== NULL
)
1789 /* format: <dst> <src> <tos> */
1792 used
= hwaddr_aton2(pos
, dst
);
1798 used
= hwaddr_aton2(pos
, src
);
1803 val
= strtol(pos
, NULL
, 0);
1804 if (val
< 0 || val
> 0xff)
1808 eth
= (struct ether_header
*) &buf
[2];
1809 os_memcpy(eth
->ether_dhost
, dst
, ETH_ALEN
);
1810 os_memcpy(eth
->ether_shost
, src
, ETH_ALEN
);
1811 eth
->ether_type
= htons(ETHERTYPE_IP
);
1812 ip
= (struct iphdr
*) (eth
+ 1);
1813 os_memset(ip
, 0, sizeof(*ip
));
1818 ip
->tot_len
= htons(HWSIM_IP_LEN
);
1820 ip
->saddr
= htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1821 ip
->daddr
= htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1822 ip
->check
= ipv4_hdr_checksum(ip
, sizeof(*ip
));
1823 dpos
= (u8
*) (ip
+ 1);
1824 for (i
= 0; i
< HWSIM_IP_LEN
- sizeof(*ip
); i
++)
1827 if (l2_packet_send(hapd
->l2_test
, dst
, ETHERTYPE_IP
, &buf
[2],
1828 HWSIM_PACKETLEN
) < 0)
1831 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: TX dst=" MACSTR
1832 " src=" MACSTR
" tos=0x%x", MAC2STR(dst
), MAC2STR(src
), tos
);
1838 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data
*hapd
,
1842 struct ether_header
*eth
;
1843 struct l2_packet_data
*l2
= NULL
;
1847 const char *ifname
= hapd
->conf
->iface
;
1849 if (os_strncmp(cmd
, "ifname=", 7) == 0) {
1852 cmd
= os_strchr(cmd
, ' ');
1858 len
= os_strlen(cmd
);
1859 if (len
& 1 || len
< ETH_HLEN
* 2)
1863 buf
= os_malloc(len
);
1867 if (hexstr2bin(cmd
, buf
, len
) < 0)
1870 eth
= (struct ether_header
*) buf
;
1871 ethertype
= ntohs(eth
->ether_type
);
1873 l2
= l2_packet_init(ifname
, hapd
->own_addr
, ethertype
,
1874 hostapd_data_test_rx
, hapd
, 1);
1878 res
= l2_packet_send(l2
, eth
->ether_dhost
, ethertype
, buf
, len
);
1879 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: TX frame res=%d", res
);
1882 l2_packet_deinit(l2
);
1885 return res
< 0 ? -1 : 0;
1889 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data
*hapd
, char *cmd
)
1891 #ifdef WPA_TRACE_BFD
1894 wpa_trace_fail_after
= atoi(cmd
);
1895 pos
= os_strchr(cmd
, ':');
1898 os_strlcpy(wpa_trace_fail_func
, pos
,
1899 sizeof(wpa_trace_fail_func
));
1901 wpa_trace_fail_after
= 0;
1905 #else /* WPA_TRACE_BFD */
1907 #endif /* WPA_TRACE_BFD */
1911 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data
*hapd
,
1912 char *buf
, size_t buflen
)
1914 #ifdef WPA_TRACE_BFD
1915 return os_snprintf(buf
, buflen
, "%u:%s", wpa_trace_fail_after
,
1916 wpa_trace_fail_func
);
1917 #else /* WPA_TRACE_BFD */
1919 #endif /* WPA_TRACE_BFD */
1923 static int hostapd_ctrl_test_fail(struct hostapd_data
*hapd
, char *cmd
)
1925 #ifdef WPA_TRACE_BFD
1928 wpa_trace_test_fail_after
= atoi(cmd
);
1929 pos
= os_strchr(cmd
, ':');
1932 os_strlcpy(wpa_trace_test_fail_func
, pos
,
1933 sizeof(wpa_trace_test_fail_func
));
1935 wpa_trace_test_fail_after
= 0;
1939 #else /* WPA_TRACE_BFD */
1941 #endif /* WPA_TRACE_BFD */
1945 static int hostapd_ctrl_get_fail(struct hostapd_data
*hapd
,
1946 char *buf
, size_t buflen
)
1948 #ifdef WPA_TRACE_BFD
1949 return os_snprintf(buf
, buflen
, "%u:%s", wpa_trace_test_fail_after
,
1950 wpa_trace_test_fail_func
);
1951 #else /* WPA_TRACE_BFD */
1953 #endif /* WPA_TRACE_BFD */
1957 static int hostapd_ctrl_reset_pn(struct hostapd_data
*hapd
, const char *cmd
)
1959 struct sta_info
*sta
;
1961 u8 zero
[WPA_TK_MAX_LEN
];
1963 os_memset(zero
, 0, sizeof(zero
));
1965 if (hwaddr_aton(cmd
, addr
))
1968 #ifdef CONFIG_IEEE80211W
1969 if (is_broadcast_ether_addr(addr
) && os_strstr(cmd
, "IGTK")) {
1970 if (hapd
->last_igtk_alg
== WPA_ALG_NONE
)
1973 wpa_printf(MSG_INFO
, "TESTING: Reset IPN for IGTK");
1975 /* First, use a zero key to avoid any possible duplicate key
1976 * avoidance in the driver. */
1977 if (hostapd_drv_set_key(hapd
->conf
->iface
, hapd
,
1978 hapd
->last_igtk_alg
,
1979 broadcast_ether_addr
,
1980 hapd
->last_igtk_key_idx
, 1, NULL
, 0,
1981 zero
, hapd
->last_igtk_len
) < 0)
1984 /* Set the previously configured key to reset its TSC */
1985 return hostapd_drv_set_key(hapd
->conf
->iface
, hapd
,
1986 hapd
->last_igtk_alg
,
1987 broadcast_ether_addr
,
1988 hapd
->last_igtk_key_idx
, 1, NULL
, 0,
1990 hapd
->last_igtk_len
);
1992 #endif /* CONFIG_IEEE80211W */
1994 if (is_broadcast_ether_addr(addr
)) {
1995 if (hapd
->last_gtk_alg
== WPA_ALG_NONE
)
1998 wpa_printf(MSG_INFO
, "TESTING: Reset PN for GTK");
2000 /* First, use a zero key to avoid any possible duplicate key
2001 * avoidance in the driver. */
2002 if (hostapd_drv_set_key(hapd
->conf
->iface
, hapd
,
2004 broadcast_ether_addr
,
2005 hapd
->last_gtk_key_idx
, 1, NULL
, 0,
2006 zero
, hapd
->last_gtk_len
) < 0)
2009 /* Set the previously configured key to reset its TSC */
2010 return hostapd_drv_set_key(hapd
->conf
->iface
, hapd
,
2012 broadcast_ether_addr
,
2013 hapd
->last_gtk_key_idx
, 1, NULL
, 0,
2014 hapd
->last_gtk
, hapd
->last_gtk_len
);
2017 sta
= ap_get_sta(hapd
, addr
);
2021 if (sta
->last_tk_alg
== WPA_ALG_NONE
)
2024 wpa_printf(MSG_INFO
, "TESTING: Reset PN for " MACSTR
,
2025 MAC2STR(sta
->addr
));
2027 /* First, use a zero key to avoid any possible duplicate key avoidance
2029 if (hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, sta
->last_tk_alg
,
2030 sta
->addr
, sta
->last_tk_key_idx
, 1, NULL
, 0,
2031 zero
, sta
->last_tk_len
) < 0)
2034 /* Set the previously configured key to reset its TSC/RSC */
2035 return hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, sta
->last_tk_alg
,
2036 sta
->addr
, sta
->last_tk_key_idx
, 1, NULL
, 0,
2037 sta
->last_tk
, sta
->last_tk_len
);
2041 static int hostapd_ctrl_set_key(struct hostapd_data
*hapd
, const char *cmd
)
2044 const char *pos
= cmd
;
2047 u8 seq
[6], key
[WPA_TK_MAX_LEN
];
2050 /* parameters: alg addr idx set_tx seq key */
2053 pos
= os_strchr(pos
, ' ');
2057 if (hwaddr_aton(pos
, addr
))
2064 pos
= os_strchr(pos
, ' ');
2069 pos
= os_strchr(pos
, ' ');
2073 if (hexstr2bin(pos
, seq
, sizeof(6)) < 0)
2079 key_len
= os_strlen(pos
) / 2;
2080 if (hexstr2bin(pos
, key
, key_len
) < 0)
2083 wpa_printf(MSG_INFO
, "TESTING: Set key");
2084 return hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, alg
, addr
, idx
,
2085 set_tx
, seq
, 6, key
, key_len
);
2089 static void restore_tk(void *ctx1
, void *ctx2
)
2091 struct hostapd_data
*hapd
= ctx1
;
2092 struct sta_info
*sta
= ctx2
;
2094 wpa_printf(MSG_INFO
, "TESTING: Restore TK for " MACSTR
,
2095 MAC2STR(sta
->addr
));
2096 /* This does not really restore the TSC properly, so this will result
2097 * in replay protection issues for now since there is no clean way of
2098 * preventing encryption of a single EAPOL frame. */
2099 hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, sta
->last_tk_alg
,
2100 sta
->addr
, sta
->last_tk_key_idx
, 1, NULL
, 0,
2101 sta
->last_tk
, sta
->last_tk_len
);
2105 static int hostapd_ctrl_resend_m1(struct hostapd_data
*hapd
, const char *cmd
)
2107 struct sta_info
*sta
;
2109 int plain
= os_strstr(cmd
, "plaintext") != NULL
;
2111 if (hwaddr_aton(cmd
, addr
))
2114 sta
= ap_get_sta(hapd
, addr
);
2115 if (!sta
|| !sta
->wpa_sm
)
2118 if (plain
&& sta
->last_tk_alg
== WPA_ALG_NONE
)
2119 plain
= 0; /* no need for special processing */
2121 wpa_printf(MSG_INFO
, "TESTING: Clear TK for " MACSTR
,
2122 MAC2STR(sta
->addr
));
2123 hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, WPA_ALG_NONE
,
2124 sta
->addr
, sta
->last_tk_key_idx
, 0, NULL
, 0,
2128 wpa_printf(MSG_INFO
, "TESTING: Send M1 to " MACSTR
, MAC2STR(sta
->addr
));
2129 return wpa_auth_resend_m1(sta
->wpa_sm
,
2130 os_strstr(cmd
, "change-anonce") != NULL
,
2131 plain
? restore_tk
: NULL
, hapd
, sta
);
2135 static int hostapd_ctrl_resend_m3(struct hostapd_data
*hapd
, const char *cmd
)
2137 struct sta_info
*sta
;
2139 int plain
= os_strstr(cmd
, "plaintext") != NULL
;
2141 if (hwaddr_aton(cmd
, addr
))
2144 sta
= ap_get_sta(hapd
, addr
);
2145 if (!sta
|| !sta
->wpa_sm
)
2148 if (plain
&& sta
->last_tk_alg
== WPA_ALG_NONE
)
2149 plain
= 0; /* no need for special processing */
2151 wpa_printf(MSG_INFO
, "TESTING: Clear TK for " MACSTR
,
2152 MAC2STR(sta
->addr
));
2153 hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, WPA_ALG_NONE
,
2154 sta
->addr
, sta
->last_tk_key_idx
, 0, NULL
, 0,
2158 wpa_printf(MSG_INFO
, "TESTING: Send M3 to " MACSTR
, MAC2STR(sta
->addr
));
2159 return wpa_auth_resend_m3(sta
->wpa_sm
,
2160 plain
? restore_tk
: NULL
, hapd
, sta
);
2164 static int hostapd_ctrl_resend_group_m1(struct hostapd_data
*hapd
,
2167 struct sta_info
*sta
;
2169 int plain
= os_strstr(cmd
, "plaintext") != NULL
;
2171 if (hwaddr_aton(cmd
, addr
))
2174 sta
= ap_get_sta(hapd
, addr
);
2175 if (!sta
|| !sta
->wpa_sm
)
2178 if (plain
&& sta
->last_tk_alg
== WPA_ALG_NONE
)
2179 plain
= 0; /* no need for special processing */
2181 wpa_printf(MSG_INFO
, "TESTING: Clear TK for " MACSTR
,
2182 MAC2STR(sta
->addr
));
2183 hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, WPA_ALG_NONE
,
2184 sta
->addr
, sta
->last_tk_key_idx
, 0, NULL
, 0,
2188 wpa_printf(MSG_INFO
,
2189 "TESTING: Send group M1 for the same GTK and zero RSC to "
2190 MACSTR
, MAC2STR(sta
->addr
));
2191 return wpa_auth_resend_group_m1(sta
->wpa_sm
,
2192 plain
? restore_tk
: NULL
, hapd
, sta
);
2195 #endif /* CONFIG_TESTING_OPTIONS */
2198 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface
*iface
,
2202 struct csa_settings settings
;
2206 ret
= hostapd_parse_csa_settings(pos
, &settings
);
2210 for (i
= 0; i
< iface
->num_bss
; i
++) {
2211 ret
= hostapd_switch_channel(iface
->bss
[i
], &settings
);
2213 /* FIX: What do we do if CSA fails in the middle of
2214 * submitting multi-BSS CSA requests? */
2220 #else /* NEED_AP_MLME */
2222 #endif /* NEED_AP_MLME */
2226 static int hostapd_ctrl_iface_mib(struct hostapd_data
*hapd
, char *reply
,
2227 int reply_size
, const char *param
)
2229 #ifdef RADIUS_SERVER
2230 if (os_strcmp(param
, "radius_server") == 0) {
2231 return radius_server_get_mib(hapd
->radius_srv
, reply
,
2234 #endif /* RADIUS_SERVER */
2239 static int hostapd_ctrl_iface_vendor(struct hostapd_data
*hapd
, char *cmd
,
2240 char *buf
, size_t buflen
)
2245 unsigned int vendor_id
, subcmd
;
2246 struct wpabuf
*reply
;
2247 size_t data_len
= 0;
2249 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
2250 vendor_id
= strtoul(cmd
, &pos
, 16);
2251 if (!isblank((unsigned char) *pos
))
2254 subcmd
= strtoul(pos
, &pos
, 10);
2257 if (!isblank((unsigned char) *pos
++))
2259 data_len
= os_strlen(pos
);
2264 data
= os_malloc(data_len
);
2268 if (hexstr2bin(pos
, data
, data_len
)) {
2269 wpa_printf(MSG_DEBUG
,
2270 "Vendor command: wrong parameter format");
2276 reply
= wpabuf_alloc((buflen
- 1) / 2);
2282 ret
= hostapd_drv_vendor_cmd(hapd
, vendor_id
, subcmd
, data
, data_len
,
2286 ret
= wpa_snprintf_hex(buf
, buflen
, wpabuf_head_u8(reply
),
2296 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data
*hapd
,
2300 struct sta_info
*sta
;
2302 if (hwaddr_aton(cmd
, addr
))
2305 sta
= ap_get_sta(hapd
, addr
);
2306 if (!sta
|| !sta
->eapol_sm
)
2309 eapol_auth_reauthenticate(sta
->eapol_sm
);
2314 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data
*hapd
, char *cmd
)
2317 struct sta_info
*sta
;
2318 char *pos
= cmd
, *param
;
2320 if (hwaddr_aton(pos
, addr
) || pos
[17] != ' ')
2324 pos
= os_strchr(pos
, ' ');
2329 sta
= ap_get_sta(hapd
, addr
);
2330 if (!sta
|| !sta
->eapol_sm
)
2333 return eapol_auth_set_conf(sta
->eapol_sm
, param
, pos
);
2337 static int hostapd_ctrl_iface_log_level(struct hostapd_data
*hapd
, char *cmd
,
2338 char *buf
, size_t buflen
)
2340 char *pos
, *end
, *stamp
;
2343 /* cmd: "LOG_LEVEL [<level>]" */
2347 ret
= os_snprintf(pos
, end
- pos
, "Current level: %s\n"
2349 debug_level_str(wpa_debug_level
),
2350 wpa_debug_timestamp
);
2351 if (os_snprintf_error(end
- pos
, ret
))
2360 stamp
= os_strchr(cmd
, ' ');
2363 while (*stamp
== ' ') {
2368 if (os_strlen(cmd
)) {
2369 int level
= str_to_debug_level(cmd
);
2372 wpa_debug_level
= level
;
2375 if (stamp
&& os_strlen(stamp
))
2376 wpa_debug_timestamp
= atoi(stamp
);
2378 os_memcpy(buf
, "OK\n", 3);
2384 static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data
*hapd
,
2385 char *buf
, size_t buflen
)
2387 struct hostapd_iface
*iface
= hapd
->iface
;
2389 struct hostapd_sta_info
*info
;
2390 struct os_reltime now
;
2392 if (!iface
->num_sta_seen
)
2395 sta_track_expire(iface
, 0);
2400 os_get_reltime(&now
);
2401 dl_list_for_each_reverse(info
, &iface
->sta_seen
,
2402 struct hostapd_sta_info
, list
) {
2403 struct os_reltime age
;
2406 os_reltime_sub(&now
, &info
->last_seen
, &age
);
2407 ret
= os_snprintf(pos
, end
- pos
, MACSTR
" %u %d\n",
2408 MAC2STR(info
->addr
), (unsigned int) age
.sec
,
2410 if (os_snprintf_error(end
- pos
, ret
))
2417 #endif /* NEED_AP_MLME */
2420 static int hostapd_ctrl_iface_req_lci(struct hostapd_data
*hapd
,
2425 if (hwaddr_aton(cmd
, addr
)) {
2426 wpa_printf(MSG_INFO
, "CTRL: REQ_LCI: Invalid MAC address");
2430 return hostapd_send_lci_req(hapd
, addr
);
2434 static int hostapd_ctrl_iface_req_range(struct hostapd_data
*hapd
, char *cmd
)
2437 char *token
, *context
= NULL
;
2438 int random_interval
, min_ap
;
2439 u8 responders
[ETH_ALEN
* RRM_RANGE_REQ_MAX_RESPONDERS
];
2440 unsigned int n_responders
;
2442 token
= str_token(cmd
, " ", &context
);
2443 if (!token
|| hwaddr_aton(token
, addr
)) {
2444 wpa_printf(MSG_INFO
,
2445 "CTRL: REQ_RANGE - Bad destination address");
2449 token
= str_token(cmd
, " ", &context
);
2453 random_interval
= atoi(token
);
2454 if (random_interval
< 0 || random_interval
> 0xffff)
2457 token
= str_token(cmd
, " ", &context
);
2461 min_ap
= atoi(token
);
2462 if (min_ap
<= 0 || min_ap
> WLAN_RRM_RANGE_REQ_MAX_MIN_AP
)
2466 while ((token
= str_token(cmd
, " ", &context
))) {
2467 if (n_responders
== RRM_RANGE_REQ_MAX_RESPONDERS
) {
2468 wpa_printf(MSG_INFO
,
2469 "CTRL: REQ_RANGE: Too many responders");
2473 if (hwaddr_aton(token
, responders
+ n_responders
* ETH_ALEN
)) {
2474 wpa_printf(MSG_INFO
,
2475 "CTRL: REQ_RANGE: Bad responder address");
2482 if (!n_responders
) {
2483 wpa_printf(MSG_INFO
,
2484 "CTRL: REQ_RANGE - No FTM responder address");
2488 return hostapd_send_range_req(hapd
, addr
, random_interval
, min_ap
,
2489 responders
, n_responders
);
2493 static int hostapd_ctrl_iface_req_beacon(struct hostapd_data
*hapd
,
2494 const char *cmd
, char *reply
,
2503 if (hwaddr_aton(cmd
, addr
))
2505 pos
= os_strchr(cmd
, ' ');
2509 if (os_strncmp(pos
, "req_mode=", 9) == 0) {
2510 int val
= hex2byte(pos
+ 9);
2516 pos
= os_strchr(pos
, ' ');
2521 req
= wpabuf_parse_bin(pos
);
2525 ret
= hostapd_send_beacon_req(hapd
, addr
, req_mode
, req
);
2528 ret
= os_snprintf(reply
, reply_size
, "%d", ret
);
2533 static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data
*hapd
, char *buf
)
2535 struct wpa_ssid_value ssid
;
2537 struct wpabuf
*nr
, *lci
= NULL
, *civic
= NULL
;
2542 if (!(hapd
->conf
->radio_measurements
[0] &
2543 WLAN_RRM_CAPS_NEIGHBOR_REPORT
)) {
2544 wpa_printf(MSG_ERROR
,
2545 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2549 if (hwaddr_aton(buf
, bssid
)) {
2550 wpa_printf(MSG_ERROR
, "CTRL: SET_NEIGHBOR: Bad BSSID");
2554 tmp
= os_strstr(buf
, "ssid=");
2555 if (!tmp
|| ssid_parse(tmp
+ 5, &ssid
)) {
2556 wpa_printf(MSG_ERROR
,
2557 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2560 buf
= os_strchr(tmp
+ 6, tmp
[5] == '"' ? '"' : ' ');
2564 tmp
= os_strstr(buf
, "nr=");
2566 wpa_printf(MSG_ERROR
,
2567 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2571 buf
= os_strchr(tmp
, ' ');
2575 nr
= wpabuf_parse_bin(tmp
+ 3);
2577 wpa_printf(MSG_ERROR
,
2578 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2585 tmp
= os_strstr(buf
, "lci=");
2587 buf
= os_strchr(tmp
, ' ');
2590 lci
= wpabuf_parse_bin(tmp
+ 4);
2592 wpa_printf(MSG_ERROR
,
2593 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
2602 tmp
= os_strstr(buf
, "civic=");
2604 buf
= os_strchr(tmp
, ' ');
2607 civic
= wpabuf_parse_bin(tmp
+ 6);
2609 wpa_printf(MSG_ERROR
,
2610 "CTRL: SET_NEIGHBOR: Bad civic subelement");
2620 if (os_strstr(buf
, "stat"))
2624 ret
= hostapd_neighbor_set(hapd
, bssid
, &ssid
, nr
, lci
, civic
,
2635 static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data
*hapd
,
2638 struct wpa_ssid_value ssid
;
2642 if (hwaddr_aton(buf
, bssid
)) {
2643 wpa_printf(MSG_ERROR
, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
2647 tmp
= os_strstr(buf
, "ssid=");
2648 if (!tmp
|| ssid_parse(tmp
+ 5, &ssid
)) {
2649 wpa_printf(MSG_ERROR
,
2650 "CTRL: REMOVE_NEIGHBORr: Bad or missing SSID");
2654 return hostapd_neighbor_remove(hapd
, bssid
, &ssid
);
2658 static int hostapd_ctrl_driver_flags(struct hostapd_iface
*iface
, char *buf
,
2664 ret
= os_snprintf(buf
, buflen
, "%016llX:\n",
2665 (long long unsigned) iface
->drv_flags
);
2666 if (os_snprintf_error(buflen
, ret
))
2672 for (i
= 0; i
< 64; i
++) {
2673 if (iface
->drv_flags
& (1LLU << i
)) {
2674 ret
= os_snprintf(pos
, end
- pos
, "%s\n",
2675 driver_flag_to_string(1LLU << i
));
2676 if (os_snprintf_error(end
- pos
, ret
))
2686 static int hostapd_ctrl_iface_receive_process(struct hostapd_data
*hapd
,
2687 char *buf
, char *reply
,
2689 struct sockaddr_storage
*from
,
2694 os_memcpy(reply
, "OK\n", 3);
2697 if (os_strcmp(buf
, "PING") == 0) {
2698 os_memcpy(reply
, "PONG\n", 5);
2700 } else if (os_strncmp(buf
, "RELOG", 5) == 0) {
2701 if (wpa_debug_reopen_file() < 0)
2703 } else if (os_strcmp(buf
, "STATUS") == 0) {
2704 reply_len
= hostapd_ctrl_iface_status(hapd
, reply
,
2706 } else if (os_strcmp(buf
, "STATUS-DRIVER") == 0) {
2707 reply_len
= hostapd_drv_status(hapd
, reply
, reply_size
);
2708 } else if (os_strcmp(buf
, "MIB") == 0) {
2709 reply_len
= ieee802_11_get_mib(hapd
, reply
, reply_size
);
2710 if (reply_len
>= 0) {
2711 res
= wpa_get_mib(hapd
->wpa_auth
, reply
+ reply_len
,
2712 reply_size
- reply_len
);
2718 if (reply_len
>= 0) {
2719 res
= ieee802_1x_get_mib(hapd
, reply
+ reply_len
,
2720 reply_size
- reply_len
);
2726 #ifndef CONFIG_NO_RADIUS
2727 if (reply_len
>= 0) {
2728 res
= radius_client_get_mib(hapd
->radius
,
2730 reply_size
- reply_len
);
2736 #endif /* CONFIG_NO_RADIUS */
2737 } else if (os_strncmp(buf
, "MIB ", 4) == 0) {
2738 reply_len
= hostapd_ctrl_iface_mib(hapd
, reply
, reply_size
,
2740 } else if (os_strcmp(buf
, "STA-FIRST") == 0) {
2741 reply_len
= hostapd_ctrl_iface_sta_first(hapd
, reply
,
2743 } else if (os_strncmp(buf
, "STA ", 4) == 0) {
2744 reply_len
= hostapd_ctrl_iface_sta(hapd
, buf
+ 4, reply
,
2746 } else if (os_strncmp(buf
, "STA-NEXT ", 9) == 0) {
2747 reply_len
= hostapd_ctrl_iface_sta_next(hapd
, buf
+ 9, reply
,
2749 } else if (os_strcmp(buf
, "ATTACH") == 0) {
2750 if (hostapd_ctrl_iface_attach(hapd
, from
, fromlen
, NULL
))
2752 } else if (os_strncmp(buf
, "ATTACH ", 7) == 0) {
2753 if (hostapd_ctrl_iface_attach(hapd
, from
, fromlen
, buf
+ 7))
2755 } else if (os_strcmp(buf
, "DETACH") == 0) {
2756 if (hostapd_ctrl_iface_detach(hapd
, from
, fromlen
))
2758 } else if (os_strncmp(buf
, "LEVEL ", 6) == 0) {
2759 if (hostapd_ctrl_iface_level(hapd
, from
, fromlen
,
2762 } else if (os_strncmp(buf
, "NEW_STA ", 8) == 0) {
2763 if (hostapd_ctrl_iface_new_sta(hapd
, buf
+ 8))
2765 } else if (os_strncmp(buf
, "DEAUTHENTICATE ", 15) == 0) {
2766 if (hostapd_ctrl_iface_deauthenticate(hapd
, buf
+ 15))
2768 } else if (os_strncmp(buf
, "DISASSOCIATE ", 13) == 0) {
2769 if (hostapd_ctrl_iface_disassociate(hapd
, buf
+ 13))
2771 #ifdef CONFIG_TAXONOMY
2772 } else if (os_strncmp(buf
, "SIGNATURE ", 10) == 0) {
2773 reply_len
= hostapd_ctrl_iface_signature(hapd
, buf
+ 10,
2775 #endif /* CONFIG_TAXONOMY */
2776 } else if (os_strncmp(buf
, "POLL_STA ", 9) == 0) {
2777 if (hostapd_ctrl_iface_poll_sta(hapd
, buf
+ 9))
2779 } else if (os_strcmp(buf
, "STOP_AP") == 0) {
2780 if (hostapd_ctrl_iface_stop_ap(hapd
))
2782 #ifdef CONFIG_IEEE80211W
2784 } else if (os_strncmp(buf
, "SA_QUERY ", 9) == 0) {
2785 if (hostapd_ctrl_iface_sa_query(hapd
, buf
+ 9))
2787 #endif /* NEED_AP_MLME */
2788 #endif /* CONFIG_IEEE80211W */
2790 } else if (os_strncmp(buf
, "WPS_PIN ", 8) == 0) {
2791 if (hostapd_ctrl_iface_wps_pin(hapd
, buf
+ 8))
2793 } else if (os_strncmp(buf
, "WPS_CHECK_PIN ", 14) == 0) {
2794 reply_len
= hostapd_ctrl_iface_wps_check_pin(
2795 hapd
, buf
+ 14, reply
, reply_size
);
2796 } else if (os_strcmp(buf
, "WPS_PBC") == 0) {
2797 if (hostapd_wps_button_pushed(hapd
, NULL
))
2799 } else if (os_strcmp(buf
, "WPS_CANCEL") == 0) {
2800 if (hostapd_wps_cancel(hapd
))
2802 } else if (os_strncmp(buf
, "WPS_AP_PIN ", 11) == 0) {
2803 reply_len
= hostapd_ctrl_iface_wps_ap_pin(hapd
, buf
+ 11,
2805 } else if (os_strncmp(buf
, "WPS_CONFIG ", 11) == 0) {
2806 if (hostapd_ctrl_iface_wps_config(hapd
, buf
+ 11) < 0)
2808 } else if (os_strncmp(buf
, "WPS_GET_STATUS", 13) == 0) {
2809 reply_len
= hostapd_ctrl_iface_wps_get_status(hapd
, reply
,
2811 #ifdef CONFIG_WPS_NFC
2812 } else if (os_strncmp(buf
, "WPS_NFC_TAG_READ ", 17) == 0) {
2813 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd
, buf
+ 17))
2815 } else if (os_strncmp(buf
, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2816 reply_len
= hostapd_ctrl_iface_wps_nfc_config_token(
2817 hapd
, buf
+ 21, reply
, reply_size
);
2818 } else if (os_strncmp(buf
, "WPS_NFC_TOKEN ", 14) == 0) {
2819 reply_len
= hostapd_ctrl_iface_wps_nfc_token(
2820 hapd
, buf
+ 14, reply
, reply_size
);
2821 } else if (os_strncmp(buf
, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2822 reply_len
= hostapd_ctrl_iface_nfc_get_handover_sel(
2823 hapd
, buf
+ 21, reply
, reply_size
);
2824 } else if (os_strncmp(buf
, "NFC_REPORT_HANDOVER ", 20) == 0) {
2825 if (hostapd_ctrl_iface_nfc_report_handover(hapd
, buf
+ 20))
2827 #endif /* CONFIG_WPS_NFC */
2828 #endif /* CONFIG_WPS */
2829 #ifdef CONFIG_INTERWORKING
2830 } else if (os_strncmp(buf
, "SET_QOS_MAP_SET ", 16) == 0) {
2831 if (hostapd_ctrl_iface_set_qos_map_set(hapd
, buf
+ 16))
2833 } else if (os_strncmp(buf
, "SEND_QOS_MAP_CONF ", 18) == 0) {
2834 if (hostapd_ctrl_iface_send_qos_map_conf(hapd
, buf
+ 18))
2836 #endif /* CONFIG_INTERWORKING */
2838 } else if (os_strncmp(buf
, "HS20_WNM_NOTIF ", 15) == 0) {
2839 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd
, buf
+ 15))
2841 } else if (os_strncmp(buf
, "HS20_DEAUTH_REQ ", 16) == 0) {
2842 if (hostapd_ctrl_iface_hs20_deauth_req(hapd
, buf
+ 16))
2844 #endif /* CONFIG_HS20 */
2845 #ifdef CONFIG_WNM_AP
2846 } else if (os_strncmp(buf
, "DISASSOC_IMMINENT ", 18) == 0) {
2847 if (hostapd_ctrl_iface_disassoc_imminent(hapd
, buf
+ 18))
2849 } else if (os_strncmp(buf
, "ESS_DISASSOC ", 13) == 0) {
2850 if (hostapd_ctrl_iface_ess_disassoc(hapd
, buf
+ 13))
2852 } else if (os_strncmp(buf
, "BSS_TM_REQ ", 11) == 0) {
2853 if (hostapd_ctrl_iface_bss_tm_req(hapd
, buf
+ 11))
2855 #endif /* CONFIG_WNM_AP */
2856 } else if (os_strcmp(buf
, "GET_CONFIG") == 0) {
2857 reply_len
= hostapd_ctrl_iface_get_config(hapd
, reply
,
2859 } else if (os_strncmp(buf
, "SET ", 4) == 0) {
2860 if (hostapd_ctrl_iface_set(hapd
, buf
+ 4))
2862 } else if (os_strncmp(buf
, "GET ", 4) == 0) {
2863 reply_len
= hostapd_ctrl_iface_get(hapd
, buf
+ 4, reply
,
2865 } else if (os_strncmp(buf
, "ENABLE", 6) == 0) {
2866 if (hostapd_ctrl_iface_enable(hapd
->iface
))
2868 } else if (os_strncmp(buf
, "RELOAD", 6) == 0) {
2869 if (hostapd_ctrl_iface_reload(hapd
->iface
))
2871 } else if (os_strncmp(buf
, "DISABLE", 7) == 0) {
2872 if (hostapd_ctrl_iface_disable(hapd
->iface
))
2874 } else if (os_strcmp(buf
, "UPDATE_BEACON") == 0) {
2875 if (ieee802_11_set_beacon(hapd
))
2877 #ifdef CONFIG_TESTING_OPTIONS
2878 } else if (os_strncmp(buf
, "RADAR ", 6) == 0) {
2879 if (hostapd_ctrl_iface_radar(hapd
, buf
+ 6))
2881 } else if (os_strncmp(buf
, "MGMT_TX ", 8) == 0) {
2882 if (hostapd_ctrl_iface_mgmt_tx(hapd
, buf
+ 8))
2884 } else if (os_strncmp(buf
, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
2885 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd
,
2888 } else if (os_strncmp(buf
, "MGMT_RX_PROCESS ", 16) == 0) {
2889 if (hostapd_ctrl_iface_mgmt_rx_process(hapd
, buf
+ 16) < 0)
2891 } else if (os_strncmp(buf
, "EAPOL_RX ", 9) == 0) {
2892 if (hostapd_ctrl_iface_eapol_rx(hapd
, buf
+ 9) < 0)
2894 } else if (os_strncmp(buf
, "DATA_TEST_CONFIG ", 17) == 0) {
2895 if (hostapd_ctrl_iface_data_test_config(hapd
, buf
+ 17) < 0)
2897 } else if (os_strncmp(buf
, "DATA_TEST_TX ", 13) == 0) {
2898 if (hostapd_ctrl_iface_data_test_tx(hapd
, buf
+ 13) < 0)
2900 } else if (os_strncmp(buf
, "DATA_TEST_FRAME ", 16) == 0) {
2901 if (hostapd_ctrl_iface_data_test_frame(hapd
, buf
+ 16) < 0)
2903 } else if (os_strncmp(buf
, "TEST_ALLOC_FAIL ", 16) == 0) {
2904 if (hostapd_ctrl_test_alloc_fail(hapd
, buf
+ 16) < 0)
2906 } else if (os_strcmp(buf
, "GET_ALLOC_FAIL") == 0) {
2907 reply_len
= hostapd_ctrl_get_alloc_fail(hapd
, reply
,
2909 } else if (os_strncmp(buf
, "TEST_FAIL ", 10) == 0) {
2910 if (hostapd_ctrl_test_fail(hapd
, buf
+ 10) < 0)
2912 } else if (os_strcmp(buf
, "GET_FAIL") == 0) {
2913 reply_len
= hostapd_ctrl_get_fail(hapd
, reply
, reply_size
);
2914 } else if (os_strncmp(buf
, "RESET_PN ", 9) == 0) {
2915 if (hostapd_ctrl_reset_pn(hapd
, buf
+ 9) < 0)
2917 } else if (os_strncmp(buf
, "SET_KEY ", 8) == 0) {
2918 if (hostapd_ctrl_set_key(hapd
, buf
+ 8) < 0)
2920 } else if (os_strncmp(buf
, "RESEND_M1 ", 10) == 0) {
2921 if (hostapd_ctrl_resend_m1(hapd
, buf
+ 10) < 0)
2923 } else if (os_strncmp(buf
, "RESEND_M3 ", 10) == 0) {
2924 if (hostapd_ctrl_resend_m3(hapd
, buf
+ 10) < 0)
2926 } else if (os_strncmp(buf
, "RESEND_GROUP_M1 ", 16) == 0) {
2927 if (hostapd_ctrl_resend_group_m1(hapd
, buf
+ 16) < 0)
2929 } else if (os_strcmp(buf
, "REKEY_GTK") == 0) {
2930 if (wpa_auth_rekey_gtk(hapd
->wpa_auth
) < 0)
2932 #endif /* CONFIG_TESTING_OPTIONS */
2933 } else if (os_strncmp(buf
, "CHAN_SWITCH ", 12) == 0) {
2934 if (hostapd_ctrl_iface_chan_switch(hapd
->iface
, buf
+ 12))
2936 } else if (os_strncmp(buf
, "VENDOR ", 7) == 0) {
2937 reply_len
= hostapd_ctrl_iface_vendor(hapd
, buf
+ 7, reply
,
2939 } else if (os_strcmp(buf
, "ERP_FLUSH") == 0) {
2940 ieee802_1x_erp_flush(hapd
);
2941 #ifdef RADIUS_SERVER
2942 radius_server_erp_flush(hapd
->radius_srv
);
2943 #endif /* RADIUS_SERVER */
2944 } else if (os_strncmp(buf
, "EAPOL_REAUTH ", 13) == 0) {
2945 if (hostapd_ctrl_iface_eapol_reauth(hapd
, buf
+ 13))
2947 } else if (os_strncmp(buf
, "EAPOL_SET ", 10) == 0) {
2948 if (hostapd_ctrl_iface_eapol_set(hapd
, buf
+ 10))
2950 } else if (os_strncmp(buf
, "LOG_LEVEL", 9) == 0) {
2951 reply_len
= hostapd_ctrl_iface_log_level(
2952 hapd
, buf
+ 9, reply
, reply_size
);
2954 } else if (os_strcmp(buf
, "TRACK_STA_LIST") == 0) {
2955 reply_len
= hostapd_ctrl_iface_track_sta_list(
2956 hapd
, reply
, reply_size
);
2957 #endif /* NEED_AP_MLME */
2958 } else if (os_strcmp(buf
, "PMKSA") == 0) {
2959 reply_len
= hostapd_ctrl_iface_pmksa_list(hapd
, reply
,
2961 } else if (os_strcmp(buf
, "PMKSA_FLUSH") == 0) {
2962 hostapd_ctrl_iface_pmksa_flush(hapd
);
2963 } else if (os_strncmp(buf
, "PMKSA_ADD ", 10) == 0) {
2964 if (hostapd_ctrl_iface_pmksa_add(hapd
, buf
+ 10) < 0)
2966 } else if (os_strncmp(buf
, "SET_NEIGHBOR ", 13) == 0) {
2967 if (hostapd_ctrl_iface_set_neighbor(hapd
, buf
+ 13))
2969 } else if (os_strncmp(buf
, "REMOVE_NEIGHBOR ", 16) == 0) {
2970 if (hostapd_ctrl_iface_remove_neighbor(hapd
, buf
+ 16))
2972 } else if (os_strncmp(buf
, "REQ_LCI ", 8) == 0) {
2973 if (hostapd_ctrl_iface_req_lci(hapd
, buf
+ 8))
2975 } else if (os_strncmp(buf
, "REQ_RANGE ", 10) == 0) {
2976 if (hostapd_ctrl_iface_req_range(hapd
, buf
+ 10))
2978 } else if (os_strncmp(buf
, "REQ_BEACON ", 11) == 0) {
2979 reply_len
= hostapd_ctrl_iface_req_beacon(hapd
, buf
+ 11,
2981 } else if (os_strcmp(buf
, "DRIVER_FLAGS") == 0) {
2982 reply_len
= hostapd_ctrl_driver_flags(hapd
->iface
, reply
,
2984 } else if (os_strcmp(buf
, "TERMINATE") == 0) {
2987 } else if (os_strncmp(buf
, "DPP_QR_CODE ", 12) == 0) {
2988 res
= hostapd_dpp_qr_code(hapd
, buf
+ 12);
2992 reply_len
= os_snprintf(reply
, reply_size
, "%d", res
);
2993 if (os_snprintf_error(reply_size
, reply_len
))
2996 } else if (os_strncmp(buf
, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
2997 res
= hostapd_dpp_bootstrap_gen(hapd
, buf
+ 18);
3001 reply_len
= os_snprintf(reply
, reply_size
, "%d", res
);
3002 if (os_snprintf_error(reply_size
, reply_len
))
3005 } else if (os_strncmp(buf
, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
3006 if (hostapd_dpp_bootstrap_remove(hapd
, buf
+ 21) < 0)
3008 } else if (os_strncmp(buf
, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
3011 uri
= hostapd_dpp_bootstrap_get_uri(hapd
, atoi(buf
+ 22));
3015 reply_len
= os_snprintf(reply
, reply_size
, "%s", uri
);
3016 if (os_snprintf_error(reply_size
, reply_len
))
3019 } else if (os_strncmp(buf
, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
3020 reply_len
= hostapd_dpp_bootstrap_info(hapd
, atoi(buf
+ 19),
3022 } else if (os_strncmp(buf
, "DPP_AUTH_INIT ", 14) == 0) {
3023 if (hostapd_dpp_auth_init(hapd
, buf
+ 13) < 0)
3025 } else if (os_strncmp(buf
, "DPP_LISTEN ", 11) == 0) {
3026 if (hostapd_dpp_listen(hapd
, buf
+ 11) < 0)
3028 } else if (os_strcmp(buf
, "DPP_STOP_LISTEN") == 0) {
3029 hostapd_dpp_stop(hapd
);
3030 hostapd_dpp_listen_stop(hapd
);
3031 } else if (os_strncmp(buf
, "DPP_CONFIGURATOR_ADD", 20) == 0) {
3032 res
= hostapd_dpp_configurator_add(hapd
, buf
+ 20);
3036 reply_len
= os_snprintf(reply
, reply_size
, "%d", res
);
3037 if (os_snprintf_error(reply_size
, reply_len
))
3040 } else if (os_strncmp(buf
, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
3041 if (hostapd_dpp_configurator_remove(hapd
, buf
+ 24) < 0)
3043 } else if (os_strncmp(buf
, "DPP_PKEX_ADD ", 13) == 0) {
3044 res
= hostapd_dpp_pkex_add(hapd
, buf
+ 12);
3048 reply_len
= os_snprintf(reply
, reply_size
, "%d", res
);
3049 if (os_snprintf_error(reply_size
, reply_len
))
3052 } else if (os_strncmp(buf
, "DPP_PKEX_REMOVE ", 16) == 0) {
3053 if (hostapd_dpp_pkex_remove(hapd
, buf
+ 16) < 0)
3055 #endif /* CONFIG_DPP */
3057 os_memcpy(reply
, "UNKNOWN COMMAND\n", 16);
3061 if (reply_len
< 0) {
3062 os_memcpy(reply
, "FAIL\n", 5);
3070 static void hostapd_ctrl_iface_receive(int sock
, void *eloop_ctx
,
3073 struct hostapd_data
*hapd
= eloop_ctx
;
3076 struct sockaddr_storage from
;
3077 socklen_t fromlen
= sizeof(from
);
3078 char *reply
, *pos
= buf
;
3079 const int reply_size
= 4096;
3081 int level
= MSG_DEBUG
;
3082 #ifdef CONFIG_CTRL_IFACE_UDP
3083 unsigned char lcookie
[COOKIE_LEN
];
3084 #endif /* CONFIG_CTRL_IFACE_UDP */
3086 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
3087 (struct sockaddr
*) &from
, &fromlen
);
3089 wpa_printf(MSG_ERROR
, "recvfrom(ctrl_iface): %s",
3095 reply
= os_malloc(reply_size
);
3096 if (reply
== NULL
) {
3097 if (sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
3099 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
3105 #ifdef CONFIG_CTRL_IFACE_UDP
3106 if (os_strcmp(buf
, "GET_COOKIE") == 0) {
3107 os_memcpy(reply
, "COOKIE=", 7);
3108 wpa_snprintf_hex(reply
+ 7, 2 * COOKIE_LEN
+ 1,
3109 cookie
, COOKIE_LEN
);
3110 reply_len
= 7 + 2 * COOKIE_LEN
;
3114 if (os_strncmp(buf
, "COOKIE=", 7) != 0 ||
3115 hexstr2bin(buf
+ 7, lcookie
, COOKIE_LEN
) < 0) {
3116 wpa_printf(MSG_DEBUG
,
3117 "CTRL: No cookie in the request - drop request");
3122 if (os_memcmp(cookie
, lcookie
, COOKIE_LEN
) != 0) {
3123 wpa_printf(MSG_DEBUG
,
3124 "CTRL: Invalid cookie in the request - drop request");
3129 pos
= buf
+ 7 + 2 * COOKIE_LEN
;
3132 #endif /* CONFIG_CTRL_IFACE_UDP */
3134 if (os_strcmp(pos
, "PING") == 0)
3135 level
= MSG_EXCESSIVE
;
3136 wpa_hexdump_ascii(level
, "RX ctrl_iface", pos
, res
);
3138 reply_len
= hostapd_ctrl_iface_receive_process(hapd
, pos
,
3142 #ifdef CONFIG_CTRL_IFACE_UDP
3144 #endif /* CONFIG_CTRL_IFACE_UDP */
3145 if (sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
3147 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
3154 #ifndef CONFIG_CTRL_IFACE_UDP
3155 static char * hostapd_ctrl_iface_path(struct hostapd_data
*hapd
)
3160 if (hapd
->conf
->ctrl_interface
== NULL
)
3163 len
= os_strlen(hapd
->conf
->ctrl_interface
) +
3164 os_strlen(hapd
->conf
->iface
) + 2;
3165 buf
= os_malloc(len
);
3169 os_snprintf(buf
, len
, "%s/%s",
3170 hapd
->conf
->ctrl_interface
, hapd
->conf
->iface
);
3171 buf
[len
- 1] = '\0';
3174 #endif /* CONFIG_CTRL_IFACE_UDP */
3177 static void hostapd_ctrl_iface_msg_cb(void *ctx
, int level
,
3178 enum wpa_msg_type type
,
3179 const char *txt
, size_t len
)
3181 struct hostapd_data
*hapd
= ctx
;
3184 hostapd_ctrl_iface_send(hapd
, level
, type
, txt
, len
);
3188 int hostapd_ctrl_iface_init(struct hostapd_data
*hapd
)
3190 #ifdef CONFIG_CTRL_IFACE_UDP
3191 int port
= HOSTAPD_CTRL_IFACE_PORT
;
3193 char port_str
[40], *tmp
;
3195 struct addrinfo hints
= { 0 }, *res
, *saveres
;
3198 if (hapd
->ctrl_sock
> -1) {
3199 wpa_printf(MSG_DEBUG
, "ctrl_iface already exists!");
3203 if (hapd
->conf
->ctrl_interface
== NULL
)
3206 pos
= os_strstr(hapd
->conf
->ctrl_interface
, "udp:");
3211 wpa_printf(MSG_ERROR
, "Invalid ctrl_iface UDP port");
3216 dl_list_init(&hapd
->ctrl_dst
);
3217 hapd
->ctrl_sock
= -1;
3218 os_get_random(cookie
, COOKIE_LEN
);
3220 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3221 hints
.ai_flags
= AI_PASSIVE
;
3222 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3224 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3225 hints
.ai_family
= AF_INET6
;
3226 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3227 hints
.ai_family
= AF_INET
;
3228 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3229 hints
.ai_socktype
= SOCK_DGRAM
;
3232 os_snprintf(p
, sizeof(p
), "%d", port
);
3233 n
= getaddrinfo(NULL
, p
, &hints
, &res
);
3235 wpa_printf(MSG_ERROR
, "getaddrinfo(): %s", gai_strerror(n
));
3240 hapd
->ctrl_sock
= socket(res
->ai_family
, res
->ai_socktype
,
3242 if (hapd
->ctrl_sock
< 0) {
3243 wpa_printf(MSG_ERROR
, "socket(PF_INET): %s", strerror(errno
));
3247 if (bind(hapd
->ctrl_sock
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
3249 if ((HOSTAPD_CTRL_IFACE_PORT
- port
) <
3250 HOSTAPD_CTRL_IFACE_PORT_LIMIT
&& !pos
)
3252 wpa_printf(MSG_ERROR
, "bind(AF_INET): %s", strerror(errno
));
3256 freeaddrinfo(saveres
);
3258 os_snprintf(port_str
, sizeof(port_str
), "udp:%d", port
);
3259 tmp
= os_strdup(port_str
);
3261 os_free(hapd
->conf
->ctrl_interface
);
3262 hapd
->conf
->ctrl_interface
= tmp
;
3264 wpa_printf(MSG_DEBUG
, "ctrl_iface_init UDP port: %d", port
);
3266 if (eloop_register_read_sock(hapd
->ctrl_sock
,
3267 hostapd_ctrl_iface_receive
, hapd
, NULL
) <
3269 hostapd_ctrl_iface_deinit(hapd
);
3273 hapd
->msg_ctx
= hapd
;
3274 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb
);
3279 if (hapd
->ctrl_sock
>= 0)
3280 close(hapd
->ctrl_sock
);
3282 #else /* CONFIG_CTRL_IFACE_UDP */
3283 struct sockaddr_un addr
;
3287 if (hapd
->ctrl_sock
> -1) {
3288 wpa_printf(MSG_DEBUG
, "ctrl_iface already exists!");
3292 dl_list_init(&hapd
->ctrl_dst
);
3294 if (hapd
->conf
->ctrl_interface
== NULL
)
3297 if (mkdir(hapd
->conf
->ctrl_interface
, S_IRWXU
| S_IRWXG
) < 0) {
3298 if (errno
== EEXIST
) {
3299 wpa_printf(MSG_DEBUG
, "Using existing control "
3300 "interface directory.");
3302 wpa_printf(MSG_ERROR
, "mkdir[ctrl_interface]: %s",
3308 if (hapd
->conf
->ctrl_interface_gid_set
&&
3309 chown(hapd
->conf
->ctrl_interface
, -1,
3310 hapd
->conf
->ctrl_interface_gid
) < 0) {
3311 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
3316 if (!hapd
->conf
->ctrl_interface_gid_set
&&
3317 hapd
->iface
->interfaces
->ctrl_iface_group
&&
3318 chown(hapd
->conf
->ctrl_interface
, -1,
3319 hapd
->iface
->interfaces
->ctrl_iface_group
) < 0) {
3320 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
3327 * Android is using umask 0077 which would leave the control interface
3328 * directory without group access. This breaks things since Wi-Fi
3329 * framework assumes that this directory can be accessed by other
3330 * applications in the wifi group. Fix this by adding group access even
3331 * if umask value would prevent this.
3333 if (chmod(hapd
->conf
->ctrl_interface
, S_IRWXU
| S_IRWXG
) < 0) {
3334 wpa_printf(MSG_ERROR
, "CTRL: Could not chmod directory: %s",
3336 /* Try to continue anyway */
3338 #endif /* ANDROID */
3340 if (os_strlen(hapd
->conf
->ctrl_interface
) + 1 +
3341 os_strlen(hapd
->conf
->iface
) >= sizeof(addr
.sun_path
))
3344 s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
3346 wpa_printf(MSG_ERROR
, "socket(PF_UNIX): %s", strerror(errno
));
3350 os_memset(&addr
, 0, sizeof(addr
));
3352 addr
.sun_len
= sizeof(addr
);
3353 #endif /* __FreeBSD__ */
3354 addr
.sun_family
= AF_UNIX
;
3355 fname
= hostapd_ctrl_iface_path(hapd
);
3358 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
3359 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
3360 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
3362 if (connect(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
3363 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
3364 " allow connections - assuming it was left"
3365 "over from forced program termination");
3366 if (unlink(fname
) < 0) {
3367 wpa_printf(MSG_ERROR
,
3368 "Could not unlink existing ctrl_iface socket '%s': %s",
3369 fname
, strerror(errno
));
3372 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) <
3374 wpa_printf(MSG_ERROR
,
3375 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
3379 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
3380 "ctrl_iface socket '%s'", fname
);
3382 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
3383 "be in use - cannot override it");
3384 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
3385 "not used anymore", fname
);
3392 if (hapd
->conf
->ctrl_interface_gid_set
&&
3393 chown(fname
, -1, hapd
->conf
->ctrl_interface_gid
) < 0) {
3394 wpa_printf(MSG_ERROR
, "chown[ctrl_interface/ifname]: %s",
3399 if (!hapd
->conf
->ctrl_interface_gid_set
&&
3400 hapd
->iface
->interfaces
->ctrl_iface_group
&&
3401 chown(fname
, -1, hapd
->iface
->interfaces
->ctrl_iface_group
) < 0) {
3402 wpa_printf(MSG_ERROR
, "chown[ctrl_interface/ifname]: %s",
3407 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
3408 wpa_printf(MSG_ERROR
, "chmod[ctrl_interface/ifname]: %s",
3414 hapd
->ctrl_sock
= s
;
3415 if (eloop_register_read_sock(s
, hostapd_ctrl_iface_receive
, hapd
,
3417 hostapd_ctrl_iface_deinit(hapd
);
3420 hapd
->msg_ctx
= hapd
;
3421 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb
);
3433 #endif /* CONFIG_CTRL_IFACE_UDP */
3437 void hostapd_ctrl_iface_deinit(struct hostapd_data
*hapd
)
3439 struct wpa_ctrl_dst
*dst
, *prev
;
3441 if (hapd
->ctrl_sock
> -1) {
3442 #ifndef CONFIG_CTRL_IFACE_UDP
3444 #endif /* !CONFIG_CTRL_IFACE_UDP */
3446 eloop_unregister_read_sock(hapd
->ctrl_sock
);
3447 close(hapd
->ctrl_sock
);
3448 hapd
->ctrl_sock
= -1;
3449 #ifndef CONFIG_CTRL_IFACE_UDP
3450 fname
= hostapd_ctrl_iface_path(hapd
);
3455 if (hapd
->conf
->ctrl_interface
&&
3456 rmdir(hapd
->conf
->ctrl_interface
) < 0) {
3457 if (errno
== ENOTEMPTY
) {
3458 wpa_printf(MSG_DEBUG
, "Control interface "
3459 "directory not empty - leaving it "
3462 wpa_printf(MSG_ERROR
,
3463 "rmdir[ctrl_interface=%s]: %s",
3464 hapd
->conf
->ctrl_interface
,
3468 #endif /* !CONFIG_CTRL_IFACE_UDP */
3471 dl_list_for_each_safe(dst
, prev
, &hapd
->ctrl_dst
, struct wpa_ctrl_dst
,
3475 #ifdef CONFIG_TESTING_OPTIONS
3476 l2_packet_deinit(hapd
->l2_test
);
3477 hapd
->l2_test
= NULL
;
3478 #endif /* CONFIG_TESTING_OPTIONS */
3482 static int hostapd_ctrl_iface_add(struct hapd_interfaces
*interfaces
,
3485 if (hostapd_add_iface(interfaces
, buf
) < 0) {
3486 wpa_printf(MSG_ERROR
, "Adding interface %s failed", buf
);
3493 static int hostapd_ctrl_iface_remove(struct hapd_interfaces
*interfaces
,
3496 if (hostapd_remove_iface(interfaces
, buf
) < 0) {
3497 wpa_printf(MSG_ERROR
, "Removing interface %s failed", buf
);
3504 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces
*interfaces
,
3505 struct sockaddr_storage
*from
,
3506 socklen_t fromlen
, char *input
)
3508 return ctrl_iface_attach(&interfaces
->global_ctrl_dst
, from
, fromlen
,
3513 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces
*interfaces
,
3514 struct sockaddr_storage
*from
,
3517 return ctrl_iface_detach(&interfaces
->global_ctrl_dst
, from
, fromlen
);
3521 static void hostapd_ctrl_iface_flush(struct hapd_interfaces
*interfaces
)
3523 #ifdef CONFIG_WPS_TESTING
3524 wps_version_number
= 0x20;
3525 wps_testing_dummy_cred
= 0;
3526 wps_corrupt_pkhash
= 0;
3527 #endif /* CONFIG_WPS_TESTING */
3529 #ifdef CONFIG_TESTING_OPTIONS
3531 dpp_test
= DPP_TEST_DISABLED
;
3532 #endif /* CONFIG_DPP */
3533 #endif /* CONFIG_TESTING_OPTIONS */
3536 hostapd_dpp_deinit_global(interfaces
);
3537 #endif /* CONFIG_DPP */
3544 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces
*interfaces
,
3547 char ifname
[IFNAMSIZ
+ 1];
3548 struct fst_iface_cfg cfg
;
3549 struct hostapd_data
*hapd
;
3550 struct fst_wpa_obj iface_obj
;
3552 if (!fst_parse_attach_command(cmd
, ifname
, sizeof(ifname
), &cfg
)) {
3553 hapd
= hostapd_get_iface(interfaces
, ifname
);
3555 if (hapd
->iface
->fst
) {
3556 wpa_printf(MSG_INFO
, "FST: Already attached");
3559 fst_hostapd_fill_iface_obj(hapd
, &iface_obj
);
3560 hapd
->iface
->fst
= fst_attach(ifname
, hapd
->own_addr
,
3562 if (hapd
->iface
->fst
)
3572 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces
*interfaces
,
3575 char ifname
[IFNAMSIZ
+ 1];
3576 struct hostapd_data
* hapd
;
3578 if (!fst_parse_detach_command(cmd
, ifname
, sizeof(ifname
))) {
3579 hapd
= hostapd_get_iface(interfaces
, ifname
);
3581 if (!fst_iface_detach(ifname
)) {
3582 hapd
->iface
->fst
= NULL
;
3583 hapd
->iface
->fst_ies
= NULL
;
3592 #endif /* CONFIG_FST */
3595 static struct hostapd_data
*
3596 hostapd_interfaces_get_hapd(struct hapd_interfaces
*interfaces
,
3601 for (i
= 0; i
< interfaces
->count
; i
++) {
3602 struct hostapd_iface
*iface
= interfaces
->iface
[i
];
3604 for (j
= 0; j
< iface
->num_bss
; j
++) {
3605 struct hostapd_data
*hapd
;
3607 hapd
= iface
->bss
[j
];
3608 if (os_strcmp(ifname
, hapd
->conf
->iface
) == 0)
3617 static int hostapd_ctrl_iface_dup_param(struct hostapd_data
*src_hapd
,
3618 struct hostapd_data
*dst_hapd
,
3624 value
= os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN
);
3626 wpa_printf(MSG_ERROR
,
3627 "DUP: cannot allocate buffer to stringify %s",
3632 if (os_strcmp(param
, "wpa") == 0) {
3633 os_snprintf(value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
, "%d",
3634 src_hapd
->conf
->wpa
);
3635 } else if (os_strcmp(param
, "wpa_key_mgmt") == 0 &&
3636 src_hapd
->conf
->wpa_key_mgmt
) {
3637 res
= hostapd_ctrl_iface_get_key_mgmt(
3638 src_hapd
, value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
);
3639 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN
, res
))
3640 goto error_stringify
;
3641 } else if (os_strcmp(param
, "wpa_pairwise") == 0 &&
3642 src_hapd
->conf
->wpa_pairwise
) {
3643 res
= wpa_write_ciphers(value
,
3644 value
+ HOSTAPD_CLI_DUP_VALUE_MAX_LEN
,
3645 src_hapd
->conf
->wpa_pairwise
, " ");
3647 goto error_stringify
;
3648 } else if (os_strcmp(param
, "rsn_pairwise") == 0 &&
3649 src_hapd
->conf
->rsn_pairwise
) {
3650 res
= wpa_write_ciphers(value
,
3651 value
+ HOSTAPD_CLI_DUP_VALUE_MAX_LEN
,
3652 src_hapd
->conf
->rsn_pairwise
, " ");
3654 goto error_stringify
;
3655 } else if (os_strcmp(param
, "wpa_passphrase") == 0 &&
3656 src_hapd
->conf
->ssid
.wpa_passphrase
) {
3657 os_snprintf(value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
, "%s",
3658 src_hapd
->conf
->ssid
.wpa_passphrase
);
3659 } else if (os_strcmp(param
, "wpa_psk") == 0 &&
3660 src_hapd
->conf
->ssid
.wpa_psk_set
) {
3661 wpa_snprintf_hex(value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
,
3662 src_hapd
->conf
->ssid
.wpa_psk
->psk
, PMK_LEN
);
3664 wpa_printf(MSG_WARNING
, "DUP: %s cannot be duplicated", param
);
3668 res
= hostapd_set_iface(dst_hapd
->iconf
, dst_hapd
->conf
, param
, value
);
3673 wpa_printf(MSG_ERROR
, "DUP: cannot stringify %s", param
);
3681 hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces
*interfaces
,
3683 char *reply
, int reply_size
)
3688 struct hostapd_iface
*iface
;
3692 show_ctrl
= !!os_strstr(input
, "ctrl");
3695 end
= reply
+ reply_size
;
3697 for (i
= 0; i
< interfaces
->count
; i
++) {
3698 iface
= interfaces
->iface
[i
];
3700 for (j
= 0; j
< iface
->num_bss
; j
++) {
3701 struct hostapd_bss_config
*conf
;
3703 conf
= iface
->conf
->bss
[j
];
3705 res
= os_snprintf(pos
, end
- pos
,
3706 "%s ctrl_iface=%s\n",
3708 conf
->ctrl_interface
?
3709 conf
->ctrl_interface
: "N/A");
3711 res
= os_snprintf(pos
, end
- pos
, "%s\n",
3713 if (os_snprintf_error(end
- pos
, res
)) {
3726 hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces
*interfaces
,
3729 char *p_start
= cmd
, *p_end
;
3730 struct hostapd_data
*src_hapd
, *dst_hapd
;
3732 /* cmd: "<src ifname> <dst ifname> <variable name> */
3734 p_end
= os_strchr(p_start
, ' ');
3736 wpa_printf(MSG_ERROR
, "DUP: no src ifname found in cmd: '%s'",
3742 src_hapd
= hostapd_interfaces_get_hapd(interfaces
, p_start
);
3744 wpa_printf(MSG_ERROR
, "DUP: no src ifname found: '%s'",
3749 p_start
= p_end
+ 1;
3750 p_end
= os_strchr(p_start
, ' ');
3752 wpa_printf(MSG_ERROR
, "DUP: no dst ifname found in cmd: '%s'",
3758 dst_hapd
= hostapd_interfaces_get_hapd(interfaces
, p_start
);
3760 wpa_printf(MSG_ERROR
, "DUP: no dst ifname found: '%s'",
3765 p_start
= p_end
+ 1;
3766 return hostapd_ctrl_iface_dup_param(src_hapd
, dst_hapd
, p_start
);
3770 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces
*interfaces
,
3772 char *buf
, char *reply
,
3774 struct sockaddr_storage
*from
,
3777 struct hostapd_data
*hapd
;
3779 hapd
= hostapd_interfaces_get_hapd(interfaces
, ifname
);
3783 res
= os_snprintf(reply
, reply_size
, "FAIL-NO-IFNAME-MATCH\n");
3784 if (os_snprintf_error(reply_size
, res
))
3789 return hostapd_ctrl_iface_receive_process(hapd
, buf
, reply
,reply_size
,
3794 static void hostapd_global_ctrl_iface_receive(int sock
, void *eloop_ctx
,
3797 void *interfaces
= eloop_ctx
;
3798 char buffer
[256], *buf
= buffer
;
3800 struct sockaddr_storage from
;
3801 socklen_t fromlen
= sizeof(from
);
3804 const int reply_size
= 4096;
3805 #ifdef CONFIG_CTRL_IFACE_UDP
3806 unsigned char lcookie
[COOKIE_LEN
];
3807 #endif /* CONFIG_CTRL_IFACE_UDP */
3809 res
= recvfrom(sock
, buffer
, sizeof(buffer
) - 1, 0,
3810 (struct sockaddr
*) &from
, &fromlen
);
3812 wpa_printf(MSG_ERROR
, "recvfrom(ctrl_iface): %s",
3817 wpa_printf(MSG_DEBUG
, "Global ctrl_iface command: %s", buf
);
3819 reply
= os_malloc(reply_size
);
3820 if (reply
== NULL
) {
3821 if (sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
3823 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
3829 os_memcpy(reply
, "OK\n", 3);
3832 #ifdef CONFIG_CTRL_IFACE_UDP
3833 if (os_strcmp(buf
, "GET_COOKIE") == 0) {
3834 os_memcpy(reply
, "COOKIE=", 7);
3835 wpa_snprintf_hex(reply
+ 7, 2 * COOKIE_LEN
+ 1,
3836 gcookie
, COOKIE_LEN
);
3837 reply_len
= 7 + 2 * COOKIE_LEN
;
3841 if (os_strncmp(buf
, "COOKIE=", 7) != 0 ||
3842 hexstr2bin(buf
+ 7, lcookie
, COOKIE_LEN
) < 0) {
3843 wpa_printf(MSG_DEBUG
,
3844 "CTRL: No cookie in the request - drop request");
3849 if (os_memcmp(gcookie
, lcookie
, COOKIE_LEN
) != 0) {
3850 wpa_printf(MSG_DEBUG
,
3851 "CTRL: Invalid cookie in the request - drop request");
3856 buf
+= 7 + 2 * COOKIE_LEN
;
3859 #endif /* CONFIG_CTRL_IFACE_UDP */
3861 if (os_strncmp(buf
, "IFNAME=", 7) == 0) {
3862 char *pos
= os_strchr(buf
+ 7, ' ');
3866 reply_len
= hostapd_global_ctrl_iface_ifname(
3867 interfaces
, buf
+ 7, pos
, reply
, reply_size
,
3873 if (os_strcmp(buf
, "PING") == 0) {
3874 os_memcpy(reply
, "PONG\n", 5);
3876 } else if (os_strncmp(buf
, "RELOG", 5) == 0) {
3877 if (wpa_debug_reopen_file() < 0)
3879 } else if (os_strcmp(buf
, "FLUSH") == 0) {
3880 hostapd_ctrl_iface_flush(interfaces
);
3881 } else if (os_strncmp(buf
, "ADD ", 4) == 0) {
3882 if (hostapd_ctrl_iface_add(interfaces
, buf
+ 4) < 0)
3884 } else if (os_strncmp(buf
, "REMOVE ", 7) == 0) {
3885 if (hostapd_ctrl_iface_remove(interfaces
, buf
+ 7) < 0)
3887 } else if (os_strcmp(buf
, "ATTACH") == 0) {
3888 if (hostapd_global_ctrl_iface_attach(interfaces
, &from
,
3891 } else if (os_strncmp(buf
, "ATTACH ", 7) == 0) {
3892 if (hostapd_global_ctrl_iface_attach(interfaces
, &from
,
3895 } else if (os_strcmp(buf
, "DETACH") == 0) {
3896 if (hostapd_global_ctrl_iface_detach(interfaces
, &from
,
3899 #ifdef CONFIG_MODULE_TESTS
3900 } else if (os_strcmp(buf
, "MODULE_TESTS") == 0) {
3901 if (hapd_module_tests() < 0)
3903 #endif /* CONFIG_MODULE_TESTS */
3905 } else if (os_strncmp(buf
, "FST-ATTACH ", 11) == 0) {
3906 if (!hostapd_global_ctrl_iface_fst_attach(interfaces
, buf
+ 11))
3907 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
3910 } else if (os_strncmp(buf
, "FST-DETACH ", 11) == 0) {
3911 if (!hostapd_global_ctrl_iface_fst_detach(interfaces
, buf
+ 11))
3912 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
3915 } else if (os_strncmp(buf
, "FST-MANAGER ", 12) == 0) {
3916 reply_len
= fst_ctrl_iface_receive(buf
+ 12, reply
, reply_size
);
3917 #endif /* CONFIG_FST */
3918 } else if (os_strncmp(buf
, "DUP_NETWORK ", 12) == 0) {
3919 if (!hostapd_global_ctrl_iface_dup_network(interfaces
,
3921 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
3924 } else if (os_strncmp(buf
, "INTERFACES", 10) == 0) {
3925 reply_len
= hostapd_global_ctrl_iface_interfaces(
3926 interfaces
, buf
+ 10, reply
, sizeof(buffer
));
3927 } else if (os_strcmp(buf
, "TERMINATE") == 0) {
3930 wpa_printf(MSG_DEBUG
, "Unrecognized global ctrl_iface command "
3936 if (reply_len
< 0) {
3937 os_memcpy(reply
, "FAIL\n", 5);
3941 if (sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
3943 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
3950 #ifndef CONFIG_CTRL_IFACE_UDP
3951 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces
*interface
)
3956 if (interface
->global_iface_path
== NULL
)
3959 len
= os_strlen(interface
->global_iface_path
) +
3960 os_strlen(interface
->global_iface_name
) + 2;
3961 buf
= os_malloc(len
);
3965 os_snprintf(buf
, len
, "%s/%s", interface
->global_iface_path
,
3966 interface
->global_iface_name
);
3967 buf
[len
- 1] = '\0';
3970 #endif /* CONFIG_CTRL_IFACE_UDP */
3973 int hostapd_global_ctrl_iface_init(struct hapd_interfaces
*interface
)
3975 #ifdef CONFIG_CTRL_IFACE_UDP
3976 int port
= HOSTAPD_GLOBAL_CTRL_IFACE_PORT
;
3979 struct addrinfo hints
= { 0 }, *res
, *saveres
;
3982 if (interface
->global_ctrl_sock
> -1) {
3983 wpa_printf(MSG_DEBUG
, "ctrl_iface already exists!");
3987 if (interface
->global_iface_path
== NULL
)
3990 pos
= os_strstr(interface
->global_iface_path
, "udp:");
3995 wpa_printf(MSG_ERROR
, "Invalid global ctrl UDP port");
4000 os_get_random(gcookie
, COOKIE_LEN
);
4002 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4003 hints
.ai_flags
= AI_PASSIVE
;
4004 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4006 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4007 hints
.ai_family
= AF_INET6
;
4008 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4009 hints
.ai_family
= AF_INET
;
4010 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4011 hints
.ai_socktype
= SOCK_DGRAM
;
4014 os_snprintf(p
, sizeof(p
), "%d", port
);
4015 n
= getaddrinfo(NULL
, p
, &hints
, &res
);
4017 wpa_printf(MSG_ERROR
, "getaddrinfo(): %s", gai_strerror(n
));
4022 interface
->global_ctrl_sock
= socket(res
->ai_family
, res
->ai_socktype
,
4024 if (interface
->global_ctrl_sock
< 0) {
4025 wpa_printf(MSG_ERROR
, "socket(PF_INET): %s", strerror(errno
));
4029 if (bind(interface
->global_ctrl_sock
, res
->ai_addr
, res
->ai_addrlen
) <
4032 if ((port
- HOSTAPD_GLOBAL_CTRL_IFACE_PORT
) <
4033 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT
&& !pos
)
4035 wpa_printf(MSG_ERROR
, "bind(AF_INET): %s", strerror(errno
));
4039 freeaddrinfo(saveres
);
4041 wpa_printf(MSG_DEBUG
, "global ctrl_iface_init UDP port: %d", port
);
4043 if (eloop_register_read_sock(interface
->global_ctrl_sock
,
4044 hostapd_global_ctrl_iface_receive
,
4045 interface
, NULL
) < 0) {
4046 hostapd_global_ctrl_iface_deinit(interface
);
4053 if (interface
->global_ctrl_sock
>= 0)
4054 close(interface
->global_ctrl_sock
);
4056 #else /* CONFIG_CTRL_IFACE_UDP */
4057 struct sockaddr_un addr
;
4061 if (interface
->global_iface_path
== NULL
) {
4062 wpa_printf(MSG_DEBUG
, "ctrl_iface not configured!");
4066 if (mkdir(interface
->global_iface_path
, S_IRWXU
| S_IRWXG
) < 0) {
4067 if (errno
== EEXIST
) {
4068 wpa_printf(MSG_DEBUG
, "Using existing control "
4069 "interface directory.");
4071 wpa_printf(MSG_ERROR
, "mkdir[ctrl_interface]: %s",
4075 } else if (interface
->ctrl_iface_group
&&
4076 chown(interface
->global_iface_path
, -1,
4077 interface
->ctrl_iface_group
) < 0) {
4078 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
4083 if (os_strlen(interface
->global_iface_path
) + 1 +
4084 os_strlen(interface
->global_iface_name
) >= sizeof(addr
.sun_path
))
4087 s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
4089 wpa_printf(MSG_ERROR
, "socket(PF_UNIX): %s", strerror(errno
));
4093 os_memset(&addr
, 0, sizeof(addr
));
4095 addr
.sun_len
= sizeof(addr
);
4096 #endif /* __FreeBSD__ */
4097 addr
.sun_family
= AF_UNIX
;
4098 fname
= hostapd_global_ctrl_iface_path(interface
);
4101 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
4102 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
4103 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
4105 if (connect(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
4106 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
4107 " allow connections - assuming it was left"
4108 "over from forced program termination");
4109 if (unlink(fname
) < 0) {
4110 wpa_printf(MSG_ERROR
,
4111 "Could not unlink existing ctrl_iface socket '%s': %s",
4112 fname
, strerror(errno
));
4115 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) <
4117 wpa_printf(MSG_ERROR
, "bind(PF_UNIX): %s",
4121 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
4122 "ctrl_iface socket '%s'", fname
);
4124 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
4125 "be in use - cannot override it");
4126 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
4127 "not used anymore", fname
);
4134 if (interface
->ctrl_iface_group
&&
4135 chown(fname
, -1, interface
->ctrl_iface_group
) < 0) {
4136 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
4141 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
4142 wpa_printf(MSG_ERROR
, "chmod[ctrl_interface/ifname]: %s",
4148 interface
->global_ctrl_sock
= s
;
4149 eloop_register_read_sock(s
, hostapd_global_ctrl_iface_receive
,
4162 #endif /* CONFIG_CTRL_IFACE_UDP */
4166 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces
*interfaces
)
4168 #ifndef CONFIG_CTRL_IFACE_UDP
4170 #endif /* CONFIG_CTRL_IFACE_UDP */
4171 struct wpa_ctrl_dst
*dst
, *prev
;
4173 if (interfaces
->global_ctrl_sock
> -1) {
4174 eloop_unregister_read_sock(interfaces
->global_ctrl_sock
);
4175 close(interfaces
->global_ctrl_sock
);
4176 interfaces
->global_ctrl_sock
= -1;
4177 #ifndef CONFIG_CTRL_IFACE_UDP
4178 fname
= hostapd_global_ctrl_iface_path(interfaces
);
4184 if (interfaces
->global_iface_path
&&
4185 rmdir(interfaces
->global_iface_path
) < 0) {
4186 if (errno
== ENOTEMPTY
) {
4187 wpa_printf(MSG_DEBUG
, "Control interface "
4188 "directory not empty - leaving it "
4191 wpa_printf(MSG_ERROR
,
4192 "rmdir[ctrl_interface=%s]: %s",
4193 interfaces
->global_iface_path
,
4197 #endif /* CONFIG_CTRL_IFACE_UDP */
4200 os_free(interfaces
->global_iface_path
);
4201 interfaces
->global_iface_path
= NULL
;
4203 dl_list_for_each_safe(dst
, prev
, &interfaces
->global_ctrl_dst
,
4204 struct wpa_ctrl_dst
, list
)
4209 static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst
*dst
,
4212 /* Enable Probe Request events based on explicit request.
4213 * Other events are enabled by default.
4215 if (str_starts(buf
, RX_PROBE_REQUEST
))
4216 return !!(dst
->events
& WPA_EVENT_RX_PROBE_REQUEST
);
4221 static void hostapd_ctrl_iface_send(struct hostapd_data
*hapd
, int level
,
4222 enum wpa_msg_type type
,
4223 const char *buf
, size_t len
)
4225 struct wpa_ctrl_dst
*dst
, *next
;
4226 struct dl_list
*ctrl_dst
;
4233 if (type
!= WPA_MSG_ONLY_GLOBAL
) {
4234 s
= hapd
->ctrl_sock
;
4235 ctrl_dst
= &hapd
->ctrl_dst
;
4237 s
= hapd
->iface
->interfaces
->global_ctrl_sock
;
4238 ctrl_dst
= &hapd
->iface
->interfaces
->global_ctrl_dst
;
4241 if (s
< 0 || dl_list_empty(ctrl_dst
))
4244 os_snprintf(levelstr
, sizeof(levelstr
), "<%d>", level
);
4245 io
[0].iov_base
= levelstr
;
4246 io
[0].iov_len
= os_strlen(levelstr
);
4247 io
[1].iov_base
= (char *) buf
;
4248 io
[1].iov_len
= len
;
4249 os_memset(&msg
, 0, sizeof(msg
));
4254 dl_list_for_each_safe(dst
, next
, ctrl_dst
, struct wpa_ctrl_dst
, list
) {
4255 if ((level
>= dst
->debug_level
) &&
4256 hostapd_ctrl_check_event_enabled(dst
, buf
)) {
4257 sockaddr_print(MSG_DEBUG
, "CTRL_IFACE monitor send",
4258 &dst
->addr
, dst
->addrlen
);
4259 msg
.msg_name
= &dst
->addr
;
4260 msg
.msg_namelen
= dst
->addrlen
;
4261 if (sendmsg(s
, &msg
, 0) < 0) {
4263 wpa_printf(MSG_INFO
, "CTRL_IFACE monitor[%d]: "
4265 idx
, errno
, strerror(errno
));
4267 if (dst
->errors
> 10 || _errno
== ENOENT
) {
4268 if (type
!= WPA_MSG_ONLY_GLOBAL
)
4269 hostapd_ctrl_iface_detach(
4273 hostapd_global_ctrl_iface_detach(
4274 hapd
->iface
->interfaces
,
4285 #endif /* CONFIG_NATIVE_WINDOWS */