2 * hostapd / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2008, 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.
17 #ifndef CONFIG_NATIVE_WINDOWS
26 #include "ieee802_1x.h"
28 #include "radius/radius_client.h"
29 #include "ieee802_11.h"
30 #include "ctrl_iface.h"
32 #include "accounting.h"
33 #include "wps_hostapd.h"
34 #include "drivers/driver.h"
38 struct wpa_ctrl_dst *next;
39 struct sockaddr_un addr;
46 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
47 const char *buf, size_t len);
50 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
51 struct sockaddr_un *from,
54 struct wpa_ctrl_dst *dst;
56 dst = os_zalloc(sizeof(*dst));
59 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
60 dst->addrlen = fromlen;
61 dst->debug_level = MSG_INFO;
62 dst->next = hapd->ctrl_dst;
64 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
65 (u8 *) from->sun_path,
66 fromlen - offsetof(struct sockaddr_un, sun_path));
71 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
72 struct sockaddr_un *from,
75 struct wpa_ctrl_dst *dst, *prev = NULL;
79 if (fromlen == dst->addrlen &&
80 os_memcmp(from->sun_path, dst->addr.sun_path,
81 fromlen - offsetof(struct sockaddr_un, sun_path))
84 hapd->ctrl_dst = dst->next;
86 prev->next = dst->next;
88 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
89 (u8 *) from->sun_path,
91 offsetof(struct sockaddr_un, sun_path));
101 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
102 struct sockaddr_un *from,
106 struct wpa_ctrl_dst *dst;
108 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
110 dst = hapd->ctrl_dst;
112 if (fromlen == dst->addrlen &&
113 os_memcmp(from->sun_path, dst->addr.sun_path,
114 fromlen - offsetof(struct sockaddr_un, sun_path))
116 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
117 "level", (u8 *) from->sun_path, fromlen -
118 offsetof(struct sockaddr_un, sun_path));
119 dst->debug_level = atoi(level);
129 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
130 struct sta_info *sta,
131 char *buf, size_t buflen)
136 ret = os_snprintf(buf, buflen, "FAIL\n");
137 if (ret < 0 || (size_t) ret >= buflen)
143 ret = os_snprintf(buf + len, buflen - len, MACSTR "\n",
145 if (ret < 0 || (size_t) ret >= buflen - len)
149 res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
152 res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
155 res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
158 res = hostapd_wps_get_mib_sta(hapd, sta->addr, buf + len,
167 static int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
168 char *buf, size_t buflen)
170 return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
174 static int hostapd_ctrl_iface_sta(struct hostapd_data *hapd,
176 char *buf, size_t buflen)
181 if (hwaddr_aton(txtaddr, addr)) {
182 ret = os_snprintf(buf, buflen, "FAIL\n");
183 if (ret < 0 || (size_t) ret >= buflen)
187 return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
192 static int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd,
194 char *buf, size_t buflen)
197 struct sta_info *sta;
200 if (hwaddr_aton(txtaddr, addr) ||
201 (sta = ap_get_sta(hapd, addr)) == NULL) {
202 ret = os_snprintf(buf, buflen, "FAIL\n");
203 if (ret < 0 || (size_t) ret >= buflen)
207 return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
211 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
215 struct sta_info *sta;
217 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
219 if (hwaddr_aton(txtaddr, addr))
222 sta = ap_get_sta(hapd, addr);
226 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
227 "notification", MAC2STR(addr));
228 sta = ap_sta_add(hapd, addr);
232 hostapd_new_assoc_sta(hapd, sta, 0);
237 #ifdef CONFIG_IEEE80211W
239 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
243 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
245 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
247 if (hwaddr_aton(txtaddr, addr))
250 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
251 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
255 #endif /* NEED_AP_MLME */
256 #endif /* CONFIG_IEEE80211W */
260 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
262 char *pin = os_strchr(txt, ' ');
270 timeout_txt = os_strchr(pin, ' ');
272 *timeout_txt++ = '\0';
273 timeout = atoi(timeout_txt);
277 return hostapd_wps_add_pin(hapd, txt, pin, timeout);
281 #ifdef CONFIG_WPS_OOB
282 static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
284 char *path, *method, *name;
286 path = os_strchr(txt, ' ');
291 method = os_strchr(path, ' ');
296 name = os_strchr(method, ' ');
300 return hostapd_wps_start_oob(hapd, txt, path, method, name);
302 #endif /* CONFIG_WPS_OOB */
303 #endif /* CONFIG_WPS */
306 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
309 struct hostapd_data *hapd = eloop_ctx;
312 struct sockaddr_un from;
313 socklen_t fromlen = sizeof(from);
315 const int reply_size = 4096;
318 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
319 (struct sockaddr *) &from, &fromlen);
321 perror("recvfrom(ctrl_iface)");
325 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
327 reply = os_malloc(reply_size);
329 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
334 os_memcpy(reply, "OK\n", 3);
337 if (os_strcmp(buf, "PING") == 0) {
338 os_memcpy(reply, "PONG\n", 5);
340 } else if (os_strcmp(buf, "MIB") == 0) {
341 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
342 if (reply_len >= 0) {
343 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
344 reply_size - reply_len);
350 if (reply_len >= 0) {
351 res = ieee802_1x_get_mib(hapd, reply + reply_len,
352 reply_size - reply_len);
358 if (reply_len >= 0) {
359 res = radius_client_get_mib(hapd->radius,
361 reply_size - reply_len);
367 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
368 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
370 } else if (os_strncmp(buf, "STA ", 4) == 0) {
371 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
373 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
374 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
376 } else if (os_strcmp(buf, "ATTACH") == 0) {
377 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
379 } else if (os_strcmp(buf, "DETACH") == 0) {
380 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
382 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
383 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
386 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
387 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
389 #ifdef CONFIG_IEEE80211W
391 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
392 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
394 #endif /* NEED_AP_MLME */
395 #endif /* CONFIG_IEEE80211W */
397 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
398 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
400 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
401 if (hostapd_wps_button_pushed(hapd))
403 #ifdef CONFIG_WPS_OOB
404 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
405 if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
407 #endif /* CONFIG_WPS_OOB */
408 #endif /* CONFIG_WPS */
410 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
415 os_memcpy(reply, "FAIL\n", 5);
418 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
423 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
428 if (hapd->conf->ctrl_interface == NULL)
431 len = os_strlen(hapd->conf->ctrl_interface) +
432 os_strlen(hapd->conf->iface) + 2;
433 buf = os_malloc(len);
437 os_snprintf(buf, len, "%s/%s",
438 hapd->conf->ctrl_interface, hapd->conf->iface);
444 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
445 const char *txt, size_t len)
447 struct hostapd_data *hapd = ctx;
450 hostapd_ctrl_iface_send(hapd, level, txt, len);
454 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
456 struct sockaddr_un addr;
460 hapd->ctrl_sock = -1;
462 if (hapd->conf->ctrl_interface == NULL)
465 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
466 if (errno == EEXIST) {
467 wpa_printf(MSG_DEBUG, "Using existing control "
468 "interface directory.");
470 perror("mkdir[ctrl_interface]");
475 if (hapd->conf->ctrl_interface_gid_set &&
476 chown(hapd->conf->ctrl_interface, 0,
477 hapd->conf->ctrl_interface_gid) < 0) {
478 perror("chown[ctrl_interface]");
482 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
483 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
486 s = socket(PF_UNIX, SOCK_DGRAM, 0);
488 perror("socket(PF_UNIX)");
492 os_memset(&addr, 0, sizeof(addr));
494 addr.sun_len = sizeof(addr);
495 #endif /* __FreeBSD__ */
496 addr.sun_family = AF_UNIX;
497 fname = hostapd_ctrl_iface_path(hapd);
500 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
501 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
502 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
504 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
505 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
506 " allow connections - assuming it was left"
507 "over from forced program termination");
508 if (unlink(fname) < 0) {
509 perror("unlink[ctrl_iface]");
510 wpa_printf(MSG_ERROR, "Could not unlink "
511 "existing ctrl_iface socket '%s'",
515 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
517 perror("bind(PF_UNIX)");
520 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
521 "ctrl_iface socket '%s'", fname);
523 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
524 "be in use - cannot override it");
525 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
526 "not used anymore", fname);
533 if (hapd->conf->ctrl_interface_gid_set &&
534 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
535 perror("chown[ctrl_interface/ifname]");
539 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
540 perror("chmod[ctrl_interface/ifname]");
546 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
548 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
563 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
565 struct wpa_ctrl_dst *dst, *prev;
567 if (hapd->ctrl_sock > -1) {
569 eloop_unregister_read_sock(hapd->ctrl_sock);
570 close(hapd->ctrl_sock);
571 hapd->ctrl_sock = -1;
572 fname = hostapd_ctrl_iface_path(hapd);
577 if (hapd->conf->ctrl_interface &&
578 rmdir(hapd->conf->ctrl_interface) < 0) {
579 if (errno == ENOTEMPTY) {
580 wpa_printf(MSG_DEBUG, "Control interface "
581 "directory not empty - leaving it "
584 perror("rmdir[ctrl_interface]");
589 dst = hapd->ctrl_dst;
598 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
599 const char *buf, size_t len)
601 struct wpa_ctrl_dst *dst, *next;
607 dst = hapd->ctrl_dst;
608 if (hapd->ctrl_sock < 0 || dst == NULL)
611 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
612 io[0].iov_base = levelstr;
613 io[0].iov_len = os_strlen(levelstr);
614 io[1].iov_base = (char *) buf;
616 os_memset(&msg, 0, sizeof(msg));
623 if (level >= dst->debug_level) {
624 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
625 (u8 *) dst->addr.sun_path, dst->addrlen -
626 offsetof(struct sockaddr_un, sun_path));
627 msg.msg_name = &dst->addr;
628 msg.msg_namelen = dst->addrlen;
629 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
631 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
633 idx, errno, strerror(errno));
635 if (dst->errors > 10 || _errno == ENOENT) {
636 hostapd_ctrl_iface_detach(
648 #endif /* CONFIG_NATIVE_WINDOWS */