2 * hostapd / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #ifndef CONFIG_NATIVE_WINDOWS
13 #ifdef CONFIG_TESTING_OPTIONS
14 #include <net/ethernet.h>
15 #include <netinet/ip.h>
16 #endif /* CONFIG_TESTING_OPTIONS */
22 #include "utils/common.h"
23 #include "utils/eloop.h"
24 #include "common/version.h"
25 #include "common/ieee802_11_defs.h"
26 #include "crypto/tls.h"
27 #include "drivers/driver.h"
28 #include "eapol_auth/eapol_auth_sm.h"
29 #include "radius/radius_client.h"
30 #include "radius/radius_server.h"
31 #include "l2_packet/l2_packet.h"
32 #include "ap/hostapd.h"
33 #include "ap/ap_config.h"
34 #include "ap/ieee802_1x.h"
35 #include "ap/wpa_auth.h"
36 #include "ap/ieee802_11.h"
37 #include "ap/sta_info.h"
38 #include "ap/wps_hostapd.h"
39 #include "ap/ctrl_iface_ap.h"
40 #include "ap/ap_drv_ops.h"
42 #include "ap/wnm_ap.h"
43 #include "ap/wpa_auth.h"
44 #include "ap/beacon.h"
45 #include "wps/wps_defs.h"
47 #include "fst/fst_ctrl_iface.h"
48 #include "config_file.h"
49 #include "ctrl_iface.h"
53 struct wpa_ctrl_dst
*next
;
54 struct sockaddr_un addr
;
61 static void hostapd_ctrl_iface_send(struct hostapd_data
*hapd
, int level
,
62 enum wpa_msg_type type
,
63 const char *buf
, size_t len
);
66 static int hostapd_ctrl_iface_attach(struct hostapd_data
*hapd
,
67 struct sockaddr_un
*from
,
70 struct wpa_ctrl_dst
*dst
;
72 dst
= os_zalloc(sizeof(*dst
));
75 os_memcpy(&dst
->addr
, from
, sizeof(struct sockaddr_un
));
76 dst
->addrlen
= fromlen
;
77 dst
->debug_level
= MSG_INFO
;
78 dst
->next
= hapd
->ctrl_dst
;
80 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor attached",
81 (u8
*) from
->sun_path
,
82 fromlen
- offsetof(struct sockaddr_un
, sun_path
));
87 static int hostapd_ctrl_iface_detach(struct hostapd_data
*hapd
,
88 struct sockaddr_un
*from
,
91 struct wpa_ctrl_dst
*dst
, *prev
= NULL
;
95 if (fromlen
== dst
->addrlen
&&
96 os_memcmp(from
->sun_path
, dst
->addr
.sun_path
,
97 fromlen
- offsetof(struct sockaddr_un
, sun_path
))
99 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor detached",
100 (u8
*) from
->sun_path
,
102 offsetof(struct sockaddr_un
, sun_path
));
104 hapd
->ctrl_dst
= dst
->next
;
106 prev
->next
= dst
->next
;
117 static int hostapd_ctrl_iface_level(struct hostapd_data
*hapd
,
118 struct sockaddr_un
*from
,
122 struct wpa_ctrl_dst
*dst
;
124 wpa_printf(MSG_DEBUG
, "CTRL_IFACE LEVEL %s", level
);
126 dst
= hapd
->ctrl_dst
;
128 if (fromlen
== dst
->addrlen
&&
129 os_memcmp(from
->sun_path
, dst
->addr
.sun_path
,
130 fromlen
- offsetof(struct sockaddr_un
, sun_path
))
132 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE changed monitor "
133 "level", (u8
*) from
->sun_path
, fromlen
-
134 offsetof(struct sockaddr_un
, sun_path
));
135 dst
->debug_level
= atoi(level
);
145 static int hostapd_ctrl_iface_new_sta(struct hostapd_data
*hapd
,
149 struct sta_info
*sta
;
151 wpa_printf(MSG_DEBUG
, "CTRL_IFACE NEW_STA %s", txtaddr
);
153 if (hwaddr_aton(txtaddr
, addr
))
156 sta
= ap_get_sta(hapd
, addr
);
160 wpa_printf(MSG_DEBUG
, "Add new STA " MACSTR
" based on ctrl_iface "
161 "notification", MAC2STR(addr
));
162 sta
= ap_sta_add(hapd
, addr
);
166 hostapd_new_assoc_sta(hapd
, sta
, 0);
171 #ifdef CONFIG_IEEE80211W
173 static int hostapd_ctrl_iface_sa_query(struct hostapd_data
*hapd
,
177 u8 trans_id
[WLAN_SA_QUERY_TR_ID_LEN
];
179 wpa_printf(MSG_DEBUG
, "CTRL_IFACE SA_QUERY %s", txtaddr
);
181 if (hwaddr_aton(txtaddr
, addr
) ||
182 os_get_random(trans_id
, WLAN_SA_QUERY_TR_ID_LEN
) < 0)
185 ieee802_11_send_sa_query_req(hapd
, addr
, trans_id
);
189 #endif /* NEED_AP_MLME */
190 #endif /* CONFIG_IEEE80211W */
194 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data
*hapd
, char *txt
)
196 char *pin
= os_strchr(txt
, ' ');
199 u8 addr_buf
[ETH_ALEN
], *addr
= NULL
;
206 timeout_txt
= os_strchr(pin
, ' ');
208 *timeout_txt
++ = '\0';
209 timeout
= atoi(timeout_txt
);
210 pos
= os_strchr(timeout_txt
, ' ');
213 if (hwaddr_aton(pos
, addr_buf
) == 0)
219 return hostapd_wps_add_pin(hapd
, addr
, txt
, pin
, timeout
);
223 static int hostapd_ctrl_iface_wps_check_pin(
224 struct hostapd_data
*hapd
, char *cmd
, char *buf
, size_t buflen
)
231 wpa_hexdump_ascii_key(MSG_DEBUG
, "WPS_CHECK_PIN",
232 (u8
*) cmd
, os_strlen(cmd
));
233 for (pos
= cmd
, len
= 0; *pos
!= '\0'; pos
++) {
234 if (*pos
< '0' || *pos
> '9')
238 wpa_printf(MSG_DEBUG
, "WPS: Too long PIN");
242 if (len
!= 4 && len
!= 8) {
243 wpa_printf(MSG_DEBUG
, "WPS: Invalid PIN length %d", (int) len
);
249 unsigned int pin_val
;
251 if (!wps_pin_valid(pin_val
)) {
252 wpa_printf(MSG_DEBUG
, "WPS: Invalid checksum digit");
253 ret
= os_snprintf(buf
, buflen
, "FAIL-CHECKSUM\n");
254 if (os_snprintf_error(buflen
, ret
))
260 ret
= os_snprintf(buf
, buflen
, "%s", pin
);
261 if (os_snprintf_error(buflen
, ret
))
268 #ifdef CONFIG_WPS_NFC
269 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data
*hapd
,
276 len
= os_strlen(pos
);
281 buf
= wpabuf_alloc(len
);
284 if (hexstr2bin(pos
, wpabuf_put(buf
, len
), len
) < 0) {
289 ret
= hostapd_wps_nfc_tag_read(hapd
, buf
);
296 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data
*hapd
,
297 char *cmd
, char *reply
,
304 if (os_strcmp(cmd
, "WPS") == 0)
306 else if (os_strcmp(cmd
, "NDEF") == 0)
311 buf
= hostapd_wps_nfc_config_token(hapd
, ndef
);
315 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
326 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data
*hapd
,
327 char *reply
, size_t max_len
,
333 buf
= hostapd_wps_nfc_token_gen(hapd
, ndef
);
337 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
348 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data
*hapd
,
349 char *cmd
, char *reply
,
352 if (os_strcmp(cmd
, "WPS") == 0)
353 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd
, reply
,
356 if (os_strcmp(cmd
, "NDEF") == 0)
357 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd
, reply
,
360 if (os_strcmp(cmd
, "enable") == 0)
361 return hostapd_wps_nfc_token_enable(hapd
);
363 if (os_strcmp(cmd
, "disable") == 0) {
364 hostapd_wps_nfc_token_disable(hapd
);
372 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data
*hapd
,
373 char *cmd
, char *reply
,
381 pos
= os_strchr(cmd
, ' ');
386 if (os_strcmp(cmd
, "WPS") == 0)
388 else if (os_strcmp(cmd
, "NDEF") == 0)
393 if (os_strcmp(pos
, "WPS-CR") == 0)
394 buf
= hostapd_wps_nfc_hs_cr(hapd
, ndef
);
400 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
411 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data
*hapd
,
415 struct wpabuf
*req
, *sel
;
417 char *pos
, *role
, *type
, *pos2
;
420 pos
= os_strchr(role
, ' ');
426 pos
= os_strchr(type
, ' ');
431 pos2
= os_strchr(pos
, ' ');
436 len
= os_strlen(pos
);
441 req
= wpabuf_alloc(len
);
444 if (hexstr2bin(pos
, wpabuf_put(req
, len
), len
) < 0) {
449 len
= os_strlen(pos2
);
456 sel
= wpabuf_alloc(len
);
461 if (hexstr2bin(pos2
, wpabuf_put(sel
, len
), len
) < 0) {
467 if (os_strcmp(role
, "RESP") == 0 && os_strcmp(type
, "WPS") == 0) {
468 ret
= hostapd_wps_nfc_report_handover(hapd
, req
, sel
);
470 wpa_printf(MSG_DEBUG
, "NFC: Unsupported connection handover "
471 "reported: role=%s type=%s", role
, type
);
480 #endif /* CONFIG_WPS_NFC */
483 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data
*hapd
, char *txt
,
484 char *buf
, size_t buflen
)
490 pos
= os_strchr(txt
, ' ');
494 if (os_strcmp(txt
, "disable") == 0) {
495 hostapd_wps_ap_pin_disable(hapd
);
496 return os_snprintf(buf
, buflen
, "OK\n");
499 if (os_strcmp(txt
, "random") == 0) {
502 pin_txt
= hostapd_wps_ap_pin_random(hapd
, timeout
);
505 return os_snprintf(buf
, buflen
, "%s", pin_txt
);
508 if (os_strcmp(txt
, "get") == 0) {
509 pin_txt
= hostapd_wps_ap_pin_get(hapd
);
512 return os_snprintf(buf
, buflen
, "%s", pin_txt
);
515 if (os_strcmp(txt
, "set") == 0) {
520 pos
= os_strchr(pos
, ' ');
525 if (os_strlen(pin
) > buflen
)
527 if (hostapd_wps_ap_pin_set(hapd
, pin
, timeout
) < 0)
529 return os_snprintf(buf
, buflen
, "%s", pin
);
536 static int hostapd_ctrl_iface_wps_config(struct hostapd_data
*hapd
, char *txt
)
539 char *ssid
, *auth
, *encr
= NULL
, *key
= NULL
;
542 pos
= os_strchr(txt
, ' ');
548 pos
= os_strchr(pos
, ' ');
552 pos
= os_strchr(pos
, ' ');
559 return hostapd_wps_config_ap(hapd
, ssid
, auth
, encr
, key
);
563 static const char * pbc_status_str(enum pbc_status status
)
566 case WPS_PBC_STATUS_DISABLE
:
568 case WPS_PBC_STATUS_ACTIVE
:
570 case WPS_PBC_STATUS_TIMEOUT
:
572 case WPS_PBC_STATUS_OVERLAP
:
580 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data
*hapd
,
581 char *buf
, size_t buflen
)
589 ret
= os_snprintf(pos
, end
- pos
, "PBC Status: %s\n",
590 pbc_status_str(hapd
->wps_stats
.pbc_status
));
592 if (os_snprintf_error(end
- pos
, ret
))
596 ret
= os_snprintf(pos
, end
- pos
, "Last WPS result: %s\n",
597 (hapd
->wps_stats
.status
== WPS_STATUS_SUCCESS
?
599 (hapd
->wps_stats
.status
== WPS_STATUS_FAILURE
?
600 "Failed" : "None")));
602 if (os_snprintf_error(end
- pos
, ret
))
606 /* If status == Failure - Add possible Reasons */
607 if(hapd
->wps_stats
.status
== WPS_STATUS_FAILURE
&&
608 hapd
->wps_stats
.failure_reason
> 0) {
609 ret
= os_snprintf(pos
, end
- pos
,
610 "Failure Reason: %s\n",
611 wps_ei_str(hapd
->wps_stats
.failure_reason
));
613 if (os_snprintf_error(end
- pos
, ret
))
618 if (hapd
->wps_stats
.status
) {
619 ret
= os_snprintf(pos
, end
- pos
, "Peer Address: " MACSTR
"\n",
620 MAC2STR(hapd
->wps_stats
.peer_addr
));
622 if (os_snprintf_error(end
- pos
, ret
))
630 #endif /* CONFIG_WPS */
634 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data
*hapd
,
640 if (hwaddr_aton(cmd
, addr
))
653 return hs20_send_wnm_notification(hapd
, addr
, 1, url
);
657 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data
*hapd
,
661 int code
, reauth_delay
, ret
;
666 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
667 if (hwaddr_aton(cmd
, addr
))
670 pos
= os_strchr(cmd
, ' ');
676 pos
= os_strchr(pos
, ' ');
680 reauth_delay
= atoi(pos
);
683 pos
= os_strchr(pos
, ' ');
686 url_len
= os_strlen(pos
);
689 req
= wpabuf_alloc(4 + url_len
);
692 wpabuf_put_u8(req
, code
);
693 wpabuf_put_le16(req
, reauth_delay
);
694 wpabuf_put_u8(req
, url_len
);
696 wpabuf_put_data(req
, pos
, url_len
);
698 wpa_printf(MSG_DEBUG
, "HS 2.0: Send WNM-Notification to " MACSTR
699 " to indicate imminent deauthentication (code=%d "
700 "reauth_delay=%d)", MAC2STR(addr
), code
, reauth_delay
);
701 ret
= hs20_send_wnm_notification_deauth_req(hapd
, addr
, req
);
706 #endif /* CONFIG_HS20 */
709 #ifdef CONFIG_INTERWORKING
711 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data
*hapd
,
714 u8 qos_map_set
[16 + 2 * 21], count
= 0;
715 const char *pos
= cmd
;
719 if (count
== sizeof(qos_map_set
)) {
720 wpa_printf(MSG_ERROR
, "Too many qos_map_set parameters");
725 if (val
< 0 || val
> 255) {
726 wpa_printf(MSG_INFO
, "Invalid QoS Map Set");
730 qos_map_set
[count
++] = val
;
731 pos
= os_strchr(pos
, ',');
737 if (count
< 16 || count
& 1) {
738 wpa_printf(MSG_INFO
, "Invalid QoS Map Set");
742 ret
= hostapd_drv_set_qos_map(hapd
, qos_map_set
, count
);
744 wpa_printf(MSG_INFO
, "Failed to set QoS Map Set");
748 os_memcpy(hapd
->conf
->qos_map_set
, qos_map_set
, count
);
749 hapd
->conf
->qos_map_set_len
= count
;
755 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data
*hapd
,
759 struct sta_info
*sta
;
761 u8
*qos_map_set
= hapd
->conf
->qos_map_set
;
762 u8 qos_map_set_len
= hapd
->conf
->qos_map_set_len
;
765 if (!qos_map_set_len
) {
766 wpa_printf(MSG_INFO
, "QoS Map Set is not set");
770 if (hwaddr_aton(cmd
, addr
))
773 sta
= ap_get_sta(hapd
, addr
);
775 wpa_printf(MSG_DEBUG
, "Station " MACSTR
" not found "
776 "for QoS Map Configuration message",
781 if (!sta
->qos_map_enabled
) {
782 wpa_printf(MSG_DEBUG
, "Station " MACSTR
" did not indicate "
783 "support for QoS Map", MAC2STR(addr
));
787 buf
= wpabuf_alloc(2 + 2 + qos_map_set_len
);
791 wpabuf_put_u8(buf
, WLAN_ACTION_QOS
);
792 wpabuf_put_u8(buf
, QOS_QOS_MAP_CONFIG
);
794 /* QoS Map Set Element */
795 wpabuf_put_u8(buf
, WLAN_EID_QOS_MAP_SET
);
796 wpabuf_put_u8(buf
, qos_map_set_len
);
797 wpabuf_put_data(buf
, qos_map_set
, qos_map_set_len
);
799 ret
= hostapd_drv_send_action(hapd
, hapd
->iface
->freq
, 0, addr
,
800 wpabuf_head(buf
), wpabuf_len(buf
));
806 #endif /* CONFIG_INTERWORKING */
811 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data
*hapd
,
816 struct sta_info
*sta
;
818 if (hwaddr_aton(cmd
, addr
))
822 disassoc_timer
= atoi(cmd
+ 17);
824 sta
= ap_get_sta(hapd
, addr
);
826 wpa_printf(MSG_DEBUG
, "Station " MACSTR
827 " not found for disassociation imminent message",
832 return wnm_send_disassoc_imminent(hapd
, sta
, disassoc_timer
);
836 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data
*hapd
,
840 const char *url
, *timerstr
;
842 struct sta_info
*sta
;
844 if (hwaddr_aton(cmd
, addr
))
847 sta
= ap_get_sta(hapd
, addr
);
849 wpa_printf(MSG_DEBUG
, "Station " MACSTR
850 " not found for ESS disassociation imminent message",
856 if (*timerstr
!= ' ')
859 disassoc_timer
= atoi(timerstr
);
860 if (disassoc_timer
< 0 || disassoc_timer
> 65535)
863 url
= os_strchr(timerstr
, ' ');
868 return wnm_send_ess_disassoc_imminent(hapd
, sta
, url
, disassoc_timer
);
872 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data
*hapd
,
876 const char *pos
, *end
;
877 int disassoc_timer
= 0;
878 struct sta_info
*sta
;
879 u8 req_mode
= 0, valid_int
= 0x01;
884 u8
*nei_pos
= nei_rep
;
886 if (hwaddr_aton(cmd
, addr
)) {
887 wpa_printf(MSG_DEBUG
, "Invalid STA MAC address");
891 sta
= ap_get_sta(hapd
, addr
);
893 wpa_printf(MSG_DEBUG
, "Station " MACSTR
894 " not found for BSS TM Request message",
899 pos
= os_strstr(cmd
, " disassoc_timer=");
902 disassoc_timer
= atoi(pos
);
903 if (disassoc_timer
< 0 || disassoc_timer
> 65535) {
904 wpa_printf(MSG_DEBUG
, "Invalid disassoc_timer");
909 pos
= os_strstr(cmd
, " valid_int=");
912 valid_int
= atoi(pos
);
915 pos
= os_strstr(cmd
, " bss_term=");
918 req_mode
|= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED
;
919 /* TODO: TSF configurable/learnable */
920 bss_term_dur
[0] = 4; /* Subelement ID */
921 bss_term_dur
[1] = 10; /* Length */
922 os_memset(bss_term_dur
, 2, 8);
923 end
= os_strchr(pos
, ',');
925 wpa_printf(MSG_DEBUG
, "Invalid bss_term data");
929 WPA_PUT_LE16(&bss_term_dur
[10], atoi(end
));
934 * BSS Transition Candidate List Entries - Neighbor Report elements
935 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
936 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
944 pos
= os_strstr(pos
, " neighbor=");
947 if (nei_pos
+ 15 > nei_rep
+ sizeof(nei_rep
)) {
948 wpa_printf(MSG_DEBUG
,
949 "Not enough room for additional neighbor");
955 *nei_pos
++ = WLAN_EID_NEIGHBOR_REPORT
;
956 nei_pos
++; /* length to be filled in */
958 if (hwaddr_aton(pos
, nei_pos
)) {
959 wpa_printf(MSG_DEBUG
, "Invalid BSSID");
965 wpa_printf(MSG_DEBUG
, "Missing BSSID Information");
970 val
= strtol(pos
, &endptr
, 0);
971 WPA_PUT_LE32(nei_pos
, val
);
973 if (*endptr
!= ',') {
974 wpa_printf(MSG_DEBUG
, "Missing Operating Class");
979 *nei_pos
++ = atoi(pos
); /* Operating Class */
980 pos
= os_strchr(pos
, ',');
982 wpa_printf(MSG_DEBUG
, "Missing Channel Number");
987 *nei_pos
++ = atoi(pos
); /* Channel Number */
988 pos
= os_strchr(pos
, ',');
990 wpa_printf(MSG_DEBUG
, "Missing PHY Type");
995 *nei_pos
++ = atoi(pos
); /* PHY Type */
996 end
= os_strchr(pos
, ' ');
997 tmp
= os_strchr(pos
, ',');
998 if (tmp
&& (!end
|| tmp
< end
)) {
999 /* Optional Subelements (hexdump) */
1003 end
= os_strchr(pos
, ' ');
1007 len
= os_strlen(pos
);
1008 if (nei_pos
+ len
/ 2 > nei_rep
+ sizeof(nei_rep
)) {
1009 wpa_printf(MSG_DEBUG
,
1010 "Not enough room for neighbor subelements");
1014 hexstr2bin(pos
, nei_pos
, len
/ 2) < 0) {
1015 wpa_printf(MSG_DEBUG
,
1016 "Invalid neighbor subelement info");
1023 nei_start
[1] = nei_pos
- nei_start
- 2;
1026 pos
= os_strstr(cmd
, " url=");
1030 end
= os_strchr(pos
, ' ');
1034 len
= os_strlen(pos
);
1035 url
= os_malloc(len
+ 1);
1038 os_memcpy(url
, pos
, len
);
1040 req_mode
|= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT
;
1043 if (os_strstr(cmd
, " pref=1"))
1044 req_mode
|= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED
;
1045 if (os_strstr(cmd
, " abridged=1"))
1046 req_mode
|= WNM_BSS_TM_REQ_ABRIDGED
;
1047 if (os_strstr(cmd
, " disassoc_imminent=1"))
1048 req_mode
|= WNM_BSS_TM_REQ_DISASSOC_IMMINENT
;
1050 ret
= wnm_send_bss_tm_req(hapd
, sta
, req_mode
, disassoc_timer
,
1051 valid_int
, bss_term_dur
, url
,
1052 nei_pos
> nei_rep
? nei_rep
: NULL
,
1058 #endif /* CONFIG_WNM */
1061 static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data
*hapd
,
1062 char *buf
, size_t buflen
)
1070 WPA_ASSERT(hapd
->conf
->wpa_key_mgmt
);
1072 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_PSK
) {
1073 ret
= os_snprintf(pos
, end
- pos
, "WPA-PSK ");
1074 if (os_snprintf_error(end
- pos
, ret
))
1078 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X
) {
1079 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP ");
1080 if (os_snprintf_error(end
- pos
, ret
))
1084 #ifdef CONFIG_IEEE80211R
1085 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_PSK
) {
1086 ret
= os_snprintf(pos
, end
- pos
, "FT-PSK ");
1087 if (os_snprintf_error(end
- pos
, ret
))
1091 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_IEEE8021X
) {
1092 ret
= os_snprintf(pos
, end
- pos
, "FT-EAP ");
1093 if (os_snprintf_error(end
- pos
, ret
))
1098 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_SAE
) {
1099 ret
= os_snprintf(pos
, end
- pos
, "FT-SAE ");
1100 if (os_snprintf_error(end
- pos
, ret
))
1104 #endif /* CONFIG_SAE */
1105 #endif /* CONFIG_IEEE80211R */
1106 #ifdef CONFIG_IEEE80211W
1107 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_PSK_SHA256
) {
1108 ret
= os_snprintf(pos
, end
- pos
, "WPA-PSK-SHA256 ");
1109 if (os_snprintf_error(end
- pos
, ret
))
1113 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X_SHA256
) {
1114 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP-SHA256 ");
1115 if (os_snprintf_error(end
- pos
, ret
))
1119 #endif /* CONFIG_IEEE80211W */
1121 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_SAE
) {
1122 ret
= os_snprintf(pos
, end
- pos
, "SAE ");
1123 if (os_snprintf_error(end
- pos
, ret
))
1127 #endif /* CONFIG_SAE */
1128 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X_SUITE_B
) {
1129 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP-SUITE-B ");
1130 if (os_snprintf_error(end
- pos
, ret
))
1134 if (hapd
->conf
->wpa_key_mgmt
&
1135 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192
) {
1136 ret
= os_snprintf(pos
, end
- pos
,
1137 "WPA-EAP-SUITE-B-192 ");
1138 if (os_snprintf_error(end
- pos
, ret
))
1143 if (pos
> buf
&& *(pos
- 1) == ' ') {
1152 static int hostapd_ctrl_iface_get_config(struct hostapd_data
*hapd
,
1153 char *buf
, size_t buflen
)
1161 ret
= os_snprintf(pos
, end
- pos
, "bssid=" MACSTR
"\n"
1163 MAC2STR(hapd
->own_addr
),
1164 wpa_ssid_txt(hapd
->conf
->ssid
.ssid
,
1165 hapd
->conf
->ssid
.ssid_len
));
1166 if (os_snprintf_error(end
- pos
, ret
))
1171 ret
= os_snprintf(pos
, end
- pos
, "wps_state=%s\n",
1172 hapd
->conf
->wps_state
== 0 ? "disabled" :
1173 (hapd
->conf
->wps_state
== 1 ? "not configured" :
1175 if (os_snprintf_error(end
- pos
, ret
))
1179 if (hapd
->conf
->wps_state
&& hapd
->conf
->wpa
&&
1180 hapd
->conf
->ssid
.wpa_passphrase
) {
1181 ret
= os_snprintf(pos
, end
- pos
, "passphrase=%s\n",
1182 hapd
->conf
->ssid
.wpa_passphrase
);
1183 if (os_snprintf_error(end
- pos
, ret
))
1188 if (hapd
->conf
->wps_state
&& hapd
->conf
->wpa
&&
1189 hapd
->conf
->ssid
.wpa_psk
&&
1190 hapd
->conf
->ssid
.wpa_psk
->group
) {
1191 char hex
[PMK_LEN
* 2 + 1];
1192 wpa_snprintf_hex(hex
, sizeof(hex
),
1193 hapd
->conf
->ssid
.wpa_psk
->psk
, PMK_LEN
);
1194 ret
= os_snprintf(pos
, end
- pos
, "psk=%s\n", hex
);
1195 if (os_snprintf_error(end
- pos
, ret
))
1199 #endif /* CONFIG_WPS */
1201 if (hapd
->conf
->wpa
) {
1202 ret
= os_snprintf(pos
, end
- pos
, "wpa=%d\n", hapd
->conf
->wpa
);
1203 if (os_snprintf_error(end
- pos
, ret
))
1208 if (hapd
->conf
->wpa
&& hapd
->conf
->wpa_key_mgmt
) {
1209 ret
= os_snprintf(pos
, end
- pos
, "key_mgmt=");
1210 if (os_snprintf_error(end
- pos
, ret
))
1214 pos
+= hostapd_ctrl_iface_get_key_mgmt(hapd
, pos
, end
- pos
);
1216 ret
= os_snprintf(pos
, end
- pos
, "\n");
1217 if (os_snprintf_error(end
- pos
, ret
))
1222 if (hapd
->conf
->wpa
) {
1223 ret
= os_snprintf(pos
, end
- pos
, "group_cipher=%s\n",
1224 wpa_cipher_txt(hapd
->conf
->wpa_group
));
1225 if (os_snprintf_error(end
- pos
, ret
))
1230 if ((hapd
->conf
->wpa
& WPA_PROTO_RSN
) && hapd
->conf
->rsn_pairwise
) {
1231 ret
= os_snprintf(pos
, end
- pos
, "rsn_pairwise_cipher=");
1232 if (os_snprintf_error(end
- pos
, ret
))
1236 ret
= wpa_write_ciphers(pos
, end
, hapd
->conf
->rsn_pairwise
,
1242 ret
= os_snprintf(pos
, end
- pos
, "\n");
1243 if (os_snprintf_error(end
- pos
, ret
))
1248 if ((hapd
->conf
->wpa
& WPA_PROTO_WPA
) && hapd
->conf
->wpa_pairwise
) {
1249 ret
= os_snprintf(pos
, end
- pos
, "wpa_pairwise_cipher=");
1250 if (os_snprintf_error(end
- pos
, ret
))
1254 ret
= wpa_write_ciphers(pos
, end
, hapd
->conf
->wpa_pairwise
,
1260 ret
= os_snprintf(pos
, end
- pos
, "\n");
1261 if (os_snprintf_error(end
- pos
, ret
))
1270 static int hostapd_ctrl_iface_set(struct hostapd_data
*hapd
, char *cmd
)
1275 value
= os_strchr(cmd
, ' ');
1280 wpa_printf(MSG_DEBUG
, "CTRL_IFACE SET '%s'='%s'", cmd
, value
);
1282 #ifdef CONFIG_WPS_TESTING
1283 } else if (os_strcasecmp(cmd
, "wps_version_number") == 0) {
1285 val
= strtol(value
, NULL
, 0);
1286 if (val
< 0 || val
> 0xff) {
1288 wpa_printf(MSG_DEBUG
, "WPS: Invalid "
1289 "wps_version_number %ld", val
);
1291 wps_version_number
= val
;
1292 wpa_printf(MSG_DEBUG
, "WPS: Testing - force WPS "
1294 (wps_version_number
& 0xf0) >> 4,
1295 wps_version_number
& 0x0f);
1296 hostapd_wps_update_ie(hapd
);
1298 } else if (os_strcasecmp(cmd
, "wps_testing_dummy_cred") == 0) {
1299 wps_testing_dummy_cred
= atoi(value
);
1300 wpa_printf(MSG_DEBUG
, "WPS: Testing - dummy_cred=%d",
1301 wps_testing_dummy_cred
);
1302 } else if (os_strcasecmp(cmd
, "wps_corrupt_pkhash") == 0) {
1303 wps_corrupt_pkhash
= atoi(value
);
1304 wpa_printf(MSG_DEBUG
, "WPS: Testing - wps_corrupt_pkhash=%d",
1305 wps_corrupt_pkhash
);
1306 #endif /* CONFIG_WPS_TESTING */
1307 #ifdef CONFIG_INTERWORKING
1308 } else if (os_strcasecmp(cmd
, "gas_frag_limit") == 0) {
1309 int val
= atoi(value
);
1313 hapd
->gas_frag_limit
= val
;
1314 #endif /* CONFIG_INTERWORKING */
1315 #ifdef CONFIG_TESTING_OPTIONS
1316 } else if (os_strcasecmp(cmd
, "ext_mgmt_frame_handling") == 0) {
1317 hapd
->ext_mgmt_frame_handling
= atoi(value
);
1318 } else if (os_strcasecmp(cmd
, "ext_eapol_frame_io") == 0) {
1319 hapd
->ext_eapol_frame_io
= atoi(value
);
1320 #endif /* CONFIG_TESTING_OPTIONS */
1322 struct sta_info
*sta
;
1325 ret
= hostapd_set_iface(hapd
->iconf
, hapd
->conf
, cmd
, value
);
1329 if (os_strcasecmp(cmd
, "deny_mac_file") == 0) {
1330 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
1331 if (hostapd_maclist_found(
1332 hapd
->conf
->deny_mac
,
1333 hapd
->conf
->num_deny_mac
, sta
->addr
,
1335 (!vlan_id
|| vlan_id
== sta
->vlan_id
))
1337 hapd
, sta
, sta
->addr
,
1338 WLAN_REASON_UNSPECIFIED
);
1340 } else if (hapd
->conf
->macaddr_acl
== DENY_UNLESS_ACCEPTED
&&
1341 os_strcasecmp(cmd
, "accept_mac_file") == 0) {
1342 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
1343 if (!hostapd_maclist_found(
1344 hapd
->conf
->accept_mac
,
1345 hapd
->conf
->num_accept_mac
,
1346 sta
->addr
, &vlan_id
) ||
1347 (vlan_id
&& vlan_id
!= sta
->vlan_id
))
1349 hapd
, sta
, sta
->addr
,
1350 WLAN_REASON_UNSPECIFIED
);
1359 static int hostapd_ctrl_iface_get(struct hostapd_data
*hapd
, char *cmd
,
1360 char *buf
, size_t buflen
)
1364 wpa_printf(MSG_DEBUG
, "CTRL_IFACE GET '%s'", cmd
);
1366 if (os_strcmp(cmd
, "version") == 0) {
1367 res
= os_snprintf(buf
, buflen
, "%s", VERSION_STR
);
1368 if (os_snprintf_error(buflen
, res
))
1371 } else if (os_strcmp(cmd
, "tls_library") == 0) {
1372 res
= tls_get_library_version(buf
, buflen
);
1373 if (os_snprintf_error(buflen
, res
))
1382 static int hostapd_ctrl_iface_enable(struct hostapd_iface
*iface
)
1384 if (hostapd_enable_iface(iface
) < 0) {
1385 wpa_printf(MSG_ERROR
, "Enabling of interface failed");
1392 static int hostapd_ctrl_iface_reload(struct hostapd_iface
*iface
)
1394 if (hostapd_reload_iface(iface
) < 0) {
1395 wpa_printf(MSG_ERROR
, "Reloading of interface failed");
1402 static int hostapd_ctrl_iface_disable(struct hostapd_iface
*iface
)
1404 if (hostapd_disable_iface(iface
) < 0) {
1405 wpa_printf(MSG_ERROR
, "Disabling of interface failed");
1412 #ifdef CONFIG_TESTING_OPTIONS
1414 static int hostapd_ctrl_iface_radar(struct hostapd_data
*hapd
, char *cmd
)
1416 union wpa_event_data data
;
1418 enum wpa_event_type event
;
1420 wpa_printf(MSG_DEBUG
, "RADAR TEST: %s", cmd
);
1422 os_memset(&data
, 0, sizeof(data
));
1424 param
= os_strchr(cmd
, ' ');
1429 if (os_strcmp(cmd
, "DETECTED") == 0)
1430 event
= EVENT_DFS_RADAR_DETECTED
;
1431 else if (os_strcmp(cmd
, "CAC-FINISHED") == 0)
1432 event
= EVENT_DFS_CAC_FINISHED
;
1433 else if (os_strcmp(cmd
, "CAC-ABORTED") == 0)
1434 event
= EVENT_DFS_CAC_ABORTED
;
1435 else if (os_strcmp(cmd
, "NOP-FINISHED") == 0)
1436 event
= EVENT_DFS_NOP_FINISHED
;
1438 wpa_printf(MSG_DEBUG
, "Unsupported RADAR test command: %s",
1443 pos
= os_strstr(param
, "freq=");
1445 data
.dfs_event
.freq
= atoi(pos
+ 5);
1447 pos
= os_strstr(param
, "ht_enabled=1");
1449 data
.dfs_event
.ht_enabled
= 1;
1451 pos
= os_strstr(param
, "chan_offset=");
1453 data
.dfs_event
.chan_offset
= atoi(pos
+ 12);
1455 pos
= os_strstr(param
, "chan_width=");
1457 data
.dfs_event
.chan_width
= atoi(pos
+ 11);
1459 pos
= os_strstr(param
, "cf1=");
1461 data
.dfs_event
.cf1
= atoi(pos
+ 4);
1463 pos
= os_strstr(param
, "cf2=");
1465 data
.dfs_event
.cf2
= atoi(pos
+ 4);
1467 wpa_supplicant_event(hapd
, event
, &data
);
1473 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data
*hapd
, char *cmd
)
1479 wpa_printf(MSG_DEBUG
, "External MGMT TX: %s", cmd
);
1481 len
= os_strlen(cmd
);
1486 buf
= os_malloc(len
);
1490 if (hexstr2bin(cmd
, buf
, len
) < 0) {
1495 res
= hostapd_drv_send_mlme(hapd
, buf
, len
, 0);
1501 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data
*hapd
, char *cmd
)
1504 u8 src
[ETH_ALEN
], *buf
;
1508 wpa_printf(MSG_DEBUG
, "External EAPOL RX: %s", cmd
);
1511 used
= hwaddr_aton2(pos
, src
);
1518 len
= os_strlen(pos
);
1523 buf
= os_malloc(len
);
1527 if (hexstr2bin(pos
, buf
, len
) < 0) {
1532 ieee802_1x_receive(hapd
, src
, buf
, len
);
1539 static u16
ipv4_hdr_checksum(const void *buf
, size_t len
)
1543 const u16
*pos
= buf
;
1545 for (i
= 0; i
< len
/ 2; i
++)
1549 sum
= (sum
& 0xffff) + (sum
>> 16);
1551 return sum
^ 0xffff;
1555 #define HWSIM_PACKETLEN 1500
1556 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1558 void hostapd_data_test_rx(void *ctx
, const u8
*src_addr
, const u8
*buf
,
1561 struct hostapd_data
*hapd
= ctx
;
1562 const struct ether_header
*eth
;
1567 if (len
!= HWSIM_PACKETLEN
)
1570 eth
= (const struct ether_header
*) buf
;
1571 os_memcpy(&ip
, eth
+ 1, sizeof(ip
));
1572 pos
= &buf
[sizeof(*eth
) + sizeof(ip
)];
1574 if (ip
.ihl
!= 5 || ip
.version
!= 4 ||
1575 ntohs(ip
.tot_len
) != HWSIM_IP_LEN
)
1578 for (i
= 0; i
< HWSIM_IP_LEN
- sizeof(ip
); i
++) {
1584 wpa_msg(hapd
->msg_ctx
, MSG_INFO
, "DATA-TEST-RX " MACSTR
" " MACSTR
,
1585 MAC2STR(eth
->ether_dhost
), MAC2STR(eth
->ether_shost
));
1589 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data
*hapd
,
1592 int enabled
= atoi(cmd
);
1597 if (hapd
->l2_test
) {
1598 l2_packet_deinit(hapd
->l2_test
);
1599 hapd
->l2_test
= NULL
;
1600 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
,
1601 "test data: Disabled");
1609 pos
= os_strstr(cmd
, " ifname=");
1613 ifname
= hapd
->conf
->iface
;
1615 hapd
->l2_test
= l2_packet_init(ifname
, hapd
->own_addr
,
1616 ETHERTYPE_IP
, hostapd_data_test_rx
,
1618 if (hapd
->l2_test
== NULL
)
1621 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: Enabled");
1627 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data
*hapd
, char *cmd
)
1629 u8 dst
[ETH_ALEN
], src
[ETH_ALEN
];
1634 u8 buf
[2 + HWSIM_PACKETLEN
];
1635 struct ether_header
*eth
;
1640 if (hapd
->l2_test
== NULL
)
1643 /* format: <dst> <src> <tos> */
1646 used
= hwaddr_aton2(pos
, dst
);
1652 used
= hwaddr_aton2(pos
, src
);
1657 val
= strtol(pos
, NULL
, 0);
1658 if (val
< 0 || val
> 0xff)
1662 eth
= (struct ether_header
*) &buf
[2];
1663 os_memcpy(eth
->ether_dhost
, dst
, ETH_ALEN
);
1664 os_memcpy(eth
->ether_shost
, src
, ETH_ALEN
);
1665 eth
->ether_type
= htons(ETHERTYPE_IP
);
1666 ip
= (struct iphdr
*) (eth
+ 1);
1667 os_memset(ip
, 0, sizeof(*ip
));
1672 ip
->tot_len
= htons(HWSIM_IP_LEN
);
1674 ip
->saddr
= htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1675 ip
->daddr
= htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1676 ip
->check
= ipv4_hdr_checksum(ip
, sizeof(*ip
));
1677 dpos
= (u8
*) (ip
+ 1);
1678 for (i
= 0; i
< HWSIM_IP_LEN
- sizeof(*ip
); i
++)
1681 if (l2_packet_send(hapd
->l2_test
, dst
, ETHERTYPE_IP
, &buf
[2],
1682 HWSIM_PACKETLEN
) < 0)
1685 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: TX dst=" MACSTR
1686 " src=" MACSTR
" tos=0x%x", MAC2STR(dst
), MAC2STR(src
), tos
);
1692 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data
*hapd
,
1696 struct ether_header
*eth
;
1697 struct l2_packet_data
*l2
= NULL
;
1701 const char *ifname
= hapd
->conf
->iface
;
1703 if (os_strncmp(cmd
, "ifname=", 7) == 0) {
1706 cmd
= os_strchr(cmd
, ' ');
1712 len
= os_strlen(cmd
);
1713 if (len
& 1 || len
< ETH_HLEN
* 2)
1717 buf
= os_malloc(len
);
1721 if (hexstr2bin(cmd
, buf
, len
) < 0)
1724 eth
= (struct ether_header
*) buf
;
1725 ethertype
= ntohs(eth
->ether_type
);
1727 l2
= l2_packet_init(ifname
, hapd
->own_addr
, ethertype
,
1728 hostapd_data_test_rx
, hapd
, 1);
1732 res
= l2_packet_send(l2
, eth
->ether_dhost
, ethertype
, buf
, len
);
1733 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: TX frame res=%d", res
);
1736 l2_packet_deinit(l2
);
1739 return res
< 0 ? -1 : 0;
1743 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data
*hapd
, char *cmd
)
1745 #ifdef WPA_TRACE_BFD
1746 extern char wpa_trace_fail_func
[256];
1747 extern unsigned int wpa_trace_fail_after
;
1750 wpa_trace_fail_after
= atoi(cmd
);
1751 pos
= os_strchr(cmd
, ':');
1754 os_strlcpy(wpa_trace_fail_func
, pos
,
1755 sizeof(wpa_trace_fail_func
));
1757 wpa_trace_fail_after
= 0;
1761 #else /* WPA_TRACE_BFD */
1763 #endif /* WPA_TRACE_BFD */
1767 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data
*hapd
,
1768 char *buf
, size_t buflen
)
1770 #ifdef WPA_TRACE_BFD
1771 extern char wpa_trace_fail_func
[256];
1772 extern unsigned int wpa_trace_fail_after
;
1774 return os_snprintf(buf
, buflen
, "%u:%s", wpa_trace_fail_after
,
1775 wpa_trace_fail_func
);
1776 #else /* WPA_TRACE_BFD */
1778 #endif /* WPA_TRACE_BFD */
1782 static int hostapd_ctrl_test_fail(struct hostapd_data
*hapd
, char *cmd
)
1784 #ifdef WPA_TRACE_BFD
1785 extern char wpa_trace_test_fail_func
[256];
1786 extern unsigned int wpa_trace_test_fail_after
;
1789 wpa_trace_test_fail_after
= atoi(cmd
);
1790 pos
= os_strchr(cmd
, ':');
1793 os_strlcpy(wpa_trace_test_fail_func
, pos
,
1794 sizeof(wpa_trace_test_fail_func
));
1796 wpa_trace_test_fail_after
= 0;
1800 #else /* WPA_TRACE_BFD */
1802 #endif /* WPA_TRACE_BFD */
1806 static int hostapd_ctrl_get_fail(struct hostapd_data
*hapd
,
1807 char *buf
, size_t buflen
)
1809 #ifdef WPA_TRACE_BFD
1810 extern char wpa_trace_test_fail_func
[256];
1811 extern unsigned int wpa_trace_test_fail_after
;
1813 return os_snprintf(buf
, buflen
, "%u:%s", wpa_trace_test_fail_after
,
1814 wpa_trace_test_fail_func
);
1815 #else /* WPA_TRACE_BFD */
1817 #endif /* WPA_TRACE_BFD */
1820 #endif /* CONFIG_TESTING_OPTIONS */
1823 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface
*iface
,
1827 struct csa_settings settings
;
1831 ret
= hostapd_parse_csa_settings(pos
, &settings
);
1835 for (i
= 0; i
< iface
->num_bss
; i
++) {
1836 ret
= hostapd_switch_channel(iface
->bss
[i
], &settings
);
1838 /* FIX: What do we do if CSA fails in the middle of
1839 * submitting multi-BSS CSA requests? */
1845 #else /* NEED_AP_MLME */
1847 #endif /* NEED_AP_MLME */
1851 static int hostapd_ctrl_iface_mib(struct hostapd_data
*hapd
, char *reply
,
1852 int reply_size
, const char *param
)
1854 #ifdef RADIUS_SERVER
1855 if (os_strcmp(param
, "radius_server") == 0) {
1856 return radius_server_get_mib(hapd
->radius_srv
, reply
,
1859 #endif /* RADIUS_SERVER */
1864 static int hostapd_ctrl_iface_vendor(struct hostapd_data
*hapd
, char *cmd
,
1865 char *buf
, size_t buflen
)
1870 unsigned int vendor_id
, subcmd
;
1871 struct wpabuf
*reply
;
1872 size_t data_len
= 0;
1874 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1875 vendor_id
= strtoul(cmd
, &pos
, 16);
1879 subcmd
= strtoul(pos
, &pos
, 10);
1882 if (!isblank(*pos
++))
1884 data_len
= os_strlen(pos
);
1889 data
= os_malloc(data_len
);
1893 if (hexstr2bin(pos
, data
, data_len
)) {
1894 wpa_printf(MSG_DEBUG
,
1895 "Vendor command: wrong parameter format");
1901 reply
= wpabuf_alloc((buflen
- 1) / 2);
1907 ret
= hostapd_drv_vendor_cmd(hapd
, vendor_id
, subcmd
, data
, data_len
,
1911 ret
= wpa_snprintf_hex(buf
, buflen
, wpabuf_head_u8(reply
),
1921 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data
*hapd
,
1925 struct sta_info
*sta
;
1927 if (hwaddr_aton(cmd
, addr
))
1930 sta
= ap_get_sta(hapd
, addr
);
1931 if (!sta
|| !sta
->eapol_sm
)
1934 eapol_auth_reauthenticate(sta
->eapol_sm
);
1939 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data
*hapd
, char *cmd
)
1942 struct sta_info
*sta
;
1943 char *pos
= cmd
, *param
;
1945 if (hwaddr_aton(pos
, addr
) || pos
[17] != ' ')
1949 pos
= os_strchr(pos
, ' ');
1954 sta
= ap_get_sta(hapd
, addr
);
1955 if (!sta
|| !sta
->eapol_sm
)
1958 return eapol_auth_set_conf(sta
->eapol_sm
, param
, pos
);
1962 static int hostapd_ctrl_iface_receive_process(struct hostapd_data
*hapd
,
1963 char *buf
, char *reply
,
1965 struct sockaddr_un
*from
,
1970 os_memcpy(reply
, "OK\n", 3);
1973 if (os_strcmp(buf
, "PING") == 0) {
1974 os_memcpy(reply
, "PONG\n", 5);
1976 } else if (os_strncmp(buf
, "RELOG", 5) == 0) {
1977 if (wpa_debug_reopen_file() < 0)
1979 } else if (os_strcmp(buf
, "STATUS") == 0) {
1980 reply_len
= hostapd_ctrl_iface_status(hapd
, reply
,
1982 } else if (os_strcmp(buf
, "STATUS-DRIVER") == 0) {
1983 reply_len
= hostapd_drv_status(hapd
, reply
, reply_size
);
1984 } else if (os_strcmp(buf
, "MIB") == 0) {
1985 reply_len
= ieee802_11_get_mib(hapd
, reply
, reply_size
);
1986 if (reply_len
>= 0) {
1987 res
= wpa_get_mib(hapd
->wpa_auth
, reply
+ reply_len
,
1988 reply_size
- reply_len
);
1994 if (reply_len
>= 0) {
1995 res
= ieee802_1x_get_mib(hapd
, reply
+ reply_len
,
1996 reply_size
- reply_len
);
2002 #ifndef CONFIG_NO_RADIUS
2003 if (reply_len
>= 0) {
2004 res
= radius_client_get_mib(hapd
->radius
,
2006 reply_size
- reply_len
);
2012 #endif /* CONFIG_NO_RADIUS */
2013 } else if (os_strncmp(buf
, "MIB ", 4) == 0) {
2014 reply_len
= hostapd_ctrl_iface_mib(hapd
, reply
, reply_size
,
2016 } else if (os_strcmp(buf
, "STA-FIRST") == 0) {
2017 reply_len
= hostapd_ctrl_iface_sta_first(hapd
, reply
,
2019 } else if (os_strncmp(buf
, "STA ", 4) == 0) {
2020 reply_len
= hostapd_ctrl_iface_sta(hapd
, buf
+ 4, reply
,
2022 } else if (os_strncmp(buf
, "STA-NEXT ", 9) == 0) {
2023 reply_len
= hostapd_ctrl_iface_sta_next(hapd
, buf
+ 9, reply
,
2025 } else if (os_strcmp(buf
, "ATTACH") == 0) {
2026 if (hostapd_ctrl_iface_attach(hapd
, from
, fromlen
))
2028 } else if (os_strcmp(buf
, "DETACH") == 0) {
2029 if (hostapd_ctrl_iface_detach(hapd
, from
, fromlen
))
2031 } else if (os_strncmp(buf
, "LEVEL ", 6) == 0) {
2032 if (hostapd_ctrl_iface_level(hapd
, from
, fromlen
,
2035 } else if (os_strncmp(buf
, "NEW_STA ", 8) == 0) {
2036 if (hostapd_ctrl_iface_new_sta(hapd
, buf
+ 8))
2038 } else if (os_strncmp(buf
, "DEAUTHENTICATE ", 15) == 0) {
2039 if (hostapd_ctrl_iface_deauthenticate(hapd
, buf
+ 15))
2041 } else if (os_strncmp(buf
, "DISASSOCIATE ", 13) == 0) {
2042 if (hostapd_ctrl_iface_disassociate(hapd
, buf
+ 13))
2044 } else if (os_strcmp(buf
, "STOP_AP") == 0) {
2045 if (hostapd_ctrl_iface_stop_ap(hapd
))
2047 #ifdef CONFIG_IEEE80211W
2049 } else if (os_strncmp(buf
, "SA_QUERY ", 9) == 0) {
2050 if (hostapd_ctrl_iface_sa_query(hapd
, buf
+ 9))
2052 #endif /* NEED_AP_MLME */
2053 #endif /* CONFIG_IEEE80211W */
2055 } else if (os_strncmp(buf
, "WPS_PIN ", 8) == 0) {
2056 if (hostapd_ctrl_iface_wps_pin(hapd
, buf
+ 8))
2058 } else if (os_strncmp(buf
, "WPS_CHECK_PIN ", 14) == 0) {
2059 reply_len
= hostapd_ctrl_iface_wps_check_pin(
2060 hapd
, buf
+ 14, reply
, reply_size
);
2061 } else if (os_strcmp(buf
, "WPS_PBC") == 0) {
2062 if (hostapd_wps_button_pushed(hapd
, NULL
))
2064 } else if (os_strcmp(buf
, "WPS_CANCEL") == 0) {
2065 if (hostapd_wps_cancel(hapd
))
2067 } else if (os_strncmp(buf
, "WPS_AP_PIN ", 11) == 0) {
2068 reply_len
= hostapd_ctrl_iface_wps_ap_pin(hapd
, buf
+ 11,
2070 } else if (os_strncmp(buf
, "WPS_CONFIG ", 11) == 0) {
2071 if (hostapd_ctrl_iface_wps_config(hapd
, buf
+ 11) < 0)
2073 } else if (os_strncmp(buf
, "WPS_GET_STATUS", 13) == 0) {
2074 reply_len
= hostapd_ctrl_iface_wps_get_status(hapd
, reply
,
2076 #ifdef CONFIG_WPS_NFC
2077 } else if (os_strncmp(buf
, "WPS_NFC_TAG_READ ", 17) == 0) {
2078 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd
, buf
+ 17))
2080 } else if (os_strncmp(buf
, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2081 reply_len
= hostapd_ctrl_iface_wps_nfc_config_token(
2082 hapd
, buf
+ 21, reply
, reply_size
);
2083 } else if (os_strncmp(buf
, "WPS_NFC_TOKEN ", 14) == 0) {
2084 reply_len
= hostapd_ctrl_iface_wps_nfc_token(
2085 hapd
, buf
+ 14, reply
, reply_size
);
2086 } else if (os_strncmp(buf
, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2087 reply_len
= hostapd_ctrl_iface_nfc_get_handover_sel(
2088 hapd
, buf
+ 21, reply
, reply_size
);
2089 } else if (os_strncmp(buf
, "NFC_REPORT_HANDOVER ", 20) == 0) {
2090 if (hostapd_ctrl_iface_nfc_report_handover(hapd
, buf
+ 20))
2092 #endif /* CONFIG_WPS_NFC */
2093 #endif /* CONFIG_WPS */
2094 #ifdef CONFIG_INTERWORKING
2095 } else if (os_strncmp(buf
, "SET_QOS_MAP_SET ", 16) == 0) {
2096 if (hostapd_ctrl_iface_set_qos_map_set(hapd
, buf
+ 16))
2098 } else if (os_strncmp(buf
, "SEND_QOS_MAP_CONF ", 18) == 0) {
2099 if (hostapd_ctrl_iface_send_qos_map_conf(hapd
, buf
+ 18))
2101 #endif /* CONFIG_INTERWORKING */
2103 } else if (os_strncmp(buf
, "HS20_WNM_NOTIF ", 15) == 0) {
2104 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd
, buf
+ 15))
2106 } else if (os_strncmp(buf
, "HS20_DEAUTH_REQ ", 16) == 0) {
2107 if (hostapd_ctrl_iface_hs20_deauth_req(hapd
, buf
+ 16))
2109 #endif /* CONFIG_HS20 */
2111 } else if (os_strncmp(buf
, "DISASSOC_IMMINENT ", 18) == 0) {
2112 if (hostapd_ctrl_iface_disassoc_imminent(hapd
, buf
+ 18))
2114 } else if (os_strncmp(buf
, "ESS_DISASSOC ", 13) == 0) {
2115 if (hostapd_ctrl_iface_ess_disassoc(hapd
, buf
+ 13))
2117 } else if (os_strncmp(buf
, "BSS_TM_REQ ", 11) == 0) {
2118 if (hostapd_ctrl_iface_bss_tm_req(hapd
, buf
+ 11))
2120 #endif /* CONFIG_WNM */
2121 } else if (os_strcmp(buf
, "GET_CONFIG") == 0) {
2122 reply_len
= hostapd_ctrl_iface_get_config(hapd
, reply
,
2124 } else if (os_strncmp(buf
, "SET ", 4) == 0) {
2125 if (hostapd_ctrl_iface_set(hapd
, buf
+ 4))
2127 } else if (os_strncmp(buf
, "GET ", 4) == 0) {
2128 reply_len
= hostapd_ctrl_iface_get(hapd
, buf
+ 4, reply
,
2130 } else if (os_strncmp(buf
, "ENABLE", 6) == 0) {
2131 if (hostapd_ctrl_iface_enable(hapd
->iface
))
2133 } else if (os_strncmp(buf
, "RELOAD", 6) == 0) {
2134 if (hostapd_ctrl_iface_reload(hapd
->iface
))
2136 } else if (os_strncmp(buf
, "DISABLE", 7) == 0) {
2137 if (hostapd_ctrl_iface_disable(hapd
->iface
))
2139 } else if (os_strcmp(buf
, "UPDATE_BEACON") == 0) {
2140 if (ieee802_11_set_beacon(hapd
))
2142 #ifdef CONFIG_TESTING_OPTIONS
2143 } else if (os_strncmp(buf
, "RADAR ", 6) == 0) {
2144 if (hostapd_ctrl_iface_radar(hapd
, buf
+ 6))
2146 } else if (os_strncmp(buf
, "MGMT_TX ", 8) == 0) {
2147 if (hostapd_ctrl_iface_mgmt_tx(hapd
, buf
+ 8))
2149 } else if (os_strncmp(buf
, "EAPOL_RX ", 9) == 0) {
2150 if (hostapd_ctrl_iface_eapol_rx(hapd
, buf
+ 9) < 0)
2152 } else if (os_strncmp(buf
, "DATA_TEST_CONFIG ", 17) == 0) {
2153 if (hostapd_ctrl_iface_data_test_config(hapd
, buf
+ 17) < 0)
2155 } else if (os_strncmp(buf
, "DATA_TEST_TX ", 13) == 0) {
2156 if (hostapd_ctrl_iface_data_test_tx(hapd
, buf
+ 13) < 0)
2158 } else if (os_strncmp(buf
, "DATA_TEST_FRAME ", 16) == 0) {
2159 if (hostapd_ctrl_iface_data_test_frame(hapd
, buf
+ 16) < 0)
2161 } else if (os_strncmp(buf
, "TEST_ALLOC_FAIL ", 16) == 0) {
2162 if (hostapd_ctrl_test_alloc_fail(hapd
, buf
+ 16) < 0)
2164 } else if (os_strcmp(buf
, "GET_ALLOC_FAIL") == 0) {
2165 reply_len
= hostapd_ctrl_get_alloc_fail(hapd
, reply
,
2167 } else if (os_strncmp(buf
, "TEST_FAIL ", 10) == 0) {
2168 if (hostapd_ctrl_test_fail(hapd
, buf
+ 10) < 0)
2170 } else if (os_strcmp(buf
, "GET_FAIL") == 0) {
2171 reply_len
= hostapd_ctrl_get_fail(hapd
, reply
, reply_size
);
2172 #endif /* CONFIG_TESTING_OPTIONS */
2173 } else if (os_strncmp(buf
, "CHAN_SWITCH ", 12) == 0) {
2174 if (hostapd_ctrl_iface_chan_switch(hapd
->iface
, buf
+ 12))
2176 } else if (os_strncmp(buf
, "VENDOR ", 7) == 0) {
2177 reply_len
= hostapd_ctrl_iface_vendor(hapd
, buf
+ 7, reply
,
2179 } else if (os_strcmp(buf
, "ERP_FLUSH") == 0) {
2180 ieee802_1x_erp_flush(hapd
);
2181 #ifdef RADIUS_SERVER
2182 radius_server_erp_flush(hapd
->radius_srv
);
2183 #endif /* RADIUS_SERVER */
2184 } else if (os_strncmp(buf
, "EAPOL_REAUTH ", 13) == 0) {
2185 if (hostapd_ctrl_iface_eapol_reauth(hapd
, buf
+ 13))
2187 } else if (os_strncmp(buf
, "EAPOL_SET ", 10) == 0) {
2188 if (hostapd_ctrl_iface_eapol_set(hapd
, buf
+ 10))
2191 os_memcpy(reply
, "UNKNOWN COMMAND\n", 16);
2195 if (reply_len
< 0) {
2196 os_memcpy(reply
, "FAIL\n", 5);
2204 static void hostapd_ctrl_iface_receive(int sock
, void *eloop_ctx
,
2207 struct hostapd_data
*hapd
= eloop_ctx
;
2210 struct sockaddr_un from
;
2211 socklen_t fromlen
= sizeof(from
);
2213 const int reply_size
= 4096;
2215 int level
= MSG_DEBUG
;
2217 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
2218 (struct sockaddr
*) &from
, &fromlen
);
2220 wpa_printf(MSG_ERROR
, "recvfrom(ctrl_iface): %s",
2225 if (os_strcmp(buf
, "PING") == 0)
2226 level
= MSG_EXCESSIVE
;
2227 wpa_hexdump_ascii(level
, "RX ctrl_iface", (u8
*) buf
, res
);
2229 reply
= os_malloc(reply_size
);
2230 if (reply
== NULL
) {
2231 if (sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
2233 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
2239 reply_len
= hostapd_ctrl_iface_receive_process(hapd
, buf
,
2243 if (sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
2245 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
2252 static char * hostapd_ctrl_iface_path(struct hostapd_data
*hapd
)
2257 if (hapd
->conf
->ctrl_interface
== NULL
)
2260 len
= os_strlen(hapd
->conf
->ctrl_interface
) +
2261 os_strlen(hapd
->conf
->iface
) + 2;
2262 buf
= os_malloc(len
);
2266 os_snprintf(buf
, len
, "%s/%s",
2267 hapd
->conf
->ctrl_interface
, hapd
->conf
->iface
);
2268 buf
[len
- 1] = '\0';
2273 static void hostapd_ctrl_iface_msg_cb(void *ctx
, int level
,
2274 enum wpa_msg_type type
,
2275 const char *txt
, size_t len
)
2277 struct hostapd_data
*hapd
= ctx
;
2280 hostapd_ctrl_iface_send(hapd
, level
, type
, txt
, len
);
2284 int hostapd_ctrl_iface_init(struct hostapd_data
*hapd
)
2286 struct sockaddr_un addr
;
2290 if (hapd
->ctrl_sock
> -1) {
2291 wpa_printf(MSG_DEBUG
, "ctrl_iface already exists!");
2295 if (hapd
->conf
->ctrl_interface
== NULL
)
2298 if (mkdir(hapd
->conf
->ctrl_interface
, S_IRWXU
| S_IRWXG
) < 0) {
2299 if (errno
== EEXIST
) {
2300 wpa_printf(MSG_DEBUG
, "Using existing control "
2301 "interface directory.");
2303 wpa_printf(MSG_ERROR
, "mkdir[ctrl_interface]: %s",
2309 if (hapd
->conf
->ctrl_interface_gid_set
&&
2310 chown(hapd
->conf
->ctrl_interface
, -1,
2311 hapd
->conf
->ctrl_interface_gid
) < 0) {
2312 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
2317 if (!hapd
->conf
->ctrl_interface_gid_set
&&
2318 hapd
->iface
->interfaces
->ctrl_iface_group
&&
2319 chown(hapd
->conf
->ctrl_interface
, -1,
2320 hapd
->iface
->interfaces
->ctrl_iface_group
) < 0) {
2321 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
2328 * Android is using umask 0077 which would leave the control interface
2329 * directory without group access. This breaks things since Wi-Fi
2330 * framework assumes that this directory can be accessed by other
2331 * applications in the wifi group. Fix this by adding group access even
2332 * if umask value would prevent this.
2334 if (chmod(hapd
->conf
->ctrl_interface
, S_IRWXU
| S_IRWXG
) < 0) {
2335 wpa_printf(MSG_ERROR
, "CTRL: Could not chmod directory: %s",
2337 /* Try to continue anyway */
2339 #endif /* ANDROID */
2341 if (os_strlen(hapd
->conf
->ctrl_interface
) + 1 +
2342 os_strlen(hapd
->conf
->iface
) >= sizeof(addr
.sun_path
))
2345 s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
2347 wpa_printf(MSG_ERROR
, "socket(PF_UNIX): %s", strerror(errno
));
2351 os_memset(&addr
, 0, sizeof(addr
));
2353 addr
.sun_len
= sizeof(addr
);
2354 #endif /* __FreeBSD__ */
2355 addr
.sun_family
= AF_UNIX
;
2356 fname
= hostapd_ctrl_iface_path(hapd
);
2359 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
2360 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
2361 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
2363 if (connect(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
2364 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
2365 " allow connections - assuming it was left"
2366 "over from forced program termination");
2367 if (unlink(fname
) < 0) {
2368 wpa_printf(MSG_ERROR
,
2369 "Could not unlink existing ctrl_iface socket '%s': %s",
2370 fname
, strerror(errno
));
2373 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) <
2375 wpa_printf(MSG_ERROR
,
2376 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
2380 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
2381 "ctrl_iface socket '%s'", fname
);
2383 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
2384 "be in use - cannot override it");
2385 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
2386 "not used anymore", fname
);
2393 if (hapd
->conf
->ctrl_interface_gid_set
&&
2394 chown(fname
, -1, hapd
->conf
->ctrl_interface_gid
) < 0) {
2395 wpa_printf(MSG_ERROR
, "chown[ctrl_interface/ifname]: %s",
2400 if (!hapd
->conf
->ctrl_interface_gid_set
&&
2401 hapd
->iface
->interfaces
->ctrl_iface_group
&&
2402 chown(fname
, -1, hapd
->iface
->interfaces
->ctrl_iface_group
) < 0) {
2403 wpa_printf(MSG_ERROR
, "chown[ctrl_interface/ifname]: %s",
2408 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
2409 wpa_printf(MSG_ERROR
, "chmod[ctrl_interface/ifname]: %s",
2415 hapd
->ctrl_sock
= s
;
2416 if (eloop_register_read_sock(s
, hostapd_ctrl_iface_receive
, hapd
,
2418 hostapd_ctrl_iface_deinit(hapd
);
2421 hapd
->msg_ctx
= hapd
;
2422 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb
);
2437 void hostapd_ctrl_iface_deinit(struct hostapd_data
*hapd
)
2439 struct wpa_ctrl_dst
*dst
, *prev
;
2441 if (hapd
->ctrl_sock
> -1) {
2443 eloop_unregister_read_sock(hapd
->ctrl_sock
);
2444 close(hapd
->ctrl_sock
);
2445 hapd
->ctrl_sock
= -1;
2446 fname
= hostapd_ctrl_iface_path(hapd
);
2451 if (hapd
->conf
->ctrl_interface
&&
2452 rmdir(hapd
->conf
->ctrl_interface
) < 0) {
2453 if (errno
== ENOTEMPTY
) {
2454 wpa_printf(MSG_DEBUG
, "Control interface "
2455 "directory not empty - leaving it "
2458 wpa_printf(MSG_ERROR
,
2459 "rmdir[ctrl_interface=%s]: %s",
2460 hapd
->conf
->ctrl_interface
,
2466 dst
= hapd
->ctrl_dst
;
2467 hapd
->ctrl_dst
= NULL
;
2474 #ifdef CONFIG_TESTING_OPTIONS
2475 l2_packet_deinit(hapd
->l2_test
);
2476 hapd
->l2_test
= NULL
;
2477 #endif /* CONFIG_TESTING_OPTIONS */
2481 static int hostapd_ctrl_iface_add(struct hapd_interfaces
*interfaces
,
2484 if (hostapd_add_iface(interfaces
, buf
) < 0) {
2485 wpa_printf(MSG_ERROR
, "Adding interface %s failed", buf
);
2492 static int hostapd_ctrl_iface_remove(struct hapd_interfaces
*interfaces
,
2495 if (hostapd_remove_iface(interfaces
, buf
) < 0) {
2496 wpa_printf(MSG_ERROR
, "Removing interface %s failed", buf
);
2503 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces
*interfaces
,
2504 struct sockaddr_un
*from
,
2507 struct wpa_ctrl_dst
*dst
;
2509 dst
= os_zalloc(sizeof(*dst
));
2512 os_memcpy(&dst
->addr
, from
, sizeof(struct sockaddr_un
));
2513 dst
->addrlen
= fromlen
;
2514 dst
->debug_level
= MSG_INFO
;
2515 dst
->next
= interfaces
->global_ctrl_dst
;
2516 interfaces
->global_ctrl_dst
= dst
;
2517 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor attached (global)",
2519 fromlen
- offsetof(struct sockaddr_un
, sun_path
));
2524 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces
*interfaces
,
2525 struct sockaddr_un
*from
,
2528 struct wpa_ctrl_dst
*dst
, *prev
= NULL
;
2530 dst
= interfaces
->global_ctrl_dst
;
2532 if (fromlen
== dst
->addrlen
&&
2533 os_memcmp(from
->sun_path
, dst
->addr
.sun_path
,
2534 fromlen
- offsetof(struct sockaddr_un
, sun_path
))
2536 wpa_hexdump(MSG_DEBUG
,
2537 "CTRL_IFACE monitor detached (global)",
2540 offsetof(struct sockaddr_un
, sun_path
));
2542 interfaces
->global_ctrl_dst
= dst
->next
;
2544 prev
->next
= dst
->next
;
2555 static void hostapd_ctrl_iface_flush(struct hapd_interfaces
*interfaces
)
2557 #ifdef CONFIG_WPS_TESTING
2558 wps_version_number
= 0x20;
2559 wps_testing_dummy_cred
= 0;
2560 wps_corrupt_pkhash
= 0;
2561 #endif /* CONFIG_WPS_TESTING */
2568 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces
*interfaces
,
2571 char ifname
[IFNAMSIZ
+ 1];
2572 struct fst_iface_cfg cfg
;
2573 struct hostapd_data
*hapd
;
2574 struct fst_wpa_obj iface_obj
;
2576 if (!fst_parse_attach_command(cmd
, ifname
, sizeof(ifname
), &cfg
)) {
2577 hapd
= hostapd_get_iface(interfaces
, ifname
);
2579 fst_hostapd_fill_iface_obj(hapd
, &iface_obj
);
2580 hapd
->iface
->fst
= fst_attach(ifname
, hapd
->own_addr
,
2582 if (hapd
->iface
->fst
)
2592 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces
*interfaces
,
2595 char ifname
[IFNAMSIZ
+ 1];
2596 struct hostapd_data
* hapd
;
2598 if (!fst_parse_detach_command(cmd
, ifname
, sizeof(ifname
))) {
2599 hapd
= hostapd_get_iface(interfaces
, ifname
);
2601 if (!fst_iface_detach(ifname
)) {
2602 hapd
->iface
->fst
= NULL
;
2603 hapd
->iface
->fst_ies
= NULL
;
2612 #endif /* CONFIG_FST */
2615 static struct hostapd_data
*
2616 hostapd_interfaces_get_hapd(struct hapd_interfaces
*interfaces
,
2621 for (i
= 0; i
< interfaces
->count
; i
++) {
2622 struct hostapd_iface
*iface
= interfaces
->iface
[i
];
2624 for (j
= 0; j
< iface
->num_bss
; j
++) {
2625 struct hostapd_data
*hapd
;
2627 hapd
= iface
->bss
[j
];
2628 if (os_strcmp(ifname
, hapd
->conf
->iface
) == 0)
2637 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces
*interfaces
,
2639 char *buf
, char *reply
,
2641 struct sockaddr_un
*from
,
2644 struct hostapd_data
*hapd
;
2646 hapd
= hostapd_interfaces_get_hapd(interfaces
, ifname
);
2650 res
= os_snprintf(reply
, reply_size
, "FAIL-NO-IFNAME-MATCH\n");
2651 if (os_snprintf_error(reply_size
, res
))
2656 return hostapd_ctrl_iface_receive_process(hapd
, buf
, reply
,reply_size
,
2661 static void hostapd_global_ctrl_iface_receive(int sock
, void *eloop_ctx
,
2664 void *interfaces
= eloop_ctx
;
2667 struct sockaddr_un from
;
2668 socklen_t fromlen
= sizeof(from
);
2671 const int reply_size
= 4096;
2673 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
2674 (struct sockaddr
*) &from
, &fromlen
);
2676 wpa_printf(MSG_ERROR
, "recvfrom(ctrl_iface): %s",
2681 wpa_printf(MSG_DEBUG
, "Global ctrl_iface command: %s", buf
);
2683 reply
= os_malloc(reply_size
);
2684 if (reply
== NULL
) {
2685 if (sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
2687 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
2693 os_memcpy(reply
, "OK\n", 3);
2696 if (os_strncmp(buf
, "IFNAME=", 7) == 0) {
2697 char *pos
= os_strchr(buf
+ 7, ' ');
2701 reply_len
= hostapd_global_ctrl_iface_ifname(
2702 interfaces
, buf
+ 7, pos
, reply
, reply_size
,
2708 if (os_strcmp(buf
, "PING") == 0) {
2709 os_memcpy(reply
, "PONG\n", 5);
2711 } else if (os_strncmp(buf
, "RELOG", 5) == 0) {
2712 if (wpa_debug_reopen_file() < 0)
2714 } else if (os_strcmp(buf
, "FLUSH") == 0) {
2715 hostapd_ctrl_iface_flush(interfaces
);
2716 } else if (os_strncmp(buf
, "ADD ", 4) == 0) {
2717 if (hostapd_ctrl_iface_add(interfaces
, buf
+ 4) < 0)
2719 } else if (os_strncmp(buf
, "REMOVE ", 7) == 0) {
2720 if (hostapd_ctrl_iface_remove(interfaces
, buf
+ 7) < 0)
2722 } else if (os_strcmp(buf
, "ATTACH") == 0) {
2723 if (hostapd_global_ctrl_iface_attach(interfaces
, &from
,
2726 } else if (os_strcmp(buf
, "DETACH") == 0) {
2727 if (hostapd_global_ctrl_iface_detach(interfaces
, &from
,
2730 #ifdef CONFIG_MODULE_TESTS
2731 } else if (os_strcmp(buf
, "MODULE_TESTS") == 0) {
2732 int hapd_module_tests(void);
2733 if (hapd_module_tests() < 0)
2735 #endif /* CONFIG_MODULE_TESTS */
2737 } else if (os_strncmp(buf
, "FST-ATTACH ", 11) == 0) {
2738 if (!hostapd_global_ctrl_iface_fst_attach(interfaces
, buf
+ 11))
2739 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
2742 } else if (os_strncmp(buf
, "FST-DETACH ", 11) == 0) {
2743 if (!hostapd_global_ctrl_iface_fst_detach(interfaces
, buf
+ 11))
2744 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
2747 } else if (os_strncmp(buf
, "FST-MANAGER ", 12) == 0) {
2748 reply_len
= fst_ctrl_iface_receive(buf
+ 12, reply
, reply_size
);
2749 #endif /* CONFIG_FST */
2751 wpa_printf(MSG_DEBUG
, "Unrecognized global ctrl_iface command "
2757 if (reply_len
< 0) {
2758 os_memcpy(reply
, "FAIL\n", 5);
2762 if (sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
2764 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
2771 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces
*interface
)
2776 if (interface
->global_iface_path
== NULL
)
2779 len
= os_strlen(interface
->global_iface_path
) +
2780 os_strlen(interface
->global_iface_name
) + 2;
2781 buf
= os_malloc(len
);
2785 os_snprintf(buf
, len
, "%s/%s", interface
->global_iface_path
,
2786 interface
->global_iface_name
);
2787 buf
[len
- 1] = '\0';
2792 int hostapd_global_ctrl_iface_init(struct hapd_interfaces
*interface
)
2794 struct sockaddr_un addr
;
2798 if (interface
->global_iface_path
== NULL
) {
2799 wpa_printf(MSG_DEBUG
, "ctrl_iface not configured!");
2803 if (mkdir(interface
->global_iface_path
, S_IRWXU
| S_IRWXG
) < 0) {
2804 if (errno
== EEXIST
) {
2805 wpa_printf(MSG_DEBUG
, "Using existing control "
2806 "interface directory.");
2808 wpa_printf(MSG_ERROR
, "mkdir[ctrl_interface]: %s",
2812 } else if (interface
->ctrl_iface_group
&&
2813 chown(interface
->global_iface_path
, -1,
2814 interface
->ctrl_iface_group
) < 0) {
2815 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
2820 if (os_strlen(interface
->global_iface_path
) + 1 +
2821 os_strlen(interface
->global_iface_name
) >= sizeof(addr
.sun_path
))
2824 s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
2826 wpa_printf(MSG_ERROR
, "socket(PF_UNIX): %s", strerror(errno
));
2830 os_memset(&addr
, 0, sizeof(addr
));
2832 addr
.sun_len
= sizeof(addr
);
2833 #endif /* __FreeBSD__ */
2834 addr
.sun_family
= AF_UNIX
;
2835 fname
= hostapd_global_ctrl_iface_path(interface
);
2838 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
2839 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
2840 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
2842 if (connect(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
2843 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
2844 " allow connections - assuming it was left"
2845 "over from forced program termination");
2846 if (unlink(fname
) < 0) {
2847 wpa_printf(MSG_ERROR
,
2848 "Could not unlink existing ctrl_iface socket '%s': %s",
2849 fname
, strerror(errno
));
2852 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) <
2854 wpa_printf(MSG_ERROR
, "bind(PF_UNIX): %s",
2858 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
2859 "ctrl_iface socket '%s'", fname
);
2861 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
2862 "be in use - cannot override it");
2863 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
2864 "not used anymore", fname
);
2871 if (interface
->ctrl_iface_group
&&
2872 chown(fname
, -1, interface
->ctrl_iface_group
) < 0) {
2873 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
2878 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
2879 wpa_printf(MSG_ERROR
, "chmod[ctrl_interface/ifname]: %s",
2885 interface
->global_ctrl_sock
= s
;
2886 eloop_register_read_sock(s
, hostapd_global_ctrl_iface_receive
,
2902 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces
*interfaces
)
2905 struct wpa_ctrl_dst
*dst
, *prev
;
2907 if (interfaces
->global_ctrl_sock
> -1) {
2908 eloop_unregister_read_sock(interfaces
->global_ctrl_sock
);
2909 close(interfaces
->global_ctrl_sock
);
2910 interfaces
->global_ctrl_sock
= -1;
2911 fname
= hostapd_global_ctrl_iface_path(interfaces
);
2917 if (interfaces
->global_iface_path
&&
2918 rmdir(interfaces
->global_iface_path
) < 0) {
2919 if (errno
== ENOTEMPTY
) {
2920 wpa_printf(MSG_DEBUG
, "Control interface "
2921 "directory not empty - leaving it "
2924 wpa_printf(MSG_ERROR
,
2925 "rmdir[ctrl_interface=%s]: %s",
2926 interfaces
->global_iface_path
,
2932 os_free(interfaces
->global_iface_path
);
2933 interfaces
->global_iface_path
= NULL
;
2935 dst
= interfaces
->global_ctrl_dst
;
2936 interfaces
->global_ctrl_dst
= NULL
;
2945 static void hostapd_ctrl_iface_send(struct hostapd_data
*hapd
, int level
,
2946 enum wpa_msg_type type
,
2947 const char *buf
, size_t len
)
2949 struct wpa_ctrl_dst
*dst
, *next
;
2956 if (type
!= WPA_MSG_ONLY_GLOBAL
) {
2957 s
= hapd
->ctrl_sock
;
2958 dst
= hapd
->ctrl_dst
;
2960 s
= hapd
->iface
->interfaces
->global_ctrl_sock
;
2961 dst
= hapd
->iface
->interfaces
->global_ctrl_dst
;
2964 if (s
< 0 || dst
== NULL
)
2967 os_snprintf(levelstr
, sizeof(levelstr
), "<%d>", level
);
2968 io
[0].iov_base
= levelstr
;
2969 io
[0].iov_len
= os_strlen(levelstr
);
2970 io
[1].iov_base
= (char *) buf
;
2971 io
[1].iov_len
= len
;
2972 os_memset(&msg
, 0, sizeof(msg
));
2979 if (level
>= dst
->debug_level
) {
2980 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor send",
2981 (u8
*) dst
->addr
.sun_path
, dst
->addrlen
-
2982 offsetof(struct sockaddr_un
, sun_path
));
2983 msg
.msg_name
= &dst
->addr
;
2984 msg
.msg_namelen
= dst
->addrlen
;
2985 if (sendmsg(s
, &msg
, 0) < 0) {
2987 wpa_printf(MSG_INFO
, "CTRL_IFACE monitor[%d]: "
2989 idx
, errno
, strerror(errno
));
2991 if (dst
->errors
> 10 || _errno
== ENOENT
) {
2992 if (type
!= WPA_MSG_ONLY_GLOBAL
)
2993 hostapd_ctrl_iface_detach(
2997 hostapd_global_ctrl_iface_detach(
2998 hapd
->iface
->interfaces
,
3010 #endif /* CONFIG_NATIVE_WINDOWS */