2 * WPA Supplicant / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
17 #include <cutils/sockets.h>
20 #include "utils/common.h"
21 #include "utils/eloop.h"
22 #include "utils/list.h"
23 #include "eapol_supp/eapol_supp_sm.h"
25 #include "wpa_supplicant_i.h"
26 #include "ctrl_iface.h"
28 /* Per-interface ctrl_iface */
31 * struct wpa_ctrl_dst - Internal data structure of control interface monitors
33 * This structure is used to store information about registered control
34 * interface monitors into struct wpa_supplicant. This data is private to
35 * ctrl_iface_unix.c and should not be touched directly from other files.
39 struct sockaddr_un addr;
46 struct ctrl_iface_priv {
47 struct wpa_supplicant *wpa_s;
49 struct dl_list ctrl_dst;
53 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
54 int level, const char *buf,
58 static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv,
59 struct sockaddr_un *from,
62 struct wpa_ctrl_dst *dst;
64 dst = os_zalloc(sizeof(*dst));
67 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
68 dst->addrlen = fromlen;
69 dst->debug_level = MSG_INFO;
70 dl_list_add(&priv->ctrl_dst, &dst->list);
71 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
72 (u8 *) from->sun_path,
73 fromlen - offsetof(struct sockaddr_un, sun_path));
78 static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
79 struct sockaddr_un *from,
82 struct wpa_ctrl_dst *dst;
84 dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
85 if (fromlen == dst->addrlen &&
86 os_memcmp(from->sun_path, dst->addr.sun_path,
87 fromlen - offsetof(struct sockaddr_un, sun_path))
89 dl_list_del(&dst->list);
91 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
92 (u8 *) from->sun_path,
94 offsetof(struct sockaddr_un, sun_path));
102 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
103 struct sockaddr_un *from,
107 struct wpa_ctrl_dst *dst;
109 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
111 dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
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,
119 offsetof(struct sockaddr_un, sun_path));
120 dst->debug_level = atoi(level);
129 static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
132 struct wpa_supplicant *wpa_s = eloop_ctx;
133 struct ctrl_iface_priv *priv = sock_ctx;
136 struct sockaddr_un from;
137 socklen_t fromlen = sizeof(from);
139 size_t reply_len = 0;
140 int new_attached = 0;
142 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
143 (struct sockaddr *) &from, &fromlen);
145 perror("recvfrom(ctrl_iface)");
150 if (os_strcmp(buf, "ATTACH") == 0) {
151 if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen))
157 } else if (os_strcmp(buf, "DETACH") == 0) {
158 if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen))
162 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
163 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
169 reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
174 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
177 } else if (reply_len == 1) {
178 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
180 } else if (reply_len == 2) {
181 sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
186 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
190 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
194 char *pbuf, *dir = NULL, *gid_str = NULL;
197 if (wpa_s->conf->ctrl_interface == NULL)
200 pbuf = os_strdup(wpa_s->conf->ctrl_interface);
203 if (os_strncmp(pbuf, "DIR=", 4) == 0) {
205 gid_str = os_strstr(dir, " GROUP=");
213 len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
214 buf = os_malloc(len);
220 res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
221 if (res < 0 || (size_t) res >= len) {
228 /* Windows/WinPcap uses interface names that are not suitable
229 * as a file name - convert invalid chars to underscores */
237 #endif /* __CYGWIN__ */
243 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
244 const char *txt, size_t len)
246 struct wpa_supplicant *wpa_s = ctx;
247 if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
249 wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
253 struct ctrl_iface_priv *
254 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
256 struct ctrl_iface_priv *priv;
257 struct sockaddr_un addr;
261 char *buf, *dir = NULL, *gid_str = NULL;
266 priv = os_zalloc(sizeof(*priv));
269 dl_list_init(&priv->ctrl_dst);
273 if (wpa_s->conf->ctrl_interface == NULL)
276 buf = os_strdup(wpa_s->conf->ctrl_interface);
280 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
281 wpa_s->conf->ctrl_interface);
282 priv->sock = android_get_control_socket(addr.sun_path);
286 if (os_strncmp(buf, "DIR=", 4) == 0) {
288 gid_str = os_strstr(dir, " GROUP=");
295 gid_str = wpa_s->conf->ctrl_interface_group;
298 if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
299 if (errno == EEXIST) {
300 wpa_printf(MSG_DEBUG, "Using existing control "
301 "interface directory.");
303 perror("mkdir[ctrl_interface]");
309 grp = getgrnam(gid_str);
313 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
314 " (from group name '%s')",
317 /* Group name not found - try to parse this as gid */
318 gid = strtol(gid_str, &endp, 10);
319 if (*gid_str == '\0' || *endp != '\0') {
320 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
325 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
330 if (gid_set && chown(dir, -1, gid) < 0) {
331 perror("chown[ctrl_interface]");
335 /* Make sure the group can enter and read the directory */
337 chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
338 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
343 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
344 sizeof(addr.sun_path)) {
345 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
349 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
350 if (priv->sock < 0) {
351 perror("socket(PF_UNIX)");
355 os_memset(&addr, 0, sizeof(addr));
356 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
357 addr.sun_len = sizeof(addr);
358 #endif /* __FreeBSD__ */
359 addr.sun_family = AF_UNIX;
360 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
363 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
364 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
365 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
367 if (connect(priv->sock, (struct sockaddr *) &addr,
369 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
370 " allow connections - assuming it was left"
371 "over from forced program termination");
372 if (unlink(fname) < 0) {
373 perror("unlink[ctrl_iface]");
374 wpa_printf(MSG_ERROR, "Could not unlink "
375 "existing ctrl_iface socket '%s'",
379 if (bind(priv->sock, (struct sockaddr *) &addr,
381 perror("supp-ctrl-iface-init: bind(PF_UNIX)");
384 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
385 "ctrl_iface socket '%s'", fname);
387 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
388 "be in use - cannot override it");
389 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
390 "not used anymore", fname);
397 if (gid_set && chown(fname, -1, gid) < 0) {
398 perror("chown[ctrl_interface/ifname]");
402 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
403 perror("chmod[ctrl_interface/ifname]");
413 * Make socket non-blocking so that we don't hang forever if
414 * target dies unexpectedly.
416 flags = fcntl(priv->sock, F_GETFL);
419 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
420 perror("fcntl(ctrl, O_NONBLOCK)");
421 /* Not fatal, continue on.*/
425 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
427 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
445 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
447 struct wpa_ctrl_dst *dst, *prev;
449 if (priv->sock > -1) {
451 char *buf, *dir = NULL, *gid_str = NULL;
452 eloop_unregister_read_sock(priv->sock);
453 if (!dl_list_empty(&priv->ctrl_dst)) {
455 * Wait a second before closing the control socket if
456 * there are any attached monitors in order to allow
457 * them to receive any pending messages.
459 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
460 "monitors to receive messages");
465 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
471 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
474 if (os_strncmp(buf, "DIR=", 4) == 0) {
476 gid_str = os_strstr(dir, " GROUP=");
484 if (rmdir(dir) < 0) {
485 if (errno == ENOTEMPTY) {
486 wpa_printf(MSG_DEBUG, "Control interface "
487 "directory not empty - leaving it "
490 perror("rmdir[ctrl_interface]");
497 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
505 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
506 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
507 * @level: Priority level of the message
509 * @len: Message length
511 * Send a packet to all monitor programs attached to the control interface.
513 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
514 int level, const char *buf,
517 struct wpa_ctrl_dst *dst, *next;
523 if (priv->sock < 0 || dl_list_empty(&priv->ctrl_dst))
526 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
527 if (res < 0 || (size_t) res >= sizeof(levelstr))
529 io[0].iov_base = levelstr;
530 io[0].iov_len = os_strlen(levelstr);
531 io[1].iov_base = (char *) buf;
533 os_memset(&msg, 0, sizeof(msg));
538 dl_list_for_each_safe(dst, next, &priv->ctrl_dst, struct wpa_ctrl_dst,
540 if (level >= dst->debug_level) {
541 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
542 (u8 *) dst->addr.sun_path, dst->addrlen -
543 offsetof(struct sockaddr_un, sun_path));
544 msg.msg_name = (void *) &dst->addr;
545 msg.msg_namelen = dst->addrlen;
546 if (sendmsg(priv->sock, &msg, 0) < 0) {
548 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
550 idx, errno, strerror(errno));
552 if (dst->errors > 1000 ||
553 (_errno != ENOBUFS && dst->errors > 10) ||
555 wpa_supplicant_ctrl_iface_detach(
567 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
571 struct sockaddr_un from;
572 socklen_t fromlen = sizeof(from);
575 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
576 "attach", priv->wpa_s->ifname);
577 eloop_wait_for_read_sock(priv->sock);
579 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
580 (struct sockaddr *) &from, &fromlen);
582 perror("recvfrom(ctrl_iface)");
587 if (os_strcmp(buf, "ATTACH") == 0) {
588 /* handle ATTACH signal of first monitor interface */
589 if (!wpa_supplicant_ctrl_iface_attach(priv, &from,
591 sendto(priv->sock, "OK\n", 3, 0,
592 (struct sockaddr *) &from, fromlen);
596 sendto(priv->sock, "FAIL\n", 5, 0,
597 (struct sockaddr *) &from, fromlen);
600 /* return FAIL for all other signals */
601 sendto(priv->sock, "FAIL\n", 5, 0,
602 (struct sockaddr *) &from, fromlen);
608 /* Global ctrl_iface */
610 struct ctrl_iface_global_priv {
611 struct wpa_global *global;
616 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
619 struct wpa_global *global = eloop_ctx;
622 struct sockaddr_un from;
623 socklen_t fromlen = sizeof(from);
627 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
628 (struct sockaddr *) &from, &fromlen);
630 perror("recvfrom(ctrl_iface)");
635 reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
639 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
642 } else if (reply_len) {
643 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
649 struct ctrl_iface_global_priv *
650 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
652 struct ctrl_iface_global_priv *priv;
653 struct sockaddr_un addr;
655 priv = os_zalloc(sizeof(*priv));
658 priv->global = global;
661 if (global->params.ctrl_interface == NULL)
665 priv->sock = android_get_control_socket(global->params.ctrl_interface);
670 wpa_printf(MSG_DEBUG, "Global control interface '%s'",
671 global->params.ctrl_interface);
673 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
674 if (priv->sock < 0) {
675 perror("socket(PF_UNIX)");
679 os_memset(&addr, 0, sizeof(addr));
680 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
681 addr.sun_len = sizeof(addr);
682 #endif /* __FreeBSD__ */
683 addr.sun_family = AF_UNIX;
684 os_strlcpy(addr.sun_path, global->params.ctrl_interface,
685 sizeof(addr.sun_path));
686 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
687 perror("supp-global-ctrl-iface-init (will try fixup): "
689 if (connect(priv->sock, (struct sockaddr *) &addr,
691 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
692 " allow connections - assuming it was left"
693 "over from forced program termination");
694 if (unlink(global->params.ctrl_interface) < 0) {
695 perror("unlink[ctrl_iface]");
696 wpa_printf(MSG_ERROR, "Could not unlink "
697 "existing ctrl_iface socket '%s'",
698 global->params.ctrl_interface);
701 if (bind(priv->sock, (struct sockaddr *) &addr,
703 perror("supp-glb-iface-init: bind(PF_UNIX)");
706 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
707 "ctrl_iface socket '%s'",
708 global->params.ctrl_interface);
710 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
711 "be in use - cannot override it");
712 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
714 global->params.ctrl_interface);
722 eloop_register_read_sock(priv->sock,
723 wpa_supplicant_global_ctrl_iface_receive,
737 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
739 if (priv->sock >= 0) {
740 eloop_unregister_read_sock(priv->sock);
743 if (priv->global->params.ctrl_interface)
744 unlink(priv->global->params.ctrl_interface);