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 wpa_ssid_txt(hapd->conf->ssid.ssid,
527 hapd->conf->ssid.ssid_len));
528 if (ret < 0 || ret >= end - pos)
533 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
534 hapd->conf->wps_state == 0 ? "disabled" :
535 (hapd->conf->wps_state == 1 ? "not configured" :
537 if (ret < 0 || ret >= end - pos)
541 if (hapd->conf->wps_state && hapd->conf->wpa &&
542 hapd->conf->ssid.wpa_passphrase) {
543 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
544 hapd->conf->ssid.wpa_passphrase);
545 if (ret < 0 || ret >= end - pos)
550 if (hapd->conf->wps_state && hapd->conf->wpa &&
551 hapd->conf->ssid.wpa_psk &&
552 hapd->conf->ssid.wpa_psk->group) {
553 char hex[PMK_LEN * 2 + 1];
554 wpa_snprintf_hex(hex, sizeof(hex),
555 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
556 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
557 if (ret < 0 || ret >= end - pos)
561 #endif /* CONFIG_WPS */
563 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
564 ret = os_snprintf(pos, end - pos, "key_mgmt=");
565 if (ret < 0 || ret >= end - pos)
569 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
570 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
571 if (ret < 0 || ret >= end - pos)
575 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
576 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
577 if (ret < 0 || ret >= end - pos)
581 #ifdef CONFIG_IEEE80211R
582 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
583 ret = os_snprintf(pos, end - pos, "FT-PSK ");
584 if (ret < 0 || ret >= end - pos)
588 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
589 ret = os_snprintf(pos, end - pos, "FT-EAP ");
590 if (ret < 0 || ret >= end - pos)
594 #endif /* CONFIG_IEEE80211R */
595 #ifdef CONFIG_IEEE80211W
596 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
597 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
598 if (ret < 0 || ret >= end - pos)
602 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
603 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
604 if (ret < 0 || ret >= end - pos)
608 #endif /* CONFIG_IEEE80211W */
610 ret = os_snprintf(pos, end - pos, "\n");
611 if (ret < 0 || ret >= end - pos)
616 if (hapd->conf->wpa && hapd->conf->wpa_group == WPA_CIPHER_CCMP) {
617 ret = os_snprintf(pos, end - pos, "group_cipher=CCMP\n");
618 if (ret < 0 || ret >= end - pos)
621 } else if (hapd->conf->wpa &&
622 hapd->conf->wpa_group == WPA_CIPHER_GCMP) {
623 ret = os_snprintf(pos, end - pos, "group_cipher=GCMP\n");
624 if (ret < 0 || ret >= end - pos)
627 } else if (hapd->conf->wpa &&
628 hapd->conf->wpa_group == WPA_CIPHER_TKIP) {
629 ret = os_snprintf(pos, end - pos, "group_cipher=TKIP\n");
630 if (ret < 0 || ret >= end - pos)
635 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
636 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
637 if (ret < 0 || ret >= end - pos)
641 if (hapd->conf->rsn_pairwise & WPA_CIPHER_CCMP) {
642 ret = os_snprintf(pos, end - pos, "CCMP ");
643 if (ret < 0 || ret >= end - pos)
647 if (hapd->conf->rsn_pairwise & WPA_CIPHER_GCMP) {
648 ret = os_snprintf(pos, end - pos, "GCMP ");
649 if (ret < 0 || ret >= end - pos)
653 if (hapd->conf->rsn_pairwise & WPA_CIPHER_TKIP) {
654 ret = os_snprintf(pos, end - pos, "TKIP ");
655 if (ret < 0 || ret >= end - pos)
660 ret = os_snprintf(pos, end - pos, "\n");
661 if (ret < 0 || ret >= end - pos)
666 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
667 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
668 if (ret < 0 || ret >= end - pos)
672 if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) {
673 ret = os_snprintf(pos, end - pos, "CCMP ");
674 if (ret < 0 || ret >= end - pos)
678 if (hapd->conf->wpa_pairwise & WPA_CIPHER_GCMP) {
679 ret = os_snprintf(pos, end - pos, "GCMP ");
680 if (ret < 0 || ret >= end - pos)
684 if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) {
685 ret = os_snprintf(pos, end - pos, "TKIP ");
686 if (ret < 0 || ret >= end - pos)
691 ret = os_snprintf(pos, end - pos, "\n");
692 if (ret < 0 || ret >= end - pos)
701 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
706 value = os_strchr(cmd, ' ');
711 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
713 #ifdef CONFIG_WPS_TESTING
714 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
716 val = strtol(value, NULL, 0);
717 if (val < 0 || val > 0xff) {
719 wpa_printf(MSG_DEBUG, "WPS: Invalid "
720 "wps_version_number %ld", val);
722 wps_version_number = val;
723 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
725 (wps_version_number & 0xf0) >> 4,
726 wps_version_number & 0x0f);
727 hostapd_wps_update_ie(hapd);
729 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
730 wps_testing_dummy_cred = atoi(value);
731 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
732 wps_testing_dummy_cred);
733 #endif /* CONFIG_WPS_TESTING */
734 #ifdef CONFIG_INTERWORKING
735 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
736 int val = atoi(value);
740 hapd->gas_frag_limit = val;
741 #endif /* CONFIG_INTERWORKING */
743 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
750 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
751 char *buf, size_t buflen)
755 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
757 if (os_strcmp(cmd, "version") == 0) {
758 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
759 if (res < 0 || (unsigned int) res >= buflen)
768 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
770 if (hostapd_enable_iface(iface) < 0) {
771 wpa_printf(MSG_ERROR, "Enabling of interface failed");
778 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
780 if (hostapd_reload_iface(iface) < 0) {
781 wpa_printf(MSG_ERROR, "Reloading of interface failed");
788 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
790 if (hostapd_disable_iface(iface) < 0) {
791 wpa_printf(MSG_ERROR, "Disabling of interface failed");
798 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
801 struct hostapd_data *hapd = eloop_ctx;
804 struct sockaddr_un from;
805 socklen_t fromlen = sizeof(from);
807 const int reply_size = 4096;
809 int level = MSG_DEBUG;
811 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
812 (struct sockaddr *) &from, &fromlen);
814 perror("recvfrom(ctrl_iface)");
818 if (os_strcmp(buf, "PING") == 0)
819 level = MSG_EXCESSIVE;
820 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
822 reply = os_malloc(reply_size);
824 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
829 os_memcpy(reply, "OK\n", 3);
832 if (os_strcmp(buf, "PING") == 0) {
833 os_memcpy(reply, "PONG\n", 5);
835 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
836 if (wpa_debug_reopen_file() < 0)
838 } else if (os_strcmp(buf, "MIB") == 0) {
839 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
840 if (reply_len >= 0) {
841 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
842 reply_size - reply_len);
848 if (reply_len >= 0) {
849 res = ieee802_1x_get_mib(hapd, reply + reply_len,
850 reply_size - reply_len);
856 #ifndef CONFIG_NO_RADIUS
857 if (reply_len >= 0) {
858 res = radius_client_get_mib(hapd->radius,
860 reply_size - reply_len);
866 #endif /* CONFIG_NO_RADIUS */
867 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
868 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
870 } else if (os_strncmp(buf, "STA ", 4) == 0) {
871 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
873 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
874 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
876 } else if (os_strcmp(buf, "ATTACH") == 0) {
877 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
879 } else if (os_strcmp(buf, "DETACH") == 0) {
880 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
882 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
883 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
886 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
887 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
889 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
890 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
892 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
893 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
895 #ifdef CONFIG_IEEE80211W
897 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
898 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
900 #endif /* NEED_AP_MLME */
901 #endif /* CONFIG_IEEE80211W */
903 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
904 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
906 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
907 reply_len = hostapd_ctrl_iface_wps_check_pin(
908 hapd, buf + 14, reply, reply_size);
909 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
910 if (hostapd_wps_button_pushed(hapd, NULL))
912 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
913 if (hostapd_wps_cancel(hapd))
915 #ifdef CONFIG_WPS_OOB
916 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
917 if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
919 #endif /* CONFIG_WPS_OOB */
920 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
921 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
923 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
924 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
926 #ifdef CONFIG_WPS_NFC
927 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
928 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
930 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
931 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
932 hapd, buf + 21, reply, reply_size);
933 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
934 reply_len = hostapd_ctrl_iface_wps_nfc_token(
935 hapd, buf + 14, reply, reply_size);
936 #endif /* CONFIG_WPS_NFC */
937 #endif /* CONFIG_WPS */
938 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
939 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
941 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
942 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
944 } else if (os_strncmp(buf, "SET ", 4) == 0) {
945 if (hostapd_ctrl_iface_set(hapd, buf + 4))
947 } else if (os_strncmp(buf, "GET ", 4) == 0) {
948 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
950 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
951 if (hostapd_ctrl_iface_enable(hapd->iface))
953 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
954 if (hostapd_ctrl_iface_reload(hapd->iface))
956 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
957 if (hostapd_ctrl_iface_disable(hapd->iface))
960 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
965 os_memcpy(reply, "FAIL\n", 5);
968 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
973 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
978 if (hapd->conf->ctrl_interface == NULL)
981 len = os_strlen(hapd->conf->ctrl_interface) +
982 os_strlen(hapd->conf->iface) + 2;
983 buf = os_malloc(len);
987 os_snprintf(buf, len, "%s/%s",
988 hapd->conf->ctrl_interface, hapd->conf->iface);
994 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
995 const char *txt, size_t len)
997 struct hostapd_data *hapd = ctx;
1000 hostapd_ctrl_iface_send(hapd, level, txt, len);
1004 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
1006 struct sockaddr_un addr;
1010 if (hapd->ctrl_sock > -1) {
1011 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
1015 if (hapd->conf->ctrl_interface == NULL)
1018 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1019 if (errno == EEXIST) {
1020 wpa_printf(MSG_DEBUG, "Using existing control "
1021 "interface directory.");
1023 perror("mkdir[ctrl_interface]");
1028 if (hapd->conf->ctrl_interface_gid_set &&
1029 chown(hapd->conf->ctrl_interface, 0,
1030 hapd->conf->ctrl_interface_gid) < 0) {
1031 perror("chown[ctrl_interface]");
1035 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
1036 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
1039 s = socket(PF_UNIX, SOCK_DGRAM, 0);
1041 perror("socket(PF_UNIX)");
1045 os_memset(&addr, 0, sizeof(addr));
1047 addr.sun_len = sizeof(addr);
1048 #endif /* __FreeBSD__ */
1049 addr.sun_family = AF_UNIX;
1050 fname = hostapd_ctrl_iface_path(hapd);
1053 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1054 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1055 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1057 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1058 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1059 " allow connections - assuming it was left"
1060 "over from forced program termination");
1061 if (unlink(fname) < 0) {
1062 perror("unlink[ctrl_iface]");
1063 wpa_printf(MSG_ERROR, "Could not unlink "
1064 "existing ctrl_iface socket '%s'",
1068 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1070 perror("hostapd-ctrl-iface: bind(PF_UNIX)");
1073 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1074 "ctrl_iface socket '%s'", fname);
1076 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1077 "be in use - cannot override it");
1078 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1079 "not used anymore", fname);
1086 if (hapd->conf->ctrl_interface_gid_set &&
1087 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
1088 perror("chown[ctrl_interface/ifname]");
1092 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1093 perror("chmod[ctrl_interface/ifname]");
1098 hapd->ctrl_sock = s;
1099 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
1101 hapd->msg_ctx = hapd;
1102 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
1117 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
1119 struct wpa_ctrl_dst *dst, *prev;
1121 if (hapd->ctrl_sock > -1) {
1123 eloop_unregister_read_sock(hapd->ctrl_sock);
1124 close(hapd->ctrl_sock);
1125 hapd->ctrl_sock = -1;
1126 fname = hostapd_ctrl_iface_path(hapd);
1131 if (hapd->conf->ctrl_interface &&
1132 rmdir(hapd->conf->ctrl_interface) < 0) {
1133 if (errno == ENOTEMPTY) {
1134 wpa_printf(MSG_DEBUG, "Control interface "
1135 "directory not empty - leaving it "
1138 perror("rmdir[ctrl_interface]");
1143 dst = hapd->ctrl_dst;
1152 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
1155 if (hostapd_add_iface(interfaces, buf) < 0) {
1156 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
1163 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
1166 if (hostapd_remove_iface(interfaces, buf) < 0) {
1167 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
1174 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
1177 void *interfaces = eloop_ctx;
1180 struct sockaddr_un from;
1181 socklen_t fromlen = sizeof(from);
1185 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1186 (struct sockaddr *) &from, &fromlen);
1188 perror("recvfrom(ctrl_iface)");
1193 os_memcpy(reply, "OK\n", 3);
1196 if (os_strcmp(buf, "PING") == 0) {
1197 os_memcpy(reply, "PONG\n", 5);
1199 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
1200 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
1202 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
1203 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
1206 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
1211 if (reply_len < 0) {
1212 os_memcpy(reply, "FAIL\n", 5);
1216 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
1220 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
1225 if (interface->global_iface_path == NULL)
1228 len = os_strlen(interface->global_iface_path) +
1229 os_strlen(interface->global_iface_name) + 2;
1230 buf = os_malloc(len);
1234 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
1235 interface->global_iface_name);
1236 buf[len - 1] = '\0';
1241 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
1243 struct sockaddr_un addr;
1247 if (interface->global_iface_path == NULL) {
1248 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
1252 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
1253 if (errno == EEXIST) {
1254 wpa_printf(MSG_DEBUG, "Using existing control "
1255 "interface directory.");
1257 perror("mkdir[ctrl_interface]");
1262 if (os_strlen(interface->global_iface_path) + 1 +
1263 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
1266 s = socket(PF_UNIX, SOCK_DGRAM, 0);
1268 perror("socket(PF_UNIX)");
1272 os_memset(&addr, 0, sizeof(addr));
1274 addr.sun_len = sizeof(addr);
1275 #endif /* __FreeBSD__ */
1276 addr.sun_family = AF_UNIX;
1277 fname = hostapd_global_ctrl_iface_path(interface);
1280 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1281 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1282 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1284 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1285 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1286 " allow connections - assuming it was left"
1287 "over from forced program termination");
1288 if (unlink(fname) < 0) {
1289 perror("unlink[ctrl_iface]");
1290 wpa_printf(MSG_ERROR, "Could not unlink "
1291 "existing ctrl_iface socket '%s'",
1295 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1297 perror("bind(PF_UNIX)");
1300 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1301 "ctrl_iface socket '%s'", fname);
1303 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1304 "be in use - cannot override it");
1305 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1306 "not used anymore", fname);
1313 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1314 perror("chmod[ctrl_interface/ifname]");
1319 interface->global_ctrl_sock = s;
1320 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
1336 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
1340 if (interfaces->global_ctrl_sock > -1) {
1341 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
1342 close(interfaces->global_ctrl_sock);
1343 interfaces->global_ctrl_sock = -1;
1344 fname = hostapd_global_ctrl_iface_path(interfaces);
1350 if (interfaces->global_iface_path &&
1351 rmdir(interfaces->global_iface_path) < 0) {
1352 if (errno == ENOTEMPTY) {
1353 wpa_printf(MSG_DEBUG, "Control interface "
1354 "directory not empty - leaving it "
1357 perror("rmdir[ctrl_interface]");
1360 os_free(interfaces->global_iface_path);
1361 interfaces->global_iface_path = NULL;
1366 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
1367 const char *buf, size_t len)
1369 struct wpa_ctrl_dst *dst, *next;
1375 dst = hapd->ctrl_dst;
1376 if (hapd->ctrl_sock < 0 || dst == NULL)
1379 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
1380 io[0].iov_base = levelstr;
1381 io[0].iov_len = os_strlen(levelstr);
1382 io[1].iov_base = (char *) buf;
1383 io[1].iov_len = len;
1384 os_memset(&msg, 0, sizeof(msg));
1391 if (level >= dst->debug_level) {
1392 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
1393 (u8 *) dst->addr.sun_path, dst->addrlen -
1394 offsetof(struct sockaddr_un, sun_path));
1395 msg.msg_name = &dst->addr;
1396 msg.msg_namelen = dst->addrlen;
1397 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
1399 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
1401 idx, errno, strerror(errno));
1403 if (dst->errors > 10 || _errno == ENOENT) {
1404 hostapd_ctrl_iface_detach(
1416 #endif /* CONFIG_NATIVE_WINDOWS */