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"
51 #include "wps/wps_defs.h"
53 #include "fst/fst_ctrl_iface.h"
54 #include "config_file.h"
55 #include "ctrl_iface.h"
58 #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
60 #ifdef CONFIG_CTRL_IFACE_UDP
62 static unsigned char cookie[COOKIE_LEN];
63 static unsigned char gcookie[COOKIE_LEN];
64 #define HOSTAPD_CTRL_IFACE_PORT 8877
65 #define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
66 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
67 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
68 #endif /* CONFIG_CTRL_IFACE_UDP */
70 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
71 enum wpa_msg_type type,
72 const char *buf, size_t len);
75 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
76 struct sockaddr_storage *from,
79 return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen);
83 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
84 struct sockaddr_storage *from,
87 return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
91 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
92 struct sockaddr_storage *from,
96 return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
100 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
104 struct sta_info *sta;
106 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
108 if (hwaddr_aton(txtaddr, addr))
111 sta = ap_get_sta(hapd, addr);
115 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
116 "notification", MAC2STR(addr));
117 sta = ap_sta_add(hapd, addr);
121 hostapd_new_assoc_sta(hapd, sta, 0);
126 #ifdef CONFIG_IEEE80211W
128 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
132 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
134 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
136 if (hwaddr_aton(txtaddr, addr) ||
137 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
140 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
144 #endif /* NEED_AP_MLME */
145 #endif /* CONFIG_IEEE80211W */
149 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
151 char *pin = os_strchr(txt, ' ');
154 u8 addr_buf[ETH_ALEN], *addr = NULL;
161 timeout_txt = os_strchr(pin, ' ');
163 *timeout_txt++ = '\0';
164 timeout = atoi(timeout_txt);
165 pos = os_strchr(timeout_txt, ' ');
168 if (hwaddr_aton(pos, addr_buf) == 0)
174 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
178 static int hostapd_ctrl_iface_wps_check_pin(
179 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
186 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
187 (u8 *) cmd, os_strlen(cmd));
188 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
189 if (*pos < '0' || *pos > '9')
193 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
197 if (len != 4 && len != 8) {
198 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
204 unsigned int pin_val;
206 if (!wps_pin_valid(pin_val)) {
207 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
208 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
209 if (os_snprintf_error(buflen, ret))
215 ret = os_snprintf(buf, buflen, "%s", pin);
216 if (os_snprintf_error(buflen, ret))
223 #ifdef CONFIG_WPS_NFC
224 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
231 len = os_strlen(pos);
236 buf = wpabuf_alloc(len);
239 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
244 ret = hostapd_wps_nfc_tag_read(hapd, buf);
251 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
252 char *cmd, char *reply,
259 if (os_strcmp(cmd, "WPS") == 0)
261 else if (os_strcmp(cmd, "NDEF") == 0)
266 buf = hostapd_wps_nfc_config_token(hapd, ndef);
270 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
281 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
282 char *reply, size_t max_len,
288 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
292 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
303 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
304 char *cmd, char *reply,
307 if (os_strcmp(cmd, "WPS") == 0)
308 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
311 if (os_strcmp(cmd, "NDEF") == 0)
312 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
315 if (os_strcmp(cmd, "enable") == 0)
316 return hostapd_wps_nfc_token_enable(hapd);
318 if (os_strcmp(cmd, "disable") == 0) {
319 hostapd_wps_nfc_token_disable(hapd);
327 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
328 char *cmd, char *reply,
336 pos = os_strchr(cmd, ' ');
341 if (os_strcmp(cmd, "WPS") == 0)
343 else if (os_strcmp(cmd, "NDEF") == 0)
348 if (os_strcmp(pos, "WPS-CR") == 0)
349 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
355 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
366 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
370 struct wpabuf *req, *sel;
372 char *pos, *role, *type, *pos2;
375 pos = os_strchr(role, ' ');
381 pos = os_strchr(type, ' ');
386 pos2 = os_strchr(pos, ' ');
391 len = os_strlen(pos);
396 req = wpabuf_alloc(len);
399 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
404 len = os_strlen(pos2);
411 sel = wpabuf_alloc(len);
416 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
422 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
423 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
425 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
426 "reported: role=%s type=%s", role, type);
435 #endif /* CONFIG_WPS_NFC */
438 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
439 char *buf, size_t buflen)
445 pos = os_strchr(txt, ' ');
449 if (os_strcmp(txt, "disable") == 0) {
450 hostapd_wps_ap_pin_disable(hapd);
451 return os_snprintf(buf, buflen, "OK\n");
454 if (os_strcmp(txt, "random") == 0) {
457 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
460 return os_snprintf(buf, buflen, "%s", pin_txt);
463 if (os_strcmp(txt, "get") == 0) {
464 pin_txt = hostapd_wps_ap_pin_get(hapd);
467 return os_snprintf(buf, buflen, "%s", pin_txt);
470 if (os_strcmp(txt, "set") == 0) {
475 pos = os_strchr(pos, ' ');
480 if (os_strlen(pin) > buflen)
482 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
484 return os_snprintf(buf, buflen, "%s", pin);
491 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
494 char *ssid, *auth, *encr = NULL, *key = NULL;
497 pos = os_strchr(txt, ' ');
503 pos = os_strchr(pos, ' ');
507 pos = os_strchr(pos, ' ');
514 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
518 static const char * pbc_status_str(enum pbc_status status)
521 case WPS_PBC_STATUS_DISABLE:
523 case WPS_PBC_STATUS_ACTIVE:
525 case WPS_PBC_STATUS_TIMEOUT:
527 case WPS_PBC_STATUS_OVERLAP:
535 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
536 char *buf, size_t buflen)
544 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
545 pbc_status_str(hapd->wps_stats.pbc_status));
547 if (os_snprintf_error(end - pos, ret))
551 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
552 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
554 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
555 "Failed" : "None")));
557 if (os_snprintf_error(end - pos, ret))
561 /* If status == Failure - Add possible Reasons */
562 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
563 hapd->wps_stats.failure_reason > 0) {
564 ret = os_snprintf(pos, end - pos,
565 "Failure Reason: %s\n",
566 wps_ei_str(hapd->wps_stats.failure_reason));
568 if (os_snprintf_error(end - pos, ret))
573 if (hapd->wps_stats.status) {
574 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
575 MAC2STR(hapd->wps_stats.peer_addr));
577 if (os_snprintf_error(end - pos, ret))
585 #endif /* CONFIG_WPS */
589 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
595 if (hwaddr_aton(cmd, addr))
608 return hs20_send_wnm_notification(hapd, addr, 1, url);
612 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
616 int code, reauth_delay, ret;
621 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
622 if (hwaddr_aton(cmd, addr))
625 pos = os_strchr(cmd, ' ');
631 pos = os_strchr(pos, ' ');
635 reauth_delay = atoi(pos);
638 pos = os_strchr(pos, ' ');
641 url_len = os_strlen(pos);
644 req = wpabuf_alloc(4 + url_len);
647 wpabuf_put_u8(req, code);
648 wpabuf_put_le16(req, reauth_delay);
649 wpabuf_put_u8(req, url_len);
651 wpabuf_put_data(req, pos, url_len);
653 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
654 " to indicate imminent deauthentication (code=%d "
655 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
656 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
661 #endif /* CONFIG_HS20 */
664 #ifdef CONFIG_INTERWORKING
666 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
669 u8 qos_map_set[16 + 2 * 21], count = 0;
670 const char *pos = cmd;
674 if (count == sizeof(qos_map_set)) {
675 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
680 if (val < 0 || val > 255) {
681 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
685 qos_map_set[count++] = val;
686 pos = os_strchr(pos, ',');
692 if (count < 16 || count & 1) {
693 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
697 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
699 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
703 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
704 hapd->conf->qos_map_set_len = count;
710 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
714 struct sta_info *sta;
716 u8 *qos_map_set = hapd->conf->qos_map_set;
717 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
720 if (!qos_map_set_len) {
721 wpa_printf(MSG_INFO, "QoS Map Set is not set");
725 if (hwaddr_aton(cmd, addr))
728 sta = ap_get_sta(hapd, addr);
730 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
731 "for QoS Map Configuration message",
736 if (!sta->qos_map_enabled) {
737 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
738 "support for QoS Map", MAC2STR(addr));
742 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
746 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
747 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
749 /* QoS Map Set Element */
750 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
751 wpabuf_put_u8(buf, qos_map_set_len);
752 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
754 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
755 wpabuf_head(buf), wpabuf_len(buf));
761 #endif /* CONFIG_INTERWORKING */
766 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
771 struct sta_info *sta;
773 if (hwaddr_aton(cmd, addr))
777 disassoc_timer = atoi(cmd + 17);
779 sta = ap_get_sta(hapd, addr);
781 wpa_printf(MSG_DEBUG, "Station " MACSTR
782 " not found for disassociation imminent message",
787 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
791 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
795 const char *url, *timerstr;
797 struct sta_info *sta;
799 if (hwaddr_aton(cmd, addr))
802 sta = ap_get_sta(hapd, addr);
804 wpa_printf(MSG_DEBUG, "Station " MACSTR
805 " not found for ESS disassociation imminent message",
811 if (*timerstr != ' ')
814 disassoc_timer = atoi(timerstr);
815 if (disassoc_timer < 0 || disassoc_timer > 65535)
818 url = os_strchr(timerstr, ' ');
823 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
827 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
831 const char *pos, *end;
832 int disassoc_timer = 0;
833 struct sta_info *sta;
834 u8 req_mode = 0, valid_int = 0x01;
839 u8 *nei_pos = nei_rep;
843 if (hwaddr_aton(cmd, addr)) {
844 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
848 sta = ap_get_sta(hapd, addr);
850 wpa_printf(MSG_DEBUG, "Station " MACSTR
851 " not found for BSS TM Request message",
856 pos = os_strstr(cmd, " disassoc_timer=");
859 disassoc_timer = atoi(pos);
860 if (disassoc_timer < 0 || disassoc_timer > 65535) {
861 wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
866 pos = os_strstr(cmd, " valid_int=");
869 valid_int = atoi(pos);
872 pos = os_strstr(cmd, " bss_term=");
875 req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
876 /* TODO: TSF configurable/learnable */
877 bss_term_dur[0] = 4; /* Subelement ID */
878 bss_term_dur[1] = 10; /* Length */
879 os_memset(bss_term_dur, 2, 8);
880 end = os_strchr(pos, ',');
882 wpa_printf(MSG_DEBUG, "Invalid bss_term data");
886 WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
891 * BSS Transition Candidate List Entries - Neighbor Report elements
892 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
893 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
901 pos = os_strstr(pos, " neighbor=");
904 if (nei_pos + 15 > nei_rep + sizeof(nei_rep)) {
905 wpa_printf(MSG_DEBUG,
906 "Not enough room for additional neighbor");
912 *nei_pos++ = WLAN_EID_NEIGHBOR_REPORT;
913 nei_pos++; /* length to be filled in */
915 if (hwaddr_aton(pos, nei_pos)) {
916 wpa_printf(MSG_DEBUG, "Invalid BSSID");
922 wpa_printf(MSG_DEBUG, "Missing BSSID Information");
927 val = strtol(pos, &endptr, 0);
928 WPA_PUT_LE32(nei_pos, val);
930 if (*endptr != ',') {
931 wpa_printf(MSG_DEBUG, "Missing Operating Class");
936 *nei_pos++ = atoi(pos); /* Operating Class */
937 pos = os_strchr(pos, ',');
939 wpa_printf(MSG_DEBUG, "Missing Channel Number");
944 *nei_pos++ = atoi(pos); /* Channel Number */
945 pos = os_strchr(pos, ',');
947 wpa_printf(MSG_DEBUG, "Missing PHY Type");
952 *nei_pos++ = atoi(pos); /* PHY Type */
953 end = os_strchr(pos, ' ');
954 tmp = os_strchr(pos, ',');
955 if (tmp && (!end || tmp < end)) {
956 /* Optional Subelements (hexdump) */
960 end = os_strchr(pos, ' ');
964 len = os_strlen(pos);
965 if (nei_pos + len / 2 > nei_rep + sizeof(nei_rep)) {
966 wpa_printf(MSG_DEBUG,
967 "Not enough room for neighbor subelements");
971 hexstr2bin(pos, nei_pos, len / 2) < 0) {
972 wpa_printf(MSG_DEBUG,
973 "Invalid neighbor subelement info");
980 nei_start[1] = nei_pos - nei_start - 2;
983 pos = os_strstr(cmd, " url=");
987 end = os_strchr(pos, ' ');
991 len = os_strlen(pos);
992 url = os_malloc(len + 1);
995 os_memcpy(url, pos, len);
997 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
1000 if (os_strstr(cmd, " pref=1"))
1001 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1002 if (os_strstr(cmd, " abridged=1"))
1003 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1004 if (os_strstr(cmd, " disassoc_imminent=1"))
1005 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1008 pos = os_strstr(cmd, "mbo=");
1010 unsigned int mbo_reason, cell_pref, reassoc_delay;
1013 ret = sscanf(pos, "mbo=%u:%u:%u", &mbo_reason,
1014 &reassoc_delay, &cell_pref);
1016 wpa_printf(MSG_DEBUG,
1017 "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
1021 if (mbo_reason > MBO_TRANSITION_REASON_PREMIUM_AP) {
1022 wpa_printf(MSG_DEBUG,
1023 "Invalid MBO transition reason code %u",
1028 /* Valid values for Cellular preference are: 0, 1, 255 */
1029 if (cell_pref != 0 && cell_pref != 1 && cell_pref != 255) {
1030 wpa_printf(MSG_DEBUG,
1031 "Invalid MBO cellular capability %u",
1036 if (reassoc_delay > 65535 ||
1038 !(req_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT))) {
1039 wpa_printf(MSG_DEBUG,
1040 "MBO: Assoc retry delay is only valid in disassoc imminent mode");
1044 *mbo_pos++ = MBO_ATTR_ID_TRANSITION_REASON;
1046 *mbo_pos++ = mbo_reason;
1047 *mbo_pos++ = MBO_ATTR_ID_CELL_DATA_PREF;
1049 *mbo_pos++ = cell_pref;
1051 if (reassoc_delay) {
1052 *mbo_pos++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY;
1054 WPA_PUT_LE16(mbo_pos, reassoc_delay);
1058 mbo_len = mbo_pos - mbo;
1060 #endif /* CONFIG_MBO */
1062 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
1063 valid_int, bss_term_dur, url,
1064 nei_pos > nei_rep ? nei_rep : NULL,
1065 nei_pos - nei_rep, mbo_len ? mbo : NULL,
1071 #endif /* CONFIG_WNM */
1074 static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
1075 char *buf, size_t buflen)
1083 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
1085 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1086 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1087 if (os_snprintf_error(end - pos, ret))
1091 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1092 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1093 if (os_snprintf_error(end - pos, ret))
1097 #ifdef CONFIG_IEEE80211R
1098 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1099 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1100 if (os_snprintf_error(end - pos, ret))
1104 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1105 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1106 if (os_snprintf_error(end - pos, ret))
1111 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1112 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1113 if (os_snprintf_error(end - pos, ret))
1117 #endif /* CONFIG_SAE */
1118 #endif /* CONFIG_IEEE80211R */
1119 #ifdef CONFIG_IEEE80211W
1120 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1121 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1122 if (os_snprintf_error(end - pos, ret))
1126 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1127 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1128 if (os_snprintf_error(end - pos, ret))
1132 #endif /* CONFIG_IEEE80211W */
1134 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1135 ret = os_snprintf(pos, end - pos, "SAE ");
1136 if (os_snprintf_error(end - pos, ret))
1140 #endif /* CONFIG_SAE */
1141 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1142 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1143 if (os_snprintf_error(end - pos, ret))
1147 if (hapd->conf->wpa_key_mgmt &
1148 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1149 ret = os_snprintf(pos, end - pos,
1150 "WPA-EAP-SUITE-B-192 ");
1151 if (os_snprintf_error(end - pos, ret))
1156 if (pos > buf && *(pos - 1) == ' ') {
1165 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1166 char *buf, size_t buflen)
1174 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1176 MAC2STR(hapd->own_addr),
1177 wpa_ssid_txt(hapd->conf->ssid.ssid,
1178 hapd->conf->ssid.ssid_len));
1179 if (os_snprintf_error(end - pos, ret))
1184 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1185 hapd->conf->wps_state == 0 ? "disabled" :
1186 (hapd->conf->wps_state == 1 ? "not configured" :
1188 if (os_snprintf_error(end - pos, ret))
1192 if (hapd->conf->wps_state && hapd->conf->wpa &&
1193 hapd->conf->ssid.wpa_passphrase) {
1194 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1195 hapd->conf->ssid.wpa_passphrase);
1196 if (os_snprintf_error(end - pos, ret))
1201 if (hapd->conf->wps_state && hapd->conf->wpa &&
1202 hapd->conf->ssid.wpa_psk &&
1203 hapd->conf->ssid.wpa_psk->group) {
1204 char hex[PMK_LEN * 2 + 1];
1205 wpa_snprintf_hex(hex, sizeof(hex),
1206 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1207 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
1208 if (os_snprintf_error(end - pos, ret))
1212 #endif /* CONFIG_WPS */
1214 if (hapd->conf->wpa) {
1215 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1216 if (os_snprintf_error(end - pos, ret))
1221 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1222 ret = os_snprintf(pos, end - pos, "key_mgmt=");
1223 if (os_snprintf_error(end - pos, ret))
1227 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
1229 ret = os_snprintf(pos, end - pos, "\n");
1230 if (os_snprintf_error(end - pos, ret))
1235 if (hapd->conf->wpa) {
1236 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1237 wpa_cipher_txt(hapd->conf->wpa_group));
1238 if (os_snprintf_error(end - pos, ret))
1243 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1244 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
1245 if (os_snprintf_error(end - pos, ret))
1249 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1255 ret = os_snprintf(pos, end - pos, "\n");
1256 if (os_snprintf_error(end - pos, ret))
1261 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1262 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
1263 if (os_snprintf_error(end - pos, ret))
1267 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
1273 ret = os_snprintf(pos, end - pos, "\n");
1274 if (os_snprintf_error(end - pos, ret))
1283 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1288 value = os_strchr(cmd, ' ');
1293 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1295 #ifdef CONFIG_WPS_TESTING
1296 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1298 val = strtol(value, NULL, 0);
1299 if (val < 0 || val > 0xff) {
1301 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1302 "wps_version_number %ld", val);
1304 wps_version_number = val;
1305 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1307 (wps_version_number & 0xf0) >> 4,
1308 wps_version_number & 0x0f);
1309 hostapd_wps_update_ie(hapd);
1311 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1312 wps_testing_dummy_cred = atoi(value);
1313 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1314 wps_testing_dummy_cred);
1315 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1316 wps_corrupt_pkhash = atoi(value);
1317 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1318 wps_corrupt_pkhash);
1319 #endif /* CONFIG_WPS_TESTING */
1320 #ifdef CONFIG_INTERWORKING
1321 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1322 int val = atoi(value);
1326 hapd->gas_frag_limit = val;
1327 #endif /* CONFIG_INTERWORKING */
1328 #ifdef CONFIG_TESTING_OPTIONS
1329 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1330 hapd->ext_mgmt_frame_handling = atoi(value);
1331 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1332 hapd->ext_eapol_frame_io = atoi(value);
1333 #endif /* CONFIG_TESTING_OPTIONS */
1335 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1338 if (!hapd->conf->mbo_enabled)
1342 if (val < 0 || val > 1)
1345 hapd->mbo_assoc_disallow = val;
1346 ieee802_11_update_beacons(hapd->iface);
1349 * TODO: Need to configure drivers that do AP MLME offload with
1350 * disallowing station logic.
1352 #endif /* CONFIG_MBO */
1354 struct sta_info *sta;
1355 struct vlan_description vlan_id;
1357 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1361 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1362 for (sta = hapd->sta_list; sta; sta = sta->next) {
1363 if (hostapd_maclist_found(
1364 hapd->conf->deny_mac,
1365 hapd->conf->num_deny_mac, sta->addr,
1367 (!vlan_id.notempty ||
1368 !vlan_compare(&vlan_id, sta->vlan_desc)))
1370 hapd, sta, sta->addr,
1371 WLAN_REASON_UNSPECIFIED);
1373 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1374 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1375 for (sta = hapd->sta_list; sta; sta = sta->next) {
1376 if (!hostapd_maclist_found(
1377 hapd->conf->accept_mac,
1378 hapd->conf->num_accept_mac,
1379 sta->addr, &vlan_id) ||
1380 (vlan_id.notempty &&
1381 vlan_compare(&vlan_id, sta->vlan_desc)))
1383 hapd, sta, sta->addr,
1384 WLAN_REASON_UNSPECIFIED);
1393 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1394 char *buf, size_t buflen)
1398 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1400 if (os_strcmp(cmd, "version") == 0) {
1401 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1402 if (os_snprintf_error(buflen, res))
1405 } else if (os_strcmp(cmd, "tls_library") == 0) {
1406 res = tls_get_library_version(buf, buflen);
1407 if (os_snprintf_error(buflen, res))
1416 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1418 if (hostapd_enable_iface(iface) < 0) {
1419 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1426 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1428 if (hostapd_reload_iface(iface) < 0) {
1429 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1436 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1438 if (hostapd_disable_iface(iface) < 0) {
1439 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1446 #ifdef CONFIG_TESTING_OPTIONS
1448 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1450 union wpa_event_data data;
1452 enum wpa_event_type event;
1454 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1456 os_memset(&data, 0, sizeof(data));
1458 param = os_strchr(cmd, ' ');
1463 if (os_strcmp(cmd, "DETECTED") == 0)
1464 event = EVENT_DFS_RADAR_DETECTED;
1465 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1466 event = EVENT_DFS_CAC_FINISHED;
1467 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1468 event = EVENT_DFS_CAC_ABORTED;
1469 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1470 event = EVENT_DFS_NOP_FINISHED;
1472 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1477 pos = os_strstr(param, "freq=");
1479 data.dfs_event.freq = atoi(pos + 5);
1481 pos = os_strstr(param, "ht_enabled=1");
1483 data.dfs_event.ht_enabled = 1;
1485 pos = os_strstr(param, "chan_offset=");
1487 data.dfs_event.chan_offset = atoi(pos + 12);
1489 pos = os_strstr(param, "chan_width=");
1491 data.dfs_event.chan_width = atoi(pos + 11);
1493 pos = os_strstr(param, "cf1=");
1495 data.dfs_event.cf1 = atoi(pos + 4);
1497 pos = os_strstr(param, "cf2=");
1499 data.dfs_event.cf2 = atoi(pos + 4);
1501 wpa_supplicant_event(hapd, event, &data);
1507 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1513 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1515 len = os_strlen(cmd);
1520 buf = os_malloc(len);
1524 if (hexstr2bin(cmd, buf, len) < 0) {
1529 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1535 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1538 u8 src[ETH_ALEN], *buf;
1542 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1545 used = hwaddr_aton2(pos, src);
1552 len = os_strlen(pos);
1557 buf = os_malloc(len);
1561 if (hexstr2bin(pos, buf, len) < 0) {
1566 ieee802_1x_receive(hapd, src, buf, len);
1573 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1577 const u16 *pos = buf;
1579 for (i = 0; i < len / 2; i++)
1583 sum = (sum & 0xffff) + (sum >> 16);
1585 return sum ^ 0xffff;
1589 #define HWSIM_PACKETLEN 1500
1590 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1592 void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1595 struct hostapd_data *hapd = ctx;
1596 const struct ether_header *eth;
1601 if (len != HWSIM_PACKETLEN)
1604 eth = (const struct ether_header *) buf;
1605 os_memcpy(&ip, eth + 1, sizeof(ip));
1606 pos = &buf[sizeof(*eth) + sizeof(ip)];
1608 if (ip.ihl != 5 || ip.version != 4 ||
1609 ntohs(ip.tot_len) != HWSIM_IP_LEN)
1612 for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
1618 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
1619 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
1623 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1626 int enabled = atoi(cmd);
1631 if (hapd->l2_test) {
1632 l2_packet_deinit(hapd->l2_test);
1633 hapd->l2_test = NULL;
1634 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1635 "test data: Disabled");
1643 pos = os_strstr(cmd, " ifname=");
1647 ifname = hapd->conf->iface;
1649 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1650 ETHERTYPE_IP, hostapd_data_test_rx,
1652 if (hapd->l2_test == NULL)
1655 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1661 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1663 u8 dst[ETH_ALEN], src[ETH_ALEN];
1668 u8 buf[2 + HWSIM_PACKETLEN];
1669 struct ether_header *eth;
1674 if (hapd->l2_test == NULL)
1677 /* format: <dst> <src> <tos> */
1680 used = hwaddr_aton2(pos, dst);
1686 used = hwaddr_aton2(pos, src);
1691 val = strtol(pos, NULL, 0);
1692 if (val < 0 || val > 0xff)
1696 eth = (struct ether_header *) &buf[2];
1697 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1698 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1699 eth->ether_type = htons(ETHERTYPE_IP);
1700 ip = (struct iphdr *) (eth + 1);
1701 os_memset(ip, 0, sizeof(*ip));
1706 ip->tot_len = htons(HWSIM_IP_LEN);
1708 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1709 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1710 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
1711 dpos = (u8 *) (ip + 1);
1712 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
1715 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
1716 HWSIM_PACKETLEN) < 0)
1719 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1720 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1726 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1730 struct ether_header *eth;
1731 struct l2_packet_data *l2 = NULL;
1735 const char *ifname = hapd->conf->iface;
1737 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1740 cmd = os_strchr(cmd, ' ');
1746 len = os_strlen(cmd);
1747 if (len & 1 || len < ETH_HLEN * 2)
1751 buf = os_malloc(len);
1755 if (hexstr2bin(cmd, buf, len) < 0)
1758 eth = (struct ether_header *) buf;
1759 ethertype = ntohs(eth->ether_type);
1761 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1762 hostapd_data_test_rx, hapd, 1);
1766 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1767 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1770 l2_packet_deinit(l2);
1773 return res < 0 ? -1 : 0;
1777 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
1779 #ifdef WPA_TRACE_BFD
1780 extern char wpa_trace_fail_func[256];
1781 extern unsigned int wpa_trace_fail_after;
1784 wpa_trace_fail_after = atoi(cmd);
1785 pos = os_strchr(cmd, ':');
1788 os_strlcpy(wpa_trace_fail_func, pos,
1789 sizeof(wpa_trace_fail_func));
1791 wpa_trace_fail_after = 0;
1795 #else /* WPA_TRACE_BFD */
1797 #endif /* WPA_TRACE_BFD */
1801 static int hostapd_ctrl_get_alloc_fail(struct hostapd_data *hapd,
1802 char *buf, size_t buflen)
1804 #ifdef WPA_TRACE_BFD
1805 extern char wpa_trace_fail_func[256];
1806 extern unsigned int wpa_trace_fail_after;
1808 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
1809 wpa_trace_fail_func);
1810 #else /* WPA_TRACE_BFD */
1812 #endif /* WPA_TRACE_BFD */
1816 static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
1818 #ifdef WPA_TRACE_BFD
1819 extern char wpa_trace_test_fail_func[256];
1820 extern unsigned int wpa_trace_test_fail_after;
1823 wpa_trace_test_fail_after = atoi(cmd);
1824 pos = os_strchr(cmd, ':');
1827 os_strlcpy(wpa_trace_test_fail_func, pos,
1828 sizeof(wpa_trace_test_fail_func));
1830 wpa_trace_test_fail_after = 0;
1834 #else /* WPA_TRACE_BFD */
1836 #endif /* WPA_TRACE_BFD */
1840 static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
1841 char *buf, size_t buflen)
1843 #ifdef WPA_TRACE_BFD
1844 extern char wpa_trace_test_fail_func[256];
1845 extern unsigned int wpa_trace_test_fail_after;
1847 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
1848 wpa_trace_test_fail_func);
1849 #else /* WPA_TRACE_BFD */
1851 #endif /* WPA_TRACE_BFD */
1854 #endif /* CONFIG_TESTING_OPTIONS */
1857 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
1861 struct csa_settings settings;
1865 ret = hostapd_parse_csa_settings(pos, &settings);
1869 for (i = 0; i < iface->num_bss; i++) {
1870 ret = hostapd_switch_channel(iface->bss[i], &settings);
1872 /* FIX: What do we do if CSA fails in the middle of
1873 * submitting multi-BSS CSA requests? */
1879 #else /* NEED_AP_MLME */
1881 #endif /* NEED_AP_MLME */
1885 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1886 int reply_size, const char *param)
1888 #ifdef RADIUS_SERVER
1889 if (os_strcmp(param, "radius_server") == 0) {
1890 return radius_server_get_mib(hapd->radius_srv, reply,
1893 #endif /* RADIUS_SERVER */
1898 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
1899 char *buf, size_t buflen)
1904 unsigned int vendor_id, subcmd;
1905 struct wpabuf *reply;
1906 size_t data_len = 0;
1908 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1909 vendor_id = strtoul(cmd, &pos, 16);
1910 if (!isblank((unsigned char) *pos))
1913 subcmd = strtoul(pos, &pos, 10);
1916 if (!isblank((unsigned char) *pos++))
1918 data_len = os_strlen(pos);
1923 data = os_malloc(data_len);
1927 if (hexstr2bin(pos, data, data_len)) {
1928 wpa_printf(MSG_DEBUG,
1929 "Vendor command: wrong parameter format");
1935 reply = wpabuf_alloc((buflen - 1) / 2);
1941 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
1945 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
1955 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
1959 struct sta_info *sta;
1961 if (hwaddr_aton(cmd, addr))
1964 sta = ap_get_sta(hapd, addr);
1965 if (!sta || !sta->eapol_sm)
1968 eapol_auth_reauthenticate(sta->eapol_sm);
1973 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
1976 struct sta_info *sta;
1977 char *pos = cmd, *param;
1979 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
1983 pos = os_strchr(pos, ' ');
1988 sta = ap_get_sta(hapd, addr);
1989 if (!sta || !sta->eapol_sm)
1992 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
1996 static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
1997 char *buf, size_t buflen)
1999 char *pos, *end, *stamp;
2002 /* cmd: "LOG_LEVEL [<level>]" */
2006 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2008 debug_level_str(wpa_debug_level),
2009 wpa_debug_timestamp);
2010 if (os_snprintf_error(end - pos, ret))
2019 stamp = os_strchr(cmd, ' ');
2022 while (*stamp == ' ') {
2027 if (os_strlen(cmd)) {
2028 int level = str_to_debug_level(cmd);
2031 wpa_debug_level = level;
2034 if (stamp && os_strlen(stamp))
2035 wpa_debug_timestamp = atoi(stamp);
2037 os_memcpy(buf, "OK\n", 3);
2043 static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2044 char *buf, size_t buflen)
2046 struct hostapd_iface *iface = hapd->iface;
2048 struct hostapd_sta_info *info;
2049 struct os_reltime now;
2051 sta_track_expire(iface, 0);
2056 os_get_reltime(&now);
2057 dl_list_for_each_reverse(info, &iface->sta_seen,
2058 struct hostapd_sta_info, list) {
2059 struct os_reltime age;
2062 os_reltime_sub(&now, &info->last_seen, &age);
2063 ret = os_snprintf(pos, end - pos, MACSTR " %u\n",
2064 MAC2STR(info->addr), (unsigned int) age.sec);
2065 if (os_snprintf_error(end - pos, ret))
2072 #endif /* NEED_AP_MLME */
2075 static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
2077 struct wpa_ssid_value ssid;
2079 struct wpabuf *nr, *lci = NULL, *civic = NULL;
2083 if (!(hapd->conf->radio_measurements[0] &
2084 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2085 wpa_printf(MSG_ERROR,
2086 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2090 if (hwaddr_aton(buf, bssid)) {
2091 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
2095 tmp = os_strstr(buf, "ssid=");
2096 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2097 wpa_printf(MSG_ERROR,
2098 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2101 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
2105 tmp = os_strstr(buf, "nr=");
2107 wpa_printf(MSG_ERROR,
2108 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2112 buf = os_strchr(tmp, ' ');
2116 nr = wpabuf_parse_bin(tmp + 3);
2118 wpa_printf(MSG_ERROR,
2119 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2126 tmp = os_strstr(buf, "lci=");
2128 buf = os_strchr(tmp, ' ');
2131 lci = wpabuf_parse_bin(tmp + 4);
2133 wpa_printf(MSG_ERROR,
2134 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
2143 tmp = os_strstr(buf, "civic=");
2145 buf = os_strchr(tmp, ' ');
2148 civic = wpabuf_parse_bin(tmp + 6);
2150 wpa_printf(MSG_ERROR,
2151 "CTRL: SET_NEIGHBOR: Bad civic subelement");
2159 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic);
2169 static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
2172 struct wpa_ssid_value ssid;
2176 if (hwaddr_aton(buf, bssid)) {
2177 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
2181 tmp = os_strstr(buf, "ssid=");
2182 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2183 wpa_printf(MSG_ERROR,
2184 "CTRL: REMOVE_NEIGHBORr: Bad or missing SSID");
2188 return hostapd_neighbor_remove(hapd, bssid, &ssid);
2192 static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
2193 char *buf, char *reply,
2195 struct sockaddr_storage *from,
2200 os_memcpy(reply, "OK\n", 3);
2203 if (os_strcmp(buf, "PING") == 0) {
2204 os_memcpy(reply, "PONG\n", 5);
2206 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2207 if (wpa_debug_reopen_file() < 0)
2209 } else if (os_strcmp(buf, "STATUS") == 0) {
2210 reply_len = hostapd_ctrl_iface_status(hapd, reply,
2212 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
2213 reply_len = hostapd_drv_status(hapd, reply, reply_size);
2214 } else if (os_strcmp(buf, "MIB") == 0) {
2215 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
2216 if (reply_len >= 0) {
2217 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
2218 reply_size - reply_len);
2224 if (reply_len >= 0) {
2225 res = ieee802_1x_get_mib(hapd, reply + reply_len,
2226 reply_size - reply_len);
2232 #ifndef CONFIG_NO_RADIUS
2233 if (reply_len >= 0) {
2234 res = radius_client_get_mib(hapd->radius,
2236 reply_size - reply_len);
2242 #endif /* CONFIG_NO_RADIUS */
2243 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
2244 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
2246 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
2247 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
2249 } else if (os_strncmp(buf, "STA ", 4) == 0) {
2250 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
2252 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
2253 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
2255 } else if (os_strcmp(buf, "ATTACH") == 0) {
2256 if (hostapd_ctrl_iface_attach(hapd, from, fromlen))
2258 } else if (os_strcmp(buf, "DETACH") == 0) {
2259 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
2261 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
2262 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
2265 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
2266 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
2268 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
2269 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
2271 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
2272 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
2274 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
2275 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
2277 } else if (os_strcmp(buf, "STOP_AP") == 0) {
2278 if (hostapd_ctrl_iface_stop_ap(hapd))
2280 #ifdef CONFIG_IEEE80211W
2282 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
2283 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
2285 #endif /* NEED_AP_MLME */
2286 #endif /* CONFIG_IEEE80211W */
2288 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
2289 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
2291 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
2292 reply_len = hostapd_ctrl_iface_wps_check_pin(
2293 hapd, buf + 14, reply, reply_size);
2294 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
2295 if (hostapd_wps_button_pushed(hapd, NULL))
2297 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
2298 if (hostapd_wps_cancel(hapd))
2300 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
2301 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
2303 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
2304 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
2306 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
2307 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
2309 #ifdef CONFIG_WPS_NFC
2310 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
2311 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
2313 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2314 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
2315 hapd, buf + 21, reply, reply_size);
2316 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
2317 reply_len = hostapd_ctrl_iface_wps_nfc_token(
2318 hapd, buf + 14, reply, reply_size);
2319 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2320 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
2321 hapd, buf + 21, reply, reply_size);
2322 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
2323 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
2325 #endif /* CONFIG_WPS_NFC */
2326 #endif /* CONFIG_WPS */
2327 #ifdef CONFIG_INTERWORKING
2328 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
2329 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
2331 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
2332 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
2334 #endif /* CONFIG_INTERWORKING */
2336 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
2337 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
2339 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
2340 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
2342 #endif /* CONFIG_HS20 */
2344 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
2345 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
2347 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
2348 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
2350 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
2351 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
2353 #endif /* CONFIG_WNM */
2354 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
2355 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
2357 } else if (os_strncmp(buf, "SET ", 4) == 0) {
2358 if (hostapd_ctrl_iface_set(hapd, buf + 4))
2360 } else if (os_strncmp(buf, "GET ", 4) == 0) {
2361 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
2363 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
2364 if (hostapd_ctrl_iface_enable(hapd->iface))
2366 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
2367 if (hostapd_ctrl_iface_reload(hapd->iface))
2369 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
2370 if (hostapd_ctrl_iface_disable(hapd->iface))
2372 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
2373 if (ieee802_11_set_beacon(hapd))
2375 #ifdef CONFIG_TESTING_OPTIONS
2376 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
2377 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
2379 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
2380 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
2382 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
2383 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
2385 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
2386 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
2388 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
2389 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
2391 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
2392 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
2394 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
2395 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
2397 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
2398 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
2400 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
2401 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
2403 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
2404 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
2405 #endif /* CONFIG_TESTING_OPTIONS */
2406 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
2407 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
2409 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
2410 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
2412 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
2413 ieee802_1x_erp_flush(hapd);
2414 #ifdef RADIUS_SERVER
2415 radius_server_erp_flush(hapd->radius_srv);
2416 #endif /* RADIUS_SERVER */
2417 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
2418 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
2420 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
2421 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
2423 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
2424 reply_len = hostapd_ctrl_iface_log_level(
2425 hapd, buf + 9, reply, reply_size);
2427 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
2428 reply_len = hostapd_ctrl_iface_track_sta_list(
2429 hapd, reply, reply_size);
2430 #endif /* NEED_AP_MLME */
2431 } else if (os_strcmp(buf, "PMKSA") == 0) {
2432 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
2434 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
2435 hostapd_ctrl_iface_pmksa_flush(hapd);
2436 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
2437 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
2439 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
2440 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
2443 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2447 if (reply_len < 0) {
2448 os_memcpy(reply, "FAIL\n", 5);
2456 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
2459 struct hostapd_data *hapd = eloop_ctx;
2462 struct sockaddr_storage from;
2463 socklen_t fromlen = sizeof(from);
2464 char *reply, *pos = buf;
2465 const int reply_size = 4096;
2467 int level = MSG_DEBUG;
2468 #ifdef CONFIG_CTRL_IFACE_UDP
2469 unsigned char lcookie[COOKIE_LEN];
2470 #endif /* CONFIG_CTRL_IFACE_UDP */
2472 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2473 (struct sockaddr *) &from, &fromlen);
2475 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2481 reply = os_malloc(reply_size);
2482 if (reply == NULL) {
2483 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
2485 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2491 #ifdef CONFIG_CTRL_IFACE_UDP
2492 if (os_strcmp(buf, "GET_COOKIE") == 0) {
2493 os_memcpy(reply, "COOKIE=", 7);
2494 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
2495 cookie, COOKIE_LEN);
2496 reply_len = 7 + 2 * COOKIE_LEN;
2500 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
2501 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
2502 wpa_printf(MSG_DEBUG,
2503 "CTRL: No cookie in the request - drop request");
2508 if (os_memcmp(cookie, lcookie, COOKIE_LEN) != 0) {
2509 wpa_printf(MSG_DEBUG,
2510 "CTRL: Invalid cookie in the request - drop request");
2515 pos = buf + 7 + 2 * COOKIE_LEN;
2518 #endif /* CONFIG_CTRL_IFACE_UDP */
2520 if (os_strcmp(pos, "PING") == 0)
2521 level = MSG_EXCESSIVE;
2522 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
2524 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
2528 #ifdef CONFIG_CTRL_IFACE_UDP
2530 #endif /* CONFIG_CTRL_IFACE_UDP */
2531 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2533 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2540 #ifndef CONFIG_CTRL_IFACE_UDP
2541 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
2546 if (hapd->conf->ctrl_interface == NULL)
2549 len = os_strlen(hapd->conf->ctrl_interface) +
2550 os_strlen(hapd->conf->iface) + 2;
2551 buf = os_malloc(len);
2555 os_snprintf(buf, len, "%s/%s",
2556 hapd->conf->ctrl_interface, hapd->conf->iface);
2557 buf[len - 1] = '\0';
2560 #endif /* CONFIG_CTRL_IFACE_UDP */
2563 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
2564 enum wpa_msg_type type,
2565 const char *txt, size_t len)
2567 struct hostapd_data *hapd = ctx;
2570 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
2574 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
2576 #ifdef CONFIG_CTRL_IFACE_UDP
2577 int port = HOSTAPD_CTRL_IFACE_PORT;
2579 char port_str[40], *tmp;
2581 struct addrinfo hints = { 0 }, *res, *saveres;
2584 if (hapd->ctrl_sock > -1) {
2585 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2589 if (hapd->conf->ctrl_interface == NULL)
2592 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
2597 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
2602 dl_list_init(&hapd->ctrl_dst);
2603 hapd->ctrl_sock = -1;
2604 os_get_random(cookie, COOKIE_LEN);
2606 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
2607 hints.ai_flags = AI_PASSIVE;
2608 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
2610 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
2611 hints.ai_family = AF_INET6;
2612 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
2613 hints.ai_family = AF_INET;
2614 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
2615 hints.ai_socktype = SOCK_DGRAM;
2618 os_snprintf(p, sizeof(p), "%d", port);
2619 n = getaddrinfo(NULL, p, &hints, &res);
2621 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
2626 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
2628 if (hapd->ctrl_sock < 0) {
2629 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
2633 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
2635 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
2636 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
2638 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
2642 freeaddrinfo(saveres);
2644 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
2645 tmp = os_strdup(port_str);
2647 os_free(hapd->conf->ctrl_interface);
2648 hapd->conf->ctrl_interface = tmp;
2650 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
2652 if (eloop_register_read_sock(hapd->ctrl_sock,
2653 hostapd_ctrl_iface_receive, hapd, NULL) <
2655 hostapd_ctrl_iface_deinit(hapd);
2659 hapd->msg_ctx = hapd;
2660 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
2665 if (hapd->ctrl_sock >= 0)
2666 close(hapd->ctrl_sock);
2668 #else /* CONFIG_CTRL_IFACE_UDP */
2669 struct sockaddr_un addr;
2673 if (hapd->ctrl_sock > -1) {
2674 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2678 dl_list_init(&hapd->ctrl_dst);
2680 if (hapd->conf->ctrl_interface == NULL)
2683 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2684 if (errno == EEXIST) {
2685 wpa_printf(MSG_DEBUG, "Using existing control "
2686 "interface directory.");
2688 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2694 if (hapd->conf->ctrl_interface_gid_set &&
2695 chown(hapd->conf->ctrl_interface, -1,
2696 hapd->conf->ctrl_interface_gid) < 0) {
2697 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2702 if (!hapd->conf->ctrl_interface_gid_set &&
2703 hapd->iface->interfaces->ctrl_iface_group &&
2704 chown(hapd->conf->ctrl_interface, -1,
2705 hapd->iface->interfaces->ctrl_iface_group) < 0) {
2706 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2713 * Android is using umask 0077 which would leave the control interface
2714 * directory without group access. This breaks things since Wi-Fi
2715 * framework assumes that this directory can be accessed by other
2716 * applications in the wifi group. Fix this by adding group access even
2717 * if umask value would prevent this.
2719 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2720 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
2722 /* Try to continue anyway */
2724 #endif /* ANDROID */
2726 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
2727 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
2730 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2732 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
2736 os_memset(&addr, 0, sizeof(addr));
2738 addr.sun_len = sizeof(addr);
2739 #endif /* __FreeBSD__ */
2740 addr.sun_family = AF_UNIX;
2741 fname = hostapd_ctrl_iface_path(hapd);
2744 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2745 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2746 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2748 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2749 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2750 " allow connections - assuming it was left"
2751 "over from forced program termination");
2752 if (unlink(fname) < 0) {
2753 wpa_printf(MSG_ERROR,
2754 "Could not unlink existing ctrl_iface socket '%s': %s",
2755 fname, strerror(errno));
2758 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2760 wpa_printf(MSG_ERROR,
2761 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
2765 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2766 "ctrl_iface socket '%s'", fname);
2768 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2769 "be in use - cannot override it");
2770 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2771 "not used anymore", fname);
2778 if (hapd->conf->ctrl_interface_gid_set &&
2779 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
2780 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2785 if (!hapd->conf->ctrl_interface_gid_set &&
2786 hapd->iface->interfaces->ctrl_iface_group &&
2787 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
2788 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2793 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2794 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2800 hapd->ctrl_sock = s;
2801 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
2803 hostapd_ctrl_iface_deinit(hapd);
2806 hapd->msg_ctx = hapd;
2807 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
2819 #endif /* CONFIG_CTRL_IFACE_UDP */
2823 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
2825 struct wpa_ctrl_dst *dst, *prev;
2827 if (hapd->ctrl_sock > -1) {
2828 #ifndef CONFIG_CTRL_IFACE_UDP
2830 #endif /* !CONFIG_CTRL_IFACE_UDP */
2832 eloop_unregister_read_sock(hapd->ctrl_sock);
2833 close(hapd->ctrl_sock);
2834 hapd->ctrl_sock = -1;
2835 #ifndef CONFIG_CTRL_IFACE_UDP
2836 fname = hostapd_ctrl_iface_path(hapd);
2841 if (hapd->conf->ctrl_interface &&
2842 rmdir(hapd->conf->ctrl_interface) < 0) {
2843 if (errno == ENOTEMPTY) {
2844 wpa_printf(MSG_DEBUG, "Control interface "
2845 "directory not empty - leaving it "
2848 wpa_printf(MSG_ERROR,
2849 "rmdir[ctrl_interface=%s]: %s",
2850 hapd->conf->ctrl_interface,
2854 #endif /* !CONFIG_CTRL_IFACE_UDP */
2857 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
2861 #ifdef CONFIG_TESTING_OPTIONS
2862 l2_packet_deinit(hapd->l2_test);
2863 hapd->l2_test = NULL;
2864 #endif /* CONFIG_TESTING_OPTIONS */
2868 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
2871 if (hostapd_add_iface(interfaces, buf) < 0) {
2872 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
2879 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
2882 if (hostapd_remove_iface(interfaces, buf) < 0) {
2883 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
2890 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
2891 struct sockaddr_storage *from,
2894 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen);
2898 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
2899 struct sockaddr_storage *from,
2902 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
2906 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
2908 #ifdef CONFIG_WPS_TESTING
2909 wps_version_number = 0x20;
2910 wps_testing_dummy_cred = 0;
2911 wps_corrupt_pkhash = 0;
2912 #endif /* CONFIG_WPS_TESTING */
2919 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
2922 char ifname[IFNAMSIZ + 1];
2923 struct fst_iface_cfg cfg;
2924 struct hostapd_data *hapd;
2925 struct fst_wpa_obj iface_obj;
2927 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
2928 hapd = hostapd_get_iface(interfaces, ifname);
2930 if (hapd->iface->fst) {
2931 wpa_printf(MSG_INFO, "FST: Already attached");
2934 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
2935 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
2937 if (hapd->iface->fst)
2947 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
2950 char ifname[IFNAMSIZ + 1];
2951 struct hostapd_data * hapd;
2953 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
2954 hapd = hostapd_get_iface(interfaces, ifname);
2956 if (!fst_iface_detach(ifname)) {
2957 hapd->iface->fst = NULL;
2958 hapd->iface->fst_ies = NULL;
2967 #endif /* CONFIG_FST */
2970 static struct hostapd_data *
2971 hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
2976 for (i = 0; i < interfaces->count; i++) {
2977 struct hostapd_iface *iface = interfaces->iface[i];
2979 for (j = 0; j < iface->num_bss; j++) {
2980 struct hostapd_data *hapd;
2982 hapd = iface->bss[j];
2983 if (os_strcmp(ifname, hapd->conf->iface) == 0)
2992 static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
2993 struct hostapd_data *dst_hapd,
2999 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
3001 wpa_printf(MSG_ERROR,
3002 "DUP: cannot allocate buffer to stringify %s",
3007 if (os_strcmp(param, "wpa") == 0) {
3008 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
3009 src_hapd->conf->wpa);
3010 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
3011 src_hapd->conf->wpa_key_mgmt) {
3012 res = hostapd_ctrl_iface_get_key_mgmt(
3013 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
3014 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
3015 goto error_stringify;
3016 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
3017 src_hapd->conf->wpa_pairwise) {
3018 res = wpa_write_ciphers(value,
3019 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3020 src_hapd->conf->wpa_pairwise, " ");
3022 goto error_stringify;
3023 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
3024 src_hapd->conf->rsn_pairwise) {
3025 res = wpa_write_ciphers(value,
3026 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3027 src_hapd->conf->rsn_pairwise, " ");
3029 goto error_stringify;
3030 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
3031 src_hapd->conf->ssid.wpa_passphrase) {
3032 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
3033 src_hapd->conf->ssid.wpa_passphrase);
3034 } else if (os_strcmp(param, "wpa_psk") == 0 &&
3035 src_hapd->conf->ssid.wpa_psk_set) {
3036 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3037 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
3039 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
3043 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
3048 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
3056 hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
3058 char *reply, int reply_size)
3063 struct hostapd_iface *iface;
3067 show_ctrl = !!os_strstr(input, "ctrl");
3070 end = reply + reply_size;
3072 for (i = 0; i < interfaces->count; i++) {
3073 iface = interfaces->iface[i];
3075 for (j = 0; j < iface->num_bss; j++) {
3076 struct hostapd_bss_config *conf;
3078 conf = iface->conf->bss[j];
3080 res = os_snprintf(pos, end - pos,
3081 "%s ctrl_iface=%s\n",
3083 conf->ctrl_interface ?
3084 conf->ctrl_interface : "N/A");
3086 res = os_snprintf(pos, end - pos, "%s\n",
3088 if (os_snprintf_error(end - pos, res)) {
3101 hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
3104 char *p_start = cmd, *p_end;
3105 struct hostapd_data *src_hapd, *dst_hapd;
3107 /* cmd: "<src ifname> <dst ifname> <variable name> */
3109 p_end = os_strchr(p_start, ' ');
3111 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
3117 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
3119 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
3124 p_start = p_end + 1;
3125 p_end = os_strchr(p_start, ' ');
3127 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
3133 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
3135 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
3140 p_start = p_end + 1;
3141 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
3145 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
3147 char *buf, char *reply,
3149 struct sockaddr_storage *from,
3152 struct hostapd_data *hapd;
3154 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
3158 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
3159 if (os_snprintf_error(reply_size, res))
3164 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
3169 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
3172 void *interfaces = eloop_ctx;
3173 char buffer[256], *buf = buffer;
3175 struct sockaddr_storage from;
3176 socklen_t fromlen = sizeof(from);
3179 const int reply_size = 4096;
3180 #ifdef CONFIG_CTRL_IFACE_UDP
3181 unsigned char lcookie[COOKIE_LEN];
3182 #endif /* CONFIG_CTRL_IFACE_UDP */
3184 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
3185 (struct sockaddr *) &from, &fromlen);
3187 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3192 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
3194 reply = os_malloc(reply_size);
3195 if (reply == NULL) {
3196 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3198 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3204 os_memcpy(reply, "OK\n", 3);
3207 #ifdef CONFIG_CTRL_IFACE_UDP
3208 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3209 os_memcpy(reply, "COOKIE=", 7);
3210 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
3211 gcookie, COOKIE_LEN);
3212 reply_len = 7 + 2 * COOKIE_LEN;
3216 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
3217 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
3218 wpa_printf(MSG_DEBUG,
3219 "CTRL: No cookie in the request - drop request");
3224 if (os_memcmp(gcookie, lcookie, COOKIE_LEN) != 0) {
3225 wpa_printf(MSG_DEBUG,
3226 "CTRL: Invalid cookie in the request - drop request");
3231 buf += 7 + 2 * COOKIE_LEN;
3234 #endif /* CONFIG_CTRL_IFACE_UDP */
3236 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
3237 char *pos = os_strchr(buf + 7, ' ');
3241 reply_len = hostapd_global_ctrl_iface_ifname(
3242 interfaces, buf + 7, pos, reply, reply_size,
3248 if (os_strcmp(buf, "PING") == 0) {
3249 os_memcpy(reply, "PONG\n", 5);
3251 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3252 if (wpa_debug_reopen_file() < 0)
3254 } else if (os_strcmp(buf, "FLUSH") == 0) {
3255 hostapd_ctrl_iface_flush(interfaces);
3256 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
3257 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
3259 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
3260 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
3262 } else if (os_strcmp(buf, "ATTACH") == 0) {
3263 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
3266 } else if (os_strcmp(buf, "DETACH") == 0) {
3267 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
3270 #ifdef CONFIG_MODULE_TESTS
3271 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
3272 int hapd_module_tests(void);
3273 if (hapd_module_tests() < 0)
3275 #endif /* CONFIG_MODULE_TESTS */
3277 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
3278 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
3279 reply_len = os_snprintf(reply, reply_size, "OK\n");
3282 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
3283 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
3284 reply_len = os_snprintf(reply, reply_size, "OK\n");
3287 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
3288 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
3289 #endif /* CONFIG_FST */
3290 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
3291 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
3293 reply_len = os_snprintf(reply, reply_size, "OK\n");
3296 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
3297 reply_len = hostapd_global_ctrl_iface_interfaces(
3298 interfaces, buf + 10, reply, sizeof(buffer));
3299 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3302 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
3308 if (reply_len < 0) {
3309 os_memcpy(reply, "FAIL\n", 5);
3313 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3315 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3322 #ifndef CONFIG_CTRL_IFACE_UDP
3323 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
3328 if (interface->global_iface_path == NULL)
3331 len = os_strlen(interface->global_iface_path) +
3332 os_strlen(interface->global_iface_name) + 2;
3333 buf = os_malloc(len);
3337 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
3338 interface->global_iface_name);
3339 buf[len - 1] = '\0';
3342 #endif /* CONFIG_CTRL_IFACE_UDP */
3345 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
3347 #ifdef CONFIG_CTRL_IFACE_UDP
3348 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
3351 struct addrinfo hints = { 0 }, *res, *saveres;
3354 if (interface->global_ctrl_sock > -1) {
3355 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3359 if (interface->global_iface_path == NULL)
3362 pos = os_strstr(interface->global_iface_path, "udp:");
3367 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
3372 dl_list_init(&interface->global_ctrl_dst);
3373 interface->global_ctrl_sock = -1;
3374 os_get_random(gcookie, COOKIE_LEN);
3376 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3377 hints.ai_flags = AI_PASSIVE;
3378 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3380 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3381 hints.ai_family = AF_INET6;
3382 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3383 hints.ai_family = AF_INET;
3384 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3385 hints.ai_socktype = SOCK_DGRAM;
3388 os_snprintf(p, sizeof(p), "%d", port);
3389 n = getaddrinfo(NULL, p, &hints, &res);
3391 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3396 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
3398 if (interface->global_ctrl_sock < 0) {
3399 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3403 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
3406 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
3407 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
3409 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
3413 freeaddrinfo(saveres);
3415 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
3417 if (eloop_register_read_sock(interface->global_ctrl_sock,
3418 hostapd_global_ctrl_iface_receive,
3419 interface, NULL) < 0) {
3420 hostapd_global_ctrl_iface_deinit(interface);
3427 if (interface->global_ctrl_sock >= 0)
3428 close(interface->global_ctrl_sock);
3430 #else /* CONFIG_CTRL_IFACE_UDP */
3431 struct sockaddr_un addr;
3435 if (interface->global_iface_path == NULL) {
3436 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
3440 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
3441 if (errno == EEXIST) {
3442 wpa_printf(MSG_DEBUG, "Using existing control "
3443 "interface directory.");
3445 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3449 } else if (interface->ctrl_iface_group &&
3450 chown(interface->global_iface_path, -1,
3451 interface->ctrl_iface_group) < 0) {
3452 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3457 if (os_strlen(interface->global_iface_path) + 1 +
3458 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
3461 s = socket(PF_UNIX, SOCK_DGRAM, 0);
3463 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
3467 os_memset(&addr, 0, sizeof(addr));
3469 addr.sun_len = sizeof(addr);
3470 #endif /* __FreeBSD__ */
3471 addr.sun_family = AF_UNIX;
3472 fname = hostapd_global_ctrl_iface_path(interface);
3475 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
3476 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3477 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
3479 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3480 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
3481 " allow connections - assuming it was left"
3482 "over from forced program termination");
3483 if (unlink(fname) < 0) {
3484 wpa_printf(MSG_ERROR,
3485 "Could not unlink existing ctrl_iface socket '%s': %s",
3486 fname, strerror(errno));
3489 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
3491 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
3495 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
3496 "ctrl_iface socket '%s'", fname);
3498 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
3499 "be in use - cannot override it");
3500 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
3501 "not used anymore", fname);
3508 if (interface->ctrl_iface_group &&
3509 chown(fname, -1, interface->ctrl_iface_group) < 0) {
3510 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3515 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
3516 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
3522 interface->global_ctrl_sock = s;
3523 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
3536 #endif /* CONFIG_CTRL_IFACE_UDP */
3540 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
3542 #ifndef CONFIG_CTRL_IFACE_UDP
3544 #endif /* CONFIG_CTRL_IFACE_UDP */
3545 struct wpa_ctrl_dst *dst, *prev;
3547 if (interfaces->global_ctrl_sock > -1) {
3548 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
3549 close(interfaces->global_ctrl_sock);
3550 interfaces->global_ctrl_sock = -1;
3551 #ifndef CONFIG_CTRL_IFACE_UDP
3552 fname = hostapd_global_ctrl_iface_path(interfaces);
3558 if (interfaces->global_iface_path &&
3559 rmdir(interfaces->global_iface_path) < 0) {
3560 if (errno == ENOTEMPTY) {
3561 wpa_printf(MSG_DEBUG, "Control interface "
3562 "directory not empty - leaving it "
3565 wpa_printf(MSG_ERROR,
3566 "rmdir[ctrl_interface=%s]: %s",
3567 interfaces->global_iface_path,
3571 #endif /* CONFIG_CTRL_IFACE_UDP */
3574 os_free(interfaces->global_iface_path);
3575 interfaces->global_iface_path = NULL;
3577 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
3578 struct wpa_ctrl_dst, list)
3583 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
3584 enum wpa_msg_type type,
3585 const char *buf, size_t len)
3587 struct wpa_ctrl_dst *dst, *next;
3588 struct dl_list *ctrl_dst;
3595 if (type != WPA_MSG_ONLY_GLOBAL) {
3596 s = hapd->ctrl_sock;
3597 ctrl_dst = &hapd->ctrl_dst;
3599 s = hapd->iface->interfaces->global_ctrl_sock;
3600 ctrl_dst = &hapd->iface->interfaces->global_ctrl_dst;
3603 if (s < 0 || dl_list_empty(ctrl_dst))
3606 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
3607 io[0].iov_base = levelstr;
3608 io[0].iov_len = os_strlen(levelstr);
3609 io[1].iov_base = (char *) buf;
3610 io[1].iov_len = len;
3611 os_memset(&msg, 0, sizeof(msg));
3616 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
3617 if (level >= dst->debug_level) {
3618 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
3619 &dst->addr, dst->addrlen);
3620 msg.msg_name = &dst->addr;
3621 msg.msg_namelen = dst->addrlen;
3622 if (sendmsg(s, &msg, 0) < 0) {
3624 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
3626 idx, errno, strerror(errno));
3628 if (dst->errors > 10 || _errno == ENOENT) {
3629 if (type != WPA_MSG_ONLY_GLOBAL)
3630 hostapd_ctrl_iface_detach(
3634 hostapd_global_ctrl_iface_detach(
3635 hapd->iface->interfaces,
3646 #endif /* CONFIG_NATIVE_WINDOWS */