2 * hostapd / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2009, 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/ieee802_11_defs.h"
26 #include "drivers/driver.h"
27 #include "radius/radius_client.h"
28 #include "ap/hostapd.h"
29 #include "ap/ap_config.h"
30 #include "ap/ieee802_1x.h"
31 #include "ap/wpa_auth.h"
32 #include "ap/ieee802_11.h"
33 #include "ap/sta_info.h"
34 #include "ap/accounting.h"
35 #include "ap/wps_hostapd.h"
36 #include "ap/ctrl_iface_ap.h"
37 #include "ctrl_iface.h"
41 struct wpa_ctrl_dst *next;
42 struct sockaddr_un addr;
49 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
50 const char *buf, size_t len);
53 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
54 struct sockaddr_un *from,
57 struct wpa_ctrl_dst *dst;
59 dst = os_zalloc(sizeof(*dst));
62 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
63 dst->addrlen = fromlen;
64 dst->debug_level = MSG_INFO;
65 dst->next = hapd->ctrl_dst;
67 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
68 (u8 *) from->sun_path,
69 fromlen - offsetof(struct sockaddr_un, sun_path));
74 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
75 struct sockaddr_un *from,
78 struct wpa_ctrl_dst *dst, *prev = NULL;
82 if (fromlen == dst->addrlen &&
83 os_memcmp(from->sun_path, dst->addr.sun_path,
84 fromlen - offsetof(struct sockaddr_un, sun_path))
87 hapd->ctrl_dst = dst->next;
89 prev->next = dst->next;
91 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
92 (u8 *) from->sun_path,
94 offsetof(struct sockaddr_un, sun_path));
104 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
105 struct sockaddr_un *from,
109 struct wpa_ctrl_dst *dst;
111 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
113 dst = hapd->ctrl_dst;
115 if (fromlen == dst->addrlen &&
116 os_memcmp(from->sun_path, dst->addr.sun_path,
117 fromlen - offsetof(struct sockaddr_un, sun_path))
119 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
120 "level", (u8 *) from->sun_path, fromlen -
121 offsetof(struct sockaddr_un, sun_path));
122 dst->debug_level = atoi(level);
132 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
136 struct sta_info *sta;
138 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
140 if (hwaddr_aton(txtaddr, addr))
143 sta = ap_get_sta(hapd, addr);
147 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
148 "notification", MAC2STR(addr));
149 sta = ap_sta_add(hapd, addr);
153 hostapd_new_assoc_sta(hapd, sta, 0);
158 #ifdef CONFIG_P2P_MANAGER
159 static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
160 u8 minor_reason_code, const u8 *addr)
162 struct ieee80211_mgmt *mgmt;
166 if (hapd->driver->send_frame == NULL)
169 mgmt = os_zalloc(sizeof(*mgmt) + 100);
173 wpa_printf(MSG_DEBUG, "P2P: Disconnect STA " MACSTR " with minor "
174 "reason code %u (stype=%u)",
175 MAC2STR(addr), minor_reason_code, stype);
177 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
178 os_memcpy(mgmt->da, addr, ETH_ALEN);
179 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
180 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
181 if (stype == WLAN_FC_STYPE_DEAUTH) {
182 mgmt->u.deauth.reason_code =
183 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
184 pos = (u8 *) (&mgmt->u.deauth.reason_code + 1);
186 mgmt->u.disassoc.reason_code =
187 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
188 pos = (u8 *) (&mgmt->u.disassoc.reason_code + 1);
191 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
193 WPA_PUT_BE24(pos, OUI_WFA);
195 *pos++ = P2P_OUI_TYPE;
197 *pos++ = P2P_ATTR_MINOR_REASON_CODE;
198 WPA_PUT_LE16(pos, 1);
200 *pos++ = minor_reason_code;
202 ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt,
203 pos - (u8 *) mgmt, 1);
206 return ret < 0 ? -1 : 0;
208 #endif /* CONFIG_P2P_MANAGER */
211 static int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
215 struct sta_info *sta;
218 wpa_printf(MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s", txtaddr);
220 if (hwaddr_aton(txtaddr, addr))
223 pos = os_strstr(txtaddr, " test=");
225 struct ieee80211_mgmt mgmt;
227 if (hapd->driver->send_frame == NULL)
231 os_memset(&mgmt, 0, sizeof(mgmt));
232 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
233 WLAN_FC_STYPE_DEAUTH);
234 os_memcpy(mgmt.da, addr, ETH_ALEN);
235 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
236 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
237 mgmt.u.deauth.reason_code =
238 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
239 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
241 sizeof(mgmt.u.deauth),
247 #ifdef CONFIG_P2P_MANAGER
248 pos = os_strstr(txtaddr, " p2p=");
250 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
251 atoi(pos + 5), addr);
253 #endif /* CONFIG_P2P_MANAGER */
255 hapd->drv.sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
256 sta = ap_get_sta(hapd, addr);
258 ap_sta_deauthenticate(hapd, sta,
259 WLAN_REASON_PREV_AUTH_NOT_VALID);
265 static int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
269 struct sta_info *sta;
272 wpa_printf(MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s", txtaddr);
274 if (hwaddr_aton(txtaddr, addr))
277 pos = os_strstr(txtaddr, " test=");
279 struct ieee80211_mgmt mgmt;
281 if (hapd->driver->send_frame == NULL)
285 os_memset(&mgmt, 0, sizeof(mgmt));
286 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
287 WLAN_FC_STYPE_DISASSOC);
288 os_memcpy(mgmt.da, addr, ETH_ALEN);
289 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
290 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
291 mgmt.u.disassoc.reason_code =
292 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
293 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
295 sizeof(mgmt.u.deauth),
301 #ifdef CONFIG_P2P_MANAGER
302 pos = os_strstr(txtaddr, " p2p=");
304 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
305 atoi(pos + 5), addr);
307 #endif /* CONFIG_P2P_MANAGER */
309 hapd->drv.sta_disassoc(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
310 sta = ap_get_sta(hapd, addr);
312 ap_sta_disassociate(hapd, sta,
313 WLAN_REASON_PREV_AUTH_NOT_VALID);
319 #ifdef CONFIG_IEEE80211W
321 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
325 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
327 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
329 if (hwaddr_aton(txtaddr, addr) ||
330 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
333 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
337 #endif /* NEED_AP_MLME */
338 #endif /* CONFIG_IEEE80211W */
342 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
344 char *pin = os_strchr(txt, ' ');
347 u8 addr_buf[ETH_ALEN], *addr = NULL;
354 timeout_txt = os_strchr(pin, ' ');
356 *timeout_txt++ = '\0';
357 timeout = atoi(timeout_txt);
358 pos = os_strchr(timeout_txt, ' ');
361 if (hwaddr_aton(pos, addr_buf) == 0)
367 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
371 #ifdef CONFIG_WPS_OOB
372 static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
374 char *path, *method, *name;
376 path = os_strchr(txt, ' ');
381 method = os_strchr(path, ' ');
386 name = os_strchr(method, ' ');
390 return hostapd_wps_start_oob(hapd, txt, path, method, name);
392 #endif /* CONFIG_WPS_OOB */
395 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
396 char *buf, size_t buflen)
402 pos = os_strchr(txt, ' ');
406 if (os_strcmp(txt, "disable") == 0) {
407 hostapd_wps_ap_pin_disable(hapd);
408 return os_snprintf(buf, buflen, "OK\n");
411 if (os_strcmp(txt, "random") == 0) {
414 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
417 return os_snprintf(buf, buflen, "%s", pin_txt);
420 if (os_strcmp(txt, "get") == 0) {
421 pin_txt = hostapd_wps_ap_pin_get(hapd);
424 return os_snprintf(buf, buflen, "%s", pin_txt);
427 if (os_strcmp(txt, "set") == 0) {
432 pos = os_strchr(pos, ' ');
437 if (os_strlen(pin) > buflen)
439 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
441 return os_snprintf(buf, buflen, "%s", pin);
446 #endif /* CONFIG_WPS */
449 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
452 struct hostapd_data *hapd = eloop_ctx;
455 struct sockaddr_un from;
456 socklen_t fromlen = sizeof(from);
458 const int reply_size = 4096;
461 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
462 (struct sockaddr *) &from, &fromlen);
464 perror("recvfrom(ctrl_iface)");
468 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
470 reply = os_malloc(reply_size);
472 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
477 os_memcpy(reply, "OK\n", 3);
480 if (os_strcmp(buf, "PING") == 0) {
481 os_memcpy(reply, "PONG\n", 5);
483 } else if (os_strcmp(buf, "MIB") == 0) {
484 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
485 if (reply_len >= 0) {
486 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
487 reply_size - reply_len);
493 if (reply_len >= 0) {
494 res = ieee802_1x_get_mib(hapd, reply + reply_len,
495 reply_size - reply_len);
501 #ifndef CONFIG_NO_RADIUS
502 if (reply_len >= 0) {
503 res = radius_client_get_mib(hapd->radius,
505 reply_size - reply_len);
511 #endif /* CONFIG_NO_RADIUS */
512 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
513 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
515 } else if (os_strncmp(buf, "STA ", 4) == 0) {
516 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
518 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
519 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
521 } else if (os_strcmp(buf, "ATTACH") == 0) {
522 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
524 } else if (os_strcmp(buf, "DETACH") == 0) {
525 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
527 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
528 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
531 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
532 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
534 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
535 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
537 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
538 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
540 #ifdef CONFIG_IEEE80211W
542 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
543 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
545 #endif /* NEED_AP_MLME */
546 #endif /* CONFIG_IEEE80211W */
548 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
549 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
551 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
552 if (hostapd_wps_button_pushed(hapd))
554 #ifdef CONFIG_WPS_OOB
555 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
556 if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
558 #endif /* CONFIG_WPS_OOB */
559 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
560 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
562 #endif /* CONFIG_WPS */
564 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
569 os_memcpy(reply, "FAIL\n", 5);
572 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
577 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
582 if (hapd->conf->ctrl_interface == NULL)
585 len = os_strlen(hapd->conf->ctrl_interface) +
586 os_strlen(hapd->conf->iface) + 2;
587 buf = os_malloc(len);
591 os_snprintf(buf, len, "%s/%s",
592 hapd->conf->ctrl_interface, hapd->conf->iface);
598 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
599 const char *txt, size_t len)
601 struct hostapd_data *hapd = ctx;
604 hostapd_ctrl_iface_send(hapd, level, txt, len);
608 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
610 struct sockaddr_un addr;
614 hapd->ctrl_sock = -1;
616 if (hapd->conf->ctrl_interface == NULL)
619 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
620 if (errno == EEXIST) {
621 wpa_printf(MSG_DEBUG, "Using existing control "
622 "interface directory.");
624 perror("mkdir[ctrl_interface]");
629 if (hapd->conf->ctrl_interface_gid_set &&
630 chown(hapd->conf->ctrl_interface, 0,
631 hapd->conf->ctrl_interface_gid) < 0) {
632 perror("chown[ctrl_interface]");
636 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
637 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
640 s = socket(PF_UNIX, SOCK_DGRAM, 0);
642 perror("socket(PF_UNIX)");
646 os_memset(&addr, 0, sizeof(addr));
648 addr.sun_len = sizeof(addr);
649 #endif /* __FreeBSD__ */
650 addr.sun_family = AF_UNIX;
651 fname = hostapd_ctrl_iface_path(hapd);
654 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
655 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
656 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
658 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
659 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
660 " allow connections - assuming it was left"
661 "over from forced program termination");
662 if (unlink(fname) < 0) {
663 perror("unlink[ctrl_iface]");
664 wpa_printf(MSG_ERROR, "Could not unlink "
665 "existing ctrl_iface socket '%s'",
669 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
671 perror("bind(PF_UNIX)");
674 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
675 "ctrl_iface socket '%s'", fname);
677 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
678 "be in use - cannot override it");
679 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
680 "not used anymore", fname);
687 if (hapd->conf->ctrl_interface_gid_set &&
688 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
689 perror("chown[ctrl_interface/ifname]");
693 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
694 perror("chmod[ctrl_interface/ifname]");
700 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
702 hapd->msg_ctx = hapd;
703 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
718 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
720 struct wpa_ctrl_dst *dst, *prev;
722 if (hapd->ctrl_sock > -1) {
724 eloop_unregister_read_sock(hapd->ctrl_sock);
725 close(hapd->ctrl_sock);
726 hapd->ctrl_sock = -1;
727 fname = hostapd_ctrl_iface_path(hapd);
732 if (hapd->conf->ctrl_interface &&
733 rmdir(hapd->conf->ctrl_interface) < 0) {
734 if (errno == ENOTEMPTY) {
735 wpa_printf(MSG_DEBUG, "Control interface "
736 "directory not empty - leaving it "
739 perror("rmdir[ctrl_interface]");
744 dst = hapd->ctrl_dst;
753 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
754 const char *buf, size_t len)
756 struct wpa_ctrl_dst *dst, *next;
762 dst = hapd->ctrl_dst;
763 if (hapd->ctrl_sock < 0 || dst == NULL)
766 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
767 io[0].iov_base = levelstr;
768 io[0].iov_len = os_strlen(levelstr);
769 io[1].iov_base = (char *) buf;
771 os_memset(&msg, 0, sizeof(msg));
778 if (level >= dst->debug_level) {
779 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
780 (u8 *) dst->addr.sun_path, dst->addrlen -
781 offsetof(struct sockaddr_un, sun_path));
782 msg.msg_name = &dst->addr;
783 msg.msg_namelen = dst->addrlen;
784 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
786 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
788 idx, errno, strerror(errno));
790 if (dst->errors > 10 || _errno == ENOENT) {
791 hostapd_ctrl_iface_detach(
803 #endif /* CONFIG_NATIVE_WINDOWS */