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 void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
45 const char *buf, size_t len);
48 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
49 struct sockaddr_un *from,
52 struct wpa_ctrl_dst *dst;
54 dst = os_zalloc(sizeof(*dst));
57 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
58 dst->addrlen = fromlen;
59 dst->debug_level = MSG_INFO;
60 dst->next = hapd->ctrl_dst;
62 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
63 (u8 *) from->sun_path, fromlen);
68 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
69 struct sockaddr_un *from,
72 struct wpa_ctrl_dst *dst, *prev = NULL;
76 if (fromlen == dst->addrlen &&
77 os_memcmp(from->sun_path, dst->addr.sun_path, fromlen) ==
80 hapd->ctrl_dst = dst->next;
82 prev->next = dst->next;
84 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
85 (u8 *) from->sun_path, fromlen);
95 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
96 struct sockaddr_un *from,
100 struct wpa_ctrl_dst *dst;
102 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
104 dst = hapd->ctrl_dst;
106 if (fromlen == dst->addrlen &&
107 os_memcmp(from->sun_path, dst->addr.sun_path, fromlen) ==
109 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
110 "level", (u8 *) from->sun_path, fromlen);
111 dst->debug_level = atoi(level);
121 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
122 struct sta_info *sta,
123 char *buf, size_t buflen)
128 ret = os_snprintf(buf, buflen, "FAIL\n");
129 if (ret < 0 || (size_t) ret >= buflen)
135 ret = os_snprintf(buf + len, buflen - len, MACSTR "\n",
137 if (ret < 0 || (size_t) ret >= buflen - len)
141 res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
144 res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
147 res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
155 static int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
156 char *buf, size_t buflen)
158 return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
162 static int hostapd_ctrl_iface_sta(struct hostapd_data *hapd,
164 char *buf, size_t buflen)
169 if (hwaddr_aton(txtaddr, addr)) {
170 ret = os_snprintf(buf, buflen, "FAIL\n");
171 if (ret < 0 || (size_t) ret >= buflen)
175 return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
180 static int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd,
182 char *buf, size_t buflen)
185 struct sta_info *sta;
188 if (hwaddr_aton(txtaddr, addr) ||
189 (sta = ap_get_sta(hapd, addr)) == NULL) {
190 ret = os_snprintf(buf, buflen, "FAIL\n");
191 if (ret < 0 || (size_t) ret >= buflen)
195 return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
199 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
203 struct sta_info *sta;
205 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
207 if (hwaddr_aton(txtaddr, addr))
210 sta = ap_get_sta(hapd, addr);
214 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
215 "notification", MAC2STR(addr));
216 sta = ap_sta_add(hapd, addr);
220 hostapd_new_assoc_sta(hapd, sta, 0);
225 #ifdef CONFIG_IEEE80211W
226 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
230 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
232 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
234 if (hwaddr_aton(txtaddr, addr))
237 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
238 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
242 #endif /* CONFIG_IEEE80211W */
246 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
248 char *pin = os_strchr(txt, ' ');
252 return hostapd_wps_add_pin(hapd, txt, pin);
254 #endif /* CONFIG_WPS */
257 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
260 struct hostapd_data *hapd = eloop_ctx;
263 struct sockaddr_un from;
264 socklen_t fromlen = sizeof(from);
266 const int reply_size = 4096;
269 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
270 (struct sockaddr *) &from, &fromlen);
272 perror("recvfrom(ctrl_iface)");
276 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
278 reply = os_malloc(reply_size);
280 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
285 os_memcpy(reply, "OK\n", 3);
288 if (os_strcmp(buf, "PING") == 0) {
289 os_memcpy(reply, "PONG\n", 5);
291 } else if (os_strcmp(buf, "MIB") == 0) {
292 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
293 if (reply_len >= 0) {
294 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
295 reply_size - reply_len);
301 if (reply_len >= 0) {
302 res = ieee802_1x_get_mib(hapd, reply + reply_len,
303 reply_size - reply_len);
309 if (reply_len >= 0) {
310 res = radius_client_get_mib(hapd->radius,
312 reply_size - reply_len);
318 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
319 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
321 } else if (os_strncmp(buf, "STA ", 4) == 0) {
322 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
324 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
325 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
327 } else if (os_strcmp(buf, "ATTACH") == 0) {
328 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
330 } else if (os_strcmp(buf, "DETACH") == 0) {
331 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
333 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
334 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
337 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
338 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
340 #ifdef CONFIG_IEEE80211W
341 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
342 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
344 #endif /* CONFIG_IEEE80211W */
346 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
347 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
349 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
350 if (hostapd_wps_button_pushed(hapd))
352 #endif /* CONFIG_WPS */
354 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
359 os_memcpy(reply, "FAIL\n", 5);
362 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
367 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
372 if (hapd->conf->ctrl_interface == NULL)
375 len = os_strlen(hapd->conf->ctrl_interface) +
376 os_strlen(hapd->conf->iface) + 2;
377 buf = os_malloc(len);
381 os_snprintf(buf, len, "%s/%s",
382 hapd->conf->ctrl_interface, hapd->conf->iface);
388 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
389 const char *txt, size_t len)
391 struct hostapd_data *hapd = ctx;
394 hostapd_ctrl_iface_send(hapd, level, txt, len);
398 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
400 struct sockaddr_un addr;
404 hapd->ctrl_sock = -1;
406 if (hapd->conf->ctrl_interface == NULL)
409 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
410 if (errno == EEXIST) {
411 wpa_printf(MSG_DEBUG, "Using existing control "
412 "interface directory.");
414 perror("mkdir[ctrl_interface]");
419 if (hapd->conf->ctrl_interface_gid_set &&
420 chown(hapd->conf->ctrl_interface, 0,
421 hapd->conf->ctrl_interface_gid) < 0) {
422 perror("chown[ctrl_interface]");
426 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
427 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
430 s = socket(PF_UNIX, SOCK_DGRAM, 0);
432 perror("socket(PF_UNIX)");
436 os_memset(&addr, 0, sizeof(addr));
437 addr.sun_family = AF_UNIX;
438 fname = hostapd_ctrl_iface_path(hapd);
441 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
442 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
443 perror("bind(PF_UNIX)");
447 if (hapd->conf->ctrl_interface_gid_set &&
448 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
449 perror("chown[ctrl_interface/ifname]");
453 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
454 perror("chmod[ctrl_interface/ifname]");
460 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
462 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
477 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
479 struct wpa_ctrl_dst *dst, *prev;
481 if (hapd->ctrl_sock > -1) {
483 eloop_unregister_read_sock(hapd->ctrl_sock);
484 close(hapd->ctrl_sock);
485 hapd->ctrl_sock = -1;
486 fname = hostapd_ctrl_iface_path(hapd);
491 if (hapd->conf->ctrl_interface &&
492 rmdir(hapd->conf->ctrl_interface) < 0) {
493 if (errno == ENOTEMPTY) {
494 wpa_printf(MSG_DEBUG, "Control interface "
495 "directory not empty - leaving it "
498 perror("rmdir[ctrl_interface]");
503 dst = hapd->ctrl_dst;
512 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
513 const char *buf, size_t len)
515 struct wpa_ctrl_dst *dst, *next;
521 dst = hapd->ctrl_dst;
522 if (hapd->ctrl_sock < 0 || dst == NULL)
525 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
526 io[0].iov_base = levelstr;
527 io[0].iov_len = os_strlen(levelstr);
528 io[1].iov_base = (char *) buf;
530 os_memset(&msg, 0, sizeof(msg));
537 if (level >= dst->debug_level) {
538 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
539 (u8 *) dst->addr.sun_path, dst->addrlen);
540 msg.msg_name = &dst->addr;
541 msg.msg_namelen = dst->addrlen;
542 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
543 fprintf(stderr, "CTRL_IFACE monitor[%d]: ",
547 if (dst->errors > 10) {
548 hostapd_ctrl_iface_detach(
560 #endif /* CONFIG_NATIVE_WINDOWS */