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) {
1111 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1112 if (os_snprintf_error(end - pos, ret))
1117 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1118 ret = os_snprintf(pos, end - pos, "key_mgmt=");
1119 if (os_snprintf_error(end - pos, ret))
1123 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1124 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1125 if (os_snprintf_error(end - pos, ret))
1129 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1130 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1131 if (os_snprintf_error(end - pos, ret))
1135 #ifdef CONFIG_IEEE80211R
1136 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1137 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1138 if (os_snprintf_error(end - pos, ret))
1142 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1143 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1144 if (os_snprintf_error(end - pos, ret))
1149 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1150 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1151 if (os_snprintf_error(end - pos, ret))
1155 #endif /* CONFIG_SAE */
1156 #endif /* CONFIG_IEEE80211R */
1157 #ifdef CONFIG_IEEE80211W
1158 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1159 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1160 if (os_snprintf_error(end - pos, ret))
1164 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1165 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1166 if (os_snprintf_error(end - pos, ret))
1170 #endif /* CONFIG_IEEE80211W */
1172 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1173 ret = os_snprintf(pos, end - pos, "SAE ");
1174 if (os_snprintf_error(end - pos, ret))
1178 #endif /* CONFIG_SAE */
1179 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1180 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1181 if (os_snprintf_error(end - pos, ret))
1185 if (hapd->conf->wpa_key_mgmt &
1186 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1187 ret = os_snprintf(pos, end - pos,
1188 "WPA-EAP-SUITE-B-192 ");
1189 if (os_snprintf_error(end - pos, ret))
1194 ret = os_snprintf(pos, end - pos, "\n");
1195 if (os_snprintf_error(end - pos, ret))
1200 if (hapd->conf->wpa) {
1201 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1202 wpa_cipher_txt(hapd->conf->wpa_group));
1203 if (os_snprintf_error(end - pos, ret))
1208 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1209 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
1210 if (os_snprintf_error(end - pos, ret))
1214 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1220 ret = os_snprintf(pos, end - pos, "\n");
1221 if (os_snprintf_error(end - pos, ret))
1226 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1227 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
1228 if (os_snprintf_error(end - pos, ret))
1232 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
1238 ret = os_snprintf(pos, end - pos, "\n");
1239 if (os_snprintf_error(end - pos, ret))
1248 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1253 value = os_strchr(cmd, ' ');
1258 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1260 #ifdef CONFIG_WPS_TESTING
1261 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1263 val = strtol(value, NULL, 0);
1264 if (val < 0 || val > 0xff) {
1266 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1267 "wps_version_number %ld", val);
1269 wps_version_number = val;
1270 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1272 (wps_version_number & 0xf0) >> 4,
1273 wps_version_number & 0x0f);
1274 hostapd_wps_update_ie(hapd);
1276 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1277 wps_testing_dummy_cred = atoi(value);
1278 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1279 wps_testing_dummy_cred);
1280 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1281 wps_corrupt_pkhash = atoi(value);
1282 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1283 wps_corrupt_pkhash);
1284 #endif /* CONFIG_WPS_TESTING */
1285 #ifdef CONFIG_INTERWORKING
1286 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1287 int val = atoi(value);
1291 hapd->gas_frag_limit = val;
1292 #endif /* CONFIG_INTERWORKING */
1293 #ifdef CONFIG_TESTING_OPTIONS
1294 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1295 hapd->ext_mgmt_frame_handling = atoi(value);
1296 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1297 hapd->ext_eapol_frame_io = atoi(value);
1298 #endif /* CONFIG_TESTING_OPTIONS */
1300 struct sta_info *sta;
1303 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1307 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1308 for (sta = hapd->sta_list; sta; sta = sta->next) {
1309 if (hostapd_maclist_found(
1310 hapd->conf->deny_mac,
1311 hapd->conf->num_deny_mac, sta->addr,
1313 (!vlan_id || vlan_id == sta->vlan_id))
1315 hapd, sta, sta->addr,
1316 WLAN_REASON_UNSPECIFIED);
1318 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1319 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1320 for (sta = hapd->sta_list; sta; sta = sta->next) {
1321 if (!hostapd_maclist_found(
1322 hapd->conf->accept_mac,
1323 hapd->conf->num_accept_mac,
1324 sta->addr, &vlan_id) ||
1325 (vlan_id && vlan_id != sta->vlan_id))
1327 hapd, sta, sta->addr,
1328 WLAN_REASON_UNSPECIFIED);
1337 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1338 char *buf, size_t buflen)
1342 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1344 if (os_strcmp(cmd, "version") == 0) {
1345 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1346 if (os_snprintf_error(buflen, res))
1349 } else if (os_strcmp(cmd, "tls_library") == 0) {
1350 res = tls_get_library_version(buf, buflen);
1351 if (os_snprintf_error(buflen, res))
1360 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1362 if (hostapd_enable_iface(iface) < 0) {
1363 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1370 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1372 if (hostapd_reload_iface(iface) < 0) {
1373 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1380 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1382 if (hostapd_disable_iface(iface) < 0) {
1383 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1390 #ifdef CONFIG_TESTING_OPTIONS
1392 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1394 union wpa_event_data data;
1396 enum wpa_event_type event;
1398 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1400 os_memset(&data, 0, sizeof(data));
1402 param = os_strchr(cmd, ' ');
1407 if (os_strcmp(cmd, "DETECTED") == 0)
1408 event = EVENT_DFS_RADAR_DETECTED;
1409 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1410 event = EVENT_DFS_CAC_FINISHED;
1411 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1412 event = EVENT_DFS_CAC_ABORTED;
1413 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1414 event = EVENT_DFS_NOP_FINISHED;
1416 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1421 pos = os_strstr(param, "freq=");
1423 data.dfs_event.freq = atoi(pos + 5);
1425 pos = os_strstr(param, "ht_enabled=1");
1427 data.dfs_event.ht_enabled = 1;
1429 pos = os_strstr(param, "chan_offset=");
1431 data.dfs_event.chan_offset = atoi(pos + 12);
1433 pos = os_strstr(param, "chan_width=");
1435 data.dfs_event.chan_width = atoi(pos + 11);
1437 pos = os_strstr(param, "cf1=");
1439 data.dfs_event.cf1 = atoi(pos + 4);
1441 pos = os_strstr(param, "cf2=");
1443 data.dfs_event.cf2 = atoi(pos + 4);
1445 wpa_supplicant_event(hapd, event, &data);
1451 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1457 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1459 len = os_strlen(cmd);
1464 buf = os_malloc(len);
1468 if (hexstr2bin(cmd, buf, len) < 0) {
1473 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1479 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1482 u8 src[ETH_ALEN], *buf;
1486 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1489 used = hwaddr_aton2(pos, src);
1496 len = os_strlen(pos);
1501 buf = os_malloc(len);
1505 if (hexstr2bin(pos, buf, len) < 0) {
1510 ieee802_1x_receive(hapd, src, buf, len);
1517 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1521 const u16 *pos = buf;
1523 for (i = 0; i < len / 2; i++)
1527 sum = (sum & 0xffff) + (sum >> 16);
1529 return sum ^ 0xffff;
1533 #define HWSIM_PACKETLEN 1500
1534 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1536 void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1539 struct hostapd_data *hapd = ctx;
1540 const struct ether_header *eth;
1545 if (len != HWSIM_PACKETLEN)
1548 eth = (const struct ether_header *) buf;
1549 os_memcpy(&ip, eth + 1, sizeof(ip));
1550 pos = &buf[sizeof(*eth) + sizeof(ip)];
1552 if (ip.ihl != 5 || ip.version != 4 ||
1553 ntohs(ip.tot_len) != HWSIM_IP_LEN)
1556 for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
1562 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
1563 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
1567 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1570 int enabled = atoi(cmd);
1575 if (hapd->l2_test) {
1576 l2_packet_deinit(hapd->l2_test);
1577 hapd->l2_test = NULL;
1578 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1579 "test data: Disabled");
1587 pos = os_strstr(cmd, " ifname=");
1591 ifname = hapd->conf->iface;
1593 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1594 ETHERTYPE_IP, hostapd_data_test_rx,
1596 if (hapd->l2_test == NULL)
1599 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1605 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1607 u8 dst[ETH_ALEN], src[ETH_ALEN];
1612 u8 buf[2 + HWSIM_PACKETLEN];
1613 struct ether_header *eth;
1618 if (hapd->l2_test == NULL)
1621 /* format: <dst> <src> <tos> */
1624 used = hwaddr_aton2(pos, dst);
1630 used = hwaddr_aton2(pos, src);
1635 val = strtol(pos, NULL, 0);
1636 if (val < 0 || val > 0xff)
1640 eth = (struct ether_header *) &buf[2];
1641 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1642 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1643 eth->ether_type = htons(ETHERTYPE_IP);
1644 ip = (struct iphdr *) (eth + 1);
1645 os_memset(ip, 0, sizeof(*ip));
1650 ip->tot_len = htons(HWSIM_IP_LEN);
1652 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1653 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1654 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
1655 dpos = (u8 *) (ip + 1);
1656 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
1659 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
1660 HWSIM_PACKETLEN) < 0)
1663 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1664 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1670 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1674 struct ether_header *eth;
1675 struct l2_packet_data *l2 = NULL;
1679 const char *ifname = hapd->conf->iface;
1681 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1684 cmd = os_strchr(cmd, ' ');
1690 len = os_strlen(cmd);
1691 if (len & 1 || len < ETH_HLEN * 2)
1695 buf = os_malloc(len);
1699 if (hexstr2bin(cmd, buf, len) < 0)
1702 eth = (struct ether_header *) buf;
1703 ethertype = ntohs(eth->ether_type);
1705 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1706 hostapd_data_test_rx, hapd, 1);
1710 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1711 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1714 l2_packet_deinit(l2);
1717 return res < 0 ? -1 : 0;
1721 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
1723 #ifdef WPA_TRACE_BFD
1724 extern char wpa_trace_fail_func[256];
1725 extern unsigned int wpa_trace_fail_after;
1728 wpa_trace_fail_after = atoi(cmd);
1729 pos = os_strchr(cmd, ':');
1732 os_strlcpy(wpa_trace_fail_func, pos,
1733 sizeof(wpa_trace_fail_func));
1735 wpa_trace_fail_after = 0;
1739 #else /* WPA_TRACE_BFD */
1741 #endif /* WPA_TRACE_BFD */
1745 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
1746 char *buf, size_t buflen)
1748 #ifdef WPA_TRACE_BFD
1749 extern char wpa_trace_fail_func[256];
1750 extern unsigned int wpa_trace_fail_after;
1752 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
1753 wpa_trace_fail_func);
1754 #else /* WPA_TRACE_BFD */
1756 #endif /* WPA_TRACE_BFD */
1760 static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
1762 #ifdef WPA_TRACE_BFD
1763 extern char wpa_trace_test_fail_func[256];
1764 extern unsigned int wpa_trace_test_fail_after;
1767 wpa_trace_test_fail_after = atoi(cmd);
1768 pos = os_strchr(cmd, ':');
1771 os_strlcpy(wpa_trace_test_fail_func, pos,
1772 sizeof(wpa_trace_test_fail_func));
1774 wpa_trace_test_fail_after = 0;
1778 #else /* WPA_TRACE_BFD */
1780 #endif /* WPA_TRACE_BFD */
1784 static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
1785 char *buf, size_t buflen)
1787 #ifdef WPA_TRACE_BFD
1788 extern char wpa_trace_test_fail_func[256];
1789 extern unsigned int wpa_trace_test_fail_after;
1791 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
1792 wpa_trace_test_fail_func);
1793 #else /* WPA_TRACE_BFD */
1795 #endif /* WPA_TRACE_BFD */
1798 #endif /* CONFIG_TESTING_OPTIONS */
1801 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
1805 struct csa_settings settings;
1809 ret = hostapd_parse_csa_settings(pos, &settings);
1813 for (i = 0; i < iface->num_bss; i++) {
1814 ret = hostapd_switch_channel(iface->bss[i], &settings);
1816 /* FIX: What do we do if CSA fails in the middle of
1817 * submitting multi-BSS CSA requests? */
1823 #else /* NEED_AP_MLME */
1825 #endif /* NEED_AP_MLME */
1829 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1830 int reply_size, const char *param)
1832 #ifdef RADIUS_SERVER
1833 if (os_strcmp(param, "radius_server") == 0) {
1834 return radius_server_get_mib(hapd->radius_srv, reply,
1837 #endif /* RADIUS_SERVER */
1842 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
1843 char *buf, size_t buflen)
1848 unsigned int vendor_id, subcmd;
1849 struct wpabuf *reply;
1850 size_t data_len = 0;
1852 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1853 vendor_id = strtoul(cmd, &pos, 16);
1857 subcmd = strtoul(pos, &pos, 10);
1860 if (!isblank(*pos++))
1862 data_len = os_strlen(pos);
1867 data = os_malloc(data_len);
1871 if (hexstr2bin(pos, data, data_len)) {
1872 wpa_printf(MSG_DEBUG,
1873 "Vendor command: wrong parameter format");
1879 reply = wpabuf_alloc((buflen - 1) / 2);
1885 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
1889 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
1899 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
1903 struct sta_info *sta;
1905 if (hwaddr_aton(cmd, addr))
1908 sta = ap_get_sta(hapd, addr);
1909 if (!sta || !sta->eapol_sm)
1912 eapol_auth_reauthenticate(sta->eapol_sm);
1917 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
1920 struct sta_info *sta;
1921 char *pos = cmd, *param;
1923 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
1927 pos = os_strchr(pos, ' ');
1932 sta = ap_get_sta(hapd, addr);
1933 if (!sta || !sta->eapol_sm)
1936 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
1940 static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
1941 char *buf, char *reply,
1943 struct sockaddr_un *from,
1948 os_memcpy(reply, "OK\n", 3);
1951 if (os_strcmp(buf, "PING") == 0) {
1952 os_memcpy(reply, "PONG\n", 5);
1954 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
1955 if (wpa_debug_reopen_file() < 0)
1957 } else if (os_strcmp(buf, "STATUS") == 0) {
1958 reply_len = hostapd_ctrl_iface_status(hapd, reply,
1960 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
1961 reply_len = hostapd_drv_status(hapd, reply, reply_size);
1962 } else if (os_strcmp(buf, "MIB") == 0) {
1963 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
1964 if (reply_len >= 0) {
1965 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
1966 reply_size - reply_len);
1972 if (reply_len >= 0) {
1973 res = ieee802_1x_get_mib(hapd, reply + reply_len,
1974 reply_size - reply_len);
1980 #ifndef CONFIG_NO_RADIUS
1981 if (reply_len >= 0) {
1982 res = radius_client_get_mib(hapd->radius,
1984 reply_size - reply_len);
1990 #endif /* CONFIG_NO_RADIUS */
1991 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
1992 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
1994 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
1995 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
1997 } else if (os_strncmp(buf, "STA ", 4) == 0) {
1998 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
2000 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
2001 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
2003 } else if (os_strcmp(buf, "ATTACH") == 0) {
2004 if (hostapd_ctrl_iface_attach(hapd, from, fromlen))
2006 } else if (os_strcmp(buf, "DETACH") == 0) {
2007 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
2009 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
2010 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
2013 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
2014 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
2016 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
2017 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
2019 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
2020 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
2022 } else if (os_strcmp(buf, "STOP_AP") == 0) {
2023 if (hostapd_ctrl_iface_stop_ap(hapd))
2025 #ifdef CONFIG_IEEE80211W
2027 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
2028 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
2030 #endif /* NEED_AP_MLME */
2031 #endif /* CONFIG_IEEE80211W */
2033 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
2034 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
2036 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
2037 reply_len = hostapd_ctrl_iface_wps_check_pin(
2038 hapd, buf + 14, reply, reply_size);
2039 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
2040 if (hostapd_wps_button_pushed(hapd, NULL))
2042 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
2043 if (hostapd_wps_cancel(hapd))
2045 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
2046 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
2048 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
2049 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
2051 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
2052 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
2054 #ifdef CONFIG_WPS_NFC
2055 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
2056 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
2058 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2059 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
2060 hapd, buf + 21, reply, reply_size);
2061 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
2062 reply_len = hostapd_ctrl_iface_wps_nfc_token(
2063 hapd, buf + 14, reply, reply_size);
2064 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2065 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
2066 hapd, buf + 21, reply, reply_size);
2067 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
2068 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
2070 #endif /* CONFIG_WPS_NFC */
2071 #endif /* CONFIG_WPS */
2072 #ifdef CONFIG_INTERWORKING
2073 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
2074 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
2076 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
2077 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
2079 #endif /* CONFIG_INTERWORKING */
2081 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
2082 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
2084 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
2085 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
2087 #endif /* CONFIG_HS20 */
2089 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
2090 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
2092 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
2093 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
2095 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
2096 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
2098 #endif /* CONFIG_WNM */
2099 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
2100 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
2102 } else if (os_strncmp(buf, "SET ", 4) == 0) {
2103 if (hostapd_ctrl_iface_set(hapd, buf + 4))
2105 } else if (os_strncmp(buf, "GET ", 4) == 0) {
2106 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
2108 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
2109 if (hostapd_ctrl_iface_enable(hapd->iface))
2111 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
2112 if (hostapd_ctrl_iface_reload(hapd->iface))
2114 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
2115 if (hostapd_ctrl_iface_disable(hapd->iface))
2117 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
2118 if (ieee802_11_set_beacon(hapd))
2120 #ifdef CONFIG_TESTING_OPTIONS
2121 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
2122 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
2124 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
2125 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
2127 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
2128 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
2130 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
2131 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
2133 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
2134 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
2136 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
2137 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
2139 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
2140 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
2142 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
2143 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
2145 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
2146 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
2148 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
2149 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
2150 #endif /* CONFIG_TESTING_OPTIONS */
2151 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
2152 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
2154 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
2155 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
2157 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
2158 ieee802_1x_erp_flush(hapd);
2159 #ifdef RADIUS_SERVER
2160 radius_server_erp_flush(hapd->radius_srv);
2161 #endif /* RADIUS_SERVER */
2162 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
2163 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
2165 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
2166 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
2169 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2173 if (reply_len < 0) {
2174 os_memcpy(reply, "FAIL\n", 5);
2182 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
2185 struct hostapd_data *hapd = eloop_ctx;
2188 struct sockaddr_un from;
2189 socklen_t fromlen = sizeof(from);
2191 const int reply_size = 4096;
2193 int level = MSG_DEBUG;
2195 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2196 (struct sockaddr *) &from, &fromlen);
2198 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2203 if (os_strcmp(buf, "PING") == 0)
2204 level = MSG_EXCESSIVE;
2205 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
2207 reply = os_malloc(reply_size);
2208 if (reply == NULL) {
2209 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
2211 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2217 reply_len = hostapd_ctrl_iface_receive_process(hapd, buf,
2221 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2223 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2230 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
2235 if (hapd->conf->ctrl_interface == NULL)
2238 len = os_strlen(hapd->conf->ctrl_interface) +
2239 os_strlen(hapd->conf->iface) + 2;
2240 buf = os_malloc(len);
2244 os_snprintf(buf, len, "%s/%s",
2245 hapd->conf->ctrl_interface, hapd->conf->iface);
2246 buf[len - 1] = '\0';
2251 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
2252 enum wpa_msg_type type,
2253 const char *txt, size_t len)
2255 struct hostapd_data *hapd = ctx;
2258 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
2262 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
2264 struct sockaddr_un addr;
2268 if (hapd->ctrl_sock > -1) {
2269 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2273 if (hapd->conf->ctrl_interface == NULL)
2276 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2277 if (errno == EEXIST) {
2278 wpa_printf(MSG_DEBUG, "Using existing control "
2279 "interface directory.");
2281 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2287 if (hapd->conf->ctrl_interface_gid_set &&
2288 chown(hapd->conf->ctrl_interface, -1,
2289 hapd->conf->ctrl_interface_gid) < 0) {
2290 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2295 if (!hapd->conf->ctrl_interface_gid_set &&
2296 hapd->iface->interfaces->ctrl_iface_group &&
2297 chown(hapd->conf->ctrl_interface, -1,
2298 hapd->iface->interfaces->ctrl_iface_group) < 0) {
2299 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2306 * Android is using umask 0077 which would leave the control interface
2307 * directory without group access. This breaks things since Wi-Fi
2308 * framework assumes that this directory can be accessed by other
2309 * applications in the wifi group. Fix this by adding group access even
2310 * if umask value would prevent this.
2312 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2313 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
2315 /* Try to continue anyway */
2317 #endif /* ANDROID */
2319 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
2320 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
2323 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2325 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
2329 os_memset(&addr, 0, sizeof(addr));
2331 addr.sun_len = sizeof(addr);
2332 #endif /* __FreeBSD__ */
2333 addr.sun_family = AF_UNIX;
2334 fname = hostapd_ctrl_iface_path(hapd);
2337 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2338 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2339 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2341 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2342 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2343 " allow connections - assuming it was left"
2344 "over from forced program termination");
2345 if (unlink(fname) < 0) {
2346 wpa_printf(MSG_ERROR,
2347 "Could not unlink existing ctrl_iface socket '%s': %s",
2348 fname, strerror(errno));
2351 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2353 wpa_printf(MSG_ERROR,
2354 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
2358 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2359 "ctrl_iface socket '%s'", fname);
2361 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2362 "be in use - cannot override it");
2363 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2364 "not used anymore", fname);
2371 if (hapd->conf->ctrl_interface_gid_set &&
2372 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
2373 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2378 if (!hapd->conf->ctrl_interface_gid_set &&
2379 hapd->iface->interfaces->ctrl_iface_group &&
2380 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
2381 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2386 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2387 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2393 hapd->ctrl_sock = s;
2394 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
2396 hostapd_ctrl_iface_deinit(hapd);
2399 hapd->msg_ctx = hapd;
2400 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
2415 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
2417 struct wpa_ctrl_dst *dst, *prev;
2419 if (hapd->ctrl_sock > -1) {
2421 eloop_unregister_read_sock(hapd->ctrl_sock);
2422 close(hapd->ctrl_sock);
2423 hapd->ctrl_sock = -1;
2424 fname = hostapd_ctrl_iface_path(hapd);
2429 if (hapd->conf->ctrl_interface &&
2430 rmdir(hapd->conf->ctrl_interface) < 0) {
2431 if (errno == ENOTEMPTY) {
2432 wpa_printf(MSG_DEBUG, "Control interface "
2433 "directory not empty - leaving it "
2436 wpa_printf(MSG_ERROR,
2437 "rmdir[ctrl_interface=%s]: %s",
2438 hapd->conf->ctrl_interface,
2444 dst = hapd->ctrl_dst;
2445 hapd->ctrl_dst = NULL;
2452 #ifdef CONFIG_TESTING_OPTIONS
2453 l2_packet_deinit(hapd->l2_test);
2454 hapd->l2_test = NULL;
2455 #endif /* CONFIG_TESTING_OPTIONS */
2459 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
2462 if (hostapd_add_iface(interfaces, buf) < 0) {
2463 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
2470 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
2473 if (hostapd_remove_iface(interfaces, buf) < 0) {
2474 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
2481 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
2482 struct sockaddr_un *from,
2485 struct wpa_ctrl_dst *dst;
2487 dst = os_zalloc(sizeof(*dst));
2490 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
2491 dst->addrlen = fromlen;
2492 dst->debug_level = MSG_INFO;
2493 dst->next = interfaces->global_ctrl_dst;
2494 interfaces->global_ctrl_dst = dst;
2495 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached (global)",
2497 fromlen - offsetof(struct sockaddr_un, sun_path));
2502 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
2503 struct sockaddr_un *from,
2506 struct wpa_ctrl_dst *dst, *prev = NULL;
2508 dst = interfaces->global_ctrl_dst;
2510 if (fromlen == dst->addrlen &&
2511 os_memcmp(from->sun_path, dst->addr.sun_path,
2512 fromlen - offsetof(struct sockaddr_un, sun_path))
2514 wpa_hexdump(MSG_DEBUG,
2515 "CTRL_IFACE monitor detached (global)",
2518 offsetof(struct sockaddr_un, sun_path));
2520 interfaces->global_ctrl_dst = dst->next;
2522 prev->next = dst->next;
2533 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
2535 #ifdef CONFIG_WPS_TESTING
2536 wps_version_number = 0x20;
2537 wps_testing_dummy_cred = 0;
2538 wps_corrupt_pkhash = 0;
2539 #endif /* CONFIG_WPS_TESTING */
2546 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
2549 char ifname[IFNAMSIZ + 1];
2550 struct fst_iface_cfg cfg;
2551 struct hostapd_data *hapd;
2552 struct fst_wpa_obj iface_obj;
2554 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
2555 hapd = hostapd_get_iface(interfaces, ifname);
2557 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2558 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
2560 if (hapd->iface->fst)
2570 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
2573 char ifname[IFNAMSIZ + 1];
2574 struct hostapd_data * hapd;
2576 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
2577 hapd = hostapd_get_iface(interfaces, ifname);
2579 if (!fst_iface_detach(ifname)) {
2580 hapd->iface->fst = NULL;
2581 hapd->iface->fst_ies = NULL;
2590 #endif /* CONFIG_FST */
2593 static struct hostapd_data *
2594 hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
2599 for (i = 0; i < interfaces->count; i++) {
2600 struct hostapd_iface *iface = interfaces->iface[i];
2602 for (j = 0; j < iface->num_bss; j++) {
2603 struct hostapd_data *hapd;
2605 hapd = iface->bss[j];
2606 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2615 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
2617 char *buf, char *reply,
2619 struct sockaddr_un *from,
2622 struct hostapd_data *hapd;
2624 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
2628 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
2629 if (os_snprintf_error(reply_size, res))
2634 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
2639 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
2642 void *interfaces = eloop_ctx;
2645 struct sockaddr_un from;
2646 socklen_t fromlen = sizeof(from);
2649 const int reply_size = 4096;
2651 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2652 (struct sockaddr *) &from, &fromlen);
2654 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2659 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
2661 reply = os_malloc(reply_size);
2662 if (reply == NULL) {
2663 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
2665 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2671 os_memcpy(reply, "OK\n", 3);
2674 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
2675 char *pos = os_strchr(buf + 7, ' ');
2679 reply_len = hostapd_global_ctrl_iface_ifname(
2680 interfaces, buf + 7, pos, reply, reply_size,
2686 if (os_strcmp(buf, "PING") == 0) {
2687 os_memcpy(reply, "PONG\n", 5);
2689 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2690 if (wpa_debug_reopen_file() < 0)
2692 } else if (os_strcmp(buf, "FLUSH") == 0) {
2693 hostapd_ctrl_iface_flush(interfaces);
2694 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
2695 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
2697 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
2698 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
2700 } else if (os_strcmp(buf, "ATTACH") == 0) {
2701 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
2704 } else if (os_strcmp(buf, "DETACH") == 0) {
2705 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
2708 #ifdef CONFIG_MODULE_TESTS
2709 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
2710 int hapd_module_tests(void);
2711 if (hapd_module_tests() < 0)
2713 #endif /* CONFIG_MODULE_TESTS */
2715 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
2716 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
2717 reply_len = os_snprintf(reply, reply_size, "OK\n");
2720 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
2721 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
2722 reply_len = os_snprintf(reply, reply_size, "OK\n");
2725 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
2726 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
2727 #endif /* CONFIG_FST */
2729 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
2735 if (reply_len < 0) {
2736 os_memcpy(reply, "FAIL\n", 5);
2740 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2742 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2749 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
2754 if (interface->global_iface_path == NULL)
2757 len = os_strlen(interface->global_iface_path) +
2758 os_strlen(interface->global_iface_name) + 2;
2759 buf = os_malloc(len);
2763 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
2764 interface->global_iface_name);
2765 buf[len - 1] = '\0';
2770 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
2772 struct sockaddr_un addr;
2776 if (interface->global_iface_path == NULL) {
2777 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
2781 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
2782 if (errno == EEXIST) {
2783 wpa_printf(MSG_DEBUG, "Using existing control "
2784 "interface directory.");
2786 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2790 } else if (interface->ctrl_iface_group &&
2791 chown(interface->global_iface_path, -1,
2792 interface->ctrl_iface_group) < 0) {
2793 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2798 if (os_strlen(interface->global_iface_path) + 1 +
2799 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
2802 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2804 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
2808 os_memset(&addr, 0, sizeof(addr));
2810 addr.sun_len = sizeof(addr);
2811 #endif /* __FreeBSD__ */
2812 addr.sun_family = AF_UNIX;
2813 fname = hostapd_global_ctrl_iface_path(interface);
2816 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2817 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2818 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2820 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2821 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2822 " allow connections - assuming it was left"
2823 "over from forced program termination");
2824 if (unlink(fname) < 0) {
2825 wpa_printf(MSG_ERROR,
2826 "Could not unlink existing ctrl_iface socket '%s': %s",
2827 fname, strerror(errno));
2830 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2832 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
2836 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2837 "ctrl_iface socket '%s'", fname);
2839 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2840 "be in use - cannot override it");
2841 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2842 "not used anymore", fname);
2849 if (interface->ctrl_iface_group &&
2850 chown(fname, -1, interface->ctrl_iface_group) < 0) {
2851 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2856 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2857 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2863 interface->global_ctrl_sock = s;
2864 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
2880 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
2883 struct wpa_ctrl_dst *dst, *prev;
2885 if (interfaces->global_ctrl_sock > -1) {
2886 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
2887 close(interfaces->global_ctrl_sock);
2888 interfaces->global_ctrl_sock = -1;
2889 fname = hostapd_global_ctrl_iface_path(interfaces);
2895 if (interfaces->global_iface_path &&
2896 rmdir(interfaces->global_iface_path) < 0) {
2897 if (errno == ENOTEMPTY) {
2898 wpa_printf(MSG_DEBUG, "Control interface "
2899 "directory not empty - leaving it "
2902 wpa_printf(MSG_ERROR,
2903 "rmdir[ctrl_interface=%s]: %s",
2904 interfaces->global_iface_path,
2910 os_free(interfaces->global_iface_path);
2911 interfaces->global_iface_path = NULL;
2913 dst = interfaces->global_ctrl_dst;
2914 interfaces->global_ctrl_dst = NULL;
2923 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
2924 enum wpa_msg_type type,
2925 const char *buf, size_t len)
2927 struct wpa_ctrl_dst *dst, *next;
2934 if (type != WPA_MSG_ONLY_GLOBAL) {
2935 s = hapd->ctrl_sock;
2936 dst = hapd->ctrl_dst;
2938 s = hapd->iface->interfaces->global_ctrl_sock;
2939 dst = hapd->iface->interfaces->global_ctrl_dst;
2942 if (s < 0 || dst == NULL)
2945 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
2946 io[0].iov_base = levelstr;
2947 io[0].iov_len = os_strlen(levelstr);
2948 io[1].iov_base = (char *) buf;
2949 io[1].iov_len = len;
2950 os_memset(&msg, 0, sizeof(msg));
2957 if (level >= dst->debug_level) {
2958 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
2959 (u8 *) dst->addr.sun_path, dst->addrlen -
2960 offsetof(struct sockaddr_un, sun_path));
2961 msg.msg_name = &dst->addr;
2962 msg.msg_namelen = dst->addrlen;
2963 if (sendmsg(s, &msg, 0) < 0) {
2965 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
2967 idx, errno, strerror(errno));
2969 if (dst->errors > 10 || _errno == ENOENT) {
2970 if (type != WPA_MSG_ONLY_GLOBAL)
2971 hostapd_ctrl_iface_detach(
2975 hostapd_global_ctrl_iface_detach(
2976 hapd->iface->interfaces,
2988 #endif /* CONFIG_NATIVE_WINDOWS */