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 #ifdef CONFIG_CTRL_IFACE_UDP
24 #endif /* CONFIG_CTRL_IFACE_UDP */
26 #include "utils/common.h"
27 #include "utils/eloop.h"
28 #include "common/version.h"
29 #include "common/ieee802_11_defs.h"
30 #include "common/ctrl_iface_common.h"
31 #include "crypto/tls.h"
32 #include "drivers/driver.h"
33 #include "eapol_auth/eapol_auth_sm.h"
34 #include "radius/radius_client.h"
35 #include "radius/radius_server.h"
36 #include "l2_packet/l2_packet.h"
37 #include "ap/hostapd.h"
38 #include "ap/ap_config.h"
39 #include "ap/ieee802_1x.h"
40 #include "ap/wpa_auth.h"
41 #include "ap/ieee802_11.h"
42 #include "ap/sta_info.h"
43 #include "ap/wps_hostapd.h"
44 #include "ap/ctrl_iface_ap.h"
45 #include "ap/ap_drv_ops.h"
47 #include "ap/wnm_ap.h"
48 #include "ap/wpa_auth.h"
49 #include "ap/beacon.h"
50 #include "ap/neighbor_db.h"
52 #include "wps/wps_defs.h"
54 #include "fst/fst_ctrl_iface.h"
55 #include "config_file.h"
56 #include "ctrl_iface.h"
59 #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
61 #ifdef CONFIG_CTRL_IFACE_UDP
63 static unsigned char cookie[COOKIE_LEN];
64 static unsigned char gcookie[COOKIE_LEN];
65 #define HOSTAPD_CTRL_IFACE_PORT 8877
66 #define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
67 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
68 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
69 #endif /* CONFIG_CTRL_IFACE_UDP */
71 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
72 enum wpa_msg_type type,
73 const char *buf, size_t len);
76 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
77 struct sockaddr_storage *from,
80 return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen);
84 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
85 struct sockaddr_storage *from,
88 return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
92 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
93 struct sockaddr_storage *from,
97 return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
101 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
105 struct sta_info *sta;
107 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
109 if (hwaddr_aton(txtaddr, addr))
112 sta = ap_get_sta(hapd, addr);
116 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
117 "notification", MAC2STR(addr));
118 sta = ap_sta_add(hapd, addr);
122 hostapd_new_assoc_sta(hapd, sta, 0);
127 #ifdef CONFIG_IEEE80211W
129 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
133 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
135 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
137 if (hwaddr_aton(txtaddr, addr) ||
138 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
141 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
145 #endif /* NEED_AP_MLME */
146 #endif /* CONFIG_IEEE80211W */
150 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
152 char *pin = os_strchr(txt, ' ');
155 u8 addr_buf[ETH_ALEN], *addr = NULL;
162 timeout_txt = os_strchr(pin, ' ');
164 *timeout_txt++ = '\0';
165 timeout = atoi(timeout_txt);
166 pos = os_strchr(timeout_txt, ' ');
169 if (hwaddr_aton(pos, addr_buf) == 0)
175 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
179 static int hostapd_ctrl_iface_wps_check_pin(
180 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
187 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
188 (u8 *) cmd, os_strlen(cmd));
189 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
190 if (*pos < '0' || *pos > '9')
194 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
198 if (len != 4 && len != 8) {
199 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
205 unsigned int pin_val;
207 if (!wps_pin_valid(pin_val)) {
208 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
209 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
210 if (os_snprintf_error(buflen, ret))
216 ret = os_snprintf(buf, buflen, "%s", pin);
217 if (os_snprintf_error(buflen, ret))
224 #ifdef CONFIG_WPS_NFC
225 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
232 len = os_strlen(pos);
237 buf = wpabuf_alloc(len);
240 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
245 ret = hostapd_wps_nfc_tag_read(hapd, buf);
252 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
253 char *cmd, char *reply,
260 if (os_strcmp(cmd, "WPS") == 0)
262 else if (os_strcmp(cmd, "NDEF") == 0)
267 buf = hostapd_wps_nfc_config_token(hapd, ndef);
271 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
282 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
283 char *reply, size_t max_len,
289 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
293 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
304 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
305 char *cmd, char *reply,
308 if (os_strcmp(cmd, "WPS") == 0)
309 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
312 if (os_strcmp(cmd, "NDEF") == 0)
313 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
316 if (os_strcmp(cmd, "enable") == 0)
317 return hostapd_wps_nfc_token_enable(hapd);
319 if (os_strcmp(cmd, "disable") == 0) {
320 hostapd_wps_nfc_token_disable(hapd);
328 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
329 char *cmd, char *reply,
337 pos = os_strchr(cmd, ' ');
342 if (os_strcmp(cmd, "WPS") == 0)
344 else if (os_strcmp(cmd, "NDEF") == 0)
349 if (os_strcmp(pos, "WPS-CR") == 0)
350 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
356 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
367 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
371 struct wpabuf *req, *sel;
373 char *pos, *role, *type, *pos2;
376 pos = os_strchr(role, ' ');
382 pos = os_strchr(type, ' ');
387 pos2 = os_strchr(pos, ' ');
392 len = os_strlen(pos);
397 req = wpabuf_alloc(len);
400 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
405 len = os_strlen(pos2);
412 sel = wpabuf_alloc(len);
417 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
423 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
424 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
426 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
427 "reported: role=%s type=%s", role, type);
436 #endif /* CONFIG_WPS_NFC */
439 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
440 char *buf, size_t buflen)
446 pos = os_strchr(txt, ' ');
450 if (os_strcmp(txt, "disable") == 0) {
451 hostapd_wps_ap_pin_disable(hapd);
452 return os_snprintf(buf, buflen, "OK\n");
455 if (os_strcmp(txt, "random") == 0) {
458 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
461 return os_snprintf(buf, buflen, "%s", pin_txt);
464 if (os_strcmp(txt, "get") == 0) {
465 pin_txt = hostapd_wps_ap_pin_get(hapd);
468 return os_snprintf(buf, buflen, "%s", pin_txt);
471 if (os_strcmp(txt, "set") == 0) {
476 pos = os_strchr(pos, ' ');
481 if (os_strlen(pin) > buflen)
483 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
485 return os_snprintf(buf, buflen, "%s", pin);
492 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
495 char *ssid, *auth, *encr = NULL, *key = NULL;
498 pos = os_strchr(txt, ' ');
504 pos = os_strchr(pos, ' ');
508 pos = os_strchr(pos, ' ');
515 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
519 static const char * pbc_status_str(enum pbc_status status)
522 case WPS_PBC_STATUS_DISABLE:
524 case WPS_PBC_STATUS_ACTIVE:
526 case WPS_PBC_STATUS_TIMEOUT:
528 case WPS_PBC_STATUS_OVERLAP:
536 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
537 char *buf, size_t buflen)
545 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
546 pbc_status_str(hapd->wps_stats.pbc_status));
548 if (os_snprintf_error(end - pos, ret))
552 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
553 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
555 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
556 "Failed" : "None")));
558 if (os_snprintf_error(end - pos, ret))
562 /* If status == Failure - Add possible Reasons */
563 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
564 hapd->wps_stats.failure_reason > 0) {
565 ret = os_snprintf(pos, end - pos,
566 "Failure Reason: %s\n",
567 wps_ei_str(hapd->wps_stats.failure_reason));
569 if (os_snprintf_error(end - pos, ret))
574 if (hapd->wps_stats.status) {
575 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
576 MAC2STR(hapd->wps_stats.peer_addr));
578 if (os_snprintf_error(end - pos, ret))
586 #endif /* CONFIG_WPS */
590 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
596 if (hwaddr_aton(cmd, addr))
609 return hs20_send_wnm_notification(hapd, addr, 1, url);
613 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
617 int code, reauth_delay, ret;
622 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
623 if (hwaddr_aton(cmd, addr))
626 pos = os_strchr(cmd, ' ');
632 pos = os_strchr(pos, ' ');
636 reauth_delay = atoi(pos);
639 pos = os_strchr(pos, ' ');
642 url_len = os_strlen(pos);
645 req = wpabuf_alloc(4 + url_len);
648 wpabuf_put_u8(req, code);
649 wpabuf_put_le16(req, reauth_delay);
650 wpabuf_put_u8(req, url_len);
652 wpabuf_put_data(req, pos, url_len);
654 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
655 " to indicate imminent deauthentication (code=%d "
656 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
657 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
662 #endif /* CONFIG_HS20 */
665 #ifdef CONFIG_INTERWORKING
667 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
670 u8 qos_map_set[16 + 2 * 21], count = 0;
671 const char *pos = cmd;
675 if (count == sizeof(qos_map_set)) {
676 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
681 if (val < 0 || val > 255) {
682 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
686 qos_map_set[count++] = val;
687 pos = os_strchr(pos, ',');
693 if (count < 16 || count & 1) {
694 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
698 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
700 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
704 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
705 hapd->conf->qos_map_set_len = count;
711 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
715 struct sta_info *sta;
717 u8 *qos_map_set = hapd->conf->qos_map_set;
718 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
721 if (!qos_map_set_len) {
722 wpa_printf(MSG_INFO, "QoS Map Set is not set");
726 if (hwaddr_aton(cmd, addr))
729 sta = ap_get_sta(hapd, addr);
731 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
732 "for QoS Map Configuration message",
737 if (!sta->qos_map_enabled) {
738 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
739 "support for QoS Map", MAC2STR(addr));
743 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
747 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
748 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
750 /* QoS Map Set Element */
751 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
752 wpabuf_put_u8(buf, qos_map_set_len);
753 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
755 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
756 wpabuf_head(buf), wpabuf_len(buf));
762 #endif /* CONFIG_INTERWORKING */
767 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
772 struct sta_info *sta;
774 if (hwaddr_aton(cmd, addr))
778 disassoc_timer = atoi(cmd + 17);
780 sta = ap_get_sta(hapd, addr);
782 wpa_printf(MSG_DEBUG, "Station " MACSTR
783 " not found for disassociation imminent message",
788 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
792 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
796 const char *url, *timerstr;
798 struct sta_info *sta;
800 if (hwaddr_aton(cmd, addr))
803 sta = ap_get_sta(hapd, addr);
805 wpa_printf(MSG_DEBUG, "Station " MACSTR
806 " not found for ESS disassociation imminent message",
812 if (*timerstr != ' ')
815 disassoc_timer = atoi(timerstr);
816 if (disassoc_timer < 0 || disassoc_timer > 65535)
819 url = os_strchr(timerstr, ' ');
824 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
828 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
832 const char *pos, *end;
833 int disassoc_timer = 0;
834 struct sta_info *sta;
835 u8 req_mode = 0, valid_int = 0x01;
840 u8 *nei_pos = nei_rep;
844 if (hwaddr_aton(cmd, addr)) {
845 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
849 sta = ap_get_sta(hapd, addr);
851 wpa_printf(MSG_DEBUG, "Station " MACSTR
852 " not found for BSS TM Request message",
857 pos = os_strstr(cmd, " disassoc_timer=");
860 disassoc_timer = atoi(pos);
861 if (disassoc_timer < 0 || disassoc_timer > 65535) {
862 wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
867 pos = os_strstr(cmd, " valid_int=");
870 valid_int = atoi(pos);
873 pos = os_strstr(cmd, " bss_term=");
876 req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
877 /* TODO: TSF configurable/learnable */
878 bss_term_dur[0] = 4; /* Subelement ID */
879 bss_term_dur[1] = 10; /* Length */
880 os_memset(bss_term_dur, 2, 8);
881 end = os_strchr(pos, ',');
883 wpa_printf(MSG_DEBUG, "Invalid bss_term data");
887 WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
892 * BSS Transition Candidate List Entries - Neighbor Report elements
893 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
894 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
902 pos = os_strstr(pos, " neighbor=");
905 if (nei_pos + 15 > nei_rep + sizeof(nei_rep)) {
906 wpa_printf(MSG_DEBUG,
907 "Not enough room for additional neighbor");
913 *nei_pos++ = WLAN_EID_NEIGHBOR_REPORT;
914 nei_pos++; /* length to be filled in */
916 if (hwaddr_aton(pos, nei_pos)) {
917 wpa_printf(MSG_DEBUG, "Invalid BSSID");
923 wpa_printf(MSG_DEBUG, "Missing BSSID Information");
928 val = strtol(pos, &endptr, 0);
929 WPA_PUT_LE32(nei_pos, val);
931 if (*endptr != ',') {
932 wpa_printf(MSG_DEBUG, "Missing Operating Class");
937 *nei_pos++ = atoi(pos); /* Operating Class */
938 pos = os_strchr(pos, ',');
940 wpa_printf(MSG_DEBUG, "Missing Channel Number");
945 *nei_pos++ = atoi(pos); /* Channel Number */
946 pos = os_strchr(pos, ',');
948 wpa_printf(MSG_DEBUG, "Missing PHY Type");
953 *nei_pos++ = atoi(pos); /* PHY Type */
954 end = os_strchr(pos, ' ');
955 tmp = os_strchr(pos, ',');
956 if (tmp && (!end || tmp < end)) {
957 /* Optional Subelements (hexdump) */
961 end = os_strchr(pos, ' ');
965 len = os_strlen(pos);
966 if (nei_pos + len / 2 > nei_rep + sizeof(nei_rep)) {
967 wpa_printf(MSG_DEBUG,
968 "Not enough room for neighbor subelements");
972 hexstr2bin(pos, nei_pos, len / 2) < 0) {
973 wpa_printf(MSG_DEBUG,
974 "Invalid neighbor subelement info");
981 nei_start[1] = nei_pos - nei_start - 2;
984 pos = os_strstr(cmd, " url=");
988 end = os_strchr(pos, ' ');
992 len = os_strlen(pos);
993 url = os_malloc(len + 1);
996 os_memcpy(url, pos, len);
998 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
1001 if (os_strstr(cmd, " pref=1"))
1002 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1003 if (os_strstr(cmd, " abridged=1"))
1004 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1005 if (os_strstr(cmd, " disassoc_imminent=1"))
1006 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1009 pos = os_strstr(cmd, "mbo=");
1011 unsigned int mbo_reason, cell_pref, reassoc_delay;
1014 ret = sscanf(pos, "mbo=%u:%u:%u", &mbo_reason,
1015 &reassoc_delay, &cell_pref);
1017 wpa_printf(MSG_DEBUG,
1018 "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
1022 if (mbo_reason > MBO_TRANSITION_REASON_PREMIUM_AP) {
1023 wpa_printf(MSG_DEBUG,
1024 "Invalid MBO transition reason code %u",
1029 /* Valid values for Cellular preference are: 0, 1, 255 */
1030 if (cell_pref != 0 && cell_pref != 1 && cell_pref != 255) {
1031 wpa_printf(MSG_DEBUG,
1032 "Invalid MBO cellular capability %u",
1037 if (reassoc_delay > 65535 ||
1039 !(req_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT))) {
1040 wpa_printf(MSG_DEBUG,
1041 "MBO: Assoc retry delay is only valid in disassoc imminent mode");
1045 *mbo_pos++ = MBO_ATTR_ID_TRANSITION_REASON;
1047 *mbo_pos++ = mbo_reason;
1048 *mbo_pos++ = MBO_ATTR_ID_CELL_DATA_PREF;
1050 *mbo_pos++ = cell_pref;
1052 if (reassoc_delay) {
1053 *mbo_pos++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY;
1055 WPA_PUT_LE16(mbo_pos, reassoc_delay);
1059 mbo_len = mbo_pos - mbo;
1061 #endif /* CONFIG_MBO */
1063 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
1064 valid_int, bss_term_dur, url,
1065 nei_pos > nei_rep ? nei_rep : NULL,
1066 nei_pos - nei_rep, mbo_len ? mbo : NULL,
1072 #endif /* CONFIG_WNM */
1075 static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
1076 char *buf, size_t buflen)
1084 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
1086 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1087 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1088 if (os_snprintf_error(end - pos, ret))
1092 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1093 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1094 if (os_snprintf_error(end - pos, ret))
1098 #ifdef CONFIG_IEEE80211R
1099 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1100 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1101 if (os_snprintf_error(end - pos, ret))
1105 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1106 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1107 if (os_snprintf_error(end - pos, ret))
1112 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1113 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1114 if (os_snprintf_error(end - pos, ret))
1118 #endif /* CONFIG_SAE */
1119 #endif /* CONFIG_IEEE80211R */
1120 #ifdef CONFIG_IEEE80211W
1121 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1122 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1123 if (os_snprintf_error(end - pos, ret))
1127 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1128 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1129 if (os_snprintf_error(end - pos, ret))
1133 #endif /* CONFIG_IEEE80211W */
1135 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1136 ret = os_snprintf(pos, end - pos, "SAE ");
1137 if (os_snprintf_error(end - pos, ret))
1141 #endif /* CONFIG_SAE */
1142 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1143 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1144 if (os_snprintf_error(end - pos, ret))
1148 if (hapd->conf->wpa_key_mgmt &
1149 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1150 ret = os_snprintf(pos, end - pos,
1151 "WPA-EAP-SUITE-B-192 ");
1152 if (os_snprintf_error(end - pos, ret))
1157 if (pos > buf && *(pos - 1) == ' ') {
1166 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1167 char *buf, size_t buflen)
1175 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1177 MAC2STR(hapd->own_addr),
1178 wpa_ssid_txt(hapd->conf->ssid.ssid,
1179 hapd->conf->ssid.ssid_len));
1180 if (os_snprintf_error(end - pos, ret))
1185 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1186 hapd->conf->wps_state == 0 ? "disabled" :
1187 (hapd->conf->wps_state == 1 ? "not configured" :
1189 if (os_snprintf_error(end - pos, ret))
1193 if (hapd->conf->wps_state && hapd->conf->wpa &&
1194 hapd->conf->ssid.wpa_passphrase) {
1195 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1196 hapd->conf->ssid.wpa_passphrase);
1197 if (os_snprintf_error(end - pos, ret))
1202 if (hapd->conf->wps_state && hapd->conf->wpa &&
1203 hapd->conf->ssid.wpa_psk &&
1204 hapd->conf->ssid.wpa_psk->group) {
1205 char hex[PMK_LEN * 2 + 1];
1206 wpa_snprintf_hex(hex, sizeof(hex),
1207 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1208 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
1209 if (os_snprintf_error(end - pos, ret))
1213 #endif /* CONFIG_WPS */
1215 if (hapd->conf->wpa) {
1216 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1217 if (os_snprintf_error(end - pos, ret))
1222 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1223 ret = os_snprintf(pos, end - pos, "key_mgmt=");
1224 if (os_snprintf_error(end - pos, ret))
1228 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
1230 ret = os_snprintf(pos, end - pos, "\n");
1231 if (os_snprintf_error(end - pos, ret))
1236 if (hapd->conf->wpa) {
1237 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1238 wpa_cipher_txt(hapd->conf->wpa_group));
1239 if (os_snprintf_error(end - pos, ret))
1244 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1245 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
1246 if (os_snprintf_error(end - pos, ret))
1250 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1256 ret = os_snprintf(pos, end - pos, "\n");
1257 if (os_snprintf_error(end - pos, ret))
1262 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1263 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
1264 if (os_snprintf_error(end - pos, ret))
1268 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
1274 ret = os_snprintf(pos, end - pos, "\n");
1275 if (os_snprintf_error(end - pos, ret))
1284 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1289 value = os_strchr(cmd, ' ');
1294 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1296 #ifdef CONFIG_WPS_TESTING
1297 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1299 val = strtol(value, NULL, 0);
1300 if (val < 0 || val > 0xff) {
1302 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1303 "wps_version_number %ld", val);
1305 wps_version_number = val;
1306 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1308 (wps_version_number & 0xf0) >> 4,
1309 wps_version_number & 0x0f);
1310 hostapd_wps_update_ie(hapd);
1312 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1313 wps_testing_dummy_cred = atoi(value);
1314 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1315 wps_testing_dummy_cred);
1316 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1317 wps_corrupt_pkhash = atoi(value);
1318 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1319 wps_corrupt_pkhash);
1320 #endif /* CONFIG_WPS_TESTING */
1321 #ifdef CONFIG_INTERWORKING
1322 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1323 int val = atoi(value);
1327 hapd->gas_frag_limit = val;
1328 #endif /* CONFIG_INTERWORKING */
1329 #ifdef CONFIG_TESTING_OPTIONS
1330 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1331 hapd->ext_mgmt_frame_handling = atoi(value);
1332 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1333 hapd->ext_eapol_frame_io = atoi(value);
1334 #endif /* CONFIG_TESTING_OPTIONS */
1336 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1339 if (!hapd->conf->mbo_enabled)
1343 if (val < 0 || val > 1)
1346 hapd->mbo_assoc_disallow = val;
1347 ieee802_11_update_beacons(hapd->iface);
1350 * TODO: Need to configure drivers that do AP MLME offload with
1351 * disallowing station logic.
1353 #endif /* CONFIG_MBO */
1355 struct sta_info *sta;
1356 struct vlan_description vlan_id;
1358 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1362 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1363 for (sta = hapd->sta_list; sta; sta = sta->next) {
1364 if (hostapd_maclist_found(
1365 hapd->conf->deny_mac,
1366 hapd->conf->num_deny_mac, sta->addr,
1368 (!vlan_id.notempty ||
1369 !vlan_compare(&vlan_id, sta->vlan_desc)))
1371 hapd, sta, sta->addr,
1372 WLAN_REASON_UNSPECIFIED);
1374 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1375 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1376 for (sta = hapd->sta_list; sta; sta = sta->next) {
1377 if (!hostapd_maclist_found(
1378 hapd->conf->accept_mac,
1379 hapd->conf->num_accept_mac,
1380 sta->addr, &vlan_id) ||
1381 (vlan_id.notempty &&
1382 vlan_compare(&vlan_id, sta->vlan_desc)))
1384 hapd, sta, sta->addr,
1385 WLAN_REASON_UNSPECIFIED);
1394 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1395 char *buf, size_t buflen)
1399 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1401 if (os_strcmp(cmd, "version") == 0) {
1402 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1403 if (os_snprintf_error(buflen, res))
1406 } else if (os_strcmp(cmd, "tls_library") == 0) {
1407 res = tls_get_library_version(buf, buflen);
1408 if (os_snprintf_error(buflen, res))
1417 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1419 if (hostapd_enable_iface(iface) < 0) {
1420 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1427 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1429 if (hostapd_reload_iface(iface) < 0) {
1430 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1437 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1439 if (hostapd_disable_iface(iface) < 0) {
1440 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1447 #ifdef CONFIG_TESTING_OPTIONS
1449 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1451 union wpa_event_data data;
1453 enum wpa_event_type event;
1455 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1457 os_memset(&data, 0, sizeof(data));
1459 param = os_strchr(cmd, ' ');
1464 if (os_strcmp(cmd, "DETECTED") == 0)
1465 event = EVENT_DFS_RADAR_DETECTED;
1466 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1467 event = EVENT_DFS_CAC_FINISHED;
1468 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1469 event = EVENT_DFS_CAC_ABORTED;
1470 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1471 event = EVENT_DFS_NOP_FINISHED;
1473 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1478 pos = os_strstr(param, "freq=");
1480 data.dfs_event.freq = atoi(pos + 5);
1482 pos = os_strstr(param, "ht_enabled=1");
1484 data.dfs_event.ht_enabled = 1;
1486 pos = os_strstr(param, "chan_offset=");
1488 data.dfs_event.chan_offset = atoi(pos + 12);
1490 pos = os_strstr(param, "chan_width=");
1492 data.dfs_event.chan_width = atoi(pos + 11);
1494 pos = os_strstr(param, "cf1=");
1496 data.dfs_event.cf1 = atoi(pos + 4);
1498 pos = os_strstr(param, "cf2=");
1500 data.dfs_event.cf2 = atoi(pos + 4);
1502 wpa_supplicant_event(hapd, event, &data);
1508 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1514 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1516 len = os_strlen(cmd);
1521 buf = os_malloc(len);
1525 if (hexstr2bin(cmd, buf, len) < 0) {
1530 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1536 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1539 u8 src[ETH_ALEN], *buf;
1543 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1546 used = hwaddr_aton2(pos, src);
1553 len = os_strlen(pos);
1558 buf = os_malloc(len);
1562 if (hexstr2bin(pos, buf, len) < 0) {
1567 ieee802_1x_receive(hapd, src, buf, len);
1574 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1578 const u16 *pos = buf;
1580 for (i = 0; i < len / 2; i++)
1584 sum = (sum & 0xffff) + (sum >> 16);
1586 return sum ^ 0xffff;
1590 #define HWSIM_PACKETLEN 1500
1591 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1593 void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1596 struct hostapd_data *hapd = ctx;
1597 const struct ether_header *eth;
1602 if (len != HWSIM_PACKETLEN)
1605 eth = (const struct ether_header *) buf;
1606 os_memcpy(&ip, eth + 1, sizeof(ip));
1607 pos = &buf[sizeof(*eth) + sizeof(ip)];
1609 if (ip.ihl != 5 || ip.version != 4 ||
1610 ntohs(ip.tot_len) != HWSIM_IP_LEN)
1613 for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
1619 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
1620 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
1624 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1627 int enabled = atoi(cmd);
1632 if (hapd->l2_test) {
1633 l2_packet_deinit(hapd->l2_test);
1634 hapd->l2_test = NULL;
1635 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1636 "test data: Disabled");
1644 pos = os_strstr(cmd, " ifname=");
1648 ifname = hapd->conf->iface;
1650 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1651 ETHERTYPE_IP, hostapd_data_test_rx,
1653 if (hapd->l2_test == NULL)
1656 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1662 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1664 u8 dst[ETH_ALEN], src[ETH_ALEN];
1669 u8 buf[2 + HWSIM_PACKETLEN];
1670 struct ether_header *eth;
1675 if (hapd->l2_test == NULL)
1678 /* format: <dst> <src> <tos> */
1681 used = hwaddr_aton2(pos, dst);
1687 used = hwaddr_aton2(pos, src);
1692 val = strtol(pos, NULL, 0);
1693 if (val < 0 || val > 0xff)
1697 eth = (struct ether_header *) &buf[2];
1698 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1699 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1700 eth->ether_type = htons(ETHERTYPE_IP);
1701 ip = (struct iphdr *) (eth + 1);
1702 os_memset(ip, 0, sizeof(*ip));
1707 ip->tot_len = htons(HWSIM_IP_LEN);
1709 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1710 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1711 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
1712 dpos = (u8 *) (ip + 1);
1713 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
1716 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
1717 HWSIM_PACKETLEN) < 0)
1720 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1721 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1727 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1731 struct ether_header *eth;
1732 struct l2_packet_data *l2 = NULL;
1736 const char *ifname = hapd->conf->iface;
1738 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1741 cmd = os_strchr(cmd, ' ');
1747 len = os_strlen(cmd);
1748 if (len & 1 || len < ETH_HLEN * 2)
1752 buf = os_malloc(len);
1756 if (hexstr2bin(cmd, buf, len) < 0)
1759 eth = (struct ether_header *) buf;
1760 ethertype = ntohs(eth->ether_type);
1762 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1763 hostapd_data_test_rx, hapd, 1);
1767 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1768 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1771 l2_packet_deinit(l2);
1774 return res < 0 ? -1 : 0;
1778 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
1780 #ifdef WPA_TRACE_BFD
1783 wpa_trace_fail_after = atoi(cmd);
1784 pos = os_strchr(cmd, ':');
1787 os_strlcpy(wpa_trace_fail_func, pos,
1788 sizeof(wpa_trace_fail_func));
1790 wpa_trace_fail_after = 0;
1794 #else /* WPA_TRACE_BFD */
1796 #endif /* WPA_TRACE_BFD */
1800 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
1801 char *buf, size_t buflen)
1803 #ifdef WPA_TRACE_BFD
1804 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
1805 wpa_trace_fail_func);
1806 #else /* WPA_TRACE_BFD */
1808 #endif /* WPA_TRACE_BFD */
1812 static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
1814 #ifdef WPA_TRACE_BFD
1817 wpa_trace_test_fail_after = atoi(cmd);
1818 pos = os_strchr(cmd, ':');
1821 os_strlcpy(wpa_trace_test_fail_func, pos,
1822 sizeof(wpa_trace_test_fail_func));
1824 wpa_trace_test_fail_after = 0;
1828 #else /* WPA_TRACE_BFD */
1830 #endif /* WPA_TRACE_BFD */
1834 static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
1835 char *buf, size_t buflen)
1837 #ifdef WPA_TRACE_BFD
1838 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
1839 wpa_trace_test_fail_func);
1840 #else /* WPA_TRACE_BFD */
1842 #endif /* WPA_TRACE_BFD */
1845 #endif /* CONFIG_TESTING_OPTIONS */
1848 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
1852 struct csa_settings settings;
1856 ret = hostapd_parse_csa_settings(pos, &settings);
1860 for (i = 0; i < iface->num_bss; i++) {
1861 ret = hostapd_switch_channel(iface->bss[i], &settings);
1863 /* FIX: What do we do if CSA fails in the middle of
1864 * submitting multi-BSS CSA requests? */
1870 #else /* NEED_AP_MLME */
1872 #endif /* NEED_AP_MLME */
1876 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1877 int reply_size, const char *param)
1879 #ifdef RADIUS_SERVER
1880 if (os_strcmp(param, "radius_server") == 0) {
1881 return radius_server_get_mib(hapd->radius_srv, reply,
1884 #endif /* RADIUS_SERVER */
1889 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
1890 char *buf, size_t buflen)
1895 unsigned int vendor_id, subcmd;
1896 struct wpabuf *reply;
1897 size_t data_len = 0;
1899 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1900 vendor_id = strtoul(cmd, &pos, 16);
1901 if (!isblank((unsigned char) *pos))
1904 subcmd = strtoul(pos, &pos, 10);
1907 if (!isblank((unsigned char) *pos++))
1909 data_len = os_strlen(pos);
1914 data = os_malloc(data_len);
1918 if (hexstr2bin(pos, data, data_len)) {
1919 wpa_printf(MSG_DEBUG,
1920 "Vendor command: wrong parameter format");
1926 reply = wpabuf_alloc((buflen - 1) / 2);
1932 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
1936 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
1946 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
1950 struct sta_info *sta;
1952 if (hwaddr_aton(cmd, addr))
1955 sta = ap_get_sta(hapd, addr);
1956 if (!sta || !sta->eapol_sm)
1959 eapol_auth_reauthenticate(sta->eapol_sm);
1964 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
1967 struct sta_info *sta;
1968 char *pos = cmd, *param;
1970 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
1974 pos = os_strchr(pos, ' ');
1979 sta = ap_get_sta(hapd, addr);
1980 if (!sta || !sta->eapol_sm)
1983 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
1987 static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
1988 char *buf, size_t buflen)
1990 char *pos, *end, *stamp;
1993 /* cmd: "LOG_LEVEL [<level>]" */
1997 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
1999 debug_level_str(wpa_debug_level),
2000 wpa_debug_timestamp);
2001 if (os_snprintf_error(end - pos, ret))
2010 stamp = os_strchr(cmd, ' ');
2013 while (*stamp == ' ') {
2018 if (os_strlen(cmd)) {
2019 int level = str_to_debug_level(cmd);
2022 wpa_debug_level = level;
2025 if (stamp && os_strlen(stamp))
2026 wpa_debug_timestamp = atoi(stamp);
2028 os_memcpy(buf, "OK\n", 3);
2034 static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2035 char *buf, size_t buflen)
2037 struct hostapd_iface *iface = hapd->iface;
2039 struct hostapd_sta_info *info;
2040 struct os_reltime now;
2042 sta_track_expire(iface, 0);
2047 os_get_reltime(&now);
2048 dl_list_for_each_reverse(info, &iface->sta_seen,
2049 struct hostapd_sta_info, list) {
2050 struct os_reltime age;
2053 os_reltime_sub(&now, &info->last_seen, &age);
2054 ret = os_snprintf(pos, end - pos, MACSTR " %u\n",
2055 MAC2STR(info->addr), (unsigned int) age.sec);
2056 if (os_snprintf_error(end - pos, ret))
2063 #endif /* NEED_AP_MLME */
2066 static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
2071 if (hwaddr_aton(cmd, addr)) {
2072 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
2076 return hostapd_send_lci_req(hapd, addr);
2080 int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
2083 char *token, *context = NULL;
2084 int random_interval, min_ap;
2085 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
2086 unsigned int n_responders;
2088 token = str_token(cmd, " ", &context);
2089 if (!token || hwaddr_aton(token, addr)) {
2090 wpa_printf(MSG_INFO,
2091 "CTRL: REQ_RANGE - Bad destination address");
2095 token = str_token(cmd, " ", &context);
2099 random_interval = atoi(token);
2100 if (random_interval < 0 || random_interval > 0xffff)
2103 token = str_token(cmd, " ", &context);
2107 min_ap = atoi(token);
2108 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
2112 while ((token = str_token(cmd, " ", &context))) {
2113 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
2114 wpa_printf(MSG_INFO,
2115 "CTRL: REQ_RANGE: Too many responders");
2119 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
2120 wpa_printf(MSG_INFO,
2121 "CTRL: REQ_RANGE: Bad responder address");
2128 if (!n_responders) {
2129 wpa_printf(MSG_INFO,
2130 "CTRL: REQ_RANGE - No FTM responder address");
2134 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
2135 responders, n_responders);
2139 static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
2141 struct wpa_ssid_value ssid;
2143 struct wpabuf *nr, *lci = NULL, *civic = NULL;
2147 if (!(hapd->conf->radio_measurements[0] &
2148 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2149 wpa_printf(MSG_ERROR,
2150 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2154 if (hwaddr_aton(buf, bssid)) {
2155 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
2159 tmp = os_strstr(buf, "ssid=");
2160 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2161 wpa_printf(MSG_ERROR,
2162 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2165 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
2169 tmp = os_strstr(buf, "nr=");
2171 wpa_printf(MSG_ERROR,
2172 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2176 buf = os_strchr(tmp, ' ');
2180 nr = wpabuf_parse_bin(tmp + 3);
2182 wpa_printf(MSG_ERROR,
2183 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2190 tmp = os_strstr(buf, "lci=");
2192 buf = os_strchr(tmp, ' ');
2195 lci = wpabuf_parse_bin(tmp + 4);
2197 wpa_printf(MSG_ERROR,
2198 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
2207 tmp = os_strstr(buf, "civic=");
2209 buf = os_strchr(tmp, ' ');
2212 civic = wpabuf_parse_bin(tmp + 6);
2214 wpa_printf(MSG_ERROR,
2215 "CTRL: SET_NEIGHBOR: Bad civic subelement");
2223 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic);
2233 static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
2236 struct wpa_ssid_value ssid;
2240 if (hwaddr_aton(buf, bssid)) {
2241 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
2245 tmp = os_strstr(buf, "ssid=");
2246 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2247 wpa_printf(MSG_ERROR,
2248 "CTRL: REMOVE_NEIGHBORr: Bad or missing SSID");
2252 return hostapd_neighbor_remove(hapd, bssid, &ssid);
2256 static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
2257 char *buf, char *reply,
2259 struct sockaddr_storage *from,
2264 os_memcpy(reply, "OK\n", 3);
2267 if (os_strcmp(buf, "PING") == 0) {
2268 os_memcpy(reply, "PONG\n", 5);
2270 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2271 if (wpa_debug_reopen_file() < 0)
2273 } else if (os_strcmp(buf, "STATUS") == 0) {
2274 reply_len = hostapd_ctrl_iface_status(hapd, reply,
2276 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
2277 reply_len = hostapd_drv_status(hapd, reply, reply_size);
2278 } else if (os_strcmp(buf, "MIB") == 0) {
2279 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
2280 if (reply_len >= 0) {
2281 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
2282 reply_size - reply_len);
2288 if (reply_len >= 0) {
2289 res = ieee802_1x_get_mib(hapd, reply + reply_len,
2290 reply_size - reply_len);
2296 #ifndef CONFIG_NO_RADIUS
2297 if (reply_len >= 0) {
2298 res = radius_client_get_mib(hapd->radius,
2300 reply_size - reply_len);
2306 #endif /* CONFIG_NO_RADIUS */
2307 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
2308 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
2310 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
2311 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
2313 } else if (os_strncmp(buf, "STA ", 4) == 0) {
2314 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
2316 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
2317 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
2319 } else if (os_strcmp(buf, "ATTACH") == 0) {
2320 if (hostapd_ctrl_iface_attach(hapd, from, fromlen))
2322 } else if (os_strcmp(buf, "DETACH") == 0) {
2323 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
2325 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
2326 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
2329 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
2330 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
2332 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
2333 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
2335 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
2336 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
2338 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
2339 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
2341 } else if (os_strcmp(buf, "STOP_AP") == 0) {
2342 if (hostapd_ctrl_iface_stop_ap(hapd))
2344 #ifdef CONFIG_IEEE80211W
2346 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
2347 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
2349 #endif /* NEED_AP_MLME */
2350 #endif /* CONFIG_IEEE80211W */
2352 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
2353 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
2355 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
2356 reply_len = hostapd_ctrl_iface_wps_check_pin(
2357 hapd, buf + 14, reply, reply_size);
2358 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
2359 if (hostapd_wps_button_pushed(hapd, NULL))
2361 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
2362 if (hostapd_wps_cancel(hapd))
2364 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
2365 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
2367 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
2368 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
2370 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
2371 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
2373 #ifdef CONFIG_WPS_NFC
2374 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
2375 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
2377 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2378 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
2379 hapd, buf + 21, reply, reply_size);
2380 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
2381 reply_len = hostapd_ctrl_iface_wps_nfc_token(
2382 hapd, buf + 14, reply, reply_size);
2383 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2384 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
2385 hapd, buf + 21, reply, reply_size);
2386 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
2387 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
2389 #endif /* CONFIG_WPS_NFC */
2390 #endif /* CONFIG_WPS */
2391 #ifdef CONFIG_INTERWORKING
2392 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
2393 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
2395 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
2396 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
2398 #endif /* CONFIG_INTERWORKING */
2400 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
2401 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
2403 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
2404 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
2406 #endif /* CONFIG_HS20 */
2408 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
2409 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
2411 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
2412 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
2414 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
2415 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
2417 #endif /* CONFIG_WNM */
2418 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
2419 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
2421 } else if (os_strncmp(buf, "SET ", 4) == 0) {
2422 if (hostapd_ctrl_iface_set(hapd, buf + 4))
2424 } else if (os_strncmp(buf, "GET ", 4) == 0) {
2425 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
2427 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
2428 if (hostapd_ctrl_iface_enable(hapd->iface))
2430 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
2431 if (hostapd_ctrl_iface_reload(hapd->iface))
2433 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
2434 if (hostapd_ctrl_iface_disable(hapd->iface))
2436 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
2437 if (ieee802_11_set_beacon(hapd))
2439 #ifdef CONFIG_TESTING_OPTIONS
2440 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
2441 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
2443 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
2444 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
2446 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
2447 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
2449 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
2450 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
2452 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
2453 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
2455 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
2456 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
2458 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
2459 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
2461 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
2462 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
2464 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
2465 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
2467 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
2468 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
2469 #endif /* CONFIG_TESTING_OPTIONS */
2470 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
2471 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
2473 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
2474 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
2476 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
2477 ieee802_1x_erp_flush(hapd);
2478 #ifdef RADIUS_SERVER
2479 radius_server_erp_flush(hapd->radius_srv);
2480 #endif /* RADIUS_SERVER */
2481 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
2482 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
2484 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
2485 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
2487 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
2488 reply_len = hostapd_ctrl_iface_log_level(
2489 hapd, buf + 9, reply, reply_size);
2491 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
2492 reply_len = hostapd_ctrl_iface_track_sta_list(
2493 hapd, reply, reply_size);
2494 #endif /* NEED_AP_MLME */
2495 } else if (os_strcmp(buf, "PMKSA") == 0) {
2496 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
2498 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
2499 hostapd_ctrl_iface_pmksa_flush(hapd);
2500 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
2501 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
2503 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
2504 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
2506 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
2507 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
2509 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
2510 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
2513 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2517 if (reply_len < 0) {
2518 os_memcpy(reply, "FAIL\n", 5);
2526 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
2529 struct hostapd_data *hapd = eloop_ctx;
2532 struct sockaddr_storage from;
2533 socklen_t fromlen = sizeof(from);
2534 char *reply, *pos = buf;
2535 const int reply_size = 4096;
2537 int level = MSG_DEBUG;
2538 #ifdef CONFIG_CTRL_IFACE_UDP
2539 unsigned char lcookie[COOKIE_LEN];
2540 #endif /* CONFIG_CTRL_IFACE_UDP */
2542 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2543 (struct sockaddr *) &from, &fromlen);
2545 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2551 reply = os_malloc(reply_size);
2552 if (reply == NULL) {
2553 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
2555 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2561 #ifdef CONFIG_CTRL_IFACE_UDP
2562 if (os_strcmp(buf, "GET_COOKIE") == 0) {
2563 os_memcpy(reply, "COOKIE=", 7);
2564 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
2565 cookie, COOKIE_LEN);
2566 reply_len = 7 + 2 * COOKIE_LEN;
2570 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
2571 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
2572 wpa_printf(MSG_DEBUG,
2573 "CTRL: No cookie in the request - drop request");
2578 if (os_memcmp(cookie, lcookie, COOKIE_LEN) != 0) {
2579 wpa_printf(MSG_DEBUG,
2580 "CTRL: Invalid cookie in the request - drop request");
2585 pos = buf + 7 + 2 * COOKIE_LEN;
2588 #endif /* CONFIG_CTRL_IFACE_UDP */
2590 if (os_strcmp(pos, "PING") == 0)
2591 level = MSG_EXCESSIVE;
2592 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
2594 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
2598 #ifdef CONFIG_CTRL_IFACE_UDP
2600 #endif /* CONFIG_CTRL_IFACE_UDP */
2601 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2603 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2610 #ifndef CONFIG_CTRL_IFACE_UDP
2611 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
2616 if (hapd->conf->ctrl_interface == NULL)
2619 len = os_strlen(hapd->conf->ctrl_interface) +
2620 os_strlen(hapd->conf->iface) + 2;
2621 buf = os_malloc(len);
2625 os_snprintf(buf, len, "%s/%s",
2626 hapd->conf->ctrl_interface, hapd->conf->iface);
2627 buf[len - 1] = '\0';
2630 #endif /* CONFIG_CTRL_IFACE_UDP */
2633 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
2634 enum wpa_msg_type type,
2635 const char *txt, size_t len)
2637 struct hostapd_data *hapd = ctx;
2640 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
2644 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
2646 #ifdef CONFIG_CTRL_IFACE_UDP
2647 int port = HOSTAPD_CTRL_IFACE_PORT;
2649 char port_str[40], *tmp;
2651 struct addrinfo hints = { 0 }, *res, *saveres;
2654 if (hapd->ctrl_sock > -1) {
2655 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2659 if (hapd->conf->ctrl_interface == NULL)
2662 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
2667 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
2672 dl_list_init(&hapd->ctrl_dst);
2673 hapd->ctrl_sock = -1;
2674 os_get_random(cookie, COOKIE_LEN);
2676 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
2677 hints.ai_flags = AI_PASSIVE;
2678 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
2680 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
2681 hints.ai_family = AF_INET6;
2682 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
2683 hints.ai_family = AF_INET;
2684 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
2685 hints.ai_socktype = SOCK_DGRAM;
2688 os_snprintf(p, sizeof(p), "%d", port);
2689 n = getaddrinfo(NULL, p, &hints, &res);
2691 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
2696 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
2698 if (hapd->ctrl_sock < 0) {
2699 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
2703 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
2705 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
2706 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
2708 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
2712 freeaddrinfo(saveres);
2714 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
2715 tmp = os_strdup(port_str);
2717 os_free(hapd->conf->ctrl_interface);
2718 hapd->conf->ctrl_interface = tmp;
2720 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
2722 if (eloop_register_read_sock(hapd->ctrl_sock,
2723 hostapd_ctrl_iface_receive, hapd, NULL) <
2725 hostapd_ctrl_iface_deinit(hapd);
2729 hapd->msg_ctx = hapd;
2730 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
2735 if (hapd->ctrl_sock >= 0)
2736 close(hapd->ctrl_sock);
2738 #else /* CONFIG_CTRL_IFACE_UDP */
2739 struct sockaddr_un addr;
2743 if (hapd->ctrl_sock > -1) {
2744 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2748 dl_list_init(&hapd->ctrl_dst);
2750 if (hapd->conf->ctrl_interface == NULL)
2753 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2754 if (errno == EEXIST) {
2755 wpa_printf(MSG_DEBUG, "Using existing control "
2756 "interface directory.");
2758 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2764 if (hapd->conf->ctrl_interface_gid_set &&
2765 chown(hapd->conf->ctrl_interface, -1,
2766 hapd->conf->ctrl_interface_gid) < 0) {
2767 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2772 if (!hapd->conf->ctrl_interface_gid_set &&
2773 hapd->iface->interfaces->ctrl_iface_group &&
2774 chown(hapd->conf->ctrl_interface, -1,
2775 hapd->iface->interfaces->ctrl_iface_group) < 0) {
2776 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2783 * Android is using umask 0077 which would leave the control interface
2784 * directory without group access. This breaks things since Wi-Fi
2785 * framework assumes that this directory can be accessed by other
2786 * applications in the wifi group. Fix this by adding group access even
2787 * if umask value would prevent this.
2789 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2790 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
2792 /* Try to continue anyway */
2794 #endif /* ANDROID */
2796 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
2797 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
2800 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2802 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
2806 os_memset(&addr, 0, sizeof(addr));
2808 addr.sun_len = sizeof(addr);
2809 #endif /* __FreeBSD__ */
2810 addr.sun_family = AF_UNIX;
2811 fname = hostapd_ctrl_iface_path(hapd);
2814 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2815 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2816 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2818 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2819 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2820 " allow connections - assuming it was left"
2821 "over from forced program termination");
2822 if (unlink(fname) < 0) {
2823 wpa_printf(MSG_ERROR,
2824 "Could not unlink existing ctrl_iface socket '%s': %s",
2825 fname, strerror(errno));
2828 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2830 wpa_printf(MSG_ERROR,
2831 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
2835 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2836 "ctrl_iface socket '%s'", fname);
2838 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2839 "be in use - cannot override it");
2840 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2841 "not used anymore", fname);
2848 if (hapd->conf->ctrl_interface_gid_set &&
2849 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
2850 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2855 if (!hapd->conf->ctrl_interface_gid_set &&
2856 hapd->iface->interfaces->ctrl_iface_group &&
2857 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
2858 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2863 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2864 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2870 hapd->ctrl_sock = s;
2871 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
2873 hostapd_ctrl_iface_deinit(hapd);
2876 hapd->msg_ctx = hapd;
2877 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
2889 #endif /* CONFIG_CTRL_IFACE_UDP */
2893 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
2895 struct wpa_ctrl_dst *dst, *prev;
2897 if (hapd->ctrl_sock > -1) {
2898 #ifndef CONFIG_CTRL_IFACE_UDP
2900 #endif /* !CONFIG_CTRL_IFACE_UDP */
2902 eloop_unregister_read_sock(hapd->ctrl_sock);
2903 close(hapd->ctrl_sock);
2904 hapd->ctrl_sock = -1;
2905 #ifndef CONFIG_CTRL_IFACE_UDP
2906 fname = hostapd_ctrl_iface_path(hapd);
2911 if (hapd->conf->ctrl_interface &&
2912 rmdir(hapd->conf->ctrl_interface) < 0) {
2913 if (errno == ENOTEMPTY) {
2914 wpa_printf(MSG_DEBUG, "Control interface "
2915 "directory not empty - leaving it "
2918 wpa_printf(MSG_ERROR,
2919 "rmdir[ctrl_interface=%s]: %s",
2920 hapd->conf->ctrl_interface,
2924 #endif /* !CONFIG_CTRL_IFACE_UDP */
2927 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
2931 #ifdef CONFIG_TESTING_OPTIONS
2932 l2_packet_deinit(hapd->l2_test);
2933 hapd->l2_test = NULL;
2934 #endif /* CONFIG_TESTING_OPTIONS */
2938 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
2941 if (hostapd_add_iface(interfaces, buf) < 0) {
2942 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
2949 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
2952 if (hostapd_remove_iface(interfaces, buf) < 0) {
2953 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
2960 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
2961 struct sockaddr_storage *from,
2964 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen);
2968 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
2969 struct sockaddr_storage *from,
2972 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
2976 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
2978 #ifdef CONFIG_WPS_TESTING
2979 wps_version_number = 0x20;
2980 wps_testing_dummy_cred = 0;
2981 wps_corrupt_pkhash = 0;
2982 #endif /* CONFIG_WPS_TESTING */
2989 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
2992 char ifname[IFNAMSIZ + 1];
2993 struct fst_iface_cfg cfg;
2994 struct hostapd_data *hapd;
2995 struct fst_wpa_obj iface_obj;
2997 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
2998 hapd = hostapd_get_iface(interfaces, ifname);
3000 if (hapd->iface->fst) {
3001 wpa_printf(MSG_INFO, "FST: Already attached");
3004 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
3005 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
3007 if (hapd->iface->fst)
3017 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
3020 char ifname[IFNAMSIZ + 1];
3021 struct hostapd_data * hapd;
3023 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
3024 hapd = hostapd_get_iface(interfaces, ifname);
3026 if (!fst_iface_detach(ifname)) {
3027 hapd->iface->fst = NULL;
3028 hapd->iface->fst_ies = NULL;
3037 #endif /* CONFIG_FST */
3040 static struct hostapd_data *
3041 hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
3046 for (i = 0; i < interfaces->count; i++) {
3047 struct hostapd_iface *iface = interfaces->iface[i];
3049 for (j = 0; j < iface->num_bss; j++) {
3050 struct hostapd_data *hapd;
3052 hapd = iface->bss[j];
3053 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3062 static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
3063 struct hostapd_data *dst_hapd,
3069 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
3071 wpa_printf(MSG_ERROR,
3072 "DUP: cannot allocate buffer to stringify %s",
3077 if (os_strcmp(param, "wpa") == 0) {
3078 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
3079 src_hapd->conf->wpa);
3080 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
3081 src_hapd->conf->wpa_key_mgmt) {
3082 res = hostapd_ctrl_iface_get_key_mgmt(
3083 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
3084 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
3085 goto error_stringify;
3086 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
3087 src_hapd->conf->wpa_pairwise) {
3088 res = wpa_write_ciphers(value,
3089 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3090 src_hapd->conf->wpa_pairwise, " ");
3092 goto error_stringify;
3093 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
3094 src_hapd->conf->rsn_pairwise) {
3095 res = wpa_write_ciphers(value,
3096 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3097 src_hapd->conf->rsn_pairwise, " ");
3099 goto error_stringify;
3100 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
3101 src_hapd->conf->ssid.wpa_passphrase) {
3102 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
3103 src_hapd->conf->ssid.wpa_passphrase);
3104 } else if (os_strcmp(param, "wpa_psk") == 0 &&
3105 src_hapd->conf->ssid.wpa_psk_set) {
3106 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3107 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
3109 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
3113 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
3118 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
3126 hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
3128 char *reply, int reply_size)
3133 struct hostapd_iface *iface;
3137 show_ctrl = !!os_strstr(input, "ctrl");
3140 end = reply + reply_size;
3142 for (i = 0; i < interfaces->count; i++) {
3143 iface = interfaces->iface[i];
3145 for (j = 0; j < iface->num_bss; j++) {
3146 struct hostapd_bss_config *conf;
3148 conf = iface->conf->bss[j];
3150 res = os_snprintf(pos, end - pos,
3151 "%s ctrl_iface=%s\n",
3153 conf->ctrl_interface ?
3154 conf->ctrl_interface : "N/A");
3156 res = os_snprintf(pos, end - pos, "%s\n",
3158 if (os_snprintf_error(end - pos, res)) {
3171 hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
3174 char *p_start = cmd, *p_end;
3175 struct hostapd_data *src_hapd, *dst_hapd;
3177 /* cmd: "<src ifname> <dst ifname> <variable name> */
3179 p_end = os_strchr(p_start, ' ');
3181 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
3187 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
3189 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
3194 p_start = p_end + 1;
3195 p_end = os_strchr(p_start, ' ');
3197 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
3203 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
3205 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
3210 p_start = p_end + 1;
3211 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
3215 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
3217 char *buf, char *reply,
3219 struct sockaddr_storage *from,
3222 struct hostapd_data *hapd;
3224 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
3228 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
3229 if (os_snprintf_error(reply_size, res))
3234 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
3239 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
3242 void *interfaces = eloop_ctx;
3243 char buffer[256], *buf = buffer;
3245 struct sockaddr_storage from;
3246 socklen_t fromlen = sizeof(from);
3249 const int reply_size = 4096;
3250 #ifdef CONFIG_CTRL_IFACE_UDP
3251 unsigned char lcookie[COOKIE_LEN];
3252 #endif /* CONFIG_CTRL_IFACE_UDP */
3254 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
3255 (struct sockaddr *) &from, &fromlen);
3257 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3262 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
3264 reply = os_malloc(reply_size);
3265 if (reply == NULL) {
3266 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3268 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3274 os_memcpy(reply, "OK\n", 3);
3277 #ifdef CONFIG_CTRL_IFACE_UDP
3278 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3279 os_memcpy(reply, "COOKIE=", 7);
3280 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
3281 gcookie, COOKIE_LEN);
3282 reply_len = 7 + 2 * COOKIE_LEN;
3286 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
3287 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
3288 wpa_printf(MSG_DEBUG,
3289 "CTRL: No cookie in the request - drop request");
3294 if (os_memcmp(gcookie, lcookie, COOKIE_LEN) != 0) {
3295 wpa_printf(MSG_DEBUG,
3296 "CTRL: Invalid cookie in the request - drop request");
3301 buf += 7 + 2 * COOKIE_LEN;
3304 #endif /* CONFIG_CTRL_IFACE_UDP */
3306 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
3307 char *pos = os_strchr(buf + 7, ' ');
3311 reply_len = hostapd_global_ctrl_iface_ifname(
3312 interfaces, buf + 7, pos, reply, reply_size,
3318 if (os_strcmp(buf, "PING") == 0) {
3319 os_memcpy(reply, "PONG\n", 5);
3321 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3322 if (wpa_debug_reopen_file() < 0)
3324 } else if (os_strcmp(buf, "FLUSH") == 0) {
3325 hostapd_ctrl_iface_flush(interfaces);
3326 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
3327 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
3329 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
3330 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
3332 } else if (os_strcmp(buf, "ATTACH") == 0) {
3333 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
3336 } else if (os_strcmp(buf, "DETACH") == 0) {
3337 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
3340 #ifdef CONFIG_MODULE_TESTS
3341 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
3342 int hapd_module_tests(void);
3343 if (hapd_module_tests() < 0)
3345 #endif /* CONFIG_MODULE_TESTS */
3347 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
3348 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
3349 reply_len = os_snprintf(reply, reply_size, "OK\n");
3352 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
3353 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
3354 reply_len = os_snprintf(reply, reply_size, "OK\n");
3357 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
3358 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
3359 #endif /* CONFIG_FST */
3360 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
3361 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
3363 reply_len = os_snprintf(reply, reply_size, "OK\n");
3366 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
3367 reply_len = hostapd_global_ctrl_iface_interfaces(
3368 interfaces, buf + 10, reply, sizeof(buffer));
3369 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3372 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
3378 if (reply_len < 0) {
3379 os_memcpy(reply, "FAIL\n", 5);
3383 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3385 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3392 #ifndef CONFIG_CTRL_IFACE_UDP
3393 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
3398 if (interface->global_iface_path == NULL)
3401 len = os_strlen(interface->global_iface_path) +
3402 os_strlen(interface->global_iface_name) + 2;
3403 buf = os_malloc(len);
3407 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
3408 interface->global_iface_name);
3409 buf[len - 1] = '\0';
3412 #endif /* CONFIG_CTRL_IFACE_UDP */
3415 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
3417 #ifdef CONFIG_CTRL_IFACE_UDP
3418 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
3421 struct addrinfo hints = { 0 }, *res, *saveres;
3424 if (interface->global_ctrl_sock > -1) {
3425 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3429 if (interface->global_iface_path == NULL)
3432 pos = os_strstr(interface->global_iface_path, "udp:");
3437 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
3442 dl_list_init(&interface->global_ctrl_dst);
3443 interface->global_ctrl_sock = -1;
3444 os_get_random(gcookie, COOKIE_LEN);
3446 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3447 hints.ai_flags = AI_PASSIVE;
3448 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3450 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3451 hints.ai_family = AF_INET6;
3452 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3453 hints.ai_family = AF_INET;
3454 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3455 hints.ai_socktype = SOCK_DGRAM;
3458 os_snprintf(p, sizeof(p), "%d", port);
3459 n = getaddrinfo(NULL, p, &hints, &res);
3461 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3466 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
3468 if (interface->global_ctrl_sock < 0) {
3469 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3473 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
3476 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
3477 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
3479 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
3483 freeaddrinfo(saveres);
3485 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
3487 if (eloop_register_read_sock(interface->global_ctrl_sock,
3488 hostapd_global_ctrl_iface_receive,
3489 interface, NULL) < 0) {
3490 hostapd_global_ctrl_iface_deinit(interface);
3497 if (interface->global_ctrl_sock >= 0)
3498 close(interface->global_ctrl_sock);
3500 #else /* CONFIG_CTRL_IFACE_UDP */
3501 struct sockaddr_un addr;
3505 if (interface->global_iface_path == NULL) {
3506 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
3510 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
3511 if (errno == EEXIST) {
3512 wpa_printf(MSG_DEBUG, "Using existing control "
3513 "interface directory.");
3515 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3519 } else if (interface->ctrl_iface_group &&
3520 chown(interface->global_iface_path, -1,
3521 interface->ctrl_iface_group) < 0) {
3522 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3527 if (os_strlen(interface->global_iface_path) + 1 +
3528 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
3531 s = socket(PF_UNIX, SOCK_DGRAM, 0);
3533 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
3537 os_memset(&addr, 0, sizeof(addr));
3539 addr.sun_len = sizeof(addr);
3540 #endif /* __FreeBSD__ */
3541 addr.sun_family = AF_UNIX;
3542 fname = hostapd_global_ctrl_iface_path(interface);
3545 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
3546 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3547 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
3549 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3550 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
3551 " allow connections - assuming it was left"
3552 "over from forced program termination");
3553 if (unlink(fname) < 0) {
3554 wpa_printf(MSG_ERROR,
3555 "Could not unlink existing ctrl_iface socket '%s': %s",
3556 fname, strerror(errno));
3559 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
3561 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
3565 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
3566 "ctrl_iface socket '%s'", fname);
3568 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
3569 "be in use - cannot override it");
3570 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
3571 "not used anymore", fname);
3578 if (interface->ctrl_iface_group &&
3579 chown(fname, -1, interface->ctrl_iface_group) < 0) {
3580 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3585 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
3586 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
3592 interface->global_ctrl_sock = s;
3593 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
3606 #endif /* CONFIG_CTRL_IFACE_UDP */
3610 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
3612 #ifndef CONFIG_CTRL_IFACE_UDP
3614 #endif /* CONFIG_CTRL_IFACE_UDP */
3615 struct wpa_ctrl_dst *dst, *prev;
3617 if (interfaces->global_ctrl_sock > -1) {
3618 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
3619 close(interfaces->global_ctrl_sock);
3620 interfaces->global_ctrl_sock = -1;
3621 #ifndef CONFIG_CTRL_IFACE_UDP
3622 fname = hostapd_global_ctrl_iface_path(interfaces);
3628 if (interfaces->global_iface_path &&
3629 rmdir(interfaces->global_iface_path) < 0) {
3630 if (errno == ENOTEMPTY) {
3631 wpa_printf(MSG_DEBUG, "Control interface "
3632 "directory not empty - leaving it "
3635 wpa_printf(MSG_ERROR,
3636 "rmdir[ctrl_interface=%s]: %s",
3637 interfaces->global_iface_path,
3641 #endif /* CONFIG_CTRL_IFACE_UDP */
3644 os_free(interfaces->global_iface_path);
3645 interfaces->global_iface_path = NULL;
3647 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
3648 struct wpa_ctrl_dst, list)
3653 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
3654 enum wpa_msg_type type,
3655 const char *buf, size_t len)
3657 struct wpa_ctrl_dst *dst, *next;
3658 struct dl_list *ctrl_dst;
3665 if (type != WPA_MSG_ONLY_GLOBAL) {
3666 s = hapd->ctrl_sock;
3667 ctrl_dst = &hapd->ctrl_dst;
3669 s = hapd->iface->interfaces->global_ctrl_sock;
3670 ctrl_dst = &hapd->iface->interfaces->global_ctrl_dst;
3673 if (s < 0 || dl_list_empty(ctrl_dst))
3676 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
3677 io[0].iov_base = levelstr;
3678 io[0].iov_len = os_strlen(levelstr);
3679 io[1].iov_base = (char *) buf;
3680 io[1].iov_len = len;
3681 os_memset(&msg, 0, sizeof(msg));
3686 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
3687 if (level >= dst->debug_level) {
3688 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
3689 &dst->addr, dst->addrlen);
3690 msg.msg_name = &dst->addr;
3691 msg.msg_namelen = dst->addrlen;
3692 if (sendmsg(s, &msg, 0) < 0) {
3694 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
3696 idx, errno, strerror(errno));
3698 if (dst->errors > 10 || _errno == ENOENT) {
3699 if (type != WPA_MSG_ONLY_GLOBAL)
3700 hostapd_ctrl_iface_detach(
3704 hostapd_global_ctrl_iface_detach(
3705 hapd->iface->interfaces,
3716 #endif /* CONFIG_NATIVE_WINDOWS */