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]");
310 * wpa_supplicant is started from /init.*.rc on Android and that seems
311 * to be using umask 0077 which would leave the control interface
312 * directory without group access. This breaks things since Wi-Fi
313 * framework assumes that this directory can be accessed by other
314 * applications in the wifi group. Fix this by adding group access even
315 * if umask value would prevent this.
317 if (chmod(dir, S_IRWXU | S_IRWXG) < 0) {
318 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
320 /* Try to continue anyway */
325 grp = getgrnam(gid_str);
329 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
330 " (from group name '%s')",
333 /* Group name not found - try to parse this as gid */
334 gid = strtol(gid_str, &endp, 10);
335 if (*gid_str == '\0' || *endp != '\0') {
336 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
341 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
346 if (gid_set && chown(dir, -1, gid) < 0) {
347 perror("chown[ctrl_interface]");
351 /* Make sure the group can enter and read the directory */
353 chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
354 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
359 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
360 sizeof(addr.sun_path)) {
361 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
365 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
366 if (priv->sock < 0) {
367 perror("socket(PF_UNIX)");
371 os_memset(&addr, 0, sizeof(addr));
372 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
373 addr.sun_len = sizeof(addr);
374 #endif /* __FreeBSD__ */
375 addr.sun_family = AF_UNIX;
376 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
379 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
380 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
381 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
383 if (connect(priv->sock, (struct sockaddr *) &addr,
385 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
386 " allow connections - assuming it was left"
387 "over from forced program termination");
388 if (unlink(fname) < 0) {
389 perror("unlink[ctrl_iface]");
390 wpa_printf(MSG_ERROR, "Could not unlink "
391 "existing ctrl_iface socket '%s'",
395 if (bind(priv->sock, (struct sockaddr *) &addr,
397 perror("supp-ctrl-iface-init: bind(PF_UNIX)");
400 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
401 "ctrl_iface socket '%s'", fname);
403 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
404 "be in use - cannot override it");
405 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
406 "not used anymore", fname);
413 if (gid_set && chown(fname, -1, gid) < 0) {
414 perror("chown[ctrl_interface/ifname]");
418 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
419 perror("chmod[ctrl_interface/ifname]");
429 * Make socket non-blocking so that we don't hang forever if
430 * target dies unexpectedly.
432 flags = fcntl(priv->sock, F_GETFL);
435 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
436 perror("fcntl(ctrl, O_NONBLOCK)");
437 /* Not fatal, continue on.*/
441 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
443 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
461 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
463 struct wpa_ctrl_dst *dst, *prev;
465 if (priv->sock > -1) {
467 char *buf, *dir = NULL, *gid_str = NULL;
468 eloop_unregister_read_sock(priv->sock);
469 if (!dl_list_empty(&priv->ctrl_dst)) {
471 * Wait a second before closing the control socket if
472 * there are any attached monitors in order to allow
473 * them to receive any pending messages.
475 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
476 "monitors to receive messages");
481 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
487 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
490 if (os_strncmp(buf, "DIR=", 4) == 0) {
492 gid_str = os_strstr(dir, " GROUP=");
500 if (rmdir(dir) < 0) {
501 if (errno == ENOTEMPTY) {
502 wpa_printf(MSG_DEBUG, "Control interface "
503 "directory not empty - leaving it "
506 perror("rmdir[ctrl_interface]");
513 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
521 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
522 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
523 * @level: Priority level of the message
525 * @len: Message length
527 * Send a packet to all monitor programs attached to the control interface.
529 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
530 int level, const char *buf,
533 struct wpa_ctrl_dst *dst, *next;
539 if (priv->sock < 0 || dl_list_empty(&priv->ctrl_dst))
542 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
543 if (res < 0 || (size_t) res >= sizeof(levelstr))
545 io[0].iov_base = levelstr;
546 io[0].iov_len = os_strlen(levelstr);
547 io[1].iov_base = (char *) buf;
549 os_memset(&msg, 0, sizeof(msg));
554 dl_list_for_each_safe(dst, next, &priv->ctrl_dst, struct wpa_ctrl_dst,
556 if (level >= dst->debug_level) {
557 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
558 (u8 *) dst->addr.sun_path, dst->addrlen -
559 offsetof(struct sockaddr_un, sun_path));
560 msg.msg_name = (void *) &dst->addr;
561 msg.msg_namelen = dst->addrlen;
562 if (sendmsg(priv->sock, &msg, 0) < 0) {
564 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
566 idx, errno, strerror(errno));
568 if (dst->errors > 1000 ||
569 (_errno != ENOBUFS && dst->errors > 10) ||
571 wpa_supplicant_ctrl_iface_detach(
583 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
587 struct sockaddr_un from;
588 socklen_t fromlen = sizeof(from);
591 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
592 "attach", priv->wpa_s->ifname);
593 eloop_wait_for_read_sock(priv->sock);
595 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
596 (struct sockaddr *) &from, &fromlen);
598 perror("recvfrom(ctrl_iface)");
603 if (os_strcmp(buf, "ATTACH") == 0) {
604 /* handle ATTACH signal of first monitor interface */
605 if (!wpa_supplicant_ctrl_iface_attach(priv, &from,
607 sendto(priv->sock, "OK\n", 3, 0,
608 (struct sockaddr *) &from, fromlen);
612 sendto(priv->sock, "FAIL\n", 5, 0,
613 (struct sockaddr *) &from, fromlen);
616 /* return FAIL for all other signals */
617 sendto(priv->sock, "FAIL\n", 5, 0,
618 (struct sockaddr *) &from, fromlen);
624 /* Global ctrl_iface */
626 struct ctrl_iface_global_priv {
627 struct wpa_global *global;
632 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
635 struct wpa_global *global = eloop_ctx;
638 struct sockaddr_un from;
639 socklen_t fromlen = sizeof(from);
643 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
644 (struct sockaddr *) &from, &fromlen);
646 perror("recvfrom(ctrl_iface)");
651 reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
655 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
658 } else if (reply_len) {
659 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
665 struct ctrl_iface_global_priv *
666 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
668 struct ctrl_iface_global_priv *priv;
669 struct sockaddr_un addr;
671 priv = os_zalloc(sizeof(*priv));
674 priv->global = global;
677 if (global->params.ctrl_interface == NULL)
681 priv->sock = android_get_control_socket(global->params.ctrl_interface);
686 wpa_printf(MSG_DEBUG, "Global control interface '%s'",
687 global->params.ctrl_interface);
689 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
690 if (priv->sock < 0) {
691 perror("socket(PF_UNIX)");
695 os_memset(&addr, 0, sizeof(addr));
696 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
697 addr.sun_len = sizeof(addr);
698 #endif /* __FreeBSD__ */
699 addr.sun_family = AF_UNIX;
700 os_strlcpy(addr.sun_path, global->params.ctrl_interface,
701 sizeof(addr.sun_path));
702 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
703 perror("supp-global-ctrl-iface-init (will try fixup): "
705 if (connect(priv->sock, (struct sockaddr *) &addr,
707 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
708 " allow connections - assuming it was left"
709 "over from forced program termination");
710 if (unlink(global->params.ctrl_interface) < 0) {
711 perror("unlink[ctrl_iface]");
712 wpa_printf(MSG_ERROR, "Could not unlink "
713 "existing ctrl_iface socket '%s'",
714 global->params.ctrl_interface);
717 if (bind(priv->sock, (struct sockaddr *) &addr,
719 perror("supp-glb-iface-init: bind(PF_UNIX)");
722 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
723 "ctrl_iface socket '%s'",
724 global->params.ctrl_interface);
726 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
727 "be in use - cannot override it");
728 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
730 global->params.ctrl_interface);
738 eloop_register_read_sock(priv->sock,
739 wpa_supplicant_global_ctrl_iface_receive,
753 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
755 if (priv->sock >= 0) {
756 eloop_unregister_read_sock(priv->sock);
759 if (priv->global->params.ctrl_interface)
760 unlink(priv->global->params.ctrl_interface);