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"
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
234 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
238 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
240 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
242 if (hwaddr_aton(txtaddr, addr))
245 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
246 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
250 #endif /* CONFIG_IEEE80211W */
254 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
256 char *pin = os_strchr(txt, ' ');
260 return hostapd_wps_add_pin(hapd, txt, pin);
264 #ifdef CONFIG_WPS_OOB
265 static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
267 char *path, *method, *name;
269 path = os_strchr(txt, ' ');
274 method = os_strchr(path, ' ');
279 name = os_strchr(method, ' ');
283 return hostapd_wps_start_oob(hapd, txt, path, method, name);
285 #endif /* CONFIG_WPS_OOB */
286 #endif /* CONFIG_WPS */
289 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
292 struct hostapd_data *hapd = eloop_ctx;
295 struct sockaddr_un from;
296 socklen_t fromlen = sizeof(from);
298 const int reply_size = 4096;
301 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
302 (struct sockaddr *) &from, &fromlen);
304 perror("recvfrom(ctrl_iface)");
308 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
310 reply = os_malloc(reply_size);
312 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
317 os_memcpy(reply, "OK\n", 3);
320 if (os_strcmp(buf, "PING") == 0) {
321 os_memcpy(reply, "PONG\n", 5);
323 } else if (os_strcmp(buf, "MIB") == 0) {
324 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
325 if (reply_len >= 0) {
326 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
327 reply_size - reply_len);
333 if (reply_len >= 0) {
334 res = ieee802_1x_get_mib(hapd, reply + reply_len,
335 reply_size - reply_len);
341 if (reply_len >= 0) {
342 res = radius_client_get_mib(hapd->radius,
344 reply_size - reply_len);
350 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
351 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
353 } else if (os_strncmp(buf, "STA ", 4) == 0) {
354 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
356 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
357 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
359 } else if (os_strcmp(buf, "ATTACH") == 0) {
360 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
362 } else if (os_strcmp(buf, "DETACH") == 0) {
363 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
365 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
366 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
369 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
370 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
372 #ifdef CONFIG_IEEE80211W
373 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
374 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
376 #endif /* CONFIG_IEEE80211W */
378 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
379 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
381 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
382 if (hostapd_wps_button_pushed(hapd))
384 #ifdef CONFIG_WPS_OOB
385 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
386 if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
388 #endif /* CONFIG_WPS_OOB */
389 #endif /* CONFIG_WPS */
391 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
396 os_memcpy(reply, "FAIL\n", 5);
399 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
404 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
409 if (hapd->conf->ctrl_interface == NULL)
412 len = os_strlen(hapd->conf->ctrl_interface) +
413 os_strlen(hapd->conf->iface) + 2;
414 buf = os_malloc(len);
418 os_snprintf(buf, len, "%s/%s",
419 hapd->conf->ctrl_interface, hapd->conf->iface);
425 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
426 const char *txt, size_t len)
428 struct hostapd_data *hapd = ctx;
431 hostapd_ctrl_iface_send(hapd, level, txt, len);
435 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
437 struct sockaddr_un addr;
441 hapd->ctrl_sock = -1;
443 if (hapd->conf->ctrl_interface == NULL)
446 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
447 if (errno == EEXIST) {
448 wpa_printf(MSG_DEBUG, "Using existing control "
449 "interface directory.");
451 perror("mkdir[ctrl_interface]");
456 if (hapd->conf->ctrl_interface_gid_set &&
457 chown(hapd->conf->ctrl_interface, 0,
458 hapd->conf->ctrl_interface_gid) < 0) {
459 perror("chown[ctrl_interface]");
463 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
464 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
467 s = socket(PF_UNIX, SOCK_DGRAM, 0);
469 perror("socket(PF_UNIX)");
473 os_memset(&addr, 0, sizeof(addr));
475 addr.sun_len = sizeof(addr);
476 #endif /* __FreeBSD__ */
477 addr.sun_family = AF_UNIX;
478 fname = hostapd_ctrl_iface_path(hapd);
481 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
482 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
483 perror("bind(PF_UNIX)");
487 if (hapd->conf->ctrl_interface_gid_set &&
488 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
489 perror("chown[ctrl_interface/ifname]");
493 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
494 perror("chmod[ctrl_interface/ifname]");
500 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
502 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
517 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
519 struct wpa_ctrl_dst *dst, *prev;
521 if (hapd->ctrl_sock > -1) {
523 eloop_unregister_read_sock(hapd->ctrl_sock);
524 close(hapd->ctrl_sock);
525 hapd->ctrl_sock = -1;
526 fname = hostapd_ctrl_iface_path(hapd);
531 if (hapd->conf->ctrl_interface &&
532 rmdir(hapd->conf->ctrl_interface) < 0) {
533 if (errno == ENOTEMPTY) {
534 wpa_printf(MSG_DEBUG, "Control interface "
535 "directory not empty - leaving it "
538 perror("rmdir[ctrl_interface]");
543 dst = hapd->ctrl_dst;
552 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
553 const char *buf, size_t len)
555 struct wpa_ctrl_dst *dst, *next;
561 dst = hapd->ctrl_dst;
562 if (hapd->ctrl_sock < 0 || dst == NULL)
565 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
566 io[0].iov_base = levelstr;
567 io[0].iov_len = os_strlen(levelstr);
568 io[1].iov_base = (char *) buf;
570 os_memset(&msg, 0, sizeof(msg));
577 if (level >= dst->debug_level) {
578 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
579 (u8 *) dst->addr.sun_path, dst->addrlen -
580 offsetof(struct sockaddr_un, sun_path));
581 msg.msg_name = &dst->addr;
582 msg.msg_namelen = dst->addrlen;
583 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
585 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
587 idx, errno, strerror(errno));
589 if (dst->errors > 10 || _errno == ENOENT) {
590 hostapd_ctrl_iface_detach(
602 #endif /* CONFIG_NATIVE_WINDOWS */