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 "crypto/tls.h"
34 #include "drivers/driver.h"
35 #include "eapol_auth/eapol_auth_sm.h"
36 #include "radius/radius_client.h"
37 #include "radius/radius_server.h"
38 #include "l2_packet/l2_packet.h"
39 #include "ap/hostapd.h"
40 #include "ap/ap_config.h"
41 #include "ap/ieee802_1x.h"
42 #include "ap/wpa_auth.h"
43 #include "ap/ieee802_11.h"
44 #include "ap/sta_info.h"
45 #include "ap/wps_hostapd.h"
46 #include "ap/ctrl_iface_ap.h"
47 #include "ap/ap_drv_ops.h"
49 #include "ap/wnm_ap.h"
50 #include "ap/wpa_auth.h"
51 #include "ap/beacon.h"
52 #include "ap/neighbor_db.h"
54 #include "ap/dpp_hostapd.h"
55 #include "wps/wps_defs.h"
57 #include "fst/fst_ctrl_iface.h"
58 #include "config_file.h"
59 #include "ctrl_iface.h"
62 #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
64 #ifdef CONFIG_CTRL_IFACE_UDP
66 static unsigned char cookie
[COOKIE_LEN
];
67 static unsigned char gcookie
[COOKIE_LEN
];
68 #define HOSTAPD_CTRL_IFACE_PORT 8877
69 #define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
70 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
71 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
72 #endif /* CONFIG_CTRL_IFACE_UDP */
74 static void hostapd_ctrl_iface_send(struct hostapd_data
*hapd
, int level
,
75 enum wpa_msg_type type
,
76 const char *buf
, size_t len
);
79 static int hostapd_ctrl_iface_attach(struct hostapd_data
*hapd
,
80 struct sockaddr_storage
*from
,
83 return ctrl_iface_attach(&hapd
->ctrl_dst
, from
, fromlen
);
87 static int hostapd_ctrl_iface_detach(struct hostapd_data
*hapd
,
88 struct sockaddr_storage
*from
,
91 return ctrl_iface_detach(&hapd
->ctrl_dst
, from
, fromlen
);
95 static int hostapd_ctrl_iface_level(struct hostapd_data
*hapd
,
96 struct sockaddr_storage
*from
,
100 return ctrl_iface_level(&hapd
->ctrl_dst
, from
, fromlen
, level
);
104 static int hostapd_ctrl_iface_new_sta(struct hostapd_data
*hapd
,
108 struct sta_info
*sta
;
110 wpa_printf(MSG_DEBUG
, "CTRL_IFACE NEW_STA %s", txtaddr
);
112 if (hwaddr_aton(txtaddr
, addr
))
115 sta
= ap_get_sta(hapd
, addr
);
119 wpa_printf(MSG_DEBUG
, "Add new STA " MACSTR
" based on ctrl_iface "
120 "notification", MAC2STR(addr
));
121 sta
= ap_sta_add(hapd
, addr
);
125 hostapd_new_assoc_sta(hapd
, sta
, 0);
130 #ifdef CONFIG_IEEE80211W
132 static int hostapd_ctrl_iface_sa_query(struct hostapd_data
*hapd
,
136 u8 trans_id
[WLAN_SA_QUERY_TR_ID_LEN
];
138 wpa_printf(MSG_DEBUG
, "CTRL_IFACE SA_QUERY %s", txtaddr
);
140 if (hwaddr_aton(txtaddr
, addr
) ||
141 os_get_random(trans_id
, WLAN_SA_QUERY_TR_ID_LEN
) < 0)
144 ieee802_11_send_sa_query_req(hapd
, addr
, trans_id
);
148 #endif /* NEED_AP_MLME */
149 #endif /* CONFIG_IEEE80211W */
153 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data
*hapd
, char *txt
)
155 char *pin
= os_strchr(txt
, ' ');
158 u8 addr_buf
[ETH_ALEN
], *addr
= NULL
;
165 timeout_txt
= os_strchr(pin
, ' ');
167 *timeout_txt
++ = '\0';
168 timeout
= atoi(timeout_txt
);
169 pos
= os_strchr(timeout_txt
, ' ');
172 if (hwaddr_aton(pos
, addr_buf
) == 0)
178 return hostapd_wps_add_pin(hapd
, addr
, txt
, pin
, timeout
);
182 static int hostapd_ctrl_iface_wps_check_pin(
183 struct hostapd_data
*hapd
, char *cmd
, char *buf
, size_t buflen
)
190 wpa_hexdump_ascii_key(MSG_DEBUG
, "WPS_CHECK_PIN",
191 (u8
*) cmd
, os_strlen(cmd
));
192 for (pos
= cmd
, len
= 0; *pos
!= '\0'; pos
++) {
193 if (*pos
< '0' || *pos
> '9')
197 wpa_printf(MSG_DEBUG
, "WPS: Too long PIN");
201 if (len
!= 4 && len
!= 8) {
202 wpa_printf(MSG_DEBUG
, "WPS: Invalid PIN length %d", (int) len
);
208 unsigned int pin_val
;
210 if (!wps_pin_valid(pin_val
)) {
211 wpa_printf(MSG_DEBUG
, "WPS: Invalid checksum digit");
212 ret
= os_snprintf(buf
, buflen
, "FAIL-CHECKSUM\n");
213 if (os_snprintf_error(buflen
, ret
))
219 ret
= os_snprintf(buf
, buflen
, "%s", pin
);
220 if (os_snprintf_error(buflen
, ret
))
227 #ifdef CONFIG_WPS_NFC
228 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data
*hapd
,
235 len
= os_strlen(pos
);
240 buf
= wpabuf_alloc(len
);
243 if (hexstr2bin(pos
, wpabuf_put(buf
, len
), len
) < 0) {
248 ret
= hostapd_wps_nfc_tag_read(hapd
, buf
);
255 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data
*hapd
,
256 char *cmd
, char *reply
,
263 if (os_strcmp(cmd
, "WPS") == 0)
265 else if (os_strcmp(cmd
, "NDEF") == 0)
270 buf
= hostapd_wps_nfc_config_token(hapd
, ndef
);
274 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
285 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data
*hapd
,
286 char *reply
, size_t max_len
,
292 buf
= hostapd_wps_nfc_token_gen(hapd
, ndef
);
296 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
307 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data
*hapd
,
308 char *cmd
, char *reply
,
311 if (os_strcmp(cmd
, "WPS") == 0)
312 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd
, reply
,
315 if (os_strcmp(cmd
, "NDEF") == 0)
316 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd
, reply
,
319 if (os_strcmp(cmd
, "enable") == 0)
320 return hostapd_wps_nfc_token_enable(hapd
);
322 if (os_strcmp(cmd
, "disable") == 0) {
323 hostapd_wps_nfc_token_disable(hapd
);
331 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data
*hapd
,
332 char *cmd
, char *reply
,
340 pos
= os_strchr(cmd
, ' ');
345 if (os_strcmp(cmd
, "WPS") == 0)
347 else if (os_strcmp(cmd
, "NDEF") == 0)
352 if (os_strcmp(pos
, "WPS-CR") == 0)
353 buf
= hostapd_wps_nfc_hs_cr(hapd
, ndef
);
359 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
370 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data
*hapd
,
374 struct wpabuf
*req
, *sel
;
376 char *pos
, *role
, *type
, *pos2
;
379 pos
= os_strchr(role
, ' ');
385 pos
= os_strchr(type
, ' ');
390 pos2
= os_strchr(pos
, ' ');
395 len
= os_strlen(pos
);
400 req
= wpabuf_alloc(len
);
403 if (hexstr2bin(pos
, wpabuf_put(req
, len
), len
) < 0) {
408 len
= os_strlen(pos2
);
415 sel
= wpabuf_alloc(len
);
420 if (hexstr2bin(pos2
, wpabuf_put(sel
, len
), len
) < 0) {
426 if (os_strcmp(role
, "RESP") == 0 && os_strcmp(type
, "WPS") == 0) {
427 ret
= hostapd_wps_nfc_report_handover(hapd
, req
, sel
);
429 wpa_printf(MSG_DEBUG
, "NFC: Unsupported connection handover "
430 "reported: role=%s type=%s", role
, type
);
439 #endif /* CONFIG_WPS_NFC */
442 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data
*hapd
, char *txt
,
443 char *buf
, size_t buflen
)
449 pos
= os_strchr(txt
, ' ');
453 if (os_strcmp(txt
, "disable") == 0) {
454 hostapd_wps_ap_pin_disable(hapd
);
455 return os_snprintf(buf
, buflen
, "OK\n");
458 if (os_strcmp(txt
, "random") == 0) {
461 pin_txt
= hostapd_wps_ap_pin_random(hapd
, timeout
);
464 return os_snprintf(buf
, buflen
, "%s", pin_txt
);
467 if (os_strcmp(txt
, "get") == 0) {
468 pin_txt
= hostapd_wps_ap_pin_get(hapd
);
471 return os_snprintf(buf
, buflen
, "%s", pin_txt
);
474 if (os_strcmp(txt
, "set") == 0) {
479 pos
= os_strchr(pos
, ' ');
484 if (os_strlen(pin
) > buflen
)
486 if (hostapd_wps_ap_pin_set(hapd
, pin
, timeout
) < 0)
488 return os_snprintf(buf
, buflen
, "%s", pin
);
495 static int hostapd_ctrl_iface_wps_config(struct hostapd_data
*hapd
, char *txt
)
498 char *ssid
, *auth
, *encr
= NULL
, *key
= NULL
;
501 pos
= os_strchr(txt
, ' ');
507 pos
= os_strchr(pos
, ' ');
511 pos
= os_strchr(pos
, ' ');
518 return hostapd_wps_config_ap(hapd
, ssid
, auth
, encr
, key
);
522 static const char * pbc_status_str(enum pbc_status status
)
525 case WPS_PBC_STATUS_DISABLE
:
527 case WPS_PBC_STATUS_ACTIVE
:
529 case WPS_PBC_STATUS_TIMEOUT
:
531 case WPS_PBC_STATUS_OVERLAP
:
539 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data
*hapd
,
540 char *buf
, size_t buflen
)
548 ret
= os_snprintf(pos
, end
- pos
, "PBC Status: %s\n",
549 pbc_status_str(hapd
->wps_stats
.pbc_status
));
551 if (os_snprintf_error(end
- pos
, ret
))
555 ret
= os_snprintf(pos
, end
- pos
, "Last WPS result: %s\n",
556 (hapd
->wps_stats
.status
== WPS_STATUS_SUCCESS
?
558 (hapd
->wps_stats
.status
== WPS_STATUS_FAILURE
?
559 "Failed" : "None")));
561 if (os_snprintf_error(end
- pos
, ret
))
565 /* If status == Failure - Add possible Reasons */
566 if(hapd
->wps_stats
.status
== WPS_STATUS_FAILURE
&&
567 hapd
->wps_stats
.failure_reason
> 0) {
568 ret
= os_snprintf(pos
, end
- pos
,
569 "Failure Reason: %s\n",
570 wps_ei_str(hapd
->wps_stats
.failure_reason
));
572 if (os_snprintf_error(end
- pos
, ret
))
577 if (hapd
->wps_stats
.status
) {
578 ret
= os_snprintf(pos
, end
- pos
, "Peer Address: " MACSTR
"\n",
579 MAC2STR(hapd
->wps_stats
.peer_addr
));
581 if (os_snprintf_error(end
- pos
, ret
))
589 #endif /* CONFIG_WPS */
593 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data
*hapd
,
599 if (hwaddr_aton(cmd
, addr
))
612 return hs20_send_wnm_notification(hapd
, addr
, 1, url
);
616 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data
*hapd
,
620 int code
, reauth_delay
, ret
;
625 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
626 if (hwaddr_aton(cmd
, addr
))
629 pos
= os_strchr(cmd
, ' ');
635 pos
= os_strchr(pos
, ' ');
639 reauth_delay
= atoi(pos
);
642 pos
= os_strchr(pos
, ' ');
645 url_len
= os_strlen(pos
);
648 req
= wpabuf_alloc(4 + url_len
);
651 wpabuf_put_u8(req
, code
);
652 wpabuf_put_le16(req
, reauth_delay
);
653 wpabuf_put_u8(req
, url_len
);
655 wpabuf_put_data(req
, pos
, url_len
);
657 wpa_printf(MSG_DEBUG
, "HS 2.0: Send WNM-Notification to " MACSTR
658 " to indicate imminent deauthentication (code=%d "
659 "reauth_delay=%d)", MAC2STR(addr
), code
, reauth_delay
);
660 ret
= hs20_send_wnm_notification_deauth_req(hapd
, addr
, req
);
665 #endif /* CONFIG_HS20 */
668 #ifdef CONFIG_INTERWORKING
670 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data
*hapd
,
673 u8 qos_map_set
[16 + 2 * 21], count
= 0;
674 const char *pos
= cmd
;
678 if (count
== sizeof(qos_map_set
)) {
679 wpa_printf(MSG_ERROR
, "Too many qos_map_set parameters");
684 if (val
< 0 || val
> 255) {
685 wpa_printf(MSG_INFO
, "Invalid QoS Map Set");
689 qos_map_set
[count
++] = val
;
690 pos
= os_strchr(pos
, ',');
696 if (count
< 16 || count
& 1) {
697 wpa_printf(MSG_INFO
, "Invalid QoS Map Set");
701 ret
= hostapd_drv_set_qos_map(hapd
, qos_map_set
, count
);
703 wpa_printf(MSG_INFO
, "Failed to set QoS Map Set");
707 os_memcpy(hapd
->conf
->qos_map_set
, qos_map_set
, count
);
708 hapd
->conf
->qos_map_set_len
= count
;
714 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data
*hapd
,
718 struct sta_info
*sta
;
720 u8
*qos_map_set
= hapd
->conf
->qos_map_set
;
721 u8 qos_map_set_len
= hapd
->conf
->qos_map_set_len
;
724 if (!qos_map_set_len
) {
725 wpa_printf(MSG_INFO
, "QoS Map Set is not set");
729 if (hwaddr_aton(cmd
, addr
))
732 sta
= ap_get_sta(hapd
, addr
);
734 wpa_printf(MSG_DEBUG
, "Station " MACSTR
" not found "
735 "for QoS Map Configuration message",
740 if (!sta
->qos_map_enabled
) {
741 wpa_printf(MSG_DEBUG
, "Station " MACSTR
" did not indicate "
742 "support for QoS Map", MAC2STR(addr
));
746 buf
= wpabuf_alloc(2 + 2 + qos_map_set_len
);
750 wpabuf_put_u8(buf
, WLAN_ACTION_QOS
);
751 wpabuf_put_u8(buf
, QOS_QOS_MAP_CONFIG
);
753 /* QoS Map Set Element */
754 wpabuf_put_u8(buf
, WLAN_EID_QOS_MAP_SET
);
755 wpabuf_put_u8(buf
, qos_map_set_len
);
756 wpabuf_put_data(buf
, qos_map_set
, qos_map_set_len
);
758 ret
= hostapd_drv_send_action(hapd
, hapd
->iface
->freq
, 0, addr
,
759 wpabuf_head(buf
), wpabuf_len(buf
));
765 #endif /* CONFIG_INTERWORKING */
770 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data
*hapd
,
775 struct sta_info
*sta
;
777 if (hwaddr_aton(cmd
, addr
))
781 disassoc_timer
= atoi(cmd
+ 17);
783 sta
= ap_get_sta(hapd
, addr
);
785 wpa_printf(MSG_DEBUG
, "Station " MACSTR
786 " not found for disassociation imminent message",
791 return wnm_send_disassoc_imminent(hapd
, sta
, disassoc_timer
);
795 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data
*hapd
,
799 const char *url
, *timerstr
;
801 struct sta_info
*sta
;
803 if (hwaddr_aton(cmd
, addr
))
806 sta
= ap_get_sta(hapd
, addr
);
808 wpa_printf(MSG_DEBUG
, "Station " MACSTR
809 " not found for ESS disassociation imminent message",
815 if (*timerstr
!= ' ')
818 disassoc_timer
= atoi(timerstr
);
819 if (disassoc_timer
< 0 || disassoc_timer
> 65535)
822 url
= os_strchr(timerstr
, ' ');
827 return wnm_send_ess_disassoc_imminent(hapd
, sta
, url
, disassoc_timer
);
831 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data
*hapd
,
835 const char *pos
, *end
;
836 int disassoc_timer
= 0;
837 struct sta_info
*sta
;
838 u8 req_mode
= 0, valid_int
= 0x01;
847 if (hwaddr_aton(cmd
, addr
)) {
848 wpa_printf(MSG_DEBUG
, "Invalid STA MAC address");
852 sta
= ap_get_sta(hapd
, addr
);
854 wpa_printf(MSG_DEBUG
, "Station " MACSTR
855 " not found for BSS TM Request message",
860 pos
= os_strstr(cmd
, " disassoc_timer=");
863 disassoc_timer
= atoi(pos
);
864 if (disassoc_timer
< 0 || disassoc_timer
> 65535) {
865 wpa_printf(MSG_DEBUG
, "Invalid disassoc_timer");
870 pos
= os_strstr(cmd
, " valid_int=");
873 valid_int
= atoi(pos
);
876 pos
= os_strstr(cmd
, " bss_term=");
879 req_mode
|= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED
;
880 /* TODO: TSF configurable/learnable */
881 bss_term_dur
[0] = 4; /* Subelement ID */
882 bss_term_dur
[1] = 10; /* Length */
883 os_memset(bss_term_dur
, 2, 8);
884 end
= os_strchr(pos
, ',');
886 wpa_printf(MSG_DEBUG
, "Invalid bss_term data");
890 WPA_PUT_LE16(&bss_term_dur
[10], atoi(end
));
893 nei_len
= ieee802_11_parse_candidate_list(cmd
, nei_rep
,
898 pos
= os_strstr(cmd
, " url=");
902 end
= os_strchr(pos
, ' ');
906 len
= os_strlen(pos
);
907 url
= os_malloc(len
+ 1);
910 os_memcpy(url
, pos
, len
);
912 req_mode
|= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT
;
915 if (os_strstr(cmd
, " pref=1"))
916 req_mode
|= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED
;
917 if (os_strstr(cmd
, " abridged=1"))
918 req_mode
|= WNM_BSS_TM_REQ_ABRIDGED
;
919 if (os_strstr(cmd
, " disassoc_imminent=1"))
920 req_mode
|= WNM_BSS_TM_REQ_DISASSOC_IMMINENT
;
923 pos
= os_strstr(cmd
, "mbo=");
925 unsigned int mbo_reason
, cell_pref
, reassoc_delay
;
928 ret
= sscanf(pos
, "mbo=%u:%u:%u", &mbo_reason
,
929 &reassoc_delay
, &cell_pref
);
931 wpa_printf(MSG_DEBUG
,
932 "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
937 if (mbo_reason
> MBO_TRANSITION_REASON_PREMIUM_AP
) {
938 wpa_printf(MSG_DEBUG
,
939 "Invalid MBO transition reason code %u",
945 /* Valid values for Cellular preference are: 0, 1, 255 */
946 if (cell_pref
!= 0 && cell_pref
!= 1 && cell_pref
!= 255) {
947 wpa_printf(MSG_DEBUG
,
948 "Invalid MBO cellular capability %u",
954 if (reassoc_delay
> 65535 ||
956 !(req_mode
& WNM_BSS_TM_REQ_DISASSOC_IMMINENT
))) {
957 wpa_printf(MSG_DEBUG
,
958 "MBO: Assoc retry delay is only valid in disassoc imminent mode");
963 *mbo_pos
++ = MBO_ATTR_ID_TRANSITION_REASON
;
965 *mbo_pos
++ = mbo_reason
;
966 *mbo_pos
++ = MBO_ATTR_ID_CELL_DATA_PREF
;
968 *mbo_pos
++ = cell_pref
;
971 *mbo_pos
++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY
;
973 WPA_PUT_LE16(mbo_pos
, reassoc_delay
);
977 mbo_len
= mbo_pos
- mbo
;
979 #endif /* CONFIG_MBO */
981 ret
= wnm_send_bss_tm_req(hapd
, sta
, req_mode
, disassoc_timer
,
982 valid_int
, bss_term_dur
, url
,
983 nei_len
? nei_rep
: NULL
, nei_len
,
984 mbo_len
? mbo
: NULL
, mbo_len
);
987 #endif /* CONFIG_MBO */
992 #endif /* CONFIG_WNM_AP */
995 static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data
*hapd
,
996 char *buf
, size_t buflen
)
1004 WPA_ASSERT(hapd
->conf
->wpa_key_mgmt
);
1006 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_PSK
) {
1007 ret
= os_snprintf(pos
, end
- pos
, "WPA-PSK ");
1008 if (os_snprintf_error(end
- pos
, ret
))
1012 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X
) {
1013 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP ");
1014 if (os_snprintf_error(end
- pos
, ret
))
1018 #ifdef CONFIG_IEEE80211R_AP
1019 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_PSK
) {
1020 ret
= os_snprintf(pos
, end
- pos
, "FT-PSK ");
1021 if (os_snprintf_error(end
- pos
, ret
))
1025 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_IEEE8021X
) {
1026 ret
= os_snprintf(pos
, end
- pos
, "FT-EAP ");
1027 if (os_snprintf_error(end
- pos
, ret
))
1032 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_SAE
) {
1033 ret
= os_snprintf(pos
, end
- pos
, "FT-SAE ");
1034 if (os_snprintf_error(end
- pos
, ret
))
1038 #endif /* CONFIG_SAE */
1040 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_FILS_SHA256
) {
1041 ret
= os_snprintf(pos
, end
- pos
, "FT-FILS-SHA256 ");
1042 if (os_snprintf_error(end
- pos
, ret
))
1046 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_FILS_SHA384
) {
1047 ret
= os_snprintf(pos
, end
- pos
, "FT-FILS-SHA384 ");
1048 if (os_snprintf_error(end
- pos
, ret
))
1052 #endif /* CONFIG_FILS */
1053 #endif /* CONFIG_IEEE80211R_AP */
1054 #ifdef CONFIG_IEEE80211W
1055 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_PSK_SHA256
) {
1056 ret
= os_snprintf(pos
, end
- pos
, "WPA-PSK-SHA256 ");
1057 if (os_snprintf_error(end
- pos
, ret
))
1061 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X_SHA256
) {
1062 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP-SHA256 ");
1063 if (os_snprintf_error(end
- pos
, ret
))
1067 #endif /* CONFIG_IEEE80211W */
1069 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_SAE
) {
1070 ret
= os_snprintf(pos
, end
- pos
, "SAE ");
1071 if (os_snprintf_error(end
- pos
, ret
))
1075 #endif /* CONFIG_SAE */
1076 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X_SUITE_B
) {
1077 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP-SUITE-B ");
1078 if (os_snprintf_error(end
- pos
, ret
))
1082 if (hapd
->conf
->wpa_key_mgmt
&
1083 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192
) {
1084 ret
= os_snprintf(pos
, end
- pos
,
1085 "WPA-EAP-SUITE-B-192 ");
1086 if (os_snprintf_error(end
- pos
, ret
))
1091 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FILS_SHA256
) {
1092 ret
= os_snprintf(pos
, end
- pos
, "FILS-SHA256 ");
1093 if (os_snprintf_error(end
- pos
, ret
))
1097 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FILS_SHA384
) {
1098 ret
= os_snprintf(pos
, end
- pos
, "FILS-SHA384 ");
1099 if (os_snprintf_error(end
- pos
, ret
))
1103 #endif /* CONFIG_FILS */
1106 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_OWE
) {
1107 ret
= os_snprintf(pos
, end
- pos
, "OWE ");
1108 if (os_snprintf_error(end
- pos
, ret
))
1112 #endif /* CONFIG_OWE */
1115 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_DPP
) {
1116 ret
= os_snprintf(pos
, end
- pos
, "DPP ");
1117 if (os_snprintf_error(end
- pos
, ret
))
1121 #endif /* CONFIG_DPP */
1123 if (pos
> buf
&& *(pos
- 1) == ' ') {
1132 static int hostapd_ctrl_iface_get_config(struct hostapd_data
*hapd
,
1133 char *buf
, size_t buflen
)
1141 ret
= os_snprintf(pos
, end
- pos
, "bssid=" MACSTR
"\n"
1143 MAC2STR(hapd
->own_addr
),
1144 wpa_ssid_txt(hapd
->conf
->ssid
.ssid
,
1145 hapd
->conf
->ssid
.ssid_len
));
1146 if (os_snprintf_error(end
- pos
, ret
))
1151 ret
= os_snprintf(pos
, end
- pos
, "wps_state=%s\n",
1152 hapd
->conf
->wps_state
== 0 ? "disabled" :
1153 (hapd
->conf
->wps_state
== 1 ? "not configured" :
1155 if (os_snprintf_error(end
- pos
, ret
))
1159 if (hapd
->conf
->wps_state
&& hapd
->conf
->wpa
&&
1160 hapd
->conf
->ssid
.wpa_passphrase
) {
1161 ret
= os_snprintf(pos
, end
- pos
, "passphrase=%s\n",
1162 hapd
->conf
->ssid
.wpa_passphrase
);
1163 if (os_snprintf_error(end
- pos
, ret
))
1168 if (hapd
->conf
->wps_state
&& hapd
->conf
->wpa
&&
1169 hapd
->conf
->ssid
.wpa_psk
&&
1170 hapd
->conf
->ssid
.wpa_psk
->group
) {
1171 char hex
[PMK_LEN
* 2 + 1];
1172 wpa_snprintf_hex(hex
, sizeof(hex
),
1173 hapd
->conf
->ssid
.wpa_psk
->psk
, PMK_LEN
);
1174 ret
= os_snprintf(pos
, end
- pos
, "psk=%s\n", hex
);
1175 if (os_snprintf_error(end
- pos
, ret
))
1179 #endif /* CONFIG_WPS */
1181 if (hapd
->conf
->wpa
) {
1182 ret
= os_snprintf(pos
, end
- pos
, "wpa=%d\n", hapd
->conf
->wpa
);
1183 if (os_snprintf_error(end
- pos
, ret
))
1188 if (hapd
->conf
->wpa
&& hapd
->conf
->wpa_key_mgmt
) {
1189 ret
= os_snprintf(pos
, end
- pos
, "key_mgmt=");
1190 if (os_snprintf_error(end
- pos
, ret
))
1194 pos
+= hostapd_ctrl_iface_get_key_mgmt(hapd
, pos
, end
- pos
);
1196 ret
= os_snprintf(pos
, end
- pos
, "\n");
1197 if (os_snprintf_error(end
- pos
, ret
))
1202 if (hapd
->conf
->wpa
) {
1203 ret
= os_snprintf(pos
, end
- pos
, "group_cipher=%s\n",
1204 wpa_cipher_txt(hapd
->conf
->wpa_group
));
1205 if (os_snprintf_error(end
- pos
, ret
))
1210 if ((hapd
->conf
->wpa
& WPA_PROTO_RSN
) && hapd
->conf
->rsn_pairwise
) {
1211 ret
= os_snprintf(pos
, end
- pos
, "rsn_pairwise_cipher=");
1212 if (os_snprintf_error(end
- pos
, ret
))
1216 ret
= wpa_write_ciphers(pos
, end
, hapd
->conf
->rsn_pairwise
,
1222 ret
= os_snprintf(pos
, end
- pos
, "\n");
1223 if (os_snprintf_error(end
- pos
, ret
))
1228 if ((hapd
->conf
->wpa
& WPA_PROTO_WPA
) && hapd
->conf
->wpa_pairwise
) {
1229 ret
= os_snprintf(pos
, end
- pos
, "wpa_pairwise_cipher=");
1230 if (os_snprintf_error(end
- pos
, ret
))
1234 ret
= wpa_write_ciphers(pos
, end
, hapd
->conf
->wpa_pairwise
,
1240 ret
= os_snprintf(pos
, end
- pos
, "\n");
1241 if (os_snprintf_error(end
- pos
, ret
))
1250 static int hostapd_ctrl_iface_set(struct hostapd_data
*hapd
, char *cmd
)
1255 value
= os_strchr(cmd
, ' ');
1260 wpa_printf(MSG_DEBUG
, "CTRL_IFACE SET '%s'='%s'", cmd
, value
);
1262 #ifdef CONFIG_WPS_TESTING
1263 } else if (os_strcasecmp(cmd
, "wps_version_number") == 0) {
1265 val
= strtol(value
, NULL
, 0);
1266 if (val
< 0 || val
> 0xff) {
1268 wpa_printf(MSG_DEBUG
, "WPS: Invalid "
1269 "wps_version_number %ld", val
);
1271 wps_version_number
= val
;
1272 wpa_printf(MSG_DEBUG
, "WPS: Testing - force WPS "
1274 (wps_version_number
& 0xf0) >> 4,
1275 wps_version_number
& 0x0f);
1276 hostapd_wps_update_ie(hapd
);
1278 } else if (os_strcasecmp(cmd
, "wps_testing_dummy_cred") == 0) {
1279 wps_testing_dummy_cred
= atoi(value
);
1280 wpa_printf(MSG_DEBUG
, "WPS: Testing - dummy_cred=%d",
1281 wps_testing_dummy_cred
);
1282 } else if (os_strcasecmp(cmd
, "wps_corrupt_pkhash") == 0) {
1283 wps_corrupt_pkhash
= atoi(value
);
1284 wpa_printf(MSG_DEBUG
, "WPS: Testing - wps_corrupt_pkhash=%d",
1285 wps_corrupt_pkhash
);
1286 #endif /* CONFIG_WPS_TESTING */
1287 #ifdef CONFIG_TESTING_OPTIONS
1288 } else if (os_strcasecmp(cmd
, "ext_mgmt_frame_handling") == 0) {
1289 hapd
->ext_mgmt_frame_handling
= atoi(value
);
1290 } else if (os_strcasecmp(cmd
, "ext_eapol_frame_io") == 0) {
1291 hapd
->ext_eapol_frame_io
= atoi(value
);
1293 } else if (os_strcasecmp(cmd
, "dpp_config_obj_override") == 0) {
1294 os_free(hapd
->dpp_config_obj_override
);
1295 hapd
->dpp_config_obj_override
= os_strdup(value
);
1296 } else if (os_strcasecmp(cmd
, "dpp_discovery_override") == 0) {
1297 os_free(hapd
->dpp_discovery_override
);
1298 hapd
->dpp_discovery_override
= os_strdup(value
);
1299 } else if (os_strcasecmp(cmd
, "dpp_groups_override") == 0) {
1300 os_free(hapd
->dpp_groups_override
);
1301 hapd
->dpp_groups_override
= os_strdup(value
);
1302 } else if (os_strcasecmp(cmd
,
1303 "dpp_ignore_netaccesskey_mismatch") == 0) {
1304 hapd
->dpp_ignore_netaccesskey_mismatch
= atoi(value
);
1305 } else if (os_strcasecmp(cmd
, "dpp_test") == 0) {
1306 dpp_test
= atoi(value
);
1307 #endif /* CONFIG_DPP */
1308 #endif /* CONFIG_TESTING_OPTIONS */
1310 } else if (os_strcasecmp(cmd
, "mbo_assoc_disallow") == 0) {
1313 if (!hapd
->conf
->mbo_enabled
)
1317 if (val
< 0 || val
> 1)
1320 hapd
->mbo_assoc_disallow
= val
;
1321 ieee802_11_update_beacons(hapd
->iface
);
1324 * TODO: Need to configure drivers that do AP MLME offload with
1325 * disallowing station logic.
1327 #endif /* CONFIG_MBO */
1329 } else if (os_strcasecmp(cmd
, "dpp_configurator_params") == 0) {
1330 os_free(hapd
->dpp_configurator_params
);
1331 hapd
->dpp_configurator_params
= os_strdup(value
);
1332 #endif /* CONFIG_DPP */
1334 struct sta_info
*sta
;
1335 struct vlan_description vlan_id
;
1337 ret
= hostapd_set_iface(hapd
->iconf
, hapd
->conf
, cmd
, value
);
1341 if (os_strcasecmp(cmd
, "deny_mac_file") == 0) {
1342 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
1343 if (hostapd_maclist_found(
1344 hapd
->conf
->deny_mac
,
1345 hapd
->conf
->num_deny_mac
, sta
->addr
,
1347 (!vlan_id
.notempty
||
1348 !vlan_compare(&vlan_id
, sta
->vlan_desc
)))
1350 hapd
, sta
, sta
->addr
,
1351 WLAN_REASON_UNSPECIFIED
);
1353 } else if (hapd
->conf
->macaddr_acl
== DENY_UNLESS_ACCEPTED
&&
1354 os_strcasecmp(cmd
, "accept_mac_file") == 0) {
1355 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
1356 if (!hostapd_maclist_found(
1357 hapd
->conf
->accept_mac
,
1358 hapd
->conf
->num_accept_mac
,
1359 sta
->addr
, &vlan_id
) ||
1360 (vlan_id
.notempty
&&
1361 vlan_compare(&vlan_id
, sta
->vlan_desc
)))
1363 hapd
, sta
, sta
->addr
,
1364 WLAN_REASON_UNSPECIFIED
);
1373 static int hostapd_ctrl_iface_get(struct hostapd_data
*hapd
, char *cmd
,
1374 char *buf
, size_t buflen
)
1378 wpa_printf(MSG_DEBUG
, "CTRL_IFACE GET '%s'", cmd
);
1380 if (os_strcmp(cmd
, "version") == 0) {
1381 res
= os_snprintf(buf
, buflen
, "%s", VERSION_STR
);
1382 if (os_snprintf_error(buflen
, res
))
1385 } else if (os_strcmp(cmd
, "tls_library") == 0) {
1386 res
= tls_get_library_version(buf
, buflen
);
1387 if (os_snprintf_error(buflen
, res
))
1396 static int hostapd_ctrl_iface_enable(struct hostapd_iface
*iface
)
1398 if (hostapd_enable_iface(iface
) < 0) {
1399 wpa_printf(MSG_ERROR
, "Enabling of interface failed");
1406 static int hostapd_ctrl_iface_reload(struct hostapd_iface
*iface
)
1408 if (hostapd_reload_iface(iface
) < 0) {
1409 wpa_printf(MSG_ERROR
, "Reloading of interface failed");
1416 static int hostapd_ctrl_iface_disable(struct hostapd_iface
*iface
)
1418 if (hostapd_disable_iface(iface
) < 0) {
1419 wpa_printf(MSG_ERROR
, "Disabling of interface failed");
1426 #ifdef CONFIG_TESTING_OPTIONS
1428 static int hostapd_ctrl_iface_radar(struct hostapd_data
*hapd
, char *cmd
)
1430 union wpa_event_data data
;
1432 enum wpa_event_type event
;
1434 wpa_printf(MSG_DEBUG
, "RADAR TEST: %s", cmd
);
1436 os_memset(&data
, 0, sizeof(data
));
1438 param
= os_strchr(cmd
, ' ');
1443 if (os_strcmp(cmd
, "DETECTED") == 0)
1444 event
= EVENT_DFS_RADAR_DETECTED
;
1445 else if (os_strcmp(cmd
, "CAC-FINISHED") == 0)
1446 event
= EVENT_DFS_CAC_FINISHED
;
1447 else if (os_strcmp(cmd
, "CAC-ABORTED") == 0)
1448 event
= EVENT_DFS_CAC_ABORTED
;
1449 else if (os_strcmp(cmd
, "NOP-FINISHED") == 0)
1450 event
= EVENT_DFS_NOP_FINISHED
;
1452 wpa_printf(MSG_DEBUG
, "Unsupported RADAR test command: %s",
1457 pos
= os_strstr(param
, "freq=");
1459 data
.dfs_event
.freq
= atoi(pos
+ 5);
1461 pos
= os_strstr(param
, "ht_enabled=1");
1463 data
.dfs_event
.ht_enabled
= 1;
1465 pos
= os_strstr(param
, "chan_offset=");
1467 data
.dfs_event
.chan_offset
= atoi(pos
+ 12);
1469 pos
= os_strstr(param
, "chan_width=");
1471 data
.dfs_event
.chan_width
= atoi(pos
+ 11);
1473 pos
= os_strstr(param
, "cf1=");
1475 data
.dfs_event
.cf1
= atoi(pos
+ 4);
1477 pos
= os_strstr(param
, "cf2=");
1479 data
.dfs_event
.cf2
= atoi(pos
+ 4);
1481 wpa_supplicant_event(hapd
, event
, &data
);
1487 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data
*hapd
, char *cmd
)
1493 wpa_printf(MSG_DEBUG
, "External MGMT TX: %s", cmd
);
1495 len
= os_strlen(cmd
);
1500 buf
= os_malloc(len
);
1504 if (hexstr2bin(cmd
, buf
, len
) < 0) {
1509 res
= hostapd_drv_send_mlme(hapd
, buf
, len
, 0);
1515 static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data
*hapd
,
1521 int stype
= 0, ok
= 0;
1522 union wpa_event_data event
;
1524 if (!hapd
->ext_mgmt_frame_handling
)
1527 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1529 wpa_printf(MSG_DEBUG
, "External MGMT TX status process: %s", cmd
);
1532 param
= os_strstr(pos
, "stype=");
1535 stype
= atoi(param
);
1538 param
= os_strstr(pos
, " ok=");
1544 param
= os_strstr(pos
, " buf=");
1549 len
= os_strlen(param
);
1554 buf
= os_malloc(len
);
1555 if (!buf
|| hexstr2bin(param
, buf
, len
) < 0) {
1560 os_memset(&event
, 0, sizeof(event
));
1561 event
.tx_status
.type
= WLAN_FC_TYPE_MGMT
;
1562 event
.tx_status
.data
= buf
;
1563 event
.tx_status
.data_len
= len
;
1564 event
.tx_status
.stype
= stype
;
1565 event
.tx_status
.ack
= ok
;
1566 hapd
->ext_mgmt_frame_handling
= 0;
1567 wpa_supplicant_event(hapd
, EVENT_TX_STATUS
, &event
);
1568 hapd
->ext_mgmt_frame_handling
= 1;
1576 static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data
*hapd
,
1582 int freq
= 0, datarate
= 0, ssi_signal
= 0;
1583 union wpa_event_data event
;
1585 if (!hapd
->ext_mgmt_frame_handling
)
1588 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1590 wpa_printf(MSG_DEBUG
, "External MGMT RX process: %s", cmd
);
1593 param
= os_strstr(pos
, "freq=");
1599 param
= os_strstr(pos
, " datarate=");
1602 datarate
= atoi(param
);
1605 param
= os_strstr(pos
, " ssi_signal=");
1608 ssi_signal
= atoi(param
);
1611 param
= os_strstr(pos
, " frame=");
1616 len
= os_strlen(param
);
1621 buf
= os_malloc(len
);
1625 if (hexstr2bin(param
, buf
, len
) < 0) {
1630 os_memset(&event
, 0, sizeof(event
));
1631 event
.rx_mgmt
.freq
= freq
;
1632 event
.rx_mgmt
.frame
= buf
;
1633 event
.rx_mgmt
.frame_len
= len
;
1634 event
.rx_mgmt
.ssi_signal
= ssi_signal
;
1635 event
.rx_mgmt
.datarate
= datarate
;
1636 hapd
->ext_mgmt_frame_handling
= 0;
1637 wpa_supplicant_event(hapd
, EVENT_RX_MGMT
, &event
);
1638 hapd
->ext_mgmt_frame_handling
= 1;
1646 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data
*hapd
, char *cmd
)
1649 u8 src
[ETH_ALEN
], *buf
;
1653 wpa_printf(MSG_DEBUG
, "External EAPOL RX: %s", cmd
);
1656 used
= hwaddr_aton2(pos
, src
);
1663 len
= os_strlen(pos
);
1668 buf
= os_malloc(len
);
1672 if (hexstr2bin(pos
, buf
, len
) < 0) {
1677 ieee802_1x_receive(hapd
, src
, buf
, len
);
1684 static u16
ipv4_hdr_checksum(const void *buf
, size_t len
)
1688 const u16
*pos
= buf
;
1690 for (i
= 0; i
< len
/ 2; i
++)
1694 sum
= (sum
& 0xffff) + (sum
>> 16);
1696 return sum
^ 0xffff;
1700 #define HWSIM_PACKETLEN 1500
1701 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1703 static void hostapd_data_test_rx(void *ctx
, const u8
*src_addr
, const u8
*buf
,
1706 struct hostapd_data
*hapd
= ctx
;
1707 const struct ether_header
*eth
;
1712 if (len
!= HWSIM_PACKETLEN
)
1715 eth
= (const struct ether_header
*) buf
;
1716 os_memcpy(&ip
, eth
+ 1, sizeof(ip
));
1717 pos
= &buf
[sizeof(*eth
) + sizeof(ip
)];
1719 if (ip
.ihl
!= 5 || ip
.version
!= 4 ||
1720 ntohs(ip
.tot_len
) != HWSIM_IP_LEN
)
1723 for (i
= 0; i
< HWSIM_IP_LEN
- sizeof(ip
); i
++) {
1729 wpa_msg(hapd
->msg_ctx
, MSG_INFO
, "DATA-TEST-RX " MACSTR
" " MACSTR
,
1730 MAC2STR(eth
->ether_dhost
), MAC2STR(eth
->ether_shost
));
1734 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data
*hapd
,
1737 int enabled
= atoi(cmd
);
1742 if (hapd
->l2_test
) {
1743 l2_packet_deinit(hapd
->l2_test
);
1744 hapd
->l2_test
= NULL
;
1745 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
,
1746 "test data: Disabled");
1754 pos
= os_strstr(cmd
, " ifname=");
1758 ifname
= hapd
->conf
->iface
;
1760 hapd
->l2_test
= l2_packet_init(ifname
, hapd
->own_addr
,
1761 ETHERTYPE_IP
, hostapd_data_test_rx
,
1763 if (hapd
->l2_test
== NULL
)
1766 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: Enabled");
1772 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data
*hapd
, char *cmd
)
1774 u8 dst
[ETH_ALEN
], src
[ETH_ALEN
];
1779 u8 buf
[2 + HWSIM_PACKETLEN
];
1780 struct ether_header
*eth
;
1785 if (hapd
->l2_test
== NULL
)
1788 /* format: <dst> <src> <tos> */
1791 used
= hwaddr_aton2(pos
, dst
);
1797 used
= hwaddr_aton2(pos
, src
);
1802 val
= strtol(pos
, NULL
, 0);
1803 if (val
< 0 || val
> 0xff)
1807 eth
= (struct ether_header
*) &buf
[2];
1808 os_memcpy(eth
->ether_dhost
, dst
, ETH_ALEN
);
1809 os_memcpy(eth
->ether_shost
, src
, ETH_ALEN
);
1810 eth
->ether_type
= htons(ETHERTYPE_IP
);
1811 ip
= (struct iphdr
*) (eth
+ 1);
1812 os_memset(ip
, 0, sizeof(*ip
));
1817 ip
->tot_len
= htons(HWSIM_IP_LEN
);
1819 ip
->saddr
= htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1820 ip
->daddr
= htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1821 ip
->check
= ipv4_hdr_checksum(ip
, sizeof(*ip
));
1822 dpos
= (u8
*) (ip
+ 1);
1823 for (i
= 0; i
< HWSIM_IP_LEN
- sizeof(*ip
); i
++)
1826 if (l2_packet_send(hapd
->l2_test
, dst
, ETHERTYPE_IP
, &buf
[2],
1827 HWSIM_PACKETLEN
) < 0)
1830 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: TX dst=" MACSTR
1831 " src=" MACSTR
" tos=0x%x", MAC2STR(dst
), MAC2STR(src
), tos
);
1837 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data
*hapd
,
1841 struct ether_header
*eth
;
1842 struct l2_packet_data
*l2
= NULL
;
1846 const char *ifname
= hapd
->conf
->iface
;
1848 if (os_strncmp(cmd
, "ifname=", 7) == 0) {
1851 cmd
= os_strchr(cmd
, ' ');
1857 len
= os_strlen(cmd
);
1858 if (len
& 1 || len
< ETH_HLEN
* 2)
1862 buf
= os_malloc(len
);
1866 if (hexstr2bin(cmd
, buf
, len
) < 0)
1869 eth
= (struct ether_header
*) buf
;
1870 ethertype
= ntohs(eth
->ether_type
);
1872 l2
= l2_packet_init(ifname
, hapd
->own_addr
, ethertype
,
1873 hostapd_data_test_rx
, hapd
, 1);
1877 res
= l2_packet_send(l2
, eth
->ether_dhost
, ethertype
, buf
, len
);
1878 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: TX frame res=%d", res
);
1881 l2_packet_deinit(l2
);
1884 return res
< 0 ? -1 : 0;
1888 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data
*hapd
, char *cmd
)
1890 #ifdef WPA_TRACE_BFD
1893 wpa_trace_fail_after
= atoi(cmd
);
1894 pos
= os_strchr(cmd
, ':');
1897 os_strlcpy(wpa_trace_fail_func
, pos
,
1898 sizeof(wpa_trace_fail_func
));
1900 wpa_trace_fail_after
= 0;
1904 #else /* WPA_TRACE_BFD */
1906 #endif /* WPA_TRACE_BFD */
1910 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data
*hapd
,
1911 char *buf
, size_t buflen
)
1913 #ifdef WPA_TRACE_BFD
1914 return os_snprintf(buf
, buflen
, "%u:%s", wpa_trace_fail_after
,
1915 wpa_trace_fail_func
);
1916 #else /* WPA_TRACE_BFD */
1918 #endif /* WPA_TRACE_BFD */
1922 static int hostapd_ctrl_test_fail(struct hostapd_data
*hapd
, char *cmd
)
1924 #ifdef WPA_TRACE_BFD
1927 wpa_trace_test_fail_after
= atoi(cmd
);
1928 pos
= os_strchr(cmd
, ':');
1931 os_strlcpy(wpa_trace_test_fail_func
, pos
,
1932 sizeof(wpa_trace_test_fail_func
));
1934 wpa_trace_test_fail_after
= 0;
1938 #else /* WPA_TRACE_BFD */
1940 #endif /* WPA_TRACE_BFD */
1944 static int hostapd_ctrl_get_fail(struct hostapd_data
*hapd
,
1945 char *buf
, size_t buflen
)
1947 #ifdef WPA_TRACE_BFD
1948 return os_snprintf(buf
, buflen
, "%u:%s", wpa_trace_test_fail_after
,
1949 wpa_trace_test_fail_func
);
1950 #else /* WPA_TRACE_BFD */
1952 #endif /* WPA_TRACE_BFD */
1956 static int hostapd_ctrl_reset_pn(struct hostapd_data
*hapd
, const char *cmd
)
1958 struct sta_info
*sta
;
1960 u8 zero
[WPA_TK_MAX_LEN
];
1962 os_memset(zero
, 0, sizeof(zero
));
1964 if (hwaddr_aton(cmd
, addr
))
1967 #ifdef CONFIG_IEEE80211W
1968 if (is_broadcast_ether_addr(addr
) && os_strstr(cmd
, "IGTK")) {
1969 if (hapd
->last_igtk_alg
== WPA_ALG_NONE
)
1972 wpa_printf(MSG_INFO
, "TESTING: Reset IPN for IGTK");
1974 /* First, use a zero key to avoid any possible duplicate key
1975 * avoidance in the driver. */
1976 if (hostapd_drv_set_key(hapd
->conf
->iface
, hapd
,
1977 hapd
->last_igtk_alg
,
1978 broadcast_ether_addr
,
1979 hapd
->last_igtk_key_idx
, 1, NULL
, 0,
1980 zero
, hapd
->last_igtk_len
) < 0)
1983 /* Set the previously configured key to reset its TSC */
1984 return hostapd_drv_set_key(hapd
->conf
->iface
, hapd
,
1985 hapd
->last_igtk_alg
,
1986 broadcast_ether_addr
,
1987 hapd
->last_igtk_key_idx
, 1, NULL
, 0,
1989 hapd
->last_igtk_len
);
1991 #endif /* CONFIG_IEEE80211W */
1993 if (is_broadcast_ether_addr(addr
)) {
1994 if (hapd
->last_gtk_alg
== WPA_ALG_NONE
)
1997 wpa_printf(MSG_INFO
, "TESTING: Reset PN for GTK");
1999 /* First, use a zero key to avoid any possible duplicate key
2000 * avoidance in the driver. */
2001 if (hostapd_drv_set_key(hapd
->conf
->iface
, hapd
,
2003 broadcast_ether_addr
,
2004 hapd
->last_gtk_key_idx
, 1, NULL
, 0,
2005 zero
, hapd
->last_gtk_len
) < 0)
2008 /* Set the previously configured key to reset its TSC */
2009 return hostapd_drv_set_key(hapd
->conf
->iface
, hapd
,
2011 broadcast_ether_addr
,
2012 hapd
->last_gtk_key_idx
, 1, NULL
, 0,
2013 hapd
->last_gtk
, hapd
->last_gtk_len
);
2016 sta
= ap_get_sta(hapd
, addr
);
2020 if (sta
->last_tk_alg
== WPA_ALG_NONE
)
2023 wpa_printf(MSG_INFO
, "TESTING: Reset PN for " MACSTR
,
2024 MAC2STR(sta
->addr
));
2026 /* First, use a zero key to avoid any possible duplicate key avoidance
2028 if (hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, sta
->last_tk_alg
,
2029 sta
->addr
, sta
->last_tk_key_idx
, 1, NULL
, 0,
2030 zero
, sta
->last_tk_len
) < 0)
2033 /* Set the previously configured key to reset its TSC/RSC */
2034 return hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, sta
->last_tk_alg
,
2035 sta
->addr
, sta
->last_tk_key_idx
, 1, NULL
, 0,
2036 sta
->last_tk
, sta
->last_tk_len
);
2040 static int hostapd_ctrl_set_key(struct hostapd_data
*hapd
, const char *cmd
)
2043 const char *pos
= cmd
;
2046 u8 seq
[6], key
[WPA_TK_MAX_LEN
];
2049 /* parameters: alg addr idx set_tx seq key */
2052 pos
= os_strchr(pos
, ' ');
2056 if (hwaddr_aton(pos
, addr
))
2063 pos
= os_strchr(pos
, ' ');
2068 pos
= os_strchr(pos
, ' ');
2072 if (hexstr2bin(pos
, seq
, sizeof(6)) < 0)
2078 key_len
= os_strlen(pos
) / 2;
2079 if (hexstr2bin(pos
, key
, key_len
) < 0)
2082 wpa_printf(MSG_INFO
, "TESTING: Set key");
2083 return hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, alg
, addr
, idx
,
2084 set_tx
, seq
, 6, key
, key_len
);
2088 static void restore_tk(void *ctx1
, void *ctx2
)
2090 struct hostapd_data
*hapd
= ctx1
;
2091 struct sta_info
*sta
= ctx2
;
2093 wpa_printf(MSG_INFO
, "TESTING: Restore TK for " MACSTR
,
2094 MAC2STR(sta
->addr
));
2095 /* This does not really restore the TSC properly, so this will result
2096 * in replay protection issues for now since there is no clean way of
2097 * preventing encryption of a single EAPOL frame. */
2098 hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, sta
->last_tk_alg
,
2099 sta
->addr
, sta
->last_tk_key_idx
, 1, NULL
, 0,
2100 sta
->last_tk
, sta
->last_tk_len
);
2104 static int hostapd_ctrl_resend_m1(struct hostapd_data
*hapd
, const char *cmd
)
2106 struct sta_info
*sta
;
2108 int plain
= os_strstr(cmd
, "plaintext") != NULL
;
2110 if (hwaddr_aton(cmd
, addr
))
2113 sta
= ap_get_sta(hapd
, addr
);
2114 if (!sta
|| !sta
->wpa_sm
)
2117 if (plain
&& sta
->last_tk_alg
== WPA_ALG_NONE
)
2118 plain
= 0; /* no need for special processing */
2120 wpa_printf(MSG_INFO
, "TESTING: Clear TK for " MACSTR
,
2121 MAC2STR(sta
->addr
));
2122 hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, WPA_ALG_NONE
,
2123 sta
->addr
, sta
->last_tk_key_idx
, 0, NULL
, 0,
2127 wpa_printf(MSG_INFO
, "TESTING: Send M1 to " MACSTR
, MAC2STR(sta
->addr
));
2128 return wpa_auth_resend_m1(sta
->wpa_sm
,
2129 os_strstr(cmd
, "change-anonce") != NULL
,
2130 plain
? restore_tk
: NULL
, hapd
, sta
);
2134 static int hostapd_ctrl_resend_m3(struct hostapd_data
*hapd
, const char *cmd
)
2136 struct sta_info
*sta
;
2138 int plain
= os_strstr(cmd
, "plaintext") != NULL
;
2140 if (hwaddr_aton(cmd
, addr
))
2143 sta
= ap_get_sta(hapd
, addr
);
2144 if (!sta
|| !sta
->wpa_sm
)
2147 if (plain
&& sta
->last_tk_alg
== WPA_ALG_NONE
)
2148 plain
= 0; /* no need for special processing */
2150 wpa_printf(MSG_INFO
, "TESTING: Clear TK for " MACSTR
,
2151 MAC2STR(sta
->addr
));
2152 hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, WPA_ALG_NONE
,
2153 sta
->addr
, sta
->last_tk_key_idx
, 0, NULL
, 0,
2157 wpa_printf(MSG_INFO
, "TESTING: Send M3 to " MACSTR
, MAC2STR(sta
->addr
));
2158 return wpa_auth_resend_m3(sta
->wpa_sm
,
2159 plain
? restore_tk
: NULL
, hapd
, sta
);
2163 static int hostapd_ctrl_resend_group_m1(struct hostapd_data
*hapd
,
2166 struct sta_info
*sta
;
2168 int plain
= os_strstr(cmd
, "plaintext") != NULL
;
2170 if (hwaddr_aton(cmd
, addr
))
2173 sta
= ap_get_sta(hapd
, addr
);
2174 if (!sta
|| !sta
->wpa_sm
)
2177 if (plain
&& sta
->last_tk_alg
== WPA_ALG_NONE
)
2178 plain
= 0; /* no need for special processing */
2180 wpa_printf(MSG_INFO
, "TESTING: Clear TK for " MACSTR
,
2181 MAC2STR(sta
->addr
));
2182 hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, WPA_ALG_NONE
,
2183 sta
->addr
, sta
->last_tk_key_idx
, 0, NULL
, 0,
2187 wpa_printf(MSG_INFO
,
2188 "TESTING: Send group M1 for the same GTK and zero RSC to "
2189 MACSTR
, MAC2STR(sta
->addr
));
2190 return wpa_auth_resend_group_m1(sta
->wpa_sm
,
2191 plain
? restore_tk
: NULL
, hapd
, sta
);
2194 #endif /* CONFIG_TESTING_OPTIONS */
2197 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface
*iface
,
2201 struct csa_settings settings
;
2205 ret
= hostapd_parse_csa_settings(pos
, &settings
);
2209 for (i
= 0; i
< iface
->num_bss
; i
++) {
2210 ret
= hostapd_switch_channel(iface
->bss
[i
], &settings
);
2212 /* FIX: What do we do if CSA fails in the middle of
2213 * submitting multi-BSS CSA requests? */
2219 #else /* NEED_AP_MLME */
2221 #endif /* NEED_AP_MLME */
2225 static int hostapd_ctrl_iface_mib(struct hostapd_data
*hapd
, char *reply
,
2226 int reply_size
, const char *param
)
2228 #ifdef RADIUS_SERVER
2229 if (os_strcmp(param
, "radius_server") == 0) {
2230 return radius_server_get_mib(hapd
->radius_srv
, reply
,
2233 #endif /* RADIUS_SERVER */
2238 static int hostapd_ctrl_iface_vendor(struct hostapd_data
*hapd
, char *cmd
,
2239 char *buf
, size_t buflen
)
2244 unsigned int vendor_id
, subcmd
;
2245 struct wpabuf
*reply
;
2246 size_t data_len
= 0;
2248 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
2249 vendor_id
= strtoul(cmd
, &pos
, 16);
2250 if (!isblank((unsigned char) *pos
))
2253 subcmd
= strtoul(pos
, &pos
, 10);
2256 if (!isblank((unsigned char) *pos
++))
2258 data_len
= os_strlen(pos
);
2263 data
= os_malloc(data_len
);
2267 if (hexstr2bin(pos
, data
, data_len
)) {
2268 wpa_printf(MSG_DEBUG
,
2269 "Vendor command: wrong parameter format");
2275 reply
= wpabuf_alloc((buflen
- 1) / 2);
2281 ret
= hostapd_drv_vendor_cmd(hapd
, vendor_id
, subcmd
, data
, data_len
,
2285 ret
= wpa_snprintf_hex(buf
, buflen
, wpabuf_head_u8(reply
),
2295 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data
*hapd
,
2299 struct sta_info
*sta
;
2301 if (hwaddr_aton(cmd
, addr
))
2304 sta
= ap_get_sta(hapd
, addr
);
2305 if (!sta
|| !sta
->eapol_sm
)
2308 eapol_auth_reauthenticate(sta
->eapol_sm
);
2313 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data
*hapd
, char *cmd
)
2316 struct sta_info
*sta
;
2317 char *pos
= cmd
, *param
;
2319 if (hwaddr_aton(pos
, addr
) || pos
[17] != ' ')
2323 pos
= os_strchr(pos
, ' ');
2328 sta
= ap_get_sta(hapd
, addr
);
2329 if (!sta
|| !sta
->eapol_sm
)
2332 return eapol_auth_set_conf(sta
->eapol_sm
, param
, pos
);
2336 static int hostapd_ctrl_iface_log_level(struct hostapd_data
*hapd
, char *cmd
,
2337 char *buf
, size_t buflen
)
2339 char *pos
, *end
, *stamp
;
2342 /* cmd: "LOG_LEVEL [<level>]" */
2346 ret
= os_snprintf(pos
, end
- pos
, "Current level: %s\n"
2348 debug_level_str(wpa_debug_level
),
2349 wpa_debug_timestamp
);
2350 if (os_snprintf_error(end
- pos
, ret
))
2359 stamp
= os_strchr(cmd
, ' ');
2362 while (*stamp
== ' ') {
2367 if (os_strlen(cmd
)) {
2368 int level
= str_to_debug_level(cmd
);
2371 wpa_debug_level
= level
;
2374 if (stamp
&& os_strlen(stamp
))
2375 wpa_debug_timestamp
= atoi(stamp
);
2377 os_memcpy(buf
, "OK\n", 3);
2383 static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data
*hapd
,
2384 char *buf
, size_t buflen
)
2386 struct hostapd_iface
*iface
= hapd
->iface
;
2388 struct hostapd_sta_info
*info
;
2389 struct os_reltime now
;
2391 if (!iface
->num_sta_seen
)
2394 sta_track_expire(iface
, 0);
2399 os_get_reltime(&now
);
2400 dl_list_for_each_reverse(info
, &iface
->sta_seen
,
2401 struct hostapd_sta_info
, list
) {
2402 struct os_reltime age
;
2405 os_reltime_sub(&now
, &info
->last_seen
, &age
);
2406 ret
= os_snprintf(pos
, end
- pos
, MACSTR
" %u %d\n",
2407 MAC2STR(info
->addr
), (unsigned int) age
.sec
,
2409 if (os_snprintf_error(end
- pos
, ret
))
2416 #endif /* NEED_AP_MLME */
2419 static int hostapd_ctrl_iface_req_lci(struct hostapd_data
*hapd
,
2424 if (hwaddr_aton(cmd
, addr
)) {
2425 wpa_printf(MSG_INFO
, "CTRL: REQ_LCI: Invalid MAC address");
2429 return hostapd_send_lci_req(hapd
, addr
);
2433 static int hostapd_ctrl_iface_req_range(struct hostapd_data
*hapd
, char *cmd
)
2436 char *token
, *context
= NULL
;
2437 int random_interval
, min_ap
;
2438 u8 responders
[ETH_ALEN
* RRM_RANGE_REQ_MAX_RESPONDERS
];
2439 unsigned int n_responders
;
2441 token
= str_token(cmd
, " ", &context
);
2442 if (!token
|| hwaddr_aton(token
, addr
)) {
2443 wpa_printf(MSG_INFO
,
2444 "CTRL: REQ_RANGE - Bad destination address");
2448 token
= str_token(cmd
, " ", &context
);
2452 random_interval
= atoi(token
);
2453 if (random_interval
< 0 || random_interval
> 0xffff)
2456 token
= str_token(cmd
, " ", &context
);
2460 min_ap
= atoi(token
);
2461 if (min_ap
<= 0 || min_ap
> WLAN_RRM_RANGE_REQ_MAX_MIN_AP
)
2465 while ((token
= str_token(cmd
, " ", &context
))) {
2466 if (n_responders
== RRM_RANGE_REQ_MAX_RESPONDERS
) {
2467 wpa_printf(MSG_INFO
,
2468 "CTRL: REQ_RANGE: Too many responders");
2472 if (hwaddr_aton(token
, responders
+ n_responders
* ETH_ALEN
)) {
2473 wpa_printf(MSG_INFO
,
2474 "CTRL: REQ_RANGE: Bad responder address");
2481 if (!n_responders
) {
2482 wpa_printf(MSG_INFO
,
2483 "CTRL: REQ_RANGE - No FTM responder address");
2487 return hostapd_send_range_req(hapd
, addr
, random_interval
, min_ap
,
2488 responders
, n_responders
);
2492 static int hostapd_ctrl_iface_req_beacon(struct hostapd_data
*hapd
,
2493 const char *cmd
, char *reply
,
2502 if (hwaddr_aton(cmd
, addr
))
2504 pos
= os_strchr(cmd
, ' ');
2508 if (os_strncmp(pos
, "req_mode=", 9) == 0) {
2509 int val
= hex2byte(pos
+ 9);
2515 pos
= os_strchr(pos
, ' ');
2520 req
= wpabuf_parse_bin(pos
);
2524 ret
= hostapd_send_beacon_req(hapd
, addr
, req_mode
, req
);
2527 ret
= os_snprintf(reply
, reply_size
, "%d", ret
);
2532 static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data
*hapd
, char *buf
)
2534 struct wpa_ssid_value ssid
;
2536 struct wpabuf
*nr
, *lci
= NULL
, *civic
= NULL
;
2541 if (!(hapd
->conf
->radio_measurements
[0] &
2542 WLAN_RRM_CAPS_NEIGHBOR_REPORT
)) {
2543 wpa_printf(MSG_ERROR
,
2544 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2548 if (hwaddr_aton(buf
, bssid
)) {
2549 wpa_printf(MSG_ERROR
, "CTRL: SET_NEIGHBOR: Bad BSSID");
2553 tmp
= os_strstr(buf
, "ssid=");
2554 if (!tmp
|| ssid_parse(tmp
+ 5, &ssid
)) {
2555 wpa_printf(MSG_ERROR
,
2556 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2559 buf
= os_strchr(tmp
+ 6, tmp
[5] == '"' ? '"' : ' ');
2563 tmp
= os_strstr(buf
, "nr=");
2565 wpa_printf(MSG_ERROR
,
2566 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2570 buf
= os_strchr(tmp
, ' ');
2574 nr
= wpabuf_parse_bin(tmp
+ 3);
2576 wpa_printf(MSG_ERROR
,
2577 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2584 tmp
= os_strstr(buf
, "lci=");
2586 buf
= os_strchr(tmp
, ' ');
2589 lci
= wpabuf_parse_bin(tmp
+ 4);
2591 wpa_printf(MSG_ERROR
,
2592 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
2601 tmp
= os_strstr(buf
, "civic=");
2603 buf
= os_strchr(tmp
, ' ');
2606 civic
= wpabuf_parse_bin(tmp
+ 6);
2608 wpa_printf(MSG_ERROR
,
2609 "CTRL: SET_NEIGHBOR: Bad civic subelement");
2619 if (os_strstr(buf
, "stat"))
2623 ret
= hostapd_neighbor_set(hapd
, bssid
, &ssid
, nr
, lci
, civic
,
2634 static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data
*hapd
,
2637 struct wpa_ssid_value ssid
;
2641 if (hwaddr_aton(buf
, bssid
)) {
2642 wpa_printf(MSG_ERROR
, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
2646 tmp
= os_strstr(buf
, "ssid=");
2647 if (!tmp
|| ssid_parse(tmp
+ 5, &ssid
)) {
2648 wpa_printf(MSG_ERROR
,
2649 "CTRL: REMOVE_NEIGHBORr: Bad or missing SSID");
2653 return hostapd_neighbor_remove(hapd
, bssid
, &ssid
);
2657 static int hostapd_ctrl_driver_flags(struct hostapd_iface
*iface
, char *buf
,
2663 ret
= os_snprintf(buf
, buflen
, "%016llX:\n",
2664 (long long unsigned) iface
->drv_flags
);
2665 if (os_snprintf_error(buflen
, ret
))
2671 for (i
= 0; i
< 64; i
++) {
2672 if (iface
->drv_flags
& (1LLU << i
)) {
2673 ret
= os_snprintf(pos
, end
- pos
, "%s\n",
2674 driver_flag_to_string(1LLU << i
));
2675 if (os_snprintf_error(end
- pos
, ret
))
2685 static int hostapd_ctrl_iface_receive_process(struct hostapd_data
*hapd
,
2686 char *buf
, char *reply
,
2688 struct sockaddr_storage
*from
,
2693 os_memcpy(reply
, "OK\n", 3);
2696 if (os_strcmp(buf
, "PING") == 0) {
2697 os_memcpy(reply
, "PONG\n", 5);
2699 } else if (os_strncmp(buf
, "RELOG", 5) == 0) {
2700 if (wpa_debug_reopen_file() < 0)
2702 } else if (os_strcmp(buf
, "STATUS") == 0) {
2703 reply_len
= hostapd_ctrl_iface_status(hapd
, reply
,
2705 } else if (os_strcmp(buf
, "STATUS-DRIVER") == 0) {
2706 reply_len
= hostapd_drv_status(hapd
, reply
, reply_size
);
2707 } else if (os_strcmp(buf
, "MIB") == 0) {
2708 reply_len
= ieee802_11_get_mib(hapd
, reply
, reply_size
);
2709 if (reply_len
>= 0) {
2710 res
= wpa_get_mib(hapd
->wpa_auth
, reply
+ reply_len
,
2711 reply_size
- reply_len
);
2717 if (reply_len
>= 0) {
2718 res
= ieee802_1x_get_mib(hapd
, reply
+ reply_len
,
2719 reply_size
- reply_len
);
2725 #ifndef CONFIG_NO_RADIUS
2726 if (reply_len
>= 0) {
2727 res
= radius_client_get_mib(hapd
->radius
,
2729 reply_size
- reply_len
);
2735 #endif /* CONFIG_NO_RADIUS */
2736 } else if (os_strncmp(buf
, "MIB ", 4) == 0) {
2737 reply_len
= hostapd_ctrl_iface_mib(hapd
, reply
, reply_size
,
2739 } else if (os_strcmp(buf
, "STA-FIRST") == 0) {
2740 reply_len
= hostapd_ctrl_iface_sta_first(hapd
, reply
,
2742 } else if (os_strncmp(buf
, "STA ", 4) == 0) {
2743 reply_len
= hostapd_ctrl_iface_sta(hapd
, buf
+ 4, reply
,
2745 } else if (os_strncmp(buf
, "STA-NEXT ", 9) == 0) {
2746 reply_len
= hostapd_ctrl_iface_sta_next(hapd
, buf
+ 9, reply
,
2748 } else if (os_strcmp(buf
, "ATTACH") == 0) {
2749 if (hostapd_ctrl_iface_attach(hapd
, from
, fromlen
))
2751 } else if (os_strcmp(buf
, "DETACH") == 0) {
2752 if (hostapd_ctrl_iface_detach(hapd
, from
, fromlen
))
2754 } else if (os_strncmp(buf
, "LEVEL ", 6) == 0) {
2755 if (hostapd_ctrl_iface_level(hapd
, from
, fromlen
,
2758 } else if (os_strncmp(buf
, "NEW_STA ", 8) == 0) {
2759 if (hostapd_ctrl_iface_new_sta(hapd
, buf
+ 8))
2761 } else if (os_strncmp(buf
, "DEAUTHENTICATE ", 15) == 0) {
2762 if (hostapd_ctrl_iface_deauthenticate(hapd
, buf
+ 15))
2764 } else if (os_strncmp(buf
, "DISASSOCIATE ", 13) == 0) {
2765 if (hostapd_ctrl_iface_disassociate(hapd
, buf
+ 13))
2767 #ifdef CONFIG_TAXONOMY
2768 } else if (os_strncmp(buf
, "SIGNATURE ", 10) == 0) {
2769 reply_len
= hostapd_ctrl_iface_signature(hapd
, buf
+ 10,
2771 #endif /* CONFIG_TAXONOMY */
2772 } else if (os_strncmp(buf
, "POLL_STA ", 9) == 0) {
2773 if (hostapd_ctrl_iface_poll_sta(hapd
, buf
+ 9))
2775 } else if (os_strcmp(buf
, "STOP_AP") == 0) {
2776 if (hostapd_ctrl_iface_stop_ap(hapd
))
2778 #ifdef CONFIG_IEEE80211W
2780 } else if (os_strncmp(buf
, "SA_QUERY ", 9) == 0) {
2781 if (hostapd_ctrl_iface_sa_query(hapd
, buf
+ 9))
2783 #endif /* NEED_AP_MLME */
2784 #endif /* CONFIG_IEEE80211W */
2786 } else if (os_strncmp(buf
, "WPS_PIN ", 8) == 0) {
2787 if (hostapd_ctrl_iface_wps_pin(hapd
, buf
+ 8))
2789 } else if (os_strncmp(buf
, "WPS_CHECK_PIN ", 14) == 0) {
2790 reply_len
= hostapd_ctrl_iface_wps_check_pin(
2791 hapd
, buf
+ 14, reply
, reply_size
);
2792 } else if (os_strcmp(buf
, "WPS_PBC") == 0) {
2793 if (hostapd_wps_button_pushed(hapd
, NULL
))
2795 } else if (os_strcmp(buf
, "WPS_CANCEL") == 0) {
2796 if (hostapd_wps_cancel(hapd
))
2798 } else if (os_strncmp(buf
, "WPS_AP_PIN ", 11) == 0) {
2799 reply_len
= hostapd_ctrl_iface_wps_ap_pin(hapd
, buf
+ 11,
2801 } else if (os_strncmp(buf
, "WPS_CONFIG ", 11) == 0) {
2802 if (hostapd_ctrl_iface_wps_config(hapd
, buf
+ 11) < 0)
2804 } else if (os_strncmp(buf
, "WPS_GET_STATUS", 13) == 0) {
2805 reply_len
= hostapd_ctrl_iface_wps_get_status(hapd
, reply
,
2807 #ifdef CONFIG_WPS_NFC
2808 } else if (os_strncmp(buf
, "WPS_NFC_TAG_READ ", 17) == 0) {
2809 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd
, buf
+ 17))
2811 } else if (os_strncmp(buf
, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2812 reply_len
= hostapd_ctrl_iface_wps_nfc_config_token(
2813 hapd
, buf
+ 21, reply
, reply_size
);
2814 } else if (os_strncmp(buf
, "WPS_NFC_TOKEN ", 14) == 0) {
2815 reply_len
= hostapd_ctrl_iface_wps_nfc_token(
2816 hapd
, buf
+ 14, reply
, reply_size
);
2817 } else if (os_strncmp(buf
, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2818 reply_len
= hostapd_ctrl_iface_nfc_get_handover_sel(
2819 hapd
, buf
+ 21, reply
, reply_size
);
2820 } else if (os_strncmp(buf
, "NFC_REPORT_HANDOVER ", 20) == 0) {
2821 if (hostapd_ctrl_iface_nfc_report_handover(hapd
, buf
+ 20))
2823 #endif /* CONFIG_WPS_NFC */
2824 #endif /* CONFIG_WPS */
2825 #ifdef CONFIG_INTERWORKING
2826 } else if (os_strncmp(buf
, "SET_QOS_MAP_SET ", 16) == 0) {
2827 if (hostapd_ctrl_iface_set_qos_map_set(hapd
, buf
+ 16))
2829 } else if (os_strncmp(buf
, "SEND_QOS_MAP_CONF ", 18) == 0) {
2830 if (hostapd_ctrl_iface_send_qos_map_conf(hapd
, buf
+ 18))
2832 #endif /* CONFIG_INTERWORKING */
2834 } else if (os_strncmp(buf
, "HS20_WNM_NOTIF ", 15) == 0) {
2835 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd
, buf
+ 15))
2837 } else if (os_strncmp(buf
, "HS20_DEAUTH_REQ ", 16) == 0) {
2838 if (hostapd_ctrl_iface_hs20_deauth_req(hapd
, buf
+ 16))
2840 #endif /* CONFIG_HS20 */
2841 #ifdef CONFIG_WNM_AP
2842 } else if (os_strncmp(buf
, "DISASSOC_IMMINENT ", 18) == 0) {
2843 if (hostapd_ctrl_iface_disassoc_imminent(hapd
, buf
+ 18))
2845 } else if (os_strncmp(buf
, "ESS_DISASSOC ", 13) == 0) {
2846 if (hostapd_ctrl_iface_ess_disassoc(hapd
, buf
+ 13))
2848 } else if (os_strncmp(buf
, "BSS_TM_REQ ", 11) == 0) {
2849 if (hostapd_ctrl_iface_bss_tm_req(hapd
, buf
+ 11))
2851 #endif /* CONFIG_WNM_AP */
2852 } else if (os_strcmp(buf
, "GET_CONFIG") == 0) {
2853 reply_len
= hostapd_ctrl_iface_get_config(hapd
, reply
,
2855 } else if (os_strncmp(buf
, "SET ", 4) == 0) {
2856 if (hostapd_ctrl_iface_set(hapd
, buf
+ 4))
2858 } else if (os_strncmp(buf
, "GET ", 4) == 0) {
2859 reply_len
= hostapd_ctrl_iface_get(hapd
, buf
+ 4, reply
,
2861 } else if (os_strncmp(buf
, "ENABLE", 6) == 0) {
2862 if (hostapd_ctrl_iface_enable(hapd
->iface
))
2864 } else if (os_strncmp(buf
, "RELOAD", 6) == 0) {
2865 if (hostapd_ctrl_iface_reload(hapd
->iface
))
2867 } else if (os_strncmp(buf
, "DISABLE", 7) == 0) {
2868 if (hostapd_ctrl_iface_disable(hapd
->iface
))
2870 } else if (os_strcmp(buf
, "UPDATE_BEACON") == 0) {
2871 if (ieee802_11_set_beacon(hapd
))
2873 #ifdef CONFIG_TESTING_OPTIONS
2874 } else if (os_strncmp(buf
, "RADAR ", 6) == 0) {
2875 if (hostapd_ctrl_iface_radar(hapd
, buf
+ 6))
2877 } else if (os_strncmp(buf
, "MGMT_TX ", 8) == 0) {
2878 if (hostapd_ctrl_iface_mgmt_tx(hapd
, buf
+ 8))
2880 } else if (os_strncmp(buf
, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
2881 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd
,
2884 } else if (os_strncmp(buf
, "MGMT_RX_PROCESS ", 16) == 0) {
2885 if (hostapd_ctrl_iface_mgmt_rx_process(hapd
, buf
+ 16) < 0)
2887 } else if (os_strncmp(buf
, "EAPOL_RX ", 9) == 0) {
2888 if (hostapd_ctrl_iface_eapol_rx(hapd
, buf
+ 9) < 0)
2890 } else if (os_strncmp(buf
, "DATA_TEST_CONFIG ", 17) == 0) {
2891 if (hostapd_ctrl_iface_data_test_config(hapd
, buf
+ 17) < 0)
2893 } else if (os_strncmp(buf
, "DATA_TEST_TX ", 13) == 0) {
2894 if (hostapd_ctrl_iface_data_test_tx(hapd
, buf
+ 13) < 0)
2896 } else if (os_strncmp(buf
, "DATA_TEST_FRAME ", 16) == 0) {
2897 if (hostapd_ctrl_iface_data_test_frame(hapd
, buf
+ 16) < 0)
2899 } else if (os_strncmp(buf
, "TEST_ALLOC_FAIL ", 16) == 0) {
2900 if (hostapd_ctrl_test_alloc_fail(hapd
, buf
+ 16) < 0)
2902 } else if (os_strcmp(buf
, "GET_ALLOC_FAIL") == 0) {
2903 reply_len
= hostapd_ctrl_get_alloc_fail(hapd
, reply
,
2905 } else if (os_strncmp(buf
, "TEST_FAIL ", 10) == 0) {
2906 if (hostapd_ctrl_test_fail(hapd
, buf
+ 10) < 0)
2908 } else if (os_strcmp(buf
, "GET_FAIL") == 0) {
2909 reply_len
= hostapd_ctrl_get_fail(hapd
, reply
, reply_size
);
2910 } else if (os_strncmp(buf
, "RESET_PN ", 9) == 0) {
2911 if (hostapd_ctrl_reset_pn(hapd
, buf
+ 9) < 0)
2913 } else if (os_strncmp(buf
, "SET_KEY ", 8) == 0) {
2914 if (hostapd_ctrl_set_key(hapd
, buf
+ 8) < 0)
2916 } else if (os_strncmp(buf
, "RESEND_M1 ", 10) == 0) {
2917 if (hostapd_ctrl_resend_m1(hapd
, buf
+ 10) < 0)
2919 } else if (os_strncmp(buf
, "RESEND_M3 ", 10) == 0) {
2920 if (hostapd_ctrl_resend_m3(hapd
, buf
+ 10) < 0)
2922 } else if (os_strncmp(buf
, "RESEND_GROUP_M1 ", 16) == 0) {
2923 if (hostapd_ctrl_resend_group_m1(hapd
, buf
+ 16) < 0)
2925 } else if (os_strcmp(buf
, "REKEY_GTK") == 0) {
2926 if (wpa_auth_rekey_gtk(hapd
->wpa_auth
) < 0)
2928 #endif /* CONFIG_TESTING_OPTIONS */
2929 } else if (os_strncmp(buf
, "CHAN_SWITCH ", 12) == 0) {
2930 if (hostapd_ctrl_iface_chan_switch(hapd
->iface
, buf
+ 12))
2932 } else if (os_strncmp(buf
, "VENDOR ", 7) == 0) {
2933 reply_len
= hostapd_ctrl_iface_vendor(hapd
, buf
+ 7, reply
,
2935 } else if (os_strcmp(buf
, "ERP_FLUSH") == 0) {
2936 ieee802_1x_erp_flush(hapd
);
2937 #ifdef RADIUS_SERVER
2938 radius_server_erp_flush(hapd
->radius_srv
);
2939 #endif /* RADIUS_SERVER */
2940 } else if (os_strncmp(buf
, "EAPOL_REAUTH ", 13) == 0) {
2941 if (hostapd_ctrl_iface_eapol_reauth(hapd
, buf
+ 13))
2943 } else if (os_strncmp(buf
, "EAPOL_SET ", 10) == 0) {
2944 if (hostapd_ctrl_iface_eapol_set(hapd
, buf
+ 10))
2946 } else if (os_strncmp(buf
, "LOG_LEVEL", 9) == 0) {
2947 reply_len
= hostapd_ctrl_iface_log_level(
2948 hapd
, buf
+ 9, reply
, reply_size
);
2950 } else if (os_strcmp(buf
, "TRACK_STA_LIST") == 0) {
2951 reply_len
= hostapd_ctrl_iface_track_sta_list(
2952 hapd
, reply
, reply_size
);
2953 #endif /* NEED_AP_MLME */
2954 } else if (os_strcmp(buf
, "PMKSA") == 0) {
2955 reply_len
= hostapd_ctrl_iface_pmksa_list(hapd
, reply
,
2957 } else if (os_strcmp(buf
, "PMKSA_FLUSH") == 0) {
2958 hostapd_ctrl_iface_pmksa_flush(hapd
);
2959 } else if (os_strncmp(buf
, "PMKSA_ADD ", 10) == 0) {
2960 if (hostapd_ctrl_iface_pmksa_add(hapd
, buf
+ 10) < 0)
2962 } else if (os_strncmp(buf
, "SET_NEIGHBOR ", 13) == 0) {
2963 if (hostapd_ctrl_iface_set_neighbor(hapd
, buf
+ 13))
2965 } else if (os_strncmp(buf
, "REMOVE_NEIGHBOR ", 16) == 0) {
2966 if (hostapd_ctrl_iface_remove_neighbor(hapd
, buf
+ 16))
2968 } else if (os_strncmp(buf
, "REQ_LCI ", 8) == 0) {
2969 if (hostapd_ctrl_iface_req_lci(hapd
, buf
+ 8))
2971 } else if (os_strncmp(buf
, "REQ_RANGE ", 10) == 0) {
2972 if (hostapd_ctrl_iface_req_range(hapd
, buf
+ 10))
2974 } else if (os_strncmp(buf
, "REQ_BEACON ", 11) == 0) {
2975 reply_len
= hostapd_ctrl_iface_req_beacon(hapd
, buf
+ 11,
2977 } else if (os_strcmp(buf
, "DRIVER_FLAGS") == 0) {
2978 reply_len
= hostapd_ctrl_driver_flags(hapd
->iface
, reply
,
2980 } else if (os_strcmp(buf
, "TERMINATE") == 0) {
2983 } else if (os_strncmp(buf
, "DPP_QR_CODE ", 12) == 0) {
2984 res
= hostapd_dpp_qr_code(hapd
, buf
+ 12);
2988 reply_len
= os_snprintf(reply
, reply_size
, "%d", res
);
2989 if (os_snprintf_error(reply_size
, reply_len
))
2992 } else if (os_strncmp(buf
, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
2993 res
= hostapd_dpp_bootstrap_gen(hapd
, buf
+ 18);
2997 reply_len
= os_snprintf(reply
, reply_size
, "%d", res
);
2998 if (os_snprintf_error(reply_size
, reply_len
))
3001 } else if (os_strncmp(buf
, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
3002 if (hostapd_dpp_bootstrap_remove(hapd
, buf
+ 21) < 0)
3004 } else if (os_strncmp(buf
, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
3007 uri
= hostapd_dpp_bootstrap_get_uri(hapd
, atoi(buf
+ 22));
3011 reply_len
= os_snprintf(reply
, reply_size
, "%s", uri
);
3012 if (os_snprintf_error(reply_size
, reply_len
))
3015 } else if (os_strncmp(buf
, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
3016 reply_len
= hostapd_dpp_bootstrap_info(hapd
, atoi(buf
+ 19),
3018 } else if (os_strncmp(buf
, "DPP_AUTH_INIT ", 14) == 0) {
3019 if (hostapd_dpp_auth_init(hapd
, buf
+ 13) < 0)
3021 } else if (os_strncmp(buf
, "DPP_LISTEN ", 11) == 0) {
3022 if (hostapd_dpp_listen(hapd
, buf
+ 11) < 0)
3024 } else if (os_strcmp(buf
, "DPP_STOP_LISTEN") == 0) {
3025 hostapd_dpp_listen_stop(hapd
);
3026 } else if (os_strncmp(buf
, "DPP_CONFIGURATOR_ADD", 20) == 0) {
3027 res
= hostapd_dpp_configurator_add(hapd
, buf
+ 20);
3031 reply_len
= os_snprintf(reply
, reply_size
, "%d", res
);
3032 if (os_snprintf_error(reply_size
, reply_len
))
3035 } else if (os_strncmp(buf
, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
3036 if (hostapd_dpp_configurator_remove(hapd
, buf
+ 24) < 0)
3038 } else if (os_strncmp(buf
, "DPP_PKEX_ADD ", 13) == 0) {
3039 res
= hostapd_dpp_pkex_add(hapd
, buf
+ 12);
3043 reply_len
= os_snprintf(reply
, reply_size
, "%d", res
);
3044 if (os_snprintf_error(reply_size
, reply_len
))
3047 } else if (os_strncmp(buf
, "DPP_PKEX_REMOVE ", 16) == 0) {
3048 if (hostapd_dpp_pkex_remove(hapd
, buf
+ 16) < 0)
3050 #endif /* CONFIG_DPP */
3052 os_memcpy(reply
, "UNKNOWN COMMAND\n", 16);
3056 if (reply_len
< 0) {
3057 os_memcpy(reply
, "FAIL\n", 5);
3065 static void hostapd_ctrl_iface_receive(int sock
, void *eloop_ctx
,
3068 struct hostapd_data
*hapd
= eloop_ctx
;
3071 struct sockaddr_storage from
;
3072 socklen_t fromlen
= sizeof(from
);
3073 char *reply
, *pos
= buf
;
3074 const int reply_size
= 4096;
3076 int level
= MSG_DEBUG
;
3077 #ifdef CONFIG_CTRL_IFACE_UDP
3078 unsigned char lcookie
[COOKIE_LEN
];
3079 #endif /* CONFIG_CTRL_IFACE_UDP */
3081 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
3082 (struct sockaddr
*) &from
, &fromlen
);
3084 wpa_printf(MSG_ERROR
, "recvfrom(ctrl_iface): %s",
3090 reply
= os_malloc(reply_size
);
3091 if (reply
== NULL
) {
3092 if (sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
3094 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
3100 #ifdef CONFIG_CTRL_IFACE_UDP
3101 if (os_strcmp(buf
, "GET_COOKIE") == 0) {
3102 os_memcpy(reply
, "COOKIE=", 7);
3103 wpa_snprintf_hex(reply
+ 7, 2 * COOKIE_LEN
+ 1,
3104 cookie
, COOKIE_LEN
);
3105 reply_len
= 7 + 2 * COOKIE_LEN
;
3109 if (os_strncmp(buf
, "COOKIE=", 7) != 0 ||
3110 hexstr2bin(buf
+ 7, lcookie
, COOKIE_LEN
) < 0) {
3111 wpa_printf(MSG_DEBUG
,
3112 "CTRL: No cookie in the request - drop request");
3117 if (os_memcmp(cookie
, lcookie
, COOKIE_LEN
) != 0) {
3118 wpa_printf(MSG_DEBUG
,
3119 "CTRL: Invalid cookie in the request - drop request");
3124 pos
= buf
+ 7 + 2 * COOKIE_LEN
;
3127 #endif /* CONFIG_CTRL_IFACE_UDP */
3129 if (os_strcmp(pos
, "PING") == 0)
3130 level
= MSG_EXCESSIVE
;
3131 wpa_hexdump_ascii(level
, "RX ctrl_iface", pos
, res
);
3133 reply_len
= hostapd_ctrl_iface_receive_process(hapd
, pos
,
3137 #ifdef CONFIG_CTRL_IFACE_UDP
3139 #endif /* CONFIG_CTRL_IFACE_UDP */
3140 if (sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
3142 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
3149 #ifndef CONFIG_CTRL_IFACE_UDP
3150 static char * hostapd_ctrl_iface_path(struct hostapd_data
*hapd
)
3155 if (hapd
->conf
->ctrl_interface
== NULL
)
3158 len
= os_strlen(hapd
->conf
->ctrl_interface
) +
3159 os_strlen(hapd
->conf
->iface
) + 2;
3160 buf
= os_malloc(len
);
3164 os_snprintf(buf
, len
, "%s/%s",
3165 hapd
->conf
->ctrl_interface
, hapd
->conf
->iface
);
3166 buf
[len
- 1] = '\0';
3169 #endif /* CONFIG_CTRL_IFACE_UDP */
3172 static void hostapd_ctrl_iface_msg_cb(void *ctx
, int level
,
3173 enum wpa_msg_type type
,
3174 const char *txt
, size_t len
)
3176 struct hostapd_data
*hapd
= ctx
;
3179 hostapd_ctrl_iface_send(hapd
, level
, type
, txt
, len
);
3183 int hostapd_ctrl_iface_init(struct hostapd_data
*hapd
)
3185 #ifdef CONFIG_CTRL_IFACE_UDP
3186 int port
= HOSTAPD_CTRL_IFACE_PORT
;
3188 char port_str
[40], *tmp
;
3190 struct addrinfo hints
= { 0 }, *res
, *saveres
;
3193 if (hapd
->ctrl_sock
> -1) {
3194 wpa_printf(MSG_DEBUG
, "ctrl_iface already exists!");
3198 if (hapd
->conf
->ctrl_interface
== NULL
)
3201 pos
= os_strstr(hapd
->conf
->ctrl_interface
, "udp:");
3206 wpa_printf(MSG_ERROR
, "Invalid ctrl_iface UDP port");
3211 dl_list_init(&hapd
->ctrl_dst
);
3212 hapd
->ctrl_sock
= -1;
3213 os_get_random(cookie
, COOKIE_LEN
);
3215 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3216 hints
.ai_flags
= AI_PASSIVE
;
3217 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3219 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3220 hints
.ai_family
= AF_INET6
;
3221 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3222 hints
.ai_family
= AF_INET
;
3223 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3224 hints
.ai_socktype
= SOCK_DGRAM
;
3227 os_snprintf(p
, sizeof(p
), "%d", port
);
3228 n
= getaddrinfo(NULL
, p
, &hints
, &res
);
3230 wpa_printf(MSG_ERROR
, "getaddrinfo(): %s", gai_strerror(n
));
3235 hapd
->ctrl_sock
= socket(res
->ai_family
, res
->ai_socktype
,
3237 if (hapd
->ctrl_sock
< 0) {
3238 wpa_printf(MSG_ERROR
, "socket(PF_INET): %s", strerror(errno
));
3242 if (bind(hapd
->ctrl_sock
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
3244 if ((HOSTAPD_CTRL_IFACE_PORT
- port
) <
3245 HOSTAPD_CTRL_IFACE_PORT_LIMIT
&& !pos
)
3247 wpa_printf(MSG_ERROR
, "bind(AF_INET): %s", strerror(errno
));
3251 freeaddrinfo(saveres
);
3253 os_snprintf(port_str
, sizeof(port_str
), "udp:%d", port
);
3254 tmp
= os_strdup(port_str
);
3256 os_free(hapd
->conf
->ctrl_interface
);
3257 hapd
->conf
->ctrl_interface
= tmp
;
3259 wpa_printf(MSG_DEBUG
, "ctrl_iface_init UDP port: %d", port
);
3261 if (eloop_register_read_sock(hapd
->ctrl_sock
,
3262 hostapd_ctrl_iface_receive
, hapd
, NULL
) <
3264 hostapd_ctrl_iface_deinit(hapd
);
3268 hapd
->msg_ctx
= hapd
;
3269 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb
);
3274 if (hapd
->ctrl_sock
>= 0)
3275 close(hapd
->ctrl_sock
);
3277 #else /* CONFIG_CTRL_IFACE_UDP */
3278 struct sockaddr_un addr
;
3282 if (hapd
->ctrl_sock
> -1) {
3283 wpa_printf(MSG_DEBUG
, "ctrl_iface already exists!");
3287 dl_list_init(&hapd
->ctrl_dst
);
3289 if (hapd
->conf
->ctrl_interface
== NULL
)
3292 if (mkdir(hapd
->conf
->ctrl_interface
, S_IRWXU
| S_IRWXG
) < 0) {
3293 if (errno
== EEXIST
) {
3294 wpa_printf(MSG_DEBUG
, "Using existing control "
3295 "interface directory.");
3297 wpa_printf(MSG_ERROR
, "mkdir[ctrl_interface]: %s",
3303 if (hapd
->conf
->ctrl_interface_gid_set
&&
3304 chown(hapd
->conf
->ctrl_interface
, -1,
3305 hapd
->conf
->ctrl_interface_gid
) < 0) {
3306 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
3311 if (!hapd
->conf
->ctrl_interface_gid_set
&&
3312 hapd
->iface
->interfaces
->ctrl_iface_group
&&
3313 chown(hapd
->conf
->ctrl_interface
, -1,
3314 hapd
->iface
->interfaces
->ctrl_iface_group
) < 0) {
3315 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
3322 * Android is using umask 0077 which would leave the control interface
3323 * directory without group access. This breaks things since Wi-Fi
3324 * framework assumes that this directory can be accessed by other
3325 * applications in the wifi group. Fix this by adding group access even
3326 * if umask value would prevent this.
3328 if (chmod(hapd
->conf
->ctrl_interface
, S_IRWXU
| S_IRWXG
) < 0) {
3329 wpa_printf(MSG_ERROR
, "CTRL: Could not chmod directory: %s",
3331 /* Try to continue anyway */
3333 #endif /* ANDROID */
3335 if (os_strlen(hapd
->conf
->ctrl_interface
) + 1 +
3336 os_strlen(hapd
->conf
->iface
) >= sizeof(addr
.sun_path
))
3339 s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
3341 wpa_printf(MSG_ERROR
, "socket(PF_UNIX): %s", strerror(errno
));
3345 os_memset(&addr
, 0, sizeof(addr
));
3347 addr
.sun_len
= sizeof(addr
);
3348 #endif /* __FreeBSD__ */
3349 addr
.sun_family
= AF_UNIX
;
3350 fname
= hostapd_ctrl_iface_path(hapd
);
3353 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
3354 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
3355 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
3357 if (connect(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
3358 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
3359 " allow connections - assuming it was left"
3360 "over from forced program termination");
3361 if (unlink(fname
) < 0) {
3362 wpa_printf(MSG_ERROR
,
3363 "Could not unlink existing ctrl_iface socket '%s': %s",
3364 fname
, strerror(errno
));
3367 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) <
3369 wpa_printf(MSG_ERROR
,
3370 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
3374 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
3375 "ctrl_iface socket '%s'", fname
);
3377 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
3378 "be in use - cannot override it");
3379 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
3380 "not used anymore", fname
);
3387 if (hapd
->conf
->ctrl_interface_gid_set
&&
3388 chown(fname
, -1, hapd
->conf
->ctrl_interface_gid
) < 0) {
3389 wpa_printf(MSG_ERROR
, "chown[ctrl_interface/ifname]: %s",
3394 if (!hapd
->conf
->ctrl_interface_gid_set
&&
3395 hapd
->iface
->interfaces
->ctrl_iface_group
&&
3396 chown(fname
, -1, hapd
->iface
->interfaces
->ctrl_iface_group
) < 0) {
3397 wpa_printf(MSG_ERROR
, "chown[ctrl_interface/ifname]: %s",
3402 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
3403 wpa_printf(MSG_ERROR
, "chmod[ctrl_interface/ifname]: %s",
3409 hapd
->ctrl_sock
= s
;
3410 if (eloop_register_read_sock(s
, hostapd_ctrl_iface_receive
, hapd
,
3412 hostapd_ctrl_iface_deinit(hapd
);
3415 hapd
->msg_ctx
= hapd
;
3416 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb
);
3428 #endif /* CONFIG_CTRL_IFACE_UDP */
3432 void hostapd_ctrl_iface_deinit(struct hostapd_data
*hapd
)
3434 struct wpa_ctrl_dst
*dst
, *prev
;
3436 if (hapd
->ctrl_sock
> -1) {
3437 #ifndef CONFIG_CTRL_IFACE_UDP
3439 #endif /* !CONFIG_CTRL_IFACE_UDP */
3441 eloop_unregister_read_sock(hapd
->ctrl_sock
);
3442 close(hapd
->ctrl_sock
);
3443 hapd
->ctrl_sock
= -1;
3444 #ifndef CONFIG_CTRL_IFACE_UDP
3445 fname
= hostapd_ctrl_iface_path(hapd
);
3450 if (hapd
->conf
->ctrl_interface
&&
3451 rmdir(hapd
->conf
->ctrl_interface
) < 0) {
3452 if (errno
== ENOTEMPTY
) {
3453 wpa_printf(MSG_DEBUG
, "Control interface "
3454 "directory not empty - leaving it "
3457 wpa_printf(MSG_ERROR
,
3458 "rmdir[ctrl_interface=%s]: %s",
3459 hapd
->conf
->ctrl_interface
,
3463 #endif /* !CONFIG_CTRL_IFACE_UDP */
3466 dl_list_for_each_safe(dst
, prev
, &hapd
->ctrl_dst
, struct wpa_ctrl_dst
,
3470 #ifdef CONFIG_TESTING_OPTIONS
3471 l2_packet_deinit(hapd
->l2_test
);
3472 hapd
->l2_test
= NULL
;
3473 #endif /* CONFIG_TESTING_OPTIONS */
3477 static int hostapd_ctrl_iface_add(struct hapd_interfaces
*interfaces
,
3480 if (hostapd_add_iface(interfaces
, buf
) < 0) {
3481 wpa_printf(MSG_ERROR
, "Adding interface %s failed", buf
);
3488 static int hostapd_ctrl_iface_remove(struct hapd_interfaces
*interfaces
,
3491 if (hostapd_remove_iface(interfaces
, buf
) < 0) {
3492 wpa_printf(MSG_ERROR
, "Removing interface %s failed", buf
);
3499 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces
*interfaces
,
3500 struct sockaddr_storage
*from
,
3503 return ctrl_iface_attach(&interfaces
->global_ctrl_dst
, from
, fromlen
);
3507 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces
*interfaces
,
3508 struct sockaddr_storage
*from
,
3511 return ctrl_iface_detach(&interfaces
->global_ctrl_dst
, from
, fromlen
);
3515 static void hostapd_ctrl_iface_flush(struct hapd_interfaces
*interfaces
)
3517 #ifdef CONFIG_WPS_TESTING
3518 wps_version_number
= 0x20;
3519 wps_testing_dummy_cred
= 0;
3520 wps_corrupt_pkhash
= 0;
3521 #endif /* CONFIG_WPS_TESTING */
3523 #ifdef CONFIG_TESTING_OPTIONS
3525 dpp_test
= DPP_TEST_DISABLED
;
3526 #endif /* CONFIG_DPP */
3527 #endif /* CONFIG_TESTING_OPTIONS */
3535 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces
*interfaces
,
3538 char ifname
[IFNAMSIZ
+ 1];
3539 struct fst_iface_cfg cfg
;
3540 struct hostapd_data
*hapd
;
3541 struct fst_wpa_obj iface_obj
;
3543 if (!fst_parse_attach_command(cmd
, ifname
, sizeof(ifname
), &cfg
)) {
3544 hapd
= hostapd_get_iface(interfaces
, ifname
);
3546 if (hapd
->iface
->fst
) {
3547 wpa_printf(MSG_INFO
, "FST: Already attached");
3550 fst_hostapd_fill_iface_obj(hapd
, &iface_obj
);
3551 hapd
->iface
->fst
= fst_attach(ifname
, hapd
->own_addr
,
3553 if (hapd
->iface
->fst
)
3563 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces
*interfaces
,
3566 char ifname
[IFNAMSIZ
+ 1];
3567 struct hostapd_data
* hapd
;
3569 if (!fst_parse_detach_command(cmd
, ifname
, sizeof(ifname
))) {
3570 hapd
= hostapd_get_iface(interfaces
, ifname
);
3572 if (!fst_iface_detach(ifname
)) {
3573 hapd
->iface
->fst
= NULL
;
3574 hapd
->iface
->fst_ies
= NULL
;
3583 #endif /* CONFIG_FST */
3586 static struct hostapd_data
*
3587 hostapd_interfaces_get_hapd(struct hapd_interfaces
*interfaces
,
3592 for (i
= 0; i
< interfaces
->count
; i
++) {
3593 struct hostapd_iface
*iface
= interfaces
->iface
[i
];
3595 for (j
= 0; j
< iface
->num_bss
; j
++) {
3596 struct hostapd_data
*hapd
;
3598 hapd
= iface
->bss
[j
];
3599 if (os_strcmp(ifname
, hapd
->conf
->iface
) == 0)
3608 static int hostapd_ctrl_iface_dup_param(struct hostapd_data
*src_hapd
,
3609 struct hostapd_data
*dst_hapd
,
3615 value
= os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN
);
3617 wpa_printf(MSG_ERROR
,
3618 "DUP: cannot allocate buffer to stringify %s",
3623 if (os_strcmp(param
, "wpa") == 0) {
3624 os_snprintf(value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
, "%d",
3625 src_hapd
->conf
->wpa
);
3626 } else if (os_strcmp(param
, "wpa_key_mgmt") == 0 &&
3627 src_hapd
->conf
->wpa_key_mgmt
) {
3628 res
= hostapd_ctrl_iface_get_key_mgmt(
3629 src_hapd
, value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
);
3630 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN
, res
))
3631 goto error_stringify
;
3632 } else if (os_strcmp(param
, "wpa_pairwise") == 0 &&
3633 src_hapd
->conf
->wpa_pairwise
) {
3634 res
= wpa_write_ciphers(value
,
3635 value
+ HOSTAPD_CLI_DUP_VALUE_MAX_LEN
,
3636 src_hapd
->conf
->wpa_pairwise
, " ");
3638 goto error_stringify
;
3639 } else if (os_strcmp(param
, "rsn_pairwise") == 0 &&
3640 src_hapd
->conf
->rsn_pairwise
) {
3641 res
= wpa_write_ciphers(value
,
3642 value
+ HOSTAPD_CLI_DUP_VALUE_MAX_LEN
,
3643 src_hapd
->conf
->rsn_pairwise
, " ");
3645 goto error_stringify
;
3646 } else if (os_strcmp(param
, "wpa_passphrase") == 0 &&
3647 src_hapd
->conf
->ssid
.wpa_passphrase
) {
3648 os_snprintf(value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
, "%s",
3649 src_hapd
->conf
->ssid
.wpa_passphrase
);
3650 } else if (os_strcmp(param
, "wpa_psk") == 0 &&
3651 src_hapd
->conf
->ssid
.wpa_psk_set
) {
3652 wpa_snprintf_hex(value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
,
3653 src_hapd
->conf
->ssid
.wpa_psk
->psk
, PMK_LEN
);
3655 wpa_printf(MSG_WARNING
, "DUP: %s cannot be duplicated", param
);
3659 res
= hostapd_set_iface(dst_hapd
->iconf
, dst_hapd
->conf
, param
, value
);
3664 wpa_printf(MSG_ERROR
, "DUP: cannot stringify %s", param
);
3672 hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces
*interfaces
,
3674 char *reply
, int reply_size
)
3679 struct hostapd_iface
*iface
;
3683 show_ctrl
= !!os_strstr(input
, "ctrl");
3686 end
= reply
+ reply_size
;
3688 for (i
= 0; i
< interfaces
->count
; i
++) {
3689 iface
= interfaces
->iface
[i
];
3691 for (j
= 0; j
< iface
->num_bss
; j
++) {
3692 struct hostapd_bss_config
*conf
;
3694 conf
= iface
->conf
->bss
[j
];
3696 res
= os_snprintf(pos
, end
- pos
,
3697 "%s ctrl_iface=%s\n",
3699 conf
->ctrl_interface
?
3700 conf
->ctrl_interface
: "N/A");
3702 res
= os_snprintf(pos
, end
- pos
, "%s\n",
3704 if (os_snprintf_error(end
- pos
, res
)) {
3717 hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces
*interfaces
,
3720 char *p_start
= cmd
, *p_end
;
3721 struct hostapd_data
*src_hapd
, *dst_hapd
;
3723 /* cmd: "<src ifname> <dst ifname> <variable name> */
3725 p_end
= os_strchr(p_start
, ' ');
3727 wpa_printf(MSG_ERROR
, "DUP: no src ifname found in cmd: '%s'",
3733 src_hapd
= hostapd_interfaces_get_hapd(interfaces
, p_start
);
3735 wpa_printf(MSG_ERROR
, "DUP: no src ifname found: '%s'",
3740 p_start
= p_end
+ 1;
3741 p_end
= os_strchr(p_start
, ' ');
3743 wpa_printf(MSG_ERROR
, "DUP: no dst ifname found in cmd: '%s'",
3749 dst_hapd
= hostapd_interfaces_get_hapd(interfaces
, p_start
);
3751 wpa_printf(MSG_ERROR
, "DUP: no dst ifname found: '%s'",
3756 p_start
= p_end
+ 1;
3757 return hostapd_ctrl_iface_dup_param(src_hapd
, dst_hapd
, p_start
);
3761 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces
*interfaces
,
3763 char *buf
, char *reply
,
3765 struct sockaddr_storage
*from
,
3768 struct hostapd_data
*hapd
;
3770 hapd
= hostapd_interfaces_get_hapd(interfaces
, ifname
);
3774 res
= os_snprintf(reply
, reply_size
, "FAIL-NO-IFNAME-MATCH\n");
3775 if (os_snprintf_error(reply_size
, res
))
3780 return hostapd_ctrl_iface_receive_process(hapd
, buf
, reply
,reply_size
,
3785 static void hostapd_global_ctrl_iface_receive(int sock
, void *eloop_ctx
,
3788 void *interfaces
= eloop_ctx
;
3789 char buffer
[256], *buf
= buffer
;
3791 struct sockaddr_storage from
;
3792 socklen_t fromlen
= sizeof(from
);
3795 const int reply_size
= 4096;
3796 #ifdef CONFIG_CTRL_IFACE_UDP
3797 unsigned char lcookie
[COOKIE_LEN
];
3798 #endif /* CONFIG_CTRL_IFACE_UDP */
3800 res
= recvfrom(sock
, buffer
, sizeof(buffer
) - 1, 0,
3801 (struct sockaddr
*) &from
, &fromlen
);
3803 wpa_printf(MSG_ERROR
, "recvfrom(ctrl_iface): %s",
3808 wpa_printf(MSG_DEBUG
, "Global ctrl_iface command: %s", buf
);
3810 reply
= os_malloc(reply_size
);
3811 if (reply
== NULL
) {
3812 if (sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
3814 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
3820 os_memcpy(reply
, "OK\n", 3);
3823 #ifdef CONFIG_CTRL_IFACE_UDP
3824 if (os_strcmp(buf
, "GET_COOKIE") == 0) {
3825 os_memcpy(reply
, "COOKIE=", 7);
3826 wpa_snprintf_hex(reply
+ 7, 2 * COOKIE_LEN
+ 1,
3827 gcookie
, COOKIE_LEN
);
3828 reply_len
= 7 + 2 * COOKIE_LEN
;
3832 if (os_strncmp(buf
, "COOKIE=", 7) != 0 ||
3833 hexstr2bin(buf
+ 7, lcookie
, COOKIE_LEN
) < 0) {
3834 wpa_printf(MSG_DEBUG
,
3835 "CTRL: No cookie in the request - drop request");
3840 if (os_memcmp(gcookie
, lcookie
, COOKIE_LEN
) != 0) {
3841 wpa_printf(MSG_DEBUG
,
3842 "CTRL: Invalid cookie in the request - drop request");
3847 buf
+= 7 + 2 * COOKIE_LEN
;
3850 #endif /* CONFIG_CTRL_IFACE_UDP */
3852 if (os_strncmp(buf
, "IFNAME=", 7) == 0) {
3853 char *pos
= os_strchr(buf
+ 7, ' ');
3857 reply_len
= hostapd_global_ctrl_iface_ifname(
3858 interfaces
, buf
+ 7, pos
, reply
, reply_size
,
3864 if (os_strcmp(buf
, "PING") == 0) {
3865 os_memcpy(reply
, "PONG\n", 5);
3867 } else if (os_strncmp(buf
, "RELOG", 5) == 0) {
3868 if (wpa_debug_reopen_file() < 0)
3870 } else if (os_strcmp(buf
, "FLUSH") == 0) {
3871 hostapd_ctrl_iface_flush(interfaces
);
3872 } else if (os_strncmp(buf
, "ADD ", 4) == 0) {
3873 if (hostapd_ctrl_iface_add(interfaces
, buf
+ 4) < 0)
3875 } else if (os_strncmp(buf
, "REMOVE ", 7) == 0) {
3876 if (hostapd_ctrl_iface_remove(interfaces
, buf
+ 7) < 0)
3878 } else if (os_strcmp(buf
, "ATTACH") == 0) {
3879 if (hostapd_global_ctrl_iface_attach(interfaces
, &from
,
3882 } else if (os_strcmp(buf
, "DETACH") == 0) {
3883 if (hostapd_global_ctrl_iface_detach(interfaces
, &from
,
3886 #ifdef CONFIG_MODULE_TESTS
3887 } else if (os_strcmp(buf
, "MODULE_TESTS") == 0) {
3888 if (hapd_module_tests() < 0)
3890 #endif /* CONFIG_MODULE_TESTS */
3892 } else if (os_strncmp(buf
, "FST-ATTACH ", 11) == 0) {
3893 if (!hostapd_global_ctrl_iface_fst_attach(interfaces
, buf
+ 11))
3894 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
3897 } else if (os_strncmp(buf
, "FST-DETACH ", 11) == 0) {
3898 if (!hostapd_global_ctrl_iface_fst_detach(interfaces
, buf
+ 11))
3899 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
3902 } else if (os_strncmp(buf
, "FST-MANAGER ", 12) == 0) {
3903 reply_len
= fst_ctrl_iface_receive(buf
+ 12, reply
, reply_size
);
3904 #endif /* CONFIG_FST */
3905 } else if (os_strncmp(buf
, "DUP_NETWORK ", 12) == 0) {
3906 if (!hostapd_global_ctrl_iface_dup_network(interfaces
,
3908 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
3911 } else if (os_strncmp(buf
, "INTERFACES", 10) == 0) {
3912 reply_len
= hostapd_global_ctrl_iface_interfaces(
3913 interfaces
, buf
+ 10, reply
, sizeof(buffer
));
3914 } else if (os_strcmp(buf
, "TERMINATE") == 0) {
3917 wpa_printf(MSG_DEBUG
, "Unrecognized global ctrl_iface command "
3923 if (reply_len
< 0) {
3924 os_memcpy(reply
, "FAIL\n", 5);
3928 if (sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
3930 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
3937 #ifndef CONFIG_CTRL_IFACE_UDP
3938 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces
*interface
)
3943 if (interface
->global_iface_path
== NULL
)
3946 len
= os_strlen(interface
->global_iface_path
) +
3947 os_strlen(interface
->global_iface_name
) + 2;
3948 buf
= os_malloc(len
);
3952 os_snprintf(buf
, len
, "%s/%s", interface
->global_iface_path
,
3953 interface
->global_iface_name
);
3954 buf
[len
- 1] = '\0';
3957 #endif /* CONFIG_CTRL_IFACE_UDP */
3960 int hostapd_global_ctrl_iface_init(struct hapd_interfaces
*interface
)
3962 #ifdef CONFIG_CTRL_IFACE_UDP
3963 int port
= HOSTAPD_GLOBAL_CTRL_IFACE_PORT
;
3966 struct addrinfo hints
= { 0 }, *res
, *saveres
;
3969 if (interface
->global_ctrl_sock
> -1) {
3970 wpa_printf(MSG_DEBUG
, "ctrl_iface already exists!");
3974 if (interface
->global_iface_path
== NULL
)
3977 pos
= os_strstr(interface
->global_iface_path
, "udp:");
3982 wpa_printf(MSG_ERROR
, "Invalid global ctrl UDP port");
3987 os_get_random(gcookie
, COOKIE_LEN
);
3989 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3990 hints
.ai_flags
= AI_PASSIVE
;
3991 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3993 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3994 hints
.ai_family
= AF_INET6
;
3995 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3996 hints
.ai_family
= AF_INET
;
3997 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3998 hints
.ai_socktype
= SOCK_DGRAM
;
4001 os_snprintf(p
, sizeof(p
), "%d", port
);
4002 n
= getaddrinfo(NULL
, p
, &hints
, &res
);
4004 wpa_printf(MSG_ERROR
, "getaddrinfo(): %s", gai_strerror(n
));
4009 interface
->global_ctrl_sock
= socket(res
->ai_family
, res
->ai_socktype
,
4011 if (interface
->global_ctrl_sock
< 0) {
4012 wpa_printf(MSG_ERROR
, "socket(PF_INET): %s", strerror(errno
));
4016 if (bind(interface
->global_ctrl_sock
, res
->ai_addr
, res
->ai_addrlen
) <
4019 if ((port
- HOSTAPD_GLOBAL_CTRL_IFACE_PORT
) <
4020 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT
&& !pos
)
4022 wpa_printf(MSG_ERROR
, "bind(AF_INET): %s", strerror(errno
));
4026 freeaddrinfo(saveres
);
4028 wpa_printf(MSG_DEBUG
, "global ctrl_iface_init UDP port: %d", port
);
4030 if (eloop_register_read_sock(interface
->global_ctrl_sock
,
4031 hostapd_global_ctrl_iface_receive
,
4032 interface
, NULL
) < 0) {
4033 hostapd_global_ctrl_iface_deinit(interface
);
4040 if (interface
->global_ctrl_sock
>= 0)
4041 close(interface
->global_ctrl_sock
);
4043 #else /* CONFIG_CTRL_IFACE_UDP */
4044 struct sockaddr_un addr
;
4048 if (interface
->global_iface_path
== NULL
) {
4049 wpa_printf(MSG_DEBUG
, "ctrl_iface not configured!");
4053 if (mkdir(interface
->global_iface_path
, S_IRWXU
| S_IRWXG
) < 0) {
4054 if (errno
== EEXIST
) {
4055 wpa_printf(MSG_DEBUG
, "Using existing control "
4056 "interface directory.");
4058 wpa_printf(MSG_ERROR
, "mkdir[ctrl_interface]: %s",
4062 } else if (interface
->ctrl_iface_group
&&
4063 chown(interface
->global_iface_path
, -1,
4064 interface
->ctrl_iface_group
) < 0) {
4065 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
4070 if (os_strlen(interface
->global_iface_path
) + 1 +
4071 os_strlen(interface
->global_iface_name
) >= sizeof(addr
.sun_path
))
4074 s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
4076 wpa_printf(MSG_ERROR
, "socket(PF_UNIX): %s", strerror(errno
));
4080 os_memset(&addr
, 0, sizeof(addr
));
4082 addr
.sun_len
= sizeof(addr
);
4083 #endif /* __FreeBSD__ */
4084 addr
.sun_family
= AF_UNIX
;
4085 fname
= hostapd_global_ctrl_iface_path(interface
);
4088 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
4089 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
4090 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
4092 if (connect(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
4093 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
4094 " allow connections - assuming it was left"
4095 "over from forced program termination");
4096 if (unlink(fname
) < 0) {
4097 wpa_printf(MSG_ERROR
,
4098 "Could not unlink existing ctrl_iface socket '%s': %s",
4099 fname
, strerror(errno
));
4102 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) <
4104 wpa_printf(MSG_ERROR
, "bind(PF_UNIX): %s",
4108 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
4109 "ctrl_iface socket '%s'", fname
);
4111 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
4112 "be in use - cannot override it");
4113 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
4114 "not used anymore", fname
);
4121 if (interface
->ctrl_iface_group
&&
4122 chown(fname
, -1, interface
->ctrl_iface_group
) < 0) {
4123 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
4128 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
4129 wpa_printf(MSG_ERROR
, "chmod[ctrl_interface/ifname]: %s",
4135 interface
->global_ctrl_sock
= s
;
4136 eloop_register_read_sock(s
, hostapd_global_ctrl_iface_receive
,
4149 #endif /* CONFIG_CTRL_IFACE_UDP */
4153 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces
*interfaces
)
4155 #ifndef CONFIG_CTRL_IFACE_UDP
4157 #endif /* CONFIG_CTRL_IFACE_UDP */
4158 struct wpa_ctrl_dst
*dst
, *prev
;
4160 if (interfaces
->global_ctrl_sock
> -1) {
4161 eloop_unregister_read_sock(interfaces
->global_ctrl_sock
);
4162 close(interfaces
->global_ctrl_sock
);
4163 interfaces
->global_ctrl_sock
= -1;
4164 #ifndef CONFIG_CTRL_IFACE_UDP
4165 fname
= hostapd_global_ctrl_iface_path(interfaces
);
4171 if (interfaces
->global_iface_path
&&
4172 rmdir(interfaces
->global_iface_path
) < 0) {
4173 if (errno
== ENOTEMPTY
) {
4174 wpa_printf(MSG_DEBUG
, "Control interface "
4175 "directory not empty - leaving it "
4178 wpa_printf(MSG_ERROR
,
4179 "rmdir[ctrl_interface=%s]: %s",
4180 interfaces
->global_iface_path
,
4184 #endif /* CONFIG_CTRL_IFACE_UDP */
4187 os_free(interfaces
->global_iface_path
);
4188 interfaces
->global_iface_path
= NULL
;
4190 dl_list_for_each_safe(dst
, prev
, &interfaces
->global_ctrl_dst
,
4191 struct wpa_ctrl_dst
, list
)
4196 static void hostapd_ctrl_iface_send(struct hostapd_data
*hapd
, int level
,
4197 enum wpa_msg_type type
,
4198 const char *buf
, size_t len
)
4200 struct wpa_ctrl_dst
*dst
, *next
;
4201 struct dl_list
*ctrl_dst
;
4208 if (type
!= WPA_MSG_ONLY_GLOBAL
) {
4209 s
= hapd
->ctrl_sock
;
4210 ctrl_dst
= &hapd
->ctrl_dst
;
4212 s
= hapd
->iface
->interfaces
->global_ctrl_sock
;
4213 ctrl_dst
= &hapd
->iface
->interfaces
->global_ctrl_dst
;
4216 if (s
< 0 || dl_list_empty(ctrl_dst
))
4219 os_snprintf(levelstr
, sizeof(levelstr
), "<%d>", level
);
4220 io
[0].iov_base
= levelstr
;
4221 io
[0].iov_len
= os_strlen(levelstr
);
4222 io
[1].iov_base
= (char *) buf
;
4223 io
[1].iov_len
= len
;
4224 os_memset(&msg
, 0, sizeof(msg
));
4229 dl_list_for_each_safe(dst
, next
, ctrl_dst
, struct wpa_ctrl_dst
, list
) {
4230 if (level
>= dst
->debug_level
) {
4231 sockaddr_print(MSG_DEBUG
, "CTRL_IFACE monitor send",
4232 &dst
->addr
, dst
->addrlen
);
4233 msg
.msg_name
= &dst
->addr
;
4234 msg
.msg_namelen
= dst
->addrlen
;
4235 if (sendmsg(s
, &msg
, 0) < 0) {
4237 wpa_printf(MSG_INFO
, "CTRL_IFACE monitor[%d]: "
4239 idx
, errno
, strerror(errno
));
4241 if (dst
->errors
> 10 || _errno
== ENOENT
) {
4242 if (type
!= WPA_MSG_ONLY_GLOBAL
)
4243 hostapd_ctrl_iface_detach(
4247 hostapd_global_ctrl_iface_detach(
4248 hapd
->iface
->interfaces
,
4259 #endif /* CONFIG_NATIVE_WINDOWS */