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"
35 #include "ctrl_iface_ap.h"
39 struct wpa_ctrl_dst *next;
40 struct sockaddr_un addr;
47 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
48 const char *buf, size_t len);
51 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
52 struct sockaddr_un *from,
55 struct wpa_ctrl_dst *dst;
57 dst = os_zalloc(sizeof(*dst));
60 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
61 dst->addrlen = fromlen;
62 dst->debug_level = MSG_INFO;
63 dst->next = hapd->ctrl_dst;
65 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
66 (u8 *) from->sun_path,
67 fromlen - offsetof(struct sockaddr_un, sun_path));
72 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
73 struct sockaddr_un *from,
76 struct wpa_ctrl_dst *dst, *prev = NULL;
80 if (fromlen == dst->addrlen &&
81 os_memcmp(from->sun_path, dst->addr.sun_path,
82 fromlen - offsetof(struct sockaddr_un, sun_path))
85 hapd->ctrl_dst = dst->next;
87 prev->next = dst->next;
89 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
90 (u8 *) from->sun_path,
92 offsetof(struct sockaddr_un, sun_path));
102 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
103 struct sockaddr_un *from,
107 struct wpa_ctrl_dst *dst;
109 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
111 dst = hapd->ctrl_dst;
113 if (fromlen == dst->addrlen &&
114 os_memcmp(from->sun_path, dst->addr.sun_path,
115 fromlen - offsetof(struct sockaddr_un, sun_path))
117 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
118 "level", (u8 *) from->sun_path, fromlen -
119 offsetof(struct sockaddr_un, sun_path));
120 dst->debug_level = atoi(level);
130 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
134 struct sta_info *sta;
136 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
138 if (hwaddr_aton(txtaddr, addr))
141 sta = ap_get_sta(hapd, addr);
145 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
146 "notification", MAC2STR(addr));
147 sta = ap_sta_add(hapd, addr);
151 hostapd_new_assoc_sta(hapd, sta, 0);
156 #ifdef CONFIG_IEEE80211W
158 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
162 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
164 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
166 if (hwaddr_aton(txtaddr, addr))
169 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
170 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
174 #endif /* NEED_AP_MLME */
175 #endif /* CONFIG_IEEE80211W */
179 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
181 char *pin = os_strchr(txt, ' ');
189 timeout_txt = os_strchr(pin, ' ');
191 *timeout_txt++ = '\0';
192 timeout = atoi(timeout_txt);
196 return hostapd_wps_add_pin(hapd, txt, pin, timeout);
200 #ifdef CONFIG_WPS_OOB
201 static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
203 char *path, *method, *name;
205 path = os_strchr(txt, ' ');
210 method = os_strchr(path, ' ');
215 name = os_strchr(method, ' ');
219 return hostapd_wps_start_oob(hapd, txt, path, method, name);
221 #endif /* CONFIG_WPS_OOB */
222 #endif /* CONFIG_WPS */
225 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
228 struct hostapd_data *hapd = eloop_ctx;
231 struct sockaddr_un from;
232 socklen_t fromlen = sizeof(from);
234 const int reply_size = 4096;
237 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
238 (struct sockaddr *) &from, &fromlen);
240 perror("recvfrom(ctrl_iface)");
244 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
246 reply = os_malloc(reply_size);
248 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
253 os_memcpy(reply, "OK\n", 3);
256 if (os_strcmp(buf, "PING") == 0) {
257 os_memcpy(reply, "PONG\n", 5);
259 } else if (os_strcmp(buf, "MIB") == 0) {
260 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
261 if (reply_len >= 0) {
262 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
263 reply_size - reply_len);
269 if (reply_len >= 0) {
270 res = ieee802_1x_get_mib(hapd, reply + reply_len,
271 reply_size - reply_len);
277 if (reply_len >= 0) {
278 res = radius_client_get_mib(hapd->radius,
280 reply_size - reply_len);
286 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
287 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
289 } else if (os_strncmp(buf, "STA ", 4) == 0) {
290 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
292 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
293 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
295 } else if (os_strcmp(buf, "ATTACH") == 0) {
296 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
298 } else if (os_strcmp(buf, "DETACH") == 0) {
299 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
301 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
302 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
305 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
306 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
308 #ifdef CONFIG_IEEE80211W
310 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
311 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
313 #endif /* NEED_AP_MLME */
314 #endif /* CONFIG_IEEE80211W */
316 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
317 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
319 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
320 if (hostapd_wps_button_pushed(hapd))
322 #ifdef CONFIG_WPS_OOB
323 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
324 if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
326 #endif /* CONFIG_WPS_OOB */
327 #endif /* CONFIG_WPS */
329 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
334 os_memcpy(reply, "FAIL\n", 5);
337 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
342 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
347 if (hapd->conf->ctrl_interface == NULL)
350 len = os_strlen(hapd->conf->ctrl_interface) +
351 os_strlen(hapd->conf->iface) + 2;
352 buf = os_malloc(len);
356 os_snprintf(buf, len, "%s/%s",
357 hapd->conf->ctrl_interface, hapd->conf->iface);
363 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
364 const char *txt, size_t len)
366 struct hostapd_data *hapd = ctx;
369 hostapd_ctrl_iface_send(hapd, level, txt, len);
373 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
375 struct sockaddr_un addr;
379 hapd->ctrl_sock = -1;
381 if (hapd->conf->ctrl_interface == NULL)
384 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
385 if (errno == EEXIST) {
386 wpa_printf(MSG_DEBUG, "Using existing control "
387 "interface directory.");
389 perror("mkdir[ctrl_interface]");
394 if (hapd->conf->ctrl_interface_gid_set &&
395 chown(hapd->conf->ctrl_interface, 0,
396 hapd->conf->ctrl_interface_gid) < 0) {
397 perror("chown[ctrl_interface]");
401 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
402 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
405 s = socket(PF_UNIX, SOCK_DGRAM, 0);
407 perror("socket(PF_UNIX)");
411 os_memset(&addr, 0, sizeof(addr));
413 addr.sun_len = sizeof(addr);
414 #endif /* __FreeBSD__ */
415 addr.sun_family = AF_UNIX;
416 fname = hostapd_ctrl_iface_path(hapd);
419 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
420 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
421 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
423 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
424 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
425 " allow connections - assuming it was left"
426 "over from forced program termination");
427 if (unlink(fname) < 0) {
428 perror("unlink[ctrl_iface]");
429 wpa_printf(MSG_ERROR, "Could not unlink "
430 "existing ctrl_iface socket '%s'",
434 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
436 perror("bind(PF_UNIX)");
439 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
440 "ctrl_iface socket '%s'", fname);
442 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
443 "be in use - cannot override it");
444 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
445 "not used anymore", fname);
452 if (hapd->conf->ctrl_interface_gid_set &&
453 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
454 perror("chown[ctrl_interface/ifname]");
458 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
459 perror("chmod[ctrl_interface/ifname]");
465 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
467 hapd->msg_ctx = hapd;
468 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
483 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
485 struct wpa_ctrl_dst *dst, *prev;
487 if (hapd->ctrl_sock > -1) {
489 eloop_unregister_read_sock(hapd->ctrl_sock);
490 close(hapd->ctrl_sock);
491 hapd->ctrl_sock = -1;
492 fname = hostapd_ctrl_iface_path(hapd);
497 if (hapd->conf->ctrl_interface &&
498 rmdir(hapd->conf->ctrl_interface) < 0) {
499 if (errno == ENOTEMPTY) {
500 wpa_printf(MSG_DEBUG, "Control interface "
501 "directory not empty - leaving it "
504 perror("rmdir[ctrl_interface]");
509 dst = hapd->ctrl_dst;
518 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
519 const char *buf, size_t len)
521 struct wpa_ctrl_dst *dst, *next;
527 dst = hapd->ctrl_dst;
528 if (hapd->ctrl_sock < 0 || dst == NULL)
531 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
532 io[0].iov_base = levelstr;
533 io[0].iov_len = os_strlen(levelstr);
534 io[1].iov_base = (char *) buf;
536 os_memset(&msg, 0, sizeof(msg));
543 if (level >= dst->debug_level) {
544 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
545 (u8 *) dst->addr.sun_path, dst->addrlen -
546 offsetof(struct sockaddr_un, sun_path));
547 msg.msg_name = &dst->addr;
548 msg.msg_namelen = dst->addrlen;
549 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
551 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
553 idx, errno, strerror(errno));
555 if (dst->errors > 10 || _errno == ENOENT) {
556 hostapd_ctrl_iface_detach(
568 #endif /* CONFIG_NATIVE_WINDOWS */