2 * WPA Supplicant / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
21 #include <cutils/sockets.h>
24 #include "utils/common.h"
25 #include "utils/eloop.h"
26 #include "utils/list.h"
27 #include "eapol_supp/eapol_supp_sm.h"
29 #include "wpa_supplicant_i.h"
30 #include "ctrl_iface.h"
32 /* Per-interface ctrl_iface */
35 * struct wpa_ctrl_dst - Internal data structure of control interface monitors
37 * This structure is used to store information about registered control
38 * interface monitors into struct wpa_supplicant. This data is private to
39 * ctrl_iface_unix.c and should not be touched directly from other files.
43 struct sockaddr_un addr;
50 struct ctrl_iface_priv {
51 struct wpa_supplicant *wpa_s;
53 struct dl_list ctrl_dst;
57 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
58 int level, const char *buf,
62 static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv,
63 struct sockaddr_un *from,
66 struct wpa_ctrl_dst *dst;
68 dst = os_zalloc(sizeof(*dst));
71 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
72 dst->addrlen = fromlen;
73 dst->debug_level = MSG_INFO;
74 dl_list_add(&priv->ctrl_dst, &dst->list);
75 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
76 (u8 *) from->sun_path,
77 fromlen - offsetof(struct sockaddr_un, sun_path));
82 static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
83 struct sockaddr_un *from,
86 struct wpa_ctrl_dst *dst;
88 dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
89 if (fromlen == dst->addrlen &&
90 os_memcmp(from->sun_path, dst->addr.sun_path,
91 fromlen - offsetof(struct sockaddr_un, sun_path))
93 dl_list_del(&dst->list);
95 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
96 (u8 *) from->sun_path,
98 offsetof(struct sockaddr_un, sun_path));
106 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
107 struct sockaddr_un *from,
111 struct wpa_ctrl_dst *dst;
113 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
115 dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
116 if (fromlen == dst->addrlen &&
117 os_memcmp(from->sun_path, dst->addr.sun_path,
118 fromlen - offsetof(struct sockaddr_un, sun_path))
120 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
121 "level", (u8 *) from->sun_path,
123 offsetof(struct sockaddr_un, sun_path));
124 dst->debug_level = atoi(level);
133 static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
136 struct wpa_supplicant *wpa_s = eloop_ctx;
137 struct ctrl_iface_priv *priv = sock_ctx;
140 struct sockaddr_un from;
141 socklen_t fromlen = sizeof(from);
143 size_t reply_len = 0;
144 int new_attached = 0;
146 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
147 (struct sockaddr *) &from, &fromlen);
149 perror("recvfrom(ctrl_iface)");
154 if (os_strcmp(buf, "ATTACH") == 0) {
155 if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen))
161 } else if (os_strcmp(buf, "DETACH") == 0) {
162 if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen))
166 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
167 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
173 reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
178 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
181 } else if (reply_len == 1) {
182 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
184 } else if (reply_len == 2) {
185 sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
190 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
194 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
198 char *pbuf, *dir = NULL, *gid_str = NULL;
201 if (wpa_s->conf->ctrl_interface == NULL)
204 pbuf = os_strdup(wpa_s->conf->ctrl_interface);
207 if (os_strncmp(pbuf, "DIR=", 4) == 0) {
209 gid_str = os_strstr(dir, " GROUP=");
217 len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
218 buf = os_malloc(len);
224 res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
225 if (res < 0 || (size_t) res >= len) {
232 /* Windows/WinPcap uses interface names that are not suitable
233 * as a file name - convert invalid chars to underscores */
241 #endif /* __CYGWIN__ */
247 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
248 const char *txt, size_t len)
250 struct wpa_supplicant *wpa_s = ctx;
251 if (wpa_s == NULL || wpa_s->ctrl_iface == NULL)
253 wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len);
257 struct ctrl_iface_priv *
258 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
260 struct ctrl_iface_priv *priv;
261 struct sockaddr_un addr;
265 char *buf, *dir = NULL, *gid_str = NULL;
269 priv = os_zalloc(sizeof(*priv));
272 dl_list_init(&priv->ctrl_dst);
276 if (wpa_s->conf->ctrl_interface == NULL)
279 buf = os_strdup(wpa_s->conf->ctrl_interface);
283 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
284 wpa_s->conf->ctrl_interface);
285 priv->sock = android_get_control_socket(addr.sun_path);
289 if (os_strncmp(buf, "DIR=", 4) == 0) {
291 gid_str = os_strstr(dir, " GROUP=");
298 gid_str = wpa_s->conf->ctrl_interface_group;
301 if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
302 if (errno == EEXIST) {
303 wpa_printf(MSG_DEBUG, "Using existing control "
304 "interface directory.");
306 perror("mkdir[ctrl_interface]");
312 grp = getgrnam(gid_str);
316 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
317 " (from group name '%s')",
320 /* Group name not found - try to parse this as gid */
321 gid = strtol(gid_str, &endp, 10);
322 if (*gid_str == '\0' || *endp != '\0') {
323 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
328 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
333 if (gid_set && chown(dir, -1, gid) < 0) {
334 perror("chown[ctrl_interface]");
338 /* Make sure the group can enter and read the directory */
340 chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
341 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
346 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
347 sizeof(addr.sun_path)) {
348 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
352 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
353 if (priv->sock < 0) {
354 perror("socket(PF_UNIX)");
358 os_memset(&addr, 0, sizeof(addr));
359 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
360 addr.sun_len = sizeof(addr);
361 #endif /* __FreeBSD__ */
362 addr.sun_family = AF_UNIX;
363 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
366 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
367 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
368 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
370 if (connect(priv->sock, (struct sockaddr *) &addr,
372 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
373 " allow connections - assuming it was left"
374 "over from forced program termination");
375 if (unlink(fname) < 0) {
376 perror("unlink[ctrl_iface]");
377 wpa_printf(MSG_ERROR, "Could not unlink "
378 "existing ctrl_iface socket '%s'",
382 if (bind(priv->sock, (struct sockaddr *) &addr,
384 perror("bind(PF_UNIX)");
387 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
388 "ctrl_iface socket '%s'", fname);
390 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
391 "be in use - cannot override it");
392 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
393 "not used anymore", fname);
400 if (gid_set && chown(fname, -1, gid) < 0) {
401 perror("chown[ctrl_interface/ifname]");
405 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
406 perror("chmod[ctrl_interface/ifname]");
414 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
416 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
434 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
436 struct wpa_ctrl_dst *dst, *prev;
438 if (priv->sock > -1) {
440 char *buf, *dir = NULL, *gid_str = NULL;
441 eloop_unregister_read_sock(priv->sock);
442 if (!dl_list_empty(&priv->ctrl_dst)) {
444 * Wait a second before closing the control socket if
445 * there are any attached monitors in order to allow
446 * them to receive any pending messages.
448 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
449 "monitors to receive messages");
454 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
460 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
463 if (os_strncmp(buf, "DIR=", 4) == 0) {
465 gid_str = os_strstr(dir, " GROUP=");
473 if (rmdir(dir) < 0) {
474 if (errno == ENOTEMPTY) {
475 wpa_printf(MSG_DEBUG, "Control interface "
476 "directory not empty - leaving it "
479 perror("rmdir[ctrl_interface]");
486 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
494 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
495 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
496 * @level: Priority level of the message
498 * @len: Message length
500 * Send a packet to all monitor programs attached to the control interface.
502 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
503 int level, const char *buf,
506 struct wpa_ctrl_dst *dst, *next;
512 if (priv->sock < 0 || dl_list_empty(&priv->ctrl_dst))
515 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
516 if (res < 0 || (size_t) res >= sizeof(levelstr))
518 io[0].iov_base = levelstr;
519 io[0].iov_len = os_strlen(levelstr);
520 io[1].iov_base = (char *) buf;
522 os_memset(&msg, 0, sizeof(msg));
527 dl_list_for_each_safe(dst, next, &priv->ctrl_dst, struct wpa_ctrl_dst,
529 if (level >= dst->debug_level) {
530 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
531 (u8 *) dst->addr.sun_path, dst->addrlen -
532 offsetof(struct sockaddr_un, sun_path));
533 msg.msg_name = (void *) &dst->addr;
534 msg.msg_namelen = dst->addrlen;
535 if (sendmsg(priv->sock, &msg, 0) < 0) {
537 wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
539 idx, errno, strerror(errno));
541 if (dst->errors > 1000 ||
542 (_errno != ENOBUFS && dst->errors > 10) ||
544 wpa_supplicant_ctrl_iface_detach(
556 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
560 struct sockaddr_un from;
561 socklen_t fromlen = sizeof(from);
564 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
565 "attach", priv->wpa_s->ifname);
566 eloop_wait_for_read_sock(priv->sock);
568 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
569 (struct sockaddr *) &from, &fromlen);
571 perror("recvfrom(ctrl_iface)");
576 if (os_strcmp(buf, "ATTACH") == 0) {
577 /* handle ATTACH signal of first monitor interface */
578 if (!wpa_supplicant_ctrl_iface_attach(priv, &from,
580 sendto(priv->sock, "OK\n", 3, 0,
581 (struct sockaddr *) &from, fromlen);
585 sendto(priv->sock, "FAIL\n", 5, 0,
586 (struct sockaddr *) &from, fromlen);
589 /* return FAIL for all other signals */
590 sendto(priv->sock, "FAIL\n", 5, 0,
591 (struct sockaddr *) &from, fromlen);
597 /* Global ctrl_iface */
599 struct ctrl_iface_global_priv {
600 struct wpa_global *global;
605 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
608 struct wpa_global *global = eloop_ctx;
611 struct sockaddr_un from;
612 socklen_t fromlen = sizeof(from);
616 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
617 (struct sockaddr *) &from, &fromlen);
619 perror("recvfrom(ctrl_iface)");
624 reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
628 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
631 } else if (reply_len) {
632 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
638 struct ctrl_iface_global_priv *
639 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
641 struct ctrl_iface_global_priv *priv;
642 struct sockaddr_un addr;
644 priv = os_zalloc(sizeof(*priv));
647 priv->global = global;
650 if (global->params.ctrl_interface == NULL)
654 priv->sock = android_get_control_socket(global->params.ctrl_interface);
659 wpa_printf(MSG_DEBUG, "Global control interface '%s'",
660 global->params.ctrl_interface);
662 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
663 if (priv->sock < 0) {
664 perror("socket(PF_UNIX)");
668 os_memset(&addr, 0, sizeof(addr));
669 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
670 addr.sun_len = sizeof(addr);
671 #endif /* __FreeBSD__ */
672 addr.sun_family = AF_UNIX;
673 os_strlcpy(addr.sun_path, global->params.ctrl_interface,
674 sizeof(addr.sun_path));
675 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
676 perror("bind(PF_UNIX)");
677 if (connect(priv->sock, (struct sockaddr *) &addr,
679 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
680 " allow connections - assuming it was left"
681 "over from forced program termination");
682 if (unlink(global->params.ctrl_interface) < 0) {
683 perror("unlink[ctrl_iface]");
684 wpa_printf(MSG_ERROR, "Could not unlink "
685 "existing ctrl_iface socket '%s'",
686 global->params.ctrl_interface);
689 if (bind(priv->sock, (struct sockaddr *) &addr,
691 perror("bind(PF_UNIX)");
694 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
695 "ctrl_iface socket '%s'",
696 global->params.ctrl_interface);
698 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
699 "be in use - cannot override it");
700 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
702 global->params.ctrl_interface);
710 eloop_register_read_sock(priv->sock,
711 wpa_supplicant_global_ctrl_iface_receive,
725 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
727 if (priv->sock >= 0) {
728 eloop_unregister_read_sock(priv->sock);
731 if (priv->global->params.ctrl_interface)
732 unlink(priv->global->params.ctrl_interface);