2 * WPA Supplicant / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
21 #include "utils/common.h"
22 #include "utils/eloop.h"
23 #include "utils/list.h"
24 #include "eapol_supp/eapol_supp_sm.h"
26 #include "wpa_supplicant_i.h"
27 #include "ctrl_iface.h"
29 /* Per-interface ctrl_iface */
32 * struct wpa_ctrl_dst - Internal data structure of control interface monitors
34 * This structure is used to store information about registered control
35 * interface monitors into struct wpa_supplicant. This data is private to
36 * ctrl_iface_unix.c and should not be touched directly from other files.
40 struct sockaddr_un addr
;
47 struct ctrl_iface_priv
{
48 struct wpa_supplicant
*wpa_s
;
50 struct dl_list ctrl_dst
;
54 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv
*priv
,
55 int level
, const char *buf
,
59 static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv
*priv
,
60 struct sockaddr_un
*from
,
63 struct wpa_ctrl_dst
*dst
;
65 dst
= os_zalloc(sizeof(*dst
));
68 os_memcpy(&dst
->addr
, from
, sizeof(struct sockaddr_un
));
69 dst
->addrlen
= fromlen
;
70 dst
->debug_level
= MSG_INFO
;
71 dl_list_add(&priv
->ctrl_dst
, &dst
->list
);
72 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor attached",
73 (u8
*) from
->sun_path
,
74 fromlen
- offsetof(struct sockaddr_un
, sun_path
));
79 static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv
*priv
,
80 struct sockaddr_un
*from
,
83 struct wpa_ctrl_dst
*dst
;
85 dl_list_for_each(dst
, &priv
->ctrl_dst
, struct wpa_ctrl_dst
, list
) {
86 if (fromlen
== dst
->addrlen
&&
87 os_memcmp(from
->sun_path
, dst
->addr
.sun_path
,
88 fromlen
- offsetof(struct sockaddr_un
, sun_path
))
90 dl_list_del(&dst
->list
);
92 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor detached",
93 (u8
*) from
->sun_path
,
95 offsetof(struct sockaddr_un
, sun_path
));
103 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv
*priv
,
104 struct sockaddr_un
*from
,
108 struct wpa_ctrl_dst
*dst
;
110 wpa_printf(MSG_DEBUG
, "CTRL_IFACE LEVEL %s", level
);
112 dl_list_for_each(dst
, &priv
->ctrl_dst
, struct wpa_ctrl_dst
, list
) {
113 if (fromlen
== dst
->addrlen
&&
114 os_memcmp(from
->sun_path
, dst
->addr
.sun_path
,
115 fromlen
- offsetof(struct sockaddr_un
, sun_path
))
117 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE changed monitor "
118 "level", (u8
*) from
->sun_path
,
120 offsetof(struct sockaddr_un
, sun_path
));
121 dst
->debug_level
= atoi(level
);
130 static void wpa_supplicant_ctrl_iface_receive(int sock
, void *eloop_ctx
,
133 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
134 struct ctrl_iface_priv
*priv
= sock_ctx
;
137 struct sockaddr_un from
;
138 socklen_t fromlen
= sizeof(from
);
140 size_t reply_len
= 0;
141 int new_attached
= 0;
143 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
144 (struct sockaddr
*) &from
, &fromlen
);
146 perror("recvfrom(ctrl_iface)");
151 if (os_strcmp(buf
, "ATTACH") == 0) {
152 if (wpa_supplicant_ctrl_iface_attach(priv
, &from
, fromlen
))
158 } else if (os_strcmp(buf
, "DETACH") == 0) {
159 if (wpa_supplicant_ctrl_iface_detach(priv
, &from
, fromlen
))
163 } else if (os_strncmp(buf
, "LEVEL ", 6) == 0) {
164 if (wpa_supplicant_ctrl_iface_level(priv
, &from
, fromlen
,
170 reply
= wpa_supplicant_ctrl_iface_process(wpa_s
, buf
,
175 sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
178 } else if (reply_len
== 1) {
179 sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
181 } else if (reply_len
== 2) {
182 sendto(sock
, "OK\n", 3, 0, (struct sockaddr
*) &from
,
187 eapol_sm_notify_ctrl_attached(wpa_s
->eapol
);
191 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant
*wpa_s
)
195 char *pbuf
, *dir
= NULL
, *gid_str
= NULL
;
198 if (wpa_s
->conf
->ctrl_interface
== NULL
)
201 pbuf
= os_strdup(wpa_s
->conf
->ctrl_interface
);
204 if (os_strncmp(pbuf
, "DIR=", 4) == 0) {
206 gid_str
= os_strstr(dir
, " GROUP=");
214 len
= os_strlen(dir
) + os_strlen(wpa_s
->ifname
) + 2;
215 buf
= os_malloc(len
);
221 res
= os_snprintf(buf
, len
, "%s/%s", dir
, wpa_s
->ifname
);
222 if (res
< 0 || (size_t) res
>= len
) {
229 /* Windows/WinPcap uses interface names that are not suitable
230 * as a file name - convert invalid chars to underscores */
238 #endif /* __CYGWIN__ */
244 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx
, int level
,
245 const char *txt
, size_t len
)
247 struct wpa_supplicant
*wpa_s
= ctx
;
248 if (wpa_s
== NULL
|| wpa_s
->ctrl_iface
== NULL
)
250 wpa_supplicant_ctrl_iface_send(wpa_s
->ctrl_iface
, level
, txt
, len
);
254 struct ctrl_iface_priv
*
255 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant
*wpa_s
)
257 struct ctrl_iface_priv
*priv
;
258 struct sockaddr_un addr
;
262 char *buf
, *dir
= NULL
, *gid_str
= NULL
;
266 priv
= os_zalloc(sizeof(*priv
));
269 dl_list_init(&priv
->ctrl_dst
);
273 if (wpa_s
->conf
->ctrl_interface
== NULL
)
276 buf
= os_strdup(wpa_s
->conf
->ctrl_interface
);
279 if (os_strncmp(buf
, "DIR=", 4) == 0) {
281 gid_str
= os_strstr(dir
, " GROUP=");
288 gid_str
= wpa_s
->conf
->ctrl_interface_group
;
291 if (mkdir(dir
, S_IRWXU
| S_IRWXG
) < 0) {
292 if (errno
== EEXIST
) {
293 wpa_printf(MSG_DEBUG
, "Using existing control "
294 "interface directory.");
296 perror("mkdir[ctrl_interface]");
302 grp
= getgrnam(gid_str
);
306 wpa_printf(MSG_DEBUG
, "ctrl_interface_group=%d"
307 " (from group name '%s')",
310 /* Group name not found - try to parse this as gid */
311 gid
= strtol(gid_str
, &endp
, 10);
312 if (*gid_str
== '\0' || *endp
!= '\0') {
313 wpa_printf(MSG_ERROR
, "CTRL: Invalid group "
318 wpa_printf(MSG_DEBUG
, "ctrl_interface_group=%d",
323 if (gid_set
&& chown(dir
, -1, gid
) < 0) {
324 perror("chown[ctrl_interface]");
328 /* Make sure the group can enter and read the directory */
330 chmod(dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
) < 0) {
331 wpa_printf(MSG_ERROR
, "CTRL: chmod[ctrl_interface]: %s",
336 if (os_strlen(dir
) + 1 + os_strlen(wpa_s
->ifname
) >=
337 sizeof(addr
.sun_path
)) {
338 wpa_printf(MSG_ERROR
, "ctrl_iface path limit exceeded");
342 priv
->sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
343 if (priv
->sock
< 0) {
344 perror("socket(PF_UNIX)");
348 os_memset(&addr
, 0, sizeof(addr
));
349 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
350 addr
.sun_len
= sizeof(addr
);
351 #endif /* __FreeBSD__ */
352 addr
.sun_family
= AF_UNIX
;
353 fname
= wpa_supplicant_ctrl_iface_path(wpa_s
);
356 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
357 if (bind(priv
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
358 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
360 if (connect(priv
->sock
, (struct sockaddr
*) &addr
,
362 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
363 " allow connections - assuming it was left"
364 "over from forced program termination");
365 if (unlink(fname
) < 0) {
366 perror("unlink[ctrl_iface]");
367 wpa_printf(MSG_ERROR
, "Could not unlink "
368 "existing ctrl_iface socket '%s'",
372 if (bind(priv
->sock
, (struct sockaddr
*) &addr
,
374 perror("bind(PF_UNIX)");
377 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
378 "ctrl_iface socket '%s'", fname
);
380 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
381 "be in use - cannot override it");
382 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
383 "not used anymore", fname
);
390 if (gid_set
&& chown(fname
, -1, gid
) < 0) {
391 perror("chown[ctrl_interface/ifname]");
395 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
396 perror("chmod[ctrl_interface/ifname]");
401 eloop_register_read_sock(priv
->sock
, wpa_supplicant_ctrl_iface_receive
,
403 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb
);
421 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv
*priv
)
423 struct wpa_ctrl_dst
*dst
, *prev
;
425 if (priv
->sock
> -1) {
427 char *buf
, *dir
= NULL
, *gid_str
= NULL
;
428 eloop_unregister_read_sock(priv
->sock
);
429 if (!dl_list_empty(&priv
->ctrl_dst
)) {
431 * Wait a second before closing the control socket if
432 * there are any attached monitors in order to allow
433 * them to receive any pending messages.
435 wpa_printf(MSG_DEBUG
, "CTRL_IFACE wait for attached "
436 "monitors to receive messages");
441 fname
= wpa_supplicant_ctrl_iface_path(priv
->wpa_s
);
447 buf
= os_strdup(priv
->wpa_s
->conf
->ctrl_interface
);
450 if (os_strncmp(buf
, "DIR=", 4) == 0) {
452 gid_str
= os_strstr(dir
, " GROUP=");
460 if (rmdir(dir
) < 0) {
461 if (errno
== ENOTEMPTY
) {
462 wpa_printf(MSG_DEBUG
, "Control interface "
463 "directory not empty - leaving it "
466 perror("rmdir[ctrl_interface]");
473 dl_list_for_each_safe(dst
, prev
, &priv
->ctrl_dst
, struct wpa_ctrl_dst
,
481 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
482 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
483 * @level: Priority level of the message
485 * @len: Message length
487 * Send a packet to all monitor programs attached to the control interface.
489 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv
*priv
,
490 int level
, const char *buf
,
493 struct wpa_ctrl_dst
*dst
, *next
;
499 if (priv
->sock
< 0 || dl_list_empty(&priv
->ctrl_dst
))
502 res
= os_snprintf(levelstr
, sizeof(levelstr
), "<%d>", level
);
503 if (res
< 0 || (size_t) res
>= sizeof(levelstr
))
505 io
[0].iov_base
= levelstr
;
506 io
[0].iov_len
= os_strlen(levelstr
);
507 io
[1].iov_base
= (char *) buf
;
509 os_memset(&msg
, 0, sizeof(msg
));
514 dl_list_for_each_safe(dst
, next
, &priv
->ctrl_dst
, struct wpa_ctrl_dst
,
516 if (level
>= dst
->debug_level
) {
517 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor send",
518 (u8
*) dst
->addr
.sun_path
, dst
->addrlen
-
519 offsetof(struct sockaddr_un
, sun_path
));
520 msg
.msg_name
= (void *) &dst
->addr
;
521 msg
.msg_namelen
= dst
->addrlen
;
522 if (sendmsg(priv
->sock
, &msg
, 0) < 0) {
524 wpa_printf(MSG_INFO
, "CTRL_IFACE monitor[%d]: "
526 idx
, errno
, strerror(errno
));
528 if (dst
->errors
> 1000 ||
529 (_errno
!= ENOBUFS
&& dst
->errors
> 10) ||
531 wpa_supplicant_ctrl_iface_detach(
543 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv
*priv
)
547 struct sockaddr_un from
;
548 socklen_t fromlen
= sizeof(from
);
551 wpa_printf(MSG_DEBUG
, "CTRL_IFACE - %s - wait for monitor to "
552 "attach", priv
->wpa_s
->ifname
);
553 eloop_wait_for_read_sock(priv
->sock
);
555 res
= recvfrom(priv
->sock
, buf
, sizeof(buf
) - 1, 0,
556 (struct sockaddr
*) &from
, &fromlen
);
558 perror("recvfrom(ctrl_iface)");
563 if (os_strcmp(buf
, "ATTACH") == 0) {
564 /* handle ATTACH signal of first monitor interface */
565 if (!wpa_supplicant_ctrl_iface_attach(priv
, &from
,
567 sendto(priv
->sock
, "OK\n", 3, 0,
568 (struct sockaddr
*) &from
, fromlen
);
572 sendto(priv
->sock
, "FAIL\n", 5, 0,
573 (struct sockaddr
*) &from
, fromlen
);
576 /* return FAIL for all other signals */
577 sendto(priv
->sock
, "FAIL\n", 5, 0,
578 (struct sockaddr
*) &from
, fromlen
);
584 /* Global ctrl_iface */
586 struct ctrl_iface_global_priv
{
587 struct wpa_global
*global
;
592 static void wpa_supplicant_global_ctrl_iface_receive(int sock
, void *eloop_ctx
,
595 struct wpa_global
*global
= eloop_ctx
;
598 struct sockaddr_un from
;
599 socklen_t fromlen
= sizeof(from
);
603 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
604 (struct sockaddr
*) &from
, &fromlen
);
606 perror("recvfrom(ctrl_iface)");
611 reply
= wpa_supplicant_global_ctrl_iface_process(global
, buf
,
615 sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
618 } else if (reply_len
) {
619 sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
625 struct ctrl_iface_global_priv
*
626 wpa_supplicant_global_ctrl_iface_init(struct wpa_global
*global
)
628 struct ctrl_iface_global_priv
*priv
;
629 struct sockaddr_un addr
;
631 priv
= os_zalloc(sizeof(*priv
));
634 priv
->global
= global
;
637 if (global
->params
.ctrl_interface
== NULL
)
640 wpa_printf(MSG_DEBUG
, "Global control interface '%s'",
641 global
->params
.ctrl_interface
);
643 priv
->sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
644 if (priv
->sock
< 0) {
645 perror("socket(PF_UNIX)");
649 os_memset(&addr
, 0, sizeof(addr
));
650 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
651 addr
.sun_len
= sizeof(addr
);
652 #endif /* __FreeBSD__ */
653 addr
.sun_family
= AF_UNIX
;
654 os_strlcpy(addr
.sun_path
, global
->params
.ctrl_interface
,
655 sizeof(addr
.sun_path
));
656 if (bind(priv
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
657 perror("bind(PF_UNIX)");
658 if (connect(priv
->sock
, (struct sockaddr
*) &addr
,
660 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
661 " allow connections - assuming it was left"
662 "over from forced program termination");
663 if (unlink(global
->params
.ctrl_interface
) < 0) {
664 perror("unlink[ctrl_iface]");
665 wpa_printf(MSG_ERROR
, "Could not unlink "
666 "existing ctrl_iface socket '%s'",
667 global
->params
.ctrl_interface
);
670 if (bind(priv
->sock
, (struct sockaddr
*) &addr
,
672 perror("bind(PF_UNIX)");
675 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
676 "ctrl_iface socket '%s'",
677 global
->params
.ctrl_interface
);
679 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
680 "be in use - cannot override it");
681 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
683 global
->params
.ctrl_interface
);
688 eloop_register_read_sock(priv
->sock
,
689 wpa_supplicant_global_ctrl_iface_receive
,
703 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv
*priv
)
705 if (priv
->sock
>= 0) {
706 eloop_unregister_read_sock(priv
->sock
);
709 if (priv
->global
->params
.ctrl_interface
)
710 unlink(priv
->global
->params
.ctrl_interface
);