2 * hostapd / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2010, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #ifndef CONFIG_NATIVE_WINDOWS
23 #include "utils/common.h"
24 #include "utils/eloop.h"
25 #include "common/version.h"
26 #include "common/ieee802_11_defs.h"
27 #include "drivers/driver.h"
28 #include "radius/radius_client.h"
29 #include "ap/hostapd.h"
30 #include "ap/ap_config.h"
31 #include "ap/ieee802_1x.h"
32 #include "ap/wpa_auth.h"
33 #include "ap/ieee802_11.h"
34 #include "ap/sta_info.h"
35 #include "ap/wps_hostapd.h"
36 #include "ap/ctrl_iface_ap.h"
37 #include "ap/ap_drv_ops.h"
38 #include "wps/wps_defs.h"
40 #include "ctrl_iface.h"
44 struct wpa_ctrl_dst *next;
45 struct sockaddr_un addr;
52 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
53 const char *buf, size_t len);
56 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
57 struct sockaddr_un *from,
60 struct wpa_ctrl_dst *dst;
62 dst = os_zalloc(sizeof(*dst));
65 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
66 dst->addrlen = fromlen;
67 dst->debug_level = MSG_INFO;
68 dst->next = hapd->ctrl_dst;
70 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
71 (u8 *) from->sun_path,
72 fromlen - offsetof(struct sockaddr_un, sun_path));
77 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
78 struct sockaddr_un *from,
81 struct wpa_ctrl_dst *dst, *prev = NULL;
85 if (fromlen == dst->addrlen &&
86 os_memcmp(from->sun_path, dst->addr.sun_path,
87 fromlen - offsetof(struct sockaddr_un, sun_path))
90 hapd->ctrl_dst = dst->next;
92 prev->next = dst->next;
94 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
95 (u8 *) from->sun_path,
97 offsetof(struct sockaddr_un, sun_path));
107 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
108 struct sockaddr_un *from,
112 struct wpa_ctrl_dst *dst;
114 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
116 dst = hapd->ctrl_dst;
118 if (fromlen == dst->addrlen &&
119 os_memcmp(from->sun_path, dst->addr.sun_path,
120 fromlen - offsetof(struct sockaddr_un, sun_path))
122 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
123 "level", (u8 *) from->sun_path, fromlen -
124 offsetof(struct sockaddr_un, sun_path));
125 dst->debug_level = atoi(level);
135 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
139 struct sta_info *sta;
141 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
143 if (hwaddr_aton(txtaddr, addr))
146 sta = ap_get_sta(hapd, addr);
150 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
151 "notification", MAC2STR(addr));
152 sta = ap_sta_add(hapd, addr);
156 hostapd_new_assoc_sta(hapd, sta, 0);
161 #ifdef CONFIG_P2P_MANAGER
162 static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
163 u8 minor_reason_code, const u8 *addr)
165 struct ieee80211_mgmt *mgmt;
169 if (hapd->driver->send_frame == NULL)
172 mgmt = os_zalloc(sizeof(*mgmt) + 100);
176 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "P2P: Disconnect STA " MACSTR
177 " with minor reason code %u (stype=%u)",
178 MAC2STR(addr), minor_reason_code, stype);
180 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
181 os_memcpy(mgmt->da, addr, ETH_ALEN);
182 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
183 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
184 if (stype == WLAN_FC_STYPE_DEAUTH) {
185 mgmt->u.deauth.reason_code =
186 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
187 pos = (u8 *) (&mgmt->u.deauth.reason_code + 1);
189 mgmt->u.disassoc.reason_code =
190 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
191 pos = (u8 *) (&mgmt->u.disassoc.reason_code + 1);
194 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
196 WPA_PUT_BE24(pos, OUI_WFA);
198 *pos++ = P2P_OUI_TYPE;
200 *pos++ = P2P_ATTR_MINOR_REASON_CODE;
201 WPA_PUT_LE16(pos, 1);
203 *pos++ = minor_reason_code;
205 ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt,
206 pos - (u8 *) mgmt, 1);
209 return ret < 0 ? -1 : 0;
211 #endif /* CONFIG_P2P_MANAGER */
214 static int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
218 struct sta_info *sta;
221 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s",
224 if (hwaddr_aton(txtaddr, addr))
227 pos = os_strstr(txtaddr, " test=");
229 struct ieee80211_mgmt mgmt;
231 if (hapd->driver->send_frame == NULL)
235 os_memset(&mgmt, 0, sizeof(mgmt));
236 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
237 WLAN_FC_STYPE_DEAUTH);
238 os_memcpy(mgmt.da, addr, ETH_ALEN);
239 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
240 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
241 mgmt.u.deauth.reason_code =
242 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
243 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
245 sizeof(mgmt.u.deauth),
251 #ifdef CONFIG_P2P_MANAGER
252 pos = os_strstr(txtaddr, " p2p=");
254 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
255 atoi(pos + 5), addr);
257 #endif /* CONFIG_P2P_MANAGER */
259 hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
260 sta = ap_get_sta(hapd, addr);
262 ap_sta_deauthenticate(hapd, sta,
263 WLAN_REASON_PREV_AUTH_NOT_VALID);
264 else if (addr[0] == 0xff)
265 hostapd_free_stas(hapd);
271 static int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
275 struct sta_info *sta;
278 wpa_dbg(hapd->msg_ctx, MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s",
281 if (hwaddr_aton(txtaddr, addr))
284 pos = os_strstr(txtaddr, " test=");
286 struct ieee80211_mgmt mgmt;
288 if (hapd->driver->send_frame == NULL)
292 os_memset(&mgmt, 0, sizeof(mgmt));
293 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
294 WLAN_FC_STYPE_DISASSOC);
295 os_memcpy(mgmt.da, addr, ETH_ALEN);
296 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
297 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
298 mgmt.u.disassoc.reason_code =
299 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
300 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
302 sizeof(mgmt.u.deauth),
308 #ifdef CONFIG_P2P_MANAGER
309 pos = os_strstr(txtaddr, " p2p=");
311 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
312 atoi(pos + 5), addr);
314 #endif /* CONFIG_P2P_MANAGER */
316 hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
317 sta = ap_get_sta(hapd, addr);
319 ap_sta_disassociate(hapd, sta,
320 WLAN_REASON_PREV_AUTH_NOT_VALID);
321 else if (addr[0] == 0xff)
322 hostapd_free_stas(hapd);
328 #ifdef CONFIG_IEEE80211W
330 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
334 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
336 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
338 if (hwaddr_aton(txtaddr, addr) ||
339 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
342 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
346 #endif /* NEED_AP_MLME */
347 #endif /* CONFIG_IEEE80211W */
351 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
353 char *pin = os_strchr(txt, ' ');
356 u8 addr_buf[ETH_ALEN], *addr = NULL;
363 timeout_txt = os_strchr(pin, ' ');
365 *timeout_txt++ = '\0';
366 timeout = atoi(timeout_txt);
367 pos = os_strchr(timeout_txt, ' ');
370 if (hwaddr_aton(pos, addr_buf) == 0)
376 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
380 static int hostapd_ctrl_iface_wps_check_pin(
381 struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
388 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
389 (u8 *) cmd, os_strlen(cmd));
390 for (pos = cmd, len = 0; *pos != '\0'; pos++) {
391 if (*pos < '0' || *pos > '9')
395 wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
399 if (len != 4 && len != 8) {
400 wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
406 unsigned int pin_val;
408 if (!wps_pin_valid(pin_val)) {
409 wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
410 ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
411 if (ret < 0 || (size_t) ret >= buflen)
417 ret = os_snprintf(buf, buflen, "%s", pin);
418 if (ret < 0 || (size_t) ret >= buflen)
425 #ifdef CONFIG_WPS_OOB
426 static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
428 char *path, *method, *name;
430 path = os_strchr(txt, ' ');
435 method = os_strchr(path, ' ');
440 name = os_strchr(method, ' ');
444 return hostapd_wps_start_oob(hapd, txt, path, method, name);
446 #endif /* CONFIG_WPS_OOB */
449 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
450 char *buf, size_t buflen)
456 pos = os_strchr(txt, ' ');
460 if (os_strcmp(txt, "disable") == 0) {
461 hostapd_wps_ap_pin_disable(hapd);
462 return os_snprintf(buf, buflen, "OK\n");
465 if (os_strcmp(txt, "random") == 0) {
468 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
471 return os_snprintf(buf, buflen, "%s", pin_txt);
474 if (os_strcmp(txt, "get") == 0) {
475 pin_txt = hostapd_wps_ap_pin_get(hapd);
478 return os_snprintf(buf, buflen, "%s", pin_txt);
481 if (os_strcmp(txt, "set") == 0) {
486 pos = os_strchr(pos, ' ');
491 if (os_strlen(pin) > buflen)
493 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
495 return os_snprintf(buf, buflen, "%s", pin);
502 static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
505 char *ssid, *auth, *encr = NULL, *key = NULL;
508 pos = os_strchr(txt, ' ');
514 pos = os_strchr(pos, ' ');
518 pos = os_strchr(pos, ' ');
525 return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
527 #endif /* CONFIG_WPS */
530 static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
536 struct ieee80211_mgmt *mgmt;
539 if (hwaddr_aton(cmd, addr))
545 url_len = os_strlen(url);
549 os_memset(buf, 0, sizeof(buf));
550 mgmt = (struct ieee80211_mgmt *) buf;
551 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
552 WLAN_FC_STYPE_ACTION);
553 os_memcpy(mgmt->da, addr, ETH_ALEN);
554 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
555 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
556 mgmt->u.action.category = WLAN_ACTION_WNM;
557 mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
558 mgmt->u.action.u.bss_tm_req.dialog_token = 1;
559 mgmt->u.action.u.bss_tm_req.req_mode =
560 WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
561 mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0);
562 mgmt->u.action.u.bss_tm_req.validity_interval = 0;
564 pos = mgmt->u.action.u.bss_tm_req.variable;
566 /* Session Information URL */
568 os_memcpy(pos, url, url_len);
571 if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
572 wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
573 "Management Request frame");
581 static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
582 char *buf, size_t buflen)
590 ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
592 MAC2STR(hapd->own_addr),
593 hapd->conf->ssid.ssid);
594 if (ret < 0 || ret >= end - pos)
599 ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
600 hapd->conf->wps_state == 0 ? "disabled" :
601 (hapd->conf->wps_state == 1 ? "not configured" :
603 if (ret < 0 || ret >= end - pos)
607 if (hapd->conf->wps_state && hapd->conf->wpa &&
608 hapd->conf->ssid.wpa_passphrase) {
609 ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
610 hapd->conf->ssid.wpa_passphrase);
611 if (ret < 0 || ret >= end - pos)
616 if (hapd->conf->wps_state && hapd->conf->wpa &&
617 hapd->conf->ssid.wpa_psk &&
618 hapd->conf->ssid.wpa_psk->group) {
619 char hex[PMK_LEN * 2 + 1];
620 wpa_snprintf_hex(hex, sizeof(hex),
621 hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
622 ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
623 if (ret < 0 || ret >= end - pos)
627 #endif /* CONFIG_WPS */
629 if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
630 ret = os_snprintf(pos, end - pos, "key_mgmt=");
631 if (ret < 0 || ret >= end - pos)
635 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
636 ret = os_snprintf(pos, end - pos, "WPA-PSK ");
637 if (ret < 0 || ret >= end - pos)
641 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
642 ret = os_snprintf(pos, end - pos, "WPA-EAP ");
643 if (ret < 0 || ret >= end - pos)
647 #ifdef CONFIG_IEEE80211R
648 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
649 ret = os_snprintf(pos, end - pos, "FT-PSK ");
650 if (ret < 0 || ret >= end - pos)
654 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
655 ret = os_snprintf(pos, end - pos, "FT-EAP ");
656 if (ret < 0 || ret >= end - pos)
660 #endif /* CONFIG_IEEE80211R */
661 #ifdef CONFIG_IEEE80211W
662 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
663 ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
664 if (ret < 0 || ret >= end - pos)
668 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
669 ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
670 if (ret < 0 || ret >= end - pos)
674 #endif /* CONFIG_IEEE80211W */
676 ret = os_snprintf(pos, end - pos, "\n");
677 if (ret < 0 || ret >= end - pos)
682 if (hapd->conf->wpa && hapd->conf->wpa_group == WPA_CIPHER_CCMP) {
683 ret = os_snprintf(pos, end - pos, "group_cipher=CCMP\n");
684 if (ret < 0 || ret >= end - pos)
687 } else if (hapd->conf->wpa &&
688 hapd->conf->wpa_group == WPA_CIPHER_TKIP) {
689 ret = os_snprintf(pos, end - pos, "group_cipher=TKIP\n");
690 if (ret < 0 || ret >= end - pos)
695 if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
696 ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
697 if (ret < 0 || ret >= end - pos)
701 if (hapd->conf->rsn_pairwise & WPA_CIPHER_CCMP) {
702 ret = os_snprintf(pos, end - pos, "CCMP ");
703 if (ret < 0 || ret >= end - pos)
707 if (hapd->conf->rsn_pairwise & WPA_CIPHER_TKIP) {
708 ret = os_snprintf(pos, end - pos, "TKIP ");
709 if (ret < 0 || ret >= end - pos)
714 ret = os_snprintf(pos, end - pos, "\n");
715 if (ret < 0 || ret >= end - pos)
720 if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
721 ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
722 if (ret < 0 || ret >= end - pos)
726 if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) {
727 ret = os_snprintf(pos, end - pos, "CCMP ");
728 if (ret < 0 || ret >= end - pos)
732 if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) {
733 ret = os_snprintf(pos, end - pos, "TKIP ");
734 if (ret < 0 || ret >= end - pos)
739 ret = os_snprintf(pos, end - pos, "\n");
740 if (ret < 0 || ret >= end - pos)
749 static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
754 value = os_strchr(cmd, ' ');
759 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
761 #ifdef CONFIG_WPS_TESTING
762 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
764 val = strtol(value, NULL, 0);
765 if (val < 0 || val > 0xff) {
767 wpa_printf(MSG_DEBUG, "WPS: Invalid "
768 "wps_version_number %ld", val);
770 wps_version_number = val;
771 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
773 (wps_version_number & 0xf0) >> 4,
774 wps_version_number & 0x0f);
775 hostapd_wps_update_ie(hapd);
777 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
778 wps_testing_dummy_cred = atoi(value);
779 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
780 wps_testing_dummy_cred);
781 #endif /* CONFIG_WPS_TESTING */
790 static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
791 char *buf, size_t buflen)
795 wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
797 if (os_strcmp(cmd, "version") == 0) {
798 res = os_snprintf(buf, buflen, "%s", VERSION_STR);
799 if (res < 0 || (unsigned int) res >= buflen)
808 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
811 struct hostapd_data *hapd = eloop_ctx;
814 struct sockaddr_un from;
815 socklen_t fromlen = sizeof(from);
817 const int reply_size = 4096;
819 int level = MSG_DEBUG;
821 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
822 (struct sockaddr *) &from, &fromlen);
824 perror("recvfrom(ctrl_iface)");
828 if (os_strcmp(buf, "PING") == 0)
829 level = MSG_EXCESSIVE;
830 wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
832 reply = os_malloc(reply_size);
834 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
839 os_memcpy(reply, "OK\n", 3);
842 if (os_strcmp(buf, "PING") == 0) {
843 os_memcpy(reply, "PONG\n", 5);
845 } else if (os_strncmp(buf, "RELOG", 5) == 0) {
846 if (wpa_debug_reopen_file() < 0)
848 } else if (os_strcmp(buf, "MIB") == 0) {
849 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
850 if (reply_len >= 0) {
851 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
852 reply_size - reply_len);
858 if (reply_len >= 0) {
859 res = ieee802_1x_get_mib(hapd, reply + reply_len,
860 reply_size - reply_len);
866 #ifndef CONFIG_NO_RADIUS
867 if (reply_len >= 0) {
868 res = radius_client_get_mib(hapd->radius,
870 reply_size - reply_len);
876 #endif /* CONFIG_NO_RADIUS */
877 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
878 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
880 } else if (os_strncmp(buf, "STA ", 4) == 0) {
881 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
883 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
884 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
886 } else if (os_strcmp(buf, "ATTACH") == 0) {
887 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
889 } else if (os_strcmp(buf, "DETACH") == 0) {
890 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
892 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
893 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
896 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
897 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
899 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
900 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
902 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
903 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
905 #ifdef CONFIG_IEEE80211W
907 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
908 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
910 #endif /* NEED_AP_MLME */
911 #endif /* CONFIG_IEEE80211W */
913 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
914 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
916 } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
917 reply_len = hostapd_ctrl_iface_wps_check_pin(
918 hapd, buf + 14, reply, reply_size);
919 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
920 if (hostapd_wps_button_pushed(hapd, NULL))
922 #ifdef CONFIG_WPS_OOB
923 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
924 if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
926 #endif /* CONFIG_WPS_OOB */
927 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
928 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
930 } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
931 if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
933 #endif /* CONFIG_WPS */
934 } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
935 if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
937 } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
938 reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
940 } else if (os_strncmp(buf, "SET ", 4) == 0) {
941 if (hostapd_ctrl_iface_set(hapd, buf + 4))
943 } else if (os_strncmp(buf, "GET ", 4) == 0) {
944 reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
947 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
952 os_memcpy(reply, "FAIL\n", 5);
955 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
960 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
965 if (hapd->conf->ctrl_interface == NULL)
968 len = os_strlen(hapd->conf->ctrl_interface) +
969 os_strlen(hapd->conf->iface) + 2;
970 buf = os_malloc(len);
974 os_snprintf(buf, len, "%s/%s",
975 hapd->conf->ctrl_interface, hapd->conf->iface);
981 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
982 const char *txt, size_t len)
984 struct hostapd_data *hapd = ctx;
987 hostapd_ctrl_iface_send(hapd, level, txt, len);
991 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
993 struct sockaddr_un addr;
997 hapd->ctrl_sock = -1;
999 if (hapd->conf->ctrl_interface == NULL)
1002 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
1003 if (errno == EEXIST) {
1004 wpa_printf(MSG_DEBUG, "Using existing control "
1005 "interface directory.");
1007 perror("mkdir[ctrl_interface]");
1012 if (hapd->conf->ctrl_interface_gid_set &&
1013 chown(hapd->conf->ctrl_interface, 0,
1014 hapd->conf->ctrl_interface_gid) < 0) {
1015 perror("chown[ctrl_interface]");
1019 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
1020 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
1023 s = socket(PF_UNIX, SOCK_DGRAM, 0);
1025 perror("socket(PF_UNIX)");
1029 os_memset(&addr, 0, sizeof(addr));
1031 addr.sun_len = sizeof(addr);
1032 #endif /* __FreeBSD__ */
1033 addr.sun_family = AF_UNIX;
1034 fname = hostapd_ctrl_iface_path(hapd);
1037 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
1038 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1039 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
1041 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1042 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1043 " allow connections - assuming it was left"
1044 "over from forced program termination");
1045 if (unlink(fname) < 0) {
1046 perror("unlink[ctrl_iface]");
1047 wpa_printf(MSG_ERROR, "Could not unlink "
1048 "existing ctrl_iface socket '%s'",
1052 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
1054 perror("bind(PF_UNIX)");
1057 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1058 "ctrl_iface socket '%s'", fname);
1060 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1061 "be in use - cannot override it");
1062 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1063 "not used anymore", fname);
1070 if (hapd->conf->ctrl_interface_gid_set &&
1071 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
1072 perror("chown[ctrl_interface/ifname]");
1076 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
1077 perror("chmod[ctrl_interface/ifname]");
1082 hapd->ctrl_sock = s;
1083 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
1085 hapd->msg_ctx = hapd;
1086 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
1101 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
1103 struct wpa_ctrl_dst *dst, *prev;
1105 if (hapd->ctrl_sock > -1) {
1107 eloop_unregister_read_sock(hapd->ctrl_sock);
1108 close(hapd->ctrl_sock);
1109 hapd->ctrl_sock = -1;
1110 fname = hostapd_ctrl_iface_path(hapd);
1115 if (hapd->conf->ctrl_interface &&
1116 rmdir(hapd->conf->ctrl_interface) < 0) {
1117 if (errno == ENOTEMPTY) {
1118 wpa_printf(MSG_DEBUG, "Control interface "
1119 "directory not empty - leaving it "
1122 perror("rmdir[ctrl_interface]");
1127 dst = hapd->ctrl_dst;
1136 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
1137 const char *buf, size_t len)
1139 struct wpa_ctrl_dst *dst, *next;
1145 dst = hapd->ctrl_dst;
1146 if (hapd->ctrl_sock < 0 || dst == NULL)
1149 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
1150 io[0].iov_base = levelstr;
1151 io[0].iov_len = os_strlen(levelstr);
1152 io[1].iov_base = (char *) buf;
1153 io[1].iov_len = len;
1154 os_memset(&msg, 0, sizeof(msg));
1161 if (level >= dst->debug_level) {
1162 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
1163 (u8 *) dst->addr.sun_path, dst->addrlen -
1164 offsetof(struct sockaddr_un, sun_path));
1165 msg.msg_name = &dst->addr;
1166 msg.msg_namelen = dst->addrlen;
1167 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
1169 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
1171 idx, errno, strerror(errno));
1173 if (dst->errors > 10 || _errno == ENOENT) {
1174 hostapd_ctrl_iface_detach(
1186 #endif /* CONFIG_NATIVE_WINDOWS */