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 "ieee802_1x.h"
27 #include "radius/radius_client.h"
28 #include "ieee802_11.h"
29 #include "ctrl_iface.h"
31 #include "accounting.h"
32 #include "wps_hostapd.h"
36 struct wpa_ctrl_dst *next;
37 struct sockaddr_un addr;
44 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
45 struct sockaddr_un *from,
48 struct wpa_ctrl_dst *dst;
50 dst = os_zalloc(sizeof(*dst));
53 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
54 dst->addrlen = fromlen;
55 dst->debug_level = MSG_INFO;
56 dst->next = hapd->ctrl_dst;
58 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
59 (u8 *) from->sun_path, fromlen);
64 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
65 struct sockaddr_un *from,
68 struct wpa_ctrl_dst *dst, *prev = NULL;
72 if (fromlen == dst->addrlen &&
73 os_memcmp(from->sun_path, dst->addr.sun_path, fromlen) ==
76 hapd->ctrl_dst = dst->next;
78 prev->next = dst->next;
80 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
81 (u8 *) from->sun_path, fromlen);
91 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
92 struct sockaddr_un *from,
96 struct wpa_ctrl_dst *dst;
98 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
100 dst = hapd->ctrl_dst;
102 if (fromlen == dst->addrlen &&
103 os_memcmp(from->sun_path, dst->addr.sun_path, fromlen) ==
105 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
106 "level", (u8 *) from->sun_path, fromlen);
107 dst->debug_level = atoi(level);
117 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
118 struct sta_info *sta,
119 char *buf, size_t buflen)
124 ret = os_snprintf(buf, buflen, "FAIL\n");
125 if (ret < 0 || (size_t) ret >= buflen)
131 ret = os_snprintf(buf + len, buflen - len, MACSTR "\n",
133 if (ret < 0 || (size_t) ret >= buflen - len)
137 res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
140 res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
143 res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
151 static int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
152 char *buf, size_t buflen)
154 return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
158 static int hostapd_ctrl_iface_sta(struct hostapd_data *hapd,
160 char *buf, size_t buflen)
165 if (hwaddr_aton(txtaddr, addr)) {
166 ret = os_snprintf(buf, buflen, "FAIL\n");
167 if (ret < 0 || (size_t) ret >= buflen)
171 return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
176 static int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd,
178 char *buf, size_t buflen)
181 struct sta_info *sta;
184 if (hwaddr_aton(txtaddr, addr) ||
185 (sta = ap_get_sta(hapd, addr)) == NULL) {
186 ret = os_snprintf(buf, buflen, "FAIL\n");
187 if (ret < 0 || (size_t) ret >= buflen)
191 return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
195 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
199 struct sta_info *sta;
201 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
203 if (hwaddr_aton(txtaddr, addr))
206 sta = ap_get_sta(hapd, addr);
210 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
211 "notification", MAC2STR(addr));
212 sta = ap_sta_add(hapd, addr);
216 hostapd_new_assoc_sta(hapd, sta, 0);
222 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
224 char *pin = os_strchr(txt, ' ');
228 return hostapd_wps_add_pin(hapd, txt, pin);
230 #endif /* CONFIG_WPS */
233 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
236 struct hostapd_data *hapd = eloop_ctx;
239 struct sockaddr_un from;
240 socklen_t fromlen = sizeof(from);
242 const int reply_size = 4096;
245 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
246 (struct sockaddr *) &from, &fromlen);
248 perror("recvfrom(ctrl_iface)");
252 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
254 reply = os_malloc(reply_size);
256 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
261 os_memcpy(reply, "OK\n", 3);
264 if (os_strcmp(buf, "PING") == 0) {
265 os_memcpy(reply, "PONG\n", 5);
267 } else if (os_strcmp(buf, "MIB") == 0) {
268 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
269 if (reply_len >= 0) {
270 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
271 reply_size - reply_len);
277 if (reply_len >= 0) {
278 res = ieee802_1x_get_mib(hapd, reply + reply_len,
279 reply_size - reply_len);
285 if (reply_len >= 0) {
286 res = radius_client_get_mib(hapd->radius,
288 reply_size - reply_len);
294 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
295 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
297 } else if (os_strncmp(buf, "STA ", 4) == 0) {
298 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
300 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
301 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
303 } else if (os_strcmp(buf, "ATTACH") == 0) {
304 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
306 } else if (os_strcmp(buf, "DETACH") == 0) {
307 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
309 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
310 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
313 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
314 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
317 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
318 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
320 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
321 if (hostapd_wps_button_pushed(hapd))
323 #endif /* CONFIG_WPS */
325 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
330 os_memcpy(reply, "FAIL\n", 5);
333 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
338 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
343 if (hapd->conf->ctrl_interface == NULL)
346 len = os_strlen(hapd->conf->ctrl_interface) +
347 os_strlen(hapd->conf->iface) + 2;
348 buf = os_malloc(len);
352 os_snprintf(buf, len, "%s/%s",
353 hapd->conf->ctrl_interface, hapd->conf->iface);
359 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
361 struct sockaddr_un addr;
365 hapd->ctrl_sock = -1;
367 if (hapd->conf->ctrl_interface == NULL)
370 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
371 if (errno == EEXIST) {
372 wpa_printf(MSG_DEBUG, "Using existing control "
373 "interface directory.");
375 perror("mkdir[ctrl_interface]");
380 if (hapd->conf->ctrl_interface_gid_set &&
381 chown(hapd->conf->ctrl_interface, 0,
382 hapd->conf->ctrl_interface_gid) < 0) {
383 perror("chown[ctrl_interface]");
387 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
388 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
391 s = socket(PF_UNIX, SOCK_DGRAM, 0);
393 perror("socket(PF_UNIX)");
397 os_memset(&addr, 0, sizeof(addr));
398 addr.sun_family = AF_UNIX;
399 fname = hostapd_ctrl_iface_path(hapd);
402 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
403 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
404 perror("bind(PF_UNIX)");
408 if (hapd->conf->ctrl_interface_gid_set &&
409 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
410 perror("chown[ctrl_interface/ifname]");
414 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
415 perror("chmod[ctrl_interface/ifname]");
421 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
437 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
439 struct wpa_ctrl_dst *dst, *prev;
441 if (hapd->ctrl_sock > -1) {
443 eloop_unregister_read_sock(hapd->ctrl_sock);
444 close(hapd->ctrl_sock);
445 hapd->ctrl_sock = -1;
446 fname = hostapd_ctrl_iface_path(hapd);
451 if (hapd->conf->ctrl_interface &&
452 rmdir(hapd->conf->ctrl_interface) < 0) {
453 if (errno == ENOTEMPTY) {
454 wpa_printf(MSG_DEBUG, "Control interface "
455 "directory not empty - leaving it "
458 perror("rmdir[ctrl_interface]");
463 dst = hapd->ctrl_dst;
472 void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
473 char *buf, size_t len)
475 struct wpa_ctrl_dst *dst, *next;
481 dst = hapd->ctrl_dst;
482 if (hapd->ctrl_sock < 0 || dst == NULL)
485 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
486 io[0].iov_base = levelstr;
487 io[0].iov_len = os_strlen(levelstr);
488 io[1].iov_base = buf;
490 os_memset(&msg, 0, sizeof(msg));
497 if (level >= dst->debug_level) {
498 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
499 (u8 *) dst->addr.sun_path, dst->addrlen);
500 msg.msg_name = &dst->addr;
501 msg.msg_namelen = dst->addrlen;
502 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
503 fprintf(stderr, "CTRL_IFACE monitor[%d]: ",
507 if (dst->errors > 10) {
508 hostapd_ctrl_iface_detach(
520 #endif /* CONFIG_NATIVE_WINDOWS */