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 "utils/module_tests.h"
29 #include "common/version.h"
30 #include "common/ieee802_11_defs.h"
31 #include "common/ctrl_iface_common.h"
32 #include "crypto/tls.h"
33 #include "drivers/driver.h"
34 #include "eapol_auth/eapol_auth_sm.h"
35 #include "radius/radius_client.h"
36 #include "radius/radius_server.h"
37 #include "l2_packet/l2_packet.h"
38 #include "ap/hostapd.h"
39 #include "ap/ap_config.h"
40 #include "ap/ieee802_1x.h"
41 #include "ap/wpa_auth.h"
42 #include "ap/ieee802_11.h"
43 #include "ap/sta_info.h"
44 #include "ap/wps_hostapd.h"
45 #include "ap/ctrl_iface_ap.h"
46 #include "ap/ap_drv_ops.h"
48 #include "ap/wnm_ap.h"
49 #include "ap/wpa_auth.h"
50 #include "ap/beacon.h"
51 #include "ap/neighbor_db.h"
53 #include "wps/wps_defs.h"
55 #include "fst/fst_ctrl_iface.h"
56 #include "config_file.h"
57 #include "ctrl_iface.h"
60 #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
62 #ifdef CONFIG_CTRL_IFACE_UDP
64 static unsigned char cookie[COOKIE_LEN];
65 static unsigned char gcookie[COOKIE_LEN];
66 #define HOSTAPD_CTRL_IFACE_PORT 8877
67 #define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
68 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
69 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT 50
70 #endif /* CONFIG_CTRL_IFACE_UDP */
72 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
73 enum wpa_msg_type type,
74 const char *buf, size_t len);
77 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
78 struct sockaddr_storage *from,
81 return ctrl_iface_attach(&hapd->ctrl_dst, from, fromlen);
85 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
86 struct sockaddr_storage *from,
89 return ctrl_iface_detach(&hapd->ctrl_dst, from, fromlen);
93 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
94 struct sockaddr_storage *from,
98 return ctrl_iface_level(&hapd->ctrl_dst, from, fromlen, level);
102 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
106 struct sta_info *sta;
108 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
110 if (hwaddr_aton(txtaddr, addr))
113 sta = ap_get_sta(hapd, addr);
117 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
118 "notification", MAC2STR(addr));
119 sta = ap_sta_add(hapd, addr);
123 hostapd_new_assoc_sta(hapd, sta, 0);
128 #ifdef CONFIG_IEEE80211W
130 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
134 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
136 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
138 if (hwaddr_aton(txtaddr, addr) ||
139 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
142 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
146 #endif /* NEED_AP_MLME */
147 #endif /* CONFIG_IEEE80211W */
151 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
153 char *pin = os_strchr(txt, ' ');
156 u8 addr_buf[ETH_ALEN], *addr = NULL;
163 timeout_txt = os_strchr(pin, ' ');
165 *timeout_txt++ = '\0';
166 timeout = atoi(timeout_txt);
167 pos = os_strchr(timeout_txt, ' ');
170 if (hwaddr_aton(pos, addr_buf) == 0)
176 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
180 static int hostapd_ctrl_iface_wps_check_pin(
181 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
188 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
189 (u8 *) cmd, os_strlen(cmd));
190 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
191 if (*pos < '0' || *pos > '9')
195 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
199 if (len != 4 && len != 8) {
200 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
206 unsigned int pin_val;
208 if (!wps_pin_valid(pin_val)) {
209 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
210 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
211 if (os_snprintf_error(buflen, ret))
217 ret = os_snprintf(buf, buflen, "%s", pin);
218 if (os_snprintf_error(buflen, ret))
225 #ifdef CONFIG_WPS_NFC
226 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
233 len = os_strlen(pos);
238 buf = wpabuf_alloc(len);
241 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
246 ret = hostapd_wps_nfc_tag_read(hapd, buf);
253 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
254 char *cmd, char *reply,
261 if (os_strcmp(cmd, "WPS") == 0)
263 else if (os_strcmp(cmd, "NDEF") == 0)
268 buf = hostapd_wps_nfc_config_token(hapd, ndef);
272 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
283 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
284 char *reply, size_t max_len,
290 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
294 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
305 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
306 char *cmd, char *reply,
309 if (os_strcmp(cmd, "WPS") == 0)
310 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
313 if (os_strcmp(cmd, "NDEF") == 0)
314 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
317 if (os_strcmp(cmd, "enable") == 0)
318 return hostapd_wps_nfc_token_enable(hapd);
320 if (os_strcmp(cmd, "disable") == 0) {
321 hostapd_wps_nfc_token_disable(hapd);
329 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
330 char *cmd, char *reply,
338 pos = os_strchr(cmd, ' ');
343 if (os_strcmp(cmd, "WPS") == 0)
345 else if (os_strcmp(cmd, "NDEF") == 0)
350 if (os_strcmp(pos, "WPS-CR") == 0)
351 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
357 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
368 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
372 struct wpabuf *req, *sel;
374 char *pos, *role, *type, *pos2;
377 pos = os_strchr(role, ' ');
383 pos = os_strchr(type, ' ');
388 pos2 = os_strchr(pos, ' ');
393 len = os_strlen(pos);
398 req = wpabuf_alloc(len);
401 if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
406 len = os_strlen(pos2);
413 sel = wpabuf_alloc(len);
418 if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
424 if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0) {
425 ret = hostapd_wps_nfc_report_handover(hapd, req, sel);
427 wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
428 "reported: role=%s type=%s", role, type);
437 #endif /* CONFIG_WPS_NFC */
440 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
441 char *buf, size_t buflen)
447 pos = os_strchr(txt, ' ');
451 if (os_strcmp(txt, "disable") == 0) {
452 hostapd_wps_ap_pin_disable(hapd);
453 return os_snprintf(buf, buflen, "OK\n");
456 if (os_strcmp(txt, "random") == 0) {
459 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
462 return os_snprintf(buf, buflen, "%s", pin_txt);
465 if (os_strcmp(txt, "get") == 0) {
466 pin_txt = hostapd_wps_ap_pin_get(hapd);
469 return os_snprintf(buf, buflen, "%s", pin_txt);
472 if (os_strcmp(txt, "set") == 0) {
477 pos = os_strchr(pos, ' ');
482 if (os_strlen(pin) > buflen)
484 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
486 return os_snprintf(buf, buflen, "%s", pin);
493 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
496 char *ssid, *auth, *encr = NULL, *key = NULL;
499 pos = os_strchr(txt, ' ');
505 pos = os_strchr(pos, ' ');
509 pos = os_strchr(pos, ' ');
516 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
520 static const char * pbc_status_str(enum pbc_status status)
523 case WPS_PBC_STATUS_DISABLE:
525 case WPS_PBC_STATUS_ACTIVE:
527 case WPS_PBC_STATUS_TIMEOUT:
529 case WPS_PBC_STATUS_OVERLAP:
537 static int hostapd_ctrl_iface_wps_get_status(struct hostapd_data *hapd,
538 char *buf, size_t buflen)
546 ret = os_snprintf(pos, end - pos, "PBC Status: %s\n",
547 pbc_status_str(hapd->wps_stats.pbc_status));
549 if (os_snprintf_error(end - pos, ret))
553 ret = os_snprintf(pos, end - pos, "Last WPS result: %s\n",
554 (hapd->wps_stats.status == WPS_STATUS_SUCCESS ?
556 (hapd->wps_stats.status == WPS_STATUS_FAILURE ?
557 "Failed" : "None")));
559 if (os_snprintf_error(end - pos, ret))
563 /* If status == Failure - Add possible Reasons */
564 if(hapd->wps_stats.status == WPS_STATUS_FAILURE &&
565 hapd->wps_stats.failure_reason > 0) {
566 ret = os_snprintf(pos, end - pos,
567 "Failure Reason: %s\n",
568 wps_ei_str(hapd->wps_stats.failure_reason));
570 if (os_snprintf_error(end - pos, ret))
575 if (hapd->wps_stats.status) {
576 ret = os_snprintf(pos, end - pos, "Peer Address: " MACSTR "\n",
577 MAC2STR(hapd->wps_stats.peer_addr));
579 if (os_snprintf_error(end - pos, ret))
587 #endif /* CONFIG_WPS */
591 static int hostapd_ctrl_iface_hs20_wnm_notif(struct hostapd_data *hapd,
597 if (hwaddr_aton(cmd, addr))
610 return hs20_send_wnm_notification(hapd, addr, 1, url);
614 static int hostapd_ctrl_iface_hs20_deauth_req(struct hostapd_data *hapd,
618 int code, reauth_delay, ret;
623 /* <STA MAC Addr> <Code(0/1)> <Re-auth-Delay(sec)> [URL] */
624 if (hwaddr_aton(cmd, addr))
627 pos = os_strchr(cmd, ' ');
633 pos = os_strchr(pos, ' ');
637 reauth_delay = atoi(pos);
640 pos = os_strchr(pos, ' ');
643 url_len = os_strlen(pos);
646 req = wpabuf_alloc(4 + url_len);
649 wpabuf_put_u8(req, code);
650 wpabuf_put_le16(req, reauth_delay);
651 wpabuf_put_u8(req, url_len);
653 wpabuf_put_data(req, pos, url_len);
655 wpa_printf(MSG_DEBUG, "HS 2.0: Send WNM-Notification to " MACSTR
656 " to indicate imminent deauthentication (code=%d "
657 "reauth_delay=%d)", MAC2STR(addr), code, reauth_delay);
658 ret = hs20_send_wnm_notification_deauth_req(hapd, addr, req);
663 #endif /* CONFIG_HS20 */
666 #ifdef CONFIG_INTERWORKING
668 static int hostapd_ctrl_iface_set_qos_map_set(struct hostapd_data *hapd,
671 u8 qos_map_set[16 + 2 * 21], count = 0;
672 const char *pos = cmd;
676 if (count == sizeof(qos_map_set)) {
677 wpa_printf(MSG_ERROR, "Too many qos_map_set parameters");
682 if (val < 0 || val > 255) {
683 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
687 qos_map_set[count++] = val;
688 pos = os_strchr(pos, ',');
694 if (count < 16 || count & 1) {
695 wpa_printf(MSG_INFO, "Invalid QoS Map Set");
699 ret = hostapd_drv_set_qos_map(hapd, qos_map_set, count);
701 wpa_printf(MSG_INFO, "Failed to set QoS Map Set");
705 os_memcpy(hapd->conf->qos_map_set, qos_map_set, count);
706 hapd->conf->qos_map_set_len = count;
712 static int hostapd_ctrl_iface_send_qos_map_conf(struct hostapd_data *hapd,
716 struct sta_info *sta;
718 u8 *qos_map_set = hapd->conf->qos_map_set;
719 u8 qos_map_set_len = hapd->conf->qos_map_set_len;
722 if (!qos_map_set_len) {
723 wpa_printf(MSG_INFO, "QoS Map Set is not set");
727 if (hwaddr_aton(cmd, addr))
730 sta = ap_get_sta(hapd, addr);
732 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
733 "for QoS Map Configuration message",
738 if (!sta->qos_map_enabled) {
739 wpa_printf(MSG_DEBUG, "Station " MACSTR " did not indicate "
740 "support for QoS Map", MAC2STR(addr));
744 buf = wpabuf_alloc(2 + 2 + qos_map_set_len);
748 wpabuf_put_u8(buf, WLAN_ACTION_QOS);
749 wpabuf_put_u8(buf, QOS_QOS_MAP_CONFIG);
751 /* QoS Map Set Element */
752 wpabuf_put_u8(buf, WLAN_EID_QOS_MAP_SET);
753 wpabuf_put_u8(buf, qos_map_set_len);
754 wpabuf_put_data(buf, qos_map_set, qos_map_set_len);
756 ret = hostapd_drv_send_action(hapd, hapd->iface->freq, 0, addr,
757 wpabuf_head(buf), wpabuf_len(buf));
763 #endif /* CONFIG_INTERWORKING */
768 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
773 struct sta_info *sta;
775 if (hwaddr_aton(cmd, addr))
779 disassoc_timer = atoi(cmd + 17);
781 sta = ap_get_sta(hapd, addr);
783 wpa_printf(MSG_DEBUG, "Station " MACSTR
784 " not found for disassociation imminent message",
789 return wnm_send_disassoc_imminent(hapd, sta, disassoc_timer);
793 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
797 const char *url, *timerstr;
799 struct sta_info *sta;
801 if (hwaddr_aton(cmd, addr))
804 sta = ap_get_sta(hapd, addr);
806 wpa_printf(MSG_DEBUG, "Station " MACSTR
807 " not found for ESS disassociation imminent message",
813 if (*timerstr != ' ')
816 disassoc_timer = atoi(timerstr);
817 if (disassoc_timer < 0 || disassoc_timer > 65535)
820 url = os_strchr(timerstr, ' ');
825 return wnm_send_ess_disassoc_imminent(hapd, sta, url, disassoc_timer);
829 static int hostapd_ctrl_iface_bss_tm_req(struct hostapd_data *hapd,
833 const char *pos, *end;
834 int disassoc_timer = 0;
835 struct sta_info *sta;
836 u8 req_mode = 0, valid_int = 0x01;
841 u8 *nei_pos = nei_rep;
845 if (hwaddr_aton(cmd, addr)) {
846 wpa_printf(MSG_DEBUG, "Invalid STA MAC address");
850 sta = ap_get_sta(hapd, addr);
852 wpa_printf(MSG_DEBUG, "Station " MACSTR
853 " not found for BSS TM Request message",
858 pos = os_strstr(cmd, " disassoc_timer=");
861 disassoc_timer = atoi(pos);
862 if (disassoc_timer < 0 || disassoc_timer > 65535) {
863 wpa_printf(MSG_DEBUG, "Invalid disassoc_timer");
868 pos = os_strstr(cmd, " valid_int=");
871 valid_int = atoi(pos);
874 pos = os_strstr(cmd, " bss_term=");
877 req_mode |= WNM_BSS_TM_REQ_BSS_TERMINATION_INCLUDED;
878 /* TODO: TSF configurable/learnable */
879 bss_term_dur[0] = 4; /* Subelement ID */
880 bss_term_dur[1] = 10; /* Length */
881 os_memset(bss_term_dur, 2, 8);
882 end = os_strchr(pos, ',');
884 wpa_printf(MSG_DEBUG, "Invalid bss_term data");
888 WPA_PUT_LE16(&bss_term_dur[10], atoi(end));
893 * BSS Transition Candidate List Entries - Neighbor Report elements
894 * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
895 * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
903 pos = os_strstr(pos, " neighbor=");
906 if (nei_pos + 15 > nei_rep + sizeof(nei_rep)) {
907 wpa_printf(MSG_DEBUG,
908 "Not enough room for additional neighbor");
914 *nei_pos++ = WLAN_EID_NEIGHBOR_REPORT;
915 nei_pos++; /* length to be filled in */
917 if (hwaddr_aton(pos, nei_pos)) {
918 wpa_printf(MSG_DEBUG, "Invalid BSSID");
924 wpa_printf(MSG_DEBUG, "Missing BSSID Information");
929 val = strtol(pos, &endptr, 0);
930 WPA_PUT_LE32(nei_pos, val);
932 if (*endptr != ',') {
933 wpa_printf(MSG_DEBUG, "Missing Operating Class");
938 *nei_pos++ = atoi(pos); /* Operating Class */
939 pos = os_strchr(pos, ',');
941 wpa_printf(MSG_DEBUG, "Missing Channel Number");
946 *nei_pos++ = atoi(pos); /* Channel Number */
947 pos = os_strchr(pos, ',');
949 wpa_printf(MSG_DEBUG, "Missing PHY Type");
954 *nei_pos++ = atoi(pos); /* PHY Type */
955 end = os_strchr(pos, ' ');
956 tmp = os_strchr(pos, ',');
957 if (tmp && (!end || tmp < end)) {
958 /* Optional Subelements (hexdump) */
962 end = os_strchr(pos, ' ');
966 len = os_strlen(pos);
967 if (nei_pos + len / 2 > nei_rep + sizeof(nei_rep)) {
968 wpa_printf(MSG_DEBUG,
969 "Not enough room for neighbor subelements");
973 hexstr2bin(pos, nei_pos, len / 2) < 0) {
974 wpa_printf(MSG_DEBUG,
975 "Invalid neighbor subelement info");
982 nei_start[1] = nei_pos - nei_start - 2;
985 pos = os_strstr(cmd, " url=");
989 end = os_strchr(pos, ' ');
993 len = os_strlen(pos);
994 url = os_malloc(len + 1);
997 os_memcpy(url, pos, len);
999 req_mode |= WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
1002 if (os_strstr(cmd, " pref=1"))
1003 req_mode |= WNM_BSS_TM_REQ_PREF_CAND_LIST_INCLUDED;
1004 if (os_strstr(cmd, " abridged=1"))
1005 req_mode |= WNM_BSS_TM_REQ_ABRIDGED;
1006 if (os_strstr(cmd, " disassoc_imminent=1"))
1007 req_mode |= WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
1010 pos = os_strstr(cmd, "mbo=");
1012 unsigned int mbo_reason, cell_pref, reassoc_delay;
1015 ret = sscanf(pos, "mbo=%u:%u:%u", &mbo_reason,
1016 &reassoc_delay, &cell_pref);
1018 wpa_printf(MSG_DEBUG,
1019 "MBO requires three arguments: mbo=<reason>:<reassoc_delay>:<cell_pref>");
1023 if (mbo_reason > MBO_TRANSITION_REASON_PREMIUM_AP) {
1024 wpa_printf(MSG_DEBUG,
1025 "Invalid MBO transition reason code %u",
1030 /* Valid values for Cellular preference are: 0, 1, 255 */
1031 if (cell_pref != 0 && cell_pref != 1 && cell_pref != 255) {
1032 wpa_printf(MSG_DEBUG,
1033 "Invalid MBO cellular capability %u",
1038 if (reassoc_delay > 65535 ||
1040 !(req_mode & WNM_BSS_TM_REQ_DISASSOC_IMMINENT))) {
1041 wpa_printf(MSG_DEBUG,
1042 "MBO: Assoc retry delay is only valid in disassoc imminent mode");
1046 *mbo_pos++ = MBO_ATTR_ID_TRANSITION_REASON;
1048 *mbo_pos++ = mbo_reason;
1049 *mbo_pos++ = MBO_ATTR_ID_CELL_DATA_PREF;
1051 *mbo_pos++ = cell_pref;
1053 if (reassoc_delay) {
1054 *mbo_pos++ = MBO_ATTR_ID_ASSOC_RETRY_DELAY;
1056 WPA_PUT_LE16(mbo_pos, reassoc_delay);
1060 mbo_len = mbo_pos - mbo;
1062 #endif /* CONFIG_MBO */
1064 ret = wnm_send_bss_tm_req(hapd, sta, req_mode, disassoc_timer,
1065 valid_int, bss_term_dur, url,
1066 nei_pos > nei_rep ? nei_rep : NULL,
1067 nei_pos - nei_rep, mbo_len ? mbo : NULL,
1073 #endif /* CONFIG_WNM */
1076 static int hostapd_ctrl_iface_get_key_mgmt(struct hostapd_data *hapd,
1077 char *buf, size_t buflen)
1085 WPA_ASSERT(hapd->conf->wpa_key_mgmt);
1087 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
1088 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
1089 if (os_snprintf_error(end - pos, ret))
1093 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
1094 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
1095 if (os_snprintf_error(end - pos, ret))
1099 #ifdef CONFIG_IEEE80211R
1100 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
1101 ret = os_snprintf(pos, end - pos, "FT-PSK ");
1102 if (os_snprintf_error(end - pos, ret))
1106 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
1107 ret = os_snprintf(pos, end - pos, "FT-EAP ");
1108 if (os_snprintf_error(end - pos, ret))
1113 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1114 ret = os_snprintf(pos, end - pos, "FT-SAE ");
1115 if (os_snprintf_error(end - pos, ret))
1119 #endif /* CONFIG_SAE */
1120 #endif /* CONFIG_IEEE80211R */
1121 #ifdef CONFIG_IEEE80211W
1122 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
1123 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
1124 if (os_snprintf_error(end - pos, ret))
1128 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1129 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
1130 if (os_snprintf_error(end - pos, ret))
1134 #endif /* CONFIG_IEEE80211W */
1136 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) {
1137 ret = os_snprintf(pos, end - pos, "SAE ");
1138 if (os_snprintf_error(end - pos, ret))
1142 #endif /* CONFIG_SAE */
1143 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1144 ret = os_snprintf(pos, end - pos, "WPA-EAP-SUITE-B ");
1145 if (os_snprintf_error(end - pos, ret))
1149 if (hapd->conf->wpa_key_mgmt &
1150 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1151 ret = os_snprintf(pos, end - pos,
1152 "WPA-EAP-SUITE-B-192 ");
1153 if (os_snprintf_error(end - pos, ret))
1158 if (pos > buf && *(pos - 1) == ' ') {
1167 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
1168 char *buf, size_t buflen)
1176 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
1178 MAC2STR(hapd->own_addr),
1179 wpa_ssid_txt(hapd->conf->ssid.ssid,
1180 hapd->conf->ssid.ssid_len));
1181 if (os_snprintf_error(end - pos, ret))
1186 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
1187 hapd->conf->wps_state == 0 ? "disabled" :
1188 (hapd->conf->wps_state == 1 ? "not configured" :
1190 if (os_snprintf_error(end - pos, ret))
1194 if (hapd->conf->wps_state && hapd->conf->wpa &&
1195 hapd->conf->ssid.wpa_passphrase) {
1196 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
1197 hapd->conf->ssid.wpa_passphrase);
1198 if (os_snprintf_error(end - pos, ret))
1203 if (hapd->conf->wps_state && hapd->conf->wpa &&
1204 hapd->conf->ssid.wpa_psk &&
1205 hapd->conf->ssid.wpa_psk->group) {
1206 char hex[PMK_LEN * 2 + 1];
1207 wpa_snprintf_hex(hex, sizeof(hex),
1208 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
1209 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
1210 if (os_snprintf_error(end - pos, ret))
1214 #endif /* CONFIG_WPS */
1216 if (hapd->conf->wpa) {
1217 ret = os_snprintf(pos, end - pos, "wpa=%d\n", hapd->conf->wpa);
1218 if (os_snprintf_error(end - pos, ret))
1223 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
1224 ret = os_snprintf(pos, end - pos, "key_mgmt=");
1225 if (os_snprintf_error(end - pos, ret))
1229 pos += hostapd_ctrl_iface_get_key_mgmt(hapd, pos, end - pos);
1231 ret = os_snprintf(pos, end - pos, "\n");
1232 if (os_snprintf_error(end - pos, ret))
1237 if (hapd->conf->wpa) {
1238 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
1239 wpa_cipher_txt(hapd->conf->wpa_group));
1240 if (os_snprintf_error(end - pos, ret))
1245 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
1246 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
1247 if (os_snprintf_error(end - pos, ret))
1251 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
1257 ret = os_snprintf(pos, end - pos, "\n");
1258 if (os_snprintf_error(end - pos, ret))
1263 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
1264 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
1265 if (os_snprintf_error(end - pos, ret))
1269 ret = wpa_write_ciphers(pos, end, hapd->conf->wpa_pairwise,
1275 ret = os_snprintf(pos, end - pos, "\n");
1276 if (os_snprintf_error(end - pos, ret))
1285 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
1290 value = os_strchr(cmd, ' ');
1295 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
1297 #ifdef CONFIG_WPS_TESTING
1298 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
1300 val = strtol(value, NULL, 0);
1301 if (val < 0 || val > 0xff) {
1303 wpa_printf(MSG_DEBUG, "WPS: Invalid "
1304 "wps_version_number %ld", val);
1306 wps_version_number = val;
1307 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
1309 (wps_version_number & 0xf0) >> 4,
1310 wps_version_number & 0x0f);
1311 hostapd_wps_update_ie(hapd);
1313 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
1314 wps_testing_dummy_cred = atoi(value);
1315 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
1316 wps_testing_dummy_cred);
1317 } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
1318 wps_corrupt_pkhash = atoi(value);
1319 wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
1320 wps_corrupt_pkhash);
1321 #endif /* CONFIG_WPS_TESTING */
1322 #ifdef CONFIG_INTERWORKING
1323 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
1324 int val = atoi(value);
1328 hapd->gas_frag_limit = val;
1329 #endif /* CONFIG_INTERWORKING */
1330 #ifdef CONFIG_TESTING_OPTIONS
1331 } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
1332 hapd->ext_mgmt_frame_handling = atoi(value);
1333 } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
1334 hapd->ext_eapol_frame_io = atoi(value);
1335 #endif /* CONFIG_TESTING_OPTIONS */
1337 } else if (os_strcasecmp(cmd, "mbo_assoc_disallow") == 0) {
1340 if (!hapd->conf->mbo_enabled)
1344 if (val < 0 || val > 1)
1347 hapd->mbo_assoc_disallow = val;
1348 ieee802_11_update_beacons(hapd->iface);
1351 * TODO: Need to configure drivers that do AP MLME offload with
1352 * disallowing station logic.
1354 #endif /* CONFIG_MBO */
1356 struct sta_info *sta;
1357 struct vlan_description vlan_id;
1359 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
1363 if (os_strcasecmp(cmd, "deny_mac_file") == 0) {
1364 for (sta = hapd->sta_list; sta; sta = sta->next) {
1365 if (hostapd_maclist_found(
1366 hapd->conf->deny_mac,
1367 hapd->conf->num_deny_mac, sta->addr,
1369 (!vlan_id.notempty ||
1370 !vlan_compare(&vlan_id, sta->vlan_desc)))
1372 hapd, sta, sta->addr,
1373 WLAN_REASON_UNSPECIFIED);
1375 } else if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED &&
1376 os_strcasecmp(cmd, "accept_mac_file") == 0) {
1377 for (sta = hapd->sta_list; sta; sta = sta->next) {
1378 if (!hostapd_maclist_found(
1379 hapd->conf->accept_mac,
1380 hapd->conf->num_accept_mac,
1381 sta->addr, &vlan_id) ||
1382 (vlan_id.notempty &&
1383 vlan_compare(&vlan_id, sta->vlan_desc)))
1385 hapd, sta, sta->addr,
1386 WLAN_REASON_UNSPECIFIED);
1395 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
1396 char *buf, size_t buflen)
1400 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
1402 if (os_strcmp(cmd, "version") == 0) {
1403 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
1404 if (os_snprintf_error(buflen, res))
1407 } else if (os_strcmp(cmd, "tls_library") == 0) {
1408 res = tls_get_library_version(buf, buflen);
1409 if (os_snprintf_error(buflen, res))
1418 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
1420 if (hostapd_enable_iface(iface) < 0) {
1421 wpa_printf(MSG_ERROR, "Enabling of interface failed");
1428 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
1430 if (hostapd_reload_iface(iface) < 0) {
1431 wpa_printf(MSG_ERROR, "Reloading of interface failed");
1438 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
1440 if (hostapd_disable_iface(iface) < 0) {
1441 wpa_printf(MSG_ERROR, "Disabling of interface failed");
1448 #ifdef CONFIG_TESTING_OPTIONS
1450 static int hostapd_ctrl_iface_radar(struct hostapd_data *hapd, char *cmd)
1452 union wpa_event_data data;
1454 enum wpa_event_type event;
1456 wpa_printf(MSG_DEBUG, "RADAR TEST: %s", cmd);
1458 os_memset(&data, 0, sizeof(data));
1460 param = os_strchr(cmd, ' ');
1465 if (os_strcmp(cmd, "DETECTED") == 0)
1466 event = EVENT_DFS_RADAR_DETECTED;
1467 else if (os_strcmp(cmd, "CAC-FINISHED") == 0)
1468 event = EVENT_DFS_CAC_FINISHED;
1469 else if (os_strcmp(cmd, "CAC-ABORTED") == 0)
1470 event = EVENT_DFS_CAC_ABORTED;
1471 else if (os_strcmp(cmd, "NOP-FINISHED") == 0)
1472 event = EVENT_DFS_NOP_FINISHED;
1474 wpa_printf(MSG_DEBUG, "Unsupported RADAR test command: %s",
1479 pos = os_strstr(param, "freq=");
1481 data.dfs_event.freq = atoi(pos + 5);
1483 pos = os_strstr(param, "ht_enabled=1");
1485 data.dfs_event.ht_enabled = 1;
1487 pos = os_strstr(param, "chan_offset=");
1489 data.dfs_event.chan_offset = atoi(pos + 12);
1491 pos = os_strstr(param, "chan_width=");
1493 data.dfs_event.chan_width = atoi(pos + 11);
1495 pos = os_strstr(param, "cf1=");
1497 data.dfs_event.cf1 = atoi(pos + 4);
1499 pos = os_strstr(param, "cf2=");
1501 data.dfs_event.cf2 = atoi(pos + 4);
1503 wpa_supplicant_event(hapd, event, &data);
1509 static int hostapd_ctrl_iface_mgmt_tx(struct hostapd_data *hapd, char *cmd)
1515 wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
1517 len = os_strlen(cmd);
1522 buf = os_malloc(len);
1526 if (hexstr2bin(cmd, buf, len) < 0) {
1531 res = hostapd_drv_send_mlme(hapd, buf, len, 0);
1537 static int hostapd_ctrl_iface_eapol_rx(struct hostapd_data *hapd, char *cmd)
1540 u8 src[ETH_ALEN], *buf;
1544 wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
1547 used = hwaddr_aton2(pos, src);
1554 len = os_strlen(pos);
1559 buf = os_malloc(len);
1563 if (hexstr2bin(pos, buf, len) < 0) {
1568 ieee802_1x_receive(hapd, src, buf, len);
1575 static u16 ipv4_hdr_checksum(const void *buf, size_t len)
1579 const u16 *pos = buf;
1581 for (i = 0; i < len / 2; i++)
1585 sum = (sum & 0xffff) + (sum >> 16);
1587 return sum ^ 0xffff;
1591 #define HWSIM_PACKETLEN 1500
1592 #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
1594 static void hostapd_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
1597 struct hostapd_data *hapd = ctx;
1598 const struct ether_header *eth;
1603 if (len != HWSIM_PACKETLEN)
1606 eth = (const struct ether_header *) buf;
1607 os_memcpy(&ip, eth + 1, sizeof(ip));
1608 pos = &buf[sizeof(*eth) + sizeof(ip)];
1610 if (ip.ihl != 5 || ip.version != 4 ||
1611 ntohs(ip.tot_len) != HWSIM_IP_LEN)
1614 for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
1620 wpa_msg(hapd->msg_ctx, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
1621 MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
1625 static int hostapd_ctrl_iface_data_test_config(struct hostapd_data *hapd,
1628 int enabled = atoi(cmd);
1633 if (hapd->l2_test) {
1634 l2_packet_deinit(hapd->l2_test);
1635 hapd->l2_test = NULL;
1636 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1637 "test data: Disabled");
1645 pos = os_strstr(cmd, " ifname=");
1649 ifname = hapd->conf->iface;
1651 hapd->l2_test = l2_packet_init(ifname, hapd->own_addr,
1652 ETHERTYPE_IP, hostapd_data_test_rx,
1654 if (hapd->l2_test == NULL)
1657 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: Enabled");
1663 static int hostapd_ctrl_iface_data_test_tx(struct hostapd_data *hapd, char *cmd)
1665 u8 dst[ETH_ALEN], src[ETH_ALEN];
1670 u8 buf[2 + HWSIM_PACKETLEN];
1671 struct ether_header *eth;
1676 if (hapd->l2_test == NULL)
1679 /* format: <dst> <src> <tos> */
1682 used = hwaddr_aton2(pos, dst);
1688 used = hwaddr_aton2(pos, src);
1693 val = strtol(pos, NULL, 0);
1694 if (val < 0 || val > 0xff)
1698 eth = (struct ether_header *) &buf[2];
1699 os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
1700 os_memcpy(eth->ether_shost, src, ETH_ALEN);
1701 eth->ether_type = htons(ETHERTYPE_IP);
1702 ip = (struct iphdr *) (eth + 1);
1703 os_memset(ip, 0, sizeof(*ip));
1708 ip->tot_len = htons(HWSIM_IP_LEN);
1710 ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
1711 ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
1712 ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
1713 dpos = (u8 *) (ip + 1);
1714 for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
1717 if (l2_packet_send(hapd->l2_test, dst, ETHERTYPE_IP, &buf[2],
1718 HWSIM_PACKETLEN) < 0)
1721 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX dst=" MACSTR
1722 " src=" MACSTR " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
1728 static int hostapd_ctrl_iface_data_test_frame(struct hostapd_data *hapd,
1732 struct ether_header *eth;
1733 struct l2_packet_data *l2 = NULL;
1737 const char *ifname = hapd->conf->iface;
1739 if (os_strncmp(cmd, "ifname=", 7) == 0) {
1742 cmd = os_strchr(cmd, ' ');
1748 len = os_strlen(cmd);
1749 if (len & 1 || len < ETH_HLEN * 2)
1753 buf = os_malloc(len);
1757 if (hexstr2bin(cmd, buf, len) < 0)
1760 eth = (struct ether_header *) buf;
1761 ethertype = ntohs(eth->ether_type);
1763 l2 = l2_packet_init(ifname, hapd->own_addr, ethertype,
1764 hostapd_data_test_rx, hapd, 1);
1768 res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
1769 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "test data: TX frame res=%d", res);
1772 l2_packet_deinit(l2);
1775 return res < 0 ? -1 : 0;
1779 static int hostapd_ctrl_test_alloc_fail(struct hostapd_data *hapd, char *cmd)
1781 #ifdef WPA_TRACE_BFD
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 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
1806 wpa_trace_fail_func);
1807 #else /* WPA_TRACE_BFD */
1809 #endif /* WPA_TRACE_BFD */
1813 static int hostapd_ctrl_test_fail(struct hostapd_data *hapd, char *cmd)
1815 #ifdef WPA_TRACE_BFD
1818 wpa_trace_test_fail_after = atoi(cmd);
1819 pos = os_strchr(cmd, ':');
1822 os_strlcpy(wpa_trace_test_fail_func, pos,
1823 sizeof(wpa_trace_test_fail_func));
1825 wpa_trace_test_fail_after = 0;
1829 #else /* WPA_TRACE_BFD */
1831 #endif /* WPA_TRACE_BFD */
1835 static int hostapd_ctrl_get_fail(struct hostapd_data *hapd,
1836 char *buf, size_t buflen)
1838 #ifdef WPA_TRACE_BFD
1839 return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
1840 wpa_trace_test_fail_func);
1841 #else /* WPA_TRACE_BFD */
1843 #endif /* WPA_TRACE_BFD */
1846 #endif /* CONFIG_TESTING_OPTIONS */
1849 static int hostapd_ctrl_iface_chan_switch(struct hostapd_iface *iface,
1853 struct csa_settings settings;
1857 ret = hostapd_parse_csa_settings(pos, &settings);
1861 for (i = 0; i < iface->num_bss; i++) {
1862 ret = hostapd_switch_channel(iface->bss[i], &settings);
1864 /* FIX: What do we do if CSA fails in the middle of
1865 * submitting multi-BSS CSA requests? */
1871 #else /* NEED_AP_MLME */
1873 #endif /* NEED_AP_MLME */
1877 static int hostapd_ctrl_iface_mib(struct hostapd_data *hapd, char *reply,
1878 int reply_size, const char *param)
1880 #ifdef RADIUS_SERVER
1881 if (os_strcmp(param, "radius_server") == 0) {
1882 return radius_server_get_mib(hapd->radius_srv, reply,
1885 #endif /* RADIUS_SERVER */
1890 static int hostapd_ctrl_iface_vendor(struct hostapd_data *hapd, char *cmd,
1891 char *buf, size_t buflen)
1896 unsigned int vendor_id, subcmd;
1897 struct wpabuf *reply;
1898 size_t data_len = 0;
1900 /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
1901 vendor_id = strtoul(cmd, &pos, 16);
1902 if (!isblank((unsigned char) *pos))
1905 subcmd = strtoul(pos, &pos, 10);
1908 if (!isblank((unsigned char) *pos++))
1910 data_len = os_strlen(pos);
1915 data = os_malloc(data_len);
1919 if (hexstr2bin(pos, data, data_len)) {
1920 wpa_printf(MSG_DEBUG,
1921 "Vendor command: wrong parameter format");
1927 reply = wpabuf_alloc((buflen - 1) / 2);
1933 ret = hostapd_drv_vendor_cmd(hapd, vendor_id, subcmd, data, data_len,
1937 ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
1947 static int hostapd_ctrl_iface_eapol_reauth(struct hostapd_data *hapd,
1951 struct sta_info *sta;
1953 if (hwaddr_aton(cmd, addr))
1956 sta = ap_get_sta(hapd, addr);
1957 if (!sta || !sta->eapol_sm)
1960 eapol_auth_reauthenticate(sta->eapol_sm);
1965 static int hostapd_ctrl_iface_eapol_set(struct hostapd_data *hapd, char *cmd)
1968 struct sta_info *sta;
1969 char *pos = cmd, *param;
1971 if (hwaddr_aton(pos, addr) || pos[17] != ' ')
1975 pos = os_strchr(pos, ' ');
1980 sta = ap_get_sta(hapd, addr);
1981 if (!sta || !sta->eapol_sm)
1984 return eapol_auth_set_conf(sta->eapol_sm, param, pos);
1988 static int hostapd_ctrl_iface_log_level(struct hostapd_data *hapd, char *cmd,
1989 char *buf, size_t buflen)
1991 char *pos, *end, *stamp;
1994 /* cmd: "LOG_LEVEL [<level>]" */
1998 ret = os_snprintf(pos, end - pos, "Current level: %s\n"
2000 debug_level_str(wpa_debug_level),
2001 wpa_debug_timestamp);
2002 if (os_snprintf_error(end - pos, ret))
2011 stamp = os_strchr(cmd, ' ');
2014 while (*stamp == ' ') {
2019 if (os_strlen(cmd)) {
2020 int level = str_to_debug_level(cmd);
2023 wpa_debug_level = level;
2026 if (stamp && os_strlen(stamp))
2027 wpa_debug_timestamp = atoi(stamp);
2029 os_memcpy(buf, "OK\n", 3);
2035 static int hostapd_ctrl_iface_track_sta_list(struct hostapd_data *hapd,
2036 char *buf, size_t buflen)
2038 struct hostapd_iface *iface = hapd->iface;
2040 struct hostapd_sta_info *info;
2041 struct os_reltime now;
2043 sta_track_expire(iface, 0);
2048 os_get_reltime(&now);
2049 dl_list_for_each_reverse(info, &iface->sta_seen,
2050 struct hostapd_sta_info, list) {
2051 struct os_reltime age;
2054 os_reltime_sub(&now, &info->last_seen, &age);
2055 ret = os_snprintf(pos, end - pos, MACSTR " %u\n",
2056 MAC2STR(info->addr), (unsigned int) age.sec);
2057 if (os_snprintf_error(end - pos, ret))
2064 #endif /* NEED_AP_MLME */
2067 static int hostapd_ctrl_iface_req_lci(struct hostapd_data *hapd,
2072 if (hwaddr_aton(cmd, addr)) {
2073 wpa_printf(MSG_INFO, "CTRL: REQ_LCI: Invalid MAC address");
2077 return hostapd_send_lci_req(hapd, addr);
2081 static int hostapd_ctrl_iface_req_range(struct hostapd_data *hapd, char *cmd)
2084 char *token, *context = NULL;
2085 int random_interval, min_ap;
2086 u8 responders[ETH_ALEN * RRM_RANGE_REQ_MAX_RESPONDERS];
2087 unsigned int n_responders;
2089 token = str_token(cmd, " ", &context);
2090 if (!token || hwaddr_aton(token, addr)) {
2091 wpa_printf(MSG_INFO,
2092 "CTRL: REQ_RANGE - Bad destination address");
2096 token = str_token(cmd, " ", &context);
2100 random_interval = atoi(token);
2101 if (random_interval < 0 || random_interval > 0xffff)
2104 token = str_token(cmd, " ", &context);
2108 min_ap = atoi(token);
2109 if (min_ap <= 0 || min_ap > WLAN_RRM_RANGE_REQ_MAX_MIN_AP)
2113 while ((token = str_token(cmd, " ", &context))) {
2114 if (n_responders == RRM_RANGE_REQ_MAX_RESPONDERS) {
2115 wpa_printf(MSG_INFO,
2116 "CTRL: REQ_RANGE: Too many responders");
2120 if (hwaddr_aton(token, responders + n_responders * ETH_ALEN)) {
2121 wpa_printf(MSG_INFO,
2122 "CTRL: REQ_RANGE: Bad responder address");
2129 if (!n_responders) {
2130 wpa_printf(MSG_INFO,
2131 "CTRL: REQ_RANGE - No FTM responder address");
2135 return hostapd_send_range_req(hapd, addr, random_interval, min_ap,
2136 responders, n_responders);
2140 static int hostapd_ctrl_iface_set_neighbor(struct hostapd_data *hapd, char *buf)
2142 struct wpa_ssid_value ssid;
2144 struct wpabuf *nr, *lci = NULL, *civic = NULL;
2148 if (!(hapd->conf->radio_measurements[0] &
2149 WLAN_RRM_CAPS_NEIGHBOR_REPORT)) {
2150 wpa_printf(MSG_ERROR,
2151 "CTRL: SET_NEIGHBOR: Neighbor report is not enabled");
2155 if (hwaddr_aton(buf, bssid)) {
2156 wpa_printf(MSG_ERROR, "CTRL: SET_NEIGHBOR: Bad BSSID");
2160 tmp = os_strstr(buf, "ssid=");
2161 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2162 wpa_printf(MSG_ERROR,
2163 "CTRL: SET_NEIGHBOR: Bad or missing SSID");
2166 buf = os_strchr(tmp + 6, tmp[5] == '"' ? '"' : ' ');
2170 tmp = os_strstr(buf, "nr=");
2172 wpa_printf(MSG_ERROR,
2173 "CTRL: SET_NEIGHBOR: Missing Neighbor Report element");
2177 buf = os_strchr(tmp, ' ');
2181 nr = wpabuf_parse_bin(tmp + 3);
2183 wpa_printf(MSG_ERROR,
2184 "CTRL: SET_NEIGHBOR: Bad Neighbor Report element");
2191 tmp = os_strstr(buf, "lci=");
2193 buf = os_strchr(tmp, ' ');
2196 lci = wpabuf_parse_bin(tmp + 4);
2198 wpa_printf(MSG_ERROR,
2199 "CTRL: SET_NEIGHBOR: Bad LCI subelement");
2208 tmp = os_strstr(buf, "civic=");
2210 buf = os_strchr(tmp, ' ');
2213 civic = wpabuf_parse_bin(tmp + 6);
2215 wpa_printf(MSG_ERROR,
2216 "CTRL: SET_NEIGHBOR: Bad civic subelement");
2224 ret = hostapd_neighbor_set(hapd, bssid, &ssid, nr, lci, civic);
2234 static int hostapd_ctrl_iface_remove_neighbor(struct hostapd_data *hapd,
2237 struct wpa_ssid_value ssid;
2241 if (hwaddr_aton(buf, bssid)) {
2242 wpa_printf(MSG_ERROR, "CTRL: REMOVE_NEIGHBOR: Bad BSSID");
2246 tmp = os_strstr(buf, "ssid=");
2247 if (!tmp || ssid_parse(tmp + 5, &ssid)) {
2248 wpa_printf(MSG_ERROR,
2249 "CTRL: REMOVE_NEIGHBORr: Bad or missing SSID");
2253 return hostapd_neighbor_remove(hapd, bssid, &ssid);
2257 static int hostapd_ctrl_driver_flags(struct hostapd_iface *iface, char *buf,
2263 ret = os_snprintf(buf, buflen, "%016llX:\n",
2264 (long long unsigned) iface->drv_flags);
2265 if (os_snprintf_error(buflen, ret))
2271 for (i = 0; i < 64; i++) {
2272 if (iface->drv_flags & (1LLU << i)) {
2273 ret = os_snprintf(pos, end - pos, "%s\n",
2274 driver_flag_to_string(1LLU << i));
2275 if (os_snprintf_error(end - pos, ret))
2285 static int hostapd_ctrl_iface_receive_process(struct hostapd_data *hapd,
2286 char *buf, char *reply,
2288 struct sockaddr_storage *from,
2293 os_memcpy(reply, "OK\n", 3);
2296 if (os_strcmp(buf, "PING") == 0) {
2297 os_memcpy(reply, "PONG\n", 5);
2299 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
2300 if (wpa_debug_reopen_file() < 0)
2302 } else if (os_strcmp(buf, "STATUS") == 0) {
2303 reply_len = hostapd_ctrl_iface_status(hapd, reply,
2305 } else if (os_strcmp(buf, "STATUS-DRIVER") == 0) {
2306 reply_len = hostapd_drv_status(hapd, reply, reply_size);
2307 } else if (os_strcmp(buf, "MIB") == 0) {
2308 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
2309 if (reply_len >= 0) {
2310 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
2311 reply_size - reply_len);
2317 if (reply_len >= 0) {
2318 res = ieee802_1x_get_mib(hapd, reply + reply_len,
2319 reply_size - reply_len);
2325 #ifndef CONFIG_NO_RADIUS
2326 if (reply_len >= 0) {
2327 res = radius_client_get_mib(hapd->radius,
2329 reply_size - reply_len);
2335 #endif /* CONFIG_NO_RADIUS */
2336 } else if (os_strncmp(buf, "MIB ", 4) == 0) {
2337 reply_len = hostapd_ctrl_iface_mib(hapd, reply, reply_size,
2339 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
2340 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
2342 } else if (os_strncmp(buf, "STA ", 4) == 0) {
2343 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
2345 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
2346 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
2348 } else if (os_strcmp(buf, "ATTACH") == 0) {
2349 if (hostapd_ctrl_iface_attach(hapd, from, fromlen))
2351 } else if (os_strcmp(buf, "DETACH") == 0) {
2352 if (hostapd_ctrl_iface_detach(hapd, from, fromlen))
2354 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
2355 if (hostapd_ctrl_iface_level(hapd, from, fromlen,
2358 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
2359 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
2361 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
2362 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
2364 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
2365 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
2367 } else if (os_strncmp(buf, "POLL_STA ", 9) == 0) {
2368 if (hostapd_ctrl_iface_poll_sta(hapd, buf + 9))
2370 } else if (os_strcmp(buf, "STOP_AP") == 0) {
2371 if (hostapd_ctrl_iface_stop_ap(hapd))
2373 #ifdef CONFIG_IEEE80211W
2375 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
2376 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
2378 #endif /* NEED_AP_MLME */
2379 #endif /* CONFIG_IEEE80211W */
2381 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
2382 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
2384 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
2385 reply_len = hostapd_ctrl_iface_wps_check_pin(
2386 hapd, buf + 14, reply, reply_size);
2387 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
2388 if (hostapd_wps_button_pushed(hapd, NULL))
2390 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
2391 if (hostapd_wps_cancel(hapd))
2393 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
2394 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
2396 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
2397 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
2399 } else if (os_strncmp(buf, "WPS_GET_STATUS", 13) == 0) {
2400 reply_len = hostapd_ctrl_iface_wps_get_status(hapd, reply,
2402 #ifdef CONFIG_WPS_NFC
2403 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
2404 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
2406 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
2407 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
2408 hapd, buf + 21, reply, reply_size);
2409 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
2410 reply_len = hostapd_ctrl_iface_wps_nfc_token(
2411 hapd, buf + 14, reply, reply_size);
2412 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
2413 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
2414 hapd, buf + 21, reply, reply_size);
2415 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
2416 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
2418 #endif /* CONFIG_WPS_NFC */
2419 #endif /* CONFIG_WPS */
2420 #ifdef CONFIG_INTERWORKING
2421 } else if (os_strncmp(buf, "SET_QOS_MAP_SET ", 16) == 0) {
2422 if (hostapd_ctrl_iface_set_qos_map_set(hapd, buf + 16))
2424 } else if (os_strncmp(buf, "SEND_QOS_MAP_CONF ", 18) == 0) {
2425 if (hostapd_ctrl_iface_send_qos_map_conf(hapd, buf + 18))
2427 #endif /* CONFIG_INTERWORKING */
2429 } else if (os_strncmp(buf, "HS20_WNM_NOTIF ", 15) == 0) {
2430 if (hostapd_ctrl_iface_hs20_wnm_notif(hapd, buf + 15))
2432 } else if (os_strncmp(buf, "HS20_DEAUTH_REQ ", 16) == 0) {
2433 if (hostapd_ctrl_iface_hs20_deauth_req(hapd, buf + 16))
2435 #endif /* CONFIG_HS20 */
2437 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
2438 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
2440 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
2441 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
2443 } else if (os_strncmp(buf, "BSS_TM_REQ ", 11) == 0) {
2444 if (hostapd_ctrl_iface_bss_tm_req(hapd, buf + 11))
2446 #endif /* CONFIG_WNM */
2447 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
2448 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
2450 } else if (os_strncmp(buf, "SET ", 4) == 0) {
2451 if (hostapd_ctrl_iface_set(hapd, buf + 4))
2453 } else if (os_strncmp(buf, "GET ", 4) == 0) {
2454 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
2456 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
2457 if (hostapd_ctrl_iface_enable(hapd->iface))
2459 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
2460 if (hostapd_ctrl_iface_reload(hapd->iface))
2462 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
2463 if (hostapd_ctrl_iface_disable(hapd->iface))
2465 } else if (os_strcmp(buf, "UPDATE_BEACON") == 0) {
2466 if (ieee802_11_set_beacon(hapd))
2468 #ifdef CONFIG_TESTING_OPTIONS
2469 } else if (os_strncmp(buf, "RADAR ", 6) == 0) {
2470 if (hostapd_ctrl_iface_radar(hapd, buf + 6))
2472 } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
2473 if (hostapd_ctrl_iface_mgmt_tx(hapd, buf + 8))
2475 } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
2476 if (hostapd_ctrl_iface_eapol_rx(hapd, buf + 9) < 0)
2478 } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
2479 if (hostapd_ctrl_iface_data_test_config(hapd, buf + 17) < 0)
2481 } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
2482 if (hostapd_ctrl_iface_data_test_tx(hapd, buf + 13) < 0)
2484 } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
2485 if (hostapd_ctrl_iface_data_test_frame(hapd, buf + 16) < 0)
2487 } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
2488 if (hostapd_ctrl_test_alloc_fail(hapd, buf + 16) < 0)
2490 } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
2491 reply_len = hostapd_ctrl_get_alloc_fail(hapd, reply,
2493 } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
2494 if (hostapd_ctrl_test_fail(hapd, buf + 10) < 0)
2496 } else if (os_strcmp(buf, "GET_FAIL") == 0) {
2497 reply_len = hostapd_ctrl_get_fail(hapd, reply, reply_size);
2498 #endif /* CONFIG_TESTING_OPTIONS */
2499 } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
2500 if (hostapd_ctrl_iface_chan_switch(hapd->iface, buf + 12))
2502 } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
2503 reply_len = hostapd_ctrl_iface_vendor(hapd, buf + 7, reply,
2505 } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
2506 ieee802_1x_erp_flush(hapd);
2507 #ifdef RADIUS_SERVER
2508 radius_server_erp_flush(hapd->radius_srv);
2509 #endif /* RADIUS_SERVER */
2510 } else if (os_strncmp(buf, "EAPOL_REAUTH ", 13) == 0) {
2511 if (hostapd_ctrl_iface_eapol_reauth(hapd, buf + 13))
2513 } else if (os_strncmp(buf, "EAPOL_SET ", 10) == 0) {
2514 if (hostapd_ctrl_iface_eapol_set(hapd, buf + 10))
2516 } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
2517 reply_len = hostapd_ctrl_iface_log_level(
2518 hapd, buf + 9, reply, reply_size);
2520 } else if (os_strcmp(buf, "TRACK_STA_LIST") == 0) {
2521 reply_len = hostapd_ctrl_iface_track_sta_list(
2522 hapd, reply, reply_size);
2523 #endif /* NEED_AP_MLME */
2524 } else if (os_strcmp(buf, "PMKSA") == 0) {
2525 reply_len = hostapd_ctrl_iface_pmksa_list(hapd, reply,
2527 } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
2528 hostapd_ctrl_iface_pmksa_flush(hapd);
2529 } else if (os_strncmp(buf, "SET_NEIGHBOR ", 13) == 0) {
2530 if (hostapd_ctrl_iface_set_neighbor(hapd, buf + 13))
2532 } else if (os_strncmp(buf, "REMOVE_NEIGHBOR ", 16) == 0) {
2533 if (hostapd_ctrl_iface_remove_neighbor(hapd, buf + 16))
2535 } else if (os_strncmp(buf, "REQ_LCI ", 8) == 0) {
2536 if (hostapd_ctrl_iface_req_lci(hapd, buf + 8))
2538 } else if (os_strncmp(buf, "REQ_RANGE ", 10) == 0) {
2539 if (hostapd_ctrl_iface_req_range(hapd, buf + 10))
2541 } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
2542 reply_len = hostapd_ctrl_driver_flags(hapd->iface, reply,
2545 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
2549 if (reply_len < 0) {
2550 os_memcpy(reply, "FAIL\n", 5);
2558 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
2561 struct hostapd_data *hapd = eloop_ctx;
2564 struct sockaddr_storage from;
2565 socklen_t fromlen = sizeof(from);
2566 char *reply, *pos = buf;
2567 const int reply_size = 4096;
2569 int level = MSG_DEBUG;
2570 #ifdef CONFIG_CTRL_IFACE_UDP
2571 unsigned char lcookie[COOKIE_LEN];
2572 #endif /* CONFIG_CTRL_IFACE_UDP */
2574 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
2575 (struct sockaddr *) &from, &fromlen);
2577 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
2583 reply = os_malloc(reply_size);
2584 if (reply == NULL) {
2585 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
2587 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2593 #ifdef CONFIG_CTRL_IFACE_UDP
2594 if (os_strcmp(buf, "GET_COOKIE") == 0) {
2595 os_memcpy(reply, "COOKIE=", 7);
2596 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
2597 cookie, COOKIE_LEN);
2598 reply_len = 7 + 2 * COOKIE_LEN;
2602 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
2603 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
2604 wpa_printf(MSG_DEBUG,
2605 "CTRL: No cookie in the request - drop request");
2610 if (os_memcmp(cookie, lcookie, COOKIE_LEN) != 0) {
2611 wpa_printf(MSG_DEBUG,
2612 "CTRL: Invalid cookie in the request - drop request");
2617 pos = buf + 7 + 2 * COOKIE_LEN;
2620 #endif /* CONFIG_CTRL_IFACE_UDP */
2622 if (os_strcmp(pos, "PING") == 0)
2623 level = MSG_EXCESSIVE;
2624 wpa_hexdump_ascii(level, "RX ctrl_iface", pos, res);
2626 reply_len = hostapd_ctrl_iface_receive_process(hapd, pos,
2630 #ifdef CONFIG_CTRL_IFACE_UDP
2632 #endif /* CONFIG_CTRL_IFACE_UDP */
2633 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
2635 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
2642 #ifndef CONFIG_CTRL_IFACE_UDP
2643 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
2648 if (hapd->conf->ctrl_interface == NULL)
2651 len = os_strlen(hapd->conf->ctrl_interface) +
2652 os_strlen(hapd->conf->iface) + 2;
2653 buf = os_malloc(len);
2657 os_snprintf(buf, len, "%s/%s",
2658 hapd->conf->ctrl_interface, hapd->conf->iface);
2659 buf[len - 1] = '\0';
2662 #endif /* CONFIG_CTRL_IFACE_UDP */
2665 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
2666 enum wpa_msg_type type,
2667 const char *txt, size_t len)
2669 struct hostapd_data *hapd = ctx;
2672 hostapd_ctrl_iface_send(hapd, level, type, txt, len);
2676 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
2678 #ifdef CONFIG_CTRL_IFACE_UDP
2679 int port = HOSTAPD_CTRL_IFACE_PORT;
2681 char port_str[40], *tmp;
2683 struct addrinfo hints = { 0 }, *res, *saveres;
2686 if (hapd->ctrl_sock > -1) {
2687 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2691 if (hapd->conf->ctrl_interface == NULL)
2694 pos = os_strstr(hapd->conf->ctrl_interface, "udp:");
2699 wpa_printf(MSG_ERROR, "Invalid ctrl_iface UDP port");
2704 dl_list_init(&hapd->ctrl_dst);
2705 hapd->ctrl_sock = -1;
2706 os_get_random(cookie, COOKIE_LEN);
2708 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
2709 hints.ai_flags = AI_PASSIVE;
2710 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
2712 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
2713 hints.ai_family = AF_INET6;
2714 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
2715 hints.ai_family = AF_INET;
2716 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
2717 hints.ai_socktype = SOCK_DGRAM;
2720 os_snprintf(p, sizeof(p), "%d", port);
2721 n = getaddrinfo(NULL, p, &hints, &res);
2723 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
2728 hapd->ctrl_sock = socket(res->ai_family, res->ai_socktype,
2730 if (hapd->ctrl_sock < 0) {
2731 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
2735 if (bind(hapd->ctrl_sock, res->ai_addr, res->ai_addrlen) < 0) {
2737 if ((HOSTAPD_CTRL_IFACE_PORT - port) <
2738 HOSTAPD_CTRL_IFACE_PORT_LIMIT && !pos)
2740 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
2744 freeaddrinfo(saveres);
2746 os_snprintf(port_str, sizeof(port_str), "udp:%d", port);
2747 tmp = os_strdup(port_str);
2749 os_free(hapd->conf->ctrl_interface);
2750 hapd->conf->ctrl_interface = tmp;
2752 wpa_printf(MSG_DEBUG, "ctrl_iface_init UDP port: %d", port);
2754 if (eloop_register_read_sock(hapd->ctrl_sock,
2755 hostapd_ctrl_iface_receive, hapd, NULL) <
2757 hostapd_ctrl_iface_deinit(hapd);
2761 hapd->msg_ctx = hapd;
2762 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
2767 if (hapd->ctrl_sock >= 0)
2768 close(hapd->ctrl_sock);
2770 #else /* CONFIG_CTRL_IFACE_UDP */
2771 struct sockaddr_un addr;
2775 if (hapd->ctrl_sock > -1) {
2776 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
2780 dl_list_init(&hapd->ctrl_dst);
2782 if (hapd->conf->ctrl_interface == NULL)
2785 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2786 if (errno == EEXIST) {
2787 wpa_printf(MSG_DEBUG, "Using existing control "
2788 "interface directory.");
2790 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
2796 if (hapd->conf->ctrl_interface_gid_set &&
2797 chown(hapd->conf->ctrl_interface, -1,
2798 hapd->conf->ctrl_interface_gid) < 0) {
2799 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2804 if (!hapd->conf->ctrl_interface_gid_set &&
2805 hapd->iface->interfaces->ctrl_iface_group &&
2806 chown(hapd->conf->ctrl_interface, -1,
2807 hapd->iface->interfaces->ctrl_iface_group) < 0) {
2808 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
2815 * Android is using umask 0077 which would leave the control interface
2816 * directory without group access. This breaks things since Wi-Fi
2817 * framework assumes that this directory can be accessed by other
2818 * applications in the wifi group. Fix this by adding group access even
2819 * if umask value would prevent this.
2821 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
2822 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
2824 /* Try to continue anyway */
2826 #endif /* ANDROID */
2828 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
2829 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
2832 s = socket(PF_UNIX, SOCK_DGRAM, 0);
2834 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
2838 os_memset(&addr, 0, sizeof(addr));
2840 addr.sun_len = sizeof(addr);
2841 #endif /* __FreeBSD__ */
2842 addr.sun_family = AF_UNIX;
2843 fname = hostapd_ctrl_iface_path(hapd);
2846 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
2847 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2848 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
2850 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2851 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
2852 " allow connections - assuming it was left"
2853 "over from forced program termination");
2854 if (unlink(fname) < 0) {
2855 wpa_printf(MSG_ERROR,
2856 "Could not unlink existing ctrl_iface socket '%s': %s",
2857 fname, strerror(errno));
2860 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
2862 wpa_printf(MSG_ERROR,
2863 "hostapd-ctrl-iface: bind(PF_UNIX): %s",
2867 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
2868 "ctrl_iface socket '%s'", fname);
2870 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
2871 "be in use - cannot override it");
2872 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
2873 "not used anymore", fname);
2880 if (hapd->conf->ctrl_interface_gid_set &&
2881 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
2882 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2887 if (!hapd->conf->ctrl_interface_gid_set &&
2888 hapd->iface->interfaces->ctrl_iface_group &&
2889 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
2890 wpa_printf(MSG_ERROR, "chown[ctrl_interface/ifname]: %s",
2895 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
2896 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
2902 hapd->ctrl_sock = s;
2903 if (eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
2905 hostapd_ctrl_iface_deinit(hapd);
2908 hapd->msg_ctx = hapd;
2909 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
2921 #endif /* CONFIG_CTRL_IFACE_UDP */
2925 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
2927 struct wpa_ctrl_dst *dst, *prev;
2929 if (hapd->ctrl_sock > -1) {
2930 #ifndef CONFIG_CTRL_IFACE_UDP
2932 #endif /* !CONFIG_CTRL_IFACE_UDP */
2934 eloop_unregister_read_sock(hapd->ctrl_sock);
2935 close(hapd->ctrl_sock);
2936 hapd->ctrl_sock = -1;
2937 #ifndef CONFIG_CTRL_IFACE_UDP
2938 fname = hostapd_ctrl_iface_path(hapd);
2943 if (hapd->conf->ctrl_interface &&
2944 rmdir(hapd->conf->ctrl_interface) < 0) {
2945 if (errno == ENOTEMPTY) {
2946 wpa_printf(MSG_DEBUG, "Control interface "
2947 "directory not empty - leaving it "
2950 wpa_printf(MSG_ERROR,
2951 "rmdir[ctrl_interface=%s]: %s",
2952 hapd->conf->ctrl_interface,
2956 #endif /* !CONFIG_CTRL_IFACE_UDP */
2959 dl_list_for_each_safe(dst, prev, &hapd->ctrl_dst, struct wpa_ctrl_dst,
2963 #ifdef CONFIG_TESTING_OPTIONS
2964 l2_packet_deinit(hapd->l2_test);
2965 hapd->l2_test = NULL;
2966 #endif /* CONFIG_TESTING_OPTIONS */
2970 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
2973 if (hostapd_add_iface(interfaces, buf) < 0) {
2974 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
2981 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
2984 if (hostapd_remove_iface(interfaces, buf) < 0) {
2985 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
2992 static int hostapd_global_ctrl_iface_attach(struct hapd_interfaces *interfaces,
2993 struct sockaddr_storage *from,
2996 return ctrl_iface_attach(&interfaces->global_ctrl_dst, from, fromlen);
3000 static int hostapd_global_ctrl_iface_detach(struct hapd_interfaces *interfaces,
3001 struct sockaddr_storage *from,
3004 return ctrl_iface_detach(&interfaces->global_ctrl_dst, from, fromlen);
3008 static void hostapd_ctrl_iface_flush(struct hapd_interfaces *interfaces)
3010 #ifdef CONFIG_WPS_TESTING
3011 wps_version_number = 0x20;
3012 wps_testing_dummy_cred = 0;
3013 wps_corrupt_pkhash = 0;
3014 #endif /* CONFIG_WPS_TESTING */
3021 hostapd_global_ctrl_iface_fst_attach(struct hapd_interfaces *interfaces,
3024 char ifname[IFNAMSIZ + 1];
3025 struct fst_iface_cfg cfg;
3026 struct hostapd_data *hapd;
3027 struct fst_wpa_obj iface_obj;
3029 if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
3030 hapd = hostapd_get_iface(interfaces, ifname);
3032 if (hapd->iface->fst) {
3033 wpa_printf(MSG_INFO, "FST: Already attached");
3036 fst_hostapd_fill_iface_obj(hapd, &iface_obj);
3037 hapd->iface->fst = fst_attach(ifname, hapd->own_addr,
3039 if (hapd->iface->fst)
3049 hostapd_global_ctrl_iface_fst_detach(struct hapd_interfaces *interfaces,
3052 char ifname[IFNAMSIZ + 1];
3053 struct hostapd_data * hapd;
3055 if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
3056 hapd = hostapd_get_iface(interfaces, ifname);
3058 if (!fst_iface_detach(ifname)) {
3059 hapd->iface->fst = NULL;
3060 hapd->iface->fst_ies = NULL;
3069 #endif /* CONFIG_FST */
3072 static struct hostapd_data *
3073 hostapd_interfaces_get_hapd(struct hapd_interfaces *interfaces,
3078 for (i = 0; i < interfaces->count; i++) {
3079 struct hostapd_iface *iface = interfaces->iface[i];
3081 for (j = 0; j < iface->num_bss; j++) {
3082 struct hostapd_data *hapd;
3084 hapd = iface->bss[j];
3085 if (os_strcmp(ifname, hapd->conf->iface) == 0)
3094 static int hostapd_ctrl_iface_dup_param(struct hostapd_data *src_hapd,
3095 struct hostapd_data *dst_hapd,
3101 value = os_zalloc(HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
3103 wpa_printf(MSG_ERROR,
3104 "DUP: cannot allocate buffer to stringify %s",
3109 if (os_strcmp(param, "wpa") == 0) {
3110 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%d",
3111 src_hapd->conf->wpa);
3112 } else if (os_strcmp(param, "wpa_key_mgmt") == 0 &&
3113 src_hapd->conf->wpa_key_mgmt) {
3114 res = hostapd_ctrl_iface_get_key_mgmt(
3115 src_hapd, value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN);
3116 if (os_snprintf_error(HOSTAPD_CLI_DUP_VALUE_MAX_LEN, res))
3117 goto error_stringify;
3118 } else if (os_strcmp(param, "wpa_pairwise") == 0 &&
3119 src_hapd->conf->wpa_pairwise) {
3120 res = wpa_write_ciphers(value,
3121 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3122 src_hapd->conf->wpa_pairwise, " ");
3124 goto error_stringify;
3125 } else if (os_strcmp(param, "rsn_pairwise") == 0 &&
3126 src_hapd->conf->rsn_pairwise) {
3127 res = wpa_write_ciphers(value,
3128 value + HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3129 src_hapd->conf->rsn_pairwise, " ");
3131 goto error_stringify;
3132 } else if (os_strcmp(param, "wpa_passphrase") == 0 &&
3133 src_hapd->conf->ssid.wpa_passphrase) {
3134 os_snprintf(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN, "%s",
3135 src_hapd->conf->ssid.wpa_passphrase);
3136 } else if (os_strcmp(param, "wpa_psk") == 0 &&
3137 src_hapd->conf->ssid.wpa_psk_set) {
3138 wpa_snprintf_hex(value, HOSTAPD_CLI_DUP_VALUE_MAX_LEN,
3139 src_hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
3141 wpa_printf(MSG_WARNING, "DUP: %s cannot be duplicated", param);
3145 res = hostapd_set_iface(dst_hapd->iconf, dst_hapd->conf, param, value);
3150 wpa_printf(MSG_ERROR, "DUP: cannot stringify %s", param);
3158 hostapd_global_ctrl_iface_interfaces(struct hapd_interfaces *interfaces,
3160 char *reply, int reply_size)
3165 struct hostapd_iface *iface;
3169 show_ctrl = !!os_strstr(input, "ctrl");
3172 end = reply + reply_size;
3174 for (i = 0; i < interfaces->count; i++) {
3175 iface = interfaces->iface[i];
3177 for (j = 0; j < iface->num_bss; j++) {
3178 struct hostapd_bss_config *conf;
3180 conf = iface->conf->bss[j];
3182 res = os_snprintf(pos, end - pos,
3183 "%s ctrl_iface=%s\n",
3185 conf->ctrl_interface ?
3186 conf->ctrl_interface : "N/A");
3188 res = os_snprintf(pos, end - pos, "%s\n",
3190 if (os_snprintf_error(end - pos, res)) {
3203 hostapd_global_ctrl_iface_dup_network(struct hapd_interfaces *interfaces,
3206 char *p_start = cmd, *p_end;
3207 struct hostapd_data *src_hapd, *dst_hapd;
3209 /* cmd: "<src ifname> <dst ifname> <variable name> */
3211 p_end = os_strchr(p_start, ' ');
3213 wpa_printf(MSG_ERROR, "DUP: no src ifname found in cmd: '%s'",
3219 src_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
3221 wpa_printf(MSG_ERROR, "DUP: no src ifname found: '%s'",
3226 p_start = p_end + 1;
3227 p_end = os_strchr(p_start, ' ');
3229 wpa_printf(MSG_ERROR, "DUP: no dst ifname found in cmd: '%s'",
3235 dst_hapd = hostapd_interfaces_get_hapd(interfaces, p_start);
3237 wpa_printf(MSG_ERROR, "DUP: no dst ifname found: '%s'",
3242 p_start = p_end + 1;
3243 return hostapd_ctrl_iface_dup_param(src_hapd, dst_hapd, p_start);
3247 static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
3249 char *buf, char *reply,
3251 struct sockaddr_storage *from,
3254 struct hostapd_data *hapd;
3256 hapd = hostapd_interfaces_get_hapd(interfaces, ifname);
3260 res = os_snprintf(reply, reply_size, "FAIL-NO-IFNAME-MATCH\n");
3261 if (os_snprintf_error(reply_size, res))
3266 return hostapd_ctrl_iface_receive_process(hapd, buf, reply,reply_size,
3271 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
3274 void *interfaces = eloop_ctx;
3275 char buffer[256], *buf = buffer;
3277 struct sockaddr_storage from;
3278 socklen_t fromlen = sizeof(from);
3281 const int reply_size = 4096;
3282 #ifdef CONFIG_CTRL_IFACE_UDP
3283 unsigned char lcookie[COOKIE_LEN];
3284 #endif /* CONFIG_CTRL_IFACE_UDP */
3286 res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
3287 (struct sockaddr *) &from, &fromlen);
3289 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
3294 wpa_printf(MSG_DEBUG, "Global ctrl_iface command: %s", buf);
3296 reply = os_malloc(reply_size);
3297 if (reply == NULL) {
3298 if (sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
3300 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3306 os_memcpy(reply, "OK\n", 3);
3309 #ifdef CONFIG_CTRL_IFACE_UDP
3310 if (os_strcmp(buf, "GET_COOKIE") == 0) {
3311 os_memcpy(reply, "COOKIE=", 7);
3312 wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1,
3313 gcookie, COOKIE_LEN);
3314 reply_len = 7 + 2 * COOKIE_LEN;
3318 if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
3319 hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) {
3320 wpa_printf(MSG_DEBUG,
3321 "CTRL: No cookie in the request - drop request");
3326 if (os_memcmp(gcookie, lcookie, COOKIE_LEN) != 0) {
3327 wpa_printf(MSG_DEBUG,
3328 "CTRL: Invalid cookie in the request - drop request");
3333 buf += 7 + 2 * COOKIE_LEN;
3336 #endif /* CONFIG_CTRL_IFACE_UDP */
3338 if (os_strncmp(buf, "IFNAME=", 7) == 0) {
3339 char *pos = os_strchr(buf + 7, ' ');
3343 reply_len = hostapd_global_ctrl_iface_ifname(
3344 interfaces, buf + 7, pos, reply, reply_size,
3350 if (os_strcmp(buf, "PING") == 0) {
3351 os_memcpy(reply, "PONG\n", 5);
3353 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
3354 if (wpa_debug_reopen_file() < 0)
3356 } else if (os_strcmp(buf, "FLUSH") == 0) {
3357 hostapd_ctrl_iface_flush(interfaces);
3358 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
3359 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
3361 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
3362 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
3364 } else if (os_strcmp(buf, "ATTACH") == 0) {
3365 if (hostapd_global_ctrl_iface_attach(interfaces, &from,
3368 } else if (os_strcmp(buf, "DETACH") == 0) {
3369 if (hostapd_global_ctrl_iface_detach(interfaces, &from,
3372 #ifdef CONFIG_MODULE_TESTS
3373 } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
3374 if (hapd_module_tests() < 0)
3376 #endif /* CONFIG_MODULE_TESTS */
3378 } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
3379 if (!hostapd_global_ctrl_iface_fst_attach(interfaces, buf + 11))
3380 reply_len = os_snprintf(reply, reply_size, "OK\n");
3383 } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
3384 if (!hostapd_global_ctrl_iface_fst_detach(interfaces, buf + 11))
3385 reply_len = os_snprintf(reply, reply_size, "OK\n");
3388 } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
3389 reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
3390 #endif /* CONFIG_FST */
3391 } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
3392 if (!hostapd_global_ctrl_iface_dup_network(interfaces,
3394 reply_len = os_snprintf(reply, reply_size, "OK\n");
3397 } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
3398 reply_len = hostapd_global_ctrl_iface_interfaces(
3399 interfaces, buf + 10, reply, sizeof(buffer));
3400 } else if (os_strcmp(buf, "TERMINATE") == 0) {
3403 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
3409 if (reply_len < 0) {
3410 os_memcpy(reply, "FAIL\n", 5);
3414 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
3416 wpa_printf(MSG_DEBUG, "CTRL: sendto failed: %s",
3423 #ifndef CONFIG_CTRL_IFACE_UDP
3424 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
3429 if (interface->global_iface_path == NULL)
3432 len = os_strlen(interface->global_iface_path) +
3433 os_strlen(interface->global_iface_name) + 2;
3434 buf = os_malloc(len);
3438 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
3439 interface->global_iface_name);
3440 buf[len - 1] = '\0';
3443 #endif /* CONFIG_CTRL_IFACE_UDP */
3446 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
3448 #ifdef CONFIG_CTRL_IFACE_UDP
3449 int port = HOSTAPD_GLOBAL_CTRL_IFACE_PORT;
3452 struct addrinfo hints = { 0 }, *res, *saveres;
3455 if (interface->global_ctrl_sock > -1) {
3456 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
3460 if (interface->global_iface_path == NULL)
3463 pos = os_strstr(interface->global_iface_path, "udp:");
3468 wpa_printf(MSG_ERROR, "Invalid global ctrl UDP port");
3473 dl_list_init(&interface->global_ctrl_dst);
3474 interface->global_ctrl_sock = -1;
3475 os_get_random(gcookie, COOKIE_LEN);
3477 #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
3478 hints.ai_flags = AI_PASSIVE;
3479 #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
3481 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
3482 hints.ai_family = AF_INET6;
3483 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3484 hints.ai_family = AF_INET;
3485 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
3486 hints.ai_socktype = SOCK_DGRAM;
3489 os_snprintf(p, sizeof(p), "%d", port);
3490 n = getaddrinfo(NULL, p, &hints, &res);
3492 wpa_printf(MSG_ERROR, "getaddrinfo(): %s", gai_strerror(n));
3497 interface->global_ctrl_sock = socket(res->ai_family, res->ai_socktype,
3499 if (interface->global_ctrl_sock < 0) {
3500 wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno));
3504 if (bind(interface->global_ctrl_sock, res->ai_addr, res->ai_addrlen) <
3507 if ((port - HOSTAPD_GLOBAL_CTRL_IFACE_PORT) <
3508 HOSTAPD_GLOBAL_CTRL_IFACE_PORT_LIMIT && !pos)
3510 wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno));
3514 freeaddrinfo(saveres);
3516 wpa_printf(MSG_DEBUG, "global ctrl_iface_init UDP port: %d", port);
3518 if (eloop_register_read_sock(interface->global_ctrl_sock,
3519 hostapd_global_ctrl_iface_receive,
3520 interface, NULL) < 0) {
3521 hostapd_global_ctrl_iface_deinit(interface);
3528 if (interface->global_ctrl_sock >= 0)
3529 close(interface->global_ctrl_sock);
3531 #else /* CONFIG_CTRL_IFACE_UDP */
3532 struct sockaddr_un addr;
3536 if (interface->global_iface_path == NULL) {
3537 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
3541 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
3542 if (errno == EEXIST) {
3543 wpa_printf(MSG_DEBUG, "Using existing control "
3544 "interface directory.");
3546 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface]: %s",
3550 } else if (interface->ctrl_iface_group &&
3551 chown(interface->global_iface_path, -1,
3552 interface->ctrl_iface_group) < 0) {
3553 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3558 if (os_strlen(interface->global_iface_path) + 1 +
3559 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
3562 s = socket(PF_UNIX, SOCK_DGRAM, 0);
3564 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
3568 os_memset(&addr, 0, sizeof(addr));
3570 addr.sun_len = sizeof(addr);
3571 #endif /* __FreeBSD__ */
3572 addr.sun_family = AF_UNIX;
3573 fname = hostapd_global_ctrl_iface_path(interface);
3576 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
3577 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3578 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
3580 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
3581 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
3582 " allow connections - assuming it was left"
3583 "over from forced program termination");
3584 if (unlink(fname) < 0) {
3585 wpa_printf(MSG_ERROR,
3586 "Could not unlink existing ctrl_iface socket '%s': %s",
3587 fname, strerror(errno));
3590 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
3592 wpa_printf(MSG_ERROR, "bind(PF_UNIX): %s",
3596 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
3597 "ctrl_iface socket '%s'", fname);
3599 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
3600 "be in use - cannot override it");
3601 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
3602 "not used anymore", fname);
3609 if (interface->ctrl_iface_group &&
3610 chown(fname, -1, interface->ctrl_iface_group) < 0) {
3611 wpa_printf(MSG_ERROR, "chown[ctrl_interface]: %s",
3616 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
3617 wpa_printf(MSG_ERROR, "chmod[ctrl_interface/ifname]: %s",
3623 interface->global_ctrl_sock = s;
3624 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
3637 #endif /* CONFIG_CTRL_IFACE_UDP */
3641 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
3643 #ifndef CONFIG_CTRL_IFACE_UDP
3645 #endif /* CONFIG_CTRL_IFACE_UDP */
3646 struct wpa_ctrl_dst *dst, *prev;
3648 if (interfaces->global_ctrl_sock > -1) {
3649 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
3650 close(interfaces->global_ctrl_sock);
3651 interfaces->global_ctrl_sock = -1;
3652 #ifndef CONFIG_CTRL_IFACE_UDP
3653 fname = hostapd_global_ctrl_iface_path(interfaces);
3659 if (interfaces->global_iface_path &&
3660 rmdir(interfaces->global_iface_path) < 0) {
3661 if (errno == ENOTEMPTY) {
3662 wpa_printf(MSG_DEBUG, "Control interface "
3663 "directory not empty - leaving it "
3666 wpa_printf(MSG_ERROR,
3667 "rmdir[ctrl_interface=%s]: %s",
3668 interfaces->global_iface_path,
3672 #endif /* CONFIG_CTRL_IFACE_UDP */
3675 os_free(interfaces->global_iface_path);
3676 interfaces->global_iface_path = NULL;
3678 dl_list_for_each_safe(dst, prev, &interfaces->global_ctrl_dst,
3679 struct wpa_ctrl_dst, list)
3684 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
3685 enum wpa_msg_type type,
3686 const char *buf, size_t len)
3688 struct wpa_ctrl_dst *dst, *next;
3689 struct dl_list *ctrl_dst;
3696 if (type != WPA_MSG_ONLY_GLOBAL) {
3697 s = hapd->ctrl_sock;
3698 ctrl_dst = &hapd->ctrl_dst;
3700 s = hapd->iface->interfaces->global_ctrl_sock;
3701 ctrl_dst = &hapd->iface->interfaces->global_ctrl_dst;
3704 if (s < 0 || dl_list_empty(ctrl_dst))
3707 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
3708 io[0].iov_base = levelstr;
3709 io[0].iov_len = os_strlen(levelstr);
3710 io[1].iov_base = (char *) buf;
3711 io[1].iov_len = len;
3712 os_memset(&msg, 0, sizeof(msg));
3717 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
3718 if (level >= dst->debug_level) {
3719 sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
3720 &dst->addr, dst->addrlen);
3721 msg.msg_name = &dst->addr;
3722 msg.msg_namelen = dst->addrlen;
3723 if (sendmsg(s, &msg, 0) < 0) {
3725 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
3727 idx, errno, strerror(errno));
3729 if (dst->errors > 10 || _errno == ENOENT) {
3730 if (type != WPA_MSG_ONLY_GLOBAL)
3731 hostapd_ctrl_iface_detach(
3735 hostapd_global_ctrl_iface_detach(
3736 hapd->iface->interfaces,
3747 #endif /* CONFIG_NATIVE_WINDOWS */