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 #if defined(CONFIG_P2P) && defined(ANDROID_P2P)
170 char *ifname, *ifend;
172 ifname = os_strstr(buf, "interface=");
173 if (ifname != NULL) {
174 ifend = os_strchr(ifname + 10, ' ');
178 *(ifname - 1) = '\0';
179 wpa_printf(MSG_DEBUG, "Found %s", ifname);
180 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
181 if (os_strcmp(wpa_s->ifname, ifname + 10) == 0)
185 wpa_printf(MSG_ERROR, "P2P: %s does not exist", ifname);
189 os_memmove(ifname, ifend, strlen(ifend) + 1);
190 wpa_printf(MSG_INFO, "wpa_s->ifname %s cmd %s", wpa_s ? wpa_s->ifname : "NULL", buf);
192 #endif /* defined CONFIG_P2P && defined ANDROID_P2P */
193 reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
198 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
201 } else if (reply_len == 1) {
202 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
204 } else if (reply_len == 2) {
205 sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
210 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
214 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
218 char *pbuf, *dir = NULL, *gid_str = NULL;
221 if (wpa_s->conf->ctrl_interface == NULL)
224 pbuf = os_strdup(wpa_s->conf->ctrl_interface);
227 if (os_strncmp(pbuf, "DIR=", 4) == 0) {
229 gid_str = os_strstr(dir, " GROUP=");
237 len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
238 buf = os_malloc(len);
244 res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
245 if (res < 0 || (size_t) res >= len) {
252 /* Windows/WinPcap uses interface names that are not suitable
253 * as a file name - convert invalid chars to underscores */
261 #endif /* __CYGWIN__ */
267 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
268 const char *txt, size_t len)
270 struct wpa_supplicant *wpa_s = ctx;
271 if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
273 wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
277 struct ctrl_iface_priv *
278 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
280 struct ctrl_iface_priv *priv;
281 struct sockaddr_un addr;
285 char *buf, *dir = NULL, *gid_str = NULL;
290 priv = os_zalloc(sizeof(*priv));
293 dl_list_init(&priv->ctrl_dst);
297 if (wpa_s->conf->ctrl_interface == NULL)
300 buf = os_strdup(wpa_s->conf->ctrl_interface);
304 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
305 wpa_s->conf->ctrl_interface);
306 priv->sock = android_get_control_socket(addr.sun_path);
310 if (os_strncmp(buf, "DIR=", 4) == 0) {
312 gid_str = os_strstr(dir, " GROUP=");
319 gid_str = wpa_s->conf->ctrl_interface_group;
322 if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
323 if (errno == EEXIST) {
324 wpa_printf(MSG_DEBUG, "Using existing control "
325 "interface directory.");
327 perror("mkdir[ctrl_interface]");
334 * wpa_supplicant is started from /init.*.rc on Android and that seems
335 * to be using umask 0077 which would leave the control interface
336 * directory without group access. This breaks things since Wi-Fi
337 * framework assumes that this directory can be accessed by other
338 * applications in the wifi group. Fix this by adding group access even
339 * if umask value would prevent this.
341 if (chmod(dir, S_IRWXU | S_IRWXG) < 0) {
342 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
344 /* Try to continue anyway */
349 grp = getgrnam(gid_str);
353 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
354 " (from group name '%s')",
357 /* Group name not found - try to parse this as gid */
358 gid = strtol(gid_str, &endp, 10);
359 if (*gid_str == '\0' || *endp != '\0') {
360 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
365 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
370 if (gid_set && chown(dir, -1, gid) < 0) {
371 perror("chown[ctrl_interface]");
375 /* Make sure the group can enter and read the directory */
377 chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
378 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
383 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
384 sizeof(addr.sun_path)) {
385 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
389 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
390 if (priv->sock < 0) {
391 perror("socket(PF_UNIX)");
395 os_memset(&addr, 0, sizeof(addr));
396 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
397 addr.sun_len = sizeof(addr);
398 #endif /* __FreeBSD__ */
399 addr.sun_family = AF_UNIX;
400 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
403 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
404 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
405 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
407 if (connect(priv->sock, (struct sockaddr *) &addr,
409 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
410 " allow connections - assuming it was left"
411 "over from forced program termination");
412 if (unlink(fname) < 0) {
413 perror("unlink[ctrl_iface]");
414 wpa_printf(MSG_ERROR, "Could not unlink "
415 "existing ctrl_iface socket '%s'",
419 if (bind(priv->sock, (struct sockaddr *) &addr,
421 perror("supp-ctrl-iface-init: bind(PF_UNIX)");
424 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
425 "ctrl_iface socket '%s'", fname);
427 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
428 "be in use - cannot override it");
429 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
430 "not used anymore", fname);
437 if (gid_set && chown(fname, -1, gid) < 0) {
438 perror("chown[ctrl_interface/ifname]");
442 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
443 perror("chmod[ctrl_interface/ifname]");
453 * Make socket non-blocking so that we don't hang forever if
454 * target dies unexpectedly.
456 flags = fcntl(priv->sock, F_GETFL);
459 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
460 perror("fcntl(ctrl, O_NONBLOCK)");
461 /* Not fatal, continue on.*/
465 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
467 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
485 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
487 struct wpa_ctrl_dst *dst, *prev;
489 if (priv->sock > -1) {
491 char *buf, *dir = NULL, *gid_str = NULL;
492 eloop_unregister_read_sock(priv->sock);
493 if (!dl_list_empty(&priv->ctrl_dst)) {
495 * Wait a second before closing the control socket if
496 * there are any attached monitors in order to allow
497 * them to receive any pending messages.
499 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
500 "monitors to receive messages");
505 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
511 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
514 if (os_strncmp(buf, "DIR=", 4) == 0) {
516 gid_str = os_strstr(dir, " GROUP=");
524 if (rmdir(dir) < 0) {
525 if (errno == ENOTEMPTY) {
526 wpa_printf(MSG_DEBUG, "Control interface "
527 "directory not empty - leaving it "
530 perror("rmdir[ctrl_interface]");
537 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
545 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
546 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
547 * @level: Priority level of the message
549 * @len: Message length
551 * Send a packet to all monitor programs attached to the control interface.
553 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
554 int level, const char *buf,
557 struct wpa_ctrl_dst *dst, *next;
563 if (priv->sock < 0 || dl_list_empty(&priv->ctrl_dst))
566 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
567 if (res < 0 || (size_t) res >= sizeof(levelstr))
569 io[0].iov_base = levelstr;
570 io[0].iov_len = os_strlen(levelstr);
571 io[1].iov_base = (char *) buf;
573 os_memset(&msg, 0, sizeof(msg));
578 dl_list_for_each_safe(dst, next, &priv->ctrl_dst, struct wpa_ctrl_dst,
580 if (level >= dst->debug_level) {
581 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
582 (u8 *) dst->addr.sun_path, dst->addrlen -
583 offsetof(struct sockaddr_un, sun_path));
584 msg.msg_name = (void *) &dst->addr;
585 msg.msg_namelen = dst->addrlen;
586 if (sendmsg(priv->sock, &msg, 0) < 0) {
588 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
590 idx, errno, strerror(errno));
592 if (dst->errors > 1000 ||
593 (_errno != ENOBUFS && dst->errors > 10) ||
595 wpa_supplicant_ctrl_iface_detach(
607 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
611 struct sockaddr_un from;
612 socklen_t fromlen = sizeof(from);
615 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
616 "attach", priv->wpa_s->ifname);
617 eloop_wait_for_read_sock(priv->sock);
619 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
620 (struct sockaddr *) &from, &fromlen);
622 perror("recvfrom(ctrl_iface)");
627 if (os_strcmp(buf, "ATTACH") == 0) {
628 /* handle ATTACH signal of first monitor interface */
629 if (!wpa_supplicant_ctrl_iface_attach(priv, &from,
631 sendto(priv->sock, "OK\n", 3, 0,
632 (struct sockaddr *) &from, fromlen);
636 sendto(priv->sock, "FAIL\n", 5, 0,
637 (struct sockaddr *) &from, fromlen);
640 /* return FAIL for all other signals */
641 sendto(priv->sock, "FAIL\n", 5, 0,
642 (struct sockaddr *) &from, fromlen);
648 /* Global ctrl_iface */
650 struct ctrl_iface_global_priv {
651 struct wpa_global *global;
656 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
659 struct wpa_global *global = eloop_ctx;
662 struct sockaddr_un from;
663 socklen_t fromlen = sizeof(from);
667 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
668 (struct sockaddr *) &from, &fromlen);
670 perror("recvfrom(ctrl_iface)");
675 reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
679 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
682 } else if (reply_len) {
683 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
689 struct ctrl_iface_global_priv *
690 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
692 struct ctrl_iface_global_priv *priv;
693 struct sockaddr_un addr;
695 priv = os_zalloc(sizeof(*priv));
698 priv->global = global;
701 if (global->params.ctrl_interface == NULL)
705 priv->sock = android_get_control_socket(global->params.ctrl_interface);
710 wpa_printf(MSG_DEBUG, "Global control interface '%s'",
711 global->params.ctrl_interface);
713 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
714 if (priv->sock < 0) {
715 perror("socket(PF_UNIX)");
719 os_memset(&addr, 0, sizeof(addr));
720 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
721 addr.sun_len = sizeof(addr);
722 #endif /* __FreeBSD__ */
723 addr.sun_family = AF_UNIX;
724 os_strlcpy(addr.sun_path, global->params.ctrl_interface,
725 sizeof(addr.sun_path));
726 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
727 perror("supp-global-ctrl-iface-init (will try fixup): "
729 if (connect(priv->sock, (struct sockaddr *) &addr,
731 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
732 " allow connections - assuming it was left"
733 "over from forced program termination");
734 if (unlink(global->params.ctrl_interface) < 0) {
735 perror("unlink[ctrl_iface]");
736 wpa_printf(MSG_ERROR, "Could not unlink "
737 "existing ctrl_iface socket '%s'",
738 global->params.ctrl_interface);
741 if (bind(priv->sock, (struct sockaddr *) &addr,
743 perror("supp-glb-iface-init: bind(PF_UNIX)");
746 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
747 "ctrl_iface socket '%s'",
748 global->params.ctrl_interface);
750 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
751 "be in use - cannot override it");
752 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
754 global->params.ctrl_interface);
762 eloop_register_read_sock(priv->sock,
763 wpa_supplicant_global_ctrl_iface_receive,
777 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
779 if (priv->sock >= 0) {
780 eloop_unregister_read_sock(priv->sock);
783 if (priv->global->params.ctrl_interface)
784 unlink(priv->global->params.ctrl_interface);