2 * hostapd / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #ifndef CONFIG_NATIVE_WINDOWS
13 #ifdef CONFIG_TESTING_OPTIONS
14 #include <net/ethernet.h>
15 #include <netinet/ip.h>
16 #endif /* CONFIG_TESTING_OPTIONS */
22 #include "utils/common.h"
23 #include "utils/eloop.h"
24 #include "common/version.h"
25 #include "common/ieee802_11_defs.h"
26 #include "crypto/tls.h"
27 #include "drivers/driver.h"
28 #include "eapol_auth/eapol_auth_sm.h"
29 #include "radius/radius_client.h"
30 #include "radius/radius_server.h"
31 #include "l2_packet/l2_packet.h"
32 #include "ap/hostapd.h"
33 #include "ap/ap_config.h"
34 #include "ap/ieee802_1x.h"
35 #include "ap/wpa_auth.h"
36 #include "ap/ieee802_11.h"
37 #include "ap/sta_info.h"
38 #include "ap/wps_hostapd.h"
39 #include "ap/ctrl_iface_ap.h"
40 #include "ap/ap_drv_ops.h"
42 #include "ap/wnm_ap.h"
43 #include "ap/wpa_auth.h"
44 #include "ap/beacon.h"
45 #include "wps/wps_defs.h"
47 #include "config_file.h"
48 #include "ctrl_iface.h"
52 struct wpa_ctrl_dst *next;
53 struct sockaddr_un addr;
60 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
61 const char *buf, size_t len);
64 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
65 struct sockaddr_un *from,
68 struct wpa_ctrl_dst *dst;
70 dst = os_zalloc(sizeof(*dst));
73 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
74 dst->addrlen = fromlen;
75 dst->debug_level = MSG_INFO;
76 dst->next = hapd->ctrl_dst;
78 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
79 (u8 *) from->sun_path,
80 fromlen - offsetof(struct sockaddr_un, sun_path));
85 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
86 struct sockaddr_un *from,
89 struct wpa_ctrl_dst *dst, *prev = NULL;
93 if (fromlen == dst->addrlen &&
94 os_memcmp(from->sun_path, dst->addr.sun_path,
95 fromlen - offsetof(struct sockaddr_un, sun_path))
97 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
98 (u8 *) from->sun_path,
100 offsetof(struct sockaddr_un, sun_path));
102 hapd->ctrl_dst = dst->next;
104 prev->next = dst->next;
115 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
116 struct sockaddr_un *from,
120 struct wpa_ctrl_dst *dst;
122 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
124 dst = hapd->ctrl_dst;
126 if (fromlen == dst->addrlen &&
127 os_memcmp(from->sun_path, dst->addr.sun_path,
128 fromlen - offsetof(struct sockaddr_un, sun_path))
130 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
131 "level", (u8 *) from->sun_path, fromlen -
132 offsetof(struct sockaddr_un, sun_path));
133 dst->debug_level = atoi(level);
143 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
147 struct sta_info *sta;
149 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
151 if (hwaddr_aton(txtaddr, addr))
154 sta = ap_get_sta(hapd, addr);
158 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
159 "notification", MAC2STR(addr));
160 sta = ap_sta_add(hapd, addr);
164 hostapd_new_assoc_sta(hapd, sta, 0);
169 #ifdef CONFIG_IEEE80211W
171 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
175 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
177 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
179 if (hwaddr_aton(txtaddr, addr) ||
180 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
183 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
187 #endif /* NEED_AP_MLME */
188 #endif /* CONFIG_IEEE80211W */
192 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
194 char *pin = os_strchr(txt, ' ');
197 u8 addr_buf[ETH_ALEN], *addr = NULL;
204 timeout_txt = os_strchr(pin, ' ');
206 *timeout_txt++ = '\0';
207 timeout = atoi(timeout_txt);
208 pos = os_strchr(timeout_txt, ' ');
211 if (hwaddr_aton(pos, addr_buf) == 0)
217 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
221 static int hostapd_ctrl_iface_wps_check_pin(
222 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
229 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
230 (u8 *) cmd, os_strlen(cmd));
231 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
232 if (*pos < '0' || *pos > '9')
236 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
240 if (len != 4 && len != 8) {
241 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
247 unsigned int pin_val;
249 if (!wps_pin_valid(pin_val)) {
250 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
251 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
252 if (os_snprintf_error(buflen, ret))
258 ret = os_snprintf(buf, buflen, "%s", pin);
259 if (os_snprintf_error(buflen, ret))
266 #ifdef CONFIG_WPS_NFC
267 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
274 len = os_strlen(pos);
279 buf = wpabuf_alloc(len);
282 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
287 ret = hostapd_wps_nfc_tag_read(hapd, buf);
294 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
295 char *cmd, char *reply,
302 if (os_strcmp(cmd, "WPS") == 0)
304 else if (os_strcmp(cmd, "NDEF") == 0)
309 buf = hostapd_wps_nfc_config_token(hapd, ndef);
313 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
324 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
325 char *reply, size_t max_len,
331 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
335 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
346 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
347 char *cmd, char *reply,
350 if (os_strcmp(cmd, "WPS") == 0)
351 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
354 if (os_strcmp(cmd, "NDEF") == 0)
355 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
358 if (os_strcmp(cmd, "enable") == 0)
359 return hostapd_wps_nfc_token_enable(hapd);
361 if (os_strcmp(cmd, "disable") == 0) {
362 hostapd_wps_nfc_token_disable(hapd);
370 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
371 char *cmd, char *reply,
379 pos = os_strchr(cmd, ' ');
384 if (os_strcmp(cmd, "WPS") == 0)
386 else if (os_strcmp(cmd, "NDEF") == 0)
391 if (os_strcmp(pos, "WPS-CR") == 0)
392 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
398 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
409 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
413 struct wpabuf *req, *sel;
415 char *pos, *role, *type, *pos2;
418 pos = os_strchr(role, ' ');
424 pos = os_strchr(type, ' ');
429 pos2 = os_strchr(pos, ' ');
434 len = os_strlen(pos);
439 req = wpabuf_alloc(len);
442 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
447 len = os_strlen(pos2);
454 sel = wpabuf_alloc(len);
459 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
465 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
466 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
468 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
469 "reported: role=%s type=%s", role, type);
478 #endif /* CONFIG_WPS_NFC */
481 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
482 char *buf, size_t buflen)
488 pos = os_strchr(txt, ' ');
492 if (os_strcmp(txt, "disable") == 0) {
493 hostapd_wps_ap_pin_disable(hapd);
494 return os_snprintf(buf, buflen, "OK\n");
497 if (os_strcmp(txt, "random") == 0) {
500 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
503 return os_snprintf(buf, buflen, "%s", pin_txt);
506 if (os_strcmp(txt, "get") == 0) {
507 pin_txt = hostapd_wps_ap_pin_get(hapd);
510 return os_snprintf(buf, buflen, "%s", pin_txt);
513 if (os_strcmp(txt, "set") == 0) {
518 pos = os_strchr(pos, ' ');
523 if (os_strlen(pin) > buflen)
525 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
527 return os_snprintf(buf, buflen, "%s", pin);
534 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
537 char *ssid, *auth, *encr = NULL, *key = NULL;
540 pos = os_strchr(txt, ' ');
546 pos = os_strchr(pos, ' ');
550 pos = os_strchr(pos, ' ');
557 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
561 static const char * pbc_status_str(enum pbc_status status)
564 case WPS_PBC_STATUS_DISABLE:
566 case WPS_PBC_STATUS_ACTIVE:
568 case WPS_PBC_STATUS_TIMEOUT:
570 case WPS_PBC_STATUS_OVERLAP:
578 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
579 char *buf, size_t buflen)
587 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
588 pbc_status_str(hapd->wps_stats.pbc_status));
590 if (os_snprintf_error(end - pos, ret))
594 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
595 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
597 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
598 "Failed" : "None")));
600 if (os_snprintf_error(end - pos, ret))
604 /* If status == Failure - Add possible Reasons */
605 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
606 hapd->wps_stats.failure_reason > 0) {
607 ret = os_snprintf(pos, end - pos,
608 "Failure Reason: %s\n",
609 wps_ei_str(hapd->wps_stats.failure_reason));
611 if (os_snprintf_error(end - pos, ret))
616 if (hapd->wps_stats.status) {
617 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
618 MAC2STR(hapd->wps_stats.peer_addr));
620 if (os_snprintf_error(end - pos, ret))
628 #endif /* CONFIG_WPS */
632 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
638 if (hwaddr_aton(cmd, addr))
651 return hs20_send_wnm_notification(hapd, addr, 1, url);
655 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
659 int code, reauth_delay, ret;
664 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
665 if (hwaddr_aton(cmd, addr))
668 pos = os_strchr(cmd, ' ');
674 pos = os_strchr(pos, ' ');
678 reauth_delay = atoi(pos);
681 pos = os_strchr(pos, ' ');
684 url_len = os_strlen(pos);
687 req = wpabuf_alloc(4 + url_len);
690 wpabuf_put_u8(req, code);
691 wpabuf_put_le16(req, reauth_delay);
692 wpabuf_put_u8(req, url_len);
694 wpabuf_put_data(req, pos, url_len);
696 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
697 " to indicate imminent deauthentication (code=%d "
698 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
699 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
704 #endif /* CONFIG_HS20 */
707 #ifdef CONFIG_INTERWORKING
709 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
712 u8 qos_map_set[16 + 2 * 21], count = 0;
713 const char *pos = cmd;
717 if (count == sizeof(qos_map_set)) {
718 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
723 if (val < 0 || val > 255) {
724 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
728 qos_map_set[count++] = val;
729 pos = os_strchr(pos, ',');
735 if (count < 16 || count & 1) {
736 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
740 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
742 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
746 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
747 hapd->conf->qos_map_set_len = count;
753 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
757 struct sta_info *sta;
759 u8 *qos_map_set = hapd->conf->qos_map_set;
760 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
763 if (!qos_map_set_len) {
764 wpa_printf(MSG_INFO, "QoS Map Set is not set");
768 if (hwaddr_aton(cmd, addr))
771 sta = ap_get_sta(hapd, addr);
773 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
774 "for QoS Map Configuration message",
779 if (!sta->qos_map_enabled) {
780 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
781 "support for QoS Map", MAC2STR(addr));
785 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
789 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
790 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
792 /* QoS Map Set Element */
793 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
794 wpabuf_put_u8(buf, qos_map_set_len);
795 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
797 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
798 wpabuf_head(buf), wpabuf_len(buf));
804 #endif /* CONFIG_INTERWORKING */
809 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
814 struct sta_info *sta;
816 if (hwaddr_aton(cmd, addr))
820 disassoc_timer = atoi(cmd + 17);
822 sta = ap_get_sta(hapd, addr);
824 wpa_printf(MSG_DEBUG, "Station " MACSTR
825 " not found for disassociation imminent message",
830 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
834 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
838 const char *url, *timerstr;
840 struct sta_info *sta;
842 if (hwaddr_aton(cmd, addr))
845 sta = ap_get_sta(hapd, addr);
847 wpa_printf(MSG_DEBUG, "Station " MACSTR
848 " not found for ESS disassociation imminent message",
854 if (*timerstr != ' ')
857 disassoc_timer = atoi(timerstr);
858 if (disassoc_timer < 0 || disassoc_timer > 65535)
861 url = os_strchr(timerstr, ' ');
866 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
870 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
874 const char *pos, *end;
875 int disassoc_timer = 0;
876 struct sta_info *sta;
877 u8 req_mode = 0, valid_int = 0x01;
882 u8 *nei_pos = nei_rep;
884 if (hwaddr_aton(cmd, addr)) {
885 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
889 sta = ap_get_sta(hapd, addr);
891 wpa_printf(MSG_DEBUG, "Station " MACSTR
892 " not found for BSS TM Request message",
897 pos = os_strstr(cmd, " disassoc_timer=");
900 disassoc_timer = atoi(pos);
901 if (disassoc_timer < 0 || disassoc_timer > 65535) {
902 wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
907 pos = os_strstr(cmd, " valid_int=");
910 valid_int = atoi(pos);
913 pos = os_strstr(cmd, " bss_term=");
916 req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
917 /* TODO: TSF configurable/learnable */
918 bss_term_dur[0] = 4; /* Subelement ID */
919 bss_term_dur[1] = 10; /* Length */
920 os_memset(bss_term_dur, 2, 8);
921 end = os_strchr(pos, ',');
923 wpa_printf(MSG_DEBUG, "Invalid bss_term data");
927 WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
932 * BSS Transition Candidate List Entries - Neighbor Report elements
933 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
934 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
942 pos = os_strstr(pos, " neighbor=");
945 if (nei_pos + 15 > nei_rep + sizeof(nei_rep)) {
946 wpa_printf(MSG_DEBUG,
947 "Not enough room for additional neighbor");
953 *nei_pos++ = WLAN_EID_NEIGHBOR_REPORT;
954 nei_pos++; /* length to be filled in */
956 if (hwaddr_aton(pos, nei_pos)) {
957 wpa_printf(MSG_DEBUG, "Invalid BSSID");
963 wpa_printf(MSG_DEBUG, "Missing BSSID Information");
968 val = strtol(pos, &endptr, 0);
969 WPA_PUT_LE32(nei_pos, val);
971 if (*endptr != ',') {
972 wpa_printf(MSG_DEBUG, "Missing Operating Class");
977 *nei_pos++ = atoi(pos); /* Operating Class */
978 pos = os_strchr(pos, ',');
980 wpa_printf(MSG_DEBUG, "Missing Channel Number");
985 *nei_pos++ = atoi(pos); /* Channel Number */
986 pos = os_strchr(pos, ',');
988 wpa_printf(MSG_DEBUG, "Missing PHY Type");
993 *nei_pos++ = atoi(pos); /* PHY Type */
994 end = os_strchr(pos, ' ');
995 tmp = os_strchr(pos, ',');
996 if (tmp && (!end || tmp < end)) {
997 /* Optional Subelements (hexdump) */
1001 end = os_strchr(pos, ' ');
1005 len = os_strlen(pos);
1006 if (nei_pos + len / 2 > nei_rep + sizeof(nei_rep)) {
1007 wpa_printf(MSG_DEBUG,
1008 "Not enough room for neighbor subelements");
1012 hexstr2bin(pos, nei_pos, len / 2) < 0) {
1013 wpa_printf(MSG_DEBUG,
1014 "Invalid neighbor subelement info");
1021 nei_start[1] = nei_pos - nei_start - 2;
1024 pos = os_strstr(cmd, " url=");
1028 end = os_strchr(pos, ' ');
1032 len = os_strlen(pos);
1033 url = os_malloc(len + 1);
1036 os_memcpy(url, pos, len);
1038 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
1041 if (os_strstr(cmd, " pref=1"))
1042 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1043 if (os_strstr(cmd, " abridged=1"))
1044 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1045 if (os_strstr(cmd, " disassoc_imminent=1"))
1046 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1048 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
1049 valid_int, bss_term_dur, url,
1050 nei_pos > nei_rep ? nei_rep : NULL,
1056 #endif /* CONFIG_WNM */
1059 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1060 char *buf, size_t buflen)
1068 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1070 MAC2STR(hapd->own_addr),
1071 wpa_ssid_txt(hapd->conf->ssid.ssid,
1072 hapd->conf->ssid.ssid_len));
1073 if (os_snprintf_error(end - pos, ret))
1078 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1079 hapd->conf->wps_state == 0 ? "disabled" :
1080 (hapd->conf->wps_state == 1 ? "not configured" :
1082 if (os_snprintf_error(end - pos, ret))
1086 if (hapd->conf->wps_state && hapd->conf->wpa &&
1087 hapd->conf->ssid.wpa_passphrase) {
1088 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1089 hapd->conf->ssid.wpa_passphrase);
1090 if (os_snprintf_error(end - pos, ret))
1095 if (hapd->conf->wps_state && hapd->conf->wpa &&
1096 hapd->conf->ssid.wpa_psk &&
1097 hapd->conf->ssid.wpa_psk->group) {
1098 char hex[PMK_LEN * 2 + 1];
1099 wpa_snprintf_hex(hex, sizeof(hex),
1100 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1101 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
1102 if (os_snprintf_error(end - pos, ret))
1106 #endif /* CONFIG_WPS */
1108 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1109 ret = os_snprintf(pos, end - pos, "key_mgmt=");
1110 if (os_snprintf_error(end - pos, ret))
1114 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1115 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1116 if (os_snprintf_error(end - pos, ret))
1120 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1121 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1122 if (os_snprintf_error(end - pos, ret))
1126 #ifdef CONFIG_IEEE80211R
1127 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1128 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1129 if (os_snprintf_error(end - pos, ret))
1133 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1134 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1135 if (os_snprintf_error(end - pos, ret))
1140 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1141 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1142 if (os_snprintf_error(end - pos, ret))
1146 #endif /* CONFIG_SAE */
1147 #endif /* CONFIG_IEEE80211R */
1148 #ifdef CONFIG_IEEE80211W
1149 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1150 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1151 if (os_snprintf_error(end - pos, ret))
1155 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1156 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1157 if (os_snprintf_error(end - pos, ret))
1161 #endif /* CONFIG_IEEE80211W */
1163 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1164 ret = os_snprintf(pos, end - pos, "SAE ");
1165 if (os_snprintf_error(end - pos, ret))
1169 #endif /* CONFIG_SAE */
1170 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1171 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1172 if (os_snprintf_error(end - pos, ret))
1176 if (hapd->conf->wpa_key_mgmt &
1177 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1178 ret = os_snprintf(pos, end - pos,
1179 "WPA-EAP-SUITE-B-192 ");
1180 if (os_snprintf_error(end - pos, ret))
1185 ret = os_snprintf(pos, end - pos, "\n");
1186 if (os_snprintf_error(end - pos, ret))
1191 if (hapd->conf->wpa) {
1192 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1193 wpa_cipher_txt(hapd->conf->wpa_group));
1194 if (os_snprintf_error(end - pos, ret))
1199 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1200 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
1201 if (os_snprintf_error(end - pos, ret))
1205 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1211 ret = os_snprintf(pos, end - pos, "\n");
1212 if (os_snprintf_error(end - pos, ret))
1217 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1218 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
1219 if (os_snprintf_error(end - pos, ret))
1223 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
1229 ret = os_snprintf(pos, end - pos, "\n");
1230 if (os_snprintf_error(end - pos, ret))
1239 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1244 value = os_strchr(cmd, ' ');
1249 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1251 #ifdef CONFIG_WPS_TESTING
1252 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1254 val = strtol(value, NULL, 0);
1255 if (val < 0 || val > 0xff) {
1257 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1258 "wps_version_number %ld", val);
1260 wps_version_number = val;
1261 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1263 (wps_version_number & 0xf0) >> 4,
1264 wps_version_number & 0x0f);
1265 hostapd_wps_update_ie(hapd);
1267 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1268 wps_testing_dummy_cred = atoi(value);
1269 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1270 wps_testing_dummy_cred);
1271 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1272 wps_corrupt_pkhash = atoi(value);
1273 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1274 wps_corrupt_pkhash);
1275 #endif /* CONFIG_WPS_TESTING */
1276 #ifdef CONFIG_INTERWORKING
1277 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1278 int val = atoi(value);
1282 hapd->gas_frag_limit = val;
1283 #endif /* CONFIG_INTERWORKING */
1284 #ifdef CONFIG_TESTING_OPTIONS
1285 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1286 hapd->ext_mgmt_frame_handling = atoi(value);
1287 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1288 hapd->ext_eapol_frame_io = atoi(value);
1289 #endif /* CONFIG_TESTING_OPTIONS */
1291 struct sta_info *sta;
1294 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1298 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1299 for (sta = hapd->sta_list; sta; sta = sta->next) {
1300 if (hostapd_maclist_found(
1301 hapd->conf->deny_mac,
1302 hapd->conf->num_deny_mac, sta->addr,
1304 (!vlan_id || vlan_id == sta->vlan_id))
1306 hapd, sta, sta->addr,
1307 WLAN_REASON_UNSPECIFIED);
1309 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1310 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1311 for (sta = hapd->sta_list; sta; sta = sta->next) {
1312 if (!hostapd_maclist_found(
1313 hapd->conf->accept_mac,
1314 hapd->conf->num_accept_mac,
1315 sta->addr, &vlan_id) ||
1316 (vlan_id && vlan_id != sta->vlan_id))
1318 hapd, sta, sta->addr,
1319 WLAN_REASON_UNSPECIFIED);
1328 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1329 char *buf, size_t buflen)
1333 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1335 if (os_strcmp(cmd, "version") == 0) {
1336 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1337 if (os_snprintf_error(buflen, res))
1340 } else if (os_strcmp(cmd, "tls_library") == 0) {
1341 res = tls_get_library_version(buf, buflen);
1342 if (os_snprintf_error(buflen, res))
1351 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1353 if (hostapd_enable_iface(iface) < 0) {
1354 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1361 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1363 if (hostapd_reload_iface(iface) < 0) {
1364 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1371 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1373 if (hostapd_disable_iface(iface) < 0) {
1374 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1381 #ifdef CONFIG_TESTING_OPTIONS
1383 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1385 union wpa_event_data data;
1387 enum wpa_event_type event;
1389 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1391 os_memset(&data, 0, sizeof(data));
1393 param = os_strchr(cmd, ' ');
1398 if (os_strcmp(cmd, "DETECTED") == 0)
1399 event = EVENT_DFS_RADAR_DETECTED;
1400 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1401 event = EVENT_DFS_CAC_FINISHED;
1402 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1403 event = EVENT_DFS_CAC_ABORTED;
1404 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1405 event = EVENT_DFS_NOP_FINISHED;
1407 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1412 pos = os_strstr(param, "freq=");
1414 data.dfs_event.freq = atoi(pos + 5);
1416 pos = os_strstr(param, "ht_enabled=1");
1418 data.dfs_event.ht_enabled = 1;
1420 pos = os_strstr(param, "chan_offset=");
1422 data.dfs_event.chan_offset = atoi(pos + 12);
1424 pos = os_strstr(param, "chan_width=");
1426 data.dfs_event.chan_width = atoi(pos + 11);
1428 pos = os_strstr(param, "cf1=");
1430 data.dfs_event.cf1 = atoi(pos + 4);
1432 pos = os_strstr(param, "cf2=");
1434 data.dfs_event.cf2 = atoi(pos + 4);
1436 wpa_supplicant_event(hapd, event, &data);
1442 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1448 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1450 len = os_strlen(cmd);
1455 buf = os_malloc(len);
1459 if (hexstr2bin(cmd, buf, len) < 0) {
1464 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1470 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1473 u8 src[ETH_ALEN], *buf;
1477 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1480 used = hwaddr_aton2(pos, src);
1487 len = os_strlen(pos);
1492 buf = os_malloc(len);
1496 if (hexstr2bin(pos, buf, len) < 0) {
1501 ieee802_1x_receive(hapd, src, buf, len);
1508 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1512 const u16 *pos = buf;
1514 for (i = 0; i < len / 2; i++)
1518 sum = (sum & 0xffff) + (sum >> 16);
1520 return sum ^ 0xffff;
1524 #define HWSIM_PACKETLEN 1500
1525 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1527 void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1530 struct hostapd_data *hapd = ctx;
1531 const struct ether_header *eth;
1536 if (len != HWSIM_PACKETLEN)
1539 eth = (const struct ether_header *) buf;
1540 os_memcpy(&ip, eth + 1, sizeof(ip));
1541 pos = &buf[sizeof(*eth) + sizeof(ip)];
1543 if (ip.ihl != 5 || ip.version != 4 ||
1544 ntohs(ip.tot_len) != HWSIM_IP_LEN)
1547 for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
1553 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
1554 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
1558 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1561 int enabled = atoi(cmd);
1566 if (hapd->l2_test) {
1567 l2_packet_deinit(hapd->l2_test);
1568 hapd->l2_test = NULL;
1569 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1570 "test data: Disabled");
1578 pos = os_strstr(cmd, " ifname=");
1582 ifname = hapd->conf->iface;
1584 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1585 ETHERTYPE_IP, hostapd_data_test_rx,
1587 if (hapd->l2_test == NULL)
1590 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1596 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1598 u8 dst[ETH_ALEN], src[ETH_ALEN];
1603 u8 buf[2 + HWSIM_PACKETLEN];
1604 struct ether_header *eth;
1609 if (hapd->l2_test == NULL)
1612 /* format: <dst> <src> <tos> */
1615 used = hwaddr_aton2(pos, dst);
1621 used = hwaddr_aton2(pos, src);
1626 val = strtol(pos, NULL, 0);
1627 if (val < 0 || val > 0xff)
1631 eth = (struct ether_header *) &buf[2];
1632 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1633 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1634 eth->ether_type = htons(ETHERTYPE_IP);
1635 ip = (struct iphdr *) (eth + 1);
1636 os_memset(ip, 0, sizeof(*ip));
1641 ip->tot_len = htons(HWSIM_IP_LEN);
1643 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1644 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1645 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
1646 dpos = (u8 *) (ip + 1);
1647 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
1650 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
1651 HWSIM_PACKETLEN) < 0)
1654 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1655 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1661 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1665 struct ether_header *eth;
1666 struct l2_packet_data *l2 = NULL;
1670 const char *ifname = hapd->conf->iface;
1672 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1675 cmd = os_strchr(cmd, ' ');
1681 len = os_strlen(cmd);
1682 if (len & 1 || len < ETH_HLEN * 2)
1686 buf = os_malloc(len);
1690 if (hexstr2bin(cmd, buf, len) < 0)
1693 eth = (struct ether_header *) buf;
1694 ethertype = ntohs(eth->ether_type);
1696 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1697 hostapd_data_test_rx, hapd, 1);
1701 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1702 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1705 l2_packet_deinit(l2);
1708 return res < 0 ? -1 : 0;
1712 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
1714 #ifdef WPA_TRACE_BFD
1715 extern char wpa_trace_fail_func[256];
1716 extern unsigned int wpa_trace_fail_after;
1719 wpa_trace_fail_after = atoi(cmd);
1720 pos = os_strchr(cmd, ':');
1723 os_strlcpy(wpa_trace_fail_func, pos,
1724 sizeof(wpa_trace_fail_func));
1726 wpa_trace_fail_after = 0;
1730 #else /* WPA_TRACE_BFD */
1732 #endif /* WPA_TRACE_BFD */
1736 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
1737 char *buf, size_t buflen)
1739 #ifdef WPA_TRACE_BFD
1740 extern char wpa_trace_fail_func[256];
1741 extern unsigned int wpa_trace_fail_after;
1743 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
1744 wpa_trace_fail_func);
1745 #else /* WPA_TRACE_BFD */
1747 #endif /* WPA_TRACE_BFD */
1751 static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
1753 #ifdef WPA_TRACE_BFD
1754 extern char wpa_trace_test_fail_func[256];
1755 extern unsigned int wpa_trace_test_fail_after;
1758 wpa_trace_test_fail_after = atoi(cmd);
1759 pos = os_strchr(cmd, ':');
1762 os_strlcpy(wpa_trace_test_fail_func, pos,
1763 sizeof(wpa_trace_test_fail_func));
1765 wpa_trace_test_fail_after = 0;
1769 #else /* WPA_TRACE_BFD */
1771 #endif /* WPA_TRACE_BFD */
1775 static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
1776 char *buf, size_t buflen)
1778 #ifdef WPA_TRACE_BFD
1779 extern char wpa_trace_test_fail_func[256];
1780 extern unsigned int wpa_trace_test_fail_after;
1782 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
1783 wpa_trace_test_fail_func);
1784 #else /* WPA_TRACE_BFD */
1786 #endif /* WPA_TRACE_BFD */
1789 #endif /* CONFIG_TESTING_OPTIONS */
1792 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
1796 struct csa_settings settings;
1800 ret = hostapd_parse_csa_settings(pos, &settings);
1804 for (i = 0; i < iface->num_bss; i++) {
1805 ret = hostapd_switch_channel(iface->bss[i], &settings);
1807 /* FIX: What do we do if CSA fails in the middle of
1808 * submitting multi-BSS CSA requests? */
1814 #else /* NEED_AP_MLME */
1816 #endif /* NEED_AP_MLME */
1820 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1821 int reply_size, const char *param)
1823 #ifdef RADIUS_SERVER
1824 if (os_strcmp(param, "radius_server") == 0) {
1825 return radius_server_get_mib(hapd->radius_srv, reply,
1828 #endif /* RADIUS_SERVER */
1833 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
1834 char *buf, size_t buflen)
1839 unsigned int vendor_id, subcmd;
1840 struct wpabuf *reply;
1841 size_t data_len = 0;
1843 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1844 vendor_id = strtoul(cmd, &pos, 16);
1848 subcmd = strtoul(pos, &pos, 10);
1851 if (!isblank(*pos++))
1853 data_len = os_strlen(pos);
1858 data = os_malloc(data_len);
1862 if (hexstr2bin(pos, data, data_len)) {
1863 wpa_printf(MSG_DEBUG,
1864 "Vendor command: wrong parameter format");
1870 reply = wpabuf_alloc((buflen - 1) / 2);
1876 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
1880 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
1890 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
1894 struct sta_info *sta;
1896 if (hwaddr_aton(cmd, addr))
1899 sta = ap_get_sta(hapd, addr);
1900 if (!sta || !sta->eapol_sm)
1903 eapol_auth_reauthenticate(sta->eapol_sm);
1908 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
1911 struct hostapd_data *hapd = eloop_ctx;
1914 struct sockaddr_un from;
1915 socklen_t fromlen = sizeof(from);
1917 const int reply_size = 4096;
1919 int level = MSG_DEBUG;
1921 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1922 (struct sockaddr *) &from, &fromlen);
1924 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
1929 if (os_strcmp(buf, "PING") == 0)
1930 level = MSG_EXCESSIVE;
1931 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
1933 reply = os_malloc(reply_size);
1934 if (reply == NULL) {
1935 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
1937 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
1943 os_memcpy(reply, "OK\n", 3);
1946 if (os_strcmp(buf, "PING") == 0) {
1947 os_memcpy(reply, "PONG\n", 5);
1949 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
1950 if (wpa_debug_reopen_file() < 0)
1952 } else if (os_strcmp(buf, "STATUS") == 0) {
1953 reply_len = hostapd_ctrl_iface_status(hapd, reply,
1955 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
1956 reply_len = hostapd_drv_status(hapd, reply, reply_size);
1957 } else if (os_strcmp(buf, "MIB") == 0) {
1958 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
1959 if (reply_len >= 0) {
1960 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
1961 reply_size - reply_len);
1967 if (reply_len >= 0) {
1968 res = ieee802_1x_get_mib(hapd, reply + reply_len,
1969 reply_size - reply_len);
1975 #ifndef CONFIG_NO_RADIUS
1976 if (reply_len >= 0) {
1977 res = radius_client_get_mib(hapd->radius,
1979 reply_size - reply_len);
1985 #endif /* CONFIG_NO_RADIUS */
1986 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
1987 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
1989 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
1990 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
1992 } else if (os_strncmp(buf, "STA ", 4) == 0) {
1993 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
1995 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
1996 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
1998 } else if (os_strcmp(buf, "ATTACH") == 0) {
1999 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
2001 } else if (os_strcmp(buf, "DETACH") == 0) {
2002 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
2004 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
2005 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
2008 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
2009 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
2011 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
2012 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
2014 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
2015 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
2017 } else if (os_strcmp(buf, "STOP_AP") == 0) {
2018 if (hostapd_ctrl_iface_stop_ap(hapd))
2020 #ifdef CONFIG_IEEE80211W
2022 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
2023 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
2025 #endif /* NEED_AP_MLME */
2026 #endif /* CONFIG_IEEE80211W */
2028 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
2029 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
2031 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
2032 reply_len = hostapd_ctrl_iface_wps_check_pin(
2033 hapd, buf + 14, reply, reply_size);
2034 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
2035 if (hostapd_wps_button_pushed(hapd, NULL))
2037 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
2038 if (hostapd_wps_cancel(hapd))
2040 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
2041 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
2043 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
2044 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
2046 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
2047 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
2049 #ifdef CONFIG_WPS_NFC
2050 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
2051 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
2053 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2054 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
2055 hapd, buf + 21, reply, reply_size);
2056 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
2057 reply_len = hostapd_ctrl_iface_wps_nfc_token(
2058 hapd, buf + 14, reply, reply_size);
2059 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2060 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
2061 hapd, buf + 21, reply, reply_size);
2062 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
2063 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
2065 #endif /* CONFIG_WPS_NFC */
2066 #endif /* CONFIG_WPS */
2067 #ifdef CONFIG_INTERWORKING
2068 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
2069 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
2071 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
2072 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
2074 #endif /* CONFIG_INTERWORKING */
2076 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
2077 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
2079 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
2080 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
2082 #endif /* CONFIG_HS20 */
2084 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
2085 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
2087 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
2088 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
2090 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
2091 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
2093 #endif /* CONFIG_WNM */
2094 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
2095 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
2097 } else if (os_strncmp(buf, "SET ", 4) == 0) {
2098 if (hostapd_ctrl_iface_set(hapd, buf + 4))
2100 } else if (os_strncmp(buf, "GET ", 4) == 0) {
2101 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
2103 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
2104 if (hostapd_ctrl_iface_enable(hapd->iface))
2106 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
2107 if (hostapd_ctrl_iface_reload(hapd->iface))
2109 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
2110 if (hostapd_ctrl_iface_disable(hapd->iface))
2112 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
2113 if (ieee802_11_set_beacon(hapd))
2115 #ifdef CONFIG_TESTING_OPTIONS
2116 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
2117 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
2119 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
2120 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
2122 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
2123 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
2125 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
2126 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
2128 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
2129 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
2131 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
2132 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
2134 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
2135 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
2137 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
2138 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
2140 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
2141 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
2143 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
2144 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
2145 #endif /* CONFIG_TESTING_OPTIONS */
2146 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
2147 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
2149 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
2150 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
2152 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
2153 ieee802_1x_erp_flush(hapd);
2154 #ifdef RADIUS_SERVER
2155 radius_server_erp_flush(hapd->radius_srv);
2156 #endif /* RADIUS_SERVER */
2157 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
2158 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
2161 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2165 if (reply_len < 0) {
2166 os_memcpy(reply, "FAIL\n", 5);
2169 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2171 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2178 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
2183 if (hapd->conf->ctrl_interface == NULL)
2186 len = os_strlen(hapd->conf->ctrl_interface) +
2187 os_strlen(hapd->conf->iface) + 2;
2188 buf = os_malloc(len);
2192 os_snprintf(buf, len, "%s/%s",
2193 hapd->conf->ctrl_interface, hapd->conf->iface);
2194 buf[len - 1] = '\0';
2199 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
2200 enum wpa_msg_type type,
2201 const char *txt, size_t len)
2203 struct hostapd_data *hapd = ctx;
2206 hostapd_ctrl_iface_send(hapd, level, txt, len);
2210 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
2212 struct sockaddr_un addr;
2216 if (hapd->ctrl_sock > -1) {
2217 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2221 if (hapd->conf->ctrl_interface == NULL)
2224 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2225 if (errno == EEXIST) {
2226 wpa_printf(MSG_DEBUG, "Using existing control "
2227 "interface directory.");
2229 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2235 if (hapd->conf->ctrl_interface_gid_set &&
2236 chown(hapd->conf->ctrl_interface, -1,
2237 hapd->conf->ctrl_interface_gid) < 0) {
2238 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2243 if (!hapd->conf->ctrl_interface_gid_set &&
2244 hapd->iface->interfaces->ctrl_iface_group &&
2245 chown(hapd->conf->ctrl_interface, -1,
2246 hapd->iface->interfaces->ctrl_iface_group) < 0) {
2247 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2254 * Android is using umask 0077 which would leave the control interface
2255 * directory without group access. This breaks things since Wi-Fi
2256 * framework assumes that this directory can be accessed by other
2257 * applications in the wifi group. Fix this by adding group access even
2258 * if umask value would prevent this.
2260 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2261 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
2263 /* Try to continue anyway */
2265 #endif /* ANDROID */
2267 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
2268 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
2271 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2273 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
2277 os_memset(&addr, 0, sizeof(addr));
2279 addr.sun_len = sizeof(addr);
2280 #endif /* __FreeBSD__ */
2281 addr.sun_family = AF_UNIX;
2282 fname = hostapd_ctrl_iface_path(hapd);
2285 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2286 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2287 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2289 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2290 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2291 " allow connections - assuming it was left"
2292 "over from forced program termination");
2293 if (unlink(fname) < 0) {
2294 wpa_printf(MSG_ERROR,
2295 "Could not unlink existing ctrl_iface socket '%s': %s",
2296 fname, strerror(errno));
2299 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2301 wpa_printf(MSG_ERROR,
2302 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
2306 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2307 "ctrl_iface socket '%s'", fname);
2309 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2310 "be in use - cannot override it");
2311 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2312 "not used anymore", fname);
2319 if (hapd->conf->ctrl_interface_gid_set &&
2320 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
2321 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2326 if (!hapd->conf->ctrl_interface_gid_set &&
2327 hapd->iface->interfaces->ctrl_iface_group &&
2328 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
2329 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2334 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2335 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2341 hapd->ctrl_sock = s;
2342 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
2344 hostapd_ctrl_iface_deinit(hapd);
2347 hapd->msg_ctx = hapd;
2348 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
2363 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
2365 struct wpa_ctrl_dst *dst, *prev;
2367 if (hapd->ctrl_sock > -1) {
2369 eloop_unregister_read_sock(hapd->ctrl_sock);
2370 close(hapd->ctrl_sock);
2371 hapd->ctrl_sock = -1;
2372 fname = hostapd_ctrl_iface_path(hapd);
2377 if (hapd->conf->ctrl_interface &&
2378 rmdir(hapd->conf->ctrl_interface) < 0) {
2379 if (errno == ENOTEMPTY) {
2380 wpa_printf(MSG_DEBUG, "Control interface "
2381 "directory not empty - leaving it "
2384 wpa_printf(MSG_ERROR,
2385 "rmdir[ctrl_interface=%s]: %s",
2386 hapd->conf->ctrl_interface,
2392 dst = hapd->ctrl_dst;
2393 hapd->ctrl_dst = NULL;
2400 #ifdef CONFIG_TESTING_OPTIONS
2401 l2_packet_deinit(hapd->l2_test);
2402 hapd->l2_test = NULL;
2403 #endif /* CONFIG_TESTING_OPTIONS */
2407 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
2410 if (hostapd_add_iface(interfaces, buf) < 0) {
2411 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
2418 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
2421 if (hostapd_remove_iface(interfaces, buf) < 0) {
2422 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
2429 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
2431 #ifdef CONFIG_WPS_TESTING
2432 wps_version_number = 0x20;
2433 wps_testing_dummy_cred = 0;
2434 wps_corrupt_pkhash = 0;
2435 #endif /* CONFIG_WPS_TESTING */
2439 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
2442 void *interfaces = eloop_ctx;
2445 struct sockaddr_un from;
2446 socklen_t fromlen = sizeof(from);
2450 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2451 (struct sockaddr *) &from, &fromlen);
2453 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2458 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
2460 os_memcpy(reply, "OK\n", 3);
2463 if (os_strcmp(buf, "PING") == 0) {
2464 os_memcpy(reply, "PONG\n", 5);
2466 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2467 if (wpa_debug_reopen_file() < 0)
2469 } else if (os_strcmp(buf, "FLUSH") == 0) {
2470 hostapd_ctrl_iface_flush(interfaces);
2471 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
2472 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
2474 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
2475 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
2477 #ifdef CONFIG_MODULE_TESTS
2478 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
2479 int hapd_module_tests(void);
2480 if (hapd_module_tests() < 0)
2482 #endif /* CONFIG_MODULE_TESTS */
2484 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
2489 if (reply_len < 0) {
2490 os_memcpy(reply, "FAIL\n", 5);
2494 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2496 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2502 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
2507 if (interface->global_iface_path == NULL)
2510 len = os_strlen(interface->global_iface_path) +
2511 os_strlen(interface->global_iface_name) + 2;
2512 buf = os_malloc(len);
2516 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
2517 interface->global_iface_name);
2518 buf[len - 1] = '\0';
2523 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
2525 struct sockaddr_un addr;
2529 if (interface->global_iface_path == NULL) {
2530 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
2534 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
2535 if (errno == EEXIST) {
2536 wpa_printf(MSG_DEBUG, "Using existing control "
2537 "interface directory.");
2539 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2543 } else if (interface->ctrl_iface_group &&
2544 chown(interface->global_iface_path, -1,
2545 interface->ctrl_iface_group) < 0) {
2546 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2551 if (os_strlen(interface->global_iface_path) + 1 +
2552 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
2555 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2557 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
2561 os_memset(&addr, 0, sizeof(addr));
2563 addr.sun_len = sizeof(addr);
2564 #endif /* __FreeBSD__ */
2565 addr.sun_family = AF_UNIX;
2566 fname = hostapd_global_ctrl_iface_path(interface);
2569 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2570 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2571 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2573 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2574 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2575 " allow connections - assuming it was left"
2576 "over from forced program termination");
2577 if (unlink(fname) < 0) {
2578 wpa_printf(MSG_ERROR,
2579 "Could not unlink existing ctrl_iface socket '%s': %s",
2580 fname, strerror(errno));
2583 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2585 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
2589 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2590 "ctrl_iface socket '%s'", fname);
2592 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2593 "be in use - cannot override it");
2594 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2595 "not used anymore", fname);
2602 if (interface->ctrl_iface_group &&
2603 chown(fname, -1, interface->ctrl_iface_group) < 0) {
2604 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2609 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2610 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2616 interface->global_ctrl_sock = s;
2617 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
2633 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
2637 if (interfaces->global_ctrl_sock > -1) {
2638 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
2639 close(interfaces->global_ctrl_sock);
2640 interfaces->global_ctrl_sock = -1;
2641 fname = hostapd_global_ctrl_iface_path(interfaces);
2647 if (interfaces->global_iface_path &&
2648 rmdir(interfaces->global_iface_path) < 0) {
2649 if (errno == ENOTEMPTY) {
2650 wpa_printf(MSG_DEBUG, "Control interface "
2651 "directory not empty - leaving it "
2654 wpa_printf(MSG_ERROR,
2655 "rmdir[ctrl_interface=%s]: %s",
2656 interfaces->global_iface_path,
2660 os_free(interfaces->global_iface_path);
2661 interfaces->global_iface_path = NULL;
2666 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
2667 const char *buf, size_t len)
2669 struct wpa_ctrl_dst *dst, *next;
2675 dst = hapd->ctrl_dst;
2676 if (hapd->ctrl_sock < 0 || dst == NULL)
2679 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
2680 io[0].iov_base = levelstr;
2681 io[0].iov_len = os_strlen(levelstr);
2682 io[1].iov_base = (char *) buf;
2683 io[1].iov_len = len;
2684 os_memset(&msg, 0, sizeof(msg));
2691 if (level >= dst->debug_level) {
2692 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
2693 (u8 *) dst->addr.sun_path, dst->addrlen -
2694 offsetof(struct sockaddr_un, sun_path));
2695 msg.msg_name = &dst->addr;
2696 msg.msg_namelen = dst->addrlen;
2697 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
2699 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
2701 idx, errno, strerror(errno));
2703 if (dst->errors > 10 || _errno == ENOENT) {
2704 hostapd_ctrl_iface_detach(
2716 #endif /* CONFIG_NATIVE_WINDOWS */