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 "crypto/tls.h"
33 #include "drivers/driver.h"
34 #include "eapol_auth/eapol_auth_sm.h"
35 #include "radius/radius_client.h"
36 #include "radius/radius_server.h"
37 #include "l2_packet/l2_packet.h"
38 #include "ap/hostapd.h"
39 #include "ap/ap_config.h"
40 #include "ap/ieee802_1x.h"
41 #include "ap/wpa_auth.h"
42 #include "ap/ieee802_11.h"
43 #include "ap/sta_info.h"
44 #include "ap/wps_hostapd.h"
45 #include "ap/ctrl_iface_ap.h"
46 #include "ap/ap_drv_ops.h"
48 #include "ap/wnm_ap.h"
49 #include "ap/wpa_auth.h"
50 #include "ap/beacon.h"
51 #include "ap/neighbor_db.h"
53 #include "wps/wps_defs.h"
55 #include "fst/fst_ctrl_iface.h"
56 #include "config_file.h"
57 #include "ctrl_iface.h"
60 #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
62 #ifdef CONFIG_CTRL_IFACE_UDP
64 static unsigned char cookie
[COOKIE_LEN
];
65 static unsigned char gcookie
[COOKIE_LEN
];
66 #define HOSTAPD_CTRL_IFACE_PORT 8877
67 #define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
68 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
69 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
70 #endif /* CONFIG_CTRL_IFACE_UDP */
72 static void hostapd_ctrl_iface_send(struct hostapd_data
*hapd
, int level
,
73 enum wpa_msg_type type
,
74 const char *buf
, size_t len
);
77 static int hostapd_ctrl_iface_attach(struct hostapd_data
*hapd
,
78 struct sockaddr_storage
*from
,
81 return ctrl_iface_attach(&hapd
->ctrl_dst
, from
, fromlen
);
85 static int hostapd_ctrl_iface_detach(struct hostapd_data
*hapd
,
86 struct sockaddr_storage
*from
,
89 return ctrl_iface_detach(&hapd
->ctrl_dst
, from
, fromlen
);
93 static int hostapd_ctrl_iface_level(struct hostapd_data
*hapd
,
94 struct sockaddr_storage
*from
,
98 return ctrl_iface_level(&hapd
->ctrl_dst
, from
, fromlen
, level
);
102 static int hostapd_ctrl_iface_new_sta(struct hostapd_data
*hapd
,
106 struct sta_info
*sta
;
108 wpa_printf(MSG_DEBUG
, "CTRL_IFACE NEW_STA %s", txtaddr
);
110 if (hwaddr_aton(txtaddr
, addr
))
113 sta
= ap_get_sta(hapd
, addr
);
117 wpa_printf(MSG_DEBUG
, "Add new STA " MACSTR
" based on ctrl_iface "
118 "notification", MAC2STR(addr
));
119 sta
= ap_sta_add(hapd
, addr
);
123 hostapd_new_assoc_sta(hapd
, sta
, 0);
128 #ifdef CONFIG_IEEE80211W
130 static int hostapd_ctrl_iface_sa_query(struct hostapd_data
*hapd
,
134 u8 trans_id
[WLAN_SA_QUERY_TR_ID_LEN
];
136 wpa_printf(MSG_DEBUG
, "CTRL_IFACE SA_QUERY %s", txtaddr
);
138 if (hwaddr_aton(txtaddr
, addr
) ||
139 os_get_random(trans_id
, WLAN_SA_QUERY_TR_ID_LEN
) < 0)
142 ieee802_11_send_sa_query_req(hapd
, addr
, trans_id
);
146 #endif /* NEED_AP_MLME */
147 #endif /* CONFIG_IEEE80211W */
151 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data
*hapd
, char *txt
)
153 char *pin
= os_strchr(txt
, ' ');
156 u8 addr_buf
[ETH_ALEN
], *addr
= NULL
;
163 timeout_txt
= os_strchr(pin
, ' ');
165 *timeout_txt
++ = '\0';
166 timeout
= atoi(timeout_txt
);
167 pos
= os_strchr(timeout_txt
, ' ');
170 if (hwaddr_aton(pos
, addr_buf
) == 0)
176 return hostapd_wps_add_pin(hapd
, addr
, txt
, pin
, timeout
);
180 static int hostapd_ctrl_iface_wps_check_pin(
181 struct hostapd_data
*hapd
, char *cmd
, char *buf
, size_t buflen
)
188 wpa_hexdump_ascii_key(MSG_DEBUG
, "WPS_CHECK_PIN",
189 (u8
*) cmd
, os_strlen(cmd
));
190 for (pos
= cmd
, len
= 0; *pos
!= '\0'; pos
++) {
191 if (*pos
< '0' || *pos
> '9')
195 wpa_printf(MSG_DEBUG
, "WPS: Too long PIN");
199 if (len
!= 4 && len
!= 8) {
200 wpa_printf(MSG_DEBUG
, "WPS: Invalid PIN length %d", (int) len
);
206 unsigned int pin_val
;
208 if (!wps_pin_valid(pin_val
)) {
209 wpa_printf(MSG_DEBUG
, "WPS: Invalid checksum digit");
210 ret
= os_snprintf(buf
, buflen
, "FAIL-CHECKSUM\n");
211 if (os_snprintf_error(buflen
, ret
))
217 ret
= os_snprintf(buf
, buflen
, "%s", pin
);
218 if (os_snprintf_error(buflen
, ret
))
225 #ifdef CONFIG_WPS_NFC
226 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data
*hapd
,
233 len
= os_strlen(pos
);
238 buf
= wpabuf_alloc(len
);
241 if (hexstr2bin(pos
, wpabuf_put(buf
, len
), len
) < 0) {
246 ret
= hostapd_wps_nfc_tag_read(hapd
, buf
);
253 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data
*hapd
,
254 char *cmd
, char *reply
,
261 if (os_strcmp(cmd
, "WPS") == 0)
263 else if (os_strcmp(cmd
, "NDEF") == 0)
268 buf
= hostapd_wps_nfc_config_token(hapd
, ndef
);
272 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
283 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data
*hapd
,
284 char *reply
, size_t max_len
,
290 buf
= hostapd_wps_nfc_token_gen(hapd
, ndef
);
294 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
305 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data
*hapd
,
306 char *cmd
, char *reply
,
309 if (os_strcmp(cmd
, "WPS") == 0)
310 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd
, reply
,
313 if (os_strcmp(cmd
, "NDEF") == 0)
314 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd
, reply
,
317 if (os_strcmp(cmd
, "enable") == 0)
318 return hostapd_wps_nfc_token_enable(hapd
);
320 if (os_strcmp(cmd
, "disable") == 0) {
321 hostapd_wps_nfc_token_disable(hapd
);
329 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data
*hapd
,
330 char *cmd
, char *reply
,
338 pos
= os_strchr(cmd
, ' ');
343 if (os_strcmp(cmd
, "WPS") == 0)
345 else if (os_strcmp(cmd
, "NDEF") == 0)
350 if (os_strcmp(pos
, "WPS-CR") == 0)
351 buf
= hostapd_wps_nfc_hs_cr(hapd
, ndef
);
357 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
368 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data
*hapd
,
372 struct wpabuf
*req
, *sel
;
374 char *pos
, *role
, *type
, *pos2
;
377 pos
= os_strchr(role
, ' ');
383 pos
= os_strchr(type
, ' ');
388 pos2
= os_strchr(pos
, ' ');
393 len
= os_strlen(pos
);
398 req
= wpabuf_alloc(len
);
401 if (hexstr2bin(pos
, wpabuf_put(req
, len
), len
) < 0) {
406 len
= os_strlen(pos2
);
413 sel
= wpabuf_alloc(len
);
418 if (hexstr2bin(pos2
, wpabuf_put(sel
, len
), len
) < 0) {
424 if (os_strcmp(role
, "RESP") == 0 && os_strcmp(type
, "WPS") == 0) {
425 ret
= hostapd_wps_nfc_report_handover(hapd
, req
, sel
);
427 wpa_printf(MSG_DEBUG
, "NFC: Unsupported connection handover "
428 "reported: role=%s type=%s", role
, type
);
437 #endif /* CONFIG_WPS_NFC */
440 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data
*hapd
, char *txt
,
441 char *buf
, size_t buflen
)
447 pos
= os_strchr(txt
, ' ');
451 if (os_strcmp(txt
, "disable") == 0) {
452 hostapd_wps_ap_pin_disable(hapd
);
453 return os_snprintf(buf
, buflen
, "OK\n");
456 if (os_strcmp(txt
, "random") == 0) {
459 pin_txt
= hostapd_wps_ap_pin_random(hapd
, timeout
);
462 return os_snprintf(buf
, buflen
, "%s", pin_txt
);
465 if (os_strcmp(txt
, "get") == 0) {
466 pin_txt
= hostapd_wps_ap_pin_get(hapd
);
469 return os_snprintf(buf
, buflen
, "%s", pin_txt
);
472 if (os_strcmp(txt
, "set") == 0) {
477 pos
= os_strchr(pos
, ' ');
482 if (os_strlen(pin
) > buflen
)
484 if (hostapd_wps_ap_pin_set(hapd
, pin
, timeout
) < 0)
486 return os_snprintf(buf
, buflen
, "%s", pin
);
493 static int hostapd_ctrl_iface_wps_config(struct hostapd_data
*hapd
, char *txt
)
496 char *ssid
, *auth
, *encr
= NULL
, *key
= NULL
;
499 pos
= os_strchr(txt
, ' ');
505 pos
= os_strchr(pos
, ' ');
509 pos
= os_strchr(pos
, ' ');
516 return hostapd_wps_config_ap(hapd
, ssid
, auth
, encr
, key
);
520 static const char * pbc_status_str(enum pbc_status status
)
523 case WPS_PBC_STATUS_DISABLE
:
525 case WPS_PBC_STATUS_ACTIVE
:
527 case WPS_PBC_STATUS_TIMEOUT
:
529 case WPS_PBC_STATUS_OVERLAP
:
537 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data
*hapd
,
538 char *buf
, size_t buflen
)
546 ret
= os_snprintf(pos
, end
- pos
, "PBC Status: %s\n",
547 pbc_status_str(hapd
->wps_stats
.pbc_status
));
549 if (os_snprintf_error(end
- pos
, ret
))
553 ret
= os_snprintf(pos
, end
- pos
, "Last WPS result: %s\n",
554 (hapd
->wps_stats
.status
== WPS_STATUS_SUCCESS
?
556 (hapd
->wps_stats
.status
== WPS_STATUS_FAILURE
?
557 "Failed" : "None")));
559 if (os_snprintf_error(end
- pos
, ret
))
563 /* If status == Failure - Add possible Reasons */
564 if(hapd
->wps_stats
.status
== WPS_STATUS_FAILURE
&&
565 hapd
->wps_stats
.failure_reason
> 0) {
566 ret
= os_snprintf(pos
, end
- pos
,
567 "Failure Reason: %s\n",
568 wps_ei_str(hapd
->wps_stats
.failure_reason
));
570 if (os_snprintf_error(end
- pos
, ret
))
575 if (hapd
->wps_stats
.status
) {
576 ret
= os_snprintf(pos
, end
- pos
, "Peer Address: " MACSTR
"\n",
577 MAC2STR(hapd
->wps_stats
.peer_addr
));
579 if (os_snprintf_error(end
- pos
, ret
))
587 #endif /* CONFIG_WPS */
591 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data
*hapd
,
597 if (hwaddr_aton(cmd
, addr
))
610 return hs20_send_wnm_notification(hapd
, addr
, 1, url
);
614 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data
*hapd
,
618 int code
, reauth_delay
, ret
;
623 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
624 if (hwaddr_aton(cmd
, addr
))
627 pos
= os_strchr(cmd
, ' ');
633 pos
= os_strchr(pos
, ' ');
637 reauth_delay
= atoi(pos
);
640 pos
= os_strchr(pos
, ' ');
643 url_len
= os_strlen(pos
);
646 req
= wpabuf_alloc(4 + url_len
);
649 wpabuf_put_u8(req
, code
);
650 wpabuf_put_le16(req
, reauth_delay
);
651 wpabuf_put_u8(req
, url_len
);
653 wpabuf_put_data(req
, pos
, url_len
);
655 wpa_printf(MSG_DEBUG
, "HS 2.0: Send WNM-Notification to " MACSTR
656 " to indicate imminent deauthentication (code=%d "
657 "reauth_delay=%d)", MAC2STR(addr
), code
, reauth_delay
);
658 ret
= hs20_send_wnm_notification_deauth_req(hapd
, addr
, req
);
663 #endif /* CONFIG_HS20 */
666 #ifdef CONFIG_INTERWORKING
668 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data
*hapd
,
671 u8 qos_map_set
[16 + 2 * 21], count
= 0;
672 const char *pos
= cmd
;
676 if (count
== sizeof(qos_map_set
)) {
677 wpa_printf(MSG_ERROR
, "Too many qos_map_set parameters");
682 if (val
< 0 || val
> 255) {
683 wpa_printf(MSG_INFO
, "Invalid QoS Map Set");
687 qos_map_set
[count
++] = val
;
688 pos
= os_strchr(pos
, ',');
694 if (count
< 16 || count
& 1) {
695 wpa_printf(MSG_INFO
, "Invalid QoS Map Set");
699 ret
= hostapd_drv_set_qos_map(hapd
, qos_map_set
, count
);
701 wpa_printf(MSG_INFO
, "Failed to set QoS Map Set");
705 os_memcpy(hapd
->conf
->qos_map_set
, qos_map_set
, count
);
706 hapd
->conf
->qos_map_set_len
= count
;
712 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data
*hapd
,
716 struct sta_info
*sta
;
718 u8
*qos_map_set
= hapd
->conf
->qos_map_set
;
719 u8 qos_map_set_len
= hapd
->conf
->qos_map_set_len
;
722 if (!qos_map_set_len
) {
723 wpa_printf(MSG_INFO
, "QoS Map Set is not set");
727 if (hwaddr_aton(cmd
, addr
))
730 sta
= ap_get_sta(hapd
, addr
);
732 wpa_printf(MSG_DEBUG
, "Station " MACSTR
" not found "
733 "for QoS Map Configuration message",
738 if (!sta
->qos_map_enabled
) {
739 wpa_printf(MSG_DEBUG
, "Station " MACSTR
" did not indicate "
740 "support for QoS Map", MAC2STR(addr
));
744 buf
= wpabuf_alloc(2 + 2 + qos_map_set_len
);
748 wpabuf_put_u8(buf
, WLAN_ACTION_QOS
);
749 wpabuf_put_u8(buf
, QOS_QOS_MAP_CONFIG
);
751 /* QoS Map Set Element */
752 wpabuf_put_u8(buf
, WLAN_EID_QOS_MAP_SET
);
753 wpabuf_put_u8(buf
, qos_map_set_len
);
754 wpabuf_put_data(buf
, qos_map_set
, qos_map_set_len
);
756 ret
= hostapd_drv_send_action(hapd
, hapd
->iface
->freq
, 0, addr
,
757 wpabuf_head(buf
), wpabuf_len(buf
));
763 #endif /* CONFIG_INTERWORKING */
768 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data
*hapd
,
773 struct sta_info
*sta
;
775 if (hwaddr_aton(cmd
, addr
))
779 disassoc_timer
= atoi(cmd
+ 17);
781 sta
= ap_get_sta(hapd
, addr
);
783 wpa_printf(MSG_DEBUG
, "Station " MACSTR
784 " not found for disassociation imminent message",
789 return wnm_send_disassoc_imminent(hapd
, sta
, disassoc_timer
);
793 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data
*hapd
,
797 const char *url
, *timerstr
;
799 struct sta_info
*sta
;
801 if (hwaddr_aton(cmd
, addr
))
804 sta
= ap_get_sta(hapd
, addr
);
806 wpa_printf(MSG_DEBUG
, "Station " MACSTR
807 " not found for ESS disassociation imminent message",
813 if (*timerstr
!= ' ')
816 disassoc_timer
= atoi(timerstr
);
817 if (disassoc_timer
< 0 || disassoc_timer
> 65535)
820 url
= os_strchr(timerstr
, ' ');
825 return wnm_send_ess_disassoc_imminent(hapd
, sta
, url
, disassoc_timer
);
829 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data
*hapd
,
833 const char *pos
, *end
;
834 int disassoc_timer
= 0;
835 struct sta_info
*sta
;
836 u8 req_mode
= 0, valid_int
= 0x01;
845 if (hwaddr_aton(cmd
, addr
)) {
846 wpa_printf(MSG_DEBUG
, "Invalid STA MAC address");
850 sta
= ap_get_sta(hapd
, addr
);
852 wpa_printf(MSG_DEBUG
, "Station " MACSTR
853 " not found for BSS TM Request message",
858 pos
= os_strstr(cmd
, " disassoc_timer=");
861 disassoc_timer
= atoi(pos
);
862 if (disassoc_timer
< 0 || disassoc_timer
> 65535) {
863 wpa_printf(MSG_DEBUG
, "Invalid disassoc_timer");
868 pos
= os_strstr(cmd
, " valid_int=");
871 valid_int
= atoi(pos
);
874 pos
= os_strstr(cmd
, " bss_term=");
877 req_mode
|= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED
;
878 /* TODO: TSF configurable/learnable */
879 bss_term_dur
[0] = 4; /* Subelement ID */
880 bss_term_dur
[1] = 10; /* Length */
881 os_memset(bss_term_dur
, 2, 8);
882 end
= os_strchr(pos
, ',');
884 wpa_printf(MSG_DEBUG
, "Invalid bss_term data");
888 WPA_PUT_LE16(&bss_term_dur
[10], atoi(end
));
891 nei_len
= ieee802_11_parse_candidate_list(cmd
, nei_rep
,
896 pos
= os_strstr(cmd
, " url=");
900 end
= os_strchr(pos
, ' ');
904 len
= os_strlen(pos
);
905 url
= os_malloc(len
+ 1);
908 os_memcpy(url
, pos
, len
);
910 req_mode
|= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT
;
913 if (os_strstr(cmd
, " pref=1"))
914 req_mode
|= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED
;
915 if (os_strstr(cmd
, " abridged=1"))
916 req_mode
|= WNM_BSS_TM_REQ_ABRIDGED
;
917 if (os_strstr(cmd
, " disassoc_imminent=1"))
918 req_mode
|= WNM_BSS_TM_REQ_DISASSOC_IMMINENT
;
921 pos
= os_strstr(cmd
, "mbo=");
923 unsigned int mbo_reason
, cell_pref
, reassoc_delay
;
926 ret
= sscanf(pos
, "mbo=%u:%u:%u", &mbo_reason
,
927 &reassoc_delay
, &cell_pref
);
929 wpa_printf(MSG_DEBUG
,
930 "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
935 if (mbo_reason
> MBO_TRANSITION_REASON_PREMIUM_AP
) {
936 wpa_printf(MSG_DEBUG
,
937 "Invalid MBO transition reason code %u",
943 /* Valid values for Cellular preference are: 0, 1, 255 */
944 if (cell_pref
!= 0 && cell_pref
!= 1 && cell_pref
!= 255) {
945 wpa_printf(MSG_DEBUG
,
946 "Invalid MBO cellular capability %u",
952 if (reassoc_delay
> 65535 ||
954 !(req_mode
& WNM_BSS_TM_REQ_DISASSOC_IMMINENT
))) {
955 wpa_printf(MSG_DEBUG
,
956 "MBO: Assoc retry delay is only valid in disassoc imminent mode");
961 *mbo_pos
++ = MBO_ATTR_ID_TRANSITION_REASON
;
963 *mbo_pos
++ = mbo_reason
;
964 *mbo_pos
++ = MBO_ATTR_ID_CELL_DATA_PREF
;
966 *mbo_pos
++ = cell_pref
;
969 *mbo_pos
++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY
;
971 WPA_PUT_LE16(mbo_pos
, reassoc_delay
);
975 mbo_len
= mbo_pos
- mbo
;
977 #endif /* CONFIG_MBO */
979 ret
= wnm_send_bss_tm_req(hapd
, sta
, req_mode
, disassoc_timer
,
980 valid_int
, bss_term_dur
, url
,
981 nei_len
? nei_rep
: NULL
, nei_len
,
982 mbo_len
? mbo
: NULL
, mbo_len
);
985 #endif /* CONFIG_MBO */
990 #endif /* CONFIG_WNM */
993 static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data
*hapd
,
994 char *buf
, size_t buflen
)
1002 WPA_ASSERT(hapd
->conf
->wpa_key_mgmt
);
1004 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_PSK
) {
1005 ret
= os_snprintf(pos
, end
- pos
, "WPA-PSK ");
1006 if (os_snprintf_error(end
- pos
, ret
))
1010 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X
) {
1011 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP ");
1012 if (os_snprintf_error(end
- pos
, ret
))
1016 #ifdef CONFIG_IEEE80211R_AP
1017 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_PSK
) {
1018 ret
= os_snprintf(pos
, end
- pos
, "FT-PSK ");
1019 if (os_snprintf_error(end
- pos
, ret
))
1023 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_IEEE8021X
) {
1024 ret
= os_snprintf(pos
, end
- pos
, "FT-EAP ");
1025 if (os_snprintf_error(end
- pos
, ret
))
1030 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_SAE
) {
1031 ret
= os_snprintf(pos
, end
- pos
, "FT-SAE ");
1032 if (os_snprintf_error(end
- pos
, ret
))
1036 #endif /* CONFIG_SAE */
1038 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_FILS_SHA256
) {
1039 ret
= os_snprintf(pos
, end
- pos
, "FT-FILS-SHA256 ");
1040 if (os_snprintf_error(end
- pos
, ret
))
1044 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_FILS_SHA384
) {
1045 ret
= os_snprintf(pos
, end
- pos
, "FT-FILS-SHA384 ");
1046 if (os_snprintf_error(end
- pos
, ret
))
1050 #endif /* CONFIG_FILS */
1051 #endif /* CONFIG_IEEE80211R_AP */
1052 #ifdef CONFIG_IEEE80211W
1053 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_PSK_SHA256
) {
1054 ret
= os_snprintf(pos
, end
- pos
, "WPA-PSK-SHA256 ");
1055 if (os_snprintf_error(end
- pos
, ret
))
1059 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X_SHA256
) {
1060 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP-SHA256 ");
1061 if (os_snprintf_error(end
- pos
, ret
))
1065 #endif /* CONFIG_IEEE80211W */
1067 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_SAE
) {
1068 ret
= os_snprintf(pos
, end
- pos
, "SAE ");
1069 if (os_snprintf_error(end
- pos
, ret
))
1073 #endif /* CONFIG_SAE */
1074 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X_SUITE_B
) {
1075 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP-SUITE-B ");
1076 if (os_snprintf_error(end
- pos
, ret
))
1080 if (hapd
->conf
->wpa_key_mgmt
&
1081 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192
) {
1082 ret
= os_snprintf(pos
, end
- pos
,
1083 "WPA-EAP-SUITE-B-192 ");
1084 if (os_snprintf_error(end
- pos
, ret
))
1089 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FILS_SHA256
) {
1090 ret
= os_snprintf(pos
, end
- pos
, "FILS-SHA256 ");
1091 if (os_snprintf_error(end
- pos
, ret
))
1095 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FILS_SHA384
) {
1096 ret
= os_snprintf(pos
, end
- pos
, "FILS-SHA384 ");
1097 if (os_snprintf_error(end
- pos
, ret
))
1101 #endif /* CONFIG_FILS */
1103 if (pos
> buf
&& *(pos
- 1) == ' ') {
1112 static int hostapd_ctrl_iface_get_config(struct hostapd_data
*hapd
,
1113 char *buf
, size_t buflen
)
1121 ret
= os_snprintf(pos
, end
- pos
, "bssid=" MACSTR
"\n"
1123 MAC2STR(hapd
->own_addr
),
1124 wpa_ssid_txt(hapd
->conf
->ssid
.ssid
,
1125 hapd
->conf
->ssid
.ssid_len
));
1126 if (os_snprintf_error(end
- pos
, ret
))
1131 ret
= os_snprintf(pos
, end
- pos
, "wps_state=%s\n",
1132 hapd
->conf
->wps_state
== 0 ? "disabled" :
1133 (hapd
->conf
->wps_state
== 1 ? "not configured" :
1135 if (os_snprintf_error(end
- pos
, ret
))
1139 if (hapd
->conf
->wps_state
&& hapd
->conf
->wpa
&&
1140 hapd
->conf
->ssid
.wpa_passphrase
) {
1141 ret
= os_snprintf(pos
, end
- pos
, "passphrase=%s\n",
1142 hapd
->conf
->ssid
.wpa_passphrase
);
1143 if (os_snprintf_error(end
- pos
, ret
))
1148 if (hapd
->conf
->wps_state
&& hapd
->conf
->wpa
&&
1149 hapd
->conf
->ssid
.wpa_psk
&&
1150 hapd
->conf
->ssid
.wpa_psk
->group
) {
1151 char hex
[PMK_LEN
* 2 + 1];
1152 wpa_snprintf_hex(hex
, sizeof(hex
),
1153 hapd
->conf
->ssid
.wpa_psk
->psk
, PMK_LEN
);
1154 ret
= os_snprintf(pos
, end
- pos
, "psk=%s\n", hex
);
1155 if (os_snprintf_error(end
- pos
, ret
))
1159 #endif /* CONFIG_WPS */
1161 if (hapd
->conf
->wpa
) {
1162 ret
= os_snprintf(pos
, end
- pos
, "wpa=%d\n", hapd
->conf
->wpa
);
1163 if (os_snprintf_error(end
- pos
, ret
))
1168 if (hapd
->conf
->wpa
&& hapd
->conf
->wpa_key_mgmt
) {
1169 ret
= os_snprintf(pos
, end
- pos
, "key_mgmt=");
1170 if (os_snprintf_error(end
- pos
, ret
))
1174 pos
+= hostapd_ctrl_iface_get_key_mgmt(hapd
, pos
, end
- pos
);
1176 ret
= os_snprintf(pos
, end
- pos
, "\n");
1177 if (os_snprintf_error(end
- pos
, ret
))
1182 if (hapd
->conf
->wpa
) {
1183 ret
= os_snprintf(pos
, end
- pos
, "group_cipher=%s\n",
1184 wpa_cipher_txt(hapd
->conf
->wpa_group
));
1185 if (os_snprintf_error(end
- pos
, ret
))
1190 if ((hapd
->conf
->wpa
& WPA_PROTO_RSN
) && hapd
->conf
->rsn_pairwise
) {
1191 ret
= os_snprintf(pos
, end
- pos
, "rsn_pairwise_cipher=");
1192 if (os_snprintf_error(end
- pos
, ret
))
1196 ret
= wpa_write_ciphers(pos
, end
, hapd
->conf
->rsn_pairwise
,
1202 ret
= os_snprintf(pos
, end
- pos
, "\n");
1203 if (os_snprintf_error(end
- pos
, ret
))
1208 if ((hapd
->conf
->wpa
& WPA_PROTO_WPA
) && hapd
->conf
->wpa_pairwise
) {
1209 ret
= os_snprintf(pos
, end
- pos
, "wpa_pairwise_cipher=");
1210 if (os_snprintf_error(end
- pos
, ret
))
1214 ret
= wpa_write_ciphers(pos
, end
, hapd
->conf
->wpa_pairwise
,
1220 ret
= os_snprintf(pos
, end
- pos
, "\n");
1221 if (os_snprintf_error(end
- pos
, ret
))
1230 static int hostapd_ctrl_iface_set(struct hostapd_data
*hapd
, char *cmd
)
1235 value
= os_strchr(cmd
, ' ');
1240 wpa_printf(MSG_DEBUG
, "CTRL_IFACE SET '%s'='%s'", cmd
, value
);
1242 #ifdef CONFIG_WPS_TESTING
1243 } else if (os_strcasecmp(cmd
, "wps_version_number") == 0) {
1245 val
= strtol(value
, NULL
, 0);
1246 if (val
< 0 || val
> 0xff) {
1248 wpa_printf(MSG_DEBUG
, "WPS: Invalid "
1249 "wps_version_number %ld", val
);
1251 wps_version_number
= val
;
1252 wpa_printf(MSG_DEBUG
, "WPS: Testing - force WPS "
1254 (wps_version_number
& 0xf0) >> 4,
1255 wps_version_number
& 0x0f);
1256 hostapd_wps_update_ie(hapd
);
1258 } else if (os_strcasecmp(cmd
, "wps_testing_dummy_cred") == 0) {
1259 wps_testing_dummy_cred
= atoi(value
);
1260 wpa_printf(MSG_DEBUG
, "WPS: Testing - dummy_cred=%d",
1261 wps_testing_dummy_cred
);
1262 } else if (os_strcasecmp(cmd
, "wps_corrupt_pkhash") == 0) {
1263 wps_corrupt_pkhash
= atoi(value
);
1264 wpa_printf(MSG_DEBUG
, "WPS: Testing - wps_corrupt_pkhash=%d",
1265 wps_corrupt_pkhash
);
1266 #endif /* CONFIG_WPS_TESTING */
1267 #ifdef CONFIG_TESTING_OPTIONS
1268 } else if (os_strcasecmp(cmd
, "ext_mgmt_frame_handling") == 0) {
1269 hapd
->ext_mgmt_frame_handling
= atoi(value
);
1270 } else if (os_strcasecmp(cmd
, "ext_eapol_frame_io") == 0) {
1271 hapd
->ext_eapol_frame_io
= atoi(value
);
1272 #endif /* CONFIG_TESTING_OPTIONS */
1274 } else if (os_strcasecmp(cmd
, "mbo_assoc_disallow") == 0) {
1277 if (!hapd
->conf
->mbo_enabled
)
1281 if (val
< 0 || val
> 1)
1284 hapd
->mbo_assoc_disallow
= val
;
1285 ieee802_11_update_beacons(hapd
->iface
);
1288 * TODO: Need to configure drivers that do AP MLME offload with
1289 * disallowing station logic.
1291 #endif /* CONFIG_MBO */
1293 struct sta_info
*sta
;
1294 struct vlan_description vlan_id
;
1296 ret
= hostapd_set_iface(hapd
->iconf
, hapd
->conf
, cmd
, value
);
1300 if (os_strcasecmp(cmd
, "deny_mac_file") == 0) {
1301 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
1302 if (hostapd_maclist_found(
1303 hapd
->conf
->deny_mac
,
1304 hapd
->conf
->num_deny_mac
, sta
->addr
,
1306 (!vlan_id
.notempty
||
1307 !vlan_compare(&vlan_id
, sta
->vlan_desc
)))
1309 hapd
, sta
, sta
->addr
,
1310 WLAN_REASON_UNSPECIFIED
);
1312 } else if (hapd
->conf
->macaddr_acl
== DENY_UNLESS_ACCEPTED
&&
1313 os_strcasecmp(cmd
, "accept_mac_file") == 0) {
1314 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
1315 if (!hostapd_maclist_found(
1316 hapd
->conf
->accept_mac
,
1317 hapd
->conf
->num_accept_mac
,
1318 sta
->addr
, &vlan_id
) ||
1319 (vlan_id
.notempty
&&
1320 vlan_compare(&vlan_id
, sta
->vlan_desc
)))
1322 hapd
, sta
, sta
->addr
,
1323 WLAN_REASON_UNSPECIFIED
);
1332 static int hostapd_ctrl_iface_get(struct hostapd_data
*hapd
, char *cmd
,
1333 char *buf
, size_t buflen
)
1337 wpa_printf(MSG_DEBUG
, "CTRL_IFACE GET '%s'", cmd
);
1339 if (os_strcmp(cmd
, "version") == 0) {
1340 res
= os_snprintf(buf
, buflen
, "%s", VERSION_STR
);
1341 if (os_snprintf_error(buflen
, res
))
1344 } else if (os_strcmp(cmd
, "tls_library") == 0) {
1345 res
= tls_get_library_version(buf
, buflen
);
1346 if (os_snprintf_error(buflen
, res
))
1355 static int hostapd_ctrl_iface_enable(struct hostapd_iface
*iface
)
1357 if (hostapd_enable_iface(iface
) < 0) {
1358 wpa_printf(MSG_ERROR
, "Enabling of interface failed");
1365 static int hostapd_ctrl_iface_reload(struct hostapd_iface
*iface
)
1367 if (hostapd_reload_iface(iface
) < 0) {
1368 wpa_printf(MSG_ERROR
, "Reloading of interface failed");
1375 static int hostapd_ctrl_iface_disable(struct hostapd_iface
*iface
)
1377 if (hostapd_disable_iface(iface
) < 0) {
1378 wpa_printf(MSG_ERROR
, "Disabling of interface failed");
1385 #ifdef CONFIG_TESTING_OPTIONS
1387 static int hostapd_ctrl_iface_radar(struct hostapd_data
*hapd
, char *cmd
)
1389 union wpa_event_data data
;
1391 enum wpa_event_type event
;
1393 wpa_printf(MSG_DEBUG
, "RADAR TEST: %s", cmd
);
1395 os_memset(&data
, 0, sizeof(data
));
1397 param
= os_strchr(cmd
, ' ');
1402 if (os_strcmp(cmd
, "DETECTED") == 0)
1403 event
= EVENT_DFS_RADAR_DETECTED
;
1404 else if (os_strcmp(cmd
, "CAC-FINISHED") == 0)
1405 event
= EVENT_DFS_CAC_FINISHED
;
1406 else if (os_strcmp(cmd
, "CAC-ABORTED") == 0)
1407 event
= EVENT_DFS_CAC_ABORTED
;
1408 else if (os_strcmp(cmd
, "NOP-FINISHED") == 0)
1409 event
= EVENT_DFS_NOP_FINISHED
;
1411 wpa_printf(MSG_DEBUG
, "Unsupported RADAR test command: %s",
1416 pos
= os_strstr(param
, "freq=");
1418 data
.dfs_event
.freq
= atoi(pos
+ 5);
1420 pos
= os_strstr(param
, "ht_enabled=1");
1422 data
.dfs_event
.ht_enabled
= 1;
1424 pos
= os_strstr(param
, "chan_offset=");
1426 data
.dfs_event
.chan_offset
= atoi(pos
+ 12);
1428 pos
= os_strstr(param
, "chan_width=");
1430 data
.dfs_event
.chan_width
= atoi(pos
+ 11);
1432 pos
= os_strstr(param
, "cf1=");
1434 data
.dfs_event
.cf1
= atoi(pos
+ 4);
1436 pos
= os_strstr(param
, "cf2=");
1438 data
.dfs_event
.cf2
= atoi(pos
+ 4);
1440 wpa_supplicant_event(hapd
, event
, &data
);
1446 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data
*hapd
, char *cmd
)
1452 wpa_printf(MSG_DEBUG
, "External MGMT TX: %s", cmd
);
1454 len
= os_strlen(cmd
);
1459 buf
= os_malloc(len
);
1463 if (hexstr2bin(cmd
, buf
, len
) < 0) {
1468 res
= hostapd_drv_send_mlme(hapd
, buf
, len
, 0);
1474 static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data
*hapd
,
1480 int freq
= 0, datarate
= 0, ssi_signal
= 0;
1481 union wpa_event_data event
;
1483 if (!hapd
->ext_mgmt_frame_handling
)
1486 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1488 wpa_printf(MSG_DEBUG
, "External MGMT RX process: %s", cmd
);
1491 param
= os_strstr(pos
, "freq=");
1497 param
= os_strstr(pos
, " datarate=");
1500 datarate
= atoi(param
);
1503 param
= os_strstr(pos
, " ssi_signal=");
1506 ssi_signal
= atoi(param
);
1509 param
= os_strstr(pos
, " frame=");
1514 len
= os_strlen(param
);
1519 buf
= os_malloc(len
);
1523 if (hexstr2bin(param
, buf
, len
) < 0) {
1528 os_memset(&event
, 0, sizeof(event
));
1529 event
.rx_mgmt
.freq
= freq
;
1530 event
.rx_mgmt
.frame
= buf
;
1531 event
.rx_mgmt
.frame_len
= len
;
1532 event
.rx_mgmt
.ssi_signal
= ssi_signal
;
1533 event
.rx_mgmt
.datarate
= datarate
;
1534 hapd
->ext_mgmt_frame_handling
= 0;
1535 wpa_supplicant_event(hapd
, EVENT_RX_MGMT
, &event
);
1536 hapd
->ext_mgmt_frame_handling
= 1;
1544 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data
*hapd
, char *cmd
)
1547 u8 src
[ETH_ALEN
], *buf
;
1551 wpa_printf(MSG_DEBUG
, "External EAPOL RX: %s", cmd
);
1554 used
= hwaddr_aton2(pos
, src
);
1561 len
= os_strlen(pos
);
1566 buf
= os_malloc(len
);
1570 if (hexstr2bin(pos
, buf
, len
) < 0) {
1575 ieee802_1x_receive(hapd
, src
, buf
, len
);
1582 static u16
ipv4_hdr_checksum(const void *buf
, size_t len
)
1586 const u16
*pos
= buf
;
1588 for (i
= 0; i
< len
/ 2; i
++)
1592 sum
= (sum
& 0xffff) + (sum
>> 16);
1594 return sum
^ 0xffff;
1598 #define HWSIM_PACKETLEN 1500
1599 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1601 static void hostapd_data_test_rx(void *ctx
, const u8
*src_addr
, const u8
*buf
,
1604 struct hostapd_data
*hapd
= ctx
;
1605 const struct ether_header
*eth
;
1610 if (len
!= HWSIM_PACKETLEN
)
1613 eth
= (const struct ether_header
*) buf
;
1614 os_memcpy(&ip
, eth
+ 1, sizeof(ip
));
1615 pos
= &buf
[sizeof(*eth
) + sizeof(ip
)];
1617 if (ip
.ihl
!= 5 || ip
.version
!= 4 ||
1618 ntohs(ip
.tot_len
) != HWSIM_IP_LEN
)
1621 for (i
= 0; i
< HWSIM_IP_LEN
- sizeof(ip
); i
++) {
1627 wpa_msg(hapd
->msg_ctx
, MSG_INFO
, "DATA-TEST-RX " MACSTR
" " MACSTR
,
1628 MAC2STR(eth
->ether_dhost
), MAC2STR(eth
->ether_shost
));
1632 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data
*hapd
,
1635 int enabled
= atoi(cmd
);
1640 if (hapd
->l2_test
) {
1641 l2_packet_deinit(hapd
->l2_test
);
1642 hapd
->l2_test
= NULL
;
1643 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
,
1644 "test data: Disabled");
1652 pos
= os_strstr(cmd
, " ifname=");
1656 ifname
= hapd
->conf
->iface
;
1658 hapd
->l2_test
= l2_packet_init(ifname
, hapd
->own_addr
,
1659 ETHERTYPE_IP
, hostapd_data_test_rx
,
1661 if (hapd
->l2_test
== NULL
)
1664 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: Enabled");
1670 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data
*hapd
, char *cmd
)
1672 u8 dst
[ETH_ALEN
], src
[ETH_ALEN
];
1677 u8 buf
[2 + HWSIM_PACKETLEN
];
1678 struct ether_header
*eth
;
1683 if (hapd
->l2_test
== NULL
)
1686 /* format: <dst> <src> <tos> */
1689 used
= hwaddr_aton2(pos
, dst
);
1695 used
= hwaddr_aton2(pos
, src
);
1700 val
= strtol(pos
, NULL
, 0);
1701 if (val
< 0 || val
> 0xff)
1705 eth
= (struct ether_header
*) &buf
[2];
1706 os_memcpy(eth
->ether_dhost
, dst
, ETH_ALEN
);
1707 os_memcpy(eth
->ether_shost
, src
, ETH_ALEN
);
1708 eth
->ether_type
= htons(ETHERTYPE_IP
);
1709 ip
= (struct iphdr
*) (eth
+ 1);
1710 os_memset(ip
, 0, sizeof(*ip
));
1715 ip
->tot_len
= htons(HWSIM_IP_LEN
);
1717 ip
->saddr
= htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1718 ip
->daddr
= htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1719 ip
->check
= ipv4_hdr_checksum(ip
, sizeof(*ip
));
1720 dpos
= (u8
*) (ip
+ 1);
1721 for (i
= 0; i
< HWSIM_IP_LEN
- sizeof(*ip
); i
++)
1724 if (l2_packet_send(hapd
->l2_test
, dst
, ETHERTYPE_IP
, &buf
[2],
1725 HWSIM_PACKETLEN
) < 0)
1728 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: TX dst=" MACSTR
1729 " src=" MACSTR
" tos=0x%x", MAC2STR(dst
), MAC2STR(src
), tos
);
1735 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data
*hapd
,
1739 struct ether_header
*eth
;
1740 struct l2_packet_data
*l2
= NULL
;
1744 const char *ifname
= hapd
->conf
->iface
;
1746 if (os_strncmp(cmd
, "ifname=", 7) == 0) {
1749 cmd
= os_strchr(cmd
, ' ');
1755 len
= os_strlen(cmd
);
1756 if (len
& 1 || len
< ETH_HLEN
* 2)
1760 buf
= os_malloc(len
);
1764 if (hexstr2bin(cmd
, buf
, len
) < 0)
1767 eth
= (struct ether_header
*) buf
;
1768 ethertype
= ntohs(eth
->ether_type
);
1770 l2
= l2_packet_init(ifname
, hapd
->own_addr
, ethertype
,
1771 hostapd_data_test_rx
, hapd
, 1);
1775 res
= l2_packet_send(l2
, eth
->ether_dhost
, ethertype
, buf
, len
);
1776 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: TX frame res=%d", res
);
1779 l2_packet_deinit(l2
);
1782 return res
< 0 ? -1 : 0;
1786 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data
*hapd
, char *cmd
)
1788 #ifdef WPA_TRACE_BFD
1791 wpa_trace_fail_after
= atoi(cmd
);
1792 pos
= os_strchr(cmd
, ':');
1795 os_strlcpy(wpa_trace_fail_func
, pos
,
1796 sizeof(wpa_trace_fail_func
));
1798 wpa_trace_fail_after
= 0;
1802 #else /* WPA_TRACE_BFD */
1804 #endif /* WPA_TRACE_BFD */
1808 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data
*hapd
,
1809 char *buf
, size_t buflen
)
1811 #ifdef WPA_TRACE_BFD
1812 return os_snprintf(buf
, buflen
, "%u:%s", wpa_trace_fail_after
,
1813 wpa_trace_fail_func
);
1814 #else /* WPA_TRACE_BFD */
1816 #endif /* WPA_TRACE_BFD */
1820 static int hostapd_ctrl_test_fail(struct hostapd_data
*hapd
, char *cmd
)
1822 #ifdef WPA_TRACE_BFD
1825 wpa_trace_test_fail_after
= atoi(cmd
);
1826 pos
= os_strchr(cmd
, ':');
1829 os_strlcpy(wpa_trace_test_fail_func
, pos
,
1830 sizeof(wpa_trace_test_fail_func
));
1832 wpa_trace_test_fail_after
= 0;
1836 #else /* WPA_TRACE_BFD */
1838 #endif /* WPA_TRACE_BFD */
1842 static int hostapd_ctrl_get_fail(struct hostapd_data
*hapd
,
1843 char *buf
, size_t buflen
)
1845 #ifdef WPA_TRACE_BFD
1846 return os_snprintf(buf
, buflen
, "%u:%s", wpa_trace_test_fail_after
,
1847 wpa_trace_test_fail_func
);
1848 #else /* WPA_TRACE_BFD */
1850 #endif /* WPA_TRACE_BFD */
1853 #endif /* CONFIG_TESTING_OPTIONS */
1856 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface
*iface
,
1860 struct csa_settings settings
;
1864 ret
= hostapd_parse_csa_settings(pos
, &settings
);
1868 for (i
= 0; i
< iface
->num_bss
; i
++) {
1869 ret
= hostapd_switch_channel(iface
->bss
[i
], &settings
);
1871 /* FIX: What do we do if CSA fails in the middle of
1872 * submitting multi-BSS CSA requests? */
1878 #else /* NEED_AP_MLME */
1880 #endif /* NEED_AP_MLME */
1884 static int hostapd_ctrl_iface_mib(struct hostapd_data
*hapd
, char *reply
,
1885 int reply_size
, const char *param
)
1887 #ifdef RADIUS_SERVER
1888 if (os_strcmp(param
, "radius_server") == 0) {
1889 return radius_server_get_mib(hapd
->radius_srv
, reply
,
1892 #endif /* RADIUS_SERVER */
1897 static int hostapd_ctrl_iface_vendor(struct hostapd_data
*hapd
, char *cmd
,
1898 char *buf
, size_t buflen
)
1903 unsigned int vendor_id
, subcmd
;
1904 struct wpabuf
*reply
;
1905 size_t data_len
= 0;
1907 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1908 vendor_id
= strtoul(cmd
, &pos
, 16);
1909 if (!isblank((unsigned char) *pos
))
1912 subcmd
= strtoul(pos
, &pos
, 10);
1915 if (!isblank((unsigned char) *pos
++))
1917 data_len
= os_strlen(pos
);
1922 data
= os_malloc(data_len
);
1926 if (hexstr2bin(pos
, data
, data_len
)) {
1927 wpa_printf(MSG_DEBUG
,
1928 "Vendor command: wrong parameter format");
1934 reply
= wpabuf_alloc((buflen
- 1) / 2);
1940 ret
= hostapd_drv_vendor_cmd(hapd
, vendor_id
, subcmd
, data
, data_len
,
1944 ret
= wpa_snprintf_hex(buf
, buflen
, wpabuf_head_u8(reply
),
1954 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data
*hapd
,
1958 struct sta_info
*sta
;
1960 if (hwaddr_aton(cmd
, addr
))
1963 sta
= ap_get_sta(hapd
, addr
);
1964 if (!sta
|| !sta
->eapol_sm
)
1967 eapol_auth_reauthenticate(sta
->eapol_sm
);
1972 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data
*hapd
, char *cmd
)
1975 struct sta_info
*sta
;
1976 char *pos
= cmd
, *param
;
1978 if (hwaddr_aton(pos
, addr
) || pos
[17] != ' ')
1982 pos
= os_strchr(pos
, ' ');
1987 sta
= ap_get_sta(hapd
, addr
);
1988 if (!sta
|| !sta
->eapol_sm
)
1991 return eapol_auth_set_conf(sta
->eapol_sm
, param
, pos
);
1995 static int hostapd_ctrl_iface_log_level(struct hostapd_data
*hapd
, char *cmd
,
1996 char *buf
, size_t buflen
)
1998 char *pos
, *end
, *stamp
;
2001 /* cmd: "LOG_LEVEL [<level>]" */
2005 ret
= os_snprintf(pos
, end
- pos
, "Current level: %s\n"
2007 debug_level_str(wpa_debug_level
),
2008 wpa_debug_timestamp
);
2009 if (os_snprintf_error(end
- pos
, ret
))
2018 stamp
= os_strchr(cmd
, ' ');
2021 while (*stamp
== ' ') {
2026 if (os_strlen(cmd
)) {
2027 int level
= str_to_debug_level(cmd
);
2030 wpa_debug_level
= level
;
2033 if (stamp
&& os_strlen(stamp
))
2034 wpa_debug_timestamp
= atoi(stamp
);
2036 os_memcpy(buf
, "OK\n", 3);
2042 static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data
*hapd
,
2043 char *buf
, size_t buflen
)
2045 struct hostapd_iface
*iface
= hapd
->iface
;
2047 struct hostapd_sta_info
*info
;
2048 struct os_reltime now
;
2050 if (!iface
->num_sta_seen
)
2053 sta_track_expire(iface
, 0);
2058 os_get_reltime(&now
);
2059 dl_list_for_each_reverse(info
, &iface
->sta_seen
,
2060 struct hostapd_sta_info
, list
) {
2061 struct os_reltime age
;
2064 os_reltime_sub(&now
, &info
->last_seen
, &age
);
2065 ret
= os_snprintf(pos
, end
- pos
, MACSTR
" %u %d\n",
2066 MAC2STR(info
->addr
), (unsigned int) age
.sec
,
2068 if (os_snprintf_error(end
- pos
, ret
))
2075 #endif /* NEED_AP_MLME */
2078 static int hostapd_ctrl_iface_req_lci(struct hostapd_data
*hapd
,
2083 if (hwaddr_aton(cmd
, addr
)) {
2084 wpa_printf(MSG_INFO
, "CTRL: REQ_LCI: Invalid MAC address");
2088 return hostapd_send_lci_req(hapd
, addr
);
2092 static int hostapd_ctrl_iface_req_range(struct hostapd_data
*hapd
, char *cmd
)
2095 char *token
, *context
= NULL
;
2096 int random_interval
, min_ap
;
2097 u8 responders
[ETH_ALEN
* RRM_RANGE_REQ_MAX_RESPONDERS
];
2098 unsigned int n_responders
;
2100 token
= str_token(cmd
, " ", &context
);
2101 if (!token
|| hwaddr_aton(token
, addr
)) {
2102 wpa_printf(MSG_INFO
,
2103 "CTRL: REQ_RANGE - Bad destination address");
2107 token
= str_token(cmd
, " ", &context
);
2111 random_interval
= atoi(token
);
2112 if (random_interval
< 0 || random_interval
> 0xffff)
2115 token
= str_token(cmd
, " ", &context
);
2119 min_ap
= atoi(token
);
2120 if (min_ap
<= 0 || min_ap
> WLAN_RRM_RANGE_REQ_MAX_MIN_AP
)
2124 while ((token
= str_token(cmd
, " ", &context
))) {
2125 if (n_responders
== RRM_RANGE_REQ_MAX_RESPONDERS
) {
2126 wpa_printf(MSG_INFO
,
2127 "CTRL: REQ_RANGE: Too many responders");
2131 if (hwaddr_aton(token
, responders
+ n_responders
* ETH_ALEN
)) {
2132 wpa_printf(MSG_INFO
,
2133 "CTRL: REQ_RANGE: Bad responder address");
2140 if (!n_responders
) {
2141 wpa_printf(MSG_INFO
,
2142 "CTRL: REQ_RANGE - No FTM responder address");
2146 return hostapd_send_range_req(hapd
, addr
, random_interval
, min_ap
,
2147 responders
, n_responders
);
2151 static int hostapd_ctrl_iface_req_beacon(struct hostapd_data
*hapd
,
2152 const char *cmd
, char *reply
,
2161 if (hwaddr_aton(cmd
, addr
))
2163 pos
= os_strchr(cmd
, ' ');
2167 if (os_strncmp(pos
, "req_mode=", 9) == 0) {
2168 int val
= hex2byte(pos
+ 9);
2174 pos
= os_strchr(pos
, ' ');
2179 req
= wpabuf_parse_bin(pos
);
2183 ret
= hostapd_send_beacon_req(hapd
, addr
, req_mode
, req
);
2186 ret
= os_snprintf(reply
, reply_size
, "%d", ret
);
2191 static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data
*hapd
, char *buf
)
2193 struct wpa_ssid_value ssid
;
2195 struct wpabuf
*nr
, *lci
= NULL
, *civic
= NULL
;
2200 if (!(hapd
->conf
->radio_measurements
[0] &
2201 WLAN_RRM_CAPS_NEIGHBOR_REPORT
)) {
2202 wpa_printf(MSG_ERROR
,
2203 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2207 if (hwaddr_aton(buf
, bssid
)) {
2208 wpa_printf(MSG_ERROR
, "CTRL: SET_NEIGHBOR: Bad BSSID");
2212 tmp
= os_strstr(buf
, "ssid=");
2213 if (!tmp
|| ssid_parse(tmp
+ 5, &ssid
)) {
2214 wpa_printf(MSG_ERROR
,
2215 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2218 buf
= os_strchr(tmp
+ 6, tmp
[5] == '"' ? '"' : ' ');
2222 tmp
= os_strstr(buf
, "nr=");
2224 wpa_printf(MSG_ERROR
,
2225 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2229 buf
= os_strchr(tmp
, ' ');
2233 nr
= wpabuf_parse_bin(tmp
+ 3);
2235 wpa_printf(MSG_ERROR
,
2236 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2243 tmp
= os_strstr(buf
, "lci=");
2245 buf
= os_strchr(tmp
, ' ');
2248 lci
= wpabuf_parse_bin(tmp
+ 4);
2250 wpa_printf(MSG_ERROR
,
2251 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
2260 tmp
= os_strstr(buf
, "civic=");
2262 buf
= os_strchr(tmp
, ' ');
2265 civic
= wpabuf_parse_bin(tmp
+ 6);
2267 wpa_printf(MSG_ERROR
,
2268 "CTRL: SET_NEIGHBOR: Bad civic subelement");
2278 if (os_strstr(buf
, "stat"))
2282 ret
= hostapd_neighbor_set(hapd
, bssid
, &ssid
, nr
, lci
, civic
,
2293 static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data
*hapd
,
2296 struct wpa_ssid_value ssid
;
2300 if (hwaddr_aton(buf
, bssid
)) {
2301 wpa_printf(MSG_ERROR
, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
2305 tmp
= os_strstr(buf
, "ssid=");
2306 if (!tmp
|| ssid_parse(tmp
+ 5, &ssid
)) {
2307 wpa_printf(MSG_ERROR
,
2308 "CTRL: REMOVE_NEIGHBORr: Bad or missing SSID");
2312 return hostapd_neighbor_remove(hapd
, bssid
, &ssid
);
2316 static int hostapd_ctrl_driver_flags(struct hostapd_iface
*iface
, char *buf
,
2322 ret
= os_snprintf(buf
, buflen
, "%016llX:\n",
2323 (long long unsigned) iface
->drv_flags
);
2324 if (os_snprintf_error(buflen
, ret
))
2330 for (i
= 0; i
< 64; i
++) {
2331 if (iface
->drv_flags
& (1LLU << i
)) {
2332 ret
= os_snprintf(pos
, end
- pos
, "%s\n",
2333 driver_flag_to_string(1LLU << i
));
2334 if (os_snprintf_error(end
- pos
, ret
))
2344 static int hostapd_ctrl_iface_receive_process(struct hostapd_data
*hapd
,
2345 char *buf
, char *reply
,
2347 struct sockaddr_storage
*from
,
2352 os_memcpy(reply
, "OK\n", 3);
2355 if (os_strcmp(buf
, "PING") == 0) {
2356 os_memcpy(reply
, "PONG\n", 5);
2358 } else if (os_strncmp(buf
, "RELOG", 5) == 0) {
2359 if (wpa_debug_reopen_file() < 0)
2361 } else if (os_strcmp(buf
, "STATUS") == 0) {
2362 reply_len
= hostapd_ctrl_iface_status(hapd
, reply
,
2364 } else if (os_strcmp(buf
, "STATUS-DRIVER") == 0) {
2365 reply_len
= hostapd_drv_status(hapd
, reply
, reply_size
);
2366 } else if (os_strcmp(buf
, "MIB") == 0) {
2367 reply_len
= ieee802_11_get_mib(hapd
, reply
, reply_size
);
2368 if (reply_len
>= 0) {
2369 res
= wpa_get_mib(hapd
->wpa_auth
, reply
+ reply_len
,
2370 reply_size
- reply_len
);
2376 if (reply_len
>= 0) {
2377 res
= ieee802_1x_get_mib(hapd
, reply
+ reply_len
,
2378 reply_size
- reply_len
);
2384 #ifndef CONFIG_NO_RADIUS
2385 if (reply_len
>= 0) {
2386 res
= radius_client_get_mib(hapd
->radius
,
2388 reply_size
- reply_len
);
2394 #endif /* CONFIG_NO_RADIUS */
2395 } else if (os_strncmp(buf
, "MIB ", 4) == 0) {
2396 reply_len
= hostapd_ctrl_iface_mib(hapd
, reply
, reply_size
,
2398 } else if (os_strcmp(buf
, "STA-FIRST") == 0) {
2399 reply_len
= hostapd_ctrl_iface_sta_first(hapd
, reply
,
2401 } else if (os_strncmp(buf
, "STA ", 4) == 0) {
2402 reply_len
= hostapd_ctrl_iface_sta(hapd
, buf
+ 4, reply
,
2404 } else if (os_strncmp(buf
, "STA-NEXT ", 9) == 0) {
2405 reply_len
= hostapd_ctrl_iface_sta_next(hapd
, buf
+ 9, reply
,
2407 } else if (os_strcmp(buf
, "ATTACH") == 0) {
2408 if (hostapd_ctrl_iface_attach(hapd
, from
, fromlen
))
2410 } else if (os_strcmp(buf
, "DETACH") == 0) {
2411 if (hostapd_ctrl_iface_detach(hapd
, from
, fromlen
))
2413 } else if (os_strncmp(buf
, "LEVEL ", 6) == 0) {
2414 if (hostapd_ctrl_iface_level(hapd
, from
, fromlen
,
2417 } else if (os_strncmp(buf
, "NEW_STA ", 8) == 0) {
2418 if (hostapd_ctrl_iface_new_sta(hapd
, buf
+ 8))
2420 } else if (os_strncmp(buf
, "DEAUTHENTICATE ", 15) == 0) {
2421 if (hostapd_ctrl_iface_deauthenticate(hapd
, buf
+ 15))
2423 } else if (os_strncmp(buf
, "DISASSOCIATE ", 13) == 0) {
2424 if (hostapd_ctrl_iface_disassociate(hapd
, buf
+ 13))
2426 #ifdef CONFIG_TAXONOMY
2427 } else if (os_strncmp(buf
, "SIGNATURE ", 10) == 0) {
2428 reply_len
= hostapd_ctrl_iface_signature(hapd
, buf
+ 10,
2430 #endif /* CONFIG_TAXONOMY */
2431 } else if (os_strncmp(buf
, "POLL_STA ", 9) == 0) {
2432 if (hostapd_ctrl_iface_poll_sta(hapd
, buf
+ 9))
2434 } else if (os_strcmp(buf
, "STOP_AP") == 0) {
2435 if (hostapd_ctrl_iface_stop_ap(hapd
))
2437 #ifdef CONFIG_IEEE80211W
2439 } else if (os_strncmp(buf
, "SA_QUERY ", 9) == 0) {
2440 if (hostapd_ctrl_iface_sa_query(hapd
, buf
+ 9))
2442 #endif /* NEED_AP_MLME */
2443 #endif /* CONFIG_IEEE80211W */
2445 } else if (os_strncmp(buf
, "WPS_PIN ", 8) == 0) {
2446 if (hostapd_ctrl_iface_wps_pin(hapd
, buf
+ 8))
2448 } else if (os_strncmp(buf
, "WPS_CHECK_PIN ", 14) == 0) {
2449 reply_len
= hostapd_ctrl_iface_wps_check_pin(
2450 hapd
, buf
+ 14, reply
, reply_size
);
2451 } else if (os_strcmp(buf
, "WPS_PBC") == 0) {
2452 if (hostapd_wps_button_pushed(hapd
, NULL
))
2454 } else if (os_strcmp(buf
, "WPS_CANCEL") == 0) {
2455 if (hostapd_wps_cancel(hapd
))
2457 } else if (os_strncmp(buf
, "WPS_AP_PIN ", 11) == 0) {
2458 reply_len
= hostapd_ctrl_iface_wps_ap_pin(hapd
, buf
+ 11,
2460 } else if (os_strncmp(buf
, "WPS_CONFIG ", 11) == 0) {
2461 if (hostapd_ctrl_iface_wps_config(hapd
, buf
+ 11) < 0)
2463 } else if (os_strncmp(buf
, "WPS_GET_STATUS", 13) == 0) {
2464 reply_len
= hostapd_ctrl_iface_wps_get_status(hapd
, reply
,
2466 #ifdef CONFIG_WPS_NFC
2467 } else if (os_strncmp(buf
, "WPS_NFC_TAG_READ ", 17) == 0) {
2468 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd
, buf
+ 17))
2470 } else if (os_strncmp(buf
, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2471 reply_len
= hostapd_ctrl_iface_wps_nfc_config_token(
2472 hapd
, buf
+ 21, reply
, reply_size
);
2473 } else if (os_strncmp(buf
, "WPS_NFC_TOKEN ", 14) == 0) {
2474 reply_len
= hostapd_ctrl_iface_wps_nfc_token(
2475 hapd
, buf
+ 14, reply
, reply_size
);
2476 } else if (os_strncmp(buf
, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2477 reply_len
= hostapd_ctrl_iface_nfc_get_handover_sel(
2478 hapd
, buf
+ 21, reply
, reply_size
);
2479 } else if (os_strncmp(buf
, "NFC_REPORT_HANDOVER ", 20) == 0) {
2480 if (hostapd_ctrl_iface_nfc_report_handover(hapd
, buf
+ 20))
2482 #endif /* CONFIG_WPS_NFC */
2483 #endif /* CONFIG_WPS */
2484 #ifdef CONFIG_INTERWORKING
2485 } else if (os_strncmp(buf
, "SET_QOS_MAP_SET ", 16) == 0) {
2486 if (hostapd_ctrl_iface_set_qos_map_set(hapd
, buf
+ 16))
2488 } else if (os_strncmp(buf
, "SEND_QOS_MAP_CONF ", 18) == 0) {
2489 if (hostapd_ctrl_iface_send_qos_map_conf(hapd
, buf
+ 18))
2491 #endif /* CONFIG_INTERWORKING */
2493 } else if (os_strncmp(buf
, "HS20_WNM_NOTIF ", 15) == 0) {
2494 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd
, buf
+ 15))
2496 } else if (os_strncmp(buf
, "HS20_DEAUTH_REQ ", 16) == 0) {
2497 if (hostapd_ctrl_iface_hs20_deauth_req(hapd
, buf
+ 16))
2499 #endif /* CONFIG_HS20 */
2501 } else if (os_strncmp(buf
, "DISASSOC_IMMINENT ", 18) == 0) {
2502 if (hostapd_ctrl_iface_disassoc_imminent(hapd
, buf
+ 18))
2504 } else if (os_strncmp(buf
, "ESS_DISASSOC ", 13) == 0) {
2505 if (hostapd_ctrl_iface_ess_disassoc(hapd
, buf
+ 13))
2507 } else if (os_strncmp(buf
, "BSS_TM_REQ ", 11) == 0) {
2508 if (hostapd_ctrl_iface_bss_tm_req(hapd
, buf
+ 11))
2510 #endif /* CONFIG_WNM */
2511 } else if (os_strcmp(buf
, "GET_CONFIG") == 0) {
2512 reply_len
= hostapd_ctrl_iface_get_config(hapd
, reply
,
2514 } else if (os_strncmp(buf
, "SET ", 4) == 0) {
2515 if (hostapd_ctrl_iface_set(hapd
, buf
+ 4))
2517 } else if (os_strncmp(buf
, "GET ", 4) == 0) {
2518 reply_len
= hostapd_ctrl_iface_get(hapd
, buf
+ 4, reply
,
2520 } else if (os_strncmp(buf
, "ENABLE", 6) == 0) {
2521 if (hostapd_ctrl_iface_enable(hapd
->iface
))
2523 } else if (os_strncmp(buf
, "RELOAD", 6) == 0) {
2524 if (hostapd_ctrl_iface_reload(hapd
->iface
))
2526 } else if (os_strncmp(buf
, "DISABLE", 7) == 0) {
2527 if (hostapd_ctrl_iface_disable(hapd
->iface
))
2529 } else if (os_strcmp(buf
, "UPDATE_BEACON") == 0) {
2530 if (ieee802_11_set_beacon(hapd
))
2532 #ifdef CONFIG_TESTING_OPTIONS
2533 } else if (os_strncmp(buf
, "RADAR ", 6) == 0) {
2534 if (hostapd_ctrl_iface_radar(hapd
, buf
+ 6))
2536 } else if (os_strncmp(buf
, "MGMT_TX ", 8) == 0) {
2537 if (hostapd_ctrl_iface_mgmt_tx(hapd
, buf
+ 8))
2539 } else if (os_strncmp(buf
, "MGMT_RX_PROCESS ", 16) == 0) {
2540 if (hostapd_ctrl_iface_mgmt_rx_process(hapd
, buf
+ 16) < 0)
2542 } else if (os_strncmp(buf
, "EAPOL_RX ", 9) == 0) {
2543 if (hostapd_ctrl_iface_eapol_rx(hapd
, buf
+ 9) < 0)
2545 } else if (os_strncmp(buf
, "DATA_TEST_CONFIG ", 17) == 0) {
2546 if (hostapd_ctrl_iface_data_test_config(hapd
, buf
+ 17) < 0)
2548 } else if (os_strncmp(buf
, "DATA_TEST_TX ", 13) == 0) {
2549 if (hostapd_ctrl_iface_data_test_tx(hapd
, buf
+ 13) < 0)
2551 } else if (os_strncmp(buf
, "DATA_TEST_FRAME ", 16) == 0) {
2552 if (hostapd_ctrl_iface_data_test_frame(hapd
, buf
+ 16) < 0)
2554 } else if (os_strncmp(buf
, "TEST_ALLOC_FAIL ", 16) == 0) {
2555 if (hostapd_ctrl_test_alloc_fail(hapd
, buf
+ 16) < 0)
2557 } else if (os_strcmp(buf
, "GET_ALLOC_FAIL") == 0) {
2558 reply_len
= hostapd_ctrl_get_alloc_fail(hapd
, reply
,
2560 } else if (os_strncmp(buf
, "TEST_FAIL ", 10) == 0) {
2561 if (hostapd_ctrl_test_fail(hapd
, buf
+ 10) < 0)
2563 } else if (os_strcmp(buf
, "GET_FAIL") == 0) {
2564 reply_len
= hostapd_ctrl_get_fail(hapd
, reply
, reply_size
);
2565 #endif /* CONFIG_TESTING_OPTIONS */
2566 } else if (os_strncmp(buf
, "CHAN_SWITCH ", 12) == 0) {
2567 if (hostapd_ctrl_iface_chan_switch(hapd
->iface
, buf
+ 12))
2569 } else if (os_strncmp(buf
, "VENDOR ", 7) == 0) {
2570 reply_len
= hostapd_ctrl_iface_vendor(hapd
, buf
+ 7, reply
,
2572 } else if (os_strcmp(buf
, "ERP_FLUSH") == 0) {
2573 ieee802_1x_erp_flush(hapd
);
2574 #ifdef RADIUS_SERVER
2575 radius_server_erp_flush(hapd
->radius_srv
);
2576 #endif /* RADIUS_SERVER */
2577 } else if (os_strncmp(buf
, "EAPOL_REAUTH ", 13) == 0) {
2578 if (hostapd_ctrl_iface_eapol_reauth(hapd
, buf
+ 13))
2580 } else if (os_strncmp(buf
, "EAPOL_SET ", 10) == 0) {
2581 if (hostapd_ctrl_iface_eapol_set(hapd
, buf
+ 10))
2583 } else if (os_strncmp(buf
, "LOG_LEVEL", 9) == 0) {
2584 reply_len
= hostapd_ctrl_iface_log_level(
2585 hapd
, buf
+ 9, reply
, reply_size
);
2587 } else if (os_strcmp(buf
, "TRACK_STA_LIST") == 0) {
2588 reply_len
= hostapd_ctrl_iface_track_sta_list(
2589 hapd
, reply
, reply_size
);
2590 #endif /* NEED_AP_MLME */
2591 } else if (os_strcmp(buf
, "PMKSA") == 0) {
2592 reply_len
= hostapd_ctrl_iface_pmksa_list(hapd
, reply
,
2594 } else if (os_strcmp(buf
, "PMKSA_FLUSH") == 0) {
2595 hostapd_ctrl_iface_pmksa_flush(hapd
);
2596 } else if (os_strncmp(buf
, "SET_NEIGHBOR ", 13) == 0) {
2597 if (hostapd_ctrl_iface_set_neighbor(hapd
, buf
+ 13))
2599 } else if (os_strncmp(buf
, "REMOVE_NEIGHBOR ", 16) == 0) {
2600 if (hostapd_ctrl_iface_remove_neighbor(hapd
, buf
+ 16))
2602 } else if (os_strncmp(buf
, "REQ_LCI ", 8) == 0) {
2603 if (hostapd_ctrl_iface_req_lci(hapd
, buf
+ 8))
2605 } else if (os_strncmp(buf
, "REQ_RANGE ", 10) == 0) {
2606 if (hostapd_ctrl_iface_req_range(hapd
, buf
+ 10))
2608 } else if (os_strncmp(buf
, "REQ_BEACON ", 11) == 0) {
2609 reply_len
= hostapd_ctrl_iface_req_beacon(hapd
, buf
+ 11,
2611 } else if (os_strcmp(buf
, "DRIVER_FLAGS") == 0) {
2612 reply_len
= hostapd_ctrl_driver_flags(hapd
->iface
, reply
,
2614 } else if (os_strcmp(buf
, "TERMINATE") == 0) {
2617 os_memcpy(reply
, "UNKNOWN COMMAND\n", 16);
2621 if (reply_len
< 0) {
2622 os_memcpy(reply
, "FAIL\n", 5);
2630 static void hostapd_ctrl_iface_receive(int sock
, void *eloop_ctx
,
2633 struct hostapd_data
*hapd
= eloop_ctx
;
2636 struct sockaddr_storage from
;
2637 socklen_t fromlen
= sizeof(from
);
2638 char *reply
, *pos
= buf
;
2639 const int reply_size
= 4096;
2641 int level
= MSG_DEBUG
;
2642 #ifdef CONFIG_CTRL_IFACE_UDP
2643 unsigned char lcookie
[COOKIE_LEN
];
2644 #endif /* CONFIG_CTRL_IFACE_UDP */
2646 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
2647 (struct sockaddr
*) &from
, &fromlen
);
2649 wpa_printf(MSG_ERROR
, "recvfrom(ctrl_iface): %s",
2655 reply
= os_malloc(reply_size
);
2656 if (reply
== NULL
) {
2657 if (sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
2659 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
2665 #ifdef CONFIG_CTRL_IFACE_UDP
2666 if (os_strcmp(buf
, "GET_COOKIE") == 0) {
2667 os_memcpy(reply
, "COOKIE=", 7);
2668 wpa_snprintf_hex(reply
+ 7, 2 * COOKIE_LEN
+ 1,
2669 cookie
, COOKIE_LEN
);
2670 reply_len
= 7 + 2 * COOKIE_LEN
;
2674 if (os_strncmp(buf
, "COOKIE=", 7) != 0 ||
2675 hexstr2bin(buf
+ 7, lcookie
, COOKIE_LEN
) < 0) {
2676 wpa_printf(MSG_DEBUG
,
2677 "CTRL: No cookie in the request - drop request");
2682 if (os_memcmp(cookie
, lcookie
, COOKIE_LEN
) != 0) {
2683 wpa_printf(MSG_DEBUG
,
2684 "CTRL: Invalid cookie in the request - drop request");
2689 pos
= buf
+ 7 + 2 * COOKIE_LEN
;
2692 #endif /* CONFIG_CTRL_IFACE_UDP */
2694 if (os_strcmp(pos
, "PING") == 0)
2695 level
= MSG_EXCESSIVE
;
2696 wpa_hexdump_ascii(level
, "RX ctrl_iface", pos
, res
);
2698 reply_len
= hostapd_ctrl_iface_receive_process(hapd
, pos
,
2702 #ifdef CONFIG_CTRL_IFACE_UDP
2704 #endif /* CONFIG_CTRL_IFACE_UDP */
2705 if (sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
2707 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
2714 #ifndef CONFIG_CTRL_IFACE_UDP
2715 static char * hostapd_ctrl_iface_path(struct hostapd_data
*hapd
)
2720 if (hapd
->conf
->ctrl_interface
== NULL
)
2723 len
= os_strlen(hapd
->conf
->ctrl_interface
) +
2724 os_strlen(hapd
->conf
->iface
) + 2;
2725 buf
= os_malloc(len
);
2729 os_snprintf(buf
, len
, "%s/%s",
2730 hapd
->conf
->ctrl_interface
, hapd
->conf
->iface
);
2731 buf
[len
- 1] = '\0';
2734 #endif /* CONFIG_CTRL_IFACE_UDP */
2737 static void hostapd_ctrl_iface_msg_cb(void *ctx
, int level
,
2738 enum wpa_msg_type type
,
2739 const char *txt
, size_t len
)
2741 struct hostapd_data
*hapd
= ctx
;
2744 hostapd_ctrl_iface_send(hapd
, level
, type
, txt
, len
);
2748 int hostapd_ctrl_iface_init(struct hostapd_data
*hapd
)
2750 #ifdef CONFIG_CTRL_IFACE_UDP
2751 int port
= HOSTAPD_CTRL_IFACE_PORT
;
2753 char port_str
[40], *tmp
;
2755 struct addrinfo hints
= { 0 }, *res
, *saveres
;
2758 if (hapd
->ctrl_sock
> -1) {
2759 wpa_printf(MSG_DEBUG
, "ctrl_iface already exists!");
2763 if (hapd
->conf
->ctrl_interface
== NULL
)
2766 pos
= os_strstr(hapd
->conf
->ctrl_interface
, "udp:");
2771 wpa_printf(MSG_ERROR
, "Invalid ctrl_iface UDP port");
2776 dl_list_init(&hapd
->ctrl_dst
);
2777 hapd
->ctrl_sock
= -1;
2778 os_get_random(cookie
, COOKIE_LEN
);
2780 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
2781 hints
.ai_flags
= AI_PASSIVE
;
2782 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
2784 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
2785 hints
.ai_family
= AF_INET6
;
2786 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
2787 hints
.ai_family
= AF_INET
;
2788 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
2789 hints
.ai_socktype
= SOCK_DGRAM
;
2792 os_snprintf(p
, sizeof(p
), "%d", port
);
2793 n
= getaddrinfo(NULL
, p
, &hints
, &res
);
2795 wpa_printf(MSG_ERROR
, "getaddrinfo(): %s", gai_strerror(n
));
2800 hapd
->ctrl_sock
= socket(res
->ai_family
, res
->ai_socktype
,
2802 if (hapd
->ctrl_sock
< 0) {
2803 wpa_printf(MSG_ERROR
, "socket(PF_INET): %s", strerror(errno
));
2807 if (bind(hapd
->ctrl_sock
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
2809 if ((HOSTAPD_CTRL_IFACE_PORT
- port
) <
2810 HOSTAPD_CTRL_IFACE_PORT_LIMIT
&& !pos
)
2812 wpa_printf(MSG_ERROR
, "bind(AF_INET): %s", strerror(errno
));
2816 freeaddrinfo(saveres
);
2818 os_snprintf(port_str
, sizeof(port_str
), "udp:%d", port
);
2819 tmp
= os_strdup(port_str
);
2821 os_free(hapd
->conf
->ctrl_interface
);
2822 hapd
->conf
->ctrl_interface
= tmp
;
2824 wpa_printf(MSG_DEBUG
, "ctrl_iface_init UDP port: %d", port
);
2826 if (eloop_register_read_sock(hapd
->ctrl_sock
,
2827 hostapd_ctrl_iface_receive
, hapd
, NULL
) <
2829 hostapd_ctrl_iface_deinit(hapd
);
2833 hapd
->msg_ctx
= hapd
;
2834 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb
);
2839 if (hapd
->ctrl_sock
>= 0)
2840 close(hapd
->ctrl_sock
);
2842 #else /* CONFIG_CTRL_IFACE_UDP */
2843 struct sockaddr_un addr
;
2847 if (hapd
->ctrl_sock
> -1) {
2848 wpa_printf(MSG_DEBUG
, "ctrl_iface already exists!");
2852 dl_list_init(&hapd
->ctrl_dst
);
2854 if (hapd
->conf
->ctrl_interface
== NULL
)
2857 if (mkdir(hapd
->conf
->ctrl_interface
, S_IRWXU
| S_IRWXG
) < 0) {
2858 if (errno
== EEXIST
) {
2859 wpa_printf(MSG_DEBUG
, "Using existing control "
2860 "interface directory.");
2862 wpa_printf(MSG_ERROR
, "mkdir[ctrl_interface]: %s",
2868 if (hapd
->conf
->ctrl_interface_gid_set
&&
2869 chown(hapd
->conf
->ctrl_interface
, -1,
2870 hapd
->conf
->ctrl_interface_gid
) < 0) {
2871 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
2876 if (!hapd
->conf
->ctrl_interface_gid_set
&&
2877 hapd
->iface
->interfaces
->ctrl_iface_group
&&
2878 chown(hapd
->conf
->ctrl_interface
, -1,
2879 hapd
->iface
->interfaces
->ctrl_iface_group
) < 0) {
2880 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
2887 * Android is using umask 0077 which would leave the control interface
2888 * directory without group access. This breaks things since Wi-Fi
2889 * framework assumes that this directory can be accessed by other
2890 * applications in the wifi group. Fix this by adding group access even
2891 * if umask value would prevent this.
2893 if (chmod(hapd
->conf
->ctrl_interface
, S_IRWXU
| S_IRWXG
) < 0) {
2894 wpa_printf(MSG_ERROR
, "CTRL: Could not chmod directory: %s",
2896 /* Try to continue anyway */
2898 #endif /* ANDROID */
2900 if (os_strlen(hapd
->conf
->ctrl_interface
) + 1 +
2901 os_strlen(hapd
->conf
->iface
) >= sizeof(addr
.sun_path
))
2904 s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
2906 wpa_printf(MSG_ERROR
, "socket(PF_UNIX): %s", strerror(errno
));
2910 os_memset(&addr
, 0, sizeof(addr
));
2912 addr
.sun_len
= sizeof(addr
);
2913 #endif /* __FreeBSD__ */
2914 addr
.sun_family
= AF_UNIX
;
2915 fname
= hostapd_ctrl_iface_path(hapd
);
2918 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
2919 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
2920 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
2922 if (connect(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
2923 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
2924 " allow connections - assuming it was left"
2925 "over from forced program termination");
2926 if (unlink(fname
) < 0) {
2927 wpa_printf(MSG_ERROR
,
2928 "Could not unlink existing ctrl_iface socket '%s': %s",
2929 fname
, strerror(errno
));
2932 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) <
2934 wpa_printf(MSG_ERROR
,
2935 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
2939 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
2940 "ctrl_iface socket '%s'", fname
);
2942 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
2943 "be in use - cannot override it");
2944 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
2945 "not used anymore", fname
);
2952 if (hapd
->conf
->ctrl_interface_gid_set
&&
2953 chown(fname
, -1, hapd
->conf
->ctrl_interface_gid
) < 0) {
2954 wpa_printf(MSG_ERROR
, "chown[ctrl_interface/ifname]: %s",
2959 if (!hapd
->conf
->ctrl_interface_gid_set
&&
2960 hapd
->iface
->interfaces
->ctrl_iface_group
&&
2961 chown(fname
, -1, hapd
->iface
->interfaces
->ctrl_iface_group
) < 0) {
2962 wpa_printf(MSG_ERROR
, "chown[ctrl_interface/ifname]: %s",
2967 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
2968 wpa_printf(MSG_ERROR
, "chmod[ctrl_interface/ifname]: %s",
2974 hapd
->ctrl_sock
= s
;
2975 if (eloop_register_read_sock(s
, hostapd_ctrl_iface_receive
, hapd
,
2977 hostapd_ctrl_iface_deinit(hapd
);
2980 hapd
->msg_ctx
= hapd
;
2981 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb
);
2993 #endif /* CONFIG_CTRL_IFACE_UDP */
2997 void hostapd_ctrl_iface_deinit(struct hostapd_data
*hapd
)
2999 struct wpa_ctrl_dst
*dst
, *prev
;
3001 if (hapd
->ctrl_sock
> -1) {
3002 #ifndef CONFIG_CTRL_IFACE_UDP
3004 #endif /* !CONFIG_CTRL_IFACE_UDP */
3006 eloop_unregister_read_sock(hapd
->ctrl_sock
);
3007 close(hapd
->ctrl_sock
);
3008 hapd
->ctrl_sock
= -1;
3009 #ifndef CONFIG_CTRL_IFACE_UDP
3010 fname
= hostapd_ctrl_iface_path(hapd
);
3015 if (hapd
->conf
->ctrl_interface
&&
3016 rmdir(hapd
->conf
->ctrl_interface
) < 0) {
3017 if (errno
== ENOTEMPTY
) {
3018 wpa_printf(MSG_DEBUG
, "Control interface "
3019 "directory not empty - leaving it "
3022 wpa_printf(MSG_ERROR
,
3023 "rmdir[ctrl_interface=%s]: %s",
3024 hapd
->conf
->ctrl_interface
,
3028 #endif /* !CONFIG_CTRL_IFACE_UDP */
3031 dl_list_for_each_safe(dst
, prev
, &hapd
->ctrl_dst
, struct wpa_ctrl_dst
,
3035 #ifdef CONFIG_TESTING_OPTIONS
3036 l2_packet_deinit(hapd
->l2_test
);
3037 hapd
->l2_test
= NULL
;
3038 #endif /* CONFIG_TESTING_OPTIONS */
3042 static int hostapd_ctrl_iface_add(struct hapd_interfaces
*interfaces
,
3045 if (hostapd_add_iface(interfaces
, buf
) < 0) {
3046 wpa_printf(MSG_ERROR
, "Adding interface %s failed", buf
);
3053 static int hostapd_ctrl_iface_remove(struct hapd_interfaces
*interfaces
,
3056 if (hostapd_remove_iface(interfaces
, buf
) < 0) {
3057 wpa_printf(MSG_ERROR
, "Removing interface %s failed", buf
);
3064 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces
*interfaces
,
3065 struct sockaddr_storage
*from
,
3068 return ctrl_iface_attach(&interfaces
->global_ctrl_dst
, from
, fromlen
);
3072 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces
*interfaces
,
3073 struct sockaddr_storage
*from
,
3076 return ctrl_iface_detach(&interfaces
->global_ctrl_dst
, from
, fromlen
);
3080 static void hostapd_ctrl_iface_flush(struct hapd_interfaces
*interfaces
)
3082 #ifdef CONFIG_WPS_TESTING
3083 wps_version_number
= 0x20;
3084 wps_testing_dummy_cred
= 0;
3085 wps_corrupt_pkhash
= 0;
3086 #endif /* CONFIG_WPS_TESTING */
3093 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces
*interfaces
,
3096 char ifname
[IFNAMSIZ
+ 1];
3097 struct fst_iface_cfg cfg
;
3098 struct hostapd_data
*hapd
;
3099 struct fst_wpa_obj iface_obj
;
3101 if (!fst_parse_attach_command(cmd
, ifname
, sizeof(ifname
), &cfg
)) {
3102 hapd
= hostapd_get_iface(interfaces
, ifname
);
3104 if (hapd
->iface
->fst
) {
3105 wpa_printf(MSG_INFO
, "FST: Already attached");
3108 fst_hostapd_fill_iface_obj(hapd
, &iface_obj
);
3109 hapd
->iface
->fst
= fst_attach(ifname
, hapd
->own_addr
,
3111 if (hapd
->iface
->fst
)
3121 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces
*interfaces
,
3124 char ifname
[IFNAMSIZ
+ 1];
3125 struct hostapd_data
* hapd
;
3127 if (!fst_parse_detach_command(cmd
, ifname
, sizeof(ifname
))) {
3128 hapd
= hostapd_get_iface(interfaces
, ifname
);
3130 if (!fst_iface_detach(ifname
)) {
3131 hapd
->iface
->fst
= NULL
;
3132 hapd
->iface
->fst_ies
= NULL
;
3141 #endif /* CONFIG_FST */
3144 static struct hostapd_data
*
3145 hostapd_interfaces_get_hapd(struct hapd_interfaces
*interfaces
,
3150 for (i
= 0; i
< interfaces
->count
; i
++) {
3151 struct hostapd_iface
*iface
= interfaces
->iface
[i
];
3153 for (j
= 0; j
< iface
->num_bss
; j
++) {
3154 struct hostapd_data
*hapd
;
3156 hapd
= iface
->bss
[j
];
3157 if (os_strcmp(ifname
, hapd
->conf
->iface
) == 0)
3166 static int hostapd_ctrl_iface_dup_param(struct hostapd_data
*src_hapd
,
3167 struct hostapd_data
*dst_hapd
,
3173 value
= os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN
);
3175 wpa_printf(MSG_ERROR
,
3176 "DUP: cannot allocate buffer to stringify %s",
3181 if (os_strcmp(param
, "wpa") == 0) {
3182 os_snprintf(value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
, "%d",
3183 src_hapd
->conf
->wpa
);
3184 } else if (os_strcmp(param
, "wpa_key_mgmt") == 0 &&
3185 src_hapd
->conf
->wpa_key_mgmt
) {
3186 res
= hostapd_ctrl_iface_get_key_mgmt(
3187 src_hapd
, value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
);
3188 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN
, res
))
3189 goto error_stringify
;
3190 } else if (os_strcmp(param
, "wpa_pairwise") == 0 &&
3191 src_hapd
->conf
->wpa_pairwise
) {
3192 res
= wpa_write_ciphers(value
,
3193 value
+ HOSTAPD_CLI_DUP_VALUE_MAX_LEN
,
3194 src_hapd
->conf
->wpa_pairwise
, " ");
3196 goto error_stringify
;
3197 } else if (os_strcmp(param
, "rsn_pairwise") == 0 &&
3198 src_hapd
->conf
->rsn_pairwise
) {
3199 res
= wpa_write_ciphers(value
,
3200 value
+ HOSTAPD_CLI_DUP_VALUE_MAX_LEN
,
3201 src_hapd
->conf
->rsn_pairwise
, " ");
3203 goto error_stringify
;
3204 } else if (os_strcmp(param
, "wpa_passphrase") == 0 &&
3205 src_hapd
->conf
->ssid
.wpa_passphrase
) {
3206 os_snprintf(value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
, "%s",
3207 src_hapd
->conf
->ssid
.wpa_passphrase
);
3208 } else if (os_strcmp(param
, "wpa_psk") == 0 &&
3209 src_hapd
->conf
->ssid
.wpa_psk_set
) {
3210 wpa_snprintf_hex(value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
,
3211 src_hapd
->conf
->ssid
.wpa_psk
->psk
, PMK_LEN
);
3213 wpa_printf(MSG_WARNING
, "DUP: %s cannot be duplicated", param
);
3217 res
= hostapd_set_iface(dst_hapd
->iconf
, dst_hapd
->conf
, param
, value
);
3222 wpa_printf(MSG_ERROR
, "DUP: cannot stringify %s", param
);
3230 hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces
*interfaces
,
3232 char *reply
, int reply_size
)
3237 struct hostapd_iface
*iface
;
3241 show_ctrl
= !!os_strstr(input
, "ctrl");
3244 end
= reply
+ reply_size
;
3246 for (i
= 0; i
< interfaces
->count
; i
++) {
3247 iface
= interfaces
->iface
[i
];
3249 for (j
= 0; j
< iface
->num_bss
; j
++) {
3250 struct hostapd_bss_config
*conf
;
3252 conf
= iface
->conf
->bss
[j
];
3254 res
= os_snprintf(pos
, end
- pos
,
3255 "%s ctrl_iface=%s\n",
3257 conf
->ctrl_interface
?
3258 conf
->ctrl_interface
: "N/A");
3260 res
= os_snprintf(pos
, end
- pos
, "%s\n",
3262 if (os_snprintf_error(end
- pos
, res
)) {
3275 hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces
*interfaces
,
3278 char *p_start
= cmd
, *p_end
;
3279 struct hostapd_data
*src_hapd
, *dst_hapd
;
3281 /* cmd: "<src ifname> <dst ifname> <variable name> */
3283 p_end
= os_strchr(p_start
, ' ');
3285 wpa_printf(MSG_ERROR
, "DUP: no src ifname found in cmd: '%s'",
3291 src_hapd
= hostapd_interfaces_get_hapd(interfaces
, p_start
);
3293 wpa_printf(MSG_ERROR
, "DUP: no src ifname found: '%s'",
3298 p_start
= p_end
+ 1;
3299 p_end
= os_strchr(p_start
, ' ');
3301 wpa_printf(MSG_ERROR
, "DUP: no dst ifname found in cmd: '%s'",
3307 dst_hapd
= hostapd_interfaces_get_hapd(interfaces
, p_start
);
3309 wpa_printf(MSG_ERROR
, "DUP: no dst ifname found: '%s'",
3314 p_start
= p_end
+ 1;
3315 return hostapd_ctrl_iface_dup_param(src_hapd
, dst_hapd
, p_start
);
3319 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces
*interfaces
,
3321 char *buf
, char *reply
,
3323 struct sockaddr_storage
*from
,
3326 struct hostapd_data
*hapd
;
3328 hapd
= hostapd_interfaces_get_hapd(interfaces
, ifname
);
3332 res
= os_snprintf(reply
, reply_size
, "FAIL-NO-IFNAME-MATCH\n");
3333 if (os_snprintf_error(reply_size
, res
))
3338 return hostapd_ctrl_iface_receive_process(hapd
, buf
, reply
,reply_size
,
3343 static void hostapd_global_ctrl_iface_receive(int sock
, void *eloop_ctx
,
3346 void *interfaces
= eloop_ctx
;
3347 char buffer
[256], *buf
= buffer
;
3349 struct sockaddr_storage from
;
3350 socklen_t fromlen
= sizeof(from
);
3353 const int reply_size
= 4096;
3354 #ifdef CONFIG_CTRL_IFACE_UDP
3355 unsigned char lcookie
[COOKIE_LEN
];
3356 #endif /* CONFIG_CTRL_IFACE_UDP */
3358 res
= recvfrom(sock
, buffer
, sizeof(buffer
) - 1, 0,
3359 (struct sockaddr
*) &from
, &fromlen
);
3361 wpa_printf(MSG_ERROR
, "recvfrom(ctrl_iface): %s",
3366 wpa_printf(MSG_DEBUG
, "Global ctrl_iface command: %s", buf
);
3368 reply
= os_malloc(reply_size
);
3369 if (reply
== NULL
) {
3370 if (sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
3372 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
3378 os_memcpy(reply
, "OK\n", 3);
3381 #ifdef CONFIG_CTRL_IFACE_UDP
3382 if (os_strcmp(buf
, "GET_COOKIE") == 0) {
3383 os_memcpy(reply
, "COOKIE=", 7);
3384 wpa_snprintf_hex(reply
+ 7, 2 * COOKIE_LEN
+ 1,
3385 gcookie
, COOKIE_LEN
);
3386 reply_len
= 7 + 2 * COOKIE_LEN
;
3390 if (os_strncmp(buf
, "COOKIE=", 7) != 0 ||
3391 hexstr2bin(buf
+ 7, lcookie
, COOKIE_LEN
) < 0) {
3392 wpa_printf(MSG_DEBUG
,
3393 "CTRL: No cookie in the request - drop request");
3398 if (os_memcmp(gcookie
, lcookie
, COOKIE_LEN
) != 0) {
3399 wpa_printf(MSG_DEBUG
,
3400 "CTRL: Invalid cookie in the request - drop request");
3405 buf
+= 7 + 2 * COOKIE_LEN
;
3408 #endif /* CONFIG_CTRL_IFACE_UDP */
3410 if (os_strncmp(buf
, "IFNAME=", 7) == 0) {
3411 char *pos
= os_strchr(buf
+ 7, ' ');
3415 reply_len
= hostapd_global_ctrl_iface_ifname(
3416 interfaces
, buf
+ 7, pos
, reply
, reply_size
,
3422 if (os_strcmp(buf
, "PING") == 0) {
3423 os_memcpy(reply
, "PONG\n", 5);
3425 } else if (os_strncmp(buf
, "RELOG", 5) == 0) {
3426 if (wpa_debug_reopen_file() < 0)
3428 } else if (os_strcmp(buf
, "FLUSH") == 0) {
3429 hostapd_ctrl_iface_flush(interfaces
);
3430 } else if (os_strncmp(buf
, "ADD ", 4) == 0) {
3431 if (hostapd_ctrl_iface_add(interfaces
, buf
+ 4) < 0)
3433 } else if (os_strncmp(buf
, "REMOVE ", 7) == 0) {
3434 if (hostapd_ctrl_iface_remove(interfaces
, buf
+ 7) < 0)
3436 } else if (os_strcmp(buf
, "ATTACH") == 0) {
3437 if (hostapd_global_ctrl_iface_attach(interfaces
, &from
,
3440 } else if (os_strcmp(buf
, "DETACH") == 0) {
3441 if (hostapd_global_ctrl_iface_detach(interfaces
, &from
,
3444 #ifdef CONFIG_MODULE_TESTS
3445 } else if (os_strcmp(buf
, "MODULE_TESTS") == 0) {
3446 if (hapd_module_tests() < 0)
3448 #endif /* CONFIG_MODULE_TESTS */
3450 } else if (os_strncmp(buf
, "FST-ATTACH ", 11) == 0) {
3451 if (!hostapd_global_ctrl_iface_fst_attach(interfaces
, buf
+ 11))
3452 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
3455 } else if (os_strncmp(buf
, "FST-DETACH ", 11) == 0) {
3456 if (!hostapd_global_ctrl_iface_fst_detach(interfaces
, buf
+ 11))
3457 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
3460 } else if (os_strncmp(buf
, "FST-MANAGER ", 12) == 0) {
3461 reply_len
= fst_ctrl_iface_receive(buf
+ 12, reply
, reply_size
);
3462 #endif /* CONFIG_FST */
3463 } else if (os_strncmp(buf
, "DUP_NETWORK ", 12) == 0) {
3464 if (!hostapd_global_ctrl_iface_dup_network(interfaces
,
3466 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
3469 } else if (os_strncmp(buf
, "INTERFACES", 10) == 0) {
3470 reply_len
= hostapd_global_ctrl_iface_interfaces(
3471 interfaces
, buf
+ 10, reply
, sizeof(buffer
));
3472 } else if (os_strcmp(buf
, "TERMINATE") == 0) {
3475 wpa_printf(MSG_DEBUG
, "Unrecognized global ctrl_iface command "
3481 if (reply_len
< 0) {
3482 os_memcpy(reply
, "FAIL\n", 5);
3486 if (sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
3488 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
3495 #ifndef CONFIG_CTRL_IFACE_UDP
3496 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces
*interface
)
3501 if (interface
->global_iface_path
== NULL
)
3504 len
= os_strlen(interface
->global_iface_path
) +
3505 os_strlen(interface
->global_iface_name
) + 2;
3506 buf
= os_malloc(len
);
3510 os_snprintf(buf
, len
, "%s/%s", interface
->global_iface_path
,
3511 interface
->global_iface_name
);
3512 buf
[len
- 1] = '\0';
3515 #endif /* CONFIG_CTRL_IFACE_UDP */
3518 int hostapd_global_ctrl_iface_init(struct hapd_interfaces
*interface
)
3520 #ifdef CONFIG_CTRL_IFACE_UDP
3521 int port
= HOSTAPD_GLOBAL_CTRL_IFACE_PORT
;
3524 struct addrinfo hints
= { 0 }, *res
, *saveres
;
3527 if (interface
->global_ctrl_sock
> -1) {
3528 wpa_printf(MSG_DEBUG
, "ctrl_iface already exists!");
3532 if (interface
->global_iface_path
== NULL
)
3535 pos
= os_strstr(interface
->global_iface_path
, "udp:");
3540 wpa_printf(MSG_ERROR
, "Invalid global ctrl UDP port");
3545 os_get_random(gcookie
, COOKIE_LEN
);
3547 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3548 hints
.ai_flags
= AI_PASSIVE
;
3549 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3551 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3552 hints
.ai_family
= AF_INET6
;
3553 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3554 hints
.ai_family
= AF_INET
;
3555 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3556 hints
.ai_socktype
= SOCK_DGRAM
;
3559 os_snprintf(p
, sizeof(p
), "%d", port
);
3560 n
= getaddrinfo(NULL
, p
, &hints
, &res
);
3562 wpa_printf(MSG_ERROR
, "getaddrinfo(): %s", gai_strerror(n
));
3567 interface
->global_ctrl_sock
= socket(res
->ai_family
, res
->ai_socktype
,
3569 if (interface
->global_ctrl_sock
< 0) {
3570 wpa_printf(MSG_ERROR
, "socket(PF_INET): %s", strerror(errno
));
3574 if (bind(interface
->global_ctrl_sock
, res
->ai_addr
, res
->ai_addrlen
) <
3577 if ((port
- HOSTAPD_GLOBAL_CTRL_IFACE_PORT
) <
3578 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT
&& !pos
)
3580 wpa_printf(MSG_ERROR
, "bind(AF_INET): %s", strerror(errno
));
3584 freeaddrinfo(saveres
);
3586 wpa_printf(MSG_DEBUG
, "global ctrl_iface_init UDP port: %d", port
);
3588 if (eloop_register_read_sock(interface
->global_ctrl_sock
,
3589 hostapd_global_ctrl_iface_receive
,
3590 interface
, NULL
) < 0) {
3591 hostapd_global_ctrl_iface_deinit(interface
);
3598 if (interface
->global_ctrl_sock
>= 0)
3599 close(interface
->global_ctrl_sock
);
3601 #else /* CONFIG_CTRL_IFACE_UDP */
3602 struct sockaddr_un addr
;
3606 if (interface
->global_iface_path
== NULL
) {
3607 wpa_printf(MSG_DEBUG
, "ctrl_iface not configured!");
3611 if (mkdir(interface
->global_iface_path
, S_IRWXU
| S_IRWXG
) < 0) {
3612 if (errno
== EEXIST
) {
3613 wpa_printf(MSG_DEBUG
, "Using existing control "
3614 "interface directory.");
3616 wpa_printf(MSG_ERROR
, "mkdir[ctrl_interface]: %s",
3620 } else if (interface
->ctrl_iface_group
&&
3621 chown(interface
->global_iface_path
, -1,
3622 interface
->ctrl_iface_group
) < 0) {
3623 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
3628 if (os_strlen(interface
->global_iface_path
) + 1 +
3629 os_strlen(interface
->global_iface_name
) >= sizeof(addr
.sun_path
))
3632 s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
3634 wpa_printf(MSG_ERROR
, "socket(PF_UNIX): %s", strerror(errno
));
3638 os_memset(&addr
, 0, sizeof(addr
));
3640 addr
.sun_len
= sizeof(addr
);
3641 #endif /* __FreeBSD__ */
3642 addr
.sun_family
= AF_UNIX
;
3643 fname
= hostapd_global_ctrl_iface_path(interface
);
3646 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
3647 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
3648 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
3650 if (connect(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
3651 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
3652 " allow connections - assuming it was left"
3653 "over from forced program termination");
3654 if (unlink(fname
) < 0) {
3655 wpa_printf(MSG_ERROR
,
3656 "Could not unlink existing ctrl_iface socket '%s': %s",
3657 fname
, strerror(errno
));
3660 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) <
3662 wpa_printf(MSG_ERROR
, "bind(PF_UNIX): %s",
3666 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
3667 "ctrl_iface socket '%s'", fname
);
3669 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
3670 "be in use - cannot override it");
3671 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
3672 "not used anymore", fname
);
3679 if (interface
->ctrl_iface_group
&&
3680 chown(fname
, -1, interface
->ctrl_iface_group
) < 0) {
3681 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
3686 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
3687 wpa_printf(MSG_ERROR
, "chmod[ctrl_interface/ifname]: %s",
3693 interface
->global_ctrl_sock
= s
;
3694 eloop_register_read_sock(s
, hostapd_global_ctrl_iface_receive
,
3707 #endif /* CONFIG_CTRL_IFACE_UDP */
3711 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces
*interfaces
)
3713 #ifndef CONFIG_CTRL_IFACE_UDP
3715 #endif /* CONFIG_CTRL_IFACE_UDP */
3716 struct wpa_ctrl_dst
*dst
, *prev
;
3718 if (interfaces
->global_ctrl_sock
> -1) {
3719 eloop_unregister_read_sock(interfaces
->global_ctrl_sock
);
3720 close(interfaces
->global_ctrl_sock
);
3721 interfaces
->global_ctrl_sock
= -1;
3722 #ifndef CONFIG_CTRL_IFACE_UDP
3723 fname
= hostapd_global_ctrl_iface_path(interfaces
);
3729 if (interfaces
->global_iface_path
&&
3730 rmdir(interfaces
->global_iface_path
) < 0) {
3731 if (errno
== ENOTEMPTY
) {
3732 wpa_printf(MSG_DEBUG
, "Control interface "
3733 "directory not empty - leaving it "
3736 wpa_printf(MSG_ERROR
,
3737 "rmdir[ctrl_interface=%s]: %s",
3738 interfaces
->global_iface_path
,
3742 #endif /* CONFIG_CTRL_IFACE_UDP */
3745 os_free(interfaces
->global_iface_path
);
3746 interfaces
->global_iface_path
= NULL
;
3748 dl_list_for_each_safe(dst
, prev
, &interfaces
->global_ctrl_dst
,
3749 struct wpa_ctrl_dst
, list
)
3754 static void hostapd_ctrl_iface_send(struct hostapd_data
*hapd
, int level
,
3755 enum wpa_msg_type type
,
3756 const char *buf
, size_t len
)
3758 struct wpa_ctrl_dst
*dst
, *next
;
3759 struct dl_list
*ctrl_dst
;
3766 if (type
!= WPA_MSG_ONLY_GLOBAL
) {
3767 s
= hapd
->ctrl_sock
;
3768 ctrl_dst
= &hapd
->ctrl_dst
;
3770 s
= hapd
->iface
->interfaces
->global_ctrl_sock
;
3771 ctrl_dst
= &hapd
->iface
->interfaces
->global_ctrl_dst
;
3774 if (s
< 0 || dl_list_empty(ctrl_dst
))
3777 os_snprintf(levelstr
, sizeof(levelstr
), "<%d>", level
);
3778 io
[0].iov_base
= levelstr
;
3779 io
[0].iov_len
= os_strlen(levelstr
);
3780 io
[1].iov_base
= (char *) buf
;
3781 io
[1].iov_len
= len
;
3782 os_memset(&msg
, 0, sizeof(msg
));
3787 dl_list_for_each_safe(dst
, next
, ctrl_dst
, struct wpa_ctrl_dst
, list
) {
3788 if (level
>= dst
->debug_level
) {
3789 sockaddr_print(MSG_DEBUG
, "CTRL_IFACE monitor send",
3790 &dst
->addr
, dst
->addrlen
);
3791 msg
.msg_name
= &dst
->addr
;
3792 msg
.msg_namelen
= dst
->addrlen
;
3793 if (sendmsg(s
, &msg
, 0) < 0) {
3795 wpa_printf(MSG_INFO
, "CTRL_IFACE monitor[%d]: "
3797 idx
, errno
, strerror(errno
));
3799 if (dst
->errors
> 10 || _errno
== ENOENT
) {
3800 if (type
!= WPA_MSG_ONLY_GLOBAL
)
3801 hostapd_ctrl_iface_detach(
3805 hostapd_global_ctrl_iface_detach(
3806 hapd
->iface
->interfaces
,
3817 #endif /* CONFIG_NATIVE_WINDOWS */