2 * hostapd / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2014, 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
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "common/version.h"
20 #include "common/ieee802_11_defs.h"
21 #include "drivers/driver.h"
22 #include "radius/radius_client.h"
23 #include "radius/radius_server.h"
24 #include "ap/hostapd.h"
25 #include "ap/ap_config.h"
26 #include "ap/ieee802_1x.h"
27 #include "ap/wpa_auth.h"
28 #include "ap/ieee802_11.h"
29 #include "ap/sta_info.h"
30 #include "ap/wps_hostapd.h"
31 #include "ap/ctrl_iface_ap.h"
32 #include "ap/ap_drv_ops.h"
34 #include "ap/wnm_ap.h"
35 #include "ap/wpa_auth.h"
36 #include "wps/wps_defs.h"
38 #include "config_file.h"
39 #include "ctrl_iface.h"
43 struct wpa_ctrl_dst *next;
44 struct sockaddr_un addr;
51 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
52 const char *buf, size_t len);
55 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
56 struct sockaddr_un *from,
59 struct wpa_ctrl_dst *dst;
61 dst = os_zalloc(sizeof(*dst));
64 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
65 dst->addrlen = fromlen;
66 dst->debug_level = MSG_INFO;
67 dst->next = hapd->ctrl_dst;
69 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
70 (u8 *) from->sun_path,
71 fromlen - offsetof(struct sockaddr_un, sun_path));
76 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
77 struct sockaddr_un *from,
80 struct wpa_ctrl_dst *dst, *prev = NULL;
84 if (fromlen == dst->addrlen &&
85 os_memcmp(from->sun_path, dst->addr.sun_path,
86 fromlen - offsetof(struct sockaddr_un, sun_path))
88 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
89 (u8 *) from->sun_path,
91 offsetof(struct sockaddr_un, sun_path));
93 hapd->ctrl_dst = dst->next;
95 prev->next = dst->next;
106 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
107 struct sockaddr_un *from,
111 struct wpa_ctrl_dst *dst;
113 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
115 dst = hapd->ctrl_dst;
117 if (fromlen == dst->addrlen &&
118 os_memcmp(from->sun_path, dst->addr.sun_path,
119 fromlen - offsetof(struct sockaddr_un, sun_path))
121 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
122 "level", (u8 *) from->sun_path, fromlen -
123 offsetof(struct sockaddr_un, sun_path));
124 dst->debug_level = atoi(level);
134 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
138 struct sta_info *sta;
140 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
142 if (hwaddr_aton(txtaddr, addr))
145 sta = ap_get_sta(hapd, addr);
149 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
150 "notification", MAC2STR(addr));
151 sta = ap_sta_add(hapd, addr);
155 hostapd_new_assoc_sta(hapd, sta, 0);
160 #ifdef CONFIG_IEEE80211W
162 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
166 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
168 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
170 if (hwaddr_aton(txtaddr, addr) ||
171 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
174 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
178 #endif /* NEED_AP_MLME */
179 #endif /* CONFIG_IEEE80211W */
183 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
185 char *pin = os_strchr(txt, ' ');
188 u8 addr_buf[ETH_ALEN], *addr = NULL;
195 timeout_txt = os_strchr(pin, ' ');
197 *timeout_txt++ = '\0';
198 timeout = atoi(timeout_txt);
199 pos = os_strchr(timeout_txt, ' ');
202 if (hwaddr_aton(pos, addr_buf) == 0)
208 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
212 static int hostapd_ctrl_iface_wps_check_pin(
213 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
220 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
221 (u8 *) cmd, os_strlen(cmd));
222 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
223 if (*pos < '0' || *pos > '9')
227 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
231 if (len != 4 && len != 8) {
232 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
238 unsigned int pin_val;
240 if (!wps_pin_valid(pin_val)) {
241 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
242 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
243 if (ret < 0 || (size_t) ret >= buflen)
249 ret = os_snprintf(buf, buflen, "%s", pin);
250 if (ret < 0 || (size_t) ret >= buflen)
257 #ifdef CONFIG_WPS_NFC
258 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
265 len = os_strlen(pos);
270 buf = wpabuf_alloc(len);
273 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
278 ret = hostapd_wps_nfc_tag_read(hapd, buf);
285 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
286 char *cmd, char *reply,
293 if (os_strcmp(cmd, "WPS") == 0)
295 else if (os_strcmp(cmd, "NDEF") == 0)
300 buf = hostapd_wps_nfc_config_token(hapd, ndef);
304 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
315 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
316 char *reply, size_t max_len,
322 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
326 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
337 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
338 char *cmd, char *reply,
341 if (os_strcmp(cmd, "WPS") == 0)
342 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
345 if (os_strcmp(cmd, "NDEF") == 0)
346 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
349 if (os_strcmp(cmd, "enable") == 0)
350 return hostapd_wps_nfc_token_enable(hapd);
352 if (os_strcmp(cmd, "disable") == 0) {
353 hostapd_wps_nfc_token_disable(hapd);
361 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
362 char *cmd, char *reply,
370 pos = os_strchr(cmd, ' ');
375 if (os_strcmp(cmd, "WPS") == 0)
377 else if (os_strcmp(cmd, "NDEF") == 0)
382 if (os_strcmp(pos, "WPS-CR") == 0)
383 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
389 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
400 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
404 struct wpabuf *req, *sel;
406 char *pos, *role, *type, *pos2;
409 pos = os_strchr(role, ' ');
415 pos = os_strchr(type, ' ');
420 pos2 = os_strchr(pos, ' ');
425 len = os_strlen(pos);
430 req = wpabuf_alloc(len);
433 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
438 len = os_strlen(pos2);
445 sel = wpabuf_alloc(len);
450 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
456 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
457 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
459 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
460 "reported: role=%s type=%s", role, type);
469 #endif /* CONFIG_WPS_NFC */
472 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
473 char *buf, size_t buflen)
479 pos = os_strchr(txt, ' ');
483 if (os_strcmp(txt, "disable") == 0) {
484 hostapd_wps_ap_pin_disable(hapd);
485 return os_snprintf(buf, buflen, "OK\n");
488 if (os_strcmp(txt, "random") == 0) {
491 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
494 return os_snprintf(buf, buflen, "%s", pin_txt);
497 if (os_strcmp(txt, "get") == 0) {
498 pin_txt = hostapd_wps_ap_pin_get(hapd);
501 return os_snprintf(buf, buflen, "%s", pin_txt);
504 if (os_strcmp(txt, "set") == 0) {
509 pos = os_strchr(pos, ' ');
514 if (os_strlen(pin) > buflen)
516 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
518 return os_snprintf(buf, buflen, "%s", pin);
525 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
528 char *ssid, *auth, *encr = NULL, *key = NULL;
531 pos = os_strchr(txt, ' ');
537 pos = os_strchr(pos, ' ');
541 pos = os_strchr(pos, ' ');
548 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
552 static const char * pbc_status_str(enum pbc_status status)
555 case WPS_PBC_STATUS_DISABLE:
557 case WPS_PBC_STATUS_ACTIVE:
559 case WPS_PBC_STATUS_TIMEOUT:
561 case WPS_PBC_STATUS_OVERLAP:
569 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
570 char *buf, size_t buflen)
578 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
579 pbc_status_str(hapd->wps_stats.pbc_status));
581 if (ret < 0 || ret >= end - pos)
585 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
586 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
588 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
589 "Failed" : "None")));
591 if (ret < 0 || ret >= end - pos)
595 /* If status == Failure - Add possible Reasons */
596 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
597 hapd->wps_stats.failure_reason > 0) {
598 ret = os_snprintf(pos, end - pos,
599 "Failure Reason: %s\n",
600 wps_ei_str(hapd->wps_stats.failure_reason));
602 if (ret < 0 || ret >= end - pos)
607 if (hapd->wps_stats.status) {
608 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
609 MAC2STR(hapd->wps_stats.peer_addr));
611 if (ret < 0 || ret >= end - pos)
619 #endif /* CONFIG_WPS */
623 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
629 if (hwaddr_aton(cmd, addr))
642 return hs20_send_wnm_notification(hapd, addr, 1, url);
646 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
650 int code, reauth_delay, ret;
655 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
656 if (hwaddr_aton(cmd, addr))
659 pos = os_strchr(cmd, ' ');
665 pos = os_strchr(pos, ' ');
669 reauth_delay = atoi(pos);
672 pos = os_strchr(pos, ' ');
675 url_len = os_strlen(pos);
678 req = wpabuf_alloc(4 + url_len);
681 wpabuf_put_u8(req, code);
682 wpabuf_put_le16(req, reauth_delay);
683 wpabuf_put_u8(req, url_len);
685 wpabuf_put_data(req, pos, url_len);
687 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
688 " to indicate imminent deauthentication (code=%d "
689 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
690 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
695 #endif /* CONFIG_HS20 */
698 #ifdef CONFIG_INTERWORKING
700 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
703 u8 qos_map_set[16 + 2 * 21], count = 0;
704 const char *pos = cmd;
708 if (count == sizeof(qos_map_set)) {
709 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
714 if (val < 0 || val > 255) {
715 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
719 qos_map_set[count++] = val;
720 pos = os_strchr(pos, ',');
726 if (count < 16 || count & 1) {
727 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
731 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
733 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
737 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
738 hapd->conf->qos_map_set_len = count;
744 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
748 struct sta_info *sta;
750 u8 *qos_map_set = hapd->conf->qos_map_set;
751 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
754 if (!qos_map_set_len) {
755 wpa_printf(MSG_INFO, "QoS Map Set is not set");
759 if (hwaddr_aton(cmd, addr))
762 sta = ap_get_sta(hapd, addr);
764 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
765 "for QoS Map Configuration message",
770 if (!sta->qos_map_enabled) {
771 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
772 "support for QoS Map", MAC2STR(addr));
776 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
780 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
781 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
783 /* QoS Map Set Element */
784 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
785 wpabuf_put_u8(buf, qos_map_set_len);
786 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
788 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
789 wpabuf_head(buf), wpabuf_len(buf));
795 #endif /* CONFIG_INTERWORKING */
800 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
805 struct sta_info *sta;
807 if (hwaddr_aton(cmd, addr))
811 disassoc_timer = atoi(cmd + 17);
813 sta = ap_get_sta(hapd, addr);
815 wpa_printf(MSG_DEBUG, "Station " MACSTR
816 " not found for disassociation imminent message",
821 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
825 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
829 const char *url, *timerstr;
831 struct sta_info *sta;
833 if (hwaddr_aton(cmd, addr))
836 sta = ap_get_sta(hapd, addr);
838 wpa_printf(MSG_DEBUG, "Station " MACSTR
839 " not found for ESS disassociation imminent message",
845 if (*timerstr != ' ')
848 disassoc_timer = atoi(timerstr);
849 if (disassoc_timer < 0 || disassoc_timer > 65535)
852 url = os_strchr(timerstr, ' ');
857 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
860 #endif /* CONFIG_WNM */
863 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
864 char *buf, size_t buflen)
872 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
874 MAC2STR(hapd->own_addr),
875 wpa_ssid_txt(hapd->conf->ssid.ssid,
876 hapd->conf->ssid.ssid_len));
877 if (ret < 0 || ret >= end - pos)
882 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
883 hapd->conf->wps_state == 0 ? "disabled" :
884 (hapd->conf->wps_state == 1 ? "not configured" :
886 if (ret < 0 || ret >= end - pos)
890 if (hapd->conf->wps_state && hapd->conf->wpa &&
891 hapd->conf->ssid.wpa_passphrase) {
892 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
893 hapd->conf->ssid.wpa_passphrase);
894 if (ret < 0 || ret >= end - pos)
899 if (hapd->conf->wps_state && hapd->conf->wpa &&
900 hapd->conf->ssid.wpa_psk &&
901 hapd->conf->ssid.wpa_psk->group) {
902 char hex[PMK_LEN * 2 + 1];
903 wpa_snprintf_hex(hex, sizeof(hex),
904 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
905 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
906 if (ret < 0 || ret >= end - pos)
910 #endif /* CONFIG_WPS */
912 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
913 ret = os_snprintf(pos, end - pos, "key_mgmt=");
914 if (ret < 0 || ret >= end - pos)
918 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
919 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
920 if (ret < 0 || ret >= end - pos)
924 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
925 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
926 if (ret < 0 || ret >= end - pos)
930 #ifdef CONFIG_IEEE80211R
931 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
932 ret = os_snprintf(pos, end - pos, "FT-PSK ");
933 if (ret < 0 || ret >= end - pos)
937 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
938 ret = os_snprintf(pos, end - pos, "FT-EAP ");
939 if (ret < 0 || ret >= end - pos)
944 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
945 ret = os_snprintf(pos, end - pos, "FT-SAE ");
946 if (ret < 0 || ret >= end - pos)
950 #endif /* CONFIG_SAE */
951 #endif /* CONFIG_IEEE80211R */
952 #ifdef CONFIG_IEEE80211W
953 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
954 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
955 if (ret < 0 || ret >= end - pos)
959 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
960 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
961 if (ret < 0 || ret >= end - pos)
965 #endif /* CONFIG_IEEE80211W */
967 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
968 ret = os_snprintf(pos, end - pos, "SAE ");
969 if (ret < 0 || ret >= end - pos)
973 #endif /* CONFIG_SAE */
975 ret = os_snprintf(pos, end - pos, "\n");
976 if (ret < 0 || ret >= end - pos)
981 if (hapd->conf->wpa) {
982 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
983 wpa_cipher_txt(hapd->conf->wpa_group));
984 if (ret < 0 || ret >= end - pos)
989 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
990 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
991 if (ret < 0 || ret >= end - pos)
995 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1001 ret = os_snprintf(pos, end - pos, "\n");
1002 if (ret < 0 || ret >= end - pos)
1007 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1008 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
1009 if (ret < 0 || ret >= end - pos)
1013 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1019 ret = os_snprintf(pos, end - pos, "\n");
1020 if (ret < 0 || ret >= end - pos)
1029 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1034 value = os_strchr(cmd, ' ');
1039 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1041 #ifdef CONFIG_WPS_TESTING
1042 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1044 val = strtol(value, NULL, 0);
1045 if (val < 0 || val > 0xff) {
1047 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1048 "wps_version_number %ld", val);
1050 wps_version_number = val;
1051 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1053 (wps_version_number & 0xf0) >> 4,
1054 wps_version_number & 0x0f);
1055 hostapd_wps_update_ie(hapd);
1057 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1058 wps_testing_dummy_cred = atoi(value);
1059 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1060 wps_testing_dummy_cred);
1061 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1062 wps_corrupt_pkhash = atoi(value);
1063 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1064 wps_corrupt_pkhash);
1065 #endif /* CONFIG_WPS_TESTING */
1066 #ifdef CONFIG_INTERWORKING
1067 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1068 int val = atoi(value);
1072 hapd->gas_frag_limit = val;
1073 #endif /* CONFIG_INTERWORKING */
1074 #ifdef CONFIG_TESTING_OPTIONS
1075 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1076 hapd->ext_mgmt_frame_handling = atoi(value);
1077 #endif /* CONFIG_TESTING_OPTIONS */
1079 struct sta_info *sta;
1082 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1086 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1087 for (sta = hapd->sta_list; sta; sta = sta->next) {
1088 if (hostapd_maclist_found(
1089 hapd->conf->deny_mac,
1090 hapd->conf->num_deny_mac, sta->addr,
1092 (!vlan_id || vlan_id == sta->vlan_id))
1094 hapd, sta, sta->addr,
1095 WLAN_REASON_UNSPECIFIED);
1097 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1098 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1099 for (sta = hapd->sta_list; sta; sta = sta->next) {
1100 if (!hostapd_maclist_found(
1101 hapd->conf->accept_mac,
1102 hapd->conf->num_accept_mac,
1103 sta->addr, &vlan_id) ||
1104 (vlan_id && vlan_id != sta->vlan_id))
1106 hapd, sta, sta->addr,
1107 WLAN_REASON_UNSPECIFIED);
1116 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1117 char *buf, size_t buflen)
1121 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1123 if (os_strcmp(cmd, "version") == 0) {
1124 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1125 if (res < 0 || (unsigned int) res >= buflen)
1134 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1136 if (hostapd_enable_iface(iface) < 0) {
1137 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1144 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1146 if (hostapd_reload_iface(iface) < 0) {
1147 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1154 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1156 if (hostapd_disable_iface(iface) < 0) {
1157 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1164 #ifdef CONFIG_TESTING_OPTIONS
1166 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1168 union wpa_event_data data;
1170 enum wpa_event_type event;
1172 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1174 os_memset(&data, 0, sizeof(data));
1176 param = os_strchr(cmd, ' ');
1181 if (os_strcmp(cmd, "DETECTED") == 0)
1182 event = EVENT_DFS_RADAR_DETECTED;
1183 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1184 event = EVENT_DFS_CAC_FINISHED;
1185 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1186 event = EVENT_DFS_CAC_ABORTED;
1187 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1188 event = EVENT_DFS_NOP_FINISHED;
1190 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1195 pos = os_strstr(param, "freq=");
1197 data.dfs_event.freq = atoi(pos + 5);
1199 pos = os_strstr(param, "ht_enabled=1");
1201 data.dfs_event.ht_enabled = 1;
1203 pos = os_strstr(param, "chan_offset=");
1205 data.dfs_event.chan_offset = atoi(pos + 12);
1207 pos = os_strstr(param, "chan_width=");
1209 data.dfs_event.chan_width = atoi(pos + 11);
1211 pos = os_strstr(param, "cf1=");
1213 data.dfs_event.cf1 = atoi(pos + 4);
1215 pos = os_strstr(param, "cf2=");
1217 data.dfs_event.cf2 = atoi(pos + 4);
1219 wpa_supplicant_event(hapd, event, &data);
1225 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1231 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1233 len = os_strlen(cmd);
1238 buf = os_malloc(len);
1242 if (hexstr2bin(cmd, buf, len) < 0) {
1247 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1252 #endif /* CONFIG_TESTING_OPTIONS */
1255 static int hostapd_ctrl_iface_chan_switch(struct hostapd_data *hapd, char *pos)
1258 struct csa_settings settings;
1259 int ret = hostapd_parse_csa_settings(pos, &settings);
1264 return hostapd_switch_channel(hapd, &settings);
1265 #else /* NEED_AP_MLME */
1267 #endif /* NEED_AP_MLME */
1271 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1272 int reply_size, const char *param)
1274 #ifdef RADIUS_SERVER
1275 if (os_strcmp(param, "radius_server") == 0) {
1276 return radius_server_get_mib(hapd->radius_srv, reply,
1279 #endif /* RADIUS_SERVER */
1284 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
1285 char *buf, size_t buflen)
1290 unsigned int vendor_id, subcmd;
1291 struct wpabuf *reply;
1292 size_t data_len = 0;
1294 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1295 vendor_id = strtoul(cmd, &pos, 16);
1299 subcmd = strtoul(pos, &pos, 10);
1302 if (!isblank(*pos++))
1304 data_len = os_strlen(pos);
1309 data = os_malloc(data_len);
1313 if (hexstr2bin(pos, data, data_len)) {
1314 wpa_printf(MSG_DEBUG,
1315 "Vendor command: wrong parameter format");
1321 reply = wpabuf_alloc((buflen - 1) / 2);
1327 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
1331 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
1341 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
1344 struct hostapd_data *hapd = eloop_ctx;
1347 struct sockaddr_un from;
1348 socklen_t fromlen = sizeof(from);
1350 const int reply_size = 4096;
1352 int level = MSG_DEBUG;
1354 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1355 (struct sockaddr *) &from, &fromlen);
1357 perror("recvfrom(ctrl_iface)");
1361 if (os_strcmp(buf, "PING") == 0)
1362 level = MSG_EXCESSIVE;
1363 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
1365 reply = os_malloc(reply_size);
1366 if (reply == NULL) {
1367 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
1372 os_memcpy(reply, "OK\n", 3);
1375 if (os_strcmp(buf, "PING") == 0) {
1376 os_memcpy(reply, "PONG\n", 5);
1378 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
1379 if (wpa_debug_reopen_file() < 0)
1381 } else if (os_strcmp(buf, "STATUS") == 0) {
1382 reply_len = hostapd_ctrl_iface_status(hapd, reply,
1384 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
1385 reply_len = hostapd_drv_status(hapd, reply, reply_size);
1386 } else if (os_strcmp(buf, "MIB") == 0) {
1387 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
1388 if (reply_len >= 0) {
1389 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
1390 reply_size - reply_len);
1396 if (reply_len >= 0) {
1397 res = ieee802_1x_get_mib(hapd, reply + reply_len,
1398 reply_size - reply_len);
1404 #ifndef CONFIG_NO_RADIUS
1405 if (reply_len >= 0) {
1406 res = radius_client_get_mib(hapd->radius,
1408 reply_size - reply_len);
1414 #endif /* CONFIG_NO_RADIUS */
1415 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
1416 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
1418 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
1419 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
1421 } else if (os_strncmp(buf, "STA ", 4) == 0) {
1422 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
1424 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
1425 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
1427 } else if (os_strcmp(buf, "ATTACH") == 0) {
1428 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
1430 } else if (os_strcmp(buf, "DETACH") == 0) {
1431 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
1433 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
1434 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
1437 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
1438 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
1440 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
1441 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
1443 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
1444 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
1446 #ifdef CONFIG_IEEE80211W
1448 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
1449 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
1451 #endif /* NEED_AP_MLME */
1452 #endif /* CONFIG_IEEE80211W */
1454 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
1455 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
1457 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
1458 reply_len = hostapd_ctrl_iface_wps_check_pin(
1459 hapd, buf + 14, reply, reply_size);
1460 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
1461 if (hostapd_wps_button_pushed(hapd, NULL))
1463 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
1464 if (hostapd_wps_cancel(hapd))
1466 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
1467 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
1469 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
1470 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
1472 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
1473 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
1475 #ifdef CONFIG_WPS_NFC
1476 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
1477 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
1479 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
1480 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
1481 hapd, buf + 21, reply, reply_size);
1482 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
1483 reply_len = hostapd_ctrl_iface_wps_nfc_token(
1484 hapd, buf + 14, reply, reply_size);
1485 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
1486 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
1487 hapd, buf + 21, reply, reply_size);
1488 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
1489 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
1491 #endif /* CONFIG_WPS_NFC */
1492 #endif /* CONFIG_WPS */
1493 #ifdef CONFIG_INTERWORKING
1494 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
1495 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
1497 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
1498 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
1500 #endif /* CONFIG_INTERWORKING */
1502 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
1503 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
1505 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
1506 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
1508 #endif /* CONFIG_HS20 */
1510 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
1511 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
1513 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
1514 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
1516 #endif /* CONFIG_WNM */
1517 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
1518 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
1520 } else if (os_strncmp(buf, "SET ", 4) == 0) {
1521 if (hostapd_ctrl_iface_set(hapd, buf + 4))
1523 } else if (os_strncmp(buf, "GET ", 4) == 0) {
1524 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
1526 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
1527 if (hostapd_ctrl_iface_enable(hapd->iface))
1529 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
1530 if (hostapd_ctrl_iface_reload(hapd->iface))
1532 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
1533 if (hostapd_ctrl_iface_disable(hapd->iface))
1535 #ifdef CONFIG_TESTING_OPTIONS
1536 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
1537 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
1539 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
1540 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
1542 #endif /* CONFIG_TESTING_OPTIONS */
1543 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
1544 if (hostapd_ctrl_iface_chan_switch(hapd, buf + 12))
1546 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
1547 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
1551 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
1555 if (reply_len < 0) {
1556 os_memcpy(reply, "FAIL\n", 5);
1559 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
1564 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
1569 if (hapd->conf->ctrl_interface == NULL)
1572 len = os_strlen(hapd->conf->ctrl_interface) +
1573 os_strlen(hapd->conf->iface) + 2;
1574 buf = os_malloc(len);
1578 os_snprintf(buf, len, "%s/%s",
1579 hapd->conf->ctrl_interface, hapd->conf->iface);
1580 buf[len - 1] = '\0';
1585 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
1586 const char *txt, size_t len)
1588 struct hostapd_data *hapd = ctx;
1591 hostapd_ctrl_iface_send(hapd, level, txt, len);
1595 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
1597 struct sockaddr_un addr;
1601 if (hapd->ctrl_sock > -1) {
1602 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
1606 if (hapd->conf->ctrl_interface == NULL)
1609 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1610 if (errno == EEXIST) {
1611 wpa_printf(MSG_DEBUG, "Using existing control "
1612 "interface directory.");
1614 perror("mkdir[ctrl_interface]");
1619 if (hapd->conf->ctrl_interface_gid_set &&
1620 chown(hapd->conf->ctrl_interface, -1,
1621 hapd->conf->ctrl_interface_gid) < 0) {
1622 perror("chown[ctrl_interface]");
1626 if (!hapd->conf->ctrl_interface_gid_set &&
1627 hapd->iface->interfaces->ctrl_iface_group &&
1628 chown(hapd->conf->ctrl_interface, -1,
1629 hapd->iface->interfaces->ctrl_iface_group) < 0) {
1630 perror("chown[ctrl_interface]");
1636 * Android is using umask 0077 which would leave the control interface
1637 * directory without group access. This breaks things since Wi-Fi
1638 * framework assumes that this directory can be accessed by other
1639 * applications in the wifi group. Fix this by adding group access even
1640 * if umask value would prevent this.
1642 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1643 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
1645 /* Try to continue anyway */
1647 #endif /* ANDROID */
1649 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
1650 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
1653 s = socket(PF_UNIX, SOCK_DGRAM, 0);
1655 perror("socket(PF_UNIX)");
1659 os_memset(&addr, 0, sizeof(addr));
1661 addr.sun_len = sizeof(addr);
1662 #endif /* __FreeBSD__ */
1663 addr.sun_family = AF_UNIX;
1664 fname = hostapd_ctrl_iface_path(hapd);
1667 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1668 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1669 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1671 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1672 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1673 " allow connections - assuming it was left"
1674 "over from forced program termination");
1675 if (unlink(fname) < 0) {
1676 perror("unlink[ctrl_iface]");
1677 wpa_printf(MSG_ERROR, "Could not unlink "
1678 "existing ctrl_iface socket '%s'",
1682 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1684 perror("hostapd-ctrl-iface: bind(PF_UNIX)");
1687 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1688 "ctrl_iface socket '%s'", fname);
1690 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1691 "be in use - cannot override it");
1692 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1693 "not used anymore", fname);
1700 if (hapd->conf->ctrl_interface_gid_set &&
1701 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
1702 perror("chown[ctrl_interface/ifname]");
1706 if (!hapd->conf->ctrl_interface_gid_set &&
1707 hapd->iface->interfaces->ctrl_iface_group &&
1708 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
1709 perror("chown[ctrl_interface/ifname]");
1713 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1714 perror("chmod[ctrl_interface/ifname]");
1719 hapd->ctrl_sock = s;
1720 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
1722 hapd->msg_ctx = hapd;
1723 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
1738 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
1740 struct wpa_ctrl_dst *dst, *prev;
1742 if (hapd->ctrl_sock > -1) {
1744 eloop_unregister_read_sock(hapd->ctrl_sock);
1745 close(hapd->ctrl_sock);
1746 hapd->ctrl_sock = -1;
1747 fname = hostapd_ctrl_iface_path(hapd);
1752 if (hapd->conf->ctrl_interface &&
1753 rmdir(hapd->conf->ctrl_interface) < 0) {
1754 if (errno == ENOTEMPTY) {
1755 wpa_printf(MSG_DEBUG, "Control interface "
1756 "directory not empty - leaving it "
1759 wpa_printf(MSG_ERROR,
1760 "rmdir[ctrl_interface=%s]: %s",
1761 hapd->conf->ctrl_interface,
1767 dst = hapd->ctrl_dst;
1776 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
1779 if (hostapd_add_iface(interfaces, buf) < 0) {
1780 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
1787 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
1790 if (hostapd_remove_iface(interfaces, buf) < 0) {
1791 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
1798 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
1800 #ifdef CONFIG_WPS_TESTING
1801 wps_version_number = 0x20;
1802 wps_testing_dummy_cred = 0;
1803 wps_corrupt_pkhash = 0;
1804 #endif /* CONFIG_WPS_TESTING */
1808 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
1811 void *interfaces = eloop_ctx;
1814 struct sockaddr_un from;
1815 socklen_t fromlen = sizeof(from);
1819 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1820 (struct sockaddr *) &from, &fromlen);
1822 perror("recvfrom(ctrl_iface)");
1826 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
1828 os_memcpy(reply, "OK\n", 3);
1831 if (os_strcmp(buf, "PING") == 0) {
1832 os_memcpy(reply, "PONG\n", 5);
1834 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
1835 if (wpa_debug_reopen_file() < 0)
1837 } else if (os_strcmp(buf, "FLUSH") == 0) {
1838 hostapd_ctrl_iface_flush(interfaces);
1839 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
1840 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
1842 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
1843 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
1845 #ifdef CONFIG_MODULE_TESTS
1846 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
1847 int hapd_module_tests(void);
1848 if (hapd_module_tests() < 0)
1850 #endif /* CONFIG_MODULE_TESTS */
1852 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
1857 if (reply_len < 0) {
1858 os_memcpy(reply, "FAIL\n", 5);
1862 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
1866 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
1871 if (interface->global_iface_path == NULL)
1874 len = os_strlen(interface->global_iface_path) +
1875 os_strlen(interface->global_iface_name) + 2;
1876 buf = os_malloc(len);
1880 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
1881 interface->global_iface_name);
1882 buf[len - 1] = '\0';
1887 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
1889 struct sockaddr_un addr;
1893 if (interface->global_iface_path == NULL) {
1894 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
1898 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
1899 if (errno == EEXIST) {
1900 wpa_printf(MSG_DEBUG, "Using existing control "
1901 "interface directory.");
1903 perror("mkdir[ctrl_interface]");
1906 } else if (interface->ctrl_iface_group &&
1907 chown(interface->global_iface_path, -1,
1908 interface->ctrl_iface_group) < 0) {
1909 perror("chown[ctrl_interface]");
1913 if (os_strlen(interface->global_iface_path) + 1 +
1914 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
1917 s = socket(PF_UNIX, SOCK_DGRAM, 0);
1919 perror("socket(PF_UNIX)");
1923 os_memset(&addr, 0, sizeof(addr));
1925 addr.sun_len = sizeof(addr);
1926 #endif /* __FreeBSD__ */
1927 addr.sun_family = AF_UNIX;
1928 fname = hostapd_global_ctrl_iface_path(interface);
1931 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1932 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1933 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1935 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1936 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1937 " allow connections - assuming it was left"
1938 "over from forced program termination");
1939 if (unlink(fname) < 0) {
1940 perror("unlink[ctrl_iface]");
1941 wpa_printf(MSG_ERROR, "Could not unlink "
1942 "existing ctrl_iface socket '%s'",
1946 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1948 perror("bind(PF_UNIX)");
1951 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1952 "ctrl_iface socket '%s'", fname);
1954 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1955 "be in use - cannot override it");
1956 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1957 "not used anymore", fname);
1964 if (interface->ctrl_iface_group &&
1965 chown(fname, -1, interface->ctrl_iface_group) < 0) {
1966 perror("chown[ctrl_interface]");
1970 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1971 perror("chmod[ctrl_interface/ifname]");
1976 interface->global_ctrl_sock = s;
1977 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
1993 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
1997 if (interfaces->global_ctrl_sock > -1) {
1998 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
1999 close(interfaces->global_ctrl_sock);
2000 interfaces->global_ctrl_sock = -1;
2001 fname = hostapd_global_ctrl_iface_path(interfaces);
2007 if (interfaces->global_iface_path &&
2008 rmdir(interfaces->global_iface_path) < 0) {
2009 if (errno == ENOTEMPTY) {
2010 wpa_printf(MSG_DEBUG, "Control interface "
2011 "directory not empty - leaving it "
2014 wpa_printf(MSG_ERROR,
2015 "rmdir[ctrl_interface=%s]: %s",
2016 interfaces->global_iface_path,
2020 os_free(interfaces->global_iface_path);
2021 interfaces->global_iface_path = NULL;
2026 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
2027 const char *buf, size_t len)
2029 struct wpa_ctrl_dst *dst, *next;
2035 dst = hapd->ctrl_dst;
2036 if (hapd->ctrl_sock < 0 || dst == NULL)
2039 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
2040 io[0].iov_base = levelstr;
2041 io[0].iov_len = os_strlen(levelstr);
2042 io[1].iov_base = (char *) buf;
2043 io[1].iov_len = len;
2044 os_memset(&msg, 0, sizeof(msg));
2051 if (level >= dst->debug_level) {
2052 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
2053 (u8 *) dst->addr.sun_path, dst->addrlen -
2054 offsetof(struct sockaddr_un, sun_path));
2055 msg.msg_name = &dst->addr;
2056 msg.msg_namelen = dst->addrlen;
2057 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
2059 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
2061 idx, errno, strerror(errno));
2063 if (dst->errors > 10 || _errno == ENOENT) {
2064 hostapd_ctrl_iface_detach(
2076 #endif /* CONFIG_NATIVE_WINDOWS */