2 * hostapd / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #ifndef CONFIG_NATIVE_WINDOWS
13 #ifdef CONFIG_TESTING_OPTIONS
14 #include <net/ethernet.h>
15 #include <netinet/ip.h>
16 #endif /* CONFIG_TESTING_OPTIONS */
22 #include "utils/common.h"
23 #include "utils/eloop.h"
24 #include "common/version.h"
25 #include "common/ieee802_11_defs.h"
26 #include "crypto/tls.h"
27 #include "drivers/driver.h"
28 #include "eapol_auth/eapol_auth_sm.h"
29 #include "radius/radius_client.h"
30 #include "radius/radius_server.h"
31 #include "l2_packet/l2_packet.h"
32 #include "ap/hostapd.h"
33 #include "ap/ap_config.h"
34 #include "ap/ieee802_1x.h"
35 #include "ap/wpa_auth.h"
36 #include "ap/ieee802_11.h"
37 #include "ap/sta_info.h"
38 #include "ap/wps_hostapd.h"
39 #include "ap/ctrl_iface_ap.h"
40 #include "ap/ap_drv_ops.h"
42 #include "ap/wnm_ap.h"
43 #include "ap/wpa_auth.h"
44 #include "ap/beacon.h"
45 #include "wps/wps_defs.h"
47 #include "fst/fst_ctrl_iface.h"
48 #include "config_file.h"
49 #include "ctrl_iface.h"
53 struct wpa_ctrl_dst *next;
54 struct sockaddr_un addr;
61 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
62 enum wpa_msg_type type,
63 const char *buf, size_t len);
66 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
67 struct sockaddr_un *from,
70 struct wpa_ctrl_dst *dst;
72 dst = os_zalloc(sizeof(*dst));
75 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
76 dst->addrlen = fromlen;
77 dst->debug_level = MSG_INFO;
78 dst->next = hapd->ctrl_dst;
80 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
81 (u8 *) from->sun_path,
82 fromlen - offsetof(struct sockaddr_un, sun_path));
87 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
88 struct sockaddr_un *from,
91 struct wpa_ctrl_dst *dst, *prev = NULL;
95 if (fromlen == dst->addrlen &&
96 os_memcmp(from->sun_path, dst->addr.sun_path,
97 fromlen - offsetof(struct sockaddr_un, sun_path))
99 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
100 (u8 *) from->sun_path,
102 offsetof(struct sockaddr_un, sun_path));
104 hapd->ctrl_dst = dst->next;
106 prev->next = dst->next;
117 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
118 struct sockaddr_un *from,
122 struct wpa_ctrl_dst *dst;
124 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
126 dst = hapd->ctrl_dst;
128 if (fromlen == dst->addrlen &&
129 os_memcmp(from->sun_path, dst->addr.sun_path,
130 fromlen - offsetof(struct sockaddr_un, sun_path))
132 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
133 "level", (u8 *) from->sun_path, fromlen -
134 offsetof(struct sockaddr_un, sun_path));
135 dst->debug_level = atoi(level);
145 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
149 struct sta_info *sta;
151 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
153 if (hwaddr_aton(txtaddr, addr))
156 sta = ap_get_sta(hapd, addr);
160 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
161 "notification", MAC2STR(addr));
162 sta = ap_sta_add(hapd, addr);
166 hostapd_new_assoc_sta(hapd, sta, 0);
171 #ifdef CONFIG_IEEE80211W
173 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
177 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
179 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
181 if (hwaddr_aton(txtaddr, addr) ||
182 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
185 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
189 #endif /* NEED_AP_MLME */
190 #endif /* CONFIG_IEEE80211W */
194 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
196 char *pin = os_strchr(txt, ' ');
199 u8 addr_buf[ETH_ALEN], *addr = NULL;
206 timeout_txt = os_strchr(pin, ' ');
208 *timeout_txt++ = '\0';
209 timeout = atoi(timeout_txt);
210 pos = os_strchr(timeout_txt, ' ');
213 if (hwaddr_aton(pos, addr_buf) == 0)
219 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
223 static int hostapd_ctrl_iface_wps_check_pin(
224 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
231 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
232 (u8 *) cmd, os_strlen(cmd));
233 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
234 if (*pos < '0' || *pos > '9')
238 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
242 if (len != 4 && len != 8) {
243 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
249 unsigned int pin_val;
251 if (!wps_pin_valid(pin_val)) {
252 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
253 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
254 if (os_snprintf_error(buflen, ret))
260 ret = os_snprintf(buf, buflen, "%s", pin);
261 if (os_snprintf_error(buflen, ret))
268 #ifdef CONFIG_WPS_NFC
269 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
276 len = os_strlen(pos);
281 buf = wpabuf_alloc(len);
284 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
289 ret = hostapd_wps_nfc_tag_read(hapd, buf);
296 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
297 char *cmd, char *reply,
304 if (os_strcmp(cmd, "WPS") == 0)
306 else if (os_strcmp(cmd, "NDEF") == 0)
311 buf = hostapd_wps_nfc_config_token(hapd, ndef);
315 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
326 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
327 char *reply, size_t max_len,
333 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
337 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
348 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
349 char *cmd, char *reply,
352 if (os_strcmp(cmd, "WPS") == 0)
353 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
356 if (os_strcmp(cmd, "NDEF") == 0)
357 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
360 if (os_strcmp(cmd, "enable") == 0)
361 return hostapd_wps_nfc_token_enable(hapd);
363 if (os_strcmp(cmd, "disable") == 0) {
364 hostapd_wps_nfc_token_disable(hapd);
372 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
373 char *cmd, char *reply,
381 pos = os_strchr(cmd, ' ');
386 if (os_strcmp(cmd, "WPS") == 0)
388 else if (os_strcmp(cmd, "NDEF") == 0)
393 if (os_strcmp(pos, "WPS-CR") == 0)
394 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
400 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
411 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
415 struct wpabuf *req, *sel;
417 char *pos, *role, *type, *pos2;
420 pos = os_strchr(role, ' ');
426 pos = os_strchr(type, ' ');
431 pos2 = os_strchr(pos, ' ');
436 len = os_strlen(pos);
441 req = wpabuf_alloc(len);
444 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
449 len = os_strlen(pos2);
456 sel = wpabuf_alloc(len);
461 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
467 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
468 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
470 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
471 "reported: role=%s type=%s", role, type);
480 #endif /* CONFIG_WPS_NFC */
483 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
484 char *buf, size_t buflen)
490 pos = os_strchr(txt, ' ');
494 if (os_strcmp(txt, "disable") == 0) {
495 hostapd_wps_ap_pin_disable(hapd);
496 return os_snprintf(buf, buflen, "OK\n");
499 if (os_strcmp(txt, "random") == 0) {
502 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
505 return os_snprintf(buf, buflen, "%s", pin_txt);
508 if (os_strcmp(txt, "get") == 0) {
509 pin_txt = hostapd_wps_ap_pin_get(hapd);
512 return os_snprintf(buf, buflen, "%s", pin_txt);
515 if (os_strcmp(txt, "set") == 0) {
520 pos = os_strchr(pos, ' ');
525 if (os_strlen(pin) > buflen)
527 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
529 return os_snprintf(buf, buflen, "%s", pin);
536 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
539 char *ssid, *auth, *encr = NULL, *key = NULL;
542 pos = os_strchr(txt, ' ');
548 pos = os_strchr(pos, ' ');
552 pos = os_strchr(pos, ' ');
559 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
563 static const char * pbc_status_str(enum pbc_status status)
566 case WPS_PBC_STATUS_DISABLE:
568 case WPS_PBC_STATUS_ACTIVE:
570 case WPS_PBC_STATUS_TIMEOUT:
572 case WPS_PBC_STATUS_OVERLAP:
580 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
581 char *buf, size_t buflen)
589 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
590 pbc_status_str(hapd->wps_stats.pbc_status));
592 if (os_snprintf_error(end - pos, ret))
596 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
597 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
599 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
600 "Failed" : "None")));
602 if (os_snprintf_error(end - pos, ret))
606 /* If status == Failure - Add possible Reasons */
607 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
608 hapd->wps_stats.failure_reason > 0) {
609 ret = os_snprintf(pos, end - pos,
610 "Failure Reason: %s\n",
611 wps_ei_str(hapd->wps_stats.failure_reason));
613 if (os_snprintf_error(end - pos, ret))
618 if (hapd->wps_stats.status) {
619 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
620 MAC2STR(hapd->wps_stats.peer_addr));
622 if (os_snprintf_error(end - pos, ret))
630 #endif /* CONFIG_WPS */
634 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
640 if (hwaddr_aton(cmd, addr))
653 return hs20_send_wnm_notification(hapd, addr, 1, url);
657 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
661 int code, reauth_delay, ret;
666 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
667 if (hwaddr_aton(cmd, addr))
670 pos = os_strchr(cmd, ' ');
676 pos = os_strchr(pos, ' ');
680 reauth_delay = atoi(pos);
683 pos = os_strchr(pos, ' ');
686 url_len = os_strlen(pos);
689 req = wpabuf_alloc(4 + url_len);
692 wpabuf_put_u8(req, code);
693 wpabuf_put_le16(req, reauth_delay);
694 wpabuf_put_u8(req, url_len);
696 wpabuf_put_data(req, pos, url_len);
698 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
699 " to indicate imminent deauthentication (code=%d "
700 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
701 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
706 #endif /* CONFIG_HS20 */
709 #ifdef CONFIG_INTERWORKING
711 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
714 u8 qos_map_set[16 + 2 * 21], count = 0;
715 const char *pos = cmd;
719 if (count == sizeof(qos_map_set)) {
720 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
725 if (val < 0 || val > 255) {
726 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
730 qos_map_set[count++] = val;
731 pos = os_strchr(pos, ',');
737 if (count < 16 || count & 1) {
738 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
742 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
744 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
748 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
749 hapd->conf->qos_map_set_len = count;
755 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
759 struct sta_info *sta;
761 u8 *qos_map_set = hapd->conf->qos_map_set;
762 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
765 if (!qos_map_set_len) {
766 wpa_printf(MSG_INFO, "QoS Map Set is not set");
770 if (hwaddr_aton(cmd, addr))
773 sta = ap_get_sta(hapd, addr);
775 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
776 "for QoS Map Configuration message",
781 if (!sta->qos_map_enabled) {
782 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
783 "support for QoS Map", MAC2STR(addr));
787 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
791 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
792 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
794 /* QoS Map Set Element */
795 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
796 wpabuf_put_u8(buf, qos_map_set_len);
797 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
799 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
800 wpabuf_head(buf), wpabuf_len(buf));
806 #endif /* CONFIG_INTERWORKING */
811 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
816 struct sta_info *sta;
818 if (hwaddr_aton(cmd, addr))
822 disassoc_timer = atoi(cmd + 17);
824 sta = ap_get_sta(hapd, addr);
826 wpa_printf(MSG_DEBUG, "Station " MACSTR
827 " not found for disassociation imminent message",
832 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
836 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
840 const char *url, *timerstr;
842 struct sta_info *sta;
844 if (hwaddr_aton(cmd, addr))
847 sta = ap_get_sta(hapd, addr);
849 wpa_printf(MSG_DEBUG, "Station " MACSTR
850 " not found for ESS disassociation imminent message",
856 if (*timerstr != ' ')
859 disassoc_timer = atoi(timerstr);
860 if (disassoc_timer < 0 || disassoc_timer > 65535)
863 url = os_strchr(timerstr, ' ');
868 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
872 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
876 const char *pos, *end;
877 int disassoc_timer = 0;
878 struct sta_info *sta;
879 u8 req_mode = 0, valid_int = 0x01;
884 u8 *nei_pos = nei_rep;
886 if (hwaddr_aton(cmd, addr)) {
887 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
891 sta = ap_get_sta(hapd, addr);
893 wpa_printf(MSG_DEBUG, "Station " MACSTR
894 " not found for BSS TM Request message",
899 pos = os_strstr(cmd, " disassoc_timer=");
902 disassoc_timer = atoi(pos);
903 if (disassoc_timer < 0 || disassoc_timer > 65535) {
904 wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
909 pos = os_strstr(cmd, " valid_int=");
912 valid_int = atoi(pos);
915 pos = os_strstr(cmd, " bss_term=");
918 req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
919 /* TODO: TSF configurable/learnable */
920 bss_term_dur[0] = 4; /* Subelement ID */
921 bss_term_dur[1] = 10; /* Length */
922 os_memset(bss_term_dur, 2, 8);
923 end = os_strchr(pos, ',');
925 wpa_printf(MSG_DEBUG, "Invalid bss_term data");
929 WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
934 * BSS Transition Candidate List Entries - Neighbor Report elements
935 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
936 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
944 pos = os_strstr(pos, " neighbor=");
947 if (nei_pos + 15 > nei_rep + sizeof(nei_rep)) {
948 wpa_printf(MSG_DEBUG,
949 "Not enough room for additional neighbor");
955 *nei_pos++ = WLAN_EID_NEIGHBOR_REPORT;
956 nei_pos++; /* length to be filled in */
958 if (hwaddr_aton(pos, nei_pos)) {
959 wpa_printf(MSG_DEBUG, "Invalid BSSID");
965 wpa_printf(MSG_DEBUG, "Missing BSSID Information");
970 val = strtol(pos, &endptr, 0);
971 WPA_PUT_LE32(nei_pos, val);
973 if (*endptr != ',') {
974 wpa_printf(MSG_DEBUG, "Missing Operating Class");
979 *nei_pos++ = atoi(pos); /* Operating Class */
980 pos = os_strchr(pos, ',');
982 wpa_printf(MSG_DEBUG, "Missing Channel Number");
987 *nei_pos++ = atoi(pos); /* Channel Number */
988 pos = os_strchr(pos, ',');
990 wpa_printf(MSG_DEBUG, "Missing PHY Type");
995 *nei_pos++ = atoi(pos); /* PHY Type */
996 end = os_strchr(pos, ' ');
997 tmp = os_strchr(pos, ',');
998 if (tmp && (!end || tmp < end)) {
999 /* Optional Subelements (hexdump) */
1003 end = os_strchr(pos, ' ');
1007 len = os_strlen(pos);
1008 if (nei_pos + len / 2 > nei_rep + sizeof(nei_rep)) {
1009 wpa_printf(MSG_DEBUG,
1010 "Not enough room for neighbor subelements");
1014 hexstr2bin(pos, nei_pos, len / 2) < 0) {
1015 wpa_printf(MSG_DEBUG,
1016 "Invalid neighbor subelement info");
1023 nei_start[1] = nei_pos - nei_start - 2;
1026 pos = os_strstr(cmd, " url=");
1030 end = os_strchr(pos, ' ');
1034 len = os_strlen(pos);
1035 url = os_malloc(len + 1);
1038 os_memcpy(url, pos, len);
1040 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
1043 if (os_strstr(cmd, " pref=1"))
1044 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1045 if (os_strstr(cmd, " abridged=1"))
1046 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1047 if (os_strstr(cmd, " disassoc_imminent=1"))
1048 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1050 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
1051 valid_int, bss_term_dur, url,
1052 nei_pos > nei_rep ? nei_rep : NULL,
1058 #endif /* CONFIG_WNM */
1061 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1062 char *buf, size_t buflen)
1070 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1072 MAC2STR(hapd->own_addr),
1073 wpa_ssid_txt(hapd->conf->ssid.ssid,
1074 hapd->conf->ssid.ssid_len));
1075 if (os_snprintf_error(end - pos, ret))
1080 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1081 hapd->conf->wps_state == 0 ? "disabled" :
1082 (hapd->conf->wps_state == 1 ? "not configured" :
1084 if (os_snprintf_error(end - pos, ret))
1088 if (hapd->conf->wps_state && hapd->conf->wpa &&
1089 hapd->conf->ssid.wpa_passphrase) {
1090 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1091 hapd->conf->ssid.wpa_passphrase);
1092 if (os_snprintf_error(end - pos, ret))
1097 if (hapd->conf->wps_state && hapd->conf->wpa &&
1098 hapd->conf->ssid.wpa_psk &&
1099 hapd->conf->ssid.wpa_psk->group) {
1100 char hex[PMK_LEN * 2 + 1];
1101 wpa_snprintf_hex(hex, sizeof(hex),
1102 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1103 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
1104 if (os_snprintf_error(end - pos, ret))
1108 #endif /* CONFIG_WPS */
1110 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1111 ret = os_snprintf(pos, end - pos, "key_mgmt=");
1112 if (os_snprintf_error(end - pos, ret))
1116 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1117 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1118 if (os_snprintf_error(end - pos, ret))
1122 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1123 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1124 if (os_snprintf_error(end - pos, ret))
1128 #ifdef CONFIG_IEEE80211R
1129 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1130 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1131 if (os_snprintf_error(end - pos, ret))
1135 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1136 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1137 if (os_snprintf_error(end - pos, ret))
1142 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1143 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1144 if (os_snprintf_error(end - pos, ret))
1148 #endif /* CONFIG_SAE */
1149 #endif /* CONFIG_IEEE80211R */
1150 #ifdef CONFIG_IEEE80211W
1151 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1152 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1153 if (os_snprintf_error(end - pos, ret))
1157 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1158 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1159 if (os_snprintf_error(end - pos, ret))
1163 #endif /* CONFIG_IEEE80211W */
1165 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1166 ret = os_snprintf(pos, end - pos, "SAE ");
1167 if (os_snprintf_error(end - pos, ret))
1171 #endif /* CONFIG_SAE */
1172 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1173 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1174 if (os_snprintf_error(end - pos, ret))
1178 if (hapd->conf->wpa_key_mgmt &
1179 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1180 ret = os_snprintf(pos, end - pos,
1181 "WPA-EAP-SUITE-B-192 ");
1182 if (os_snprintf_error(end - pos, ret))
1187 ret = os_snprintf(pos, end - pos, "\n");
1188 if (os_snprintf_error(end - pos, ret))
1193 if (hapd->conf->wpa) {
1194 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1195 wpa_cipher_txt(hapd->conf->wpa_group));
1196 if (os_snprintf_error(end - pos, ret))
1201 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1202 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
1203 if (os_snprintf_error(end - pos, ret))
1207 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1213 ret = os_snprintf(pos, end - pos, "\n");
1214 if (os_snprintf_error(end - pos, ret))
1219 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1220 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
1221 if (os_snprintf_error(end - pos, ret))
1225 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
1231 ret = os_snprintf(pos, end - pos, "\n");
1232 if (os_snprintf_error(end - pos, ret))
1241 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1246 value = os_strchr(cmd, ' ');
1251 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1253 #ifdef CONFIG_WPS_TESTING
1254 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1256 val = strtol(value, NULL, 0);
1257 if (val < 0 || val > 0xff) {
1259 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1260 "wps_version_number %ld", val);
1262 wps_version_number = val;
1263 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1265 (wps_version_number & 0xf0) >> 4,
1266 wps_version_number & 0x0f);
1267 hostapd_wps_update_ie(hapd);
1269 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1270 wps_testing_dummy_cred = atoi(value);
1271 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1272 wps_testing_dummy_cred);
1273 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1274 wps_corrupt_pkhash = atoi(value);
1275 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1276 wps_corrupt_pkhash);
1277 #endif /* CONFIG_WPS_TESTING */
1278 #ifdef CONFIG_INTERWORKING
1279 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1280 int val = atoi(value);
1284 hapd->gas_frag_limit = val;
1285 #endif /* CONFIG_INTERWORKING */
1286 #ifdef CONFIG_TESTING_OPTIONS
1287 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1288 hapd->ext_mgmt_frame_handling = atoi(value);
1289 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1290 hapd->ext_eapol_frame_io = atoi(value);
1291 #endif /* CONFIG_TESTING_OPTIONS */
1293 struct sta_info *sta;
1296 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1300 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1301 for (sta = hapd->sta_list; sta; sta = sta->next) {
1302 if (hostapd_maclist_found(
1303 hapd->conf->deny_mac,
1304 hapd->conf->num_deny_mac, sta->addr,
1306 (!vlan_id || vlan_id == sta->vlan_id))
1308 hapd, sta, sta->addr,
1309 WLAN_REASON_UNSPECIFIED);
1311 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1312 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1313 for (sta = hapd->sta_list; sta; sta = sta->next) {
1314 if (!hostapd_maclist_found(
1315 hapd->conf->accept_mac,
1316 hapd->conf->num_accept_mac,
1317 sta->addr, &vlan_id) ||
1318 (vlan_id && vlan_id != sta->vlan_id))
1320 hapd, sta, sta->addr,
1321 WLAN_REASON_UNSPECIFIED);
1330 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1331 char *buf, size_t buflen)
1335 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1337 if (os_strcmp(cmd, "version") == 0) {
1338 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1339 if (os_snprintf_error(buflen, res))
1342 } else if (os_strcmp(cmd, "tls_library") == 0) {
1343 res = tls_get_library_version(buf, buflen);
1344 if (os_snprintf_error(buflen, res))
1353 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1355 if (hostapd_enable_iface(iface) < 0) {
1356 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1363 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1365 if (hostapd_reload_iface(iface) < 0) {
1366 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1373 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1375 if (hostapd_disable_iface(iface) < 0) {
1376 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1383 #ifdef CONFIG_TESTING_OPTIONS
1385 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1387 union wpa_event_data data;
1389 enum wpa_event_type event;
1391 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1393 os_memset(&data, 0, sizeof(data));
1395 param = os_strchr(cmd, ' ');
1400 if (os_strcmp(cmd, "DETECTED") == 0)
1401 event = EVENT_DFS_RADAR_DETECTED;
1402 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1403 event = EVENT_DFS_CAC_FINISHED;
1404 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1405 event = EVENT_DFS_CAC_ABORTED;
1406 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1407 event = EVENT_DFS_NOP_FINISHED;
1409 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1414 pos = os_strstr(param, "freq=");
1416 data.dfs_event.freq = atoi(pos + 5);
1418 pos = os_strstr(param, "ht_enabled=1");
1420 data.dfs_event.ht_enabled = 1;
1422 pos = os_strstr(param, "chan_offset=");
1424 data.dfs_event.chan_offset = atoi(pos + 12);
1426 pos = os_strstr(param, "chan_width=");
1428 data.dfs_event.chan_width = atoi(pos + 11);
1430 pos = os_strstr(param, "cf1=");
1432 data.dfs_event.cf1 = atoi(pos + 4);
1434 pos = os_strstr(param, "cf2=");
1436 data.dfs_event.cf2 = atoi(pos + 4);
1438 wpa_supplicant_event(hapd, event, &data);
1444 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1450 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1452 len = os_strlen(cmd);
1457 buf = os_malloc(len);
1461 if (hexstr2bin(cmd, buf, len) < 0) {
1466 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1472 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1475 u8 src[ETH_ALEN], *buf;
1479 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1482 used = hwaddr_aton2(pos, src);
1489 len = os_strlen(pos);
1494 buf = os_malloc(len);
1498 if (hexstr2bin(pos, buf, len) < 0) {
1503 ieee802_1x_receive(hapd, src, buf, len);
1510 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1514 const u16 *pos = buf;
1516 for (i = 0; i < len / 2; i++)
1520 sum = (sum & 0xffff) + (sum >> 16);
1522 return sum ^ 0xffff;
1526 #define HWSIM_PACKETLEN 1500
1527 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1529 void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1532 struct hostapd_data *hapd = ctx;
1533 const struct ether_header *eth;
1538 if (len != HWSIM_PACKETLEN)
1541 eth = (const struct ether_header *) buf;
1542 os_memcpy(&ip, eth + 1, sizeof(ip));
1543 pos = &buf[sizeof(*eth) + sizeof(ip)];
1545 if (ip.ihl != 5 || ip.version != 4 ||
1546 ntohs(ip.tot_len) != HWSIM_IP_LEN)
1549 for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
1555 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
1556 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
1560 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1563 int enabled = atoi(cmd);
1568 if (hapd->l2_test) {
1569 l2_packet_deinit(hapd->l2_test);
1570 hapd->l2_test = NULL;
1571 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1572 "test data: Disabled");
1580 pos = os_strstr(cmd, " ifname=");
1584 ifname = hapd->conf->iface;
1586 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1587 ETHERTYPE_IP, hostapd_data_test_rx,
1589 if (hapd->l2_test == NULL)
1592 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1598 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1600 u8 dst[ETH_ALEN], src[ETH_ALEN];
1605 u8 buf[2 + HWSIM_PACKETLEN];
1606 struct ether_header *eth;
1611 if (hapd->l2_test == NULL)
1614 /* format: <dst> <src> <tos> */
1617 used = hwaddr_aton2(pos, dst);
1623 used = hwaddr_aton2(pos, src);
1628 val = strtol(pos, NULL, 0);
1629 if (val < 0 || val > 0xff)
1633 eth = (struct ether_header *) &buf[2];
1634 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1635 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1636 eth->ether_type = htons(ETHERTYPE_IP);
1637 ip = (struct iphdr *) (eth + 1);
1638 os_memset(ip, 0, sizeof(*ip));
1643 ip->tot_len = htons(HWSIM_IP_LEN);
1645 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1646 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1647 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
1648 dpos = (u8 *) (ip + 1);
1649 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
1652 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
1653 HWSIM_PACKETLEN) < 0)
1656 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1657 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1663 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1667 struct ether_header *eth;
1668 struct l2_packet_data *l2 = NULL;
1672 const char *ifname = hapd->conf->iface;
1674 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1677 cmd = os_strchr(cmd, ' ');
1683 len = os_strlen(cmd);
1684 if (len & 1 || len < ETH_HLEN * 2)
1688 buf = os_malloc(len);
1692 if (hexstr2bin(cmd, buf, len) < 0)
1695 eth = (struct ether_header *) buf;
1696 ethertype = ntohs(eth->ether_type);
1698 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1699 hostapd_data_test_rx, hapd, 1);
1703 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1704 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1707 l2_packet_deinit(l2);
1710 return res < 0 ? -1 : 0;
1714 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
1716 #ifdef WPA_TRACE_BFD
1717 extern char wpa_trace_fail_func[256];
1718 extern unsigned int wpa_trace_fail_after;
1721 wpa_trace_fail_after = atoi(cmd);
1722 pos = os_strchr(cmd, ':');
1725 os_strlcpy(wpa_trace_fail_func, pos,
1726 sizeof(wpa_trace_fail_func));
1728 wpa_trace_fail_after = 0;
1732 #else /* WPA_TRACE_BFD */
1734 #endif /* WPA_TRACE_BFD */
1738 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
1739 char *buf, size_t buflen)
1741 #ifdef WPA_TRACE_BFD
1742 extern char wpa_trace_fail_func[256];
1743 extern unsigned int wpa_trace_fail_after;
1745 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
1746 wpa_trace_fail_func);
1747 #else /* WPA_TRACE_BFD */
1749 #endif /* WPA_TRACE_BFD */
1753 static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
1755 #ifdef WPA_TRACE_BFD
1756 extern char wpa_trace_test_fail_func[256];
1757 extern unsigned int wpa_trace_test_fail_after;
1760 wpa_trace_test_fail_after = atoi(cmd);
1761 pos = os_strchr(cmd, ':');
1764 os_strlcpy(wpa_trace_test_fail_func, pos,
1765 sizeof(wpa_trace_test_fail_func));
1767 wpa_trace_test_fail_after = 0;
1771 #else /* WPA_TRACE_BFD */
1773 #endif /* WPA_TRACE_BFD */
1777 static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
1778 char *buf, size_t buflen)
1780 #ifdef WPA_TRACE_BFD
1781 extern char wpa_trace_test_fail_func[256];
1782 extern unsigned int wpa_trace_test_fail_after;
1784 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
1785 wpa_trace_test_fail_func);
1786 #else /* WPA_TRACE_BFD */
1788 #endif /* WPA_TRACE_BFD */
1791 #endif /* CONFIG_TESTING_OPTIONS */
1794 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
1798 struct csa_settings settings;
1802 ret = hostapd_parse_csa_settings(pos, &settings);
1806 for (i = 0; i < iface->num_bss; i++) {
1807 ret = hostapd_switch_channel(iface->bss[i], &settings);
1809 /* FIX: What do we do if CSA fails in the middle of
1810 * submitting multi-BSS CSA requests? */
1816 #else /* NEED_AP_MLME */
1818 #endif /* NEED_AP_MLME */
1822 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1823 int reply_size, const char *param)
1825 #ifdef RADIUS_SERVER
1826 if (os_strcmp(param, "radius_server") == 0) {
1827 return radius_server_get_mib(hapd->radius_srv, reply,
1830 #endif /* RADIUS_SERVER */
1835 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
1836 char *buf, size_t buflen)
1841 unsigned int vendor_id, subcmd;
1842 struct wpabuf *reply;
1843 size_t data_len = 0;
1845 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1846 vendor_id = strtoul(cmd, &pos, 16);
1850 subcmd = strtoul(pos, &pos, 10);
1853 if (!isblank(*pos++))
1855 data_len = os_strlen(pos);
1860 data = os_malloc(data_len);
1864 if (hexstr2bin(pos, data, data_len)) {
1865 wpa_printf(MSG_DEBUG,
1866 "Vendor command: wrong parameter format");
1872 reply = wpabuf_alloc((buflen - 1) / 2);
1878 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
1882 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
1892 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
1896 struct sta_info *sta;
1898 if (hwaddr_aton(cmd, addr))
1901 sta = ap_get_sta(hapd, addr);
1902 if (!sta || !sta->eapol_sm)
1905 eapol_auth_reauthenticate(sta->eapol_sm);
1910 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
1913 struct sta_info *sta;
1914 char *pos = cmd, *param;
1916 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
1920 pos = os_strchr(pos, ' ');
1925 sta = ap_get_sta(hapd, addr);
1926 if (!sta || !sta->eapol_sm)
1929 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
1933 static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
1934 char *buf, char *reply,
1936 struct sockaddr_un *from,
1941 os_memcpy(reply, "OK\n", 3);
1944 if (os_strcmp(buf, "PING") == 0) {
1945 os_memcpy(reply, "PONG\n", 5);
1947 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
1948 if (wpa_debug_reopen_file() < 0)
1950 } else if (os_strcmp(buf, "STATUS") == 0) {
1951 reply_len = hostapd_ctrl_iface_status(hapd, reply,
1953 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
1954 reply_len = hostapd_drv_status(hapd, reply, reply_size);
1955 } else if (os_strcmp(buf, "MIB") == 0) {
1956 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
1957 if (reply_len >= 0) {
1958 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
1959 reply_size - reply_len);
1965 if (reply_len >= 0) {
1966 res = ieee802_1x_get_mib(hapd, reply + reply_len,
1967 reply_size - reply_len);
1973 #ifndef CONFIG_NO_RADIUS
1974 if (reply_len >= 0) {
1975 res = radius_client_get_mib(hapd->radius,
1977 reply_size - reply_len);
1983 #endif /* CONFIG_NO_RADIUS */
1984 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
1985 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
1987 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
1988 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
1990 } else if (os_strncmp(buf, "STA ", 4) == 0) {
1991 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
1993 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
1994 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
1996 } else if (os_strcmp(buf, "ATTACH") == 0) {
1997 if (hostapd_ctrl_iface_attach(hapd, from, fromlen))
1999 } else if (os_strcmp(buf, "DETACH") == 0) {
2000 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
2002 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
2003 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
2006 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
2007 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
2009 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
2010 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
2012 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
2013 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
2015 } else if (os_strcmp(buf, "STOP_AP") == 0) {
2016 if (hostapd_ctrl_iface_stop_ap(hapd))
2018 #ifdef CONFIG_IEEE80211W
2020 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
2021 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
2023 #endif /* NEED_AP_MLME */
2024 #endif /* CONFIG_IEEE80211W */
2026 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
2027 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
2029 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
2030 reply_len = hostapd_ctrl_iface_wps_check_pin(
2031 hapd, buf + 14, reply, reply_size);
2032 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
2033 if (hostapd_wps_button_pushed(hapd, NULL))
2035 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
2036 if (hostapd_wps_cancel(hapd))
2038 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
2039 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
2041 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
2042 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
2044 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
2045 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
2047 #ifdef CONFIG_WPS_NFC
2048 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
2049 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
2051 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2052 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
2053 hapd, buf + 21, reply, reply_size);
2054 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
2055 reply_len = hostapd_ctrl_iface_wps_nfc_token(
2056 hapd, buf + 14, reply, reply_size);
2057 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2058 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
2059 hapd, buf + 21, reply, reply_size);
2060 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
2061 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
2063 #endif /* CONFIG_WPS_NFC */
2064 #endif /* CONFIG_WPS */
2065 #ifdef CONFIG_INTERWORKING
2066 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
2067 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
2069 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
2070 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
2072 #endif /* CONFIG_INTERWORKING */
2074 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
2075 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
2077 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
2078 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
2080 #endif /* CONFIG_HS20 */
2082 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
2083 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
2085 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
2086 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
2088 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
2089 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
2091 #endif /* CONFIG_WNM */
2092 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
2093 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
2095 } else if (os_strncmp(buf, "SET ", 4) == 0) {
2096 if (hostapd_ctrl_iface_set(hapd, buf + 4))
2098 } else if (os_strncmp(buf, "GET ", 4) == 0) {
2099 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
2101 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
2102 if (hostapd_ctrl_iface_enable(hapd->iface))
2104 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
2105 if (hostapd_ctrl_iface_reload(hapd->iface))
2107 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
2108 if (hostapd_ctrl_iface_disable(hapd->iface))
2110 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
2111 if (ieee802_11_set_beacon(hapd))
2113 #ifdef CONFIG_TESTING_OPTIONS
2114 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
2115 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
2117 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
2118 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
2120 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
2121 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
2123 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
2124 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
2126 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
2127 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
2129 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
2130 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
2132 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
2133 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
2135 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
2136 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
2138 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
2139 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
2141 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
2142 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
2143 #endif /* CONFIG_TESTING_OPTIONS */
2144 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
2145 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
2147 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
2148 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
2150 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
2151 ieee802_1x_erp_flush(hapd);
2152 #ifdef RADIUS_SERVER
2153 radius_server_erp_flush(hapd->radius_srv);
2154 #endif /* RADIUS_SERVER */
2155 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
2156 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
2158 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
2159 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
2162 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2166 if (reply_len < 0) {
2167 os_memcpy(reply, "FAIL\n", 5);
2175 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
2178 struct hostapd_data *hapd = eloop_ctx;
2181 struct sockaddr_un from;
2182 socklen_t fromlen = sizeof(from);
2184 const int reply_size = 4096;
2186 int level = MSG_DEBUG;
2188 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2189 (struct sockaddr *) &from, &fromlen);
2191 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2196 if (os_strcmp(buf, "PING") == 0)
2197 level = MSG_EXCESSIVE;
2198 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
2200 reply = os_malloc(reply_size);
2201 if (reply == NULL) {
2202 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
2204 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2210 reply_len = hostapd_ctrl_iface_receive_process(hapd, buf,
2214 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2216 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2223 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
2228 if (hapd->conf->ctrl_interface == NULL)
2231 len = os_strlen(hapd->conf->ctrl_interface) +
2232 os_strlen(hapd->conf->iface) + 2;
2233 buf = os_malloc(len);
2237 os_snprintf(buf, len, "%s/%s",
2238 hapd->conf->ctrl_interface, hapd->conf->iface);
2239 buf[len - 1] = '\0';
2244 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
2245 enum wpa_msg_type type,
2246 const char *txt, size_t len)
2248 struct hostapd_data *hapd = ctx;
2251 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
2255 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
2257 struct sockaddr_un addr;
2261 if (hapd->ctrl_sock > -1) {
2262 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2266 if (hapd->conf->ctrl_interface == NULL)
2269 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2270 if (errno == EEXIST) {
2271 wpa_printf(MSG_DEBUG, "Using existing control "
2272 "interface directory.");
2274 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2280 if (hapd->conf->ctrl_interface_gid_set &&
2281 chown(hapd->conf->ctrl_interface, -1,
2282 hapd->conf->ctrl_interface_gid) < 0) {
2283 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2288 if (!hapd->conf->ctrl_interface_gid_set &&
2289 hapd->iface->interfaces->ctrl_iface_group &&
2290 chown(hapd->conf->ctrl_interface, -1,
2291 hapd->iface->interfaces->ctrl_iface_group) < 0) {
2292 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2299 * Android is using umask 0077 which would leave the control interface
2300 * directory without group access. This breaks things since Wi-Fi
2301 * framework assumes that this directory can be accessed by other
2302 * applications in the wifi group. Fix this by adding group access even
2303 * if umask value would prevent this.
2305 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2306 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
2308 /* Try to continue anyway */
2310 #endif /* ANDROID */
2312 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
2313 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
2316 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2318 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
2322 os_memset(&addr, 0, sizeof(addr));
2324 addr.sun_len = sizeof(addr);
2325 #endif /* __FreeBSD__ */
2326 addr.sun_family = AF_UNIX;
2327 fname = hostapd_ctrl_iface_path(hapd);
2330 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2331 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2332 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2334 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2335 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2336 " allow connections - assuming it was left"
2337 "over from forced program termination");
2338 if (unlink(fname) < 0) {
2339 wpa_printf(MSG_ERROR,
2340 "Could not unlink existing ctrl_iface socket '%s': %s",
2341 fname, strerror(errno));
2344 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2346 wpa_printf(MSG_ERROR,
2347 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
2351 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2352 "ctrl_iface socket '%s'", fname);
2354 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2355 "be in use - cannot override it");
2356 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2357 "not used anymore", fname);
2364 if (hapd->conf->ctrl_interface_gid_set &&
2365 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
2366 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2371 if (!hapd->conf->ctrl_interface_gid_set &&
2372 hapd->iface->interfaces->ctrl_iface_group &&
2373 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
2374 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2379 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2380 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2386 hapd->ctrl_sock = s;
2387 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
2389 hostapd_ctrl_iface_deinit(hapd);
2392 hapd->msg_ctx = hapd;
2393 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
2408 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
2410 struct wpa_ctrl_dst *dst, *prev;
2412 if (hapd->ctrl_sock > -1) {
2414 eloop_unregister_read_sock(hapd->ctrl_sock);
2415 close(hapd->ctrl_sock);
2416 hapd->ctrl_sock = -1;
2417 fname = hostapd_ctrl_iface_path(hapd);
2422 if (hapd->conf->ctrl_interface &&
2423 rmdir(hapd->conf->ctrl_interface) < 0) {
2424 if (errno == ENOTEMPTY) {
2425 wpa_printf(MSG_DEBUG, "Control interface "
2426 "directory not empty - leaving it "
2429 wpa_printf(MSG_ERROR,
2430 "rmdir[ctrl_interface=%s]: %s",
2431 hapd->conf->ctrl_interface,
2437 dst = hapd->ctrl_dst;
2438 hapd->ctrl_dst = NULL;
2445 #ifdef CONFIG_TESTING_OPTIONS
2446 l2_packet_deinit(hapd->l2_test);
2447 hapd->l2_test = NULL;
2448 #endif /* CONFIG_TESTING_OPTIONS */
2452 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
2455 if (hostapd_add_iface(interfaces, buf) < 0) {
2456 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
2463 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
2466 if (hostapd_remove_iface(interfaces, buf) < 0) {
2467 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
2474 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
2475 struct sockaddr_un *from,
2478 struct wpa_ctrl_dst *dst;
2480 dst = os_zalloc(sizeof(*dst));
2483 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
2484 dst->addrlen = fromlen;
2485 dst->debug_level = MSG_INFO;
2486 dst->next = interfaces->global_ctrl_dst;
2487 interfaces->global_ctrl_dst = dst;
2488 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached (global)",
2490 fromlen - offsetof(struct sockaddr_un, sun_path));
2495 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
2496 struct sockaddr_un *from,
2499 struct wpa_ctrl_dst *dst, *prev = NULL;
2501 dst = interfaces->global_ctrl_dst;
2503 if (fromlen == dst->addrlen &&
2504 os_memcmp(from->sun_path, dst->addr.sun_path,
2505 fromlen - offsetof(struct sockaddr_un, sun_path))
2507 wpa_hexdump(MSG_DEBUG,
2508 "CTRL_IFACE monitor detached (global)",
2511 offsetof(struct sockaddr_un, sun_path));
2513 interfaces->global_ctrl_dst = dst->next;
2515 prev->next = dst->next;
2526 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
2528 #ifdef CONFIG_WPS_TESTING
2529 wps_version_number = 0x20;
2530 wps_testing_dummy_cred = 0;
2531 wps_corrupt_pkhash = 0;
2532 #endif /* CONFIG_WPS_TESTING */
2539 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
2542 char ifname[IFNAMSIZ + 1];
2543 struct fst_iface_cfg cfg;
2544 struct hostapd_data *hapd;
2545 struct fst_wpa_obj iface_obj;
2547 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
2548 hapd = hostapd_get_iface(interfaces, ifname);
2550 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2551 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
2553 if (hapd->iface->fst)
2563 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
2566 char ifname[IFNAMSIZ + 1];
2567 struct hostapd_data * hapd;
2569 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
2570 hapd = hostapd_get_iface(interfaces, ifname);
2572 if (!fst_iface_detach(ifname)) {
2573 hapd->iface->fst = NULL;
2582 #endif /* CONFIG_FST */
2584 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
2586 char *buf, char *reply,
2588 struct sockaddr_un *from,
2592 struct hostapd_data *hapd = NULL;
2594 for (i = 0; hapd == NULL && i < interfaces->count; i++) {
2595 struct hostapd_iface *iface = interfaces->iface[i];
2597 for (j = 0; j < iface->num_bss; j++) {
2598 hapd = iface->bss[j];
2599 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2608 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
2609 if (os_snprintf_error(reply_size, res))
2614 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
2619 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
2622 void *interfaces = eloop_ctx;
2625 struct sockaddr_un from;
2626 socklen_t fromlen = sizeof(from);
2629 const int reply_size = 4096;
2631 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2632 (struct sockaddr *) &from, &fromlen);
2634 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2639 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
2641 reply = os_malloc(reply_size);
2642 if (reply == NULL) {
2643 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
2645 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2651 os_memcpy(reply, "OK\n", 3);
2654 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
2655 char *pos = os_strchr(buf + 7, ' ');
2659 reply_len = hostapd_global_ctrl_iface_ifname(
2660 interfaces, buf + 7, pos, reply, reply_size,
2666 if (os_strcmp(buf, "PING") == 0) {
2667 os_memcpy(reply, "PONG\n", 5);
2669 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2670 if (wpa_debug_reopen_file() < 0)
2672 } else if (os_strcmp(buf, "FLUSH") == 0) {
2673 hostapd_ctrl_iface_flush(interfaces);
2674 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
2675 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
2677 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
2678 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
2680 } else if (os_strcmp(buf, "ATTACH") == 0) {
2681 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
2684 } else if (os_strcmp(buf, "DETACH") == 0) {
2685 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
2688 #ifdef CONFIG_MODULE_TESTS
2689 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
2690 int hapd_module_tests(void);
2691 if (hapd_module_tests() < 0)
2693 #endif /* CONFIG_MODULE_TESTS */
2695 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
2696 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
2697 reply_len = os_snprintf(reply, reply_size, "OK\n");
2700 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
2701 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
2702 reply_len = os_snprintf(reply, reply_size, "OK\n");
2705 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
2706 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
2707 #endif /* CONFIG_FST */
2709 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
2715 if (reply_len < 0) {
2716 os_memcpy(reply, "FAIL\n", 5);
2720 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2722 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2729 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
2734 if (interface->global_iface_path == NULL)
2737 len = os_strlen(interface->global_iface_path) +
2738 os_strlen(interface->global_iface_name) + 2;
2739 buf = os_malloc(len);
2743 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
2744 interface->global_iface_name);
2745 buf[len - 1] = '\0';
2750 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
2752 struct sockaddr_un addr;
2756 if (interface->global_iface_path == NULL) {
2757 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
2761 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
2762 if (errno == EEXIST) {
2763 wpa_printf(MSG_DEBUG, "Using existing control "
2764 "interface directory.");
2766 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2770 } else if (interface->ctrl_iface_group &&
2771 chown(interface->global_iface_path, -1,
2772 interface->ctrl_iface_group) < 0) {
2773 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2778 if (os_strlen(interface->global_iface_path) + 1 +
2779 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
2782 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2784 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
2788 os_memset(&addr, 0, sizeof(addr));
2790 addr.sun_len = sizeof(addr);
2791 #endif /* __FreeBSD__ */
2792 addr.sun_family = AF_UNIX;
2793 fname = hostapd_global_ctrl_iface_path(interface);
2796 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2797 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2798 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2800 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2801 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2802 " allow connections - assuming it was left"
2803 "over from forced program termination");
2804 if (unlink(fname) < 0) {
2805 wpa_printf(MSG_ERROR,
2806 "Could not unlink existing ctrl_iface socket '%s': %s",
2807 fname, strerror(errno));
2810 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2812 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
2816 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2817 "ctrl_iface socket '%s'", fname);
2819 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2820 "be in use - cannot override it");
2821 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2822 "not used anymore", fname);
2829 if (interface->ctrl_iface_group &&
2830 chown(fname, -1, interface->ctrl_iface_group) < 0) {
2831 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2836 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2837 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2843 interface->global_ctrl_sock = s;
2844 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
2860 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
2863 struct wpa_ctrl_dst *dst, *prev;
2865 if (interfaces->global_ctrl_sock > -1) {
2866 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
2867 close(interfaces->global_ctrl_sock);
2868 interfaces->global_ctrl_sock = -1;
2869 fname = hostapd_global_ctrl_iface_path(interfaces);
2875 if (interfaces->global_iface_path &&
2876 rmdir(interfaces->global_iface_path) < 0) {
2877 if (errno == ENOTEMPTY) {
2878 wpa_printf(MSG_DEBUG, "Control interface "
2879 "directory not empty - leaving it "
2882 wpa_printf(MSG_ERROR,
2883 "rmdir[ctrl_interface=%s]: %s",
2884 interfaces->global_iface_path,
2890 os_free(interfaces->global_iface_path);
2891 interfaces->global_iface_path = NULL;
2893 dst = interfaces->global_ctrl_dst;
2894 interfaces->global_ctrl_dst = NULL;
2903 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
2904 enum wpa_msg_type type,
2905 const char *buf, size_t len)
2907 struct wpa_ctrl_dst *dst, *next;
2914 if (type != WPA_MSG_ONLY_GLOBAL) {
2915 s = hapd->ctrl_sock;
2916 dst = hapd->ctrl_dst;
2918 s = hapd->iface->interfaces->global_ctrl_sock;
2919 dst = hapd->iface->interfaces->global_ctrl_dst;
2922 if (s < 0 || dst == NULL)
2925 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
2926 io[0].iov_base = levelstr;
2927 io[0].iov_len = os_strlen(levelstr);
2928 io[1].iov_base = (char *) buf;
2929 io[1].iov_len = len;
2930 os_memset(&msg, 0, sizeof(msg));
2937 if (level >= dst->debug_level) {
2938 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
2939 (u8 *) dst->addr.sun_path, dst->addrlen -
2940 offsetof(struct sockaddr_un, sun_path));
2941 msg.msg_name = &dst->addr;
2942 msg.msg_namelen = dst->addrlen;
2943 if (sendmsg(s, &msg, 0) < 0) {
2945 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
2947 idx, errno, strerror(errno));
2949 if (dst->errors > 10 || _errno == ENOENT) {
2950 if (type != WPA_MSG_ONLY_GLOBAL)
2951 hostapd_ctrl_iface_detach(
2955 hostapd_global_ctrl_iface_detach(
2956 hapd->iface->interfaces,
2968 #endif /* CONFIG_NATIVE_WINDOWS */