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 "ap/wpa_auth.h"
33 #include "wps/wps_defs.h"
35 #include "config_file.h"
36 #include "ctrl_iface.h"
40 struct wpa_ctrl_dst *next;
41 struct sockaddr_un addr;
48 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
49 const char *buf, size_t len);
52 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
53 struct sockaddr_un *from,
56 struct wpa_ctrl_dst *dst;
58 dst = os_zalloc(sizeof(*dst));
61 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
62 dst->addrlen = fromlen;
63 dst->debug_level = MSG_INFO;
64 dst->next = hapd->ctrl_dst;
66 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
67 (u8 *) from->sun_path,
68 fromlen - offsetof(struct sockaddr_un, sun_path));
73 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
74 struct sockaddr_un *from,
77 struct wpa_ctrl_dst *dst, *prev = NULL;
81 if (fromlen == dst->addrlen &&
82 os_memcmp(from->sun_path, dst->addr.sun_path,
83 fromlen - offsetof(struct sockaddr_un, sun_path))
86 hapd->ctrl_dst = dst->next;
88 prev->next = dst->next;
90 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
91 (u8 *) from->sun_path,
93 offsetof(struct sockaddr_un, sun_path));
103 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
104 struct sockaddr_un *from,
108 struct wpa_ctrl_dst *dst;
110 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
112 dst = hapd->ctrl_dst;
114 if (fromlen == dst->addrlen &&
115 os_memcmp(from->sun_path, dst->addr.sun_path,
116 fromlen - offsetof(struct sockaddr_un, sun_path))
118 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
119 "level", (u8 *) from->sun_path, fromlen -
120 offsetof(struct sockaddr_un, sun_path));
121 dst->debug_level = atoi(level);
131 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
135 struct sta_info *sta;
137 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
139 if (hwaddr_aton(txtaddr, addr))
142 sta = ap_get_sta(hapd, addr);
146 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
147 "notification", MAC2STR(addr));
148 sta = ap_sta_add(hapd, addr);
152 hostapd_new_assoc_sta(hapd, sta, 0);
157 #ifdef CONFIG_IEEE80211W
159 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
163 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
165 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
167 if (hwaddr_aton(txtaddr, addr) ||
168 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
171 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
175 #endif /* NEED_AP_MLME */
176 #endif /* CONFIG_IEEE80211W */
180 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
182 char *pin = os_strchr(txt, ' ');
185 u8 addr_buf[ETH_ALEN], *addr = NULL;
192 timeout_txt = os_strchr(pin, ' ');
194 *timeout_txt++ = '\0';
195 timeout = atoi(timeout_txt);
196 pos = os_strchr(timeout_txt, ' ');
199 if (hwaddr_aton(pos, addr_buf) == 0)
205 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
209 static int hostapd_ctrl_iface_wps_check_pin(
210 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
217 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
218 (u8 *) cmd, os_strlen(cmd));
219 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
220 if (*pos < '0' || *pos > '9')
224 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
228 if (len != 4 && len != 8) {
229 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
235 unsigned int pin_val;
237 if (!wps_pin_valid(pin_val)) {
238 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
239 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
240 if (ret < 0 || (size_t) ret >= buflen)
246 ret = os_snprintf(buf, buflen, "%s", pin);
247 if (ret < 0 || (size_t) ret >= buflen)
254 #ifdef CONFIG_WPS_NFC
255 static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
262 len = os_strlen(pos);
267 buf = wpabuf_alloc(len);
270 if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
275 ret = hostapd_wps_nfc_tag_read(hapd, buf);
282 static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
283 char *cmd, char *reply,
290 if (os_strcmp(cmd, "WPS") == 0)
292 else if (os_strcmp(cmd, "NDEF") == 0)
297 buf = hostapd_wps_nfc_config_token(hapd, ndef);
301 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
312 static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
313 char *reply, size_t max_len,
319 buf = hostapd_wps_nfc_token_gen(hapd, ndef);
323 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
334 static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
335 char *cmd, char *reply,
338 if (os_strcmp(cmd, "WPS") == 0)
339 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
342 if (os_strcmp(cmd, "NDEF") == 0)
343 return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
346 if (os_strcmp(cmd, "enable") == 0)
347 return hostapd_wps_nfc_token_enable(hapd);
349 if (os_strcmp(cmd, "disable") == 0) {
350 hostapd_wps_nfc_token_disable(hapd);
358 static int hostapd_ctrl_iface_nfc_get_handover_sel(struct hostapd_data *hapd,
359 char *cmd, char *reply,
367 pos = os_strchr(cmd, ' ');
372 if (os_strcmp(cmd, "WPS") == 0)
374 else if (os_strcmp(cmd, "NDEF") == 0)
379 if (os_strcmp(pos, "WPS-CR") == 0)
380 buf = hostapd_wps_nfc_hs_cr(hapd, ndef);
386 res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
397 static int hostapd_ctrl_iface_nfc_report_handover(struct hostapd_data *hapd,
401 * Since NFC connection handover provided full WPS Credential, there is
402 * no need for additional operations within hostapd. Just report this in
405 wpa_printf(MSG_DEBUG, "NFC: Connection handover reported: %s", cmd);
409 #endif /* CONFIG_WPS_NFC */
412 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
413 char *buf, size_t buflen)
419 pos = os_strchr(txt, ' ');
423 if (os_strcmp(txt, "disable") == 0) {
424 hostapd_wps_ap_pin_disable(hapd);
425 return os_snprintf(buf, buflen, "OK\n");
428 if (os_strcmp(txt, "random") == 0) {
431 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
434 return os_snprintf(buf, buflen, "%s", pin_txt);
437 if (os_strcmp(txt, "get") == 0) {
438 pin_txt = hostapd_wps_ap_pin_get(hapd);
441 return os_snprintf(buf, buflen, "%s", pin_txt);
444 if (os_strcmp(txt, "set") == 0) {
449 pos = os_strchr(pos, ' ');
454 if (os_strlen(pin) > buflen)
456 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
458 return os_snprintf(buf, buflen, "%s", pin);
465 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
468 char *ssid, *auth, *encr = NULL, *key = NULL;
471 pos = os_strchr(txt, ' ');
477 pos = os_strchr(pos, ' ');
481 pos = os_strchr(pos, ' ');
488 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
490 #endif /* CONFIG_WPS */
495 static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
500 struct ieee80211_mgmt *mgmt;
503 if (hwaddr_aton(cmd, addr))
507 disassoc_timer = atoi(cmd + 17);
509 os_memset(buf, 0, sizeof(buf));
510 mgmt = (struct ieee80211_mgmt *) buf;
511 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
512 WLAN_FC_STYPE_ACTION);
513 os_memcpy(mgmt->da, addr, ETH_ALEN);
514 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
515 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
516 mgmt->u.action.category = WLAN_ACTION_WNM;
517 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
518 mgmt->u.action.u.bss_tm_req.dialog_token = 1;
519 mgmt->u.action.u.bss_tm_req.req_mode =
520 WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
521 mgmt->u.action.u.bss_tm_req.disassoc_timer =
522 host_to_le16(disassoc_timer);
523 mgmt->u.action.u.bss_tm_req.validity_interval = 0;
525 pos = mgmt->u.action.u.bss_tm_req.variable;
527 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
528 wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
529 "Management Request frame");
537 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
541 const char *url, *timerstr;
543 struct ieee80211_mgmt *mgmt;
547 if (hwaddr_aton(cmd, addr))
551 if (*timerstr != ' ')
554 disassoc_timer = atoi(timerstr);
555 if (disassoc_timer < 0 || disassoc_timer > 65535)
558 url = os_strchr(timerstr, ' ');
562 url_len = os_strlen(url);
566 os_memset(buf, 0, sizeof(buf));
567 mgmt = (struct ieee80211_mgmt *) buf;
568 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
569 WLAN_FC_STYPE_ACTION);
570 os_memcpy(mgmt->da, addr, ETH_ALEN);
571 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
572 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
573 mgmt->u.action.category = WLAN_ACTION_WNM;
574 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
575 mgmt->u.action.u.bss_tm_req.dialog_token = 1;
576 mgmt->u.action.u.bss_tm_req.req_mode =
577 WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
578 mgmt->u.action.u.bss_tm_req.disassoc_timer =
579 host_to_le16(disassoc_timer);
580 mgmt->u.action.u.bss_tm_req.validity_interval = 0x01;
582 pos = mgmt->u.action.u.bss_tm_req.variable;
584 /* Session Information URL */
586 os_memcpy(pos, url, url_len);
589 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
590 wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
591 "Management Request frame");
595 /* send disassociation frame after time-out */
596 if (disassoc_timer) {
597 struct sta_info *sta;
600 * Prevent STA from reconnecting using cached PMKSA to force
601 * full authentication with the authentication server (which may
602 * decide to reject the connection),
604 wpa_auth_pmksa_remove(hapd->wpa_auth, addr);
606 sta = ap_get_sta(hapd, addr);
608 wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
609 "for ESS disassociation imminent message",
614 sta->timeout_next = STA_DISASSOC_FROM_CLI;
615 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
616 eloop_register_timeout(disassoc_timer / 1000,
617 disassoc_timer % 1000 * 1000,
618 ap_handle_timer, hapd, sta);
624 #endif /* CONFIG_WNM */
627 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
628 char *buf, size_t buflen)
636 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
638 MAC2STR(hapd->own_addr),
639 wpa_ssid_txt(hapd->conf->ssid.ssid,
640 hapd->conf->ssid.ssid_len));
641 if (ret < 0 || ret >= end - pos)
646 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
647 hapd->conf->wps_state == 0 ? "disabled" :
648 (hapd->conf->wps_state == 1 ? "not configured" :
650 if (ret < 0 || ret >= end - pos)
654 if (hapd->conf->wps_state && hapd->conf->wpa &&
655 hapd->conf->ssid.wpa_passphrase) {
656 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
657 hapd->conf->ssid.wpa_passphrase);
658 if (ret < 0 || ret >= end - pos)
663 if (hapd->conf->wps_state && hapd->conf->wpa &&
664 hapd->conf->ssid.wpa_psk &&
665 hapd->conf->ssid.wpa_psk->group) {
666 char hex[PMK_LEN * 2 + 1];
667 wpa_snprintf_hex(hex, sizeof(hex),
668 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
669 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
670 if (ret < 0 || ret >= end - pos)
674 #endif /* CONFIG_WPS */
676 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
677 ret = os_snprintf(pos, end - pos, "key_mgmt=");
678 if (ret < 0 || ret >= end - pos)
682 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
683 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
684 if (ret < 0 || ret >= end - pos)
688 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
689 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
690 if (ret < 0 || ret >= end - pos)
694 #ifdef CONFIG_IEEE80211R
695 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
696 ret = os_snprintf(pos, end - pos, "FT-PSK ");
697 if (ret < 0 || ret >= end - pos)
701 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
702 ret = os_snprintf(pos, end - pos, "FT-EAP ");
703 if (ret < 0 || ret >= end - pos)
707 #endif /* CONFIG_IEEE80211R */
708 #ifdef CONFIG_IEEE80211W
709 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
710 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
711 if (ret < 0 || ret >= end - pos)
715 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
716 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
717 if (ret < 0 || ret >= end - pos)
721 #endif /* CONFIG_IEEE80211W */
723 ret = os_snprintf(pos, end - pos, "\n");
724 if (ret < 0 || ret >= end - pos)
729 if (hapd->conf->wpa) {
730 ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
731 wpa_cipher_txt(hapd->conf->wpa_group));
732 if (ret < 0 || ret >= end - pos)
737 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
738 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
739 if (ret < 0 || ret >= end - pos)
743 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
749 ret = os_snprintf(pos, end - pos, "\n");
750 if (ret < 0 || ret >= end - pos)
755 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
756 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
757 if (ret < 0 || ret >= end - pos)
761 ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
767 ret = os_snprintf(pos, end - pos, "\n");
768 if (ret < 0 || ret >= end - pos)
777 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
782 value = os_strchr(cmd, ' ');
787 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
789 #ifdef CONFIG_WPS_TESTING
790 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
792 val = strtol(value, NULL, 0);
793 if (val < 0 || val > 0xff) {
795 wpa_printf(MSG_DEBUG, "WPS: Invalid "
796 "wps_version_number %ld", val);
798 wps_version_number = val;
799 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
801 (wps_version_number & 0xf0) >> 4,
802 wps_version_number & 0x0f);
803 hostapd_wps_update_ie(hapd);
805 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
806 wps_testing_dummy_cred = atoi(value);
807 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
808 wps_testing_dummy_cred);
809 #endif /* CONFIG_WPS_TESTING */
810 #ifdef CONFIG_INTERWORKING
811 } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
812 int val = atoi(value);
816 hapd->gas_frag_limit = val;
817 #endif /* CONFIG_INTERWORKING */
819 ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
826 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
827 char *buf, size_t buflen)
831 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
833 if (os_strcmp(cmd, "version") == 0) {
834 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
835 if (res < 0 || (unsigned int) res >= buflen)
844 static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
846 if (hostapd_enable_iface(iface) < 0) {
847 wpa_printf(MSG_ERROR, "Enabling of interface failed");
854 static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
856 if (hostapd_reload_iface(iface) < 0) {
857 wpa_printf(MSG_ERROR, "Reloading of interface failed");
864 static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
866 if (hostapd_disable_iface(iface) < 0) {
867 wpa_printf(MSG_ERROR, "Disabling of interface failed");
874 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
877 struct hostapd_data *hapd = eloop_ctx;
880 struct sockaddr_un from;
881 socklen_t fromlen = sizeof(from);
883 const int reply_size = 4096;
885 int level = MSG_DEBUG;
887 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
888 (struct sockaddr *) &from, &fromlen);
890 perror("recvfrom(ctrl_iface)");
894 if (os_strcmp(buf, "PING") == 0)
895 level = MSG_EXCESSIVE;
896 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
898 reply = os_malloc(reply_size);
900 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
905 os_memcpy(reply, "OK\n", 3);
908 if (os_strcmp(buf, "PING") == 0) {
909 os_memcpy(reply, "PONG\n", 5);
911 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
912 if (wpa_debug_reopen_file() < 0)
914 } else if (os_strcmp(buf, "MIB") == 0) {
915 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
916 if (reply_len >= 0) {
917 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
918 reply_size - reply_len);
924 if (reply_len >= 0) {
925 res = ieee802_1x_get_mib(hapd, reply + reply_len,
926 reply_size - reply_len);
932 #ifndef CONFIG_NO_RADIUS
933 if (reply_len >= 0) {
934 res = radius_client_get_mib(hapd->radius,
936 reply_size - reply_len);
942 #endif /* CONFIG_NO_RADIUS */
943 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
944 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
946 } else if (os_strncmp(buf, "STA ", 4) == 0) {
947 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
949 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
950 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
952 } else if (os_strcmp(buf, "ATTACH") == 0) {
953 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
955 } else if (os_strcmp(buf, "DETACH") == 0) {
956 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
958 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
959 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
962 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
963 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
965 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
966 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
968 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
969 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
971 #ifdef CONFIG_IEEE80211W
973 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
974 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
976 #endif /* NEED_AP_MLME */
977 #endif /* CONFIG_IEEE80211W */
979 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
980 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
982 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
983 reply_len = hostapd_ctrl_iface_wps_check_pin(
984 hapd, buf + 14, reply, reply_size);
985 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
986 if (hostapd_wps_button_pushed(hapd, NULL))
988 } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
989 if (hostapd_wps_cancel(hapd))
991 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
992 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
994 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
995 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
997 #ifdef CONFIG_WPS_NFC
998 } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
999 if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
1001 } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
1002 reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
1003 hapd, buf + 21, reply, reply_size);
1004 } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
1005 reply_len = hostapd_ctrl_iface_wps_nfc_token(
1006 hapd, buf + 14, reply, reply_size);
1007 } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
1008 reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
1009 hapd, buf + 21, reply, reply_size);
1010 } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
1011 if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
1013 #endif /* CONFIG_WPS_NFC */
1014 #endif /* CONFIG_WPS */
1016 } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
1017 if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
1019 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
1020 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
1022 #endif /* CONFIG_WNM */
1023 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
1024 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
1026 } else if (os_strncmp(buf, "SET ", 4) == 0) {
1027 if (hostapd_ctrl_iface_set(hapd, buf + 4))
1029 } else if (os_strncmp(buf, "GET ", 4) == 0) {
1030 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
1032 } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
1033 if (hostapd_ctrl_iface_enable(hapd->iface))
1035 } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
1036 if (hostapd_ctrl_iface_reload(hapd->iface))
1038 } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
1039 if (hostapd_ctrl_iface_disable(hapd->iface))
1042 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
1046 if (reply_len < 0) {
1047 os_memcpy(reply, "FAIL\n", 5);
1050 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
1055 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
1060 if (hapd->conf->ctrl_interface == NULL)
1063 len = os_strlen(hapd->conf->ctrl_interface) +
1064 os_strlen(hapd->conf->iface) + 2;
1065 buf = os_malloc(len);
1069 os_snprintf(buf, len, "%s/%s",
1070 hapd->conf->ctrl_interface, hapd->conf->iface);
1071 buf[len - 1] = '\0';
1076 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
1077 const char *txt, size_t len)
1079 struct hostapd_data *hapd = ctx;
1082 hostapd_ctrl_iface_send(hapd, level, txt, len);
1086 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
1088 struct sockaddr_un addr;
1092 if (hapd->ctrl_sock > -1) {
1093 wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
1097 if (hapd->conf->ctrl_interface == NULL)
1100 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1101 if (errno == EEXIST) {
1102 wpa_printf(MSG_DEBUG, "Using existing control "
1103 "interface directory.");
1105 perror("mkdir[ctrl_interface]");
1110 if (hapd->conf->ctrl_interface_gid_set &&
1111 chown(hapd->conf->ctrl_interface, -1,
1112 hapd->conf->ctrl_interface_gid) < 0) {
1113 perror("chown[ctrl_interface]");
1117 if (!hapd->conf->ctrl_interface_gid_set &&
1118 hapd->iface->interfaces->ctrl_iface_group &&
1119 chown(hapd->conf->ctrl_interface, -1,
1120 hapd->iface->interfaces->ctrl_iface_group) < 0) {
1121 perror("chown[ctrl_interface]");
1127 * Android is using umask 0077 which would leave the control interface
1128 * directory without group access. This breaks things since Wi-Fi
1129 * framework assumes that this directory can be accessed by other
1130 * applications in the wifi group. Fix this by adding group access even
1131 * if umask value would prevent this.
1133 if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1134 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
1136 /* Try to continue anyway */
1138 #endif /* ANDROID */
1140 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
1141 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
1144 s = socket(PF_UNIX, SOCK_DGRAM, 0);
1146 perror("socket(PF_UNIX)");
1150 os_memset(&addr, 0, sizeof(addr));
1152 addr.sun_len = sizeof(addr);
1153 #endif /* __FreeBSD__ */
1154 addr.sun_family = AF_UNIX;
1155 fname = hostapd_ctrl_iface_path(hapd);
1158 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1159 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1160 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1162 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1163 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1164 " allow connections - assuming it was left"
1165 "over from forced program termination");
1166 if (unlink(fname) < 0) {
1167 perror("unlink[ctrl_iface]");
1168 wpa_printf(MSG_ERROR, "Could not unlink "
1169 "existing ctrl_iface socket '%s'",
1173 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1175 perror("hostapd-ctrl-iface: bind(PF_UNIX)");
1178 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1179 "ctrl_iface socket '%s'", fname);
1181 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1182 "be in use - cannot override it");
1183 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1184 "not used anymore", fname);
1191 if (hapd->conf->ctrl_interface_gid_set &&
1192 chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
1193 perror("chown[ctrl_interface/ifname]");
1197 if (!hapd->conf->ctrl_interface_gid_set &&
1198 hapd->iface->interfaces->ctrl_iface_group &&
1199 chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
1200 perror("chown[ctrl_interface/ifname]");
1204 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1205 perror("chmod[ctrl_interface/ifname]");
1210 hapd->ctrl_sock = s;
1211 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
1213 hapd->msg_ctx = hapd;
1214 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
1229 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
1231 struct wpa_ctrl_dst *dst, *prev;
1233 if (hapd->ctrl_sock > -1) {
1235 eloop_unregister_read_sock(hapd->ctrl_sock);
1236 close(hapd->ctrl_sock);
1237 hapd->ctrl_sock = -1;
1238 fname = hostapd_ctrl_iface_path(hapd);
1243 if (hapd->conf->ctrl_interface &&
1244 rmdir(hapd->conf->ctrl_interface) < 0) {
1245 if (errno == ENOTEMPTY) {
1246 wpa_printf(MSG_DEBUG, "Control interface "
1247 "directory not empty - leaving it "
1250 perror("rmdir[ctrl_interface]");
1255 dst = hapd->ctrl_dst;
1264 static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
1267 if (hostapd_add_iface(interfaces, buf) < 0) {
1268 wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
1275 static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
1278 if (hostapd_remove_iface(interfaces, buf) < 0) {
1279 wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
1286 static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
1289 void *interfaces = eloop_ctx;
1292 struct sockaddr_un from;
1293 socklen_t fromlen = sizeof(from);
1297 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1298 (struct sockaddr *) &from, &fromlen);
1300 perror("recvfrom(ctrl_iface)");
1305 os_memcpy(reply, "OK\n", 3);
1308 if (os_strcmp(buf, "PING") == 0) {
1309 os_memcpy(reply, "PONG\n", 5);
1311 } else if (os_strncmp(buf, "ADD ", 4) == 0) {
1312 if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
1314 } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
1315 if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
1318 wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
1323 if (reply_len < 0) {
1324 os_memcpy(reply, "FAIL\n", 5);
1328 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
1332 static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
1337 if (interface->global_iface_path == NULL)
1340 len = os_strlen(interface->global_iface_path) +
1341 os_strlen(interface->global_iface_name) + 2;
1342 buf = os_malloc(len);
1346 os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
1347 interface->global_iface_name);
1348 buf[len - 1] = '\0';
1353 int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
1355 struct sockaddr_un addr;
1359 if (interface->global_iface_path == NULL) {
1360 wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
1364 if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
1365 if (errno == EEXIST) {
1366 wpa_printf(MSG_DEBUG, "Using existing control "
1367 "interface directory.");
1369 perror("mkdir[ctrl_interface]");
1372 } else if (interface->ctrl_iface_group &&
1373 chown(interface->global_iface_path, -1,
1374 interface->ctrl_iface_group) < 0) {
1375 perror("chown[ctrl_interface]");
1379 if (os_strlen(interface->global_iface_path) + 1 +
1380 os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
1383 s = socket(PF_UNIX, SOCK_DGRAM, 0);
1385 perror("socket(PF_UNIX)");
1389 os_memset(&addr, 0, sizeof(addr));
1391 addr.sun_len = sizeof(addr);
1392 #endif /* __FreeBSD__ */
1393 addr.sun_family = AF_UNIX;
1394 fname = hostapd_global_ctrl_iface_path(interface);
1397 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1398 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1399 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1401 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1402 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1403 " allow connections - assuming it was left"
1404 "over from forced program termination");
1405 if (unlink(fname) < 0) {
1406 perror("unlink[ctrl_iface]");
1407 wpa_printf(MSG_ERROR, "Could not unlink "
1408 "existing ctrl_iface socket '%s'",
1412 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1414 perror("bind(PF_UNIX)");
1417 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1418 "ctrl_iface socket '%s'", fname);
1420 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1421 "be in use - cannot override it");
1422 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1423 "not used anymore", fname);
1430 if (interface->ctrl_iface_group &&
1431 chown(fname, -1, interface->ctrl_iface_group) < 0) {
1432 perror("chown[ctrl_interface]");
1436 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1437 perror("chmod[ctrl_interface/ifname]");
1442 interface->global_ctrl_sock = s;
1443 eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
1459 void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
1463 if (interfaces->global_ctrl_sock > -1) {
1464 eloop_unregister_read_sock(interfaces->global_ctrl_sock);
1465 close(interfaces->global_ctrl_sock);
1466 interfaces->global_ctrl_sock = -1;
1467 fname = hostapd_global_ctrl_iface_path(interfaces);
1473 if (interfaces->global_iface_path &&
1474 rmdir(interfaces->global_iface_path) < 0) {
1475 if (errno == ENOTEMPTY) {
1476 wpa_printf(MSG_DEBUG, "Control interface "
1477 "directory not empty - leaving it "
1480 perror("rmdir[ctrl_interface]");
1483 os_free(interfaces->global_iface_path);
1484 interfaces->global_iface_path = NULL;
1489 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
1490 const char *buf, size_t len)
1492 struct wpa_ctrl_dst *dst, *next;
1498 dst = hapd->ctrl_dst;
1499 if (hapd->ctrl_sock < 0 || dst == NULL)
1502 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
1503 io[0].iov_base = levelstr;
1504 io[0].iov_len = os_strlen(levelstr);
1505 io[1].iov_base = (char *) buf;
1506 io[1].iov_len = len;
1507 os_memset(&msg, 0, sizeof(msg));
1514 if (level >= dst->debug_level) {
1515 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
1516 (u8 *) dst->addr.sun_path, dst->addrlen -
1517 offsetof(struct sockaddr_un, sun_path));
1518 msg.msg_name = &dst->addr;
1519 msg.msg_namelen = dst->addrlen;
1520 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
1522 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
1524 idx, errno, strerror(errno));
1526 if (dst->errors > 10 || _errno == ENOENT) {
1527 hostapd_ctrl_iface_detach(
1539 #endif /* CONFIG_NATIVE_WINDOWS */