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
25 #include "drivers/driver.h"
26 #include "radius/radius_client.h"
27 #include "ap/hostapd.h"
28 #include "ap/config.h"
29 #include "ap/ieee802_1x.h"
31 #include "ap/ieee802_11.h"
32 #include "ap/sta_info.h"
33 #include "ap/accounting.h"
34 #include "ctrl_iface.h"
35 #include "wps_hostapd.h"
36 #include "ctrl_iface_ap.h"
40 struct wpa_ctrl_dst *next;
41 struct sockaddr_un addr;
48 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
49 const char *buf, size_t len);
52 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
53 struct sockaddr_un *from,
56 struct wpa_ctrl_dst *dst;
58 dst = os_zalloc(sizeof(*dst));
61 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
62 dst->addrlen = fromlen;
63 dst->debug_level = MSG_INFO;
64 dst->next = hapd->ctrl_dst;
66 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
67 (u8 *) from->sun_path,
68 fromlen - offsetof(struct sockaddr_un, sun_path));
73 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
74 struct sockaddr_un *from,
77 struct wpa_ctrl_dst *dst, *prev = NULL;
81 if (fromlen == dst->addrlen &&
82 os_memcmp(from->sun_path, dst->addr.sun_path,
83 fromlen - offsetof(struct sockaddr_un, sun_path))
86 hapd->ctrl_dst = dst->next;
88 prev->next = dst->next;
90 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
91 (u8 *) from->sun_path,
93 offsetof(struct sockaddr_un, sun_path));
103 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
104 struct sockaddr_un *from,
108 struct wpa_ctrl_dst *dst;
110 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
112 dst = hapd->ctrl_dst;
114 if (fromlen == dst->addrlen &&
115 os_memcmp(from->sun_path, dst->addr.sun_path,
116 fromlen - offsetof(struct sockaddr_un, sun_path))
118 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
119 "level", (u8 *) from->sun_path, fromlen -
120 offsetof(struct sockaddr_un, sun_path));
121 dst->debug_level = atoi(level);
131 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
135 struct sta_info *sta;
137 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
139 if (hwaddr_aton(txtaddr, addr))
142 sta = ap_get_sta(hapd, addr);
146 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
147 "notification", MAC2STR(addr));
148 sta = ap_sta_add(hapd, addr);
152 hostapd_new_assoc_sta(hapd, sta, 0);
157 #ifdef CONFIG_IEEE80211W
159 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
163 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
165 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
167 if (hwaddr_aton(txtaddr, addr))
170 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
171 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
175 #endif /* NEED_AP_MLME */
176 #endif /* CONFIG_IEEE80211W */
180 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
182 char *pin = os_strchr(txt, ' ');
190 timeout_txt = os_strchr(pin, ' ');
192 *timeout_txt++ = '\0';
193 timeout = atoi(timeout_txt);
197 return hostapd_wps_add_pin(hapd, txt, pin, timeout);
201 #ifdef CONFIG_WPS_OOB
202 static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
204 char *path, *method, *name;
206 path = os_strchr(txt, ' ');
211 method = os_strchr(path, ' ');
216 name = os_strchr(method, ' ');
220 return hostapd_wps_start_oob(hapd, txt, path, method, name);
222 #endif /* CONFIG_WPS_OOB */
223 #endif /* CONFIG_WPS */
226 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
229 struct hostapd_data *hapd = eloop_ctx;
232 struct sockaddr_un from;
233 socklen_t fromlen = sizeof(from);
235 const int reply_size = 4096;
238 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
239 (struct sockaddr *) &from, &fromlen);
241 perror("recvfrom(ctrl_iface)");
245 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
247 reply = os_malloc(reply_size);
249 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
254 os_memcpy(reply, "OK\n", 3);
257 if (os_strcmp(buf, "PING") == 0) {
258 os_memcpy(reply, "PONG\n", 5);
260 } else if (os_strcmp(buf, "MIB") == 0) {
261 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
262 if (reply_len >= 0) {
263 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
264 reply_size - reply_len);
270 if (reply_len >= 0) {
271 res = ieee802_1x_get_mib(hapd, reply + reply_len,
272 reply_size - reply_len);
278 #ifndef CONFIG_NO_RADIUS
279 if (reply_len >= 0) {
280 res = radius_client_get_mib(hapd->radius,
282 reply_size - reply_len);
288 #endif /* CONFIG_NO_RADIUS */
289 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
290 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
292 } else if (os_strncmp(buf, "STA ", 4) == 0) {
293 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
295 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
296 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
298 } else if (os_strcmp(buf, "ATTACH") == 0) {
299 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
301 } else if (os_strcmp(buf, "DETACH") == 0) {
302 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
304 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
305 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
308 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
309 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
311 #ifdef CONFIG_IEEE80211W
313 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
314 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
316 #endif /* NEED_AP_MLME */
317 #endif /* CONFIG_IEEE80211W */
319 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
320 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
322 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
323 if (hostapd_wps_button_pushed(hapd))
325 #ifdef CONFIG_WPS_OOB
326 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
327 if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
329 #endif /* CONFIG_WPS_OOB */
330 #endif /* CONFIG_WPS */
332 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
337 os_memcpy(reply, "FAIL\n", 5);
340 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
345 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
350 if (hapd->conf->ctrl_interface == NULL)
353 len = os_strlen(hapd->conf->ctrl_interface) +
354 os_strlen(hapd->conf->iface) + 2;
355 buf = os_malloc(len);
359 os_snprintf(buf, len, "%s/%s",
360 hapd->conf->ctrl_interface, hapd->conf->iface);
366 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
367 const char *txt, size_t len)
369 struct hostapd_data *hapd = ctx;
372 hostapd_ctrl_iface_send(hapd, level, txt, len);
376 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
378 struct sockaddr_un addr;
382 hapd->ctrl_sock = -1;
384 if (hapd->conf->ctrl_interface == NULL)
387 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
388 if (errno == EEXIST) {
389 wpa_printf(MSG_DEBUG, "Using existing control "
390 "interface directory.");
392 perror("mkdir[ctrl_interface]");
397 if (hapd->conf->ctrl_interface_gid_set &&
398 chown(hapd->conf->ctrl_interface, 0,
399 hapd->conf->ctrl_interface_gid) < 0) {
400 perror("chown[ctrl_interface]");
404 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
405 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
408 s = socket(PF_UNIX, SOCK_DGRAM, 0);
410 perror("socket(PF_UNIX)");
414 os_memset(&addr, 0, sizeof(addr));
416 addr.sun_len = sizeof(addr);
417 #endif /* __FreeBSD__ */
418 addr.sun_family = AF_UNIX;
419 fname = hostapd_ctrl_iface_path(hapd);
422 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
423 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
424 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
426 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
427 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
428 " allow connections - assuming it was left"
429 "over from forced program termination");
430 if (unlink(fname) < 0) {
431 perror("unlink[ctrl_iface]");
432 wpa_printf(MSG_ERROR, "Could not unlink "
433 "existing ctrl_iface socket '%s'",
437 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
439 perror("bind(PF_UNIX)");
442 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
443 "ctrl_iface socket '%s'", fname);
445 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
446 "be in use - cannot override it");
447 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
448 "not used anymore", fname);
455 if (hapd->conf->ctrl_interface_gid_set &&
456 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
457 perror("chown[ctrl_interface/ifname]");
461 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
462 perror("chmod[ctrl_interface/ifname]");
468 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
470 hapd->msg_ctx = hapd;
471 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
486 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
488 struct wpa_ctrl_dst *dst, *prev;
490 if (hapd->ctrl_sock > -1) {
492 eloop_unregister_read_sock(hapd->ctrl_sock);
493 close(hapd->ctrl_sock);
494 hapd->ctrl_sock = -1;
495 fname = hostapd_ctrl_iface_path(hapd);
500 if (hapd->conf->ctrl_interface &&
501 rmdir(hapd->conf->ctrl_interface) < 0) {
502 if (errno == ENOTEMPTY) {
503 wpa_printf(MSG_DEBUG, "Control interface "
504 "directory not empty - leaving it "
507 perror("rmdir[ctrl_interface]");
512 dst = hapd->ctrl_dst;
521 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
522 const char *buf, size_t len)
524 struct wpa_ctrl_dst *dst, *next;
530 dst = hapd->ctrl_dst;
531 if (hapd->ctrl_sock < 0 || dst == NULL)
534 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
535 io[0].iov_base = levelstr;
536 io[0].iov_len = os_strlen(levelstr);
537 io[1].iov_base = (char *) buf;
539 os_memset(&msg, 0, sizeof(msg));
546 if (level >= dst->debug_level) {
547 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
548 (u8 *) dst->addr.sun_path, dst->addrlen -
549 offsetof(struct sockaddr_un, sun_path));
550 msg.msg_name = &dst->addr;
551 msg.msg_namelen = dst->addrlen;
552 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
554 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
556 idx, errno, strerror(errno));
558 if (dst->errors > 10 || _errno == ENOENT) {
559 hostapd_ctrl_iface_detach(
571 #endif /* CONFIG_NATIVE_WINDOWS */