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 static int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
162 struct sta_info *sta;
165 wpa_printf(MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s", txtaddr);
167 if (hwaddr_aton(txtaddr, addr))
170 pos = os_strstr(txtaddr, " test=");
172 struct ieee80211_mgmt mgmt;
174 if (hapd->driver->send_frame == NULL)
178 os_memset(&mgmt, 0, sizeof(mgmt));
179 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
180 WLAN_FC_STYPE_DEAUTH);
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 mgmt.u.deauth.reason_code =
185 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
186 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
188 sizeof(mgmt.u.deauth),
194 hapd->drv.sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
195 sta = ap_get_sta(hapd, addr);
197 ap_sta_deauthenticate(hapd, sta,
198 WLAN_REASON_PREV_AUTH_NOT_VALID);
204 static int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
208 struct sta_info *sta;
211 wpa_printf(MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s", txtaddr);
213 if (hwaddr_aton(txtaddr, addr))
216 pos = os_strstr(txtaddr, " test=");
218 struct ieee80211_mgmt mgmt;
220 if (hapd->driver->send_frame == NULL)
224 os_memset(&mgmt, 0, sizeof(mgmt));
225 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
226 WLAN_FC_STYPE_DISASSOC);
227 os_memcpy(mgmt.da, addr, ETH_ALEN);
228 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
229 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
230 mgmt.u.deauth.reason_code =
231 host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
232 if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
234 sizeof(mgmt.u.deauth),
240 hapd->drv.sta_disassoc(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
241 sta = ap_get_sta(hapd, addr);
243 ap_sta_disassociate(hapd, sta,
244 WLAN_REASON_PREV_AUTH_NOT_VALID);
250 #ifdef CONFIG_IEEE80211W
252 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
256 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
258 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
260 if (hwaddr_aton(txtaddr, addr) ||
261 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
264 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
268 #endif /* NEED_AP_MLME */
269 #endif /* CONFIG_IEEE80211W */
273 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
275 char *pin = os_strchr(txt, ' ');
283 timeout_txt = os_strchr(pin, ' ');
285 *timeout_txt++ = '\0';
286 timeout = atoi(timeout_txt);
290 return hostapd_wps_add_pin(hapd, txt, pin, timeout);
294 #ifdef CONFIG_WPS_OOB
295 static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
297 char *path, *method, *name;
299 path = os_strchr(txt, ' ');
304 method = os_strchr(path, ' ');
309 name = os_strchr(method, ' ');
313 return hostapd_wps_start_oob(hapd, txt, path, method, name);
315 #endif /* CONFIG_WPS_OOB */
316 #endif /* CONFIG_WPS */
319 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
322 struct hostapd_data *hapd = eloop_ctx;
325 struct sockaddr_un from;
326 socklen_t fromlen = sizeof(from);
328 const int reply_size = 4096;
331 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
332 (struct sockaddr *) &from, &fromlen);
334 perror("recvfrom(ctrl_iface)");
338 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
340 reply = os_malloc(reply_size);
342 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
347 os_memcpy(reply, "OK\n", 3);
350 if (os_strcmp(buf, "PING") == 0) {
351 os_memcpy(reply, "PONG\n", 5);
353 } else if (os_strcmp(buf, "MIB") == 0) {
354 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
355 if (reply_len >= 0) {
356 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
357 reply_size - reply_len);
363 if (reply_len >= 0) {
364 res = ieee802_1x_get_mib(hapd, reply + reply_len,
365 reply_size - reply_len);
371 #ifndef CONFIG_NO_RADIUS
372 if (reply_len >= 0) {
373 res = radius_client_get_mib(hapd->radius,
375 reply_size - reply_len);
381 #endif /* CONFIG_NO_RADIUS */
382 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
383 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
385 } else if (os_strncmp(buf, "STA ", 4) == 0) {
386 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
388 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
389 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
391 } else if (os_strcmp(buf, "ATTACH") == 0) {
392 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
394 } else if (os_strcmp(buf, "DETACH") == 0) {
395 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
397 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
398 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
401 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
402 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
404 } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
405 if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
407 } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
408 if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
410 #ifdef CONFIG_IEEE80211W
412 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
413 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
415 #endif /* NEED_AP_MLME */
416 #endif /* CONFIG_IEEE80211W */
418 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
419 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
421 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
422 if (hostapd_wps_button_pushed(hapd))
424 #ifdef CONFIG_WPS_OOB
425 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
426 if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
428 #endif /* CONFIG_WPS_OOB */
429 #endif /* CONFIG_WPS */
431 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
436 os_memcpy(reply, "FAIL\n", 5);
439 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
444 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
449 if (hapd->conf->ctrl_interface == NULL)
452 len = os_strlen(hapd->conf->ctrl_interface) +
453 os_strlen(hapd->conf->iface) + 2;
454 buf = os_malloc(len);
458 os_snprintf(buf, len, "%s/%s",
459 hapd->conf->ctrl_interface, hapd->conf->iface);
465 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
466 const char *txt, size_t len)
468 struct hostapd_data *hapd = ctx;
471 hostapd_ctrl_iface_send(hapd, level, txt, len);
475 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
477 struct sockaddr_un addr;
481 hapd->ctrl_sock = -1;
483 if (hapd->conf->ctrl_interface == NULL)
486 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
487 if (errno == EEXIST) {
488 wpa_printf(MSG_DEBUG, "Using existing control "
489 "interface directory.");
491 perror("mkdir[ctrl_interface]");
496 if (hapd->conf->ctrl_interface_gid_set &&
497 chown(hapd->conf->ctrl_interface, 0,
498 hapd->conf->ctrl_interface_gid) < 0) {
499 perror("chown[ctrl_interface]");
503 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
504 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
507 s = socket(PF_UNIX, SOCK_DGRAM, 0);
509 perror("socket(PF_UNIX)");
513 os_memset(&addr, 0, sizeof(addr));
515 addr.sun_len = sizeof(addr);
516 #endif /* __FreeBSD__ */
517 addr.sun_family = AF_UNIX;
518 fname = hostapd_ctrl_iface_path(hapd);
521 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
522 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
523 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
525 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
526 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
527 " allow connections - assuming it was left"
528 "over from forced program termination");
529 if (unlink(fname) < 0) {
530 perror("unlink[ctrl_iface]");
531 wpa_printf(MSG_ERROR, "Could not unlink "
532 "existing ctrl_iface socket '%s'",
536 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
538 perror("bind(PF_UNIX)");
541 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
542 "ctrl_iface socket '%s'", fname);
544 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
545 "be in use - cannot override it");
546 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
547 "not used anymore", fname);
554 if (hapd->conf->ctrl_interface_gid_set &&
555 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
556 perror("chown[ctrl_interface/ifname]");
560 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
561 perror("chmod[ctrl_interface/ifname]");
567 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
569 hapd->msg_ctx = hapd;
570 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
585 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
587 struct wpa_ctrl_dst *dst, *prev;
589 if (hapd->ctrl_sock > -1) {
591 eloop_unregister_read_sock(hapd->ctrl_sock);
592 close(hapd->ctrl_sock);
593 hapd->ctrl_sock = -1;
594 fname = hostapd_ctrl_iface_path(hapd);
599 if (hapd->conf->ctrl_interface &&
600 rmdir(hapd->conf->ctrl_interface) < 0) {
601 if (errno == ENOTEMPTY) {
602 wpa_printf(MSG_DEBUG, "Control interface "
603 "directory not empty - leaving it "
606 perror("rmdir[ctrl_interface]");
611 dst = hapd->ctrl_dst;
620 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
621 const char *buf, size_t len)
623 struct wpa_ctrl_dst *dst, *next;
629 dst = hapd->ctrl_dst;
630 if (hapd->ctrl_sock < 0 || dst == NULL)
633 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
634 io[0].iov_base = levelstr;
635 io[0].iov_len = os_strlen(levelstr);
636 io[1].iov_base = (char *) buf;
638 os_memset(&msg, 0, sizeof(msg));
645 if (level >= dst->debug_level) {
646 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
647 (u8 *) dst->addr.sun_path, dst->addrlen -
648 offsetof(struct sockaddr_un, sun_path));
649 msg.msg_name = &dst->addr;
650 msg.msg_namelen = dst->addrlen;
651 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
653 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
655 idx, errno, strerror(errno));
657 if (dst->errors > 10 || _errno == ENOENT) {
658 hostapd_ctrl_iface_detach(
670 #endif /* CONFIG_NATIVE_WINDOWS */