2 * hostapd / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #ifndef CONFIG_NATIVE_WINDOWS
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 "drivers/driver.h"
27 #include "radius/radius_client.h"
28 #include "radius/radius_server.h"
29 #include "l2_packet/l2_packet.h"
30 #include "ap/hostapd.h"
31 #include "ap/ap_config.h"
32 #include "ap/ieee802_1x.h"
33 #include "ap/wpa_auth.h"
34 #include "ap/ieee802_11.h"
35 #include "ap/sta_info.h"
36 #include "ap/wps_hostapd.h"
37 #include "ap/ctrl_iface_ap.h"
38 #include "ap/ap_drv_ops.h"
40 #include "ap/wnm_ap.h"
41 #include "ap/wpa_auth.h"
42 #include "wps/wps_defs.h"
44 #include "config_file.h"
45 #include "ctrl_iface.h"
49 struct wpa_ctrl_dst *next;
50 struct sockaddr_un addr;
57 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
58 const char *buf, size_t len);
61 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
62 struct sockaddr_un *from,
65 struct wpa_ctrl_dst *dst;
67 dst = os_zalloc(sizeof(*dst));
70 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
71 dst->addrlen = fromlen;
72 dst->debug_level = MSG_INFO;
73 dst->next = hapd->ctrl_dst;
75 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
76 (u8 *) from->sun_path,
77 fromlen - offsetof(struct sockaddr_un, sun_path));
82 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
83 struct sockaddr_un *from,
86 struct wpa_ctrl_dst *dst, *prev = NULL;
90 if (fromlen == dst->addrlen &&
91 os_memcmp(from->sun_path, dst->addr.sun_path,
92 fromlen - offsetof(struct sockaddr_un, sun_path))
94 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
95 (u8 *) from->sun_path,
97 offsetof(struct sockaddr_un, sun_path));
99 hapd->ctrl_dst = dst->next;
101 prev->next = dst->next;
112 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
113 struct sockaddr_un *from,
117 struct wpa_ctrl_dst *dst;
119 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
121 dst = hapd->ctrl_dst;
123 if (fromlen == dst->addrlen &&
124 os_memcmp(from->sun_path, dst->addr.sun_path,
125 fromlen - offsetof(struct sockaddr_un, sun_path))
127 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
128 "level", (u8 *) from->sun_path, fromlen -
129 offsetof(struct sockaddr_un, sun_path));
130 dst->debug_level = atoi(level);
140 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
144 struct sta_info *sta;
146 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
148 if (hwaddr_aton(txtaddr, addr))
151 sta = ap_get_sta(hapd, addr);
155 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
156 "notification", MAC2STR(addr));
157 sta = ap_sta_add(hapd, addr);
161 hostapd_new_assoc_sta(hapd, sta, 0);
166 #ifdef CONFIG_IEEE80211W
168 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
172 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
174 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
176 if (hwaddr_aton(txtaddr, addr) ||
177 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
180 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
184 #endif /* NEED_AP_MLME */
185 #endif /* CONFIG_IEEE80211W */
189 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
191 char *pin = os_strchr(txt, ' ');
194 u8 addr_buf[ETH_ALEN], *addr = NULL;
201 timeout_txt = os_strchr(pin, ' ');
203 *timeout_txt++ = '\0';
204 timeout = atoi(timeout_txt);
205 pos = os_strchr(timeout_txt, ' ');
208 if (hwaddr_aton(pos, addr_buf) == 0)
214 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
218 static int hostapd_ctrl_iface_wps_check_pin(
219 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
226 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
227 (u8 *) cmd, os_strlen(cmd));
228 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
229 if (*pos < '0' || *pos > '9')
233 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
237 if (len != 4 && len != 8) {
238 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
244 unsigned int pin_val;
246 if (!wps_pin_valid(pin_val)) {
247 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
248 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
249 if (ret < 0 || (size_t) ret >= buflen)
255 ret = os_snprintf(buf, buflen, "%s", pin);
256 if (ret < 0 || (size_t) ret >= buflen)
263 #ifdef CONFIG_WPS_NFC
264 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
271 len = os_strlen(pos);
276 buf = wpabuf_alloc(len);
279 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
284 ret = hostapd_wps_nfc_tag_read(hapd, buf);
291 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
292 char *cmd, char *reply,
299 if (os_strcmp(cmd, "WPS") == 0)
301 else if (os_strcmp(cmd, "NDEF") == 0)
306 buf = hostapd_wps_nfc_config_token(hapd, ndef);
310 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
321 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
322 char *reply, size_t max_len,
328 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
332 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
343 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
344 char *cmd, char *reply,
347 if (os_strcmp(cmd, "WPS") == 0)
348 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
351 if (os_strcmp(cmd, "NDEF") == 0)
352 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
355 if (os_strcmp(cmd, "enable") == 0)
356 return hostapd_wps_nfc_token_enable(hapd);
358 if (os_strcmp(cmd, "disable") == 0) {
359 hostapd_wps_nfc_token_disable(hapd);
367 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
368 char *cmd, char *reply,
376 pos = os_strchr(cmd, ' ');
381 if (os_strcmp(cmd, "WPS") == 0)
383 else if (os_strcmp(cmd, "NDEF") == 0)
388 if (os_strcmp(pos, "WPS-CR") == 0)
389 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
395 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
406 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
410 struct wpabuf *req, *sel;
412 char *pos, *role, *type, *pos2;
415 pos = os_strchr(role, ' ');
421 pos = os_strchr(type, ' ');
426 pos2 = os_strchr(pos, ' ');
431 len = os_strlen(pos);
436 req = wpabuf_alloc(len);
439 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
444 len = os_strlen(pos2);
451 sel = wpabuf_alloc(len);
456 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
462 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
463 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
465 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
466 "reported: role=%s type=%s", role, type);
475 #endif /* CONFIG_WPS_NFC */
478 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
479 char *buf, size_t buflen)
485 pos = os_strchr(txt, ' ');
489 if (os_strcmp(txt, "disable") == 0) {
490 hostapd_wps_ap_pin_disable(hapd);
491 return os_snprintf(buf, buflen, "OK\n");
494 if (os_strcmp(txt, "random") == 0) {
497 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
500 return os_snprintf(buf, buflen, "%s", pin_txt);
503 if (os_strcmp(txt, "get") == 0) {
504 pin_txt = hostapd_wps_ap_pin_get(hapd);
507 return os_snprintf(buf, buflen, "%s", pin_txt);
510 if (os_strcmp(txt, "set") == 0) {
515 pos = os_strchr(pos, ' ');
520 if (os_strlen(pin) > buflen)
522 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
524 return os_snprintf(buf, buflen, "%s", pin);
531 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
534 char *ssid, *auth, *encr = NULL, *key = NULL;
537 pos = os_strchr(txt, ' ');
543 pos = os_strchr(pos, ' ');
547 pos = os_strchr(pos, ' ');
554 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
558 static const char * pbc_status_str(enum pbc_status status)
561 case WPS_PBC_STATUS_DISABLE:
563 case WPS_PBC_STATUS_ACTIVE:
565 case WPS_PBC_STATUS_TIMEOUT:
567 case WPS_PBC_STATUS_OVERLAP:
575 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
576 char *buf, size_t buflen)
584 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
585 pbc_status_str(hapd->wps_stats.pbc_status));
587 if (ret < 0 || ret >= end - pos)
591 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
592 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
594 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
595 "Failed" : "None")));
597 if (ret < 0 || ret >= end - pos)
601 /* If status == Failure - Add possible Reasons */
602 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
603 hapd->wps_stats.failure_reason > 0) {
604 ret = os_snprintf(pos, end - pos,
605 "Failure Reason: %s\n",
606 wps_ei_str(hapd->wps_stats.failure_reason));
608 if (ret < 0 || ret >= end - pos)
613 if (hapd->wps_stats.status) {
614 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
615 MAC2STR(hapd->wps_stats.peer_addr));
617 if (ret < 0 || ret >= end - pos)
625 #endif /* CONFIG_WPS */
629 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
635 if (hwaddr_aton(cmd, addr))
648 return hs20_send_wnm_notification(hapd, addr, 1, url);
652 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
656 int code, reauth_delay, ret;
661 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
662 if (hwaddr_aton(cmd, addr))
665 pos = os_strchr(cmd, ' ');
671 pos = os_strchr(pos, ' ');
675 reauth_delay = atoi(pos);
678 pos = os_strchr(pos, ' ');
681 url_len = os_strlen(pos);
684 req = wpabuf_alloc(4 + url_len);
687 wpabuf_put_u8(req, code);
688 wpabuf_put_le16(req, reauth_delay);
689 wpabuf_put_u8(req, url_len);
691 wpabuf_put_data(req, pos, url_len);
693 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
694 " to indicate imminent deauthentication (code=%d "
695 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
696 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
701 #endif /* CONFIG_HS20 */
704 #ifdef CONFIG_INTERWORKING
706 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
709 u8 qos_map_set[16 + 2 * 21], count = 0;
710 const char *pos = cmd;
714 if (count == sizeof(qos_map_set)) {
715 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
720 if (val < 0 || val > 255) {
721 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
725 qos_map_set[count++] = val;
726 pos = os_strchr(pos, ',');
732 if (count < 16 || count & 1) {
733 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
737 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
739 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
743 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
744 hapd->conf->qos_map_set_len = count;
750 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
754 struct sta_info *sta;
756 u8 *qos_map_set = hapd->conf->qos_map_set;
757 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
760 if (!qos_map_set_len) {
761 wpa_printf(MSG_INFO, "QoS Map Set is not set");
765 if (hwaddr_aton(cmd, addr))
768 sta = ap_get_sta(hapd, addr);
770 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
771 "for QoS Map Configuration message",
776 if (!sta->qos_map_enabled) {
777 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
778 "support for QoS Map", MAC2STR(addr));
782 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
786 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
787 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
789 /* QoS Map Set Element */
790 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
791 wpabuf_put_u8(buf, qos_map_set_len);
792 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
794 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
795 wpabuf_head(buf), wpabuf_len(buf));
801 #endif /* CONFIG_INTERWORKING */
806 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
811 struct sta_info *sta;
813 if (hwaddr_aton(cmd, addr))
817 disassoc_timer = atoi(cmd + 17);
819 sta = ap_get_sta(hapd, addr);
821 wpa_printf(MSG_DEBUG, "Station " MACSTR
822 " not found for disassociation imminent message",
827 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
831 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
835 const char *url, *timerstr;
837 struct sta_info *sta;
839 if (hwaddr_aton(cmd, addr))
842 sta = ap_get_sta(hapd, addr);
844 wpa_printf(MSG_DEBUG, "Station " MACSTR
845 " not found for ESS disassociation imminent message",
851 if (*timerstr != ' ')
854 disassoc_timer = atoi(timerstr);
855 if (disassoc_timer < 0 || disassoc_timer > 65535)
858 url = os_strchr(timerstr, ' ');
863 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
867 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
871 const char *pos, *end;
872 int disassoc_timer = 0;
873 struct sta_info *sta;
874 u8 req_mode = 0, valid_int = 0x01;
879 u8 *nei_pos = nei_rep;
881 if (hwaddr_aton(cmd, addr)) {
882 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
886 sta = ap_get_sta(hapd, addr);
888 wpa_printf(MSG_DEBUG, "Station " MACSTR
889 " not found for BSS TM Request message",
894 pos = os_strstr(cmd, " disassoc_timer=");
897 disassoc_timer = atoi(pos);
898 if (disassoc_timer < 0 || disassoc_timer > 65535) {
899 wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
904 pos = os_strstr(cmd, " valid_int=");
907 valid_int = atoi(pos);
910 pos = os_strstr(cmd, " bss_term=");
913 req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
914 /* TODO: TSF configurable/learnable */
915 bss_term_dur[0] = 4; /* Subelement ID */
916 bss_term_dur[1] = 10; /* Length */
917 os_memset(bss_term_dur, 2, 8);
918 end = os_strchr(pos, ',');
920 wpa_printf(MSG_DEBUG, "Invalid bss_term data");
924 WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
929 * BSS Transition Candidate List Entries - Neighbor Report elements
930 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
931 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
939 pos = os_strstr(pos, " neighbor=");
942 if (nei_pos + 15 > nei_rep + sizeof(nei_rep)) {
943 wpa_printf(MSG_DEBUG,
944 "Not enough room for additional neighbor");
950 *nei_pos++ = WLAN_EID_NEIGHBOR_REPORT;
951 nei_pos++; /* length to be filled in */
953 if (hwaddr_aton(pos, nei_pos)) {
954 wpa_printf(MSG_DEBUG, "Invalid BSSID");
960 wpa_printf(MSG_DEBUG, "Missing BSSID Information");
965 val = strtol(pos, &endptr, 0);
966 WPA_PUT_LE32(nei_pos, val);
968 if (*endptr != ',') {
969 wpa_printf(MSG_DEBUG, "Missing Operating Class");
974 *nei_pos++ = atoi(pos); /* Operating Class */
975 pos = os_strchr(pos, ',');
977 wpa_printf(MSG_DEBUG, "Missing Channel Number");
982 *nei_pos++ = atoi(pos); /* Channel Number */
983 pos = os_strchr(pos, ',');
985 wpa_printf(MSG_DEBUG, "Missing PHY Type");
990 *nei_pos++ = atoi(pos); /* PHY Type */
991 end = os_strchr(pos, ' ');
992 tmp = os_strchr(pos, ',');
993 if (tmp && (!end || tmp < end)) {
994 /* Optional Subelements (hexdump) */
998 end = os_strchr(pos, ' ');
1002 len = os_strlen(pos);
1003 if (nei_pos + len / 2 > nei_rep + sizeof(nei_rep)) {
1004 wpa_printf(MSG_DEBUG,
1005 "Not enough room for neighbor subelements");
1009 hexstr2bin(pos, nei_pos, len / 2) < 0) {
1010 wpa_printf(MSG_DEBUG,
1011 "Invalid neighbor subelement info");
1018 nei_start[1] = nei_pos - nei_start - 2;
1021 pos = os_strstr(cmd, " url=");
1025 end = os_strchr(pos, ' ');
1029 len = os_strlen(pos);
1030 url = os_malloc(len + 1);
1033 os_memcpy(url, pos, len);
1035 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
1038 if (os_strstr(cmd, " pref=1"))
1039 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1040 if (os_strstr(cmd, " abridged=1"))
1041 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1042 if (os_strstr(cmd, " disassoc_imminent=1"))
1043 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1045 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
1046 valid_int, bss_term_dur, url,
1047 nei_pos > nei_rep ? nei_rep : NULL,
1053 #endif /* CONFIG_WNM */
1056 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1057 char *buf, size_t buflen)
1065 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1067 MAC2STR(hapd->own_addr),
1068 wpa_ssid_txt(hapd->conf->ssid.ssid,
1069 hapd->conf->ssid.ssid_len));
1070 if (ret < 0 || ret >= end - pos)
1075 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1076 hapd->conf->wps_state == 0 ? "disabled" :
1077 (hapd->conf->wps_state == 1 ? "not configured" :
1079 if (ret < 0 || ret >= end - pos)
1083 if (hapd->conf->wps_state && hapd->conf->wpa &&
1084 hapd->conf->ssid.wpa_passphrase) {
1085 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1086 hapd->conf->ssid.wpa_passphrase);
1087 if (ret < 0 || ret >= end - pos)
1092 if (hapd->conf->wps_state && hapd->conf->wpa &&
1093 hapd->conf->ssid.wpa_psk &&
1094 hapd->conf->ssid.wpa_psk->group) {
1095 char hex[PMK_LEN * 2 + 1];
1096 wpa_snprintf_hex(hex, sizeof(hex),
1097 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1098 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
1099 if (ret < 0 || ret >= end - pos)
1103 #endif /* CONFIG_WPS */
1105 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1106 ret = os_snprintf(pos, end - pos, "key_mgmt=");
1107 if (ret < 0 || ret >= end - pos)
1111 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1112 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1113 if (ret < 0 || ret >= end - pos)
1117 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1118 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1119 if (ret < 0 || ret >= end - pos)
1123 #ifdef CONFIG_IEEE80211R
1124 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1125 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1126 if (ret < 0 || ret >= end - pos)
1130 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1131 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1132 if (ret < 0 || ret >= end - pos)
1137 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1138 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1139 if (ret < 0 || ret >= end - pos)
1143 #endif /* CONFIG_SAE */
1144 #endif /* CONFIG_IEEE80211R */
1145 #ifdef CONFIG_IEEE80211W
1146 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1147 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1148 if (ret < 0 || ret >= end - pos)
1152 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1153 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1154 if (ret < 0 || ret >= end - pos)
1158 #endif /* CONFIG_IEEE80211W */
1160 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1161 ret = os_snprintf(pos, end - pos, "SAE ");
1162 if (ret < 0 || ret >= end - pos)
1166 #endif /* CONFIG_SAE */
1167 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1168 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1169 if (ret < 0 || ret >= end - pos)
1174 ret = os_snprintf(pos, end - pos, "\n");
1175 if (ret < 0 || ret >= end - pos)
1180 if (hapd->conf->wpa) {
1181 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1182 wpa_cipher_txt(hapd->conf->wpa_group));
1183 if (ret < 0 || ret >= end - pos)
1188 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1189 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
1190 if (ret < 0 || ret >= end - pos)
1194 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1200 ret = os_snprintf(pos, end - pos, "\n");
1201 if (ret < 0 || ret >= end - pos)
1206 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1207 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
1208 if (ret < 0 || ret >= end - pos)
1212 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
1218 ret = os_snprintf(pos, end - pos, "\n");
1219 if (ret < 0 || ret >= end - pos)
1228 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1233 value = os_strchr(cmd, ' ');
1238 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1240 #ifdef CONFIG_WPS_TESTING
1241 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1243 val = strtol(value, NULL, 0);
1244 if (val < 0 || val > 0xff) {
1246 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1247 "wps_version_number %ld", val);
1249 wps_version_number = val;
1250 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1252 (wps_version_number & 0xf0) >> 4,
1253 wps_version_number & 0x0f);
1254 hostapd_wps_update_ie(hapd);
1256 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1257 wps_testing_dummy_cred = atoi(value);
1258 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1259 wps_testing_dummy_cred);
1260 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1261 wps_corrupt_pkhash = atoi(value);
1262 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1263 wps_corrupt_pkhash);
1264 #endif /* CONFIG_WPS_TESTING */
1265 #ifdef CONFIG_INTERWORKING
1266 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1267 int val = atoi(value);
1271 hapd->gas_frag_limit = val;
1272 #endif /* CONFIG_INTERWORKING */
1273 #ifdef CONFIG_TESTING_OPTIONS
1274 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1275 hapd->ext_mgmt_frame_handling = atoi(value);
1276 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1277 hapd->ext_eapol_frame_io = atoi(value);
1278 #endif /* CONFIG_TESTING_OPTIONS */
1280 struct sta_info *sta;
1283 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1287 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1288 for (sta = hapd->sta_list; sta; sta = sta->next) {
1289 if (hostapd_maclist_found(
1290 hapd->conf->deny_mac,
1291 hapd->conf->num_deny_mac, sta->addr,
1293 (!vlan_id || vlan_id == sta->vlan_id))
1295 hapd, sta, sta->addr,
1296 WLAN_REASON_UNSPECIFIED);
1298 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1299 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1300 for (sta = hapd->sta_list; sta; sta = sta->next) {
1301 if (!hostapd_maclist_found(
1302 hapd->conf->accept_mac,
1303 hapd->conf->num_accept_mac,
1304 sta->addr, &vlan_id) ||
1305 (vlan_id && vlan_id != sta->vlan_id))
1307 hapd, sta, sta->addr,
1308 WLAN_REASON_UNSPECIFIED);
1317 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1318 char *buf, size_t buflen)
1322 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1324 if (os_strcmp(cmd, "version") == 0) {
1325 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1326 if (res < 0 || (unsigned int) res >= buflen)
1335 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1337 if (hostapd_enable_iface(iface) < 0) {
1338 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1345 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1347 if (hostapd_reload_iface(iface) < 0) {
1348 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1355 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1357 if (hostapd_disable_iface(iface) < 0) {
1358 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1365 #ifdef CONFIG_TESTING_OPTIONS
1367 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1369 union wpa_event_data data;
1371 enum wpa_event_type event;
1373 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1375 os_memset(&data, 0, sizeof(data));
1377 param = os_strchr(cmd, ' ');
1382 if (os_strcmp(cmd, "DETECTED") == 0)
1383 event = EVENT_DFS_RADAR_DETECTED;
1384 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1385 event = EVENT_DFS_CAC_FINISHED;
1386 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1387 event = EVENT_DFS_CAC_ABORTED;
1388 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1389 event = EVENT_DFS_NOP_FINISHED;
1391 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1396 pos = os_strstr(param, "freq=");
1398 data.dfs_event.freq = atoi(pos + 5);
1400 pos = os_strstr(param, "ht_enabled=1");
1402 data.dfs_event.ht_enabled = 1;
1404 pos = os_strstr(param, "chan_offset=");
1406 data.dfs_event.chan_offset = atoi(pos + 12);
1408 pos = os_strstr(param, "chan_width=");
1410 data.dfs_event.chan_width = atoi(pos + 11);
1412 pos = os_strstr(param, "cf1=");
1414 data.dfs_event.cf1 = atoi(pos + 4);
1416 pos = os_strstr(param, "cf2=");
1418 data.dfs_event.cf2 = atoi(pos + 4);
1420 wpa_supplicant_event(hapd, event, &data);
1426 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1432 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1434 len = os_strlen(cmd);
1439 buf = os_malloc(len);
1443 if (hexstr2bin(cmd, buf, len) < 0) {
1448 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1454 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1457 u8 src[ETH_ALEN], *buf;
1461 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1464 used = hwaddr_aton2(pos, src);
1471 len = os_strlen(pos);
1476 buf = os_malloc(len);
1480 if (hexstr2bin(pos, buf, len) < 0) {
1485 ieee802_1x_receive(hapd, src, buf, len);
1492 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1496 const u16 *pos = buf;
1498 for (i = 0; i < len / 2; i++)
1502 sum = (sum & 0xffff) + (sum >> 16);
1504 return sum ^ 0xffff;
1508 #define HWSIM_PACKETLEN 1500
1509 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1511 void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1514 struct hostapd_data *hapd = ctx;
1515 const struct ether_header *eth;
1516 const struct iphdr *ip;
1520 if (len != HWSIM_PACKETLEN)
1523 eth = (const struct ether_header *) buf;
1524 ip = (const struct iphdr *) (eth + 1);
1525 pos = (const u8 *) (ip + 1);
1527 if (ip->ihl != 5 || ip->version != 4 ||
1528 ntohs(ip->tot_len) != HWSIM_IP_LEN)
1531 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++) {
1537 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
1538 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
1542 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1545 int enabled = atoi(cmd);
1550 if (hapd->l2_test) {
1551 l2_packet_deinit(hapd->l2_test);
1552 hapd->l2_test = NULL;
1553 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1554 "test data: Disabled");
1562 pos = os_strstr(cmd, " ifname=");
1566 ifname = hapd->conf->iface;
1568 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1569 ETHERTYPE_IP, hostapd_data_test_rx,
1571 if (hapd->l2_test == NULL)
1574 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1580 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1582 u8 dst[ETH_ALEN], src[ETH_ALEN];
1587 u8 buf[HWSIM_PACKETLEN];
1588 struct ether_header *eth;
1593 if (hapd->l2_test == NULL)
1596 /* format: <dst> <src> <tos> */
1599 used = hwaddr_aton2(pos, dst);
1605 used = hwaddr_aton2(pos, src);
1610 val = strtol(pos, NULL, 0);
1611 if (val < 0 || val > 0xff)
1615 eth = (struct ether_header *) buf;
1616 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1617 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1618 eth->ether_type = htons(ETHERTYPE_IP);
1619 ip = (struct iphdr *) (eth + 1);
1620 os_memset(ip, 0, sizeof(*ip));
1625 ip->tot_len = htons(HWSIM_IP_LEN);
1627 ip->saddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 1);
1628 ip->daddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 2);
1629 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
1630 dpos = (u8 *) (ip + 1);
1631 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
1634 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, buf,
1635 HWSIM_PACKETLEN) < 0)
1638 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1639 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1645 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1649 struct ether_header *eth;
1650 struct l2_packet_data *l2 = NULL;
1654 const char *ifname = hapd->conf->iface;
1656 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1659 cmd = os_strchr(cmd, ' ');
1665 len = os_strlen(cmd);
1666 if (len & 1 || len < ETH_HLEN * 2)
1670 buf = os_malloc(len);
1674 if (hexstr2bin(cmd, buf, len) < 0)
1677 eth = (struct ether_header *) buf;
1678 ethertype = ntohs(eth->ether_type);
1680 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1681 hostapd_data_test_rx, hapd, 1);
1685 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1686 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1689 l2_packet_deinit(l2);
1692 return res < 0 ? -1 : 0;
1695 #endif /* CONFIG_TESTING_OPTIONS */
1698 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
1702 struct csa_settings settings;
1706 ret = hostapd_parse_csa_settings(pos, &settings);
1710 for (i = 0; i < iface->num_bss; i++) {
1711 ret = hostapd_switch_channel(iface->bss[i], &settings);
1713 /* FIX: What do we do if CSA fails in the middle of
1714 * submitting multi-BSS CSA requests? */
1720 #else /* NEED_AP_MLME */
1722 #endif /* NEED_AP_MLME */
1726 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1727 int reply_size, const char *param)
1729 #ifdef RADIUS_SERVER
1730 if (os_strcmp(param, "radius_server") == 0) {
1731 return radius_server_get_mib(hapd->radius_srv, reply,
1734 #endif /* RADIUS_SERVER */
1739 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
1740 char *buf, size_t buflen)
1745 unsigned int vendor_id, subcmd;
1746 struct wpabuf *reply;
1747 size_t data_len = 0;
1749 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1750 vendor_id = strtoul(cmd, &pos, 16);
1754 subcmd = strtoul(pos, &pos, 10);
1757 if (!isblank(*pos++))
1759 data_len = os_strlen(pos);
1764 data = os_malloc(data_len);
1768 if (hexstr2bin(pos, data, data_len)) {
1769 wpa_printf(MSG_DEBUG,
1770 "Vendor command: wrong parameter format");
1776 reply = wpabuf_alloc((buflen - 1) / 2);
1782 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
1786 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
1796 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
1799 struct hostapd_data *hapd = eloop_ctx;
1802 struct sockaddr_un from;
1803 socklen_t fromlen = sizeof(from);
1805 const int reply_size = 4096;
1807 int level = MSG_DEBUG;
1809 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1810 (struct sockaddr *) &from, &fromlen);
1812 perror("recvfrom(ctrl_iface)");
1816 if (os_strcmp(buf, "PING") == 0)
1817 level = MSG_EXCESSIVE;
1818 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
1820 reply = os_malloc(reply_size);
1821 if (reply == NULL) {
1822 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
1824 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
1830 os_memcpy(reply, "OK\n", 3);
1833 if (os_strcmp(buf, "PING") == 0) {
1834 os_memcpy(reply, "PONG\n", 5);
1836 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
1837 if (wpa_debug_reopen_file() < 0)
1839 } else if (os_strcmp(buf, "STATUS") == 0) {
1840 reply_len = hostapd_ctrl_iface_status(hapd, reply,
1842 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
1843 reply_len = hostapd_drv_status(hapd, reply, reply_size);
1844 } else if (os_strcmp(buf, "MIB") == 0) {
1845 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
1846 if (reply_len >= 0) {
1847 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
1848 reply_size - reply_len);
1854 if (reply_len >= 0) {
1855 res = ieee802_1x_get_mib(hapd, reply + reply_len,
1856 reply_size - reply_len);
1862 #ifndef CONFIG_NO_RADIUS
1863 if (reply_len >= 0) {
1864 res = radius_client_get_mib(hapd->radius,
1866 reply_size - reply_len);
1872 #endif /* CONFIG_NO_RADIUS */
1873 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
1874 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
1876 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
1877 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
1879 } else if (os_strncmp(buf, "STA ", 4) == 0) {
1880 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
1882 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
1883 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
1885 } else if (os_strcmp(buf, "ATTACH") == 0) {
1886 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
1888 } else if (os_strcmp(buf, "DETACH") == 0) {
1889 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
1891 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
1892 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
1895 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
1896 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
1898 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
1899 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
1901 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
1902 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
1904 #ifdef CONFIG_IEEE80211W
1906 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
1907 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
1909 #endif /* NEED_AP_MLME */
1910 #endif /* CONFIG_IEEE80211W */
1912 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
1913 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
1915 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
1916 reply_len = hostapd_ctrl_iface_wps_check_pin(
1917 hapd, buf + 14, reply, reply_size);
1918 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
1919 if (hostapd_wps_button_pushed(hapd, NULL))
1921 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
1922 if (hostapd_wps_cancel(hapd))
1924 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
1925 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
1927 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
1928 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
1930 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
1931 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
1933 #ifdef CONFIG_WPS_NFC
1934 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
1935 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
1937 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
1938 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
1939 hapd, buf + 21, reply, reply_size);
1940 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
1941 reply_len = hostapd_ctrl_iface_wps_nfc_token(
1942 hapd, buf + 14, reply, reply_size);
1943 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
1944 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
1945 hapd, buf + 21, reply, reply_size);
1946 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
1947 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
1949 #endif /* CONFIG_WPS_NFC */
1950 #endif /* CONFIG_WPS */
1951 #ifdef CONFIG_INTERWORKING
1952 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
1953 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
1955 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
1956 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
1958 #endif /* CONFIG_INTERWORKING */
1960 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
1961 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
1963 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
1964 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
1966 #endif /* CONFIG_HS20 */
1968 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
1969 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
1971 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
1972 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
1974 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
1975 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
1977 #endif /* CONFIG_WNM */
1978 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
1979 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
1981 } else if (os_strncmp(buf, "SET ", 4) == 0) {
1982 if (hostapd_ctrl_iface_set(hapd, buf + 4))
1984 } else if (os_strncmp(buf, "GET ", 4) == 0) {
1985 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
1987 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
1988 if (hostapd_ctrl_iface_enable(hapd->iface))
1990 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
1991 if (hostapd_ctrl_iface_reload(hapd->iface))
1993 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
1994 if (hostapd_ctrl_iface_disable(hapd->iface))
1996 #ifdef CONFIG_TESTING_OPTIONS
1997 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
1998 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
2000 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
2001 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
2003 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
2004 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
2006 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
2007 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
2009 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
2010 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
2012 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
2013 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
2015 #endif /* CONFIG_TESTING_OPTIONS */
2016 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
2017 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
2019 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
2020 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
2024 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2028 if (reply_len < 0) {
2029 os_memcpy(reply, "FAIL\n", 5);
2032 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2034 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2041 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
2046 if (hapd->conf->ctrl_interface == NULL)
2049 len = os_strlen(hapd->conf->ctrl_interface) +
2050 os_strlen(hapd->conf->iface) + 2;
2051 buf = os_malloc(len);
2055 os_snprintf(buf, len, "%s/%s",
2056 hapd->conf->ctrl_interface, hapd->conf->iface);
2057 buf[len - 1] = '\0';
2062 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
2063 const char *txt, size_t len)
2065 struct hostapd_data *hapd = ctx;
2068 hostapd_ctrl_iface_send(hapd, level, txt, len);
2072 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
2074 struct sockaddr_un addr;
2078 if (hapd->ctrl_sock > -1) {
2079 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2083 if (hapd->conf->ctrl_interface == NULL)
2086 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2087 if (errno == EEXIST) {
2088 wpa_printf(MSG_DEBUG, "Using existing control "
2089 "interface directory.");
2091 perror("mkdir[ctrl_interface]");
2096 if (hapd->conf->ctrl_interface_gid_set &&
2097 chown(hapd->conf->ctrl_interface, -1,
2098 hapd->conf->ctrl_interface_gid) < 0) {
2099 perror("chown[ctrl_interface]");
2103 if (!hapd->conf->ctrl_interface_gid_set &&
2104 hapd->iface->interfaces->ctrl_iface_group &&
2105 chown(hapd->conf->ctrl_interface, -1,
2106 hapd->iface->interfaces->ctrl_iface_group) < 0) {
2107 perror("chown[ctrl_interface]");
2113 * Android is using umask 0077 which would leave the control interface
2114 * directory without group access. This breaks things since Wi-Fi
2115 * framework assumes that this directory can be accessed by other
2116 * applications in the wifi group. Fix this by adding group access even
2117 * if umask value would prevent this.
2119 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2120 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
2122 /* Try to continue anyway */
2124 #endif /* ANDROID */
2126 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
2127 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
2130 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2132 perror("socket(PF_UNIX)");
2136 os_memset(&addr, 0, sizeof(addr));
2138 addr.sun_len = sizeof(addr);
2139 #endif /* __FreeBSD__ */
2140 addr.sun_family = AF_UNIX;
2141 fname = hostapd_ctrl_iface_path(hapd);
2144 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2145 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2146 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2148 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2149 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2150 " allow connections - assuming it was left"
2151 "over from forced program termination");
2152 if (unlink(fname) < 0) {
2153 perror("unlink[ctrl_iface]");
2154 wpa_printf(MSG_ERROR, "Could not unlink "
2155 "existing ctrl_iface socket '%s'",
2159 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2161 perror("hostapd-ctrl-iface: bind(PF_UNIX)");
2164 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2165 "ctrl_iface socket '%s'", fname);
2167 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2168 "be in use - cannot override it");
2169 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2170 "not used anymore", fname);
2177 if (hapd->conf->ctrl_interface_gid_set &&
2178 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
2179 perror("chown[ctrl_interface/ifname]");
2183 if (!hapd->conf->ctrl_interface_gid_set &&
2184 hapd->iface->interfaces->ctrl_iface_group &&
2185 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
2186 perror("chown[ctrl_interface/ifname]");
2190 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2191 perror("chmod[ctrl_interface/ifname]");
2196 hapd->ctrl_sock = s;
2197 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
2199 hapd->msg_ctx = hapd;
2200 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
2215 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
2217 struct wpa_ctrl_dst *dst, *prev;
2219 if (hapd->ctrl_sock > -1) {
2221 eloop_unregister_read_sock(hapd->ctrl_sock);
2222 close(hapd->ctrl_sock);
2223 hapd->ctrl_sock = -1;
2224 fname = hostapd_ctrl_iface_path(hapd);
2229 if (hapd->conf->ctrl_interface &&
2230 rmdir(hapd->conf->ctrl_interface) < 0) {
2231 if (errno == ENOTEMPTY) {
2232 wpa_printf(MSG_DEBUG, "Control interface "
2233 "directory not empty - leaving it "
2236 wpa_printf(MSG_ERROR,
2237 "rmdir[ctrl_interface=%s]: %s",
2238 hapd->conf->ctrl_interface,
2244 dst = hapd->ctrl_dst;
2251 #ifdef CONFIG_TESTING_OPTIONS
2252 l2_packet_deinit(hapd->l2_test);
2253 hapd->l2_test = NULL;
2254 #endif /* CONFIG_TESTING_OPTIONS */
2258 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
2261 if (hostapd_add_iface(interfaces, buf) < 0) {
2262 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
2269 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
2272 if (hostapd_remove_iface(interfaces, buf) < 0) {
2273 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
2280 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
2282 #ifdef CONFIG_WPS_TESTING
2283 wps_version_number = 0x20;
2284 wps_testing_dummy_cred = 0;
2285 wps_corrupt_pkhash = 0;
2286 #endif /* CONFIG_WPS_TESTING */
2290 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
2293 void *interfaces = eloop_ctx;
2296 struct sockaddr_un from;
2297 socklen_t fromlen = sizeof(from);
2301 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2302 (struct sockaddr *) &from, &fromlen);
2304 perror("recvfrom(ctrl_iface)");
2308 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
2310 os_memcpy(reply, "OK\n", 3);
2313 if (os_strcmp(buf, "PING") == 0) {
2314 os_memcpy(reply, "PONG\n", 5);
2316 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2317 if (wpa_debug_reopen_file() < 0)
2319 } else if (os_strcmp(buf, "FLUSH") == 0) {
2320 hostapd_ctrl_iface_flush(interfaces);
2321 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
2322 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
2324 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
2325 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
2327 #ifdef CONFIG_MODULE_TESTS
2328 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
2329 int hapd_module_tests(void);
2330 if (hapd_module_tests() < 0)
2332 #endif /* CONFIG_MODULE_TESTS */
2334 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
2339 if (reply_len < 0) {
2340 os_memcpy(reply, "FAIL\n", 5);
2344 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2346 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2352 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
2357 if (interface->global_iface_path == NULL)
2360 len = os_strlen(interface->global_iface_path) +
2361 os_strlen(interface->global_iface_name) + 2;
2362 buf = os_malloc(len);
2366 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
2367 interface->global_iface_name);
2368 buf[len - 1] = '\0';
2373 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
2375 struct sockaddr_un addr;
2379 if (interface->global_iface_path == NULL) {
2380 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
2384 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
2385 if (errno == EEXIST) {
2386 wpa_printf(MSG_DEBUG, "Using existing control "
2387 "interface directory.");
2389 perror("mkdir[ctrl_interface]");
2392 } else if (interface->ctrl_iface_group &&
2393 chown(interface->global_iface_path, -1,
2394 interface->ctrl_iface_group) < 0) {
2395 perror("chown[ctrl_interface]");
2399 if (os_strlen(interface->global_iface_path) + 1 +
2400 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
2403 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2405 perror("socket(PF_UNIX)");
2409 os_memset(&addr, 0, sizeof(addr));
2411 addr.sun_len = sizeof(addr);
2412 #endif /* __FreeBSD__ */
2413 addr.sun_family = AF_UNIX;
2414 fname = hostapd_global_ctrl_iface_path(interface);
2417 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2418 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2419 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2421 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2422 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2423 " allow connections - assuming it was left"
2424 "over from forced program termination");
2425 if (unlink(fname) < 0) {
2426 perror("unlink[ctrl_iface]");
2427 wpa_printf(MSG_ERROR, "Could not unlink "
2428 "existing ctrl_iface socket '%s'",
2432 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2434 perror("bind(PF_UNIX)");
2437 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2438 "ctrl_iface socket '%s'", fname);
2440 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2441 "be in use - cannot override it");
2442 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2443 "not used anymore", fname);
2450 if (interface->ctrl_iface_group &&
2451 chown(fname, -1, interface->ctrl_iface_group) < 0) {
2452 perror("chown[ctrl_interface]");
2456 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2457 perror("chmod[ctrl_interface/ifname]");
2462 interface->global_ctrl_sock = s;
2463 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
2479 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
2483 if (interfaces->global_ctrl_sock > -1) {
2484 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
2485 close(interfaces->global_ctrl_sock);
2486 interfaces->global_ctrl_sock = -1;
2487 fname = hostapd_global_ctrl_iface_path(interfaces);
2493 if (interfaces->global_iface_path &&
2494 rmdir(interfaces->global_iface_path) < 0) {
2495 if (errno == ENOTEMPTY) {
2496 wpa_printf(MSG_DEBUG, "Control interface "
2497 "directory not empty - leaving it "
2500 wpa_printf(MSG_ERROR,
2501 "rmdir[ctrl_interface=%s]: %s",
2502 interfaces->global_iface_path,
2506 os_free(interfaces->global_iface_path);
2507 interfaces->global_iface_path = NULL;
2512 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
2513 const char *buf, size_t len)
2515 struct wpa_ctrl_dst *dst, *next;
2521 dst = hapd->ctrl_dst;
2522 if (hapd->ctrl_sock < 0 || dst == NULL)
2525 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
2526 io[0].iov_base = levelstr;
2527 io[0].iov_len = os_strlen(levelstr);
2528 io[1].iov_base = (char *) buf;
2529 io[1].iov_len = len;
2530 os_memset(&msg, 0, sizeof(msg));
2537 if (level >= dst->debug_level) {
2538 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
2539 (u8 *) dst->addr.sun_path, dst->addrlen -
2540 offsetof(struct sockaddr_un, sun_path));
2541 msg.msg_name = &dst->addr;
2542 msg.msg_namelen = dst->addrlen;
2543 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
2545 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
2547 idx, errno, strerror(errno));
2549 if (dst->errors > 10 || _errno == ENOENT) {
2550 hostapd_ctrl_iface_detach(
2562 #endif /* CONFIG_NATIVE_WINDOWS */