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.
15 #include <cutils/sockets.h>
18 #include "utils/common.h"
19 #include "utils/eloop.h"
20 #include "utils/list.h"
21 #include "eapol_supp/eapol_supp_sm.h"
23 #include "wpa_supplicant_i.h"
24 #include "ctrl_iface.h"
26 /* Per-interface ctrl_iface */
29 * struct wpa_ctrl_dst - Internal data structure of control interface monitors
31 * This structure is used to store information about registered control
32 * interface monitors into struct wpa_supplicant. This data is private to
33 * ctrl_iface_unix.c and should not be touched directly from other files.
37 struct sockaddr_un addr;
44 struct ctrl_iface_priv {
45 struct wpa_supplicant *wpa_s;
47 struct dl_list ctrl_dst;
51 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
52 int level, const char *buf,
56 static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv,
57 struct sockaddr_un *from,
60 struct wpa_ctrl_dst *dst;
62 dst = os_zalloc(sizeof(*dst));
65 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
66 dst->addrlen = fromlen;
67 dst->debug_level = MSG_INFO;
68 dl_list_add(&priv->ctrl_dst, &dst->list);
69 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
70 (u8 *) from->sun_path,
71 fromlen - offsetof(struct sockaddr_un, sun_path));
76 static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
77 struct sockaddr_un *from,
80 struct wpa_ctrl_dst *dst;
82 dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
83 if (fromlen == dst->addrlen &&
84 os_memcmp(from->sun_path, dst->addr.sun_path,
85 fromlen - offsetof(struct sockaddr_un, sun_path))
87 dl_list_del(&dst->list);
89 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
90 (u8 *) from->sun_path,
92 offsetof(struct sockaddr_un, sun_path));
100 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
101 struct sockaddr_un *from,
105 struct wpa_ctrl_dst *dst;
107 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
109 dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
110 if (fromlen == dst->addrlen &&
111 os_memcmp(from->sun_path, dst->addr.sun_path,
112 fromlen - offsetof(struct sockaddr_un, sun_path))
114 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
115 "level", (u8 *) from->sun_path,
117 offsetof(struct sockaddr_un, sun_path));
118 dst->debug_level = atoi(level);
127 static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
130 struct wpa_supplicant *wpa_s = eloop_ctx;
131 struct ctrl_iface_priv *priv = sock_ctx;
134 struct sockaddr_un from;
135 socklen_t fromlen = sizeof(from);
137 size_t reply_len = 0;
138 int new_attached = 0;
140 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
141 (struct sockaddr *) &from, &fromlen);
143 perror("recvfrom(ctrl_iface)");
148 if (os_strcmp(buf, "ATTACH") == 0) {
149 if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen))
155 } else if (os_strcmp(buf, "DETACH") == 0) {
156 if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen))
160 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
161 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
167 #if defined(CONFIG_P2P) && defined(ANDROID_P2P)
168 char *ifname, *ifend;
170 ifname = os_strstr(buf, "interface=");
171 if (ifname != NULL) {
172 ifend = os_strchr(ifname + 10, ' ');
177 wpa_printf(MSG_DEBUG, "Found %s", ifname);
178 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
179 if (os_strcmp(wpa_s->ifname, ifname + 10) == 0)
183 wpa_printf(MSG_ERROR, "P2P: %s does not exist", ifname);
186 os_memmove(ifname, ifend, strlen(ifend) + 1);
188 wpa_printf(MSG_INFO, "wpa_s %p cmd %s", wpa_s, buf);
189 #endif /* defined CONFIG_P2P && defined ANDROID_P2P */
190 reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
195 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
198 } else if (reply_len == 1) {
199 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
201 } else if (reply_len == 2) {
202 sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
207 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
211 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
215 char *pbuf, *dir = NULL, *gid_str = NULL;
218 if (wpa_s->conf->ctrl_interface == NULL)
221 pbuf = os_strdup(wpa_s->conf->ctrl_interface);
224 if (os_strncmp(pbuf, "DIR=", 4) == 0) {
226 gid_str = os_strstr(dir, " GROUP=");
234 len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
235 buf = os_malloc(len);
241 res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
242 if (res < 0 || (size_t) res >= len) {
249 /* Windows/WinPcap uses interface names that are not suitable
250 * as a file name - convert invalid chars to underscores */
258 #endif /* __CYGWIN__ */
264 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
265 const char *txt, size_t len)
267 struct wpa_supplicant *wpa_s = ctx;
268 if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
270 wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
274 struct ctrl_iface_priv *
275 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
277 struct ctrl_iface_priv *priv;
278 struct sockaddr_un addr;
282 char *buf, *dir = NULL, *gid_str = NULL;
286 priv = os_zalloc(sizeof(*priv));
289 dl_list_init(&priv->ctrl_dst);
293 if (wpa_s->conf->ctrl_interface == NULL)
296 buf = os_strdup(wpa_s->conf->ctrl_interface);
300 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
301 wpa_s->conf->ctrl_interface);
302 priv->sock = android_get_control_socket(addr.sun_path);
306 if (os_strncmp(buf, "DIR=", 4) == 0) {
308 gid_str = os_strstr(dir, " GROUP=");
315 gid_str = wpa_s->conf->ctrl_interface_group;
318 if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
319 if (errno == EEXIST) {
320 wpa_printf(MSG_DEBUG, "Using existing control "
321 "interface directory.");
323 perror("mkdir[ctrl_interface]");
329 grp = getgrnam(gid_str);
333 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
334 " (from group name '%s')",
337 /* Group name not found - try to parse this as gid */
338 gid = strtol(gid_str, &endp, 10);
339 if (*gid_str == '\0' || *endp != '\0') {
340 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
345 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
350 if (gid_set && chown(dir, -1, gid) < 0) {
351 perror("chown[ctrl_interface]");
355 /* Make sure the group can enter and read the directory */
357 chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
358 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
363 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
364 sizeof(addr.sun_path)) {
365 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
369 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
370 if (priv->sock < 0) {
371 perror("socket(PF_UNIX)");
375 os_memset(&addr, 0, sizeof(addr));
376 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
377 addr.sun_len = sizeof(addr);
378 #endif /* __FreeBSD__ */
379 addr.sun_family = AF_UNIX;
380 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
383 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
384 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
385 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
387 if (connect(priv->sock, (struct sockaddr *) &addr,
389 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
390 " allow connections - assuming it was left"
391 "over from forced program termination");
392 if (unlink(fname) < 0) {
393 perror("unlink[ctrl_iface]");
394 wpa_printf(MSG_ERROR, "Could not unlink "
395 "existing ctrl_iface socket '%s'",
399 if (bind(priv->sock, (struct sockaddr *) &addr,
401 perror("supp-ctrl-iface-init: bind(PF_UNIX)");
404 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
405 "ctrl_iface socket '%s'", fname);
407 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
408 "be in use - cannot override it");
409 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
410 "not used anymore", fname);
417 if (gid_set && chown(fname, -1, gid) < 0) {
418 perror("chown[ctrl_interface/ifname]");
422 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
423 perror("chmod[ctrl_interface/ifname]");
431 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
433 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
451 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
453 struct wpa_ctrl_dst *dst, *prev;
455 if (priv->sock > -1) {
457 char *buf, *dir = NULL, *gid_str = NULL;
458 eloop_unregister_read_sock(priv->sock);
459 if (!dl_list_empty(&priv->ctrl_dst)) {
461 * Wait a second before closing the control socket if
462 * there are any attached monitors in order to allow
463 * them to receive any pending messages.
465 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
466 "monitors to receive messages");
471 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
477 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
480 if (os_strncmp(buf, "DIR=", 4) == 0) {
482 gid_str = os_strstr(dir, " GROUP=");
490 if (rmdir(dir) < 0) {
491 if (errno == ENOTEMPTY) {
492 wpa_printf(MSG_DEBUG, "Control interface "
493 "directory not empty - leaving it "
496 perror("rmdir[ctrl_interface]");
503 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
511 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
512 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
513 * @level: Priority level of the message
515 * @len: Message length
517 * Send a packet to all monitor programs attached to the control interface.
519 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
520 int level, const char *buf,
523 struct wpa_ctrl_dst *dst, *next;
529 if (priv->sock < 0 || dl_list_empty(&priv->ctrl_dst))
532 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
533 if (res < 0 || (size_t) res >= sizeof(levelstr))
535 io[0].iov_base = levelstr;
536 io[0].iov_len = os_strlen(levelstr);
537 io[1].iov_base = (char *) buf;
539 os_memset(&msg, 0, sizeof(msg));
544 dl_list_for_each_safe(dst, next, &priv->ctrl_dst, struct wpa_ctrl_dst,
546 if (level >= dst->debug_level) {
547 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
548 (u8 *) dst->addr.sun_path, dst->addrlen -
549 offsetof(struct sockaddr_un, sun_path));
550 msg.msg_name = (void *) &dst->addr;
551 msg.msg_namelen = dst->addrlen;
552 if (sendmsg(priv->sock, &msg, 0) < 0) {
554 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
556 idx, errno, strerror(errno));
558 if (dst->errors > 1000 ||
559 (_errno != ENOBUFS && dst->errors > 10) ||
561 wpa_supplicant_ctrl_iface_detach(
573 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
577 struct sockaddr_un from;
578 socklen_t fromlen = sizeof(from);
581 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
582 "attach", priv->wpa_s->ifname);
583 eloop_wait_for_read_sock(priv->sock);
585 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
586 (struct sockaddr *) &from, &fromlen);
588 perror("recvfrom(ctrl_iface)");
593 if (os_strcmp(buf, "ATTACH") == 0) {
594 /* handle ATTACH signal of first monitor interface */
595 if (!wpa_supplicant_ctrl_iface_attach(priv, &from,
597 sendto(priv->sock, "OK\n", 3, 0,
598 (struct sockaddr *) &from, fromlen);
602 sendto(priv->sock, "FAIL\n", 5, 0,
603 (struct sockaddr *) &from, fromlen);
606 /* return FAIL for all other signals */
607 sendto(priv->sock, "FAIL\n", 5, 0,
608 (struct sockaddr *) &from, fromlen);
614 /* Global ctrl_iface */
616 struct ctrl_iface_global_priv {
617 struct wpa_global *global;
622 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
625 struct wpa_global *global = eloop_ctx;
628 struct sockaddr_un from;
629 socklen_t fromlen = sizeof(from);
633 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
634 (struct sockaddr *) &from, &fromlen);
636 perror("recvfrom(ctrl_iface)");
641 reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
645 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
648 } else if (reply_len) {
649 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
655 struct ctrl_iface_global_priv *
656 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
658 struct ctrl_iface_global_priv *priv;
659 struct sockaddr_un addr;
661 priv = os_zalloc(sizeof(*priv));
664 priv->global = global;
667 if (global->params.ctrl_interface == NULL)
671 priv->sock = android_get_control_socket(global->params.ctrl_interface);
676 wpa_printf(MSG_DEBUG, "Global control interface '%s'",
677 global->params.ctrl_interface);
679 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
680 if (priv->sock < 0) {
681 perror("socket(PF_UNIX)");
685 os_memset(&addr, 0, sizeof(addr));
686 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
687 addr.sun_len = sizeof(addr);
688 #endif /* __FreeBSD__ */
689 addr.sun_family = AF_UNIX;
690 os_strlcpy(addr.sun_path, global->params.ctrl_interface,
691 sizeof(addr.sun_path));
692 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
693 perror("supp-global-ctrl-iface-init (will try fixup): "
695 if (connect(priv->sock, (struct sockaddr *) &addr,
697 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
698 " allow connections - assuming it was left"
699 "over from forced program termination");
700 if (unlink(global->params.ctrl_interface) < 0) {
701 perror("unlink[ctrl_iface]");
702 wpa_printf(MSG_ERROR, "Could not unlink "
703 "existing ctrl_iface socket '%s'",
704 global->params.ctrl_interface);
707 if (bind(priv->sock, (struct sockaddr *) &addr,
709 perror("supp-glb-iface-init: bind(PF_UNIX)");
712 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
713 "ctrl_iface socket '%s'",
714 global->params.ctrl_interface);
716 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
717 "be in use - cannot override it");
718 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
720 global->params.ctrl_interface);
728 eloop_register_read_sock(priv->sock,
729 wpa_supplicant_global_ctrl_iface_receive,
743 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
745 if (priv->sock >= 0) {
746 eloop_unregister_read_sock(priv->sock);
749 if (priv->global->params.ctrl_interface)
750 unlink(priv->global->params.ctrl_interface);