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 #endif /* __linux__ */
20 #include <cutils/sockets.h>
23 #include "utils/common.h"
24 #include "utils/eloop.h"
25 #include "utils/list.h"
26 #include "common/ctrl_iface_common.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 */
34 struct ctrl_iface_priv {
35 struct wpa_supplicant *wpa_s;
37 struct dl_list ctrl_dst;
38 int android_control_socket;
39 struct dl_list msg_queue;
40 unsigned int throttle_count;
44 struct ctrl_iface_global_priv {
45 struct wpa_global *global;
47 struct dl_list ctrl_dst;
48 int android_control_socket;
49 struct dl_list msg_queue;
50 unsigned int throttle_count;
53 struct ctrl_iface_msg {
55 struct wpa_supplicant *wpa_s;
57 enum wpa_msg_type type;
63 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
64 const char *ifname, int sock,
65 struct dl_list *ctrl_dst,
66 int level, const char *buf,
68 struct ctrl_iface_priv *priv,
69 struct ctrl_iface_global_priv *gp);
70 static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
71 struct ctrl_iface_priv *priv);
72 static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
73 struct ctrl_iface_global_priv *priv);
76 static void wpas_ctrl_sock_debug(const char *title, int sock, const char *buf,
82 int level = MSG_MSGDUMP;
84 if (len >= 5 && os_strncmp(buf, "PONG\n", 5) == 0)
85 level = MSG_EXCESSIVE;
87 optlen = sizeof(sndbuf);
89 if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) < 0)
92 if (ioctl(sock, TIOCOUTQ, &outq) < 0)
96 "CTRL-DEBUG: %s: sock=%d sndbuf=%d outq=%d send_len=%d",
97 title, sock, sndbuf, outq, (int) len);
98 #endif /* __linux__ */
102 static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst,
103 struct sockaddr_storage *from,
104 socklen_t fromlen, int global)
106 return ctrl_iface_attach(ctrl_dst, from, fromlen);
110 static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst,
111 struct sockaddr_storage *from,
114 return ctrl_iface_detach(ctrl_dst, from, fromlen);
118 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
119 struct sockaddr_storage *from,
123 wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
125 return ctrl_iface_level(&priv->ctrl_dst, from, fromlen, level);
129 static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
132 struct wpa_supplicant *wpa_s = eloop_ctx;
133 struct ctrl_iface_priv *priv = sock_ctx;
136 struct sockaddr_storage from;
137 socklen_t fromlen = sizeof(from);
138 char *reply = NULL, *reply_buf = NULL;
139 size_t reply_len = 0;
140 int new_attached = 0;
142 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
143 (struct sockaddr *) &from, &fromlen);
145 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
151 if (os_strcmp(buf, "ATTACH") == 0) {
152 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
159 } else if (os_strcmp(buf, "DETACH") == 0) {
160 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
165 } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
166 if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen,
172 reply_buf = wpa_supplicant_ctrl_iface_process(wpa_s, buf,
177 * There could be some password/key material in the command, so
178 * clear the buffer explicitly now that it is not needed
181 os_memset(buf, 0, res);
184 if (!reply && reply_len == 1) {
187 } else if (!reply && reply_len == 2) {
193 wpas_ctrl_sock_debug("ctrl_sock-sendto", sock, reply,
195 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
198 wpa_dbg(wpa_s, MSG_DEBUG,
199 "ctrl_iface sendto failed: %d - %s",
200 _errno, strerror(_errno));
201 if (_errno == ENOBUFS || _errno == EAGAIN) {
203 * The socket send buffer could be full. This
204 * may happen if client programs are not
205 * receiving their pending messages. Close and
206 * reopen the socket as a workaround to avoid
207 * getting stuck being unable to send any new
210 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
212 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to reinitialize ctrl_iface socket");
216 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to send response to ATTACH - detaching");
218 wpa_supplicant_ctrl_iface_detach(
219 &priv->ctrl_dst, &from, fromlen);
226 eapol_sm_notify_ctrl_attached(wpa_s->eapol);
230 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
234 char *pbuf, *dir = NULL;
237 if (wpa_s->conf->ctrl_interface == NULL)
240 pbuf = os_strdup(wpa_s->conf->ctrl_interface);
243 if (os_strncmp(pbuf, "DIR=", 4) == 0) {
246 gid_str = os_strstr(dir, " GROUP=");
252 len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
253 buf = os_malloc(len);
259 res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
260 if (os_snprintf_error(len, res)) {
267 /* Windows/WinPcap uses interface names that are not suitable
268 * as a file name - convert invalid chars to underscores */
276 #endif /* __CYGWIN__ */
282 static int wpas_ctrl_iface_throttle(int sock)
288 optlen = sizeof(sndbuf);
290 if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, &optlen) < 0 ||
291 ioctl(sock, TIOCOUTQ, &outq) < 0 ||
292 sndbuf <= 0 || outq < 0)
294 return outq > sndbuf / 2;
295 #else /* __linux__ */
297 #endif /* __linux__ */
301 static void wpas_ctrl_msg_send_pending_global(struct wpa_global *global)
303 struct ctrl_iface_global_priv *gpriv;
304 struct ctrl_iface_msg *msg;
306 gpriv = global->ctrl_iface;
307 while (gpriv && !dl_list_empty(&gpriv->msg_queue) &&
308 !wpas_ctrl_iface_throttle(gpriv->sock)) {
309 msg = dl_list_first(&gpriv->msg_queue, struct ctrl_iface_msg,
313 dl_list_del(&msg->list);
314 wpa_supplicant_ctrl_iface_send(
316 msg->type != WPA_MSG_PER_INTERFACE ?
317 NULL : msg->wpa_s->ifname,
318 gpriv->sock, &gpriv->ctrl_dst, msg->level,
319 msg->txt, msg->len, NULL, gpriv);
325 static void wpas_ctrl_msg_send_pending_iface(struct wpa_supplicant *wpa_s)
327 struct ctrl_iface_priv *priv;
328 struct ctrl_iface_msg *msg;
330 priv = wpa_s->ctrl_iface;
331 while (priv && !dl_list_empty(&priv->msg_queue) &&
332 !wpas_ctrl_iface_throttle(priv->sock)) {
333 msg = dl_list_first(&priv->msg_queue, struct ctrl_iface_msg,
337 dl_list_del(&msg->list);
338 wpa_supplicant_ctrl_iface_send(wpa_s, NULL, priv->sock,
339 &priv->ctrl_dst, msg->level,
340 msg->txt, msg->len, priv, NULL);
346 static void wpas_ctrl_msg_queue_timeout(void *eloop_ctx, void *timeout_ctx)
348 struct wpa_supplicant *wpa_s = eloop_ctx;
349 struct ctrl_iface_priv *priv;
350 struct ctrl_iface_global_priv *gpriv;
351 int sock = -1, gsock = -1;
353 wpas_ctrl_msg_send_pending_global(wpa_s->global);
354 wpas_ctrl_msg_send_pending_iface(wpa_s);
356 priv = wpa_s->ctrl_iface;
357 if (priv && !dl_list_empty(&priv->msg_queue))
360 gpriv = wpa_s->global->ctrl_iface;
361 if (gpriv && !dl_list_empty(&gpriv->msg_queue))
364 if (sock > -1 || gsock > -1) {
365 /* Continue pending message transmission from a timeout */
366 wpa_printf(MSG_MSGDUMP,
367 "CTRL: Had to throttle pending event message transmission for (sock %d gsock %d)",
369 eloop_register_timeout(0, 20000, wpas_ctrl_msg_queue_timeout,
375 static void wpas_ctrl_msg_queue(struct dl_list *queue,
376 struct wpa_supplicant *wpa_s, int level,
377 enum wpa_msg_type type,
378 const char *txt, size_t len)
380 struct ctrl_iface_msg *msg;
382 msg = os_zalloc(sizeof(*msg) + len);
389 os_memcpy(msg + 1, txt, len);
390 msg->txt = (const char *) (msg + 1);
392 dl_list_add_tail(queue, &msg->list);
393 eloop_cancel_timeout(wpas_ctrl_msg_queue_timeout, wpa_s, NULL);
394 eloop_register_timeout(0, 0, wpas_ctrl_msg_queue_timeout, wpa_s, NULL);
398 static void wpas_ctrl_msg_queue_limit(unsigned int throttle_count,
399 struct dl_list *queue)
401 struct ctrl_iface_msg *msg;
403 if (throttle_count < 2000)
406 msg = dl_list_first(queue, struct ctrl_iface_msg, list);
408 wpa_printf(MSG_DEBUG, "CTRL: Dropped oldest pending message");
409 dl_list_del(&msg->list);
415 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
416 enum wpa_msg_type type,
417 const char *txt, size_t len)
419 struct wpa_supplicant *wpa_s = ctx;
420 struct ctrl_iface_priv *priv;
421 struct ctrl_iface_global_priv *gpriv;
426 gpriv = wpa_s->global->ctrl_iface;
428 if (type != WPA_MSG_NO_GLOBAL && gpriv &&
429 !dl_list_empty(&gpriv->ctrl_dst)) {
430 if (!dl_list_empty(&gpriv->msg_queue) ||
431 wpas_ctrl_iface_throttle(gpriv->sock)) {
432 if (gpriv->throttle_count == 0) {
433 wpa_printf(MSG_MSGDUMP,
434 "CTRL: Had to throttle global event message for sock %d",
437 gpriv->throttle_count++;
438 wpas_ctrl_msg_queue_limit(gpriv->throttle_count,
440 wpas_ctrl_msg_queue(&gpriv->msg_queue, wpa_s, level,
443 if (gpriv->throttle_count) {
444 wpa_printf(MSG_MSGDUMP,
445 "CTRL: Had to throttle %u global event message(s) for sock %d",
446 gpriv->throttle_count, gpriv->sock);
448 gpriv->throttle_count = 0;
449 wpa_supplicant_ctrl_iface_send(
451 type != WPA_MSG_PER_INTERFACE ?
452 NULL : wpa_s->ifname,
453 gpriv->sock, &gpriv->ctrl_dst, level,
454 txt, len, NULL, gpriv);
458 priv = wpa_s->ctrl_iface;
460 if (type != WPA_MSG_ONLY_GLOBAL && priv) {
461 if (!dl_list_empty(&priv->msg_queue) ||
462 wpas_ctrl_iface_throttle(priv->sock)) {
463 if (priv->throttle_count == 0) {
464 wpa_printf(MSG_MSGDUMP,
465 "CTRL: Had to throttle event message for sock %d",
468 priv->throttle_count++;
469 wpas_ctrl_msg_queue_limit(priv->throttle_count,
471 wpas_ctrl_msg_queue(&priv->msg_queue, wpa_s, level,
474 if (priv->throttle_count) {
475 wpa_printf(MSG_MSGDUMP,
476 "CTRL: Had to throttle %u event message(s) for sock %d",
477 priv->throttle_count, priv->sock);
479 priv->throttle_count = 0;
480 wpa_supplicant_ctrl_iface_send(wpa_s, NULL, priv->sock,
481 &priv->ctrl_dst, level,
482 txt, len, priv, NULL);
488 static int wpas_ctrl_iface_open_sock(struct wpa_supplicant *wpa_s,
489 struct ctrl_iface_priv *priv)
491 struct sockaddr_un addr;
495 char *buf, *dir = NULL, *gid_str = NULL;
500 buf = os_strdup(wpa_s->conf->ctrl_interface);
504 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "wpa_%s",
505 wpa_s->conf->ctrl_interface);
506 priv->sock = android_get_control_socket(addr.sun_path);
507 if (priv->sock >= 0) {
508 priv->android_control_socket = 1;
512 if (os_strncmp(buf, "DIR=", 4) == 0) {
514 gid_str = os_strstr(dir, " GROUP=");
521 gid_str = wpa_s->conf->ctrl_interface_group;
524 if (mkdir(dir, S_IRWXU | S_IRWXG) < 0) {
525 if (errno == EEXIST) {
526 wpa_printf(MSG_DEBUG, "Using existing control "
527 "interface directory.");
529 wpa_printf(MSG_ERROR, "mkdir[ctrl_interface=%s]: %s",
530 dir, strerror(errno));
537 * wpa_supplicant is started from /init.*.rc on Android and that seems
538 * to be using umask 0077 which would leave the control interface
539 * directory without group access. This breaks things since Wi-Fi
540 * framework assumes that this directory can be accessed by other
541 * applications in the wifi group. Fix this by adding group access even
542 * if umask value would prevent this.
544 if (chmod(dir, S_IRWXU | S_IRWXG) < 0) {
545 wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
547 /* Try to continue anyway */
552 grp = getgrnam(gid_str);
556 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
557 " (from group name '%s')",
560 /* Group name not found - try to parse this as gid */
561 gid = strtol(gid_str, &endp, 10);
562 if (*gid_str == '\0' || *endp != '\0') {
563 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
568 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
573 if (gid_set && chown(dir, -1, gid) < 0) {
574 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
575 dir, (int) gid, strerror(errno));
579 /* Make sure the group can enter and read the directory */
581 chmod(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP) < 0) {
582 wpa_printf(MSG_ERROR, "CTRL: chmod[ctrl_interface]: %s",
587 if (os_strlen(dir) + 1 + os_strlen(wpa_s->ifname) >=
588 sizeof(addr.sun_path)) {
589 wpa_printf(MSG_ERROR, "ctrl_iface path limit exceeded");
593 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
594 if (priv->sock < 0) {
595 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
599 os_memset(&addr, 0, sizeof(addr));
600 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
601 addr.sun_len = sizeof(addr);
602 #endif /* __FreeBSD__ */
603 addr.sun_family = AF_UNIX;
604 fname = wpa_supplicant_ctrl_iface_path(wpa_s);
607 os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
608 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
609 wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
611 if (connect(priv->sock, (struct sockaddr *) &addr,
613 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
614 " allow connections - assuming it was left"
615 "over from forced program termination");
616 if (unlink(fname) < 0) {
617 wpa_printf(MSG_ERROR,
618 "Could not unlink existing ctrl_iface socket '%s': %s",
619 fname, strerror(errno));
622 if (bind(priv->sock, (struct sockaddr *) &addr,
624 wpa_printf(MSG_ERROR, "supp-ctrl-iface-init: bind(PF_UNIX): %s",
628 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
629 "ctrl_iface socket '%s'", fname);
631 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
632 "be in use - cannot override it");
633 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
634 "not used anymore", fname);
641 if (gid_set && chown(fname, -1, gid) < 0) {
642 wpa_printf(MSG_ERROR, "chown[ctrl_interface=%s,gid=%d]: %s",
643 fname, (int) gid, strerror(errno));
647 if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
648 wpa_printf(MSG_ERROR, "chmod[ctrl_interface=%s]: %s",
649 fname, strerror(errno));
659 * Make socket non-blocking so that we don't hang forever if
660 * target dies unexpectedly.
662 flags = fcntl(priv->sock, F_GETFL);
665 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
666 wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
668 /* Not fatal, continue on.*/
672 eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive,
674 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
680 if (priv->sock >= 0) {
693 struct ctrl_iface_priv *
694 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
696 struct ctrl_iface_priv *priv;
698 priv = os_zalloc(sizeof(*priv));
701 dl_list_init(&priv->ctrl_dst);
702 dl_list_init(&priv->msg_queue);
706 if (wpa_s->conf->ctrl_interface == NULL)
710 if (wpa_s->global->params.ctrl_interface) {
713 if (wpa_s->global->params.ctrl_interface[0] == '/') {
714 if (os_strcmp(wpa_s->global->params.ctrl_interface,
715 wpa_s->conf->ctrl_interface) == 0)
717 } else if (os_strncmp(wpa_s->global->params.ctrl_interface,
718 "@android:", 9) == 0 ||
719 os_strncmp(wpa_s->global->params.ctrl_interface,
720 "@abstract:", 10) == 0) {
724 * Currently, Android uses @android:wpa_* as the naming
725 * convention for the global ctrl interface. This logic
726 * needs to be revisited if the above naming convention
729 pos = os_strchr(wpa_s->global->params.ctrl_interface,
733 wpa_s->conf->ctrl_interface) == 0)
739 * The invalid configuration combination might be
740 * possible to hit in an Android OTA upgrade case, so
741 * instead of refusing to start the wpa_supplicant
742 * process, do not open the per-interface ctrl_iface
743 * and continue with the global control interface that
744 * was set from the command line since the Wi-Fi
745 * framework will use it for operations.
747 wpa_printf(MSG_ERROR,
748 "global ctrl interface %s matches ctrl interface %s - do not open per-interface ctrl interface",
749 wpa_s->global->params.ctrl_interface,
750 wpa_s->conf->ctrl_interface);
756 if (wpas_ctrl_iface_open_sock(wpa_s, priv) < 0) {
765 static int wpas_ctrl_iface_reinit(struct wpa_supplicant *wpa_s,
766 struct ctrl_iface_priv *priv)
774 * On Android, the control socket being used may be the socket
775 * that is created when wpa_supplicant is started as a /init.*.rc
776 * service. Such a socket is maintained as a key-value pair in
777 * Android's environment. Closing this control socket would leave us
778 * in a bad state with an invalid socket descriptor.
780 if (priv->android_control_socket)
783 eloop_unregister_read_sock(priv->sock);
786 res = wpas_ctrl_iface_open_sock(wpa_s, priv);
793 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
795 struct wpa_ctrl_dst *dst, *prev;
796 struct ctrl_iface_msg *msg, *prev_msg;
797 struct ctrl_iface_global_priv *gpriv;
799 if (priv->sock > -1) {
801 char *buf, *dir = NULL;
802 eloop_unregister_read_sock(priv->sock);
803 if (!dl_list_empty(&priv->ctrl_dst)) {
805 * Wait before closing the control socket if
806 * there are any attached monitors in order to allow
807 * them to receive any pending messages.
809 wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached "
810 "monitors to receive messages");
815 fname = wpa_supplicant_ctrl_iface_path(priv->wpa_s);
821 if (priv->wpa_s->conf->ctrl_interface == NULL)
823 buf = os_strdup(priv->wpa_s->conf->ctrl_interface);
826 if (os_strncmp(buf, "DIR=", 4) == 0) {
829 gid_str = os_strstr(dir, " GROUP=");
835 if (rmdir(dir) < 0) {
836 if (errno == ENOTEMPTY) {
837 wpa_printf(MSG_DEBUG, "Control interface "
838 "directory not empty - leaving it "
841 wpa_printf(MSG_ERROR,
842 "rmdir[ctrl_interface=%s]: %s",
843 dir, strerror(errno));
850 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
852 dl_list_del(&dst->list);
855 dl_list_for_each_safe(msg, prev_msg, &priv->msg_queue,
856 struct ctrl_iface_msg, list) {
857 dl_list_del(&msg->list);
860 gpriv = priv->wpa_s->global->ctrl_iface;
862 dl_list_for_each_safe(msg, prev_msg, &gpriv->msg_queue,
863 struct ctrl_iface_msg, list) {
864 if (msg->wpa_s == priv->wpa_s) {
865 dl_list_del(&msg->list);
870 eloop_cancel_timeout(wpas_ctrl_msg_queue_timeout, priv->wpa_s, NULL);
876 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
877 * @ifname: Interface name for global control socket or %NULL
878 * @sock: Local socket fd
879 * @ctrl_dst: List of attached listeners
880 * @level: Priority level of the message
882 * @len: Message length
884 * Send a packet to all monitor programs attached to the control interface.
886 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
887 const char *ifname, int sock,
888 struct dl_list *ctrl_dst,
889 int level, const char *buf,
891 struct ctrl_iface_priv *priv,
892 struct ctrl_iface_global_priv *gp)
894 struct wpa_ctrl_dst *dst, *next;
900 if (sock < 0 || dl_list_empty(ctrl_dst))
903 res = os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
904 if (os_snprintf_error(sizeof(levelstr), res))
908 io[idx].iov_base = "IFNAME=";
911 io[idx].iov_base = (char *) ifname;
912 io[idx].iov_len = os_strlen(ifname);
914 io[idx].iov_base = " ";
918 io[idx].iov_base = levelstr;
919 io[idx].iov_len = os_strlen(levelstr);
921 io[idx].iov_base = (char *) buf;
922 io[idx].iov_len = len;
924 os_memset(&msg, 0, sizeof(msg));
926 msg.msg_iovlen = idx;
928 dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
932 if (level < dst->debug_level)
935 msg.msg_name = (void *) &dst->addr;
936 msg.msg_namelen = dst->addrlen;
937 wpas_ctrl_sock_debug("ctrl_sock-sendmsg", sock, buf, len);
938 if (sendmsg(sock, &msg, MSG_DONTWAIT) >= 0) {
939 sockaddr_print(MSG_MSGDUMP,
940 "CTRL_IFACE monitor sent successfully to",
941 &dst->addr, dst->addrlen);
947 os_snprintf(txt, sizeof(txt), "CTRL_IFACE monitor: %d (%s) for",
948 _errno, strerror(_errno));
949 sockaddr_print(MSG_DEBUG, txt, &dst->addr, dst->addrlen);
952 if (dst->errors > 10 || _errno == ENOENT || _errno == EPERM) {
953 sockaddr_print(MSG_INFO, "CTRL_IFACE: Detach monitor that cannot receive messages:",
954 &dst->addr, dst->addrlen);
955 wpa_supplicant_ctrl_iface_detach(ctrl_dst, &dst->addr,
959 if (_errno == ENOBUFS || _errno == EAGAIN) {
961 * The socket send buffer could be full. This may happen
962 * if client programs are not receiving their pending
963 * messages. Close and reopen the socket as a workaround
964 * to avoid getting stuck being unable to send any new
968 sock = wpas_ctrl_iface_reinit(wpa_s, priv);
970 sock = wpas_ctrl_iface_global_reinit(
975 wpa_dbg(wpa_s, MSG_DEBUG,
976 "Failed to reinitialize ctrl_iface socket");
984 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
988 struct sockaddr_storage from;
989 socklen_t fromlen = sizeof(from);
991 if (priv->sock == -1)
995 wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to "
996 "attach", priv->wpa_s->ifname);
997 eloop_wait_for_read_sock(priv->sock);
999 res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0,
1000 (struct sockaddr *) &from, &fromlen);
1002 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
1008 if (os_strcmp(buf, "ATTACH") == 0) {
1009 /* handle ATTACH signal of first monitor interface */
1010 if (!wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst,
1013 if (sendto(priv->sock, "OK\n", 3, 0,
1014 (struct sockaddr *) &from, fromlen) <
1016 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
1019 /* OK to continue */
1022 if (sendto(priv->sock, "FAIL\n", 5, 0,
1023 (struct sockaddr *) &from, fromlen) <
1025 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
1030 /* return FAIL for all other signals */
1031 if (sendto(priv->sock, "FAIL\n", 5, 0,
1032 (struct sockaddr *) &from, fromlen) < 0) {
1033 wpa_printf(MSG_DEBUG,
1034 "ctrl_iface sendto failed: %s",
1042 /* Global ctrl_iface */
1044 static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx,
1047 struct wpa_global *global = eloop_ctx;
1048 struct ctrl_iface_global_priv *priv = sock_ctx;
1051 struct sockaddr_storage from;
1052 socklen_t fromlen = sizeof(from);
1053 char *reply = NULL, *reply_buf = NULL;
1056 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
1057 (struct sockaddr *) &from, &fromlen);
1059 wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s",
1065 if (os_strcmp(buf, "ATTACH") == 0) {
1066 if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from,
1071 } else if (os_strcmp(buf, "DETACH") == 0) {
1072 if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from,
1078 reply_buf = wpa_supplicant_global_ctrl_iface_process(
1079 global, buf, &reply_len);
1083 * There could be some password/key material in the command, so
1084 * clear the buffer explicitly now that it is not needed
1087 os_memset(buf, 0, res);
1090 if (!reply && reply_len == 1) {
1093 } else if (!reply && reply_len == 2) {
1099 wpas_ctrl_sock_debug("global_ctrl_sock-sendto",
1100 sock, reply, reply_len);
1101 if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from,
1103 wpa_printf(MSG_DEBUG, "ctrl_iface sendto failed: %s",
1111 static int wpas_global_ctrl_iface_open_sock(struct wpa_global *global,
1112 struct ctrl_iface_global_priv *priv)
1114 struct sockaddr_un addr;
1115 const char *ctrl = global->params.ctrl_interface;
1118 wpa_printf(MSG_DEBUG, "Global control interface '%s'", ctrl);
1121 if (os_strncmp(ctrl, "@android:", 9) == 0) {
1122 priv->sock = android_get_control_socket(ctrl + 9);
1123 if (priv->sock < 0) {
1124 wpa_printf(MSG_ERROR, "Failed to open Android control "
1125 "socket '%s'", ctrl + 9);
1128 wpa_printf(MSG_DEBUG, "Using Android control socket '%s'",
1130 priv->android_control_socket = 1;
1134 if (os_strncmp(ctrl, "@abstract:", 10) != 0) {
1136 * Backwards compatibility - try to open an Android control
1137 * socket and if that fails, assume this was a UNIX domain
1140 priv->sock = android_get_control_socket(ctrl);
1141 if (priv->sock >= 0) {
1142 wpa_printf(MSG_DEBUG,
1143 "Using Android control socket '%s'",
1145 priv->android_control_socket = 1;
1149 #endif /* ANDROID */
1151 priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
1152 if (priv->sock < 0) {
1153 wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
1157 os_memset(&addr, 0, sizeof(addr));
1158 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
1159 addr.sun_len = sizeof(addr);
1160 #endif /* __FreeBSD__ */
1161 addr.sun_family = AF_UNIX;
1163 if (os_strncmp(ctrl, "@abstract:", 10) == 0) {
1164 addr.sun_path[0] = '\0';
1165 os_strlcpy(addr.sun_path + 1, ctrl + 10,
1166 sizeof(addr.sun_path) - 1);
1167 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) <
1169 wpa_printf(MSG_ERROR, "supp-global-ctrl-iface-init: "
1170 "bind(PF_UNIX;%s) failed: %s",
1171 ctrl, strerror(errno));
1174 wpa_printf(MSG_DEBUG, "Using Abstract control socket '%s'",
1179 os_strlcpy(addr.sun_path, ctrl, sizeof(addr.sun_path));
1180 if (bind(priv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1181 wpa_printf(MSG_INFO, "supp-global-ctrl-iface-init(%s) (will try fixup): bind(PF_UNIX): %s",
1182 ctrl, strerror(errno));
1183 if (connect(priv->sock, (struct sockaddr *) &addr,
1184 sizeof(addr)) < 0) {
1185 wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
1186 " allow connections - assuming it was left"
1187 "over from forced program termination");
1188 if (unlink(ctrl) < 0) {
1189 wpa_printf(MSG_ERROR,
1190 "Could not unlink existing ctrl_iface socket '%s': %s",
1191 ctrl, strerror(errno));
1194 if (bind(priv->sock, (struct sockaddr *) &addr,
1195 sizeof(addr)) < 0) {
1196 wpa_printf(MSG_ERROR, "supp-glb-iface-init: bind(PF_UNIX;%s): %s",
1197 ctrl, strerror(errno));
1200 wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
1201 "ctrl_iface socket '%s'",
1204 wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
1205 "be in use - cannot override it");
1206 wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
1213 wpa_printf(MSG_DEBUG, "Using UNIX control socket '%s'", ctrl);
1215 if (global->params.ctrl_interface_group) {
1216 char *gid_str = global->params.ctrl_interface_group;
1221 grp = getgrnam(gid_str);
1224 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d"
1225 " (from group name '%s')",
1226 (int) gid, gid_str);
1228 /* Group name not found - try to parse this as gid */
1229 gid = strtol(gid_str, &endp, 10);
1230 if (*gid_str == '\0' || *endp != '\0') {
1231 wpa_printf(MSG_ERROR, "CTRL: Invalid group "
1235 wpa_printf(MSG_DEBUG, "ctrl_interface_group=%d",
1238 if (chown(ctrl, -1, gid) < 0) {
1239 wpa_printf(MSG_ERROR,
1240 "chown[global_ctrl_interface=%s,gid=%d]: %s",
1241 ctrl, (int) gid, strerror(errno));
1245 if (chmod(ctrl, S_IRWXU | S_IRWXG) < 0) {
1246 wpa_printf(MSG_ERROR,
1247 "chmod[global_ctrl_interface=%s]: %s",
1248 ctrl, strerror(errno));
1252 if (chmod(ctrl, S_IRWXU) < 0) {
1253 wpa_printf(MSG_DEBUG,
1254 "chmod[global_ctrl_interface=%s](S_IRWXU): %s",
1255 ctrl, strerror(errno));
1256 /* continue anyway since group change was not required
1264 * Make socket non-blocking so that we don't hang forever if
1265 * target dies unexpectedly.
1267 flags = fcntl(priv->sock, F_GETFL);
1269 flags |= O_NONBLOCK;
1270 if (fcntl(priv->sock, F_SETFL, flags) < 0) {
1271 wpa_printf(MSG_INFO, "fcntl(ctrl, O_NONBLOCK): %s",
1273 /* Not fatal, continue on.*/
1277 eloop_register_read_sock(priv->sock,
1278 wpa_supplicant_global_ctrl_iface_receive,
1284 if (priv->sock >= 0) {
1292 struct ctrl_iface_global_priv *
1293 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
1295 struct ctrl_iface_global_priv *priv;
1297 priv = os_zalloc(sizeof(*priv));
1300 dl_list_init(&priv->ctrl_dst);
1301 dl_list_init(&priv->msg_queue);
1302 priv->global = global;
1305 if (global->params.ctrl_interface == NULL)
1308 if (wpas_global_ctrl_iface_open_sock(global, priv) < 0) {
1313 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb);
1319 static int wpas_ctrl_iface_global_reinit(struct wpa_global *global,
1320 struct ctrl_iface_global_priv *priv)
1324 if (priv->sock <= 0)
1328 * On Android, the control socket being used may be the socket
1329 * that is created when wpa_supplicant is started as a /init.*.rc
1330 * service. Such a socket is maintained as a key-value pair in
1331 * Android's environment. Closing this control socket would leave us
1332 * in a bad state with an invalid socket descriptor.
1334 if (priv->android_control_socket)
1337 eloop_unregister_read_sock(priv->sock);
1340 res = wpas_global_ctrl_iface_open_sock(global, priv);
1348 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
1350 struct wpa_ctrl_dst *dst, *prev;
1351 struct ctrl_iface_msg *msg, *prev_msg;
1353 if (priv->sock >= 0) {
1354 eloop_unregister_read_sock(priv->sock);
1357 if (priv->global->params.ctrl_interface)
1358 unlink(priv->global->params.ctrl_interface);
1359 dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst,
1361 dl_list_del(&dst->list);
1364 dl_list_for_each_safe(msg, prev_msg, &priv->msg_queue,
1365 struct ctrl_iface_msg, list) {
1366 dl_list_del(&msg->list);