2 * hostapd / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2018, 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"
33 #include "common/dpp.h"
34 #endif /* CONFIG_DPP */
35 #include "common/wpa_ctrl.h"
36 #include "crypto/tls.h"
37 #include "drivers/driver.h"
38 #include "eapol_auth/eapol_auth_sm.h"
39 #include "radius/radius_client.h"
40 #include "radius/radius_server.h"
41 #include "l2_packet/l2_packet.h"
42 #include "ap/hostapd.h"
43 #include "ap/ap_config.h"
44 #include "ap/ieee802_1x.h"
45 #include "ap/wpa_auth.h"
46 #include "ap/ieee802_11.h"
47 #include "ap/sta_info.h"
48 #include "ap/wps_hostapd.h"
49 #include "ap/ctrl_iface_ap.h"
50 #include "ap/ap_drv_ops.h"
52 #include "ap/wnm_ap.h"
53 #include "ap/wpa_auth.h"
54 #include "ap/beacon.h"
55 #include "ap/neighbor_db.h"
57 #include "ap/dpp_hostapd.h"
58 #include "wps/wps_defs.h"
60 #include "fst/fst_ctrl_iface.h"
61 #include "config_file.h"
62 #include "ctrl_iface.h"
65 #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
67 #ifdef CONFIG_CTRL_IFACE_UDP
69 static unsigned char cookie
[COOKIE_LEN
];
70 static unsigned char gcookie
[COOKIE_LEN
];
71 #define HOSTAPD_CTRL_IFACE_PORT 8877
72 #define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
73 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
74 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
75 #endif /* CONFIG_CTRL_IFACE_UDP */
77 static void hostapd_ctrl_iface_send(struct hostapd_data
*hapd
, int level
,
78 enum wpa_msg_type type
,
79 const char *buf
, size_t len
);
82 static int hostapd_ctrl_iface_attach(struct hostapd_data
*hapd
,
83 struct sockaddr_storage
*from
,
84 socklen_t fromlen
, const char *input
)
86 return ctrl_iface_attach(&hapd
->ctrl_dst
, from
, fromlen
, input
);
90 static int hostapd_ctrl_iface_detach(struct hostapd_data
*hapd
,
91 struct sockaddr_storage
*from
,
94 return ctrl_iface_detach(&hapd
->ctrl_dst
, from
, fromlen
);
98 static int hostapd_ctrl_iface_level(struct hostapd_data
*hapd
,
99 struct sockaddr_storage
*from
,
103 return ctrl_iface_level(&hapd
->ctrl_dst
, from
, fromlen
, level
);
107 static int hostapd_ctrl_iface_new_sta(struct hostapd_data
*hapd
,
111 struct sta_info
*sta
;
113 wpa_printf(MSG_DEBUG
, "CTRL_IFACE NEW_STA %s", txtaddr
);
115 if (hwaddr_aton(txtaddr
, addr
))
118 sta
= ap_get_sta(hapd
, addr
);
122 wpa_printf(MSG_DEBUG
, "Add new STA " MACSTR
" based on ctrl_iface "
123 "notification", MAC2STR(addr
));
124 sta
= ap_sta_add(hapd
, addr
);
128 hostapd_new_assoc_sta(hapd
, sta
, 0);
133 #ifdef CONFIG_IEEE80211W
135 static int hostapd_ctrl_iface_sa_query(struct hostapd_data
*hapd
,
139 u8 trans_id
[WLAN_SA_QUERY_TR_ID_LEN
];
141 wpa_printf(MSG_DEBUG
, "CTRL_IFACE SA_QUERY %s", txtaddr
);
143 if (hwaddr_aton(txtaddr
, addr
) ||
144 os_get_random(trans_id
, WLAN_SA_QUERY_TR_ID_LEN
) < 0)
147 ieee802_11_send_sa_query_req(hapd
, addr
, trans_id
);
151 #endif /* NEED_AP_MLME */
152 #endif /* CONFIG_IEEE80211W */
156 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data
*hapd
, char *txt
)
158 char *pin
= os_strchr(txt
, ' ');
161 u8 addr_buf
[ETH_ALEN
], *addr
= NULL
;
168 timeout_txt
= os_strchr(pin
, ' ');
170 *timeout_txt
++ = '\0';
171 timeout
= atoi(timeout_txt
);
172 pos
= os_strchr(timeout_txt
, ' ');
175 if (hwaddr_aton(pos
, addr_buf
) == 0)
181 return hostapd_wps_add_pin(hapd
, addr
, txt
, pin
, timeout
);
185 static int hostapd_ctrl_iface_wps_check_pin(
186 struct hostapd_data
*hapd
, char *cmd
, char *buf
, size_t buflen
)
193 wpa_hexdump_ascii_key(MSG_DEBUG
, "WPS_CHECK_PIN",
194 (u8
*) cmd
, os_strlen(cmd
));
195 for (pos
= cmd
, len
= 0; *pos
!= '\0'; pos
++) {
196 if (*pos
< '0' || *pos
> '9')
200 wpa_printf(MSG_DEBUG
, "WPS: Too long PIN");
204 if (len
!= 4 && len
!= 8) {
205 wpa_printf(MSG_DEBUG
, "WPS: Invalid PIN length %d", (int) len
);
211 unsigned int pin_val
;
213 if (!wps_pin_valid(pin_val
)) {
214 wpa_printf(MSG_DEBUG
, "WPS: Invalid checksum digit");
215 ret
= os_snprintf(buf
, buflen
, "FAIL-CHECKSUM\n");
216 if (os_snprintf_error(buflen
, ret
))
222 ret
= os_snprintf(buf
, buflen
, "%s", pin
);
223 if (os_snprintf_error(buflen
, ret
))
230 #ifdef CONFIG_WPS_NFC
231 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data
*hapd
,
238 len
= os_strlen(pos
);
243 buf
= wpabuf_alloc(len
);
246 if (hexstr2bin(pos
, wpabuf_put(buf
, len
), len
) < 0) {
251 ret
= hostapd_wps_nfc_tag_read(hapd
, buf
);
258 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data
*hapd
,
259 char *cmd
, char *reply
,
266 if (os_strcmp(cmd
, "WPS") == 0)
268 else if (os_strcmp(cmd
, "NDEF") == 0)
273 buf
= hostapd_wps_nfc_config_token(hapd
, ndef
);
277 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
288 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data
*hapd
,
289 char *reply
, size_t max_len
,
295 buf
= hostapd_wps_nfc_token_gen(hapd
, ndef
);
299 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
310 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data
*hapd
,
311 char *cmd
, char *reply
,
314 if (os_strcmp(cmd
, "WPS") == 0)
315 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd
, reply
,
318 if (os_strcmp(cmd
, "NDEF") == 0)
319 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd
, reply
,
322 if (os_strcmp(cmd
, "enable") == 0)
323 return hostapd_wps_nfc_token_enable(hapd
);
325 if (os_strcmp(cmd
, "disable") == 0) {
326 hostapd_wps_nfc_token_disable(hapd
);
334 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data
*hapd
,
335 char *cmd
, char *reply
,
343 pos
= os_strchr(cmd
, ' ');
348 if (os_strcmp(cmd
, "WPS") == 0)
350 else if (os_strcmp(cmd
, "NDEF") == 0)
355 if (os_strcmp(pos
, "WPS-CR") == 0)
356 buf
= hostapd_wps_nfc_hs_cr(hapd
, ndef
);
362 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
373 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data
*hapd
,
377 struct wpabuf
*req
, *sel
;
379 char *pos
, *role
, *type
, *pos2
;
382 pos
= os_strchr(role
, ' ');
388 pos
= os_strchr(type
, ' ');
393 pos2
= os_strchr(pos
, ' ');
398 len
= os_strlen(pos
);
403 req
= wpabuf_alloc(len
);
406 if (hexstr2bin(pos
, wpabuf_put(req
, len
), len
) < 0) {
411 len
= os_strlen(pos2
);
418 sel
= wpabuf_alloc(len
);
423 if (hexstr2bin(pos2
, wpabuf_put(sel
, len
), len
) < 0) {
429 if (os_strcmp(role
, "RESP") == 0 && os_strcmp(type
, "WPS") == 0) {
430 ret
= hostapd_wps_nfc_report_handover(hapd
, req
, sel
);
432 wpa_printf(MSG_DEBUG
, "NFC: Unsupported connection handover "
433 "reported: role=%s type=%s", role
, type
);
442 #endif /* CONFIG_WPS_NFC */
445 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data
*hapd
, char *txt
,
446 char *buf
, size_t buflen
)
452 pos
= os_strchr(txt
, ' ');
456 if (os_strcmp(txt
, "disable") == 0) {
457 hostapd_wps_ap_pin_disable(hapd
);
458 return os_snprintf(buf
, buflen
, "OK\n");
461 if (os_strcmp(txt
, "random") == 0) {
464 pin_txt
= hostapd_wps_ap_pin_random(hapd
, timeout
);
467 return os_snprintf(buf
, buflen
, "%s", pin_txt
);
470 if (os_strcmp(txt
, "get") == 0) {
471 pin_txt
= hostapd_wps_ap_pin_get(hapd
);
474 return os_snprintf(buf
, buflen
, "%s", pin_txt
);
477 if (os_strcmp(txt
, "set") == 0) {
482 pos
= os_strchr(pos
, ' ');
487 if (os_strlen(pin
) > buflen
)
489 if (hostapd_wps_ap_pin_set(hapd
, pin
, timeout
) < 0)
491 return os_snprintf(buf
, buflen
, "%s", pin
);
498 static int hostapd_ctrl_iface_wps_config(struct hostapd_data
*hapd
, char *txt
)
501 char *ssid
, *auth
, *encr
= NULL
, *key
= NULL
;
504 pos
= os_strchr(txt
, ' ');
510 pos
= os_strchr(pos
, ' ');
514 pos
= os_strchr(pos
, ' ');
521 return hostapd_wps_config_ap(hapd
, ssid
, auth
, encr
, key
);
525 static const char * pbc_status_str(enum pbc_status status
)
528 case WPS_PBC_STATUS_DISABLE
:
530 case WPS_PBC_STATUS_ACTIVE
:
532 case WPS_PBC_STATUS_TIMEOUT
:
534 case WPS_PBC_STATUS_OVERLAP
:
542 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data
*hapd
,
543 char *buf
, size_t buflen
)
551 ret
= os_snprintf(pos
, end
- pos
, "PBC Status: %s\n",
552 pbc_status_str(hapd
->wps_stats
.pbc_status
));
554 if (os_snprintf_error(end
- pos
, ret
))
558 ret
= os_snprintf(pos
, end
- pos
, "Last WPS result: %s\n",
559 (hapd
->wps_stats
.status
== WPS_STATUS_SUCCESS
?
561 (hapd
->wps_stats
.status
== WPS_STATUS_FAILURE
?
562 "Failed" : "None")));
564 if (os_snprintf_error(end
- pos
, ret
))
568 /* If status == Failure - Add possible Reasons */
569 if(hapd
->wps_stats
.status
== WPS_STATUS_FAILURE
&&
570 hapd
->wps_stats
.failure_reason
> 0) {
571 ret
= os_snprintf(pos
, end
- pos
,
572 "Failure Reason: %s\n",
573 wps_ei_str(hapd
->wps_stats
.failure_reason
));
575 if (os_snprintf_error(end
- pos
, ret
))
580 if (hapd
->wps_stats
.status
) {
581 ret
= os_snprintf(pos
, end
- pos
, "Peer Address: " MACSTR
"\n",
582 MAC2STR(hapd
->wps_stats
.peer_addr
));
584 if (os_snprintf_error(end
- pos
, ret
))
592 #endif /* CONFIG_WPS */
596 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data
*hapd
,
602 if (hwaddr_aton(cmd
, addr
))
615 return hs20_send_wnm_notification(hapd
, addr
, 1, url
);
619 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data
*hapd
,
623 int code
, reauth_delay
, ret
;
628 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
629 if (hwaddr_aton(cmd
, addr
))
632 pos
= os_strchr(cmd
, ' ');
638 pos
= os_strchr(pos
, ' ');
642 reauth_delay
= atoi(pos
);
645 pos
= os_strchr(pos
, ' ');
648 url_len
= os_strlen(pos
);
651 req
= wpabuf_alloc(4 + url_len
);
654 wpabuf_put_u8(req
, code
);
655 wpabuf_put_le16(req
, reauth_delay
);
656 wpabuf_put_u8(req
, url_len
);
658 wpabuf_put_data(req
, pos
, url_len
);
660 wpa_printf(MSG_DEBUG
, "HS 2.0: Send WNM-Notification to " MACSTR
661 " to indicate imminent deauthentication (code=%d "
662 "reauth_delay=%d)", MAC2STR(addr
), code
, reauth_delay
);
663 ret
= hs20_send_wnm_notification_deauth_req(hapd
, addr
, req
);
668 #endif /* CONFIG_HS20 */
671 #ifdef CONFIG_INTERWORKING
673 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data
*hapd
,
676 u8 qos_map_set
[16 + 2 * 21], count
= 0;
677 const char *pos
= cmd
;
681 if (count
== sizeof(qos_map_set
)) {
682 wpa_printf(MSG_ERROR
, "Too many qos_map_set parameters");
687 if (val
< 0 || val
> 255) {
688 wpa_printf(MSG_INFO
, "Invalid QoS Map Set");
692 qos_map_set
[count
++] = val
;
693 pos
= os_strchr(pos
, ',');
699 if (count
< 16 || count
& 1) {
700 wpa_printf(MSG_INFO
, "Invalid QoS Map Set");
704 ret
= hostapd_drv_set_qos_map(hapd
, qos_map_set
, count
);
706 wpa_printf(MSG_INFO
, "Failed to set QoS Map Set");
710 os_memcpy(hapd
->conf
->qos_map_set
, qos_map_set
, count
);
711 hapd
->conf
->qos_map_set_len
= count
;
717 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data
*hapd
,
721 struct sta_info
*sta
;
723 u8
*qos_map_set
= hapd
->conf
->qos_map_set
;
724 u8 qos_map_set_len
= hapd
->conf
->qos_map_set_len
;
727 if (!qos_map_set_len
) {
728 wpa_printf(MSG_INFO
, "QoS Map Set is not set");
732 if (hwaddr_aton(cmd
, addr
))
735 sta
= ap_get_sta(hapd
, addr
);
737 wpa_printf(MSG_DEBUG
, "Station " MACSTR
" not found "
738 "for QoS Map Configuration message",
743 if (!sta
->qos_map_enabled
) {
744 wpa_printf(MSG_DEBUG
, "Station " MACSTR
" did not indicate "
745 "support for QoS Map", MAC2STR(addr
));
749 buf
= wpabuf_alloc(2 + 2 + qos_map_set_len
);
753 wpabuf_put_u8(buf
, WLAN_ACTION_QOS
);
754 wpabuf_put_u8(buf
, QOS_QOS_MAP_CONFIG
);
756 /* QoS Map Set Element */
757 wpabuf_put_u8(buf
, WLAN_EID_QOS_MAP_SET
);
758 wpabuf_put_u8(buf
, qos_map_set_len
);
759 wpabuf_put_data(buf
, qos_map_set
, qos_map_set_len
);
761 ret
= hostapd_drv_send_action(hapd
, hapd
->iface
->freq
, 0, addr
,
762 wpabuf_head(buf
), wpabuf_len(buf
));
768 #endif /* CONFIG_INTERWORKING */
773 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data
*hapd
,
778 struct sta_info
*sta
;
780 if (hwaddr_aton(cmd
, addr
))
784 disassoc_timer
= atoi(cmd
+ 17);
786 sta
= ap_get_sta(hapd
, addr
);
788 wpa_printf(MSG_DEBUG
, "Station " MACSTR
789 " not found for disassociation imminent message",
794 return wnm_send_disassoc_imminent(hapd
, sta
, disassoc_timer
);
798 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data
*hapd
,
802 const char *url
, *timerstr
;
804 struct sta_info
*sta
;
806 if (hwaddr_aton(cmd
, addr
))
809 sta
= ap_get_sta(hapd
, addr
);
811 wpa_printf(MSG_DEBUG
, "Station " MACSTR
812 " not found for ESS disassociation imminent message",
818 if (*timerstr
!= ' ')
821 disassoc_timer
= atoi(timerstr
);
822 if (disassoc_timer
< 0 || disassoc_timer
> 65535)
825 url
= os_strchr(timerstr
, ' ');
830 return wnm_send_ess_disassoc_imminent(hapd
, sta
, url
, disassoc_timer
);
834 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data
*hapd
,
838 const char *pos
, *end
;
839 int disassoc_timer
= 0;
840 struct sta_info
*sta
;
841 u8 req_mode
= 0, valid_int
= 0x01;
850 if (hwaddr_aton(cmd
, addr
)) {
851 wpa_printf(MSG_DEBUG
, "Invalid STA MAC address");
855 sta
= ap_get_sta(hapd
, addr
);
857 wpa_printf(MSG_DEBUG
, "Station " MACSTR
858 " not found for BSS TM Request message",
863 pos
= os_strstr(cmd
, " disassoc_timer=");
866 disassoc_timer
= atoi(pos
);
867 if (disassoc_timer
< 0 || disassoc_timer
> 65535) {
868 wpa_printf(MSG_DEBUG
, "Invalid disassoc_timer");
873 pos
= os_strstr(cmd
, " valid_int=");
876 valid_int
= atoi(pos
);
879 pos
= os_strstr(cmd
, " bss_term=");
882 req_mode
|= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED
;
883 /* TODO: TSF configurable/learnable */
884 bss_term_dur
[0] = 4; /* Subelement ID */
885 bss_term_dur
[1] = 10; /* Length */
886 os_memset(bss_term_dur
, 2, 8);
887 end
= os_strchr(pos
, ',');
889 wpa_printf(MSG_DEBUG
, "Invalid bss_term data");
893 WPA_PUT_LE16(&bss_term_dur
[10], atoi(end
));
896 nei_len
= ieee802_11_parse_candidate_list(cmd
, nei_rep
,
901 pos
= os_strstr(cmd
, " url=");
905 end
= os_strchr(pos
, ' ');
909 len
= os_strlen(pos
);
910 url
= os_malloc(len
+ 1);
913 os_memcpy(url
, pos
, len
);
915 req_mode
|= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT
;
918 if (os_strstr(cmd
, " pref=1"))
919 req_mode
|= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED
;
920 if (os_strstr(cmd
, " abridged=1"))
921 req_mode
|= WNM_BSS_TM_REQ_ABRIDGED
;
922 if (os_strstr(cmd
, " disassoc_imminent=1"))
923 req_mode
|= WNM_BSS_TM_REQ_DISASSOC_IMMINENT
;
926 pos
= os_strstr(cmd
, "mbo=");
928 unsigned int mbo_reason
, cell_pref
, reassoc_delay
;
931 ret
= sscanf(pos
, "mbo=%u:%u:%u", &mbo_reason
,
932 &reassoc_delay
, &cell_pref
);
934 wpa_printf(MSG_DEBUG
,
935 "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
940 if (mbo_reason
> MBO_TRANSITION_REASON_PREMIUM_AP
) {
941 wpa_printf(MSG_DEBUG
,
942 "Invalid MBO transition reason code %u",
948 /* Valid values for Cellular preference are: 0, 1, 255 */
949 if (cell_pref
!= 0 && cell_pref
!= 1 && cell_pref
!= 255) {
950 wpa_printf(MSG_DEBUG
,
951 "Invalid MBO cellular capability %u",
957 if (reassoc_delay
> 65535 ||
959 !(req_mode
& WNM_BSS_TM_REQ_DISASSOC_IMMINENT
))) {
960 wpa_printf(MSG_DEBUG
,
961 "MBO: Assoc retry delay is only valid in disassoc imminent mode");
966 *mbo_pos
++ = MBO_ATTR_ID_TRANSITION_REASON
;
968 *mbo_pos
++ = mbo_reason
;
969 *mbo_pos
++ = MBO_ATTR_ID_CELL_DATA_PREF
;
971 *mbo_pos
++ = cell_pref
;
974 *mbo_pos
++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY
;
976 WPA_PUT_LE16(mbo_pos
, reassoc_delay
);
980 mbo_len
= mbo_pos
- mbo
;
982 #endif /* CONFIG_MBO */
984 ret
= wnm_send_bss_tm_req(hapd
, sta
, req_mode
, disassoc_timer
,
985 valid_int
, bss_term_dur
, url
,
986 nei_len
? nei_rep
: NULL
, nei_len
,
987 mbo_len
? mbo
: NULL
, mbo_len
);
990 #endif /* CONFIG_MBO */
995 #endif /* CONFIG_WNM_AP */
998 static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data
*hapd
,
999 char *buf
, size_t buflen
)
1007 WPA_ASSERT(hapd
->conf
->wpa_key_mgmt
);
1009 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_PSK
) {
1010 ret
= os_snprintf(pos
, end
- pos
, "WPA-PSK ");
1011 if (os_snprintf_error(end
- pos
, ret
))
1015 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X
) {
1016 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP ");
1017 if (os_snprintf_error(end
- pos
, ret
))
1021 #ifdef CONFIG_IEEE80211R_AP
1022 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_PSK
) {
1023 ret
= os_snprintf(pos
, end
- pos
, "FT-PSK ");
1024 if (os_snprintf_error(end
- pos
, ret
))
1028 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_IEEE8021X
) {
1029 ret
= os_snprintf(pos
, end
- pos
, "FT-EAP ");
1030 if (os_snprintf_error(end
- pos
, ret
))
1034 #ifdef CONFIG_SHA384
1035 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_IEEE8021X_SHA384
) {
1036 ret
= os_snprintf(pos
, end
- pos
, "FT-EAP-SHA384 ");
1037 if (os_snprintf_error(end
- pos
, ret
))
1041 #endif /* CONFIG_SHA384 */
1043 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_SAE
) {
1044 ret
= os_snprintf(pos
, end
- pos
, "FT-SAE ");
1045 if (os_snprintf_error(end
- pos
, ret
))
1049 #endif /* CONFIG_SAE */
1051 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_FILS_SHA256
) {
1052 ret
= os_snprintf(pos
, end
- pos
, "FT-FILS-SHA256 ");
1053 if (os_snprintf_error(end
- pos
, ret
))
1057 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_FILS_SHA384
) {
1058 ret
= os_snprintf(pos
, end
- pos
, "FT-FILS-SHA384 ");
1059 if (os_snprintf_error(end
- pos
, ret
))
1063 #endif /* CONFIG_FILS */
1064 #endif /* CONFIG_IEEE80211R_AP */
1065 #ifdef CONFIG_IEEE80211W
1066 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_PSK_SHA256
) {
1067 ret
= os_snprintf(pos
, end
- pos
, "WPA-PSK-SHA256 ");
1068 if (os_snprintf_error(end
- pos
, ret
))
1072 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X_SHA256
) {
1073 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP-SHA256 ");
1074 if (os_snprintf_error(end
- pos
, ret
))
1078 #endif /* CONFIG_IEEE80211W */
1080 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_SAE
) {
1081 ret
= os_snprintf(pos
, end
- pos
, "SAE ");
1082 if (os_snprintf_error(end
- pos
, ret
))
1086 #endif /* CONFIG_SAE */
1087 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X_SUITE_B
) {
1088 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP-SUITE-B ");
1089 if (os_snprintf_error(end
- pos
, ret
))
1093 if (hapd
->conf
->wpa_key_mgmt
&
1094 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192
) {
1095 ret
= os_snprintf(pos
, end
- pos
,
1096 "WPA-EAP-SUITE-B-192 ");
1097 if (os_snprintf_error(end
- pos
, ret
))
1102 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FILS_SHA256
) {
1103 ret
= os_snprintf(pos
, end
- pos
, "FILS-SHA256 ");
1104 if (os_snprintf_error(end
- pos
, ret
))
1108 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FILS_SHA384
) {
1109 ret
= os_snprintf(pos
, end
- pos
, "FILS-SHA384 ");
1110 if (os_snprintf_error(end
- pos
, ret
))
1114 #endif /* CONFIG_FILS */
1117 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_OWE
) {
1118 ret
= os_snprintf(pos
, end
- pos
, "OWE ");
1119 if (os_snprintf_error(end
- pos
, ret
))
1123 #endif /* CONFIG_OWE */
1126 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_DPP
) {
1127 ret
= os_snprintf(pos
, end
- pos
, "DPP ");
1128 if (os_snprintf_error(end
- pos
, ret
))
1132 #endif /* CONFIG_DPP */
1134 if (pos
> buf
&& *(pos
- 1) == ' ') {
1143 static int hostapd_ctrl_iface_get_config(struct hostapd_data
*hapd
,
1144 char *buf
, size_t buflen
)
1152 ret
= os_snprintf(pos
, end
- pos
, "bssid=" MACSTR
"\n"
1154 MAC2STR(hapd
->own_addr
),
1155 wpa_ssid_txt(hapd
->conf
->ssid
.ssid
,
1156 hapd
->conf
->ssid
.ssid_len
));
1157 if (os_snprintf_error(end
- pos
, ret
))
1162 ret
= os_snprintf(pos
, end
- pos
, "wps_state=%s\n",
1163 hapd
->conf
->wps_state
== 0 ? "disabled" :
1164 (hapd
->conf
->wps_state
== 1 ? "not configured" :
1166 if (os_snprintf_error(end
- pos
, ret
))
1170 if (hapd
->conf
->wps_state
&& hapd
->conf
->wpa
&&
1171 hapd
->conf
->ssid
.wpa_passphrase
) {
1172 ret
= os_snprintf(pos
, end
- pos
, "passphrase=%s\n",
1173 hapd
->conf
->ssid
.wpa_passphrase
);
1174 if (os_snprintf_error(end
- pos
, ret
))
1179 if (hapd
->conf
->wps_state
&& hapd
->conf
->wpa
&&
1180 hapd
->conf
->ssid
.wpa_psk
&&
1181 hapd
->conf
->ssid
.wpa_psk
->group
) {
1182 char hex
[PMK_LEN
* 2 + 1];
1183 wpa_snprintf_hex(hex
, sizeof(hex
),
1184 hapd
->conf
->ssid
.wpa_psk
->psk
, PMK_LEN
);
1185 ret
= os_snprintf(pos
, end
- pos
, "psk=%s\n", hex
);
1186 if (os_snprintf_error(end
- pos
, ret
))
1190 #endif /* CONFIG_WPS */
1192 if (hapd
->conf
->wpa
) {
1193 ret
= os_snprintf(pos
, end
- pos
, "wpa=%d\n", hapd
->conf
->wpa
);
1194 if (os_snprintf_error(end
- pos
, ret
))
1199 if (hapd
->conf
->wpa
&& hapd
->conf
->wpa_key_mgmt
) {
1200 ret
= os_snprintf(pos
, end
- pos
, "key_mgmt=");
1201 if (os_snprintf_error(end
- pos
, ret
))
1205 pos
+= hostapd_ctrl_iface_get_key_mgmt(hapd
, pos
, end
- pos
);
1207 ret
= os_snprintf(pos
, end
- pos
, "\n");
1208 if (os_snprintf_error(end
- pos
, ret
))
1213 if (hapd
->conf
->wpa
) {
1214 ret
= os_snprintf(pos
, end
- pos
, "group_cipher=%s\n",
1215 wpa_cipher_txt(hapd
->conf
->wpa_group
));
1216 if (os_snprintf_error(end
- pos
, ret
))
1221 if ((hapd
->conf
->wpa
& WPA_PROTO_RSN
) && hapd
->conf
->rsn_pairwise
) {
1222 ret
= os_snprintf(pos
, end
- pos
, "rsn_pairwise_cipher=");
1223 if (os_snprintf_error(end
- pos
, ret
))
1227 ret
= wpa_write_ciphers(pos
, end
, hapd
->conf
->rsn_pairwise
,
1233 ret
= os_snprintf(pos
, end
- pos
, "\n");
1234 if (os_snprintf_error(end
- pos
, ret
))
1239 if ((hapd
->conf
->wpa
& WPA_PROTO_WPA
) && hapd
->conf
->wpa_pairwise
) {
1240 ret
= os_snprintf(pos
, end
- pos
, "wpa_pairwise_cipher=");
1241 if (os_snprintf_error(end
- pos
, ret
))
1245 ret
= wpa_write_ciphers(pos
, end
, hapd
->conf
->wpa_pairwise
,
1251 ret
= os_snprintf(pos
, end
- pos
, "\n");
1252 if (os_snprintf_error(end
- pos
, ret
))
1261 static void hostapd_disassoc_accept_mac(struct hostapd_data
*hapd
)
1263 struct sta_info
*sta
;
1264 struct vlan_description vlan_id
;
1266 if (hapd
->conf
->macaddr_acl
!= DENY_UNLESS_ACCEPTED
)
1269 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
1270 if (!hostapd_maclist_found(hapd
->conf
->accept_mac
,
1271 hapd
->conf
->num_accept_mac
,
1272 sta
->addr
, &vlan_id
) ||
1273 (vlan_id
.notempty
&&
1274 vlan_compare(&vlan_id
, sta
->vlan_desc
)))
1275 ap_sta_disconnect(hapd
, sta
, sta
->addr
,
1276 WLAN_REASON_UNSPECIFIED
);
1281 static void hostapd_disassoc_deny_mac(struct hostapd_data
*hapd
)
1283 struct sta_info
*sta
;
1284 struct vlan_description vlan_id
;
1286 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
1287 if (hostapd_maclist_found(hapd
->conf
->deny_mac
,
1288 hapd
->conf
->num_deny_mac
, sta
->addr
,
1290 (!vlan_id
.notempty
||
1291 !vlan_compare(&vlan_id
, sta
->vlan_desc
)))
1292 ap_sta_disconnect(hapd
, sta
, sta
->addr
,
1293 WLAN_REASON_UNSPECIFIED
);
1297 static int hostapd_ctrl_iface_set(struct hostapd_data
*hapd
, char *cmd
)
1302 value
= os_strchr(cmd
, ' ');
1307 wpa_printf(MSG_DEBUG
, "CTRL_IFACE SET '%s'='%s'", cmd
, value
);
1309 #ifdef CONFIG_WPS_TESTING
1310 } else if (os_strcasecmp(cmd
, "wps_version_number") == 0) {
1312 val
= strtol(value
, NULL
, 0);
1313 if (val
< 0 || val
> 0xff) {
1315 wpa_printf(MSG_DEBUG
, "WPS: Invalid "
1316 "wps_version_number %ld", val
);
1318 wps_version_number
= val
;
1319 wpa_printf(MSG_DEBUG
, "WPS: Testing - force WPS "
1321 (wps_version_number
& 0xf0) >> 4,
1322 wps_version_number
& 0x0f);
1323 hostapd_wps_update_ie(hapd
);
1325 } else if (os_strcasecmp(cmd
, "wps_testing_dummy_cred") == 0) {
1326 wps_testing_dummy_cred
= atoi(value
);
1327 wpa_printf(MSG_DEBUG
, "WPS: Testing - dummy_cred=%d",
1328 wps_testing_dummy_cred
);
1329 } else if (os_strcasecmp(cmd
, "wps_corrupt_pkhash") == 0) {
1330 wps_corrupt_pkhash
= atoi(value
);
1331 wpa_printf(MSG_DEBUG
, "WPS: Testing - wps_corrupt_pkhash=%d",
1332 wps_corrupt_pkhash
);
1333 #endif /* CONFIG_WPS_TESTING */
1334 #ifdef CONFIG_TESTING_OPTIONS
1335 } else if (os_strcasecmp(cmd
, "ext_mgmt_frame_handling") == 0) {
1336 hapd
->ext_mgmt_frame_handling
= atoi(value
);
1337 } else if (os_strcasecmp(cmd
, "ext_eapol_frame_io") == 0) {
1338 hapd
->ext_eapol_frame_io
= atoi(value
);
1340 } else if (os_strcasecmp(cmd
, "dpp_config_obj_override") == 0) {
1341 os_free(hapd
->dpp_config_obj_override
);
1342 hapd
->dpp_config_obj_override
= os_strdup(value
);
1343 } else if (os_strcasecmp(cmd
, "dpp_discovery_override") == 0) {
1344 os_free(hapd
->dpp_discovery_override
);
1345 hapd
->dpp_discovery_override
= os_strdup(value
);
1346 } else if (os_strcasecmp(cmd
, "dpp_groups_override") == 0) {
1347 os_free(hapd
->dpp_groups_override
);
1348 hapd
->dpp_groups_override
= os_strdup(value
);
1349 } else if (os_strcasecmp(cmd
,
1350 "dpp_ignore_netaccesskey_mismatch") == 0) {
1351 hapd
->dpp_ignore_netaccesskey_mismatch
= atoi(value
);
1352 } else if (os_strcasecmp(cmd
, "dpp_test") == 0) {
1353 dpp_test
= atoi(value
);
1354 #endif /* CONFIG_DPP */
1355 #endif /* CONFIG_TESTING_OPTIONS */
1357 } else if (os_strcasecmp(cmd
, "mbo_assoc_disallow") == 0) {
1360 if (!hapd
->conf
->mbo_enabled
)
1364 if (val
< 0 || val
> 1)
1367 hapd
->mbo_assoc_disallow
= val
;
1368 ieee802_11_update_beacons(hapd
->iface
);
1371 * TODO: Need to configure drivers that do AP MLME offload with
1372 * disallowing station logic.
1374 #endif /* CONFIG_MBO */
1376 } else if (os_strcasecmp(cmd
, "dpp_configurator_params") == 0) {
1377 os_free(hapd
->dpp_configurator_params
);
1378 hapd
->dpp_configurator_params
= os_strdup(value
);
1379 #endif /* CONFIG_DPP */
1381 ret
= hostapd_set_iface(hapd
->iconf
, hapd
->conf
, cmd
, value
);
1385 if (os_strcasecmp(cmd
, "deny_mac_file") == 0) {
1386 hostapd_disassoc_deny_mac(hapd
);
1387 } else if (os_strcasecmp(cmd
, "accept_mac_file") == 0) {
1388 hostapd_disassoc_accept_mac(hapd
);
1396 static int hostapd_ctrl_iface_get(struct hostapd_data
*hapd
, char *cmd
,
1397 char *buf
, size_t buflen
)
1401 wpa_printf(MSG_DEBUG
, "CTRL_IFACE GET '%s'", cmd
);
1403 if (os_strcmp(cmd
, "version") == 0) {
1404 res
= os_snprintf(buf
, buflen
, "%s", VERSION_STR
);
1405 if (os_snprintf_error(buflen
, res
))
1408 } else if (os_strcmp(cmd
, "tls_library") == 0) {
1409 res
= tls_get_library_version(buf
, buflen
);
1410 if (os_snprintf_error(buflen
, res
))
1419 static int hostapd_ctrl_iface_enable(struct hostapd_iface
*iface
)
1421 if (hostapd_enable_iface(iface
) < 0) {
1422 wpa_printf(MSG_ERROR
, "Enabling of interface failed");
1429 static int hostapd_ctrl_iface_reload(struct hostapd_iface
*iface
)
1431 if (hostapd_reload_iface(iface
) < 0) {
1432 wpa_printf(MSG_ERROR
, "Reloading of interface failed");
1439 static int hostapd_ctrl_iface_disable(struct hostapd_iface
*iface
)
1441 if (hostapd_disable_iface(iface
) < 0) {
1442 wpa_printf(MSG_ERROR
, "Disabling of interface failed");
1449 #ifdef CONFIG_TESTING_OPTIONS
1451 static int hostapd_ctrl_iface_radar(struct hostapd_data
*hapd
, char *cmd
)
1453 union wpa_event_data data
;
1455 enum wpa_event_type event
;
1457 wpa_printf(MSG_DEBUG
, "RADAR TEST: %s", cmd
);
1459 os_memset(&data
, 0, sizeof(data
));
1461 param
= os_strchr(cmd
, ' ');
1466 if (os_strcmp(cmd
, "DETECTED") == 0)
1467 event
= EVENT_DFS_RADAR_DETECTED
;
1468 else if (os_strcmp(cmd
, "CAC-FINISHED") == 0)
1469 event
= EVENT_DFS_CAC_FINISHED
;
1470 else if (os_strcmp(cmd
, "CAC-ABORTED") == 0)
1471 event
= EVENT_DFS_CAC_ABORTED
;
1472 else if (os_strcmp(cmd
, "NOP-FINISHED") == 0)
1473 event
= EVENT_DFS_NOP_FINISHED
;
1475 wpa_printf(MSG_DEBUG
, "Unsupported RADAR test command: %s",
1480 pos
= os_strstr(param
, "freq=");
1482 data
.dfs_event
.freq
= atoi(pos
+ 5);
1484 pos
= os_strstr(param
, "ht_enabled=1");
1486 data
.dfs_event
.ht_enabled
= 1;
1488 pos
= os_strstr(param
, "chan_offset=");
1490 data
.dfs_event
.chan_offset
= atoi(pos
+ 12);
1492 pos
= os_strstr(param
, "chan_width=");
1494 data
.dfs_event
.chan_width
= atoi(pos
+ 11);
1496 pos
= os_strstr(param
, "cf1=");
1498 data
.dfs_event
.cf1
= atoi(pos
+ 4);
1500 pos
= os_strstr(param
, "cf2=");
1502 data
.dfs_event
.cf2
= atoi(pos
+ 4);
1504 wpa_supplicant_event(hapd
, event
, &data
);
1510 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data
*hapd
, char *cmd
)
1516 wpa_printf(MSG_DEBUG
, "External MGMT TX: %s", cmd
);
1518 len
= os_strlen(cmd
);
1523 buf
= os_malloc(len
);
1527 if (hexstr2bin(cmd
, buf
, len
) < 0) {
1532 res
= hostapd_drv_send_mlme(hapd
, buf
, len
, 0);
1538 static int hostapd_ctrl_iface_mgmt_tx_status_process(struct hostapd_data
*hapd
,
1544 int stype
= 0, ok
= 0;
1545 union wpa_event_data event
;
1547 if (!hapd
->ext_mgmt_frame_handling
)
1550 /* stype=<val> ok=<0/1> buf=<frame hexdump> */
1552 wpa_printf(MSG_DEBUG
, "External MGMT TX status process: %s", cmd
);
1555 param
= os_strstr(pos
, "stype=");
1558 stype
= atoi(param
);
1561 param
= os_strstr(pos
, " ok=");
1567 param
= os_strstr(pos
, " buf=");
1572 len
= os_strlen(param
);
1577 buf
= os_malloc(len
);
1578 if (!buf
|| hexstr2bin(param
, buf
, len
) < 0) {
1583 os_memset(&event
, 0, sizeof(event
));
1584 event
.tx_status
.type
= WLAN_FC_TYPE_MGMT
;
1585 event
.tx_status
.data
= buf
;
1586 event
.tx_status
.data_len
= len
;
1587 event
.tx_status
.stype
= stype
;
1588 event
.tx_status
.ack
= ok
;
1589 hapd
->ext_mgmt_frame_handling
= 0;
1590 wpa_supplicant_event(hapd
, EVENT_TX_STATUS
, &event
);
1591 hapd
->ext_mgmt_frame_handling
= 1;
1599 static int hostapd_ctrl_iface_mgmt_rx_process(struct hostapd_data
*hapd
,
1605 int freq
= 0, datarate
= 0, ssi_signal
= 0;
1606 union wpa_event_data event
;
1608 if (!hapd
->ext_mgmt_frame_handling
)
1611 /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
1613 wpa_printf(MSG_DEBUG
, "External MGMT RX process: %s", cmd
);
1616 param
= os_strstr(pos
, "freq=");
1622 param
= os_strstr(pos
, " datarate=");
1625 datarate
= atoi(param
);
1628 param
= os_strstr(pos
, " ssi_signal=");
1631 ssi_signal
= atoi(param
);
1634 param
= os_strstr(pos
, " frame=");
1639 len
= os_strlen(param
);
1644 buf
= os_malloc(len
);
1648 if (hexstr2bin(param
, buf
, len
) < 0) {
1653 os_memset(&event
, 0, sizeof(event
));
1654 event
.rx_mgmt
.freq
= freq
;
1655 event
.rx_mgmt
.frame
= buf
;
1656 event
.rx_mgmt
.frame_len
= len
;
1657 event
.rx_mgmt
.ssi_signal
= ssi_signal
;
1658 event
.rx_mgmt
.datarate
= datarate
;
1659 hapd
->ext_mgmt_frame_handling
= 0;
1660 wpa_supplicant_event(hapd
, EVENT_RX_MGMT
, &event
);
1661 hapd
->ext_mgmt_frame_handling
= 1;
1669 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data
*hapd
, char *cmd
)
1672 u8 src
[ETH_ALEN
], *buf
;
1676 wpa_printf(MSG_DEBUG
, "External EAPOL RX: %s", cmd
);
1679 used
= hwaddr_aton2(pos
, src
);
1686 len
= os_strlen(pos
);
1691 buf
= os_malloc(len
);
1695 if (hexstr2bin(pos
, buf
, len
) < 0) {
1700 ieee802_1x_receive(hapd
, src
, buf
, len
);
1707 static u16
ipv4_hdr_checksum(const void *buf
, size_t len
)
1711 const u16
*pos
= buf
;
1713 for (i
= 0; i
< len
/ 2; i
++)
1717 sum
= (sum
& 0xffff) + (sum
>> 16);
1719 return sum
^ 0xffff;
1723 #define HWSIM_PACKETLEN 1500
1724 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1726 static void hostapd_data_test_rx(void *ctx
, const u8
*src_addr
, const u8
*buf
,
1729 struct hostapd_data
*hapd
= ctx
;
1730 const struct ether_header
*eth
;
1735 if (len
!= HWSIM_PACKETLEN
)
1738 eth
= (const struct ether_header
*) buf
;
1739 os_memcpy(&ip
, eth
+ 1, sizeof(ip
));
1740 pos
= &buf
[sizeof(*eth
) + sizeof(ip
)];
1742 if (ip
.ihl
!= 5 || ip
.version
!= 4 ||
1743 ntohs(ip
.tot_len
) != HWSIM_IP_LEN
)
1746 for (i
= 0; i
< HWSIM_IP_LEN
- sizeof(ip
); i
++) {
1752 wpa_msg(hapd
->msg_ctx
, MSG_INFO
, "DATA-TEST-RX " MACSTR
" " MACSTR
,
1753 MAC2STR(eth
->ether_dhost
), MAC2STR(eth
->ether_shost
));
1757 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data
*hapd
,
1760 int enabled
= atoi(cmd
);
1765 if (hapd
->l2_test
) {
1766 l2_packet_deinit(hapd
->l2_test
);
1767 hapd
->l2_test
= NULL
;
1768 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
,
1769 "test data: Disabled");
1777 pos
= os_strstr(cmd
, " ifname=");
1781 ifname
= hapd
->conf
->iface
;
1783 hapd
->l2_test
= l2_packet_init(ifname
, hapd
->own_addr
,
1784 ETHERTYPE_IP
, hostapd_data_test_rx
,
1786 if (hapd
->l2_test
== NULL
)
1789 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: Enabled");
1795 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data
*hapd
, char *cmd
)
1797 u8 dst
[ETH_ALEN
], src
[ETH_ALEN
];
1802 u8 buf
[2 + HWSIM_PACKETLEN
];
1803 struct ether_header
*eth
;
1808 if (hapd
->l2_test
== NULL
)
1811 /* format: <dst> <src> <tos> */
1814 used
= hwaddr_aton2(pos
, dst
);
1820 used
= hwaddr_aton2(pos
, src
);
1825 val
= strtol(pos
, NULL
, 0);
1826 if (val
< 0 || val
> 0xff)
1830 eth
= (struct ether_header
*) &buf
[2];
1831 os_memcpy(eth
->ether_dhost
, dst
, ETH_ALEN
);
1832 os_memcpy(eth
->ether_shost
, src
, ETH_ALEN
);
1833 eth
->ether_type
= htons(ETHERTYPE_IP
);
1834 ip
= (struct iphdr
*) (eth
+ 1);
1835 os_memset(ip
, 0, sizeof(*ip
));
1840 ip
->tot_len
= htons(HWSIM_IP_LEN
);
1842 ip
->saddr
= htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1843 ip
->daddr
= htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1844 ip
->check
= ipv4_hdr_checksum(ip
, sizeof(*ip
));
1845 dpos
= (u8
*) (ip
+ 1);
1846 for (i
= 0; i
< HWSIM_IP_LEN
- sizeof(*ip
); i
++)
1849 if (l2_packet_send(hapd
->l2_test
, dst
, ETHERTYPE_IP
, &buf
[2],
1850 HWSIM_PACKETLEN
) < 0)
1853 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: TX dst=" MACSTR
1854 " src=" MACSTR
" tos=0x%x", MAC2STR(dst
), MAC2STR(src
), tos
);
1860 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data
*hapd
,
1864 struct ether_header
*eth
;
1865 struct l2_packet_data
*l2
= NULL
;
1869 const char *ifname
= hapd
->conf
->iface
;
1871 if (os_strncmp(cmd
, "ifname=", 7) == 0) {
1874 cmd
= os_strchr(cmd
, ' ');
1880 len
= os_strlen(cmd
);
1881 if (len
& 1 || len
< ETH_HLEN
* 2)
1885 buf
= os_malloc(len
);
1889 if (hexstr2bin(cmd
, buf
, len
) < 0)
1892 eth
= (struct ether_header
*) buf
;
1893 ethertype
= ntohs(eth
->ether_type
);
1895 l2
= l2_packet_init(ifname
, hapd
->own_addr
, ethertype
,
1896 hostapd_data_test_rx
, hapd
, 1);
1900 res
= l2_packet_send(l2
, eth
->ether_dhost
, ethertype
, buf
, len
);
1901 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: TX frame res=%d", res
);
1904 l2_packet_deinit(l2
);
1907 return res
< 0 ? -1 : 0;
1911 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data
*hapd
, char *cmd
)
1913 #ifdef WPA_TRACE_BFD
1916 wpa_trace_fail_after
= atoi(cmd
);
1917 pos
= os_strchr(cmd
, ':');
1920 os_strlcpy(wpa_trace_fail_func
, pos
,
1921 sizeof(wpa_trace_fail_func
));
1923 wpa_trace_fail_after
= 0;
1927 #else /* WPA_TRACE_BFD */
1929 #endif /* WPA_TRACE_BFD */
1933 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data
*hapd
,
1934 char *buf
, size_t buflen
)
1936 #ifdef WPA_TRACE_BFD
1937 return os_snprintf(buf
, buflen
, "%u:%s", wpa_trace_fail_after
,
1938 wpa_trace_fail_func
);
1939 #else /* WPA_TRACE_BFD */
1941 #endif /* WPA_TRACE_BFD */
1945 static int hostapd_ctrl_test_fail(struct hostapd_data
*hapd
, char *cmd
)
1947 #ifdef WPA_TRACE_BFD
1950 wpa_trace_test_fail_after
= atoi(cmd
);
1951 pos
= os_strchr(cmd
, ':');
1954 os_strlcpy(wpa_trace_test_fail_func
, pos
,
1955 sizeof(wpa_trace_test_fail_func
));
1957 wpa_trace_test_fail_after
= 0;
1961 #else /* WPA_TRACE_BFD */
1963 #endif /* WPA_TRACE_BFD */
1967 static int hostapd_ctrl_get_fail(struct hostapd_data
*hapd
,
1968 char *buf
, size_t buflen
)
1970 #ifdef WPA_TRACE_BFD
1971 return os_snprintf(buf
, buflen
, "%u:%s", wpa_trace_test_fail_after
,
1972 wpa_trace_test_fail_func
);
1973 #else /* WPA_TRACE_BFD */
1975 #endif /* WPA_TRACE_BFD */
1979 static int hostapd_ctrl_reset_pn(struct hostapd_data
*hapd
, const char *cmd
)
1981 struct sta_info
*sta
;
1983 u8 zero
[WPA_TK_MAX_LEN
];
1985 os_memset(zero
, 0, sizeof(zero
));
1987 if (hwaddr_aton(cmd
, addr
))
1990 #ifdef CONFIG_IEEE80211W
1991 if (is_broadcast_ether_addr(addr
) && os_strstr(cmd
, "IGTK")) {
1992 if (hapd
->last_igtk_alg
== WPA_ALG_NONE
)
1995 wpa_printf(MSG_INFO
, "TESTING: Reset IPN for IGTK");
1997 /* First, use a zero key to avoid any possible duplicate key
1998 * avoidance in the driver. */
1999 if (hostapd_drv_set_key(hapd
->conf
->iface
, hapd
,
2000 hapd
->last_igtk_alg
,
2001 broadcast_ether_addr
,
2002 hapd
->last_igtk_key_idx
, 1, NULL
, 0,
2003 zero
, hapd
->last_igtk_len
) < 0)
2006 /* Set the previously configured key to reset its TSC */
2007 return hostapd_drv_set_key(hapd
->conf
->iface
, hapd
,
2008 hapd
->last_igtk_alg
,
2009 broadcast_ether_addr
,
2010 hapd
->last_igtk_key_idx
, 1, NULL
, 0,
2012 hapd
->last_igtk_len
);
2014 #endif /* CONFIG_IEEE80211W */
2016 if (is_broadcast_ether_addr(addr
)) {
2017 if (hapd
->last_gtk_alg
== WPA_ALG_NONE
)
2020 wpa_printf(MSG_INFO
, "TESTING: Reset PN for GTK");
2022 /* First, use a zero key to avoid any possible duplicate key
2023 * avoidance in the driver. */
2024 if (hostapd_drv_set_key(hapd
->conf
->iface
, hapd
,
2026 broadcast_ether_addr
,
2027 hapd
->last_gtk_key_idx
, 1, NULL
, 0,
2028 zero
, hapd
->last_gtk_len
) < 0)
2031 /* Set the previously configured key to reset its TSC */
2032 return hostapd_drv_set_key(hapd
->conf
->iface
, hapd
,
2034 broadcast_ether_addr
,
2035 hapd
->last_gtk_key_idx
, 1, NULL
, 0,
2036 hapd
->last_gtk
, hapd
->last_gtk_len
);
2039 sta
= ap_get_sta(hapd
, addr
);
2043 if (sta
->last_tk_alg
== WPA_ALG_NONE
)
2046 wpa_printf(MSG_INFO
, "TESTING: Reset PN for " MACSTR
,
2047 MAC2STR(sta
->addr
));
2049 /* First, use a zero key to avoid any possible duplicate key avoidance
2051 if (hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, sta
->last_tk_alg
,
2052 sta
->addr
, sta
->last_tk_key_idx
, 1, NULL
, 0,
2053 zero
, sta
->last_tk_len
) < 0)
2056 /* Set the previously configured key to reset its TSC/RSC */
2057 return hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, sta
->last_tk_alg
,
2058 sta
->addr
, sta
->last_tk_key_idx
, 1, NULL
, 0,
2059 sta
->last_tk
, sta
->last_tk_len
);
2063 static int hostapd_ctrl_set_key(struct hostapd_data
*hapd
, const char *cmd
)
2066 const char *pos
= cmd
;
2069 u8 seq
[6], key
[WPA_TK_MAX_LEN
];
2072 /* parameters: alg addr idx set_tx seq key */
2075 pos
= os_strchr(pos
, ' ');
2079 if (hwaddr_aton(pos
, addr
))
2086 pos
= os_strchr(pos
, ' ');
2091 pos
= os_strchr(pos
, ' ');
2095 if (hexstr2bin(pos
, seq
, sizeof(6)) < 0)
2101 key_len
= os_strlen(pos
) / 2;
2102 if (hexstr2bin(pos
, key
, key_len
) < 0)
2105 wpa_printf(MSG_INFO
, "TESTING: Set key");
2106 return hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, alg
, addr
, idx
,
2107 set_tx
, seq
, 6, key
, key_len
);
2111 static void restore_tk(void *ctx1
, void *ctx2
)
2113 struct hostapd_data
*hapd
= ctx1
;
2114 struct sta_info
*sta
= ctx2
;
2116 wpa_printf(MSG_INFO
, "TESTING: Restore TK for " MACSTR
,
2117 MAC2STR(sta
->addr
));
2118 /* This does not really restore the TSC properly, so this will result
2119 * in replay protection issues for now since there is no clean way of
2120 * preventing encryption of a single EAPOL frame. */
2121 hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, sta
->last_tk_alg
,
2122 sta
->addr
, sta
->last_tk_key_idx
, 1, NULL
, 0,
2123 sta
->last_tk
, sta
->last_tk_len
);
2127 static int hostapd_ctrl_resend_m1(struct hostapd_data
*hapd
, const char *cmd
)
2129 struct sta_info
*sta
;
2131 int plain
= os_strstr(cmd
, "plaintext") != NULL
;
2133 if (hwaddr_aton(cmd
, addr
))
2136 sta
= ap_get_sta(hapd
, addr
);
2137 if (!sta
|| !sta
->wpa_sm
)
2140 if (plain
&& sta
->last_tk_alg
== WPA_ALG_NONE
)
2141 plain
= 0; /* no need for special processing */
2143 wpa_printf(MSG_INFO
, "TESTING: Clear TK for " MACSTR
,
2144 MAC2STR(sta
->addr
));
2145 hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, WPA_ALG_NONE
,
2146 sta
->addr
, sta
->last_tk_key_idx
, 0, NULL
, 0,
2150 wpa_printf(MSG_INFO
, "TESTING: Send M1 to " MACSTR
, MAC2STR(sta
->addr
));
2151 return wpa_auth_resend_m1(sta
->wpa_sm
,
2152 os_strstr(cmd
, "change-anonce") != NULL
,
2153 plain
? restore_tk
: NULL
, hapd
, sta
);
2157 static int hostapd_ctrl_resend_m3(struct hostapd_data
*hapd
, const char *cmd
)
2159 struct sta_info
*sta
;
2161 int plain
= os_strstr(cmd
, "plaintext") != NULL
;
2163 if (hwaddr_aton(cmd
, addr
))
2166 sta
= ap_get_sta(hapd
, addr
);
2167 if (!sta
|| !sta
->wpa_sm
)
2170 if (plain
&& sta
->last_tk_alg
== WPA_ALG_NONE
)
2171 plain
= 0; /* no need for special processing */
2173 wpa_printf(MSG_INFO
, "TESTING: Clear TK for " MACSTR
,
2174 MAC2STR(sta
->addr
));
2175 hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, WPA_ALG_NONE
,
2176 sta
->addr
, sta
->last_tk_key_idx
, 0, NULL
, 0,
2180 wpa_printf(MSG_INFO
, "TESTING: Send M3 to " MACSTR
, MAC2STR(sta
->addr
));
2181 return wpa_auth_resend_m3(sta
->wpa_sm
,
2182 plain
? restore_tk
: NULL
, hapd
, sta
);
2186 static int hostapd_ctrl_resend_group_m1(struct hostapd_data
*hapd
,
2189 struct sta_info
*sta
;
2191 int plain
= os_strstr(cmd
, "plaintext") != NULL
;
2193 if (hwaddr_aton(cmd
, addr
))
2196 sta
= ap_get_sta(hapd
, addr
);
2197 if (!sta
|| !sta
->wpa_sm
)
2200 if (plain
&& sta
->last_tk_alg
== WPA_ALG_NONE
)
2201 plain
= 0; /* no need for special processing */
2203 wpa_printf(MSG_INFO
, "TESTING: Clear TK for " MACSTR
,
2204 MAC2STR(sta
->addr
));
2205 hostapd_drv_set_key(hapd
->conf
->iface
, hapd
, WPA_ALG_NONE
,
2206 sta
->addr
, sta
->last_tk_key_idx
, 0, NULL
, 0,
2210 wpa_printf(MSG_INFO
,
2211 "TESTING: Send group M1 for the same GTK and zero RSC to "
2212 MACSTR
, MAC2STR(sta
->addr
));
2213 return wpa_auth_resend_group_m1(sta
->wpa_sm
,
2214 plain
? restore_tk
: NULL
, hapd
, sta
);
2217 #endif /* CONFIG_TESTING_OPTIONS */
2220 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface
*iface
,
2224 struct csa_settings settings
;
2228 ret
= hostapd_parse_csa_settings(pos
, &settings
);
2232 for (i
= 0; i
< iface
->num_bss
; i
++) {
2234 /* Save CHAN_SWITCH VHT config */
2235 hostapd_chan_switch_vht_config(
2236 iface
->bss
[i
], settings
.freq_params
.vht_enabled
);
2238 ret
= hostapd_switch_channel(iface
->bss
[i
], &settings
);
2240 /* FIX: What do we do if CSA fails in the middle of
2241 * submitting multi-BSS CSA requests? */
2247 #else /* NEED_AP_MLME */
2249 #endif /* NEED_AP_MLME */
2253 static int hostapd_ctrl_iface_mib(struct hostapd_data
*hapd
, char *reply
,
2254 int reply_size
, const char *param
)
2256 #ifdef RADIUS_SERVER
2257 if (os_strcmp(param
, "radius_server") == 0) {
2258 return radius_server_get_mib(hapd
->radius_srv
, reply
,
2261 #endif /* RADIUS_SERVER */
2266 static int hostapd_ctrl_iface_vendor(struct hostapd_data
*hapd
, char *cmd
,
2267 char *buf
, size_t buflen
)
2272 unsigned int vendor_id
, subcmd
;
2273 struct wpabuf
*reply
;
2274 size_t data_len
= 0;
2276 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
2277 vendor_id
= strtoul(cmd
, &pos
, 16);
2278 if (!isblank((unsigned char) *pos
))
2281 subcmd
= strtoul(pos
, &pos
, 10);
2284 if (!isblank((unsigned char) *pos
++))
2286 data_len
= os_strlen(pos
);
2291 data
= os_malloc(data_len
);
2295 if (hexstr2bin(pos
, data
, data_len
)) {
2296 wpa_printf(MSG_DEBUG
,
2297 "Vendor command: wrong parameter format");
2303 reply
= wpabuf_alloc((buflen
- 1) / 2);
2309 ret
= hostapd_drv_vendor_cmd(hapd
, vendor_id
, subcmd
, data
, data_len
,
2313 ret
= wpa_snprintf_hex(buf
, buflen
, wpabuf_head_u8(reply
),
2323 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data
*hapd
,
2327 struct sta_info
*sta
;
2329 if (hwaddr_aton(cmd
, addr
))
2332 sta
= ap_get_sta(hapd
, addr
);
2333 if (!sta
|| !sta
->eapol_sm
)
2336 eapol_auth_reauthenticate(sta
->eapol_sm
);
2341 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data
*hapd
, char *cmd
)
2344 struct sta_info
*sta
;
2345 char *pos
= cmd
, *param
;
2347 if (hwaddr_aton(pos
, addr
) || pos
[17] != ' ')
2351 pos
= os_strchr(pos
, ' ');
2356 sta
= ap_get_sta(hapd
, addr
);
2357 if (!sta
|| !sta
->eapol_sm
)
2360 return eapol_auth_set_conf(sta
->eapol_sm
, param
, pos
);
2364 static int hostapd_ctrl_iface_log_level(struct hostapd_data
*hapd
, char *cmd
,
2365 char *buf
, size_t buflen
)
2367 char *pos
, *end
, *stamp
;
2370 /* cmd: "LOG_LEVEL [<level>]" */
2374 ret
= os_snprintf(pos
, end
- pos
, "Current level: %s\n"
2376 debug_level_str(wpa_debug_level
),
2377 wpa_debug_timestamp
);
2378 if (os_snprintf_error(end
- pos
, ret
))
2387 stamp
= os_strchr(cmd
, ' ');
2390 while (*stamp
== ' ') {
2395 if (os_strlen(cmd
)) {
2396 int level
= str_to_debug_level(cmd
);
2399 wpa_debug_level
= level
;
2402 if (stamp
&& os_strlen(stamp
))
2403 wpa_debug_timestamp
= atoi(stamp
);
2405 os_memcpy(buf
, "OK\n", 3);
2411 static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data
*hapd
,
2412 char *buf
, size_t buflen
)
2414 struct hostapd_iface
*iface
= hapd
->iface
;
2416 struct hostapd_sta_info
*info
;
2417 struct os_reltime now
;
2419 if (!iface
->num_sta_seen
)
2422 sta_track_expire(iface
, 0);
2427 os_get_reltime(&now
);
2428 dl_list_for_each_reverse(info
, &iface
->sta_seen
,
2429 struct hostapd_sta_info
, list
) {
2430 struct os_reltime age
;
2433 os_reltime_sub(&now
, &info
->last_seen
, &age
);
2434 ret
= os_snprintf(pos
, end
- pos
, MACSTR
" %u %d\n",
2435 MAC2STR(info
->addr
), (unsigned int) age
.sec
,
2437 if (os_snprintf_error(end
- pos
, ret
))
2444 #endif /* NEED_AP_MLME */
2447 static int hostapd_ctrl_iface_req_lci(struct hostapd_data
*hapd
,
2452 if (hwaddr_aton(cmd
, addr
)) {
2453 wpa_printf(MSG_INFO
, "CTRL: REQ_LCI: Invalid MAC address");
2457 return hostapd_send_lci_req(hapd
, addr
);
2461 static int hostapd_ctrl_iface_req_range(struct hostapd_data
*hapd
, char *cmd
)
2464 char *token
, *context
= NULL
;
2465 int random_interval
, min_ap
;
2466 u8 responders
[ETH_ALEN
* RRM_RANGE_REQ_MAX_RESPONDERS
];
2467 unsigned int n_responders
;
2469 token
= str_token(cmd
, " ", &context
);
2470 if (!token
|| hwaddr_aton(token
, addr
)) {
2471 wpa_printf(MSG_INFO
,
2472 "CTRL: REQ_RANGE - Bad destination address");
2476 token
= str_token(cmd
, " ", &context
);
2480 random_interval
= atoi(token
);
2481 if (random_interval
< 0 || random_interval
> 0xffff)
2484 token
= str_token(cmd
, " ", &context
);
2488 min_ap
= atoi(token
);
2489 if (min_ap
<= 0 || min_ap
> WLAN_RRM_RANGE_REQ_MAX_MIN_AP
)
2493 while ((token
= str_token(cmd
, " ", &context
))) {
2494 if (n_responders
== RRM_RANGE_REQ_MAX_RESPONDERS
) {
2495 wpa_printf(MSG_INFO
,
2496 "CTRL: REQ_RANGE: Too many responders");
2500 if (hwaddr_aton(token
, responders
+ n_responders
* ETH_ALEN
)) {
2501 wpa_printf(MSG_INFO
,
2502 "CTRL: REQ_RANGE: Bad responder address");
2509 if (!n_responders
) {
2510 wpa_printf(MSG_INFO
,
2511 "CTRL: REQ_RANGE - No FTM responder address");
2515 return hostapd_send_range_req(hapd
, addr
, random_interval
, min_ap
,
2516 responders
, n_responders
);
2520 static int hostapd_ctrl_iface_req_beacon(struct hostapd_data
*hapd
,
2521 const char *cmd
, char *reply
,
2530 if (hwaddr_aton(cmd
, addr
))
2532 pos
= os_strchr(cmd
, ' ');
2536 if (os_strncmp(pos
, "req_mode=", 9) == 0) {
2537 int val
= hex2byte(pos
+ 9);
2543 pos
= os_strchr(pos
, ' ');
2548 req
= wpabuf_parse_bin(pos
);
2552 ret
= hostapd_send_beacon_req(hapd
, addr
, req_mode
, req
);
2555 ret
= os_snprintf(reply
, reply_size
, "%d", ret
);
2560 static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data
*hapd
, char *buf
)
2562 struct wpa_ssid_value ssid
;
2564 struct wpabuf
*nr
, *lci
= NULL
, *civic
= NULL
;
2569 if (!(hapd
->conf
->radio_measurements
[0] &
2570 WLAN_RRM_CAPS_NEIGHBOR_REPORT
)) {
2571 wpa_printf(MSG_ERROR
,
2572 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2576 if (hwaddr_aton(buf
, bssid
)) {
2577 wpa_printf(MSG_ERROR
, "CTRL: SET_NEIGHBOR: Bad BSSID");
2581 tmp
= os_strstr(buf
, "ssid=");
2582 if (!tmp
|| ssid_parse(tmp
+ 5, &ssid
)) {
2583 wpa_printf(MSG_ERROR
,
2584 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2587 buf
= os_strchr(tmp
+ 6, tmp
[5] == '"' ? '"' : ' ');
2591 tmp
= os_strstr(buf
, "nr=");
2593 wpa_printf(MSG_ERROR
,
2594 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2598 buf
= os_strchr(tmp
, ' ');
2602 nr
= wpabuf_parse_bin(tmp
+ 3);
2604 wpa_printf(MSG_ERROR
,
2605 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2612 tmp
= os_strstr(buf
, "lci=");
2614 buf
= os_strchr(tmp
, ' ');
2617 lci
= wpabuf_parse_bin(tmp
+ 4);
2619 wpa_printf(MSG_ERROR
,
2620 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
2629 tmp
= os_strstr(buf
, "civic=");
2631 buf
= os_strchr(tmp
, ' ');
2634 civic
= wpabuf_parse_bin(tmp
+ 6);
2636 wpa_printf(MSG_ERROR
,
2637 "CTRL: SET_NEIGHBOR: Bad civic subelement");
2647 if (os_strstr(buf
, "stat"))
2651 ret
= hostapd_neighbor_set(hapd
, bssid
, &ssid
, nr
, lci
, civic
,
2662 static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data
*hapd
,
2665 struct wpa_ssid_value ssid
;
2669 if (hwaddr_aton(buf
, bssid
)) {
2670 wpa_printf(MSG_ERROR
, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
2674 tmp
= os_strstr(buf
, "ssid=");
2675 if (!tmp
|| ssid_parse(tmp
+ 5, &ssid
)) {
2676 wpa_printf(MSG_ERROR
,
2677 "CTRL: REMOVE_NEIGHBORr: Bad or missing SSID");
2681 return hostapd_neighbor_remove(hapd
, bssid
, &ssid
);
2685 static int hostapd_ctrl_driver_flags(struct hostapd_iface
*iface
, char *buf
,
2691 ret
= os_snprintf(buf
, buflen
, "%016llX:\n",
2692 (long long unsigned) iface
->drv_flags
);
2693 if (os_snprintf_error(buflen
, ret
))
2699 for (i
= 0; i
< 64; i
++) {
2700 if (iface
->drv_flags
& (1LLU << i
)) {
2701 ret
= os_snprintf(pos
, end
- pos
, "%s\n",
2702 driver_flag_to_string(1LLU << i
));
2703 if (os_snprintf_error(end
- pos
, ret
))
2713 static int hostapd_ctrl_iface_acl_del_mac(struct mac_acl_entry
**acl
, int *num
,
2714 const char *txtaddr
)
2717 struct vlan_description vlan_id
;
2722 if (hwaddr_aton(txtaddr
, addr
))
2725 if (hostapd_maclist_found(*acl
, *num
, addr
, &vlan_id
))
2726 hostapd_remove_acl_mac(acl
, num
, addr
);
2732 static void hostapd_ctrl_iface_acl_clear_list(struct mac_acl_entry
**acl
,
2736 hostapd_remove_acl_mac(acl
, num
, (*acl
)[0].addr
);
2740 static int hostapd_ctrl_iface_acl_show_mac(struct mac_acl_entry
*acl
, int num
,
2741 char *buf
, size_t buflen
)
2743 int i
= 0, len
= 0, ret
= 0;
2749 ret
= os_snprintf(buf
+ len
, buflen
- len
,
2750 MACSTR
" VLAN_ID=%d\n",
2751 MAC2STR(acl
[i
].addr
),
2752 acl
[i
].vlan_id
.untagged
);
2753 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2762 static int hostapd_ctrl_iface_acl_add_mac(struct mac_acl_entry
**acl
, int *num
,
2766 struct vlan_description vlan_id
;
2767 int ret
= 0, vlanid
= 0;
2770 if (hwaddr_aton(cmd
, addr
))
2773 pos
= os_strstr(cmd
, "VLAN_ID=");
2775 vlanid
= atoi(pos
+ 8);
2777 if (!hostapd_maclist_found(*acl
, *num
, addr
, &vlan_id
)) {
2778 ret
= hostapd_add_acl_maclist(acl
, num
, vlanid
, addr
);
2779 if (ret
!= -1 && *acl
)
2780 qsort(*acl
, *num
, sizeof(**acl
), hostapd_acl_comp
);
2783 return ret
< 0 ? -1 : 0;
2787 static int hostapd_ctrl_iface_receive_process(struct hostapd_data
*hapd
,
2788 char *buf
, char *reply
,
2790 struct sockaddr_storage
*from
,
2795 os_memcpy(reply
, "OK\n", 3);
2798 if (os_strcmp(buf
, "PING") == 0) {
2799 os_memcpy(reply
, "PONG\n", 5);
2801 } else if (os_strncmp(buf
, "RELOG", 5) == 0) {
2802 if (wpa_debug_reopen_file() < 0)
2804 } else if (os_strncmp(buf
, "NOTE ", 5) == 0) {
2805 wpa_printf(MSG_INFO
, "NOTE: %s", buf
+ 5);
2806 } else if (os_strcmp(buf
, "STATUS") == 0) {
2807 reply_len
= hostapd_ctrl_iface_status(hapd
, reply
,
2809 } else if (os_strcmp(buf
, "STATUS-DRIVER") == 0) {
2810 reply_len
= hostapd_drv_status(hapd
, reply
, reply_size
);
2811 } else if (os_strcmp(buf
, "MIB") == 0) {
2812 reply_len
= ieee802_11_get_mib(hapd
, reply
, reply_size
);
2813 if (reply_len
>= 0) {
2814 res
= wpa_get_mib(hapd
->wpa_auth
, reply
+ reply_len
,
2815 reply_size
- reply_len
);
2821 if (reply_len
>= 0) {
2822 res
= ieee802_1x_get_mib(hapd
, reply
+ reply_len
,
2823 reply_size
- reply_len
);
2829 #ifndef CONFIG_NO_RADIUS
2830 if (reply_len
>= 0) {
2831 res
= radius_client_get_mib(hapd
->radius
,
2833 reply_size
- reply_len
);
2839 #endif /* CONFIG_NO_RADIUS */
2840 } else if (os_strncmp(buf
, "MIB ", 4) == 0) {
2841 reply_len
= hostapd_ctrl_iface_mib(hapd
, reply
, reply_size
,
2843 } else if (os_strcmp(buf
, "STA-FIRST") == 0) {
2844 reply_len
= hostapd_ctrl_iface_sta_first(hapd
, reply
,
2846 } else if (os_strncmp(buf
, "STA ", 4) == 0) {
2847 reply_len
= hostapd_ctrl_iface_sta(hapd
, buf
+ 4, reply
,
2849 } else if (os_strncmp(buf
, "STA-NEXT ", 9) == 0) {
2850 reply_len
= hostapd_ctrl_iface_sta_next(hapd
, buf
+ 9, reply
,
2852 } else if (os_strcmp(buf
, "ATTACH") == 0) {
2853 if (hostapd_ctrl_iface_attach(hapd
, from
, fromlen
, NULL
))
2855 } else if (os_strncmp(buf
, "ATTACH ", 7) == 0) {
2856 if (hostapd_ctrl_iface_attach(hapd
, from
, fromlen
, buf
+ 7))
2858 } else if (os_strcmp(buf
, "DETACH") == 0) {
2859 if (hostapd_ctrl_iface_detach(hapd
, from
, fromlen
))
2861 } else if (os_strncmp(buf
, "LEVEL ", 6) == 0) {
2862 if (hostapd_ctrl_iface_level(hapd
, from
, fromlen
,
2865 } else if (os_strncmp(buf
, "NEW_STA ", 8) == 0) {
2866 if (hostapd_ctrl_iface_new_sta(hapd
, buf
+ 8))
2868 } else if (os_strncmp(buf
, "DEAUTHENTICATE ", 15) == 0) {
2869 if (hostapd_ctrl_iface_deauthenticate(hapd
, buf
+ 15))
2871 } else if (os_strncmp(buf
, "DISASSOCIATE ", 13) == 0) {
2872 if (hostapd_ctrl_iface_disassociate(hapd
, buf
+ 13))
2874 #ifdef CONFIG_TAXONOMY
2875 } else if (os_strncmp(buf
, "SIGNATURE ", 10) == 0) {
2876 reply_len
= hostapd_ctrl_iface_signature(hapd
, buf
+ 10,
2878 #endif /* CONFIG_TAXONOMY */
2879 } else if (os_strncmp(buf
, "POLL_STA ", 9) == 0) {
2880 if (hostapd_ctrl_iface_poll_sta(hapd
, buf
+ 9))
2882 } else if (os_strcmp(buf
, "STOP_AP") == 0) {
2883 if (hostapd_ctrl_iface_stop_ap(hapd
))
2885 #ifdef CONFIG_IEEE80211W
2887 } else if (os_strncmp(buf
, "SA_QUERY ", 9) == 0) {
2888 if (hostapd_ctrl_iface_sa_query(hapd
, buf
+ 9))
2890 #endif /* NEED_AP_MLME */
2891 #endif /* CONFIG_IEEE80211W */
2893 } else if (os_strncmp(buf
, "WPS_PIN ", 8) == 0) {
2894 if (hostapd_ctrl_iface_wps_pin(hapd
, buf
+ 8))
2896 } else if (os_strncmp(buf
, "WPS_CHECK_PIN ", 14) == 0) {
2897 reply_len
= hostapd_ctrl_iface_wps_check_pin(
2898 hapd
, buf
+ 14, reply
, reply_size
);
2899 } else if (os_strcmp(buf
, "WPS_PBC") == 0) {
2900 if (hostapd_wps_button_pushed(hapd
, NULL
))
2902 } else if (os_strcmp(buf
, "WPS_CANCEL") == 0) {
2903 if (hostapd_wps_cancel(hapd
))
2905 } else if (os_strncmp(buf
, "WPS_AP_PIN ", 11) == 0) {
2906 reply_len
= hostapd_ctrl_iface_wps_ap_pin(hapd
, buf
+ 11,
2908 } else if (os_strncmp(buf
, "WPS_CONFIG ", 11) == 0) {
2909 if (hostapd_ctrl_iface_wps_config(hapd
, buf
+ 11) < 0)
2911 } else if (os_strncmp(buf
, "WPS_GET_STATUS", 13) == 0) {
2912 reply_len
= hostapd_ctrl_iface_wps_get_status(hapd
, reply
,
2914 #ifdef CONFIG_WPS_NFC
2915 } else if (os_strncmp(buf
, "WPS_NFC_TAG_READ ", 17) == 0) {
2916 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd
, buf
+ 17))
2918 } else if (os_strncmp(buf
, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2919 reply_len
= hostapd_ctrl_iface_wps_nfc_config_token(
2920 hapd
, buf
+ 21, reply
, reply_size
);
2921 } else if (os_strncmp(buf
, "WPS_NFC_TOKEN ", 14) == 0) {
2922 reply_len
= hostapd_ctrl_iface_wps_nfc_token(
2923 hapd
, buf
+ 14, reply
, reply_size
);
2924 } else if (os_strncmp(buf
, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2925 reply_len
= hostapd_ctrl_iface_nfc_get_handover_sel(
2926 hapd
, buf
+ 21, reply
, reply_size
);
2927 } else if (os_strncmp(buf
, "NFC_REPORT_HANDOVER ", 20) == 0) {
2928 if (hostapd_ctrl_iface_nfc_report_handover(hapd
, buf
+ 20))
2930 #endif /* CONFIG_WPS_NFC */
2931 #endif /* CONFIG_WPS */
2932 #ifdef CONFIG_INTERWORKING
2933 } else if (os_strncmp(buf
, "SET_QOS_MAP_SET ", 16) == 0) {
2934 if (hostapd_ctrl_iface_set_qos_map_set(hapd
, buf
+ 16))
2936 } else if (os_strncmp(buf
, "SEND_QOS_MAP_CONF ", 18) == 0) {
2937 if (hostapd_ctrl_iface_send_qos_map_conf(hapd
, buf
+ 18))
2939 #endif /* CONFIG_INTERWORKING */
2941 } else if (os_strncmp(buf
, "HS20_WNM_NOTIF ", 15) == 0) {
2942 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd
, buf
+ 15))
2944 } else if (os_strncmp(buf
, "HS20_DEAUTH_REQ ", 16) == 0) {
2945 if (hostapd_ctrl_iface_hs20_deauth_req(hapd
, buf
+ 16))
2947 #endif /* CONFIG_HS20 */
2948 #ifdef CONFIG_WNM_AP
2949 } else if (os_strncmp(buf
, "DISASSOC_IMMINENT ", 18) == 0) {
2950 if (hostapd_ctrl_iface_disassoc_imminent(hapd
, buf
+ 18))
2952 } else if (os_strncmp(buf
, "ESS_DISASSOC ", 13) == 0) {
2953 if (hostapd_ctrl_iface_ess_disassoc(hapd
, buf
+ 13))
2955 } else if (os_strncmp(buf
, "BSS_TM_REQ ", 11) == 0) {
2956 if (hostapd_ctrl_iface_bss_tm_req(hapd
, buf
+ 11))
2958 #endif /* CONFIG_WNM_AP */
2959 } else if (os_strcmp(buf
, "GET_CONFIG") == 0) {
2960 reply_len
= hostapd_ctrl_iface_get_config(hapd
, reply
,
2962 } else if (os_strncmp(buf
, "SET ", 4) == 0) {
2963 if (hostapd_ctrl_iface_set(hapd
, buf
+ 4))
2965 } else if (os_strncmp(buf
, "GET ", 4) == 0) {
2966 reply_len
= hostapd_ctrl_iface_get(hapd
, buf
+ 4, reply
,
2968 } else if (os_strncmp(buf
, "ENABLE", 6) == 0) {
2969 if (hostapd_ctrl_iface_enable(hapd
->iface
))
2971 } else if (os_strncmp(buf
, "RELOAD", 6) == 0) {
2972 if (hostapd_ctrl_iface_reload(hapd
->iface
))
2974 } else if (os_strncmp(buf
, "DISABLE", 7) == 0) {
2975 if (hostapd_ctrl_iface_disable(hapd
->iface
))
2977 } else if (os_strcmp(buf
, "UPDATE_BEACON") == 0) {
2978 if (ieee802_11_set_beacon(hapd
))
2980 #ifdef CONFIG_TESTING_OPTIONS
2981 } else if (os_strncmp(buf
, "RADAR ", 6) == 0) {
2982 if (hostapd_ctrl_iface_radar(hapd
, buf
+ 6))
2984 } else if (os_strncmp(buf
, "MGMT_TX ", 8) == 0) {
2985 if (hostapd_ctrl_iface_mgmt_tx(hapd
, buf
+ 8))
2987 } else if (os_strncmp(buf
, "MGMT_TX_STATUS_PROCESS ", 23) == 0) {
2988 if (hostapd_ctrl_iface_mgmt_tx_status_process(hapd
,
2991 } else if (os_strncmp(buf
, "MGMT_RX_PROCESS ", 16) == 0) {
2992 if (hostapd_ctrl_iface_mgmt_rx_process(hapd
, buf
+ 16) < 0)
2994 } else if (os_strncmp(buf
, "EAPOL_RX ", 9) == 0) {
2995 if (hostapd_ctrl_iface_eapol_rx(hapd
, buf
+ 9) < 0)
2997 } else if (os_strncmp(buf
, "DATA_TEST_CONFIG ", 17) == 0) {
2998 if (hostapd_ctrl_iface_data_test_config(hapd
, buf
+ 17) < 0)
3000 } else if (os_strncmp(buf
, "DATA_TEST_TX ", 13) == 0) {
3001 if (hostapd_ctrl_iface_data_test_tx(hapd
, buf
+ 13) < 0)
3003 } else if (os_strncmp(buf
, "DATA_TEST_FRAME ", 16) == 0) {
3004 if (hostapd_ctrl_iface_data_test_frame(hapd
, buf
+ 16) < 0)
3006 } else if (os_strncmp(buf
, "TEST_ALLOC_FAIL ", 16) == 0) {
3007 if (hostapd_ctrl_test_alloc_fail(hapd
, buf
+ 16) < 0)
3009 } else if (os_strcmp(buf
, "GET_ALLOC_FAIL") == 0) {
3010 reply_len
= hostapd_ctrl_get_alloc_fail(hapd
, reply
,
3012 } else if (os_strncmp(buf
, "TEST_FAIL ", 10) == 0) {
3013 if (hostapd_ctrl_test_fail(hapd
, buf
+ 10) < 0)
3015 } else if (os_strcmp(buf
, "GET_FAIL") == 0) {
3016 reply_len
= hostapd_ctrl_get_fail(hapd
, reply
, reply_size
);
3017 } else if (os_strncmp(buf
, "RESET_PN ", 9) == 0) {
3018 if (hostapd_ctrl_reset_pn(hapd
, buf
+ 9) < 0)
3020 } else if (os_strncmp(buf
, "SET_KEY ", 8) == 0) {
3021 if (hostapd_ctrl_set_key(hapd
, buf
+ 8) < 0)
3023 } else if (os_strncmp(buf
, "RESEND_M1 ", 10) == 0) {
3024 if (hostapd_ctrl_resend_m1(hapd
, buf
+ 10) < 0)
3026 } else if (os_strncmp(buf
, "RESEND_M3 ", 10) == 0) {
3027 if (hostapd_ctrl_resend_m3(hapd
, buf
+ 10) < 0)
3029 } else if (os_strncmp(buf
, "RESEND_GROUP_M1 ", 16) == 0) {
3030 if (hostapd_ctrl_resend_group_m1(hapd
, buf
+ 16) < 0)
3032 } else if (os_strcmp(buf
, "REKEY_GTK") == 0) {
3033 if (wpa_auth_rekey_gtk(hapd
->wpa_auth
) < 0)
3035 #endif /* CONFIG_TESTING_OPTIONS */
3036 } else if (os_strncmp(buf
, "CHAN_SWITCH ", 12) == 0) {
3037 if (hostapd_ctrl_iface_chan_switch(hapd
->iface
, buf
+ 12))
3039 } else if (os_strncmp(buf
, "VENDOR ", 7) == 0) {
3040 reply_len
= hostapd_ctrl_iface_vendor(hapd
, buf
+ 7, reply
,
3042 } else if (os_strcmp(buf
, "ERP_FLUSH") == 0) {
3043 ieee802_1x_erp_flush(hapd
);
3044 #ifdef RADIUS_SERVER
3045 radius_server_erp_flush(hapd
->radius_srv
);
3046 #endif /* RADIUS_SERVER */
3047 } else if (os_strncmp(buf
, "EAPOL_REAUTH ", 13) == 0) {
3048 if (hostapd_ctrl_iface_eapol_reauth(hapd
, buf
+ 13))
3050 } else if (os_strncmp(buf
, "EAPOL_SET ", 10) == 0) {
3051 if (hostapd_ctrl_iface_eapol_set(hapd
, buf
+ 10))
3053 } else if (os_strncmp(buf
, "LOG_LEVEL", 9) == 0) {
3054 reply_len
= hostapd_ctrl_iface_log_level(
3055 hapd
, buf
+ 9, reply
, reply_size
);
3057 } else if (os_strcmp(buf
, "TRACK_STA_LIST") == 0) {
3058 reply_len
= hostapd_ctrl_iface_track_sta_list(
3059 hapd
, reply
, reply_size
);
3060 #endif /* NEED_AP_MLME */
3061 } else if (os_strcmp(buf
, "PMKSA") == 0) {
3062 reply_len
= hostapd_ctrl_iface_pmksa_list(hapd
, reply
,
3064 } else if (os_strcmp(buf
, "PMKSA_FLUSH") == 0) {
3065 hostapd_ctrl_iface_pmksa_flush(hapd
);
3066 } else if (os_strncmp(buf
, "PMKSA_ADD ", 10) == 0) {
3067 if (hostapd_ctrl_iface_pmksa_add(hapd
, buf
+ 10) < 0)
3069 } else if (os_strncmp(buf
, "SET_NEIGHBOR ", 13) == 0) {
3070 if (hostapd_ctrl_iface_set_neighbor(hapd
, buf
+ 13))
3072 } else if (os_strncmp(buf
, "REMOVE_NEIGHBOR ", 16) == 0) {
3073 if (hostapd_ctrl_iface_remove_neighbor(hapd
, buf
+ 16))
3075 } else if (os_strncmp(buf
, "REQ_LCI ", 8) == 0) {
3076 if (hostapd_ctrl_iface_req_lci(hapd
, buf
+ 8))
3078 } else if (os_strncmp(buf
, "REQ_RANGE ", 10) == 0) {
3079 if (hostapd_ctrl_iface_req_range(hapd
, buf
+ 10))
3081 } else if (os_strncmp(buf
, "REQ_BEACON ", 11) == 0) {
3082 reply_len
= hostapd_ctrl_iface_req_beacon(hapd
, buf
+ 11,
3084 } else if (os_strcmp(buf
, "DRIVER_FLAGS") == 0) {
3085 reply_len
= hostapd_ctrl_driver_flags(hapd
->iface
, reply
,
3087 } else if (os_strcmp(buf
, "TERMINATE") == 0) {
3089 } else if (os_strncmp(buf
, "ACCEPT_ACL ", 11) == 0) {
3090 if (os_strncmp(buf
+ 11, "ADD_MAC ", 8) == 0) {
3091 if (!hostapd_ctrl_iface_acl_add_mac(
3092 &hapd
->conf
->accept_mac
,
3093 &hapd
->conf
->num_accept_mac
, buf
+ 19))
3094 hostapd_disassoc_accept_mac(hapd
);
3097 } else if (os_strncmp((buf
+ 11), "DEL_MAC ", 8) == 0) {
3098 hostapd_ctrl_iface_acl_del_mac(
3099 &hapd
->conf
->accept_mac
,
3100 &hapd
->conf
->num_accept_mac
, buf
+ 19);
3101 } else if (os_strcmp(buf
+ 11, "SHOW") == 0) {
3102 reply_len
= hostapd_ctrl_iface_acl_show_mac(
3103 hapd
->conf
->accept_mac
,
3104 hapd
->conf
->num_accept_mac
, reply
, reply_size
);
3105 } else if (os_strcmp(buf
+ 11, "CLEAR") == 0) {
3106 hostapd_ctrl_iface_acl_clear_list(
3107 &hapd
->conf
->accept_mac
,
3108 &hapd
->conf
->num_accept_mac
);
3110 } else if (os_strncmp(buf
, "DENY_ACL ", 9) == 0) {
3111 if (os_strncmp(buf
+ 9, "ADD_MAC ", 8) == 0) {
3112 if (!hostapd_ctrl_iface_acl_add_mac(
3113 &hapd
->conf
->deny_mac
,
3114 &hapd
->conf
->num_deny_mac
, buf
+ 17))
3115 hostapd_disassoc_deny_mac(hapd
);
3116 } else if (os_strncmp(buf
+ 9, "DEL_MAC ", 8) == 0) {
3117 hostapd_ctrl_iface_acl_del_mac(
3118 &hapd
->conf
->deny_mac
,
3119 &hapd
->conf
->num_deny_mac
, buf
+ 17);
3120 } else if (os_strcmp(buf
+ 9, "SHOW") == 0) {
3121 reply_len
= hostapd_ctrl_iface_acl_show_mac(
3122 hapd
->conf
->deny_mac
,
3123 hapd
->conf
->num_deny_mac
, reply
, reply_size
);
3124 } else if (os_strcmp(buf
+ 9, "CLEAR") == 0) {
3125 hostapd_ctrl_iface_acl_clear_list(
3126 &hapd
->conf
->deny_mac
,
3127 &hapd
->conf
->num_deny_mac
);
3130 } else if (os_strncmp(buf
, "DPP_QR_CODE ", 12) == 0) {
3131 res
= hostapd_dpp_qr_code(hapd
, buf
+ 12);
3135 reply_len
= os_snprintf(reply
, reply_size
, "%d", res
);
3136 if (os_snprintf_error(reply_size
, reply_len
))
3139 } else if (os_strncmp(buf
, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
3140 res
= hostapd_dpp_bootstrap_gen(hapd
, buf
+ 18);
3144 reply_len
= os_snprintf(reply
, reply_size
, "%d", res
);
3145 if (os_snprintf_error(reply_size
, reply_len
))
3148 } else if (os_strncmp(buf
, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
3149 if (hostapd_dpp_bootstrap_remove(hapd
, buf
+ 21) < 0)
3151 } else if (os_strncmp(buf
, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
3154 uri
= hostapd_dpp_bootstrap_get_uri(hapd
, atoi(buf
+ 22));
3158 reply_len
= os_snprintf(reply
, reply_size
, "%s", uri
);
3159 if (os_snprintf_error(reply_size
, reply_len
))
3162 } else if (os_strncmp(buf
, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
3163 reply_len
= hostapd_dpp_bootstrap_info(hapd
, atoi(buf
+ 19),
3165 } else if (os_strncmp(buf
, "DPP_AUTH_INIT ", 14) == 0) {
3166 if (hostapd_dpp_auth_init(hapd
, buf
+ 13) < 0)
3168 } else if (os_strncmp(buf
, "DPP_LISTEN ", 11) == 0) {
3169 if (hostapd_dpp_listen(hapd
, buf
+ 11) < 0)
3171 } else if (os_strcmp(buf
, "DPP_STOP_LISTEN") == 0) {
3172 hostapd_dpp_stop(hapd
);
3173 hostapd_dpp_listen_stop(hapd
);
3174 } else if (os_strncmp(buf
, "DPP_CONFIGURATOR_ADD", 20) == 0) {
3175 res
= hostapd_dpp_configurator_add(hapd
, buf
+ 20);
3179 reply_len
= os_snprintf(reply
, reply_size
, "%d", res
);
3180 if (os_snprintf_error(reply_size
, reply_len
))
3183 } else if (os_strncmp(buf
, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
3184 if (hostapd_dpp_configurator_remove(hapd
, buf
+ 24) < 0)
3186 } else if (os_strncmp(buf
, "DPP_CONFIGURATOR_SIGN ", 22) == 0) {
3187 if (hostapd_dpp_configurator_sign(hapd
, buf
+ 22) < 0)
3189 } else if (os_strncmp(buf
, "DPP_CONFIGURATOR_GET_KEY ", 25) == 0) {
3190 reply_len
= hostapd_dpp_configurator_get_key(hapd
,
3193 } else if (os_strncmp(buf
, "DPP_PKEX_ADD ", 13) == 0) {
3194 res
= hostapd_dpp_pkex_add(hapd
, buf
+ 12);
3198 reply_len
= os_snprintf(reply
, reply_size
, "%d", res
);
3199 if (os_snprintf_error(reply_size
, reply_len
))
3202 } else if (os_strncmp(buf
, "DPP_PKEX_REMOVE ", 16) == 0) {
3203 if (hostapd_dpp_pkex_remove(hapd
, buf
+ 16) < 0)
3205 #endif /* CONFIG_DPP */
3207 os_memcpy(reply
, "UNKNOWN COMMAND\n", 16);
3211 if (reply_len
< 0) {
3212 os_memcpy(reply
, "FAIL\n", 5);
3220 static void hostapd_ctrl_iface_receive(int sock
, void *eloop_ctx
,
3223 struct hostapd_data
*hapd
= eloop_ctx
;
3226 struct sockaddr_storage from
;
3227 socklen_t fromlen
= sizeof(from
);
3228 char *reply
, *pos
= buf
;
3229 const int reply_size
= 4096;
3231 int level
= MSG_DEBUG
;
3232 #ifdef CONFIG_CTRL_IFACE_UDP
3233 unsigned char lcookie
[COOKIE_LEN
];
3234 #endif /* CONFIG_CTRL_IFACE_UDP */
3236 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
3237 (struct sockaddr
*) &from
, &fromlen
);
3239 wpa_printf(MSG_ERROR
, "recvfrom(ctrl_iface): %s",
3245 reply
= os_malloc(reply_size
);
3246 if (reply
== NULL
) {
3247 if (sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
3249 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
3255 #ifdef CONFIG_CTRL_IFACE_UDP
3256 if (os_strcmp(buf
, "GET_COOKIE") == 0) {
3257 os_memcpy(reply
, "COOKIE=", 7);
3258 wpa_snprintf_hex(reply
+ 7, 2 * COOKIE_LEN
+ 1,
3259 cookie
, COOKIE_LEN
);
3260 reply_len
= 7 + 2 * COOKIE_LEN
;
3264 if (os_strncmp(buf
, "COOKIE=", 7) != 0 ||
3265 hexstr2bin(buf
+ 7, lcookie
, COOKIE_LEN
) < 0) {
3266 wpa_printf(MSG_DEBUG
,
3267 "CTRL: No cookie in the request - drop request");
3272 if (os_memcmp(cookie
, lcookie
, COOKIE_LEN
) != 0) {
3273 wpa_printf(MSG_DEBUG
,
3274 "CTRL: Invalid cookie in the request - drop request");
3279 pos
= buf
+ 7 + 2 * COOKIE_LEN
;
3282 #endif /* CONFIG_CTRL_IFACE_UDP */
3284 if (os_strcmp(pos
, "PING") == 0)
3285 level
= MSG_EXCESSIVE
;
3286 wpa_hexdump_ascii(level
, "RX ctrl_iface", pos
, res
);
3288 reply_len
= hostapd_ctrl_iface_receive_process(hapd
, pos
,
3292 #ifdef CONFIG_CTRL_IFACE_UDP
3294 #endif /* CONFIG_CTRL_IFACE_UDP */
3295 if (sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
3297 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
3304 #ifndef CONFIG_CTRL_IFACE_UDP
3305 static char * hostapd_ctrl_iface_path(struct hostapd_data
*hapd
)
3310 if (hapd
->conf
->ctrl_interface
== NULL
)
3313 len
= os_strlen(hapd
->conf
->ctrl_interface
) +
3314 os_strlen(hapd
->conf
->iface
) + 2;
3315 buf
= os_malloc(len
);
3319 os_snprintf(buf
, len
, "%s/%s",
3320 hapd
->conf
->ctrl_interface
, hapd
->conf
->iface
);
3321 buf
[len
- 1] = '\0';
3324 #endif /* CONFIG_CTRL_IFACE_UDP */
3327 static void hostapd_ctrl_iface_msg_cb(void *ctx
, int level
,
3328 enum wpa_msg_type type
,
3329 const char *txt
, size_t len
)
3331 struct hostapd_data
*hapd
= ctx
;
3334 hostapd_ctrl_iface_send(hapd
, level
, type
, txt
, len
);
3338 int hostapd_ctrl_iface_init(struct hostapd_data
*hapd
)
3340 #ifdef CONFIG_CTRL_IFACE_UDP
3341 int port
= HOSTAPD_CTRL_IFACE_PORT
;
3343 char port_str
[40], *tmp
;
3345 struct addrinfo hints
= { 0 }, *res
, *saveres
;
3348 if (hapd
->ctrl_sock
> -1) {
3349 wpa_printf(MSG_DEBUG
, "ctrl_iface already exists!");
3353 if (hapd
->conf
->ctrl_interface
== NULL
)
3356 pos
= os_strstr(hapd
->conf
->ctrl_interface
, "udp:");
3361 wpa_printf(MSG_ERROR
, "Invalid ctrl_iface UDP port");
3366 dl_list_init(&hapd
->ctrl_dst
);
3367 hapd
->ctrl_sock
= -1;
3368 os_get_random(cookie
, COOKIE_LEN
);
3370 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3371 hints
.ai_flags
= AI_PASSIVE
;
3372 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3374 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3375 hints
.ai_family
= AF_INET6
;
3376 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3377 hints
.ai_family
= AF_INET
;
3378 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3379 hints
.ai_socktype
= SOCK_DGRAM
;
3382 os_snprintf(p
, sizeof(p
), "%d", port
);
3383 n
= getaddrinfo(NULL
, p
, &hints
, &res
);
3385 wpa_printf(MSG_ERROR
, "getaddrinfo(): %s", gai_strerror(n
));
3390 hapd
->ctrl_sock
= socket(res
->ai_family
, res
->ai_socktype
,
3392 if (hapd
->ctrl_sock
< 0) {
3393 wpa_printf(MSG_ERROR
, "socket(PF_INET): %s", strerror(errno
));
3397 if (bind(hapd
->ctrl_sock
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
3399 if ((HOSTAPD_CTRL_IFACE_PORT
- port
) <
3400 HOSTAPD_CTRL_IFACE_PORT_LIMIT
&& !pos
)
3402 wpa_printf(MSG_ERROR
, "bind(AF_INET): %s", strerror(errno
));
3406 freeaddrinfo(saveres
);
3408 os_snprintf(port_str
, sizeof(port_str
), "udp:%d", port
);
3409 tmp
= os_strdup(port_str
);
3411 os_free(hapd
->conf
->ctrl_interface
);
3412 hapd
->conf
->ctrl_interface
= tmp
;
3414 wpa_printf(MSG_DEBUG
, "ctrl_iface_init UDP port: %d", port
);
3416 if (eloop_register_read_sock(hapd
->ctrl_sock
,
3417 hostapd_ctrl_iface_receive
, hapd
, NULL
) <
3419 hostapd_ctrl_iface_deinit(hapd
);
3423 hapd
->msg_ctx
= hapd
;
3424 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb
);
3429 if (hapd
->ctrl_sock
>= 0)
3430 close(hapd
->ctrl_sock
);
3432 #else /* CONFIG_CTRL_IFACE_UDP */
3433 struct sockaddr_un addr
;
3437 if (hapd
->ctrl_sock
> -1) {
3438 wpa_printf(MSG_DEBUG
, "ctrl_iface already exists!");
3442 dl_list_init(&hapd
->ctrl_dst
);
3444 if (hapd
->conf
->ctrl_interface
== NULL
)
3447 if (mkdir(hapd
->conf
->ctrl_interface
, S_IRWXU
| S_IRWXG
) < 0) {
3448 if (errno
== EEXIST
) {
3449 wpa_printf(MSG_DEBUG
, "Using existing control "
3450 "interface directory.");
3452 wpa_printf(MSG_ERROR
, "mkdir[ctrl_interface]: %s",
3458 if (hapd
->conf
->ctrl_interface_gid_set
&&
3459 chown(hapd
->conf
->ctrl_interface
, -1,
3460 hapd
->conf
->ctrl_interface_gid
) < 0) {
3461 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
3466 if (!hapd
->conf
->ctrl_interface_gid_set
&&
3467 hapd
->iface
->interfaces
->ctrl_iface_group
&&
3468 chown(hapd
->conf
->ctrl_interface
, -1,
3469 hapd
->iface
->interfaces
->ctrl_iface_group
) < 0) {
3470 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
3477 * Android is using umask 0077 which would leave the control interface
3478 * directory without group access. This breaks things since Wi-Fi
3479 * framework assumes that this directory can be accessed by other
3480 * applications in the wifi group. Fix this by adding group access even
3481 * if umask value would prevent this.
3483 if (chmod(hapd
->conf
->ctrl_interface
, S_IRWXU
| S_IRWXG
) < 0) {
3484 wpa_printf(MSG_ERROR
, "CTRL: Could not chmod directory: %s",
3486 /* Try to continue anyway */
3488 #endif /* ANDROID */
3490 if (os_strlen(hapd
->conf
->ctrl_interface
) + 1 +
3491 os_strlen(hapd
->conf
->iface
) >= sizeof(addr
.sun_path
))
3494 s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
3496 wpa_printf(MSG_ERROR
, "socket(PF_UNIX): %s", strerror(errno
));
3500 os_memset(&addr
, 0, sizeof(addr
));
3502 addr
.sun_len
= sizeof(addr
);
3503 #endif /* __FreeBSD__ */
3504 addr
.sun_family
= AF_UNIX
;
3505 fname
= hostapd_ctrl_iface_path(hapd
);
3508 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
3509 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
3510 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
3512 if (connect(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
3513 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
3514 " allow connections - assuming it was left"
3515 "over from forced program termination");
3516 if (unlink(fname
) < 0) {
3517 wpa_printf(MSG_ERROR
,
3518 "Could not unlink existing ctrl_iface socket '%s': %s",
3519 fname
, strerror(errno
));
3522 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) <
3524 wpa_printf(MSG_ERROR
,
3525 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
3529 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
3530 "ctrl_iface socket '%s'", fname
);
3532 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
3533 "be in use - cannot override it");
3534 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
3535 "not used anymore", fname
);
3542 if (hapd
->conf
->ctrl_interface_gid_set
&&
3543 chown(fname
, -1, hapd
->conf
->ctrl_interface_gid
) < 0) {
3544 wpa_printf(MSG_ERROR
, "chown[ctrl_interface/ifname]: %s",
3549 if (!hapd
->conf
->ctrl_interface_gid_set
&&
3550 hapd
->iface
->interfaces
->ctrl_iface_group
&&
3551 chown(fname
, -1, hapd
->iface
->interfaces
->ctrl_iface_group
) < 0) {
3552 wpa_printf(MSG_ERROR
, "chown[ctrl_interface/ifname]: %s",
3557 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
3558 wpa_printf(MSG_ERROR
, "chmod[ctrl_interface/ifname]: %s",
3564 hapd
->ctrl_sock
= s
;
3565 if (eloop_register_read_sock(s
, hostapd_ctrl_iface_receive
, hapd
,
3567 hostapd_ctrl_iface_deinit(hapd
);
3570 hapd
->msg_ctx
= hapd
;
3571 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb
);
3583 #endif /* CONFIG_CTRL_IFACE_UDP */
3587 void hostapd_ctrl_iface_deinit(struct hostapd_data
*hapd
)
3589 struct wpa_ctrl_dst
*dst
, *prev
;
3591 if (hapd
->ctrl_sock
> -1) {
3592 #ifndef CONFIG_CTRL_IFACE_UDP
3594 #endif /* !CONFIG_CTRL_IFACE_UDP */
3596 eloop_unregister_read_sock(hapd
->ctrl_sock
);
3597 close(hapd
->ctrl_sock
);
3598 hapd
->ctrl_sock
= -1;
3599 #ifndef CONFIG_CTRL_IFACE_UDP
3600 fname
= hostapd_ctrl_iface_path(hapd
);
3605 if (hapd
->conf
->ctrl_interface
&&
3606 rmdir(hapd
->conf
->ctrl_interface
) < 0) {
3607 if (errno
== ENOTEMPTY
) {
3608 wpa_printf(MSG_DEBUG
, "Control interface "
3609 "directory not empty - leaving it "
3612 wpa_printf(MSG_ERROR
,
3613 "rmdir[ctrl_interface=%s]: %s",
3614 hapd
->conf
->ctrl_interface
,
3618 #endif /* !CONFIG_CTRL_IFACE_UDP */
3621 dl_list_for_each_safe(dst
, prev
, &hapd
->ctrl_dst
, struct wpa_ctrl_dst
,
3625 #ifdef CONFIG_TESTING_OPTIONS
3626 l2_packet_deinit(hapd
->l2_test
);
3627 hapd
->l2_test
= NULL
;
3628 #endif /* CONFIG_TESTING_OPTIONS */
3632 static int hostapd_ctrl_iface_add(struct hapd_interfaces
*interfaces
,
3635 if (hostapd_add_iface(interfaces
, buf
) < 0) {
3636 wpa_printf(MSG_ERROR
, "Adding interface %s failed", buf
);
3643 static int hostapd_ctrl_iface_remove(struct hapd_interfaces
*interfaces
,
3646 if (hostapd_remove_iface(interfaces
, buf
) < 0) {
3647 wpa_printf(MSG_ERROR
, "Removing interface %s failed", buf
);
3654 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces
*interfaces
,
3655 struct sockaddr_storage
*from
,
3656 socklen_t fromlen
, char *input
)
3658 return ctrl_iface_attach(&interfaces
->global_ctrl_dst
, from
, fromlen
,
3663 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces
*interfaces
,
3664 struct sockaddr_storage
*from
,
3667 return ctrl_iface_detach(&interfaces
->global_ctrl_dst
, from
, fromlen
);
3671 static void hostapd_ctrl_iface_flush(struct hapd_interfaces
*interfaces
)
3673 #ifdef CONFIG_WPS_TESTING
3674 wps_version_number
= 0x20;
3675 wps_testing_dummy_cred
= 0;
3676 wps_corrupt_pkhash
= 0;
3677 #endif /* CONFIG_WPS_TESTING */
3679 #ifdef CONFIG_TESTING_OPTIONS
3681 dpp_test
= DPP_TEST_DISABLED
;
3682 #endif /* CONFIG_DPP */
3683 #endif /* CONFIG_TESTING_OPTIONS */
3686 hostapd_dpp_deinit_global(interfaces
);
3687 #endif /* CONFIG_DPP */
3694 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces
*interfaces
,
3697 char ifname
[IFNAMSIZ
+ 1];
3698 struct fst_iface_cfg cfg
;
3699 struct hostapd_data
*hapd
;
3700 struct fst_wpa_obj iface_obj
;
3702 if (!fst_parse_attach_command(cmd
, ifname
, sizeof(ifname
), &cfg
)) {
3703 hapd
= hostapd_get_iface(interfaces
, ifname
);
3705 if (hapd
->iface
->fst
) {
3706 wpa_printf(MSG_INFO
, "FST: Already attached");
3709 fst_hostapd_fill_iface_obj(hapd
, &iface_obj
);
3710 hapd
->iface
->fst
= fst_attach(ifname
, hapd
->own_addr
,
3712 if (hapd
->iface
->fst
)
3722 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces
*interfaces
,
3725 char ifname
[IFNAMSIZ
+ 1];
3726 struct hostapd_data
* hapd
;
3728 if (!fst_parse_detach_command(cmd
, ifname
, sizeof(ifname
))) {
3729 hapd
= hostapd_get_iface(interfaces
, ifname
);
3731 if (!fst_iface_detach(ifname
)) {
3732 hapd
->iface
->fst
= NULL
;
3733 hapd
->iface
->fst_ies
= NULL
;
3742 #endif /* CONFIG_FST */
3745 static struct hostapd_data
*
3746 hostapd_interfaces_get_hapd(struct hapd_interfaces
*interfaces
,
3751 for (i
= 0; i
< interfaces
->count
; i
++) {
3752 struct hostapd_iface
*iface
= interfaces
->iface
[i
];
3754 for (j
= 0; j
< iface
->num_bss
; j
++) {
3755 struct hostapd_data
*hapd
;
3757 hapd
= iface
->bss
[j
];
3758 if (os_strcmp(ifname
, hapd
->conf
->iface
) == 0)
3767 static int hostapd_ctrl_iface_dup_param(struct hostapd_data
*src_hapd
,
3768 struct hostapd_data
*dst_hapd
,
3774 value
= os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN
);
3776 wpa_printf(MSG_ERROR
,
3777 "DUP: cannot allocate buffer to stringify %s",
3782 if (os_strcmp(param
, "wpa") == 0) {
3783 os_snprintf(value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
, "%d",
3784 src_hapd
->conf
->wpa
);
3785 } else if (os_strcmp(param
, "wpa_key_mgmt") == 0 &&
3786 src_hapd
->conf
->wpa_key_mgmt
) {
3787 res
= hostapd_ctrl_iface_get_key_mgmt(
3788 src_hapd
, value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
);
3789 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN
, res
))
3790 goto error_stringify
;
3791 } else if (os_strcmp(param
, "wpa_pairwise") == 0 &&
3792 src_hapd
->conf
->wpa_pairwise
) {
3793 res
= wpa_write_ciphers(value
,
3794 value
+ HOSTAPD_CLI_DUP_VALUE_MAX_LEN
,
3795 src_hapd
->conf
->wpa_pairwise
, " ");
3797 goto error_stringify
;
3798 } else if (os_strcmp(param
, "rsn_pairwise") == 0 &&
3799 src_hapd
->conf
->rsn_pairwise
) {
3800 res
= wpa_write_ciphers(value
,
3801 value
+ HOSTAPD_CLI_DUP_VALUE_MAX_LEN
,
3802 src_hapd
->conf
->rsn_pairwise
, " ");
3804 goto error_stringify
;
3805 } else if (os_strcmp(param
, "wpa_passphrase") == 0 &&
3806 src_hapd
->conf
->ssid
.wpa_passphrase
) {
3807 os_snprintf(value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
, "%s",
3808 src_hapd
->conf
->ssid
.wpa_passphrase
);
3809 } else if (os_strcmp(param
, "wpa_psk") == 0 &&
3810 src_hapd
->conf
->ssid
.wpa_psk_set
) {
3811 wpa_snprintf_hex(value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
,
3812 src_hapd
->conf
->ssid
.wpa_psk
->psk
, PMK_LEN
);
3814 wpa_printf(MSG_WARNING
, "DUP: %s cannot be duplicated", param
);
3818 res
= hostapd_set_iface(dst_hapd
->iconf
, dst_hapd
->conf
, param
, value
);
3823 wpa_printf(MSG_ERROR
, "DUP: cannot stringify %s", param
);
3831 hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces
*interfaces
,
3833 char *reply
, int reply_size
)
3838 struct hostapd_iface
*iface
;
3842 show_ctrl
= !!os_strstr(input
, "ctrl");
3845 end
= reply
+ reply_size
;
3847 for (i
= 0; i
< interfaces
->count
; i
++) {
3848 iface
= interfaces
->iface
[i
];
3850 for (j
= 0; j
< iface
->num_bss
; j
++) {
3851 struct hostapd_bss_config
*conf
;
3853 conf
= iface
->conf
->bss
[j
];
3855 res
= os_snprintf(pos
, end
- pos
,
3856 "%s ctrl_iface=%s\n",
3858 conf
->ctrl_interface
?
3859 conf
->ctrl_interface
: "N/A");
3861 res
= os_snprintf(pos
, end
- pos
, "%s\n",
3863 if (os_snprintf_error(end
- pos
, res
)) {
3876 hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces
*interfaces
,
3879 char *p_start
= cmd
, *p_end
;
3880 struct hostapd_data
*src_hapd
, *dst_hapd
;
3882 /* cmd: "<src ifname> <dst ifname> <variable name> */
3884 p_end
= os_strchr(p_start
, ' ');
3886 wpa_printf(MSG_ERROR
, "DUP: no src ifname found in cmd: '%s'",
3892 src_hapd
= hostapd_interfaces_get_hapd(interfaces
, p_start
);
3894 wpa_printf(MSG_ERROR
, "DUP: no src ifname found: '%s'",
3899 p_start
= p_end
+ 1;
3900 p_end
= os_strchr(p_start
, ' ');
3902 wpa_printf(MSG_ERROR
, "DUP: no dst ifname found in cmd: '%s'",
3908 dst_hapd
= hostapd_interfaces_get_hapd(interfaces
, p_start
);
3910 wpa_printf(MSG_ERROR
, "DUP: no dst ifname found: '%s'",
3915 p_start
= p_end
+ 1;
3916 return hostapd_ctrl_iface_dup_param(src_hapd
, dst_hapd
, p_start
);
3920 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces
*interfaces
,
3922 char *buf
, char *reply
,
3924 struct sockaddr_storage
*from
,
3927 struct hostapd_data
*hapd
;
3929 hapd
= hostapd_interfaces_get_hapd(interfaces
, ifname
);
3933 res
= os_snprintf(reply
, reply_size
, "FAIL-NO-IFNAME-MATCH\n");
3934 if (os_snprintf_error(reply_size
, res
))
3939 return hostapd_ctrl_iface_receive_process(hapd
, buf
, reply
,reply_size
,
3944 static void hostapd_global_ctrl_iface_receive(int sock
, void *eloop_ctx
,
3947 void *interfaces
= eloop_ctx
;
3948 char buffer
[256], *buf
= buffer
;
3950 struct sockaddr_storage from
;
3951 socklen_t fromlen
= sizeof(from
);
3954 const int reply_size
= 4096;
3955 #ifdef CONFIG_CTRL_IFACE_UDP
3956 unsigned char lcookie
[COOKIE_LEN
];
3957 #endif /* CONFIG_CTRL_IFACE_UDP */
3959 res
= recvfrom(sock
, buffer
, sizeof(buffer
) - 1, 0,
3960 (struct sockaddr
*) &from
, &fromlen
);
3962 wpa_printf(MSG_ERROR
, "recvfrom(ctrl_iface): %s",
3967 wpa_printf(MSG_DEBUG
, "Global ctrl_iface command: %s", buf
);
3969 reply
= os_malloc(reply_size
);
3970 if (reply
== NULL
) {
3971 if (sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
3973 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
3979 os_memcpy(reply
, "OK\n", 3);
3982 #ifdef CONFIG_CTRL_IFACE_UDP
3983 if (os_strcmp(buf
, "GET_COOKIE") == 0) {
3984 os_memcpy(reply
, "COOKIE=", 7);
3985 wpa_snprintf_hex(reply
+ 7, 2 * COOKIE_LEN
+ 1,
3986 gcookie
, COOKIE_LEN
);
3987 reply_len
= 7 + 2 * COOKIE_LEN
;
3991 if (os_strncmp(buf
, "COOKIE=", 7) != 0 ||
3992 hexstr2bin(buf
+ 7, lcookie
, COOKIE_LEN
) < 0) {
3993 wpa_printf(MSG_DEBUG
,
3994 "CTRL: No cookie in the request - drop request");
3999 if (os_memcmp(gcookie
, lcookie
, COOKIE_LEN
) != 0) {
4000 wpa_printf(MSG_DEBUG
,
4001 "CTRL: Invalid cookie in the request - drop request");
4006 buf
+= 7 + 2 * COOKIE_LEN
;
4009 #endif /* CONFIG_CTRL_IFACE_UDP */
4011 if (os_strncmp(buf
, "IFNAME=", 7) == 0) {
4012 char *pos
= os_strchr(buf
+ 7, ' ');
4016 reply_len
= hostapd_global_ctrl_iface_ifname(
4017 interfaces
, buf
+ 7, pos
, reply
, reply_size
,
4023 if (os_strcmp(buf
, "PING") == 0) {
4024 os_memcpy(reply
, "PONG\n", 5);
4026 } else if (os_strncmp(buf
, "RELOG", 5) == 0) {
4027 if (wpa_debug_reopen_file() < 0)
4029 } else if (os_strcmp(buf
, "FLUSH") == 0) {
4030 hostapd_ctrl_iface_flush(interfaces
);
4031 } else if (os_strncmp(buf
, "ADD ", 4) == 0) {
4032 if (hostapd_ctrl_iface_add(interfaces
, buf
+ 4) < 0)
4034 } else if (os_strncmp(buf
, "REMOVE ", 7) == 0) {
4035 if (hostapd_ctrl_iface_remove(interfaces
, buf
+ 7) < 0)
4037 } else if (os_strcmp(buf
, "ATTACH") == 0) {
4038 if (hostapd_global_ctrl_iface_attach(interfaces
, &from
,
4041 } else if (os_strncmp(buf
, "ATTACH ", 7) == 0) {
4042 if (hostapd_global_ctrl_iface_attach(interfaces
, &from
,
4045 } else if (os_strcmp(buf
, "DETACH") == 0) {
4046 if (hostapd_global_ctrl_iface_detach(interfaces
, &from
,
4049 #ifdef CONFIG_MODULE_TESTS
4050 } else if (os_strcmp(buf
, "MODULE_TESTS") == 0) {
4051 if (hapd_module_tests() < 0)
4053 #endif /* CONFIG_MODULE_TESTS */
4055 } else if (os_strncmp(buf
, "FST-ATTACH ", 11) == 0) {
4056 if (!hostapd_global_ctrl_iface_fst_attach(interfaces
, buf
+ 11))
4057 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
4060 } else if (os_strncmp(buf
, "FST-DETACH ", 11) == 0) {
4061 if (!hostapd_global_ctrl_iface_fst_detach(interfaces
, buf
+ 11))
4062 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
4065 } else if (os_strncmp(buf
, "FST-MANAGER ", 12) == 0) {
4066 reply_len
= fst_ctrl_iface_receive(buf
+ 12, reply
, reply_size
);
4067 #endif /* CONFIG_FST */
4068 } else if (os_strncmp(buf
, "DUP_NETWORK ", 12) == 0) {
4069 if (!hostapd_global_ctrl_iface_dup_network(interfaces
,
4071 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
4074 } else if (os_strncmp(buf
, "INTERFACES", 10) == 0) {
4075 reply_len
= hostapd_global_ctrl_iface_interfaces(
4076 interfaces
, buf
+ 10, reply
, sizeof(buffer
));
4077 } else if (os_strcmp(buf
, "TERMINATE") == 0) {
4080 wpa_printf(MSG_DEBUG
, "Unrecognized global ctrl_iface command "
4086 if (reply_len
< 0) {
4087 os_memcpy(reply
, "FAIL\n", 5);
4091 if (sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
4093 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
4100 #ifndef CONFIG_CTRL_IFACE_UDP
4101 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces
*interface
)
4106 if (interface
->global_iface_path
== NULL
)
4109 len
= os_strlen(interface
->global_iface_path
) +
4110 os_strlen(interface
->global_iface_name
) + 2;
4111 buf
= os_malloc(len
);
4115 os_snprintf(buf
, len
, "%s/%s", interface
->global_iface_path
,
4116 interface
->global_iface_name
);
4117 buf
[len
- 1] = '\0';
4120 #endif /* CONFIG_CTRL_IFACE_UDP */
4123 int hostapd_global_ctrl_iface_init(struct hapd_interfaces
*interface
)
4125 #ifdef CONFIG_CTRL_IFACE_UDP
4126 int port
= HOSTAPD_GLOBAL_CTRL_IFACE_PORT
;
4129 struct addrinfo hints
= { 0 }, *res
, *saveres
;
4132 if (interface
->global_ctrl_sock
> -1) {
4133 wpa_printf(MSG_DEBUG
, "ctrl_iface already exists!");
4137 if (interface
->global_iface_path
== NULL
)
4140 pos
= os_strstr(interface
->global_iface_path
, "udp:");
4145 wpa_printf(MSG_ERROR
, "Invalid global ctrl UDP port");
4150 os_get_random(gcookie
, COOKIE_LEN
);
4152 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
4153 hints
.ai_flags
= AI_PASSIVE
;
4154 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
4156 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
4157 hints
.ai_family
= AF_INET6
;
4158 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4159 hints
.ai_family
= AF_INET
;
4160 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
4161 hints
.ai_socktype
= SOCK_DGRAM
;
4164 os_snprintf(p
, sizeof(p
), "%d", port
);
4165 n
= getaddrinfo(NULL
, p
, &hints
, &res
);
4167 wpa_printf(MSG_ERROR
, "getaddrinfo(): %s", gai_strerror(n
));
4172 interface
->global_ctrl_sock
= socket(res
->ai_family
, res
->ai_socktype
,
4174 if (interface
->global_ctrl_sock
< 0) {
4175 wpa_printf(MSG_ERROR
, "socket(PF_INET): %s", strerror(errno
));
4179 if (bind(interface
->global_ctrl_sock
, res
->ai_addr
, res
->ai_addrlen
) <
4182 if ((port
- HOSTAPD_GLOBAL_CTRL_IFACE_PORT
) <
4183 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT
&& !pos
)
4185 wpa_printf(MSG_ERROR
, "bind(AF_INET): %s", strerror(errno
));
4189 freeaddrinfo(saveres
);
4191 wpa_printf(MSG_DEBUG
, "global ctrl_iface_init UDP port: %d", port
);
4193 if (eloop_register_read_sock(interface
->global_ctrl_sock
,
4194 hostapd_global_ctrl_iface_receive
,
4195 interface
, NULL
) < 0) {
4196 hostapd_global_ctrl_iface_deinit(interface
);
4203 if (interface
->global_ctrl_sock
>= 0)
4204 close(interface
->global_ctrl_sock
);
4206 #else /* CONFIG_CTRL_IFACE_UDP */
4207 struct sockaddr_un addr
;
4211 if (interface
->global_iface_path
== NULL
) {
4212 wpa_printf(MSG_DEBUG
, "ctrl_iface not configured!");
4216 if (mkdir(interface
->global_iface_path
, S_IRWXU
| S_IRWXG
) < 0) {
4217 if (errno
== EEXIST
) {
4218 wpa_printf(MSG_DEBUG
, "Using existing control "
4219 "interface directory.");
4221 wpa_printf(MSG_ERROR
, "mkdir[ctrl_interface]: %s",
4225 } else if (interface
->ctrl_iface_group
&&
4226 chown(interface
->global_iface_path
, -1,
4227 interface
->ctrl_iface_group
) < 0) {
4228 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
4233 if (os_strlen(interface
->global_iface_path
) + 1 +
4234 os_strlen(interface
->global_iface_name
) >= sizeof(addr
.sun_path
))
4237 s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
4239 wpa_printf(MSG_ERROR
, "socket(PF_UNIX): %s", strerror(errno
));
4243 os_memset(&addr
, 0, sizeof(addr
));
4245 addr
.sun_len
= sizeof(addr
);
4246 #endif /* __FreeBSD__ */
4247 addr
.sun_family
= AF_UNIX
;
4248 fname
= hostapd_global_ctrl_iface_path(interface
);
4251 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
4252 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
4253 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
4255 if (connect(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
4256 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
4257 " allow connections - assuming it was left"
4258 "over from forced program termination");
4259 if (unlink(fname
) < 0) {
4260 wpa_printf(MSG_ERROR
,
4261 "Could not unlink existing ctrl_iface socket '%s': %s",
4262 fname
, strerror(errno
));
4265 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) <
4267 wpa_printf(MSG_ERROR
, "bind(PF_UNIX): %s",
4271 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
4272 "ctrl_iface socket '%s'", fname
);
4274 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
4275 "be in use - cannot override it");
4276 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
4277 "not used anymore", fname
);
4284 if (interface
->ctrl_iface_group
&&
4285 chown(fname
, -1, interface
->ctrl_iface_group
) < 0) {
4286 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
4291 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
4292 wpa_printf(MSG_ERROR
, "chmod[ctrl_interface/ifname]: %s",
4298 interface
->global_ctrl_sock
= s
;
4299 eloop_register_read_sock(s
, hostapd_global_ctrl_iface_receive
,
4312 #endif /* CONFIG_CTRL_IFACE_UDP */
4316 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces
*interfaces
)
4318 #ifndef CONFIG_CTRL_IFACE_UDP
4320 #endif /* CONFIG_CTRL_IFACE_UDP */
4321 struct wpa_ctrl_dst
*dst
, *prev
;
4323 if (interfaces
->global_ctrl_sock
> -1) {
4324 eloop_unregister_read_sock(interfaces
->global_ctrl_sock
);
4325 close(interfaces
->global_ctrl_sock
);
4326 interfaces
->global_ctrl_sock
= -1;
4327 #ifndef CONFIG_CTRL_IFACE_UDP
4328 fname
= hostapd_global_ctrl_iface_path(interfaces
);
4334 if (interfaces
->global_iface_path
&&
4335 rmdir(interfaces
->global_iface_path
) < 0) {
4336 if (errno
== ENOTEMPTY
) {
4337 wpa_printf(MSG_DEBUG
, "Control interface "
4338 "directory not empty - leaving it "
4341 wpa_printf(MSG_ERROR
,
4342 "rmdir[ctrl_interface=%s]: %s",
4343 interfaces
->global_iface_path
,
4347 #endif /* CONFIG_CTRL_IFACE_UDP */
4350 os_free(interfaces
->global_iface_path
);
4351 interfaces
->global_iface_path
= NULL
;
4353 dl_list_for_each_safe(dst
, prev
, &interfaces
->global_ctrl_dst
,
4354 struct wpa_ctrl_dst
, list
)
4359 static int hostapd_ctrl_check_event_enabled(struct wpa_ctrl_dst
*dst
,
4362 /* Enable Probe Request events based on explicit request.
4363 * Other events are enabled by default.
4365 if (str_starts(buf
, RX_PROBE_REQUEST
))
4366 return !!(dst
->events
& WPA_EVENT_RX_PROBE_REQUEST
);
4371 static void hostapd_ctrl_iface_send(struct hostapd_data
*hapd
, int level
,
4372 enum wpa_msg_type type
,
4373 const char *buf
, size_t len
)
4375 struct wpa_ctrl_dst
*dst
, *next
;
4376 struct dl_list
*ctrl_dst
;
4383 if (type
!= WPA_MSG_ONLY_GLOBAL
) {
4384 s
= hapd
->ctrl_sock
;
4385 ctrl_dst
= &hapd
->ctrl_dst
;
4387 s
= hapd
->iface
->interfaces
->global_ctrl_sock
;
4388 ctrl_dst
= &hapd
->iface
->interfaces
->global_ctrl_dst
;
4391 if (s
< 0 || dl_list_empty(ctrl_dst
))
4394 os_snprintf(levelstr
, sizeof(levelstr
), "<%d>", level
);
4395 io
[0].iov_base
= levelstr
;
4396 io
[0].iov_len
= os_strlen(levelstr
);
4397 io
[1].iov_base
= (char *) buf
;
4398 io
[1].iov_len
= len
;
4399 os_memset(&msg
, 0, sizeof(msg
));
4404 dl_list_for_each_safe(dst
, next
, ctrl_dst
, struct wpa_ctrl_dst
, list
) {
4405 if ((level
>= dst
->debug_level
) &&
4406 hostapd_ctrl_check_event_enabled(dst
, buf
)) {
4407 sockaddr_print(MSG_DEBUG
, "CTRL_IFACE monitor send",
4408 &dst
->addr
, dst
->addrlen
);
4409 msg
.msg_name
= &dst
->addr
;
4410 msg
.msg_namelen
= dst
->addrlen
;
4411 if (sendmsg(s
, &msg
, 0) < 0) {
4413 wpa_printf(MSG_INFO
, "CTRL_IFACE monitor[%d]: "
4415 idx
, errno
, strerror(errno
));
4417 if (dst
->errors
> 10 || _errno
== ENOENT
) {
4418 if (type
!= WPA_MSG_ONLY_GLOBAL
)
4419 hostapd_ctrl_iface_detach(
4423 hostapd_global_ctrl_iface_detach(
4424 hapd
->iface
->interfaces
,
4435 #endif /* CONFIG_NATIVE_WINDOWS */