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);
163 static int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
164 char *buf, size_t buflen)
166 return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
170 static int hostapd_ctrl_iface_sta(struct hostapd_data *hapd,
172 char *buf, size_t buflen)
177 if (hwaddr_aton(txtaddr, addr)) {
178 ret = os_snprintf(buf, buflen, "FAIL\n");
179 if (ret < 0 || (size_t) ret >= buflen)
183 return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
188 static int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd,
190 char *buf, size_t buflen)
193 struct sta_info *sta;
196 if (hwaddr_aton(txtaddr, addr) ||
197 (sta = ap_get_sta(hapd, addr)) == NULL) {
198 ret = os_snprintf(buf, buflen, "FAIL\n");
199 if (ret < 0 || (size_t) ret >= buflen)
203 return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
207 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
211 struct sta_info *sta;
213 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
215 if (hwaddr_aton(txtaddr, addr))
218 sta = ap_get_sta(hapd, addr);
222 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
223 "notification", MAC2STR(addr));
224 sta = ap_sta_add(hapd, addr);
228 hostapd_new_assoc_sta(hapd, sta, 0);
233 #ifdef CONFIG_IEEE80211W
235 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
239 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
241 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
243 if (hwaddr_aton(txtaddr, addr))
246 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
247 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
251 #endif /* NEED_MLME */
252 #endif /* CONFIG_IEEE80211W */
256 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
258 char *pin = os_strchr(txt, ' ');
266 timeout_txt = os_strchr(pin, ' ');
268 *timeout_txt++ = '\0';
269 timeout = atoi(timeout_txt);
273 return hostapd_wps_add_pin(hapd, txt, pin, timeout);
277 #ifdef CONFIG_WPS_OOB
278 static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
280 char *path, *method, *name;
282 path = os_strchr(txt, ' ');
287 method = os_strchr(path, ' ');
292 name = os_strchr(method, ' ');
296 return hostapd_wps_start_oob(hapd, txt, path, method, name);
298 #endif /* CONFIG_WPS_OOB */
299 #endif /* CONFIG_WPS */
302 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
305 struct hostapd_data *hapd = eloop_ctx;
308 struct sockaddr_un from;
309 socklen_t fromlen = sizeof(from);
311 const int reply_size = 4096;
314 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
315 (struct sockaddr *) &from, &fromlen);
317 perror("recvfrom(ctrl_iface)");
321 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
323 reply = os_malloc(reply_size);
325 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
330 os_memcpy(reply, "OK\n", 3);
333 if (os_strcmp(buf, "PING") == 0) {
334 os_memcpy(reply, "PONG\n", 5);
336 } else if (os_strcmp(buf, "MIB") == 0) {
337 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
338 if (reply_len >= 0) {
339 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
340 reply_size - reply_len);
346 if (reply_len >= 0) {
347 res = ieee802_1x_get_mib(hapd, reply + reply_len,
348 reply_size - reply_len);
354 if (reply_len >= 0) {
355 res = radius_client_get_mib(hapd->radius,
357 reply_size - reply_len);
363 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
364 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
366 } else if (os_strncmp(buf, "STA ", 4) == 0) {
367 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
369 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
370 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
372 } else if (os_strcmp(buf, "ATTACH") == 0) {
373 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
375 } else if (os_strcmp(buf, "DETACH") == 0) {
376 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
378 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
379 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
382 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
383 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
385 #ifdef CONFIG_IEEE80211W
387 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
388 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
390 #endif /* NEED_MLME */
391 #endif /* CONFIG_IEEE80211W */
393 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
394 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
396 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
397 if (hostapd_wps_button_pushed(hapd))
399 #ifdef CONFIG_WPS_OOB
400 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
401 if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
403 #endif /* CONFIG_WPS_OOB */
404 #endif /* CONFIG_WPS */
406 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
411 os_memcpy(reply, "FAIL\n", 5);
414 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
419 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
424 if (hapd->conf->ctrl_interface == NULL)
427 len = os_strlen(hapd->conf->ctrl_interface) +
428 os_strlen(hapd->conf->iface) + 2;
429 buf = os_malloc(len);
433 os_snprintf(buf, len, "%s/%s",
434 hapd->conf->ctrl_interface, hapd->conf->iface);
440 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
441 const char *txt, size_t len)
443 struct hostapd_data *hapd = ctx;
446 hostapd_ctrl_iface_send(hapd, level, txt, len);
450 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
452 struct sockaddr_un addr;
456 hapd->ctrl_sock = -1;
458 if (hapd->conf->ctrl_interface == NULL)
461 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
462 if (errno == EEXIST) {
463 wpa_printf(MSG_DEBUG, "Using existing control "
464 "interface directory.");
466 perror("mkdir[ctrl_interface]");
471 if (hapd->conf->ctrl_interface_gid_set &&
472 chown(hapd->conf->ctrl_interface, 0,
473 hapd->conf->ctrl_interface_gid) < 0) {
474 perror("chown[ctrl_interface]");
478 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
479 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
482 s = socket(PF_UNIX, SOCK_DGRAM, 0);
484 perror("socket(PF_UNIX)");
488 os_memset(&addr, 0, sizeof(addr));
490 addr.sun_len = sizeof(addr);
491 #endif /* __FreeBSD__ */
492 addr.sun_family = AF_UNIX;
493 fname = hostapd_ctrl_iface_path(hapd);
496 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
497 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
498 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
500 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
501 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
502 " allow connections - assuming it was left"
503 "over from forced program termination");
504 if (unlink(fname) < 0) {
505 perror("unlink[ctrl_iface]");
506 wpa_printf(MSG_ERROR, "Could not unlink "
507 "existing ctrl_iface socket '%s'",
511 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
513 perror("bind(PF_UNIX)");
516 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
517 "ctrl_iface socket '%s'", fname);
519 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
520 "be in use - cannot override it");
521 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
522 "not used anymore", fname);
529 if (hapd->conf->ctrl_interface_gid_set &&
530 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
531 perror("chown[ctrl_interface/ifname]");
535 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
536 perror("chmod[ctrl_interface/ifname]");
542 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
544 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
559 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
561 struct wpa_ctrl_dst *dst, *prev;
563 if (hapd->ctrl_sock > -1) {
565 eloop_unregister_read_sock(hapd->ctrl_sock);
566 close(hapd->ctrl_sock);
567 hapd->ctrl_sock = -1;
568 fname = hostapd_ctrl_iface_path(hapd);
573 if (hapd->conf->ctrl_interface &&
574 rmdir(hapd->conf->ctrl_interface) < 0) {
575 if (errno == ENOTEMPTY) {
576 wpa_printf(MSG_DEBUG, "Control interface "
577 "directory not empty - leaving it "
580 perror("rmdir[ctrl_interface]");
585 dst = hapd->ctrl_dst;
594 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
595 const char *buf, size_t len)
597 struct wpa_ctrl_dst *dst, *next;
603 dst = hapd->ctrl_dst;
604 if (hapd->ctrl_sock < 0 || dst == NULL)
607 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
608 io[0].iov_base = levelstr;
609 io[0].iov_len = os_strlen(levelstr);
610 io[1].iov_base = (char *) buf;
612 os_memset(&msg, 0, sizeof(msg));
619 if (level >= dst->debug_level) {
620 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
621 (u8 *) dst->addr.sun_path, dst->addrlen -
622 offsetof(struct sockaddr_un, sun_path));
623 msg.msg_name = &dst->addr;
624 msg.msg_namelen = dst->addrlen;
625 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
627 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
629 idx, errno, strerror(errno));
631 if (dst->errors > 10 || _errno == ENOENT) {
632 hostapd_ctrl_iface_detach(
644 #endif /* CONFIG_NATIVE_WINDOWS */