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"
37 struct wpa_ctrl_dst *next;
38 struct sockaddr_un addr;
45 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
46 const char *buf, size_t len);
49 static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
50 struct sockaddr_un *from,
53 struct wpa_ctrl_dst *dst;
55 dst = os_zalloc(sizeof(*dst));
58 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
59 dst->addrlen = fromlen;
60 dst->debug_level = MSG_INFO;
61 dst->next = hapd->ctrl_dst;
63 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
64 (u8 *) from->sun_path, fromlen);
69 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
70 struct sockaddr_un *from,
73 struct wpa_ctrl_dst *dst, *prev = NULL;
77 if (fromlen == dst->addrlen &&
78 os_memcmp(from->sun_path, dst->addr.sun_path, fromlen) ==
81 hapd->ctrl_dst = dst->next;
83 prev->next = dst->next;
85 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
86 (u8 *) from->sun_path, fromlen);
96 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
97 struct sockaddr_un *from,
101 struct wpa_ctrl_dst *dst;
103 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
105 dst = hapd->ctrl_dst;
107 if (fromlen == dst->addrlen &&
108 os_memcmp(from->sun_path, dst->addr.sun_path, fromlen) ==
110 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
111 "level", (u8 *) from->sun_path, fromlen);
112 dst->debug_level = atoi(level);
122 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
123 struct sta_info *sta,
124 char *buf, size_t buflen)
129 ret = os_snprintf(buf, buflen, "FAIL\n");
130 if (ret < 0 || (size_t) ret >= buflen)
136 ret = os_snprintf(buf + len, buflen - len, MACSTR "\n",
138 if (ret < 0 || (size_t) ret >= buflen - len)
142 res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
145 res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
148 res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
156 static int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
157 char *buf, size_t buflen)
159 return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
163 static int hostapd_ctrl_iface_sta(struct hostapd_data *hapd,
165 char *buf, size_t buflen)
170 if (hwaddr_aton(txtaddr, addr)) {
171 ret = os_snprintf(buf, buflen, "FAIL\n");
172 if (ret < 0 || (size_t) ret >= buflen)
176 return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
181 static int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd,
183 char *buf, size_t buflen)
186 struct sta_info *sta;
189 if (hwaddr_aton(txtaddr, addr) ||
190 (sta = ap_get_sta(hapd, addr)) == NULL) {
191 ret = os_snprintf(buf, buflen, "FAIL\n");
192 if (ret < 0 || (size_t) ret >= buflen)
196 return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
200 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
204 struct sta_info *sta;
206 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
208 if (hwaddr_aton(txtaddr, addr))
211 sta = ap_get_sta(hapd, addr);
215 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
216 "notification", MAC2STR(addr));
217 sta = ap_sta_add(hapd, addr);
221 hostapd_new_assoc_sta(hapd, sta, 0);
226 #ifdef CONFIG_IEEE80211W
227 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
231 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
233 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
235 if (hwaddr_aton(txtaddr, addr))
238 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
239 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
243 #endif /* CONFIG_IEEE80211W */
247 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
249 char *pin = os_strchr(txt, ' ');
253 return hostapd_wps_add_pin(hapd, txt, pin);
257 #ifdef CONFIG_WPS_OOB
258 static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
260 char *path, *method, *name;
262 path = os_strchr(txt, ' ');
267 method = os_strchr(path, ' ');
272 name = os_strchr(method, ' ');
276 return hostapd_wps_start_oob(hapd, txt, path, method, name);
278 #endif /* CONFIG_WPS_OOB */
279 #endif /* CONFIG_WPS */
282 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
285 struct hostapd_data *hapd = eloop_ctx;
288 struct sockaddr_un from;
289 socklen_t fromlen = sizeof(from);
291 const int reply_size = 4096;
294 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
295 (struct sockaddr *) &from, &fromlen);
297 perror("recvfrom(ctrl_iface)");
301 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
303 reply = os_malloc(reply_size);
305 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
310 os_memcpy(reply, "OK\n", 3);
313 if (os_strcmp(buf, "PING") == 0) {
314 os_memcpy(reply, "PONG\n", 5);
316 } else if (os_strcmp(buf, "MIB") == 0) {
317 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
318 if (reply_len >= 0) {
319 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
320 reply_size - reply_len);
326 if (reply_len >= 0) {
327 res = ieee802_1x_get_mib(hapd, reply + reply_len,
328 reply_size - reply_len);
334 if (reply_len >= 0) {
335 res = radius_client_get_mib(hapd->radius,
337 reply_size - reply_len);
343 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
344 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
346 } else if (os_strncmp(buf, "STA ", 4) == 0) {
347 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
349 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
350 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
352 } else if (os_strcmp(buf, "ATTACH") == 0) {
353 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
355 } else if (os_strcmp(buf, "DETACH") == 0) {
356 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
358 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
359 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
362 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
363 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
365 #ifdef CONFIG_IEEE80211W
366 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
367 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
369 #endif /* CONFIG_IEEE80211W */
371 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
372 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
374 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
375 if (hostapd_wps_button_pushed(hapd))
377 #ifdef CONFIG_WPS_OOB
378 } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
379 if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
381 #endif /* CONFIG_WPS_OOB */
382 #endif /* CONFIG_WPS */
384 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
389 os_memcpy(reply, "FAIL\n", 5);
392 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
397 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
402 if (hapd->conf->ctrl_interface == NULL)
405 len = os_strlen(hapd->conf->ctrl_interface) +
406 os_strlen(hapd->conf->iface) + 2;
407 buf = os_malloc(len);
411 os_snprintf(buf, len, "%s/%s",
412 hapd->conf->ctrl_interface, hapd->conf->iface);
418 static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
419 const char *txt, size_t len)
421 struct hostapd_data *hapd = ctx;
424 hostapd_ctrl_iface_send(hapd, level, txt, len);
428 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
430 struct sockaddr_un addr;
434 hapd->ctrl_sock = -1;
436 if (hapd->conf->ctrl_interface == NULL)
439 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
440 if (errno == EEXIST) {
441 wpa_printf(MSG_DEBUG, "Using existing control "
442 "interface directory.");
444 perror("mkdir[ctrl_interface]");
449 if (hapd->conf->ctrl_interface_gid_set &&
450 chown(hapd->conf->ctrl_interface, 0,
451 hapd->conf->ctrl_interface_gid) < 0) {
452 perror("chown[ctrl_interface]");
456 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
457 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
460 s = socket(PF_UNIX, SOCK_DGRAM, 0);
462 perror("socket(PF_UNIX)");
466 os_memset(&addr, 0, sizeof(addr));
467 addr.sun_family = AF_UNIX;
468 fname = hostapd_ctrl_iface_path(hapd);
471 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
472 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
473 perror("bind(PF_UNIX)");
477 if (hapd->conf->ctrl_interface_gid_set &&
478 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
479 perror("chown[ctrl_interface/ifname]");
483 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
484 perror("chmod[ctrl_interface/ifname]");
490 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
492 wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
507 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
509 struct wpa_ctrl_dst *dst, *prev;
511 if (hapd->ctrl_sock > -1) {
513 eloop_unregister_read_sock(hapd->ctrl_sock);
514 close(hapd->ctrl_sock);
515 hapd->ctrl_sock = -1;
516 fname = hostapd_ctrl_iface_path(hapd);
521 if (hapd->conf->ctrl_interface &&
522 rmdir(hapd->conf->ctrl_interface) < 0) {
523 if (errno == ENOTEMPTY) {
524 wpa_printf(MSG_DEBUG, "Control interface "
525 "directory not empty - leaving it "
528 perror("rmdir[ctrl_interface]");
533 dst = hapd->ctrl_dst;
542 static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
543 const char *buf, size_t len)
545 struct wpa_ctrl_dst *dst, *next;
551 dst = hapd->ctrl_dst;
552 if (hapd->ctrl_sock < 0 || dst == NULL)
555 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
556 io[0].iov_base = levelstr;
557 io[0].iov_len = os_strlen(levelstr);
558 io[1].iov_base = (char *) buf;
560 os_memset(&msg, 0, sizeof(msg));
567 if (level >= dst->debug_level) {
568 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
569 (u8 *) dst->addr.sun_path, dst->addrlen);
570 msg.msg_name = &dst->addr;
571 msg.msg_namelen = dst->addrlen;
572 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
574 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
576 idx, errno, strerror(errno));
578 if (dst->errors > 10 || _errno == ENOENT) {
579 hostapd_ctrl_iface_detach(
591 #endif /* CONFIG_NATIVE_WINDOWS */