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"
52 #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
55 struct wpa_ctrl_dst *next;
56 struct sockaddr_un addr;
63 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
64 enum wpa_msg_type type,
65 const char *buf, size_t len);
68 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
69 struct sockaddr_un *from,
72 struct wpa_ctrl_dst *dst;
74 dst = os_zalloc(sizeof(*dst));
77 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
78 dst->addrlen = fromlen;
79 dst->debug_level = MSG_INFO;
80 dst->next = hapd->ctrl_dst;
82 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
83 (u8 *) from->sun_path,
84 fromlen - offsetof(struct sockaddr_un, sun_path));
89 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
90 struct sockaddr_un *from,
93 struct wpa_ctrl_dst *dst, *prev = NULL;
97 if (fromlen == dst->addrlen &&
98 os_memcmp(from->sun_path, dst->addr.sun_path,
99 fromlen - offsetof(struct sockaddr_un, sun_path))
101 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
102 (u8 *) from->sun_path,
104 offsetof(struct sockaddr_un, sun_path));
106 hapd->ctrl_dst = dst->next;
108 prev->next = dst->next;
119 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
120 struct sockaddr_un *from,
124 struct wpa_ctrl_dst *dst;
126 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
128 dst = hapd->ctrl_dst;
130 if (fromlen == dst->addrlen &&
131 os_memcmp(from->sun_path, dst->addr.sun_path,
132 fromlen - offsetof(struct sockaddr_un, sun_path))
134 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
135 "level", (u8 *) from->sun_path, fromlen -
136 offsetof(struct sockaddr_un, sun_path));
137 dst->debug_level = atoi(level);
147 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
151 struct sta_info *sta;
153 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
155 if (hwaddr_aton(txtaddr, addr))
158 sta = ap_get_sta(hapd, addr);
162 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
163 "notification", MAC2STR(addr));
164 sta = ap_sta_add(hapd, addr);
168 hostapd_new_assoc_sta(hapd, sta, 0);
173 #ifdef CONFIG_IEEE80211W
175 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
179 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
181 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
183 if (hwaddr_aton(txtaddr, addr) ||
184 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
187 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
191 #endif /* NEED_AP_MLME */
192 #endif /* CONFIG_IEEE80211W */
196 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
198 char *pin = os_strchr(txt, ' ');
201 u8 addr_buf[ETH_ALEN], *addr = NULL;
208 timeout_txt = os_strchr(pin, ' ');
210 *timeout_txt++ = '\0';
211 timeout = atoi(timeout_txt);
212 pos = os_strchr(timeout_txt, ' ');
215 if (hwaddr_aton(pos, addr_buf) == 0)
221 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
225 static int hostapd_ctrl_iface_wps_check_pin(
226 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
233 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
234 (u8 *) cmd, os_strlen(cmd));
235 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
236 if (*pos < '0' || *pos > '9')
240 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
244 if (len != 4 && len != 8) {
245 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
251 unsigned int pin_val;
253 if (!wps_pin_valid(pin_val)) {
254 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
255 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
256 if (os_snprintf_error(buflen, ret))
262 ret = os_snprintf(buf, buflen, "%s", pin);
263 if (os_snprintf_error(buflen, ret))
270 #ifdef CONFIG_WPS_NFC
271 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
278 len = os_strlen(pos);
283 buf = wpabuf_alloc(len);
286 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
291 ret = hostapd_wps_nfc_tag_read(hapd, buf);
298 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
299 char *cmd, char *reply,
306 if (os_strcmp(cmd, "WPS") == 0)
308 else if (os_strcmp(cmd, "NDEF") == 0)
313 buf = hostapd_wps_nfc_config_token(hapd, ndef);
317 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
328 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
329 char *reply, size_t max_len,
335 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
339 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
350 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
351 char *cmd, char *reply,
354 if (os_strcmp(cmd, "WPS") == 0)
355 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
358 if (os_strcmp(cmd, "NDEF") == 0)
359 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
362 if (os_strcmp(cmd, "enable") == 0)
363 return hostapd_wps_nfc_token_enable(hapd);
365 if (os_strcmp(cmd, "disable") == 0) {
366 hostapd_wps_nfc_token_disable(hapd);
374 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
375 char *cmd, char *reply,
383 pos = os_strchr(cmd, ' ');
388 if (os_strcmp(cmd, "WPS") == 0)
390 else if (os_strcmp(cmd, "NDEF") == 0)
395 if (os_strcmp(pos, "WPS-CR") == 0)
396 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
402 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
413 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
417 struct wpabuf *req, *sel;
419 char *pos, *role, *type, *pos2;
422 pos = os_strchr(role, ' ');
428 pos = os_strchr(type, ' ');
433 pos2 = os_strchr(pos, ' ');
438 len = os_strlen(pos);
443 req = wpabuf_alloc(len);
446 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
451 len = os_strlen(pos2);
458 sel = wpabuf_alloc(len);
463 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
469 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
470 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
472 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
473 "reported: role=%s type=%s", role, type);
482 #endif /* CONFIG_WPS_NFC */
485 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
486 char *buf, size_t buflen)
492 pos = os_strchr(txt, ' ');
496 if (os_strcmp(txt, "disable") == 0) {
497 hostapd_wps_ap_pin_disable(hapd);
498 return os_snprintf(buf, buflen, "OK\n");
501 if (os_strcmp(txt, "random") == 0) {
504 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
507 return os_snprintf(buf, buflen, "%s", pin_txt);
510 if (os_strcmp(txt, "get") == 0) {
511 pin_txt = hostapd_wps_ap_pin_get(hapd);
514 return os_snprintf(buf, buflen, "%s", pin_txt);
517 if (os_strcmp(txt, "set") == 0) {
522 pos = os_strchr(pos, ' ');
527 if (os_strlen(pin) > buflen)
529 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
531 return os_snprintf(buf, buflen, "%s", pin);
538 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
541 char *ssid, *auth, *encr = NULL, *key = NULL;
544 pos = os_strchr(txt, ' ');
550 pos = os_strchr(pos, ' ');
554 pos = os_strchr(pos, ' ');
561 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
565 static const char * pbc_status_str(enum pbc_status status)
568 case WPS_PBC_STATUS_DISABLE:
570 case WPS_PBC_STATUS_ACTIVE:
572 case WPS_PBC_STATUS_TIMEOUT:
574 case WPS_PBC_STATUS_OVERLAP:
582 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
583 char *buf, size_t buflen)
591 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
592 pbc_status_str(hapd->wps_stats.pbc_status));
594 if (os_snprintf_error(end - pos, ret))
598 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
599 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
601 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
602 "Failed" : "None")));
604 if (os_snprintf_error(end - pos, ret))
608 /* If status == Failure - Add possible Reasons */
609 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
610 hapd->wps_stats.failure_reason > 0) {
611 ret = os_snprintf(pos, end - pos,
612 "Failure Reason: %s\n",
613 wps_ei_str(hapd->wps_stats.failure_reason));
615 if (os_snprintf_error(end - pos, ret))
620 if (hapd->wps_stats.status) {
621 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
622 MAC2STR(hapd->wps_stats.peer_addr));
624 if (os_snprintf_error(end - pos, ret))
632 #endif /* CONFIG_WPS */
636 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
642 if (hwaddr_aton(cmd, addr))
655 return hs20_send_wnm_notification(hapd, addr, 1, url);
659 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
663 int code, reauth_delay, ret;
668 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
669 if (hwaddr_aton(cmd, addr))
672 pos = os_strchr(cmd, ' ');
678 pos = os_strchr(pos, ' ');
682 reauth_delay = atoi(pos);
685 pos = os_strchr(pos, ' ');
688 url_len = os_strlen(pos);
691 req = wpabuf_alloc(4 + url_len);
694 wpabuf_put_u8(req, code);
695 wpabuf_put_le16(req, reauth_delay);
696 wpabuf_put_u8(req, url_len);
698 wpabuf_put_data(req, pos, url_len);
700 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
701 " to indicate imminent deauthentication (code=%d "
702 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
703 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
708 #endif /* CONFIG_HS20 */
711 #ifdef CONFIG_INTERWORKING
713 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
716 u8 qos_map_set[16 + 2 * 21], count = 0;
717 const char *pos = cmd;
721 if (count == sizeof(qos_map_set)) {
722 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
727 if (val < 0 || val > 255) {
728 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
732 qos_map_set[count++] = val;
733 pos = os_strchr(pos, ',');
739 if (count < 16 || count & 1) {
740 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
744 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
746 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
750 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
751 hapd->conf->qos_map_set_len = count;
757 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
761 struct sta_info *sta;
763 u8 *qos_map_set = hapd->conf->qos_map_set;
764 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
767 if (!qos_map_set_len) {
768 wpa_printf(MSG_INFO, "QoS Map Set is not set");
772 if (hwaddr_aton(cmd, addr))
775 sta = ap_get_sta(hapd, addr);
777 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
778 "for QoS Map Configuration message",
783 if (!sta->qos_map_enabled) {
784 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
785 "support for QoS Map", MAC2STR(addr));
789 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
793 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
794 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
796 /* QoS Map Set Element */
797 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
798 wpabuf_put_u8(buf, qos_map_set_len);
799 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
801 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
802 wpabuf_head(buf), wpabuf_len(buf));
808 #endif /* CONFIG_INTERWORKING */
813 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
818 struct sta_info *sta;
820 if (hwaddr_aton(cmd, addr))
824 disassoc_timer = atoi(cmd + 17);
826 sta = ap_get_sta(hapd, addr);
828 wpa_printf(MSG_DEBUG, "Station " MACSTR
829 " not found for disassociation imminent message",
834 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
838 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
842 const char *url, *timerstr;
844 struct sta_info *sta;
846 if (hwaddr_aton(cmd, addr))
849 sta = ap_get_sta(hapd, addr);
851 wpa_printf(MSG_DEBUG, "Station " MACSTR
852 " not found for ESS disassociation imminent message",
858 if (*timerstr != ' ')
861 disassoc_timer = atoi(timerstr);
862 if (disassoc_timer < 0 || disassoc_timer > 65535)
865 url = os_strchr(timerstr, ' ');
870 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
874 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
878 const char *pos, *end;
879 int disassoc_timer = 0;
880 struct sta_info *sta;
881 u8 req_mode = 0, valid_int = 0x01;
886 u8 *nei_pos = nei_rep;
888 if (hwaddr_aton(cmd, addr)) {
889 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
893 sta = ap_get_sta(hapd, addr);
895 wpa_printf(MSG_DEBUG, "Station " MACSTR
896 " not found for BSS TM Request message",
901 pos = os_strstr(cmd, " disassoc_timer=");
904 disassoc_timer = atoi(pos);
905 if (disassoc_timer < 0 || disassoc_timer > 65535) {
906 wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
911 pos = os_strstr(cmd, " valid_int=");
914 valid_int = atoi(pos);
917 pos = os_strstr(cmd, " bss_term=");
920 req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
921 /* TODO: TSF configurable/learnable */
922 bss_term_dur[0] = 4; /* Subelement ID */
923 bss_term_dur[1] = 10; /* Length */
924 os_memset(bss_term_dur, 2, 8);
925 end = os_strchr(pos, ',');
927 wpa_printf(MSG_DEBUG, "Invalid bss_term data");
931 WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
936 * BSS Transition Candidate List Entries - Neighbor Report elements
937 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
938 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
946 pos = os_strstr(pos, " neighbor=");
949 if (nei_pos + 15 > nei_rep + sizeof(nei_rep)) {
950 wpa_printf(MSG_DEBUG,
951 "Not enough room for additional neighbor");
957 *nei_pos++ = WLAN_EID_NEIGHBOR_REPORT;
958 nei_pos++; /* length to be filled in */
960 if (hwaddr_aton(pos, nei_pos)) {
961 wpa_printf(MSG_DEBUG, "Invalid BSSID");
967 wpa_printf(MSG_DEBUG, "Missing BSSID Information");
972 val = strtol(pos, &endptr, 0);
973 WPA_PUT_LE32(nei_pos, val);
975 if (*endptr != ',') {
976 wpa_printf(MSG_DEBUG, "Missing Operating Class");
981 *nei_pos++ = atoi(pos); /* Operating Class */
982 pos = os_strchr(pos, ',');
984 wpa_printf(MSG_DEBUG, "Missing Channel Number");
989 *nei_pos++ = atoi(pos); /* Channel Number */
990 pos = os_strchr(pos, ',');
992 wpa_printf(MSG_DEBUG, "Missing PHY Type");
997 *nei_pos++ = atoi(pos); /* PHY Type */
998 end = os_strchr(pos, ' ');
999 tmp = os_strchr(pos, ',');
1000 if (tmp && (!end || tmp < end)) {
1001 /* Optional Subelements (hexdump) */
1005 end = os_strchr(pos, ' ');
1009 len = os_strlen(pos);
1010 if (nei_pos + len / 2 > nei_rep + sizeof(nei_rep)) {
1011 wpa_printf(MSG_DEBUG,
1012 "Not enough room for neighbor subelements");
1016 hexstr2bin(pos, nei_pos, len / 2) < 0) {
1017 wpa_printf(MSG_DEBUG,
1018 "Invalid neighbor subelement info");
1025 nei_start[1] = nei_pos - nei_start - 2;
1028 pos = os_strstr(cmd, " url=");
1032 end = os_strchr(pos, ' ');
1036 len = os_strlen(pos);
1037 url = os_malloc(len + 1);
1040 os_memcpy(url, pos, len);
1042 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
1045 if (os_strstr(cmd, " pref=1"))
1046 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1047 if (os_strstr(cmd, " abridged=1"))
1048 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1049 if (os_strstr(cmd, " disassoc_imminent=1"))
1050 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1052 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
1053 valid_int, bss_term_dur, url,
1054 nei_pos > nei_rep ? nei_rep : NULL,
1060 #endif /* CONFIG_WNM */
1063 static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
1064 char *buf, size_t buflen)
1072 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
1074 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1075 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1076 if (os_snprintf_error(end - pos, ret))
1080 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1081 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1082 if (os_snprintf_error(end - pos, ret))
1086 #ifdef CONFIG_IEEE80211R
1087 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1088 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1089 if (os_snprintf_error(end - pos, ret))
1093 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1094 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1095 if (os_snprintf_error(end - pos, ret))
1100 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1101 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1102 if (os_snprintf_error(end - pos, ret))
1106 #endif /* CONFIG_SAE */
1107 #endif /* CONFIG_IEEE80211R */
1108 #ifdef CONFIG_IEEE80211W
1109 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1110 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1111 if (os_snprintf_error(end - pos, ret))
1115 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1116 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1117 if (os_snprintf_error(end - pos, ret))
1121 #endif /* CONFIG_IEEE80211W */
1123 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1124 ret = os_snprintf(pos, end - pos, "SAE ");
1125 if (os_snprintf_error(end - pos, ret))
1129 #endif /* CONFIG_SAE */
1130 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1131 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1132 if (os_snprintf_error(end - pos, ret))
1136 if (hapd->conf->wpa_key_mgmt &
1137 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1138 ret = os_snprintf(pos, end - pos,
1139 "WPA-EAP-SUITE-B-192 ");
1140 if (os_snprintf_error(end - pos, ret))
1145 if (pos > buf && *(pos - 1) == ' ') {
1154 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1155 char *buf, size_t buflen)
1163 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1165 MAC2STR(hapd->own_addr),
1166 wpa_ssid_txt(hapd->conf->ssid.ssid,
1167 hapd->conf->ssid.ssid_len));
1168 if (os_snprintf_error(end - pos, ret))
1173 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1174 hapd->conf->wps_state == 0 ? "disabled" :
1175 (hapd->conf->wps_state == 1 ? "not configured" :
1177 if (os_snprintf_error(end - pos, ret))
1181 if (hapd->conf->wps_state && hapd->conf->wpa &&
1182 hapd->conf->ssid.wpa_passphrase) {
1183 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1184 hapd->conf->ssid.wpa_passphrase);
1185 if (os_snprintf_error(end - pos, ret))
1190 if (hapd->conf->wps_state && hapd->conf->wpa &&
1191 hapd->conf->ssid.wpa_psk &&
1192 hapd->conf->ssid.wpa_psk->group) {
1193 char hex[PMK_LEN * 2 + 1];
1194 wpa_snprintf_hex(hex, sizeof(hex),
1195 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1196 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
1197 if (os_snprintf_error(end - pos, ret))
1201 #endif /* CONFIG_WPS */
1203 if (hapd->conf->wpa) {
1204 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1205 if (os_snprintf_error(end - pos, ret))
1210 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1211 ret = os_snprintf(pos, end - pos, "key_mgmt=");
1212 if (os_snprintf_error(end - pos, ret))
1216 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
1218 ret = os_snprintf(pos, end - pos, "\n");
1219 if (os_snprintf_error(end - pos, ret))
1224 if (hapd->conf->wpa) {
1225 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1226 wpa_cipher_txt(hapd->conf->wpa_group));
1227 if (os_snprintf_error(end - pos, ret))
1232 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1233 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
1234 if (os_snprintf_error(end - pos, ret))
1238 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1244 ret = os_snprintf(pos, end - pos, "\n");
1245 if (os_snprintf_error(end - pos, ret))
1250 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1251 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
1252 if (os_snprintf_error(end - pos, ret))
1256 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
1262 ret = os_snprintf(pos, end - pos, "\n");
1263 if (os_snprintf_error(end - pos, ret))
1272 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1277 value = os_strchr(cmd, ' ');
1282 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1284 #ifdef CONFIG_WPS_TESTING
1285 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1287 val = strtol(value, NULL, 0);
1288 if (val < 0 || val > 0xff) {
1290 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1291 "wps_version_number %ld", val);
1293 wps_version_number = val;
1294 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1296 (wps_version_number & 0xf0) >> 4,
1297 wps_version_number & 0x0f);
1298 hostapd_wps_update_ie(hapd);
1300 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1301 wps_testing_dummy_cred = atoi(value);
1302 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1303 wps_testing_dummy_cred);
1304 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1305 wps_corrupt_pkhash = atoi(value);
1306 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1307 wps_corrupt_pkhash);
1308 #endif /* CONFIG_WPS_TESTING */
1309 #ifdef CONFIG_INTERWORKING
1310 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1311 int val = atoi(value);
1315 hapd->gas_frag_limit = val;
1316 #endif /* CONFIG_INTERWORKING */
1317 #ifdef CONFIG_TESTING_OPTIONS
1318 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1319 hapd->ext_mgmt_frame_handling = atoi(value);
1320 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1321 hapd->ext_eapol_frame_io = atoi(value);
1322 #endif /* CONFIG_TESTING_OPTIONS */
1324 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1327 if (!hapd->conf->mbo_enabled)
1331 if (val < 0 || val > 1)
1334 hapd->mbo_assoc_disallow = val;
1335 ieee802_11_update_beacons(hapd->iface);
1338 * TODO: Need to configure drivers that do AP MLME offload with
1339 * disallowing station logic.
1341 #endif /* CONFIG_MBO */
1343 struct sta_info *sta;
1344 struct vlan_description vlan_id;
1346 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1350 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1351 for (sta = hapd->sta_list; sta; sta = sta->next) {
1352 if (hostapd_maclist_found(
1353 hapd->conf->deny_mac,
1354 hapd->conf->num_deny_mac, sta->addr,
1356 (!vlan_id.notempty ||
1357 !vlan_compare(&vlan_id, sta->vlan_desc)))
1359 hapd, sta, sta->addr,
1360 WLAN_REASON_UNSPECIFIED);
1362 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1363 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1364 for (sta = hapd->sta_list; sta; sta = sta->next) {
1365 if (!hostapd_maclist_found(
1366 hapd->conf->accept_mac,
1367 hapd->conf->num_accept_mac,
1368 sta->addr, &vlan_id) ||
1369 (vlan_id.notempty &&
1370 vlan_compare(&vlan_id, sta->vlan_desc)))
1372 hapd, sta, sta->addr,
1373 WLAN_REASON_UNSPECIFIED);
1382 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1383 char *buf, size_t buflen)
1387 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1389 if (os_strcmp(cmd, "version") == 0) {
1390 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1391 if (os_snprintf_error(buflen, res))
1394 } else if (os_strcmp(cmd, "tls_library") == 0) {
1395 res = tls_get_library_version(buf, buflen);
1396 if (os_snprintf_error(buflen, res))
1405 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1407 if (hostapd_enable_iface(iface) < 0) {
1408 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1415 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1417 if (hostapd_reload_iface(iface) < 0) {
1418 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1425 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1427 if (hostapd_disable_iface(iface) < 0) {
1428 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1435 #ifdef CONFIG_TESTING_OPTIONS
1437 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1439 union wpa_event_data data;
1441 enum wpa_event_type event;
1443 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1445 os_memset(&data, 0, sizeof(data));
1447 param = os_strchr(cmd, ' ');
1452 if (os_strcmp(cmd, "DETECTED") == 0)
1453 event = EVENT_DFS_RADAR_DETECTED;
1454 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1455 event = EVENT_DFS_CAC_FINISHED;
1456 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1457 event = EVENT_DFS_CAC_ABORTED;
1458 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1459 event = EVENT_DFS_NOP_FINISHED;
1461 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1466 pos = os_strstr(param, "freq=");
1468 data.dfs_event.freq = atoi(pos + 5);
1470 pos = os_strstr(param, "ht_enabled=1");
1472 data.dfs_event.ht_enabled = 1;
1474 pos = os_strstr(param, "chan_offset=");
1476 data.dfs_event.chan_offset = atoi(pos + 12);
1478 pos = os_strstr(param, "chan_width=");
1480 data.dfs_event.chan_width = atoi(pos + 11);
1482 pos = os_strstr(param, "cf1=");
1484 data.dfs_event.cf1 = atoi(pos + 4);
1486 pos = os_strstr(param, "cf2=");
1488 data.dfs_event.cf2 = atoi(pos + 4);
1490 wpa_supplicant_event(hapd, event, &data);
1496 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1502 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1504 len = os_strlen(cmd);
1509 buf = os_malloc(len);
1513 if (hexstr2bin(cmd, buf, len) < 0) {
1518 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1524 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1527 u8 src[ETH_ALEN], *buf;
1531 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1534 used = hwaddr_aton2(pos, src);
1541 len = os_strlen(pos);
1546 buf = os_malloc(len);
1550 if (hexstr2bin(pos, buf, len) < 0) {
1555 ieee802_1x_receive(hapd, src, buf, len);
1562 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1566 const u16 *pos = buf;
1568 for (i = 0; i < len / 2; i++)
1572 sum = (sum & 0xffff) + (sum >> 16);
1574 return sum ^ 0xffff;
1578 #define HWSIM_PACKETLEN 1500
1579 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1581 void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1584 struct hostapd_data *hapd = ctx;
1585 const struct ether_header *eth;
1590 if (len != HWSIM_PACKETLEN)
1593 eth = (const struct ether_header *) buf;
1594 os_memcpy(&ip, eth + 1, sizeof(ip));
1595 pos = &buf[sizeof(*eth) + sizeof(ip)];
1597 if (ip.ihl != 5 || ip.version != 4 ||
1598 ntohs(ip.tot_len) != HWSIM_IP_LEN)
1601 for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
1607 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
1608 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
1612 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1615 int enabled = atoi(cmd);
1620 if (hapd->l2_test) {
1621 l2_packet_deinit(hapd->l2_test);
1622 hapd->l2_test = NULL;
1623 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1624 "test data: Disabled");
1632 pos = os_strstr(cmd, " ifname=");
1636 ifname = hapd->conf->iface;
1638 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1639 ETHERTYPE_IP, hostapd_data_test_rx,
1641 if (hapd->l2_test == NULL)
1644 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1650 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1652 u8 dst[ETH_ALEN], src[ETH_ALEN];
1657 u8 buf[2 + HWSIM_PACKETLEN];
1658 struct ether_header *eth;
1663 if (hapd->l2_test == NULL)
1666 /* format: <dst> <src> <tos> */
1669 used = hwaddr_aton2(pos, dst);
1675 used = hwaddr_aton2(pos, src);
1680 val = strtol(pos, NULL, 0);
1681 if (val < 0 || val > 0xff)
1685 eth = (struct ether_header *) &buf[2];
1686 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1687 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1688 eth->ether_type = htons(ETHERTYPE_IP);
1689 ip = (struct iphdr *) (eth + 1);
1690 os_memset(ip, 0, sizeof(*ip));
1695 ip->tot_len = htons(HWSIM_IP_LEN);
1697 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1698 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1699 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
1700 dpos = (u8 *) (ip + 1);
1701 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
1704 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
1705 HWSIM_PACKETLEN) < 0)
1708 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1709 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1715 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1719 struct ether_header *eth;
1720 struct l2_packet_data *l2 = NULL;
1724 const char *ifname = hapd->conf->iface;
1726 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1729 cmd = os_strchr(cmd, ' ');
1735 len = os_strlen(cmd);
1736 if (len & 1 || len < ETH_HLEN * 2)
1740 buf = os_malloc(len);
1744 if (hexstr2bin(cmd, buf, len) < 0)
1747 eth = (struct ether_header *) buf;
1748 ethertype = ntohs(eth->ether_type);
1750 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1751 hostapd_data_test_rx, hapd, 1);
1755 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1756 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1759 l2_packet_deinit(l2);
1762 return res < 0 ? -1 : 0;
1766 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
1768 #ifdef WPA_TRACE_BFD
1769 extern char wpa_trace_fail_func[256];
1770 extern unsigned int wpa_trace_fail_after;
1773 wpa_trace_fail_after = atoi(cmd);
1774 pos = os_strchr(cmd, ':');
1777 os_strlcpy(wpa_trace_fail_func, pos,
1778 sizeof(wpa_trace_fail_func));
1780 wpa_trace_fail_after = 0;
1784 #else /* WPA_TRACE_BFD */
1786 #endif /* WPA_TRACE_BFD */
1790 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
1791 char *buf, size_t buflen)
1793 #ifdef WPA_TRACE_BFD
1794 extern char wpa_trace_fail_func[256];
1795 extern unsigned int wpa_trace_fail_after;
1797 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
1798 wpa_trace_fail_func);
1799 #else /* WPA_TRACE_BFD */
1801 #endif /* WPA_TRACE_BFD */
1805 static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
1807 #ifdef WPA_TRACE_BFD
1808 extern char wpa_trace_test_fail_func[256];
1809 extern unsigned int wpa_trace_test_fail_after;
1812 wpa_trace_test_fail_after = atoi(cmd);
1813 pos = os_strchr(cmd, ':');
1816 os_strlcpy(wpa_trace_test_fail_func, pos,
1817 sizeof(wpa_trace_test_fail_func));
1819 wpa_trace_test_fail_after = 0;
1823 #else /* WPA_TRACE_BFD */
1825 #endif /* WPA_TRACE_BFD */
1829 static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
1830 char *buf, size_t buflen)
1832 #ifdef WPA_TRACE_BFD
1833 extern char wpa_trace_test_fail_func[256];
1834 extern unsigned int wpa_trace_test_fail_after;
1836 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
1837 wpa_trace_test_fail_func);
1838 #else /* WPA_TRACE_BFD */
1840 #endif /* WPA_TRACE_BFD */
1843 #endif /* CONFIG_TESTING_OPTIONS */
1846 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
1850 struct csa_settings settings;
1854 ret = hostapd_parse_csa_settings(pos, &settings);
1858 for (i = 0; i < iface->num_bss; i++) {
1859 ret = hostapd_switch_channel(iface->bss[i], &settings);
1861 /* FIX: What do we do if CSA fails in the middle of
1862 * submitting multi-BSS CSA requests? */
1868 #else /* NEED_AP_MLME */
1870 #endif /* NEED_AP_MLME */
1874 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1875 int reply_size, const char *param)
1877 #ifdef RADIUS_SERVER
1878 if (os_strcmp(param, "radius_server") == 0) {
1879 return radius_server_get_mib(hapd->radius_srv, reply,
1882 #endif /* RADIUS_SERVER */
1887 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
1888 char *buf, size_t buflen)
1893 unsigned int vendor_id, subcmd;
1894 struct wpabuf *reply;
1895 size_t data_len = 0;
1897 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1898 vendor_id = strtoul(cmd, &pos, 16);
1899 if (!isblank((unsigned char) *pos))
1902 subcmd = strtoul(pos, &pos, 10);
1905 if (!isblank((unsigned char) *pos++))
1907 data_len = os_strlen(pos);
1912 data = os_malloc(data_len);
1916 if (hexstr2bin(pos, data, data_len)) {
1917 wpa_printf(MSG_DEBUG,
1918 "Vendor command: wrong parameter format");
1924 reply = wpabuf_alloc((buflen - 1) / 2);
1930 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
1934 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
1944 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
1948 struct sta_info *sta;
1950 if (hwaddr_aton(cmd, addr))
1953 sta = ap_get_sta(hapd, addr);
1954 if (!sta || !sta->eapol_sm)
1957 eapol_auth_reauthenticate(sta->eapol_sm);
1962 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
1965 struct sta_info *sta;
1966 char *pos = cmd, *param;
1968 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
1972 pos = os_strchr(pos, ' ');
1977 sta = ap_get_sta(hapd, addr);
1978 if (!sta || !sta->eapol_sm)
1981 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
1985 static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
1986 char *buf, size_t buflen)
1988 char *pos, *end, *stamp;
1991 /* cmd: "LOG_LEVEL [<level>]" */
1995 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1997 debug_level_str(wpa_debug_level),
1998 wpa_debug_timestamp);
1999 if (os_snprintf_error(end - pos, ret))
2008 stamp = os_strchr(cmd, ' ');
2011 while (*stamp == ' ') {
2016 if (os_strlen(cmd)) {
2017 int level = str_to_debug_level(cmd);
2020 wpa_debug_level = level;
2023 if (stamp && os_strlen(stamp))
2024 wpa_debug_timestamp = atoi(stamp);
2026 os_memcpy(buf, "OK\n", 3);
2032 static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2033 char *buf, size_t buflen)
2035 struct hostapd_iface *iface = hapd->iface;
2037 struct hostapd_sta_info *info;
2038 struct os_reltime now;
2040 sta_track_expire(iface, 0);
2045 os_get_reltime(&now);
2046 dl_list_for_each_reverse(info, &iface->sta_seen,
2047 struct hostapd_sta_info, list) {
2048 struct os_reltime age;
2051 os_reltime_sub(&now, &info->last_seen, &age);
2052 ret = os_snprintf(pos, end - pos, MACSTR " %u\n",
2053 MAC2STR(info->addr), (unsigned int) age.sec);
2054 if (os_snprintf_error(end - pos, ret))
2061 #endif /* NEED_AP_MLME */
2064 static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
2065 char *buf, char *reply,
2067 struct sockaddr_un *from,
2072 os_memcpy(reply, "OK\n", 3);
2075 if (os_strcmp(buf, "PING") == 0) {
2076 os_memcpy(reply, "PONG\n", 5);
2078 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2079 if (wpa_debug_reopen_file() < 0)
2081 } else if (os_strcmp(buf, "STATUS") == 0) {
2082 reply_len = hostapd_ctrl_iface_status(hapd, reply,
2084 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
2085 reply_len = hostapd_drv_status(hapd, reply, reply_size);
2086 } else if (os_strcmp(buf, "MIB") == 0) {
2087 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
2088 if (reply_len >= 0) {
2089 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
2090 reply_size - reply_len);
2096 if (reply_len >= 0) {
2097 res = ieee802_1x_get_mib(hapd, reply + reply_len,
2098 reply_size - reply_len);
2104 #ifndef CONFIG_NO_RADIUS
2105 if (reply_len >= 0) {
2106 res = radius_client_get_mib(hapd->radius,
2108 reply_size - reply_len);
2114 #endif /* CONFIG_NO_RADIUS */
2115 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
2116 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
2118 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
2119 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
2121 } else if (os_strncmp(buf, "STA ", 4) == 0) {
2122 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
2124 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
2125 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
2127 } else if (os_strcmp(buf, "ATTACH") == 0) {
2128 if (hostapd_ctrl_iface_attach(hapd, from, fromlen))
2130 } else if (os_strcmp(buf, "DETACH") == 0) {
2131 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
2133 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
2134 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
2137 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
2138 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
2140 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
2141 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
2143 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
2144 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
2146 } else if (os_strcmp(buf, "STOP_AP") == 0) {
2147 if (hostapd_ctrl_iface_stop_ap(hapd))
2149 #ifdef CONFIG_IEEE80211W
2151 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
2152 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
2154 #endif /* NEED_AP_MLME */
2155 #endif /* CONFIG_IEEE80211W */
2157 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
2158 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
2160 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
2161 reply_len = hostapd_ctrl_iface_wps_check_pin(
2162 hapd, buf + 14, reply, reply_size);
2163 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
2164 if (hostapd_wps_button_pushed(hapd, NULL))
2166 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
2167 if (hostapd_wps_cancel(hapd))
2169 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
2170 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
2172 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
2173 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
2175 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
2176 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
2178 #ifdef CONFIG_WPS_NFC
2179 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
2180 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
2182 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2183 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
2184 hapd, buf + 21, reply, reply_size);
2185 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
2186 reply_len = hostapd_ctrl_iface_wps_nfc_token(
2187 hapd, buf + 14, reply, reply_size);
2188 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2189 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
2190 hapd, buf + 21, reply, reply_size);
2191 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
2192 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
2194 #endif /* CONFIG_WPS_NFC */
2195 #endif /* CONFIG_WPS */
2196 #ifdef CONFIG_INTERWORKING
2197 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
2198 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
2200 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
2201 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
2203 #endif /* CONFIG_INTERWORKING */
2205 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
2206 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
2208 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
2209 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
2211 #endif /* CONFIG_HS20 */
2213 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
2214 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
2216 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
2217 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
2219 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
2220 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
2222 #endif /* CONFIG_WNM */
2223 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
2224 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
2226 } else if (os_strncmp(buf, "SET ", 4) == 0) {
2227 if (hostapd_ctrl_iface_set(hapd, buf + 4))
2229 } else if (os_strncmp(buf, "GET ", 4) == 0) {
2230 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
2232 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
2233 if (hostapd_ctrl_iface_enable(hapd->iface))
2235 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
2236 if (hostapd_ctrl_iface_reload(hapd->iface))
2238 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
2239 if (hostapd_ctrl_iface_disable(hapd->iface))
2241 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
2242 if (ieee802_11_set_beacon(hapd))
2244 #ifdef CONFIG_TESTING_OPTIONS
2245 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
2246 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
2248 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
2249 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
2251 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
2252 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
2254 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
2255 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
2257 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
2258 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
2260 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
2261 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
2263 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
2264 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
2266 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
2267 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
2269 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
2270 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
2272 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
2273 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
2274 #endif /* CONFIG_TESTING_OPTIONS */
2275 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
2276 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
2278 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
2279 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
2281 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
2282 ieee802_1x_erp_flush(hapd);
2283 #ifdef RADIUS_SERVER
2284 radius_server_erp_flush(hapd->radius_srv);
2285 #endif /* RADIUS_SERVER */
2286 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
2287 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
2289 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
2290 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
2292 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
2293 reply_len = hostapd_ctrl_iface_log_level(
2294 hapd, buf + 9, reply, reply_size);
2296 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
2297 reply_len = hostapd_ctrl_iface_track_sta_list(
2298 hapd, reply, reply_size);
2299 #endif /* NEED_AP_MLME */
2301 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2305 if (reply_len < 0) {
2306 os_memcpy(reply, "FAIL\n", 5);
2314 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
2317 struct hostapd_data *hapd = eloop_ctx;
2320 struct sockaddr_un from;
2321 socklen_t fromlen = sizeof(from);
2323 const int reply_size = 4096;
2325 int level = MSG_DEBUG;
2327 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2328 (struct sockaddr *) &from, &fromlen);
2330 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2335 if (os_strcmp(buf, "PING") == 0)
2336 level = MSG_EXCESSIVE;
2337 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
2339 reply = os_malloc(reply_size);
2340 if (reply == NULL) {
2341 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
2343 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2349 reply_len = hostapd_ctrl_iface_receive_process(hapd, buf,
2353 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2355 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2362 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
2367 if (hapd->conf->ctrl_interface == NULL)
2370 len = os_strlen(hapd->conf->ctrl_interface) +
2371 os_strlen(hapd->conf->iface) + 2;
2372 buf = os_malloc(len);
2376 os_snprintf(buf, len, "%s/%s",
2377 hapd->conf->ctrl_interface, hapd->conf->iface);
2378 buf[len - 1] = '\0';
2383 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
2384 enum wpa_msg_type type,
2385 const char *txt, size_t len)
2387 struct hostapd_data *hapd = ctx;
2390 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
2394 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
2396 struct sockaddr_un addr;
2400 if (hapd->ctrl_sock > -1) {
2401 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2405 if (hapd->conf->ctrl_interface == NULL)
2408 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2409 if (errno == EEXIST) {
2410 wpa_printf(MSG_DEBUG, "Using existing control "
2411 "interface directory.");
2413 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2419 if (hapd->conf->ctrl_interface_gid_set &&
2420 chown(hapd->conf->ctrl_interface, -1,
2421 hapd->conf->ctrl_interface_gid) < 0) {
2422 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2427 if (!hapd->conf->ctrl_interface_gid_set &&
2428 hapd->iface->interfaces->ctrl_iface_group &&
2429 chown(hapd->conf->ctrl_interface, -1,
2430 hapd->iface->interfaces->ctrl_iface_group) < 0) {
2431 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2438 * Android is using umask 0077 which would leave the control interface
2439 * directory without group access. This breaks things since Wi-Fi
2440 * framework assumes that this directory can be accessed by other
2441 * applications in the wifi group. Fix this by adding group access even
2442 * if umask value would prevent this.
2444 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2445 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
2447 /* Try to continue anyway */
2449 #endif /* ANDROID */
2451 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
2452 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
2455 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2457 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
2461 os_memset(&addr, 0, sizeof(addr));
2463 addr.sun_len = sizeof(addr);
2464 #endif /* __FreeBSD__ */
2465 addr.sun_family = AF_UNIX;
2466 fname = hostapd_ctrl_iface_path(hapd);
2469 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2470 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2471 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2473 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2474 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2475 " allow connections - assuming it was left"
2476 "over from forced program termination");
2477 if (unlink(fname) < 0) {
2478 wpa_printf(MSG_ERROR,
2479 "Could not unlink existing ctrl_iface socket '%s': %s",
2480 fname, strerror(errno));
2483 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2485 wpa_printf(MSG_ERROR,
2486 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
2490 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2491 "ctrl_iface socket '%s'", fname);
2493 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2494 "be in use - cannot override it");
2495 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2496 "not used anymore", fname);
2503 if (hapd->conf->ctrl_interface_gid_set &&
2504 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
2505 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2510 if (!hapd->conf->ctrl_interface_gid_set &&
2511 hapd->iface->interfaces->ctrl_iface_group &&
2512 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
2513 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2518 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2519 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2525 hapd->ctrl_sock = s;
2526 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
2528 hostapd_ctrl_iface_deinit(hapd);
2531 hapd->msg_ctx = hapd;
2532 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
2547 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
2549 struct wpa_ctrl_dst *dst, *prev;
2551 if (hapd->ctrl_sock > -1) {
2553 eloop_unregister_read_sock(hapd->ctrl_sock);
2554 close(hapd->ctrl_sock);
2555 hapd->ctrl_sock = -1;
2556 fname = hostapd_ctrl_iface_path(hapd);
2561 if (hapd->conf->ctrl_interface &&
2562 rmdir(hapd->conf->ctrl_interface) < 0) {
2563 if (errno == ENOTEMPTY) {
2564 wpa_printf(MSG_DEBUG, "Control interface "
2565 "directory not empty - leaving it "
2568 wpa_printf(MSG_ERROR,
2569 "rmdir[ctrl_interface=%s]: %s",
2570 hapd->conf->ctrl_interface,
2576 dst = hapd->ctrl_dst;
2577 hapd->ctrl_dst = NULL;
2584 #ifdef CONFIG_TESTING_OPTIONS
2585 l2_packet_deinit(hapd->l2_test);
2586 hapd->l2_test = NULL;
2587 #endif /* CONFIG_TESTING_OPTIONS */
2591 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
2594 if (hostapd_add_iface(interfaces, buf) < 0) {
2595 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
2602 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
2605 if (hostapd_remove_iface(interfaces, buf) < 0) {
2606 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
2613 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
2614 struct sockaddr_un *from,
2617 struct wpa_ctrl_dst *dst;
2619 dst = os_zalloc(sizeof(*dst));
2622 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
2623 dst->addrlen = fromlen;
2624 dst->debug_level = MSG_INFO;
2625 dst->next = interfaces->global_ctrl_dst;
2626 interfaces->global_ctrl_dst = dst;
2627 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached (global)",
2629 fromlen - offsetof(struct sockaddr_un, sun_path));
2634 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
2635 struct sockaddr_un *from,
2638 struct wpa_ctrl_dst *dst, *prev = NULL;
2640 dst = interfaces->global_ctrl_dst;
2642 if (fromlen == dst->addrlen &&
2643 os_memcmp(from->sun_path, dst->addr.sun_path,
2644 fromlen - offsetof(struct sockaddr_un, sun_path))
2646 wpa_hexdump(MSG_DEBUG,
2647 "CTRL_IFACE monitor detached (global)",
2650 offsetof(struct sockaddr_un, sun_path));
2652 interfaces->global_ctrl_dst = dst->next;
2654 prev->next = dst->next;
2665 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
2667 #ifdef CONFIG_WPS_TESTING
2668 wps_version_number = 0x20;
2669 wps_testing_dummy_cred = 0;
2670 wps_corrupt_pkhash = 0;
2671 #endif /* CONFIG_WPS_TESTING */
2678 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
2681 char ifname[IFNAMSIZ + 1];
2682 struct fst_iface_cfg cfg;
2683 struct hostapd_data *hapd;
2684 struct fst_wpa_obj iface_obj;
2686 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
2687 hapd = hostapd_get_iface(interfaces, ifname);
2689 if (hapd->iface->fst) {
2690 wpa_printf(MSG_INFO, "FST: Already attached");
2693 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2694 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
2696 if (hapd->iface->fst)
2706 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
2709 char ifname[IFNAMSIZ + 1];
2710 struct hostapd_data * hapd;
2712 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
2713 hapd = hostapd_get_iface(interfaces, ifname);
2715 if (!fst_iface_detach(ifname)) {
2716 hapd->iface->fst = NULL;
2717 hapd->iface->fst_ies = NULL;
2726 #endif /* CONFIG_FST */
2729 static struct hostapd_data *
2730 hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
2735 for (i = 0; i < interfaces->count; i++) {
2736 struct hostapd_iface *iface = interfaces->iface[i];
2738 for (j = 0; j < iface->num_bss; j++) {
2739 struct hostapd_data *hapd;
2741 hapd = iface->bss[j];
2742 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2751 static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
2752 struct hostapd_data *dst_hapd,
2758 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
2760 wpa_printf(MSG_ERROR,
2761 "DUP: cannot allocate buffer to stringify %s",
2766 if (os_strcmp(param, "wpa") == 0) {
2767 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
2768 src_hapd->conf->wpa);
2769 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
2770 src_hapd->conf->wpa_key_mgmt) {
2771 res = hostapd_ctrl_iface_get_key_mgmt(
2772 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
2773 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
2774 goto error_stringify;
2775 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
2776 src_hapd->conf->wpa_pairwise) {
2777 res = wpa_write_ciphers(value,
2778 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
2779 src_hapd->conf->wpa_pairwise, " ");
2781 goto error_stringify;
2782 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
2783 src_hapd->conf->rsn_pairwise) {
2784 res = wpa_write_ciphers(value,
2785 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
2786 src_hapd->conf->rsn_pairwise, " ");
2788 goto error_stringify;
2789 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
2790 src_hapd->conf->ssid.wpa_passphrase) {
2791 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
2792 src_hapd->conf->ssid.wpa_passphrase);
2793 } else if (os_strcmp(param, "wpa_psk") == 0 &&
2794 src_hapd->conf->ssid.wpa_psk_set) {
2795 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
2796 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
2798 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
2802 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
2807 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
2815 hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
2818 char *p_start = cmd, *p_end;
2819 struct hostapd_data *src_hapd, *dst_hapd;
2821 /* cmd: "<src ifname> <dst ifname> <variable name> */
2823 p_end = os_strchr(p_start, ' ');
2825 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
2831 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
2833 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
2838 p_start = p_end + 1;
2839 p_end = os_strchr(p_start, ' ');
2841 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
2847 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
2849 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
2854 p_start = p_end + 1;
2855 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
2859 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
2861 char *buf, char *reply,
2863 struct sockaddr_un *from,
2866 struct hostapd_data *hapd;
2868 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
2872 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
2873 if (os_snprintf_error(reply_size, res))
2878 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
2883 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
2886 void *interfaces = eloop_ctx;
2889 struct sockaddr_un from;
2890 socklen_t fromlen = sizeof(from);
2893 const int reply_size = 4096;
2895 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2896 (struct sockaddr *) &from, &fromlen);
2898 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2903 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
2905 reply = os_malloc(reply_size);
2906 if (reply == NULL) {
2907 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
2909 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2915 os_memcpy(reply, "OK\n", 3);
2918 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
2919 char *pos = os_strchr(buf + 7, ' ');
2923 reply_len = hostapd_global_ctrl_iface_ifname(
2924 interfaces, buf + 7, pos, reply, reply_size,
2930 if (os_strcmp(buf, "PING") == 0) {
2931 os_memcpy(reply, "PONG\n", 5);
2933 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2934 if (wpa_debug_reopen_file() < 0)
2936 } else if (os_strcmp(buf, "FLUSH") == 0) {
2937 hostapd_ctrl_iface_flush(interfaces);
2938 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
2939 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
2941 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
2942 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
2944 } else if (os_strcmp(buf, "ATTACH") == 0) {
2945 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
2948 } else if (os_strcmp(buf, "DETACH") == 0) {
2949 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
2952 #ifdef CONFIG_MODULE_TESTS
2953 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
2954 int hapd_module_tests(void);
2955 if (hapd_module_tests() < 0)
2957 #endif /* CONFIG_MODULE_TESTS */
2959 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
2960 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
2961 reply_len = os_snprintf(reply, reply_size, "OK\n");
2964 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
2965 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
2966 reply_len = os_snprintf(reply, reply_size, "OK\n");
2969 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
2970 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
2971 #endif /* CONFIG_FST */
2972 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
2973 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
2975 reply_len = os_snprintf(reply, reply_size, "OK\n");
2979 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
2985 if (reply_len < 0) {
2986 os_memcpy(reply, "FAIL\n", 5);
2990 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2992 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2999 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
3004 if (interface->global_iface_path == NULL)
3007 len = os_strlen(interface->global_iface_path) +
3008 os_strlen(interface->global_iface_name) + 2;
3009 buf = os_malloc(len);
3013 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
3014 interface->global_iface_name);
3015 buf[len - 1] = '\0';
3020 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
3022 struct sockaddr_un addr;
3026 if (interface->global_iface_path == NULL) {
3027 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
3031 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
3032 if (errno == EEXIST) {
3033 wpa_printf(MSG_DEBUG, "Using existing control "
3034 "interface directory.");
3036 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3040 } else if (interface->ctrl_iface_group &&
3041 chown(interface->global_iface_path, -1,
3042 interface->ctrl_iface_group) < 0) {
3043 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3048 if (os_strlen(interface->global_iface_path) + 1 +
3049 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
3052 s = socket(PF_UNIX, SOCK_DGRAM, 0);
3054 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
3058 os_memset(&addr, 0, sizeof(addr));
3060 addr.sun_len = sizeof(addr);
3061 #endif /* __FreeBSD__ */
3062 addr.sun_family = AF_UNIX;
3063 fname = hostapd_global_ctrl_iface_path(interface);
3066 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
3067 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3068 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
3070 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3071 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
3072 " allow connections - assuming it was left"
3073 "over from forced program termination");
3074 if (unlink(fname) < 0) {
3075 wpa_printf(MSG_ERROR,
3076 "Could not unlink existing ctrl_iface socket '%s': %s",
3077 fname, strerror(errno));
3080 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
3082 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
3086 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
3087 "ctrl_iface socket '%s'", fname);
3089 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
3090 "be in use - cannot override it");
3091 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
3092 "not used anymore", fname);
3099 if (interface->ctrl_iface_group &&
3100 chown(fname, -1, interface->ctrl_iface_group) < 0) {
3101 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3106 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
3107 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
3113 interface->global_ctrl_sock = s;
3114 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
3130 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
3133 struct wpa_ctrl_dst *dst, *prev;
3135 if (interfaces->global_ctrl_sock > -1) {
3136 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
3137 close(interfaces->global_ctrl_sock);
3138 interfaces->global_ctrl_sock = -1;
3139 fname = hostapd_global_ctrl_iface_path(interfaces);
3145 if (interfaces->global_iface_path &&
3146 rmdir(interfaces->global_iface_path) < 0) {
3147 if (errno == ENOTEMPTY) {
3148 wpa_printf(MSG_DEBUG, "Control interface "
3149 "directory not empty - leaving it "
3152 wpa_printf(MSG_ERROR,
3153 "rmdir[ctrl_interface=%s]: %s",
3154 interfaces->global_iface_path,
3160 os_free(interfaces->global_iface_path);
3161 interfaces->global_iface_path = NULL;
3163 dst = interfaces->global_ctrl_dst;
3164 interfaces->global_ctrl_dst = NULL;
3173 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
3174 enum wpa_msg_type type,
3175 const char *buf, size_t len)
3177 struct wpa_ctrl_dst *dst, *next;
3184 if (type != WPA_MSG_ONLY_GLOBAL) {
3185 s = hapd->ctrl_sock;
3186 dst = hapd->ctrl_dst;
3188 s = hapd->iface->interfaces->global_ctrl_sock;
3189 dst = hapd->iface->interfaces->global_ctrl_dst;
3192 if (s < 0 || dst == NULL)
3195 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
3196 io[0].iov_base = levelstr;
3197 io[0].iov_len = os_strlen(levelstr);
3198 io[1].iov_base = (char *) buf;
3199 io[1].iov_len = len;
3200 os_memset(&msg, 0, sizeof(msg));
3207 if (level >= dst->debug_level) {
3208 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
3209 (u8 *) dst->addr.sun_path, dst->addrlen -
3210 offsetof(struct sockaddr_un, sun_path));
3211 msg.msg_name = &dst->addr;
3212 msg.msg_namelen = dst->addrlen;
3213 if (sendmsg(s, &msg, 0) < 0) {
3215 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
3217 idx, errno, strerror(errno));
3219 if (dst->errors > 10 || _errno == ENOENT) {
3220 if (type != WPA_MSG_ONLY_GLOBAL)
3221 hostapd_ctrl_iface_detach(
3225 hostapd_global_ctrl_iface_detach(
3226 hapd->iface->interfaces,
3238 #endif /* CONFIG_NATIVE_WINDOWS */