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;
55 struct dl_list msg_queue;
56 unsigned int throttle_count;
60 struct ctrl_iface_global_priv {
61 struct wpa_global *global;
63 struct dl_list ctrl_dst;
64 int android_control_socket;
65 struct dl_list msg_queue;
66 unsigned int throttle_count;
69 struct ctrl_iface_msg {
71 struct wpa_supplicant *wpa_s;
73 enum wpa_msg_type type;
79 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
80 const char *ifname, int sock,
81 struct dl_list *ctrl_dst,
82 int level, const char *buf,
84 struct ctrl_iface_priv *priv,
85 struct ctrl_iface_global_priv *gp);
86 static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
87 struct ctrl_iface_priv *priv);
88 static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
89 struct ctrl_iface_global_priv *priv);
92 static void wpas_ctrl_sock_debug(const char *title, int sock, const char *buf,
98 int level = MSG_MSGDUMP;
100 if (len >= 5 && os_strncmp(buf, "PONG\n", 5) == 0)
101 level = MSG_EXCESSIVE;
103 optlen = sizeof(sndbuf);
105 if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) < 0)
108 if (ioctl(sock, SIOCOUTQ, &outq) < 0)
112 "CTRL-DEBUG: %s: sock=%d sndbuf=%d outq=%d send_len=%d",
113 title, sock, sndbuf, outq, (int) len);
114 #endif /* __linux__ */
118 static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst,
119 struct sockaddr_un *from,
120 socklen_t fromlen, int global)
122 struct wpa_ctrl_dst *dst;
125 dst = os_zalloc(sizeof(*dst));
128 os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
129 dst->addrlen = fromlen;
130 dst->debug_level = MSG_INFO;
131 dl_list_add(ctrl_dst, &dst->list);
132 printf_encode(addr_txt, sizeof(addr_txt),
133 (u8 *) from->sun_path,
134 fromlen - offsetof(struct sockaddr_un, sun_path));
135 wpa_printf(MSG_DEBUG, "CTRL_IFACE %smonitor attached %s",
136 global ? "global " : "", addr_txt);
141 static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst,
142 struct sockaddr_un *from,
145 struct wpa_ctrl_dst *dst;
147 dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
148 if (fromlen == dst->addrlen &&
149 os_memcmp(from->sun_path, dst->addr.sun_path,
150 fromlen - offsetof(struct sockaddr_un, sun_path))
153 printf_encode(addr_txt, sizeof(addr_txt),
154 (u8 *) from->sun_path,
156 offsetof(struct sockaddr_un, sun_path));
157 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached %s",
159 dl_list_del(&dst->list);
168 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
169 struct sockaddr_un *from,
173 struct wpa_ctrl_dst *dst;
175 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
177 dl_list_for_each(dst, &priv->ctrl_dst, struct wpa_ctrl_dst, list) {
178 if (fromlen == dst->addrlen &&
179 os_memcmp(from->sun_path, dst->addr.sun_path,
180 fromlen - offsetof(struct sockaddr_un, sun_path))
183 dst->debug_level = atoi(level);
184 printf_encode(addr_txt, sizeof(addr_txt),
185 (u8 *) from->sun_path, fromlen -
186 offsetof(struct sockaddr_un, sun_path));
187 wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor level to %d for %s",
188 dst->debug_level, addr_txt);
197 static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
200 struct wpa_supplicant *wpa_s = eloop_ctx;
201 struct ctrl_iface_priv *priv = sock_ctx;
204 struct sockaddr_un from;
205 socklen_t fromlen = sizeof(from);
206 char *reply = NULL, *reply_buf = NULL;
207 size_t reply_len = 0;
208 int new_attached = 0;
210 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
211 (struct sockaddr *) &from, &fromlen);
213 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
219 if (os_strcmp(buf, "ATTACH") == 0) {
220 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
227 } else if (os_strcmp(buf, "DETACH") == 0) {
228 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
233 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
234 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
240 reply_buf = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
245 * There could be some password/key material in the command, so
246 * clear the buffer explicitly now that it is not needed
249 os_memset(buf, 0, res);
252 if (!reply && reply_len == 1) {
255 } else if (!reply && reply_len == 2) {
261 wpas_ctrl_sock_debug("ctrl_sock-sendto", sock, reply,
263 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
266 wpa_dbg(wpa_s, MSG_DEBUG,
267 "ctrl_iface sendto failed: %d - %s",
268 _errno, strerror(_errno));
269 if (_errno == ENOBUFS || _errno == EAGAIN) {
271 * The socket send buffer could be full. This
272 * may happen if client programs are not
273 * receiving their pending messages. Close and
274 * reopen the socket as a workaround to avoid
275 * getting stuck being unable to send any new
278 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
280 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to reinitialize ctrl_iface socket");
284 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to send response to ATTACH - detaching");
286 wpa_supplicant_ctrl_iface_detach(
287 &priv->ctrl_dst, &from, fromlen);
294 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
298 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
302 char *pbuf, *dir = NULL;
305 if (wpa_s->conf->ctrl_interface == NULL)
308 pbuf = os_strdup(wpa_s->conf->ctrl_interface);
311 if (os_strncmp(pbuf, "DIR=", 4) == 0) {
314 gid_str = os_strstr(dir, " GROUP=");
320 len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
321 buf = os_malloc(len);
327 res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
328 if (os_snprintf_error(len, res)) {
335 /* Windows/WinPcap uses interface names that are not suitable
336 * as a file name - convert invalid chars to underscores */
344 #endif /* __CYGWIN__ */
350 static int wpas_ctrl_iface_throttle(int sock)
356 optlen = sizeof(sndbuf);
358 if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) < 0 ||
359 ioctl(sock, SIOCOUTQ, &outq) < 0 ||
360 sndbuf <= 0 || outq < 0)
362 return outq > sndbuf / 2;
363 #else /* __linux__ */
365 #endif /* __linux__ */
369 static void wpas_ctrl_msg_send_pending_global(struct wpa_global *global)
371 struct ctrl_iface_global_priv *gpriv;
372 struct ctrl_iface_msg *msg;
374 gpriv = global->ctrl_iface;
375 while (gpriv && !dl_list_empty(&gpriv->msg_queue) &&
376 !wpas_ctrl_iface_throttle(gpriv->sock)) {
377 msg = dl_list_first(&gpriv->msg_queue, struct ctrl_iface_msg,
381 dl_list_del(&msg->list);
382 wpa_supplicant_ctrl_iface_send(
384 msg->type != WPA_MSG_PER_INTERFACE ?
385 NULL : msg->wpa_s->ifname,
386 gpriv->sock, &gpriv->ctrl_dst, msg->level,
387 msg->txt, msg->len, NULL, gpriv);
393 static void wpas_ctrl_msg_send_pending_iface(struct wpa_supplicant *wpa_s)
395 struct ctrl_iface_priv *priv;
396 struct ctrl_iface_msg *msg;
398 priv = wpa_s->ctrl_iface;
399 while (priv && !dl_list_empty(&priv->msg_queue) &&
400 !wpas_ctrl_iface_throttle(priv->sock)) {
401 msg = dl_list_first(&priv->msg_queue, struct ctrl_iface_msg,
405 dl_list_del(&msg->list);
406 wpa_supplicant_ctrl_iface_send(wpa_s, NULL, priv->sock,
407 &priv->ctrl_dst, msg->level,
408 msg->txt, msg->len, priv, NULL);
414 static void wpas_ctrl_msg_queue_timeout(void *eloop_ctx, void *timeout_ctx)
416 struct wpa_supplicant *wpa_s = eloop_ctx;
417 struct ctrl_iface_priv *priv;
418 struct ctrl_iface_global_priv *gpriv;
419 int sock = -1, gsock = -1;
421 wpas_ctrl_msg_send_pending_global(wpa_s->global);
422 wpas_ctrl_msg_send_pending_iface(wpa_s);
424 priv = wpa_s->ctrl_iface;
425 if (priv && !dl_list_empty(&priv->msg_queue))
428 gpriv = wpa_s->global->ctrl_iface;
429 if (gpriv && !dl_list_empty(&gpriv->msg_queue))
432 if (sock > -1 || gsock > -1) {
433 /* Continue pending message transmission from a timeout */
434 wpa_printf(MSG_MSGDUMP,
435 "CTRL: Had to throttle pending event message transmission for (sock %d gsock %d)",
437 eloop_register_timeout(0, 20000, wpas_ctrl_msg_queue_timeout,
443 static void wpas_ctrl_msg_queue(struct dl_list *queue,
444 struct wpa_supplicant *wpa_s, int level,
445 enum wpa_msg_type type,
446 const char *txt, size_t len)
448 struct ctrl_iface_msg *msg;
450 msg = os_zalloc(sizeof(*msg) + len);
457 os_memcpy(msg + 1, txt, len);
458 msg->txt = (const char *) (msg + 1);
460 dl_list_add_tail(queue, &msg->list);
461 eloop_cancel_timeout(wpas_ctrl_msg_queue_timeout, wpa_s, NULL);
462 eloop_register_timeout(0, 0, wpas_ctrl_msg_queue_timeout, wpa_s, NULL);
466 static void wpas_ctrl_msg_queue_limit(unsigned int throttle_count,
467 struct dl_list *queue)
469 struct ctrl_iface_msg *msg;
471 if (throttle_count < 2000)
474 msg = dl_list_first(queue, struct ctrl_iface_msg, list);
476 wpa_printf(MSG_DEBUG, "CTRL: Dropped oldest pending message");
477 dl_list_del(&msg->list);
483 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
484 enum wpa_msg_type type,
485 const char *txt, size_t len)
487 struct wpa_supplicant *wpa_s = ctx;
488 struct ctrl_iface_priv *priv;
489 struct ctrl_iface_global_priv *gpriv;
494 gpriv = wpa_s->global->ctrl_iface;
496 if (type != WPA_MSG_NO_GLOBAL && gpriv &&
497 !dl_list_empty(&gpriv->ctrl_dst)) {
498 if (!dl_list_empty(&gpriv->msg_queue) ||
499 wpas_ctrl_iface_throttle(gpriv->sock)) {
500 if (gpriv->throttle_count == 0) {
501 wpa_printf(MSG_MSGDUMP,
502 "CTRL: Had to throttle global event message for sock %d",
505 gpriv->throttle_count++;
506 wpas_ctrl_msg_queue_limit(gpriv->throttle_count,
508 wpas_ctrl_msg_queue(&gpriv->msg_queue, wpa_s, level,
511 if (gpriv->throttle_count) {
512 wpa_printf(MSG_MSGDUMP,
513 "CTRL: Had to throttle %u global event message(s) for sock %d",
514 gpriv->throttle_count, gpriv->sock);
516 gpriv->throttle_count = 0;
517 wpa_supplicant_ctrl_iface_send(
519 type != WPA_MSG_PER_INTERFACE ?
520 NULL : wpa_s->ifname,
521 gpriv->sock, &gpriv->ctrl_dst, level,
522 txt, len, NULL, gpriv);
526 priv = wpa_s->ctrl_iface;
528 if (type != WPA_MSG_ONLY_GLOBAL && priv) {
529 if (!dl_list_empty(&priv->msg_queue) ||
530 wpas_ctrl_iface_throttle(priv->sock)) {
531 if (priv->throttle_count == 0) {
532 wpa_printf(MSG_MSGDUMP,
533 "CTRL: Had to throttle event message for sock %d",
536 priv->throttle_count++;
537 wpas_ctrl_msg_queue_limit(priv->throttle_count,
539 wpas_ctrl_msg_queue(&priv->msg_queue, wpa_s, level,
542 if (priv->throttle_count) {
543 wpa_printf(MSG_MSGDUMP,
544 "CTRL: Had to throttle %u event message(s) for sock %d",
545 priv->throttle_count, priv->sock);
547 priv->throttle_count = 0;
548 wpa_supplicant_ctrl_iface_send(wpa_s, NULL, priv->sock,
549 &priv->ctrl_dst, level,
550 txt, len, priv, NULL);
556 static int wpas_ctrl_iface_open_sock(struct wpa_supplicant *wpa_s,
557 struct ctrl_iface_priv *priv)
559 struct sockaddr_un addr;
563 char *buf, *dir = NULL, *gid_str = NULL;
568 buf = os_strdup(wpa_s->conf->ctrl_interface);
572 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
573 wpa_s->conf->ctrl_interface);
574 priv->sock = android_get_control_socket(addr.sun_path);
575 if (priv->sock >= 0) {
576 priv->android_control_socket = 1;
580 if (os_strncmp(buf, "DIR=", 4) == 0) {
582 gid_str = os_strstr(dir, " GROUP=");
589 gid_str = wpa_s->conf->ctrl_interface_group;
592 if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
593 if (errno == EEXIST) {
594 wpa_printf(MSG_DEBUG, "Using existing control "
595 "interface directory.");
597 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface=%s]: %s",
598 dir, strerror(errno));
605 * wpa_supplicant is started from /init.*.rc on Android and that seems
606 * to be using umask 0077 which would leave the control interface
607 * directory without group access. This breaks things since Wi-Fi
608 * framework assumes that this directory can be accessed by other
609 * applications in the wifi group. Fix this by adding group access even
610 * if umask value would prevent this.
612 if (chmod(dir, S_IRWXU | S_IRWXG) < 0) {
613 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
615 /* Try to continue anyway */
620 grp = getgrnam(gid_str);
624 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
625 " (from group name '%s')",
628 /* Group name not found - try to parse this as gid */
629 gid = strtol(gid_str, &endp, 10);
630 if (*gid_str == '\0' || *endp != '\0') {
631 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
636 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
641 if (gid_set && chown(dir, -1, gid) < 0) {
642 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
643 dir, (int) gid, strerror(errno));
647 /* Make sure the group can enter and read the directory */
649 chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
650 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
655 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
656 sizeof(addr.sun_path)) {
657 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
661 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
662 if (priv->sock < 0) {
663 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
667 os_memset(&addr, 0, sizeof(addr));
668 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
669 addr.sun_len = sizeof(addr);
670 #endif /* __FreeBSD__ */
671 addr.sun_family = AF_UNIX;
672 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
675 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
676 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
677 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
679 if (connect(priv->sock, (struct sockaddr *) &addr,
681 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
682 " allow connections - assuming it was left"
683 "over from forced program termination");
684 if (unlink(fname) < 0) {
685 wpa_printf(MSG_ERROR,
686 "Could not unlink existing ctrl_iface socket '%s': %s",
687 fname, strerror(errno));
690 if (bind(priv->sock, (struct sockaddr *) &addr,
692 wpa_printf(MSG_ERROR, "supp-ctrl-iface-init: bind(PF_UNIX): %s",
696 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
697 "ctrl_iface socket '%s'", fname);
699 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
700 "be in use - cannot override it");
701 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
702 "not used anymore", fname);
709 if (gid_set && chown(fname, -1, gid) < 0) {
710 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
711 fname, (int) gid, strerror(errno));
715 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
716 wpa_printf(MSG_ERROR, "chmod[ctrl_interface=%s]: %s",
717 fname, strerror(errno));
727 * Make socket non-blocking so that we don't hang forever if
728 * target dies unexpectedly.
730 flags = fcntl(priv->sock, F_GETFL);
733 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
734 wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
736 /* Not fatal, continue on.*/
740 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
742 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
748 if (priv->sock >= 0) {
761 struct ctrl_iface_priv *
762 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
764 struct ctrl_iface_priv *priv;
766 priv = os_zalloc(sizeof(*priv));
769 dl_list_init(&priv->ctrl_dst);
770 dl_list_init(&priv->msg_queue);
774 if (wpa_s->conf->ctrl_interface == NULL)
778 if (wpa_s->global->params.ctrl_interface) {
781 if (wpa_s->global->params.ctrl_interface[0] == '/') {
782 if (os_strcmp(wpa_s->global->params.ctrl_interface,
783 wpa_s->conf->ctrl_interface) == 0)
785 } else if (os_strncmp(wpa_s->global->params.ctrl_interface,
786 "@android:", 9) == 0 ||
787 os_strncmp(wpa_s->global->params.ctrl_interface,
788 "@abstract:", 10) == 0) {
792 * Currently, Android uses @android:wpa_* as the naming
793 * convention for the global ctrl interface. This logic
794 * needs to be revisited if the above naming convention
797 pos = os_strchr(wpa_s->global->params.ctrl_interface,
801 wpa_s->conf->ctrl_interface) == 0)
807 * The invalid configuration combination might be
808 * possible to hit in an Android OTA upgrade case, so
809 * instead of refusing to start the wpa_supplicant
810 * process, do not open the per-interface ctrl_iface
811 * and continue with the global control interface that
812 * was set from the command line since the Wi-Fi
813 * framework will use it for operations.
815 wpa_printf(MSG_ERROR,
816 "global ctrl interface %s matches ctrl interface %s - do not open per-interface ctrl interface",
817 wpa_s->global->params.ctrl_interface,
818 wpa_s->conf->ctrl_interface);
824 if (wpas_ctrl_iface_open_sock(wpa_s, priv) < 0) {
833 static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
834 struct ctrl_iface_priv *priv)
842 * On Android, the control socket being used may be the socket
843 * that is created when wpa_supplicant is started as a /init.*.rc
844 * service. Such a socket is maintained as a key-value pair in
845 * Android's environment. Closing this control socket would leave us
846 * in a bad state with an invalid socket descriptor.
848 if (priv->android_control_socket)
851 eloop_unregister_read_sock(priv->sock);
854 res = wpas_ctrl_iface_open_sock(wpa_s, priv);
861 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
863 struct wpa_ctrl_dst *dst, *prev;
864 struct ctrl_iface_msg *msg, *prev_msg;
865 struct ctrl_iface_global_priv *gpriv;
867 if (priv->sock > -1) {
869 char *buf, *dir = NULL;
870 eloop_unregister_read_sock(priv->sock);
871 if (!dl_list_empty(&priv->ctrl_dst)) {
873 * Wait before closing the control socket if
874 * there are any attached monitors in order to allow
875 * them to receive any pending messages.
877 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
878 "monitors to receive messages");
883 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
889 if (priv->wpa_s->conf->ctrl_interface == NULL)
891 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
894 if (os_strncmp(buf, "DIR=", 4) == 0) {
897 gid_str = os_strstr(dir, " GROUP=");
903 if (rmdir(dir) < 0) {
904 if (errno == ENOTEMPTY) {
905 wpa_printf(MSG_DEBUG, "Control interface "
906 "directory not empty - leaving it "
909 wpa_printf(MSG_ERROR,
910 "rmdir[ctrl_interface=%s]: %s",
911 dir, strerror(errno));
918 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
920 dl_list_del(&dst->list);
923 dl_list_for_each_safe(msg, prev_msg, &priv->msg_queue,
924 struct ctrl_iface_msg, list) {
925 dl_list_del(&msg->list);
928 gpriv = priv->wpa_s->global->ctrl_iface;
930 dl_list_for_each_safe(msg, prev_msg, &gpriv->msg_queue,
931 struct ctrl_iface_msg, list) {
932 if (msg->wpa_s == priv->wpa_s) {
933 dl_list_del(&msg->list);
938 eloop_cancel_timeout(wpas_ctrl_msg_queue_timeout, priv->wpa_s, NULL);
944 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
945 * @ifname: Interface name for global control socket or %NULL
946 * @sock: Local socket fd
947 * @ctrl_dst: List of attached listeners
948 * @level: Priority level of the message
950 * @len: Message length
952 * Send a packet to all monitor programs attached to the control interface.
954 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
955 const char *ifname, int sock,
956 struct dl_list *ctrl_dst,
957 int level, const char *buf,
959 struct ctrl_iface_priv *priv,
960 struct ctrl_iface_global_priv *gp)
962 struct wpa_ctrl_dst *dst, *next;
968 if (sock < 0 || dl_list_empty(ctrl_dst))
971 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
972 if (os_snprintf_error(sizeof(levelstr), res))
976 io[idx].iov_base = "IFNAME=";
979 io[idx].iov_base = (char *) ifname;
980 io[idx].iov_len = os_strlen(ifname);
982 io[idx].iov_base = " ";
986 io[idx].iov_base = levelstr;
987 io[idx].iov_len = os_strlen(levelstr);
989 io[idx].iov_base = (char *) buf;
990 io[idx].iov_len = len;
992 os_memset(&msg, 0, sizeof(msg));
994 msg.msg_iovlen = idx;
996 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
1000 if (level < dst->debug_level)
1003 printf_encode(addr_txt, sizeof(addr_txt),
1004 (u8 *) dst->addr.sun_path, dst->addrlen -
1005 offsetof(struct sockaddr_un, sun_path));
1006 msg.msg_name = (void *) &dst->addr;
1007 msg.msg_namelen = dst->addrlen;
1008 wpas_ctrl_sock_debug("ctrl_sock-sendmsg", sock, buf, len);
1009 if (sendmsg(sock, &msg, MSG_DONTWAIT) >= 0) {
1010 wpa_printf(MSG_MSGDUMP,
1011 "CTRL_IFACE monitor sent successfully to %s",
1018 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor[%s]: %d - %s",
1019 addr_txt, errno, strerror(errno));
1022 if (dst->errors > 10 || _errno == ENOENT || _errno == EPERM) {
1023 wpa_printf(MSG_INFO, "CTRL_IFACE: Detach monitor %s that cannot receive messages",
1025 wpa_supplicant_ctrl_iface_detach(ctrl_dst, &dst->addr,
1029 if (_errno == ENOBUFS || _errno == EAGAIN) {
1031 * The socket send buffer could be full. This may happen
1032 * if client programs are not receiving their pending
1033 * messages. Close and reopen the socket as a workaround
1034 * to avoid getting stuck being unable to send any new
1038 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
1040 sock = wpas_ctrl_iface_global_reinit(
1045 wpa_dbg(wpa_s, MSG_DEBUG,
1046 "Failed to reinitialize ctrl_iface socket");
1054 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
1058 struct sockaddr_un from;
1059 socklen_t fromlen = sizeof(from);
1062 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
1063 "attach", priv->wpa_s->ifname);
1064 eloop_wait_for_read_sock(priv->sock);
1066 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
1067 (struct sockaddr *) &from, &fromlen);
1069 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
1075 if (os_strcmp(buf, "ATTACH") == 0) {
1076 /* handle ATTACH signal of first monitor interface */
1077 if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
1080 if (sendto(priv->sock, "OK\n", 3, 0,
1081 (struct sockaddr *) &from, fromlen) <
1083 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
1086 /* OK to continue */
1089 if (sendto(priv->sock, "FAIL\n", 5, 0,
1090 (struct sockaddr *) &from, fromlen) <
1092 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
1097 /* return FAIL for all other signals */
1098 if (sendto(priv->sock, "FAIL\n", 5, 0,
1099 (struct sockaddr *) &from, fromlen) < 0) {
1100 wpa_printf(MSG_DEBUG,
1101 "ctrl_iface sendto failed: %s",
1109 /* Global ctrl_iface */
1111 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
1114 struct wpa_global *global = eloop_ctx;
1115 struct ctrl_iface_global_priv *priv = sock_ctx;
1118 struct sockaddr_un from;
1119 socklen_t fromlen = sizeof(from);
1120 char *reply = NULL, *reply_buf = NULL;
1123 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1124 (struct sockaddr *) &from, &fromlen);
1126 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
1132 if (os_strcmp(buf, "ATTACH") == 0) {
1133 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
1138 } else if (os_strcmp(buf, "DETACH") == 0) {
1139 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
1145 reply_buf = wpa_supplicant_global_ctrl_iface_process(
1146 global, buf, &reply_len);
1150 * There could be some password/key material in the command, so
1151 * clear the buffer explicitly now that it is not needed
1154 os_memset(buf, 0, res);
1157 if (!reply && reply_len == 1) {
1160 } else if (!reply && reply_len == 2) {
1166 wpas_ctrl_sock_debug("global_ctrl_sock-sendto",
1167 sock, reply, reply_len);
1168 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
1170 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
1178 static int wpas_global_ctrl_iface_open_sock(struct wpa_global *global,
1179 struct ctrl_iface_global_priv *priv)
1181 struct sockaddr_un addr;
1182 const char *ctrl = global->params.ctrl_interface;
1185 wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl);
1188 if (os_strncmp(ctrl, "@android:", 9) == 0) {
1189 priv->sock = android_get_control_socket(ctrl + 9);
1190 if (priv->sock < 0) {
1191 wpa_printf(MSG_ERROR, "Failed to open Android control "
1192 "socket '%s'", ctrl + 9);
1195 wpa_printf(MSG_DEBUG, "Using Android control socket '%s'",
1197 priv->android_control_socket = 1;
1201 if (os_strncmp(ctrl, "@abstract:", 10) != 0) {
1203 * Backwards compatibility - try to open an Android control
1204 * socket and if that fails, assume this was a UNIX domain
1207 priv->sock = android_get_control_socket(ctrl);
1208 if (priv->sock >= 0) {
1209 wpa_printf(MSG_DEBUG,
1210 "Using Android control socket '%s'",
1212 priv->android_control_socket = 1;
1216 #endif /* ANDROID */
1218 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
1219 if (priv->sock < 0) {
1220 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
1224 os_memset(&addr, 0, sizeof(addr));
1225 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1226 addr.sun_len = sizeof(addr);
1227 #endif /* __FreeBSD__ */
1228 addr.sun_family = AF_UNIX;
1230 if (os_strncmp(ctrl, "@abstract:", 10) == 0) {
1231 addr.sun_path[0] = '\0';
1232 os_strlcpy(addr.sun_path + 1, ctrl + 10,
1233 sizeof(addr.sun_path) - 1);
1234 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) <
1236 wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: "
1237 "bind(PF_UNIX;%s) failed: %s",
1238 ctrl, strerror(errno));
1241 wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'",
1246 os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path));
1247 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1248 wpa_printf(MSG_INFO, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s",
1249 ctrl, strerror(errno));
1250 if (connect(priv->sock, (struct sockaddr *) &addr,
1251 sizeof(addr)) < 0) {
1252 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1253 " allow connections - assuming it was left"
1254 "over from forced program termination");
1255 if (unlink(ctrl) < 0) {
1256 wpa_printf(MSG_ERROR,
1257 "Could not unlink existing ctrl_iface socket '%s': %s",
1258 ctrl, strerror(errno));
1261 if (bind(priv->sock, (struct sockaddr *) &addr,
1262 sizeof(addr)) < 0) {
1263 wpa_printf(MSG_ERROR, "supp-glb-iface-init: bind(PF_UNIX;%s): %s",
1264 ctrl, strerror(errno));
1267 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1268 "ctrl_iface socket '%s'",
1271 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1272 "be in use - cannot override it");
1273 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1280 wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl);
1282 if (global->params.ctrl_interface_group) {
1283 char *gid_str = global->params.ctrl_interface_group;
1288 grp = getgrnam(gid_str);
1291 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
1292 " (from group name '%s')",
1293 (int) gid, gid_str);
1295 /* Group name not found - try to parse this as gid */
1296 gid = strtol(gid_str, &endp, 10);
1297 if (*gid_str == '\0' || *endp != '\0') {
1298 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
1302 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
1305 if (chown(ctrl, -1, gid) < 0) {
1306 wpa_printf(MSG_ERROR,
1307 "chown[global_ctrl_interface=%s,gid=%d]: %s",
1308 ctrl, (int) gid, strerror(errno));
1312 if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) {
1313 wpa_printf(MSG_ERROR,
1314 "chmod[global_ctrl_interface=%s]: %s",
1315 ctrl, strerror(errno));
1319 if (chmod(ctrl, S_IRWXU) < 0) {
1320 wpa_printf(MSG_DEBUG,
1321 "chmod[global_ctrl_interface=%s](S_IRWXU): %s",
1322 ctrl, strerror(errno));
1323 /* continue anyway since group change was not required
1331 * Make socket non-blocking so that we don't hang forever if
1332 * target dies unexpectedly.
1334 flags = fcntl(priv->sock, F_GETFL);
1336 flags |= O_NONBLOCK;
1337 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
1338 wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
1340 /* Not fatal, continue on.*/
1344 eloop_register_read_sock(priv->sock,
1345 wpa_supplicant_global_ctrl_iface_receive,
1351 if (priv->sock >= 0) {
1359 struct ctrl_iface_global_priv *
1360 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
1362 struct ctrl_iface_global_priv *priv;
1364 priv = os_zalloc(sizeof(*priv));
1367 dl_list_init(&priv->ctrl_dst);
1368 dl_list_init(&priv->msg_queue);
1369 priv->global = global;
1372 if (global->params.ctrl_interface == NULL)
1375 if (wpas_global_ctrl_iface_open_sock(global, priv) < 0) {
1380 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
1386 static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
1387 struct ctrl_iface_global_priv *priv)
1391 if (priv->sock <= 0)
1395 * On Android, the control socket being used may be the socket
1396 * that is created when wpa_supplicant is started as a /init.*.rc
1397 * service. Such a socket is maintained as a key-value pair in
1398 * Android's environment. Closing this control socket would leave us
1399 * in a bad state with an invalid socket descriptor.
1401 if (priv->android_control_socket)
1404 eloop_unregister_read_sock(priv->sock);
1407 res = wpas_global_ctrl_iface_open_sock(global, priv);
1415 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
1417 struct wpa_ctrl_dst *dst, *prev;
1418 struct ctrl_iface_msg *msg, *prev_msg;
1420 if (priv->sock >= 0) {
1421 eloop_unregister_read_sock(priv->sock);
1424 if (priv->global->params.ctrl_interface)
1425 unlink(priv->global->params.ctrl_interface);
1426 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
1428 dl_list_del(&dst->list);
1431 dl_list_for_each_safe(msg, prev_msg, &priv->msg_queue,
1432 struct ctrl_iface_msg, list) {
1433 dl_list_del(&msg->list);