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 int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
46 struct sockaddr_un *from,
49 struct wpa_ctrl_dst *dst;
51 dst = os_zalloc(sizeof(*dst));
54 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
55 dst->addrlen = fromlen;
56 dst->debug_level = MSG_INFO;
57 dst->next = hapd->ctrl_dst;
59 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
60 (u8 *) from->sun_path, fromlen);
65 static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
66 struct sockaddr_un *from,
69 struct wpa_ctrl_dst *dst, *prev = NULL;
73 if (fromlen == dst->addrlen &&
74 os_memcmp(from->sun_path, dst->addr.sun_path, fromlen) ==
77 hapd->ctrl_dst = dst->next;
79 prev->next = dst->next;
81 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
82 (u8 *) from->sun_path, fromlen);
92 static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
93 struct sockaddr_un *from,
97 struct wpa_ctrl_dst *dst;
99 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
101 dst = hapd->ctrl_dst;
103 if (fromlen == dst->addrlen &&
104 os_memcmp(from->sun_path, dst->addr.sun_path, fromlen) ==
106 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
107 "level", (u8 *) from->sun_path, fromlen);
108 dst->debug_level = atoi(level);
118 static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
119 struct sta_info *sta,
120 char *buf, size_t buflen)
125 ret = os_snprintf(buf, buflen, "FAIL\n");
126 if (ret < 0 || (size_t) ret >= buflen)
132 ret = os_snprintf(buf + len, buflen - len, MACSTR "\n",
134 if (ret < 0 || (size_t) ret >= buflen - len)
138 res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
141 res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
144 res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
152 static int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
153 char *buf, size_t buflen)
155 return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
159 static int hostapd_ctrl_iface_sta(struct hostapd_data *hapd,
161 char *buf, size_t buflen)
166 if (hwaddr_aton(txtaddr, addr)) {
167 ret = os_snprintf(buf, buflen, "FAIL\n");
168 if (ret < 0 || (size_t) ret >= buflen)
172 return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
177 static int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd,
179 char *buf, size_t buflen)
182 struct sta_info *sta;
185 if (hwaddr_aton(txtaddr, addr) ||
186 (sta = ap_get_sta(hapd, addr)) == NULL) {
187 ret = os_snprintf(buf, buflen, "FAIL\n");
188 if (ret < 0 || (size_t) ret >= buflen)
192 return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
196 static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
200 struct sta_info *sta;
202 wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
204 if (hwaddr_aton(txtaddr, addr))
207 sta = ap_get_sta(hapd, addr);
211 wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
212 "notification", MAC2STR(addr));
213 sta = ap_sta_add(hapd, addr);
217 hostapd_new_assoc_sta(hapd, sta, 0);
222 #ifdef CONFIG_IEEE80211W
223 static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
227 u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
229 wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
231 if (hwaddr_aton(txtaddr, addr))
234 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
235 ieee802_11_send_sa_query_req(hapd, addr, trans_id);
239 #endif /* CONFIG_IEEE80211W */
243 static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
245 char *pin = os_strchr(txt, ' ');
249 return hostapd_wps_add_pin(hapd, txt, pin);
251 #endif /* CONFIG_WPS */
254 static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
257 struct hostapd_data *hapd = eloop_ctx;
260 struct sockaddr_un from;
261 socklen_t fromlen = sizeof(from);
263 const int reply_size = 4096;
266 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
267 (struct sockaddr *) &from, &fromlen);
269 perror("recvfrom(ctrl_iface)");
273 wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
275 reply = os_malloc(reply_size);
277 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
282 os_memcpy(reply, "OK\n", 3);
285 if (os_strcmp(buf, "PING") == 0) {
286 os_memcpy(reply, "PONG\n", 5);
288 } else if (os_strcmp(buf, "MIB") == 0) {
289 reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
290 if (reply_len >= 0) {
291 res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
292 reply_size - reply_len);
298 if (reply_len >= 0) {
299 res = ieee802_1x_get_mib(hapd, reply + reply_len,
300 reply_size - reply_len);
306 if (reply_len >= 0) {
307 res = radius_client_get_mib(hapd->radius,
309 reply_size - reply_len);
315 } else if (os_strcmp(buf, "STA-FIRST") == 0) {
316 reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
318 } else if (os_strncmp(buf, "STA ", 4) == 0) {
319 reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
321 } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
322 reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
324 } else if (os_strcmp(buf, "ATTACH") == 0) {
325 if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
327 } else if (os_strcmp(buf, "DETACH") == 0) {
328 if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
330 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
331 if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
334 } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
335 if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
337 #ifdef CONFIG_IEEE80211W
338 } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
339 if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
341 #endif /* CONFIG_IEEE80211W */
343 } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
344 if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
346 } else if (os_strcmp(buf, "WPS_PBC") == 0) {
347 if (hostapd_wps_button_pushed(hapd))
349 #endif /* CONFIG_WPS */
351 os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
356 os_memcpy(reply, "FAIL\n", 5);
359 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
364 static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
369 if (hapd->conf->ctrl_interface == NULL)
372 len = os_strlen(hapd->conf->ctrl_interface) +
373 os_strlen(hapd->conf->iface) + 2;
374 buf = os_malloc(len);
378 os_snprintf(buf, len, "%s/%s",
379 hapd->conf->ctrl_interface, hapd->conf->iface);
385 int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
387 struct sockaddr_un addr;
391 hapd->ctrl_sock = -1;
393 if (hapd->conf->ctrl_interface == NULL)
396 if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
397 if (errno == EEXIST) {
398 wpa_printf(MSG_DEBUG, "Using existing control "
399 "interface directory.");
401 perror("mkdir[ctrl_interface]");
406 if (hapd->conf->ctrl_interface_gid_set &&
407 chown(hapd->conf->ctrl_interface, 0,
408 hapd->conf->ctrl_interface_gid) < 0) {
409 perror("chown[ctrl_interface]");
413 if (os_strlen(hapd->conf->ctrl_interface) + 1 +
414 os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
417 s = socket(PF_UNIX, SOCK_DGRAM, 0);
419 perror("socket(PF_UNIX)");
423 os_memset(&addr, 0, sizeof(addr));
424 addr.sun_family = AF_UNIX;
425 fname = hostapd_ctrl_iface_path(hapd);
428 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
429 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
430 perror("bind(PF_UNIX)");
434 if (hapd->conf->ctrl_interface_gid_set &&
435 chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
436 perror("chown[ctrl_interface/ifname]");
440 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
441 perror("chmod[ctrl_interface/ifname]");
447 eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
463 void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
465 struct wpa_ctrl_dst *dst, *prev;
467 if (hapd->ctrl_sock > -1) {
469 eloop_unregister_read_sock(hapd->ctrl_sock);
470 close(hapd->ctrl_sock);
471 hapd->ctrl_sock = -1;
472 fname = hostapd_ctrl_iface_path(hapd);
477 if (hapd->conf->ctrl_interface &&
478 rmdir(hapd->conf->ctrl_interface) < 0) {
479 if (errno == ENOTEMPTY) {
480 wpa_printf(MSG_DEBUG, "Control interface "
481 "directory not empty - leaving it "
484 perror("rmdir[ctrl_interface]");
489 dst = hapd->ctrl_dst;
498 void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
499 char *buf, size_t len)
501 struct wpa_ctrl_dst *dst, *next;
507 dst = hapd->ctrl_dst;
508 if (hapd->ctrl_sock < 0 || dst == NULL)
511 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
512 io[0].iov_base = levelstr;
513 io[0].iov_len = os_strlen(levelstr);
514 io[1].iov_base = buf;
516 os_memset(&msg, 0, sizeof(msg));
523 if (level >= dst->debug_level) {
524 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
525 (u8 *) dst->addr.sun_path, dst->addrlen);
526 msg.msg_name = &dst->addr;
527 msg.msg_namelen = dst->addrlen;
528 if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
529 fprintf(stderr, "CTRL_IFACE monitor[%d]: ",
533 if (dst->errors > 10) {
534 hostapd_ctrl_iface_detach(
546 #endif /* CONFIG_NATIVE_WINDOWS */