2 * WPA Supplicant / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2005, 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.
20 #include <cutils/sockets.h>
27 #include "wpa_supplicant_i.h"
28 #include "ctrl_iface.h"
30 /* Per-interface ctrl_iface */
33 * struct wpa_ctrl_dst - Internal data structure of control interface monitors
35 * This structure is used to store information about registered control
36 * interface monitors into struct wpa_supplicant. This data is private to
37 * ctrl_iface_unix.c and should not be touched directly from other files.
40 struct wpa_ctrl_dst *next;
41 struct sockaddr_un addr;
48 struct ctrl_iface_priv {
49 struct wpa_supplicant *wpa_s;
51 struct wpa_ctrl_dst *ctrl_dst;
55 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
56 int level, const char *buf,
60 static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv,
61 struct sockaddr_un *from,
64 struct wpa_ctrl_dst *dst;
66 dst = os_zalloc(sizeof(*dst));
69 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
70 dst->addrlen = fromlen;
71 dst->debug_level = MSG_INFO;
72 dst->next = priv->ctrl_dst;
74 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
75 (u8 *) from->sun_path, fromlen - sizeof(from->sun_family));
80 static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
81 struct sockaddr_un *from,
84 struct wpa_ctrl_dst *dst, *prev = NULL;
88 if (fromlen == dst->addrlen &&
89 os_memcmp(from->sun_path, dst->addr.sun_path,
90 fromlen - sizeof(from->sun_family)) == 0) {
92 priv->ctrl_dst = dst->next;
94 prev->next = dst->next;
96 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
97 (u8 *) from->sun_path,
98 fromlen - sizeof(from->sun_family));
108 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
109 struct sockaddr_un *from,
113 struct wpa_ctrl_dst *dst;
115 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
117 dst = priv->ctrl_dst;
119 if (fromlen == dst->addrlen &&
120 os_memcmp(from->sun_path, dst->addr.sun_path,
121 fromlen - sizeof(from->sun_family)) == 0) {
122 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
123 "level", (u8 *) from->sun_path,
124 fromlen - sizeof(from->sun_family));
125 dst->debug_level = atoi(level);
135 static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
138 struct wpa_supplicant *wpa_s = eloop_ctx;
139 struct ctrl_iface_priv *priv = sock_ctx;
142 struct sockaddr_un from;
143 socklen_t fromlen = sizeof(from);
145 size_t reply_len = 0;
146 int new_attached = 0;
148 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
149 (struct sockaddr *) &from, &fromlen);
151 perror("recvfrom(ctrl_iface)");
156 if (os_strcmp(buf, "ATTACH") == 0) {
157 if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen))
163 } else if (os_strcmp(buf, "DETACH") == 0) {
164 if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen))
168 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
169 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
175 reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
180 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
183 } else if (reply_len == 1) {
184 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
186 } else if (reply_len == 2) {
187 sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from,
192 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
196 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
200 char *pbuf, *dir = NULL, *gid_str = NULL;
202 if (wpa_s->conf->ctrl_interface == NULL)
205 pbuf = os_strdup(wpa_s->conf->ctrl_interface);
208 if (os_strncmp(pbuf, "DIR=", 4) == 0) {
210 gid_str = os_strstr(dir, " GROUP=");
218 len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
219 buf = os_malloc(len);
225 os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
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;
265 priv = os_zalloc(sizeof(*priv));
271 if (wpa_s->conf->ctrl_interface == NULL)
274 buf = os_strdup(wpa_s->conf->ctrl_interface);
278 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
279 wpa_s->conf->ctrl_interface);
280 priv->sock = android_get_control_socket(addr.sun_path);
284 if (os_strncmp(buf, "DIR=", 4) == 0) {
286 gid_str = os_strstr(dir, " GROUP=");
293 gid_str = wpa_s->conf->ctrl_interface_group;
296 if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
297 if (errno == EEXIST) {
298 wpa_printf(MSG_DEBUG, "Using existing control "
299 "interface directory.");
301 perror("mkdir[ctrl_interface]");
307 grp = getgrnam(gid_str);
311 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
312 " (from group name '%s')",
315 /* Group name not found - try to parse this as gid */
316 gid = strtol(gid_str, &endp, 10);
317 if (*gid_str == '\0' || *endp != '\0') {
318 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
323 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
328 if (gid_set && chown(dir, -1, gid) < 0) {
329 perror("chown[ctrl_interface]");
333 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
334 sizeof(addr.sun_path))
337 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
338 if (priv->sock < 0) {
339 perror("socket(PF_UNIX)");
343 os_memset(&addr, 0, sizeof(addr));
344 addr.sun_family = AF_UNIX;
345 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
348 os_strncpy(addr.sun_path, fname, sizeof(addr.sun_path));
349 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
350 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
352 if (connect(priv->sock, (struct sockaddr *) &addr,
354 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
355 " allow connections - assuming it was left"
356 "over from forced program termination");
357 if (unlink(fname) < 0) {
358 perror("unlink[ctrl_iface]");
359 wpa_printf(MSG_ERROR, "Could not unlink "
360 "existing ctrl_iface socket '%s'",
364 if (bind(priv->sock, (struct sockaddr *) &addr,
366 perror("bind(PF_UNIX)");
369 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
370 "ctrl_iface socket '%s'", fname);
372 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
373 "be in use - cannot override it");
374 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
375 "not used anymore", fname);
382 if (gid_set && chown(fname, -1, gid) < 0) {
383 perror("chown[ctrl_interface/ifname]");
387 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
388 perror("chmod[ctrl_interface/ifname]");
394 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
396 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
414 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
416 struct wpa_ctrl_dst *dst, *prev;
418 if (priv->sock > -1) {
420 char *buf, *dir = NULL, *gid_str = NULL;
421 eloop_unregister_read_sock(priv->sock);
422 if (priv->ctrl_dst) {
424 * Wait a second before closing the control socket if
425 * there are any attached monitors in order to allow
426 * them to receive any pending messages.
428 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
429 "monitors to receive messages");
434 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
440 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
443 if (os_strncmp(buf, "DIR=", 4) == 0) {
445 gid_str = os_strstr(dir, " GROUP=");
453 if (rmdir(dir) < 0) {
454 if (errno == ENOTEMPTY) {
455 wpa_printf(MSG_DEBUG, "Control interface "
456 "directory not empty - leaving it "
459 perror("rmdir[ctrl_interface]");
466 dst = priv->ctrl_dst;
477 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
478 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
479 * @level: Priority level of the message
481 * @len: Message length
483 * Send a packet to all monitor programs attached to the control interface.
485 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
486 int level, const char *buf,
489 struct wpa_ctrl_dst *dst, *next;
495 dst = priv->ctrl_dst;
496 if (priv->sock < 0 || dst == NULL)
499 os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
500 io[0].iov_base = levelstr;
501 io[0].iov_len = os_strlen(levelstr);
502 io[1].iov_base = (char *) buf;
504 os_memset(&msg, 0, sizeof(msg));
511 if (level >= dst->debug_level) {
512 wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
513 (u8 *) dst->addr.sun_path, dst->addrlen -
514 sizeof(dst->addr.sun_family));
515 msg.msg_name = (void *) &dst->addr;
516 msg.msg_namelen = dst->addrlen;
517 if (sendmsg(priv->sock, &msg, 0) < 0) {
518 perror("sendmsg(CTRL_IFACE monitor)");
520 if (dst->errors > 10) {
521 wpa_supplicant_ctrl_iface_detach(
534 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
538 struct sockaddr_un from;
539 socklen_t fromlen = sizeof(from);
542 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
543 "attach", priv->wpa_s->ifname);
544 eloop_wait_for_read_sock(priv->sock);
546 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
547 (struct sockaddr *) &from, &fromlen);
549 perror("recvfrom(ctrl_iface)");
554 if (os_strcmp(buf, "ATTACH") == 0) {
555 /* handle ATTACH signal of first monitor interface */
556 if (!wpa_supplicant_ctrl_iface_attach(priv, &from,
558 sendto(priv->sock, "OK\n", 3, 0,
559 (struct sockaddr *) &from, fromlen);
563 sendto(priv->sock, "FAIL\n", 5, 0,
564 (struct sockaddr *) &from, fromlen);
567 /* return FAIL for all other signals */
568 sendto(priv->sock, "FAIL\n", 5, 0,
569 (struct sockaddr *) &from, fromlen);
575 /* Global ctrl_iface */
577 struct ctrl_iface_global_priv {
578 struct wpa_global *global;
583 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
586 struct wpa_global *global = eloop_ctx;
589 struct sockaddr_un from;
590 socklen_t fromlen = sizeof(from);
594 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
595 (struct sockaddr *) &from, &fromlen);
597 perror("recvfrom(ctrl_iface)");
602 reply = wpa_supplicant_global_ctrl_iface_process(global, buf,
606 sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
609 } else if (reply_len) {
610 sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
616 struct ctrl_iface_global_priv *
617 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
619 struct ctrl_iface_global_priv *priv;
620 struct sockaddr_un addr;
622 priv = os_zalloc(sizeof(*priv));
625 priv->global = global;
628 if (global->params.ctrl_interface == NULL)
632 priv->sock = android_get_control_socket(global->params.ctrl_interface);
636 wpa_printf(MSG_DEBUG, "Global control interface '%s'",
637 global->params.ctrl_interface);
639 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
640 if (priv->sock < 0) {
641 perror("socket(PF_UNIX)");
645 os_memset(&addr, 0, sizeof(addr));
646 addr.sun_family = AF_UNIX;
647 os_strncpy(addr.sun_path, global->params.ctrl_interface,
648 sizeof(addr.sun_path));
649 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
650 perror("bind(PF_UNIX)");
651 if (connect(priv->sock, (struct sockaddr *) &addr,
653 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
654 " allow connections - assuming it was left"
655 "over from forced program termination");
656 if (unlink(global->params.ctrl_interface) < 0) {
657 perror("unlink[ctrl_iface]");
658 wpa_printf(MSG_ERROR, "Could not unlink "
659 "existing ctrl_iface socket '%s'",
660 global->params.ctrl_interface);
663 if (bind(priv->sock, (struct sockaddr *) &addr,
665 perror("bind(PF_UNIX)");
668 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
669 "ctrl_iface socket '%s'",
670 global->params.ctrl_interface);
672 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
673 "be in use - cannot override it");
674 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
676 global->params.ctrl_interface);
681 eloop_register_read_sock(priv->sock,
682 wpa_supplicant_global_ctrl_iface_receive,
696 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
698 if (priv->sock >= 0) {
699 eloop_unregister_read_sock(priv->sock);
702 if (priv->global->params.ctrl_interface)
703 unlink(priv->global->params.ctrl_interface);