2 * hostapd / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2012, 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
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "common/version.h"
20 #include "common/ieee802_11_defs.h"
21 #include "drivers/driver.h"
22 #include "radius/radius_client.h"
23 #include "ap/hostapd.h"
24 #include "ap/ap_config.h"
25 #include "ap/ieee802_1x.h"
26 #include "ap/wpa_auth.h"
27 #include "ap/ieee802_11.h"
28 #include "ap/sta_info.h"
29 #include "ap/wps_hostapd.h"
30 #include "ap/ctrl_iface_ap.h"
31 #include "ap/ap_drv_ops.h"
32 #include "wps/wps_defs.h"
34 #include "config_file.h"
35 #include "ctrl_iface.h"
39 struct wpa_ctrl_dst *next;
40 struct sockaddr_un addr;
47 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
48 const char *buf, size_t len);
51 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
52 struct sockaddr_un *from,
55 struct wpa_ctrl_dst *dst;
57 dst = os_zalloc(sizeof(*dst));
60 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
61 dst->addrlen = fromlen;
62 dst->debug_level = MSG_INFO;
63 dst->next = hapd->ctrl_dst;
65 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
66 (u8 *) from->sun_path,
67 fromlen - offsetof(struct sockaddr_un, sun_path));
72 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
73 struct sockaddr_un *from,
76 struct wpa_ctrl_dst *dst, *prev = NULL;
80 if (fromlen == dst->addrlen &&
81 os_memcmp(from->sun_path, dst->addr.sun_path,
82 fromlen - offsetof(struct sockaddr_un, sun_path))
85 hapd->ctrl_dst = dst->next;
87 prev->next = dst->next;
89 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
90 (u8 *) from->sun_path,
92 offsetof(struct sockaddr_un, sun_path));
102 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
103 struct sockaddr_un *from,
107 struct wpa_ctrl_dst *dst;
109 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
111 dst = hapd->ctrl_dst;
113 if (fromlen == dst->addrlen &&
114 os_memcmp(from->sun_path, dst->addr.sun_path,
115 fromlen - offsetof(struct sockaddr_un, sun_path))
117 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
118 "level", (u8 *) from->sun_path, fromlen -
119 offsetof(struct sockaddr_un, sun_path));
120 dst->debug_level = atoi(level);
130 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
134 struct sta_info *sta;
136 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
138 if (hwaddr_aton(txtaddr, addr))
141 sta = ap_get_sta(hapd, addr);
145 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
146 "notification", MAC2STR(addr));
147 sta = ap_sta_add(hapd, addr);
151 hostapd_new_assoc_sta(hapd, sta, 0);
156 #ifdef CONFIG_IEEE80211W
158 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
162 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
164 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
166 if (hwaddr_aton(txtaddr, addr) ||
167 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
170 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
174 #endif /* NEED_AP_MLME */
175 #endif /* CONFIG_IEEE80211W */
179 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
181 char *pin = os_strchr(txt, ' ');
184 u8 addr_buf[ETH_ALEN], *addr = NULL;
191 timeout_txt = os_strchr(pin, ' ');
193 *timeout_txt++ = '\0';
194 timeout = atoi(timeout_txt);
195 pos = os_strchr(timeout_txt, ' ');
198 if (hwaddr_aton(pos, addr_buf) == 0)
204 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
208 static int hostapd_ctrl_iface_wps_check_pin(
209 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
216 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
217 (u8 *) cmd, os_strlen(cmd));
218 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
219 if (*pos < '0' || *pos > '9')
223 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
227 if (len != 4 && len != 8) {
228 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
234 unsigned int pin_val;
236 if (!wps_pin_valid(pin_val)) {
237 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
238 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
239 if (ret < 0 || (size_t) ret >= buflen)
245 ret = os_snprintf(buf, buflen, "%s", pin);
246 if (ret < 0 || (size_t) ret >= buflen)
253 #ifdef CONFIG_WPS_OOB
254 static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
256 char *path, *method, *name;
258 path = os_strchr(txt, ' ');
263 method = os_strchr(path, ' ');
268 name = os_strchr(method, ' ');
272 return hostapd_wps_start_oob(hapd, txt, path, method, name);
274 #endif /* CONFIG_WPS_OOB */
277 #ifdef CONFIG_WPS_NFC
278 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
285 len = os_strlen(pos);
290 buf = wpabuf_alloc(len);
293 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
298 ret = hostapd_wps_nfc_tag_read(hapd, buf);
305 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
306 char *cmd, char *reply,
313 if (os_strcmp(cmd, "WPS") == 0)
315 else if (os_strcmp(cmd, "NDEF") == 0)
320 buf = hostapd_wps_nfc_config_token(hapd, ndef);
324 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
335 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
336 char *reply, size_t max_len,
342 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
346 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
357 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
358 char *cmd, char *reply,
361 if (os_strcmp(cmd, "WPS") == 0)
362 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
365 if (os_strcmp(cmd, "NDEF") == 0)
366 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
369 if (os_strcmp(cmd, "enable") == 0)
370 return hostapd_wps_nfc_token_enable(hapd);
372 if (os_strcmp(cmd, "disable") == 0) {
373 hostapd_wps_nfc_token_disable(hapd);
379 #endif /* CONFIG_WPS_NFC */
382 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
383 char *buf, size_t buflen)
389 pos = os_strchr(txt, ' ');
393 if (os_strcmp(txt, "disable") == 0) {
394 hostapd_wps_ap_pin_disable(hapd);
395 return os_snprintf(buf, buflen, "OK\n");
398 if (os_strcmp(txt, "random") == 0) {
401 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
404 return os_snprintf(buf, buflen, "%s", pin_txt);
407 if (os_strcmp(txt, "get") == 0) {
408 pin_txt = hostapd_wps_ap_pin_get(hapd);
411 return os_snprintf(buf, buflen, "%s", pin_txt);
414 if (os_strcmp(txt, "set") == 0) {
419 pos = os_strchr(pos, ' ');
424 if (os_strlen(pin) > buflen)
426 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
428 return os_snprintf(buf, buflen, "%s", pin);
435 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
438 char *ssid, *auth, *encr = NULL, *key = NULL;
441 pos = os_strchr(txt, ' ');
447 pos = os_strchr(pos, ' ');
451 pos = os_strchr(pos, ' ');
458 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
460 #endif /* CONFIG_WPS */
463 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
469 struct ieee80211_mgmt *mgmt;
472 if (hwaddr_aton(cmd, addr))
478 url_len = os_strlen(url);
482 os_memset(buf, 0, sizeof(buf));
483 mgmt = (struct ieee80211_mgmt *) buf;
484 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
485 WLAN_FC_STYPE_ACTION);
486 os_memcpy(mgmt->da, addr, ETH_ALEN);
487 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
488 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
489 mgmt->u.action.category = WLAN_ACTION_WNM;
490 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
491 mgmt->u.action.u.bss_tm_req.dialog_token = 1;
492 mgmt->u.action.u.bss_tm_req.req_mode =
493 WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
494 mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0);
495 mgmt->u.action.u.bss_tm_req.validity_interval = 0;
497 pos = mgmt->u.action.u.bss_tm_req.variable;
499 /* Session Information URL */
501 os_memcpy(pos, url, url_len);
504 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
505 wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
506 "Management Request frame");
514 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
515 char *buf, size_t buflen)
523 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
525 MAC2STR(hapd->own_addr),
526 hapd->conf->ssid.ssid);
527 if (ret < 0 || ret >= end - pos)
532 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
533 hapd->conf->wps_state == 0 ? "disabled" :
534 (hapd->conf->wps_state == 1 ? "not configured" :
536 if (ret < 0 || ret >= end - pos)
540 if (hapd->conf->wps_state && hapd->conf->wpa &&
541 hapd->conf->ssid.wpa_passphrase) {
542 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
543 hapd->conf->ssid.wpa_passphrase);
544 if (ret < 0 || ret >= end - pos)
549 if (hapd->conf->wps_state && hapd->conf->wpa &&
550 hapd->conf->ssid.wpa_psk &&
551 hapd->conf->ssid.wpa_psk->group) {
552 char hex[PMK_LEN * 2 + 1];
553 wpa_snprintf_hex(hex, sizeof(hex),
554 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
555 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
556 if (ret < 0 || ret >= end - pos)
560 #endif /* CONFIG_WPS */
562 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
563 ret = os_snprintf(pos, end - pos, "key_mgmt=");
564 if (ret < 0 || ret >= end - pos)
568 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
569 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
570 if (ret < 0 || ret >= end - pos)
574 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
575 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
576 if (ret < 0 || ret >= end - pos)
580 #ifdef CONFIG_IEEE80211R
581 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
582 ret = os_snprintf(pos, end - pos, "FT-PSK ");
583 if (ret < 0 || ret >= end - pos)
587 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
588 ret = os_snprintf(pos, end - pos, "FT-EAP ");
589 if (ret < 0 || ret >= end - pos)
593 #endif /* CONFIG_IEEE80211R */
594 #ifdef CONFIG_IEEE80211W
595 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
596 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
597 if (ret < 0 || ret >= end - pos)
601 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
602 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
603 if (ret < 0 || ret >= end - pos)
607 #endif /* CONFIG_IEEE80211W */
609 ret = os_snprintf(pos, end - pos, "\n");
610 if (ret < 0 || ret >= end - pos)
615 if (hapd->conf->wpa && hapd->conf->wpa_group == WPA_CIPHER_CCMP) {
616 ret = os_snprintf(pos, end - pos, "group_cipher=CCMP\n");
617 if (ret < 0 || ret >= end - pos)
620 } else if (hapd->conf->wpa &&
621 hapd->conf->wpa_group == WPA_CIPHER_TKIP) {
622 ret = os_snprintf(pos, end - pos, "group_cipher=TKIP\n");
623 if (ret < 0 || ret >= end - pos)
628 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
629 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
630 if (ret < 0 || ret >= end - pos)
634 if (hapd->conf->rsn_pairwise & WPA_CIPHER_CCMP) {
635 ret = os_snprintf(pos, end - pos, "CCMP ");
636 if (ret < 0 || ret >= end - pos)
640 if (hapd->conf->rsn_pairwise & WPA_CIPHER_TKIP) {
641 ret = os_snprintf(pos, end - pos, "TKIP ");
642 if (ret < 0 || ret >= end - pos)
647 ret = os_snprintf(pos, end - pos, "\n");
648 if (ret < 0 || ret >= end - pos)
653 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
654 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
655 if (ret < 0 || ret >= end - pos)
659 if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) {
660 ret = os_snprintf(pos, end - pos, "CCMP ");
661 if (ret < 0 || ret >= end - pos)
665 if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) {
666 ret = os_snprintf(pos, end - pos, "TKIP ");
667 if (ret < 0 || ret >= end - pos)
672 ret = os_snprintf(pos, end - pos, "\n");
673 if (ret < 0 || ret >= end - pos)
682 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
687 value = os_strchr(cmd, ' ');
692 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
694 #ifdef CONFIG_WPS_TESTING
695 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
697 val = strtol(value, NULL, 0);
698 if (val < 0 || val > 0xff) {
700 wpa_printf(MSG_DEBUG, "WPS: Invalid "
701 "wps_version_number %ld", val);
703 wps_version_number = val;
704 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
706 (wps_version_number & 0xf0) >> 4,
707 wps_version_number & 0x0f);
708 hostapd_wps_update_ie(hapd);
710 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
711 wps_testing_dummy_cred = atoi(value);
712 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
713 wps_testing_dummy_cred);
714 #endif /* CONFIG_WPS_TESTING */
715 #ifdef CONFIG_INTERWORKING
716 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
717 int val = atoi(value);
721 hapd->gas_frag_limit = val;
722 #endif /* CONFIG_INTERWORKING */
724 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
731 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
732 char *buf, size_t buflen)
736 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
738 if (os_strcmp(cmd, "version") == 0) {
739 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
740 if (res < 0 || (unsigned int) res >= buflen)
749 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
752 struct hostapd_data *hapd = eloop_ctx;
755 struct sockaddr_un from;
756 socklen_t fromlen = sizeof(from);
758 const int reply_size = 4096;
760 int level = MSG_DEBUG;
762 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
763 (struct sockaddr *) &from, &fromlen);
765 perror("recvfrom(ctrl_iface)");
769 if (os_strcmp(buf, "PING") == 0)
770 level = MSG_EXCESSIVE;
771 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
773 reply = os_malloc(reply_size);
775 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
780 os_memcpy(reply, "OK\n", 3);
783 if (os_strcmp(buf, "PING") == 0) {
784 os_memcpy(reply, "PONG\n", 5);
786 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
787 if (wpa_debug_reopen_file() < 0)
789 } else if (os_strcmp(buf, "MIB") == 0) {
790 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
791 if (reply_len >= 0) {
792 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
793 reply_size - reply_len);
799 if (reply_len >= 0) {
800 res = ieee802_1x_get_mib(hapd, reply + reply_len,
801 reply_size - reply_len);
807 #ifndef CONFIG_NO_RADIUS
808 if (reply_len >= 0) {
809 res = radius_client_get_mib(hapd->radius,
811 reply_size - reply_len);
817 #endif /* CONFIG_NO_RADIUS */
818 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
819 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
821 } else if (os_strncmp(buf, "STA ", 4) == 0) {
822 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
824 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
825 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
827 } else if (os_strcmp(buf, "ATTACH") == 0) {
828 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
830 } else if (os_strcmp(buf, "DETACH") == 0) {
831 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
833 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
834 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
837 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
838 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
840 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
841 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
843 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
844 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
846 #ifdef CONFIG_IEEE80211W
848 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
849 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
851 #endif /* NEED_AP_MLME */
852 #endif /* CONFIG_IEEE80211W */
854 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
855 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
857 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
858 reply_len = hostapd_ctrl_iface_wps_check_pin(
859 hapd, buf + 14, reply, reply_size);
860 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
861 if (hostapd_wps_button_pushed(hapd, NULL))
863 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
864 if (hostapd_wps_cancel(hapd))
866 #ifdef CONFIG_WPS_OOB
867 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
868 if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
870 #endif /* CONFIG_WPS_OOB */
871 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
872 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
874 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
875 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
877 #ifdef CONFIG_WPS_NFC
878 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
879 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
881 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
882 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
883 hapd, buf + 21, reply, reply_size);
884 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
885 reply_len = hostapd_ctrl_iface_wps_nfc_token(
886 hapd, buf + 14, reply, reply_size);
887 #endif /* CONFIG_WPS_NFC */
888 #endif /* CONFIG_WPS */
889 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
890 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
892 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
893 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
895 } else if (os_strncmp(buf, "SET ", 4) == 0) {
896 if (hostapd_ctrl_iface_set(hapd, buf + 4))
898 } else if (os_strncmp(buf, "GET ", 4) == 0) {
899 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
902 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
907 os_memcpy(reply, "FAIL\n", 5);
910 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
915 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
920 if (hapd->conf->ctrl_interface == NULL)
923 len = os_strlen(hapd->conf->ctrl_interface) +
924 os_strlen(hapd->conf->iface) + 2;
925 buf = os_malloc(len);
929 os_snprintf(buf, len, "%s/%s",
930 hapd->conf->ctrl_interface, hapd->conf->iface);
936 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
937 const char *txt, size_t len)
939 struct hostapd_data *hapd = ctx;
942 hostapd_ctrl_iface_send(hapd, level, txt, len);
946 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
948 struct sockaddr_un addr;
952 if (hapd->ctrl_sock > -1) {
953 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
957 if (hapd->conf->ctrl_interface == NULL)
960 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
961 if (errno == EEXIST) {
962 wpa_printf(MSG_DEBUG, "Using existing control "
963 "interface directory.");
965 perror("mkdir[ctrl_interface]");
970 if (hapd->conf->ctrl_interface_gid_set &&
971 chown(hapd->conf->ctrl_interface, 0,
972 hapd->conf->ctrl_interface_gid) < 0) {
973 perror("chown[ctrl_interface]");
977 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
978 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
981 s = socket(PF_UNIX, SOCK_DGRAM, 0);
983 perror("socket(PF_UNIX)");
987 os_memset(&addr, 0, sizeof(addr));
989 addr.sun_len = sizeof(addr);
990 #endif /* __FreeBSD__ */
991 addr.sun_family = AF_UNIX;
992 fname = hostapd_ctrl_iface_path(hapd);
995 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
996 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
997 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
999 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1000 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1001 " allow connections - assuming it was left"
1002 "over from forced program termination");
1003 if (unlink(fname) < 0) {
1004 perror("unlink[ctrl_iface]");
1005 wpa_printf(MSG_ERROR, "Could not unlink "
1006 "existing ctrl_iface socket '%s'",
1010 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1012 perror("hostapd-ctrl-iface: bind(PF_UNIX)");
1015 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1016 "ctrl_iface socket '%s'", fname);
1018 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1019 "be in use - cannot override it");
1020 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1021 "not used anymore", fname);
1028 if (hapd->conf->ctrl_interface_gid_set &&
1029 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
1030 perror("chown[ctrl_interface/ifname]");
1034 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1035 perror("chmod[ctrl_interface/ifname]");
1040 hapd->ctrl_sock = s;
1041 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
1043 hapd->msg_ctx = hapd;
1044 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
1059 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
1061 struct wpa_ctrl_dst *dst, *prev;
1063 if (hapd->ctrl_sock > -1) {
1065 eloop_unregister_read_sock(hapd->ctrl_sock);
1066 close(hapd->ctrl_sock);
1067 hapd->ctrl_sock = -1;
1068 fname = hostapd_ctrl_iface_path(hapd);
1073 if (hapd->conf->ctrl_interface &&
1074 rmdir(hapd->conf->ctrl_interface) < 0) {
1075 if (errno == ENOTEMPTY) {
1076 wpa_printf(MSG_DEBUG, "Control interface "
1077 "directory not empty - leaving it "
1080 perror("rmdir[ctrl_interface]");
1085 dst = hapd->ctrl_dst;
1094 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
1095 const char *buf, size_t len)
1097 struct wpa_ctrl_dst *dst, *next;
1103 dst = hapd->ctrl_dst;
1104 if (hapd->ctrl_sock < 0 || dst == NULL)
1107 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
1108 io[0].iov_base = levelstr;
1109 io[0].iov_len = os_strlen(levelstr);
1110 io[1].iov_base = (char *) buf;
1111 io[1].iov_len = len;
1112 os_memset(&msg, 0, sizeof(msg));
1119 if (level >= dst->debug_level) {
1120 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
1121 (u8 *) dst->addr.sun_path, dst->addrlen -
1122 offsetof(struct sockaddr_un, sun_path));
1123 msg.msg_name = &dst->addr;
1124 msg.msg_namelen = dst->addrlen;
1125 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
1127 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
1129 idx, errno, strerror(errno));
1131 if (dst->errors > 10 || _errno == ENOENT) {
1132 hostapd_ctrl_iface_detach(
1144 #endif /* CONFIG_NATIVE_WINDOWS */