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 "crypto/tls.h"
27 #include "drivers/driver.h"
28 #include "radius/radius_client.h"
29 #include "radius/radius_server.h"
30 #include "l2_packet/l2_packet.h"
31 #include "ap/hostapd.h"
32 #include "ap/ap_config.h"
33 #include "ap/ieee802_1x.h"
34 #include "ap/wpa_auth.h"
35 #include "ap/ieee802_11.h"
36 #include "ap/sta_info.h"
37 #include "ap/wps_hostapd.h"
38 #include "ap/ctrl_iface_ap.h"
39 #include "ap/ap_drv_ops.h"
41 #include "ap/wnm_ap.h"
42 #include "ap/wpa_auth.h"
43 #include "wps/wps_defs.h"
45 #include "config_file.h"
46 #include "ctrl_iface.h"
50 struct wpa_ctrl_dst *next;
51 struct sockaddr_un addr;
58 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
59 const char *buf, size_t len);
62 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
63 struct sockaddr_un *from,
66 struct wpa_ctrl_dst *dst;
68 dst = os_zalloc(sizeof(*dst));
71 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
72 dst->addrlen = fromlen;
73 dst->debug_level = MSG_INFO;
74 dst->next = hapd->ctrl_dst;
76 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
77 (u8 *) from->sun_path,
78 fromlen - offsetof(struct sockaddr_un, sun_path));
83 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
84 struct sockaddr_un *from,
87 struct wpa_ctrl_dst *dst, *prev = NULL;
91 if (fromlen == dst->addrlen &&
92 os_memcmp(from->sun_path, dst->addr.sun_path,
93 fromlen - offsetof(struct sockaddr_un, sun_path))
95 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
96 (u8 *) from->sun_path,
98 offsetof(struct sockaddr_un, sun_path));
100 hapd->ctrl_dst = dst->next;
102 prev->next = dst->next;
113 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
114 struct sockaddr_un *from,
118 struct wpa_ctrl_dst *dst;
120 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
122 dst = hapd->ctrl_dst;
124 if (fromlen == dst->addrlen &&
125 os_memcmp(from->sun_path, dst->addr.sun_path,
126 fromlen - offsetof(struct sockaddr_un, sun_path))
128 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
129 "level", (u8 *) from->sun_path, fromlen -
130 offsetof(struct sockaddr_un, sun_path));
131 dst->debug_level = atoi(level);
141 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
145 struct sta_info *sta;
147 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
149 if (hwaddr_aton(txtaddr, addr))
152 sta = ap_get_sta(hapd, addr);
156 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
157 "notification", MAC2STR(addr));
158 sta = ap_sta_add(hapd, addr);
162 hostapd_new_assoc_sta(hapd, sta, 0);
167 #ifdef CONFIG_IEEE80211W
169 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
173 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
175 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
177 if (hwaddr_aton(txtaddr, addr) ||
178 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
181 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
185 #endif /* NEED_AP_MLME */
186 #endif /* CONFIG_IEEE80211W */
190 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
192 char *pin = os_strchr(txt, ' ');
195 u8 addr_buf[ETH_ALEN], *addr = NULL;
202 timeout_txt = os_strchr(pin, ' ');
204 *timeout_txt++ = '\0';
205 timeout = atoi(timeout_txt);
206 pos = os_strchr(timeout_txt, ' ');
209 if (hwaddr_aton(pos, addr_buf) == 0)
215 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
219 static int hostapd_ctrl_iface_wps_check_pin(
220 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
227 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
228 (u8 *) cmd, os_strlen(cmd));
229 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
230 if (*pos < '0' || *pos > '9')
234 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
238 if (len != 4 && len != 8) {
239 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
245 unsigned int pin_val;
247 if (!wps_pin_valid(pin_val)) {
248 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
249 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
250 if (os_snprintf_error(buflen, ret))
256 ret = os_snprintf(buf, buflen, "%s", pin);
257 if (os_snprintf_error(buflen, ret))
264 #ifdef CONFIG_WPS_NFC
265 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
272 len = os_strlen(pos);
277 buf = wpabuf_alloc(len);
280 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
285 ret = hostapd_wps_nfc_tag_read(hapd, buf);
292 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
293 char *cmd, char *reply,
300 if (os_strcmp(cmd, "WPS") == 0)
302 else if (os_strcmp(cmd, "NDEF") == 0)
307 buf = hostapd_wps_nfc_config_token(hapd, ndef);
311 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
322 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
323 char *reply, size_t max_len,
329 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
333 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
344 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
345 char *cmd, char *reply,
348 if (os_strcmp(cmd, "WPS") == 0)
349 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
352 if (os_strcmp(cmd, "NDEF") == 0)
353 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
356 if (os_strcmp(cmd, "enable") == 0)
357 return hostapd_wps_nfc_token_enable(hapd);
359 if (os_strcmp(cmd, "disable") == 0) {
360 hostapd_wps_nfc_token_disable(hapd);
368 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
369 char *cmd, char *reply,
377 pos = os_strchr(cmd, ' ');
382 if (os_strcmp(cmd, "WPS") == 0)
384 else if (os_strcmp(cmd, "NDEF") == 0)
389 if (os_strcmp(pos, "WPS-CR") == 0)
390 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
396 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
407 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
411 struct wpabuf *req, *sel;
413 char *pos, *role, *type, *pos2;
416 pos = os_strchr(role, ' ');
422 pos = os_strchr(type, ' ');
427 pos2 = os_strchr(pos, ' ');
432 len = os_strlen(pos);
437 req = wpabuf_alloc(len);
440 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
445 len = os_strlen(pos2);
452 sel = wpabuf_alloc(len);
457 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
463 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
464 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
466 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
467 "reported: role=%s type=%s", role, type);
476 #endif /* CONFIG_WPS_NFC */
479 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
480 char *buf, size_t buflen)
486 pos = os_strchr(txt, ' ');
490 if (os_strcmp(txt, "disable") == 0) {
491 hostapd_wps_ap_pin_disable(hapd);
492 return os_snprintf(buf, buflen, "OK\n");
495 if (os_strcmp(txt, "random") == 0) {
498 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
501 return os_snprintf(buf, buflen, "%s", pin_txt);
504 if (os_strcmp(txt, "get") == 0) {
505 pin_txt = hostapd_wps_ap_pin_get(hapd);
508 return os_snprintf(buf, buflen, "%s", pin_txt);
511 if (os_strcmp(txt, "set") == 0) {
516 pos = os_strchr(pos, ' ');
521 if (os_strlen(pin) > buflen)
523 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
525 return os_snprintf(buf, buflen, "%s", pin);
532 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
535 char *ssid, *auth, *encr = NULL, *key = NULL;
538 pos = os_strchr(txt, ' ');
544 pos = os_strchr(pos, ' ');
548 pos = os_strchr(pos, ' ');
555 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
559 static const char * pbc_status_str(enum pbc_status status)
562 case WPS_PBC_STATUS_DISABLE:
564 case WPS_PBC_STATUS_ACTIVE:
566 case WPS_PBC_STATUS_TIMEOUT:
568 case WPS_PBC_STATUS_OVERLAP:
576 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
577 char *buf, size_t buflen)
585 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
586 pbc_status_str(hapd->wps_stats.pbc_status));
588 if (os_snprintf_error(end - pos, ret))
592 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
593 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
595 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
596 "Failed" : "None")));
598 if (os_snprintf_error(end - pos, ret))
602 /* If status == Failure - Add possible Reasons */
603 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
604 hapd->wps_stats.failure_reason > 0) {
605 ret = os_snprintf(pos, end - pos,
606 "Failure Reason: %s\n",
607 wps_ei_str(hapd->wps_stats.failure_reason));
609 if (os_snprintf_error(end - pos, ret))
614 if (hapd->wps_stats.status) {
615 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
616 MAC2STR(hapd->wps_stats.peer_addr));
618 if (os_snprintf_error(end - pos, ret))
626 #endif /* CONFIG_WPS */
630 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
636 if (hwaddr_aton(cmd, addr))
649 return hs20_send_wnm_notification(hapd, addr, 1, url);
653 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
657 int code, reauth_delay, ret;
662 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
663 if (hwaddr_aton(cmd, addr))
666 pos = os_strchr(cmd, ' ');
672 pos = os_strchr(pos, ' ');
676 reauth_delay = atoi(pos);
679 pos = os_strchr(pos, ' ');
682 url_len = os_strlen(pos);
685 req = wpabuf_alloc(4 + url_len);
688 wpabuf_put_u8(req, code);
689 wpabuf_put_le16(req, reauth_delay);
690 wpabuf_put_u8(req, url_len);
692 wpabuf_put_data(req, pos, url_len);
694 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
695 " to indicate imminent deauthentication (code=%d "
696 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
697 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
702 #endif /* CONFIG_HS20 */
705 #ifdef CONFIG_INTERWORKING
707 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
710 u8 qos_map_set[16 + 2 * 21], count = 0;
711 const char *pos = cmd;
715 if (count == sizeof(qos_map_set)) {
716 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
721 if (val < 0 || val > 255) {
722 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
726 qos_map_set[count++] = val;
727 pos = os_strchr(pos, ',');
733 if (count < 16 || count & 1) {
734 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
738 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
740 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
744 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
745 hapd->conf->qos_map_set_len = count;
751 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
755 struct sta_info *sta;
757 u8 *qos_map_set = hapd->conf->qos_map_set;
758 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
761 if (!qos_map_set_len) {
762 wpa_printf(MSG_INFO, "QoS Map Set is not set");
766 if (hwaddr_aton(cmd, addr))
769 sta = ap_get_sta(hapd, addr);
771 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
772 "for QoS Map Configuration message",
777 if (!sta->qos_map_enabled) {
778 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
779 "support for QoS Map", MAC2STR(addr));
783 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
787 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
788 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
790 /* QoS Map Set Element */
791 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
792 wpabuf_put_u8(buf, qos_map_set_len);
793 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
795 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
796 wpabuf_head(buf), wpabuf_len(buf));
802 #endif /* CONFIG_INTERWORKING */
807 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
812 struct sta_info *sta;
814 if (hwaddr_aton(cmd, addr))
818 disassoc_timer = atoi(cmd + 17);
820 sta = ap_get_sta(hapd, addr);
822 wpa_printf(MSG_DEBUG, "Station " MACSTR
823 " not found for disassociation imminent message",
828 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
832 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
836 const char *url, *timerstr;
838 struct sta_info *sta;
840 if (hwaddr_aton(cmd, addr))
843 sta = ap_get_sta(hapd, addr);
845 wpa_printf(MSG_DEBUG, "Station " MACSTR
846 " not found for ESS disassociation imminent message",
852 if (*timerstr != ' ')
855 disassoc_timer = atoi(timerstr);
856 if (disassoc_timer < 0 || disassoc_timer > 65535)
859 url = os_strchr(timerstr, ' ');
864 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
868 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
872 const char *pos, *end;
873 int disassoc_timer = 0;
874 struct sta_info *sta;
875 u8 req_mode = 0, valid_int = 0x01;
880 u8 *nei_pos = nei_rep;
882 if (hwaddr_aton(cmd, addr)) {
883 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
887 sta = ap_get_sta(hapd, addr);
889 wpa_printf(MSG_DEBUG, "Station " MACSTR
890 " not found for BSS TM Request message",
895 pos = os_strstr(cmd, " disassoc_timer=");
898 disassoc_timer = atoi(pos);
899 if (disassoc_timer < 0 || disassoc_timer > 65535) {
900 wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
905 pos = os_strstr(cmd, " valid_int=");
908 valid_int = atoi(pos);
911 pos = os_strstr(cmd, " bss_term=");
914 req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
915 /* TODO: TSF configurable/learnable */
916 bss_term_dur[0] = 4; /* Subelement ID */
917 bss_term_dur[1] = 10; /* Length */
918 os_memset(bss_term_dur, 2, 8);
919 end = os_strchr(pos, ',');
921 wpa_printf(MSG_DEBUG, "Invalid bss_term data");
925 WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
930 * BSS Transition Candidate List Entries - Neighbor Report elements
931 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
932 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
940 pos = os_strstr(pos, " neighbor=");
943 if (nei_pos + 15 > nei_rep + sizeof(nei_rep)) {
944 wpa_printf(MSG_DEBUG,
945 "Not enough room for additional neighbor");
951 *nei_pos++ = WLAN_EID_NEIGHBOR_REPORT;
952 nei_pos++; /* length to be filled in */
954 if (hwaddr_aton(pos, nei_pos)) {
955 wpa_printf(MSG_DEBUG, "Invalid BSSID");
961 wpa_printf(MSG_DEBUG, "Missing BSSID Information");
966 val = strtol(pos, &endptr, 0);
967 WPA_PUT_LE32(nei_pos, val);
969 if (*endptr != ',') {
970 wpa_printf(MSG_DEBUG, "Missing Operating Class");
975 *nei_pos++ = atoi(pos); /* Operating Class */
976 pos = os_strchr(pos, ',');
978 wpa_printf(MSG_DEBUG, "Missing Channel Number");
983 *nei_pos++ = atoi(pos); /* Channel Number */
984 pos = os_strchr(pos, ',');
986 wpa_printf(MSG_DEBUG, "Missing PHY Type");
991 *nei_pos++ = atoi(pos); /* PHY Type */
992 end = os_strchr(pos, ' ');
993 tmp = os_strchr(pos, ',');
994 if (tmp && (!end || tmp < end)) {
995 /* Optional Subelements (hexdump) */
999 end = os_strchr(pos, ' ');
1003 len = os_strlen(pos);
1004 if (nei_pos + len / 2 > nei_rep + sizeof(nei_rep)) {
1005 wpa_printf(MSG_DEBUG,
1006 "Not enough room for neighbor subelements");
1010 hexstr2bin(pos, nei_pos, len / 2) < 0) {
1011 wpa_printf(MSG_DEBUG,
1012 "Invalid neighbor subelement info");
1019 nei_start[1] = nei_pos - nei_start - 2;
1022 pos = os_strstr(cmd, " url=");
1026 end = os_strchr(pos, ' ');
1030 len = os_strlen(pos);
1031 url = os_malloc(len + 1);
1034 os_memcpy(url, pos, len);
1036 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
1039 if (os_strstr(cmd, " pref=1"))
1040 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1041 if (os_strstr(cmd, " abridged=1"))
1042 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1043 if (os_strstr(cmd, " disassoc_imminent=1"))
1044 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1046 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
1047 valid_int, bss_term_dur, url,
1048 nei_pos > nei_rep ? nei_rep : NULL,
1054 #endif /* CONFIG_WNM */
1057 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1058 char *buf, size_t buflen)
1066 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1068 MAC2STR(hapd->own_addr),
1069 wpa_ssid_txt(hapd->conf->ssid.ssid,
1070 hapd->conf->ssid.ssid_len));
1071 if (os_snprintf_error(end - pos, ret))
1076 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1077 hapd->conf->wps_state == 0 ? "disabled" :
1078 (hapd->conf->wps_state == 1 ? "not configured" :
1080 if (os_snprintf_error(end - pos, ret))
1084 if (hapd->conf->wps_state && hapd->conf->wpa &&
1085 hapd->conf->ssid.wpa_passphrase) {
1086 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1087 hapd->conf->ssid.wpa_passphrase);
1088 if (os_snprintf_error(end - pos, ret))
1093 if (hapd->conf->wps_state && hapd->conf->wpa &&
1094 hapd->conf->ssid.wpa_psk &&
1095 hapd->conf->ssid.wpa_psk->group) {
1096 char hex[PMK_LEN * 2 + 1];
1097 wpa_snprintf_hex(hex, sizeof(hex),
1098 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1099 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
1100 if (os_snprintf_error(end - pos, ret))
1104 #endif /* CONFIG_WPS */
1106 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1107 ret = os_snprintf(pos, end - pos, "key_mgmt=");
1108 if (os_snprintf_error(end - pos, ret))
1112 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1113 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1114 if (os_snprintf_error(end - pos, ret))
1118 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1119 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1120 if (os_snprintf_error(end - pos, ret))
1124 #ifdef CONFIG_IEEE80211R
1125 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1126 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1127 if (os_snprintf_error(end - pos, ret))
1131 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1132 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1133 if (os_snprintf_error(end - pos, ret))
1138 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1139 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1140 if (os_snprintf_error(end - pos, ret))
1144 #endif /* CONFIG_SAE */
1145 #endif /* CONFIG_IEEE80211R */
1146 #ifdef CONFIG_IEEE80211W
1147 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1148 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1149 if (os_snprintf_error(end - pos, ret))
1153 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1154 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1155 if (os_snprintf_error(end - pos, ret))
1159 #endif /* CONFIG_IEEE80211W */
1161 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1162 ret = os_snprintf(pos, end - pos, "SAE ");
1163 if (os_snprintf_error(end - pos, ret))
1167 #endif /* CONFIG_SAE */
1168 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1169 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1170 if (os_snprintf_error(end - pos, ret))
1175 ret = os_snprintf(pos, end - pos, "\n");
1176 if (os_snprintf_error(end - pos, ret))
1181 if (hapd->conf->wpa) {
1182 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1183 wpa_cipher_txt(hapd->conf->wpa_group));
1184 if (os_snprintf_error(end - pos, ret))
1189 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1190 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
1191 if (os_snprintf_error(end - pos, ret))
1195 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1201 ret = os_snprintf(pos, end - pos, "\n");
1202 if (os_snprintf_error(end - pos, ret))
1207 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1208 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
1209 if (os_snprintf_error(end - pos, ret))
1213 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
1219 ret = os_snprintf(pos, end - pos, "\n");
1220 if (os_snprintf_error(end - pos, ret))
1229 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1234 value = os_strchr(cmd, ' ');
1239 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1241 #ifdef CONFIG_WPS_TESTING
1242 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1244 val = strtol(value, NULL, 0);
1245 if (val < 0 || val > 0xff) {
1247 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1248 "wps_version_number %ld", val);
1250 wps_version_number = val;
1251 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1253 (wps_version_number & 0xf0) >> 4,
1254 wps_version_number & 0x0f);
1255 hostapd_wps_update_ie(hapd);
1257 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1258 wps_testing_dummy_cred = atoi(value);
1259 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1260 wps_testing_dummy_cred);
1261 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1262 wps_corrupt_pkhash = atoi(value);
1263 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1264 wps_corrupt_pkhash);
1265 #endif /* CONFIG_WPS_TESTING */
1266 #ifdef CONFIG_INTERWORKING
1267 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1268 int val = atoi(value);
1272 hapd->gas_frag_limit = val;
1273 #endif /* CONFIG_INTERWORKING */
1274 #ifdef CONFIG_TESTING_OPTIONS
1275 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1276 hapd->ext_mgmt_frame_handling = atoi(value);
1277 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1278 hapd->ext_eapol_frame_io = atoi(value);
1279 #endif /* CONFIG_TESTING_OPTIONS */
1281 struct sta_info *sta;
1284 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1288 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1289 for (sta = hapd->sta_list; sta; sta = sta->next) {
1290 if (hostapd_maclist_found(
1291 hapd->conf->deny_mac,
1292 hapd->conf->num_deny_mac, sta->addr,
1294 (!vlan_id || vlan_id == sta->vlan_id))
1296 hapd, sta, sta->addr,
1297 WLAN_REASON_UNSPECIFIED);
1299 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1300 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1301 for (sta = hapd->sta_list; sta; sta = sta->next) {
1302 if (!hostapd_maclist_found(
1303 hapd->conf->accept_mac,
1304 hapd->conf->num_accept_mac,
1305 sta->addr, &vlan_id) ||
1306 (vlan_id && vlan_id != sta->vlan_id))
1308 hapd, sta, sta->addr,
1309 WLAN_REASON_UNSPECIFIED);
1318 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1319 char *buf, size_t buflen)
1323 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1325 if (os_strcmp(cmd, "version") == 0) {
1326 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1327 if (os_snprintf_error(buflen, res))
1330 } else if (os_strcmp(cmd, "tls_library") == 0) {
1331 res = tls_get_library_version(buf, buflen);
1332 if (os_snprintf_error(buflen, res))
1341 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1343 if (hostapd_enable_iface(iface) < 0) {
1344 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1351 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1353 if (hostapd_reload_iface(iface) < 0) {
1354 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1361 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1363 if (hostapd_disable_iface(iface) < 0) {
1364 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1371 #ifdef CONFIG_TESTING_OPTIONS
1373 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1375 union wpa_event_data data;
1377 enum wpa_event_type event;
1379 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1381 os_memset(&data, 0, sizeof(data));
1383 param = os_strchr(cmd, ' ');
1388 if (os_strcmp(cmd, "DETECTED") == 0)
1389 event = EVENT_DFS_RADAR_DETECTED;
1390 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1391 event = EVENT_DFS_CAC_FINISHED;
1392 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1393 event = EVENT_DFS_CAC_ABORTED;
1394 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1395 event = EVENT_DFS_NOP_FINISHED;
1397 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1402 pos = os_strstr(param, "freq=");
1404 data.dfs_event.freq = atoi(pos + 5);
1406 pos = os_strstr(param, "ht_enabled=1");
1408 data.dfs_event.ht_enabled = 1;
1410 pos = os_strstr(param, "chan_offset=");
1412 data.dfs_event.chan_offset = atoi(pos + 12);
1414 pos = os_strstr(param, "chan_width=");
1416 data.dfs_event.chan_width = atoi(pos + 11);
1418 pos = os_strstr(param, "cf1=");
1420 data.dfs_event.cf1 = atoi(pos + 4);
1422 pos = os_strstr(param, "cf2=");
1424 data.dfs_event.cf2 = atoi(pos + 4);
1426 wpa_supplicant_event(hapd, event, &data);
1432 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1438 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1440 len = os_strlen(cmd);
1445 buf = os_malloc(len);
1449 if (hexstr2bin(cmd, buf, len) < 0) {
1454 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1460 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1463 u8 src[ETH_ALEN], *buf;
1467 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1470 used = hwaddr_aton2(pos, src);
1477 len = os_strlen(pos);
1482 buf = os_malloc(len);
1486 if (hexstr2bin(pos, buf, len) < 0) {
1491 ieee802_1x_receive(hapd, src, buf, len);
1498 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1502 const u16 *pos = buf;
1504 for (i = 0; i < len / 2; i++)
1508 sum = (sum & 0xffff) + (sum >> 16);
1510 return sum ^ 0xffff;
1514 #define HWSIM_PACKETLEN 1500
1515 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1517 void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1520 struct hostapd_data *hapd = ctx;
1521 const struct ether_header *eth;
1522 const struct iphdr *ip;
1526 if (len != HWSIM_PACKETLEN)
1529 eth = (const struct ether_header *) buf;
1530 ip = (const struct iphdr *) (eth + 1);
1531 pos = (const u8 *) (ip + 1);
1533 if (ip->ihl != 5 || ip->version != 4 ||
1534 ntohs(ip->tot_len) != HWSIM_IP_LEN)
1537 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++) {
1543 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
1544 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
1548 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1551 int enabled = atoi(cmd);
1556 if (hapd->l2_test) {
1557 l2_packet_deinit(hapd->l2_test);
1558 hapd->l2_test = NULL;
1559 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1560 "test data: Disabled");
1568 pos = os_strstr(cmd, " ifname=");
1572 ifname = hapd->conf->iface;
1574 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1575 ETHERTYPE_IP, hostapd_data_test_rx,
1577 if (hapd->l2_test == NULL)
1580 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1586 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1588 u8 dst[ETH_ALEN], src[ETH_ALEN];
1593 u8 buf[HWSIM_PACKETLEN];
1594 struct ether_header *eth;
1599 if (hapd->l2_test == NULL)
1602 /* format: <dst> <src> <tos> */
1605 used = hwaddr_aton2(pos, dst);
1611 used = hwaddr_aton2(pos, src);
1616 val = strtol(pos, NULL, 0);
1617 if (val < 0 || val > 0xff)
1621 eth = (struct ether_header *) buf;
1622 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1623 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1624 eth->ether_type = htons(ETHERTYPE_IP);
1625 ip = (struct iphdr *) (eth + 1);
1626 os_memset(ip, 0, sizeof(*ip));
1631 ip->tot_len = htons(HWSIM_IP_LEN);
1633 ip->saddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 1);
1634 ip->daddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 2);
1635 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
1636 dpos = (u8 *) (ip + 1);
1637 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
1640 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, buf,
1641 HWSIM_PACKETLEN) < 0)
1644 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1645 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1651 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1655 struct ether_header *eth;
1656 struct l2_packet_data *l2 = NULL;
1660 const char *ifname = hapd->conf->iface;
1662 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1665 cmd = os_strchr(cmd, ' ');
1671 len = os_strlen(cmd);
1672 if (len & 1 || len < ETH_HLEN * 2)
1676 buf = os_malloc(len);
1680 if (hexstr2bin(cmd, buf, len) < 0)
1683 eth = (struct ether_header *) buf;
1684 ethertype = ntohs(eth->ether_type);
1686 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1687 hostapd_data_test_rx, hapd, 1);
1691 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1692 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1695 l2_packet_deinit(l2);
1698 return res < 0 ? -1 : 0;
1702 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
1704 #ifdef WPA_TRACE_BFD
1705 extern char wpa_trace_fail_func[256];
1706 extern unsigned int wpa_trace_fail_after;
1709 wpa_trace_fail_after = atoi(cmd);
1710 pos = os_strchr(cmd, ':');
1713 os_strlcpy(wpa_trace_fail_func, pos,
1714 sizeof(wpa_trace_fail_func));
1716 wpa_trace_fail_after = 0;
1720 #else /* WPA_TRACE_BFD */
1722 #endif /* WPA_TRACE_BFD */
1726 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
1727 char *buf, size_t buflen)
1729 #ifdef WPA_TRACE_BFD
1730 extern char wpa_trace_fail_func[256];
1731 extern unsigned int wpa_trace_fail_after;
1733 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
1734 wpa_trace_fail_func);
1735 #else /* WPA_TRACE_BFD */
1737 #endif /* WPA_TRACE_BFD */
1740 #endif /* CONFIG_TESTING_OPTIONS */
1743 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
1747 struct csa_settings settings;
1751 ret = hostapd_parse_csa_settings(pos, &settings);
1755 for (i = 0; i < iface->num_bss; i++) {
1756 ret = hostapd_switch_channel(iface->bss[i], &settings);
1758 /* FIX: What do we do if CSA fails in the middle of
1759 * submitting multi-BSS CSA requests? */
1765 #else /* NEED_AP_MLME */
1767 #endif /* NEED_AP_MLME */
1771 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1772 int reply_size, const char *param)
1774 #ifdef RADIUS_SERVER
1775 if (os_strcmp(param, "radius_server") == 0) {
1776 return radius_server_get_mib(hapd->radius_srv, reply,
1779 #endif /* RADIUS_SERVER */
1784 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
1785 char *buf, size_t buflen)
1790 unsigned int vendor_id, subcmd;
1791 struct wpabuf *reply;
1792 size_t data_len = 0;
1794 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1795 vendor_id = strtoul(cmd, &pos, 16);
1799 subcmd = strtoul(pos, &pos, 10);
1802 if (!isblank(*pos++))
1804 data_len = os_strlen(pos);
1809 data = os_malloc(data_len);
1813 if (hexstr2bin(pos, data, data_len)) {
1814 wpa_printf(MSG_DEBUG,
1815 "Vendor command: wrong parameter format");
1821 reply = wpabuf_alloc((buflen - 1) / 2);
1827 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
1831 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
1841 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
1844 struct hostapd_data *hapd = eloop_ctx;
1847 struct sockaddr_un from;
1848 socklen_t fromlen = sizeof(from);
1850 const int reply_size = 4096;
1852 int level = MSG_DEBUG;
1854 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1855 (struct sockaddr *) &from, &fromlen);
1857 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
1862 if (os_strcmp(buf, "PING") == 0)
1863 level = MSG_EXCESSIVE;
1864 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
1866 reply = os_malloc(reply_size);
1867 if (reply == NULL) {
1868 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
1870 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
1876 os_memcpy(reply, "OK\n", 3);
1879 if (os_strcmp(buf, "PING") == 0) {
1880 os_memcpy(reply, "PONG\n", 5);
1882 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
1883 if (wpa_debug_reopen_file() < 0)
1885 } else if (os_strcmp(buf, "STATUS") == 0) {
1886 reply_len = hostapd_ctrl_iface_status(hapd, reply,
1888 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
1889 reply_len = hostapd_drv_status(hapd, reply, reply_size);
1890 } else if (os_strcmp(buf, "MIB") == 0) {
1891 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
1892 if (reply_len >= 0) {
1893 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
1894 reply_size - reply_len);
1900 if (reply_len >= 0) {
1901 res = ieee802_1x_get_mib(hapd, reply + reply_len,
1902 reply_size - reply_len);
1908 #ifndef CONFIG_NO_RADIUS
1909 if (reply_len >= 0) {
1910 res = radius_client_get_mib(hapd->radius,
1912 reply_size - reply_len);
1918 #endif /* CONFIG_NO_RADIUS */
1919 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
1920 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
1922 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
1923 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
1925 } else if (os_strncmp(buf, "STA ", 4) == 0) {
1926 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
1928 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
1929 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
1931 } else if (os_strcmp(buf, "ATTACH") == 0) {
1932 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
1934 } else if (os_strcmp(buf, "DETACH") == 0) {
1935 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
1937 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
1938 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
1941 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
1942 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
1944 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
1945 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
1947 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
1948 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
1950 #ifdef CONFIG_IEEE80211W
1952 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
1953 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
1955 #endif /* NEED_AP_MLME */
1956 #endif /* CONFIG_IEEE80211W */
1958 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
1959 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
1961 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
1962 reply_len = hostapd_ctrl_iface_wps_check_pin(
1963 hapd, buf + 14, reply, reply_size);
1964 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
1965 if (hostapd_wps_button_pushed(hapd, NULL))
1967 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
1968 if (hostapd_wps_cancel(hapd))
1970 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
1971 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
1973 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
1974 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
1976 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
1977 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
1979 #ifdef CONFIG_WPS_NFC
1980 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
1981 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
1983 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
1984 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
1985 hapd, buf + 21, reply, reply_size);
1986 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
1987 reply_len = hostapd_ctrl_iface_wps_nfc_token(
1988 hapd, buf + 14, reply, reply_size);
1989 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
1990 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
1991 hapd, buf + 21, reply, reply_size);
1992 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
1993 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
1995 #endif /* CONFIG_WPS_NFC */
1996 #endif /* CONFIG_WPS */
1997 #ifdef CONFIG_INTERWORKING
1998 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
1999 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
2001 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
2002 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
2004 #endif /* CONFIG_INTERWORKING */
2006 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
2007 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
2009 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
2010 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
2012 #endif /* CONFIG_HS20 */
2014 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
2015 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
2017 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
2018 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
2020 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
2021 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
2023 #endif /* CONFIG_WNM */
2024 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
2025 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
2027 } else if (os_strncmp(buf, "SET ", 4) == 0) {
2028 if (hostapd_ctrl_iface_set(hapd, buf + 4))
2030 } else if (os_strncmp(buf, "GET ", 4) == 0) {
2031 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
2033 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
2034 if (hostapd_ctrl_iface_enable(hapd->iface))
2036 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
2037 if (hostapd_ctrl_iface_reload(hapd->iface))
2039 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
2040 if (hostapd_ctrl_iface_disable(hapd->iface))
2042 #ifdef CONFIG_TESTING_OPTIONS
2043 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
2044 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
2046 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
2047 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
2049 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
2050 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
2052 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
2053 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
2055 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
2056 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
2058 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
2059 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
2061 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
2062 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
2064 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
2065 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
2067 #endif /* CONFIG_TESTING_OPTIONS */
2068 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
2069 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
2071 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
2072 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
2074 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
2075 ieee802_1x_erp_flush(hapd);
2076 #ifdef RADIUS_SERVER
2077 radius_server_erp_flush(hapd->radius_srv);
2078 #endif /* RADIUS_SERVER */
2080 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2084 if (reply_len < 0) {
2085 os_memcpy(reply, "FAIL\n", 5);
2088 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2090 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2097 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
2102 if (hapd->conf->ctrl_interface == NULL)
2105 len = os_strlen(hapd->conf->ctrl_interface) +
2106 os_strlen(hapd->conf->iface) + 2;
2107 buf = os_malloc(len);
2111 os_snprintf(buf, len, "%s/%s",
2112 hapd->conf->ctrl_interface, hapd->conf->iface);
2113 buf[len - 1] = '\0';
2118 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
2119 const char *txt, size_t len)
2121 struct hostapd_data *hapd = ctx;
2124 hostapd_ctrl_iface_send(hapd, level, txt, len);
2128 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
2130 struct sockaddr_un addr;
2134 if (hapd->ctrl_sock > -1) {
2135 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2139 if (hapd->conf->ctrl_interface == NULL)
2142 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2143 if (errno == EEXIST) {
2144 wpa_printf(MSG_DEBUG, "Using existing control "
2145 "interface directory.");
2147 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2153 if (hapd->conf->ctrl_interface_gid_set &&
2154 chown(hapd->conf->ctrl_interface, -1,
2155 hapd->conf->ctrl_interface_gid) < 0) {
2156 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2161 if (!hapd->conf->ctrl_interface_gid_set &&
2162 hapd->iface->interfaces->ctrl_iface_group &&
2163 chown(hapd->conf->ctrl_interface, -1,
2164 hapd->iface->interfaces->ctrl_iface_group) < 0) {
2165 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2172 * Android is using umask 0077 which would leave the control interface
2173 * directory without group access. This breaks things since Wi-Fi
2174 * framework assumes that this directory can be accessed by other
2175 * applications in the wifi group. Fix this by adding group access even
2176 * if umask value would prevent this.
2178 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2179 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
2181 /* Try to continue anyway */
2183 #endif /* ANDROID */
2185 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
2186 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
2189 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2191 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
2195 os_memset(&addr, 0, sizeof(addr));
2197 addr.sun_len = sizeof(addr);
2198 #endif /* __FreeBSD__ */
2199 addr.sun_family = AF_UNIX;
2200 fname = hostapd_ctrl_iface_path(hapd);
2203 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2204 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2205 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2207 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2208 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2209 " allow connections - assuming it was left"
2210 "over from forced program termination");
2211 if (unlink(fname) < 0) {
2212 wpa_printf(MSG_ERROR,
2213 "Could not unlink existing ctrl_iface socket '%s': %s",
2214 fname, strerror(errno));
2217 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2219 wpa_printf(MSG_ERROR,
2220 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
2224 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2225 "ctrl_iface socket '%s'", fname);
2227 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2228 "be in use - cannot override it");
2229 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2230 "not used anymore", fname);
2237 if (hapd->conf->ctrl_interface_gid_set &&
2238 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
2239 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2244 if (!hapd->conf->ctrl_interface_gid_set &&
2245 hapd->iface->interfaces->ctrl_iface_group &&
2246 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
2247 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2252 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2253 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2259 hapd->ctrl_sock = s;
2260 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
2262 hostapd_ctrl_iface_deinit(hapd);
2265 hapd->msg_ctx = hapd;
2266 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
2281 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
2283 struct wpa_ctrl_dst *dst, *prev;
2285 if (hapd->ctrl_sock > -1) {
2287 eloop_unregister_read_sock(hapd->ctrl_sock);
2288 close(hapd->ctrl_sock);
2289 hapd->ctrl_sock = -1;
2290 fname = hostapd_ctrl_iface_path(hapd);
2295 if (hapd->conf->ctrl_interface &&
2296 rmdir(hapd->conf->ctrl_interface) < 0) {
2297 if (errno == ENOTEMPTY) {
2298 wpa_printf(MSG_DEBUG, "Control interface "
2299 "directory not empty - leaving it "
2302 wpa_printf(MSG_ERROR,
2303 "rmdir[ctrl_interface=%s]: %s",
2304 hapd->conf->ctrl_interface,
2310 dst = hapd->ctrl_dst;
2311 hapd->ctrl_dst = NULL;
2318 #ifdef CONFIG_TESTING_OPTIONS
2319 l2_packet_deinit(hapd->l2_test);
2320 hapd->l2_test = NULL;
2321 #endif /* CONFIG_TESTING_OPTIONS */
2325 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
2328 if (hostapd_add_iface(interfaces, buf) < 0) {
2329 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
2336 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
2339 if (hostapd_remove_iface(interfaces, buf) < 0) {
2340 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
2347 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
2349 #ifdef CONFIG_WPS_TESTING
2350 wps_version_number = 0x20;
2351 wps_testing_dummy_cred = 0;
2352 wps_corrupt_pkhash = 0;
2353 #endif /* CONFIG_WPS_TESTING */
2357 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
2360 void *interfaces = eloop_ctx;
2363 struct sockaddr_un from;
2364 socklen_t fromlen = sizeof(from);
2368 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2369 (struct sockaddr *) &from, &fromlen);
2371 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2376 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
2378 os_memcpy(reply, "OK\n", 3);
2381 if (os_strcmp(buf, "PING") == 0) {
2382 os_memcpy(reply, "PONG\n", 5);
2384 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2385 if (wpa_debug_reopen_file() < 0)
2387 } else if (os_strcmp(buf, "FLUSH") == 0) {
2388 hostapd_ctrl_iface_flush(interfaces);
2389 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
2390 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
2392 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
2393 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
2395 #ifdef CONFIG_MODULE_TESTS
2396 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
2397 int hapd_module_tests(void);
2398 if (hapd_module_tests() < 0)
2400 #endif /* CONFIG_MODULE_TESTS */
2402 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
2407 if (reply_len < 0) {
2408 os_memcpy(reply, "FAIL\n", 5);
2412 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2414 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2420 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
2425 if (interface->global_iface_path == NULL)
2428 len = os_strlen(interface->global_iface_path) +
2429 os_strlen(interface->global_iface_name) + 2;
2430 buf = os_malloc(len);
2434 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
2435 interface->global_iface_name);
2436 buf[len - 1] = '\0';
2441 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
2443 struct sockaddr_un addr;
2447 if (interface->global_iface_path == NULL) {
2448 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
2452 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
2453 if (errno == EEXIST) {
2454 wpa_printf(MSG_DEBUG, "Using existing control "
2455 "interface directory.");
2457 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2461 } else if (interface->ctrl_iface_group &&
2462 chown(interface->global_iface_path, -1,
2463 interface->ctrl_iface_group) < 0) {
2464 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2469 if (os_strlen(interface->global_iface_path) + 1 +
2470 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
2473 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2475 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
2479 os_memset(&addr, 0, sizeof(addr));
2481 addr.sun_len = sizeof(addr);
2482 #endif /* __FreeBSD__ */
2483 addr.sun_family = AF_UNIX;
2484 fname = hostapd_global_ctrl_iface_path(interface);
2487 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2488 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2489 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2491 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2492 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2493 " allow connections - assuming it was left"
2494 "over from forced program termination");
2495 if (unlink(fname) < 0) {
2496 wpa_printf(MSG_ERROR,
2497 "Could not unlink existing ctrl_iface socket '%s': %s",
2498 fname, strerror(errno));
2501 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2503 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
2507 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2508 "ctrl_iface socket '%s'", fname);
2510 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2511 "be in use - cannot override it");
2512 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2513 "not used anymore", fname);
2520 if (interface->ctrl_iface_group &&
2521 chown(fname, -1, interface->ctrl_iface_group) < 0) {
2522 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2527 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2528 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2534 interface->global_ctrl_sock = s;
2535 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
2551 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
2555 if (interfaces->global_ctrl_sock > -1) {
2556 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
2557 close(interfaces->global_ctrl_sock);
2558 interfaces->global_ctrl_sock = -1;
2559 fname = hostapd_global_ctrl_iface_path(interfaces);
2565 if (interfaces->global_iface_path &&
2566 rmdir(interfaces->global_iface_path) < 0) {
2567 if (errno == ENOTEMPTY) {
2568 wpa_printf(MSG_DEBUG, "Control interface "
2569 "directory not empty - leaving it "
2572 wpa_printf(MSG_ERROR,
2573 "rmdir[ctrl_interface=%s]: %s",
2574 interfaces->global_iface_path,
2578 os_free(interfaces->global_iface_path);
2579 interfaces->global_iface_path = NULL;
2584 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
2585 const char *buf, size_t len)
2587 struct wpa_ctrl_dst *dst, *next;
2593 dst = hapd->ctrl_dst;
2594 if (hapd->ctrl_sock < 0 || dst == NULL)
2597 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
2598 io[0].iov_base = levelstr;
2599 io[0].iov_len = os_strlen(levelstr);
2600 io[1].iov_base = (char *) buf;
2601 io[1].iov_len = len;
2602 os_memset(&msg, 0, sizeof(msg));
2609 if (level >= dst->debug_level) {
2610 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
2611 (u8 *) dst->addr.sun_path, dst->addrlen -
2612 offsetof(struct sockaddr_un, sun_path));
2613 msg.msg_name = &dst->addr;
2614 msg.msg_namelen = dst->addrlen;
2615 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
2617 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
2619 idx, errno, strerror(errno));
2621 if (dst->errors > 10 || _errno == ENOENT) {
2622 hostapd_ctrl_iface_detach(
2634 #endif /* CONFIG_NATIVE_WINDOWS */