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 "common/ctrl_iface_common.h"
27 #include "crypto/tls.h"
28 #include "drivers/driver.h"
29 #include "eapol_auth/eapol_auth_sm.h"
30 #include "radius/radius_client.h"
31 #include "radius/radius_server.h"
32 #include "l2_packet/l2_packet.h"
33 #include "ap/hostapd.h"
34 #include "ap/ap_config.h"
35 #include "ap/ieee802_1x.h"
36 #include "ap/wpa_auth.h"
37 #include "ap/ieee802_11.h"
38 #include "ap/sta_info.h"
39 #include "ap/wps_hostapd.h"
40 #include "ap/ctrl_iface_ap.h"
41 #include "ap/ap_drv_ops.h"
43 #include "ap/wnm_ap.h"
44 #include "ap/wpa_auth.h"
45 #include "ap/beacon.h"
46 #include "wps/wps_defs.h"
48 #include "fst/fst_ctrl_iface.h"
49 #include "config_file.h"
50 #include "ctrl_iface.h"
53 #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
56 static void hostapd_ctrl_iface_send(struct hostapd_data
*hapd
, int level
,
57 enum wpa_msg_type type
,
58 const char *buf
, size_t len
);
61 static int hostapd_ctrl_iface_attach(struct hostapd_data
*hapd
,
62 struct sockaddr_un
*from
,
65 return ctrl_iface_attach(&hapd
->ctrl_dst
, from
, fromlen
);
69 static int hostapd_ctrl_iface_detach(struct hostapd_data
*hapd
,
70 struct sockaddr_un
*from
,
73 return ctrl_iface_detach(&hapd
->ctrl_dst
, from
, fromlen
);
77 static int hostapd_ctrl_iface_level(struct hostapd_data
*hapd
,
78 struct sockaddr_un
*from
,
82 return ctrl_iface_level(&hapd
->ctrl_dst
, from
, fromlen
, level
);
86 static int hostapd_ctrl_iface_new_sta(struct hostapd_data
*hapd
,
92 wpa_printf(MSG_DEBUG
, "CTRL_IFACE NEW_STA %s", txtaddr
);
94 if (hwaddr_aton(txtaddr
, addr
))
97 sta
= ap_get_sta(hapd
, addr
);
101 wpa_printf(MSG_DEBUG
, "Add new STA " MACSTR
" based on ctrl_iface "
102 "notification", MAC2STR(addr
));
103 sta
= ap_sta_add(hapd
, addr
);
107 hostapd_new_assoc_sta(hapd
, sta
, 0);
112 #ifdef CONFIG_IEEE80211W
114 static int hostapd_ctrl_iface_sa_query(struct hostapd_data
*hapd
,
118 u8 trans_id
[WLAN_SA_QUERY_TR_ID_LEN
];
120 wpa_printf(MSG_DEBUG
, "CTRL_IFACE SA_QUERY %s", txtaddr
);
122 if (hwaddr_aton(txtaddr
, addr
) ||
123 os_get_random(trans_id
, WLAN_SA_QUERY_TR_ID_LEN
) < 0)
126 ieee802_11_send_sa_query_req(hapd
, addr
, trans_id
);
130 #endif /* NEED_AP_MLME */
131 #endif /* CONFIG_IEEE80211W */
135 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data
*hapd
, char *txt
)
137 char *pin
= os_strchr(txt
, ' ');
140 u8 addr_buf
[ETH_ALEN
], *addr
= NULL
;
147 timeout_txt
= os_strchr(pin
, ' ');
149 *timeout_txt
++ = '\0';
150 timeout
= atoi(timeout_txt
);
151 pos
= os_strchr(timeout_txt
, ' ');
154 if (hwaddr_aton(pos
, addr_buf
) == 0)
160 return hostapd_wps_add_pin(hapd
, addr
, txt
, pin
, timeout
);
164 static int hostapd_ctrl_iface_wps_check_pin(
165 struct hostapd_data
*hapd
, char *cmd
, char *buf
, size_t buflen
)
172 wpa_hexdump_ascii_key(MSG_DEBUG
, "WPS_CHECK_PIN",
173 (u8
*) cmd
, os_strlen(cmd
));
174 for (pos
= cmd
, len
= 0; *pos
!= '\0'; pos
++) {
175 if (*pos
< '0' || *pos
> '9')
179 wpa_printf(MSG_DEBUG
, "WPS: Too long PIN");
183 if (len
!= 4 && len
!= 8) {
184 wpa_printf(MSG_DEBUG
, "WPS: Invalid PIN length %d", (int) len
);
190 unsigned int pin_val
;
192 if (!wps_pin_valid(pin_val
)) {
193 wpa_printf(MSG_DEBUG
, "WPS: Invalid checksum digit");
194 ret
= os_snprintf(buf
, buflen
, "FAIL-CHECKSUM\n");
195 if (os_snprintf_error(buflen
, ret
))
201 ret
= os_snprintf(buf
, buflen
, "%s", pin
);
202 if (os_snprintf_error(buflen
, ret
))
209 #ifdef CONFIG_WPS_NFC
210 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data
*hapd
,
217 len
= os_strlen(pos
);
222 buf
= wpabuf_alloc(len
);
225 if (hexstr2bin(pos
, wpabuf_put(buf
, len
), len
) < 0) {
230 ret
= hostapd_wps_nfc_tag_read(hapd
, buf
);
237 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data
*hapd
,
238 char *cmd
, char *reply
,
245 if (os_strcmp(cmd
, "WPS") == 0)
247 else if (os_strcmp(cmd
, "NDEF") == 0)
252 buf
= hostapd_wps_nfc_config_token(hapd
, ndef
);
256 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
267 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data
*hapd
,
268 char *reply
, size_t max_len
,
274 buf
= hostapd_wps_nfc_token_gen(hapd
, ndef
);
278 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
289 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data
*hapd
,
290 char *cmd
, char *reply
,
293 if (os_strcmp(cmd
, "WPS") == 0)
294 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd
, reply
,
297 if (os_strcmp(cmd
, "NDEF") == 0)
298 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd
, reply
,
301 if (os_strcmp(cmd
, "enable") == 0)
302 return hostapd_wps_nfc_token_enable(hapd
);
304 if (os_strcmp(cmd
, "disable") == 0) {
305 hostapd_wps_nfc_token_disable(hapd
);
313 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data
*hapd
,
314 char *cmd
, char *reply
,
322 pos
= os_strchr(cmd
, ' ');
327 if (os_strcmp(cmd
, "WPS") == 0)
329 else if (os_strcmp(cmd
, "NDEF") == 0)
334 if (os_strcmp(pos
, "WPS-CR") == 0)
335 buf
= hostapd_wps_nfc_hs_cr(hapd
, ndef
);
341 res
= wpa_snprintf_hex_uppercase(reply
, max_len
, wpabuf_head(buf
),
352 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data
*hapd
,
356 struct wpabuf
*req
, *sel
;
358 char *pos
, *role
, *type
, *pos2
;
361 pos
= os_strchr(role
, ' ');
367 pos
= os_strchr(type
, ' ');
372 pos2
= os_strchr(pos
, ' ');
377 len
= os_strlen(pos
);
382 req
= wpabuf_alloc(len
);
385 if (hexstr2bin(pos
, wpabuf_put(req
, len
), len
) < 0) {
390 len
= os_strlen(pos2
);
397 sel
= wpabuf_alloc(len
);
402 if (hexstr2bin(pos2
, wpabuf_put(sel
, len
), len
) < 0) {
408 if (os_strcmp(role
, "RESP") == 0 && os_strcmp(type
, "WPS") == 0) {
409 ret
= hostapd_wps_nfc_report_handover(hapd
, req
, sel
);
411 wpa_printf(MSG_DEBUG
, "NFC: Unsupported connection handover "
412 "reported: role=%s type=%s", role
, type
);
421 #endif /* CONFIG_WPS_NFC */
424 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data
*hapd
, char *txt
,
425 char *buf
, size_t buflen
)
431 pos
= os_strchr(txt
, ' ');
435 if (os_strcmp(txt
, "disable") == 0) {
436 hostapd_wps_ap_pin_disable(hapd
);
437 return os_snprintf(buf
, buflen
, "OK\n");
440 if (os_strcmp(txt
, "random") == 0) {
443 pin_txt
= hostapd_wps_ap_pin_random(hapd
, timeout
);
446 return os_snprintf(buf
, buflen
, "%s", pin_txt
);
449 if (os_strcmp(txt
, "get") == 0) {
450 pin_txt
= hostapd_wps_ap_pin_get(hapd
);
453 return os_snprintf(buf
, buflen
, "%s", pin_txt
);
456 if (os_strcmp(txt
, "set") == 0) {
461 pos
= os_strchr(pos
, ' ');
466 if (os_strlen(pin
) > buflen
)
468 if (hostapd_wps_ap_pin_set(hapd
, pin
, timeout
) < 0)
470 return os_snprintf(buf
, buflen
, "%s", pin
);
477 static int hostapd_ctrl_iface_wps_config(struct hostapd_data
*hapd
, char *txt
)
480 char *ssid
, *auth
, *encr
= NULL
, *key
= NULL
;
483 pos
= os_strchr(txt
, ' ');
489 pos
= os_strchr(pos
, ' ');
493 pos
= os_strchr(pos
, ' ');
500 return hostapd_wps_config_ap(hapd
, ssid
, auth
, encr
, key
);
504 static const char * pbc_status_str(enum pbc_status status
)
507 case WPS_PBC_STATUS_DISABLE
:
509 case WPS_PBC_STATUS_ACTIVE
:
511 case WPS_PBC_STATUS_TIMEOUT
:
513 case WPS_PBC_STATUS_OVERLAP
:
521 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data
*hapd
,
522 char *buf
, size_t buflen
)
530 ret
= os_snprintf(pos
, end
- pos
, "PBC Status: %s\n",
531 pbc_status_str(hapd
->wps_stats
.pbc_status
));
533 if (os_snprintf_error(end
- pos
, ret
))
537 ret
= os_snprintf(pos
, end
- pos
, "Last WPS result: %s\n",
538 (hapd
->wps_stats
.status
== WPS_STATUS_SUCCESS
?
540 (hapd
->wps_stats
.status
== WPS_STATUS_FAILURE
?
541 "Failed" : "None")));
543 if (os_snprintf_error(end
- pos
, ret
))
547 /* If status == Failure - Add possible Reasons */
548 if(hapd
->wps_stats
.status
== WPS_STATUS_FAILURE
&&
549 hapd
->wps_stats
.failure_reason
> 0) {
550 ret
= os_snprintf(pos
, end
- pos
,
551 "Failure Reason: %s\n",
552 wps_ei_str(hapd
->wps_stats
.failure_reason
));
554 if (os_snprintf_error(end
- pos
, ret
))
559 if (hapd
->wps_stats
.status
) {
560 ret
= os_snprintf(pos
, end
- pos
, "Peer Address: " MACSTR
"\n",
561 MAC2STR(hapd
->wps_stats
.peer_addr
));
563 if (os_snprintf_error(end
- pos
, ret
))
571 #endif /* CONFIG_WPS */
575 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data
*hapd
,
581 if (hwaddr_aton(cmd
, addr
))
594 return hs20_send_wnm_notification(hapd
, addr
, 1, url
);
598 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data
*hapd
,
602 int code
, reauth_delay
, ret
;
607 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
608 if (hwaddr_aton(cmd
, addr
))
611 pos
= os_strchr(cmd
, ' ');
617 pos
= os_strchr(pos
, ' ');
621 reauth_delay
= atoi(pos
);
624 pos
= os_strchr(pos
, ' ');
627 url_len
= os_strlen(pos
);
630 req
= wpabuf_alloc(4 + url_len
);
633 wpabuf_put_u8(req
, code
);
634 wpabuf_put_le16(req
, reauth_delay
);
635 wpabuf_put_u8(req
, url_len
);
637 wpabuf_put_data(req
, pos
, url_len
);
639 wpa_printf(MSG_DEBUG
, "HS 2.0: Send WNM-Notification to " MACSTR
640 " to indicate imminent deauthentication (code=%d "
641 "reauth_delay=%d)", MAC2STR(addr
), code
, reauth_delay
);
642 ret
= hs20_send_wnm_notification_deauth_req(hapd
, addr
, req
);
647 #endif /* CONFIG_HS20 */
650 #ifdef CONFIG_INTERWORKING
652 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data
*hapd
,
655 u8 qos_map_set
[16 + 2 * 21], count
= 0;
656 const char *pos
= cmd
;
660 if (count
== sizeof(qos_map_set
)) {
661 wpa_printf(MSG_ERROR
, "Too many qos_map_set parameters");
666 if (val
< 0 || val
> 255) {
667 wpa_printf(MSG_INFO
, "Invalid QoS Map Set");
671 qos_map_set
[count
++] = val
;
672 pos
= os_strchr(pos
, ',');
678 if (count
< 16 || count
& 1) {
679 wpa_printf(MSG_INFO
, "Invalid QoS Map Set");
683 ret
= hostapd_drv_set_qos_map(hapd
, qos_map_set
, count
);
685 wpa_printf(MSG_INFO
, "Failed to set QoS Map Set");
689 os_memcpy(hapd
->conf
->qos_map_set
, qos_map_set
, count
);
690 hapd
->conf
->qos_map_set_len
= count
;
696 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data
*hapd
,
700 struct sta_info
*sta
;
702 u8
*qos_map_set
= hapd
->conf
->qos_map_set
;
703 u8 qos_map_set_len
= hapd
->conf
->qos_map_set_len
;
706 if (!qos_map_set_len
) {
707 wpa_printf(MSG_INFO
, "QoS Map Set is not set");
711 if (hwaddr_aton(cmd
, addr
))
714 sta
= ap_get_sta(hapd
, addr
);
716 wpa_printf(MSG_DEBUG
, "Station " MACSTR
" not found "
717 "for QoS Map Configuration message",
722 if (!sta
->qos_map_enabled
) {
723 wpa_printf(MSG_DEBUG
, "Station " MACSTR
" did not indicate "
724 "support for QoS Map", MAC2STR(addr
));
728 buf
= wpabuf_alloc(2 + 2 + qos_map_set_len
);
732 wpabuf_put_u8(buf
, WLAN_ACTION_QOS
);
733 wpabuf_put_u8(buf
, QOS_QOS_MAP_CONFIG
);
735 /* QoS Map Set Element */
736 wpabuf_put_u8(buf
, WLAN_EID_QOS_MAP_SET
);
737 wpabuf_put_u8(buf
, qos_map_set_len
);
738 wpabuf_put_data(buf
, qos_map_set
, qos_map_set_len
);
740 ret
= hostapd_drv_send_action(hapd
, hapd
->iface
->freq
, 0, addr
,
741 wpabuf_head(buf
), wpabuf_len(buf
));
747 #endif /* CONFIG_INTERWORKING */
752 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data
*hapd
,
757 struct sta_info
*sta
;
759 if (hwaddr_aton(cmd
, addr
))
763 disassoc_timer
= atoi(cmd
+ 17);
765 sta
= ap_get_sta(hapd
, addr
);
767 wpa_printf(MSG_DEBUG
, "Station " MACSTR
768 " not found for disassociation imminent message",
773 return wnm_send_disassoc_imminent(hapd
, sta
, disassoc_timer
);
777 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data
*hapd
,
781 const char *url
, *timerstr
;
783 struct sta_info
*sta
;
785 if (hwaddr_aton(cmd
, addr
))
788 sta
= ap_get_sta(hapd
, addr
);
790 wpa_printf(MSG_DEBUG
, "Station " MACSTR
791 " not found for ESS disassociation imminent message",
797 if (*timerstr
!= ' ')
800 disassoc_timer
= atoi(timerstr
);
801 if (disassoc_timer
< 0 || disassoc_timer
> 65535)
804 url
= os_strchr(timerstr
, ' ');
809 return wnm_send_ess_disassoc_imminent(hapd
, sta
, url
, disassoc_timer
);
813 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data
*hapd
,
817 const char *pos
, *end
;
818 int disassoc_timer
= 0;
819 struct sta_info
*sta
;
820 u8 req_mode
= 0, valid_int
= 0x01;
825 u8
*nei_pos
= nei_rep
;
829 if (hwaddr_aton(cmd
, addr
)) {
830 wpa_printf(MSG_DEBUG
, "Invalid STA MAC address");
834 sta
= ap_get_sta(hapd
, addr
);
836 wpa_printf(MSG_DEBUG
, "Station " MACSTR
837 " not found for BSS TM Request message",
842 pos
= os_strstr(cmd
, " disassoc_timer=");
845 disassoc_timer
= atoi(pos
);
846 if (disassoc_timer
< 0 || disassoc_timer
> 65535) {
847 wpa_printf(MSG_DEBUG
, "Invalid disassoc_timer");
852 pos
= os_strstr(cmd
, " valid_int=");
855 valid_int
= atoi(pos
);
858 pos
= os_strstr(cmd
, " bss_term=");
861 req_mode
|= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED
;
862 /* TODO: TSF configurable/learnable */
863 bss_term_dur
[0] = 4; /* Subelement ID */
864 bss_term_dur
[1] = 10; /* Length */
865 os_memset(bss_term_dur
, 2, 8);
866 end
= os_strchr(pos
, ',');
868 wpa_printf(MSG_DEBUG
, "Invalid bss_term data");
872 WPA_PUT_LE16(&bss_term_dur
[10], atoi(end
));
877 * BSS Transition Candidate List Entries - Neighbor Report elements
878 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
879 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
887 pos
= os_strstr(pos
, " neighbor=");
890 if (nei_pos
+ 15 > nei_rep
+ sizeof(nei_rep
)) {
891 wpa_printf(MSG_DEBUG
,
892 "Not enough room for additional neighbor");
898 *nei_pos
++ = WLAN_EID_NEIGHBOR_REPORT
;
899 nei_pos
++; /* length to be filled in */
901 if (hwaddr_aton(pos
, nei_pos
)) {
902 wpa_printf(MSG_DEBUG
, "Invalid BSSID");
908 wpa_printf(MSG_DEBUG
, "Missing BSSID Information");
913 val
= strtol(pos
, &endptr
, 0);
914 WPA_PUT_LE32(nei_pos
, val
);
916 if (*endptr
!= ',') {
917 wpa_printf(MSG_DEBUG
, "Missing Operating Class");
922 *nei_pos
++ = atoi(pos
); /* Operating Class */
923 pos
= os_strchr(pos
, ',');
925 wpa_printf(MSG_DEBUG
, "Missing Channel Number");
930 *nei_pos
++ = atoi(pos
); /* Channel Number */
931 pos
= os_strchr(pos
, ',');
933 wpa_printf(MSG_DEBUG
, "Missing PHY Type");
938 *nei_pos
++ = atoi(pos
); /* PHY Type */
939 end
= os_strchr(pos
, ' ');
940 tmp
= os_strchr(pos
, ',');
941 if (tmp
&& (!end
|| tmp
< end
)) {
942 /* Optional Subelements (hexdump) */
946 end
= os_strchr(pos
, ' ');
950 len
= os_strlen(pos
);
951 if (nei_pos
+ len
/ 2 > nei_rep
+ sizeof(nei_rep
)) {
952 wpa_printf(MSG_DEBUG
,
953 "Not enough room for neighbor subelements");
957 hexstr2bin(pos
, nei_pos
, len
/ 2) < 0) {
958 wpa_printf(MSG_DEBUG
,
959 "Invalid neighbor subelement info");
966 nei_start
[1] = nei_pos
- nei_start
- 2;
969 pos
= os_strstr(cmd
, " url=");
973 end
= os_strchr(pos
, ' ');
977 len
= os_strlen(pos
);
978 url
= os_malloc(len
+ 1);
981 os_memcpy(url
, pos
, len
);
983 req_mode
|= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT
;
986 if (os_strstr(cmd
, " pref=1"))
987 req_mode
|= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED
;
988 if (os_strstr(cmd
, " abridged=1"))
989 req_mode
|= WNM_BSS_TM_REQ_ABRIDGED
;
990 if (os_strstr(cmd
, " disassoc_imminent=1"))
991 req_mode
|= WNM_BSS_TM_REQ_DISASSOC_IMMINENT
;
994 pos
= os_strstr(cmd
, "mbo=");
996 unsigned int mbo_reason
, cell_pref
, reassoc_delay
;
999 ret
= sscanf(pos
, "mbo=%u:%u:%u", &mbo_reason
,
1000 &reassoc_delay
, &cell_pref
);
1002 wpa_printf(MSG_DEBUG
,
1003 "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
1007 if (mbo_reason
> MBO_TRANSITION_REASON_PREMIUM_AP
) {
1008 wpa_printf(MSG_DEBUG
,
1009 "Invalid MBO transition reason code %u",
1014 /* Valid values for Cellular preference are: 0, 1, 255 */
1015 if (cell_pref
!= 0 && cell_pref
!= 1 && cell_pref
!= 255) {
1016 wpa_printf(MSG_DEBUG
,
1017 "Invalid MBO cellular capability %u",
1022 if (reassoc_delay
> 65535 ||
1024 !(req_mode
& WNM_BSS_TM_REQ_DISASSOC_IMMINENT
))) {
1025 wpa_printf(MSG_DEBUG
,
1026 "MBO: Assoc retry delay is only valid in disassoc imminent mode");
1030 *mbo_pos
++ = MBO_ATTR_ID_TRANSITION_REASON
;
1032 *mbo_pos
++ = mbo_reason
;
1033 *mbo_pos
++ = MBO_ATTR_ID_CELL_DATA_PREF
;
1035 *mbo_pos
++ = cell_pref
;
1037 if (reassoc_delay
) {
1038 *mbo_pos
++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY
;
1040 WPA_PUT_LE16(mbo_pos
, reassoc_delay
);
1044 mbo_len
= mbo_pos
- mbo
;
1046 #endif /* CONFIG_MBO */
1048 ret
= wnm_send_bss_tm_req(hapd
, sta
, req_mode
, disassoc_timer
,
1049 valid_int
, bss_term_dur
, url
,
1050 nei_pos
> nei_rep
? nei_rep
: NULL
,
1051 nei_pos
- nei_rep
, mbo_len
? mbo
: NULL
,
1057 #endif /* CONFIG_WNM */
1060 static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data
*hapd
,
1061 char *buf
, size_t buflen
)
1069 WPA_ASSERT(hapd
->conf
->wpa_key_mgmt
);
1071 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_PSK
) {
1072 ret
= os_snprintf(pos
, end
- pos
, "WPA-PSK ");
1073 if (os_snprintf_error(end
- pos
, ret
))
1077 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X
) {
1078 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP ");
1079 if (os_snprintf_error(end
- pos
, ret
))
1083 #ifdef CONFIG_IEEE80211R
1084 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_PSK
) {
1085 ret
= os_snprintf(pos
, end
- pos
, "FT-PSK ");
1086 if (os_snprintf_error(end
- pos
, ret
))
1090 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_IEEE8021X
) {
1091 ret
= os_snprintf(pos
, end
- pos
, "FT-EAP ");
1092 if (os_snprintf_error(end
- pos
, ret
))
1097 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_FT_SAE
) {
1098 ret
= os_snprintf(pos
, end
- pos
, "FT-SAE ");
1099 if (os_snprintf_error(end
- pos
, ret
))
1103 #endif /* CONFIG_SAE */
1104 #endif /* CONFIG_IEEE80211R */
1105 #ifdef CONFIG_IEEE80211W
1106 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_PSK_SHA256
) {
1107 ret
= os_snprintf(pos
, end
- pos
, "WPA-PSK-SHA256 ");
1108 if (os_snprintf_error(end
- pos
, ret
))
1112 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X_SHA256
) {
1113 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP-SHA256 ");
1114 if (os_snprintf_error(end
- pos
, ret
))
1118 #endif /* CONFIG_IEEE80211W */
1120 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_SAE
) {
1121 ret
= os_snprintf(pos
, end
- pos
, "SAE ");
1122 if (os_snprintf_error(end
- pos
, ret
))
1126 #endif /* CONFIG_SAE */
1127 if (hapd
->conf
->wpa_key_mgmt
& WPA_KEY_MGMT_IEEE8021X_SUITE_B
) {
1128 ret
= os_snprintf(pos
, end
- pos
, "WPA-EAP-SUITE-B ");
1129 if (os_snprintf_error(end
- pos
, ret
))
1133 if (hapd
->conf
->wpa_key_mgmt
&
1134 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192
) {
1135 ret
= os_snprintf(pos
, end
- pos
,
1136 "WPA-EAP-SUITE-B-192 ");
1137 if (os_snprintf_error(end
- pos
, ret
))
1142 if (pos
> buf
&& *(pos
- 1) == ' ') {
1151 static int hostapd_ctrl_iface_get_config(struct hostapd_data
*hapd
,
1152 char *buf
, size_t buflen
)
1160 ret
= os_snprintf(pos
, end
- pos
, "bssid=" MACSTR
"\n"
1162 MAC2STR(hapd
->own_addr
),
1163 wpa_ssid_txt(hapd
->conf
->ssid
.ssid
,
1164 hapd
->conf
->ssid
.ssid_len
));
1165 if (os_snprintf_error(end
- pos
, ret
))
1170 ret
= os_snprintf(pos
, end
- pos
, "wps_state=%s\n",
1171 hapd
->conf
->wps_state
== 0 ? "disabled" :
1172 (hapd
->conf
->wps_state
== 1 ? "not configured" :
1174 if (os_snprintf_error(end
- pos
, ret
))
1178 if (hapd
->conf
->wps_state
&& hapd
->conf
->wpa
&&
1179 hapd
->conf
->ssid
.wpa_passphrase
) {
1180 ret
= os_snprintf(pos
, end
- pos
, "passphrase=%s\n",
1181 hapd
->conf
->ssid
.wpa_passphrase
);
1182 if (os_snprintf_error(end
- pos
, ret
))
1187 if (hapd
->conf
->wps_state
&& hapd
->conf
->wpa
&&
1188 hapd
->conf
->ssid
.wpa_psk
&&
1189 hapd
->conf
->ssid
.wpa_psk
->group
) {
1190 char hex
[PMK_LEN
* 2 + 1];
1191 wpa_snprintf_hex(hex
, sizeof(hex
),
1192 hapd
->conf
->ssid
.wpa_psk
->psk
, PMK_LEN
);
1193 ret
= os_snprintf(pos
, end
- pos
, "psk=%s\n", hex
);
1194 if (os_snprintf_error(end
- pos
, ret
))
1198 #endif /* CONFIG_WPS */
1200 if (hapd
->conf
->wpa
) {
1201 ret
= os_snprintf(pos
, end
- pos
, "wpa=%d\n", hapd
->conf
->wpa
);
1202 if (os_snprintf_error(end
- pos
, ret
))
1207 if (hapd
->conf
->wpa
&& hapd
->conf
->wpa_key_mgmt
) {
1208 ret
= os_snprintf(pos
, end
- pos
, "key_mgmt=");
1209 if (os_snprintf_error(end
- pos
, ret
))
1213 pos
+= hostapd_ctrl_iface_get_key_mgmt(hapd
, pos
, end
- pos
);
1215 ret
= os_snprintf(pos
, end
- pos
, "\n");
1216 if (os_snprintf_error(end
- pos
, ret
))
1221 if (hapd
->conf
->wpa
) {
1222 ret
= os_snprintf(pos
, end
- pos
, "group_cipher=%s\n",
1223 wpa_cipher_txt(hapd
->conf
->wpa_group
));
1224 if (os_snprintf_error(end
- pos
, ret
))
1229 if ((hapd
->conf
->wpa
& WPA_PROTO_RSN
) && hapd
->conf
->rsn_pairwise
) {
1230 ret
= os_snprintf(pos
, end
- pos
, "rsn_pairwise_cipher=");
1231 if (os_snprintf_error(end
- pos
, ret
))
1235 ret
= wpa_write_ciphers(pos
, end
, hapd
->conf
->rsn_pairwise
,
1241 ret
= os_snprintf(pos
, end
- pos
, "\n");
1242 if (os_snprintf_error(end
- pos
, ret
))
1247 if ((hapd
->conf
->wpa
& WPA_PROTO_WPA
) && hapd
->conf
->wpa_pairwise
) {
1248 ret
= os_snprintf(pos
, end
- pos
, "wpa_pairwise_cipher=");
1249 if (os_snprintf_error(end
- pos
, ret
))
1253 ret
= wpa_write_ciphers(pos
, end
, hapd
->conf
->wpa_pairwise
,
1259 ret
= os_snprintf(pos
, end
- pos
, "\n");
1260 if (os_snprintf_error(end
- pos
, ret
))
1269 static int hostapd_ctrl_iface_set(struct hostapd_data
*hapd
, char *cmd
)
1274 value
= os_strchr(cmd
, ' ');
1279 wpa_printf(MSG_DEBUG
, "CTRL_IFACE SET '%s'='%s'", cmd
, value
);
1281 #ifdef CONFIG_WPS_TESTING
1282 } else if (os_strcasecmp(cmd
, "wps_version_number") == 0) {
1284 val
= strtol(value
, NULL
, 0);
1285 if (val
< 0 || val
> 0xff) {
1287 wpa_printf(MSG_DEBUG
, "WPS: Invalid "
1288 "wps_version_number %ld", val
);
1290 wps_version_number
= val
;
1291 wpa_printf(MSG_DEBUG
, "WPS: Testing - force WPS "
1293 (wps_version_number
& 0xf0) >> 4,
1294 wps_version_number
& 0x0f);
1295 hostapd_wps_update_ie(hapd
);
1297 } else if (os_strcasecmp(cmd
, "wps_testing_dummy_cred") == 0) {
1298 wps_testing_dummy_cred
= atoi(value
);
1299 wpa_printf(MSG_DEBUG
, "WPS: Testing - dummy_cred=%d",
1300 wps_testing_dummy_cred
);
1301 } else if (os_strcasecmp(cmd
, "wps_corrupt_pkhash") == 0) {
1302 wps_corrupt_pkhash
= atoi(value
);
1303 wpa_printf(MSG_DEBUG
, "WPS: Testing - wps_corrupt_pkhash=%d",
1304 wps_corrupt_pkhash
);
1305 #endif /* CONFIG_WPS_TESTING */
1306 #ifdef CONFIG_INTERWORKING
1307 } else if (os_strcasecmp(cmd
, "gas_frag_limit") == 0) {
1308 int val
= atoi(value
);
1312 hapd
->gas_frag_limit
= val
;
1313 #endif /* CONFIG_INTERWORKING */
1314 #ifdef CONFIG_TESTING_OPTIONS
1315 } else if (os_strcasecmp(cmd
, "ext_mgmt_frame_handling") == 0) {
1316 hapd
->ext_mgmt_frame_handling
= atoi(value
);
1317 } else if (os_strcasecmp(cmd
, "ext_eapol_frame_io") == 0) {
1318 hapd
->ext_eapol_frame_io
= atoi(value
);
1319 #endif /* CONFIG_TESTING_OPTIONS */
1321 } else if (os_strcasecmp(cmd
, "mbo_assoc_disallow") == 0) {
1324 if (!hapd
->conf
->mbo_enabled
)
1328 if (val
< 0 || val
> 1)
1331 hapd
->mbo_assoc_disallow
= val
;
1332 ieee802_11_update_beacons(hapd
->iface
);
1335 * TODO: Need to configure drivers that do AP MLME offload with
1336 * disallowing station logic.
1338 #endif /* CONFIG_MBO */
1340 struct sta_info
*sta
;
1341 struct vlan_description vlan_id
;
1343 ret
= hostapd_set_iface(hapd
->iconf
, hapd
->conf
, cmd
, value
);
1347 if (os_strcasecmp(cmd
, "deny_mac_file") == 0) {
1348 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
1349 if (hostapd_maclist_found(
1350 hapd
->conf
->deny_mac
,
1351 hapd
->conf
->num_deny_mac
, sta
->addr
,
1353 (!vlan_id
.notempty
||
1354 !vlan_compare(&vlan_id
, sta
->vlan_desc
)))
1356 hapd
, sta
, sta
->addr
,
1357 WLAN_REASON_UNSPECIFIED
);
1359 } else if (hapd
->conf
->macaddr_acl
== DENY_UNLESS_ACCEPTED
&&
1360 os_strcasecmp(cmd
, "accept_mac_file") == 0) {
1361 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
1362 if (!hostapd_maclist_found(
1363 hapd
->conf
->accept_mac
,
1364 hapd
->conf
->num_accept_mac
,
1365 sta
->addr
, &vlan_id
) ||
1366 (vlan_id
.notempty
&&
1367 vlan_compare(&vlan_id
, sta
->vlan_desc
)))
1369 hapd
, sta
, sta
->addr
,
1370 WLAN_REASON_UNSPECIFIED
);
1379 static int hostapd_ctrl_iface_get(struct hostapd_data
*hapd
, char *cmd
,
1380 char *buf
, size_t buflen
)
1384 wpa_printf(MSG_DEBUG
, "CTRL_IFACE GET '%s'", cmd
);
1386 if (os_strcmp(cmd
, "version") == 0) {
1387 res
= os_snprintf(buf
, buflen
, "%s", VERSION_STR
);
1388 if (os_snprintf_error(buflen
, res
))
1391 } else if (os_strcmp(cmd
, "tls_library") == 0) {
1392 res
= tls_get_library_version(buf
, buflen
);
1393 if (os_snprintf_error(buflen
, res
))
1402 static int hostapd_ctrl_iface_enable(struct hostapd_iface
*iface
)
1404 if (hostapd_enable_iface(iface
) < 0) {
1405 wpa_printf(MSG_ERROR
, "Enabling of interface failed");
1412 static int hostapd_ctrl_iface_reload(struct hostapd_iface
*iface
)
1414 if (hostapd_reload_iface(iface
) < 0) {
1415 wpa_printf(MSG_ERROR
, "Reloading of interface failed");
1422 static int hostapd_ctrl_iface_disable(struct hostapd_iface
*iface
)
1424 if (hostapd_disable_iface(iface
) < 0) {
1425 wpa_printf(MSG_ERROR
, "Disabling of interface failed");
1432 #ifdef CONFIG_TESTING_OPTIONS
1434 static int hostapd_ctrl_iface_radar(struct hostapd_data
*hapd
, char *cmd
)
1436 union wpa_event_data data
;
1438 enum wpa_event_type event
;
1440 wpa_printf(MSG_DEBUG
, "RADAR TEST: %s", cmd
);
1442 os_memset(&data
, 0, sizeof(data
));
1444 param
= os_strchr(cmd
, ' ');
1449 if (os_strcmp(cmd
, "DETECTED") == 0)
1450 event
= EVENT_DFS_RADAR_DETECTED
;
1451 else if (os_strcmp(cmd
, "CAC-FINISHED") == 0)
1452 event
= EVENT_DFS_CAC_FINISHED
;
1453 else if (os_strcmp(cmd
, "CAC-ABORTED") == 0)
1454 event
= EVENT_DFS_CAC_ABORTED
;
1455 else if (os_strcmp(cmd
, "NOP-FINISHED") == 0)
1456 event
= EVENT_DFS_NOP_FINISHED
;
1458 wpa_printf(MSG_DEBUG
, "Unsupported RADAR test command: %s",
1463 pos
= os_strstr(param
, "freq=");
1465 data
.dfs_event
.freq
= atoi(pos
+ 5);
1467 pos
= os_strstr(param
, "ht_enabled=1");
1469 data
.dfs_event
.ht_enabled
= 1;
1471 pos
= os_strstr(param
, "chan_offset=");
1473 data
.dfs_event
.chan_offset
= atoi(pos
+ 12);
1475 pos
= os_strstr(param
, "chan_width=");
1477 data
.dfs_event
.chan_width
= atoi(pos
+ 11);
1479 pos
= os_strstr(param
, "cf1=");
1481 data
.dfs_event
.cf1
= atoi(pos
+ 4);
1483 pos
= os_strstr(param
, "cf2=");
1485 data
.dfs_event
.cf2
= atoi(pos
+ 4);
1487 wpa_supplicant_event(hapd
, event
, &data
);
1493 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data
*hapd
, char *cmd
)
1499 wpa_printf(MSG_DEBUG
, "External MGMT TX: %s", cmd
);
1501 len
= os_strlen(cmd
);
1506 buf
= os_malloc(len
);
1510 if (hexstr2bin(cmd
, buf
, len
) < 0) {
1515 res
= hostapd_drv_send_mlme(hapd
, buf
, len
, 0);
1521 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data
*hapd
, char *cmd
)
1524 u8 src
[ETH_ALEN
], *buf
;
1528 wpa_printf(MSG_DEBUG
, "External EAPOL RX: %s", cmd
);
1531 used
= hwaddr_aton2(pos
, src
);
1538 len
= os_strlen(pos
);
1543 buf
= os_malloc(len
);
1547 if (hexstr2bin(pos
, buf
, len
) < 0) {
1552 ieee802_1x_receive(hapd
, src
, buf
, len
);
1559 static u16
ipv4_hdr_checksum(const void *buf
, size_t len
)
1563 const u16
*pos
= buf
;
1565 for (i
= 0; i
< len
/ 2; i
++)
1569 sum
= (sum
& 0xffff) + (sum
>> 16);
1571 return sum
^ 0xffff;
1575 #define HWSIM_PACKETLEN 1500
1576 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1578 void hostapd_data_test_rx(void *ctx
, const u8
*src_addr
, const u8
*buf
,
1581 struct hostapd_data
*hapd
= ctx
;
1582 const struct ether_header
*eth
;
1587 if (len
!= HWSIM_PACKETLEN
)
1590 eth
= (const struct ether_header
*) buf
;
1591 os_memcpy(&ip
, eth
+ 1, sizeof(ip
));
1592 pos
= &buf
[sizeof(*eth
) + sizeof(ip
)];
1594 if (ip
.ihl
!= 5 || ip
.version
!= 4 ||
1595 ntohs(ip
.tot_len
) != HWSIM_IP_LEN
)
1598 for (i
= 0; i
< HWSIM_IP_LEN
- sizeof(ip
); i
++) {
1604 wpa_msg(hapd
->msg_ctx
, MSG_INFO
, "DATA-TEST-RX " MACSTR
" " MACSTR
,
1605 MAC2STR(eth
->ether_dhost
), MAC2STR(eth
->ether_shost
));
1609 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data
*hapd
,
1612 int enabled
= atoi(cmd
);
1617 if (hapd
->l2_test
) {
1618 l2_packet_deinit(hapd
->l2_test
);
1619 hapd
->l2_test
= NULL
;
1620 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
,
1621 "test data: Disabled");
1629 pos
= os_strstr(cmd
, " ifname=");
1633 ifname
= hapd
->conf
->iface
;
1635 hapd
->l2_test
= l2_packet_init(ifname
, hapd
->own_addr
,
1636 ETHERTYPE_IP
, hostapd_data_test_rx
,
1638 if (hapd
->l2_test
== NULL
)
1641 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: Enabled");
1647 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data
*hapd
, char *cmd
)
1649 u8 dst
[ETH_ALEN
], src
[ETH_ALEN
];
1654 u8 buf
[2 + HWSIM_PACKETLEN
];
1655 struct ether_header
*eth
;
1660 if (hapd
->l2_test
== NULL
)
1663 /* format: <dst> <src> <tos> */
1666 used
= hwaddr_aton2(pos
, dst
);
1672 used
= hwaddr_aton2(pos
, src
);
1677 val
= strtol(pos
, NULL
, 0);
1678 if (val
< 0 || val
> 0xff)
1682 eth
= (struct ether_header
*) &buf
[2];
1683 os_memcpy(eth
->ether_dhost
, dst
, ETH_ALEN
);
1684 os_memcpy(eth
->ether_shost
, src
, ETH_ALEN
);
1685 eth
->ether_type
= htons(ETHERTYPE_IP
);
1686 ip
= (struct iphdr
*) (eth
+ 1);
1687 os_memset(ip
, 0, sizeof(*ip
));
1692 ip
->tot_len
= htons(HWSIM_IP_LEN
);
1694 ip
->saddr
= htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1695 ip
->daddr
= htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1696 ip
->check
= ipv4_hdr_checksum(ip
, sizeof(*ip
));
1697 dpos
= (u8
*) (ip
+ 1);
1698 for (i
= 0; i
< HWSIM_IP_LEN
- sizeof(*ip
); i
++)
1701 if (l2_packet_send(hapd
->l2_test
, dst
, ETHERTYPE_IP
, &buf
[2],
1702 HWSIM_PACKETLEN
) < 0)
1705 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: TX dst=" MACSTR
1706 " src=" MACSTR
" tos=0x%x", MAC2STR(dst
), MAC2STR(src
), tos
);
1712 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data
*hapd
,
1716 struct ether_header
*eth
;
1717 struct l2_packet_data
*l2
= NULL
;
1721 const char *ifname
= hapd
->conf
->iface
;
1723 if (os_strncmp(cmd
, "ifname=", 7) == 0) {
1726 cmd
= os_strchr(cmd
, ' ');
1732 len
= os_strlen(cmd
);
1733 if (len
& 1 || len
< ETH_HLEN
* 2)
1737 buf
= os_malloc(len
);
1741 if (hexstr2bin(cmd
, buf
, len
) < 0)
1744 eth
= (struct ether_header
*) buf
;
1745 ethertype
= ntohs(eth
->ether_type
);
1747 l2
= l2_packet_init(ifname
, hapd
->own_addr
, ethertype
,
1748 hostapd_data_test_rx
, hapd
, 1);
1752 res
= l2_packet_send(l2
, eth
->ether_dhost
, ethertype
, buf
, len
);
1753 wpa_dbg(hapd
->msg_ctx
, MSG_DEBUG
, "test data: TX frame res=%d", res
);
1756 l2_packet_deinit(l2
);
1759 return res
< 0 ? -1 : 0;
1763 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data
*hapd
, char *cmd
)
1765 #ifdef WPA_TRACE_BFD
1766 extern char wpa_trace_fail_func
[256];
1767 extern unsigned int wpa_trace_fail_after
;
1770 wpa_trace_fail_after
= atoi(cmd
);
1771 pos
= os_strchr(cmd
, ':');
1774 os_strlcpy(wpa_trace_fail_func
, pos
,
1775 sizeof(wpa_trace_fail_func
));
1777 wpa_trace_fail_after
= 0;
1781 #else /* WPA_TRACE_BFD */
1783 #endif /* WPA_TRACE_BFD */
1787 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data
*hapd
,
1788 char *buf
, size_t buflen
)
1790 #ifdef WPA_TRACE_BFD
1791 extern char wpa_trace_fail_func
[256];
1792 extern unsigned int wpa_trace_fail_after
;
1794 return os_snprintf(buf
, buflen
, "%u:%s", wpa_trace_fail_after
,
1795 wpa_trace_fail_func
);
1796 #else /* WPA_TRACE_BFD */
1798 #endif /* WPA_TRACE_BFD */
1802 static int hostapd_ctrl_test_fail(struct hostapd_data
*hapd
, char *cmd
)
1804 #ifdef WPA_TRACE_BFD
1805 extern char wpa_trace_test_fail_func
[256];
1806 extern unsigned int wpa_trace_test_fail_after
;
1809 wpa_trace_test_fail_after
= atoi(cmd
);
1810 pos
= os_strchr(cmd
, ':');
1813 os_strlcpy(wpa_trace_test_fail_func
, pos
,
1814 sizeof(wpa_trace_test_fail_func
));
1816 wpa_trace_test_fail_after
= 0;
1820 #else /* WPA_TRACE_BFD */
1822 #endif /* WPA_TRACE_BFD */
1826 static int hostapd_ctrl_get_fail(struct hostapd_data
*hapd
,
1827 char *buf
, size_t buflen
)
1829 #ifdef WPA_TRACE_BFD
1830 extern char wpa_trace_test_fail_func
[256];
1831 extern unsigned int wpa_trace_test_fail_after
;
1833 return os_snprintf(buf
, buflen
, "%u:%s", wpa_trace_test_fail_after
,
1834 wpa_trace_test_fail_func
);
1835 #else /* WPA_TRACE_BFD */
1837 #endif /* WPA_TRACE_BFD */
1840 #endif /* CONFIG_TESTING_OPTIONS */
1843 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface
*iface
,
1847 struct csa_settings settings
;
1851 ret
= hostapd_parse_csa_settings(pos
, &settings
);
1855 for (i
= 0; i
< iface
->num_bss
; i
++) {
1856 ret
= hostapd_switch_channel(iface
->bss
[i
], &settings
);
1858 /* FIX: What do we do if CSA fails in the middle of
1859 * submitting multi-BSS CSA requests? */
1865 #else /* NEED_AP_MLME */
1867 #endif /* NEED_AP_MLME */
1871 static int hostapd_ctrl_iface_mib(struct hostapd_data
*hapd
, char *reply
,
1872 int reply_size
, const char *param
)
1874 #ifdef RADIUS_SERVER
1875 if (os_strcmp(param
, "radius_server") == 0) {
1876 return radius_server_get_mib(hapd
->radius_srv
, reply
,
1879 #endif /* RADIUS_SERVER */
1884 static int hostapd_ctrl_iface_vendor(struct hostapd_data
*hapd
, char *cmd
,
1885 char *buf
, size_t buflen
)
1890 unsigned int vendor_id
, subcmd
;
1891 struct wpabuf
*reply
;
1892 size_t data_len
= 0;
1894 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1895 vendor_id
= strtoul(cmd
, &pos
, 16);
1896 if (!isblank((unsigned char) *pos
))
1899 subcmd
= strtoul(pos
, &pos
, 10);
1902 if (!isblank((unsigned char) *pos
++))
1904 data_len
= os_strlen(pos
);
1909 data
= os_malloc(data_len
);
1913 if (hexstr2bin(pos
, data
, data_len
)) {
1914 wpa_printf(MSG_DEBUG
,
1915 "Vendor command: wrong parameter format");
1921 reply
= wpabuf_alloc((buflen
- 1) / 2);
1927 ret
= hostapd_drv_vendor_cmd(hapd
, vendor_id
, subcmd
, data
, data_len
,
1931 ret
= wpa_snprintf_hex(buf
, buflen
, wpabuf_head_u8(reply
),
1941 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data
*hapd
,
1945 struct sta_info
*sta
;
1947 if (hwaddr_aton(cmd
, addr
))
1950 sta
= ap_get_sta(hapd
, addr
);
1951 if (!sta
|| !sta
->eapol_sm
)
1954 eapol_auth_reauthenticate(sta
->eapol_sm
);
1959 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data
*hapd
, char *cmd
)
1962 struct sta_info
*sta
;
1963 char *pos
= cmd
, *param
;
1965 if (hwaddr_aton(pos
, addr
) || pos
[17] != ' ')
1969 pos
= os_strchr(pos
, ' ');
1974 sta
= ap_get_sta(hapd
, addr
);
1975 if (!sta
|| !sta
->eapol_sm
)
1978 return eapol_auth_set_conf(sta
->eapol_sm
, param
, pos
);
1982 static int hostapd_ctrl_iface_log_level(struct hostapd_data
*hapd
, char *cmd
,
1983 char *buf
, size_t buflen
)
1985 char *pos
, *end
, *stamp
;
1988 /* cmd: "LOG_LEVEL [<level>]" */
1992 ret
= os_snprintf(pos
, end
- pos
, "Current level: %s\n"
1994 debug_level_str(wpa_debug_level
),
1995 wpa_debug_timestamp
);
1996 if (os_snprintf_error(end
- pos
, ret
))
2005 stamp
= os_strchr(cmd
, ' ');
2008 while (*stamp
== ' ') {
2013 if (os_strlen(cmd
)) {
2014 int level
= str_to_debug_level(cmd
);
2017 wpa_debug_level
= level
;
2020 if (stamp
&& os_strlen(stamp
))
2021 wpa_debug_timestamp
= atoi(stamp
);
2023 os_memcpy(buf
, "OK\n", 3);
2029 static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data
*hapd
,
2030 char *buf
, size_t buflen
)
2032 struct hostapd_iface
*iface
= hapd
->iface
;
2034 struct hostapd_sta_info
*info
;
2035 struct os_reltime now
;
2037 sta_track_expire(iface
, 0);
2042 os_get_reltime(&now
);
2043 dl_list_for_each_reverse(info
, &iface
->sta_seen
,
2044 struct hostapd_sta_info
, list
) {
2045 struct os_reltime age
;
2048 os_reltime_sub(&now
, &info
->last_seen
, &age
);
2049 ret
= os_snprintf(pos
, end
- pos
, MACSTR
" %u\n",
2050 MAC2STR(info
->addr
), (unsigned int) age
.sec
);
2051 if (os_snprintf_error(end
- pos
, ret
))
2058 #endif /* NEED_AP_MLME */
2061 static int hostapd_ctrl_iface_receive_process(struct hostapd_data
*hapd
,
2062 char *buf
, char *reply
,
2064 struct sockaddr_un
*from
,
2069 os_memcpy(reply
, "OK\n", 3);
2072 if (os_strcmp(buf
, "PING") == 0) {
2073 os_memcpy(reply
, "PONG\n", 5);
2075 } else if (os_strncmp(buf
, "RELOG", 5) == 0) {
2076 if (wpa_debug_reopen_file() < 0)
2078 } else if (os_strcmp(buf
, "STATUS") == 0) {
2079 reply_len
= hostapd_ctrl_iface_status(hapd
, reply
,
2081 } else if (os_strcmp(buf
, "STATUS-DRIVER") == 0) {
2082 reply_len
= hostapd_drv_status(hapd
, reply
, reply_size
);
2083 } else if (os_strcmp(buf
, "MIB") == 0) {
2084 reply_len
= ieee802_11_get_mib(hapd
, reply
, reply_size
);
2085 if (reply_len
>= 0) {
2086 res
= wpa_get_mib(hapd
->wpa_auth
, reply
+ reply_len
,
2087 reply_size
- reply_len
);
2093 if (reply_len
>= 0) {
2094 res
= ieee802_1x_get_mib(hapd
, reply
+ reply_len
,
2095 reply_size
- reply_len
);
2101 #ifndef CONFIG_NO_RADIUS
2102 if (reply_len
>= 0) {
2103 res
= radius_client_get_mib(hapd
->radius
,
2105 reply_size
- reply_len
);
2111 #endif /* CONFIG_NO_RADIUS */
2112 } else if (os_strncmp(buf
, "MIB ", 4) == 0) {
2113 reply_len
= hostapd_ctrl_iface_mib(hapd
, reply
, reply_size
,
2115 } else if (os_strcmp(buf
, "STA-FIRST") == 0) {
2116 reply_len
= hostapd_ctrl_iface_sta_first(hapd
, reply
,
2118 } else if (os_strncmp(buf
, "STA ", 4) == 0) {
2119 reply_len
= hostapd_ctrl_iface_sta(hapd
, buf
+ 4, reply
,
2121 } else if (os_strncmp(buf
, "STA-NEXT ", 9) == 0) {
2122 reply_len
= hostapd_ctrl_iface_sta_next(hapd
, buf
+ 9, reply
,
2124 } else if (os_strcmp(buf
, "ATTACH") == 0) {
2125 if (hostapd_ctrl_iface_attach(hapd
, from
, fromlen
))
2127 } else if (os_strcmp(buf
, "DETACH") == 0) {
2128 if (hostapd_ctrl_iface_detach(hapd
, from
, fromlen
))
2130 } else if (os_strncmp(buf
, "LEVEL ", 6) == 0) {
2131 if (hostapd_ctrl_iface_level(hapd
, from
, fromlen
,
2134 } else if (os_strncmp(buf
, "NEW_STA ", 8) == 0) {
2135 if (hostapd_ctrl_iface_new_sta(hapd
, buf
+ 8))
2137 } else if (os_strncmp(buf
, "DEAUTHENTICATE ", 15) == 0) {
2138 if (hostapd_ctrl_iface_deauthenticate(hapd
, buf
+ 15))
2140 } else if (os_strncmp(buf
, "DISASSOCIATE ", 13) == 0) {
2141 if (hostapd_ctrl_iface_disassociate(hapd
, buf
+ 13))
2143 } else if (os_strcmp(buf
, "STOP_AP") == 0) {
2144 if (hostapd_ctrl_iface_stop_ap(hapd
))
2146 #ifdef CONFIG_IEEE80211W
2148 } else if (os_strncmp(buf
, "SA_QUERY ", 9) == 0) {
2149 if (hostapd_ctrl_iface_sa_query(hapd
, buf
+ 9))
2151 #endif /* NEED_AP_MLME */
2152 #endif /* CONFIG_IEEE80211W */
2154 } else if (os_strncmp(buf
, "WPS_PIN ", 8) == 0) {
2155 if (hostapd_ctrl_iface_wps_pin(hapd
, buf
+ 8))
2157 } else if (os_strncmp(buf
, "WPS_CHECK_PIN ", 14) == 0) {
2158 reply_len
= hostapd_ctrl_iface_wps_check_pin(
2159 hapd
, buf
+ 14, reply
, reply_size
);
2160 } else if (os_strcmp(buf
, "WPS_PBC") == 0) {
2161 if (hostapd_wps_button_pushed(hapd
, NULL
))
2163 } else if (os_strcmp(buf
, "WPS_CANCEL") == 0) {
2164 if (hostapd_wps_cancel(hapd
))
2166 } else if (os_strncmp(buf
, "WPS_AP_PIN ", 11) == 0) {
2167 reply_len
= hostapd_ctrl_iface_wps_ap_pin(hapd
, buf
+ 11,
2169 } else if (os_strncmp(buf
, "WPS_CONFIG ", 11) == 0) {
2170 if (hostapd_ctrl_iface_wps_config(hapd
, buf
+ 11) < 0)
2172 } else if (os_strncmp(buf
, "WPS_GET_STATUS", 13) == 0) {
2173 reply_len
= hostapd_ctrl_iface_wps_get_status(hapd
, reply
,
2175 #ifdef CONFIG_WPS_NFC
2176 } else if (os_strncmp(buf
, "WPS_NFC_TAG_READ ", 17) == 0) {
2177 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd
, buf
+ 17))
2179 } else if (os_strncmp(buf
, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2180 reply_len
= hostapd_ctrl_iface_wps_nfc_config_token(
2181 hapd
, buf
+ 21, reply
, reply_size
);
2182 } else if (os_strncmp(buf
, "WPS_NFC_TOKEN ", 14) == 0) {
2183 reply_len
= hostapd_ctrl_iface_wps_nfc_token(
2184 hapd
, buf
+ 14, reply
, reply_size
);
2185 } else if (os_strncmp(buf
, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2186 reply_len
= hostapd_ctrl_iface_nfc_get_handover_sel(
2187 hapd
, buf
+ 21, reply
, reply_size
);
2188 } else if (os_strncmp(buf
, "NFC_REPORT_HANDOVER ", 20) == 0) {
2189 if (hostapd_ctrl_iface_nfc_report_handover(hapd
, buf
+ 20))
2191 #endif /* CONFIG_WPS_NFC */
2192 #endif /* CONFIG_WPS */
2193 #ifdef CONFIG_INTERWORKING
2194 } else if (os_strncmp(buf
, "SET_QOS_MAP_SET ", 16) == 0) {
2195 if (hostapd_ctrl_iface_set_qos_map_set(hapd
, buf
+ 16))
2197 } else if (os_strncmp(buf
, "SEND_QOS_MAP_CONF ", 18) == 0) {
2198 if (hostapd_ctrl_iface_send_qos_map_conf(hapd
, buf
+ 18))
2200 #endif /* CONFIG_INTERWORKING */
2202 } else if (os_strncmp(buf
, "HS20_WNM_NOTIF ", 15) == 0) {
2203 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd
, buf
+ 15))
2205 } else if (os_strncmp(buf
, "HS20_DEAUTH_REQ ", 16) == 0) {
2206 if (hostapd_ctrl_iface_hs20_deauth_req(hapd
, buf
+ 16))
2208 #endif /* CONFIG_HS20 */
2210 } else if (os_strncmp(buf
, "DISASSOC_IMMINENT ", 18) == 0) {
2211 if (hostapd_ctrl_iface_disassoc_imminent(hapd
, buf
+ 18))
2213 } else if (os_strncmp(buf
, "ESS_DISASSOC ", 13) == 0) {
2214 if (hostapd_ctrl_iface_ess_disassoc(hapd
, buf
+ 13))
2216 } else if (os_strncmp(buf
, "BSS_TM_REQ ", 11) == 0) {
2217 if (hostapd_ctrl_iface_bss_tm_req(hapd
, buf
+ 11))
2219 #endif /* CONFIG_WNM */
2220 } else if (os_strcmp(buf
, "GET_CONFIG") == 0) {
2221 reply_len
= hostapd_ctrl_iface_get_config(hapd
, reply
,
2223 } else if (os_strncmp(buf
, "SET ", 4) == 0) {
2224 if (hostapd_ctrl_iface_set(hapd
, buf
+ 4))
2226 } else if (os_strncmp(buf
, "GET ", 4) == 0) {
2227 reply_len
= hostapd_ctrl_iface_get(hapd
, buf
+ 4, reply
,
2229 } else if (os_strncmp(buf
, "ENABLE", 6) == 0) {
2230 if (hostapd_ctrl_iface_enable(hapd
->iface
))
2232 } else if (os_strncmp(buf
, "RELOAD", 6) == 0) {
2233 if (hostapd_ctrl_iface_reload(hapd
->iface
))
2235 } else if (os_strncmp(buf
, "DISABLE", 7) == 0) {
2236 if (hostapd_ctrl_iface_disable(hapd
->iface
))
2238 } else if (os_strcmp(buf
, "UPDATE_BEACON") == 0) {
2239 if (ieee802_11_set_beacon(hapd
))
2241 #ifdef CONFIG_TESTING_OPTIONS
2242 } else if (os_strncmp(buf
, "RADAR ", 6) == 0) {
2243 if (hostapd_ctrl_iface_radar(hapd
, buf
+ 6))
2245 } else if (os_strncmp(buf
, "MGMT_TX ", 8) == 0) {
2246 if (hostapd_ctrl_iface_mgmt_tx(hapd
, buf
+ 8))
2248 } else if (os_strncmp(buf
, "EAPOL_RX ", 9) == 0) {
2249 if (hostapd_ctrl_iface_eapol_rx(hapd
, buf
+ 9) < 0)
2251 } else if (os_strncmp(buf
, "DATA_TEST_CONFIG ", 17) == 0) {
2252 if (hostapd_ctrl_iface_data_test_config(hapd
, buf
+ 17) < 0)
2254 } else if (os_strncmp(buf
, "DATA_TEST_TX ", 13) == 0) {
2255 if (hostapd_ctrl_iface_data_test_tx(hapd
, buf
+ 13) < 0)
2257 } else if (os_strncmp(buf
, "DATA_TEST_FRAME ", 16) == 0) {
2258 if (hostapd_ctrl_iface_data_test_frame(hapd
, buf
+ 16) < 0)
2260 } else if (os_strncmp(buf
, "TEST_ALLOC_FAIL ", 16) == 0) {
2261 if (hostapd_ctrl_test_alloc_fail(hapd
, buf
+ 16) < 0)
2263 } else if (os_strcmp(buf
, "GET_ALLOC_FAIL") == 0) {
2264 reply_len
= hostapd_ctrl_get_alloc_fail(hapd
, reply
,
2266 } else if (os_strncmp(buf
, "TEST_FAIL ", 10) == 0) {
2267 if (hostapd_ctrl_test_fail(hapd
, buf
+ 10) < 0)
2269 } else if (os_strcmp(buf
, "GET_FAIL") == 0) {
2270 reply_len
= hostapd_ctrl_get_fail(hapd
, reply
, reply_size
);
2271 #endif /* CONFIG_TESTING_OPTIONS */
2272 } else if (os_strncmp(buf
, "CHAN_SWITCH ", 12) == 0) {
2273 if (hostapd_ctrl_iface_chan_switch(hapd
->iface
, buf
+ 12))
2275 } else if (os_strncmp(buf
, "VENDOR ", 7) == 0) {
2276 reply_len
= hostapd_ctrl_iface_vendor(hapd
, buf
+ 7, reply
,
2278 } else if (os_strcmp(buf
, "ERP_FLUSH") == 0) {
2279 ieee802_1x_erp_flush(hapd
);
2280 #ifdef RADIUS_SERVER
2281 radius_server_erp_flush(hapd
->radius_srv
);
2282 #endif /* RADIUS_SERVER */
2283 } else if (os_strncmp(buf
, "EAPOL_REAUTH ", 13) == 0) {
2284 if (hostapd_ctrl_iface_eapol_reauth(hapd
, buf
+ 13))
2286 } else if (os_strncmp(buf
, "EAPOL_SET ", 10) == 0) {
2287 if (hostapd_ctrl_iface_eapol_set(hapd
, buf
+ 10))
2289 } else if (os_strncmp(buf
, "LOG_LEVEL", 9) == 0) {
2290 reply_len
= hostapd_ctrl_iface_log_level(
2291 hapd
, buf
+ 9, reply
, reply_size
);
2293 } else if (os_strcmp(buf
, "TRACK_STA_LIST") == 0) {
2294 reply_len
= hostapd_ctrl_iface_track_sta_list(
2295 hapd
, reply
, reply_size
);
2296 #endif /* NEED_AP_MLME */
2298 os_memcpy(reply
, "UNKNOWN COMMAND\n", 16);
2302 if (reply_len
< 0) {
2303 os_memcpy(reply
, "FAIL\n", 5);
2311 static void hostapd_ctrl_iface_receive(int sock
, void *eloop_ctx
,
2314 struct hostapd_data
*hapd
= eloop_ctx
;
2317 struct sockaddr_un from
;
2318 socklen_t fromlen
= sizeof(from
);
2320 const int reply_size
= 4096;
2322 int level
= MSG_DEBUG
;
2324 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
2325 (struct sockaddr
*) &from
, &fromlen
);
2327 wpa_printf(MSG_ERROR
, "recvfrom(ctrl_iface): %s",
2332 if (os_strcmp(buf
, "PING") == 0)
2333 level
= MSG_EXCESSIVE
;
2334 wpa_hexdump_ascii(level
, "RX ctrl_iface", (u8
*) buf
, res
);
2336 reply
= os_malloc(reply_size
);
2337 if (reply
== NULL
) {
2338 if (sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
2340 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
2346 reply_len
= hostapd_ctrl_iface_receive_process(hapd
, buf
,
2350 if (sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
2352 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
2359 static char * hostapd_ctrl_iface_path(struct hostapd_data
*hapd
)
2364 if (hapd
->conf
->ctrl_interface
== NULL
)
2367 len
= os_strlen(hapd
->conf
->ctrl_interface
) +
2368 os_strlen(hapd
->conf
->iface
) + 2;
2369 buf
= os_malloc(len
);
2373 os_snprintf(buf
, len
, "%s/%s",
2374 hapd
->conf
->ctrl_interface
, hapd
->conf
->iface
);
2375 buf
[len
- 1] = '\0';
2380 static void hostapd_ctrl_iface_msg_cb(void *ctx
, int level
,
2381 enum wpa_msg_type type
,
2382 const char *txt
, size_t len
)
2384 struct hostapd_data
*hapd
= ctx
;
2387 hostapd_ctrl_iface_send(hapd
, level
, type
, txt
, len
);
2391 int hostapd_ctrl_iface_init(struct hostapd_data
*hapd
)
2393 struct sockaddr_un addr
;
2397 if (hapd
->ctrl_sock
> -1) {
2398 wpa_printf(MSG_DEBUG
, "ctrl_iface already exists!");
2402 dl_list_init(&hapd
->ctrl_dst
);
2404 if (hapd
->conf
->ctrl_interface
== NULL
)
2407 if (mkdir(hapd
->conf
->ctrl_interface
, S_IRWXU
| S_IRWXG
) < 0) {
2408 if (errno
== EEXIST
) {
2409 wpa_printf(MSG_DEBUG
, "Using existing control "
2410 "interface directory.");
2412 wpa_printf(MSG_ERROR
, "mkdir[ctrl_interface]: %s",
2418 if (hapd
->conf
->ctrl_interface_gid_set
&&
2419 chown(hapd
->conf
->ctrl_interface
, -1,
2420 hapd
->conf
->ctrl_interface_gid
) < 0) {
2421 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
2426 if (!hapd
->conf
->ctrl_interface_gid_set
&&
2427 hapd
->iface
->interfaces
->ctrl_iface_group
&&
2428 chown(hapd
->conf
->ctrl_interface
, -1,
2429 hapd
->iface
->interfaces
->ctrl_iface_group
) < 0) {
2430 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
2437 * Android is using umask 0077 which would leave the control interface
2438 * directory without group access. This breaks things since Wi-Fi
2439 * framework assumes that this directory can be accessed by other
2440 * applications in the wifi group. Fix this by adding group access even
2441 * if umask value would prevent this.
2443 if (chmod(hapd
->conf
->ctrl_interface
, S_IRWXU
| S_IRWXG
) < 0) {
2444 wpa_printf(MSG_ERROR
, "CTRL: Could not chmod directory: %s",
2446 /* Try to continue anyway */
2448 #endif /* ANDROID */
2450 if (os_strlen(hapd
->conf
->ctrl_interface
) + 1 +
2451 os_strlen(hapd
->conf
->iface
) >= sizeof(addr
.sun_path
))
2454 s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
2456 wpa_printf(MSG_ERROR
, "socket(PF_UNIX): %s", strerror(errno
));
2460 os_memset(&addr
, 0, sizeof(addr
));
2462 addr
.sun_len
= sizeof(addr
);
2463 #endif /* __FreeBSD__ */
2464 addr
.sun_family
= AF_UNIX
;
2465 fname
= hostapd_ctrl_iface_path(hapd
);
2468 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
2469 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
2470 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
2472 if (connect(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
2473 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
2474 " allow connections - assuming it was left"
2475 "over from forced program termination");
2476 if (unlink(fname
) < 0) {
2477 wpa_printf(MSG_ERROR
,
2478 "Could not unlink existing ctrl_iface socket '%s': %s",
2479 fname
, strerror(errno
));
2482 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) <
2484 wpa_printf(MSG_ERROR
,
2485 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
2489 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
2490 "ctrl_iface socket '%s'", fname
);
2492 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
2493 "be in use - cannot override it");
2494 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
2495 "not used anymore", fname
);
2502 if (hapd
->conf
->ctrl_interface_gid_set
&&
2503 chown(fname
, -1, hapd
->conf
->ctrl_interface_gid
) < 0) {
2504 wpa_printf(MSG_ERROR
, "chown[ctrl_interface/ifname]: %s",
2509 if (!hapd
->conf
->ctrl_interface_gid_set
&&
2510 hapd
->iface
->interfaces
->ctrl_iface_group
&&
2511 chown(fname
, -1, hapd
->iface
->interfaces
->ctrl_iface_group
) < 0) {
2512 wpa_printf(MSG_ERROR
, "chown[ctrl_interface/ifname]: %s",
2517 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
2518 wpa_printf(MSG_ERROR
, "chmod[ctrl_interface/ifname]: %s",
2524 hapd
->ctrl_sock
= s
;
2525 if (eloop_register_read_sock(s
, hostapd_ctrl_iface_receive
, hapd
,
2527 hostapd_ctrl_iface_deinit(hapd
);
2530 hapd
->msg_ctx
= hapd
;
2531 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb
);
2546 void hostapd_ctrl_iface_deinit(struct hostapd_data
*hapd
)
2548 struct wpa_ctrl_dst
*dst
, *prev
;
2550 if (hapd
->ctrl_sock
> -1) {
2552 eloop_unregister_read_sock(hapd
->ctrl_sock
);
2553 close(hapd
->ctrl_sock
);
2554 hapd
->ctrl_sock
= -1;
2555 fname
= hostapd_ctrl_iface_path(hapd
);
2560 if (hapd
->conf
->ctrl_interface
&&
2561 rmdir(hapd
->conf
->ctrl_interface
) < 0) {
2562 if (errno
== ENOTEMPTY
) {
2563 wpa_printf(MSG_DEBUG
, "Control interface "
2564 "directory not empty - leaving it "
2567 wpa_printf(MSG_ERROR
,
2568 "rmdir[ctrl_interface=%s]: %s",
2569 hapd
->conf
->ctrl_interface
,
2575 dl_list_for_each_safe(dst
, prev
, &hapd
->ctrl_dst
, struct wpa_ctrl_dst
,
2579 #ifdef CONFIG_TESTING_OPTIONS
2580 l2_packet_deinit(hapd
->l2_test
);
2581 hapd
->l2_test
= NULL
;
2582 #endif /* CONFIG_TESTING_OPTIONS */
2586 static int hostapd_ctrl_iface_add(struct hapd_interfaces
*interfaces
,
2589 if (hostapd_add_iface(interfaces
, buf
) < 0) {
2590 wpa_printf(MSG_ERROR
, "Adding interface %s failed", buf
);
2597 static int hostapd_ctrl_iface_remove(struct hapd_interfaces
*interfaces
,
2600 if (hostapd_remove_iface(interfaces
, buf
) < 0) {
2601 wpa_printf(MSG_ERROR
, "Removing interface %s failed", buf
);
2608 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces
*interfaces
,
2609 struct sockaddr_un
*from
,
2612 return ctrl_iface_attach(&interfaces
->global_ctrl_dst
, from
, fromlen
);
2616 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces
*interfaces
,
2617 struct sockaddr_un
*from
,
2620 return ctrl_iface_detach(&interfaces
->global_ctrl_dst
, from
, fromlen
);
2624 static void hostapd_ctrl_iface_flush(struct hapd_interfaces
*interfaces
)
2626 #ifdef CONFIG_WPS_TESTING
2627 wps_version_number
= 0x20;
2628 wps_testing_dummy_cred
= 0;
2629 wps_corrupt_pkhash
= 0;
2630 #endif /* CONFIG_WPS_TESTING */
2637 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces
*interfaces
,
2640 char ifname
[IFNAMSIZ
+ 1];
2641 struct fst_iface_cfg cfg
;
2642 struct hostapd_data
*hapd
;
2643 struct fst_wpa_obj iface_obj
;
2645 if (!fst_parse_attach_command(cmd
, ifname
, sizeof(ifname
), &cfg
)) {
2646 hapd
= hostapd_get_iface(interfaces
, ifname
);
2648 if (hapd
->iface
->fst
) {
2649 wpa_printf(MSG_INFO
, "FST: Already attached");
2652 fst_hostapd_fill_iface_obj(hapd
, &iface_obj
);
2653 hapd
->iface
->fst
= fst_attach(ifname
, hapd
->own_addr
,
2655 if (hapd
->iface
->fst
)
2665 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces
*interfaces
,
2668 char ifname
[IFNAMSIZ
+ 1];
2669 struct hostapd_data
* hapd
;
2671 if (!fst_parse_detach_command(cmd
, ifname
, sizeof(ifname
))) {
2672 hapd
= hostapd_get_iface(interfaces
, ifname
);
2674 if (!fst_iface_detach(ifname
)) {
2675 hapd
->iface
->fst
= NULL
;
2676 hapd
->iface
->fst_ies
= NULL
;
2685 #endif /* CONFIG_FST */
2688 static struct hostapd_data
*
2689 hostapd_interfaces_get_hapd(struct hapd_interfaces
*interfaces
,
2694 for (i
= 0; i
< interfaces
->count
; i
++) {
2695 struct hostapd_iface
*iface
= interfaces
->iface
[i
];
2697 for (j
= 0; j
< iface
->num_bss
; j
++) {
2698 struct hostapd_data
*hapd
;
2700 hapd
= iface
->bss
[j
];
2701 if (os_strcmp(ifname
, hapd
->conf
->iface
) == 0)
2710 static int hostapd_ctrl_iface_dup_param(struct hostapd_data
*src_hapd
,
2711 struct hostapd_data
*dst_hapd
,
2717 value
= os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN
);
2719 wpa_printf(MSG_ERROR
,
2720 "DUP: cannot allocate buffer to stringify %s",
2725 if (os_strcmp(param
, "wpa") == 0) {
2726 os_snprintf(value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
, "%d",
2727 src_hapd
->conf
->wpa
);
2728 } else if (os_strcmp(param
, "wpa_key_mgmt") == 0 &&
2729 src_hapd
->conf
->wpa_key_mgmt
) {
2730 res
= hostapd_ctrl_iface_get_key_mgmt(
2731 src_hapd
, value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
);
2732 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN
, res
))
2733 goto error_stringify
;
2734 } else if (os_strcmp(param
, "wpa_pairwise") == 0 &&
2735 src_hapd
->conf
->wpa_pairwise
) {
2736 res
= wpa_write_ciphers(value
,
2737 value
+ HOSTAPD_CLI_DUP_VALUE_MAX_LEN
,
2738 src_hapd
->conf
->wpa_pairwise
, " ");
2740 goto error_stringify
;
2741 } else if (os_strcmp(param
, "rsn_pairwise") == 0 &&
2742 src_hapd
->conf
->rsn_pairwise
) {
2743 res
= wpa_write_ciphers(value
,
2744 value
+ HOSTAPD_CLI_DUP_VALUE_MAX_LEN
,
2745 src_hapd
->conf
->rsn_pairwise
, " ");
2747 goto error_stringify
;
2748 } else if (os_strcmp(param
, "wpa_passphrase") == 0 &&
2749 src_hapd
->conf
->ssid
.wpa_passphrase
) {
2750 os_snprintf(value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
, "%s",
2751 src_hapd
->conf
->ssid
.wpa_passphrase
);
2752 } else if (os_strcmp(param
, "wpa_psk") == 0 &&
2753 src_hapd
->conf
->ssid
.wpa_psk_set
) {
2754 wpa_snprintf_hex(value
, HOSTAPD_CLI_DUP_VALUE_MAX_LEN
,
2755 src_hapd
->conf
->ssid
.wpa_psk
->psk
, PMK_LEN
);
2757 wpa_printf(MSG_WARNING
, "DUP: %s cannot be duplicated", param
);
2761 res
= hostapd_set_iface(dst_hapd
->iconf
, dst_hapd
->conf
, param
, value
);
2766 wpa_printf(MSG_ERROR
, "DUP: cannot stringify %s", param
);
2774 hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces
*interfaces
,
2777 char *p_start
= cmd
, *p_end
;
2778 struct hostapd_data
*src_hapd
, *dst_hapd
;
2780 /* cmd: "<src ifname> <dst ifname> <variable name> */
2782 p_end
= os_strchr(p_start
, ' ');
2784 wpa_printf(MSG_ERROR
, "DUP: no src ifname found in cmd: '%s'",
2790 src_hapd
= hostapd_interfaces_get_hapd(interfaces
, p_start
);
2792 wpa_printf(MSG_ERROR
, "DUP: no src ifname found: '%s'",
2797 p_start
= p_end
+ 1;
2798 p_end
= os_strchr(p_start
, ' ');
2800 wpa_printf(MSG_ERROR
, "DUP: no dst ifname found in cmd: '%s'",
2806 dst_hapd
= hostapd_interfaces_get_hapd(interfaces
, p_start
);
2808 wpa_printf(MSG_ERROR
, "DUP: no dst ifname found: '%s'",
2813 p_start
= p_end
+ 1;
2814 return hostapd_ctrl_iface_dup_param(src_hapd
, dst_hapd
, p_start
);
2818 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces
*interfaces
,
2820 char *buf
, char *reply
,
2822 struct sockaddr_un
*from
,
2825 struct hostapd_data
*hapd
;
2827 hapd
= hostapd_interfaces_get_hapd(interfaces
, ifname
);
2831 res
= os_snprintf(reply
, reply_size
, "FAIL-NO-IFNAME-MATCH\n");
2832 if (os_snprintf_error(reply_size
, res
))
2837 return hostapd_ctrl_iface_receive_process(hapd
, buf
, reply
,reply_size
,
2842 static void hostapd_global_ctrl_iface_receive(int sock
, void *eloop_ctx
,
2845 void *interfaces
= eloop_ctx
;
2848 struct sockaddr_un from
;
2849 socklen_t fromlen
= sizeof(from
);
2852 const int reply_size
= 4096;
2854 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
2855 (struct sockaddr
*) &from
, &fromlen
);
2857 wpa_printf(MSG_ERROR
, "recvfrom(ctrl_iface): %s",
2862 wpa_printf(MSG_DEBUG
, "Global ctrl_iface command: %s", buf
);
2864 reply
= os_malloc(reply_size
);
2865 if (reply
== NULL
) {
2866 if (sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
2868 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
2874 os_memcpy(reply
, "OK\n", 3);
2877 if (os_strncmp(buf
, "IFNAME=", 7) == 0) {
2878 char *pos
= os_strchr(buf
+ 7, ' ');
2882 reply_len
= hostapd_global_ctrl_iface_ifname(
2883 interfaces
, buf
+ 7, pos
, reply
, reply_size
,
2889 if (os_strcmp(buf
, "PING") == 0) {
2890 os_memcpy(reply
, "PONG\n", 5);
2892 } else if (os_strncmp(buf
, "RELOG", 5) == 0) {
2893 if (wpa_debug_reopen_file() < 0)
2895 } else if (os_strcmp(buf
, "FLUSH") == 0) {
2896 hostapd_ctrl_iface_flush(interfaces
);
2897 } else if (os_strncmp(buf
, "ADD ", 4) == 0) {
2898 if (hostapd_ctrl_iface_add(interfaces
, buf
+ 4) < 0)
2900 } else if (os_strncmp(buf
, "REMOVE ", 7) == 0) {
2901 if (hostapd_ctrl_iface_remove(interfaces
, buf
+ 7) < 0)
2903 } else if (os_strcmp(buf
, "ATTACH") == 0) {
2904 if (hostapd_global_ctrl_iface_attach(interfaces
, &from
,
2907 } else if (os_strcmp(buf
, "DETACH") == 0) {
2908 if (hostapd_global_ctrl_iface_detach(interfaces
, &from
,
2911 #ifdef CONFIG_MODULE_TESTS
2912 } else if (os_strcmp(buf
, "MODULE_TESTS") == 0) {
2913 int hapd_module_tests(void);
2914 if (hapd_module_tests() < 0)
2916 #endif /* CONFIG_MODULE_TESTS */
2918 } else if (os_strncmp(buf
, "FST-ATTACH ", 11) == 0) {
2919 if (!hostapd_global_ctrl_iface_fst_attach(interfaces
, buf
+ 11))
2920 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
2923 } else if (os_strncmp(buf
, "FST-DETACH ", 11) == 0) {
2924 if (!hostapd_global_ctrl_iface_fst_detach(interfaces
, buf
+ 11))
2925 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
2928 } else if (os_strncmp(buf
, "FST-MANAGER ", 12) == 0) {
2929 reply_len
= fst_ctrl_iface_receive(buf
+ 12, reply
, reply_size
);
2930 #endif /* CONFIG_FST */
2931 } else if (os_strncmp(buf
, "DUP_NETWORK ", 12) == 0) {
2932 if (!hostapd_global_ctrl_iface_dup_network(interfaces
,
2934 reply_len
= os_snprintf(reply
, reply_size
, "OK\n");
2938 wpa_printf(MSG_DEBUG
, "Unrecognized global ctrl_iface command "
2944 if (reply_len
< 0) {
2945 os_memcpy(reply
, "FAIL\n", 5);
2949 if (sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
2951 wpa_printf(MSG_DEBUG
, "CTRL: sendto failed: %s",
2958 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces
*interface
)
2963 if (interface
->global_iface_path
== NULL
)
2966 len
= os_strlen(interface
->global_iface_path
) +
2967 os_strlen(interface
->global_iface_name
) + 2;
2968 buf
= os_malloc(len
);
2972 os_snprintf(buf
, len
, "%s/%s", interface
->global_iface_path
,
2973 interface
->global_iface_name
);
2974 buf
[len
- 1] = '\0';
2979 int hostapd_global_ctrl_iface_init(struct hapd_interfaces
*interface
)
2981 struct sockaddr_un addr
;
2985 if (interface
->global_iface_path
== NULL
) {
2986 wpa_printf(MSG_DEBUG
, "ctrl_iface not configured!");
2990 if (mkdir(interface
->global_iface_path
, S_IRWXU
| S_IRWXG
) < 0) {
2991 if (errno
== EEXIST
) {
2992 wpa_printf(MSG_DEBUG
, "Using existing control "
2993 "interface directory.");
2995 wpa_printf(MSG_ERROR
, "mkdir[ctrl_interface]: %s",
2999 } else if (interface
->ctrl_iface_group
&&
3000 chown(interface
->global_iface_path
, -1,
3001 interface
->ctrl_iface_group
) < 0) {
3002 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
3007 if (os_strlen(interface
->global_iface_path
) + 1 +
3008 os_strlen(interface
->global_iface_name
) >= sizeof(addr
.sun_path
))
3011 s
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
3013 wpa_printf(MSG_ERROR
, "socket(PF_UNIX): %s", strerror(errno
));
3017 os_memset(&addr
, 0, sizeof(addr
));
3019 addr
.sun_len
= sizeof(addr
);
3020 #endif /* __FreeBSD__ */
3021 addr
.sun_family
= AF_UNIX
;
3022 fname
= hostapd_global_ctrl_iface_path(interface
);
3025 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
3026 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
3027 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
3029 if (connect(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
3030 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
3031 " allow connections - assuming it was left"
3032 "over from forced program termination");
3033 if (unlink(fname
) < 0) {
3034 wpa_printf(MSG_ERROR
,
3035 "Could not unlink existing ctrl_iface socket '%s': %s",
3036 fname
, strerror(errno
));
3039 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) <
3041 wpa_printf(MSG_ERROR
, "bind(PF_UNIX): %s",
3045 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
3046 "ctrl_iface socket '%s'", fname
);
3048 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
3049 "be in use - cannot override it");
3050 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
3051 "not used anymore", fname
);
3058 if (interface
->ctrl_iface_group
&&
3059 chown(fname
, -1, interface
->ctrl_iface_group
) < 0) {
3060 wpa_printf(MSG_ERROR
, "chown[ctrl_interface]: %s",
3065 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
3066 wpa_printf(MSG_ERROR
, "chmod[ctrl_interface/ifname]: %s",
3072 interface
->global_ctrl_sock
= s
;
3073 eloop_register_read_sock(s
, hostapd_global_ctrl_iface_receive
,
3089 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces
*interfaces
)
3092 struct wpa_ctrl_dst
*dst
, *prev
;
3094 if (interfaces
->global_ctrl_sock
> -1) {
3095 eloop_unregister_read_sock(interfaces
->global_ctrl_sock
);
3096 close(interfaces
->global_ctrl_sock
);
3097 interfaces
->global_ctrl_sock
= -1;
3098 fname
= hostapd_global_ctrl_iface_path(interfaces
);
3104 if (interfaces
->global_iface_path
&&
3105 rmdir(interfaces
->global_iface_path
) < 0) {
3106 if (errno
== ENOTEMPTY
) {
3107 wpa_printf(MSG_DEBUG
, "Control interface "
3108 "directory not empty - leaving it "
3111 wpa_printf(MSG_ERROR
,
3112 "rmdir[ctrl_interface=%s]: %s",
3113 interfaces
->global_iface_path
,
3119 os_free(interfaces
->global_iface_path
);
3120 interfaces
->global_iface_path
= NULL
;
3122 dl_list_for_each_safe(dst
, prev
, &interfaces
->global_ctrl_dst
,
3123 struct wpa_ctrl_dst
, list
)
3128 static void hostapd_ctrl_iface_send(struct hostapd_data
*hapd
, int level
,
3129 enum wpa_msg_type type
,
3130 const char *buf
, size_t len
)
3132 struct wpa_ctrl_dst
*dst
, *next
;
3133 struct dl_list
*ctrl_dst
;
3140 if (type
!= WPA_MSG_ONLY_GLOBAL
) {
3141 s
= hapd
->ctrl_sock
;
3142 ctrl_dst
= &hapd
->ctrl_dst
;
3144 s
= hapd
->iface
->interfaces
->global_ctrl_sock
;
3145 ctrl_dst
= &hapd
->iface
->interfaces
->global_ctrl_dst
;
3148 if (s
< 0 || dl_list_empty(ctrl_dst
))
3151 os_snprintf(levelstr
, sizeof(levelstr
), "<%d>", level
);
3152 io
[0].iov_base
= levelstr
;
3153 io
[0].iov_len
= os_strlen(levelstr
);
3154 io
[1].iov_base
= (char *) buf
;
3155 io
[1].iov_len
= len
;
3156 os_memset(&msg
, 0, sizeof(msg
));
3161 dl_list_for_each_safe(dst
, next
, ctrl_dst
, struct wpa_ctrl_dst
, list
) {
3162 if (level
>= dst
->debug_level
) {
3163 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor send",
3164 (u8
*) dst
->addr
.sun_path
, dst
->addrlen
-
3165 offsetof(struct sockaddr_un
, sun_path
));
3166 msg
.msg_name
= &dst
->addr
;
3167 msg
.msg_namelen
= dst
->addrlen
;
3168 if (sendmsg(s
, &msg
, 0) < 0) {
3170 wpa_printf(MSG_INFO
, "CTRL_IFACE monitor[%d]: "
3172 idx
, errno
, strerror(errno
));
3174 if (dst
->errors
> 10 || _errno
== ENOENT
) {
3175 if (type
!= WPA_MSG_ONLY_GLOBAL
)
3176 hostapd_ctrl_iface_detach(
3180 hostapd_global_ctrl_iface_detach(
3181 hapd
->iface
->interfaces
,
3192 #endif /* CONFIG_NATIVE_WINDOWS */