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);
221 #ifdef CONFIG_IEEE80211W
222 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
226 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
228 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
230 if (hwaddr_aton(txtaddr, addr))
233 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
234 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
238 #endif /* CONFIG_IEEE80211W */
242 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
244 char *pin = os_strchr(txt, ' ');
248 return hostapd_wps_add_pin(hapd, txt, pin);
250 #endif /* CONFIG_WPS */
253 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
256 struct hostapd_data *hapd = eloop_ctx;
259 struct sockaddr_un from;
260 socklen_t fromlen = sizeof(from);
262 const int reply_size = 4096;
265 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
266 (struct sockaddr *) &from, &fromlen);
268 perror("recvfrom(ctrl_iface)");
272 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
274 reply = os_malloc(reply_size);
276 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
281 os_memcpy(reply, "OK\n", 3);
284 if (os_strcmp(buf, "PING") == 0) {
285 os_memcpy(reply, "PONG\n", 5);
287 } else if (os_strcmp(buf, "MIB") == 0) {
288 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
289 if (reply_len >= 0) {
290 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
291 reply_size - reply_len);
297 if (reply_len >= 0) {
298 res = ieee802_1x_get_mib(hapd, reply + reply_len,
299 reply_size - reply_len);
305 if (reply_len >= 0) {
306 res = radius_client_get_mib(hapd->radius,
308 reply_size - reply_len);
314 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
315 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
317 } else if (os_strncmp(buf, "STA ", 4) == 0) {
318 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
320 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
321 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
323 } else if (os_strcmp(buf, "ATTACH") == 0) {
324 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
326 } else if (os_strcmp(buf, "DETACH") == 0) {
327 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
329 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
330 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
333 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
334 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
336 #ifdef CONFIG_IEEE80211W
337 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
338 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
340 #endif /* CONFIG_IEEE80211W */
342 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
343 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
345 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
346 if (hostapd_wps_button_pushed(hapd))
348 #endif /* CONFIG_WPS */
350 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
355 os_memcpy(reply, "FAIL\n", 5);
358 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
363 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
368 if (hapd->conf->ctrl_interface == NULL)
371 len = os_strlen(hapd->conf->ctrl_interface) +
372 os_strlen(hapd->conf->iface) + 2;
373 buf = os_malloc(len);
377 os_snprintf(buf, len, "%s/%s",
378 hapd->conf->ctrl_interface, hapd->conf->iface);
384 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
386 struct sockaddr_un addr;
390 hapd->ctrl_sock = -1;
392 if (hapd->conf->ctrl_interface == NULL)
395 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
396 if (errno == EEXIST) {
397 wpa_printf(MSG_DEBUG, "Using existing control "
398 "interface directory.");
400 perror("mkdir[ctrl_interface]");
405 if (hapd->conf->ctrl_interface_gid_set &&
406 chown(hapd->conf->ctrl_interface, 0,
407 hapd->conf->ctrl_interface_gid) < 0) {
408 perror("chown[ctrl_interface]");
412 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
413 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
416 s = socket(PF_UNIX, SOCK_DGRAM, 0);
418 perror("socket(PF_UNIX)");
422 os_memset(&addr, 0, sizeof(addr));
423 addr.sun_family = AF_UNIX;
424 fname = hostapd_ctrl_iface_path(hapd);
427 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
428 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
429 perror("bind(PF_UNIX)");
433 if (hapd->conf->ctrl_interface_gid_set &&
434 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
435 perror("chown[ctrl_interface/ifname]");
439 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
440 perror("chmod[ctrl_interface/ifname]");
446 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
462 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
464 struct wpa_ctrl_dst *dst, *prev;
466 if (hapd->ctrl_sock > -1) {
468 eloop_unregister_read_sock(hapd->ctrl_sock);
469 close(hapd->ctrl_sock);
470 hapd->ctrl_sock = -1;
471 fname = hostapd_ctrl_iface_path(hapd);
476 if (hapd->conf->ctrl_interface &&
477 rmdir(hapd->conf->ctrl_interface) < 0) {
478 if (errno == ENOTEMPTY) {
479 wpa_printf(MSG_DEBUG, "Control interface "
480 "directory not empty - leaving it "
483 perror("rmdir[ctrl_interface]");
488 dst = hapd->ctrl_dst;
497 void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
498 char *buf, size_t len)
500 struct wpa_ctrl_dst *dst, *next;
506 dst = hapd->ctrl_dst;
507 if (hapd->ctrl_sock < 0 || dst == NULL)
510 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
511 io[0].iov_base = levelstr;
512 io[0].iov_len = os_strlen(levelstr);
513 io[1].iov_base = buf;
515 os_memset(&msg, 0, sizeof(msg));
522 if (level >= dst->debug_level) {
523 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
524 (u8 *) dst->addr.sun_path, dst->addrlen);
525 msg.msg_name = &dst->addr;
526 msg.msg_namelen = dst->addrlen;
527 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
528 fprintf(stderr, "CTRL_IFACE monitor[%d]: ",
532 if (dst->errors > 10) {
533 hostapd_ctrl_iface_detach(
545 #endif /* CONFIG_NATIVE_WINDOWS */