2 * WPA Supplicant / UNIX domain socket -based control interface
3 * Copyright (c) 2004-2005, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
24 #include "eapol_supp/eapol_supp_sm.h"
25 #include "wpa_supplicant_i.h"
26 #include "ctrl_iface.h"
28 /* Per-interface ctrl_iface */
31 * struct wpa_ctrl_dst - Internal data structure of control interface monitors
33 * This structure is used to store information about registered control
34 * interface monitors into struct wpa_supplicant. This data is private to
35 * ctrl_iface_unix.c and should not be touched directly from other files.
38 struct wpa_ctrl_dst
*next
;
39 struct sockaddr_un addr
;
46 struct ctrl_iface_priv
{
47 struct wpa_supplicant
*wpa_s
;
49 struct wpa_ctrl_dst
*ctrl_dst
;
53 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv
*priv
,
54 int level
, const char *buf
,
58 static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv
*priv
,
59 struct sockaddr_un
*from
,
62 struct wpa_ctrl_dst
*dst
;
64 dst
= os_zalloc(sizeof(*dst
));
67 os_memcpy(&dst
->addr
, from
, sizeof(struct sockaddr_un
));
68 dst
->addrlen
= fromlen
;
69 dst
->debug_level
= MSG_INFO
;
70 dst
->next
= priv
->ctrl_dst
;
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
, *prev
= NULL
;
87 if (fromlen
== dst
->addrlen
&&
88 os_memcmp(from
->sun_path
, dst
->addr
.sun_path
,
89 fromlen
- offsetof(struct sockaddr_un
, sun_path
))
92 priv
->ctrl_dst
= dst
->next
;
94 prev
->next
= dst
->next
;
96 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor detached",
97 (u8
*) from
->sun_path
,
99 offsetof(struct sockaddr_un
, sun_path
));
109 static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv
*priv
,
110 struct sockaddr_un
*from
,
114 struct wpa_ctrl_dst
*dst
;
116 wpa_printf(MSG_DEBUG
, "CTRL_IFACE LEVEL %s", level
);
118 dst
= priv
->ctrl_dst
;
120 if (fromlen
== dst
->addrlen
&&
121 os_memcmp(from
->sun_path
, dst
->addr
.sun_path
,
122 fromlen
- offsetof(struct sockaddr_un
, sun_path
))
124 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE changed monitor "
125 "level", (u8
*) from
->sun_path
,
127 offsetof(struct sockaddr_un
, sun_path
));
128 dst
->debug_level
= atoi(level
);
138 static void wpa_supplicant_ctrl_iface_receive(int sock
, void *eloop_ctx
,
141 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
142 struct ctrl_iface_priv
*priv
= sock_ctx
;
145 struct sockaddr_un from
;
146 socklen_t fromlen
= sizeof(from
);
148 size_t reply_len
= 0;
149 int new_attached
= 0;
151 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
152 (struct sockaddr
*) &from
, &fromlen
);
154 perror("recvfrom(ctrl_iface)");
159 if (os_strcmp(buf
, "ATTACH") == 0) {
160 if (wpa_supplicant_ctrl_iface_attach(priv
, &from
, fromlen
))
166 } else if (os_strcmp(buf
, "DETACH") == 0) {
167 if (wpa_supplicant_ctrl_iface_detach(priv
, &from
, fromlen
))
171 } else if (os_strncmp(buf
, "LEVEL ", 6) == 0) {
172 if (wpa_supplicant_ctrl_iface_level(priv
, &from
, fromlen
,
178 reply
= wpa_supplicant_ctrl_iface_process(wpa_s
, buf
,
183 sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
186 } else if (reply_len
== 1) {
187 sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
189 } else if (reply_len
== 2) {
190 sendto(sock
, "OK\n", 3, 0, (struct sockaddr
*) &from
,
195 eapol_sm_notify_ctrl_attached(wpa_s
->eapol
);
199 static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant
*wpa_s
)
203 char *pbuf
, *dir
= NULL
, *gid_str
= NULL
;
206 if (wpa_s
->conf
->ctrl_interface
== NULL
)
209 pbuf
= os_strdup(wpa_s
->conf
->ctrl_interface
);
212 if (os_strncmp(pbuf
, "DIR=", 4) == 0) {
214 gid_str
= os_strstr(dir
, " GROUP=");
222 len
= os_strlen(dir
) + os_strlen(wpa_s
->ifname
) + 2;
223 buf
= os_malloc(len
);
229 res
= os_snprintf(buf
, len
, "%s/%s", dir
, wpa_s
->ifname
);
230 if (res
< 0 || (size_t) res
>= len
) {
237 /* Windows/WinPcap uses interface names that are not suitable
238 * as a file name - convert invalid chars to underscores */
246 #endif /* __CYGWIN__ */
252 static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx
, int level
,
253 const char *txt
, size_t len
)
255 struct wpa_supplicant
*wpa_s
= ctx
;
256 if (wpa_s
== NULL
|| wpa_s
->ctrl_iface
== NULL
)
258 wpa_supplicant_ctrl_iface_send(wpa_s
->ctrl_iface
, level
, txt
, len
);
262 struct ctrl_iface_priv
*
263 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant
*wpa_s
)
265 struct ctrl_iface_priv
*priv
;
266 struct sockaddr_un addr
;
270 char *buf
, *dir
= NULL
, *gid_str
= NULL
;
274 priv
= os_zalloc(sizeof(*priv
));
280 if (wpa_s
->conf
->ctrl_interface
== NULL
)
283 buf
= os_strdup(wpa_s
->conf
->ctrl_interface
);
286 if (os_strncmp(buf
, "DIR=", 4) == 0) {
288 gid_str
= os_strstr(dir
, " GROUP=");
295 gid_str
= wpa_s
->conf
->ctrl_interface_group
;
298 if (mkdir(dir
, S_IRWXU
| S_IRWXG
) < 0) {
299 if (errno
== EEXIST
) {
300 wpa_printf(MSG_DEBUG
, "Using existing control "
301 "interface directory.");
303 perror("mkdir[ctrl_interface]");
309 grp
= getgrnam(gid_str
);
313 wpa_printf(MSG_DEBUG
, "ctrl_interface_group=%d"
314 " (from group name '%s')",
317 /* Group name not found - try to parse this as gid */
318 gid
= strtol(gid_str
, &endp
, 10);
319 if (*gid_str
== '\0' || *endp
!= '\0') {
320 wpa_printf(MSG_ERROR
, "CTRL: Invalid group "
325 wpa_printf(MSG_DEBUG
, "ctrl_interface_group=%d",
330 if (gid_set
&& chown(dir
, -1, gid
) < 0) {
331 perror("chown[ctrl_interface]");
335 /* Make sure the group can enter and read the directory */
337 chmod(dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
) < 0) {
338 wpa_printf(MSG_ERROR
, "CTRL: chmod[ctrl_interface]: %s",
343 if (os_strlen(dir
) + 1 + os_strlen(wpa_s
->ifname
) >=
344 sizeof(addr
.sun_path
)) {
345 wpa_printf(MSG_ERROR
, "ctrl_iface path limit exceeded");
349 priv
->sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
350 if (priv
->sock
< 0) {
351 perror("socket(PF_UNIX)");
355 os_memset(&addr
, 0, sizeof(addr
));
357 addr
.sun_len
= sizeof(addr
);
358 #endif /* __FreeBSD__ */
359 addr
.sun_family
= AF_UNIX
;
360 fname
= wpa_supplicant_ctrl_iface_path(wpa_s
);
363 os_strlcpy(addr
.sun_path
, fname
, sizeof(addr
.sun_path
));
364 if (bind(priv
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
365 wpa_printf(MSG_DEBUG
, "ctrl_iface bind(PF_UNIX) failed: %s",
367 if (connect(priv
->sock
, (struct sockaddr
*) &addr
,
369 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
370 " allow connections - assuming it was left"
371 "over from forced program termination");
372 if (unlink(fname
) < 0) {
373 perror("unlink[ctrl_iface]");
374 wpa_printf(MSG_ERROR
, "Could not unlink "
375 "existing ctrl_iface socket '%s'",
379 if (bind(priv
->sock
, (struct sockaddr
*) &addr
,
381 perror("bind(PF_UNIX)");
384 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
385 "ctrl_iface socket '%s'", fname
);
387 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
388 "be in use - cannot override it");
389 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
390 "not used anymore", fname
);
397 if (gid_set
&& chown(fname
, -1, gid
) < 0) {
398 perror("chown[ctrl_interface/ifname]");
402 if (chmod(fname
, S_IRWXU
| S_IRWXG
) < 0) {
403 perror("chmod[ctrl_interface/ifname]");
408 eloop_register_read_sock(priv
->sock
, wpa_supplicant_ctrl_iface_receive
,
410 wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb
);
428 void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv
*priv
)
430 struct wpa_ctrl_dst
*dst
, *prev
;
432 if (priv
->sock
> -1) {
434 char *buf
, *dir
= NULL
, *gid_str
= NULL
;
435 eloop_unregister_read_sock(priv
->sock
);
436 if (priv
->ctrl_dst
) {
438 * Wait a second before closing the control socket if
439 * there are any attached monitors in order to allow
440 * them to receive any pending messages.
442 wpa_printf(MSG_DEBUG
, "CTRL_IFACE wait for attached "
443 "monitors to receive messages");
448 fname
= wpa_supplicant_ctrl_iface_path(priv
->wpa_s
);
454 buf
= os_strdup(priv
->wpa_s
->conf
->ctrl_interface
);
457 if (os_strncmp(buf
, "DIR=", 4) == 0) {
459 gid_str
= os_strstr(dir
, " GROUP=");
467 if (rmdir(dir
) < 0) {
468 if (errno
== ENOTEMPTY
) {
469 wpa_printf(MSG_DEBUG
, "Control interface "
470 "directory not empty - leaving it "
473 perror("rmdir[ctrl_interface]");
480 dst
= priv
->ctrl_dst
;
491 * wpa_supplicant_ctrl_iface_send - Send a control interface packet to monitors
492 * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
493 * @level: Priority level of the message
495 * @len: Message length
497 * Send a packet to all monitor programs attached to the control interface.
499 static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv
*priv
,
500 int level
, const char *buf
,
503 struct wpa_ctrl_dst
*dst
, *next
;
509 dst
= priv
->ctrl_dst
;
510 if (priv
->sock
< 0 || dst
== NULL
)
513 res
= os_snprintf(levelstr
, sizeof(levelstr
), "<%d>", level
);
514 if (res
< 0 || (size_t) res
>= sizeof(levelstr
))
516 io
[0].iov_base
= levelstr
;
517 io
[0].iov_len
= os_strlen(levelstr
);
518 io
[1].iov_base
= (char *) buf
;
520 os_memset(&msg
, 0, sizeof(msg
));
527 if (level
>= dst
->debug_level
) {
528 wpa_hexdump(MSG_DEBUG
, "CTRL_IFACE monitor send",
529 (u8
*) dst
->addr
.sun_path
, dst
->addrlen
-
530 offsetof(struct sockaddr_un
, sun_path
));
531 msg
.msg_name
= (void *) &dst
->addr
;
532 msg
.msg_namelen
= dst
->addrlen
;
533 if (sendmsg(priv
->sock
, &msg
, 0) < 0) {
535 wpa_printf(MSG_INFO
, "CTRL_IFACE monitor[%d]: "
537 idx
, errno
, strerror(errno
));
539 if (dst
->errors
> 10 || _errno
== ENOENT
) {
540 wpa_supplicant_ctrl_iface_detach(
553 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv
*priv
)
557 struct sockaddr_un from
;
558 socklen_t fromlen
= sizeof(from
);
561 wpa_printf(MSG_DEBUG
, "CTRL_IFACE - %s - wait for monitor to "
562 "attach", priv
->wpa_s
->ifname
);
563 eloop_wait_for_read_sock(priv
->sock
);
565 res
= recvfrom(priv
->sock
, buf
, sizeof(buf
) - 1, 0,
566 (struct sockaddr
*) &from
, &fromlen
);
568 perror("recvfrom(ctrl_iface)");
573 if (os_strcmp(buf
, "ATTACH") == 0) {
574 /* handle ATTACH signal of first monitor interface */
575 if (!wpa_supplicant_ctrl_iface_attach(priv
, &from
,
577 sendto(priv
->sock
, "OK\n", 3, 0,
578 (struct sockaddr
*) &from
, fromlen
);
582 sendto(priv
->sock
, "FAIL\n", 5, 0,
583 (struct sockaddr
*) &from
, fromlen
);
586 /* return FAIL for all other signals */
587 sendto(priv
->sock
, "FAIL\n", 5, 0,
588 (struct sockaddr
*) &from
, fromlen
);
594 /* Global ctrl_iface */
596 struct ctrl_iface_global_priv
{
597 struct wpa_global
*global
;
602 static void wpa_supplicant_global_ctrl_iface_receive(int sock
, void *eloop_ctx
,
605 struct wpa_global
*global
= eloop_ctx
;
608 struct sockaddr_un from
;
609 socklen_t fromlen
= sizeof(from
);
613 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
614 (struct sockaddr
*) &from
, &fromlen
);
616 perror("recvfrom(ctrl_iface)");
621 reply
= wpa_supplicant_global_ctrl_iface_process(global
, buf
,
625 sendto(sock
, reply
, reply_len
, 0, (struct sockaddr
*) &from
,
628 } else if (reply_len
) {
629 sendto(sock
, "FAIL\n", 5, 0, (struct sockaddr
*) &from
,
635 struct ctrl_iface_global_priv
*
636 wpa_supplicant_global_ctrl_iface_init(struct wpa_global
*global
)
638 struct ctrl_iface_global_priv
*priv
;
639 struct sockaddr_un addr
;
641 priv
= os_zalloc(sizeof(*priv
));
644 priv
->global
= global
;
647 if (global
->params
.ctrl_interface
== NULL
)
650 wpa_printf(MSG_DEBUG
, "Global control interface '%s'",
651 global
->params
.ctrl_interface
);
653 priv
->sock
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
654 if (priv
->sock
< 0) {
655 perror("socket(PF_UNIX)");
659 os_memset(&addr
, 0, sizeof(addr
));
661 addr
.sun_len
= sizeof(addr
);
662 #endif /* __FreeBSD__ */
663 addr
.sun_family
= AF_UNIX
;
664 os_strlcpy(addr
.sun_path
, global
->params
.ctrl_interface
,
665 sizeof(addr
.sun_path
));
666 if (bind(priv
->sock
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
667 perror("bind(PF_UNIX)");
668 if (connect(priv
->sock
, (struct sockaddr
*) &addr
,
670 wpa_printf(MSG_DEBUG
, "ctrl_iface exists, but does not"
671 " allow connections - assuming it was left"
672 "over from forced program termination");
673 if (unlink(global
->params
.ctrl_interface
) < 0) {
674 perror("unlink[ctrl_iface]");
675 wpa_printf(MSG_ERROR
, "Could not unlink "
676 "existing ctrl_iface socket '%s'",
677 global
->params
.ctrl_interface
);
680 if (bind(priv
->sock
, (struct sockaddr
*) &addr
,
682 perror("bind(PF_UNIX)");
685 wpa_printf(MSG_DEBUG
, "Successfully replaced leftover "
686 "ctrl_iface socket '%s'",
687 global
->params
.ctrl_interface
);
689 wpa_printf(MSG_INFO
, "ctrl_iface exists and seems to "
690 "be in use - cannot override it");
691 wpa_printf(MSG_INFO
, "Delete '%s' manually if it is "
693 global
->params
.ctrl_interface
);
698 eloop_register_read_sock(priv
->sock
,
699 wpa_supplicant_global_ctrl_iface_receive
,
713 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv
*priv
)
715 if (priv
->sock
>= 0) {
716 eloop_unregister_read_sock(priv
->sock
);
719 if (priv
->global
->params
.ctrl_interface
)
720 unlink(priv
->global
->params
.ctrl_interface
);