2 * WPA Supplicant / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2014, 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 <sys/ioctl.h>
18 #include <linux/sockios.h>
19 #endif /* __linux__ */
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;
54 int android_control_socket;
58 struct ctrl_iface_global_priv {
59 struct wpa_global *global;
61 struct dl_list ctrl_dst;
62 int android_control_socket;
66 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
67 const char *ifname, int sock,
68 struct dl_list *ctrl_dst,
69 int level, const char *buf,
71 struct ctrl_iface_priv *priv,
72 struct ctrl_iface_global_priv *gp);
73 static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
74 struct ctrl_iface_priv *priv);
75 static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
76 struct ctrl_iface_global_priv *priv);
79 static void wpas_ctrl_sock_debug(const char *title, int sock, const char *buf,
85 int level = MSG_MSGDUMP;
87 if (len >= 5 && os_strncmp(buf, "PONG\n", 5) == 0)
88 level = MSG_EXCESSIVE;
90 optlen = sizeof(sndbuf);
92 if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) < 0)
95 if (ioctl(sock, SIOCOUTQ, &outq) < 0)
99 "CTRL-DEBUG: %s: sock=%d sndbuf=%d outq=%d send_len=%d",
100 title, sock, sndbuf, outq, (int) len);
101 #endif /* __linux__ */
105 static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst,
106 struct sockaddr_un *from,
107 socklen_t fromlen, int global)
109 struct wpa_ctrl_dst *dst;
112 dst = os_zalloc(sizeof(*dst));
115 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
116 dst->addrlen = fromlen;
117 dst->debug_level = MSG_INFO;
118 dl_list_add(ctrl_dst, &dst->list);
119 printf_encode(addr_txt, sizeof(addr_txt),
120 (u8 *) from->sun_path,
121 fromlen - offsetof(struct sockaddr_un, sun_path));
122 wpa_printf(MSG_DEBUG, "CTRL_IFACE %smonitor attached %s",
123 global ? "global " : "", addr_txt);
128 static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst,
129 struct sockaddr_un *from,
132 struct wpa_ctrl_dst *dst;
134 dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
135 if (fromlen == dst->addrlen &&
136 os_memcmp(from->sun_path, dst->addr.sun_path,
137 fromlen - offsetof(struct sockaddr_un, sun_path))
140 printf_encode(addr_txt, sizeof(addr_txt),
141 (u8 *) from->sun_path,
143 offsetof(struct sockaddr_un, sun_path));
144 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached %s",
146 dl_list_del(&dst->list);
155 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
156 struct sockaddr_un *from,
160 struct wpa_ctrl_dst *dst;
162 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
164 dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
165 if (fromlen == dst->addrlen &&
166 os_memcmp(from->sun_path, dst->addr.sun_path,
167 fromlen - offsetof(struct sockaddr_un, sun_path))
170 dst->debug_level = atoi(level);
171 printf_encode(addr_txt, sizeof(addr_txt),
172 (u8 *) from->sun_path, fromlen -
173 offsetof(struct sockaddr_un, sun_path));
174 wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor level to %d for %s",
175 dst->debug_level, addr_txt);
184 static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
187 struct wpa_supplicant *wpa_s = eloop_ctx;
188 struct ctrl_iface_priv *priv = sock_ctx;
191 struct sockaddr_un from;
192 socklen_t fromlen = sizeof(from);
193 char *reply = NULL, *reply_buf = NULL;
194 size_t reply_len = 0;
195 int new_attached = 0;
197 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
198 (struct sockaddr *) &from, &fromlen);
200 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
206 if (os_strcmp(buf, "ATTACH") == 0) {
207 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
214 } else if (os_strcmp(buf, "DETACH") == 0) {
215 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
220 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
221 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
227 reply_buf = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
232 * There could be some password/key material in the command, so
233 * clear the buffer explicitly now that it is not needed
236 os_memset(buf, 0, res);
239 if (!reply && reply_len == 1) {
242 } else if (!reply && reply_len == 2) {
248 wpas_ctrl_sock_debug("ctrl_sock-sendto", sock, reply,
250 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
253 wpa_dbg(wpa_s, MSG_DEBUG,
254 "ctrl_iface sendto failed: %d - %s",
255 _errno, strerror(_errno));
256 if (_errno == ENOBUFS || _errno == EAGAIN) {
258 * The socket send buffer could be full. This
259 * may happen if client programs are not
260 * receiving their pending messages. Close and
261 * reopen the socket as a workaround to avoid
262 * getting stuck being unable to send any new
265 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
267 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to reinitialize ctrl_iface socket");
271 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to send response to ATTACH - detaching");
273 wpa_supplicant_ctrl_iface_detach(
274 &priv->ctrl_dst, &from, fromlen);
281 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
285 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
289 char *pbuf, *dir = NULL;
292 if (wpa_s->conf->ctrl_interface == NULL)
295 pbuf = os_strdup(wpa_s->conf->ctrl_interface);
298 if (os_strncmp(pbuf, "DIR=", 4) == 0) {
301 gid_str = os_strstr(dir, " GROUP=");
307 len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
308 buf = os_malloc(len);
314 res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
315 if (os_snprintf_error(len, res)) {
322 /* Windows/WinPcap uses interface names that are not suitable
323 * as a file name - convert invalid chars to underscores */
331 #endif /* __CYGWIN__ */
337 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
338 enum wpa_msg_type type,
339 const char *txt, size_t len)
341 struct wpa_supplicant *wpa_s = ctx;
346 if (type != WPA_MSG_NO_GLOBAL && wpa_s->global->ctrl_iface) {
347 struct ctrl_iface_global_priv *priv = wpa_s->global->ctrl_iface;
348 if (!dl_list_empty(&priv->ctrl_dst)) {
349 wpa_supplicant_ctrl_iface_send(
351 type != WPA_MSG_PER_INTERFACE ?
352 NULL : wpa_s->ifname,
353 priv->sock, &priv->ctrl_dst, level, txt, len,
358 if (type == WPA_MSG_ONLY_GLOBAL || wpa_s->ctrl_iface == NULL)
360 wpa_supplicant_ctrl_iface_send(wpa_s, NULL, wpa_s->ctrl_iface->sock,
361 &wpa_s->ctrl_iface->ctrl_dst,
362 level, txt, len, wpa_s->ctrl_iface,
367 static int wpas_ctrl_iface_open_sock(struct wpa_supplicant *wpa_s,
368 struct ctrl_iface_priv *priv)
370 struct sockaddr_un addr;
374 char *buf, *dir = NULL, *gid_str = NULL;
379 buf = os_strdup(wpa_s->conf->ctrl_interface);
383 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
384 wpa_s->conf->ctrl_interface);
385 priv->sock = android_get_control_socket(addr.sun_path);
386 if (priv->sock >= 0) {
387 priv->android_control_socket = 1;
391 if (os_strncmp(buf, "DIR=", 4) == 0) {
393 gid_str = os_strstr(dir, " GROUP=");
400 gid_str = wpa_s->conf->ctrl_interface_group;
403 if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
404 if (errno == EEXIST) {
405 wpa_printf(MSG_DEBUG, "Using existing control "
406 "interface directory.");
408 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface=%s]: %s",
409 dir, strerror(errno));
416 * wpa_supplicant is started from /init.*.rc on Android and that seems
417 * to be using umask 0077 which would leave the control interface
418 * directory without group access. This breaks things since Wi-Fi
419 * framework assumes that this directory can be accessed by other
420 * applications in the wifi group. Fix this by adding group access even
421 * if umask value would prevent this.
423 if (chmod(dir, S_IRWXU | S_IRWXG) < 0) {
424 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
426 /* Try to continue anyway */
431 grp = getgrnam(gid_str);
435 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
436 " (from group name '%s')",
439 /* Group name not found - try to parse this as gid */
440 gid = strtol(gid_str, &endp, 10);
441 if (*gid_str == '\0' || *endp != '\0') {
442 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
447 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
452 if (gid_set && chown(dir, -1, gid) < 0) {
453 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
454 dir, (int) gid, strerror(errno));
458 /* Make sure the group can enter and read the directory */
460 chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
461 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
466 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
467 sizeof(addr.sun_path)) {
468 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
472 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
473 if (priv->sock < 0) {
474 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
478 os_memset(&addr, 0, sizeof(addr));
479 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
480 addr.sun_len = sizeof(addr);
481 #endif /* __FreeBSD__ */
482 addr.sun_family = AF_UNIX;
483 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
486 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
487 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
488 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
490 if (connect(priv->sock, (struct sockaddr *) &addr,
492 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
493 " allow connections - assuming it was left"
494 "over from forced program termination");
495 if (unlink(fname) < 0) {
496 wpa_printf(MSG_ERROR,
497 "Could not unlink existing ctrl_iface socket '%s': %s",
498 fname, strerror(errno));
501 if (bind(priv->sock, (struct sockaddr *) &addr,
503 wpa_printf(MSG_ERROR, "supp-ctrl-iface-init: bind(PF_UNIX): %s",
507 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
508 "ctrl_iface socket '%s'", fname);
510 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
511 "be in use - cannot override it");
512 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
513 "not used anymore", fname);
520 if (gid_set && chown(fname, -1, gid) < 0) {
521 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
522 fname, (int) gid, strerror(errno));
526 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
527 wpa_printf(MSG_ERROR, "chmod[ctrl_interface=%s]: %s",
528 fname, strerror(errno));
538 * Make socket non-blocking so that we don't hang forever if
539 * target dies unexpectedly.
541 flags = fcntl(priv->sock, F_GETFL);
544 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
545 wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
547 /* Not fatal, continue on.*/
551 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
553 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
559 if (priv->sock >= 0) {
572 struct ctrl_iface_priv *
573 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
575 struct ctrl_iface_priv *priv;
577 priv = os_zalloc(sizeof(*priv));
580 dl_list_init(&priv->ctrl_dst);
584 if (wpa_s->conf->ctrl_interface == NULL)
588 if (wpa_s->global->params.ctrl_interface) {
591 if (wpa_s->global->params.ctrl_interface[0] == '/') {
592 if (os_strcmp(wpa_s->global->params.ctrl_interface,
593 wpa_s->conf->ctrl_interface) == 0)
595 } else if (os_strncmp(wpa_s->global->params.ctrl_interface,
596 "@android:", 9) == 0 ||
597 os_strncmp(wpa_s->global->params.ctrl_interface,
598 "@abstract:", 10) == 0) {
602 * Currently, Android uses @android:wpa_* as the naming
603 * convention for the global ctrl interface. This logic
604 * needs to be revisited if the above naming convention
607 pos = os_strchr(wpa_s->global->params.ctrl_interface,
611 wpa_s->conf->ctrl_interface) == 0)
617 * The invalid configuration combination might be
618 * possible to hit in an Android OTA upgrade case, so
619 * instead of refusing to start the wpa_supplicant
620 * process, do not open the per-interface ctrl_iface
621 * and continue with the global control interface that
622 * was set from the command line since the Wi-Fi
623 * framework will use it for operations.
625 wpa_printf(MSG_ERROR,
626 "global ctrl interface %s matches ctrl interface %s - do not open per-interface ctrl interface",
627 wpa_s->global->params.ctrl_interface,
628 wpa_s->conf->ctrl_interface);
634 if (wpas_ctrl_iface_open_sock(wpa_s, priv) < 0) {
643 static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
644 struct ctrl_iface_priv *priv)
652 * On Android, the control socket being used may be the socket
653 * that is created when wpa_supplicant is started as a /init.*.rc
654 * service. Such a socket is maintained as a key-value pair in
655 * Android's environment. Closing this control socket would leave us
656 * in a bad state with an invalid socket descriptor.
658 if (priv->android_control_socket)
661 eloop_unregister_read_sock(priv->sock);
664 res = wpas_ctrl_iface_open_sock(wpa_s, priv);
671 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
673 struct wpa_ctrl_dst *dst, *prev;
675 if (priv->sock > -1) {
677 char *buf, *dir = NULL;
678 eloop_unregister_read_sock(priv->sock);
679 if (!dl_list_empty(&priv->ctrl_dst)) {
681 * Wait before closing the control socket if
682 * there are any attached monitors in order to allow
683 * them to receive any pending messages.
685 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
686 "monitors to receive messages");
691 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
697 if (priv->wpa_s->conf->ctrl_interface == NULL)
699 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
702 if (os_strncmp(buf, "DIR=", 4) == 0) {
705 gid_str = os_strstr(dir, " GROUP=");
711 if (rmdir(dir) < 0) {
712 if (errno == ENOTEMPTY) {
713 wpa_printf(MSG_DEBUG, "Control interface "
714 "directory not empty - leaving it "
717 wpa_printf(MSG_ERROR,
718 "rmdir[ctrl_interface=%s]: %s",
719 dir, strerror(errno));
726 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
734 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
735 * @ifname: Interface name for global control socket or %NULL
736 * @sock: Local socket fd
737 * @ctrl_dst: List of attached listeners
738 * @level: Priority level of the message
740 * @len: Message length
742 * Send a packet to all monitor programs attached to the control interface.
744 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
745 const char *ifname, int sock,
746 struct dl_list *ctrl_dst,
747 int level, const char *buf,
749 struct ctrl_iface_priv *priv,
750 struct ctrl_iface_global_priv *gp)
752 struct wpa_ctrl_dst *dst, *next;
758 if (sock < 0 || dl_list_empty(ctrl_dst))
761 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
762 if (os_snprintf_error(sizeof(levelstr), res))
766 io[idx].iov_base = "IFNAME=";
769 io[idx].iov_base = (char *) ifname;
770 io[idx].iov_len = os_strlen(ifname);
772 io[idx].iov_base = " ";
776 io[idx].iov_base = levelstr;
777 io[idx].iov_len = os_strlen(levelstr);
779 io[idx].iov_base = (char *) buf;
780 io[idx].iov_len = len;
782 os_memset(&msg, 0, sizeof(msg));
784 msg.msg_iovlen = idx;
786 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
790 if (level < dst->debug_level)
793 printf_encode(addr_txt, sizeof(addr_txt),
794 (u8 *) dst->addr.sun_path, dst->addrlen -
795 offsetof(struct sockaddr_un, sun_path));
796 msg.msg_name = (void *) &dst->addr;
797 msg.msg_namelen = dst->addrlen;
798 wpas_ctrl_sock_debug("ctrl_sock-sendmsg", sock, buf, len);
799 if (sendmsg(sock, &msg, MSG_DONTWAIT) >= 0) {
800 wpa_printf(MSG_MSGDUMP,
801 "CTRL_IFACE monitor sent successfully to %s",
808 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor[%s]: %d - %s",
809 addr_txt, errno, strerror(errno));
812 if (dst->errors > 10 || _errno == ENOENT || _errno == EPERM) {
813 wpa_printf(MSG_INFO, "CTRL_IFACE: Detach monitor %s that cannot receive messages",
815 wpa_supplicant_ctrl_iface_detach(ctrl_dst, &dst->addr,
819 if (_errno == ENOBUFS || _errno == EAGAIN) {
821 * The socket send buffer could be full. This may happen
822 * if client programs are not receiving their pending
823 * messages. Close and reopen the socket as a workaround
824 * to avoid getting stuck being unable to send any new
828 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
830 sock = wpas_ctrl_iface_global_reinit(
835 wpa_dbg(wpa_s, MSG_DEBUG,
836 "Failed to reinitialize ctrl_iface socket");
844 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
848 struct sockaddr_un from;
849 socklen_t fromlen = sizeof(from);
852 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
853 "attach", priv->wpa_s->ifname);
854 eloop_wait_for_read_sock(priv->sock);
856 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
857 (struct sockaddr *) &from, &fromlen);
859 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
865 if (os_strcmp(buf, "ATTACH") == 0) {
866 /* handle ATTACH signal of first monitor interface */
867 if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
870 if (sendto(priv->sock, "OK\n", 3, 0,
871 (struct sockaddr *) &from, fromlen) <
873 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
879 if (sendto(priv->sock, "FAIL\n", 5, 0,
880 (struct sockaddr *) &from, fromlen) <
882 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
887 /* return FAIL for all other signals */
888 if (sendto(priv->sock, "FAIL\n", 5, 0,
889 (struct sockaddr *) &from, fromlen) < 0) {
890 wpa_printf(MSG_DEBUG,
891 "ctrl_iface sendto failed: %s",
899 /* Global ctrl_iface */
901 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
904 struct wpa_global *global = eloop_ctx;
905 struct ctrl_iface_global_priv *priv = sock_ctx;
908 struct sockaddr_un from;
909 socklen_t fromlen = sizeof(from);
910 char *reply = NULL, *reply_buf = NULL;
913 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
914 (struct sockaddr *) &from, &fromlen);
916 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
922 if (os_strcmp(buf, "ATTACH") == 0) {
923 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
928 } else if (os_strcmp(buf, "DETACH") == 0) {
929 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
935 reply_buf = wpa_supplicant_global_ctrl_iface_process(
936 global, buf, &reply_len);
940 * There could be some password/key material in the command, so
941 * clear the buffer explicitly now that it is not needed
944 os_memset(buf, 0, res);
947 if (!reply && reply_len == 1) {
950 } else if (!reply && reply_len == 2) {
956 wpas_ctrl_sock_debug("global_ctrl_sock-sendto",
957 sock, reply, reply_len);
958 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
960 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
968 static int wpas_global_ctrl_iface_open_sock(struct wpa_global *global,
969 struct ctrl_iface_global_priv *priv)
971 struct sockaddr_un addr;
972 const char *ctrl = global->params.ctrl_interface;
975 wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl);
978 if (os_strncmp(ctrl, "@android:", 9) == 0) {
979 priv->sock = android_get_control_socket(ctrl + 9);
980 if (priv->sock < 0) {
981 wpa_printf(MSG_ERROR, "Failed to open Android control "
982 "socket '%s'", ctrl + 9);
985 wpa_printf(MSG_DEBUG, "Using Android control socket '%s'",
987 priv->android_control_socket = 1;
991 if (os_strncmp(ctrl, "@abstract:", 10) != 0) {
993 * Backwards compatibility - try to open an Android control
994 * socket and if that fails, assume this was a UNIX domain
997 priv->sock = android_get_control_socket(ctrl);
998 if (priv->sock >= 0) {
999 wpa_printf(MSG_DEBUG,
1000 "Using Android control socket '%s'",
1002 priv->android_control_socket = 1;
1006 #endif /* ANDROID */
1008 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
1009 if (priv->sock < 0) {
1010 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
1014 os_memset(&addr, 0, sizeof(addr));
1015 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1016 addr.sun_len = sizeof(addr);
1017 #endif /* __FreeBSD__ */
1018 addr.sun_family = AF_UNIX;
1020 if (os_strncmp(ctrl, "@abstract:", 10) == 0) {
1021 addr.sun_path[0] = '\0';
1022 os_strlcpy(addr.sun_path + 1, ctrl + 10,
1023 sizeof(addr.sun_path) - 1);
1024 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) <
1026 wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: "
1027 "bind(PF_UNIX;%s) failed: %s",
1028 ctrl, strerror(errno));
1031 wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'",
1036 os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path));
1037 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1038 wpa_printf(MSG_INFO, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s",
1039 ctrl, strerror(errno));
1040 if (connect(priv->sock, (struct sockaddr *) &addr,
1041 sizeof(addr)) < 0) {
1042 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1043 " allow connections - assuming it was left"
1044 "over from forced program termination");
1045 if (unlink(ctrl) < 0) {
1046 wpa_printf(MSG_ERROR,
1047 "Could not unlink existing ctrl_iface socket '%s': %s",
1048 ctrl, strerror(errno));
1051 if (bind(priv->sock, (struct sockaddr *) &addr,
1052 sizeof(addr)) < 0) {
1053 wpa_printf(MSG_ERROR, "supp-glb-iface-init: bind(PF_UNIX;%s): %s",
1054 ctrl, strerror(errno));
1057 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1058 "ctrl_iface socket '%s'",
1061 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1062 "be in use - cannot override it");
1063 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1070 wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl);
1072 if (global->params.ctrl_interface_group) {
1073 char *gid_str = global->params.ctrl_interface_group;
1078 grp = getgrnam(gid_str);
1081 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
1082 " (from group name '%s')",
1083 (int) gid, gid_str);
1085 /* Group name not found - try to parse this as gid */
1086 gid = strtol(gid_str, &endp, 10);
1087 if (*gid_str == '\0' || *endp != '\0') {
1088 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
1092 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
1095 if (chown(ctrl, -1, gid) < 0) {
1096 wpa_printf(MSG_ERROR,
1097 "chown[global_ctrl_interface=%s,gid=%d]: %s",
1098 ctrl, (int) gid, strerror(errno));
1102 if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) {
1103 wpa_printf(MSG_ERROR,
1104 "chmod[global_ctrl_interface=%s]: %s",
1105 ctrl, strerror(errno));
1109 if (chmod(ctrl, S_IRWXU) < 0) {
1110 wpa_printf(MSG_DEBUG,
1111 "chmod[global_ctrl_interface=%s](S_IRWXU): %s",
1112 ctrl, strerror(errno));
1113 /* continue anyway since group change was not required
1121 * Make socket non-blocking so that we don't hang forever if
1122 * target dies unexpectedly.
1124 flags = fcntl(priv->sock, F_GETFL);
1126 flags |= O_NONBLOCK;
1127 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
1128 wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
1130 /* Not fatal, continue on.*/
1134 eloop_register_read_sock(priv->sock,
1135 wpa_supplicant_global_ctrl_iface_receive,
1141 if (priv->sock >= 0) {
1149 struct ctrl_iface_global_priv *
1150 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
1152 struct ctrl_iface_global_priv *priv;
1154 priv = os_zalloc(sizeof(*priv));
1157 dl_list_init(&priv->ctrl_dst);
1158 priv->global = global;
1161 if (global->params.ctrl_interface == NULL)
1164 if (wpas_global_ctrl_iface_open_sock(global, priv) < 0) {
1169 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
1175 static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
1176 struct ctrl_iface_global_priv *priv)
1180 if (priv->sock <= 0)
1184 * On Android, the control socket being used may be the socket
1185 * that is created when wpa_supplicant is started as a /init.*.rc
1186 * service. Such a socket is maintained as a key-value pair in
1187 * Android's environment. Closing this control socket would leave us
1188 * in a bad state with an invalid socket descriptor.
1190 if (priv->android_control_socket)
1193 eloop_unregister_read_sock(priv->sock);
1196 res = wpas_global_ctrl_iface_open_sock(global, priv);
1204 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
1206 struct wpa_ctrl_dst *dst, *prev;
1208 if (priv->sock >= 0) {
1209 eloop_unregister_read_sock(priv->sock);
1212 if (priv->global->params.ctrl_interface)
1213 unlink(priv->global->params.ctrl_interface);
1214 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,