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 "wps/wps_defs.h"
38 #include "ctrl_iface.h"
42 struct wpa_ctrl_dst *next;
43 struct sockaddr_un addr;
50 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
51 const char *buf, size_t len);
54 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
55 struct sockaddr_un *from,
58 struct wpa_ctrl_dst *dst;
60 dst = os_zalloc(sizeof(*dst));
63 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
64 dst->addrlen = fromlen;
65 dst->debug_level = MSG_INFO;
66 dst->next = hapd->ctrl_dst;
68 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
69 (u8 *) from->sun_path,
70 fromlen - offsetof(struct sockaddr_un, sun_path));
75 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
76 struct sockaddr_un *from,
79 struct wpa_ctrl_dst *dst, *prev = NULL;
83 if (fromlen == dst->addrlen &&
84 os_memcmp(from->sun_path, dst->addr.sun_path,
85 fromlen - offsetof(struct sockaddr_un, sun_path))
88 hapd->ctrl_dst = dst->next;
90 prev->next = dst->next;
92 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
93 (u8 *) from->sun_path,
95 offsetof(struct sockaddr_un, sun_path));
105 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
106 struct sockaddr_un *from,
110 struct wpa_ctrl_dst *dst;
112 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
114 dst = hapd->ctrl_dst;
116 if (fromlen == dst->addrlen &&
117 os_memcmp(from->sun_path, dst->addr.sun_path,
118 fromlen - offsetof(struct sockaddr_un, sun_path))
120 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
121 "level", (u8 *) from->sun_path, fromlen -
122 offsetof(struct sockaddr_un, sun_path));
123 dst->debug_level = atoi(level);
133 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
137 struct sta_info *sta;
139 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
141 if (hwaddr_aton(txtaddr, addr))
144 sta = ap_get_sta(hapd, addr);
148 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
149 "notification", MAC2STR(addr));
150 sta = ap_sta_add(hapd, addr);
154 hostapd_new_assoc_sta(hapd, sta, 0);
159 #ifdef CONFIG_P2P_MANAGER
160 static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
161 u8 minor_reason_code, const u8 *addr)
163 struct ieee80211_mgmt *mgmt;
167 if (hapd->driver->send_frame == NULL)
170 mgmt = os_zalloc(sizeof(*mgmt) + 100);
174 wpa_printf(MSG_DEBUG, "P2P: Disconnect STA " MACSTR " with minor "
175 "reason code %u (stype=%u)",
176 MAC2STR(addr), minor_reason_code, stype);
178 mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
179 os_memcpy(mgmt->da, addr, ETH_ALEN);
180 os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
181 os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
182 if (stype == WLAN_FC_STYPE_DEAUTH) {
183 mgmt->u.deauth.reason_code =
184 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
185 pos = (u8 *) (&mgmt->u.deauth.reason_code + 1);
187 mgmt->u.disassoc.reason_code =
188 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
189 pos = (u8 *) (&mgmt->u.disassoc.reason_code + 1);
192 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
194 WPA_PUT_BE24(pos, OUI_WFA);
196 *pos++ = P2P_OUI_TYPE;
198 *pos++ = P2P_ATTR_MINOR_REASON_CODE;
199 WPA_PUT_LE16(pos, 1);
201 *pos++ = minor_reason_code;
203 ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt,
204 pos - (u8 *) mgmt, 1);
207 return ret < 0 ? -1 : 0;
209 #endif /* CONFIG_P2P_MANAGER */
212 static int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
216 struct sta_info *sta;
219 wpa_printf(MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s", txtaddr);
221 if (hwaddr_aton(txtaddr, addr))
224 pos = os_strstr(txtaddr, " test=");
226 struct ieee80211_mgmt mgmt;
228 if (hapd->driver->send_frame == NULL)
232 os_memset(&mgmt, 0, sizeof(mgmt));
233 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
234 WLAN_FC_STYPE_DEAUTH);
235 os_memcpy(mgmt.da, addr, ETH_ALEN);
236 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
237 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
238 mgmt.u.deauth.reason_code =
239 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
240 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
242 sizeof(mgmt.u.deauth),
248 #ifdef CONFIG_P2P_MANAGER
249 pos = os_strstr(txtaddr, " p2p=");
251 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
252 atoi(pos + 5), addr);
254 #endif /* CONFIG_P2P_MANAGER */
256 hapd->drv.sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
257 sta = ap_get_sta(hapd, addr);
259 ap_sta_deauthenticate(hapd, sta,
260 WLAN_REASON_PREV_AUTH_NOT_VALID);
266 static int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
270 struct sta_info *sta;
273 wpa_printf(MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s", txtaddr);
275 if (hwaddr_aton(txtaddr, addr))
278 pos = os_strstr(txtaddr, " test=");
280 struct ieee80211_mgmt mgmt;
282 if (hapd->driver->send_frame == NULL)
286 os_memset(&mgmt, 0, sizeof(mgmt));
287 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
288 WLAN_FC_STYPE_DISASSOC);
289 os_memcpy(mgmt.da, addr, ETH_ALEN);
290 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
291 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
292 mgmt.u.disassoc.reason_code =
293 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
294 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
296 sizeof(mgmt.u.deauth),
302 #ifdef CONFIG_P2P_MANAGER
303 pos = os_strstr(txtaddr, " p2p=");
305 return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
306 atoi(pos + 5), addr);
308 #endif /* CONFIG_P2P_MANAGER */
310 hapd->drv.sta_disassoc(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
311 sta = ap_get_sta(hapd, addr);
313 ap_sta_disassociate(hapd, sta,
314 WLAN_REASON_PREV_AUTH_NOT_VALID);
320 #ifdef CONFIG_IEEE80211W
322 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
326 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
328 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
330 if (hwaddr_aton(txtaddr, addr) ||
331 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
334 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
338 #endif /* NEED_AP_MLME */
339 #endif /* CONFIG_IEEE80211W */
343 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
345 char *pin = os_strchr(txt, ' ');
348 u8 addr_buf[ETH_ALEN], *addr = NULL;
355 timeout_txt = os_strchr(pin, ' ');
357 *timeout_txt++ = '\0';
358 timeout = atoi(timeout_txt);
359 pos = os_strchr(timeout_txt, ' ');
362 if (hwaddr_aton(pos, addr_buf) == 0)
368 return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
372 #ifdef CONFIG_WPS_OOB
373 static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
375 char *path, *method, *name;
377 path = os_strchr(txt, ' ');
382 method = os_strchr(path, ' ');
387 name = os_strchr(method, ' ');
391 return hostapd_wps_start_oob(hapd, txt, path, method, name);
393 #endif /* CONFIG_WPS_OOB */
396 static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
397 char *buf, size_t buflen)
403 pos = os_strchr(txt, ' ');
407 if (os_strcmp(txt, "disable") == 0) {
408 hostapd_wps_ap_pin_disable(hapd);
409 return os_snprintf(buf, buflen, "OK\n");
412 if (os_strcmp(txt, "random") == 0) {
415 pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
418 return os_snprintf(buf, buflen, "%s", pin_txt);
421 if (os_strcmp(txt, "get") == 0) {
422 pin_txt = hostapd_wps_ap_pin_get(hapd);
425 return os_snprintf(buf, buflen, "%s", pin_txt);
428 if (os_strcmp(txt, "set") == 0) {
433 pos = os_strchr(pos, ' ');
438 if (os_strlen(pin) > buflen)
440 if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
442 return os_snprintf(buf, buflen, "%s", pin);
447 #endif /* CONFIG_WPS */
450 static int hostapd_ctrl_iface_set(struct hostapd_data *wpa_s, char *cmd)
455 value = os_strchr(cmd, ' ');
460 wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
462 #ifdef CONFIG_WPS_TESTING
463 } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
465 val = strtol(value, NULL, 0);
466 if (val < 0 || val > 0xff) {
468 wpa_printf(MSG_DEBUG, "WPS: Invalid "
469 "wps_version_number %ld", val);
471 wps_version_number = val;
472 wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
474 (wps_version_number & 0xf0) >> 4,
475 wps_version_number & 0x0f);
477 } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
478 wps_testing_dummy_cred = atoi(value);
479 wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
480 wps_testing_dummy_cred);
481 #endif /* CONFIG_WPS_TESTING */
490 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
493 struct hostapd_data *hapd = eloop_ctx;
496 struct sockaddr_un from;
497 socklen_t fromlen = sizeof(from);
499 const int reply_size = 4096;
502 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
503 (struct sockaddr *) &from, &fromlen);
505 perror("recvfrom(ctrl_iface)");
509 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
511 reply = os_malloc(reply_size);
513 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
518 os_memcpy(reply, "OK\n", 3);
521 if (os_strcmp(buf, "PING") == 0) {
522 os_memcpy(reply, "PONG\n", 5);
524 } else if (os_strcmp(buf, "MIB") == 0) {
525 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
526 if (reply_len >= 0) {
527 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
528 reply_size - reply_len);
534 if (reply_len >= 0) {
535 res = ieee802_1x_get_mib(hapd, reply + reply_len,
536 reply_size - reply_len);
542 #ifndef CONFIG_NO_RADIUS
543 if (reply_len >= 0) {
544 res = radius_client_get_mib(hapd->radius,
546 reply_size - reply_len);
552 #endif /* CONFIG_NO_RADIUS */
553 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
554 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
556 } else if (os_strncmp(buf, "STA ", 4) == 0) {
557 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
559 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
560 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
562 } else if (os_strcmp(buf, "ATTACH") == 0) {
563 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
565 } else if (os_strcmp(buf, "DETACH") == 0) {
566 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
568 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
569 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
572 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
573 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
575 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
576 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
578 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
579 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
581 #ifdef CONFIG_IEEE80211W
583 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
584 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
586 #endif /* NEED_AP_MLME */
587 #endif /* CONFIG_IEEE80211W */
589 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
590 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
592 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
593 if (hostapd_wps_button_pushed(hapd))
595 #ifdef CONFIG_WPS_OOB
596 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
597 if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
599 #endif /* CONFIG_WPS_OOB */
600 } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
601 reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
603 #endif /* CONFIG_WPS */
604 } else if (os_strncmp(buf, "SET ", 4) == 0) {
605 if (hostapd_ctrl_iface_set(hapd, buf + 4))
608 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
613 os_memcpy(reply, "FAIL\n", 5);
616 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
621 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
626 if (hapd->conf->ctrl_interface == NULL)
629 len = os_strlen(hapd->conf->ctrl_interface) +
630 os_strlen(hapd->conf->iface) + 2;
631 buf = os_malloc(len);
635 os_snprintf(buf, len, "%s/%s",
636 hapd->conf->ctrl_interface, hapd->conf->iface);
642 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
643 const char *txt, size_t len)
645 struct hostapd_data *hapd = ctx;
648 hostapd_ctrl_iface_send(hapd, level, txt, len);
652 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
654 struct sockaddr_un addr;
658 hapd->ctrl_sock = -1;
660 if (hapd->conf->ctrl_interface == NULL)
663 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
664 if (errno == EEXIST) {
665 wpa_printf(MSG_DEBUG, "Using existing control "
666 "interface directory.");
668 perror("mkdir[ctrl_interface]");
673 if (hapd->conf->ctrl_interface_gid_set &&
674 chown(hapd->conf->ctrl_interface, 0,
675 hapd->conf->ctrl_interface_gid) < 0) {
676 perror("chown[ctrl_interface]");
680 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
681 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
684 s = socket(PF_UNIX, SOCK_DGRAM, 0);
686 perror("socket(PF_UNIX)");
690 os_memset(&addr, 0, sizeof(addr));
692 addr.sun_len = sizeof(addr);
693 #endif /* __FreeBSD__ */
694 addr.sun_family = AF_UNIX;
695 fname = hostapd_ctrl_iface_path(hapd);
698 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
699 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
700 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
702 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
703 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
704 " allow connections - assuming it was left"
705 "over from forced program termination");
706 if (unlink(fname) < 0) {
707 perror("unlink[ctrl_iface]");
708 wpa_printf(MSG_ERROR, "Could not unlink "
709 "existing ctrl_iface socket '%s'",
713 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
715 perror("bind(PF_UNIX)");
718 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
719 "ctrl_iface socket '%s'", fname);
721 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
722 "be in use - cannot override it");
723 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
724 "not used anymore", fname);
731 if (hapd->conf->ctrl_interface_gid_set &&
732 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
733 perror("chown[ctrl_interface/ifname]");
737 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
738 perror("chmod[ctrl_interface/ifname]");
744 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
746 hapd->msg_ctx = hapd;
747 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
762 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
764 struct wpa_ctrl_dst *dst, *prev;
766 if (hapd->ctrl_sock > -1) {
768 eloop_unregister_read_sock(hapd->ctrl_sock);
769 close(hapd->ctrl_sock);
770 hapd->ctrl_sock = -1;
771 fname = hostapd_ctrl_iface_path(hapd);
776 if (hapd->conf->ctrl_interface &&
777 rmdir(hapd->conf->ctrl_interface) < 0) {
778 if (errno == ENOTEMPTY) {
779 wpa_printf(MSG_DEBUG, "Control interface "
780 "directory not empty - leaving it "
783 perror("rmdir[ctrl_interface]");
788 dst = hapd->ctrl_dst;
797 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
798 const char *buf, size_t len)
800 struct wpa_ctrl_dst *dst, *next;
806 dst = hapd->ctrl_dst;
807 if (hapd->ctrl_sock < 0 || dst == NULL)
810 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
811 io[0].iov_base = levelstr;
812 io[0].iov_len = os_strlen(levelstr);
813 io[1].iov_base = (char *) buf;
815 os_memset(&msg, 0, sizeof(msg));
822 if (level >= dst->debug_level) {
823 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
824 (u8 *) dst->addr.sun_path, dst->addrlen -
825 offsetof(struct sockaddr_un, sun_path));
826 msg.msg_name = &dst->addr;
827 msg.msg_namelen = dst->addrlen;
828 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
830 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
832 idx, errno, strerror(errno));
834 if (dst->errors > 10 || _errno == ENOENT) {
835 hostapd_ctrl_iface_detach(
847 #endif /* CONFIG_NATIVE_WINDOWS */