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]");
333 grp = getgrnam(gid_str);
337 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
338 " (from group name '%s')",
341 /* Group name not found - try to parse this as gid */
342 gid = strtol(gid_str, &endp, 10);
343 if (*gid_str == '\0' || *endp != '\0') {
344 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
349 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
354 if (gid_set && chown(dir, -1, gid) < 0) {
355 perror("chown[ctrl_interface]");
359 /* Make sure the group can enter and read the directory */
361 chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
362 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
367 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
368 sizeof(addr.sun_path)) {
369 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
373 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
374 if (priv->sock < 0) {
375 perror("socket(PF_UNIX)");
379 os_memset(&addr, 0, sizeof(addr));
380 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
381 addr.sun_len = sizeof(addr);
382 #endif /* __FreeBSD__ */
383 addr.sun_family = AF_UNIX;
384 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
387 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
388 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
389 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
391 if (connect(priv->sock, (struct sockaddr *) &addr,
393 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
394 " allow connections - assuming it was left"
395 "over from forced program termination");
396 if (unlink(fname) < 0) {
397 perror("unlink[ctrl_iface]");
398 wpa_printf(MSG_ERROR, "Could not unlink "
399 "existing ctrl_iface socket '%s'",
403 if (bind(priv->sock, (struct sockaddr *) &addr,
405 perror("supp-ctrl-iface-init: bind(PF_UNIX)");
408 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
409 "ctrl_iface socket '%s'", fname);
411 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
412 "be in use - cannot override it");
413 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
414 "not used anymore", fname);
421 if (gid_set && chown(fname, -1, gid) < 0) {
422 perror("chown[ctrl_interface/ifname]");
426 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
427 perror("chmod[ctrl_interface/ifname]");
437 * Make socket non-blocking so that we don't hang forever if
438 * target dies unexpectedly.
440 flags = fcntl(priv->sock, F_GETFL);
443 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
444 perror("fcntl(ctrl, O_NONBLOCK)");
445 /* Not fatal, continue on.*/
449 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
451 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
469 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
471 struct wpa_ctrl_dst *dst, *prev;
473 if (priv->sock > -1) {
475 char *buf, *dir = NULL, *gid_str = NULL;
476 eloop_unregister_read_sock(priv->sock);
477 if (!dl_list_empty(&priv->ctrl_dst)) {
479 * Wait a second before closing the control socket if
480 * there are any attached monitors in order to allow
481 * them to receive any pending messages.
483 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
484 "monitors to receive messages");
489 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
495 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
498 if (os_strncmp(buf, "DIR=", 4) == 0) {
500 gid_str = os_strstr(dir, " GROUP=");
508 if (rmdir(dir) < 0) {
509 if (errno == ENOTEMPTY) {
510 wpa_printf(MSG_DEBUG, "Control interface "
511 "directory not empty - leaving it "
514 perror("rmdir[ctrl_interface]");
521 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
529 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
530 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
531 * @level: Priority level of the message
533 * @len: Message length
535 * Send a packet to all monitor programs attached to the control interface.
537 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
538 int level, const char *buf,
541 struct wpa_ctrl_dst *dst, *next;
547 if (priv->sock < 0 || dl_list_empty(&priv->ctrl_dst))
550 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
551 if (res < 0 || (size_t) res >= sizeof(levelstr))
553 io[0].iov_base = levelstr;
554 io[0].iov_len = os_strlen(levelstr);
555 io[1].iov_base = (char *) buf;
557 os_memset(&msg, 0, sizeof(msg));
562 dl_list_for_each_safe(dst, next, &priv->ctrl_dst, struct wpa_ctrl_dst,
564 if (level >= dst->debug_level) {
565 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
566 (u8 *) dst->addr.sun_path, dst->addrlen -
567 offsetof(struct sockaddr_un, sun_path));
568 msg.msg_name = (void *) &dst->addr;
569 msg.msg_namelen = dst->addrlen;
570 if (sendmsg(priv->sock, &msg, 0) < 0) {
572 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
574 idx, errno, strerror(errno));
576 if (dst->errors > 1000 ||
577 (_errno != ENOBUFS && dst->errors > 10) ||
579 wpa_supplicant_ctrl_iface_detach(
591 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
595 struct sockaddr_un from;
596 socklen_t fromlen = sizeof(from);
599 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
600 "attach", priv->wpa_s->ifname);
601 eloop_wait_for_read_sock(priv->sock);
603 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
604 (struct sockaddr *) &from, &fromlen);
606 perror("recvfrom(ctrl_iface)");
611 if (os_strcmp(buf, "ATTACH") == 0) {
612 /* handle ATTACH signal of first monitor interface */
613 if (!wpa_supplicant_ctrl_iface_attach(priv, &from,
615 sendto(priv->sock, "OK\n", 3, 0,
616 (struct sockaddr *) &from, fromlen);
620 sendto(priv->sock, "FAIL\n", 5, 0,
621 (struct sockaddr *) &from, fromlen);
624 /* return FAIL for all other signals */
625 sendto(priv->sock, "FAIL\n", 5, 0,
626 (struct sockaddr *) &from, fromlen);
632 /* Global ctrl_iface */
634 struct ctrl_iface_global_priv {
635 struct wpa_global *global;
640 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
643 struct wpa_global *global = eloop_ctx;
646 struct sockaddr_un from;
647 socklen_t fromlen = sizeof(from);
651 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
652 (struct sockaddr *) &from, &fromlen);
654 perror("recvfrom(ctrl_iface)");
659 reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
663 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
666 } else if (reply_len) {
667 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
673 struct ctrl_iface_global_priv *
674 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
676 struct ctrl_iface_global_priv *priv;
677 struct sockaddr_un addr;
679 priv = os_zalloc(sizeof(*priv));
682 priv->global = global;
685 if (global->params.ctrl_interface == NULL)
689 priv->sock = android_get_control_socket(global->params.ctrl_interface);
694 wpa_printf(MSG_DEBUG, "Global control interface '%s'",
695 global->params.ctrl_interface);
697 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
698 if (priv->sock < 0) {
699 perror("socket(PF_UNIX)");
703 os_memset(&addr, 0, sizeof(addr));
704 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
705 addr.sun_len = sizeof(addr);
706 #endif /* __FreeBSD__ */
707 addr.sun_family = AF_UNIX;
708 os_strlcpy(addr.sun_path, global->params.ctrl_interface,
709 sizeof(addr.sun_path));
710 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
711 perror("supp-global-ctrl-iface-init (will try fixup): "
713 if (connect(priv->sock, (struct sockaddr *) &addr,
715 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
716 " allow connections - assuming it was left"
717 "over from forced program termination");
718 if (unlink(global->params.ctrl_interface) < 0) {
719 perror("unlink[ctrl_iface]");
720 wpa_printf(MSG_ERROR, "Could not unlink "
721 "existing ctrl_iface socket '%s'",
722 global->params.ctrl_interface);
725 if (bind(priv->sock, (struct sockaddr *) &addr,
727 perror("supp-glb-iface-init: bind(PF_UNIX)");
730 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
731 "ctrl_iface socket '%s'",
732 global->params.ctrl_interface);
734 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
735 "be in use - cannot override it");
736 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
738 global->params.ctrl_interface);
746 eloop_register_read_sock(priv->sock,
747 wpa_supplicant_global_ctrl_iface_receive,
761 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
763 if (priv->sock >= 0) {
764 eloop_unregister_read_sock(priv->sock);
767 if (priv->global->params.ctrl_interface)
768 unlink(priv->global->params.ctrl_interface);